32a585917fce4675cbe061b65bf80df807e93f2f
[platform/upstream/libabigail.git] / src / abg-dwarf-reader.cc
1 // -*- Mode: C++ -*-
2 //
3 // Copyright (C) 2013-2019 Red Hat, Inc.
4 //
5 // This file is part of the GNU Application Binary Interface Generic
6 // Analysis and Instrumentation Library (libabigail).  This library is
7 // free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option) any
10 // later version.
11
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Lesser Public License for more details.
16
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this program; see the file COPYING-LGPLV3.  If
19 // not, see <http://www.gnu.org/licenses/>.
20 //
21 // Author: Dodji Seketeli
22
23 /// @file
24 ///
25 /// This file contains the definitions of the entry points to
26 /// de-serialize an instance of @ref abigail::corpus from a file in
27 /// elf format, containing dwarf information.
28
29 #include "config.h"
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <assert.h>
36 #include <limits.h>
37 #include <cstring>
38 #include <cmath>
39 #include <elfutils/libdwfl.h>
40 #include <dwarf.h>
41 #include <algorithm>
42 #include <stack>
43 #include <deque>
44 #include <list>
45 #include <ostream>
46 #include <sstream>
47
48 #include "abg-cxx-compat.h"
49 #include "abg-ir-priv.h"
50 #include "abg-suppression-priv.h"
51 #include "abg-corpus-priv.h"
52
53 #include "abg-internal.h"
54 // <headers defining libabigail's API go under here>
55 ABG_BEGIN_EXPORT_DECLARATIONS
56
57 #include "abg-dwarf-reader.h"
58 #include "abg-sptr-utils.h"
59 #include "abg-tools-utils.h"
60
61 ABG_END_EXPORT_DECLARATIONS
62 // </headers defining libabigail's API>
63
64 #ifndef UINT64_MAX
65 #define UINT64_MAX 0xffffffffffffffff
66 #endif
67
68 using std::string;
69
70 namespace abigail
71 {
72
73 using std::cerr;
74
75 /// The namespace for the DWARF reader.
76 namespace dwarf_reader
77 {
78
79 using abg_compat::dynamic_pointer_cast;
80 using abg_compat::static_pointer_cast;
81 using abg_compat::unordered_map;
82 using abg_compat::unordered_set;
83 using std::stack;
84 using std::deque;
85 using std::list;
86
87 /// Where a DIE comes from. For instance, a DIE can come from the main
88 /// debug info section, the alternate debug info section or from the
89 /// type unit section.
90 enum die_source
91 {
92   NO_DEBUG_INFO_DIE_SOURCE,
93   PRIMARY_DEBUG_INFO_DIE_SOURCE,
94   ALT_DEBUG_INFO_DIE_SOURCE,
95   TYPE_UNIT_DIE_SOURCE,
96   NUMBER_OF_DIE_SOURCES,        // This one must always be the latest
97                                 // enumerator
98 };
99
100 /// Prefix increment operator for @ref die_source.
101 ///
102 /// @param source the die_source to increment.
103 /// @return the incremented source.
104 static die_source&
105 operator++(die_source& source)
106 {
107   source = static_cast<die_source>(source + 1);
108   return source;
109 }
110
111 /// A functor used by @ref dwfl_sptr.
112 struct dwfl_deleter
113 {
114   void
115   operator()(Dwfl* dwfl)
116   {dwfl_end(dwfl);}
117 };//end struct dwfl_deleter
118
119 /// A convenience typedef for a shared pointer to a Dwfl.
120 typedef shared_ptr<Dwfl> dwfl_sptr;
121
122 /// A convenience typedef for a vector of Dwarf_Off.
123 typedef vector<Dwarf_Off> dwarf_offsets_type;
124
125 /// Convenience typedef for a map which key is the offset of a dwarf
126 /// die and which value is the corresponding artefact.
127 typedef unordered_map<Dwarf_Off, type_or_decl_base_sptr> die_artefact_map_type;
128
129 /// Convenience typedef for a map which key is the offset of a dwarf
130 /// die, (given by dwarf_dieoffset()) and which value is the
131 /// corresponding class_decl.
132 typedef unordered_map<Dwarf_Off, class_decl_sptr> die_class_map_type;
133
134 /// Convenience typedef for a map which key is the offset of a dwarf
135 /// die, (given by dwarf_dieoffset()) and which value is the
136 /// corresponding class_or_union_sptr.
137 typedef unordered_map<Dwarf_Off, class_or_union_sptr> die_class_or_union_map_type;
138
139 /// Convenience typedef for a map which key the offset of a dwarf die
140 /// and which value is the corresponding function_decl.
141 typedef unordered_map<Dwarf_Off, function_decl_sptr> die_function_decl_map_type;
142
143 /// Convenience typedef for a map which key is the offset of a dwarf
144 /// die and which value is the corresponding function_type.
145 typedef unordered_map<Dwarf_Off, function_type_sptr> die_function_type_map_type;
146
147 /// Convenience typedef for a map which key is the offset of a
148 /// DW_TAG_compile_unit and the value is the corresponding @ref
149 /// translation_unit_sptr.
150 typedef unordered_map<Dwarf_Off, translation_unit_sptr> die_tu_map_type;
151
152 /// Convenience typedef for a map which key is the offset of a DIE and
153 /// the value is the corresponding qualified name of the DIE.
154 typedef unordered_map<Dwarf_Off, interned_string> die_istring_map_type;
155
156 /// Convenience typedef for a map which is an interned_string and
157 /// which value is a vector of offsets.
158 typedef unordered_map<interned_string,
159                       dwarf_offsets_type,
160                       hash_interned_string>
161 istring_dwarf_offsets_map_type;
162
163 /// Convenience typedef for a map which key is an elf address and
164 /// which value is an elf_symbol_sptr.
165 typedef unordered_map<GElf_Addr, elf_symbol_sptr> addr_elf_symbol_sptr_map_type;
166
167 /// Convenience typedef for a set of ELF addresses.
168 typedef unordered_set<GElf_Addr> address_set_type;
169
170 typedef unordered_set<interned_string, hash_interned_string> istring_set_type;
171
172 /// Convenience typedef for a shared pointer to an @ref address_set_type.
173 typedef shared_ptr<address_set_type> address_set_sptr;
174
175 /// Convenience typedef for a shared pointer to an
176 /// addr_elf_symbol_sptr_map_type.
177 typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
178
179 /// Convenience typedef for a map that associates an @ref
180 /// interned_string to a @ref function_type_sptr.
181 typedef unordered_map<interned_string,
182                       function_type_sptr,
183                       hash_interned_string> istring_fn_type_map_type;
184
185 /// Convenience typedef for a stack containing the scopes up to the
186 /// current point in the abigail Internal Representation (aka IR) tree
187 /// that is being built.
188 typedef stack<scope_decl*> scope_stack_type;
189
190 /// Convenience typedef for a map which key is a dwarf offset.  The
191 /// value is also a dwarf offset.
192 typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
193
194 /// Convenience typedef for a map which key is a string and which
195 /// value is a vector of smart pointer to a class.
196 typedef unordered_map<string, classes_type> string_classes_map;
197
198 /// The abstraction of the place where a partial unit has been
199 /// imported.  This is what the DW_TAG_imported_unit DIE expresses.
200 ///
201 /// This type thus contains:
202 ///     - the offset to which the partial unit is imported
203 ///     - the offset of the imported partial unit.
204 ///     - the offset of the imported partial unit.
205 struct imported_unit_point
206 {
207   Dwarf_Off     offset_of_import;
208   // The boolean below is true iff the imported unit comes from the
209   // alternate debug info file.
210   die_source    imported_unit_die_source;
211   Dwarf_Off     imported_unit_die_off;
212   Dwarf_Off     imported_unit_cu_off;
213   Dwarf_Off     imported_unit_child_off;
214
215   /// Default constructor for @ref the type imported_unit_point.
216   imported_unit_point ()
217     : offset_of_import(),
218       imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
219       imported_unit_die_off(),
220       imported_unit_cu_off(),
221       imported_unit_child_off()
222   {}
223
224   /// Constructor of @ref the type imported_unit_point.
225   ///
226   /// @param import_off the offset of the point at which the unit has
227   /// been imported.
228   imported_unit_point (Dwarf_Off import_off)
229     : offset_of_import(import_off),
230       imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
231       imported_unit_die_off(),
232       imported_unit_cu_off(),
233       imported_unit_child_off()
234   {}
235
236   /// Constructor of @ref the type imported_unit_point.
237   ///
238   /// @param import_off the offset of the point at which the unit has
239   /// been imported.
240   ///
241   /// @param from where the imported DIE comes from.
242   ///
243   /// @param imported_die the die of the unit that has been imported.
244   imported_unit_point (Dwarf_Off        import_off,
245                        const Dwarf_Die& imported_die,
246                        die_source from)
247     : offset_of_import(import_off),
248       imported_unit_die_source(from),
249       imported_unit_die_off(dwarf_dieoffset
250                             (const_cast<Dwarf_Die*>(&imported_die))),
251       imported_unit_cu_off(),
252       imported_unit_child_off()
253   {
254     Dwarf_Die imported_unit_child;
255
256     dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
257                 &imported_unit_child);
258     imported_unit_child_off =
259       dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
260
261     Dwarf_Die cu_die_memory;
262     Dwarf_Die *cu_die;
263
264     cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
265                          &cu_die_memory, 0, 0);
266     imported_unit_cu_off = dwarf_dieoffset(cu_die);
267   }
268 }; // struct imported_unit_point
269
270 /// Convenience typedef for a vector of @ref imported_unit_point.
271 typedef vector<imported_unit_point> imported_unit_points_type;
272
273 /// Convenience typedef for a vector of @ref imported_unit_point.
274 typedef unordered_map<Dwarf_Off, imported_unit_points_type>
275 tu_die_imported_unit_points_map_type;
276
277 /// "Less than" operator for instances of @ref imported_unit_point
278 /// type.
279 ///
280 /// @param the left hand side operand of the "Less than" operator.
281 ///
282 /// @param the right hand side operand of the "Less than" operator.
283 ///
284 /// @return true iff @p l is less than @p r.
285 static bool
286 operator<(const imported_unit_point& l, const imported_unit_point& r)
287 {return l.offset_of_import < r.offset_of_import;}
288
289 static void
290 add_symbol_to_map(const elf_symbol_sptr& sym,
291                   string_elf_symbols_map_type& map);
292
293 static bool
294 find_symbol_table_section(Elf* elf_handle, Elf_Scn*& section);
295
296 static bool
297 get_symbol_versionning_sections(Elf*            elf_handle,
298                                 Elf_Scn*&       versym_section,
299                                 Elf_Scn*&       verdef_section,
300                                 Elf_Scn*&       verneed_section);
301
302 static bool
303 get_parent_die(const read_context&      ctxt,
304                const Dwarf_Die* die,
305                Dwarf_Die&               parent_die,
306                size_t                   where_offset);
307
308 static bool
309 get_scope_die(const read_context&       ctxt,
310               const Dwarf_Die*          die,
311               size_t                    where_offset,
312               Dwarf_Die&                scope_die);
313
314 static bool
315 die_is_anonymous(const Dwarf_Die* die);
316
317 static bool
318 die_is_type(const Dwarf_Die* die);
319
320 static bool
321 die_is_decl(const Dwarf_Die* die);
322
323 static bool
324 die_is_namespace(const Dwarf_Die* die);
325
326 static bool
327 die_is_unspecified(Dwarf_Die* die);
328
329 static bool
330 die_is_void_type(Dwarf_Die* die);
331
332 static bool
333 die_is_pointer_type(const Dwarf_Die* die);
334
335 static bool
336 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
337
338 static bool
339 die_is_reference_type(const Dwarf_Die* die);
340
341 static bool
342 die_is_pointer_or_reference_type(const Dwarf_Die* die);
343
344 static bool
345 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
346
347 static bool
348 die_is_class_type(const Dwarf_Die* die);
349
350 static bool
351 die_is_qualified_type(const Dwarf_Die* die);
352
353 static bool
354 die_is_function_type(const Dwarf_Die *die);
355
356 static bool
357 die_has_object_pointer(const Dwarf_Die* die,
358                        Dwarf_Die& object_pointer);
359
360 static bool
361 die_this_pointer_from_object_pointer(Dwarf_Die* die,
362                                      Dwarf_Die& this_pointer);
363
364 static bool
365 die_this_pointer_is_const(Dwarf_Die* die);
366
367 static bool
368 die_object_pointer_is_for_const_method(Dwarf_Die* die);
369
370 static bool
371 die_is_at_class_scope(const read_context& ctxt,
372                       const Dwarf_Die* die,
373                       size_t where_offset,
374                       Dwarf_Die& class_scope_die);
375 static bool
376 eval_last_constant_dwarf_sub_expr(Dwarf_Op*     expr,
377                                   uint64_t      expr_len,
378                                   int64_t&      value,
379                                   bool& is_tls_address);
380
381 static translation_unit::language
382 dwarf_language_to_tu_language(size_t l);
383
384 static bool
385 die_unsigned_constant_attribute(const Dwarf_Die*        die,
386                                 unsigned                attr_name,
387                                 uint64_t&               cst);
388
389 static bool
390 die_signed_constant_attribute(const Dwarf_Die*die,
391                               unsigned  attr_name,
392                               int64_t&  cst);
393
394 static bool
395 die_constant_attribute(const Dwarf_Die *die,
396                        unsigned attr_name,
397                        array_type_def::subrange_type::bound_value &value);
398
399 static bool
400 die_attribute_has_form(const Dwarf_Die* die,
401                        unsigned attr_name,
402                        unsigned int     form);
403
404 static bool
405 form_is_DW_FORM_strx(unsigned form);
406
407 static bool
408 die_attribute_is_signed(const Dwarf_Die* die, unsigned attr_name);
409
410 static bool
411 die_attribute_is_unsigned(const Dwarf_Die* die, unsigned attr_name);
412
413 static bool
414 die_attribute_has_no_signedness(const Dwarf_Die* die, unsigned attr_name);
415
416 static bool
417 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
418
419 static string
420 die_name(const Dwarf_Die* die);
421
422 static location
423 die_location(const read_context& ctxt, const Dwarf_Die* die);
424
425 static bool
426 die_location_address(Dwarf_Die* die,
427                      Dwarf_Addr&        address,
428                      bool&              is_tls_address);
429
430 static bool
431 die_die_attribute(const Dwarf_Die* die,
432                   unsigned attr_name,
433                   Dwarf_Die& result,
434                   bool look_thru_abstract_origin = true);
435
436 static string
437 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
438
439 static string
440 build_internal_anonymous_die_name(const string &base_name,
441                                   size_t anonymous_type_index);
442
443
444 static string
445 get_internal_anonymous_die_name(Dwarf_Die *die,
446                                 size_t anonymous_type_index);
447
448 static string
449 die_qualified_type_name(const read_context& ctxt,
450                         const Dwarf_Die* die,
451                         size_t where);
452
453 static string
454 die_qualified_decl_name(const read_context& ctxt,
455                         const Dwarf_Die* die,
456                         size_t where);
457
458 static string
459 die_qualified_name(const read_context& ctxt,
460                    const Dwarf_Die* die,
461                    size_t where);
462
463 static bool
464 die_qualified_type_name_empty(const read_context& ctxt,
465                               const Dwarf_Die* die, size_t where,
466                               string &qualified_name);
467
468 static void
469 die_return_and_parm_names_from_fn_type_die(const read_context& ctxt,
470                                            const Dwarf_Die* die,
471                                            size_t where_offset,
472                                            bool pretty_print,
473                                            string &return_type_name,
474                                            string &class_name,
475                                            vector<string>& parm_names,
476                                            bool& is_const,
477                                            bool& is_static);
478
479 static string
480 die_function_signature(const read_context& ctxt,
481                        const Dwarf_Die *die,
482                        size_t where_offset);
483
484 static bool
485 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
486
487 static bool
488 die_function_type_is_method_type(const read_context& ctxt,
489                                  const Dwarf_Die *die,
490                                  size_t where_offset,
491                                  Dwarf_Die& object_pointer_die,
492                                  Dwarf_Die& class_die,
493                                  bool& is_static);
494
495 static string
496 die_pretty_print_type(read_context& ctxt,
497                       const Dwarf_Die* die,
498                       size_t where_offset);
499
500 static string
501 die_pretty_print_decl(read_context& ctxt,
502                       const Dwarf_Die* die,
503                       size_t where_offset);
504
505 static string
506 die_pretty_print(read_context& ctxt,
507                  const Dwarf_Die* die,
508                  size_t where_offset);
509
510 static void
511 maybe_canonicalize_type(const Dwarf_Die* die,
512                         read_context& ctxt);
513
514 static void
515 maybe_canonicalize_type(const type_base_sptr&   t,
516                         read_context&           ctxt);
517
518 static void
519 maybe_canonicalize_type(const Dwarf_Die*        die,
520                         const type_base_sptr&   t,
521                         read_context&           ctxt);
522
523 static uint64_t
524 get_default_array_lower_bound(translation_unit::language l);
525
526 static bool
527 find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
528                                          Dwarf_Off,
529                                          imported_unit_points_type::const_iterator&);
530
531 static array_type_def::subrange_sptr
532 build_subrange_type(read_context&       ctxt,
533                     const Dwarf_Die*    die,
534                     size_t              where_offset,
535                     bool                associate_type_to_die = true);
536
537 static void
538 build_subranges_from_array_type_die(read_context&                       ctxt,
539                                     const Dwarf_Die*                    die,
540                                     array_type_def::subranges_type&     subranges,
541                                     size_t                              where_offset,
542                                     bool                                associate_type_to_die = true);
543
544 static bool
545 compare_dies(const read_context& ctxt,
546              const Dwarf_Die *l, const Dwarf_Die *r,
547              bool update_canonical_dies_on_the_fly);
548
549 /// Convert an elf symbol type (given by the ELF{32,64}_ST_TYPE
550 /// macros) into an elf_symbol::type value.
551 ///
552 /// Note that this function aborts when given an unexpected value.
553 ///
554 /// @param the symbol type value to convert.
555 ///
556 /// @return the converted value.
557 static elf_symbol::type
558 stt_to_elf_symbol_type(unsigned char stt)
559 {
560   elf_symbol::type t = elf_symbol::NOTYPE_TYPE;
561
562   switch (stt)
563     {
564     case STT_NOTYPE:
565       t = elf_symbol::NOTYPE_TYPE;
566       break;
567     case STT_OBJECT:
568       t = elf_symbol::OBJECT_TYPE;
569       break;
570     case STT_FUNC:
571       t = elf_symbol::FUNC_TYPE;
572       break;
573     case STT_SECTION:
574       t = elf_symbol::SECTION_TYPE;
575       break;
576     case STT_FILE:
577       t = elf_symbol::FILE_TYPE;
578       break;
579     case STT_COMMON:
580       t = elf_symbol::COMMON_TYPE;
581       break;
582     case STT_TLS:
583       t = elf_symbol::TLS_TYPE;
584       break;
585     case STT_GNU_IFUNC:
586       t = elf_symbol::GNU_IFUNC_TYPE;
587       break;
588     default:
589       // An unknown value that probably ought to be supported?  Let's
590       // abort right here rather than yielding garbage.
591       ABG_ASSERT_NOT_REACHED;
592     }
593
594   return t;
595 }
596
597 /// Convert an elf symbol binding (given by the ELF{32,64}_ST_BIND
598 /// macros) into an elf_symbol::binding value.
599 ///
600 /// Note that this function aborts when given an unexpected value.
601 ///
602 /// @param the symbol binding value to convert.
603 ///
604 /// @return the converted value.
605 static elf_symbol::binding
606 stb_to_elf_symbol_binding(unsigned char stb)
607 {
608   elf_symbol::binding b = elf_symbol::GLOBAL_BINDING;
609
610   switch (stb)
611     {
612     case STB_LOCAL:
613       b = elf_symbol::LOCAL_BINDING;
614       break;
615     case STB_GLOBAL:
616       b = elf_symbol::GLOBAL_BINDING;
617       break;
618     case STB_WEAK:
619       b = elf_symbol::WEAK_BINDING;
620       break;
621     case STB_GNU_UNIQUE:
622       b = elf_symbol::GNU_UNIQUE_BINDING;
623       break;
624     default:
625       ABG_ASSERT_NOT_REACHED;
626     }
627
628   return b;
629
630 }
631
632 /// Convert an ELF symbol visiblity given by the symbols ->st_other
633 /// data member as returned by the GELF_ST_VISIBILITY macro into a
634 /// elf_symbol::visiblity value.
635 ///
636 /// @param stv the value of the ->st_other data member of the ELF
637 /// symbol.
638 ///
639 /// @return the converted elf_symbol::visiblity value.
640 static elf_symbol::visibility
641 stv_to_elf_symbol_visibility(unsigned char stv)
642 {
643
644   elf_symbol::visibility v = elf_symbol::DEFAULT_VISIBILITY;
645
646   switch (stv)
647     {
648     case STV_DEFAULT:
649       v = elf_symbol::DEFAULT_VISIBILITY;
650       break;
651     case STV_INTERNAL:
652       v = elf_symbol::INTERNAL_VISIBILITY;
653       break;
654     case STV_HIDDEN:
655       v = elf_symbol::HIDDEN_VISIBILITY;
656       break;
657     case STV_PROTECTED:
658       v = elf_symbol::PROTECTED_VISIBILITY;
659       break;
660     default:
661       ABG_ASSERT_NOT_REACHED;
662     }
663
664   return v;
665 }
666
667 /// Convert the value of the e_machine field of GElf_Ehdr into a
668 /// string.  This is to get a string representing the architecture of
669 /// the elf file at hand.
670 ///
671 /// @param e_machine the value of GElf_Ehdr::e_machine.
672 ///
673 /// @return the string representation of GElf_Ehdr::e_machine.
674 static string
675 e_machine_to_string(GElf_Half e_machine)
676 {
677   string result;
678   switch (e_machine)
679     {
680     case EM_NONE:
681       result = "elf-no-arch";
682       break;
683     case EM_M32:
684       result = "elf-att-we-32100";
685       break;
686     case EM_SPARC:
687       result = "elf-sun-sparc";
688       break;
689     case EM_386:
690       result = "elf-intel-80386";
691       break;
692     case EM_68K:
693       result = "elf-motorola-68k";
694       break;
695     case EM_88K:
696       result = "elf-motorola-88k";
697       break;
698     case EM_860:
699       result = "elf-intel-80860";
700       break;
701     case EM_MIPS:
702       result = "elf-mips-r3000-be";
703       break;
704     case EM_S370:
705       result = "elf-ibm-s370";
706       break;
707     case EM_MIPS_RS3_LE:
708       result = "elf-mips-r3000-le";
709       break;
710     case EM_PARISC:
711       result = "elf-hp-parisc";
712       break;
713     case EM_VPP500:
714       result = "elf-fujitsu-vpp500";
715       break;
716     case EM_SPARC32PLUS:
717       result = "elf-sun-sparc-v8plus";
718       break;
719     case EM_960:
720       result = "elf-intel-80960";
721       break;
722     case EM_PPC:
723       result = "elf-powerpc";
724       break;
725     case EM_PPC64:
726       result = "elf-powerpc-64";
727       break;
728     case EM_S390:
729       result = "elf-ibm-s390";
730       break;
731     case EM_V800:
732       result = "elf-nec-v800";
733       break;
734     case EM_FR20:
735       result = "elf-fujitsu-fr20";
736       break;
737     case EM_RH32:
738       result = "elf-trw-rh32";
739       break;
740     case EM_RCE:
741       result = "elf-motorola-rce";
742       break;
743     case EM_ARM:
744       result = "elf-arm";
745       break;
746     case EM_FAKE_ALPHA:
747       result = "elf-digital-alpha";
748       break;
749     case EM_SH:
750       result = "elf-hitachi-sh";
751       break;
752     case EM_SPARCV9:
753       result = "elf-sun-sparc-v9-64";
754       break;
755     case EM_TRICORE:
756       result = "elf-siemens-tricore";
757       break;
758     case EM_ARC:
759       result = "elf-argonaut-risc-core";
760       break;
761     case EM_H8_300:
762       result = "elf-hitachi-h8-300";
763       break;
764     case EM_H8_300H:
765       result = "elf-hitachi-h8-300h";
766       break;
767     case EM_H8S:
768       result = "elf-hitachi-h8s";
769       break;
770     case EM_H8_500:
771       result = "elf-hitachi-h8-500";
772       break;
773     case EM_IA_64:
774       result = "elf-intel-ia-64";
775       break;
776     case EM_MIPS_X:
777       result = "elf-stanford-mips-x";
778       break;
779     case EM_COLDFIRE:
780       result = "elf-motorola-coldfire";
781       break;
782     case EM_68HC12:
783       result = "elf-motorola-68hc12";
784       break;
785     case EM_MMA:
786       result = "elf-fujitsu-mma";
787       break;
788     case EM_PCP:
789       result = "elf-siemens-pcp";
790       break;
791     case EM_NCPU:
792       result = "elf-sony-ncpu";
793       break;
794     case EM_NDR1:
795       result = "elf-denso-ndr1";
796       break;
797     case EM_STARCORE:
798       result = "elf-motorola-starcore";
799       break;
800     case EM_ME16:
801       result = "elf-toyota-me16";
802       break;
803     case EM_ST100:
804       result = "elf-stm-st100";
805       break;
806     case EM_TINYJ:
807       result = "elf-alc-tinyj";
808       break;
809     case EM_X86_64:
810       result = "elf-amd-x86_64";
811       break;
812     case EM_PDSP:
813       result = "elf-sony-pdsp";
814       break;
815     case EM_FX66:
816       result = "elf-siemens-fx66";
817       break;
818     case EM_ST9PLUS:
819       result = "elf-stm-st9+";
820       break;
821     case EM_ST7:
822       result = "elf-stm-st7";
823       break;
824     case EM_68HC16:
825       result = "elf-motorola-68hc16";
826       break;
827     case EM_68HC11:
828       result = "elf-motorola-68hc11";
829       break;
830     case EM_68HC08:
831       result = "elf-motorola-68hc08";
832       break;
833     case EM_68HC05:
834       result = "elf-motorola-68hc05";
835       break;
836     case EM_SVX:
837       result = "elf-sg-svx";
838       break;
839     case EM_ST19:
840       result = "elf-stm-st19";
841       break;
842     case EM_VAX:
843       result = "elf-digital-vax";
844       break;
845     case EM_CRIS:
846       result = "elf-axis-cris";
847       break;
848     case EM_JAVELIN:
849       result = "elf-infineon-javelin";
850       break;
851     case EM_FIREPATH:
852       result = "elf-firepath";
853       break;
854     case EM_ZSP:
855       result = "elf-lsi-zsp";
856       break;
857     case EM_MMIX:
858       result = "elf-don-knuth-mmix";
859       break;
860     case EM_HUANY:
861       result = "elf-harvard-huany";
862       break;
863     case EM_PRISM:
864       result = "elf-sitera-prism";
865       break;
866     case EM_AVR:
867       result = "elf-atmel-avr";
868       break;
869     case EM_FR30:
870       result = "elf-fujistu-fr30";
871       break;
872     case EM_D10V:
873       result = "elf-mitsubishi-d10v";
874       break;
875     case EM_D30V:
876       result = "elf-mitsubishi-d30v";
877       break;
878     case EM_V850:
879       result = "elf-nec-v850";
880       break;
881     case EM_M32R:
882       result = "elf-mitsubishi-m32r";
883       break;
884     case EM_MN10300:
885       result = "elf-matsushita-mn10300";
886       break;
887     case EM_MN10200:
888       result = "elf-matsushita-mn10200";
889       break;
890     case EM_PJ:
891       result = "elf-picojava";
892       break;
893     case EM_OPENRISC:
894       result = "elf-openrisc-32";
895       break;
896     case EM_ARC_A5:
897       result = "elf-arc-a5";
898       break;
899     case EM_XTENSA:
900       result = "elf-tensilica-xtensa";
901       break;
902
903 #ifdef HAVE_EM_AARCH64_MACRO
904     case EM_AARCH64:
905       result = "elf-arm-aarch64";
906       break;
907 #endif
908
909 #ifdef HAVE_EM_TILEPRO_MACRO
910     case EM_TILEPRO:
911       result = "elf-tilera-tilepro";
912       break;
913 #endif
914
915 #ifdef HAVE_EM_TILEGX_MACRO
916     case EM_TILEGX:
917       result = "elf-tilera-tilegx";
918       break;
919 #endif
920
921     case EM_NUM:
922       result = "elf-last-arch-number";
923       break;
924     case EM_ALPHA:
925       result = "elf-non-official-alpha";
926       break;
927     default:
928       {
929         std::ostringstream o;
930         o << "elf-unknown-arch-value-" << e_machine;
931         result = o.str();
932       }
933       break;
934     }
935   return result;
936 }
937
938 /// The kind of ELF hash table found by the function
939 /// find_hash_table_section_index.
940 enum hash_table_kind
941 {
942   NO_HASH_TABLE_KIND = 0,
943   SYSV_HASH_TABLE_KIND,
944   GNU_HASH_TABLE_KIND
945 };
946
947 /// Get the offset offset of the hash table section.
948 ///
949 /// @param elf_handle the elf handle to use.
950 ///
951 /// @param ht_section_offset this is set to the resulting offset
952 /// of the hash table section.  This is set iff the function returns true.
953 ///
954 /// @param symtab_section_offset the offset of the section of the
955 /// symbol table the hash table refers to.
956 static hash_table_kind
957 find_hash_table_section_index(Elf*      elf_handle,
958                               size_t&   ht_section_index,
959                               size_t&   symtab_section_index)
960 {
961   if (!elf_handle)
962     return NO_HASH_TABLE_KIND;
963
964   GElf_Shdr header_mem, *section_header;
965   bool found_sysv_ht = false, found_gnu_ht = false;
966   for (Elf_Scn* section = elf_nextscn(elf_handle, 0);
967        section != 0;
968        section = elf_nextscn(elf_handle, section))
969     {
970       section_header= gelf_getshdr(section, &header_mem);
971       if (section_header->sh_type != SHT_HASH
972           && section_header->sh_type != SHT_GNU_HASH)
973         continue;
974
975       ht_section_index = elf_ndxscn(section);
976       symtab_section_index = section_header->sh_link;
977
978       if (section_header->sh_type == SHT_HASH)
979         found_sysv_ht = true;
980       else if (section_header->sh_type == SHT_GNU_HASH)
981         found_gnu_ht = true;
982     }
983
984   if (found_gnu_ht)
985     return GNU_HASH_TABLE_KIND;
986   else if (found_sysv_ht)
987     return SYSV_HASH_TABLE_KIND;
988   else
989     return NO_HASH_TABLE_KIND;
990 }
991
992 /// Find the symbol table.
993 ///
994 /// If we are looking at a relocatable or executable file, this
995 /// function will return the .symtab symbol table (of type
996 /// SHT_SYMTAB).  But if we are looking at a DSO it returns the
997 /// .dynsym symbol table (of type SHT_DYNSYM).
998 ///
999 /// @param elf_handle the elf handle to consider.
1000 ///
1001 /// @param symtab the symbol table found.
1002 ///
1003 /// @return true iff the symbol table is found.
1004 static bool
1005 find_symbol_table_section(Elf* elf_handle, Elf_Scn*& symtab)
1006 {
1007   Elf_Scn* section = 0, *dynsym = 0, *sym_tab = 0;
1008   while ((section = elf_nextscn(elf_handle, section)) != 0)
1009     {
1010       GElf_Shdr header_mem, *header;
1011       header = gelf_getshdr(section, &header_mem);
1012       if (header->sh_type == SHT_DYNSYM)
1013         dynsym = section;
1014       else if (header->sh_type == SHT_SYMTAB)
1015         sym_tab = section;
1016     }
1017
1018   if (dynsym || sym_tab)
1019     {
1020       GElf_Ehdr eh_mem;
1021       GElf_Ehdr* elf_header = gelf_getehdr(elf_handle, &eh_mem);
1022       if (elf_header->e_type == ET_REL
1023           || elf_header->e_type == ET_EXEC)
1024         symtab = sym_tab ? sym_tab : dynsym;
1025       else
1026         symtab = dynsym ? dynsym : sym_tab;
1027       return true;
1028     }
1029   return false;
1030 }
1031
1032 /// Find the index (in the section headers table) of the symbol table
1033 /// section.
1034 ///
1035 /// If we are looking at a relocatable or executable file, this
1036 /// function will return the index for the .symtab symbol table (of
1037 /// type SHT_SYMTAB).  But if we are looking at a DSO it returns the
1038 /// index for the .dynsym symbol table (of type SHT_DYNSYM).
1039 ///
1040 /// @param elf_handle the elf handle to use.
1041 ///
1042 /// @param symtab_index the index of the symbol_table, that was found.
1043 ///
1044 /// @return true iff the symbol table section index was found.
1045 static bool
1046 find_symbol_table_section_index(Elf* elf_handle,
1047                                 size_t& symtab_index)
1048 {
1049   Elf_Scn* section = 0;
1050   if (!find_symbol_table_section(elf_handle, section))
1051     return false;
1052
1053   symtab_index = elf_ndxscn(section);
1054   return true;
1055 }
1056
1057 /// Find and return a section by its name and its type.
1058 ///
1059 /// @param elf_handle the elf handle to use.
1060 ///
1061 /// @param name the name of the section.
1062 ///
1063 /// @param section_type the type of the section.  This is the
1064 /// Elf32_Shdr::sh_type (or Elf64_Shdr::sh_type) data member.
1065 /// Examples of values of this parameter are SHT_PROGBITS or SHT_NOBITS.
1066 ///
1067 /// @return the section found, nor nil if none was found.
1068 static Elf_Scn*
1069 find_section(Elf* elf_handle, const string& name, Elf64_Word section_type)
1070 {
1071   size_t section_header_string_index = 0;
1072   if (elf_getshdrstrndx (elf_handle, &section_header_string_index) < 0)
1073     return 0;
1074
1075   Elf_Scn* section = 0;
1076   GElf_Shdr header_mem, *header;
1077   while ((section = elf_nextscn(elf_handle, section)) != 0)
1078     {
1079       header = gelf_getshdr(section, &header_mem);
1080       if (header == NULL || header->sh_type != section_type)
1081       continue;
1082
1083       const char* section_name =
1084         elf_strptr(elf_handle, section_header_string_index, header->sh_name);
1085       if (section_name && name == section_name)
1086         return section;
1087     }
1088
1089   return 0;
1090 }
1091
1092 /// Test if the ELF binary denoted by a given ELF handle is a Linux
1093 /// Kernel Module.
1094 ///
1095 /// @param elf_handle the ELF handle to consider.
1096 ///
1097 /// @return true iff the binary denoted by @p elf_handle is a Linux
1098 /// kernel module.
1099 static bool
1100 binary_is_linux_kernel_module(Elf *elf_handle)
1101 {
1102   return (find_section(elf_handle, ".modinfo", SHT_PROGBITS)
1103           && find_section(elf_handle,
1104                           ".gnu.linkonce.this_module",
1105                           SHT_PROGBITS));
1106 }
1107
1108 /// Test if the ELF binary denoted by a given ELF handle is a Linux
1109 /// Kernel binary (either vmlinux or a kernel module).
1110 ///
1111 /// @param elf_handle the ELF handle to consider.
1112 ///
1113 /// @return true iff the binary denoted by @p elf_handle is a Linux
1114 /// kernel binary
1115 static bool
1116 binary_is_linux_kernel(Elf *elf_handle)
1117 {
1118   return (find_section(elf_handle,
1119                        "__ksymtab_strings",
1120                        SHT_PROGBITS)
1121           || binary_is_linux_kernel_module(elf_handle));
1122 }
1123
1124 /// Find and return the .text section.
1125 ///
1126 /// @param elf_handle the elf handle to use.
1127 ///
1128 /// @return the .text section found.
1129 static Elf_Scn*
1130 find_text_section(Elf* elf_handle)
1131 {return find_section(elf_handle, ".text", SHT_PROGBITS);}
1132
1133 /// Find and return the .bss section.
1134 ///
1135 /// @param elf_handle.
1136 ///
1137 /// @return the .bss section found.
1138 static Elf_Scn*
1139 find_bss_section(Elf* elf_handle)
1140 {return find_section(elf_handle, ".bss", SHT_NOBITS);}
1141
1142 /// Find and return the .rodata section.
1143 ///
1144 /// @param elf_handle.
1145 ///
1146 /// @return the .rodata section found.
1147 static Elf_Scn*
1148 find_rodata_section(Elf* elf_handle)
1149 {return find_section(elf_handle, ".rodata", SHT_PROGBITS);}
1150
1151 /// Find and return the .data section.
1152 ///
1153 /// @param elf_handle the elf handle to use.
1154 ///
1155 /// @return the .data section found.
1156 static Elf_Scn*
1157 find_data_section(Elf* elf_handle)
1158 {return find_section(elf_handle, ".data", SHT_PROGBITS);}
1159
1160 /// Find and return the .data1 section.
1161 ///
1162 /// @param elf_handle the elf handle to use.
1163 ///
1164 /// @return the .data1 section found.
1165 static Elf_Scn*
1166 find_data1_section(Elf* elf_handle)
1167 {return find_section(elf_handle, ".data1", SHT_PROGBITS);}
1168
1169 /// Find the __ksymtab_strings section of a Linux kernel binary.
1170 ///
1171 ///
1172 /// @return the find_ksymtab_strings_section of the linux kernel
1173 /// binary denoted by @p elf_handle, or nil if such a section could
1174 /// not be found.
1175 static Elf_Scn*
1176 find_ksymtab_strings_section(Elf *elf_handle)
1177 {
1178   if (binary_is_linux_kernel(elf_handle))
1179     return find_section(elf_handle, "__ksymtab_strings", SHT_PROGBITS);
1180   return 0;
1181 }
1182
1183 /// Get the address at which a given binary is loaded in memoryâ‹…
1184 ///
1185 /// @param elf_handle the elf handle for the binary to consider.
1186 ///
1187 /// @param load_address the address where the binary is loaded.  This
1188 /// is set by the function iff it returns true.
1189 ///
1190 /// @return true if the function could get the binary load address
1191 /// and assign @p load_address to it.
1192 static bool
1193 get_binary_load_address(Elf *elf_handle,
1194                         GElf_Addr &load_address)
1195 {
1196   GElf_Ehdr eh_mem;
1197   GElf_Ehdr *elf_header = gelf_getehdr(elf_handle, &eh_mem);
1198   size_t num_segments = elf_header->e_phnum;
1199   GElf_Phdr *program_header = 0;
1200   GElf_Addr result;
1201   bool found_loaded_segment = false;
1202   GElf_Phdr ph_mem;
1203
1204   for (unsigned i = 0; i < num_segments; ++i)
1205     {
1206       program_header = gelf_getphdr(elf_handle, i, &ph_mem);
1207       if (program_header && program_header->p_type == PT_LOAD)
1208         {
1209           if (!found_loaded_segment)
1210             {
1211               result = program_header->p_vaddr;
1212               found_loaded_segment = true;
1213             }
1214
1215           if (program_header->p_vaddr < result)
1216             // The resulting load address we want is the lowest
1217             // load address of all the loaded segments.
1218             result = program_header->p_vaddr;
1219         }
1220     }
1221
1222   if (found_loaded_segment)
1223     {
1224       load_address = result;
1225       return true;
1226     }
1227   return false;
1228 }
1229
1230 /// Find the file name of the alternate debug info file.
1231 ///
1232 /// @param elf_module the elf module to consider.
1233 ///
1234 /// @param out parameter.  Is set to the file name of the alternate
1235 /// debug info file, iff this function returns true.
1236 ///
1237 /// @return true iff the location of the alternate debug info file was
1238 /// found.
1239 static bool
1240 find_alt_debug_info_link(Dwfl_Module *elf_module,
1241                          string &alt_file_name)
1242 {
1243   GElf_Addr bias = 0;
1244   Dwarf *dwarf = dwfl_module_getdwarf(elf_module, &bias);
1245   Elf *elf = dwarf_getelf(dwarf);
1246   GElf_Ehdr ehmem, *elf_header;
1247   elf_header = gelf_getehdr(elf, &ehmem);
1248
1249   Elf_Scn* section = 0;
1250   while ((section = elf_nextscn(elf, section)) != 0)
1251     {
1252       GElf_Shdr header_mem, *header;
1253       header = gelf_getshdr(section, &header_mem);
1254       if (header->sh_type != SHT_PROGBITS)
1255         continue;
1256
1257       const char *section_name = elf_strptr(elf,
1258                                             elf_header->e_shstrndx,
1259                                             header->sh_name);
1260
1261       char *alt_name = 0;
1262       char *buildid = 0;
1263       size_t buildid_len = 0;
1264       if (section_name != 0
1265           && strcmp(section_name, ".gnu_debugaltlink") == 0)
1266         {
1267           Elf_Data *data = elf_getdata(section, 0);
1268           if (data != 0 && data->d_size != 0)
1269             {
1270               alt_name = (char*) data->d_buf;
1271               char *end_of_alt_name =
1272                 (char *) memchr(alt_name, '\0', data->d_size);
1273               buildid_len = data->d_size - (end_of_alt_name - alt_name + 1);
1274               if (buildid_len == 0)
1275                 return false;
1276               buildid = end_of_alt_name + 1;
1277             }
1278         }
1279       else
1280         continue;
1281
1282       if (buildid == 0 || alt_name == 0)
1283         return false;
1284
1285       alt_file_name = alt_name;
1286       return true;
1287     }
1288
1289   return false;
1290 }
1291
1292 /// Find alternate debuginfo file of a given "link" under a set of
1293 /// root directories.
1294 ///
1295 /// The link is a string that is read by the function
1296 /// find_alt_debug_info_link().  That link is a path that is relative
1297 /// to a given debug info file, e.g, "../../../.dwz/something.debug".
1298 /// It designates the alternate debug info file associated to a given
1299 /// debug info file.
1300 ///
1301 /// This function will thus try to find the .dwz/something.debug file
1302 /// under some given root directories.
1303 ///
1304 /// @param root_dirs the set of root directories to look from.
1305 ///
1306 /// @param alt_file_name a relative path to the alternate debug info
1307 /// file to look for.
1308 ///
1309 /// @param alt_file_path the resulting absolute path to the alternate
1310 /// debuginfo path denoted by @p alt_file_name and found under one of
1311 /// the directories in @p root_dirs.  This is set iff the function
1312 /// returns true.
1313 ///
1314 /// @return true iff the function found the alternate debuginfo file.
1315 static bool
1316 find_alt_debug_info_path(const vector<char**> root_dirs,
1317                          const string &alt_file_name,
1318                          string &alt_file_path)
1319 {
1320   if (alt_file_name.empty())
1321     return false;
1322
1323   string altfile_name = tools_utils::trim_leading_string(alt_file_name, "../");
1324
1325   for (vector<char**>::const_iterator i = root_dirs.begin();
1326        i != root_dirs.end();
1327        ++i)
1328     if (tools_utils::find_file_under_dir(**i, altfile_name, alt_file_path))
1329       return true;
1330
1331   return false;
1332 }
1333
1334 /// Return the alternate debug info associated to a given main debug
1335 /// info file.
1336 ///
1337 /// @param elf_module the elf module to consider.
1338 ///
1339 /// @param debug_root_dirs a set of root debuginfo directories under
1340 /// which too look for the alternate debuginfo file.
1341 ///
1342 /// @param alt_file_name output parameter.  This is set to the file
1343 /// path of the alternate debug info file associated to @p elf_module.
1344 /// This is set iff the function returns a non-null result.
1345 ///
1346 /// @param alt_fd the file descriptor used to access the alternate
1347 /// debug info.  If this parameter is set by the function, then the
1348 /// caller needs to fclose it, otherwise the file descriptor is going
1349 /// to be leaked.  Note however that on recent versions of elfutils
1350 /// where libdw.h contains the function dwarf_getalt(), this parameter
1351 /// is set to 0, so it doesn't need to be fclosed.
1352 ///
1353 /// Note that the alternate debug info file is a DWARF extension as of
1354 /// DWARF 4 ans is decribed at
1355 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
1356 ///
1357 /// @return the alternate debuginfo, or null.  If @p alt_fd is
1358 /// non-zero, then the caller of this function needs to call
1359 /// dwarf_end() on the returned alternate debuginfo pointer,
1360 /// otherwise, it's going to be leaked.
1361 static Dwarf*
1362 find_alt_debug_info(Dwfl_Module *elf_module,
1363                     const vector<char**> debug_root_dirs,
1364                     string& alt_file_name,
1365                     int& alt_fd)
1366 {
1367   if (elf_module == 0)
1368     return 0;
1369
1370   Dwarf* result = 0;
1371   find_alt_debug_info_link(elf_module, alt_file_name);
1372
1373 #ifdef LIBDW_HAS_DWARF_GETALT
1374   // We are on recent versions of elfutils where the function
1375   // dwarf_getalt exists, so let's use it.
1376   Dwarf_Addr bias = 0;
1377   Dwarf* dwarf = dwfl_module_getdwarf(elf_module, &bias);
1378   result = dwarf_getalt(dwarf);
1379   alt_fd = 0;
1380 #else
1381   // We are on an old version of elfutils where the function
1382   // dwarf_getalt doesn't exist yet, so let's open code its
1383   // functionality
1384   char *alt_name = 0;
1385   const char *file_name = 0;
1386   void **user_data = 0;
1387   Dwarf_Addr low_addr = 0;
1388   char *alt_file = 0;
1389
1390   file_name = dwfl_module_info(elf_module, &user_data,
1391                                &low_addr, 0, 0, 0, 0, 0);
1392
1393   alt_fd = dwfl_standard_find_debuginfo(elf_module, user_data,
1394                                         file_name, low_addr,
1395                                         alt_name, file_name,
1396                                         0, &alt_file);
1397
1398   result = dwarf_begin(alt_fd, DWARF_C_READ);
1399 #endif
1400
1401   if (result == 0)
1402     {
1403       // So we didn't find the alternate debuginfo file from the
1404       // information that is in the debuginfo file associated to
1405       // elf_module.  Maybe the alternate debuginfo file is located
1406       // under one of the directories in debug_root_dirs.  So let's
1407       // look in there.
1408       string alt_file_path;
1409       if (!find_alt_debug_info_path(debug_root_dirs,
1410                                     alt_file_name,
1411                                     alt_file_path))
1412         return result;
1413
1414       // If we reach this point it means we have found the path to the
1415       // alternate debuginfo file and it's in alt_file_path.  So let's
1416       // open it and read it.
1417       int fd = open(alt_file_path.c_str(), O_RDONLY);
1418       if (fd == -1)
1419         return result;
1420       result = dwarf_begin(fd, DWARF_C_READ);
1421
1422 #ifdef LIBDW_HAS_DWARF_GETALT
1423       Dwarf_Addr bias = 0;
1424       Dwarf* dwarf = dwfl_module_getdwarf(elf_module, &bias);
1425       dwarf_setalt(dwarf, result);
1426 #endif
1427     }
1428
1429   return result;
1430 }
1431
1432 /// Compare a symbol name against another name, possibly demangling
1433 /// the symbol_name before performing the comparison.
1434 ///
1435 /// @param symbol_name the symbol_name to take in account.
1436 ///
1437 /// @param name the second name to take in account.
1438 ///
1439 /// @param demangle if true, demangle @p symbol_name and compare the
1440 /// result of the demangling with @p name.
1441 ///
1442 /// @return true iff symbol_name equals name.
1443 static bool
1444 compare_symbol_name(const string& symbol_name,
1445                     const string& name,
1446                     bool demangle)
1447 {
1448   if (demangle)
1449     {
1450       string m = demangle_cplus_mangled_name(symbol_name);
1451       return m == name;
1452     }
1453   return symbol_name == name;
1454 }
1455
1456 /// Return the SHT_GNU_versym, SHT_GNU_verdef and SHT_GNU_verneed
1457 /// sections that are involved in symbol versionning.
1458 ///
1459 /// @param elf_handle the elf handle to use.
1460 ///
1461 /// @param versym_section the SHT_GNU_versym section found.  If the
1462 /// section wasn't found, this is set to nil.
1463 ///
1464 /// @param verdef_section the SHT_GNU_verdef section found.  If the
1465 /// section wasn't found, this is set to nil.
1466 ///
1467 /// @param verneed_section the SHT_GNU_verneed section found.  If the
1468 /// section wasn't found, this is set to nil.
1469 ///
1470 /// @return true iff at least one of the sections where found.
1471 static bool
1472 get_symbol_versionning_sections(Elf*            elf_handle,
1473                                 Elf_Scn*&       versym_section,
1474                                 Elf_Scn*&       verdef_section,
1475                                 Elf_Scn*&       verneed_section)
1476 {
1477   Elf_Scn* section = NULL;
1478   GElf_Shdr mem;
1479   Elf_Scn* versym = NULL, *verdef = NULL, *verneed = NULL;
1480
1481   while ((section = elf_nextscn(elf_handle, section)) != NULL)
1482     {
1483       GElf_Shdr* h = gelf_getshdr(section, &mem);
1484       if (h->sh_type == SHT_GNU_versym)
1485         versym = section;
1486       else if (h->sh_type == SHT_GNU_verdef)
1487         verdef = section;
1488       else if (h->sh_type == SHT_GNU_verneed)
1489         verneed = section;
1490     }
1491
1492   if (versym || verdef || verneed)
1493     {
1494       // At least one the versionning sections was found.  Return it.
1495       versym_section = versym;
1496       verdef_section = verdef;
1497       verneed_section = verneed;
1498       return true;
1499     }
1500
1501   return false;
1502 }
1503
1504 /// Get the version definition (from the SHT_GNU_verdef section) of a
1505 /// given symbol represented by a pointer to GElf_Versym.
1506 ///
1507 /// @param elf_hande the elf handle to use.
1508 ///
1509 /// @param versym the symbol to get the version definition for.
1510 ///
1511 /// @param verdef_section the SHT_GNU_verdef section.
1512 ///
1513 /// @param version the resulting version definition.  This is set iff
1514 /// the function returns true.
1515 ///
1516 /// @return true upon successful completion, false otherwise.
1517 static bool
1518 get_version_definition_for_versym(Elf*                   elf_handle,
1519                                   GElf_Versym*           versym,
1520                                   Elf_Scn*               verdef_section,
1521                                   elf_symbol::version&   version)
1522 {
1523   Elf_Data* verdef_data = elf_getdata(verdef_section, NULL);
1524   GElf_Verdef verdef_mem;
1525   GElf_Verdef* verdef = gelf_getverdef(verdef_data, 0, &verdef_mem);
1526   size_t vd_offset = 0;
1527
1528   for (;; vd_offset += verdef->vd_next)
1529     {
1530       for (;verdef != 0;)
1531         {
1532           if (verdef->vd_ndx == (*versym & 0x7fff))
1533             // Found the version of the symbol.
1534             break;
1535           vd_offset += verdef->vd_next;
1536           verdef = (verdef->vd_next == 0
1537                     ? 0
1538                     : gelf_getverdef(verdef_data, vd_offset, &verdef_mem));
1539         }
1540
1541       if (verdef != 0)
1542         {
1543           GElf_Verdaux verdaux_mem;
1544           GElf_Verdaux *verdaux = gelf_getverdaux(verdef_data,
1545                                                   vd_offset + verdef->vd_aux,
1546                                                   &verdaux_mem);
1547           GElf_Shdr header_mem;
1548           GElf_Shdr* verdef_section_header = gelf_getshdr(verdef_section,
1549                                                           &header_mem);
1550           size_t verdef_stridx = verdef_section_header->sh_link;
1551           version.str(elf_strptr(elf_handle, verdef_stridx, verdaux->vda_name));
1552           if (*versym & 0x8000)
1553             version.is_default(false);
1554           else
1555             version.is_default(true);
1556           return true;
1557         }
1558       if (!verdef || verdef->vd_next == 0)
1559         break;
1560     }
1561   return false;
1562 }
1563
1564 /// Get the version needed (from the SHT_GNU_verneed section) to
1565 /// resolve an undefined symbol represented by a pointer to
1566 /// GElf_Versym.
1567 ///
1568 /// @param elf_hande the elf handle to use.
1569 ///
1570 /// @param versym the symbol to get the version definition for.
1571 ///
1572 /// @param verneed_section the SHT_GNU_verneed section.
1573 ///
1574 /// @param version the resulting version definition.  This is set iff
1575 /// the function returns true.
1576 ///
1577 /// @return true upon successful completion, false otherwise.
1578 static bool
1579 get_version_needed_for_versym(Elf*                      elf_handle,
1580                               GElf_Versym*              versym,
1581                               Elf_Scn*                  verneed_section,
1582                               elf_symbol::version&      version)
1583 {
1584   if (versym == 0 || elf_handle == 0 || verneed_section == 0)
1585     return false;
1586
1587   size_t vn_offset = 0;
1588   Elf_Data* verneed_data = elf_getdata(verneed_section, NULL);
1589   GElf_Verneed verneed_mem;
1590   GElf_Verneed* verneed = gelf_getverneed(verneed_data, 0, &verneed_mem);
1591
1592   for (;verneed; vn_offset += verneed->vn_next)
1593     {
1594       size_t vna_offset = vn_offset;
1595       GElf_Vernaux vernaux_mem;
1596       GElf_Vernaux *vernaux = gelf_getvernaux(verneed_data,
1597                                               vn_offset + verneed->vn_aux,
1598                                               &vernaux_mem);
1599       for (;vernaux != 0 && verneed;)
1600         {
1601           if (vernaux->vna_other == *versym)
1602             // Found the version of the symbol.
1603             break;
1604           vna_offset += verneed->vn_next;
1605           verneed = (verneed->vn_next == 0
1606                      ? 0
1607                      : gelf_getverneed(verneed_data, vna_offset, &verneed_mem));
1608         }
1609
1610       if (verneed != 0 && vernaux != 0 && vernaux->vna_other == *versym)
1611         {
1612           GElf_Shdr header_mem;
1613           GElf_Shdr* verneed_section_header = gelf_getshdr(verneed_section,
1614                                                            &header_mem);
1615           size_t verneed_stridx = verneed_section_header->sh_link;
1616           version.str(elf_strptr(elf_handle,
1617                                  verneed_stridx,
1618                                  vernaux->vna_name));
1619           if (*versym & 0x8000)
1620             version.is_default(false);
1621           else
1622             version.is_default(true);
1623           return true;
1624         }
1625
1626       if (!verneed || verneed->vn_next == 0)
1627         break;
1628     }
1629   return false;
1630 }
1631
1632 /// Return the version for a symbol that is at a given index in its
1633 /// SHT_SYMTAB section.
1634 ///
1635 /// @param elf_handle the elf handle to use.
1636 ///
1637 /// @param symbol_index the index of the symbol to consider.
1638 ///
1639 /// @param get_def_version if this is true, it means that that we want
1640 /// the version for a defined symbol; in that case, the version is
1641 /// looked for in a section of type SHT_GNU_verdef.  Otherwise, if
1642 /// this parameter is false, this means that we want the version for
1643 /// an undefined symbol; in that case, the version is the needed one
1644 /// for the symbol to be resolved; so the version is looked fo in a
1645 /// section of type SHT_GNU_verneed.
1646 ///
1647 /// @param version the version found for symbol at @p symbol_index.
1648 ///
1649 /// @return true iff a version was found for symbol at index @p
1650 /// symbol_index.
1651 static bool
1652 get_version_for_symbol(Elf*                     elf_handle,
1653                        size_t                   symbol_index,
1654                        bool                     get_def_version,
1655                        elf_symbol::version&     version)
1656 {
1657   Elf_Scn *versym_section = NULL,
1658     *verdef_section = NULL,
1659     *verneed_section = NULL;
1660
1661   if (!get_symbol_versionning_sections(elf_handle,
1662                                        versym_section,
1663                                        verdef_section,
1664                                        verneed_section))
1665     return false;
1666
1667   GElf_Versym versym_mem;
1668   Elf_Data* versym_data = (versym_section)
1669     ? elf_getdata(versym_section, NULL)
1670     : NULL;
1671   GElf_Versym* versym = (versym_data)
1672     ? gelf_getversym(versym_data, symbol_index, &versym_mem)
1673     : NULL;
1674
1675   if (versym == 0 || *versym <= 1)
1676     // I got these value from the code of readelf.c in elfutils.
1677     // Apparently, if the symbol version entry has these values, the
1678     // symbol must be discarded. This is not documented in the
1679     // official specification.
1680     return false;
1681
1682   if (get_def_version)
1683     {
1684       if (*versym == 0x8001)
1685         // I got this value from the code of readelf.c in elfutils
1686         // too.  It's not really documented in the official
1687         // specification.
1688         return false;
1689
1690       if (verdef_section
1691           && get_version_definition_for_versym(elf_handle, versym,
1692                                                verdef_section, version))
1693         return true;
1694     }
1695   else
1696     {
1697       if (verneed_section
1698           && get_version_needed_for_versym(elf_handle, versym,
1699                                            verneed_section, version))
1700         return true;
1701     }
1702
1703   return false;
1704 }
1705
1706 /// Lookup a symbol using the SysV ELF hash table.
1707 ///
1708 /// Note that this function hasn't been tested.  So it hasn't been
1709 /// debugged yet.  IOW, it is not known to work.  Or rather, it's
1710 /// almost like it's surely doesn't work ;-)
1711 ///
1712 /// Use it at your own risks.  :-)
1713 ///
1714 ///@parm env the environment we are operating from.
1715 ///
1716 /// @param elf_handle the elf_handle to use.
1717 ///
1718 /// @param sym_name the symbol name to look for.
1719 ///
1720 /// @param ht_index the index (in the section headers table) of the
1721 /// hash table section to use.
1722 ///
1723 /// @param sym_tab_index the index (in the section headers table) of
1724 /// the symbol table to use.
1725 ///
1726 /// @param demangle if true, demangle @p sym_name before comparing it
1727 /// to names from the symbol table.
1728 ///
1729 /// @param syms_found a vector of symbols found with the name @p
1730 /// sym_name.  table.
1731 static bool
1732 lookup_symbol_from_sysv_hash_tab(const environment*             env,
1733                                  Elf*                           elf_handle,
1734                                  const string&                  sym_name,
1735                                  size_t                 ht_index,
1736                                  size_t                 sym_tab_index,
1737                                  bool                           demangle,
1738                                  vector<elf_symbol_sptr>&       syms_found)
1739 {
1740   Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1741   ABG_ASSERT(sym_tab_section);
1742
1743   Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
1744   ABG_ASSERT(sym_tab_data);
1745
1746   GElf_Shdr sheader_mem;
1747   GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
1748                                                    &sheader_mem);
1749   Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1750   ABG_ASSERT(hash_section);
1751
1752   // Poke at the different parts of the hash table and get them ready
1753   // to be used.
1754   unsigned long hash = elf_hash(sym_name.c_str());
1755   Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1756   Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1757   size_t nb_buckets = ht_data[0];
1758   size_t nb_chains = ht_data[1];
1759
1760   if (nb_buckets == 0)
1761     // An empty hash table.  Not sure if that is possible, but it
1762     // would mean an empty table of exported symbols.
1763     return false;
1764
1765   //size_t nb_chains = ht_data[1];
1766   Elf32_Word* ht_buckets = &ht_data[2];
1767   Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
1768
1769   // Now do the real work.
1770   size_t bucket = hash % nb_buckets;
1771   size_t symbol_index = ht_buckets[bucket];
1772
1773   GElf_Sym symbol;
1774   const char* sym_name_str;
1775   size_t sym_size;
1776   elf_symbol::type sym_type;
1777   elf_symbol::binding sym_binding;
1778   elf_symbol::visibility sym_visibility;
1779   bool found = false;
1780   Elf_Scn *strings_section = find_ksymtab_strings_section(elf_handle);
1781   size_t strings_ndx = strings_section
1782     ? elf_ndxscn(strings_section)
1783     : 0;
1784
1785   do
1786     {
1787       ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
1788       sym_name_str = elf_strptr(elf_handle,
1789                                 sym_tab_section_header->sh_link,
1790                                 symbol.st_name);
1791       if (sym_name_str
1792           && compare_symbol_name(sym_name_str, sym_name, demangle))
1793         {
1794           sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1795           sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1796           sym_visibility =
1797             stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1798           sym_size = symbol.st_size;
1799           elf_symbol::version ver;
1800           if (get_version_for_symbol(elf_handle, symbol_index,
1801                                      /*get_def_version=*/true, ver))
1802             ABG_ASSERT(!ver.str().empty());
1803           elf_symbol_sptr symbol_found =
1804             elf_symbol::create(env,
1805                                symbol_index,
1806                                sym_size,
1807                                sym_name_str,
1808                                sym_type,
1809                                sym_binding,
1810                                symbol.st_shndx != SHN_UNDEF,
1811                                symbol.st_shndx == SHN_COMMON,
1812                                ver, sym_visibility,
1813                                symbol.st_shndx == strings_ndx);
1814           syms_found.push_back(symbol_found);
1815           found = true;
1816         }
1817       symbol_index = ht_chains[symbol_index];
1818     } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
1819
1820   return found;
1821 }
1822
1823 /// Get the size of the elf class, in bytes.
1824 ///
1825 /// @param elf_handle the elf handle to use.
1826 ///
1827 /// @return the size computed.
1828 static char
1829 get_elf_class_size_in_bytes(Elf* elf_handle)
1830 {
1831   char result = 0;
1832   GElf_Ehdr hdr;
1833
1834   ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
1835   int c = hdr.e_ident[EI_CLASS];
1836
1837   switch (c)
1838     {
1839     case ELFCLASS32:
1840       result = 4;
1841       break;
1842     case ELFCLASS64:
1843       result = 8;
1844       break;
1845     default:
1846       ABG_ASSERT_NOT_REACHED;
1847     }
1848
1849   return result;
1850 }
1851
1852 /// Get a given word of a bloom filter, referred to by the index of
1853 /// the word.  The word size depends on the current elf class and this
1854 /// function abstracts that nicely.
1855 ///
1856 /// @param elf_handle the elf handle to use.
1857 ///
1858 /// @param bloom_filter the bloom filter to consider.
1859 ///
1860 /// @param index the index of the bloom filter to return.
1861 static GElf_Word
1862 bloom_word_at(Elf*              elf_handle,
1863               Elf32_Word*       bloom_filter,
1864               size_t            index)
1865 {
1866   GElf_Word result = 0;
1867   GElf_Ehdr h;
1868   ABG_ASSERT(gelf_getehdr(elf_handle, &h));
1869   int c;
1870   c = h.e_ident[EI_CLASS];
1871
1872   switch(c)
1873     {
1874     case ELFCLASS32:
1875       result = bloom_filter[index];
1876       break ;
1877     case ELFCLASS64:
1878       {
1879         GElf_Word* f= reinterpret_cast<GElf_Word*>(bloom_filter);
1880         result = f[index];
1881       }
1882       break;
1883     default:
1884       abort();
1885     }
1886
1887   return result;
1888 }
1889
1890 /// The abstraction of the gnu elf hash table.
1891 ///
1892 /// The members of this struct are explained at
1893 ///   - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1894 ///   - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1895 struct gnu_ht
1896 {
1897   size_t nb_buckets;
1898   Elf32_Word* buckets;
1899   Elf32_Word* chain;
1900   size_t first_sym_index;
1901   size_t bf_nwords;
1902   size_t bf_size;
1903   Elf32_Word* bloom_filter;
1904   size_t shift;
1905   size_t sym_count;
1906   Elf_Scn* sym_tab_section;
1907   GElf_Shdr sym_tab_section_header;
1908
1909   gnu_ht()
1910     : nb_buckets(0),
1911       buckets(0),
1912       chain(0),
1913       first_sym_index(0),
1914       bf_nwords(0),
1915       bf_size(0),
1916       bloom_filter(0),
1917       shift(0),
1918       sym_count(0),
1919       sym_tab_section(0)
1920   {}
1921 }; // end struct gnu_ht
1922
1923 /// Setup the members of the gnu hash table.
1924 ///
1925 /// @param elf_handle a handle on the elf file to use.
1926 ///
1927 /// @param ht_index the index  (into the elf section headers table) of
1928 /// the hash table section to use.
1929 ///
1930 /// @param sym_tab_index the index (into the elf section headers
1931 /// table) of the symbol table the gnu hash table is about.
1932 ///
1933 /// @param ht the resulting hash table.
1934 ///
1935 /// @return true iff the hash table @ ht could be setup.
1936 static bool
1937 setup_gnu_ht(Elf* elf_handle,
1938              size_t ht_index,
1939              size_t sym_tab_index,
1940              gnu_ht& ht)
1941 {
1942   ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1943   ABG_ASSERT(ht.sym_tab_section);
1944   ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
1945   ht.sym_count =
1946     ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
1947   Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1948   ABG_ASSERT(hash_section);
1949
1950   // Poke at the different parts of the hash table and get them ready
1951   // to be used.
1952   Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1953   Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1954
1955   ht.nb_buckets = ht_data[0];
1956   if (ht.nb_buckets == 0)
1957     // An empty hash table.  Not sure if that is possible, but it
1958     // would mean an empty table of exported symbols.
1959     return false;
1960   ht.first_sym_index = ht_data[1];
1961   // The number of words used by the bloom filter.  A size of a word
1962   // is ELFCLASS.
1963   ht.bf_nwords = ht_data[2];
1964   // The shift used by the bloom filter code.
1965   ht.shift = ht_data[3];
1966   // The data of the bloom filter proper.
1967   ht.bloom_filter = &ht_data[4];
1968   // The size of the bloom filter in 4 bytes word.  This is going to
1969   // be used to index the 'bloom_filter' above, which is of type
1970   // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1971   // words.
1972   ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1973   // The buckets of the hash table.
1974   ht.buckets = ht.bloom_filter + ht.bf_size;
1975   // The chain of the hash table.
1976   ht.chain = ht.buckets + ht.nb_buckets;
1977
1978   return true;
1979 }
1980
1981 /// Look into the symbol tables of the underlying elf file and find
1982 /// the symbol we are being asked.
1983 ///
1984 /// This function uses the GNU hash table for the symbol lookup.
1985 ///
1986 /// The reference of for the implementation of this function can be
1987 /// found at:
1988 ///   - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1989 ///   - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1990 ///
1991 /// @param elf_handle the elf handle to use.
1992 ///
1993 /// @param sym_name the name of the symbol to look for.
1994 ///
1995 /// @param ht_index the index of the hash table header to use.
1996 ///
1997 /// @param sym_tab_index the index of the symbol table header to use
1998 /// with this hash table.
1999 ///
2000 /// @param demangle if true, demangle @p sym_name.
2001 ///
2002 /// @param syms_found the vector of symbols found with the name @p
2003 /// sym_name.
2004 ///
2005 /// @return true if a symbol was actually found.
2006 static bool
2007 lookup_symbol_from_gnu_hash_tab(const environment*              env,
2008                                 Elf*                            elf_handle,
2009                                 const string&                   sym_name,
2010                                 size_t                          ht_index,
2011                                 size_t                          sym_tab_index,
2012                                 bool                            demangle,
2013                                 vector<elf_symbol_sptr>&        syms_found)
2014 {
2015   gnu_ht ht;
2016   if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
2017     return false;
2018
2019   // Now do the real work.
2020
2021   // Compute bloom hashes (GNU hash and second bloom specific hashes).
2022   size_t h1 = elf_gnu_hash(sym_name.c_str());
2023   size_t h2 = h1 >> ht.shift;
2024   // The size of one of the words used in the bloom
2025   // filter, in bits.
2026   int c = get_elf_class_size_in_bytes(elf_handle) * 8;
2027   int n =  (h1 / c) % ht.bf_nwords;
2028   unsigned char bitmask = (1 << (h1 % c)) | (1 << (h2 % c));
2029
2030   // Test if the symbol is *NOT* present in this ELF file.
2031   if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
2032     return false;
2033
2034   size_t i = ht.buckets[h1 % ht.nb_buckets];
2035   if (i == STN_UNDEF)
2036     return false;
2037
2038   Elf32_Word stop_word, *stop_wordp;
2039   elf_symbol::version ver;
2040   GElf_Sym symbol;
2041   const char* sym_name_str;
2042   bool found = false;
2043
2044   elf_symbol::type sym_type;
2045   elf_symbol::binding sym_binding;
2046   elf_symbol::visibility sym_visibility;
2047   Elf_Scn *strings_section = find_ksymtab_strings_section(elf_handle);
2048     size_t strings_ndx = strings_section
2049     ? elf_ndxscn(strings_section)
2050     : 0;
2051
2052   // Let's walk the hash table and record the versions of all the
2053   // symbols which name equal sym_name.
2054   for (i = ht.buckets[h1 % ht.nb_buckets],
2055          stop_wordp = &ht.chain[i - ht.first_sym_index];
2056        i != STN_UNDEF
2057          && (stop_wordp
2058              < ht.chain + (ht.sym_count - ht.first_sym_index));
2059        ++i, ++stop_wordp)
2060     {
2061       stop_word = *stop_wordp;
2062       if ((stop_word & ~ 1)!= (h1 & ~1))
2063         // A given bucket can reference several hashes.  Here we
2064         // stumbled across a hash value different from the one we are
2065         // looking for.  Let's keep walking.
2066         continue;
2067
2068       ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
2069                          i, &symbol));
2070       sym_name_str = elf_strptr(elf_handle,
2071                                 ht.sym_tab_section_header.sh_link,
2072                                 symbol.st_name);
2073       if (sym_name_str
2074           && compare_symbol_name(sym_name_str, sym_name, demangle))
2075         {
2076           // So we found a symbol (in the symbol table) that equals
2077           // sym_name.  Now lets try to get its version and record it.
2078           sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
2079           sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
2080          sym_visibility =
2081            stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
2082
2083           if (get_version_for_symbol(elf_handle, i,
2084                                      /*get_def_version=*/true,
2085                                      ver))
2086             ABG_ASSERT(!ver.str().empty());
2087
2088           elf_symbol_sptr symbol_found =
2089             elf_symbol::create(env, i,
2090                                symbol.st_size,
2091                                sym_name_str,
2092                                sym_type, sym_binding,
2093                                symbol.st_shndx != SHN_UNDEF,
2094                                symbol.st_shndx == SHN_COMMON,
2095                                ver, sym_visibility,
2096                                symbol.st_shndx == strings_ndx);
2097           syms_found.push_back(symbol_found);
2098           found = true;
2099         }
2100
2101       if (stop_word & 1)
2102         // The last bit of the stop_word is 1.  That means we need to
2103         // stop here.  We reached the end of the chain of values
2104         // referenced by the hask bucket.
2105         break;
2106     }
2107   return found;
2108 }
2109
2110 /// Look into the symbol tables of the underlying elf file and find
2111 /// the symbol we are being asked.
2112 ///
2113 /// This function uses the elf hash table (be it the GNU hash table or
2114 /// the sysv hash table) for the symbol lookup.
2115 ///
2116 /// @param env the environment we are operating from.
2117 ///
2118 /// @param elf_handle the elf handle to use.
2119 ///
2120 /// @param ht_kind the kind of hash table to use.  This is returned by
2121 /// the function function find_hash_table_section_index.
2122 ///
2123 /// @param ht_index the index (in the section headers table) of the
2124 /// hash table section to use.
2125 ///
2126 /// @param sym_tab_index the index (in section headers table) of the
2127 /// symbol table index to use with this hash table.
2128 ///
2129 /// @param symbol_name the name of the symbol to look for.
2130 ///
2131 /// @param demangle if true, demangle @p sym_name.
2132 ///
2133 /// @param syms_found the symbols that were actually found with the
2134 /// name @p symbol_name.
2135 ///
2136 /// @return true iff the function found the symbol from the elf hash
2137 /// table.
2138 static bool
2139 lookup_symbol_from_elf_hash_tab(const environment*              env,
2140                                 Elf*                            elf_handle,
2141                                 hash_table_kind         ht_kind,
2142                                 size_t                          ht_index,
2143                                 size_t                          symtab_index,
2144                                 const string&                   symbol_name,
2145                                 bool                            demangle,
2146                                 vector<elf_symbol_sptr>&        syms_found)
2147 {
2148   if (elf_handle == 0 || symbol_name.empty())
2149     return false;
2150
2151   if (ht_kind == NO_HASH_TABLE_KIND)
2152     return false;
2153
2154   if (ht_kind == SYSV_HASH_TABLE_KIND)
2155     return lookup_symbol_from_sysv_hash_tab(env,
2156                                             elf_handle, symbol_name,
2157                                             ht_index,
2158                                             symtab_index,
2159                                             demangle,
2160                                             syms_found);
2161   else if (ht_kind == GNU_HASH_TABLE_KIND)
2162     return lookup_symbol_from_gnu_hash_tab(env,
2163                                            elf_handle, symbol_name,
2164                                            ht_index,
2165                                            symtab_index,
2166                                            demangle,
2167                                            syms_found);
2168   return false;
2169 }
2170
2171 /// Lookup a symbol from the symbol table directly.
2172 ///
2173 ///
2174 /// @param env the environment we are operating from.
2175 ///
2176 /// @param elf_handle the elf handle to use.
2177 ///
2178 /// @param sym_name the name of the symbol to look up.
2179 ///
2180 /// @param sym_tab_index the index (in the section headers table) of
2181 /// the symbol table section.
2182 ///
2183 /// @param demangle if true, demangle the names found in the symbol
2184 /// table before comparing them with @p sym_name.
2185 ///
2186 /// @param sym_name_found the actual name of the symbol found.
2187 ///
2188 /// @param sym_type the type of the symbol found.
2189 ///
2190 /// @param sym_binding the binding of the symbol found.
2191 ///
2192 /// @param sym_versions the versions of the symbol found.
2193 ///
2194 /// @return true iff the symbol was found.
2195 static bool
2196 lookup_symbol_from_symtab(const environment*            env,
2197                           Elf*                          elf_handle,
2198                           const string&         sym_name,
2199                           size_t                        sym_tab_index,
2200                           bool                          demangle,
2201                           vector<elf_symbol_sptr>&      syms_found)
2202 {
2203   // TODO: read all of the symbol table, store it in memory in a data
2204   // structure that associates each symbol with its versions and in
2205   // which lookups of a given symbol is fast.
2206   Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
2207   ABG_ASSERT(sym_tab_section);
2208
2209   GElf_Shdr header_mem;
2210   GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
2211                                             &header_mem);
2212
2213   size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
2214   Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
2215   GElf_Sym* sym;
2216   char* name_str = 0;
2217   elf_symbol::version ver;
2218   bool found = false;
2219   Elf_Scn *strings_section = find_ksymtab_strings_section(elf_handle);
2220   size_t strings_ndx = strings_section
2221     ? elf_ndxscn(strings_section)
2222     : 0;
2223
2224   for (size_t i = 0; i < symcount; ++i)
2225     {
2226       GElf_Sym sym_mem;
2227       sym = gelf_getsym(symtab, i, &sym_mem);
2228       name_str = elf_strptr(elf_handle,
2229                             sym_tab_header->sh_link,
2230                             sym->st_name);
2231
2232       if (name_str && compare_symbol_name(name_str, sym_name, demangle))
2233         {
2234           elf_symbol::type sym_type =
2235             stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
2236           elf_symbol::binding sym_binding =
2237             stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
2238           elf_symbol::visibility sym_visibility =
2239             stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
2240           bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
2241           bool sym_is_common = sym->st_shndx == SHN_COMMON;
2242
2243           if (get_version_for_symbol(elf_handle, i,
2244                                      /*get_def_version=*/sym_is_defined,
2245                                      ver))
2246             ABG_ASSERT(!ver.str().empty());
2247           elf_symbol_sptr symbol_found =
2248             elf_symbol::create(env, i, sym->st_size,
2249                                name_str, sym_type,
2250                                sym_binding, sym_is_defined,
2251                                sym_is_common, ver, sym_visibility,
2252                                sym->st_shndx == strings_ndx);
2253           syms_found.push_back(symbol_found);
2254           found = true;
2255         }
2256     }
2257
2258   if (found)
2259     return true;
2260
2261   return false;
2262 }
2263
2264 /// Look into the symbol tables of the underlying elf file and see
2265 /// if we find a given symbol.
2266 ///
2267 /// @param env the environment we are operating from.
2268 ///
2269 /// @param symbol_name the name of the symbol to look for.
2270 ///
2271 /// @param demangle if true, try to demangle the symbol name found in
2272 /// the symbol table before comparing it to @p symbol_name.
2273 ///
2274 /// @param syms_found the list of symbols found, with the name @p
2275 /// symbol_name.
2276 ///
2277 /// @param sym_type this is set to the type of the symbol found.  This
2278 /// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
2279 /// STT_FUNC, STT_IFUNC, etc ...
2280 ///
2281 /// Note that this parameter is set iff the function returns true.
2282 ///
2283 /// @param sym_binding this is set to the binding of the symbol found.
2284 /// This is a standard elf.h value of the symbol binding kind, that
2285 /// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
2286 ///
2287 /// @param symbol_versions the versions of the symbol @p symbol_name,
2288 /// if it was found.
2289 ///
2290 /// @return true iff a symbol with the name @p symbol_name was found.
2291 static bool
2292 lookup_symbol_from_elf(const environment*               env,
2293                        Elf*                             elf_handle,
2294                        const string&                    symbol_name,
2295                        bool                             demangle,
2296                        vector<elf_symbol_sptr>& syms_found)
2297 {
2298   size_t hash_table_index = 0, symbol_table_index = 0;
2299   hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
2300
2301   if (!demangle)
2302     ht_kind = find_hash_table_section_index(elf_handle,
2303                                             hash_table_index,
2304                                             symbol_table_index);
2305
2306   if (ht_kind == NO_HASH_TABLE_KIND)
2307     {
2308       if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
2309         return false;
2310
2311       return lookup_symbol_from_symtab(env,
2312                                        elf_handle,
2313                                        symbol_name,
2314                                        symbol_table_index,
2315                                        demangle,
2316                                        syms_found);
2317     }
2318
2319   return lookup_symbol_from_elf_hash_tab(env,
2320                                          elf_handle,
2321                                          ht_kind,
2322                                          hash_table_index,
2323                                          symbol_table_index,
2324                                          symbol_name,
2325                                          demangle,
2326                                          syms_found);
2327 }
2328
2329 /// Look into the symbol tables of the underlying elf file and see if
2330 /// we find a given public (global or weak) symbol of function type.
2331 ///
2332 /// @param env the environment we are operating from.
2333 ///
2334 /// @param elf_handle the elf handle to use for the query.
2335 ///
2336 /// @param symbol_name the function symbol to look for.
2337 ///
2338 /// @param func_syms the vector of public functions symbols found, if
2339 /// any.
2340 ///
2341 /// @return true iff the symbol was found.
2342 static bool
2343 lookup_public_function_symbol_from_elf(const environment*               env,
2344                                        Elf*                             elf_handle,
2345                                        const string&                    symbol_name,
2346                                        vector<elf_symbol_sptr>& func_syms)
2347 {
2348   vector<elf_symbol_sptr> syms_found;
2349   bool found = false;
2350
2351   if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
2352                              /*demangle=*/false, syms_found))
2353     {
2354       for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
2355            i != syms_found.end();
2356            ++i)
2357         {
2358           elf_symbol::type type = (*i)->get_type();
2359           elf_symbol::binding binding = (*i)->get_binding();
2360
2361           if ((type == elf_symbol::FUNC_TYPE
2362                || type == elf_symbol::GNU_IFUNC_TYPE
2363                || type == elf_symbol::COMMON_TYPE)
2364               && (binding == elf_symbol::GLOBAL_BINDING
2365                   || binding == elf_symbol::WEAK_BINDING))
2366             {
2367               func_syms.push_back(*i);
2368               found = true;
2369             }
2370         }
2371     }
2372
2373   return found;
2374 }
2375
2376 /// Look into the symbol tables of the underlying elf file and see if
2377 /// we find a given public (global or weak) symbol of variable type.
2378 ///
2379 /// @param env the environment we are operating from.
2380 ///
2381 /// @param elf the elf handle to use for the query.
2382 ///
2383 /// @param symname the variable symbol to look for.
2384 ///
2385 /// @param var_syms the vector of public variable symbols found, if any.
2386 ///
2387 /// @return true iff symbol @p symname was found.
2388 static bool
2389 lookup_public_variable_symbol_from_elf(const environment*               env,
2390                                        Elf*                             elf,
2391                                        const string&                    symname,
2392                                        vector<elf_symbol_sptr>& var_syms)
2393 {
2394   vector<elf_symbol_sptr> syms_found;
2395   bool found = false;
2396
2397   if (lookup_symbol_from_elf(env, elf, symname, /*demangle=*/false, syms_found))
2398     {
2399       for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
2400            i != syms_found.end();
2401            ++i)
2402         if ((*i)->is_variable()
2403             && ((*i)->get_binding() == elf_symbol::GLOBAL_BINDING
2404                 || (*i)->get_binding() == elf_symbol::WEAK_BINDING))
2405           {
2406             var_syms.push_back(*i);
2407             found = true;
2408           }
2409     }
2410
2411   return found;
2412 }
2413
2414 /// Get data tag information of an ELF file by looking up into its
2415 /// dynamic segment
2416 ///
2417 /// @param elf the elf handle to use for the query.
2418 ///
2419 /// @param dt_tag data tag to look for in dynamic segment
2420 /// @param dt_tag_data vector of found information for a given @p data_tag
2421 ///
2422 /// @return true iff data tag @p data_tag was found
2423
2424 bool
2425 lookup_data_tag_from_dynamic_segment(Elf*                       elf,
2426                                      Elf64_Sxword               data_tag,
2427                                      vector<string>&            dt_tag_data)
2428 {
2429   size_t num_prog_headers = 0;
2430   bool found = false;
2431   if (elf_getphdrnum(elf, &num_prog_headers) < 0)
2432     return found;
2433
2434   // Cycle through each program header.
2435   for (size_t i = 0; i < num_prog_headers; ++i)
2436     {
2437       GElf_Phdr phdr_mem;
2438       GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
2439       if (phdr == NULL || phdr->p_type != PT_DYNAMIC)
2440         continue;
2441
2442       // Poke at the dynamic segment like a section, so that we can
2443       // get its section header information; also we'd like to read
2444       // the data of the segment by using elf_getdata() but that
2445       // function needs a Elf_Scn data structure to act on.
2446       // Elfutils doesn't really have any particular function to
2447       // access segment data, other than the functions used to
2448       // access section data.
2449       Elf_Scn *dynamic_section = gelf_offscn(elf, phdr->p_offset);
2450       GElf_Shdr  shdr_mem;
2451       GElf_Shdr *dynamic_section_header = gelf_getshdr(dynamic_section,
2452                                                        &shdr_mem);
2453       if (dynamic_section_header == NULL
2454           || dynamic_section_header->sh_type != SHT_DYNAMIC)
2455         continue;
2456
2457       // Get data of the dynamic segment (seen as a section).
2458       Elf_Data *data = elf_getdata(dynamic_section, NULL);
2459       if (data == NULL)
2460         continue;
2461
2462       // Get the index of the section headers string table.
2463       size_t string_table_index = 0;
2464       ABG_ASSERT (elf_getshdrstrndx(elf, &string_table_index) >= 0);
2465
2466       size_t dynamic_section_header_entry_size = gelf_fsize(elf,
2467                                                             ELF_T_DYN, 1,
2468                                                             EV_CURRENT);
2469
2470       GElf_Shdr link_mem;
2471       GElf_Shdr *link =
2472         gelf_getshdr(elf_getscn(elf,
2473                                 dynamic_section_header->sh_link),
2474                      &link_mem);
2475       ABG_ASSERT(link != NULL);
2476
2477       size_t num_dynamic_section_entries =
2478         dynamic_section_header->sh_size / dynamic_section_header_entry_size;
2479
2480       // Now walk through all the DT_* data tags that are in the
2481       // segment/section
2482       for (size_t j = 0; j < num_dynamic_section_entries; ++j)
2483         {
2484           GElf_Dyn dynamic_section_mem;
2485           GElf_Dyn *dynamic_section = gelf_getdyn(data,
2486                                                   j,
2487                                                   &dynamic_section_mem);
2488           if (dynamic_section->d_tag == data_tag)
2489             {
2490               dt_tag_data.push_back(elf_strptr(elf,
2491                                                dynamic_section_header->sh_link,
2492                                                dynamic_section->d_un.d_val));
2493               found = true;
2494             }
2495         }
2496     }
2497   return found;
2498 }
2499
2500 /// Convert the type of ELF file into @ref elf_type.
2501 ///
2502 /// @param elf the elf handle to use for the query.
2503 ///
2504 /// @return the @ref elf_type for a given elf type.
2505 static elf_type
2506 elf_file_type(Elf* elf)
2507 {
2508   GElf_Ehdr ehdr_mem;
2509   GElf_Ehdr *header = gelf_getehdr (elf, &ehdr_mem);
2510   vector<string> dt_debug_data;
2511
2512   switch (header->e_type)
2513     {
2514     case ET_DYN:
2515       if (lookup_data_tag_from_dynamic_segment(elf, DT_DEBUG, dt_debug_data))
2516         return ELF_TYPE_PI_EXEC;
2517       else
2518         return ELF_TYPE_DSO;
2519     case ET_EXEC:
2520       return ELF_TYPE_EXEC;
2521     case ET_REL:
2522       return ELF_TYPE_RELOCATABLE;
2523     default:
2524       return ELF_TYPE_UNKNOWN;
2525     }
2526 }
2527
2528 // ---------------------------------------
2529 // <location expression evaluation types>
2530 // ---------------------------------------
2531
2532 /// An abstraction of a value representing the result of the
2533 /// evaluation of a dwarf expression.  This is abstraction represents
2534 /// a partial view on the possible values because we are only
2535 /// interested in extracting the latest and longuest constant
2536 /// sub-expression of a given dwarf expression.
2537 class expr_result
2538 {
2539   bool is_const_;
2540   int64_t const_value_;
2541
2542 public:
2543   expr_result()
2544     : is_const_(true),
2545       const_value_(0)
2546   {}
2547
2548   expr_result(bool is_const)
2549     : is_const_(is_const),
2550       const_value_(0)
2551   {}
2552
2553   explicit expr_result(int64_t v)
2554     :is_const_(true),
2555      const_value_(v)
2556   {}
2557
2558   /// @return true if the value is a constant.  Otherwise, return
2559   /// false, meaning the value represents a quantity for which we need
2560   /// inferior (a running program) state to determine the value.
2561   bool
2562   is_const() const
2563   {return is_const_;}
2564
2565
2566   /// @param f a flag saying if the value is set to a constant or not.
2567   void
2568   is_const(bool f)
2569   {is_const_ = f;}
2570
2571   /// Get the current constant value iff this represents a
2572   /// constant.
2573   ///
2574   /// @param value the out parameter.  Is set to the constant value of
2575   /// the @ref expr_result.  This is set iff the function return true.
2576   ///
2577   ///@return true if this has a constant value, false otherwise.
2578   bool
2579   const_value(int64_t& value)
2580   {
2581     if (is_const())
2582       {
2583         value = const_value_;
2584         return true;
2585       }
2586     return false;
2587   }
2588
2589   /// Getter of the constant value of the current @ref expr_result.
2590   ///
2591   /// Note that the current @ref expr_result must be constant,
2592   /// otherwise the current process is aborted.
2593   ///
2594   /// @return the constant value of the current @ref expr_result.
2595   int64_t
2596   const_value() const
2597   {
2598     ABG_ASSERT(is_const());
2599     return const_value_;
2600   }
2601
2602   operator int64_t() const
2603   {return const_value();}
2604
2605   expr_result&
2606   operator=(const int64_t v)
2607   {
2608     const_value_ = v;
2609     return *this;
2610   }
2611
2612   bool
2613   operator==(const expr_result& o) const
2614   {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
2615
2616   bool
2617   operator>=(const expr_result& o) const
2618   {return const_value_ >= o.const_value_;}
2619
2620   bool
2621   operator<=(const expr_result& o) const
2622   {return const_value_ <= o.const_value_;}
2623
2624   bool
2625   operator>(const expr_result& o) const
2626   {return const_value_ > o.const_value_;}
2627
2628   bool
2629   operator<(const expr_result& o) const
2630   {return const_value_ < o.const_value_;}
2631
2632   expr_result
2633   operator+(const expr_result& v) const
2634   {
2635     expr_result r(*this);
2636     r.const_value_ += v.const_value_;
2637     r.is_const_ = r.is_const_ && v.is_const_;
2638     return r;
2639   }
2640
2641   expr_result&
2642   operator+=(int64_t v)
2643   {
2644     const_value_ += v;
2645     return *this;
2646   }
2647
2648   expr_result
2649   operator-(const expr_result& v) const
2650   {
2651     expr_result r(*this);
2652     r.const_value_ -= v.const_value_;
2653     r.is_const_ = r.is_const_ && v.is_const_;
2654     return r;
2655   }
2656
2657   expr_result
2658   operator%(const expr_result& v) const
2659   {
2660     expr_result r(*this);
2661     r.const_value_ %= v.const_value_;
2662     r.is_const_ = r.is_const_ && v.is_const();
2663     return r;
2664   }
2665
2666   expr_result
2667   operator*(const expr_result& v) const
2668   {
2669     expr_result r(*this);
2670     r.const_value_ *= v.const_value_;
2671     r.is_const_ = r.is_const_ && v.is_const();
2672     return r;
2673   }
2674
2675   expr_result
2676   operator|(const expr_result& v) const
2677   {
2678     expr_result r(*this);
2679     r.const_value_ |= v.const_value_;
2680     r.is_const_ = r.is_const_ && v.is_const_;
2681     return r;
2682   }
2683
2684   expr_result
2685   operator^(const expr_result& v) const
2686   {
2687     expr_result r(*this);
2688     r.const_value_ ^= v.const_value_;
2689     r.is_const_ = r.is_const_ && v.is_const_;
2690     return r;
2691   }
2692
2693   expr_result
2694   operator>>(const expr_result& v) const
2695   {
2696     expr_result r(*this);
2697     r.const_value_ = r.const_value_ >> v.const_value_;
2698     r.is_const_ = r.is_const_ && v.is_const_;
2699     return r;
2700   }
2701
2702   expr_result
2703   operator<<(const expr_result& v) const
2704   {
2705     expr_result r(*this);
2706     r.const_value_ = r.const_value_ << v.const_value_;
2707     r.is_const_ = r.is_const_ && v.is_const_;
2708     return r;
2709   }
2710
2711   expr_result
2712   operator~() const
2713   {
2714     expr_result r(*this);
2715     r.const_value_ = ~r.const_value_;
2716     return r;
2717   }
2718
2719   expr_result
2720   neg() const
2721   {
2722     expr_result r(*this);
2723     r.const_value_ = -r.const_value_;
2724     return r;
2725   }
2726
2727   expr_result
2728   abs() const
2729   {
2730     expr_result r = *this;
2731     r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
2732     return r;
2733   }
2734
2735   expr_result
2736   operator&(const expr_result& o)
2737   {
2738     expr_result r(*this);
2739     r.const_value_ &= o.const_value_;
2740     r.is_const_ = r.is_const_ && o.is_const_;
2741     return r;
2742   }
2743
2744   expr_result
2745   operator/(const expr_result& o)
2746   {
2747     expr_result r(*this);
2748     r.is_const_ = r.is_const_ && o.is_const_;
2749     return r.const_value() / o.const_value();
2750   }
2751 };// class end expr_result;
2752
2753 /// A class that implements a stack of @ref expr_result, to be used in
2754 /// the engine evaluating DWARF expressions.
2755 class expr_result_stack_type
2756 {
2757   vector<expr_result> elems_;
2758
2759 public:
2760
2761   expr_result_stack_type()
2762   {elems_.reserve(4);}
2763
2764   expr_result&
2765   operator[](unsigned i)
2766   {
2767     unsigned s = elems_.size();
2768     ABG_ASSERT(s > i);
2769     return elems_[s - 1 -i];
2770   }
2771
2772   const expr_result&
2773   operator[](unsigned i) const
2774   {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
2775
2776   unsigned
2777   size() const
2778   {return elems_.size();}
2779
2780   vector<expr_result>::reverse_iterator
2781   begin()
2782   {return elems_.rbegin();}
2783
2784   const vector<expr_result>::reverse_iterator
2785   begin() const
2786   {return const_cast<expr_result_stack_type*>(this)->begin();}
2787
2788   vector<expr_result>::reverse_iterator
2789   end()
2790   {return elems_.rend();}
2791
2792   const vector<expr_result>::reverse_iterator
2793   end() const
2794   {return const_cast<expr_result_stack_type*>(this)->end();}
2795
2796   expr_result&
2797   front()
2798   {return elems_.back();}
2799
2800   const expr_result&
2801   front() const
2802   {return const_cast<expr_result_stack_type*>(this)->front();}
2803
2804   void
2805   push_front(expr_result e)
2806   {elems_.push_back(e);}
2807
2808   expr_result
2809   pop_front()
2810   {
2811     expr_result r = front();
2812     elems_.pop_back();
2813     return r;
2814   }
2815
2816   void
2817   erase(vector<expr_result>::reverse_iterator i)
2818   {elems_.erase(--i.base());}
2819
2820   void
2821   clear()
2822   {elems_.clear();}
2823 }; // end class expr_result_stack_type
2824
2825 /// Abstraction of the evaluation context of a dwarf expression.
2826 struct dwarf_expr_eval_context
2827 {
2828   expr_result accum;
2829   expr_result_stack_type stack;
2830   // Is set to true if the result of the expression that got evaluated
2831   // is a TLS address.
2832   bool set_tls_addr;
2833
2834   dwarf_expr_eval_context()
2835     : accum(/*is_const=*/false),
2836       set_tls_addr(false)
2837   {
2838     stack.push_front(expr_result(true));
2839   }
2840
2841   void
2842   reset()
2843   {
2844     stack.clear();
2845     stack.push_front(expr_result(true));
2846     accum = expr_result(false);
2847     set_tls_addr = false;
2848   }
2849
2850   /// Set a flag to to tell that the result of the expression that got
2851   /// evaluated is a TLS address.
2852   ///
2853   /// @param f true iff the result of the expression that got
2854   /// evaluated is a TLS address, false otherwise.
2855   void
2856   set_tls_address(bool f)
2857   {set_tls_addr = f;}
2858
2859   /// Getter for the flag that tells if the result of the expression
2860   /// that got evaluated is a TLS address.
2861   ///
2862   /// @return true iff the result of the expression that got evaluated
2863   /// is a TLS address.
2864   bool
2865   set_tls_address() const
2866   {return set_tls_addr;}
2867
2868   expr_result
2869   pop()
2870   {
2871     expr_result r = stack.front();
2872     stack.pop_front();
2873     return r;
2874   }
2875
2876   void
2877   push(const expr_result& v)
2878   {stack.push_front(v);}
2879 };//end class dwarf_expr_eval_context
2880
2881 // ---------------------------------------
2882 // </location expression evaluation types>
2883 // ---------------------------------------
2884
2885 /// An enum for the diffent kinds of linux kernel specific symbol
2886 /// tables.
2887 enum kernel_symbol_table_kind
2888 {
2889   /// This is for an undefined kind of kernel symbol table.
2890   KERNEL_SYMBOL_TABLE_KIND_UNDEFINED,
2891
2892   /// The __ksymtab symbol table.
2893   KERNEL_SYMBOL_TABLE_KIND_KSYMTAB,
2894
2895   /// The __ksymtab_gpl symbol table.
2896   KERNEL_SYMBOL_TABLE_KIND_KSYMTAB_GPL
2897 };
2898
2899 /// An enum which specifies the format of the kernel symbol table
2900 /// (__ksymtab or __ksymtab_gpl).
2901 enum ksymtab_format
2902 {
2903   /// This enumerator means that no __ksymtab format has been
2904   /// determined yet.
2905   UNDEFINED_KSYMTAB_FORMAT,
2906
2907   /// Before Linux v4.19, the format of the __ksymtab (and the
2908   /// __ksymtab_gpl) section was the following.
2909   ///
2910   /// It's an array of entries.  Each entry describes a symbol.  Each
2911   /// entry is made of two words.  each is of the word size of the
2912   /// architecture. (8-bytes on a 64 bits arch and 4-bytes on a 32
2913   /// bits arch) The first word is the address of a symbol.  The
2914   /// second one is the address of a static global variable symbol
2915   /// which value is the string representing the symbol name.  That
2916   /// string is in the __ksymtab_strings section.
2917   ///
2918   /// So we are mostly interested in the symbol address part of each
2919   /// entry.
2920   ///
2921   /// Thus this enumerator means that we have the pre v4.19 __ksymtab
2922   /// section format.
2923   PRE_V4_19_KSYMTAB_FORMAT,
2924
2925   /// Since, Linux v4.19, the format of the __ksymtab section has
2926   /// changed.  The commit that changed is
2927   /// https://github.com/torvalds/linux/commit/7290d58095712a89f845e1bca05334796dd49ed2.
2928   ///
2929   /// The __ksymtab and __ksymtab_gpl sections each are an array of
2930   /// entries.  Each entry describes a symbol.  Each entry is made of
2931   /// two words.  Each word is 4-bytes length.  The first word is the
2932   /// 'place-relative' address of a symbol.  The second one is the
2933   /// 'place-relative' address of a static global variable symbol
2934   /// which value is the string representing the symbol name.  That
2935   /// string is in the __ksymtab_strings section.
2936   ///
2937   /// Below is the description of what a "place-relative address"
2938   /// means.  For that, we are going to define the meaning of four
2939   /// values: 'N', 'S', 'O', and 'A'.
2940   ///
2941   /// *** 'N' and '0' ***
2942   /// Suppose 'N' is the value of the number stored at offset 'O' (big
2943   /// oh, not zero) in the __ksymtab section.
2944   ///
2945   /// *** 'S'***
2946   /// That N designates a symbol in the symtab section which value is
2947   /// S.  So S is the symbol value (in the .symtab symbol table)
2948   /// referred to by the number N found at offset 'O'.
2949   ///
2950   /// *** 'A' ***
2951   /// Also, suppose the __ksymtab section will be loaded at memory
2952   /// address A, as indicated by the 'address' field of the section
2953   /// header describing the __ksymtab section.
2954   ///
2955   /// So here is the formula that gives us S, from N:
2956   ///
2957   ///     S = N + O + A.
2958   ///
2959   /// Storing addresses this way does away with the need to have
2960   /// relocations for the __ksymtab section.  So in effect, vmlinux
2961   /// binaries implementing this new format of __ksymtab won't have
2962   /// any .rela__ksymtab relocation section for the __ksymtab section
2963   /// in particular (nor any relocation section at all).
2964   ///
2965   ///
2966   /// Note that we are mostly interested in the symbol address part of
2967   /// each entry.
2968   V4_19_KSYMTAB_FORMAT
2969 }; // end enum ksymtab_format
2970
2971 /// The context used to build ABI corpus from debug info in DWARF
2972 /// format.
2973 ///
2974 /// This context is to be created by create_read_context().  It's then
2975 /// passed to all the routines that read specific dwarf bits as they
2976 /// get some important data from it.
2977 ///
2978 /// When a new data member is added to this context, it must be
2979 /// initiliazed by the read_context::initiliaze() function.  So please
2980 /// do not forget.
2981 class read_context
2982 {
2983 public:
2984   struct options_type
2985   {
2986     environment*        env;
2987     bool                load_in_linux_kernel_mode;
2988     bool                load_all_types;
2989     bool                ignore_symbol_table;
2990     bool                show_stats;
2991     bool                do_log;
2992
2993     options_type()
2994       : env(),
2995         load_in_linux_kernel_mode(),
2996         load_all_types(),
2997         ignore_symbol_table(),
2998         show_stats(),
2999         do_log()
3000     {}
3001   };// read_context::options_type
3002
3003   /// A set of containers that contains one container per kind of @ref
3004   /// die_source.  This allows to associate DIEs to things, depending
3005   /// on the source of the DIE.
3006   template <typename ContainerType>
3007   class die_source_dependant_container_set
3008   {
3009     ContainerType primary_debug_info_container_;
3010     ContainerType alt_debug_info_container_;
3011     ContainerType type_unit_container_;
3012
3013   public:
3014
3015     /// Getter for the container associated to DIEs coming from a
3016     /// given @ref die_source.
3017     ///
3018     /// @param source the die_source for which we want the container.
3019     ///
3020     /// @return the container that associates DIEs coming from @p
3021     /// source to something.
3022     ContainerType&
3023     get_container(die_source source)
3024     {
3025       ContainerType *result = 0;
3026       switch (source)
3027         {
3028         case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3029           result = &primary_debug_info_container_;
3030           break;
3031         case ALT_DEBUG_INFO_DIE_SOURCE:
3032           result = &alt_debug_info_container_;
3033           break;
3034         case TYPE_UNIT_DIE_SOURCE:
3035           result = &type_unit_container_;
3036           break;
3037         case NO_DEBUG_INFO_DIE_SOURCE:
3038         case NUMBER_OF_DIE_SOURCES:
3039           ABG_ASSERT_NOT_REACHED;
3040         }
3041       return *result;
3042     }
3043
3044     /// Getter for the container associated to DIEs coming from a
3045     /// given @ref die_source.
3046     ///
3047     /// @param source the die_source for which we want the container.
3048     ///
3049     /// @return the container that associates DIEs coming from @p
3050     /// source to something.
3051     const ContainerType&
3052     get_container(die_source source) const
3053     {
3054       return const_cast<die_source_dependant_container_set*>(this)->
3055         get_container(source);
3056     }
3057
3058     /// Getter for the container associated to DIEs coming from the
3059     /// same source as a given DIE.
3060     ///
3061     /// @param ctxt the read context to consider.
3062     ///
3063     /// @param die the DIE which should have the same source as the
3064     /// source of the container we want.
3065     ///
3066     /// @return the container that associates DIEs coming from the
3067     /// same source as @p die.
3068     ContainerType&
3069     get_container(const read_context& ctxt, const Dwarf_Die *die)
3070     {
3071       die_source source = NO_DEBUG_INFO_DIE_SOURCE;
3072       ABG_ASSERT(ctxt.get_die_source(die, source));
3073       return get_container(source);
3074     }
3075
3076     /// Getter for the container associated to DIEs coming from the
3077     /// same source as a given DIE.
3078     ///
3079     /// @param ctxt the read context to consider.
3080     ///
3081     /// @param die the DIE which should have the same source as the
3082     /// source of the container we want.
3083     ///
3084     /// @return the container that associates DIEs coming from the
3085     /// same source as @p die.
3086     const ContainerType&
3087     get_container(const read_context& ctxt, const Dwarf_Die *die) const
3088     {
3089       return const_cast<die_source_dependant_container_set*>(this)->
3090         get_container(ctxt, die);
3091     }
3092
3093     /// Clear the container set.
3094     void
3095     clear()
3096     {
3097       primary_debug_info_container_.clear();
3098       alt_debug_info_container_.clear();
3099       type_unit_container_.clear();
3100     }
3101   }; // end die_dependant_container_set
3102
3103   suppr::suppressions_type      supprs_;
3104   unsigned short                dwarf_version_;
3105   Dwfl_Callbacks                offline_callbacks_;
3106   // The set of directories under which to look for debug info.
3107   vector<char**>                debug_info_root_paths_;
3108   dwfl_sptr                     handle_;
3109   Dwarf*                        dwarf_;
3110   // The alternate debug info.  Alternate debug info sections are a
3111   // DWARF extension as of DWARF4 and are described at
3112   // http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.  Below are
3113   // the file desctor used to access the alternate debug info
3114   // sections, and the representation of the DWARF debug info.  Both
3115   // need to be freed after we are done using them, with fclose and
3116   // dwarf_end.
3117   int                           alt_fd_;
3118   Dwarf*                        alt_dwarf_;
3119   string                        alt_debug_info_path_;
3120   // The address range of the offline elf file we are looking at.
3121   Dwfl_Module*                  elf_module_;
3122   mutable Elf*                  elf_handle_;
3123   string                        elf_path_;
3124   mutable Elf_Scn*              bss_section_;
3125   mutable Elf_Scn*              text_section_;
3126   mutable Elf_Scn*              rodata_section_;
3127   mutable Elf_Scn*              data_section_;
3128   mutable Elf_Scn*              data1_section_;
3129   mutable Elf_Scn*              symtab_section_;
3130   // The "Official procedure descriptor section, aka .opd", used in
3131   // ppc64 elf v1 binaries.  This section contains the procedure
3132   // descriptors on that platform.
3133   Elf_Scn*                      opd_section_;
3134   /// The format of the special __ksymtab section from the linux
3135   /// kernel binary.
3136   mutable ksymtab_format        ksymtab_format_;
3137   /// The size of one entry of the __ksymtab section.
3138   mutable size_t                ksymtab_entry_size_;
3139   /// The number of entries in the __ksymtab section.
3140   mutable size_t                nb_ksymtab_entries_;
3141   /// The number of entries in the __ksymtab_gpl section.
3142   mutable size_t                nb_ksymtab_gpl_entries_;
3143   /// The special __ksymtab and __ksymtab_gpl sections from linux
3144   /// kernel or module binaries.  The former is used to store
3145   /// references to symbols exported using the EXPORT_SYMBOL macro
3146   /// from the linux kernel.  The latter is used to store references
3147   /// to symbols exported using the EXPORT_SYMBOL_GPL macro from the
3148   /// linux kernel.
3149   Elf_Scn*                      ksymtab_section_;
3150   Elf_Scn*                      ksymtab_reloc_section_;
3151   Elf_Scn*                      ksymtab_gpl_section_;
3152   Elf_Scn*                      ksymtab_gpl_reloc_section_;
3153   Elf_Scn*                      ksymtab_strings_section_;
3154   Elf_Scn*                      versym_section_;
3155   Elf_Scn*                      verdef_section_;
3156   Elf_Scn*                      verneed_section_;
3157   bool                          symbol_versionning_sections_loaded_;
3158   bool                          symbol_versionning_sections_found_;
3159   Dwarf_Die*                    cur_tu_die_;
3160   mutable dwarf_expr_eval_context       dwarf_expr_eval_context_;
3161   // A set of maps (one per kind of die source) that associates a decl
3162   // string representation with the DIEs (offsets) representing that
3163   // decl.
3164   mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
3165   decl_die_repr_die_offsets_maps_;
3166   // A set of maps (one per kind of die source) that associates a type
3167   // string representation with the DIEs (offsets) representing that
3168   // type.
3169   mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
3170   type_die_repr_die_offsets_maps_;
3171   mutable die_source_dependant_container_set<die_istring_map_type>
3172   die_qualified_name_maps_;
3173   mutable die_source_dependant_container_set<die_istring_map_type>
3174   die_pretty_repr_maps_;
3175   mutable die_source_dependant_container_set<die_istring_map_type>
3176   die_pretty_type_repr_maps_;
3177   // A set of maps (one per kind of die source) that associates the
3178   // offset of a decl die to its corresponding decl artifact.
3179   mutable die_source_dependant_container_set<die_artefact_map_type>
3180   decl_die_artefact_maps_;
3181   // A set of maps (one per kind of die source) that associates the
3182   // offset of a type die to its corresponding type artifact.
3183   mutable die_source_dependant_container_set<die_artefact_map_type>
3184   type_die_artefact_maps_;
3185   /// A set of vectors (one per kind of die source) that associates
3186   /// the offset of a type DIE to the offset of its canonical DIE.
3187   mutable die_source_dependant_container_set<offset_offset_map_type>
3188   canonical_type_die_offsets_;
3189   /// A set of vectors (one per kind of die source) that associates
3190   /// the offset of a decl DIE to the offset of its canonical DIE.
3191   mutable die_source_dependant_container_set<offset_offset_map_type>
3192   canonical_decl_die_offsets_;
3193   /// A map that associates a function type representations to
3194   /// function types, inside a translation unit.
3195   mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
3196
3197   die_class_or_union_map_type   die_wip_classes_map_;
3198   die_class_or_union_map_type   alternate_die_wip_classes_map_;
3199   die_class_or_union_map_type   type_unit_die_wip_classes_map_;
3200   die_function_type_map_type    die_wip_function_types_map_;
3201   die_function_type_map_type    alternate_die_wip_function_types_map_;
3202   die_function_type_map_type    type_unit_die_wip_function_types_map_;
3203   die_function_decl_map_type    die_function_with_no_symbol_map_;
3204   vector<Dwarf_Off>             types_to_canonicalize_;
3205   vector<Dwarf_Off>             alt_types_to_canonicalize_;
3206   vector<Dwarf_Off>             type_unit_types_to_canonicalize_;
3207   vector<type_base_sptr>        extra_types_to_canonicalize_;
3208   string_classes_map            decl_only_classes_map_;
3209   die_tu_map_type               die_tu_map_;
3210   corpus_group_sptr             cur_corpus_group_;
3211   corpus_sptr                   cur_corpus_;
3212   translation_unit_sptr cur_tu_;
3213   scope_decl_sptr               nil_scope_;
3214   scope_stack_type              scope_stack_;
3215   offset_offset_map_type        primary_die_parent_map_;
3216   // A map that associates each tu die to a vector of unit import
3217   // points, in the main debug info
3218   tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
3219   // A map that associates each tu die to a vector of unit import
3220   // points, in the alternate debug info
3221   tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
3222   tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
3223   // A DIE -> parent map for DIEs coming from the alternate debug info
3224   // file.
3225   offset_offset_map_type        alternate_die_parent_map_;
3226   offset_offset_map_type        type_section_die_parent_map_;
3227   list<var_decl_sptr>           var_decls_to_add_;
3228   addr_elf_symbol_sptr_map_sptr fun_addr_sym_map_;
3229   // On PPC64, the function entry point address is different from the
3230   // GElf_Sym::st_value value, which is the address of the descriptor
3231   // of the function.  The map below thus associates the address of
3232   // the entry point to the function symbol.  If we are not on ppc64,
3233   // then this map ought to be empty.  Only the fun_addr_sym_map_ is
3234   // used in that case.  On ppc64, though, both maps are used.
3235   addr_elf_symbol_sptr_map_sptr fun_entry_addr_sym_map_;
3236   string_elf_symbols_map_sptr   fun_syms_;
3237   addr_elf_symbol_sptr_map_sptr var_addr_sym_map_;
3238   string_elf_symbols_map_sptr   var_syms_;
3239   string_elf_symbols_map_sptr   undefined_fun_syms_;
3240   string_elf_symbols_map_sptr   undefined_var_syms_;
3241   address_set_sptr              linux_exported_fn_syms_;
3242   address_set_sptr              linux_exported_var_syms_;
3243   address_set_sptr              linux_exported_gpl_fn_syms_;
3244   address_set_sptr              linux_exported_gpl_var_syms_;
3245   vector<string>                dt_needed_;
3246   string                        dt_soname_;
3247   string                        elf_architecture_;
3248   corpus::exported_decls_builder* exported_decls_builder_;
3249   options_type                  options_;
3250   read_context();
3251
3252 public:
3253
3254   /// Constructor of read_context.
3255   ///
3256   /// @param elf_path the path to the elf file the context is to be
3257   /// used for.
3258   ///
3259   /// @param debug_info_root_paths a vector of pointers to the path to
3260   /// the root directory under which the debug info is to be found for
3261   /// @p elf_path.  Leave this empty if the debug info is not in a
3262   /// split file.
3263   ///
3264   /// @param environment the environment used by the current context.
3265   /// This environment contains resources needed by the reader and by
3266   /// the types and declarations that are to be created later.  Note
3267   /// that ABI artifacts that are to be compared all need to be
3268   /// created within the same environment.
3269   ///
3270   /// Please also note that the life time of this environment object
3271   /// must be greater than the life time of the resulting @ref
3272   /// read_context the context uses resources that are allocated in
3273   /// the environment.
3274   ///
3275   /// @param load_all_types if set to false only the types that are
3276   /// reachable from publicly exported declarations (of functions and
3277   /// variables) are read.  If set to true then all types found in the
3278   /// debug information are loaded.
3279   ///
3280   /// @param linux_kernel_mode if set to true, then consider the special
3281   /// linux kernel symbol tables when determining if a symbol is
3282   /// exported or not.
3283   read_context(const string&    elf_path,
3284                const vector<char**>& debug_info_root_paths,
3285                ir::environment* environment,
3286                bool             load_all_types,
3287                bool             linux_kernel_mode)
3288   {
3289     initialize(elf_path, debug_info_root_paths, environment,
3290                load_all_types, linux_kernel_mode);
3291   }
3292
3293   /// Initializer of read_context.
3294   ///
3295   /// @param elf_path the path to the elf file the context is to be
3296   /// used for.
3297   ///
3298   /// @param debug_info_root_paths a vector of pointers to the path to
3299   /// the root directory under which the debug info is to be found for
3300   /// @p elf_path.  Leave this empty if the debug info is not in a
3301   /// split file.
3302   ///
3303   /// @param environment the environment used by the current context.
3304   /// This environment contains resources needed by the reader and by
3305   /// the types and declarations that are to be created later.  Note
3306   /// that ABI artifacts that are to be compared all need to be
3307   /// created within the same environment.
3308   ///
3309   /// Please also note that the life time of this environment object
3310   /// must be greater than the life time of the resulting @ref
3311   /// read_context the context uses resources that are allocated in
3312   /// the environment.
3313   ///
3314   /// @param load_all_types if set to false only the types that are
3315   /// reachable from publicly exported declarations (of functions and
3316   /// variables) are read.  If set to true then all types found in the
3317   /// debug information are loaded.
3318   ///
3319   /// @param linux_kernel_mode if set to true, then consider the
3320   /// special linux kernel symbol tables when determining if a symbol
3321   /// is exported or not.
3322   void
3323   initialize(const string&      elf_path,
3324              const vector<char**>& debug_info_root_paths,
3325              ir::environment* environment,
3326              bool               load_all_types,
3327              bool               linux_kernel_mode)
3328   {
3329     dwarf_version_ = 0;
3330     dwarf_ = 0;
3331     handle_.reset();
3332     alt_fd_ = 0;
3333     alt_dwarf_ = 0;
3334     elf_module_ = 0;
3335     elf_handle_ = 0;
3336     elf_path_ = elf_path;
3337     bss_section_ = 0;
3338     text_section_ = 0;
3339     rodata_section_ = 0;
3340     data_section_ = 0;
3341     data1_section_ = 0;
3342     symtab_section_ = 0;
3343     opd_section_ = 0;
3344     ksymtab_format_ = UNDEFINED_KSYMTAB_FORMAT;
3345     ksymtab_entry_size_ = 0;
3346     nb_ksymtab_entries_ = 0;
3347     nb_ksymtab_gpl_entries_ = 0;
3348     ksymtab_section_ = 0;
3349     ksymtab_reloc_section_ = 0;
3350     ksymtab_gpl_section_ = 0;
3351     ksymtab_gpl_reloc_section_ = 0;
3352     ksymtab_strings_section_ = 0;
3353     versym_section_ = 0;
3354     verdef_section_ = 0;
3355     verneed_section_ = 0;
3356     symbol_versionning_sections_loaded_ = 0;
3357     symbol_versionning_sections_found_ = 0;
3358     cur_tu_die_ =  0;
3359     exported_decls_builder_ = 0;
3360
3361     clear_alt_debug_info_data();
3362
3363     supprs_.clear();
3364     decl_die_repr_die_offsets_maps_.clear();
3365     type_die_repr_die_offsets_maps_.clear();
3366     die_qualified_name_maps_.clear();
3367     die_pretty_repr_maps_.clear();
3368     die_pretty_type_repr_maps_.clear();
3369     decl_die_artefact_maps_.clear();
3370     type_die_artefact_maps_.clear();
3371     canonical_type_die_offsets_.clear();
3372     canonical_decl_die_offsets_.clear();
3373     die_wip_classes_map_.clear();
3374     alternate_die_wip_classes_map_.clear();
3375     type_unit_die_wip_classes_map_.clear();
3376     die_wip_function_types_map_.clear();
3377     alternate_die_wip_function_types_map_.clear();
3378     type_unit_die_wip_function_types_map_.clear();
3379     die_function_with_no_symbol_map_.clear();
3380     types_to_canonicalize_.clear();
3381     alt_types_to_canonicalize_.clear();
3382     type_unit_types_to_canonicalize_.clear();
3383     extra_types_to_canonicalize_.clear();
3384     decl_only_classes_map_.clear();
3385     die_tu_map_.clear();
3386     cur_corpus_group_.reset();
3387     cur_corpus_.reset();
3388     cur_tu_.reset();
3389     primary_die_parent_map_.clear();
3390     tu_die_imported_unit_points_map_.clear();
3391     alt_tu_die_imported_unit_points_map_.clear();
3392     type_units_tu_die_imported_unit_points_map_.clear();
3393     alternate_die_parent_map_.clear();
3394     type_section_die_parent_map_.clear();
3395     var_decls_to_add_.clear();
3396     fun_addr_sym_map_.reset();
3397     fun_entry_addr_sym_map_.reset();
3398     fun_syms_.reset();
3399     var_addr_sym_map_.reset();
3400     var_syms_.reset();
3401     undefined_fun_syms_.reset();
3402     undefined_var_syms_.reset();
3403     linux_exported_fn_syms_.reset();
3404     linux_exported_var_syms_.reset();
3405     linux_exported_gpl_fn_syms_.reset();
3406     linux_exported_gpl_var_syms_.reset();
3407     dt_needed_.clear();
3408     dt_soname_.clear();
3409     elf_architecture_.clear();
3410
3411     clear_per_translation_unit_data();
3412
3413     memset(&offline_callbacks_, 0, sizeof(offline_callbacks_));
3414     create_default_dwfl(debug_info_root_paths);
3415     options_.env = environment;
3416     options_.load_in_linux_kernel_mode = linux_kernel_mode;
3417     options_.load_all_types = load_all_types;
3418     load_in_linux_kernel_mode(linux_kernel_mode);
3419   }
3420
3421   /// Clear the resources related to the alternate DWARF data.
3422   void
3423   clear_alt_debug_info_data()
3424   {
3425     if (alt_fd_)
3426       {
3427         close(alt_fd_);
3428         alt_fd_ = 0;
3429         if (alt_dwarf_)
3430           {
3431             dwarf_end(alt_dwarf_);
3432             alt_dwarf_ = 0;
3433           }
3434         alt_debug_info_path_.clear();
3435       }
3436   }
3437
3438   /// Detructor of the @ref read_context type.
3439   ~read_context()
3440   {
3441     clear_alt_debug_info_data();
3442   }
3443
3444   /// Clear the data that is relevant only for the current translation
3445   /// unit being read.  The rest of the data is relevant for the
3446   /// entire ABI corpus.
3447   void
3448   clear_per_translation_unit_data()
3449   {
3450     while (!scope_stack().empty())
3451       scope_stack().pop();
3452     var_decls_to_re_add_to_tree().clear();
3453     per_tu_repr_to_fn_type_maps().clear();
3454   }
3455
3456   /// Clear the data that is relevant for the current corpus being
3457   /// read.
3458   void
3459   clear_per_corpus_data()
3460   {
3461     die_qualified_name_maps_.clear();
3462     die_pretty_repr_maps_.clear();
3463     die_pretty_type_repr_maps_.clear();
3464     clear_types_to_canonicalize();
3465   }
3466
3467   /// Getter of the options of the read context.
3468   ///
3469   /// @return the options of the read context.
3470   options_type&
3471   options()
3472   {return options_;}
3473
3474   /// Getter of the options of the read context.
3475   ///
3476   /// @return the options of the read context.
3477   const options_type&
3478   options() const
3479   {return options_;}
3480
3481   /// Getter of the options of the read context.
3482   ///
3483   /// @return the options of the read context.
3484   void
3485   options(const options_type& o)
3486   {options_ = o;}
3487
3488   /// Getter for the current environment.
3489   ///
3490   /// @return the current environment.
3491   const ir::environment*
3492   env() const
3493   {return options_.env;}
3494
3495   /// Getter for the current environment.
3496   ///
3497   /// @return the current environment.
3498   ir::environment*
3499   env()
3500   {return options_.env;}
3501
3502   /// Setter for the current environment.
3503   ///
3504   /// @param env the new current environment.
3505   void
3506   env(ir::environment* env)
3507   {options_.env = env;}
3508
3509   /// Getter of the suppression specifications to be used during
3510   /// ELF/DWARF parsing.
3511   ///
3512   /// @return the suppression specifications.
3513   const suppr::suppressions_type&
3514   get_suppressions() const
3515   {return supprs_;}
3516
3517   /// Getter of the suppression specifications to be used during
3518   /// ELF/DWARF parsing.
3519   ///
3520   /// @return the suppression specifications.
3521   suppr::suppressions_type&
3522   get_suppressions()
3523   {return supprs_;}
3524
3525   /// Getter for the callbacks of the Dwarf Front End library of
3526   /// elfutils that is used by this reader to read dwarf.
3527   ///
3528   /// @return the callbacks.
3529   const Dwfl_Callbacks*
3530   offline_callbacks() const
3531   {return &offline_callbacks_;}
3532
3533   /// Getter for the callbacks of the Dwarf Front End library of
3534   /// elfutils that is used by this reader to read dwarf.
3535   /// @returnthe callbacks
3536   Dwfl_Callbacks*
3537   offline_callbacks()
3538   {return &offline_callbacks_;}
3539
3540   /// Constructor for a default Dwfl handle that knows how to load debug
3541   /// info from a library or executable elf file.
3542   ///
3543   /// @param debug_info_root_paths a vector of pointers to the root
3544   /// path under which to look for the debug info of the elf files
3545   /// that are later handled by the Dwfl.  This is for cases where the
3546   /// debug info is split into a different file from the binary we
3547   /// want to inspect.  On Red Hat compatible systems, this root path
3548   /// is usually /usr/lib/debug by default.  If this argument is set
3549   /// to the empty set, then "./debug" and /usr/lib/debug will be
3550   /// searched for sub-directories containing the debug info file.
3551   /// Note that for now, elfutils wants this path to be absolute
3552   /// otherwise things just don't work and the debug info is not
3553   /// found.
3554   ///
3555   /// @return the constructed Dwfl handle.
3556   void
3557   create_default_dwfl(const vector<char**>& debug_info_root_paths)
3558   {
3559     offline_callbacks()->find_debuginfo = dwfl_standard_find_debuginfo;
3560     offline_callbacks()->section_address = dwfl_offline_section_address;
3561     offline_callbacks()->debuginfo_path =
3562       debug_info_root_paths.empty() ? 0 : debug_info_root_paths.front();
3563     handle_.reset(dwfl_begin(offline_callbacks()),
3564                   dwfl_deleter());
3565     debug_info_root_paths_ = debug_info_root_paths;
3566   }
3567
3568   unsigned short
3569   dwarf_version() const
3570   {return dwarf_version_;}
3571
3572   void
3573   dwarf_version(unsigned short v)
3574   {dwarf_version_ = v;}
3575
3576   /// Getter for a smart pointer to a handle on the dwarf front end
3577   /// library that we use to read dwarf.
3578   ///
3579   /// @return the dwfl handle.
3580   dwfl_sptr
3581   dwfl_handle() const
3582   {return handle_;}
3583
3584   /// Setter for a smart pointer to a handle on the dwarf front end
3585   /// library that we use to read dwarf.
3586   ///
3587   /// @param h the new dwfl handle.
3588   void
3589   dwfl_handle(dwfl_sptr& h)
3590   {handle_ = h;}
3591
3592   Dwfl_Module*
3593   elf_module() const
3594   {return elf_module_;}
3595
3596   /// Return the ELF descriptor for the binary we are analizing.
3597   ///
3598   /// @return a pointer to the Elf descriptor representing the binary
3599   /// we are analizing.
3600   Elf*
3601   elf_handle() const
3602   {
3603     if (elf_handle_ == 0)
3604       {
3605         if (elf_module())
3606           {
3607             GElf_Addr bias = 0;
3608             elf_handle_ = dwfl_module_getelf(elf_module(), &bias);
3609           }
3610       }
3611     return elf_handle_;
3612   }
3613
3614   /// Return the ELF descriptor used for DWARF access.
3615   ///
3616   /// This can be the same as read_context::elf_handle() above, if the
3617   /// DWARF info is in the same ELF file as the one of the binary we
3618   /// are analizing.  It is different if e.g, the debug info is split
3619   /// from the ELF file we are analizing.
3620   ///
3621   /// @return a pointer to the ELF descriptor used to access debug
3622   /// info.
3623   Elf*
3624   dwarf_elf_handle() const
3625   {return dwarf_getelf(dwarf());}
3626
3627   /// Test if the debug information is in a separate ELF file wrt the
3628   /// main ELF file of the program (application or shared library) we
3629   /// are analizing.
3630   ///
3631   /// @return true if the debug information is in a separate ELF file
3632   /// compared to the main ELF file of the program (application or
3633   /// shared library) that we are looking at.
3634   bool
3635   dwarf_is_splitted() const
3636   {return dwarf_elf_handle() != elf_handle();}
3637
3638   /// Add paths to the set of paths under which to look for split
3639   /// debuginfo files.
3640   ///
3641   /// @param debug_info_root_paths the paths to add.
3642   void
3643   add_debug_info_root_paths(const vector<char **>& debug_info_root_paths)
3644   {
3645     debug_info_root_paths_.insert(debug_info_root_paths_.end(),
3646                                   debug_info_root_paths.begin(),
3647                                   debug_info_root_paths.end());
3648   }
3649
3650   /// Add a path to the set of paths under which to look for split
3651   /// debuginfo files.
3652   ///
3653   /// @param debug_info_root_path the path to add.
3654   void
3655   add_debug_info_root_path(char** debug_info_root_path)
3656   {debug_info_root_paths_.push_back(debug_info_root_path);}
3657
3658   /// Find the alternate debuginfo file associated to a given elf file.
3659   ///
3660   /// @param elf_module represents the elf file to consider.
3661   ///
3662   /// @param alt_file_name the resulting path to the alternate
3663   /// debuginfo file found.  This is set iff the function returns a
3664   /// non-nil value.
3665   Dwarf*
3666   find_alt_debug_info(Dwfl_Module *elf_module,
3667                       string& alt_file_name,
3668                       int& alt_fd)
3669   {
3670     Dwarf *result = 0;
3671     result = dwarf_reader::find_alt_debug_info(elf_module,
3672                                                debug_info_root_paths_,
3673                                                alt_file_name, alt_fd);
3674     return result;
3675   }
3676
3677   /// Load the debug info associated with an elf file that is at a
3678   /// given path.
3679   ///
3680   /// @return a pointer to the DWARF debug info pointer upon
3681   /// successful debug info loading, NULL otherwise.
3682   Dwarf*
3683   load_debug_info()
3684   {
3685     if (!dwfl_handle())
3686       return 0;
3687
3688     if (dwarf_)
3689       return dwarf_;
3690
3691     elf_module_ =
3692       dwfl_report_offline(dwfl_handle().get(),
3693                           basename(const_cast<char*>(elf_path().c_str())),
3694                           elf_path().c_str(),
3695                           -1);
3696     dwfl_report_end(dwfl_handle().get(), 0, 0);
3697
3698     Dwarf_Addr bias = 0;
3699     dwarf_ = dwfl_module_getdwarf(elf_module_, &bias);
3700     // Look for split debuginfo files under multiple possible
3701     // debuginfo roots.
3702     for (vector<char**>::const_iterator i = debug_info_root_paths_.begin();
3703          dwarf_ == 0 && i != debug_info_root_paths_.end();
3704          ++i)
3705       {
3706         offline_callbacks()->debuginfo_path = *i;
3707         dwarf_ = dwfl_module_getdwarf(elf_module_, &bias);
3708       }
3709
3710     if (!alt_dwarf_)
3711       alt_dwarf_ = find_alt_debug_info(elf_module_,
3712                                        alt_debug_info_path_,
3713                                        alt_fd_);
3714
3715     return dwarf_;
3716   }
3717
3718   /// Return the main debug info we are looking at.
3719   ///
3720   /// @return the main debug info.
3721   Dwarf*
3722   dwarf() const
3723   {return dwarf_;}
3724
3725   /// Return the alternate debug info we are looking at.
3726   ///
3727   /// Note that "alternate debug info sections" is a GNU extension as
3728   /// of DWARF4 and is described at
3729   /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3730   ///
3731   /// @return the alternate debug info.
3732   Dwarf*
3733   alt_dwarf() const
3734   {return alt_dwarf_;}
3735
3736   /// Return the correct debug info, depending on the DIE source we
3737   /// are looking at.
3738   ///
3739   /// @param source the DIE source to consider.
3740   ///
3741   /// @return the right debug info, depending on @p source.
3742   Dwarf*
3743   dwarf_per_die_source(die_source source) const
3744   {
3745     Dwarf *result = 0;
3746     switch(source)
3747       {
3748       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3749       case TYPE_UNIT_DIE_SOURCE:
3750         result = dwarf();
3751         break;
3752       case ALT_DEBUG_INFO_DIE_SOURCE:
3753         result = alt_dwarf();
3754         break;
3755       case NO_DEBUG_INFO_DIE_SOURCE:
3756       case NUMBER_OF_DIE_SOURCES:
3757         ABG_ASSERT_NOT_REACHED;
3758       }
3759     return result;
3760   }
3761
3762   /// Return the path to the alternate debug info as contained in the
3763   /// .gnu_debugaltlink section of the main elf file.
3764   ///
3765   /// Note that "alternate debug info sections" is a GNU extension as
3766   /// of DWARF4 and is described at
3767   /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3768   ///
3769   /// @return the path to the alternate debug info file, or an empty
3770   /// path if no alternate debug info file is associated.
3771   const string&
3772   alt_debug_info_path() const
3773   {return alt_debug_info_path_;}
3774
3775   /// Return the path to the ELF path we are reading.
3776   ///
3777   /// @return the elf path.
3778   const string&
3779   elf_path() const
3780   {return elf_path_;}
3781
3782   /// Return the bss section of the ELF file we are reading.
3783   ///
3784   /// The first time this function is called, the ELF file is scanned
3785   /// to look for the section we are looking for.  Once the section is
3786   /// found, it's cached.
3787   ///
3788   /// Subsequent calls to this function just return the cached
3789   /// version.
3790   ///
3791   /// @return the bss section.
3792   Elf_Scn*
3793   bss_section() const
3794   {
3795     if (!bss_section_)
3796       bss_section_ = find_bss_section(elf_handle());
3797     return bss_section_;
3798   }
3799
3800   /// Return the text section of the ELF file we are reading.
3801   ///
3802   /// The first time this function is called, the ELF file is scanned
3803   /// to look for the section we are looking for.  Once the section is
3804   /// found, it's cached.
3805   ///
3806   /// Subsequent calls to this function just return the cached
3807   /// version.
3808   ///
3809   /// return the text section.
3810   Elf_Scn*
3811   text_section() const
3812   {
3813     if (!text_section_)
3814       text_section_ = find_text_section(elf_handle());
3815     return text_section_;
3816   }
3817
3818   /// Return the rodata section of the ELF file we are reading.
3819   ///
3820   /// The first time this function is called, the ELF file is scanned
3821   /// to look for the section we are looking for.  Once the section is
3822   /// found, it's cached.
3823   ///
3824   /// Subsequent calls to this function just return the cached
3825   /// version.
3826   ///
3827   /// return the rodata section.
3828   Elf_Scn*
3829   rodata_section() const
3830   {
3831     if (!rodata_section_)
3832       rodata_section_ =find_rodata_section(elf_handle());
3833     return rodata_section_;
3834   }
3835
3836   /// Return the data section of the ELF file we are reading.
3837   ///
3838   /// The first time this function is called, the ELF file is scanned
3839   /// to look for the section we are looking for.  Once the section is
3840   /// found, it's cached.
3841   ///
3842   /// Subsequent calls to this function just return the cached
3843   /// version.
3844   ///
3845   /// return the data section.
3846   Elf_Scn*
3847   data_section() const
3848   {
3849     if (!data_section_)
3850       data_section_ = find_data_section(elf_handle());
3851     return data_section_;
3852   }
3853
3854   /// Return the data1 section of the ELF file we are reading.
3855   ///
3856   /// The first time this function is called, the ELF file is scanned
3857   /// to look for the section we are looking for.  Once the section is
3858   /// found, it's cached.
3859   ///
3860   /// Subsequent calls to this function just return the cached
3861   /// version.
3862   ///
3863   /// return the data1 section.
3864   Elf_Scn*
3865   data1_section() const
3866   {
3867     if (!data1_section_)
3868       data1_section_ = find_data1_section(elf_handle());
3869     return data1_section_;
3870   }
3871
3872   const Dwarf_Die*
3873   cur_tu_die() const
3874   {return cur_tu_die_;}
3875
3876   void
3877   cur_tu_die(Dwarf_Die* cur_tu_die)
3878   {cur_tu_die_ = cur_tu_die;}
3879
3880   dwarf_expr_eval_context&
3881   dwarf_expr_eval_ctxt() const
3882   {return dwarf_expr_eval_context_;}
3883
3884   /// Getter of the maps set that associates a representation of a
3885   /// decl DIE to a vector of offsets of DIEs having that representation.
3886   ///
3887   /// @return the maps set that associates a representation of a decl
3888   /// DIE to a vector of offsets of DIEs having that representation.
3889   const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
3890   decl_die_repr_die_offsets_maps() const
3891   {return decl_die_repr_die_offsets_maps_;}
3892
3893   /// Getter of the maps set that associates a representation of a
3894   /// decl DIE to a vector of offsets of DIEs having that representation.
3895   ///
3896   /// @return the maps set that associates a representation of a decl
3897   /// DIE to a vector of offsets of DIEs having that representation.
3898   die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
3899   decl_die_repr_die_offsets_maps()
3900   {return decl_die_repr_die_offsets_maps_;}
3901
3902   /// Getter of the maps set that associate a representation of a type
3903   /// DIE to a vector of offsets of DIEs having that representation.
3904   ///
3905   /// @return the maps set that associate a representation of a type
3906   /// DIE to a vector of offsets of DIEs having that representation.
3907   const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
3908   type_die_repr_die_offsets_maps() const
3909   {return type_die_repr_die_offsets_maps_;}
3910
3911   /// Getter of the maps set that associate a representation of a type
3912   /// DIE to a vector of offsets of DIEs having that representation.
3913   ///
3914   /// @return the maps set that associate a representation of a type
3915   /// DIE to a vector of offsets of DIEs having that representation.
3916   die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
3917   type_die_repr_die_offsets_maps()
3918   {return type_die_repr_die_offsets_maps_;}
3919
3920
3921   /// Compute the offset of the canonical DIE of a given DIE.
3922   ///
3923   /// @param die the DIE to consider.
3924   ///
3925   /// @param canonical_die_offset out parameter.  This is set to the
3926   /// resulting canonical DIE that was computed.
3927   ///
3928   /// @param die_as_type if yes, it means @p die has to be considered
3929   /// as a type.
3930   void
3931   compute_canonical_die_offset(const Dwarf_Die *die,
3932                                Dwarf_Off &canonical_die_offset,
3933                                bool die_as_type) const
3934   {
3935     offset_offset_map_type &canonical_dies =
3936       die_as_type
3937       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
3938       get_container(*this, die)
3939       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
3940       get_container(*this, die);
3941
3942     Dwarf_Die canonical_die;
3943     compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
3944
3945     canonical_die_offset = dwarf_dieoffset(&canonical_die);
3946   }
3947
3948   /// Compute (find) the canonical DIE of a given DIE.
3949   ///
3950   /// @param die the DIE to consider.
3951   ///
3952   /// @param canonical_dies the vector in which the canonical dies ar
3953   /// stored.  The index of each element is the offset of the DIE we
3954   /// want the canonical DIE for.  And the value of the element at
3955   /// that index is the canonical DIE offset we are looking for.
3956   ///
3957   /// @param canonical_die_offset out parameter.  This is set to the
3958   /// resulting canonical DIE that was computed.
3959   ///
3960   /// @param die_as_type if yes, it means @p die has to be considered
3961   /// as a type.
3962   void
3963   compute_canonical_die(const Dwarf_Die *die,
3964                         offset_offset_map_type& canonical_dies,
3965                         Dwarf_Die &canonical_die,
3966                         bool die_as_type) const
3967   {
3968     die_source source;
3969     ABG_ASSERT(get_die_source(die, source));
3970
3971     Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3972
3973     compute_canonical_die(die_offset, source,
3974                           canonical_dies,
3975                           canonical_die, die_as_type);
3976   }
3977
3978   /// Compute (find) the canonical DIE of a given DIE.
3979   ///
3980   /// @param die_offset the offset of the DIE to consider.
3981   ///
3982   /// @param source the source of the DIE to consider.
3983   ///
3984   /// @param canonical_dies the vector in which the canonical dies ar
3985   /// stored.  The index of each element is the offset of the DIE we
3986   /// want the canonical DIE for.  And the value of the element at
3987   /// that index is the canonical DIE offset we are looking for.
3988   ///
3989   /// @param canonical_die_offset out parameter.  This is set to the
3990   /// resulting canonical DIE that was computed.
3991   ///
3992   /// @param die_as_type if yes, it means @p die has to be considered
3993   /// as a type.
3994   void
3995   compute_canonical_die(Dwarf_Off die_offset,
3996                         die_source source,
3997                         offset_offset_map_type& canonical_dies,
3998                         Dwarf_Die &canonical_die,
3999                         bool die_as_type) const
4000   {
4001     // The map that associates the string representation of 'die'
4002     // with a vector of offsets of potentially equivalent DIEs.
4003     istring_dwarf_offsets_map_type& map =
4004       die_as_type
4005       ? (const_cast<read_context*>(this)->
4006          type_die_repr_die_offsets_maps().get_container(source))
4007       : (const_cast<read_context*>(this)->
4008          decl_die_repr_die_offsets_maps().get_container(source));
4009
4010     Dwarf_Die die;
4011     ABG_ASSERT(dwarf_offdie(dwarf_per_die_source(source), die_offset, &die));
4012
4013     // The variable repr is the the string representation of 'die'.
4014     //
4015     // Even if die_as_type is true -- which means that 'die' is said
4016     // to be considered as a type -- we always consider a
4017     // DW_TAG_subprogram DIE as a decl here, as far as its string
4018     // representation is concerned.
4019     interned_string name =
4020       (die_as_type)
4021       ? get_die_pretty_type_representation(&die, /*where=*/0)
4022       : get_die_pretty_representation(&die, /*where=*/0);
4023
4024     Dwarf_Off canonical_die_offset = 0;
4025     istring_dwarf_offsets_map_type::iterator i = map.find(name);
4026     if (i == map.end())
4027       {
4028         dwarf_offsets_type offsets;
4029         offsets.push_back(die_offset);
4030         map[name] = offsets;
4031         set_canonical_die_offset(canonical_dies, die_offset, die_offset);
4032         get_die_from_offset(source, die_offset, &canonical_die);
4033         return;
4034       }
4035
4036     if (odr_is_relevant(&die))
4037       {
4038         // ODR is relevant for this DIE.  In this case, all types with
4039         // the same name are considered equivalent.  So the array
4040         // i->second shoud only have on element.  If not, then
4041         // the DIEs referenced in the array should all compare equal.
4042         // Otherwise, this is an ODR violation.  In any case, return
4043         // the first element of the array.
4044         // ABG_ASSERT(i->second.size() == 1);
4045         canonical_die_offset = i->second.front();
4046         get_die_from_offset(source, canonical_die_offset, &canonical_die);
4047         set_canonical_die_offset(canonical_dies, die_offset, die_offset);
4048         return;
4049       }
4050
4051     Dwarf_Off cur_die_offset;
4052     Dwarf_Die potential_canonical_die;
4053     for (dwarf_offsets_type::const_iterator o = i->second.begin();
4054          o != i->second.end();
4055          ++o)
4056       {
4057         cur_die_offset = *o;
4058         get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
4059         if (compare_dies(*this, &die, &potential_canonical_die,
4060                          /*update_canonical_dies_on_the_fly=*/false))
4061           {
4062             canonical_die_offset = cur_die_offset;
4063             set_canonical_die_offset(canonical_dies, die_offset,
4064                                      canonical_die_offset);
4065             get_die_from_offset(source, canonical_die_offset, &canonical_die);
4066             return;
4067           }
4068       }
4069
4070     canonical_die_offset = die_offset;
4071     i->second.push_back(die_offset);
4072     set_canonical_die_offset(canonical_dies, die_offset, die_offset);
4073     get_die_from_offset(source, canonical_die_offset, &canonical_die);
4074   }
4075
4076   /// Getter of the canonical DIE of a given DIE.
4077   ///
4078   /// @param die the DIE to consider.
4079   ///
4080   /// @param canonical_die output parameter.  Is set to the resuling
4081   /// canonical die, if this function returns true.
4082   ///
4083   /// @param where the offset of the logical DIE we are supposed to be
4084   /// calling this function from.  If set to zero this means this is
4085   /// to be ignored.
4086   ///
4087   /// @param die_as_type if set to yes, it means @p die is to be
4088   /// considered as a type DIE.
4089   ///
4090   /// @return true iff a canonical DIE was found for @p die.
4091   bool
4092   get_canonical_die(const Dwarf_Die *die,
4093                     Dwarf_Die &canonical_die,
4094                     size_t where,
4095                     bool die_as_type) const
4096   {
4097     die_source source;
4098     ABG_ASSERT(get_die_source(die, source));
4099
4100     offset_offset_map_type &canonical_dies =
4101       die_as_type
4102       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
4103       get_container(source)
4104       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
4105       get_container(source);
4106
4107     Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
4108     if (Dwarf_Off canonical_die_offset =
4109         get_canonical_die_offset(canonical_dies, die_offset))
4110       {
4111         get_die_from_offset(source, canonical_die_offset, &canonical_die);
4112         return true;
4113       }
4114
4115     // The map that associates the string representation of 'die'
4116     // with a vector of offsets of potentially equivalent DIEs.
4117     istring_dwarf_offsets_map_type& map =
4118       die_as_type
4119       ? (const_cast<read_context*>(this)->
4120          type_die_repr_die_offsets_maps().get_container(*this, die))
4121       : (const_cast<read_context*>(this)->
4122          decl_die_repr_die_offsets_maps().get_container(*this, die));
4123
4124     // The variable repr is the the string representation of 'die'.
4125     //
4126     // Even if die_as_type is true -- which means that 'die' is said
4127     // to be considered as a type -- we always consider a
4128     // DW_TAG_subprogram DIE as a decl here, as far as its string
4129     // representation is concerned.
4130     interned_string name =
4131       (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
4132       ? get_die_pretty_type_representation(die, where)
4133       : get_die_pretty_representation(die, where);
4134
4135     istring_dwarf_offsets_map_type::iterator i = map.find(name);
4136     if (i == map.end())
4137       return false;
4138
4139     if (odr_is_relevant(die))
4140       {
4141         // ODR is relevant for this DIE.  In this case, all types with
4142         // the same name are considered equivalent.  So the array
4143         // i->second shoud only have on element.  If not, then
4144         // the DIEs referenced in the array should all compare equal.
4145         // Otherwise, this is an ODR violation.  In any case, return
4146         // the first element of the array.
4147         // ABG_ASSERT(i->second.size() == 1);
4148         Dwarf_Off canonical_die_offset = i->second.front();
4149         get_die_from_offset(source, canonical_die_offset, &canonical_die);
4150         set_canonical_die_offset(canonical_dies,
4151                                  die_offset,
4152                                  canonical_die_offset);
4153         return true;
4154       }
4155
4156     Dwarf_Off cur_die_offset;
4157     for (dwarf_offsets_type::const_iterator o = i->second.begin();
4158          o != i->second.end();
4159          ++o)
4160       {
4161         cur_die_offset = *o;
4162         get_die_from_offset(source, cur_die_offset, &canonical_die);
4163         // compare die and canonical_die.
4164         if (compare_dies(*this, die, &canonical_die,
4165                          /*update_canonical_dies_on_the_fly=*/true))
4166           {
4167             set_canonical_die_offset(canonical_dies,
4168                                      die_offset,
4169                                      cur_die_offset);
4170             return true;
4171           }
4172       }
4173
4174     return false;
4175   }
4176
4177   /// Retrieve the canonical DIE of a given DIE.
4178   ///
4179   /// The canonical DIE is a DIE that is structurally equivalent to
4180   /// this one.
4181   ///
4182   /// Note that this function caches the canonical DIE that was
4183   /// computed.  Subsequent invocations of this function on the same
4184   /// DIE return the same cached DIE.
4185   ///
4186   /// @param die the DIE to get a canonical type for.
4187   ///
4188   /// @param canonical_die the resulting canonical DIE.
4189   ///
4190   /// @param where the offset of the logical DIE we are supposed to be
4191   /// calling this function from.  If set to zero this means this is
4192   /// to be ignored.
4193   ///
4194   /// @param die_as_type if true, consider DIE is a type.
4195   ///
4196   /// @return true if an *existing* canonical DIE was found.
4197   /// Otherwise, @p die is considered as being a canonical DIE for
4198   /// itself. @p canonical_die is thus set to the canonical die in
4199   /// either cases.
4200   bool
4201   get_or_compute_canonical_die(const Dwarf_Die* die,
4202                                Dwarf_Die& canonical_die,
4203                                size_t where,
4204                                bool die_as_type) const
4205   {
4206     die_source source;
4207     ABG_ASSERT(get_die_source(die, source));
4208
4209     offset_offset_map_type &canonical_dies =
4210       die_as_type
4211       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
4212       get_container(source)
4213       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
4214       get_container(source);
4215
4216     Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
4217
4218     if (Dwarf_Off canonical_die_offset =
4219         get_canonical_die_offset(canonical_dies,
4220                                  initial_die_offset))
4221       {
4222         get_die_from_offset(source, canonical_die_offset, &canonical_die);
4223         return true;
4224       }
4225
4226     // The map that associates the string representation of 'die'
4227     // with a vector of offsets of potentially equivalent DIEs.
4228     istring_dwarf_offsets_map_type& map =
4229       die_as_type
4230       ? (const_cast<read_context*>(this)->
4231          type_die_repr_die_offsets_maps().get_container(*this, die))
4232       : (const_cast<read_context*>(this)->
4233          decl_die_repr_die_offsets_maps().get_container(*this, die));
4234
4235     // The variable repr is the the string representation of 'die'.
4236     //
4237     // Even if die_as_type is true -- which means that 'die' is said
4238     // to be considered as a type -- we always consider a
4239     // DW_TAG_subprogram DIE as a decl here, as far as its string
4240     // representation is concerned.
4241     interned_string name =
4242       (die_as_type)
4243       ? get_die_pretty_type_representation(die, where)
4244       : get_die_pretty_representation(die, where);
4245
4246     istring_dwarf_offsets_map_type::iterator i = map.find(name);
4247     if (i == map.end())
4248       {
4249         dwarf_offsets_type offsets;
4250         offsets.push_back(initial_die_offset);
4251         map[name] = offsets;
4252         get_die_from_offset(source, initial_die_offset, &canonical_die);
4253         set_canonical_die_offset(canonical_dies,
4254                                  initial_die_offset,
4255                                  initial_die_offset);
4256         return false;
4257       }
4258
4259     if (odr_is_relevant(die))
4260       {
4261         // ODR is relevant for this DIE.  In this case, all types with
4262         // the same name are considered equivalent.  So the array
4263         // i->second shoud only have on element.  If not, then
4264         // the DIEs referenced in the array should all compare equal.
4265         // Otherwise, this is an ODR violation.  In any case, return
4266         // the first element of the array.
4267         // ABG_ASSERT(i->second.size() == 1);
4268         Dwarf_Off die_offset = i->second.front();
4269         get_die_from_offset(source, die_offset, &canonical_die);
4270         set_canonical_die_offset(canonical_dies,
4271                                  initial_die_offset,
4272                                  die_offset);
4273         return true;
4274       }
4275
4276     // walk i->second without any iterator (using a while loop rather
4277     // than a for loop) because compare_dies might add new content to
4278     // the end of the i->second vector during the walking.
4279     dwarf_offsets_type::size_type n = 0, s = i->second.size();
4280     while (n < s)
4281       {
4282         Dwarf_Off die_offset = i->second[n];
4283         get_die_from_offset(source, die_offset, &canonical_die);
4284         // compare die and canonical_die.
4285         if (compare_dies(*this, die, &canonical_die,
4286                          /*update_canonical_dies_on_the_fly=*/true))
4287           {
4288             set_canonical_die_offset(canonical_dies,
4289                                      initial_die_offset,
4290                                      die_offset);
4291             return true;
4292           }
4293         ++n;
4294       }
4295
4296     // We didn't find a canonical DIE for 'die'.  So let's consider
4297     // that it is its own canonical DIE.
4298     get_die_from_offset(source, initial_die_offset, &canonical_die);
4299     i->second.push_back(initial_die_offset);
4300     set_canonical_die_offset(canonical_dies,
4301                              initial_die_offset,
4302                              initial_die_offset);
4303
4304     return false;
4305   }
4306
4307   /// Get the source of the DIE.
4308   ///
4309   /// The function returns an enumerator value saying if the DIE comes
4310   /// from the .debug_info section of the primary debug info file, the
4311   /// .debug_info section of the alternate debug info file, or the
4312   /// .debug_types section.
4313   ///
4314   /// @param die the DIE to get the source of.
4315   ///
4316   /// @param source out parameter.  The function sets this parameter
4317   /// to the source of the DIE @p iff it returns true.
4318   ///
4319   /// @return true iff the source of the DIE could be determined and
4320   /// returned.
4321   bool
4322   get_die_source(const Dwarf_Die *die, die_source &source) const
4323   {
4324     ABG_ASSERT(die);
4325     return get_die_source(*die, source);
4326   }
4327
4328   /// Get the source of the DIE.
4329   ///
4330   /// The function returns an enumerator value saying if the DIE comes
4331   /// from the .debug_info section of the primary debug info file, the
4332   /// .debug_info section of the alternate debug info file, or the
4333   /// .debug_types section.
4334   ///
4335   /// @param die the DIE to get the source of.
4336   ///
4337   /// @param source out parameter.  The function sets this parameter
4338   /// to the source of the DIE @p iff it returns true.
4339   ///
4340   /// @return true iff the source of the DIE could be determined and
4341   /// returned.
4342   bool
4343   get_die_source(const Dwarf_Die &die, die_source &source) const
4344   {
4345     Dwarf_Die cu_die;
4346     Dwarf_Die cu_kind;
4347     uint8_t address_size = 0, offset_size = 0;
4348     if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
4349                      &cu_die, &address_size,
4350                      &offset_size))
4351       return false;
4352
4353     Dwarf_Half version = 0;
4354     Dwarf_Off abbrev_offset = 0;
4355     uint64_t type_signature = 0;
4356     Dwarf_Off type_offset = 0;
4357     if (!dwarf_cu_die(cu_die.cu, &cu_kind,
4358                       &version, &abbrev_offset,
4359                       &address_size, &offset_size,
4360                       &type_signature, &type_offset))
4361       return false;
4362
4363     int tag = dwarf_tag(&cu_kind);
4364
4365     if (tag == DW_TAG_compile_unit
4366         || tag == DW_TAG_partial_unit)
4367       {
4368         Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
4369         if (dwarf() == die_dwarf)
4370           source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
4371         else if (alt_dwarf() == die_dwarf)
4372           source = ALT_DEBUG_INFO_DIE_SOURCE;
4373         else
4374           ABG_ASSERT_NOT_REACHED;
4375       }
4376     else if (tag == DW_TAG_type_unit)
4377       source = TYPE_UNIT_DIE_SOURCE;
4378     else
4379       return false;
4380
4381     return true;
4382   }
4383
4384   /// Getter for the DIE designated by an offset.
4385   ///
4386   /// @param source the source of the DIE to get.
4387   ///
4388   /// @param offset the offset of the DIE to get.
4389   ///
4390   /// @param die the resulting DIE.  The pointer has to point to an
4391   /// allocated memory region.
4392   void
4393   get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
4394   {
4395     if (source == TYPE_UNIT_DIE_SOURCE)
4396       ABG_ASSERT(dwarf_offdie_types(dwarf_per_die_source(source), offset, die));
4397     else
4398       ABG_ASSERT(dwarf_offdie(dwarf_per_die_source(source), offset, die));
4399   }
4400
4401 public:
4402
4403   /// Add an entry to the relevant die->decl map.
4404   ///
4405   /// @param die the DIE to add the the map.
4406   ///
4407   /// @param decl the decl to consider.
4408   ///
4409   /// @param where_offset where in the DIE stream we logically are.
4410   ///
4411   /// @param do_associate_by_repr if true then this function
4412   /// associates the representation string of @p die with the
4413   /// declaration @p decl, in a corpus-wide manner.  That is, in the
4414   /// entire current corpus, there is going to be just one declaration
4415   /// associated with a DIE of the string representation of @p die.
4416   ///
4417   /// @param do_associate_by_repr_per_tu if true, then this function
4418   /// associates the representation string of @p die with the
4419   /// declaration @p decl in a translation unit wide manner.  That is,
4420   /// in the entire current translation unit, there is going to be
4421   /// just one declaration associated with a DIE of the string
4422   /// representation of @p die.
4423   void
4424   associate_die_to_decl(Dwarf_Die* die,
4425                         decl_base_sptr decl,
4426                         size_t where_offset,
4427                         bool do_associate_by_repr = false)
4428   {
4429     die_source source;
4430     ABG_ASSERT(get_die_source(die, source));
4431
4432     die_artefact_map_type& m =
4433       decl_die_artefact_maps().get_container(source);
4434
4435     size_t die_offset;
4436     if (do_associate_by_repr)
4437       {
4438         Dwarf_Die equiv_die;
4439         get_or_compute_canonical_die(die, equiv_die, where_offset,
4440                                      /*die_as_type=*/false);
4441         die_offset = dwarf_dieoffset(&equiv_die);
4442       }
4443     else
4444       die_offset = dwarf_dieoffset(die);
4445
4446     m[die_offset] = decl;
4447   }
4448
4449 public:
4450
4451   /// Lookup the decl for a given DIE.
4452   ///
4453   /// The returned decl is either the decl of the DIE that as the
4454   /// exact offset @p die_offset
4455   /// die_offset, or
4456   /// give
4457   ///
4458   /// @param die_offset the offset of the DIE to consider.
4459   ///
4460   /// @param source where the DIE represented by @p die_offset comes
4461   /// from.
4462   ///
4463   /// Note that "alternate debug info sections" is a GNU extension as
4464   /// of DWARF4 and is described at
4465   /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
4466   ///
4467   /// @return the resulting decl, or null if no decl is associated to
4468   /// the DIE represented by @p die_offset.
4469   decl_base_sptr
4470   lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
4471   {
4472     decl_base_sptr result =
4473       is_decl(lookup_artifact_from_die_offset(die_offset, source,
4474                                               /*die_as_type=*/false));
4475
4476     return result;
4477   }
4478
4479   /// Get the qualified name of a given DIE.
4480   ///
4481   /// If the name of the DIE was already computed before just return
4482   /// that name from a cache.  Otherwise, build the name, cache it and
4483   /// return it.
4484   ///
4485   /// @param die the DIE to consider.
4486   ///
4487   /// @param where_offset where in the DIE stream we logically are.
4488   ///
4489   /// @return the interned string representing the qualified name of
4490   /// @p die.
4491   interned_string
4492   get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
4493   {
4494     ABG_ASSERT(die);
4495     die_istring_map_type& map =
4496       die_qualified_name_maps_.get_container(*this, die);
4497
4498     size_t die_offset = dwarf_dieoffset(die);
4499     die_istring_map_type::const_iterator i = map.find(die_offset);
4500
4501     if (i == map.end())
4502       {
4503         read_context& ctxt  = *const_cast<read_context*>(this);
4504         string qualified_name = die_qualified_name(ctxt, die, where_offset);
4505         interned_string istr = env()->intern(qualified_name);
4506         map[die_offset] = istr;
4507         return istr;
4508       }
4509
4510     return i->second;
4511   }
4512
4513   /// Get the qualified name of a given DIE.
4514   ///
4515   /// If the name of the DIE was already computed before just return
4516   /// that name from a cache.  Otherwise, build the name, cache it and
4517   /// return it.
4518   ///
4519   /// @param die the DIE to consider.
4520   ///
4521   /// @param where_offset where in the DIE stream we logically are.
4522   ///
4523   /// @return the interned string representing the qualified name of
4524   /// @p die.
4525   interned_string
4526   get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
4527   {
4528     return const_cast<read_context*>(this)->
4529       get_die_qualified_name(die, where_offset);
4530   }
4531
4532   /// Get the qualified name of a given DIE which is considered to be
4533   /// the DIE for a type.
4534   ///
4535   /// For instance, for a DW_TAG_subprogram DIE, this function
4536   /// computes the name of the function *type* that corresponds to the
4537   /// function.
4538   ///
4539   /// If the name of the DIE was already computed before just return
4540   /// that name from a cache.  Otherwise, build the name, cache it and
4541   /// return it.
4542   ///
4543   /// @param die the DIE to consider.
4544   ///
4545   /// @param where_offset where in the DIE stream we logically are.
4546   ///
4547   /// @return the interned string representing the qualified name of
4548   /// @p die.
4549   interned_string
4550   get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
4551   {
4552     ABG_ASSERT(die);
4553
4554     // The name of the translation unit die is "".
4555     if (die == cur_tu_die())
4556       return env()->intern("");
4557
4558     die_istring_map_type& map =
4559       die_qualified_name_maps_.get_container(*const_cast<read_context*>(this),
4560                                              die);
4561
4562     size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
4563     die_istring_map_type::const_iterator i =
4564       map.find(die_offset);
4565
4566     if (i == map.end())
4567       {
4568         read_context& ctxt  = *const_cast<read_context*>(this);
4569         string qualified_name;
4570         int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
4571         if ((tag == DW_TAG_structure_type
4572              || tag == DW_TAG_class_type
4573              || tag == DW_TAG_union_type)
4574             && die_is_anonymous(die))
4575           {
4576             location l = die_location(*this, die);
4577             qualified_name = l ? l.expand() : "noloc";
4578             qualified_name = "unnamed-at-" + qualified_name;
4579           }
4580         else
4581           qualified_name =
4582             die_qualified_type_name(ctxt, die, where_offset);
4583
4584         interned_string istr = env()->intern(qualified_name);
4585         map[die_offset] = istr;
4586         return istr;
4587       }
4588
4589     return i->second;
4590   }
4591
4592   /// Get the pretty representation of a DIE that represents a type.
4593   ///
4594   /// For instance, for the DW_TAG_subprogram, this function computes
4595   /// the pretty representation of the type of the function, not the
4596   /// pretty representation of the function declaration.
4597   ///
4598   /// Once the pretty representation is computed, it's stored in a
4599   /// cache.  Subsequent invocations of this function on the same DIE
4600   /// will yield the cached name.
4601   ///
4602   /// @param die the DIE to consider.
4603   ///
4604   /// @param where_offset where in the DIE stream we logically are.
4605   ///
4606   /// @return the interned_string that represents the pretty
4607   /// representation.
4608   interned_string
4609   get_die_pretty_type_representation(const Dwarf_Die *die,
4610                                      size_t where_offset) const
4611   {
4612     ABG_ASSERT(die);
4613     die_istring_map_type& map =
4614       die_pretty_type_repr_maps_.get_container(*const_cast<read_context*>(this),
4615                                                die);
4616
4617     size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
4618     die_istring_map_type::const_iterator i = map.find(die_offset);
4619
4620     if (i == map.end())
4621       {
4622         read_context& ctxt = *const_cast<read_context*>(this);
4623         string pretty_representation =
4624           die_pretty_print_type(ctxt, die, where_offset);
4625         interned_string istr = env()->intern(pretty_representation);
4626         map[die_offset] = istr;
4627         return istr;
4628       }
4629
4630     return i->second;
4631   }
4632
4633   /// Get the pretty representation of a DIE.
4634   ///
4635   /// Once the pretty representation is computed, it's stored in a
4636   /// cache.  Subsequent invocations of this function on the same DIE
4637   /// will yield the cached name.
4638   ///
4639   /// @param die the DIE to consider.
4640   ///
4641   /// @param where_offset where in the DIE stream we logically are.
4642   ///
4643   /// @return the interned_string that represents the pretty
4644   /// representation.
4645   interned_string
4646   get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
4647   {
4648     ABG_ASSERT(die);
4649
4650     die_istring_map_type& map =
4651       die_pretty_repr_maps_.get_container(*const_cast<read_context*>(this),
4652                                           die);
4653
4654     size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
4655     die_istring_map_type::const_iterator i = map.find(die_offset);
4656
4657     if (i == map.end())
4658       {
4659         read_context& ctxt = *const_cast<read_context*>(this);
4660         string pretty_representation =
4661           die_pretty_print(ctxt, die, where_offset);
4662         interned_string istr = env()->intern(pretty_representation);
4663         map[die_offset] = istr;
4664         return istr;
4665       }
4666
4667     return i->second;
4668   }
4669
4670   /// Lookup the artifact that was built to represent a type that has
4671   /// the same pretty representation as the type denoted by a given
4672   /// DIE.
4673   ///
4674   /// Note that the DIE must have previously been associated with the
4675   /// artifact using the functions associate_die_to_decl or
4676   /// associate_die_to_type.
4677   ///
4678   /// Also, note that the scope of the lookup is the current ABI
4679   /// corpus.
4680   ///
4681   /// @param die the DIE to consider.
4682   ///
4683   /// @param where_offset where in the DIE stream we logically are.
4684   ///
4685   /// @return the type artifact found.
4686   type_or_decl_base_sptr
4687   lookup_type_artifact_from_die(Dwarf_Die *die) const
4688   {
4689     type_or_decl_base_sptr artifact =
4690       lookup_artifact_from_die(die, /*type_as_die=*/true);
4691     if (function_decl_sptr fn = is_function_decl(artifact))
4692       return fn->get_type();
4693     return artifact;
4694   }
4695
4696   /// Lookup the artifact that was built to represent a type or a
4697   /// declaration that has the same pretty representation as the type
4698   /// denoted by a given DIE.
4699   ///
4700   /// Note that the DIE must have previously been associated with the
4701   /// artifact using the functions associate_die_to_decl or
4702   /// associate_die_to_type.
4703   ///
4704   /// Also, note that the scope of the lookup is the current ABI
4705   /// corpus.
4706   ///
4707   /// @param die the DIE to consider.
4708   ///
4709   /// @param where_offset where in the DIE stream we logically are.
4710   ///
4711   /// @param die_as_type if true, it means the DIE is to be considered
4712   /// as a type.
4713   ///
4714   /// @return the artifact found.
4715   type_or_decl_base_sptr
4716   lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
4717   {
4718     Dwarf_Die equiv_die;
4719     if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
4720       return type_or_decl_base_sptr();
4721
4722     const die_artefact_map_type& m =
4723       die_as_type
4724       ? type_die_artefact_maps().get_container(*this, &equiv_die)
4725       : decl_die_artefact_maps().get_container(*this, &equiv_die);
4726
4727     size_t die_offset = dwarf_dieoffset(&equiv_die);
4728     die_artefact_map_type::const_iterator i = m.find(die_offset);
4729
4730     if (i == m.end())
4731       return type_or_decl_base_sptr();
4732     return i->second;
4733   }
4734
4735   /// Lookup the artifact that was built to represent a type or a
4736   /// declaration that has the same pretty representation as the type
4737   /// denoted by the offset of a given DIE.
4738   ///
4739   /// Note that the DIE must have previously been associated with the
4740   /// artifact using either associate_die_to_decl or
4741   /// associate_die_to_type.
4742   ///
4743   /// Also, note that the scope of the lookup is the current ABI
4744   /// corpus.
4745   ///
4746   /// @param die the DIE to consider.
4747   ///
4748   /// @param where_offset where in the DIE stream we logically are.
4749   ///
4750   /// @param die_as_type if true, it means the DIE is to be considered
4751   /// as a type.
4752   ///
4753   /// @return the artifact found.
4754   type_or_decl_base_sptr
4755   lookup_artifact_from_die_offset(Dwarf_Off die_offset,
4756                                   die_source source,
4757                                   bool die_as_type = false) const
4758   {
4759     const die_artefact_map_type& m =
4760       die_as_type
4761       ? type_die_artefact_maps().get_container(source)
4762       : decl_die_artefact_maps().get_container(source);
4763
4764     die_artefact_map_type::const_iterator i = m.find(die_offset);
4765     if (i == m.end())
4766       return type_or_decl_base_sptr();
4767     return i->second;
4768   }
4769
4770   /// Get the language used to generate a given DIE.
4771   ///
4772   /// @param die the DIE to consider.
4773   ///
4774   /// @param lang the resulting language.
4775   ///
4776   /// @return true iff the language of the DIE was found.
4777   bool
4778   get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
4779   {
4780     Dwarf_Die cu_die;
4781     ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
4782
4783     uint64_t l = 0;
4784     if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
4785       return false;
4786
4787     lang = dwarf_language_to_tu_language(l);
4788     return true;
4789   }
4790
4791   /// Test if a given DIE originates from a program written in the C
4792   /// language.
4793   ///
4794   /// @param die the DIE to consider.
4795   ///
4796   /// @return true iff @p die originates from a program in the C
4797   /// language.
4798   bool
4799   die_is_in_c(const Dwarf_Die *die) const
4800   {
4801     translation_unit::language l = translation_unit::LANG_UNKNOWN;
4802     if (!get_die_language(die, l))
4803       return false;
4804     return is_c_language(l);
4805   }
4806
4807   /// Test if a given DIE originates from a program written in the C++
4808   /// language.
4809   ///
4810   /// @param die the DIE to consider.
4811   ///
4812   /// @return true iff @p die originates from a program in the C++
4813   /// language.
4814   bool
4815   die_is_in_cplus_plus(const Dwarf_Die *die) const
4816   {
4817     translation_unit::language l = translation_unit::LANG_UNKNOWN;
4818     if (!get_die_language(die, l))
4819       return false;
4820     return is_cplus_plus_language(l);
4821   }
4822
4823   /// Test if a given DIE originates from a program written either in
4824   /// C or C++.
4825   ///
4826   /// @param die the DIE to consider.
4827   ///
4828   /// @return true iff @p die originates from a program written either in
4829   /// C or C++.
4830   bool
4831   die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
4832   {
4833     translation_unit::language l = translation_unit::LANG_UNKNOWN;
4834     if (!get_die_language(die, l))
4835       return false;
4836     return (is_cplus_plus_language(l) || is_c_language(l));
4837   }
4838
4839   /// Check if we can assume the One Definition Rule[1] to be relevant
4840   /// for the current translation unit.
4841   ///
4842   /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
4843   ///
4844   /// At the moment this returns true if the current translation unit
4845   /// is in C++ language.  In that case, it's relevant to assume that
4846   /// we use optimizations based on the ODR.
4847   bool
4848   odr_is_relevant() const
4849   {return odr_is_relevant(cur_transl_unit()->get_language());}
4850
4851   /// Check if we can assume the One Definition Rule[1] to be relevant
4852   /// for a given language.
4853   ///
4854   /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
4855   ///
4856   /// At the moment this returns true if the language considered
4857   /// is C++, Java or Ada.
4858   bool
4859   odr_is_relevant(translation_unit::language l) const
4860   {
4861     return (is_cplus_plus_language(l)
4862             || is_java_language(l)
4863             || is_ada_language(l));
4864   }
4865
4866   /// Check if we can assume the One Definition Rule to be relevant
4867   /// for a given DIE.
4868   ///
4869   /// @param die the DIE to consider.
4870   ///
4871   /// @return true if the ODR is relevant for @p die.
4872   bool
4873   odr_is_relevant(Dwarf_Off die_offset, die_source source) const
4874   {
4875     Dwarf_Die die;
4876     ABG_ASSERT(dwarf_offdie(dwarf_per_die_source(source), die_offset, &die));
4877     return odr_is_relevant(&die);
4878   }
4879
4880   /// Check if we can assume the One Definition Rule to be relevant
4881   /// for a given DIE.
4882   ///
4883   /// @param die the DIE to consider.
4884   ///
4885   /// @return true if the ODR is relevant for @p die.
4886   bool
4887   odr_is_relevant(const Dwarf_Die *die) const
4888   {
4889     translation_unit::language lang;
4890     if (!get_die_language(die, lang))
4891       return odr_is_relevant();
4892
4893     return odr_is_relevant(lang);
4894   }
4895
4896   /// Getter for the maps set that associates a decl DIE offset to an
4897   /// artifact.
4898   ///
4899   /// @return the maps set that associates a decl DIE offset to an
4900   /// artifact.
4901   die_source_dependant_container_set<die_artefact_map_type>&
4902   decl_die_artefact_maps()
4903   {return decl_die_artefact_maps_;}
4904
4905   /// Getter for the maps set that associates a decl DIE offset to an
4906   /// artifact.
4907   ///
4908   /// @return the maps set that associates a decl DIE offset to an
4909   /// artifact.
4910   const die_source_dependant_container_set<die_artefact_map_type>&
4911   decl_die_artefact_maps() const
4912   {return decl_die_artefact_maps_;}
4913
4914   /// Getter for the maps set that associates a type DIE offset to an
4915   /// artifact.
4916   ///
4917   /// @return the maps set that associates a type DIE offset to an
4918   /// artifact.
4919   die_source_dependant_container_set<die_artefact_map_type>&
4920   type_die_artefact_maps()
4921   {return type_die_artefact_maps_;}
4922
4923   /// Getter for the maps set that associates a type DIE offset to an
4924   /// artifact.
4925   ///
4926   /// @return the maps set that associates a type DIE offset to an
4927   /// artifact.
4928   const die_source_dependant_container_set<die_artefact_map_type>&
4929   type_die_artefact_maps() const
4930   {return type_die_artefact_maps_;}
4931
4932   /// Getter of the maps that associates function type representations
4933   /// to function types, inside a translation unit.
4934   ///
4935   /// @return the maps that associates function type representations
4936   /// to function types, inside a translation unit.
4937   istring_fn_type_map_type&
4938   per_tu_repr_to_fn_type_maps()
4939   {return per_tu_repr_to_fn_type_maps_;}
4940
4941   /// Getter of the maps that associates function type representations
4942   /// to function types, inside a translation unit.
4943   ///
4944   /// @return the maps that associates function type representations
4945   /// to function types, inside a translation unit.
4946   const istring_fn_type_map_type&
4947   per_tu_repr_to_fn_type_maps() const
4948   {return per_tu_repr_to_fn_type_maps_;}
4949
4950   /// Associate the representation of a function type DIE to a given
4951   /// function type, inside the current translation unit.
4952   ///
4953   /// @param die the DIE to associate to the function type, using its
4954   /// representation.
4955   ///
4956   /// @param fn_type the function type to associate to @p die.
4957   void
4958   associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
4959                                        const function_type_sptr &fn_type)
4960   {
4961     if (!die_is_function_type(die))
4962       return;
4963
4964     interned_string repr =
4965       get_die_pretty_type_representation(die, /*where=*/0);
4966     ABG_ASSERT(!repr.empty());
4967
4968     per_tu_repr_to_fn_type_maps()[repr]= fn_type;
4969   }
4970
4971   /// Lookup the function type associated to a given function type
4972   /// DIE, in the current translation unit.
4973   ///
4974   /// @param die the DIE of function type to consider.
4975   ///
4976   /// @return the @ref function_type_sptr associated to @p die, or nil
4977   /// of no function_type is associated to @p die.
4978   function_type_sptr
4979   lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
4980   {
4981     if (!die_is_function_type(die))
4982       return function_type_sptr();
4983
4984     interned_string repr =
4985       get_die_pretty_representation(die, /*where=*/0);
4986     ABG_ASSERT(!repr.empty());
4987
4988     istring_fn_type_map_type::const_iterator i =
4989       per_tu_repr_to_fn_type_maps().find(repr);
4990
4991     if (i == per_tu_repr_to_fn_type_maps().end())
4992       return function_type_sptr();
4993
4994     return i->second;
4995   }
4996
4997   /// Set the canonical DIE offset of a given DIE.
4998   ///
4999   /// @param canonical_dies the vector that holds canonical DIEs.
5000   ///
5001   /// @param die_offset the offset of the DIE to set the canonical DIE
5002   /// for.
5003   ///
5004   /// @param canonical_die_offset the canonical DIE offset to
5005   /// associate to @p die_offset.
5006   void
5007   set_canonical_die_offset(offset_offset_map_type &canonical_dies,
5008                            Dwarf_Off die_offset,
5009                            Dwarf_Off canonical_die_offset) const
5010   {
5011     canonical_dies[die_offset] = canonical_die_offset;}
5012
5013   /// Set the canonical DIE offset of a given DIE.
5014   ///
5015   ///
5016   /// @param die_offset the offset of the DIE to set the canonical DIE
5017   /// for.
5018   ///
5019   /// @param source the source of the DIE denoted by @p die_offset.
5020   ///
5021   /// @param canonical_die_offset the canonical DIE offset to
5022   /// associate to @p die_offset.
5023   ///
5024   /// @param die_as_type if true, it means that @p die_offset has to
5025   /// be considered as a type.
5026   void
5027   set_canonical_die_offset(Dwarf_Off die_offset,
5028                            die_source source,
5029                            Dwarf_Off canonical_die_offset,
5030                            bool die_as_type) const
5031   {
5032     offset_offset_map_type &canonical_dies =
5033       die_as_type
5034       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
5035       get_container(source)
5036       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
5037       get_container(source);
5038
5039     set_canonical_die_offset(canonical_dies,
5040                              die_offset,
5041                              canonical_die_offset);
5042   }
5043
5044   /// Set the canonical DIE offset of a given DIE.
5045   ///
5046   ///
5047   /// @param die the DIE to set the canonical DIE for.
5048   ///
5049   /// @param canonical_die_offset the canonical DIE offset to
5050   /// associate to @p die_offset.
5051   ///
5052   /// @param die_as_type if true, it means that @p die has to be
5053   /// considered as a type.
5054   void
5055   set_canonical_die_offset(const Dwarf_Die *die,
5056                            Dwarf_Off canonical_die_offset,
5057                            bool die_as_type) const
5058   {
5059     die_source source;
5060     ABG_ASSERT(get_die_source(die, source));
5061
5062     Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
5063
5064     set_canonical_die_offset(die_offset, source,
5065                              canonical_die_offset,
5066                              die_as_type);
5067   }
5068
5069   /// Get the canonical DIE offset of a given DIE.
5070   ///
5071   /// @param canonical_dies the vector that contains canonical DIES.
5072   ///
5073   /// @param die_offset the offset of the DIE to consider.
5074   ///
5075   /// @return the canonical of the DIE denoted by @p die_offset, or
5076   /// zero if no canonical DIE was found.
5077   Dwarf_Off
5078   get_canonical_die_offset(offset_offset_map_type &canonical_dies,
5079                            Dwarf_Off die_offset) const
5080   {
5081     offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
5082     if (it == canonical_dies.end())
5083       return 0;
5084     return it->second;
5085   }
5086
5087   /// Get the canonical DIE offset of a given DIE.
5088   ///
5089   /// @param die_offset the offset of the DIE to consider.
5090   ///
5091   /// @param source the source of the DIE denoted by @p die_offset.
5092   ///
5093   /// @param die_as_type if true, it means that @p is to be considered
5094   /// as a type DIE.
5095   ///
5096   /// @return the canonical of the DIE denoted by @p die_offset, or
5097   /// zero if no canonical DIE was found.
5098   Dwarf_Off
5099   get_canonical_die_offset(Dwarf_Off die_offset,
5100                            die_source source,
5101                            bool die_as_type) const
5102   {
5103     offset_offset_map_type &canonical_dies =
5104       die_as_type
5105       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
5106       get_container(source)
5107       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
5108       get_container(source);
5109
5110     return get_canonical_die_offset(canonical_dies, die_offset);
5111   }
5112
5113   /// Associate a DIE (representing a type) to the type that it
5114   /// represents.
5115   ///
5116   /// @param die the DIE to consider.
5117   ///
5118   /// @param type the type to associate the DIE to.
5119   ///
5120   /// @param where_offset where in the DIE stream we logically are.
5121   void
5122   associate_die_to_type(const Dwarf_Die *die,
5123                         type_base_sptr  type,
5124                         size_t          where)
5125   {
5126     if (!type)
5127       return;
5128
5129     Dwarf_Die equiv_die;
5130     get_or_compute_canonical_die(die, equiv_die, where, /*die_as_type=*/true);
5131
5132     die_artefact_map_type& m =
5133       type_die_artefact_maps().get_container(*this, &equiv_die);
5134
5135     size_t die_offset = dwarf_dieoffset(&equiv_die);
5136     m[die_offset] = type;
5137   }
5138
5139   /// Lookup the type associated to a given DIE.
5140   ///
5141   /// Note that the DIE must have been associated to type by a
5142   /// previous invocation of the function
5143   /// read_context::associate_die_to_type().
5144   ///
5145   /// @param die the DIE to consider.
5146   ///
5147   /// @return the type associated to the DIE or NULL if no type is
5148   /// associated to the DIE.
5149   type_base_sptr
5150   lookup_type_from_die(const Dwarf_Die* die) const
5151   {
5152     type_or_decl_base_sptr artifact =
5153       lookup_artifact_from_die(die, /*die_as_type=*/true);
5154     if (function_decl_sptr fn = is_function_decl(artifact))
5155       return fn->get_type();
5156     return is_type(artifact);
5157   }
5158
5159   /// Lookup the type associated to a DIE at a given offset, from a
5160   /// given source.
5161   ///
5162   /// Note that the DIE must have been associated to type by a
5163   /// previous invocation of the function
5164   /// read_context::associate_die_to_type().
5165   ///
5166   /// @param die_offset the offset of the DIE to consider.
5167   ///
5168   /// @param source the source of the DIE to consider.
5169   ///
5170   /// @return the type associated to the DIE or NULL if no type is
5171   /// associated to the DIE.
5172   type_base_sptr
5173   lookup_type_from_die_offset(size_t die_offset, die_source source) const
5174   {
5175     type_base_sptr result;
5176     const die_artefact_map_type& m =
5177       type_die_artefact_maps().get_container(source);
5178     die_artefact_map_type::const_iterator i = m.find(die_offset);
5179     if (i != m.end())
5180       {
5181         if (function_decl_sptr fn = is_function_decl(i->second))
5182           return fn->get_type();
5183         result = is_type(i->second);
5184       }
5185
5186     if (!result)
5187       {
5188         // Maybe we are looking for a class type being constructed?
5189         const die_class_or_union_map_type& m = die_wip_classes_map(source);
5190         die_class_or_union_map_type::const_iterator i = m.find(die_offset);
5191
5192         if (i != m.end())
5193           result = i->second;
5194       }
5195
5196     if (!result)
5197       {
5198         // Maybe we are looking for a function type being constructed?
5199         const die_function_type_map_type& m =
5200           die_wip_function_types_map(source);
5201         die_function_type_map_type::const_iterator i = m.find(die_offset);
5202
5203         if (i != m.end())
5204           result = i->second;
5205       }
5206
5207     return result;
5208   }
5209
5210   /// Getter of a map that associates a die that represents a
5211   /// class/struct with the declaration of the class, while the class
5212   /// is being constructed.
5213   ///
5214   /// @param source where the DIE is from.
5215   ///
5216   /// @return the map that associates a DIE to the class that is being
5217   /// built.
5218   const die_class_or_union_map_type&
5219   die_wip_classes_map(die_source source) const
5220   {return const_cast<read_context*>(this)->die_wip_classes_map(source);}
5221
5222   /// Getter of a map that associates a die that represents a
5223   /// class/struct with the declaration of the class, while the class
5224   /// is being constructed.
5225   ///
5226   /// @param source where the DIE comes from.
5227   ///
5228   /// @return the map that associates a DIE to the class that is being
5229   /// built.
5230   die_class_or_union_map_type&
5231   die_wip_classes_map(die_source source)
5232   {
5233     switch (source)
5234       {
5235       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5236         break;
5237       case ALT_DEBUG_INFO_DIE_SOURCE:
5238         return alternate_die_wip_classes_map_;
5239       case TYPE_UNIT_DIE_SOURCE:
5240         return type_unit_die_wip_classes_map_;
5241       case NO_DEBUG_INFO_DIE_SOURCE:
5242       case NUMBER_OF_DIE_SOURCES:
5243         ABG_ASSERT_NOT_REACHED;
5244       }
5245     return die_wip_classes_map_;
5246   }
5247
5248   /// Getter for a map that associates a die (that represents a
5249   /// function type) whith a function type, while the function type is
5250   /// being constructed (WIP == work in progress).
5251   ///
5252   /// @param source where the DIE comes from.n
5253   ///
5254   /// @return the map of wip function types.
5255   const die_function_type_map_type&
5256   die_wip_function_types_map(die_source source) const
5257   {return const_cast<read_context*>(this)->die_wip_function_types_map(source);}
5258
5259   /// Getter for a map that associates a die (that represents a
5260   /// function type) whith a function type, while the function type is
5261   /// being constructed (WIP == work in progress).
5262   ///
5263   /// @param source where DIEs of the map come from.
5264   ///
5265   /// @return the map of wip function types.
5266   die_function_type_map_type&
5267   die_wip_function_types_map(die_source source)
5268   {
5269     switch (source)
5270       {
5271       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5272         break;
5273       case ALT_DEBUG_INFO_DIE_SOURCE:
5274         return alternate_die_wip_function_types_map_;
5275       case TYPE_UNIT_DIE_SOURCE:
5276         return type_unit_die_wip_function_types_map_;
5277       case NO_DEBUG_INFO_DIE_SOURCE:
5278       case NUMBER_OF_DIE_SOURCES:
5279         ABG_ASSERT_NOT_REACHED;
5280       }
5281     return die_wip_function_types_map_;
5282   }
5283
5284   /// Getter for a map that associates a die with a function decl
5285   /// which has a linkage name but no elf symbol yet.
5286   ///
5287   /// This is to fixup function decls with linkage names, but with no
5288   /// link to their underlying elf symbol.  There are some DIEs like
5289   /// that in DWARF sometimes, especially when the compiler optimizes
5290   /// stuff aggressively.
5291   die_function_decl_map_type&
5292   die_function_decl_with_no_symbol_map()
5293   {return die_function_with_no_symbol_map_;}
5294
5295   /// Return true iff a given offset is for the DIE of a class that is
5296   /// being built, but that is not fully built yet.  WIP == "work in
5297   /// progress".
5298   ///
5299   /// @param offset the DIE offset to consider.
5300   ///
5301   /// @param source where the DIE of the map come from.
5302   ///
5303   /// @return true iff @p offset is the offset of the DIE of a class
5304   /// that is being currently built.
5305   bool
5306   is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
5307   {
5308     die_class_or_union_map_type::const_iterator i =
5309       die_wip_classes_map(source).find(offset);
5310     return (i != die_wip_classes_map(source).end());
5311   }
5312
5313   /// Return true iff a given offset is for the DIE of a function type
5314   /// that is being built at the moment, but is not fully built yet.
5315   /// WIP == work in progress.
5316   ///
5317   /// @param offset DIE offset to consider.
5318   ///
5319   /// @param source where the DIE comes from.
5320   ///
5321   /// @return true iff @p offset is the offset of the DIE of a
5322   /// function type that is being currently built.
5323   bool
5324   is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
5325   {
5326     die_function_type_map_type::const_iterator i =
5327       die_wip_function_types_map(source).find(offset);
5328     return (i != die_wip_function_types_map(source).end());
5329   }
5330
5331   /// Getter for the map of declaration-only classes that are to be
5332   /// resolved to their definition classes by the end of the corpus
5333   /// loading.
5334   ///
5335   /// @return a map of string -> vector of classes where the key is
5336   /// the fully qualified name of the class and the value is the
5337   /// vector of declaration-only class.
5338   const string_classes_map&
5339   declaration_only_classes() const
5340   {return decl_only_classes_map_;}
5341
5342   /// Getter for the map of declaration-only classes that are to be
5343   /// resolved to their definition classes by the end of the corpus
5344   /// loading.
5345   ///
5346   /// @return a map of string -> vector of classes where the key is
5347   /// the fully qualified name of the class and the value is the
5348   /// vector of declaration-only class.
5349   string_classes_map&
5350   declaration_only_classes()
5351   {return decl_only_classes_map_;}
5352
5353   /// If a given class is a declaration-only class then stash it on
5354   /// the side so that at the end of the corpus reading we can resolve
5355   /// it to its definition.
5356   ///
5357   /// @param klass the class to consider.
5358   void
5359   maybe_schedule_declaration_only_class_for_resolution(class_decl_sptr& klass)
5360   {
5361     if (klass->get_is_declaration_only()
5362         && klass->get_definition_of_declaration() == 0)
5363       {
5364         string qn = klass->get_qualified_name();
5365         string_classes_map::iterator record =
5366           declaration_only_classes().find(qn);
5367         if (record == declaration_only_classes().end())
5368           declaration_only_classes()[qn].push_back(klass);
5369         else
5370           record->second.push_back(klass);
5371       }
5372   }
5373
5374   /// Test if a given declaration-only class has been scheduled for
5375   /// resolution to a defined class.
5376   ///
5377   /// @param klass the class to consider for the test.
5378   ///
5379   /// @return true iff @p klass is a declaration-only class and if
5380   /// it's been scheduled for resolution to a defined class.
5381   bool
5382   is_decl_only_class_scheduled_for_resolution(class_decl_sptr& klass)
5383   {
5384     if (klass->get_is_declaration_only())
5385       return (declaration_only_classes().find(klass->get_qualified_name())
5386               != declaration_only_classes().end());
5387
5388     return false;
5389   }
5390
5391   /// Walk the declaration-only classes that have been found during
5392   /// the building of the corpus and resolve them to their definitions.
5393   void
5394   resolve_declaration_only_classes()
5395   {
5396     vector<string> resolved_classes;
5397
5398     for (string_classes_map::iterator i =
5399            declaration_only_classes().begin();
5400          i != declaration_only_classes().end();
5401          ++i)
5402       {
5403         bool to_resolve = false;
5404         for (classes_type::iterator j = i->second.begin();
5405              j != i->second.end();
5406              ++j)
5407           if ((*j)->get_is_declaration_only()
5408               && ((*j)->get_definition_of_declaration() == 0))
5409             to_resolve = true;
5410
5411         if (!to_resolve)
5412           {
5413             resolved_classes.push_back(i->first);
5414             continue;
5415           }
5416
5417         // Now, for each decl-only class that have the current name
5418         // 'i->first', let's try to poke at the fully defined class
5419         // that is defined in the same translation unit as the
5420         // declaration.
5421         //
5422         // If we find one class (defined in the TU of the declaration)
5423         // that defines the declaration, then the declaration can be
5424         // resolved to that class.
5425         //
5426         // If no defining class is found in the TU of the declaration,
5427         // then there are possibly three cases to consider:
5428         //
5429         //   1/ There is exactly one class that defines the
5430         //   declaration and that class is defined in another TU.  In
5431         //   this case, the declaration is resolved to that
5432         //   definition.
5433         //
5434         //   2/ There are more than one class that define that
5435         //   declaration and none of them is defined in the TU of the
5436         //   declaration.  In this case, the declaration is left
5437         //   unresolved.
5438         //
5439         //   3/ No class defines the declaration.  In this case, the
5440         //   declaration is left unresoved.
5441
5442         // So get the classes that might define the current
5443         // declarations which name is i->first.
5444         const type_base_wptrs_type *classes =
5445           lookup_class_types(i->first, *current_corpus());
5446         if (!classes)
5447           continue;
5448
5449         unordered_map<string, class_decl_sptr> per_tu_class_map;
5450         for (type_base_wptrs_type::const_iterator c = classes->begin();
5451              c != classes->end();
5452              ++c)
5453           {
5454             class_decl_sptr klass = is_class_type(type_base_sptr(*c));
5455             ABG_ASSERT(klass);
5456
5457             klass = is_class_type(look_through_decl_only_class(klass));
5458             if (klass->get_is_declaration_only())
5459               continue;
5460
5461             string tu_path = klass->get_translation_unit()->get_absolute_path();
5462             if (tu_path.empty())
5463               continue;
5464
5465             // Build a map that associates the translation unit path
5466             // to the class (that potentially defines the declarations
5467             // that we consider) that are defined in that translation unit.
5468             per_tu_class_map[tu_path] = klass;
5469           }
5470
5471         if (!per_tu_class_map.empty())
5472           {
5473             // Walk the declarations to resolve and resolve them
5474             // either to the definitions that are in the same TU as
5475             // the declaration, or to the definition found elsewhere,
5476             // if there is only one such definition.
5477             for (classes_type::iterator j = i->second.begin();
5478                  j != i->second.end();
5479                  ++j)
5480               {
5481                 if ((*j)->get_is_declaration_only()
5482                     && ((*j)->get_definition_of_declaration() == 0))
5483                   {
5484                     string tu_path =
5485                       (*j)->get_translation_unit()->get_absolute_path();
5486                     unordered_map<string, class_decl_sptr>::const_iterator e =
5487                       per_tu_class_map.find(tu_path);
5488                     if (e != per_tu_class_map.end())
5489                       (*j)->set_definition_of_declaration(e->second);
5490                     else if (per_tu_class_map.size() == 1)
5491                       (*j)->set_definition_of_declaration
5492                         (per_tu_class_map.begin()->second);
5493                   }
5494               }
5495             resolved_classes.push_back(i->first);
5496           }
5497       }
5498
5499     size_t num_decl_only_classes = declaration_only_classes().size(),
5500       num_resolved = resolved_classes.size();
5501     if (show_stats())
5502       cerr << "resolved " << num_resolved
5503            << " class declarations out of "
5504            << num_decl_only_classes
5505            << "\n";
5506
5507     for (vector<string>::const_iterator i = resolved_classes.begin();
5508          i != resolved_classes.end();
5509          ++i)
5510       declaration_only_classes().erase(*i);
5511
5512     for (string_classes_map::iterator i = declaration_only_classes().begin();
5513          i != declaration_only_classes().end();
5514          ++i)
5515       {
5516         if (show_stats())
5517           {
5518             if (i == declaration_only_classes().begin())
5519               cerr << "Here are the "
5520                    << num_decl_only_classes - num_resolved
5521                    << " unresolved class declarations:\n";
5522             else
5523               cerr << "    " << i->first << "\n";
5524           }
5525       }
5526   }
5527
5528   /// Some functions described by DWARF may have their linkage name
5529   /// set, but no link to their actual underlying elf symbol.  When
5530   /// these are virtual member functions, comparing the enclosing type
5531   /// against another one which has its underlying symbol properly set
5532   /// might lead to spurious type changes.
5533   ///
5534   /// If the corpus contains a symbol with the same name as the
5535   /// linkage name of the function, then set up the link between the
5536   /// function and its underlying symbol.
5537   ///
5538   /// Note that for the moment, only virtual member functions are
5539   /// fixed up like this.  This is because they really are the only
5540   /// fuctions of functions that can affect types (in spurious ways).
5541   void
5542   fixup_functions_with_no_symbols()
5543   {
5544     corpus_sptr corp = current_corpus();
5545     if (!corp)
5546       return;
5547
5548     die_function_decl_map_type &fns_with_no_symbol =
5549       die_function_decl_with_no_symbol_map();
5550
5551     if (do_log())
5552       cerr << fns_with_no_symbol.size()
5553            << " functions to fixup, potentially\n";
5554
5555     for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
5556          i != fns_with_no_symbol.end();
5557          ++i)
5558       if (elf_symbol_sptr sym =
5559           corp->lookup_function_symbol(i->second->get_linkage_name()))
5560         {
5561           ABG_ASSERT(is_member_function(i->second));
5562           ABG_ASSERT(get_member_function_is_virtual(i->second));
5563           i->second->set_symbol(sym);
5564           if (do_log())
5565             cerr << "fixed up '"
5566                  << i->second->get_pretty_representation()
5567                  << "' with symbol '"
5568                  << sym->get_id_string()
5569                  << "'\n";
5570         }
5571
5572     fns_with_no_symbol.clear();
5573   }
5574
5575   /// Return a reference to the vector containing the offsets of the
5576   /// types that need late canonicalizing.
5577   ///
5578   /// @param source whe DIEs referred to by the offsets contained in
5579   /// the vector to return are from.
5580   vector<Dwarf_Off>&
5581   types_to_canonicalize(die_source source)
5582   {
5583     switch (source)
5584       {
5585       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5586         break;
5587       case ALT_DEBUG_INFO_DIE_SOURCE:
5588         return alt_types_to_canonicalize_;
5589       case TYPE_UNIT_DIE_SOURCE:
5590         return type_unit_types_to_canonicalize_;
5591       case NO_DEBUG_INFO_DIE_SOURCE:
5592       case NUMBER_OF_DIE_SOURCES:
5593         ABG_ASSERT_NOT_REACHED;
5594       }
5595     return types_to_canonicalize_;
5596   }
5597
5598   /// Return a reference to the vector containing the offsets of the
5599   /// types that need late canonicalizing.
5600   ///
5601   /// @param source where the DIEs referred to by the offset in the
5602   /// returned vector are from.
5603   const vector<Dwarf_Off>&
5604   types_to_canonicalize(die_source source) const
5605   {return const_cast<read_context*>(this)->types_to_canonicalize(source);}
5606
5607   /// Return a reference to the vector containing the types created
5608   /// during the binary analysis but that are not tied to a given
5609   /// DWARF DIE.
5610   ///
5611   /// @return reference to the vector containing the types created
5612   /// during the binary analysis but that are not tied to a given
5613   /// DWARF DIE.
5614   const vector<type_base_sptr>&
5615   extra_types_to_canonicalize() const
5616   {return extra_types_to_canonicalize_;}
5617
5618   /// Clear the containers holding types to canonicalize.
5619   void
5620   clear_types_to_canonicalize()
5621   {
5622     types_to_canonicalize_.clear();
5623     alt_types_to_canonicalize_.clear();
5624     type_unit_types_to_canonicalize_.clear();
5625     extra_types_to_canonicalize_.clear();
5626   }
5627
5628   /// Put the offset of a DIE representing a type on a side vector so
5629   /// that when the reading of the debug info of the current
5630   /// translation unit is done, we can get back to the type DIE and
5631   /// from there, to the type it's associated to, and then
5632   /// canonicalize it.  This what we call late canonicalization.
5633   ///
5634   /// @param die the type DIE to schedule for late type
5635   /// canonicalization.
5636   void
5637   schedule_type_for_late_canonicalization(const Dwarf_Die *die)
5638   {
5639     Dwarf_Off o;
5640     die_source source;
5641
5642     Dwarf_Die equiv_die;
5643     ABG_ASSERT(get_canonical_die(die, equiv_die,
5644                                   /*where=*/0,
5645                                  /*die_as_type=*/true));
5646
5647     ABG_ASSERT(get_die_source(&equiv_die, source));
5648     o = dwarf_dieoffset(&equiv_die);
5649
5650     const die_artefact_map_type& m =
5651       type_die_artefact_maps().get_container(*this, die);
5652
5653     die_artefact_map_type::const_iterator i = m.find(o);
5654     ABG_ASSERT(i != m.end());
5655
5656     // Then really do the scheduling.
5657     types_to_canonicalize(source).push_back(o);
5658   }
5659
5660   /// Types that were created but not tied to a particular DIE, must
5661   /// be scheduled for late canonicalization using this method.
5662   ///
5663   /// @param t the type to schedule for late canonicalization.
5664   void
5665   schedule_type_for_late_canonicalization(const type_base_sptr &t)
5666   {
5667     extra_types_to_canonicalize_.push_back(t);
5668   }
5669
5670   /// Canonicalize types which DIE offsets are stored in vectors on
5671   /// the side.  This is a sub-routine of
5672   /// read_context::perform_late_type_canonicalizing().
5673   ///
5674   /// @param source where the DIE of the types to canonicalize are
5675   /// from.
5676   void
5677   canonicalize_types_scheduled(die_source source)
5678   {
5679     tools_utils::timer cn_timer;
5680     if (do_log())
5681       {
5682         cerr << "going to canonicalize types";
5683         corpus_sptr c = current_corpus();
5684         if (c)
5685           cerr << " of corpus " << current_corpus()->get_path();
5686         cerr << " (DIEs source: " << source << ")\n";
5687         cn_timer.start();
5688       }
5689
5690     if (!types_to_canonicalize(source).empty())
5691       {
5692         tools_utils::timer single_type_cn_timer;
5693         size_t total = types_to_canonicalize(source).size();
5694         if (do_log())
5695           cerr << total << " types to canonicalize\n";
5696         for (size_t i = 0; i < total; ++i)
5697           {
5698             Dwarf_Off element = types_to_canonicalize(source)[i];
5699             type_base_sptr t =
5700               lookup_type_from_die_offset(element, source);
5701             ABG_ASSERT(t);
5702             if (do_log())
5703               {
5704                 cerr << "canonicalizing type "
5705                      << get_pretty_representation(t, false)
5706                      << " [" << i + 1 << "/" << total << "]";
5707                 if (corpus_sptr c = current_corpus())
5708                   cerr << "@" << c->get_path();
5709                 cerr << " ...";
5710                 single_type_cn_timer.start();
5711               }
5712             canonicalize(t);
5713             if (do_log())
5714               {
5715                 cerr << " DONE";
5716                 single_type_cn_timer.stop();
5717                 cerr << ":" <<single_type_cn_timer << "\n";
5718               }
5719           }
5720
5721         // Now canonicalize types that were created but not tied to
5722         // any DIE.
5723         if (!extra_types_to_canonicalize().empty())
5724           {
5725             tools_utils::timer single_type_cn_timer;
5726             size_t total = extra_types_to_canonicalize().size();
5727             if (do_log())
5728               cerr << total << " extra types to canonicalize\n";
5729             size_t i = 1;
5730             for (vector<type_base_sptr>::const_iterator it =
5731                    extra_types_to_canonicalize().begin();
5732                  it != extra_types_to_canonicalize().end();
5733                  ++it, ++i)
5734               {
5735                 if (do_log())
5736                   {
5737                     cerr << "canonicalizing extra type "
5738                          << get_pretty_representation(*it, false)
5739                          << " [" << i << "/" << total << "]";
5740                     if (corpus_sptr c = current_corpus())
5741                       cerr << "@" << c->get_path();
5742                     cerr << " ...";
5743                     single_type_cn_timer.start();
5744                   }
5745                 canonicalize(*it);
5746                 if (do_log())
5747                   {
5748                     single_type_cn_timer.stop();
5749                     cerr << "DONE:"
5750                          << single_type_cn_timer
5751                          << "\n";
5752                   }
5753               }
5754           }
5755       }
5756
5757     if (do_log())
5758       {
5759         cn_timer.stop();
5760         cerr << "finished canonicalizing types";
5761         corpus_sptr c = current_corpus();
5762         if (c)
5763           cerr << " of corpus " << current_corpus()->get_path();
5764         cerr << " (DIEs source: "
5765              << source << "):"
5766              << cn_timer
5767              << "\n";
5768       }
5769   }
5770
5771   /// Compute the number of canonicalized and missed types in the late
5772   /// canonicalization phase.
5773   ///
5774   /// @param source where the DIEs of the canonicalized types are
5775   /// from.
5776   ///
5777   /// @param canonicalized the number of types that got canonicalized
5778   /// is added to the value already present in this parameter.
5779   ///
5780   /// @param missed the number of types scheduled for late
5781   /// canonicalization and which couldn't be canonicalized (for a
5782   /// reason) is added to the value already present in this parameter.
5783   void
5784   add_late_canonicalized_types_stats(die_source source,
5785                                      size_t&            canonicalized,
5786                                      size_t&            missed) const
5787   {
5788     for (vector<Dwarf_Off>::const_iterator i =
5789            types_to_canonicalize(source).begin();
5790          i != types_to_canonicalize(source).end();
5791          ++i)
5792       {
5793         type_base_sptr t = lookup_type_from_die_offset(*i, source);
5794         if (t->get_canonical_type())
5795           ++canonicalized;
5796         else
5797           ++missed;
5798       }
5799   }
5800
5801   /// Compute the number of canonicalized and missed types in the late
5802   /// canonicalization phase.
5803   ///
5804   /// @param canonicalized the number of types that got canonicalized
5805   /// is added to the value already present in this parameter.
5806   ///
5807   /// @param missed the number of types scheduled for late
5808   /// canonicalization and which couldn't be canonicalized (for a
5809   /// reason) is added to the value already present in this parameter.
5810   void
5811   add_late_canonicalized_types_stats(size_t& canonicalized,
5812                                      size_t& missed) const
5813   {
5814     for (die_source source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5815          source < NUMBER_OF_DIE_SOURCES;
5816          ++source)
5817       add_late_canonicalized_types_stats(source, canonicalized, missed);
5818   }
5819
5820   // Look at the types that need to be canonicalized after the
5821   // translation unit has been constructed and canonicalize them.
5822   void
5823   perform_late_type_canonicalizing()
5824   {
5825     for (die_source source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5826          source < NUMBER_OF_DIE_SOURCES;
5827          ++source)
5828       canonicalize_types_scheduled(source);
5829
5830     if (show_stats())
5831       {
5832         size_t num_canonicalized = 0, num_missed = 0, total = 0;
5833         add_late_canonicalized_types_stats(num_canonicalized,
5834                                            num_missed);
5835         total = num_canonicalized + num_missed;
5836         cerr << "binary: "
5837              << elf_path()
5838              << "\n";
5839         cerr << "    # late canonicalized types: "
5840              << num_canonicalized
5841              << " (" << num_canonicalized * 100 / total << "%)\n"
5842              << "    # missed canonicalization opportunities: "
5843              << num_missed
5844              << " (" << num_missed * 100 / total << "%)\n";
5845       }
5846
5847   }
5848
5849   const die_tu_map_type&
5850   die_tu_map() const
5851   {return die_tu_map_;}
5852
5853   die_tu_map_type&
5854   die_tu_map()
5855   {return die_tu_map_;}
5856
5857   /// Getter for the map that associates a translation unit DIE to the
5858   /// vector of imported unit points that it contains.
5859   ///
5860   /// @param source where the DIEs are from.
5861   ///
5862   /// @return the map.
5863   const tu_die_imported_unit_points_map_type&
5864   tu_die_imported_unit_points_map(die_source source) const
5865   {return const_cast<read_context*>(this)->tu_die_imported_unit_points_map(source);}
5866
5867   /// Getter for the map that associates a translation unit DIE to the
5868   /// vector of imported unit points that it contains.
5869   ///
5870   /// @param source where the DIEs are from.
5871   ///
5872   /// @return the map.
5873   tu_die_imported_unit_points_map_type&
5874   tu_die_imported_unit_points_map(die_source source)
5875   {
5876     switch (source)
5877       {
5878       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5879         break;
5880       case ALT_DEBUG_INFO_DIE_SOURCE:
5881         return alt_tu_die_imported_unit_points_map_;
5882       case TYPE_UNIT_DIE_SOURCE:
5883         return type_units_tu_die_imported_unit_points_map_;
5884       case NO_DEBUG_INFO_DIE_SOURCE:
5885       case NUMBER_OF_DIE_SOURCES:
5886         // We cannot reach this point.
5887         ABG_ASSERT_NOT_REACHED;
5888       }
5889     return tu_die_imported_unit_points_map_;
5890   }
5891
5892   /// Getter of the current corpus being constructed.
5893   ///
5894   /// @return the current corpus.
5895   const corpus_sptr
5896   current_corpus() const
5897   {return cur_corpus_;}
5898
5899   /// Getter of the current corpus being constructed.
5900   ///
5901   /// @return the current corpus.
5902   corpus_sptr
5903   current_corpus()
5904   {return cur_corpus_;}
5905
5906   /// Setter of the current corpus being constructed.
5907   ///
5908   /// @param c the new corpus.
5909   void
5910   current_corpus(const corpus_sptr& c)
5911   {
5912     if (c)
5913       cur_corpus_ = c;
5914   }
5915
5916   /// Reset the current corpus being constructed.
5917   ///
5918   /// This actually deletes the current corpus being constructed.
5919   void
5920   reset_current_corpus()
5921   {cur_corpus_.reset();}
5922
5923   /// Getter of the current corpus group being constructed.
5924   ///
5925   /// @return current the current corpus being constructed, if any, or
5926   /// nil.
5927   const corpus_group_sptr
5928   current_corpus_group() const
5929   {return cur_corpus_group_;}
5930
5931   /// Getter of the current corpus group being constructed.
5932   ///
5933   /// @return current the current corpus being constructed, if any, or
5934   /// nil.
5935   corpus_group_sptr
5936   current_corpus_group()
5937   {return cur_corpus_group_;}
5938
5939   /// Setter of the current corpus group being constructed.
5940   ///
5941   /// @param g the new corpus group.
5942   void
5943   current_corpus_group(const corpus_group_sptr& g)
5944   {
5945     if (g)
5946       cur_corpus_group_ = g;
5947   }
5948
5949   /// Test if there is a corpus group being built.
5950   ///
5951   /// @return if there is a corpus group being built, false otherwise.
5952   bool
5953   has_corpus_group() const
5954   {return bool(cur_corpus_group_);}
5955
5956   /// Return the main corpus from the current corpus group, if any.
5957   ///
5958   /// @return the main corpus of the current corpus group, if any, nil
5959   /// if no corpus group is being constructed.
5960   corpus_sptr
5961   main_corpus_from_current_group()
5962   {
5963     if (cur_corpus_group_)
5964       return cur_corpus_group_->get_main_corpus();
5965     return corpus_sptr();
5966   }
5967
5968   /// Return the main corpus from the current corpus group, if any.
5969   ///
5970   /// @return the main corpus of the current corpus group, if any, nil
5971   /// if no corpus group is being constructed.
5972   const corpus_sptr
5973   main_corpus_from_current_group() const
5974   {return const_cast<read_context*>(this)->main_corpus_from_current_group();}
5975
5976   /// Test if the current corpus being built is the main corpus of the
5977   /// current corpus group.
5978   ///
5979   /// @return return true iff the current corpus being built is the
5980   /// main corpus of the current corpus group.
5981   bool
5982   current_corpus_is_main_corpus_from_current_group() const
5983   {
5984     corpus_sptr main_corpus = main_corpus_from_current_group();
5985
5986     if (main_corpus && main_corpus.get() == cur_corpus_.get())
5987       return true;
5988
5989     return false;
5990   }
5991
5992   /// Return true if the current corpus is part of a corpus group
5993   /// being built and if it's not the main corpus of the group.
5994   ///
5995   /// For instance, this would return true if we are loading a linux
5996   /// kernel *module* that is part of the current corpus group that is
5997   /// being built.  In this case, it means we should re-use types
5998   /// coming from the "vmlinux" binary that is the main corpus of the
5999   /// group.
6000   ///
6001   /// @return the corpus group the current corpus belongs to, if the
6002   /// current corpus is part of a corpus group being built. Nil otherwise.
6003   corpus_sptr
6004   should_reuse_type_from_corpus_group() const
6005   {
6006     if (has_corpus_group() && is_c_language(cur_transl_unit()->get_language()))
6007       if (corpus_sptr main_corpus = main_corpus_from_current_group())
6008         if (!current_corpus_is_main_corpus_from_current_group())
6009           return current_corpus_group();
6010
6011     return corpus_sptr();
6012   }
6013
6014   /// Get the map that associates each DIE to its parent DIE.  This is
6015   /// for DIEs coming from the main debug info sections.
6016   ///
6017   /// @param source where the DIEs in the map come from.
6018   ///
6019   /// @return the DIE -> parent map.
6020   const offset_offset_map_type&
6021   die_parent_map(die_source source) const
6022   {return const_cast<read_context*>(this)->die_parent_map(source);}
6023
6024   /// Get the map that associates each DIE to its parent DIE.  This is
6025   /// for DIEs coming from the main debug info sections.
6026   ///
6027   /// @param source where the DIEs in the map come from.
6028   ///
6029   /// @return the DIE -> parent map.
6030   offset_offset_map_type&
6031   die_parent_map(die_source source)
6032   {
6033     switch (source)
6034       {
6035       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
6036         break;
6037       case ALT_DEBUG_INFO_DIE_SOURCE:
6038         return alternate_die_parent_map_;
6039       case TYPE_UNIT_DIE_SOURCE:
6040         return type_section_die_parent_map();
6041       case NO_DEBUG_INFO_DIE_SOURCE:
6042       case NUMBER_OF_DIE_SOURCES:
6043         ABG_ASSERT_NOT_REACHED;
6044       }
6045     return primary_die_parent_map_;
6046   }
6047
6048   const offset_offset_map_type&
6049   type_section_die_parent_map() const
6050   {return type_section_die_parent_map_;}
6051
6052   offset_offset_map_type&
6053   type_section_die_parent_map()
6054   {return type_section_die_parent_map_;}
6055
6056   /// Getter of the current translation unit.
6057   ///
6058   /// @return the current translation unit being constructed.
6059   const translation_unit_sptr&
6060   cur_transl_unit() const
6061   {return cur_tu_;}
6062
6063   /// Getter of the current translation unit.
6064   ///
6065   /// @return the current translation unit being constructed.
6066   translation_unit_sptr&
6067   cur_transl_unit()
6068   {return cur_tu_;}
6069
6070   /// Setter of the current translation unit.
6071   ///
6072   /// @param tu the current translation unit being constructed.
6073   void
6074   cur_transl_unit(translation_unit_sptr tu)
6075   {
6076     if (tu)
6077       cur_tu_ = tu;
6078   }
6079
6080   /// Return the global scope of the current translation unit.
6081   ///
6082   /// @return the global scope of the current translation unit.
6083   const scope_decl_sptr&
6084   global_scope() const
6085   {return cur_transl_unit()->get_global_scope();}
6086
6087   /// Return a scope that is nil.
6088   ///
6089   /// @return a scope that is nil.
6090   const scope_decl_sptr&
6091   nil_scope() const
6092   {return nil_scope_;}
6093
6094   const scope_stack_type&
6095   scope_stack() const
6096   {return scope_stack_;}
6097
6098   scope_stack_type&
6099   scope_stack()
6100   {return scope_stack_;}
6101
6102   scope_decl*
6103   current_scope()
6104   {
6105     if (scope_stack().empty())
6106       {
6107         if (cur_transl_unit())
6108           scope_stack().push(cur_transl_unit()->get_global_scope().get());
6109       }
6110     return scope_stack().top();
6111   }
6112
6113   list<var_decl_sptr>&
6114   var_decls_to_re_add_to_tree()
6115   {return var_decls_to_add_;}
6116
6117   /// Return the type of the current elf file.
6118   ///
6119   /// @return the type of the current elf file.
6120   elf_type
6121   get_elf_file_type()
6122   {
6123     return elf_file_type(elf_handle());
6124   }
6125
6126   /// The section containing the symbol table from the current ELF
6127   /// file.
6128   ///
6129   /// Note that after it's first invocation, this function caches the
6130   /// symbol table that it found.  Subsequent invocations just return
6131   /// the cached symbol table section.
6132   ///
6133   /// @return the symbol table section if found
6134   Elf_Scn*
6135   find_symbol_table_section() const
6136   {
6137     if (!symtab_section_)
6138       dwarf_reader::find_symbol_table_section(elf_handle(),
6139                                               const_cast<read_context*>(this)->symtab_section_);
6140     return symtab_section_;
6141   }
6142
6143   /// Return the "Official Procedure descriptors section."  This
6144   /// section is named .opd, and is usually present only on PPC64
6145   /// ELFv1 binaries.
6146   ///
6147   /// @return the .opd section, if found.  Return nil otherwise.
6148   Elf_Scn*
6149   find_opd_section() const
6150   {
6151     if (!opd_section_)
6152       const_cast<read_context*>(this)->opd_section_=
6153         find_section(elf_handle(), ".opd", SHT_PROGBITS);
6154     return opd_section_;
6155   }
6156
6157   /// Return the __ksymtab section of a linux kernel ELF file (either
6158   /// a vmlinux binary or a kernel module).
6159   ///
6160   /// @return the __ksymtab section if found, nil otherwise.
6161   Elf_Scn*
6162   find_ksymtab_section() const
6163   {
6164     if (!ksymtab_section_)
6165       const_cast<read_context*>(this)->ksymtab_section_ =
6166         find_section(elf_handle(), "__ksymtab", SHT_PROGBITS);
6167     return ksymtab_section_;
6168   }
6169
6170   /// Return the .rel{a,}__ksymtab section of a linux kernel ELF file (either
6171   /// a vmlinux binary or a kernel module).
6172   ///
6173   /// @return the .rel{a,}__ksymtab section if found, nil otherwise.
6174   Elf_Scn*
6175   find_ksymtab_reloc_section() const
6176   {
6177     if (!ksymtab_reloc_section_)
6178       {
6179         Elf_Scn *sec = find_section(elf_handle(), ".rela__ksymtab", SHT_RELA);
6180         if (!sec)
6181           sec = find_section(elf_handle(), ".rel__ksymtab", SHT_REL);
6182         const_cast<read_context*>(this)->ksymtab_reloc_section_ = sec;
6183       }
6184     return ksymtab_reloc_section_;
6185   }
6186
6187   /// Return the __ksymtab_gpl section of a linux kernel ELF file
6188   /// (either a vmlinux binary or a kernel module).
6189   ///
6190   /// @return the __ksymtab_gpl section if found, nil otherwise.
6191   Elf_Scn*
6192   find_ksymtab_gpl_section() const
6193   {
6194     if (!ksymtab_gpl_section_)
6195       const_cast<read_context*>(this)->ksymtab_gpl_section_ =
6196         find_section(elf_handle(), "__ksymtab_gpl", SHT_PROGBITS);
6197     return ksymtab_gpl_section_;
6198   }
6199
6200   /// Return the .rel{a,}__ksymtab_gpl section of a linux kernel ELF file
6201   /// (either a vmlinux binary or a kernel module).
6202   ///
6203   /// @return the .rel{a,}__ksymtab_gpl section if found, nil otherwise.
6204   Elf_Scn*
6205   find_ksymtab_gpl_reloc_section() const
6206   {
6207     if (!ksymtab_gpl_reloc_section_)
6208       {
6209         Elf_Scn *sec = find_section(elf_handle(), ".rela__ksymtab_gpl", SHT_RELA);
6210         if (!sec)
6211           sec = find_section(elf_handle(), ".rel__ksymtab_gpl", SHT_REL);
6212         const_cast<read_context*>(this)->ksymtab_gpl_reloc_section_ = sec;
6213       }
6214     return ksymtab_gpl_reloc_section_;
6215   }
6216
6217   /// Return the __ksymtab_strings section of a linux kernel ELF file
6218   /// (either a vmlinux binary or a kernel module).
6219   ///
6220   /// @return the __ksymtab_strings section if found, nil otherwise.
6221   Elf_Scn*
6222   find_ksymtab_strings_section() const
6223   {
6224     if (!ksymtab_strings_section_)
6225       const_cast<read_context*>(this)->ksymtab_strings_section_ =
6226         dwarf_reader::find_ksymtab_strings_section(elf_handle());
6227     return ksymtab_strings_section_;
6228   }
6229
6230   /// Return either a __ksymtab or a __ksymtab_gpl section, in case
6231   /// only the __ksymtab_gpl exists.
6232   ///
6233   /// @return the __ksymtab section if it exists, or the
6234   /// __ksymtab_gpl; or NULL if neither is found.
6235   Elf_Scn*
6236   find_any_ksymtab_section() const
6237   {
6238     Elf_Scn *result = find_ksymtab_section();
6239     if (!result)
6240       result = find_ksymtab_gpl_section();
6241     return result;
6242   }
6243
6244   /// Return either a .rel{a,}__ksymtab or a .rel{a,}__ksymtab_gpl section
6245   ///
6246   /// @return the .rel{a,}__ksymtab section if it exists, or the
6247   /// .rel{a,}__ksymtab_gpl; or NULL if neither is found.
6248   Elf_Scn*
6249   find_any_ksymtab_reloc_section() const
6250   {
6251     Elf_Scn *result = find_ksymtab_reloc_section();
6252     if (!result)
6253       result = find_ksymtab_gpl_reloc_section();
6254     return result;
6255   }
6256
6257   /// Return the SHT_GNU_versym, SHT_GNU_verdef and SHT_GNU_verneed
6258   /// sections that are involved in symbol versionning.
6259   ///
6260   /// @param versym_section the SHT_GNU_versym section found.
6261   ///
6262   /// @param verdef_section the SHT_GNU_verdef section found.
6263   ///
6264   /// @param verneed_section the SHT_GNU_verneed section found.
6265   ///
6266   /// @return true iff the sections where found.
6267   bool
6268   get_symbol_versionning_sections(Elf_Scn*&     versym_section,
6269                                   Elf_Scn*&     verdef_section,
6270                                   Elf_Scn*&     verneed_section)
6271   {
6272     if (!symbol_versionning_sections_loaded_)
6273       {
6274         symbol_versionning_sections_found_ =
6275           dwarf_reader::get_symbol_versionning_sections(elf_handle(),
6276                                                         versym_section_,
6277                                                         verdef_section_,
6278                                                         verneed_section_);
6279         symbol_versionning_sections_loaded_ = true;
6280       }
6281
6282     versym_section = versym_section_;
6283     verdef_section = verdef_section_;
6284     verneed_section = verneed_section_;
6285     return symbol_versionning_sections_found_;
6286   }
6287
6288   /// Return the version for a symbol that is at a given index in its
6289   /// SHT_SYMTAB section.
6290   ///
6291   /// The first invocation of this function caches the results and
6292   /// subsequent invocations just return the cached results.
6293   ///
6294   /// @param symbol_index the index of the symbol to consider.
6295   ///
6296   /// @param get_def_version if this is true, it means that that we want
6297   /// the version for a defined symbol; in that case, the version is
6298   /// looked for in a section of type SHT_GNU_verdef.  Otherwise, if
6299   /// this parameter is false, this means that we want the version for
6300   /// an undefined symbol; in that case, the version is the needed one
6301   /// for the symbol to be resolved; so the version is looked fo in a
6302   /// section of type SHT_GNU_verneed.
6303   ///
6304   /// @param version the version found for symbol at @p symbol_index.
6305   ///
6306   /// @return true iff a version was found for symbol at index @p
6307   /// symbol_index.
6308   bool
6309   get_version_for_symbol(size_t         symbol_index,
6310                          bool                   get_def_version,
6311                          elf_symbol::version&   version)
6312   {
6313     Elf_Scn *versym_section = NULL,
6314       *verdef_section = NULL,
6315       *verneed_section = NULL;
6316
6317     if (!get_symbol_versionning_sections(versym_section,
6318                                          verdef_section,
6319                                          verneed_section))
6320       return false;
6321
6322     GElf_Versym versym_mem;
6323     Elf_Data* versym_data = (versym_section)
6324       ? elf_getdata(versym_section, NULL)
6325       : NULL;
6326     GElf_Versym* versym = (versym_data)
6327       ? gelf_getversym(versym_data, symbol_index, &versym_mem)
6328       : NULL;
6329
6330     if (versym == 0 || *versym <= 1)
6331       // I got these value from the code of readelf.c in elfutils.
6332       // Apparently, if the symbol version entry has these values, the
6333       // symbol must be discarded. This is not documented in the
6334       // official specification.
6335       return false;
6336
6337     if (get_def_version)
6338       {
6339         if (*versym == 0x8001)
6340           // I got this value from the code of readelf.c in elfutils
6341           // too.  It's not really documented in the official
6342           // specification.
6343           return false;
6344
6345         if (verdef_section
6346             && get_version_definition_for_versym(elf_handle(), versym,
6347                                                  verdef_section, version))
6348           return true;
6349       }
6350     else
6351       {
6352         if (verneed_section
6353             && get_version_needed_for_versym(elf_handle(), versym,
6354                                              verneed_section, version))
6355           return true;
6356       }
6357
6358     return false;
6359   }
6360
6361   /// Look into the symbol tables of the underlying elf file and see
6362   /// if we find a given symbol.
6363   ///
6364   /// @param symbol_name the name of the symbol to look for.
6365   ///
6366   /// @param demangle if true, demangle the symbols found in the symbol
6367   /// tables.
6368   ///
6369   /// @param syms the vector of symbols with the name @p symbol_name
6370   /// that were found.
6371   ///
6372   /// @return true iff the symbol was found.
6373   bool
6374   lookup_symbol_from_elf(const string&                  symbol_name,
6375                          bool                           demangle,
6376                          vector<elf_symbol_sptr>&       syms) const
6377   {
6378     return dwarf_reader::lookup_symbol_from_elf(env(),
6379                                                 elf_handle(),
6380                                                 symbol_name,
6381                                                 demangle,
6382                                                 syms);
6383   }
6384
6385   /// Lookup an elf symbol, referred to by its index, from the .symtab
6386   /// section.
6387   ///
6388   /// The resulting symbol returned is an instance of a GElf_Sym, from
6389   /// the libelf library.
6390   ///
6391   /// @param symbol_index the index of the symbol to look up.
6392   ///
6393   /// @param elf_sym out parameter.  This is set to the resulting ELF
6394   /// symbol iff the function returns TRUE, meaning the symbol was
6395   /// found.
6396   ///
6397   /// @return TRUE iff the symbol was found.
6398   bool
6399   lookup_native_elf_symbol_from_index(size_t symbol_index, GElf_Sym &elf_sym)
6400   {
6401     Elf_Scn* symtab_section = find_symbol_table_section();
6402     if (!symtab_section)
6403       return false;
6404
6405     Elf_Data* symtab = elf_getdata(symtab_section, 0);
6406     ABG_ASSERT(symtab);
6407
6408     if (!gelf_getsym(symtab, symbol_index, &elf_sym))
6409       return false;
6410
6411     return true;
6412   }
6413
6414   /// Given the index of a symbol into the symbol table of an ELF
6415   /// file, look the symbol up, build an instace of @ref elf_symbol
6416   /// and return it.
6417   ///
6418   /// @param symbol_index the index of the symbol into the symbol
6419   /// table of the current elf file.
6420   ///
6421   /// @return the elf symbol found or nil if none was found.
6422   elf_symbol_sptr
6423   lookup_elf_symbol_from_index(size_t symbol_index)
6424   {
6425     GElf_Sym s;
6426     elf_symbol_sptr result =
6427       lookup_elf_symbol_from_index(symbol_index, s);
6428     return result;
6429   }
6430
6431   /// Lookup an ELF symbol given its index into the .symtab section.
6432   ///
6433   /// This function returns both the native symbol (from libelf) and
6434   /// the @p abigail::ir::elf_symbol instance, which is the
6435   /// libabigail-specific representation of the symbol.
6436   ///
6437   /// @param symbol_index the index of the symbol to look for.
6438   ///
6439   /// @param native_sym output parameter.  This is set to the native
6440   /// ELF symbol found iff the function returns a non-nil value.
6441   ///
6442   /// @return an instance of libabigail::ir::elf_symbol representing
6443   /// the ELF symbol found, iff one was found.  Otherwise, returns
6444   /// nil.
6445   elf_symbol_sptr
6446   lookup_elf_symbol_from_index(size_t symbol_index,
6447                                GElf_Sym &native_sym)
6448   {
6449     if (!lookup_native_elf_symbol_from_index(symbol_index, native_sym))
6450       return elf_symbol_sptr();
6451
6452     Elf_Scn* symtab_section = find_symbol_table_section();
6453     if (!symtab_section)
6454       return elf_symbol_sptr();
6455
6456     GElf_Shdr header_mem;
6457     GElf_Shdr* symtab_sheader = gelf_getshdr(symtab_section,
6458                                              &header_mem);
6459
6460     Elf_Data* symtab = elf_getdata(symtab_section, 0);
6461     ABG_ASSERT(symtab);
6462
6463     bool sym_is_defined = native_sym.st_shndx != SHN_UNDEF;
6464     bool sym_is_common = native_sym.st_shndx == SHN_COMMON; // this occurs in
6465                                                             // relocatable
6466                                                             // files.
6467     const char* name_str = elf_strptr(elf_handle(),
6468                                       symtab_sheader->sh_link,
6469                                       native_sym.st_name);
6470     if (name_str == 0)
6471       name_str = "";
6472
6473     elf_symbol::version ver;
6474     get_version_for_symbol(symbol_index,
6475                            sym_is_defined,
6476                            ver);
6477
6478     elf_symbol::visibility vis =
6479       stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(native_sym.st_other));
6480
6481     Elf_Scn *strings_section = find_ksymtab_strings_section();
6482     size_t strings_ndx = strings_section
6483       ? elf_ndxscn(strings_section)
6484       : 0;
6485
6486     elf_symbol_sptr sym =
6487       elf_symbol::create(env(), symbol_index, native_sym.st_size,
6488                          name_str, stt_to_elf_symbol_type
6489                          (GELF_ST_TYPE(native_sym.st_info)),
6490                          stb_to_elf_symbol_binding
6491                          (GELF_ST_BIND(native_sym.st_info)),
6492                          sym_is_defined, sym_is_common, ver, vis,
6493                          native_sym.st_shndx == strings_ndx);
6494     return sym;
6495   }
6496
6497   /// Read 8 bytes and convert their value into an uint64_t.
6498   ///
6499   /// @param bytes the array of bytes to read the next 8 bytes from.
6500   /// Note that this array must be at least 8 bytes long.
6501   ///
6502   /// @param result where to store the resuting uint64_t that was read.
6503   ///
6504   /// @param is_big_endian if true, read the 8 bytes in Big Endian
6505   /// mode, otherwise, read them in Little Endian.
6506   ///
6507   /// @param true if the 8 bytes could be read, false otherwise.
6508   bool
6509   read_uint64_from_array_of_bytes(const uint8_t *bytes,
6510                                   bool                  is_big_endian,
6511                                   uint64_t              &result) const
6512   {
6513     return read_int_from_array_of_bytes(bytes, 8, is_big_endian, result);
6514   }
6515
6516   /// Read N bytes and convert their value into an integer type T.
6517   ///
6518   /// Note that N cannot be bigger than 8 for now. The type passed needs to be
6519   /// at least of the size of number_of_bytes.
6520   ///
6521   /// @param bytes the array of bytes to read the next 8 bytes from.
6522   /// Note that this array must be at least 8 bytes long.
6523   ///
6524   /// @param number_of_bytes the number of bytes to read.  This number
6525   /// cannot be bigger than 8.
6526   ///
6527   /// @param is_big_endian if true, read the 8 bytes in Big Endian
6528   /// mode, otherwise, read them in Little Endian.
6529   ///
6530   /// @param result where to store the resuting integer that was read.
6531   ///
6532   ///
6533   /// @param true if the 8 bytes could be read, false otherwise.
6534   template<typename T>
6535   bool
6536   read_int_from_array_of_bytes(const uint8_t    *bytes,
6537                                unsigned char    number_of_bytes,
6538                                bool             is_big_endian,
6539                                T                &result) const
6540   {
6541     if (!bytes)
6542       return false;
6543
6544     ABG_ASSERT(number_of_bytes <= 8);
6545     ABG_ASSERT(number_of_bytes <= sizeof(T));
6546
6547     T res = 0;
6548
6549     const uint8_t *cur = bytes;
6550     if (is_big_endian)
6551       {
6552         // In Big Endian, the most significant byte is at the lowest
6553         // address.
6554         const uint8_t* msb = cur;
6555         res = *msb;
6556
6557         // Now read the remaining least significant bytes.
6558         for (uint i = 1; i < number_of_bytes; ++i)
6559           res = (res << 8) | ((T)msb[i]);
6560       }
6561     else
6562       {
6563         // In Little Endian, the least significant byte is at the
6564         // lowest address.
6565         const uint8_t* lsb = cur;
6566         res = *lsb;
6567         // Now read the remaining most significant bytes.
6568         for (uint i = 1; i < number_of_bytes; ++i)
6569           res = res | (((T)lsb[i]) << i * 8);
6570       }
6571
6572     result = res;
6573     return true;
6574   }
6575
6576   /// Lookup the address of the function entry point that corresponds
6577   /// to the address of a given function descriptor.
6578   ///
6579   /// On PPC64, a function pointer is the address of a function
6580   /// descriptor.  Function descriptors are located in the .opd
6581   /// section.  Each function descriptor is a triplet of three
6582   /// addresses, each one on 64 bits.  Among those three address only
6583   /// the first one is of any interest to us: the address of the entry
6584   /// point of the function.
6585   ///
6586   /// This function returns the address of the entry point of the
6587   /// function whose descriptor's address is given.
6588   ///
6589   /// http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#FUNC-DES
6590   ///
6591   /// https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/deeply_understand_64_bit_powerpc_elf_abi_function_descriptors?lang=en
6592   ///
6593   /// @param fn_desc_address the address of the function descriptor to
6594   /// consider.
6595   ///
6596   /// @return the address of the entry point of the function whose
6597   /// descriptor has the address @p fn_desc_address.  If there is no
6598   /// .opd section (e.g because we are not on ppc64) or more generally
6599   /// if the function descriptor could not be found then this function
6600   /// just returns the address of the fuction descriptor.
6601   GElf_Addr
6602   lookup_ppc64_elf_fn_entry_point_address(GElf_Addr fn_desc_address) const
6603   {
6604     if (!elf_handle())
6605       return fn_desc_address;
6606
6607     if (!elf_architecture_is_ppc64())
6608       return fn_desc_address;
6609
6610     bool is_big_endian = elf_architecture_is_big_endian();
6611
6612     Elf_Scn *opd_section = find_opd_section();
6613     if (!opd_section)
6614       return fn_desc_address;
6615
6616     GElf_Shdr header_mem;
6617     // The section header of the .opd section.
6618     GElf_Shdr *opd_sheader = gelf_getshdr(opd_section, &header_mem);
6619
6620     // The offset of the function descriptor entry, in the .opd
6621     // section.
6622     size_t fn_desc_offset = fn_desc_address - opd_sheader->sh_addr;
6623     Elf_Data *elf_data = elf_rawdata(opd_section, 0);
6624
6625     // Ensure that the opd_section has at least 8 bytes, starting from
6626     // the offset we want read the data from.
6627     if (elf_data->d_size <= fn_desc_offset + 8)
6628       return fn_desc_address;
6629
6630     // A pointer to the data of the .opd section, that we can actually
6631     // do something with.
6632     uint8_t * bytes = (uint8_t*) elf_data->d_buf;
6633
6634     // The resulting address we are looking for is going to be formed
6635     // in this variable.
6636     GElf_Addr result = 0;
6637     ABG_ASSERT(read_uint64_from_array_of_bytes(bytes + fn_desc_offset,
6638                                            is_big_endian, result));
6639
6640     return result;
6641   }
6642
6643   /// Given the address of the beginning of a function, lookup the
6644   /// symbol of the function, build an instance of @ref elf_symbol out
6645   /// of it and return it.
6646   ///
6647   /// @param symbol_start_addr the address of the beginning of the
6648   /// function to consider.
6649   ///
6650   /// @param sym the resulting symbol.  This is set iff the function
6651   /// returns true.
6652   ///
6653   /// @return the elf symbol found at address @p symbol_start_addr, or
6654   /// nil if none was found.
6655   elf_symbol_sptr
6656   lookup_elf_fn_symbol_from_address(GElf_Addr symbol_start_addr) const
6657   {
6658     addr_elf_symbol_sptr_map_type::const_iterator i,
6659       nil = fun_entry_addr_sym_map().end();
6660
6661     if ((i = fun_entry_addr_sym_map().find(symbol_start_addr)) == nil)
6662       return elf_symbol_sptr();
6663
6664     return i->second;
6665   }
6666
6667   /// Given the address of a global variable, lookup the symbol of the
6668   /// variable, build an instance of @ref elf_symbol out of it and
6669   /// return it.
6670   ///
6671   /// @param symbol_start_addr the address of the beginning of the
6672   /// variable to consider.
6673   ///
6674   /// @param the symbol found, iff the function returns true.
6675   ///
6676   /// @return the elf symbol found or nil if none was found.
6677   elf_symbol_sptr
6678   lookup_elf_var_symbol_from_address(GElf_Addr symbol_start_addr) const
6679   {
6680     addr_elf_symbol_sptr_map_type::const_iterator i,
6681       nil = var_addr_sym_map().end();
6682
6683     if ((i = var_addr_sym_map().find(symbol_start_addr)) == nil)
6684       return elf_symbol_sptr();
6685
6686     return i->second;
6687   }
6688
6689   /// Lookup an elf symbol, knowing its address.
6690   ///
6691   /// This function first looks for a function symbol having this
6692   /// address; if it doesn't find any, then it looks for a variable
6693   /// symbol.
6694   ///
6695   /// @param symbol_addr the address of the symbol of the symbol we
6696   /// are looking for.  Note that the address is a relative offset
6697   /// starting from the beginning of the .text section.  Addresses
6698   /// that are presen in the symbol table (the one named .symtab).
6699   ///
6700   /// @return the elf symbol if found, or nil otherwise.
6701   elf_symbol_sptr
6702   lookup_elf_symbol_from_address(GElf_Addr symbol_addr) const
6703   {
6704     elf_symbol_sptr result = lookup_elf_fn_symbol_from_address(symbol_addr);
6705     if (!result)
6706       result = lookup_elf_var_symbol_from_address(symbol_addr);
6707     return result;
6708   }
6709
6710   /// Look in the symbol tables of the underying elf file and see if
6711   /// we find a symbol of a given name of function type.
6712   ///
6713   /// @param sym_name the name of the symbol to look for.
6714   ///
6715   /// @param syms the public function symbols that were found, with
6716   /// the name @p sym_name.
6717   ///
6718   /// @return true iff the symbol was found.
6719   bool
6720   lookup_public_function_symbol_from_elf(const string&                  sym_name,
6721                                          vector<elf_symbol_sptr>&       syms)
6722   {
6723     return dwarf_reader::lookup_public_function_symbol_from_elf(env(),
6724                                                                 elf_handle(),
6725                                                                 sym_name,
6726                                                                 syms);
6727   }
6728
6729   /// Look in the symbol tables of the underying elf file and see if
6730   /// we find a symbol of a given name of variable type.
6731   ///
6732   /// @param sym_name the name of the symbol to look for.
6733   ///
6734   /// @param syms the variable symbols that were found, with the name
6735   /// @p sym_name.
6736   ///
6737   /// @return true iff the symbol was found.
6738   bool
6739   lookup_public_variable_symbol_from_elf(const string&            sym_name,
6740                                          vector<elf_symbol_sptr>& syms)
6741   {
6742     return dwarf_reader::lookup_public_variable_symbol_from_elf(env(),
6743                                                                 elf_handle(),
6744                                                                 sym_name,
6745                                                                 syms);
6746   }
6747
6748   /// Test if a given function symbol has been exported.
6749   ///
6750   /// @param symbol_address the address of the symbol we are looking
6751   /// for.  Note that this address must be a relative offset from the
6752   /// beginning of the .text section, just like the kind of addresses
6753   /// that are present in the .symtab section.
6754   ///
6755   /// @returnthe elf symbol if found, or nil otherwise.
6756   elf_symbol_sptr
6757   function_symbol_is_exported(GElf_Addr symbol_address) const
6758   {
6759     elf_symbol_sptr symbol = lookup_elf_fn_symbol_from_address(symbol_address);
6760     if (!symbol)
6761       return symbol;
6762
6763     if (!symbol->is_public())
6764       return elf_symbol_sptr();
6765
6766     address_set_sptr set;
6767     bool looking_at_linux_kernel_binary =
6768       load_in_linux_kernel_mode() && is_linux_kernel_binary();
6769
6770     if (looking_at_linux_kernel_binary)
6771       {
6772         if ((set = linux_exported_fn_syms()))
6773           {
6774             if (set->find(symbol_address) != set->end())
6775               return symbol;
6776           }
6777         if ((set = linux_exported_gpl_fn_syms()))
6778           {
6779             if (set->find(symbol_address) != set->end())
6780               return symbol;
6781           }
6782         return elf_symbol_sptr();
6783       }
6784
6785     return symbol;
6786   }
6787
6788   /// Test if a given variable symbol has been exported.
6789   ///
6790   /// @param symbol_address the address of the symbol we are looking
6791   /// for.  Note that this address must be a relative offset from the
6792   /// beginning of the .text section, just like the kind of addresses
6793   /// that are present in the .symtab section.
6794   ///
6795   /// @returnthe elf symbol if found, or nil otherwise.
6796   elf_symbol_sptr
6797   variable_symbol_is_exported(GElf_Addr symbol_address) const
6798   {
6799     elf_symbol_sptr symbol = lookup_elf_var_symbol_from_address(symbol_address);
6800     if (!symbol)
6801       return symbol;
6802
6803     if (!symbol->is_public())
6804       return elf_symbol_sptr();
6805
6806     address_set_sptr set;
6807     bool looking_at_linux_kernel_binary =
6808       load_in_linux_kernel_mode() && is_linux_kernel_binary();
6809
6810     if (looking_at_linux_kernel_binary)
6811       {
6812         if ((set = linux_exported_var_syms()))
6813           {
6814             if (set->find(symbol_address) != set->end())
6815               return symbol;
6816           }
6817         if ((set = linux_exported_gpl_var_syms()))
6818           {
6819             if (set->find(symbol_address) != set->end())
6820               return symbol;
6821           }
6822         return elf_symbol_sptr();
6823       }
6824
6825     return symbol;
6826   }
6827
6828   /// Getter for the map of function address -> symbol.
6829   ///
6830   /// @return the function address -> symbol map.
6831   const addr_elf_symbol_sptr_map_sptr
6832   fun_addr_sym_map_sptr() const
6833   {
6834     maybe_load_symbol_maps();
6835     return fun_addr_sym_map_;
6836   }
6837
6838   /// Getter for the map of function address -> symbol.
6839   ///
6840   /// @return the function address -> symbol map.
6841   addr_elf_symbol_sptr_map_sptr
6842   fun_addr_sym_map_sptr()
6843   {
6844     maybe_load_symbol_maps();
6845     return fun_addr_sym_map_;
6846   }
6847
6848   /// Getter for the map of function symbol address -> function symbol
6849   /// index.
6850   ///
6851   /// @return the map.  Note that this initializes the map once when
6852   /// its nedded.
6853   const addr_elf_symbol_sptr_map_type&
6854   fun_addr_sym_map() const
6855   {
6856     maybe_load_symbol_maps();
6857     return *fun_addr_sym_map_;
6858   }
6859
6860   /// Getter for the map of function symbol address -> function symbol
6861   /// index.
6862   ///
6863   /// @return the map.  Note that this initializes the map once when
6864   /// its nedded.
6865   addr_elf_symbol_sptr_map_type&
6866   fun_addr_sym_map()
6867   {
6868     maybe_load_symbol_maps();
6869     return *fun_addr_sym_map_;
6870   }
6871
6872   /// Getter for a pointer to the map that associates the address of
6873   /// an entry point of a function with the symbol of that function.
6874   ///
6875   /// Note that on non-"PPC64 ELFv1" binaries, this map is the same as
6876   /// the one that assciates the address of a function with the symbol
6877   /// of that function.
6878   ///
6879   /// @return a pointer to the map that associates the address of an
6880   /// entry point of a function with the symbol of that function.
6881   addr_elf_symbol_sptr_map_sptr&
6882   fun_entry_addr_sym_map_sptr()
6883   {
6884     if (!fun_entry_addr_sym_map_ && !fun_addr_sym_map_)
6885       maybe_load_symbol_maps();
6886     if (elf_architecture_is_ppc64())
6887       return fun_entry_addr_sym_map_;
6888     return fun_addr_sym_map_;
6889   }
6890
6891   /// Getter for a pointer to the map that associates the address of
6892   /// an entry point of a function with the symbol of that function.
6893   ///
6894   /// Note that on non-"PPC64 ELFv1" binaries, this map is the same as
6895   /// the one that assciates the address of a function with the symbol
6896   /// of that function.
6897   ///
6898   /// @return a pointer to the map that associates the address of an
6899   /// entry point of a function with the symbol of that function.
6900   const addr_elf_symbol_sptr_map_sptr&
6901   fun_entry_addr_sym_map_sptr() const
6902   {return const_cast<read_context*>(this)->fun_entry_addr_sym_map_sptr();}
6903
6904
6905   /// Getter for the map that associates the address of an entry point
6906   /// of a function with the symbol of that function.
6907   ///
6908   /// Note that on non-"PPC64 ELFv1" binaries, this map is the same as
6909   /// the one that assciates the address of a function with the symbol
6910   /// of that function.
6911   ///
6912   /// @return the map that associates the address of an entry point of
6913   /// a function with the symbol of that function.
6914   addr_elf_symbol_sptr_map_type&
6915   fun_entry_addr_sym_map()
6916   {return *fun_entry_addr_sym_map_sptr();}
6917
6918   /// Getter for the map that associates the address of an entry point
6919   /// of a function with the symbol of that function.
6920   ///
6921   /// Note that on non-"PPC64 ELFv1" binaries, this map is the same as
6922   /// the one that assciates the address of a function with the symbol
6923   /// of that function.
6924   ///
6925   /// @return the map that associates the address of an entry point of
6926   /// a function with the symbol of that function.
6927   const addr_elf_symbol_sptr_map_type&
6928   fun_entry_addr_sym_map() const
6929   { return *fun_entry_addr_sym_map_sptr();}
6930
6931   /// Getter for the map of function symbols (name -> sym).
6932   ///
6933   /// @return a shared pointer to the map of function symbols.
6934   const string_elf_symbols_map_sptr&
6935   fun_syms_sptr() const
6936   {
6937     maybe_load_symbol_maps();
6938     return fun_syms_;
6939   }
6940
6941   /// Getter for the map of function symbols (name -> sym).
6942   ///
6943   /// @return a shared pointer to the map of function symbols.
6944   string_elf_symbols_map_sptr&
6945   fun_syms_sptr()
6946   {
6947     maybe_load_symbol_maps();
6948     return fun_syms_;
6949   }
6950
6951   /// Getter for the map of function symbols (name -> sym).
6952   ///
6953   /// @return a reference to the map of function symbols.
6954   const string_elf_symbols_map_type&
6955   fun_syms() const
6956   {
6957     maybe_load_symbol_maps();
6958     return *fun_syms_;
6959   }
6960
6961   /// Getter for the map of function symbols (name -> sym).
6962   ///
6963   /// @return a reference to the map of function symbols.
6964   string_elf_symbols_map_type&
6965   fun_syms()
6966   {
6967     maybe_load_symbol_maps();
6968     return *fun_syms_;
6969   }
6970
6971   /// Getter for the map of variable symbols (name -> sym)
6972   ///
6973   /// @return a shared pointer to the map of variable symbols.
6974   const string_elf_symbols_map_sptr
6975   var_syms_sptr() const
6976   {
6977     maybe_load_symbol_maps();
6978     return var_syms_;
6979   }
6980
6981   /// Getter for the map of variable symbols (name -> sym)
6982   ///
6983   /// @return a shared pointer to the map of variable symbols.
6984   string_elf_symbols_map_sptr
6985   var_syms_sptr()
6986   {
6987     maybe_load_symbol_maps();
6988     return var_syms_;
6989   }
6990
6991   /// Getter for the map of variable symbols (name -> sym)
6992   ///
6993   /// @return a reference to the map of variable symbols.
6994   const string_elf_symbols_map_type&
6995   var_syms() const
6996   {
6997     maybe_load_symbol_maps();
6998     return *var_syms_;
6999   }
7000
7001   /// Getter for the map of variable symbols (name -> sym)
7002   ///
7003   /// @return a reference to the map of variable symbols.
7004   string_elf_symbols_map_type&
7005   var_syms()
7006   {
7007     maybe_load_symbol_maps();
7008     return *var_syms_;
7009   }
7010
7011   /// Getter for the map of undefined function symbols (name -> vector
7012   /// of symbols).
7013   ///
7014   /// @return a (smart) pointer to the map of undefined function
7015   /// symbols.
7016   const string_elf_symbols_map_sptr&
7017   undefined_fun_syms_sptr() const
7018   {
7019     maybe_load_symbol_maps();
7020     return undefined_fun_syms_;
7021   }
7022
7023   /// Getter for the map of undefined function symbols (name -> vector
7024   /// of symbols).
7025   ///
7026   /// @return a (smart) pointer to the map of undefined function
7027   /// symbols.
7028   string_elf_symbols_map_sptr&
7029   undefined_fun_syms_sptr()
7030   {
7031     maybe_load_symbol_maps();
7032     return undefined_fun_syms_;
7033   }
7034
7035   /// Getter for the map of undefined function symbols (name -> vector
7036   /// of symbols).
7037   ///
7038   /// @return a reference to the map of undefined function symbols.
7039   const string_elf_symbols_map_type&
7040   undefined_fun_syms() const
7041   {
7042     maybe_load_symbol_maps();
7043     return *undefined_fun_syms_;
7044   }
7045
7046   /// Getter for the map of undefined function symbols (name -> vector
7047   /// of symbols).
7048   ///
7049   /// @return a reference to the map of undefined function symbols.
7050   string_elf_symbols_map_type&
7051   undefined_fun_syms()
7052   {
7053     maybe_load_symbol_maps();
7054     return *undefined_fun_syms_;
7055   }
7056
7057   /// Getter for the map of undefined variable symbols (name -> vector
7058   /// of symbols).
7059   ///
7060   /// @return a (smart) pointer to the map of undefined variable
7061   /// symbols.
7062   const string_elf_symbols_map_sptr&
7063   undefined_var_syms_sptr() const
7064   {
7065     maybe_load_symbol_maps();
7066     return undefined_var_syms_;
7067   }
7068
7069   /// Getter for the map of undefined variable symbols (name -> vector
7070   /// of symbols).
7071   ///
7072   /// @return a (smart) pointer to the map of undefined variable
7073   /// symbols.
7074   string_elf_symbols_map_sptr&
7075   undefined_var_syms_sptr()
7076   {
7077     maybe_load_symbol_maps();
7078     return undefined_var_syms_;
7079   }
7080
7081   /// Getter for the map of undefined variable symbols (name -> vector
7082   /// of symbols).
7083   ///
7084   /// @return a reference to the map of undefined variable symbols.
7085   const string_elf_symbols_map_type&
7086   undefined_var_syms() const
7087   {
7088     maybe_load_symbol_maps();
7089     return *undefined_var_syms_;
7090   }
7091
7092   /// Getter for the map of undefined variable symbols (name -> vector
7093   /// of symbols).
7094   ///
7095   /// @return a reference to the map of undefined variable symbols.
7096   string_elf_symbols_map_type&
7097   undefined_var_syms()
7098   {
7099     maybe_load_symbol_maps();
7100     return *undefined_var_syms_;
7101   }
7102
7103   /// Getter for the set of addresses of function symbols that are
7104   /// explicitely exported, for a linux kernel (module) binary.  These
7105   /// are the addresses of function symbols present in the __ksymtab
7106   /// section
7107   address_set_sptr&
7108   linux_exported_fn_syms()
7109   {return linux_exported_fn_syms_;}
7110
7111   /// Getter for the set of addresses of functions that are
7112   /// explicitely exported, for a linux kernel (module) binary.  These
7113   /// are the addresses of function symbols present in the __ksymtab
7114   /// section.
7115   ///
7116   /// @return the set of addresses of exported function symbols.
7117   const address_set_sptr&
7118   linux_exported_fn_syms() const
7119   {return const_cast<read_context*>(this)->linux_exported_fn_syms();}
7120
7121   /// Create an empty set of addresses of functions exported from a
7122   /// linux kernel (module) binary, or return the one that already
7123   /// exists.
7124   ///
7125   /// @return the set of addresses of exported function symbols.
7126   address_set_sptr&
7127   create_or_get_linux_exported_fn_syms()
7128   {
7129     if (!linux_exported_fn_syms_)
7130       linux_exported_fn_syms_.reset(new address_set_type);
7131     return linux_exported_fn_syms_;
7132   }
7133
7134   /// Getter for the set of addresses of v ariables that are
7135   /// explicitely exported, for a linux kernel (module) binary.  These
7136   /// are the addresses of variable symbols present in the __ksymtab
7137   /// section.
7138   ///
7139   /// @return the set of addresses of exported variable symbols.
7140   address_set_sptr&
7141   linux_exported_var_syms()
7142   {return linux_exported_var_syms_;}
7143
7144   /// Getter for the set of addresses of variables that are
7145   /// explicitely exported, for a linux kernel (module) binary.  These
7146   /// are the addresses of variable symbols present in the __ksymtab
7147   /// section.
7148   ///
7149   /// @return the set of addresses of exported variable symbols.
7150   const address_set_sptr&
7151   linux_exported_var_syms() const
7152   {return const_cast<read_context*>(this)->linux_exported_var_syms();}
7153
7154
7155   /// Create an empty set of addresses of variables exported from a
7156   /// linux kernel (module) binary, or return the one that already
7157   /// exists.
7158   ///
7159   /// @return the set of addresses of exported variable symbols.
7160   address_set_sptr&
7161   create_or_get_linux_exported_var_syms()
7162   {
7163     if (!linux_exported_var_syms_)
7164       linux_exported_var_syms_.reset(new address_set_type);
7165     return linux_exported_var_syms_;
7166   }
7167
7168
7169   /// Getter for the set of addresses of function symbols that are
7170   /// explicitely exported as GPL, for a linux kernel (module) binary.
7171   /// These are the addresses of function symbols present in the
7172   /// __ksymtab_gpl section.
7173   address_set_sptr&
7174   linux_exported_gpl_fn_syms()
7175   {return linux_exported_gpl_fn_syms_;}
7176
7177   /// Getter for the set of addresses of function symbols that are
7178   /// explicitely exported as GPL, for a linux kernel (module) binary.
7179   /// These are the addresses of function symbols present in the
7180   /// __ksymtab_gpl section.
7181   const address_set_sptr&
7182   linux_exported_gpl_fn_syms() const
7183   {return const_cast<read_context*>(this)->linux_exported_gpl_fn_syms();}
7184
7185   /// Create an empty set of addresses of GPL functions exported from
7186   /// a linux kernel (module) binary, or return the one that already
7187   /// exists.
7188   ///
7189   /// @return the set of addresses of exported function symbols.
7190   address_set_sptr&
7191   create_or_get_linux_exported_gpl_fn_syms()
7192   {
7193     if (!linux_exported_gpl_fn_syms_)
7194       linux_exported_gpl_fn_syms_.reset(new address_set_type);
7195     return linux_exported_gpl_fn_syms_;
7196   }
7197
7198   /// Getter for the set of addresses of variable symbols that are
7199   /// explicitely exported as GPL, for a linux kernel (module) binary.
7200   /// These are the addresses of variable symbols present in the
7201   /// __ksymtab_gpl section.
7202   address_set_sptr&
7203   linux_exported_gpl_var_syms()
7204   {return linux_exported_gpl_var_syms_;}
7205
7206   /// Getter for the set of addresses of variable symbols that are
7207   /// explicitely exported as GPL, for a linux kernel (module) binary.
7208   /// These are the addresses of variable symbols present in the
7209   /// __ksymtab_gpl section.
7210   const address_set_sptr&
7211   linux_exported_gpl_var_syms() const
7212   {return const_cast<read_context*>(this)->linux_exported_gpl_var_syms();}
7213
7214   /// Create an empty set of addresses of GPL variables exported from
7215   /// a linux kernel (module) binary, or return the one that already
7216   /// exists.
7217   ///
7218   /// @return the set of addresses of exported variable symbols.
7219   address_set_sptr&
7220   create_or_get_linux_exported_gpl_var_syms()
7221   {
7222     if (!linux_exported_gpl_var_syms_)
7223       linux_exported_gpl_var_syms_.reset(new address_set_type);
7224     return linux_exported_gpl_var_syms_;
7225   }
7226
7227   /// Getter for the ELF dt_needed tag.
7228   const vector<string>&
7229   dt_needed() const
7230   {return dt_needed_;}
7231
7232   /// Getter for the ELF dt_soname tag.
7233   const string&
7234   dt_soname() const
7235   {return dt_soname_;}
7236
7237   /// Getter for the ELF architecture of the current file.
7238   const string&
7239   elf_architecture() const
7240   {return elf_architecture_;}
7241
7242   /// Return the size of a word for the current architecture.
7243   /// @return the size of a word.
7244   unsigned char
7245   architecture_word_size() const
7246   {
7247     unsigned char word_size = 0;
7248     GElf_Ehdr eh_mem;
7249     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
7250     if (elf_header->e_ident[EI_CLASS] == ELFCLASS32)
7251       word_size = 4;
7252     else if (elf_header->e_ident[EI_CLASS] == ELFCLASS64)
7253       word_size = 8;
7254     else
7255       ABG_ASSERT_NOT_REACHED;
7256     return word_size;
7257   }
7258
7259   /// Test if the architecture of the current binary is ppc64.
7260   ///
7261   /// @return true iff the architecture of the current binary is ppc64.
7262   bool
7263   elf_architecture_is_ppc64() const
7264   {
7265     GElf_Ehdr eh_mem;
7266     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
7267
7268     return (elf_header && elf_header->e_machine == EM_PPC64);
7269   }
7270
7271   /// Test if the endianness of the current binary is Big Endian.
7272   ///
7273   /// https://en.wikipedia.org/wiki/Endianness.
7274   ///
7275   /// @return true iff the current binary is Big Endian.
7276   bool
7277   elf_architecture_is_big_endian() const
7278   {
7279     GElf_Ehdr eh_mem;
7280     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
7281
7282     bool is_big_endian = (elf_header->e_ident[EI_DATA] == ELFDATA2MSB);
7283
7284     if (!is_big_endian)
7285       ABG_ASSERT(elf_header->e_ident[EI_DATA] == ELFDATA2LSB);
7286
7287     return is_big_endian;
7288   }
7289
7290   /// Test if the current elf file being read is an executable.
7291   ///
7292   /// @return true iff the current elf file being read is an
7293   /// executable.
7294   bool
7295   current_elf_file_is_executable() const
7296   {
7297     GElf_Ehdr eh_mem;
7298     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
7299     return elf_header->e_type == ET_EXEC;
7300   }
7301
7302   /// Test if the current elf file being read is a dynamic shared
7303   /// object.
7304   ///
7305   /// @return true iff the current elf file being read is a
7306   /// dynamic shared object.
7307   bool
7308   current_elf_file_is_dso() const
7309   {
7310     GElf_Ehdr eh_mem;
7311     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
7312     return elf_header->e_type == ET_DYN;
7313   }
7314
7315   /// Getter for the map of global variables symbol address -> global
7316   /// variable symbol index.
7317   ///
7318   /// @return the map.  Note that this initializes the map once when
7319   /// its nedded.
7320   const addr_elf_symbol_sptr_map_type&
7321   var_addr_sym_map() const
7322   {return const_cast<read_context*>(this)->var_addr_sym_map();}
7323
7324   /// Getter for the map of global variables symbol address -> global
7325   /// variable symbol index.
7326   ///
7327   /// @return the map.  Note that this initializes the map once when
7328   /// its nedded.
7329   addr_elf_symbol_sptr_map_type&
7330   var_addr_sym_map()
7331   {
7332     if (!var_addr_sym_map_)
7333       maybe_load_symbol_maps();
7334     return *var_addr_sym_map_;
7335   }
7336
7337   /// Load the maps address -> function symbol, address -> variable
7338   /// symbol and the maps of function and variable undefined symbols.
7339   ///
7340   /// @param load_fun_map whether to load the address to function map.
7341   ///
7342   /// @param load_var_map whether to laod the address to variable map.
7343   ///
7344   /// @param load_undefined_fun_map whether to load the undefined
7345   /// function map.
7346   ///
7347   /// @param load_undefined_var_map whether to laod the undefined
7348   /// variable map.
7349   ///
7350   /// @return return true iff the maps have be loaded.
7351   bool
7352   load_symbol_maps_from_symtab_section(bool load_fun_map,
7353                                        bool load_var_map,
7354                                        bool load_undefined_fun_map,
7355                                        bool load_undefined_var_map)
7356   {
7357     Elf_Scn* symtab_section = find_symbol_table_section();
7358     if (!symtab_section)
7359       return false;
7360
7361     GElf_Shdr header_mem;
7362     GElf_Shdr* symtab_sheader = gelf_getshdr(symtab_section,
7363                                              &header_mem);
7364     size_t nb_syms = symtab_sheader->sh_size / symtab_sheader->sh_entsize;
7365
7366     Elf_Data* symtab = elf_getdata(symtab_section, 0);
7367     ABG_ASSERT(symtab);
7368
7369     GElf_Ehdr elf_header;
7370     ABG_ASSERT(gelf_getehdr(elf_handle(), &elf_header));
7371
7372     bool is_ppc64 = elf_architecture_is_ppc64();
7373
7374     for (size_t i = 0; i < nb_syms; ++i)
7375       {
7376         GElf_Sym* sym, sym_mem;
7377         sym = gelf_getsym(symtab, i, &sym_mem);
7378         ABG_ASSERT(sym);
7379
7380         if ((load_fun_map || load_undefined_fun_map)
7381             && (GELF_ST_TYPE(sym->st_info) == STT_FUNC
7382                 || GELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC))
7383           {
7384             elf_symbol_sptr symbol = lookup_elf_symbol_from_index(i);
7385             ABG_ASSERT(symbol);
7386             ABG_ASSERT(symbol->is_function());
7387
7388
7389             if (load_fun_map && symbol->is_public())
7390               {
7391                 {
7392                   string_elf_symbols_map_type::iterator it =
7393                     fun_syms_->find(symbol->get_name());
7394                   if (it == fun_syms_->end())
7395                     {
7396                       (*fun_syms_)[symbol->get_name()] = elf_symbols();
7397                       it = fun_syms_->find(symbol->get_name());
7398                     }
7399                   string name = symbol->get_name();
7400                   it->second.push_back(symbol);
7401                 }
7402
7403                 {
7404                   GElf_Addr symbol_value =
7405                     maybe_adjust_et_rel_sym_addr_to_abs_addr(sym);
7406
7407                   addr_elf_symbol_sptr_map_type::const_iterator it =
7408                     fun_addr_sym_map_->find(symbol_value);
7409                   if (it == fun_addr_sym_map_->end())
7410                     (*fun_addr_sym_map_)[symbol_value] = symbol;
7411                   else //if (sym->st_value != 0)
7412                     it->second->get_main_symbol()->add_alias(symbol);
7413
7414                   if (is_ppc64)
7415                     {
7416                       // For ppc64 ELFv1 binaries, we need to build a
7417                       // function entry point address -> function
7418                       // symbol map.  This is in addition to the
7419                       // function pointer -> symbol map.  This is
7420                       // because on ppc64 ELFv1, a function pointer is
7421                       // different from a function entry point
7422                       // address.
7423                       //
7424                       // On ppc64 ELFv1, the DWARF DIE of a function
7425                       // references the address of the entry point of
7426                       // the function symbol; whereas the value of the
7427                       // function symbol is the function pointer.  As
7428                       // these addresses are different, if I we want
7429                       // to get to the symbol of a function from its
7430                       // entry point address (as referenced by DWARF
7431                       // function DIEs) we must have the two maps I
7432                       // mentionned right above.
7433                       //
7434                       // In other words, we need a map that associates
7435                       // a function enty point address with the symbol
7436                       // of that function, to be able to get the
7437                       // function symbol that corresponds to a given
7438                       // function DIE, on ppc64.
7439                       //
7440                       // The value of the function pointer (the value
7441                       // of the symbol) usually refers to the offset
7442                       // of a table in the .opd section.  But
7443                       // sometimes, for a symbol named "foo", the
7444                       // corresponding symbol named ".foo" (note the
7445                       // dot before foo) which value is the entry
7446                       // point address of the function; that entry
7447                       // point address refers to a region in the .text
7448                       // section.
7449                       //
7450                       // So we are only interested in values of the
7451                       // symbol that are in the .opd section.
7452                       GElf_Addr fn_desc_addr = sym->st_value;
7453                       GElf_Addr fn_entry_point_addr =
7454                         lookup_ppc64_elf_fn_entry_point_address(fn_desc_addr);
7455                       addr_elf_symbol_sptr_map_type::const_iterator it2 =
7456                         fun_entry_addr_sym_map().find(fn_entry_point_addr);
7457
7458                       if (it2 == fun_entry_addr_sym_map().end())
7459                         fun_entry_addr_sym_map()[fn_entry_point_addr] = symbol;
7460                       else if (address_is_in_opd_section(fn_desc_addr))
7461                         {
7462                           // Either
7463                           //
7464                           // 'symbol' must have been registered as an
7465                           // alias for it2->second->get_main_symbol(),
7466                           // right before the "if (ppc64)" statement.
7467                           //
7468                           // Or
7469                           //
7470                           // if the name of 'symbol' is foo, then the
7471                           // name of it2->second is ".foo".  That is,
7472                           // foo is the name of the symbol when it
7473                           // refers to the function descriptor in the
7474                           // .opd section and ".foo" is an internal
7475                           // name for the address of the entry point
7476                           // of foo.
7477                           //
7478                           // In the latter case, we just want to keep
7479                           // a refernce to "foo" as .foo is an
7480                           // internal name.
7481
7482                           bool two_symbols_alias =
7483                             it2->second->get_main_symbol()->does_alias(*symbol);
7484                           bool symbol_is_foo_and_prev_symbol_is_dot_foo =
7485                             (it2->second->get_name()
7486                              == string(".") + symbol->get_name());
7487
7488                           ABG_ASSERT(two_symbols_alias
7489                                  || symbol_is_foo_and_prev_symbol_is_dot_foo);
7490
7491                           if (symbol_is_foo_and_prev_symbol_is_dot_foo)
7492                             // Let's just keep a reference of the
7493                             // symbol that the user sees in the source
7494                             // code (the one named foo).  The symbol
7495                             // which name is prefixed with a "dot" is
7496                             // an artificial one.
7497                             fun_entry_addr_sym_map()[fn_entry_point_addr] = symbol;
7498                         }
7499                     }
7500                 }
7501               }
7502             else if (load_undefined_fun_map && !symbol->is_defined())
7503               {
7504                 string_elf_symbols_map_type::iterator it =
7505                   undefined_fun_syms_->find(symbol->get_name());
7506                 if (it == undefined_fun_syms_->end())
7507                   {
7508                     (*undefined_fun_syms_)[symbol->get_name()] = elf_symbols();
7509                     it = undefined_fun_syms_->find(symbol->get_name());
7510                   }
7511                 it->second.push_back(symbol);
7512               }
7513           }
7514         else if ((load_var_map || load_undefined_var_map)
7515                  && (GELF_ST_TYPE(sym->st_info) == STT_OBJECT
7516                      || GELF_ST_TYPE(sym->st_info) == STT_TLS)
7517                  // If the symbol is for an OBJECT, the index of the
7518                  // section it refers to cannot be absolute.
7519                  // Otherwise that OBJECT is not a variable.
7520                  && (sym->st_shndx != SHN_ABS
7521                      || GELF_ST_TYPE(sym->st_info) != STT_OBJECT ))
7522           {
7523             elf_symbol_sptr symbol = lookup_elf_symbol_from_index(i);
7524             ABG_ASSERT(symbol);
7525             ABG_ASSERT(symbol->is_variable());
7526
7527             if (load_var_map && symbol->is_public())
7528               {
7529                 {
7530                   string_elf_symbols_map_type::iterator it =
7531                     var_syms_->find(symbol->get_name());
7532                   if (it == var_syms_->end())
7533                     {
7534                       (*var_syms_)[symbol->get_name()] = elf_symbols();
7535                       it = var_syms_->find(symbol->get_name());
7536                     }
7537                   string name = symbol->get_name();
7538                   it->second.push_back(symbol);
7539                 }
7540
7541                 if (symbol->is_common_symbol())
7542                   {
7543                     string_elf_symbols_map_type::iterator it =
7544                       var_syms_->find(symbol->get_name());
7545                     ABG_ASSERT(it != var_syms_->end());
7546                     const elf_symbols& common_sym_instances = it->second;
7547                     ABG_ASSERT(!common_sym_instances.empty());
7548                     if (common_sym_instances.size() > 1)
7549                       {
7550                         elf_symbol_sptr main_common_sym =
7551                           common_sym_instances[0];
7552                         ABG_ASSERT(main_common_sym->get_name()
7553                                == symbol->get_name());
7554                         ABG_ASSERT(main_common_sym->is_common_symbol());
7555                         ABG_ASSERT(symbol.get() != main_common_sym.get());
7556                         main_common_sym->add_common_instance(symbol);
7557                       }
7558                   }
7559                 else
7560                   {
7561                     GElf_Addr symbol_value =
7562                       maybe_adjust_et_rel_sym_addr_to_abs_addr(sym);
7563                     addr_elf_symbol_sptr_map_type::const_iterator it =
7564                       var_addr_sym_map_->find(symbol_value);
7565                     if (it == var_addr_sym_map_->end())
7566                       (*var_addr_sym_map_)[symbol_value] = symbol;
7567                     else
7568                       it->second->get_main_symbol()->add_alias(symbol);
7569                   }
7570               }
7571             else if (load_undefined_var_map && !symbol->is_defined())
7572               {
7573                 string_elf_symbols_map_type::iterator it =
7574                   undefined_var_syms_->find(symbol->get_name());
7575                 if (it == undefined_var_syms_->end())
7576                   {
7577                     (*undefined_var_syms_)[symbol->get_name()] = elf_symbols();
7578                     it = undefined_var_syms_->find(symbol->get_name());
7579                   }
7580                 it->second.push_back(symbol);
7581               }
7582           }
7583       }
7584     return true;
7585   }
7586
7587   /// Try reading the first __ksymtab section entry as if it is in the
7588   /// pre-v4_19 format and lookup a symbol from the .symbol section to
7589   /// see if that succeeds.  If it does, then we can assume the
7590   /// __ksymtab section is in the pre-v4_19 format.
7591   ///
7592   /// @return the symbol resulting from the lookup of the symbol
7593   /// address we got from reading the first entry of the ksymtab
7594   /// section assuming the pre-v4.19 format.  If nil, it means the
7595   /// __ksymtab section is not in the pre-v4.19 format.
7596   elf_symbol_sptr
7597   try_reading_first_ksymtab_entry_using_pre_v4_19_format() const
7598   {
7599     Elf_Scn *section = find_any_ksymtab_section();
7600     Elf_Data *elf_data = elf_rawdata(section, 0);
7601     uint8_t *bytes = reinterpret_cast<uint8_t*>(elf_data->d_buf);
7602     bool is_big_endian = elf_architecture_is_big_endian();
7603     elf_symbol_sptr symbol;
7604     unsigned char symbol_value_size = architecture_word_size();
7605
7606     GElf_Addr symbol_address = 0, adjusted_symbol_address = 0;
7607     ABG_ASSERT(read_int_from_array_of_bytes(bytes,
7608                                             symbol_value_size,
7609                                             is_big_endian,
7610                                             symbol_address));
7611     adjusted_symbol_address = maybe_adjust_fn_sym_address(symbol_address);
7612     symbol = lookup_elf_symbol_from_address(adjusted_symbol_address);
7613     return symbol;
7614   }
7615
7616   /// Try reading the first __ksymtab section entry as if it is in the
7617   /// v4_19 format and lookup a symbol from the .symbol section to see
7618   /// if that succeeds.  If it does, then we can assume the __ksymtab
7619   /// section is in the v4_19 format.
7620   ///
7621   /// @return the symbol resulting from the lookup of the symbol
7622   /// address we got from reading the first entry of the ksymtab
7623   /// section assuming the v4.19 format.  If nil, it means the
7624   /// __ksymtab section is not in the v4.19 format.
7625   elf_symbol_sptr
7626   try_reading_first_ksymtab_entry_using_v4_19_format() const
7627   {
7628     Elf_Scn *section = find_any_ksymtab_section();
7629     Elf_Data *elf_data = elf_rawdata(section, 0);
7630     uint8_t *bytes = reinterpret_cast<uint8_t*>(elf_data->d_buf);
7631     bool is_big_endian = elf_architecture_is_big_endian();
7632     elf_symbol_sptr symbol;
7633
7634     int32_t offset = 0;
7635     const unsigned char symbol_value_size = sizeof(offset);
7636     GElf_Addr symbol_address = 0, adjusted_symbol_address = 0;
7637     ABG_ASSERT(read_int_from_array_of_bytes(bytes,
7638                                             symbol_value_size,
7639                                             is_big_endian,
7640                                             offset));
7641     GElf_Shdr mem;
7642     GElf_Shdr *section_header = gelf_getshdr(section, &mem);
7643     symbol_address = offset + section_header->sh_addr;
7644
7645     adjusted_symbol_address = maybe_adjust_fn_sym_address(symbol_address);
7646     symbol = lookup_elf_symbol_from_address(adjusted_symbol_address);
7647     return symbol;
7648   }
7649
7650   /// Try to determine the format of the __ksymtab and __ksymtab_gpl
7651   /// sections of Linux kernel modules.
7652   ///
7653   /// This is important because we need to know the format of these
7654   /// sections to be able to read from them.
7655   ///
7656   /// @return the format the __ksymtab[_gpl] sections.
7657   enum ksymtab_format
7658   get_ksymtab_format_module() const
7659   {
7660     Elf_Scn *section = find_any_ksymtab_reloc_section();
7661
7662     ABG_ASSERT(section);
7663
7664     // Libdwfl has a weird quirk where, in the process of obtaining an Elf
7665     // descriptor via dwfl_module_getelf(), it will apply all relocations it
7666     // knows how to and it will zero the relocation info after applying it. If
7667     // the .rela__ksymtab* section contained only simple (absolute) relocations,
7668     // they will have been all applied and sh_size will be 0. For arches that
7669     // support relative ksymtabs, simple relocations only appear in pre-4.19
7670     // kernel modules.
7671     GElf_Shdr section_mem;
7672     GElf_Shdr *section_shdr = gelf_getshdr(section, &section_mem);
7673     if (section_shdr->sh_size == 0)
7674       return PRE_V4_19_KSYMTAB_FORMAT;
7675
7676     bool is_relasec = (section_shdr->sh_type == SHT_RELA);
7677
7678     // If we still have a normal non-zeroed relocation section, we can guess
7679     // what format the ksymtab is in depending on what types of relocs it
7680     // contains.
7681
7682     uint64_t type;
7683     Elf_Data *section_data = elf_getdata(section, 0);
7684     if (is_relasec)
7685       {
7686         GElf_Rela rela;
7687         gelf_getrela(section_data, 0, &rela);
7688         type = GELF_R_TYPE(rela.r_info);
7689       }
7690     else
7691       {
7692         GElf_Rel rel;
7693         gelf_getrel(section_data, 0, &rel);
7694         type = GELF_R_TYPE(rel.r_info);
7695       }
7696
7697     // Sigh, I dislike the arch-dependent code here, but this seems to be a
7698     // reliable heuristic for kernel modules for now. Relative ksymtabs only
7699     // supported on x86 and arm64 as of v4.19.
7700     ksymtab_format format;
7701     switch (type)
7702       {
7703       case R_X86_64_64: // Same as R_386_32, fallthrough
7704 #ifdef HAVE_R_AARCH64_ABS64_MACRO
7705       case R_AARCH64_ABS64:
7706 #endif
7707         format = PRE_V4_19_KSYMTAB_FORMAT;
7708         break;
7709       case R_X86_64_PC32: // Same as R_386_PC32, fallthrough
7710 #ifdef HAVE_R_AARCH64_PREL32_MACRO
7711       case R_AARCH64_PREL32:
7712 #endif
7713         format = V4_19_KSYMTAB_FORMAT;
7714         break;
7715       default:
7716         // Fall back to other methods of determining the ksymtab format.
7717         format = UNDEFINED_KSYMTAB_FORMAT;
7718         break;
7719       }
7720     return format;
7721   }
7722
7723   /// Determine the format of the __ksymtab and __ksymtab_gpl
7724   /// sections.
7725   ///
7726   /// This is important because we need the know the format of these
7727   /// sections to be able to read from them.
7728   ///
7729   /// @return the format the __ksymtab[_gpl] sections.
7730   enum ksymtab_format
7731   get_ksymtab_format() const
7732   {
7733     if (!find_any_ksymtab_section())
7734       ksymtab_format_ = UNDEFINED_KSYMTAB_FORMAT;
7735     else
7736       {
7737         if (ksymtab_format_ == UNDEFINED_KSYMTAB_FORMAT)
7738           {
7739             // Since Linux kernel modules are relocatable, we can first try
7740             // using a heuristic based on relocations to guess the ksymtab format.
7741             if (is_linux_kernel_module())
7742              {
7743                ksymtab_format_ = get_ksymtab_format_module();
7744                if (ksymtab_format_ != UNDEFINED_KSYMTAB_FORMAT)
7745                   return ksymtab_format_;
7746              }
7747
7748             // If it's not a kernel module or we couldn't determine its format
7749             // with relocations, fall back to the heuristics below.
7750
7751             // OK this is a dirty little heuristic to determine the
7752             // format of the ksymtab section.
7753             //
7754             // We try to read the first ksymtab entry assuming a
7755             // pre-v4.19 format.  If that succeeds then we are in the
7756             // pr-v4.19 format.  Otherwise, try reading it assuming a
7757             // v4.19 format.  For now, we just support
7758             // PRE_V4_19_KSYMTAB_FORMAT and V4_19_KSYMTAB_FORMAT.
7759             if (try_reading_first_ksymtab_entry_using_pre_v4_19_format())
7760               ksymtab_format_ = PRE_V4_19_KSYMTAB_FORMAT;
7761             else if (try_reading_first_ksymtab_entry_using_v4_19_format())
7762               ksymtab_format_ = V4_19_KSYMTAB_FORMAT;
7763             else
7764               // If a new format emerges, then we need to add its
7765               // support above.
7766               ABG_ASSERT_NOT_REACHED;
7767           }
7768       }
7769     return ksymtab_format_;
7770   }
7771
7772   /// Getter of the size of the symbol value part of an entry of the
7773   /// ksymtab section.
7774   ///
7775   /// @return the size of the symbol value part of the entry of the
7776   /// ksymtab section.
7777   unsigned char
7778   get_ksymtab_symbol_value_size() const
7779   {
7780     unsigned char result = 0;
7781     ksymtab_format format = get_ksymtab_format();
7782     if (format == UNDEFINED_KSYMTAB_FORMAT)
7783       ;
7784     else if (format == PRE_V4_19_KSYMTAB_FORMAT)
7785       result = architecture_word_size();
7786     else if (format == V4_19_KSYMTAB_FORMAT)
7787       result = 4;
7788     else
7789       ABG_ASSERT_NOT_REACHED;
7790
7791     return result;
7792   }
7793
7794   /// Getter of the size of one entry of the ksymtab section.
7795   ///
7796   /// @return the size of one entry of the ksymtab section.
7797   unsigned char
7798   get_ksymtab_entry_size() const
7799   {
7800     if (ksymtab_entry_size_ == 0)
7801       // The entry size if 2 *  symbol_value_size.
7802       ksymtab_entry_size_ = 2 * get_ksymtab_symbol_value_size();
7803
7804     return ksymtab_entry_size_;
7805   }
7806
7807   /// Getter of the number of entries that are present in the ksymtab
7808   /// section.
7809   ///
7810   /// @return the number of entries that are present in the ksymtab
7811   /// section.
7812   size_t
7813   get_nb_ksymtab_entries() const
7814   {
7815     if (nb_ksymtab_entries_ == 0)
7816       {
7817         Elf_Scn *section = find_ksymtab_section();
7818         if (section)
7819           {
7820             GElf_Shdr header_mem;
7821             GElf_Shdr *section_header = gelf_getshdr(section, &header_mem);
7822             size_t entry_size = get_ksymtab_entry_size();
7823             ABG_ASSERT(entry_size);
7824             nb_ksymtab_entries_ = section_header->sh_size / entry_size;
7825           }
7826       }
7827     return nb_ksymtab_entries_;
7828   }
7829
7830   /// Getter of the number of entries that are present in the
7831   /// ksymtab_gpl section.
7832   ///
7833   /// @return the number of entries that are present in the
7834   /// ksymtab_gpl section.
7835   size_t
7836   get_nb_ksymtab_gpl_entries()
7837   {
7838     if (nb_ksymtab_gpl_entries_ == 0)
7839       {
7840         Elf_Scn *section = find_ksymtab_gpl_section();
7841         if (section)
7842           {
7843             GElf_Shdr header_mem;
7844             GElf_Shdr *section_header = gelf_getshdr(section, &header_mem);
7845             size_t entry_size = get_ksymtab_entry_size();
7846             ABG_ASSERT(entry_size);
7847             nb_ksymtab_gpl_entries_ = section_header->sh_size / entry_size;
7848           }
7849       }
7850     return nb_ksymtab_gpl_entries_;
7851   }
7852
7853   /// Populate the symbol map by reading exported symbols from the
7854   /// ksymtab directly.
7855   ///
7856   /// @param section the ksymtab section to read from
7857   ///
7858   /// @param exported_fns_set the set of exported functions
7859   ///
7860   /// @param exported_vars_set the set of exported variables
7861   ///
7862   /// @param nb_entries the number of ksymtab entries to read
7863   ///
7864   /// @return true upon successful completion, false otherwise.
7865   bool
7866   populate_symbol_map_from_ksymtab(Elf_Scn *section,
7867                                    address_set_sptr exported_fns_set,
7868                                    address_set_sptr exported_vars_set,
7869                                    size_t nb_entries)
7870   {
7871     // The data of the section.
7872     Elf_Data *elf_data = elf_rawdata(section, 0);
7873
7874     // An array-of-bytes view of the elf data above.  Something we can
7875     // actually program with.  Phew.
7876     uint8_t *bytes = reinterpret_cast<uint8_t*>(elf_data->d_buf);
7877
7878     // This is where to store an address of a symbol that we read from
7879     // the section.
7880     GElf_Addr symbol_address = 0, adjusted_symbol_address = 0;
7881
7882     // So the section is an array of entries.  Each entry describes a
7883     // symbol.  Each entry is made of two words.
7884     //
7885     // The first word is the address of a symbol.  The second one is
7886     // the address of a static global variable symbol which value is
7887     // the string representing the symbol name.  That string is in the
7888     // __ksymtab_strings section.  Here, we are only interested in the
7889     // first entry.
7890     //
7891     // Lets thus walk the array of entries, and let's read just the
7892     // symbol address part of each entry.
7893     bool is_big_endian = elf_architecture_is_big_endian();
7894     elf_symbol_sptr symbol;
7895     unsigned char symbol_value_size = get_ksymtab_symbol_value_size();
7896
7897     for (size_t i = 0, entry_offset = 0;
7898          i < nb_entries;
7899          ++i, entry_offset = get_ksymtab_entry_size() * i)
7900       {
7901         symbol_address = 0;
7902         ABG_ASSERT(read_int_from_array_of_bytes(&bytes[entry_offset],
7903                                                 symbol_value_size,
7904                                                 is_big_endian,
7905                                                 symbol_address));
7906
7907         // Starting from linux kernel v4.19, it can happen that the
7908         // address value read from the ksymtab[_gpl] section might
7909         // need some decoding to get the real symbol address that has
7910         // a meaning in the .symbol section.
7911         symbol_address =
7912           maybe_adjust_sym_address_from_v4_19_ksymtab(symbol_address,
7913                                                       entry_offset, section);
7914
7915         // We might also want to adjust the symbol address, depending
7916         // on if we are looking at an ET_REL, an executable or a
7917         // shared object binary.
7918         adjusted_symbol_address = maybe_adjust_fn_sym_address(symbol_address);
7919
7920         if (adjusted_symbol_address == 0)
7921           // The resulting symbol address is zero, not sure this
7922           // valid; ignore it.
7923           continue;
7924
7925         // OK now the symbol address should be in a suitable form to
7926         // be used to look the symbol up in the usual .symbol section
7927         // (aka ELF symbol table).
7928         symbol = lookup_elf_symbol_from_address(adjusted_symbol_address);
7929         if (!symbol)
7930           {
7931             adjusted_symbol_address =
7932               maybe_adjust_var_sym_address(symbol_address);
7933             symbol = lookup_elf_symbol_from_address(adjusted_symbol_address);
7934             if (!symbol)
7935               // This must be a symbol that is of type neither FUNC
7936               // (function) nor OBJECT (variable).  There are for intance,
7937               // symbols of type 'NOTYPE' in the ksymtab symbol table.  I
7938               // am not sure what those are.
7939               continue;
7940           }
7941
7942         address_set_sptr set;
7943         if (symbol->is_function())
7944           {
7945             ABG_ASSERT(lookup_elf_fn_symbol_from_address
7946                        (adjusted_symbol_address));
7947             set = exported_fns_set;
7948           }
7949         else if (symbol->is_variable())
7950           {
7951             ABG_ASSERT(lookup_elf_var_symbol_from_address
7952                        (adjusted_symbol_address));
7953             set = exported_vars_set;
7954           }
7955         else
7956           ABG_ASSERT_NOT_REACHED;
7957         set->insert(adjusted_symbol_address);
7958       }
7959     return true;
7960   }
7961
7962   /// Populate the symbol map by extracting the exported symbols from a
7963   /// ksymtab rela section.
7964   ///
7965   /// @param section the ksymtab section to read from
7966   ///
7967   /// @param exported_fns_set the set of exported functions
7968   ///
7969   /// @param exported_vars_set the set of exported variables
7970   ///
7971   /// @return true upon successful completion, false otherwise.
7972   bool
7973   populate_symbol_map_from_ksymtab_reloc(Elf_Scn *reloc_section,
7974                                          address_set_sptr exported_fns_set,
7975                                          address_set_sptr exported_vars_set)
7976   {
7977     GElf_Shdr reloc_section_mem;
7978     GElf_Shdr *reloc_section_shdr = gelf_getshdr(reloc_section,
7979                                                  &reloc_section_mem);
7980     size_t reloc_count =
7981       reloc_section_shdr->sh_size / reloc_section_shdr->sh_entsize;
7982
7983     Elf_Data *reloc_section_data = elf_getdata(reloc_section, 0);
7984
7985     bool is_relasec = (reloc_section_shdr->sh_type == SHT_RELA);
7986     elf_symbol_sptr symbol;
7987     GElf_Sym native_symbol;
7988     for (unsigned int i = 0; i < reloc_count; i++)
7989       {
7990         if (is_relasec)
7991           {
7992             GElf_Rela rela;
7993             gelf_getrela(reloc_section_data, i, &rela);
7994             symbol = lookup_elf_symbol_from_index(GELF_R_SYM(rela.r_info),
7995                                                   native_symbol);
7996           }
7997         else
7998           {
7999             GElf_Rel rel;
8000             gelf_getrel(reloc_section_data, i, &rel);
8001             symbol = lookup_elf_symbol_from_index(GELF_R_SYM(rel.r_info),
8002                                                   native_symbol);
8003           }
8004
8005         ABG_ASSERT(symbol);
8006
8007         // If the symbol is a linux string constant then ignore it.
8008         if (symbol->get_is_linux_string_cst())
8009           continue;
8010
8011         if (!symbol->is_function() && !symbol->is_variable())
8012           {
8013             if (do_log())
8014               {
8015                 if (symbol->get_type() == elf_symbol::NOTYPE_TYPE)
8016                   cerr << "skipping NOTYPE symbol "
8017                        << symbol->get_name()
8018                        << " shndx: "
8019                        << symbol->get_index()
8020                        << " @"
8021                        << elf_path()
8022                        << "\n";
8023                 else if (symbol->get_type() == elf_symbol::SECTION_TYPE)
8024                   cerr << "skipping SECTION symbol "
8025                        << "shndx: "
8026                        << symbol->get_index()
8027                        << " @"
8028                        << elf_path()
8029                        << "\n";
8030                }
8031             continue;
8032           }
8033
8034         // If we are looking at an ET_REL (relocatable) binary, then
8035         // the symbol value of native_symbol is relative to the
8036         // section that symbol is defined in.  We need to translate it
8037         // into an absolute (okay, binary-relative, rather) address.
8038         GElf_Addr symbol_address =
8039           maybe_adjust_et_rel_sym_addr_to_abs_addr (&native_symbol);
8040
8041         address_set_sptr set;
8042         if (symbol->is_function())
8043           {
8044             ABG_ASSERT(lookup_elf_fn_symbol_from_address(symbol_address));
8045             set = exported_fns_set;
8046           }
8047         else if (symbol->is_variable())
8048           {
8049             ABG_ASSERT(lookup_elf_var_symbol_from_address(symbol_address));
8050             set = exported_vars_set;
8051           }
8052         else
8053           ABG_ASSERT_NOT_REACHED;
8054         set->insert(symbol_address);
8055       }
8056     return true;
8057   }
8058
8059   /// Load a given kernel symbol table.
8060   ///
8061   /// One can thus retrieve the resulting symbols by using the
8062   /// accessors read_context::linux_exported_fn_syms(),
8063   /// read_context::linux_exported_var_syms(),
8064   /// read_context::linux_exported_gpl_fn_syms(), or
8065   /// read_context::linux_exported_gpl_var_syms().
8066   ///
8067   /// @param kind the kind of kernel symbol table to load.
8068   ///
8069   /// @return true upon successful completion, false otherwise.
8070   bool
8071   load_kernel_symbol_table(kernel_symbol_table_kind kind)
8072   {
8073     size_t nb_entries = 0;
8074     Elf_Scn *section = 0, *reloc_section = 0;
8075     address_set_sptr linux_exported_fns_set, linux_exported_vars_set;
8076
8077     switch (kind)
8078       {
8079       case KERNEL_SYMBOL_TABLE_KIND_UNDEFINED:
8080         break;
8081       case KERNEL_SYMBOL_TABLE_KIND_KSYMTAB:
8082         section = find_ksymtab_section();
8083         reloc_section = find_ksymtab_reloc_section();
8084         nb_entries = get_nb_ksymtab_entries();
8085         linux_exported_fns_set = create_or_get_linux_exported_fn_syms();
8086         linux_exported_vars_set = create_or_get_linux_exported_var_syms();
8087         break;
8088       case KERNEL_SYMBOL_TABLE_KIND_KSYMTAB_GPL:
8089         section = find_ksymtab_gpl_section();
8090         reloc_section = find_ksymtab_gpl_reloc_section();
8091         nb_entries = get_nb_ksymtab_gpl_entries();
8092         linux_exported_fns_set = create_or_get_linux_exported_gpl_fn_syms();
8093         linux_exported_vars_set = create_or_get_linux_exported_gpl_var_syms();
8094         break;
8095       }
8096
8097     if (!linux_exported_vars_set
8098         || !linux_exported_fns_set
8099         || !section
8100         || !nb_entries)
8101       return false;
8102
8103     ksymtab_format format = get_ksymtab_format();
8104
8105     // Although pre-v4.19 kernel modules can have a relocation section for the
8106     // __ksymtab section, libdwfl zeroes the rela section after applying
8107     // "simple" absolute relocations via dwfl_module_getelf(). For v4.19 and
8108     // above, we get PC-relative relocations so dwfl_module_getelf() doesn't
8109     // apply those relocations and we're safe to read the relocation section to
8110     // determine which exported symbols are in the ksymtab.
8111     if (!reloc_section || format == PRE_V4_19_KSYMTAB_FORMAT)
8112       return populate_symbol_map_from_ksymtab(section, linux_exported_fns_set,
8113                                               linux_exported_vars_set,
8114                                               nb_entries);
8115     else
8116       return populate_symbol_map_from_ksymtab_reloc(reloc_section,
8117                                                     linux_exported_fns_set,
8118                                                     linux_exported_vars_set);
8119   }
8120
8121   /// Load the special __ksymtab section. This is for linux kernel
8122   /// (module) files.
8123   ///
8124   /// @return true upon successful completion, false otherwise.
8125   bool
8126   load_ksymtab_symbols()
8127   {
8128     return load_kernel_symbol_table(KERNEL_SYMBOL_TABLE_KIND_KSYMTAB);
8129   }
8130
8131   /// Load the special __ksymtab_gpl section. This is for linux kernel
8132   /// (module) files.
8133   ///
8134   /// @return true upon successful completion, false otherwise.
8135   bool
8136   load_ksymtab_gpl_symbols()
8137   {
8138     return load_kernel_symbol_table(KERNEL_SYMBOL_TABLE_KIND_KSYMTAB_GPL);
8139   }
8140
8141   /// Load linux kernel (module) specific exported symbol sections.
8142   ///
8143   /// @return true upon successful completion, false otherwise.
8144   bool
8145   load_linux_specific_exported_symbol_maps()
8146   {
8147     bool loaded = false;
8148     if (!linux_exported_fn_syms_
8149         || !linux_exported_var_syms_)
8150       loaded |= load_ksymtab_symbols();
8151
8152     if (!linux_exported_gpl_fn_syms_
8153         || !linux_exported_gpl_var_syms_)
8154       loaded |= load_ksymtab_gpl_symbols();
8155
8156     return loaded;
8157   }
8158
8159   /// Load the maps of function symbol address -> function symbol,
8160   /// global variable symbol address -> variable symbol and also the
8161   /// maps of function and variable undefined symbols.
8162   ///
8163   /// All these maps are loaded only if they are not loaded already.
8164   ///
8165   /// @return true iff everything went fine.
8166   bool
8167   load_symbol_maps()
8168   {
8169     bool load_fun_map = !fun_addr_sym_map_ ;
8170     bool load_var_map = !var_addr_sym_map_;
8171     bool load_undefined_fun_map = !undefined_fun_syms_;
8172     bool load_undefined_var_map = !undefined_var_syms_;
8173
8174     if (!fun_syms_)
8175       fun_syms_.reset(new string_elf_symbols_map_type);
8176
8177     if (!fun_addr_sym_map_)
8178       fun_addr_sym_map_.reset(new addr_elf_symbol_sptr_map_type);
8179
8180     if (!fun_entry_addr_sym_map_ && elf_architecture_is_ppc64())
8181       fun_entry_addr_sym_map_.reset(new addr_elf_symbol_sptr_map_type);
8182
8183     if (!var_syms_)
8184       var_syms_.reset(new string_elf_symbols_map_type);
8185
8186     if (!var_addr_sym_map_)
8187       var_addr_sym_map_.reset(new addr_elf_symbol_sptr_map_type);
8188
8189     if (!undefined_fun_syms_)
8190       undefined_fun_syms_.reset(new string_elf_symbols_map_type);
8191
8192     if (!undefined_var_syms_)
8193       undefined_var_syms_.reset(new string_elf_symbols_map_type);
8194
8195     if (!options_.ignore_symbol_table)
8196       {
8197         if (load_symbol_maps_from_symtab_section(load_fun_map,
8198                                                  load_var_map,
8199                                                  load_undefined_fun_map,
8200                                                  load_undefined_var_map))
8201           {
8202             if (load_in_linux_kernel_mode() && is_linux_kernel_binary())
8203               return load_linux_specific_exported_symbol_maps();
8204             return true;
8205           }
8206         return false;
8207       }
8208     return true;
8209   }
8210
8211   /// Return true if an address is in the ".opd" section that is
8212   /// present on the ppc64 platform.
8213   ///
8214   /// @param addr the address to consider.
8215   ///
8216   /// @return true iff @p addr is designates a word that is in the
8217   /// ".opd" section.
8218   bool
8219   address_is_in_opd_section(Dwarf_Addr addr)
8220   {
8221     Elf_Scn * opd_section = find_opd_section();
8222     if (!opd_section)
8223       return false;
8224     if (address_is_in_section(addr, opd_section))
8225       return true;
8226     return false;
8227   }
8228
8229   /// Load the symbol maps if necessary.
8230   ///
8231   /// @return true iff the symbol maps has been loaded by this
8232   /// invocation.
8233   bool
8234   maybe_load_symbol_maps() const
8235   {
8236     if (!fun_addr_sym_map_
8237         || !var_addr_sym_map_
8238         || !fun_syms_
8239         || !var_syms_
8240         || !undefined_fun_syms_
8241         || !undefined_var_syms_)
8242       return const_cast<read_context*>(this)->load_symbol_maps();
8243     return false;
8244   }
8245
8246   /// Load the DT_NEEDED and DT_SONAME elf TAGS.
8247   ///
8248   void
8249   load_dt_soname_and_needed()
8250   {
8251     lookup_data_tag_from_dynamic_segment(elf_handle(), DT_NEEDED, dt_needed_);
8252
8253     vector<string> dt_tag_data;
8254     lookup_data_tag_from_dynamic_segment(elf_handle(), DT_SONAME, dt_tag_data);
8255     if (!dt_tag_data.empty())
8256       dt_soname_ = dt_tag_data[0];
8257   }
8258
8259   /// Read the string representing the architecture of the current ELF
8260   /// file.
8261   void
8262   load_elf_architecture()
8263   {
8264     if (!elf_handle())
8265       return;
8266
8267     GElf_Ehdr eh_mem;
8268     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
8269
8270     elf_architecture_ = e_machine_to_string(elf_header->e_machine);
8271   }
8272
8273   /// Load various ELF data.
8274   ///
8275   /// This function loads ELF data that are not symbol maps or debug
8276   /// info.  That is, things like various tags, elf architecture and
8277   /// so on.
8278   void
8279   load_elf_properties()
8280   {
8281     load_dt_soname_and_needed();
8282     load_elf_architecture();
8283   }
8284
8285   /// Convert the value of the symbol address part of a post V4.19
8286   /// ksymtab entry (that contains place-relative addresses) into its
8287   /// corresponding symbol value in the .symtab section.  The value of
8288   /// the symbol in .symtab equals to addr_offset + address-of-ksymtab
8289   /// + addr.
8290   ///
8291   /// @param addr the address read from the ksymtab section.
8292   ///
8293   /// @param addr_offset the offset at which @p addr was read.
8294   ///
8295   /// @param ksymtab_section the kymstab section @p addr was read
8296   /// from.
8297   GElf_Addr
8298   maybe_adjust_sym_address_from_v4_19_ksymtab(GElf_Addr addr,
8299                                               size_t addr_offset,
8300                                               Elf_Scn *ksymtab_section) const
8301   {
8302     GElf_Addr result = addr;
8303
8304     if (get_ksymtab_format() == V4_19_KSYMTAB_FORMAT)
8305       {
8306         int32_t offset = addr;
8307         GElf_Shdr mem;
8308         GElf_Shdr *section_header = gelf_getshdr(ksymtab_section, &mem);
8309         result = offset + section_header->sh_addr + addr_offset;
8310       }
8311
8312     return result;
8313   }
8314
8315   /// This is a sub-routine of maybe_adjust_fn_sym_address and
8316   /// maybe_adjust_var_sym_address.
8317   ///
8318   /// Given an address that we got by looking at some debug
8319   /// information (e.g, a symbol's address referred to by a DWARF
8320   /// TAG), If the ELF file we are interested in is a shared library
8321   /// or an executable, then adjust the address to be coherent with
8322   /// where the executable (or shared library) is loaded.  That way,
8323   /// the address can be used to look for symbols in the executable or
8324   /// shared library.
8325   ///
8326   /// @return the adjusted address, or the same address as @p addr if
8327   /// it didn't need any adjustment.
8328   Dwarf_Addr
8329   maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
8330   {
8331     if (addr == 0)
8332       return addr;
8333
8334     GElf_Ehdr eh_mem;
8335     GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
8336
8337     if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
8338       {
8339         Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
8340         ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
8341                                            dwarf_elf_load_address));
8342         ABG_ASSERT(get_binary_load_address(elf_handle(),
8343                                            elf_load_address));
8344         if (dwarf_is_splitted()
8345             && (dwarf_elf_load_address != elf_load_address))
8346           // This means that in theory the DWARF and the executable are
8347           // not loaded at the same address.  And addr is meaningful
8348           // only in the context of the DWARF.
8349           //
8350           // So let's transform addr into an offset relative to where
8351           // the DWARF is loaded, and let's add that relative offset
8352           // to the load address of the executable.  That way, addr
8353           // becomes meaningful in the context of the executable and
8354           // can thus be used to compare against the address of
8355           // symbols of the executable, for instance.
8356           addr = addr - dwarf_elf_load_address + elf_load_address;
8357       }
8358
8359     return addr;
8360   }
8361
8362   /// For a relocatable (*.o) elf file, this function expects an
8363   /// absolute address, representing a function symbol.  It then
8364   /// extracts the address of the .text section from the symbol
8365   /// absolute address to get the relative address of the function
8366   /// from the beginning of the .text section.
8367   ///
8368   /// For executable or shared library, this function expects an
8369   /// address of a function symbol that was retrieved by looking at a
8370   /// DWARF "file".  The function thus adjusts the address to make it
8371   /// be meaningful in the context of the ELF file.
8372   ///
8373   /// In both cases, the address can then be compared against the
8374   /// st_value field of a function symbol from the ELF file.
8375   ///
8376   /// @param addr an adress for a function symbol that was retrieved
8377   /// from a DWARF file.
8378   ///
8379   /// @return the (possibly) adjusted address, or just @p addr if no
8380   /// adjustment took place.
8381   Dwarf_Addr
8382   maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
8383   {
8384     if (addr == 0)
8385       return addr;
8386
8387     Elf* elf = elf_handle();
8388     GElf_Ehdr eh_mem;
8389     GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
8390
8391     if (elf_header->e_type == ET_REL)
8392       // We are looking at a relocatable file.  In this case, we don't
8393       // do anything because:
8394       //
8395       // 1/ the addresses from DWARF are absolute (relative to the
8396       // beginning of the relocatable file)
8397       //
8398       // 2/ The ELF symbol addresses that we store in our lookup
8399       // tables are translated from section-related to absolute as
8400       // well.  So we don't have anything to do at this point for
8401       // ET_REL files.
8402       ;
8403     else
8404       addr = maybe_adjust_address_for_exec_or_dyn(addr);
8405
8406     return addr;
8407   }
8408
8409   /// Translate a section-relative symbol address (i.e, symbol value)
8410   /// into an absolute symbol address by adding the address of the
8411   /// section the symbol belongs to, to the address value.
8412   ///
8413   /// This is useful when looking at symbol values coming from
8414   /// relocatable files (of ET_REL kind).  If the binary is not
8415   /// ET_REL, then the function does nothing and returns the input
8416   /// address unchanged.
8417   ///
8418   /// @param addr the symbol address to possibly translate.
8419   ///
8420   /// @param section the section the symbol which value is @p addr
8421   /// belongs to.
8422   ///
8423   /// @return the section-relative address, translated into an
8424   /// absolute address, if @p section is an ET_REL binary.  Otherwise,
8425   /// return @p addr, unchanged.
8426   GElf_Addr
8427   maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Addr addr, Elf_Scn *section)
8428   {
8429     if (section == 0)
8430       return addr;
8431
8432     Elf* elf = elf_handle();
8433     GElf_Ehdr elf_header;
8434
8435     if (!gelf_getehdr(elf, &elf_header))
8436       return addr;
8437
8438     if (elf_header.e_type != ET_REL)
8439       return addr;
8440
8441     GElf_Shdr section_header;
8442     if (!gelf_getshdr(section, &section_header))
8443       return addr;
8444
8445     return addr + section_header.sh_addr;
8446   }
8447
8448   /// Translate a section-relative symbol address (i.e, symbol value)
8449   /// into an absolute symbol address by adding the address of the
8450   /// section the symbol belongs to, to the address value.
8451   ///
8452   /// This is useful when looking at symbol values coming from
8453   /// relocatable files (of ET_REL kind).  If the binary is not
8454   /// ET_REL, then the function does nothing and returns the input
8455   /// address unchanged.
8456   ///
8457   /// @param sym the symbol whose address to possibly needs to be
8458   /// translated.
8459   ///
8460   /// @return the section-relative address, translated into an
8461   /// absolute address, if @p sym is from an ET_REL binary.
8462   /// Otherwise, return the address of @p sym, unchanged.
8463   GElf_Addr
8464   maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Sym *sym)
8465   {
8466     Elf_Scn *symbol_section = elf_getscn(elf_handle(), sym->st_shndx);
8467     ABG_ASSERT(symbol_section);
8468     GElf_Addr result = sym->st_value;
8469     result = maybe_adjust_et_rel_sym_addr_to_abs_addr(result, symbol_section);
8470     return result;
8471   }
8472
8473   /// Test if a given address is in a given section.
8474   ///
8475   /// @param addr the address to consider.
8476   ///
8477   /// @param section the section to consider.
8478   bool
8479   address_is_in_section(Dwarf_Addr addr, Elf_Scn* section) const
8480   {
8481     if (!section)
8482       return false;
8483
8484     GElf_Shdr sheader_mem;
8485     GElf_Shdr* sheader = gelf_getshdr(section, &sheader_mem);
8486
8487     if (sheader->sh_addr <= addr && addr <= sheader->sh_addr + sheader->sh_size)
8488       return true;
8489
8490     return false;
8491   }
8492
8493   /// Get the section which a global variable address comes from.
8494   ///
8495   /// @param var_addr the address for the variable.
8496   ///
8497   /// @return the ELF section the @p var_addr comes from, or nil if no
8498   /// section was found for that variable address.
8499   Elf_Scn*
8500   get_data_section_for_variable_address(Dwarf_Addr var_addr) const
8501   {
8502     // There are several potential 'data sections" from which a
8503     // variable address can come from: .data, .data1 and .rodata.
8504     // Let's try to try them all in sequence.
8505
8506     Elf_Scn* data_scn = bss_section();
8507     if (!address_is_in_section(var_addr, data_scn))
8508       {
8509         data_scn = data_section();
8510         if (!address_is_in_section(var_addr, data_scn))
8511           {
8512             data_scn = data1_section();
8513             if (!address_is_in_section(var_addr, data_scn))
8514               {
8515                 data_scn = rodata_section();
8516                 if (!address_is_in_section(var_addr, data_scn))
8517                   return 0;
8518               }
8519           }
8520       }
8521     return data_scn;
8522   }
8523
8524   /// For a relocatable (*.o) elf file, this function expects an
8525   /// absolute address, representing a global variable symbol.  It
8526   /// then extracts the address of the {.data,.data1,.rodata,.bss}
8527   /// section from the symbol absolute address to get the relative
8528   /// address of the variable from the beginning of the data section.
8529   ///
8530   /// For executable or shared library, this function expects an
8531   /// address of a variable symbol that was retrieved by looking at a
8532   /// DWARF "file".  The function thus adjusts the address to make it
8533   /// be meaningful in the context of the ELF file.
8534   ///
8535   /// In both cases, the address can then be compared against the
8536   /// st_value field of a function symbol from the ELF file.
8537   ///
8538   /// @param addr an address for a global variable symbol that was
8539   /// retrieved from a DWARF file.
8540   ///
8541   /// @return the (possibly) adjusted address, or just @p addr if no
8542   /// adjustment took place.
8543   Dwarf_Addr
8544   maybe_adjust_var_sym_address(Dwarf_Addr addr) const
8545   {
8546     Elf* elf = elf_handle();
8547     GElf_Ehdr eh_mem;
8548     GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
8549
8550     if (elf_header->e_type == ET_REL)
8551       // We are looking at a relocatable file.  In this case, we don't
8552       // do anything because:
8553       //
8554       // 1/ the addresses from DWARF are absolute (relative to the
8555       // beginning of the relocatable file)
8556       //
8557       // 2/ The ELF symbol addresses that we store in our lookup
8558       // tables are translated from section-related to absolute as
8559       // well.  So we don't have anything to do at this point for
8560       // ET_REL files.
8561       ;
8562     else
8563       addr = maybe_adjust_address_for_exec_or_dyn(addr);
8564
8565     return addr;
8566   }
8567
8568
8569   /// Get the address of the function.
8570   ///
8571   /// The address of the function is considered to be the value of the
8572   /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
8573   /// only) to not point to an absolute address anymore, but rather to
8574   /// the address of the function inside the .text segment.
8575   ///
8576   /// @param function_die the die of the function to consider.
8577   ///
8578   /// @param address the resulting address iff the function returns
8579   /// true.
8580   ///
8581   /// @return true if the function address was found.
8582   bool
8583   get_function_address(Dwarf_Die* function_die,
8584                        Dwarf_Addr& address) const
8585   {
8586     Dwarf_Addr low_pc = 0;
8587     if (!die_address_attribute(function_die, DW_AT_low_pc, low_pc))
8588       return false;
8589
8590     low_pc = maybe_adjust_fn_sym_address(low_pc);
8591     address = low_pc;
8592     return true;
8593   }
8594
8595   /// Get the address of the global variable.
8596   ///
8597   /// The address of the global variable is considered to be the value
8598   /// of the DW_AT_location attribute, possibly adjusted (in
8599   /// relocatable files only) to not point to an absolute address
8600   /// anymore, but rather to the address of the global variable inside
8601   /// the data segment.
8602   ///
8603   /// @param variable_die the die of the function to consider.
8604   ///
8605   /// @param address the resulting address iff this function returns
8606   /// true.
8607   ///
8608   /// @return true if the variable address was found.
8609   bool
8610   get_variable_address(Dwarf_Die*       variable_die,
8611                        Dwarf_Addr&      address) const
8612   {
8613     bool is_tls_address = false;
8614     if (!die_location_address(variable_die, address, is_tls_address))
8615       return false;
8616     if (!is_tls_address)
8617       address = maybe_adjust_var_sym_address(address);
8618     return true;
8619   }
8620
8621   /// Tests if a suppression specification can match ABI artifacts
8622   /// coming from the binary being analyzed.
8623   ///
8624   /// This tests if the suppression matches the soname of and binary
8625   /// name of the ELF binary being analyzed.
8626   ///
8627   /// @param s the suppression specification to consider.
8628   bool
8629   suppression_can_match(const suppr::suppression_base& s) const
8630   {
8631     if (s.priv_->matches_soname(dt_soname())
8632         && s.priv_->matches_binary_name(elf_path()))
8633       return true;
8634     return false;
8635   }
8636
8637   /// Test whether if a given function suppression matches a function
8638   /// designated by a regular expression that describes its linkage
8639   /// name (symbol name).
8640   ///
8641   /// @param s the suppression specification to evaluate to see if it
8642   /// matches a given function linkage name
8643   ///
8644   /// @param fn_linkage_name the linkage name of the function of interest.
8645   ///
8646   /// @return true iff the suppression specification @p s matches the
8647   /// function whose linkage name is @p fn_linkage_name.
8648   bool
8649   suppression_matches_function_sym_name(const suppr::function_suppression_sptr& s,
8650                                         const string& fn_linkage_name) const
8651   {
8652     if (!s)
8653       return false;
8654     return suppression_matches_function_sym_name(*s,fn_linkage_name);
8655   }
8656
8657   /// Test whether if a given function suppression matches a function
8658   /// designated by a regular expression that describes its linkage
8659   /// name (symbol name).
8660   ///
8661   /// @param s the suppression specification to evaluate to see if it
8662   /// matches a given function linkage name
8663   ///
8664   /// @param fn_linkage_name the linkage name of the function of interest.
8665   ///
8666   /// @return true iff the suppression specification @p s matches the
8667   /// function whose linkage name is @p fn_linkage_name.
8668   bool
8669   suppression_matches_function_sym_name(const suppr::function_suppression& s,
8670                                         const string& fn_linkage_name) const
8671   {
8672     if (!suppression_can_match(s))
8673       return false;
8674
8675     return suppr::suppression_matches_function_sym_name(s, fn_linkage_name);
8676   }
8677
8678   /// Test whether if a given function suppression matches a function
8679   /// designated by a regular expression that describes its name.
8680   ///
8681   /// @param s the suppression specification to evaluate to see if it
8682   /// matches a given function name.
8683   ///
8684   /// @param fn_name the name of the function of interest.  Note that
8685   /// this name must be *non* qualified.
8686   ///
8687   /// @return true iff the suppression specification @p s matches the
8688   /// function whose name is @p fn_name.
8689   bool
8690   suppression_matches_function_name(const suppr::function_suppression_sptr& s,
8691                                     const string& fn_name) const
8692   {
8693     if (!s)
8694       return false;
8695     return suppression_matches_function_name(*s, fn_name);
8696   }
8697
8698   /// Test whether if a given function suppression matches a function
8699   /// designated by a regular expression that describes its name.
8700   ///
8701   /// @param s the suppression specification to evaluate to see if it
8702   /// matches a given function name.
8703   ///
8704   /// @param fn_name the name of the function of interest.  Note that
8705   /// this name must be *non* qualified.
8706   ///
8707   /// @return true iff the suppression specification @p s matches the
8708   /// function whose name is @p fn_name.
8709   bool
8710   suppression_matches_function_name(const suppr::function_suppression& s,
8711                                     const string& fn_name) const
8712   {
8713     if (!suppression_can_match(s))
8714       return false;
8715
8716     return suppr::suppression_matches_function_name(s, fn_name);
8717   }
8718
8719   /// Test whether if a given variable suppression specification
8720   /// matches a variable denoted by its name.
8721   ///
8722   /// @param s the variable suppression specification to consider.
8723   ///
8724   /// @param var_name the name of the variable to consider.
8725   ///
8726   /// @return true iff the suppression specification @p s matches the
8727   /// variable whose name is @p var_name.
8728   bool
8729   suppression_matches_variable_name(const suppr::variable_suppression& s,
8730                                     const string& var_name) const
8731   {
8732     if (!suppression_can_match(s))
8733       return false;
8734
8735     return suppr::suppression_matches_variable_name(s, var_name);
8736   }
8737
8738   /// Test whether if a given variable suppression specification
8739   /// matches a variable denoted by its linkage name.
8740   ///
8741   /// @param s the variable suppression specification to consider.
8742   ///
8743   /// @param var_linkage_name the linkage name of the variable to consider.
8744   ///
8745   /// @return true iff variable suppression specification @p s matches
8746   /// the variable denoted by linkage name @p var_linkage_name.
8747   bool
8748   suppression_matches_variable_sym_name(const suppr::variable_suppression& s,
8749                                         const string& var_linkage_name) const
8750   {
8751     if (!suppression_can_match(s))
8752       return false;
8753
8754     return suppr::suppression_matches_variable_sym_name(s, var_linkage_name);
8755   }
8756
8757   /// Test if a given type suppression specification matches a type
8758   /// designated by its name and location.
8759   ///
8760   /// @param s the suppression specification to consider.
8761   ///
8762   /// @param type_name the fully qualified type name to consider.
8763   ///
8764   /// @param type_location the type location to consider.
8765   ///
8766   /// @return true iff the type suppression specification matches a
8767   /// type of a given name and location.
8768   bool
8769   suppression_matches_type_name_or_location(const suppr::type_suppression& s,
8770                                             const string& type_name,
8771                                             const location& type_location) const
8772   {
8773     if (!suppression_can_match(s))
8774       return false;
8775
8776     return suppr::suppression_matches_type_name_or_location(s, type_name,
8777                                                             type_location);
8778   }
8779
8780   /// Test if a type suppression specification matches the name of a
8781   /// type within a given scope.
8782   ///
8783   /// @param s the type suppression specification to consider.
8784   ///
8785   /// @param type_scope the type scope to consider.
8786   ///
8787   /// @param type the type to consider.
8788   ///
8789   /// @return true iff the type suppression specification matches a
8790   /// the name of type @p type.
8791   bool
8792   suppression_matches_type_name(const suppr::type_suppression&  s,
8793                                 const scope_decl*               type_scope,
8794                                 const type_base_sptr&           type) const
8795   {
8796     if (!suppression_can_match(s))
8797       return false;
8798     return suppr::suppression_matches_type_name(s, type_scope, type);
8799   }
8800
8801   /// Getter of the exported decls builder object.
8802   ///
8803   /// @return the exported decls builder.
8804   corpus::exported_decls_builder*
8805   exported_decls_builder()
8806   {return exported_decls_builder_;}
8807
8808   /// Setter of the exported decls builder object.
8809   ///
8810   /// Note that this @ref read_context is not responsible for the live
8811   /// time of the exported_decls_builder object.  The corpus is.
8812   ///
8813   /// @param b the new builder.
8814   void
8815   exported_decls_builder(corpus::exported_decls_builder* b)
8816   {exported_decls_builder_ = b;}
8817
8818   /// Getter of the "load_all_types" flag.  This flag tells if all the
8819   /// types (including those not reachable by public declarations) are
8820   /// to be read and represented in the final ABI corpus.
8821   ///
8822   /// @return the load_all_types flag.
8823   bool
8824   load_all_types() const
8825   {return options_.load_all_types;}
8826
8827   /// Setter of the "load_all_types" flag.  This flag tells if all the
8828   /// types (including those not reachable by public declarations) are
8829   /// to be read and represented in the final ABI corpus.
8830   ///
8831   /// @param f the new load_all_types flag.
8832   void
8833   load_all_types(bool f)
8834   {options_.load_all_types = f;}
8835
8836   bool
8837   load_in_linux_kernel_mode() const
8838   {return options_.load_in_linux_kernel_mode;}
8839
8840   void
8841   load_in_linux_kernel_mode(bool f)
8842   {options_.load_in_linux_kernel_mode = f;}
8843
8844   /// Guess if the current binary is a Linux Kernel or a Linux Kernel module.
8845   ///
8846   /// To guess that, the function looks for the presence of the
8847   /// special "__ksymtab_strings" section in the binary.
8848   ///
8849   bool
8850   is_linux_kernel_binary() const
8851   {
8852     return find_section(elf_handle(), "__ksymtab_strings", SHT_PROGBITS)
8853            || is_linux_kernel_module();
8854   }
8855
8856   /// Guess if the current binary is a Linux Kernel module.
8857   ///
8858   /// To guess that, the function looks for the presence of the special
8859   /// ".modinfo" and ".gnu.linkonce.this_module" sections in the binary.
8860   ///
8861   bool
8862   is_linux_kernel_module() const
8863   {
8864     return find_section(elf_handle(), ".modinfo", SHT_PROGBITS)
8865            && find_section(elf_handle(), ".gnu.linkonce.this_module", SHT_PROGBITS);
8866   }
8867
8868   /// Getter of the "show_stats" flag.
8869   ///
8870   /// This flag tells if we should emit statistics about various
8871   /// internal stuff.
8872   ///
8873   /// @return the value of the flag.
8874   bool
8875   show_stats() const
8876   {return options_.show_stats;}
8877
8878   /// Setter of the "show_stats" flag.
8879   ///
8880   /// This flag tells if we should emit statistics about various
8881   /// internal stuff.
8882   ///
8883   /// @param f the value of the flag.
8884   void
8885   show_stats(bool f)
8886   {options_.show_stats = f;}
8887
8888   /// Getter of the "do_log" flag.
8889   ///
8890   /// This flag tells if we should log about various internal
8891   /// details.
8892   ///
8893   /// return the "do_log" flag.
8894   bool
8895   do_log() const
8896   {return options_.do_log;}
8897
8898   /// Setter of the "do_log" flag.
8899   ///
8900   /// This flag tells if we should log about various internal details.
8901   ///
8902   /// @param f the new value of the flag.
8903   void
8904   do_log(bool f)
8905   {options_.do_log = f;}
8906
8907   /// If a given function decl is suitable for the set of exported
8908   /// functions of the current corpus, this function adds it to that
8909   /// set.
8910   ///
8911   /// @param fn the function to consider for inclusion into the set of
8912   /// exported functions of the current corpus.
8913   void
8914   maybe_add_fn_to_exported_decls(function_decl* fn)
8915   {
8916     if (fn)
8917       if (corpus::exported_decls_builder* b = exported_decls_builder())
8918         b->maybe_add_fn_to_exported_fns(fn);
8919   }
8920
8921   /// If a given variable decl is suitable for the set of exported
8922   /// variables of the current corpus, this variable adds it to that
8923   /// set.
8924   ///
8925   /// @param fn the variable to consider for inclusion into the set of
8926   /// exported variables of the current corpus.
8927   void
8928   maybe_add_var_to_exported_decls(var_decl* var)
8929   {
8930     if (var)
8931       if (corpus::exported_decls_builder* b = exported_decls_builder())
8932         b->maybe_add_var_to_exported_vars(var);
8933   }
8934
8935   /// Walk the DIEs under a given die and for each child, populate the
8936   /// die -> parent map to record the child -> parent relationship
8937   /// that
8938   /// exists between the child and the given die.
8939   ///
8940   /// The function also builds the vector of places where units are
8941   /// imported.
8942   ///
8943   /// This is done recursively as for each child DIE, this function
8944   /// walks its children as well.
8945   ///
8946   /// @param die the DIE whose children to walk recursively.
8947   ///
8948   /// @param source where the DIE @p die comes from.
8949   ///
8950   /// @param imported_units a vector containing all the offsets of the
8951   /// points where unit have been imported, under @p die.
8952   void
8953   build_die_parent_relations_under(Dwarf_Die*                   die,
8954                                    die_source                   source,
8955                                    imported_unit_points_type &  imported_units)
8956   {
8957     if (!die)
8958       return;
8959
8960     offset_offset_map_type& parent_of = die_parent_map(source);
8961
8962     Dwarf_Die child;
8963     if (dwarf_child(die, &child) != 0)
8964       return;
8965
8966     do
8967       {
8968         parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
8969         if (dwarf_tag(&child) == DW_TAG_imported_unit)
8970           {
8971             Dwarf_Die imported_unit;
8972             if (die_die_attribute(&child, DW_AT_import, imported_unit))
8973               {
8974                 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
8975                 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
8976                 imported_units.push_back
8977                   (imported_unit_point(dwarf_dieoffset(&child),
8978                                        imported_unit,
8979                                        imported_unit_die_source));
8980               }
8981           }
8982         build_die_parent_relations_under(&child, source, imported_units);
8983       }
8984     while (dwarf_siblingof(&child, &child) == 0);
8985
8986   }
8987
8988   /// Determine if we do have to build a DIE -> parent map, depending
8989   /// on a given language.
8990   ///
8991   /// Some languages like C++, Ada etc, do have the concept of
8992   /// namespace and yet, the DIE data structure doesn't provide us
8993   /// with a way to get the parent namespace of a given DIE.  So for
8994   /// those languages, we need to build a DIE -> parent map so that we
8995   /// can get the namespace DIE (or more generally the scope DIE) of a given
8996   /// DIE as we need it.
8997   ///
8998   /// But then some more basic languages like C or assembly don't have
8999   /// that need.
9000   ///
9001   /// This function, depending on the language, tells us if we need to
9002   /// build the DIE -> parent map or not.
9003   ///
9004   /// @param lang the language to consider.
9005   ///
9006   /// @return true iff we need to build the DIE -> parent map for this
9007   /// language.
9008   bool
9009   do_we_build_die_parent_maps(translation_unit::language lang)
9010   {
9011     if (is_c_language(lang))
9012       return false;
9013
9014     switch (lang)
9015       {
9016       case translation_unit::LANG_UNKNOWN:
9017 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
9018       case translation_unit::LANG_Mips_Assembler:
9019 #endif
9020         return false;
9021       default:
9022         break;
9023       }
9024     return true;
9025   }
9026
9027   /// Walk all the DIEs accessible in the debug info (and in the
9028   /// alternate debug info as well) and build maps representing the
9029   /// relationship DIE -> parent.  That is, make it so that we can get
9030   /// the parent for a given DIE.
9031   ///
9032   /// Note that the goal of this map is to be able to get the parent
9033   /// of a given DIE. This is to mainly to handle namespaces.  For instance,
9034   /// when we get a DIE of a type, and we want to build an internal
9035   /// representation for it, we need to get its fully qualified name.
9036   /// For that, we need to know what is the parent DIE of that type
9037   /// DIE, so that we can know what the namespace of that type is.
9038   ///
9039   /// Note that as the C language doesn't have namespaces (all types
9040   /// are defined in the same global namespace), this function doesn't
9041   /// build the DIE -> parent map if the current translation unit
9042   /// comes from C.  This saves time on big C ELF files with a lot of
9043   /// DIEs.
9044   void
9045   build_die_parent_maps()
9046   {
9047     bool we_do_have_to_build_die_parent_map = false;
9048     uint8_t address_size = 0;
9049     size_t header_size = 0;
9050     // Get the DIE of the current translation unit, look at it to get
9051     // its language. If that language is in C, then all types are in
9052     // the global namespace so we don't need to build the DIE ->
9053     // parent map.  So we dont build it in that case.
9054     for (Dwarf_Off offset = 0, next_offset = 0;
9055          (dwarf_next_unit(dwarf(), offset, &next_offset, &header_size,
9056                           NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
9057          offset = next_offset)
9058       {
9059         Dwarf_Off die_offset = offset + header_size;
9060         Dwarf_Die cu;
9061         if (!dwarf_offdie(dwarf(), die_offset, &cu))
9062           continue;
9063
9064         uint64_t l = 0;
9065         die_unsigned_constant_attribute(&cu, DW_AT_language, l);
9066         translation_unit::language lang = dwarf_language_to_tu_language(l);
9067         if (do_we_build_die_parent_maps(lang))
9068           we_do_have_to_build_die_parent_map = true;
9069       }
9070
9071     if (!we_do_have_to_build_die_parent_map)
9072       return;
9073
9074     // Build the DIE -> parent relation for DIEs coming from the
9075     // .debug_info section in the alternate debug info file.
9076     die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
9077     for (Dwarf_Off offset = 0, next_offset = 0;
9078          (dwarf_next_unit(alt_dwarf(), offset, &next_offset, &header_size,
9079                           NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
9080          offset = next_offset)
9081       {
9082         Dwarf_Off die_offset = offset + header_size;
9083         Dwarf_Die cu;
9084         if (!dwarf_offdie(alt_dwarf(), die_offset, &cu))
9085           continue;
9086         cur_tu_die(&cu);
9087
9088         imported_unit_points_type& imported_units =
9089           tu_die_imported_unit_points_map(source)[die_offset] =
9090           imported_unit_points_type();
9091         build_die_parent_relations_under(&cu, source, imported_units);
9092       }
9093
9094     // Build the DIE -> parent relation for DIEs coming from the
9095     // .debug_info section of the main debug info file.
9096     source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
9097     address_size = 0;
9098     header_size = 0;
9099     for (Dwarf_Off offset = 0, next_offset = 0;
9100          (dwarf_next_unit(dwarf(), offset, &next_offset, &header_size,
9101                           NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
9102          offset = next_offset)
9103       {
9104         Dwarf_Off die_offset = offset + header_size;
9105         Dwarf_Die cu;
9106         if (!dwarf_offdie(dwarf(), die_offset, &cu))
9107           continue;
9108         cur_tu_die(&cu);
9109         imported_unit_points_type& imported_units =
9110           tu_die_imported_unit_points_map(source)[die_offset] =
9111           imported_unit_points_type();
9112         build_die_parent_relations_under(&cu, source, imported_units);
9113       }
9114
9115     // Build the DIE -> parent relation for DIEs coming from the
9116     // .debug_types section.
9117     source = TYPE_UNIT_DIE_SOURCE;
9118     address_size = 0;
9119     header_size = 0;
9120     uint64_t type_signature = 0;
9121     Dwarf_Off type_offset;
9122     for (Dwarf_Off offset = 0, next_offset = 0;
9123          (dwarf_next_unit(dwarf(), offset, &next_offset, &header_size,
9124                           NULL, NULL, &address_size, NULL,
9125                           &type_signature, &type_offset) == 0);
9126          offset = next_offset)
9127       {
9128         Dwarf_Off die_offset = offset + header_size;
9129         Dwarf_Die cu;
9130
9131         if (!dwarf_offdie_types(dwarf(), die_offset, &cu))
9132           continue;
9133         cur_tu_die(&cu);
9134         imported_unit_points_type& imported_units =
9135           tu_die_imported_unit_points_map(source)[die_offset] =
9136           imported_unit_points_type();
9137         build_die_parent_relations_under(&cu, source, imported_units);
9138       }
9139   }
9140 };// end class read_context.
9141
9142 static type_or_decl_base_sptr
9143 build_ir_node_from_die(read_context&    ctxt,
9144                        Dwarf_Die*       die,
9145                        scope_decl*      scope,
9146                        bool             called_from_public_decl,
9147                        size_t           where_offset,
9148                        bool             is_required_decl_spec = false);
9149
9150 static type_or_decl_base_sptr
9151 build_ir_node_from_die(read_context&    ctxt,
9152                        Dwarf_Die*       die,
9153                        bool             called_from_public_decl,
9154                        size_t           where_offset);
9155
9156 static class_decl_sptr
9157 add_or_update_class_type(read_context&  ctxt,
9158                          Dwarf_Die*     die,
9159                          scope_decl*    scope,
9160                          bool           is_struct,
9161                          class_decl_sptr  klass,
9162                          bool           called_from_public_decl,
9163                          size_t         where_offset);
9164
9165 static union_decl_sptr
9166 add_or_update_union_type(read_context&  ctxt,
9167                          Dwarf_Die*     die,
9168                          scope_decl*    scope,
9169                          union_decl_sptr  union_type,
9170                          bool           called_from_public_decl,
9171                          size_t         where_offset);
9172
9173 static decl_base_sptr
9174 build_ir_node_for_void_type(read_context& ctxt);
9175
9176 static function_decl_sptr
9177 build_function_decl(read_context&       ctxt,
9178                     Dwarf_Die*          die,
9179                     size_t              where_offset,
9180                     function_decl_sptr  fn);
9181
9182 static bool
9183 function_is_suppressed(const read_context& ctxt,
9184                        const scope_decl* scope,
9185                        Dwarf_Die *function_die);
9186
9187 static function_decl_sptr
9188 build_or_get_fn_decl_if_not_suppressed(read_context&    ctxt,
9189                                        scope_decl       *scope,
9190                                        Dwarf_Die        *die,
9191                                        size_t   where_offset,
9192                                        function_decl_sptr f = function_decl_sptr());
9193
9194 static var_decl_sptr
9195 build_var_decl(read_context&    ctxt,
9196                Dwarf_Die        *die,
9197                size_t           where_offset,
9198                var_decl_sptr    result = var_decl_sptr());
9199
9200 static var_decl_sptr
9201 build_or_get_var_decl_if_not_suppressed(read_context&   ctxt,
9202                                         scope_decl      *scope,
9203                                         Dwarf_Die       *die,
9204                                         size_t  where_offset,
9205                                         var_decl_sptr   res = var_decl_sptr(),
9206                                         bool is_required_decl_spec = false);
9207 static bool
9208 variable_is_suppressed(const read_context& ctxt,
9209                        const scope_decl* scope,
9210                        Dwarf_Die *variable_die,
9211                        bool is_required_decl_spec = false);
9212
9213 static void
9214 finish_member_function_reading(Dwarf_Die*                die,
9215                                const function_decl_sptr& f,
9216                                const class_or_union_sptr& klass,
9217                                read_context&             ctxt);
9218
9219 /// Setter of the debug info root path for a dwarf reader context.
9220 ///
9221 /// @param ctxt the dwarf reader context to consider.
9222 ///
9223 /// @param path the new debug info root path.  This must be a pointer to a
9224 /// character string which life time should be greater than the life
9225 /// time of the read context.
9226 void
9227 set_debug_info_root_path(read_context& ctxt, char** path)
9228 {ctxt.offline_callbacks()->debuginfo_path = path;}
9229
9230 /// Setter of the debug info root path for a dwarf reader context.
9231 ///
9232 /// @param ctxt the dwarf reader context to consider.
9233 ///
9234 /// @return a pointer to the debug info root path.
9235 ///
9236 /// time of the read context.
9237 char**
9238 get_debug_info_root_path(read_context& ctxt)
9239 {return ctxt.offline_callbacks()->debuginfo_path;}
9240
9241 /// Getter of the "show_stats" flag.
9242 ///
9243 /// This flag tells if we should emit statistics about various
9244 /// internal stuff.
9245 ///
9246 /// @param ctx the read context to consider for this flag.
9247 ///
9248 /// @return the value of the flag.
9249 bool
9250 get_show_stats(read_context& ctxt)
9251 {return ctxt.show_stats();}
9252
9253 /// Setter of the "show_stats" flag.
9254 ///
9255 /// This flag tells if we should emit statistics about various
9256 /// internal stuff.
9257 ///
9258 /// @param ctxt the read context to consider for this flag.
9259 ///
9260 /// @param f the value of the flag.
9261 void
9262 set_show_stats(read_context& ctxt, bool f)
9263 {ctxt.show_stats(f);}
9264
9265 /// Setter of the "do_log" flag.
9266 ///
9267 /// This flag tells if we should emit verbose logs for various
9268 /// internal things related to DWARF reading.
9269 ///
9270 /// @param ctxt the DWARF reading context to consider.
9271 ///
9272 /// @param f the new value of the flag.
9273 void
9274 set_do_log(read_context& ctxt, bool f)
9275 {ctxt.do_log(f);}
9276
9277 /// Setter of the "set_ignore_symbol_table" flag.
9278 ///
9279 /// This flag tells if we should load information about ELF symbol
9280 /// tables.  Not loading the symbol tables is a speed optimization
9281 /// that is done when the set of symbols we care about is provided
9282 /// off-hand.  This is the case when we are supposed to analyze a
9283 /// Linux kernel binary.  In that case, because we have the white list
9284 /// of functions/variable symbols we care about, we don't need to
9285 /// analyze the symbol table; things are thus faster in that case.
9286 ///
9287 /// By default, the symbol table is analyzed so this boolean is set to
9288 /// false.
9289 ///
9290 /// @param ctxt the read context to consider.
9291 ///
9292 /// @param f the new value of the flag.
9293 void
9294 set_ignore_symbol_table(read_context &ctxt, bool f)
9295 {ctxt.options_.ignore_symbol_table = f;}
9296
9297 /// Getter of the "set_ignore_symbol_table" flag.
9298 ///
9299 /// This flag tells if we should load information about ELF symbol
9300 /// tables.  Not loading the symbol tables is a speed optimization
9301 /// that is done when the set of symbols we care about is provided
9302 /// off-hand.  This is the case when we are supposed to analyze a
9303 /// Linux kernel binary.  In that case, because we have the white list
9304 /// of functions/variable symbols we care about, we don't need to
9305 /// analyze the symbol table; things are thus faster in that case.
9306 ///
9307 /// By default, the symbol table is analyzed so this boolean is set to
9308 /// false.
9309 ///
9310 /// @param ctxt the read context to consider.
9311 ///
9312 /// @return the value of the flag.
9313 bool
9314 get_ignore_symbol_table(const read_context& ctxt)
9315 {return ctxt.options_.ignore_symbol_table;}
9316
9317 /// Test if a given DIE is anonymous
9318 ///
9319 /// @param die the DIE to consider.
9320 ///
9321 /// @return true iff @p die is anonymous.
9322 static bool
9323 die_is_anonymous(const Dwarf_Die* die)
9324 {
9325   Dwarf_Attribute attr;
9326   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
9327     return true;
9328   return false;
9329 }
9330
9331 /// Get the value of an attribute that is supposed to be a string, or
9332 /// an empty string if the attribute could not be found.
9333 ///
9334 /// @param die the DIE to get the attribute value from.
9335 ///
9336 /// @param attr_name the attribute name.  Must come from dwarf.h and
9337 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
9338 ///
9339 /// @return the string representing the value of the attribute, or an
9340 /// empty string if no string attribute could be found.
9341 static string
9342 die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
9343 {
9344   if (!die)
9345     return "";
9346
9347   Dwarf_Attribute attr;
9348   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
9349     return "";
9350
9351   const char* str = dwarf_formstring(&attr);
9352   return str ? str : "";
9353 }
9354
9355 /// Get the value of an attribute that is supposed to be an unsigned
9356 /// constant.
9357 ///
9358 /// @param die the DIE to read the information from.
9359 ///
9360 /// @param attr_name the DW_AT_* name of the attribute.  Must come
9361 /// from dwarf.h and be an enumerator representing an attribute like,
9362 /// e.g, DW_AT_decl_line.
9363 ///
9364 ///@param cst the output parameter that is set to the value of the
9365 /// attribute @p attr_name.  This parameter is set iff the function
9366 /// return true.
9367 ///
9368 /// @return true if there was an attribute of the name @p attr_name
9369 /// and with a value that is a constant, false otherwise.
9370 static bool
9371 die_unsigned_constant_attribute(const Dwarf_Die*        die,
9372                                 unsigned        attr_name,
9373                                 uint64_t&       cst)
9374 {
9375   if (!die)
9376     return false;
9377
9378   Dwarf_Attribute attr;
9379   Dwarf_Word result = 0;
9380   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
9381       || dwarf_formudata(&attr, &result))
9382     return false;
9383
9384   cst = result;
9385   return true;
9386 }
9387
9388 /// Read a signed constant value from a given attribute.
9389 ///
9390 /// The signed constant expected must be of form DW_FORM_sdata.
9391 ///
9392 /// @param die the DIE to get the attribute from.
9393 ///
9394 /// @param attr_name the attribute name.
9395 ///
9396 /// @param cst the resulting signed constant read.
9397 ///
9398 /// @return true iff a signed constant attribute of the name @p
9399 /// attr_name was found on the DIE @p die.
9400 static bool
9401 die_signed_constant_attribute(const Dwarf_Die *die,
9402                               unsigned  attr_name,
9403                               int64_t&  cst)
9404 {
9405   if (!die)
9406     return false;
9407
9408   Dwarf_Attribute attr;
9409   Dwarf_Sword result = 0;
9410   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
9411       || dwarf_formsdata(&attr, &result))
9412     return false;
9413
9414   cst = result;
9415   return true;
9416 }
9417
9418 /// Read the value of a constant attribute that is either signed or
9419 /// unsigned into a array_type_def::subrange_type::bound_value value.
9420 ///
9421 /// The bound_value instance will capture the actual signedness of the
9422 /// read attribute.
9423 ///
9424 /// @param die the DIE from which to read the value of the attribute.
9425 ///
9426 /// @param attr_name the attribute name to consider.
9427 ///
9428 /// @param value the resulting value read from attribute @p attr_name
9429 /// on DIE @p die.
9430 ///
9431 /// @return true iff DIE @p die has an attribute named @p attr_name
9432 /// with a constant value.
9433 static bool
9434 die_constant_attribute(const Dwarf_Die *die,
9435                        unsigned attr_name,
9436                        array_type_def::subrange_type::bound_value &value)
9437 {
9438   if (die_attribute_is_unsigned(die, attr_name)
9439       || die_attribute_has_no_signedness(die, attr_name))
9440     {
9441       uint64_t l = 0;
9442       if (!die_unsigned_constant_attribute(die, attr_name, l))
9443         return false;
9444       value.set_unsigned(l);
9445     }
9446   else
9447     {
9448       int64_t l = 0;
9449       if (!die_signed_constant_attribute(die, attr_name, l))
9450         return false;
9451       value.set_signed(l);
9452     }
9453   return true;
9454 }
9455
9456 /// Test if a given attribute on a DIE has a particular form.
9457 ///
9458 /// @param die the DIE to consider.
9459 ///
9460 /// @param attr_name the attribute name to consider on DIE @p die.
9461 ///
9462 /// @param attr_form the attribute form that we expect attribute @p
9463 /// attr_name has on DIE @p die.
9464 ///
9465 /// @return true iff the attribute named @p attr_name on DIE @p die
9466 /// has the form @p attr_form.
9467 static bool
9468 die_attribute_has_form(const Dwarf_Die  *die,
9469                        unsigned attr_name,
9470                        unsigned int     attr_form)
9471 {
9472   Dwarf_Attribute attr;
9473   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
9474     return false;
9475
9476   return dwarf_hasform(&attr, attr_form);
9477 }
9478
9479 /// Test if a given DWARF form is DW_FORM_strx{1,4}.
9480 ///
9481 /// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
9482 /// enum in dwarf.h so we have to use an unsigned int for the form,
9483 /// grrr.
9484 ///
9485 /// @param form the form to consider.
9486 ///
9487 /// @return true iff @p form is DW_FORM_strx{1,4}.
9488 static bool
9489 form_is_DW_FORM_strx(unsigned form)
9490 {
9491   if (form)
9492     {
9493 #if defined HAVE_DW_FORM_strx1          \
9494   && defined HAVE_DW_FORM_strx2 \
9495   && defined HAVE_DW_FORM_strx3 \
9496   && defined HAVE_DW_FORM_strx4
9497       if (form == DW_FORM_strx1
9498           || form == DW_FORM_strx2
9499           || form == DW_FORM_strx3
9500           ||form == DW_FORM_strx4)
9501         return true;
9502 #endif
9503     }
9504   return false;
9505 }
9506
9507 /// Test if a given DIE attribute is signed.
9508 ///
9509 /// @param die the DIE to consider.
9510 ///
9511 /// @param attr_name the attribute name to consider.
9512 ///
9513 /// @return true iff the attribute named @p attr_name on DIE @p die is
9514 /// signed.
9515 static bool
9516 die_attribute_is_signed(const Dwarf_Die* die, unsigned attr_name)
9517 {
9518   if (die_attribute_has_form(die, attr_name, DW_FORM_sdata))
9519     return true;
9520   return false;
9521 }
9522
9523 /// Test if a given DIE attribute is unsigned.
9524 ///
9525 /// @param die the DIE to consider.
9526 ///
9527 /// @param attr_name the attribute name to consider.
9528 ///
9529 /// @return true iff the attribute named @p attr_name on DIE @p die is
9530 /// unsigned.
9531 static bool
9532 die_attribute_is_unsigned(const Dwarf_Die* die, unsigned attr_name)
9533 {
9534   if (die_attribute_has_form(die, attr_name, DW_FORM_udata))
9535     return true;
9536   return false;
9537 }
9538
9539 /// Test if a given DIE attribute is neither explicitely signed nor
9540 /// unsigned.  Usually this is the case for attribute of the form
9541 /// DW_FORM_data*.
9542 ///
9543 /// @param die the DIE to consider.
9544 ///
9545 /// @param attr_name the name of the attribute to consider.
9546 ///
9547 /// @return true iff the attribute named @p attr_name of DIE @p die is
9548 /// neither specifically signed nor unsigned.
9549 static bool
9550 die_attribute_has_no_signedness(const Dwarf_Die *die, unsigned attr_name)
9551 {
9552   return (!die_attribute_is_unsigned(die, attr_name)
9553           && !die_attribute_is_signed(die, attr_name));
9554 }
9555
9556 /// Get the value of a DIE attribute; that value is meant to be a
9557 /// flag.
9558 ///
9559 /// @param die the DIE to get the attribute from.
9560 ///
9561 /// @param attr_name the DW_AT_* name of the attribute.  Must come
9562 /// from dwarf.h and be an enumerator representing an attribute like,
9563 /// e.g, DW_AT_external.
9564 ///
9565 /// @param flag the output parameter to store the flag value into.
9566 /// This is set iff the function returns true.
9567 ///
9568 /// @return true if the DIE has a flag attribute named @p attr_name,
9569 /// false otherwise.
9570 static bool
9571 die_flag_attribute(Dwarf_Die* die, unsigned attr_name, bool& flag)
9572 {
9573   Dwarf_Attribute attr;
9574   bool f = false;
9575   if (!dwarf_attr_integrate(die, attr_name, &attr)
9576       || dwarf_formflag(&attr, &f))
9577     return false;
9578
9579   flag = f;
9580   return true;
9581 }
9582
9583 /// Get the mangled name from a given DIE.
9584 ///
9585 /// @param die the DIE to read the mangled name from.
9586 ///
9587 /// @return the mangled name if it's present in the DIE, or just an
9588 /// empty string if it's not.
9589 static string
9590 die_linkage_name(const Dwarf_Die* die)
9591 {
9592   if (!die)
9593     return "";
9594
9595   string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
9596   if (linkage_name.empty())
9597     linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
9598   return linkage_name;
9599 }
9600
9601 /// Get the file path that is the value of the DW_AT_decl_file
9602 /// attribute on a given DIE, if the DIE is a decl DIE having that
9603 /// attribute.
9604 ///
9605 /// @param die the DIE to consider.
9606 ///
9607 /// @return a string containing the file path that is the logical
9608 /// value of the DW_AT_decl_file attribute.  If the DIE @p die
9609 /// doesn't have a DW_AT_decl_file attribute, then the return value is
9610 /// just an empty string.
9611 static string
9612 die_decl_file_attribute(const Dwarf_Die* die)
9613 {
9614   if (!die)
9615     return "";
9616
9617   const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
9618
9619   return str ? str : "";
9620 }
9621
9622 /// Get the value of an attribute which value is supposed to be a
9623 /// reference to a DIE.
9624 ///
9625 /// @param die the DIE to read the value from.
9626 ///
9627 /// @param die_is_in_alt_di true if @p die comes from alternate debug
9628 /// info sections.
9629 ///
9630 /// @param attr_name the DW_AT_* attribute name to read.
9631 ///
9632 /// @param result the DIE resulting from reading the attribute value.
9633 /// This is set iff the function returns true.
9634 ///
9635 /// @param look_thru_abstract_origin if yes, the function looks
9636 /// through the possible DW_AT_abstract_origin attribute all the way
9637 /// down to the initial DIE that is cloned and look on that DIE to see
9638 /// if it has the @p attr_name attribute.
9639 ///
9640 /// @return true if the DIE @p die contains an attribute named @p
9641 /// attr_name that is a DIE reference, false otherwise.
9642 static bool
9643 die_die_attribute(const Dwarf_Die* die,
9644                   unsigned attr_name,
9645                   Dwarf_Die& result,
9646                   bool look_thru_abstract_origin)
9647 {
9648   Dwarf_Attribute attr;
9649   if (look_thru_abstract_origin)
9650     {
9651       if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
9652         return false;
9653     }
9654   else
9655     {
9656       if (!dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
9657         return false;
9658     }
9659   bool r = dwarf_formref_die(&attr, &result);
9660   return r;
9661 }
9662
9663 /// Read and return a DW_FORM_addr attribute from a given DIE.
9664 ///
9665 /// @param die the DIE to consider.
9666 ///
9667 /// @param attr_name the name of the DW_FORM_addr attribute to read
9668 /// the value from.
9669 ///
9670 /// @param the resulting address.
9671 ///
9672 /// @return true iff the attribute could be read, was of the expected
9673 /// DW_FORM_addr and could thus be translated into the @p result.
9674 static bool
9675 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
9676 {
9677   Dwarf_Attribute attr;
9678   if (!dwarf_attr_integrate(die, attr_name, &attr))
9679     return false;
9680   return dwarf_formaddr(&attr, &result) == 0;
9681 }
9682
9683 /// Returns the source location associated with a decl DIE.
9684 ///
9685 /// @param ctxt the @ref read_context to use.
9686 ///
9687 /// @param die the DIE the read the source location from.
9688 ///
9689 /// @return the location associated with @p die.
9690 static location
9691 die_location(const read_context& ctxt, const Dwarf_Die* die)
9692 {
9693   if (!die)
9694     return location();
9695
9696   string file = die_decl_file_attribute(die);
9697   uint64_t line = 0;
9698   die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
9699
9700   if (!file.empty() && line != 0)
9701     {
9702       translation_unit_sptr tu = ctxt.cur_transl_unit();
9703       location l = tu->get_loc_mgr().create_new_location(file, line, 1);
9704       return l;
9705     }
9706   return location();
9707 }
9708
9709 /// Return a copy of the name of a DIE.
9710 ///
9711 /// @param die the DIE to consider.
9712 ///
9713 /// @return a copy of the name of the DIE.
9714 static string
9715 die_name(const Dwarf_Die* die)
9716 {
9717   string name = die_string_attribute(die, DW_AT_name);
9718   return name;
9719 }
9720
9721 /// Return the location, the name and the mangled name of a given DIE.
9722 ///
9723 /// @param ctxt the read context to use.
9724 ///
9725 /// @param die the DIE to read location and names from.
9726 ///
9727 /// @param loc the location output parameter to set.
9728 ///
9729 /// @param name the name output parameter to set.
9730 ///
9731 /// @param linkage_name the linkage_name output parameter to set.
9732 static void
9733 die_loc_and_name(const read_context&    ctxt,
9734                  Dwarf_Die*             die,
9735                  location&              loc,
9736                  string&                name,
9737                  string&                linkage_name)
9738 {
9739   loc = die_location(ctxt, die);
9740   name = die_name(die);
9741   linkage_name = die_linkage_name(die);
9742 }
9743
9744 /// Get the size of a (type) DIE as the value for the parameter
9745 /// DW_AT_byte_size or DW_AT_bit_size.
9746 ///
9747 /// @param die the DIE to read the information from.
9748 ///
9749 /// @param size the resulting size in bits.  This is set iff the
9750 /// function return true.
9751 ///
9752 /// @return true if the size attribute was found.
9753 static bool
9754 die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
9755 {
9756   if (!die)
9757     return false;
9758
9759   uint64_t byte_size = 0, bit_size = 0;
9760
9761   if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
9762     {
9763       if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
9764         return false;
9765     }
9766   else
9767     bit_size = byte_size * 8;
9768
9769   size = bit_size;
9770
9771   return true;
9772 }
9773
9774 /// Get the access specifier (from the DW_AT_accessibility attribute
9775 /// value) of a given DIE.
9776 ///
9777 /// @param die the DIE to consider.
9778 ///
9779 /// @param access the resulting access.  This is set iff the function
9780 /// returns true.
9781 ///
9782 /// @return bool if the DIE contains the DW_AT_accessibility die.
9783 static bool
9784 die_access_specifier(Dwarf_Die * die, access_specifier& access)
9785 {
9786   if (!die)
9787     return false;
9788
9789   uint64_t a = 0;
9790   if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
9791     return false;
9792
9793   access_specifier result = private_access;
9794
9795   switch (a)
9796     {
9797     case private_access:
9798       result = private_access;
9799       break;
9800
9801     case protected_access:
9802       result = protected_access;
9803       break;
9804
9805     case public_access:
9806       result = public_access;
9807       break;
9808
9809     default:
9810       break;
9811     }
9812
9813   access = result;
9814   return true;
9815 }
9816
9817 /// Test whether a given DIE represents a decl that is public.  That
9818 /// is, one with the DW_AT_external attribute set.
9819 ///
9820 /// @param die the DIE to consider for testing.
9821 ///
9822 /// @return true if a DW_AT_external attribute is present and its
9823 /// value is set to the true; return false otherwise.
9824 static bool
9825 die_is_public_decl(Dwarf_Die* die)
9826 {
9827   bool is_public = false;
9828   die_flag_attribute(die, DW_AT_external, is_public);
9829   return is_public;
9830 }
9831
9832 /// Test whether a given DIE represents a declaration-only DIE.
9833 ///
9834 /// That is, if the DIE has the DW_AT_declaration flag set.
9835 ///
9836 /// @param die the DIE to consider.
9837 //
9838 /// @return true if a DW_AT_declaration is present, false otherwise.
9839 static bool
9840 die_is_declaration_only(Dwarf_Die* die)
9841 {
9842   bool is_declaration_only = false;
9843   die_flag_attribute(die, DW_AT_declaration, is_declaration_only);
9844   return is_declaration_only;
9845 }
9846
9847 /// Tests whether a given DIE is artificial.
9848 ///
9849 /// @param die the test to test for.
9850 ///
9851 /// @return true if the DIE is artificial, false otherwise.
9852 static bool
9853 die_is_artificial(Dwarf_Die* die)
9854 {
9855   bool is_artificial;
9856   return die_flag_attribute(die, DW_AT_artificial, is_artificial);
9857 }
9858
9859 ///@return true if a tag represents a type, false otherwise.
9860 ///
9861 ///@param tag the tag to consider.
9862 static bool
9863 is_type_tag(unsigned tag)
9864 {
9865   bool result = false;
9866
9867   switch (tag)
9868     {
9869     case DW_TAG_array_type:
9870     case DW_TAG_class_type:
9871     case DW_TAG_enumeration_type:
9872     case DW_TAG_pointer_type:
9873     case DW_TAG_reference_type:
9874     case DW_TAG_string_type:
9875     case DW_TAG_structure_type:
9876     case DW_TAG_subroutine_type:
9877     case DW_TAG_typedef:
9878     case DW_TAG_union_type:
9879     case DW_TAG_ptr_to_member_type:
9880     case DW_TAG_set_type:
9881     case DW_TAG_subrange_type:
9882     case DW_TAG_base_type:
9883     case DW_TAG_const_type:
9884     case DW_TAG_file_type:
9885     case DW_TAG_packed_type:
9886     case DW_TAG_thrown_type:
9887     case DW_TAG_volatile_type:
9888     case DW_TAG_restrict_type:
9889     case DW_TAG_interface_type:
9890     case DW_TAG_unspecified_type:
9891     case DW_TAG_shared_type:
9892     case DW_TAG_rvalue_reference_type:
9893       result = true;
9894       break;
9895
9896     default:
9897       result = false;
9898       break;
9899     }
9900
9901   return result;
9902 }
9903
9904 /// Test if a given DIE is a type to be canonicalized.  note that a
9905 /// function DIE (DW_TAG_subprogram) is considered to be a
9906 /// canonicalize-able type too because we can consider that DIE as
9907 /// being the type of the function, as well as the function decl
9908 /// itself.
9909 ///
9910 /// @param tag the tag of the DIE to consider.
9911 ///
9912 /// @return true iff the DIE of tag @p tag is a canonicalize-able DIE.
9913 static bool
9914 is_canonicalizeable_type_tag(unsigned tag)
9915 {
9916   bool result = false;
9917
9918   switch (tag)
9919     {
9920     case DW_TAG_array_type:
9921     case DW_TAG_class_type:
9922     case DW_TAG_enumeration_type:
9923     case DW_TAG_pointer_type:
9924     case DW_TAG_reference_type:
9925     case DW_TAG_structure_type:
9926     case DW_TAG_subroutine_type:
9927     case DW_TAG_subprogram:
9928     case DW_TAG_typedef:
9929     case DW_TAG_union_type:
9930     case DW_TAG_base_type:
9931     case DW_TAG_const_type:
9932     case DW_TAG_volatile_type:
9933     case DW_TAG_restrict_type:
9934     case DW_TAG_rvalue_reference_type:
9935       result = true;
9936       break;
9937
9938     default:
9939       result = false;
9940       break;
9941     }
9942
9943   return result;
9944 }
9945
9946 /// Test if a DIE tag represents a declaration.
9947 ///
9948 /// @param tag the DWARF tag to consider.
9949 ///
9950 /// @return true iff @p tag is for a declaration.
9951 static bool
9952 is_decl_tag(unsigned tag)
9953 {
9954   switch (tag)
9955     {
9956     case DW_TAG_formal_parameter:
9957     case DW_TAG_imported_declaration:
9958     case DW_TAG_member:
9959     case DW_TAG_unspecified_parameters:
9960     case DW_TAG_subprogram:
9961     case DW_TAG_variable:
9962     case DW_TAG_namespace:
9963     case DW_TAG_GNU_template_template_param:
9964     case DW_TAG_GNU_template_parameter_pack:
9965     case DW_TAG_GNU_formal_parameter_pack:
9966       return true;
9967     }
9968   return false;
9969 }
9970
9971 /// Test if a DIE represents a type DIE.
9972 ///
9973 /// @param die the DIE to consider.
9974 ///
9975 /// @return true if @p die represents a type, false otherwise.
9976 static bool
9977 die_is_type(const Dwarf_Die* die)
9978 {
9979   if (!die)
9980     return false;
9981   return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
9982 }
9983
9984 /// Test if a DIE represents a declaration.
9985 ///
9986 /// @param die the DIE to consider.
9987 ///
9988 /// @return true if @p die represents a decl, false otherwise.
9989 static bool
9990 die_is_decl(const Dwarf_Die* die)
9991 {
9992   if (!die)
9993     return false;
9994   return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
9995 }
9996
9997 /// Test if a DIE represents a namespace.
9998 ///
9999 /// @param die the DIE to consider.
10000 ///
10001 /// @return true if @p die represents a namespace, false otherwise.
10002 static bool
10003 die_is_namespace(const Dwarf_Die* die)
10004 {
10005   if (!die)
10006     return false;
10007   return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
10008 }
10009
10010 /// Test if a DIE has tag DW_TAG_unspecified_type.
10011 ///
10012 /// @param die the DIE to consider.
10013 ///
10014 /// @return true if @p die has tag DW_TAG_unspecified_type.
10015 static bool
10016 die_is_unspecified(Dwarf_Die* die)
10017 {
10018   if (!die)
10019     return false;
10020   return (dwarf_tag(die) == DW_TAG_unspecified_type);
10021 }
10022
10023 /// Test if a DIE represents a void type.
10024 ///
10025 /// @param die the DIE to consider.
10026 ///
10027 /// @return true if @p die represents a void type, false otherwise.
10028 static bool
10029 die_is_void_type(Dwarf_Die* die)
10030 {
10031   if (!die || dwarf_tag(die) != DW_TAG_base_type)
10032     return false;
10033
10034   string name = die_name(die);
10035   if (name == "void")
10036     return true;
10037
10038   return false;
10039 }
10040
10041 /// Test if a DIE represents a pointer type.
10042 ///
10043 /// @param die the die to consider.
10044 ///
10045 /// @return true iff @p die represents a pointer type.
10046 static bool
10047 die_is_pointer_type(const Dwarf_Die* die)
10048 {
10049   if (!die)
10050     return false;
10051
10052   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10053   if (tag == DW_TAG_pointer_type)
10054     return true;
10055
10056   return false;
10057 }
10058
10059 /// Test if a DIE is for a pointer, reference or qualified type to
10060 /// anonymous class or struct.
10061 ///
10062 /// @param die the DIE to consider.
10063 ///
10064 /// @return true iff @p is for a pointer, reference or qualified type
10065 /// to anonymous class or struct.
10066 static bool
10067 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
10068 {
10069   if (!die_is_pointer_or_reference_type(die)
10070       && !die_is_qualified_type(die))
10071     return false;
10072
10073   Dwarf_Die underlying_type_die;
10074   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
10075     return false;
10076
10077   if (!die_is_class_type(&underlying_type_die))
10078     return false;
10079
10080   string name = die_name(&underlying_type_die);
10081
10082   return name.empty();
10083 }
10084
10085 /// Test if a DIE represents a reference type.
10086 ///
10087 /// @param die the die to consider.
10088 ///
10089 /// @return true iff @p die represents a reference type.
10090 static bool
10091 die_is_reference_type(const Dwarf_Die* die)
10092 {
10093   if (!die)
10094     return false;
10095
10096   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10097   if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
10098     return true;
10099
10100   return false;
10101 }
10102
10103 /// Test if a DIE represents an array type.
10104 ///
10105 /// @param die the die to consider.
10106 ///
10107 /// @return true iff @p die represents an array type.
10108 static bool
10109 die_is_array_type(const Dwarf_Die* die)
10110 {
10111   if (!die)
10112     return false;
10113
10114   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10115   if (tag == DW_TAG_array_type)
10116     return true;
10117
10118   return false;
10119 }
10120
10121 /// Test if a DIE represents a pointer, reference or array type.
10122 ///
10123 /// @param die the die to consider.
10124 ///
10125 /// @return true iff @p die represents a pointer or reference type.
10126 static bool
10127 die_is_pointer_or_reference_type(const Dwarf_Die* die)
10128 {return (die_is_pointer_type(die)
10129          || die_is_reference_type(die)
10130          || die_is_array_type(die));}
10131
10132 /// Test if a DIE represents a pointer, a reference or a typedef type.
10133 ///
10134 /// @param die the die to consider.
10135 ///
10136 /// @return true iff @p die represents a pointer, a reference or a
10137 /// typedef type.
10138 static bool
10139 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
10140 {return (die_is_pointer_or_reference_type(die)
10141          || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
10142
10143 /// Test if a DIE represents a class type.
10144 ///
10145 /// @param die the die to consider.
10146 ///
10147 /// @return true iff @p die represents a class type.
10148 static bool
10149 die_is_class_type(const Dwarf_Die* die)
10150 {
10151   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10152
10153   if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
10154     return true;
10155
10156   return false;
10157 }
10158
10159 /// Test if a DIE is for a qualified type.
10160 ///
10161 /// @param die the DIE to consider.
10162 ///
10163 /// @return true iff @p die is for a qualified type.
10164 static bool
10165 die_is_qualified_type(const Dwarf_Die* die)
10166 {
10167   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10168     if (tag == DW_TAG_const_type
10169         || tag == DW_TAG_volatile_type
10170         || tag == DW_TAG_restrict_type)
10171       return true;
10172
10173     return false;
10174 }
10175
10176 /// Test if a DIE is for a function type.
10177 ///
10178 /// @param die the DIE to consider.
10179 ///
10180 /// @return true iff @p die is for a function type.
10181 static bool
10182 die_is_function_type(const Dwarf_Die *die)
10183 {
10184   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10185   if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
10186     return true;
10187
10188   return false;
10189 }
10190
10191 /// Test if a DIE for a function pointer or member function has an
10192 /// DW_AT_object_pointer attribute.
10193 ///
10194 /// @param die the DIE to consider.
10195 ///
10196 /// @param object_pointer out parameter.  It's set to the DIE for the
10197 /// object pointer iff the function returns true.
10198 ///
10199 /// @return true iff the DIE @p die has an object pointer.  In that
10200 /// case, the parameter @p object_pointer is set to the DIE of that
10201 /// object pointer.
10202 static bool
10203 die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
10204 {
10205   if (!die)
10206     return false;
10207
10208   if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
10209     return true;
10210
10211   return false;
10212 }
10213
10214 /// When given the object pointer DIE of a function type or member
10215 /// function DIE, this function returns the "this" pointer that points
10216 /// to the associated class.
10217 ///
10218 /// @param die the DIE of the object pointer of the function or member
10219 /// function to consider.
10220 ///
10221 /// @param this_pointer_die out parameter.  This is set to the DIE of
10222 /// the "this" pointer iff the function returns true.
10223 ///
10224 /// @return true iff the function found the "this" pointer from the
10225 /// object pointer DIE @p die.  In that case, the parameter @p
10226 /// this_pointer_die is set to the DIE of that "this" pointer.
10227 static bool
10228 die_this_pointer_from_object_pointer(Dwarf_Die* die,
10229                                      Dwarf_Die& this_pointer_die)
10230 {
10231   ABG_ASSERT(die);
10232   ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
10233
10234   if (die_die_attribute(die, DW_AT_type, this_pointer_die))
10235     return true;
10236
10237   return false;
10238 }
10239
10240 /// Test if a given "this" pointer that points to a particular class
10241 /// type is for a const class or not.  If it's for a const class, then
10242 /// it means the function type or the member function associated to
10243 /// that "this" pointer is const.
10244 ///
10245 /// @param die the DIE of the "this" pointer to consider.
10246 ///
10247 /// @return true iff @p die points to a const class type.
10248 static bool
10249 die_this_pointer_is_const(Dwarf_Die* die)
10250 {
10251   ABG_ASSERT(die);
10252
10253   if (dwarf_tag(die) == DW_TAG_pointer_type)
10254     {
10255       Dwarf_Die pointed_to_type_die;
10256       if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
10257         if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
10258           return true;
10259     }
10260
10261   return false;
10262 }
10263
10264 /// Test if an object pointer (referred-to via a DW_AT_object_pointer
10265 /// attribute) points to a const implicit class and so is for a const
10266 /// method or or a const member function type.
10267 ///
10268 /// @param die the DIE of the object pointer to consider.
10269 ///
10270 /// @return true iff the object pointer represented by @p die is for a
10271 /// a const method or const member function type.
10272 static bool
10273 die_object_pointer_is_for_const_method(Dwarf_Die* die)
10274 {
10275   ABG_ASSERT(die);
10276   ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
10277
10278   Dwarf_Die this_pointer_die;
10279   if (die_this_pointer_from_object_pointer(die, this_pointer_die))
10280     if (die_this_pointer_is_const(&this_pointer_die))
10281       return true;
10282
10283   return false;
10284 }
10285
10286 /// Test if a DIE represents an entity that is at class scope.
10287 ///
10288 /// @param ctxt the read context to use.
10289 ///
10290 /// @param die the DIE to consider.
10291 ///
10292 /// @param where_offset where we are logically at in the DIE stream.
10293 ///
10294 /// @param class_scope_die out parameter.  Set to the DIE of the
10295 /// containing class iff @p die happens to be at class scope; that is,
10296 /// iff the function returns true.
10297 ///
10298 /// @return true iff @p die is at class scope.  In that case, @p
10299 /// class_scope_die is set to the DIE of the class that contains @p
10300 /// die.
10301 static bool
10302 die_is_at_class_scope(const read_context& ctxt,
10303                       const Dwarf_Die* die,
10304                       size_t where_offset,
10305                       Dwarf_Die& class_scope_die)
10306 {
10307   if (!get_scope_die(ctxt, die, where_offset, class_scope_die))
10308     return false;
10309
10310   int tag = dwarf_tag(&class_scope_die);
10311
10312   return (tag == DW_TAG_structure_type
10313           || tag == DW_TAG_class_type
10314           || tag == DW_TAG_union_type);
10315 }
10316
10317 /// Return the leaf object under a pointer, reference or qualified
10318 /// type DIE.
10319 ///
10320 /// @param die the DIE of the type to consider.
10321 ///
10322 /// @param peeled_die out parameter.  Set to the DIE of the leaf
10323 /// object iff the function actually peeled anything.
10324 ///
10325 /// @return true upon successful completion.
10326 static bool
10327 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
10328 {
10329   if (!die)
10330     return false;
10331
10332   int tag = dwarf_tag(die);
10333
10334   if (tag == DW_TAG_const_type
10335       || tag == DW_TAG_volatile_type
10336       || tag == DW_TAG_restrict_type
10337       || tag == DW_TAG_pointer_type
10338       || tag == DW_TAG_reference_type
10339       || tag == DW_TAG_rvalue_reference_type)
10340     {
10341       if (!die_die_attribute(die, DW_AT_type, peeled_die))
10342         return false;
10343     }
10344   else
10345     return false;
10346
10347   while (tag == DW_TAG_const_type
10348          || tag == DW_TAG_volatile_type
10349          || tag == DW_TAG_restrict_type
10350          || tag == DW_TAG_pointer_type
10351          || tag == DW_TAG_reference_type
10352          || tag == DW_TAG_rvalue_reference_type)
10353     {
10354       if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
10355         break;
10356       tag = dwarf_tag(&peeled_die);
10357     }
10358
10359   return true;
10360 }
10361
10362 /// Return the leaf object under a typedef type DIE.
10363 ///
10364 /// @param die the DIE of the type to consider.
10365 ///
10366 /// @param peeled_die out parameter.  Set to the DIE of the leaf
10367 /// object iff the function actually peeled anything.
10368 ///
10369 /// @return true upon successful completion.
10370 static bool
10371 die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
10372 {
10373   if (!die)
10374     return false;
10375
10376   int tag = dwarf_tag(die);
10377
10378   if (tag == DW_TAG_typedef)
10379     {
10380       if (!die_die_attribute(die, DW_AT_type, peeled_die))
10381         return false;
10382     }
10383   else
10384     return false;
10385
10386   while (tag == DW_TAG_typedef)
10387     {
10388       if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
10389         break;
10390       tag = dwarf_tag(&peeled_die);
10391     }
10392
10393   return true;
10394
10395 }
10396
10397 /// Return the leaf DIE under a pointer, a reference or a typedef DIE.
10398 ///
10399 /// @param die the DIE to consider.
10400 ///
10401 /// @param peeled_die the resulting peeled (or leaf) DIE.  This is set
10402 /// iff the function returned true.
10403 ///
10404 /// @return true iff the function could peel @p die.
10405 static bool
10406 die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
10407 {
10408   if (!die)
10409     return false;
10410
10411   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10412
10413   if (tag == DW_TAG_pointer_type
10414       || tag == DW_TAG_reference_type
10415       || tag == DW_TAG_rvalue_reference_type
10416       || tag == DW_TAG_typedef)
10417     {
10418       if (!die_die_attribute(die, DW_AT_type, peeled_die))
10419         return false;
10420     }
10421   else
10422     return false;
10423
10424   while (tag == DW_TAG_pointer_type
10425          || tag == DW_TAG_reference_type
10426          || tag == DW_TAG_rvalue_reference_type
10427          || tag == DW_TAG_typedef)
10428     {
10429       if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
10430         break;
10431       tag = dwarf_tag(&peeled_die);
10432     }
10433   return true;
10434 }
10435
10436 /// Test if a DIE for a function type represents a method type.
10437 ///
10438 /// @param ctxt the read context.
10439 ///
10440 /// @param die the DIE to consider.
10441 ///
10442 /// @param where_offset where we logically are in the stream of DIEs.
10443 ///
10444 /// @param object_pointer_die out parameter.  This is set by the
10445 /// function to the DIE that refers to the formal function parameter
10446 /// which holds the implicit "this" pointer of the method.  That die
10447 /// is called the object pointer DIE. This is set iff the function
10448 ///
10449 /// @param class_die out parameter.  This is set by the function to
10450 /// the DIE that represents the class of the method type.  This is set
10451 /// iff the function returns true.
10452 ///
10453 /// @param is_static out parameter.  This is set to true by the
10454 /// function if @p die is a static method.  This is set iff the
10455 /// function returns true.
10456 ///
10457 /// @return true iff @p die is a DIE for a method type.
10458 static bool
10459 die_function_type_is_method_type(const read_context& ctxt,
10460                                  const Dwarf_Die *die,
10461                                  size_t where_offset,
10462                                  Dwarf_Die& object_pointer_die,
10463                                  Dwarf_Die& class_die,
10464                                  bool& is_static)
10465 {
10466   if (!die)
10467     return false;
10468
10469   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10470   ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
10471
10472   bool has_object_pointer = false;
10473   is_static = false;
10474   if (tag == DW_TAG_subprogram)
10475     {
10476       Dwarf_Die spec_or_origin_die;
10477       if (die_die_attribute(die, DW_AT_specification,
10478                             spec_or_origin_die)
10479           || die_die_attribute(die, DW_AT_abstract_origin,
10480                                spec_or_origin_die))
10481         {
10482           if (die_has_object_pointer(&spec_or_origin_die,
10483                                      object_pointer_die))
10484             has_object_pointer = true;
10485           else
10486             {
10487               if (die_is_at_class_scope(ctxt, &spec_or_origin_die,
10488                                         where_offset, class_die))
10489                 is_static = true;
10490               else
10491                 return false;
10492             }
10493         }
10494       else
10495         {
10496           if (die_has_object_pointer(die, object_pointer_die))
10497             has_object_pointer = true;
10498           else
10499             {
10500               if (die_is_at_class_scope(ctxt, die, where_offset, class_die))
10501                 is_static = true;
10502               else
10503                 return false;
10504             }
10505         }
10506     }
10507   else
10508     {
10509       if (die_has_object_pointer(die, object_pointer_die))
10510         has_object_pointer = true;
10511       else
10512         return false;
10513     }
10514
10515   if (!is_static)
10516     {
10517       ABG_ASSERT(has_object_pointer);
10518       // The object pointer die points to a DW_TAG_formal_parameter which
10519       // is the "this" parameter.  The type of the "this" parameter is a
10520       // pointer.  Let's get that pointer type.
10521       Dwarf_Die this_type_die;
10522       if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
10523         return false;
10524
10525       // So the class type is the type pointed to by the type of the "this"
10526       // parameter.
10527       if (!die_peel_qual_ptr(&this_type_die, class_die))
10528         return false;
10529
10530       // And make we return a class type, rather than a typedef to a
10531       // class.
10532       die_peel_typedef(&class_die, class_die);
10533     }
10534
10535   return true;
10536 }
10537
10538 enum virtuality
10539 {
10540   VIRTUALITY_NOT_VIRTUAL,
10541   VIRTUALITY_VIRTUAL,
10542   VIRTUALITY_PURE_VIRTUAL
10543 };
10544
10545 /// Get the virtual-ness of a given DIE, that is, the value of the
10546 /// DW_AT_virtuality attribute.
10547 ///
10548 /// @param die the DIE to read from.
10549 ///
10550 /// @param virt the resulting virtuality attribute.  This is set iff
10551 /// the function returns true.
10552 ///
10553 /// @return true if the virtual-ness could be determined.
10554 static bool
10555 die_virtuality(const Dwarf_Die* die, virtuality& virt)
10556 {
10557   if (!die)
10558     return false;
10559
10560   uint64_t v = 0;
10561   die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
10562
10563   if (v == DW_VIRTUALITY_virtual)
10564     virt = VIRTUALITY_VIRTUAL;
10565   else if (v == DW_VIRTUALITY_pure_virtual)
10566     virt = VIRTUALITY_PURE_VIRTUAL;
10567   else
10568     virt = VIRTUALITY_NOT_VIRTUAL;
10569
10570   return true;
10571 }
10572
10573 /// Test whether the DIE represent either a virtual base or function.
10574 ///
10575 /// @param die the DIE to consider.
10576 ///
10577 /// @return bool if the DIE represents a virtual base or function,
10578 /// false othersise.
10579 static bool
10580 die_is_virtual(const Dwarf_Die* die)
10581 {
10582   virtuality v;
10583   if (!die_virtuality(die, v))
10584     return false;
10585
10586   return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
10587 }
10588
10589 /// Test if the DIE represents an entity that was declared inlined.
10590 ///
10591 /// @param die the DIE to test for.
10592 ///
10593 /// @return true if the DIE represents an entity that was declared
10594 /// inlined.
10595 static bool
10596 die_is_declared_inline(Dwarf_Die* die)
10597 {
10598   uint64_t inline_value = 0;
10599   if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
10600     return false;
10601   return inline_value == DW_INL_declared_inlined;
10602 }
10603
10604 /// This function is a fast routine (optmization) to compare the values of
10605 /// two string attributes of two DIEs.
10606 ///
10607 /// @param l the first DIE to consider.
10608 ///
10609 /// @param r the second DIE to consider.
10610 ///
10611 /// @param attr_name the name of the attribute to compare, on the two
10612 /// DIEs above.
10613 ///
10614 /// @param result out parameter.  This is set to the result of the
10615 /// comparison.  If the value of attribute @p attr_name on DIE @p l
10616 /// equals the value of attribute @p attr_name on DIE @p r, then the
10617 /// the argument of this parameter is set to true.  Otherwise, it's
10618 /// set to false.  Note that the argument of this parameter is set iff
10619 /// the function returned true.
10620 ///
10621 /// @return true iff the comparison could be performed.  There are
10622 /// cases in which the comparison cannot be performed.  For instance,
10623 /// if one of the DIEs does not have the attribute @p attr_name.  In
10624 /// any case, if this function returns true, then the parameter @p
10625 /// result is set to the result of the comparison.
10626 static bool
10627 compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
10628                                     unsigned attr_name,
10629                                     bool &result)
10630 {
10631   Dwarf_Attribute l_attr, r_attr;
10632   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
10633       || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
10634     return false;
10635
10636   ABG_ASSERT(l_attr.form == DW_FORM_strp
10637              || l_attr.form == DW_FORM_string
10638              || l_attr.form == DW_FORM_GNU_strp_alt
10639              || form_is_DW_FORM_strx(l_attr.form));
10640
10641   ABG_ASSERT(r_attr.form == DW_FORM_strp
10642              || r_attr.form == DW_FORM_string
10643              || r_attr.form == DW_FORM_GNU_strp_alt
10644              || form_is_DW_FORM_strx(r_attr.form));
10645
10646   if ((l_attr.form == DW_FORM_strp
10647        && r_attr.form == DW_FORM_strp)
10648       || (l_attr.form == DW_FORM_GNU_strp_alt
10649           && r_attr.form == DW_FORM_GNU_strp_alt)
10650       || (form_is_DW_FORM_strx(l_attr.form)
10651           && form_is_DW_FORM_strx(r_attr.form)))
10652     {
10653       // So these string attributes are actually pointers into a
10654       // string table.  The string table is most likely de-duplicated
10655       // so comparing the *values* of the pointers should be enough.
10656       //
10657       // This is the fast path.
10658       if (l_attr.valp == r_attr.valp)
10659           result = true;
10660       else if (l_attr.valp && r_attr.valp)
10661         result = *l_attr.valp == *r_attr.valp;
10662       else
10663         result = false;
10664       return true;
10665     }
10666
10667   // If we reached this point it means we couldn't use the fast path
10668   // because the string atttributes are strings that are "inline" in
10669   // the debug info section.  Let's just compare them the slow and
10670   // obvious way.
10671   string l_str = die_string_attribute(l, attr_name),
10672     r_str = die_string_attribute(r, attr_name);
10673   result = l_str == r_str;
10674
10675   return true;
10676 }
10677
10678 /// Compare the file path of the compilation units (aka CUs)
10679 /// associated to two DIEs.
10680 ///
10681 /// If the DIEs are for pointers or typedefs, this function also
10682 /// compares the file paths of the CUs of the leaf DIEs (underlying
10683 /// DIEs of the pointer or the typedef).
10684 ///
10685 /// @param l the first type DIE to consider.
10686 ///
10687 /// @param r the second type DIE to consider.
10688 ///
10689 /// @return true iff the file paths of the DIEs of the two types are
10690 /// equal.
10691 static bool
10692 compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
10693 {
10694   Dwarf_Die l_cu, r_cu;
10695   if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
10696       ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
10697     return false;
10698
10699   bool compared =
10700     compare_dies_string_attribute_value(&l_cu, &r_cu,
10701                                         DW_AT_name,
10702                                         result);
10703   if (compared)
10704     {
10705       Dwarf_Die peeled_l, peeled_r;
10706       if (die_is_pointer_reference_or_typedef_type(l)
10707           && die_is_pointer_reference_or_typedef_type(r)
10708           && die_peel_pointer_and_typedef(l, peeled_l)
10709           && die_peel_pointer_and_typedef(r, peeled_r))
10710         {
10711           if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
10712               ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
10713             return false;
10714           compared =
10715             compare_dies_string_attribute_value(&l_cu, &r_cu,
10716                                                 DW_AT_name,
10717                                                 result);
10718         }
10719     }
10720
10721   return  compared;
10722 }
10723
10724 // -----------------------------------
10725 // <location expression evaluation>
10726 // -----------------------------------
10727
10728 /// Get the value of a given DIE attribute, knowing that it must be a
10729 /// location expression.
10730 ///
10731 /// @param die the DIE to read the attribute from.
10732 ///
10733 /// @param attr_name the name of the attribute to read the value for.
10734 ///
10735 /// @param expr the pointer to allocate and fill with the resulting
10736 /// array of operators + operands forming a dwarf expression.  This is
10737 /// set iff the function returns true.
10738 ///
10739 /// @param expr_len the length of the resulting dwarf expression.
10740 /// This is set iff the function returns true.
10741 ///
10742 /// @return true if the attribute exists and has a dwarf expression as
10743 /// value.  In that case the expr and expr_len arguments are set to
10744 /// the resulting dwarf exprssion.
10745 static bool
10746 die_location_expr(const Dwarf_Die* die,
10747                   unsigned attr_name,
10748                   Dwarf_Op** expr,
10749                   uint64_t* expr_len)
10750 {
10751   if (!die)
10752     return false;
10753
10754   Dwarf_Attribute attr;
10755   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
10756     return false;
10757
10758   size_t len = 0;
10759   bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
10760
10761   if (result)
10762     *expr_len = len;
10763
10764   return result;
10765 }
10766
10767 /// If the current operation in the dwarf expression represents a push
10768 /// of a constant value onto the dwarf expr virtual machine (aka
10769 /// DEVM), perform the operation and update the DEVM.
10770 ///
10771 /// If the result of the operation is a constant, update the DEVM
10772 /// accumulator with its value.  Otherwise, the DEVM accumulator is
10773 /// left with its previous value.
10774 ///
10775 /// @param ops the array of the dwarf expression operations to consider.
10776 ///
10777 /// @param ops_len the lengths of @p ops array above.
10778 ///
10779 /// @param index the index of the operation to interpret, in @p ops.
10780 ///
10781 /// @param next_index the index of the operation to interpret at the
10782 /// next step, after this function completed and returned.  This is
10783 /// set an output parameter that is set iff the function returns true.
10784 ///
10785 /// @param ctxt the DEVM evaluation context.
10786 ///
10787 /// @return true if the current operation actually pushes a constant
10788 /// value onto the DEVM stack, false otherwise.
10789 static bool
10790 op_pushes_constant_value(Dwarf_Op*                      ops,
10791                          uint64_t                       ops_len,
10792                          uint64_t                       index,
10793                          uint64_t&                      next_index,
10794                          dwarf_expr_eval_context&       ctxt)
10795 {
10796   ABG_ASSERT(index < ops_len);
10797
10798   Dwarf_Op& op = ops[index];
10799   int64_t value = 0;
10800
10801   switch (op.atom)
10802     {
10803     case DW_OP_addr:
10804       value = ops[index].number;
10805       break;
10806
10807     case DW_OP_const1u:
10808     case DW_OP_const1s:
10809     case DW_OP_const2u:
10810     case DW_OP_const2s:
10811     case DW_OP_const4u:
10812     case DW_OP_const4s:
10813     case DW_OP_const8u:
10814     case DW_OP_const8s:
10815     case DW_OP_constu:
10816     case DW_OP_consts:
10817       value = ops[index].number;
10818       break;
10819
10820     case DW_OP_lit0:
10821       value = 0;
10822       break;
10823     case DW_OP_lit1:
10824       value = 1;
10825       break;
10826     case DW_OP_lit2:
10827       value = 2;
10828       break;
10829     case DW_OP_lit3:
10830       value = 3;
10831       break;
10832     case DW_OP_lit4:
10833       value = 4;
10834       break;
10835     case DW_OP_lit5:
10836       value = 5;
10837       break;
10838     case DW_OP_lit6:
10839       value = 6;
10840       break;
10841     case DW_OP_lit7:
10842       value = 7;
10843       break;
10844     case DW_OP_lit8:
10845       value = 8;
10846       break;
10847     case DW_OP_lit9:
10848       value = 9;
10849       break;
10850     case DW_OP_lit10:
10851       value = 10;
10852       break;
10853     case DW_OP_lit11:
10854       value = 11;
10855       break;
10856     case DW_OP_lit12:
10857       value = 12;
10858       break;
10859     case DW_OP_lit13:
10860       value = 13;
10861       break;
10862     case DW_OP_lit14:
10863       value = 14;
10864       break;
10865     case DW_OP_lit15:
10866       value = 15;
10867       break;
10868     case DW_OP_lit16:
10869       value = 16;
10870       break;
10871     case DW_OP_lit17:
10872       value = 17;
10873       break;
10874     case DW_OP_lit18:
10875       value = 18;
10876       break;
10877     case DW_OP_lit19:
10878       value = 19;
10879       break;
10880     case DW_OP_lit20:
10881       value = 20;
10882       break;
10883     case DW_OP_lit21:
10884       value = 21;
10885       break;
10886     case DW_OP_lit22:
10887       value = 22;
10888       break;
10889     case DW_OP_lit23:
10890       value = 23;
10891       break;
10892     case DW_OP_lit24:
10893       value = 24;
10894       break;
10895     case DW_OP_lit25:
10896       value = 25;
10897       break;
10898     case DW_OP_lit26:
10899       value = 26;
10900       break;
10901     case DW_OP_lit27:
10902       value = 27;
10903       break;
10904     case DW_OP_lit28:
10905       value = 28;
10906       break;
10907     case DW_OP_lit29:
10908       value = 29;
10909       break;
10910     case DW_OP_lit30:
10911       value = 30;
10912       break;
10913     case DW_OP_lit31:
10914       value = 31;
10915       break;
10916
10917     default:
10918       return false;
10919     }
10920
10921   expr_result r(value);
10922   ctxt.push(r);
10923   ctxt.accum = r;
10924   next_index = index + 1;
10925
10926   return true;
10927 }
10928
10929 /// If the current operation in the dwarf expression represents a push
10930 /// of a non-constant value onto the dwarf expr virtual machine (aka
10931 /// DEVM), perform the operation and update the DEVM.  A non-constant
10932 /// is namely a quantity for which we need inferior (a running program
10933 /// image) state to know the exact value.
10934 ///
10935 /// Upon successful completion, as the result of the operation is a
10936 /// non-constant the DEVM accumulator value is left to its state as of
10937 /// before the invocation of this function.
10938 ///
10939 /// @param ops the array of the dwarf expression operations to consider.
10940 ///
10941 /// @param ops_len the lengths of @p ops array above.
10942 ///
10943 /// @param index the index of the operation to interpret, in @p ops.
10944 ///
10945 /// @param next_index the index of the operation to interpret at the
10946 /// next step, after this function completed and returned.  This is
10947 /// set an output parameter that is set iff the function returns true.
10948 ///
10949 /// @param ctxt the DEVM evaluation context.
10950 ///
10951 /// @return true if the current operation actually pushes a
10952 /// non-constant value onto the DEVM stack, false otherwise.
10953 static bool
10954 op_pushes_non_constant_value(Dwarf_Op* ops,
10955                              uint64_t ops_len,
10956                              uint64_t index,
10957                              uint64_t& next_index,
10958                              dwarf_expr_eval_context& ctxt)
10959 {
10960   ABG_ASSERT(index < ops_len);
10961   Dwarf_Op& op = ops[index];
10962
10963   switch (op.atom)
10964     {
10965     case DW_OP_reg0:
10966     case DW_OP_reg1:
10967     case DW_OP_reg2:
10968     case DW_OP_reg3:
10969     case DW_OP_reg4:
10970     case DW_OP_reg5:
10971     case DW_OP_reg6:
10972     case DW_OP_reg7:
10973     case DW_OP_reg8:
10974     case DW_OP_reg9:
10975     case DW_OP_reg10:
10976     case DW_OP_reg11:
10977     case DW_OP_reg12:
10978     case DW_OP_reg13:
10979     case DW_OP_reg14:
10980     case DW_OP_reg15:
10981     case DW_OP_reg16:
10982     case DW_OP_reg17:
10983     case DW_OP_reg18:
10984     case DW_OP_reg19:
10985     case DW_OP_reg20:
10986     case DW_OP_reg21:
10987     case DW_OP_reg22:
10988     case DW_OP_reg23:
10989     case DW_OP_reg24:
10990     case DW_OP_reg25:
10991     case DW_OP_reg26:
10992     case DW_OP_reg27:
10993     case DW_OP_reg28:
10994     case DW_OP_reg29:
10995     case DW_OP_reg30:
10996     case DW_OP_reg31:
10997       next_index = index + 1;
10998       break;
10999
11000     case DW_OP_breg0:
11001     case DW_OP_breg1:
11002     case DW_OP_breg2:
11003     case DW_OP_breg3:
11004     case DW_OP_breg4:
11005     case DW_OP_breg5:
11006     case DW_OP_breg6:
11007     case DW_OP_breg7:
11008     case DW_OP_breg8:
11009     case DW_OP_breg9:
11010     case DW_OP_breg10:
11011     case DW_OP_breg11:
11012     case DW_OP_breg12:
11013     case DW_OP_breg13:
11014     case DW_OP_breg14:
11015     case DW_OP_breg15:
11016     case DW_OP_breg16:
11017     case DW_OP_breg17:
11018     case DW_OP_breg18:
11019     case DW_OP_breg19:
11020     case DW_OP_breg20:
11021     case DW_OP_breg21:
11022     case DW_OP_breg22:
11023     case DW_OP_breg23:
11024     case DW_OP_breg24:
11025     case DW_OP_breg25:
11026     case DW_OP_breg26:
11027     case DW_OP_breg27:
11028     case DW_OP_breg28:
11029     case DW_OP_breg29:
11030     case DW_OP_breg30:
11031     case DW_OP_breg31:
11032       next_index = index + 1;
11033       break;
11034
11035     case DW_OP_regx:
11036       next_index = index + 2;
11037       break;
11038
11039     case DW_OP_fbreg:
11040       next_index = index + 1;
11041       break;
11042
11043     case DW_OP_bregx:
11044       next_index = index + 1;
11045       break;
11046
11047     default:
11048       return false;
11049     }
11050
11051   expr_result r(false);
11052   ctxt.push(r);
11053
11054   return true;
11055 }
11056
11057 /// If the current operation in the dwarf expression represents a
11058 /// manipulation of the stack of the DWARF Expression Virtual Machine
11059 /// (aka DEVM), this function performs the operation and updates the
11060 /// state of the DEVM.  If the result of the operation represents a
11061 /// constant value, then the accumulator of the DEVM is set to that
11062 /// result's value, Otherwise, the DEVM accumulator is left with its
11063 /// previous value.
11064 ///
11065 /// @param expr the array of the dwarf expression operations to consider.
11066 ///
11067 /// @param expr_len the lengths of @p ops array above.
11068 ///
11069 /// @param index the index of the operation to interpret, in @p ops.
11070 ///
11071 /// @param next_index the index of the operation to interpret at the
11072 /// next step, after this function completed and returned.  This is
11073 /// set an output parameter that is set iff the function returns true.
11074 ///
11075 /// @param ctxt the DEVM evaluation context.
11076 ///
11077 /// @return true if the current operation actually manipulates the
11078 /// DEVM stack, false otherwise.
11079 static bool
11080 op_manipulates_stack(Dwarf_Op* expr,
11081                      uint64_t expr_len,
11082                      uint64_t index,
11083                      uint64_t& next_index,
11084                      dwarf_expr_eval_context& ctxt)
11085 {
11086   Dwarf_Op& op = expr[index];
11087   expr_result v;
11088
11089   switch (op.atom)
11090     {
11091     case DW_OP_dup:
11092       v = ctxt.stack.front();
11093       ctxt.push(v);
11094       break;
11095
11096     case DW_OP_drop:
11097       v = ctxt.stack.front();
11098       ctxt.pop();
11099       break;
11100
11101     case DW_OP_over:
11102       ABG_ASSERT(ctxt.stack.size() > 1);
11103       v = ctxt.stack[1];
11104       ctxt.push(v);
11105       break;
11106
11107     case DW_OP_pick:
11108       ABG_ASSERT(index + 1 < expr_len);
11109       v = op.number;
11110       ctxt.push(v);
11111       break;
11112
11113     case DW_OP_swap:
11114       ABG_ASSERT(ctxt.stack.size() > 1);
11115       v = ctxt.stack[1];
11116       ctxt.stack.erase(ctxt.stack.begin() + 1);
11117       ctxt.push(v);
11118       break;
11119
11120     case DW_OP_rot:
11121       ABG_ASSERT(ctxt.stack.size() > 2);
11122       v = ctxt.stack[2];
11123       ctxt.stack.erase(ctxt.stack.begin() + 2);
11124       ctxt.push(v);
11125       break;
11126
11127     case DW_OP_deref:
11128     case DW_OP_deref_size:
11129       ABG_ASSERT(ctxt.stack.size() > 0);
11130       ctxt.pop();
11131       v.is_const(false);
11132       ctxt.push(v);
11133       break;
11134
11135     case DW_OP_xderef:
11136     case DW_OP_xderef_size:
11137       ABG_ASSERT(ctxt.stack.size() > 1);
11138       ctxt.pop();
11139       ctxt.pop();
11140       v.is_const(false);
11141       ctxt.push(v);
11142       break;
11143
11144     case DW_OP_push_object_address:
11145       v.is_const(false);
11146       ctxt.push(v);
11147       break;
11148
11149     case DW_OP_form_tls_address:
11150     case DW_OP_GNU_push_tls_address:
11151       ABG_ASSERT(ctxt.stack.size() > 0);
11152       v = ctxt.pop();
11153       if (op.atom == DW_OP_form_tls_address)
11154         v.is_const(false);
11155       ctxt.push(v);
11156       break;
11157
11158     case DW_OP_call_frame_cfa:
11159       v.is_const(false);
11160       ctxt.push(v);
11161       break;
11162
11163     default:
11164       return false;
11165     }
11166
11167   if (v.is_const())
11168     ctxt.accum = v;
11169
11170   if (op.atom == DW_OP_form_tls_address
11171       || op.atom == DW_OP_GNU_push_tls_address)
11172     ctxt.set_tls_address(true);
11173   else
11174     ctxt.set_tls_address(false);
11175
11176   next_index = index + 1;
11177
11178   return true;
11179 }
11180
11181 /// If the current operation in the dwarf expression represents a push
11182 /// of an arithmetic or logic operation onto the dwarf expr virtual
11183 /// machine (aka DEVM), perform the operation and update the DEVM.
11184 ///
11185 /// If the result of the operation is a constant, update the DEVM
11186 /// accumulator with its value.  Otherwise, the DEVM accumulator is
11187 /// left with its previous value.
11188 ///
11189 /// @param expr the array of the dwarf expression operations to consider.
11190 ///
11191 /// @param expr_len the lengths of @p expr array above.
11192 ///
11193 /// @param index the index of the operation to interpret, in @p expr.
11194 ///
11195 /// @param next_index the index of the operation to interpret at the
11196 /// next step, after this function completed and returned.  This is
11197 /// set an output parameter that is set iff the function returns true.
11198 ///
11199 /// @param ctxt the DEVM evaluation context.
11200 ///
11201 /// @return true if the current operation actually represent an
11202 /// arithmetic or logic operation.
11203 static bool
11204 op_is_arith_logic(Dwarf_Op* expr,
11205                   uint64_t expr_len,
11206                   uint64_t index,
11207                   uint64_t& next_index,
11208                   dwarf_expr_eval_context& ctxt)
11209 {
11210   ABG_ASSERT(index < expr_len);
11211
11212   Dwarf_Op& op = expr[index];
11213   expr_result val1, val2;
11214
11215   switch (op.atom)
11216     {
11217     case DW_OP_abs:
11218       val1 = ctxt.pop();
11219       val1 = val1.abs();
11220       ctxt.push(val1);
11221       break;
11222
11223     case DW_OP_and:
11224       ABG_ASSERT(ctxt.stack.size() > 1);
11225       val1 = ctxt.pop();
11226       val2 = ctxt.pop();
11227       ctxt.push(val1 & val2);
11228       break;
11229
11230     case DW_OP_div:
11231       val1 = ctxt.pop();
11232       val2 = ctxt.pop();
11233       if (!val1.is_const())
11234         val1 = 1;
11235       ctxt.push(val2 / val1);
11236       break;
11237
11238     case DW_OP_minus:
11239       val1 = ctxt.pop();
11240       val2 = ctxt.pop();
11241       ctxt.push(val2 - val1);
11242       break;
11243
11244     case DW_OP_mod:
11245       val1 = ctxt.pop();
11246       val2 = ctxt.pop();
11247       ctxt.push(val2 % val1);
11248       break;
11249
11250     case DW_OP_mul:
11251       val1 = ctxt.pop();
11252       val2 = ctxt.pop();
11253       ctxt.push(val2 * val1);
11254       break;
11255
11256     case DW_OP_neg:
11257       val1 = ctxt.pop();
11258       ctxt.push(-val1);
11259       break;
11260
11261     case DW_OP_not:
11262       val1 = ctxt.pop();
11263       ctxt.push(~val1);
11264       break;
11265
11266     case DW_OP_or:
11267       val1 = ctxt.pop();
11268       val2 = ctxt.pop();
11269       ctxt.push(val1 | val2);
11270       break;
11271
11272     case DW_OP_plus:
11273       val1 = ctxt.pop();
11274       val2 = ctxt.pop();
11275       ctxt.push(val2 + val1);
11276       break;
11277
11278     case DW_OP_plus_uconst:
11279       val1 = ctxt.pop();
11280       val1 += op.number;
11281       ctxt.push(val1);
11282       break;
11283
11284     case DW_OP_shl:
11285       val1 = ctxt.pop();
11286       val2 = ctxt.pop();
11287       ctxt.push(val2 << val1);
11288       break;
11289
11290     case DW_OP_shr:
11291     case DW_OP_shra:
11292       val1 = ctxt.pop();
11293       val2 = ctxt.pop();
11294       ctxt.push(val2 >> val1);
11295       break;
11296
11297     case DW_OP_xor:
11298       val1 = ctxt.pop();
11299       val2 = ctxt.pop();
11300       ctxt.push(val2 ^ val1);
11301       break;
11302
11303     default:
11304       return false;
11305     }
11306
11307   if (ctxt.stack.front().is_const())
11308     ctxt.accum = ctxt.stack.front();
11309
11310   next_index = index + 1;
11311   return true;
11312 }
11313
11314 /// If the current operation in the dwarf expression represents a push
11315 /// of a control flow operation onto the dwarf expr virtual machine
11316 /// (aka DEVM), perform the operation and update the DEVM.
11317 ///
11318 /// If the result of the operation is a constant, update the DEVM
11319 /// accumulator with its value.  Otherwise, the DEVM accumulator is
11320 /// left with its previous value.
11321 ///
11322 /// @param expr the array of the dwarf expression operations to consider.
11323 ///
11324 /// @param expr_len the lengths of @p expr array above.
11325 ///
11326 /// @param index the index of the operation to interpret, in @p expr.
11327 ///
11328 /// @param next_index the index of the operation to interpret at the
11329 /// next step, after this function completed and returned.  This is
11330 /// set an output parameter that is set iff the function returns true.
11331 ///
11332 /// @param ctxt the DEVM evaluation context.
11333 ///
11334 /// @return true if the current operation actually represents a
11335 /// control flow operation, false otherwise.
11336 static bool
11337 op_is_control_flow(Dwarf_Op* expr,
11338                    uint64_t expr_len,
11339                    uint64_t index,
11340                    uint64_t& next_index,
11341                    dwarf_expr_eval_context& ctxt)
11342 {
11343   ABG_ASSERT(index < expr_len);
11344
11345   Dwarf_Op& op = expr[index];
11346   expr_result val1, val2;
11347
11348   switch (op.atom)
11349     {
11350     case DW_OP_eq:
11351     case DW_OP_ge:
11352     case DW_OP_gt:
11353     case DW_OP_le:
11354     case DW_OP_lt:
11355     case DW_OP_ne:
11356       {
11357         bool value = true;
11358         val1 = ctxt.pop();
11359         val2 = ctxt.pop();
11360         if (op.atom == DW_OP_eq)
11361           value = val2 == val1;
11362         else if (op.atom == DW_OP_ge)
11363           value = val2 >= val1;
11364         else if (op.atom == DW_OP_gt)
11365           value = val2 > val1;
11366         else if (op.atom == DW_OP_le)
11367           value = val2 <= val1;
11368         else if (op.atom == DW_OP_lt)
11369           value = val2 < val1;
11370         else if (op.atom == DW_OP_ne)
11371           value = val2 != val1;
11372
11373         val1 = value ? 1 : 0;
11374         ctxt.push(val1);
11375       }
11376       break;
11377
11378     case DW_OP_skip:
11379       if (op.number > 0)
11380         index += op.number - 1;
11381       break;
11382
11383     case DW_OP_bra:
11384       val1 = ctxt.pop();
11385       if (val1 != 0)
11386         index += val1.const_value() - 1;
11387       break;
11388
11389     case DW_OP_call2:
11390     case DW_OP_call4:
11391     case DW_OP_call_ref:
11392     case DW_OP_nop:
11393       break;
11394
11395     default:
11396       return false;
11397     }
11398
11399   if (ctxt.stack.front().is_const())
11400     ctxt.accum = ctxt.stack.front();
11401
11402   next_index = index + 1;
11403   return true;
11404 }
11405
11406 /// This function quickly evaluates a DWARF expression that is a
11407 /// constant.
11408 ///
11409 /// This is a "fast path" function that quickly evaluates a DWARF
11410 /// expression that is only made of a DW_OP_plus_uconst operator.
11411 ///
11412 /// This is a sub-routine of die_member_offset.
11413 ///
11414 /// @param expr the DWARF expression to evaluate.
11415 ///
11416 /// @param expr_len the length of the expression @p expr.
11417 ///
11418 /// @param value out parameter.  This is set to the result of the
11419 /// evaluation of @p expr, iff this function returns true.
11420 ///
11421 /// @return true iff the evaluation of @p expr went OK.
11422 static bool
11423 eval_quickly(Dwarf_Op*  expr,
11424              uint64_t   expr_len,
11425              int64_t&   value)
11426 {
11427   if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
11428     {
11429       value = expr[0].number;
11430       return true;
11431     }
11432   return false;
11433 }
11434
11435 /// Evaluate the value of the last sub-expression that is a constant,
11436 /// inside a given DWARF expression.
11437 ///
11438 /// @param expr the DWARF expression to consider.
11439 ///
11440 /// @param expr_len the length of the expression to consider.
11441 ///
11442 /// @param value the resulting value of the last constant
11443 /// sub-expression of the DWARF expression.  This is set iff the
11444 /// function returns true.
11445 ///
11446 /// @param is_tls_address out parameter.  This is set to true iff
11447 /// the resulting value of the evaluation is a TLS (thread local
11448 /// storage) address.
11449 ///
11450 /// @param eval_ctxt the evaluation context to (re)use.  Note that
11451 /// this function initializes this context before using it.
11452 ///
11453 /// @return true if the function could find a constant sub-expression
11454 /// to evaluate, false otherwise.
11455 static bool
11456 eval_last_constant_dwarf_sub_expr(Dwarf_Op*     expr,
11457                                   uint64_t      expr_len,
11458                                   int64_t&      value,
11459                                   bool& is_tls_address,
11460                                   dwarf_expr_eval_context &eval_ctxt)
11461 {
11462   // Reset the evaluation context before evaluating the constant sub
11463   // expression contained in the DWARF expression 'expr'.
11464   eval_ctxt.reset();
11465
11466   uint64_t index = 0, next_index = 0;
11467   do
11468     {
11469       if (op_is_arith_logic(expr, expr_len, index,
11470                             next_index, eval_ctxt)
11471           || op_pushes_constant_value(expr, expr_len, index,
11472                                       next_index, eval_ctxt)
11473           || op_manipulates_stack(expr, expr_len, index,
11474                                   next_index, eval_ctxt)
11475           || op_pushes_non_constant_value(expr, expr_len, index,
11476                                           next_index, eval_ctxt)
11477           || op_is_control_flow(expr, expr_len, index,
11478                                 next_index, eval_ctxt))
11479         ;
11480       else
11481         next_index = index + 1;
11482
11483       ABG_ASSERT(next_index > index);
11484       index = next_index;
11485     } while (index < expr_len);
11486
11487   is_tls_address = eval_ctxt.set_tls_address();
11488   if (eval_ctxt.accum.is_const())
11489     {
11490       value = eval_ctxt.accum;
11491       return true;
11492     }
11493   return false;
11494 }
11495
11496 /// Evaluate the value of the last sub-expression that is a constant,
11497 /// inside a given DWARF expression.
11498 ///
11499 /// @param expr the DWARF expression to consider.
11500 ///
11501 /// @param expr_len the length of the expression to consider.
11502 ///
11503 /// @param value the resulting value of the last constant
11504 /// sub-expression of the DWARF expression.  This is set iff the
11505 /// function returns true.
11506 ///
11507 /// @return true if the function could find a constant sub-expression
11508 /// to evaluate, false otherwise.
11509 static bool
11510 eval_last_constant_dwarf_sub_expr(Dwarf_Op*     expr,
11511                                   uint64_t      expr_len,
11512                                   int64_t&      value,
11513                                   bool& is_tls_address)
11514 {
11515   dwarf_expr_eval_context eval_ctxt;
11516   return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
11517                                            is_tls_address, eval_ctxt);
11518 }
11519
11520 // -----------------------------------
11521 // </location expression evaluation>
11522 // -----------------------------------
11523
11524 /// Get the offset of a struct/class member as represented by the
11525 /// value of the DW_AT_data_member_location attribute.
11526 ///
11527 /// There is a huge gotcha in here.  The value of the
11528 /// DW_AT_data_member_location is not necessarily a constant that one
11529 /// would just read and be done with it.  Rather, it can be a DWARF
11530 /// expression that one has to interpret.  In general, the offset can
11531 /// be given by the DW_AT_bit_offset attribute.  In that case the
11532 /// offset is a constant.  But it can also be given by the
11533 /// DW_AT_data_member_location attribute.  In that case it's a DWARF
11534 /// location expression.
11535 ///
11536 /// When the it's the DW_AT_data_member_location that is present,
11537 /// there are three cases to possibly take into account:
11538 ///
11539 ///     1/ The offset in the vtable where the offset of a virtual base
11540 ///        can be found, aka vptr offset.  Given the address of a
11541 ///        given object O, the vptr offset for B is given by the
11542 ///        (DWARF) expression:
11543 ///
11544 ///            address(O) + *(*address(0) - VIRTUAL_OFFSET)
11545 ///
11546 ///        where VIRTUAL_OFFSET is a constant value; In this case,
11547 ///        this function returns the constant VIRTUAL_OFFSET, as this
11548 ///        is enough to detect changes in a given virtual base
11549 ///        relative to the other virtual bases.
11550 ///
11551 ///     2/ The offset of a regular data member.  Given the address of
11552 ///        a struct object named O, the memory location for a
11553 ///        particular data member is given by the (DWARF) expression:
11554 ///
11555 ///            address(O) + OFFSET
11556 ///
11557 ///       where OFFSET is a constant.  In this case, this function
11558 ///       returns the OFFSET constant.
11559 ///
11560 ///     3/ The offset of a virtual member function in the virtual
11561 ///     pointer.  The DWARF expression is a constant that designates
11562 ///     the offset of the function in the vtable.  In this case this
11563 ///     function returns that constant.
11564 ///
11565 ///@param ctxt the read context to consider.
11566 ///
11567 ///@param die the DIE to read the information from.
11568 ///
11569 ///@param offset the resulting constant offset, in bits.  This
11570 ///argument is set iff the function returns true.
11571 static bool
11572 die_member_offset(const read_context& ctxt,
11573                   const Dwarf_Die* die,
11574                   int64_t& offset)
11575 {
11576   Dwarf_Op* expr = NULL;
11577   uint64_t expr_len = 0;
11578   uint64_t off = 0;
11579
11580   if (die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
11581     {
11582       // The DW_AT_bit_offset is present.  If it contains a non-zero
11583       // value, let's read that one.
11584       if (off != 0)
11585         {
11586           offset = off;
11587           return true;
11588         }
11589     }
11590
11591   if (!die_location_expr(die, DW_AT_data_member_location, &expr, &expr_len))
11592     return false;
11593
11594   // Otherwise, the DW_AT_data_member_location attribute is present.
11595   // In that case, let's evaluate it and get its constant
11596   // sub-expression and return that one.
11597
11598   if (!eval_quickly(expr, expr_len, offset))
11599     {
11600       bool is_tls_address = false;
11601       if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
11602                                              offset, is_tls_address,
11603                                              ctxt.dwarf_expr_eval_ctxt()))
11604         return false;
11605     }
11606
11607   offset *= 8;
11608   return true;
11609 }
11610
11611 /// Read the value of the DW_AT_location attribute from a DIE,
11612 /// evaluate the resulting DWARF expression and, if it's a constant
11613 /// expression, return it.
11614 ///
11615 /// @param die the DIE to consider.
11616 ///
11617 /// @param address the resulting constant address.  This is set iff
11618 /// the function returns true.
11619 ///
11620 /// @return true iff the whole sequence of action described above
11621 /// could be completed normally.
11622 static bool
11623 die_location_address(Dwarf_Die* die,
11624                      Dwarf_Addr&        address,
11625                      bool&              is_tls_address)
11626 {
11627   Dwarf_Op* expr = NULL;
11628   uint64_t expr_len = 0;
11629
11630   is_tls_address = false;
11631   if (!die_location_expr(die, DW_AT_location, &expr, &expr_len))
11632     return false;
11633
11634   int64_t addr = 0;
11635   if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, addr, is_tls_address))
11636     return false;
11637
11638   address = addr;
11639   return true;
11640 }
11641
11642
11643 /// Return the index of a function in its virtual table.  That is,
11644 /// return the value of the DW_AT_vtable_elem_location attribute.
11645 ///
11646 /// @param die the DIE of the function to consider.
11647 ///
11648 /// @param vindex the resulting index.  This is set iff the function
11649 /// returns true.
11650 ///
11651 /// @return true if the DIE has a DW_AT_vtable_elem_location
11652 /// attribute.
11653 static bool
11654 die_virtual_function_index(Dwarf_Die* die,
11655                            int64_t& vindex)
11656 {
11657   if (!die)
11658     return false;
11659
11660   Dwarf_Op* expr = NULL;
11661   uint64_t expr_len = 0;
11662   if (!die_location_expr(die, DW_AT_vtable_elem_location,
11663                          &expr, &expr_len))
11664     return false;
11665
11666   int64_t i = 0;
11667   bool is_tls_addr = false;
11668   if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
11669     return false;
11670
11671   vindex = i;
11672   return true;
11673 }
11674
11675 /// Test if a given DIE represents an anonymous type.
11676 ///
11677 /// Anonymous types we are interested in are classes, unions and
11678 /// enumerations.
11679 ///
11680 /// @param die the DIE to consider.
11681 ///
11682 /// @return true iff @p die represents an anonymous type.
11683 bool
11684 is_anonymous_type_die(Dwarf_Die *die)
11685 {
11686   int tag = dwarf_tag(die);
11687
11688   if (tag == DW_TAG_class_type
11689       || tag == DW_TAG_structure_type
11690       || tag == DW_TAG_union_type
11691       || tag == DW_TAG_enumeration_type)
11692     return die_is_anonymous(die);
11693
11694   return false;
11695 }
11696
11697 /// Return the base of the internal name to represent an anonymous
11698 /// type.
11699 ///
11700 /// Typically, anonymous enums would be named
11701 /// __anonymous_enum__<number>, anonymous struct or classes would be
11702 /// named __anonymous_struct__<number> and anonymous unions would be
11703 /// named __anonymous_union__<number>.  The first part of these
11704 /// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
11705 /// the base name.  This function returns that base name, depending on
11706 /// the kind of type DIE we are looking at.
11707 ///
11708 /// @param die the type DIE to look at.  This function expects a type
11709 /// DIE with an empty DW_AT_name property value (anonymous).
11710 ///
11711 /// @return a string representing the base of the internal anonymous
11712 /// name.
11713 static string
11714 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
11715 {
11716   ABG_ASSERT(die_is_type(die));
11717   ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
11718
11719   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11720   string type_name;
11721   if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
11722     type_name = tools_utils::get_anonymous_struct_internal_name_prefix();
11723   else if (tag == DW_TAG_union_type)
11724     type_name = tools_utils::get_anonymous_union_internal_name_prefix();
11725   else if (tag == DW_TAG_enumeration_type)
11726     type_name = tools_utils::get_anonymous_enum_internal_name_prefix();
11727
11728   return type_name;
11729 }
11730
11731 /// Build a full internal anonymous type name.
11732 ///
11733 /// @param base_name this is the base name as returned by the function
11734 /// @ref get_internal_anonymous_die_prefix_name.
11735 ///
11736 /// @param anonymous_type_index this is the index of the anonymous
11737 /// type in its scope.  That is, if there are more than one anonymous
11738 /// types of a given kind in a scope, this index is what tells them
11739 /// appart, starting from 0.
11740 ///
11741 /// @return the built string, which is a concatenation of @p base_name
11742 /// and @p anonymous_type_index.
11743 static string
11744 build_internal_anonymous_die_name(const string &base_name,
11745                                   size_t anonymous_type_index)
11746 {
11747   string name = base_name;
11748   if (anonymous_type_index && !base_name.empty())
11749     {
11750       std::ostringstream o;
11751       o << base_name << anonymous_type_index;
11752       name = o.str();
11753     }
11754   return name;
11755 }
11756
11757 /// Build a full internal anonymous type name.
11758 ///
11759 /// @param die the DIE representing the anonymous type to consider.
11760 ///
11761 /// @param anonymous_type_index the index of the anonymous type
11762 /// represented by @p DIE, in its scope.  That is, if there are
11763 /// several different anonymous types of the same kind as @p die, this
11764 /// index is what tells them appart.
11765 ///
11766 /// @return the internal name of the anonymous type represented by @p
11767 /// DIE.
11768 static string
11769 get_internal_anonymous_die_name(Dwarf_Die *die,
11770                                 size_t anonymous_type_index)
11771 {
11772   string name = get_internal_anonymous_die_prefix_name(die);
11773   name = build_internal_anonymous_die_name(name, anonymous_type_index);
11774   return name;
11775 }
11776
11777 // ------------------------------------
11778 // <DIE pretty printer>
11779 // ------------------------------------
11780
11781 /// Compute the qualified name of a DIE that represents a type.
11782 ///
11783 /// For instance, if the DIE tag is DW_TAG_subprogram then this
11784 /// function computes the name of the function *type*.
11785 ///
11786 /// @param ctxt the read context.
11787 ///
11788 /// @param die the DIE to consider.
11789 ///
11790 /// @param where_offset where in the are logically are in the DIE
11791 /// stream.
11792 ///
11793 /// @return a copy of the qualified name of the type.
11794 static string
11795 die_qualified_type_name(const read_context& ctxt,
11796                         const Dwarf_Die* die,
11797                         size_t where_offset)
11798 {
11799   if (!die)
11800     return "";
11801
11802   int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
11803   if (tag == DW_TAG_compile_unit
11804       || tag == DW_TAG_partial_unit
11805       || tag == DW_TAG_type_unit)
11806     return "";
11807
11808   string name = die_name(die);
11809
11810   Dwarf_Die scope_die;
11811   if (!get_scope_die(ctxt, die, where_offset, scope_die))
11812     return "";
11813
11814   string parent_name = die_qualified_name(ctxt, &scope_die, where_offset);
11815   bool colon_colon = die_is_type(die) || die_is_namespace(die);
11816   string separator = colon_colon ? "::" : ".";
11817
11818   string repr;
11819
11820   switch (tag)
11821     {
11822     case DW_TAG_unspecified_type:
11823       break;
11824
11825     case DW_TAG_base_type:
11826       {
11827         abigail::ir::integral_type int_type;
11828         if (parse_integral_type(name, int_type))
11829           repr = int_type;
11830         else
11831           repr = name;
11832       }
11833       break;
11834
11835     case DW_TAG_typedef:
11836     case DW_TAG_enumeration_type:
11837     case DW_TAG_structure_type:
11838     case DW_TAG_class_type:
11839     case DW_TAG_union_type:
11840       {
11841         if (tag == DW_TAG_typedef)
11842           {
11843             // If the underlying type of the typedef is unspecified,
11844             // bail out as we don't support that yet.
11845             Dwarf_Die underlying_type_die;
11846             if (die_die_attribute(die, DW_AT_type, underlying_type_die))
11847               {
11848                 string n = die_qualified_type_name(ctxt, &underlying_type_die,
11849                                                    where_offset);
11850                 if (die_is_unspecified(&underlying_type_die)
11851                     || n.empty())
11852                   break;
11853               }
11854           }
11855
11856         if (name.empty())
11857           // TODO: handle cases where there are more than one
11858           // anonymous type of the same kind in the same scope.  In
11859           // that case, their name must be built with the function
11860           // get_internal_anonymous_die_name or something of the same
11861           // kind.
11862           name = get_internal_anonymous_die_prefix_name(die);
11863
11864         ABG_ASSERT(!name.empty());
11865         repr = parent_name.empty() ? name : parent_name + separator + name;
11866       }
11867       break;
11868
11869     case DW_TAG_const_type:
11870     case DW_TAG_volatile_type:
11871     case DW_TAG_restrict_type:
11872       {
11873         Dwarf_Die underlying_type_die;
11874         bool has_underlying_type_die =
11875           die_die_attribute(die, DW_AT_type, underlying_type_die);
11876
11877         if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
11878           break;
11879
11880         if (tag == DW_TAG_const_type)
11881           {
11882             if (has_underlying_type_die
11883                 && die_is_reference_type(&underlying_type_die))
11884               // A reference is always const.  So, to lower false
11885               // positive reports in diff computations, we consider a
11886               // const reference just as a reference.  But we need to
11887               // keep the qualified-ness of the type.  So we introduce
11888               // a 'no-op' qualifier here.  Please remember that this
11889               // has to be kept in sync with what is done in
11890               // get_name_of_qualified_type.  So if you change this
11891               // here, you have to change that code there too.
11892               repr = "";
11893             else if (!has_underlying_type_die
11894                      || die_is_void_type(&underlying_type_die))
11895               {
11896                 repr = "void";
11897                 break;
11898               }
11899             else
11900               repr = "const";
11901           }
11902         else if (tag == DW_TAG_volatile_type)
11903           repr = "volatile";
11904         else if (tag == DW_TAG_restrict_type)
11905           repr = "restrict";
11906         else
11907           ABG_ASSERT_NOT_REACHED;
11908
11909         string underlying_type_repr;
11910         if (has_underlying_type_die)
11911           underlying_type_repr =
11912             die_qualified_type_name(ctxt, &underlying_type_die, where_offset);
11913         else
11914           underlying_type_repr = "void";
11915
11916         if (underlying_type_repr.empty())
11917           repr.clear();
11918         else
11919           {
11920             if (has_underlying_type_die
11921                 && die_is_pointer_or_reference_type(&underlying_type_die))
11922               repr = underlying_type_repr + " " + repr;
11923             else
11924               repr += " " + underlying_type_repr;
11925           }
11926       }
11927       break;
11928
11929     case DW_TAG_pointer_type:
11930     case DW_TAG_reference_type:
11931     case DW_TAG_rvalue_reference_type:
11932       {
11933         Dwarf_Die pointed_to_type_die;
11934         if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
11935           {
11936             if (tag == DW_TAG_pointer_type)
11937               repr = "void*";
11938             break;
11939           }
11940
11941         if (die_is_unspecified(&pointed_to_type_die))
11942           break;
11943
11944         string pointed_type_repr =
11945           die_qualified_type_name(ctxt, &pointed_to_type_die, where_offset);
11946
11947         repr = pointed_type_repr;
11948         if (repr.empty())
11949           break;
11950
11951         if (tag == DW_TAG_pointer_type)
11952           repr += "*";
11953         else if (tag == DW_TAG_reference_type)
11954           repr += "&";
11955         else if (tag == DW_TAG_rvalue_reference_type)
11956           repr += "&&";
11957         else
11958           ABG_ASSERT_NOT_REACHED;
11959       }
11960       break;
11961
11962     case DW_TAG_subrange_type:
11963       {
11964         // In Ada, this one can be generated on its own, that is, not
11965         // as a sub-type of an array.  So we need to support it on its
11966         // own.  Note that when it's emitted as the sub-type of an
11967         // array like in C and C++, this is handled differently, for
11968         // now.  But we try to make this usable by other languages
11969         // that are not Ada, even if we modelled it after Ada.
11970
11971         // So we build a subrange type for the sole purpose of using
11972         // the ::as_string() method of that type.  So we don't add
11973         // that type to the current type tree being built.
11974         array_type_def::subrange_sptr s =
11975           build_subrange_type(const_cast<read_context&>(ctxt),
11976                               die, where_offset,
11977                               /*associate_die_to_type=*/false);
11978         repr += s->as_string();
11979         break;
11980       }
11981
11982     case DW_TAG_array_type:
11983       {
11984         Dwarf_Die element_type_die;
11985         if (!die_die_attribute(die, DW_AT_type, element_type_die))
11986           break;
11987         string element_type_name =
11988           die_qualified_type_name(ctxt, &element_type_die, where_offset);
11989         if (element_type_name.empty())
11990           break;
11991
11992         array_type_def::subranges_type subranges;
11993         build_subranges_from_array_type_die(const_cast<read_context&>(ctxt),
11994                                             die, subranges, where_offset,
11995                                             /*associate_type_to_die=*/false);
11996
11997         repr = element_type_name;
11998         repr += array_type_def::subrange_type::vector_as_string(subranges);
11999       }
12000       break;
12001
12002     case DW_TAG_subroutine_type:
12003     case DW_TAG_subprogram:
12004       {
12005         string return_type_name;
12006         string class_name;
12007         vector<string> parm_names;
12008         bool is_const = false;
12009         bool is_static = false;
12010
12011         die_return_and_parm_names_from_fn_type_die(ctxt, die, where_offset,
12012                                                    /*pretty_print=*/true,
12013                                                    return_type_name, class_name,
12014                                                    parm_names, is_const,
12015                                                    is_static);
12016         if (return_type_name.empty())
12017           return_type_name = "void";
12018
12019         repr = return_type_name;
12020
12021         if (!class_name.empty())
12022           {
12023             // This is a method, so print the class name.
12024             repr += " (" + class_name + "::*)";
12025           }
12026
12027         // Now parameters.
12028         repr += " (";
12029         for (vector<string>::const_iterator i = parm_names.begin();
12030              i != parm_names.end();
12031              ++i)
12032           {
12033             if (i != parm_names.begin())
12034               repr += ", ";
12035             repr += *i;
12036           }
12037         repr += ")";
12038
12039       }
12040       break;
12041
12042     case DW_TAG_string_type:
12043     case DW_TAG_ptr_to_member_type:
12044     case DW_TAG_set_type:
12045     case DW_TAG_file_type:
12046     case DW_TAG_packed_type:
12047     case DW_TAG_thrown_type:
12048     case DW_TAG_interface_type:
12049     case DW_TAG_shared_type:
12050       break;
12051     }
12052
12053   return repr;
12054 }
12055
12056 /// Compute the qualified name of a decl represented by a given DIE.
12057 ///
12058 /// For instance, for a DIE of tag DW_TAG_subprogram this function
12059 /// computes the signature of the function *declaration*.
12060 ///
12061 /// @param ctxt the read context.
12062 ///
12063 /// @param die the DIE to consider.
12064 ///
12065 /// @param where_offset where we are logically at in the DIE stream.
12066 ///
12067 /// @return a copy of the computed name.
12068 static string
12069 die_qualified_decl_name(const read_context& ctxt,
12070                         const Dwarf_Die* die,
12071                         size_t where_offset)
12072 {
12073   if (!die || !die_is_decl(die))
12074     return "";
12075
12076   string name = die_name(die);
12077
12078   Dwarf_Die scope_die;
12079   if (!get_scope_die(ctxt, die, where_offset, scope_die))
12080     return "";
12081
12082   string scope_name = die_qualified_name(ctxt, &scope_die, where_offset);
12083   string separator = "::";
12084
12085   string repr;
12086
12087   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
12088   switch (tag)
12089     {
12090     case DW_TAG_namespace:
12091     case DW_TAG_member:
12092     case DW_TAG_variable:
12093       repr = scope_name.empty() ? name : scope_name + separator + name;
12094       break;
12095     case DW_TAG_subprogram:
12096       repr = die_function_signature(ctxt, die, where_offset);
12097       break;
12098
12099     case DW_TAG_unspecified_parameters:
12100       repr = "...";
12101       break;
12102
12103     case DW_TAG_formal_parameter:
12104     case DW_TAG_imported_declaration:
12105     case DW_TAG_GNU_template_template_param:
12106     case DW_TAG_GNU_template_parameter_pack:
12107     case DW_TAG_GNU_formal_parameter_pack:
12108       break;
12109     }
12110   return repr;
12111 }
12112
12113 /// Compute the qualified name of the artifact represented by a given
12114 /// DIE.
12115 ///
12116 /// If the DIE represents a type, then the function computes the name
12117 /// of the type.  Otherwise, if the DIE represents a decl then the
12118 /// function computes the name of the decl.  Note that a DIE of tag
12119 /// DW_TAG_subprogram is going to be considered as a "type" -- just
12120 /// like if it was a DW_TAG_subroutine_type.
12121 ///
12122 /// @param ctxt the read context.
12123 ///
12124 /// @param die the DIE to consider.
12125 ///
12126 /// @param where_offset where we are logically at in the DIE stream.
12127 ///
12128 /// @return a copy of the computed name.
12129 static string
12130 die_qualified_name(const read_context& ctxt, const Dwarf_Die* die, size_t where)
12131 {
12132   if (die_is_type(die))
12133     return die_qualified_type_name(ctxt, die, where);
12134   else if (die_is_decl(die))
12135     return die_qualified_decl_name(ctxt, die, where);
12136   return "";
12137 }
12138
12139 /// Test if the qualified name of a given type should be empty.
12140 ///
12141 /// The reason why the name of a DIE with a given tag would be empty
12142 /// is that libabigail's internal representation doesn't yet support
12143 /// that tag; or if the DIE's qualified name is built from names of
12144 /// sub-types DIEs whose tags are not yet supported.
12145 ///
12146 /// @param ctxt the reading context.
12147 ///
12148 /// @param die the DIE to consider.
12149 ///
12150 /// @param where where we are logically at, in the DIE stream.
12151 ///
12152 /// @param qualified_name the qualified name of the DIE.  This is set
12153 /// only iff the function returns false.
12154 ///
12155 /// @return true if the qualified name of the DIE is empty.
12156 static bool
12157 die_qualified_type_name_empty(const read_context& ctxt,
12158                               const Dwarf_Die* die,
12159                               size_t where, string &qualified_name)
12160 {
12161   if (!die)
12162     return true;
12163
12164   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
12165
12166   string qname;
12167   if (tag == DW_TAG_typedef
12168       || tag == DW_TAG_pointer_type
12169       || tag == DW_TAG_reference_type
12170       || tag == DW_TAG_rvalue_reference_type
12171       || tag == DW_TAG_array_type
12172       || tag == DW_TAG_const_type
12173       || tag == DW_TAG_volatile_type
12174       || tag == DW_TAG_restrict_type)
12175     {
12176       Dwarf_Die underlying_type_die;
12177       if (die_die_attribute(die, DW_AT_type, underlying_type_die))
12178         {
12179           string name =
12180             die_qualified_type_name(ctxt, &underlying_type_die, where);
12181           if (name.empty())
12182             return true;
12183         }
12184     }
12185   else
12186     {
12187       string name = die_qualified_type_name(ctxt, die, where);
12188       if (name.empty())
12189         return true;
12190     }
12191
12192   qname = die_qualified_type_name(ctxt, die, where);
12193   if (qname.empty())
12194     return true;
12195
12196   qualified_name = qname;
12197   return false;
12198 }
12199
12200 /// Given the DIE that represents a function type, compute the names
12201 /// of the following properties the function's type:
12202 ///
12203 ///   - return type
12204 ///   - enclosing class (if the function is a member function)
12205 ///   - function parameter types
12206 ///
12207 /// When the function we are looking at is a member function, it also
12208 /// tells if it's const.
12209 ///
12210 /// @param ctxt the reading context.
12211 ///
12212 /// @param die the DIE of the function or function type we are looking
12213 /// at.
12214 ///
12215 /// @param where_offset where we are logically at in the DIE stream.
12216 ///
12217 /// @param pretty_print if set to yes, the type names are going to be
12218 /// pretty-printed names; otherwise, they are just qualified type
12219 /// names.
12220 ///
12221 /// @param return_type_name out parameter.  This contains the name of
12222 /// the return type of the function.
12223 ///
12224 /// @param class_name out parameter.  If the function is a member
12225 /// function, this contains the name of the enclosing class.
12226 ///
12227 /// @param parm_names out parameter.  This vector is set to the names
12228 /// of the types of the parameters of the function.
12229 ///
12230 /// @param is_const out parameter.  If the function is a member
12231 /// function, this is set to true iff the member function is const.
12232 ///
12233 /// @param is_static out parameter.  If the function is a static
12234 /// member function, then this is set to true.
12235 static void
12236 die_return_and_parm_names_from_fn_type_die(const read_context& ctxt,
12237                                            const Dwarf_Die* die,
12238                                            size_t where_offset,
12239                                            bool pretty_print,
12240                                            string &return_type_name,
12241                                            string &class_name,
12242                                            vector<string>& parm_names,
12243                                            bool& is_const,
12244                                            bool& is_static)
12245 {
12246   Dwarf_Die child;
12247   Dwarf_Die ret_type_die;
12248   if (!die_die_attribute(die, DW_AT_type, ret_type_die))
12249     return_type_name = "void";
12250   else
12251     return_type_name =
12252       pretty_print
12253       ? ctxt.get_die_pretty_representation(&ret_type_die, where_offset)
12254       : ctxt.get_die_qualified_type_name(&ret_type_die, where_offset);
12255
12256   if (return_type_name.empty())
12257     return_type_name = "void";
12258
12259   Dwarf_Die object_pointer_die, class_die;
12260   bool is_method_type =
12261     die_function_type_is_method_type(ctxt, die, where_offset,
12262                                      object_pointer_die,
12263                                      class_die, is_static);
12264
12265   is_const = false;
12266   if (is_method_type)
12267     {
12268       class_name = ctxt.get_die_qualified_type_name(&class_die, where_offset);
12269
12270       Dwarf_Die this_pointer_die;
12271       Dwarf_Die pointed_to_die;
12272       if (!is_static
12273           && die_die_attribute(&object_pointer_die, DW_AT_type,
12274                                this_pointer_die))
12275         if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
12276           if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
12277             is_const = true;
12278
12279       string fn_name = die_name(die);
12280       string non_qualified_class_name = die_name(&class_die);
12281       bool is_ctor = fn_name == non_qualified_class_name;
12282       bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
12283
12284       if (is_ctor || is_dtor)
12285         return_type_name.clear();
12286     }
12287
12288   if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
12289     do
12290       {
12291         int child_tag = dwarf_tag(&child);
12292         if (child_tag == DW_TAG_formal_parameter)
12293           {
12294             Dwarf_Die parm_type_die;
12295             if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
12296               continue;
12297             string qualified_name =
12298               pretty_print
12299               ? ctxt.get_die_pretty_representation(&parm_type_die, where_offset)
12300               : ctxt.get_die_qualified_type_name(&parm_type_die, where_offset);
12301
12302             if (qualified_name.empty())
12303               continue;
12304             parm_names.push_back(qualified_name);
12305           }
12306         else if (child_tag == DW_TAG_unspecified_parameters)
12307           {
12308             // This is a variadic function parameter.
12309             parm_names.push_back("variadic parameter type");
12310             // After a DW_TAG_unspecified_parameters tag, we shouldn't
12311             // keep reading for parameters.  The
12312             // unspecified_parameters TAG should be the last parameter
12313             // that we record. For instance, if there are multiple
12314             // DW_TAG_unspecified_parameters DIEs then we should care
12315             // only for the first one.
12316             break;
12317           }
12318       }
12319     while (dwarf_siblingof(&child, &child) == 0);
12320
12321   if (class_name.empty())
12322     {
12323       Dwarf_Die parent_die;
12324       if (get_parent_die(ctxt, die, parent_die, where_offset))
12325         {
12326           if (die_is_class_type(&parent_die))
12327             class_name =
12328               ctxt.get_die_qualified_type_name(&parent_die, where_offset);
12329         }
12330     }
12331 }
12332
12333 /// This computes the signature of the a function declaration
12334 /// represented by a DIE.
12335 ///
12336 /// @param ctxt the reading context.
12337 ///
12338 /// @param fn_die the DIE of the function to consider.
12339 ///
12340 /// @param where_offset where we are logically at in the stream of
12341 /// DIEs.
12342 ///
12343 /// @return a copy of the computed function signature string.
12344 static string
12345 die_function_signature(const read_context& ctxt,
12346                        const Dwarf_Die *fn_die,
12347                        size_t where_offset)
12348 {
12349
12350   translation_unit::language lang;
12351   bool has_lang = false;
12352   if ((has_lang = ctxt.get_die_language(fn_die, lang)))
12353     {
12354       // In a binary originating from the C language, it's OK to use
12355       // the linkage name of the function as a key for the map which
12356       // is meant to reduce the number of DIE comparisons involved
12357       // during DIE canonicalization computation.
12358       if (is_c_language(lang))
12359         {
12360           string fn_name = die_linkage_name(fn_die);
12361           if (fn_name.empty())
12362             fn_name = die_name(fn_die);
12363           return fn_name;
12364         }
12365     }
12366
12367   // TODO: When we can structurally compare DIEs originating from C++
12368   // as well, we can use the linkage name of functions in C++ too, to
12369   // reduce the number of comparisons involved during DIE
12370   // canonicalization.
12371
12372   string return_type_name;
12373   Dwarf_Die ret_type_die;
12374   if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
12375     return_type_name = ctxt.get_die_qualified_type_name(&ret_type_die,
12376                                                         where_offset);
12377
12378   if (return_type_name.empty())
12379     return_type_name = "void";
12380
12381   Dwarf_Die scope_die;
12382   string scope_name;
12383   if (get_scope_die(ctxt, fn_die, where_offset, scope_die))
12384     scope_name = ctxt.get_die_qualified_name(&scope_die, where_offset);
12385   string fn_name = die_name(fn_die);
12386   if (!scope_name.empty())
12387     fn_name  = scope_name + "::" + fn_name;
12388
12389   string class_name;
12390   vector<string> parm_names;
12391   bool is_const = false;
12392   bool is_static = false;
12393
12394   die_return_and_parm_names_from_fn_type_die(ctxt, fn_die, where_offset,
12395                                              /*pretty_print=*/false,
12396                                              return_type_name, class_name,
12397                                              parm_names, is_const, is_static);
12398
12399   bool is_virtual = die_is_virtual(fn_die);
12400
12401   string repr = class_name.empty() ? "function" : "method";
12402   if (is_virtual)
12403     repr += " virtual";
12404
12405   if (!return_type_name.empty())
12406     repr += " " + return_type_name;
12407
12408   repr += " " + fn_name;
12409
12410   // Now parameters.
12411   repr += "(";
12412   bool some_parm_emitted = false;
12413   for (vector<string>::const_iterator i = parm_names.begin();
12414        i != parm_names.end();
12415        ++i)
12416     {
12417       if (i != parm_names.begin())
12418         {
12419           if (some_parm_emitted)
12420             repr += ", ";
12421         }
12422       else
12423         if (!is_static && !class_name.empty())
12424           // We are printing a non-static method name, skip the implicit "this"
12425           // parameter type.
12426           continue;
12427       repr += *i;
12428       some_parm_emitted = true;
12429     }
12430   repr += ")";
12431
12432   if (is_const)
12433     {
12434       ABG_ASSERT(!class_name.empty());
12435       repr += " const";
12436     }
12437
12438   return repr;
12439 }
12440
12441 /// Return a pretty string representation of a type, for internal purposes.
12442 ///
12443 /// By internal purpose, we mean things like key-ing types for lookup
12444 /// purposes and so on.
12445 ///
12446 /// Note that this function is also used to pretty print functions.
12447 /// For functions, it prints the *type* of the function.
12448 ///
12449 /// @param ctxt the context to use.
12450 ///
12451 /// @param the DIE of the type to pretty print.
12452 ///
12453 /// @param where_offset where we logically are placed when calling
12454 /// this.  It's useful to handle inclusion of DW_TAG_compile_unit
12455 /// entries.
12456 ///
12457 /// @return the resulting pretty representation.
12458 static string
12459 die_pretty_print_type(read_context& ctxt,
12460                       const Dwarf_Die* die,
12461                       size_t where_offset)
12462 {
12463   if (!die
12464       || (!die_is_type(die)
12465           && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
12466     return "";
12467
12468   string repr;
12469
12470   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
12471   switch (tag)
12472     {
12473     case DW_TAG_string_type:
12474       // For now, we won't try to go get the actual representation of
12475       // the string because this would make things more complicated;
12476       // for that we'd need to interpret some location expressions to
12477       // get the length of the string.  And for dynamically allocated
12478       // strings, the result of the location expression evaluation
12479       // might not even be a constant.  So at the moment I consider
12480       // this to be a lot of hassle for no great return.  Until proven
12481       // otherwise, of course.
12482       repr = "string type";
12483
12484     case DW_TAG_unspecified_type:
12485     case DW_TAG_ptr_to_member_type:
12486       break;
12487
12488     case DW_TAG_namespace:
12489       repr = "namespace " + ctxt.get_die_qualified_type_name(die, where_offset);
12490       break;
12491
12492     case DW_TAG_base_type:
12493       repr = ctxt.get_die_qualified_type_name(die, where_offset);
12494       break;
12495
12496     case DW_TAG_typedef:
12497       {
12498         string qualified_name;
12499         if (!die_qualified_type_name_empty(ctxt, die,
12500                                            where_offset,
12501                                            qualified_name))
12502           repr = "typedef " + qualified_name;
12503       }
12504       break;
12505
12506     case DW_TAG_const_type:
12507     case DW_TAG_volatile_type:
12508     case DW_TAG_restrict_type:
12509     case DW_TAG_pointer_type:
12510     case DW_TAG_reference_type:
12511     case DW_TAG_rvalue_reference_type:
12512       repr = ctxt.get_die_qualified_type_name(die, where_offset);
12513       break;
12514
12515     case DW_TAG_enumeration_type:
12516       {
12517         string qualified_name =
12518           ctxt.get_die_qualified_type_name(die, where_offset);
12519         repr = "enum " + qualified_name;
12520       }
12521       break;
12522
12523     case DW_TAG_structure_type:
12524     case DW_TAG_class_type:
12525       {
12526         string qualified_name =
12527           ctxt.get_die_qualified_type_name(die, where_offset);
12528         repr = "class " + qualified_name;
12529       }
12530       break;
12531
12532     case DW_TAG_union_type:
12533       {
12534         string qualified_name =
12535           ctxt.get_die_qualified_type_name(die, where_offset);
12536         repr = "union " + qualified_name;
12537       }
12538       break;
12539
12540     case DW_TAG_array_type:
12541       {
12542         Dwarf_Die element_type_die;
12543         if (!die_die_attribute(die, DW_AT_type, element_type_die))
12544           break;
12545         string element_type_name =
12546           ctxt.get_die_qualified_type_name(&element_type_die, where_offset);
12547         if (element_type_name.empty())
12548           break;
12549
12550         array_type_def::subranges_type subranges;
12551         build_subranges_from_array_type_die(ctxt, die, subranges, where_offset,
12552                                             /*associate_type_to_die=*/false);
12553
12554         repr = element_type_name;
12555         repr += array_type_def::subrange_type::vector_as_string(subranges);
12556       }
12557       break;
12558
12559     case DW_TAG_subrange_type:
12560       {
12561         // So this can be generated by Ada, on its own; that is, not
12562         // as a subtype of an array.  In that case we need to handle
12563         // it properly.
12564
12565         // For now, we consider that the pretty printed name of the
12566         // subrange type is its name.  We might need something more
12567         // advance, should the needs of the users get more
12568         // complicated.
12569         repr += die_qualified_type_name(ctxt, die, where_offset);
12570       }
12571       break;
12572
12573     case DW_TAG_subroutine_type:
12574     case DW_TAG_subprogram:
12575       {
12576         string return_type_name;
12577         string class_name;
12578         vector<string> parm_names;
12579         bool is_const = false;
12580         bool is_static = false;
12581
12582         die_return_and_parm_names_from_fn_type_die(ctxt, die, where_offset,
12583                                                    /*pretty_print=*/true,
12584                                                    return_type_name, class_name,
12585                                                    parm_names, is_const,
12586                                                    is_static);
12587         if (class_name.empty())
12588           repr = "function type";
12589         else
12590           repr = "method type";
12591         repr += " " + ctxt.get_die_qualified_type_name(die, where_offset);
12592       }
12593       break;
12594
12595     case DW_TAG_set_type:
12596     case DW_TAG_file_type:
12597     case DW_TAG_packed_type:
12598     case DW_TAG_thrown_type:
12599     case DW_TAG_interface_type:
12600     case DW_TAG_shared_type:
12601       ABG_ASSERT_NOT_REACHED;
12602     }
12603
12604   return repr;
12605 }
12606
12607 /// Return a pretty string representation of a declaration, for
12608 /// internal purposes.
12609 ///
12610 /// By internal purpose, we mean things like key-ing declarations for
12611 /// lookup purposes and so on.
12612 ///
12613 /// Note that this function is also used to pretty print functions.
12614 /// For functions, it prints the signature of the function.
12615 ///
12616 /// @param ctxt the context to use.
12617 ///
12618 /// @param the DIE of the declaration to pretty print.
12619 ///
12620 /// @param where_offset where we logically are placed when calling
12621 /// this.  It's useful to handle inclusion of DW_TAG_compile_unit
12622 /// entries.
12623 ///
12624 /// @return the resulting pretty representation.
12625 static string
12626 die_pretty_print_decl(read_context& ctxt,
12627                       const Dwarf_Die* die,
12628                       size_t where_offset)
12629 {
12630   if (!die || !die_is_decl(die))
12631     return "";
12632
12633   string repr;
12634
12635   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
12636   switch (tag)
12637     {
12638     case DW_TAG_namespace:
12639       repr = "namespace " + die_qualified_name(ctxt, die, where_offset);
12640       break;
12641
12642     case DW_TAG_member:
12643     case DW_TAG_variable:
12644       {
12645         string type_repr = "void";
12646         Dwarf_Die type_die;
12647         if (die_die_attribute(die, DW_AT_type, type_die))
12648           type_repr = die_qualified_type_name(ctxt, &type_die, where_offset);
12649         repr = die_qualified_name(ctxt, die, where_offset);
12650         if (!repr.empty())
12651           repr = type_repr + " " + repr;
12652       }
12653       break;
12654
12655     case DW_TAG_subprogram:
12656       repr = die_function_signature(ctxt, die, where_offset);
12657       break;
12658
12659     default:
12660       break;
12661     }
12662   return repr;
12663 }
12664
12665 /// Compute the pretty printed representation of an artifact
12666 /// represented by a DIE.
12667 ///
12668 /// If the DIE is a type, compute the its pretty representation as a
12669 /// type; otherwise, if it's a declaration, compute its pretty
12670 /// representation as a declaration.  Note for For instance, that a
12671 /// DW_TAG_subprogram DIE is going to be represented as a function
12672 /// *type*.
12673 ///
12674 /// @param ctxt the reading context.
12675 ///
12676 /// @param die the DIE to consider.
12677 ///
12678 /// @param where_offset we in the DIE stream we are logically at.
12679 ///
12680 /// @return a copy of the pretty printed artifact.
12681 static string
12682 die_pretty_print(read_context& ctxt, const Dwarf_Die* die, size_t where_offset)
12683 {
12684   if (die_is_type(die))
12685     return die_pretty_print_type(ctxt, die, where_offset);
12686   else if (die_is_decl(die))
12687     return die_pretty_print_decl(ctxt, die, where_offset);
12688   return "";
12689 }
12690
12691 // -----------------------------------
12692 // </die pretty printer>
12693 // -----------------------------------
12694
12695
12696 // ----------------------------------
12697 // <die comparison engine>
12698 // ---------------------------------
12699
12700 /// Compares two decls DIEs
12701 ///
12702 /// This works only for DIEs emitted by the C language.
12703 ///
12704 /// This implementation doesn't yet support namespaces.
12705 ///
12706 /// This is a subroutine of compare_dies.
12707 ///
12708 /// @return true iff @p l equals @p r.
12709 static bool
12710 compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
12711 {
12712   ABG_ASSERT(l && r);
12713
12714   int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
12715   int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12716   if (l_tag != r_tag)
12717     return false;
12718
12719   bool result = false;
12720
12721   if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
12722     {
12723       // Fast path for functions and global variables.
12724       if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
12725                                               result)
12726           || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
12727                                                  result))
12728         {
12729           if (!result)
12730             return false;
12731         }
12732
12733       if (compare_dies_string_attribute_value(l, r, DW_AT_name,
12734                                               result))
12735         {
12736           if (!result)
12737             return false;
12738         }
12739       return true;
12740     }
12741
12742   // Fast path for types.
12743   if (compare_dies_string_attribute_value(l, r, DW_AT_name,
12744                                           result))
12745     return result;
12746   return true;
12747 }
12748
12749 /// Compares two type DIEs
12750 ///
12751 /// This is a subroutine of compare_dies.
12752 ///
12753 /// @param l the left operand of the comparison operator.
12754 ///
12755 /// @param r the right operand of the comparison operator.
12756 ///
12757 /// @return true iff @p l equals @p r.
12758 static bool
12759 compare_as_type_dies(const Dwarf_Die *l, const Dwarf_Die *r)
12760 {
12761   ABG_ASSERT(l && r);
12762   ABG_ASSERT(die_is_type(l));
12763   ABG_ASSERT(die_is_type(r));
12764
12765   if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
12766       && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
12767       && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12768           != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
12769     // For now, we cannot compare DW_TAG_string_type because of its
12770     // string_length attribute that is a location descriptor that is
12771     // not necessarily a constant.  So it's super hard to evaluate it
12772     // in a libabigail context.  So for now, we just say that all
12773     // DW_TAG_string_type DIEs are different, by default.
12774     return false;
12775
12776   uint64_t l_size = 0, r_size = 0;
12777   die_size_in_bits(l, l_size);
12778   die_size_in_bits(r, r_size);
12779
12780   return l_size == r_size;
12781 }
12782
12783 /// Test if two DIEs representing function declarations have the same
12784 /// linkage name, and thus are considered equal if they are C or C++,
12785 /// because the two DIEs represent functions in the same binary.
12786 ///
12787 /// If the DIEs don't have a linkage name, the function compares their
12788 /// name.  But in that case, the caller of the function must know that
12789 /// in C++ for instance, that doesn't imply that the two functions are
12790 /// equal.
12791 ///
12792 /// @param ctxt the @ref read_context to consider.
12793 ///
12794 /// @param l the first function DIE to consider.
12795 ///
12796 /// @param r the second function DIE to consider.
12797 ///
12798 /// @return true iff the function represented by @p l have the same
12799 /// linkage name as the function represented by @p r.
12800 static bool
12801 fn_die_equal_by_linkage_name(const read_context &ctxt,
12802                              const Dwarf_Die *l,
12803                              const Dwarf_Die *r)
12804 {
12805   if (!!l != !!r)
12806     return false;
12807
12808   if (!l)
12809     return false;
12810
12811   int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
12812   ABG_ASSERT(tag == DW_TAG_subprogram);
12813   tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12814   ABG_ASSERT(tag == DW_TAG_subprogram);
12815
12816   string lname = die_name(l), rname = die_name(r);
12817   string llinkage_name = die_linkage_name(l),
12818     rlinkage_name = die_linkage_name(r);
12819
12820   if (ctxt.die_is_in_c_or_cplusplus(l)
12821       && ctxt.die_is_in_c_or_cplusplus(r))
12822     {
12823       if (!llinkage_name.empty() && !rlinkage_name.empty())
12824         return llinkage_name == rlinkage_name;
12825       else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
12826         return false;
12827       else
12828         return lname == rname;
12829     }
12830
12831   return (!llinkage_name.empty()
12832           && !rlinkage_name.empty()
12833           && llinkage_name == rlinkage_name);
12834 }
12835
12836 /// Compare two DIEs emitted by a C compiler.
12837 ///
12838 /// @param ctxt the read context used to load the DWARF information.
12839 ///
12840 /// @param l the left-hand-side argument of this comparison operator.
12841 ///
12842 /// @param r the righ-hand-side argument of this comparison operator.
12843 ///
12844 /// @param aggregates_being_compared this holds the names of the set
12845 /// of aggregates being compared.  It's used by the comparison
12846 /// function to avoid recursing infinitely when faced with types
12847 /// referencing themselves through pointers or references.  By
12848 /// default, just pass an empty instance of @ref istring_set_type to
12849 /// it.
12850 ///
12851 /// @param update_canonical_dies_on_the_fly if true, when two
12852 /// sub-types compare equal (during the comparison of @p l and @p r)
12853 /// update their canonical type.  That way, two types of the same name
12854 /// are structurally compared to each other only once.  So the
12855 /// non-linear structural comparison of two types of the same name
12856 /// only happen once.
12857 ///
12858 /// @return true iff @p l equals @p r.
12859 static bool
12860 compare_dies(const read_context& ctxt,
12861              const Dwarf_Die *l, const Dwarf_Die *r,
12862              istring_set_type& aggregates_being_compared,
12863              bool update_canonical_dies_on_the_fly)
12864 {
12865   ABG_ASSERT(l);
12866   ABG_ASSERT(r);
12867
12868   int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
12869     r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12870
12871   if (l_tag != r_tag)
12872     return false;
12873
12874   Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l)),
12875     r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
12876   Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
12877   die_source l_die_source, r_die_source;
12878   ABG_ASSERT(ctxt.get_die_source(l, l_die_source));
12879   ABG_ASSERT(ctxt.get_die_source(r, r_die_source));
12880
12881   // If 'l' and 'r' already have canonical DIEs, then just compare the
12882   // offsets of their canonical DIEs.
12883   bool l_has_canonical_die_offset =
12884     (l_canonical_die_offset =
12885      ctxt.get_canonical_die_offset(l_offset, l_die_source,
12886                                    /*die_as_type=*/true));
12887
12888   bool r_has_canonical_die_offset =
12889     (r_canonical_die_offset =
12890      ctxt.get_canonical_die_offset(r_offset, r_die_source,
12891                                    /*die_as_type=*/true));
12892
12893   if (l_has_canonical_die_offset && r_has_canonical_die_offset)
12894     return l_canonical_die_offset == r_canonical_die_offset;
12895
12896   bool result = true;
12897
12898   switch (l_tag)
12899     {
12900     case DW_TAG_base_type:
12901     case DW_TAG_string_type:
12902       if (!compare_as_type_dies(l, r)
12903           || !compare_as_decl_dies(l, r))
12904         result = false;
12905       break;
12906
12907     case DW_TAG_typedef:
12908     case DW_TAG_pointer_type:
12909     case DW_TAG_reference_type:
12910     case DW_TAG_rvalue_reference_type:
12911     case DW_TAG_const_type:
12912     case DW_TAG_volatile_type:
12913     case DW_TAG_restrict_type:
12914       {
12915         if (!compare_as_type_dies(l, r))
12916           {
12917             result = false;
12918             break;
12919           }
12920
12921         bool from_the_same_tu = false;
12922         if (!pointer_or_qual_die_of_anonymous_class_type(l)
12923             && compare_dies_cu_decl_file(l, r, from_the_same_tu)
12924             && from_the_same_tu)
12925           {
12926             // These two typedefs, pointer, reference, or qualified
12927             // types have the same name and are defined in the same TU.
12928             // They thus ought to be the same.
12929             //
12930             // Note that pointers, reference or qualified types to
12931             // anonymous types are not taking into account here because
12932             // those always need to be structurally compared.
12933             result = true;
12934             break;
12935           }
12936       }
12937
12938       {
12939         // No fancy optimization in this case.  We need to
12940         // structurally compare the two DIEs.
12941         Dwarf_Die lu_type_die, ru_type_die;
12942         bool lu_is_void, ru_is_void;
12943
12944         lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
12945         ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
12946
12947         if (lu_is_void && ru_is_void)
12948           result = true;
12949         else if (lu_is_void != ru_is_void)
12950           result = false;
12951         else
12952           result = compare_dies(ctxt, &lu_type_die, &ru_type_die,
12953                                 aggregates_being_compared,
12954                                 update_canonical_dies_on_the_fly);
12955       }
12956       break;
12957
12958     case DW_TAG_enumeration_type:
12959       if (!compare_as_type_dies(l, r)
12960           || !compare_as_decl_dies(l, r))
12961         result = false;
12962       else
12963         {
12964           // Walk the enumerators.
12965           Dwarf_Die l_enumtor, r_enumtor;
12966           bool found_l_enumtor, found_r_enumtor;
12967
12968           for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
12969                                              &l_enumtor) == 0,
12970                  found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
12971                                                &r_enumtor) == 0;
12972                found_l_enumtor && found_r_enumtor;
12973                found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
12974                  found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
12975             {
12976               int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
12977               if ( l_tag != r_tag)
12978                 {
12979                   result = false;
12980                   break;
12981                 }
12982
12983               if (l_tag != DW_TAG_enumerator)
12984                 continue;
12985
12986               uint64_t l_val = 0, r_val = 0;
12987               die_unsigned_constant_attribute(&l_enumtor,
12988                                               DW_AT_const_value,
12989                                               l_val);
12990               die_unsigned_constant_attribute(&r_enumtor,
12991                                               DW_AT_const_value,
12992                                               r_val);
12993               if (l_val != r_val)
12994                 {
12995                   result = false;
12996                   break;
12997                 }
12998             }
12999           if (found_l_enumtor != found_r_enumtor )
13000             result = false;
13001
13002         }
13003       break;
13004
13005     case DW_TAG_structure_type:
13006     case DW_TAG_union_type:
13007       {
13008         interned_string ln = ctxt.get_die_pretty_type_representation(l, 0);
13009         interned_string rn = ctxt.get_die_pretty_type_representation(r, 0);
13010
13011         if ((aggregates_being_compared.find(ln)
13012              != aggregates_being_compared.end())
13013             || (aggregates_being_compared.find(rn)
13014                 != aggregates_being_compared.end()))
13015           result = true;
13016         else if (!compare_as_decl_dies(l, r))
13017           result = false;
13018         else if (!compare_as_type_dies(l, r))
13019           result = false;
13020         else
13021           {
13022             aggregates_being_compared.insert(ln);
13023             aggregates_being_compared.insert(rn);
13024
13025             Dwarf_Die l_member, r_member;
13026             bool found_l_member, found_r_member;
13027             for (found_l_member = dwarf_child(const_cast<Dwarf_Die*>(l),
13028                                               &l_member) == 0,
13029                    found_r_member = dwarf_child(const_cast<Dwarf_Die*>(r),
13030                                                 &r_member) == 0;
13031                  found_l_member && found_r_member;
13032                  found_l_member = dwarf_siblingof(&l_member, &l_member) == 0,
13033                    found_r_member = dwarf_siblingof(&r_member, &r_member) == 0)
13034               {
13035                 int l_tag = dwarf_tag(&l_member), r_tag = dwarf_tag(&r_member);
13036                 if (l_tag != r_tag)
13037                   {
13038                     result = false;
13039                     break;
13040                   }
13041
13042                 if (l_tag != DW_TAG_member && l_tag != DW_TAG_variable)
13043                   continue;
13044
13045                 if (!compare_dies(ctxt, &l_member, &r_member,
13046                                   aggregates_being_compared,
13047                                   update_canonical_dies_on_the_fly))
13048                   {
13049                     result = false;
13050                     break;
13051                   }
13052               }
13053             if (found_l_member != found_r_member)
13054               result = false;
13055
13056             aggregates_being_compared.erase(ln);
13057             aggregates_being_compared.erase(rn);
13058           }
13059       }
13060       break;
13061
13062     case DW_TAG_array_type:
13063       {
13064         Dwarf_Die l_child, r_child;
13065         bool found_l_child, found_r_child;
13066         for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
13067                                          &l_child) == 0,
13068                found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
13069                                            &r_child) == 0;
13070              found_l_child && found_r_child;
13071              found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
13072                found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
13073           {
13074             int l_child_tag = dwarf_tag(&l_child),
13075               r_child_tag = dwarf_tag(&r_child);
13076             if (l_child_tag == DW_TAG_subrange_type
13077                 || r_child_tag == DW_TAG_subrange_type)
13078               if (!compare_dies(ctxt, &l_child, &r_child,
13079                                 aggregates_being_compared,
13080                                 update_canonical_dies_on_the_fly))
13081                 {
13082                   result = false;
13083                   break;
13084                 }
13085           }
13086         if (found_l_child != found_r_child)
13087           result = false;
13088       }
13089       break;
13090
13091     case DW_TAG_subrange_type:
13092       {
13093         uint64_t l_lower_bound = 0, r_lower_bound = 0,
13094           l_upper_bound = 0, r_upper_bound = 0;
13095         die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
13096         die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
13097         if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
13098                                              l_upper_bound))
13099           {
13100             uint64_t l_count = 0;
13101             if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
13102               {
13103                 l_upper_bound = l_lower_bound + l_count;
13104                 if (l_upper_bound)
13105                   --l_upper_bound;
13106               }
13107           }
13108         if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
13109                                              r_upper_bound))
13110           {
13111             uint64_t r_count = 0;
13112             if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
13113               {
13114                 r_upper_bound = r_lower_bound + r_count;
13115                 if (r_upper_bound)
13116                   --r_upper_bound;
13117               }
13118           }
13119
13120         if ((l_lower_bound != r_lower_bound)
13121             || (l_upper_bound != r_upper_bound))
13122           result = false;
13123       }
13124       break;
13125
13126     case DW_TAG_subroutine_type:
13127     case DW_TAG_subprogram:
13128       {
13129         interned_string ln = ctxt.get_die_pretty_type_representation(l, 0);
13130         interned_string rn = ctxt.get_die_pretty_type_representation(r, 0);
13131
13132         if ((aggregates_being_compared.find(ln)
13133              != aggregates_being_compared.end())
13134             || (aggregates_being_compared.find(rn)
13135                 != aggregates_being_compared.end()))
13136           result = true;
13137       else if (l_tag == DW_TAG_subroutine_type)
13138         {
13139           // The string reprs of l and r are already equal.  Now let's
13140           // just check if they both come from the same TU.
13141           bool from_the_same_tu = false;
13142           if (compare_dies_cu_decl_file(l, r, from_the_same_tu)
13143               && from_the_same_tu)
13144             result = true;
13145         }
13146       else
13147         {
13148           if (!fn_die_equal_by_linkage_name(ctxt, l, r))
13149             {
13150               result = false;
13151               break;
13152             }
13153
13154           if (!ctxt.die_is_in_c(l) && !ctxt.die_is_in_c(r))
13155             {
13156               // In C, we cannot have two different functions with the
13157               // same linkage name in a given binary.  But here we are
13158               // looking at DIEs that don't originate from C.  So we
13159               // need to compare return types and parameter types.
13160               Dwarf_Die l_return_type, r_return_type;
13161               bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
13162                                                               l_return_type);
13163               bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
13164                                                               r_return_type);
13165               if (l_return_type_is_void != r_return_type_is_void
13166                   || (!l_return_type_is_void
13167                       && !compare_dies(ctxt,
13168                                        &l_return_type, &r_return_type,
13169                                        aggregates_being_compared,
13170                                        update_canonical_dies_on_the_fly)))
13171                 result = false;
13172               else
13173                 {
13174                   Dwarf_Die l_child, r_child;
13175                   bool found_l_child, found_r_child;
13176                   for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
13177                                                    &l_child) == 0,
13178                          found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
13179                                                      &r_child) == 0;
13180                        found_l_child && found_r_child;
13181                        found_l_child = dwarf_siblingof(&l_child,
13182                                                        &l_child) == 0,
13183                          found_r_child = dwarf_siblingof(&r_child,
13184                                                          &r_child)==0)
13185                     {
13186                       int l_child_tag = dwarf_tag(&l_child);
13187                       int r_child_tag = dwarf_tag(&r_child);
13188                       if (l_child_tag != r_child_tag
13189                           || (l_child_tag == DW_TAG_formal_parameter
13190                               && !compare_dies(ctxt, &l_child, &r_child,
13191                                                aggregates_being_compared,
13192                                                update_canonical_dies_on_the_fly)))
13193                         {
13194                           result = false;
13195                           break;
13196                         }
13197                     }
13198                   if (found_l_child != found_r_child)
13199                     result = false;
13200                 }
13201             }
13202
13203           aggregates_being_compared.erase(ln);
13204           aggregates_being_compared.erase(rn);
13205         }
13206       }
13207       break;
13208
13209     case DW_TAG_formal_parameter:
13210       {
13211         Dwarf_Die l_type, r_type;
13212         bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
13213         bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
13214         if ((l_type_is_void != r_type_is_void)
13215             || !compare_dies(ctxt, &l_type, &r_type,
13216                              aggregates_being_compared,
13217                              update_canonical_dies_on_the_fly))
13218           result = false;
13219       }
13220       break;
13221
13222     case DW_TAG_variable:
13223     case DW_TAG_member:
13224       if (compare_as_decl_dies(l, r))
13225         {
13226           // Compare the offsets of the data members
13227           if (l_tag == DW_TAG_member)
13228             {
13229               int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
13230               die_member_offset(ctxt, l, l_offset_in_bits);
13231               die_member_offset(ctxt, r, r_offset_in_bits);
13232               if (l_offset_in_bits != r_offset_in_bits)
13233                 result = false;
13234             }
13235           if (result)
13236             {
13237               // Compare the types of the data members or variables.
13238               Dwarf_Die l_type, r_type;
13239               ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
13240               ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
13241               if (aggregates_being_compared.size () < 5)
13242                 {
13243                   if (!compare_dies(ctxt, &l_type, &r_type,
13244                                     aggregates_being_compared,
13245                                     update_canonical_dies_on_the_fly))
13246                     result = false;
13247                 }
13248               else
13249                 {
13250                   if (!compare_as_type_dies(&l_type, &r_type)
13251                       ||!compare_as_decl_dies(&l_type, &r_type))
13252                     return false;
13253                 }
13254             }
13255         }
13256       else
13257         result = false;
13258       break;
13259
13260     case DW_TAG_class_type:
13261     case DW_TAG_enumerator:
13262     case DW_TAG_packed_type:
13263     case DW_TAG_set_type:
13264     case DW_TAG_file_type:
13265     case DW_TAG_ptr_to_member_type:
13266     case DW_TAG_thrown_type:
13267     case DW_TAG_interface_type:
13268     case DW_TAG_unspecified_type:
13269     case DW_TAG_shared_type:
13270     case DW_TAG_compile_unit:
13271     case DW_TAG_namespace:
13272     case DW_TAG_module:
13273     case DW_TAG_constant:
13274     case DW_TAG_partial_unit:
13275     case DW_TAG_imported_unit:
13276     case DW_TAG_dwarf_procedure:
13277     case DW_TAG_imported_declaration:
13278     case DW_TAG_entry_point:
13279     case DW_TAG_label:
13280     case DW_TAG_lexical_block:
13281     case DW_TAG_unspecified_parameters:
13282     case DW_TAG_variant:
13283     case DW_TAG_common_block:
13284     case DW_TAG_common_inclusion:
13285     case DW_TAG_inheritance:
13286     case DW_TAG_inlined_subroutine:
13287     case DW_TAG_with_stmt:
13288     case DW_TAG_access_declaration:
13289     case DW_TAG_catch_block:
13290     case DW_TAG_friend:
13291     case DW_TAG_namelist:
13292     case DW_TAG_namelist_item:
13293     case DW_TAG_template_type_parameter:
13294     case DW_TAG_template_value_parameter:
13295     case DW_TAG_try_block:
13296     case DW_TAG_variant_part:
13297     case DW_TAG_imported_module:
13298     case DW_TAG_condition:
13299     case DW_TAG_type_unit:
13300     case DW_TAG_template_alias:
13301     case DW_TAG_lo_user:
13302     case DW_TAG_MIPS_loop:
13303     case DW_TAG_format_label:
13304     case DW_TAG_function_template:
13305     case DW_TAG_class_template:
13306     case DW_TAG_GNU_BINCL:
13307     case DW_TAG_GNU_EINCL:
13308     case DW_TAG_GNU_template_template_param:
13309     case DW_TAG_GNU_template_parameter_pack:
13310     case DW_TAG_GNU_formal_parameter_pack:
13311     case DW_TAG_GNU_call_site:
13312     case DW_TAG_GNU_call_site_parameter:
13313     case DW_TAG_hi_user:
13314       ABG_ASSERT_NOT_REACHED;
13315     }
13316
13317   if (result == true
13318       && update_canonical_dies_on_the_fly
13319       && is_canonicalizeable_type_tag(l_tag))
13320     {
13321       // If 'l' has no canonical DIE and if 'r' has one, then propagage
13322       // the canonical DIE of 'r' to 'l'.
13323       //
13324       // In case 'r' has no canonical DIE, then compute it, and then
13325       // propagate that canonical DIE to 'r'.
13326       die_source l_source = NO_DEBUG_INFO_DIE_SOURCE,
13327         r_source = NO_DEBUG_INFO_DIE_SOURCE;
13328       ABG_ASSERT(ctxt.get_die_source(l, l_source));
13329       ABG_ASSERT(ctxt.get_die_source(r, r_source));
13330       if (!l_has_canonical_die_offset
13331           // A DIE can be equivalent only to another DIE of the same
13332           // source.
13333           && l_source == r_source)
13334         {
13335           if (!r_has_canonical_die_offset)
13336             ctxt.compute_canonical_die_offset(r, r_canonical_die_offset,
13337                                               /*die_as_type=*/true);
13338           ABG_ASSERT(r_canonical_die_offset);
13339           ctxt.set_canonical_die_offset(l, r_canonical_die_offset,
13340                                         /*die_as_type=*/true);
13341         }
13342     }
13343   return result;
13344 }
13345
13346 /// Compare two DIEs emitted by a C compiler.
13347 ///
13348 /// @param ctxt the read context used to load the DWARF information.
13349 ///
13350 /// @param l the left-hand-side argument of this comparison operator.
13351 ///
13352 /// @param r the righ-hand-side argument of this comparison operator.
13353 ///
13354 /// @param update_canonical_dies_on_the_fly if yes, then this function
13355 /// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
13356 /// comparing l and r.  This helps in making so that sub-type DIEs of
13357 /// 'l' and 'r' are compared structurally only once.  This is how we
13358 /// turn this exponential comparison problem into a problem that is a
13359 /// closer to a linear one.
13360 ///
13361 /// @return true iff @p l equals @p r.
13362 static bool
13363 compare_dies(const read_context& ctxt,
13364              const Dwarf_Die *l,
13365              const Dwarf_Die *r,
13366              bool update_canonical_dies_on_the_fly)
13367 {
13368   istring_set_type aggregates_being_compared;
13369   return compare_dies(ctxt, l, r, aggregates_being_compared,
13370                       update_canonical_dies_on_the_fly);
13371 }
13372
13373 // ----------------------------------
13374 // </die comparison engine>
13375 // ---------------------------------
13376
13377 /// Get the point where a DW_AT_import DIE is used to import a given
13378 /// (unit) DIE, between two DIEs.
13379 ///
13380 /// @param ctxt the dwarf reading context to consider.
13381 ///
13382 /// @param partial_unit_offset the imported unit for which we want to
13383 /// know the insertion point.  This is usually a partial unit (with
13384 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
13385 /// so.
13386 ///
13387 /// @param first_die_offset the offset of the DIE from which this
13388 /// function starts looking for the import point of
13389 /// @partial_unit_offset.  Note that this offset is excluded from the
13390 /// set of potential solutions.
13391 ///
13392 /// @param first_die_cu_offset the offset of the (compilation) unit
13393 /// that @p first_die_cu_offset belongs to.
13394 ///
13395 /// @param source where the DIE of first_die_cu_offset unit comes
13396 /// from.
13397 ///
13398 /// @param last_die_offset the offset of the last DIE of the up to
13399 /// which this function looks for the import point of @p
13400 /// partial_unit_offset.  Note that this offset is excluded from the
13401 /// set of potential solutions.
13402 ///
13403 /// @param imported_point_offset.  The resulting
13404 /// imported_point_offset.  Note that if the imported DIE @p
13405 /// partial_unit_offset is not found between @p first_die_offset and
13406 /// @p last_die_offset, this parameter is left untouched by this
13407 /// function.
13408 ///
13409 /// @return true iff an imported unit is found between @p
13410 /// first_die_offset and @p last_die_offset.
13411 static bool
13412 find_import_unit_point_between_dies(const read_context& ctxt,
13413                                     size_t              partial_unit_offset,
13414                                     Dwarf_Off           first_die_offset,
13415                                     Dwarf_Off           first_die_cu_offset,
13416                                     die_source          source,
13417                                     size_t              last_die_offset,
13418                                     size_t&             imported_point_offset)
13419 {
13420   const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
13421     ctxt.tu_die_imported_unit_points_map(source);
13422
13423   tu_die_imported_unit_points_map_type::const_iterator iter =
13424     tu_die_imported_unit_points_map.find(first_die_cu_offset);
13425
13426   ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
13427
13428   const imported_unit_points_type& imported_unit_points = iter->second;
13429   if (imported_unit_points.empty())
13430     return false;
13431
13432   imported_unit_points_type::const_iterator b = imported_unit_points.begin();
13433   imported_unit_points_type::const_iterator e = imported_unit_points.end();
13434
13435   find_lower_bound_in_imported_unit_points(imported_unit_points,
13436                                            first_die_offset,
13437                                            b);
13438
13439   if (last_die_offset != static_cast<size_t>(-1))
13440     find_lower_bound_in_imported_unit_points(imported_unit_points,
13441                                              last_die_offset,
13442                                              e);
13443
13444   if (e != imported_unit_points.end())
13445     {
13446       for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
13447         if (i->imported_unit_die_off == partial_unit_offset)
13448           {
13449             imported_point_offset = i->offset_of_import ;
13450             return true;
13451           }
13452
13453       for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
13454         {
13455           if (find_import_unit_point_between_dies(ctxt,
13456                                                   partial_unit_offset,
13457                                                   i->imported_unit_child_off,
13458                                                   i->imported_unit_cu_off,
13459                                                   i->imported_unit_die_source,
13460                                                   /*(Dwarf_Off)*/-1,
13461                                                   imported_point_offset))
13462             return true;
13463         }
13464     }
13465   else
13466     {
13467       for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
13468         if (i->imported_unit_die_off == partial_unit_offset)
13469           {
13470             imported_point_offset = i->offset_of_import ;
13471             return true;
13472           }
13473
13474       for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
13475         {
13476           if (find_import_unit_point_between_dies(ctxt,
13477                                                   partial_unit_offset,
13478                                                   i->imported_unit_child_off,
13479                                                   i->imported_unit_cu_off,
13480                                                   i->imported_unit_die_source,
13481                                                   /*(Dwarf_Off)*/-1,
13482                                                   imported_point_offset))
13483             return true;
13484         }
13485     }
13486
13487   return false;
13488 }
13489
13490 /// In the current translation unit, get the last point where a
13491 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
13492 /// given DIE is found.  That given DIE is called the limit DIE.
13493 ///
13494 /// Said otherwise, this function returns the last import point of a
13495 /// unit, before a limit.
13496 ///
13497 /// @param ctxt the dwarf reading context to consider.
13498 ///
13499 /// @param partial_unit_offset the imported unit for which we want to
13500 /// know the insertion point of.  This is usually a partial unit (with
13501 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
13502 /// so.
13503 ///
13504 /// @param where_offset the offset of the limit DIE.
13505 ///
13506 /// @param imported_point_offset.  The resulting imported_point_offset.
13507 /// Note that if the imported DIE @p partial_unit_offset is not found
13508 /// before @p die_offset, this is set to the last @p
13509 /// partial_unit_offset found under @p parent_die.
13510 ///
13511 /// @return true iff an imported unit is found before @p die_offset.
13512 /// Note that if an imported unit is found after @p die_offset then @p
13513 /// imported_point_offset is set and the function return false.
13514 static bool
13515 find_import_unit_point_before_die(const read_context&   ctxt,
13516                                   size_t                partial_unit_offset,
13517                                   size_t                where_offset,
13518                                   size_t&               imported_point_offset)
13519 {
13520   size_t import_point_offset = 0;
13521   Dwarf_Die first_die_of_tu;
13522
13523   if (dwarf_child(const_cast<Dwarf_Die*>(ctxt.cur_tu_die()),
13524                   &first_die_of_tu) != 0)
13525     return false;
13526
13527   Dwarf_Die cu_die_memory;
13528   Dwarf_Die *cu_die;
13529
13530   cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
13531                        &cu_die_memory, 0, 0);
13532
13533   if (find_import_unit_point_between_dies(ctxt, partial_unit_offset,
13534                                           dwarf_dieoffset(&first_die_of_tu),
13535                                           dwarf_dieoffset(cu_die),
13536                                           /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
13537                                           where_offset,
13538                                           import_point_offset))
13539     {
13540       imported_point_offset = import_point_offset;
13541       return true;
13542     }
13543
13544   if (import_point_offset)
13545     {
13546       imported_point_offset = import_point_offset;
13547       return true;
13548     }
13549
13550   return false;
13551 }
13552
13553 /// Return the parent DIE for a given DIE.
13554 ///
13555 /// Note that the function build_die_parent_map() must have been
13556 /// called before this one can work.  This function either succeeds or
13557 /// aborts the current process.
13558 ///
13559 /// @param ctxt the read context to consider.
13560 ///
13561 /// @param die the DIE for which we want the parent.
13562 ///
13563 /// @param parent_die the output parameter set to the parent die of
13564 /// @p die.  Its memory must be allocated and handled by the caller.
13565 ///
13566 /// @param where_offset the offset of the DIE where we are "logically"
13567 /// positionned at, in the DIE tree.  This is useful when @p die is
13568 /// e.g, DW_TAG_partial_unit that can be included in several places in
13569 /// the DIE tree.
13570 ///
13571 /// @return true if the function could get a parent DIE, false
13572 /// otherwise.
13573 static bool
13574 get_parent_die(const read_context&      ctxt,
13575                const Dwarf_Die* die,
13576                Dwarf_Die&               parent_die,
13577                size_t                   where_offset)
13578 {
13579   ABG_ASSERT(ctxt.dwarf());
13580
13581   die_source source = NO_DEBUG_INFO_DIE_SOURCE;
13582   ABG_ASSERT(ctxt.get_die_source(die, source));
13583
13584   const offset_offset_map_type& m = ctxt.die_parent_map(source);
13585   offset_offset_map_type::const_iterator i =
13586     m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
13587
13588   if (i == m.end())
13589     return false;
13590
13591   switch (source)
13592     {
13593     case PRIMARY_DEBUG_INFO_DIE_SOURCE:
13594       ABG_ASSERT(dwarf_offdie(ctxt.dwarf(), i->second, &parent_die));
13595       break;
13596     case ALT_DEBUG_INFO_DIE_SOURCE:
13597       ABG_ASSERT(dwarf_offdie(ctxt.alt_dwarf(), i->second, &parent_die));
13598       break;
13599     case TYPE_UNIT_DIE_SOURCE:
13600       ABG_ASSERT(dwarf_offdie_types(ctxt.dwarf(), i->second, &parent_die));
13601       break;
13602     case NO_DEBUG_INFO_DIE_SOURCE:
13603     case NUMBER_OF_DIE_SOURCES:
13604       ABG_ASSERT_NOT_REACHED;
13605     }
13606
13607   if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
13608     {
13609       if (where_offset == 0)
13610         {
13611           parent_die = *ctxt.cur_tu_die();
13612           return true;
13613         }
13614       size_t import_point_offset = 0;
13615       bool found =
13616         find_import_unit_point_before_die(ctxt,
13617                                           dwarf_dieoffset(&parent_die),
13618                                           where_offset,
13619                                           import_point_offset);
13620       if (!found)
13621         // It looks like parent_die (which comes from the alternate
13622         // debug info file) hasn't been imported into this TU.  So,
13623         // Let's assume its logical parent is the DIE of the current
13624         // TU.
13625         parent_die = *ctxt.cur_tu_die();
13626       else
13627         {
13628           ABG_ASSERT(import_point_offset);
13629           Dwarf_Die import_point_die;
13630           ABG_ASSERT(dwarf_offdie(ctxt.dwarf(),
13631                               import_point_offset,
13632                               &import_point_die));
13633           return get_parent_die(ctxt, &import_point_die,
13634                                 parent_die, where_offset);
13635         }
13636     }
13637
13638   return true;
13639 }
13640
13641 /// Get the DIE representing the scope of a given DIE.
13642 ///
13643 /// Please note that when the DIE we are looking at has a
13644 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
13645 /// DIE is the parent DIE of the DIE referred to by that attribute.
13646 /// This is the only case where a scope DIE is different from the
13647 /// parent DIE of a given DIE.
13648 ///
13649 /// Also note that if the current translation unit is from C, then
13650 /// this returns the global scope.
13651 ///
13652 /// @param ctxt the reading context to use.
13653 ///
13654 /// @param die the DIE to consider.
13655 ///
13656 /// @param where_offset where we are logically at in the DIE stream.
13657 ///
13658 /// @param scope_die out parameter.  This is set to the resulting
13659 /// scope DIE iff the function returns true.
13660 static bool
13661 get_scope_die(const read_context&       ctxt,
13662               const Dwarf_Die*          die,
13663               size_t                    where_offset,
13664               Dwarf_Die&                scope_die)
13665 {
13666   if (is_c_language(ctxt.cur_transl_unit()->get_language()))
13667     {
13668       ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
13669       return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
13670     }
13671
13672   Dwarf_Die logical_parent_die;
13673   if (die_die_attribute(die, DW_AT_specification,
13674                         logical_parent_die, false)
13675       || die_die_attribute(die, DW_AT_abstract_origin,
13676                            logical_parent_die, false))
13677     return get_scope_die(ctxt, &logical_parent_die, where_offset, scope_die);
13678
13679   if (!get_parent_die(ctxt, die, scope_die, where_offset))
13680     return false;
13681
13682   if (dwarf_tag(&scope_die) == DW_TAG_subprogram
13683       || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
13684       || dwarf_tag(&scope_die) == DW_TAG_array_type)
13685     return get_scope_die(ctxt, &scope_die, where_offset, scope_die);
13686
13687   return true;
13688 }
13689
13690 /// Return the abigail IR node representing the scope of a given DIE.
13691 ///
13692 /// Note that it is the logical scope that is returned.  That is, if
13693 /// the DIE has a DW_AT_specification or DW_AT_abstract_origin
13694 /// attribute, it's the scope of the referred-to DIE (via these
13695 /// attributes) that is returned.
13696 ///
13697 /// Also note that if the current translation unit is from C, then
13698 /// this returns the global scope.
13699 ///
13700 /// @param ctxt the dwarf reading context to use.
13701 ///
13702 /// @param die the DIE to get the scope for.
13703 ///
13704 /// @param called_from_public_decl is true if this function has been
13705 /// initially called within the context of a public decl.
13706 ///
13707 /// @param where_offset the offset of the DIE where we are "logically"
13708 /// positionned at, in the DIE tree.  This is useful when @p die is
13709 /// e.g, DW_TAG_partial_unit that can be included in several places in
13710 /// the DIE tree.
13711 static scope_decl_sptr
13712 get_scope_for_die(read_context& ctxt,
13713                   Dwarf_Die*    die,
13714                   bool          called_for_public_decl,
13715                   size_t        where_offset)
13716 {
13717   die_source source_of_die;
13718   ABG_ASSERT(ctxt.get_die_source(die, source_of_die));
13719
13720   if (is_c_language(ctxt.cur_transl_unit()->get_language()))
13721     {
13722       ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
13723       return ctxt.global_scope();
13724     }
13725
13726   Dwarf_Die cloned_die;
13727   if (die_die_attribute(die, DW_AT_specification, cloned_die, false)
13728       || die_die_attribute(die, DW_AT_abstract_origin, cloned_die, false))
13729     return get_scope_for_die(ctxt, &cloned_die,
13730                              called_for_public_decl,
13731                              where_offset);
13732
13733   Dwarf_Die parent_die;
13734
13735   if (!get_parent_die(ctxt, die, parent_die, where_offset))
13736     return ctxt.nil_scope();
13737
13738   if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
13739       || dwarf_tag(&parent_die) == DW_TAG_partial_unit
13740       || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13741     {
13742       if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
13743           || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13744         {
13745           ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
13746                  || source_of_die == TYPE_UNIT_DIE_SOURCE);
13747           return ctxt.cur_transl_unit()->get_global_scope();
13748         }
13749
13750       // For top level DIEs like DW_TAG_compile_unit, we just want to
13751       // return the global scope for the corresponding translation
13752       // unit.  This must have been set by
13753       // build_translation_unit_and_add_to_ir if we already started to
13754       // build the translation unit of parent_die.  Otherwise, just
13755       // return the global scope of the current translation unit.
13756       die_tu_map_type::const_iterator i =
13757         ctxt.die_tu_map().find(dwarf_dieoffset(&parent_die));
13758       if (i != ctxt.die_tu_map().end())
13759         return i->second->get_global_scope();
13760       return ctxt.cur_transl_unit()->get_global_scope();
13761     }
13762
13763   scope_decl_sptr s;
13764   type_or_decl_base_sptr d;
13765   if (dwarf_tag(&parent_die) == DW_TAG_subprogram
13766       || dwarf_tag(&parent_die) == DW_TAG_array_type)
13767     // this is an entity defined in a scope that is a function.
13768     // Normally, I would say that this should be dropped.  But I have
13769     // seen a case where a typedef DIE needed by a function parameter
13770     // was defined right before the parameter, under the scope of the
13771     // function.  Yeah, weird.  So if I drop the typedef DIE, I'd drop
13772     // the function parm too.  So for that case, let's say that the
13773     // scope is the scope of the function itself.  Note that this is
13774     // an error of the DWARF emitter.  We should never see this DIE in
13775     // this context.
13776     {
13777       scope_decl_sptr s = get_scope_for_die(ctxt, &parent_die,
13778                                             called_for_public_decl,
13779                                             where_offset);
13780       if (is_anonymous_type_die(die))
13781         // For anonymous type that have nothing to do in a function or
13782         // array type context, let's put it in the containing
13783         // namespace.  That is, do not let it be in a containing class
13784         // or union where it has nothing to do.
13785         while (is_class_or_union_type(s))
13786           {
13787             if (!get_parent_die(ctxt, &parent_die, parent_die, where_offset))
13788               return ctxt.nil_scope();
13789             s = get_scope_for_die(ctxt, &parent_die,
13790                                   called_for_public_decl,
13791                                   where_offset);
13792           }
13793       return s;
13794     }
13795   else
13796     d = build_ir_node_from_die(ctxt, &parent_die,
13797                                called_for_public_decl,
13798                                where_offset);
13799   s =  dynamic_pointer_cast<scope_decl>(d);
13800   if (!s)
13801     // this is an entity defined in someting that is not a scope.
13802     // Let's drop it.
13803     return ctxt.nil_scope();
13804
13805   class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
13806   if (cl && cl->get_is_declaration_only())
13807     {
13808       scope_decl_sptr scop (cl->get_definition_of_declaration());
13809       if (scop)
13810         s = scop;
13811       else
13812         s = cl;
13813     }
13814   return s;
13815 }
13816
13817 /// Convert a DWARF constant representing the value of the
13818 /// DW_AT_language property into the translation_unit::language
13819 /// enumerator.
13820 ///
13821 /// @param l the DWARF constant to convert.
13822 ///
13823 /// @return the resulting translation_unit::language enumerator.
13824 static translation_unit::language
13825 dwarf_language_to_tu_language(size_t l)
13826 {
13827   switch (l)
13828     {
13829     case DW_LANG_C89:
13830       return translation_unit::LANG_C89;
13831     case DW_LANG_C:
13832       return translation_unit::LANG_C;
13833     case DW_LANG_Ada83:
13834       return translation_unit::LANG_Ada83;
13835     case DW_LANG_C_plus_plus:
13836       return translation_unit::LANG_C_plus_plus;
13837     case DW_LANG_Cobol74:
13838       return translation_unit::LANG_Cobol74;
13839     case DW_LANG_Cobol85:
13840       return translation_unit::LANG_Cobol85;
13841     case DW_LANG_Fortran77:
13842       return translation_unit::LANG_Fortran77;
13843     case DW_LANG_Fortran90:
13844       return translation_unit::LANG_Fortran90;
13845     case DW_LANG_Pascal83:
13846       return translation_unit::LANG_Pascal83;
13847     case DW_LANG_Modula2:
13848       return translation_unit::LANG_Modula2;
13849     case DW_LANG_Java:
13850       return translation_unit::LANG_Java;
13851     case DW_LANG_C99:
13852       return translation_unit::LANG_C99;
13853     case DW_LANG_Ada95:
13854       return translation_unit::LANG_Ada95;
13855     case DW_LANG_Fortran95:
13856       return translation_unit::LANG_Fortran95;
13857     case DW_LANG_PL1:
13858       return translation_unit::LANG_PL1;
13859     case DW_LANG_ObjC:
13860       return translation_unit::LANG_ObjC;
13861     case DW_LANG_ObjC_plus_plus:
13862       return translation_unit::LANG_ObjC_plus_plus;
13863
13864 #ifdef HAVE_DW_LANG_Rust_enumerator
13865     case DW_LANG_Rust:
13866       return translation_unit::LANG_Rust;
13867 #endif
13868
13869 #ifdef HAVE_DW_LANG_UPC_enumerator
13870     case DW_LANG_UPC:
13871       return translation_unit::LANG_UPC;
13872 #endif
13873
13874 #ifdef HAVE_DW_LANG_D_enumerator
13875     case DW_LANG_D:
13876       return translation_unit::LANG_D;
13877 #endif
13878
13879 #ifdef HAVE_DW_LANG_Python_enumerator
13880     case DW_LANG_Python:
13881       return translation_unit::LANG_Python;
13882 #endif
13883
13884 #ifdef HAVE_DW_LANG_Go_enumerator
13885     case DW_LANG_Go:
13886       return translation_unit::LANG_Go;
13887 #endif
13888
13889 #ifdef HAVE_DW_LANG_C11_enumerator
13890     case DW_LANG_C11:
13891       return translation_unit::LANG_C11;
13892 #endif
13893
13894 #ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
13895       case DW_LANG_C_plus_plus_03:
13896         return translation_unit::LANG_C_plus_plus_03;
13897 #endif
13898
13899 #ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
13900     case DW_LANG_C_plus_plus_11:
13901       return translation_unit::LANG_C_plus_plus_11;
13902 #endif
13903
13904 #ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
13905     case DW_LANG_C_plus_plus_14:
13906       return translation_unit::LANG_C_plus_plus_14;
13907 #endif
13908
13909 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
13910     case DW_LANG_Mips_Assembler:
13911       return translation_unit::LANG_Mips_Assembler;
13912 #endif
13913
13914     default:
13915       return translation_unit::LANG_UNKNOWN;
13916     }
13917 }
13918
13919 /// Get the default array lower bound value as defined by the DWARF
13920 /// specification, version 4, depending on the language of the
13921 /// translation unit.
13922 ///
13923 /// @param l the language of the translation unit.
13924 ///
13925 /// @return the default array lower bound value.
13926 static uint64_t
13927 get_default_array_lower_bound(translation_unit::language l)
13928 {
13929   int value = 0;
13930   switch (l)
13931     {
13932     case translation_unit::LANG_UNKNOWN:
13933       value = 0;
13934       break;
13935     case translation_unit::LANG_Cobol74:
13936     case translation_unit::LANG_Cobol85:
13937       value = 1;
13938       break;
13939     case translation_unit::LANG_C89:
13940     case translation_unit::LANG_C99:
13941     case translation_unit::LANG_C11:
13942     case translation_unit::LANG_C:
13943     case translation_unit::LANG_C_plus_plus_03:
13944     case translation_unit::LANG_C_plus_plus_11:
13945     case translation_unit::LANG_C_plus_plus_14:
13946     case translation_unit::LANG_C_plus_plus:
13947     case translation_unit::LANG_ObjC:
13948     case translation_unit::LANG_ObjC_plus_plus:
13949     case translation_unit::LANG_Rust:
13950       value = 0;
13951       break;
13952     case translation_unit::LANG_Fortran77:
13953     case translation_unit::LANG_Fortran90:
13954     case translation_unit::LANG_Fortran95:
13955     case translation_unit::LANG_Ada83:
13956     case translation_unit::LANG_Ada95:
13957     case translation_unit::LANG_Pascal83:
13958     case translation_unit::LANG_Modula2:
13959       value = 1;
13960       break;
13961     case translation_unit::LANG_Java:
13962       value = 0;
13963       break;
13964     case translation_unit::LANG_PL1:
13965       value = 1;
13966       break;
13967     case translation_unit::LANG_UPC:
13968     case translation_unit::LANG_D:
13969     case translation_unit::LANG_Python:
13970     case translation_unit::LANG_Go:
13971     case translation_unit::LANG_Mips_Assembler:
13972       value = 0;
13973       break;
13974     }
13975
13976   return value;
13977 }
13978
13979 /// For a given offset, find the lower bound of a sorted vector of
13980 /// imported unit point offset.
13981 ///
13982 /// The lower bound is the smallest point (the point with the smallest
13983 /// offset) which is the greater than a given offset.
13984 ///
13985 /// @param imported_unit_points_type the sorted vector  of imported
13986 /// unit points.
13987 ///
13988 /// @param val the offset to consider when looking for the lower
13989 /// bound.
13990 ///
13991 /// @param r an iterator to the lower bound found.  This parameter is
13992 /// set iff the function returns true.
13993 ///
13994 /// @return true iff the lower bound has been found.
13995 static bool
13996 find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
13997                                          Dwarf_Off val,
13998                                          imported_unit_points_type::const_iterator& r)
13999 {
14000   imported_unit_point v(val);
14001   imported_unit_points_type::const_iterator result =
14002     std::lower_bound(p.begin(), p.end(), v);
14003
14004   bool is_ok = result != p.end();
14005
14006   if (is_ok)
14007     r = result;
14008
14009   return is_ok;
14010 }
14011
14012 /// Given a DW_TAG_compile_unit, build and return the corresponding
14013 /// abigail::translation_unit ir node.  Note that this function
14014 /// recursively reads the children dies of the current DIE and
14015 /// populates the resulting translation unit.
14016 ///
14017 /// @param ctxt the read_context to use.
14018 ///
14019 /// @param die the DW_TAG_compile_unit DIE to consider.
14020 ///
14021 /// @param address_size the size of the addresses expressed in this
14022 /// translation unit in general.
14023 ///
14024 /// @return a pointer to the resulting translation_unit.
14025 static translation_unit_sptr
14026 build_translation_unit_and_add_to_ir(read_context&      ctxt,
14027                                      Dwarf_Die* die,
14028                                      char               address_size)
14029 {
14030   translation_unit_sptr result;
14031
14032   if (!die)
14033     return result;
14034   ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
14035
14036   // Clear the part of the context that is dependent on the translation
14037   // unit we are reading.
14038   ctxt.clear_per_translation_unit_data();
14039
14040   ctxt.cur_tu_die(die);
14041
14042   string path = die_string_attribute(die, DW_AT_name);
14043   string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
14044
14045   // See if the same translation unit exits already in the current
14046   // corpus.  Sometimes, the same translation unit can be present
14047   // several times in the same debug info.  The content of the
14048   // different instances of the translation unit are different.  So to
14049   // represent that, we are going to re-use the same translation
14050   // unit.  That is, it's going to be the union of all the translation
14051   // units of the same path.
14052   {
14053     string abs_path = compilation_dir + "/" + path;
14054     result = ctxt.current_corpus()->find_translation_unit(abs_path);
14055   }
14056
14057   if (!result)
14058     {
14059       result.reset(new translation_unit(ctxt.env(),
14060                                         path,
14061                                         address_size));
14062       result->set_compilation_dir_path(compilation_dir);
14063       ctxt.current_corpus()->add(result);
14064       uint64_t l = 0;
14065       die_unsigned_constant_attribute(die, DW_AT_language, l);
14066       result->set_language(dwarf_language_to_tu_language(l));
14067     }
14068
14069   ctxt.cur_transl_unit(result);
14070   ctxt.die_tu_map()[dwarf_dieoffset(die)] = result;
14071
14072   Dwarf_Die child;
14073   if (dwarf_child(die, &child) != 0)
14074     return result;
14075
14076   result->set_is_constructed(false);
14077
14078   do
14079     build_ir_node_from_die(ctxt, &child,
14080                            die_is_public_decl(&child),
14081                            dwarf_dieoffset(&child));
14082   while (dwarf_siblingof(&child, &child) == 0);
14083
14084   if (!ctxt.var_decls_to_re_add_to_tree().empty())
14085     for (list<var_decl_sptr>::const_iterator v =
14086            ctxt.var_decls_to_re_add_to_tree().begin();
14087          v != ctxt.var_decls_to_re_add_to_tree().end();
14088          ++v)
14089       {
14090         if (is_member_decl(*v))
14091           continue;
14092
14093         ABG_ASSERT((*v)->get_scope());
14094         string demangled_name =
14095           demangle_cplus_mangled_name((*v)->get_linkage_name());
14096         if (!demangled_name.empty())
14097           {
14098             std::list<string> fqn_comps;
14099             fqn_to_components(demangled_name, fqn_comps);
14100             string mem_name = fqn_comps.back();
14101             fqn_comps.pop_back();
14102             class_decl_sptr class_type;
14103             string ty_name;
14104             if (!fqn_comps.empty())
14105               {
14106                 ty_name = components_to_type_name(fqn_comps);
14107                 class_type =
14108                   lookup_class_type(ty_name, *ctxt.cur_transl_unit());
14109               }
14110             if (class_type)
14111               {
14112                 // So we are seeing a member variable for which there
14113                 // is a global variable definition DIE not having a
14114                 // reference attribute pointing back to the member
14115                 // variable declaration DIE.  Thus remove the global
14116                 // variable definition from its current non-class
14117                 // scope ...
14118                 decl_base_sptr d;
14119                 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
14120                   // This is the data member with the same name in cl.
14121                   // We just need to flag it as static.
14122                   ;
14123                 else
14124                   {
14125                     // In this case there is no data member with the
14126                     // same name in cl already.  Let's add it there then
14127                     // ...
14128                     remove_decl_from_scope(*v);
14129                     d = add_decl_to_scope(*v, class_type);
14130                   }
14131
14132                 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
14133                 // Let's flag the data member as static.
14134                 set_member_is_static(d, true);
14135               }
14136           }
14137       }
14138   ctxt.var_decls_to_re_add_to_tree().clear();
14139
14140   result->set_is_constructed(true);
14141
14142   return result;
14143 }
14144
14145 /// Build a abigail::namespace_decl out of a DW_TAG_namespace or
14146 /// DW_TAG_module (for fortran) DIE.
14147 ///
14148 /// Note that this function connects the DW_TAG_namespace to the IR
14149 /// being currently created, reads the children of the DIE and
14150 /// connects them to the IR as well.
14151 ///
14152 /// @param ctxt the read context to use.
14153 ///
14154 /// @param die the DIE to read from.  Must be either DW_TAG_namespace
14155 /// or DW_TAG_module.
14156 ///
14157 /// @param where_offset the offset of the DIE where we are "logically"
14158 /// positionned at, in the DIE tree.  This is useful when @p die is
14159 /// e.g, DW_TAG_partial_unit that can be included in several places in
14160 /// the DIE tree.
14161 ///
14162 /// @return the resulting @ref abigail::namespace_decl or NULL if it
14163 /// couldn't be created.
14164 static namespace_decl_sptr
14165 build_namespace_decl_and_add_to_ir(read_context&        ctxt,
14166                                    Dwarf_Die*           die,
14167                                    size_t               where_offset)
14168 {
14169   namespace_decl_sptr result;
14170
14171   if (!die)
14172     return result;
14173
14174   die_source source;
14175   ABG_ASSERT(ctxt.get_die_source(die, source));
14176
14177   unsigned tag = dwarf_tag(die);
14178   if (tag != DW_TAG_namespace && tag != DW_TAG_module)
14179     return result;
14180
14181   scope_decl_sptr scope = get_scope_for_die(ctxt, die,
14182                                             /*called_for_public_decl=*/false,
14183                                             where_offset);
14184
14185   string name, linkage_name;
14186   location loc;
14187   die_loc_and_name(ctxt, die, loc, name, linkage_name);
14188
14189   result.reset(new namespace_decl(ctxt.env(), name, loc));
14190   add_decl_to_scope(result, scope.get());
14191   ctxt.associate_die_to_decl(die, result, where_offset);
14192
14193   Dwarf_Die child;
14194   if (dwarf_child(die, &child) != 0)
14195     return result;
14196
14197   ctxt.scope_stack().push(result.get());
14198   do
14199     build_ir_node_from_die(ctxt, &child,
14200                            /*called_from_public_decl=*/false,
14201                            where_offset);
14202   while (dwarf_siblingof(&child, &child) == 0);
14203   ctxt.scope_stack().pop();
14204
14205   return result;
14206 }
14207
14208 /// Build a @ref type_decl out of a DW_TAG_base_type DIE.
14209 ///
14210 /// @param ctxt the read context to use.
14211 ///
14212 /// @param die the DW_TAG_base_type to consider.
14213 ///
14214 /// @param where_offset where we are logically at in the DIE stream.
14215 ///
14216 /// @return the resulting decl_base_sptr.
14217 static type_decl_sptr
14218 build_type_decl(read_context& ctxt, Dwarf_Die* die, size_t where_offset)
14219 {
14220   type_decl_sptr result;
14221
14222   if (!die)
14223     return result;
14224   ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
14225
14226   uint64_t byte_size = 0, bit_size = 0;
14227   if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
14228     if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
14229       return result;
14230
14231   if (bit_size == 0 && byte_size != 0)
14232     // Update the bit size.
14233     bit_size = byte_size * 8;
14234
14235   string type_name, linkage_name;
14236   location loc;
14237   die_loc_and_name(ctxt, die, loc, type_name, linkage_name);
14238
14239   if (byte_size == 0)
14240     {
14241       // The size of the type is zero, that must mean that we are
14242       // looking at the definition of the void type.
14243       if (type_name == "void")
14244         result = is_type_decl(build_ir_node_for_void_type(ctxt));
14245       else
14246         // A type of size zero that is not void? Hmmh, I am not sure
14247         // what that means.  Return nil for now.
14248         return result;
14249     }
14250
14251   if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
14252     {
14253       string normalized_type_name = type_name;
14254       integral_type int_type;
14255       if (parse_integral_type(type_name, int_type))
14256         normalized_type_name = int_type.to_string();
14257       result = lookup_basic_type(normalized_type_name, *corp);
14258     }
14259
14260   if (!result)
14261     if (corpus_sptr corp = ctxt.current_corpus())
14262       result = lookup_basic_type(type_name, *corp);
14263   if (!result)
14264     result.reset(new type_decl(ctxt.env(), type_name, bit_size,
14265                                /*alignment=*/0, loc, linkage_name));
14266   ctxt.associate_die_to_type(die, result, where_offset);
14267   return result;
14268 }
14269
14270 /// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
14271 ///
14272 /// @param ctxt the read context to use.
14273 ///
14274 /// @param die the DIE to read from.
14275 ///
14276 /// @param scope the scope of the final enum.  Note that this function
14277 /// does *NOT* add the built type to this scope.  The scope is just so
14278 /// that the function knows how to name anonymous enums.
14279 ///
14280 /// @return the built enum_type_decl or NULL if it could not be built.
14281 static enum_type_decl_sptr
14282 build_enum_type(read_context&   ctxt,
14283                 Dwarf_Die*      die,
14284                 scope_decl*     scope,
14285                 size_t          where_offset)
14286 {
14287   enum_type_decl_sptr result;
14288   if (!die)
14289     return result;
14290
14291   unsigned tag = dwarf_tag(die);
14292   if (tag != DW_TAG_enumeration_type)
14293     return result;
14294
14295   string name, linkage_name;
14296   location loc;
14297   die_loc_and_name(ctxt, die, loc, name, linkage_name);
14298
14299   bool enum_is_anonymous = false;
14300   // If the enum is anonymous, let's give it a name.
14301   if (name.empty())
14302     {
14303       name = get_internal_anonymous_die_prefix_name(die);
14304       ABG_ASSERT(!name.empty());
14305       // But we remember that the type is anonymous.
14306       enum_is_anonymous = true;
14307
14308       if (size_t s = scope->get_num_anonymous_member_enums())
14309         name = build_internal_anonymous_die_name(name, s);
14310     }
14311
14312   bool use_odr = ctxt.odr_is_relevant(die);
14313   // If the type has location, then associate it to its
14314   // representation.  This way, all occurences of types with the same
14315   // representation (name) and location can be later detected as being
14316   // for the same type.
14317
14318   if (!enum_is_anonymous)
14319     {
14320       if (use_odr)
14321         {
14322           if (enum_type_decl_sptr pre_existing_enum =
14323               is_enum_type(ctxt.lookup_artifact_from_die(die)))
14324             result = pre_existing_enum;
14325         }
14326       else if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
14327         {
14328           if (loc)
14329             result = lookup_enum_type_per_location(loc.expand(), *corp);
14330         }
14331       else if (loc)
14332         {
14333           if (enum_type_decl_sptr pre_existing_enum =
14334               is_enum_type(ctxt.lookup_artifact_from_die(die)))
14335             if (pre_existing_enum->get_location() == loc)
14336               result = pre_existing_enum;
14337         }
14338
14339       if (result)
14340         {
14341           ctxt.associate_die_to_type(die, result, where_offset);
14342           return result;
14343         }
14344     }
14345   // TODO: for anonymous enums, maybe have a map of loc -> enums so that
14346   // we can look them up?
14347
14348   uint64_t size = 0;
14349   if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14350     size *= 8;
14351
14352   // for now we consider that underlying types of enums are all anonymous
14353   bool enum_underlying_type_is_anonymous= true;
14354   string underlying_type_name;
14355   if (enum_underlying_type_is_anonymous)
14356     {
14357       underlying_type_name = "unnamed-enum";
14358       enum_underlying_type_is_anonymous = true;
14359     }
14360   else
14361     underlying_type_name = string("enum-") + name;
14362   underlying_type_name += "-underlying-type";
14363
14364   enum_type_decl::enumerators enms;
14365   Dwarf_Die child;
14366   if (dwarf_child(die, &child) == 0)
14367     {
14368       do
14369         {
14370           if (dwarf_tag(&child) != DW_TAG_enumerator)
14371             continue;
14372
14373           string n, m;
14374           location l;
14375           die_loc_and_name(ctxt, &child, l, n, m);
14376           uint64_t val = 0;
14377           die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
14378           enms.push_back(enum_type_decl::enumerator(ctxt.env(), n, val));
14379         }
14380       while (dwarf_siblingof(&child, &child) == 0);
14381     }
14382
14383   // DWARF up to version 4 (at least) doesn't seem to carry the
14384   // underlying type, so let's create an artificial one here, which
14385   // sole purpose is to be passed to the constructor of the
14386   // enum_type_decl type.
14387   type_decl_sptr t(new type_decl(ctxt.env(), underlying_type_name,
14388                                  size, size, location()));
14389   t->set_is_anonymous(enum_underlying_type_is_anonymous);
14390   translation_unit_sptr tu = ctxt.cur_transl_unit();
14391   decl_base_sptr d =
14392     add_decl_to_scope(t, tu->get_global_scope().get());
14393   canonicalize(t);
14394
14395   t = dynamic_pointer_cast<type_decl>(d);
14396   ABG_ASSERT(t);
14397   result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
14398   result->set_is_anonymous(enum_is_anonymous);
14399   ctxt.associate_die_to_type(die, result, where_offset);
14400   return result;
14401 }
14402
14403 /// Once a function_decl has been built and added to a class as a
14404 /// member function, this function updates the information of the
14405 /// function_decl concerning the properties of its relationship with
14406 /// the member class.  That is, it updates properties like
14407 /// virtualness, access, constness, cdtorness, etc ...
14408 ///
14409 /// @param die the DIE of the function_decl that has been just built.
14410 ///
14411 /// @param f the function_decl that has just been built from @p die.
14412 ///
14413 /// @param klass the @ref class_or_union that @p f belongs to.
14414 ///
14415 /// @param ctxt the context used to read the ELF/DWARF information.
14416 static void
14417 finish_member_function_reading(Dwarf_Die*                 die,
14418                                const function_decl_sptr&  f,
14419                                const class_or_union_sptr& klass,
14420                                read_context&              ctxt)
14421 {
14422   ABG_ASSERT(klass);
14423
14424   method_decl_sptr m = is_method_decl(f);
14425   ABG_ASSERT(m);
14426
14427   method_type_sptr method_t = is_method_type(m->get_type());
14428   ABG_ASSERT(method_t);
14429
14430   bool is_ctor = (f->get_name() == klass->get_name());
14431   bool is_dtor = (!f->get_name().empty()
14432                   && static_cast<string>(f->get_name())[0] == '~');
14433   bool is_virtual = die_is_virtual(die);
14434   int64_t vindex = -1;
14435   if (is_virtual)
14436     die_virtual_function_index(die, vindex);
14437   access_specifier access = private_access;
14438   if (class_decl_sptr c = is_class_type(klass))
14439     if (c->is_struct())
14440       access = public_access;
14441   die_access_specifier(die, access);
14442
14443   bool is_static = false;
14444   {
14445     // Let's see if the first parameter is a pointer to an instance of
14446     // the same class type as the current class and has a
14447     // DW_AT_artificial attribute flag set.  We are not looking at
14448     // DW_AT_object_pointer (for DWARF 3) because it wasn't being
14449     // emitted in GCC 4_4, which was already DWARF 3.
14450     function_decl::parameter_sptr first_parm;
14451     if (!f->get_parameters().empty())
14452       first_parm = f->get_parameters()[0];
14453
14454     bool is_artificial =
14455       first_parm && first_parm->get_artificial();;
14456     pointer_type_def_sptr this_ptr_type;
14457     type_base_sptr other_klass;
14458
14459     if (is_artificial)
14460       this_ptr_type = is_pointer_type(first_parm->get_type());
14461     if (this_ptr_type)
14462       other_klass = this_ptr_type->get_pointed_to_type();
14463     // Sometimes, other_klass can be qualified; e.g, volatile.  In
14464     // that case, let's get the unqualified version of other_klass.
14465     if (qualified_type_def_sptr q = is_qualified_type(other_klass))
14466       other_klass = q->get_underlying_type();
14467
14468     if (other_klass
14469         && get_type_name(other_klass) == klass->get_qualified_name())
14470       ;
14471     else
14472       is_static = true;
14473   }
14474   set_member_access_specifier(m, access);
14475   if (vindex != -1)
14476     set_member_function_vtable_offset(m, vindex);
14477   set_member_function_is_virtual(m, is_virtual);
14478   set_member_is_static(m, is_static);
14479   set_member_function_is_ctor(m, is_ctor);
14480   set_member_function_is_dtor(m, is_dtor);
14481   set_member_function_is_const(m, method_t->get_is_const());
14482
14483   ABG_ASSERT(is_member_function(m));
14484
14485   if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
14486     {
14487       // This is a virtual member function which has a linkage name
14488       // but has no underlying symbol set.
14489       //
14490       // The underlying elf symbol to set to this function can show up
14491       // later in the DWARF input or it can be that, because of some
14492       // compiler optimization, the relation between this function and
14493       // its underlying elf symbol is simply not emitted in the DWARF.
14494       //
14495       // Let's thus schedule this function for a later fixup pass
14496       // (performed by
14497       // read_context::fixup_functions_with_no_symbols()) that will
14498       // set its underlying symbol.
14499       //
14500       // Note that if the underying symbol is encountered later in the
14501       // DWARF input, then the part of build_function_decl() that
14502       // updates the function to set its underlying symbol will
14503       // de-schedule this function wrt fixup pass.
14504       Dwarf_Off die_offset = dwarf_dieoffset(die);
14505       die_function_decl_map_type &fns_with_no_symbol =
14506         ctxt.die_function_decl_with_no_symbol_map();
14507       die_function_decl_map_type::const_iterator i =
14508         fns_with_no_symbol.find(die_offset);
14509       if (i == fns_with_no_symbol.end())
14510         fns_with_no_symbol[die_offset] = f;
14511     }
14512
14513 }
14514
14515 /// If a function DIE has attributes which have not yet been read and
14516 /// added to the internal representation that represents that function
14517 /// then read those extra attributes and update the internal
14518 /// representation.
14519 ///
14520 /// @param ctxt the read context to use.
14521 ///
14522 /// @param die the function DIE to consider.
14523 ///
14524 /// @param where_offset where we logical are, currently, in the stream
14525 /// of DIEs.  If you don't know what this is, you can just set it to zero.
14526 ///
14527 /// @param existing_fn the representation of the function to update.
14528 ///
14529 /// @return the updated function  representation.
14530 static function_decl_sptr
14531 maybe_finish_function_decl_reading(read_context&                ctxt,
14532                                    Dwarf_Die*                   die,
14533                                    size_t                       where_offset,
14534                                    const function_decl_sptr&    existing_fn)
14535 {
14536   function_decl_sptr result = build_function_decl(ctxt, die,
14537                                                   where_offset,
14538                                                   existing_fn);
14539
14540   return result;
14541 }
14542
14543 /// Lookup a class or a typedef with a given qualified name in the
14544 /// corpus that a given scope belongs to.
14545 ///
14546 /// @param scope the scope to consider.
14547 ///
14548 /// @param type_name the qualified name of the type to look for.
14549 ///
14550 /// @return the typedef or class type found.
14551 static type_base_sptr
14552 lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
14553 {
14554   string qname = build_qualified_name(scope, type_name);
14555   corpus* corp = scope->get_corpus();
14556   type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
14557   return result;
14558 }
14559
14560 /// Lookup a class of typedef type from the current corpus being
14561 /// constructed.
14562 ///
14563 /// The type being looked for has the same name as a given DIE.
14564 ///
14565 /// @param ctxt the reading context to use.
14566 ///
14567 /// @param die the DIE which has the same name as the type we are
14568 /// looking for.
14569 ///
14570 /// @param called_for_public_decl whether this function is being
14571 /// called from a a publicly defined declaration.
14572 ///
14573 /// @param where_offset where we are logically at in the DIE stream.
14574 ///
14575 /// @return the type found.
14576 static type_base_sptr
14577 lookup_class_or_typedef_from_corpus(read_context& ctxt,
14578                                     Dwarf_Die* die,
14579                                     bool called_for_public_decl,
14580                                     size_t where_offset)
14581 {
14582   if (!die)
14583     return class_decl_sptr();
14584
14585   string class_name = die_string_attribute(die, DW_AT_name);
14586   if (class_name.empty())
14587     return class_decl_sptr();
14588
14589   scope_decl_sptr scope = get_scope_for_die(ctxt, die,
14590                                             called_for_public_decl,
14591                                             where_offset);
14592   if (scope)
14593     return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
14594
14595   return type_base_sptr();
14596 }
14597
14598 /// Lookup a class, typedef or enum type with a given qualified name
14599 /// in the corpus that a given scope belongs to.
14600 ///
14601 /// @param scope the scope to consider.
14602 ///
14603 /// @param type_name the qualified name of the type to look for.
14604 ///
14605 /// @return the typedef, enum or class type found.
14606 static type_base_sptr
14607 lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
14608                                               const string& type_name)
14609 {
14610   string qname = build_qualified_name(scope, type_name);
14611   corpus* corp = scope->get_corpus();
14612   type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
14613   return result;
14614 }
14615
14616 /// Lookup a class, typedef or enum type in a given scope, in the
14617 /// corpus that scope belongs to.
14618 ///
14619 /// @param die the DIE of the class, typedef or enum to lookup.
14620 ///
14621 /// @param anonymous_member_type_idx if @p DIE represents an anonymous
14622 /// type, this is the index of that anonymous type in its scope, in
14623 /// case there are several anonymous types of the same kind in that
14624 /// scope.
14625 ///
14626 /// @param scope the scope in which to look the type for.
14627 ///
14628 /// @return the typedef, enum or class type found.
14629 static type_base_sptr
14630 lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
14631                                               size_t anonymous_member_type_idx,
14632                                               scope_decl* scope)
14633 {
14634   if (!die)
14635     return class_decl_sptr();
14636
14637   string type_name = die_string_attribute(die, DW_AT_name);
14638   if (is_anonymous_type_die(die))
14639     type_name =
14640       get_internal_anonymous_die_name(die, anonymous_member_type_idx);
14641
14642   if (type_name.empty())
14643     return class_decl_sptr();
14644
14645   return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
14646 }
14647
14648 /// Test if a DIE represents a function that is a member of a given
14649 /// class type.
14650 ///
14651 /// @param ctxt the reading context.
14652 ///
14653 /// @param function_die the DIE of the function to consider.
14654 ///
14655 /// @param class_type the class type to consider.
14656 ///
14657 /// @param where_offset where we are logically at in the DIE stream.
14658 ///
14659 /// @return the method declaration corresponding to the member
14660 /// function of @p class_type, iff @p function_die is for a member
14661 /// function of @p class_type.
14662 static method_decl_sptr
14663 is_function_for_die_a_member_of_class(read_context& ctxt,
14664                                       Dwarf_Die* function_die,
14665                                       const class_or_union_sptr& class_type)
14666 {
14667   type_or_decl_base_sptr artifact = ctxt.lookup_artifact_from_die(function_die);
14668
14669   if (!artifact)
14670     return method_decl_sptr();
14671
14672   method_decl_sptr method = is_method_decl(artifact);
14673   method_type_sptr method_type;
14674
14675   if (method)
14676     method_type = method->get_type();
14677   else
14678     method_type = is_method_type(artifact);
14679   ABG_ASSERT(method_type);
14680
14681   class_or_union_sptr method_class = method_type->get_class_type();
14682   ABG_ASSERT(method_class);
14683
14684   string method_class_name = method_class->get_qualified_name(),
14685     class_type_name = class_type->get_qualified_name();
14686
14687   if (method_class_name == class_type_name)
14688     {
14689       //ABG_ASSERT(class_type.get() == method_class.get());
14690       return method;
14691     }
14692
14693   return method_decl_sptr();
14694 }
14695
14696 /// If a given function DIE represents an existing member function of
14697 /// a given class, then update that member function with new
14698 /// properties present in the DIE.  Otherwise, if the DIE represents a
14699 /// new member function that is not already present in the class then
14700 /// add that new member function to the class.
14701 ///
14702 /// @param ctxt the reading context.
14703 ///
14704 /// @param function_die the DIE of the potential member function to
14705 /// consider.
14706 ///
14707 /// @param class_type the class type to consider.
14708 ///
14709 /// @param called_from_public_decl is true iff this function was
14710 /// called from a publicly defined and exported declaration.
14711 ///
14712 /// @param where_offset where we are logically at in the DIE stream.
14713 ///
14714 /// @return the method decl representing the member function.
14715 static method_decl_sptr
14716 add_or_update_member_function(read_context& ctxt,
14717                               Dwarf_Die* function_die,
14718                               const class_or_union_sptr& class_type,
14719                               bool called_from_public_decl,
14720                               size_t where_offset)
14721 {
14722   method_decl_sptr method =
14723     is_function_for_die_a_member_of_class(ctxt, function_die, class_type);
14724
14725   if (!method)
14726     method = is_method_decl(build_ir_node_from_die(ctxt, function_die,
14727                                                    class_type.get(),
14728                                                    called_from_public_decl,
14729                                                    where_offset));
14730   if (!method)
14731     return method_decl_sptr();
14732
14733   finish_member_function_reading(function_die,
14734                                  is_function_decl(method),
14735                                  class_type, ctxt);
14736   return method;
14737 }
14738
14739 /// Build a an IR node for class type from a DW_TAG_structure_type or
14740 /// DW_TAG_class_type DIE and add that node to the ABI corpus being
14741 /// currently built.
14742 ///
14743 /// If the represents class type that already exists, then update the
14744 /// existing class type with the new properties found in the DIE.
14745 ///
14746 /// It meanst that this function can also update an existing
14747 /// class_decl node with data members, member functions and other
14748 /// properties coming from the DIE.
14749 ///
14750 /// @param ctxt the read context to consider.
14751 ///
14752 /// @param die the DIE to read information from.  Must be either a
14753 /// DW_TAG_structure_type or a DW_TAG_class_type.
14754 ///
14755 /// @param scope a pointer to the scope_decl* under which this class
14756 /// is to be added to.
14757 ///
14758 /// @param is_struct whether the class was declared as a struct.
14759 ///
14760 /// @param klass if non-null, this is a klass to append the members
14761 /// to.  Otherwise, this function just builds the class from scratch.
14762 ///
14763 /// @param called_from_public_decl set to true if this class is being
14764 /// called from a "Public declaration like vars or public symbols".
14765 ///
14766 /// @param where_offset the offset of the DIE where we are "logically"
14767 /// positionned at, in the DIE tree.  This is useful when @p die is
14768 /// e.g, DW_TAG_partial_unit that can be included in several places in
14769 /// the DIE tree.
14770 ///
14771 /// @return the resulting class_type.
14772 static class_decl_sptr
14773 add_or_update_class_type(read_context&   ctxt,
14774                          Dwarf_Die*      die,
14775                          scope_decl*     scope,
14776                          bool            is_struct,
14777                          class_decl_sptr klass,
14778                          bool            called_from_public_decl,
14779                          size_t  where_offset)
14780 {
14781   class_decl_sptr result;
14782   if (!die)
14783     return result;
14784
14785   die_source source;
14786   ABG_ASSERT(ctxt.get_die_source(die, source));
14787
14788   unsigned tag = dwarf_tag(die);
14789
14790   if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
14791     return result;
14792
14793   {
14794     die_class_or_union_map_type::const_iterator i =
14795       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14796     if (i != ctxt.die_wip_classes_map(source).end())
14797       {
14798         class_decl_sptr class_type = is_class_type(i->second);
14799         ABG_ASSERT(class_type);
14800         return class_type;
14801       }
14802   }
14803
14804   if (!ctxt.die_is_in_cplus_plus(die))
14805     // In c++, a given class might be put together "piecewise".  That
14806     // is, in a translation unit, some data members of that class
14807     // might be defined; then in another later, some member types
14808     // might be defined.  So we can't just re-use a class "verbatim"
14809     // just because we've seen previously.  So in c++, re-using the
14810     // class is a much clever process.  In the other languages however
14811     // (like in C) we can re-use a class definition verbatim.
14812     if (class_decl_sptr class_type =
14813         is_class_type(ctxt.lookup_type_from_die(die)))
14814       if (!class_type->get_is_declaration_only())
14815         return class_type;
14816
14817   string name, linkage_name;
14818   location loc;
14819   die_loc_and_name(ctxt, die, loc, name, linkage_name);
14820   bool is_declaration_only = die_is_declaration_only(die);
14821
14822   bool is_anonymous = false;
14823   if (name.empty())
14824     {
14825       // So we are looking at an anonymous struct.  Let's
14826       // give it a name.
14827       name = get_internal_anonymous_die_prefix_name(die);
14828       ABG_ASSERT(!name.empty());
14829       // But we remember that the type is anonymous.
14830       is_anonymous = true;
14831
14832       if (size_t s = scope->get_num_anonymous_member_classes())
14833         name = build_internal_anonymous_die_name(name, s);
14834     }
14835
14836   if (!is_anonymous)
14837     {
14838       if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
14839         {
14840           if (loc)
14841             // TODO: if there is only one class defined in the corpus
14842             // for this location, then re-use it.  But if there are
14843             // more than one, then do not re-use it, for now.
14844             result = lookup_class_type_per_location(loc.expand(), *corp);
14845           else
14846             // TODO: if there is just one class for that name defined,
14847             // then re-use it.  Otherwise, don't.
14848             result = lookup_class_type(name, *corp);
14849           if (result
14850               // If we are seeing a declaration of a definition we
14851               // already had, or if we are seing a type with the same
14852               // declaration-only-ness that we had before, then keep
14853               // the one we already had.
14854               && (result->get_is_declaration_only() == is_declaration_only
14855                   || (!result->get_is_declaration_only()
14856                       && is_declaration_only)))
14857             {
14858               ctxt.associate_die_to_type(die, result, where_offset);
14859               return result;
14860             }
14861           else
14862             // We might be seeing the definition of a declaration we
14863             // already had.  In that case, keep the definition and
14864             // drop the declaration.
14865             result.reset();
14866         }
14867     }
14868
14869   // If we've already seen the same class as 'die', then let's re-use
14870   // that one, unless it's an anonymous class.  We can't really safely
14871   // re-use anonymous classes as they have no name, by construction.
14872   // What we can do, rather, is to reuse the typedef that name them,
14873   // when they do have a naming typedef.
14874   if (!is_anonymous)
14875     if (class_decl_sptr pre_existing_class =
14876         is_class_type(ctxt.lookup_type_artifact_from_die(die)))
14877       klass = pre_existing_class;
14878
14879   uint64_t size = 0;
14880   die_size_in_bits(die, size);
14881
14882   Dwarf_Die child;
14883   bool has_child = (dwarf_child(die, &child) == 0);
14884
14885   decl_base_sptr res;
14886   if (klass)
14887     {
14888       res = result = klass;
14889       if (loc)
14890         result->set_location(loc);
14891     }
14892   else
14893     {
14894       result.reset(new class_decl(ctxt.env(), name, size,
14895                                   /*alignment=*/0, is_struct, loc,
14896                                   decl_base::VISIBILITY_DEFAULT));
14897       result->set_is_anonymous(is_anonymous);
14898
14899       if (is_declaration_only)
14900         result->set_is_declaration_only(true);
14901
14902       res = add_decl_to_scope(result, scope);
14903       result = dynamic_pointer_cast<class_decl>(res);
14904       ABG_ASSERT(result);
14905     }
14906
14907   if (size)
14908     result->set_size_in_bits(size);
14909
14910   ctxt.associate_die_to_type(die, result, where_offset);
14911
14912   ctxt.maybe_schedule_declaration_only_class_for_resolution(result);
14913
14914   if (!has_child)
14915     // TODO: set the access specifier for the declaration-only class
14916     // here.
14917     return result;
14918
14919   ctxt.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14920
14921   scope_decl_sptr scop =
14922     dynamic_pointer_cast<scope_decl>(res);
14923   ABG_ASSERT(scop);
14924   ctxt.scope_stack().push(scop.get());
14925
14926   if (has_child)
14927     {
14928       int anonymous_member_class_index = -1;
14929       int anonymous_member_union_index = -1;
14930       int anonymous_member_enum_index = -1;
14931
14932       do
14933         {
14934           tag = dwarf_tag(&child);
14935
14936           // Handle base classes.
14937           if (tag == DW_TAG_inheritance)
14938             {
14939               result->set_is_declaration_only(false);
14940
14941               Dwarf_Die type_die;
14942               if (!die_die_attribute(&child, DW_AT_type, type_die))
14943                 continue;
14944
14945               type_base_sptr base_type;
14946               if (!(base_type =
14947                     lookup_class_or_typedef_from_corpus(ctxt, &type_die,
14948                                                         called_from_public_decl,
14949                                                         where_offset)))
14950                 {
14951                   base_type =
14952                     is_type(build_ir_node_from_die(ctxt, &type_die,
14953                                                    called_from_public_decl,
14954                                                    where_offset));
14955                 }
14956               // Sometimes base_type can be a typedef.  Let's make
14957               // sure that typedef is compatible with a class type.
14958               class_decl_sptr b = is_compatible_with_class_type(base_type);
14959               if (!b)
14960                 continue;
14961
14962               access_specifier access =
14963                 is_struct
14964                 ? public_access
14965                 : private_access;
14966
14967               die_access_specifier(&child, access);
14968
14969               bool is_virt= die_is_virtual(&child);
14970               int64_t offset = 0;
14971               bool is_offset_present =
14972                 die_member_offset(ctxt, &child, offset);
14973
14974               class_decl::base_spec_sptr base(new class_decl::base_spec
14975                                               (b, access,
14976                                                is_offset_present ? offset : -1,
14977                                                is_virt));
14978               if (b->get_is_declaration_only())
14979                 ABG_ASSERT(ctxt.is_decl_only_class_scheduled_for_resolution(b));
14980               if (result->find_base_class(b->get_qualified_name()))
14981                 continue;
14982               result->add_base_specifier(base);
14983             }
14984           // Handle data members.
14985           else if (tag == DW_TAG_member
14986                    || tag == DW_TAG_variable)
14987             {
14988               Dwarf_Die type_die;
14989               if (!die_die_attribute(&child, DW_AT_type, type_die))
14990                 continue;
14991
14992               string n, m;
14993               location loc;
14994               die_loc_and_name(ctxt, &child, loc, n, m);
14995               /// For now, we skip the hidden vtable pointer.
14996               /// Currently, we're looking for a member starting with
14997               /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
14998               /// use as a name for the hidden vtable pointer.
14999               if (n.substr(0, 5) == "_vptr"
15000                   && !std::isalnum(n.at(5))
15001                   && n.at(5) != '_')
15002                 continue;
15003
15004               // If the variable is already a member of this class,
15005               // move on.
15006               if (lookup_var_decl_in_scope(n, result))
15007                 continue;
15008
15009               int64_t offset_in_bits = 0;
15010               bool is_laid_out = die_member_offset(ctxt, &child,
15011                                                    offset_in_bits);
15012               // For now, is_static == !is_laid_out.  When we have
15013               // templates, we'll try to be more specific.  For now,
15014               // this approximation should do OK.
15015               bool is_static = !is_laid_out;
15016
15017               if (is_static && variable_is_suppressed(ctxt,
15018                                                       result.get(),
15019                                                       &child))
15020                 continue;
15021
15022               decl_base_sptr ty = is_decl(
15023                                           build_ir_node_from_die(ctxt, &type_die,
15024                                                                  called_from_public_decl,
15025                                                                  where_offset));
15026               type_base_sptr t = is_type(ty);
15027               if (!t)
15028                 continue;
15029
15030               // The call to build_ir_node_from_die above could have
15031               // triggered the adding of a data member named 'n' into
15032               // result.  So let's check again if the variable is
15033               // already a member of this class.
15034               if (lookup_var_decl_in_scope(n, result))
15035                 continue;
15036
15037               if (!is_static)
15038                 // We have a non-static data member.  So this class
15039                 // cannot be a declaration-only class anymore, even if
15040                 // some DWARF emitters might consider it otherwise.
15041                 result->set_is_declaration_only(false);
15042               access_specifier access =
15043                 is_struct
15044                 ? public_access
15045                 : private_access;
15046
15047               die_access_specifier(&child, access);
15048
15049               var_decl_sptr dm(new var_decl(n, t, loc, m));
15050               result->add_data_member(dm, access, is_laid_out,
15051                                       is_static, offset_in_bits);
15052               ABG_ASSERT(has_scope(dm));
15053               ctxt.associate_die_to_decl(&child, dm, where_offset,
15054                                          /*associate_by_repr=*/false);
15055             }
15056           // Handle member functions;
15057           else if (tag == DW_TAG_subprogram)
15058             {
15059               decl_base_sptr r =
15060                 add_or_update_member_function(ctxt, &child, result,
15061                                               called_from_public_decl,
15062                                               where_offset);
15063               if (function_decl_sptr f = is_function_decl(r))
15064                 ctxt.associate_die_to_decl(&child, f, where_offset,
15065                                            /*associate_by_repr=*/true);
15066             }
15067           // Handle member types
15068           else if (die_is_type(&child))
15069             {
15070               // Track the anonymous type index in the current
15071               // scope. Look for what this means by reading the
15072               // comment of the function
15073               // build_internal_anonymous_die_name.
15074               int anonymous_member_type_index = 0;
15075               if (is_anonymous_type_die(&child))
15076                 {
15077                   // Update the anonymous type index.
15078                   if (die_is_class_type(&child))
15079                     anonymous_member_type_index =
15080                       ++anonymous_member_class_index;
15081                   else if (dwarf_tag(&child) == DW_TAG_union_type)
15082                     anonymous_member_type_index =
15083                       ++anonymous_member_union_index;
15084                   else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
15085                     anonymous_member_type_index =
15086                       ++anonymous_member_enum_index;
15087                 }
15088               // if the type is not already a member of this class,
15089               // then add it to the class.
15090               if (!lookup_class_typedef_or_enum_type_from_corpus
15091                   (&child, anonymous_member_type_index, result.get()))
15092                 build_ir_node_from_die(ctxt, &child, result.get(),
15093                                        called_from_public_decl,
15094                                        where_offset);
15095             }
15096         } while (dwarf_siblingof(&child, &child) == 0);
15097     }
15098
15099   ctxt.scope_stack().pop();
15100
15101   {
15102     die_class_or_union_map_type::const_iterator i =
15103       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
15104     if (i != ctxt.die_wip_classes_map(source).end())
15105       {
15106         if (is_member_type(i->second))
15107           set_member_access_specifier(res,
15108                                       get_member_access_specifier(i->second));
15109         ctxt.die_wip_classes_map(source).erase(i);
15110       }
15111   }
15112
15113   ctxt.maybe_schedule_declaration_only_class_for_resolution(result);
15114   return result;
15115 }
15116
15117 /// Build an @ref union_decl from a DW_TAG_union_type DIE.
15118 ///
15119 /// @param ctxt the read context to use.
15120 ///
15121 /// @param die the DIE to read from.
15122 ///
15123 /// @param scope the scope the resulting @ref union_decl belongs to.
15124 ///
15125 /// @param union_type if this parameter is non-nil, then this function
15126 /// updates the @ref union_decl that it points to, rather than
15127 /// creating a new @ref union_decl.
15128 ///
15129 /// @param called_from_public_decl is true if this function has been
15130 /// initially called within the context of a public decl.
15131 ///
15132 /// @param where_offset the offset of the DIE where we are "logically"
15133 /// positionned at, in the DIE tree.  This is useful when @p die is
15134 /// e.g, DW_TAG_partial_unit that can be included in several places in
15135 /// the DIE tree.
15136 static union_decl_sptr
15137 add_or_update_union_type(read_context&  ctxt,
15138                          Dwarf_Die*     die,
15139                          scope_decl*    scope,
15140                          union_decl_sptr union_type,
15141                          bool           called_from_public_decl,
15142                          size_t where_offset)
15143 {
15144   union_decl_sptr result;
15145   if (!die)
15146     return result;
15147
15148   unsigned tag = dwarf_tag(die);
15149
15150   if (tag != DW_TAG_union_type)
15151     return result;
15152
15153   die_source source;
15154   ABG_ASSERT(ctxt.get_die_source(die, source));
15155   {
15156     die_class_or_union_map_type::const_iterator i =
15157       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
15158     if (i != ctxt.die_wip_classes_map(source).end())
15159       {
15160         union_decl_sptr u = is_union_type(i->second);
15161         ABG_ASSERT(u);
15162         return u;
15163       }
15164   }
15165
15166   string name, linkage_name;
15167   location loc;
15168   die_loc_and_name(ctxt, die, loc, name, linkage_name);
15169   bool is_declaration_only = die_is_declaration_only(die);
15170
15171   bool is_anonymous = false;
15172   if (name.empty())
15173     {
15174       // So we are looking at an anonymous union.  Let's give it a
15175       // name.
15176       name = get_internal_anonymous_die_prefix_name(die);
15177       ABG_ASSERT(!name.empty());
15178       // But we remember that the type is anonymous.
15179       is_anonymous = true;
15180
15181       if (size_t s = scope->get_num_anonymous_member_unions())
15182         name = build_internal_anonymous_die_name(name, s);
15183     }
15184
15185   // If the type has location, then associate it to its
15186   // representation.  This way, all occurences of types with the same
15187   // representation (name) and location can be later detected as being
15188   // for the same type.
15189
15190   if (!is_anonymous)
15191     {
15192       if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
15193         {
15194           if (loc)
15195             result = lookup_union_type_per_location(loc.expand(), *corp);
15196           else
15197             result = lookup_union_type(name, *corp);
15198
15199           if (result)
15200             {
15201               ctxt.associate_die_to_type(die, result, where_offset);
15202               return result;
15203             }
15204         }
15205     }
15206
15207   // if we've already seen a union with the same union as 'die' then
15208   // let's re-use that one. We can't really safely re-use anonymous
15209   // classes as they have no name, by construction.  What we can do,
15210   // rather, is to reuse the typedef that name them, when they do have
15211   // a naming typedef.
15212   if (!is_anonymous)
15213     if (union_decl_sptr pre_existing_union =
15214         is_union_type(ctxt.lookup_artifact_from_die(die)))
15215       union_type = pre_existing_union;
15216
15217   uint64_t size = 0;
15218   die_size_in_bits(die, size);
15219
15220   if (union_type)
15221     {
15222       result = union_type;
15223       result->set_location(loc);
15224     }
15225   else
15226     {
15227       result.reset(new union_decl(ctxt.env(), name, size,
15228                                   loc, decl_base::VISIBILITY_DEFAULT));
15229       result->set_is_anonymous(is_anonymous);
15230       if (is_declaration_only)
15231         result->set_is_declaration_only(true);
15232       result = is_union_type(add_decl_to_scope(result, scope));
15233       ABG_ASSERT(result);
15234     }
15235
15236   if (size)
15237     {
15238       result->set_size_in_bits(size);
15239       result->set_is_declaration_only(false);
15240     }
15241
15242   ctxt.associate_die_to_type(die, result, where_offset);
15243
15244   // TODO: maybe schedule declaration-only union for result like we do
15245   // for classes:
15246   // ctxt.maybe_schedule_declaration_only_class_for_resolution(result);
15247
15248   Dwarf_Die child;
15249   bool has_child = (dwarf_child(die, &child) == 0);
15250   if (!has_child)
15251     return result;
15252
15253   ctxt.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
15254
15255   scope_decl_sptr scop =
15256     dynamic_pointer_cast<scope_decl>(result);
15257   ABG_ASSERT(scop);
15258   ctxt.scope_stack().push(scop.get());
15259
15260   if (has_child)
15261     {
15262       do
15263         {
15264           tag = dwarf_tag(&child);
15265           // Handle data members.
15266           if (tag == DW_TAG_member || tag == DW_TAG_variable)
15267             {
15268               Dwarf_Die type_die;
15269               if (!die_die_attribute(&child, DW_AT_type, type_die))
15270                 continue;
15271
15272               string n, m;
15273               location loc;
15274               die_loc_and_name(ctxt, &child, loc, n, m);
15275
15276               if (lookup_var_decl_in_scope(n, result))
15277                 continue;
15278
15279               ssize_t offset_in_bits = 0;
15280               decl_base_sptr ty =
15281                 is_decl(build_ir_node_from_die(ctxt, &type_die,
15282                                                called_from_public_decl,
15283                                                where_offset));
15284               type_base_sptr t = is_type(ty);
15285               if (!t)
15286                 continue;
15287
15288               // We have a non-static data member.  So this class
15289               // cannot be a declaration-only class anymore, even if
15290               // some DWARF emitters might consider it otherwise.
15291               result->set_is_declaration_only(false);
15292               access_specifier access = private_access;
15293
15294               die_access_specifier(&child, access);
15295
15296               var_decl_sptr dm(new var_decl(n, t, loc, m));
15297               result->add_data_member(dm, access, /*is_laid_out=*/true,
15298                                       /*is_static=*/false,
15299                                       offset_in_bits);
15300               ABG_ASSERT(has_scope(dm));
15301               ctxt.associate_die_to_decl(&child, dm, where_offset,
15302                                          /*associate_by_repr=*/false);
15303             }
15304           // Handle member functions;
15305           else if (tag == DW_TAG_subprogram)
15306             {
15307               decl_base_sptr r =
15308                 is_decl(build_ir_node_from_die(ctxt, &child,
15309                                                result.get(),
15310                                                called_from_public_decl,
15311                                                where_offset));
15312               if (!r)
15313                 continue;
15314
15315               function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
15316               ABG_ASSERT(f);
15317
15318               finish_member_function_reading(&child, f, result, ctxt);
15319
15320               ctxt.associate_die_to_decl(&child, f, where_offset,
15321                                          /*associate_by_repr=*/false);
15322             }
15323           // Handle member types
15324           else if (die_is_type(&child))
15325             decl_base_sptr td =
15326               is_decl(build_ir_node_from_die(ctxt, &child, result.get(),
15327                                              called_from_public_decl,
15328                                              where_offset));
15329         } while (dwarf_siblingof(&child, &child) == 0);
15330     }
15331
15332   ctxt.scope_stack().pop();
15333
15334   {
15335     die_class_or_union_map_type::const_iterator i =
15336       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
15337     if (i != ctxt.die_wip_classes_map(source).end())
15338       {
15339         if (is_member_type(i->second))
15340           set_member_access_specifier(result,
15341                                       get_member_access_specifier(i->second));
15342         ctxt.die_wip_classes_map(source).erase(i);
15343       }
15344   }
15345
15346   return result;
15347 }
15348
15349 /// build a qualified type from a DW_TAG_const_type,
15350 /// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
15351 ///
15352 /// @param ctxt the read context to consider.
15353 ///
15354 /// @param die the input DIE to read from.
15355 ///
15356 /// @param called_from_public_decl true if this function was called
15357 /// from a context where either a public function or a public variable
15358 /// is being built.
15359 ///
15360 /// @param where_offset the offset of the DIE where we are "logically"
15361 /// positionned at, in the DIE tree.  This is useful when @p die is
15362 /// e.g, DW_TAG_partial_unit that can be included in several places in
15363 /// the DIE tree.
15364 ///
15365 /// @return the resulting qualified_type_def.
15366 static type_base_sptr
15367 build_qualified_type(read_context&      ctxt,
15368                      Dwarf_Die* die,
15369                      bool               called_from_public_decl,
15370                      size_t             where_offset)
15371 {
15372   type_base_sptr result;
15373   if (!die)
15374     return result;
15375
15376   die_source source;
15377   ABG_ASSERT(ctxt.get_die_source(die, source));
15378
15379   unsigned tag = dwarf_tag(die);
15380
15381   if (tag != DW_TAG_const_type
15382       && tag != DW_TAG_volatile_type
15383       && tag != DW_TAG_restrict_type)
15384     return result;
15385
15386   Dwarf_Die underlying_type_die;
15387   decl_base_sptr utype_decl;
15388   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15389     // So, if no DW_AT_type is present, then this means (if we are
15390     // looking at a debug info emitted by GCC) that we are looking
15391     // at a qualified void type.
15392     utype_decl = build_ir_node_for_void_type(ctxt);
15393
15394   if (!utype_decl)
15395     utype_decl = is_decl(build_ir_node_from_die(ctxt, &underlying_type_die,
15396                                                 called_from_public_decl,
15397                                                 where_offset));
15398   if (!utype_decl)
15399     return result;
15400
15401   // The call to build_ir_node_from_die() could have triggered the
15402   // creation of the type for this DIE.  In that case, just return it.
15403   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
15404     {
15405       result = t;
15406       ctxt.associate_die_to_type(die, result, where_offset);
15407       return result;
15408     }
15409
15410   type_base_sptr utype = is_type(utype_decl);
15411   ABG_ASSERT(utype);
15412
15413   qualified_type_def::CV qual = qualified_type_def::CV_NONE;
15414   if (tag == DW_TAG_const_type)
15415     qual |= qualified_type_def::CV_CONST;
15416   else if (tag == DW_TAG_volatile_type)
15417     qual |= qualified_type_def::CV_VOLATILE;
15418   else if (tag == DW_TAG_restrict_type)
15419     qual |= qualified_type_def::CV_RESTRICT;
15420   else
15421     ABG_ASSERT_NOT_REACHED;
15422
15423   if (!result)
15424     result.reset(new qualified_type_def(utype, qual, location()));
15425
15426   ctxt.associate_die_to_type(die, result, where_offset);
15427
15428   return result;
15429 }
15430
15431 /// Strip qualification from a qualified type, when it makes sense.
15432 ///
15433 /// DWARF constructs "const reference".  This is redundant because a
15434 /// reference is always const.  The issue is these redundant types then
15435 /// leak into the IR and make for bad diagnostics.
15436 ///
15437 /// This function thus strips the const qualifier from the type in
15438 /// that case.  It might contain code to strip other cases like this
15439 /// in the future.
15440 ///
15441 /// @param t the type to strip const qualification from.
15442 ///
15443 /// @param ctxt the @ref read_context to use.
15444 ///
15445 /// @return the stripped type or just return @p t.
15446 static decl_base_sptr
15447 maybe_strip_qualification(const qualified_type_def_sptr t,
15448                           read_context &ctxt)
15449 {
15450   if (!t)
15451     return t;
15452
15453   decl_base_sptr result = t;
15454   type_base_sptr u = t->get_underlying_type();
15455   environment* env = t->get_environment();
15456
15457   if (t->get_cv_quals() & qualified_type_def::CV_CONST
15458       && (is_reference_type(u)))
15459     {
15460       // Let's strip only the const qualifier.  To do that, the "const"
15461       // qualified is turned into a no-op "none" qualified.
15462       result.reset(new qualified_type_def
15463                    (u, t->get_cv_quals() & ~qualified_type_def::CV_CONST,
15464                     t->get_location()));
15465     }
15466   else if (t->get_cv_quals() & qualified_type_def::CV_CONST
15467            && env->is_void_type(u))
15468     {
15469       // So this type is a "const void".  Let's strip the "const"
15470       // qualifier out and make this just be "void", so that a "const
15471       // void" type and a "void" type compare equal after going through
15472       // this function.
15473       result = is_decl(u);
15474     }
15475   else if (is_array_of_qualified_element(u))
15476     {
15477       // In C and C++, a cv qualifiers of a qualified array apply to
15478       // the array element type.  So the qualifiers of the array can
15479       // be dropped and applied to the element type.
15480       //
15481       // Here, the element type is qualified already.  So apply the
15482       // qualifiers of the array itself to the already qualified
15483       // element type and drop the array qualifiers.
15484       array_type_def_sptr array = is_array_type(u);
15485       qualified_type_def_sptr element_type =
15486         is_qualified_type(array->get_element_type());
15487       qualified_type_def::CV quals = element_type->get_cv_quals();
15488       quals |= t->get_cv_quals();
15489       element_type->set_cv_quals(quals);
15490       result = is_decl(u);
15491       if (u->get_canonical_type()
15492           || element_type->get_canonical_type())
15493         // We shouldn't be editing types that were already
15494         // canonicalized.  For those, canonicalization should be
15495         // delayed until after all editing is done.
15496         ABG_ASSERT_NOT_REACHED;
15497     }
15498   else if (is_array_type(u) && !is_array_of_qualified_element(is_array_type(u)))
15499     {
15500       // In C and C++, a cv qualifiers of a qualified array apply to
15501       // the array element type.  So the qualifiers of the array can
15502       // be dropped and applied to the element type.
15503       //
15504       // Here, the element type is not qualified.  So apply the
15505       // qualifiers of the array itself to the element type and drop
15506       // the array qualifiers.
15507       array_type_def_sptr array = is_array_type(u);
15508       type_base_sptr element_type = array->get_element_type();
15509       qualified_type_def_sptr qual_type
15510         (new qualified_type_def(element_type,
15511                                 t->get_cv_quals(),
15512                                 t->get_location()));
15513       add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
15514       array->set_element_type(qual_type);
15515       ctxt.schedule_type_for_late_canonicalization(is_type(qual_type));
15516       result = is_decl(u);
15517       if (u->get_canonical_type())
15518         // We shouldn't be editing types that were already
15519         // canonicalized.  For those, canonicalization should be
15520         // delayed until after all editing is done.
15521         ABG_ASSERT_NOT_REACHED;
15522     }
15523
15524   return result;
15525 }
15526
15527 /// Build a pointer type from a DW_TAG_pointer_type DIE.
15528 ///
15529 /// @param ctxt the read context to consider.
15530 ///
15531 /// @param die the DIE to read information from.
15532 ///
15533 /// @param called_from_public_decl true if this function was called
15534 /// from a context where either a public function or a public variable
15535 /// is being built.
15536 ///
15537 /// @param where_offset the offset of the DIE where we are "logically"
15538 /// positionned at, in the DIE tree.  This is useful when @p die is
15539 /// e.g, DW_TAG_partial_unit that can be included in several places in
15540 /// the DIE tree.
15541 ///
15542 /// @return the resulting pointer to pointer_type_def.
15543 static pointer_type_def_sptr
15544 build_pointer_type_def(read_context&    ctxt,
15545                        Dwarf_Die*       die,
15546                        bool             called_from_public_decl,
15547                        size_t           where_offset)
15548 {
15549   pointer_type_def_sptr result;
15550
15551   if (!die)
15552     return result;
15553
15554   die_source source;
15555   ABG_ASSERT(ctxt.get_die_source(die, source));
15556
15557   unsigned tag = dwarf_tag(die);
15558   if (tag != DW_TAG_pointer_type)
15559     return result;
15560
15561   type_or_decl_base_sptr utype_decl;
15562   Dwarf_Die underlying_type_die;
15563   bool has_underlying_type_die = false;
15564   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15565     // If the DW_AT_type attribute is missing, that means we are
15566     // looking at a pointer to "void".
15567     utype_decl = build_ir_node_for_void_type(ctxt);
15568   else
15569     has_underlying_type_die = true;
15570
15571   if (!utype_decl && has_underlying_type_die)
15572     utype_decl = build_ir_node_from_die(ctxt, &underlying_type_die,
15573                                         called_from_public_decl,
15574                                         where_offset);
15575   if (!utype_decl)
15576     return result;
15577
15578   // The call to build_ir_node_from_die() could have triggered the
15579   // creation of the type for this DIE.  In that case, just return it.
15580   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
15581     {
15582       result = is_pointer_type(t);
15583       ABG_ASSERT(result);
15584       return result;
15585     }
15586
15587   type_base_sptr utype = is_type(utype_decl);
15588   ABG_ASSERT(utype);
15589
15590   // if the DIE for the pointer type doesn't have a byte_size
15591   // attribute then we assume the size of the pointer is the address
15592   // size of the current translation unit.
15593   uint64_t size = ctxt.cur_transl_unit()->get_address_size();
15594   if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15595     // The size as expressed by DW_AT_byte_size is in byte, so let's
15596     // convert it to bits.
15597     size *= 8;
15598
15599   // And the size of the pointer must be the same as the address size
15600   // of the current translation unit.
15601   ABG_ASSERT((size_t) ctxt.cur_transl_unit()->get_address_size() == size);
15602
15603   result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
15604   ABG_ASSERT(result->get_pointed_to_type());
15605
15606   ctxt.associate_die_to_type(die, result, where_offset);
15607   return result;
15608 }
15609
15610 /// Build a reference type from either a DW_TAG_reference_type or
15611 /// DW_TAG_rvalue_reference_type DIE.
15612 ///
15613 /// @param ctxt the read context to consider.
15614 ///
15615 /// @param die the DIE to read from.
15616 ///
15617 /// @param called_from_public_decl true if this function was called
15618 /// from a context where either a public function or a public variable
15619 /// is being built.
15620 ///
15621 /// @param where_offset the offset of the DIE where we are "logically"
15622 /// positionned at, in the DIE tree.  This is useful when @p die is
15623 /// e.g, DW_TAG_partial_unit that can be included in several places in
15624 /// the DIE tree.
15625 ///
15626 /// @return a pointer to the resulting reference_type_def.
15627 static reference_type_def_sptr
15628 build_reference_type(read_context&      ctxt,
15629                      Dwarf_Die* die,
15630                      bool               called_from_public_decl,
15631                      size_t             where_offset)
15632 {
15633   reference_type_def_sptr result;
15634
15635   if (!die)
15636     return result;
15637
15638   die_source source;
15639   ABG_ASSERT(ctxt.get_die_source(die, source));
15640
15641   unsigned tag = dwarf_tag(die);
15642   if (tag != DW_TAG_reference_type
15643       && tag != DW_TAG_rvalue_reference_type)
15644     return result;
15645
15646   Dwarf_Die underlying_type_die;
15647   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15648     return result;
15649
15650   type_or_decl_base_sptr utype_decl =
15651     build_ir_node_from_die(ctxt, &underlying_type_die,
15652                            called_from_public_decl,
15653                            where_offset);
15654   if (!utype_decl)
15655     return result;
15656
15657   // The call to build_ir_node_from_die() could have triggered the
15658   // creation of the type for this DIE.  In that case, just return it.
15659   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
15660     {
15661       result = is_reference_type(t);
15662       ABG_ASSERT(result);
15663       return result;
15664     }
15665
15666   type_base_sptr utype = is_type(utype_decl);
15667   ABG_ASSERT(utype);
15668
15669   // if the DIE for the reference type doesn't have a byte_size
15670   // attribute then we assume the size of the reference is the address
15671   // size of the current translation unit.
15672   uint64_t size = ctxt.cur_transl_unit()->get_address_size();
15673   if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15674     size *= 8;
15675
15676   // And the size of the pointer must be the same as the address size
15677   // of the current translation unit.
15678   ABG_ASSERT((size_t) ctxt.cur_transl_unit()->get_address_size() == size);
15679
15680   bool is_lvalue = (tag == DW_TAG_reference_type) ? true : false;
15681
15682   result.reset(new reference_type_def(utype, is_lvalue, size,
15683                                       /*alignment=*/0,
15684                                       location()));
15685   if (corpus_sptr corp = ctxt.current_corpus())
15686     if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
15687       result = t;
15688   ctxt.associate_die_to_type(die, result, where_offset);
15689   return result;
15690 }
15691
15692 /// Build a subroutine type from a DW_TAG_subroutine_type DIE.
15693 ///
15694 /// @param ctxt the read context to consider.
15695 ///
15696 /// @param die the DIE to read from.
15697 ///
15698 /// @param is_method points to a class or union declaration iff we're
15699 /// building the type for a method.  This is the enclosing class or
15700 /// union of the method.
15701 ///
15702 /// @param where_offset the offset of the DIE where we are "logically"
15703 /// positioned at, in the DIE tree.  This is useful when @p die is
15704 /// e.g, DW_TAG_partial_unit that can be included in several places in
15705 /// the DIE tree.
15706 ///
15707 /// @return a pointer to the resulting function_type_sptr.
15708 static function_type_sptr
15709 build_function_type(read_context&       ctxt,
15710                     Dwarf_Die*          die,
15711                     class_or_union_sptr is_method,
15712                     size_t              where_offset)
15713 {
15714   function_type_sptr result;
15715
15716   if (!die)
15717     return result;
15718
15719   ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
15720              || dwarf_tag(die) == DW_TAG_subprogram);
15721
15722   die_source source;
15723   ABG_ASSERT(ctxt.get_die_source(die, source));
15724
15725   decl_base_sptr type_decl;
15726
15727   translation_unit_sptr tu = ctxt.cur_transl_unit();
15728   ABG_ASSERT(tu);
15729
15730   /// If, inside the current translation unit, we've already seen a
15731   /// function type with the same text representation, then reuse that
15732   /// one instead.
15733   if (type_base_sptr t = ctxt.lookup_fn_type_from_die_repr_per_tu(die))
15734     {
15735       result = is_function_type(t);
15736       ABG_ASSERT(result);
15737       ctxt.associate_die_to_type(die, result, where_offset);
15738       return result;
15739     }
15740
15741   bool odr_is_relevant = ctxt.odr_is_relevant(die);
15742   if (odr_is_relevant)
15743     {
15744       // So we can rely on the One Definition Rule to say that if
15745       // several different function types have the same name (or
15746       // rather, representation) across the entire binary, then they
15747       // ought to designate the same function type.  So let's ensure
15748       // that if we've already seen a function type with the same
15749       // representation as the function type 'die', then it's the same
15750       // type as the one denoted by 'die'.
15751       if (function_type_sptr fn_type =
15752           is_function_type(ctxt.lookup_type_artifact_from_die(die)))
15753         {
15754           ctxt.associate_die_to_type(die, fn_type, where_offset);
15755           return fn_type;
15756         }
15757     }
15758
15759   // Let's look at the DIE to detect if it's the DIE for a method
15760   // (type).  If it is, we can deduce the name of its enclosing class
15761   // and if it's a static or const.
15762   bool is_const = false;
15763   bool is_static = false;
15764   Dwarf_Die object_pointer_die;
15765   Dwarf_Die class_type_die;
15766   bool has_this_parm_die =
15767     die_function_type_is_method_type(ctxt, die, where_offset,
15768                                      object_pointer_die,
15769                                      class_type_die,
15770                                      is_static);
15771   if (has_this_parm_die)
15772     {
15773       // The function (type) has a "this" parameter DIE. It means it's
15774       // a member function DIE.
15775       if (!is_static)
15776         if (die_object_pointer_is_for_const_method(&object_pointer_die))
15777           is_const = true;
15778
15779       if (!is_method)
15780         {
15781           // We were initially called as if the function represented
15782           // by DIE was *NOT* a member function.  But now we know it's
15783           // a member function.  Let's take that into account.
15784           class_or_union_sptr klass_type =
15785             is_class_or_union_type(build_ir_node_from_die(ctxt, &class_type_die,
15786                                                           /*called_from_pub_decl=*/true,
15787                                                           where_offset));
15788           ABG_ASSERT(klass_type);
15789           is_method = klass_type;
15790         }
15791     }
15792
15793   // Let's create the type early and record it as being for the DIE
15794   // 'die'.  This way, when building the sub-type triggers the
15795   // creation of a type matching the same 'die', then we'll reuse this
15796   // one.
15797
15798   result.reset(is_method
15799                ? new method_type(is_method, is_const,
15800                                  tu->get_address_size(),
15801                                  /*alignment=*/0)
15802                : new function_type(ctxt.env(), tu->get_address_size(),
15803                                    /*alignment=*/0));
15804   ctxt.associate_die_to_type(die, result, where_offset);
15805   ctxt.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
15806   ctxt.associate_die_repr_to_fn_type_per_tu(die, result);
15807
15808   type_base_sptr return_type;
15809   Dwarf_Die ret_type_die;
15810   if (die_die_attribute(die, DW_AT_type, ret_type_die))
15811     return_type =
15812       is_type(build_ir_node_from_die(ctxt, &ret_type_die,
15813                                      /*called_from_public_decl=*/true,
15814                                      where_offset));
15815   if (!return_type)
15816     return_type = is_type(build_ir_node_for_void_type(ctxt));
15817   result->set_return_type(return_type);
15818
15819   Dwarf_Die child;
15820   function_decl::parameters function_parms;
15821
15822   if (dwarf_child(die, &child) == 0)
15823     do
15824       {
15825         int child_tag = dwarf_tag(&child);
15826         if (child_tag == DW_TAG_formal_parameter)
15827           {
15828             // This is a "normal" function parameter.
15829             string name, linkage_name;
15830             location loc;
15831             die_loc_and_name(ctxt, &child, loc, name, linkage_name);
15832             if (!tools_utils::string_is_ascii_identifier(name))
15833               // Sometimes, bogus compiler emit names that are
15834               // non-ascii garbage.  Let's just ditch that for now.
15835               name.clear();
15836             bool is_artificial = die_is_artificial(&child);
15837             type_base_sptr parm_type;
15838             Dwarf_Die parm_type_die;
15839             if (die_die_attribute(&child, DW_AT_type, parm_type_die))
15840               parm_type =
15841                 is_type(build_ir_node_from_die(ctxt, &parm_type_die,
15842                                                /*called_from_public_decl=*/true,
15843                                                where_offset));
15844             if (!parm_type)
15845               continue;
15846             function_decl::parameter_sptr p
15847               (new function_decl::parameter(parm_type, name, loc,
15848                                             /*variadic_marker=*/false,
15849                                             is_artificial));
15850             function_parms.push_back(p);
15851           }
15852         else if (child_tag == DW_TAG_unspecified_parameters)
15853           {
15854             // This is a variadic function parameter.
15855             bool is_artificial = die_is_artificial(&child);
15856             ir::environment* env = ctxt.env();
15857             ABG_ASSERT(env);
15858             type_base_sptr parm_type = env->get_variadic_parameter_type();
15859             function_decl::parameter_sptr p
15860               (new function_decl::parameter(parm_type,
15861                                             /*name=*/"",
15862                                             location(),
15863                                             /*variadic_marker=*/true,
15864                                             is_artificial));
15865             function_parms.push_back(p);
15866             // After a DW_TAG_unspecified_parameters tag, we shouldn't
15867             // keep reading for parameters.  The
15868             // unspecified_parameters TAG should be the last parameter
15869             // that we record. For instance, if there are multiple
15870             // DW_TAG_unspecified_parameters DIEs then we should care
15871             // only for the first one.
15872             break;
15873           }
15874       }
15875     while (dwarf_siblingof(&child, &child) == 0);
15876
15877   result->set_parameters(function_parms);
15878
15879   tu->bind_function_type_life_time(result);
15880
15881   {
15882     die_function_type_map_type::const_iterator i =
15883       ctxt.die_wip_function_types_map(source).
15884       find(dwarf_dieoffset(die));
15885     if (i != ctxt.die_wip_function_types_map(source).end())
15886       ctxt.die_wip_function_types_map(source).erase(i);
15887   }
15888
15889   maybe_canonicalize_type(result, ctxt);
15890   return result;
15891 }
15892
15893 /// Build a subrange type from a DW_TAG_subrange_type.
15894 ///
15895 /// @param ctxt the read context to consider.
15896 ///
15897 /// @param die the DIE to read from.
15898 ///
15899 /// @param where_offset the offset of the DIE where we are "logically"
15900 /// positionned at in the DIE tree.  This is useful when @p die is
15901 /// e,g, DW_TAG_partial_unit that can be included in several places in
15902 /// the DIE tree.
15903 ///
15904 /// @param associate_die_to_type if this is true then the resulting
15905 /// type is associated to the @p die, so that next time when the
15906 /// system looks up the type associated to it, the current resulting
15907 /// type is returned.  If false, then no association is done and the
15908 /// resulting type can be destroyed right after.  This can be useful
15909 /// when the sole purpose of building the @ref
15910 /// array_type_def::subrange_type is to use some of its method like,
15911 /// e.g, its name pretty printing methods.
15912 ///
15913 /// @return the newly built instance of @ref
15914 /// array_type_def::subrange_type, or nil if no type could be built.
15915 static array_type_def::subrange_sptr
15916 build_subrange_type(read_context&       ctxt,
15917                     const Dwarf_Die*            die,
15918                     size_t              where_offset,
15919                     bool                associate_type_to_die)
15920 {
15921   array_type_def::subrange_sptr result;
15922
15923   if (!die)
15924     return result;
15925
15926   die_source source;
15927   ABG_ASSERT(ctxt.get_die_source(die, source));
15928
15929   unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
15930   if (tag != DW_TAG_subrange_type)
15931     return result;
15932
15933   string name = die_name(die);
15934
15935   translation_unit::language language = ctxt.cur_transl_unit()->get_language();
15936   array_type_def::subrange_type::bound_value lower_bound =
15937     get_default_array_lower_bound(language);
15938   array_type_def::subrange_type::bound_value upper_bound;
15939   uint64_t count = 0;
15940   bool is_infinite = false;
15941
15942   // The DWARF 4 specifications says, in [5.11 Subrange
15943   // Type Entries]:
15944   //
15945   //     The subrange entry may have the attributes
15946   //     DW_AT_lower_bound and DW_AT_upper_bound to
15947   //     specify, respectively, the lower and upper bound
15948   //     values of the subrange.
15949   //
15950   // So let's look for DW_AT_lower_bound first.
15951   die_constant_attribute(die, DW_AT_lower_bound, lower_bound);
15952
15953   // Then, DW_AT_upper_bound.
15954   if (!die_constant_attribute(die, DW_AT_upper_bound, upper_bound))
15955     {
15956       // The DWARF 4 spec says, in [5.11 Subrange Type
15957       // Entries]:
15958       //
15959       //   The DW_AT_upper_bound attribute may be replaced
15960       //   by a DW_AT_count attribute, whose value
15961       //   describes the number of elements in the
15962       //   subrange rather than the value of the last
15963       //   element."
15964       //
15965       // So, as DW_AT_upper_bound is not present in this
15966       // case, let's see if there is a DW_AT_count.
15967       die_unsigned_constant_attribute(die, DW_AT_count, count);
15968
15969       // We can deduce the upper_bound from the
15970       // lower_bound and the number of elements of the
15971       // array:
15972       if (int64_t u = lower_bound.get_signed_value() + count)
15973         upper_bound = u - 1;
15974
15975       if (upper_bound.get_unsigned_value() == 0 && count == 0)
15976         // No upper_bound nor count was present on the DIE, this means
15977         // the array is considered to have an infinite (or rather not
15978         // known) size.
15979         is_infinite = true;
15980     }
15981
15982   if (UINT64_MAX == upper_bound.get_unsigned_value())
15983     {
15984       // If the upper_bound size is the max of the integer value, then
15985       // it most certainly means infinite size.
15986       is_infinite = true;
15987       upper_bound.set_unsigned(0);
15988     }
15989
15990   result.reset
15991     (new array_type_def::subrange_type(ctxt.env(),
15992                                        name,
15993                                        lower_bound,
15994                                        upper_bound,
15995                                        location()));
15996   result->is_infinite(is_infinite);
15997
15998   // load the underlying type.
15999   Dwarf_Die underlying_type_die;
16000   type_base_sptr underlying_type;
16001   if (die_die_attribute(die, DW_AT_type, underlying_type_die))
16002     underlying_type =
16003       is_type(build_ir_node_from_die(ctxt,
16004                                      &underlying_type_die,
16005                                      /*called_from_public_decl=*/true,
16006                                      where_offset));
16007
16008   if (underlying_type)
16009     result->set_underlying_type(underlying_type);
16010
16011   if (associate_type_to_die)
16012     ctxt.associate_die_to_type(die, result, where_offset);
16013
16014   return result;
16015 }
16016
16017 /// Build the sub-ranges of an array type.
16018 ///
16019 /// This is a sub-routine of build_array_type().
16020 ///
16021 /// @param ctxt the context to read from.
16022 ///
16023 /// @param die the DIE of tag DW_TAG_array_type which contains
16024 /// children DIEs that represent the sub-ranges.
16025 ///
16026 /// @param subranges out parameter.  This is set to the sub-ranges
16027 /// that are built from @p die.
16028 ///
16029 /// @param where_offset the offset of the DIE where we are "logically"
16030 /// positioned at, in the DIE tree.  This is useful when @p die is
16031 /// e.g, DW_TAG_partial_unit that can be included in several places in
16032 /// the DIE tree.
16033 static void
16034 build_subranges_from_array_type_die(read_context&                       ctxt,
16035                                     const Dwarf_Die*                    die,
16036                                     array_type_def::subranges_type&     subranges,
16037                                     size_t                              where_offset,
16038                                     bool                                associate_type_to_die)
16039 {
16040   Dwarf_Die child;
16041
16042   if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
16043     {
16044       do
16045         {
16046           int child_tag = dwarf_tag(&child);
16047           if (child_tag == DW_TAG_subrange_type)
16048             {
16049               array_type_def::subrange_sptr s;
16050               if (associate_type_to_die)
16051                 {
16052                   // We are being called to create the type, add it to
16053                   // the current type graph and associate it to the
16054                   // DIE it's been created from.
16055                   type_or_decl_base_sptr t =
16056                     build_ir_node_from_die(ctxt, &child,
16057                                            /*called_from_public_decl=*/true,
16058                                            where_offset);
16059                   s = is_subrange_type(t);
16060                 }
16061               else
16062                 // We are being called to create the type but *NOT*
16063                 // add it to the current tyupe tree, *NOR* associate
16064                 // it to the DIE it's been created from.
16065                 s = build_subrange_type(ctxt, &child,
16066                                         where_offset,
16067                                         /*associate_type_to_die=*/false);
16068               if (s)
16069                 subranges.push_back(s);
16070             }
16071         }
16072       while (dwarf_siblingof(&child, &child) == 0);
16073     }
16074 }
16075
16076 /// Build an array type from a DW_TAG_array_type DIE.
16077 ///
16078 /// @param ctxt the read context to consider.
16079 ///
16080 /// @param die the DIE to read from.
16081 ///
16082 /// @param called_from_public_decl true if this function was called
16083 /// from a context where either a public function or a public variable
16084 /// is being built.
16085 ///
16086 /// @param where_offset the offset of the DIE where we are "logically"
16087 /// positioned at, in the DIE tree.  This is useful when @p die is
16088 /// e.g, DW_TAG_partial_unit that can be included in several places in
16089 /// the DIE tree.
16090 ///
16091 /// @return a pointer to the resulting array_type_def.
16092 static array_type_def_sptr
16093 build_array_type(read_context&  ctxt,
16094                  Dwarf_Die*     die,
16095                  bool           called_from_public_decl,
16096                  size_t where_offset)
16097 {
16098   array_type_def_sptr result;
16099
16100   if (!die)
16101     return result;
16102
16103   die_source source;
16104   ABG_ASSERT(ctxt.get_die_source(die, source));
16105
16106   unsigned tag = dwarf_tag(die);
16107   if (tag != DW_TAG_array_type)
16108     return result;
16109
16110   decl_base_sptr type_decl;
16111   Dwarf_Die type_die;
16112
16113   if (die_die_attribute(die, DW_AT_type, type_die))
16114     type_decl = is_decl(build_ir_node_from_die(ctxt, &type_die,
16115                                                called_from_public_decl,
16116                                                where_offset));
16117   if (!type_decl)
16118     return result;
16119
16120   // The call to build_ir_node_from_die() could have triggered the
16121   // creation of the type for this DIE.  In that case, just return it.
16122   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
16123     {
16124       result = is_array_type(t);
16125       ABG_ASSERT(result);
16126       return result;
16127     }
16128
16129   type_base_sptr type = is_type(type_decl);
16130   ABG_ASSERT(type);
16131
16132   array_type_def::subranges_type subranges;
16133
16134   build_subranges_from_array_type_die(ctxt, die, subranges, where_offset);
16135
16136   result.reset(new array_type_def(type, subranges, location()));
16137
16138   return result;
16139 }
16140
16141 /// Create a typedef_decl from a DW_TAG_typedef DIE.
16142 ///
16143 /// @param ctxt the read context to consider.
16144 ///
16145 /// @param die the DIE to read from.
16146 ///
16147 /// @param called_from_public_decl true if this function was called
16148 /// from a context where either a public function or a public variable
16149 /// is being built.
16150 ///
16151 /// @param where_offset the offset of the DIE where we are "logically"
16152 /// positionned at, in the DIE tree.  This is useful when @p die is
16153 /// e.g, DW_TAG_partial_unit that can be included in several places in
16154 /// the DIE tree.
16155 ///
16156 /// @return the newly created typedef_decl.
16157 static typedef_decl_sptr
16158 build_typedef_type(read_context&        ctxt,
16159                    Dwarf_Die*           die,
16160                    bool         called_from_public_decl,
16161                    size_t               where_offset)
16162 {
16163   typedef_decl_sptr result;
16164
16165   if (!die)
16166     return result;
16167
16168   die_source source;
16169   ABG_ASSERT(ctxt.get_die_source(die, source));
16170
16171   unsigned tag = dwarf_tag(die);
16172   if (tag != DW_TAG_typedef)
16173     return result;
16174
16175   string name, linkage_name;
16176   location loc;
16177   die_loc_and_name(ctxt, die, loc, name, linkage_name);
16178
16179   if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
16180     if (loc)
16181       result = lookup_typedef_type_per_location(loc.expand(), *corp);
16182
16183   if (!ctxt.odr_is_relevant(die))
16184     if (typedef_decl_sptr t = is_typedef(ctxt.lookup_artifact_from_die(die)))
16185       result = t;
16186
16187   if (!result)
16188     {
16189       type_base_sptr utype;
16190       Dwarf_Die underlying_type_die;
16191       if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
16192         // A typedef DIE with no underlying type means a typedef to
16193         // void type.
16194         utype = ctxt.env()->get_void_type();
16195
16196       if (!utype)
16197         utype =
16198           is_type(build_ir_node_from_die(ctxt,
16199                                          &underlying_type_die,
16200                                          called_from_public_decl,
16201                                          where_offset));
16202       if (!utype)
16203         return result;
16204
16205       // The call to build_ir_node_from_die() could have triggered the
16206       // creation of the type for this DIE.  In that case, just return
16207       // it.
16208       if (type_base_sptr t = ctxt.lookup_type_from_die(die))
16209         {
16210           result = is_typedef(t);
16211           ABG_ASSERT(result);
16212           return result;
16213         }
16214
16215       ABG_ASSERT(utype);
16216       result.reset(new typedef_decl(name, utype, loc, linkage_name));
16217
16218       if (class_decl_sptr klass = is_class_type(utype))
16219         if (is_anonymous_type(klass))
16220           klass->set_naming_typedef(result);
16221     }
16222
16223   ctxt.associate_die_to_type(die, result, where_offset);
16224
16225   return result;
16226 }
16227
16228 /// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
16229 /// denoted by the DIE is not suppressed by a suppression
16230 /// specification associated to the current read context.
16231 ///
16232 /// Note that if a member variable declaration with the same name as
16233 /// the name of the DIE we are looking at exists, this function returns
16234 /// that existing variable declaration.
16235 ///
16236 /// @param ctxt the read context to use.
16237 ///
16238 /// @param die the DIE representing the variable we are looking at.
16239 ///
16240 /// @param where_offset the offset of the DIE where we are "logically"
16241 /// positionned at, in the DIE tree.  This is useful when @p die is
16242 /// e.g, DW_TAG_partial_unit that can be included in several places in
16243 /// the DIE tree.
16244 ///
16245 /// @param result if this is set to an existing var_decl, this means
16246 /// that the function will append the new properties it sees on @p die
16247 /// to that exising var_decl.  Otherwise, if this parameter is NULL, a
16248 /// new var_decl is going to be allocated and returned.
16249 ///
16250 /// @param is_required_decl_spec this is true iff the variable to
16251 /// build is referred to as being the specification of another
16252 /// variable.
16253 ///
16254 /// @return a pointer to the newly created var_decl.  If the var_decl
16255 /// could not be built, this function returns NULL.
16256 static var_decl_sptr
16257 build_or_get_var_decl_if_not_suppressed(read_context&   ctxt,
16258                                         scope_decl      *scope,
16259                                         Dwarf_Die       *die,
16260                                         size_t  where_offset,
16261                                         var_decl_sptr   result,
16262                                         bool is_required_decl_spec)
16263 {
16264   var_decl_sptr var;
16265   if (variable_is_suppressed(ctxt, scope, die, is_required_decl_spec))
16266     return var;
16267
16268   if (class_decl* class_type = is_class_type(scope))
16269     {
16270       string var_name = die_name(die);
16271       if (!var_name.empty())
16272         if ((var = class_type->find_data_member(var_name)))
16273           return var;
16274     }
16275   var = build_var_decl(ctxt, die, where_offset, result);
16276   return var;
16277 }
16278
16279 /// Create a variable symbol with a given name.
16280 ///
16281 /// @param sym_name the name of the variable symbol.
16282 ///
16283 /// @param env the environment to create the default symbol in.
16284 ///
16285 /// @return the newly created symbol.
16286 static elf_symbol_sptr
16287 create_default_var_sym(const string& sym_name, const environment *env)
16288 {
16289   elf_symbol::version ver;
16290   elf_symbol::visibility vis = elf_symbol::DEFAULT_VISIBILITY;
16291   elf_symbol_sptr result =
16292     elf_symbol::create(env,
16293                        /*symbol index=*/ 0,
16294                        /*symbol size=*/ 0,
16295                        sym_name,
16296                        /*symbol type=*/ elf_symbol::OBJECT_TYPE,
16297                        /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
16298                        /*symbol is defined=*/ true,
16299                        /*symbol is common=*/ false,
16300                        /*symbol version=*/ ver,
16301                        /*symbol_visibility=*/vis,
16302                        /*is_linux_string_cst=*/false);
16303   return result;
16304 }
16305
16306 /// Build a @ref var_decl out of a DW_TAG_variable DIE.
16307 ///
16308 /// @param ctxt the read context to use.
16309 ///
16310 /// @param die the DIE representing the variable we are looking at.
16311 ///
16312 /// @param where_offset the offset of the DIE where we are "logically"
16313 /// positionned at, in the DIE tree.  This is useful when @p die is
16314 /// e.g, DW_TAG_partial_unit that can be included in several places in
16315 /// the DIE tree.
16316 ///
16317 /// @param result if this is set to an existing var_decl, this means
16318 /// that the function will append the new properties it sees on @p die
16319 /// to that exising var_decl.  Otherwise, if this parameter is NULL, a
16320 /// new var_decl is going to be allocated and returned.
16321 ///
16322 /// @return a pointer to the newly created var_decl.  If the var_decl
16323 /// could not be built, this function returns NULL.
16324 static var_decl_sptr
16325 build_var_decl(read_context&    ctxt,
16326                Dwarf_Die        *die,
16327                size_t           where_offset,
16328                var_decl_sptr    result)
16329 {
16330   if (!die)
16331     return result;
16332
16333   int tag = dwarf_tag(die);
16334   ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
16335
16336   if (!die_is_public_decl(die))
16337     return result;
16338
16339   die_source source;
16340   ABG_ASSERT(ctxt.get_die_source(die, source));
16341
16342   type_base_sptr type;
16343   Dwarf_Die type_die;
16344   if (die_die_attribute(die, DW_AT_type, type_die))
16345     {
16346       decl_base_sptr ty =
16347         is_decl(build_ir_node_from_die(ctxt, &type_die,
16348                                        /*called_from_public_decl=*/true,
16349                                        where_offset));
16350       if (!ty)
16351         return result;
16352       type = is_type(ty);
16353       ABG_ASSERT(type);
16354     }
16355
16356   if (!type)
16357     return result;
16358
16359   string name, linkage_name;
16360   location loc;
16361   die_loc_and_name(ctxt, die, loc, name, linkage_name);
16362
16363   if (!result)
16364     result.reset(new var_decl(name, type, loc, linkage_name));
16365   else
16366     {
16367       // We were called to append properties that might have been
16368       // missing from the first version of the variable.  And usually
16369       // that missing property is the mangled name.
16370       if (!linkage_name.empty())
16371         result->set_linkage_name(linkage_name);
16372     }
16373
16374   // Check if a variable symbol with this name is exported by the elf
16375   // binary.  If it is, then set the symbol of the variable, if it's
16376   // not set already.
16377   if (!result->get_symbol())
16378     {
16379       elf_symbol_sptr var_sym;
16380       if (get_ignore_symbol_table(ctxt))
16381         {
16382           string var_name =
16383             result->get_linkage_name().empty()
16384             ? result->get_name()
16385             : result->get_linkage_name();
16386
16387           var_sym = create_default_var_sym(var_name, ctxt.env());
16388           ABG_ASSERT(var_sym);
16389           add_symbol_to_map(var_sym, ctxt.var_syms());
16390         }
16391       else
16392         {
16393           Dwarf_Addr var_addr;
16394           if (ctxt.get_variable_address(die, var_addr))
16395             var_sym = var_sym = ctxt.variable_symbol_is_exported(var_addr);
16396         }
16397
16398       if (var_sym)
16399         {
16400           result->set_symbol(var_sym);
16401           // If the linkage name is not set or is wrong, set it to
16402           // the name of the underlying symbol.
16403           string linkage_name = result->get_linkage_name();
16404           if (linkage_name.empty()
16405               || !var_sym->get_alias_from_name(linkage_name))
16406             result->set_linkage_name(var_sym->get_name());
16407           result->set_is_in_public_symbol_table(true);
16408         }
16409     }
16410
16411   return result;
16412 }
16413
16414 /// Test if a given function denoted by its DIE and its scope is
16415 /// suppressed by any of the suppression specifications associated to
16416 /// a given context of ELF/DWARF reading.
16417 ///
16418 /// Note that a non-member function which symbol is not exported is
16419 /// also suppressed.
16420 ///
16421 /// @param ctxt the ELF/DWARF reading content of interest.
16422 ///
16423 /// @param scope of the scope of the function.
16424 ///
16425 /// @param function_die the DIE representing the function.
16426 ///
16427 /// @return true iff @p function_die is suppressed by at least one
16428 /// suppression specification attached to the @p ctxt.
16429 static bool
16430 function_is_suppressed(const read_context& ctxt,
16431                        const scope_decl* scope,
16432                        Dwarf_Die *function_die)
16433 {
16434   if (function_die == 0
16435       || dwarf_tag(function_die) != DW_TAG_subprogram)
16436     return false;
16437
16438   string fname = die_string_attribute(function_die, DW_AT_name);
16439   string flinkage_name = die_linkage_name(function_die);
16440   string qualified_name = build_qualified_name(scope, fname);
16441
16442   // A non-member function which symbol is not exported is suppressed.
16443   if (!is_class_type(scope) && !die_is_declaration_only(function_die))
16444     {
16445       Dwarf_Addr fn_addr;
16446       elf_symbol_sptr fn_sym;
16447       if (!ctxt.get_function_address(function_die, fn_addr))
16448         return true;
16449       if (!get_ignore_symbol_table(ctxt))
16450         {
16451           // We were not instructed to ignore (avoid loading) the
16452           // symbol table, so we can rely on its presence to see if
16453           // the address corresponds to the address of an exported
16454           // function symbol.
16455           if (!ctxt.function_symbol_is_exported(fn_addr))
16456             return true;
16457         }
16458     }
16459
16460   return suppr::function_is_suppressed(ctxt, qualified_name,
16461                                        flinkage_name,
16462                                        /*require_drop_property=*/true);
16463 }
16464
16465 /// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
16466 /// function denoted by the DIE is not suppressed by a suppression
16467 /// specification associated to the current read context.
16468 ///
16469 /// Note that if a member function declaration with the same signature
16470 /// (pretty representation) as one of the DIE we are looking at
16471 /// exists, this function returns that existing function declaration.
16472 ///
16473 /// @param ctxt the read context to use.
16474 ///
16475 /// @param scope the scope of the function we are looking at.
16476 ///
16477 /// @param fn_die the DIE representing the function we are looking at.
16478 ///
16479 /// @param where_offset the offset of the DIE where we are "logically"
16480 /// positionned at, in the DIE tree.  This is useful when @p die is
16481 /// e.g, DW_TAG_partial_unit that can be included in several places in
16482 /// the DIE tree.
16483 ///
16484 /// @param result if this is set to an existing function_decl, this
16485 /// means that the function will append the new properties it sees on
16486 /// @p fn_die to that exising function_decl.  Otherwise, if this
16487 /// parameter is NULL, a new function_decl is going to be allocated
16488 /// and returned.
16489 ///
16490 /// @return a pointer to the newly created var_decl.  If the var_decl
16491 /// could not be built, this function returns NULL.
16492 static function_decl_sptr
16493 build_or_get_fn_decl_if_not_suppressed(read_context&      ctxt,
16494                                        scope_decl         *scope,
16495                                        Dwarf_Die          *fn_die,
16496                                        size_t             where_offset,
16497                                        function_decl_sptr result)
16498 {
16499   function_decl_sptr fn;
16500   if (function_is_suppressed(ctxt, scope, fn_die))
16501     return fn;
16502
16503   if (!result)
16504     if ((fn = is_function_decl(ctxt.lookup_artifact_from_die(fn_die))))
16505       {
16506         fn = maybe_finish_function_decl_reading(ctxt, fn_die, where_offset, fn);
16507         ctxt.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
16508         ctxt.associate_die_to_type(fn_die, fn->get_type(), where_offset);
16509         return fn;
16510       }
16511
16512   fn = build_function_decl(ctxt, fn_die, where_offset, result);
16513
16514   return fn;
16515 }
16516
16517 /// Test if a given variable denoted by its DIE and its scope is
16518 /// suppressed by any of the suppression specifications associated to
16519 /// a given context of ELF/DWARF reading.
16520 ///
16521 /// @param ctxt the ELF/DWARF reading content of interest.
16522 ///
16523 /// @param scope of the scope of the variable.
16524 ///
16525 /// @param variable_die the DIE representing the variable.
16526 ///
16527 /// @param is_required_decl_spec if true, means that the @p
16528 /// variable_die being considered is for a variable decl that is a
16529 /// specification for a concrete variable being built.
16530 ///
16531 /// @return true iff @p variable_die is suppressed by at least one
16532 /// suppression specification attached to the @p ctxt.
16533 static bool
16534 variable_is_suppressed(const read_context& ctxt,
16535                        const scope_decl* scope,
16536                        Dwarf_Die *variable_die,
16537                        bool is_required_decl_spec)
16538 {
16539   if (variable_die == 0
16540       || (dwarf_tag(variable_die) != DW_TAG_variable
16541           && dwarf_tag(variable_die) != DW_TAG_member))
16542     return false;
16543
16544   string name = die_string_attribute(variable_die, DW_AT_name);
16545   string linkage_name = die_linkage_name(variable_die);
16546   string qualified_name = build_qualified_name(scope, name);
16547
16548   // If a non member variable that is a declaration (has no exported
16549   // symbol), is not the specification of another concrete variable,
16550   // then it's suppressed.  This is a size optimization; it removes
16551   // useless declaration-only variables from the IR.
16552   //
16553   // Otherwise, if a non-member variable is the specification of
16554   // another concrete variable, then this function looks at
16555   // suppression specification specifications to know if its
16556   // suppressed.
16557   if (!is_class_type(scope) && !is_required_decl_spec)
16558     {
16559       Dwarf_Addr var_addr = 0;
16560       elf_symbol_sptr var_sym;
16561       if (!ctxt.get_variable_address(variable_die, var_addr))
16562         return true;
16563       if (!get_ignore_symbol_table(ctxt))
16564         {
16565           // We were not instructed to ignore (avoid loading) the
16566           // symbol table, so we can rely on its presence to see if
16567           // the address corresponds to the address of an exported
16568           // variable symbol.
16569           if (!ctxt.variable_symbol_is_exported(var_addr))
16570             return true;
16571         }
16572     }
16573
16574   return suppr::variable_is_suppressed(ctxt, qualified_name,
16575                                        linkage_name,
16576                                        /*require_drop_property=*/true);
16577 }
16578
16579 /// Test if a type (designated by a given DIE) in a given scope is
16580 /// suppressed by the suppression specifications that are associated
16581 /// to a given read context.
16582 ///
16583 /// @param ctxt the read context to consider.
16584 ///
16585 /// @param scope of the scope of the type DIE to consider.
16586 ///
16587 /// @param type_die the DIE that designates the type to consider.
16588 ///
16589 /// @param type_is_private out parameter.  If this function returns
16590 /// true (the type @p type_die is suppressed) and if the type was
16591 /// suppressed because it's private then this parameter is set to
16592 /// true.
16593 ///
16594 /// @return true iff the type designated by the DIE @p type_die, in
16595 /// the scope @p scope is suppressed by at the suppression
16596 /// specifications associated to the current read context.
16597 static bool
16598 type_is_suppressed(const read_context& ctxt,
16599                    const scope_decl* scope,
16600                    Dwarf_Die *type_die,
16601                    bool &type_is_private)
16602 {
16603   if (type_die == 0
16604       || (dwarf_tag(type_die) != DW_TAG_enumeration_type
16605           && dwarf_tag(type_die) != DW_TAG_class_type
16606           && dwarf_tag(type_die) != DW_TAG_structure_type
16607           && dwarf_tag(type_die) != DW_TAG_union_type))
16608     return false;
16609
16610   string type_name, linkage_name;
16611   location type_location;
16612   die_loc_and_name(ctxt, type_die, type_location, type_name, linkage_name);
16613   string qualified_name = build_qualified_name(scope, type_name);
16614
16615   return suppr::type_is_suppressed(ctxt, qualified_name,
16616                                    type_location,
16617                                    type_is_private,
16618                                    /*require_drop_property=*/true);
16619 }
16620
16621 /// Test if a type (designated by a given DIE) in a given scope is
16622 /// suppressed by the suppression specifications that are associated
16623 /// to a given read context.
16624 ///
16625 /// @param ctxt the read context to consider.
16626 ///
16627 /// @param scope of the scope of the type DIE to consider.
16628 ///
16629 /// @param type_die the DIE that designates the type to consider.
16630 ///
16631 /// @return true iff the type designated by the DIE @p type_die, in
16632 /// the scope @p scope is suppressed by at the suppression
16633 /// specifications associated to the current read context.
16634 static bool
16635 type_is_suppressed(const read_context& ctxt,
16636                    const scope_decl* scope,
16637                    Dwarf_Die *type_die)
16638 {
16639   bool type_is_private = false;
16640   return type_is_suppressed(ctxt, scope, type_die, type_is_private);
16641 }
16642
16643 /// Get the opaque version of a type that was suppressed because it's
16644 /// a private type.
16645 ///
16646 /// The opaque version version of the type is just a declared-only
16647 /// version of the type (class or union type) denoted by @p type_die.
16648 ///
16649 /// @param ctxt the read context in use.
16650 ///
16651 /// @param scope the scope of the type die we are looking at.
16652 ///
16653 /// @param type_die the type DIE we are looking at.
16654 ///
16655 /// @param where_offset the offset of the DIE where we are "logically"
16656 /// positionned at, in the DIE tree.  This is useful when @p die is
16657 /// e.g, DW_TAG_partial_unit that can be included in several places in
16658 /// the DIE tree.
16659 ///
16660 /// @return the opaque version of the type denoted by @p type_die or
16661 /// nil if no opaque version was found.
16662 static class_or_union_sptr
16663 get_opaque_version_of_type(read_context &ctxt,
16664                            scope_decl           *scope,
16665                            Dwarf_Die            *type_die,
16666                            size_t               where_offset)
16667 {
16668   class_or_union_sptr result;
16669
16670   if (type_die == 0)
16671     return result;
16672
16673   unsigned tag = dwarf_tag(type_die);
16674   if (tag != DW_TAG_class_type
16675       && tag != DW_TAG_structure_type
16676       && tag != DW_TAG_union_type)
16677     return result;
16678
16679   string type_name, linkage_name;
16680   location type_location;
16681   die_loc_and_name(ctxt, type_die, type_location, type_name, linkage_name);
16682   if (!type_location)
16683     return result;
16684
16685   string qualified_name = build_qualified_name(scope, type_name);
16686
16687   // TODO: also handle declaration-only unions.  To do that, we mostly
16688   // need to adapt add_or_update_union_type to make it schedule
16689   // declaration-only unions for resolution too.
16690   string_classes_map::const_iterator i =
16691     ctxt.declaration_only_classes().find(qualified_name);
16692   if (i != ctxt.declaration_only_classes().end())
16693     result = i->second.back();
16694
16695   if (!result)
16696     {
16697       if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
16698         {
16699           // So we didn't find any pre-existing forward-declared-only
16700           // class for the class definition that we could return as an
16701           // opaque type.  So let's build one.
16702           //
16703           // TODO: we need to be able to do this for unions too!
16704           class_decl_sptr klass(new class_decl(ctxt.env(), type_name,
16705                                                /*alignment=*/0, /*size=*/0,
16706                                                tag == DW_TAG_structure_type,
16707                                                type_location,
16708                                                decl_base::VISIBILITY_DEFAULT));
16709           klass->set_is_declaration_only(true);
16710           add_decl_to_scope(klass, scope);
16711           ctxt.associate_die_to_type(type_die, klass, where_offset);
16712           ctxt.maybe_schedule_declaration_only_class_for_resolution(klass);
16713           result = klass;
16714         }
16715     }
16716
16717   return result;
16718 }
16719
16720 /// Create a function symbol with a given name.
16721 ///
16722 /// @param sym_name the name of the symbol to create.
16723 ///
16724 /// @param env the environment to create the symbol in.
16725 ///
16726 /// @return the newly created symbol.
16727 elf_symbol_sptr
16728 create_default_fn_sym(const string& sym_name, const environment *env)
16729 {
16730   elf_symbol::version ver;
16731   elf_symbol_sptr result =
16732     elf_symbol::create(env,
16733                        /*symbol index=*/ 0,
16734                        /*symbol size=*/ 0,
16735                        sym_name,
16736                        /*symbol type=*/ elf_symbol::FUNC_TYPE,
16737                        /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
16738                        /*symbol is defined=*/ true,
16739                        /*symbol is common=*/ false,
16740                        /*symbol version=*/ ver,
16741                        /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY,
16742                        /*symbol is linux string cst=*/false);
16743   return result;
16744 }
16745
16746 /// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
16747 ///
16748 /// @param ctxt the read context to use
16749 ///
16750 /// @param die the DW_TAG_subprogram DIE to read from.
16751 ///
16752 /// @param where_offset the offset of the DIE where we are "logically"
16753 /// positionned at, in the DIE tree.  This is useful when @p die is
16754 /// e.g, DW_TAG_partial_unit that can be included in several places in
16755 /// the DIE tree.
16756 ///
16757 /// @param called_for_public_decl this is set to true if the function
16758 /// was called for a public (function) decl.
16759 static function_decl_sptr
16760 build_function_decl(read_context&       ctxt,
16761                     Dwarf_Die*          die,
16762                     size_t              where_offset,
16763                     function_decl_sptr  fn)
16764 {
16765   function_decl_sptr result = fn;
16766   if (!die)
16767     return result;
16768   ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
16769
16770   die_source source;
16771   ABG_ASSERT(ctxt.get_die_source(die, source));
16772
16773   if (!die_is_public_decl(die))
16774     return result;
16775
16776   translation_unit_sptr tu = ctxt.cur_transl_unit();
16777   ABG_ASSERT(tu);
16778
16779   string fname, flinkage_name;
16780   location floc;
16781   die_loc_and_name(ctxt, die, floc, fname, flinkage_name);
16782
16783   size_t is_inline = die_is_declared_inline(die);
16784   class_or_union_sptr is_method =
16785     is_class_or_union_type(get_scope_for_die(ctxt, die, true, where_offset));
16786
16787   if (result)
16788     {
16789       // Add the properties that might have been missing from the
16790       // first declaration of the function.  For now, it usually is
16791       // the mangled name that goes missing in the first declarations.
16792       //
16793       // Also note that if 'fn' has just been cloned, the current
16794       // linkage name (of the current DIE) might be different from the
16795       // linkage name of 'fn'.  In that case, update the linkage name
16796       // of 'fn' too.
16797       if (!flinkage_name.empty()
16798           && result->get_linkage_name() != flinkage_name)
16799         result->set_linkage_name(flinkage_name);
16800       if (floc)
16801         if (!result->get_location())
16802           result->set_location(floc);
16803     }
16804   else
16805     {
16806       function_type_sptr fn_type(build_function_type(ctxt, die, is_method,
16807                                                      where_offset));
16808       if (!fn_type)
16809         return result;
16810
16811       result.reset(is_method
16812                    ? new method_decl(fname, fn_type,
16813                                      is_inline, floc,
16814                                      flinkage_name)
16815                    : new function_decl(fname, fn_type,
16816                                        is_inline, floc,
16817                                        flinkage_name));
16818     }
16819
16820   // Set the symbol of the function.  If the linkage name is not set
16821   // or is wrong, set it to the name of the underlying symbol.
16822   if (!result->get_symbol())
16823     {
16824       elf_symbol_sptr fn_sym;
16825       if (get_ignore_symbol_table(ctxt))
16826         {
16827           string fn_name =
16828             result->get_linkage_name().empty()
16829             ? result->get_name()
16830             : result->get_linkage_name();
16831
16832           fn_sym = create_default_fn_sym(fn_name, ctxt.env());
16833           ABG_ASSERT(fn_sym);
16834           add_symbol_to_map(fn_sym, ctxt.fun_syms());
16835         }
16836       else
16837         {
16838           Dwarf_Addr fn_addr;
16839           if (ctxt.get_function_address(die, fn_addr))
16840             fn_sym = ctxt.function_symbol_is_exported(fn_addr);
16841         }
16842
16843       if (fn_sym)
16844         {
16845           result->set_symbol(fn_sym);
16846           string linkage_name = result->get_linkage_name();
16847           if (linkage_name.empty()
16848               || !fn_sym->get_alias_from_name(linkage_name))
16849             result->set_linkage_name(fn_sym->get_name());
16850           result->set_is_in_public_symbol_table(true);
16851         }
16852     }
16853
16854   ctxt.associate_die_to_type(die, result->get_type(), where_offset);
16855
16856   size_t die_offset = dwarf_dieoffset(die);
16857
16858   if (fn
16859       && is_member_function(fn)
16860       && get_member_function_is_virtual(fn)
16861       && !result->get_linkage_name().empty())
16862     // This function is a virtual member function which has its
16863     // linkage name *and* and has its underlying symbol correctly set.
16864     // It thus doesn't need any fixup related to elf symbol.  So
16865     // remove it from the set of virtual member functions with linkage
16866     // names and no elf symbol that need to be fixed up.
16867     ctxt.die_function_decl_with_no_symbol_map().erase(die_offset);
16868   return result;
16869 }
16870
16871 /// Add a set of addresses (representing function symbols) to a
16872 /// function symbol name -> symbol map.
16873 ///
16874 /// For a given symbol address, the function retrieves the name of the
16875 /// symbol as well as the symbol itself and inserts an entry {symbol
16876 /// name, symbol} into a map of symbol name -> symbol map.
16877 ///
16878 /// @param syms the set of symbol addresses to consider.
16879 ///
16880 /// @param map the map to populate.
16881 ///
16882 /// @param ctxt the context in which we are loading a given ELF file.
16883 static void
16884 add_fn_symbols_to_map(address_set_type& syms,
16885                       string_elf_symbols_map_type& map,
16886                       read_context& ctxt)
16887 {
16888   for (address_set_type::iterator i = syms.begin(); i != syms.end(); ++i)
16889     {
16890       elf_symbol_sptr sym = ctxt.lookup_elf_fn_symbol_from_address(*i);
16891       ABG_ASSERT(sym);
16892       string_elf_symbols_map_type::iterator it =
16893         ctxt.fun_syms().find(sym->get_name());
16894       ABG_ASSERT(it != ctxt.fun_syms().end());
16895       map.insert(*it);
16896     }
16897 }
16898
16899 /// Add a symbol to a symbol map.
16900 ///
16901 /// @param sym the symbol to add.
16902 ///
16903 /// @param map the symbol map to add the symbol into.
16904 static void
16905 add_symbol_to_map(const elf_symbol_sptr& sym,
16906                   string_elf_symbols_map_type& map)
16907 {
16908   if (!sym)
16909     return;
16910
16911   string_elf_symbols_map_type::iterator it = map.find(sym->get_name());
16912   if (it == map.end())
16913     {
16914       elf_symbols syms;
16915       syms.push_back(sym);
16916       map[sym->get_name()] = syms;
16917     }
16918   else
16919     it->second.push_back(sym);
16920 }
16921
16922 /// Add a set of addresses (representing variable symbols) to a
16923 /// variable symbol name -> symbol map.
16924 ///
16925 /// For a given symbol address, the variable retrieves the name of the
16926 /// symbol as well as the symbol itself and inserts an entry {symbol
16927 /// name, symbol} into a map of symbol name -> symbol map.
16928 ///
16929 /// @param syms the set of symbol addresses to consider.
16930 ///
16931 /// @param map the map to populate.
16932 ///
16933 /// @param ctxt the context in which we are loading a given ELF file.
16934 static void
16935 add_var_symbols_to_map(address_set_type& syms,
16936                        string_elf_symbols_map_type& map,
16937                        read_context& ctxt)
16938 {
16939   for (address_set_type::iterator i = syms.begin(); i != syms.end(); ++i)
16940     {
16941       elf_symbol_sptr sym = ctxt.lookup_elf_var_symbol_from_address(*i);
16942       ABG_ASSERT(sym);
16943       string_elf_symbols_map_type::iterator it =
16944         ctxt.var_syms().find(sym->get_name());
16945       ABG_ASSERT(it != ctxt.var_syms().end());
16946       map.insert(*it);
16947     }
16948 }
16949
16950 /// Read all @ref abigail::translation_unit possible from the debug info
16951 /// accessible through a DWARF Front End Library handle, and stuff
16952 /// them into a libabigail ABI Corpus.
16953 ///
16954 /// @param ctxt the read context.
16955 ///
16956 /// @return a pointer to the resulting corpus, or NULL if the corpus
16957 /// could not be constructed.
16958 static corpus_sptr
16959 read_debug_info_into_corpus(read_context& ctxt)
16960 {
16961   ctxt.clear_per_corpus_data();
16962
16963   if (!ctxt.current_corpus())
16964     {
16965       corpus_sptr corp (new corpus(ctxt.env(), ctxt.elf_path()));
16966       ctxt.current_corpus(corp);
16967       if (!ctxt.env())
16968         ctxt.env(corp->get_environment());
16969     }
16970
16971   // First set some mundane properties of the corpus gathered from
16972   // ELF.
16973   ctxt.current_corpus()->set_path(ctxt.elf_path());
16974   if (ctxt.is_linux_kernel_binary())
16975     ctxt.current_corpus()->set_origin(corpus::LINUX_KERNEL_BINARY_ORIGIN);
16976   else
16977     ctxt.current_corpus()->set_origin(corpus::DWARF_ORIGIN);
16978   ctxt.current_corpus()->set_soname(ctxt.dt_soname());
16979   ctxt.current_corpus()->set_needed(ctxt.dt_needed());
16980   ctxt.current_corpus()->set_architecture_name(ctxt.elf_architecture());
16981   if (corpus_group_sptr group = ctxt.current_corpus_group())
16982     group->add_corpus(ctxt.current_corpus());
16983
16984   // Set symbols information to the corpus.
16985   if (!get_ignore_symbol_table(ctxt))
16986     {
16987       if (ctxt.load_in_linux_kernel_mode() && ctxt.is_linux_kernel_binary())
16988         {
16989           string_elf_symbols_map_sptr exported_fn_symbols_map
16990             (new string_elf_symbols_map_type);
16991           add_fn_symbols_to_map(*ctxt.linux_exported_fn_syms(),
16992                                 *exported_fn_symbols_map,
16993                                 ctxt);
16994           add_fn_symbols_to_map(*ctxt.linux_exported_gpl_fn_syms(),
16995                                 *exported_fn_symbols_map,
16996                                 ctxt);
16997           ctxt.current_corpus()->set_fun_symbol_map(exported_fn_symbols_map);
16998
16999           string_elf_symbols_map_sptr exported_var_symbols_map
17000             (new string_elf_symbols_map_type);
17001           add_var_symbols_to_map(*ctxt.linux_exported_var_syms(),
17002                                  *exported_var_symbols_map,
17003                                  ctxt);
17004           add_var_symbols_to_map(*ctxt.linux_exported_gpl_var_syms(),
17005                                  *exported_var_symbols_map,
17006                                  ctxt);
17007           ctxt.current_corpus()->set_var_symbol_map(exported_var_symbols_map);
17008         }
17009       else
17010         {
17011           ctxt.current_corpus()->set_fun_symbol_map(ctxt.fun_syms_sptr());
17012           ctxt.current_corpus()->set_var_symbol_map(ctxt.var_syms_sptr());
17013         }
17014
17015       ctxt.current_corpus()->set_undefined_fun_symbol_map
17016         (ctxt.undefined_fun_syms_sptr());
17017       ctxt.current_corpus()->set_undefined_var_symbol_map
17018         (ctxt.undefined_var_syms_sptr());
17019     }
17020   else
17021     {
17022       ctxt.current_corpus()->set_fun_symbol_map(ctxt.fun_syms_sptr());
17023       ctxt.current_corpus()->set_var_symbol_map(ctxt.var_syms_sptr());
17024     }
17025
17026   // Get out now if no debug info is found.
17027   if (!ctxt.dwarf())
17028     return ctxt.current_corpus();
17029
17030   uint8_t address_size = 0;
17031   size_t header_size = 0;
17032
17033   // Set the set of exported declaration that are defined.
17034   ctxt.exported_decls_builder
17035     (ctxt.current_corpus()->get_exported_decls_builder().get());
17036
17037   // Walk all the DIEs of the debug info to build a DIE -> parent map
17038   // useful for get_die_parent() to work.
17039   {
17040     tools_utils::timer t;
17041     if (ctxt.do_log())
17042       {
17043         cerr << "building die -> parent maps ...";
17044         t.start();
17045       }
17046
17047     ctxt.build_die_parent_maps();
17048
17049     if (ctxt.do_log())
17050       {
17051         t.stop();
17052         cerr << " DONE@" << ctxt.current_corpus()->get_path()
17053              << ":"
17054              << t
17055              << "\n";
17056       }
17057   }
17058
17059   ctxt.env()->canonicalization_is_done(false);
17060
17061   {
17062     tools_utils::timer t;
17063     if (ctxt.do_log())
17064       {
17065         cerr << "building the libabigail internal representation ...";
17066         t.start();
17067       }
17068     // And now walk all the DIEs again to build the libabigail IR.
17069     Dwarf_Half dwarf_version = 0;
17070     for (Dwarf_Off offset = 0, next_offset = 0;
17071          (dwarf_next_unit(ctxt.dwarf(), offset, &next_offset, &header_size,
17072                           &dwarf_version, NULL, &address_size, NULL,
17073                           NULL, NULL) == 0);
17074          offset = next_offset)
17075       {
17076         Dwarf_Off die_offset = offset + header_size;
17077         Dwarf_Die unit;
17078         if (!dwarf_offdie(ctxt.dwarf(), die_offset, &unit)
17079             || dwarf_tag(&unit) != DW_TAG_compile_unit)
17080           continue;
17081
17082         ctxt.dwarf_version(dwarf_version);
17083
17084         address_size *= 8;
17085
17086         // Build a translation_unit IR node from cu; note that cu must
17087         // be a DW_TAG_compile_unit die.
17088         translation_unit_sptr ir_node =
17089           build_translation_unit_and_add_to_ir(ctxt, &unit, address_size);
17090         ABG_ASSERT(ir_node);
17091       }
17092     if (ctxt.do_log())
17093       {
17094         t.stop();
17095         cerr << " DONE@" << ctxt.current_corpus()->get_path()
17096              << ":"
17097              << t
17098              << "\n";
17099       }
17100   }
17101
17102   {
17103     tools_utils::timer t;
17104     if (ctxt.do_log())
17105       {
17106         cerr << "resolving declaration only classes ...";
17107         t.start();
17108       }
17109     ctxt.resolve_declaration_only_classes();
17110     if (ctxt.do_log())
17111       {
17112         t.stop();
17113         cerr << " DONE@" << ctxt.current_corpus()->get_path()
17114              << ":"
17115              << t
17116              <<"\n";
17117       }
17118   }
17119
17120   {
17121     tools_utils::timer t;
17122     if (ctxt.do_log())
17123       {
17124         cerr << "fixing up functions with linkage name but "
17125              << "no advertised underlying symbols ....";
17126         t.start();
17127       }
17128     ctxt.fixup_functions_with_no_symbols();
17129     if (ctxt.do_log())
17130       {
17131         t.stop();
17132         cerr << " DONE@" << ctxt.current_corpus()->get_path()
17133              <<":"
17134              << t
17135              <<"\n";
17136       }
17137   }
17138
17139   /// Now, look at the types that needs to be canonicalized after the
17140   /// translation has been constructed (which is just now) and
17141   /// canonicalize them.
17142   ///
17143   /// These types need to be constructed at the end of the translation
17144   /// unit reading phase because some types are modified by some DIEs
17145   /// even after the principal DIE describing the type has been read;
17146   /// this happens for clones of virtual destructors (for instance) or
17147   /// even for some static data members.  We need to do that for types
17148   /// are in the alternate debug info section and for types that in
17149   /// the main debug info section.
17150   {
17151     tools_utils::timer t;
17152     if (ctxt.do_log())
17153       {
17154         cerr << "perform late type canonicalizing ...\n";
17155         t.start();
17156       }
17157
17158     ctxt.perform_late_type_canonicalizing();
17159     if (ctxt.do_log())
17160       {
17161         t.stop();
17162         cerr << "late type canonicalizing DONE@"
17163              << ctxt.current_corpus()->get_path()
17164              << ":"
17165              << t
17166              << "\n";
17167       }
17168   }
17169
17170   ctxt.env()->canonicalization_is_done(true);
17171
17172   {
17173     tools_utils::timer t;
17174     if (ctxt.do_log())
17175       {
17176         cerr << "sort functions and variables ...";
17177         t.start();
17178       }
17179     ctxt.current_corpus()->sort_functions();
17180     ctxt.current_corpus()->sort_variables();
17181     if (ctxt.do_log())
17182       {
17183         t.stop();
17184         cerr << " DONE@" << ctxt.current_corpus()->get_path()
17185              << ":"
17186              << t
17187              <<" \n";
17188       }
17189   }
17190
17191   return ctxt.current_corpus();
17192 }
17193
17194 /// Canonicalize a type if it's suitable for early canonicalizing, or,
17195 /// if it's not, schedule it for late canonicalization, after the
17196 /// debug info of the current translation unit has been fully read.
17197 ///
17198 /// A (composite) type is deemed suitable for early canonicalizing iff
17199 /// all of its sub-types are canonicalized themselve.  Non composite
17200 /// types are always deemed suitable for early canonicalization.
17201 ///
17202 /// Note that this function doesn't work on *ANONYMOUS* classes,
17203 /// structs, unions or enums because it first does some
17204 /// canonicalization of the DWARF DIE @p die.  That canonicalization
17205 /// is done by looking up @p die by name; and because these are
17206 /// anonymous types, they don't have names! and so that
17207 /// canonicalization fails.  So the type artifact associated to @p
17208 /// die often ends being *NOT* canonicalized.  This later leads to
17209 /// extreme slowness of operation, especially when comparisons are
17210 /// later performed on these anonymous types.
17211 ///
17212 /// So when you have classes, structs, unions, or enums that can be
17213 /// anonymous, please use this overload instead:
17214 ///
17215 ///     void
17216 ///     maybe_canonicalize_type(const Dwarf_Die*        die,
17217 ///                             const type_base_sptr&   t,
17218 ///                             read_context&           ctxt);
17219 ///
17220 /// It knows how to deal with anonymous types.
17221 ///
17222 /// @p looks up the type artifact
17223 /// associated to @p die.  During that lookup, ; but then those types don't have
17224 /// names because they are anonymous.
17225 ///
17226 /// @param die the type DIE to consider for canonicalization.  Note
17227 /// that this DIE must have been associated with its type using the
17228 /// function read_context::associate_die_to_type() prior to calling
17229 /// this function.
17230 ///
17231 /// @param ctxt the @ref read_context to use.
17232 static void
17233 maybe_canonicalize_type(const Dwarf_Die *die, read_context& ctxt)
17234 {
17235   die_source source;
17236   ABG_ASSERT(ctxt.get_die_source(die, source));
17237
17238   size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
17239   type_base_sptr t = ctxt.lookup_type_from_die(die);
17240
17241   if (!t)
17242     return;
17243
17244   type_base_sptr peeled_type =
17245     peel_typedef_pointer_or_reference_type(t, /*peel_qual_types=*/false);
17246   if (is_class_type(peeled_type)
17247       || is_union_type(peeled_type)
17248       || is_function_type(peeled_type)
17249       || is_array_type(peeled_type)
17250       || is_qualified_type(peeled_type))
17251     // We delay canonicalization of classes/unions or typedef,
17252     // pointers, references and array to classes/unions.  This is
17253     // because the (underlying) class might not be finished yet and we
17254     // might not be able to able detect it here (thinking about
17255     // classes that are work-in-progress, or classes that might be
17256     // later amended by some DWARF construct).  So we err on the safe
17257     // side.  We also delay canonicalization for array and qualified
17258     // types because they can be edited (in particular by
17259     // maybe_strip_qualification) after they are initially built.
17260     ctxt.schedule_type_for_late_canonicalization(die);
17261   else if ((is_function_type(t)
17262             && ctxt.is_wip_function_type_die_offset(die_offset, source))
17263            || type_has_non_canonicalized_subtype(t))
17264     ctxt.schedule_type_for_late_canonicalization(die);
17265   else
17266     canonicalize(t);
17267 }
17268
17269 /// Canonicalize a type if it's suitable for early canonicalizing, or,
17270 /// if it's not, schedule it for late canonicalization, after the
17271 /// debug info of the current translation unit has been fully read.
17272 ///
17273 /// A (composite) type is deemed suitable for early canonicalizing iff
17274 /// all of its sub-types are canonicalized themselve.  Non composite
17275 /// types are always deemed suitable for early canonicalization.
17276 ///
17277 /// Note that this function nows how to deal with anonymous classes,
17278 /// structs and enums, unlike the overload below:
17279 ///
17280 ///     void maybe_canonicalize_type(const Dwarf_Die *die, read_context& ctxt)
17281 ///
17282 /// The problem, though is that this function is much slower that that
17283 /// overload above because of how the types that are meant for later
17284 /// canonicalization are stored.  So the idea is that this function
17285 /// should be used only for the smallest possible subset of types that
17286 /// are anonymous and thus cannot be handled by the overload above.
17287 ///
17288 /// @param t the type DIE to consider for canonicalization.
17289 ///
17290 /// @param ctxt the @ref read_context to use.
17291 static void
17292 maybe_canonicalize_type(const type_base_sptr& t,
17293                         read_context&   ctxt)
17294 {
17295   if (!t)
17296     return;
17297
17298   type_base_sptr peeled_type =
17299     peel_typedef_pointer_or_reference_type(t, /*peel_qual_types=*/false);
17300   if (is_class_type(peeled_type)
17301       || is_union_type(peeled_type)
17302       || is_function_type(peeled_type)
17303       || is_array_type(peeled_type)
17304       || is_qualified_type(peeled_type))
17305     // We delay canonicalization of classes/unions or typedef,
17306     // pointers, references and array to classes/unions.  This is
17307     // because the (underlying) class might not be finished yet and we
17308     // might not be able to able detect it here (thinking about
17309     // classes that are work-in-progress, or classes that might be
17310     // later amended by some DWARF construct).  So we err on the safe
17311     // side.  We also delay canonicalization for array and qualified
17312     // types because they can be edited (in particular by
17313     // maybe_strip_qualification) after they are initially built.
17314     ctxt.schedule_type_for_late_canonicalization(t);
17315   else if (type_has_non_canonicalized_subtype(t))
17316     ctxt.schedule_type_for_late_canonicalization(t);
17317   else
17318     canonicalize(t);
17319 }
17320
17321 /// Canonicalize a type if it's suitable for early canonicalizing, or,
17322 /// if it's not, schedule it for late canonicalization, after the
17323 /// debug info of the current translation unit has been fully read.
17324 ///
17325 /// A (composite) type is deemed suitable for early canonicalizing iff
17326 /// all of its sub-types are canonicalized themselve.  Non composite
17327 /// types are always deemed suitable for early canonicalization.
17328 ///
17329 /// Note that this function knows how to properly use either one of
17330 /// the following two overloads:
17331 ///
17332 ///     1/
17333 ///     void maybe_canonicalize_type(const Dwarf_Die*   die,
17334 ///                                  const type_base_sptr&      t,
17335 ///                                  read_context&              ctxt);
17336 ///
17337 ///     2/
17338 ///     void maybe_canonicalize_type(const Dwarf_Die *die, read_context& ctxt);
17339 ///
17340 /// So this function uses 1/ for most types and uses uses 2/ function
17341 /// types.  Using 2/ is slower and bigger than using 1/, but then 1/
17342 /// deals poorly with anonymous types because of how poorly DIEs
17343 /// canonicalization works on anonymous types.  That's why this
17344 /// function uses 2/ only for the types that really need it.
17345 ///
17346 /// @param die the DIE of the type denoted by @p t.
17347 ///
17348 /// @param t the type to consider.  Its DIE is @p die.
17349 ///
17350 /// @param ctxt the read context in use.
17351 static void
17352 maybe_canonicalize_type(const Dwarf_Die *die,
17353                         const type_base_sptr&   t,
17354                         read_context&           ctxt)
17355 {
17356   if (const function_type_sptr ft = is_function_type(t))
17357     {
17358       maybe_canonicalize_type(ft, ctxt);
17359       return;
17360     }
17361
17362   maybe_canonicalize_type(die, ctxt);
17363 }
17364
17365 /// If a given decl is a member type declaration, set its access
17366 /// specifier from the DIE that represents it.
17367 ///
17368 /// @param member_type_declaration the member type declaration to
17369 /// consider.
17370 static void
17371 maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
17372                                        Dwarf_Die* die)
17373 {
17374   if (is_type(member_type_declaration)
17375       && is_member_decl(member_type_declaration))
17376     {
17377       class_or_union* scope =
17378         is_class_or_union_type(member_type_declaration->get_scope());
17379       ABG_ASSERT(scope);
17380
17381       access_specifier access = private_access;
17382       if (class_decl* cl = is_class_type(scope))
17383         if (cl->is_struct())
17384           access = public_access;
17385
17386       die_access_specifier(die, access);
17387       set_member_access_specifier(member_type_declaration, access);
17388     }
17389 }
17390
17391 /// Build an IR node from a given DIE and add the node to the current
17392 /// IR being build and held in the read_context.  Doing that is called
17393 /// "emitting an IR node for the DIE".
17394 ///
17395 /// @param ctxt the read context.
17396 ///
17397 /// @param die the DIE to consider.
17398 ///
17399 /// @param scope the scope under which the resulting IR node has to be
17400 /// added.
17401 ///
17402 /// @param called_from_public_decl set to yes if this function is
17403 /// called from the functions used to build a public decl (functions
17404 /// and variables).  In that case, this function accepts building IR
17405 /// nodes representing types.  Otherwise, this function only creates
17406 /// IR nodes representing public decls (functions and variables).
17407 /// This is done to avoid emitting IR nodes for types that are not
17408 /// referenced by public functions or variables.
17409 ///
17410 /// @param where_offset the offset of the DIE where we are "logically"
17411 /// positionned at, in the DIE tree.  This is useful when @p die is
17412 /// e.g, DW_TAG_partial_unit that can be included in several places in
17413 /// the DIE tree.
17414 ///
17415 /// @param is_required_decl_spec if true, it means the ir node to
17416 /// build is for a decl that is a specification for another decl that
17417 /// is concrete.  If you don't know what this is, set it to false.
17418 ///
17419 /// @return the resulting IR node.
17420 static type_or_decl_base_sptr
17421 build_ir_node_from_die(read_context&    ctxt,
17422                        Dwarf_Die*       die,
17423                        scope_decl*      scope,
17424                        bool             called_from_public_decl,
17425                        size_t           where_offset,
17426                        bool             is_required_decl_spec)
17427 {
17428   type_or_decl_base_sptr result;
17429
17430   if (!die || !scope)
17431     return result;
17432
17433   int tag = dwarf_tag(die);
17434
17435   if (!called_from_public_decl)
17436     {
17437       if (ctxt.load_all_types() && die_is_type(die))
17438         /* We were instructed to load debug info for all types,
17439            included those that are not reachable from a public
17440            declaration.  So load the debug info for this type.  */;
17441       else if (tag != DW_TAG_subprogram
17442                && tag != DW_TAG_variable
17443                && tag != DW_TAG_member
17444                && tag != DW_TAG_namespace)
17445         return result;
17446     }
17447
17448   die_source source_of_die;
17449   ABG_ASSERT(ctxt.get_die_source(die, source_of_die));
17450
17451   if ((result = ctxt.lookup_decl_from_die_offset(dwarf_dieoffset(die),
17452                                                  source_of_die)))
17453     return result;
17454
17455   switch (tag)
17456     {
17457       // Type DIEs we support.
17458     case DW_TAG_base_type:
17459       if (type_decl_sptr t = build_type_decl(ctxt, die, where_offset))
17460         {
17461           result =
17462             add_decl_to_scope(t, ctxt.cur_transl_unit()->get_global_scope());
17463           canonicalize(t);
17464         }
17465       break;
17466
17467     case DW_TAG_typedef:
17468       {
17469         typedef_decl_sptr t = build_typedef_type(ctxt, die,
17470                                                  called_from_public_decl,
17471                                                  where_offset);
17472         result = add_decl_to_scope(t, scope);
17473         if (result)
17474           {
17475             maybe_set_member_type_access_specifier(is_decl(result), die);
17476             maybe_canonicalize_type(die, ctxt);
17477           }
17478       }
17479       break;
17480
17481     case DW_TAG_pointer_type:
17482       {
17483         pointer_type_def_sptr p =
17484           build_pointer_type_def(ctxt, die,
17485                                  called_from_public_decl,
17486                                  where_offset);
17487         if (p)
17488           {
17489             result =
17490               add_decl_to_scope(p, ctxt.cur_transl_unit()->get_global_scope());
17491             ABG_ASSERT(result->get_translation_unit());
17492             maybe_canonicalize_type(die, ctxt);
17493           }
17494       }
17495       break;
17496
17497     case DW_TAG_reference_type:
17498     case DW_TAG_rvalue_reference_type:
17499       {
17500         reference_type_def_sptr r =
17501           build_reference_type(ctxt, die,
17502                                called_from_public_decl,
17503                                where_offset);
17504         if (r)
17505           {
17506             result =
17507               add_decl_to_scope(r, ctxt.cur_transl_unit()->get_global_scope());
17508
17509             ctxt.associate_die_to_type(die, r, where_offset);
17510             maybe_canonicalize_type(die, ctxt);
17511           }
17512       }
17513       break;
17514
17515     case DW_TAG_const_type:
17516     case DW_TAG_volatile_type:
17517     case DW_TAG_restrict_type:
17518       {
17519         type_base_sptr q =
17520           build_qualified_type(ctxt, die,
17521                                called_from_public_decl,
17522                                where_offset);
17523         if (q)
17524           {
17525             // Strip some potentially redundant type qualifiers from
17526             // the qualified type we just built.
17527             decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
17528                                                          ctxt);
17529             if (!d)
17530               d = get_type_declaration(q);
17531             ABG_ASSERT(d);
17532             type_base_sptr ty = is_type(d);
17533             // Associate the die to type ty again because 'ty'might be
17534             // different from 'q', because 'ty' is 'q' possibly
17535             // stripped from some redundant type qualifier.
17536             ctxt.associate_die_to_type(die, ty, where_offset);
17537             result =
17538               add_decl_to_scope(d, ctxt.cur_transl_unit()->get_global_scope());
17539             maybe_canonicalize_type(die, ctxt);
17540           }
17541       }
17542       break;
17543
17544     case DW_TAG_enumeration_type:
17545       {
17546         if (!type_is_suppressed(ctxt, scope, die))
17547           {
17548             enum_type_decl_sptr e = build_enum_type(ctxt, die, scope,
17549                                                     where_offset);
17550             result = add_decl_to_scope(e, scope);
17551             if (result)
17552               {
17553                 maybe_set_member_type_access_specifier(is_decl(result), die);
17554                 maybe_canonicalize_type(die, ctxt);
17555               }
17556           }
17557       }
17558       break;
17559
17560     case DW_TAG_class_type:
17561     case DW_TAG_structure_type:
17562       {
17563         bool type_is_private = false;
17564         bool type_suppressed=
17565           type_is_suppressed(ctxt, scope, die, type_is_private);
17566
17567         if (type_suppressed && type_is_private)
17568           // The type is suppressed because it's private.  If other
17569           // non-suppressed and declaration-only instances of this
17570           // type exist in the current corpus, then it means those
17571           // non-suppressed instances are opaque versions of the
17572           // suppressed private type.  Lets return one of these opaque
17573           // types then.
17574           result = get_opaque_version_of_type(ctxt, scope, die, where_offset);
17575         else if (!type_suppressed)
17576           {
17577             Dwarf_Die spec_die;
17578             scope_decl_sptr scop;
17579             class_decl_sptr klass;
17580             if (die_die_attribute(die, DW_AT_specification, spec_die))
17581               {
17582                 scope_decl_sptr skope =
17583                   get_scope_for_die(ctxt, &spec_die,
17584                                     called_from_public_decl,
17585                                     where_offset);
17586                 ABG_ASSERT(skope);
17587                 decl_base_sptr cl =
17588                   is_decl(build_ir_node_from_die(ctxt, &spec_die,
17589                                                  skope.get(),
17590                                                  called_from_public_decl,
17591                                                  where_offset));
17592                 ABG_ASSERT(cl);
17593                 klass = dynamic_pointer_cast<class_decl>(cl);
17594                 ABG_ASSERT(klass);
17595
17596                 klass =
17597                   add_or_update_class_type(ctxt, die,
17598                                            skope.get(),
17599                                            tag == DW_TAG_structure_type,
17600                                            klass,
17601                                            called_from_public_decl,
17602                                            where_offset);
17603               }
17604             else
17605               klass =
17606                 add_or_update_class_type(ctxt, die, scope,
17607                                          tag == DW_TAG_structure_type,
17608                                          class_decl_sptr(),
17609                                          called_from_public_decl,
17610                                          where_offset);
17611             result = klass;
17612             if (klass)
17613               {
17614                 maybe_set_member_type_access_specifier(klass, die);
17615                 maybe_canonicalize_type(die, klass, ctxt);
17616               }
17617           }
17618       }
17619       break;
17620     case DW_TAG_union_type:
17621       if (!type_is_suppressed(ctxt, scope, die))
17622         {
17623           union_decl_sptr union_type =
17624             add_or_update_union_type(ctxt, die, scope,
17625                                      union_decl_sptr(),
17626                                      called_from_public_decl,
17627                                      where_offset);
17628           if (union_type)
17629             {
17630               maybe_set_member_type_access_specifier(union_type, die);
17631               maybe_canonicalize_type(die, union_type, ctxt);
17632             }
17633           result = union_type;
17634         }
17635       break;
17636     case DW_TAG_string_type:
17637       break;
17638     case DW_TAG_subroutine_type:
17639       {
17640         function_type_sptr f = build_function_type(ctxt, die,
17641                                                    class_decl_sptr(),
17642                                                    where_offset);
17643         if (f)
17644           {
17645             result = f;
17646             maybe_canonicalize_type(die, ctxt);
17647           }
17648       }
17649       break;
17650     case DW_TAG_array_type:
17651       {
17652         array_type_def_sptr a = build_array_type(ctxt,
17653                                                  die,
17654                                                  called_from_public_decl,
17655                                                  where_offset);
17656         if (a)
17657           {
17658             result =
17659               add_decl_to_scope(a, ctxt.cur_transl_unit()->get_global_scope());
17660             ctxt.associate_die_to_type(die, a, where_offset);
17661             maybe_canonicalize_type(die, ctxt);
17662           }
17663         break;
17664       }
17665     case DW_TAG_subrange_type:
17666       {
17667         // If we got here, this means the subrange type is a "free
17668         // form" defined in the global namespace of the current
17669         // translation unit, like what is found in Ada.
17670         array_type_def::subrange_sptr s =
17671           build_subrange_type(ctxt, die, where_offset);
17672         if (s)
17673           {
17674             result =
17675               add_decl_to_scope(s, ctxt.cur_transl_unit()->get_global_scope());
17676             ctxt.associate_die_to_type(die, s, where_offset);
17677             maybe_canonicalize_type(die, ctxt);
17678           }
17679       }
17680       break;
17681     case DW_TAG_packed_type:
17682       break;
17683     case DW_TAG_set_type:
17684       break;
17685     case DW_TAG_file_type:
17686       break;
17687     case DW_TAG_ptr_to_member_type:
17688       break;
17689     case DW_TAG_thrown_type:
17690       break;
17691     case DW_TAG_interface_type:
17692       break;
17693     case DW_TAG_unspecified_type:
17694       break;
17695     case DW_TAG_shared_type:
17696       break;
17697
17698     case DW_TAG_compile_unit:
17699       // We shouldn't reach this point b/c this should be handled by
17700       // build_translation_unit.
17701       ABG_ASSERT_NOT_REACHED;
17702
17703     case DW_TAG_namespace:
17704     case DW_TAG_module:
17705       result = build_namespace_decl_and_add_to_ir(ctxt, die, where_offset);
17706       break;
17707
17708     case DW_TAG_variable:
17709     case DW_TAG_member:
17710       {
17711         Dwarf_Die spec_die;
17712         bool var_is_cloned = false;
17713
17714         if (tag == DW_TAG_member)
17715           ABG_ASSERT(!is_c_language(ctxt.cur_transl_unit()->get_language()));
17716
17717         if (die_die_attribute(die, DW_AT_specification, spec_die,false)
17718             || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
17719                                                   spec_die, false)))
17720           {
17721             scope_decl_sptr spec_scope = get_scope_for_die(ctxt, &spec_die,
17722                                                            called_from_public_decl,
17723                                                            where_offset);
17724             if (spec_scope)
17725               {
17726                 decl_base_sptr d =
17727                   is_decl(build_ir_node_from_die(ctxt, &spec_die,
17728                                                  spec_scope.get(),
17729                                                  called_from_public_decl,
17730                                                  where_offset,
17731                                                  /*is_required_decl_spec=*/true));
17732                 if (d)
17733                   {
17734                     var_decl_sptr m =
17735                       dynamic_pointer_cast<var_decl>(d);
17736                     if (var_is_cloned)
17737                       m = m->clone();
17738                     m = build_var_decl(ctxt, die, where_offset, m);
17739                     if (is_data_member(m))
17740                       {
17741                         set_member_is_static(m, true);
17742                         ctxt.associate_die_to_decl(die, m, where_offset,
17743                                                    /*associate_by_repr=*/false);
17744                       }
17745                     else
17746                       {
17747                         ABG_ASSERT(has_scope(m));
17748                         ctxt.var_decls_to_re_add_to_tree().push_back(m);
17749                       }
17750                     ABG_ASSERT(m->get_scope());
17751                     ctxt.maybe_add_var_to_exported_decls(m.get());
17752                     return m;
17753                   }
17754               }
17755           }
17756         else if (var_decl_sptr v =
17757                  build_or_get_var_decl_if_not_suppressed(ctxt, scope, die,
17758                                                          where_offset,
17759                                                          /*result=*/var_decl_sptr(),
17760                                                          is_required_decl_spec))
17761           {
17762             result = add_decl_to_scope(v, scope);
17763             ABG_ASSERT(is_decl(result)->get_scope());
17764             v = dynamic_pointer_cast<var_decl>(result);
17765             ABG_ASSERT(v);
17766             ABG_ASSERT(v->get_scope());
17767             ctxt.var_decls_to_re_add_to_tree().push_back(v);
17768             ctxt.maybe_add_var_to_exported_decls(v.get());
17769           }
17770       }
17771       break;
17772
17773     case DW_TAG_subprogram:
17774       {
17775         Dwarf_Die spec_die;
17776         Dwarf_Die abstract_origin_die;
17777         Dwarf_Die *interface_die = 0, *origin_die = 0;
17778         scope_decl_sptr interface_scope;
17779         if (die_is_artificial(die))
17780           break;
17781
17782         function_decl_sptr fn;
17783         bool has_spec = die_die_attribute(die, DW_AT_specification,
17784                                           spec_die, true);
17785         bool has_abstract_origin =
17786           die_die_attribute(die, DW_AT_abstract_origin,
17787                             abstract_origin_die, true);
17788         if (has_spec || has_abstract_origin)
17789           {
17790             interface_die =
17791               has_spec
17792               ? &spec_die
17793               : &abstract_origin_die;
17794             origin_die =
17795               has_abstract_origin
17796               ? &abstract_origin_die
17797               : &spec_die;
17798
17799             string linkage_name = die_linkage_name(die);
17800             string spec_linkage_name = die_linkage_name(interface_die);
17801
17802             interface_scope = get_scope_for_die(ctxt, interface_die,
17803                                                 called_from_public_decl,
17804                                                 where_offset);
17805             if (interface_scope)
17806               {
17807                 decl_base_sptr d =
17808                   is_decl(build_ir_node_from_die(ctxt,
17809                                                  origin_die,
17810                                                  interface_scope.get(),
17811                                                  called_from_public_decl,
17812                                                  where_offset));
17813                 if (d)
17814                   {
17815                     fn = dynamic_pointer_cast<function_decl>(d);
17816                     if (has_abstract_origin
17817                         && (linkage_name != spec_linkage_name))
17818                       // The current DIE has 'd' as abstract orign,
17819                       // and has a linkage name that is different
17820                       // from from the linkage name of 'd'.  That
17821                       // means, the current DIE represents a clone
17822                       // of 'd'.
17823                       fn = fn->clone();
17824                   }
17825               }
17826           }
17827         ctxt.scope_stack().push(scope);
17828
17829         scope_decl* logical_scope =
17830           interface_scope
17831           ? interface_scope.get()
17832           : scope;
17833
17834         result = build_or_get_fn_decl_if_not_suppressed(ctxt, logical_scope,
17835                                                         die, where_offset, fn);
17836
17837         if (result && !fn)
17838           result = add_decl_to_scope(is_decl(result), logical_scope);
17839
17840         fn = is_function_decl(result);
17841         if (fn && is_member_function(fn))
17842           {
17843             class_decl_sptr klass(static_cast<class_decl*>(logical_scope),
17844                                   sptr_utils::noop_deleter());
17845             ABG_ASSERT(klass);
17846             finish_member_function_reading(die, fn, klass, ctxt);
17847           }
17848
17849         if (fn)
17850           {
17851             ctxt.maybe_add_fn_to_exported_decls(fn.get());
17852             ctxt.associate_die_to_decl(die, fn, where_offset,
17853                                        /*associate_by_repr=*/false);
17854             maybe_canonicalize_type(die, ctxt);
17855           }
17856
17857         ctxt.scope_stack().pop();
17858       }
17859       break;
17860
17861     case DW_TAG_formal_parameter:
17862       // We should not read this case as it should have been dealt
17863       // with by build_function_decl above.
17864       ABG_ASSERT_NOT_REACHED;
17865
17866     case DW_TAG_constant:
17867       break;
17868     case DW_TAG_enumerator:
17869       break;
17870
17871     case DW_TAG_partial_unit:
17872     case DW_TAG_imported_unit:
17873       // For now, the DIEs under these are read lazily when they are
17874       // referenced by a public decl DIE that is under a
17875       // DW_TAG_compile_unit, so we shouldn't get here.
17876       ABG_ASSERT_NOT_REACHED;
17877
17878       // Other declaration we don't really intend to support yet.
17879     case DW_TAG_dwarf_procedure:
17880     case DW_TAG_imported_declaration:
17881     case DW_TAG_entry_point:
17882     case DW_TAG_label:
17883     case DW_TAG_lexical_block:
17884     case DW_TAG_unspecified_parameters:
17885     case DW_TAG_variant:
17886     case DW_TAG_common_block:
17887     case DW_TAG_common_inclusion:
17888     case DW_TAG_inheritance:
17889     case DW_TAG_inlined_subroutine:
17890     case DW_TAG_with_stmt:
17891     case DW_TAG_access_declaration:
17892     case DW_TAG_catch_block:
17893     case DW_TAG_friend:
17894     case DW_TAG_namelist:
17895     case DW_TAG_namelist_item:
17896     case DW_TAG_template_type_parameter:
17897     case DW_TAG_template_value_parameter:
17898     case DW_TAG_try_block:
17899     case DW_TAG_variant_part:
17900     case DW_TAG_imported_module:
17901     case DW_TAG_condition:
17902     case DW_TAG_type_unit:
17903     case DW_TAG_template_alias:
17904     case DW_TAG_lo_user:
17905     case DW_TAG_MIPS_loop:
17906     case DW_TAG_format_label:
17907     case DW_TAG_function_template:
17908     case DW_TAG_class_template:
17909     case DW_TAG_GNU_BINCL:
17910     case DW_TAG_GNU_EINCL:
17911     case DW_TAG_GNU_template_template_param:
17912     case DW_TAG_GNU_template_parameter_pack:
17913     case DW_TAG_GNU_formal_parameter_pack:
17914     case DW_TAG_GNU_call_site:
17915     case DW_TAG_GNU_call_site_parameter:
17916     case DW_TAG_hi_user:
17917     default:
17918       break;
17919     }
17920
17921   if (result && tag != DW_TAG_subroutine_type)
17922     ctxt.associate_die_to_decl(die, is_decl(result), where_offset,
17923                                /*associate_by_repr=*/false);
17924
17925   return result;
17926 }
17927
17928 ///  Build the IR node for a void type.
17929 ///
17930 ///  @param ctxt the read context to use.
17931 ///
17932 ///  @return the void type node.
17933 static decl_base_sptr
17934 build_ir_node_for_void_type(read_context& ctxt)
17935 {
17936   ir::environment* env = ctxt.env();
17937   ABG_ASSERT(env);
17938   type_base_sptr t = env->get_void_type();
17939   decl_base_sptr type_declaration = get_type_declaration(t);
17940   if (!has_scope(type_declaration))
17941     add_decl_to_scope(type_declaration,
17942                       ctxt.cur_transl_unit()->get_global_scope());
17943   canonicalize(t);
17944   return type_declaration;
17945 }
17946
17947 /// Build an IR node from a given DIE and add the node to the current
17948 /// IR being build and held in the read_context.  Doing that is called
17949 /// "emitting an IR node for the DIE".
17950 ///
17951 /// @param ctxt the read context.
17952 ///
17953 /// @param die the DIE to consider.
17954 ///
17955 /// @param called_from_public_decl set to yes if this function is
17956 /// called from the functions used to build a public decl (functions
17957 /// and variables).  In that case, this function accepts building IR
17958 /// nodes representing types.  Otherwise, this function only creates
17959 /// IR nodes representing public decls (functions and variables).
17960 /// This is done to avoid emitting IR nodes for types that are not
17961 /// referenced by public functions or variables.
17962 ///
17963 /// @param where_offset the offset of the DIE where we are "logically"
17964 /// positionned at, in the DIE tree.  This is useful when @p die is
17965 /// e.g, DW_TAG_partial_unit that can be included in several places in
17966 /// the DIE tree.
17967 ///
17968 /// @return the resulting IR node.
17969 static type_or_decl_base_sptr
17970 build_ir_node_from_die(read_context&    ctxt,
17971                        Dwarf_Die*       die,
17972                        bool             called_from_public_decl,
17973                        size_t           where_offset)
17974 {
17975   if (!die)
17976     return decl_base_sptr();
17977
17978   if (is_c_language(ctxt.cur_transl_unit()->get_language()))
17979     {
17980       const scope_decl_sptr& scop = ctxt.global_scope();
17981       return build_ir_node_from_die(ctxt, die, scop.get(),
17982                                     called_from_public_decl,
17983                                     where_offset);
17984     }
17985
17986   scope_decl_sptr scope = get_scope_for_die(ctxt, die,
17987                                             called_from_public_decl,
17988                                             where_offset);
17989   return build_ir_node_from_die(ctxt, die, scope.get(),
17990                                 called_from_public_decl,
17991                                 where_offset);
17992 }
17993
17994 status
17995 operator|(status l, status r)
17996 {
17997   return static_cast<status>(static_cast<unsigned>(l)
17998                              | static_cast<unsigned>(r));
17999 }
18000
18001 status
18002 operator&(status l, status r)
18003 {
18004   return static_cast<status>(static_cast<unsigned>(l)
18005                              & static_cast<unsigned>(r));
18006 }
18007
18008 status&
18009 operator|=(status& l, status r)
18010 {
18011   l = l | r;
18012   return l;
18013 }
18014
18015 status&
18016 operator&=(status& l, status r)
18017 {
18018   l = l & r;
18019   return l;
18020 }
18021
18022 /// Emit a diagnostic status with english sentences to describe the
18023 /// problems encoded in a given abigail::dwarf_reader::status, if
18024 /// there is an error.
18025 ///
18026 /// @param status the status to diagnose
18027 ///
18028 /// @return a string containing sentences that describe the possible
18029 /// errors encoded in @p s.  If there is no error to encode, then the
18030 /// empty string is returned.
18031 string
18032 status_to_diagnostic_string(status s)
18033 {
18034   string str;
18035
18036   if (s & STATUS_DEBUG_INFO_NOT_FOUND)
18037     str += "could not find debug info\n";
18038
18039   if (s & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
18040     str += "could not find alternate debug info\n";
18041
18042   if (s & STATUS_NO_SYMBOLS_FOUND)
18043     str += "could not load ELF symbols\n";
18044
18045   return str;
18046 }
18047
18048 /// Create a dwarf_reader::read_context.
18049 ///
18050 /// @param elf_path the path to the elf file the context is to be used for.
18051 ///
18052 /// @param debug_info_root_paths a pointer to the path to the root
18053 /// directory under which the debug info is to be found for @p
18054 /// elf_path.  Leave this to NULL if the debug info is not in a split
18055 /// file.
18056 ///
18057 /// @param environment the environment used by the current context.
18058 /// This environment contains resources needed by the reader and by
18059 /// the types and declarations that are to be created later.  Note
18060 /// that ABI artifacts that are to be compared all need to be created
18061 /// within the same environment.
18062 ///
18063 /// Please also note that the life time of this environment object
18064 /// must be greater than the life time of the resulting @ref
18065 /// read_context the context uses resources that are allocated in the
18066 /// environment.
18067 ///
18068 /// @param load_all_types if set to false only the types that are
18069 /// reachable from publicly exported declarations (of functions and
18070 /// variables) are read.  If set to true then all types found in the
18071 /// debug information are loaded.
18072 ///
18073 /// @param linux_kernel_mode if set to true, then consider the special
18074 /// linux kernel symbol tables when determining if a symbol is
18075 /// exported or not.
18076 ///
18077 /// @return a smart pointer to the resulting dwarf_reader::read_context.
18078 read_context_sptr
18079 create_read_context(const std::string&          elf_path,
18080                     const vector<char**>&       debug_info_root_paths,
18081                     ir::environment*            environment,
18082                     bool                        load_all_types,
18083                     bool                        linux_kernel_mode)
18084 {
18085   // Create a DWARF Front End Library handle to be used by functions
18086   // of that library.
18087   read_context_sptr result(new read_context(elf_path, debug_info_root_paths,
18088                                             environment, load_all_types,
18089                                             linux_kernel_mode));
18090   return result;
18091 }
18092
18093 /// Getter for the path to the binary this @ref read_context is for.
18094 ///
18095 /// @return the path to the binary the @ref read_context is for.
18096 const string&
18097 read_context_get_path(const read_context& ctxt)
18098 {return ctxt.elf_path();}
18099
18100 /// Re-initialize a read_context so that it can re-used to read
18101 /// another binary.
18102 ///
18103 /// @param ctxt the context to re-initialize.
18104 ///
18105 /// @param elf_path the path to the elf file the context is to be used
18106 /// for.
18107 ///
18108 /// @param debug_info_root_path a pointer to the path to the root
18109 /// directory under which the debug info is to be found for @p
18110 /// elf_path.  Leave this to NULL if the debug info is not in a split
18111 /// file.
18112 ///
18113 /// @param environment the environment used by the current context.
18114 /// This environment contains resources needed by the reader and by
18115 /// the types and declarations that are to be created later.  Note
18116 /// that ABI artifacts that are to be compared all need to be created
18117 /// within the same environment.
18118 ///
18119 /// Please also note that the life time of this environment object
18120 /// must be greater than the life time of the resulting @ref
18121 /// read_context the context uses resources that are allocated in the
18122 /// environment.
18123 ///
18124 /// @param load_all_types if set to false only the types that are
18125 /// reachable from publicly exported declarations (of functions and
18126 /// variables) are read.  If set to true then all types found in the
18127 /// debug information are loaded.
18128 ///
18129 /// @param linux_kernel_mode if set to true, then consider the special
18130 /// linux kernel symbol tables when determining if a symbol is
18131 /// exported or not.
18132 ///
18133 /// @return a smart pointer to the resulting dwarf_reader::read_context.
18134 void
18135 reset_read_context(read_context_sptr    &ctxt,
18136                    const std::string&    elf_path,
18137                    const vector<char**>& debug_info_root_path,
18138                    ir::environment*      environment,
18139                    bool          read_all_types,
18140                    bool          linux_kernel_mode)
18141 {
18142   if (ctxt)
18143     ctxt->initialize(elf_path, debug_info_root_path, environment,
18144                      read_all_types, linux_kernel_mode);
18145 }
18146
18147 /// Add suppressions specifications to the set of suppressions to be
18148 /// used during the construction of the ABI internal representation
18149 /// (the ABI corpus) from ELF and DWARF.
18150 ///
18151 /// During the construction of the ABI corpus, ABI artifacts that
18152 /// match the a given suppression specification are dropped on the
18153 /// floor; that is, they are discarded and won't be part of the final
18154 /// ABI corpus.  This is a way to reduce the amount of data held by
18155 /// the final ABI corpus.
18156 ///
18157 /// Note that the suppression specifications provided to this function
18158 /// are only considered during the construction of the ABI corpus.
18159 /// For instance, they are not taken into account during e.g
18160 /// comparisons of two ABI corpora that might happen later.  If you
18161 /// want to apply suppression specificatins to the comparison (or
18162 /// reporting) of ABI corpora please refer to the documentation of the
18163 /// @ref diff_context type to learn how to set suppressions that are
18164 /// to be used in that context.
18165 ///
18166 /// @param ctxt the context that is going to be used by functions that
18167 /// read ELF and DWARF information to construct and ABI corpus.
18168 ///
18169 /// @param supprs the suppression specifications to be applied during
18170 /// the construction of the ABI corpus.
18171 void
18172 add_read_context_suppressions(read_context& ctxt,
18173                               const suppr::suppressions_type& supprs)
18174 {
18175   for (suppr::suppressions_type::const_iterator i = supprs.begin();
18176        i != supprs.end();
18177        ++i)
18178     if ((*i)->get_drops_artifact_from_ir())
18179       ctxt.get_suppressions().push_back(*i);
18180 }
18181
18182 /// Set the @ref corpus_group being created to the current read context.
18183 ///
18184 /// @param ctxt the read_context to consider.
18185 ///
18186 /// @param group the @ref corpus_group to set.
18187 void
18188 set_read_context_corpus_group(read_context& ctxt,
18189                               corpus_group_sptr& group)
18190 {
18191   ctxt.cur_corpus_group_ = group;
18192 }
18193
18194 /// Read all @ref abigail::translation_unit possible from the debug info
18195 /// accessible from an elf file, stuff them into a libabigail ABI
18196 /// Corpus and return it.
18197 ///
18198 /// @param ctxt the context to use for reading the elf file.
18199 ///
18200 /// @param resulting_corp a pointer to the resulting abigail::corpus.
18201 ///
18202 /// @return the resulting status.
18203 corpus_sptr
18204 read_corpus_from_elf(read_context& ctxt, status& status)
18205 {
18206   status = STATUS_UNKNOWN;
18207
18208   // Load debug info from the elf path.
18209   if (!ctxt.load_debug_info())
18210     status |= STATUS_DEBUG_INFO_NOT_FOUND;
18211
18212   {
18213     string alt_di_path;
18214     if (refers_to_alt_debug_info(ctxt, alt_di_path) && !ctxt.alt_dwarf())
18215       status |= STATUS_ALT_DEBUG_INFO_NOT_FOUND;
18216   }
18217
18218   if (!get_ignore_symbol_table(ctxt))
18219     {
18220       ctxt.load_elf_properties();
18221       // Read the symbols for publicly defined decls
18222       if (!ctxt.load_symbol_maps())
18223         status |= STATUS_NO_SYMBOLS_FOUND;
18224     }
18225
18226   if (// If no elf symbol was found ...
18227       status & STATUS_NO_SYMBOLS_FOUND
18228       // ... or if debug info was found but not the required alternate
18229       // debug info ...
18230       || ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
18231           && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
18232     // ... then we cannot handle the binary.
18233     return corpus_sptr();
18234
18235   // Read the variable and function descriptions from the debug info
18236   // we have, through the dwfl handle.
18237   corpus_sptr corp = read_debug_info_into_corpus(ctxt);
18238
18239   status |= STATUS_OK;
18240
18241   return corp;
18242 }
18243
18244 /// Read a corpus and add it to a given @ref corpus_group.
18245 ///
18246 /// @param ctxt the reading context to consider.
18247 ///
18248 /// @param group the @ref corpus_group to add the new corpus to.
18249 ///
18250 /// @param status output parameter. The status of the read.  It is set
18251 /// by this function upon its completion.
18252 corpus_sptr
18253 read_and_add_corpus_to_group_from_elf(read_context& ctxt,
18254                                       corpus_group& group,
18255                                       status& status)
18256 {
18257   corpus_sptr result;
18258   corpus_sptr corp = read_corpus_from_elf(ctxt, status);
18259   if (status & STATUS_OK)
18260     {
18261       if (!corp->get_group())
18262         group.add_corpus(corp);
18263       result = corp;
18264     }
18265
18266   return result;
18267 }
18268
18269 /// Read all @ref abigail::translation_unit possible from the debug info
18270 /// accessible from an elf file, stuff them into a libabigail ABI
18271 /// Corpus and return it.
18272 ///
18273 /// @param elf_path the path to the elf file.
18274 ///
18275 /// @param debug_info_root_paths a vector of pointers to root paths
18276 /// under which to look for the debug info of the elf files that are
18277 /// later handled by the Dwfl.  This for cases where the debug info is
18278 /// split into a different file from the binary we want to inspect.
18279 /// On Red Hat compatible systems, this root path is usually
18280 /// /usr/lib/debug by default.  If this argument is set to NULL, then
18281 /// "./debug" and /usr/lib/debug will be searched for sub-directories
18282 /// containing the debug info file.
18283 ///
18284 /// @param environment the environment used by the current context.
18285 /// This environment contains resources needed by the reader and by
18286 /// the types and declarations that are to be created later.  Note
18287 /// that ABI artifacts that are to be compared all need to be created
18288 /// within the same environment.  Also, the lifetime of the
18289 /// environment must be greater than the lifetime of the resulting
18290 /// corpus because the corpus uses resources that are allocated in the
18291 /// environment.
18292 ///
18293 /// @param load_all_types if set to false only the types that are
18294 /// reachable from publicly exported declarations (of functions and
18295 /// variables) are read.  If set to true then all types found in the
18296 /// debug information are loaded.
18297 ///
18298 /// @param resulting_corp a pointer to the resulting abigail::corpus.
18299 ///
18300 /// @return the resulting status.
18301 corpus_sptr
18302 read_corpus_from_elf(const std::string& elf_path,
18303                      const vector<char**>& debug_info_root_paths,
18304                      ir::environment*   environment,
18305                      bool               load_all_types,
18306                      status&            status)
18307 {
18308   read_context_sptr c = create_read_context(elf_path,
18309                                             debug_info_root_paths,
18310                                             environment,
18311                                             load_all_types);
18312   read_context& ctxt = *c;
18313   return read_corpus_from_elf(ctxt, status);
18314 }
18315
18316 /// Look into the symbol tables of a given elf file and see if we find
18317 /// a given symbol.
18318 ///
18319 /// @param env the environment we are operating from.
18320 ///
18321 /// @param elf_path the path to the elf file to consider.
18322 ///
18323 /// @param symbol_name the name of the symbol to look for.
18324 ///
18325 /// @param demangle if true, try to demangle the symbol name found in
18326 /// the symbol table.
18327 ///
18328 /// @param syms the vector of symbols found with the name @p symbol_name.
18329 ///
18330 /// @return true iff the symbol was found among the publicly exported
18331 /// symbols of the ELF file.
18332 bool
18333 lookup_symbol_from_elf(const environment*               env,
18334                        const string&                    elf_path,
18335                        const string&                    symbol_name,
18336                        bool                             demangle,
18337                        vector<elf_symbol_sptr>& syms)
18338
18339 {
18340   if (elf_version(EV_CURRENT) == EV_NONE)
18341     return false;
18342
18343   int fd = open(elf_path.c_str(), O_RDONLY);
18344   if (fd < 0)
18345     return false;
18346
18347   struct stat s;
18348   if (fstat(fd, &s))
18349     return false;
18350
18351   Elf* elf = elf_begin(fd, ELF_C_READ, 0);
18352   if (elf == 0)
18353     return false;
18354
18355   bool value = lookup_symbol_from_elf(env, elf, symbol_name,
18356                                       demangle, syms);
18357   elf_end(elf);
18358   close(fd);
18359
18360   return value;
18361 }
18362
18363 /// Look into the symbol tables of an elf file to see if a public
18364 /// function of a given name is found.
18365 ///
18366 /// @param env the environment we are operating from.
18367 ///
18368 /// @param elf_path the path to the elf file to consider.
18369 ///
18370 /// @param symbol_name the name of the function to look for.
18371 ///
18372 /// @param syms the vector of public function symbols found with the
18373 /// name @p symname.
18374 ///
18375 /// @return true iff a function with symbol name @p symbol_name is
18376 /// found.
18377 bool
18378 lookup_public_function_symbol_from_elf(const environment*               env,
18379                                        const string&                    path,
18380                                        const string&                    symname,
18381                                        vector<elf_symbol_sptr>& syms)
18382 {
18383   if (elf_version(EV_CURRENT) == EV_NONE)
18384     return false;
18385
18386   int fd = open(path.c_str(), O_RDONLY);
18387   if (fd < 0)
18388     return false;
18389
18390   struct stat s;
18391   if (fstat(fd, &s))
18392     return false;
18393
18394   Elf* elf = elf_begin(fd, ELF_C_READ, 0);
18395   if (elf == 0)
18396     return false;
18397
18398   bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
18399   elf_end(elf);
18400   close(fd);
18401
18402   return value;
18403 }
18404
18405 /// Check if the underlying elf file refers to an alternate debug info
18406 /// file associated to it.
18407 ///
18408 /// Note that "alternate debug info sections" is a GNU extension as
18409 /// of DWARF4 and is described at
18410 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
18411 ///
18412 /// @param ctxt the context used to read the elf file.
18413 ///
18414 /// @param alt_di the path to the alternate debug info file.  This is
18415 /// set iff the function returns true.
18416 ///
18417 /// @return true if the ELF file refers to an alternate debug info
18418 /// file.
18419 bool
18420 refers_to_alt_debug_info(const read_context&    ctxt,
18421                          string&                alt_di_path)
18422 {
18423   if (!ctxt.alt_debug_info_path().empty())
18424     {
18425       alt_di_path = ctxt.alt_debug_info_path();
18426       return true;
18427     }
18428   return false;
18429 }
18430
18431 /// Check if the underlying elf file has an alternate debug info file
18432 /// associated to it.
18433 ///
18434 /// Note that "alternate debug info sections" is a GNU extension as
18435 /// of DWARF4 and is described at
18436 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
18437 ///
18438 /// @param ctxt the read_context to use to handle the underlying elf file.
18439 ///
18440 /// @param has_alt_di out parameter.  This is set to true upon
18441 /// succesful completion of the function iff an alternate debug info
18442 /// file was found, false otherwise.  Note thas this parameter is set
18443 /// only if the function returns STATUS_OK.
18444 ///
18445 /// @param alt_debug_info_path if the function returned STATUS_OK and
18446 /// if @p has been set to true, then this parameter contains the path
18447 /// to the alternate debug info file found.
18448 ///
18449 /// return STATUS_OK upon successful completion, false otherwise.
18450 status
18451 has_alt_debug_info(read_context&        ctxt,
18452                    bool&                has_alt_di,
18453                    string&              alt_debug_info_path)
18454 {
18455   // Load debug info from the elf path.
18456   if (!ctxt.load_debug_info())
18457     return STATUS_DEBUG_INFO_NOT_FOUND;
18458
18459   if (ctxt.alt_dwarf())
18460     {
18461       has_alt_di = true;
18462       alt_debug_info_path = ctxt.alt_debug_info_path();
18463     }
18464   else
18465     has_alt_di = false;
18466
18467   return STATUS_OK;
18468 }
18469
18470 /// Check if a given elf file has an alternate debug info file
18471 /// associated to it.
18472 ///
18473 /// Note that "alternate debug info sections" is a GNU extension as
18474 /// of DWARF4 and is described at
18475 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
18476 ///
18477 /// @param elf_path the path to the elf file to consider.
18478 ///
18479 /// @param a pointer to the root directory under which the split debug info
18480 /// file associated to elf_path is to be found.  This has to be NULL
18481 /// if the debug info file is not in a split file.
18482 ///
18483 /// @param has_alt_di out parameter.  This is set to true upon
18484 /// succesful completion of the function iff an alternate debug info
18485 /// file was found, false otherwise.  Note thas this parameter is set
18486 /// only if the function returns STATUS_OK.
18487 ///
18488 /// @param alt_debug_info_path if the function returned STATUS_OK and
18489 /// if @p has been set to true, then this parameter contains the path
18490 /// to the alternate debug info file found.
18491 ///
18492 /// return STATUS_OK upon successful completion, false otherwise.
18493 status
18494 has_alt_debug_info(const string&        elf_path,
18495                    char**               debug_info_root_path,
18496                    bool&                has_alt_di,
18497                    string&              alt_debug_info_path)
18498 {
18499   vector<char**> di_roots;
18500   di_roots.push_back(debug_info_root_path);
18501   read_context_sptr c = create_read_context(elf_path, di_roots, 0);
18502   read_context& ctxt = *c;
18503
18504   // Load debug info from the elf path.
18505   if (!ctxt.load_debug_info())
18506     return STATUS_DEBUG_INFO_NOT_FOUND;
18507
18508   if (ctxt.alt_dwarf())
18509     {
18510       has_alt_di = true;
18511       alt_debug_info_path = ctxt.alt_debug_info_path();
18512     }
18513   else
18514     has_alt_di = false;
18515
18516   return STATUS_OK;
18517 }
18518
18519 /// Fetch the SONAME ELF property from an ELF binary file.
18520 ///
18521 /// @param path The path to the elf file to consider.
18522 ///
18523 /// @param soname out parameter. Set to the SONAME property of the
18524 /// binary file, if it present in the ELF file.
18525 ///
18526 /// return false if an error occured while looking for the SONAME
18527 /// property in the binary, true otherwise.
18528 bool
18529 get_soname_of_elf_file(const string& path, string &soname)
18530 {
18531
18532   int fd = open(path.c_str(), O_RDONLY);
18533   if (fd == -1)
18534     return false;
18535
18536   elf_version (EV_CURRENT);
18537   Elf* elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
18538
18539   GElf_Ehdr ehdr_mem;
18540   GElf_Ehdr* ehdr = gelf_getehdr (elf, &ehdr_mem);
18541   if (ehdr == NULL)
18542     return false;
18543
18544   for (int i = 0; i < ehdr->e_phnum; ++i)
18545     {
18546       GElf_Phdr phdr_mem;
18547       GElf_Phdr* phdr = gelf_getphdr (elf, i, &phdr_mem);
18548
18549       if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
18550         {
18551           Elf_Scn* scn = gelf_offscn (elf, phdr->p_offset);
18552           GElf_Shdr shdr_mem;
18553           GElf_Shdr* shdr = gelf_getshdr (scn, &shdr_mem);
18554           int maxcnt = (shdr != NULL
18555                         ? shdr->sh_size / shdr->sh_entsize : INT_MAX);
18556           ABG_ASSERT (shdr == NULL || shdr->sh_type == SHT_DYNAMIC);
18557           Elf_Data* data = elf_getdata (scn, NULL);
18558           if (data == NULL)
18559             break;
18560
18561           for (int cnt = 0; cnt < maxcnt; ++cnt)
18562             {
18563               GElf_Dyn dynmem;
18564               GElf_Dyn* dyn = gelf_getdyn (data, cnt, &dynmem);
18565               if (dyn == NULL)
18566                 continue;
18567
18568               if (dyn->d_tag == DT_NULL)
18569                 break;
18570
18571               if (dyn->d_tag != DT_SONAME)
18572                 continue;
18573
18574               soname = elf_strptr (elf, shdr->sh_link, dyn->d_un.d_val);
18575               break;
18576             }
18577           break;
18578         }
18579     }
18580
18581   elf_end(elf);
18582   close(fd);
18583
18584   return true;
18585 }
18586
18587 /// Get the type of a given elf type.
18588 ///
18589 /// @param path the absolute path to the ELF file to analyzed.
18590 ///
18591 /// @param type the kind of the ELF file designated by @p path.
18592 ///
18593 /// @param out parameter.  Is set to the type of ELF file of @p path.
18594 /// This parameter is set iff the function returns true.
18595 ///
18596 /// @return true iff the file could be opened and analyzed.
18597 bool
18598 get_type_of_elf_file(const string& path, elf_type& type)
18599 {
18600   int fd = open(path.c_str(), O_RDONLY);
18601   if (fd == -1)
18602     return false;
18603
18604   elf_version (EV_CURRENT);
18605   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
18606   type = elf_file_type(elf);
18607   elf_end(elf);
18608   close(fd);
18609
18610   return true;
18611 }
18612
18613 }// end namespace dwarf_reader
18614
18615 }// end namespace abigail