1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2023 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7
8 /// @file
9 ///
10 /// This file contains the definitions of the entry points to
11 /// de-serialize an instance of @ref abigail::corpus from a file in
12 /// elf format, containing dwarf information.
13
14 #include "abg-internal.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <libgen.h>
20 #include <assert.h>
21 #include <limits.h>
22 #include <elfutils/libdwfl.h>
23 #include <dwarf.h>
24 #include <algorithm>
25 #include <cmath>
26 #include <cstring>
27 #include <deque>
28 #include <list>
29 #include <memory>
30 #include <ostream>
31 #include <sstream>
32 #include <stack>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <map>
36
37 #include "abg-ir-priv.h"
38 #include "abg-suppression-priv.h"
39 #include "abg-corpus-priv.h"
40 #include "abg-symtab-reader.h"
41
42 // <headers defining libabigail's API go under here>
43 ABG_BEGIN_EXPORT_DECLARATIONS
44
45 #include "abg-dwarf-reader.h"
46 #include "abg-elf-based-reader.h"
47 #include "abg-sptr-utils.h"
48 #include "abg-tools-utils.h"
49 #include "abg-elf-helpers.h"
50
51 ABG_END_EXPORT_DECLARATIONS
52 // </headers defining libabigail's API>
53
54 #ifndef UINT64_MAX
55 #define UINT64_MAX 0xffffffffffffffff
56 #endif
57
58 using std::string;
59
60 namespace abigail
61 {
62
63 using std::cerr;
64
65 /// The namespace for the DWARF reader.
66 namespace dwarf
67 {
68
69 using std::dynamic_pointer_cast;
70 using std::static_pointer_cast;
71 using std::unordered_map;
72 using std::unordered_set;
73 using std::stack;
74 using std::deque;
75 using std::list;
76 using std::map;
77 using abg_compat::optional;
78
79 using namespace elf_helpers; // TODO: avoid using namespace
80
81 /// Where a DIE comes from. For instance, a DIE can come from the main
82 /// debug info section, the alternate debug info section or from the
83 /// type unit section.
84 enum die_source
85 {
86 NO_DEBUG_INFO_DIE_SOURCE,
87 PRIMARY_DEBUG_INFO_DIE_SOURCE,
88 ALT_DEBUG_INFO_DIE_SOURCE,
89 TYPE_UNIT_DIE_SOURCE,
90 NUMBER_OF_DIE_SOURCES, // This one must always be the latest
91 // enumerator
92 };
93
94
95 /// A convenience typedef for a vector of Dwarf_Off.
96 typedef vector<Dwarf_Off> dwarf_offsets_type;
97
98 /// Convenience typedef for a map which key is the offset of a dwarf
99 /// die and which value is the corresponding artefact.
100 typedef unordered_map<Dwarf_Off, type_or_decl_base_sptr> die_artefact_map_type;
101
102 /// Convenience typedef for a map which key is the offset of a dwarf
103 /// die, (given by dwarf_dieoffset()) and which value is the
104 /// corresponding class_decl.
105 typedef unordered_map<Dwarf_Off, class_decl_sptr> die_class_map_type;
106
107 /// Convenience typedef for a map which key is the offset of a dwarf
108 /// die, (given by dwarf_dieoffset()) and which value is the
109 /// corresponding class_or_union_sptr.
110 typedef unordered_map<Dwarf_Off, class_or_union_sptr> die_class_or_union_map_type;
111
112 /// Convenience typedef for a map which key the offset of a dwarf die
113 /// and which value is the corresponding function_decl.
114 typedef unordered_map<Dwarf_Off, function_decl_sptr> die_function_decl_map_type;
115
116 /// Convenience typedef for a map which key is the offset of a dwarf
117 /// die and which value is the corresponding function_type.
118 typedef unordered_map<Dwarf_Off, function_type_sptr> die_function_type_map_type;
119
120 /// Convenience typedef for a map which key is the offset of a
121 /// DW_TAG_compile_unit and the value is the corresponding @ref
122 /// translation_unit_sptr.
123 typedef unordered_map<Dwarf_Off, translation_unit_sptr> die_tu_map_type;
124
125 /// Convenience typedef for a map which key is the offset of a DIE and
126 /// the value is the corresponding qualified name of the DIE.
127 typedef unordered_map<Dwarf_Off, interned_string> die_istring_map_type;
128
129 /// Convenience typedef for a map which is an interned_string and
130 /// which value is a vector of offsets.
131 typedef unordered_map<interned_string,
132 dwarf_offsets_type,
133 hash_interned_string>
134 istring_dwarf_offsets_map_type;
135
136 /// A hasher for a pair of Dwarf_Off. This is used as a hasher for
137 /// the type @ref dwarf_offset_pair_set_type.
138 struct dwarf_offset_pair_hash
139 {
140 size_t
operator ()abigail::dwarf::dwarf_offset_pair_hash141 operator()(const std::pair<Dwarf_Off, Dwarf_Off>& p) const
142 {return abigail::hashing::combine_hashes(p.first, p.second);}
143 };// end struct dwarf_offset_pair_hash
144
145 typedef unordered_set<std::pair<Dwarf_Off,
146 Dwarf_Off>,
147 dwarf_offset_pair_hash> dwarf_offset_pair_set_type;
148
149 /// An abstraction of a DIE offset that also encapsulate the source of
150 /// the DIE.
151 struct offset_type
152 {
153 die_source source_;
154 Dwarf_Off offset_;
155
offset_typeabigail::dwarf::offset_type156 offset_type()
157 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
158 offset_(0)
159 {}
160
offset_typeabigail::dwarf::offset_type161 offset_type(die_source source, Dwarf_Off offset)
162 : source_(source),
163 offset_(offset)
164 {}
165
offset_typeabigail::dwarf::offset_type166 offset_type(Dwarf_Off offset)
167 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
168 offset_(offset)
169 {}
170
operator ==abigail::dwarf::offset_type171 bool operator==(const offset_type& o) const
172 {return source_ == o.source_ && offset_ == o.offset_;}
173
operator Dwarf_Offabigail::dwarf::offset_type174 operator Dwarf_Off() const
175 {return offset_;}
176 }; // end struct offset_type
177
178 /// A convenience typedef for a pair of offset_type.
179 typedef std::pair<offset_type, offset_type> offset_pair_type;
180
181 /// A hasher for an instance of offset_type.
182 struct offset_hash
183 {
184 size_t
operator ()abigail::dwarf::offset_hash185 operator()(const offset_type& p) const
186 {return abigail::hashing::combine_hashes(p.source_, p.offset_);}
187 };// end struct offset_hash
188
189 /// A hasher for a pair of offset_type. This is used as a hasher for
190 /// the type @ref offset_pair_set_type, for instance.
191 struct offset_pair_hash
192 {
193 size_t
operator ()abigail::dwarf::offset_pair_hash194 operator()(const std::pair<offset_type, offset_type>& p) const
195 {
196 size_t h1 = abigail::hashing::combine_hashes(p.first.source_,
197 p.first.offset_);
198 size_t h2 = abigail::hashing::combine_hashes(p.second.source_,
199 p.second.offset_);
200 return abigail::hashing::combine_hashes(h1, h2);
201 }
202 };// end struct offset_pair_hash
203
204 /// A convenience typedef for an unordered set of DIE offsets.
205 typedef unordered_set<offset_type, offset_hash> offset_set_type;
206
207 ///A convenience typedef for an unordered set of pairs of offset_type.
208 typedef unordered_set<std::pair<offset_type,
209 offset_type>,
210 offset_pair_hash> offset_pair_set_type;
211
212 /// A convenience typedef for a vector of pairs of offset_type.
213 typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
214
215 /// A convenience typedef for an unordered map that associates a pair
216 /// of offset_type to a vector of pairs offset_type.
217 typedef unordered_map<std::pair<offset_type, offset_type>,
218 offset_pair_vector_type,
219 offset_pair_hash> offset_pair_vect_map_type;
220
221 /// A convenience typedef for an unordered_map that associates a pair
222 /// of offset_type to a set of pairs of offset_type.
223 typedef unordered_map<std::pair<offset_type, offset_type>,
224 offset_pair_set_type,
225 offset_pair_hash> offset_pair_set_map_type;
226
227 /// A convenience typedef for a vector of pairs of offset_type.
228 typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
229
230 class reader;
231
232 static translation_unit_sptr
233 build_translation_unit_and_add_to_ir(reader& rdr,
234 Dwarf_Die* die,
235 char address_size);
236
237 static void
238 maybe_propagate_canonical_type(const reader& rdr,
239 const Dwarf_Die* l,
240 const Dwarf_Die* r);
241
242 static void
243 propagate_canonical_type(const reader& rdr,
244 const Dwarf_Die* l,
245 const Dwarf_Die* r);
246
247 /// Convenience typedef for a shared pointer to an
248 /// addr_elf_symbol_sptr_map_type.
249 typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
250
251 /// Convenience typedef for a map that associates an @ref
252 /// interned_string to a @ref function_type_sptr.
253 typedef unordered_map<interned_string,
254 function_type_sptr,
255 hash_interned_string> istring_fn_type_map_type;
256
257 /// Convenience typedef for a stack containing the scopes up to the
258 /// current point in the abigail Internal Representation (aka IR) tree
259 /// that is being built.
260 typedef stack<scope_decl*> scope_stack_type;
261
262 /// Convenience typedef for a map which key is a dwarf offset. The
263 /// value is also a dwarf offset.
264 typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
265
266 /// Convenience typedef for a map which key is a string and which
267 /// value is a vector of smart pointer to a class_or_union_sptr.
268 typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_map;
269
270 /// Convenience typedef for a map which key is a string and which
271 /// value is a vector of smart pointer to a class.
272 typedef unordered_map<string, classes_type> string_classes_map;
273
274 /// Convenience typedef for a map which key is a string and which
275 /// value is a vector of smart pointer to a enum.
276 typedef unordered_map<string, enums_type> string_enums_map;
277
278 /// The abstraction of the place where a partial unit has been
279 /// imported. This is what the DW_TAG_imported_unit DIE expresses.
280 ///
281 /// This type thus contains:
282 /// - the offset to which the partial unit is imported
283 /// - the offset of the imported partial unit.
284 /// - the offset of the imported partial unit.
285 struct imported_unit_point
286 {
287 Dwarf_Off offset_of_import;
288 // The boolean below is true iff the imported unit comes from the
289 // alternate debug info file.
290 die_source imported_unit_die_source;
291 Dwarf_Off imported_unit_die_off;
292 Dwarf_Off imported_unit_cu_off;
293 Dwarf_Off imported_unit_child_off;
294
295 /// Default constructor for @ref the type imported_unit_point.
imported_unit_pointabigail::dwarf::imported_unit_point296 imported_unit_point()
297 : offset_of_import(),
298 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
299 imported_unit_die_off(),
300 imported_unit_cu_off(),
301 imported_unit_child_off()
302 {}
303
304 /// Constructor of @ref the type imported_unit_point.
305 ///
306 /// @param import_off the offset of the point at which the unit has
307 /// been imported.
imported_unit_pointabigail::dwarf::imported_unit_point308 imported_unit_point(Dwarf_Off import_off)
309 : offset_of_import(import_off),
310 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
311 imported_unit_die_off(),
312 imported_unit_cu_off(),
313 imported_unit_child_off()
314 {}
315
316 /// Constructor of @ref the type imported_unit_point.
317 ///
318 /// @param import_off the offset of the point at which the unit has
319 /// been imported.
320 ///
321 /// @param from where the imported DIE comes from.
322 ///
323 /// @param imported_die the die of the unit that has been imported.
imported_unit_pointabigail::dwarf::imported_unit_point324 imported_unit_point(Dwarf_Off import_off,
325 const Dwarf_Die& imported_die,
326 die_source from)
327 : offset_of_import(import_off),
328 imported_unit_die_source(from),
329 imported_unit_die_off(dwarf_dieoffset
330 (const_cast<Dwarf_Die*>(&imported_die))),
331 imported_unit_cu_off(),
332 imported_unit_child_off()
333 {
334 Dwarf_Die imported_unit_child;
335
336 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
337 &imported_unit_child) == 0);
338
339 imported_unit_child_off =
340 dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
341
342 Dwarf_Die cu_die_memory;
343 Dwarf_Die *cu_die;
344
345 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
346 &cu_die_memory, 0, 0);
347 imported_unit_cu_off = dwarf_dieoffset(cu_die);
348 }
349 }; // struct imported_unit_point
350
351 /// Convenience typedef for a vector of @ref imported_unit_point.
352 typedef vector<imported_unit_point> imported_unit_points_type;
353
354 /// Convenience typedef for a vector of @ref imported_unit_point.
355 typedef unordered_map<Dwarf_Off, imported_unit_points_type>
356 tu_die_imported_unit_points_map_type;
357
358 /// "Less than" operator for instances of @ref imported_unit_point
359 /// type.
360 ///
361 /// @param the left hand side operand of the "Less than" operator.
362 ///
363 /// @param the right hand side operand of the "Less than" operator.
364 ///
365 /// @return true iff @p l is less than @p r.
366 static bool
operator <(const imported_unit_point & l,const imported_unit_point & r)367 operator<(const imported_unit_point& l, const imported_unit_point& r)
368 {return l.offset_of_import < r.offset_of_import;}
369
370 static bool
371 get_parent_die(const reader& rdr,
372 const Dwarf_Die* die,
373 Dwarf_Die& parent_die,
374 size_t where_offset);
375
376 static bool
377 get_scope_die(const reader& rdr,
378 const Dwarf_Die* die,
379 size_t where_offset,
380 Dwarf_Die& scope_die);
381
382 static bool
383 die_is_anonymous(const Dwarf_Die* die);
384
385 static bool
386 die_is_anonymous_data_member(const Dwarf_Die* die);
387
388 static bool
389 die_is_type(const Dwarf_Die* die);
390
391 static bool
392 die_is_decl(const Dwarf_Die* die);
393
394 static bool
395 die_is_declaration_only(Dwarf_Die* die);
396
397 static bool
398 die_is_variable_decl(const Dwarf_Die *die);
399
400 static bool
401 die_is_function_decl(const Dwarf_Die *die);
402
403 static bool
404 die_has_size_attribute(const Dwarf_Die *die);
405
406 static bool
407 die_has_no_child(const Dwarf_Die *die);
408
409 static bool
410 die_is_namespace(const Dwarf_Die* die);
411
412 static bool
413 die_is_unspecified(Dwarf_Die* die);
414
415 static bool
416 die_is_void_type(Dwarf_Die* die);
417
418 static bool
419 die_is_pointer_type(const Dwarf_Die* die);
420
421 static bool
422 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
423
424 static bool
425 die_is_reference_type(const Dwarf_Die* die);
426
427 static bool
428 die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
429
430 static bool
431 die_is_pointer_or_reference_type(const Dwarf_Die* die);
432
433 static bool
434 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
435
436 static bool
437 die_is_class_type(const Dwarf_Die* die);
438
439 static bool
440 die_is_qualified_type(const Dwarf_Die* die);
441
442 static bool
443 die_is_function_type(const Dwarf_Die *die);
444
445 static bool
446 die_has_object_pointer(const Dwarf_Die* die,
447 Dwarf_Die& object_pointer);
448
449 static bool
450 die_has_children(const Dwarf_Die* die);
451
452 static bool
453 die_this_pointer_from_object_pointer(Dwarf_Die* die,
454 Dwarf_Die& this_pointer);
455
456 static bool
457 die_this_pointer_is_const(Dwarf_Die* die);
458
459 static bool
460 die_object_pointer_is_for_const_method(Dwarf_Die* die);
461
462 static bool
463 is_type_die_to_be_canonicalized(const Dwarf_Die *die);
464
465 static bool
466 die_is_at_class_scope(const reader& rdr,
467 const Dwarf_Die* die,
468 size_t where_offset,
469 Dwarf_Die& class_scope_die);
470 static bool
471 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
472 size_t expr_len,
473 int64_t& value,
474 bool& is_tls_address);
475
476 static translation_unit::language
477 dwarf_language_to_tu_language(size_t l);
478
479 static bool
480 die_unsigned_constant_attribute(const Dwarf_Die* die,
481 unsigned attr_name,
482 uint64_t& cst);
483
484 static bool
485 die_signed_constant_attribute(const Dwarf_Die*die,
486 unsigned attr_name,
487 int64_t& cst);
488
489 static bool
490 die_constant_attribute(const Dwarf_Die *die,
491 unsigned attr_name,
492 bool is_signed,
493 array_type_def::subrange_type::bound_value &value);
494
495 static bool
496 die_member_offset(const reader& rdr,
497 const Dwarf_Die* die,
498 int64_t& offset);
499
500 static bool
501 form_is_DW_FORM_strx(unsigned form);
502
503 static bool
504 form_is_DW_FORM_line_strp(unsigned form);
505
506 static bool
507 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
508
509 static string
510 die_name(const Dwarf_Die* die);
511
512 static location
513 die_location(const reader& rdr, const Dwarf_Die* die);
514
515 static bool
516 die_location_address(Dwarf_Die* die,
517 Dwarf_Addr& address,
518 bool& is_tls_address);
519
520 static bool
521 die_die_attribute(const Dwarf_Die* die,
522 unsigned attr_name,
523 Dwarf_Die& result,
524 bool recursively = true);
525
526 static bool
527 subrange_die_indirect_bound_value(const Dwarf_Die *die,
528 unsigned attr_name,
529 array_type_def::subrange_type::bound_value& v,
530 bool& is_signed);
531
532 static bool
533 subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
534 unsigned attr_name,
535 Dwarf_Die& referenced_subrange);
536 static string
537 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
538
539 static string
540 build_internal_anonymous_die_name(const string &base_name,
541 size_t anonymous_type_index);
542
543 static string
544 get_internal_anonymous_die_name(Dwarf_Die *die,
545 size_t anonymous_type_index);
546
547 static string
548 die_qualified_type_name(const reader& rdr,
549 const Dwarf_Die* die,
550 size_t where);
551
552 static string
553 die_qualified_decl_name(const reader& rdr,
554 const Dwarf_Die* die,
555 size_t where);
556
557 static string
558 die_qualified_name(const reader& rdr,
559 const Dwarf_Die* die,
560 size_t where);
561
562 static bool
563 die_qualified_type_name_empty(const reader& rdr,
564 const Dwarf_Die* die, size_t where,
565 string &qualified_name);
566
567 static void
568 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
569 const Dwarf_Die* die,
570 size_t where_offset,
571 bool pretty_print,
572 string &return_type_name,
573 string &class_name,
574 vector<string>& parm_names,
575 bool& is_const,
576 bool& is_static);
577
578 static string
579 die_function_signature(const reader& rdr,
580 const Dwarf_Die *die,
581 size_t where_offset);
582
583 static bool
584 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
585
586 static bool
587 die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
588
589 static bool
590 die_function_type_is_method_type(const reader& rdr,
591 const Dwarf_Die *die,
592 size_t where_offset,
593 Dwarf_Die& object_pointer_die,
594 Dwarf_Die& class_die,
595 bool& is_static);
596
597 static string
598 die_pretty_print_type(reader& rdr,
599 const Dwarf_Die* die,
600 size_t where_offset);
601
602 static string
603 die_pretty_print_decl(reader& rdr,
604 const Dwarf_Die* die,
605 size_t where_offset);
606
607 static string
608 die_pretty_print(reader& rdr,
609 const Dwarf_Die* die,
610 size_t where_offset);
611
612 static void
613 maybe_canonicalize_type(const type_base_sptr& t,
614 reader& rdr);
615
616 static uint64_t
617 get_default_array_lower_bound(translation_unit::language l);
618
619 static bool
620 find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
621 Dwarf_Off,
622 imported_unit_points_type::const_iterator&);
623
624 static array_type_def::subrange_sptr
625 build_subrange_type(reader& rdr,
626 const Dwarf_Die* die,
627 size_t where_offset,
628 bool associate_type_to_die = true);
629
630 static void
631 build_subranges_from_array_type_die(reader& rdr,
632 const Dwarf_Die* die,
633 array_type_def::subranges_type& subranges,
634 size_t where_offset,
635 bool associate_type_to_die = true);
636
637 static comparison_result
638 compare_dies(const reader& rdr,
639 const Dwarf_Die *l, const Dwarf_Die *r,
640 bool update_canonical_dies_on_the_fly);
641
642 static bool
643 compare_dies_during_canonicalization(reader& rdr,
644 const Dwarf_Die *l, const Dwarf_Die *r,
645 bool update_canonical_dies_on_the_fly);
646
647 static bool
648 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
649
650 /// Compare a symbol name against another name, possibly demangling
651 /// the symbol_name before performing the comparison.
652 ///
653 /// @param symbol_name the symbol_name to take in account.
654 ///
655 /// @param name the second name to take in account.
656 ///
657 /// @param demangle if true, demangle @p symbol_name and compare the
658 /// result of the demangling with @p name.
659 ///
660 /// @return true iff symbol_name equals name.
661 static bool
compare_symbol_name(const string & symbol_name,const string & name,bool demangle)662 compare_symbol_name(const string& symbol_name,
663 const string& name,
664 bool demangle)
665 {
666 if (demangle)
667 {
668 string m = demangle_cplus_mangled_name(symbol_name);
669 return m == name;
670 }
671 return symbol_name == name;
672 }
673
674 /// Lookup a symbol using the SysV ELF hash table.
675 ///
676 /// Note that this function hasn't been tested. So it hasn't been
677 /// debugged yet. IOW, it is not known to work. Or rather, it's
678 /// almost like it's surely doesn't work ;-)
679 ///
680 /// Use it at your own risks. :-)
681 ///
682 ///@parm env the environment we are operating from.
683 ///
684 /// @param elf_handle the elf_handle to use.
685 ///
686 /// @param sym_name the symbol name to look for.
687 ///
688 /// @param ht_index the index (in the section headers table) of the
689 /// hash table section to use.
690 ///
691 /// @param sym_tab_index the index (in the section headers table) of
692 /// the symbol table to use.
693 ///
694 /// @param demangle if true, demangle @p sym_name before comparing it
695 /// to names from the symbol table.
696 ///
697 /// @param syms_found a vector of symbols found with the name @p
698 /// sym_name. table.
699 static bool
lookup_symbol_from_sysv_hash_tab(const environment & env,Elf * elf_handle,const string & sym_name,size_t ht_index,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)700 lookup_symbol_from_sysv_hash_tab(const environment& env,
701 Elf* elf_handle,
702 const string& sym_name,
703 size_t ht_index,
704 size_t sym_tab_index,
705 bool demangle,
706 vector<elf_symbol_sptr>& syms_found)
707 {
708 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
709 ABG_ASSERT(sym_tab_section);
710
711 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
712 ABG_ASSERT(sym_tab_data);
713
714 GElf_Shdr sheader_mem;
715 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
716 &sheader_mem);
717 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
718 ABG_ASSERT(hash_section);
719
720 // Poke at the different parts of the hash table and get them ready
721 // to be used.
722 unsigned long hash = elf_hash(sym_name.c_str());
723 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
724 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
725 size_t nb_buckets = ht_data[0];
726 size_t nb_chains = ht_data[1];
727
728 if (nb_buckets == 0)
729 // An empty hash table. Not sure if that is possible, but it
730 // would mean an empty table of exported symbols.
731 return false;
732
733 //size_t nb_chains = ht_data[1];
734 Elf32_Word* ht_buckets = &ht_data[2];
735 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
736
737 // Now do the real work.
738 size_t bucket = hash % nb_buckets;
739 size_t symbol_index = ht_buckets[bucket];
740
741 GElf_Sym symbol;
742 const char* sym_name_str;
743 size_t sym_size;
744 elf_symbol::type sym_type;
745 elf_symbol::binding sym_binding;
746 elf_symbol::visibility sym_visibility;
747 bool found = false;
748
749 do
750 {
751 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
752 sym_name_str = elf_strptr(elf_handle,
753 sym_tab_section_header->sh_link,
754 symbol.st_name);
755 if (sym_name_str
756 && compare_symbol_name(sym_name_str, sym_name, demangle))
757 {
758 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
759 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
760 sym_visibility =
761 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
762 sym_size = symbol.st_size;
763 elf_symbol::version ver;
764 if (get_version_for_symbol(elf_handle, symbol_index,
765 /*get_def_version=*/true, ver))
766 ABG_ASSERT(!ver.str().empty());
767 elf_symbol_sptr symbol_found =
768 elf_symbol::create(env,
769 symbol_index,
770 sym_size,
771 sym_name_str,
772 sym_type,
773 sym_binding,
774 symbol.st_shndx != SHN_UNDEF,
775 symbol.st_shndx == SHN_COMMON,
776 ver, sym_visibility);
777 syms_found.push_back(symbol_found);
778 found = true;
779 }
780 symbol_index = ht_chains[symbol_index];
781 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
782
783 return found;
784 }
785
786 /// Get the size of the elf class, in bytes.
787 ///
788 /// @param elf_handle the elf handle to use.
789 ///
790 /// @return the size computed.
791 static char
get_elf_class_size_in_bytes(Elf * elf_handle)792 get_elf_class_size_in_bytes(Elf* elf_handle)
793 {
794 char result = 0;
795 GElf_Ehdr hdr;
796
797 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
798 int c = hdr.e_ident[EI_CLASS];
799
800 switch (c)
801 {
802 case ELFCLASS32:
803 result = 4;
804 break;
805 case ELFCLASS64:
806 result = 8;
807 break;
808 default:
809 ABG_ASSERT_NOT_REACHED;
810 }
811
812 return result;
813 }
814
815 /// Get a given word of a bloom filter, referred to by the index of
816 /// the word.
817 ///
818 /// The bloom word size depends on the current elf class (32 bits for
819 /// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
820 /// abstracts that nicely.
821 ///
822 /// @param elf_handle the elf handle to use.
823 ///
824 /// @param bloom_filter the bloom filter to consider.
825 ///
826 /// @param index the index of the bloom filter to return.
827 ///
828 /// @return a 64 bits work containing the bloom word found at index @p
829 /// index. Note that if we are looking at an ELFCLASS32 binary, the 4
830 /// most significant bytes of the result are going to be zero.
831 static Elf64_Xword
bloom_word_at(Elf * elf_handle,Elf32_Word * bloom_filter,size_t index)832 bloom_word_at(Elf* elf_handle,
833 Elf32_Word* bloom_filter,
834 size_t index)
835 {
836 Elf64_Xword result = 0;
837 GElf_Ehdr h;
838 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
839 int c;
840 c = h.e_ident[EI_CLASS];
841
842 switch(c)
843 {
844 case ELFCLASS32:
845 result = bloom_filter[index];
846 break ;
847 case ELFCLASS64:
848 {
849 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
850 result = f[index];
851 }
852 break;
853 default:
854 abort();
855 }
856
857 return result;
858 }
859
860 /// The abstraction of the gnu elf hash table.
861 ///
862 /// The members of this struct are explained at
863 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
864 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
865 struct gnu_ht
866 {
867 size_t nb_buckets;
868 Elf32_Word* buckets;
869 Elf32_Word* chain;
870 size_t first_sym_index;
871 size_t bf_nwords;
872 size_t bf_size;
873 Elf32_Word* bloom_filter;
874 size_t shift;
875 size_t sym_count;
876 Elf_Scn* sym_tab_section;
877 GElf_Shdr sym_tab_section_header;
878
gnu_htabigail::dwarf::gnu_ht879 gnu_ht()
880 : nb_buckets(0),
881 buckets(0),
882 chain(0),
883 first_sym_index(0),
884 bf_nwords(0),
885 bf_size(0),
886 bloom_filter(0),
887 shift(0),
888 sym_count(0),
889 sym_tab_section(0)
890 {}
891 }; // end struct gnu_ht
892
893 /// Setup the members of the gnu hash table.
894 ///
895 /// @param elf_handle a handle on the elf file to use.
896 ///
897 /// @param ht_index the index (into the elf section headers table) of
898 /// the hash table section to use.
899 ///
900 /// @param sym_tab_index the index (into the elf section headers
901 /// table) of the symbol table the gnu hash table is about.
902 ///
903 /// @param ht the resulting hash table.
904 ///
905 /// @return true iff the hash table @ ht could be setup.
906 static bool
setup_gnu_ht(Elf * elf_handle,size_t ht_index,size_t sym_tab_index,gnu_ht & ht)907 setup_gnu_ht(Elf* elf_handle,
908 size_t ht_index,
909 size_t sym_tab_index,
910 gnu_ht& ht)
911 {
912 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
913 ABG_ASSERT(ht.sym_tab_section);
914 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
915 ht.sym_count =
916 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
917 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
918 ABG_ASSERT(hash_section);
919
920 // Poke at the different parts of the hash table and get them ready
921 // to be used.
922 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
923 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
924
925 ht.nb_buckets = ht_data[0];
926 if (ht.nb_buckets == 0)
927 // An empty hash table. Not sure if that is possible, but it
928 // would mean an empty table of exported symbols.
929 return false;
930 ht.first_sym_index = ht_data[1];
931 // The number of words used by the bloom filter. A size of a word
932 // is ELFCLASS.
933 ht.bf_nwords = ht_data[2];
934 // The shift used by the bloom filter code.
935 ht.shift = ht_data[3];
936 // The data of the bloom filter proper.
937 ht.bloom_filter = &ht_data[4];
938 // The size of the bloom filter in 4 bytes word. This is going to
939 // be used to index the 'bloom_filter' above, which is of type
940 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
941 // words.
942 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
943 // The buckets of the hash table.
944 ht.buckets = ht.bloom_filter + ht.bf_size;
945 // The chain of the hash table.
946 ht.chain = ht.buckets + ht.nb_buckets;
947
948 return true;
949 }
950
951 /// Look into the symbol tables of the underlying elf file and find
952 /// the symbol we are being asked.
953 ///
954 /// This function uses the GNU hash table for the symbol lookup.
955 ///
956 /// The reference of for the implementation of this function can be
957 /// found at:
958 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
959 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
960 ///
961 /// @param elf_handle the elf handle to use.
962 ///
963 /// @param sym_name the name of the symbol to look for.
964 ///
965 /// @param ht_index the index of the hash table header to use.
966 ///
967 /// @param sym_tab_index the index of the symbol table header to use
968 /// with this hash table.
969 ///
970 /// @param demangle if true, demangle @p sym_name.
971 ///
972 /// @param syms_found the vector of symbols found with the name @p
973 /// sym_name.
974 ///
975 /// @return true if a symbol was actually found.
976 static bool
lookup_symbol_from_gnu_hash_tab(const environment & env,Elf * elf_handle,const string & sym_name,size_t ht_index,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)977 lookup_symbol_from_gnu_hash_tab(const environment& env,
978 Elf* elf_handle,
979 const string& sym_name,
980 size_t ht_index,
981 size_t sym_tab_index,
982 bool demangle,
983 vector<elf_symbol_sptr>& syms_found)
984 {
985 gnu_ht ht;
986 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
987 return false;
988
989 // Now do the real work.
990
991 // Compute bloom hashes (GNU hash and second bloom specific hashes).
992 size_t h1 = elf_gnu_hash(sym_name.c_str());
993 size_t h2 = h1 >> ht.shift;
994 // The size of one of the words used in the bloom
995 // filter, in bits.
996 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
997 int n = (h1 / c) % ht.bf_nwords;
998 // The bitmask of the bloom filter has a size of either 32-bits on
999 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1000 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1001 // type used here. When dealing with 32bits binaries, the upper
1002 // bits of the bitmask will be zero anyway.
1003 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1004
1005 // Test if the symbol is *NOT* present in this ELF file.
1006 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1007 return false;
1008
1009 size_t i = ht.buckets[h1 % ht.nb_buckets];
1010 if (i == STN_UNDEF)
1011 return false;
1012
1013 Elf32_Word stop_word, *stop_wordp;
1014 elf_symbol::version ver;
1015 GElf_Sym symbol;
1016 const char* sym_name_str;
1017 bool found = false;
1018
1019 elf_symbol::type sym_type;
1020 elf_symbol::binding sym_binding;
1021 elf_symbol::visibility sym_visibility;
1022
1023 // Let's walk the hash table and record the versions of all the
1024 // symbols which name equal sym_name.
1025 for (i = ht.buckets[h1 % ht.nb_buckets],
1026 stop_wordp = &ht.chain[i - ht.first_sym_index];
1027 i != STN_UNDEF
1028 && (stop_wordp
1029 < ht.chain + (ht.sym_count - ht.first_sym_index));
1030 ++i, ++stop_wordp)
1031 {
1032 stop_word = *stop_wordp;
1033 if ((stop_word & ~ 1)!= (h1 & ~1))
1034 // A given bucket can reference several hashes. Here we
1035 // stumbled across a hash value different from the one we are
1036 // looking for. Let's keep walking.
1037 continue;
1038
1039 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1040 i, &symbol));
1041 sym_name_str = elf_strptr(elf_handle,
1042 ht.sym_tab_section_header.sh_link,
1043 symbol.st_name);
1044 if (sym_name_str
1045 && compare_symbol_name(sym_name_str, sym_name, demangle))
1046 {
1047 // So we found a symbol (in the symbol table) that equals
1048 // sym_name. Now lets try to get its version and record it.
1049 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1050 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1051 sym_visibility =
1052 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1053
1054 if (get_version_for_symbol(elf_handle, i,
1055 /*get_def_version=*/true,
1056 ver))
1057 ABG_ASSERT(!ver.str().empty());
1058
1059 elf_symbol_sptr symbol_found =
1060 elf_symbol::create(env, i,
1061 symbol.st_size,
1062 sym_name_str,
1063 sym_type, sym_binding,
1064 symbol.st_shndx != SHN_UNDEF,
1065 symbol.st_shndx == SHN_COMMON,
1066 ver, sym_visibility);
1067 syms_found.push_back(symbol_found);
1068 found = true;
1069 }
1070
1071 if (stop_word & 1)
1072 // The last bit of the stop_word is 1. That means we need to
1073 // stop here. We reached the end of the chain of values
1074 // referenced by the hask bucket.
1075 break;
1076 }
1077 return found;
1078 }
1079
1080 /// Look into the symbol tables of the underlying elf file and find
1081 /// the symbol we are being asked.
1082 ///
1083 /// This function uses the elf hash table (be it the GNU hash table or
1084 /// the sysv hash table) for the symbol lookup.
1085 ///
1086 /// @param env the environment we are operating from.
1087 ///
1088 /// @param elf_handle the elf handle to use.
1089 ///
1090 /// @param ht_kind the kind of hash table to use. This is returned by
1091 /// the function function find_hash_table_section_index.
1092 ///
1093 /// @param ht_index the index (in the section headers table) of the
1094 /// hash table section to use.
1095 ///
1096 /// @param sym_tab_index the index (in section headers table) of the
1097 /// symbol table index to use with this hash table.
1098 ///
1099 /// @param symbol_name the name of the symbol to look for.
1100 ///
1101 /// @param demangle if true, demangle @p sym_name.
1102 ///
1103 /// @param syms_found the symbols that were actually found with the
1104 /// name @p symbol_name.
1105 ///
1106 /// @return true iff the function found the symbol from the elf hash
1107 /// table.
1108 static bool
lookup_symbol_from_elf_hash_tab(const environment & env,Elf * elf_handle,hash_table_kind ht_kind,size_t ht_index,size_t symtab_index,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms_found)1109 lookup_symbol_from_elf_hash_tab(const environment& env,
1110 Elf* elf_handle,
1111 hash_table_kind ht_kind,
1112 size_t ht_index,
1113 size_t symtab_index,
1114 const string& symbol_name,
1115 bool demangle,
1116 vector<elf_symbol_sptr>& syms_found)
1117 {
1118 if (elf_handle == 0 || symbol_name.empty())
1119 return false;
1120
1121 if (ht_kind == NO_HASH_TABLE_KIND)
1122 return false;
1123
1124 if (ht_kind == SYSV_HASH_TABLE_KIND)
1125 return lookup_symbol_from_sysv_hash_tab(env,
1126 elf_handle, symbol_name,
1127 ht_index,
1128 symtab_index,
1129 demangle,
1130 syms_found);
1131 else if (ht_kind == GNU_HASH_TABLE_KIND)
1132 return lookup_symbol_from_gnu_hash_tab(env,
1133 elf_handle, symbol_name,
1134 ht_index,
1135 symtab_index,
1136 demangle,
1137 syms_found);
1138 return false;
1139 }
1140
1141 /// Lookup a symbol from the symbol table directly.
1142 ///
1143 ///
1144 /// @param env the environment we are operating from.
1145 ///
1146 /// @param elf_handle the elf handle to use.
1147 ///
1148 /// @param sym_name the name of the symbol to look up.
1149 ///
1150 /// @param sym_tab_index the index (in the section headers table) of
1151 /// the symbol table section.
1152 ///
1153 /// @param demangle if true, demangle the names found in the symbol
1154 /// table before comparing them with @p sym_name.
1155 ///
1156 /// @param sym_name_found the actual name of the symbol found.
1157 ///
1158 /// @param sym_type the type of the symbol found.
1159 ///
1160 /// @param sym_binding the binding of the symbol found.
1161 ///
1162 /// @param sym_versions the versions of the symbol found.
1163 ///
1164 /// @return true iff the symbol was found.
1165 static bool
lookup_symbol_from_symtab(const environment & env,Elf * elf_handle,const string & sym_name,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)1166 lookup_symbol_from_symtab(const environment& env,
1167 Elf* elf_handle,
1168 const string& sym_name,
1169 size_t sym_tab_index,
1170 bool demangle,
1171 vector<elf_symbol_sptr>& syms_found)
1172 {
1173 // TODO: read all of the symbol table, store it in memory in a data
1174 // structure that associates each symbol with its versions and in
1175 // which lookups of a given symbol is fast.
1176 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1177 ABG_ASSERT(sym_tab_section);
1178
1179 GElf_Shdr header_mem;
1180 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1181 &header_mem);
1182
1183 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1184 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1185 GElf_Sym* sym;
1186 char* name_str = 0;
1187 elf_symbol::version ver;
1188 bool found = false;
1189
1190 for (size_t i = 0; i < symcount; ++i)
1191 {
1192 GElf_Sym sym_mem;
1193 sym = gelf_getsym(symtab, i, &sym_mem);
1194 name_str = elf_strptr(elf_handle,
1195 sym_tab_header->sh_link,
1196 sym->st_name);
1197
1198 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1199 {
1200 elf_symbol::type sym_type =
1201 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1202 elf_symbol::binding sym_binding =
1203 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1204 elf_symbol::visibility sym_visibility =
1205 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1206 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1207 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1208
1209 if (get_version_for_symbol(elf_handle, i,
1210 /*get_def_version=*/sym_is_defined,
1211 ver))
1212 ABG_ASSERT(!ver.str().empty());
1213 elf_symbol_sptr symbol_found =
1214 elf_symbol::create(env, i, sym->st_size,
1215 name_str, sym_type,
1216 sym_binding, sym_is_defined,
1217 sym_is_common, ver, sym_visibility);
1218 syms_found.push_back(symbol_found);
1219 found = true;
1220 }
1221 }
1222
1223 if (found)
1224 return true;
1225
1226 return false;
1227 }
1228
1229 /// Look into the symbol tables of the underlying elf file and see
1230 /// if we find a given symbol.
1231 ///
1232 /// @param env the environment we are operating from.
1233 ///
1234 /// @param symbol_name the name of the symbol to look for.
1235 ///
1236 /// @param demangle if true, try to demangle the symbol name found in
1237 /// the symbol table before comparing it to @p symbol_name.
1238 ///
1239 /// @param syms_found the list of symbols found, with the name @p
1240 /// symbol_name.
1241 ///
1242 /// @param sym_type this is set to the type of the symbol found. This
1243 /// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1244 /// STT_FUNC, STT_IFUNC, etc ...
1245 ///
1246 /// Note that this parameter is set iff the function returns true.
1247 ///
1248 /// @param sym_binding this is set to the binding of the symbol found.
1249 /// This is a standard elf.h value of the symbol binding kind, that
1250 /// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1251 ///
1252 /// @param symbol_versions the versions of the symbol @p symbol_name,
1253 /// if it was found.
1254 ///
1255 /// @return true iff a symbol with the name @p symbol_name was found.
1256 static bool
lookup_symbol_from_elf(const environment & env,Elf * elf_handle,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms_found)1257 lookup_symbol_from_elf(const environment& env,
1258 Elf* elf_handle,
1259 const string& symbol_name,
1260 bool demangle,
1261 vector<elf_symbol_sptr>& syms_found)
1262 {
1263 size_t hash_table_index = 0, symbol_table_index = 0;
1264 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1265
1266 if (!demangle)
1267 ht_kind = find_hash_table_section_index(elf_handle,
1268 hash_table_index,
1269 symbol_table_index);
1270
1271 if (ht_kind == NO_HASH_TABLE_KIND)
1272 {
1273 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1274 return false;
1275
1276 return lookup_symbol_from_symtab(env,
1277 elf_handle,
1278 symbol_name,
1279 symbol_table_index,
1280 demangle,
1281 syms_found);
1282 }
1283
1284 return lookup_symbol_from_elf_hash_tab(env,
1285 elf_handle,
1286 ht_kind,
1287 hash_table_index,
1288 symbol_table_index,
1289 symbol_name,
1290 demangle,
1291 syms_found);
1292 }
1293
1294 /// Look into the symbol tables of the underlying elf file and see if
1295 /// we find a given public (global or weak) symbol of function type.
1296 ///
1297 /// @param env the environment we are operating from.
1298 ///
1299 /// @param elf_handle the elf handle to use for the query.
1300 ///
1301 /// @param symbol_name the function symbol to look for.
1302 ///
1303 /// @param func_syms the vector of public functions symbols found, if
1304 /// any.
1305 ///
1306 /// @return true iff the symbol was found.
1307 static bool
lookup_public_function_symbol_from_elf(environment & env,Elf * elf_handle,const string & symbol_name,vector<elf_symbol_sptr> & func_syms)1308 lookup_public_function_symbol_from_elf(environment& env,
1309 Elf* elf_handle,
1310 const string& symbol_name,
1311 vector<elf_symbol_sptr>& func_syms)
1312 {
1313 vector<elf_symbol_sptr> syms_found;
1314 bool found = false;
1315
1316 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1317 /*demangle=*/false, syms_found))
1318 {
1319 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1320 i != syms_found.end();
1321 ++i)
1322 {
1323 elf_symbol::type type = (*i)->get_type();
1324 elf_symbol::binding binding = (*i)->get_binding();
1325
1326 if ((type == elf_symbol::FUNC_TYPE
1327 || type == elf_symbol::GNU_IFUNC_TYPE
1328 || type == elf_symbol::COMMON_TYPE)
1329 && (binding == elf_symbol::GLOBAL_BINDING
1330 || binding == elf_symbol::WEAK_BINDING))
1331 {
1332 func_syms.push_back(*i);
1333 found = true;
1334 }
1335 }
1336 }
1337
1338 return found;
1339 }
1340
1341 // ---------------------------------------
1342 // <location expression evaluation types>
1343 // ---------------------------------------
1344
1345 /// An abstraction of a value representing the result of the
1346 /// evaluation of a dwarf expression. This is abstraction represents
1347 /// a partial view on the possible values because we are only
1348 /// interested in extracting the latest and longuest constant
1349 /// sub-expression of a given dwarf expression.
1350 class expr_result
1351 {
1352 bool is_const_;
1353 int64_t const_value_;
1354
1355 public:
expr_result()1356 expr_result()
1357 : is_const_(true),
1358 const_value_(0)
1359 {}
1360
expr_result(bool is_const)1361 expr_result(bool is_const)
1362 : is_const_(is_const),
1363 const_value_(0)
1364 {}
1365
expr_result(int64_t v)1366 explicit expr_result(int64_t v)
1367 :is_const_(true),
1368 const_value_(v)
1369 {}
1370
1371 /// @return true if the value is a constant. Otherwise, return
1372 /// false, meaning the value represents a quantity for which we need
1373 /// inferior (a running program) state to determine the value.
1374 bool
is_const() const1375 is_const() const
1376 {return is_const_;}
1377
1378
1379 /// @param f a flag saying if the value is set to a constant or not.
1380 void
is_const(bool f)1381 is_const(bool f)
1382 {is_const_ = f;}
1383
1384 /// Get the current constant value iff this represents a
1385 /// constant.
1386 ///
1387 /// @param value the out parameter. Is set to the constant value of
1388 /// the @ref expr_result. This is set iff the function return true.
1389 ///
1390 ///@return true if this has a constant value, false otherwise.
1391 bool
const_value(int64_t & value)1392 const_value(int64_t& value)
1393 {
1394 if (is_const())
1395 {
1396 value = const_value_;
1397 return true;
1398 }
1399 return false;
1400 }
1401
1402 /// Getter of the constant value of the current @ref expr_result.
1403 ///
1404 /// Note that the current @ref expr_result must be constant,
1405 /// otherwise the current process is aborted.
1406 ///
1407 /// @return the constant value of the current @ref expr_result.
1408 int64_t
const_value() const1409 const_value() const
1410 {
1411 ABG_ASSERT(is_const());
1412 return const_value_;
1413 }
1414
operator int64_t() const1415 operator int64_t() const
1416 {return const_value();}
1417
1418 expr_result&
operator =(const int64_t v)1419 operator=(const int64_t v)
1420 {
1421 const_value_ = v;
1422 return *this;
1423 }
1424
1425 bool
operator ==(const expr_result & o) const1426 operator==(const expr_result& o) const
1427 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1428
1429 bool
operator >=(const expr_result & o) const1430 operator>=(const expr_result& o) const
1431 {return const_value_ >= o.const_value_;}
1432
1433 bool
operator <=(const expr_result & o) const1434 operator<=(const expr_result& o) const
1435 {return const_value_ <= o.const_value_;}
1436
1437 bool
operator >(const expr_result & o) const1438 operator>(const expr_result& o) const
1439 {return const_value_ > o.const_value_;}
1440
1441 bool
operator <(const expr_result & o) const1442 operator<(const expr_result& o) const
1443 {return const_value_ < o.const_value_;}
1444
1445 expr_result
operator +(const expr_result & v) const1446 operator+(const expr_result& v) const
1447 {
1448 expr_result r(*this);
1449 r.const_value_ += v.const_value_;
1450 r.is_const_ = r.is_const_ && v.is_const_;
1451 return r;
1452 }
1453
1454 expr_result&
operator +=(int64_t v)1455 operator+=(int64_t v)
1456 {
1457 const_value_ += v;
1458 return *this;
1459 }
1460
1461 expr_result
operator -(const expr_result & v) const1462 operator-(const expr_result& v) const
1463 {
1464 expr_result r(*this);
1465 r.const_value_ -= v.const_value_;
1466 r.is_const_ = r.is_const_ && v.is_const_;
1467 return r;
1468 }
1469
1470 expr_result
operator %(const expr_result & v) const1471 operator%(const expr_result& v) const
1472 {
1473 expr_result r(*this);
1474 r.const_value_ %= v.const_value_;
1475 r.is_const_ = r.is_const_ && v.is_const();
1476 return r;
1477 }
1478
1479 expr_result
operator *(const expr_result & v) const1480 operator*(const expr_result& v) const
1481 {
1482 expr_result r(*this);
1483 r.const_value_ *= v.const_value_;
1484 r.is_const_ = r.is_const_ && v.is_const();
1485 return r;
1486 }
1487
1488 expr_result
operator |(const expr_result & v) const1489 operator|(const expr_result& v) const
1490 {
1491 expr_result r(*this);
1492 r.const_value_ |= v.const_value_;
1493 r.is_const_ = r.is_const_ && v.is_const_;
1494 return r;
1495 }
1496
1497 expr_result
operator ^(const expr_result & v) const1498 operator^(const expr_result& v) const
1499 {
1500 expr_result r(*this);
1501 r.const_value_ ^= v.const_value_;
1502 r.is_const_ = r.is_const_ && v.is_const_;
1503 return r;
1504 }
1505
1506 expr_result
operator >>(const expr_result & v) const1507 operator>>(const expr_result& v) const
1508 {
1509 expr_result r(*this);
1510 r.const_value_ = r.const_value_ >> v.const_value_;
1511 r.is_const_ = r.is_const_ && v.is_const_;
1512 return r;
1513 }
1514
1515 expr_result
operator <<(const expr_result & v) const1516 operator<<(const expr_result& v) const
1517 {
1518 expr_result r(*this);
1519 r.const_value_ = r.const_value_ << v.const_value_;
1520 r.is_const_ = r.is_const_ && v.is_const_;
1521 return r;
1522 }
1523
1524 expr_result
operator ~() const1525 operator~() const
1526 {
1527 expr_result r(*this);
1528 r.const_value_ = ~r.const_value_;
1529 return r;
1530 }
1531
1532 expr_result
neg() const1533 neg() const
1534 {
1535 expr_result r(*this);
1536 r.const_value_ = -r.const_value_;
1537 return r;
1538 }
1539
1540 expr_result
abs() const1541 abs() const
1542 {
1543 expr_result r = *this;
1544 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1545 return r;
1546 }
1547
1548 expr_result
operator &(const expr_result & o)1549 operator&(const expr_result& o)
1550 {
1551 expr_result r(*this);
1552 r.const_value_ &= o.const_value_;
1553 r.is_const_ = r.is_const_ && o.is_const_;
1554 return r;
1555 }
1556
1557 expr_result
operator /(const expr_result & o)1558 operator/(const expr_result& o)
1559 {
1560 expr_result r(*this);
1561 r.is_const_ = r.is_const_ && o.is_const_;
1562 return r.const_value() / o.const_value();
1563 }
1564 };// class end expr_result;
1565
1566 /// A class that implements a stack of @ref expr_result, to be used in
1567 /// the engine evaluating DWARF expressions.
1568 class expr_result_stack_type
1569 {
1570 vector<expr_result> elems_;
1571
1572 public:
1573
expr_result_stack_type()1574 expr_result_stack_type()
1575 {elems_.reserve(4);}
1576
1577 expr_result&
operator [](unsigned i)1578 operator[](unsigned i)
1579 {
1580 unsigned s = elems_.size();
1581 ABG_ASSERT(s > i);
1582 return elems_[s - 1 -i];
1583 }
1584
1585 const expr_result&
operator [](unsigned i) const1586 operator[](unsigned i) const
1587 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1588
1589 unsigned
size() const1590 size() const
1591 {return elems_.size();}
1592
1593 vector<expr_result>::reverse_iterator
begin()1594 begin()
1595 {return elems_.rbegin();}
1596
1597 const vector<expr_result>::reverse_iterator
begin() const1598 begin() const
1599 {return const_cast<expr_result_stack_type*>(this)->begin();}
1600
1601 vector<expr_result>::reverse_iterator
end()1602 end()
1603 {return elems_.rend();}
1604
1605 const vector<expr_result>::reverse_iterator
end() const1606 end() const
1607 {return const_cast<expr_result_stack_type*>(this)->end();}
1608
1609 expr_result&
front()1610 front()
1611 {return elems_.back();}
1612
1613 const expr_result&
front() const1614 front() const
1615 {return const_cast<expr_result_stack_type*>(this)->front();}
1616
1617 void
push_front(expr_result e)1618 push_front(expr_result e)
1619 {elems_.push_back(e);}
1620
1621 expr_result
pop_front()1622 pop_front()
1623 {
1624 expr_result r = front();
1625 elems_.pop_back();
1626 return r;
1627 }
1628
1629 void
erase(vector<expr_result>::reverse_iterator i)1630 erase(vector<expr_result>::reverse_iterator i)
1631 {elems_.erase(--i.base());}
1632
1633 void
clear()1634 clear()
1635 {elems_.clear();}
1636 }; // end class expr_result_stack_type
1637
1638 /// Abstraction of the evaluation context of a dwarf expression.
1639 struct dwarf_expr_eval_context
1640 {
1641 expr_result accum;
1642 expr_result_stack_type stack;
1643 // Is set to true if the result of the expression that got evaluated
1644 // is a TLS address.
1645 bool set_tls_addr;
1646
dwarf_expr_eval_contextabigail::dwarf::dwarf_expr_eval_context1647 dwarf_expr_eval_context()
1648 : accum(/*is_const=*/false),
1649 set_tls_addr(false)
1650 {
1651 stack.push_front(expr_result(true));
1652 }
1653
1654 void
resetabigail::dwarf::dwarf_expr_eval_context1655 reset()
1656 {
1657 stack.clear();
1658 stack.push_front(expr_result(true));
1659 accum = expr_result(false);
1660 set_tls_addr = false;
1661 }
1662
1663 /// Set a flag to to tell that the result of the expression that got
1664 /// evaluated is a TLS address.
1665 ///
1666 /// @param f true iff the result of the expression that got
1667 /// evaluated is a TLS address, false otherwise.
1668 void
set_tls_addressabigail::dwarf::dwarf_expr_eval_context1669 set_tls_address(bool f)
1670 {set_tls_addr = f;}
1671
1672 /// Getter for the flag that tells if the result of the expression
1673 /// that got evaluated is a TLS address.
1674 ///
1675 /// @return true iff the result of the expression that got evaluated
1676 /// is a TLS address.
1677 bool
set_tls_addressabigail::dwarf::dwarf_expr_eval_context1678 set_tls_address() const
1679 {return set_tls_addr;}
1680
1681 expr_result
popabigail::dwarf::dwarf_expr_eval_context1682 pop()
1683 {
1684 expr_result r = stack.front();
1685 stack.pop_front();
1686 return r;
1687 }
1688
1689 void
pushabigail::dwarf::dwarf_expr_eval_context1690 push(const expr_result& v)
1691 {stack.push_front(v);}
1692 };//end class dwarf_expr_eval_context
1693
1694 // ---------------------------------------
1695 // </location expression evaluation types>
1696 // ---------------------------------------
1697
1698 class reader;
1699
1700 typedef shared_ptr<reader> reader_sptr;
1701
1702 /// The DWARF reader used to build the ABI corpus from debug info in
1703 /// DWARF format.
1704 ///
1705 /// This type is to be instanciated
1706 /// abigail::dwarf::reader::create().
1707 class reader : public elf_based_reader
1708 {
1709 public:
1710
1711 /// A set of containers that contains one container per kind of @ref
1712 /// die_source. This allows to associate DIEs to things, depending
1713 /// on the source of the DIE.
1714 template <typename ContainerType>
1715 class die_source_dependant_container_set
1716 {
1717 ContainerType primary_debug_info_container_;
1718 ContainerType alt_debug_info_container_;
1719 ContainerType type_unit_container_;
1720
1721 public:
1722
1723 /// Getter for the container associated to DIEs coming from a
1724 /// given @ref die_source.
1725 ///
1726 /// @param source the die_source for which we want the container.
1727 ///
1728 /// @return the container that associates DIEs coming from @p
1729 /// source to something.
1730 ContainerType&
get_container(die_source source)1731 get_container(die_source source)
1732 {
1733 ContainerType *result = 0;
1734 switch (source)
1735 {
1736 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1737 result = &primary_debug_info_container_;
1738 break;
1739 case ALT_DEBUG_INFO_DIE_SOURCE:
1740 result = &alt_debug_info_container_;
1741 break;
1742 case TYPE_UNIT_DIE_SOURCE:
1743 result = &type_unit_container_;
1744 break;
1745 case NO_DEBUG_INFO_DIE_SOURCE:
1746 case NUMBER_OF_DIE_SOURCES:
1747 ABG_ASSERT_NOT_REACHED;
1748 }
1749 return *result;
1750 }
1751
1752 /// Getter for the container associated to DIEs coming from a
1753 /// given @ref die_source.
1754 ///
1755 /// @param source the die_source for which we want the container.
1756 ///
1757 /// @return the container that associates DIEs coming from @p
1758 /// source to something.
1759 const ContainerType&
get_container(die_source source) const1760 get_container(die_source source) const
1761 {
1762 return const_cast<die_source_dependant_container_set*>(this)->
1763 get_container(source);
1764 }
1765
1766 /// Getter for the container associated to DIEs coming from the
1767 /// same source as a given DIE.
1768 ///
1769 /// @param rdr the DWARF reader to consider.
1770 ///
1771 /// @param die the DIE which should have the same source as the
1772 /// source of the container we want.
1773 ///
1774 /// @return the container that associates DIEs coming from the
1775 /// same source as @p die.
1776 ContainerType&
get_container(const reader & rdr,const Dwarf_Die * die)1777 get_container(const reader& rdr, const Dwarf_Die *die)
1778 {
1779 const die_source source = rdr.get_die_source(die);
1780 return get_container(source);
1781 }
1782
1783 /// Getter for the container associated to DIEs coming from the
1784 /// same source as a given DIE.
1785 ///
1786 /// @param rdr the DWARF reader to consider.
1787 ///
1788 /// @param die the DIE which should have the same source as the
1789 /// source of the container we want.
1790 ///
1791 /// @return the container that associates DIEs coming from the
1792 /// same source as @p die.
1793 const ContainerType&
get_container(const reader & rdr,const Dwarf_Die * die) const1794 get_container(const reader& rdr, const Dwarf_Die *die) const
1795 {
1796 return const_cast<die_source_dependant_container_set*>(this)->
1797 get_container(rdr, die);
1798 }
1799
1800 /// Clear the container set.
1801 void
clear()1802 clear()
1803 {
1804 primary_debug_info_container_.clear();
1805 alt_debug_info_container_.clear();
1806 type_unit_container_.clear();
1807 }
1808 }; // end die_dependant_container_set
1809
1810 unsigned short dwarf_version_;
1811 Dwarf_Die* cur_tu_die_;
1812 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1813 // A set of maps (one per kind of die source) that associates a decl
1814 // string representation with the DIEs (offsets) representing that
1815 // decl.
1816 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1817 decl_die_repr_die_offsets_maps_;
1818 // A set of maps (one per kind of die source) that associates a type
1819 // string representation with the DIEs (offsets) representing that
1820 // type.
1821 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1822 type_die_repr_die_offsets_maps_;
1823 mutable die_source_dependant_container_set<die_istring_map_type>
1824 die_qualified_name_maps_;
1825 mutable die_source_dependant_container_set<die_istring_map_type>
1826 die_pretty_repr_maps_;
1827 mutable die_source_dependant_container_set<die_istring_map_type>
1828 die_pretty_type_repr_maps_;
1829 // A set of maps (one per kind of die source) that associates the
1830 // offset of a decl die to its corresponding decl artifact.
1831 mutable die_source_dependant_container_set<die_artefact_map_type>
1832 decl_die_artefact_maps_;
1833 // A set of maps (one per kind of die source) that associates the
1834 // offset of a type die to its corresponding type artifact.
1835 mutable die_source_dependant_container_set<die_artefact_map_type>
1836 type_die_artefact_maps_;
1837 /// A set of vectors (one per kind of die source) that associates
1838 /// the offset of a type DIE to the offset of its canonical DIE.
1839 mutable die_source_dependant_container_set<offset_offset_map_type>
1840 canonical_type_die_offsets_;
1841 /// A set of vectors (one per kind of die source) that associates
1842 /// the offset of a decl DIE to the offset of its canonical DIE.
1843 mutable die_source_dependant_container_set<offset_offset_map_type>
1844 canonical_decl_die_offsets_;
1845 /// A map that associates a function type representations to
1846 /// function types, inside a translation unit.
1847 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1848 /// A map that associates a pair of DIE offsets to the result of the
1849 /// comparison of that pair.
1850 mutable std::unordered_map<std::pair<offset_type,offset_type>,
1851 abigail::ir::comparison_result,
1852 dwarf_offset_pair_hash> die_comparison_results_;
1853 // The set of types pair that have been canonical-type-propagated.
1854 mutable offset_pair_set_type propagated_types_;
1855 die_class_or_union_map_type die_wip_classes_map_;
1856 die_class_or_union_map_type alternate_die_wip_classes_map_;
1857 die_class_or_union_map_type type_unit_die_wip_classes_map_;
1858 die_function_type_map_type die_wip_function_types_map_;
1859 die_function_type_map_type alternate_die_wip_function_types_map_;
1860 die_function_type_map_type type_unit_die_wip_function_types_map_;
1861 die_function_decl_map_type die_function_with_no_symbol_map_;
1862 vector<type_base_sptr> types_to_canonicalize_;
1863 string_classes_or_unions_map decl_only_classes_map_;
1864 string_enums_map decl_only_enums_map_;
1865 die_tu_map_type die_tu_map_;
1866 translation_unit_sptr cur_tu_;
1867 scope_decl_sptr nil_scope_;
1868 scope_stack_type scope_stack_;
1869 offset_offset_map_type primary_die_parent_map_;
1870 // A map that associates each tu die to a vector of unit import
1871 // points, in the main debug info
1872 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1873 // A map that associates each tu die to a vector of unit import
1874 // points, in the alternate debug info
1875 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1876 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1877 // A DIE -> parent map for DIEs coming from the alternate debug info
1878 // file.
1879 offset_offset_map_type alternate_die_parent_map_;
1880 offset_offset_map_type type_section_die_parent_map_;
1881 list<var_decl_sptr> var_decls_to_add_;
1882 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1883 bool debug_die_canonicalization_is_on_;
1884 bool use_canonical_die_comparison_;
1885 #endif
1886 mutable size_t compare_count_;
1887 mutable size_t canonical_propagated_count_;
1888 mutable size_t cancelled_propagation_count_;
1889 mutable optional<bool> leverage_dwarf_factorization_;
1890
1891 protected:
1892
1893 reader() = delete;
1894
1895 /// Constructor of reader.
1896 ///
1897 /// @param elf_path the path to the elf file the context is to be
1898 /// used for.
1899 ///
1900 /// @param debug_info_root_paths a vector of pointers to the path to
1901 /// the root directory under which the debug info is to be found for
1902 /// @p elf_path. Leave this empty if the debug info is not in a
1903 /// split file.
1904 ///
1905 /// @param environment the environment used by the current context.
1906 /// This environment contains resources needed by the DWARF reader and by
1907 /// the types and declarations that are to be created later. Note
1908 /// that ABI artifacts that are to be compared all need to be
1909 /// created within the same environment.
1910 ///
1911 /// Please also note that the life time of this environment object
1912 /// must be greater than the life time of the resulting @ref
1913 /// reader the context uses resources that are allocated in
1914 /// the environment.
1915 ///
1916 /// @param load_all_types if set to false only the types that are
1917 /// reachable from publicly exported declarations (of functions and
1918 /// variables) are read. If set to true then all types found in the
1919 /// debug information are loaded.
1920 ///
1921 /// @param linux_kernel_mode if set to true, then consider the special
1922 /// linux kernel symbol tables when determining if a symbol is
1923 /// exported or not.
reader(const string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,bool linux_kernel_mode)1924 reader(const string& elf_path,
1925 const vector<char**>& debug_info_root_paths,
1926 environment& environment,
1927 bool load_all_types,
1928 bool linux_kernel_mode)
1929 : elf_based_reader(elf_path,
1930 debug_info_root_paths,
1931 environment)
1932 {
1933 initialize(load_all_types, linux_kernel_mode);
1934 }
1935
1936 public:
1937
1938 /// Initializer of reader.
1939 ///
1940 /// Resets the reader so that it can be re-used to read another binary.
1941 ///
1942 /// @param load_all_types if set to false only the types that are
1943 /// reachable from publicly exported declarations (of functions and
1944 /// variables) are read. If set to true then all types found in the
1945 /// debug information are loaded.
1946 ///
1947 /// @param linux_kernel_mode if set to true, then consider the
1948 /// special linux kernel symbol tables when determining if a symbol
1949 /// is exported or not.
1950 void
initialize(bool load_all_types,bool linux_kernel_mode)1951 initialize(bool load_all_types, bool linux_kernel_mode)
1952 {
1953 dwarf_version_ = 0;
1954 cur_tu_die_ = 0;
1955 decl_die_repr_die_offsets_maps_.clear();
1956 type_die_repr_die_offsets_maps_.clear();
1957 die_qualified_name_maps_.clear();
1958 die_pretty_repr_maps_.clear();
1959 die_pretty_type_repr_maps_.clear();
1960 decl_die_artefact_maps_.clear();
1961 type_die_artefact_maps_.clear();
1962 canonical_type_die_offsets_.clear();
1963 canonical_decl_die_offsets_.clear();
1964 die_wip_classes_map_.clear();
1965 alternate_die_wip_classes_map_.clear();
1966 type_unit_die_wip_classes_map_.clear();
1967 die_wip_function_types_map_.clear();
1968 alternate_die_wip_function_types_map_.clear();
1969 type_unit_die_wip_function_types_map_.clear();
1970 die_function_with_no_symbol_map_.clear();
1971 types_to_canonicalize_.clear();
1972 decl_only_classes_map_.clear();
1973 die_tu_map_.clear();
1974 corpus().reset();
1975 corpus_group().reset();
1976 cur_tu_.reset();
1977 primary_die_parent_map_.clear();
1978 tu_die_imported_unit_points_map_.clear();
1979 alt_tu_die_imported_unit_points_map_.clear();
1980 type_units_tu_die_imported_unit_points_map_.clear();
1981 alternate_die_parent_map_.clear();
1982 type_section_die_parent_map_.clear();
1983 var_decls_to_add_.clear();
1984 clear_per_translation_unit_data();
1985 options().load_in_linux_kernel_mode = linux_kernel_mode;
1986 options().load_all_types = load_all_types;
1987 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1988 debug_die_canonicalization_is_on_ =
1989 env().debug_die_canonicalization_is_on();
1990 use_canonical_die_comparison_ = true;
1991 #endif
1992 compare_count_ = 0;
1993 canonical_propagated_count_ = 0;
1994 cancelled_propagation_count_ = 0;
1995 load_in_linux_kernel_mode(linux_kernel_mode);
1996 }
1997
1998 /// Initializer of reader.
1999 ///
2000 /// Resets the reader so that it can be re-used to read another binary.
2001 ///
2002 /// @param elf_path the path to the new ELF file.
2003 ///
2004 /// @param debug_info_root_paths the vector of debug-info path to
2005 /// look for split debug info.
2006 ///
2007 /// @param load_all_types if set to false only the types that are
2008 /// reachable from publicly exported declarations (of functions and
2009 /// variables) are read. If set to true then all types found in the
2010 /// debug information are loaded.
2011 ///
2012 /// @param linux_kernel_mode if set to true, then consider the
2013 /// special linux kernel symbol tables when determining if a symbol
2014 /// is exported or not.
2015 void
initialize(const string & elf_path,const vector<char ** > & debug_info_root_paths,bool load_all_types,bool linux_kernel_mode)2016 initialize(const string& elf_path,
2017 const vector<char**>& debug_info_root_paths,
2018 bool load_all_types,
2019 bool linux_kernel_mode)
2020 {
2021 elf_based_reader::initialize(elf_path, debug_info_root_paths);
2022 initialize(load_all_types, linux_kernel_mode);
2023 }
2024
2025 /// Create an instance of DWARF Reader.
2026 ///
2027 /// @param elf_path the path to the ELF file to read from.
2028 ///
2029 /// @param debug_info_root_paths a vector of paths where to look up
2030 /// split debug info files.
2031 ///
2032 /// @param environment the environment to be used by the reader.
2033 ///
2034 /// @param load_all_types if set to false only the types that are
2035 /// reachable from publicly exported declarations (of functions and
2036 /// variables) are read. If set to true then all types found in the
2037 /// debug information are loaded.
2038 ///
2039 /// @param linux_kernel_mode if set to true, then consider the
2040 /// special linux kernel symbol tables when determining if a symbol
2041 /// is exported or not.
2042 static dwarf::reader_sptr
create(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,bool linux_kernel_mode)2043 create(const std::string& elf_path,
2044 const vector<char**>& debug_info_root_paths,
2045 environment& environment,
2046 bool load_all_types,
2047 bool linux_kernel_mode)
2048 {
2049 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2050 environment, load_all_types,
2051 linux_kernel_mode));
2052 return result;
2053 }
2054
2055 /// Destructor of the @ref reader type.
~reader()2056 ~reader()
2057 {
2058 }
2059
2060 /// Read and analyze the ELF and DWARF information associated with
2061 /// the underlying ELF file and build an ABI corpus out of it.
2062 ///
2063 /// @param status output parameter. This is set to the status of
2064 /// the analysis of the debug info.
2065 ///
2066 /// @return the resulting ABI corpus.
2067 corpus_sptr
read_corpus(status & status)2068 read_corpus(status& status)
2069 {
2070 status = STATUS_UNKNOWN;
2071
2072 // Load the generic ELF parts of the corpus.
2073 elf::reader::read_corpus(status);
2074
2075 if (!(status & STATUS_OK))
2076 {
2077 // Something went badly wrong. There is nothing we can do
2078 // with this ELF file. Bail out.
2079 return corpus_sptr();
2080 }
2081
2082 // If we couldn't find debug info from the elf path, then say it.
2083 if (dwarf_debug_info() == nullptr)
2084 status |= STATUS_DEBUG_INFO_NOT_FOUND;
2085
2086 {
2087 string alt_di_path;
2088 if (refers_to_alt_debug_info(alt_di_path)
2089 && !alternate_dwarf_debug_info())
2090 status |= STATUS_ALT_DEBUG_INFO_NOT_FOUND;
2091 }
2092
2093 if (// If debug info was found but not the required alternate debug
2094 // info ...
2095 ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2096 && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2097 // ... then we cannot handle the binary.
2098 return corpus_sptr();
2099
2100 // Read the variable and function descriptions from the debug info
2101 // we have, through the dwfl handle.
2102 corpus_sptr corp = read_debug_info_into_corpus();
2103
2104 status |= STATUS_OK;
2105
2106 return corp;
2107 }
2108
2109 /// Read an analyze the DWARF information.
2110 ///
2111 /// Construct an ABI corpus from it.
2112 ///
2113 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2114 ///
2115 /// @return the resulting ABI corpus.
2116 corpus_sptr
read_debug_info_into_corpus()2117 read_debug_info_into_corpus()
2118 {
2119 clear_per_corpus_data();
2120
2121 // First set some mundane properties of the corpus gathered from
2122 // ELF.
2123 corpus::origin origin = corpus()->get_origin();
2124 origin |= corpus::DWARF_ORIGIN;
2125 corpus()->set_origin(origin);
2126
2127 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2128 && !env().user_set_analyze_exported_interfaces_only())
2129 // So we are looking at the Linux Kernel and the user has not set
2130 // any particular option regarding the amount of types to analyse.
2131 // In that case, we need to only analyze types that are reachable
2132 // from exported interfaces otherwise we get such a massive amount
2133 // of type DIEs to look at that things are just too slow down the
2134 // road.
2135 env().analyze_exported_interfaces_only(true);
2136
2137 corpus()->set_soname(dt_soname());
2138 corpus()->set_needed(dt_needed());
2139 corpus()->set_architecture_name(elf_architecture());
2140 // Set symbols information to the corpus.
2141 corpus()->set_symtab(symtab());
2142
2143 // Get out now if no debug info is found or if the symbol table is
2144 // empty.
2145 if (!dwarf_debug_info()
2146 || !corpus()->get_symtab()
2147 || !corpus()->get_symtab()->has_symbols())
2148 return corpus();
2149
2150 uint8_t address_size = 0;
2151 size_t header_size = 0;
2152
2153 #ifdef WITH_DEBUG_SELF_COMPARISON
2154 if (env().self_comparison_debug_is_on())
2155 env().set_self_comparison_debug_input(corpus());
2156 #endif
2157
2158 env().priv_->do_log(do_log());
2159
2160 // Walk all the DIEs of the debug info to build a DIE -> parent map
2161 // useful for get_die_parent() to work.
2162 {
2163 tools_utils::timer t;
2164 if (do_log())
2165 {
2166 cerr << "building die -> parent maps ...";
2167 t.start();
2168 }
2169
2170 build_die_parent_maps();
2171
2172 if (do_log())
2173 {
2174 t.stop();
2175 cerr << " DONE@" << corpus()->get_path()
2176 << ":"
2177 << t
2178 << "\n";
2179 }
2180 }
2181
2182 env().canonicalization_is_done(false);
2183
2184 {
2185 tools_utils::timer t;
2186 if (do_log())
2187 {
2188 cerr << "building the libabigail internal representation ...\n";
2189 t.start();
2190 }
2191 // And now walk all the DIEs again to build the libabigail IR.
2192 Dwarf_Half dwarf_vers = 0;
2193 for (Dwarf_Off offset = 0, next_offset = 0;
2194 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2195 offset, &next_offset, &header_size,
2196 &dwarf_vers, NULL, &address_size, NULL,
2197 NULL, NULL) == 0);
2198 offset = next_offset)
2199 {
2200 Dwarf_Off die_offset = offset + header_size;
2201 Dwarf_Die unit;
2202 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2203 die_offset, &unit)
2204 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2205 continue;
2206
2207 dwarf_version(dwarf_vers);
2208
2209 address_size *= 8;
2210
2211 // Build a translation_unit IR node from cu; note that cu must
2212 // be a DW_TAG_compile_unit die.
2213 translation_unit_sptr ir_node =
2214 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2215 ABG_ASSERT(ir_node);
2216 }
2217 if (do_log())
2218 {
2219 t.stop();
2220 cerr << "building the libabigail internal representation "
2221 << "DONE for corpus << corpus()->get_path()"
2222 << " in :"
2223 << t
2224 << "\n";
2225
2226 cerr << "Number of aggregate types compared: "
2227 << compare_count_ << "\n"
2228 << "Number of canonical types propagated: "
2229 << canonical_propagated_count_ << "\n"
2230 << "Number of cancelled propagated canonical types:"
2231 << cancelled_propagation_count_ << "\n";
2232 }
2233 }
2234
2235 {
2236 tools_utils::timer t;
2237 if (do_log())
2238 {
2239 cerr << "resolving declaration only classes ...";
2240 t.start();
2241 }
2242 resolve_declaration_only_classes();
2243 if (do_log())
2244 {
2245 t.stop();
2246 cerr << " DONE@" << corpus()->get_path()
2247 << ":"
2248 << t
2249 <<"\n";
2250 }
2251 }
2252
2253 {
2254 tools_utils::timer t;
2255 if (do_log())
2256 {
2257 cerr << "resolving declaration only enums ...";
2258 t.start();
2259 }
2260 resolve_declaration_only_enums();
2261 if (do_log())
2262 {
2263 t.stop();
2264 cerr << " DONE@" << corpus()->get_path()
2265 << ":"
2266 << t
2267 <<"\n";
2268 }
2269 }
2270
2271 {
2272 tools_utils::timer t;
2273 if (do_log())
2274 {
2275 cerr << "fixing up functions with linkage name but "
2276 << "no advertised underlying symbols ....";
2277 t.start();
2278 }
2279 fixup_functions_with_no_symbols();
2280 if (do_log())
2281 {
2282 t.stop();
2283 cerr << " DONE@" << corpus()->get_path()
2284 <<":"
2285 << t
2286 <<"\n";
2287 }
2288 }
2289
2290 /// Now, look at the types that needs to be canonicalized after the
2291 /// translation has been constructed (which is just now) and
2292 /// canonicalize them.
2293 ///
2294 /// These types need to be constructed at the end of the translation
2295 /// unit reading phase because some types are modified by some DIEs
2296 /// even after the principal DIE describing the type has been read;
2297 /// this happens for clones of virtual destructors (for instance) or
2298 /// even for some static data members. We need to do that for types
2299 /// are in the alternate debug info section and for types that in
2300 /// the main debug info section.
2301 {
2302 tools_utils::timer t;
2303 if (do_log())
2304 {
2305 cerr << "perform late type canonicalizing ...\n";
2306 t.start();
2307 }
2308
2309 perform_late_type_canonicalizing();
2310 if (do_log())
2311 {
2312 t.stop();
2313 cerr << "late type canonicalizing DONE for "
2314 << corpus()->get_path()
2315 << " in :"
2316 << t
2317 << "\n";
2318 }
2319 }
2320
2321 env().canonicalization_is_done(true);
2322
2323 {
2324 tools_utils::timer t;
2325 if (do_log())
2326 {
2327 cerr << "sort functions and variables ...";
2328 t.start();
2329 }
2330 corpus()->sort_functions();
2331 corpus()->sort_variables();
2332 if (do_log())
2333 {
2334 t.stop();
2335 cerr << " DONE@" << corpus()->get_path()
2336 << ":"
2337 << t
2338 <<" \n";
2339 }
2340 }
2341
2342 return corpus();
2343 }
2344
2345 /// Clear the data that is relevant only for the current translation
2346 /// unit being read. The rest of the data is relevant for the
2347 /// entire ABI corpus.
2348 void
clear_per_translation_unit_data()2349 clear_per_translation_unit_data()
2350 {
2351 while (!scope_stack().empty())
2352 scope_stack().pop();
2353 var_decls_to_re_add_to_tree().clear();
2354 per_tu_repr_to_fn_type_maps().clear();
2355 }
2356
2357 /// Clear the data that is relevant for the current corpus being
2358 /// read.
2359 void
clear_per_corpus_data()2360 clear_per_corpus_data()
2361 {
2362 die_qualified_name_maps_.clear();
2363 die_pretty_repr_maps_.clear();
2364 die_pretty_type_repr_maps_.clear();
2365 clear_types_to_canonicalize();
2366 }
2367
2368 /// Getter for the current environment.
2369 ///
2370 /// @return the current environment.
2371 environment&
env()2372 env()
2373 {return options().env;}
2374
2375 /// Getter for the current environment.
2376 ///
2377 /// @return the current environment.
2378 const environment&
env() const2379 env() const
2380 {return const_cast<reader*>(this)->env();}
2381
2382 /// Getter for the flag that tells us if we are dropping functions
2383 /// and variables that have undefined symbols.
2384 ///
2385 /// @return true iff we are dropping functions and variables that have
2386 /// undefined symbols.
2387 bool
drop_undefined_syms() const2388 drop_undefined_syms() const
2389 {return options().drop_undefined_syms;}
2390
2391 /// Setter for the flag that tells us if we are dropping functions
2392 /// and variables that have undefined symbols.
2393 ///
2394 /// @param f the new value of the flag.
2395 void
drop_undefined_syms(bool f)2396 drop_undefined_syms(bool f)
2397 {options().drop_undefined_syms = f;}
2398
2399 /// Getter of the DWARF version.
2400 unsigned short
dwarf_version() const2401 dwarf_version() const
2402 {return dwarf_version_;}
2403
2404 void
dwarf_version(unsigned short v)2405 dwarf_version(unsigned short v)
2406 {dwarf_version_ = v;}
2407
2408 /// Return the ELF descriptor used for DWARF access.
2409 ///
2410 /// This can be the same as reader::elf_handle() above, if the
2411 /// DWARF info is in the same ELF file as the one of the binary we
2412 /// are analizing. It is different if e.g, the debug info is split
2413 /// from the ELF file we are analizing.
2414 ///
2415 /// @return a pointer to the ELF descriptor used to access debug
2416 /// info.
2417 Elf*
dwarf_elf_handle() const2418 dwarf_elf_handle() const
2419 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2420
2421 /// Test if the debug information is in a separate ELF file wrt the
2422 /// main ELF file of the program (application or shared library) we
2423 /// are analizing.
2424 ///
2425 /// @return true if the debug information is in a separate ELF file
2426 /// compared to the main ELF file of the program (application or
2427 /// shared library) that we are looking at.
2428 bool
dwarf_is_splitted() const2429 dwarf_is_splitted() const
2430 {return dwarf_elf_handle() != elf_handle();}
2431
2432 /// Return the correct debug info, depending on the DIE source we
2433 /// are looking at.
2434 ///
2435 /// @param source the DIE source to consider.
2436 ///
2437 /// @return the right debug info, depending on @p source.
2438 const Dwarf*
dwarf_per_die_source(die_source source) const2439 dwarf_per_die_source(die_source source) const
2440 {
2441 const Dwarf *result = 0;
2442 switch(source)
2443 {
2444 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2445 case TYPE_UNIT_DIE_SOURCE:
2446 result = dwarf_debug_info();
2447 break;
2448 case ALT_DEBUG_INFO_DIE_SOURCE:
2449 result = alternate_dwarf_debug_info();
2450 break;
2451 case NO_DEBUG_INFO_DIE_SOURCE:
2452 case NUMBER_OF_DIE_SOURCES:
2453 ABG_ASSERT_NOT_REACHED;
2454 }
2455 return result;
2456 }
2457
2458 /// Return the path to the ELF path we are reading.
2459 ///
2460 /// @return the elf path.
2461 const string&
elf_path() const2462 elf_path() const
2463 {return corpus_path();}
2464
2465 const Dwarf_Die*
cur_tu_die() const2466 cur_tu_die() const
2467 {return cur_tu_die_;}
2468
2469 void
cur_tu_die(Dwarf_Die * cur_tu_die)2470 cur_tu_die(Dwarf_Die* cur_tu_die)
2471 {cur_tu_die_ = cur_tu_die;}
2472
2473 dwarf_expr_eval_context&
dwarf_expr_eval_ctxt() const2474 dwarf_expr_eval_ctxt() const
2475 {return dwarf_expr_eval_context_;}
2476
2477 /// Getter of the maps set that associates a representation of a
2478 /// decl DIE to a vector of offsets of DIEs having that representation.
2479 ///
2480 /// @return the maps set that associates a representation of a decl
2481 /// DIE to a vector of offsets of DIEs having that representation.
2482 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
decl_die_repr_die_offsets_maps() const2483 decl_die_repr_die_offsets_maps() const
2484 {return decl_die_repr_die_offsets_maps_;}
2485
2486 /// Getter of the maps set that associates a representation of a
2487 /// decl DIE to a vector of offsets of DIEs having that representation.
2488 ///
2489 /// @return the maps set that associates a representation of a decl
2490 /// DIE to a vector of offsets of DIEs having that representation.
2491 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
decl_die_repr_die_offsets_maps()2492 decl_die_repr_die_offsets_maps()
2493 {return decl_die_repr_die_offsets_maps_;}
2494
2495 /// Getter of the maps set that associate a representation of a type
2496 /// DIE to a vector of offsets of DIEs having that representation.
2497 ///
2498 /// @return the maps set that associate a representation of a type
2499 /// DIE to a vector of offsets of DIEs having that representation.
2500 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
type_die_repr_die_offsets_maps() const2501 type_die_repr_die_offsets_maps() const
2502 {return type_die_repr_die_offsets_maps_;}
2503
2504 /// Getter of the maps set that associate a representation of a type
2505 /// DIE to a vector of offsets of DIEs having that representation.
2506 ///
2507 /// @return the maps set that associate a representation of a type
2508 /// DIE to a vector of offsets of DIEs having that representation.
2509 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
type_die_repr_die_offsets_maps()2510 type_die_repr_die_offsets_maps()
2511 {return type_die_repr_die_offsets_maps_;}
2512
2513
2514 /// Compute the offset of the canonical DIE of a given DIE.
2515 ///
2516 /// @param die the DIE to consider.
2517 ///
2518 /// @param canonical_die_offset out parameter. This is set to the
2519 /// resulting canonical DIE that was computed.
2520 ///
2521 /// @param die_as_type if yes, it means @p die has to be considered
2522 /// as a type.
2523 void
compute_canonical_die_offset(const Dwarf_Die * die,Dwarf_Off & canonical_die_offset,bool die_as_type) const2524 compute_canonical_die_offset(const Dwarf_Die *die,
2525 Dwarf_Off &canonical_die_offset,
2526 bool die_as_type) const
2527 {
2528 offset_offset_map_type &canonical_dies =
2529 die_as_type
2530 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2531 get_container(*this, die)
2532 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2533 get_container(*this, die);
2534
2535 Dwarf_Die canonical_die;
2536 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2537
2538 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2539 }
2540
2541 /// Compute (find) the canonical DIE of a given DIE.
2542 ///
2543 /// @param die the DIE to consider.
2544 ///
2545 /// @param canonical_dies the vector in which the canonical dies ar
2546 /// stored. The index of each element is the offset of the DIE we
2547 /// want the canonical DIE for. And the value of the element at
2548 /// that index is the canonical DIE offset we are looking for.
2549 ///
2550 /// @param canonical_die_offset out parameter. This is set to the
2551 /// resulting canonical DIE that was computed.
2552 ///
2553 /// @param die_as_type if yes, it means @p die has to be considered
2554 /// as a type.
2555 void
compute_canonical_die(const Dwarf_Die * die,offset_offset_map_type & canonical_dies,Dwarf_Die & canonical_die,bool die_as_type) const2556 compute_canonical_die(const Dwarf_Die *die,
2557 offset_offset_map_type& canonical_dies,
2558 Dwarf_Die &canonical_die,
2559 bool die_as_type) const
2560 {
2561 const die_source source = get_die_source(die);
2562
2563 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2564
2565 compute_canonical_die(die_offset, source,
2566 canonical_dies,
2567 canonical_die, die_as_type);
2568 }
2569
2570 /// Compute (find) the canonical DIE of a given DIE.
2571 ///
2572 /// @param die_offset the offset of the DIE to consider.
2573 ///
2574 /// @param source the source of the DIE to consider.
2575 ///
2576 /// @param canonical_dies the vector in which the canonical dies ar
2577 /// stored. The index of each element is the offset of the DIE we
2578 /// want the canonical DIE for. And the value of the element at
2579 /// that index is the canonical DIE offset we are looking for.
2580 ///
2581 /// @param canonical_die_offset out parameter. This is set to the
2582 /// resulting canonical DIE that was computed.
2583 ///
2584 /// @param die_as_type if yes, it means @p die has to be considered
2585 /// as a type.
2586 void
compute_canonical_die(Dwarf_Off die_offset,die_source source,offset_offset_map_type & canonical_dies,Dwarf_Die & canonical_die,bool die_as_type) const2587 compute_canonical_die(Dwarf_Off die_offset,
2588 die_source source,
2589 offset_offset_map_type& canonical_dies,
2590 Dwarf_Die &canonical_die,
2591 bool die_as_type) const
2592 {
2593 // The map that associates the string representation of 'die'
2594 // with a vector of offsets of potentially equivalent DIEs.
2595 istring_dwarf_offsets_map_type& map =
2596 die_as_type
2597 ? (const_cast<reader*>(this)->
2598 type_die_repr_die_offsets_maps().get_container(source))
2599 : (const_cast<reader*>(this)->
2600 decl_die_repr_die_offsets_maps().get_container(source));
2601
2602 Dwarf_Die die;
2603 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2604 die_offset, &die));
2605
2606 // The variable repr is the the string representation of 'die'.
2607 //
2608 // Even if die_as_type is true -- which means that 'die' is said
2609 // to be considered as a type -- we always consider a
2610 // DW_TAG_subprogram DIE as a decl here, as far as its string
2611 // representation is concerned.
2612 interned_string name =
2613 (die_as_type)
2614 ? get_die_pretty_type_representation(&die, /*where=*/0)
2615 : get_die_pretty_representation(&die, /*where=*/0);
2616
2617 Dwarf_Off canonical_die_offset = 0;
2618 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2619 if (i == map.end())
2620 {
2621 dwarf_offsets_type offsets;
2622 offsets.push_back(die_offset);
2623 map[name] = offsets;
2624 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2625 get_die_from_offset(source, die_offset, &canonical_die);
2626 return;
2627 }
2628
2629 Dwarf_Off cur_die_offset;
2630 Dwarf_Die potential_canonical_die;
2631 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2632 o != i->second.end();
2633 ++o)
2634 {
2635 cur_die_offset = *o;
2636 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2637 if (compare_dies(*this, &die, &potential_canonical_die,
2638 /*update_canonical_dies_on_the_fly=*/false))
2639 {
2640 canonical_die_offset = cur_die_offset;
2641 set_canonical_die_offset(canonical_dies, die_offset,
2642 canonical_die_offset);
2643 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2644 return;
2645 }
2646 }
2647
2648 canonical_die_offset = die_offset;
2649 i->second.push_back(die_offset);
2650 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2651 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2652 }
2653
2654 /// Getter of the canonical DIE of a given DIE.
2655 ///
2656 /// @param die the DIE to consider.
2657 ///
2658 /// @param canonical_die output parameter. Is set to the resulting
2659 /// canonical die, if this function returns true.
2660 ///
2661 /// @param where the offset of the logical DIE we are supposed to be
2662 /// calling this function from. If set to zero this means this is
2663 /// to be ignored.
2664 ///
2665 /// @param die_as_type if set to yes, it means @p die is to be
2666 /// considered as a type DIE.
2667 ///
2668 /// @return true iff a canonical DIE was found for @p die.
2669 bool
get_canonical_die(const Dwarf_Die * die,Dwarf_Die & canonical_die,size_t where,bool die_as_type)2670 get_canonical_die(const Dwarf_Die *die,
2671 Dwarf_Die &canonical_die,
2672 size_t where,
2673 bool die_as_type)
2674 {
2675 const die_source source = get_die_source(die);
2676
2677 offset_offset_map_type &canonical_dies =
2678 die_as_type
2679 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2680 get_container(source)
2681 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2682 get_container(source);
2683
2684 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2685 if (Dwarf_Off canonical_die_offset =
2686 get_canonical_die_offset(canonical_dies, die_offset))
2687 {
2688 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2689 return true;
2690 }
2691
2692 // The map that associates the string representation of 'die'
2693 // with a vector of offsets of potentially equivalent DIEs.
2694 istring_dwarf_offsets_map_type& map =
2695 die_as_type
2696 ? (const_cast<reader*>(this)->
2697 type_die_repr_die_offsets_maps().get_container(*this, die))
2698 : (const_cast<reader*>(this)->
2699 decl_die_repr_die_offsets_maps().get_container(*this, die));
2700
2701 // The variable repr is the the string representation of 'die'.
2702 //
2703 // Even if die_as_type is true -- which means that 'die' is said
2704 // to be considered as a type -- we always consider a
2705 // DW_TAG_subprogram DIE as a decl here, as far as its string
2706 // representation is concerned.
2707 interned_string name =
2708 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2709 ? get_die_pretty_type_representation(die, where)
2710 : get_die_pretty_representation(die, where);
2711
2712 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2713 if (i == map.end())
2714 return false;
2715
2716 Dwarf_Off cur_die_offset;
2717 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2718 o != i->second.end();
2719 ++o)
2720 {
2721 cur_die_offset = *o;
2722 get_die_from_offset(source, cur_die_offset, &canonical_die);
2723 // compare die and canonical_die.
2724 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2725 die, &canonical_die,
2726 /*update_canonical_dies_on_the_fly=*/true))
2727 {
2728 set_canonical_die_offset(canonical_dies,
2729 die_offset,
2730 cur_die_offset);
2731 return true;
2732 }
2733 }
2734
2735 return false;
2736 }
2737
2738 /// Retrieve the canonical DIE of a given DIE.
2739 ///
2740 /// The canonical DIE is a DIE that is structurally equivalent to
2741 /// this one.
2742 ///
2743 /// Note that this function caches the canonical DIE that was
2744 /// computed. Subsequent invocations of this function on the same
2745 /// DIE return the same cached DIE.
2746 ///
2747 /// @param die the DIE to get a canonical type for.
2748 ///
2749 /// @param canonical_die the resulting canonical DIE.
2750 ///
2751 /// @param where the offset of the logical DIE we are supposed to be
2752 /// calling this function from. If set to zero this means this is
2753 /// to be ignored.
2754 ///
2755 /// @param die_as_type if true, consider DIE is a type.
2756 ///
2757 /// @return true if an *existing* canonical DIE was found.
2758 /// Otherwise, @p die is considered as being a canonical DIE for
2759 /// itself. @p canonical_die is thus set to the canonical die in
2760 /// either cases.
2761 bool
get_or_compute_canonical_die(const Dwarf_Die * die,Dwarf_Die & canonical_die,size_t where,bool die_as_type) const2762 get_or_compute_canonical_die(const Dwarf_Die* die,
2763 Dwarf_Die& canonical_die,
2764 size_t where,
2765 bool die_as_type) const
2766 {
2767 const die_source source = get_die_source(die);
2768
2769 offset_offset_map_type &canonical_dies =
2770 die_as_type
2771 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2772 get_container(source)
2773 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2774 get_container(source);
2775
2776 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2777
2778 if (Dwarf_Off canonical_die_offset =
2779 get_canonical_die_offset(canonical_dies,
2780 initial_die_offset))
2781 {
2782 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2783 return true;
2784 }
2785
2786 if (!is_type_die_to_be_canonicalized(die))
2787 return false;
2788
2789 // The map that associates the string representation of 'die'
2790 // with a vector of offsets of potentially equivalent DIEs.
2791 istring_dwarf_offsets_map_type& map =
2792 die_as_type
2793 ? (const_cast<reader*>(this)->
2794 type_die_repr_die_offsets_maps().get_container(*this, die))
2795 : (const_cast<reader*>(this)->
2796 decl_die_repr_die_offsets_maps().get_container(*this, die));
2797
2798 // The variable repr is the the string representation of 'die'.
2799 //
2800 // Even if die_as_type is true -- which means that 'die' is said
2801 // to be considered as a type -- we always consider a
2802 // DW_TAG_subprogram DIE as a decl here, as far as its string
2803 // representation is concerned.
2804 interned_string name =
2805 (die_as_type)
2806 ? get_die_pretty_type_representation(die, where)
2807 : get_die_pretty_representation(die, where);
2808
2809 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2810 if (i == map.end())
2811 {
2812 dwarf_offsets_type offsets;
2813 offsets.push_back(initial_die_offset);
2814 map[name] = offsets;
2815 get_die_from_offset(source, initial_die_offset, &canonical_die);
2816 set_canonical_die_offset(canonical_dies,
2817 initial_die_offset,
2818 initial_die_offset);
2819 return false;
2820 }
2821
2822 // walk i->second without any iterator (using a while loop rather
2823 // than a for loop) because compare_dies might add new content to
2824 // the end of the i->second vector during the walking.
2825 dwarf_offsets_type::size_type n = 0, s = i->second.size();
2826 while (n < s)
2827 {
2828 Dwarf_Off die_offset = i->second[n];
2829 get_die_from_offset(source, die_offset, &canonical_die);
2830 // compare die and canonical_die.
2831 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2832 die, &canonical_die,
2833 /*update_canonical_dies_on_the_fly=*/true))
2834 {
2835 set_canonical_die_offset(canonical_dies,
2836 initial_die_offset,
2837 die_offset);
2838 return true;
2839 }
2840 ++n;
2841 }
2842
2843 // We didn't find a canonical DIE for 'die'. So let's consider
2844 // that it is its own canonical DIE.
2845 get_die_from_offset(source, initial_die_offset, &canonical_die);
2846 i->second.push_back(initial_die_offset);
2847 set_canonical_die_offset(canonical_dies,
2848 initial_die_offset,
2849 initial_die_offset);
2850
2851 return false;
2852 }
2853
2854 /// Get the source of the DIE.
2855 ///
2856 /// The function returns an enumerator value saying if the DIE comes
2857 /// from the .debug_info section of the primary debug info file, the
2858 /// .debug_info section of the alternate debug info file, or the
2859 /// .debug_types section.
2860 ///
2861 /// @param die the DIE to get the source of.
2862 ///
2863 /// @return the source of the DIE if it could be determined,
2864 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2865 die_source
get_die_source(const Dwarf_Die * die) const2866 get_die_source(const Dwarf_Die *die) const
2867 {
2868 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2869 ABG_ASSERT(die);
2870 ABG_ASSERT(get_die_source(*die, source));
2871 return source;
2872 }
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 /// @param source out parameter. The function sets this parameter
2884 /// to the source of the DIE @p iff it returns true.
2885 ///
2886 /// @return true iff the source of the DIE could be determined and
2887 /// returned.
2888 bool
get_die_source(const Dwarf_Die & die,die_source & source) const2889 get_die_source(const Dwarf_Die &die, die_source &source) const
2890 {
2891 Dwarf_Die cu_die;
2892 Dwarf_Die cu_kind;
2893 uint8_t address_size = 0, offset_size = 0;
2894 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2895 &cu_die, &address_size,
2896 &offset_size))
2897 return false;
2898
2899 Dwarf_Half version = 0;
2900 Dwarf_Off abbrev_offset = 0;
2901 uint64_t type_signature = 0;
2902 Dwarf_Off type_offset = 0;
2903 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2904 &version, &abbrev_offset,
2905 &address_size, &offset_size,
2906 &type_signature, &type_offset))
2907 return false;
2908
2909 int tag = dwarf_tag(&cu_kind);
2910
2911 if (tag == DW_TAG_compile_unit
2912 || tag == DW_TAG_partial_unit)
2913 {
2914 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
2915 if (dwarf_debug_info() == die_dwarf)
2916 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
2917 else if (alternate_dwarf_debug_info() == die_dwarf)
2918 source = ALT_DEBUG_INFO_DIE_SOURCE;
2919 else
2920 ABG_ASSERT_NOT_REACHED;
2921 }
2922 else if (tag == DW_TAG_type_unit)
2923 source = TYPE_UNIT_DIE_SOURCE;
2924 else
2925 return false;
2926
2927 return true;
2928 }
2929
2930 /// Getter for the DIE designated by an offset.
2931 ///
2932 /// @param source the source of the DIE to get.
2933 ///
2934 /// @param offset the offset of the DIE to get.
2935 ///
2936 /// @param die the resulting DIE. The pointer has to point to an
2937 /// allocated memory region.
2938 void
get_die_from_offset(die_source source,Dwarf_Off offset,Dwarf_Die * die) const2939 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
2940 {
2941 if (source == TYPE_UNIT_DIE_SOURCE)
2942 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2943 offset, die));
2944 else
2945 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2946 offset, die));
2947 }
2948
2949 public:
2950
2951 /// Add an entry to the relevant die->decl map.
2952 ///
2953 /// @param die the DIE to add the the map.
2954 ///
2955 /// @param decl the decl to consider.
2956 ///
2957 /// @param where_offset where in the DIE stream we logically are.
2958 ///
2959 /// @param do_associate_by_repr if true then this function
2960 /// associates the representation string of @p die with the
2961 /// declaration @p decl, in a corpus-wide manner. That is, in the
2962 /// entire current corpus, there is going to be just one declaration
2963 /// associated with a DIE of the string representation of @p die.
2964 ///
2965 /// @param do_associate_by_repr_per_tu if true, then this function
2966 /// associates the representation string of @p die with the
2967 /// declaration @p decl in a translation unit wide manner. That is,
2968 /// in the entire current translation unit, there is going to be
2969 /// just one declaration associated with a DIE of the string
2970 /// representation of @p die.
2971 void
associate_die_to_decl(Dwarf_Die * die,decl_base_sptr decl,size_t where_offset,bool do_associate_by_repr=false)2972 associate_die_to_decl(Dwarf_Die* die,
2973 decl_base_sptr decl,
2974 size_t where_offset,
2975 bool do_associate_by_repr = false)
2976 {
2977 const die_source source = get_die_source(die);
2978
2979 die_artefact_map_type& m =
2980 decl_die_artefact_maps().get_container(source);
2981
2982 size_t die_offset;
2983 if (do_associate_by_repr)
2984 {
2985 Dwarf_Die equiv_die;
2986 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
2987 /*die_as_type=*/false))
2988 return;
2989 die_offset = dwarf_dieoffset(&equiv_die);
2990 }
2991 else
2992 die_offset = dwarf_dieoffset(die);
2993
2994 m[die_offset] = decl;
2995 }
2996
2997 /// Lookup the decl for a given DIE.
2998 ///
2999 /// The returned decl is either the decl of the DIE that as the
3000 /// exact offset @p die_offset
3001 /// die_offset, or
3002 /// give
3003 ///
3004 /// @param die_offset the offset of the DIE to consider.
3005 ///
3006 /// @param source where the DIE represented by @p die_offset comes
3007 /// from.
3008 ///
3009 /// Note that "alternate debug info sections" is a GNU extension as
3010 /// of DWARF4 and is described at
3011 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3012 ///
3013 /// @return the resulting decl, or null if no decl is associated to
3014 /// the DIE represented by @p die_offset.
3015 decl_base_sptr
lookup_decl_from_die_offset(Dwarf_Off die_offset,die_source source)3016 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3017 {
3018 decl_base_sptr result =
3019 is_decl(lookup_artifact_from_die_offset(die_offset, source,
3020 /*die_as_type=*/false));
3021
3022 return result;
3023 }
3024
3025 /// Get the qualified name of a given DIE.
3026 ///
3027 /// If the name of the DIE was already computed before just return
3028 /// that name from a cache. Otherwise, build the name, cache it and
3029 /// return it.
3030 ///
3031 /// @param die the DIE to consider.
3032 ///
3033 /// @param where_offset where in the DIE stream we logically are.
3034 ///
3035 /// @return the interned string representing the qualified name of
3036 /// @p die.
3037 interned_string
get_die_qualified_name(Dwarf_Die * die,size_t where_offset)3038 get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3039 {
3040 ABG_ASSERT(die);
3041 die_istring_map_type& map =
3042 die_qualified_name_maps_.get_container(*this, die);
3043
3044 size_t die_offset = dwarf_dieoffset(die);
3045 die_istring_map_type::const_iterator i = map.find(die_offset);
3046
3047 if (i == map.end())
3048 {
3049 reader& rdr = *const_cast<reader*>(this);
3050 string qualified_name = die_qualified_name(rdr, die, where_offset);
3051 interned_string istr = env().intern(qualified_name);
3052 map[die_offset] = istr;
3053 return istr;
3054 }
3055
3056 return i->second;
3057 }
3058
3059 /// Get the qualified name of a given DIE.
3060 ///
3061 /// If the name of the DIE was already computed before just return
3062 /// that name from a cache. Otherwise, build the name, cache it and
3063 /// return it.
3064 ///
3065 /// @param die the DIE to consider.
3066 ///
3067 /// @param where_offset where in the DIE stream we logically are.
3068 ///
3069 /// @return the interned string representing the qualified name of
3070 /// @p die.
3071 interned_string
get_die_qualified_name(Dwarf_Die * die,size_t where_offset) const3072 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3073 {
3074 return const_cast<reader*>(this)->
3075 get_die_qualified_name(die, where_offset);
3076 }
3077
3078 /// Get the qualified name of a given DIE which is considered to be
3079 /// the DIE for a type.
3080 ///
3081 /// For instance, for a DW_TAG_subprogram DIE, this function
3082 /// computes the name of the function *type* that corresponds to the
3083 /// function.
3084 ///
3085 /// If the name of the DIE was already computed before just return
3086 /// that name from a cache. Otherwise, build the name, cache it and
3087 /// return it.
3088 ///
3089 /// @param die the DIE to consider.
3090 ///
3091 /// @param where_offset where in the DIE stream we logically are.
3092 ///
3093 /// @return the interned string representing the qualified name of
3094 /// @p die.
3095 interned_string
get_die_qualified_type_name(const Dwarf_Die * die,size_t where_offset) const3096 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3097 {
3098 ABG_ASSERT(die);
3099
3100 // The name of the translation unit die is "".
3101 if (die == cur_tu_die())
3102 return env().intern("");
3103
3104 die_istring_map_type& map =
3105 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3106 die);
3107
3108 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3109 die_istring_map_type::const_iterator i =
3110 map.find(die_offset);
3111
3112 if (i == map.end())
3113 {
3114 reader& rdr = *const_cast<reader*>(this);
3115 string qualified_name;
3116 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3117 if ((tag == DW_TAG_structure_type
3118 || tag == DW_TAG_class_type
3119 || tag == DW_TAG_union_type)
3120 && die_is_anonymous(die))
3121 {
3122 location l = die_location(*this, die);
3123 qualified_name = l ? l.expand() : "noloc";
3124 qualified_name = "unnamed-at-" + qualified_name;
3125 }
3126 else
3127 qualified_name =
3128 die_qualified_type_name(rdr, die, where_offset);
3129
3130 interned_string istr = env().intern(qualified_name);
3131 map[die_offset] = istr;
3132 return istr;
3133 }
3134
3135 return i->second;
3136 }
3137
3138 /// Get the pretty representation of a DIE that represents a type.
3139 ///
3140 /// For instance, for the DW_TAG_subprogram, this function computes
3141 /// the pretty representation of the type of the function, not the
3142 /// pretty representation of the function declaration.
3143 ///
3144 /// Once the pretty representation is computed, it's stored in a
3145 /// cache. Subsequent invocations of this function on the same DIE
3146 /// will yield the cached name.
3147 ///
3148 /// @param die the DIE to consider.
3149 ///
3150 /// @param where_offset where in the DIE stream we logically are.
3151 ///
3152 /// @return the interned_string that represents the pretty
3153 /// representation.
3154 interned_string
get_die_pretty_type_representation(const Dwarf_Die * die,size_t where_offset) const3155 get_die_pretty_type_representation(const Dwarf_Die *die,
3156 size_t where_offset) const
3157 {
3158 ABG_ASSERT(die);
3159 die_istring_map_type& map =
3160 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3161 die);
3162
3163 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3164 die_istring_map_type::const_iterator i = map.find(die_offset);
3165
3166 if (i == map.end())
3167 {
3168 reader& rdr = *const_cast<reader*>(this);
3169 string pretty_representation =
3170 die_pretty_print_type(rdr, die, where_offset);
3171 interned_string istr = env().intern(pretty_representation);
3172 map[die_offset] = istr;
3173 return istr;
3174 }
3175
3176 return i->second;
3177 }
3178
3179 /// Get the pretty representation of a DIE.
3180 ///
3181 /// Once the pretty representation is computed, it's stored in a
3182 /// cache. Subsequent invocations of this function on the same DIE
3183 /// will yield the cached name.
3184 ///
3185 /// @param die the DIE to consider.
3186 ///
3187 /// @param where_offset where in the DIE stream we logically are.
3188 ///
3189 /// @return the interned_string that represents the pretty
3190 /// representation.
3191 interned_string
get_die_pretty_representation(const Dwarf_Die * die,size_t where_offset) const3192 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3193 {
3194 ABG_ASSERT(die);
3195
3196 die_istring_map_type& map =
3197 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3198 die);
3199
3200 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3201 die_istring_map_type::const_iterator i = map.find(die_offset);
3202
3203 if (i == map.end())
3204 {
3205 reader& rdr = *const_cast<reader*>(this);
3206 string pretty_representation =
3207 die_pretty_print(rdr, die, where_offset);
3208 interned_string istr = env().intern(pretty_representation);
3209 map[die_offset] = istr;
3210 return istr;
3211 }
3212
3213 return i->second;
3214 }
3215
3216 /// Lookup the artifact that was built to represent a type that has
3217 /// the same pretty representation as the type denoted by a given
3218 /// DIE.
3219 ///
3220 /// Note that the DIE must have previously been associated with the
3221 /// artifact using the functions associate_die_to_decl or
3222 /// associate_die_to_type.
3223 ///
3224 /// Also, note that the scope of the lookup is the current ABI
3225 /// corpus.
3226 ///
3227 /// @param die the DIE to consider.
3228 ///
3229 /// @param where_offset where in the DIE stream we logically are.
3230 ///
3231 /// @return the type artifact found.
3232 type_or_decl_base_sptr
lookup_type_artifact_from_die(Dwarf_Die * die) const3233 lookup_type_artifact_from_die(Dwarf_Die *die) const
3234 {
3235 type_or_decl_base_sptr artifact =
3236 lookup_artifact_from_die(die, /*type_as_die=*/true);
3237 if (function_decl_sptr fn = is_function_decl(artifact))
3238 return fn->get_type();
3239 return artifact;
3240 }
3241
3242 /// Lookup the artifact that was built to represent a type or a
3243 /// declaration that has the same pretty representation as the type
3244 /// denoted by a given DIE.
3245 ///
3246 /// Note that the DIE must have previously been associated with the
3247 /// artifact using the functions associate_die_to_decl or
3248 /// associate_die_to_type.
3249 ///
3250 /// Also, note that the scope of the lookup is the current ABI
3251 /// corpus.
3252 ///
3253 /// @param die the DIE to consider.
3254 ///
3255 /// @param where_offset where in the DIE stream we logically are.
3256 ///
3257 /// @param die_as_type if true, it means the DIE is to be considered
3258 /// as a type.
3259 ///
3260 /// @return the artifact found.
3261 type_or_decl_base_sptr
lookup_artifact_from_die(const Dwarf_Die * die,bool die_as_type=false) const3262 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3263 {
3264 Dwarf_Die equiv_die;
3265 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3266 return type_or_decl_base_sptr();
3267
3268 const die_artefact_map_type& m =
3269 die_as_type
3270 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3271 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3272
3273 size_t die_offset = dwarf_dieoffset(&equiv_die);
3274 die_artefact_map_type::const_iterator i = m.find(die_offset);
3275
3276 if (i == m.end())
3277 return type_or_decl_base_sptr();
3278 return i->second;
3279 }
3280
3281 /// Lookup the artifact that was built to represent a type or a
3282 /// declaration that has the same pretty representation as the type
3283 /// denoted by the offset of a given DIE.
3284 ///
3285 /// Note that the DIE must have previously been associated with the
3286 /// artifact using either associate_die_to_decl or
3287 /// associate_die_to_type.
3288 ///
3289 /// Also, note that the scope of the lookup is the current ABI
3290 /// corpus.
3291 ///
3292 /// @param die the DIE to consider.
3293 ///
3294 /// @param where_offset where in the DIE stream we logically are.
3295 ///
3296 /// @param die_as_type if true, it means the DIE is to be considered
3297 /// as a type.
3298 ///
3299 /// @return the artifact found.
3300 type_or_decl_base_sptr
lookup_artifact_from_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type=false) const3301 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3302 die_source source,
3303 bool die_as_type = false) const
3304 {
3305 const die_artefact_map_type& m =
3306 die_as_type
3307 ? type_die_artefact_maps().get_container(source)
3308 : decl_die_artefact_maps().get_container(source);
3309
3310 die_artefact_map_type::const_iterator i = m.find(die_offset);
3311 if (i == m.end())
3312 return type_or_decl_base_sptr();
3313 return i->second;
3314 }
3315
3316 /// Get the language used to generate a given DIE.
3317 ///
3318 /// @param die the DIE to consider.
3319 ///
3320 /// @param lang the resulting language.
3321 ///
3322 /// @return true iff the language of the DIE was found.
3323 bool
get_die_language(const Dwarf_Die * die,translation_unit::language & lang) const3324 get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
3325 {
3326 Dwarf_Die cu_die;
3327 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
3328
3329 uint64_t l = 0;
3330 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
3331 return false;
3332
3333 lang = dwarf_language_to_tu_language(l);
3334 return true;
3335 }
3336
3337 /// Test if a given DIE originates from a program written in the C
3338 /// language.
3339 ///
3340 /// @param die the DIE to consider.
3341 ///
3342 /// @return true iff @p die originates from a program in the C
3343 /// language.
3344 bool
die_is_in_c(const Dwarf_Die * die) const3345 die_is_in_c(const Dwarf_Die *die) const
3346 {
3347 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3348 if (!get_die_language(die, l))
3349 return false;
3350 return is_c_language(l);
3351 }
3352
3353 /// Test if a given DIE originates from a program written in the C++
3354 /// language.
3355 ///
3356 /// @param die the DIE to consider.
3357 ///
3358 /// @return true iff @p die originates from a program in the C++
3359 /// language.
3360 bool
die_is_in_cplus_plus(const Dwarf_Die * die) const3361 die_is_in_cplus_plus(const Dwarf_Die *die) const
3362 {
3363 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3364 if (!get_die_language(die, l))
3365 return false;
3366 return is_cplus_plus_language(l);
3367 }
3368
3369 /// Test if a given DIE originates from a program written either in
3370 /// C or C++.
3371 ///
3372 /// @param die the DIE to consider.
3373 ///
3374 /// @return true iff @p die originates from a program written either in
3375 /// C or C++.
3376 bool
die_is_in_c_or_cplusplus(const Dwarf_Die * die) const3377 die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
3378 {
3379 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3380 if (!get_die_language(die, l))
3381 return false;
3382 return (is_cplus_plus_language(l) || is_c_language(l));
3383 }
3384
3385 /// Check if we can assume the One Definition Rule[1] to be relevant
3386 /// for the current translation unit.
3387 ///
3388 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3389 ///
3390 /// At the moment this returns true if the current translation unit
3391 /// is in C++ language. In that case, it's relevant to assume that
3392 /// we use optimizations based on the ODR.
3393 bool
odr_is_relevant() const3394 odr_is_relevant() const
3395 {return odr_is_relevant(cur_transl_unit()->get_language());}
3396
3397 /// Check if we can assume the One Definition Rule[1] to be relevant
3398 /// for a given language.
3399 ///
3400 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3401 ///
3402 /// At the moment this returns true if the language considered
3403 /// is C++, Java or Ada.
3404 bool
odr_is_relevant(translation_unit::language l) const3405 odr_is_relevant(translation_unit::language l) const
3406 {
3407 return (is_cplus_plus_language(l)
3408 || is_java_language(l)
3409 || is_ada_language(l));
3410 }
3411
3412 /// Check if we can assume the One Definition Rule to be relevant
3413 /// for a given DIE.
3414 ///
3415 /// @param die the DIE to consider.
3416 ///
3417 /// @return true if the ODR is relevant for @p die.
3418 bool
odr_is_relevant(Dwarf_Off die_offset,die_source source) const3419 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3420 {
3421 Dwarf_Die die;
3422 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3423 die_offset, &die));
3424 return odr_is_relevant(&die);
3425 }
3426
3427 /// Check if we can assume the One Definition Rule to be relevant
3428 /// for a given DIE.
3429 ///
3430 /// @param die the DIE to consider.
3431 ///
3432 /// @return true if the ODR is relevant for @p die.
3433 bool
odr_is_relevant(const Dwarf_Die * die) const3434 odr_is_relevant(const Dwarf_Die *die) const
3435 {
3436 translation_unit::language lang;
3437 if (!get_die_language(die, lang))
3438 return odr_is_relevant();
3439
3440 return odr_is_relevant(lang);
3441 }
3442
3443 /// Getter for the maps set that associates a decl DIE offset to an
3444 /// artifact.
3445 ///
3446 /// @return the maps set that associates a decl DIE offset to an
3447 /// artifact.
3448 die_source_dependant_container_set<die_artefact_map_type>&
decl_die_artefact_maps()3449 decl_die_artefact_maps()
3450 {return decl_die_artefact_maps_;}
3451
3452 /// Getter for the maps set that associates a decl DIE offset to an
3453 /// artifact.
3454 ///
3455 /// @return the maps set that associates a decl DIE offset to an
3456 /// artifact.
3457 const die_source_dependant_container_set<die_artefact_map_type>&
decl_die_artefact_maps() const3458 decl_die_artefact_maps() const
3459 {return decl_die_artefact_maps_;}
3460
3461 /// Getter for the maps set that associates a type DIE offset to an
3462 /// artifact.
3463 ///
3464 /// @return the maps set that associates a type DIE offset to an
3465 /// artifact.
3466 die_source_dependant_container_set<die_artefact_map_type>&
type_die_artefact_maps()3467 type_die_artefact_maps()
3468 {return type_die_artefact_maps_;}
3469
3470 /// Getter for the maps set that associates a type DIE offset to an
3471 /// artifact.
3472 ///
3473 /// @return the maps set that associates a type DIE offset to an
3474 /// artifact.
3475 const die_source_dependant_container_set<die_artefact_map_type>&
type_die_artefact_maps() const3476 type_die_artefact_maps() const
3477 {return type_die_artefact_maps_;}
3478
3479 /// Getter of the maps that associates function type representations
3480 /// to function types, inside a translation unit.
3481 ///
3482 /// @return the maps that associates function type representations
3483 /// to function types, inside a translation unit.
3484 istring_fn_type_map_type&
per_tu_repr_to_fn_type_maps()3485 per_tu_repr_to_fn_type_maps()
3486 {return per_tu_repr_to_fn_type_maps_;}
3487
3488 /// Getter of the maps that associates function type representations
3489 /// to function types, inside a translation unit.
3490 ///
3491 /// @return the maps that associates function type representations
3492 /// to function types, inside a translation unit.
3493 const istring_fn_type_map_type&
per_tu_repr_to_fn_type_maps() const3494 per_tu_repr_to_fn_type_maps() const
3495 {return per_tu_repr_to_fn_type_maps_;}
3496
3497 /// Associate the representation of a function type DIE to a given
3498 /// function type, inside the current translation unit.
3499 ///
3500 /// @param die the DIE to associate to the function type, using its
3501 /// representation.
3502 ///
3503 /// @param fn_type the function type to associate to @p die.
3504 void
associate_die_repr_to_fn_type_per_tu(const Dwarf_Die * die,const function_type_sptr & fn_type)3505 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3506 const function_type_sptr &fn_type)
3507 {
3508 if (!die_is_function_type(die))
3509 return;
3510
3511 interned_string repr =
3512 get_die_pretty_type_representation(die, /*where=*/0);
3513 ABG_ASSERT(!repr.empty());
3514
3515 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3516 }
3517
3518 /// Lookup the function type associated to a given function type
3519 /// DIE, in the current translation unit.
3520 ///
3521 /// @param die the DIE of function type to consider.
3522 ///
3523 /// @return the @ref function_type_sptr associated to @p die, or nil
3524 /// of no function_type is associated to @p die.
3525 function_type_sptr
lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die * die)3526 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3527 {
3528 if (!die_is_function_type(die))
3529 return function_type_sptr();
3530
3531 interned_string repr = die_name(die).empty() ?
3532 get_die_pretty_type_representation(die, /*where=*/0)
3533 : get_die_pretty_representation(die, /*where=*/0);
3534 ABG_ASSERT(!repr.empty());
3535
3536 istring_fn_type_map_type::const_iterator i =
3537 per_tu_repr_to_fn_type_maps().find(repr);
3538
3539 if (i == per_tu_repr_to_fn_type_maps().end())
3540 return function_type_sptr();
3541
3542 return i->second;
3543 }
3544
3545 /// Set the canonical DIE offset of a given DIE.
3546 ///
3547 /// @param canonical_dies the vector that holds canonical DIEs.
3548 ///
3549 /// @param die_offset the offset of the DIE to set the canonical DIE
3550 /// for.
3551 ///
3552 /// @param canonical_die_offset the canonical DIE offset to
3553 /// associate to @p die_offset.
3554 void
set_canonical_die_offset(offset_offset_map_type & canonical_dies,Dwarf_Off die_offset,Dwarf_Off canonical_die_offset) const3555 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3556 Dwarf_Off die_offset,
3557 Dwarf_Off canonical_die_offset) const
3558 {
3559 canonical_dies[die_offset] = canonical_die_offset;}
3560
3561 /// Set the canonical DIE offset of a given DIE.
3562 ///
3563 ///
3564 /// @param die_offset the offset of the DIE to set the canonical DIE
3565 /// for.
3566 ///
3567 /// @param source the source of the DIE denoted by @p die_offset.
3568 ///
3569 /// @param canonical_die_offset the canonical DIE offset to
3570 /// associate to @p die_offset.
3571 ///
3572 /// @param die_as_type if true, it means that @p die_offset has to
3573 /// be considered as a type.
3574 void
set_canonical_die_offset(Dwarf_Off die_offset,die_source source,Dwarf_Off canonical_die_offset,bool die_as_type) const3575 set_canonical_die_offset(Dwarf_Off die_offset,
3576 die_source source,
3577 Dwarf_Off canonical_die_offset,
3578 bool die_as_type) const
3579 {
3580 offset_offset_map_type &canonical_dies =
3581 die_as_type
3582 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3583 get_container(source)
3584 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3585 get_container(source);
3586
3587 set_canonical_die_offset(canonical_dies,
3588 die_offset,
3589 canonical_die_offset);
3590 }
3591
3592 /// Set the canonical DIE offset of a given DIE.
3593 ///
3594 ///
3595 /// @param die the DIE to set the canonical DIE for.
3596 ///
3597 /// @param canonical_die_offset the canonical DIE offset to
3598 /// associate to @p die_offset.
3599 ///
3600 /// @param die_as_type if true, it means that @p die has to be
3601 /// considered as a type.
3602 void
set_canonical_die_offset(const Dwarf_Die * die,Dwarf_Off canonical_die_offset,bool die_as_type) const3603 set_canonical_die_offset(const Dwarf_Die *die,
3604 Dwarf_Off canonical_die_offset,
3605 bool die_as_type) const
3606 {
3607 const die_source source = get_die_source(die);
3608
3609 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3610
3611 set_canonical_die_offset(die_offset, source,
3612 canonical_die_offset,
3613 die_as_type);
3614 }
3615
3616 /// Get the canonical DIE offset of a given DIE.
3617 ///
3618 /// @param canonical_dies the vector that contains canonical DIES.
3619 ///
3620 /// @param die_offset the offset of the DIE to consider.
3621 ///
3622 /// @return the canonical of the DIE denoted by @p die_offset, or
3623 /// zero if no canonical DIE was found.
3624 Dwarf_Off
get_canonical_die_offset(offset_offset_map_type & canonical_dies,Dwarf_Off die_offset) const3625 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3626 Dwarf_Off die_offset) const
3627 {
3628 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3629 if (it == canonical_dies.end())
3630 return 0;
3631 return it->second;
3632 }
3633
3634 /// Get the canonical DIE offset of a given DIE.
3635 ///
3636 /// @param die_offset the offset of the DIE to consider.
3637 ///
3638 /// @param source the source of the DIE denoted by @p die_offset.
3639 ///
3640 /// @param die_as_type if true, it means that @p is to be considered
3641 /// as a type DIE.
3642 ///
3643 /// @return the canonical of the DIE denoted by @p die_offset, or
3644 /// zero if no canonical DIE was found.
3645 Dwarf_Off
get_canonical_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type) const3646 get_canonical_die_offset(Dwarf_Off die_offset,
3647 die_source source,
3648 bool die_as_type) const
3649 {
3650 offset_offset_map_type &canonical_dies =
3651 die_as_type
3652 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3653 get_container(source)
3654 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3655 get_container(source);
3656
3657 return get_canonical_die_offset(canonical_dies, die_offset);
3658 }
3659
3660 /// Erase the canonical type of a given DIE.
3661 ///
3662 /// @param die_offset the offset of the DIE to consider.
3663 ///
3664 /// @param source the source of the canonical type.
3665 ///
3666 /// @param die_as_type if true, it means that @p is to be considered
3667 /// as a type DIE.
3668 ///
3669 /// @return the canonical of the DIE denoted by @p die_offset, or
3670 /// zero if no canonical DIE was found and erased..
3671 bool
erase_canonical_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type) const3672 erase_canonical_die_offset(Dwarf_Off die_offset,
3673 die_source source,
3674 bool die_as_type) const
3675 {
3676 offset_offset_map_type &canonical_dies =
3677 die_as_type
3678 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3679 get_container(source)
3680 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3681 get_container(source);
3682
3683 return canonical_dies.erase(die_offset);
3684 }
3685
3686
3687 /// Associate a DIE (representing a type) to the type that it
3688 /// represents.
3689 ///
3690 /// @param die the DIE to consider.
3691 ///
3692 /// @param type the type to associate the DIE to.
3693 ///
3694 /// @param where_offset where in the DIE stream we logically are.
3695 void
associate_die_to_type(const Dwarf_Die * die,type_base_sptr type,size_t where)3696 associate_die_to_type(const Dwarf_Die *die,
3697 type_base_sptr type,
3698 size_t where)
3699 {
3700 if (!type)
3701 return;
3702
3703 Dwarf_Die equiv_die;
3704 if (!get_or_compute_canonical_die(die, equiv_die, where,
3705 /*die_as_type=*/true))
3706 return;
3707
3708 die_artefact_map_type& m =
3709 type_die_artefact_maps().get_container(*this, &equiv_die);
3710
3711 size_t die_offset = dwarf_dieoffset(&equiv_die);
3712 m[die_offset] = type;
3713 }
3714
3715 /// Lookup the type associated to a given DIE.
3716 ///
3717 /// Note that the DIE must have been associated to type by a
3718 /// previous invocation of the function
3719 /// reader::associate_die_to_type().
3720 ///
3721 /// @param die the DIE to consider.
3722 ///
3723 /// @return the type associated to the DIE or NULL if no type is
3724 /// associated to the DIE.
3725 type_base_sptr
lookup_type_from_die(const Dwarf_Die * die) const3726 lookup_type_from_die(const Dwarf_Die* die) const
3727 {
3728 type_or_decl_base_sptr artifact =
3729 lookup_artifact_from_die(die, /*die_as_type=*/true);
3730 if (function_decl_sptr fn = is_function_decl(artifact))
3731 return fn->get_type();
3732 return is_type(artifact);
3733 }
3734
3735 /// Lookup the type associated to a DIE at a given offset, from a
3736 /// given source.
3737 ///
3738 /// Note that the DIE must have been associated to type by a
3739 /// previous invocation of the function
3740 /// reader::associate_die_to_type().
3741 ///
3742 /// @param die_offset the offset of the DIE to consider.
3743 ///
3744 /// @param source the source of the DIE to consider.
3745 ///
3746 /// @return the type associated to the DIE or NULL if no type is
3747 /// associated to the DIE.
3748 type_base_sptr
lookup_type_from_die_offset(size_t die_offset,die_source source) const3749 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3750 {
3751 type_base_sptr result;
3752 const die_artefact_map_type& m =
3753 type_die_artefact_maps().get_container(source);
3754 die_artefact_map_type::const_iterator i = m.find(die_offset);
3755 if (i != m.end())
3756 {
3757 if (function_decl_sptr fn = is_function_decl(i->second))
3758 return fn->get_type();
3759 result = is_type(i->second);
3760 }
3761
3762 if (!result)
3763 {
3764 // Maybe we are looking for a class type being constructed?
3765 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3766 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3767
3768 if (i != m.end())
3769 result = i->second;
3770 }
3771
3772 if (!result)
3773 {
3774 // Maybe we are looking for a function type being constructed?
3775 const die_function_type_map_type& m =
3776 die_wip_function_types_map(source);
3777 die_function_type_map_type::const_iterator i = m.find(die_offset);
3778
3779 if (i != m.end())
3780 result = i->second;
3781 }
3782
3783 return result;
3784 }
3785
3786 /// Getter of a map that associates a die that represents a
3787 /// class/struct with the declaration of the class, while the class
3788 /// is being constructed.
3789 ///
3790 /// @param source where the DIE is from.
3791 ///
3792 /// @return the map that associates a DIE to the class that is being
3793 /// built.
3794 const die_class_or_union_map_type&
die_wip_classes_map(die_source source) const3795 die_wip_classes_map(die_source source) const
3796 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3797
3798 /// Getter of a map that associates a die that represents a
3799 /// class/struct with the declaration of the class, while the class
3800 /// is being constructed.
3801 ///
3802 /// @param source where the DIE comes from.
3803 ///
3804 /// @return the map that associates a DIE to the class that is being
3805 /// built.
3806 die_class_or_union_map_type&
die_wip_classes_map(die_source source)3807 die_wip_classes_map(die_source source)
3808 {
3809 switch (source)
3810 {
3811 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3812 break;
3813 case ALT_DEBUG_INFO_DIE_SOURCE:
3814 return alternate_die_wip_classes_map_;
3815 case TYPE_UNIT_DIE_SOURCE:
3816 return type_unit_die_wip_classes_map_;
3817 case NO_DEBUG_INFO_DIE_SOURCE:
3818 case NUMBER_OF_DIE_SOURCES:
3819 ABG_ASSERT_NOT_REACHED;
3820 }
3821 return die_wip_classes_map_;
3822 }
3823
3824 /// Getter for a map that associates a die (that represents a
3825 /// function type) whith a function type, while the function type is
3826 /// being constructed (WIP == work in progress).
3827 ///
3828 /// @param source where the DIE comes from.n
3829 ///
3830 /// @return the map of wip function types.
3831 const die_function_type_map_type&
die_wip_function_types_map(die_source source) const3832 die_wip_function_types_map(die_source source) const
3833 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3834
3835 /// Getter for a map that associates a die (that represents a
3836 /// function type) whith a function type, while the function type is
3837 /// being constructed (WIP == work in progress).
3838 ///
3839 /// @param source where DIEs of the map come from.
3840 ///
3841 /// @return the map of wip function types.
3842 die_function_type_map_type&
die_wip_function_types_map(die_source source)3843 die_wip_function_types_map(die_source source)
3844 {
3845 switch (source)
3846 {
3847 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3848 break;
3849 case ALT_DEBUG_INFO_DIE_SOURCE:
3850 return alternate_die_wip_function_types_map_;
3851 case TYPE_UNIT_DIE_SOURCE:
3852 return type_unit_die_wip_function_types_map_;
3853 case NO_DEBUG_INFO_DIE_SOURCE:
3854 case NUMBER_OF_DIE_SOURCES:
3855 ABG_ASSERT_NOT_REACHED;
3856 }
3857 return die_wip_function_types_map_;
3858 }
3859
3860 /// Getter for a map that associates a die with a function decl
3861 /// which has a linkage name but no elf symbol yet.
3862 ///
3863 /// This is to fixup function decls with linkage names, but with no
3864 /// link to their underlying elf symbol. There are some DIEs like
3865 /// that in DWARF sometimes, especially when the compiler optimizes
3866 /// stuff aggressively.
3867 die_function_decl_map_type&
die_function_decl_with_no_symbol_map()3868 die_function_decl_with_no_symbol_map()
3869 {return die_function_with_no_symbol_map_;}
3870
3871 /// Return true iff a given offset is for the DIE of a class that is
3872 /// being built, but that is not fully built yet. WIP == "work in
3873 /// progress".
3874 ///
3875 /// @param offset the DIE offset to consider.
3876 ///
3877 /// @param source where the DIE of the map come from.
3878 ///
3879 /// @return true iff @p offset is the offset of the DIE of a class
3880 /// that is being currently built.
3881 bool
is_wip_class_die_offset(Dwarf_Off offset,die_source source) const3882 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3883 {
3884 die_class_or_union_map_type::const_iterator i =
3885 die_wip_classes_map(source).find(offset);
3886 return (i != die_wip_classes_map(source).end());
3887 }
3888
3889 /// Return true iff a given offset is for the DIE of a function type
3890 /// that is being built at the moment, but is not fully built yet.
3891 /// WIP == work in progress.
3892 ///
3893 /// @param offset DIE offset to consider.
3894 ///
3895 /// @param source where the DIE comes from.
3896 ///
3897 /// @return true iff @p offset is the offset of the DIE of a
3898 /// function type that is being currently built.
3899 bool
is_wip_function_type_die_offset(Dwarf_Off offset,die_source source) const3900 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3901 {
3902 die_function_type_map_type::const_iterator i =
3903 die_wip_function_types_map(source).find(offset);
3904 return (i != die_wip_function_types_map(source).end());
3905 }
3906
3907 /// Sometimes, a data member die can erroneously have an empty name as
3908 /// a result of a bug of the DWARF emitter.
3909 ///
3910 /// This is what happens in
3911 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3912 ///
3913 /// In that case, this function constructs an artificial name for that
3914 /// data member. The pattern of the name is as follows:
3915 ///
3916 /// "unnamed-@-<location>".
3917 ///
3918 ///location is either the value of the data member location of the
3919 ///data member if it has one or concatenation of its source location
3920 ///if it has none. If no location can be calculated then the function
3921 ///returns the empty string.
3922 string
build_name_for_buggy_anonymous_data_member(Dwarf_Die * die)3923 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
3924 {
3925 string result;
3926 // Let's make sure we are looking at a data member with an empty
3927 // name ...
3928 if (!die
3929 || dwarf_tag(die) != DW_TAG_member
3930 || !die_name(die).empty())
3931 return result;
3932
3933 // ... and yet, it's not an anonymous data member (aka unnamed
3934 // field) as described in
3935 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3936 if (die_is_anonymous_data_member(die))
3937 return result;
3938
3939 // If we come this far, it means we are looking at a buggy data
3940 // member with no name. Let's build a name for it so that it can be
3941 // addressed.
3942 int64_t offset_in_bits = 0;
3943 bool has_offset = die_member_offset(*this, die, offset_in_bits);
3944 location loc;
3945 if (!has_offset)
3946 {
3947 loc = die_location(*this, die);
3948 if (!loc)
3949 return result;
3950 }
3951
3952 std::ostringstream o;
3953 o << "unnamed-dm-@-";
3954 if (has_offset)
3955 o << "offset-" << offset_in_bits << "bits";
3956 else
3957 o << "loc-" << loc.expand();
3958
3959 return o.str();
3960 }
3961
3962 /// Getter for the map of declaration-only classes that are to be
3963 /// resolved to their definition classes by the end of the corpus
3964 /// loading.
3965 ///
3966 /// @return a map of string -> vector of classes where the key is
3967 /// the fully qualified name of the class and the value is the
3968 /// vector of declaration-only class.
3969 const string_classes_or_unions_map&
declaration_only_classes() const3970 declaration_only_classes() const
3971 {return decl_only_classes_map_;}
3972
3973 /// Getter for the map of declaration-only classes that are to be
3974 /// resolved to their definition classes by the end of the corpus
3975 /// loading.
3976 ///
3977 /// @return a map of string -> vector of classes where the key is
3978 /// the fully qualified name of the class and the value is the
3979 /// vector of declaration-only class.
3980 string_classes_or_unions_map&
declaration_only_classes()3981 declaration_only_classes()
3982 {return decl_only_classes_map_;}
3983
3984 /// If a given class is a declaration-only class then stash it on
3985 /// the side so that at the end of the corpus reading we can resolve
3986 /// it to its definition.
3987 ///
3988 /// @param klass the class to consider.
3989 void
maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr & cou)3990 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
3991 {
3992 if (cou->get_is_declaration_only()
3993 && cou->get_definition_of_declaration() == 0)
3994 {
3995 string qn = cou->get_qualified_name();
3996 string_classes_or_unions_map::iterator record =
3997 declaration_only_classes().find(qn);
3998 if (record == declaration_only_classes().end())
3999 declaration_only_classes()[qn].push_back(cou);
4000 else
4001 record->second.push_back(cou);
4002 }
4003 }
4004
4005 /// Test if a given declaration-only class has been scheduled for
4006 /// resolution to a defined class.
4007 ///
4008 /// @param klass the class to consider for the test.
4009 ///
4010 /// @return true iff @p klass is a declaration-only class and if
4011 /// it's been scheduled for resolution to a defined class.
4012 bool
is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr & cou)4013 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4014 {
4015 if (cou->get_is_declaration_only())
4016 return (declaration_only_classes().find(cou->get_qualified_name())
4017 != declaration_only_classes().end());
4018
4019 return false;
4020 }
4021
4022 /// Compare two ABI artifacts in a context which canonicalization
4023 /// has not be done yet.
4024 ///
4025 /// @param l the left-hand-side operand of the comparison
4026 ///
4027 /// @param r the right-hand-side operand of the comparison.
4028 ///
4029 /// @return true if @p l equals @p r.
4030 bool
compare_before_canonicalisation(const type_or_decl_base_sptr & l,const type_or_decl_base_sptr & r)4031 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4032 const type_or_decl_base_sptr &r)
4033 {
4034 if (!l || !r)
4035 return !!l == !!r;
4036
4037 const environment& e = l->get_environment();
4038 ABG_ASSERT(!e.canonicalization_is_done());
4039
4040 e.priv_->allow_type_comparison_results_caching(true);
4041 bool s0 = e.decl_only_class_equals_definition();
4042 e.decl_only_class_equals_definition(true);
4043 bool equal = l == r;
4044 e.decl_only_class_equals_definition(s0);
4045 e.priv_->clear_type_comparison_results_cache();
4046 e.priv_->allow_type_comparison_results_caching(false);
4047 return equal;
4048 }
4049
4050 /// Walk the declaration-only classes that have been found during
4051 /// the building of the corpus and resolve them to their definitions.
4052 void
resolve_declaration_only_classes()4053 resolve_declaration_only_classes()
4054 {
4055 vector<string> resolved_classes;
4056
4057 for (string_classes_or_unions_map::iterator i =
4058 declaration_only_classes().begin();
4059 i != declaration_only_classes().end();
4060 ++i)
4061 {
4062 bool to_resolve = false;
4063 for (classes_or_unions_type::iterator j = i->second.begin();
4064 j != i->second.end();
4065 ++j)
4066 if ((*j)->get_is_declaration_only()
4067 && ((*j)->get_definition_of_declaration() == 0))
4068 to_resolve = true;
4069
4070 if (!to_resolve)
4071 {
4072 resolved_classes.push_back(i->first);
4073 continue;
4074 }
4075
4076 // Now, for each decl-only class that have the current name
4077 // 'i->first', let's try to poke at the fully defined class
4078 // that is defined in the same translation unit as the
4079 // declaration.
4080 //
4081 // If we find one class (defined in the TU of the declaration)
4082 // that defines the declaration, then the declaration can be
4083 // resolved to that class.
4084 //
4085 // If no defining class is found in the TU of the declaration,
4086 // then there are possibly three cases to consider:
4087 //
4088 // 1/ There is exactly one class that defines the
4089 // declaration and that class is defined in another TU. In
4090 // this case, the declaration is resolved to that
4091 // definition.
4092 //
4093 // 2/ There are more than one class that define that
4094 // declaration and none of them is defined in the TU of the
4095 // declaration. If those classes are all different, then
4096 // the declaration is left unresolved.
4097 //
4098 // 3/ No class defines the declaration. In this case, the
4099 // declaration is left unresoved.
4100
4101 // So get the classes that might define the current
4102 // declarations which name is i->first.
4103 const type_base_wptrs_type *classes =
4104 lookup_class_types(i->first, *corpus());
4105 if (!classes)
4106 classes = lookup_union_types(i->first, *corpus());
4107
4108 if (!classes)
4109 continue;
4110
4111 // This is a map that associates the translation unit path to
4112 // the class (that potentially defines the declarations that
4113 // we consider) that are defined in that translation unit. It
4114 // should stay ordered by using the TU path as key to ensure
4115 // stability of the order of classe definitions in ABIXML
4116 // output.
4117 map<string, class_or_union_sptr> per_tu_class_map;
4118 for (type_base_wptrs_type::const_iterator c = classes->begin();
4119 c != classes->end();
4120 ++c)
4121 {
4122 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4123 ABG_ASSERT(klass);
4124
4125 klass = is_class_or_union_type(look_through_decl_only_class(klass));
4126 if (klass->get_is_declaration_only())
4127 continue;
4128
4129 string tu_path = klass->get_translation_unit()->get_absolute_path();
4130 if (tu_path.empty())
4131 continue;
4132
4133 // Build a map that associates the translation unit path
4134 // to the class (that potentially defines the declarations
4135 // that we consider) that are defined in that translation unit.
4136 per_tu_class_map[tu_path] = klass;
4137 }
4138
4139 if (!per_tu_class_map.empty())
4140 {
4141 // Walk the declarations to resolve and resolve them
4142 // either to the definitions that are in the same TU as
4143 // the declaration, or to the definition found elsewhere,
4144 // if there is only one such definition.
4145 for (classes_or_unions_type::iterator j = i->second.begin();
4146 j != i->second.end();
4147 ++j)
4148 {
4149 if ((*j)->get_is_declaration_only()
4150 && ((*j)->get_definition_of_declaration() == 0))
4151 {
4152 string tu_path =
4153 (*j)->get_translation_unit()->get_absolute_path();
4154 map<string, class_or_union_sptr>::const_iterator e =
4155 per_tu_class_map.find(tu_path);
4156 if (e != per_tu_class_map.end())
4157 (*j)->set_definition_of_declaration(e->second);
4158 else if (per_tu_class_map.size() == 1)
4159 (*j)->set_definition_of_declaration
4160 (per_tu_class_map.begin()->second);
4161 else
4162 {
4163 // We are in case where there are more than
4164 // one definition for the declaration. Let's
4165 // see if they are all equal. If they are,
4166 // then the declaration resolves to the
4167 // definition. Otherwise, we are in the case
4168 // 3/ described above.
4169 map<string,
4170 class_or_union_sptr>::const_iterator it;
4171 class_or_union_sptr first_class =
4172 per_tu_class_map.begin()->second;
4173 bool all_class_definitions_are_equal = true;
4174 for (it = per_tu_class_map.begin();
4175 it != per_tu_class_map.end();
4176 ++it)
4177 {
4178 if (it == per_tu_class_map.begin())
4179 continue;
4180 else
4181 {
4182 if (!compare_before_canonicalisation(it->second,
4183 first_class))
4184 {
4185 all_class_definitions_are_equal = false;
4186 break;
4187 }
4188 }
4189 }
4190 if (all_class_definitions_are_equal)
4191 (*j)->set_definition_of_declaration(first_class);
4192 }
4193 }
4194 }
4195 resolved_classes.push_back(i->first);
4196 }
4197 }
4198
4199 size_t num_decl_only_classes = declaration_only_classes().size(),
4200 num_resolved = resolved_classes.size();
4201 if (show_stats())
4202 cerr << "resolved " << num_resolved
4203 << " class declarations out of "
4204 << num_decl_only_classes
4205 << "\n";
4206
4207 for (vector<string>::const_iterator i = resolved_classes.begin();
4208 i != resolved_classes.end();
4209 ++i)
4210 declaration_only_classes().erase(*i);
4211
4212 if (show_stats() && !declaration_only_classes().empty())
4213 {
4214 cerr << "Here are the "
4215 << num_decl_only_classes - num_resolved
4216 << " unresolved class declarations:\n";
4217 for (string_classes_or_unions_map::iterator i =
4218 declaration_only_classes().begin();
4219 i != declaration_only_classes().end();
4220 ++i)
4221 cerr << " " << i->first << "\n";
4222 }
4223 }
4224
4225 /// Getter for the map of declaration-only enums that are to be
4226 /// resolved to their definition enums by the end of the corpus
4227 /// loading.
4228 ///
4229 /// @return a map of string -> vector of enums where the key is
4230 /// the fully qualified name of the enum and the value is the
4231 /// vector of declaration-only enum.
4232 const string_enums_map&
declaration_only_enums() const4233 declaration_only_enums() const
4234 {return decl_only_enums_map_;}
4235
4236 /// Getter for the map of declaration-only enums that are to be
4237 /// resolved to their definition enums by the end of the corpus
4238 /// loading.
4239 ///
4240 /// @return a map of string -> vector of enums where the key is
4241 /// the fully qualified name of the enum and the value is the
4242 /// vector of declaration-only enum.
4243 string_enums_map&
declaration_only_enums()4244 declaration_only_enums()
4245 {return decl_only_enums_map_;}
4246
4247 /// If a given enum is a declaration-only enum then stash it on
4248 /// the side so that at the end of the corpus reading we can resolve
4249 /// it to its definition.
4250 ///
4251 /// @param enom the enum to consider.
4252 void
maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr & enom)4253 maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr& enom)
4254 {
4255 if (enom->get_is_declaration_only()
4256 && enom->get_definition_of_declaration() == 0)
4257 {
4258 string qn = enom->get_qualified_name();
4259 string_enums_map::iterator record =
4260 declaration_only_enums().find(qn);
4261 if (record == declaration_only_enums().end())
4262 declaration_only_enums()[qn].push_back(enom);
4263 else
4264 record->second.push_back(enom);
4265 }
4266 }
4267
4268 /// Test if a given declaration-only enum has been scheduled for
4269 /// resolution to a defined enum.
4270 ///
4271 /// @param enom the enum to consider for the test.
4272 ///
4273 /// @return true iff @p enom is a declaration-only enum and if
4274 /// it's been scheduled for resolution to a defined enum.
4275 bool
is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr & enom)4276 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4277 {
4278 if (enom->get_is_declaration_only())
4279 return (declaration_only_enums().find(enom->get_qualified_name())
4280 != declaration_only_enums().end());
4281
4282 return false;
4283 }
4284
4285 /// Walk the declaration-only enums that have been found during
4286 /// the building of the corpus and resolve them to their definitions.
4287 ///
4288 /// TODO: Do away with this function by factorizing it with
4289 /// resolve_declaration_only_classes. All declaration-only decls
4290 /// could be handled the same way as declaration-only-ness is a
4291 /// property of abigail::ir::decl_base now.
4292 void
resolve_declaration_only_enums()4293 resolve_declaration_only_enums()
4294 {
4295 vector<string> resolved_enums;
4296
4297 for (string_enums_map::iterator i =
4298 declaration_only_enums().begin();
4299 i != declaration_only_enums().end();
4300 ++i)
4301 {
4302 bool to_resolve = false;
4303 for (enums_type::iterator j = i->second.begin();
4304 j != i->second.end();
4305 ++j)
4306 if ((*j)->get_is_declaration_only()
4307 && ((*j)->get_definition_of_declaration() == 0))
4308 to_resolve = true;
4309
4310 if (!to_resolve)
4311 {
4312 resolved_enums.push_back(i->first);
4313 continue;
4314 }
4315
4316 // Now, for each decl-only enum that have the current name
4317 // 'i->first', let's try to poke at the fully defined enum
4318 // that is defined in the same translation unit as the
4319 // declaration.
4320 //
4321 // If we find one enum (defined in the TU of the declaration)
4322 // that defines the declaration, then the declaration can be
4323 // resolved to that enum.
4324 //
4325 // If no defining enum is found in the TU of the declaration,
4326 // then there are possibly three cases to consider:
4327 //
4328 // 1/ There is exactly one enum that defines the
4329 // declaration and that enum is defined in another TU. In
4330 // this case, the declaration is resolved to that
4331 // definition.
4332 //
4333 // 2/ There are more than one enum that define that
4334 // declaration and none of them is defined in the TU of the
4335 // declaration. In this case, the declaration is left
4336 // unresolved.
4337 //
4338 // 3/ No enum defines the declaration. In this case, the
4339 // declaration is left unresoved.
4340
4341 // So get the enums that might define the current
4342 // declarations which name is i->first.
4343 const type_base_wptrs_type *enums =
4344 lookup_enum_types(i->first, *corpus());
4345 if (!enums)
4346 continue;
4347
4348 // This is a map that associates the translation unit path to
4349 // the enum (that potentially defines the declarations that
4350 // we consider) that are defined in that translation unit. It
4351 // should stay ordered by using the TU path as key to ensure
4352 // stability of the order of enum definitions in ABIXML
4353 // output.
4354 map<string, enum_type_decl_sptr> per_tu_enum_map;
4355 for (type_base_wptrs_type::const_iterator c = enums->begin();
4356 c != enums->end();
4357 ++c)
4358 {
4359 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4360 ABG_ASSERT(enom);
4361
4362 enom = is_enum_type(look_through_decl_only_enum(enom));
4363 if (enom->get_is_declaration_only())
4364 continue;
4365
4366 string tu_path = enom->get_translation_unit()->get_absolute_path();
4367 if (tu_path.empty())
4368 continue;
4369
4370 // Build a map that associates the translation unit path
4371 // to the enum (that potentially defines the declarations
4372 // that we consider) that are defined in that translation unit.
4373 per_tu_enum_map[tu_path] = enom;
4374 }
4375
4376 if (!per_tu_enum_map.empty())
4377 {
4378 // Walk the declarations to resolve and resolve them
4379 // either to the definitions that are in the same TU as
4380 // the declaration, or to the definition found elsewhere,
4381 // if there is only one such definition.
4382 for (enums_type::iterator j = i->second.begin();
4383 j != i->second.end();
4384 ++j)
4385 {
4386 if ((*j)->get_is_declaration_only()
4387 && ((*j)->get_definition_of_declaration() == 0))
4388 {
4389 string tu_path =
4390 (*j)->get_translation_unit()->get_absolute_path();
4391 map<string, enum_type_decl_sptr>::const_iterator e =
4392 per_tu_enum_map.find(tu_path);
4393 if (e != per_tu_enum_map.end())
4394 (*j)->set_definition_of_declaration(e->second);
4395 else if (per_tu_enum_map.size() == 1)
4396 (*j)->set_definition_of_declaration
4397 (per_tu_enum_map.begin()->second);
4398 else
4399 {
4400 // We are in case where there are more than
4401 // one definition for the declaration. Let's
4402 // see if they are all equal. If they are,
4403 // then the declaration resolves to the
4404 // definition. Otherwise, we are in the case
4405 // 3/ described above.
4406 map<string,
4407 enum_type_decl_sptr>::const_iterator it;
4408 enum_type_decl_sptr first_enum =
4409 per_tu_enum_map.begin()->second;
4410 bool all_enum_definitions_are_equal = true;
4411 for (it = per_tu_enum_map.begin();
4412 it != per_tu_enum_map.end();
4413 ++it)
4414 {
4415 if (it == per_tu_enum_map.begin())
4416 continue;
4417 else
4418 {
4419 if (!compare_before_canonicalisation(it->second,
4420 first_enum))
4421 {
4422 all_enum_definitions_are_equal = false;
4423 break;
4424 }
4425 }
4426 }
4427 if (all_enum_definitions_are_equal)
4428 (*j)->set_definition_of_declaration(first_enum);
4429 }
4430 }
4431 }
4432 resolved_enums.push_back(i->first);
4433 }
4434 }
4435
4436 size_t num_decl_only_enums = declaration_only_enums().size(),
4437 num_resolved = resolved_enums.size();
4438 if (show_stats())
4439 cerr << "resolved " << num_resolved
4440 << " enum declarations out of "
4441 << num_decl_only_enums
4442 << "\n";
4443
4444 for (vector<string>::const_iterator i = resolved_enums.begin();
4445 i != resolved_enums.end();
4446 ++i)
4447 declaration_only_enums().erase(*i);
4448
4449 if (show_stats() && !declaration_only_enums().empty())
4450 {
4451 cerr << "Here are the "
4452 << num_decl_only_enums - num_resolved
4453 << " unresolved enum declarations:\n";
4454 for (string_enums_map::iterator i = declaration_only_enums().begin();
4455 i != declaration_only_enums().end();
4456 ++i)
4457 cerr << " " << i->first << "\n";
4458 }
4459 }
4460
4461 /// Test if a symbol belongs to a function of the current ABI
4462 /// corpus.
4463 ///
4464 /// This is a sub-routine of fixup_functions_with_no_symbols.
4465 ///
4466 /// @param fn the function symbol to consider.
4467 ///
4468 /// @returnt true if @p fn belongs to a function of the current ABI
4469 /// corpus.
4470 bool
symbol_already_belongs_to_a_function(elf_symbol_sptr & fn)4471 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4472 {
4473 corpus_sptr corp = corpus();
4474 if (!corp)
4475 return false;
4476
4477 string id = fn->get_id_string();
4478
4479 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4480 if (!fns)
4481 return false;
4482
4483 for (auto f : *fns)
4484 if (f->get_symbol())
4485 return true;
4486
4487 return false;
4488 }
4489
4490 /// Some functions described by DWARF may have their linkage name
4491 /// set, but no link to their actual underlying elf symbol. When
4492 /// these are virtual member functions, comparing the enclosing type
4493 /// against another one which has its underlying symbol properly set
4494 /// might lead to spurious type changes.
4495 ///
4496 /// If the corpus contains a symbol with the same name as the
4497 /// linkage name of the function, then set up the link between the
4498 /// function and its underlying symbol.
4499 ///
4500 /// Note that for the moment, only virtual member functions are
4501 /// fixed up like this. This is because they really are the only
4502 /// fuctions of functions that can affect types (in spurious ways).
4503 void
fixup_functions_with_no_symbols()4504 fixup_functions_with_no_symbols()
4505 {
4506 corpus_sptr corp = corpus();
4507 if (!corp)
4508 return;
4509
4510 die_function_decl_map_type &fns_with_no_symbol =
4511 die_function_decl_with_no_symbol_map();
4512
4513 if (do_log())
4514 cerr << fns_with_no_symbol.size()
4515 << " functions to fixup, potentially\n";
4516
4517 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4518 i != fns_with_no_symbol.end();
4519 ++i)
4520 if (elf_symbol_sptr sym =
4521 corp->lookup_function_symbol(i->second->get_linkage_name()))
4522 {
4523 // So i->second is a virtual member function that was
4524 // previously scheduled to be set a function symbol.
4525 //
4526 // But if it appears that it now has a symbol already set,
4527 // then do not set a symbol to it again.
4528 //
4529 // Or if it appears that another virtual member function
4530 // from the current ABI Corpus, with the same linkage
4531 // (mangled) name has already been set a symbol, then do not
4532 // set a symbol to this function either. Otherwise, there
4533 // will be two virtual member functions with the same symbol
4534 // in the class and that leads to spurious hard-to-debug
4535 // change reports later down the road.
4536 if (i->second->get_symbol()
4537 || symbol_already_belongs_to_a_function(sym))
4538 continue;
4539
4540 ABG_ASSERT(is_member_function(i->second));
4541 ABG_ASSERT(get_member_function_is_virtual(i->second));
4542 i->second->set_symbol(sym);
4543 // The function_decl now has an associated (public) ELF symbol so
4544 // it ought to be advertised as being public.
4545 i->second->set_is_in_public_symbol_table(true);
4546 // Add the function to the set of exported decls of the
4547 // current corpus.
4548 maybe_add_fn_to_exported_decls(i->second.get());
4549 if (do_log())
4550 cerr << "fixed up '"
4551 << i->second->get_pretty_representation()
4552 << "' with symbol '"
4553 << sym->get_id_string()
4554 << "'\n";
4555 }
4556
4557 fns_with_no_symbol.clear();
4558 }
4559
4560 /// Return a reference to the vector containing the types created
4561 /// during the binary analysis but that are not tied to a given
4562 /// DWARF DIE.
4563 ///
4564 /// @return reference to the vector containing the types created
4565 /// during the binary analysis but that are not tied to a given
4566 /// DWARF DIE.
4567 const vector<type_base_sptr>&
types_to_canonicalize() const4568 types_to_canonicalize() const
4569 {return types_to_canonicalize_;}
4570
4571 /// Clear the containers holding types to canonicalize.
4572 void
clear_types_to_canonicalize()4573 clear_types_to_canonicalize()
4574 {
4575 types_to_canonicalize_.clear();
4576 }
4577
4578 /// Types that were created but not tied to a particular DIE, must
4579 /// be scheduled for late canonicalization using this method.
4580 ///
4581 /// @param t the type to schedule for late canonicalization.
4582 void
schedule_type_for_late_canonicalization(const type_base_sptr & t)4583 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4584 {
4585 types_to_canonicalize_.push_back(t);
4586 }
4587
4588 /// Canonicalize types which DIE offsets are stored in vectors on
4589 /// the side. This is a sub-routine of
4590 /// reader::perform_late_type_canonicalizing().
4591 ///
4592 /// @param source where the DIE of the types to canonicalize are
4593 /// from.
4594 void
canonicalize_types_scheduled()4595 canonicalize_types_scheduled()
4596 {
4597 tools_utils::timer cn_timer;
4598 if (do_log())
4599 {
4600 cerr << "DWARF Reader is going to canonicalize types";
4601 corpus_sptr c = corpus();
4602 if (c)
4603 cerr << " of corpus " << corpus()->get_path() << "\n";
4604 cn_timer.start();
4605 }
4606
4607 if (!types_to_canonicalize().empty())
4608 canonicalize_types(types_to_canonicalize().begin(),
4609 types_to_canonicalize().end(),
4610 [](const vector<type_base_sptr>::const_iterator& i)
4611 {return *i;});
4612
4613 if (do_log())
4614 {
4615 cn_timer.stop();
4616 cerr << "finished canonicalizing types";
4617 corpus_sptr c = corpus();
4618 if (c)
4619 cerr << " of corpus " << corpus()->get_path();
4620 cerr << ": (" << cn_timer << ")\n";
4621 }
4622 }
4623
4624 /// Compute the number of canonicalized and missed types in the late
4625 /// canonicalization phase.
4626 ///
4627 /// @param source where the DIEs of the canonicalized types are
4628 /// from.
4629 ///
4630 /// @param canonicalized the number of types that got canonicalized
4631 /// is added to the value already present in this parameter.
4632 ///
4633 /// @param missed the number of types scheduled for late
4634 /// canonicalization and which couldn't be canonicalized (for a
4635 /// reason) is added to the value already present in this parameter.
4636 void
add_late_canonicalized_types_stats(size_t & canonicalized,size_t & missed) const4637 add_late_canonicalized_types_stats(size_t& canonicalized,
4638 size_t& missed) const
4639 {
4640 for (auto t : types_to_canonicalize())
4641 {
4642 if (t->get_canonical_type())
4643 ++canonicalized;
4644 else
4645 ++missed;
4646 }
4647 }
4648
4649 // Look at the types that need to be canonicalized after the
4650 // translation unit has been constructed and canonicalize them.
4651 void
perform_late_type_canonicalizing()4652 perform_late_type_canonicalizing()
4653 {
4654 canonicalize_types_scheduled();
4655
4656 if (show_stats())
4657 {
4658 size_t num_canonicalized = 0, num_missed = 0, total = 0;
4659 add_late_canonicalized_types_stats(num_canonicalized,
4660 num_missed);
4661 total = num_canonicalized + num_missed;
4662 cerr << "binary: "
4663 << elf_path()
4664 << "\n";
4665 cerr << " # late canonicalized types: "
4666 << num_canonicalized;
4667 if (total)
4668 cerr << " (" << num_canonicalized * 100 / total << "%)";
4669 cerr << "\n"
4670 << " # missed canonicalization opportunities: "
4671 << num_missed;
4672 if (total)
4673 cerr << " (" << num_missed * 100 / total << "%)";
4674 cerr << "\n";
4675 }
4676
4677 }
4678
4679 const die_tu_map_type&
die_tu_map() const4680 die_tu_map() const
4681 {return die_tu_map_;}
4682
4683 die_tu_map_type&
die_tu_map()4684 die_tu_map()
4685 {return die_tu_map_;}
4686
4687 /// Getter for the map that associates a translation unit DIE to the
4688 /// vector of imported unit points that it contains.
4689 ///
4690 /// @param source where the DIEs are from.
4691 ///
4692 /// @return the map.
4693 const tu_die_imported_unit_points_map_type&
tu_die_imported_unit_points_map(die_source source) const4694 tu_die_imported_unit_points_map(die_source source) const
4695 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4696
4697 /// Getter for the map that associates a translation unit DIE to the
4698 /// vector of imported unit points that it contains.
4699 ///
4700 /// @param source where the DIEs are from.
4701 ///
4702 /// @return the map.
4703 tu_die_imported_unit_points_map_type&
tu_die_imported_unit_points_map(die_source source)4704 tu_die_imported_unit_points_map(die_source source)
4705 {
4706 switch (source)
4707 {
4708 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4709 break;
4710 case ALT_DEBUG_INFO_DIE_SOURCE:
4711 return alt_tu_die_imported_unit_points_map_;
4712 case TYPE_UNIT_DIE_SOURCE:
4713 return type_units_tu_die_imported_unit_points_map_;
4714 case NO_DEBUG_INFO_DIE_SOURCE:
4715 case NUMBER_OF_DIE_SOURCES:
4716 // We cannot reach this point.
4717 ABG_ASSERT_NOT_REACHED;
4718 }
4719 return tu_die_imported_unit_points_map_;
4720 }
4721
4722 /// Reset the current corpus being constructed.
4723 ///
4724 /// This actually deletes the current corpus being constructed.
4725 void
reset_corpus()4726 reset_corpus()
4727 {corpus().reset();}
4728
4729 /// Get the map that associates each DIE to its parent DIE. This is
4730 /// for DIEs coming from the main debug info sections.
4731 ///
4732 /// @param source where the DIEs in the map come from.
4733 ///
4734 /// @return the DIE -> parent map.
4735 const offset_offset_map_type&
die_parent_map(die_source source) const4736 die_parent_map(die_source source) const
4737 {return const_cast<reader*>(this)->die_parent_map(source);}
4738
4739 /// Get the map that associates each DIE to its parent DIE. This is
4740 /// for DIEs coming from the main debug info sections.
4741 ///
4742 /// @param source where the DIEs in the map come from.
4743 ///
4744 /// @return the DIE -> parent map.
4745 offset_offset_map_type&
die_parent_map(die_source source)4746 die_parent_map(die_source source)
4747 {
4748 switch (source)
4749 {
4750 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4751 break;
4752 case ALT_DEBUG_INFO_DIE_SOURCE:
4753 return alternate_die_parent_map_;
4754 case TYPE_UNIT_DIE_SOURCE:
4755 return type_section_die_parent_map();
4756 case NO_DEBUG_INFO_DIE_SOURCE:
4757 case NUMBER_OF_DIE_SOURCES:
4758 ABG_ASSERT_NOT_REACHED;
4759 }
4760 return primary_die_parent_map_;
4761 }
4762
4763 const offset_offset_map_type&
type_section_die_parent_map() const4764 type_section_die_parent_map() const
4765 {return type_section_die_parent_map_;}
4766
4767 offset_offset_map_type&
type_section_die_parent_map()4768 type_section_die_parent_map()
4769 {return type_section_die_parent_map_;}
4770
4771 /// Getter of the current translation unit.
4772 ///
4773 /// @return the current translation unit being constructed.
4774 const translation_unit_sptr&
cur_transl_unit() const4775 cur_transl_unit() const
4776 {return cur_tu_;}
4777
4778 /// Getter of the current translation unit.
4779 ///
4780 /// @return the current translation unit being constructed.
4781 translation_unit_sptr&
cur_transl_unit()4782 cur_transl_unit()
4783 {return cur_tu_;}
4784
4785 /// Setter of the current translation unit.
4786 ///
4787 /// @param tu the current translation unit being constructed.
4788 void
cur_transl_unit(translation_unit_sptr tu)4789 cur_transl_unit(translation_unit_sptr tu)
4790 {
4791 if (tu)
4792 cur_tu_ = tu;
4793 }
4794
4795 /// Return the global scope of the current translation unit.
4796 ///
4797 /// @return the global scope of the current translation unit.
4798 const scope_decl_sptr&
global_scope() const4799 global_scope() const
4800 {return cur_transl_unit()->get_global_scope();}
4801
4802 /// Return a scope that is nil.
4803 ///
4804 /// @return a scope that is nil.
4805 const scope_decl_sptr&
nil_scope() const4806 nil_scope() const
4807 {return nil_scope_;}
4808
4809 const scope_stack_type&
scope_stack() const4810 scope_stack() const
4811 {return scope_stack_;}
4812
4813 scope_stack_type&
scope_stack()4814 scope_stack()
4815 {return scope_stack_;}
4816
4817 scope_decl*
current_scope()4818 current_scope()
4819 {
4820 if (scope_stack().empty())
4821 {
4822 if (cur_transl_unit())
4823 scope_stack().push(cur_transl_unit()->get_global_scope().get());
4824 }
4825 return scope_stack().top();
4826 }
4827
4828 list<var_decl_sptr>&
var_decls_to_re_add_to_tree()4829 var_decls_to_re_add_to_tree()
4830 {return var_decls_to_add_;}
4831
4832 /// Test if a DIE represents a decl (function or variable) that has
4833 /// a symbol that is exported, whatever that means. This is
4834 /// supposed to work for Linux Kernel binaries as well.
4835 ///
4836 /// This is useful to limit the amount of DIEs taken into account to
4837 /// the strict limit of what an ABI actually means. Limiting the
4838 /// volume of DIEs analyzed this way is an important optimization to
4839 /// keep big binaries "manageable" by libabigail.
4840 ///
4841 /// @param DIE the die to consider.
4842 bool
is_decl_die_with_exported_symbol(const Dwarf_Die * die)4843 is_decl_die_with_exported_symbol(const Dwarf_Die *die)
4844 {
4845 if (!die || !die_is_decl(die))
4846 return false;
4847
4848 bool result = false, address_found = false, symbol_is_exported = false;;
4849 Dwarf_Addr decl_symbol_address = 0;
4850
4851 if (die_is_variable_decl(die))
4852 {
4853 if ((address_found = get_variable_address(die, decl_symbol_address)))
4854 symbol_is_exported =
4855 !!variable_symbol_is_exported(decl_symbol_address);
4856 }
4857 else if (die_is_function_decl(die))
4858 {
4859 if ((address_found = get_function_address(die, decl_symbol_address)))
4860 symbol_is_exported =
4861 !!function_symbol_is_exported(decl_symbol_address);
4862 }
4863
4864 if (address_found)
4865 result = symbol_is_exported;
4866
4867 return result;
4868 }
4869
4870 /// This is a sub-routine of maybe_adjust_fn_sym_address and
4871 /// maybe_adjust_var_sym_address.
4872 ///
4873 /// Given an address that we got by looking at some debug
4874 /// information (e.g, a symbol's address referred to by a DWARF
4875 /// TAG), If the ELF file we are interested in is a shared library
4876 /// or an executable, then adjust the address to be coherent with
4877 /// where the executable (or shared library) is loaded. That way,
4878 /// the address can be used to look for symbols in the executable or
4879 /// shared library.
4880 ///
4881 /// @return the adjusted address, or the same address as @p addr if
4882 /// it didn't need any adjustment.
4883 Dwarf_Addr
maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const4884 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4885 {
4886 if (addr == 0)
4887 return addr;
4888
4889 GElf_Ehdr eh_mem;
4890 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4891
4892 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4893 {
4894 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4895 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
4896 dwarf_elf_load_address));
4897 ABG_ASSERT(get_binary_load_address(elf_handle(),
4898 elf_load_address));
4899 if (dwarf_is_splitted()
4900 && (dwarf_elf_load_address != elf_load_address))
4901 // This means that in theory the DWARF and the executable are
4902 // not loaded at the same address. And addr is meaningful
4903 // only in the context of the DWARF.
4904 //
4905 // So let's transform addr into an offset relative to where
4906 // the DWARF is loaded, and let's add that relative offset
4907 // to the load address of the executable. That way, addr
4908 // becomes meaningful in the context of the executable and
4909 // can thus be used to compare against the address of
4910 // symbols of the executable, for instance.
4911 addr = addr - dwarf_elf_load_address + elf_load_address;
4912 }
4913
4914 return addr;
4915 }
4916
4917 /// For a relocatable (*.o) elf file, this function expects an
4918 /// absolute address, representing a function symbol. It then
4919 /// extracts the address of the .text section from the symbol
4920 /// absolute address to get the relative address of the function
4921 /// from the beginning of the .text section.
4922 ///
4923 /// For executable or shared library, this function expects an
4924 /// address of a function symbol that was retrieved by looking at a
4925 /// DWARF "file". The function thus adjusts the address to make it
4926 /// be meaningful in the context of the ELF file.
4927 ///
4928 /// In both cases, the address can then be compared against the
4929 /// st_value field of a function symbol from the ELF file.
4930 ///
4931 /// @param addr an adress for a function symbol that was retrieved
4932 /// from a DWARF file.
4933 ///
4934 /// @return the (possibly) adjusted address, or just @p addr if no
4935 /// adjustment took place.
4936 Dwarf_Addr
maybe_adjust_fn_sym_address(Dwarf_Addr addr) const4937 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4938 {
4939 if (addr == 0)
4940 return addr;
4941
4942 Elf* elf = elf_handle();
4943 GElf_Ehdr eh_mem;
4944 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4945
4946 if (elf_header->e_type == ET_REL)
4947 // We are looking at a relocatable file. In this case, we don't
4948 // do anything because:
4949 //
4950 // 1/ the addresses from DWARF are absolute (relative to the
4951 // beginning of the relocatable file)
4952 //
4953 // 2/ The ELF symbol addresses that we store in our lookup
4954 // tables are translated from section-related to absolute as
4955 // well. So we don't have anything to do at this point for
4956 // ET_REL files.
4957 ;
4958 else
4959 addr = maybe_adjust_address_for_exec_or_dyn(addr);
4960
4961 return addr;
4962 }
4963
4964 /// For a relocatable (*.o) elf file, this function expects an
4965 /// absolute address, representing a global variable symbol. It
4966 /// then extracts the address of the {.data,.data1,.rodata,.bss}
4967 /// section from the symbol absolute address to get the relative
4968 /// address of the variable from the beginning of the data section.
4969 ///
4970 /// For executable or shared library, this function expects an
4971 /// address of a variable symbol that was retrieved by looking at a
4972 /// DWARF "file". The function thus adjusts the address to make it
4973 /// be meaningful in the context of the ELF file.
4974 ///
4975 /// In both cases, the address can then be compared against the
4976 /// st_value field of a function symbol from the ELF file.
4977 ///
4978 /// @param addr an address for a global variable symbol that was
4979 /// retrieved from a DWARF file.
4980 ///
4981 /// @return the (possibly) adjusted address, or just @p addr if no
4982 /// adjustment took place.
4983 Dwarf_Addr
maybe_adjust_var_sym_address(Dwarf_Addr addr) const4984 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
4985 {
4986 Elf* elf = elf_handle();
4987 GElf_Ehdr eh_mem;
4988 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4989
4990 if (elf_header->e_type == ET_REL)
4991 // We are looking at a relocatable file. In this case, we don't
4992 // do anything because:
4993 //
4994 // 1/ the addresses from DWARF are absolute (relative to the
4995 // beginning of the relocatable file)
4996 //
4997 // 2/ The ELF symbol addresses that we store in our lookup
4998 // tables are translated from section-related to absolute as
4999 // well. So we don't have anything to do at this point for
5000 // ET_REL files.
5001 ;
5002 else
5003 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5004
5005 return addr;
5006 }
5007
5008 /// Get the first exported function address in the set of addresses
5009 /// referred to by the DW_AT_ranges attribute of a given DIE.
5010 ///
5011 /// @param die the DIE we are considering.
5012 ///
5013 /// @param address output parameter. This is set to the first
5014 /// address found in the sequence pointed to by the DW_AT_ranges
5015 /// attribute found on the DIE @p die, iff the function returns
5016 /// true. Otherwise, no value is set into this output parameter.
5017 ///
5018 /// @return true iff the DIE @p die does have a DW_AT_ranges
5019 /// attribute and an address of an exported function was found in
5020 /// its sequence value.
5021 bool
get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die * die,Dwarf_Addr & address) const5022 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5023 Dwarf_Addr& address) const
5024 {
5025 Dwarf_Addr base;
5026 Dwarf_Addr end_addr;
5027 ptrdiff_t offset = 0;
5028
5029 do
5030 {
5031 Dwarf_Addr addr = 0, fn_addr = 0;
5032 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5033 {
5034 fn_addr = maybe_adjust_fn_sym_address(addr);
5035 if (function_symbol_is_exported(fn_addr))
5036 {
5037 address = fn_addr;
5038 return true;
5039 }
5040 }
5041 } while (offset > 0);
5042 return false;
5043 }
5044
5045 /// Get the address of the function.
5046 ///
5047 /// The address of the function is considered to be the value of the
5048 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5049 /// only) to not point to an absolute address anymore, but rather to
5050 /// the address of the function inside the .text segment.
5051 ///
5052 /// @param function_die the die of the function to consider.
5053 ///
5054 /// @param address the resulting address iff the function returns
5055 /// true.
5056 ///
5057 /// @return true if the function address was found.
5058 bool
get_function_address(const Dwarf_Die * function_die,Dwarf_Addr & address) const5059 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5060 {
5061 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5062 DW_AT_low_pc, address))
5063 // So no DW_AT_low_pc was found. Let's see if the function DIE
5064 // has got a DW_AT_ranges attribute instead. If it does, the
5065 // first address of the set of addresses represented by the
5066 // value of that DW_AT_ranges represents the function (symbol)
5067 // address we are looking for.
5068 if (!get_first_exported_fn_address_from_DW_AT_ranges
5069 (const_cast<Dwarf_Die*>(function_die),
5070 address))
5071 return false;
5072
5073 address = maybe_adjust_fn_sym_address(address);
5074 return true;
5075 }
5076
5077 /// Get the address of the global variable.
5078 ///
5079 /// The address of the global variable is considered to be the value
5080 /// of the DW_AT_location attribute, possibly adjusted (in
5081 /// relocatable files only) to not point to an absolute address
5082 /// anymore, but rather to the address of the global variable inside
5083 /// the data segment.
5084 ///
5085 /// @param variable_die the die of the function to consider.
5086 ///
5087 /// @param address the resulting address iff this function returns
5088 /// true.
5089 ///
5090 /// @return true if the variable address was found.
5091 bool
get_variable_address(const Dwarf_Die * variable_die,Dwarf_Addr & address) const5092 get_variable_address(const Dwarf_Die* variable_die,
5093 Dwarf_Addr& address) const
5094 {
5095 bool is_tls_address = false;
5096 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5097 address, is_tls_address))
5098 return false;
5099 if (!is_tls_address)
5100 address = maybe_adjust_var_sym_address(address);
5101 return true;
5102 }
5103
5104 /// Getter of the exported decls builder object.
5105 ///
5106 /// @return the exported decls builder.
5107 corpus::exported_decls_builder*
exported_decls_builder()5108 exported_decls_builder()
5109 {return corpus()->get_exported_decls_builder().get();}
5110
5111 /// Getter of the "load_all_types" flag. This flag tells if all the
5112 /// types (including those not reachable by public declarations) are
5113 /// to be read and represented in the final ABI corpus.
5114 ///
5115 /// @return the load_all_types flag.
5116 bool
load_all_types() const5117 load_all_types() const
5118 {return options().load_all_types;}
5119
5120 /// Setter of the "load_all_types" flag. This flag tells if all the
5121 /// types (including those not reachable by public declarations) are
5122 /// to be read and represented in the final ABI corpus.
5123 ///
5124 /// @param f the new load_all_types flag.
5125 void
load_all_types(bool f)5126 load_all_types(bool f)
5127 {options().load_all_types = f;}
5128
5129 bool
load_in_linux_kernel_mode() const5130 load_in_linux_kernel_mode() const
5131 {return options().load_in_linux_kernel_mode;}
5132
5133 void
load_in_linux_kernel_mode(bool f)5134 load_in_linux_kernel_mode(bool f)
5135 {options().load_in_linux_kernel_mode = f;}
5136
5137 /// Test if it's allowed to assume that the DWARF debug info has
5138 /// been factorized (for instance, with the DWZ tool) so that if two
5139 /// type DIEs originating from the .gnu_debugaltlink section have
5140 /// different offsets, they represent different types.
5141 ///
5142 /// @return true iff we can assume that the DWARF debug info has
5143 /// been factorized.
5144 bool
leverage_dwarf_factorization() const5145 leverage_dwarf_factorization() const
5146 {
5147 if (!leverage_dwarf_factorization_.has_value())
5148 {
5149 if (options().leverage_dwarf_factorization
5150 && elf_helpers::find_section_by_name(elf_handle(),
5151 ".gnu_debugaltlink"))
5152 leverage_dwarf_factorization_ = true;
5153 else
5154 leverage_dwarf_factorization_ = false;
5155 }
5156 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5157
5158 return *leverage_dwarf_factorization_;
5159 }
5160 /// Getter of the "show_stats" flag.
5161 ///
5162 /// This flag tells if we should emit statistics about various
5163 /// internal stuff.
5164 ///
5165 /// @return the value of the flag.
5166 bool
show_stats() const5167 show_stats() const
5168 {return options().show_stats;}
5169
5170 /// Setter of the "show_stats" flag.
5171 ///
5172 /// This flag tells if we should emit statistics about various
5173 /// internal stuff.
5174 ///
5175 /// @param f the value of the flag.
5176 void
show_stats(bool f)5177 show_stats(bool f)
5178 {options().show_stats = f;}
5179
5180 /// Getter of the "do_log" flag.
5181 ///
5182 /// This flag tells if we should log about various internal
5183 /// details.
5184 ///
5185 /// return the "do_log" flag.
5186 bool
do_log() const5187 do_log() const
5188 {return options().do_log;}
5189
5190 /// Setter of the "do_log" flag.
5191 ///
5192 /// This flag tells if we should log about various internal details.
5193 ///
5194 /// @param f the new value of the flag.
5195 void
do_log(bool f)5196 do_log(bool f)
5197 {options().do_log = f;}
5198
5199 /// Walk the DIEs under a given die and for each child, populate the
5200 /// die -> parent map to record the child -> parent relationship
5201 /// that
5202 /// exists between the child and the given die.
5203 ///
5204 /// The function also builds the vector of places where units are
5205 /// imported.
5206 ///
5207 /// This is done recursively as for each child DIE, this function
5208 /// walks its children as well.
5209 ///
5210 /// @param die the DIE whose children to walk recursively.
5211 ///
5212 /// @param source where the DIE @p die comes from.
5213 ///
5214 /// @param imported_units a vector containing all the offsets of the
5215 /// points where unit have been imported, under @p die.
5216 void
build_die_parent_relations_under(Dwarf_Die * die,die_source source,imported_unit_points_type & imported_units)5217 build_die_parent_relations_under(Dwarf_Die* die,
5218 die_source source,
5219 imported_unit_points_type & imported_units)
5220 {
5221 if (!die)
5222 return;
5223
5224 offset_offset_map_type& parent_of = die_parent_map(source);
5225
5226 Dwarf_Die child;
5227 if (dwarf_child(die, &child) != 0)
5228 return;
5229
5230 do
5231 {
5232 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5233 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5234 {
5235 Dwarf_Die imported_unit;
5236 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5237 // If the imported_unit has a sub-tree, let's record
5238 // this point at which the sub-tree is imported into
5239 // the current debug info.
5240 //
5241 // Otherwise, if the imported_unit has no sub-tree,
5242 // there is no point in recording where a non-existent
5243 // sub-tree is being imported.
5244 //
5245 // Note that the imported_unit_points_type type below
5246 // expects the imported_unit to have a sub-tree.
5247 && die_has_children(&imported_unit))
5248 {
5249 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5250 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5251 imported_units.push_back
5252 (imported_unit_point(dwarf_dieoffset(&child),
5253 imported_unit,
5254 imported_unit_die_source));
5255 }
5256 }
5257 build_die_parent_relations_under(&child, source, imported_units);
5258 }
5259 while (dwarf_siblingof(&child, &child) == 0);
5260
5261 }
5262
5263 /// Determine if we do have to build a DIE -> parent map, depending
5264 /// on a given language.
5265 ///
5266 /// Some languages like C++, Ada etc, do have the concept of
5267 /// namespace and yet, the DIE data structure doesn't provide us
5268 /// with a way to get the parent namespace of a given DIE. So for
5269 /// those languages, we need to build a DIE -> parent map so that we
5270 /// can get the namespace DIE (or more generally the scope DIE) of a given
5271 /// DIE as we need it.
5272 ///
5273 /// But then some more basic languages like C or assembly don't have
5274 /// that need.
5275 ///
5276 /// This function, depending on the language, tells us if we need to
5277 /// build the DIE -> parent map or not.
5278 ///
5279 /// @param lang the language to consider.
5280 ///
5281 /// @return true iff we need to build the DIE -> parent map for this
5282 /// language.
5283 bool
do_we_build_die_parent_maps(translation_unit::language lang)5284 do_we_build_die_parent_maps(translation_unit::language lang)
5285 {
5286 if (is_c_language(lang))
5287 return false;
5288
5289 switch (lang)
5290 {
5291 case translation_unit::LANG_UNKNOWN:
5292 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5293 case translation_unit::LANG_Mips_Assembler:
5294 #endif
5295 return false;
5296 default:
5297 break;
5298 }
5299 return true;
5300 }
5301
5302 /// Walk all the DIEs accessible in the debug info (and in the
5303 /// alternate debug info as well) and build maps representing the
5304 /// relationship DIE -> parent. That is, make it so that we can get
5305 /// the parent for a given DIE.
5306 ///
5307 /// Note that the goal of this map is to be able to get the parent
5308 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5309 /// when we get a DIE of a type, and we want to build an internal
5310 /// representation for it, we need to get its fully qualified name.
5311 /// For that, we need to know what is the parent DIE of that type
5312 /// DIE, so that we can know what the namespace of that type is.
5313 ///
5314 /// Note that as the C language doesn't have namespaces (all types
5315 /// are defined in the same global namespace), this function doesn't
5316 /// build the DIE -> parent map if the current translation unit
5317 /// comes from C. This saves time on big C ELF files with a lot of
5318 /// DIEs.
5319 void
build_die_parent_maps()5320 build_die_parent_maps()
5321 {
5322 bool we_do_have_to_build_die_parent_map = false;
5323 uint8_t address_size = 0;
5324 size_t header_size = 0;
5325 // Get the DIE of the current translation unit, look at it to get
5326 // its language. If that language is in C, then all types are in
5327 // the global namespace so we don't need to build the DIE ->
5328 // parent map. So we dont build it in that case.
5329 for (Dwarf_Off offset = 0, next_offset = 0;
5330 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5331 offset, &next_offset, &header_size,
5332 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5333 offset = next_offset)
5334 {
5335 Dwarf_Off die_offset = offset + header_size;
5336 Dwarf_Die cu;
5337 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5338 die_offset, &cu))
5339 continue;
5340
5341 uint64_t l = 0;
5342 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5343 translation_unit::language lang = dwarf_language_to_tu_language(l);
5344 if (do_we_build_die_parent_maps(lang))
5345 we_do_have_to_build_die_parent_map = true;
5346 }
5347
5348 if (!we_do_have_to_build_die_parent_map)
5349 return;
5350
5351 // Build the DIE -> parent relation for DIEs coming from the
5352 // .debug_info section in the alternate debug info file.
5353 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5354 for (Dwarf_Off offset = 0, next_offset = 0;
5355 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5356 offset, &next_offset, &header_size,
5357 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5358 offset = next_offset)
5359 {
5360 Dwarf_Off die_offset = offset + header_size;
5361 Dwarf_Die cu;
5362 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5363 die_offset, &cu))
5364 continue;
5365 cur_tu_die(&cu);
5366
5367 imported_unit_points_type& imported_units =
5368 tu_die_imported_unit_points_map(source)[die_offset] =
5369 imported_unit_points_type();
5370 build_die_parent_relations_under(&cu, source, imported_units);
5371 }
5372
5373 // Build the DIE -> parent relation for DIEs coming from the
5374 // .debug_info section of the main debug info file.
5375 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5376 address_size = 0;
5377 header_size = 0;
5378 for (Dwarf_Off offset = 0, next_offset = 0;
5379 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5380 offset, &next_offset, &header_size,
5381 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5382 offset = next_offset)
5383 {
5384 Dwarf_Off die_offset = offset + header_size;
5385 Dwarf_Die cu;
5386 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5387 die_offset, &cu))
5388 continue;
5389 cur_tu_die(&cu);
5390 imported_unit_points_type& imported_units =
5391 tu_die_imported_unit_points_map(source)[die_offset] =
5392 imported_unit_points_type();
5393 build_die_parent_relations_under(&cu, source, imported_units);
5394 }
5395
5396 // Build the DIE -> parent relation for DIEs coming from the
5397 // .debug_types section.
5398 source = TYPE_UNIT_DIE_SOURCE;
5399 address_size = 0;
5400 header_size = 0;
5401 uint64_t type_signature = 0;
5402 Dwarf_Off type_offset;
5403 for (Dwarf_Off offset = 0, next_offset = 0;
5404 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5405 offset, &next_offset, &header_size,
5406 NULL, NULL, &address_size, NULL,
5407 &type_signature, &type_offset) == 0);
5408 offset = next_offset)
5409 {
5410 Dwarf_Off die_offset = offset + header_size;
5411 Dwarf_Die cu;
5412
5413 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5414 die_offset, &cu))
5415 continue;
5416 cur_tu_die(&cu);
5417 imported_unit_points_type& imported_units =
5418 tu_die_imported_unit_points_map(source)[die_offset] =
5419 imported_unit_points_type();
5420 build_die_parent_relations_under(&cu, source, imported_units);
5421 }
5422 }
5423 };// end class reader.
5424
5425 /// The type of the aggregates being compared during a DIE comparison.
5426 ///
5427 /// This encapsulates the stack of aggregates being compared at any
5428 /// single point.
5429 ///
5430 /// This is useful to detect "comparison cycles" and thus avoid the
5431 /// resulting infinite loops.
5432 ///
5433 /// This is also useful for implementing a very important optimization
5434 /// that takes place during the canonicalization
5435 struct offset_pairs_stack_type
5436 {
5437 // The DWARF DWARF reader that is useful for so many things.
5438 const reader& rdr_;
5439 // The set of types that are being compared. This is to speed up
5440 // searches.
5441 offset_pair_set_type set_;
5442 // The stack of types that are being compared. The top of the
5443 // stack is the back of the vector.
5444 offset_pair_vector_type vect_;
5445 // A map that associates a redundant type pair to the vector of
5446 // types that depends on it.
5447 offset_pair_vect_map_type redundant_types_;
5448 // A map that associates a dependant type to the vector of redundant
5449 // types it depends on.
5450 offset_pair_vect_map_type dependant_types_;
5451
offset_pairs_stack_typeabigail::dwarf::offset_pairs_stack_type5452 offset_pairs_stack_type(const reader& rdr)
5453 : rdr_ (rdr)
5454 {}
5455
5456 /// Add a pair of types being compared to the stack of aggregates
5457 /// being compared.
5458 ///
5459 /// @param p the pair of offsets of the type DIEs to consider.
5460 void
addabigail::dwarf::offset_pairs_stack_type5461 add(const offset_pair_type& p)
5462 {
5463 set_.insert(p);
5464 vect_.push_back(p);
5465 }
5466
5467 /// Erase a pair of types being compared from the stack of
5468 /// aggregates being compared.
5469 ///
5470 /// @param p the pair of offsets of the type DIEs to consider.
5471 ///
5472 /// @return true iff @p was found and erased from the stack.
5473 bool
eraseabigail::dwarf::offset_pairs_stack_type5474 erase(const offset_pair_type& p)
5475 {
5476 if (set_.erase(p))
5477 {
5478 offset_pair_vector_type::iterator i;
5479
5480 for (i = vect_.begin();i < vect_.end(); ++i)
5481 if (*i == p)
5482 break;
5483
5484 if (i != vect_.end())
5485 vect_.erase(i);
5486
5487 return true;
5488 }
5489
5490 return false;
5491 }
5492
5493 /// Test if a pair of type DIEs is part of the stack of type DIEs
5494 /// being compared.
5495 ///
5496 /// @param p the pair of offsets of the type DIEs to consider.
5497 ///
5498 /// @return true iff @p was found in the stack of types being
5499 /// compared.
5500 bool
containsabigail::dwarf::offset_pairs_stack_type5501 contains(const offset_pair_type &p) const
5502 {
5503 if (set_.find(p) == set_.end())
5504 return false;
5505 return true;
5506 }
5507
5508 /// Get the set of comparison pair that depends on a given
5509 /// comparison pair.
5510 ///
5511 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5512 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5513 /// words, the pair T appears in the comparison stack BEFORE the
5514 /// pair P.
5515 ///
5516 /// So, this function returns the vector of comparison pairs that
5517 /// appear in the comparison stack AFTER a given comparison pair.
5518 ///
5519 /// @param p the comparison pair to consider.
5520 ///
5521 /// @param pairs out parameter. This is filled with the comparison
5522 /// pairs that depend on @p, iff the function returns true.
5523 ///
5524 /// @return true iff comparison pairs depending on @p have been
5525 /// found and collected in @pairs.
5526 bool
get_pairs_that_depend_onabigail::dwarf::offset_pairs_stack_type5527 get_pairs_that_depend_on(const offset_pair_type& p,
5528 offset_pair_vector_type& pairs) const
5529 {
5530 bool result = false;
5531 if (!contains(p))
5532 return result;
5533
5534 // First, get an iterator on the position of 'p'.
5535 offset_pair_vector_type::const_iterator i;
5536 for (i = vect_.begin(); i != vect_.end(); ++i)
5537 if (*i == p)
5538 break;
5539
5540 if (i == vect_.end())
5541 return result;
5542
5543 // Then, harvest all the comparison pairs that come after the
5544 // position of 'p'.
5545 for (++i; i != vect_.end(); ++i)
5546 {
5547 pairs.push_back(*i);
5548 result = true;
5549 }
5550
5551 return result;
5552 }
5553
5554 /// Record the fact that a set of comparison pairs depends on a
5555 /// given comparison pair.
5556 ///
5557 /// Set a map that associates each dependant comparison pair to the
5558 /// pair it depends on.
5559 ///
5560 /// @param p the comparison pair that the set depends on.
5561 ///
5562 /// @param dependant_types the set of types that depends on @p.
5563 void
record_dependant_typesabigail::dwarf::offset_pairs_stack_type5564 record_dependant_types(const offset_pair_type& p,
5565 const offset_pair_vector_type& dependant_types)
5566 {
5567 for (auto type_pair : dependant_types)
5568 dependant_types_[type_pair].push_back(p);
5569 }
5570
5571 /// Record a comparison pair as being redundant.
5572 ///
5573 ///
5574 /// @param p the comparison pair to record as redundant.
5575 void
record_redundant_type_die_pairabigail::dwarf::offset_pairs_stack_type5576 record_redundant_type_die_pair(const offset_pair_type& p)
5577 {
5578 offset_pair_vector_type dependant_types;
5579 get_pairs_that_depend_on(p, dependant_types);
5580
5581 // First, record the relationship "p -> [pairs that depend on p]".
5582 auto it = redundant_types_.find(p);
5583 if (it == redundant_types_.end())
5584 {
5585 auto entry = std::make_pair(p, dependant_types);
5586 redundant_types_.insert(entry);
5587 }
5588 else
5589 it->second.insert(it->second.end(),
5590 dependant_types.begin(),
5591 dependant_types.end());
5592
5593 // For each dependant type pair, record the association:
5594 // dependant_pair --> [vect of redundant types]
5595 record_dependant_types(p, dependant_types);
5596 }
5597
5598 /// Test if a given pair has been detected as redundant.
5599 ///
5600 /// @param p the pair of DIEs to consider.
5601 ///
5602 /// @return iff @p is redundant.
5603 bool
is_redundantabigail::dwarf::offset_pairs_stack_type5604 is_redundant(const offset_pair_type& p)
5605 {
5606 auto i = redundant_types_.find(p);
5607 if (i != redundant_types_.end())
5608 return true;
5609 return false;
5610 }
5611
5612 /// Test if a given pair is dependant on at least a redundant type.
5613 ///
5614 /// @param p the pair to consider.
5615 ///
5616 /// @return true iff @p depends on a redundant type.
5617 bool
depends_on_redundant_typesabigail::dwarf::offset_pairs_stack_type5618 depends_on_redundant_types(const offset_pair_type& p)
5619 {
5620 auto i = dependant_types_.find(p);
5621 if (i == dependant_types_.end())
5622 return false;
5623 return true;
5624 }
5625
5626 /// Remove a redundant pair from the system.
5627 ///
5628 /// This needs updating the system to also remove the dependant
5629 /// types that depend on the redundant pair (if they depend only on
5630 /// that redundant pair).
5631 ///
5632 /// @param p the pair to consider.
5633 ///
5634 /// @param erase_canonical_die_offset if true then erase the cached
5635 /// comparison results for the redundant pair and its dependant
5636 /// types.
5637 void
erase_redundant_type_pair_entryabigail::dwarf::offset_pairs_stack_type5638 erase_redundant_type_pair_entry(const offset_pair_type& p,
5639 bool erase_cached_results = false)
5640 {
5641 // First, update the dependant types that depend on the redundant
5642 // type pair
5643 auto redundant_type = redundant_types_.find(p);
5644 if (redundant_type != redundant_types_.end())
5645 {
5646 for (auto dependant_type : redundant_type->second)
5647 {
5648 // Each dependant_type depends on the redundant type 'p',
5649 // among others.
5650 auto dependant_types_it = dependant_types_.find(dependant_type);
5651 ABG_ASSERT(dependant_types_it != dependant_types_.end());
5652 // Erase the redundant type 'p' from the redundant types
5653 // that dependant_type depends on.
5654 {
5655 auto i = dependant_types_it->second.begin();
5656 for (; i!= dependant_types_it->second.end();++i)
5657 if (*i == p)
5658 break;
5659 if (i != dependant_types_it->second.end())
5660 dependant_types_it->second.erase(i);
5661 }
5662 // If the dependant type itself doesn't depend on ANY
5663 // redundant type anymore, then remove the depend type
5664 // from the map of the dependant types.
5665 if (dependant_types_it->second.empty())
5666 {
5667 if (erase_cached_results)
5668 rdr_.die_comparison_results_.erase(dependant_type);
5669 dependant_types_.erase(dependant_types_it);
5670 }
5671 }
5672 }
5673 if (erase_cached_results)
5674 rdr_.die_comparison_results_.erase(p);
5675 redundant_types_.erase(p);
5676 }
5677
5678 /// If a comparison pair has been detected as redundant, stop
5679 /// tracking it as well as its dependant pairs. That will
5680 /// essentially make it impossible to reset/cancel the canonical
5681 /// propagated types for those depdant pairs, but will also save
5682 /// ressources.
5683 ///
5684 /// @param p the comparison pair to consider.
5685 void
confirm_canonical_propagated_typeabigail::dwarf::offset_pairs_stack_type5686 confirm_canonical_propagated_type(const offset_pair_type& p)
5687 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5688
5689 /// Walk the types that depend on a comparison pair and cancel their
5690 /// canonical-propagate-type, that means remove their canonical
5691 /// types and mark them as not being canonically-propagated. Also,
5692 /// erase their cached comparison results that was likely set to
5693 /// COMPARISON_RESULT_UNKNOWN.
5694 ///
5695 /// @param p the pair to consider.
5696 void
cancel_canonical_propagated_typeabigail::dwarf::offset_pairs_stack_type5697 cancel_canonical_propagated_type(const offset_pair_type& p)
5698 {
5699 offset_pair_set_type dependant_types;
5700 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5701 for (auto dependant_type : dependant_types)
5702 {
5703 // If this dependant type was canonical-type-propagated then
5704 // erase that canonical type.
5705 if (rdr_.propagated_types_.find(dependant_type)
5706 != rdr_.propagated_types_.end())
5707 {
5708 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5709 dependant_type.first.source_,
5710 /*die_as_type=*/true);
5711 rdr_.propagated_types_.erase(dependant_type);
5712 rdr_.cancelled_propagation_count_++;
5713 }
5714 // Update the cached result. We know the comparison result
5715 // must now be different.
5716 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
5717 if (comp_result_it != rdr_.die_comparison_results_.end())
5718 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5719 }
5720
5721 // Update the cached result of the root type to cancel too.
5722 auto comp_result_it = rdr_.die_comparison_results_.find(p);
5723 if (comp_result_it != rdr_.die_comparison_results_.end())
5724 {
5725 // At this point, the result of p is either
5726 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
5727 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
5728 // Make sure it's the cached result is now
5729 // COMPARISON_RESULT_DIFFERENT.
5730 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
5731 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5732 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
5733 }
5734
5735 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5736 {
5737 rdr_.erase_canonical_die_offset(p.first.offset_,
5738 p.first.source_,
5739 /*die_as_type=*/true);
5740 rdr_.propagated_types_.erase(p);
5741 rdr_.cancelled_propagation_count_++;
5742 }
5743 }
5744
5745 /// Get the set of comparison pairs that depend on a given pair.
5746 ///
5747 /// @param p the pair to consider.
5748 ///
5749 /// @param result this is set to the pairs that depend on @p, iff
5750 /// the function returned true.
5751 ///
5752 /// @param transitive_closure if set to true, the transitive closure
5753 /// of the @result is set to it.
5754 ///
5755 /// @return true iff @result could be filled with the dependant
5756 /// types.
5757 bool
get_dependant_typesabigail::dwarf::offset_pairs_stack_type5758 get_dependant_types(const offset_pair_type& p,
5759 offset_pair_set_type& result,
5760 bool transitive_closure = false)
5761 {
5762 auto i = redundant_types_.find(p);
5763 if (i != redundant_types_.end())
5764 {
5765 for (auto dependant_type : i->second)
5766 if (result.find(dependant_type) == result.end())
5767 {
5768 result.insert(dependant_type);
5769 if (transitive_closure)
5770 get_dependant_types(p, result, /*transitive_closure=*/true);
5771 }
5772 return true;
5773 }
5774 return false;
5775 }
5776 }; // end struct offset_pairs_stack_type
5777
5778 static type_or_decl_base_sptr
5779 build_ir_node_from_die(reader& rdr,
5780 Dwarf_Die* die,
5781 scope_decl* scope,
5782 bool called_from_public_decl,
5783 size_t where_offset,
5784 bool is_declaration_only = true,
5785 bool is_required_decl_spec = false);
5786
5787 static type_or_decl_base_sptr
5788 build_ir_node_from_die(reader& rdr,
5789 Dwarf_Die* die,
5790 bool called_from_public_decl,
5791 size_t where_offset);
5792
5793 static decl_base_sptr
5794 build_ir_node_for_void_type(reader& rdr);
5795
5796 static type_or_decl_base_sptr
5797 build_ir_node_for_void_pointer_type(reader& rdr);
5798
5799 static class_decl_sptr
5800 add_or_update_class_type(reader& rdr,
5801 Dwarf_Die* die,
5802 scope_decl* scope,
5803 bool is_struct,
5804 class_decl_sptr klass,
5805 bool called_from_public_decl,
5806 size_t where_offset,
5807 bool is_declaration_only);
5808
5809 static union_decl_sptr
5810 add_or_update_union_type(reader& rdr,
5811 Dwarf_Die* die,
5812 scope_decl* scope,
5813 union_decl_sptr union_type,
5814 bool called_from_public_decl,
5815 size_t where_offset,
5816 bool is_declaration_only);
5817
5818 static decl_base_sptr
5819 build_ir_node_for_void_type(reader& rdr);
5820
5821 static decl_base_sptr
5822 build_ir_node_for_variadic_parameter_type(reader &rdr);
5823
5824 static function_decl_sptr
5825 build_function_decl(reader& rdr,
5826 Dwarf_Die* die,
5827 size_t where_offset,
5828 function_decl_sptr fn);
5829
5830 static bool
5831 function_is_suppressed(const reader& rdr,
5832 const scope_decl* scope,
5833 Dwarf_Die *function_die,
5834 bool is_declaration_only);
5835
5836 static function_decl_sptr
5837 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5838 scope_decl *scope,
5839 Dwarf_Die *die,
5840 size_t where_offset,
5841 bool is_declaration_only,
5842 function_decl_sptr f);
5843
5844 static var_decl_sptr
5845 build_var_decl(reader& rdr,
5846 Dwarf_Die *die,
5847 size_t where_offset,
5848 var_decl_sptr result = var_decl_sptr());
5849
5850 static var_decl_sptr
5851 build_or_get_var_decl_if_not_suppressed(reader& rdr,
5852 scope_decl *scope,
5853 Dwarf_Die *die,
5854 size_t where_offset,
5855 var_decl_sptr res = var_decl_sptr(),
5856 bool is_required_decl_spec = false);
5857 static bool
5858 variable_is_suppressed(const reader& rdr,
5859 const scope_decl* scope,
5860 Dwarf_Die *variable_die,
5861 bool is_required_decl_spec = false);
5862
5863 static void
5864 finish_member_function_reading(Dwarf_Die* die,
5865 const function_decl_sptr& f,
5866 const class_or_union_sptr klass,
5867 reader& rdr);
5868
5869 /// Test if a given DIE is anonymous
5870 ///
5871 /// @param die the DIE to consider.
5872 ///
5873 /// @return true iff @p die is anonymous.
5874 static bool
die_is_anonymous(const Dwarf_Die * die)5875 die_is_anonymous(const Dwarf_Die* die)
5876 {
5877 Dwarf_Attribute attr;
5878 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5879 return true;
5880 return false;
5881 }
5882
5883 /// Test if a DIE is an anonymous data member, aka, "unnamed field".
5884 ///
5885 /// Unnamed fields are specified at
5886 /// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
5887 ///
5888 /// @param die the DIE to consider.
5889 ///
5890 /// @return true iff @p die is an anonymous data member.
5891 static bool
die_is_anonymous_data_member(const Dwarf_Die * die)5892 die_is_anonymous_data_member(const Dwarf_Die* die)
5893 {
5894 if (!die
5895 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
5896 || !die_name(die).empty())
5897 return false;
5898
5899 Dwarf_Die type_die;
5900 if (!die_die_attribute(die, DW_AT_type, type_die))
5901 return false;
5902
5903 if (dwarf_tag(&type_die) != DW_TAG_structure_type
5904 && dwarf_tag(&type_die) != DW_TAG_union_type)
5905 return false;
5906
5907 return true;
5908 }
5909
5910 /// Get the value of an attribute that is supposed to be a string, or
5911 /// an empty string if the attribute could not be found.
5912 ///
5913 /// @param die the DIE to get the attribute value from.
5914 ///
5915 /// @param attr_name the attribute name. Must come from dwarf.h and
5916 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5917 ///
5918 /// @return the string representing the value of the attribute, or an
5919 /// empty string if no string attribute could be found.
5920 static string
die_string_attribute(const Dwarf_Die * die,unsigned attr_name)5921 die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5922 {
5923 if (!die)
5924 return "";
5925
5926 Dwarf_Attribute attr;
5927 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5928 return "";
5929
5930 const char* str = dwarf_formstring(&attr);
5931 return str ? str : "";
5932 }
5933
5934 /// Get the value of an attribute that is supposed to be a string, or
5935 /// an empty string if the attribute could not be found.
5936 ///
5937 /// @param die the DIE to get the attribute value from.
5938 ///
5939 /// @param attr_name the attribute name. Must come from dwarf.h and
5940 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5941 ///
5942 /// @return the char* representing the value of the attribute, or an
5943 /// empty string if no string attribute could be found.
5944 static const char*
die_char_str_attribute(const Dwarf_Die * die,unsigned attr_name)5945 die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
5946 {
5947 if (!die)
5948 return nullptr;
5949
5950 Dwarf_Attribute attr;
5951 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5952 return nullptr;
5953
5954 const char* str = dwarf_formstring(&attr);
5955 return str;
5956 }
5957
5958 /// Get the value of an attribute that is supposed to be an unsigned
5959 /// constant.
5960 ///
5961 /// @param die the DIE to read the information from.
5962 ///
5963 /// @param attr_name the DW_AT_* name of the attribute. Must come
5964 /// from dwarf.h and be an enumerator representing an attribute like,
5965 /// e.g, DW_AT_decl_line.
5966 ///
5967 ///@param cst the output parameter that is set to the value of the
5968 /// attribute @p attr_name. This parameter is set iff the function
5969 /// return true.
5970 ///
5971 /// @return true if there was an attribute of the name @p attr_name
5972 /// and with a value that is a constant, false otherwise.
5973 static bool
die_unsigned_constant_attribute(const Dwarf_Die * die,unsigned attr_name,uint64_t & cst)5974 die_unsigned_constant_attribute(const Dwarf_Die* die,
5975 unsigned attr_name,
5976 uint64_t& cst)
5977 {
5978 if (!die)
5979 return false;
5980
5981 Dwarf_Attribute attr;
5982 Dwarf_Word result = 0;
5983 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5984 || dwarf_formudata(&attr, &result))
5985 return false;
5986
5987 cst = result;
5988 return true;
5989 }
5990
5991 /// Read a signed constant value from a given attribute.
5992 ///
5993 /// The signed constant expected must be of constant form.
5994 ///
5995 /// @param die the DIE to get the attribute from.
5996 ///
5997 /// @param attr_name the attribute name.
5998 ///
5999 /// @param cst the resulting signed constant read.
6000 ///
6001 /// @return true iff a signed constant attribute of the name @p
6002 /// attr_name was found on the DIE @p die.
6003 static bool
die_signed_constant_attribute(const Dwarf_Die * die,unsigned attr_name,int64_t & cst)6004 die_signed_constant_attribute(const Dwarf_Die *die,
6005 unsigned attr_name,
6006 int64_t& cst)
6007 {
6008 if (!die)
6009 return false;
6010
6011 Dwarf_Attribute attr;
6012 Dwarf_Sword result = 0;
6013 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6014 || dwarf_formsdata(&attr, &result))
6015 return false;
6016
6017 cst = result;
6018 return true;
6019 }
6020
6021 /// Read the value of a constant attribute that is either signed or
6022 /// unsigned into a array_type_def::subrange_type::bound_value value.
6023 ///
6024 /// The bound_value instance will capture the actual signedness of the
6025 /// read attribute.
6026 ///
6027 /// @param die the DIE from which to read the value of the attribute.
6028 ///
6029 /// @param attr_name the attribute name to consider.
6030 ///
6031 /// @param is_signed true if the attribute value has to read as
6032 /// signed.
6033 ///
6034 /// @param value the resulting value read from attribute @p attr_name
6035 /// on DIE @p die.
6036 ///
6037 /// @return true iff DIE @p die has an attribute named @p attr_name
6038 /// with a constant value.
6039 static bool
die_constant_attribute(const Dwarf_Die * die,unsigned attr_name,bool is_signed,array_type_def::subrange_type::bound_value & value)6040 die_constant_attribute(const Dwarf_Die *die,
6041 unsigned attr_name,
6042 bool is_signed,
6043 array_type_def::subrange_type::bound_value &value)
6044 {
6045 if (!is_signed)
6046 {
6047 uint64_t l = 0;
6048 if (!die_unsigned_constant_attribute(die, attr_name, l))
6049 return false;
6050 value.set_unsigned(l);
6051 }
6052 else
6053 {
6054 int64_t l = 0;
6055 if (!die_signed_constant_attribute(die, attr_name, l))
6056 return false;
6057 value.set_signed(l);
6058 }
6059 return true;
6060 }
6061
6062 /// Test if a given DWARF form is DW_FORM_strx{1,4}.
6063 ///
6064 /// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6065 /// enum in dwarf.h so we have to use an unsigned int for the form,
6066 /// grrr.
6067 ///
6068 /// @param form the form to consider.
6069 ///
6070 /// @return true iff @p form is DW_FORM_strx{1,4}.
6071 static bool
form_is_DW_FORM_strx(unsigned form)6072 form_is_DW_FORM_strx(unsigned form)
6073 {
6074 if (form)
6075 {
6076 #if defined HAVE_DW_FORM_strx1 \
6077 && defined HAVE_DW_FORM_strx2 \
6078 && defined HAVE_DW_FORM_strx3 \
6079 && defined HAVE_DW_FORM_strx4
6080 if (form == DW_FORM_strx1
6081 || form == DW_FORM_strx2
6082 || form == DW_FORM_strx3
6083 ||form == DW_FORM_strx4)
6084 return true;
6085 #endif
6086 }
6087 return false;
6088 }
6089
6090 /// Test if a given DWARF form is DW_FORM_line_strp.
6091 ///
6092 /// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6093 /// enum in dwarf.h so we have to use an unsigned int for the form,
6094 /// grrr.
6095 ///
6096 /// @param form the form to consider.
6097 ///
6098 /// @return true iff @p form is DW_FORM_line_strp.
6099 static bool
form_is_DW_FORM_line_strp(unsigned form)6100 form_is_DW_FORM_line_strp(unsigned form)
6101 {
6102 if (form)
6103 {
6104 #if defined HAVE_DW_FORM_line_strp
6105 if (form == DW_FORM_line_strp)
6106 return true;
6107 #endif
6108 }
6109 return false;
6110 }
6111
6112 /// Get the value of a DIE attribute; that value is meant to be a
6113 /// flag.
6114 ///
6115 /// @param die the DIE to get the attribute from.
6116 ///
6117 /// @param attr_name the DW_AT_* name of the attribute. Must come
6118 /// from dwarf.h and be an enumerator representing an attribute like,
6119 /// e.g, DW_AT_external.
6120 ///
6121 /// @param flag the output parameter to store the flag value into.
6122 /// This is set iff the function returns true.
6123 ///
6124 /// @param recursively if true, the function looks through the
6125 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6126 /// all the way down to the initial DIE that is cloned and look on
6127 /// that DIE to see if it has the @p attr_name attribute.
6128 ///
6129 /// @return true if the DIE has a flag attribute named @p attr_name,
6130 /// false otherwise.
6131 static bool
die_flag_attribute(const Dwarf_Die * die,unsigned attr_name,bool & flag,bool recursively=true)6132 die_flag_attribute(const Dwarf_Die* die,
6133 unsigned attr_name,
6134 bool& flag,
6135 bool recursively = true)
6136 {
6137 Dwarf_Attribute attr;
6138 if (recursively
6139 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6140 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6141 return false;
6142
6143 bool f = false;
6144 if (dwarf_formflag(&attr, &f))
6145 return false;
6146
6147 flag = f;
6148 return true;
6149 }
6150
6151 /// Get the mangled name from a given DIE.
6152 ///
6153 /// @param die the DIE to read the mangled name from.
6154 ///
6155 /// @return the mangled name if it's present in the DIE, or just an
6156 /// empty string if it's not.
6157 static string
die_linkage_name(const Dwarf_Die * die)6158 die_linkage_name(const Dwarf_Die* die)
6159 {
6160 if (!die)
6161 return "";
6162
6163 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6164 if (linkage_name.empty())
6165 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6166 return linkage_name;
6167 }
6168
6169 /// Get the file path that is the value of the DW_AT_decl_file
6170 /// attribute on a given DIE, if the DIE is a decl DIE having that
6171 /// attribute.
6172 ///
6173 /// @param die the DIE to consider.
6174 ///
6175 /// @return a string containing the file path that is the logical
6176 /// value of the DW_AT_decl_file attribute. If the DIE @p die
6177 /// doesn't have a DW_AT_decl_file attribute, then the return value is
6178 /// just an empty string.
6179 static string
die_decl_file_attribute(const Dwarf_Die * die)6180 die_decl_file_attribute(const Dwarf_Die* die)
6181 {
6182 if (!die)
6183 return "";
6184
6185 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6186
6187 return str ? str : "";
6188 }
6189
6190 /// Get the value of an attribute which value is supposed to be a
6191 /// reference to a DIE.
6192 ///
6193 /// @param die the DIE to read the value from.
6194 ///
6195 /// @param attr_name the DW_AT_* attribute name to read.
6196 ///
6197 /// @param result the DIE resulting from reading the attribute value.
6198 /// This is set iff the function returns true.
6199 ///
6200 /// @param recursively if true, the function looks through the
6201 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6202 /// all the way down to the initial DIE that is cloned and look on
6203 /// that DIE to see if it has the @p attr_name attribute.
6204 ///
6205 /// @return true if the DIE @p die contains an attribute named @p
6206 /// attr_name that is a DIE reference, false otherwise.
6207 static bool
die_die_attribute(const Dwarf_Die * die,unsigned attr_name,Dwarf_Die & result,bool recursively)6208 die_die_attribute(const Dwarf_Die* die,
6209 unsigned attr_name,
6210 Dwarf_Die& result,
6211 bool recursively)
6212 {
6213 Dwarf_Attribute attr;
6214 if (recursively
6215 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6216 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6217 return false;
6218
6219 return dwarf_formref_die(&attr, &result);
6220 }
6221
6222 /// Test if a subrange DIE indirectly references another subrange DIE
6223 /// through a given attribute.
6224 ///
6225 /// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6226 /// attribute be a reference to either a data member or a variable
6227 /// which type is itself a DW_TAG_subrange_type. This latter subrange
6228 /// DIE is said to be "indirectly referenced" by the former subrange
6229 /// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6230 /// the value we want for the DW_AT_upper_bound of the former.
6231 ///
6232 /// This function tests if the former subrange DIE does indirectly
6233 /// reference another subrange DIE through a given attribute (not
6234 /// necessarily DW_AT_upper_bound).
6235 ///
6236 /// @param die the DIE to consider. Note that It must be a
6237 /// DW_TAG_subrange_type.
6238 ///
6239 /// @param attr_name the name of the attribute to look through for the
6240 /// indirectly referenced subrange DIE.
6241 ///
6242 /// @param referenced_subrange if the function returns true, then the
6243 /// argument of this parameter is set to the indirectly referenced
6244 /// DW_TAG_subrange_type DIE.
6245 ///
6246 /// @return true iff @p DIE indirectly references a subrange DIE
6247 /// through the attribute @p attr_name.
6248 static bool
subrange_die_indirectly_references_subrange_die(const Dwarf_Die * die,unsigned attr_name,Dwarf_Die & referenced_subrange)6249 subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6250 unsigned attr_name,
6251 Dwarf_Die& referenced_subrange)
6252 {
6253 bool result = false;
6254
6255 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6256 return result;
6257
6258 Dwarf_Die referenced_die;
6259 if (die_die_attribute(die, attr_name, referenced_die))
6260 {
6261 unsigned tag = dwarf_tag(&referenced_die);
6262 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6263 {
6264 Dwarf_Die type_die;
6265 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6266 {
6267 tag = dwarf_tag(&type_die);
6268 if (tag == DW_TAG_subrange_type)
6269 {
6270 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6271 result = true;
6272 }
6273 }
6274 }
6275 }
6276 return result;
6277 }
6278
6279 /// Return the bound value of subrange die by looking at an indirectly
6280 /// referenced subrange DIE.
6281 ///
6282 /// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6283 /// attribute be a reference to either a data member or a variable
6284 /// which type is itself a DW_TAG_subrange_type. This latter subrange
6285 /// DIE is said to be "indirectly referenced" by the former subrange
6286 /// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6287 /// the value we want for the DW_AT_{lower,upper}_bound of the former.
6288 ///
6289 /// This function gets the DW_AT_{lower,upper}_bound value of a
6290 /// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6291 /// the indirectly referenced subrange type, if it exists.
6292 ///
6293 /// @param die the subrange DIE to consider.
6294 ///
6295 /// @param attr_name the name of the attribute to consider, typically,
6296 /// DW_AT_{lower,upper}_bound.
6297 ///
6298 /// @param v the found value, iff this function returned true.
6299 ///
6300 /// @param is_signed, this is set to true if @p v is signed. This
6301 /// parameter is set at all only if the function returns true.
6302 ///
6303 /// @return true iff the DW_AT_{lower,upper}_bound was found on the
6304 /// indirectly referenced subrange type.
6305 static bool
subrange_die_indirect_bound_value(const Dwarf_Die * die,unsigned attr_name,array_type_def::subrange_type::bound_value & v,bool & is_signed)6306 subrange_die_indirect_bound_value(const Dwarf_Die *die,
6307 unsigned attr_name,
6308 array_type_def::subrange_type::bound_value& v,
6309 bool& is_signed)
6310 {
6311 bool result = false;
6312
6313 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6314 return result;
6315
6316 Dwarf_Die subrange_die;
6317 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6318 subrange_die))
6319 {
6320 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6321 result = true;
6322 }
6323 return result;
6324 }
6325
6326 /// Read and return an addresss class attribute from a given DIE.
6327 ///
6328 /// @param die the DIE to consider.
6329 ///
6330 /// @param attr_name the name of the address class attribute to read
6331 /// the value from.
6332 ///
6333 /// @param the resulting address.
6334 ///
6335 /// @return true iff the attribute could be read, was of the expected
6336 /// address class and could thus be translated into the @p result.
6337 static bool
die_address_attribute(Dwarf_Die * die,unsigned attr_name,Dwarf_Addr & result)6338 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6339 {
6340 Dwarf_Attribute attr;
6341 if (!dwarf_attr_integrate(die, attr_name, &attr))
6342 return false;
6343 return dwarf_formaddr(&attr, &result) == 0;
6344 }
6345
6346 /// Returns the source location associated with a decl DIE.
6347 ///
6348 /// @param rdr the @ref reader to use.
6349 ///
6350 /// @param die the DIE the read the source location from.
6351 ///
6352 /// @return the location associated with @p die.
6353 static location
die_location(const reader & rdr,const Dwarf_Die * die)6354 die_location(const reader& rdr, const Dwarf_Die* die)
6355 {
6356 if (!die)
6357 return location();
6358
6359 string file = die_decl_file_attribute(die);
6360 uint64_t line = 0;
6361 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6362
6363 if (!file.empty() && line != 0)
6364 {
6365 translation_unit_sptr tu = rdr.cur_transl_unit();
6366 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6367 return l;
6368 }
6369 return location();
6370 }
6371
6372 /// Return a copy of the name of a DIE.
6373 ///
6374 /// @param die the DIE to consider.
6375 ///
6376 /// @return a copy of the name of the DIE.
6377 static string
die_name(const Dwarf_Die * die)6378 die_name(const Dwarf_Die* die)
6379 {
6380 string name = die_string_attribute(die, DW_AT_name);
6381 return name;
6382 }
6383
6384 /// Return the location, the name and the mangled name of a given DIE.
6385 ///
6386 /// @param rdr the DWARF reader to use.
6387 ///
6388 /// @param die the DIE to read location and names from.
6389 ///
6390 /// @param loc the location output parameter to set.
6391 ///
6392 /// @param name the name output parameter to set.
6393 ///
6394 /// @param linkage_name the linkage_name output parameter to set.
6395 static void
die_loc_and_name(const reader & rdr,Dwarf_Die * die,location & loc,string & name,string & linkage_name)6396 die_loc_and_name(const reader& rdr,
6397 Dwarf_Die* die,
6398 location& loc,
6399 string& name,
6400 string& linkage_name)
6401 {
6402 loc = die_location(rdr, die);
6403 name = die_name(die);
6404 linkage_name = die_linkage_name(die);
6405 }
6406
6407 /// Get the size of a (type) DIE as the value for the parameter
6408 /// DW_AT_byte_size or DW_AT_bit_size.
6409 ///
6410 /// @param die the DIE to read the information from.
6411 ///
6412 /// @param size the resulting size in bits. This is set iff the
6413 /// function return true.
6414 ///
6415 /// @return true if the size attribute was found.
6416 static bool
die_size_in_bits(const Dwarf_Die * die,uint64_t & size)6417 die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6418 {
6419 if (!die)
6420 return false;
6421
6422 uint64_t byte_size = 0, bit_size = 0;
6423
6424 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6425 {
6426 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6427 return false;
6428 }
6429 else
6430 bit_size = byte_size * 8;
6431
6432 size = bit_size;
6433
6434 return true;
6435 }
6436
6437 /// Get the access specifier (from the DW_AT_accessibility attribute
6438 /// value) of a given DIE.
6439 ///
6440 /// @param die the DIE to consider.
6441 ///
6442 /// @param access the resulting access. This is set iff the function
6443 /// returns true.
6444 ///
6445 /// @return bool if the DIE contains the DW_AT_accessibility die.
6446 static bool
die_access_specifier(Dwarf_Die * die,access_specifier & access)6447 die_access_specifier(Dwarf_Die * die, access_specifier& access)
6448 {
6449 if (!die)
6450 return false;
6451
6452 uint64_t a = 0;
6453 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6454 return false;
6455
6456 access_specifier result = private_access;
6457
6458 switch (a)
6459 {
6460 case private_access:
6461 result = private_access;
6462 break;
6463
6464 case protected_access:
6465 result = protected_access;
6466 break;
6467
6468 case public_access:
6469 result = public_access;
6470 break;
6471
6472 default:
6473 break;
6474 }
6475
6476 access = result;
6477 return true;
6478 }
6479
6480 /// Test whether a given DIE represents a decl that is public. That
6481 /// is, one with the DW_AT_external attribute set.
6482 ///
6483 /// @param die the DIE to consider for testing.
6484 ///
6485 /// @return true if a DW_AT_external attribute is present and its
6486 /// value is set to the true; return false otherwise.
6487 static bool
die_is_public_decl(const Dwarf_Die * die)6488 die_is_public_decl(const Dwarf_Die* die)
6489 {
6490 if (!die)
6491 return false;
6492 bool is_public = false;
6493
6494 // If this is a DW_TAG_subprogram DIE, look for the
6495 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6496 // then it's public. In all other cases, this should return false.
6497
6498 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6499 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6500 die_flag_attribute(die, DW_AT_external, is_public);
6501 else if (tag == DW_TAG_namespace)
6502 {
6503 string name = die_name(die);
6504 is_public = !name.empty();
6505 }
6506
6507 return is_public;
6508 }
6509
6510 /// Test if a DIE is effectively public.
6511 ///
6512 /// This is meant to return true when either the DIE is public or when
6513 /// it's a variable DIE that is at (global) namespace level.
6514 ///
6515 /// @return true iff either the DIE is public or is a variable DIE
6516 /// that is at (global) namespace level.
6517 static bool
die_is_effectively_public_decl(const reader & rdr,const Dwarf_Die * die)6518 die_is_effectively_public_decl(const reader& rdr,
6519 const Dwarf_Die* die)
6520 {
6521 if (die_is_public_decl(die))
6522 return true;
6523
6524 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6525 if (tag == DW_TAG_variable || tag == DW_TAG_member)
6526 {
6527 // The DIE is a variable.
6528 Dwarf_Die parent_die;
6529 size_t where_offset = 0;
6530 if (!get_parent_die(rdr, die, parent_die, where_offset))
6531 return false;
6532
6533 tag = dwarf_tag(&parent_die);
6534 if (tag == DW_TAG_compile_unit
6535 || tag == DW_TAG_partial_unit
6536 || tag == DW_TAG_type_unit)
6537 // The DIE is at global scope.
6538 return true;
6539
6540 if (tag == DW_TAG_namespace)
6541 {
6542 string name = die_name(&parent_die);
6543 if (name.empty())
6544 // The DIE at unnamed namespace scope, so it's not public.
6545 return false;
6546 // The DIE is at namespace scope.
6547 return true;
6548 }
6549 }
6550 return false;
6551 }
6552
6553 /// Test whether a given DIE represents a declaration-only DIE.
6554 ///
6555 /// That is, if the DIE has the DW_AT_declaration flag set.
6556 ///
6557 /// @param die the DIE to consider.
6558 //
6559 /// @return true if a DW_AT_declaration is present, false otherwise.
6560 static bool
die_is_declaration_only(Dwarf_Die * die)6561 die_is_declaration_only(Dwarf_Die* die)
6562 {
6563 bool is_declaration = false;
6564 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6565 if (is_declaration && !die_has_size_attribute(die))
6566 return true;
6567 return false;
6568 }
6569
6570 /// Test if a DIE is for a function decl.
6571 ///
6572 /// @param die the DIE to consider.
6573 ///
6574 /// @return true iff @p die represents a function decl.
6575 static bool
die_is_function_decl(const Dwarf_Die * die)6576 die_is_function_decl(const Dwarf_Die *die)
6577 {
6578 if (!die)
6579 return false;
6580
6581 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6582 if (tag == DW_TAG_subprogram)
6583 return true;
6584 return false;
6585 }
6586
6587 /// Test if a DIE is for a variable decl.
6588 ///
6589 /// @param die the DIE to consider.
6590 ///
6591 /// @return true iff @p die represents a variable decl.
6592 static bool
die_is_variable_decl(const Dwarf_Die * die)6593 die_is_variable_decl(const Dwarf_Die *die)
6594 {
6595 if (!die)
6596 return false;
6597
6598 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6599 if (tag == DW_TAG_variable)
6600 return true;
6601 return false;
6602 }
6603
6604 /// Test if a DIE has size attribute.
6605 ///
6606 /// @param die the DIE to consider.
6607 ///
6608 /// @return true if the DIE has a size attribute.
6609 static bool
die_has_size_attribute(const Dwarf_Die * die)6610 die_has_size_attribute(const Dwarf_Die *die)
6611 {
6612 uint64_t s;
6613 if (die_size_in_bits(die, s))
6614 return true;
6615 return false;
6616 }
6617
6618 /// Test that a DIE has no child DIE.
6619 ///
6620 /// @param die the DIE to consider.
6621 ///
6622 /// @return true iff @p die has no child DIE.
6623 static bool
die_has_no_child(const Dwarf_Die * die)6624 die_has_no_child(const Dwarf_Die *die)
6625 {
6626 if (!die)
6627 return true;
6628
6629 Dwarf_Die child;
6630 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6631 return false;
6632 return true;
6633 }
6634
6635 /// Test whether a given DIE represents a declaration-only DIE.
6636 ///
6637 /// That is, if the DIE has the DW_AT_declaration flag set.
6638 ///
6639 /// @param die the DIE to consider.
6640 //
6641 /// @return true if a DW_AT_declaration is present, false otherwise.
6642 static bool
die_is_declaration_only(const Dwarf_Die * die)6643 die_is_declaration_only(const Dwarf_Die* die)
6644 {return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6645
6646 /// Tests whether a given DIE is artificial.
6647 ///
6648 /// @param die the test to test for.
6649 ///
6650 /// @return true if the DIE is artificial, false otherwise.
6651 static bool
die_is_artificial(Dwarf_Die * die)6652 die_is_artificial(Dwarf_Die* die)
6653 {
6654 bool is_artificial;
6655 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6656 }
6657
6658 ///@return true if a tag represents a type, false otherwise.
6659 ///
6660 ///@param tag the tag to consider.
6661 static bool
is_type_tag(unsigned tag)6662 is_type_tag(unsigned tag)
6663 {
6664 bool result = false;
6665
6666 switch (tag)
6667 {
6668 case DW_TAG_array_type:
6669 case DW_TAG_class_type:
6670 case DW_TAG_enumeration_type:
6671 case DW_TAG_pointer_type:
6672 case DW_TAG_reference_type:
6673 case DW_TAG_string_type:
6674 case DW_TAG_structure_type:
6675 case DW_TAG_subroutine_type:
6676 case DW_TAG_typedef:
6677 case DW_TAG_union_type:
6678 case DW_TAG_ptr_to_member_type:
6679 case DW_TAG_set_type:
6680 case DW_TAG_subrange_type:
6681 case DW_TAG_base_type:
6682 case DW_TAG_const_type:
6683 case DW_TAG_file_type:
6684 case DW_TAG_packed_type:
6685 case DW_TAG_thrown_type:
6686 case DW_TAG_volatile_type:
6687 case DW_TAG_restrict_type:
6688 case DW_TAG_interface_type:
6689 case DW_TAG_unspecified_type:
6690 case DW_TAG_shared_type:
6691 case DW_TAG_rvalue_reference_type:
6692 case DW_TAG_coarray_type:
6693 case DW_TAG_atomic_type:
6694 case DW_TAG_immutable_type:
6695 result = true;
6696 break;
6697
6698 default:
6699 result = false;
6700 break;
6701 }
6702
6703 return result;
6704 }
6705
6706 /// Test if a given DIE is a type whose canonical type is to be
6707 /// propagated during DIE canonicalization
6708 ///
6709 /// This is a sub-routine of compare_dies.
6710 ///
6711 /// @param tag the tag of the DIE to consider.
6712 ///
6713 /// @return true iff the DIE of tag @p tag is can see its canonical
6714 /// type be propagated during the type comparison that happens during
6715 /// DIE canonicalization.
6716 static bool
is_canon_type_to_be_propagated_tag(unsigned tag)6717 is_canon_type_to_be_propagated_tag(unsigned tag)
6718 {
6719 bool result = false;
6720
6721 switch (tag)
6722 {
6723 case DW_TAG_class_type:
6724 case DW_TAG_structure_type:
6725 case DW_TAG_union_type:
6726 case DW_TAG_subroutine_type:
6727 case DW_TAG_subprogram:
6728 result = true;
6729 break;
6730
6731 default:
6732 result = false;
6733 break;
6734 }
6735
6736 return result;
6737 }
6738
6739 /// Test if a given kind of DIE ought to have its comparison result
6740 /// cached by compare_dies, so that subsequent invocations of
6741 /// compare_dies can be faster.
6742 ///
6743 /// @param tag the tag of the DIE to consider.
6744 ///
6745 /// @return true iff DIEs of the tag @p tag ought to have its
6746 /// comparison results cached.
6747 static bool
type_comparison_result_to_be_cached(unsigned tag)6748 type_comparison_result_to_be_cached(unsigned tag)
6749 {
6750 bool r = false;
6751 switch (tag)
6752 {
6753 case DW_TAG_class_type:
6754 case DW_TAG_structure_type:
6755 case DW_TAG_union_type:
6756 case DW_TAG_subroutine_type:
6757 case DW_TAG_subprogram:
6758 r = true;
6759 break;
6760
6761 default:
6762 r = false;
6763 break;
6764 }
6765 return r;
6766 }
6767
6768 /// Cache the result of comparing to type DIEs.
6769 ///
6770 /// @param rdr the context to consider.
6771 ///
6772 /// @param tag the tag of the DIEs to consider.
6773 ///
6774 /// @param p the offsets of the pair of DIEs being compared.
6775 ///
6776 /// @param result the comparison result to be cached.
6777 static bool
maybe_cache_type_comparison_result(const reader & rdr,int tag,const offset_pair_type & p,comparison_result result)6778 maybe_cache_type_comparison_result(const reader& rdr,
6779 int tag,
6780 const offset_pair_type& p,
6781 comparison_result result)
6782 {
6783 if (!type_comparison_result_to_be_cached(tag)
6784 || (result != COMPARISON_RESULT_EQUAL
6785 && result != COMPARISON_RESULT_DIFFERENT))
6786 return false;
6787
6788 rdr.die_comparison_results_[p] = result;
6789
6790 return true;
6791
6792 }
6793
6794 /// Get the cached result of the comparison of a pair of DIEs.
6795 ///
6796 /// @param rdr the context to consider.
6797 ///
6798 /// @param tag the tag of the pair of DIEs to consider.
6799 ///
6800 /// @param p the offsets of the pair of DIEs to consider.
6801 ///
6802 /// @param result out parameter set to the cached result of the
6803 /// comparison of @p p if it has been found.
6804 ///
6805 /// @return true iff a cached result for the comparisonof @p has been
6806 /// found and set into @p result.
6807 static bool
get_cached_type_comparison_result(const reader & rdr,const offset_pair_type & p,comparison_result & result)6808 get_cached_type_comparison_result(const reader& rdr,
6809 const offset_pair_type& p,
6810 comparison_result& result)
6811 {
6812 auto i = rdr.die_comparison_results_.find(p);
6813 if (i != rdr.die_comparison_results_.end())
6814 {
6815 result = i->second;
6816 return true;
6817 }
6818 return false;
6819 }
6820
6821 /// Get the cached result of the comparison of a pair of DIEs, if the
6822 /// kind of DIEs ought to have its comparison results cached.
6823 ///
6824 /// @param rdr the context to consider.
6825 ///
6826 /// @param tag the tag of the pair of DIEs to consider.
6827 ///
6828 /// @param p the offsets of the pair of DIEs to consider.
6829 ///
6830 /// @param result out parameter set to the cached result of the
6831 /// comparison of @p p if it has been found.
6832 ///
6833 /// @return true iff a cached result for the comparisonof @p has been
6834 /// found and set into @p result.
6835 static bool
maybe_get_cached_type_comparison_result(const reader & rdr,int tag,const offset_pair_type & p,comparison_result & result)6836 maybe_get_cached_type_comparison_result(const reader& rdr,
6837 int tag,
6838 const offset_pair_type& p,
6839 comparison_result& result)
6840 {
6841 if (type_comparison_result_to_be_cached(tag))
6842 {
6843 // Types of this kind might have their comparison result cached
6844 // when they are not canonicalized. So let's see if we have a
6845 // cached comparison result.
6846 if (get_cached_type_comparison_result(rdr, p, result))
6847 return true;
6848 }
6849 return false;
6850 }
6851
6852 /// Test if a given DIE is to be canonicalized.
6853 ///
6854 /// @param die the DIE to consider.
6855 ///
6856 /// @return true iff @p die is to be canonicalized.
6857 static bool
is_type_die_to_be_canonicalized(const Dwarf_Die * die)6858 is_type_die_to_be_canonicalized(const Dwarf_Die *die)
6859 {
6860 bool result = false;
6861 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6862
6863 if (!is_type_tag(tag))
6864 return false;
6865
6866 switch (tag)
6867 {
6868 case DW_TAG_class_type:
6869 case DW_TAG_structure_type:
6870 case DW_TAG_union_type:
6871 result = !die_is_declaration_only(die);
6872 break;
6873
6874 case DW_TAG_subroutine_type:
6875 case DW_TAG_subprogram:
6876 case DW_TAG_array_type:
6877 result = true;
6878
6879 default:
6880 break;
6881 }
6882
6883 return result;
6884 }
6885
6886 /// Test if a DIE tag represents a declaration.
6887 ///
6888 /// @param tag the DWARF tag to consider.
6889 ///
6890 /// @return true iff @p tag is for a declaration.
6891 static bool
is_decl_tag(unsigned tag)6892 is_decl_tag(unsigned tag)
6893 {
6894 switch (tag)
6895 {
6896 case DW_TAG_formal_parameter:
6897 case DW_TAG_imported_declaration:
6898 case DW_TAG_member:
6899 case DW_TAG_unspecified_parameters:
6900 case DW_TAG_subprogram:
6901 case DW_TAG_variable:
6902 case DW_TAG_namespace:
6903 case DW_TAG_GNU_template_template_param:
6904 case DW_TAG_GNU_template_parameter_pack:
6905 case DW_TAG_GNU_formal_parameter_pack:
6906 return true;
6907 }
6908 return false;
6909 }
6910
6911 /// Test if a DIE represents a type DIE.
6912 ///
6913 /// @param die the DIE to consider.
6914 ///
6915 /// @return true if @p die represents a type, false otherwise.
6916 static bool
die_is_type(const Dwarf_Die * die)6917 die_is_type(const Dwarf_Die* die)
6918 {
6919 if (!die)
6920 return false;
6921 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6922 }
6923
6924 /// Test if a DIE represents a declaration.
6925 ///
6926 /// @param die the DIE to consider.
6927 ///
6928 /// @return true if @p die represents a decl, false otherwise.
6929 static bool
die_is_decl(const Dwarf_Die * die)6930 die_is_decl(const Dwarf_Die* die)
6931 {
6932 if (!die)
6933 return false;
6934 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6935 }
6936
6937 /// Test if a DIE represents a namespace.
6938 ///
6939 /// @param die the DIE to consider.
6940 ///
6941 /// @return true if @p die represents a namespace, false otherwise.
6942 static bool
die_is_namespace(const Dwarf_Die * die)6943 die_is_namespace(const Dwarf_Die* die)
6944 {
6945 if (!die)
6946 return false;
6947 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
6948 }
6949
6950 /// Test if a DIE has tag DW_TAG_unspecified_type.
6951 ///
6952 /// @param die the DIE to consider.
6953 ///
6954 /// @return true if @p die has tag DW_TAG_unspecified_type.
6955 static bool
die_is_unspecified(Dwarf_Die * die)6956 die_is_unspecified(Dwarf_Die* die)
6957 {
6958 if (!die)
6959 return false;
6960 return (dwarf_tag(die) == DW_TAG_unspecified_type);
6961 }
6962
6963 /// Test if a DIE represents a void type.
6964 ///
6965 /// @param die the DIE to consider.
6966 ///
6967 /// @return true if @p die represents a void type, false otherwise.
6968 static bool
die_is_void_type(Dwarf_Die * die)6969 die_is_void_type(Dwarf_Die* die)
6970 {
6971 if (!die || dwarf_tag(die) != DW_TAG_base_type)
6972 return false;
6973
6974 string name = die_name(die);
6975 if (name == "void")
6976 return true;
6977
6978 return false;
6979 }
6980
6981 /// Test if a DIE represents a pointer type.
6982 ///
6983 /// @param die the die to consider.
6984 ///
6985 /// @return true iff @p die represents a pointer type.
6986 static bool
die_is_pointer_type(const Dwarf_Die * die)6987 die_is_pointer_type(const Dwarf_Die* die)
6988 {
6989 if (!die)
6990 return false;
6991
6992 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6993 if (tag == DW_TAG_pointer_type)
6994 return true;
6995
6996 return false;
6997 }
6998
6999 /// Test if a DIE is for a pointer, reference or qualified type to
7000 /// anonymous class or struct.
7001 ///
7002 /// @param die the DIE to consider.
7003 ///
7004 /// @return true iff @p is for a pointer, reference or qualified type
7005 /// to anonymous class or struct.
7006 static bool
pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die * die)7007 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7008 {
7009 if (!die_is_pointer_array_or_reference_type(die)
7010 && !die_is_qualified_type(die))
7011 return false;
7012
7013 Dwarf_Die underlying_type_die;
7014 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7015 return false;
7016
7017 if (!die_is_class_type(&underlying_type_die))
7018 return false;
7019
7020 string name = die_name(&underlying_type_die);
7021
7022 return name.empty();
7023 }
7024
7025 /// Test if a DIE represents a reference type.
7026 ///
7027 /// @param die the die to consider.
7028 ///
7029 /// @return true iff @p die represents a reference type.
7030 static bool
die_is_reference_type(const Dwarf_Die * die)7031 die_is_reference_type(const Dwarf_Die* die)
7032 {
7033 if (!die)
7034 return false;
7035
7036 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7037 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7038 return true;
7039
7040 return false;
7041 }
7042
7043 /// Test if a DIE represents an array type.
7044 ///
7045 /// @param die the die to consider.
7046 ///
7047 /// @return true iff @p die represents an array type.
7048 static bool
die_is_array_type(const Dwarf_Die * die)7049 die_is_array_type(const Dwarf_Die* die)
7050 {
7051 if (!die)
7052 return false;
7053
7054 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7055 if (tag == DW_TAG_array_type)
7056 return true;
7057
7058 return false;
7059 }
7060
7061 /// Test if a DIE represents a pointer, reference or array type.
7062 ///
7063 /// @param die the die to consider.
7064 ///
7065 /// @return true iff @p die represents a pointer or reference type.
7066 static bool
die_is_pointer_array_or_reference_type(const Dwarf_Die * die)7067 die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7068 {return (die_is_pointer_type(die)
7069 || die_is_reference_type(die)
7070 || die_is_array_type(die));}
7071
7072 /// Test if a DIE represents a pointer or a reference type.
7073 ///
7074 /// @param die the die to consider.
7075 ///
7076 /// @return true iff @p die represents a pointer or reference type.
7077 static bool
die_is_pointer_or_reference_type(const Dwarf_Die * die)7078 die_is_pointer_or_reference_type(const Dwarf_Die* die)
7079 {return (die_is_pointer_type(die) || die_is_reference_type(die));}
7080
7081 /// Test if a DIE represents a pointer, a reference or a typedef type.
7082 ///
7083 /// @param die the die to consider.
7084 ///
7085 /// @return true iff @p die represents a pointer, a reference or a
7086 /// typedef type.
7087 static bool
die_is_pointer_reference_or_typedef_type(const Dwarf_Die * die)7088 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7089 {return (die_is_pointer_array_or_reference_type(die)
7090 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7091
7092 /// Test if a DIE represents a class type.
7093 ///
7094 /// @param die the die to consider.
7095 ///
7096 /// @return true iff @p die represents a class type.
7097 static bool
die_is_class_type(const Dwarf_Die * die)7098 die_is_class_type(const Dwarf_Die* die)
7099 {
7100 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7101
7102 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7103 return true;
7104
7105 return false;
7106 }
7107
7108 /// Test if a DIE is for a qualified type.
7109 ///
7110 /// @param die the DIE to consider.
7111 ///
7112 /// @return true iff @p die is for a qualified type.
7113 static bool
die_is_qualified_type(const Dwarf_Die * die)7114 die_is_qualified_type(const Dwarf_Die* die)
7115 {
7116 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7117 if (tag == DW_TAG_const_type
7118 || tag == DW_TAG_volatile_type
7119 || tag == DW_TAG_restrict_type)
7120 return true;
7121
7122 return false;
7123 }
7124
7125 /// Test if a DIE is for a function type.
7126 ///
7127 /// @param die the DIE to consider.
7128 ///
7129 /// @return true iff @p die is for a function type.
7130 static bool
die_is_function_type(const Dwarf_Die * die)7131 die_is_function_type(const Dwarf_Die *die)
7132 {
7133 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7134 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7135 return true;
7136
7137 return false;
7138 }
7139
7140 /// Test if a DIE for a function pointer or member function has an
7141 /// DW_AT_object_pointer attribute.
7142 ///
7143 /// @param die the DIE to consider.
7144 ///
7145 /// @param object_pointer out parameter. It's set to the DIE for the
7146 /// object pointer iff the function returns true.
7147 ///
7148 /// @return true iff the DIE @p die has an object pointer. In that
7149 /// case, the parameter @p object_pointer is set to the DIE of that
7150 /// object pointer.
7151 static bool
die_has_object_pointer(const Dwarf_Die * die,Dwarf_Die & object_pointer)7152 die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7153 {
7154 if (!die)
7155 return false;
7156
7157 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7158 return true;
7159
7160 return false;
7161 }
7162
7163 /// Test if a DIE has children DIEs.
7164 ///
7165 /// @param die the DIE to consider.
7166 ///
7167 /// @return true iff @p DIE has at least one child node.
7168 static bool
die_has_children(const Dwarf_Die * die)7169 die_has_children(const Dwarf_Die* die)
7170 {
7171 if (!die)
7172 return false;
7173
7174 Dwarf_Die child;
7175 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7176 return true;
7177
7178 return false;
7179 }
7180
7181 /// When given the object pointer DIE of a function type or member
7182 /// function DIE, this function returns the "this" pointer that points
7183 /// to the associated class.
7184 ///
7185 /// @param die the DIE of the object pointer of the function or member
7186 /// function to consider.
7187 ///
7188 /// @param this_pointer_die out parameter. This is set to the DIE of
7189 /// the "this" pointer iff the function returns true.
7190 ///
7191 /// @return true iff the function found the "this" pointer from the
7192 /// object pointer DIE @p die. In that case, the parameter @p
7193 /// this_pointer_die is set to the DIE of that "this" pointer.
7194 static bool
die_this_pointer_from_object_pointer(Dwarf_Die * die,Dwarf_Die & this_pointer_die)7195 die_this_pointer_from_object_pointer(Dwarf_Die* die,
7196 Dwarf_Die& this_pointer_die)
7197 {
7198 ABG_ASSERT(die);
7199 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7200
7201 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7202 return true;
7203
7204 return false;
7205 }
7206
7207 /// Test if a given "this" pointer that points to a particular class
7208 /// type is for a const class or not. If it's for a const class, then
7209 /// it means the function type or the member function associated to
7210 /// that "this" pointer is const.
7211 ///
7212 /// @param die the DIE of the "this" pointer to consider.
7213 ///
7214 /// @return true iff @p die points to a const class type.
7215 static bool
die_this_pointer_is_const(Dwarf_Die * die)7216 die_this_pointer_is_const(Dwarf_Die* die)
7217 {
7218 ABG_ASSERT(die);
7219
7220 if (dwarf_tag(die) == DW_TAG_pointer_type)
7221 {
7222 Dwarf_Die pointed_to_type_die;
7223 if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7224 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7225 return true;
7226 }
7227
7228 return false;
7229 }
7230
7231 /// Test if an object pointer (referred-to via a DW_AT_object_pointer
7232 /// attribute) points to a const implicit class and so is for a const
7233 /// method or or a const member function type.
7234 ///
7235 /// @param die the DIE of the object pointer to consider.
7236 ///
7237 /// @return true iff the object pointer represented by @p die is for a
7238 /// a const method or const member function type.
7239 static bool
die_object_pointer_is_for_const_method(Dwarf_Die * die)7240 die_object_pointer_is_for_const_method(Dwarf_Die* die)
7241 {
7242 ABG_ASSERT(die);
7243 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7244
7245 Dwarf_Die this_pointer_die;
7246 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7247 if (die_this_pointer_is_const(&this_pointer_die))
7248 return true;
7249
7250 return false;
7251 }
7252
7253 /// Test if a DIE represents an entity that is at class scope.
7254 ///
7255 /// @param rdr the DWARF reader to use.
7256 ///
7257 /// @param die the DIE to consider.
7258 ///
7259 /// @param where_offset where we are logically at in the DIE stream.
7260 ///
7261 /// @param class_scope_die out parameter. Set to the DIE of the
7262 /// containing class iff @p die happens to be at class scope; that is,
7263 /// iff the function returns true.
7264 ///
7265 /// @return true iff @p die is at class scope. In that case, @p
7266 /// class_scope_die is set to the DIE of the class that contains @p
7267 /// die.
7268 static bool
die_is_at_class_scope(const reader & rdr,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & class_scope_die)7269 die_is_at_class_scope(const reader& rdr,
7270 const Dwarf_Die* die,
7271 size_t where_offset,
7272 Dwarf_Die& class_scope_die)
7273 {
7274 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7275 return false;
7276
7277 int tag = dwarf_tag(&class_scope_die);
7278
7279 return (tag == DW_TAG_structure_type
7280 || tag == DW_TAG_class_type
7281 || tag == DW_TAG_union_type);
7282 }
7283
7284 /// Return the leaf object under a pointer, reference or qualified
7285 /// type DIE.
7286 ///
7287 /// @param die the DIE of the type to consider.
7288 ///
7289 /// @param peeled_die out parameter. Set to the DIE of the leaf
7290 /// object iff the function actually peeled anything.
7291 ///
7292 /// @return true upon successful completion.
7293 static bool
die_peel_qual_ptr(Dwarf_Die * die,Dwarf_Die & peeled_die)7294 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7295 {
7296 if (!die)
7297 return false;
7298
7299 int tag = dwarf_tag(die);
7300
7301 if (tag == DW_TAG_const_type
7302 || tag == DW_TAG_volatile_type
7303 || tag == DW_TAG_restrict_type
7304 || tag == DW_TAG_pointer_type
7305 || tag == DW_TAG_reference_type
7306 || tag == DW_TAG_rvalue_reference_type)
7307 {
7308 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7309 return false;
7310 }
7311 else
7312 return false;
7313
7314 memcpy(&peeled_die, die, sizeof(peeled_die));
7315
7316 while (tag == DW_TAG_const_type
7317 || tag == DW_TAG_volatile_type
7318 || tag == DW_TAG_restrict_type
7319 || tag == DW_TAG_pointer_type
7320 || tag == DW_TAG_reference_type
7321 || tag == DW_TAG_rvalue_reference_type)
7322 {
7323 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7324 break;
7325 tag = dwarf_tag(&peeled_die);
7326 }
7327
7328 return true;
7329 }
7330
7331 /// Return the leaf object under a qualified type DIE.
7332 ///
7333 /// @param die the DIE of the type to consider.
7334 ///
7335 /// @param peeled_die out parameter. Set to the DIE of the leaf
7336 /// object iff the function actually peeled anything.
7337 ///
7338 /// @return true upon successful completion.
7339 static bool
die_peel_qualified(Dwarf_Die * die,Dwarf_Die & peeled_die)7340 die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7341 {
7342 if (!die)
7343 return false;
7344
7345 memcpy(&peeled_die, die, sizeof(peeled_die));
7346
7347 int tag = dwarf_tag(&peeled_die);
7348
7349 bool result = false;
7350 while (tag == DW_TAG_const_type
7351 || tag == DW_TAG_volatile_type
7352 || tag == DW_TAG_restrict_type)
7353 {
7354 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7355 break;
7356 tag = dwarf_tag(&peeled_die);
7357 result = true;
7358 }
7359
7360 return result;
7361 }
7362
7363 /// Return the leaf object under a typedef type DIE.
7364 ///
7365 /// @param die the DIE of the type to consider.
7366 ///
7367 /// @param peeled_die out parameter. Set to the DIE of the leaf
7368 /// object iff the function actually peeled anything.
7369 ///
7370 /// @return true upon successful completion.
7371 static bool
die_peel_typedef(Dwarf_Die * die,Dwarf_Die & peeled_die)7372 die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7373 {
7374 if (!die)
7375 return false;
7376
7377 int tag = dwarf_tag(die);
7378
7379 memcpy(&peeled_die, die, sizeof(peeled_die));
7380
7381 if (tag == DW_TAG_typedef)
7382 {
7383 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7384 return false;
7385 }
7386 else
7387 return false;
7388
7389 while (tag == DW_TAG_typedef)
7390 {
7391 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7392 break;
7393 tag = dwarf_tag(&peeled_die);
7394 }
7395
7396 return true;
7397
7398 }
7399
7400 /// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7401 ///
7402 /// @param die the DIE to consider.
7403 ///
7404 /// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7405 /// iff the function returned true.
7406 ///
7407 /// @return true iff the function could peel @p die.
7408 static bool
die_peel_pointer_and_typedef(const Dwarf_Die * die,Dwarf_Die & peeled_die)7409 die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7410 {
7411 if (!die)
7412 return false;
7413
7414 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7415
7416 if (tag == DW_TAG_pointer_type
7417 || tag == DW_TAG_reference_type
7418 || tag == DW_TAG_rvalue_reference_type
7419 || tag == DW_TAG_typedef)
7420 {
7421 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7422 return false;
7423 }
7424 else
7425 return false;
7426
7427 while (tag == DW_TAG_pointer_type
7428 || tag == DW_TAG_reference_type
7429 || tag == DW_TAG_rvalue_reference_type
7430 || tag == DW_TAG_typedef)
7431 {
7432 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7433 break;
7434 tag = dwarf_tag(&peeled_die);
7435 }
7436 return true;
7437 }
7438
7439 /// Test if a DIE for a function type represents a method type.
7440 ///
7441 /// @param rdr the DWARF reader.
7442 ///
7443 /// @param die the DIE to consider.
7444 ///
7445 /// @param where_offset where we logically are in the stream of DIEs.
7446 ///
7447 /// @param object_pointer_die out parameter. This is set by the
7448 /// function to the DIE that refers to the formal function parameter
7449 /// which holds the implicit "this" pointer of the method. That die
7450 /// is called the object pointer DIE. This is set iff the function
7451 ///
7452 /// @param class_die out parameter. This is set by the function to
7453 /// the DIE that represents the class of the method type. This is set
7454 /// iff the function returns true.
7455 ///
7456 /// @param is_static out parameter. This is set to true by the
7457 /// function if @p die is a static method. This is set iff the
7458 /// function returns true.
7459 ///
7460 /// @return true iff @p die is a DIE for a method type.
7461 static bool
die_function_type_is_method_type(const reader & rdr,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & object_pointer_die,Dwarf_Die & class_die,bool & is_static)7462 die_function_type_is_method_type(const reader& rdr,
7463 const Dwarf_Die *die,
7464 size_t where_offset,
7465 Dwarf_Die& object_pointer_die,
7466 Dwarf_Die& class_die,
7467 bool& is_static)
7468 {
7469 if (!die)
7470 return false;
7471
7472 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7473 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7474
7475 bool has_object_pointer = false;
7476 is_static = false;
7477 if (tag == DW_TAG_subprogram)
7478 {
7479 Dwarf_Die spec_or_origin_die;
7480 if (die_die_attribute(die, DW_AT_specification,
7481 spec_or_origin_die)
7482 || die_die_attribute(die, DW_AT_abstract_origin,
7483 spec_or_origin_die))
7484 {
7485 if (die_has_object_pointer(&spec_or_origin_die,
7486 object_pointer_die))
7487 has_object_pointer = true;
7488 else
7489 {
7490 if (die_is_at_class_scope(rdr, &spec_or_origin_die,
7491 where_offset, class_die))
7492 is_static = true;
7493 else
7494 return false;
7495 }
7496 }
7497 else
7498 {
7499 if (die_has_object_pointer(die, object_pointer_die))
7500 has_object_pointer = true;
7501 else
7502 {
7503 if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7504 is_static = true;
7505 else
7506 return false;
7507 }
7508 }
7509 }
7510 else
7511 {
7512 if (die_has_object_pointer(die, object_pointer_die))
7513 has_object_pointer = true;
7514 else
7515 return false;
7516 }
7517
7518 if (!is_static)
7519 {
7520 ABG_ASSERT(has_object_pointer);
7521 // The object pointer die points to a DW_TAG_formal_parameter which
7522 // is the "this" parameter. The type of the "this" parameter is a
7523 // pointer. Let's get that pointer type.
7524 Dwarf_Die this_type_die;
7525 if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7526 return false;
7527
7528 // So the class type is the type pointed to by the type of the "this"
7529 // parameter.
7530 if (!die_peel_qual_ptr(&this_type_die, class_die))
7531 return false;
7532
7533 // And make we return a class type, rather than a typedef to a
7534 // class.
7535 die_peel_typedef(&class_die, class_die);
7536 }
7537
7538 return true;
7539 }
7540
7541 enum virtuality
7542 {
7543 VIRTUALITY_NOT_VIRTUAL,
7544 VIRTUALITY_VIRTUAL,
7545 VIRTUALITY_PURE_VIRTUAL
7546 };
7547
7548 /// Get the virtual-ness of a given DIE, that is, the value of the
7549 /// DW_AT_virtuality attribute.
7550 ///
7551 /// @param die the DIE to read from.
7552 ///
7553 /// @param virt the resulting virtuality attribute. This is set iff
7554 /// the function returns true.
7555 ///
7556 /// @return true if the virtual-ness could be determined.
7557 static bool
die_virtuality(const Dwarf_Die * die,virtuality & virt)7558 die_virtuality(const Dwarf_Die* die, virtuality& virt)
7559 {
7560 if (!die)
7561 return false;
7562
7563 uint64_t v = 0;
7564 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7565
7566 if (v == DW_VIRTUALITY_virtual)
7567 virt = VIRTUALITY_VIRTUAL;
7568 else if (v == DW_VIRTUALITY_pure_virtual)
7569 virt = VIRTUALITY_PURE_VIRTUAL;
7570 else
7571 virt = VIRTUALITY_NOT_VIRTUAL;
7572
7573 return true;
7574 }
7575
7576 /// Test whether the DIE represent either a virtual base or function.
7577 ///
7578 /// @param die the DIE to consider.
7579 ///
7580 /// @return bool if the DIE represents a virtual base or function,
7581 /// false othersise.
7582 static bool
die_is_virtual(const Dwarf_Die * die)7583 die_is_virtual(const Dwarf_Die* die)
7584 {
7585 virtuality v;
7586 if (!die_virtuality(die, v))
7587 return false;
7588
7589 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7590 }
7591
7592 /// Test if the DIE represents an entity that was declared inlined.
7593 ///
7594 /// @param die the DIE to test for.
7595 ///
7596 /// @return true if the DIE represents an entity that was declared
7597 /// inlined.
7598 static bool
die_is_declared_inline(Dwarf_Die * die)7599 die_is_declared_inline(Dwarf_Die* die)
7600 {
7601 uint64_t inline_value = 0;
7602 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7603 return false;
7604 return inline_value == DW_INL_declared_inlined;
7605 }
7606
7607 /// Compare two DWARF strings using the most accurate (and slowest)
7608 /// method possible.
7609 ///
7610 /// @param l the DIE that carries the first string to consider, as an
7611 /// attribute value.
7612 ///
7613 /// @param attr_name the name of the attribute which value is the
7614 /// string to compare.
7615 ///
7616 /// @return true iff the string carried by @p l equals the one carried
7617 /// by @p r.
7618 static bool
slowly_compare_strings(const Dwarf_Die * l,const Dwarf_Die * r,unsigned attr_name)7619 slowly_compare_strings(const Dwarf_Die *l,
7620 const Dwarf_Die *r,
7621 unsigned attr_name)
7622 {
7623 const char *l_str = die_char_str_attribute(l, attr_name),
7624 *r_str = die_char_str_attribute(r, attr_name);
7625 if (!l_str && !r_str)
7626 return true;
7627 return l_str && r_str && !strcmp(l_str, r_str);
7628 }
7629
7630 /// This function is a fast routine (optimization) to compare the
7631 /// values of two string attributes of two DIEs.
7632 ///
7633 /// @param l the first DIE to consider.
7634 ///
7635 /// @param r the second DIE to consider.
7636 ///
7637 /// @param attr_name the name of the attribute to compare, on the two
7638 /// DIEs above.
7639 ///
7640 /// @param result out parameter. This is set to the result of the
7641 /// comparison. If the value of attribute @p attr_name on DIE @p l
7642 /// equals the value of attribute @p attr_name on DIE @p r, then the
7643 /// the argument of this parameter is set to true. Otherwise, it's
7644 /// set to false. Note that the argument of this parameter is set iff
7645 /// the function returned true.
7646 ///
7647 /// @return true iff the comparison could be performed. There are
7648 /// cases in which the comparison cannot be performed. For instance,
7649 /// if one of the DIEs does not have the attribute @p attr_name. In
7650 /// any case, if this function returns true, then the parameter @p
7651 /// result is set to the result of the comparison.
7652 static bool
compare_dies_string_attribute_value(const Dwarf_Die * l,const Dwarf_Die * r,unsigned attr_name,bool & result)7653 compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7654 unsigned attr_name,
7655 bool &result)
7656 {
7657 Dwarf_Attribute l_attr, r_attr;
7658 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7659 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7660 return false;
7661
7662 ABG_ASSERT(l_attr.form == DW_FORM_strp
7663 || l_attr.form == DW_FORM_string
7664 || l_attr.form == DW_FORM_GNU_strp_alt
7665 || form_is_DW_FORM_strx(l_attr.form)
7666 || form_is_DW_FORM_line_strp(l_attr.form));
7667
7668 ABG_ASSERT(r_attr.form == DW_FORM_strp
7669 || r_attr.form == DW_FORM_string
7670 || r_attr.form == DW_FORM_GNU_strp_alt
7671 || form_is_DW_FORM_strx(r_attr.form)
7672 || form_is_DW_FORM_line_strp(r_attr.form));
7673
7674 if ((l_attr.form == DW_FORM_strp
7675 && r_attr.form == DW_FORM_strp)
7676 || (l_attr.form == DW_FORM_GNU_strp_alt
7677 && r_attr.form == DW_FORM_GNU_strp_alt)
7678 || (form_is_DW_FORM_strx(l_attr.form)
7679 && form_is_DW_FORM_strx(r_attr.form))
7680 || (form_is_DW_FORM_line_strp(l_attr.form)
7681 && form_is_DW_FORM_line_strp(r_attr.form)))
7682 {
7683 // So these string attributes are actually pointers into a
7684 // string table. The string table is most likely de-duplicated
7685 // so comparing the *values* of the pointers should be enough.
7686 //
7687 // This is the fast path.
7688 if (l_attr.valp == r_attr.valp)
7689 {
7690 #if WITH_DEBUG_TYPE_CANONICALIZATION
7691 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
7692 #endif
7693 result = true;
7694 return true;
7695 }
7696 }
7697
7698 // If we reached this point it means we couldn't use the fast path
7699 // because the string atttributes are strings that are "inline" in
7700 // the debug info section. Let's just compare them the slow and
7701 // obvious way.
7702 result = slowly_compare_strings(l, r, attr_name);
7703 return true;
7704 }
7705
7706 /// Compare the file path of the compilation units (aka CUs)
7707 /// associated to two DIEs.
7708 ///
7709 /// If the DIEs are for pointers or typedefs, this function also
7710 /// compares the file paths of the CUs of the leaf DIEs (underlying
7711 /// DIEs of the pointer or the typedef).
7712 ///
7713 /// @param l the first type DIE to consider.
7714 ///
7715 /// @param r the second type DIE to consider.
7716 ///
7717 /// @return true iff the file paths of the DIEs of the two types are
7718 /// equal.
7719 static bool
compare_dies_cu_decl_file(const Dwarf_Die * l,const Dwarf_Die * r,bool & result)7720 compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7721 {
7722 Dwarf_Die l_cu, r_cu;
7723 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7724 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7725 return false;
7726
7727 bool compared =
7728 compare_dies_string_attribute_value(&l_cu, &r_cu,
7729 DW_AT_name,
7730 result);
7731 if (compared && result)
7732 {
7733 Dwarf_Die peeled_l, peeled_r;
7734 if (die_is_pointer_reference_or_typedef_type(l)
7735 && die_is_pointer_reference_or_typedef_type(r)
7736 && die_peel_pointer_and_typedef(l, peeled_l)
7737 && die_peel_pointer_and_typedef(r, peeled_r))
7738 {
7739 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7740 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7741 return false;
7742 compared =
7743 compare_dies_string_attribute_value(&l_cu, &r_cu,
7744 DW_AT_name,
7745 result);
7746 }
7747 }
7748
7749 return compared;
7750 }
7751
7752 // -----------------------------------
7753 // <location expression evaluation>
7754 // -----------------------------------
7755
7756 /// Get the value of a given DIE attribute, knowing that it must be a
7757 /// location expression.
7758 ///
7759 /// @param die the DIE to read the attribute from.
7760 ///
7761 /// @param attr_name the name of the attribute to read the value for.
7762 ///
7763 /// @param expr the pointer to allocate and fill with the resulting
7764 /// array of operators + operands forming a dwarf expression. This is
7765 /// set iff the function returns true.
7766 ///
7767 /// @param expr_len the length of the resulting dwarf expression.
7768 /// This is set iff the function returns true.
7769 ///
7770 /// @return true if the attribute exists and has a non-empty dwarf expression
7771 /// as value. In that case the expr and expr_len arguments are set to the
7772 /// resulting dwarf expression.
7773 static bool
die_location_expr(const Dwarf_Die * die,unsigned attr_name,Dwarf_Op ** expr,size_t * expr_len)7774 die_location_expr(const Dwarf_Die* die,
7775 unsigned attr_name,
7776 Dwarf_Op** expr,
7777 size_t* expr_len)
7778 {
7779 if (!die)
7780 return false;
7781
7782 Dwarf_Attribute attr;
7783 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7784 return false;
7785
7786 size_t len = 0;
7787 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7788
7789 // Ignore location expressions where reading them succeeded but
7790 // their length is 0.
7791 result &= len > 0;
7792
7793 if (result)
7794 *expr_len = len;
7795
7796 return result;
7797 }
7798
7799 /// If the current operation in the dwarf expression represents a push
7800 /// of a constant value onto the dwarf expr virtual machine (aka
7801 /// DEVM), perform the operation and update the DEVM.
7802 ///
7803 /// If the result of the operation is a constant, update the DEVM
7804 /// accumulator with its value. Otherwise, the DEVM accumulator is
7805 /// left with its previous value.
7806 ///
7807 /// @param ops the array of the dwarf expression operations to consider.
7808 ///
7809 /// @param ops_len the lengths of @p ops array above.
7810 ///
7811 /// @param index the index of the operation to interpret, in @p ops.
7812 ///
7813 /// @param next_index the index of the operation to interpret at the
7814 /// next step, after this function completed and returned. This is
7815 /// set an output parameter that is set iff the function returns true.
7816 ///
7817 /// @param ctxt the DEVM evaluation context.
7818 ///
7819 /// @return true if the current operation actually pushes a constant
7820 /// value onto the DEVM stack, false otherwise.
7821 static bool
op_pushes_constant_value(Dwarf_Op * ops,size_t ops_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)7822 op_pushes_constant_value(Dwarf_Op* ops,
7823 size_t ops_len,
7824 size_t index,
7825 size_t& next_index,
7826 dwarf_expr_eval_context& ctxt)
7827 {
7828 ABG_ASSERT(index < ops_len);
7829
7830 Dwarf_Op& op = ops[index];
7831 int64_t value = 0;
7832
7833 switch (op.atom)
7834 {
7835 case DW_OP_addr:
7836 value = ops[index].number;
7837 break;
7838
7839 case DW_OP_const1u:
7840 case DW_OP_const1s:
7841 case DW_OP_const2u:
7842 case DW_OP_const2s:
7843 case DW_OP_const4u:
7844 case DW_OP_const4s:
7845 case DW_OP_const8u:
7846 case DW_OP_const8s:
7847 case DW_OP_constu:
7848 case DW_OP_consts:
7849 value = ops[index].number;
7850 break;
7851
7852 case DW_OP_lit0:
7853 value = 0;
7854 break;
7855 case DW_OP_lit1:
7856 value = 1;
7857 break;
7858 case DW_OP_lit2:
7859 value = 2;
7860 break;
7861 case DW_OP_lit3:
7862 value = 3;
7863 break;
7864 case DW_OP_lit4:
7865 value = 4;
7866 break;
7867 case DW_OP_lit5:
7868 value = 5;
7869 break;
7870 case DW_OP_lit6:
7871 value = 6;
7872 break;
7873 case DW_OP_lit7:
7874 value = 7;
7875 break;
7876 case DW_OP_lit8:
7877 value = 8;
7878 break;
7879 case DW_OP_lit9:
7880 value = 9;
7881 break;
7882 case DW_OP_lit10:
7883 value = 10;
7884 break;
7885 case DW_OP_lit11:
7886 value = 11;
7887 break;
7888 case DW_OP_lit12:
7889 value = 12;
7890 break;
7891 case DW_OP_lit13:
7892 value = 13;
7893 break;
7894 case DW_OP_lit14:
7895 value = 14;
7896 break;
7897 case DW_OP_lit15:
7898 value = 15;
7899 break;
7900 case DW_OP_lit16:
7901 value = 16;
7902 break;
7903 case DW_OP_lit17:
7904 value = 17;
7905 break;
7906 case DW_OP_lit18:
7907 value = 18;
7908 break;
7909 case DW_OP_lit19:
7910 value = 19;
7911 break;
7912 case DW_OP_lit20:
7913 value = 20;
7914 break;
7915 case DW_OP_lit21:
7916 value = 21;
7917 break;
7918 case DW_OP_lit22:
7919 value = 22;
7920 break;
7921 case DW_OP_lit23:
7922 value = 23;
7923 break;
7924 case DW_OP_lit24:
7925 value = 24;
7926 break;
7927 case DW_OP_lit25:
7928 value = 25;
7929 break;
7930 case DW_OP_lit26:
7931 value = 26;
7932 break;
7933 case DW_OP_lit27:
7934 value = 27;
7935 break;
7936 case DW_OP_lit28:
7937 value = 28;
7938 break;
7939 case DW_OP_lit29:
7940 value = 29;
7941 break;
7942 case DW_OP_lit30:
7943 value = 30;
7944 break;
7945 case DW_OP_lit31:
7946 value = 31;
7947 break;
7948
7949 default:
7950 return false;
7951 }
7952
7953 expr_result r(value);
7954 ctxt.push(r);
7955 ctxt.accum = r;
7956 next_index = index + 1;
7957
7958 return true;
7959 }
7960
7961 /// If the current operation in the dwarf expression represents a push
7962 /// of a non-constant value onto the dwarf expr virtual machine (aka
7963 /// DEVM), perform the operation and update the DEVM. A non-constant
7964 /// is namely a quantity for which we need inferior (a running program
7965 /// image) state to know the exact value.
7966 ///
7967 /// Upon successful completion, as the result of the operation is a
7968 /// non-constant the DEVM accumulator value is left to its state as of
7969 /// before the invocation of this function.
7970 ///
7971 /// @param ops the array of the dwarf expression operations to consider.
7972 ///
7973 /// @param ops_len the lengths of @p ops array above.
7974 ///
7975 /// @param index the index of the operation to interpret, in @p ops.
7976 ///
7977 /// @param next_index the index of the operation to interpret at the
7978 /// next step, after this function completed and returned. This is
7979 /// set an output parameter that is set iff the function returns true.
7980 ///
7981 /// @param ctxt the DEVM evaluation context.
7982 ///
7983 /// @return true if the current operation actually pushes a
7984 /// non-constant value onto the DEVM stack, false otherwise.
7985 static bool
op_pushes_non_constant_value(Dwarf_Op * ops,size_t ops_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)7986 op_pushes_non_constant_value(Dwarf_Op* ops,
7987 size_t ops_len,
7988 size_t index,
7989 size_t& next_index,
7990 dwarf_expr_eval_context& ctxt)
7991 {
7992 ABG_ASSERT(index < ops_len);
7993 Dwarf_Op& op = ops[index];
7994
7995 switch (op.atom)
7996 {
7997 case DW_OP_reg0:
7998 case DW_OP_reg1:
7999 case DW_OP_reg2:
8000 case DW_OP_reg3:
8001 case DW_OP_reg4:
8002 case DW_OP_reg5:
8003 case DW_OP_reg6:
8004 case DW_OP_reg7:
8005 case DW_OP_reg8:
8006 case DW_OP_reg9:
8007 case DW_OP_reg10:
8008 case DW_OP_reg11:
8009 case DW_OP_reg12:
8010 case DW_OP_reg13:
8011 case DW_OP_reg14:
8012 case DW_OP_reg15:
8013 case DW_OP_reg16:
8014 case DW_OP_reg17:
8015 case DW_OP_reg18:
8016 case DW_OP_reg19:
8017 case DW_OP_reg20:
8018 case DW_OP_reg21:
8019 case DW_OP_reg22:
8020 case DW_OP_reg23:
8021 case DW_OP_reg24:
8022 case DW_OP_reg25:
8023 case DW_OP_reg26:
8024 case DW_OP_reg27:
8025 case DW_OP_reg28:
8026 case DW_OP_reg29:
8027 case DW_OP_reg30:
8028 case DW_OP_reg31:
8029 next_index = index + 1;
8030 break;
8031
8032 case DW_OP_breg0:
8033 case DW_OP_breg1:
8034 case DW_OP_breg2:
8035 case DW_OP_breg3:
8036 case DW_OP_breg4:
8037 case DW_OP_breg5:
8038 case DW_OP_breg6:
8039 case DW_OP_breg7:
8040 case DW_OP_breg8:
8041 case DW_OP_breg9:
8042 case DW_OP_breg10:
8043 case DW_OP_breg11:
8044 case DW_OP_breg12:
8045 case DW_OP_breg13:
8046 case DW_OP_breg14:
8047 case DW_OP_breg15:
8048 case DW_OP_breg16:
8049 case DW_OP_breg17:
8050 case DW_OP_breg18:
8051 case DW_OP_breg19:
8052 case DW_OP_breg20:
8053 case DW_OP_breg21:
8054 case DW_OP_breg22:
8055 case DW_OP_breg23:
8056 case DW_OP_breg24:
8057 case DW_OP_breg25:
8058 case DW_OP_breg26:
8059 case DW_OP_breg27:
8060 case DW_OP_breg28:
8061 case DW_OP_breg29:
8062 case DW_OP_breg30:
8063 case DW_OP_breg31:
8064 next_index = index + 1;
8065 break;
8066
8067 case DW_OP_regx:
8068 next_index = index + 2;
8069 break;
8070
8071 case DW_OP_fbreg:
8072 next_index = index + 1;
8073 break;
8074
8075 case DW_OP_bregx:
8076 next_index = index + 1;
8077 break;
8078
8079 case DW_OP_GNU_variable_value:
8080 next_index = index + 1;
8081 break;
8082
8083 default:
8084 return false;
8085 }
8086
8087 expr_result r(false);
8088 ctxt.push(r);
8089
8090 return true;
8091 }
8092
8093 /// If the current operation in the dwarf expression represents a
8094 /// manipulation of the stack of the DWARF Expression Virtual Machine
8095 /// (aka DEVM), this function performs the operation and updates the
8096 /// state of the DEVM. If the result of the operation represents a
8097 /// constant value, then the accumulator of the DEVM is set to that
8098 /// result's value, Otherwise, the DEVM accumulator is left with its
8099 /// previous value.
8100 ///
8101 /// @param expr the array of the dwarf expression operations to consider.
8102 ///
8103 /// @param expr_len the lengths of @p ops array above.
8104 ///
8105 /// @param index the index of the operation to interpret, in @p ops.
8106 ///
8107 /// @param next_index the index of the operation to interpret at the
8108 /// next step, after this function completed and returned. This is
8109 /// set an output parameter that is set iff the function returns true.
8110 ///
8111 /// @param ctxt the DEVM evaluation context.
8112 ///
8113 /// @return true if the current operation actually manipulates the
8114 /// DEVM stack, false otherwise.
8115 static bool
op_manipulates_stack(Dwarf_Op * expr,size_t expr_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)8116 op_manipulates_stack(Dwarf_Op* expr,
8117 size_t expr_len,
8118 size_t index,
8119 size_t& next_index,
8120 dwarf_expr_eval_context& ctxt)
8121 {
8122 Dwarf_Op& op = expr[index];
8123 expr_result v;
8124
8125 switch (op.atom)
8126 {
8127 case DW_OP_dup:
8128 v = ctxt.stack.front();
8129 ctxt.push(v);
8130 break;
8131
8132 case DW_OP_drop:
8133 v = ctxt.stack.front();
8134 ctxt.pop();
8135 break;
8136
8137 case DW_OP_over:
8138 ABG_ASSERT(ctxt.stack.size() > 1);
8139 v = ctxt.stack[1];
8140 ctxt.push(v);
8141 break;
8142
8143 case DW_OP_pick:
8144 ABG_ASSERT(index + 1 < expr_len);
8145 v = op.number;
8146 ctxt.push(v);
8147 break;
8148
8149 case DW_OP_swap:
8150 ABG_ASSERT(ctxt.stack.size() > 1);
8151 v = ctxt.stack[1];
8152 ctxt.stack.erase(ctxt.stack.begin() + 1);
8153 ctxt.push(v);
8154 break;
8155
8156 case DW_OP_rot:
8157 ABG_ASSERT(ctxt.stack.size() > 2);
8158 v = ctxt.stack[2];
8159 ctxt.stack.erase(ctxt.stack.begin() + 2);
8160 ctxt.push(v);
8161 break;
8162
8163 case DW_OP_deref:
8164 case DW_OP_deref_size:
8165 ABG_ASSERT(ctxt.stack.size() > 0);
8166 ctxt.pop();
8167 v.is_const(false);
8168 ctxt.push(v);
8169 break;
8170
8171 case DW_OP_xderef:
8172 case DW_OP_xderef_size:
8173 ABG_ASSERT(ctxt.stack.size() > 1);
8174 ctxt.pop();
8175 ctxt.pop();
8176 v.is_const(false);
8177 ctxt.push(v);
8178 break;
8179
8180 case DW_OP_push_object_address:
8181 v.is_const(false);
8182 ctxt.push(v);
8183 break;
8184
8185 case DW_OP_form_tls_address:
8186 case DW_OP_GNU_push_tls_address:
8187 ABG_ASSERT(ctxt.stack.size() > 0);
8188 v = ctxt.pop();
8189 if (op.atom == DW_OP_form_tls_address)
8190 v.is_const(false);
8191 ctxt.push(v);
8192 break;
8193
8194 case DW_OP_call_frame_cfa:
8195 v.is_const(false);
8196 ctxt.push(v);
8197 break;
8198
8199 default:
8200 return false;
8201 }
8202
8203 if (v.is_const())
8204 ctxt.accum = v;
8205
8206 if (op.atom == DW_OP_form_tls_address
8207 || op.atom == DW_OP_GNU_push_tls_address)
8208 ctxt.set_tls_address(true);
8209 else
8210 ctxt.set_tls_address(false);
8211
8212 next_index = index + 1;
8213
8214 return true;
8215 }
8216
8217 /// If the current operation in the dwarf expression represents a push
8218 /// of an arithmetic or logic operation onto the dwarf expr virtual
8219 /// machine (aka DEVM), perform the operation and update the DEVM.
8220 ///
8221 /// If the result of the operation is a constant, update the DEVM
8222 /// accumulator with its value. Otherwise, the DEVM accumulator is
8223 /// left with its previous value.
8224 ///
8225 /// @param expr the array of the dwarf expression operations to consider.
8226 ///
8227 /// @param expr_len the lengths of @p expr array above.
8228 ///
8229 /// @param index the index of the operation to interpret, in @p expr.
8230 ///
8231 /// @param next_index the index of the operation to interpret at the
8232 /// next step, after this function completed and returned. This is
8233 /// set an output parameter that is set iff the function returns true.
8234 ///
8235 /// @param ctxt the DEVM evaluation context.
8236 ///
8237 /// @return true if the current operation actually represent an
8238 /// arithmetic or logic operation.
8239 static bool
op_is_arith_logic(Dwarf_Op * expr,size_t expr_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)8240 op_is_arith_logic(Dwarf_Op* expr,
8241 size_t expr_len,
8242 size_t index,
8243 size_t& next_index,
8244 dwarf_expr_eval_context& ctxt)
8245 {
8246 ABG_ASSERT(index < expr_len);
8247
8248 Dwarf_Op& op = expr[index];
8249 expr_result val1, val2;
8250 bool result = false;
8251
8252 switch (op.atom)
8253 {
8254 case DW_OP_abs:
8255 ABG_ASSERT(ctxt.stack.size() > 0);
8256 val1 = ctxt.pop();
8257 val1 = val1.abs();
8258 ctxt.push(val1);
8259 result = true;
8260 break;
8261
8262 case DW_OP_and:
8263 ABG_ASSERT(ctxt.stack.size() > 1);
8264 val1 = ctxt.pop();
8265 val2 = ctxt.pop();
8266 ctxt.push(val1 & val2);
8267 break;
8268
8269 case DW_OP_div:
8270 ABG_ASSERT(ctxt.stack.size() > 1);
8271 val1 = ctxt.pop();
8272 val2 = ctxt.pop();
8273 if (!val1.is_const())
8274 val1 = 1;
8275 ctxt.push(val2 / val1);
8276 result = true;
8277 break;
8278
8279 case DW_OP_minus:
8280 ABG_ASSERT(ctxt.stack.size() > 1);
8281 val1 = ctxt.pop();
8282 val2 = ctxt.pop();
8283 ctxt.push(val2 - val1);
8284 result = true;
8285 break;
8286
8287 case DW_OP_mod:
8288 ABG_ASSERT(ctxt.stack.size() > 1);
8289 val1 = ctxt.pop();
8290 val2 = ctxt.pop();
8291 ctxt.push(val2 % val1);
8292 result = true;
8293 break;
8294
8295 case DW_OP_mul:
8296 ABG_ASSERT(ctxt.stack.size() > 1);
8297 val1 = ctxt.pop();
8298 val2 = ctxt.pop();
8299 ctxt.push(val2 * val1);
8300 result = true;
8301 break;
8302
8303 case DW_OP_neg:
8304 ABG_ASSERT(ctxt.stack.size() > 0);
8305 val1 = ctxt.pop();
8306 ctxt.push(-val1);
8307 result = true;
8308 break;
8309
8310 case DW_OP_not:
8311 ABG_ASSERT(ctxt.stack.size() > 0);
8312 val1 = ctxt.pop();
8313 ctxt.push(~val1);
8314 result = true;
8315 break;
8316
8317 case DW_OP_or:
8318 ABG_ASSERT(ctxt.stack.size() > 1);
8319 val1 = ctxt.pop();
8320 val2 = ctxt.pop();
8321 ctxt.push(val1 | val2);
8322 result = true;
8323 break;
8324
8325 case DW_OP_plus:
8326 ABG_ASSERT(ctxt.stack.size() > 1);
8327 val1 = ctxt.pop();
8328 val2 = ctxt.pop();
8329 ctxt.push(val2 + val1);
8330 result = true;
8331 break;
8332
8333 case DW_OP_plus_uconst:
8334 ABG_ASSERT(ctxt.stack.size() > 0);
8335 val1 = ctxt.pop();
8336 val1 += op.number;
8337 ctxt.push(val1);
8338 result = true;
8339 break;
8340
8341 case DW_OP_shl:
8342 ABG_ASSERT(ctxt.stack.size() > 1);
8343 val1 = ctxt.pop();
8344 val2 = ctxt.pop();
8345 ctxt.push(val2 << val1);
8346 result = true;
8347 break;
8348
8349 case DW_OP_shr:
8350 case DW_OP_shra:
8351 ABG_ASSERT(ctxt.stack.size() > 1);
8352 val1 = ctxt.pop();
8353 val2 = ctxt.pop();
8354 ctxt.push(val2 >> val1);
8355 result = true;
8356 break;
8357
8358 case DW_OP_xor:
8359 ABG_ASSERT(ctxt.stack.size() > 1);
8360 val1 = ctxt.pop();
8361 val2 = ctxt.pop();
8362 ctxt.push(val2 ^ val1);
8363 result = true;
8364 break;
8365
8366 default:
8367 break;
8368 }
8369
8370 if (result == true)
8371 {
8372 if (ctxt.stack.front().is_const())
8373 ctxt.accum = ctxt.stack.front();
8374
8375 next_index = index + 1;
8376 }
8377 return result;;
8378 }
8379
8380 /// If the current operation in the dwarf expression represents a push
8381 /// of a control flow operation onto the dwarf expr virtual machine
8382 /// (aka DEVM), perform the operation and update the DEVM.
8383 ///
8384 /// If the result of the operation is a constant, update the DEVM
8385 /// accumulator with its value. Otherwise, the DEVM accumulator is
8386 /// left with its previous value.
8387 ///
8388 /// @param expr the array of the dwarf expression operations to consider.
8389 ///
8390 /// @param expr_len the lengths of @p expr array above.
8391 ///
8392 /// @param index the index of the operation to interpret, in @p expr.
8393 ///
8394 /// @param next_index the index of the operation to interpret at the
8395 /// next step, after this function completed and returned. This is
8396 /// set an output parameter that is set iff the function returns true.
8397 ///
8398 /// @param ctxt the DEVM evaluation context.
8399 ///
8400 /// @return true if the current operation actually represents a
8401 /// control flow operation, false otherwise.
8402 static bool
op_is_control_flow(Dwarf_Op * expr,size_t expr_len,size_t index,size_t & next_index,dwarf_expr_eval_context & ctxt)8403 op_is_control_flow(Dwarf_Op* expr,
8404 size_t expr_len,
8405 size_t index,
8406 size_t& next_index,
8407 dwarf_expr_eval_context& ctxt)
8408 {
8409 ABG_ASSERT(index < expr_len);
8410
8411 Dwarf_Op& op = expr[index];
8412 expr_result val1, val2;
8413
8414 switch (op.atom)
8415 {
8416 case DW_OP_eq:
8417 case DW_OP_ge:
8418 case DW_OP_gt:
8419 case DW_OP_le:
8420 case DW_OP_lt:
8421 case DW_OP_ne:
8422 {
8423 bool value = true;
8424 val1 = ctxt.pop();
8425 val2 = ctxt.pop();
8426 if (op.atom == DW_OP_eq)
8427 value = val2 == val1;
8428 else if (op.atom == DW_OP_ge)
8429 value = val2 >= val1;
8430 else if (op.atom == DW_OP_gt)
8431 value = val2 > val1;
8432 else if (op.atom == DW_OP_le)
8433 value = val2 <= val1;
8434 else if (op.atom == DW_OP_lt)
8435 value = val2 < val1;
8436 else if (op.atom == DW_OP_ne)
8437 value = val2 != val1;
8438
8439 val1 = value ? 1 : 0;
8440 ctxt.push(val1);
8441 }
8442 break;
8443
8444 case DW_OP_skip:
8445 if (op.number > 0)
8446 index += op.number - 1;
8447 break;
8448
8449 case DW_OP_bra:
8450 val1 = ctxt.pop();
8451 if (val1.const_value() != 0)
8452 index += val1.const_value() - 1;
8453 break;
8454
8455 case DW_OP_call2:
8456 case DW_OP_call4:
8457 case DW_OP_call_ref:
8458 case DW_OP_nop:
8459 break;
8460
8461 default:
8462 return false;
8463 }
8464
8465 if (ctxt.stack.front().is_const())
8466 ctxt.accum = ctxt.stack.front();
8467
8468 next_index = index + 1;
8469 return true;
8470 }
8471
8472 /// This function quickly evaluates a DWARF expression that is a
8473 /// constant.
8474 ///
8475 /// This is a "fast path" function that quickly evaluates a DWARF
8476 /// expression that is only made of a DW_OP_plus_uconst operator.
8477 ///
8478 /// This is a sub-routine of die_member_offset.
8479 ///
8480 /// @param expr the DWARF expression to evaluate.
8481 ///
8482 /// @param expr_len the length of the expression @p expr.
8483 ///
8484 /// @param value out parameter. This is set to the result of the
8485 /// evaluation of @p expr, iff this function returns true.
8486 ///
8487 /// @return true iff the evaluation of @p expr went OK.
8488 static bool
eval_quickly(Dwarf_Op * expr,uint64_t expr_len,int64_t & value)8489 eval_quickly(Dwarf_Op* expr,
8490 uint64_t expr_len,
8491 int64_t& value)
8492 {
8493 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8494 {
8495 value = expr[0].number;
8496 return true;
8497 }
8498 return false;
8499 }
8500
8501 /// Evaluate the value of the last sub-expression that is a constant,
8502 /// inside a given DWARF expression.
8503 ///
8504 /// @param expr the DWARF expression to consider.
8505 ///
8506 /// @param expr_len the length of the expression to consider.
8507 ///
8508 /// @param value the resulting value of the last constant
8509 /// sub-expression of the DWARF expression. This is set iff the
8510 /// function returns true.
8511 ///
8512 /// @param is_tls_address out parameter. This is set to true iff
8513 /// the resulting value of the evaluation is a TLS (thread local
8514 /// storage) address.
8515 ///
8516 /// @param eval_ctxt the evaluation context to (re)use. Note that
8517 /// this function initializes this context before using it.
8518 ///
8519 /// @return true if the function could find a constant sub-expression
8520 /// to evaluate, false otherwise.
8521 static bool
eval_last_constant_dwarf_sub_expr(Dwarf_Op * expr,size_t expr_len,int64_t & value,bool & is_tls_address,dwarf_expr_eval_context & eval_ctxt)8522 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8523 size_t expr_len,
8524 int64_t& value,
8525 bool& is_tls_address,
8526 dwarf_expr_eval_context &eval_ctxt)
8527 {
8528 // Reset the evaluation context before evaluating the constant sub
8529 // expression contained in the DWARF expression 'expr'.
8530 eval_ctxt.reset();
8531
8532 size_t index = 0, next_index = 0;
8533 do
8534 {
8535 if (op_is_arith_logic(expr, expr_len, index,
8536 next_index, eval_ctxt)
8537 || op_pushes_constant_value(expr, expr_len, index,
8538 next_index, eval_ctxt)
8539 || op_manipulates_stack(expr, expr_len, index,
8540 next_index, eval_ctxt)
8541 || op_pushes_non_constant_value(expr, expr_len, index,
8542 next_index, eval_ctxt)
8543 || op_is_control_flow(expr, expr_len, index,
8544 next_index, eval_ctxt))
8545 ;
8546 else
8547 next_index = index + 1;
8548
8549 ABG_ASSERT(next_index > index);
8550 index = next_index;
8551 } while (index < expr_len);
8552
8553 is_tls_address = eval_ctxt.set_tls_address();
8554 if (eval_ctxt.accum.is_const())
8555 {
8556 value = eval_ctxt.accum;
8557 return true;
8558 }
8559 return false;
8560 }
8561
8562 /// Evaluate the value of the last sub-expression that is a constant,
8563 /// inside a given DWARF expression.
8564 ///
8565 /// @param expr the DWARF expression to consider.
8566 ///
8567 /// @param expr_len the length of the expression to consider.
8568 ///
8569 /// @param value the resulting value of the last constant
8570 /// sub-expression of the DWARF expression. This is set iff the
8571 /// function returns true.
8572 ///
8573 /// @return true if the function could find a constant sub-expression
8574 /// to evaluate, false otherwise.
8575 static bool
eval_last_constant_dwarf_sub_expr(Dwarf_Op * expr,size_t expr_len,int64_t & value,bool & is_tls_address)8576 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8577 size_t expr_len,
8578 int64_t& value,
8579 bool& is_tls_address)
8580 {
8581 dwarf_expr_eval_context eval_ctxt;
8582 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8583 is_tls_address, eval_ctxt);
8584 }
8585
8586 // -----------------------------------
8587 // </location expression evaluation>
8588 // -----------------------------------
8589
8590 /// Convert a DW_AT_bit_offset attribute value into the same value as
8591 /// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8592 ///
8593 /// On big endian machines, the value of the DW_AT_bit_offset
8594 /// attribute + 8 * the value of the DW_AT_data_member_location
8595 /// attribute is the same as the value of the DW_AT_data_bit_offset
8596 /// attribute.
8597 ///
8598 /// On little endian machines however, the situation is different.
8599 /// The DW_AT_bit_offset value for a bit field is the number of bits
8600 /// to the left of the most significant bit of the bit field, within
8601 /// the integer value at DW_AT_data_member_location.
8602 ///
8603 /// The DW_AT_data_bit_offset offset value is the number of bits to
8604 /// the right of the least significant bit of the bit field, again
8605 /// relative to the containing integer value.
8606 ///
8607 /// In other words, DW_AT_data_bit_offset is what everybody would
8608 /// instinctively think of as being the "offset of the bit field". 8 *
8609 /// DW_AT_data_member_location + DW_AT_bit_offset however is very
8610 /// counter-intuitive on little endian machines.
8611 ///
8612 /// This function thus reads the value of a DW_AT_bit_offset property
8613 /// of a DIE and converts it into what the DW_AT_data_bit_offset would
8614 /// have been if it was present, ignoring the contribution of
8615 /// DW_AT_data_member_location.
8616 ///
8617 /// Note that DW_AT_bit_offset has been made obsolete starting from
8618 /// DWARF5 (for GCC; Clang still emits it).
8619 ///
8620 /// If you like coffee and it's not too late, now might be a good time
8621 /// to have a coffee break. Otherwise if it's late at night, you
8622 /// might want to consider an herbal tea break. Then come back to
8623 /// read this.
8624 ///
8625 ///
8626 /// In what follows, the bit fields are all contained within the first
8627 /// whole int of the struct, so DW_AT_data_member_location is 0.
8628 ///
8629 /// Okay, to have a better idea of what DW_AT_bit_offset and
8630 /// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8631 /// have bit fields data members defined as:
8632 ///
8633 /// struct S
8634 /// {
8635 /// int j:5;
8636 /// int k:6;
8637 /// int m:5;
8638 /// int n:8;
8639 /// };
8640 ///
8641 /// The below wonderful (at least!) ASCII art sketch describes the
8642 /// layout of the bitfields of 'struct S' on a little endian machine.
8643 /// You need to read the sketch from the bottom-up.
8644 ///
8645 /// So please scroll down to its bottom. Note how the 32 bits integer
8646 /// word containing the bit fields is laid out with its least
8647 /// significant bit starting on the right hand side, at index 0.
8648 ///
8649 /// Then slowly scroll up starting from there, and take the time to
8650 /// read each line and see how the bit fields are laid out and what
8651 /// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8652 /// the bit fields.
8653 ///
8654 /// DW_AT_bit_offset(n)
8655 /// < - - - - - - >
8656 /// | | n |
8657 /// ^ ^< - - - - >^
8658 /// DW_AT_data_bit_offset(n)
8659 /// < - - - - - - - - - - - - - - - >
8660 /// | |
8661 /// ^ ^
8662 /// DW_AT_bit_offset(m)
8663 /// <--------------------------------->
8664 /// | | m |
8665 /// ^ ^< - >^
8666 /// DW_AT_data_bit_offset(m)
8667 /// < - - - - - - - - - - >
8668 /// | |
8669 /// ^ ^
8670 /// DW_AT_bit_offset(k)
8671 /// <-------------------------------------------->
8672 /// | | k |
8673 /// ^ ^< - - >^
8674 /// DW_AT_data_bit_offset(k)
8675 /// < - - - - >
8676 /// | |
8677 /// ^ ^
8678 /// DW_AT_bit_offset(j)
8679 /// <-------------------------------------------------------->
8680 /// | |
8681 /// ^ ^
8682 /// n m k j
8683 /// < - - - - - - > < - - - > < - - - - > < - - - >
8684 ///
8685 /// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8686 /// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8687 /// 31 27 23 16 15 11 10 6 5 4 0
8688 ///
8689 /// So, the different bit fields all fit in one 32 bits word, assuming
8690 /// the bit fields are tightly packed.
8691 ///
8692 /// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8693 /// on this little endian machine and let's see how it relates to
8694 /// DW_AT_data_bit_offset of j.
8695 ///
8696 /// DW_AT_bit_offset(j) would be equal to the number of bits from the
8697 /// left of the 32 bits word (i.e from bit number 31) to the most
8698 /// significant bit of the j bit field (i.e, bit number 4). Thus:
8699 ///
8700 /// DW_AT_bit_offset(j) =
8701 /// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8702 ///
8703 /// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8704 /// 32 bits word (i.e, bit number 0) to the lest significant bit of
8705 /// the 'j' bit field (ie, bit number 0). Thus:
8706 ///
8707 /// DW_AT_data_bit_offset(j) = 0.
8708 ///
8709 /// More generally, we can notice that:
8710 ///
8711 /// sizeof_in_bits(int) =
8712 /// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8713 ///
8714 /// It follows that:
8715 ///
8716 /// DW_AT_data_bit_offset(j) =
8717 /// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8718 ///
8719 /// Thus:
8720 ///
8721 /// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8722 ///
8723 /// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8724 /// from the right hand side of the word. It is what we would
8725 /// intuitively think it is. DW_AT_bit_offset however is super
8726 /// counter-intuitive, pfff.
8727 ///
8728 /// Anyway, this general equation holds true for all bit fields.
8729 ///
8730 /// Similarly, it follows that:
8731 ///
8732 /// DW_AT_bit_offset(k) =
8733 /// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8734 ///
8735 /// Thus:
8736 /// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8737 ///
8738 ///
8739 /// Likewise:
8740 ///
8741 /// DW_AT_bit_offset(m) =
8742 /// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8743 ///
8744 ///
8745 /// Thus:
8746 /// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8747 ///
8748 /// And:
8749 ///
8750 ///
8751 /// Lastly:
8752 ///
8753 /// DW_AT_bit_offset(n) =
8754 /// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8755 ///
8756 /// Thus:
8757 /// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8758 ///
8759 /// Luckily, the body of the function is much smaller than this
8760 /// comment. Enjoy!
8761 ///
8762 /// @param die the DIE to consider.
8763 ///
8764 /// @param is_big_endian this is true iff the machine we are looking at
8765 /// is big endian.
8766 ///
8767 /// @param offset this is the output parameter into which the value of
8768 /// the DW_AT_bit_offset is put, converted as if it was the value of
8769 /// the DW_AT_data_bit_offset parameter, less the contribution of
8770 /// DW_AT_data_member_location. This parameter is set iff the
8771 /// function returns true.
8772 ///
8773 /// @return true if DW_AT_bit_offset was found on @p die.
8774 static bool
read_and_convert_DW_at_bit_offset(const Dwarf_Die * die,bool is_big_endian,uint64_t & offset)8775 read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8776 bool is_big_endian,
8777 uint64_t &offset)
8778 {
8779 uint64_t off = 0;
8780 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8781 return false;
8782
8783 if (is_big_endian)
8784 {
8785 offset = off;
8786 return true;
8787 }
8788
8789 // Okay, we are looking at a little endian machine. We need to
8790 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8791 // have been. To understand this, you really need to read the
8792 // preliminary comment of this function.
8793 uint64_t containing_anonymous_object_size = 0;
8794 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8795 containing_anonymous_object_size));
8796 containing_anonymous_object_size *= 8;
8797
8798 uint64_t bitfield_size = 0;
8799 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8800 bitfield_size));
8801
8802 // As noted in the the preliminary comment of this function if we
8803 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8804 // its DW_AT_bit_offset value, the equation is:
8805 //
8806 // DW_AT_data_bit_offset(k) =
8807 // sizeof_in_bits(containing_anonymous_object_size)
8808 // - DW_AT_data_bit_offset(k)
8809 // - sizeof_in_bits(k)
8810 offset = containing_anonymous_object_size - off - bitfield_size;
8811
8812 return true;
8813 }
8814
8815 /// Get the value of the DW_AT_data_member_location of the given DIE
8816 /// attribute as an constant.
8817 ///
8818 /// @param die the DIE to read the attribute from.
8819 ///
8820 /// @param offset the attribute as a constant value. This is set iff
8821 /// the function returns true.
8822 ///
8823 /// @return true if the attribute exists and has a constant value. In
8824 /// that case the offset is set to the value.
8825 static bool
die_constant_data_member_location(const Dwarf_Die * die,int64_t & offset)8826 die_constant_data_member_location(const Dwarf_Die *die,
8827 int64_t& offset)
8828 {
8829 if (!die)
8830 return false;
8831
8832 Dwarf_Attribute attr;
8833 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8834 DW_AT_data_member_location,
8835 &attr))
8836 return false;
8837
8838 Dwarf_Word val;
8839 if (dwarf_formudata(&attr, &val) != 0)
8840 return false;
8841
8842 offset = val;
8843 return true;
8844 }
8845
8846 /// Get the offset of a struct/class member as represented by the
8847 /// value of the DW_AT_data_member_location attribute.
8848 ///
8849 /// There is a huge gotcha in here. The value of the
8850 /// DW_AT_data_member_location is not necessarily a constant that one
8851 /// would just read and be done with it. Rather, it can be a DWARF
8852 /// expression that one has to interpret. In general, the offset can
8853 /// be given by the DW_AT_data_bit_offset or by the
8854 /// DW_AT_data_member_location attribute and optionally the
8855 /// DW_AT_bit_offset attribute. The bit offset attributes are
8856 /// always simple constants, but the DW_AT_data_member_location
8857 /// attribute is a DWARF location expression.
8858 ///
8859 /// When it's the DW_AT_data_member_location that is present,
8860 /// there are three cases to possibly take into account:
8861 ///
8862 /// 1/ The offset in the vtable where the offset of a virtual base
8863 /// can be found, aka vptr offset. Given the address of a
8864 /// given object O, the vptr offset for B is given by the
8865 /// (DWARF) expression:
8866 ///
8867 /// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8868 ///
8869 /// where VIRTUAL_OFFSET is a constant value; In this case,
8870 /// this function returns the constant VIRTUAL_OFFSET, as this
8871 /// is enough to detect changes in a given virtual base
8872 /// relative to the other virtual bases.
8873 ///
8874 /// 2/ The offset of a regular data member. Given the address of
8875 /// a struct object named O, the memory location for a
8876 /// particular data member is given by the (DWARF) expression:
8877 ///
8878 /// address(O) + OFFSET
8879 ///
8880 /// where OFFSET is a constant. In this case, this function
8881 /// returns the OFFSET constant.
8882 ///
8883 /// 3/ The offset of a virtual member function in the virtual
8884 /// pointer. The DWARF expression is a constant that designates
8885 /// the offset of the function in the vtable. In this case this
8886 /// function returns that constant.
8887 ///
8888 /// @param rdr the DWARF reader to consider.
8889 ///
8890 /// @param die the DIE to read the information from.
8891 ///
8892 /// @param offset the resulting constant offset, in bits. This
8893 /// argument is set iff the function returns true.
8894 static bool
die_member_offset(const reader & rdr,const Dwarf_Die * die,int64_t & offset)8895 die_member_offset(const reader& rdr,
8896 const Dwarf_Die* die,
8897 int64_t& offset)
8898 {
8899 Dwarf_Op* expr = NULL;
8900 size_t expr_len = 0;
8901 uint64_t bit_offset = 0;
8902
8903 // First let's see if the DW_AT_data_bit_offset attribute is
8904 // present.
8905 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8906 {
8907 offset = bit_offset;
8908 return true;
8909 }
8910
8911 // First try to read DW_AT_data_member_location as a plain constant.
8912 // We do this because the generic method using die_location_expr
8913 // might hit a bug in elfutils libdw dwarf_location_expression only
8914 // fixed in elfutils 0.184+. The bug only triggers if the attribute
8915 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8916 // handle all constants here because that is more consistent (and
8917 // slightly faster in the general case where the attribute isn't a
8918 // full DWARF expression).
8919 if (!die_constant_data_member_location(die, offset))
8920 {
8921 // Otherwise, let's see if the DW_AT_data_member_location
8922 // attribute and, optionally, the DW_AT_bit_offset attributes
8923 // are present.
8924 if (!die_location_expr(die, DW_AT_data_member_location,
8925 &expr, &expr_len))
8926 return false;
8927
8928 // The DW_AT_data_member_location attribute is present. Let's
8929 // evaluate it and get its constant sub-expression and return
8930 // that one.
8931 if (!eval_quickly(expr, expr_len, offset))
8932 {
8933 bool is_tls_address = false;
8934 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8935 offset, is_tls_address,
8936 rdr.dwarf_expr_eval_ctxt()))
8937 return false;
8938 }
8939 }
8940 offset *= 8;
8941
8942 // On little endian machines, we need to convert the
8943 // DW_AT_bit_offset attribute into a relative offset to 8 *
8944 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8945 // would be if it were used instead.
8946 //
8947 // In other words, before adding it to 8 *
8948 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
8949 // converted into a human-understandable form that represents the
8950 // offset of the bitfield data member it describes. For details
8951 // about the conversion, please read the extensive comments of
8952 // read_and_convert_DW_at_bit_offset.
8953 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
8954 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
8955 offset += bit_offset;
8956
8957 return true;
8958 }
8959
8960 /// Read the value of the DW_AT_location attribute from a DIE,
8961 /// evaluate the resulting DWARF expression and, if it's a constant
8962 /// expression, return it.
8963 ///
8964 /// @param die the DIE to consider.
8965 ///
8966 /// @param address the resulting constant address. This is set iff
8967 /// the function returns true.
8968 ///
8969 /// @return true iff the whole sequence of action described above
8970 /// could be completed normally.
8971 static bool
die_location_address(Dwarf_Die * die,Dwarf_Addr & address,bool & is_tls_address)8972 die_location_address(Dwarf_Die* die,
8973 Dwarf_Addr& address,
8974 bool& is_tls_address)
8975 {
8976 Dwarf_Op* expr = NULL;
8977 size_t expr_len = 0;
8978
8979 is_tls_address = false;
8980
8981 if (!die)
8982 return false;
8983
8984 Dwarf_Attribute attr;
8985 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
8986 return false;
8987
8988 if (dwarf_getlocation(&attr, &expr, &expr_len))
8989 return false;
8990 // Ignore location expressions where reading them succeeded but
8991 // their length is 0.
8992 if (expr_len == 0)
8993 return false;
8994
8995 Dwarf_Attribute result;
8996 if (!dwarf_getlocation_attr(&attr, expr, &result))
8997 // A location that has been interpreted as an address.
8998 return !dwarf_formaddr(&result, &address);
8999
9000 // Just get the address out of the number field.
9001 address = expr->number;
9002 return true;
9003 }
9004
9005 /// Return the index of a function in its virtual table. That is,
9006 /// return the value of the DW_AT_vtable_elem_location attribute.
9007 ///
9008 /// @param die the DIE of the function to consider.
9009 ///
9010 /// @param vindex the resulting index. This is set iff the function
9011 /// returns true.
9012 ///
9013 /// @return true if the DIE has a DW_AT_vtable_elem_location
9014 /// attribute.
9015 static bool
die_virtual_function_index(Dwarf_Die * die,int64_t & vindex)9016 die_virtual_function_index(Dwarf_Die* die,
9017 int64_t& vindex)
9018 {
9019 if (!die)
9020 return false;
9021
9022 Dwarf_Op* expr = NULL;
9023 size_t expr_len = 0;
9024 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9025 &expr, &expr_len))
9026 return false;
9027
9028 int64_t i = 0;
9029 bool is_tls_addr = false;
9030 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9031 return false;
9032
9033 vindex = i;
9034 return true;
9035 }
9036
9037 /// Test if a given DIE represents an anonymous type.
9038 ///
9039 /// Anonymous types we are interested in are classes, unions and
9040 /// enumerations.
9041 ///
9042 /// @param die the DIE to consider.
9043 ///
9044 /// @return true iff @p die represents an anonymous type.
9045 bool
is_anonymous_type_die(Dwarf_Die * die)9046 is_anonymous_type_die(Dwarf_Die *die)
9047 {
9048 int tag = dwarf_tag(die);
9049
9050 if (tag == DW_TAG_class_type
9051 || tag == DW_TAG_structure_type
9052 || tag == DW_TAG_union_type
9053 || tag == DW_TAG_enumeration_type)
9054 return die_is_anonymous(die);
9055
9056 return false;
9057 }
9058
9059 /// Return the base of the internal name to represent an anonymous
9060 /// type.
9061 ///
9062 /// Typically, anonymous enums would be named
9063 /// __anonymous_enum__<number>, anonymous struct or classes would be
9064 /// named __anonymous_struct__<number> and anonymous unions would be
9065 /// named __anonymous_union__<number>. The first part of these
9066 /// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9067 /// the base name. This function returns that base name, depending on
9068 /// the kind of type DIE we are looking at.
9069 ///
9070 /// @param die the type DIE to look at. This function expects a type
9071 /// DIE with an empty DW_AT_name property value (anonymous).
9072 ///
9073 /// @return a string representing the base of the internal anonymous
9074 /// name.
9075 static string
get_internal_anonymous_die_prefix_name(const Dwarf_Die * die)9076 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9077 {
9078 ABG_ASSERT(die_is_type(die));
9079 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9080
9081 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9082 string type_name;
9083 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9084 type_name = tools_utils::get_anonymous_struct_internal_name_prefix();
9085 else if (tag == DW_TAG_union_type)
9086 type_name = tools_utils::get_anonymous_union_internal_name_prefix();
9087 else if (tag == DW_TAG_enumeration_type)
9088 type_name = tools_utils::get_anonymous_enum_internal_name_prefix();
9089
9090 return type_name;
9091 }
9092
9093 /// Build a full internal anonymous type name.
9094 ///
9095 /// @param base_name this is the base name as returned by the function
9096 /// @ref get_internal_anonymous_die_prefix_name.
9097 ///
9098 /// @param anonymous_type_index this is the index of the anonymous
9099 /// type in its scope. That is, if there are more than one anonymous
9100 /// types of a given kind in a scope, this index is what tells them
9101 /// appart, starting from 0.
9102 ///
9103 /// @return the built string, which is a concatenation of @p base_name
9104 /// and @p anonymous_type_index.
9105 static string
build_internal_anonymous_die_name(const string & base_name,size_t anonymous_type_index)9106 build_internal_anonymous_die_name(const string &base_name,
9107 size_t anonymous_type_index)
9108 {
9109 string name = base_name;
9110 if (anonymous_type_index && !base_name.empty())
9111 {
9112 std::ostringstream o;
9113 o << base_name << anonymous_type_index;
9114 name = o.str();
9115 }
9116 return name;
9117 }
9118
9119
9120 /// Build a full internal anonymous type name.
9121 ///
9122 /// @param die the DIE representing the anonymous type to consider.
9123 ///
9124 /// @param anonymous_type_index the index of the anonymous type
9125 /// represented by @p DIE, in its scope. That is, if there are
9126 /// several different anonymous types of the same kind as @p die, this
9127 /// index is what tells them appart.
9128 ///
9129 /// @return the internal name of the anonymous type represented by @p
9130 /// DIE.
9131 static string
get_internal_anonymous_die_name(Dwarf_Die * die,size_t anonymous_type_index)9132 get_internal_anonymous_die_name(Dwarf_Die *die,
9133 size_t anonymous_type_index)
9134 {
9135 string name = get_internal_anonymous_die_prefix_name(die);
9136 name = build_internal_anonymous_die_name(name, anonymous_type_index);
9137 return name;
9138 }
9139
9140 // ------------------------------------
9141 // <DIE pretty printer>
9142 // ------------------------------------
9143
9144 /// Compute the qualified name of a DIE that represents a type.
9145 ///
9146 /// For instance, if the DIE tag is DW_TAG_subprogram then this
9147 /// function computes the name of the function *type*.
9148 ///
9149 /// @param rdr the DWARF reader.
9150 ///
9151 /// @param die the DIE to consider.
9152 ///
9153 /// @param where_offset where in the are logically are in the DIE
9154 /// stream.
9155 ///
9156 /// @return a copy of the qualified name of the type.
9157 static string
die_qualified_type_name(const reader & rdr,const Dwarf_Die * die,size_t where_offset)9158 die_qualified_type_name(const reader& rdr,
9159 const Dwarf_Die* die,
9160 size_t where_offset)
9161 {
9162 if (!die)
9163 return "";
9164
9165 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9166 if (tag == DW_TAG_compile_unit
9167 || tag == DW_TAG_partial_unit
9168 || tag == DW_TAG_type_unit)
9169 return "";
9170
9171 string name = die_name(die);
9172
9173 Dwarf_Die scope_die;
9174 if (!get_scope_die(rdr, die, where_offset, scope_die))
9175 return "";
9176
9177 string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
9178 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9179 string separator = colon_colon ? "::" : ".";
9180
9181 string repr;
9182
9183 switch (tag)
9184 {
9185 case DW_TAG_unspecified_type:
9186 break;
9187
9188 case DW_TAG_base_type:
9189 {
9190 abigail::ir::integral_type int_type;
9191 if (parse_integral_type(name, int_type))
9192 repr = int_type;
9193 else
9194 repr = name;
9195 }
9196 break;
9197
9198 case DW_TAG_typedef:
9199 case DW_TAG_enumeration_type:
9200 case DW_TAG_structure_type:
9201 case DW_TAG_class_type:
9202 case DW_TAG_union_type:
9203 {
9204 if (name.empty())
9205 // TODO: handle cases where there are more than one
9206 // anonymous type of the same kind in the same scope. In
9207 // that case, their name must be built with the function
9208 // get_internal_anonymous_die_name or something of the same
9209 // kind.
9210 name = get_internal_anonymous_die_prefix_name(die);
9211
9212 ABG_ASSERT(!name.empty());
9213 repr = parent_name.empty() ? name : parent_name + separator + name;
9214 }
9215 break;
9216
9217 case DW_TAG_const_type:
9218 case DW_TAG_volatile_type:
9219 case DW_TAG_restrict_type:
9220 {
9221 Dwarf_Die underlying_type_die;
9222 bool has_underlying_type_die =
9223 die_die_attribute(die, DW_AT_type, underlying_type_die);
9224
9225 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9226 break;
9227
9228 if (tag == DW_TAG_const_type)
9229 {
9230 if (has_underlying_type_die
9231 && die_is_reference_type(&underlying_type_die))
9232 // A reference is always const. So, to lower false
9233 // positive reports in diff computations, we consider a
9234 // const reference just as a reference. But we need to
9235 // keep the qualified-ness of the type. So we introduce
9236 // a 'no-op' qualifier here. Please remember that this
9237 // has to be kept in sync with what is done in
9238 // get_name_of_qualified_type. So if you change this
9239 // here, you have to change that code there too.
9240 repr = "";
9241 else if (!has_underlying_type_die
9242 || die_is_void_type(&underlying_type_die))
9243 {
9244 repr = "void";
9245 break;
9246 }
9247 else
9248 repr = "const";
9249 }
9250 else if (tag == DW_TAG_volatile_type)
9251 repr = "volatile";
9252 else if (tag == DW_TAG_restrict_type)
9253 repr = "restrict";
9254 else
9255 ABG_ASSERT_NOT_REACHED;
9256
9257 string underlying_type_repr;
9258 if (has_underlying_type_die)
9259 underlying_type_repr =
9260 die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9261 else
9262 underlying_type_repr = "void";
9263
9264 if (underlying_type_repr.empty())
9265 repr.clear();
9266 else
9267 {
9268 if (has_underlying_type_die)
9269 {
9270 Dwarf_Die peeled;
9271 die_peel_qualified(&underlying_type_die, peeled);
9272 if (die_is_pointer_or_reference_type(&peeled))
9273 repr = underlying_type_repr + " " + repr;
9274 else
9275 repr += " " + underlying_type_repr;
9276 }
9277 else
9278 repr += " " + underlying_type_repr;
9279 }
9280 }
9281 break;
9282
9283 case DW_TAG_pointer_type:
9284 case DW_TAG_reference_type:
9285 case DW_TAG_rvalue_reference_type:
9286 {
9287 Dwarf_Die pointed_to_type_die;
9288 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9289 {
9290 if (tag == DW_TAG_pointer_type)
9291 repr = "void*";
9292 break;
9293 }
9294
9295 if (die_is_unspecified(&pointed_to_type_die))
9296 break;
9297
9298 string pointed_type_repr =
9299 die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9300
9301 repr = pointed_type_repr;
9302 if (repr.empty())
9303 break;
9304
9305 if (tag == DW_TAG_pointer_type)
9306 repr += "*";
9307 else if (tag == DW_TAG_reference_type)
9308 repr += "&";
9309 else if (tag == DW_TAG_rvalue_reference_type)
9310 repr += "&&";
9311 else
9312 ABG_ASSERT_NOT_REACHED;
9313 }
9314 break;
9315
9316 case DW_TAG_subrange_type:
9317 {
9318 // In Ada, this one can be generated on its own, that is, not
9319 // as a sub-type of an array. So we need to support it on its
9320 // own. Note that when it's emitted as the sub-type of an
9321 // array like in C and C++, this is handled differently, for
9322 // now. But we try to make this usable by other languages
9323 // that are not Ada, even if we modelled it after Ada.
9324
9325 // So we build a subrange type for the sole purpose of using
9326 // the ::as_string() method of that type. So we don't add
9327 // that type to the current type tree being built.
9328 array_type_def::subrange_sptr s =
9329 build_subrange_type(const_cast<reader&>(rdr),
9330 die, where_offset,
9331 /*associate_die_to_type=*/false);
9332 repr += s->as_string();
9333 break;
9334 }
9335
9336 case DW_TAG_array_type:
9337 {
9338 Dwarf_Die element_type_die;
9339 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9340 break;
9341 string element_type_name =
9342 die_qualified_type_name(rdr, &element_type_die, where_offset);
9343 if (element_type_name.empty())
9344 break;
9345
9346 array_type_def::subranges_type subranges;
9347 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9348 die, subranges, where_offset,
9349 /*associate_type_to_die=*/false);
9350
9351 repr = element_type_name;
9352 repr += array_type_def::subrange_type::vector_as_string(subranges);
9353 }
9354 break;
9355
9356 case DW_TAG_subroutine_type:
9357 case DW_TAG_subprogram:
9358 {
9359 string return_type_name;
9360 string class_name;
9361 vector<string> parm_names;
9362 bool is_const = false;
9363 bool is_static = false;
9364
9365 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9366 /*pretty_print=*/true,
9367 return_type_name, class_name,
9368 parm_names, is_const,
9369 is_static);
9370 if (return_type_name.empty())
9371 return_type_name = "void";
9372
9373 repr = return_type_name;
9374
9375 if (!class_name.empty())
9376 {
9377 // This is a method, so print the class name.
9378 repr += " (" + class_name + "::*)";
9379 }
9380
9381 // Now parameters.
9382 repr += " (";
9383 for (vector<string>::const_iterator i = parm_names.begin();
9384 i != parm_names.end();
9385 ++i)
9386 {
9387 if (i != parm_names.begin())
9388 repr += ", ";
9389 repr += *i;
9390 }
9391 repr += ")";
9392
9393 }
9394 break;
9395
9396 case DW_TAG_string_type:
9397 case DW_TAG_ptr_to_member_type:
9398 case DW_TAG_set_type:
9399 case DW_TAG_file_type:
9400 case DW_TAG_packed_type:
9401 case DW_TAG_thrown_type:
9402 case DW_TAG_interface_type:
9403 case DW_TAG_shared_type:
9404 break;
9405 }
9406
9407 return repr;
9408 }
9409
9410 /// Compute the qualified name of a decl represented by a given DIE.
9411 ///
9412 /// For instance, for a DIE of tag DW_TAG_subprogram this function
9413 /// computes the signature of the function *declaration*.
9414 ///
9415 /// @param rdr the DWARF reader.
9416 ///
9417 /// @param die the DIE to consider.
9418 ///
9419 /// @param where_offset where we are logically at in the DIE stream.
9420 ///
9421 /// @return a copy of the computed name.
9422 static string
die_qualified_decl_name(const reader & rdr,const Dwarf_Die * die,size_t where_offset)9423 die_qualified_decl_name(const reader& rdr,
9424 const Dwarf_Die* die,
9425 size_t where_offset)
9426 {
9427 if (!die || !die_is_decl(die))
9428 return "";
9429
9430 string name = die_name(die);
9431
9432 Dwarf_Die scope_die;
9433 if (!get_scope_die(rdr, die, where_offset, scope_die))
9434 return "";
9435
9436 string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9437 string separator = "::";
9438
9439 string repr;
9440
9441 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9442 switch (tag)
9443 {
9444 case DW_TAG_namespace:
9445 case DW_TAG_member:
9446 case DW_TAG_variable:
9447 repr = scope_name.empty() ? name : scope_name + separator + name;
9448 break;
9449 case DW_TAG_subprogram:
9450 repr = die_function_signature(rdr, die, where_offset);
9451 break;
9452
9453 case DW_TAG_unspecified_parameters:
9454 repr = "...";
9455 break;
9456
9457 case DW_TAG_formal_parameter:
9458 case DW_TAG_imported_declaration:
9459 case DW_TAG_GNU_template_template_param:
9460 case DW_TAG_GNU_template_parameter_pack:
9461 case DW_TAG_GNU_formal_parameter_pack:
9462 break;
9463 }
9464 return repr;
9465 }
9466
9467 /// Compute the qualified name of the artifact represented by a given
9468 /// DIE.
9469 ///
9470 /// If the DIE represents a type, then the function computes the name
9471 /// of the type. Otherwise, if the DIE represents a decl then the
9472 /// function computes the name of the decl. Note that a DIE of tag
9473 /// DW_TAG_subprogram is going to be considered as a "type" -- just
9474 /// like if it was a DW_TAG_subroutine_type.
9475 ///
9476 /// @param rdr the DWARF reader.
9477 ///
9478 /// @param die the DIE to consider.
9479 ///
9480 /// @param where_offset where we are logically at in the DIE stream.
9481 ///
9482 /// @return a copy of the computed name.
9483 static string
die_qualified_name(const reader & rdr,const Dwarf_Die * die,size_t where)9484 die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9485 {
9486 if (die_is_type(die))
9487 return die_qualified_type_name(rdr, die, where);
9488 else if (die_is_decl(die))
9489 return die_qualified_decl_name(rdr, die, where);
9490 return "";
9491 }
9492
9493 /// Test if the qualified name of a given type should be empty.
9494 ///
9495 /// The reason why the name of a DIE with a given tag would be empty
9496 /// is that libabigail's internal representation doesn't yet support
9497 /// that tag; or if the DIE's qualified name is built from names of
9498 /// sub-types DIEs whose tags are not yet supported.
9499 ///
9500 /// @param rdr the DWARF reader.
9501 ///
9502 /// @param die the DIE to consider.
9503 ///
9504 /// @param where where we are logically at, in the DIE stream.
9505 ///
9506 /// @param qualified_name the qualified name of the DIE. This is set
9507 /// only iff the function returns false.
9508 ///
9509 /// @return true if the qualified name of the DIE is empty.
9510 static bool
die_qualified_type_name_empty(const reader & rdr,const Dwarf_Die * die,size_t where,string & qualified_name)9511 die_qualified_type_name_empty(const reader& rdr,
9512 const Dwarf_Die* die,
9513 size_t where, string &qualified_name)
9514 {
9515 if (!die)
9516 return true;
9517
9518 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9519
9520 string qname;
9521 if (tag == DW_TAG_typedef
9522 || tag == DW_TAG_pointer_type
9523 || tag == DW_TAG_reference_type
9524 || tag == DW_TAG_rvalue_reference_type
9525 || tag == DW_TAG_array_type
9526 || tag == DW_TAG_const_type
9527 || tag == DW_TAG_volatile_type
9528 || tag == DW_TAG_restrict_type)
9529 {
9530 Dwarf_Die underlying_type_die;
9531 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9532 {
9533 string name =
9534 die_qualified_type_name(rdr, &underlying_type_die, where);
9535 if (name.empty())
9536 return true;
9537 }
9538 }
9539 else
9540 {
9541 string name = die_qualified_type_name(rdr, die, where);
9542 if (name.empty())
9543 return true;
9544 }
9545
9546 qname = die_qualified_type_name(rdr, die, where);
9547 if (qname.empty())
9548 return true;
9549
9550 qualified_name = qname;
9551 return false;
9552 }
9553
9554 /// Given the DIE that represents a function type, compute the names
9555 /// of the following properties the function's type:
9556 ///
9557 /// - return type
9558 /// - enclosing class (if the function is a member function)
9559 /// - function parameter types
9560 ///
9561 /// When the function we are looking at is a member function, it also
9562 /// tells if it's const.
9563 ///
9564 /// @param rdr the DWARF reader.
9565 ///
9566 /// @param die the DIE of the function or function type we are looking
9567 /// at.
9568 ///
9569 /// @param where_offset where we are logically at in the DIE stream.
9570 ///
9571 /// @param pretty_print if set to yes, the type names are going to be
9572 /// pretty-printed names; otherwise, they are just qualified type
9573 /// names.
9574 ///
9575 /// @param return_type_name out parameter. This contains the name of
9576 /// the return type of the function.
9577 ///
9578 /// @param class_name out parameter. If the function is a member
9579 /// function, this contains the name of the enclosing class.
9580 ///
9581 /// @param parm_names out parameter. This vector is set to the names
9582 /// of the types of the parameters of the function.
9583 ///
9584 /// @param is_const out parameter. If the function is a member
9585 /// function, this is set to true iff the member function is const.
9586 ///
9587 /// @param is_static out parameter. If the function is a static
9588 /// member function, then this is set to true.
9589 static void
die_return_and_parm_names_from_fn_type_die(const reader & rdr,const Dwarf_Die * die,size_t where_offset,bool pretty_print,string & return_type_name,string & class_name,vector<string> & parm_names,bool & is_const,bool & is_static)9590 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9591 const Dwarf_Die* die,
9592 size_t where_offset,
9593 bool pretty_print,
9594 string &return_type_name,
9595 string &class_name,
9596 vector<string>& parm_names,
9597 bool& is_const,
9598 bool& is_static)
9599 {
9600 Dwarf_Die child;
9601 Dwarf_Die ret_type_die;
9602 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9603 return_type_name = "void";
9604 else
9605 return_type_name =
9606 pretty_print
9607 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9608 : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9609
9610 if (return_type_name.empty())
9611 return_type_name = "void";
9612
9613 Dwarf_Die object_pointer_die, class_die;
9614 bool is_method_type =
9615 die_function_type_is_method_type(rdr, die, where_offset,
9616 object_pointer_die,
9617 class_die, is_static);
9618
9619 is_const = false;
9620 if (is_method_type)
9621 {
9622 class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9623
9624 Dwarf_Die this_pointer_die;
9625 Dwarf_Die pointed_to_die;
9626 if (!is_static
9627 && die_die_attribute(&object_pointer_die, DW_AT_type,
9628 this_pointer_die))
9629 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9630 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9631 is_const = true;
9632
9633 string fn_name = die_name(die);
9634 string non_qualified_class_name = die_name(&class_die);
9635 bool is_ctor = fn_name == non_qualified_class_name;
9636 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9637
9638 if (is_ctor || is_dtor)
9639 return_type_name.clear();
9640 }
9641
9642 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9643 do
9644 {
9645 int child_tag = dwarf_tag(&child);
9646 if (child_tag == DW_TAG_formal_parameter)
9647 {
9648 Dwarf_Die parm_type_die;
9649 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9650 continue;
9651 string qualified_name =
9652 pretty_print
9653 ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
9654 : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
9655
9656 if (qualified_name.empty())
9657 continue;
9658 parm_names.push_back(qualified_name);
9659 }
9660 else if (child_tag == DW_TAG_unspecified_parameters)
9661 {
9662 // This is a variadic function parameter.
9663 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
9664 // After a DW_TAG_unspecified_parameters tag, we shouldn't
9665 // keep reading for parameters. The
9666 // unspecified_parameters TAG should be the last parameter
9667 // that we record. For instance, if there are multiple
9668 // DW_TAG_unspecified_parameters DIEs then we should care
9669 // only for the first one.
9670 break;
9671 }
9672 }
9673 while (dwarf_siblingof(&child, &child) == 0);
9674
9675 if (class_name.empty())
9676 {
9677 Dwarf_Die parent_die;
9678 if (get_parent_die(rdr, die, parent_die, where_offset))
9679 {
9680 if (die_is_class_type(&parent_die))
9681 class_name =
9682 rdr.get_die_qualified_type_name(&parent_die, where_offset);
9683 }
9684 }
9685 }
9686
9687 /// This computes the signature of the a function declaration
9688 /// represented by a DIE.
9689 ///
9690 /// @param rdr the DWARF reader.
9691 ///
9692 /// @param fn_die the DIE of the function to consider.
9693 ///
9694 /// @param where_offset where we are logically at in the stream of
9695 /// DIEs.
9696 ///
9697 /// @return a copy of the computed function signature string.
9698 static string
die_function_signature(const reader & rdr,const Dwarf_Die * fn_die,size_t where_offset)9699 die_function_signature(const reader& rdr,
9700 const Dwarf_Die *fn_die,
9701 size_t where_offset)
9702 {
9703
9704 translation_unit::language lang;
9705 bool has_lang = false;
9706 if ((has_lang = rdr.get_die_language(fn_die, lang)))
9707 {
9708 // In a binary originating from the C language, it's OK to use
9709 // the linkage name of the function as a key for the map which
9710 // is meant to reduce the number of DIE comparisons involved
9711 // during DIE canonicalization computation.
9712 if (is_c_language(lang))
9713 {
9714 string fn_name = die_linkage_name(fn_die);
9715 if (fn_name.empty())
9716 fn_name = die_name(fn_die);
9717 return fn_name;
9718 }
9719 }
9720
9721 // TODO: When we can structurally compare DIEs originating from C++
9722 // as well, we can use the linkage name of functions in C++ too, to
9723 // reduce the number of comparisons involved during DIE
9724 // canonicalization.
9725
9726 string return_type_name;
9727 Dwarf_Die ret_type_die;
9728 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9729 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
9730 where_offset);
9731
9732 if (return_type_name.empty())
9733 return_type_name = "void";
9734
9735 Dwarf_Die scope_die;
9736 string scope_name;
9737 if (get_scope_die(rdr, fn_die, where_offset, scope_die))
9738 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
9739 string fn_name = die_name(fn_die);
9740 if (!scope_name.empty())
9741 fn_name = scope_name + "::" + fn_name;
9742
9743 string class_name;
9744 vector<string> parm_names;
9745 bool is_const = false;
9746 bool is_static = false;
9747
9748 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
9749 /*pretty_print=*/false,
9750 return_type_name, class_name,
9751 parm_names, is_const, is_static);
9752
9753 bool is_virtual = die_is_virtual(fn_die);
9754
9755 string repr = class_name.empty() ? "function" : "method";
9756 if (is_virtual)
9757 repr += " virtual";
9758
9759 if (!return_type_name.empty())
9760 repr += " " + return_type_name;
9761
9762 repr += " " + fn_name;
9763
9764 // Now parameters.
9765 repr += "(";
9766 bool some_parm_emitted = false;
9767 for (vector<string>::const_iterator i = parm_names.begin();
9768 i != parm_names.end();
9769 ++i)
9770 {
9771 if (i != parm_names.begin())
9772 {
9773 if (some_parm_emitted)
9774 repr += ", ";
9775 }
9776 else
9777 if (!is_static && !class_name.empty())
9778 // We are printing a non-static method name, skip the implicit "this"
9779 // parameter type.
9780 continue;
9781 repr += *i;
9782 some_parm_emitted = true;
9783 }
9784 repr += ")";
9785
9786 if (is_const)
9787 {
9788 ABG_ASSERT(!class_name.empty());
9789 repr += " const";
9790 }
9791
9792 return repr;
9793 }
9794
9795 /// Return a pretty string representation of a type, for internal purposes.
9796 ///
9797 /// By internal purpose, we mean things like key-ing types for lookup
9798 /// purposes and so on.
9799 ///
9800 /// Note that this function is also used to pretty print functions.
9801 /// For functions, it prints the *type* of the function.
9802 ///
9803 /// @param rdr the context to use.
9804 ///
9805 /// @param the DIE of the type to pretty print.
9806 ///
9807 /// @param where_offset where we logically are placed when calling
9808 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9809 /// entries.
9810 ///
9811 /// @return the resulting pretty representation.
9812 static string
die_pretty_print_type(reader & rdr,const Dwarf_Die * die,size_t where_offset)9813 die_pretty_print_type(reader& rdr,
9814 const Dwarf_Die* die,
9815 size_t where_offset)
9816 {
9817 if (!die
9818 || (!die_is_type(die)
9819 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9820 return "";
9821
9822 string repr;
9823
9824 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9825 switch (tag)
9826 {
9827 case DW_TAG_string_type:
9828 // For now, we won't try to go get the actual representation of
9829 // the string because this would make things more complicated;
9830 // for that we'd need to interpret some location expressions to
9831 // get the length of the string. And for dynamically allocated
9832 // strings, the result of the location expression evaluation
9833 // might not even be a constant. So at the moment I consider
9834 // this to be a lot of hassle for no great return. Until proven
9835 // otherwise, of course.
9836 repr = "string type";
9837
9838 case DW_TAG_unspecified_type:
9839 case DW_TAG_ptr_to_member_type:
9840 break;
9841
9842 case DW_TAG_namespace:
9843 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
9844 break;
9845
9846 case DW_TAG_base_type:
9847 repr = rdr.get_die_qualified_type_name(die, where_offset);
9848 break;
9849
9850 case DW_TAG_typedef:
9851 {
9852 string qualified_name;
9853 if (!die_qualified_type_name_empty(rdr, die,
9854 where_offset,
9855 qualified_name))
9856 repr = "typedef " + qualified_name;
9857 }
9858 break;
9859
9860 case DW_TAG_const_type:
9861 case DW_TAG_volatile_type:
9862 case DW_TAG_restrict_type:
9863 case DW_TAG_pointer_type:
9864 case DW_TAG_reference_type:
9865 case DW_TAG_rvalue_reference_type:
9866 repr = rdr.get_die_qualified_type_name(die, where_offset);
9867 break;
9868
9869 case DW_TAG_enumeration_type:
9870 {
9871 string qualified_name =
9872 rdr.get_die_qualified_type_name(die, where_offset);
9873 repr = "enum " + qualified_name;
9874 }
9875 break;
9876
9877 case DW_TAG_structure_type:
9878 case DW_TAG_class_type:
9879 {
9880 string qualified_name =
9881 rdr.get_die_qualified_type_name(die, where_offset);
9882 repr = "class " + qualified_name;
9883 }
9884 break;
9885
9886 case DW_TAG_union_type:
9887 {
9888 string qualified_name =
9889 rdr.get_die_qualified_type_name(die, where_offset);
9890 repr = "union " + qualified_name;
9891 }
9892 break;
9893
9894 case DW_TAG_array_type:
9895 {
9896 Dwarf_Die element_type_die;
9897 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9898 break;
9899 string element_type_name =
9900 rdr.get_die_qualified_type_name(&element_type_die, where_offset);
9901 if (element_type_name.empty())
9902 break;
9903
9904 array_type_def::subranges_type subranges;
9905 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
9906 /*associate_type_to_die=*/false);
9907
9908 repr = element_type_name;
9909 repr += array_type_def::subrange_type::vector_as_string(subranges);
9910 }
9911 break;
9912
9913 case DW_TAG_subrange_type:
9914 {
9915 // So this can be generated by Ada, on its own; that is, not
9916 // as a subtype of an array. In that case we need to handle
9917 // it properly.
9918
9919 // For now, we consider that the pretty printed name of the
9920 // subrange type is its name. We might need something more
9921 // advance, should the needs of the users get more
9922 // complicated.
9923 repr += die_qualified_type_name(rdr, die, where_offset);
9924 }
9925 break;
9926
9927 case DW_TAG_subroutine_type:
9928 case DW_TAG_subprogram:
9929 {
9930 string return_type_name;
9931 string class_name;
9932 vector<string> parm_names;
9933 bool is_const = false;
9934 bool is_static = false;
9935
9936 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9937 /*pretty_print=*/true,
9938 return_type_name, class_name,
9939 parm_names, is_const,
9940 is_static);
9941 if (class_name.empty())
9942 repr = "function type";
9943 else
9944 repr = "method type";
9945 repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
9946 }
9947 break;
9948
9949 case DW_TAG_set_type:
9950 case DW_TAG_file_type:
9951 case DW_TAG_packed_type:
9952 case DW_TAG_thrown_type:
9953 case DW_TAG_interface_type:
9954 case DW_TAG_shared_type:
9955 ABG_ASSERT_NOT_REACHED;
9956 }
9957
9958 return repr;
9959 }
9960
9961 /// Return a pretty string representation of a declaration, for
9962 /// internal purposes.
9963 ///
9964 /// By internal purpose, we mean things like key-ing declarations for
9965 /// lookup purposes and so on.
9966 ///
9967 /// Note that this function is also used to pretty print functions.
9968 /// For functions, it prints the signature of the function.
9969 ///
9970 /// @param rdr the context to use.
9971 ///
9972 /// @param the DIE of the declaration to pretty print.
9973 ///
9974 /// @param where_offset where we logically are placed when calling
9975 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9976 /// entries.
9977 ///
9978 /// @return the resulting pretty representation.
9979 static string
die_pretty_print_decl(reader & rdr,const Dwarf_Die * die,size_t where_offset)9980 die_pretty_print_decl(reader& rdr,
9981 const Dwarf_Die* die,
9982 size_t where_offset)
9983 {
9984 if (!die || !die_is_decl(die))
9985 return "";
9986
9987 string repr;
9988
9989 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9990 switch (tag)
9991 {
9992 case DW_TAG_namespace:
9993 repr = "namespace " + die_qualified_name(rdr, die, where_offset);
9994 break;
9995
9996 case DW_TAG_member:
9997 case DW_TAG_variable:
9998 {
9999 string type_repr = "void";
10000 Dwarf_Die type_die;
10001 if (die_die_attribute(die, DW_AT_type, type_die))
10002 type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
10003 repr = die_qualified_name(rdr, die, where_offset);
10004 if (!repr.empty())
10005 repr = type_repr + " " + repr;
10006 }
10007 break;
10008
10009 case DW_TAG_subprogram:
10010 repr = die_function_signature(rdr, die, where_offset);
10011 break;
10012
10013 default:
10014 break;
10015 }
10016 return repr;
10017 }
10018
10019 /// Compute the pretty printed representation of an artifact
10020 /// represented by a DIE.
10021 ///
10022 /// If the DIE is a type, compute the its pretty representation as a
10023 /// type; otherwise, if it's a declaration, compute its pretty
10024 /// representation as a declaration. Note for For instance, that a
10025 /// DW_TAG_subprogram DIE is going to be represented as a function
10026 /// *type*.
10027 ///
10028 /// @param rdr the DWARF reader.
10029 ///
10030 /// @param die the DIE to consider.
10031 ///
10032 /// @param where_offset we in the DIE stream we are logically at.
10033 ///
10034 /// @return a copy of the pretty printed artifact.
10035 static string
die_pretty_print(reader & rdr,const Dwarf_Die * die,size_t where_offset)10036 die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
10037 {
10038 if (die_is_type(die))
10039 return die_pretty_print_type(rdr, die, where_offset);
10040 else if (die_is_decl(die))
10041 return die_pretty_print_decl(rdr, die, where_offset);
10042 return "";
10043 }
10044
10045 // -----------------------------------
10046 // </die pretty printer>
10047 // -----------------------------------
10048
10049
10050 // ----------------------------------
10051 // <die comparison engine>
10052 // ---------------------------------
10053
10054 /// Compares two decls DIEs
10055 ///
10056 /// This works only for DIEs emitted by the C language.
10057 ///
10058 /// This implementation doesn't yet support namespaces.
10059 ///
10060 /// This is a subroutine of compare_dies.
10061 ///
10062 /// @return true iff @p l equals @p r.
10063 static bool
compare_as_decl_dies(const Dwarf_Die * l,const Dwarf_Die * r)10064 compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10065 {
10066 ABG_ASSERT(l && r);
10067
10068 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10069 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10070 if (l_tag != r_tag)
10071 return false;
10072
10073 bool result = false;
10074
10075 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
10076 {
10077 // Fast path for functions and global variables.
10078 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
10079 result)
10080 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
10081 result))
10082 {
10083 if (!result)
10084 return false;
10085 }
10086
10087 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10088 result))
10089 {
10090 if (!result)
10091 return false;
10092 }
10093 return true;
10094 }
10095
10096 // Fast path for types.
10097 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10098 result))
10099 return result;
10100 return true;
10101 }
10102
10103 /// Test if at least one of two ODR-relevant DIEs is decl-only.
10104 ///
10105 /// @param rdr the DWARF reader to consider.
10106 ///
10107 /// @param l the first type DIE to consider.
10108 ///
10109 /// @param r the second type DIE to consider.
10110 ///
10111 /// @return true iff either @p l or @p r is decl-only and both are
10112 /// ODR-relevant.
10113 static bool
at_least_one_decl_only_among_odr_relevant_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10114 at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
10115 const Dwarf_Die *l,
10116 const Dwarf_Die *r)
10117 {
10118 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
10119 return false;
10120
10121 if ((die_is_declaration_only(l) && die_has_no_child(l))
10122 || (die_is_declaration_only(r) && die_has_no_child(r)))
10123 return true;
10124 return false;
10125 }
10126
10127 /// Compares two type DIEs
10128 ///
10129 /// This is a subroutine of compare_dies.
10130 ///
10131 /// Note that this function doesn't look at the name of the DIEs.
10132 /// Naming is taken into account by the function compare_as_decl_dies.
10133 ///
10134 /// If the two DIEs are from a translation unit that is subject to the
10135 /// ONE Definition Rule, then the function considers that if one DIE
10136 /// is a declaration, then it's equivalent to the second. In that
10137 /// case, the sizes of the two DIEs are not compared. This is so that
10138 /// a declaration of a type compares equal to the definition of the
10139 /// type.
10140 ///
10141 /// @param rdr the DWARF reader to consider.
10142 ///
10143 /// @param l the left operand of the comparison operator.
10144 ///
10145 /// @param r the right operand of the comparison operator.
10146 ///
10147 /// @return true iff @p l equals @p r.
10148 static bool
compare_as_type_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10149 compare_as_type_dies(const reader& rdr,
10150 const Dwarf_Die *l,
10151 const Dwarf_Die *r)
10152 {
10153 ABG_ASSERT(l && r);
10154 ABG_ASSERT(die_is_type(l));
10155 ABG_ASSERT(die_is_type(r));
10156
10157 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10158 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10159 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10160 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10161 // For now, we cannot compare DW_TAG_string_type because of its
10162 // string_length attribute that is a location descriptor that is
10163 // not necessarily a constant. So it's super hard to evaluate it
10164 // in a libabigail context. So for now, we just say that all
10165 // DW_TAG_string_type DIEs are different, by default.
10166 return false;
10167
10168 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10169 // A declaration of a type compares equal to the definition of the
10170 // type.
10171 return true;
10172
10173 uint64_t l_size = 0, r_size = 0;
10174 die_size_in_bits(l, l_size);
10175 die_size_in_bits(r, r_size);
10176
10177 return l_size == r_size;
10178 }
10179
10180 /// Compare two DIEs as decls (looking as their names etc) and as
10181 /// types (looking at their size etc).
10182 ///
10183 /// @param rdr the DWARF reader to consider.
10184 ///
10185 /// @param l the first DIE to consider.
10186 ///
10187 /// @param r the second DIE to consider.
10188 ///
10189 /// @return TRUE iff @p l equals @p r as far as naming and size is
10190 /// concerned.
10191 static bool
compare_as_decl_and_type_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10192 compare_as_decl_and_type_dies(const reader &rdr,
10193 const Dwarf_Die *l,
10194 const Dwarf_Die *r)
10195 {
10196 if (!compare_as_decl_dies(l, r)
10197 || !compare_as_type_dies(rdr, l, r))
10198 return false;
10199
10200 return true;
10201 }
10202
10203 /// Test if two DIEs representing function declarations have the same
10204 /// linkage name, and thus are considered equal if they are C or C++,
10205 /// because the two DIEs represent functions in the same binary.
10206 ///
10207 /// If the DIEs don't have a linkage name, the function compares their
10208 /// name. But in that case, the caller of the function must know that
10209 /// in C++ for instance, that doesn't imply that the two functions are
10210 /// equal.
10211 ///
10212 /// @param rdr the @ref reader to consider.
10213 ///
10214 /// @param l the first function DIE to consider.
10215 ///
10216 /// @param r the second function DIE to consider.
10217 ///
10218 /// @return true iff the function represented by @p l have the same
10219 /// linkage name as the function represented by @p r.
10220 static bool
fn_die_equal_by_linkage_name(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10221 fn_die_equal_by_linkage_name(const reader &rdr,
10222 const Dwarf_Die *l,
10223 const Dwarf_Die *r)
10224 {
10225 if (!!l != !!r)
10226 return false;
10227
10228 if (!l)
10229 return false;
10230
10231 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10232 ABG_ASSERT(tag == DW_TAG_subprogram);
10233 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10234 ABG_ASSERT(tag == DW_TAG_subprogram);
10235
10236 string lname = die_name(l), rname = die_name(r);
10237 string llinkage_name = die_linkage_name(l),
10238 rlinkage_name = die_linkage_name(r);
10239
10240 if (rdr.die_is_in_c_or_cplusplus(l)
10241 && rdr.die_is_in_c_or_cplusplus(r))
10242 {
10243 if (!llinkage_name.empty() && !rlinkage_name.empty())
10244 return llinkage_name == rlinkage_name;
10245 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10246 return false;
10247 else
10248 return lname == rname;
10249 }
10250
10251 return (!llinkage_name.empty()
10252 && !rlinkage_name.empty()
10253 && llinkage_name == rlinkage_name);
10254 }
10255
10256 /// Compare two DIEs in the context of DIE canonicalization.
10257 ///
10258 /// If DIE canonicalization is on, the function compares the DIEs
10259 /// canonically and structurally. The two types of comparison should
10260 /// be equal, of course.
10261 ///
10262 /// @param rdr the DWARF reader.
10263 ///
10264 /// @param l_offset the offset of the first canonical DIE to compare.
10265 ///
10266 /// @param r_offset the offset of the second canonical DIE to compare.
10267 ///
10268 /// @param l_die_source the source of the DIE denoted by the offset @p
10269 /// l_offset.
10270 ///
10271 /// @param r_die_source the source of the DIE denoted by the offset @p
10272 /// r_offset.
10273 ///
10274 /// @param l_has_canonical_die_offset output parameter. Is set to
10275 /// true if @p l_offset has a canonical DIE.
10276 ///
10277 /// @param r_has_canonical_die_offset output parameter. Is set to
10278 /// true if @p r_offset has a canonical DIE.
10279 ///
10280 /// @param l_canonical_die_offset output parameter. If @p
10281 /// l_has_canonical_die_offset is set to true, then this parameter is
10282 /// set to the offset of the canonical DIE of the DIE designated by @p
10283 /// l_offset.
10284 static bool
try_canonical_die_comparison(const reader & rdr,Dwarf_Off l_offset,Dwarf_Off r_offset,die_source l_die_source,die_source r_die_source,bool & l_has_canonical_die_offset,bool & r_has_canonical_die_offset,Dwarf_Off & l_canonical_die_offset,Dwarf_Off & r_canonical_die_offset,bool & result)10285 try_canonical_die_comparison(const reader& rdr,
10286 Dwarf_Off l_offset, Dwarf_Off r_offset,
10287 die_source l_die_source, die_source r_die_source,
10288 bool& l_has_canonical_die_offset,
10289 bool& r_has_canonical_die_offset,
10290 Dwarf_Off& l_canonical_die_offset,
10291 Dwarf_Off& r_canonical_die_offset,
10292 bool& result)
10293 {
10294 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10295 if (rdr.debug_die_canonicalization_is_on_
10296 && !rdr.use_canonical_die_comparison_)
10297 return false;
10298 #endif
10299
10300
10301 l_has_canonical_die_offset =
10302 (l_canonical_die_offset =
10303 rdr.get_canonical_die_offset(l_offset, l_die_source,
10304 /*die_as_type=*/true));
10305
10306 r_has_canonical_die_offset =
10307 (r_canonical_die_offset =
10308 rdr.get_canonical_die_offset(r_offset, r_die_source,
10309 /*die_as_type=*/true));
10310
10311 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10312 {
10313 result = (l_canonical_die_offset == r_canonical_die_offset);
10314 return true;
10315 }
10316
10317 return false;
10318 }
10319
10320 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10321 /// This function is called whenever a DIE comparison fails.
10322 ///
10323 /// This function is intended for debugging purposes. The idea is for
10324 /// hackers to set a breakpoint on this function so that they can
10325 /// discover why exactly the comparison failed. They then can execute
10326 /// the program from compare_dies_during_canonicalization, for
10327 /// instance.
10328 ///
10329 /// @param @l the left-hand side of the DIE comparison.
10330 ///
10331 /// @param @r the right-hand side of the DIE comparison.
10332 static void
notify_die_comparison_failed(const Dwarf_Die *,const Dwarf_Die *)10333 notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10334 {
10335 }
10336
10337 #define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10338 notify_die_comparison_failed(l, r)
10339 #else
10340 #define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10341 #endif
10342
10343 /// A macro used to return from DIE comparison routines.
10344 ///
10345 /// If the return value is false, the macro invokes the
10346 /// notify_die_comparison_failed signalling function before returning.
10347 /// That way, hackers willing to learn more about why the comparison
10348 /// routine returned "false" can just set a breakpoint on
10349 /// notify_die_comparison_failed and execute the program from
10350 /// compare_dies_during_canonicalization, for instance.
10351 ///
10352 /// @param value the value to return from the DIE comparison routines.
10353 #define ABG_RETURN(value) \
10354 do \
10355 { \
10356 if ((value) == COMPARISON_RESULT_DIFFERENT) \
10357 { \
10358 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10359 } \
10360 return return_comparison_result(l, r, dies_being_compared, \
10361 value, aggregates_being_compared, \
10362 update_canonical_dies_on_the_fly); \
10363 } \
10364 while(false)
10365
10366 /// A macro used to return the "false" boolean from DIE comparison
10367 /// routines.
10368 ///
10369 /// As the return value is false, the macro invokes the
10370 /// notify_die_comparison_failed signalling function before returning.
10371 ///
10372 /// @param value the value to return from the DIE comparison routines.
10373 #define ABG_RETURN_FALSE \
10374 do \
10375 { \
10376 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10377 return return_comparison_result(l, r, dies_being_compared, \
10378 COMPARISON_RESULT_DIFFERENT, \
10379 aggregates_being_compared, \
10380 update_canonical_dies_on_the_fly); \
10381 } while(false)
10382
10383 /// A macro to set the 'result' variable to 'false'.
10384 ///
10385 /// The macro invokes the notify_die_comparison_failed function so
10386 /// that the hacker can set a debugging breakpoint on
10387 /// notify_die_comparison_failed to know where a DIE comparison failed
10388 /// during compare_dies_during_canonicalization for instance.
10389 ///
10390 /// @param result the 'result' variable to set.
10391 ///
10392 /// @param l the first DIE of the comparison operation.
10393 ///
10394 /// @param r the second DIE of the comparison operation.
10395 #define SET_RESULT_TO_FALSE(result, l , r) \
10396 do \
10397 { \
10398 result = COMPARISON_RESULT_DIFFERENT; \
10399 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10400 } while(false)
10401
10402 /// A macro to set the 'result' variable to a given value.
10403 ///
10404 /// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10405 /// invokes the notify_die_comparison_failed function so that the
10406 /// hacker can set a debugging breakpoint on
10407 /// notify_die_comparison_failed to know where a DIE comparison failed
10408 /// during compare_dies_during_canonicalization for instance.
10409 ///
10410 /// @param result the 'result' variable to set.
10411 ///
10412 /// @param l the first DIE of the comparison operation.
10413 ///
10414 /// @param r the second DIE of the comparison operation.
10415 #define SET_RESULT_TO(result, value, l , r) \
10416 do \
10417 { \
10418 result = (value); \
10419 if (result == COMPARISON_RESULT_DIFFERENT) \
10420 { \
10421 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10422 } \
10423 } while(false)
10424
10425 #define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10426 do \
10427 { \
10428 if (aggregates_being_compared.contains(dies_being_compared)) \
10429 { \
10430 result = COMPARISON_RESULT_CYCLE_DETECTED; \
10431 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10432 ABG_RETURN(result); \
10433 } \
10434 } \
10435 while(false)
10436
10437 /// Get the next member sibling of a given class or union member DIE.
10438 ///
10439 /// @param die the DIE to consider.
10440 ///
10441 /// @param member out parameter. This is set to the next member
10442 /// sibling, iff the function returns TRUE.
10443 ///
10444 /// @return TRUE iff the function set @p member to the next member
10445 /// sibling DIE.
10446 static bool
get_next_member_sibling_die(const Dwarf_Die * die,Dwarf_Die * member)10447 get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10448 {
10449 if (!die)
10450 return false;
10451
10452 bool found_member = false;
10453 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10454 member) == 0);
10455 found_member;
10456 found_member = (dwarf_siblingof(member, member) == 0))
10457 {
10458 int tag = dwarf_tag(member);
10459 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10460 break;
10461 }
10462
10463 return found_member;
10464 }
10465
10466 /// Get the first child DIE of a class/struct/union DIE that is a
10467 /// member DIE.
10468 ///
10469 /// @param die the DIE to consider.
10470 ///
10471 /// @param child out parameter. This is set to the first child DIE of
10472 /// @p iff this function returns TRUE.
10473 ///
10474 /// @return TRUE iff @p child is set to the first child DIE of @p die
10475 /// that is a member DIE.
10476 static bool
get_member_child_die(const Dwarf_Die * die,Dwarf_Die * child)10477 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10478 {
10479 if (!die)
10480 return false;
10481
10482 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10483 ABG_ASSERT(tag == DW_TAG_structure_type
10484 || tag == DW_TAG_union_type
10485 || tag == DW_TAG_class_type);
10486
10487 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10488 child) == 0);
10489
10490 if (!found_child)
10491 return false;
10492
10493 tag = dwarf_tag(child);
10494
10495 if (!(tag == DW_TAG_member
10496 || tag == DW_TAG_inheritance
10497 || tag == DW_TAG_subprogram))
10498 found_child = get_next_member_sibling_die(child, child);
10499
10500 return found_child;
10501 }
10502
10503 /// This is a sub-routine of return_comparison_result.
10504 ///
10505 /// Propagate the canonical type of a the right-hand-side DIE to the
10506 /// lef-hand-side DIE. This is a optimization that is done when the
10507 /// two DIEs compare equal.
10508 ///
10509 /// If the right-hand-side DIE is not canonicalized, the function
10510 /// performs its canonicalization.
10511 ///
10512 /// This optimization is performed only if
10513 /// is_canon_type_to_be_propagated_tag returns true.
10514 ///
10515 /// @param rdr the current context to consider.
10516 ///
10517 /// @param l the left-hand-side DIE of the comparison. It's going to
10518 /// receive the canonical type of the other DIE.
10519 ///
10520 /// @param r the right-hand-side DIE of the comparison. Its canonical
10521 /// type is propagated to @p l.
10522 static void
maybe_propagate_canonical_type(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10523 maybe_propagate_canonical_type(const reader& rdr,
10524 const Dwarf_Die* l,
10525 const Dwarf_Die* r)
10526 {
10527 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10528 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10529
10530 if (l_tag != r_tag)
10531 return;
10532
10533 if (is_canon_type_to_be_propagated_tag(l_tag))
10534 propagate_canonical_type(rdr, l, r);
10535 }
10536
10537 /// Propagate the canonical type of a the right-hand-side DIE to the
10538 /// left-hand-side DIE. This is a optimization that is done when the
10539 /// two DIEs compare equal.
10540 ///
10541 /// If the right-hand-side DIE is not canonicalized, the function
10542 /// performs its canonicalization.
10543 ///
10544 /// @param rdr the current context to consider.
10545 ///
10546 /// @param l the left-hand-side DIE of the comparison. It's going to
10547 /// receive the canonical type of the other DIE.
10548 ///
10549 /// @param r the right-hand-side DIE of the comparison. Its canonical
10550 /// type is propagated to @p l.
10551 static void
propagate_canonical_type(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r)10552 propagate_canonical_type(const reader& rdr,
10553 const Dwarf_Die* l,
10554 const Dwarf_Die* r)
10555 {
10556 ABG_ASSERT(l && r);
10557
10558 // If 'l' has no canonical DIE and if 'r' has one, then propagage
10559 // the canonical DIE of 'r' to 'l'.
10560 //
10561 // In case 'r' has no canonical DIE, then compute it, and then
10562 // propagate that canonical DIE to 'r'.
10563 const die_source l_source = rdr.get_die_source(l);
10564 const die_source r_source = rdr.get_die_source(r);
10565
10566 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10567 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10568 bool l_has_canonical_die_offset = false;
10569 bool r_has_canonical_die_offset = false;
10570 Dwarf_Off l_canonical_die_offset = 0;
10571 Dwarf_Off r_canonical_die_offset = 0;
10572
10573 l_has_canonical_die_offset =
10574 (l_canonical_die_offset =
10575 rdr.get_canonical_die_offset(l_offset, l_source,
10576 /*die_as_type=*/true));
10577
10578 r_has_canonical_die_offset =
10579 (r_canonical_die_offset =
10580 rdr.get_canonical_die_offset(r_offset, r_source,
10581 /*die_as_type=*/true));
10582
10583
10584 if (!l_has_canonical_die_offset
10585 && r_has_canonical_die_offset
10586 // A DIE can be equivalent only to another DIE of the same
10587 // source.
10588 && l_source == r_source)
10589 {
10590 ABG_ASSERT(r_canonical_die_offset);
10591 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10592 /*die_as_type=*/true);
10593 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10594 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10595 rdr.canonical_propagated_count_++;
10596 }
10597 }
10598
10599 /// This function does the book keeping of comparison pairs necessary
10600 /// to handle
10601 ///
10602 /// * the detection of cycles during the comparison of aggregate
10603 /// types, in conjuction with the macro
10604 /// RETURN_IF_COMPARISON_CYCLE_DETECTED
10605 ///
10606 /// * the handling of the canonical type propagation optimisation
10607 /// to speed-up type canonicalization.
10608 ///
10609 ///
10610 /// Note that this function is essentially a sub-routine of
10611 /// compare_dies.
10612 ///
10613 /// @param l the left-hand-side DIE being compared.
10614 ///
10615 /// @param r the right-hand-side DIE being compared.
10616 ///
10617 /// @param cur_dies the pair of die offsets of l and r. This is
10618 /// redundant as it can been computed from @p l and @p r. However,
10619 /// getting it as an argument is an optimization to avoid computing it
10620 /// over and over again, given how often this function is invoked from
10621 /// compare_dies.
10622 ///
10623 /// @param return the result of comparing @p l against @p r.
10624 ///
10625 /// @param comparison_stack the stack of pair of type DIEs being
10626 /// compared.
10627 ///
10628 /// @param do_propagate_canonical_type if true then the function
10629 /// performs canonical DIEs propagation, meaning that if @p l equals
10630 /// @p r and if @p r has a canonical type, then the canonical type of
10631 /// @p l is set to the canonical type of @p r.
10632 static comparison_result
return_comparison_result(const Dwarf_Die * l,const Dwarf_Die * r,const offset_pair_type & cur_dies,comparison_result result,offset_pairs_stack_type & comparison_stack,bool do_propagate_canonical_type=true)10633 return_comparison_result(const Dwarf_Die* l,
10634 const Dwarf_Die* r,
10635 const offset_pair_type& cur_dies,
10636 comparison_result result,
10637 offset_pairs_stack_type& comparison_stack,
10638 bool do_propagate_canonical_type = true)
10639 {
10640 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10641
10642 if (result == COMPARISON_RESULT_EQUAL)
10643 {
10644 // The result comparing the two types is "true", basically. So
10645 // let's propagate the canonical type of r onto l, so that we
10646 // don't need to compute the canonical type of r.
10647 if (do_propagate_canonical_type)
10648 {
10649 // Propagate canonical type.
10650 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10651
10652 // TODO: do we need to confirm any tentative canonical
10653 // propagation?
10654 }
10655 }
10656 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10657 {
10658 // So upon detection of the comparison cycle, compare_dies
10659 // returned early with the comparison result
10660 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
10661 // carry on with the comparison of all the OTHER sub-types of
10662 // the redundant type. If they all compare equal, then it means
10663 // the redundant type pair compared equal. Otherwise, it
10664 // compared different.
10665 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
10666 // Let's fall through to let the end of this function set the
10667 // result to COMPARISON_RESULT_UNKNOWN;
10668 }
10669 else if (result == COMPARISON_RESULT_UNKNOWN)
10670 {
10671 // Here is an introductory comment describing what we are going
10672 // to do in this case where the result of the comparison of the
10673 // current pair of type is not "false", basically.
10674 //
10675 // This means that we don't yet know what the result of
10676 // comparing these two types is, because one of the sub-types of
10677 // the types being compared is "redundant", meaning it appears
10678 // more than once in the comparison stack, so if we were to
10679 // naively try to carry on with the comparison member-wise, we'd
10680 // end up with an endless loop, a.k.a "comparison cycle".
10681 //
10682 // If the current type pair is redundant then:
10683 //
10684 // * This is a redundant type that has just been fully
10685 // compared. In that case, all the types that depend on
10686 // this redundant type and that have been tentatively
10687 // canonical-type-propagated must see their canonical types
10688 // "confirmed". This means that this type is going to be
10689 // considered as not being redundant anymore, meaning all
10690 // the types that depend on it must be updated as not being
10691 // dependant on it anymore, and the type itsef must be
10692 // removed from the map of redundant types.
10693 //
10694 // After the type's canonical-type-propagation is confirmed,
10695 // the result of its comparison must also be changed into
10696 // COMPARISON_RESULT_EQUAL.
10697 //
10698 // After that, If the current type depends on a redundant type,
10699 // then propagate its canonical type AND track it as having its
10700 // type being canonical-type-propagated.
10701 //
10702 // If the current type is not redundant however, then it must be
10703 // dependant on a redundant type. If it's not dependant on a
10704 // redundant type, then it must be of those types which
10705 // comparisons are not tracked for cycle, probably because they
10706 // are not aggregates. Otherwise, ABORT to understand why. I
10707 // believe this should not happen. In any case, after that
10708 // safety check is passed, we just need to return at this point.
10709
10710 if (comparison_stack.is_redundant(cur_dies)
10711 && comparison_stack.vect_.back() == cur_dies)
10712 {
10713 // We are in the case described above of a redundant type
10714 // that has been fully compared.
10715 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10716 comparison_stack.confirm_canonical_propagated_type(cur_dies);
10717
10718 result = COMPARISON_RESULT_EQUAL;
10719 }
10720 else if (is_canon_type_to_be_propagated_tag(l_tag)
10721 && comparison_stack.vect_.back() == cur_dies)
10722 {
10723 // The current type is not redundant. So, as described in
10724 // the introductory comment above, it must be dependant on a
10725 // redundant type.
10726 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
10727 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10728 // Then pass through.
10729 }
10730 }
10731 else if (result == COMPARISON_RESULT_DIFFERENT)
10732 {
10733 // Here is an introductory comment describing what we are going
10734 // to do in this case where the result of the comparison of the
10735 // current pair of type is "false", basically.
10736 //
10737 // If the type pair {l,r} is redundant then cancel the
10738 // canonical-type-propagation of all the dependant pairs that
10739 // depends on this redundant {l, r}. This means walk the types
10740 // that depends on {l, r} and cancel their
10741 // canonical-propagate-type, that means remove their canonical
10742 // types and mark them as not being canonically-propagated.
10743 // Also, erase their cached comparison results that was likely
10744 // set to COMPARISON_RESULT_UNKNOWN.
10745 //
10746 // Also, update the cached result for this pair, that was likely
10747 // to be COMPARISON_RESULT_UNKNOWN.
10748 if (comparison_stack.is_redundant(cur_dies)
10749 && comparison_stack.vect_.back() == cur_dies)
10750 comparison_stack.cancel_canonical_propagated_type(cur_dies);
10751 }
10752 else
10753 {
10754 // We should never reach here.
10755 ABG_ASSERT_NOT_REACHED;
10756 }
10757
10758 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10759 result = COMPARISON_RESULT_UNKNOWN;
10760 else if (is_canon_type_to_be_propagated_tag(l_tag)
10761 && !comparison_stack.vect_.empty()
10762 && comparison_stack.vect_.back() == cur_dies)
10763 //Finally pop the pair types being compared from comparison_stack
10764 //iff {l,r} is on the top of the stack. If it's not, then it means
10765 //we are looking at a type that was detected as a being redundant
10766 //and thus hasn't been pushed to the stack yet gain.
10767 comparison_stack.erase(cur_dies);
10768
10769 maybe_cache_type_comparison_result(comparison_stack.rdr_,
10770 l_tag, cur_dies, result);
10771
10772 return result;
10773 }
10774
10775 /// Compare two DIEs emitted by a C compiler.
10776 ///
10777 /// @param rdr the DWARF reader used to load the DWARF information.
10778 ///
10779 /// @param l the left-hand-side argument of this comparison operator.
10780 ///
10781 /// @param r the righ-hand-side argument of this comparison operator.
10782 ///
10783 /// @param aggregates_being_compared this holds the names of the set
10784 /// of aggregates being compared. It's used by the comparison
10785 /// function to avoid recursing infinitely when faced with types
10786 /// referencing themselves through pointers or references. By
10787 /// default, just pass an empty instance of @ref istring_set_type to
10788 /// it.
10789 ///
10790 /// @param update_canonical_dies_on_the_fly if true, when two
10791 /// sub-types compare equal (during the comparison of @p l and @p r)
10792 /// update their canonical type. That way, two types of the same name
10793 /// are structurally compared to each other only once. So the
10794 /// non-linear structural comparison of two types of the same name
10795 /// only happen once.
10796 ///
10797 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
10798 static comparison_result
compare_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r,offset_pairs_stack_type & aggregates_being_compared,bool update_canonical_dies_on_the_fly)10799 compare_dies(const reader& rdr,
10800 const Dwarf_Die *l, const Dwarf_Die *r,
10801 offset_pairs_stack_type& aggregates_being_compared,
10802 bool update_canonical_dies_on_the_fly)
10803 {
10804 ABG_ASSERT(l);
10805 ABG_ASSERT(r);
10806
10807 const die_source l_die_source = rdr.get_die_source(l);
10808 const die_source r_die_source = rdr.get_die_source(r);
10809
10810 offset_type l_offset =
10811 {
10812 l_die_source,
10813 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10814 };
10815
10816 offset_type r_offset =
10817 {
10818 r_die_source,
10819 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
10820 };
10821
10822 offset_pair_type dies_being_compared(l_offset, r_offset);
10823
10824 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10825 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10826
10827 if (l_tag != r_tag)
10828 ABG_RETURN_FALSE;
10829
10830 if (l_offset == r_offset)
10831 return COMPARISON_RESULT_EQUAL;
10832
10833 if (rdr.leverage_dwarf_factorization()
10834 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
10835 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
10836 if (l_offset != r_offset)
10837 return COMPARISON_RESULT_DIFFERENT;
10838
10839 comparison_result result = COMPARISON_RESULT_EQUAL;
10840 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
10841 dies_being_compared,
10842 result))
10843 return result;
10844
10845 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10846 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
10847
10848 // If 'l' and 'r' already have canonical DIEs, then just compare the
10849 // offsets of their canonical DIEs.
10850 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
10851 {
10852 bool canonical_compare_result = false;
10853 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
10854 l_die_source, r_die_source,
10855 l_has_canonical_die_offset,
10856 r_has_canonical_die_offset,
10857 l_canonical_die_offset,
10858 r_canonical_die_offset,
10859 canonical_compare_result))
10860 {
10861 comparison_result result;
10862 SET_RESULT_TO(result,
10863 (canonical_compare_result
10864 ? COMPARISON_RESULT_EQUAL
10865 : COMPARISON_RESULT_DIFFERENT),
10866 l, r);
10867 return result;
10868 }
10869 }
10870
10871
10872
10873 switch (l_tag)
10874 {
10875 case DW_TAG_base_type:
10876 case DW_TAG_string_type:
10877 case DW_TAG_unspecified_type:
10878 if (!compare_as_decl_and_type_dies(rdr, l, r))
10879 SET_RESULT_TO_FALSE(result, l, r);
10880 break;
10881
10882 case DW_TAG_typedef:
10883 case DW_TAG_pointer_type:
10884 case DW_TAG_reference_type:
10885 case DW_TAG_rvalue_reference_type:
10886 case DW_TAG_const_type:
10887 case DW_TAG_volatile_type:
10888 case DW_TAG_restrict_type:
10889 {
10890 if (!compare_as_type_dies(rdr, l, r))
10891 {
10892 SET_RESULT_TO_FALSE(result, l, r);
10893 break;
10894 }
10895
10896 bool from_the_same_tu = false;
10897 if (!pointer_or_qual_die_of_anonymous_class_type(l)
10898 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
10899 && from_the_same_tu)
10900 {
10901 // These two typedefs, pointer, reference, or qualified
10902 // types have the same name and are defined in the same TU.
10903 // They thus ought to be the same.
10904 //
10905 // Note that pointers, reference or qualified types to
10906 // anonymous types are not taking into account here because
10907 // those always need to be structurally compared.
10908 SET_RESULT_TO_FALSE(result, l, r);
10909 break;
10910 }
10911 }
10912
10913 {
10914 // No fancy optimization in this case. We need to
10915 // structurally compare the two DIEs.
10916 Dwarf_Die lu_type_die, ru_type_die;
10917 bool lu_is_void, ru_is_void;
10918
10919 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
10920 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
10921
10922 if (lu_is_void && ru_is_void)
10923 result = COMPARISON_RESULT_EQUAL;
10924 else if (lu_is_void != ru_is_void)
10925 SET_RESULT_TO_FALSE(result, l, r);
10926 else
10927 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
10928 aggregates_being_compared,
10929 update_canonical_dies_on_the_fly);
10930 }
10931 break;
10932
10933 case DW_TAG_enumeration_type:
10934 if (!compare_as_decl_and_type_dies(rdr, l, r))
10935 SET_RESULT_TO_FALSE(result, l, r);
10936 else
10937 {
10938 // Walk the enumerators.
10939 Dwarf_Die l_enumtor, r_enumtor;
10940 bool found_l_enumtor = true, found_r_enumtor = true;
10941
10942 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10943 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
10944 &l_enumtor) == 0,
10945 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
10946 &r_enumtor) == 0;
10947 found_l_enumtor && found_r_enumtor;
10948 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
10949 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
10950 {
10951 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
10952 if ( l_tag != r_tag)
10953 {
10954 SET_RESULT_TO_FALSE(result, l, r);
10955 break;
10956 }
10957
10958 if (l_tag != DW_TAG_enumerator)
10959 continue;
10960
10961 uint64_t l_val = 0, r_val = 0;
10962 die_unsigned_constant_attribute(&l_enumtor,
10963 DW_AT_const_value,
10964 l_val);
10965 die_unsigned_constant_attribute(&r_enumtor,
10966 DW_AT_const_value,
10967 r_val);
10968 if (l_val != r_val)
10969 {
10970 SET_RESULT_TO_FALSE(result, l, r);
10971 break;
10972 }
10973 }
10974 if (found_l_enumtor != found_r_enumtor )
10975 SET_RESULT_TO_FALSE(result, l, r);
10976 }
10977 break;
10978
10979 case DW_TAG_structure_type:
10980 case DW_TAG_union_type:
10981 case DW_TAG_class_type:
10982 {
10983 RETURN_IF_COMPARISON_CYCLE_DETECTED;
10984
10985 rdr.compare_count_++;
10986
10987 if (!compare_as_decl_and_type_dies(rdr, l, r))
10988 SET_RESULT_TO_FALSE(result, l, r);
10989 else if (rdr.options().assume_odr_for_cplusplus
10990 && rdr.odr_is_relevant(l)
10991 && rdr.odr_is_relevant(r)
10992 && !die_is_anonymous(l)
10993 && !die_is_anonymous(r))
10994 result = COMPARISON_RESULT_EQUAL;
10995 else
10996 {
10997 aggregates_being_compared.add(dies_being_compared);
10998
10999 Dwarf_Die l_member, r_member;
11000 bool found_l_member = true, found_r_member = true;
11001
11002 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11003 for (found_l_member = get_member_child_die(l, &l_member),
11004 found_r_member = get_member_child_die(r, &r_member);
11005 found_l_member && found_r_member;
11006 found_l_member = get_next_member_sibling_die(&l_member,
11007 &l_member),
11008 found_r_member = get_next_member_sibling_die(&r_member,
11009 &r_member))
11010 {
11011 int l_tag = dwarf_tag(&l_member),
11012 r_tag = dwarf_tag(&r_member);
11013
11014 if (l_tag != r_tag)
11015 {
11016 SET_RESULT_TO_FALSE(result, l, r);
11017 break;
11018 }
11019
11020 ABG_ASSERT(l_tag == DW_TAG_member
11021 || l_tag == DW_TAG_variable
11022 || l_tag == DW_TAG_inheritance
11023 || l_tag == DW_TAG_subprogram);
11024
11025 comparison_result local_result =
11026 compare_dies(rdr, &l_member, &r_member,
11027 aggregates_being_compared,
11028 update_canonical_dies_on_the_fly);
11029
11030 if (local_result == COMPARISON_RESULT_UNKNOWN)
11031 // Note that if the result of comparing any
11032 // sub-type is COMPARISON_RESULT_EQUAL, just
11033 // because we have at least one sub-type's
11034 // comparison being COMPARISON_RESULT_UNKNOWN
11035 // means that the comparison of this type will
11036 // return COMPARISON_RESULT_UNKNOWN to show
11037 // callers that this type (and all the types that
11038 // depend on it) depends on a redundant type
11039 result = local_result;
11040
11041 if (local_result == COMPARISON_RESULT_DIFFERENT)
11042 {
11043 SET_RESULT_TO_FALSE(result, l, r);
11044 break;
11045 }
11046 }
11047 if (found_l_member != found_r_member)
11048 {
11049 SET_RESULT_TO_FALSE(result, l, r);
11050 break;
11051 }
11052 }
11053 }
11054 break;
11055
11056 case DW_TAG_array_type:
11057 {
11058 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11059
11060 aggregates_being_compared.add(dies_being_compared);
11061
11062 rdr.compare_count_++;
11063
11064 Dwarf_Die l_child, r_child;
11065 bool found_l_child, found_r_child;
11066 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11067 &l_child) == 0,
11068 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11069 &r_child) == 0;
11070 found_l_child && found_r_child;
11071 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
11072 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
11073 {
11074 int l_child_tag = dwarf_tag(&l_child),
11075 r_child_tag = dwarf_tag(&r_child);
11076 if (l_child_tag == DW_TAG_subrange_type
11077 || r_child_tag == DW_TAG_subrange_type)
11078 {
11079 result = compare_dies(rdr, &l_child, &r_child,
11080 aggregates_being_compared,
11081 update_canonical_dies_on_the_fly);
11082 if (!result)
11083 {
11084 SET_RESULT_TO_FALSE(result, l, r);
11085 break;
11086 }
11087 }
11088 }
11089 if (found_l_child != found_r_child)
11090 SET_RESULT_TO_FALSE(result, l, r);
11091 // Compare the types of the elements of the array.
11092 Dwarf_Die ltype_die, rtype_die;
11093 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
11094 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
11095 ABG_ASSERT(found_ltype && found_rtype);
11096
11097 result = compare_dies(rdr, <ype_die, &rtype_die,
11098 aggregates_being_compared,
11099 update_canonical_dies_on_the_fly);
11100 if (!result)
11101 ABG_RETURN_FALSE;
11102 }
11103 break;
11104
11105 case DW_TAG_subrange_type:
11106 {
11107 uint64_t l_lower_bound = 0, r_lower_bound = 0,
11108 l_upper_bound = 0, r_upper_bound = 0;
11109 bool l_lower_bound_set = false, r_lower_bound_set = false,
11110 l_upper_bound_set = false, r_upper_bound_set = false;
11111
11112 l_lower_bound_set =
11113 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
11114 r_lower_bound_set =
11115 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
11116
11117 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
11118 l_upper_bound))
11119 {
11120 uint64_t l_count = 0;
11121 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
11122 {
11123 l_upper_bound = l_lower_bound + l_count;
11124 l_upper_bound_set = true;
11125 if (l_upper_bound)
11126 --l_upper_bound;
11127 }
11128 }
11129 else
11130 l_upper_bound_set = true;
11131
11132 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
11133 r_upper_bound))
11134 {
11135 uint64_t r_count = 0;
11136 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
11137 {
11138 r_upper_bound = r_lower_bound + r_count;
11139 r_upper_bound_set = true;
11140 if (r_upper_bound)
11141 --r_upper_bound;
11142 }
11143 }
11144 else
11145 r_upper_bound_set = true;
11146
11147 if ((l_lower_bound_set != r_lower_bound_set)
11148 || (l_upper_bound_set != r_upper_bound_set)
11149 || (l_lower_bound != r_lower_bound)
11150 || (l_upper_bound != r_upper_bound))
11151 SET_RESULT_TO_FALSE(result, l, r);
11152 }
11153 break;
11154
11155 case DW_TAG_subroutine_type:
11156 case DW_TAG_subprogram:
11157 {
11158 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11159
11160 aggregates_being_compared.add(dies_being_compared);
11161
11162 rdr.compare_count_++;
11163
11164 if (l_tag == DW_TAG_subprogram
11165 && !fn_die_equal_by_linkage_name(rdr, l, r))
11166 {
11167 SET_RESULT_TO_FALSE(result, l, r);
11168 break;
11169 }
11170 else if (l_tag == DW_TAG_subprogram
11171 && rdr.die_is_in_c(l) && rdr.die_is_in_c(r)
11172 /*&& fn_die_equal_by_linkage_name(rdr, l, r)*/)
11173 {
11174 result = COMPARISON_RESULT_EQUAL;
11175 break;
11176 }
11177 else if (!rdr.die_is_in_c(l) && !rdr.die_is_in_c(r))
11178 {
11179 // In C, we cannot have two different functions with the
11180 // same linkage name in a given binary. But here we are
11181 // looking at DIEs that don't originate from C. So we
11182 // need to compare return types and parameter types.
11183 Dwarf_Die l_return_type, r_return_type;
11184 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
11185 l_return_type);
11186 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
11187 r_return_type);
11188 if (l_return_type_is_void != r_return_type_is_void
11189 || (!l_return_type_is_void
11190 && !compare_dies(rdr,
11191 &l_return_type, &r_return_type,
11192 aggregates_being_compared,
11193 update_canonical_dies_on_the_fly)))
11194 SET_RESULT_TO_FALSE(result, l, r);
11195 else
11196 {
11197 Dwarf_Die l_child, r_child;
11198 bool found_l_child, found_r_child;
11199 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11200 &l_child) == 0,
11201 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11202 &r_child) == 0;
11203 found_l_child && found_r_child;
11204 found_l_child = dwarf_siblingof(&l_child,
11205 &l_child) == 0,
11206 found_r_child = dwarf_siblingof(&r_child,
11207 &r_child)==0)
11208 {
11209 int l_child_tag = dwarf_tag(&l_child);
11210 int r_child_tag = dwarf_tag(&r_child);
11211 comparison_result local_result =
11212 COMPARISON_RESULT_EQUAL;
11213 if (l_child_tag != r_child_tag)
11214 local_result = COMPARISON_RESULT_DIFFERENT;
11215 if (l_child_tag == DW_TAG_formal_parameter)
11216 local_result =
11217 compare_dies(rdr, &l_child, &r_child,
11218 aggregates_being_compared,
11219 update_canonical_dies_on_the_fly);
11220 if (local_result == COMPARISON_RESULT_DIFFERENT)
11221 {
11222 result = local_result;
11223 SET_RESULT_TO_FALSE(result, l, r);
11224 break;
11225 }
11226 if (local_result == COMPARISON_RESULT_UNKNOWN)
11227 // Note that if the result of comparing any
11228 // sub-type is COMPARISON_RESULT_EQUAL, just
11229 // because we have at least one sub-type's
11230 // comparison being COMPARISON_RESULT_UNKNOWN
11231 // means that the comparison of this type will
11232 // return COMPARISON_RESULT_UNKNOWN to show
11233 // callers that this type (and all the types
11234 // that depend on it) depends on a redundant
11235 // type and so, can't be
11236 // canonical-type-propagated.
11237 result = local_result;
11238 }
11239 if (found_l_child != found_r_child)
11240 {
11241 SET_RESULT_TO_FALSE(result, l, r);
11242 break;
11243 }
11244 }
11245 }
11246 }
11247 break;
11248
11249 case DW_TAG_formal_parameter:
11250 {
11251 Dwarf_Die l_type, r_type;
11252 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
11253 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
11254 if (l_type_is_void != r_type_is_void)
11255 SET_RESULT_TO_FALSE(result, l, r);
11256 else if (!l_type_is_void)
11257 {
11258 comparison_result local_result =
11259 compare_dies(rdr, &l_type, &r_type,
11260 aggregates_being_compared,
11261 update_canonical_dies_on_the_fly);
11262 SET_RESULT_TO(result, local_result, l, r);
11263 }
11264 }
11265 break;
11266
11267 case DW_TAG_variable:
11268 case DW_TAG_member:
11269 if (compare_as_decl_dies(l, r))
11270 {
11271 // Compare the offsets of the data members
11272 if (l_tag == DW_TAG_member)
11273 {
11274 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11275 die_member_offset(rdr, l, l_offset_in_bits);
11276 die_member_offset(rdr, r, r_offset_in_bits);
11277 if (l_offset_in_bits != r_offset_in_bits)
11278 SET_RESULT_TO_FALSE(result, l, r);
11279 }
11280 if (result)
11281 {
11282 // Compare the types of the data members or variables.
11283 Dwarf_Die l_type, r_type;
11284 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11285 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11286 comparison_result local_result =
11287 compare_dies(rdr, &l_type, &r_type,
11288 aggregates_being_compared,
11289 update_canonical_dies_on_the_fly);
11290 SET_RESULT_TO(result, local_result, l, r);
11291 }
11292 }
11293 else
11294 SET_RESULT_TO_FALSE(result, l, r);
11295 break;
11296
11297 case DW_TAG_inheritance:
11298 {
11299 Dwarf_Die l_type, r_type;
11300 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11301 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11302 result = compare_dies(rdr, &l_type, &r_type,
11303 aggregates_being_compared,
11304 update_canonical_dies_on_the_fly);
11305 if (!result)
11306 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11307
11308 uint64_t l_a = 0, r_a = 0;
11309 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11310 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11311 if (l_a != r_a)
11312 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11313
11314 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11315 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11316 if (l_a != r_a)
11317 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11318
11319 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11320 die_member_offset(rdr, l, l_offset_in_bits);
11321 die_member_offset(rdr, r, r_offset_in_bits);
11322 if (l_offset_in_bits != r_offset_in_bits)
11323 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11324 }
11325 break;
11326
11327 case DW_TAG_ptr_to_member_type:
11328 {
11329 bool comp_result = false;
11330 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11331 if (!comp_result)
11332 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11333
11334 Dwarf_Die l_type, r_type;
11335 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11336 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11337 result = compare_dies(rdr, &l_type, &r_type,
11338 aggregates_being_compared,
11339 update_canonical_dies_on_the_fly);
11340 if (!result)
11341 ABG_RETURN(result);
11342
11343 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11344 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11345 result = compare_dies(rdr, &l_type, &r_type,
11346 aggregates_being_compared,
11347 update_canonical_dies_on_the_fly);
11348 if (!result)
11349 ABG_RETURN(result);
11350 }
11351 break;
11352
11353 case DW_TAG_enumerator:
11354 case DW_TAG_packed_type:
11355 case DW_TAG_set_type:
11356 case DW_TAG_file_type:
11357 case DW_TAG_thrown_type:
11358 case DW_TAG_interface_type:
11359 case DW_TAG_shared_type:
11360 case DW_TAG_compile_unit:
11361 case DW_TAG_namespace:
11362 case DW_TAG_module:
11363 case DW_TAG_constant:
11364 case DW_TAG_partial_unit:
11365 case DW_TAG_imported_unit:
11366 case DW_TAG_dwarf_procedure:
11367 case DW_TAG_imported_declaration:
11368 case DW_TAG_entry_point:
11369 case DW_TAG_label:
11370 case DW_TAG_lexical_block:
11371 case DW_TAG_unspecified_parameters:
11372 case DW_TAG_variant:
11373 case DW_TAG_common_block:
11374 case DW_TAG_common_inclusion:
11375 case DW_TAG_inlined_subroutine:
11376 case DW_TAG_with_stmt:
11377 case DW_TAG_access_declaration:
11378 case DW_TAG_catch_block:
11379 case DW_TAG_friend:
11380 case DW_TAG_namelist:
11381 case DW_TAG_namelist_item:
11382 case DW_TAG_template_type_parameter:
11383 case DW_TAG_template_value_parameter:
11384 case DW_TAG_try_block:
11385 case DW_TAG_variant_part:
11386 case DW_TAG_imported_module:
11387 case DW_TAG_condition:
11388 case DW_TAG_type_unit:
11389 case DW_TAG_template_alias:
11390 case DW_TAG_lo_user:
11391 case DW_TAG_MIPS_loop:
11392 case DW_TAG_format_label:
11393 case DW_TAG_function_template:
11394 case DW_TAG_class_template:
11395 case DW_TAG_GNU_BINCL:
11396 case DW_TAG_GNU_EINCL:
11397 case DW_TAG_GNU_template_template_param:
11398 case DW_TAG_GNU_template_parameter_pack:
11399 case DW_TAG_GNU_formal_parameter_pack:
11400 case DW_TAG_GNU_call_site:
11401 case DW_TAG_GNU_call_site_parameter:
11402 case DW_TAG_hi_user:
11403 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11404 if (rdr.debug_die_canonicalization_is_on_)
11405 ABG_ASSERT_NOT_REACHED;
11406 #endif
11407 ABG_ASSERT_NOT_REACHED;
11408 break;
11409 }
11410
11411 ABG_RETURN(result);
11412 }
11413
11414 /// Compare two DIEs emitted by a C compiler.
11415 ///
11416 /// @param rdr the DWARF reader used to load the DWARF information.
11417 ///
11418 /// @param l the left-hand-side argument of this comparison operator.
11419 ///
11420 /// @param r the righ-hand-side argument of this comparison operator.
11421 ///
11422 /// @param update_canonical_dies_on_the_fly if yes, then this function
11423 /// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11424 /// comparing l and r. This helps in making so that sub-type DIEs of
11425 /// 'l' and 'r' are compared structurally only once. This is how we
11426 /// turn this exponential comparison problem into a problem that is a
11427 /// closer to a linear one.
11428 ///
11429 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11430 static comparison_result
compare_dies(const reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r,bool update_canonical_dies_on_the_fly)11431 compare_dies(const reader& rdr,
11432 const Dwarf_Die *l,
11433 const Dwarf_Die *r,
11434 bool update_canonical_dies_on_the_fly)
11435 {
11436 offset_pairs_stack_type aggregates_being_compared(rdr);
11437 return compare_dies(rdr, l, r, aggregates_being_compared,
11438 update_canonical_dies_on_the_fly);
11439 }
11440
11441 /// Compare two DIEs for the purpose of canonicalization.
11442 ///
11443 /// This is a sub-routine of reader::get_canonical_die.
11444 ///
11445 /// When DIE canonicalization debugging is on, this function performs
11446 /// both structural and canonical comparison. It expects that both
11447 /// comparison yield the same result.
11448 ///
11449 /// @param rdr the DWARF reader.
11450 ///
11451 /// @param l the left-hand-side comparison operand DIE.
11452 ///
11453 /// @param r the right-hand-side comparison operand DIE.
11454 ///
11455 /// @param update_canonical_dies_on_the_fly if true, then some
11456 /// aggregate DIEs will see their canonical types propagated.
11457 ///
11458 /// @return true iff @p l equals @p r.
11459 static bool
compare_dies_during_canonicalization(reader & rdr,const Dwarf_Die * l,const Dwarf_Die * r,bool update_canonical_dies_on_the_fly)11460 compare_dies_during_canonicalization(reader& rdr,
11461 const Dwarf_Die *l,
11462 const Dwarf_Die *r,
11463 bool update_canonical_dies_on_the_fly)
11464 {
11465 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11466 if (rdr.debug_die_canonicalization_is_on_)
11467 {
11468 bool canonical_equality = false, structural_equality = false;
11469 rdr.use_canonical_die_comparison_ = false;
11470 structural_equality = compare_dies(rdr, l, r,
11471 /*update_canonical_dies_on_the_fly=*/false);
11472 rdr.use_canonical_die_comparison_ = true;
11473 canonical_equality = compare_dies(rdr, l, r,
11474 update_canonical_dies_on_the_fly);
11475 if (canonical_equality != structural_equality)
11476 {
11477 std::cerr << "structural & canonical equality different for DIEs: "
11478 << std::hex
11479 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11480 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11481 << std::dec
11482 << ", repr: '"
11483 << rdr.get_die_pretty_type_representation(l, 0)
11484 << "'"
11485 << std::endl;
11486 ABG_ASSERT_NOT_REACHED;
11487 }
11488 return structural_equality;
11489 }
11490 #endif
11491 return compare_dies(rdr, l, r,
11492 update_canonical_dies_on_the_fly);
11493 }
11494
11495 // ----------------------------------
11496 // </die comparison engine>
11497 // ---------------------------------
11498
11499 /// Get the point where a DW_AT_import DIE is used to import a given
11500 /// (unit) DIE, between two DIEs.
11501 ///
11502 /// @param rdr the dwarf reader to consider.
11503 ///
11504 /// @param partial_unit_offset the imported unit for which we want to
11505 /// know the insertion point. This is usually a partial unit (with
11506 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11507 /// so.
11508 ///
11509 /// @param first_die_offset the offset of the DIE from which this
11510 /// function starts looking for the import point of
11511 /// @partial_unit_offset. Note that this offset is excluded from the
11512 /// set of potential solutions.
11513 ///
11514 /// @param first_die_cu_offset the offset of the (compilation) unit
11515 /// that @p first_die_cu_offset belongs to.
11516 ///
11517 /// @param source where the DIE of first_die_cu_offset unit comes
11518 /// from.
11519 ///
11520 /// @param last_die_offset the offset of the last DIE of the up to
11521 /// which this function looks for the import point of @p
11522 /// partial_unit_offset. Note that this offset is excluded from the
11523 /// set of potential solutions.
11524 ///
11525 /// @param imported_point_offset. The resulting
11526 /// imported_point_offset. Note that if the imported DIE @p
11527 /// partial_unit_offset is not found between @p first_die_offset and
11528 /// @p last_die_offset, this parameter is left untouched by this
11529 /// function.
11530 ///
11531 /// @return true iff an imported unit is found between @p
11532 /// first_die_offset and @p last_die_offset.
11533 static bool
find_import_unit_point_between_dies(const reader & rdr,size_t partial_unit_offset,Dwarf_Off first_die_offset,Dwarf_Off first_die_cu_offset,die_source source,size_t last_die_offset,size_t & imported_point_offset)11534 find_import_unit_point_between_dies(const reader& rdr,
11535 size_t partial_unit_offset,
11536 Dwarf_Off first_die_offset,
11537 Dwarf_Off first_die_cu_offset,
11538 die_source source,
11539 size_t last_die_offset,
11540 size_t& imported_point_offset)
11541 {
11542 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11543 rdr.tu_die_imported_unit_points_map(source);
11544
11545 tu_die_imported_unit_points_map_type::const_iterator iter =
11546 tu_die_imported_unit_points_map.find(first_die_cu_offset);
11547
11548 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11549
11550 const imported_unit_points_type& imported_unit_points = iter->second;
11551 if (imported_unit_points.empty())
11552 return false;
11553
11554 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11555 imported_unit_points_type::const_iterator e = imported_unit_points.end();
11556
11557 find_lower_bound_in_imported_unit_points(imported_unit_points,
11558 first_die_offset,
11559 b);
11560
11561 if (last_die_offset != static_cast<size_t>(-1))
11562 find_lower_bound_in_imported_unit_points(imported_unit_points,
11563 last_die_offset,
11564 e);
11565
11566 if (e != imported_unit_points.end())
11567 {
11568 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11569 if (i->imported_unit_die_off == partial_unit_offset)
11570 {
11571 imported_point_offset = i->offset_of_import ;
11572 return true;
11573 }
11574
11575 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11576 {
11577 if (find_import_unit_point_between_dies(rdr,
11578 partial_unit_offset,
11579 i->imported_unit_child_off,
11580 i->imported_unit_cu_off,
11581 i->imported_unit_die_source,
11582 /*(Dwarf_Off)*/-1,
11583 imported_point_offset))
11584 return true;
11585 }
11586 }
11587 else
11588 {
11589 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11590 if (i->imported_unit_die_off == partial_unit_offset)
11591 {
11592 imported_point_offset = i->offset_of_import ;
11593 return true;
11594 }
11595
11596 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11597 {
11598 if (find_import_unit_point_between_dies(rdr,
11599 partial_unit_offset,
11600 i->imported_unit_child_off,
11601 i->imported_unit_cu_off,
11602 i->imported_unit_die_source,
11603 /*(Dwarf_Off)*/-1,
11604 imported_point_offset))
11605 return true;
11606 }
11607 }
11608
11609 return false;
11610 }
11611
11612 /// In the current translation unit, get the last point where a
11613 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
11614 /// given DIE is found. That given DIE is called the limit DIE.
11615 ///
11616 /// Said otherwise, this function returns the last import point of a
11617 /// unit, before a limit.
11618 ///
11619 /// @param rdr the dwarf reader to consider.
11620 ///
11621 /// @param partial_unit_offset the imported unit for which we want to
11622 /// know the insertion point of. This is usually a partial unit (with
11623 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11624 /// so.
11625 ///
11626 /// @param where_offset the offset of the limit DIE.
11627 ///
11628 /// @param imported_point_offset. The resulting imported_point_offset.
11629 /// Note that if the imported DIE @p partial_unit_offset is not found
11630 /// before @p die_offset, this is set to the last @p
11631 /// partial_unit_offset found under @p parent_die.
11632 ///
11633 /// @return true iff an imported unit is found before @p die_offset.
11634 /// Note that if an imported unit is found after @p die_offset then @p
11635 /// imported_point_offset is set and the function return false.
11636 static bool
find_import_unit_point_before_die(const reader & rdr,size_t partial_unit_offset,size_t where_offset,size_t & imported_point_offset)11637 find_import_unit_point_before_die(const reader& rdr,
11638 size_t partial_unit_offset,
11639 size_t where_offset,
11640 size_t& imported_point_offset)
11641 {
11642 size_t import_point_offset = 0;
11643 Dwarf_Die first_die_of_tu;
11644
11645 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11646 &first_die_of_tu) != 0)
11647 return false;
11648
11649 Dwarf_Die cu_die_memory;
11650 Dwarf_Die *cu_die;
11651
11652 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
11653 &cu_die_memory, 0, 0);
11654
11655 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
11656 dwarf_dieoffset(&first_die_of_tu),
11657 dwarf_dieoffset(cu_die),
11658 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
11659 where_offset,
11660 import_point_offset))
11661 {
11662 imported_point_offset = import_point_offset;
11663 return true;
11664 }
11665
11666 if (import_point_offset)
11667 {
11668 imported_point_offset = import_point_offset;
11669 return true;
11670 }
11671
11672 return false;
11673 }
11674
11675 /// Return the parent DIE for a given DIE.
11676 ///
11677 /// Note that the function build_die_parent_map() must have been
11678 /// called before this one can work. This function either succeeds or
11679 /// aborts the current process.
11680 ///
11681 /// @param rdr the DWARF reader to consider.
11682 ///
11683 /// @param die the DIE for which we want the parent.
11684 ///
11685 /// @param parent_die the output parameter set to the parent die of
11686 /// @p die. Its memory must be allocated and handled by the caller.
11687 ///
11688 /// @param where_offset the offset of the DIE where we are "logically"
11689 /// positionned at, in the DIE tree. This is useful when @p die is
11690 /// e.g, DW_TAG_partial_unit that can be included in several places in
11691 /// the DIE tree.
11692 ///
11693 /// @return true if the function could get a parent DIE, false
11694 /// otherwise.
11695 static bool
get_parent_die(const reader & rdr,const Dwarf_Die * die,Dwarf_Die & parent_die,size_t where_offset)11696 get_parent_die(const reader& rdr,
11697 const Dwarf_Die* die,
11698 Dwarf_Die& parent_die,
11699 size_t where_offset)
11700 {
11701 ABG_ASSERT(rdr.dwarf_debug_info());
11702
11703 const die_source source = rdr.get_die_source(die);
11704
11705 const offset_offset_map_type& m = rdr.die_parent_map(source);
11706 offset_offset_map_type::const_iterator i =
11707 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
11708
11709 if (i == m.end())
11710 return false;
11711
11712 switch (source)
11713 {
11714 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
11715 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11716 i->second, &parent_die));
11717 break;
11718 case ALT_DEBUG_INFO_DIE_SOURCE:
11719 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
11720 i->second, &parent_die));
11721 break;
11722 case TYPE_UNIT_DIE_SOURCE:
11723 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11724 i->second, &parent_die));
11725 break;
11726 case NO_DEBUG_INFO_DIE_SOURCE:
11727 case NUMBER_OF_DIE_SOURCES:
11728 ABG_ASSERT_NOT_REACHED;
11729 }
11730
11731 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
11732 {
11733 if (where_offset == 0)
11734 {
11735 parent_die = *rdr.cur_tu_die();
11736 return true;
11737 }
11738 size_t import_point_offset = 0;
11739 bool found =
11740 find_import_unit_point_before_die(rdr,
11741 dwarf_dieoffset(&parent_die),
11742 where_offset,
11743 import_point_offset);
11744 if (!found)
11745 // It looks like parent_die (which comes from the alternate
11746 // debug info file) hasn't been imported into this TU. So,
11747 // Let's assume its logical parent is the DIE of the current
11748 // TU.
11749 parent_die = *rdr.cur_tu_die();
11750 else
11751 {
11752 ABG_ASSERT(import_point_offset);
11753 Dwarf_Die import_point_die;
11754 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11755 import_point_offset,
11756 &import_point_die));
11757 return get_parent_die(rdr, &import_point_die,
11758 parent_die, where_offset);
11759 }
11760 }
11761
11762 return true;
11763 }
11764
11765 /// Get the DIE representing the scope of a given DIE.
11766 ///
11767 /// Please note that when the DIE we are looking at has a
11768 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
11769 /// DIE is the parent DIE of the DIE referred to by that attribute.
11770 /// This is the only case where a scope DIE is different from the
11771 /// parent DIE of a given DIE.
11772 ///
11773 /// Also note that if the current translation unit is from C, then
11774 /// this returns the global scope.
11775 ///
11776 /// @param rdr the DWARF reader to use.
11777 ///
11778 /// @param die the DIE to consider.
11779 ///
11780 /// @param where_offset where we are logically at in the DIE stream.
11781 ///
11782 /// @param scope_die out parameter. This is set to the resulting
11783 /// scope DIE iff the function returns true.
11784 static bool
get_scope_die(const reader & rdr,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & scope_die)11785 get_scope_die(const reader& rdr,
11786 const Dwarf_Die* die,
11787 size_t where_offset,
11788 Dwarf_Die& scope_die)
11789 {
11790 if (is_c_language(rdr.cur_transl_unit()->get_language()))
11791 {
11792 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
11793 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
11794 }
11795
11796 Dwarf_Die logical_parent_die;
11797 if (die_die_attribute(die, DW_AT_specification,
11798 logical_parent_die, false)
11799 || die_die_attribute(die, DW_AT_abstract_origin,
11800 logical_parent_die, false))
11801 return get_scope_die(rdr, &logical_parent_die, where_offset, scope_die);
11802
11803 if (!get_parent_die(rdr, die, scope_die, where_offset))
11804 return false;
11805
11806 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
11807 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
11808 || dwarf_tag(&scope_die) == DW_TAG_array_type)
11809 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
11810
11811 return true;
11812 }
11813
11814 /// Return the abigail IR node representing the scope of a given DIE.
11815 ///
11816 /// Note that it is the logical scope that is returned. That is, if
11817 /// the DIE has a DW_AT_specification or DW_AT_abstract_origin
11818 /// attribute, it's the scope of the referred-to DIE (via these
11819 /// attributes) that is returned.
11820 ///
11821 /// Also note that if the current translation unit is from C, then
11822 /// this returns the global scope.
11823 ///
11824 /// @param rdr the dwarf reader to use.
11825 ///
11826 /// @param die the DIE to get the scope for.
11827 ///
11828 /// @param called_from_public_decl is true if this function has been
11829 /// initially called within the context of a public decl.
11830 ///
11831 /// @param where_offset the offset of the DIE where we are "logically"
11832 /// positionned at, in the DIE tree. This is useful when @p die is
11833 /// e.g, DW_TAG_partial_unit that can be included in several places in
11834 /// the DIE tree.
11835 static scope_decl_sptr
get_scope_for_die(reader & rdr,Dwarf_Die * die,bool called_for_public_decl,size_t where_offset)11836 get_scope_for_die(reader& rdr,
11837 Dwarf_Die* die,
11838 bool called_for_public_decl,
11839 size_t where_offset)
11840 {
11841 const die_source source_of_die = rdr.get_die_source(die);
11842
11843 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11844 rdr.get_die_language(die, die_lang);
11845 if (is_c_language(die_lang)
11846 || rdr.die_parent_map(source_of_die).empty())
11847 {
11848 // In units for the C languages all decls belong to the global
11849 // namespace. This is generally the case if Libabigail
11850 // determined that no DIE -> parent map was needed.
11851 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11852 return rdr.global_scope();
11853 }
11854
11855 Dwarf_Die cloned_die;
11856 if (die_die_attribute(die, DW_AT_specification, cloned_die, false)
11857 || die_die_attribute(die, DW_AT_abstract_origin, cloned_die, false))
11858 return get_scope_for_die(rdr, &cloned_die,
11859 called_for_public_decl,
11860 where_offset);
11861
11862 Dwarf_Die parent_die;
11863
11864 if (!get_parent_die(rdr, die, parent_die, where_offset))
11865 return rdr.nil_scope();
11866
11867 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
11868 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
11869 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11870 {
11871 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
11872 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11873 {
11874 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
11875 || source_of_die == TYPE_UNIT_DIE_SOURCE);
11876 return rdr.cur_transl_unit()->get_global_scope();
11877 }
11878
11879 // For top level DIEs like DW_TAG_compile_unit, we just want to
11880 // return the global scope for the corresponding translation
11881 // unit. This must have been set by
11882 // build_translation_unit_and_add_to_ir if we already started to
11883 // build the translation unit of parent_die. Otherwise, just
11884 // return the global scope of the current translation unit.
11885 die_tu_map_type::const_iterator i =
11886 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
11887 if (i != rdr.die_tu_map().end())
11888 return i->second->get_global_scope();
11889 return rdr.cur_transl_unit()->get_global_scope();
11890 }
11891
11892 scope_decl_sptr s;
11893 type_or_decl_base_sptr d;
11894 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
11895 || dwarf_tag(&parent_die) == DW_TAG_array_type
11896 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
11897 // this is an entity defined in a scope that is a function.
11898 // Normally, I would say that this should be dropped. But I have
11899 // seen a case where a typedef DIE needed by a function parameter
11900 // was defined right before the parameter, under the scope of the
11901 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
11902 // the function parm too. So for that case, let's say that the
11903 // scope is the scope of the function itself. Note that this is
11904 // an error of the DWARF emitter. We should never see this DIE in
11905 // this context.
11906 {
11907 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
11908 called_for_public_decl,
11909 where_offset);
11910 if (is_anonymous_type_die(die))
11911 // For anonymous type that have nothing to do in a function or
11912 // array type context, let's put it in the containing
11913 // namespace. That is, do not let it be in a containing class
11914 // or union where it has nothing to do.
11915 while (is_class_or_union_type(s))
11916 {
11917 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
11918 return rdr.nil_scope();
11919 s = get_scope_for_die(rdr, &parent_die,
11920 called_for_public_decl,
11921 where_offset);
11922 }
11923 return s;
11924 }
11925 else
11926 d = build_ir_node_from_die(rdr, &parent_die,
11927 called_for_public_decl,
11928 where_offset);
11929 s = dynamic_pointer_cast<scope_decl>(d);
11930 if (!s)
11931 // this is an entity defined in someting that is not a scope.
11932 // Let's drop it.
11933 return rdr.nil_scope();
11934
11935 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
11936 if (cl && cl->get_is_declaration_only())
11937 {
11938 scope_decl_sptr scop =
11939 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
11940 if (scop)
11941 s = scop;
11942 else
11943 s = cl;
11944 }
11945 return s;
11946 }
11947
11948 /// Convert a DWARF constant representing the value of the
11949 /// DW_AT_language property into the translation_unit::language
11950 /// enumerator.
11951 ///
11952 /// @param l the DWARF constant to convert.
11953 ///
11954 /// @return the resulting translation_unit::language enumerator.
11955 static translation_unit::language
dwarf_language_to_tu_language(size_t l)11956 dwarf_language_to_tu_language(size_t l)
11957 {
11958 switch (l)
11959 {
11960 case DW_LANG_C89:
11961 return translation_unit::LANG_C89;
11962 case DW_LANG_C:
11963 return translation_unit::LANG_C;
11964 case DW_LANG_Ada83:
11965 return translation_unit::LANG_Ada83;
11966 case DW_LANG_C_plus_plus:
11967 return translation_unit::LANG_C_plus_plus;
11968 case DW_LANG_Cobol74:
11969 return translation_unit::LANG_Cobol74;
11970 case DW_LANG_Cobol85:
11971 return translation_unit::LANG_Cobol85;
11972 case DW_LANG_Fortran77:
11973 return translation_unit::LANG_Fortran77;
11974 case DW_LANG_Fortran90:
11975 return translation_unit::LANG_Fortran90;
11976 case DW_LANG_Pascal83:
11977 return translation_unit::LANG_Pascal83;
11978 case DW_LANG_Modula2:
11979 return translation_unit::LANG_Modula2;
11980 case DW_LANG_Java:
11981 return translation_unit::LANG_Java;
11982 case DW_LANG_C99:
11983 return translation_unit::LANG_C99;
11984 case DW_LANG_Ada95:
11985 return translation_unit::LANG_Ada95;
11986 case DW_LANG_Fortran95:
11987 return translation_unit::LANG_Fortran95;
11988 case DW_LANG_PLI:
11989 return translation_unit::LANG_PLI;
11990 case DW_LANG_ObjC:
11991 return translation_unit::LANG_ObjC;
11992 case DW_LANG_ObjC_plus_plus:
11993 return translation_unit::LANG_ObjC_plus_plus;
11994
11995 #ifdef HAVE_DW_LANG_Rust_enumerator
11996 case DW_LANG_Rust:
11997 return translation_unit::LANG_Rust;
11998 #endif
11999
12000 #ifdef HAVE_DW_LANG_UPC_enumerator
12001 case DW_LANG_UPC:
12002 return translation_unit::LANG_UPC;
12003 #endif
12004
12005 #ifdef HAVE_DW_LANG_D_enumerator
12006 case DW_LANG_D:
12007 return translation_unit::LANG_D;
12008 #endif
12009
12010 #ifdef HAVE_DW_LANG_Python_enumerator
12011 case DW_LANG_Python:
12012 return translation_unit::LANG_Python;
12013 #endif
12014
12015 #ifdef HAVE_DW_LANG_Go_enumerator
12016 case DW_LANG_Go:
12017 return translation_unit::LANG_Go;
12018 #endif
12019
12020 #ifdef HAVE_DW_LANG_C11_enumerator
12021 case DW_LANG_C11:
12022 return translation_unit::LANG_C11;
12023 #endif
12024
12025 #ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
12026 case DW_LANG_C_plus_plus_03:
12027 return translation_unit::LANG_C_plus_plus_03;
12028 #endif
12029
12030 #ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
12031 case DW_LANG_C_plus_plus_11:
12032 return translation_unit::LANG_C_plus_plus_11;
12033 #endif
12034
12035 #ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
12036 case DW_LANG_C_plus_plus_14:
12037 return translation_unit::LANG_C_plus_plus_14;
12038 #endif
12039
12040 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
12041 case DW_LANG_Mips_Assembler:
12042 return translation_unit::LANG_Mips_Assembler;
12043 #endif
12044
12045 default:
12046 return translation_unit::LANG_UNKNOWN;
12047 }
12048 }
12049
12050 /// Get the default array lower bound value as defined by the DWARF
12051 /// specification, version 4, depending on the language of the
12052 /// translation unit.
12053 ///
12054 /// @param l the language of the translation unit.
12055 ///
12056 /// @return the default array lower bound value.
12057 static uint64_t
get_default_array_lower_bound(translation_unit::language l)12058 get_default_array_lower_bound(translation_unit::language l)
12059 {
12060 int value = 0;
12061 switch (l)
12062 {
12063 case translation_unit::LANG_UNKNOWN:
12064 value = 0;
12065 break;
12066 case translation_unit::LANG_Cobol74:
12067 case translation_unit::LANG_Cobol85:
12068 value = 1;
12069 break;
12070 case translation_unit::LANG_C89:
12071 case translation_unit::LANG_C99:
12072 case translation_unit::LANG_C11:
12073 case translation_unit::LANG_C:
12074 case translation_unit::LANG_C_plus_plus_03:
12075 case translation_unit::LANG_C_plus_plus_11:
12076 case translation_unit::LANG_C_plus_plus_14:
12077 case translation_unit::LANG_C_plus_plus:
12078 case translation_unit::LANG_ObjC:
12079 case translation_unit::LANG_ObjC_plus_plus:
12080 case translation_unit::LANG_Rust:
12081 value = 0;
12082 break;
12083 case translation_unit::LANG_Fortran77:
12084 case translation_unit::LANG_Fortran90:
12085 case translation_unit::LANG_Fortran95:
12086 case translation_unit::LANG_Ada83:
12087 case translation_unit::LANG_Ada95:
12088 case translation_unit::LANG_Pascal83:
12089 case translation_unit::LANG_Modula2:
12090 value = 1;
12091 break;
12092 case translation_unit::LANG_Java:
12093 value = 0;
12094 break;
12095 case translation_unit::LANG_PLI:
12096 value = 1;
12097 break;
12098 case translation_unit::LANG_UPC:
12099 case translation_unit::LANG_D:
12100 case translation_unit::LANG_Python:
12101 case translation_unit::LANG_Go:
12102 case translation_unit::LANG_Mips_Assembler:
12103 value = 0;
12104 break;
12105 }
12106
12107 return value;
12108 }
12109
12110 /// For a given offset, find the lower bound of a sorted vector of
12111 /// imported unit point offset.
12112 ///
12113 /// The lower bound is the smallest point (the point with the smallest
12114 /// offset) which is the greater than a given offset.
12115 ///
12116 /// @param imported_unit_points_type the sorted vector of imported
12117 /// unit points.
12118 ///
12119 /// @param val the offset to consider when looking for the lower
12120 /// bound.
12121 ///
12122 /// @param r an iterator to the lower bound found. This parameter is
12123 /// set iff the function returns true.
12124 ///
12125 /// @return true iff the lower bound has been found.
12126 static bool
find_lower_bound_in_imported_unit_points(const imported_unit_points_type & p,Dwarf_Off val,imported_unit_points_type::const_iterator & r)12127 find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
12128 Dwarf_Off val,
12129 imported_unit_points_type::const_iterator& r)
12130 {
12131 imported_unit_point v(val);
12132 imported_unit_points_type::const_iterator result =
12133 std::lower_bound(p.begin(), p.end(), v);
12134
12135 bool is_ok = result != p.end();
12136
12137 if (is_ok)
12138 r = result;
12139
12140 return is_ok;
12141 }
12142
12143 /// Given a DW_TAG_compile_unit, build and return the corresponding
12144 /// abigail::translation_unit ir node. Note that this function
12145 /// recursively reads the children dies of the current DIE and
12146 /// populates the resulting translation unit.
12147 ///
12148 /// @param rdr the DWARF reader to use.
12149 ///
12150 /// @param die the DW_TAG_compile_unit DIE to consider.
12151 ///
12152 /// @param address_size the size of the addresses expressed in this
12153 /// translation unit in general.
12154 ///
12155 /// @return a pointer to the resulting translation_unit.
12156 static translation_unit_sptr
build_translation_unit_and_add_to_ir(reader & rdr,Dwarf_Die * die,char address_size)12157 build_translation_unit_and_add_to_ir(reader& rdr,
12158 Dwarf_Die* die,
12159 char address_size)
12160 {
12161 translation_unit_sptr result;
12162
12163 if (!die)
12164 return result;
12165 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
12166
12167 // Clear the part of the context that is dependent on the translation
12168 // unit we are reading.
12169 rdr.clear_per_translation_unit_data();
12170
12171 rdr.cur_tu_die(die);
12172
12173 string path = die_string_attribute(die, DW_AT_name);
12174 if (path == "<artificial>")
12175 {
12176 // This is a file artificially generated by the compiler, so its
12177 // name is '<artificial>'. As we want all different translation
12178 // units to have unique path names, let's suffix this path name
12179 // with its die offset.
12180 std::ostringstream o;
12181 o << path << "-" << std::hex << dwarf_dieoffset(die);
12182 path = o.str();
12183 }
12184 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
12185
12186 // See if the same translation unit exits already in the current
12187 // corpus. Sometimes, the same translation unit can be present
12188 // several times in the same debug info. The content of the
12189 // different instances of the translation unit are different. So to
12190 // represent that, we are going to re-use the same translation
12191 // unit. That is, it's going to be the union of all the translation
12192 // units of the same path.
12193 {
12194 const string& abs_path =
12195 compilation_dir.empty() ? path : compilation_dir + "/" + path;
12196 result = rdr.corpus()->find_translation_unit(abs_path);
12197 }
12198
12199 if (!result)
12200 {
12201 result.reset(new translation_unit(rdr.env(),
12202 path,
12203 address_size));
12204 result->set_compilation_dir_path(compilation_dir);
12205 rdr.corpus()->add(result);
12206 uint64_t l = 0;
12207 die_unsigned_constant_attribute(die, DW_AT_language, l);
12208 result->set_language(dwarf_language_to_tu_language(l));
12209 }
12210
12211 rdr.cur_transl_unit(result);
12212 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
12213
12214 Dwarf_Die child;
12215 if (dwarf_child(die, &child) != 0)
12216 return result;
12217
12218 result->set_is_constructed(false);
12219
12220 do
12221 // Analyze all the DIEs we encounter unless we are asked to only
12222 // analyze exported interfaces and the types reachables from them.
12223 if (!rdr.env().analyze_exported_interfaces_only()
12224 || rdr.is_decl_die_with_exported_symbol(&child))
12225 build_ir_node_from_die(rdr, &child,
12226 die_is_public_decl(&child),
12227 dwarf_dieoffset(&child));
12228 while (dwarf_siblingof(&child, &child) == 0);
12229
12230 if (!rdr.var_decls_to_re_add_to_tree().empty())
12231 for (list<var_decl_sptr>::const_iterator v =
12232 rdr.var_decls_to_re_add_to_tree().begin();
12233 v != rdr.var_decls_to_re_add_to_tree().end();
12234 ++v)
12235 {
12236 if (is_member_decl(*v))
12237 continue;
12238
12239 ABG_ASSERT((*v)->get_scope());
12240 string demangled_name =
12241 demangle_cplus_mangled_name((*v)->get_linkage_name());
12242 if (!demangled_name.empty())
12243 {
12244 std::list<string> fqn_comps;
12245 fqn_to_components(demangled_name, fqn_comps);
12246 string mem_name = fqn_comps.back();
12247 fqn_comps.pop_back();
12248 class_decl_sptr class_type;
12249 string ty_name;
12250 if (!fqn_comps.empty())
12251 {
12252 ty_name = components_to_type_name(fqn_comps);
12253 class_type =
12254 lookup_class_type(ty_name, *rdr.cur_transl_unit());
12255 }
12256 if (class_type)
12257 {
12258 // So we are seeing a member variable for which there
12259 // is a global variable definition DIE not having a
12260 // reference attribute pointing back to the member
12261 // variable declaration DIE. Thus remove the global
12262 // variable definition from its current non-class
12263 // scope ...
12264 decl_base_sptr d;
12265 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12266 // This is the data member with the same name in cl.
12267 // We just need to flag it as static.
12268 ;
12269 else
12270 {
12271 // In this case there is no data member with the
12272 // same name in cl already. Let's add it there then
12273 // ...
12274 remove_decl_from_scope(*v);
12275 d = add_decl_to_scope(*v, class_type);
12276 }
12277
12278 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12279 // Let's flag the data member as static.
12280 set_member_is_static(d, true);
12281 }
12282 }
12283 }
12284 rdr.var_decls_to_re_add_to_tree().clear();
12285
12286 result->set_is_constructed(true);
12287
12288 return result;
12289 }
12290
12291 /// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12292 /// DW_TAG_module (for fortran) DIE.
12293 ///
12294 /// Note that this function connects the DW_TAG_namespace to the IR
12295 /// being currently created, reads the children of the DIE and
12296 /// connects them to the IR as well.
12297 ///
12298 /// @param rdr the DWARF reader to use.
12299 ///
12300 /// @param die the DIE to read from. Must be either DW_TAG_namespace
12301 /// or DW_TAG_module.
12302 ///
12303 /// @param where_offset the offset of the DIE where we are "logically"
12304 /// positionned at, in the DIE tree. This is useful when @p die is
12305 /// e.g, DW_TAG_partial_unit that can be included in several places in
12306 /// the DIE tree.
12307 ///
12308 /// @return the resulting @ref abigail::namespace_decl or NULL if it
12309 /// couldn't be created.
12310 static namespace_decl_sptr
build_namespace_decl_and_add_to_ir(reader & rdr,Dwarf_Die * die,size_t where_offset)12311 build_namespace_decl_and_add_to_ir(reader& rdr,
12312 Dwarf_Die* die,
12313 size_t where_offset)
12314 {
12315 namespace_decl_sptr result;
12316
12317 if (!die)
12318 return result;
12319
12320 unsigned tag = dwarf_tag(die);
12321 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12322 return result;
12323
12324 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12325 /*called_for_public_decl=*/false,
12326 where_offset);
12327
12328 string name, linkage_name;
12329 location loc;
12330 die_loc_and_name(rdr, die, loc, name, linkage_name);
12331
12332 result.reset(new namespace_decl(rdr.env(), name, loc));
12333 add_decl_to_scope(result, scope.get());
12334 rdr.associate_die_to_decl(die, result, where_offset);
12335
12336 Dwarf_Die child;
12337 if (dwarf_child(die, &child) != 0)
12338 return result;
12339
12340 rdr.scope_stack().push(result.get());
12341 do
12342 build_ir_node_from_die(rdr, &child,
12343 // If this namespace DIE is private
12344 // (anonymous) then all its content is
12345 // considered private. Otherwise, its
12346 // public decls are considered public.
12347 /*called_from_public_decl=*/
12348 die_is_public_decl(die) && die_is_public_decl(&child),
12349 where_offset);
12350 while (dwarf_siblingof(&child, &child) == 0);
12351 rdr.scope_stack().pop();
12352
12353 return result;
12354 }
12355
12356 /// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12357 ///
12358 /// @param rdr the DWARF reader to use.
12359 ///
12360 /// @param die the DW_TAG_base_type to consider.
12361 ///
12362 /// @param where_offset where we are logically at in the DIE stream.
12363 ///
12364 /// @return the resulting decl_base_sptr.
12365 static type_decl_sptr
build_type_decl(reader & rdr,Dwarf_Die * die,size_t where_offset)12366 build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12367 {
12368 type_decl_sptr result;
12369
12370 if (!die)
12371 return result;
12372 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12373
12374 uint64_t byte_size = 0, bit_size = 0;
12375 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12376 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12377 return result;
12378
12379 if (bit_size == 0 && byte_size != 0)
12380 // Update the bit size.
12381 bit_size = byte_size * 8;
12382
12383 string type_name, linkage_name;
12384 location loc;
12385 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12386
12387 if (byte_size == 0)
12388 {
12389 // The size of the type is zero, that must mean that we are
12390 // looking at the definition of the void type.
12391 if (type_name == "void")
12392 result = is_type_decl(build_ir_node_for_void_type(rdr));
12393 else
12394 // A type of size zero that is not void? Hmmh, I am not sure
12395 // what that means. Return nil for now.
12396 return result;
12397 }
12398
12399 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12400 {
12401 string normalized_type_name = type_name;
12402 integral_type int_type;
12403 if (parse_integral_type(type_name, int_type))
12404 normalized_type_name = int_type.to_string();
12405 result = lookup_basic_type(normalized_type_name, *corp);
12406 }
12407
12408 if (!result)
12409 if (corpus_sptr corp = rdr.corpus())
12410 result = lookup_basic_type(type_name, *corp);
12411 if (!result)
12412 result.reset(new type_decl(rdr.env(), type_name, bit_size,
12413 /*alignment=*/0, loc, linkage_name));
12414 rdr.associate_die_to_type(die, result, where_offset);
12415 return result;
12416 }
12417
12418 /// Construct the type that is to be used as the underlying type of an
12419 /// enum.
12420 ///
12421 /// @param rdr the DWARF reader to use.
12422 ///
12423 /// @param enum_name the name of the enum that this type is going to
12424 /// be the underlying type of.
12425 ///
12426 /// @param enum_size the size of the enum.
12427 ///
12428 /// @param is_anonymous whether the underlying type is anonymous or
12429 /// not. By default, this should be set to true as before c++11 (and
12430 /// in C), it's almost the case.
12431 static type_decl_sptr
build_enum_underlying_type(reader & rdr,string enum_name,uint64_t enum_size,bool is_anonymous=true)12432 build_enum_underlying_type(reader& rdr,
12433 string enum_name,
12434 uint64_t enum_size,
12435 bool is_anonymous = true)
12436 {
12437 string underlying_type_name =
12438 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12439 enum_size);
12440
12441 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12442 enum_size, enum_size, location()));
12443 result->set_is_anonymous(is_anonymous);
12444 result->set_is_artificial(true);
12445 translation_unit_sptr tu = rdr.cur_transl_unit();
12446 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12447 result = dynamic_pointer_cast<type_decl>(d);
12448 ABG_ASSERT(result);
12449 canonicalize(result);
12450 return result;
12451 }
12452
12453 /// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12454 ///
12455 /// @param rdr the DWARF reader to use.
12456 ///
12457 /// @param die the DIE to read from.
12458 ///
12459 /// @param scope the scope of the final enum. Note that this function
12460 /// does *NOT* add the built type to this scope. The scope is just so
12461 /// that the function knows how to name anonymous enums.
12462 ///
12463 /// @param is_declaration_only is true if the DIE denoted by @p die is
12464 /// a declaration-only DIE.
12465 ///
12466 /// @return the built enum_type_decl or NULL if it could not be built.
12467 static enum_type_decl_sptr
build_enum_type(reader & rdr,Dwarf_Die * die,scope_decl * scope,size_t where_offset,bool is_declaration_only)12468 build_enum_type(reader& rdr,
12469 Dwarf_Die* die,
12470 scope_decl* scope,
12471 size_t where_offset,
12472 bool is_declaration_only)
12473 {
12474 enum_type_decl_sptr result;
12475 if (!die)
12476 return result;
12477
12478 unsigned tag = dwarf_tag(die);
12479 if (tag != DW_TAG_enumeration_type)
12480 return result;
12481
12482 string name, linkage_name;
12483 location loc;
12484 die_loc_and_name(rdr, die, loc, name, linkage_name);
12485
12486 bool is_anonymous = false;
12487 // If the enum is anonymous, let's give it a name.
12488 if (name.empty())
12489 {
12490 name = get_internal_anonymous_die_prefix_name(die);
12491 ABG_ASSERT(!name.empty());
12492 // But we remember that the type is anonymous.
12493 is_anonymous = true;
12494
12495 if (size_t s = scope->get_num_anonymous_member_enums())
12496 name = build_internal_anonymous_die_name(name, s);
12497 }
12498
12499 bool use_odr = rdr.odr_is_relevant(die);
12500 // If the type has location, then associate it to its
12501 // representation. This way, all occurences of types with the same
12502 // representation (name) and location can be later detected as being
12503 // for the same type.
12504
12505 if (!is_anonymous)
12506 {
12507 if (use_odr)
12508 {
12509 if (enum_type_decl_sptr pre_existing_enum =
12510 is_enum_type(rdr.lookup_artifact_from_die(die)))
12511 result = pre_existing_enum;
12512 }
12513 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12514 {
12515 if (loc)
12516 result = lookup_enum_type_per_location(loc.expand(), *corp);
12517 }
12518 else if (loc)
12519 {
12520 if (enum_type_decl_sptr pre_existing_enum =
12521 is_enum_type(rdr.lookup_artifact_from_die(die)))
12522 if (pre_existing_enum->get_location() == loc)
12523 result = pre_existing_enum;
12524 }
12525
12526 if (result)
12527 {
12528 rdr.associate_die_to_type(die, result, where_offset);
12529 return result;
12530 }
12531 }
12532 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12533 // we can look them up?
12534
12535 uint64_t size = 0;
12536 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12537 size *= 8;
12538 bool is_artificial = die_is_artificial(die);
12539
12540 // for now we consider that underlying types of enums are all anonymous
12541 bool enum_underlying_type_is_anonymous= true;
12542
12543 enum_type_decl::enumerators enms;
12544 Dwarf_Die child;
12545 if (dwarf_child(die, &child) == 0)
12546 {
12547 do
12548 {
12549 if (dwarf_tag(&child) != DW_TAG_enumerator)
12550 continue;
12551
12552 string n, m;
12553 location l;
12554 die_loc_and_name(rdr, &child, l, n, m);
12555 uint64_t val = 0;
12556 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12557 enms.push_back(enum_type_decl::enumerator(n, val));
12558 }
12559 while (dwarf_siblingof(&child, &child) == 0);
12560 }
12561
12562 // DWARF up to version 4 (at least) doesn't seem to carry the
12563 // underlying type, so let's create an artificial one here, which
12564 // sole purpose is to be passed to the constructor of the
12565 // enum_type_decl type.
12566 type_decl_sptr t =
12567 build_enum_underlying_type(rdr, name, size,
12568 enum_underlying_type_is_anonymous);
12569 t->set_is_declaration_only(is_declaration_only);
12570
12571 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12572 result->set_is_anonymous(is_anonymous);
12573 result->set_is_declaration_only(is_declaration_only);
12574 result->set_is_artificial(is_artificial);
12575 rdr.associate_die_to_type(die, result, where_offset);
12576
12577 rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12578
12579 return result;
12580 }
12581
12582 /// Once a function_decl has been built and added to a class as a
12583 /// member function, this function updates the information of the
12584 /// function_decl concerning the properties of its relationship with
12585 /// the member class. That is, it updates properties like
12586 /// virtualness, access, constness, cdtorness, etc ...
12587 ///
12588 /// @param die the DIE of the function_decl that has been just built.
12589 ///
12590 /// @param f the function_decl that has just been built from @p die.
12591 ///
12592 /// @param klass the @ref class_or_union that @p f belongs to.
12593 ///
12594 /// @param rdr the context used to read the ELF/DWARF information.
12595 static void
finish_member_function_reading(Dwarf_Die * die,const function_decl_sptr & f,const class_or_union_sptr klass,reader & rdr)12596 finish_member_function_reading(Dwarf_Die* die,
12597 const function_decl_sptr& f,
12598 const class_or_union_sptr klass,
12599 reader& rdr)
12600 {
12601 ABG_ASSERT(klass);
12602
12603 method_decl_sptr m = is_method_decl(f);
12604 ABG_ASSERT(m);
12605
12606 method_type_sptr method_t = is_method_type(m->get_type());
12607 ABG_ASSERT(method_t);
12608
12609 bool is_ctor = (f->get_name() == klass->get_name());
12610 bool is_dtor = (!f->get_name().empty()
12611 && static_cast<string>(f->get_name())[0] == '~');
12612 bool is_virtual = die_is_virtual(die);
12613 int64_t vindex = -1;
12614 if (is_virtual)
12615 die_virtual_function_index(die, vindex);
12616 access_specifier access = public_access;
12617 if (class_decl_sptr c = is_class_type(klass))
12618 if (!c->is_struct())
12619 access = private_access;
12620 die_access_specifier(die, access);
12621
12622 bool is_static = false;
12623 {
12624 // Let's see if the first parameter is a pointer to an instance of
12625 // the same class type as the current class and has a
12626 // DW_AT_artificial attribute flag set. We are not looking at
12627 // DW_AT_object_pointer (for DWARF 3) because it wasn't being
12628 // emitted in GCC 4_4, which was already DWARF 3.
12629 function_decl::parameter_sptr first_parm;
12630 if (!f->get_parameters().empty())
12631 first_parm = f->get_parameters()[0];
12632
12633 bool is_artificial = first_parm && first_parm->get_is_artificial();
12634 type_base_sptr this_ptr_type, other_klass;
12635
12636 if (is_artificial)
12637 this_ptr_type = first_parm->get_type();
12638
12639 // Sometimes, the type of the "this" pointer is "const class_type* const".
12640 //
12641 // Meaning that the "this pointer" itself is const qualified. So
12642 // let's get the underlying underlying non-qualified pointer.
12643 if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
12644 this_ptr_type = q->get_underlying_type();
12645
12646 // Now, get the pointed-to type.
12647 if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
12648 other_klass = p->get_pointed_to_type();
12649
12650 // Sometimes, other_klass can be qualified; e.g, volatile. In
12651 // that case, let's get the unqualified version of other_klass.
12652 if (qualified_type_def_sptr q = is_qualified_type(other_klass))
12653 other_klass = q->get_underlying_type();
12654
12655 if (other_klass
12656 && get_type_name(other_klass) == klass->get_qualified_name())
12657 ;
12658 else
12659 is_static = true;
12660
12661 if (is_static)
12662 {
12663 // If we are looking at a DWARF version that is high enough
12664 // for the DW_AT_object_pointer attribute to be present, let's
12665 // see if it's present. If it is, then the current member
12666 // function is not static.
12667 Dwarf_Die object_pointer_die;
12668 if (die_has_object_pointer(die, object_pointer_die))
12669 is_static = false;
12670 }
12671 }
12672 set_member_access_specifier(m, access);
12673 if (vindex != -1)
12674 set_member_function_vtable_offset(m, vindex);
12675 if (is_virtual)
12676 set_member_function_is_virtual(m, is_virtual);
12677 set_member_is_static(m, is_static);
12678 set_member_function_is_ctor(m, is_ctor);
12679 set_member_function_is_dtor(m, is_dtor);
12680 set_member_function_is_const(m, method_t->get_is_const());
12681
12682 ABG_ASSERT(is_member_function(m));
12683
12684 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
12685 {
12686 // This is a virtual member function which has a linkage name
12687 // but has no underlying symbol set.
12688 //
12689 // The underlying elf symbol to set to this function can show up
12690 // later in the DWARF input or it can be that, because of some
12691 // compiler optimization, the relation between this function and
12692 // its underlying elf symbol is simply not emitted in the DWARF.
12693 //
12694 // Let's thus schedule this function for a later fixup pass
12695 // (performed by
12696 // reader::fixup_functions_with_no_symbols()) that will
12697 // set its underlying symbol.
12698 //
12699 // Note that if the underying symbol is encountered later in the
12700 // DWARF input, then the part of build_function_decl() that
12701 // updates the function to set its underlying symbol will
12702 // de-schedule this function wrt fixup pass.
12703 Dwarf_Off die_offset = dwarf_dieoffset(die);
12704 die_function_decl_map_type &fns_with_no_symbol =
12705 rdr.die_function_decl_with_no_symbol_map();
12706 die_function_decl_map_type::const_iterator i =
12707 fns_with_no_symbol.find(die_offset);
12708 if (i == fns_with_no_symbol.end())
12709 fns_with_no_symbol[die_offset] = f;
12710 }
12711
12712 }
12713
12714 /// If a function DIE has attributes which have not yet been read and
12715 /// added to the internal representation that represents that function
12716 /// then read those extra attributes and update the internal
12717 /// representation.
12718 ///
12719 /// @param rdr the DWARF reader to use.
12720 ///
12721 /// @param die the function DIE to consider.
12722 ///
12723 /// @param where_offset where we logical are, currently, in the stream
12724 /// of DIEs. If you don't know what this is, you can just set it to zero.
12725 ///
12726 /// @param existing_fn the representation of the function to update.
12727 ///
12728 /// @return the updated function representation.
12729 static function_decl_sptr
maybe_finish_function_decl_reading(reader & rdr,Dwarf_Die * die,size_t where_offset,const function_decl_sptr & existing_fn)12730 maybe_finish_function_decl_reading(reader& rdr,
12731 Dwarf_Die* die,
12732 size_t where_offset,
12733 const function_decl_sptr& existing_fn)
12734 {
12735 function_decl_sptr result = build_function_decl(rdr, die,
12736 where_offset,
12737 existing_fn);
12738
12739 return result;
12740 }
12741
12742 /// Lookup a class or a typedef with a given qualified name in the
12743 /// corpus that a given scope belongs to.
12744 ///
12745 /// @param scope the scope to consider.
12746 ///
12747 /// @param type_name the qualified name of the type to look for.
12748 ///
12749 /// @return the typedef or class type found.
12750 static type_base_sptr
lookup_class_or_typedef_from_corpus(scope_decl * scope,const string & type_name)12751 lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
12752 {
12753 string qname = build_qualified_name(scope, type_name);
12754 corpus* corp = scope->get_corpus();
12755 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
12756 return result;
12757 }
12758
12759 /// Lookup a class of typedef type from the current corpus being
12760 /// constructed.
12761 ///
12762 /// The type being looked for has the same name as a given DIE.
12763 ///
12764 /// @param rdr the DWARF reader to use.
12765 ///
12766 /// @param die the DIE which has the same name as the type we are
12767 /// looking for.
12768 ///
12769 /// @param called_for_public_decl whether this function is being
12770 /// called from a a publicly defined declaration.
12771 ///
12772 /// @param where_offset where we are logically at in the DIE stream.
12773 ///
12774 /// @return the type found.
12775 static type_base_sptr
lookup_class_or_typedef_from_corpus(reader & rdr,Dwarf_Die * die,bool called_for_public_decl,size_t where_offset)12776 lookup_class_or_typedef_from_corpus(reader& rdr,
12777 Dwarf_Die* die,
12778 bool called_for_public_decl,
12779 size_t where_offset)
12780 {
12781 if (!die)
12782 return class_decl_sptr();
12783
12784 string class_name = die_string_attribute(die, DW_AT_name);
12785 if (class_name.empty())
12786 return class_decl_sptr();
12787
12788 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12789 called_for_public_decl,
12790 where_offset);
12791 if (scope)
12792 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
12793
12794 return type_base_sptr();
12795 }
12796
12797 /// Lookup a class, typedef or enum type with a given qualified name
12798 /// in the corpus that a given scope belongs to.
12799 ///
12800 /// @param scope the scope to consider.
12801 ///
12802 /// @param type_name the qualified name of the type to look for.
12803 ///
12804 /// @return the typedef, enum or class type found.
12805 static type_base_sptr
lookup_class_typedef_or_enum_type_from_corpus(scope_decl * scope,const string & type_name)12806 lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
12807 const string& type_name)
12808 {
12809 string qname = build_qualified_name(scope, type_name);
12810 corpus* corp = scope->get_corpus();
12811 type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
12812 return result;
12813 }
12814
12815 /// Lookup a class, typedef or enum type in a given scope, in the
12816 /// corpus that scope belongs to.
12817 ///
12818 /// @param die the DIE of the class, typedef or enum to lookup.
12819 ///
12820 /// @param anonymous_member_type_idx if @p DIE represents an anonymous
12821 /// type, this is the index of that anonymous type in its scope, in
12822 /// case there are several anonymous types of the same kind in that
12823 /// scope.
12824 ///
12825 /// @param scope the scope in which to look the type for.
12826 ///
12827 /// @return the typedef, enum or class type found.
12828 static type_base_sptr
lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die * die,size_t anonymous_member_type_idx,scope_decl * scope)12829 lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
12830 size_t anonymous_member_type_idx,
12831 scope_decl* scope)
12832 {
12833 if (!die)
12834 return class_decl_sptr();
12835
12836 string type_name = die_string_attribute(die, DW_AT_name);
12837 if (is_anonymous_type_die(die))
12838 type_name =
12839 get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12840
12841 if (type_name.empty())
12842 return class_decl_sptr();
12843
12844 return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
12845 }
12846
12847
12848 /// Test if a DIE represents a function that is a member of a given
12849 /// class type.
12850 ///
12851 /// @param rdr the DWARF reader.
12852 ///
12853 /// @param function_die the DIE of the function to consider.
12854 ///
12855 /// @param class_type the class type to consider.
12856 ///
12857 /// @param where_offset where we are logically at in the DIE stream.
12858 ///
12859 /// @return the method declaration corresponding to the member
12860 /// function of @p class_type, iff @p function_die is for a member
12861 /// function of @p class_type.
12862 static method_decl_sptr
is_function_for_die_a_member_of_class(reader & rdr,Dwarf_Die * function_die,const class_or_union_sptr & class_type)12863 is_function_for_die_a_member_of_class(reader& rdr,
12864 Dwarf_Die* function_die,
12865 const class_or_union_sptr& class_type)
12866 {
12867 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
12868
12869 if (!artifact)
12870 return method_decl_sptr();
12871
12872 method_decl_sptr method = is_method_decl(artifact);
12873 method_type_sptr method_type;
12874
12875 if (method)
12876 method_type = method->get_type();
12877 else
12878 method_type = is_method_type(artifact);
12879 ABG_ASSERT(method_type);
12880
12881 class_or_union_sptr method_class = method_type->get_class_type();
12882 ABG_ASSERT(method_class);
12883
12884 string method_class_name = method_class->get_qualified_name(),
12885 class_type_name = class_type->get_qualified_name();
12886
12887 if (method_class_name == class_type_name)
12888 {
12889 //ABG_ASSERT(class_type.get() == method_class.get());
12890 return method;
12891 }
12892
12893 return method_decl_sptr();
12894 }
12895
12896 /// If a given function DIE represents an existing member function of
12897 /// a given class, then update that member function with new
12898 /// properties present in the DIE. Otherwise, if the DIE represents a
12899 /// new member function that is not already present in the class then
12900 /// add that new member function to the class.
12901 ///
12902 /// @param rdr the DWARF reader.
12903 ///
12904 /// @param function_die the DIE of the potential member function to
12905 /// consider.
12906 ///
12907 /// @param class_type the class type to consider.
12908 ///
12909 /// @param called_from_public_decl is true iff this function was
12910 /// called from a publicly defined and exported declaration.
12911 ///
12912 /// @param where_offset where we are logically at in the DIE stream.
12913 ///
12914 /// @return the method decl representing the member function.
12915 static method_decl_sptr
add_or_update_member_function(reader & rdr,Dwarf_Die * function_die,const class_or_union_sptr & class_type,bool called_from_public_decl,size_t where_offset)12916 add_or_update_member_function(reader& rdr,
12917 Dwarf_Die* function_die,
12918 const class_or_union_sptr& class_type,
12919 bool called_from_public_decl,
12920 size_t where_offset)
12921 {
12922 method_decl_sptr method =
12923 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
12924
12925 if (!method)
12926 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
12927 class_type.get(),
12928 called_from_public_decl,
12929 where_offset));
12930 if (!method)
12931 return method_decl_sptr();
12932
12933 finish_member_function_reading(function_die,
12934 is_function_decl(method),
12935 class_type, rdr);
12936 return method;
12937 }
12938
12939 /// Build a an IR node for class type from a DW_TAG_structure_type or
12940 /// DW_TAG_class_type DIE and add that node to the ABI corpus being
12941 /// currently built.
12942 ///
12943 /// If the represents class type that already exists, then update the
12944 /// existing class type with the new properties found in the DIE.
12945 ///
12946 /// It meanst that this function can also update an existing
12947 /// class_decl node with data members, member functions and other
12948 /// properties coming from the DIE.
12949 ///
12950 /// @param rdr the DWARF reader to consider.
12951 ///
12952 /// @param die the DIE to read information from. Must be either a
12953 /// DW_TAG_structure_type or a DW_TAG_class_type.
12954 ///
12955 /// @param scope a pointer to the scope_decl* under which this class
12956 /// is to be added to.
12957 ///
12958 /// @param is_struct whether the class was declared as a struct.
12959 ///
12960 /// @param klass if non-null, this is a klass to append the members
12961 /// to. Otherwise, this function just builds the class from scratch.
12962 ///
12963 /// @param called_from_public_decl set to true if this class is being
12964 /// called from a "Public declaration like vars or public symbols".
12965 ///
12966 /// @param where_offset the offset of the DIE where we are "logically"
12967 /// positionned at, in the DIE tree. This is useful when @p die is
12968 /// e.g, DW_TAG_partial_unit that can be included in several places in
12969 /// the DIE tree.
12970 ///
12971 /// @param is_declaration_only is true if the DIE denoted by @p die is
12972 /// a declaration-only DIE.
12973 ///
12974 /// @return the resulting class_type.
12975 static class_decl_sptr
add_or_update_class_type(reader & rdr,Dwarf_Die * die,scope_decl * scope,bool is_struct,class_decl_sptr klass,bool called_from_public_decl,size_t where_offset,bool is_declaration_only)12976 add_or_update_class_type(reader& rdr,
12977 Dwarf_Die* die,
12978 scope_decl* scope,
12979 bool is_struct,
12980 class_decl_sptr klass,
12981 bool called_from_public_decl,
12982 size_t where_offset,
12983 bool is_declaration_only)
12984 {
12985 class_decl_sptr result;
12986 if (!die)
12987 return result;
12988
12989 const die_source source = rdr.get_die_source(die);
12990
12991 unsigned tag = dwarf_tag(die);
12992
12993 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
12994 return result;
12995
12996 {
12997 die_class_or_union_map_type::const_iterator i =
12998 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12999 if (i != rdr.die_wip_classes_map(source).end())
13000 {
13001 class_decl_sptr class_type = is_class_type(i->second);
13002 ABG_ASSERT(class_type);
13003 return class_type;
13004 }
13005 }
13006
13007 string name, linkage_name;
13008 location loc;
13009 die_loc_and_name(rdr, die, loc, name, linkage_name);
13010
13011 bool is_anonymous = false;
13012 if (name.empty())
13013 {
13014 // So we are looking at an anonymous struct. Let's
13015 // give it a name.
13016 name = get_internal_anonymous_die_prefix_name(die);
13017 ABG_ASSERT(!name.empty());
13018 // But we remember that the type is anonymous.
13019 is_anonymous = true;
13020
13021 if (size_t s = scope->get_num_anonymous_member_classes())
13022 name = build_internal_anonymous_die_name(name, s);
13023 }
13024
13025 if (!is_anonymous)
13026 {
13027 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13028 {
13029 if (loc)
13030 // TODO: if there is only one class defined in the corpus
13031 // for this location, then re-use it. But if there are
13032 // more than one, then do not re-use it, for now.
13033 result = lookup_class_type_per_location(loc.expand(), *corp);
13034 else
13035 // TODO: if there is just one class for that name defined,
13036 // then re-use it. Otherwise, don't.
13037 result = lookup_class_type(name, *corp);
13038 if (result
13039 // If we are seeing a declaration of a definition we
13040 // already had, or if we are seing a type with the same
13041 // declaration-only-ness that we had before, then keep
13042 // the one we already had.
13043 && (result->get_is_declaration_only() == is_declaration_only
13044 || (!result->get_is_declaration_only()
13045 && is_declaration_only)))
13046 {
13047 rdr.associate_die_to_type(die, result, where_offset);
13048 return result;
13049 }
13050 else
13051 // We might be seeing the definition of a declaration we
13052 // already had. In that case, keep the definition and
13053 // drop the declaration.
13054 result.reset();
13055 }
13056 }
13057
13058 // If we've already seen the same class as 'die', then let's re-use
13059 // that one, unless it's an anonymous class. We can't really safely
13060 // re-use anonymous classes as they have no name, by construction.
13061 // What we can do, rather, is to reuse the typedef that name them,
13062 // when they do have a naming typedef.
13063 if (!is_anonymous)
13064 if (class_decl_sptr pre_existing_class =
13065 is_class_type(rdr.lookup_type_artifact_from_die(die)))
13066 klass = pre_existing_class;
13067
13068 uint64_t size = 0;
13069 die_size_in_bits(die, size);
13070 bool is_artificial = die_is_artificial(die);
13071
13072 Dwarf_Die child;
13073 bool has_child = (dwarf_child(die, &child) == 0);
13074
13075 decl_base_sptr res;
13076 if (klass)
13077 {
13078 res = result = klass;
13079 if (has_child && klass->get_is_declaration_only()
13080 && klass->get_definition_of_declaration())
13081 res = result = is_class_type(klass->get_definition_of_declaration());
13082 if (loc)
13083 result->set_location(loc);
13084 }
13085 else
13086 {
13087 result.reset(new class_decl(rdr.env(), name, size,
13088 /*alignment=*/0, is_struct, loc,
13089 decl_base::VISIBILITY_DEFAULT,
13090 is_anonymous));
13091
13092 result->set_is_declaration_only(is_declaration_only);
13093
13094 res = add_decl_to_scope(result, scope);
13095 result = dynamic_pointer_cast<class_decl>(res);
13096 ABG_ASSERT(result);
13097 }
13098
13099 if (!klass || klass->get_is_declaration_only())
13100 if (size != result->get_size_in_bits())
13101 result->set_size_in_bits(size);
13102
13103 if (klass)
13104 // We are amending a class that was built before. So let's check
13105 // if we need to amend its "declaration-only-ness" status.
13106 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
13107 // The size of the class doesn't match its
13108 // 'declaration-only-ness". We might have a non-zero sized
13109 // class which is declaration-only, or a zero sized class that
13110 // is not declaration-only. Let's set the declaration-only-ness
13111 // according to what we are instructed to.
13112 //
13113 // Note however that there are binaries out there emitted by
13114 // compilers (Clang, in C++) emit declarations-only classes that
13115 // have non-zero size. So we must honor these too. That is why
13116 // we are not forcing the declaration-only-ness to false when a
13117 // class has non-zero size. An example of such binary is
13118 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
13119 result->set_is_declaration_only(is_declaration_only);
13120
13121 // If a non-decl-only class has children node and is advertized as
13122 // having a non-zero size let's trust that.
13123 if (!result->get_is_declaration_only() && has_child)
13124 if (result->get_size_in_bits() == 0 && size != 0)
13125 result->set_size_in_bits(size);
13126
13127 result->set_is_artificial(is_artificial);
13128
13129 rdr.associate_die_to_type(die, result, where_offset);
13130
13131 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13132
13133 if (!has_child)
13134 // TODO: set the access specifier for the declaration-only class
13135 // here.
13136 return result;
13137
13138 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13139
13140 bool is_incomplete_type = false;
13141 if (is_declaration_only && size == 0 && has_child)
13142 // this is an incomplete DWARF type as defined by [5.7.1]
13143 //
13144 // An incomplete structure, union or class type is represented by
13145 // a structure, union or class entry that does not have a byte
13146 // size attribute and that has a DW_AT_declaration attribute.
13147 //
13148 // Let's consider that it's thus a decl-only class, likely
13149 // referred to by a pointer. If we later encounter a definition
13150 // for this decl-only class type, then this decl-only class will
13151 // be resolved to it by the code in
13152 // reader::resolve_declaration_only_classes.
13153 is_incomplete_type = true;
13154
13155 scope_decl_sptr scop =
13156 dynamic_pointer_cast<scope_decl>(res);
13157 ABG_ASSERT(scop);
13158 rdr.scope_stack().push(scop.get());
13159
13160 if (has_child && !is_incomplete_type)
13161 {
13162 int anonymous_member_class_index = -1;
13163 int anonymous_member_union_index = -1;
13164 int anonymous_member_enum_index = -1;
13165
13166 do
13167 {
13168 tag = dwarf_tag(&child);
13169
13170 // Handle base classes.
13171 if (tag == DW_TAG_inheritance)
13172 {
13173 result->set_is_declaration_only(false);
13174
13175 Dwarf_Die type_die;
13176 if (!die_die_attribute(&child, DW_AT_type, type_die))
13177 continue;
13178
13179 type_base_sptr base_type;
13180 if (!(base_type =
13181 lookup_class_or_typedef_from_corpus(rdr, &type_die,
13182 called_from_public_decl,
13183 where_offset)))
13184 {
13185 base_type =
13186 is_type(build_ir_node_from_die(rdr, &type_die,
13187 called_from_public_decl,
13188 where_offset));
13189 }
13190 // Sometimes base_type can be a typedef. Let's make
13191 // sure that typedef is compatible with a class type.
13192 class_decl_sptr b = is_compatible_with_class_type(base_type);
13193 if (!b)
13194 continue;
13195
13196 access_specifier access =
13197 is_struct
13198 ? public_access
13199 : private_access;
13200
13201 die_access_specifier(&child, access);
13202
13203 bool is_virt= die_is_virtual(&child);
13204 int64_t offset = 0;
13205 bool is_offset_present =
13206 die_member_offset(rdr, &child, offset);
13207
13208 class_decl::base_spec_sptr base(new class_decl::base_spec
13209 (b, access,
13210 is_offset_present ? offset : -1,
13211 is_virt));
13212 if (b->get_is_declaration_only())
13213 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
13214 if (result->find_base_class(b->get_qualified_name()))
13215 continue;
13216 result->add_base_specifier(base);
13217 }
13218 // Handle data members.
13219 else if (tag == DW_TAG_member
13220 || tag == DW_TAG_variable)
13221 {
13222 Dwarf_Die type_die;
13223 if (!die_die_attribute(&child, DW_AT_type, type_die))
13224 continue;
13225
13226 string n, m;
13227 location loc;
13228 die_loc_and_name(rdr, &child, loc, n, m);
13229 /// For now, we skip the hidden vtable pointer.
13230 /// Currently, we're looking for a member starting with
13231 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
13232 /// use as a name for the hidden vtable pointer.
13233 if (n.substr(0, 5) == "_vptr"
13234 && n.size() > 5
13235 && !std::isalnum(n.at(5))
13236 && n.at(5) != '_')
13237 continue;
13238
13239 // If the variable is already a member of this class,
13240 // move on. If it's an anonymous data member, we need
13241 // to handle it differently. We'll do that later below.
13242 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13243 continue;
13244
13245 int64_t offset_in_bits = 0;
13246 bool is_laid_out = die_member_offset(rdr, &child,
13247 offset_in_bits);
13248 // For now, is_static == !is_laid_out. When we have
13249 // templates, we'll try to be more specific. For now,
13250 // this approximation should do OK.
13251 bool is_static = !is_laid_out;
13252
13253 if (is_static && variable_is_suppressed(rdr,
13254 result.get(),
13255 &child))
13256 continue;
13257
13258 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
13259 called_from_public_decl,
13260 where_offset));
13261 type_base_sptr t = is_type(ty);
13262 if (!t)
13263 continue;
13264
13265 if (n.empty() && !die_is_anonymous_data_member(&child))
13266 {
13267 // We must be in a case where the data member has an
13268 // empty name because the DWARF emitter has a bug.
13269 // Let's generate an artificial name for that data
13270 // member.
13271 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
13272 ABG_ASSERT(!n.empty());
13273 }
13274
13275 // The call to build_ir_node_from_die above could have
13276 // triggered the adding of a data member named 'n' into
13277 // result. So let's check again if the variable is
13278 // already a member of this class. Here again, if it's
13279 // an anonymous data member, we need to handle it
13280 // differently. We'll do that later below.
13281 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13282 continue;
13283
13284 if (!is_static)
13285 // We have a non-static data member. So this class
13286 // cannot be a declaration-only class anymore, even if
13287 // some DWARF emitters might consider it otherwise.
13288 result->set_is_declaration_only(false);
13289 access_specifier access =
13290 is_struct
13291 ? public_access
13292 : private_access;
13293
13294 die_access_specifier(&child, access);
13295
13296 var_decl_sptr dm(new var_decl(n, t, loc, m));
13297 if (n.empty()
13298 && anonymous_data_member_exists_in_class(*dm, *result))
13299 // dm is an anonymous data member that was already
13300 // present in the current class so let's not add it.
13301 continue;
13302 result->add_data_member(dm, access, is_laid_out,
13303 is_static, offset_in_bits);
13304 ABG_ASSERT(has_scope(dm));
13305 rdr.associate_die_to_decl(&child, dm, where_offset,
13306 /*associate_by_repr=*/false);
13307 }
13308 // Handle member functions;
13309 else if (tag == DW_TAG_subprogram)
13310 {
13311 decl_base_sptr r =
13312 add_or_update_member_function(rdr, &child, result,
13313 called_from_public_decl,
13314 where_offset);
13315 if (function_decl_sptr f = is_function_decl(r))
13316 rdr.associate_die_to_decl(&child, f, where_offset,
13317 /*associate_by_repr=*/true);
13318 }
13319 // Handle member types
13320 else if (die_is_type(&child))
13321 {
13322 // Track the anonymous type index in the current
13323 // scope. Look for what this means by reading the
13324 // comment of the function
13325 // build_internal_anonymous_die_name.
13326 int anonymous_member_type_index = 0;
13327 if (is_anonymous_type_die(&child))
13328 {
13329 // Update the anonymous type index.
13330 if (die_is_class_type(&child))
13331 anonymous_member_type_index =
13332 ++anonymous_member_class_index;
13333 else if (dwarf_tag(&child) == DW_TAG_union_type)
13334 anonymous_member_type_index =
13335 ++anonymous_member_union_index;
13336 else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13337 anonymous_member_type_index =
13338 ++anonymous_member_enum_index;
13339 }
13340 // if the type is not already a member of this class,
13341 // then add it to the class.
13342 if ((is_anonymous_type_die(&child)
13343 && !lookup_class_typedef_or_enum_type_from_corpus
13344 (&child, anonymous_member_type_index, result.get()))
13345 || !result->find_member_type(die_name(&child)))
13346 build_ir_node_from_die(rdr, &child, result.get(),
13347 called_from_public_decl,
13348 where_offset);
13349 }
13350 } while (dwarf_siblingof(&child, &child) == 0);
13351 }
13352
13353 rdr.scope_stack().pop();
13354
13355 {
13356 die_class_or_union_map_type::const_iterator i =
13357 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13358 if (i != rdr.die_wip_classes_map(source).end())
13359 {
13360 if (is_member_type(i->second))
13361 set_member_access_specifier(res,
13362 get_member_access_specifier(i->second));
13363 rdr.die_wip_classes_map(source).erase(i);
13364 }
13365 }
13366
13367 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13368 return result;
13369 }
13370
13371 /// Build an @ref union_decl from a DW_TAG_union_type DIE.
13372 ///
13373 /// @param rdr the DWARF reader to use.
13374 ///
13375 /// @param die the DIE to read from.
13376 ///
13377 /// @param scope the scope the resulting @ref union_decl belongs to.
13378 ///
13379 /// @param union_type if this parameter is non-nil, then this function
13380 /// updates the @ref union_decl that it points to, rather than
13381 /// creating a new @ref union_decl.
13382 ///
13383 /// @param called_from_public_decl is true if this function has been
13384 /// initially called within the context of a public decl.
13385 ///
13386 /// @param where_offset the offset of the DIE where we are "logically"
13387 /// positionned at, in the DIE tree. This is useful when @p die is
13388 /// e.g, DW_TAG_partial_unit that can be included in several places in
13389 /// the DIE tree.
13390 ///
13391 /// @param is_declaration_only is true if the DIE denoted by @p die is
13392 /// a declaration-only DIE.
13393 ///
13394 /// @return the resulting @ref union_decl type.
13395 static union_decl_sptr
add_or_update_union_type(reader & rdr,Dwarf_Die * die,scope_decl * scope,union_decl_sptr union_type,bool called_from_public_decl,size_t where_offset,bool is_declaration_only)13396 add_or_update_union_type(reader& rdr,
13397 Dwarf_Die* die,
13398 scope_decl* scope,
13399 union_decl_sptr union_type,
13400 bool called_from_public_decl,
13401 size_t where_offset,
13402 bool is_declaration_only)
13403 {
13404 union_decl_sptr result;
13405 if (!die)
13406 return result;
13407
13408 unsigned tag = dwarf_tag(die);
13409
13410 if (tag != DW_TAG_union_type)
13411 return result;
13412
13413 const die_source source = rdr.get_die_source(die);
13414 {
13415 die_class_or_union_map_type::const_iterator i =
13416 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13417 if (i != rdr.die_wip_classes_map(source).end())
13418 {
13419 union_decl_sptr u = is_union_type(i->second);
13420 ABG_ASSERT(u);
13421 return u;
13422 }
13423 }
13424
13425 string name, linkage_name;
13426 location loc;
13427 die_loc_and_name(rdr, die, loc, name, linkage_name);
13428
13429 bool is_anonymous = false;
13430 if (name.empty())
13431 {
13432 // So we are looking at an anonymous union. Let's give it a
13433 // name.
13434 name = get_internal_anonymous_die_prefix_name(die);
13435 ABG_ASSERT(!name.empty());
13436 // But we remember that the type is anonymous.
13437 is_anonymous = true;
13438
13439 if (size_t s = scope->get_num_anonymous_member_unions())
13440 name = build_internal_anonymous_die_name(name, s);
13441 }
13442
13443 // If the type has location, then associate it to its
13444 // representation. This way, all occurences of types with the same
13445 // representation (name) and location can be later detected as being
13446 // for the same type.
13447
13448 if (!is_anonymous)
13449 {
13450 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13451 {
13452 if (loc)
13453 result = lookup_union_type_per_location(loc.expand(), *corp);
13454 else
13455 result = lookup_union_type(name, *corp);
13456
13457 if (result)
13458 {
13459 rdr.associate_die_to_type(die, result, where_offset);
13460 return result;
13461 }
13462 }
13463 }
13464
13465 // if we've already seen a union with the same union as 'die' then
13466 // let's re-use that one. We can't really safely re-use anonymous
13467 // unions as they have no name, by construction. What we can do,
13468 // rather, is to reuse the typedef that name them, when they do have
13469 // a naming typedef.
13470 if (!is_anonymous)
13471 if (union_decl_sptr pre_existing_union =
13472 is_union_type(rdr.lookup_artifact_from_die(die)))
13473 union_type = pre_existing_union;
13474
13475 uint64_t size = 0;
13476 die_size_in_bits(die, size);
13477 bool is_artificial = die_is_artificial(die);
13478
13479 if (union_type)
13480 {
13481 result = union_type;
13482 result->set_location(loc);
13483 }
13484 else
13485 {
13486 result.reset(new union_decl(rdr.env(), name, size, loc,
13487 decl_base::VISIBILITY_DEFAULT,
13488 is_anonymous));
13489 if (is_declaration_only)
13490 result->set_is_declaration_only(true);
13491 result = is_union_type(add_decl_to_scope(result, scope));
13492 ABG_ASSERT(result);
13493 }
13494
13495 if (size)
13496 {
13497 result->set_size_in_bits(size);
13498 result->set_is_declaration_only(false);
13499 }
13500
13501 result->set_is_artificial(is_artificial);
13502
13503 rdr.associate_die_to_type(die, result, where_offset);
13504
13505 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13506
13507 Dwarf_Die child;
13508 bool has_child = (dwarf_child(die, &child) == 0);
13509 if (!has_child)
13510 return result;
13511
13512 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13513
13514 scope_decl_sptr scop =
13515 dynamic_pointer_cast<scope_decl>(result);
13516 ABG_ASSERT(scop);
13517 rdr.scope_stack().push(scop.get());
13518
13519 if (has_child)
13520 {
13521 do
13522 {
13523 tag = dwarf_tag(&child);
13524 // Handle data members.
13525 if (tag == DW_TAG_member || tag == DW_TAG_variable)
13526 {
13527 Dwarf_Die type_die;
13528 if (!die_die_attribute(&child, DW_AT_type, type_die))
13529 continue;
13530
13531 string n, m;
13532 location loc;
13533 die_loc_and_name(rdr, &child, loc, n, m);
13534
13535 // Because we can be updating an existing union, let's
13536 // make sure we don't already have a member of the same
13537 // name. Anonymous member are handled a bit later below
13538 // so let's not consider them here.
13539 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13540 continue;
13541
13542 ssize_t offset_in_bits = 0;
13543 decl_base_sptr ty =
13544 is_decl(build_ir_node_from_die(rdr, &type_die,
13545 called_from_public_decl,
13546 where_offset));
13547 type_base_sptr t = is_type(ty);
13548 if (!t)
13549 continue;
13550
13551 // We have a non-static data member. So this union
13552 // cannot be a declaration-only union anymore, even if
13553 // some DWARF emitters might consider it otherwise.
13554 result->set_is_declaration_only(false);
13555 access_specifier access = public_access;
13556
13557 die_access_specifier(&child, access);
13558
13559 var_decl_sptr dm(new var_decl(n, t, loc, m));
13560 // If dm is an anonymous data member, let's make sure
13561 // the current union doesn't already have it as a data
13562 // member.
13563 if (n.empty() && result->find_data_member(dm))
13564 continue;
13565
13566 result->add_data_member(dm, access, /*is_laid_out=*/true,
13567 /*is_static=*/false,
13568 offset_in_bits);
13569 ABG_ASSERT(has_scope(dm));
13570 rdr.associate_die_to_decl(&child, dm, where_offset,
13571 /*associate_by_repr=*/false);
13572 }
13573 // Handle member functions;
13574 else if (tag == DW_TAG_subprogram)
13575 {
13576 decl_base_sptr r =
13577 is_decl(build_ir_node_from_die(rdr, &child,
13578 result.get(),
13579 called_from_public_decl,
13580 where_offset));
13581 if (!r)
13582 continue;
13583
13584 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13585 ABG_ASSERT(f);
13586
13587 finish_member_function_reading(&child, f, result, rdr);
13588
13589 rdr.associate_die_to_decl(&child, f, where_offset,
13590 /*associate_by_repr=*/false);
13591 }
13592 // Handle member types
13593 else if (die_is_type(&child))
13594 decl_base_sptr td =
13595 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13596 called_from_public_decl,
13597 where_offset));
13598 } while (dwarf_siblingof(&child, &child) == 0);
13599 }
13600
13601 rdr.scope_stack().pop();
13602
13603 {
13604 die_class_or_union_map_type::const_iterator i =
13605 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13606 if (i != rdr.die_wip_classes_map(source).end())
13607 {
13608 if (is_member_type(i->second))
13609 set_member_access_specifier(result,
13610 get_member_access_specifier(i->second));
13611 rdr.die_wip_classes_map(source).erase(i);
13612 }
13613 }
13614
13615 return result;
13616 }
13617
13618 /// build a qualified type from a DW_TAG_const_type,
13619 /// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13620 ///
13621 /// @param rdr the DWARF reader to consider.
13622 ///
13623 /// @param die the input DIE to read from.
13624 ///
13625 /// @param called_from_public_decl true if this function was called
13626 /// from a context where either a public function or a public variable
13627 /// is being built.
13628 ///
13629 /// @param where_offset the offset of the DIE where we are "logically"
13630 /// positionned at, in the DIE tree. This is useful when @p die is
13631 /// e.g, DW_TAG_partial_unit that can be included in several places in
13632 /// the DIE tree.
13633 ///
13634 /// @return the resulting qualified_type_def.
13635 static type_base_sptr
build_qualified_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13636 build_qualified_type(reader& rdr,
13637 Dwarf_Die* die,
13638 bool called_from_public_decl,
13639 size_t where_offset)
13640 {
13641 type_base_sptr result;
13642 if (!die)
13643 return result;
13644
13645 unsigned tag = dwarf_tag(die);
13646
13647 if (tag != DW_TAG_const_type
13648 && tag != DW_TAG_volatile_type
13649 && tag != DW_TAG_restrict_type)
13650 return result;
13651
13652 Dwarf_Die underlying_type_die;
13653 decl_base_sptr utype_decl;
13654 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13655 // So, if no DW_AT_type is present, then this means (if we are
13656 // looking at a debug info emitted by GCC) that we are looking
13657 // at a qualified void type.
13658 utype_decl = build_ir_node_for_void_type(rdr);
13659
13660 if (!utype_decl)
13661 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
13662 called_from_public_decl,
13663 where_offset));
13664 if (!utype_decl)
13665 return result;
13666
13667 // The call to build_ir_node_from_die() could have triggered the
13668 // creation of the type for this DIE. In that case, just return it.
13669 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13670 {
13671 result = t;
13672 rdr.associate_die_to_type(die, result, where_offset);
13673 return result;
13674 }
13675
13676 type_base_sptr utype = is_type(utype_decl);
13677 ABG_ASSERT(utype);
13678
13679 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
13680 if (tag == DW_TAG_const_type)
13681 qual |= qualified_type_def::CV_CONST;
13682 else if (tag == DW_TAG_volatile_type)
13683 qual |= qualified_type_def::CV_VOLATILE;
13684 else if (tag == DW_TAG_restrict_type)
13685 qual |= qualified_type_def::CV_RESTRICT;
13686 else
13687 ABG_ASSERT_NOT_REACHED;
13688
13689 if (!result)
13690 result.reset(new qualified_type_def(utype, qual, location()));
13691
13692 rdr.associate_die_to_type(die, result, where_offset);
13693
13694 return result;
13695 }
13696
13697 /// Walk a tree of typedef of qualified arrays and schedule all type
13698 /// nodes for canonicalization.
13699 ///
13700 /// This is to be used after an array tree has been cloned. In that
13701 /// case, the newly cloned type nodes have to be scheduled for
13702 /// canonicalization.
13703 ///
13704 /// This is a subroutine of maybe_strip_qualification.
13705 ///
13706 /// @param t the type node to be scheduled for canonicalization.
13707 ///
13708 /// @param rdr the DWARF reader to use.
13709 static void
schedule_array_tree_for_late_canonicalization(const type_base_sptr & t,reader & rdr)13710 schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
13711 reader &rdr)
13712 {
13713 if (typedef_decl_sptr type = is_typedef(t))
13714 {
13715 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13716 rdr);
13717 rdr.schedule_type_for_late_canonicalization(t);
13718 }
13719 else if (qualified_type_def_sptr type = is_qualified_type(t))
13720 {
13721 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13722 rdr);
13723 rdr.schedule_type_for_late_canonicalization(t);
13724 }
13725 else if (array_type_def_sptr type = is_array_type(t))
13726 {
13727 for (vector<array_type_def::subrange_sptr>::const_iterator i =
13728 type->get_subranges().begin();
13729 i != type->get_subranges().end();
13730 ++i)
13731 {
13732 if (!(*i)->get_scope())
13733 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
13734 rdr.schedule_type_for_late_canonicalization(*i);
13735
13736 }
13737 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
13738 rdr);
13739 rdr.schedule_type_for_late_canonicalization(type);
13740 }
13741 }
13742
13743 /// Strip qualification from a qualified type, when it makes sense.
13744 ///
13745 /// DWARF constructs "const reference". This is redundant because a
13746 /// reference is always const. The issue is these redundant types then
13747 /// leak into the IR and make for bad diagnostics.
13748 ///
13749 /// This function thus strips the const qualifier from the type in
13750 /// that case. It might contain code to strip other cases like this
13751 /// in the future.
13752 ///
13753 /// @param t the type to strip const qualification from.
13754 ///
13755 /// @param rdr the @ref reader to use.
13756 ///
13757 /// @return the stripped type or just return @p t.
13758 static decl_base_sptr
maybe_strip_qualification(const qualified_type_def_sptr t,reader & rdr)13759 maybe_strip_qualification(const qualified_type_def_sptr t,
13760 reader &rdr)
13761 {
13762 if (!t)
13763 return t;
13764
13765 decl_base_sptr result = t;
13766 type_base_sptr u = t->get_underlying_type();
13767
13768 strip_redundant_quals_from_underyling_types(t);
13769 result = strip_useless_const_qualification(t);
13770 if (result.get() != t.get())
13771 return result;
13772
13773 if (is_array_type(u) || is_typedef_of_array(u))
13774 {
13775 array_type_def_sptr array;
13776 scope_decl * scope = 0;
13777 if ((array = is_array_type(u)))
13778 {
13779 scope = array->get_scope();
13780 ABG_ASSERT(scope);
13781 array = is_array_type(clone_array_tree(array));
13782 schedule_array_tree_for_late_canonicalization(array, rdr);
13783 add_decl_to_scope(array, scope);
13784 t->set_underlying_type(array);
13785 u = t->get_underlying_type();
13786 }
13787 else if (is_typedef_of_array(u))
13788 {
13789 scope = is_decl(u)->get_scope();
13790 ABG_ASSERT(scope);
13791 typedef_decl_sptr typdef =
13792 is_typedef(clone_array_tree(is_typedef(u)));
13793 schedule_array_tree_for_late_canonicalization(typdef, rdr);
13794 ABG_ASSERT(typdef);
13795 add_decl_to_scope(typdef, scope);
13796 t->set_underlying_type(typdef);
13797 u = t->get_underlying_type();
13798 array = is_typedef_of_array(u);
13799 }
13800 else
13801 ABG_ASSERT_NOT_REACHED;
13802
13803 ABG_ASSERT(array);
13804 // We should not be editing types that are already canonicalized.
13805 ABG_ASSERT(!array->get_canonical_type());
13806 type_base_sptr element_type = array->get_element_type();
13807
13808 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
13809 {
13810 // We should not be editing types that are already canonicalized.
13811 ABG_ASSERT(!qualified->get_canonical_type());
13812 qualified_type_def::CV quals = qualified->get_cv_quals();
13813 quals |= t->get_cv_quals();
13814 qualified->set_cv_quals(quals);
13815 strip_redundant_quals_from_underyling_types(qualified);
13816 result = is_decl(u);
13817 }
13818 else
13819 {
13820 qualified_type_def_sptr qual_type
13821 (new qualified_type_def(element_type,
13822 t->get_cv_quals(),
13823 t->get_location()));
13824 strip_redundant_quals_from_underyling_types(qual_type);
13825 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
13826 array->set_element_type(qual_type);
13827 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
13828 result = is_decl(u);
13829 }
13830 }
13831
13832 return result;
13833 }
13834
13835 /// Build a pointer type from a DW_TAG_pointer_type DIE.
13836 ///
13837 /// @param rdr the DWARF reader to consider.
13838 ///
13839 /// @param die the DIE to read information from.
13840 ///
13841 /// @param called_from_public_decl true if this function was called
13842 /// from a context where either a public function or a public variable
13843 /// is being built.
13844 ///
13845 /// @param where_offset the offset of the DIE where we are "logically"
13846 /// positionned at, in the DIE tree. This is useful when @p die is
13847 /// e.g, DW_TAG_partial_unit that can be included in several places in
13848 /// the DIE tree.
13849 ///
13850 /// @return the resulting pointer to pointer_type_def.
13851 static pointer_type_def_sptr
build_pointer_type_def(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13852 build_pointer_type_def(reader& rdr,
13853 Dwarf_Die* die,
13854 bool called_from_public_decl,
13855 size_t where_offset)
13856 {
13857 pointer_type_def_sptr result;
13858
13859 if (!die)
13860 return result;
13861
13862 unsigned tag = dwarf_tag(die);
13863 if (tag != DW_TAG_pointer_type)
13864 return result;
13865
13866 type_or_decl_base_sptr utype_decl;
13867 Dwarf_Die underlying_type_die;
13868 bool has_underlying_type_die = false;
13869 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13870 // If the DW_AT_type attribute is missing, that means we are
13871 // looking at a pointer to "void".
13872 utype_decl = build_ir_node_for_void_type(rdr);
13873 else
13874 has_underlying_type_die = true;
13875
13876 if (!utype_decl && has_underlying_type_die)
13877 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
13878 called_from_public_decl,
13879 where_offset);
13880 if (!utype_decl)
13881 return result;
13882
13883 // The call to build_ir_node_from_die() could have triggered the
13884 // creation of the type for this DIE. In that case, just return it.
13885 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13886 {
13887 result = is_pointer_type(t);
13888 ABG_ASSERT(result);
13889 return result;
13890 }
13891
13892 type_base_sptr utype = is_type(utype_decl);
13893 ABG_ASSERT(utype);
13894
13895 // if the DIE for the pointer type doesn't have a byte_size
13896 // attribute then we assume the size of the pointer is the address
13897 // size of the current translation unit.
13898 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13899 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13900 // The size as expressed by DW_AT_byte_size is in byte, so let's
13901 // convert it to bits.
13902 size *= 8;
13903
13904 // And the size of the pointer must be the same as the address size
13905 // of the current translation unit.
13906 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13907
13908 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
13909 ABG_ASSERT(result->get_pointed_to_type());
13910
13911 if (is_void_pointer_type(result))
13912 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
13913
13914 rdr.associate_die_to_type(die, result, where_offset);
13915 return result;
13916 }
13917
13918 /// Build a reference type from either a DW_TAG_reference_type or
13919 /// DW_TAG_rvalue_reference_type DIE.
13920 ///
13921 /// @param rdr the DWARF reader to consider.
13922 ///
13923 /// @param die the DIE to read from.
13924 ///
13925 /// @param called_from_public_decl true if this function was called
13926 /// from a context where either a public function or a public variable
13927 /// is being built.
13928 ///
13929 /// @param where_offset the offset of the DIE where we are "logically"
13930 /// positionned at, in the DIE tree. This is useful when @p die is
13931 /// e.g, DW_TAG_partial_unit that can be included in several places in
13932 /// the DIE tree.
13933 ///
13934 /// @return a pointer to the resulting reference_type_def.
13935 static reference_type_def_sptr
build_reference_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13936 build_reference_type(reader& rdr,
13937 Dwarf_Die* die,
13938 bool called_from_public_decl,
13939 size_t where_offset)
13940 {
13941 reference_type_def_sptr result;
13942
13943 if (!die)
13944 return result;
13945
13946 unsigned tag = dwarf_tag(die);
13947 if (tag != DW_TAG_reference_type
13948 && tag != DW_TAG_rvalue_reference_type)
13949 return result;
13950
13951 Dwarf_Die underlying_type_die;
13952 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13953 return result;
13954
13955 type_or_decl_base_sptr utype_decl =
13956 build_ir_node_from_die(rdr, &underlying_type_die,
13957 called_from_public_decl,
13958 where_offset);
13959 if (!utype_decl)
13960 return result;
13961
13962 // The call to build_ir_node_from_die() could have triggered the
13963 // creation of the type for this DIE. In that case, just return it.
13964 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13965 {
13966 result = is_reference_type(t);
13967 ABG_ASSERT(result);
13968 return result;
13969 }
13970
13971 type_base_sptr utype = is_type(utype_decl);
13972 ABG_ASSERT(utype);
13973
13974 // if the DIE for the reference type doesn't have a byte_size
13975 // attribute then we assume the size of the reference is the address
13976 // size of the current translation unit.
13977 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13978 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13979 size *= 8;
13980
13981 // And the size of the pointer must be the same as the address size
13982 // of the current translation unit.
13983 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13984
13985 bool is_lvalue = tag == DW_TAG_reference_type;
13986
13987 result.reset(new reference_type_def(utype, is_lvalue, size,
13988 /*alignment=*/0,
13989 location()));
13990 if (corpus_sptr corp = rdr.corpus())
13991 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
13992 result = t;
13993 rdr.associate_die_to_type(die, result, where_offset);
13994 return result;
13995 }
13996
13997 /// Build a subroutine type from a DW_TAG_subroutine_type DIE.
13998 ///
13999 /// @param rdr the DWARF reader to consider.
14000 ///
14001 /// @param die the DIE to read from.
14002 ///
14003 /// @param is_method points to a class or union declaration iff we're
14004 /// building the type for a method. This is the enclosing class or
14005 /// union of the method.
14006 ///
14007 /// @param where_offset the offset of the DIE where we are "logically"
14008 /// positioned at, in the DIE tree. This is useful when @p die is
14009 /// e.g, DW_TAG_partial_unit that can be included in several places in
14010 /// the DIE tree.
14011 ///
14012 /// @return a pointer to the resulting function_type_sptr.
14013 static function_type_sptr
build_function_type(reader & rdr,Dwarf_Die * die,class_or_union_sptr is_method,size_t where_offset)14014 build_function_type(reader& rdr,
14015 Dwarf_Die* die,
14016 class_or_union_sptr is_method,
14017 size_t where_offset)
14018 {
14019 function_type_sptr result;
14020
14021 if (!die)
14022 return result;
14023
14024 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
14025 || dwarf_tag(die) == DW_TAG_subprogram);
14026
14027 const die_source source = rdr.get_die_source(die);
14028
14029 {
14030 size_t off = dwarf_dieoffset(die);
14031 auto i = rdr.die_wip_function_types_map(source).find(off);
14032 if (i != rdr.die_wip_function_types_map(source).end())
14033 {
14034 function_type_sptr fn_type = is_function_type(i->second);
14035 ABG_ASSERT(fn_type);
14036 return fn_type;
14037 }
14038 }
14039
14040 decl_base_sptr type_decl;
14041
14042 translation_unit_sptr tu = rdr.cur_transl_unit();
14043 ABG_ASSERT(tu);
14044
14045 /// If, inside the current translation unit, we've already seen a
14046 /// function type with the same text representation, then reuse that
14047 /// one instead.
14048 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
14049 {
14050 result = is_function_type(t);
14051 ABG_ASSERT(result);
14052 rdr.associate_die_to_type(die, result, where_offset);
14053 return result;
14054 }
14055
14056 bool odr_is_relevant = rdr.odr_is_relevant(die);
14057 if (odr_is_relevant)
14058 {
14059 // So we can rely on the One Definition Rule to say that if
14060 // several different function types have the same name (or
14061 // rather, representation) across the entire binary, then they
14062 // ought to designate the same function type. So let's ensure
14063 // that if we've already seen a function type with the same
14064 // representation as the function type 'die', then it's the same
14065 // type as the one denoted by 'die'.
14066 if (function_type_sptr fn_type =
14067 is_function_type(rdr.lookup_type_artifact_from_die(die)))
14068 {
14069 rdr.associate_die_to_type(die, fn_type, where_offset);
14070 return fn_type;
14071 }
14072 }
14073
14074 // Let's look at the DIE to detect if it's the DIE for a method
14075 // (type). If it is, we can deduce the name of its enclosing class
14076 // and if it's a static or const.
14077 bool is_const = false;
14078 bool is_static = false;
14079 Dwarf_Die object_pointer_die;
14080 Dwarf_Die class_type_die;
14081 bool has_this_parm_die =
14082 die_function_type_is_method_type(rdr, die, where_offset,
14083 object_pointer_die,
14084 class_type_die,
14085 is_static);
14086 if (has_this_parm_die)
14087 {
14088 // The function (type) has a "this" parameter DIE. It means it's
14089 // a member function DIE.
14090 if (!is_static)
14091 if (die_object_pointer_is_for_const_method(&object_pointer_die))
14092 is_const = true;
14093
14094 if (!is_method)
14095 {
14096 // We were initially called as if the function represented
14097 // by DIE was *NOT* a member function. But now we know it's
14098 // a member function. Let's take that into account.
14099 class_or_union_sptr klass_type =
14100 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
14101 /*called_from_pub_decl=*/true,
14102 where_offset));
14103 ABG_ASSERT(klass_type);
14104 is_method = klass_type;
14105 }
14106 }
14107
14108 // Let's create the type early and record it as being for the DIE
14109 // 'die'. This way, when building the sub-type triggers the
14110 // creation of a type matching the same 'die', then we'll reuse this
14111 // one.
14112
14113 result.reset(is_method
14114 ? new method_type(is_method, is_const,
14115 tu->get_address_size(),
14116 /*alignment=*/0)
14117 : new function_type(rdr.env(), tu->get_address_size(),
14118 /*alignment=*/0));
14119 rdr.associate_die_to_type(die, result, where_offset);
14120 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
14121
14122 type_base_sptr return_type;
14123 Dwarf_Die ret_type_die;
14124 if (die_die_attribute(die, DW_AT_type, ret_type_die))
14125 return_type =
14126 is_type(build_ir_node_from_die(rdr, &ret_type_die,
14127 /*called_from_public_decl=*/true,
14128 where_offset));
14129 if (!return_type)
14130 return_type = is_type(build_ir_node_for_void_type(rdr));
14131 result->set_return_type(return_type);
14132
14133 Dwarf_Die child;
14134 function_decl::parameters function_parms;
14135
14136 if (dwarf_child(die, &child) == 0)
14137 do
14138 {
14139 int child_tag = dwarf_tag(&child);
14140 if (child_tag == DW_TAG_formal_parameter)
14141 {
14142 // This is a "normal" function parameter.
14143 string name, linkage_name;
14144 location loc;
14145 die_loc_and_name(rdr, &child, loc, name, linkage_name);
14146 if (!tools_utils::string_is_ascii_identifier(name))
14147 // Sometimes, bogus compiler emit names that are
14148 // non-ascii garbage. Let's just ditch that for now.
14149 name.clear();
14150 bool is_artificial = die_is_artificial(&child);
14151 type_base_sptr parm_type;
14152 Dwarf_Die parm_type_die;
14153 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
14154 parm_type =
14155 is_type(build_ir_node_from_die(rdr, &parm_type_die,
14156 /*called_from_public_decl=*/true,
14157 where_offset));
14158 if (!parm_type)
14159 continue;
14160 function_decl::parameter_sptr p
14161 (new function_decl::parameter(parm_type, name, loc,
14162 /*variadic_marker=*/false,
14163 is_artificial));
14164 function_parms.push_back(p);
14165 }
14166 else if (child_tag == DW_TAG_unspecified_parameters)
14167 {
14168 // This is a variadic function parameter.
14169 bool is_artificial = die_is_artificial(&child);
14170
14171 type_base_sptr parm_type =
14172 is_type(build_ir_node_for_variadic_parameter_type(rdr));
14173 function_decl::parameter_sptr p
14174 (new function_decl::parameter(parm_type,
14175 /*name=*/"",
14176 location(),
14177 /*variadic_marker=*/true,
14178 is_artificial));
14179 function_parms.push_back(p);
14180 // After a DW_TAG_unspecified_parameters tag, we shouldn't
14181 // keep reading for parameters. The
14182 // unspecified_parameters TAG should be the last parameter
14183 // that we record. For instance, if there are multiple
14184 // DW_TAG_unspecified_parameters DIEs then we should care
14185 // only for the first one.
14186 break;
14187 }
14188 }
14189 while (dwarf_siblingof(&child, &child) == 0);
14190
14191 result->set_parameters(function_parms);
14192
14193 tu->bind_function_type_life_time(result);
14194
14195 result->set_is_artificial(true);
14196
14197 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
14198
14199 {
14200 die_function_type_map_type::const_iterator i =
14201 rdr.die_wip_function_types_map(source).
14202 find(dwarf_dieoffset(die));
14203 if (i != rdr.die_wip_function_types_map(source).end())
14204 rdr.die_wip_function_types_map(source).erase(i);
14205 }
14206
14207 maybe_canonicalize_type(result, rdr);
14208 return result;
14209 }
14210
14211 /// Build a subrange type from a DW_TAG_subrange_type.
14212 ///
14213 /// @param rdr the DWARF reader to consider.
14214 ///
14215 /// @param die the DIE to read from.
14216 ///
14217 /// @param where_offset the offset of the DIE where we are "logically"
14218 /// positionned at in the DIE tree. This is useful when @p die is
14219 /// e,g, DW_TAG_partial_unit that can be included in several places in
14220 /// the DIE tree.
14221 ///
14222 /// @param associate_die_to_type if this is true then the resulting
14223 /// type is associated to the @p die, so that next time when the
14224 /// system looks up the type associated to it, the current resulting
14225 /// type is returned. If false, then no association is done and the
14226 /// resulting type can be destroyed right after. This can be useful
14227 /// when the sole purpose of building the @ref
14228 /// array_type_def::subrange_type is to use some of its method like,
14229 /// e.g, its name pretty printing methods.
14230 ///
14231 /// @return the newly built instance of @ref
14232 /// array_type_def::subrange_type, or nil if no type could be built.
14233 static array_type_def::subrange_sptr
build_subrange_type(reader & rdr,const Dwarf_Die * die,size_t where_offset,bool associate_type_to_die)14234 build_subrange_type(reader& rdr,
14235 const Dwarf_Die* die,
14236 size_t where_offset,
14237 bool associate_type_to_die)
14238 {
14239 array_type_def::subrange_sptr result;
14240
14241 if (!die)
14242 return result;
14243
14244 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
14245 if (tag != DW_TAG_subrange_type)
14246 return result;
14247
14248 string name = die_name(die);
14249
14250 // load the underlying type.
14251 Dwarf_Die underlying_type_die;
14252 type_base_sptr underlying_type;
14253 /* Unless there is an underlying type which says differently. */
14254 bool is_signed = false;
14255 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
14256 underlying_type =
14257 is_type(build_ir_node_from_die(rdr,
14258 &underlying_type_die,
14259 /*called_from_public_decl=*/true,
14260 where_offset));
14261
14262 if (underlying_type)
14263 {
14264 uint64_t ate;
14265 if (die_unsigned_constant_attribute (&underlying_type_die,
14266 DW_AT_encoding,
14267 ate))
14268 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
14269 }
14270
14271 translation_unit::language language = rdr.cur_transl_unit()->get_language();
14272 array_type_def::subrange_type::bound_value lower_bound =
14273 get_default_array_lower_bound(language);
14274 array_type_def::subrange_type::bound_value upper_bound;
14275 uint64_t count = 0;
14276 bool is_infinite = false;
14277 bool count_present = false;
14278
14279 // The DWARF 4 specifications says, in [5.11 Subrange
14280 // Type Entries]:
14281 //
14282 // The subrange entry may have the attributes
14283 // DW_AT_lower_bound and DW_AT_upper_bound to
14284 // specify, respectively, the lower and upper bound
14285 // values of the subrange.
14286 //
14287 // So let's look for DW_AT_lower_bound first.
14288 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14289
14290 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
14291 is_signed, upper_bound);
14292 if (!found_upper_bound)
14293 found_upper_bound = subrange_die_indirect_bound_value(die,
14294 DW_AT_upper_bound,
14295 upper_bound,
14296 is_signed);
14297 // Then, DW_AT_upper_bound.
14298 if (!found_upper_bound)
14299 {
14300 // The DWARF 4 spec says, in [5.11 Subrange Type
14301 // Entries]:
14302 //
14303 // The DW_AT_upper_bound attribute may be replaced
14304 // by a DW_AT_count attribute, whose value
14305 // describes the number of elements in the
14306 // subrange rather than the value of the last
14307 // element."
14308 //
14309 // So, as DW_AT_upper_bound is not present in this
14310 // case, let's see if there is a DW_AT_count.
14311 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
14312 {
14313 count_present = true;
14314 // We can deduce the upper_bound from the
14315 // lower_bound and the number of elements of the
14316 // array:
14317 int64_t u = lower_bound.get_signed_value() + count;
14318 upper_bound = u - 1;
14319 }
14320
14321 if (!count_present)
14322 // No upper_bound nor count was present on the DIE, this means
14323 // the array is considered to have an infinite (or rather not
14324 // known) size.
14325 is_infinite = true;
14326 }
14327
14328 if (UINT64_MAX == upper_bound.get_unsigned_value())
14329 // If the upper_bound size is the max of the integer value
14330 // then it most certainly means unknown size.
14331 is_infinite = true;
14332
14333 result.reset
14334 (new array_type_def::subrange_type(rdr.env(),
14335 name,
14336 lower_bound,
14337 upper_bound,
14338 location()));
14339 result->is_infinite(is_infinite);
14340
14341 if (underlying_type)
14342 result->set_underlying_type(underlying_type);
14343
14344 // Let's ensure the resulting subrange looks metabolically healhty.
14345 ABG_ASSERT(result->is_infinite()
14346 || (result->get_length() ==
14347 (uint64_t) (result->get_upper_bound()
14348 - result->get_lower_bound() + 1)));
14349
14350 if (associate_type_to_die)
14351 rdr.associate_die_to_type(die, result, where_offset);
14352
14353 return result;
14354 }
14355
14356 /// Build the sub-ranges of an array type.
14357 ///
14358 /// This is a sub-routine of build_array_type().
14359 ///
14360 /// @param rdr the context to read from.
14361 ///
14362 /// @param die the DIE of tag DW_TAG_array_type which contains
14363 /// children DIEs that represent the sub-ranges.
14364 ///
14365 /// @param subranges out parameter. This is set to the sub-ranges
14366 /// that are built from @p die.
14367 ///
14368 /// @param where_offset the offset of the DIE where we are "logically"
14369 /// positioned at, in the DIE tree. This is useful when @p die is
14370 /// e.g, DW_TAG_partial_unit that can be included in several places in
14371 /// the DIE tree.
14372 static void
build_subranges_from_array_type_die(reader & rdr,const Dwarf_Die * die,array_type_def::subranges_type & subranges,size_t where_offset,bool associate_type_to_die)14373 build_subranges_from_array_type_die(reader& rdr,
14374 const Dwarf_Die* die,
14375 array_type_def::subranges_type& subranges,
14376 size_t where_offset,
14377 bool associate_type_to_die)
14378 {
14379 Dwarf_Die child;
14380
14381 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14382 {
14383 do
14384 {
14385 int child_tag = dwarf_tag(&child);
14386 if (child_tag == DW_TAG_subrange_type)
14387 {
14388 array_type_def::subrange_sptr s;
14389 if (associate_type_to_die)
14390 {
14391 // We are being called to create the type, add it to
14392 // the current type graph and associate it to the
14393 // DIE it's been created from.
14394 type_or_decl_base_sptr t =
14395 build_ir_node_from_die(rdr, &child,
14396 /*called_from_public_decl=*/true,
14397 where_offset);
14398 s = is_subrange_type(t);
14399 }
14400 else
14401 // We are being called to create the type but *NOT*
14402 // add it to the current tyupe tree, *NOR* associate
14403 // it to the DIE it's been created from.
14404 s = build_subrange_type(rdr, &child,
14405 where_offset,
14406 /*associate_type_to_die=*/false);
14407 if (s)
14408 subranges.push_back(s);
14409 }
14410 }
14411 while (dwarf_siblingof(&child, &child) == 0);
14412 }
14413 }
14414
14415 /// Build an array type from a DW_TAG_array_type DIE.
14416 ///
14417 /// @param rdr the DWARF reader to consider.
14418 ///
14419 /// @param die the DIE to read from.
14420 ///
14421 /// @param called_from_public_decl true if this function was called
14422 /// from a context where either a public function or a public variable
14423 /// is being built.
14424 ///
14425 /// @param where_offset the offset of the DIE where we are "logically"
14426 /// positioned at, in the DIE tree. This is useful when @p die is
14427 /// e.g, DW_TAG_partial_unit that can be included in several places in
14428 /// the DIE tree.
14429 ///
14430 /// @return a pointer to the resulting array_type_def.
14431 static array_type_def_sptr
build_array_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)14432 build_array_type(reader& rdr,
14433 Dwarf_Die* die,
14434 bool called_from_public_decl,
14435 size_t where_offset)
14436 {
14437 array_type_def_sptr result;
14438
14439 if (!die)
14440 return result;
14441
14442 unsigned tag = dwarf_tag(die);
14443 if (tag != DW_TAG_array_type)
14444 return result;
14445
14446 decl_base_sptr type_decl;
14447 Dwarf_Die type_die;
14448
14449 if (die_die_attribute(die, DW_AT_type, type_die))
14450 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14451 called_from_public_decl,
14452 where_offset));
14453 if (!type_decl)
14454 return result;
14455
14456 // The call to build_ir_node_from_die() could have triggered the
14457 // creation of the type for this DIE. In that case, just return it.
14458 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14459 {
14460 result = is_array_type(t);
14461 ABG_ASSERT(result);
14462 return result;
14463 }
14464
14465 type_base_sptr type = is_type(type_decl);
14466 ABG_ASSERT(type);
14467
14468 array_type_def::subranges_type subranges;
14469
14470 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14471
14472 result.reset(new array_type_def(type, subranges, location()));
14473
14474 return result;
14475 }
14476
14477 /// Create a typedef_decl from a DW_TAG_typedef DIE.
14478 ///
14479 /// @param rdr the DWARF reader to consider.
14480 ///
14481 /// @param die the DIE to read from.
14482 ///
14483 /// @param called_from_public_decl true if this function was called
14484 /// from a context where either a public function or a public variable
14485 /// is being built.
14486 ///
14487 /// @param where_offset the offset of the DIE where we are "logically"
14488 /// positionned at, in the DIE tree. This is useful when @p die is
14489 /// e.g, DW_TAG_partial_unit that can be included in several places in
14490 /// the DIE tree.
14491 ///
14492 /// @return the newly created typedef_decl.
14493 static typedef_decl_sptr
build_typedef_type(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)14494 build_typedef_type(reader& rdr,
14495 Dwarf_Die* die,
14496 bool called_from_public_decl,
14497 size_t where_offset)
14498 {
14499 typedef_decl_sptr result;
14500
14501 if (!die)
14502 return result;
14503
14504 unsigned tag = dwarf_tag(die);
14505 if (tag != DW_TAG_typedef)
14506 return result;
14507
14508 string name, linkage_name;
14509 location loc;
14510 die_loc_and_name(rdr, die, loc, name, linkage_name);
14511
14512 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14513 if (loc)
14514 result = lookup_typedef_type_per_location(loc.expand(), *corp);
14515
14516 if (!result)
14517 {
14518 type_base_sptr utype;
14519 Dwarf_Die underlying_type_die;
14520 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14521 // A typedef DIE with no underlying type means a typedef to
14522 // void type.
14523 utype = rdr.env().get_void_type();
14524
14525 if (!utype)
14526 utype =
14527 is_type(build_ir_node_from_die(rdr,
14528 &underlying_type_die,
14529 called_from_public_decl,
14530 where_offset));
14531 if (!utype)
14532 return result;
14533
14534 ABG_ASSERT(utype);
14535 result.reset(new typedef_decl(name, utype, loc, linkage_name));
14536
14537 if ((is_class_or_union_type(utype) || is_enum_type(utype))
14538 && is_anonymous_type(utype))
14539 {
14540 // This is a naming typedef for an enum or a class. Let's
14541 // mark the underlying decl as such.
14542 decl_base_sptr decl = is_decl(utype);
14543 ABG_ASSERT(decl);
14544 decl->set_naming_typedef(result);
14545 }
14546 }
14547
14548 rdr.associate_die_to_type(die, result, where_offset);
14549
14550 return result;
14551 }
14552
14553 /// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
14554 /// denoted by the DIE is not suppressed by a suppression
14555 /// specification associated to the current DWARF reader.
14556 ///
14557 /// Note that if a member variable declaration with the same name as
14558 /// the name of the DIE we are looking at exists, this function returns
14559 /// that existing variable declaration.
14560 ///
14561 /// @param rdr the DWARF reader to use.
14562 ///
14563 /// @param die the DIE representing the variable we are looking at.
14564 ///
14565 /// @param where_offset the offset of the DIE where we are "logically"
14566 /// positionned at, in the DIE tree. This is useful when @p die is
14567 /// e.g, DW_TAG_partial_unit that can be included in several places in
14568 /// the DIE tree.
14569 ///
14570 /// @param result if this is set to an existing var_decl, this means
14571 /// that the function will append the new properties it sees on @p die
14572 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14573 /// new var_decl is going to be allocated and returned.
14574 ///
14575 /// @param is_required_decl_spec this is true iff the variable to
14576 /// build is referred to as being the specification of another
14577 /// variable.
14578 ///
14579 /// @return a pointer to the newly created var_decl. If the var_decl
14580 /// could not be built, this function returns NULL.
14581 static var_decl_sptr
build_or_get_var_decl_if_not_suppressed(reader & rdr,scope_decl * scope,Dwarf_Die * die,size_t where_offset,var_decl_sptr result,bool is_required_decl_spec)14582 build_or_get_var_decl_if_not_suppressed(reader& rdr,
14583 scope_decl *scope,
14584 Dwarf_Die *die,
14585 size_t where_offset,
14586 var_decl_sptr result,
14587 bool is_required_decl_spec)
14588 {
14589 var_decl_sptr var;
14590 if (variable_is_suppressed(rdr, scope, die, is_required_decl_spec))
14591 return var;
14592
14593 if (class_decl* class_type = is_class_type(scope))
14594 {
14595 string var_name = die_name(die);
14596 if (!var_name.empty())
14597 if ((var = class_type->find_data_member(var_name)))
14598 return var;
14599 }
14600 var = build_var_decl(rdr, die, where_offset, result);
14601 return var;
14602 }
14603
14604 /// Build a @ref var_decl out of a DW_TAG_variable DIE.
14605 ///
14606 /// @param rdr the DWARF reader to use.
14607 ///
14608 /// @param die the DIE representing the variable we are looking at.
14609 ///
14610 /// @param where_offset the offset of the DIE where we are "logically"
14611 /// positionned at, in the DIE tree. This is useful when @p die is
14612 /// e.g, DW_TAG_partial_unit that can be included in several places in
14613 /// the DIE tree.
14614 ///
14615 /// @param result if this is set to an existing var_decl, this means
14616 /// that the function will append the new properties it sees on @p die
14617 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14618 /// new var_decl is going to be allocated and returned.
14619 ///
14620 /// @return a pointer to the newly created var_decl. If the var_decl
14621 /// could not be built, this function returns NULL.
14622 static var_decl_sptr
build_var_decl(reader & rdr,Dwarf_Die * die,size_t where_offset,var_decl_sptr result)14623 build_var_decl(reader& rdr,
14624 Dwarf_Die *die,
14625 size_t where_offset,
14626 var_decl_sptr result)
14627 {
14628 if (!die)
14629 return result;
14630
14631 int tag = dwarf_tag(die);
14632 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
14633
14634 if (!die_is_public_decl(die))
14635 return result;
14636
14637 type_base_sptr type;
14638 Dwarf_Die type_die;
14639 if (die_die_attribute(die, DW_AT_type, type_die))
14640 {
14641 decl_base_sptr ty =
14642 is_decl(build_ir_node_from_die(rdr, &type_die,
14643 /*called_from_public_decl=*/true,
14644 where_offset));
14645 if (!ty)
14646 return result;
14647 type = is_type(ty);
14648 ABG_ASSERT(type);
14649 }
14650
14651 if (!type && !result)
14652 return result;
14653
14654 string name, linkage_name;
14655 location loc;
14656 die_loc_and_name(rdr, die, loc, name, linkage_name);
14657
14658 if (!result)
14659 result.reset(new var_decl(name, type, loc, linkage_name));
14660 else
14661 {
14662 // We were called to append properties that might have been
14663 // missing from the first version of the variable. And usually
14664 // that missing property is the mangled name or the type.
14665 if (!linkage_name.empty())
14666 result->set_linkage_name(linkage_name);
14667
14668 if (type)
14669 result->set_type(type);
14670 }
14671
14672 // Check if a variable symbol with this name is exported by the elf
14673 // binary. If it is, then set the symbol of the variable, if it's
14674 // not set already.
14675 if (!result->get_symbol())
14676 {
14677 elf_symbol_sptr var_sym;
14678 Dwarf_Addr var_addr;
14679
14680 if (rdr.get_variable_address(die, var_addr))
14681 {
14682 rdr.symtab()->
14683 update_main_symbol(var_addr,
14684 result->get_linkage_name().empty()
14685 ? result->get_name()
14686 : result->get_linkage_name());
14687 var_sym = rdr.variable_symbol_is_exported(var_addr);
14688 }
14689
14690 if (var_sym)
14691 {
14692 result->set_symbol(var_sym);
14693 // If the linkage name is not set or is wrong, set it to
14694 // the name of the underlying symbol.
14695 string linkage_name = result->get_linkage_name();
14696 if (linkage_name.empty()
14697 || !var_sym->get_alias_from_name(linkage_name))
14698 result->set_linkage_name(var_sym->get_name());
14699 result->set_is_in_public_symbol_table(true);
14700 }
14701 }
14702
14703 return result;
14704 }
14705
14706 /// Test if a given function denoted by its DIE and its scope is
14707 /// suppressed by any of the suppression specifications associated to
14708 /// a given context of ELF/DWARF reading.
14709 ///
14710 /// Note that a non-member function which symbol is not exported is
14711 /// also suppressed.
14712 ///
14713 /// @param rdr the ELF/DWARF reading content of interest.
14714 ///
14715 /// @param scope of the scope of the function.
14716 ///
14717 /// @param function_die the DIE representing the function.
14718 ///
14719 /// @param is_declaration_only is true if the DIE denoted by @p die is
14720 /// a declaration-only DIE.
14721 ///
14722 /// @return true iff @p function_die is suppressed by at least one
14723 /// suppression specification attached to the @p rdr.
14724 static bool
function_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * function_die,bool is_declaration_only)14725 function_is_suppressed(const reader& rdr,
14726 const scope_decl* scope,
14727 Dwarf_Die *function_die,
14728 bool is_declaration_only)
14729 {
14730 if (function_die == 0
14731 || dwarf_tag(function_die) != DW_TAG_subprogram)
14732 return false;
14733
14734 string fname = die_string_attribute(function_die, DW_AT_name);
14735 string flinkage_name = die_linkage_name(function_die);
14736 if (flinkage_name.empty() && rdr.die_is_in_c(function_die))
14737 flinkage_name = fname;
14738 string qualified_name = build_qualified_name(scope, fname);
14739
14740 // A non-member non-static function which symbol is not exported is
14741 // suppressed.
14742 //
14743 // Note that if the non-member non-static function has an undefined
14744 // symbol, by default, it's not suppressed. Unless we are asked to
14745 // drop undefined symbols too.
14746 if (!is_class_type(scope)
14747 && (!is_declaration_only || rdr.drop_undefined_syms()))
14748 {
14749 Dwarf_Addr fn_addr;
14750 if (!rdr.get_function_address(function_die, fn_addr))
14751 return true;
14752
14753 elf_symbol_sptr symbol =
14754 rdr.function_symbol_is_exported(fn_addr);
14755 if (!symbol)
14756 return true;
14757 if (!symbol->is_suppressed())
14758 return false;
14759
14760 // Since there is only one symbol in DWARF associated with an elf_symbol,
14761 // we can assume this is the main symbol then. Otherwise the main hinting
14762 // did not work as expected.
14763 ABG_ASSERT(symbol->is_main_symbol());
14764 if (symbol->has_aliases())
14765 for (elf_symbol_sptr a = symbol->get_next_alias();
14766 !a->is_main_symbol(); a = a->get_next_alias())
14767 if (!a->is_suppressed())
14768 return false;
14769 }
14770
14771 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
14772 /*require_drop_property=*/true);
14773 }
14774
14775 /// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
14776 /// function denoted by the DIE is not suppressed by a suppression
14777 /// specification associated to the current DWARF reader.
14778 ///
14779 /// Note that if a member function declaration with the same signature
14780 /// (pretty representation) as one of the DIE we are looking at
14781 /// exists, this function returns that existing function declaration.
14782 /// Similarly, if there is already a constructed member function with
14783 /// the same linkage name as the one on the DIE, this function returns
14784 /// that member function.
14785 ///
14786 /// Also note that the function_decl IR returned by this function must
14787 /// be passed to finish_member_function_reading because several
14788 /// properties from the DIE are actually read by that function, and
14789 /// the corresponding properties on the function_decl IR are updated
14790 /// accordingly. This is done to support "updating" a function_decl
14791 /// IR with properties scathered across several DIEs.
14792 ///
14793 /// @param rdr the DWARF reader to use.
14794 ///
14795 /// @param scope the scope of the function we are looking at.
14796 ///
14797 /// @param fn_die the DIE representing the function we are looking at.
14798 ///
14799 /// @param where_offset the offset of the DIE where we are "logically"
14800 /// positionned at, in the DIE tree. This is useful when @p die is
14801 /// e.g, DW_TAG_partial_unit that can be included in several places in
14802 /// the DIE tree.
14803 ///
14804 /// @param is_declaration_only is true if the DIE denoted by @p fn_die
14805 /// is a declaration-only DIE.
14806 ///
14807 /// @param result if this is set to an existing function_decl, this
14808 /// means that the function will append the new properties it sees on
14809 /// @p fn_die to that exising function_decl. Otherwise, if this
14810 /// parameter is NULL, a new function_decl is going to be allocated
14811 /// and returned.
14812 ///
14813 /// @return a pointer to the newly created var_decl. If the var_decl
14814 /// could not be built, this function returns NULL.
14815 static function_decl_sptr
build_or_get_fn_decl_if_not_suppressed(reader & rdr,scope_decl * scope,Dwarf_Die * fn_die,size_t where_offset,bool is_declaration_only,function_decl_sptr result)14816 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
14817 scope_decl *scope,
14818 Dwarf_Die *fn_die,
14819 size_t where_offset,
14820 bool is_declaration_only,
14821 function_decl_sptr result)
14822 {
14823 function_decl_sptr fn;
14824 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
14825 return fn;
14826
14827 string name = die_name(fn_die);
14828 string linkage_name = die_linkage_name(fn_die);
14829 bool is_dtor = !name.empty() && name[0]== '~';
14830 bool is_virtual = false;
14831 if (is_dtor)
14832 {
14833 Dwarf_Attribute attr;
14834 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
14835 DW_AT_vtable_elem_location,
14836 &attr))
14837 is_virtual = true;
14838 }
14839
14840
14841 // If we've already built an IR for a function with the same
14842 // signature (from another DIE), reuse it, unless that function is a
14843 // virtual C++ destructor. Several virtual C++ destructors with the
14844 // same signature can be implemented by several different ELF
14845 // symbols. So re-using C++ destructors like that can lead to us
14846 // missing some destructors.
14847 if (!result && (!(is_dtor && is_virtual)))
14848 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
14849 {
14850 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
14851 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
14852 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
14853 return fn;
14854 }
14855
14856 // If a member function with the same linkage name as the one
14857 // carried by the DIE already exists, then return it.
14858 if (class_decl* klass = is_class_type(scope))
14859 {
14860 string linkage_name = die_linkage_name(fn_die);
14861 fn = klass->find_member_function_sptr(linkage_name);
14862 if (fn)
14863 // We found a member function that has the same signature.
14864 // Let's mark it for update.
14865 result = fn;
14866 }
14867
14868 if (!fn || !fn->get_symbol())
14869 // We haven't yet been able to construct a function IR, or, we
14870 // have one 'partial' function IR that doesn't have any associated
14871 // symbol yet. Note that in the later case, a function IR without
14872 // any associated symbol will be dropped on the floor by
14873 // potential_member_fn_should_be_dropped. So let's build or a new
14874 // function IR or complete the existing partial IR.
14875 fn = build_function_decl(rdr, fn_die, where_offset, result);
14876
14877 return fn;
14878 }
14879
14880 /// Test if a given variable denoted by its DIE and its scope is
14881 /// suppressed by any of the suppression specifications associated to
14882 /// a given context of ELF/DWARF reading.
14883 ///
14884 /// @param rdr the ELF/DWARF reading content of interest.
14885 ///
14886 /// @param scope of the scope of the variable.
14887 ///
14888 /// @param variable_die the DIE representing the variable.
14889 ///
14890 /// @param is_required_decl_spec if true, means that the @p
14891 /// variable_die being considered is for a variable decl that is a
14892 /// specification for a concrete variable being built.
14893 ///
14894 /// @return true iff @p variable_die is suppressed by at least one
14895 /// suppression specification attached to the @p rdr.
14896 static bool
variable_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * variable_die,bool is_required_decl_spec)14897 variable_is_suppressed(const reader& rdr,
14898 const scope_decl* scope,
14899 Dwarf_Die *variable_die,
14900 bool is_required_decl_spec)
14901 {
14902 if (variable_die == 0
14903 || (dwarf_tag(variable_die) != DW_TAG_variable
14904 && dwarf_tag(variable_die) != DW_TAG_member))
14905 return false;
14906
14907 string name = die_string_attribute(variable_die, DW_AT_name);
14908 string linkage_name = die_linkage_name(variable_die);
14909 if (linkage_name.empty() && rdr.die_is_in_c(variable_die))
14910 linkage_name = name;
14911 string qualified_name = build_qualified_name(scope, name);
14912
14913 // If a non member variable that is a declaration (has no defined
14914 // and exported symbol) and is not the specification of another
14915 // concrete variable, then it's suppressed. This is a size
14916 // optimization; it removes useless declaration-only variables from
14917 // the IR.
14918 if (!is_class_type(scope) && !is_required_decl_spec)
14919 {
14920 Dwarf_Addr var_addr = 0;
14921 if (!rdr.get_variable_address(variable_die, var_addr))
14922 return true;
14923
14924 elf_symbol_sptr symbol =
14925 rdr.variable_symbol_is_exported(var_addr);
14926 if (!symbol)
14927 return true;
14928 if (!symbol->is_suppressed())
14929 return false;
14930
14931 // Since there is only one symbol in DWARF associated with an elf_symbol,
14932 // we can assume this is the main symbol then. Otherwise the main hinting
14933 // did not work as expected.
14934 ABG_ASSERT(symbol->is_main_symbol());
14935 if (symbol->has_aliases())
14936 for (elf_symbol_sptr a = symbol->get_next_alias();
14937 !a->is_main_symbol(); a = a->get_next_alias())
14938 if (!a->is_suppressed())
14939 return false;
14940 }
14941
14942 return suppr::is_variable_suppressed(rdr,
14943 qualified_name,
14944 linkage_name,
14945 /*require_drop_property=*/true);
14946 }
14947
14948 /// Test if a type (designated by a given DIE) in a given scope is
14949 /// suppressed by the suppression specifications that are associated
14950 /// to a given DWARF reader.
14951 ///
14952 /// @param rdr the DWARF reader to consider.
14953 ///
14954 /// @param scope of the scope of the type DIE to consider.
14955 ///
14956 /// @param type_die the DIE that designates the type to consider.
14957 ///
14958 /// @param type_is_private out parameter. If this function returns
14959 /// true (the type @p type_die is suppressed) and if the type was
14960 /// suppressed because it's private then this parameter is set to
14961 /// true.
14962 ///
14963 /// @return true iff the type designated by the DIE @p type_die, in
14964 /// the scope @p scope is suppressed by at the suppression
14965 /// specifications associated to the current DWARF reader.
14966 static bool
type_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * type_die,bool & type_is_private)14967 type_is_suppressed(const reader& rdr,
14968 const scope_decl* scope,
14969 Dwarf_Die *type_die,
14970 bool &type_is_private)
14971 {
14972 if (type_die == 0
14973 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
14974 && dwarf_tag(type_die) != DW_TAG_class_type
14975 && dwarf_tag(type_die) != DW_TAG_structure_type
14976 && dwarf_tag(type_die) != DW_TAG_union_type))
14977 return false;
14978
14979 string type_name, linkage_name;
14980 location type_location;
14981 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
14982 string qualified_name = build_qualified_name(scope, type_name);
14983
14984 return suppr::is_type_suppressed(rdr,
14985 qualified_name,
14986 type_location,
14987 type_is_private,
14988 /*require_drop_property=*/true);
14989 }
14990
14991 /// Test if a type (designated by a given DIE) in a given scope is
14992 /// suppressed by the suppression specifications that are associated
14993 /// to a given DWARF reader.
14994 ///
14995 /// @param rdr the DWARF reader to consider.
14996 ///
14997 /// @param scope of the scope of the type DIE to consider.
14998 ///
14999 /// @param type_die the DIE that designates the type to consider.
15000 ///
15001 /// @return true iff the type designated by the DIE @p type_die, in
15002 /// the scope @p scope is suppressed by at the suppression
15003 /// specifications associated to the current DWARF reader.
15004 static bool
type_is_suppressed(const reader & rdr,const scope_decl * scope,Dwarf_Die * type_die)15005 type_is_suppressed(const reader& rdr,
15006 const scope_decl* scope,
15007 Dwarf_Die *type_die)
15008 {
15009 bool type_is_private = false;
15010 return type_is_suppressed(rdr, scope, type_die, type_is_private);
15011 }
15012
15013 /// Get the opaque version of a type that was suppressed because it's
15014 /// a private type.
15015 ///
15016 /// The opaque version version of the type is just a declared-only
15017 /// version of the type (class, union or enum type) denoted by @p
15018 /// type_die.
15019 ///
15020 /// @param rdr the DWARF reader in use.
15021 ///
15022 /// @param scope the scope of the type die we are looking at.
15023 ///
15024 /// @param type_die the type DIE we are looking at.
15025 ///
15026 /// @param where_offset the offset of the DIE where we are "logically"
15027 /// positionned at, in the DIE tree. This is useful when @p die is
15028 /// e.g, DW_TAG_partial_unit that can be included in several places in
15029 /// the DIE tree.
15030 ///
15031 /// @return the opaque version of the type denoted by @p type_die or
15032 /// nil if no opaque version was found.
15033 static type_or_decl_base_sptr
get_opaque_version_of_type(reader & rdr,scope_decl * scope,Dwarf_Die * type_die,size_t where_offset)15034 get_opaque_version_of_type(reader &rdr,
15035 scope_decl *scope,
15036 Dwarf_Die *type_die,
15037 size_t where_offset)
15038 {
15039 type_or_decl_base_sptr result;
15040
15041 if (type_die == 0)
15042 return result;
15043
15044 unsigned tag = dwarf_tag(type_die);
15045 if (tag != DW_TAG_class_type
15046 && tag != DW_TAG_structure_type
15047 && tag != DW_TAG_union_type
15048 && tag != DW_TAG_enumeration_type)
15049 return result;
15050
15051 string type_name, linkage_name;
15052 location type_location;
15053 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15054 if (!type_location)
15055 return result;
15056
15057 string qualified_name = build_qualified_name(scope, type_name);
15058
15059 //
15060 // TODO: also handle declaration-only unions. To do that, we mostly
15061 // need to adapt add_or_update_union_type to make it schedule
15062 // declaration-only unions for resolution too.
15063 //
15064 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
15065 {
15066 string_classes_or_unions_map::const_iterator i =
15067 rdr.declaration_only_classes().find(qualified_name);
15068 if (i != rdr.declaration_only_classes().end())
15069 result = i->second.back();
15070
15071 if (!result)
15072 {
15073 // So we didn't find any pre-existing forward-declared-only
15074 // class for the class definition that we could return as an
15075 // opaque type. So let's build one.
15076 //
15077 // TODO: we need to be able to do this for unions too!
15078 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
15079 /*alignment=*/0, /*size=*/0,
15080 tag == DW_TAG_structure_type,
15081 type_location,
15082 decl_base::VISIBILITY_DEFAULT));
15083 klass->set_is_declaration_only(true);
15084 klass->set_is_artificial(die_is_artificial(type_die));
15085 add_decl_to_scope(klass, scope);
15086 rdr.associate_die_to_type(type_die, klass, where_offset);
15087 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
15088 result = klass;
15089 }
15090 }
15091
15092 if (tag == DW_TAG_enumeration_type)
15093 {
15094 string_enums_map::const_iterator i =
15095 rdr.declaration_only_enums().find(qualified_name);
15096 if (i != rdr.declaration_only_enums().end())
15097 result = i->second.back();
15098
15099 if (!result)
15100 {
15101 uint64_t size = 0;
15102 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
15103 size *= 8;
15104 type_decl_sptr underlying_type =
15105 build_enum_underlying_type(rdr, type_name, size,
15106 /*anonymous=*/true);
15107 enum_type_decl::enumerators enumeratorz;
15108 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
15109 type_location,
15110 underlying_type,
15111 enumeratorz,
15112 linkage_name));
15113 enum_type->set_is_artificial(die_is_artificial(type_die));
15114 add_decl_to_scope(enum_type, scope);
15115 result = enum_type;
15116 }
15117 }
15118
15119 return result;
15120 }
15121
15122 /// Create a function symbol with a given name.
15123 ///
15124 /// @param sym_name the name of the symbol to create.
15125 ///
15126 /// @param env the environment to create the symbol in.
15127 ///
15128 /// @return the newly created symbol.
15129 elf_symbol_sptr
create_default_fn_sym(const string & sym_name,const environment & env)15130 create_default_fn_sym(const string& sym_name, const environment& env)
15131 {
15132 elf_symbol::version ver;
15133 elf_symbol_sptr result =
15134 elf_symbol::create(env,
15135 /*symbol index=*/ 0,
15136 /*symbol size=*/ 0,
15137 sym_name,
15138 /*symbol type=*/ elf_symbol::FUNC_TYPE,
15139 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
15140 /*symbol is defined=*/ true,
15141 /*symbol is common=*/ false,
15142 /*symbol version=*/ ver,
15143 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
15144 return result;
15145 }
15146
15147 /// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
15148 ///
15149 /// @param rdr the DWARF reader to use
15150 ///
15151 /// @param die the DW_TAG_subprogram DIE to read from.
15152 ///
15153 /// @param where_offset the offset of the DIE where we are "logically"
15154 /// positionned at, in the DIE tree. This is useful when @p die is
15155 /// e.g, DW_TAG_partial_unit that can be included in several places in
15156 /// the DIE tree.
15157 ///
15158 /// @param called_for_public_decl this is set to true if the function
15159 /// was called for a public (function) decl.
15160 static function_decl_sptr
build_function_decl(reader & rdr,Dwarf_Die * die,size_t where_offset,function_decl_sptr fn)15161 build_function_decl(reader& rdr,
15162 Dwarf_Die* die,
15163 size_t where_offset,
15164 function_decl_sptr fn)
15165 {
15166 function_decl_sptr result = fn;
15167 if (!die)
15168 return result;
15169 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
15170
15171 if (!die_is_public_decl(die))
15172 return result;
15173
15174 translation_unit_sptr tu = rdr.cur_transl_unit();
15175 ABG_ASSERT(tu);
15176
15177 string fname, flinkage_name;
15178 location floc;
15179 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
15180
15181 size_t is_inline = die_is_declared_inline(die);
15182 class_or_union_sptr is_method =
15183 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
15184
15185 if (result)
15186 {
15187 // Add the properties that might have been missing from the
15188 // first declaration of the function. For now, it usually is
15189 // the mangled name that goes missing in the first declarations.
15190 //
15191 // Also note that if 'fn' has just been cloned, the current
15192 // linkage name (of the current DIE) might be different from the
15193 // linkage name of 'fn'. In that case, update the linkage name
15194 // of 'fn' too.
15195 if (!flinkage_name.empty()
15196 && result->get_linkage_name() != flinkage_name)
15197 result->set_linkage_name(flinkage_name);
15198 if (floc)
15199 if (!result->get_location())
15200 result->set_location(floc);
15201 }
15202 else
15203 {
15204 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
15205 where_offset));
15206 if (!fn_type)
15207 return result;
15208
15209 maybe_canonicalize_type(fn_type, rdr);
15210
15211 result.reset(is_method
15212 ? new method_decl(fname, fn_type,
15213 is_inline, floc,
15214 flinkage_name)
15215 : new function_decl(fname, fn_type,
15216 is_inline, floc,
15217 flinkage_name));
15218 }
15219
15220 // Set the symbol of the function. If the linkage name is not set
15221 // or is wrong, set it to the name of the underlying symbol.
15222 if (!result->get_symbol())
15223 {
15224 elf_symbol_sptr fn_sym;
15225 Dwarf_Addr fn_addr;
15226 if (rdr.get_function_address(die, fn_addr))
15227 {
15228 rdr.symtab()->
15229 update_main_symbol(fn_addr,
15230 result->get_linkage_name().empty()
15231 ? result->get_name()
15232 : result->get_linkage_name());
15233 fn_sym = rdr.function_symbol_is_exported(fn_addr);
15234 }
15235
15236 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
15237 {
15238 result->set_symbol(fn_sym);
15239 string linkage_name = result->get_linkage_name();
15240 if (linkage_name.empty())
15241 result->set_linkage_name(fn_sym->get_name());
15242 result->set_is_in_public_symbol_table(true);
15243 }
15244 }
15245
15246 rdr.associate_die_to_type(die, result->get_type(), where_offset);
15247
15248 size_t die_offset = dwarf_dieoffset(die);
15249
15250 if (fn
15251 && is_member_function(fn)
15252 && get_member_function_is_virtual(fn)
15253 && !result->get_linkage_name().empty())
15254 // This function is a virtual member function which has its
15255 // linkage name *and* and has its underlying symbol correctly set.
15256 // It thus doesn't need any fixup related to elf symbol. So
15257 // remove it from the set of virtual member functions with linkage
15258 // names and no elf symbol that need to be fixed up.
15259 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
15260 return result;
15261 }
15262
15263 /// Canonicalize a type if it's suitable for early canonicalizing, or,
15264 /// if it's not, schedule it for late canonicalization, after the
15265 /// debug info of the current translation unit has been fully read.
15266 ///
15267 /// A (composite) type is deemed suitable for early canonicalizing iff
15268 /// all of its sub-types are canonicalized themselve. Non composite
15269 /// types are always deemed suitable for early canonicalization.
15270 ///
15271 /// Note that this function knows how to deal with anonymous classes,
15272 /// structs and enums, unlike the overload below:
15273 ///
15274 /// @param t the type DIE to consider for canonicalization.
15275 ///
15276 /// @param rdr the @ref reader to use.
15277 static void
maybe_canonicalize_type(const type_base_sptr & t,reader & rdr)15278 maybe_canonicalize_type(const type_base_sptr& t,
15279 reader& rdr)
15280 {
15281 if (!t)
15282 return;
15283
15284 type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15285 if (is_class_type(peeled_type)
15286 || is_union_type(peeled_type)
15287 || is_function_type(peeled_type)
15288 || is_array_type(peeled_type)
15289 || is_qualified_type(peeled_type)
15290 || is_enum_type(peeled_type)
15291 ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15292 // We delay canonicalization of classes/unions or typedef,
15293 // pointers, references and array to classes/unions. This is
15294 // because the (underlying) class might not be finished yet and we
15295 // might not be able to able detect it here (thinking about
15296 // classes that are work-in-progress, or classes that might be
15297 // later amended by some DWARF construct). So we err on the safe
15298 // side. We also delay canonicalization for array and qualified
15299 // types because they can be edited (in particular by
15300 // maybe_strip_qualification) after they are initially built.
15301 rdr.schedule_type_for_late_canonicalization(t);
15302 else if (type_has_non_canonicalized_subtype(t))
15303 rdr.schedule_type_for_late_canonicalization(t);
15304 else
15305 canonicalize(t);
15306 }
15307
15308 /// If a given decl is a member type declaration, set its access
15309 /// specifier from the DIE that represents it.
15310 ///
15311 /// @param member_type_declaration the member type declaration to
15312 /// consider.
15313 static void
maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,Dwarf_Die * die)15314 maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15315 Dwarf_Die* die)
15316 {
15317 if (is_type(member_type_declaration)
15318 && is_member_decl(member_type_declaration))
15319 {
15320 class_or_union* scope =
15321 is_class_or_union_type(member_type_declaration->get_scope());
15322 ABG_ASSERT(scope);
15323
15324 access_specifier access = public_access;
15325 if (class_decl* cl = is_class_type(scope))
15326 if (!cl->is_struct())
15327 access = private_access;
15328
15329 die_access_specifier(die, access);
15330 set_member_access_specifier(member_type_declaration, access);
15331 }
15332 }
15333
15334 /// This function tests if a given function which might be intented to
15335 /// be added to a class scope (to become a member function) should be
15336 /// dropped on the floor instead and not be added to the class.
15337 ///
15338 /// This is a subroutine of build_ir_node_from_die.
15339 ///
15340 /// @param fn the function to consider.
15341 ///
15342 /// @param scope the scope the function is intended to be added
15343 /// to. This might be of class type or not.
15344 ///
15345 /// @param fn_die the DWARF die of @p fn.
15346 ///
15347 /// @return true iff @p fn should be dropped on the floor.
15348 static bool
potential_member_fn_should_be_dropped(const function_decl_sptr & fn,Dwarf_Die * fn_die)15349 potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15350 Dwarf_Die *fn_die)
15351 {
15352 if (!fn || fn->get_scope())
15353 return false;
15354
15355 if (// A function that is not virtual ...
15356 !die_is_virtual(fn_die)
15357 // ... has a linkage name ...
15358 && !fn->get_linkage_name().empty()
15359 // .. and yet has no ELF symbol associated ...
15360 && !fn->get_symbol())
15361 // Should not be added to its class scope.
15362 //
15363 // Why would it? It's not part of the ABI anyway, as it doesn't
15364 // have any ELF symbol associated and is not a virtual member
15365 // function. It just constitutes bloat in the IR and might even
15366 // induce spurious change reports down the road.
15367 return true;
15368
15369 return false;
15370 }
15371
15372 /// Build an IR node from a given DIE and add the node to the current
15373 /// IR being build and held in the DWARF reader. Doing that is called
15374 /// "emitting an IR node for the DIE".
15375 ///
15376 /// @param rdr the DWARF reader.
15377 ///
15378 /// @param die the DIE to consider.
15379 ///
15380 /// @param scope the scope under which the resulting IR node has to be
15381 /// added.
15382 ///
15383 /// @param called_from_public_decl set to yes if this function is
15384 /// called from the functions used to build a public decl (functions
15385 /// and variables). In that case, this function accepts building IR
15386 /// nodes representing types. Otherwise, this function only creates
15387 /// IR nodes representing public decls (functions and variables).
15388 /// This is done to avoid emitting IR nodes for types that are not
15389 /// referenced by public functions or variables.
15390 ///
15391 /// @param where_offset the offset of the DIE where we are "logically"
15392 /// positionned at, in the DIE tree. This is useful when @p die is
15393 /// e.g, DW_TAG_partial_unit that can be included in several places in
15394 /// the DIE tree.
15395 ///
15396 /// @param is_required_decl_spec if true, it means the ir node to
15397 /// build is for a decl that is a specification for another decl that
15398 /// is concrete. If you don't know what this is, set it to false.
15399 ///
15400 /// @param is_declaration_only is true if the DIE denoted by @p die is
15401 /// a declaration-only DIE.
15402 ///
15403 /// @return the resulting IR node.
15404 static type_or_decl_base_sptr
build_ir_node_from_die(reader & rdr,Dwarf_Die * die,scope_decl * scope,bool called_from_public_decl,size_t where_offset,bool is_declaration_only,bool is_required_decl_spec)15405 build_ir_node_from_die(reader& rdr,
15406 Dwarf_Die* die,
15407 scope_decl* scope,
15408 bool called_from_public_decl,
15409 size_t where_offset,
15410 bool is_declaration_only,
15411 bool is_required_decl_spec)
15412 {
15413 type_or_decl_base_sptr result;
15414
15415 if (!die || !scope)
15416 return result;
15417
15418 int tag = dwarf_tag(die);
15419
15420 if (!called_from_public_decl)
15421 {
15422 if (rdr.load_all_types() && die_is_type(die))
15423 /* We were instructed to load debug info for all types,
15424 included those that are not reachable from a public
15425 declaration. So load the debug info for this type. */;
15426 else if (tag != DW_TAG_subprogram
15427 && tag != DW_TAG_variable
15428 && tag != DW_TAG_member
15429 && tag != DW_TAG_namespace)
15430 return result;
15431 }
15432
15433 const die_source source_of_die = rdr.get_die_source(die);
15434
15435 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15436 source_of_die)))
15437 {
15438 if (rdr.load_all_types())
15439 if (called_from_public_decl)
15440 if (type_base_sptr t = is_type(result))
15441 if (corpus *abi_corpus = scope->get_corpus())
15442 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15443
15444 return result;
15445 }
15446
15447 // This is *the* bit of code that ensures we have the right notion
15448 // of "declared" at any point in a DIE chain formed from
15449 // DW_AT_abstract_origin and DW_AT_specification links. There should
15450 // be no other callers of die_is_declaration_only.
15451 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15452
15453 switch (tag)
15454 {
15455 // Type DIEs we support.
15456 case DW_TAG_base_type:
15457 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15458 {
15459 result =
15460 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15461 canonicalize(t);
15462 }
15463 break;
15464
15465 case DW_TAG_typedef:
15466 {
15467 typedef_decl_sptr t = build_typedef_type(rdr, die,
15468 called_from_public_decl,
15469 where_offset);
15470
15471 result = add_decl_to_scope(t, scope);
15472 if (result)
15473 {
15474 maybe_set_member_type_access_specifier(is_decl(result), die);
15475 maybe_canonicalize_type(t, rdr);
15476 }
15477 }
15478 break;
15479
15480 case DW_TAG_pointer_type:
15481 {
15482 pointer_type_def_sptr p =
15483 build_pointer_type_def(rdr, die,
15484 called_from_public_decl,
15485 where_offset);
15486 if (p)
15487 {
15488 result =
15489 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15490 ABG_ASSERT(result->get_translation_unit());
15491 maybe_canonicalize_type(p, rdr);
15492 }
15493 }
15494 break;
15495
15496 case DW_TAG_reference_type:
15497 case DW_TAG_rvalue_reference_type:
15498 {
15499 reference_type_def_sptr r =
15500 build_reference_type(rdr, die,
15501 called_from_public_decl,
15502 where_offset);
15503 if (r)
15504 {
15505 result =
15506 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
15507
15508 rdr.associate_die_to_type(die, r, where_offset);
15509 maybe_canonicalize_type(r, rdr);
15510 }
15511 }
15512 break;
15513
15514 case DW_TAG_const_type:
15515 case DW_TAG_volatile_type:
15516 case DW_TAG_restrict_type:
15517 {
15518 type_base_sptr q =
15519 build_qualified_type(rdr, die,
15520 called_from_public_decl,
15521 where_offset);
15522 if (q)
15523 {
15524 // Strip some potentially redundant type qualifiers from
15525 // the qualified type we just built.
15526 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
15527 rdr);
15528 if (!d)
15529 d = get_type_declaration(q);
15530 ABG_ASSERT(d);
15531 type_base_sptr ty = is_type(d);
15532 // Associate the die to type ty again because 'ty'might be
15533 // different from 'q', because 'ty' is 'q' possibly
15534 // stripped from some redundant type qualifier.
15535 rdr.associate_die_to_type(die, ty, where_offset);
15536 result =
15537 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
15538 maybe_canonicalize_type(is_type(result), rdr);
15539 }
15540 }
15541 break;
15542
15543 case DW_TAG_enumeration_type:
15544 {
15545 bool type_is_private = false;
15546 bool type_suppressed =
15547 type_is_suppressed(rdr, scope, die, type_is_private);
15548 if (type_suppressed && type_is_private)
15549 {
15550 // The type is suppressed because it's private. If other
15551 // non-suppressed and declaration-only instances of this
15552 // type exist in the current corpus, then it means those
15553 // non-suppressed instances are opaque versions of the
15554 // suppressed private type. Lets return one of these opaque
15555 // types then.
15556 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15557 maybe_canonicalize_type(is_type(result), rdr);
15558 }
15559 else if (!type_suppressed)
15560 {
15561 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
15562 where_offset,
15563 is_declaration_only);
15564 result = add_decl_to_scope(e, scope);
15565 if (result)
15566 {
15567 maybe_set_member_type_access_specifier(is_decl(result), die);
15568 maybe_canonicalize_type(is_type(result), rdr);
15569 }
15570 }
15571 }
15572 break;
15573
15574 case DW_TAG_class_type:
15575 case DW_TAG_structure_type:
15576 {
15577 bool type_is_private = false;
15578 bool type_suppressed=
15579 type_is_suppressed(rdr, scope, die, type_is_private);
15580
15581 if (type_suppressed && type_is_private)
15582 {
15583 // The type is suppressed because it's private. If other
15584 // non-suppressed and declaration-only instances of this
15585 // type exist in the current corpus, then it means those
15586 // non-suppressed instances are opaque versions of the
15587 // suppressed private type. Lets return one of these opaque
15588 // types then.
15589 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15590 maybe_canonicalize_type(is_type(result), rdr);
15591 }
15592 else if (!type_suppressed)
15593 {
15594 Dwarf_Die spec_die;
15595 scope_decl_sptr scop;
15596 class_decl_sptr klass;
15597 if (die_die_attribute(die, DW_AT_specification, spec_die))
15598 {
15599 scope_decl_sptr skope =
15600 get_scope_for_die(rdr, &spec_die,
15601 called_from_public_decl,
15602 where_offset);
15603 ABG_ASSERT(skope);
15604 decl_base_sptr cl =
15605 is_decl(build_ir_node_from_die(rdr, &spec_die,
15606 skope.get(),
15607 called_from_public_decl,
15608 where_offset,
15609 is_declaration_only,
15610 /*is_required_decl_spec=*/false));
15611 ABG_ASSERT(cl);
15612 klass = dynamic_pointer_cast<class_decl>(cl);
15613 ABG_ASSERT(klass);
15614
15615 klass =
15616 add_or_update_class_type(rdr, die,
15617 skope.get(),
15618 tag == DW_TAG_structure_type,
15619 klass,
15620 called_from_public_decl,
15621 where_offset,
15622 is_declaration_only);
15623 }
15624 else
15625 klass =
15626 add_or_update_class_type(rdr, die, scope,
15627 tag == DW_TAG_structure_type,
15628 class_decl_sptr(),
15629 called_from_public_decl,
15630 where_offset,
15631 is_declaration_only);
15632 result = klass;
15633 if (klass)
15634 {
15635 maybe_set_member_type_access_specifier(klass, die);
15636 maybe_canonicalize_type(klass, rdr);
15637 }
15638 }
15639 }
15640 break;
15641 case DW_TAG_union_type:
15642 if (!type_is_suppressed(rdr, scope, die))
15643 {
15644 union_decl_sptr union_type =
15645 add_or_update_union_type(rdr, die, scope,
15646 union_decl_sptr(),
15647 called_from_public_decl,
15648 where_offset,
15649 is_declaration_only);
15650 if (union_type)
15651 {
15652 maybe_set_member_type_access_specifier(union_type, die);
15653 maybe_canonicalize_type(union_type, rdr);
15654 }
15655 result = union_type;
15656 }
15657 break;
15658 case DW_TAG_string_type:
15659 break;
15660 case DW_TAG_subroutine_type:
15661 {
15662 function_type_sptr f = build_function_type(rdr, die,
15663 class_decl_sptr(),
15664 where_offset);
15665 if (f)
15666 {
15667 result = f;
15668 result->set_is_artificial(false);
15669 maybe_canonicalize_type(f, rdr);
15670 }
15671 }
15672 break;
15673 case DW_TAG_array_type:
15674 {
15675 array_type_def_sptr a = build_array_type(rdr,
15676 die,
15677 called_from_public_decl,
15678 where_offset);
15679 if (a)
15680 {
15681 result =
15682 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
15683 rdr.associate_die_to_type(die, a, where_offset);
15684 maybe_canonicalize_type(a, rdr);
15685 }
15686 break;
15687 }
15688 case DW_TAG_subrange_type:
15689 {
15690 // If we got here, this means the subrange type is a "free
15691 // form" defined in the global namespace of the current
15692 // translation unit, like what is found in Ada.
15693 array_type_def::subrange_sptr s =
15694 build_subrange_type(rdr, die, where_offset);
15695 if (s)
15696 {
15697 result =
15698 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
15699 rdr.associate_die_to_type(die, s, where_offset);
15700 maybe_canonicalize_type(s, rdr);
15701 }
15702 }
15703 break;
15704 case DW_TAG_packed_type:
15705 break;
15706 case DW_TAG_set_type:
15707 break;
15708 case DW_TAG_file_type:
15709 break;
15710 case DW_TAG_ptr_to_member_type:
15711 break;
15712 case DW_TAG_thrown_type:
15713 break;
15714 case DW_TAG_interface_type:
15715 break;
15716 case DW_TAG_unspecified_type:
15717 break;
15718 case DW_TAG_shared_type:
15719 break;
15720
15721 case DW_TAG_compile_unit:
15722 // We shouldn't reach this point b/c this should be handled by
15723 // build_translation_unit.
15724 ABG_ASSERT_NOT_REACHED;
15725
15726 case DW_TAG_namespace:
15727 case DW_TAG_module:
15728 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
15729 break;
15730
15731 case DW_TAG_variable:
15732 case DW_TAG_member:
15733 {
15734 Dwarf_Die spec_die;
15735 bool var_is_cloned = false;
15736
15737 if (tag == DW_TAG_member)
15738 ABG_ASSERT(!is_c_language(rdr.cur_transl_unit()->get_language()));
15739
15740 if (die_die_attribute(die, DW_AT_specification, spec_die, false)
15741 || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
15742 spec_die, false)))
15743 {
15744 scope_decl_sptr spec_scope =
15745 get_scope_for_die(rdr, &spec_die,
15746 /*called_from_public_decl=*/
15747 die_is_effectively_public_decl(rdr, die),
15748 where_offset);
15749 if (spec_scope)
15750 {
15751 decl_base_sptr d =
15752 is_decl(build_ir_node_from_die(rdr, &spec_die,
15753 spec_scope.get(),
15754 called_from_public_decl,
15755 where_offset,
15756 is_declaration_only,
15757 /*is_required_decl_spec=*/true));
15758 if (d)
15759 {
15760 var_decl_sptr m =
15761 dynamic_pointer_cast<var_decl>(d);
15762 if (var_is_cloned)
15763 m = m->clone();
15764 m = build_var_decl(rdr, die, where_offset, m);
15765 if (is_data_member(m))
15766 {
15767 set_member_is_static(m, true);
15768 rdr.associate_die_to_decl(die, m, where_offset,
15769 /*associate_by_repr=*/false);
15770 }
15771 else
15772 {
15773 ABG_ASSERT(has_scope(m));
15774 rdr.var_decls_to_re_add_to_tree().push_back(m);
15775 }
15776 ABG_ASSERT(m->get_scope());
15777 rdr.maybe_add_var_to_exported_decls(m.get());
15778 result = m;
15779 }
15780 }
15781 }
15782 else if (var_decl_sptr v =
15783 build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
15784 where_offset,
15785 /*result=*/var_decl_sptr(),
15786 is_required_decl_spec))
15787 {
15788 result = add_decl_to_scope(v, scope);
15789 ABG_ASSERT(is_decl(result)->get_scope());
15790 v = dynamic_pointer_cast<var_decl>(result);
15791 ABG_ASSERT(v);
15792 ABG_ASSERT(v->get_scope());
15793 rdr.var_decls_to_re_add_to_tree().push_back(v);
15794 rdr.maybe_add_var_to_exported_decls(v.get());
15795 }
15796 }
15797 break;
15798
15799 case DW_TAG_subprogram:
15800 {
15801 Dwarf_Die spec_die;
15802 Dwarf_Die abstract_origin_die;
15803 Dwarf_Die *interface_die = 0, *origin_die = 0;
15804 scope_decl_sptr interface_scope;
15805 if (die_is_artificial(die))
15806 break;
15807
15808 function_decl_sptr fn;
15809 bool has_spec = die_die_attribute(die, DW_AT_specification,
15810 spec_die, true);
15811 bool has_abstract_origin =
15812 die_die_attribute(die, DW_AT_abstract_origin,
15813 abstract_origin_die, true);
15814 if (has_spec || has_abstract_origin)
15815 {
15816 interface_die =
15817 has_spec
15818 ? &spec_die
15819 : &abstract_origin_die;
15820 origin_die =
15821 has_abstract_origin
15822 ? &abstract_origin_die
15823 : &spec_die;
15824
15825 string linkage_name = die_linkage_name(die);
15826 string spec_linkage_name = die_linkage_name(interface_die);
15827
15828 interface_scope = get_scope_for_die(rdr, interface_die,
15829 called_from_public_decl,
15830 where_offset);
15831 if (interface_scope)
15832 {
15833 decl_base_sptr d;
15834 class_decl_sptr c = is_class_type(interface_scope);
15835 if (c && !linkage_name.empty())
15836 d = c->find_member_function_sptr(linkage_name);
15837
15838 if (!d)
15839 d = is_decl(build_ir_node_from_die(rdr,
15840 origin_die,
15841 interface_scope.get(),
15842 called_from_public_decl,
15843 where_offset,
15844 is_declaration_only,
15845 /*is_required_decl_spec=*/true));
15846 if (d)
15847 {
15848 fn = dynamic_pointer_cast<function_decl>(d);
15849 if (has_abstract_origin
15850 && (linkage_name != spec_linkage_name)
15851 && !c->find_member_function_sptr(linkage_name))
15852 // The current DIE has 'd' as abstract orign,
15853 // and has a linkage name that is different
15854 // from from the linkage name of 'd'. That
15855 // means, the current DIE represents a clone
15856 // of 'd'.
15857 fn = fn->clone();
15858 }
15859 }
15860 }
15861 rdr.scope_stack().push(scope);
15862
15863 scope_decl* logical_scope =
15864 interface_scope
15865 ? interface_scope.get()
15866 : scope;
15867
15868 result = build_or_get_fn_decl_if_not_suppressed(rdr, logical_scope,
15869 die, where_offset,
15870 is_declaration_only,
15871 fn);
15872
15873 if (result && !fn)
15874 {
15875 if (potential_member_fn_should_be_dropped(is_function_decl(result),
15876 die)
15877 && !is_required_decl_spec)
15878 {
15879 result.reset();
15880 break;
15881 }
15882 result = add_decl_to_scope(is_decl(result), logical_scope);
15883 }
15884
15885 fn = is_function_decl(result);
15886 if (fn && is_member_function(fn))
15887 {
15888 class_decl_sptr klass(static_cast<class_decl*>(logical_scope),
15889 sptr_utils::noop_deleter());
15890 ABG_ASSERT(klass);
15891 finish_member_function_reading(die, fn, klass, rdr);
15892 }
15893
15894 if (fn)
15895 {
15896 rdr.maybe_add_fn_to_exported_decls(fn.get());
15897 rdr.associate_die_to_decl(die, fn, where_offset,
15898 /*associate_by_repr=*/false);
15899 maybe_canonicalize_type(fn->get_type(), rdr);
15900 }
15901
15902 rdr.scope_stack().pop();
15903 }
15904 break;
15905
15906 case DW_TAG_formal_parameter:
15907 // We should not read this case as it should have been dealt
15908 // with by build_function_decl above.
15909 ABG_ASSERT_NOT_REACHED;
15910
15911 case DW_TAG_constant:
15912 break;
15913 case DW_TAG_enumerator:
15914 break;
15915
15916 case DW_TAG_partial_unit:
15917 case DW_TAG_imported_unit:
15918 // For now, the DIEs under these are read lazily when they are
15919 // referenced by a public decl DIE that is under a
15920 // DW_TAG_compile_unit, so we shouldn't get here.
15921 ABG_ASSERT_NOT_REACHED;
15922
15923 // Other declaration we don't really intend to support yet.
15924 case DW_TAG_dwarf_procedure:
15925 case DW_TAG_imported_declaration:
15926 case DW_TAG_entry_point:
15927 case DW_TAG_label:
15928 case DW_TAG_lexical_block:
15929 case DW_TAG_unspecified_parameters:
15930 case DW_TAG_variant:
15931 case DW_TAG_common_block:
15932 case DW_TAG_common_inclusion:
15933 case DW_TAG_inheritance:
15934 case DW_TAG_inlined_subroutine:
15935 case DW_TAG_with_stmt:
15936 case DW_TAG_access_declaration:
15937 case DW_TAG_catch_block:
15938 case DW_TAG_friend:
15939 case DW_TAG_namelist:
15940 case DW_TAG_namelist_item:
15941 case DW_TAG_template_type_parameter:
15942 case DW_TAG_template_value_parameter:
15943 case DW_TAG_try_block:
15944 case DW_TAG_variant_part:
15945 case DW_TAG_imported_module:
15946 case DW_TAG_condition:
15947 case DW_TAG_type_unit:
15948 case DW_TAG_template_alias:
15949 case DW_TAG_lo_user:
15950 case DW_TAG_MIPS_loop:
15951 case DW_TAG_format_label:
15952 case DW_TAG_function_template:
15953 case DW_TAG_class_template:
15954 case DW_TAG_GNU_BINCL:
15955 case DW_TAG_GNU_EINCL:
15956 case DW_TAG_GNU_template_template_param:
15957 case DW_TAG_GNU_template_parameter_pack:
15958 case DW_TAG_GNU_formal_parameter_pack:
15959 case DW_TAG_GNU_call_site:
15960 case DW_TAG_GNU_call_site_parameter:
15961 case DW_TAG_hi_user:
15962 default:
15963 break;
15964 }
15965
15966 if (result && tag != DW_TAG_subroutine_type)
15967 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
15968 /*associate_by_repr=*/false);
15969
15970 if (result)
15971 if (rdr.load_all_types())
15972 if (called_from_public_decl)
15973 if (type_base_sptr t = is_type(result))
15974 if (corpus *abi_corpus = scope->get_corpus())
15975 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15976
15977 return result;
15978 }
15979
15980 /// Build the IR node for a void type.
15981 ///
15982 /// @param rdr the DWARF reader to use.
15983 ///
15984 /// @return the void type node.
15985 static decl_base_sptr
build_ir_node_for_void_type(reader & rdr)15986 build_ir_node_for_void_type(reader& rdr)
15987 {
15988 const environment& env = rdr.env();
15989
15990 type_base_sptr t = env.get_void_type();
15991 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
15992 decl_base_sptr type_declaration = get_type_declaration(t);
15993 canonicalize(t);
15994 return type_declaration;
15995 }
15996
15997 /// Build the IR node for a "pointer to void type".
15998 ///
15999 /// That IR node is shared across the ABI corpus.
16000 ///
16001 /// Note that this function just gets that IR node from the
16002 /// environment and, if it's not added to any scope yet, adds it to
16003 /// the global scope associated to the current translation unit.
16004 ///
16005 /// @param rdr the DWARF reader to consider.
16006 ///
16007 /// @return the IR node.
16008 static type_or_decl_base_sptr
build_ir_node_for_void_pointer_type(reader & rdr)16009 build_ir_node_for_void_pointer_type(reader& rdr)
16010 {
16011 const environment& env = rdr.env();
16012
16013 type_base_sptr t = env.get_void_pointer_type();
16014 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16015 decl_base_sptr type_declaration = get_type_declaration(t);
16016 canonicalize(t);
16017 return type_declaration;
16018 }
16019
16020 /// Build the IR node for a variadic parameter type.
16021 ///
16022 /// @param rdr the DWARF reader to use.
16023 ///
16024 /// @return the variadic parameter type.
16025 static decl_base_sptr
build_ir_node_for_variadic_parameter_type(reader & rdr)16026 build_ir_node_for_variadic_parameter_type(reader &rdr)
16027 {
16028
16029 const environment& env = rdr.env();
16030
16031 type_base_sptr t = env.get_variadic_parameter_type();
16032 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16033 decl_base_sptr type_declaration = get_type_declaration(t);
16034 canonicalize(t);
16035 return type_declaration;
16036 }
16037
16038 /// Build an IR node from a given DIE and add the node to the current
16039 /// IR being build and held in the DWARF reader. Doing that is called
16040 /// "emitting an IR node for the DIE".
16041 ///
16042 /// @param rdr the DWARF reader.
16043 ///
16044 /// @param die the DIE to consider.
16045 ///
16046 /// @param called_from_public_decl set to yes if this function is
16047 /// called from the functions used to build a public decl (functions
16048 /// and variables). In that case, this function accepts building IR
16049 /// nodes representing types. Otherwise, this function only creates
16050 /// IR nodes representing public decls (functions and variables).
16051 /// This is done to avoid emitting IR nodes for types that are not
16052 /// referenced by public functions or variables.
16053 ///
16054 /// @param where_offset the offset of the DIE where we are "logically"
16055 /// positionned at, in the DIE tree. This is useful when @p die is
16056 /// e.g, DW_TAG_partial_unit that can be included in several places in
16057 /// the DIE tree.
16058 ///
16059 /// @return the resulting IR node.
16060 static type_or_decl_base_sptr
build_ir_node_from_die(reader & rdr,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)16061 build_ir_node_from_die(reader& rdr,
16062 Dwarf_Die* die,
16063 bool called_from_public_decl,
16064 size_t where_offset)
16065 {
16066 if (!die)
16067 return decl_base_sptr();
16068
16069 if (is_c_language(rdr.cur_transl_unit()->get_language()))
16070 {
16071 const scope_decl_sptr& scop = rdr.global_scope();
16072 return build_ir_node_from_die(rdr, die, scop.get(),
16073 called_from_public_decl,
16074 where_offset,
16075 true);
16076 }
16077
16078 // Normaly, a decl that is meant to be external has a DW_AT_external
16079 // set. But then some compilers fail to always emit that flag. For
16080 // instance, for static data members, some compilers won't emit the
16081 // DW_AT_external. In that case, we assume that if the variable is
16082 // at global or named namespace scope, then we can assume it's
16083 // external. If the variable doesn't have any ELF symbol associated
16084 // to it, it'll be dropped on the floor anyway. Those variable
16085 // decls are considered as being "effectively public".
16086 bool consider_as_called_from_public_decl =
16087 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
16088 scope_decl_sptr scope = get_scope_for_die(rdr, die,
16089 consider_as_called_from_public_decl,
16090 where_offset);
16091 return build_ir_node_from_die(rdr, die, scope.get(),
16092 called_from_public_decl,
16093 where_offset,
16094 true);
16095 }
16096
16097 /// Create a dwarf::reader.
16098 ///
16099 /// @param elf_path the path to the elf file the reader is to be used
16100 /// for.
16101 ///
16102 /// @param debug_info_root_paths a vector to the paths to the
16103 /// directories under which the debug info is to be found for @p
16104 /// elf_path. Pass an empty vector if the debug info is not in a
16105 /// split file.
16106 ///
16107 /// @param environment the environment used by the current context.
16108 /// This environment contains resources needed by the DWARF reader and by
16109 /// the types and declarations that are to be created later. Note
16110 /// that ABI artifacts that are to be compared all need to be created
16111 /// within the same environment.
16112 ///
16113 /// Please also note that the life time of this environment object
16114 /// must be greater than the life time of the resulting @ref
16115 /// reader the context uses resources that are allocated in the
16116 /// environment.
16117 ///
16118 /// @param load_all_types if set to false only the types that are
16119 /// reachable from publicly exported declarations (of functions and
16120 /// variables) are read. If set to true then all types found in the
16121 /// debug information are loaded.
16122 ///
16123 /// @param linux_kernel_mode if set to true, then consider the special
16124 /// linux kernel symbol tables when determining if a symbol is
16125 /// exported or not.
16126 ///
16127 /// @return a smart pointer to the resulting dwarf::reader.
16128 elf_based_reader_sptr
create_reader(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,bool linux_kernel_mode)16129 create_reader(const std::string& elf_path,
16130 const vector<char**>& debug_info_root_paths,
16131 environment& environment,
16132 bool load_all_types,
16133 bool linux_kernel_mode)
16134 {
16135
16136 reader_sptr r = reader::create(elf_path,
16137 debug_info_root_paths,
16138 environment,
16139 load_all_types,
16140 linux_kernel_mode);
16141 return static_pointer_cast<elf_based_reader>(r);
16142 }
16143
16144 /// Re-initialize a reader so that it can re-used to read
16145 /// another binary.
16146 ///
16147 /// @param rdr the context to re-initialize.
16148 ///
16149 /// @param elf_path the path to the elf file the context is to be used
16150 /// for.
16151 ///
16152 /// @param debug_info_root_path a pointer to the path to the root
16153 /// directory under which the debug info is to be found for @p
16154 /// elf_path. Leave this to NULL if the debug info is not in a split
16155 /// file.
16156 ///
16157 /// @param environment the environment used by the current context.
16158 /// This environment contains resources needed by the DWARF reader and by
16159 /// the types and declarations that are to be created later. Note
16160 /// that ABI artifacts that are to be compared all need to be created
16161 /// within the same environment.
16162 ///
16163 /// Please also note that the life time of this environment object
16164 /// must be greater than the life time of the resulting @ref
16165 /// reader the context uses resources that are allocated in the
16166 /// environment.
16167 ///
16168 /// @param load_all_types if set to false only the types that are
16169 /// reachable from publicly exported declarations (of functions and
16170 /// variables) are read. If set to true then all types found in the
16171 /// debug information are loaded.
16172 ///
16173 /// @param linux_kernel_mode if set to true, then consider the special
16174 /// linux kernel symbol tables when determining if a symbol is
16175 /// exported or not.
16176 ///
16177 /// @return a smart pointer to the resulting dwarf::reader.
16178 void
reset_reader(elf_based_reader & rdr,const std::string & elf_path,const vector<char ** > & debug_info_root_path,bool read_all_types,bool linux_kernel_mode)16179 reset_reader(elf_based_reader& rdr,
16180 const std::string& elf_path,
16181 const vector<char**>&debug_info_root_path,
16182 bool read_all_types,
16183 bool linux_kernel_mode)
16184 {
16185 reader& r = dynamic_cast<reader&>(rdr);
16186 r.initialize(elf_path, debug_info_root_path,
16187 read_all_types, linux_kernel_mode);
16188 }
16189
16190 /// Read all @ref abigail::translation_unit possible from the debug info
16191 /// accessible from an elf file, stuff them into a libabigail ABI
16192 /// Corpus and return it.
16193 ///
16194 /// @param elf_path the path to the elf file.
16195 ///
16196 /// @param debug_info_root_paths a vector of pointers to root paths
16197 /// under which to look for the debug info of the elf files that are
16198 /// later handled by the Dwfl. This for cases where the debug info is
16199 /// split into a different file from the binary we want to inspect.
16200 /// On Red Hat compatible systems, this root path is usually
16201 /// /usr/lib/debug by default. If this argument is set to NULL, then
16202 /// "./debug" and /usr/lib/debug will be searched for sub-directories
16203 /// containing the debug info file.
16204 ///
16205 /// @param environment the environment used by the current context.
16206 /// This environment contains resources needed by the DWARF reader and by
16207 /// the types and declarations that are to be created later. Note
16208 /// that ABI artifacts that are to be compared all need to be created
16209 /// within the same environment. Also, the lifetime of the
16210 /// environment must be greater than the lifetime of the resulting
16211 /// corpus because the corpus uses resources that are allocated in the
16212 /// environment.
16213 ///
16214 /// @param load_all_types if set to false only the types that are
16215 /// reachable from publicly exported declarations (of functions and
16216 /// variables) are read. If set to true then all types found in the
16217 /// debug information are loaded.
16218 ///
16219 /// @param resulting_corp a pointer to the resulting abigail::corpus.
16220 ///
16221 /// @return the resulting status.
16222 corpus_sptr
read_corpus_from_elf(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,environment & environment,bool load_all_types,fe_iface::status & status)16223 read_corpus_from_elf(const std::string& elf_path,
16224 const vector<char**>& debug_info_root_paths,
16225 environment& environment,
16226 bool load_all_types,
16227 fe_iface::status& status)
16228 {
16229 elf_based_reader_sptr rdr =
16230 dwarf::reader::create(elf_path, debug_info_root_paths,
16231 environment, load_all_types,
16232 /*linux_kernel_mode=*/false);
16233
16234 return rdr->read_corpus(status);
16235 }
16236
16237 /// Look into the symbol tables of a given elf file and see if we find
16238 /// a given symbol.
16239 ///
16240 /// @param env the environment we are operating from.
16241 ///
16242 /// @param elf_path the path to the elf file to consider.
16243 ///
16244 /// @param symbol_name the name of the symbol to look for.
16245 ///
16246 /// @param demangle if true, try to demangle the symbol name found in
16247 /// the symbol table.
16248 ///
16249 /// @param syms the vector of symbols found with the name @p symbol_name.
16250 ///
16251 /// @return true iff the symbol was found among the publicly exported
16252 /// symbols of the ELF file.
16253 bool
lookup_symbol_from_elf(const environment & env,const string & elf_path,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms)16254 lookup_symbol_from_elf(const environment& env,
16255 const string& elf_path,
16256 const string& symbol_name,
16257 bool demangle,
16258 vector<elf_symbol_sptr>& syms)
16259
16260 {
16261 if (elf_version(EV_CURRENT) == EV_NONE)
16262 return false;
16263
16264 int fd = open(elf_path.c_str(), O_RDONLY);
16265 if (fd < 0)
16266 return false;
16267
16268 struct stat s;
16269 if (fstat(fd, &s))
16270 return false;
16271
16272 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16273 if (elf == 0)
16274 return false;
16275
16276 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
16277 demangle, syms);
16278 elf_end(elf);
16279 close(fd);
16280
16281 return value;
16282 }
16283
16284 /// Look into the symbol tables of an elf file to see if a public
16285 /// function of a given name is found.
16286 ///
16287 /// @param env the environment we are operating from.
16288 ///
16289 /// @param elf_path the path to the elf file to consider.
16290 ///
16291 /// @param symbol_name the name of the function to look for.
16292 ///
16293 /// @param syms the vector of public function symbols found with the
16294 /// name @p symname.
16295 ///
16296 /// @return true iff a function with symbol name @p symbol_name is
16297 /// found.
16298 bool
lookup_public_function_symbol_from_elf(environment & env,const string & path,const string & symname,vector<elf_symbol_sptr> & syms)16299 lookup_public_function_symbol_from_elf(environment& env,
16300 const string& path,
16301 const string& symname,
16302 vector<elf_symbol_sptr>& syms)
16303 {
16304 if (elf_version(EV_CURRENT) == EV_NONE)
16305 return false;
16306
16307 int fd = open(path.c_str(), O_RDONLY);
16308 if (fd < 0)
16309 return false;
16310
16311 struct stat s;
16312 if (fstat(fd, &s))
16313 return false;
16314
16315 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16316 if (elf == 0)
16317 return false;
16318
16319 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16320 elf_end(elf);
16321 close(fd);
16322
16323 return value;
16324 }
16325
16326 }// end namespace dwarf
16327
16328 }// end namespace abigail
16329