• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2020 Red Hat, Inc.
5 //
6 // Author: Dodji Seketeli
7 
8 /// @file
9 ///
10 /// This file contains the definitions of the entry points to
11 /// de-serialize an instance of @ref abigail::corpus from a file in
12 /// elf format, containing dwarf information.
13 
14 #include "config.h"
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <libgen.h>
20 #include <assert.h>
21 #include <limits.h>
22 #include <elfutils/libdwfl.h>
23 #include <dwarf.h>
24 #include <algorithm>
25 #include <cmath>
26 #include <cstring>
27 #include <deque>
28 #include <list>
29 #include <memory>
30 #include <ostream>
31 #include <sstream>
32 #include <stack>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <map>
36 
37 #include "abg-ir-priv.h"
38 #include "abg-suppression-priv.h"
39 #include "abg-corpus-priv.h"
40 #include "abg-elf-helpers.h"
41 #include "abg-internal.h"
42 
43 // <headers defining libabigail's API go under here>
44 ABG_BEGIN_EXPORT_DECLARATIONS
45 
46 #include "abg-dwarf-reader.h"
47 #include "abg-sptr-utils.h"
48 #include "abg-symtab-reader.h"
49 #include "abg-tools-utils.h"
50 
51 ABG_END_EXPORT_DECLARATIONS
52 // </headers defining libabigail's API>
53 
54 #ifndef UINT64_MAX
55 #define UINT64_MAX 0xffffffffffffffff
56 #endif
57 
58 using std::string;
59 using namespace abigail::elf_reader;
60 
61 namespace abigail
62 {
63 
64 using std::cerr;
65 
66 /// The namespace for the DWARF reader.
67 namespace dwarf_reader
68 {
69 
70 using std::dynamic_pointer_cast;
71 using std::static_pointer_cast;
72 using std::unordered_map;
73 using std::unordered_set;
74 using std::stack;
75 using std::deque;
76 using std::list;
77 using std::map;
78 
79 using namespace elf_helpers; // TODO: avoid using namespace
80 
81 /// Where a DIE comes from. For instance, a DIE can come from the main
82 /// debug info section, the alternate debug info section or from the
83 /// type unit section.
84 enum die_source
85 {
86   NO_DEBUG_INFO_DIE_SOURCE,
87   PRIMARY_DEBUG_INFO_DIE_SOURCE,
88   ALT_DEBUG_INFO_DIE_SOURCE,
89   TYPE_UNIT_DIE_SOURCE,
90   NUMBER_OF_DIE_SOURCES,	// This one must always be the latest
91 				// enumerator
92 };
93 
94 /// Prefix increment operator for @ref die_source.
95 ///
96 /// @param source the die_source to increment.
97 /// @return the incremented source.
98 static die_source&
operator ++(die_source & source)99 operator++(die_source& source)
100 {
101   source = static_cast<die_source>(source + 1);
102   return source;
103 }
104 
105 /// A functor used by @ref dwfl_sptr.
106 struct dwfl_deleter
107 {
108   void
operator ()abigail::dwarf_reader::dwfl_deleter109   operator()(Dwfl* dwfl)
110   {dwfl_end(dwfl);}
111 };//end struct dwfl_deleter
112 
113 /// A convenience typedef for a shared pointer to a Dwfl.
114 typedef shared_ptr<Dwfl> dwfl_sptr;
115 
116 /// A convenience typedef for a vector of Dwarf_Off.
117 typedef vector<Dwarf_Off> dwarf_offsets_type;
118 
119 /// Convenience typedef for a map which key is the offset of a dwarf
120 /// die and which value is the corresponding artefact.
121 typedef unordered_map<Dwarf_Off, type_or_decl_base_sptr> die_artefact_map_type;
122 
123 /// Convenience typedef for a map which key is the offset of a dwarf
124 /// die, (given by dwarf_dieoffset()) and which value is the
125 /// corresponding class_decl.
126 typedef unordered_map<Dwarf_Off, class_decl_sptr> die_class_map_type;
127 
128 /// Convenience typedef for a map which key is the offset of a dwarf
129 /// die, (given by dwarf_dieoffset()) and which value is the
130 /// corresponding class_or_union_sptr.
131 typedef unordered_map<Dwarf_Off, class_or_union_sptr> die_class_or_union_map_type;
132 
133 /// Convenience typedef for a map which key the offset of a dwarf die
134 /// and which value is the corresponding function_decl.
135 typedef unordered_map<Dwarf_Off, function_decl_sptr> die_function_decl_map_type;
136 
137 /// Convenience typedef for a map which key is the offset of a dwarf
138 /// die and which value is the corresponding function_type.
139 typedef unordered_map<Dwarf_Off, function_type_sptr> die_function_type_map_type;
140 
141 /// Convenience typedef for a map which key is the offset of a
142 /// DW_TAG_compile_unit and the value is the corresponding @ref
143 /// translation_unit_sptr.
144 typedef unordered_map<Dwarf_Off, translation_unit_sptr> die_tu_map_type;
145 
146 /// Convenience typedef for a map which key is the offset of a DIE and
147 /// the value is the corresponding qualified name of the DIE.
148 typedef unordered_map<Dwarf_Off, interned_string> die_istring_map_type;
149 
150 /// Convenience typedef for a map which is an interned_string and
151 /// which value is a vector of offsets.
152 typedef unordered_map<interned_string,
153 		      dwarf_offsets_type,
154 		      hash_interned_string>
155 istring_dwarf_offsets_map_type;
156 
157 /// Convenience typedef for a map which key is an elf address and
158 /// which value is an elf_symbol_sptr.
159 typedef unordered_map<GElf_Addr, elf_symbol_sptr> addr_elf_symbol_sptr_map_type;
160 
161 /// Convenience typedef for a set of ELF addresses.
162 typedef unordered_set<GElf_Addr> address_set_type;
163 
164 typedef unordered_set<interned_string, hash_interned_string> istring_set_type;
165 
166 /// Convenience typedef for a shared pointer to an @ref address_set_type.
167 typedef shared_ptr<address_set_type> address_set_sptr;
168 
169 /// Convenience typedef for a shared pointer to an
170 /// addr_elf_symbol_sptr_map_type.
171 typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
172 
173 /// Convenience typedef for a map that associates an @ref
174 /// interned_string to a @ref function_type_sptr.
175 typedef unordered_map<interned_string,
176 		      function_type_sptr,
177 		      hash_interned_string> istring_fn_type_map_type;
178 
179 /// Convenience typedef for a stack containing the scopes up to the
180 /// current point in the abigail Internal Representation (aka IR) tree
181 /// that is being built.
182 typedef stack<scope_decl*> scope_stack_type;
183 
184 /// Convenience typedef for a map which key is a dwarf offset.  The
185 /// value is also a dwarf offset.
186 typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
187 
188 /// Convenience typedef for a map which key is a string and which
189 /// value is a vector of smart pointer to a class.
190 typedef unordered_map<string, classes_type> string_classes_map;
191 
192 /// Convenience typedef for a map which key is a string and which
193 /// value is a vector of smart pointer to a enum.
194 typedef unordered_map<string, enums_type> string_enums_map;
195 
196 /// The abstraction of the place where a partial unit has been
197 /// imported.  This is what the DW_TAG_imported_unit DIE expresses.
198 ///
199 /// This type thus contains:
200 ///	- the offset to which the partial unit is imported
201 ///	- the offset of the imported partial unit.
202 ///	- the offset of the imported partial unit.
203 struct imported_unit_point
204 {
205   Dwarf_Off	offset_of_import;
206   // The boolean below is true iff the imported unit comes from the
207   // alternate debug info file.
208   die_source	imported_unit_die_source;
209   Dwarf_Off	imported_unit_die_off;
210   Dwarf_Off	imported_unit_cu_off;
211   Dwarf_Off	imported_unit_child_off;
212 
213   /// Default constructor for @ref the type imported_unit_point.
imported_unit_pointabigail::dwarf_reader::imported_unit_point214   imported_unit_point()
215     : offset_of_import(),
216       imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
217       imported_unit_die_off(),
218       imported_unit_cu_off(),
219       imported_unit_child_off()
220   {}
221 
222   /// Constructor of @ref the type imported_unit_point.
223   ///
224   /// @param import_off the offset of the point at which the unit has
225   /// been imported.
imported_unit_pointabigail::dwarf_reader::imported_unit_point226   imported_unit_point(Dwarf_Off import_off)
227     : offset_of_import(import_off),
228       imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
229       imported_unit_die_off(),
230       imported_unit_cu_off(),
231       imported_unit_child_off()
232   {}
233 
234   /// Constructor of @ref the type imported_unit_point.
235   ///
236   /// @param import_off the offset of the point at which the unit has
237   /// been imported.
238   ///
239   /// @param from where the imported DIE comes from.
240   ///
241   /// @param imported_die the die of the unit that has been imported.
imported_unit_pointabigail::dwarf_reader::imported_unit_point242   imported_unit_point(Dwarf_Off	import_off,
243 		      const Dwarf_Die& imported_die,
244 		      die_source from)
245     : offset_of_import(import_off),
246       imported_unit_die_source(from),
247       imported_unit_die_off(dwarf_dieoffset
248 			    (const_cast<Dwarf_Die*>(&imported_die))),
249       imported_unit_cu_off(),
250       imported_unit_child_off()
251   {
252     Dwarf_Die imported_unit_child;
253 
254     ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
255 			   &imported_unit_child) == 0);
256 
257     imported_unit_child_off =
258       dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
259 
260     Dwarf_Die cu_die_memory;
261     Dwarf_Die *cu_die;
262 
263     cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
264 			 &cu_die_memory, 0, 0);
265     imported_unit_cu_off = dwarf_dieoffset(cu_die);
266   }
267 }; // struct imported_unit_point
268 
269 /// Convenience typedef for a vector of @ref imported_unit_point.
270 typedef vector<imported_unit_point> imported_unit_points_type;
271 
272 /// Convenience typedef for a vector of @ref imported_unit_point.
273 typedef unordered_map<Dwarf_Off, imported_unit_points_type>
274 tu_die_imported_unit_points_map_type;
275 
276 /// "Less than" operator for instances of @ref imported_unit_point
277 /// type.
278 ///
279 /// @param the left hand side operand of the "Less than" operator.
280 ///
281 /// @param the right hand side operand of the "Less than" operator.
282 ///
283 /// @return true iff @p l is less than @p r.
284 static bool
operator <(const imported_unit_point & l,const imported_unit_point & r)285 operator<(const imported_unit_point& l, const imported_unit_point& r)
286 {return l.offset_of_import < r.offset_of_import;}
287 
288 static bool
289 get_parent_die(const read_context&	ctxt,
290 	       const Dwarf_Die*	die,
291 	       Dwarf_Die&		parent_die,
292 	       size_t			where_offset);
293 
294 static bool
295 get_scope_die(const read_context&	ctxt,
296 	      const Dwarf_Die*		die,
297 	      size_t			where_offset,
298 	      Dwarf_Die&		scope_die);
299 
300 static bool
301 die_is_anonymous(const Dwarf_Die* die);
302 
303 static bool
304 die_is_type(const Dwarf_Die* die);
305 
306 static bool
307 die_is_decl(const Dwarf_Die* die);
308 
309 static bool
310 die_is_namespace(const Dwarf_Die* die);
311 
312 static bool
313 die_is_unspecified(Dwarf_Die* die);
314 
315 static bool
316 die_is_void_type(Dwarf_Die* die);
317 
318 static bool
319 die_is_pointer_type(const Dwarf_Die* die);
320 
321 static bool
322 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
323 
324 static bool
325 die_is_reference_type(const Dwarf_Die* die);
326 
327 static bool
328 die_is_pointer_or_reference_type(const Dwarf_Die* die);
329 
330 static bool
331 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
332 
333 static bool
334 die_is_class_type(const Dwarf_Die* die);
335 
336 static bool
337 die_is_qualified_type(const Dwarf_Die* die);
338 
339 static bool
340 die_is_function_type(const Dwarf_Die *die);
341 
342 static bool
343 die_has_object_pointer(const Dwarf_Die* die,
344 		       Dwarf_Die& object_pointer);
345 
346 static bool
347 die_has_children(const Dwarf_Die* die);
348 
349 static bool
350 die_this_pointer_from_object_pointer(Dwarf_Die* die,
351 				     Dwarf_Die& this_pointer);
352 
353 static bool
354 die_this_pointer_is_const(Dwarf_Die* die);
355 
356 static bool
357 die_object_pointer_is_for_const_method(Dwarf_Die* die);
358 
359 static bool
360 die_is_at_class_scope(const read_context& ctxt,
361 		      const Dwarf_Die* die,
362 		      size_t where_offset,
363 		      Dwarf_Die& class_scope_die);
364 static bool
365 eval_last_constant_dwarf_sub_expr(Dwarf_Op*	expr,
366 				  uint64_t	expr_len,
367 				  int64_t&	value,
368 				  bool&	is_tls_address);
369 
370 static translation_unit::language
371 dwarf_language_to_tu_language(size_t l);
372 
373 static bool
374 die_unsigned_constant_attribute(const Dwarf_Die*	die,
375 				unsigned		attr_name,
376 				uint64_t&		cst);
377 
378 static bool
379 die_signed_constant_attribute(const Dwarf_Die*die,
380 			      unsigned	attr_name,
381 			      int64_t&	cst);
382 
383 static bool
384 die_constant_attribute(const Dwarf_Die *die,
385 		       unsigned attr_name,
386 		       bool is_signed,
387 		       array_type_def::subrange_type::bound_value &value);
388 
389 static bool
390 form_is_DW_FORM_strx(unsigned form);
391 
392 static bool
393 form_is_DW_FORM_line_strp(unsigned form);
394 
395 static bool
396 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
397 
398 static string
399 die_name(const Dwarf_Die* die);
400 
401 static location
402 die_location(const read_context& ctxt, const Dwarf_Die* die);
403 
404 static bool
405 die_location_address(Dwarf_Die*	die,
406 		     Dwarf_Addr&	address,
407 		     bool&		is_tls_address);
408 
409 static bool
410 die_die_attribute(const Dwarf_Die* die,
411 		  unsigned attr_name,
412 		  Dwarf_Die& result,
413 		  bool recursively = true);
414 
415 static string
416 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
417 
418 static string
419 build_internal_anonymous_die_name(const string &base_name,
420 				  size_t anonymous_type_index);
421 
422 static string
423 get_internal_anonymous_die_name(Dwarf_Die *die,
424 				size_t anonymous_type_index);
425 
426 static string
427 build_internal_underlying_enum_type_name(const string &base_name,
428 					 bool is_anonymous,
429 					 uint64_t size);
430 
431 static string
432 die_qualified_type_name(const read_context& ctxt,
433 			const Dwarf_Die* die,
434 			size_t where);
435 
436 static string
437 die_qualified_decl_name(const read_context& ctxt,
438 			const Dwarf_Die* die,
439 			size_t where);
440 
441 static string
442 die_qualified_name(const read_context& ctxt,
443 		   const Dwarf_Die* die,
444 		   size_t where);
445 
446 static bool
447 die_qualified_type_name_empty(const read_context& ctxt,
448 			      const Dwarf_Die* die, size_t where,
449 			      string &qualified_name);
450 
451 static void
452 die_return_and_parm_names_from_fn_type_die(const read_context& ctxt,
453 					   const Dwarf_Die* die,
454 					   size_t where_offset,
455 					   bool pretty_print,
456 					   string &return_type_name,
457 					   string &class_name,
458 					   vector<string>& parm_names,
459 					   bool& is_const,
460 					   bool& is_static);
461 
462 static string
463 die_function_signature(const read_context& ctxt,
464 		       const Dwarf_Die *die,
465 		       size_t where_offset);
466 
467 static bool
468 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
469 
470 static bool
471 die_function_type_is_method_type(const read_context& ctxt,
472 				 const Dwarf_Die *die,
473 				 size_t where_offset,
474 				 Dwarf_Die& object_pointer_die,
475 				 Dwarf_Die& class_die,
476 				 bool& is_static);
477 
478 static string
479 die_pretty_print_type(read_context& ctxt,
480 		      const Dwarf_Die* die,
481 		      size_t where_offset);
482 
483 static string
484 die_pretty_print_decl(read_context& ctxt,
485 		      const Dwarf_Die* die,
486 		      size_t where_offset);
487 
488 static string
489 die_pretty_print(read_context& ctxt,
490 		 const Dwarf_Die* die,
491 		 size_t where_offset);
492 
493 static void
494 maybe_canonicalize_type(const Dwarf_Die* die,
495 			read_context& ctxt);
496 
497 static void
498 maybe_canonicalize_type(const type_base_sptr&	t,
499 			read_context&		ctxt);
500 
501 static uint64_t
502 get_default_array_lower_bound(translation_unit::language l);
503 
504 static bool
505 find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
506 					 Dwarf_Off,
507 					 imported_unit_points_type::const_iterator&);
508 
509 static array_type_def::subrange_sptr
510 build_subrange_type(read_context&	ctxt,
511 		    const Dwarf_Die*	die,
512 		    size_t		where_offset,
513 		    bool		associate_type_to_die = true);
514 
515 static void
516 build_subranges_from_array_type_die(read_context&			ctxt,
517 				    const Dwarf_Die*			die,
518 				    array_type_def::subranges_type&	subranges,
519 				    size_t				where_offset,
520 				    bool				associate_type_to_die = true);
521 
522 static bool
523 compare_dies(const read_context& ctxt,
524 	     const Dwarf_Die *l, const Dwarf_Die *r,
525 	     bool update_canonical_dies_on_the_fly);
526 
527 
528 /// Find the file name of the alternate debug info file.
529 ///
530 /// @param elf_module the elf module to consider.
531 ///
532 /// @param out parameter.  Is set to the file name of the alternate
533 /// debug info file, iff this function returns true.
534 ///
535 /// @return true iff the location of the alternate debug info file was
536 /// found.
537 static bool
find_alt_debug_info_link(Dwfl_Module * elf_module,string & alt_file_name)538 find_alt_debug_info_link(Dwfl_Module *elf_module,
539 			 string &alt_file_name)
540 {
541   GElf_Addr bias = 0;
542   Dwarf *dwarf = dwfl_module_getdwarf(elf_module, &bias);
543   Elf *elf = dwarf_getelf(dwarf);
544   GElf_Ehdr ehmem, *elf_header;
545   elf_header = gelf_getehdr(elf, &ehmem);
546 
547   Elf_Scn* section = 0;
548   while ((section = elf_nextscn(elf, section)) != 0)
549     {
550       GElf_Shdr header_mem, *header;
551       header = gelf_getshdr(section, &header_mem);
552       if (header->sh_type != SHT_PROGBITS)
553 	continue;
554 
555       const char *section_name = elf_strptr(elf,
556 					    elf_header->e_shstrndx,
557 					    header->sh_name);
558 
559       char *alt_name = 0;
560       char *buildid = 0;
561       size_t buildid_len = 0;
562       if (section_name != 0
563 	  && strcmp(section_name, ".gnu_debugaltlink") == 0)
564 	{
565 	  Elf_Data *data = elf_getdata(section, 0);
566 	  if (data != 0 && data->d_size != 0)
567 	    {
568 	      alt_name = (char*) data->d_buf;
569 	      char *end_of_alt_name =
570 		(char *) memchr(alt_name, '\0', data->d_size);
571 	      buildid_len = data->d_size - (end_of_alt_name - alt_name + 1);
572 	      if (buildid_len == 0)
573 		return false;
574 	      buildid = end_of_alt_name + 1;
575 	    }
576 	}
577       else
578 	continue;
579 
580       if (buildid == 0 || alt_name == 0)
581 	return false;
582 
583       alt_file_name = alt_name;
584       return true;
585     }
586 
587   return false;
588 }
589 
590 /// Find alternate debuginfo file of a given "link" under a set of
591 /// root directories.
592 ///
593 /// The link is a string that is read by the function
594 /// find_alt_debug_info_link().  That link is a path that is relative
595 /// to a given debug info file, e.g, "../../../.dwz/something.debug".
596 /// It designates the alternate debug info file associated to a given
597 /// debug info file.
598 ///
599 /// This function will thus try to find the .dwz/something.debug file
600 /// under some given root directories.
601 ///
602 /// @param root_dirs the set of root directories to look from.
603 ///
604 /// @param alt_file_name a relative path to the alternate debug info
605 /// file to look for.
606 ///
607 /// @param alt_file_path the resulting absolute path to the alternate
608 /// debuginfo path denoted by @p alt_file_name and found under one of
609 /// the directories in @p root_dirs.  This is set iff the function
610 /// returns true.
611 ///
612 /// @return true iff the function found the alternate debuginfo file.
613 static bool
find_alt_debug_info_path(const vector<char ** > root_dirs,const string & alt_file_name,string & alt_file_path)614 find_alt_debug_info_path(const vector<char**> root_dirs,
615 			 const string &alt_file_name,
616 			 string &alt_file_path)
617 {
618   if (alt_file_name.empty())
619     return false;
620 
621   string altfile_name = tools_utils::trim_leading_string(alt_file_name, "../");
622 
623   for (vector<char**>::const_iterator i = root_dirs.begin();
624        i != root_dirs.end();
625        ++i)
626     if (tools_utils::find_file_under_dir(**i, altfile_name, alt_file_path))
627       return true;
628 
629   return false;
630 }
631 
632 /// Return the alternate debug info associated to a given main debug
633 /// info file.
634 ///
635 /// @param elf_module the elf module to consider.
636 ///
637 /// @param debug_root_dirs a set of root debuginfo directories under
638 /// which too look for the alternate debuginfo file.
639 ///
640 /// @param alt_file_name output parameter.  This is set to the file
641 /// path of the alternate debug info file associated to @p elf_module.
642 /// This is set iff the function returns a non-null result.
643 ///
644 /// @param alt_fd the file descriptor used to access the alternate
645 /// debug info.  If this parameter is set by the function, then the
646 /// caller needs to fclose it, otherwise the file descriptor is going
647 /// to be leaked.  Note however that on recent versions of elfutils
648 /// where libdw.h contains the function dwarf_getalt(), this parameter
649 /// is set to 0, so it doesn't need to be fclosed.
650 ///
651 /// Note that the alternate debug info file is a DWARF extension as of
652 /// DWARF 4 ans is decribed at
653 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
654 ///
655 /// @return the alternate debuginfo, or null.  If @p alt_fd is
656 /// non-zero, then the caller of this function needs to call
657 /// dwarf_end() on the returned alternate debuginfo pointer,
658 /// otherwise, it's going to be leaked.
659 static Dwarf*
find_alt_debug_info(Dwfl_Module * elf_module,const vector<char ** > debug_root_dirs,string & alt_file_name,int & alt_fd)660 find_alt_debug_info(Dwfl_Module *elf_module,
661 		    const vector<char**> debug_root_dirs,
662 		    string& alt_file_name,
663 		    int& alt_fd)
664 {
665   if (elf_module == 0)
666     return 0;
667 
668   Dwarf* result = 0;
669   find_alt_debug_info_link(elf_module, alt_file_name);
670 
671 #ifdef LIBDW_HAS_DWARF_GETALT
672   // We are on recent versions of elfutils where the function
673   // dwarf_getalt exists, so let's use it.
674   Dwarf_Addr bias = 0;
675   Dwarf* dwarf = dwfl_module_getdwarf(elf_module, &bias);
676   result = dwarf_getalt(dwarf);
677   alt_fd = 0;
678 #else
679   // We are on an old version of elfutils where the function
680   // dwarf_getalt doesn't exist yet, so let's open code its
681   // functionality
682   char *alt_name = 0;
683   const char *file_name = 0;
684   void **user_data = 0;
685   Dwarf_Addr low_addr = 0;
686   char *alt_file = 0;
687 
688   file_name = dwfl_module_info(elf_module, &user_data,
689 			       &low_addr, 0, 0, 0, 0, 0);
690 
691   alt_fd = dwfl_standard_find_debuginfo(elf_module, user_data,
692 					file_name, low_addr,
693 					alt_name, file_name,
694 					0, &alt_file);
695 
696   result = dwarf_begin(alt_fd, DWARF_C_READ);
697 #endif
698 
699   if (result == 0)
700     {
701       // So we didn't find the alternate debuginfo file from the
702       // information that is in the debuginfo file associated to
703       // elf_module.  Maybe the alternate debuginfo file is located
704       // under one of the directories in debug_root_dirs.  So let's
705       // look in there.
706       string alt_file_path;
707       if (!find_alt_debug_info_path(debug_root_dirs,
708 				    alt_file_name,
709 				    alt_file_path))
710 	return result;
711 
712       // If we reach this point it means we have found the path to the
713       // alternate debuginfo file and it's in alt_file_path.  So let's
714       // open it and read it.
715       int fd = open(alt_file_path.c_str(), O_RDONLY);
716       if (fd == -1)
717 	return result;
718       result = dwarf_begin(fd, DWARF_C_READ);
719 
720 #ifdef LIBDW_HAS_DWARF_GETALT
721       Dwarf_Addr bias = 0;
722       Dwarf* dwarf = dwfl_module_getdwarf(elf_module, &bias);
723       dwarf_setalt(dwarf, result);
724 #endif
725     }
726 
727   return result;
728 }
729 
730 /// Compare a symbol name against another name, possibly demangling
731 /// the symbol_name before performing the comparison.
732 ///
733 /// @param symbol_name the symbol_name to take in account.
734 ///
735 /// @param name the second name to take in account.
736 ///
737 /// @param demangle if true, demangle @p symbol_name and compare the
738 /// result of the demangling with @p name.
739 ///
740 /// @return true iff symbol_name equals name.
741 static bool
compare_symbol_name(const string & symbol_name,const string & name,bool demangle)742 compare_symbol_name(const string& symbol_name,
743 		    const string& name,
744 		    bool demangle)
745 {
746   if (demangle)
747     {
748       string m = demangle_cplus_mangled_name(symbol_name);
749       return m == name;
750     }
751   return symbol_name == name;
752 }
753 
754 /// Lookup a symbol using the SysV ELF hash table.
755 ///
756 /// Note that this function hasn't been tested.  So it hasn't been
757 /// debugged yet.  IOW, it is not known to work.  Or rather, it's
758 /// almost like it's surely doesn't work ;-)
759 ///
760 /// Use it at your own risks.  :-)
761 ///
762 ///@parm env the environment we are operating from.
763 ///
764 /// @param elf_handle the elf_handle to use.
765 ///
766 /// @param sym_name the symbol name to look for.
767 ///
768 /// @param ht_index the index (in the section headers table) of the
769 /// hash table section to use.
770 ///
771 /// @param sym_tab_index the index (in the section headers table) of
772 /// the symbol table to use.
773 ///
774 /// @param demangle if true, demangle @p sym_name before comparing it
775 /// to names from the symbol table.
776 ///
777 /// @param syms_found a vector of symbols found with the name @p
778 /// sym_name.  table.
779 static bool
lookup_symbol_from_sysv_hash_tab(const environment * env,Elf * elf_handle,const string & sym_name,size_t ht_index,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)780 lookup_symbol_from_sysv_hash_tab(const environment*		env,
781 				 Elf*				elf_handle,
782 				 const string&			sym_name,
783 				 size_t			ht_index,
784 				 size_t			sym_tab_index,
785 				 bool				demangle,
786 				 vector<elf_symbol_sptr>&	syms_found)
787 {
788   Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
789   ABG_ASSERT(sym_tab_section);
790 
791   Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
792   ABG_ASSERT(sym_tab_data);
793 
794   GElf_Shdr sheader_mem;
795   GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
796 						   &sheader_mem);
797   Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
798   ABG_ASSERT(hash_section);
799 
800   // Poke at the different parts of the hash table and get them ready
801   // to be used.
802   unsigned long hash = elf_hash(sym_name.c_str());
803   Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
804   Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
805   size_t nb_buckets = ht_data[0];
806   size_t nb_chains = ht_data[1];
807 
808   if (nb_buckets == 0)
809     // An empty hash table.  Not sure if that is possible, but it
810     // would mean an empty table of exported symbols.
811     return false;
812 
813   //size_t nb_chains = ht_data[1];
814   Elf32_Word* ht_buckets = &ht_data[2];
815   Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
816 
817   // Now do the real work.
818   size_t bucket = hash % nb_buckets;
819   size_t symbol_index = ht_buckets[bucket];
820 
821   GElf_Sym symbol;
822   const char* sym_name_str;
823   size_t sym_size;
824   elf_symbol::type sym_type;
825   elf_symbol::binding sym_binding;
826   elf_symbol::visibility sym_visibility;
827   bool found = false;
828 
829   do
830     {
831       ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
832       sym_name_str = elf_strptr(elf_handle,
833 				sym_tab_section_header->sh_link,
834 				symbol.st_name);
835       if (sym_name_str
836 	  && compare_symbol_name(sym_name_str, sym_name, demangle))
837 	{
838 	  sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
839 	  sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
840 	  sym_visibility =
841 	    stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
842 	  sym_size = symbol.st_size;
843 	  elf_symbol::version ver;
844 	  if (get_version_for_symbol(elf_handle, symbol_index,
845 				     /*get_def_version=*/true, ver))
846 	    ABG_ASSERT(!ver.str().empty());
847 	  elf_symbol_sptr symbol_found =
848 	    elf_symbol::create(env,
849 			       symbol_index,
850 			       sym_size,
851 			       sym_name_str,
852 			       sym_type,
853 			       sym_binding,
854 			       symbol.st_shndx != SHN_UNDEF,
855 			       symbol.st_shndx == SHN_COMMON,
856 			       ver, sym_visibility);
857 	  syms_found.push_back(symbol_found);
858 	  found = true;
859 	}
860       symbol_index = ht_chains[symbol_index];
861     } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
862 
863   return found;
864 }
865 
866 /// Get the size of the elf class, in bytes.
867 ///
868 /// @param elf_handle the elf handle to use.
869 ///
870 /// @return the size computed.
871 static char
get_elf_class_size_in_bytes(Elf * elf_handle)872 get_elf_class_size_in_bytes(Elf* elf_handle)
873 {
874   char result = 0;
875   GElf_Ehdr hdr;
876 
877   ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
878   int c = hdr.e_ident[EI_CLASS];
879 
880   switch (c)
881     {
882     case ELFCLASS32:
883       result = 4;
884       break;
885     case ELFCLASS64:
886       result = 8;
887       break;
888     default:
889       ABG_ASSERT_NOT_REACHED;
890     }
891 
892   return result;
893 }
894 
895 /// Get a given word of a bloom filter, referred to by the index of
896 /// the word.
897 ///
898 /// The bloom word size depends on the current elf class (32 bits for
899 /// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
900 /// abstracts that nicely.
901 ///
902 /// @param elf_handle the elf handle to use.
903 ///
904 /// @param bloom_filter the bloom filter to consider.
905 ///
906 /// @param index the index of the bloom filter to return.
907 ///
908 /// @return a 64 bits work containing the bloom word found at index @p
909 /// index.  Note that if we are looking at an ELFCLASS32 binary, the 4
910 /// most significant bytes of the result are going to be zero.
911 static Elf64_Xword
bloom_word_at(Elf * elf_handle,Elf32_Word * bloom_filter,size_t index)912 bloom_word_at(Elf*		elf_handle,
913 	      Elf32_Word*	bloom_filter,
914 	      size_t		index)
915 {
916   Elf64_Xword result = 0;
917   GElf_Ehdr h;
918   ABG_ASSERT(gelf_getehdr(elf_handle, &h));
919   int c;
920   c = h.e_ident[EI_CLASS];
921 
922   switch(c)
923     {
924     case ELFCLASS32:
925       result = bloom_filter[index];
926       break ;
927     case ELFCLASS64:
928       {
929 	Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
930 	result = f[index];
931       }
932       break;
933     default:
934       abort();
935     }
936 
937   return result;
938 }
939 
940 /// The abstraction of the gnu elf hash table.
941 ///
942 /// The members of this struct are explained at
943 ///   - https://sourceware.org/ml/binutils/2006-10/msg00377.html
944 ///   - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
945 struct gnu_ht
946 {
947   size_t nb_buckets;
948   Elf32_Word* buckets;
949   Elf32_Word* chain;
950   size_t first_sym_index;
951   size_t bf_nwords;
952   size_t bf_size;
953   Elf32_Word* bloom_filter;
954   size_t shift;
955   size_t sym_count;
956   Elf_Scn* sym_tab_section;
957   GElf_Shdr sym_tab_section_header;
958 
gnu_htabigail::dwarf_reader::gnu_ht959   gnu_ht()
960     : nb_buckets(0),
961       buckets(0),
962       chain(0),
963       first_sym_index(0),
964       bf_nwords(0),
965       bf_size(0),
966       bloom_filter(0),
967       shift(0),
968       sym_count(0),
969       sym_tab_section(0)
970   {}
971 }; // end struct gnu_ht
972 
973 /// Setup the members of the gnu hash table.
974 ///
975 /// @param elf_handle a handle on the elf file to use.
976 ///
977 /// @param ht_index the index  (into the elf section headers table) of
978 /// the hash table section to use.
979 ///
980 /// @param sym_tab_index the index (into the elf section headers
981 /// table) of the symbol table the gnu hash table is about.
982 ///
983 /// @param ht the resulting hash table.
984 ///
985 /// @return true iff the hash table @ ht could be setup.
986 static bool
setup_gnu_ht(Elf * elf_handle,size_t ht_index,size_t sym_tab_index,gnu_ht & ht)987 setup_gnu_ht(Elf* elf_handle,
988 	     size_t ht_index,
989 	     size_t sym_tab_index,
990 	     gnu_ht& ht)
991 {
992   ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
993   ABG_ASSERT(ht.sym_tab_section);
994   ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
995   ht.sym_count =
996     ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
997   Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
998   ABG_ASSERT(hash_section);
999 
1000   // Poke at the different parts of the hash table and get them ready
1001   // to be used.
1002   Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1003   Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1004 
1005   ht.nb_buckets = ht_data[0];
1006   if (ht.nb_buckets == 0)
1007     // An empty hash table.  Not sure if that is possible, but it
1008     // would mean an empty table of exported symbols.
1009     return false;
1010   ht.first_sym_index = ht_data[1];
1011   // The number of words used by the bloom filter.  A size of a word
1012   // is ELFCLASS.
1013   ht.bf_nwords = ht_data[2];
1014   // The shift used by the bloom filter code.
1015   ht.shift = ht_data[3];
1016   // The data of the bloom filter proper.
1017   ht.bloom_filter = &ht_data[4];
1018   // The size of the bloom filter in 4 bytes word.  This is going to
1019   // be used to index the 'bloom_filter' above, which is of type
1020   // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1021   // words.
1022   ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1023   // The buckets of the hash table.
1024   ht.buckets = ht.bloom_filter + ht.bf_size;
1025   // The chain of the hash table.
1026   ht.chain = ht.buckets + ht.nb_buckets;
1027 
1028   return true;
1029 }
1030 
1031 /// Look into the symbol tables of the underlying elf file and find
1032 /// the symbol we are being asked.
1033 ///
1034 /// This function uses the GNU hash table for the symbol lookup.
1035 ///
1036 /// The reference of for the implementation of this function can be
1037 /// found at:
1038 ///   - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1039 ///   - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1040 ///
1041 /// @param elf_handle the elf handle to use.
1042 ///
1043 /// @param sym_name the name of the symbol to look for.
1044 ///
1045 /// @param ht_index the index of the hash table header to use.
1046 ///
1047 /// @param sym_tab_index the index of the symbol table header to use
1048 /// with this hash table.
1049 ///
1050 /// @param demangle if true, demangle @p sym_name.
1051 ///
1052 /// @param syms_found the vector of symbols found with the name @p
1053 /// sym_name.
1054 ///
1055 /// @return true if a symbol was actually found.
1056 static bool
lookup_symbol_from_gnu_hash_tab(const environment * env,Elf * elf_handle,const string & sym_name,size_t ht_index,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)1057 lookup_symbol_from_gnu_hash_tab(const environment*		env,
1058 				Elf*				elf_handle,
1059 				const string&			sym_name,
1060 				size_t				ht_index,
1061 				size_t				sym_tab_index,
1062 				bool				demangle,
1063 				vector<elf_symbol_sptr>&	syms_found)
1064 {
1065   gnu_ht ht;
1066   if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
1067     return false;
1068 
1069   // Now do the real work.
1070 
1071   // Compute bloom hashes (GNU hash and second bloom specific hashes).
1072   size_t h1 = elf_gnu_hash(sym_name.c_str());
1073   size_t h2 = h1 >> ht.shift;
1074   // The size of one of the words used in the bloom
1075   // filter, in bits.
1076   int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1077   int n =  (h1 / c) % ht.bf_nwords;
1078   // The bitmask of the bloom filter has a size of either 32-bits on
1079   // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries.  So we
1080   // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1081   // type used here.  When dealing with 32bits binaries, the upper
1082   // bits of the bitmask will be zero anyway.
1083   Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1084 
1085   // Test if the symbol is *NOT* present in this ELF file.
1086   if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1087     return false;
1088 
1089   size_t i = ht.buckets[h1 % ht.nb_buckets];
1090   if (i == STN_UNDEF)
1091     return false;
1092 
1093   Elf32_Word stop_word, *stop_wordp;
1094   elf_symbol::version ver;
1095   GElf_Sym symbol;
1096   const char* sym_name_str;
1097   bool found = false;
1098 
1099   elf_symbol::type sym_type;
1100   elf_symbol::binding sym_binding;
1101   elf_symbol::visibility sym_visibility;
1102 
1103   // Let's walk the hash table and record the versions of all the
1104   // symbols which name equal sym_name.
1105   for (i = ht.buckets[h1 % ht.nb_buckets],
1106 	 stop_wordp = &ht.chain[i - ht.first_sym_index];
1107        i != STN_UNDEF
1108 	 && (stop_wordp
1109 	     < ht.chain + (ht.sym_count - ht.first_sym_index));
1110        ++i, ++stop_wordp)
1111     {
1112       stop_word = *stop_wordp;
1113       if ((stop_word & ~ 1)!= (h1 & ~1))
1114 	// A given bucket can reference several hashes.  Here we
1115 	// stumbled across a hash value different from the one we are
1116 	// looking for.  Let's keep walking.
1117 	continue;
1118 
1119       ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1120 			 i, &symbol));
1121       sym_name_str = elf_strptr(elf_handle,
1122 				ht.sym_tab_section_header.sh_link,
1123 				symbol.st_name);
1124       if (sym_name_str
1125 	  && compare_symbol_name(sym_name_str, sym_name, demangle))
1126 	{
1127 	  // So we found a symbol (in the symbol table) that equals
1128 	  // sym_name.  Now lets try to get its version and record it.
1129 	  sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1130 	  sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1131 	 sym_visibility =
1132 	   stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1133 
1134 	  if (get_version_for_symbol(elf_handle, i,
1135 				     /*get_def_version=*/true,
1136 				     ver))
1137 	    ABG_ASSERT(!ver.str().empty());
1138 
1139 	  elf_symbol_sptr symbol_found =
1140 	    elf_symbol::create(env, i,
1141 			       symbol.st_size,
1142 			       sym_name_str,
1143 			       sym_type, sym_binding,
1144 			       symbol.st_shndx != SHN_UNDEF,
1145 			       symbol.st_shndx == SHN_COMMON,
1146 			       ver, sym_visibility);
1147 	  syms_found.push_back(symbol_found);
1148 	  found = true;
1149 	}
1150 
1151       if (stop_word & 1)
1152 	// The last bit of the stop_word is 1.  That means we need to
1153 	// stop here.  We reached the end of the chain of values
1154 	// referenced by the hask bucket.
1155 	break;
1156     }
1157   return found;
1158 }
1159 
1160 /// Look into the symbol tables of the underlying elf file and find
1161 /// the symbol we are being asked.
1162 ///
1163 /// This function uses the elf hash table (be it the GNU hash table or
1164 /// the sysv hash table) for the symbol lookup.
1165 ///
1166 /// @param env the environment we are operating from.
1167 ///
1168 /// @param elf_handle the elf handle to use.
1169 ///
1170 /// @param ht_kind the kind of hash table to use.  This is returned by
1171 /// the function function find_hash_table_section_index.
1172 ///
1173 /// @param ht_index the index (in the section headers table) of the
1174 /// hash table section to use.
1175 ///
1176 /// @param sym_tab_index the index (in section headers table) of the
1177 /// symbol table index to use with this hash table.
1178 ///
1179 /// @param symbol_name the name of the symbol to look for.
1180 ///
1181 /// @param demangle if true, demangle @p sym_name.
1182 ///
1183 /// @param syms_found the symbols that were actually found with the
1184 /// name @p symbol_name.
1185 ///
1186 /// @return true iff the function found the symbol from the elf hash
1187 /// table.
1188 static bool
lookup_symbol_from_elf_hash_tab(const environment * env,Elf * elf_handle,hash_table_kind ht_kind,size_t ht_index,size_t symtab_index,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms_found)1189 lookup_symbol_from_elf_hash_tab(const environment*		env,
1190 				Elf*				elf_handle,
1191 				hash_table_kind		ht_kind,
1192 				size_t				ht_index,
1193 				size_t				symtab_index,
1194 				const string&			symbol_name,
1195 				bool				demangle,
1196 				vector<elf_symbol_sptr>&	syms_found)
1197 {
1198   if (elf_handle == 0 || symbol_name.empty())
1199     return false;
1200 
1201   if (ht_kind == NO_HASH_TABLE_KIND)
1202     return false;
1203 
1204   if (ht_kind == SYSV_HASH_TABLE_KIND)
1205     return lookup_symbol_from_sysv_hash_tab(env,
1206 					    elf_handle, symbol_name,
1207 					    ht_index,
1208 					    symtab_index,
1209 					    demangle,
1210 					    syms_found);
1211   else if (ht_kind == GNU_HASH_TABLE_KIND)
1212     return lookup_symbol_from_gnu_hash_tab(env,
1213 					   elf_handle, symbol_name,
1214 					   ht_index,
1215 					   symtab_index,
1216 					   demangle,
1217 					   syms_found);
1218   return false;
1219 }
1220 
1221 /// Lookup a symbol from the symbol table directly.
1222 ///
1223 ///
1224 /// @param env the environment we are operating from.
1225 ///
1226 /// @param elf_handle the elf handle to use.
1227 ///
1228 /// @param sym_name the name of the symbol to look up.
1229 ///
1230 /// @param sym_tab_index the index (in the section headers table) of
1231 /// the symbol table section.
1232 ///
1233 /// @param demangle if true, demangle the names found in the symbol
1234 /// table before comparing them with @p sym_name.
1235 ///
1236 /// @param sym_name_found the actual name of the symbol found.
1237 ///
1238 /// @param sym_type the type of the symbol found.
1239 ///
1240 /// @param sym_binding the binding of the symbol found.
1241 ///
1242 /// @param sym_versions the versions of the symbol found.
1243 ///
1244 /// @return true iff the symbol was found.
1245 static bool
lookup_symbol_from_symtab(const environment * env,Elf * elf_handle,const string & sym_name,size_t sym_tab_index,bool demangle,vector<elf_symbol_sptr> & syms_found)1246 lookup_symbol_from_symtab(const environment*		env,
1247 			  Elf*				elf_handle,
1248 			  const string&		sym_name,
1249 			  size_t			sym_tab_index,
1250 			  bool				demangle,
1251 			  vector<elf_symbol_sptr>&	syms_found)
1252 {
1253   // TODO: read all of the symbol table, store it in memory in a data
1254   // structure that associates each symbol with its versions and in
1255   // which lookups of a given symbol is fast.
1256   Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1257   ABG_ASSERT(sym_tab_section);
1258 
1259   GElf_Shdr header_mem;
1260   GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1261 					    &header_mem);
1262 
1263   size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1264   Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1265   GElf_Sym* sym;
1266   char* name_str = 0;
1267   elf_symbol::version ver;
1268   bool found = false;
1269 
1270   for (size_t i = 0; i < symcount; ++i)
1271     {
1272       GElf_Sym sym_mem;
1273       sym = gelf_getsym(symtab, i, &sym_mem);
1274       name_str = elf_strptr(elf_handle,
1275 			    sym_tab_header->sh_link,
1276 			    sym->st_name);
1277 
1278       if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1279 	{
1280 	  elf_symbol::type sym_type =
1281 	    stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1282 	  elf_symbol::binding sym_binding =
1283 	    stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1284 	  elf_symbol::visibility sym_visibility =
1285 	    stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1286 	  bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1287 	  bool sym_is_common = sym->st_shndx == SHN_COMMON;
1288 
1289 	  if (get_version_for_symbol(elf_handle, i,
1290 				     /*get_def_version=*/sym_is_defined,
1291 				     ver))
1292 	    ABG_ASSERT(!ver.str().empty());
1293 	  elf_symbol_sptr symbol_found =
1294 	    elf_symbol::create(env, i, sym->st_size,
1295 			       name_str, sym_type,
1296 			       sym_binding, sym_is_defined,
1297 			       sym_is_common, ver, sym_visibility);
1298 	  syms_found.push_back(symbol_found);
1299 	  found = true;
1300 	}
1301     }
1302 
1303   if (found)
1304     return true;
1305 
1306   return false;
1307 }
1308 
1309 /// Look into the symbol tables of the underlying elf file and see
1310 /// if we find a given symbol.
1311 ///
1312 /// @param env the environment we are operating from.
1313 ///
1314 /// @param symbol_name the name of the symbol to look for.
1315 ///
1316 /// @param demangle if true, try to demangle the symbol name found in
1317 /// the symbol table before comparing it to @p symbol_name.
1318 ///
1319 /// @param syms_found the list of symbols found, with the name @p
1320 /// symbol_name.
1321 ///
1322 /// @param sym_type this is set to the type of the symbol found.  This
1323 /// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1324 /// STT_FUNC, STT_IFUNC, etc ...
1325 ///
1326 /// Note that this parameter is set iff the function returns true.
1327 ///
1328 /// @param sym_binding this is set to the binding of the symbol found.
1329 /// This is a standard elf.h value of the symbol binding kind, that
1330 /// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1331 ///
1332 /// @param symbol_versions the versions of the symbol @p symbol_name,
1333 /// if it was found.
1334 ///
1335 /// @return true iff a symbol with the name @p symbol_name was found.
1336 static bool
lookup_symbol_from_elf(const environment * env,Elf * elf_handle,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms_found)1337 lookup_symbol_from_elf(const environment*		env,
1338 		       Elf*				elf_handle,
1339 		       const string&			symbol_name,
1340 		       bool				demangle,
1341 		       vector<elf_symbol_sptr>&	syms_found)
1342 {
1343   size_t hash_table_index = 0, symbol_table_index = 0;
1344   hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1345 
1346   if (!demangle)
1347     ht_kind = find_hash_table_section_index(elf_handle,
1348 					    hash_table_index,
1349 					    symbol_table_index);
1350 
1351   if (ht_kind == NO_HASH_TABLE_KIND)
1352     {
1353       if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1354 	return false;
1355 
1356       return lookup_symbol_from_symtab(env,
1357 				       elf_handle,
1358 				       symbol_name,
1359 				       symbol_table_index,
1360 				       demangle,
1361 				       syms_found);
1362     }
1363 
1364   return lookup_symbol_from_elf_hash_tab(env,
1365 					 elf_handle,
1366 					 ht_kind,
1367 					 hash_table_index,
1368 					 symbol_table_index,
1369 					 symbol_name,
1370 					 demangle,
1371 					 syms_found);
1372 }
1373 
1374 /// Look into the symbol tables of the underlying elf file and see if
1375 /// we find a given public (global or weak) symbol of function type.
1376 ///
1377 /// @param env the environment we are operating from.
1378 ///
1379 /// @param elf_handle the elf handle to use for the query.
1380 ///
1381 /// @param symbol_name the function symbol to look for.
1382 ///
1383 /// @param func_syms the vector of public functions symbols found, if
1384 /// any.
1385 ///
1386 /// @return true iff the symbol was found.
1387 static bool
lookup_public_function_symbol_from_elf(const environment * env,Elf * elf_handle,const string & symbol_name,vector<elf_symbol_sptr> & func_syms)1388 lookup_public_function_symbol_from_elf(const environment*		env,
1389 				       Elf*				elf_handle,
1390 				       const string&			symbol_name,
1391 				       vector<elf_symbol_sptr>&	func_syms)
1392 {
1393   vector<elf_symbol_sptr> syms_found;
1394   bool found = false;
1395 
1396   if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1397 			     /*demangle=*/false, syms_found))
1398     {
1399       for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1400 	   i != syms_found.end();
1401 	   ++i)
1402 	{
1403 	  elf_symbol::type type = (*i)->get_type();
1404 	  elf_symbol::binding binding = (*i)->get_binding();
1405 
1406 	  if ((type == elf_symbol::FUNC_TYPE
1407 	       || type == elf_symbol::GNU_IFUNC_TYPE
1408 	       || type == elf_symbol::COMMON_TYPE)
1409 	      && (binding == elf_symbol::GLOBAL_BINDING
1410 		  || binding == elf_symbol::WEAK_BINDING))
1411 	    {
1412 	      func_syms.push_back(*i);
1413 	      found = true;
1414 	    }
1415 	}
1416     }
1417 
1418   return found;
1419 }
1420 
1421 /// Get data tag information of an ELF file by looking up into its
1422 /// dynamic segment
1423 ///
1424 /// @param elf the elf handle to use for the query.
1425 ///
1426 /// @param dt_tag data tag to look for in dynamic segment
1427 /// @param dt_tag_data vector of found information for a given @p data_tag
1428 ///
1429 /// @return true iff data tag @p data_tag was found
1430 
1431 bool
lookup_data_tag_from_dynamic_segment(Elf * elf,Elf64_Sxword data_tag,vector<string> & dt_tag_data)1432 lookup_data_tag_from_dynamic_segment(Elf*                       elf,
1433                                      Elf64_Sxword               data_tag,
1434                                      vector<string>&            dt_tag_data)
1435 {
1436   size_t num_prog_headers = 0;
1437   bool found = false;
1438   if (elf_getphdrnum(elf, &num_prog_headers) < 0)
1439     return found;
1440 
1441   // Cycle through each program header.
1442   for (size_t i = 0; i < num_prog_headers; ++i)
1443     {
1444       GElf_Phdr phdr_mem;
1445       GElf_Phdr *phdr = gelf_getphdr(elf, i, &phdr_mem);
1446       if (phdr == NULL || phdr->p_type != PT_DYNAMIC)
1447         continue;
1448 
1449       // Poke at the dynamic segment like a section, so that we can
1450       // get its section header information; also we'd like to read
1451       // the data of the segment by using elf_getdata() but that
1452       // function needs a Elf_Scn data structure to act on.
1453       // Elfutils doesn't really have any particular function to
1454       // access segment data, other than the functions used to
1455       // access section data.
1456       Elf_Scn *dynamic_section = gelf_offscn(elf, phdr->p_offset);
1457       GElf_Shdr  shdr_mem;
1458       GElf_Shdr *dynamic_section_header = gelf_getshdr(dynamic_section,
1459 						       &shdr_mem);
1460       if (dynamic_section_header == NULL
1461           || dynamic_section_header->sh_type != SHT_DYNAMIC)
1462         continue;
1463 
1464       // Get data of the dynamic segment (seen as a section).
1465       Elf_Data *data = elf_getdata(dynamic_section, NULL);
1466       if (data == NULL)
1467         continue;
1468 
1469       // Get the index of the section headers string table.
1470       size_t string_table_index = 0;
1471       ABG_ASSERT (elf_getshdrstrndx(elf, &string_table_index) >= 0);
1472 
1473       size_t dynamic_section_header_entry_size = gelf_fsize(elf,
1474                                                             ELF_T_DYN, 1,
1475                                                             EV_CURRENT);
1476 
1477       GElf_Shdr link_mem;
1478       GElf_Shdr *link =
1479         gelf_getshdr(elf_getscn(elf,
1480                                 dynamic_section_header->sh_link),
1481 		     &link_mem);
1482       ABG_ASSERT(link != NULL);
1483 
1484       size_t num_dynamic_section_entries =
1485         dynamic_section_header->sh_size / dynamic_section_header_entry_size;
1486 
1487       // Now walk through all the DT_* data tags that are in the
1488       // segment/section
1489       for (size_t j = 0; j < num_dynamic_section_entries; ++j)
1490         {
1491           GElf_Dyn dynamic_section_mem;
1492           GElf_Dyn *dynamic_section = gelf_getdyn(data,
1493                                                   j,
1494                                                   &dynamic_section_mem);
1495           if (dynamic_section->d_tag == data_tag)
1496             {
1497               dt_tag_data.push_back(elf_strptr(elf,
1498                                                dynamic_section_header->sh_link,
1499 					       dynamic_section->d_un.d_val));
1500               found = true;
1501             }
1502         }
1503     }
1504   return found;
1505 }
1506 
1507 /// Convert the type of ELF file into @ref elf_type.
1508 ///
1509 /// @param elf the elf handle to use for the query.
1510 ///
1511 /// @return the @ref elf_type for a given elf type.
1512 static elf_type
elf_file_type(Elf * elf)1513 elf_file_type(Elf* elf)
1514 {
1515   GElf_Ehdr ehdr_mem;
1516   GElf_Ehdr *header = gelf_getehdr (elf, &ehdr_mem);
1517   vector<string> dt_debug_data;
1518 
1519   switch (header->e_type)
1520     {
1521     case ET_DYN:
1522       if (lookup_data_tag_from_dynamic_segment(elf, DT_DEBUG, dt_debug_data))
1523 	return ELF_TYPE_PI_EXEC;
1524       else
1525 	return ELF_TYPE_DSO;
1526     case ET_EXEC:
1527       return ELF_TYPE_EXEC;
1528     case ET_REL:
1529       return ELF_TYPE_RELOCATABLE;
1530     default:
1531       return ELF_TYPE_UNKNOWN;
1532     }
1533 }
1534 
1535 // ---------------------------------------
1536 // <location expression evaluation types>
1537 // ---------------------------------------
1538 
1539 /// An abstraction of a value representing the result of the
1540 /// evaluation of a dwarf expression.  This is abstraction represents
1541 /// a partial view on the possible values because we are only
1542 /// interested in extracting the latest and longuest constant
1543 /// sub-expression of a given dwarf expression.
1544 class expr_result
1545 {
1546   bool is_const_;
1547   int64_t const_value_;
1548 
1549 public:
expr_result()1550   expr_result()
1551     : is_const_(true),
1552       const_value_(0)
1553   {}
1554 
expr_result(bool is_const)1555   expr_result(bool is_const)
1556     : is_const_(is_const),
1557       const_value_(0)
1558   {}
1559 
expr_result(int64_t v)1560   explicit expr_result(int64_t v)
1561     :is_const_(true),
1562      const_value_(v)
1563   {}
1564 
1565   /// @return true if the value is a constant.  Otherwise, return
1566   /// false, meaning the value represents a quantity for which we need
1567   /// inferior (a running program) state to determine the value.
1568   bool
is_const() const1569   is_const() const
1570   {return is_const_;}
1571 
1572 
1573   /// @param f a flag saying if the value is set to a constant or not.
1574   void
is_const(bool f)1575   is_const(bool f)
1576   {is_const_ = f;}
1577 
1578   /// Get the current constant value iff this represents a
1579   /// constant.
1580   ///
1581   /// @param value the out parameter.  Is set to the constant value of
1582   /// the @ref expr_result.  This is set iff the function return true.
1583   ///
1584   ///@return true if this has a constant value, false otherwise.
1585   bool
const_value(int64_t & value)1586   const_value(int64_t& value)
1587   {
1588     if (is_const())
1589       {
1590 	value = const_value_;
1591 	return true;
1592       }
1593     return false;
1594   }
1595 
1596   /// Getter of the constant value of the current @ref expr_result.
1597   ///
1598   /// Note that the current @ref expr_result must be constant,
1599   /// otherwise the current process is aborted.
1600   ///
1601   /// @return the constant value of the current @ref expr_result.
1602   int64_t
const_value() const1603   const_value() const
1604   {
1605     ABG_ASSERT(is_const());
1606     return const_value_;
1607   }
1608 
operator int64_t() const1609   operator int64_t() const
1610   {return const_value();}
1611 
1612   expr_result&
operator =(const int64_t v)1613   operator=(const int64_t v)
1614   {
1615     const_value_ = v;
1616     return *this;
1617   }
1618 
1619   bool
operator ==(const expr_result & o) const1620   operator==(const expr_result& o) const
1621   {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1622 
1623   bool
operator >=(const expr_result & o) const1624   operator>=(const expr_result& o) const
1625   {return const_value_ >= o.const_value_;}
1626 
1627   bool
operator <=(const expr_result & o) const1628   operator<=(const expr_result& o) const
1629   {return const_value_ <= o.const_value_;}
1630 
1631   bool
operator >(const expr_result & o) const1632   operator>(const expr_result& o) const
1633   {return const_value_ > o.const_value_;}
1634 
1635   bool
operator <(const expr_result & o) const1636   operator<(const expr_result& o) const
1637   {return const_value_ < o.const_value_;}
1638 
1639   expr_result
operator +(const expr_result & v) const1640   operator+(const expr_result& v) const
1641   {
1642     expr_result r(*this);
1643     r.const_value_ += v.const_value_;
1644     r.is_const_ = r.is_const_ && v.is_const_;
1645     return r;
1646   }
1647 
1648   expr_result&
operator +=(int64_t v)1649   operator+=(int64_t v)
1650   {
1651     const_value_ += v;
1652     return *this;
1653   }
1654 
1655   expr_result
operator -(const expr_result & v) const1656   operator-(const expr_result& v) const
1657   {
1658     expr_result r(*this);
1659     r.const_value_ -= v.const_value_;
1660     r.is_const_ = r.is_const_ && v.is_const_;
1661     return r;
1662   }
1663 
1664   expr_result
operator %(const expr_result & v) const1665   operator%(const expr_result& v) const
1666   {
1667     expr_result r(*this);
1668     r.const_value_ %= v.const_value_;
1669     r.is_const_ = r.is_const_ && v.is_const();
1670     return r;
1671   }
1672 
1673   expr_result
operator *(const expr_result & v) const1674   operator*(const expr_result& v) const
1675   {
1676     expr_result r(*this);
1677     r.const_value_ *= v.const_value_;
1678     r.is_const_ = r.is_const_ && v.is_const();
1679     return r;
1680   }
1681 
1682   expr_result
operator |(const expr_result & v) const1683   operator|(const expr_result& v) const
1684   {
1685     expr_result r(*this);
1686     r.const_value_ |= v.const_value_;
1687     r.is_const_ = r.is_const_ && v.is_const_;
1688     return r;
1689   }
1690 
1691   expr_result
operator ^(const expr_result & v) const1692   operator^(const expr_result& v) const
1693   {
1694     expr_result r(*this);
1695     r.const_value_ ^= v.const_value_;
1696     r.is_const_ = r.is_const_ && v.is_const_;
1697     return r;
1698   }
1699 
1700   expr_result
operator >>(const expr_result & v) const1701   operator>>(const expr_result& v) const
1702   {
1703     expr_result r(*this);
1704     r.const_value_ = r.const_value_ >> v.const_value_;
1705     r.is_const_ = r.is_const_ && v.is_const_;
1706     return r;
1707   }
1708 
1709   expr_result
operator <<(const expr_result & v) const1710   operator<<(const expr_result& v) const
1711   {
1712     expr_result r(*this);
1713     r.const_value_ = r.const_value_ << v.const_value_;
1714     r.is_const_ = r.is_const_ && v.is_const_;
1715     return r;
1716   }
1717 
1718   expr_result
operator ~() const1719   operator~() const
1720   {
1721     expr_result r(*this);
1722     r.const_value_ = ~r.const_value_;
1723     return r;
1724   }
1725 
1726   expr_result
neg() const1727   neg() const
1728   {
1729     expr_result r(*this);
1730     r.const_value_ = -r.const_value_;
1731     return r;
1732   }
1733 
1734   expr_result
abs() const1735   abs() const
1736   {
1737     expr_result r = *this;
1738     r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1739     return r;
1740   }
1741 
1742   expr_result
operator &(const expr_result & o)1743   operator&(const expr_result& o)
1744   {
1745     expr_result r(*this);
1746     r.const_value_ &= o.const_value_;
1747     r.is_const_ = r.is_const_ && o.is_const_;
1748     return r;
1749   }
1750 
1751   expr_result
operator /(const expr_result & o)1752   operator/(const expr_result& o)
1753   {
1754     expr_result r(*this);
1755     r.is_const_ = r.is_const_ && o.is_const_;
1756     return r.const_value() / o.const_value();
1757   }
1758 };// class end expr_result;
1759 
1760 /// A class that implements a stack of @ref expr_result, to be used in
1761 /// the engine evaluating DWARF expressions.
1762 class expr_result_stack_type
1763 {
1764   vector<expr_result> elems_;
1765 
1766 public:
1767 
expr_result_stack_type()1768   expr_result_stack_type()
1769   {elems_.reserve(4);}
1770 
1771   expr_result&
operator [](unsigned i)1772   operator[](unsigned i)
1773   {
1774     unsigned s = elems_.size();
1775     ABG_ASSERT(s > i);
1776     return elems_[s - 1 -i];
1777   }
1778 
1779   const expr_result&
operator [](unsigned i) const1780   operator[](unsigned i) const
1781   {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1782 
1783   unsigned
size() const1784   size() const
1785   {return elems_.size();}
1786 
1787   vector<expr_result>::reverse_iterator
begin()1788   begin()
1789   {return elems_.rbegin();}
1790 
1791   const vector<expr_result>::reverse_iterator
begin() const1792   begin() const
1793   {return const_cast<expr_result_stack_type*>(this)->begin();}
1794 
1795   vector<expr_result>::reverse_iterator
end()1796   end()
1797   {return elems_.rend();}
1798 
1799   const vector<expr_result>::reverse_iterator
end() const1800   end() const
1801   {return const_cast<expr_result_stack_type*>(this)->end();}
1802 
1803   expr_result&
front()1804   front()
1805   {return elems_.back();}
1806 
1807   const expr_result&
front() const1808   front() const
1809   {return const_cast<expr_result_stack_type*>(this)->front();}
1810 
1811   void
push_front(expr_result e)1812   push_front(expr_result e)
1813   {elems_.push_back(e);}
1814 
1815   expr_result
pop_front()1816   pop_front()
1817   {
1818     expr_result r = front();
1819     elems_.pop_back();
1820     return r;
1821   }
1822 
1823   void
erase(vector<expr_result>::reverse_iterator i)1824   erase(vector<expr_result>::reverse_iterator i)
1825   {elems_.erase(--i.base());}
1826 
1827   void
clear()1828   clear()
1829   {elems_.clear();}
1830 }; // end class expr_result_stack_type
1831 
1832 /// Abstraction of the evaluation context of a dwarf expression.
1833 struct dwarf_expr_eval_context
1834 {
1835   expr_result accum;
1836   expr_result_stack_type stack;
1837   // Is set to true if the result of the expression that got evaluated
1838   // is a TLS address.
1839   bool set_tls_addr;
1840 
dwarf_expr_eval_contextabigail::dwarf_reader::dwarf_expr_eval_context1841   dwarf_expr_eval_context()
1842     : accum(/*is_const=*/false),
1843       set_tls_addr(false)
1844   {
1845     stack.push_front(expr_result(true));
1846   }
1847 
1848   void
resetabigail::dwarf_reader::dwarf_expr_eval_context1849   reset()
1850   {
1851     stack.clear();
1852     stack.push_front(expr_result(true));
1853     accum = expr_result(false);
1854     set_tls_addr = false;
1855   }
1856 
1857   /// Set a flag to to tell that the result of the expression that got
1858   /// evaluated is a TLS address.
1859   ///
1860   /// @param f true iff the result of the expression that got
1861   /// evaluated is a TLS address, false otherwise.
1862   void
set_tls_addressabigail::dwarf_reader::dwarf_expr_eval_context1863   set_tls_address(bool f)
1864   {set_tls_addr = f;}
1865 
1866   /// Getter for the flag that tells if the result of the expression
1867   /// that got evaluated is a TLS address.
1868   ///
1869   /// @return true iff the result of the expression that got evaluated
1870   /// is a TLS address.
1871   bool
set_tls_addressabigail::dwarf_reader::dwarf_expr_eval_context1872   set_tls_address() const
1873   {return set_tls_addr;}
1874 
1875   expr_result
popabigail::dwarf_reader::dwarf_expr_eval_context1876   pop()
1877   {
1878     expr_result r = stack.front();
1879     stack.pop_front();
1880     return r;
1881   }
1882 
1883   void
pushabigail::dwarf_reader::dwarf_expr_eval_context1884   push(const expr_result& v)
1885   {stack.push_front(v);}
1886 };//end class dwarf_expr_eval_context
1887 
1888 // ---------------------------------------
1889 // </location expression evaluation types>
1890 // ---------------------------------------
1891 
1892 /// The context used to build ABI corpus from debug info in DWARF
1893 /// format.
1894 ///
1895 /// This context is to be created by create_read_context().  It's then
1896 /// passed to all the routines that read specific dwarf bits as they
1897 /// get some important data from it.
1898 ///
1899 /// When a new data member is added to this context, it must be
1900 /// initiliazed by the read_context::initiliaze() function.  So please
1901 /// do not forget.
1902 class read_context
1903 {
1904 public:
1905   struct options_type
1906   {
1907     environment*	env;
1908     bool		load_in_linux_kernel_mode;
1909     bool		load_all_types;
1910     bool		show_stats;
1911     bool		do_log;
1912 
options_typeabigail::dwarf_reader::read_context::options_type1913     options_type()
1914       : env(),
1915 	load_in_linux_kernel_mode(),
1916 	load_all_types(),
1917 	show_stats(),
1918 	do_log()
1919     {}
1920   };// read_context::options_type
1921 
1922   /// A set of containers that contains one container per kind of @ref
1923   /// die_source.  This allows to associate DIEs to things, depending
1924   /// on the source of the DIE.
1925   template <typename ContainerType>
1926   class die_source_dependant_container_set
1927   {
1928     ContainerType primary_debug_info_container_;
1929     ContainerType alt_debug_info_container_;
1930     ContainerType type_unit_container_;
1931 
1932   public:
1933 
1934     /// Getter for the container associated to DIEs coming from a
1935     /// given @ref die_source.
1936     ///
1937     /// @param source the die_source for which we want the container.
1938     ///
1939     /// @return the container that associates DIEs coming from @p
1940     /// source to something.
1941     ContainerType&
get_container(die_source source)1942     get_container(die_source source)
1943     {
1944       ContainerType *result = 0;
1945       switch (source)
1946 	{
1947 	case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1948 	  result = &primary_debug_info_container_;
1949 	  break;
1950 	case ALT_DEBUG_INFO_DIE_SOURCE:
1951 	  result = &alt_debug_info_container_;
1952 	  break;
1953 	case TYPE_UNIT_DIE_SOURCE:
1954 	  result = &type_unit_container_;
1955 	  break;
1956 	case NO_DEBUG_INFO_DIE_SOURCE:
1957 	case NUMBER_OF_DIE_SOURCES:
1958 	  ABG_ASSERT_NOT_REACHED;
1959 	}
1960       return *result;
1961     }
1962 
1963     /// Getter for the container associated to DIEs coming from a
1964     /// given @ref die_source.
1965     ///
1966     /// @param source the die_source for which we want the container.
1967     ///
1968     /// @return the container that associates DIEs coming from @p
1969     /// source to something.
1970     const ContainerType&
get_container(die_source source) const1971     get_container(die_source source) const
1972     {
1973       return const_cast<die_source_dependant_container_set*>(this)->
1974 	get_container(source);
1975     }
1976 
1977     /// Getter for the container associated to DIEs coming from the
1978     /// same source as a given DIE.
1979     ///
1980     /// @param ctxt the read context to consider.
1981     ///
1982     /// @param die the DIE which should have the same source as the
1983     /// source of the container we want.
1984     ///
1985     /// @return the container that associates DIEs coming from the
1986     /// same source as @p die.
1987     ContainerType&
get_container(const read_context & ctxt,const Dwarf_Die * die)1988     get_container(const read_context& ctxt, const Dwarf_Die *die)
1989     {
1990       const die_source source = ctxt.get_die_source(die);
1991       return get_container(source);
1992     }
1993 
1994     /// Getter for the container associated to DIEs coming from the
1995     /// same source as a given DIE.
1996     ///
1997     /// @param ctxt the read context to consider.
1998     ///
1999     /// @param die the DIE which should have the same source as the
2000     /// source of the container we want.
2001     ///
2002     /// @return the container that associates DIEs coming from the
2003     /// same source as @p die.
2004     const ContainerType&
get_container(const read_context & ctxt,const Dwarf_Die * die) const2005     get_container(const read_context& ctxt, const Dwarf_Die *die) const
2006     {
2007       return const_cast<die_source_dependant_container_set*>(this)->
2008 	get_container(ctxt, die);
2009     }
2010 
2011     /// Clear the container set.
2012     void
clear()2013     clear()
2014     {
2015       primary_debug_info_container_.clear();
2016       alt_debug_info_container_.clear();
2017       type_unit_container_.clear();
2018     }
2019   }; // end die_dependant_container_set
2020 
2021   suppr::suppressions_type	supprs_;
2022   unsigned short		dwarf_version_;
2023   Dwfl_Callbacks		offline_callbacks_;
2024   // The set of directories under which to look for debug info.
2025   vector<char**>		debug_info_root_paths_;
2026   dwfl_sptr			handle_;
2027   Dwarf*			dwarf_;
2028   // The alternate debug info.  Alternate debug info sections are a
2029   // DWARF extension as of DWARF4 and are described at
2030   // http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.  Below are
2031   // the file desctor used to access the alternate debug info
2032   // sections, and the representation of the DWARF debug info.  Both
2033   // need to be freed after we are done using them, with fclose and
2034   // dwarf_end.
2035   int				alt_fd_;
2036   Dwarf*			alt_dwarf_;
2037   string			alt_debug_info_path_;
2038   // The address range of the offline elf file we are looking at.
2039   Dwfl_Module*			elf_module_;
2040   mutable Elf*			elf_handle_;
2041   string			elf_path_;
2042   mutable Elf_Scn*		symtab_section_;
2043   Dwarf_Die*			cur_tu_die_;
2044   mutable dwarf_expr_eval_context	dwarf_expr_eval_context_;
2045   // A set of maps (one per kind of die source) that associates a decl
2046   // string representation with the DIEs (offsets) representing that
2047   // decl.
2048   mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2049   decl_die_repr_die_offsets_maps_;
2050   // A set of maps (one per kind of die source) that associates a type
2051   // string representation with the DIEs (offsets) representing that
2052   // type.
2053   mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2054   type_die_repr_die_offsets_maps_;
2055   mutable die_source_dependant_container_set<die_istring_map_type>
2056   die_qualified_name_maps_;
2057   mutable die_source_dependant_container_set<die_istring_map_type>
2058   die_pretty_repr_maps_;
2059   mutable die_source_dependant_container_set<die_istring_map_type>
2060   die_pretty_type_repr_maps_;
2061   // A set of maps (one per kind of die source) that associates the
2062   // offset of a decl die to its corresponding decl artifact.
2063   mutable die_source_dependant_container_set<die_artefact_map_type>
2064   decl_die_artefact_maps_;
2065   // A set of maps (one per kind of die source) that associates the
2066   // offset of a type die to its corresponding type artifact.
2067   mutable die_source_dependant_container_set<die_artefact_map_type>
2068   type_die_artefact_maps_;
2069   /// A set of vectors (one per kind of die source) that associates
2070   /// the offset of a type DIE to the offset of its canonical DIE.
2071   mutable die_source_dependant_container_set<offset_offset_map_type>
2072   canonical_type_die_offsets_;
2073   /// A set of vectors (one per kind of die source) that associates
2074   /// the offset of a decl DIE to the offset of its canonical DIE.
2075   mutable die_source_dependant_container_set<offset_offset_map_type>
2076   canonical_decl_die_offsets_;
2077   /// A map that associates a function type representations to
2078   /// function types, inside a translation unit.
2079   mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
2080 
2081   die_class_or_union_map_type	die_wip_classes_map_;
2082   die_class_or_union_map_type	alternate_die_wip_classes_map_;
2083   die_class_or_union_map_type	type_unit_die_wip_classes_map_;
2084   die_function_type_map_type	die_wip_function_types_map_;
2085   die_function_type_map_type	alternate_die_wip_function_types_map_;
2086   die_function_type_map_type	type_unit_die_wip_function_types_map_;
2087   die_function_decl_map_type	die_function_with_no_symbol_map_;
2088   vector<Dwarf_Off>		types_to_canonicalize_;
2089   vector<Dwarf_Off>		alt_types_to_canonicalize_;
2090   vector<Dwarf_Off>		type_unit_types_to_canonicalize_;
2091   vector<type_base_sptr>	extra_types_to_canonicalize_;
2092   string_classes_map		decl_only_classes_map_;
2093   string_enums_map		decl_only_enums_map_;
2094   die_tu_map_type		die_tu_map_;
2095   corpus_group_sptr		cur_corpus_group_;
2096   corpus_sptr			cur_corpus_;
2097   translation_unit_sptr	cur_tu_;
2098   scope_decl_sptr		nil_scope_;
2099   scope_stack_type		scope_stack_;
2100   offset_offset_map_type	primary_die_parent_map_;
2101   // A map that associates each tu die to a vector of unit import
2102   // points, in the main debug info
2103   tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
2104   // A map that associates each tu die to a vector of unit import
2105   // points, in the alternate debug info
2106   tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
2107   tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
2108   // A DIE -> parent map for DIEs coming from the alternate debug info
2109   // file.
2110   offset_offset_map_type	alternate_die_parent_map_;
2111   offset_offset_map_type	type_section_die_parent_map_;
2112   list<var_decl_sptr>		var_decls_to_add_;
2113   vector<string>		dt_needed_;
2114   string			dt_soname_;
2115   string			elf_architecture_;
2116   corpus::exported_decls_builder* exported_decls_builder_;
2117   options_type			options_;
2118   bool				drop_undefined_syms_;
2119   read_context();
2120 
2121 private:
2122   mutable symtab_reader::symtab_sptr symtab_;
2123 
2124 public:
2125 
2126   /// Constructor of read_context.
2127   ///
2128   /// @param elf_path the path to the elf file the context is to be
2129   /// used for.
2130   ///
2131   /// @param debug_info_root_paths a vector of pointers to the path to
2132   /// the root directory under which the debug info is to be found for
2133   /// @p elf_path.  Leave this empty if the debug info is not in a
2134   /// split file.
2135   ///
2136   /// @param environment the environment used by the current context.
2137   /// This environment contains resources needed by the reader and by
2138   /// the types and declarations that are to be created later.  Note
2139   /// that ABI artifacts that are to be compared all need to be
2140   /// created within the same environment.
2141   ///
2142   /// Please also note that the life time of this environment object
2143   /// must be greater than the life time of the resulting @ref
2144   /// read_context the context uses resources that are allocated in
2145   /// the environment.
2146   ///
2147   /// @param load_all_types if set to false only the types that are
2148   /// reachable from publicly exported declarations (of functions and
2149   /// variables) are read.  If set to true then all types found in the
2150   /// debug information are loaded.
2151   ///
2152   /// @param linux_kernel_mode if set to true, then consider the special
2153   /// linux kernel symbol tables when determining if a symbol is
2154   /// exported or not.
read_context(const string & elf_path,const vector<char ** > & debug_info_root_paths,ir::environment * environment,bool load_all_types,bool linux_kernel_mode)2155   read_context(const string&	elf_path,
2156 	       const vector<char**>& debug_info_root_paths,
2157 	       ir::environment* environment,
2158 	       bool		load_all_types,
2159 	       bool		linux_kernel_mode)
2160   {
2161     initialize(elf_path, debug_info_root_paths, environment,
2162 	       load_all_types, linux_kernel_mode);
2163   }
2164 
2165   /// Initializer of read_context.
2166   ///
2167   /// @param elf_path the path to the elf file the context is to be
2168   /// used for.
2169   ///
2170   /// @param debug_info_root_paths a vector of pointers to the path to
2171   /// the root directory under which the debug info is to be found for
2172   /// @p elf_path.  Leave this empty if the debug info is not in a
2173   /// split file.
2174   ///
2175   /// @param environment the environment used by the current context.
2176   /// This environment contains resources needed by the reader and by
2177   /// the types and declarations that are to be created later.  Note
2178   /// that ABI artifacts that are to be compared all need to be
2179   /// created within the same environment.
2180   ///
2181   /// Please also note that the life time of this environment object
2182   /// must be greater than the life time of the resulting @ref
2183   /// read_context the context uses resources that are allocated in
2184   /// the environment.
2185   ///
2186   /// @param load_all_types if set to false only the types that are
2187   /// reachable from publicly exported declarations (of functions and
2188   /// variables) are read.  If set to true then all types found in the
2189   /// debug information are loaded.
2190   ///
2191   /// @param linux_kernel_mode if set to true, then consider the
2192   /// special linux kernel symbol tables when determining if a symbol
2193   /// is exported or not.
2194   void
initialize(const string & elf_path,const vector<char ** > & debug_info_root_paths,ir::environment * environment,bool load_all_types,bool linux_kernel_mode)2195   initialize(const string&	elf_path,
2196 	     const vector<char**>& debug_info_root_paths,
2197 	     ir::environment* environment,
2198 	     bool		load_all_types,
2199 	     bool		linux_kernel_mode)
2200   {
2201     dwarf_version_ = 0;
2202     dwarf_ = 0;
2203     handle_.reset();
2204     alt_fd_ = 0;
2205     alt_dwarf_ = 0;
2206     elf_module_ = 0;
2207     elf_handle_ = 0;
2208     elf_path_ = elf_path;
2209     symtab_section_ = 0;
2210     cur_tu_die_ =  0;
2211     exported_decls_builder_ = 0;
2212 
2213     clear_alt_debug_info_data();
2214 
2215     supprs_.clear();
2216     decl_die_repr_die_offsets_maps_.clear();
2217     type_die_repr_die_offsets_maps_.clear();
2218     die_qualified_name_maps_.clear();
2219     die_pretty_repr_maps_.clear();
2220     die_pretty_type_repr_maps_.clear();
2221     decl_die_artefact_maps_.clear();
2222     type_die_artefact_maps_.clear();
2223     canonical_type_die_offsets_.clear();
2224     canonical_decl_die_offsets_.clear();
2225     die_wip_classes_map_.clear();
2226     alternate_die_wip_classes_map_.clear();
2227     type_unit_die_wip_classes_map_.clear();
2228     die_wip_function_types_map_.clear();
2229     alternate_die_wip_function_types_map_.clear();
2230     type_unit_die_wip_function_types_map_.clear();
2231     die_function_with_no_symbol_map_.clear();
2232     types_to_canonicalize_.clear();
2233     alt_types_to_canonicalize_.clear();
2234     type_unit_types_to_canonicalize_.clear();
2235     extra_types_to_canonicalize_.clear();
2236     decl_only_classes_map_.clear();
2237     die_tu_map_.clear();
2238     cur_corpus_group_.reset();
2239     cur_corpus_.reset();
2240     cur_tu_.reset();
2241     primary_die_parent_map_.clear();
2242     tu_die_imported_unit_points_map_.clear();
2243     alt_tu_die_imported_unit_points_map_.clear();
2244     type_units_tu_die_imported_unit_points_map_.clear();
2245     alternate_die_parent_map_.clear();
2246     type_section_die_parent_map_.clear();
2247     var_decls_to_add_.clear();
2248     dt_needed_.clear();
2249     dt_soname_.clear();
2250     elf_architecture_.clear();
2251 
2252     symtab_.reset();
2253 
2254     clear_per_translation_unit_data();
2255 
2256     memset(&offline_callbacks_, 0, sizeof(offline_callbacks_));
2257     create_default_dwfl(debug_info_root_paths);
2258     options_.env = environment;
2259     options_.load_in_linux_kernel_mode = linux_kernel_mode;
2260     options_.load_all_types = load_all_types;
2261     drop_undefined_syms_ = false;
2262     load_in_linux_kernel_mode(linux_kernel_mode);
2263   }
2264 
2265   /// Clear the resources related to the alternate DWARF data.
2266   void
clear_alt_debug_info_data()2267   clear_alt_debug_info_data()
2268   {
2269     if (alt_fd_)
2270       {
2271 	close(alt_fd_);
2272 	alt_fd_ = 0;
2273 	if (alt_dwarf_)
2274 	  {
2275 	    dwarf_end(alt_dwarf_);
2276 	    alt_dwarf_ = 0;
2277 	  }
2278 	alt_debug_info_path_.clear();
2279       }
2280   }
2281 
2282   /// Detructor of the @ref read_context type.
~read_context()2283   ~read_context()
2284   {
2285     clear_alt_debug_info_data();
2286   }
2287 
2288   /// Clear the data that is relevant only for the current translation
2289   /// unit being read.  The rest of the data is relevant for the
2290   /// entire ABI corpus.
2291   void
clear_per_translation_unit_data()2292   clear_per_translation_unit_data()
2293   {
2294     while (!scope_stack().empty())
2295       scope_stack().pop();
2296     var_decls_to_re_add_to_tree().clear();
2297     per_tu_repr_to_fn_type_maps().clear();
2298   }
2299 
2300   /// Clear the data that is relevant for the current corpus being
2301   /// read.
2302   void
clear_per_corpus_data()2303   clear_per_corpus_data()
2304   {
2305     die_qualified_name_maps_.clear();
2306     die_pretty_repr_maps_.clear();
2307     die_pretty_type_repr_maps_.clear();
2308     clear_types_to_canonicalize();
2309   }
2310 
2311   /// Getter for the current environment.
2312   ///
2313   /// @return the current environment.
2314   const ir::environment*
env() const2315   env() const
2316   {return options_.env;}
2317 
2318   /// Getter for the current environment.
2319   ///
2320   /// @return the current environment.
2321   ir::environment*
env()2322   env()
2323   {return options_.env;}
2324 
2325   /// Setter for the current environment.
2326   ///
2327   /// @param env the new current environment.
2328   void
env(ir::environment * env)2329   env(ir::environment* env)
2330   {options_.env = env;}
2331 
2332   /// Getter for the flag that tells us if we are dropping functions
2333   /// and variables that have undefined symbols.
2334   ///
2335   /// @return true iff we are dropping functions and variables that have
2336   /// undefined symbols.
2337   bool
drop_undefined_syms() const2338   drop_undefined_syms() const
2339   {return drop_undefined_syms_;}
2340 
2341   /// Setter for the flag that tells us if we are dropping functions
2342   /// and variables that have undefined symbols.
2343   ///
2344   /// @param f the new value of the flag.
2345   void
drop_undefined_syms(bool f)2346   drop_undefined_syms(bool f)
2347   {drop_undefined_syms_ = f;}
2348 
2349   /// Getter of the suppression specifications to be used during
2350   /// ELF/DWARF parsing.
2351   ///
2352   /// @return the suppression specifications.
2353   const suppr::suppressions_type&
get_suppressions() const2354   get_suppressions() const
2355   {return supprs_;}
2356 
2357   /// Getter of the suppression specifications to be used during
2358   /// ELF/DWARF parsing.
2359   ///
2360   /// @return the suppression specifications.
2361   suppr::suppressions_type&
get_suppressions()2362   get_suppressions()
2363   {return supprs_;}
2364 
2365   /// Getter for the callbacks of the Dwarf Front End library of
2366   /// elfutils that is used by this reader to read dwarf.
2367   ///
2368   /// @return the callbacks.
2369   const Dwfl_Callbacks*
offline_callbacks() const2370   offline_callbacks() const
2371   {return &offline_callbacks_;}
2372 
2373   /// Getter for the callbacks of the Dwarf Front End library of
2374   /// elfutils that is used by this reader to read dwarf.
2375   /// @returnthe callbacks
2376   Dwfl_Callbacks*
offline_callbacks()2377   offline_callbacks()
2378   {return &offline_callbacks_;}
2379 
2380   /// Constructor for a default Dwfl handle that knows how to load debug
2381   /// info from a library or executable elf file.
2382   ///
2383   /// @param debug_info_root_paths a vector of pointers to the root
2384   /// path under which to look for the debug info of the elf files
2385   /// that are later handled by the Dwfl.  This is for cases where the
2386   /// debug info is split into a different file from the binary we
2387   /// want to inspect.  On Red Hat compatible systems, this root path
2388   /// is usually /usr/lib/debug by default.  If this argument is set
2389   /// to the empty set, then "./debug" and /usr/lib/debug will be
2390   /// searched for sub-directories containing the debug info file.
2391   /// Note that for now, elfutils wants this path to be absolute
2392   /// otherwise things just don't work and the debug info is not
2393   /// found.
2394   ///
2395   /// @return the constructed Dwfl handle.
2396   void
create_default_dwfl(const vector<char ** > & debug_info_root_paths)2397   create_default_dwfl(const vector<char**>& debug_info_root_paths)
2398   {
2399     offline_callbacks()->find_debuginfo = dwfl_standard_find_debuginfo;
2400     offline_callbacks()->section_address = dwfl_offline_section_address;
2401     offline_callbacks()->debuginfo_path =
2402       debug_info_root_paths.empty() ? 0 : debug_info_root_paths.front();
2403     handle_.reset(dwfl_begin(offline_callbacks()),
2404 		  dwfl_deleter());
2405     debug_info_root_paths_ = debug_info_root_paths;
2406   }
2407 
2408   unsigned short
dwarf_version() const2409   dwarf_version() const
2410   {return dwarf_version_;}
2411 
2412   void
dwarf_version(unsigned short v)2413   dwarf_version(unsigned short v)
2414   {dwarf_version_ = v;}
2415 
2416   /// Getter for a smart pointer to a handle on the dwarf front end
2417   /// library that we use to read dwarf.
2418   ///
2419   /// @return the dwfl handle.
2420   dwfl_sptr
dwfl_handle() const2421   dwfl_handle() const
2422   {return handle_;}
2423 
2424   /// Setter for a smart pointer to a handle on the dwarf front end
2425   /// library that we use to read dwarf.
2426   ///
2427   /// @param h the new dwfl handle.
2428   void
dwfl_handle(dwfl_sptr & h)2429   dwfl_handle(dwfl_sptr& h)
2430   {handle_ = h;}
2431 
2432   Dwfl_Module*
elf_module() const2433   elf_module() const
2434   {return elf_module_;}
2435 
2436   /// Return the ELF descriptor for the binary we are analizing.
2437   ///
2438   /// @return a pointer to the Elf descriptor representing the binary
2439   /// we are analizing.
2440   Elf*
elf_handle() const2441   elf_handle() const
2442   {
2443     if (elf_handle_ == 0)
2444       {
2445 	if (elf_module())
2446 	  {
2447 	    GElf_Addr bias = 0;
2448 	    elf_handle_ = dwfl_module_getelf(elf_module(), &bias);
2449 	  }
2450       }
2451     return elf_handle_;
2452   }
2453 
2454   /// Return the ELF descriptor used for DWARF access.
2455   ///
2456   /// This can be the same as read_context::elf_handle() above, if the
2457   /// DWARF info is in the same ELF file as the one of the binary we
2458   /// are analizing.  It is different if e.g, the debug info is split
2459   /// from the ELF file we are analizing.
2460   ///
2461   /// @return a pointer to the ELF descriptor used to access debug
2462   /// info.
2463   Elf*
dwarf_elf_handle() const2464   dwarf_elf_handle() const
2465   {return dwarf_getelf(dwarf());}
2466 
2467   /// Test if the debug information is in a separate ELF file wrt the
2468   /// main ELF file of the program (application or shared library) we
2469   /// are analizing.
2470   ///
2471   /// @return true if the debug information is in a separate ELF file
2472   /// compared to the main ELF file of the program (application or
2473   /// shared library) that we are looking at.
2474   bool
dwarf_is_splitted() const2475   dwarf_is_splitted() const
2476   {return dwarf_elf_handle() != elf_handle();}
2477 
2478   /// Add paths to the set of paths under which to look for split
2479   /// debuginfo files.
2480   ///
2481   /// @param debug_info_root_paths the paths to add.
2482   void
add_debug_info_root_paths(const vector<char ** > & debug_info_root_paths)2483   add_debug_info_root_paths(const vector<char **>& debug_info_root_paths)
2484   {
2485     debug_info_root_paths_.insert(debug_info_root_paths_.end(),
2486 				  debug_info_root_paths.begin(),
2487 				  debug_info_root_paths.end());
2488   }
2489 
2490   /// Add a path to the set of paths under which to look for split
2491   /// debuginfo files.
2492   ///
2493   /// @param debug_info_root_path the path to add.
2494   void
add_debug_info_root_path(char ** debug_info_root_path)2495   add_debug_info_root_path(char** debug_info_root_path)
2496   {debug_info_root_paths_.push_back(debug_info_root_path);}
2497 
2498   /// Find the alternate debuginfo file associated to a given elf file.
2499   ///
2500   /// @param elf_module represents the elf file to consider.
2501   ///
2502   /// @param alt_file_name the resulting path to the alternate
2503   /// debuginfo file found.  This is set iff the function returns a
2504   /// non-nil value.
2505   Dwarf*
find_alt_debug_info(Dwfl_Module * elf_module,string & alt_file_name,int & alt_fd)2506   find_alt_debug_info(Dwfl_Module *elf_module,
2507 		      string& alt_file_name,
2508 		      int& alt_fd)
2509   {
2510     Dwarf *result = 0;
2511     result = dwarf_reader::find_alt_debug_info(elf_module,
2512 					       debug_info_root_paths_,
2513 					       alt_file_name, alt_fd);
2514     return result;
2515   }
2516 
2517   /// Load the debug info associated with an elf file that is at a
2518   /// given path.
2519   ///
2520   /// @return a pointer to the DWARF debug info pointer upon
2521   /// successful debug info loading, NULL otherwise.
2522   Dwarf*
load_debug_info()2523   load_debug_info()
2524   {
2525     if (!dwfl_handle())
2526       return 0;
2527 
2528     if (dwarf_)
2529       return dwarf_;
2530 
2531     elf_module_ =
2532       dwfl_report_offline(dwfl_handle().get(),
2533 			  basename(const_cast<char*>(elf_path().c_str())),
2534 			  elf_path().c_str(),
2535 			  -1);
2536     dwfl_report_end(dwfl_handle().get(), 0, 0);
2537 
2538     Dwarf_Addr bias = 0;
2539     dwarf_ = dwfl_module_getdwarf(elf_module_, &bias);
2540     // Look for split debuginfo files under multiple possible
2541     // debuginfo roots.
2542     for (vector<char**>::const_iterator i = debug_info_root_paths_.begin();
2543 	 dwarf_ == 0 && i != debug_info_root_paths_.end();
2544 	 ++i)
2545       {
2546 	offline_callbacks()->debuginfo_path = *i;
2547 	dwarf_ = dwfl_module_getdwarf(elf_module_, &bias);
2548       }
2549 
2550     if (!alt_dwarf_)
2551       alt_dwarf_ = find_alt_debug_info(elf_module_,
2552 				       alt_debug_info_path_,
2553 				       alt_fd_);
2554 
2555     return dwarf_;
2556   }
2557 
2558   /// Return the main debug info we are looking at.
2559   ///
2560   /// @return the main debug info.
2561   Dwarf*
dwarf() const2562   dwarf() const
2563   {return dwarf_;}
2564 
2565   /// Return the alternate debug info we are looking at.
2566   ///
2567   /// Note that "alternate debug info sections" is a GNU extension as
2568   /// of DWARF4 and is described at
2569   /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
2570   ///
2571   /// @return the alternate debug info.
2572   Dwarf*
alt_dwarf() const2573   alt_dwarf() const
2574   {return alt_dwarf_;}
2575 
2576   /// Return the correct debug info, depending on the DIE source we
2577   /// are looking at.
2578   ///
2579   /// @param source the DIE source to consider.
2580   ///
2581   /// @return the right debug info, depending on @p source.
2582   Dwarf*
dwarf_per_die_source(die_source source) const2583   dwarf_per_die_source(die_source source) const
2584   {
2585     Dwarf *result = 0;
2586     switch(source)
2587       {
2588       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2589       case TYPE_UNIT_DIE_SOURCE:
2590 	result = dwarf();
2591 	break;
2592       case ALT_DEBUG_INFO_DIE_SOURCE:
2593 	result = alt_dwarf();
2594 	break;
2595       case NO_DEBUG_INFO_DIE_SOURCE:
2596       case NUMBER_OF_DIE_SOURCES:
2597 	ABG_ASSERT_NOT_REACHED;
2598       }
2599     return result;
2600   }
2601 
2602   /// Return the path to the alternate debug info as contained in the
2603   /// .gnu_debugaltlink section of the main elf file.
2604   ///
2605   /// Note that "alternate debug info sections" is a GNU extension as
2606   /// of DWARF4 and is described at
2607   /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
2608   ///
2609   /// @return the path to the alternate debug info file, or an empty
2610   /// path if no alternate debug info file is associated.
2611   const string&
alt_debug_info_path() const2612   alt_debug_info_path() const
2613   {return alt_debug_info_path_;}
2614 
2615   /// Return the path to the ELF path we are reading.
2616   ///
2617   /// @return the elf path.
2618   const string&
elf_path() const2619   elf_path() const
2620   {return elf_path_;}
2621 
2622   const Dwarf_Die*
cur_tu_die() const2623   cur_tu_die() const
2624   {return cur_tu_die_;}
2625 
2626   void
cur_tu_die(Dwarf_Die * cur_tu_die)2627   cur_tu_die(Dwarf_Die* cur_tu_die)
2628   {cur_tu_die_ = cur_tu_die;}
2629 
2630   dwarf_expr_eval_context&
dwarf_expr_eval_ctxt() const2631   dwarf_expr_eval_ctxt() const
2632   {return dwarf_expr_eval_context_;}
2633 
2634   /// Getter of the maps set that associates a representation of a
2635   /// decl DIE to a vector of offsets of DIEs having that representation.
2636   ///
2637   /// @return the maps set that associates a representation of a decl
2638   /// DIE to a vector of offsets of DIEs having that representation.
2639   const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
decl_die_repr_die_offsets_maps() const2640   decl_die_repr_die_offsets_maps() const
2641   {return decl_die_repr_die_offsets_maps_;}
2642 
2643   /// Getter of the maps set that associates a representation of a
2644   /// decl DIE to a vector of offsets of DIEs having that representation.
2645   ///
2646   /// @return the maps set that associates a representation of a decl
2647   /// DIE to a vector of offsets of DIEs having that representation.
2648   die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
decl_die_repr_die_offsets_maps()2649   decl_die_repr_die_offsets_maps()
2650   {return decl_die_repr_die_offsets_maps_;}
2651 
2652   /// Getter of the maps set that associate a representation of a type
2653   /// DIE to a vector of offsets of DIEs having that representation.
2654   ///
2655   /// @return the maps set that associate a representation of a type
2656   /// DIE to a vector of offsets of DIEs having that representation.
2657   const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
type_die_repr_die_offsets_maps() const2658   type_die_repr_die_offsets_maps() const
2659   {return type_die_repr_die_offsets_maps_;}
2660 
2661   /// Getter of the maps set that associate a representation of a type
2662   /// DIE to a vector of offsets of DIEs having that representation.
2663   ///
2664   /// @return the maps set that associate a representation of a type
2665   /// DIE to a vector of offsets of DIEs having that representation.
2666   die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
type_die_repr_die_offsets_maps()2667   type_die_repr_die_offsets_maps()
2668   {return type_die_repr_die_offsets_maps_;}
2669 
2670 
2671   /// Compute the offset of the canonical DIE of a given DIE.
2672   ///
2673   /// @param die the DIE to consider.
2674   ///
2675   /// @param canonical_die_offset out parameter.  This is set to the
2676   /// resulting canonical DIE that was computed.
2677   ///
2678   /// @param die_as_type if yes, it means @p die has to be considered
2679   /// as a type.
2680   void
compute_canonical_die_offset(const Dwarf_Die * die,Dwarf_Off & canonical_die_offset,bool die_as_type) const2681   compute_canonical_die_offset(const Dwarf_Die *die,
2682 			       Dwarf_Off &canonical_die_offset,
2683 			       bool die_as_type) const
2684   {
2685     offset_offset_map_type &canonical_dies =
2686       die_as_type
2687       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
2688       get_container(*this, die)
2689       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
2690       get_container(*this, die);
2691 
2692     Dwarf_Die canonical_die;
2693     compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2694 
2695     canonical_die_offset = dwarf_dieoffset(&canonical_die);
2696   }
2697 
2698   /// Compute (find) the canonical DIE of a given DIE.
2699   ///
2700   /// @param die the DIE to consider.
2701   ///
2702   /// @param canonical_dies the vector in which the canonical dies ar
2703   /// stored.  The index of each element is the offset of the DIE we
2704   /// want the canonical DIE for.  And the value of the element at
2705   /// that index is the canonical DIE offset we are looking for.
2706   ///
2707   /// @param canonical_die_offset out parameter.  This is set to the
2708   /// resulting canonical DIE that was computed.
2709   ///
2710   /// @param die_as_type if yes, it means @p die has to be considered
2711   /// as a type.
2712   void
compute_canonical_die(const Dwarf_Die * die,offset_offset_map_type & canonical_dies,Dwarf_Die & canonical_die,bool die_as_type) const2713   compute_canonical_die(const Dwarf_Die *die,
2714 			offset_offset_map_type& canonical_dies,
2715 			Dwarf_Die &canonical_die,
2716 			bool die_as_type) const
2717   {
2718     const die_source source = get_die_source(die);
2719 
2720     Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2721 
2722     compute_canonical_die(die_offset, source,
2723 			  canonical_dies,
2724 			  canonical_die, die_as_type);
2725   }
2726 
2727   /// Compute (find) the canonical DIE of a given DIE.
2728   ///
2729   /// @param die_offset the offset of the DIE to consider.
2730   ///
2731   /// @param source the source of the DIE to consider.
2732   ///
2733   /// @param canonical_dies the vector in which the canonical dies ar
2734   /// stored.  The index of each element is the offset of the DIE we
2735   /// want the canonical DIE for.  And the value of the element at
2736   /// that index is the canonical DIE offset we are looking for.
2737   ///
2738   /// @param canonical_die_offset out parameter.  This is set to the
2739   /// resulting canonical DIE that was computed.
2740   ///
2741   /// @param die_as_type if yes, it means @p die has to be considered
2742   /// as a type.
2743   void
compute_canonical_die(Dwarf_Off die_offset,die_source source,offset_offset_map_type & canonical_dies,Dwarf_Die & canonical_die,bool die_as_type) const2744   compute_canonical_die(Dwarf_Off die_offset,
2745 			die_source source,
2746 			offset_offset_map_type& canonical_dies,
2747 			Dwarf_Die &canonical_die,
2748 			bool die_as_type) const
2749   {
2750     // The map that associates the string representation of 'die'
2751     // with a vector of offsets of potentially equivalent DIEs.
2752     istring_dwarf_offsets_map_type& map =
2753       die_as_type
2754       ? (const_cast<read_context*>(this)->
2755 	 type_die_repr_die_offsets_maps().get_container(source))
2756       : (const_cast<read_context*>(this)->
2757 	 decl_die_repr_die_offsets_maps().get_container(source));
2758 
2759     Dwarf_Die die;
2760     ABG_ASSERT(dwarf_offdie(dwarf_per_die_source(source), die_offset, &die));
2761 
2762     // The variable repr is the the string representation of 'die'.
2763     //
2764     // Even if die_as_type is true -- which means that 'die' is said
2765     // to be considered as a type -- we always consider a
2766     // DW_TAG_subprogram DIE as a decl here, as far as its string
2767     // representation is concerned.
2768     interned_string name =
2769       (die_as_type)
2770       ? get_die_pretty_type_representation(&die, /*where=*/0)
2771       : get_die_pretty_representation(&die, /*where=*/0);
2772 
2773     Dwarf_Off canonical_die_offset = 0;
2774     istring_dwarf_offsets_map_type::iterator i = map.find(name);
2775     if (i == map.end())
2776       {
2777 	dwarf_offsets_type offsets;
2778 	offsets.push_back(die_offset);
2779 	map[name] = offsets;
2780 	set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2781 	get_die_from_offset(source, die_offset, &canonical_die);
2782 	return;
2783       }
2784 
2785     if (odr_is_relevant(&die))
2786       {
2787 	// ODR is relevant for this DIE.  In this case, all types with
2788 	// the same name are considered equivalent.  So the array
2789 	// i->second shoud only have on element.  If not, then
2790 	// the DIEs referenced in the array should all compare equal.
2791 	// Otherwise, this is an ODR violation.  In any case, return
2792 	// the first element of the array.
2793 	// ABG_ASSERT(i->second.size() == 1);
2794 	canonical_die_offset = i->second.front();
2795 	get_die_from_offset(source, canonical_die_offset, &canonical_die);
2796 	set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2797 	return;
2798       }
2799 
2800     Dwarf_Off cur_die_offset;
2801     Dwarf_Die potential_canonical_die;
2802     for (dwarf_offsets_type::const_iterator o = i->second.begin();
2803 	 o != i->second.end();
2804 	 ++o)
2805       {
2806 	cur_die_offset = *o;
2807 	get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2808 	if (compare_dies(*this, &die, &potential_canonical_die,
2809 			 /*update_canonical_dies_on_the_fly=*/false))
2810 	  {
2811 	    canonical_die_offset = cur_die_offset;
2812 	    set_canonical_die_offset(canonical_dies, die_offset,
2813 				     canonical_die_offset);
2814 	    get_die_from_offset(source, canonical_die_offset, &canonical_die);
2815 	    return;
2816 	  }
2817       }
2818 
2819     canonical_die_offset = die_offset;
2820     i->second.push_back(die_offset);
2821     set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2822     get_die_from_offset(source, canonical_die_offset, &canonical_die);
2823   }
2824 
2825   /// Getter of the canonical DIE of a given DIE.
2826   ///
2827   /// @param die the DIE to consider.
2828   ///
2829   /// @param canonical_die output parameter.  Is set to the resuling
2830   /// canonical die, if this function returns true.
2831   ///
2832   /// @param where the offset of the logical DIE we are supposed to be
2833   /// calling this function from.  If set to zero this means this is
2834   /// to be ignored.
2835   ///
2836   /// @param die_as_type if set to yes, it means @p die is to be
2837   /// considered as a type DIE.
2838   ///
2839   /// @return true iff a canonical DIE was found for @p die.
2840   bool
get_canonical_die(const Dwarf_Die * die,Dwarf_Die & canonical_die,size_t where,bool die_as_type) const2841   get_canonical_die(const Dwarf_Die *die,
2842 		    Dwarf_Die &canonical_die,
2843 		    size_t where,
2844 		    bool die_as_type) const
2845   {
2846     const die_source source = get_die_source(die);
2847 
2848     offset_offset_map_type &canonical_dies =
2849       die_as_type
2850       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
2851       get_container(source)
2852       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
2853       get_container(source);
2854 
2855     Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2856     if (Dwarf_Off canonical_die_offset =
2857 	get_canonical_die_offset(canonical_dies, die_offset))
2858       {
2859 	get_die_from_offset(source, canonical_die_offset, &canonical_die);
2860 	return true;
2861       }
2862 
2863     // The map that associates the string representation of 'die'
2864     // with a vector of offsets of potentially equivalent DIEs.
2865     istring_dwarf_offsets_map_type& map =
2866       die_as_type
2867       ? (const_cast<read_context*>(this)->
2868 	 type_die_repr_die_offsets_maps().get_container(*this, die))
2869       : (const_cast<read_context*>(this)->
2870 	 decl_die_repr_die_offsets_maps().get_container(*this, die));
2871 
2872     // The variable repr is the the string representation of 'die'.
2873     //
2874     // Even if die_as_type is true -- which means that 'die' is said
2875     // to be considered as a type -- we always consider a
2876     // DW_TAG_subprogram DIE as a decl here, as far as its string
2877     // representation is concerned.
2878     interned_string name =
2879       (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2880       ? get_die_pretty_type_representation(die, where)
2881       : get_die_pretty_representation(die, where);
2882 
2883     istring_dwarf_offsets_map_type::iterator i = map.find(name);
2884     if (i == map.end())
2885       return false;
2886 
2887     if (odr_is_relevant(die))
2888       {
2889 	// ODR is relevant for this DIE.  In this case, all types with
2890 	// the same name are considered equivalent.  So the array
2891 	// i->second shoud only have on element.  If not, then
2892 	// the DIEs referenced in the array should all compare equal.
2893 	// Otherwise, this is an ODR violation.  In any case, return
2894 	// the first element of the array.
2895 	// ABG_ASSERT(i->second.size() == 1);
2896 	Dwarf_Off canonical_die_offset = i->second.front();
2897 	get_die_from_offset(source, canonical_die_offset, &canonical_die);
2898 	set_canonical_die_offset(canonical_dies,
2899 				 die_offset,
2900 				 canonical_die_offset);
2901 	return true;
2902       }
2903 
2904     Dwarf_Off cur_die_offset;
2905     for (dwarf_offsets_type::const_iterator o = i->second.begin();
2906 	 o != i->second.end();
2907 	 ++o)
2908       {
2909 	cur_die_offset = *o;
2910 	get_die_from_offset(source, cur_die_offset, &canonical_die);
2911 	// compare die and canonical_die.
2912 	if (compare_dies(*this, die, &canonical_die,
2913 			 /*update_canonical_dies_on_the_fly=*/true))
2914 	  {
2915 	    set_canonical_die_offset(canonical_dies,
2916 				     die_offset,
2917 				     cur_die_offset);
2918 	    return true;
2919 	  }
2920       }
2921 
2922     return false;
2923   }
2924 
2925   /// Retrieve the canonical DIE of a given DIE.
2926   ///
2927   /// The canonical DIE is a DIE that is structurally equivalent to
2928   /// this one.
2929   ///
2930   /// Note that this function caches the canonical DIE that was
2931   /// computed.  Subsequent invocations of this function on the same
2932   /// DIE return the same cached DIE.
2933   ///
2934   /// @param die the DIE to get a canonical type for.
2935   ///
2936   /// @param canonical_die the resulting canonical DIE.
2937   ///
2938   /// @param where the offset of the logical DIE we are supposed to be
2939   /// calling this function from.  If set to zero this means this is
2940   /// to be ignored.
2941   ///
2942   /// @param die_as_type if true, consider DIE is a type.
2943   ///
2944   /// @return true if an *existing* canonical DIE was found.
2945   /// Otherwise, @p die is considered as being a canonical DIE for
2946   /// itself. @p canonical_die is thus set to the canonical die in
2947   /// either cases.
2948   bool
get_or_compute_canonical_die(const Dwarf_Die * die,Dwarf_Die & canonical_die,size_t where,bool die_as_type) const2949   get_or_compute_canonical_die(const Dwarf_Die* die,
2950 			       Dwarf_Die& canonical_die,
2951 			       size_t where,
2952 			       bool die_as_type) const
2953   {
2954     const die_source source = get_die_source(die);
2955 
2956     offset_offset_map_type &canonical_dies =
2957       die_as_type
2958       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
2959       get_container(source)
2960       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
2961       get_container(source);
2962 
2963     Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2964 
2965     if (Dwarf_Off canonical_die_offset =
2966 	get_canonical_die_offset(canonical_dies,
2967 				 initial_die_offset))
2968       {
2969 	get_die_from_offset(source, canonical_die_offset, &canonical_die);
2970 	return true;
2971       }
2972 
2973     // The map that associates the string representation of 'die'
2974     // with a vector of offsets of potentially equivalent DIEs.
2975     istring_dwarf_offsets_map_type& map =
2976       die_as_type
2977       ? (const_cast<read_context*>(this)->
2978 	 type_die_repr_die_offsets_maps().get_container(*this, die))
2979       : (const_cast<read_context*>(this)->
2980 	 decl_die_repr_die_offsets_maps().get_container(*this, die));
2981 
2982     // The variable repr is the the string representation of 'die'.
2983     //
2984     // Even if die_as_type is true -- which means that 'die' is said
2985     // to be considered as a type -- we always consider a
2986     // DW_TAG_subprogram DIE as a decl here, as far as its string
2987     // representation is concerned.
2988     interned_string name =
2989       (die_as_type)
2990       ? get_die_pretty_type_representation(die, where)
2991       : get_die_pretty_representation(die, where);
2992 
2993     istring_dwarf_offsets_map_type::iterator i = map.find(name);
2994     if (i == map.end())
2995       {
2996 	dwarf_offsets_type offsets;
2997 	offsets.push_back(initial_die_offset);
2998 	map[name] = offsets;
2999 	get_die_from_offset(source, initial_die_offset, &canonical_die);
3000 	set_canonical_die_offset(canonical_dies,
3001 				 initial_die_offset,
3002 				 initial_die_offset);
3003 	return false;
3004       }
3005 
3006     if (odr_is_relevant(die))
3007       {
3008 	// ODR is relevant for this DIE.  In this case, all types with
3009 	// the same name are considered equivalent.  So the array
3010 	// i->second shoud only have on element.  If not, then
3011 	// the DIEs referenced in the array should all compare equal.
3012 	// Otherwise, this is an ODR violation.  In any case, return
3013 	// the first element of the array.
3014 	// ABG_ASSERT(i->second.size() == 1);
3015 	Dwarf_Off die_offset = i->second.front();
3016 	get_die_from_offset(source, die_offset, &canonical_die);
3017 	set_canonical_die_offset(canonical_dies,
3018 				 initial_die_offset,
3019 				 die_offset);
3020 	return true;
3021       }
3022 
3023     // walk i->second without any iterator (using a while loop rather
3024     // than a for loop) because compare_dies might add new content to
3025     // the end of the i->second vector during the walking.
3026     dwarf_offsets_type::size_type n = 0, s = i->second.size();
3027     while (n < s)
3028       {
3029 	Dwarf_Off die_offset = i->second[n];
3030 	get_die_from_offset(source, die_offset, &canonical_die);
3031 	// compare die and canonical_die.
3032 	if (compare_dies(*this, die, &canonical_die,
3033 			 /*update_canonical_dies_on_the_fly=*/true))
3034 	  {
3035 	    set_canonical_die_offset(canonical_dies,
3036 				     initial_die_offset,
3037 				     die_offset);
3038 	    return true;
3039 	  }
3040 	++n;
3041       }
3042 
3043     // We didn't find a canonical DIE for 'die'.  So let's consider
3044     // that it is its own canonical DIE.
3045     get_die_from_offset(source, initial_die_offset, &canonical_die);
3046     i->second.push_back(initial_die_offset);
3047     set_canonical_die_offset(canonical_dies,
3048 			     initial_die_offset,
3049 			     initial_die_offset);
3050 
3051     return false;
3052   }
3053 
3054   /// Get the source of the DIE.
3055   ///
3056   /// The function returns an enumerator value saying if the DIE comes
3057   /// from the .debug_info section of the primary debug info file, the
3058   /// .debug_info section of the alternate debug info file, or the
3059   /// .debug_types section.
3060   ///
3061   /// @param die the DIE to get the source of.
3062   ///
3063   /// @return the source of the DIE if it could be determined,
3064   /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
3065   die_source
get_die_source(const Dwarf_Die * die) const3066   get_die_source(const Dwarf_Die *die) const
3067   {
3068     die_source source = NO_DEBUG_INFO_DIE_SOURCE;
3069     ABG_ASSERT(die);
3070     ABG_ASSERT(get_die_source(*die, source));
3071     return source;
3072   }
3073 
3074   /// Get the source of the DIE.
3075   ///
3076   /// The function returns an enumerator value saying if the DIE comes
3077   /// from the .debug_info section of the primary debug info file, the
3078   /// .debug_info section of the alternate debug info file, or the
3079   /// .debug_types section.
3080   ///
3081   /// @param die the DIE to get the source of.
3082   ///
3083   /// @param source out parameter.  The function sets this parameter
3084   /// to the source of the DIE @p iff it returns true.
3085   ///
3086   /// @return true iff the source of the DIE could be determined and
3087   /// returned.
3088   bool
get_die_source(const Dwarf_Die & die,die_source & source) const3089   get_die_source(const Dwarf_Die &die, die_source &source) const
3090   {
3091     Dwarf_Die cu_die;
3092     Dwarf_Die cu_kind;
3093     uint8_t address_size = 0, offset_size = 0;
3094     if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
3095 		     &cu_die, &address_size,
3096 		     &offset_size))
3097       return false;
3098 
3099     Dwarf_Half version = 0;
3100     Dwarf_Off abbrev_offset = 0;
3101     uint64_t type_signature = 0;
3102     Dwarf_Off type_offset = 0;
3103     if (!dwarf_cu_die(cu_die.cu, &cu_kind,
3104 		      &version, &abbrev_offset,
3105 		      &address_size, &offset_size,
3106 		      &type_signature, &type_offset))
3107       return false;
3108 
3109     int tag = dwarf_tag(&cu_kind);
3110 
3111     if (tag == DW_TAG_compile_unit
3112 	|| tag == DW_TAG_partial_unit)
3113       {
3114 	Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
3115 	if (dwarf() == die_dwarf)
3116 	  source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
3117 	else if (alt_dwarf() == die_dwarf)
3118 	  source = ALT_DEBUG_INFO_DIE_SOURCE;
3119 	else
3120 	  ABG_ASSERT_NOT_REACHED;
3121       }
3122     else if (tag == DW_TAG_type_unit)
3123       source = TYPE_UNIT_DIE_SOURCE;
3124     else
3125       return false;
3126 
3127     return true;
3128   }
3129 
3130   /// Getter for the DIE designated by an offset.
3131   ///
3132   /// @param source the source of the DIE to get.
3133   ///
3134   /// @param offset the offset of the DIE to get.
3135   ///
3136   /// @param die the resulting DIE.  The pointer has to point to an
3137   /// allocated memory region.
3138   void
get_die_from_offset(die_source source,Dwarf_Off offset,Dwarf_Die * die) const3139   get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
3140   {
3141     if (source == TYPE_UNIT_DIE_SOURCE)
3142       ABG_ASSERT(dwarf_offdie_types(dwarf_per_die_source(source), offset, die));
3143     else
3144       ABG_ASSERT(dwarf_offdie(dwarf_per_die_source(source), offset, die));
3145   }
3146 
3147 public:
3148 
3149   /// Add an entry to the relevant die->decl map.
3150   ///
3151   /// @param die the DIE to add the the map.
3152   ///
3153   /// @param decl the decl to consider.
3154   ///
3155   /// @param where_offset where in the DIE stream we logically are.
3156   ///
3157   /// @param do_associate_by_repr if true then this function
3158   /// associates the representation string of @p die with the
3159   /// declaration @p decl, in a corpus-wide manner.  That is, in the
3160   /// entire current corpus, there is going to be just one declaration
3161   /// associated with a DIE of the string representation of @p die.
3162   ///
3163   /// @param do_associate_by_repr_per_tu if true, then this function
3164   /// associates the representation string of @p die with the
3165   /// declaration @p decl in a translation unit wide manner.  That is,
3166   /// in the entire current translation unit, there is going to be
3167   /// just one declaration associated with a DIE of the string
3168   /// representation of @p die.
3169   void
associate_die_to_decl(Dwarf_Die * die,decl_base_sptr decl,size_t where_offset,bool do_associate_by_repr=false)3170   associate_die_to_decl(Dwarf_Die* die,
3171 			decl_base_sptr decl,
3172 			size_t where_offset,
3173 			bool do_associate_by_repr = false)
3174   {
3175     const die_source source = get_die_source(die);
3176 
3177     die_artefact_map_type& m =
3178       decl_die_artefact_maps().get_container(source);
3179 
3180     size_t die_offset;
3181     if (do_associate_by_repr)
3182       {
3183 	Dwarf_Die equiv_die;
3184 	get_or_compute_canonical_die(die, equiv_die, where_offset,
3185 				     /*die_as_type=*/false);
3186 	die_offset = dwarf_dieoffset(&equiv_die);
3187       }
3188     else
3189       die_offset = dwarf_dieoffset(die);
3190 
3191     m[die_offset] = decl;
3192   }
3193 
3194   /// Lookup the decl for a given DIE.
3195   ///
3196   /// The returned decl is either the decl of the DIE that as the
3197   /// exact offset @p die_offset
3198   /// die_offset, or
3199   /// give
3200   ///
3201   /// @param die_offset the offset of the DIE to consider.
3202   ///
3203   /// @param source where the DIE represented by @p die_offset comes
3204   /// from.
3205   ///
3206   /// Note that "alternate debug info sections" is a GNU extension as
3207   /// of DWARF4 and is described at
3208   /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3209   ///
3210   /// @return the resulting decl, or null if no decl is associated to
3211   /// the DIE represented by @p die_offset.
3212   decl_base_sptr
lookup_decl_from_die_offset(Dwarf_Off die_offset,die_source source)3213   lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3214   {
3215     decl_base_sptr result =
3216       is_decl(lookup_artifact_from_die_offset(die_offset, source,
3217 					      /*die_as_type=*/false));
3218 
3219     return result;
3220   }
3221 
3222   /// Get the qualified name of a given DIE.
3223   ///
3224   /// If the name of the DIE was already computed before just return
3225   /// that name from a cache.  Otherwise, build the name, cache it and
3226   /// return it.
3227   ///
3228   /// @param die the DIE to consider.
3229   ///
3230   /// @param where_offset where in the DIE stream we logically are.
3231   ///
3232   /// @return the interned string representing the qualified name of
3233   /// @p die.
3234   interned_string
get_die_qualified_name(Dwarf_Die * die,size_t where_offset)3235   get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3236   {
3237     ABG_ASSERT(die);
3238     die_istring_map_type& map =
3239       die_qualified_name_maps_.get_container(*this, die);
3240 
3241     size_t die_offset = dwarf_dieoffset(die);
3242     die_istring_map_type::const_iterator i = map.find(die_offset);
3243 
3244     if (i == map.end())
3245       {
3246 	read_context& ctxt  = *const_cast<read_context*>(this);
3247 	string qualified_name = die_qualified_name(ctxt, die, where_offset);
3248 	interned_string istr = env()->intern(qualified_name);
3249 	map[die_offset] = istr;
3250 	return istr;
3251       }
3252 
3253     return i->second;
3254   }
3255 
3256   /// Get the qualified name of a given DIE.
3257   ///
3258   /// If the name of the DIE was already computed before just return
3259   /// that name from a cache.  Otherwise, build the name, cache it and
3260   /// return it.
3261   ///
3262   /// @param die the DIE to consider.
3263   ///
3264   /// @param where_offset where in the DIE stream we logically are.
3265   ///
3266   /// @return the interned string representing the qualified name of
3267   /// @p die.
3268   interned_string
get_die_qualified_name(Dwarf_Die * die,size_t where_offset) const3269   get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3270   {
3271     return const_cast<read_context*>(this)->
3272       get_die_qualified_name(die, where_offset);
3273   }
3274 
3275   /// Get the qualified name of a given DIE which is considered to be
3276   /// the DIE for a type.
3277   ///
3278   /// For instance, for a DW_TAG_subprogram DIE, this function
3279   /// computes the name of the function *type* that corresponds to the
3280   /// function.
3281   ///
3282   /// If the name of the DIE was already computed before just return
3283   /// that name from a cache.  Otherwise, build the name, cache it and
3284   /// return it.
3285   ///
3286   /// @param die the DIE to consider.
3287   ///
3288   /// @param where_offset where in the DIE stream we logically are.
3289   ///
3290   /// @return the interned string representing the qualified name of
3291   /// @p die.
3292   interned_string
get_die_qualified_type_name(const Dwarf_Die * die,size_t where_offset) const3293   get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3294   {
3295     ABG_ASSERT(die);
3296 
3297     // The name of the translation unit die is "".
3298     if (die == cur_tu_die())
3299       return env()->intern("");
3300 
3301     die_istring_map_type& map =
3302       die_qualified_name_maps_.get_container(*const_cast<read_context*>(this),
3303 					     die);
3304 
3305     size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3306     die_istring_map_type::const_iterator i =
3307       map.find(die_offset);
3308 
3309     if (i == map.end())
3310       {
3311 	read_context& ctxt  = *const_cast<read_context*>(this);
3312 	string qualified_name;
3313 	int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3314 	if ((tag == DW_TAG_structure_type
3315 	     || tag == DW_TAG_class_type
3316 	     || tag == DW_TAG_union_type)
3317 	    && die_is_anonymous(die))
3318 	  {
3319 	    location l = die_location(*this, die);
3320 	    qualified_name = l ? l.expand() : "noloc";
3321 	    qualified_name = "unnamed-at-" + qualified_name;
3322 	  }
3323 	else
3324 	  qualified_name =
3325 	    die_qualified_type_name(ctxt, die, where_offset);
3326 
3327 	interned_string istr = env()->intern(qualified_name);
3328 	map[die_offset] = istr;
3329 	return istr;
3330       }
3331 
3332     return i->second;
3333   }
3334 
3335   /// Get the pretty representation of a DIE that represents a type.
3336   ///
3337   /// For instance, for the DW_TAG_subprogram, this function computes
3338   /// the pretty representation of the type of the function, not the
3339   /// pretty representation of the function declaration.
3340   ///
3341   /// Once the pretty representation is computed, it's stored in a
3342   /// cache.  Subsequent invocations of this function on the same DIE
3343   /// will yield the cached name.
3344   ///
3345   /// @param die the DIE to consider.
3346   ///
3347   /// @param where_offset where in the DIE stream we logically are.
3348   ///
3349   /// @return the interned_string that represents the pretty
3350   /// representation.
3351   interned_string
get_die_pretty_type_representation(const Dwarf_Die * die,size_t where_offset) const3352   get_die_pretty_type_representation(const Dwarf_Die *die,
3353 				     size_t where_offset) const
3354   {
3355     ABG_ASSERT(die);
3356     die_istring_map_type& map =
3357       die_pretty_type_repr_maps_.get_container(*const_cast<read_context*>(this),
3358 					       die);
3359 
3360     size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3361     die_istring_map_type::const_iterator i = map.find(die_offset);
3362 
3363     if (i == map.end())
3364       {
3365 	read_context& ctxt = *const_cast<read_context*>(this);
3366 	string pretty_representation =
3367 	  die_pretty_print_type(ctxt, die, where_offset);
3368 	interned_string istr = env()->intern(pretty_representation);
3369 	map[die_offset] = istr;
3370 	return istr;
3371       }
3372 
3373     return i->second;
3374   }
3375 
3376   /// Get the pretty representation of a DIE.
3377   ///
3378   /// Once the pretty representation is computed, it's stored in a
3379   /// cache.  Subsequent invocations of this function on the same DIE
3380   /// will yield the cached name.
3381   ///
3382   /// @param die the DIE to consider.
3383   ///
3384   /// @param where_offset where in the DIE stream we logically are.
3385   ///
3386   /// @return the interned_string that represents the pretty
3387   /// representation.
3388   interned_string
get_die_pretty_representation(const Dwarf_Die * die,size_t where_offset) const3389   get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3390   {
3391     ABG_ASSERT(die);
3392 
3393     die_istring_map_type& map =
3394       die_pretty_repr_maps_.get_container(*const_cast<read_context*>(this),
3395 					  die);
3396 
3397     size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3398     die_istring_map_type::const_iterator i = map.find(die_offset);
3399 
3400     if (i == map.end())
3401       {
3402 	read_context& ctxt = *const_cast<read_context*>(this);
3403 	string pretty_representation =
3404 	  die_pretty_print(ctxt, die, where_offset);
3405 	interned_string istr = env()->intern(pretty_representation);
3406 	map[die_offset] = istr;
3407 	return istr;
3408       }
3409 
3410     return i->second;
3411   }
3412 
3413   /// Lookup the artifact that was built to represent a type that has
3414   /// the same pretty representation as the type denoted by a given
3415   /// DIE.
3416   ///
3417   /// Note that the DIE must have previously been associated with the
3418   /// artifact using the functions associate_die_to_decl or
3419   /// associate_die_to_type.
3420   ///
3421   /// Also, note that the scope of the lookup is the current ABI
3422   /// corpus.
3423   ///
3424   /// @param die the DIE to consider.
3425   ///
3426   /// @param where_offset where in the DIE stream we logically are.
3427   ///
3428   /// @return the type artifact found.
3429   type_or_decl_base_sptr
lookup_type_artifact_from_die(Dwarf_Die * die) const3430   lookup_type_artifact_from_die(Dwarf_Die *die) const
3431   {
3432     type_or_decl_base_sptr artifact =
3433       lookup_artifact_from_die(die, /*type_as_die=*/true);
3434     if (function_decl_sptr fn = is_function_decl(artifact))
3435       return fn->get_type();
3436     return artifact;
3437   }
3438 
3439   /// Lookup the artifact that was built to represent a type or a
3440   /// declaration that has the same pretty representation as the type
3441   /// denoted by a given DIE.
3442   ///
3443   /// Note that the DIE must have previously been associated with the
3444   /// artifact using the functions associate_die_to_decl or
3445   /// associate_die_to_type.
3446   ///
3447   /// Also, note that the scope of the lookup is the current ABI
3448   /// corpus.
3449   ///
3450   /// @param die the DIE to consider.
3451   ///
3452   /// @param where_offset where in the DIE stream we logically are.
3453   ///
3454   /// @param die_as_type if true, it means the DIE is to be considered
3455   /// as a type.
3456   ///
3457   /// @return the artifact found.
3458   type_or_decl_base_sptr
lookup_artifact_from_die(const Dwarf_Die * die,bool die_as_type=false) const3459   lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3460   {
3461     Dwarf_Die equiv_die;
3462     if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3463       return type_or_decl_base_sptr();
3464 
3465     const die_artefact_map_type& m =
3466       die_as_type
3467       ? type_die_artefact_maps().get_container(*this, &equiv_die)
3468       : decl_die_artefact_maps().get_container(*this, &equiv_die);
3469 
3470     size_t die_offset = dwarf_dieoffset(&equiv_die);
3471     die_artefact_map_type::const_iterator i = m.find(die_offset);
3472 
3473     if (i == m.end())
3474       return type_or_decl_base_sptr();
3475     return i->second;
3476   }
3477 
3478   /// Lookup the artifact that was built to represent a type or a
3479   /// declaration that has the same pretty representation as the type
3480   /// denoted by the offset of a given DIE.
3481   ///
3482   /// Note that the DIE must have previously been associated with the
3483   /// artifact using either associate_die_to_decl or
3484   /// associate_die_to_type.
3485   ///
3486   /// Also, note that the scope of the lookup is the current ABI
3487   /// corpus.
3488   ///
3489   /// @param die the DIE to consider.
3490   ///
3491   /// @param where_offset where in the DIE stream we logically are.
3492   ///
3493   /// @param die_as_type if true, it means the DIE is to be considered
3494   /// as a type.
3495   ///
3496   /// @return the artifact found.
3497   type_or_decl_base_sptr
lookup_artifact_from_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type=false) const3498   lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3499 				  die_source source,
3500 				  bool die_as_type = false) const
3501   {
3502     const die_artefact_map_type& m =
3503       die_as_type
3504       ? type_die_artefact_maps().get_container(source)
3505       : decl_die_artefact_maps().get_container(source);
3506 
3507     die_artefact_map_type::const_iterator i = m.find(die_offset);
3508     if (i == m.end())
3509       return type_or_decl_base_sptr();
3510     return i->second;
3511   }
3512 
3513   /// Get the language used to generate a given DIE.
3514   ///
3515   /// @param die the DIE to consider.
3516   ///
3517   /// @param lang the resulting language.
3518   ///
3519   /// @return true iff the language of the DIE was found.
3520   bool
get_die_language(const Dwarf_Die * die,translation_unit::language & lang) const3521   get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
3522   {
3523     Dwarf_Die cu_die;
3524     ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
3525 
3526     uint64_t l = 0;
3527     if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
3528       return false;
3529 
3530     lang = dwarf_language_to_tu_language(l);
3531     return true;
3532   }
3533 
3534   /// Test if a given DIE originates from a program written in the C
3535   /// language.
3536   ///
3537   /// @param die the DIE to consider.
3538   ///
3539   /// @return true iff @p die originates from a program in the C
3540   /// language.
3541   bool
die_is_in_c(const Dwarf_Die * die) const3542   die_is_in_c(const Dwarf_Die *die) const
3543   {
3544     translation_unit::language l = translation_unit::LANG_UNKNOWN;
3545     if (!get_die_language(die, l))
3546       return false;
3547     return is_c_language(l);
3548   }
3549 
3550   /// Test if a given DIE originates from a program written in the C++
3551   /// language.
3552   ///
3553   /// @param die the DIE to consider.
3554   ///
3555   /// @return true iff @p die originates from a program in the C++
3556   /// language.
3557   bool
die_is_in_cplus_plus(const Dwarf_Die * die) const3558   die_is_in_cplus_plus(const Dwarf_Die *die) const
3559   {
3560     translation_unit::language l = translation_unit::LANG_UNKNOWN;
3561     if (!get_die_language(die, l))
3562       return false;
3563     return is_cplus_plus_language(l);
3564   }
3565 
3566   /// Test if a given DIE originates from a program written either in
3567   /// C or C++.
3568   ///
3569   /// @param die the DIE to consider.
3570   ///
3571   /// @return true iff @p die originates from a program written either in
3572   /// C or C++.
3573   bool
die_is_in_c_or_cplusplus(const Dwarf_Die * die) const3574   die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
3575   {
3576     translation_unit::language l = translation_unit::LANG_UNKNOWN;
3577     if (!get_die_language(die, l))
3578       return false;
3579     return (is_cplus_plus_language(l) || is_c_language(l));
3580   }
3581 
3582   /// Check if we can assume the One Definition Rule[1] to be relevant
3583   /// for the current translation unit.
3584   ///
3585   /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3586   ///
3587   /// At the moment this returns true if the current translation unit
3588   /// is in C++ language.  In that case, it's relevant to assume that
3589   /// we use optimizations based on the ODR.
3590   bool
odr_is_relevant() const3591   odr_is_relevant() const
3592   {return odr_is_relevant(cur_transl_unit()->get_language());}
3593 
3594   /// Check if we can assume the One Definition Rule[1] to be relevant
3595   /// for a given language.
3596   ///
3597   /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3598   ///
3599   /// At the moment this returns true if the language considered
3600   /// is C++, Java or Ada.
3601   bool
odr_is_relevant(translation_unit::language l) const3602   odr_is_relevant(translation_unit::language l) const
3603   {
3604     return (is_cplus_plus_language(l)
3605 	    || is_java_language(l)
3606 	    || is_ada_language(l));
3607   }
3608 
3609   /// Check if we can assume the One Definition Rule to be relevant
3610   /// for a given DIE.
3611   ///
3612   /// @param die the DIE to consider.
3613   ///
3614   /// @return true if the ODR is relevant for @p die.
3615   bool
odr_is_relevant(Dwarf_Off die_offset,die_source source) const3616   odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3617   {
3618     Dwarf_Die die;
3619     ABG_ASSERT(dwarf_offdie(dwarf_per_die_source(source), die_offset, &die));
3620     return odr_is_relevant(&die);
3621   }
3622 
3623   /// Check if we can assume the One Definition Rule to be relevant
3624   /// for a given DIE.
3625   ///
3626   /// @param die the DIE to consider.
3627   ///
3628   /// @return true if the ODR is relevant for @p die.
3629   bool
odr_is_relevant(const Dwarf_Die * die) const3630   odr_is_relevant(const Dwarf_Die *die) const
3631   {
3632     translation_unit::language lang;
3633     if (!get_die_language(die, lang))
3634       return odr_is_relevant();
3635 
3636     return odr_is_relevant(lang);
3637   }
3638 
3639   /// Getter for the maps set that associates a decl DIE offset to an
3640   /// artifact.
3641   ///
3642   /// @return the maps set that associates a decl DIE offset to an
3643   /// artifact.
3644   die_source_dependant_container_set<die_artefact_map_type>&
decl_die_artefact_maps()3645   decl_die_artefact_maps()
3646   {return decl_die_artefact_maps_;}
3647 
3648   /// Getter for the maps set that associates a decl DIE offset to an
3649   /// artifact.
3650   ///
3651   /// @return the maps set that associates a decl DIE offset to an
3652   /// artifact.
3653   const die_source_dependant_container_set<die_artefact_map_type>&
decl_die_artefact_maps() const3654   decl_die_artefact_maps() const
3655   {return decl_die_artefact_maps_;}
3656 
3657   /// Getter for the maps set that associates a type DIE offset to an
3658   /// artifact.
3659   ///
3660   /// @return the maps set that associates a type DIE offset to an
3661   /// artifact.
3662   die_source_dependant_container_set<die_artefact_map_type>&
type_die_artefact_maps()3663   type_die_artefact_maps()
3664   {return type_die_artefact_maps_;}
3665 
3666   /// Getter for the maps set that associates a type DIE offset to an
3667   /// artifact.
3668   ///
3669   /// @return the maps set that associates a type DIE offset to an
3670   /// artifact.
3671   const die_source_dependant_container_set<die_artefact_map_type>&
type_die_artefact_maps() const3672   type_die_artefact_maps() const
3673   {return type_die_artefact_maps_;}
3674 
3675   /// Getter of the maps that associates function type representations
3676   /// to function types, inside a translation unit.
3677   ///
3678   /// @return the maps that associates function type representations
3679   /// to function types, inside a translation unit.
3680   istring_fn_type_map_type&
per_tu_repr_to_fn_type_maps()3681   per_tu_repr_to_fn_type_maps()
3682   {return per_tu_repr_to_fn_type_maps_;}
3683 
3684   /// Getter of the maps that associates function type representations
3685   /// to function types, inside a translation unit.
3686   ///
3687   /// @return the maps that associates function type representations
3688   /// to function types, inside a translation unit.
3689   const istring_fn_type_map_type&
per_tu_repr_to_fn_type_maps() const3690   per_tu_repr_to_fn_type_maps() const
3691   {return per_tu_repr_to_fn_type_maps_;}
3692 
3693   /// Associate the representation of a function type DIE to a given
3694   /// function type, inside the current translation unit.
3695   ///
3696   /// @param die the DIE to associate to the function type, using its
3697   /// representation.
3698   ///
3699   /// @param fn_type the function type to associate to @p die.
3700   void
associate_die_repr_to_fn_type_per_tu(const Dwarf_Die * die,const function_type_sptr & fn_type)3701   associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3702 				       const function_type_sptr &fn_type)
3703   {
3704     if (!die_is_function_type(die))
3705       return;
3706 
3707     interned_string repr =
3708       get_die_pretty_type_representation(die, /*where=*/0);
3709     ABG_ASSERT(!repr.empty());
3710 
3711     per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3712   }
3713 
3714   /// Lookup the function type associated to a given function type
3715   /// DIE, in the current translation unit.
3716   ///
3717   /// @param die the DIE of function type to consider.
3718   ///
3719   /// @return the @ref function_type_sptr associated to @p die, or nil
3720   /// of no function_type is associated to @p die.
3721   function_type_sptr
lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die * die)3722   lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3723   {
3724     if (!die_is_function_type(die))
3725       return function_type_sptr();
3726 
3727     interned_string repr =
3728       get_die_pretty_representation(die, /*where=*/0);
3729     ABG_ASSERT(!repr.empty());
3730 
3731     istring_fn_type_map_type::const_iterator i =
3732       per_tu_repr_to_fn_type_maps().find(repr);
3733 
3734     if (i == per_tu_repr_to_fn_type_maps().end())
3735       return function_type_sptr();
3736 
3737     return i->second;
3738   }
3739 
3740   /// Set the canonical DIE offset of a given DIE.
3741   ///
3742   /// @param canonical_dies the vector that holds canonical DIEs.
3743   ///
3744   /// @param die_offset the offset of the DIE to set the canonical DIE
3745   /// for.
3746   ///
3747   /// @param canonical_die_offset the canonical DIE offset to
3748   /// associate to @p die_offset.
3749   void
set_canonical_die_offset(offset_offset_map_type & canonical_dies,Dwarf_Off die_offset,Dwarf_Off canonical_die_offset) const3750   set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3751 			   Dwarf_Off die_offset,
3752 			   Dwarf_Off canonical_die_offset) const
3753   {
3754     canonical_dies[die_offset] = canonical_die_offset;}
3755 
3756   /// Set the canonical DIE offset of a given DIE.
3757   ///
3758   ///
3759   /// @param die_offset the offset of the DIE to set the canonical DIE
3760   /// for.
3761   ///
3762   /// @param source the source of the DIE denoted by @p die_offset.
3763   ///
3764   /// @param canonical_die_offset the canonical DIE offset to
3765   /// associate to @p die_offset.
3766   ///
3767   /// @param die_as_type if true, it means that @p die_offset has to
3768   /// be considered as a type.
3769   void
set_canonical_die_offset(Dwarf_Off die_offset,die_source source,Dwarf_Off canonical_die_offset,bool die_as_type) const3770   set_canonical_die_offset(Dwarf_Off die_offset,
3771 			   die_source source,
3772 			   Dwarf_Off canonical_die_offset,
3773 			   bool die_as_type) const
3774   {
3775     offset_offset_map_type &canonical_dies =
3776       die_as_type
3777       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
3778       get_container(source)
3779       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
3780       get_container(source);
3781 
3782     set_canonical_die_offset(canonical_dies,
3783 			     die_offset,
3784 			     canonical_die_offset);
3785   }
3786 
3787   /// Set the canonical DIE offset of a given DIE.
3788   ///
3789   ///
3790   /// @param die the DIE to set the canonical DIE for.
3791   ///
3792   /// @param canonical_die_offset the canonical DIE offset to
3793   /// associate to @p die_offset.
3794   ///
3795   /// @param die_as_type if true, it means that @p die has to be
3796   /// considered as a type.
3797   void
set_canonical_die_offset(const Dwarf_Die * die,Dwarf_Off canonical_die_offset,bool die_as_type) const3798   set_canonical_die_offset(const Dwarf_Die *die,
3799 			   Dwarf_Off canonical_die_offset,
3800 			   bool die_as_type) const
3801   {
3802     const die_source source = get_die_source(die);
3803 
3804     Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3805 
3806     set_canonical_die_offset(die_offset, source,
3807 			     canonical_die_offset,
3808 			     die_as_type);
3809   }
3810 
3811   /// Get the canonical DIE offset of a given DIE.
3812   ///
3813   /// @param canonical_dies the vector that contains canonical DIES.
3814   ///
3815   /// @param die_offset the offset of the DIE to consider.
3816   ///
3817   /// @return the canonical of the DIE denoted by @p die_offset, or
3818   /// zero if no canonical DIE was found.
3819   Dwarf_Off
get_canonical_die_offset(offset_offset_map_type & canonical_dies,Dwarf_Off die_offset) const3820   get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3821 			   Dwarf_Off die_offset) const
3822   {
3823     offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3824     if (it == canonical_dies.end())
3825       return 0;
3826     return it->second;
3827   }
3828 
3829   /// Get the canonical DIE offset of a given DIE.
3830   ///
3831   /// @param die_offset the offset of the DIE to consider.
3832   ///
3833   /// @param source the source of the DIE denoted by @p die_offset.
3834   ///
3835   /// @param die_as_type if true, it means that @p is to be considered
3836   /// as a type DIE.
3837   ///
3838   /// @return the canonical of the DIE denoted by @p die_offset, or
3839   /// zero if no canonical DIE was found.
3840   Dwarf_Off
get_canonical_die_offset(Dwarf_Off die_offset,die_source source,bool die_as_type) const3841   get_canonical_die_offset(Dwarf_Off die_offset,
3842 			   die_source source,
3843 			   bool die_as_type) const
3844   {
3845     offset_offset_map_type &canonical_dies =
3846       die_as_type
3847       ? const_cast<read_context*>(this)->canonical_type_die_offsets_.
3848       get_container(source)
3849       : const_cast<read_context*>(this)->canonical_decl_die_offsets_.
3850       get_container(source);
3851 
3852     return get_canonical_die_offset(canonical_dies, die_offset);
3853   }
3854 
3855   /// Associate a DIE (representing a type) to the type that it
3856   /// represents.
3857   ///
3858   /// @param die the DIE to consider.
3859   ///
3860   /// @param type the type to associate the DIE to.
3861   ///
3862   /// @param where_offset where in the DIE stream we logically are.
3863   void
associate_die_to_type(const Dwarf_Die * die,type_base_sptr type,size_t where)3864   associate_die_to_type(const Dwarf_Die	*die,
3865 			type_base_sptr	type,
3866 			size_t		where)
3867   {
3868     if (!type)
3869       return;
3870 
3871     Dwarf_Die equiv_die;
3872     get_or_compute_canonical_die(die, equiv_die, where, /*die_as_type=*/true);
3873 
3874     die_artefact_map_type& m =
3875       type_die_artefact_maps().get_container(*this, &equiv_die);
3876 
3877     size_t die_offset = dwarf_dieoffset(&equiv_die);
3878     m[die_offset] = type;
3879   }
3880 
3881   /// Lookup the type associated to a given DIE.
3882   ///
3883   /// Note that the DIE must have been associated to type by a
3884   /// previous invocation of the function
3885   /// read_context::associate_die_to_type().
3886   ///
3887   /// @param die the DIE to consider.
3888   ///
3889   /// @return the type associated to the DIE or NULL if no type is
3890   /// associated to the DIE.
3891   type_base_sptr
lookup_type_from_die(const Dwarf_Die * die) const3892   lookup_type_from_die(const Dwarf_Die* die) const
3893   {
3894     type_or_decl_base_sptr artifact =
3895       lookup_artifact_from_die(die, /*die_as_type=*/true);
3896     if (function_decl_sptr fn = is_function_decl(artifact))
3897       return fn->get_type();
3898     return is_type(artifact);
3899   }
3900 
3901   /// Lookup the type associated to a DIE at a given offset, from a
3902   /// given source.
3903   ///
3904   /// Note that the DIE must have been associated to type by a
3905   /// previous invocation of the function
3906   /// read_context::associate_die_to_type().
3907   ///
3908   /// @param die_offset the offset of the DIE to consider.
3909   ///
3910   /// @param source the source of the DIE to consider.
3911   ///
3912   /// @return the type associated to the DIE or NULL if no type is
3913   /// associated to the DIE.
3914   type_base_sptr
lookup_type_from_die_offset(size_t die_offset,die_source source) const3915   lookup_type_from_die_offset(size_t die_offset, die_source source) const
3916   {
3917     type_base_sptr result;
3918     const die_artefact_map_type& m =
3919       type_die_artefact_maps().get_container(source);
3920     die_artefact_map_type::const_iterator i = m.find(die_offset);
3921     if (i != m.end())
3922       {
3923 	if (function_decl_sptr fn = is_function_decl(i->second))
3924 	  return fn->get_type();
3925 	result = is_type(i->second);
3926       }
3927 
3928     if (!result)
3929       {
3930 	// Maybe we are looking for a class type being constructed?
3931 	const die_class_or_union_map_type& m = die_wip_classes_map(source);
3932 	die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3933 
3934 	if (i != m.end())
3935 	  result = i->second;
3936       }
3937 
3938     if (!result)
3939       {
3940 	// Maybe we are looking for a function type being constructed?
3941 	const die_function_type_map_type& m =
3942 	  die_wip_function_types_map(source);
3943 	die_function_type_map_type::const_iterator i = m.find(die_offset);
3944 
3945 	if (i != m.end())
3946 	  result = i->second;
3947       }
3948 
3949     return result;
3950   }
3951 
3952   /// Getter of a map that associates a die that represents a
3953   /// class/struct with the declaration of the class, while the class
3954   /// is being constructed.
3955   ///
3956   /// @param source where the DIE is from.
3957   ///
3958   /// @return the map that associates a DIE to the class that is being
3959   /// built.
3960   const die_class_or_union_map_type&
die_wip_classes_map(die_source source) const3961   die_wip_classes_map(die_source source) const
3962   {return const_cast<read_context*>(this)->die_wip_classes_map(source);}
3963 
3964   /// Getter of a map that associates a die that represents a
3965   /// class/struct with the declaration of the class, while the class
3966   /// is being constructed.
3967   ///
3968   /// @param source where the DIE comes from.
3969   ///
3970   /// @return the map that associates a DIE to the class that is being
3971   /// built.
3972   die_class_or_union_map_type&
die_wip_classes_map(die_source source)3973   die_wip_classes_map(die_source source)
3974   {
3975     switch (source)
3976       {
3977       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3978 	break;
3979       case ALT_DEBUG_INFO_DIE_SOURCE:
3980 	return alternate_die_wip_classes_map_;
3981       case TYPE_UNIT_DIE_SOURCE:
3982 	return type_unit_die_wip_classes_map_;
3983       case NO_DEBUG_INFO_DIE_SOURCE:
3984       case NUMBER_OF_DIE_SOURCES:
3985 	ABG_ASSERT_NOT_REACHED;
3986       }
3987     return die_wip_classes_map_;
3988   }
3989 
3990   /// Getter for a map that associates a die (that represents a
3991   /// function type) whith a function type, while the function type is
3992   /// being constructed (WIP == work in progress).
3993   ///
3994   /// @param source where the DIE comes from.n
3995   ///
3996   /// @return the map of wip function types.
3997   const die_function_type_map_type&
die_wip_function_types_map(die_source source) const3998   die_wip_function_types_map(die_source source) const
3999   {return const_cast<read_context*>(this)->die_wip_function_types_map(source);}
4000 
4001   /// Getter for a map that associates a die (that represents a
4002   /// function type) whith a function type, while the function type is
4003   /// being constructed (WIP == work in progress).
4004   ///
4005   /// @param source where DIEs of the map come from.
4006   ///
4007   /// @return the map of wip function types.
4008   die_function_type_map_type&
die_wip_function_types_map(die_source source)4009   die_wip_function_types_map(die_source source)
4010   {
4011     switch (source)
4012       {
4013       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4014 	break;
4015       case ALT_DEBUG_INFO_DIE_SOURCE:
4016 	return alternate_die_wip_function_types_map_;
4017       case TYPE_UNIT_DIE_SOURCE:
4018 	return type_unit_die_wip_function_types_map_;
4019       case NO_DEBUG_INFO_DIE_SOURCE:
4020       case NUMBER_OF_DIE_SOURCES:
4021 	ABG_ASSERT_NOT_REACHED;
4022       }
4023     return die_wip_function_types_map_;
4024   }
4025 
4026   /// Getter for a map that associates a die with a function decl
4027   /// which has a linkage name but no elf symbol yet.
4028   ///
4029   /// This is to fixup function decls with linkage names, but with no
4030   /// link to their underlying elf symbol.  There are some DIEs like
4031   /// that in DWARF sometimes, especially when the compiler optimizes
4032   /// stuff aggressively.
4033   die_function_decl_map_type&
die_function_decl_with_no_symbol_map()4034   die_function_decl_with_no_symbol_map()
4035   {return die_function_with_no_symbol_map_;}
4036 
4037   /// Return true iff a given offset is for the DIE of a class that is
4038   /// being built, but that is not fully built yet.  WIP == "work in
4039   /// progress".
4040   ///
4041   /// @param offset the DIE offset to consider.
4042   ///
4043   /// @param source where the DIE of the map come from.
4044   ///
4045   /// @return true iff @p offset is the offset of the DIE of a class
4046   /// that is being currently built.
4047   bool
is_wip_class_die_offset(Dwarf_Off offset,die_source source) const4048   is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
4049   {
4050     die_class_or_union_map_type::const_iterator i =
4051       die_wip_classes_map(source).find(offset);
4052     return (i != die_wip_classes_map(source).end());
4053   }
4054 
4055   /// Return true iff a given offset is for the DIE of a function type
4056   /// that is being built at the moment, but is not fully built yet.
4057   /// WIP == work in progress.
4058   ///
4059   /// @param offset DIE offset to consider.
4060   ///
4061   /// @param source where the DIE comes from.
4062   ///
4063   /// @return true iff @p offset is the offset of the DIE of a
4064   /// function type that is being currently built.
4065   bool
is_wip_function_type_die_offset(Dwarf_Off offset,die_source source) const4066   is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
4067   {
4068     die_function_type_map_type::const_iterator i =
4069       die_wip_function_types_map(source).find(offset);
4070     return (i != die_wip_function_types_map(source).end());
4071   }
4072 
4073   /// Getter for the map of declaration-only classes that are to be
4074   /// resolved to their definition classes by the end of the corpus
4075   /// loading.
4076   ///
4077   /// @return a map of string -> vector of classes where the key is
4078   /// the fully qualified name of the class and the value is the
4079   /// vector of declaration-only class.
4080   const string_classes_map&
declaration_only_classes() const4081   declaration_only_classes() const
4082   {return decl_only_classes_map_;}
4083 
4084   /// Getter for the map of declaration-only classes that are to be
4085   /// resolved to their definition classes by the end of the corpus
4086   /// loading.
4087   ///
4088   /// @return a map of string -> vector of classes where the key is
4089   /// the fully qualified name of the class and the value is the
4090   /// vector of declaration-only class.
4091   string_classes_map&
declaration_only_classes()4092   declaration_only_classes()
4093   {return decl_only_classes_map_;}
4094 
4095   /// If a given class is a declaration-only class then stash it on
4096   /// the side so that at the end of the corpus reading we can resolve
4097   /// it to its definition.
4098   ///
4099   /// @param klass the class to consider.
4100   void
maybe_schedule_declaration_only_class_for_resolution(class_decl_sptr & klass)4101   maybe_schedule_declaration_only_class_for_resolution(class_decl_sptr& klass)
4102   {
4103     if (klass->get_is_declaration_only()
4104 	&& klass->get_definition_of_declaration() == 0)
4105       {
4106 	string qn = klass->get_qualified_name();
4107 	string_classes_map::iterator record =
4108 	  declaration_only_classes().find(qn);
4109 	if (record == declaration_only_classes().end())
4110 	  declaration_only_classes()[qn].push_back(klass);
4111 	else
4112 	  record->second.push_back(klass);
4113       }
4114   }
4115 
4116   /// Test if a given declaration-only class has been scheduled for
4117   /// resolution to a defined class.
4118   ///
4119   /// @param klass the class to consider for the test.
4120   ///
4121   /// @return true iff @p klass is a declaration-only class and if
4122   /// it's been scheduled for resolution to a defined class.
4123   bool
is_decl_only_class_scheduled_for_resolution(class_decl_sptr & klass)4124   is_decl_only_class_scheduled_for_resolution(class_decl_sptr& klass)
4125   {
4126     if (klass->get_is_declaration_only())
4127       return (declaration_only_classes().find(klass->get_qualified_name())
4128 	      != declaration_only_classes().end());
4129 
4130     return false;
4131   }
4132 
4133   /// Compare two ABI artifacts in a context which canonicalization
4134   /// has not be done yet.
4135   ///
4136   /// @param l the left-hand-side operand of the comparison
4137   ///
4138   /// @param r the right-hand-side operand of the comparison.
4139   ///
4140   /// @return true if @p l equals @p r.
4141   bool
compare_before_canonicalisation(const type_or_decl_base_sptr & l,const type_or_decl_base_sptr & r)4142   compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4143 				  const type_or_decl_base_sptr &r)
4144   {
4145     if (!l || !r)
4146       return !!l == !!r;
4147 
4148     const environment* e = l->get_environment();
4149     ABG_ASSERT(!e->canonicalization_is_done());
4150 
4151     bool s0 = e->decl_only_class_equals_definition();
4152     bool s1 = e->use_enum_binary_only_equality();
4153     e->decl_only_class_equals_definition(true);
4154     e->use_enum_binary_only_equality(true);
4155     bool equal = l == r;
4156     e->decl_only_class_equals_definition(s0);
4157     e->use_enum_binary_only_equality(s1);
4158     return equal;
4159   }
4160 
4161   /// Walk the declaration-only classes that have been found during
4162   /// the building of the corpus and resolve them to their definitions.
4163   void
resolve_declaration_only_classes()4164   resolve_declaration_only_classes()
4165   {
4166     vector<string> resolved_classes;
4167 
4168     for (string_classes_map::iterator i =
4169 	   declaration_only_classes().begin();
4170 	 i != declaration_only_classes().end();
4171 	 ++i)
4172       {
4173 	bool to_resolve = false;
4174 	for (classes_type::iterator j = i->second.begin();
4175 	     j != i->second.end();
4176 	     ++j)
4177 	  if ((*j)->get_is_declaration_only()
4178 	      && ((*j)->get_definition_of_declaration() == 0))
4179 	    to_resolve = true;
4180 
4181 	if (!to_resolve)
4182 	  {
4183 	    resolved_classes.push_back(i->first);
4184 	    continue;
4185 	  }
4186 
4187 	// Now, for each decl-only class that have the current name
4188 	// 'i->first', let's try to poke at the fully defined class
4189 	// that is defined in the same translation unit as the
4190 	// declaration.
4191 	//
4192 	// If we find one class (defined in the TU of the declaration)
4193 	// that defines the declaration, then the declaration can be
4194 	// resolved to that class.
4195 	//
4196 	// If no defining class is found in the TU of the declaration,
4197 	// then there are possibly three cases to consider:
4198 	//
4199 	//   1/ There is exactly one class that defines the
4200 	//   declaration and that class is defined in another TU.  In
4201 	//   this case, the declaration is resolved to that
4202 	//   definition.
4203 	//
4204 	//   2/ There are more than one class that define that
4205 	//   declaration and none of them is defined in the TU of the
4206 	//   declaration.  If those classes are all different, then
4207 	//   the declaration is left unresolved.
4208 	//
4209 	//   3/ No class defines the declaration.  In this case, the
4210 	//   declaration is left unresoved.
4211 
4212 	// So get the classes that might define the current
4213 	// declarations which name is i->first.
4214 	const type_base_wptrs_type *classes =
4215 	  lookup_class_types(i->first, *current_corpus());
4216 	if (!classes)
4217 	  continue;
4218 
4219 	// This is a map that associates the translation unit path to
4220 	// the class (that potentially defines the declarations that
4221 	// we consider) that are defined in that translation unit.  It
4222 	// should stay ordered by using the TU path as key to ensure
4223 	// stability of the order of classe definitions in ABIXML
4224 	// output.
4225 	map<string, class_decl_sptr> per_tu_class_map;
4226 	for (type_base_wptrs_type::const_iterator c = classes->begin();
4227 	     c != classes->end();
4228 	     ++c)
4229 	  {
4230 	    class_decl_sptr klass = is_class_type(type_base_sptr(*c));
4231 	    ABG_ASSERT(klass);
4232 
4233 	    klass = is_class_type(look_through_decl_only_class(klass));
4234 	    if (klass->get_is_declaration_only())
4235 	      continue;
4236 
4237 	    string tu_path = klass->get_translation_unit()->get_absolute_path();
4238 	    if (tu_path.empty())
4239 	      continue;
4240 
4241 	    // Build a map that associates the translation unit path
4242 	    // to the class (that potentially defines the declarations
4243 	    // that we consider) that are defined in that translation unit.
4244 	    per_tu_class_map[tu_path] = klass;
4245 	  }
4246 
4247 	if (!per_tu_class_map.empty())
4248 	  {
4249 	    // Walk the declarations to resolve and resolve them
4250 	    // either to the definitions that are in the same TU as
4251 	    // the declaration, or to the definition found elsewhere,
4252 	    // if there is only one such definition.
4253 	    for (classes_type::iterator j = i->second.begin();
4254 		 j != i->second.end();
4255 		 ++j)
4256 	      {
4257 		if ((*j)->get_is_declaration_only()
4258 		    && ((*j)->get_definition_of_declaration() == 0))
4259 		  {
4260 		    string tu_path =
4261 		      (*j)->get_translation_unit()->get_absolute_path();
4262 		    map<string, class_decl_sptr>::const_iterator e =
4263 		      per_tu_class_map.find(tu_path);
4264 		    if (e != per_tu_class_map.end())
4265 		      (*j)->set_definition_of_declaration(e->second);
4266 		    else if (per_tu_class_map.size() == 1)
4267 		      (*j)->set_definition_of_declaration
4268 			(per_tu_class_map.begin()->second);
4269 		    else if (per_tu_class_map.size() > 1)
4270 		      {
4271 			// We are in case where there are more than
4272 			// one definition for the declaration.  Let's
4273 			// see if they are all equal.  If they are,
4274 			// then the declaration resolves to the
4275 			// definition.  Otherwise, we are in the case
4276 			// 3/ described above.
4277 			map<string,
4278 			    class_decl_sptr>::const_iterator it;
4279 			class_decl_sptr first_class =
4280 			  per_tu_class_map.begin()->second;
4281 			bool all_class_definitions_are_equal = true;
4282 			for (it = per_tu_class_map.begin();
4283 			     it != per_tu_class_map.end();
4284 			     ++it)
4285 			  {
4286 			    if (it == per_tu_class_map.begin())
4287 			      continue;
4288 			    else
4289 			      {
4290 				if (!compare_before_canonicalisation(it->second,
4291 								     first_class))
4292 				  {
4293 				    all_class_definitions_are_equal = false;
4294 				    break;
4295 				  }
4296 			      }
4297 			  }
4298 			if (all_class_definitions_are_equal)
4299 			  (*j)->set_definition_of_declaration(first_class);
4300 		      }
4301 		  }
4302 	      }
4303 	    resolved_classes.push_back(i->first);
4304 	  }
4305       }
4306 
4307     size_t num_decl_only_classes = declaration_only_classes().size(),
4308       num_resolved = resolved_classes.size();
4309     if (show_stats())
4310       cerr << "resolved " << num_resolved
4311 	   << " class declarations out of "
4312 	   << num_decl_only_classes
4313 	   << "\n";
4314 
4315     for (vector<string>::const_iterator i = resolved_classes.begin();
4316 	 i != resolved_classes.end();
4317 	 ++i)
4318       declaration_only_classes().erase(*i);
4319 
4320     for (string_classes_map::iterator i = declaration_only_classes().begin();
4321 	 i != declaration_only_classes().end();
4322 	 ++i)
4323       {
4324 	if (show_stats())
4325 	  {
4326 	    if (i == declaration_only_classes().begin())
4327 	      cerr << "Here are the "
4328 		   << num_decl_only_classes - num_resolved
4329 		   << " unresolved class declarations:\n";
4330 	    else
4331 	      cerr << "    " << i->first << "\n";
4332 	  }
4333       }
4334   }
4335 
4336   /// Getter for the map of declaration-only enums that are to be
4337   /// resolved to their definition enums by the end of the corpus
4338   /// loading.
4339   ///
4340   /// @return a map of string -> vector of enums where the key is
4341   /// the fully qualified name of the enum and the value is the
4342   /// vector of declaration-only enum.
4343   const string_enums_map&
declaration_only_enums() const4344   declaration_only_enums() const
4345   {return decl_only_enums_map_;}
4346 
4347   /// Getter for the map of declaration-only enums that are to be
4348   /// resolved to their definition enums by the end of the corpus
4349   /// loading.
4350   ///
4351   /// @return a map of string -> vector of enums where the key is
4352   /// the fully qualified name of the enum and the value is the
4353   /// vector of declaration-only enum.
4354   string_enums_map&
declaration_only_enums()4355   declaration_only_enums()
4356   {return decl_only_enums_map_;}
4357 
4358   /// If a given enum is a declaration-only enum then stash it on
4359   /// the side so that at the end of the corpus reading we can resolve
4360   /// it to its definition.
4361   ///
4362   /// @param enom the enum to consider.
4363   void
maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr & enom)4364   maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr& enom)
4365   {
4366     if (enom->get_is_declaration_only()
4367 	&& enom->get_definition_of_declaration() == 0)
4368       {
4369 	string qn = enom->get_qualified_name();
4370 	string_enums_map::iterator record =
4371 	  declaration_only_enums().find(qn);
4372 	if (record == declaration_only_enums().end())
4373 	  declaration_only_enums()[qn].push_back(enom);
4374 	else
4375 	  record->second.push_back(enom);
4376       }
4377   }
4378 
4379   /// Test if a given declaration-only enum has been scheduled for
4380   /// resolution to a defined enum.
4381   ///
4382   /// @param enom the enum to consider for the test.
4383   ///
4384   /// @return true iff @p enom is a declaration-only enum and if
4385   /// it's been scheduled for resolution to a defined enum.
4386   bool
is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr & enom)4387   is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4388   {
4389     if (enom->get_is_declaration_only())
4390       return (declaration_only_enums().find(enom->get_qualified_name())
4391 	      != declaration_only_enums().end());
4392 
4393     return false;
4394   }
4395 
4396   /// Walk the declaration-only enums that have been found during
4397   /// the building of the corpus and resolve them to their definitions.
4398   ///
4399   /// TODO: Do away with this function by factorizing it with
4400   /// resolve_declaration_only_classes.  All declaration-only decls
4401   /// could be handled the same way as declaration-only-ness is a
4402   /// property of abigail::ir::decl_base now.
4403   void
resolve_declaration_only_enums()4404   resolve_declaration_only_enums()
4405   {
4406     vector<string> resolved_enums;
4407 
4408     for (string_enums_map::iterator i =
4409 	   declaration_only_enums().begin();
4410 	 i != declaration_only_enums().end();
4411 	 ++i)
4412       {
4413 	bool to_resolve = false;
4414 	for (enums_type::iterator j = i->second.begin();
4415 	     j != i->second.end();
4416 	     ++j)
4417 	  if ((*j)->get_is_declaration_only()
4418 	      && ((*j)->get_definition_of_declaration() == 0))
4419 	    to_resolve = true;
4420 
4421 	if (!to_resolve)
4422 	  {
4423 	    resolved_enums.push_back(i->first);
4424 	    continue;
4425 	  }
4426 
4427 	// Now, for each decl-only enum that have the current name
4428 	// 'i->first', let's try to poke at the fully defined enum
4429 	// that is defined in the same translation unit as the
4430 	// declaration.
4431 	//
4432 	// If we find one enum (defined in the TU of the declaration)
4433 	// that defines the declaration, then the declaration can be
4434 	// resolved to that enum.
4435 	//
4436 	// If no defining enum is found in the TU of the declaration,
4437 	// then there are possibly three cases to consider:
4438 	//
4439 	//   1/ There is exactly one enum that defines the
4440 	//   declaration and that enum is defined in another TU.  In
4441 	//   this case, the declaration is resolved to that
4442 	//   definition.
4443 	//
4444 	//   2/ There are more than one enum that define that
4445 	//   declaration and none of them is defined in the TU of the
4446 	//   declaration.  In this case, the declaration is left
4447 	//   unresolved.
4448 	//
4449 	//   3/ No enum defines the declaration.  In this case, the
4450 	//   declaration is left unresoved.
4451 
4452 	// So get the enums that might define the current
4453 	// declarations which name is i->first.
4454 	const type_base_wptrs_type *enums =
4455 	  lookup_enum_types(i->first, *current_corpus());
4456 	if (!enums)
4457 	  continue;
4458 
4459 	unordered_map<string, enum_type_decl_sptr> per_tu_enum_map;
4460 	for (type_base_wptrs_type::const_iterator c = enums->begin();
4461 	     c != enums->end();
4462 	     ++c)
4463 	  {
4464 	    enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4465 	    ABG_ASSERT(enom);
4466 
4467 	    enom = is_enum_type(look_through_decl_only_enum(enom));
4468 	    if (enom->get_is_declaration_only())
4469 	      continue;
4470 
4471 	    string tu_path = enom->get_translation_unit()->get_absolute_path();
4472 	    if (tu_path.empty())
4473 	      continue;
4474 
4475 	    // Build a map that associates the translation unit path
4476 	    // to the enum (that potentially defines the declarations
4477 	    // that we consider) that are defined in that translation unit.
4478 	    per_tu_enum_map[tu_path] = enom;
4479 	  }
4480 
4481 	if (!per_tu_enum_map.empty())
4482 	  {
4483 	    // Walk the declarations to resolve and resolve them
4484 	    // either to the definitions that are in the same TU as
4485 	    // the declaration, or to the definition found elsewhere,
4486 	    // if there is only one such definition.
4487 	    for (enums_type::iterator j = i->second.begin();
4488 		 j != i->second.end();
4489 		 ++j)
4490 	      {
4491 		if ((*j)->get_is_declaration_only()
4492 		    && ((*j)->get_definition_of_declaration() == 0))
4493 		  {
4494 		    string tu_path =
4495 		      (*j)->get_translation_unit()->get_absolute_path();
4496 		    unordered_map<string, enum_type_decl_sptr>::const_iterator e =
4497 		      per_tu_enum_map.find(tu_path);
4498 		    if (e != per_tu_enum_map.end())
4499 		      (*j)->set_definition_of_declaration(e->second);
4500 		    else if (per_tu_enum_map.size() == 1)
4501 		      (*j)->set_definition_of_declaration
4502 			(per_tu_enum_map.begin()->second);
4503 		  }
4504 	      }
4505 	    resolved_enums.push_back(i->first);
4506 	  }
4507       }
4508 
4509     size_t num_decl_only_enums = declaration_only_enums().size(),
4510       num_resolved = resolved_enums.size();
4511     if (show_stats())
4512       cerr << "resolved " << num_resolved
4513 	   << " enum declarations out of "
4514 	   << num_decl_only_enums
4515 	   << "\n";
4516 
4517     for (vector<string>::const_iterator i = resolved_enums.begin();
4518 	 i != resolved_enums.end();
4519 	 ++i)
4520       declaration_only_enums().erase(*i);
4521 
4522     for (string_enums_map::iterator i = declaration_only_enums().begin();
4523 	 i != declaration_only_enums().end();
4524 	 ++i)
4525       {
4526 	if (show_stats())
4527 	  {
4528 	    if (i == declaration_only_enums().begin())
4529 	      cerr << "Here are the "
4530 		   << num_decl_only_enums - num_resolved
4531 		   << " unresolved enum declarations:\n";
4532 	    else
4533 	      cerr << "    " << i->first << "\n";
4534 	  }
4535       }
4536   }
4537 
4538   /// Test if a symbol belongs to a function of the current ABI
4539   /// corpus.
4540   ///
4541   /// This is a sub-routine of fixup_functions_with_no_symbols.
4542   ///
4543   /// @param fn the function symbol to consider.
4544   ///
4545   /// @returnt true if @p fn belongs to a function of the current ABI
4546   /// corpus.
4547   bool
symbol_already_belongs_to_a_function(elf_symbol_sptr & fn)4548   symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4549   {
4550     corpus_sptr corp = current_corpus();
4551     if (!corp)
4552       return false;
4553 
4554     string id = fn->get_id_string();
4555 
4556     const vector<function_decl*> *fns = corp->lookup_functions(id);
4557     if (!fns)
4558       return false;
4559 
4560     for (vector<function_decl*>::const_iterator i = fns->begin();
4561 	 i != fns->end();
4562 	 ++i)
4563       {
4564 	function_decl* f = *i;
4565 	ABG_ASSERT(f);
4566 	if (f->get_symbol())
4567 	  return true;
4568       }
4569     return false;
4570   }
4571 
4572   /// Some functions described by DWARF may have their linkage name
4573   /// set, but no link to their actual underlying elf symbol.  When
4574   /// these are virtual member functions, comparing the enclosing type
4575   /// against another one which has its underlying symbol properly set
4576   /// might lead to spurious type changes.
4577   ///
4578   /// If the corpus contains a symbol with the same name as the
4579   /// linkage name of the function, then set up the link between the
4580   /// function and its underlying symbol.
4581   ///
4582   /// Note that for the moment, only virtual member functions are
4583   /// fixed up like this.  This is because they really are the only
4584   /// fuctions of functions that can affect types (in spurious ways).
4585   void
fixup_functions_with_no_symbols()4586   fixup_functions_with_no_symbols()
4587   {
4588     corpus_sptr corp = current_corpus();
4589     if (!corp)
4590       return;
4591 
4592     die_function_decl_map_type &fns_with_no_symbol =
4593       die_function_decl_with_no_symbol_map();
4594 
4595     if (do_log())
4596       cerr << fns_with_no_symbol.size()
4597 	   << " functions to fixup, potentially\n";
4598 
4599     for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4600 	 i != fns_with_no_symbol.end();
4601 	 ++i)
4602       if (elf_symbol_sptr sym =
4603 	  corp->lookup_function_symbol(i->second->get_linkage_name()))
4604 	{
4605 	  // So i->second is a virtual member function that was
4606 	  // previously scheduled to be set a function symbol.
4607 	  //
4608 	  // But if it appears that it now has a symbol already set,
4609 	  // then do not set a symbol to it again.
4610 	  //
4611 	  // Or if it appears that another virtual member function
4612 	  // from the current ABI Corpus, with the same linkage
4613 	  // (mangled) name has already been set a symbol, then do not
4614 	  // set a symbol to this function either.  Otherwise, there
4615 	  // will be two virtual member functions with the same symbol
4616 	  // in the class and that leads to spurious hard-to-debug
4617 	  // change reports later down the road.
4618 	  if (i->second->get_symbol()
4619 	      || symbol_already_belongs_to_a_function(sym))
4620 	    continue;
4621 
4622 	  ABG_ASSERT(is_member_function(i->second));
4623 	  ABG_ASSERT(get_member_function_is_virtual(i->second));
4624 	  i->second->set_symbol(sym);
4625 	  // The function_decl now has an associated (public) ELF symbol so
4626 	  // it ought to be advertised as being public.
4627 	  i->second->set_is_in_public_symbol_table(true);
4628 	  // Add the function to the set of exported decls of the
4629 	  // current corpus.
4630 	  maybe_add_fn_to_exported_decls(i->second.get());
4631 	  if (do_log())
4632 	    cerr << "fixed up '"
4633 		 << i->second->get_pretty_representation()
4634 		 << "' with symbol '"
4635 		 << sym->get_id_string()
4636 		 << "'\n";
4637 	}
4638 
4639     fns_with_no_symbol.clear();
4640   }
4641 
4642   /// Return a reference to the vector containing the offsets of the
4643   /// types that need late canonicalizing.
4644   ///
4645   /// @param source whe DIEs referred to by the offsets contained in
4646   /// the vector to return are from.
4647   vector<Dwarf_Off>&
types_to_canonicalize(die_source source)4648   types_to_canonicalize(die_source source)
4649   {
4650     switch (source)
4651       {
4652       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4653 	break;
4654       case ALT_DEBUG_INFO_DIE_SOURCE:
4655 	return alt_types_to_canonicalize_;
4656       case TYPE_UNIT_DIE_SOURCE:
4657 	return type_unit_types_to_canonicalize_;
4658       case NO_DEBUG_INFO_DIE_SOURCE:
4659       case NUMBER_OF_DIE_SOURCES:
4660 	ABG_ASSERT_NOT_REACHED;
4661       }
4662     return types_to_canonicalize_;
4663   }
4664 
4665   /// Return a reference to the vector containing the offsets of the
4666   /// types that need late canonicalizing.
4667   ///
4668   /// @param source where the DIEs referred to by the offset in the
4669   /// returned vector are from.
4670   const vector<Dwarf_Off>&
types_to_canonicalize(die_source source) const4671   types_to_canonicalize(die_source source) const
4672   {return const_cast<read_context*>(this)->types_to_canonicalize(source);}
4673 
4674   /// Return a reference to the vector containing the types created
4675   /// during the binary analysis but that are not tied to a given
4676   /// DWARF DIE.
4677   ///
4678   /// @return reference to the vector containing the types created
4679   /// during the binary analysis but that are not tied to a given
4680   /// DWARF DIE.
4681   const vector<type_base_sptr>&
extra_types_to_canonicalize() const4682   extra_types_to_canonicalize() const
4683   {return extra_types_to_canonicalize_;}
4684 
4685   /// Clear the containers holding types to canonicalize.
4686   void
clear_types_to_canonicalize()4687   clear_types_to_canonicalize()
4688   {
4689     types_to_canonicalize_.clear();
4690     alt_types_to_canonicalize_.clear();
4691     type_unit_types_to_canonicalize_.clear();
4692     extra_types_to_canonicalize_.clear();
4693   }
4694 
4695   /// Put the offset of a DIE representing a type on a side vector so
4696   /// that when the reading of the debug info of the current
4697   /// translation unit is done, we can get back to the type DIE and
4698   /// from there, to the type it's associated to, and then
4699   /// canonicalize it.  This what we call late canonicalization.
4700   ///
4701   /// @param die the type DIE to schedule for late type
4702   /// canonicalization.
4703   void
schedule_type_for_late_canonicalization(const Dwarf_Die * die)4704   schedule_type_for_late_canonicalization(const Dwarf_Die *die)
4705   {
4706     Dwarf_Off o;
4707 
4708     Dwarf_Die equiv_die;
4709     ABG_ASSERT(get_canonical_die(die, equiv_die,
4710 				  /*where=*/0,
4711 				 /*die_as_type=*/true));
4712 
4713     const die_source source = get_die_source(&equiv_die);
4714     o = dwarf_dieoffset(&equiv_die);
4715 
4716     const die_artefact_map_type& m =
4717       type_die_artefact_maps().get_container(*this, die);
4718 
4719     die_artefact_map_type::const_iterator i = m.find(o);
4720     ABG_ASSERT(i != m.end());
4721 
4722     // Then really do the scheduling.
4723     types_to_canonicalize(source).push_back(o);
4724   }
4725 
4726   /// Types that were created but not tied to a particular DIE, must
4727   /// be scheduled for late canonicalization using this method.
4728   ///
4729   /// @param t the type to schedule for late canonicalization.
4730   void
schedule_type_for_late_canonicalization(const type_base_sptr & t)4731   schedule_type_for_late_canonicalization(const type_base_sptr &t)
4732   {
4733     extra_types_to_canonicalize_.push_back(t);
4734   }
4735 
4736   /// Canonicalize types which DIE offsets are stored in vectors on
4737   /// the side.  This is a sub-routine of
4738   /// read_context::perform_late_type_canonicalizing().
4739   ///
4740   /// @param source where the DIE of the types to canonicalize are
4741   /// from.
4742   void
canonicalize_types_scheduled(die_source source)4743   canonicalize_types_scheduled(die_source source)
4744   {
4745     tools_utils::timer cn_timer;
4746     if (do_log())
4747       {
4748 	cerr << "going to canonicalize types";
4749 	corpus_sptr c = current_corpus();
4750 	if (c)
4751 	  cerr << " of corpus " << current_corpus()->get_path();
4752 	cerr << " (DIEs source: " << source << ")\n";
4753 	cn_timer.start();
4754       }
4755 
4756     if (!types_to_canonicalize(source).empty()
4757 	|| !extra_types_to_canonicalize().empty())
4758       {
4759 	tools_utils::timer single_type_cn_timer;
4760 	size_t total = types_to_canonicalize(source).size();
4761 	if (do_log())
4762 	  cerr << total << " types to canonicalize\n";
4763 	for (size_t i = 0; i < total; ++i)
4764 	  {
4765 	    Dwarf_Off element = types_to_canonicalize(source)[i];
4766 	    type_base_sptr t =
4767 	      lookup_type_from_die_offset(element, source);
4768 	    ABG_ASSERT(t);
4769 	    if (do_log())
4770 	      {
4771 		cerr << "canonicalizing type "
4772 		     << get_pretty_representation(t, false)
4773 		     << " [" << i + 1 << "/" << total << "]";
4774 		if (corpus_sptr c = current_corpus())
4775 		  cerr << "@" << c->get_path();
4776 		cerr << " ...";
4777 		single_type_cn_timer.start();
4778 	      }
4779 	    canonicalize(t);
4780 	    if (do_log())
4781 	      {
4782 		cerr << " DONE";
4783 		single_type_cn_timer.stop();
4784 		cerr << ":" <<single_type_cn_timer << "\n";
4785 	      }
4786 	  }
4787 
4788 	// Now canonicalize types that were created but not tied to
4789 	// any DIE.
4790 	if (!extra_types_to_canonicalize().empty())
4791 	  {
4792 	    tools_utils::timer single_type_cn_timer;
4793 	    size_t total = extra_types_to_canonicalize().size();
4794 	    if (do_log())
4795 	      cerr << total << " extra types to canonicalize\n";
4796 	    size_t i = 1;
4797 	    for (vector<type_base_sptr>::const_iterator it =
4798 		   extra_types_to_canonicalize().begin();
4799 		 it != extra_types_to_canonicalize().end();
4800 		 ++it, ++i)
4801 	      {
4802 		if (do_log())
4803 		  {
4804 		    cerr << "canonicalizing extra type "
4805 			 << get_pretty_representation(*it, false)
4806 			 << " [" << i << "/" << total << "]";
4807 		    if (corpus_sptr c = current_corpus())
4808 		      cerr << "@" << c->get_path();
4809 		    cerr << " ...";
4810 		    single_type_cn_timer.start();
4811 		  }
4812 		canonicalize(*it);
4813 		if (do_log())
4814 		  {
4815 		    single_type_cn_timer.stop();
4816 		    cerr << "DONE:"
4817 			 << single_type_cn_timer
4818 			 << "\n";
4819 		  }
4820 	      }
4821 	  }
4822       }
4823 
4824     if (do_log())
4825       {
4826 	cn_timer.stop();
4827 	cerr << "finished canonicalizing types";
4828 	corpus_sptr c = current_corpus();
4829 	if (c)
4830 	  cerr << " of corpus " << current_corpus()->get_path();
4831 	cerr << " (DIEs source: "
4832 	     << source << "):"
4833 	     << cn_timer
4834 	     << "\n";
4835       }
4836   }
4837 
4838   /// Compute the number of canonicalized and missed types in the late
4839   /// canonicalization phase.
4840   ///
4841   /// @param source where the DIEs of the canonicalized types are
4842   /// from.
4843   ///
4844   /// @param canonicalized the number of types that got canonicalized
4845   /// is added to the value already present in this parameter.
4846   ///
4847   /// @param missed the number of types scheduled for late
4848   /// canonicalization and which couldn't be canonicalized (for a
4849   /// reason) is added to the value already present in this parameter.
4850   void
add_late_canonicalized_types_stats(die_source source,size_t & canonicalized,size_t & missed) const4851   add_late_canonicalized_types_stats(die_source	source,
4852 				     size_t&		canonicalized,
4853 				     size_t&		missed) const
4854   {
4855     for (vector<Dwarf_Off>::const_iterator i =
4856 	   types_to_canonicalize(source).begin();
4857 	 i != types_to_canonicalize(source).end();
4858 	 ++i)
4859       {
4860         type_base_sptr t = lookup_type_from_die_offset(*i, source);
4861 	if (t->get_canonical_type())
4862 	  ++canonicalized;
4863 	else
4864 	  ++missed;
4865       }
4866   }
4867 
4868   /// Compute the number of canonicalized and missed types in the late
4869   /// canonicalization phase.
4870   ///
4871   /// @param canonicalized the number of types that got canonicalized
4872   /// is added to the value already present in this parameter.
4873   ///
4874   /// @param missed the number of types scheduled for late
4875   /// canonicalization and which couldn't be canonicalized (for a
4876   /// reason) is added to the value already present in this parameter.
4877   void
add_late_canonicalized_types_stats(size_t & canonicalized,size_t & missed) const4878   add_late_canonicalized_types_stats(size_t& canonicalized,
4879 				     size_t& missed) const
4880   {
4881     for (die_source source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
4882 	 source < NUMBER_OF_DIE_SOURCES;
4883 	 ++source)
4884       add_late_canonicalized_types_stats(source, canonicalized, missed);
4885   }
4886 
4887   // Look at the types that need to be canonicalized after the
4888   // translation unit has been constructed and canonicalize them.
4889   void
perform_late_type_canonicalizing()4890   perform_late_type_canonicalizing()
4891   {
4892     for (die_source source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
4893 	 source < NUMBER_OF_DIE_SOURCES;
4894 	 ++source)
4895       canonicalize_types_scheduled(source);
4896 
4897     if (show_stats())
4898       {
4899 	size_t num_canonicalized = 0, num_missed = 0, total = 0;
4900 	add_late_canonicalized_types_stats(num_canonicalized,
4901 					   num_missed);
4902 	total = num_canonicalized + num_missed;
4903 	cerr << "binary: "
4904 	     << elf_path()
4905 	     << "\n";
4906 	cerr << "    # late canonicalized types: "
4907              << num_canonicalized;
4908         if (total)
4909           cerr << " (" << num_canonicalized * 100 / total << "%)";
4910         cerr << "\n"
4911 	     << "    # missed canonicalization opportunities: "
4912              << num_missed;
4913         if (total)
4914           cerr << " (" << num_missed * 100 / total << "%)";
4915         cerr << "\n";
4916       }
4917 
4918   }
4919 
4920   const die_tu_map_type&
die_tu_map() const4921   die_tu_map() const
4922   {return die_tu_map_;}
4923 
4924   die_tu_map_type&
die_tu_map()4925   die_tu_map()
4926   {return die_tu_map_;}
4927 
4928   /// Getter for the map that associates a translation unit DIE to the
4929   /// vector of imported unit points that it contains.
4930   ///
4931   /// @param source where the DIEs are from.
4932   ///
4933   /// @return the map.
4934   const tu_die_imported_unit_points_map_type&
tu_die_imported_unit_points_map(die_source source) const4935   tu_die_imported_unit_points_map(die_source source) const
4936   {return const_cast<read_context*>(this)->tu_die_imported_unit_points_map(source);}
4937 
4938   /// Getter for the map that associates a translation unit DIE to the
4939   /// vector of imported unit points that it contains.
4940   ///
4941   /// @param source where the DIEs are from.
4942   ///
4943   /// @return the map.
4944   tu_die_imported_unit_points_map_type&
tu_die_imported_unit_points_map(die_source source)4945   tu_die_imported_unit_points_map(die_source source)
4946   {
4947     switch (source)
4948       {
4949       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4950 	break;
4951       case ALT_DEBUG_INFO_DIE_SOURCE:
4952 	return alt_tu_die_imported_unit_points_map_;
4953       case TYPE_UNIT_DIE_SOURCE:
4954 	return type_units_tu_die_imported_unit_points_map_;
4955       case NO_DEBUG_INFO_DIE_SOURCE:
4956       case NUMBER_OF_DIE_SOURCES:
4957 	// We cannot reach this point.
4958 	ABG_ASSERT_NOT_REACHED;
4959       }
4960     return tu_die_imported_unit_points_map_;
4961   }
4962 
4963   /// Getter of the current corpus being constructed.
4964   ///
4965   /// @return the current corpus.
4966   const corpus_sptr
current_corpus() const4967   current_corpus() const
4968   {return cur_corpus_;}
4969 
4970   /// Getter of the current corpus being constructed.
4971   ///
4972   /// @return the current corpus.
4973   corpus_sptr
current_corpus()4974   current_corpus()
4975   {return cur_corpus_;}
4976 
4977   /// Setter of the current corpus being constructed.
4978   ///
4979   /// @param c the new corpus.
4980   void
current_corpus(const corpus_sptr & c)4981   current_corpus(const corpus_sptr& c)
4982   {
4983     if (c)
4984       cur_corpus_ = c;
4985   }
4986 
4987   /// Reset the current corpus being constructed.
4988   ///
4989   /// This actually deletes the current corpus being constructed.
4990   void
reset_current_corpus()4991   reset_current_corpus()
4992   {cur_corpus_.reset();}
4993 
4994   /// Getter of the current corpus group being constructed.
4995   ///
4996   /// @return current the current corpus being constructed, if any, or
4997   /// nil.
4998   const corpus_group_sptr
current_corpus_group() const4999   current_corpus_group() const
5000   {return cur_corpus_group_;}
5001 
5002   /// Getter of the current corpus group being constructed.
5003   ///
5004   /// @return current the current corpus being constructed, if any, or
5005   /// nil.
5006   corpus_group_sptr
current_corpus_group()5007   current_corpus_group()
5008   {return cur_corpus_group_;}
5009 
5010   /// Setter of the current corpus group being constructed.
5011   ///
5012   /// @param g the new corpus group.
5013   void
current_corpus_group(const corpus_group_sptr & g)5014   current_corpus_group(const corpus_group_sptr& g)
5015   {
5016     if (g)
5017       cur_corpus_group_ = g;
5018   }
5019 
5020   /// Test if there is a corpus group being built.
5021   ///
5022   /// @return if there is a corpus group being built, false otherwise.
5023   bool
has_corpus_group() const5024   has_corpus_group() const
5025   {return bool(cur_corpus_group_);}
5026 
5027   /// Return the main corpus from the current corpus group, if any.
5028   ///
5029   /// @return the main corpus of the current corpus group, if any, nil
5030   /// if no corpus group is being constructed.
5031   corpus_sptr
main_corpus_from_current_group()5032   main_corpus_from_current_group()
5033   {
5034     if (cur_corpus_group_)
5035       return cur_corpus_group_->get_main_corpus();
5036     return corpus_sptr();
5037   }
5038 
5039   /// Return the main corpus from the current corpus group, if any.
5040   ///
5041   /// @return the main corpus of the current corpus group, if any, nil
5042   /// if no corpus group is being constructed.
5043   const corpus_sptr
main_corpus_from_current_group() const5044   main_corpus_from_current_group() const
5045   {return const_cast<read_context*>(this)->main_corpus_from_current_group();}
5046 
5047   /// Test if the current corpus being built is the main corpus of the
5048   /// current corpus group.
5049   ///
5050   /// @return return true iff the current corpus being built is the
5051   /// main corpus of the current corpus group.
5052   bool
current_corpus_is_main_corpus_from_current_group() const5053   current_corpus_is_main_corpus_from_current_group() const
5054   {
5055     corpus_sptr main_corpus = main_corpus_from_current_group();
5056 
5057     if (main_corpus && main_corpus.get() == cur_corpus_.get())
5058       return true;
5059 
5060     return false;
5061   }
5062 
5063   /// Return true if the current corpus is part of a corpus group
5064   /// being built and if it's not the main corpus of the group.
5065   ///
5066   /// For instance, this would return true if we are loading a linux
5067   /// kernel *module* that is part of the current corpus group that is
5068   /// being built.  In this case, it means we should re-use types
5069   /// coming from the "vmlinux" binary that is the main corpus of the
5070   /// group.
5071   ///
5072   /// @return the corpus group the current corpus belongs to, if the
5073   /// current corpus is part of a corpus group being built. Nil otherwise.
5074   corpus_sptr
should_reuse_type_from_corpus_group() const5075   should_reuse_type_from_corpus_group() const
5076   {
5077     if (has_corpus_group() && is_c_language(cur_transl_unit()->get_language()))
5078       if (corpus_sptr main_corpus = main_corpus_from_current_group())
5079 	if (!current_corpus_is_main_corpus_from_current_group())
5080 	  return current_corpus_group();
5081 
5082     return corpus_sptr();
5083   }
5084 
5085   /// Get the map that associates each DIE to its parent DIE.  This is
5086   /// for DIEs coming from the main debug info sections.
5087   ///
5088   /// @param source where the DIEs in the map come from.
5089   ///
5090   /// @return the DIE -> parent map.
5091   const offset_offset_map_type&
die_parent_map(die_source source) const5092   die_parent_map(die_source source) const
5093   {return const_cast<read_context*>(this)->die_parent_map(source);}
5094 
5095   /// Get the map that associates each DIE to its parent DIE.  This is
5096   /// for DIEs coming from the main debug info sections.
5097   ///
5098   /// @param source where the DIEs in the map come from.
5099   ///
5100   /// @return the DIE -> parent map.
5101   offset_offset_map_type&
die_parent_map(die_source source)5102   die_parent_map(die_source source)
5103   {
5104     switch (source)
5105       {
5106       case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5107 	break;
5108       case ALT_DEBUG_INFO_DIE_SOURCE:
5109 	return alternate_die_parent_map_;
5110       case TYPE_UNIT_DIE_SOURCE:
5111 	return type_section_die_parent_map();
5112       case NO_DEBUG_INFO_DIE_SOURCE:
5113       case NUMBER_OF_DIE_SOURCES:
5114 	ABG_ASSERT_NOT_REACHED;
5115       }
5116     return primary_die_parent_map_;
5117   }
5118 
5119   const offset_offset_map_type&
type_section_die_parent_map() const5120   type_section_die_parent_map() const
5121   {return type_section_die_parent_map_;}
5122 
5123   offset_offset_map_type&
type_section_die_parent_map()5124   type_section_die_parent_map()
5125   {return type_section_die_parent_map_;}
5126 
5127   /// Getter of the current translation unit.
5128   ///
5129   /// @return the current translation unit being constructed.
5130   const translation_unit_sptr&
cur_transl_unit() const5131   cur_transl_unit() const
5132   {return cur_tu_;}
5133 
5134   /// Getter of the current translation unit.
5135   ///
5136   /// @return the current translation unit being constructed.
5137   translation_unit_sptr&
cur_transl_unit()5138   cur_transl_unit()
5139   {return cur_tu_;}
5140 
5141   /// Setter of the current translation unit.
5142   ///
5143   /// @param tu the current translation unit being constructed.
5144   void
cur_transl_unit(translation_unit_sptr tu)5145   cur_transl_unit(translation_unit_sptr tu)
5146   {
5147     if (tu)
5148       cur_tu_ = tu;
5149   }
5150 
5151   /// Return the global scope of the current translation unit.
5152   ///
5153   /// @return the global scope of the current translation unit.
5154   const scope_decl_sptr&
global_scope() const5155   global_scope() const
5156   {return cur_transl_unit()->get_global_scope();}
5157 
5158   /// Return a scope that is nil.
5159   ///
5160   /// @return a scope that is nil.
5161   const scope_decl_sptr&
nil_scope() const5162   nil_scope() const
5163   {return nil_scope_;}
5164 
5165   const scope_stack_type&
scope_stack() const5166   scope_stack() const
5167   {return scope_stack_;}
5168 
5169   scope_stack_type&
scope_stack()5170   scope_stack()
5171   {return scope_stack_;}
5172 
5173   scope_decl*
current_scope()5174   current_scope()
5175   {
5176     if (scope_stack().empty())
5177       {
5178 	if (cur_transl_unit())
5179 	  scope_stack().push(cur_transl_unit()->get_global_scope().get());
5180       }
5181     return scope_stack().top();
5182   }
5183 
5184   list<var_decl_sptr>&
var_decls_to_re_add_to_tree()5185   var_decls_to_re_add_to_tree()
5186   {return var_decls_to_add_;}
5187 
5188   /// The section containing the symbol table from the current ELF
5189   /// file.
5190   ///
5191   /// Note that after it's first invocation, this function caches the
5192   /// symbol table that it found.  Subsequent invocations just return
5193   /// the cached symbol table section.
5194   ///
5195   /// @return the symbol table section if found
5196   Elf_Scn*
find_symbol_table_section() const5197   find_symbol_table_section() const
5198   {
5199     if (!symtab_section_)
5200       symtab_section_ = elf_helpers::find_symbol_table_section(elf_handle());
5201     return symtab_section_;
5202   }
5203 
5204   /// Lookup an elf symbol, referred to by its index, from the .symtab
5205   /// section.
5206   ///
5207   /// The resulting symbol returned is an instance of a GElf_Sym, from
5208   /// the libelf library.
5209   ///
5210   /// @param symbol_index the index of the symbol to look up.
5211   ///
5212   /// @param elf_sym out parameter.  This is set to the resulting ELF
5213   /// symbol iff the function returns TRUE, meaning the symbol was
5214   /// found.
5215   ///
5216   /// @return TRUE iff the symbol was found.
5217   bool
lookup_native_elf_symbol_from_index(size_t symbol_index,GElf_Sym & elf_sym)5218   lookup_native_elf_symbol_from_index(size_t symbol_index, GElf_Sym &elf_sym)
5219   {
5220     Elf_Scn* symtab_section = find_symbol_table_section();
5221     if (!symtab_section)
5222       return false;
5223 
5224     Elf_Data* symtab = elf_getdata(symtab_section, 0);
5225     ABG_ASSERT(symtab);
5226 
5227     if (!gelf_getsym(symtab, symbol_index, &elf_sym))
5228       return false;
5229 
5230     return true;
5231   }
5232 
5233   /// Test if a given function symbol has been exported.
5234   ///
5235   /// @param symbol_address the address of the symbol we are looking
5236   /// for.  Note that this address must be a relative offset from the
5237   /// beginning of the .text section, just like the kind of addresses
5238   /// that are present in the .symtab section.
5239   ///
5240   /// @returnthe elf symbol if found, or nil otherwise.
5241   elf_symbol_sptr
function_symbol_is_exported(GElf_Addr symbol_address) const5242   function_symbol_is_exported(GElf_Addr symbol_address) const
5243   {
5244     elf_symbol_sptr symbol = symtab()->lookup_symbol(symbol_address);
5245     if (!symbol)
5246       return symbol;
5247 
5248     if (!symbol->is_function() || !symbol->is_public())
5249       return elf_symbol_sptr();
5250 
5251     address_set_sptr set;
5252     bool looking_at_linux_kernel_binary =
5253       load_in_linux_kernel_mode() && is_linux_kernel(elf_handle());
5254 
5255     if (looking_at_linux_kernel_binary)
5256       {
5257 	if (symbol->is_in_ksymtab())
5258 	  return symbol;
5259 	return elf_symbol_sptr();
5260       }
5261 
5262     return symbol;
5263   }
5264 
5265   /// Test if a given variable symbol has been exported.
5266   ///
5267   /// @param symbol_address the address of the symbol we are looking
5268   /// for.  Note that this address must be a relative offset from the
5269   /// beginning of the .text section, just like the kind of addresses
5270   /// that are present in the .symtab section.
5271   ///
5272   /// @returnthe elf symbol if found, or nil otherwise.
5273   elf_symbol_sptr
variable_symbol_is_exported(GElf_Addr symbol_address) const5274   variable_symbol_is_exported(GElf_Addr symbol_address) const
5275   {
5276     elf_symbol_sptr symbol = symtab()->lookup_symbol(symbol_address);
5277     if (!symbol)
5278       return symbol;
5279 
5280     if (!symbol->is_variable() || !symbol->is_public())
5281       return elf_symbol_sptr();
5282 
5283     address_set_sptr set;
5284     bool looking_at_linux_kernel_binary =
5285       load_in_linux_kernel_mode() && is_linux_kernel(elf_handle());
5286 
5287     if (looking_at_linux_kernel_binary)
5288       {
5289 	if (symbol->is_in_ksymtab())
5290 	  return symbol;
5291 	return elf_symbol_sptr();
5292       }
5293 
5294     return symbol;
5295   }
5296 
5297   /// Getter for the symtab reader. Will load the symtab from the elf handle if
5298   /// not yet set.
5299   ///
5300   /// @return a shared pointer to the symtab object
5301   const symtab_reader::symtab_sptr&
symtab() const5302   symtab() const
5303   {
5304     if (!symtab_)
5305       symtab_ = symtab_reader::symtab::load
5306 	(elf_handle(), options_.env,
5307 	 [&](const elf_symbol_sptr& symbol)
5308 	 {return is_elf_symbol_suppressed(symbol);});
5309 
5310     if (!symtab_)
5311       std::cerr << "Symbol table of '" << elf_path_
5312 		<< "' could not be loaded\n";
5313     return symtab_;
5314   }
5315 
5316   /// Getter for the ELF dt_needed tag.
5317   const vector<string>&
dt_needed() const5318   dt_needed() const
5319   {return dt_needed_;}
5320 
5321   /// Getter for the ELF dt_soname tag.
5322   const string&
dt_soname() const5323   dt_soname() const
5324   {return dt_soname_;}
5325 
5326   /// Getter for the ELF architecture of the current file.
5327   const string&
elf_architecture() const5328   elf_architecture() const
5329   {return elf_architecture_;}
5330 
5331   /// Test if a given ELF symbol was suppressed by a suppression
5332   /// specification.
5333   ///
5334   /// @param symbol the ELF symbol to consider.
5335   ///
5336   /// @return true iff @p symbol is suppressed.
5337   bool
is_elf_symbol_suppressed(const elf_symbol_sptr & symbol) const5338   is_elf_symbol_suppressed(const elf_symbol_sptr& symbol) const
5339   {
5340     return (symbol
5341 	    && suppr::is_elf_symbol_suppressed(*this,
5342 					       symbol->get_name(),
5343 					       symbol->get_type()));
5344   }
5345 
5346   /// Load the DT_NEEDED and DT_SONAME elf TAGS.
5347   ///
5348   void
load_dt_soname_and_needed()5349   load_dt_soname_and_needed()
5350   {
5351     lookup_data_tag_from_dynamic_segment(elf_handle(), DT_NEEDED, dt_needed_);
5352 
5353     vector<string> dt_tag_data;
5354     lookup_data_tag_from_dynamic_segment(elf_handle(), DT_SONAME, dt_tag_data);
5355     if (!dt_tag_data.empty())
5356       dt_soname_ = dt_tag_data[0];
5357   }
5358 
5359   /// Read the string representing the architecture of the current ELF
5360   /// file.
5361   void
load_elf_architecture()5362   load_elf_architecture()
5363   {
5364     if (!elf_handle())
5365       return;
5366 
5367     GElf_Ehdr eh_mem;
5368     GElf_Ehdr* elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5369 
5370     elf_architecture_ = e_machine_to_string(elf_header->e_machine);
5371   }
5372 
5373   /// Load various ELF data.
5374   ///
5375   /// This function loads ELF data that are not symbol maps or debug
5376   /// info.  That is, things like various tags, elf architecture and
5377   /// so on.
5378   void
load_elf_properties()5379   load_elf_properties()
5380   {
5381     load_dt_soname_and_needed();
5382     load_elf_architecture();
5383   }
5384 
5385   /// This is a sub-routine of maybe_adjust_fn_sym_address and
5386   /// maybe_adjust_var_sym_address.
5387   ///
5388   /// Given an address that we got by looking at some debug
5389   /// information (e.g, a symbol's address referred to by a DWARF
5390   /// TAG), If the ELF file we are interested in is a shared library
5391   /// or an executable, then adjust the address to be coherent with
5392   /// where the executable (or shared library) is loaded.  That way,
5393   /// the address can be used to look for symbols in the executable or
5394   /// shared library.
5395   ///
5396   /// @return the adjusted address, or the same address as @p addr if
5397   /// it didn't need any adjustment.
5398   Dwarf_Addr
maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const5399   maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
5400   {
5401     if (addr == 0)
5402       return addr;
5403 
5404     GElf_Ehdr eh_mem;
5405     GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5406 
5407     if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
5408       {
5409 	Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
5410 	ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
5411 					   dwarf_elf_load_address));
5412 	ABG_ASSERT(get_binary_load_address(elf_handle(),
5413 					   elf_load_address));
5414 	if (dwarf_is_splitted()
5415 	    && (dwarf_elf_load_address != elf_load_address))
5416 	  // This means that in theory the DWARF and the executable are
5417 	  // not loaded at the same address.  And addr is meaningful
5418 	  // only in the context of the DWARF.
5419 	  //
5420 	  // So let's transform addr into an offset relative to where
5421 	  // the DWARF is loaded, and let's add that relative offset
5422 	  // to the load address of the executable.  That way, addr
5423 	  // becomes meaningful in the context of the executable and
5424 	  // can thus be used to compare against the address of
5425 	  // symbols of the executable, for instance.
5426 	  addr = addr - dwarf_elf_load_address + elf_load_address;
5427       }
5428 
5429     return addr;
5430   }
5431 
5432   /// For a relocatable (*.o) elf file, this function expects an
5433   /// absolute address, representing a function symbol.  It then
5434   /// extracts the address of the .text section from the symbol
5435   /// absolute address to get the relative address of the function
5436   /// from the beginning of the .text section.
5437   ///
5438   /// For executable or shared library, this function expects an
5439   /// address of a function symbol that was retrieved by looking at a
5440   /// DWARF "file".  The function thus adjusts the address to make it
5441   /// be meaningful in the context of the ELF file.
5442   ///
5443   /// In both cases, the address can then be compared against the
5444   /// st_value field of a function symbol from the ELF file.
5445   ///
5446   /// @param addr an adress for a function symbol that was retrieved
5447   /// from a DWARF file.
5448   ///
5449   /// @return the (possibly) adjusted address, or just @p addr if no
5450   /// adjustment took place.
5451   Dwarf_Addr
maybe_adjust_fn_sym_address(Dwarf_Addr addr) const5452   maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
5453   {
5454     if (addr == 0)
5455       return addr;
5456 
5457     Elf* elf = elf_handle();
5458     GElf_Ehdr eh_mem;
5459     GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5460 
5461     if (elf_header->e_type == ET_REL)
5462       // We are looking at a relocatable file.  In this case, we don't
5463       // do anything because:
5464       //
5465       // 1/ the addresses from DWARF are absolute (relative to the
5466       // beginning of the relocatable file)
5467       //
5468       // 2/ The ELF symbol addresses that we store in our lookup
5469       // tables are translated from section-related to absolute as
5470       // well.  So we don't have anything to do at this point for
5471       // ET_REL files.
5472       ;
5473     else
5474       addr = maybe_adjust_address_for_exec_or_dyn(addr);
5475 
5476     return addr;
5477   }
5478 
5479   /// For a relocatable (*.o) elf file, this function expects an
5480   /// absolute address, representing a global variable symbol.  It
5481   /// then extracts the address of the {.data,.data1,.rodata,.bss}
5482   /// section from the symbol absolute address to get the relative
5483   /// address of the variable from the beginning of the data section.
5484   ///
5485   /// For executable or shared library, this function expects an
5486   /// address of a variable symbol that was retrieved by looking at a
5487   /// DWARF "file".  The function thus adjusts the address to make it
5488   /// be meaningful in the context of the ELF file.
5489   ///
5490   /// In both cases, the address can then be compared against the
5491   /// st_value field of a function symbol from the ELF file.
5492   ///
5493   /// @param addr an address for a global variable symbol that was
5494   /// retrieved from a DWARF file.
5495   ///
5496   /// @return the (possibly) adjusted address, or just @p addr if no
5497   /// adjustment took place.
5498   Dwarf_Addr
maybe_adjust_var_sym_address(Dwarf_Addr addr) const5499   maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5500   {
5501     Elf* elf = elf_handle();
5502     GElf_Ehdr eh_mem;
5503     GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5504 
5505     if (elf_header->e_type == ET_REL)
5506       // We are looking at a relocatable file.  In this case, we don't
5507       // do anything because:
5508       //
5509       // 1/ the addresses from DWARF are absolute (relative to the
5510       // beginning of the relocatable file)
5511       //
5512       // 2/ The ELF symbol addresses that we store in our lookup
5513       // tables are translated from section-related to absolute as
5514       // well.  So we don't have anything to do at this point for
5515       // ET_REL files.
5516       ;
5517     else
5518       addr = maybe_adjust_address_for_exec_or_dyn(addr);
5519 
5520     return addr;
5521   }
5522 
5523   /// Get the first exported function address in the set of addresses
5524   /// referred to by the DW_AT_ranges attribute of a given DIE.
5525   ///
5526   /// @param die the DIE we are considering.
5527   ///
5528   /// @param address output parameter.  This is set to the first
5529   /// address found in the sequence pointed to by the DW_AT_ranges
5530   /// attribute found on the DIE @p die, iff the function returns
5531   /// true.  Otherwise, no value is set into this output parameter.
5532   ///
5533   /// @return true iff the DIE @p die does have a DW_AT_ranges
5534   /// attribute and an address of an exported function was found in
5535   /// its sequence value.
5536   bool
get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die * die,Dwarf_Addr & address) const5537   get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5538 						  Dwarf_Addr& address) const
5539   {
5540     Dwarf_Addr base;
5541     Dwarf_Addr end_addr;
5542     ptrdiff_t offset = 0;
5543 
5544     do
5545       {
5546 	Dwarf_Addr addr = 0, fn_addr = 0;
5547 	if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5548 	  {
5549 	    fn_addr = maybe_adjust_fn_sym_address(addr);
5550 	    if (function_symbol_is_exported(fn_addr))
5551 	      {
5552 		address = fn_addr;
5553 		return true;
5554 	      }
5555 	  }
5556       } while (offset > 0);
5557     return false;
5558   }
5559 
5560   /// Get the address of the function.
5561   ///
5562   /// The address of the function is considered to be the value of the
5563   /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5564   /// only) to not point to an absolute address anymore, but rather to
5565   /// the address of the function inside the .text segment.
5566   ///
5567   /// @param function_die the die of the function to consider.
5568   ///
5569   /// @param address the resulting address iff the function returns
5570   /// true.
5571   ///
5572   /// @return true if the function address was found.
5573   bool
get_function_address(Dwarf_Die * function_die,Dwarf_Addr & address) const5574   get_function_address(Dwarf_Die* function_die, Dwarf_Addr& address) const
5575   {
5576     if (!die_address_attribute(function_die, DW_AT_low_pc, address))
5577       // So no DW_AT_low_pc was found.  Let's see if the function DIE
5578       // has got a DW_AT_ranges attribute instead.  If it does, the
5579       // first address of the set of addresses represented by the
5580       // value of that DW_AT_ranges represents the function (symbol)
5581       // address we are looking for.
5582       if (!get_first_exported_fn_address_from_DW_AT_ranges(function_die,
5583 							   address))
5584 	return false;
5585 
5586     address = maybe_adjust_fn_sym_address(address);
5587     return true;
5588   }
5589 
5590   /// Get the address of the global variable.
5591   ///
5592   /// The address of the global variable is considered to be the value
5593   /// of the DW_AT_location attribute, possibly adjusted (in
5594   /// relocatable files only) to not point to an absolute address
5595   /// anymore, but rather to the address of the global variable inside
5596   /// the data segment.
5597   ///
5598   /// @param variable_die the die of the function to consider.
5599   ///
5600   /// @param address the resulting address iff this function returns
5601   /// true.
5602   ///
5603   /// @return true if the variable address was found.
5604   bool
get_variable_address(Dwarf_Die * variable_die,Dwarf_Addr & address) const5605   get_variable_address(Dwarf_Die*	variable_die,
5606 		       Dwarf_Addr&	address) const
5607   {
5608     bool is_tls_address = false;
5609     if (!die_location_address(variable_die, address, is_tls_address))
5610       return false;
5611     if (!is_tls_address)
5612       address = maybe_adjust_var_sym_address(address);
5613     return true;
5614   }
5615 
5616   /// Tests if a suppression specification can match ABI artifacts
5617   /// coming from the binary being analyzed.
5618   ///
5619   /// This tests if the suppression can match the soname of and binary
5620   /// name of the ELF binary being analyzed.  More precisely, if there
5621   /// are any soname or file name property in the suppression and if
5622   /// those do *NOT* match the current binary, then the function
5623   /// returns false.
5624   ///
5625   /// @param s the suppression specification to consider.
5626   ///
5627   /// @return true iff either there are no soname/filename related
5628   /// property on the suppression, or if none of the soname/filename
5629   /// properties of the suppression match the current binary.
5630   bool
suppression_can_match(const suppr::suppression_base & s) const5631   suppression_can_match(const suppr::suppression_base& s) const
5632   {
5633     if (!s.priv_->matches_soname(dt_soname()))
5634       if (s.has_soname_related_property())
5635 	// The suppression has some SONAME related properties, but
5636 	// none of them match the SONAME of the current binary.  So
5637 	// the suppression cannot match the current binary.
5638 	return false;
5639 
5640     if (!s.priv_->matches_binary_name(elf_path()))
5641       if (s.has_file_name_related_property())
5642 	// The suppression has some file_name related properties, but
5643 	// none of them match the file name of the current binary.  So
5644 	// the suppression cannot match the current binary.
5645 	return false;
5646 
5647     return true;
5648   }
5649 
5650   /// Test whether if a given function suppression matches a function
5651   /// designated by a regular expression that describes its linkage
5652   /// name (symbol name).
5653   ///
5654   /// @param s the suppression specification to evaluate to see if it
5655   /// matches a given function linkage name
5656   ///
5657   /// @param fn_linkage_name the linkage name of the function of interest.
5658   ///
5659   /// @return true iff the suppression specification @p s matches the
5660   /// function whose linkage name is @p fn_linkage_name.
5661   bool
suppression_matches_function_sym_name(const suppr::function_suppression & s,const string & fn_linkage_name) const5662   suppression_matches_function_sym_name(const suppr::function_suppression& s,
5663 					const string& fn_linkage_name) const
5664   {
5665     if (!suppression_can_match(s))
5666       return false;
5667 
5668     return suppr::suppression_matches_function_sym_name(s, fn_linkage_name);
5669   }
5670 
5671   /// Test whether if a given function suppression matches a function
5672   /// designated by a regular expression that describes its name.
5673   ///
5674   /// @param s the suppression specification to evaluate to see if it
5675   /// matches a given function name.
5676   ///
5677   /// @param fn_name the name of the function of interest.  Note that
5678   /// this name must be *non* qualified.
5679   ///
5680   /// @return true iff the suppression specification @p s matches the
5681   /// function whose name is @p fn_name.
5682   bool
suppression_matches_function_name(const suppr::function_suppression & s,const string & fn_name) const5683   suppression_matches_function_name(const suppr::function_suppression& s,
5684 				    const string& fn_name) const
5685   {
5686     if (!suppression_can_match(s))
5687       return false;
5688 
5689     return suppr::suppression_matches_function_name(s, fn_name);
5690   }
5691 
5692   /// Test whether if a given variable suppression specification
5693   /// matches a variable denoted by its name.
5694   ///
5695   /// @param s the variable suppression specification to consider.
5696   ///
5697   /// @param var_name the name of the variable to consider.
5698   ///
5699   /// @return true iff the suppression specification @p s matches the
5700   /// variable whose name is @p var_name.
5701   bool
suppression_matches_variable_name(const suppr::variable_suppression & s,const string & var_name) const5702   suppression_matches_variable_name(const suppr::variable_suppression& s,
5703 				    const string& var_name) const
5704   {
5705     if (!suppression_can_match(s))
5706       return false;
5707 
5708     return suppr::suppression_matches_variable_name(s, var_name);
5709   }
5710 
5711   /// Test whether if a given variable suppression specification
5712   /// matches a variable denoted by its linkage name.
5713   ///
5714   /// @param s the variable suppression specification to consider.
5715   ///
5716   /// @param var_linkage_name the linkage name of the variable to consider.
5717   ///
5718   /// @return true iff variable suppression specification @p s matches
5719   /// the variable denoted by linkage name @p var_linkage_name.
5720   bool
suppression_matches_variable_sym_name(const suppr::variable_suppression & s,const string & var_linkage_name) const5721   suppression_matches_variable_sym_name(const suppr::variable_suppression& s,
5722 					const string& var_linkage_name) const
5723   {
5724     if (!suppression_can_match(s))
5725       return false;
5726 
5727     return suppr::suppression_matches_variable_sym_name(s, var_linkage_name);
5728   }
5729 
5730   /// Test if a given type suppression specification matches a type
5731   /// designated by its name and location.
5732   ///
5733   /// @param s the suppression specification to consider.
5734   ///
5735   /// @param type_name the fully qualified type name to consider.
5736   ///
5737   /// @param type_location the type location to consider.
5738   ///
5739   /// @return true iff the type suppression specification matches a
5740   /// type of a given name and location.
5741   bool
suppression_matches_type_name_or_location(const suppr::type_suppression & s,const string & type_name,const location & type_location) const5742   suppression_matches_type_name_or_location(const suppr::type_suppression& s,
5743 					    const string& type_name,
5744 					    const location& type_location) const
5745   {
5746     if (!suppression_can_match(s))
5747       return false;
5748 
5749     return suppr::suppression_matches_type_name_or_location(s, type_name,
5750 							    type_location);
5751   }
5752 
5753   /// Getter of the exported decls builder object.
5754   ///
5755   /// @return the exported decls builder.
5756   corpus::exported_decls_builder*
exported_decls_builder()5757   exported_decls_builder()
5758   {return exported_decls_builder_;}
5759 
5760   /// Setter of the exported decls builder object.
5761   ///
5762   /// Note that this @ref read_context is not responsible for the live
5763   /// time of the exported_decls_builder object.  The corpus is.
5764   ///
5765   /// @param b the new builder.
5766   void
exported_decls_builder(corpus::exported_decls_builder * b)5767   exported_decls_builder(corpus::exported_decls_builder* b)
5768   {exported_decls_builder_ = b;}
5769 
5770   /// Getter of the "load_all_types" flag.  This flag tells if all the
5771   /// types (including those not reachable by public declarations) are
5772   /// to be read and represented in the final ABI corpus.
5773   ///
5774   /// @return the load_all_types flag.
5775   bool
load_all_types() const5776   load_all_types() const
5777   {return options_.load_all_types;}
5778 
5779   /// Setter of the "load_all_types" flag.  This flag tells if all the
5780   /// types (including those not reachable by public declarations) are
5781   /// to be read and represented in the final ABI corpus.
5782   ///
5783   /// @param f the new load_all_types flag.
5784   void
load_all_types(bool f)5785   load_all_types(bool f)
5786   {options_.load_all_types = f;}
5787 
5788   bool
load_in_linux_kernel_mode() const5789   load_in_linux_kernel_mode() const
5790   {return options_.load_in_linux_kernel_mode;}
5791 
5792   void
load_in_linux_kernel_mode(bool f)5793   load_in_linux_kernel_mode(bool f)
5794   {options_.load_in_linux_kernel_mode = f;}
5795 
5796   /// Getter of the "show_stats" flag.
5797   ///
5798   /// This flag tells if we should emit statistics about various
5799   /// internal stuff.
5800   ///
5801   /// @return the value of the flag.
5802   bool
show_stats() const5803   show_stats() const
5804   {return options_.show_stats;}
5805 
5806   /// Setter of the "show_stats" flag.
5807   ///
5808   /// This flag tells if we should emit statistics about various
5809   /// internal stuff.
5810   ///
5811   /// @param f the value of the flag.
5812   void
show_stats(bool f)5813   show_stats(bool f)
5814   {options_.show_stats = f;}
5815 
5816   /// Getter of the "do_log" flag.
5817   ///
5818   /// This flag tells if we should log about various internal
5819   /// details.
5820   ///
5821   /// return the "do_log" flag.
5822   bool
do_log() const5823   do_log() const
5824   {return options_.do_log;}
5825 
5826   /// Setter of the "do_log" flag.
5827   ///
5828   /// This flag tells if we should log about various internal details.
5829   ///
5830   /// @param f the new value of the flag.
5831   void
do_log(bool f)5832   do_log(bool f)
5833   {options_.do_log = f;}
5834 
5835   /// If a given function decl is suitable for the set of exported
5836   /// functions of the current corpus, this function adds it to that
5837   /// set.
5838   ///
5839   /// @param fn the function to consider for inclusion into the set of
5840   /// exported functions of the current corpus.
5841   void
maybe_add_fn_to_exported_decls(function_decl * fn)5842   maybe_add_fn_to_exported_decls(function_decl* fn)
5843   {
5844     if (fn)
5845       if (corpus::exported_decls_builder* b = exported_decls_builder())
5846 	b->maybe_add_fn_to_exported_fns(fn);
5847   }
5848 
5849   /// If a given variable decl is suitable for the set of exported
5850   /// variables of the current corpus, this variable adds it to that
5851   /// set.
5852   ///
5853   /// @param fn the variable to consider for inclusion into the set of
5854   /// exported variables of the current corpus.
5855   void
maybe_add_var_to_exported_decls(var_decl * var)5856   maybe_add_var_to_exported_decls(var_decl* var)
5857   {
5858     if (var)
5859       if (corpus::exported_decls_builder* b = exported_decls_builder())
5860 	b->maybe_add_var_to_exported_vars(var);
5861   }
5862 
5863   /// Walk the DIEs under a given die and for each child, populate the
5864   /// die -> parent map to record the child -> parent relationship
5865   /// that
5866   /// exists between the child and the given die.
5867   ///
5868   /// The function also builds the vector of places where units are
5869   /// imported.
5870   ///
5871   /// This is done recursively as for each child DIE, this function
5872   /// walks its children as well.
5873   ///
5874   /// @param die the DIE whose children to walk recursively.
5875   ///
5876   /// @param source where the DIE @p die comes from.
5877   ///
5878   /// @param imported_units a vector containing all the offsets of the
5879   /// points where unit have been imported, under @p die.
5880   void
build_die_parent_relations_under(Dwarf_Die * die,die_source source,imported_unit_points_type & imported_units)5881   build_die_parent_relations_under(Dwarf_Die*			die,
5882 				   die_source			source,
5883 				   imported_unit_points_type &	imported_units)
5884   {
5885     if (!die)
5886       return;
5887 
5888     offset_offset_map_type& parent_of = die_parent_map(source);
5889 
5890     Dwarf_Die child;
5891     if (dwarf_child(die, &child) != 0)
5892       return;
5893 
5894     do
5895       {
5896 	parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5897 	if (dwarf_tag(&child) == DW_TAG_imported_unit)
5898 	  {
5899 	    Dwarf_Die imported_unit;
5900 	    if (die_die_attribute(&child, DW_AT_import, imported_unit)
5901 		// If the imported_unit has a sub-tree, let's record
5902 		// this point at which the sub-tree is imported into
5903 		// the current debug info.
5904 		//
5905 		// Otherwise, if the imported_unit has no sub-tree,
5906 		// there is no point in recording where a non-existent
5907 		// sub-tree is being imported.
5908 		//
5909 		// Note that the imported_unit_points_type type below
5910 		// expects the imported_unit to have a sub-tree.
5911 		&& die_has_children(&imported_unit))
5912 	      {
5913 		die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5914 		ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5915 		imported_units.push_back
5916 		  (imported_unit_point(dwarf_dieoffset(&child),
5917 				       imported_unit,
5918 				       imported_unit_die_source));
5919 	      }
5920 	  }
5921 	build_die_parent_relations_under(&child, source, imported_units);
5922       }
5923     while (dwarf_siblingof(&child, &child) == 0);
5924 
5925   }
5926 
5927   /// Determine if we do have to build a DIE -> parent map, depending
5928   /// on a given language.
5929   ///
5930   /// Some languages like C++, Ada etc, do have the concept of
5931   /// namespace and yet, the DIE data structure doesn't provide us
5932   /// with a way to get the parent namespace of a given DIE.  So for
5933   /// those languages, we need to build a DIE -> parent map so that we
5934   /// can get the namespace DIE (or more generally the scope DIE) of a given
5935   /// DIE as we need it.
5936   ///
5937   /// But then some more basic languages like C or assembly don't have
5938   /// that need.
5939   ///
5940   /// This function, depending on the language, tells us if we need to
5941   /// build the DIE -> parent map or not.
5942   ///
5943   /// @param lang the language to consider.
5944   ///
5945   /// @return true iff we need to build the DIE -> parent map for this
5946   /// language.
5947   bool
do_we_build_die_parent_maps(translation_unit::language lang)5948   do_we_build_die_parent_maps(translation_unit::language lang)
5949   {
5950     if (is_c_language(lang))
5951       return false;
5952 
5953     switch (lang)
5954       {
5955       case translation_unit::LANG_UNKNOWN:
5956 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5957       case translation_unit::LANG_Mips_Assembler:
5958 #endif
5959 	return false;
5960       default:
5961 	break;
5962       }
5963     return true;
5964   }
5965 
5966   /// Walk all the DIEs accessible in the debug info (and in the
5967   /// alternate debug info as well) and build maps representing the
5968   /// relationship DIE -> parent.  That is, make it so that we can get
5969   /// the parent for a given DIE.
5970   ///
5971   /// Note that the goal of this map is to be able to get the parent
5972   /// of a given DIE. This is to mainly to handle namespaces.  For instance,
5973   /// when we get a DIE of a type, and we want to build an internal
5974   /// representation for it, we need to get its fully qualified name.
5975   /// For that, we need to know what is the parent DIE of that type
5976   /// DIE, so that we can know what the namespace of that type is.
5977   ///
5978   /// Note that as the C language doesn't have namespaces (all types
5979   /// are defined in the same global namespace), this function doesn't
5980   /// build the DIE -> parent map if the current translation unit
5981   /// comes from C.  This saves time on big C ELF files with a lot of
5982   /// DIEs.
5983   void
build_die_parent_maps()5984   build_die_parent_maps()
5985   {
5986     bool we_do_have_to_build_die_parent_map = false;
5987     uint8_t address_size = 0;
5988     size_t header_size = 0;
5989     // Get the DIE of the current translation unit, look at it to get
5990     // its language. If that language is in C, then all types are in
5991     // the global namespace so we don't need to build the DIE ->
5992     // parent map.  So we dont build it in that case.
5993     for (Dwarf_Off offset = 0, next_offset = 0;
5994 	 (dwarf_next_unit(dwarf(), offset, &next_offset, &header_size,
5995 			  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5996 	 offset = next_offset)
5997       {
5998 	Dwarf_Off die_offset = offset + header_size;
5999 	Dwarf_Die cu;
6000 	if (!dwarf_offdie(dwarf(), die_offset, &cu))
6001 	  continue;
6002 
6003 	uint64_t l = 0;
6004 	die_unsigned_constant_attribute(&cu, DW_AT_language, l);
6005 	translation_unit::language lang = dwarf_language_to_tu_language(l);
6006 	if (do_we_build_die_parent_maps(lang))
6007 	  we_do_have_to_build_die_parent_map = true;
6008       }
6009 
6010     if (!we_do_have_to_build_die_parent_map)
6011       return;
6012 
6013     // Build the DIE -> parent relation for DIEs coming from the
6014     // .debug_info section in the alternate debug info file.
6015     die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
6016     for (Dwarf_Off offset = 0, next_offset = 0;
6017 	 (dwarf_next_unit(alt_dwarf(), offset, &next_offset, &header_size,
6018 			  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
6019 	 offset = next_offset)
6020       {
6021 	Dwarf_Off die_offset = offset + header_size;
6022 	Dwarf_Die cu;
6023 	if (!dwarf_offdie(alt_dwarf(), die_offset, &cu))
6024 	  continue;
6025 	cur_tu_die(&cu);
6026 
6027 	imported_unit_points_type& imported_units =
6028 	  tu_die_imported_unit_points_map(source)[die_offset] =
6029 	  imported_unit_points_type();
6030 	build_die_parent_relations_under(&cu, source, imported_units);
6031       }
6032 
6033     // Build the DIE -> parent relation for DIEs coming from the
6034     // .debug_info section of the main debug info file.
6035     source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
6036     address_size = 0;
6037     header_size = 0;
6038     for (Dwarf_Off offset = 0, next_offset = 0;
6039 	 (dwarf_next_unit(dwarf(), offset, &next_offset, &header_size,
6040 			  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
6041 	 offset = next_offset)
6042       {
6043 	Dwarf_Off die_offset = offset + header_size;
6044 	Dwarf_Die cu;
6045 	if (!dwarf_offdie(dwarf(), die_offset, &cu))
6046 	  continue;
6047 	cur_tu_die(&cu);
6048 	imported_unit_points_type& imported_units =
6049 	  tu_die_imported_unit_points_map(source)[die_offset] =
6050 	  imported_unit_points_type();
6051 	build_die_parent_relations_under(&cu, source, imported_units);
6052       }
6053 
6054     // Build the DIE -> parent relation for DIEs coming from the
6055     // .debug_types section.
6056     source = TYPE_UNIT_DIE_SOURCE;
6057     address_size = 0;
6058     header_size = 0;
6059     uint64_t type_signature = 0;
6060     Dwarf_Off type_offset;
6061     for (Dwarf_Off offset = 0, next_offset = 0;
6062 	 (dwarf_next_unit(dwarf(), offset, &next_offset, &header_size,
6063 			  NULL, NULL, &address_size, NULL,
6064 			  &type_signature, &type_offset) == 0);
6065 	 offset = next_offset)
6066       {
6067 	Dwarf_Off die_offset = offset + header_size;
6068 	Dwarf_Die cu;
6069 
6070 	if (!dwarf_offdie_types(dwarf(), die_offset, &cu))
6071 	  continue;
6072 	cur_tu_die(&cu);
6073 	imported_unit_points_type& imported_units =
6074 	  tu_die_imported_unit_points_map(source)[die_offset] =
6075 	  imported_unit_points_type();
6076 	build_die_parent_relations_under(&cu, source, imported_units);
6077       }
6078   }
6079 };// end class read_context.
6080 
6081 static type_or_decl_base_sptr
6082 build_ir_node_from_die(read_context&	ctxt,
6083 		       Dwarf_Die*	die,
6084 		       scope_decl*	scope,
6085 		       bool		called_from_public_decl,
6086 		       size_t		where_offset,
6087 		       bool		is_declaration_only = true,
6088 		       bool		is_required_decl_spec = false);
6089 
6090 static type_or_decl_base_sptr
6091 build_ir_node_from_die(read_context&	ctxt,
6092 		       Dwarf_Die*	die,
6093 		       bool		called_from_public_decl,
6094 		       size_t		where_offset);
6095 
6096 static class_decl_sptr
6097 add_or_update_class_type(read_context&	 ctxt,
6098 			 Dwarf_Die*	 die,
6099 			 scope_decl*	 scope,
6100 			 bool		 is_struct,
6101 			 class_decl_sptr klass,
6102 			 bool		 called_from_public_decl,
6103 			 size_t		 where_offset,
6104 			 bool		 is_declaration_only);
6105 
6106 static union_decl_sptr
6107 add_or_update_union_type(read_context&	 ctxt,
6108 			 Dwarf_Die*	 die,
6109 			 scope_decl*	 scope,
6110 			 union_decl_sptr union_type,
6111 			 bool		 called_from_public_decl,
6112 			 size_t		 where_offset,
6113 			 bool		 is_declaration_only);
6114 
6115 static decl_base_sptr
6116 build_ir_node_for_void_type(read_context& ctxt);
6117 
6118 static decl_base_sptr
6119 build_ir_node_for_variadic_parameter_type(read_context &ctxt);
6120 
6121 static function_decl_sptr
6122 build_function_decl(read_context&	ctxt,
6123 		    Dwarf_Die*		die,
6124 		    size_t		where_offset,
6125 		    function_decl_sptr	fn);
6126 
6127 static bool
6128 function_is_suppressed(const read_context& ctxt,
6129 		       const scope_decl* scope,
6130 		       Dwarf_Die *function_die,
6131 		       bool is_declaration_only);
6132 
6133 static function_decl_sptr
6134 build_or_get_fn_decl_if_not_suppressed(read_context&	ctxt,
6135 				       scope_decl	*scope,
6136 				       Dwarf_Die	*die,
6137 				       size_t	where_offset,
6138 				       bool is_declaration_only,
6139 				       function_decl_sptr f);
6140 
6141 static var_decl_sptr
6142 build_var_decl(read_context&	ctxt,
6143 	       Dwarf_Die	*die,
6144 	       size_t		where_offset,
6145 	       var_decl_sptr	result = var_decl_sptr());
6146 
6147 static var_decl_sptr
6148 build_or_get_var_decl_if_not_suppressed(read_context&	ctxt,
6149 					scope_decl	*scope,
6150 					Dwarf_Die	*die,
6151 					size_t	where_offset,
6152 					var_decl_sptr	res = var_decl_sptr(),
6153 					bool is_required_decl_spec = false);
6154 static bool
6155 variable_is_suppressed(const read_context& ctxt,
6156 		       const scope_decl* scope,
6157 		       Dwarf_Die *variable_die,
6158 		       bool is_required_decl_spec = false);
6159 
6160 static void
6161 finish_member_function_reading(Dwarf_Die*			die,
6162 			       const function_decl_sptr&	f,
6163 			       const class_or_union_sptr	klass,
6164 			       read_context&			ctxt);
6165 
6166 /// Setter of the debug info root path for a dwarf reader context.
6167 ///
6168 /// @param ctxt the dwarf reader context to consider.
6169 ///
6170 /// @param path the new debug info root path.  This must be a pointer to a
6171 /// character string which life time should be greater than the life
6172 /// time of the read context.
6173 void
set_debug_info_root_path(read_context & ctxt,char ** path)6174 set_debug_info_root_path(read_context& ctxt, char** path)
6175 {ctxt.offline_callbacks()->debuginfo_path = path;}
6176 
6177 /// Setter of the debug info root path for a dwarf reader context.
6178 ///
6179 /// @param ctxt the dwarf reader context to consider.
6180 ///
6181 /// @return a pointer to the debug info root path.
6182 ///
6183 /// time of the read context.
6184 char**
get_debug_info_root_path(read_context & ctxt)6185 get_debug_info_root_path(read_context& ctxt)
6186 {return ctxt.offline_callbacks()->debuginfo_path;}
6187 
6188 /// Getter of the "show_stats" flag.
6189 ///
6190 /// This flag tells if we should emit statistics about various
6191 /// internal stuff.
6192 ///
6193 /// @param ctx the read context to consider for this flag.
6194 ///
6195 /// @return the value of the flag.
6196 bool
get_show_stats(read_context & ctxt)6197 get_show_stats(read_context& ctxt)
6198 {return ctxt.show_stats();}
6199 
6200 /// Setter of the "show_stats" flag.
6201 ///
6202 /// This flag tells if we should emit statistics about various
6203 /// internal stuff.
6204 ///
6205 /// @param ctxt the read context to consider for this flag.
6206 ///
6207 /// @param f the value of the flag.
6208 void
set_show_stats(read_context & ctxt,bool f)6209 set_show_stats(read_context& ctxt, bool f)
6210 {ctxt.show_stats(f);}
6211 
6212 /// Setter of the "drop_undefined_syms" flag.
6213 ///
6214 /// This flag tells if we should drop functions or variables
6215 /// with undefined symbols.
6216 ///
6217 /// @param ctxt the read context to consider for this flag.
6218 ///
6219 /// @param f the value of the flag.
6220 void
set_drop_undefined_syms(read_context & ctxt,bool f)6221 set_drop_undefined_syms(read_context& ctxt, bool f)
6222 {ctxt.drop_undefined_syms(f);}
6223 
6224 /// Setter of the "do_log" flag.
6225 ///
6226 /// This flag tells if we should emit verbose logs for various
6227 /// internal things related to DWARF reading.
6228 ///
6229 /// @param ctxt the DWARF reading context to consider.
6230 ///
6231 /// @param f the new value of the flag.
6232 void
set_do_log(read_context & ctxt,bool f)6233 set_do_log(read_context& ctxt, bool f)
6234 {ctxt.do_log(f);}
6235 
6236 /// Test if a given DIE is anonymous
6237 ///
6238 /// @param die the DIE to consider.
6239 ///
6240 /// @return true iff @p die is anonymous.
6241 static bool
die_is_anonymous(const Dwarf_Die * die)6242 die_is_anonymous(const Dwarf_Die* die)
6243 {
6244   Dwarf_Attribute attr;
6245   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
6246     return true;
6247   return false;
6248 }
6249 
6250 /// Get the value of an attribute that is supposed to be a string, or
6251 /// an empty string if the attribute could not be found.
6252 ///
6253 /// @param die the DIE to get the attribute value from.
6254 ///
6255 /// @param attr_name the attribute name.  Must come from dwarf.h and
6256 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
6257 ///
6258 /// @return the string representing the value of the attribute, or an
6259 /// empty string if no string attribute could be found.
6260 static string
die_string_attribute(const Dwarf_Die * die,unsigned attr_name)6261 die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
6262 {
6263   if (!die)
6264     return "";
6265 
6266   Dwarf_Attribute attr;
6267   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6268     return "";
6269 
6270   const char* str = dwarf_formstring(&attr);
6271   return str ? str : "";
6272 }
6273 
6274 /// Get the value of an attribute that is supposed to be an unsigned
6275 /// constant.
6276 ///
6277 /// @param die the DIE to read the information from.
6278 ///
6279 /// @param attr_name the DW_AT_* name of the attribute.  Must come
6280 /// from dwarf.h and be an enumerator representing an attribute like,
6281 /// e.g, DW_AT_decl_line.
6282 ///
6283 ///@param cst the output parameter that is set to the value of the
6284 /// attribute @p attr_name.  This parameter is set iff the function
6285 /// return true.
6286 ///
6287 /// @return true if there was an attribute of the name @p attr_name
6288 /// and with a value that is a constant, false otherwise.
6289 static bool
die_unsigned_constant_attribute(const Dwarf_Die * die,unsigned attr_name,uint64_t & cst)6290 die_unsigned_constant_attribute(const Dwarf_Die*	die,
6291 				unsigned	attr_name,
6292 				uint64_t&	cst)
6293 {
6294   if (!die)
6295     return false;
6296 
6297   Dwarf_Attribute attr;
6298   Dwarf_Word result = 0;
6299   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6300       || dwarf_formudata(&attr, &result))
6301     return false;
6302 
6303   cst = result;
6304   return true;
6305 }
6306 
6307 /// Read a signed constant value from a given attribute.
6308 ///
6309 /// The signed constant expected must be of constant form.
6310 ///
6311 /// @param die the DIE to get the attribute from.
6312 ///
6313 /// @param attr_name the attribute name.
6314 ///
6315 /// @param cst the resulting signed constant read.
6316 ///
6317 /// @return true iff a signed constant attribute of the name @p
6318 /// attr_name was found on the DIE @p die.
6319 static bool
die_signed_constant_attribute(const Dwarf_Die * die,unsigned attr_name,int64_t & cst)6320 die_signed_constant_attribute(const Dwarf_Die *die,
6321 			      unsigned	attr_name,
6322 			      int64_t&	cst)
6323 {
6324   if (!die)
6325     return false;
6326 
6327   Dwarf_Attribute attr;
6328   Dwarf_Sword result = 0;
6329   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6330       || dwarf_formsdata(&attr, &result))
6331     return false;
6332 
6333   cst = result;
6334   return true;
6335 }
6336 
6337 /// Read the value of a constant attribute that is either signed or
6338 /// unsigned into a array_type_def::subrange_type::bound_value value.
6339 ///
6340 /// The bound_value instance will capture the actual signedness of the
6341 /// read attribute.
6342 ///
6343 /// @param die the DIE from which to read the value of the attribute.
6344 ///
6345 /// @param attr_name the attribute name to consider.
6346 ///
6347 /// @param is_signed true if the attribute value has to read as
6348 /// signed.
6349 ///
6350 /// @param value the resulting value read from attribute @p attr_name
6351 /// on DIE @p die.
6352 ///
6353 /// @return true iff DIE @p die has an attribute named @p attr_name
6354 /// with a constant value.
6355 static bool
die_constant_attribute(const Dwarf_Die * die,unsigned attr_name,bool is_signed,array_type_def::subrange_type::bound_value & value)6356 die_constant_attribute(const Dwarf_Die *die,
6357 		       unsigned attr_name,
6358 		       bool is_signed,
6359 		       array_type_def::subrange_type::bound_value &value)
6360 {
6361   if (!is_signed)
6362     {
6363       uint64_t l = 0;
6364       if (!die_unsigned_constant_attribute(die, attr_name, l))
6365 	return false;
6366       value.set_unsigned(l);
6367     }
6368   else
6369     {
6370       int64_t l = 0;
6371       if (!die_signed_constant_attribute(die, attr_name, l))
6372 	return false;
6373       value.set_signed(l);
6374     }
6375   return true;
6376 }
6377 
6378 /// Test if a given DWARF form is DW_FORM_strx{1,4}.
6379 ///
6380 /// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6381 /// enum in dwarf.h so we have to use an unsigned int for the form,
6382 /// grrr.
6383 ///
6384 /// @param form the form to consider.
6385 ///
6386 /// @return true iff @p form is DW_FORM_strx{1,4}.
6387 static bool
form_is_DW_FORM_strx(unsigned form)6388 form_is_DW_FORM_strx(unsigned form)
6389 {
6390   if (form)
6391     {
6392 #if defined HAVE_DW_FORM_strx1		\
6393   && defined HAVE_DW_FORM_strx2	\
6394   && defined HAVE_DW_FORM_strx3	\
6395   && defined HAVE_DW_FORM_strx4
6396       if (form == DW_FORM_strx1
6397 	  || form == DW_FORM_strx2
6398 	  || form == DW_FORM_strx3
6399 	  ||form == DW_FORM_strx4)
6400 	return true;
6401 #endif
6402     }
6403   return false;
6404 }
6405 
6406 /// Test if a given DWARF form is DW_FORM_line_strp.
6407 ///
6408 /// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6409 /// enum in dwarf.h so we have to use an unsigned int for the form,
6410 /// grrr.
6411 ///
6412 /// @param form the form to consider.
6413 ///
6414 /// @return true iff @p form is DW_FORM_line_strp.
6415 static bool
form_is_DW_FORM_line_strp(unsigned form)6416 form_is_DW_FORM_line_strp(unsigned form)
6417 {
6418   if (form)
6419     {
6420 #if defined HAVE_DW_FORM_line_strp
6421       if (form == DW_FORM_line_strp)
6422 	return true;
6423 #endif
6424     }
6425   return false;
6426 }
6427 
6428 /// Get the value of a DIE attribute; that value is meant to be a
6429 /// flag.
6430 ///
6431 /// @param die the DIE to get the attribute from.
6432 ///
6433 /// @param attr_name the DW_AT_* name of the attribute.  Must come
6434 /// from dwarf.h and be an enumerator representing an attribute like,
6435 /// e.g, DW_AT_external.
6436 ///
6437 /// @param flag the output parameter to store the flag value into.
6438 /// This is set iff the function returns true.
6439 ///
6440 /// @param recursively if true, the function looks through the
6441 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6442 /// all the way down to the initial DIE that is cloned and look on
6443 /// that DIE to see if it has the @p attr_name attribute.
6444 ///
6445 /// @return true if the DIE has a flag attribute named @p attr_name,
6446 /// false otherwise.
6447 static bool
die_flag_attribute(const Dwarf_Die * die,unsigned attr_name,bool & flag,bool recursively=true)6448 die_flag_attribute(const Dwarf_Die* die,
6449 		   unsigned attr_name,
6450 		   bool& flag,
6451 		   bool recursively = true)
6452 {
6453   Dwarf_Attribute attr;
6454   if (recursively
6455       ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6456       : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6457     return false;
6458 
6459   bool f = false;
6460   if (dwarf_formflag(&attr, &f))
6461     return false;
6462 
6463   flag = f;
6464   return true;
6465 }
6466 
6467 /// Get the mangled name from a given DIE.
6468 ///
6469 /// @param die the DIE to read the mangled name from.
6470 ///
6471 /// @return the mangled name if it's present in the DIE, or just an
6472 /// empty string if it's not.
6473 static string
die_linkage_name(const Dwarf_Die * die)6474 die_linkage_name(const Dwarf_Die* die)
6475 {
6476   if (!die)
6477     return "";
6478 
6479   string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6480   if (linkage_name.empty())
6481     linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6482   return linkage_name;
6483 }
6484 
6485 /// Get the file path that is the value of the DW_AT_decl_file
6486 /// attribute on a given DIE, if the DIE is a decl DIE having that
6487 /// attribute.
6488 ///
6489 /// @param die the DIE to consider.
6490 ///
6491 /// @return a string containing the file path that is the logical
6492 /// value of the DW_AT_decl_file attribute.  If the DIE @p die
6493 /// doesn't have a DW_AT_decl_file attribute, then the return value is
6494 /// just an empty string.
6495 static string
die_decl_file_attribute(const Dwarf_Die * die)6496 die_decl_file_attribute(const Dwarf_Die* die)
6497 {
6498   if (!die)
6499     return "";
6500 
6501   const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6502 
6503   return str ? str : "";
6504 }
6505 
6506 /// Get the value of an attribute which value is supposed to be a
6507 /// reference to a DIE.
6508 ///
6509 /// @param die the DIE to read the value from.
6510 ///
6511 /// @param attr_name the DW_AT_* attribute name to read.
6512 ///
6513 /// @param result the DIE resulting from reading the attribute value.
6514 /// This is set iff the function returns true.
6515 ///
6516 /// @param recursively if true, the function looks through the
6517 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6518 /// all the way down to the initial DIE that is cloned and look on
6519 /// that DIE to see if it has the @p attr_name attribute.
6520 ///
6521 /// @return true if the DIE @p die contains an attribute named @p
6522 /// attr_name that is a DIE reference, false otherwise.
6523 static bool
die_die_attribute(const Dwarf_Die * die,unsigned attr_name,Dwarf_Die & result,bool recursively)6524 die_die_attribute(const Dwarf_Die* die,
6525 		  unsigned attr_name,
6526 		  Dwarf_Die& result,
6527 		  bool recursively)
6528 {
6529   Dwarf_Attribute attr;
6530   if (recursively
6531       ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6532       : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6533     return false;
6534 
6535   return dwarf_formref_die(&attr, &result);
6536 }
6537 
6538 /// Read and return an addresss class attribute from a given DIE.
6539 ///
6540 /// @param die the DIE to consider.
6541 ///
6542 /// @param attr_name the name of the address class attribute to read
6543 /// the value from.
6544 ///
6545 /// @param the resulting address.
6546 ///
6547 /// @return true iff the attribute could be read, was of the expected
6548 /// address class and could thus be translated into the @p result.
6549 static bool
die_address_attribute(Dwarf_Die * die,unsigned attr_name,Dwarf_Addr & result)6550 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6551 {
6552   Dwarf_Attribute attr;
6553   if (!dwarf_attr_integrate(die, attr_name, &attr))
6554     return false;
6555   return dwarf_formaddr(&attr, &result) == 0;
6556 }
6557 
6558 /// Returns the source location associated with a decl DIE.
6559 ///
6560 /// @param ctxt the @ref read_context to use.
6561 ///
6562 /// @param die the DIE the read the source location from.
6563 ///
6564 /// @return the location associated with @p die.
6565 static location
die_location(const read_context & ctxt,const Dwarf_Die * die)6566 die_location(const read_context& ctxt, const Dwarf_Die* die)
6567 {
6568   if (!die)
6569     return location();
6570 
6571   string file = die_decl_file_attribute(die);
6572   uint64_t line = 0;
6573   die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6574 
6575   if (!file.empty() && line != 0)
6576     {
6577       translation_unit_sptr tu = ctxt.cur_transl_unit();
6578       location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6579       return l;
6580     }
6581   return location();
6582 }
6583 
6584 /// Return a copy of the name of a DIE.
6585 ///
6586 /// @param die the DIE to consider.
6587 ///
6588 /// @return a copy of the name of the DIE.
6589 static string
die_name(const Dwarf_Die * die)6590 die_name(const Dwarf_Die* die)
6591 {
6592   string name = die_string_attribute(die, DW_AT_name);
6593   return name;
6594 }
6595 
6596 /// Return the location, the name and the mangled name of a given DIE.
6597 ///
6598 /// @param ctxt the read context to use.
6599 ///
6600 /// @param die the DIE to read location and names from.
6601 ///
6602 /// @param loc the location output parameter to set.
6603 ///
6604 /// @param name the name output parameter to set.
6605 ///
6606 /// @param linkage_name the linkage_name output parameter to set.
6607 static void
die_loc_and_name(const read_context & ctxt,Dwarf_Die * die,location & loc,string & name,string & linkage_name)6608 die_loc_and_name(const read_context&	ctxt,
6609 		 Dwarf_Die*		die,
6610 		 location&		loc,
6611 		 string&		name,
6612 		 string&		linkage_name)
6613 {
6614   loc = die_location(ctxt, die);
6615   name = die_name(die);
6616   linkage_name = die_linkage_name(die);
6617 }
6618 
6619 /// Get the size of a (type) DIE as the value for the parameter
6620 /// DW_AT_byte_size or DW_AT_bit_size.
6621 ///
6622 /// @param die the DIE to read the information from.
6623 ///
6624 /// @param size the resulting size in bits.  This is set iff the
6625 /// function return true.
6626 ///
6627 /// @return true if the size attribute was found.
6628 static bool
die_size_in_bits(const Dwarf_Die * die,uint64_t & size)6629 die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6630 {
6631   if (!die)
6632     return false;
6633 
6634   uint64_t byte_size = 0, bit_size = 0;
6635 
6636   if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6637     {
6638       if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6639 	return false;
6640     }
6641   else
6642     bit_size = byte_size * 8;
6643 
6644   size = bit_size;
6645 
6646   return true;
6647 }
6648 
6649 /// Get the access specifier (from the DW_AT_accessibility attribute
6650 /// value) of a given DIE.
6651 ///
6652 /// @param die the DIE to consider.
6653 ///
6654 /// @param access the resulting access.  This is set iff the function
6655 /// returns true.
6656 ///
6657 /// @return bool if the DIE contains the DW_AT_accessibility die.
6658 static bool
die_access_specifier(Dwarf_Die * die,access_specifier & access)6659 die_access_specifier(Dwarf_Die * die, access_specifier& access)
6660 {
6661   if (!die)
6662     return false;
6663 
6664   uint64_t a = 0;
6665   if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6666     return false;
6667 
6668   access_specifier result = private_access;
6669 
6670   switch (a)
6671     {
6672     case private_access:
6673       result = private_access;
6674       break;
6675 
6676     case protected_access:
6677       result = protected_access;
6678       break;
6679 
6680     case public_access:
6681       result = public_access;
6682       break;
6683 
6684     default:
6685       break;
6686     }
6687 
6688   access = result;
6689   return true;
6690 }
6691 
6692 /// Test whether a given DIE represents a decl that is public.  That
6693 /// is, one with the DW_AT_external attribute set.
6694 ///
6695 /// @param die the DIE to consider for testing.
6696 ///
6697 /// @return true if a DW_AT_external attribute is present and its
6698 /// value is set to the true; return false otherwise.
6699 static bool
die_is_public_decl(const Dwarf_Die * die)6700 die_is_public_decl(const Dwarf_Die* die)
6701 {
6702   bool is_public = false;
6703   die_flag_attribute(die, DW_AT_external, is_public);
6704   return is_public;
6705 }
6706 
6707 /// Test if a DIE is effectively public.
6708 ///
6709 /// This is meant to return true when either the DIE is public or when
6710 /// it's a variable DIE that is at (global) namespace level.
6711 ///
6712 /// @return true iff either the DIE is public or is a variable DIE
6713 /// that is at (global) namespace level.
6714 static bool
die_is_effectively_public_decl(const read_context & ctxt,const Dwarf_Die * die)6715 die_is_effectively_public_decl(const read_context& ctxt,
6716 			       const Dwarf_Die* die)
6717 {
6718   if (die_is_public_decl(die))
6719     return true;
6720 
6721   unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6722   if (tag == DW_TAG_variable || tag == DW_TAG_member)
6723     {
6724       // The DIE is a variable.
6725       Dwarf_Die parent_die;
6726       size_t where_offset = 0;
6727       if (!get_parent_die(ctxt, die, parent_die, where_offset))
6728 	return false;
6729 
6730       tag = dwarf_tag(&parent_die);
6731       if (tag == DW_TAG_compile_unit
6732 	  || tag == DW_TAG_partial_unit
6733 	  || tag == DW_TAG_type_unit)
6734 	// The DIE is at global scope.
6735 	return true;
6736 
6737       if (tag == DW_TAG_namespace)
6738 	{
6739 	  string name = die_name(&parent_die);
6740 	  if (name.empty())
6741 	    // The DIE at unnamed namespace scope, so it's not public.
6742 	    return false;
6743 	  // The DIE is at namespace scope.
6744 	  return true;
6745 	}
6746     }
6747   return false;
6748 }
6749 
6750 /// Test whether a given DIE represents a declaration-only DIE.
6751 ///
6752 /// That is, if the DIE has the DW_AT_declaration flag set.
6753 ///
6754 /// @param die the DIE to consider.
6755 //
6756 /// @return true if a DW_AT_declaration is present, false otherwise.
6757 static bool
die_is_declaration_only(Dwarf_Die * die)6758 die_is_declaration_only(Dwarf_Die* die)
6759 {
6760   bool is_declaration_only = false;
6761   die_flag_attribute(die, DW_AT_declaration, is_declaration_only, false);
6762   return is_declaration_only;
6763 }
6764 
6765 /// Tests whether a given DIE is artificial.
6766 ///
6767 /// @param die the test to test for.
6768 ///
6769 /// @return true if the DIE is artificial, false otherwise.
6770 static bool
die_is_artificial(Dwarf_Die * die)6771 die_is_artificial(Dwarf_Die* die)
6772 {
6773   bool is_artificial;
6774   return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6775 }
6776 
6777 ///@return true if a tag represents a type, false otherwise.
6778 ///
6779 ///@param tag the tag to consider.
6780 static bool
is_type_tag(unsigned tag)6781 is_type_tag(unsigned tag)
6782 {
6783   bool result = false;
6784 
6785   switch (tag)
6786     {
6787     case DW_TAG_array_type:
6788     case DW_TAG_class_type:
6789     case DW_TAG_enumeration_type:
6790     case DW_TAG_pointer_type:
6791     case DW_TAG_reference_type:
6792     case DW_TAG_string_type:
6793     case DW_TAG_structure_type:
6794     case DW_TAG_subroutine_type:
6795     case DW_TAG_typedef:
6796     case DW_TAG_union_type:
6797     case DW_TAG_ptr_to_member_type:
6798     case DW_TAG_set_type:
6799     case DW_TAG_subrange_type:
6800     case DW_TAG_base_type:
6801     case DW_TAG_const_type:
6802     case DW_TAG_file_type:
6803     case DW_TAG_packed_type:
6804     case DW_TAG_thrown_type:
6805     case DW_TAG_volatile_type:
6806     case DW_TAG_restrict_type:
6807     case DW_TAG_interface_type:
6808     case DW_TAG_unspecified_type:
6809     case DW_TAG_shared_type:
6810     case DW_TAG_rvalue_reference_type:
6811     case DW_TAG_coarray_type:
6812     case DW_TAG_atomic_type:
6813     case DW_TAG_immutable_type:
6814       result = true;
6815       break;
6816 
6817     default:
6818       result = false;
6819       break;
6820     }
6821 
6822   return result;
6823 }
6824 
6825 /// Test if a given DIE is a type to be canonicalized.  note that a
6826 /// function DIE (DW_TAG_subprogram) is considered to be a
6827 /// canonicalize-able type too because we can consider that DIE as
6828 /// being the type of the function, as well as the function decl
6829 /// itself.
6830 ///
6831 /// @param tag the tag of the DIE to consider.
6832 ///
6833 /// @return true iff the DIE of tag @p tag is a canonicalize-able DIE.
6834 static bool
is_canonicalizeable_type_tag(unsigned tag)6835 is_canonicalizeable_type_tag(unsigned tag)
6836 {
6837   bool result = false;
6838 
6839   switch (tag)
6840     {
6841     case DW_TAG_array_type:
6842     case DW_TAG_class_type:
6843     case DW_TAG_enumeration_type:
6844     case DW_TAG_pointer_type:
6845     case DW_TAG_reference_type:
6846     case DW_TAG_structure_type:
6847     case DW_TAG_subroutine_type:
6848     case DW_TAG_subprogram:
6849     case DW_TAG_typedef:
6850     case DW_TAG_union_type:
6851     case DW_TAG_base_type:
6852     case DW_TAG_const_type:
6853     case DW_TAG_volatile_type:
6854     case DW_TAG_restrict_type:
6855     case DW_TAG_rvalue_reference_type:
6856       result = true;
6857       break;
6858 
6859     default:
6860       result = false;
6861       break;
6862     }
6863 
6864   return result;
6865 }
6866 
6867 /// Test if a DIE tag represents a declaration.
6868 ///
6869 /// @param tag the DWARF tag to consider.
6870 ///
6871 /// @return true iff @p tag is for a declaration.
6872 static bool
is_decl_tag(unsigned tag)6873 is_decl_tag(unsigned tag)
6874 {
6875   switch (tag)
6876     {
6877     case DW_TAG_formal_parameter:
6878     case DW_TAG_imported_declaration:
6879     case DW_TAG_member:
6880     case DW_TAG_unspecified_parameters:
6881     case DW_TAG_subprogram:
6882     case DW_TAG_variable:
6883     case DW_TAG_namespace:
6884     case DW_TAG_GNU_template_template_param:
6885     case DW_TAG_GNU_template_parameter_pack:
6886     case DW_TAG_GNU_formal_parameter_pack:
6887       return true;
6888     }
6889   return false;
6890 }
6891 
6892 /// Test if a DIE represents a type DIE.
6893 ///
6894 /// @param die the DIE to consider.
6895 ///
6896 /// @return true if @p die represents a type, false otherwise.
6897 static bool
die_is_type(const Dwarf_Die * die)6898 die_is_type(const Dwarf_Die* die)
6899 {
6900   if (!die)
6901     return false;
6902   return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6903 }
6904 
6905 /// Test if a DIE represents a declaration.
6906 ///
6907 /// @param die the DIE to consider.
6908 ///
6909 /// @return true if @p die represents a decl, false otherwise.
6910 static bool
die_is_decl(const Dwarf_Die * die)6911 die_is_decl(const Dwarf_Die* die)
6912 {
6913   if (!die)
6914     return false;
6915   return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6916 }
6917 
6918 /// Test if a DIE represents a namespace.
6919 ///
6920 /// @param die the DIE to consider.
6921 ///
6922 /// @return true if @p die represents a namespace, false otherwise.
6923 static bool
die_is_namespace(const Dwarf_Die * die)6924 die_is_namespace(const Dwarf_Die* die)
6925 {
6926   if (!die)
6927     return false;
6928   return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
6929 }
6930 
6931 /// Test if a DIE has tag DW_TAG_unspecified_type.
6932 ///
6933 /// @param die the DIE to consider.
6934 ///
6935 /// @return true if @p die has tag DW_TAG_unspecified_type.
6936 static bool
die_is_unspecified(Dwarf_Die * die)6937 die_is_unspecified(Dwarf_Die* die)
6938 {
6939   if (!die)
6940     return false;
6941   return (dwarf_tag(die) == DW_TAG_unspecified_type);
6942 }
6943 
6944 /// Test if a DIE represents a void type.
6945 ///
6946 /// @param die the DIE to consider.
6947 ///
6948 /// @return true if @p die represents a void type, false otherwise.
6949 static bool
die_is_void_type(Dwarf_Die * die)6950 die_is_void_type(Dwarf_Die* die)
6951 {
6952   if (!die || dwarf_tag(die) != DW_TAG_base_type)
6953     return false;
6954 
6955   string name = die_name(die);
6956   if (name == "void")
6957     return true;
6958 
6959   return false;
6960 }
6961 
6962 /// Test if a DIE represents a pointer type.
6963 ///
6964 /// @param die the die to consider.
6965 ///
6966 /// @return true iff @p die represents a pointer type.
6967 static bool
die_is_pointer_type(const Dwarf_Die * die)6968 die_is_pointer_type(const Dwarf_Die* die)
6969 {
6970   if (!die)
6971     return false;
6972 
6973   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6974   if (tag == DW_TAG_pointer_type)
6975     return true;
6976 
6977   return false;
6978 }
6979 
6980 /// Test if a DIE is for a pointer, reference or qualified type to
6981 /// anonymous class or struct.
6982 ///
6983 /// @param die the DIE to consider.
6984 ///
6985 /// @return true iff @p is for a pointer, reference or qualified type
6986 /// to anonymous class or struct.
6987 static bool
pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die * die)6988 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
6989 {
6990   if (!die_is_pointer_or_reference_type(die)
6991       && !die_is_qualified_type(die))
6992     return false;
6993 
6994   Dwarf_Die underlying_type_die;
6995   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
6996     return false;
6997 
6998   if (!die_is_class_type(&underlying_type_die))
6999     return false;
7000 
7001   string name = die_name(&underlying_type_die);
7002 
7003   return name.empty();
7004 }
7005 
7006 /// Test if a DIE represents a reference type.
7007 ///
7008 /// @param die the die to consider.
7009 ///
7010 /// @return true iff @p die represents a reference type.
7011 static bool
die_is_reference_type(const Dwarf_Die * die)7012 die_is_reference_type(const Dwarf_Die* die)
7013 {
7014   if (!die)
7015     return false;
7016 
7017   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7018   if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7019     return true;
7020 
7021   return false;
7022 }
7023 
7024 /// Test if a DIE represents an array type.
7025 ///
7026 /// @param die the die to consider.
7027 ///
7028 /// @return true iff @p die represents an array type.
7029 static bool
die_is_array_type(const Dwarf_Die * die)7030 die_is_array_type(const Dwarf_Die* die)
7031 {
7032   if (!die)
7033     return false;
7034 
7035   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7036   if (tag == DW_TAG_array_type)
7037     return true;
7038 
7039   return false;
7040 }
7041 
7042 /// Test if a DIE represents a pointer, reference or array type.
7043 ///
7044 /// @param die the die to consider.
7045 ///
7046 /// @return true iff @p die represents a pointer or reference type.
7047 static bool
die_is_pointer_or_reference_type(const Dwarf_Die * die)7048 die_is_pointer_or_reference_type(const Dwarf_Die* die)
7049 {return (die_is_pointer_type(die)
7050 	 || die_is_reference_type(die)
7051 	 || die_is_array_type(die));}
7052 
7053 /// Test if a DIE represents a pointer, a reference or a typedef type.
7054 ///
7055 /// @param die the die to consider.
7056 ///
7057 /// @return true iff @p die represents a pointer, a reference or a
7058 /// typedef type.
7059 static bool
die_is_pointer_reference_or_typedef_type(const Dwarf_Die * die)7060 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7061 {return (die_is_pointer_or_reference_type(die)
7062 	 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7063 
7064 /// Test if a DIE represents a class type.
7065 ///
7066 /// @param die the die to consider.
7067 ///
7068 /// @return true iff @p die represents a class type.
7069 static bool
die_is_class_type(const Dwarf_Die * die)7070 die_is_class_type(const Dwarf_Die* die)
7071 {
7072   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7073 
7074   if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7075     return true;
7076 
7077   return false;
7078 }
7079 
7080 /// Test if a DIE is for a qualified type.
7081 ///
7082 /// @param die the DIE to consider.
7083 ///
7084 /// @return true iff @p die is for a qualified type.
7085 static bool
die_is_qualified_type(const Dwarf_Die * die)7086 die_is_qualified_type(const Dwarf_Die* die)
7087 {
7088   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7089     if (tag == DW_TAG_const_type
7090 	|| tag == DW_TAG_volatile_type
7091 	|| tag == DW_TAG_restrict_type)
7092       return true;
7093 
7094     return false;
7095 }
7096 
7097 /// Test if a DIE is for a function type.
7098 ///
7099 /// @param die the DIE to consider.
7100 ///
7101 /// @return true iff @p die is for a function type.
7102 static bool
die_is_function_type(const Dwarf_Die * die)7103 die_is_function_type(const Dwarf_Die *die)
7104 {
7105   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7106   if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7107     return true;
7108 
7109   return false;
7110 }
7111 
7112 /// Test if a DIE for a function pointer or member function has an
7113 /// DW_AT_object_pointer attribute.
7114 ///
7115 /// @param die the DIE to consider.
7116 ///
7117 /// @param object_pointer out parameter.  It's set to the DIE for the
7118 /// object pointer iff the function returns true.
7119 ///
7120 /// @return true iff the DIE @p die has an object pointer.  In that
7121 /// case, the parameter @p object_pointer is set to the DIE of that
7122 /// object pointer.
7123 static bool
die_has_object_pointer(const Dwarf_Die * die,Dwarf_Die & object_pointer)7124 die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7125 {
7126   if (!die)
7127     return false;
7128 
7129   if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7130     return true;
7131 
7132   return false;
7133 }
7134 
7135 /// Test if a DIE has children DIEs.
7136 ///
7137 /// @param die the DIE to consider.
7138 ///
7139 /// @return true iff @p DIE has at least one child node.
7140 static bool
die_has_children(const Dwarf_Die * die)7141 die_has_children(const Dwarf_Die* die)
7142 {
7143   if (!die)
7144     return false;
7145 
7146   Dwarf_Die child;
7147   if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7148     return true;
7149 
7150   return false;
7151 }
7152 
7153 /// When given the object pointer DIE of a function type or member
7154 /// function DIE, this function returns the "this" pointer that points
7155 /// to the associated class.
7156 ///
7157 /// @param die the DIE of the object pointer of the function or member
7158 /// function to consider.
7159 ///
7160 /// @param this_pointer_die out parameter.  This is set to the DIE of
7161 /// the "this" pointer iff the function returns true.
7162 ///
7163 /// @return true iff the function found the "this" pointer from the
7164 /// object pointer DIE @p die.  In that case, the parameter @p
7165 /// this_pointer_die is set to the DIE of that "this" pointer.
7166 static bool
die_this_pointer_from_object_pointer(Dwarf_Die * die,Dwarf_Die & this_pointer_die)7167 die_this_pointer_from_object_pointer(Dwarf_Die* die,
7168 				     Dwarf_Die& this_pointer_die)
7169 {
7170   ABG_ASSERT(die);
7171   ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7172 
7173   if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7174     return true;
7175 
7176   return false;
7177 }
7178 
7179 /// Test if a given "this" pointer that points to a particular class
7180 /// type is for a const class or not.  If it's for a const class, then
7181 /// it means the function type or the member function associated to
7182 /// that "this" pointer is const.
7183 ///
7184 /// @param die the DIE of the "this" pointer to consider.
7185 ///
7186 /// @return true iff @p die points to a const class type.
7187 static bool
die_this_pointer_is_const(Dwarf_Die * die)7188 die_this_pointer_is_const(Dwarf_Die* die)
7189 {
7190   ABG_ASSERT(die);
7191 
7192   if (dwarf_tag(die) == DW_TAG_pointer_type)
7193     {
7194       Dwarf_Die pointed_to_type_die;
7195       if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7196 	if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7197 	  return true;
7198     }
7199 
7200   return false;
7201 }
7202 
7203 /// Test if an object pointer (referred-to via a DW_AT_object_pointer
7204 /// attribute) points to a const implicit class and so is for a const
7205 /// method or or a const member function type.
7206 ///
7207 /// @param die the DIE of the object pointer to consider.
7208 ///
7209 /// @return true iff the object pointer represented by @p die is for a
7210 /// a const method or const member function type.
7211 static bool
die_object_pointer_is_for_const_method(Dwarf_Die * die)7212 die_object_pointer_is_for_const_method(Dwarf_Die* die)
7213 {
7214   ABG_ASSERT(die);
7215   ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7216 
7217   Dwarf_Die this_pointer_die;
7218   if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7219     if (die_this_pointer_is_const(&this_pointer_die))
7220       return true;
7221 
7222   return false;
7223 }
7224 
7225 /// Test if a DIE represents an entity that is at class scope.
7226 ///
7227 /// @param ctxt the read context to use.
7228 ///
7229 /// @param die the DIE to consider.
7230 ///
7231 /// @param where_offset where we are logically at in the DIE stream.
7232 ///
7233 /// @param class_scope_die out parameter.  Set to the DIE of the
7234 /// containing class iff @p die happens to be at class scope; that is,
7235 /// iff the function returns true.
7236 ///
7237 /// @return true iff @p die is at class scope.  In that case, @p
7238 /// class_scope_die is set to the DIE of the class that contains @p
7239 /// die.
7240 static bool
die_is_at_class_scope(const read_context & ctxt,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & class_scope_die)7241 die_is_at_class_scope(const read_context& ctxt,
7242 		      const Dwarf_Die* die,
7243 		      size_t where_offset,
7244 		      Dwarf_Die& class_scope_die)
7245 {
7246   if (!get_scope_die(ctxt, die, where_offset, class_scope_die))
7247     return false;
7248 
7249   int tag = dwarf_tag(&class_scope_die);
7250 
7251   return (tag == DW_TAG_structure_type
7252 	  || tag == DW_TAG_class_type
7253 	  || tag == DW_TAG_union_type);
7254 }
7255 
7256 /// Return the leaf object under a pointer, reference or qualified
7257 /// type DIE.
7258 ///
7259 /// @param die the DIE of the type to consider.
7260 ///
7261 /// @param peeled_die out parameter.  Set to the DIE of the leaf
7262 /// object iff the function actually peeled anything.
7263 ///
7264 /// @return true upon successful completion.
7265 static bool
die_peel_qual_ptr(Dwarf_Die * die,Dwarf_Die & peeled_die)7266 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7267 {
7268   if (!die)
7269     return false;
7270 
7271   int tag = dwarf_tag(die);
7272 
7273   if (tag == DW_TAG_const_type
7274       || tag == DW_TAG_volatile_type
7275       || tag == DW_TAG_restrict_type
7276       || tag == DW_TAG_pointer_type
7277       || tag == DW_TAG_reference_type
7278       || tag == DW_TAG_rvalue_reference_type)
7279     {
7280       if (!die_die_attribute(die, DW_AT_type, peeled_die))
7281 	return false;
7282     }
7283   else
7284     return false;
7285 
7286   while (tag == DW_TAG_const_type
7287 	 || tag == DW_TAG_volatile_type
7288 	 || tag == DW_TAG_restrict_type
7289 	 || tag == DW_TAG_pointer_type
7290 	 || tag == DW_TAG_reference_type
7291 	 || tag == DW_TAG_rvalue_reference_type)
7292     {
7293       if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7294 	break;
7295       tag = dwarf_tag(&peeled_die);
7296     }
7297 
7298   return true;
7299 }
7300 
7301 /// Return the leaf object under a typedef type DIE.
7302 ///
7303 /// @param die the DIE of the type to consider.
7304 ///
7305 /// @param peeled_die out parameter.  Set to the DIE of the leaf
7306 /// object iff the function actually peeled anything.
7307 ///
7308 /// @return true upon successful completion.
7309 static bool
die_peel_typedef(Dwarf_Die * die,Dwarf_Die & peeled_die)7310 die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7311 {
7312   if (!die)
7313     return false;
7314 
7315   int tag = dwarf_tag(die);
7316 
7317   if (tag == DW_TAG_typedef)
7318     {
7319       if (!die_die_attribute(die, DW_AT_type, peeled_die))
7320 	return false;
7321     }
7322   else
7323     return false;
7324 
7325   while (tag == DW_TAG_typedef)
7326     {
7327       if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7328 	break;
7329       tag = dwarf_tag(&peeled_die);
7330     }
7331 
7332   return true;
7333 
7334 }
7335 
7336 /// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7337 ///
7338 /// @param die the DIE to consider.
7339 ///
7340 /// @param peeled_die the resulting peeled (or leaf) DIE.  This is set
7341 /// iff the function returned true.
7342 ///
7343 /// @return true iff the function could peel @p die.
7344 static bool
die_peel_pointer_and_typedef(const Dwarf_Die * die,Dwarf_Die & peeled_die)7345 die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7346 {
7347   if (!die)
7348     return false;
7349 
7350   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7351 
7352   if (tag == DW_TAG_pointer_type
7353       || tag == DW_TAG_reference_type
7354       || tag == DW_TAG_rvalue_reference_type
7355       || tag == DW_TAG_typedef)
7356     {
7357       if (!die_die_attribute(die, DW_AT_type, peeled_die))
7358 	return false;
7359     }
7360   else
7361     return false;
7362 
7363   while (tag == DW_TAG_pointer_type
7364 	 || tag == DW_TAG_reference_type
7365 	 || tag == DW_TAG_rvalue_reference_type
7366 	 || tag == DW_TAG_typedef)
7367     {
7368       if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7369 	break;
7370       tag = dwarf_tag(&peeled_die);
7371     }
7372   return true;
7373 }
7374 
7375 /// Test if a DIE for a function type represents a method type.
7376 ///
7377 /// @param ctxt the read context.
7378 ///
7379 /// @param die the DIE to consider.
7380 ///
7381 /// @param where_offset where we logically are in the stream of DIEs.
7382 ///
7383 /// @param object_pointer_die out parameter.  This is set by the
7384 /// function to the DIE that refers to the formal function parameter
7385 /// which holds the implicit "this" pointer of the method.  That die
7386 /// is called the object pointer DIE. This is set iff the function
7387 ///
7388 /// @param class_die out parameter.  This is set by the function to
7389 /// the DIE that represents the class of the method type.  This is set
7390 /// iff the function returns true.
7391 ///
7392 /// @param is_static out parameter.  This is set to true by the
7393 /// function if @p die is a static method.  This is set iff the
7394 /// function returns true.
7395 ///
7396 /// @return true iff @p die is a DIE for a method type.
7397 static bool
die_function_type_is_method_type(const read_context & ctxt,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & object_pointer_die,Dwarf_Die & class_die,bool & is_static)7398 die_function_type_is_method_type(const read_context& ctxt,
7399 				 const Dwarf_Die *die,
7400 				 size_t where_offset,
7401 				 Dwarf_Die& object_pointer_die,
7402 				 Dwarf_Die& class_die,
7403 				 bool& is_static)
7404 {
7405   if (!die)
7406     return false;
7407 
7408   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7409   ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7410 
7411   bool has_object_pointer = false;
7412   is_static = false;
7413   if (tag == DW_TAG_subprogram)
7414     {
7415       Dwarf_Die spec_or_origin_die;
7416       if (die_die_attribute(die, DW_AT_specification,
7417 			    spec_or_origin_die)
7418 	  || die_die_attribute(die, DW_AT_abstract_origin,
7419 			       spec_or_origin_die))
7420 	{
7421 	  if (die_has_object_pointer(&spec_or_origin_die,
7422 				     object_pointer_die))
7423 	    has_object_pointer = true;
7424 	  else
7425 	    {
7426 	      if (die_is_at_class_scope(ctxt, &spec_or_origin_die,
7427 					where_offset, class_die))
7428 		is_static = true;
7429 	      else
7430 		return false;
7431 	    }
7432 	}
7433       else
7434 	{
7435 	  if (die_has_object_pointer(die, object_pointer_die))
7436 	    has_object_pointer = true;
7437 	  else
7438 	    {
7439 	      if (die_is_at_class_scope(ctxt, die, where_offset, class_die))
7440 		is_static = true;
7441 	      else
7442 		return false;
7443 	    }
7444 	}
7445     }
7446   else
7447     {
7448       if (die_has_object_pointer(die, object_pointer_die))
7449 	has_object_pointer = true;
7450       else
7451 	return false;
7452     }
7453 
7454   if (!is_static)
7455     {
7456       ABG_ASSERT(has_object_pointer);
7457       // The object pointer die points to a DW_TAG_formal_parameter which
7458       // is the "this" parameter.  The type of the "this" parameter is a
7459       // pointer.  Let's get that pointer type.
7460       Dwarf_Die this_type_die;
7461       if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7462 	return false;
7463 
7464       // So the class type is the type pointed to by the type of the "this"
7465       // parameter.
7466       if (!die_peel_qual_ptr(&this_type_die, class_die))
7467 	return false;
7468 
7469       // And make we return a class type, rather than a typedef to a
7470       // class.
7471       die_peel_typedef(&class_die, class_die);
7472     }
7473 
7474   return true;
7475 }
7476 
7477 enum virtuality
7478 {
7479   VIRTUALITY_NOT_VIRTUAL,
7480   VIRTUALITY_VIRTUAL,
7481   VIRTUALITY_PURE_VIRTUAL
7482 };
7483 
7484 /// Get the virtual-ness of a given DIE, that is, the value of the
7485 /// DW_AT_virtuality attribute.
7486 ///
7487 /// @param die the DIE to read from.
7488 ///
7489 /// @param virt the resulting virtuality attribute.  This is set iff
7490 /// the function returns true.
7491 ///
7492 /// @return true if the virtual-ness could be determined.
7493 static bool
die_virtuality(const Dwarf_Die * die,virtuality & virt)7494 die_virtuality(const Dwarf_Die* die, virtuality& virt)
7495 {
7496   if (!die)
7497     return false;
7498 
7499   uint64_t v = 0;
7500   die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7501 
7502   if (v == DW_VIRTUALITY_virtual)
7503     virt = VIRTUALITY_VIRTUAL;
7504   else if (v == DW_VIRTUALITY_pure_virtual)
7505     virt = VIRTUALITY_PURE_VIRTUAL;
7506   else
7507     virt = VIRTUALITY_NOT_VIRTUAL;
7508 
7509   return true;
7510 }
7511 
7512 /// Test whether the DIE represent either a virtual base or function.
7513 ///
7514 /// @param die the DIE to consider.
7515 ///
7516 /// @return bool if the DIE represents a virtual base or function,
7517 /// false othersise.
7518 static bool
die_is_virtual(const Dwarf_Die * die)7519 die_is_virtual(const Dwarf_Die* die)
7520 {
7521   virtuality v;
7522   if (!die_virtuality(die, v))
7523     return false;
7524 
7525   return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7526 }
7527 
7528 /// Test if the DIE represents an entity that was declared inlined.
7529 ///
7530 /// @param die the DIE to test for.
7531 ///
7532 /// @return true if the DIE represents an entity that was declared
7533 /// inlined.
7534 static bool
die_is_declared_inline(Dwarf_Die * die)7535 die_is_declared_inline(Dwarf_Die* die)
7536 {
7537   uint64_t inline_value = 0;
7538   if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7539     return false;
7540   return inline_value == DW_INL_declared_inlined;
7541 }
7542 
7543 /// This function is a fast routine (optimization) to compare the
7544 /// values of two string attributes of two DIEs.
7545 ///
7546 /// @param l the first DIE to consider.
7547 ///
7548 /// @param r the second DIE to consider.
7549 ///
7550 /// @param attr_name the name of the attribute to compare, on the two
7551 /// DIEs above.
7552 ///
7553 /// @param result out parameter.  This is set to the result of the
7554 /// comparison.  If the value of attribute @p attr_name on DIE @p l
7555 /// equals the value of attribute @p attr_name on DIE @p r, then the
7556 /// the argument of this parameter is set to true.  Otherwise, it's
7557 /// set to false.  Note that the argument of this parameter is set iff
7558 /// the function returned true.
7559 ///
7560 /// @return true iff the comparison could be performed.  There are
7561 /// cases in which the comparison cannot be performed.  For instance,
7562 /// if one of the DIEs does not have the attribute @p attr_name.  In
7563 /// any case, if this function returns true, then the parameter @p
7564 /// result is set to the result of the comparison.
7565 static bool
compare_dies_string_attribute_value(const Dwarf_Die * l,const Dwarf_Die * r,unsigned attr_name,bool & result)7566 compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7567 				    unsigned attr_name,
7568 				    bool &result)
7569 {
7570   Dwarf_Attribute l_attr, r_attr;
7571   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7572       || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7573     return false;
7574 
7575   ABG_ASSERT(l_attr.form == DW_FORM_strp
7576 	     || l_attr.form == DW_FORM_string
7577 	     || l_attr.form == DW_FORM_GNU_strp_alt
7578 	     || form_is_DW_FORM_strx(l_attr.form)
7579 	     || form_is_DW_FORM_line_strp(l_attr.form));
7580 
7581   ABG_ASSERT(r_attr.form == DW_FORM_strp
7582 	     || r_attr.form == DW_FORM_string
7583 	     || r_attr.form == DW_FORM_GNU_strp_alt
7584 	     || form_is_DW_FORM_strx(r_attr.form)
7585 	     || form_is_DW_FORM_line_strp(r_attr.form));
7586 
7587   if ((l_attr.form == DW_FORM_strp
7588        && r_attr.form == DW_FORM_strp)
7589       || (l_attr.form == DW_FORM_GNU_strp_alt
7590 	  && r_attr.form == DW_FORM_GNU_strp_alt)
7591       || (form_is_DW_FORM_strx(l_attr.form)
7592 	  && form_is_DW_FORM_strx(r_attr.form))
7593       || (form_is_DW_FORM_line_strp(l_attr.form)
7594 	  && form_is_DW_FORM_line_strp(r_attr.form)))
7595     {
7596       // So these string attributes are actually pointers into a
7597       // string table.  The string table is most likely de-duplicated
7598       // so comparing the *values* of the pointers should be enough.
7599       //
7600       // This is the fast path.
7601       if (l_attr.valp == r_attr.valp)
7602 	  result = true;
7603       else if (l_attr.valp && r_attr.valp)
7604 	result = *l_attr.valp == *r_attr.valp;
7605       else
7606 	result = false;
7607       return true;
7608     }
7609 
7610   // If we reached this point it means we couldn't use the fast path
7611   // because the string atttributes are strings that are "inline" in
7612   // the debug info section.  Let's just compare them the slow and
7613   // obvious way.
7614   string l_str = die_string_attribute(l, attr_name),
7615     r_str = die_string_attribute(r, attr_name);
7616   result = l_str == r_str;
7617 
7618   return true;
7619 }
7620 
7621 /// Compare the file path of the compilation units (aka CUs)
7622 /// associated to two DIEs.
7623 ///
7624 /// If the DIEs are for pointers or typedefs, this function also
7625 /// compares the file paths of the CUs of the leaf DIEs (underlying
7626 /// DIEs of the pointer or the typedef).
7627 ///
7628 /// @param l the first type DIE to consider.
7629 ///
7630 /// @param r the second type DIE to consider.
7631 ///
7632 /// @return true iff the file paths of the DIEs of the two types are
7633 /// equal.
7634 static bool
compare_dies_cu_decl_file(const Dwarf_Die * l,const Dwarf_Die * r,bool & result)7635 compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7636 {
7637   Dwarf_Die l_cu, r_cu;
7638   if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7639       ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7640     return false;
7641 
7642   bool compared =
7643     compare_dies_string_attribute_value(&l_cu, &r_cu,
7644 					DW_AT_name,
7645 					result);
7646   if (compared)
7647     {
7648       Dwarf_Die peeled_l, peeled_r;
7649       if (die_is_pointer_reference_or_typedef_type(l)
7650 	  && die_is_pointer_reference_or_typedef_type(r)
7651 	  && die_peel_pointer_and_typedef(l, peeled_l)
7652 	  && die_peel_pointer_and_typedef(r, peeled_r))
7653 	{
7654 	  if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7655 	      ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7656 	    return false;
7657 	  compared =
7658 	    compare_dies_string_attribute_value(&l_cu, &r_cu,
7659 						DW_AT_name,
7660 						result);
7661 	}
7662     }
7663 
7664   return  compared;
7665 }
7666 
7667 // -----------------------------------
7668 // <location expression evaluation>
7669 // -----------------------------------
7670 
7671 /// Get the value of a given DIE attribute, knowing that it must be a
7672 /// location expression.
7673 ///
7674 /// @param die the DIE to read the attribute from.
7675 ///
7676 /// @param attr_name the name of the attribute to read the value for.
7677 ///
7678 /// @param expr the pointer to allocate and fill with the resulting
7679 /// array of operators + operands forming a dwarf expression.  This is
7680 /// set iff the function returns true.
7681 ///
7682 /// @param expr_len the length of the resulting dwarf expression.
7683 /// This is set iff the function returns true.
7684 ///
7685 /// @return true if the attribute exists and has a non-empty dwarf expression
7686 /// as value.  In that case the expr and expr_len arguments are set to the
7687 /// resulting dwarf expression.
7688 static bool
die_location_expr(const Dwarf_Die * die,unsigned attr_name,Dwarf_Op ** expr,uint64_t * expr_len)7689 die_location_expr(const Dwarf_Die* die,
7690 		  unsigned attr_name,
7691 		  Dwarf_Op** expr,
7692 		  uint64_t* expr_len)
7693 {
7694   if (!die)
7695     return false;
7696 
7697   Dwarf_Attribute attr;
7698   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7699     return false;
7700 
7701   size_t len = 0;
7702   bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7703 
7704   // Ignore location expressions where reading them succeeded but
7705   // their length is 0.
7706   result &= len > 0;
7707 
7708   if (result)
7709     *expr_len = len;
7710 
7711   return result;
7712 }
7713 
7714 /// If the current operation in the dwarf expression represents a push
7715 /// of a constant value onto the dwarf expr virtual machine (aka
7716 /// DEVM), perform the operation and update the DEVM.
7717 ///
7718 /// If the result of the operation is a constant, update the DEVM
7719 /// accumulator with its value.  Otherwise, the DEVM accumulator is
7720 /// left with its previous value.
7721 ///
7722 /// @param ops the array of the dwarf expression operations to consider.
7723 ///
7724 /// @param ops_len the lengths of @p ops array above.
7725 ///
7726 /// @param index the index of the operation to interpret, in @p ops.
7727 ///
7728 /// @param next_index the index of the operation to interpret at the
7729 /// next step, after this function completed and returned.  This is
7730 /// set an output parameter that is set iff the function returns true.
7731 ///
7732 /// @param ctxt the DEVM evaluation context.
7733 ///
7734 /// @return true if the current operation actually pushes a constant
7735 /// value onto the DEVM stack, false otherwise.
7736 static bool
op_pushes_constant_value(Dwarf_Op * ops,uint64_t ops_len,uint64_t index,uint64_t & next_index,dwarf_expr_eval_context & ctxt)7737 op_pushes_constant_value(Dwarf_Op*			ops,
7738 			 uint64_t			ops_len,
7739 			 uint64_t			index,
7740 			 uint64_t&			next_index,
7741 			 dwarf_expr_eval_context&	ctxt)
7742 {
7743   ABG_ASSERT(index < ops_len);
7744 
7745   Dwarf_Op& op = ops[index];
7746   int64_t value = 0;
7747 
7748   switch (op.atom)
7749     {
7750     case DW_OP_addr:
7751       value = ops[index].number;
7752       break;
7753 
7754     case DW_OP_const1u:
7755     case DW_OP_const1s:
7756     case DW_OP_const2u:
7757     case DW_OP_const2s:
7758     case DW_OP_const4u:
7759     case DW_OP_const4s:
7760     case DW_OP_const8u:
7761     case DW_OP_const8s:
7762     case DW_OP_constu:
7763     case DW_OP_consts:
7764       value = ops[index].number;
7765       break;
7766 
7767     case DW_OP_lit0:
7768       value = 0;
7769       break;
7770     case DW_OP_lit1:
7771       value = 1;
7772       break;
7773     case DW_OP_lit2:
7774       value = 2;
7775       break;
7776     case DW_OP_lit3:
7777       value = 3;
7778       break;
7779     case DW_OP_lit4:
7780       value = 4;
7781       break;
7782     case DW_OP_lit5:
7783       value = 5;
7784       break;
7785     case DW_OP_lit6:
7786       value = 6;
7787       break;
7788     case DW_OP_lit7:
7789       value = 7;
7790       break;
7791     case DW_OP_lit8:
7792       value = 8;
7793       break;
7794     case DW_OP_lit9:
7795       value = 9;
7796       break;
7797     case DW_OP_lit10:
7798       value = 10;
7799       break;
7800     case DW_OP_lit11:
7801       value = 11;
7802       break;
7803     case DW_OP_lit12:
7804       value = 12;
7805       break;
7806     case DW_OP_lit13:
7807       value = 13;
7808       break;
7809     case DW_OP_lit14:
7810       value = 14;
7811       break;
7812     case DW_OP_lit15:
7813       value = 15;
7814       break;
7815     case DW_OP_lit16:
7816       value = 16;
7817       break;
7818     case DW_OP_lit17:
7819       value = 17;
7820       break;
7821     case DW_OP_lit18:
7822       value = 18;
7823       break;
7824     case DW_OP_lit19:
7825       value = 19;
7826       break;
7827     case DW_OP_lit20:
7828       value = 20;
7829       break;
7830     case DW_OP_lit21:
7831       value = 21;
7832       break;
7833     case DW_OP_lit22:
7834       value = 22;
7835       break;
7836     case DW_OP_lit23:
7837       value = 23;
7838       break;
7839     case DW_OP_lit24:
7840       value = 24;
7841       break;
7842     case DW_OP_lit25:
7843       value = 25;
7844       break;
7845     case DW_OP_lit26:
7846       value = 26;
7847       break;
7848     case DW_OP_lit27:
7849       value = 27;
7850       break;
7851     case DW_OP_lit28:
7852       value = 28;
7853       break;
7854     case DW_OP_lit29:
7855       value = 29;
7856       break;
7857     case DW_OP_lit30:
7858       value = 30;
7859       break;
7860     case DW_OP_lit31:
7861       value = 31;
7862       break;
7863 
7864     default:
7865       return false;
7866     }
7867 
7868   expr_result r(value);
7869   ctxt.push(r);
7870   ctxt.accum = r;
7871   next_index = index + 1;
7872 
7873   return true;
7874 }
7875 
7876 /// If the current operation in the dwarf expression represents a push
7877 /// of a non-constant value onto the dwarf expr virtual machine (aka
7878 /// DEVM), perform the operation and update the DEVM.  A non-constant
7879 /// is namely a quantity for which we need inferior (a running program
7880 /// image) state to know the exact value.
7881 ///
7882 /// Upon successful completion, as the result of the operation is a
7883 /// non-constant the DEVM accumulator value is left to its state as of
7884 /// before the invocation of this function.
7885 ///
7886 /// @param ops the array of the dwarf expression operations to consider.
7887 ///
7888 /// @param ops_len the lengths of @p ops array above.
7889 ///
7890 /// @param index the index of the operation to interpret, in @p ops.
7891 ///
7892 /// @param next_index the index of the operation to interpret at the
7893 /// next step, after this function completed and returned.  This is
7894 /// set an output parameter that is set iff the function returns true.
7895 ///
7896 /// @param ctxt the DEVM evaluation context.
7897 ///
7898 /// @return true if the current operation actually pushes a
7899 /// non-constant value onto the DEVM stack, false otherwise.
7900 static bool
op_pushes_non_constant_value(Dwarf_Op * ops,uint64_t ops_len,uint64_t index,uint64_t & next_index,dwarf_expr_eval_context & ctxt)7901 op_pushes_non_constant_value(Dwarf_Op* ops,
7902 			     uint64_t ops_len,
7903 			     uint64_t index,
7904 			     uint64_t& next_index,
7905 			     dwarf_expr_eval_context& ctxt)
7906 {
7907   ABG_ASSERT(index < ops_len);
7908   Dwarf_Op& op = ops[index];
7909 
7910   switch (op.atom)
7911     {
7912     case DW_OP_reg0:
7913     case DW_OP_reg1:
7914     case DW_OP_reg2:
7915     case DW_OP_reg3:
7916     case DW_OP_reg4:
7917     case DW_OP_reg5:
7918     case DW_OP_reg6:
7919     case DW_OP_reg7:
7920     case DW_OP_reg8:
7921     case DW_OP_reg9:
7922     case DW_OP_reg10:
7923     case DW_OP_reg11:
7924     case DW_OP_reg12:
7925     case DW_OP_reg13:
7926     case DW_OP_reg14:
7927     case DW_OP_reg15:
7928     case DW_OP_reg16:
7929     case DW_OP_reg17:
7930     case DW_OP_reg18:
7931     case DW_OP_reg19:
7932     case DW_OP_reg20:
7933     case DW_OP_reg21:
7934     case DW_OP_reg22:
7935     case DW_OP_reg23:
7936     case DW_OP_reg24:
7937     case DW_OP_reg25:
7938     case DW_OP_reg26:
7939     case DW_OP_reg27:
7940     case DW_OP_reg28:
7941     case DW_OP_reg29:
7942     case DW_OP_reg30:
7943     case DW_OP_reg31:
7944       next_index = index + 1;
7945       break;
7946 
7947     case DW_OP_breg0:
7948     case DW_OP_breg1:
7949     case DW_OP_breg2:
7950     case DW_OP_breg3:
7951     case DW_OP_breg4:
7952     case DW_OP_breg5:
7953     case DW_OP_breg6:
7954     case DW_OP_breg7:
7955     case DW_OP_breg8:
7956     case DW_OP_breg9:
7957     case DW_OP_breg10:
7958     case DW_OP_breg11:
7959     case DW_OP_breg12:
7960     case DW_OP_breg13:
7961     case DW_OP_breg14:
7962     case DW_OP_breg15:
7963     case DW_OP_breg16:
7964     case DW_OP_breg17:
7965     case DW_OP_breg18:
7966     case DW_OP_breg19:
7967     case DW_OP_breg20:
7968     case DW_OP_breg21:
7969     case DW_OP_breg22:
7970     case DW_OP_breg23:
7971     case DW_OP_breg24:
7972     case DW_OP_breg25:
7973     case DW_OP_breg26:
7974     case DW_OP_breg27:
7975     case DW_OP_breg28:
7976     case DW_OP_breg29:
7977     case DW_OP_breg30:
7978     case DW_OP_breg31:
7979       next_index = index + 1;
7980       break;
7981 
7982     case DW_OP_regx:
7983       next_index = index + 2;
7984       break;
7985 
7986     case DW_OP_fbreg:
7987       next_index = index + 1;
7988       break;
7989 
7990     case DW_OP_bregx:
7991       next_index = index + 1;
7992       break;
7993 
7994     default:
7995       return false;
7996     }
7997 
7998   expr_result r(false);
7999   ctxt.push(r);
8000 
8001   return true;
8002 }
8003 
8004 /// If the current operation in the dwarf expression represents a
8005 /// manipulation of the stack of the DWARF Expression Virtual Machine
8006 /// (aka DEVM), this function performs the operation and updates the
8007 /// state of the DEVM.  If the result of the operation represents a
8008 /// constant value, then the accumulator of the DEVM is set to that
8009 /// result's value, Otherwise, the DEVM accumulator is left with its
8010 /// previous value.
8011 ///
8012 /// @param expr the array of the dwarf expression operations to consider.
8013 ///
8014 /// @param expr_len the lengths of @p ops array above.
8015 ///
8016 /// @param index the index of the operation to interpret, in @p ops.
8017 ///
8018 /// @param next_index the index of the operation to interpret at the
8019 /// next step, after this function completed and returned.  This is
8020 /// set an output parameter that is set iff the function returns true.
8021 ///
8022 /// @param ctxt the DEVM evaluation context.
8023 ///
8024 /// @return true if the current operation actually manipulates the
8025 /// DEVM stack, false otherwise.
8026 static bool
op_manipulates_stack(Dwarf_Op * expr,uint64_t expr_len,uint64_t index,uint64_t & next_index,dwarf_expr_eval_context & ctxt)8027 op_manipulates_stack(Dwarf_Op* expr,
8028 		     uint64_t expr_len,
8029 		     uint64_t index,
8030 		     uint64_t& next_index,
8031 		     dwarf_expr_eval_context& ctxt)
8032 {
8033   Dwarf_Op& op = expr[index];
8034   expr_result v;
8035 
8036   switch (op.atom)
8037     {
8038     case DW_OP_dup:
8039       v = ctxt.stack.front();
8040       ctxt.push(v);
8041       break;
8042 
8043     case DW_OP_drop:
8044       v = ctxt.stack.front();
8045       ctxt.pop();
8046       break;
8047 
8048     case DW_OP_over:
8049       ABG_ASSERT(ctxt.stack.size() > 1);
8050       v = ctxt.stack[1];
8051       ctxt.push(v);
8052       break;
8053 
8054     case DW_OP_pick:
8055       ABG_ASSERT(index + 1 < expr_len);
8056       v = op.number;
8057       ctxt.push(v);
8058       break;
8059 
8060     case DW_OP_swap:
8061       ABG_ASSERT(ctxt.stack.size() > 1);
8062       v = ctxt.stack[1];
8063       ctxt.stack.erase(ctxt.stack.begin() + 1);
8064       ctxt.push(v);
8065       break;
8066 
8067     case DW_OP_rot:
8068       ABG_ASSERT(ctxt.stack.size() > 2);
8069       v = ctxt.stack[2];
8070       ctxt.stack.erase(ctxt.stack.begin() + 2);
8071       ctxt.push(v);
8072       break;
8073 
8074     case DW_OP_deref:
8075     case DW_OP_deref_size:
8076       ABG_ASSERT(ctxt.stack.size() > 0);
8077       ctxt.pop();
8078       v.is_const(false);
8079       ctxt.push(v);
8080       break;
8081 
8082     case DW_OP_xderef:
8083     case DW_OP_xderef_size:
8084       ABG_ASSERT(ctxt.stack.size() > 1);
8085       ctxt.pop();
8086       ctxt.pop();
8087       v.is_const(false);
8088       ctxt.push(v);
8089       break;
8090 
8091     case DW_OP_push_object_address:
8092       v.is_const(false);
8093       ctxt.push(v);
8094       break;
8095 
8096     case DW_OP_form_tls_address:
8097     case DW_OP_GNU_push_tls_address:
8098       ABG_ASSERT(ctxt.stack.size() > 0);
8099       v = ctxt.pop();
8100       if (op.atom == DW_OP_form_tls_address)
8101 	v.is_const(false);
8102       ctxt.push(v);
8103       break;
8104 
8105     case DW_OP_call_frame_cfa:
8106       v.is_const(false);
8107       ctxt.push(v);
8108       break;
8109 
8110     default:
8111       return false;
8112     }
8113 
8114   if (v.is_const())
8115     ctxt.accum = v;
8116 
8117   if (op.atom == DW_OP_form_tls_address
8118       || op.atom == DW_OP_GNU_push_tls_address)
8119     ctxt.set_tls_address(true);
8120   else
8121     ctxt.set_tls_address(false);
8122 
8123   next_index = index + 1;
8124 
8125   return true;
8126 }
8127 
8128 /// If the current operation in the dwarf expression represents a push
8129 /// of an arithmetic or logic operation onto the dwarf expr virtual
8130 /// machine (aka DEVM), perform the operation and update the DEVM.
8131 ///
8132 /// If the result of the operation is a constant, update the DEVM
8133 /// accumulator with its value.  Otherwise, the DEVM accumulator is
8134 /// left with its previous value.
8135 ///
8136 /// @param expr the array of the dwarf expression operations to consider.
8137 ///
8138 /// @param expr_len the lengths of @p expr array above.
8139 ///
8140 /// @param index the index of the operation to interpret, in @p expr.
8141 ///
8142 /// @param next_index the index of the operation to interpret at the
8143 /// next step, after this function completed and returned.  This is
8144 /// set an output parameter that is set iff the function returns true.
8145 ///
8146 /// @param ctxt the DEVM evaluation context.
8147 ///
8148 /// @return true if the current operation actually represent an
8149 /// arithmetic or logic operation.
8150 static bool
op_is_arith_logic(Dwarf_Op * expr,uint64_t expr_len,uint64_t index,uint64_t & next_index,dwarf_expr_eval_context & ctxt)8151 op_is_arith_logic(Dwarf_Op* expr,
8152 		  uint64_t expr_len,
8153 		  uint64_t index,
8154 		  uint64_t& next_index,
8155 		  dwarf_expr_eval_context& ctxt)
8156 {
8157   ABG_ASSERT(index < expr_len);
8158 
8159   Dwarf_Op& op = expr[index];
8160   expr_result val1, val2;
8161 
8162   switch (op.atom)
8163     {
8164     case DW_OP_abs:
8165       val1 = ctxt.pop();
8166       val1 = val1.abs();
8167       ctxt.push(val1);
8168       break;
8169 
8170     case DW_OP_and:
8171       ABG_ASSERT(ctxt.stack.size() > 1);
8172       val1 = ctxt.pop();
8173       val2 = ctxt.pop();
8174       ctxt.push(val1 & val2);
8175       break;
8176 
8177     case DW_OP_div:
8178       val1 = ctxt.pop();
8179       val2 = ctxt.pop();
8180       if (!val1.is_const())
8181 	val1 = 1;
8182       ctxt.push(val2 / val1);
8183       break;
8184 
8185     case DW_OP_minus:
8186       val1 = ctxt.pop();
8187       val2 = ctxt.pop();
8188       ctxt.push(val2 - val1);
8189       break;
8190 
8191     case DW_OP_mod:
8192       val1 = ctxt.pop();
8193       val2 = ctxt.pop();
8194       ctxt.push(val2 % val1);
8195       break;
8196 
8197     case DW_OP_mul:
8198       val1 = ctxt.pop();
8199       val2 = ctxt.pop();
8200       ctxt.push(val2 * val1);
8201       break;
8202 
8203     case DW_OP_neg:
8204       val1 = ctxt.pop();
8205       ctxt.push(-val1);
8206       break;
8207 
8208     case DW_OP_not:
8209       val1 = ctxt.pop();
8210       ctxt.push(~val1);
8211       break;
8212 
8213     case DW_OP_or:
8214       val1 = ctxt.pop();
8215       val2 = ctxt.pop();
8216       ctxt.push(val1 | val2);
8217       break;
8218 
8219     case DW_OP_plus:
8220       val1 = ctxt.pop();
8221       val2 = ctxt.pop();
8222       ctxt.push(val2 + val1);
8223       break;
8224 
8225     case DW_OP_plus_uconst:
8226       val1 = ctxt.pop();
8227       val1 += op.number;
8228       ctxt.push(val1);
8229       break;
8230 
8231     case DW_OP_shl:
8232       val1 = ctxt.pop();
8233       val2 = ctxt.pop();
8234       ctxt.push(val2 << val1);
8235       break;
8236 
8237     case DW_OP_shr:
8238     case DW_OP_shra:
8239       val1 = ctxt.pop();
8240       val2 = ctxt.pop();
8241       ctxt.push(val2 >> val1);
8242       break;
8243 
8244     case DW_OP_xor:
8245       val1 = ctxt.pop();
8246       val2 = ctxt.pop();
8247       ctxt.push(val2 ^ val1);
8248       break;
8249 
8250     default:
8251       return false;
8252     }
8253 
8254   if (ctxt.stack.front().is_const())
8255     ctxt.accum = ctxt.stack.front();
8256 
8257   next_index = index + 1;
8258   return true;
8259 }
8260 
8261 /// If the current operation in the dwarf expression represents a push
8262 /// of a control flow operation onto the dwarf expr virtual machine
8263 /// (aka DEVM), perform the operation and update the DEVM.
8264 ///
8265 /// If the result of the operation is a constant, update the DEVM
8266 /// accumulator with its value.  Otherwise, the DEVM accumulator is
8267 /// left with its previous value.
8268 ///
8269 /// @param expr the array of the dwarf expression operations to consider.
8270 ///
8271 /// @param expr_len the lengths of @p expr array above.
8272 ///
8273 /// @param index the index of the operation to interpret, in @p expr.
8274 ///
8275 /// @param next_index the index of the operation to interpret at the
8276 /// next step, after this function completed and returned.  This is
8277 /// set an output parameter that is set iff the function returns true.
8278 ///
8279 /// @param ctxt the DEVM evaluation context.
8280 ///
8281 /// @return true if the current operation actually represents a
8282 /// control flow operation, false otherwise.
8283 static bool
op_is_control_flow(Dwarf_Op * expr,uint64_t expr_len,uint64_t index,uint64_t & next_index,dwarf_expr_eval_context & ctxt)8284 op_is_control_flow(Dwarf_Op* expr,
8285 		   uint64_t expr_len,
8286 		   uint64_t index,
8287 		   uint64_t& next_index,
8288 		   dwarf_expr_eval_context& ctxt)
8289 {
8290   ABG_ASSERT(index < expr_len);
8291 
8292   Dwarf_Op& op = expr[index];
8293   expr_result val1, val2;
8294 
8295   switch (op.atom)
8296     {
8297     case DW_OP_eq:
8298     case DW_OP_ge:
8299     case DW_OP_gt:
8300     case DW_OP_le:
8301     case DW_OP_lt:
8302     case DW_OP_ne:
8303       {
8304 	bool value = true;
8305 	val1 = ctxt.pop();
8306 	val2 = ctxt.pop();
8307 	if (op.atom == DW_OP_eq)
8308 	  value = val2 == val1;
8309 	else if (op.atom == DW_OP_ge)
8310 	  value = val2 >= val1;
8311 	else if (op.atom == DW_OP_gt)
8312 	  value = val2 > val1;
8313 	else if (op.atom == DW_OP_le)
8314 	  value = val2 <= val1;
8315 	else if (op.atom == DW_OP_lt)
8316 	  value = val2 < val1;
8317 	else if (op.atom == DW_OP_ne)
8318 	  value = val2 != val1;
8319 
8320 	val1 = value ? 1 : 0;
8321 	ctxt.push(val1);
8322       }
8323       break;
8324 
8325     case DW_OP_skip:
8326       if (op.number > 0)
8327 	index += op.number - 1;
8328       break;
8329 
8330     case DW_OP_bra:
8331       val1 = ctxt.pop();
8332       if (val1 != 0)
8333 	index += val1.const_value() - 1;
8334       break;
8335 
8336     case DW_OP_call2:
8337     case DW_OP_call4:
8338     case DW_OP_call_ref:
8339     case DW_OP_nop:
8340       break;
8341 
8342     default:
8343       return false;
8344     }
8345 
8346   if (ctxt.stack.front().is_const())
8347     ctxt.accum = ctxt.stack.front();
8348 
8349   next_index = index + 1;
8350   return true;
8351 }
8352 
8353 /// This function quickly evaluates a DWARF expression that is a
8354 /// constant.
8355 ///
8356 /// This is a "fast path" function that quickly evaluates a DWARF
8357 /// expression that is only made of a DW_OP_plus_uconst operator.
8358 ///
8359 /// This is a sub-routine of die_member_offset.
8360 ///
8361 /// @param expr the DWARF expression to evaluate.
8362 ///
8363 /// @param expr_len the length of the expression @p expr.
8364 ///
8365 /// @param value out parameter.  This is set to the result of the
8366 /// evaluation of @p expr, iff this function returns true.
8367 ///
8368 /// @return true iff the evaluation of @p expr went OK.
8369 static bool
eval_quickly(Dwarf_Op * expr,uint64_t expr_len,int64_t & value)8370 eval_quickly(Dwarf_Op*	expr,
8371 	     uint64_t	expr_len,
8372 	     int64_t&	value)
8373 {
8374   if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8375     {
8376       value = expr[0].number;
8377       return true;
8378     }
8379   return false;
8380 }
8381 
8382 /// Evaluate the value of the last sub-expression that is a constant,
8383 /// inside a given DWARF expression.
8384 ///
8385 /// @param expr the DWARF expression to consider.
8386 ///
8387 /// @param expr_len the length of the expression to consider.
8388 ///
8389 /// @param value the resulting value of the last constant
8390 /// sub-expression of the DWARF expression.  This is set iff the
8391 /// function returns true.
8392 ///
8393 /// @param is_tls_address out parameter.  This is set to true iff
8394 /// the resulting value of the evaluation is a TLS (thread local
8395 /// storage) address.
8396 ///
8397 /// @param eval_ctxt the evaluation context to (re)use.  Note that
8398 /// this function initializes this context before using it.
8399 ///
8400 /// @return true if the function could find a constant sub-expression
8401 /// to evaluate, false otherwise.
8402 static bool
eval_last_constant_dwarf_sub_expr(Dwarf_Op * expr,uint64_t expr_len,int64_t & value,bool & is_tls_address,dwarf_expr_eval_context & eval_ctxt)8403 eval_last_constant_dwarf_sub_expr(Dwarf_Op*	expr,
8404 				  uint64_t	expr_len,
8405 				  int64_t&	value,
8406 				  bool&	is_tls_address,
8407 				  dwarf_expr_eval_context &eval_ctxt)
8408 {
8409   // Reset the evaluation context before evaluating the constant sub
8410   // expression contained in the DWARF expression 'expr'.
8411   eval_ctxt.reset();
8412 
8413   uint64_t index = 0, next_index = 0;
8414   do
8415     {
8416       if (op_is_arith_logic(expr, expr_len, index,
8417 			    next_index, eval_ctxt)
8418 	  || op_pushes_constant_value(expr, expr_len, index,
8419 				      next_index, eval_ctxt)
8420 	  || op_manipulates_stack(expr, expr_len, index,
8421 				  next_index, eval_ctxt)
8422 	  || op_pushes_non_constant_value(expr, expr_len, index,
8423 					  next_index, eval_ctxt)
8424 	  || op_is_control_flow(expr, expr_len, index,
8425 				next_index, eval_ctxt))
8426 	;
8427       else
8428 	next_index = index + 1;
8429 
8430       ABG_ASSERT(next_index > index);
8431       index = next_index;
8432     } while (index < expr_len);
8433 
8434   is_tls_address = eval_ctxt.set_tls_address();
8435   if (eval_ctxt.accum.is_const())
8436     {
8437       value = eval_ctxt.accum;
8438       return true;
8439     }
8440   return false;
8441 }
8442 
8443 /// Evaluate the value of the last sub-expression that is a constant,
8444 /// inside a given DWARF expression.
8445 ///
8446 /// @param expr the DWARF expression to consider.
8447 ///
8448 /// @param expr_len the length of the expression to consider.
8449 ///
8450 /// @param value the resulting value of the last constant
8451 /// sub-expression of the DWARF expression.  This is set iff the
8452 /// function returns true.
8453 ///
8454 /// @return true if the function could find a constant sub-expression
8455 /// to evaluate, false otherwise.
8456 static bool
eval_last_constant_dwarf_sub_expr(Dwarf_Op * expr,uint64_t expr_len,int64_t & value,bool & is_tls_address)8457 eval_last_constant_dwarf_sub_expr(Dwarf_Op*	expr,
8458 				  uint64_t	expr_len,
8459 				  int64_t&	value,
8460 				  bool&	is_tls_address)
8461 {
8462   dwarf_expr_eval_context eval_ctxt;
8463   return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8464 					   is_tls_address, eval_ctxt);
8465 }
8466 
8467 // -----------------------------------
8468 // </location expression evaluation>
8469 // -----------------------------------
8470 
8471 /// Convert a DW_AT_bit_offset attribute value into the same value as
8472 /// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8473 ///
8474 /// On big endian machines, the value of the DW_AT_bit_offset
8475 /// attribute + 8 * the value of the DW_AT_data_member_location
8476 /// attribute is the same as the value of the DW_AT_data_bit_offset
8477 /// attribute.
8478 ///
8479 /// On little endian machines however, the situation is different.
8480 /// The DW_AT_bit_offset value for a bit field is the number of bits
8481 /// to the left of the most significant bit of the bit field, within
8482 /// the integer value at DW_AT_data_member_location.
8483 ///
8484 /// The DW_AT_data_bit_offset offset value is the number of bits to
8485 /// the right of the least significant bit of the bit field, again
8486 /// relative to the containing integer value.
8487 ///
8488 /// In other words, DW_AT_data_bit_offset is what everybody would
8489 /// instinctively think of as being the "offset of the bit field". 8 *
8490 /// DW_AT_data_member_location + DW_AT_bit_offset however is very
8491 /// counter-intuitive on little endian machines.
8492 ///
8493 /// This function thus reads the value of a DW_AT_bit_offset property
8494 /// of a DIE and converts it into what the DW_AT_data_bit_offset would
8495 /// have been if it was present, ignoring the contribution of
8496 /// DW_AT_data_member_location.
8497 ///
8498 /// Note that DW_AT_bit_offset has been made obsolete starting from
8499 /// DWARF5 (for GCC; Clang still emits it).
8500 ///
8501 /// If you like coffee and it's not too late, now might be a good time
8502 /// to have a coffee break.  Otherwise if it's late at night, you
8503 /// might want to consider an herbal tea break.  Then come back to
8504 /// read this.
8505 ///
8506 ///
8507 /// In what follows, the bit fields are all contained within the first
8508 /// whole int of the struct, so DW_AT_data_member_location is 0.
8509 ///
8510 /// Okay, to have a better idea of what DW_AT_bit_offset and
8511 /// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8512 /// have bit fields data members defined as:
8513 ///
8514 ///      struct S
8515 ///      {
8516 ///        int j:5;
8517 ///        int k:6;
8518 ///        int m:5;
8519 ///        int n:8;
8520 ///      };
8521 ///
8522 /// The below wonderful (at least!) ASCII art sketch describes the
8523 /// layout of the bitfields of 'struct S' on a little endian machine.
8524 /// You need to read the sketch from the bottom-up.
8525 ///
8526 /// So please scroll down to its bottom.  Note how the 32 bits integer
8527 /// word containing the bit fields is laid out with its least
8528 /// significant bit starting on the right hand side, at index 0.
8529 ///
8530 /// Then slowly scroll up starting from there, and take the time to
8531 /// read each line and see how the bit fields are laid out and what
8532 /// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8533 /// the bit fields.
8534 ///
8535 /// DW_AT_bit_offset(n)
8536 /// <   - - - - - - >
8537 /// |               |       n      |
8538 /// ^               ^< - -   - -  >^
8539 ///                                           DW_AT_data_bit_offset(n)
8540 ///                                <  - - - - -  - - - - - - - - - - >
8541 ///                                |                                 |
8542 ///                                ^                                 ^
8543 ///                 DW_AT_bit_offset(m)
8544 /// <--------------------------------->
8545 /// |                                 |   m   |
8546 /// ^                                 ^<  -  >^
8547 ///                                           DW_AT_data_bit_offset(m)
8548 ///                                           <  - - - - - - - - - - >
8549 ///                                           |                      |
8550 ///                                           ^                      ^
8551 ///                           DW_AT_bit_offset(k)
8552 /// <-------------------------------------------->
8553 /// |                                            |    k    |
8554 /// ^                                            ^<  - -  >^
8555 ///                                                     DW_AT_data_bit_offset(k)
8556 ///                                                        < - - - - >
8557 ///                                                        |         |
8558 ///                                                        ^         ^
8559 ///                                      DW_AT_bit_offset(j)
8560 /// <-------------------------------------------------------->
8561 /// |                                                        |
8562 /// ^                                                        ^
8563 ///                       n               m          k          j
8564 ///                 <  - - - - - - >  < - - - >  < - - - - > < - - - >
8565 ///
8566 /// | | | | | | | | |  | | | | | | |  | | | | |  | | | | | | | | | | |
8567 /// ^       ^       ^              ^  ^       ^  ^       ^ ^ ^       ^
8568 /// 31      27      23             16 15      11 10      6 5 4       0
8569 ///
8570 /// So, the different bit fields all fit in one 32 bits word, assuming
8571 /// the bit fields are tightly packed.
8572 ///
8573 /// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8574 /// on this little endian machine and let's see how it relates to
8575 /// DW_AT_data_bit_offset of j.
8576 ///
8577 /// DW_AT_bit_offset(j) would be equal to the number of bits from the
8578 /// left of the 32 bits word (i.e from bit number 31) to the most
8579 /// significant bit of the j bit field (i.e, bit number 4).  Thus:
8580 ///
8581 ///       DW_AT_bit_offset(j) =
8582 ///         sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8583 ///
8584 /// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8585 /// 32 bits word (i.e, bit number 0) to the lest significant bit of
8586 /// the 'j' bit field (ie, bit number 0).  Thus:
8587 ///
8588 ///       DW_AT_data_bit_offset(j) = 0.
8589 ///
8590 /// More generally, we can notice that:
8591 ///
8592 ///       sizeof_in_bits(int) =
8593 ///         DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8594 ///
8595 /// It follows that:
8596 ///
8597 ///       DW_AT_data_bit_offset(j) =
8598 ///          sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8599 ///
8600 /// Thus:
8601 ///
8602 ///       DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8603 ///
8604 /// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8605 /// from the right hand side of the word.  It is what we would
8606 /// intuitively think it is.  DW_AT_bit_offset however is super
8607 /// counter-intuitive, pfff.
8608 ///
8609 /// Anyway, this general equation holds true for all bit fields.
8610 ///
8611 /// Similarly, it follows that:
8612 ///
8613 ///       DW_AT_bit_offset(k) =
8614 ///         sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8615 ///
8616 /// Thus:
8617 ///       DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8618 ///
8619 ///
8620 /// Likewise:
8621 ///
8622 ///      DW_AT_bit_offset(m) =
8623 ///        sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8624 ///
8625 ///
8626 /// Thus:
8627 ///      DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8628 ///
8629 /// And:
8630 ///
8631 ///
8632 /// Lastly:
8633 ///
8634 ///      DW_AT_bit_offset(n) =
8635 ///        sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8636 ///
8637 /// Thus:
8638 ///      DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8639 ///
8640 /// Luckily, the body of the function is much smaller than this
8641 /// comment.  Enjoy!
8642 ///
8643 /// @param die the DIE to consider.
8644 ///
8645 /// @param is_big_endian this is true iff the machine we are looking at
8646 /// is big endian.
8647 ///
8648 /// @param offset this is the output parameter into which the value of
8649 /// the DW_AT_bit_offset is put, converted as if it was the value of
8650 /// the DW_AT_data_bit_offset parameter, less the contribution of
8651 /// DW_AT_data_member_location.  This parameter is set iff the
8652 /// function returns true.
8653 ///
8654 /// @return true if DW_AT_bit_offset was found on @p die.
8655 static bool
read_and_convert_DW_at_bit_offset(const Dwarf_Die * die,bool is_big_endian,uint64_t & offset)8656 read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8657 				  bool is_big_endian,
8658 				  uint64_t &offset)
8659 {
8660   uint64_t off = 0;
8661   if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8662     return false;
8663 
8664   if (is_big_endian)
8665     {
8666       offset = off;
8667       return true;
8668     }
8669 
8670   // Okay, we are looking at a little endian machine.  We need to
8671   // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8672   // have been.  To understand this, you really need to read the
8673   // preliminary comment of this function.
8674   uint64_t containing_anonymous_object_size = 0;
8675   ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8676 					     containing_anonymous_object_size));
8677   containing_anonymous_object_size *= 8;
8678 
8679   uint64_t bitfield_size = 0;
8680   ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8681 					     bitfield_size));
8682 
8683   // As noted in the the preliminary comment of this function if we
8684   // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8685   // its DW_AT_bit_offset value, the equation is:
8686   //
8687   //     DW_AT_data_bit_offset(k) =
8688   //       sizeof_in_bits(containing_anonymous_object_size)
8689   //       - DW_AT_data_bit_offset(k)
8690   //       - sizeof_in_bits(k)
8691   offset = containing_anonymous_object_size - off - bitfield_size;
8692 
8693   return true;
8694 }
8695 
8696 /// Get the value of the DW_AT_data_member_location of the given DIE
8697 /// attribute as an constant.
8698 ///
8699 /// @param die the DIE to read the attribute from.
8700 ///
8701 /// @param offset the attribute as a constant value.  This is set iff
8702 /// the function returns true.
8703 ///
8704 /// @return true if the attribute exists and has a constant value.  In
8705 /// that case the offset is set to the value.
8706 static bool
die_constant_data_member_location(const Dwarf_Die * die,int64_t & offset)8707 die_constant_data_member_location(const Dwarf_Die *die,
8708 				  int64_t& offset)
8709 {
8710   if (!die)
8711     return false;
8712 
8713   Dwarf_Attribute attr;
8714   if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8715 		  DW_AT_data_member_location,
8716 		  &attr))
8717     return false;
8718 
8719   Dwarf_Word val;
8720   if (dwarf_formudata(&attr, &val) != 0)
8721     return false;
8722 
8723   offset = val;
8724   return true;
8725 }
8726 
8727 /// Get the offset of a struct/class member as represented by the
8728 /// value of the DW_AT_data_member_location attribute.
8729 ///
8730 /// There is a huge gotcha in here.  The value of the
8731 /// DW_AT_data_member_location is not necessarily a constant that one
8732 /// would just read and be done with it.  Rather, it can be a DWARF
8733 /// expression that one has to interpret.  In general, the offset can
8734 /// be given by the DW_AT_data_bit_offset or by the
8735 /// DW_AT_data_member_location attribute and optionally the
8736 /// DW_AT_bit_offset attribute.  The bit offset attributes are
8737 /// always simple constants, but the DW_AT_data_member_location
8738 /// attribute is a DWARF location expression.
8739 ///
8740 /// When it's the DW_AT_data_member_location that is present,
8741 /// there are three cases to possibly take into account:
8742 ///
8743 ///     1/ The offset in the vtable where the offset of a virtual base
8744 ///        can be found, aka vptr offset.  Given the address of a
8745 ///        given object O, the vptr offset for B is given by the
8746 ///        (DWARF) expression:
8747 ///
8748 ///            address(O) + *(*address(0) - VIRTUAL_OFFSET)
8749 ///
8750 ///        where VIRTUAL_OFFSET is a constant value; In this case,
8751 ///        this function returns the constant VIRTUAL_OFFSET, as this
8752 ///        is enough to detect changes in a given virtual base
8753 ///        relative to the other virtual bases.
8754 ///
8755 ///     2/ The offset of a regular data member.  Given the address of
8756 ///        a struct object named O, the memory location for a
8757 ///        particular data member is given by the (DWARF) expression:
8758 ///
8759 ///            address(O) + OFFSET
8760 ///
8761 ///       where OFFSET is a constant.  In this case, this function
8762 ///       returns the OFFSET constant.
8763 ///
8764 ///     3/ The offset of a virtual member function in the virtual
8765 ///     pointer.  The DWARF expression is a constant that designates
8766 ///     the offset of the function in the vtable.  In this case this
8767 ///     function returns that constant.
8768 ///
8769 /// @param ctxt the read context to consider.
8770 ///
8771 /// @param die the DIE to read the information from.
8772 ///
8773 /// @param offset the resulting constant offset, in bits.  This
8774 /// argument is set iff the function returns true.
8775 static bool
die_member_offset(const read_context & ctxt,const Dwarf_Die * die,int64_t & offset)8776 die_member_offset(const read_context& ctxt,
8777 		  const Dwarf_Die* die,
8778 		  int64_t& offset)
8779 {
8780   Dwarf_Op* expr = NULL;
8781   uint64_t expr_len = 0;
8782   uint64_t bit_offset = 0;
8783 
8784   // First let's see if the DW_AT_data_bit_offset attribute is
8785   // present.
8786   if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8787     {
8788       offset = bit_offset;
8789       return true;
8790     }
8791 
8792   // First try to read DW_AT_data_member_location as a plain constant.
8793   // We do this because the generic method using die_location_expr
8794   // might hit a bug in elfutils libdw dwarf_location_expression only
8795   // fixed in elfutils 0.184+. The bug only triggers if the attribute
8796   // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8797   // handle all constants here because that is more consistent (and
8798   // slightly faster in the general case where the attribute isn't a
8799   // full DWARF expression).
8800   if (!die_constant_data_member_location(die, offset))
8801     {
8802       // Otherwise, let's see if the DW_AT_data_member_location
8803       // attribute and, optionally, the DW_AT_bit_offset attributes
8804       // are present.
8805       if (!die_location_expr(die, DW_AT_data_member_location,
8806 			     &expr, &expr_len))
8807 	return false;
8808 
8809       // The DW_AT_data_member_location attribute is present.  Let's
8810       // evaluate it and get its constant sub-expression and return
8811       // that one.
8812       if (!eval_quickly(expr, expr_len, offset))
8813 	{
8814 	  bool is_tls_address = false;
8815 	  if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8816 						 offset, is_tls_address,
8817 						 ctxt.dwarf_expr_eval_ctxt()))
8818 	    return false;
8819 	}
8820     }
8821   offset *= 8;
8822 
8823   // On little endian machines, we need to convert the
8824   // DW_AT_bit_offset attribute into a relative offset to 8 *
8825   // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8826   // would be if it were used instead.
8827   //
8828   // In other words, before adding it to 8 *
8829   // DW_AT_data_member_location, DW_AT_bit_offset needs to be
8830   // converted into a human-understandable form that represents the
8831   // offset of the bitfield data member it describes.  For details
8832   // about the conversion, please read the extensive comments of
8833   // read_and_convert_DW_at_bit_offset.
8834   bool is_big_endian = architecture_is_big_endian(ctxt.elf_handle());
8835   if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
8836     offset += bit_offset;
8837 
8838   return true;
8839 }
8840 
8841 /// Read the value of the DW_AT_location attribute from a DIE,
8842 /// evaluate the resulting DWARF expression and, if it's a constant
8843 /// expression, return it.
8844 ///
8845 /// @param die the DIE to consider.
8846 ///
8847 /// @param address the resulting constant address.  This is set iff
8848 /// the function returns true.
8849 ///
8850 /// @return true iff the whole sequence of action described above
8851 /// could be completed normally.
8852 static bool
die_location_address(Dwarf_Die * die,Dwarf_Addr & address,bool & is_tls_address)8853 die_location_address(Dwarf_Die*	die,
8854 		     Dwarf_Addr&	address,
8855 		     bool&		is_tls_address)
8856 {
8857   Dwarf_Op* expr = NULL;
8858   uint64_t expr_len = 0;
8859 
8860   is_tls_address = false;
8861 
8862   if (!die)
8863     return false;
8864 
8865   Dwarf_Attribute attr;
8866   if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
8867     return false;
8868 
8869   if (dwarf_getlocation(&attr, &expr, &expr_len))
8870     return false;
8871   // Ignore location expressions where reading them succeeded but
8872   // their length is 0.
8873   if (expr_len == 0)
8874     return false;
8875 
8876   Dwarf_Attribute result;
8877   if (!dwarf_getlocation_attr(&attr, expr, &result))
8878     // A location that has been interpreted as an address.
8879     return !dwarf_formaddr(&result, &address);
8880 
8881   // Just get the address out of the number field.
8882   address = expr->number;
8883   return true;
8884 }
8885 
8886 /// Return the index of a function in its virtual table.  That is,
8887 /// return the value of the DW_AT_vtable_elem_location attribute.
8888 ///
8889 /// @param die the DIE of the function to consider.
8890 ///
8891 /// @param vindex the resulting index.  This is set iff the function
8892 /// returns true.
8893 ///
8894 /// @return true if the DIE has a DW_AT_vtable_elem_location
8895 /// attribute.
8896 static bool
die_virtual_function_index(Dwarf_Die * die,int64_t & vindex)8897 die_virtual_function_index(Dwarf_Die* die,
8898 			   int64_t& vindex)
8899 {
8900   if (!die)
8901     return false;
8902 
8903   Dwarf_Op* expr = NULL;
8904   uint64_t expr_len = 0;
8905   if (!die_location_expr(die, DW_AT_vtable_elem_location,
8906 			 &expr, &expr_len))
8907     return false;
8908 
8909   int64_t i = 0;
8910   bool is_tls_addr = false;
8911   if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
8912     return false;
8913 
8914   vindex = i;
8915   return true;
8916 }
8917 
8918 /// Test if a given DIE represents an anonymous type.
8919 ///
8920 /// Anonymous types we are interested in are classes, unions and
8921 /// enumerations.
8922 ///
8923 /// @param die the DIE to consider.
8924 ///
8925 /// @return true iff @p die represents an anonymous type.
8926 bool
is_anonymous_type_die(Dwarf_Die * die)8927 is_anonymous_type_die(Dwarf_Die *die)
8928 {
8929   int tag = dwarf_tag(die);
8930 
8931   if (tag == DW_TAG_class_type
8932       || tag == DW_TAG_structure_type
8933       || tag == DW_TAG_union_type
8934       || tag == DW_TAG_enumeration_type)
8935     return die_is_anonymous(die);
8936 
8937   return false;
8938 }
8939 
8940 /// Return the base of the internal name to represent an anonymous
8941 /// type.
8942 ///
8943 /// Typically, anonymous enums would be named
8944 /// __anonymous_enum__<number>, anonymous struct or classes would be
8945 /// named __anonymous_struct__<number> and anonymous unions would be
8946 /// named __anonymous_union__<number>.  The first part of these
8947 /// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
8948 /// the base name.  This function returns that base name, depending on
8949 /// the kind of type DIE we are looking at.
8950 ///
8951 /// @param die the type DIE to look at.  This function expects a type
8952 /// DIE with an empty DW_AT_name property value (anonymous).
8953 ///
8954 /// @return a string representing the base of the internal anonymous
8955 /// name.
8956 static string
get_internal_anonymous_die_prefix_name(const Dwarf_Die * die)8957 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
8958 {
8959   ABG_ASSERT(die_is_type(die));
8960   ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
8961 
8962   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8963   string type_name;
8964   if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
8965     type_name = tools_utils::get_anonymous_struct_internal_name_prefix();
8966   else if (tag == DW_TAG_union_type)
8967     type_name = tools_utils::get_anonymous_union_internal_name_prefix();
8968   else if (tag == DW_TAG_enumeration_type)
8969     type_name = tools_utils::get_anonymous_enum_internal_name_prefix();
8970 
8971   return type_name;
8972 }
8973 
8974 /// Build a full internal anonymous type name.
8975 ///
8976 /// @param base_name this is the base name as returned by the function
8977 /// @ref get_internal_anonymous_die_prefix_name.
8978 ///
8979 /// @param anonymous_type_index this is the index of the anonymous
8980 /// type in its scope.  That is, if there are more than one anonymous
8981 /// types of a given kind in a scope, this index is what tells them
8982 /// appart, starting from 0.
8983 ///
8984 /// @return the built string, which is a concatenation of @p base_name
8985 /// and @p anonymous_type_index.
8986 static string
build_internal_anonymous_die_name(const string & base_name,size_t anonymous_type_index)8987 build_internal_anonymous_die_name(const string &base_name,
8988 				  size_t anonymous_type_index)
8989 {
8990   string name = base_name;
8991   if (anonymous_type_index && !base_name.empty())
8992     {
8993       std::ostringstream o;
8994       o << base_name << anonymous_type_index;
8995       name = o.str();
8996     }
8997   return name;
8998 }
8999 
9000 /// Build the internal name of the underlying type of an enum.
9001 ///
9002 /// @param base_name the (unqualified) name of the enum the underlying
9003 /// type is destined to.
9004 ///
9005 /// @param is_anonymous true if the underlying type of the enum is to
9006 /// be anonymous.
9007 static string
build_internal_underlying_enum_type_name(const string & base_name,bool is_anonymous,uint64_t size)9008 build_internal_underlying_enum_type_name(const string &base_name,
9009 					 bool is_anonymous,
9010 					 uint64_t size)
9011 {
9012   std::ostringstream o;
9013 
9014   if (is_anonymous)
9015     o << "unnamed-enum";
9016   else
9017     o << "enum-" << base_name;
9018 
9019   o << "-underlying-type-" << size;
9020 
9021   return o.str();
9022 }
9023 
9024 /// Build a full internal anonymous type name.
9025 ///
9026 /// @param die the DIE representing the anonymous type to consider.
9027 ///
9028 /// @param anonymous_type_index the index of the anonymous type
9029 /// represented by @p DIE, in its scope.  That is, if there are
9030 /// several different anonymous types of the same kind as @p die, this
9031 /// index is what tells them appart.
9032 ///
9033 /// @return the internal name of the anonymous type represented by @p
9034 /// DIE.
9035 static string
get_internal_anonymous_die_name(Dwarf_Die * die,size_t anonymous_type_index)9036 get_internal_anonymous_die_name(Dwarf_Die *die,
9037 				size_t anonymous_type_index)
9038 {
9039   string name = get_internal_anonymous_die_prefix_name(die);
9040   name = build_internal_anonymous_die_name(name, anonymous_type_index);
9041   return name;
9042 }
9043 
9044 // ------------------------------------
9045 // <DIE pretty printer>
9046 // ------------------------------------
9047 
9048 /// Compute the qualified name of a DIE that represents a type.
9049 ///
9050 /// For instance, if the DIE tag is DW_TAG_subprogram then this
9051 /// function computes the name of the function *type*.
9052 ///
9053 /// @param ctxt the read context.
9054 ///
9055 /// @param die the DIE to consider.
9056 ///
9057 /// @param where_offset where in the are logically are in the DIE
9058 /// stream.
9059 ///
9060 /// @return a copy of the qualified name of the type.
9061 static string
die_qualified_type_name(const read_context & ctxt,const Dwarf_Die * die,size_t where_offset)9062 die_qualified_type_name(const read_context& ctxt,
9063 			const Dwarf_Die* die,
9064 			size_t where_offset)
9065 {
9066   if (!die)
9067     return "";
9068 
9069   int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9070   if (tag == DW_TAG_compile_unit
9071       || tag == DW_TAG_partial_unit
9072       || tag == DW_TAG_type_unit)
9073     return "";
9074 
9075   string name = die_name(die);
9076 
9077   Dwarf_Die scope_die;
9078   if (!get_scope_die(ctxt, die, where_offset, scope_die))
9079     return "";
9080 
9081   string parent_name = die_qualified_name(ctxt, &scope_die, where_offset);
9082   bool colon_colon = die_is_type(die) || die_is_namespace(die);
9083   string separator = colon_colon ? "::" : ".";
9084 
9085   string repr;
9086 
9087   switch (tag)
9088     {
9089     case DW_TAG_unspecified_type:
9090       break;
9091 
9092     case DW_TAG_base_type:
9093       {
9094 	abigail::ir::integral_type int_type;
9095 	if (parse_integral_type(name, int_type))
9096 	  repr = int_type;
9097 	else
9098 	  repr = name;
9099       }
9100       break;
9101 
9102     case DW_TAG_typedef:
9103     case DW_TAG_enumeration_type:
9104     case DW_TAG_structure_type:
9105     case DW_TAG_class_type:
9106     case DW_TAG_union_type:
9107       {
9108 	if (tag == DW_TAG_typedef)
9109 	  {
9110 	    // If the underlying type of the typedef is unspecified,
9111 	    // bail out as we don't support that yet.
9112 	    Dwarf_Die underlying_type_die;
9113 	    if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9114 	      {
9115 		string n = die_qualified_type_name(ctxt, &underlying_type_die,
9116 						   where_offset);
9117 		if (die_is_unspecified(&underlying_type_die)
9118 		    || n.empty())
9119 		  break;
9120 	      }
9121 	  }
9122 
9123 	if (name.empty())
9124 	  // TODO: handle cases where there are more than one
9125 	  // anonymous type of the same kind in the same scope.  In
9126 	  // that case, their name must be built with the function
9127 	  // get_internal_anonymous_die_name or something of the same
9128 	  // kind.
9129 	  name = get_internal_anonymous_die_prefix_name(die);
9130 
9131 	ABG_ASSERT(!name.empty());
9132 	repr = parent_name.empty() ? name : parent_name + separator + name;
9133       }
9134       break;
9135 
9136     case DW_TAG_const_type:
9137     case DW_TAG_volatile_type:
9138     case DW_TAG_restrict_type:
9139       {
9140 	Dwarf_Die underlying_type_die;
9141 	bool has_underlying_type_die =
9142 	  die_die_attribute(die, DW_AT_type, underlying_type_die);
9143 
9144 	if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9145 	  break;
9146 
9147 	if (tag == DW_TAG_const_type)
9148 	  {
9149 	    if (has_underlying_type_die
9150 		&& die_is_reference_type(&underlying_type_die))
9151 	      // A reference is always const.  So, to lower false
9152 	      // positive reports in diff computations, we consider a
9153 	      // const reference just as a reference.  But we need to
9154 	      // keep the qualified-ness of the type.  So we introduce
9155 	      // a 'no-op' qualifier here.  Please remember that this
9156 	      // has to be kept in sync with what is done in
9157 	      // get_name_of_qualified_type.  So if you change this
9158 	      // here, you have to change that code there too.
9159 	      repr = "";
9160 	    else if (!has_underlying_type_die
9161 		     || die_is_void_type(&underlying_type_die))
9162 	      {
9163 		repr = "void";
9164 		break;
9165 	      }
9166 	    else
9167 	      repr = "const";
9168 	  }
9169 	else if (tag == DW_TAG_volatile_type)
9170 	  repr = "volatile";
9171 	else if (tag == DW_TAG_restrict_type)
9172 	  repr = "restrict";
9173 	else
9174 	  ABG_ASSERT_NOT_REACHED;
9175 
9176 	string underlying_type_repr;
9177 	if (has_underlying_type_die)
9178 	  underlying_type_repr =
9179 	    die_qualified_type_name(ctxt, &underlying_type_die, where_offset);
9180 	else
9181 	  underlying_type_repr = "void";
9182 
9183 	if (underlying_type_repr.empty())
9184 	  repr.clear();
9185 	else
9186 	  {
9187 	    if (has_underlying_type_die
9188 		&& die_is_pointer_or_reference_type(&underlying_type_die))
9189 	      repr = underlying_type_repr + " " + repr;
9190 	    else
9191 	      repr += " " + underlying_type_repr;
9192 	  }
9193       }
9194       break;
9195 
9196     case DW_TAG_pointer_type:
9197     case DW_TAG_reference_type:
9198     case DW_TAG_rvalue_reference_type:
9199       {
9200 	Dwarf_Die pointed_to_type_die;
9201 	if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9202 	  {
9203 	    if (tag == DW_TAG_pointer_type)
9204 	      repr = "void*";
9205 	    break;
9206 	  }
9207 
9208 	if (die_is_unspecified(&pointed_to_type_die))
9209 	  break;
9210 
9211 	string pointed_type_repr =
9212 	  die_qualified_type_name(ctxt, &pointed_to_type_die, where_offset);
9213 
9214 	repr = pointed_type_repr;
9215 	if (repr.empty())
9216 	  break;
9217 
9218 	if (tag == DW_TAG_pointer_type)
9219 	  repr += "*";
9220 	else if (tag == DW_TAG_reference_type)
9221 	  repr += "&";
9222 	else if (tag == DW_TAG_rvalue_reference_type)
9223 	  repr += "&&";
9224 	else
9225 	  ABG_ASSERT_NOT_REACHED;
9226       }
9227       break;
9228 
9229     case DW_TAG_subrange_type:
9230       {
9231 	// In Ada, this one can be generated on its own, that is, not
9232 	// as a sub-type of an array.  So we need to support it on its
9233 	// own.  Note that when it's emitted as the sub-type of an
9234 	// array like in C and C++, this is handled differently, for
9235 	// now.  But we try to make this usable by other languages
9236 	// that are not Ada, even if we modelled it after Ada.
9237 
9238 	// So we build a subrange type for the sole purpose of using
9239 	// the ::as_string() method of that type.  So we don't add
9240 	// that type to the current type tree being built.
9241 	array_type_def::subrange_sptr s =
9242 	  build_subrange_type(const_cast<read_context&>(ctxt),
9243 			      die, where_offset,
9244 			      /*associate_die_to_type=*/false);
9245 	repr += s->as_string();
9246 	break;
9247       }
9248 
9249     case DW_TAG_array_type:
9250       {
9251 	Dwarf_Die element_type_die;
9252 	if (!die_die_attribute(die, DW_AT_type, element_type_die))
9253 	  break;
9254 	string element_type_name =
9255 	  die_qualified_type_name(ctxt, &element_type_die, where_offset);
9256 	if (element_type_name.empty())
9257 	  break;
9258 
9259 	array_type_def::subranges_type subranges;
9260 	build_subranges_from_array_type_die(const_cast<read_context&>(ctxt),
9261 					    die, subranges, where_offset,
9262 					    /*associate_type_to_die=*/false);
9263 
9264 	repr = element_type_name;
9265 	repr += array_type_def::subrange_type::vector_as_string(subranges);
9266       }
9267       break;
9268 
9269     case DW_TAG_subroutine_type:
9270     case DW_TAG_subprogram:
9271       {
9272 	string return_type_name;
9273 	string class_name;
9274 	vector<string> parm_names;
9275 	bool is_const = false;
9276 	bool is_static = false;
9277 
9278 	die_return_and_parm_names_from_fn_type_die(ctxt, die, where_offset,
9279 						   /*pretty_print=*/true,
9280 						   return_type_name, class_name,
9281 						   parm_names, is_const,
9282 						   is_static);
9283 	if (return_type_name.empty())
9284 	  return_type_name = "void";
9285 
9286 	repr = return_type_name;
9287 
9288 	if (!class_name.empty())
9289 	  {
9290 	    // This is a method, so print the class name.
9291 	    repr += " (" + class_name + "::*)";
9292 	  }
9293 
9294 	// Now parameters.
9295 	repr += " (";
9296 	for (vector<string>::const_iterator i = parm_names.begin();
9297 	     i != parm_names.end();
9298 	     ++i)
9299 	  {
9300 	    if (i != parm_names.begin())
9301 	      repr += ", ";
9302 	    repr += *i;
9303 	  }
9304 	repr += ")";
9305 
9306       }
9307       break;
9308 
9309     case DW_TAG_string_type:
9310     case DW_TAG_ptr_to_member_type:
9311     case DW_TAG_set_type:
9312     case DW_TAG_file_type:
9313     case DW_TAG_packed_type:
9314     case DW_TAG_thrown_type:
9315     case DW_TAG_interface_type:
9316     case DW_TAG_shared_type:
9317       break;
9318     }
9319 
9320   return repr;
9321 }
9322 
9323 /// Compute the qualified name of a decl represented by a given DIE.
9324 ///
9325 /// For instance, for a DIE of tag DW_TAG_subprogram this function
9326 /// computes the signature of the function *declaration*.
9327 ///
9328 /// @param ctxt the read context.
9329 ///
9330 /// @param die the DIE to consider.
9331 ///
9332 /// @param where_offset where we are logically at in the DIE stream.
9333 ///
9334 /// @return a copy of the computed name.
9335 static string
die_qualified_decl_name(const read_context & ctxt,const Dwarf_Die * die,size_t where_offset)9336 die_qualified_decl_name(const read_context& ctxt,
9337 			const Dwarf_Die* die,
9338 			size_t where_offset)
9339 {
9340   if (!die || !die_is_decl(die))
9341     return "";
9342 
9343   string name = die_name(die);
9344 
9345   Dwarf_Die scope_die;
9346   if (!get_scope_die(ctxt, die, where_offset, scope_die))
9347     return "";
9348 
9349   string scope_name = die_qualified_name(ctxt, &scope_die, where_offset);
9350   string separator = "::";
9351 
9352   string repr;
9353 
9354   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9355   switch (tag)
9356     {
9357     case DW_TAG_namespace:
9358     case DW_TAG_member:
9359     case DW_TAG_variable:
9360       repr = scope_name.empty() ? name : scope_name + separator + name;
9361       break;
9362     case DW_TAG_subprogram:
9363       repr = die_function_signature(ctxt, die, where_offset);
9364       break;
9365 
9366     case DW_TAG_unspecified_parameters:
9367       repr = "...";
9368       break;
9369 
9370     case DW_TAG_formal_parameter:
9371     case DW_TAG_imported_declaration:
9372     case DW_TAG_GNU_template_template_param:
9373     case DW_TAG_GNU_template_parameter_pack:
9374     case DW_TAG_GNU_formal_parameter_pack:
9375       break;
9376     }
9377   return repr;
9378 }
9379 
9380 /// Compute the qualified name of the artifact represented by a given
9381 /// DIE.
9382 ///
9383 /// If the DIE represents a type, then the function computes the name
9384 /// of the type.  Otherwise, if the DIE represents a decl then the
9385 /// function computes the name of the decl.  Note that a DIE of tag
9386 /// DW_TAG_subprogram is going to be considered as a "type" -- just
9387 /// like if it was a DW_TAG_subroutine_type.
9388 ///
9389 /// @param ctxt the read context.
9390 ///
9391 /// @param die the DIE to consider.
9392 ///
9393 /// @param where_offset where we are logically at in the DIE stream.
9394 ///
9395 /// @return a copy of the computed name.
9396 static string
die_qualified_name(const read_context & ctxt,const Dwarf_Die * die,size_t where)9397 die_qualified_name(const read_context& ctxt, const Dwarf_Die* die, size_t where)
9398 {
9399   if (die_is_type(die))
9400     return die_qualified_type_name(ctxt, die, where);
9401   else if (die_is_decl(die))
9402     return die_qualified_decl_name(ctxt, die, where);
9403   return "";
9404 }
9405 
9406 /// Test if the qualified name of a given type should be empty.
9407 ///
9408 /// The reason why the name of a DIE with a given tag would be empty
9409 /// is that libabigail's internal representation doesn't yet support
9410 /// that tag; or if the DIE's qualified name is built from names of
9411 /// sub-types DIEs whose tags are not yet supported.
9412 ///
9413 /// @param ctxt the reading context.
9414 ///
9415 /// @param die the DIE to consider.
9416 ///
9417 /// @param where where we are logically at, in the DIE stream.
9418 ///
9419 /// @param qualified_name the qualified name of the DIE.  This is set
9420 /// only iff the function returns false.
9421 ///
9422 /// @return true if the qualified name of the DIE is empty.
9423 static bool
die_qualified_type_name_empty(const read_context & ctxt,const Dwarf_Die * die,size_t where,string & qualified_name)9424 die_qualified_type_name_empty(const read_context& ctxt,
9425 			      const Dwarf_Die* die,
9426 			      size_t where, string &qualified_name)
9427 {
9428   if (!die)
9429     return true;
9430 
9431   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9432 
9433   string qname;
9434   if (tag == DW_TAG_typedef
9435       || tag == DW_TAG_pointer_type
9436       || tag == DW_TAG_reference_type
9437       || tag == DW_TAG_rvalue_reference_type
9438       || tag == DW_TAG_array_type
9439       || tag == DW_TAG_const_type
9440       || tag == DW_TAG_volatile_type
9441       || tag == DW_TAG_restrict_type)
9442     {
9443       Dwarf_Die underlying_type_die;
9444       if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9445 	{
9446 	  string name =
9447 	    die_qualified_type_name(ctxt, &underlying_type_die, where);
9448 	  if (name.empty())
9449 	    return true;
9450 	}
9451     }
9452   else
9453     {
9454       string name = die_qualified_type_name(ctxt, die, where);
9455       if (name.empty())
9456 	return true;
9457     }
9458 
9459   qname = die_qualified_type_name(ctxt, die, where);
9460   if (qname.empty())
9461     return true;
9462 
9463   qualified_name = qname;
9464   return false;
9465 }
9466 
9467 /// Given the DIE that represents a function type, compute the names
9468 /// of the following properties the function's type:
9469 ///
9470 ///   - return type
9471 ///   - enclosing class (if the function is a member function)
9472 ///   - function parameter types
9473 ///
9474 /// When the function we are looking at is a member function, it also
9475 /// tells if it's const.
9476 ///
9477 /// @param ctxt the reading context.
9478 ///
9479 /// @param die the DIE of the function or function type we are looking
9480 /// at.
9481 ///
9482 /// @param where_offset where we are logically at in the DIE stream.
9483 ///
9484 /// @param pretty_print if set to yes, the type names are going to be
9485 /// pretty-printed names; otherwise, they are just qualified type
9486 /// names.
9487 ///
9488 /// @param return_type_name out parameter.  This contains the name of
9489 /// the return type of the function.
9490 ///
9491 /// @param class_name out parameter.  If the function is a member
9492 /// function, this contains the name of the enclosing class.
9493 ///
9494 /// @param parm_names out parameter.  This vector is set to the names
9495 /// of the types of the parameters of the function.
9496 ///
9497 /// @param is_const out parameter.  If the function is a member
9498 /// function, this is set to true iff the member function is const.
9499 ///
9500 /// @param is_static out parameter.  If the function is a static
9501 /// member function, then this is set to true.
9502 static void
die_return_and_parm_names_from_fn_type_die(const read_context & ctxt,const Dwarf_Die * die,size_t where_offset,bool pretty_print,string & return_type_name,string & class_name,vector<string> & parm_names,bool & is_const,bool & is_static)9503 die_return_and_parm_names_from_fn_type_die(const read_context& ctxt,
9504 					   const Dwarf_Die* die,
9505 					   size_t where_offset,
9506 					   bool pretty_print,
9507 					   string &return_type_name,
9508 					   string &class_name,
9509 					   vector<string>& parm_names,
9510 					   bool& is_const,
9511 					   bool& is_static)
9512 {
9513   Dwarf_Die child;
9514   Dwarf_Die ret_type_die;
9515   if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9516     return_type_name = "void";
9517   else
9518     return_type_name =
9519       pretty_print
9520       ? ctxt.get_die_pretty_representation(&ret_type_die, where_offset)
9521       : ctxt.get_die_qualified_type_name(&ret_type_die, where_offset);
9522 
9523   if (return_type_name.empty())
9524     return_type_name = "void";
9525 
9526   Dwarf_Die object_pointer_die, class_die;
9527   bool is_method_type =
9528     die_function_type_is_method_type(ctxt, die, where_offset,
9529 				     object_pointer_die,
9530 				     class_die, is_static);
9531 
9532   is_const = false;
9533   if (is_method_type)
9534     {
9535       class_name = ctxt.get_die_qualified_type_name(&class_die, where_offset);
9536 
9537       Dwarf_Die this_pointer_die;
9538       Dwarf_Die pointed_to_die;
9539       if (!is_static
9540 	  && die_die_attribute(&object_pointer_die, DW_AT_type,
9541 			       this_pointer_die))
9542 	if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9543 	  if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9544 	    is_const = true;
9545 
9546       string fn_name = die_name(die);
9547       string non_qualified_class_name = die_name(&class_die);
9548       bool is_ctor = fn_name == non_qualified_class_name;
9549       bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9550 
9551       if (is_ctor || is_dtor)
9552 	return_type_name.clear();
9553     }
9554 
9555   if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9556     do
9557       {
9558 	int child_tag = dwarf_tag(&child);
9559 	if (child_tag == DW_TAG_formal_parameter)
9560 	  {
9561 	    Dwarf_Die parm_type_die;
9562 	    if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9563 	      continue;
9564 	    string qualified_name =
9565 	      pretty_print
9566 	      ? ctxt.get_die_pretty_representation(&parm_type_die, where_offset)
9567 	      : ctxt.get_die_qualified_type_name(&parm_type_die, where_offset);
9568 
9569 	    if (qualified_name.empty())
9570 	      continue;
9571 	    parm_names.push_back(qualified_name);
9572 	  }
9573 	else if (child_tag == DW_TAG_unspecified_parameters)
9574 	  {
9575 	    // This is a variadic function parameter.
9576 	    parm_names.push_back("variadic parameter type");
9577 	    // After a DW_TAG_unspecified_parameters tag, we shouldn't
9578 	    // keep reading for parameters.  The
9579 	    // unspecified_parameters TAG should be the last parameter
9580 	    // that we record. For instance, if there are multiple
9581 	    // DW_TAG_unspecified_parameters DIEs then we should care
9582 	    // only for the first one.
9583 	    break;
9584 	  }
9585       }
9586     while (dwarf_siblingof(&child, &child) == 0);
9587 
9588   if (class_name.empty())
9589     {
9590       Dwarf_Die parent_die;
9591       if (get_parent_die(ctxt, die, parent_die, where_offset))
9592 	{
9593 	  if (die_is_class_type(&parent_die))
9594 	    class_name =
9595 	      ctxt.get_die_qualified_type_name(&parent_die, where_offset);
9596 	}
9597     }
9598 }
9599 
9600 /// This computes the signature of the a function declaration
9601 /// represented by a DIE.
9602 ///
9603 /// @param ctxt the reading context.
9604 ///
9605 /// @param fn_die the DIE of the function to consider.
9606 ///
9607 /// @param where_offset where we are logically at in the stream of
9608 /// DIEs.
9609 ///
9610 /// @return a copy of the computed function signature string.
9611 static string
die_function_signature(const read_context & ctxt,const Dwarf_Die * fn_die,size_t where_offset)9612 die_function_signature(const read_context& ctxt,
9613 		       const Dwarf_Die *fn_die,
9614 		       size_t where_offset)
9615 {
9616 
9617   translation_unit::language lang;
9618   bool has_lang = false;
9619   if ((has_lang = ctxt.get_die_language(fn_die, lang)))
9620     {
9621       // In a binary originating from the C language, it's OK to use
9622       // the linkage name of the function as a key for the map which
9623       // is meant to reduce the number of DIE comparisons involved
9624       // during DIE canonicalization computation.
9625       if (is_c_language(lang))
9626 	{
9627 	  string fn_name = die_linkage_name(fn_die);
9628 	  if (fn_name.empty())
9629 	    fn_name = die_name(fn_die);
9630 	  return fn_name;
9631 	}
9632     }
9633 
9634   // TODO: When we can structurally compare DIEs originating from C++
9635   // as well, we can use the linkage name of functions in C++ too, to
9636   // reduce the number of comparisons involved during DIE
9637   // canonicalization.
9638 
9639   string return_type_name;
9640   Dwarf_Die ret_type_die;
9641   if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9642     return_type_name = ctxt.get_die_qualified_type_name(&ret_type_die,
9643 							where_offset);
9644 
9645   if (return_type_name.empty())
9646     return_type_name = "void";
9647 
9648   Dwarf_Die scope_die;
9649   string scope_name;
9650   if (get_scope_die(ctxt, fn_die, where_offset, scope_die))
9651     scope_name = ctxt.get_die_qualified_name(&scope_die, where_offset);
9652   string fn_name = die_name(fn_die);
9653   if (!scope_name.empty())
9654     fn_name  = scope_name + "::" + fn_name;
9655 
9656   string class_name;
9657   vector<string> parm_names;
9658   bool is_const = false;
9659   bool is_static = false;
9660 
9661   die_return_and_parm_names_from_fn_type_die(ctxt, fn_die, where_offset,
9662 					     /*pretty_print=*/false,
9663 					     return_type_name, class_name,
9664 					     parm_names, is_const, is_static);
9665 
9666   bool is_virtual = die_is_virtual(fn_die);
9667 
9668   string repr = class_name.empty() ? "function" : "method";
9669   if (is_virtual)
9670     repr += " virtual";
9671 
9672   if (!return_type_name.empty())
9673     repr += " " + return_type_name;
9674 
9675   repr += " " + fn_name;
9676 
9677   // Now parameters.
9678   repr += "(";
9679   bool some_parm_emitted = false;
9680   for (vector<string>::const_iterator i = parm_names.begin();
9681        i != parm_names.end();
9682        ++i)
9683     {
9684       if (i != parm_names.begin())
9685 	{
9686 	  if (some_parm_emitted)
9687 	    repr += ", ";
9688 	}
9689       else
9690 	if (!is_static && !class_name.empty())
9691 	  // We are printing a non-static method name, skip the implicit "this"
9692 	  // parameter type.
9693 	  continue;
9694       repr += *i;
9695       some_parm_emitted = true;
9696     }
9697   repr += ")";
9698 
9699   if (is_const)
9700     {
9701       ABG_ASSERT(!class_name.empty());
9702       repr += " const";
9703     }
9704 
9705   return repr;
9706 }
9707 
9708 /// Return a pretty string representation of a type, for internal purposes.
9709 ///
9710 /// By internal purpose, we mean things like key-ing types for lookup
9711 /// purposes and so on.
9712 ///
9713 /// Note that this function is also used to pretty print functions.
9714 /// For functions, it prints the *type* of the function.
9715 ///
9716 /// @param ctxt the context to use.
9717 ///
9718 /// @param the DIE of the type to pretty print.
9719 ///
9720 /// @param where_offset where we logically are placed when calling
9721 /// this.  It's useful to handle inclusion of DW_TAG_compile_unit
9722 /// entries.
9723 ///
9724 /// @return the resulting pretty representation.
9725 static string
die_pretty_print_type(read_context & ctxt,const Dwarf_Die * die,size_t where_offset)9726 die_pretty_print_type(read_context& ctxt,
9727 		      const Dwarf_Die* die,
9728 		      size_t where_offset)
9729 {
9730   if (!die
9731       || (!die_is_type(die)
9732 	  && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9733     return "";
9734 
9735   string repr;
9736 
9737   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9738   switch (tag)
9739     {
9740     case DW_TAG_string_type:
9741       // For now, we won't try to go get the actual representation of
9742       // the string because this would make things more complicated;
9743       // for that we'd need to interpret some location expressions to
9744       // get the length of the string.  And for dynamically allocated
9745       // strings, the result of the location expression evaluation
9746       // might not even be a constant.  So at the moment I consider
9747       // this to be a lot of hassle for no great return.  Until proven
9748       // otherwise, of course.
9749       repr = "string type";
9750       break;
9751 
9752     case DW_TAG_unspecified_type:
9753     case DW_TAG_ptr_to_member_type:
9754       break;
9755 
9756     case DW_TAG_namespace:
9757       repr = "namespace " + ctxt.get_die_qualified_type_name(die, where_offset);
9758       break;
9759 
9760     case DW_TAG_base_type:
9761       repr = ctxt.get_die_qualified_type_name(die, where_offset);
9762       break;
9763 
9764     case DW_TAG_typedef:
9765       {
9766 	string qualified_name;
9767 	if (!die_qualified_type_name_empty(ctxt, die,
9768 					   where_offset,
9769 					   qualified_name))
9770 	  repr = "typedef " + qualified_name;
9771       }
9772       break;
9773 
9774     case DW_TAG_const_type:
9775     case DW_TAG_volatile_type:
9776     case DW_TAG_restrict_type:
9777     case DW_TAG_pointer_type:
9778     case DW_TAG_reference_type:
9779     case DW_TAG_rvalue_reference_type:
9780       repr = ctxt.get_die_qualified_type_name(die, where_offset);
9781       break;
9782 
9783     case DW_TAG_enumeration_type:
9784       {
9785 	string qualified_name =
9786 	  ctxt.get_die_qualified_type_name(die, where_offset);
9787 	repr = "enum " + qualified_name;
9788       }
9789       break;
9790 
9791     case DW_TAG_structure_type:
9792     case DW_TAG_class_type:
9793       {
9794 	string qualified_name =
9795 	  ctxt.get_die_qualified_type_name(die, where_offset);
9796 	repr = "class " + qualified_name;
9797       }
9798       break;
9799 
9800     case DW_TAG_union_type:
9801       {
9802 	string qualified_name =
9803 	  ctxt.get_die_qualified_type_name(die, where_offset);
9804 	repr = "union " + qualified_name;
9805       }
9806       break;
9807 
9808     case DW_TAG_array_type:
9809       {
9810 	Dwarf_Die element_type_die;
9811 	if (!die_die_attribute(die, DW_AT_type, element_type_die))
9812 	  break;
9813 	string element_type_name =
9814 	  ctxt.get_die_qualified_type_name(&element_type_die, where_offset);
9815 	if (element_type_name.empty())
9816 	  break;
9817 
9818 	array_type_def::subranges_type subranges;
9819 	build_subranges_from_array_type_die(ctxt, die, subranges, where_offset,
9820 					    /*associate_type_to_die=*/false);
9821 
9822 	repr = element_type_name;
9823 	repr += array_type_def::subrange_type::vector_as_string(subranges);
9824       }
9825       break;
9826 
9827     case DW_TAG_subrange_type:
9828       {
9829 	// So this can be generated by Ada, on its own; that is, not
9830 	// as a subtype of an array.  In that case we need to handle
9831 	// it properly.
9832 
9833 	// For now, we consider that the pretty printed name of the
9834 	// subrange type is its name.  We might need something more
9835 	// advance, should the needs of the users get more
9836 	// complicated.
9837 	repr += die_qualified_type_name(ctxt, die, where_offset);
9838       }
9839       break;
9840 
9841     case DW_TAG_subroutine_type:
9842     case DW_TAG_subprogram:
9843       {
9844 	string return_type_name;
9845 	string class_name;
9846 	vector<string> parm_names;
9847 	bool is_const = false;
9848 	bool is_static = false;
9849 
9850 	die_return_and_parm_names_from_fn_type_die(ctxt, die, where_offset,
9851 						   /*pretty_print=*/true,
9852 						   return_type_name, class_name,
9853 						   parm_names, is_const,
9854 						   is_static);
9855 	if (class_name.empty())
9856 	  repr = "function type";
9857 	else
9858 	  repr = "method type";
9859 	repr += " " + ctxt.get_die_qualified_type_name(die, where_offset);
9860       }
9861       break;
9862 
9863     case DW_TAG_set_type:
9864     case DW_TAG_file_type:
9865     case DW_TAG_packed_type:
9866     case DW_TAG_thrown_type:
9867     case DW_TAG_interface_type:
9868     case DW_TAG_shared_type:
9869       ABG_ASSERT_NOT_REACHED;
9870     }
9871 
9872   return repr;
9873 }
9874 
9875 /// Return a pretty string representation of a declaration, for
9876 /// internal purposes.
9877 ///
9878 /// By internal purpose, we mean things like key-ing declarations for
9879 /// lookup purposes and so on.
9880 ///
9881 /// Note that this function is also used to pretty print functions.
9882 /// For functions, it prints the signature of the function.
9883 ///
9884 /// @param ctxt the context to use.
9885 ///
9886 /// @param the DIE of the declaration to pretty print.
9887 ///
9888 /// @param where_offset where we logically are placed when calling
9889 /// this.  It's useful to handle inclusion of DW_TAG_compile_unit
9890 /// entries.
9891 ///
9892 /// @return the resulting pretty representation.
9893 static string
die_pretty_print_decl(read_context & ctxt,const Dwarf_Die * die,size_t where_offset)9894 die_pretty_print_decl(read_context& ctxt,
9895 		      const Dwarf_Die* die,
9896 		      size_t where_offset)
9897 {
9898   if (!die || !die_is_decl(die))
9899     return "";
9900 
9901   string repr;
9902 
9903   int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9904   switch (tag)
9905     {
9906     case DW_TAG_namespace:
9907       repr = "namespace " + die_qualified_name(ctxt, die, where_offset);
9908       break;
9909 
9910     case DW_TAG_member:
9911     case DW_TAG_variable:
9912       {
9913 	string type_repr = "void";
9914 	Dwarf_Die type_die;
9915 	if (die_die_attribute(die, DW_AT_type, type_die))
9916 	  type_repr = die_qualified_type_name(ctxt, &type_die, where_offset);
9917 	repr = die_qualified_name(ctxt, die, where_offset);
9918 	if (!repr.empty())
9919 	  repr = type_repr + " " + repr;
9920       }
9921       break;
9922 
9923     case DW_TAG_subprogram:
9924       repr = die_function_signature(ctxt, die, where_offset);
9925       break;
9926 
9927     default:
9928       break;
9929     }
9930   return repr;
9931 }
9932 
9933 /// Compute the pretty printed representation of an artifact
9934 /// represented by a DIE.
9935 ///
9936 /// If the DIE is a type, compute the its pretty representation as a
9937 /// type; otherwise, if it's a declaration, compute its pretty
9938 /// representation as a declaration.  Note for For instance, that a
9939 /// DW_TAG_subprogram DIE is going to be represented as a function
9940 /// *type*.
9941 ///
9942 /// @param ctxt the reading context.
9943 ///
9944 /// @param die the DIE to consider.
9945 ///
9946 /// @param where_offset we in the DIE stream we are logically at.
9947 ///
9948 /// @return a copy of the pretty printed artifact.
9949 static string
die_pretty_print(read_context & ctxt,const Dwarf_Die * die,size_t where_offset)9950 die_pretty_print(read_context& ctxt, const Dwarf_Die* die, size_t where_offset)
9951 {
9952   if (die_is_type(die))
9953     return die_pretty_print_type(ctxt, die, where_offset);
9954   else if (die_is_decl(die))
9955     return die_pretty_print_decl(ctxt, die, where_offset);
9956   return "";
9957 }
9958 
9959 // -----------------------------------
9960 // </die pretty printer>
9961 // -----------------------------------
9962 
9963 
9964 // ----------------------------------
9965 // <die comparison engine>
9966 // ---------------------------------
9967 
9968 /// Compares two decls DIEs
9969 ///
9970 /// This works only for DIEs emitted by the C language.
9971 ///
9972 /// This implementation doesn't yet support namespaces.
9973 ///
9974 /// This is a subroutine of compare_dies.
9975 ///
9976 /// @return true iff @p l equals @p r.
9977 static bool
compare_as_decl_dies(const Dwarf_Die * l,const Dwarf_Die * r)9978 compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
9979 {
9980   ABG_ASSERT(l && r);
9981 
9982   int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
9983   int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
9984   if (l_tag != r_tag)
9985     return false;
9986 
9987   bool result = false;
9988 
9989   if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
9990     {
9991       // Fast path for functions and global variables.
9992       if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
9993 					      result)
9994 	  || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
9995 						 result))
9996 	{
9997 	  if (!result)
9998 	    return false;
9999 	}
10000 
10001       if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10002 					      result))
10003 	{
10004 	  if (!result)
10005 	    return false;
10006 	}
10007       return true;
10008     }
10009 
10010   // Fast path for types.
10011   if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10012 					  result))
10013     return result;
10014   return true;
10015 }
10016 
10017 /// Compares two type DIEs
10018 ///
10019 /// This is a subroutine of compare_dies.
10020 ///
10021 /// @param l the left operand of the comparison operator.
10022 ///
10023 /// @param r the right operand of the comparison operator.
10024 ///
10025 /// @return true iff @p l equals @p r.
10026 static bool
compare_as_type_dies(const Dwarf_Die * l,const Dwarf_Die * r)10027 compare_as_type_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10028 {
10029   ABG_ASSERT(l && r);
10030   ABG_ASSERT(die_is_type(l));
10031   ABG_ASSERT(die_is_type(r));
10032 
10033   if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10034       && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10035       && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10036 	  != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10037     // For now, we cannot compare DW_TAG_string_type because of its
10038     // string_length attribute that is a location descriptor that is
10039     // not necessarily a constant.  So it's super hard to evaluate it
10040     // in a libabigail context.  So for now, we just say that all
10041     // DW_TAG_string_type DIEs are different, by default.
10042     return false;
10043 
10044   uint64_t l_size = 0, r_size = 0;
10045   die_size_in_bits(l, l_size);
10046   die_size_in_bits(r, r_size);
10047 
10048   return l_size == r_size;
10049 }
10050 
10051 /// Test if two DIEs representing function declarations have the same
10052 /// linkage name, and thus are considered equal if they are C or C++,
10053 /// because the two DIEs represent functions in the same binary.
10054 ///
10055 /// If the DIEs don't have a linkage name, the function compares their
10056 /// name.  But in that case, the caller of the function must know that
10057 /// in C++ for instance, that doesn't imply that the two functions are
10058 /// equal.
10059 ///
10060 /// @param ctxt the @ref read_context to consider.
10061 ///
10062 /// @param l the first function DIE to consider.
10063 ///
10064 /// @param r the second function DIE to consider.
10065 ///
10066 /// @return true iff the function represented by @p l have the same
10067 /// linkage name as the function represented by @p r.
10068 static bool
fn_die_equal_by_linkage_name(const read_context & ctxt,const Dwarf_Die * l,const Dwarf_Die * r)10069 fn_die_equal_by_linkage_name(const read_context &ctxt,
10070 			     const Dwarf_Die *l,
10071 			     const Dwarf_Die *r)
10072 {
10073   if (!!l != !!r)
10074     return false;
10075 
10076   if (!l)
10077     return false;
10078 
10079   int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10080   ABG_ASSERT(tag == DW_TAG_subprogram);
10081   tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10082   ABG_ASSERT(tag == DW_TAG_subprogram);
10083 
10084   string lname = die_name(l), rname = die_name(r);
10085   string llinkage_name = die_linkage_name(l),
10086     rlinkage_name = die_linkage_name(r);
10087 
10088   if (ctxt.die_is_in_c_or_cplusplus(l)
10089       && ctxt.die_is_in_c_or_cplusplus(r))
10090     {
10091       if (!llinkage_name.empty() && !rlinkage_name.empty())
10092 	return llinkage_name == rlinkage_name;
10093       else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10094 	return false;
10095       else
10096 	return lname == rname;
10097     }
10098 
10099   return (!llinkage_name.empty()
10100 	  && !rlinkage_name.empty()
10101 	  && llinkage_name == rlinkage_name);
10102 }
10103 
10104 /// Compare two DIEs emitted by a C compiler.
10105 ///
10106 /// @param ctxt the read context used to load the DWARF information.
10107 ///
10108 /// @param l the left-hand-side argument of this comparison operator.
10109 ///
10110 /// @param r the righ-hand-side argument of this comparison operator.
10111 ///
10112 /// @param aggregates_being_compared this holds the names of the set
10113 /// of aggregates being compared.  It's used by the comparison
10114 /// function to avoid recursing infinitely when faced with types
10115 /// referencing themselves through pointers or references.  By
10116 /// default, just pass an empty instance of @ref istring_set_type to
10117 /// it.
10118 ///
10119 /// @param update_canonical_dies_on_the_fly if true, when two
10120 /// sub-types compare equal (during the comparison of @p l and @p r)
10121 /// update their canonical type.  That way, two types of the same name
10122 /// are structurally compared to each other only once.  So the
10123 /// non-linear structural comparison of two types of the same name
10124 /// only happen once.
10125 ///
10126 /// @return true iff @p l equals @p r.
10127 static bool
compare_dies(const read_context & ctxt,const Dwarf_Die * l,const Dwarf_Die * r,istring_set_type & aggregates_being_compared,bool update_canonical_dies_on_the_fly)10128 compare_dies(const read_context& ctxt,
10129 	     const Dwarf_Die *l, const Dwarf_Die *r,
10130 	     istring_set_type& aggregates_being_compared,
10131 	     bool update_canonical_dies_on_the_fly)
10132 {
10133   ABG_ASSERT(l);
10134   ABG_ASSERT(r);
10135 
10136   int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10137     r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10138 
10139   if (l_tag != r_tag)
10140     return false;
10141 
10142   Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l)),
10143     r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10144   Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10145   const die_source l_die_source = ctxt.get_die_source(l);
10146   const die_source r_die_source = ctxt.get_die_source(r);
10147 
10148   // If 'l' and 'r' already have canonical DIEs, then just compare the
10149   // offsets of their canonical DIEs.
10150   bool l_has_canonical_die_offset =
10151     (l_canonical_die_offset =
10152      ctxt.get_canonical_die_offset(l_offset, l_die_source,
10153 				   /*die_as_type=*/true));
10154 
10155   bool r_has_canonical_die_offset =
10156     (r_canonical_die_offset =
10157      ctxt.get_canonical_die_offset(r_offset, r_die_source,
10158 				   /*die_as_type=*/true));
10159 
10160   if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10161     return l_canonical_die_offset == r_canonical_die_offset;
10162 
10163   bool result = true;
10164 
10165   switch (l_tag)
10166     {
10167     case DW_TAG_base_type:
10168     case DW_TAG_string_type:
10169       if (!compare_as_type_dies(l, r)
10170 	  || !compare_as_decl_dies(l, r))
10171 	result = false;
10172       break;
10173 
10174     case DW_TAG_typedef:
10175     case DW_TAG_pointer_type:
10176     case DW_TAG_reference_type:
10177     case DW_TAG_rvalue_reference_type:
10178     case DW_TAG_const_type:
10179     case DW_TAG_volatile_type:
10180     case DW_TAG_restrict_type:
10181       {
10182 	if (!compare_as_type_dies(l, r))
10183 	  {
10184 	    result = false;
10185 	    break;
10186 	  }
10187 
10188 	bool from_the_same_tu = false;
10189 	if (!pointer_or_qual_die_of_anonymous_class_type(l)
10190 	    && compare_dies_cu_decl_file(l, r, from_the_same_tu)
10191 	    && from_the_same_tu)
10192 	  {
10193 	    // These two typedefs, pointer, reference, or qualified
10194 	    // types have the same name and are defined in the same TU.
10195 	    // They thus ought to be the same.
10196 	    //
10197 	    // Note that pointers, reference or qualified types to
10198 	    // anonymous types are not taking into account here because
10199 	    // those always need to be structurally compared.
10200 	    result = true;
10201 	    break;
10202 	  }
10203       }
10204 
10205       {
10206 	// No fancy optimization in this case.  We need to
10207 	// structurally compare the two DIEs.
10208 	Dwarf_Die lu_type_die, ru_type_die;
10209 	bool lu_is_void, ru_is_void;
10210 
10211 	lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
10212 	ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
10213 
10214 	if (lu_is_void && ru_is_void)
10215 	  result = true;
10216 	else if (lu_is_void != ru_is_void)
10217 	  result = false;
10218 	else
10219 	  result = compare_dies(ctxt, &lu_type_die, &ru_type_die,
10220 				aggregates_being_compared,
10221 				update_canonical_dies_on_the_fly);
10222       }
10223       break;
10224 
10225     case DW_TAG_enumeration_type:
10226       if (!compare_as_type_dies(l, r)
10227 	  || !compare_as_decl_dies(l, r))
10228 	result = false;
10229       else
10230 	{
10231 	  // Walk the enumerators.
10232 	  Dwarf_Die l_enumtor, r_enumtor;
10233 	  bool found_l_enumtor, found_r_enumtor;
10234 
10235 	  for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
10236 					     &l_enumtor) == 0,
10237 		 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
10238 					       &r_enumtor) == 0;
10239 	       found_l_enumtor && found_r_enumtor;
10240 	       found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
10241 		 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
10242 	    {
10243 	      int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
10244 	      if ( l_tag != r_tag)
10245 		{
10246 		  result = false;
10247 		  break;
10248 		}
10249 
10250 	      if (l_tag != DW_TAG_enumerator)
10251 		continue;
10252 
10253 	      uint64_t l_val = 0, r_val = 0;
10254 	      die_unsigned_constant_attribute(&l_enumtor,
10255 					      DW_AT_const_value,
10256 					      l_val);
10257 	      die_unsigned_constant_attribute(&r_enumtor,
10258 					      DW_AT_const_value,
10259 					      r_val);
10260 	      if (l_val != r_val)
10261 		{
10262 		  result = false;
10263 		  break;
10264 		}
10265 	    }
10266 	  if (found_l_enumtor != found_r_enumtor )
10267 	    result = false;
10268 
10269 	}
10270       break;
10271 
10272     case DW_TAG_structure_type:
10273     case DW_TAG_union_type:
10274       {
10275 	interned_string ln = ctxt.get_die_pretty_type_representation(l, 0);
10276 	interned_string rn = ctxt.get_die_pretty_type_representation(r, 0);
10277 
10278 	if ((aggregates_being_compared.find(ln)
10279 	     != aggregates_being_compared.end())
10280 	    || (aggregates_being_compared.find(rn)
10281 		!= aggregates_being_compared.end()))
10282 	  result = true;
10283 	else if (!compare_as_decl_dies(l, r))
10284 	  result = false;
10285 	else if (!compare_as_type_dies(l, r))
10286 	  result = false;
10287 	else
10288 	  {
10289 	    aggregates_being_compared.insert(ln);
10290 	    aggregates_being_compared.insert(rn);
10291 
10292 	    Dwarf_Die l_member, r_member;
10293 	    bool found_l_member, found_r_member;
10294 	    for (found_l_member = dwarf_child(const_cast<Dwarf_Die*>(l),
10295 					      &l_member) == 0,
10296 		   found_r_member = dwarf_child(const_cast<Dwarf_Die*>(r),
10297 						&r_member) == 0;
10298 		 found_l_member && found_r_member;
10299 		 found_l_member = dwarf_siblingof(&l_member, &l_member) == 0,
10300 		   found_r_member = dwarf_siblingof(&r_member, &r_member) == 0)
10301 	      {
10302 		int l_tag = dwarf_tag(&l_member), r_tag = dwarf_tag(&r_member);
10303 		if (l_tag != r_tag)
10304 		  {
10305 		    result = false;
10306 		    break;
10307 		  }
10308 
10309 		if (l_tag != DW_TAG_member && l_tag != DW_TAG_variable)
10310 		  continue;
10311 
10312 		if (!compare_dies(ctxt, &l_member, &r_member,
10313 				  aggregates_being_compared,
10314 				  update_canonical_dies_on_the_fly))
10315 		  {
10316 		    result = false;
10317 		    break;
10318 		  }
10319 	      }
10320 	    if (found_l_member != found_r_member)
10321 	      result = false;
10322 
10323 	    aggregates_being_compared.erase(ln);
10324 	    aggregates_being_compared.erase(rn);
10325 	  }
10326       }
10327       break;
10328 
10329     case DW_TAG_array_type:
10330       {
10331 	Dwarf_Die l_child, r_child;
10332 	bool found_l_child, found_r_child;
10333 	for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
10334 					 &l_child) == 0,
10335 	       found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
10336 					   &r_child) == 0;
10337 	     found_l_child && found_r_child;
10338 	     found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
10339 	       found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
10340 	  {
10341 	    int l_child_tag = dwarf_tag(&l_child),
10342 	      r_child_tag = dwarf_tag(&r_child);
10343 	    if (l_child_tag == DW_TAG_subrange_type
10344 		|| r_child_tag == DW_TAG_subrange_type)
10345 	      if (!compare_dies(ctxt, &l_child, &r_child,
10346 				aggregates_being_compared,
10347 				update_canonical_dies_on_the_fly))
10348 		{
10349 		  result = false;
10350 		  break;
10351 		}
10352 	  }
10353 	if (found_l_child != found_r_child)
10354 	  result = false;
10355 	// Compare the types of the elements of the array.
10356 	Dwarf_Die ltype_die, rtype_die;
10357 	bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
10358 	bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
10359 	ABG_ASSERT(found_ltype && found_rtype);
10360 
10361 	if (!compare_dies(ctxt, &ltype_die, &rtype_die,
10362 			  aggregates_being_compared,
10363 			  update_canonical_dies_on_the_fly))
10364 	  return false;
10365       }
10366       break;
10367 
10368     case DW_TAG_subrange_type:
10369       {
10370 	uint64_t l_lower_bound = 0, r_lower_bound = 0,
10371 	  l_upper_bound = 0, r_upper_bound = 0;
10372 	die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
10373 	die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
10374 	if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
10375 					     l_upper_bound))
10376 	  {
10377 	    uint64_t l_count = 0;
10378 	    if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
10379 	      {
10380 		l_upper_bound = l_lower_bound + l_count;
10381 		if (l_upper_bound)
10382 		  --l_upper_bound;
10383 	      }
10384 	  }
10385 	if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
10386 					     r_upper_bound))
10387 	  {
10388 	    uint64_t r_count = 0;
10389 	    if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
10390 	      {
10391 		r_upper_bound = r_lower_bound + r_count;
10392 		if (r_upper_bound)
10393 		  --r_upper_bound;
10394 	      }
10395 	  }
10396 
10397 	if ((l_lower_bound != r_lower_bound)
10398 	    || (l_upper_bound != r_upper_bound))
10399 	  result = false;
10400       }
10401       break;
10402 
10403     case DW_TAG_subroutine_type:
10404     case DW_TAG_subprogram:
10405       {
10406 	interned_string ln = ctxt.get_die_pretty_type_representation(l, 0);
10407 	interned_string rn = ctxt.get_die_pretty_type_representation(r, 0);
10408 
10409 	if ((aggregates_being_compared.find(ln)
10410 	     != aggregates_being_compared.end())
10411 	    || (aggregates_being_compared.find(rn)
10412 		!= aggregates_being_compared.end()))
10413 	  {
10414 	    result = true;
10415 	    break;
10416 	  }
10417 	else if (l_tag == DW_TAG_subroutine_type)
10418 	  {
10419 	    // So, we are looking at types that are pointed to by a
10420 	    // function pointer.  These are not real concrete function
10421 	    // types, rather, they denote interfaces of functions.
10422 	    //
10423 	    // If the textual representations are different, then
10424 	    // obviously they are different DIEs.
10425 	    if (ln != rn)
10426 	      {
10427 		result = false;
10428 		break;
10429 	      }
10430 
10431 	    // So if their textual representation are the same and
10432 	    // they come from the same TU, then they represent the
10433 	    // same DIE.
10434 	    bool from_the_same_tu = false;
10435 	    if (compare_dies_cu_decl_file(l, r, from_the_same_tu)
10436 		&& from_the_same_tu)
10437 	      {
10438 		result = true;
10439 		break;
10440 	      }
10441 	  }
10442 
10443 	if (l_tag == DW_TAG_subprogram
10444 	    && !fn_die_equal_by_linkage_name(ctxt, l, r))
10445 	  {
10446 	    result = false;
10447 	    break;
10448 	  }
10449 	else if (l_tag == DW_TAG_subprogram
10450 		 && ctxt.die_is_in_c(l) && ctxt.die_is_in_c(r)
10451 		 /*&& fn_die_equal_by_linkage_name(ctxt, l, r)*/)
10452 	  {
10453 	    result = true;
10454 	    break;
10455 	  }
10456 	else if (!ctxt.die_is_in_c(l) && !ctxt.die_is_in_c(r))
10457 	  {
10458 	    // In C, we cannot have two different functions with the
10459 	    // same linkage name in a given binary.  But here we are
10460 	    // looking at DIEs that don't originate from C.  So we
10461 	    // need to compare return types and parameter types.
10462 	    Dwarf_Die l_return_type, r_return_type;
10463 	    bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
10464 							    l_return_type);
10465 	    bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
10466 							    r_return_type);
10467 	    if (l_return_type_is_void != r_return_type_is_void
10468 		|| (!l_return_type_is_void
10469 		    && !compare_dies(ctxt,
10470 				     &l_return_type, &r_return_type,
10471 				     aggregates_being_compared,
10472 				     update_canonical_dies_on_the_fly)))
10473 	      result = false;
10474 	    else
10475 	      {
10476 		Dwarf_Die l_child, r_child;
10477 		bool found_l_child, found_r_child;
10478 		for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
10479 						 &l_child) == 0,
10480 		       found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
10481 						   &r_child) == 0;
10482 		     found_l_child && found_r_child;
10483 		     found_l_child = dwarf_siblingof(&l_child,
10484 						     &l_child) == 0,
10485 		       found_r_child = dwarf_siblingof(&r_child,
10486 						       &r_child)==0)
10487 		  {
10488 		    int l_child_tag = dwarf_tag(&l_child);
10489 		    int r_child_tag = dwarf_tag(&r_child);
10490 		    if (l_child_tag != r_child_tag
10491 			|| (l_child_tag == DW_TAG_formal_parameter
10492 			    && !compare_dies(ctxt, &l_child, &r_child,
10493 					     aggregates_being_compared,
10494 					     update_canonical_dies_on_the_fly)))
10495 		      {
10496 			result = false;
10497 			break;
10498 		      }
10499 		  }
10500 		if (found_l_child != found_r_child)
10501 		  result = false;
10502 	      }
10503 	  }
10504 
10505 	aggregates_being_compared.erase(ln);
10506 	aggregates_being_compared.erase(rn);
10507       }
10508       break;
10509 
10510     case DW_TAG_formal_parameter:
10511       {
10512 	Dwarf_Die l_type, r_type;
10513 	bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
10514 	bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
10515 	if ((l_type_is_void != r_type_is_void)
10516 	    || !compare_dies(ctxt, &l_type, &r_type,
10517 			     aggregates_being_compared,
10518 			     update_canonical_dies_on_the_fly))
10519 	  result = false;
10520       }
10521       break;
10522 
10523     case DW_TAG_variable:
10524     case DW_TAG_member:
10525       if (compare_as_decl_dies(l, r))
10526 	{
10527 	  // Compare the offsets of the data members
10528 	  if (l_tag == DW_TAG_member)
10529 	    {
10530 	      int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
10531 	      die_member_offset(ctxt, l, l_offset_in_bits);
10532 	      die_member_offset(ctxt, r, r_offset_in_bits);
10533 	      if (l_offset_in_bits != r_offset_in_bits)
10534 		result = false;
10535 	    }
10536 	  if (result)
10537 	    {
10538 	      // Compare the types of the data members or variables.
10539 	      Dwarf_Die l_type, r_type;
10540 	      ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
10541 	      ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
10542 	      if (aggregates_being_compared.size () < 5)
10543 		{
10544 		  if (!compare_dies(ctxt, &l_type, &r_type,
10545 				    aggregates_being_compared,
10546 				    update_canonical_dies_on_the_fly))
10547 		    result = false;
10548 		}
10549 	      else
10550 		{
10551 		  if (!compare_as_type_dies(&l_type, &r_type)
10552 		      ||!compare_as_decl_dies(&l_type, &r_type))
10553 		    return false;
10554 		}
10555 	    }
10556 	}
10557       else
10558 	result = false;
10559       break;
10560 
10561     case DW_TAG_class_type:
10562     case DW_TAG_enumerator:
10563     case DW_TAG_packed_type:
10564     case DW_TAG_set_type:
10565     case DW_TAG_file_type:
10566     case DW_TAG_ptr_to_member_type:
10567     case DW_TAG_thrown_type:
10568     case DW_TAG_interface_type:
10569     case DW_TAG_unspecified_type:
10570     case DW_TAG_shared_type:
10571     case DW_TAG_compile_unit:
10572     case DW_TAG_namespace:
10573     case DW_TAG_module:
10574     case DW_TAG_constant:
10575     case DW_TAG_partial_unit:
10576     case DW_TAG_imported_unit:
10577     case DW_TAG_dwarf_procedure:
10578     case DW_TAG_imported_declaration:
10579     case DW_TAG_entry_point:
10580     case DW_TAG_label:
10581     case DW_TAG_lexical_block:
10582     case DW_TAG_unspecified_parameters:
10583     case DW_TAG_variant:
10584     case DW_TAG_common_block:
10585     case DW_TAG_common_inclusion:
10586     case DW_TAG_inheritance:
10587     case DW_TAG_inlined_subroutine:
10588     case DW_TAG_with_stmt:
10589     case DW_TAG_access_declaration:
10590     case DW_TAG_catch_block:
10591     case DW_TAG_friend:
10592     case DW_TAG_namelist:
10593     case DW_TAG_namelist_item:
10594     case DW_TAG_template_type_parameter:
10595     case DW_TAG_template_value_parameter:
10596     case DW_TAG_try_block:
10597     case DW_TAG_variant_part:
10598     case DW_TAG_imported_module:
10599     case DW_TAG_condition:
10600     case DW_TAG_type_unit:
10601     case DW_TAG_template_alias:
10602     case DW_TAG_lo_user:
10603     case DW_TAG_MIPS_loop:
10604     case DW_TAG_format_label:
10605     case DW_TAG_function_template:
10606     case DW_TAG_class_template:
10607     case DW_TAG_GNU_BINCL:
10608     case DW_TAG_GNU_EINCL:
10609     case DW_TAG_GNU_template_template_param:
10610     case DW_TAG_GNU_template_parameter_pack:
10611     case DW_TAG_GNU_formal_parameter_pack:
10612     case DW_TAG_GNU_call_site:
10613     case DW_TAG_GNU_call_site_parameter:
10614     case DW_TAG_hi_user:
10615       ABG_ASSERT_NOT_REACHED;
10616     }
10617 
10618   if (result == true
10619       && update_canonical_dies_on_the_fly
10620       && is_canonicalizeable_type_tag(l_tag))
10621     {
10622       // If 'l' has no canonical DIE and if 'r' has one, then propagage
10623       // the canonical DIE of 'r' to 'l'.
10624       //
10625       // In case 'r' has no canonical DIE, then compute it, and then
10626       // propagate that canonical DIE to 'r'.
10627       const die_source l_source = ctxt.get_die_source(l);
10628       const die_source r_source = ctxt.get_die_source(r);
10629 
10630       if (!l_has_canonical_die_offset
10631 	  // A DIE can be equivalent only to another DIE of the same
10632 	  // source.
10633 	  && l_source == r_source)
10634 	{
10635 	  if (!r_has_canonical_die_offset)
10636 	    ctxt.compute_canonical_die_offset(r, r_canonical_die_offset,
10637 					      /*die_as_type=*/true);
10638 	  ABG_ASSERT(r_canonical_die_offset);
10639 	  ctxt.set_canonical_die_offset(l, r_canonical_die_offset,
10640 					/*die_as_type=*/true);
10641 	}
10642     }
10643   return result;
10644 }
10645 
10646 /// Compare two DIEs emitted by a C compiler.
10647 ///
10648 /// @param ctxt the read context used to load the DWARF information.
10649 ///
10650 /// @param l the left-hand-side argument of this comparison operator.
10651 ///
10652 /// @param r the righ-hand-side argument of this comparison operator.
10653 ///
10654 /// @param update_canonical_dies_on_the_fly if yes, then this function
10655 /// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
10656 /// comparing l and r.  This helps in making so that sub-type DIEs of
10657 /// 'l' and 'r' are compared structurally only once.  This is how we
10658 /// turn this exponential comparison problem into a problem that is a
10659 /// closer to a linear one.
10660 ///
10661 /// @return true iff @p l equals @p r.
10662 static bool
compare_dies(const read_context & ctxt,const Dwarf_Die * l,const Dwarf_Die * r,bool update_canonical_dies_on_the_fly)10663 compare_dies(const read_context& ctxt,
10664 	     const Dwarf_Die *l,
10665 	     const Dwarf_Die *r,
10666 	     bool update_canonical_dies_on_the_fly)
10667 {
10668   istring_set_type aggregates_being_compared;
10669   return compare_dies(ctxt, l, r, aggregates_being_compared,
10670 		      update_canonical_dies_on_the_fly);
10671 }
10672 
10673 // ----------------------------------
10674 // </die comparison engine>
10675 // ---------------------------------
10676 
10677 /// Get the point where a DW_AT_import DIE is used to import a given
10678 /// (unit) DIE, between two DIEs.
10679 ///
10680 /// @param ctxt the dwarf reading context to consider.
10681 ///
10682 /// @param partial_unit_offset the imported unit for which we want to
10683 /// know the insertion point.  This is usually a partial unit (with
10684 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
10685 /// so.
10686 ///
10687 /// @param first_die_offset the offset of the DIE from which this
10688 /// function starts looking for the import point of
10689 /// @partial_unit_offset.  Note that this offset is excluded from the
10690 /// set of potential solutions.
10691 ///
10692 /// @param first_die_cu_offset the offset of the (compilation) unit
10693 /// that @p first_die_cu_offset belongs to.
10694 ///
10695 /// @param source where the DIE of first_die_cu_offset unit comes
10696 /// from.
10697 ///
10698 /// @param last_die_offset the offset of the last DIE of the up to
10699 /// which this function looks for the import point of @p
10700 /// partial_unit_offset.  Note that this offset is excluded from the
10701 /// set of potential solutions.
10702 ///
10703 /// @param imported_point_offset.  The resulting
10704 /// imported_point_offset.  Note that if the imported DIE @p
10705 /// partial_unit_offset is not found between @p first_die_offset and
10706 /// @p last_die_offset, this parameter is left untouched by this
10707 /// function.
10708 ///
10709 /// @return true iff an imported unit is found between @p
10710 /// first_die_offset and @p last_die_offset.
10711 static bool
find_import_unit_point_between_dies(const read_context & ctxt,size_t partial_unit_offset,Dwarf_Off first_die_offset,Dwarf_Off first_die_cu_offset,die_source source,size_t last_die_offset,size_t & imported_point_offset)10712 find_import_unit_point_between_dies(const read_context& ctxt,
10713 				    size_t		partial_unit_offset,
10714 				    Dwarf_Off		first_die_offset,
10715 				    Dwarf_Off		first_die_cu_offset,
10716 				    die_source		source,
10717 				    size_t		last_die_offset,
10718 				    size_t&		imported_point_offset)
10719 {
10720   const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
10721     ctxt.tu_die_imported_unit_points_map(source);
10722 
10723   tu_die_imported_unit_points_map_type::const_iterator iter =
10724     tu_die_imported_unit_points_map.find(first_die_cu_offset);
10725 
10726   ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
10727 
10728   const imported_unit_points_type& imported_unit_points = iter->second;
10729   if (imported_unit_points.empty())
10730     return false;
10731 
10732   imported_unit_points_type::const_iterator b = imported_unit_points.begin();
10733   imported_unit_points_type::const_iterator e = imported_unit_points.end();
10734 
10735   find_lower_bound_in_imported_unit_points(imported_unit_points,
10736 					   first_die_offset,
10737 					   b);
10738 
10739   if (last_die_offset != static_cast<size_t>(-1))
10740     find_lower_bound_in_imported_unit_points(imported_unit_points,
10741 					     last_die_offset,
10742 					     e);
10743 
10744   if (e != imported_unit_points.end())
10745     {
10746       for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
10747 	if (i->imported_unit_die_off == partial_unit_offset)
10748 	  {
10749 	    imported_point_offset = i->offset_of_import ;
10750 	    return true;
10751 	  }
10752 
10753       for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
10754 	{
10755 	  if (find_import_unit_point_between_dies(ctxt,
10756 						  partial_unit_offset,
10757 						  i->imported_unit_child_off,
10758 						  i->imported_unit_cu_off,
10759 						  i->imported_unit_die_source,
10760 						  /*(Dwarf_Off)*/-1,
10761 						  imported_point_offset))
10762 	    return true;
10763 	}
10764     }
10765   else
10766     {
10767       for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
10768 	if (i->imported_unit_die_off == partial_unit_offset)
10769 	  {
10770 	    imported_point_offset = i->offset_of_import ;
10771 	    return true;
10772 	  }
10773 
10774       for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
10775 	{
10776 	  if (find_import_unit_point_between_dies(ctxt,
10777 						  partial_unit_offset,
10778 						  i->imported_unit_child_off,
10779 						  i->imported_unit_cu_off,
10780 						  i->imported_unit_die_source,
10781 						  /*(Dwarf_Off)*/-1,
10782 						  imported_point_offset))
10783 	    return true;
10784 	}
10785     }
10786 
10787   return false;
10788 }
10789 
10790 /// In the current translation unit, get the last point where a
10791 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
10792 /// given DIE is found.  That given DIE is called the limit DIE.
10793 ///
10794 /// Said otherwise, this function returns the last import point of a
10795 /// unit, before a limit.
10796 ///
10797 /// @param ctxt the dwarf reading context to consider.
10798 ///
10799 /// @param partial_unit_offset the imported unit for which we want to
10800 /// know the insertion point of.  This is usually a partial unit (with
10801 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
10802 /// so.
10803 ///
10804 /// @param where_offset the offset of the limit DIE.
10805 ///
10806 /// @param imported_point_offset.  The resulting imported_point_offset.
10807 /// Note that if the imported DIE @p partial_unit_offset is not found
10808 /// before @p die_offset, this is set to the last @p
10809 /// partial_unit_offset found under @p parent_die.
10810 ///
10811 /// @return true iff an imported unit is found before @p die_offset.
10812 /// Note that if an imported unit is found after @p die_offset then @p
10813 /// imported_point_offset is set and the function return false.
10814 static bool
find_import_unit_point_before_die(const read_context & ctxt,size_t partial_unit_offset,size_t where_offset,size_t & imported_point_offset)10815 find_import_unit_point_before_die(const read_context&	ctxt,
10816 				  size_t		partial_unit_offset,
10817 				  size_t		where_offset,
10818 				  size_t&		imported_point_offset)
10819 {
10820   size_t import_point_offset = 0;
10821   Dwarf_Die first_die_of_tu;
10822 
10823   if (dwarf_child(const_cast<Dwarf_Die*>(ctxt.cur_tu_die()),
10824 		  &first_die_of_tu) != 0)
10825     return false;
10826 
10827   Dwarf_Die cu_die_memory;
10828   Dwarf_Die *cu_die;
10829 
10830   cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
10831 		       &cu_die_memory, 0, 0);
10832 
10833   if (find_import_unit_point_between_dies(ctxt, partial_unit_offset,
10834 					  dwarf_dieoffset(&first_die_of_tu),
10835 					  dwarf_dieoffset(cu_die),
10836 					  /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
10837 					  where_offset,
10838 					  import_point_offset))
10839     {
10840       imported_point_offset = import_point_offset;
10841       return true;
10842     }
10843 
10844   if (import_point_offset)
10845     {
10846       imported_point_offset = import_point_offset;
10847       return true;
10848     }
10849 
10850   return false;
10851 }
10852 
10853 /// Return the parent DIE for a given DIE.
10854 ///
10855 /// Note that the function build_die_parent_map() must have been
10856 /// called before this one can work.  This function either succeeds or
10857 /// aborts the current process.
10858 ///
10859 /// @param ctxt the read context to consider.
10860 ///
10861 /// @param die the DIE for which we want the parent.
10862 ///
10863 /// @param parent_die the output parameter set to the parent die of
10864 /// @p die.  Its memory must be allocated and handled by the caller.
10865 ///
10866 /// @param where_offset the offset of the DIE where we are "logically"
10867 /// positionned at, in the DIE tree.  This is useful when @p die is
10868 /// e.g, DW_TAG_partial_unit that can be included in several places in
10869 /// the DIE tree.
10870 ///
10871 /// @return true if the function could get a parent DIE, false
10872 /// otherwise.
10873 static bool
get_parent_die(const read_context & ctxt,const Dwarf_Die * die,Dwarf_Die & parent_die,size_t where_offset)10874 get_parent_die(const read_context&	ctxt,
10875 	       const Dwarf_Die*	die,
10876 	       Dwarf_Die&		parent_die,
10877 	       size_t			where_offset)
10878 {
10879   ABG_ASSERT(ctxt.dwarf());
10880 
10881   const die_source source = ctxt.get_die_source(die);
10882 
10883   const offset_offset_map_type& m = ctxt.die_parent_map(source);
10884   offset_offset_map_type::const_iterator i =
10885     m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
10886 
10887   if (i == m.end())
10888     return false;
10889 
10890   switch (source)
10891     {
10892     case PRIMARY_DEBUG_INFO_DIE_SOURCE:
10893       ABG_ASSERT(dwarf_offdie(ctxt.dwarf(), i->second, &parent_die));
10894       break;
10895     case ALT_DEBUG_INFO_DIE_SOURCE:
10896       ABG_ASSERT(dwarf_offdie(ctxt.alt_dwarf(), i->second, &parent_die));
10897       break;
10898     case TYPE_UNIT_DIE_SOURCE:
10899       ABG_ASSERT(dwarf_offdie_types(ctxt.dwarf(), i->second, &parent_die));
10900       break;
10901     case NO_DEBUG_INFO_DIE_SOURCE:
10902     case NUMBER_OF_DIE_SOURCES:
10903       ABG_ASSERT_NOT_REACHED;
10904     }
10905 
10906   if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
10907     {
10908       if (where_offset == 0)
10909 	{
10910 	  parent_die = *ctxt.cur_tu_die();
10911 	  return true;
10912 	}
10913       size_t import_point_offset = 0;
10914       bool found =
10915 	find_import_unit_point_before_die(ctxt,
10916 					  dwarf_dieoffset(&parent_die),
10917 					  where_offset,
10918 					  import_point_offset);
10919       if (!found)
10920 	// It looks like parent_die (which comes from the alternate
10921 	// debug info file) hasn't been imported into this TU.  So,
10922 	// Let's assume its logical parent is the DIE of the current
10923 	// TU.
10924 	parent_die = *ctxt.cur_tu_die();
10925       else
10926 	{
10927 	  ABG_ASSERT(import_point_offset);
10928 	  Dwarf_Die import_point_die;
10929 	  ABG_ASSERT(dwarf_offdie(ctxt.dwarf(),
10930 			      import_point_offset,
10931 			      &import_point_die));
10932 	  return get_parent_die(ctxt, &import_point_die,
10933 				parent_die, where_offset);
10934 	}
10935     }
10936 
10937   return true;
10938 }
10939 
10940 /// Get the DIE representing the scope of a given DIE.
10941 ///
10942 /// Please note that when the DIE we are looking at has a
10943 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
10944 /// DIE is the parent DIE of the DIE referred to by that attribute.
10945 /// This is the only case where a scope DIE is different from the
10946 /// parent DIE of a given DIE.
10947 ///
10948 /// Also note that if the current translation unit is from C, then
10949 /// this returns the global scope.
10950 ///
10951 /// @param ctxt the reading context to use.
10952 ///
10953 /// @param die the DIE to consider.
10954 ///
10955 /// @param where_offset where we are logically at in the DIE stream.
10956 ///
10957 /// @param scope_die out parameter.  This is set to the resulting
10958 /// scope DIE iff the function returns true.
10959 static bool
get_scope_die(const read_context & ctxt,const Dwarf_Die * die,size_t where_offset,Dwarf_Die & scope_die)10960 get_scope_die(const read_context&	ctxt,
10961 	      const Dwarf_Die*		die,
10962 	      size_t			where_offset,
10963 	      Dwarf_Die&		scope_die)
10964 {
10965   if (is_c_language(ctxt.cur_transl_unit()->get_language()))
10966     {
10967       ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
10968       return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
10969     }
10970 
10971   Dwarf_Die logical_parent_die;
10972   if (die_die_attribute(die, DW_AT_specification,
10973 			logical_parent_die, false)
10974       || die_die_attribute(die, DW_AT_abstract_origin,
10975 			   logical_parent_die, false))
10976     return get_scope_die(ctxt, &logical_parent_die, where_offset, scope_die);
10977 
10978   if (!get_parent_die(ctxt, die, scope_die, where_offset))
10979     return false;
10980 
10981   if (dwarf_tag(&scope_die) == DW_TAG_subprogram
10982       || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
10983       || dwarf_tag(&scope_die) == DW_TAG_array_type)
10984     return get_scope_die(ctxt, &scope_die, where_offset, scope_die);
10985 
10986   return true;
10987 }
10988 
10989 /// Return the abigail IR node representing the scope of a given DIE.
10990 ///
10991 /// Note that it is the logical scope that is returned.  That is, if
10992 /// the DIE has a DW_AT_specification or DW_AT_abstract_origin
10993 /// attribute, it's the scope of the referred-to DIE (via these
10994 /// attributes) that is returned.
10995 ///
10996 /// Also note that if the current translation unit is from C, then
10997 /// this returns the global scope.
10998 ///
10999 /// @param ctxt the dwarf reading context to use.
11000 ///
11001 /// @param die the DIE to get the scope for.
11002 ///
11003 /// @param called_from_public_decl is true if this function has been
11004 /// initially called within the context of a public decl.
11005 ///
11006 /// @param where_offset the offset of the DIE where we are "logically"
11007 /// positionned at, in the DIE tree.  This is useful when @p die is
11008 /// e.g, DW_TAG_partial_unit that can be included in several places in
11009 /// the DIE tree.
11010 static scope_decl_sptr
get_scope_for_die(read_context & ctxt,Dwarf_Die * die,bool called_for_public_decl,size_t where_offset)11011 get_scope_for_die(read_context& ctxt,
11012 		  Dwarf_Die*	die,
11013 		  bool		called_for_public_decl,
11014 		  size_t	where_offset)
11015 {
11016   const die_source source_of_die = ctxt.get_die_source(die);
11017 
11018   translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11019   ctxt.get_die_language(die, die_lang);
11020   if (is_c_language(die_lang))
11021     {
11022       ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11023       return ctxt.global_scope();
11024     }
11025 
11026   Dwarf_Die cloned_die;
11027   if (die_die_attribute(die, DW_AT_specification, cloned_die, false)
11028       || die_die_attribute(die, DW_AT_abstract_origin, cloned_die, false))
11029     return get_scope_for_die(ctxt, &cloned_die,
11030 			     called_for_public_decl,
11031 			     where_offset);
11032 
11033   Dwarf_Die parent_die;
11034 
11035   if (!get_parent_die(ctxt, die, parent_die, where_offset))
11036     return ctxt.nil_scope();
11037 
11038   if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
11039       || dwarf_tag(&parent_die) == DW_TAG_partial_unit
11040       || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11041     {
11042       if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
11043 	  || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11044 	{
11045 	  ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
11046 		 || source_of_die == TYPE_UNIT_DIE_SOURCE);
11047 	  return ctxt.cur_transl_unit()->get_global_scope();
11048 	}
11049 
11050       // For top level DIEs like DW_TAG_compile_unit, we just want to
11051       // return the global scope for the corresponding translation
11052       // unit.  This must have been set by
11053       // build_translation_unit_and_add_to_ir if we already started to
11054       // build the translation unit of parent_die.  Otherwise, just
11055       // return the global scope of the current translation unit.
11056       die_tu_map_type::const_iterator i =
11057 	ctxt.die_tu_map().find(dwarf_dieoffset(&parent_die));
11058       if (i != ctxt.die_tu_map().end())
11059 	return i->second->get_global_scope();
11060       return ctxt.cur_transl_unit()->get_global_scope();
11061     }
11062 
11063   scope_decl_sptr s;
11064   type_or_decl_base_sptr d;
11065   if (dwarf_tag(&parent_die) == DW_TAG_subprogram
11066       || dwarf_tag(&parent_die) == DW_TAG_array_type)
11067     // this is an entity defined in a scope that is a function.
11068     // Normally, I would say that this should be dropped.  But I have
11069     // seen a case where a typedef DIE needed by a function parameter
11070     // was defined right before the parameter, under the scope of the
11071     // function.  Yeah, weird.  So if I drop the typedef DIE, I'd drop
11072     // the function parm too.  So for that case, let's say that the
11073     // scope is the scope of the function itself.  Note that this is
11074     // an error of the DWARF emitter.  We should never see this DIE in
11075     // this context.
11076     {
11077       scope_decl_sptr s = get_scope_for_die(ctxt, &parent_die,
11078 					    called_for_public_decl,
11079 					    where_offset);
11080       if (is_anonymous_type_die(die))
11081 	// For anonymous type that have nothing to do in a function or
11082 	// array type context, let's put it in the containing
11083 	// namespace.  That is, do not let it be in a containing class
11084 	// or union where it has nothing to do.
11085 	while (is_class_or_union_type(s))
11086 	  {
11087 	    if (!get_parent_die(ctxt, &parent_die, parent_die, where_offset))
11088 	      return ctxt.nil_scope();
11089 	    s = get_scope_for_die(ctxt, &parent_die,
11090 				  called_for_public_decl,
11091 				  where_offset);
11092 	  }
11093       return s;
11094     }
11095   else
11096     d = build_ir_node_from_die(ctxt, &parent_die,
11097 			       called_for_public_decl,
11098 			       where_offset);
11099   s =  dynamic_pointer_cast<scope_decl>(d);
11100   if (!s)
11101     // this is an entity defined in someting that is not a scope.
11102     // Let's drop it.
11103     return ctxt.nil_scope();
11104 
11105   class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
11106   if (cl && cl->get_is_declaration_only())
11107     {
11108       scope_decl_sptr scop  =
11109 	dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
11110       if (scop)
11111 	s = scop;
11112       else
11113 	s = cl;
11114     }
11115   return s;
11116 }
11117 
11118 /// Convert a DWARF constant representing the value of the
11119 /// DW_AT_language property into the translation_unit::language
11120 /// enumerator.
11121 ///
11122 /// @param l the DWARF constant to convert.
11123 ///
11124 /// @return the resulting translation_unit::language enumerator.
11125 static translation_unit::language
dwarf_language_to_tu_language(size_t l)11126 dwarf_language_to_tu_language(size_t l)
11127 {
11128   switch (l)
11129     {
11130     case DW_LANG_C89:
11131       return translation_unit::LANG_C89;
11132     case DW_LANG_C:
11133       return translation_unit::LANG_C;
11134     case DW_LANG_Ada83:
11135       return translation_unit::LANG_Ada83;
11136     case DW_LANG_C_plus_plus:
11137       return translation_unit::LANG_C_plus_plus;
11138     case DW_LANG_Cobol74:
11139       return translation_unit::LANG_Cobol74;
11140     case DW_LANG_Cobol85:
11141       return translation_unit::LANG_Cobol85;
11142     case DW_LANG_Fortran77:
11143       return translation_unit::LANG_Fortran77;
11144     case DW_LANG_Fortran90:
11145       return translation_unit::LANG_Fortran90;
11146     case DW_LANG_Pascal83:
11147       return translation_unit::LANG_Pascal83;
11148     case DW_LANG_Modula2:
11149       return translation_unit::LANG_Modula2;
11150     case DW_LANG_Java:
11151       return translation_unit::LANG_Java;
11152     case DW_LANG_C99:
11153       return translation_unit::LANG_C99;
11154     case DW_LANG_Ada95:
11155       return translation_unit::LANG_Ada95;
11156     case DW_LANG_Fortran95:
11157       return translation_unit::LANG_Fortran95;
11158     case DW_LANG_PLI:
11159       return translation_unit::LANG_PLI;
11160     case DW_LANG_ObjC:
11161       return translation_unit::LANG_ObjC;
11162     case DW_LANG_ObjC_plus_plus:
11163       return translation_unit::LANG_ObjC_plus_plus;
11164 
11165 #ifdef HAVE_DW_LANG_Rust_enumerator
11166     case DW_LANG_Rust:
11167       return translation_unit::LANG_Rust;
11168 #endif
11169 
11170 #ifdef HAVE_DW_LANG_UPC_enumerator
11171     case DW_LANG_UPC:
11172       return translation_unit::LANG_UPC;
11173 #endif
11174 
11175 #ifdef HAVE_DW_LANG_D_enumerator
11176     case DW_LANG_D:
11177       return translation_unit::LANG_D;
11178 #endif
11179 
11180 #ifdef HAVE_DW_LANG_Python_enumerator
11181     case DW_LANG_Python:
11182       return translation_unit::LANG_Python;
11183 #endif
11184 
11185 #ifdef HAVE_DW_LANG_Go_enumerator
11186     case DW_LANG_Go:
11187       return translation_unit::LANG_Go;
11188 #endif
11189 
11190 #ifdef HAVE_DW_LANG_C11_enumerator
11191     case DW_LANG_C11:
11192       return translation_unit::LANG_C11;
11193 #endif
11194 
11195 #ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
11196       case DW_LANG_C_plus_plus_03:
11197 	return translation_unit::LANG_C_plus_plus_03;
11198 #endif
11199 
11200 #ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
11201     case DW_LANG_C_plus_plus_11:
11202       return translation_unit::LANG_C_plus_plus_11;
11203 #endif
11204 
11205 #ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
11206     case DW_LANG_C_plus_plus_14:
11207       return translation_unit::LANG_C_plus_plus_14;
11208 #endif
11209 
11210 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
11211     case DW_LANG_Mips_Assembler:
11212       return translation_unit::LANG_Mips_Assembler;
11213 #endif
11214 
11215     default:
11216       return translation_unit::LANG_UNKNOWN;
11217     }
11218 }
11219 
11220 /// Get the default array lower bound value as defined by the DWARF
11221 /// specification, version 4, depending on the language of the
11222 /// translation unit.
11223 ///
11224 /// @param l the language of the translation unit.
11225 ///
11226 /// @return the default array lower bound value.
11227 static uint64_t
get_default_array_lower_bound(translation_unit::language l)11228 get_default_array_lower_bound(translation_unit::language l)
11229 {
11230   int value = 0;
11231   switch (l)
11232     {
11233     case translation_unit::LANG_UNKNOWN:
11234       value = 0;
11235       break;
11236     case translation_unit::LANG_Cobol74:
11237     case translation_unit::LANG_Cobol85:
11238       value = 1;
11239       break;
11240     case translation_unit::LANG_C89:
11241     case translation_unit::LANG_C99:
11242     case translation_unit::LANG_C11:
11243     case translation_unit::LANG_C:
11244     case translation_unit::LANG_C_plus_plus_03:
11245     case translation_unit::LANG_C_plus_plus_11:
11246     case translation_unit::LANG_C_plus_plus_14:
11247     case translation_unit::LANG_C_plus_plus:
11248     case translation_unit::LANG_ObjC:
11249     case translation_unit::LANG_ObjC_plus_plus:
11250     case translation_unit::LANG_Rust:
11251       value = 0;
11252       break;
11253     case translation_unit::LANG_Fortran77:
11254     case translation_unit::LANG_Fortran90:
11255     case translation_unit::LANG_Fortran95:
11256     case translation_unit::LANG_Ada83:
11257     case translation_unit::LANG_Ada95:
11258     case translation_unit::LANG_Pascal83:
11259     case translation_unit::LANG_Modula2:
11260       value = 1;
11261       break;
11262     case translation_unit::LANG_Java:
11263       value = 0;
11264       break;
11265     case translation_unit::LANG_PLI:
11266       value = 1;
11267       break;
11268     case translation_unit::LANG_UPC:
11269     case translation_unit::LANG_D:
11270     case translation_unit::LANG_Python:
11271     case translation_unit::LANG_Go:
11272     case translation_unit::LANG_Mips_Assembler:
11273       value = 0;
11274       break;
11275     }
11276 
11277   return value;
11278 }
11279 
11280 /// For a given offset, find the lower bound of a sorted vector of
11281 /// imported unit point offset.
11282 ///
11283 /// The lower bound is the smallest point (the point with the smallest
11284 /// offset) which is the greater than a given offset.
11285 ///
11286 /// @param imported_unit_points_type the sorted vector  of imported
11287 /// unit points.
11288 ///
11289 /// @param val the offset to consider when looking for the lower
11290 /// bound.
11291 ///
11292 /// @param r an iterator to the lower bound found.  This parameter is
11293 /// set iff the function returns true.
11294 ///
11295 /// @return true iff the lower bound has been found.
11296 static bool
find_lower_bound_in_imported_unit_points(const imported_unit_points_type & p,Dwarf_Off val,imported_unit_points_type::const_iterator & r)11297 find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
11298 					 Dwarf_Off val,
11299 					 imported_unit_points_type::const_iterator& r)
11300 {
11301   imported_unit_point v(val);
11302   imported_unit_points_type::const_iterator result =
11303     std::lower_bound(p.begin(), p.end(), v);
11304 
11305   bool is_ok = result != p.end();
11306 
11307   if (is_ok)
11308     r = result;
11309 
11310   return is_ok;
11311 }
11312 
11313 /// Given a DW_TAG_compile_unit, build and return the corresponding
11314 /// abigail::translation_unit ir node.  Note that this function
11315 /// recursively reads the children dies of the current DIE and
11316 /// populates the resulting translation unit.
11317 ///
11318 /// @param ctxt the read_context to use.
11319 ///
11320 /// @param die the DW_TAG_compile_unit DIE to consider.
11321 ///
11322 /// @param address_size the size of the addresses expressed in this
11323 /// translation unit in general.
11324 ///
11325 /// @return a pointer to the resulting translation_unit.
11326 static translation_unit_sptr
build_translation_unit_and_add_to_ir(read_context & ctxt,Dwarf_Die * die,char address_size)11327 build_translation_unit_and_add_to_ir(read_context&	ctxt,
11328 				     Dwarf_Die*	die,
11329 				     char		address_size)
11330 {
11331   translation_unit_sptr result;
11332 
11333   if (!die)
11334     return result;
11335   ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
11336 
11337   // Clear the part of the context that is dependent on the translation
11338   // unit we are reading.
11339   ctxt.clear_per_translation_unit_data();
11340 
11341   ctxt.cur_tu_die(die);
11342 
11343   string path = die_string_attribute(die, DW_AT_name);
11344   if (path == "<artificial>")
11345     {
11346       // This is a file artificially generated by the compiler, so its
11347       // name is '<artificial>'.  As we want all different translation
11348       // units to have unique path names, let's suffix this path name
11349       // with its die offset.
11350       std::ostringstream o;
11351       o << path << "-" << std::hex << dwarf_dieoffset(die);
11352       path = o.str();
11353     }
11354   string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
11355 
11356   // See if the same translation unit exits already in the current
11357   // corpus.  Sometimes, the same translation unit can be present
11358   // several times in the same debug info.  The content of the
11359   // different instances of the translation unit are different.  So to
11360   // represent that, we are going to re-use the same translation
11361   // unit.  That is, it's going to be the union of all the translation
11362   // units of the same path.
11363   {
11364     const string& abs_path =
11365       compilation_dir.empty() ? path : compilation_dir + "/" + path;
11366     result = ctxt.current_corpus()->find_translation_unit(abs_path);
11367   }
11368 
11369   if (!result)
11370     {
11371       result.reset(new translation_unit(ctxt.env(),
11372 					path,
11373 					address_size));
11374       result->set_compilation_dir_path(compilation_dir);
11375       ctxt.current_corpus()->add(result);
11376       uint64_t l = 0;
11377       die_unsigned_constant_attribute(die, DW_AT_language, l);
11378       result->set_language(dwarf_language_to_tu_language(l));
11379     }
11380 
11381   ctxt.cur_transl_unit(result);
11382   ctxt.die_tu_map()[dwarf_dieoffset(die)] = result;
11383 
11384   Dwarf_Die child;
11385   if (dwarf_child(die, &child) != 0)
11386     return result;
11387 
11388   result->set_is_constructed(false);
11389 
11390   do
11391     build_ir_node_from_die(ctxt, &child,
11392 			   die_is_public_decl(&child),
11393 			   dwarf_dieoffset(&child));
11394   while (dwarf_siblingof(&child, &child) == 0);
11395 
11396   if (!ctxt.var_decls_to_re_add_to_tree().empty())
11397     for (list<var_decl_sptr>::const_iterator v =
11398 	   ctxt.var_decls_to_re_add_to_tree().begin();
11399 	 v != ctxt.var_decls_to_re_add_to_tree().end();
11400 	 ++v)
11401       {
11402 	if (is_member_decl(*v))
11403 	  continue;
11404 
11405 	ABG_ASSERT((*v)->get_scope());
11406 	string demangled_name =
11407 	  demangle_cplus_mangled_name((*v)->get_linkage_name());
11408 	if (!demangled_name.empty())
11409 	  {
11410 	    std::list<string> fqn_comps;
11411 	    fqn_to_components(demangled_name, fqn_comps);
11412 	    string mem_name = fqn_comps.back();
11413 	    fqn_comps.pop_back();
11414 	    class_decl_sptr class_type;
11415 	    string ty_name;
11416 	    if (!fqn_comps.empty())
11417 	      {
11418 		ty_name = components_to_type_name(fqn_comps);
11419 		class_type =
11420 		  lookup_class_type(ty_name, *ctxt.cur_transl_unit());
11421 	      }
11422 	    if (class_type)
11423 	      {
11424 		// So we are seeing a member variable for which there
11425 		// is a global variable definition DIE not having a
11426 		// reference attribute pointing back to the member
11427 		// variable declaration DIE.  Thus remove the global
11428 		// variable definition from its current non-class
11429 		// scope ...
11430 		decl_base_sptr d;
11431 		if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
11432 		  // This is the data member with the same name in cl.
11433 		  // We just need to flag it as static.
11434 		  ;
11435 		else
11436 		  {
11437 		    // In this case there is no data member with the
11438 		    // same name in cl already.  Let's add it there then
11439 		    // ...
11440 		    remove_decl_from_scope(*v);
11441 		    d = add_decl_to_scope(*v, class_type);
11442 		  }
11443 
11444 		ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
11445 		// Let's flag the data member as static.
11446 		set_member_is_static(d, true);
11447 	      }
11448 	  }
11449       }
11450   ctxt.var_decls_to_re_add_to_tree().clear();
11451 
11452   result->set_is_constructed(true);
11453 
11454   return result;
11455 }
11456 
11457 /// Build a abigail::namespace_decl out of a DW_TAG_namespace or
11458 /// DW_TAG_module (for fortran) DIE.
11459 ///
11460 /// Note that this function connects the DW_TAG_namespace to the IR
11461 /// being currently created, reads the children of the DIE and
11462 /// connects them to the IR as well.
11463 ///
11464 /// @param ctxt the read context to use.
11465 ///
11466 /// @param die the DIE to read from.  Must be either DW_TAG_namespace
11467 /// or DW_TAG_module.
11468 ///
11469 /// @param where_offset the offset of the DIE where we are "logically"
11470 /// positionned at, in the DIE tree.  This is useful when @p die is
11471 /// e.g, DW_TAG_partial_unit that can be included in several places in
11472 /// the DIE tree.
11473 ///
11474 /// @return the resulting @ref abigail::namespace_decl or NULL if it
11475 /// couldn't be created.
11476 static namespace_decl_sptr
build_namespace_decl_and_add_to_ir(read_context & ctxt,Dwarf_Die * die,size_t where_offset)11477 build_namespace_decl_and_add_to_ir(read_context&	ctxt,
11478 				   Dwarf_Die*		die,
11479 				   size_t		where_offset)
11480 {
11481   namespace_decl_sptr result;
11482 
11483   if (!die)
11484     return result;
11485 
11486   unsigned tag = dwarf_tag(die);
11487   if (tag != DW_TAG_namespace && tag != DW_TAG_module)
11488     return result;
11489 
11490   scope_decl_sptr scope = get_scope_for_die(ctxt, die,
11491 					    /*called_for_public_decl=*/false,
11492 					    where_offset);
11493 
11494   string name, linkage_name;
11495   location loc;
11496   die_loc_and_name(ctxt, die, loc, name, linkage_name);
11497 
11498   result.reset(new namespace_decl(ctxt.env(), name, loc));
11499   add_decl_to_scope(result, scope.get());
11500   ctxt.associate_die_to_decl(die, result, where_offset);
11501 
11502   Dwarf_Die child;
11503   if (dwarf_child(die, &child) != 0)
11504     return result;
11505 
11506   ctxt.scope_stack().push(result.get());
11507   do
11508     build_ir_node_from_die(ctxt, &child,
11509 			   /*called_from_public_decl=*/false,
11510 			   where_offset);
11511   while (dwarf_siblingof(&child, &child) == 0);
11512   ctxt.scope_stack().pop();
11513 
11514   return result;
11515 }
11516 
11517 /// Build a @ref type_decl out of a DW_TAG_base_type DIE.
11518 ///
11519 /// @param ctxt the read context to use.
11520 ///
11521 /// @param die the DW_TAG_base_type to consider.
11522 ///
11523 /// @param where_offset where we are logically at in the DIE stream.
11524 ///
11525 /// @return the resulting decl_base_sptr.
11526 static type_decl_sptr
build_type_decl(read_context & ctxt,Dwarf_Die * die,size_t where_offset)11527 build_type_decl(read_context& ctxt, Dwarf_Die* die, size_t where_offset)
11528 {
11529   type_decl_sptr result;
11530 
11531   if (!die)
11532     return result;
11533   ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
11534 
11535   uint64_t byte_size = 0, bit_size = 0;
11536   if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
11537     if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
11538       return result;
11539 
11540   if (bit_size == 0 && byte_size != 0)
11541     // Update the bit size.
11542     bit_size = byte_size * 8;
11543 
11544   string type_name, linkage_name;
11545   location loc;
11546   die_loc_and_name(ctxt, die, loc, type_name, linkage_name);
11547 
11548   if (byte_size == 0)
11549     {
11550       // The size of the type is zero, that must mean that we are
11551       // looking at the definition of the void type.
11552       if (type_name == "void")
11553 	result = is_type_decl(build_ir_node_for_void_type(ctxt));
11554       else
11555 	// A type of size zero that is not void? Hmmh, I am not sure
11556 	// what that means.  Return nil for now.
11557 	return result;
11558     }
11559 
11560   if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
11561     {
11562       string normalized_type_name = type_name;
11563       integral_type int_type;
11564       if (parse_integral_type(type_name, int_type))
11565 	normalized_type_name = int_type.to_string();
11566       result = lookup_basic_type(normalized_type_name, *corp);
11567     }
11568 
11569   if (!result)
11570     if (corpus_sptr corp = ctxt.current_corpus())
11571       result = lookup_basic_type(type_name, *corp);
11572   if (!result)
11573     result.reset(new type_decl(ctxt.env(), type_name, bit_size,
11574 			       /*alignment=*/0, loc, linkage_name));
11575   ctxt.associate_die_to_type(die, result, where_offset);
11576   return result;
11577 }
11578 
11579 /// Construct the type that is to be used as the underlying type of an
11580 /// enum.
11581 ///
11582 /// @param ctxt the read context to use.
11583 ///
11584 /// @param enum_name the name of the enum that this type is going to
11585 /// be the underlying type of.
11586 ///
11587 /// @param enum_size the size of the enum.
11588 ///
11589 /// @param is_anonymous whether the underlying type is anonymous or
11590 /// not. By default, this should be set to true as before c++11 (and
11591 /// in C), it's almost the case.
11592 static type_decl_sptr
build_enum_underlying_type(read_context & ctxt,string enum_name,uint64_t enum_size,bool is_anonymous=true)11593 build_enum_underlying_type(read_context& ctxt,
11594 			   string enum_name,
11595 			   uint64_t enum_size,
11596 			   bool is_anonymous = true)
11597 {
11598   string underlying_type_name =
11599     build_internal_underlying_enum_type_name(enum_name, is_anonymous,
11600 					     enum_size);
11601 
11602   type_decl_sptr result(new type_decl(ctxt.env(), underlying_type_name,
11603 				      enum_size, enum_size, location()));
11604   result->set_is_anonymous(is_anonymous);
11605   result->set_is_artificial(true);
11606   translation_unit_sptr tu = ctxt.cur_transl_unit();
11607   decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
11608   result = dynamic_pointer_cast<type_decl>(d);
11609   ABG_ASSERT(result);
11610   canonicalize(result);
11611   return result;
11612 }
11613 
11614 /// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
11615 ///
11616 /// @param ctxt the read context to use.
11617 ///
11618 /// @param die the DIE to read from.
11619 ///
11620 /// @param scope the scope of the final enum.  Note that this function
11621 /// does *NOT* add the built type to this scope.  The scope is just so
11622 /// that the function knows how to name anonymous enums.
11623 ///
11624 /// @param is_declaration_only is true if the DIE denoted by @p die is
11625 /// a declaration-only DIE.
11626 ///
11627 /// @return the built enum_type_decl or NULL if it could not be built.
11628 static enum_type_decl_sptr
build_enum_type(read_context & ctxt,Dwarf_Die * die,scope_decl * scope,size_t where_offset,bool is_declaration_only)11629 build_enum_type(read_context&	ctxt,
11630 		Dwarf_Die*	die,
11631 		scope_decl*	scope,
11632 		size_t		where_offset,
11633 		bool		is_declaration_only)
11634 {
11635   enum_type_decl_sptr result;
11636   if (!die)
11637     return result;
11638 
11639   unsigned tag = dwarf_tag(die);
11640   if (tag != DW_TAG_enumeration_type)
11641     return result;
11642 
11643   string name, linkage_name;
11644   location loc;
11645   die_loc_and_name(ctxt, die, loc, name, linkage_name);
11646 
11647   bool is_anonymous = false;
11648   // If the enum is anonymous, let's give it a name.
11649   if (name.empty())
11650     {
11651       name = get_internal_anonymous_die_prefix_name(die);
11652       ABG_ASSERT(!name.empty());
11653       // But we remember that the type is anonymous.
11654       is_anonymous = true;
11655 
11656       if (size_t s = scope->get_num_anonymous_member_enums())
11657 	name = build_internal_anonymous_die_name(name, s);
11658     }
11659 
11660   bool use_odr = ctxt.odr_is_relevant(die);
11661   // If the type has location, then associate it to its
11662   // representation.  This way, all occurences of types with the same
11663   // representation (name) and location can be later detected as being
11664   // for the same type.
11665 
11666   if (!is_anonymous)
11667     {
11668       if (use_odr)
11669 	{
11670 	  if (enum_type_decl_sptr pre_existing_enum =
11671 	      is_enum_type(ctxt.lookup_artifact_from_die(die)))
11672 	    result = pre_existing_enum;
11673 	}
11674       else if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
11675 	{
11676 	  if (loc)
11677 	    result = lookup_enum_type_per_location(loc.expand(), *corp);
11678 	}
11679       else if (loc)
11680 	{
11681 	  if (enum_type_decl_sptr pre_existing_enum =
11682 	      is_enum_type(ctxt.lookup_artifact_from_die(die)))
11683 	    if (pre_existing_enum->get_location() == loc)
11684 	      result = pre_existing_enum;
11685 	}
11686 
11687       if (result)
11688 	{
11689 	  ctxt.associate_die_to_type(die, result, where_offset);
11690 	  return result;
11691 	}
11692     }
11693   // TODO: for anonymous enums, maybe have a map of loc -> enums so that
11694   // we can look them up?
11695 
11696   uint64_t size = 0;
11697   if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
11698     size *= 8;
11699   bool is_artificial = die_is_artificial(die);
11700 
11701   // for now we consider that underlying types of enums are all anonymous
11702   bool enum_underlying_type_is_anonymous= true;
11703 
11704   enum_type_decl::enumerators enms;
11705   Dwarf_Die child;
11706   if (dwarf_child(die, &child) == 0)
11707     {
11708       do
11709 	{
11710 	  if (dwarf_tag(&child) != DW_TAG_enumerator)
11711 	    continue;
11712 
11713 	  string n, m;
11714 	  location l;
11715 	  die_loc_and_name(ctxt, &child, l, n, m);
11716 	  uint64_t val = 0;
11717 	  die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
11718 	  enms.push_back(enum_type_decl::enumerator(ctxt.env(), n, val));
11719 	}
11720       while (dwarf_siblingof(&child, &child) == 0);
11721     }
11722 
11723   // DWARF up to version 4 (at least) doesn't seem to carry the
11724   // underlying type, so let's create an artificial one here, which
11725   // sole purpose is to be passed to the constructor of the
11726   // enum_type_decl type.
11727   type_decl_sptr t =
11728     build_enum_underlying_type(ctxt, name, size,
11729 			       enum_underlying_type_is_anonymous);
11730   t->set_is_declaration_only(is_declaration_only);
11731 
11732   result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
11733   result->set_is_anonymous(is_anonymous);
11734   result->set_is_declaration_only(is_declaration_only);
11735   result->set_is_artificial(is_artificial);
11736   ctxt.associate_die_to_type(die, result, where_offset);
11737 
11738   ctxt.maybe_schedule_declaration_only_enum_for_resolution(result);
11739 
11740   return result;
11741 }
11742 
11743 /// Once a function_decl has been built and added to a class as a
11744 /// member function, this function updates the information of the
11745 /// function_decl concerning the properties of its relationship with
11746 /// the member class.  That is, it updates properties like
11747 /// virtualness, access, constness, cdtorness, etc ...
11748 ///
11749 /// @param die the DIE of the function_decl that has been just built.
11750 ///
11751 /// @param f the function_decl that has just been built from @p die.
11752 ///
11753 /// @param klass the @ref class_or_union that @p f belongs to.
11754 ///
11755 /// @param ctxt the context used to read the ELF/DWARF information.
11756 static void
finish_member_function_reading(Dwarf_Die * die,const function_decl_sptr & f,const class_or_union_sptr klass,read_context & ctxt)11757 finish_member_function_reading(Dwarf_Die*			die,
11758 			       const function_decl_sptr&	f,
11759 			       const class_or_union_sptr	klass,
11760 			       read_context&			ctxt)
11761 {
11762   ABG_ASSERT(klass);
11763 
11764   method_decl_sptr m = is_method_decl(f);
11765   ABG_ASSERT(m);
11766 
11767   method_type_sptr method_t = is_method_type(m->get_type());
11768   ABG_ASSERT(method_t);
11769 
11770   bool is_ctor = (f->get_name() == klass->get_name());
11771   bool is_dtor = (!f->get_name().empty()
11772 		  && static_cast<string>(f->get_name())[0] == '~');
11773   bool is_virtual = die_is_virtual(die);
11774   int64_t vindex = -1;
11775   if (is_virtual)
11776     die_virtual_function_index(die, vindex);
11777   access_specifier access = public_access;
11778   if (class_decl_sptr c = is_class_type(klass))
11779     if (!c->is_struct())
11780       access = private_access;
11781   die_access_specifier(die, access);
11782 
11783   bool is_static = false;
11784   {
11785     // Let's see if the first parameter is a pointer to an instance of
11786     // the same class type as the current class and has a
11787     // DW_AT_artificial attribute flag set.  We are not looking at
11788     // DW_AT_object_pointer (for DWARF 3) because it wasn't being
11789     // emitted in GCC 4_4, which was already DWARF 3.
11790     function_decl::parameter_sptr first_parm;
11791     if (!f->get_parameters().empty())
11792       first_parm = f->get_parameters()[0];
11793 
11794     bool is_artificial = first_parm && first_parm->get_is_artificial();
11795     type_base_sptr this_ptr_type, other_klass;
11796 
11797     if (is_artificial)
11798       this_ptr_type = first_parm->get_type();
11799 
11800     // Sometimes, the type of the "this" pointer is "const class_type* const".
11801     //
11802     // Meaning that the "this pointer" itself is const qualified.  So
11803     // let's get the underlying underlying non-qualified pointer.
11804     if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
11805       this_ptr_type = q->get_underlying_type();
11806 
11807     // Now, get the pointed-to type.
11808     if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
11809       other_klass = p->get_pointed_to_type();
11810 
11811     // Sometimes, other_klass can be qualified; e.g, volatile.  In
11812     // that case, let's get the unqualified version of other_klass.
11813     if (qualified_type_def_sptr q = is_qualified_type(other_klass))
11814       other_klass = q->get_underlying_type();
11815 
11816     if (other_klass
11817 	&& get_type_name(other_klass) == klass->get_qualified_name())
11818       ;
11819     else
11820       is_static = true;
11821 
11822     if (is_static)
11823       {
11824 	// If we are looking at a DWARF version that is high enough
11825 	// for the DW_AT_object_pointer attribute to be present, let's
11826 	// see if it's present.  If it is, then the current member
11827 	// function is not static.
11828 	Dwarf_Die object_pointer_die;
11829 	if (die_has_object_pointer(die, object_pointer_die))
11830 	  is_static = false;
11831       }
11832   }
11833   set_member_access_specifier(m, access);
11834   if (vindex != -1)
11835     set_member_function_vtable_offset(m, vindex);
11836   if (is_virtual)
11837     set_member_function_is_virtual(m, is_virtual);
11838   set_member_is_static(m, is_static);
11839   set_member_function_is_ctor(m, is_ctor);
11840   set_member_function_is_dtor(m, is_dtor);
11841   set_member_function_is_const(m, method_t->get_is_const());
11842 
11843   ABG_ASSERT(is_member_function(m));
11844 
11845   if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
11846     {
11847       // This is a virtual member function which has a linkage name
11848       // but has no underlying symbol set.
11849       //
11850       // The underlying elf symbol to set to this function can show up
11851       // later in the DWARF input or it can be that, because of some
11852       // compiler optimization, the relation between this function and
11853       // its underlying elf symbol is simply not emitted in the DWARF.
11854       //
11855       // Let's thus schedule this function for a later fixup pass
11856       // (performed by
11857       // read_context::fixup_functions_with_no_symbols()) that will
11858       // set its underlying symbol.
11859       //
11860       // Note that if the underying symbol is encountered later in the
11861       // DWARF input, then the part of build_function_decl() that
11862       // updates the function to set its underlying symbol will
11863       // de-schedule this function wrt fixup pass.
11864       Dwarf_Off die_offset = dwarf_dieoffset(die);
11865       die_function_decl_map_type &fns_with_no_symbol =
11866 	ctxt.die_function_decl_with_no_symbol_map();
11867       die_function_decl_map_type::const_iterator i =
11868 	fns_with_no_symbol.find(die_offset);
11869       if (i == fns_with_no_symbol.end())
11870 	fns_with_no_symbol[die_offset] = f;
11871     }
11872 
11873 }
11874 
11875 /// If a function DIE has attributes which have not yet been read and
11876 /// added to the internal representation that represents that function
11877 /// then read those extra attributes and update the internal
11878 /// representation.
11879 ///
11880 /// @param ctxt the read context to use.
11881 ///
11882 /// @param die the function DIE to consider.
11883 ///
11884 /// @param where_offset where we logical are, currently, in the stream
11885 /// of DIEs.  If you don't know what this is, you can just set it to zero.
11886 ///
11887 /// @param existing_fn the representation of the function to update.
11888 ///
11889 /// @return the updated function  representation.
11890 static function_decl_sptr
maybe_finish_function_decl_reading(read_context & ctxt,Dwarf_Die * die,size_t where_offset,const function_decl_sptr & existing_fn)11891 maybe_finish_function_decl_reading(read_context&		ctxt,
11892 				   Dwarf_Die*			die,
11893 				   size_t			where_offset,
11894 				   const function_decl_sptr&	existing_fn)
11895 {
11896   function_decl_sptr result = build_function_decl(ctxt, die,
11897 						  where_offset,
11898 						  existing_fn);
11899 
11900   return result;
11901 }
11902 
11903 /// Lookup a class or a typedef with a given qualified name in the
11904 /// corpus that a given scope belongs to.
11905 ///
11906 /// @param scope the scope to consider.
11907 ///
11908 /// @param type_name the qualified name of the type to look for.
11909 ///
11910 /// @return the typedef or class type found.
11911 static type_base_sptr
lookup_class_or_typedef_from_corpus(scope_decl * scope,const string & type_name)11912 lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
11913 {
11914   string qname = build_qualified_name(scope, type_name);
11915   corpus* corp = scope->get_corpus();
11916   type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
11917   return result;
11918 }
11919 
11920 /// Lookup a class of typedef type from the current corpus being
11921 /// constructed.
11922 ///
11923 /// The type being looked for has the same name as a given DIE.
11924 ///
11925 /// @param ctxt the reading context to use.
11926 ///
11927 /// @param die the DIE which has the same name as the type we are
11928 /// looking for.
11929 ///
11930 /// @param called_for_public_decl whether this function is being
11931 /// called from a a publicly defined declaration.
11932 ///
11933 /// @param where_offset where we are logically at in the DIE stream.
11934 ///
11935 /// @return the type found.
11936 static type_base_sptr
lookup_class_or_typedef_from_corpus(read_context & ctxt,Dwarf_Die * die,bool called_for_public_decl,size_t where_offset)11937 lookup_class_or_typedef_from_corpus(read_context& ctxt,
11938 				    Dwarf_Die* die,
11939 				    bool called_for_public_decl,
11940 				    size_t where_offset)
11941 {
11942   if (!die)
11943     return class_decl_sptr();
11944 
11945   string class_name = die_string_attribute(die, DW_AT_name);
11946   if (class_name.empty())
11947     return class_decl_sptr();
11948 
11949   scope_decl_sptr scope = get_scope_for_die(ctxt, die,
11950 					    called_for_public_decl,
11951 					    where_offset);
11952   if (scope)
11953     return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
11954 
11955   return type_base_sptr();
11956 }
11957 
11958 /// Lookup a class, typedef or enum type with a given qualified name
11959 /// in the corpus that a given scope belongs to.
11960 ///
11961 /// @param scope the scope to consider.
11962 ///
11963 /// @param type_name the qualified name of the type to look for.
11964 ///
11965 /// @return the typedef, enum or class type found.
11966 static type_base_sptr
lookup_class_typedef_or_enum_type_from_corpus(scope_decl * scope,const string & type_name)11967 lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
11968 					      const string& type_name)
11969 {
11970   string qname = build_qualified_name(scope, type_name);
11971   corpus* corp = scope->get_corpus();
11972   type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
11973   return result;
11974 }
11975 
11976 /// Lookup a class, typedef or enum type in a given scope, in the
11977 /// corpus that scope belongs to.
11978 ///
11979 /// @param die the DIE of the class, typedef or enum to lookup.
11980 ///
11981 /// @param anonymous_member_type_idx if @p DIE represents an anonymous
11982 /// type, this is the index of that anonymous type in its scope, in
11983 /// case there are several anonymous types of the same kind in that
11984 /// scope.
11985 ///
11986 /// @param scope the scope in which to look the type for.
11987 ///
11988 /// @return the typedef, enum or class type found.
11989 static type_base_sptr
lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die * die,size_t anonymous_member_type_idx,scope_decl * scope)11990 lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
11991 					      size_t anonymous_member_type_idx,
11992 					      scope_decl* scope)
11993 {
11994   if (!die)
11995     return class_decl_sptr();
11996 
11997   string type_name = die_string_attribute(die, DW_AT_name);
11998   if (is_anonymous_type_die(die))
11999     type_name =
12000       get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12001 
12002   if (type_name.empty())
12003     return class_decl_sptr();
12004 
12005   return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
12006 }
12007 
12008 /// Test if a DIE represents a function that is a member of a given
12009 /// class type.
12010 ///
12011 /// @param ctxt the reading context.
12012 ///
12013 /// @param function_die the DIE of the function to consider.
12014 ///
12015 /// @param class_type the class type to consider.
12016 ///
12017 /// @param where_offset where we are logically at in the DIE stream.
12018 ///
12019 /// @return the method declaration corresponding to the member
12020 /// function of @p class_type, iff @p function_die is for a member
12021 /// function of @p class_type.
12022 static method_decl_sptr
is_function_for_die_a_member_of_class(read_context & ctxt,Dwarf_Die * function_die,const class_or_union_sptr & class_type)12023 is_function_for_die_a_member_of_class(read_context& ctxt,
12024 				      Dwarf_Die* function_die,
12025 				      const class_or_union_sptr& class_type)
12026 {
12027   type_or_decl_base_sptr artifact = ctxt.lookup_artifact_from_die(function_die);
12028 
12029   if (!artifact)
12030     return method_decl_sptr();
12031 
12032   method_decl_sptr method = is_method_decl(artifact);
12033   method_type_sptr method_type;
12034 
12035   if (method)
12036     method_type = method->get_type();
12037   else
12038     method_type = is_method_type(artifact);
12039   ABG_ASSERT(method_type);
12040 
12041   class_or_union_sptr method_class = method_type->get_class_type();
12042   ABG_ASSERT(method_class);
12043 
12044   string method_class_name = method_class->get_qualified_name(),
12045     class_type_name = class_type->get_qualified_name();
12046 
12047   if (method_class_name == class_type_name)
12048     {
12049       //ABG_ASSERT(class_type.get() == method_class.get());
12050       return method;
12051     }
12052 
12053   return method_decl_sptr();
12054 }
12055 
12056 /// If a given function DIE represents an existing member function of
12057 /// a given class, then update that member function with new
12058 /// properties present in the DIE.  Otherwise, if the DIE represents a
12059 /// new member function that is not already present in the class then
12060 /// add that new member function to the class.
12061 ///
12062 /// @param ctxt the reading context.
12063 ///
12064 /// @param function_die the DIE of the potential member function to
12065 /// consider.
12066 ///
12067 /// @param class_type the class type to consider.
12068 ///
12069 /// @param called_from_public_decl is true iff this function was
12070 /// called from a publicly defined and exported declaration.
12071 ///
12072 /// @param where_offset where we are logically at in the DIE stream.
12073 ///
12074 /// @return the method decl representing the member function.
12075 static method_decl_sptr
add_or_update_member_function(read_context & ctxt,Dwarf_Die * function_die,const class_or_union_sptr & class_type,bool called_from_public_decl,size_t where_offset)12076 add_or_update_member_function(read_context& ctxt,
12077 			      Dwarf_Die* function_die,
12078 			      const class_or_union_sptr& class_type,
12079 			      bool called_from_public_decl,
12080 			      size_t where_offset)
12081 {
12082   method_decl_sptr method =
12083     is_function_for_die_a_member_of_class(ctxt, function_die, class_type);
12084 
12085   if (!method)
12086     method = is_method_decl(build_ir_node_from_die(ctxt, function_die,
12087 						   class_type.get(),
12088 						   called_from_public_decl,
12089 						   where_offset));
12090   if (!method)
12091     return method_decl_sptr();
12092 
12093   finish_member_function_reading(function_die,
12094 				 is_function_decl(method),
12095 				 class_type, ctxt);
12096   return method;
12097 }
12098 
12099 /// Build a an IR node for class type from a DW_TAG_structure_type or
12100 /// DW_TAG_class_type DIE and add that node to the ABI corpus being
12101 /// currently built.
12102 ///
12103 /// If the represents class type that already exists, then update the
12104 /// existing class type with the new properties found in the DIE.
12105 ///
12106 /// It meanst that this function can also update an existing
12107 /// class_decl node with data members, member functions and other
12108 /// properties coming from the DIE.
12109 ///
12110 /// @param ctxt the read context to consider.
12111 ///
12112 /// @param die the DIE to read information from.  Must be either a
12113 /// DW_TAG_structure_type or a DW_TAG_class_type.
12114 ///
12115 /// @param scope a pointer to the scope_decl* under which this class
12116 /// is to be added to.
12117 ///
12118 /// @param is_struct whether the class was declared as a struct.
12119 ///
12120 /// @param klass if non-null, this is a klass to append the members
12121 /// to.  Otherwise, this function just builds the class from scratch.
12122 ///
12123 /// @param called_from_public_decl set to true if this class is being
12124 /// called from a "Public declaration like vars or public symbols".
12125 ///
12126 /// @param where_offset the offset of the DIE where we are "logically"
12127 /// positionned at, in the DIE tree.  This is useful when @p die is
12128 /// e.g, DW_TAG_partial_unit that can be included in several places in
12129 /// the DIE tree.
12130 ///
12131 /// @param is_declaration_only is true if the DIE denoted by @p die is
12132 /// a declaration-only DIE.
12133 ///
12134 /// @return the resulting class_type.
12135 static class_decl_sptr
add_or_update_class_type(read_context & ctxt,Dwarf_Die * die,scope_decl * scope,bool is_struct,class_decl_sptr klass,bool called_from_public_decl,size_t where_offset,bool is_declaration_only)12136 add_or_update_class_type(read_context&	 ctxt,
12137 			 Dwarf_Die*	 die,
12138 			 scope_decl*	 scope,
12139 			 bool		 is_struct,
12140 			 class_decl_sptr klass,
12141 			 bool		 called_from_public_decl,
12142 			 size_t		 where_offset,
12143 			 bool		 is_declaration_only)
12144 {
12145   class_decl_sptr result;
12146   if (!die)
12147     return result;
12148 
12149   const die_source source = ctxt.get_die_source(die);
12150 
12151   unsigned tag = dwarf_tag(die);
12152 
12153   if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
12154     return result;
12155 
12156   {
12157     die_class_or_union_map_type::const_iterator i =
12158       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12159     if (i != ctxt.die_wip_classes_map(source).end())
12160       {
12161 	class_decl_sptr class_type = is_class_type(i->second);
12162 	ABG_ASSERT(class_type);
12163 	return class_type;
12164       }
12165   }
12166 
12167   if (!ctxt.die_is_in_cplus_plus(die))
12168     // In c++, a given class might be put together "piecewise".  That
12169     // is, in a translation unit, some data members of that class
12170     // might be defined; then in another later, some member types
12171     // might be defined.  So we can't just re-use a class "verbatim"
12172     // just because we've seen previously.  So in c++, re-using the
12173     // class is a much clever process.  In the other languages however
12174     // (like in C) we can re-use a class definition verbatim.
12175     if (class_decl_sptr class_type =
12176 	is_class_type(ctxt.lookup_type_from_die(die)))
12177       if (!class_type->get_is_declaration_only())
12178 	return class_type;
12179 
12180   string name, linkage_name;
12181   location loc;
12182   die_loc_and_name(ctxt, die, loc, name, linkage_name);
12183 
12184   bool is_anonymous = false;
12185   if (name.empty())
12186     {
12187       // So we are looking at an anonymous struct.  Let's
12188       // give it a name.
12189       name = get_internal_anonymous_die_prefix_name(die);
12190       ABG_ASSERT(!name.empty());
12191       // But we remember that the type is anonymous.
12192       is_anonymous = true;
12193 
12194       if (size_t s = scope->get_num_anonymous_member_classes())
12195 	name = build_internal_anonymous_die_name(name, s);
12196     }
12197 
12198   if (!is_anonymous)
12199     {
12200       if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
12201 	{
12202 	  if (loc)
12203 	    // TODO: if there is only one class defined in the corpus
12204 	    // for this location, then re-use it.  But if there are
12205 	    // more than one, then do not re-use it, for now.
12206 	    result = lookup_class_type_per_location(loc.expand(), *corp);
12207 	  else
12208 	    // TODO: if there is just one class for that name defined,
12209 	    // then re-use it.  Otherwise, don't.
12210 	    result = lookup_class_type(name, *corp);
12211 	  if (result
12212 	      // If we are seeing a declaration of a definition we
12213 	      // already had, or if we are seing a type with the same
12214 	      // declaration-only-ness that we had before, then keep
12215 	      // the one we already had.
12216 	      && (result->get_is_declaration_only() == is_declaration_only
12217 		  || (!result->get_is_declaration_only()
12218 		      && is_declaration_only)))
12219 	    {
12220 	      ctxt.associate_die_to_type(die, result, where_offset);
12221 	      return result;
12222 	    }
12223 	  else
12224 	    // We might be seeing the definition of a declaration we
12225 	    // already had.  In that case, keep the definition and
12226 	    // drop the declaration.
12227 	    result.reset();
12228 	}
12229     }
12230 
12231   // If we've already seen the same class as 'die', then let's re-use
12232   // that one, unless it's an anonymous class.  We can't really safely
12233   // re-use anonymous classes as they have no name, by construction.
12234   // What we can do, rather, is to reuse the typedef that name them,
12235   // when they do have a naming typedef.
12236   if (!is_anonymous)
12237     if (class_decl_sptr pre_existing_class =
12238 	is_class_type(ctxt.lookup_type_artifact_from_die(die)))
12239       klass = pre_existing_class;
12240 
12241   uint64_t size = 0;
12242   die_size_in_bits(die, size);
12243   bool is_artificial = die_is_artificial(die);
12244 
12245   Dwarf_Die child;
12246   bool has_child = (dwarf_child(die, &child) == 0);
12247 
12248   decl_base_sptr res;
12249   if (klass)
12250     {
12251       res = result = klass;
12252       if (has_child && klass->get_is_declaration_only()
12253 	  && klass->get_definition_of_declaration())
12254 	res = result = is_class_type(klass->get_definition_of_declaration());
12255       if (loc)
12256 	result->set_location(loc);
12257     }
12258   else
12259     {
12260       result.reset(new class_decl(ctxt.env(), name, size,
12261 				  /*alignment=*/0, is_struct, loc,
12262 				  decl_base::VISIBILITY_DEFAULT,
12263 				  is_anonymous));
12264 
12265       result->set_is_declaration_only(is_declaration_only);
12266 
12267       res = add_decl_to_scope(result, scope);
12268       result = dynamic_pointer_cast<class_decl>(res);
12269       ABG_ASSERT(result);
12270     }
12271 
12272   if (size != result->get_size_in_bits())
12273     result->set_size_in_bits(size);
12274 
12275   if (klass)
12276     // We are amending a class that was built before.  So let's check
12277     // if we need to amend its "declaration-only-ness" status.
12278     if (!!result->get_size_in_bits() == result->get_is_declaration_only())
12279       // The size of the class doesn't match its
12280       // 'declaration-only-ness".  We might have a non-zero sized
12281       // class which is declaration-only, or a zero sized class that
12282       // is not declaration-only.  Let's set the declaration-only-ness
12283       // according to what we are instructed to.
12284       //
12285       // Note however that there are binaries out there emitted by
12286       // compilers (Clang, in C++) emit declarations-only classes that
12287       // have non-zero size.  So we must honor these too. That is why
12288       // we are not forcing the declaration-only-ness to false when a
12289       // class has non-zero size.  An example of such binary is
12290       // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
12291       result->set_is_declaration_only(is_declaration_only);
12292 
12293   result->set_is_artificial(is_artificial);
12294 
12295   ctxt.associate_die_to_type(die, result, where_offset);
12296 
12297   ctxt.maybe_schedule_declaration_only_class_for_resolution(result);
12298 
12299   if (!has_child)
12300     // TODO: set the access specifier for the declaration-only class
12301     // here.
12302     return result;
12303 
12304   ctxt.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
12305 
12306   scope_decl_sptr scop =
12307     dynamic_pointer_cast<scope_decl>(res);
12308   ABG_ASSERT(scop);
12309   ctxt.scope_stack().push(scop.get());
12310 
12311   if (has_child)
12312     {
12313       int anonymous_member_class_index = -1;
12314       int anonymous_member_union_index = -1;
12315       int anonymous_member_enum_index = -1;
12316 
12317       do
12318 	{
12319 	  tag = dwarf_tag(&child);
12320 
12321 	  // Handle base classes.
12322 	  if (tag == DW_TAG_inheritance)
12323 	    {
12324 	      result->set_is_declaration_only(false);
12325 
12326 	      Dwarf_Die type_die;
12327 	      if (!die_die_attribute(&child, DW_AT_type, type_die))
12328 		continue;
12329 
12330 	      type_base_sptr base_type;
12331 	      if (!(base_type =
12332 		    lookup_class_or_typedef_from_corpus(ctxt, &type_die,
12333 							called_from_public_decl,
12334 							where_offset)))
12335 		{
12336 		  base_type =
12337 		    is_type(build_ir_node_from_die(ctxt, &type_die,
12338 						   called_from_public_decl,
12339 						   where_offset));
12340 		}
12341 	      // Sometimes base_type can be a typedef.  Let's make
12342 	      // sure that typedef is compatible with a class type.
12343 	      class_decl_sptr b = is_compatible_with_class_type(base_type);
12344 	      if (!b)
12345 		continue;
12346 
12347 	      access_specifier access =
12348 		is_struct
12349 		? public_access
12350 		: private_access;
12351 
12352 	      die_access_specifier(&child, access);
12353 
12354 	      bool is_virt= die_is_virtual(&child);
12355 	      int64_t offset = 0;
12356 	      bool is_offset_present =
12357 		die_member_offset(ctxt, &child, offset);
12358 
12359 	      class_decl::base_spec_sptr base(new class_decl::base_spec
12360 					      (b, access,
12361 					       is_offset_present ? offset : -1,
12362 					       is_virt));
12363 	      if (b->get_is_declaration_only())
12364 		ABG_ASSERT(ctxt.is_decl_only_class_scheduled_for_resolution(b));
12365 	      if (result->find_base_class(b->get_qualified_name()))
12366 		continue;
12367 	      result->add_base_specifier(base);
12368 	    }
12369 	  // Handle data members.
12370 	  else if (tag == DW_TAG_member
12371 		   || tag == DW_TAG_variable)
12372 	    {
12373 	      Dwarf_Die type_die;
12374 	      if (!die_die_attribute(&child, DW_AT_type, type_die))
12375 		continue;
12376 
12377 	      string n, m;
12378 	      location loc;
12379 	      die_loc_and_name(ctxt, &child, loc, n, m);
12380 	      /// For now, we skip the hidden vtable pointer.
12381 	      /// Currently, we're looking for a member starting with
12382 	      /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
12383 	      /// use as a name for the hidden vtable pointer.
12384 	      if (n.substr(0, 5) == "_vptr"
12385 		  && !std::isalnum(n.at(5))
12386 		  && n.at(5) != '_')
12387 		continue;
12388 
12389 	      // If the variable is already a member of this class,
12390 	      // move on.  If it's an anonymous data member, we need
12391 	      // to handle it differently.  We'll do that later below.
12392 	      if (!n.empty() && lookup_var_decl_in_scope(n, result))
12393 		continue;
12394 
12395 	      int64_t offset_in_bits = 0;
12396 	      bool is_laid_out = die_member_offset(ctxt, &child,
12397 						   offset_in_bits);
12398 	      // For now, is_static == !is_laid_out.  When we have
12399 	      // templates, we'll try to be more specific.  For now,
12400 	      // this approximation should do OK.
12401 	      bool is_static = !is_laid_out;
12402 
12403 	      if (is_static && variable_is_suppressed(ctxt,
12404 						      result.get(),
12405 						      &child))
12406 		continue;
12407 
12408 	      decl_base_sptr ty = is_decl(build_ir_node_from_die(ctxt, &type_die,
12409 								 called_from_public_decl,
12410 								 where_offset));
12411 	      type_base_sptr t = is_type(ty);
12412 	      if (!t)
12413 		continue;
12414 
12415 	      // The call to build_ir_node_from_die above could have
12416 	      // triggered the adding of a data member named 'n' into
12417 	      // result.  So let's check again if the variable is
12418 	      // already a member of this class.  Here again, if it's
12419 	      // an anonymous data member, we need to handle it
12420 	      // differently.  We'll do that later below.
12421 	      if (!n.empty() && lookup_var_decl_in_scope(n, result))
12422 		continue;
12423 
12424 	      if (!is_static)
12425 		// We have a non-static data member.  So this class
12426 		// cannot be a declaration-only class anymore, even if
12427 		// some DWARF emitters might consider it otherwise.
12428 		result->set_is_declaration_only(false);
12429 	      access_specifier access =
12430 		is_struct
12431 		? public_access
12432 		: private_access;
12433 
12434 	      die_access_specifier(&child, access);
12435 
12436 	      var_decl_sptr dm(new var_decl(n, t, loc, m));
12437 	      if (n.empty() && result->find_data_member(dm))
12438 		// dm is an anonymous data member that was already
12439 		// present in the current class so let's not add it.
12440 		continue;
12441 	      result->add_data_member(dm, access, is_laid_out,
12442 				      is_static, offset_in_bits);
12443 	      ABG_ASSERT(has_scope(dm));
12444 	      ctxt.associate_die_to_decl(&child, dm, where_offset,
12445 					 /*associate_by_repr=*/false);
12446 	    }
12447 	  // Handle member functions;
12448 	  else if (tag == DW_TAG_subprogram)
12449 	    {
12450 	      decl_base_sptr r =
12451 		add_or_update_member_function(ctxt, &child, result,
12452 					      called_from_public_decl,
12453 					      where_offset);
12454 	      if (function_decl_sptr f = is_function_decl(r))
12455 		ctxt.associate_die_to_decl(&child, f, where_offset,
12456 					   /*associate_by_repr=*/true);
12457 	    }
12458 	  // Handle member types
12459 	  else if (die_is_type(&child))
12460 	    {
12461 	      // Track the anonymous type index in the current
12462 	      // scope. Look for what this means by reading the
12463 	      // comment of the function
12464 	      // build_internal_anonymous_die_name.
12465 	      int anonymous_member_type_index = 0;
12466 	      if (is_anonymous_type_die(&child))
12467 		{
12468 		  // Update the anonymous type index.
12469 		  if (die_is_class_type(&child))
12470 		    anonymous_member_type_index =
12471 		      ++anonymous_member_class_index;
12472 		  else if (dwarf_tag(&child) == DW_TAG_union_type)
12473 		    anonymous_member_type_index =
12474 		      ++anonymous_member_union_index;
12475 		  else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
12476 		    anonymous_member_type_index =
12477 		      ++anonymous_member_enum_index;
12478 		}
12479 	      // if the type is not already a member of this class,
12480 	      // then add it to the class.
12481 	      if (!lookup_class_typedef_or_enum_type_from_corpus
12482 		  (&child, anonymous_member_type_index, result.get()))
12483 		build_ir_node_from_die(ctxt, &child, result.get(),
12484 				       called_from_public_decl,
12485 				       where_offset);
12486 	    }
12487 	} while (dwarf_siblingof(&child, &child) == 0);
12488     }
12489 
12490   ctxt.scope_stack().pop();
12491 
12492   {
12493     die_class_or_union_map_type::const_iterator i =
12494       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12495     if (i != ctxt.die_wip_classes_map(source).end())
12496       {
12497 	if (is_member_type(i->second))
12498 	  set_member_access_specifier(res,
12499 				      get_member_access_specifier(i->second));
12500 	ctxt.die_wip_classes_map(source).erase(i);
12501       }
12502   }
12503 
12504   ctxt.maybe_schedule_declaration_only_class_for_resolution(result);
12505   return result;
12506 }
12507 
12508 /// Build an @ref union_decl from a DW_TAG_union_type DIE.
12509 ///
12510 /// @param ctxt the read context to use.
12511 ///
12512 /// @param die the DIE to read from.
12513 ///
12514 /// @param scope the scope the resulting @ref union_decl belongs to.
12515 ///
12516 /// @param union_type if this parameter is non-nil, then this function
12517 /// updates the @ref union_decl that it points to, rather than
12518 /// creating a new @ref union_decl.
12519 ///
12520 /// @param called_from_public_decl is true if this function has been
12521 /// initially called within the context of a public decl.
12522 ///
12523 /// @param where_offset the offset of the DIE where we are "logically"
12524 /// positionned at, in the DIE tree.  This is useful when @p die is
12525 /// e.g, DW_TAG_partial_unit that can be included in several places in
12526 /// the DIE tree.
12527 ///
12528 /// @param is_declaration_only is true if the DIE denoted by @p die is
12529 /// a declaration-only DIE.
12530 ///
12531 /// @return the resulting @ref union_decl type.
12532 static union_decl_sptr
add_or_update_union_type(read_context & ctxt,Dwarf_Die * die,scope_decl * scope,union_decl_sptr union_type,bool called_from_public_decl,size_t where_offset,bool is_declaration_only)12533 add_or_update_union_type(read_context&	 ctxt,
12534 			 Dwarf_Die*	 die,
12535 			 scope_decl*	 scope,
12536 			 union_decl_sptr union_type,
12537 			 bool		 called_from_public_decl,
12538 			 size_t	 where_offset,
12539 			 bool		 is_declaration_only)
12540 {
12541   union_decl_sptr result;
12542   if (!die)
12543     return result;
12544 
12545   unsigned tag = dwarf_tag(die);
12546 
12547   if (tag != DW_TAG_union_type)
12548     return result;
12549 
12550   const die_source source = ctxt.get_die_source(die);
12551   {
12552     die_class_or_union_map_type::const_iterator i =
12553       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12554     if (i != ctxt.die_wip_classes_map(source).end())
12555       {
12556 	union_decl_sptr u = is_union_type(i->second);
12557 	ABG_ASSERT(u);
12558 	return u;
12559       }
12560   }
12561 
12562   string name, linkage_name;
12563   location loc;
12564   die_loc_and_name(ctxt, die, loc, name, linkage_name);
12565 
12566   bool is_anonymous = false;
12567   if (name.empty())
12568     {
12569       // So we are looking at an anonymous union.  Let's give it a
12570       // name.
12571       name = get_internal_anonymous_die_prefix_name(die);
12572       ABG_ASSERT(!name.empty());
12573       // But we remember that the type is anonymous.
12574       is_anonymous = true;
12575 
12576       if (size_t s = scope->get_num_anonymous_member_unions())
12577 	name = build_internal_anonymous_die_name(name, s);
12578     }
12579 
12580   // If the type has location, then associate it to its
12581   // representation.  This way, all occurences of types with the same
12582   // representation (name) and location can be later detected as being
12583   // for the same type.
12584 
12585   if (!is_anonymous)
12586     {
12587       if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
12588 	{
12589 	  if (loc)
12590 	    result = lookup_union_type_per_location(loc.expand(), *corp);
12591 	  else
12592 	    result = lookup_union_type(name, *corp);
12593 
12594 	  if (result)
12595 	    {
12596 	      ctxt.associate_die_to_type(die, result, where_offset);
12597 	      return result;
12598 	    }
12599 	}
12600     }
12601 
12602   // if we've already seen a union with the same union as 'die' then
12603   // let's re-use that one. We can't really safely re-use anonymous
12604   // unions as they have no name, by construction.  What we can do,
12605   // rather, is to reuse the typedef that name them, when they do have
12606   // a naming typedef.
12607   if (!is_anonymous)
12608     if (union_decl_sptr pre_existing_union =
12609 	is_union_type(ctxt.lookup_artifact_from_die(die)))
12610       union_type = pre_existing_union;
12611 
12612   uint64_t size = 0;
12613   die_size_in_bits(die, size);
12614   bool is_artificial = die_is_artificial(die);
12615 
12616   if (union_type)
12617     {
12618       result = union_type;
12619       result->set_location(loc);
12620     }
12621   else
12622     {
12623       result.reset(new union_decl(ctxt.env(), name, size, loc,
12624 				  decl_base::VISIBILITY_DEFAULT,
12625 				  is_anonymous));
12626       if (is_declaration_only)
12627 	result->set_is_declaration_only(true);
12628       result = is_union_type(add_decl_to_scope(result, scope));
12629       ABG_ASSERT(result);
12630     }
12631 
12632   if (size)
12633     {
12634       result->set_size_in_bits(size);
12635       result->set_is_declaration_only(false);
12636     }
12637 
12638   result->set_is_artificial(is_artificial);
12639 
12640   ctxt.associate_die_to_type(die, result, where_offset);
12641 
12642   // TODO: maybe schedule declaration-only union for result like we do
12643   // for classes:
12644   // ctxt.maybe_schedule_declaration_only_class_for_resolution(result);
12645 
12646   Dwarf_Die child;
12647   bool has_child = (dwarf_child(die, &child) == 0);
12648   if (!has_child)
12649     return result;
12650 
12651   ctxt.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
12652 
12653   scope_decl_sptr scop =
12654     dynamic_pointer_cast<scope_decl>(result);
12655   ABG_ASSERT(scop);
12656   ctxt.scope_stack().push(scop.get());
12657 
12658   if (has_child)
12659     {
12660       do
12661 	{
12662 	  tag = dwarf_tag(&child);
12663 	  // Handle data members.
12664 	  if (tag == DW_TAG_member || tag == DW_TAG_variable)
12665 	    {
12666 	      Dwarf_Die type_die;
12667 	      if (!die_die_attribute(&child, DW_AT_type, type_die))
12668 		continue;
12669 
12670 	      string n, m;
12671 	      location loc;
12672 	      die_loc_and_name(ctxt, &child, loc, n, m);
12673 
12674 	      // Because we can be updating an existing union, let's
12675 	      // make sure we don't already have a member of the same
12676 	      // name.  Anonymous member are handled a bit later below
12677 	      // so let's not consider them here.
12678 	      if (!n.empty() && lookup_var_decl_in_scope(n, result))
12679 		continue;
12680 
12681 	      ssize_t offset_in_bits = 0;
12682 	      decl_base_sptr ty =
12683 		is_decl(build_ir_node_from_die(ctxt, &type_die,
12684 					       called_from_public_decl,
12685 					       where_offset));
12686 	      type_base_sptr t = is_type(ty);
12687 	      if (!t)
12688 		continue;
12689 
12690 	      // We have a non-static data member.  So this union
12691 	      // cannot be a declaration-only union anymore, even if
12692 	      // some DWARF emitters might consider it otherwise.
12693 	      result->set_is_declaration_only(false);
12694 	      access_specifier access = public_access;
12695 
12696 	      die_access_specifier(&child, access);
12697 
12698 	      var_decl_sptr dm(new var_decl(n, t, loc, m));
12699 	      // If dm is an anonymous data member, let's make sure
12700 	      // the current union doesn't already have it as a data
12701 	      // member.
12702 	      if (n.empty() && result->find_data_member(dm))
12703 		continue;
12704 
12705 	      result->add_data_member(dm, access, /*is_laid_out=*/true,
12706 				      /*is_static=*/false,
12707 				      offset_in_bits);
12708 	      ABG_ASSERT(has_scope(dm));
12709 	      ctxt.associate_die_to_decl(&child, dm, where_offset,
12710 					 /*associate_by_repr=*/false);
12711 	    }
12712 	  // Handle member functions;
12713 	  else if (tag == DW_TAG_subprogram)
12714 	    {
12715 	      decl_base_sptr r =
12716 		is_decl(build_ir_node_from_die(ctxt, &child,
12717 					       result.get(),
12718 					       called_from_public_decl,
12719 					       where_offset));
12720 	      if (!r)
12721 		continue;
12722 
12723 	      function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
12724 	      ABG_ASSERT(f);
12725 
12726 	      finish_member_function_reading(&child, f, result, ctxt);
12727 
12728 	      ctxt.associate_die_to_decl(&child, f, where_offset,
12729 					 /*associate_by_repr=*/false);
12730 	    }
12731 	  // Handle member types
12732 	  else if (die_is_type(&child))
12733 	    decl_base_sptr td =
12734 	      is_decl(build_ir_node_from_die(ctxt, &child, result.get(),
12735 					     called_from_public_decl,
12736 					     where_offset));
12737 	} while (dwarf_siblingof(&child, &child) == 0);
12738     }
12739 
12740   ctxt.scope_stack().pop();
12741 
12742   {
12743     die_class_or_union_map_type::const_iterator i =
12744       ctxt.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12745     if (i != ctxt.die_wip_classes_map(source).end())
12746       {
12747 	if (is_member_type(i->second))
12748 	  set_member_access_specifier(result,
12749 				      get_member_access_specifier(i->second));
12750 	ctxt.die_wip_classes_map(source).erase(i);
12751       }
12752   }
12753 
12754   return result;
12755 }
12756 
12757 /// build a qualified type from a DW_TAG_const_type,
12758 /// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
12759 ///
12760 /// @param ctxt the read context to consider.
12761 ///
12762 /// @param die the input DIE to read from.
12763 ///
12764 /// @param called_from_public_decl true if this function was called
12765 /// from a context where either a public function or a public variable
12766 /// is being built.
12767 ///
12768 /// @param where_offset the offset of the DIE where we are "logically"
12769 /// positionned at, in the DIE tree.  This is useful when @p die is
12770 /// e.g, DW_TAG_partial_unit that can be included in several places in
12771 /// the DIE tree.
12772 ///
12773 /// @return the resulting qualified_type_def.
12774 static type_base_sptr
build_qualified_type(read_context & ctxt,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)12775 build_qualified_type(read_context&	ctxt,
12776 		     Dwarf_Die*	die,
12777 		     bool		called_from_public_decl,
12778 		     size_t		where_offset)
12779 {
12780   type_base_sptr result;
12781   if (!die)
12782     return result;
12783 
12784   unsigned tag = dwarf_tag(die);
12785 
12786   if (tag != DW_TAG_const_type
12787       && tag != DW_TAG_volatile_type
12788       && tag != DW_TAG_restrict_type)
12789     return result;
12790 
12791   Dwarf_Die underlying_type_die;
12792   decl_base_sptr utype_decl;
12793   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
12794     // So, if no DW_AT_type is present, then this means (if we are
12795     // looking at a debug info emitted by GCC) that we are looking
12796     // at a qualified void type.
12797     utype_decl = build_ir_node_for_void_type(ctxt);
12798 
12799   if (!utype_decl)
12800     utype_decl = is_decl(build_ir_node_from_die(ctxt, &underlying_type_die,
12801 						called_from_public_decl,
12802 						where_offset));
12803   if (!utype_decl)
12804     return result;
12805 
12806   // The call to build_ir_node_from_die() could have triggered the
12807   // creation of the type for this DIE.  In that case, just return it.
12808   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
12809     {
12810       result = t;
12811       ctxt.associate_die_to_type(die, result, where_offset);
12812       return result;
12813     }
12814 
12815   type_base_sptr utype = is_type(utype_decl);
12816   ABG_ASSERT(utype);
12817 
12818   qualified_type_def::CV qual = qualified_type_def::CV_NONE;
12819   if (tag == DW_TAG_const_type)
12820     qual |= qualified_type_def::CV_CONST;
12821   else if (tag == DW_TAG_volatile_type)
12822     qual |= qualified_type_def::CV_VOLATILE;
12823   else if (tag == DW_TAG_restrict_type)
12824     qual |= qualified_type_def::CV_RESTRICT;
12825   else
12826     ABG_ASSERT_NOT_REACHED;
12827 
12828   if (!result)
12829     result.reset(new qualified_type_def(utype, qual, location()));
12830 
12831   ctxt.associate_die_to_type(die, result, where_offset);
12832 
12833   return result;
12834 }
12835 
12836 /// Walk a tree of typedef of qualified arrays and schedule all type
12837 /// nodes for canonicalization.
12838 ///
12839 /// This is to be used after an array tree has been cloned.  In that
12840 /// case, the newly cloned type nodes have to be scheduled for
12841 /// canonicalization.
12842 ///
12843 /// This is a subroutine of maybe_strip_qualification.
12844 ///
12845 /// @param t the type node to be scheduled for canonicalization.
12846 ///
12847 /// @param ctxt the contexter of the reader to use.
12848 static void
schedule_array_tree_for_late_canonicalization(const type_base_sptr & t,read_context & ctxt)12849 schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
12850 					      read_context &ctxt)
12851 {
12852   if (typedef_decl_sptr type = is_typedef(t))
12853     {
12854       schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
12855 						    ctxt);
12856       ctxt.schedule_type_for_late_canonicalization(t);
12857     }
12858   else if (qualified_type_def_sptr type = is_qualified_type(t))
12859     {
12860       schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
12861 						    ctxt);
12862       ctxt.schedule_type_for_late_canonicalization(t);
12863     }
12864   else if (array_type_def_sptr type = is_array_type(t))
12865     {
12866       for (vector<array_type_def::subrange_sptr>::const_iterator i =
12867 	     type->get_subranges().begin();
12868 	   i != type->get_subranges().end();
12869 	   ++i)
12870 	{
12871 	  if (!(*i)->get_scope())
12872 	    add_decl_to_scope(*i, ctxt.cur_transl_unit()->get_global_scope());
12873 	  ctxt.schedule_type_for_late_canonicalization(*i);
12874 
12875 	}
12876       schedule_array_tree_for_late_canonicalization(type->get_element_type(),
12877 						    ctxt);
12878       ctxt.schedule_type_for_late_canonicalization(type);
12879     }
12880 }
12881 
12882 /// Strip qualification from a qualified type, when it makes sense.
12883 ///
12884 /// DWARF constructs "const reference".  This is redundant because a
12885 /// reference is always const.  The issue is these redundant types then
12886 /// leak into the IR and make for bad diagnostics.
12887 ///
12888 /// This function thus strips the const qualifier from the type in
12889 /// that case.  It might contain code to strip other cases like this
12890 /// in the future.
12891 ///
12892 /// @param t the type to strip const qualification from.
12893 ///
12894 /// @param ctxt the @ref read_context to use.
12895 ///
12896 /// @return the stripped type or just return @p t.
12897 static decl_base_sptr
maybe_strip_qualification(const qualified_type_def_sptr t,read_context & ctxt)12898 maybe_strip_qualification(const qualified_type_def_sptr t,
12899 			  read_context &ctxt)
12900 {
12901   if (!t)
12902     return t;
12903 
12904   decl_base_sptr result = t;
12905   type_base_sptr u = t->get_underlying_type();
12906 
12907   result = strip_useless_const_qualification(t);
12908   if (result.get() != t.get())
12909     return result;
12910 
12911   if (is_array_type(u) || is_typedef_of_array(u))
12912     {
12913       array_type_def_sptr array;
12914       scope_decl * scope = 0;
12915       if ((array = is_array_type(u)))
12916 	{
12917 	  scope = array->get_scope();
12918 	  ABG_ASSERT(scope);
12919 	  array = is_array_type(clone_array_tree(array));
12920 	  schedule_array_tree_for_late_canonicalization(array, ctxt);
12921 	  add_decl_to_scope(array, scope);
12922 	  t->set_underlying_type(array);
12923 	  u = t->get_underlying_type();
12924 	}
12925       else if (is_typedef_of_array(u))
12926 	{
12927 	  scope = is_decl(u)->get_scope();
12928 	  ABG_ASSERT(scope);
12929 	  typedef_decl_sptr typdef =
12930 	    is_typedef(clone_array_tree(is_typedef(u)));
12931 	  schedule_array_tree_for_late_canonicalization(typdef, ctxt);
12932 	  ABG_ASSERT(typdef);
12933 	  add_decl_to_scope(typdef, scope);
12934 	  t->set_underlying_type(typdef);
12935 	  u = t->get_underlying_type();
12936 	  array = is_typedef_of_array(u);
12937 	}
12938       else
12939 	ABG_ASSERT_NOT_REACHED;
12940 
12941       ABG_ASSERT(array);
12942       // We should not be editing types that are already canonicalized.
12943       ABG_ASSERT(!array->get_canonical_type());
12944       type_base_sptr element_type = array->get_element_type();
12945 
12946       if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
12947 	{
12948 	  // We should not be editing types that are already canonicalized.
12949 	  ABG_ASSERT(!qualified->get_canonical_type());
12950 	  qualified_type_def::CV quals = qualified->get_cv_quals();
12951 	  quals |= t->get_cv_quals();
12952 	  qualified->set_cv_quals(quals);
12953 	  result = is_decl(u);
12954 	}
12955       else
12956 	{
12957 	  qualified_type_def_sptr qual_type
12958 	    (new qualified_type_def(element_type,
12959 				    t->get_cv_quals(),
12960 				    t->get_location()));
12961 	  add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
12962 	  array->set_element_type(qual_type);
12963 	  ctxt.schedule_type_for_late_canonicalization(is_type(qual_type));
12964 	  result = is_decl(u);
12965 	}
12966     }
12967 
12968   return result;
12969 }
12970 
12971 /// Build a pointer type from a DW_TAG_pointer_type DIE.
12972 ///
12973 /// @param ctxt the read context to consider.
12974 ///
12975 /// @param die the DIE to read information from.
12976 ///
12977 /// @param called_from_public_decl true if this function was called
12978 /// from a context where either a public function or a public variable
12979 /// is being built.
12980 ///
12981 /// @param where_offset the offset of the DIE where we are "logically"
12982 /// positionned at, in the DIE tree.  This is useful when @p die is
12983 /// e.g, DW_TAG_partial_unit that can be included in several places in
12984 /// the DIE tree.
12985 ///
12986 /// @return the resulting pointer to pointer_type_def.
12987 static pointer_type_def_sptr
build_pointer_type_def(read_context & ctxt,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)12988 build_pointer_type_def(read_context&	ctxt,
12989 		       Dwarf_Die*	die,
12990 		       bool		called_from_public_decl,
12991 		       size_t		where_offset)
12992 {
12993   pointer_type_def_sptr result;
12994 
12995   if (!die)
12996     return result;
12997 
12998   unsigned tag = dwarf_tag(die);
12999   if (tag != DW_TAG_pointer_type)
13000     return result;
13001 
13002   type_or_decl_base_sptr utype_decl;
13003   Dwarf_Die underlying_type_die;
13004   bool has_underlying_type_die = false;
13005   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13006     // If the DW_AT_type attribute is missing, that means we are
13007     // looking at a pointer to "void".
13008     utype_decl = build_ir_node_for_void_type(ctxt);
13009   else
13010     has_underlying_type_die = true;
13011 
13012   if (!utype_decl && has_underlying_type_die)
13013     utype_decl = build_ir_node_from_die(ctxt, &underlying_type_die,
13014 					called_from_public_decl,
13015 					where_offset);
13016   if (!utype_decl)
13017     return result;
13018 
13019   // The call to build_ir_node_from_die() could have triggered the
13020   // creation of the type for this DIE.  In that case, just return it.
13021   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
13022     {
13023       result = is_pointer_type(t);
13024       ABG_ASSERT(result);
13025       return result;
13026     }
13027 
13028   type_base_sptr utype = is_type(utype_decl);
13029   ABG_ASSERT(utype);
13030 
13031   // if the DIE for the pointer type doesn't have a byte_size
13032   // attribute then we assume the size of the pointer is the address
13033   // size of the current translation unit.
13034   uint64_t size = ctxt.cur_transl_unit()->get_address_size();
13035   if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13036     // The size as expressed by DW_AT_byte_size is in byte, so let's
13037     // convert it to bits.
13038     size *= 8;
13039 
13040   // And the size of the pointer must be the same as the address size
13041   // of the current translation unit.
13042   ABG_ASSERT((size_t) ctxt.cur_transl_unit()->get_address_size() == size);
13043 
13044   result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
13045   ABG_ASSERT(result->get_pointed_to_type());
13046 
13047   ctxt.associate_die_to_type(die, result, where_offset);
13048   return result;
13049 }
13050 
13051 /// Build a reference type from either a DW_TAG_reference_type or
13052 /// DW_TAG_rvalue_reference_type DIE.
13053 ///
13054 /// @param ctxt the read context to consider.
13055 ///
13056 /// @param die the DIE to read from.
13057 ///
13058 /// @param called_from_public_decl true if this function was called
13059 /// from a context where either a public function or a public variable
13060 /// is being built.
13061 ///
13062 /// @param where_offset the offset of the DIE where we are "logically"
13063 /// positionned at, in the DIE tree.  This is useful when @p die is
13064 /// e.g, DW_TAG_partial_unit that can be included in several places in
13065 /// the DIE tree.
13066 ///
13067 /// @return a pointer to the resulting reference_type_def.
13068 static reference_type_def_sptr
build_reference_type(read_context & ctxt,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13069 build_reference_type(read_context&	ctxt,
13070 		     Dwarf_Die*	die,
13071 		     bool		called_from_public_decl,
13072 		     size_t		where_offset)
13073 {
13074   reference_type_def_sptr result;
13075 
13076   if (!die)
13077     return result;
13078 
13079   unsigned tag = dwarf_tag(die);
13080   if (tag != DW_TAG_reference_type
13081       && tag != DW_TAG_rvalue_reference_type)
13082     return result;
13083 
13084   Dwarf_Die underlying_type_die;
13085   if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13086     return result;
13087 
13088   type_or_decl_base_sptr utype_decl =
13089     build_ir_node_from_die(ctxt, &underlying_type_die,
13090 			   called_from_public_decl,
13091 			   where_offset);
13092   if (!utype_decl)
13093     return result;
13094 
13095   // The call to build_ir_node_from_die() could have triggered the
13096   // creation of the type for this DIE.  In that case, just return it.
13097   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
13098     {
13099       result = is_reference_type(t);
13100       ABG_ASSERT(result);
13101       return result;
13102     }
13103 
13104   type_base_sptr utype = is_type(utype_decl);
13105   ABG_ASSERT(utype);
13106 
13107   // if the DIE for the reference type doesn't have a byte_size
13108   // attribute then we assume the size of the reference is the address
13109   // size of the current translation unit.
13110   uint64_t size = ctxt.cur_transl_unit()->get_address_size();
13111   if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13112     size *= 8;
13113 
13114   // And the size of the pointer must be the same as the address size
13115   // of the current translation unit.
13116   ABG_ASSERT((size_t) ctxt.cur_transl_unit()->get_address_size() == size);
13117 
13118   bool is_lvalue = tag == DW_TAG_reference_type;
13119 
13120   result.reset(new reference_type_def(utype, is_lvalue, size,
13121 				      /*alignment=*/0,
13122 				      location()));
13123   if (corpus_sptr corp = ctxt.current_corpus())
13124     if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
13125       result = t;
13126   ctxt.associate_die_to_type(die, result, where_offset);
13127   return result;
13128 }
13129 
13130 /// Build a subroutine type from a DW_TAG_subroutine_type DIE.
13131 ///
13132 /// @param ctxt the read context to consider.
13133 ///
13134 /// @param die the DIE to read from.
13135 ///
13136 /// @param is_method points to a class or union declaration iff we're
13137 /// building the type for a method.  This is the enclosing class or
13138 /// union of the method.
13139 ///
13140 /// @param where_offset the offset of the DIE where we are "logically"
13141 /// positioned at, in the DIE tree.  This is useful when @p die is
13142 /// e.g, DW_TAG_partial_unit that can be included in several places in
13143 /// the DIE tree.
13144 ///
13145 /// @return a pointer to the resulting function_type_sptr.
13146 static function_type_sptr
build_function_type(read_context & ctxt,Dwarf_Die * die,class_or_union_sptr is_method,size_t where_offset)13147 build_function_type(read_context&	ctxt,
13148 		    Dwarf_Die*		die,
13149 		    class_or_union_sptr is_method,
13150 		    size_t		where_offset)
13151 {
13152   function_type_sptr result;
13153 
13154   if (!die)
13155     return result;
13156 
13157   ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
13158 	     || dwarf_tag(die) == DW_TAG_subprogram);
13159 
13160   const die_source source = ctxt.get_die_source(die);
13161 
13162   decl_base_sptr type_decl;
13163 
13164   translation_unit_sptr tu = ctxt.cur_transl_unit();
13165   ABG_ASSERT(tu);
13166 
13167   /// If, inside the current translation unit, we've already seen a
13168   /// function type with the same text representation, then reuse that
13169   /// one instead.
13170   if (type_base_sptr t = ctxt.lookup_fn_type_from_die_repr_per_tu(die))
13171     {
13172       result = is_function_type(t);
13173       ABG_ASSERT(result);
13174       ctxt.associate_die_to_type(die, result, where_offset);
13175       return result;
13176     }
13177 
13178   bool odr_is_relevant = ctxt.odr_is_relevant(die);
13179   if (odr_is_relevant)
13180     {
13181       // So we can rely on the One Definition Rule to say that if
13182       // several different function types have the same name (or
13183       // rather, representation) across the entire binary, then they
13184       // ought to designate the same function type.  So let's ensure
13185       // that if we've already seen a function type with the same
13186       // representation as the function type 'die', then it's the same
13187       // type as the one denoted by 'die'.
13188       if (function_type_sptr fn_type =
13189 	  is_function_type(ctxt.lookup_type_artifact_from_die(die)))
13190 	{
13191 	  ctxt.associate_die_to_type(die, fn_type, where_offset);
13192 	  return fn_type;
13193 	}
13194     }
13195 
13196   // Let's look at the DIE to detect if it's the DIE for a method
13197   // (type).  If it is, we can deduce the name of its enclosing class
13198   // and if it's a static or const.
13199   bool is_const = false;
13200   bool is_static = false;
13201   Dwarf_Die object_pointer_die;
13202   Dwarf_Die class_type_die;
13203   bool has_this_parm_die =
13204     die_function_type_is_method_type(ctxt, die, where_offset,
13205 				     object_pointer_die,
13206 				     class_type_die,
13207 				     is_static);
13208   if (has_this_parm_die)
13209     {
13210       // The function (type) has a "this" parameter DIE. It means it's
13211       // a member function DIE.
13212       if (!is_static)
13213 	if (die_object_pointer_is_for_const_method(&object_pointer_die))
13214 	  is_const = true;
13215 
13216       if (!is_method)
13217 	{
13218 	  // We were initially called as if the function represented
13219 	  // by DIE was *NOT* a member function.  But now we know it's
13220 	  // a member function.  Let's take that into account.
13221 	  class_or_union_sptr klass_type =
13222 	    is_class_or_union_type(build_ir_node_from_die(ctxt, &class_type_die,
13223 							  /*called_from_pub_decl=*/true,
13224 							  where_offset));
13225 	  ABG_ASSERT(klass_type);
13226 	  is_method = klass_type;
13227 	}
13228     }
13229 
13230   // Let's create the type early and record it as being for the DIE
13231   // 'die'.  This way, when building the sub-type triggers the
13232   // creation of a type matching the same 'die', then we'll reuse this
13233   // one.
13234 
13235   result.reset(is_method
13236 	       ? new method_type(is_method, is_const,
13237 				 tu->get_address_size(),
13238 				 /*alignment=*/0)
13239 	       : new function_type(ctxt.env(), tu->get_address_size(),
13240 				   /*alignment=*/0));
13241   ctxt.associate_die_to_type(die, result, where_offset);
13242   ctxt.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
13243 
13244   type_base_sptr return_type;
13245   Dwarf_Die ret_type_die;
13246   if (die_die_attribute(die, DW_AT_type, ret_type_die))
13247     return_type =
13248       is_type(build_ir_node_from_die(ctxt, &ret_type_die,
13249 				     /*called_from_public_decl=*/true,
13250 				     where_offset));
13251   if (!return_type)
13252     return_type = is_type(build_ir_node_for_void_type(ctxt));
13253   result->set_return_type(return_type);
13254 
13255   Dwarf_Die child;
13256   function_decl::parameters function_parms;
13257 
13258   if (dwarf_child(die, &child) == 0)
13259     do
13260       {
13261 	int child_tag = dwarf_tag(&child);
13262 	if (child_tag == DW_TAG_formal_parameter)
13263 	  {
13264 	    // This is a "normal" function parameter.
13265 	    string name, linkage_name;
13266 	    location loc;
13267 	    die_loc_and_name(ctxt, &child, loc, name, linkage_name);
13268 	    if (!tools_utils::string_is_ascii_identifier(name))
13269 	      // Sometimes, bogus compiler emit names that are
13270 	      // non-ascii garbage.  Let's just ditch that for now.
13271 	      name.clear();
13272 	    bool is_artificial = die_is_artificial(&child);
13273 	    type_base_sptr parm_type;
13274 	    Dwarf_Die parm_type_die;
13275 	    if (die_die_attribute(&child, DW_AT_type, parm_type_die))
13276 	      parm_type =
13277 		is_type(build_ir_node_from_die(ctxt, &parm_type_die,
13278 					       /*called_from_public_decl=*/true,
13279 					       where_offset));
13280 	    if (!parm_type)
13281 	      continue;
13282 	    function_decl::parameter_sptr p
13283 	      (new function_decl::parameter(parm_type, name, loc,
13284 					    /*variadic_marker=*/false,
13285 					    is_artificial));
13286 	    function_parms.push_back(p);
13287 	  }
13288 	else if (child_tag == DW_TAG_unspecified_parameters)
13289 	  {
13290 	    // This is a variadic function parameter.
13291 	    bool is_artificial = die_is_artificial(&child);
13292 	    ir::environment* env = ctxt.env();
13293 	    ABG_ASSERT(env);
13294 	    type_base_sptr parm_type =
13295 	      is_type(build_ir_node_for_variadic_parameter_type(ctxt));
13296 	    function_decl::parameter_sptr p
13297 	      (new function_decl::parameter(parm_type,
13298 					    /*name=*/"",
13299 					    location(),
13300 					    /*variadic_marker=*/true,
13301 					    is_artificial));
13302 	    function_parms.push_back(p);
13303 	    // After a DW_TAG_unspecified_parameters tag, we shouldn't
13304 	    // keep reading for parameters.  The
13305 	    // unspecified_parameters TAG should be the last parameter
13306 	    // that we record. For instance, if there are multiple
13307 	    // DW_TAG_unspecified_parameters DIEs then we should care
13308 	    // only for the first one.
13309 	    break;
13310 	  }
13311       }
13312     while (dwarf_siblingof(&child, &child) == 0);
13313 
13314   result->set_parameters(function_parms);
13315 
13316   tu->bind_function_type_life_time(result);
13317 
13318   result->set_is_artificial(true);
13319 
13320   ctxt.associate_die_repr_to_fn_type_per_tu(die, result);
13321 
13322   {
13323     die_function_type_map_type::const_iterator i =
13324       ctxt.die_wip_function_types_map(source).
13325       find(dwarf_dieoffset(die));
13326     if (i != ctxt.die_wip_function_types_map(source).end())
13327       ctxt.die_wip_function_types_map(source).erase(i);
13328   }
13329 
13330   maybe_canonicalize_type(result, ctxt);
13331   return result;
13332 }
13333 
13334 /// Build a subrange type from a DW_TAG_subrange_type.
13335 ///
13336 /// @param ctxt the read context to consider.
13337 ///
13338 /// @param die the DIE to read from.
13339 ///
13340 /// @param where_offset the offset of the DIE where we are "logically"
13341 /// positionned at in the DIE tree.  This is useful when @p die is
13342 /// e,g, DW_TAG_partial_unit that can be included in several places in
13343 /// the DIE tree.
13344 ///
13345 /// @param associate_die_to_type if this is true then the resulting
13346 /// type is associated to the @p die, so that next time when the
13347 /// system looks up the type associated to it, the current resulting
13348 /// type is returned.  If false, then no association is done and the
13349 /// resulting type can be destroyed right after.  This can be useful
13350 /// when the sole purpose of building the @ref
13351 /// array_type_def::subrange_type is to use some of its method like,
13352 /// e.g, its name pretty printing methods.
13353 ///
13354 /// @return the newly built instance of @ref
13355 /// array_type_def::subrange_type, or nil if no type could be built.
13356 static array_type_def::subrange_sptr
build_subrange_type(read_context & ctxt,const Dwarf_Die * die,size_t where_offset,bool associate_type_to_die)13357 build_subrange_type(read_context&	ctxt,
13358 		    const Dwarf_Die*		die,
13359 		    size_t		where_offset,
13360 		    bool		associate_type_to_die)
13361 {
13362   array_type_def::subrange_sptr result;
13363 
13364   if (!die)
13365     return result;
13366 
13367   unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
13368   if (tag != DW_TAG_subrange_type)
13369     return result;
13370 
13371   string name = die_name(die);
13372 
13373   // load the underlying type.
13374   Dwarf_Die underlying_type_die;
13375   type_base_sptr underlying_type;
13376   /* Unless there is an underlying type which says differently.  */
13377   bool is_signed = false;
13378   if (die_die_attribute(die, DW_AT_type, underlying_type_die))
13379     underlying_type =
13380       is_type(build_ir_node_from_die(ctxt,
13381 				     &underlying_type_die,
13382 				     /*called_from_public_decl=*/true,
13383 				     where_offset));
13384 
13385   if (underlying_type)
13386     {
13387       uint64_t ate;
13388       if (die_unsigned_constant_attribute (&underlying_type_die,
13389 					   DW_AT_encoding,
13390 					   ate))
13391 	  is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
13392     }
13393 
13394   translation_unit::language language = ctxt.cur_transl_unit()->get_language();
13395   array_type_def::subrange_type::bound_value lower_bound =
13396     get_default_array_lower_bound(language);
13397   array_type_def::subrange_type::bound_value upper_bound;
13398   uint64_t count = 0;
13399   bool is_infinite = false;
13400 
13401   // The DWARF 4 specifications says, in [5.11 Subrange
13402   // Type Entries]:
13403   //
13404   //     The subrange entry may have the attributes
13405   //     DW_AT_lower_bound and DW_AT_upper_bound to
13406   //     specify, respectively, the lower and upper bound
13407   //     values of the subrange.
13408   //
13409   // So let's look for DW_AT_lower_bound first.
13410   die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
13411 
13412   // Then, DW_AT_upper_bound.
13413   if (!die_constant_attribute(die, DW_AT_upper_bound, is_signed, upper_bound))
13414     {
13415       // The DWARF 4 spec says, in [5.11 Subrange Type
13416       // Entries]:
13417       //
13418       //   The DW_AT_upper_bound attribute may be replaced
13419       //   by a DW_AT_count attribute, whose value
13420       //   describes the number of elements in the
13421       //   subrange rather than the value of the last
13422       //   element."
13423       //
13424       // So, as DW_AT_upper_bound is not present in this
13425       // case, let's see if there is a DW_AT_count.
13426       die_unsigned_constant_attribute(die, DW_AT_count, count);
13427 
13428       // We can deduce the upper_bound from the
13429       // lower_bound and the number of elements of the
13430       // array:
13431       if (int64_t u = lower_bound.get_signed_value() + count)
13432 	upper_bound = u - 1;
13433 
13434       if (upper_bound.get_unsigned_value() == 0 && count == 0)
13435 	// No upper_bound nor count was present on the DIE, this means
13436 	// the array is considered to have an infinite (or rather not
13437 	// known) size.
13438 	is_infinite = true;
13439     }
13440 
13441   if (UINT64_MAX == upper_bound.get_unsigned_value())
13442     {
13443       // If the upper_bound size is the max of the integer value, then
13444       // it most certainly means infinite size.
13445       is_infinite = true;
13446       upper_bound.set_unsigned(0);
13447     }
13448 
13449   result.reset
13450     (new array_type_def::subrange_type(ctxt.env(),
13451 				       name,
13452 				       lower_bound,
13453 				       upper_bound,
13454 				       location()));
13455   result->is_infinite(is_infinite);
13456 
13457   if (underlying_type)
13458     result->set_underlying_type(underlying_type);
13459 
13460   ABG_ASSERT(result->is_infinite()
13461 	     || (result->get_length() ==
13462 		 (uint64_t) (result->get_upper_bound()
13463 			     - result->get_lower_bound() + 1)));
13464 
13465   if (associate_type_to_die)
13466     ctxt.associate_die_to_type(die, result, where_offset);
13467 
13468   return result;
13469 }
13470 
13471 /// Build the sub-ranges of an array type.
13472 ///
13473 /// This is a sub-routine of build_array_type().
13474 ///
13475 /// @param ctxt the context to read from.
13476 ///
13477 /// @param die the DIE of tag DW_TAG_array_type which contains
13478 /// children DIEs that represent the sub-ranges.
13479 ///
13480 /// @param subranges out parameter.  This is set to the sub-ranges
13481 /// that are built from @p die.
13482 ///
13483 /// @param where_offset the offset of the DIE where we are "logically"
13484 /// positioned at, in the DIE tree.  This is useful when @p die is
13485 /// e.g, DW_TAG_partial_unit that can be included in several places in
13486 /// the DIE tree.
13487 static void
build_subranges_from_array_type_die(read_context & ctxt,const Dwarf_Die * die,array_type_def::subranges_type & subranges,size_t where_offset,bool associate_type_to_die)13488 build_subranges_from_array_type_die(read_context&			ctxt,
13489 				    const Dwarf_Die*			die,
13490 				    array_type_def::subranges_type&	subranges,
13491 				    size_t				where_offset,
13492 				    bool				associate_type_to_die)
13493 {
13494   Dwarf_Die child;
13495 
13496   if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
13497     {
13498       do
13499 	{
13500 	  int child_tag = dwarf_tag(&child);
13501 	  if (child_tag == DW_TAG_subrange_type)
13502 	    {
13503 	      array_type_def::subrange_sptr s;
13504 	      if (associate_type_to_die)
13505 		{
13506 		  // We are being called to create the type, add it to
13507 		  // the current type graph and associate it to the
13508 		  // DIE it's been created from.
13509 		  type_or_decl_base_sptr t =
13510 		    build_ir_node_from_die(ctxt, &child,
13511 					   /*called_from_public_decl=*/true,
13512 					   where_offset);
13513 		  s = is_subrange_type(t);
13514 		}
13515 	      else
13516 		// We are being called to create the type but *NOT*
13517 		// add it to the current tyupe tree, *NOR* associate
13518 		// it to the DIE it's been created from.
13519 		s = build_subrange_type(ctxt, &child,
13520 					where_offset,
13521 					/*associate_type_to_die=*/false);
13522 	      if (s)
13523 		subranges.push_back(s);
13524 	    }
13525 	}
13526       while (dwarf_siblingof(&child, &child) == 0);
13527     }
13528 }
13529 
13530 /// Build an array type from a DW_TAG_array_type DIE.
13531 ///
13532 /// @param ctxt the read context to consider.
13533 ///
13534 /// @param die the DIE to read from.
13535 ///
13536 /// @param called_from_public_decl true if this function was called
13537 /// from a context where either a public function or a public variable
13538 /// is being built.
13539 ///
13540 /// @param where_offset the offset of the DIE where we are "logically"
13541 /// positioned at, in the DIE tree.  This is useful when @p die is
13542 /// e.g, DW_TAG_partial_unit that can be included in several places in
13543 /// the DIE tree.
13544 ///
13545 /// @return a pointer to the resulting array_type_def.
13546 static array_type_def_sptr
build_array_type(read_context & ctxt,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13547 build_array_type(read_context&	ctxt,
13548 		 Dwarf_Die*	die,
13549 		 bool		called_from_public_decl,
13550 		 size_t	where_offset)
13551 {
13552   array_type_def_sptr result;
13553 
13554   if (!die)
13555     return result;
13556 
13557   unsigned tag = dwarf_tag(die);
13558   if (tag != DW_TAG_array_type)
13559     return result;
13560 
13561   decl_base_sptr type_decl;
13562   Dwarf_Die type_die;
13563 
13564   if (die_die_attribute(die, DW_AT_type, type_die))
13565     type_decl = is_decl(build_ir_node_from_die(ctxt, &type_die,
13566 					       called_from_public_decl,
13567 					       where_offset));
13568   if (!type_decl)
13569     return result;
13570 
13571   // The call to build_ir_node_from_die() could have triggered the
13572   // creation of the type for this DIE.  In that case, just return it.
13573   if (type_base_sptr t = ctxt.lookup_type_from_die(die))
13574     {
13575       result = is_array_type(t);
13576       ABG_ASSERT(result);
13577       return result;
13578     }
13579 
13580   type_base_sptr type = is_type(type_decl);
13581   ABG_ASSERT(type);
13582 
13583   array_type_def::subranges_type subranges;
13584 
13585   build_subranges_from_array_type_die(ctxt, die, subranges, where_offset);
13586 
13587   result.reset(new array_type_def(type, subranges, location()));
13588 
13589   return result;
13590 }
13591 
13592 /// Create a typedef_decl from a DW_TAG_typedef DIE.
13593 ///
13594 /// @param ctxt the read context to consider.
13595 ///
13596 /// @param die the DIE to read from.
13597 ///
13598 /// @param called_from_public_decl true if this function was called
13599 /// from a context where either a public function or a public variable
13600 /// is being built.
13601 ///
13602 /// @param where_offset the offset of the DIE where we are "logically"
13603 /// positionned at, in the DIE tree.  This is useful when @p die is
13604 /// e.g, DW_TAG_partial_unit that can be included in several places in
13605 /// the DIE tree.
13606 ///
13607 /// @return the newly created typedef_decl.
13608 static typedef_decl_sptr
build_typedef_type(read_context & ctxt,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)13609 build_typedef_type(read_context&	ctxt,
13610 		   Dwarf_Die*		die,
13611 		   bool		called_from_public_decl,
13612 		   size_t		where_offset)
13613 {
13614   typedef_decl_sptr result;
13615 
13616   if (!die)
13617     return result;
13618 
13619   unsigned tag = dwarf_tag(die);
13620   if (tag != DW_TAG_typedef)
13621     return result;
13622 
13623   string name, linkage_name;
13624   location loc;
13625   die_loc_and_name(ctxt, die, loc, name, linkage_name);
13626 
13627   if (corpus_sptr corp = ctxt.should_reuse_type_from_corpus_group())
13628     if (loc)
13629       result = lookup_typedef_type_per_location(loc.expand(), *corp);
13630 
13631   if (!ctxt.odr_is_relevant(die))
13632     if (typedef_decl_sptr t = is_typedef(ctxt.lookup_artifact_from_die(die)))
13633       result = t;
13634 
13635   if (!result)
13636     {
13637       type_base_sptr utype;
13638       Dwarf_Die underlying_type_die;
13639       if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13640 	// A typedef DIE with no underlying type means a typedef to
13641 	// void type.
13642 	utype = ctxt.env()->get_void_type();
13643 
13644       if (!utype)
13645 	utype =
13646 	  is_type(build_ir_node_from_die(ctxt,
13647 					 &underlying_type_die,
13648 					 called_from_public_decl,
13649 					 where_offset));
13650       if (!utype)
13651 	return result;
13652 
13653       // The call to build_ir_node_from_die() could have triggered the
13654       // creation of the type for this DIE.  In that case, just return
13655       // it.
13656       if (type_base_sptr t = ctxt.lookup_type_from_die(die))
13657 	{
13658 	  result = is_typedef(t);
13659 	  ABG_ASSERT(result);
13660 	  return result;
13661 	}
13662 
13663       ABG_ASSERT(utype);
13664       result.reset(new typedef_decl(name, utype, loc, linkage_name));
13665 
13666       if ((is_class_or_union_type(utype) || is_enum_type(utype))
13667 	  && is_anonymous_type(utype))
13668 	{
13669 	  // This is a naming typedef for an enum or a class.  Let's
13670 	  // mark the underlying decl as such.
13671 	  decl_base_sptr decl = is_decl(utype);
13672 	  ABG_ASSERT(decl);
13673 	  decl->set_naming_typedef(result);
13674 	}
13675     }
13676 
13677   ctxt.associate_die_to_type(die, result, where_offset);
13678 
13679   return result;
13680 }
13681 
13682 /// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
13683 /// denoted by the DIE is not suppressed by a suppression
13684 /// specification associated to the current read context.
13685 ///
13686 /// Note that if a member variable declaration with the same name as
13687 /// the name of the DIE we are looking at exists, this function returns
13688 /// that existing variable declaration.
13689 ///
13690 /// @param ctxt the read context to use.
13691 ///
13692 /// @param die the DIE representing the variable we are looking at.
13693 ///
13694 /// @param where_offset the offset of the DIE where we are "logically"
13695 /// positionned at, in the DIE tree.  This is useful when @p die is
13696 /// e.g, DW_TAG_partial_unit that can be included in several places in
13697 /// the DIE tree.
13698 ///
13699 /// @param result if this is set to an existing var_decl, this means
13700 /// that the function will append the new properties it sees on @p die
13701 /// to that exising var_decl.  Otherwise, if this parameter is NULL, a
13702 /// new var_decl is going to be allocated and returned.
13703 ///
13704 /// @param is_required_decl_spec this is true iff the variable to
13705 /// build is referred to as being the specification of another
13706 /// variable.
13707 ///
13708 /// @return a pointer to the newly created var_decl.  If the var_decl
13709 /// could not be built, this function returns NULL.
13710 static var_decl_sptr
build_or_get_var_decl_if_not_suppressed(read_context & ctxt,scope_decl * scope,Dwarf_Die * die,size_t where_offset,var_decl_sptr result,bool is_required_decl_spec)13711 build_or_get_var_decl_if_not_suppressed(read_context&	ctxt,
13712 					scope_decl	*scope,
13713 					Dwarf_Die	*die,
13714 					size_t	where_offset,
13715 					var_decl_sptr	result,
13716 					bool is_required_decl_spec)
13717 {
13718   var_decl_sptr var;
13719   if (variable_is_suppressed(ctxt, scope, die, is_required_decl_spec))
13720     return var;
13721 
13722   if (class_decl* class_type = is_class_type(scope))
13723     {
13724       string var_name = die_name(die);
13725       if (!var_name.empty())
13726 	if ((var = class_type->find_data_member(var_name)))
13727 	  return var;
13728     }
13729   var = build_var_decl(ctxt, die, where_offset, result);
13730   return var;
13731 }
13732 
13733 /// Build a @ref var_decl out of a DW_TAG_variable DIE.
13734 ///
13735 /// @param ctxt the read context to use.
13736 ///
13737 /// @param die the DIE representing the variable we are looking at.
13738 ///
13739 /// @param where_offset the offset of the DIE where we are "logically"
13740 /// positionned at, in the DIE tree.  This is useful when @p die is
13741 /// e.g, DW_TAG_partial_unit that can be included in several places in
13742 /// the DIE tree.
13743 ///
13744 /// @param result if this is set to an existing var_decl, this means
13745 /// that the function will append the new properties it sees on @p die
13746 /// to that exising var_decl.  Otherwise, if this parameter is NULL, a
13747 /// new var_decl is going to be allocated and returned.
13748 ///
13749 /// @return a pointer to the newly created var_decl.  If the var_decl
13750 /// could not be built, this function returns NULL.
13751 static var_decl_sptr
build_var_decl(read_context & ctxt,Dwarf_Die * die,size_t where_offset,var_decl_sptr result)13752 build_var_decl(read_context&	ctxt,
13753 	       Dwarf_Die	*die,
13754 	       size_t		where_offset,
13755 	       var_decl_sptr	result)
13756 {
13757   if (!die)
13758     return result;
13759 
13760   int tag = dwarf_tag(die);
13761   ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
13762 
13763   if (!die_is_public_decl(die))
13764     return result;
13765 
13766   type_base_sptr type;
13767   Dwarf_Die type_die;
13768   if (die_die_attribute(die, DW_AT_type, type_die))
13769     {
13770       decl_base_sptr ty =
13771 	is_decl(build_ir_node_from_die(ctxt, &type_die,
13772 				       /*called_from_public_decl=*/true,
13773 				       where_offset));
13774       if (!ty)
13775 	return result;
13776       type = is_type(ty);
13777       ABG_ASSERT(type);
13778     }
13779 
13780   if (!type)
13781     return result;
13782 
13783   string name, linkage_name;
13784   location loc;
13785   die_loc_and_name(ctxt, die, loc, name, linkage_name);
13786 
13787   if (!result)
13788     result.reset(new var_decl(name, type, loc, linkage_name));
13789   else
13790     {
13791       // We were called to append properties that might have been
13792       // missing from the first version of the variable.  And usually
13793       // that missing property is the mangled name.
13794       if (!linkage_name.empty())
13795 	result->set_linkage_name(linkage_name);
13796     }
13797 
13798   // Check if a variable symbol with this name is exported by the elf
13799   // binary.  If it is, then set the symbol of the variable, if it's
13800   // not set already.
13801   if (!result->get_symbol())
13802     {
13803       elf_symbol_sptr var_sym;
13804       Dwarf_Addr      var_addr;
13805       if (ctxt.get_variable_address(die, var_addr))
13806 	{
13807 	  ctxt.symtab()->update_main_symbol(var_addr,
13808 					    result->get_linkage_name().empty()
13809 					      ? result->get_name()
13810 					      : result->get_linkage_name());
13811 	  var_sym = ctxt.variable_symbol_is_exported(var_addr);
13812 	}
13813 
13814       if (var_sym)
13815 	{
13816 	  result->set_symbol(var_sym);
13817 	  // If the linkage name is not set or is wrong, set it to
13818 	  // the name of the underlying symbol.
13819 	  string linkage_name = result->get_linkage_name();
13820 	  if (linkage_name.empty()
13821 	      || !var_sym->get_alias_from_name(linkage_name))
13822 	    result->set_linkage_name(var_sym->get_name());
13823 	  result->set_is_in_public_symbol_table(true);
13824 	}
13825     }
13826 
13827   return result;
13828 }
13829 
13830 /// Test if a given function denoted by its DIE and its scope is
13831 /// suppressed by any of the suppression specifications associated to
13832 /// a given context of ELF/DWARF reading.
13833 ///
13834 /// Note that a non-member function which symbol is not exported is
13835 /// also suppressed.
13836 ///
13837 /// @param ctxt the ELF/DWARF reading content of interest.
13838 ///
13839 /// @param scope of the scope of the function.
13840 ///
13841 /// @param function_die the DIE representing the function.
13842 ///
13843 /// @param is_declaration_only is true if the DIE denoted by @p die is
13844 /// a declaration-only DIE.
13845 ///
13846 /// @return true iff @p function_die is suppressed by at least one
13847 /// suppression specification attached to the @p ctxt.
13848 static bool
function_is_suppressed(const read_context & ctxt,const scope_decl * scope,Dwarf_Die * function_die,bool is_declaration_only)13849 function_is_suppressed(const read_context& ctxt,
13850 		       const scope_decl* scope,
13851 		       Dwarf_Die *function_die,
13852 		       bool is_declaration_only)
13853 {
13854   if (function_die == 0
13855       || dwarf_tag(function_die) != DW_TAG_subprogram)
13856     return false;
13857 
13858   string fname = die_string_attribute(function_die, DW_AT_name);
13859   string flinkage_name = die_linkage_name(function_die);
13860   if (flinkage_name.empty() && ctxt.die_is_in_c(function_die))
13861     flinkage_name = fname;
13862   string qualified_name = build_qualified_name(scope, fname);
13863 
13864   // A non-member non-static function which symbol is not exported is
13865   // suppressed.
13866   //
13867   // Note that if the non-member non-static function has an undefined
13868   // symbol, by default, it's not suppressed.  Unless we are asked to
13869   // drop undefined symbols too.
13870   if (!is_class_type(scope)
13871       && (!is_declaration_only || ctxt.drop_undefined_syms()))
13872     {
13873       Dwarf_Addr fn_addr;
13874       if (!ctxt.get_function_address(function_die, fn_addr))
13875 	return true;
13876 
13877       elf_symbol_sptr symbol = ctxt.function_symbol_is_exported(fn_addr);
13878       if (!symbol)
13879 	return true;
13880       if (!symbol->is_suppressed())
13881 	return false;
13882 
13883       // Since there is only one symbol in DWARF associated with an elf_symbol,
13884       // we can assume this is the main symbol then. Otherwise the main hinting
13885       // did not work as expected.
13886       ABG_ASSERT(symbol->is_main_symbol());
13887       if (symbol->has_aliases())
13888 	for (elf_symbol_sptr a = symbol->get_next_alias();
13889 	     !a->is_main_symbol(); a = a->get_next_alias())
13890 	  if (!a->is_suppressed())
13891 	    return false;
13892     }
13893 
13894   return suppr::function_is_suppressed(ctxt, qualified_name,
13895 				       flinkage_name,
13896 				       /*require_drop_property=*/true);
13897 }
13898 
13899 /// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
13900 /// function denoted by the DIE is not suppressed by a suppression
13901 /// specification associated to the current read context.
13902 ///
13903 /// Note that if a member function declaration with the same signature
13904 /// (pretty representation) as one of the DIE we are looking at
13905 /// exists, this function returns that existing function declaration.
13906 /// Similarly, if there is already a constructed member function with
13907 /// the same linkage name as the one on the DIE, this function returns
13908 /// that member function.
13909 ///
13910 /// Also note that the function_decl IR returned by this function must
13911 /// be passed to finish_member_function_reading because several
13912 /// properties from the DIE are actually read by that function, and
13913 /// the corresponding properties on the function_decl IR are updated
13914 /// accordingly.  This is done to support "updating" a function_decl
13915 /// IR with properties scathered across several DIEs.
13916 ///
13917 /// @param ctxt the read context to use.
13918 ///
13919 /// @param scope the scope of the function we are looking at.
13920 ///
13921 /// @param fn_die the DIE representing the function we are looking at.
13922 ///
13923 /// @param where_offset the offset of the DIE where we are "logically"
13924 /// positionned at, in the DIE tree.  This is useful when @p die is
13925 /// e.g, DW_TAG_partial_unit that can be included in several places in
13926 /// the DIE tree.
13927 ///
13928 /// @param is_declaration_only is true if the DIE denoted by @p fn_die
13929 /// is a declaration-only DIE.
13930 ///
13931 /// @param result if this is set to an existing function_decl, this
13932 /// means that the function will append the new properties it sees on
13933 /// @p fn_die to that exising function_decl.  Otherwise, if this
13934 /// parameter is NULL, a new function_decl is going to be allocated
13935 /// and returned.
13936 ///
13937 /// @return a pointer to the newly created var_decl.  If the var_decl
13938 /// could not be built, this function returns NULL.
13939 static function_decl_sptr
build_or_get_fn_decl_if_not_suppressed(read_context & ctxt,scope_decl * scope,Dwarf_Die * fn_die,size_t where_offset,bool is_declaration_only,function_decl_sptr result)13940 build_or_get_fn_decl_if_not_suppressed(read_context&	  ctxt,
13941 				       scope_decl	  *scope,
13942 				       Dwarf_Die	  *fn_die,
13943 				       size_t		  where_offset,
13944 				       bool		  is_declaration_only,
13945 				       function_decl_sptr result)
13946 {
13947   function_decl_sptr fn;
13948   if (function_is_suppressed(ctxt, scope, fn_die, is_declaration_only))
13949     return fn;
13950 
13951   string name = die_name(fn_die);
13952   string linkage_name = die_linkage_name(fn_die);
13953   bool is_dtor = !name.empty() && name[0]== '~';
13954   bool is_virtual = false;
13955   if (is_dtor)
13956     {
13957       Dwarf_Attribute attr;
13958       if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
13959 			       DW_AT_vtable_elem_location,
13960 			       &attr))
13961 	is_virtual = true;
13962     }
13963 
13964 
13965   // If we've already built an IR for a function with the same
13966   // signature (from another DIE), reuse it, unless that function is a
13967   // virtual C++ destructor.  Several virtual C++ destructors with the
13968   // same signature can be implemented by several different ELF
13969   // symbols.  So re-using C++ destructors like that can lead to us
13970   // missing some destructors.
13971   if (!result && (!(is_dtor && is_virtual)))
13972     if ((fn = is_function_decl(ctxt.lookup_artifact_from_die(fn_die))))
13973       {
13974 	fn = maybe_finish_function_decl_reading(ctxt, fn_die, where_offset, fn);
13975 	ctxt.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
13976 	ctxt.associate_die_to_type(fn_die, fn->get_type(), where_offset);
13977 	return fn;
13978       }
13979 
13980   // If a member function with the same linkage name as the one
13981   // carried by the DIE already exists, then return it.
13982   if (class_decl* klass = is_class_type(scope))
13983     {
13984       string linkage_name = die_linkage_name(fn_die);
13985       fn = klass->find_member_function_sptr(linkage_name);
13986     }
13987 
13988   if (!fn || !fn->get_symbol())
13989     // We haven't yet been able to construct a function IR, or, we
13990     // have one 'partial' function IR that doesn't have any associated
13991     // symbol yet.  Note that in the later case, a function IR without
13992     // any associated symbol will be dropped on the floor by
13993     // potential_member_fn_should_be_dropped.  So let's build or a new
13994     // function IR or complete the existing partial IR.
13995     fn = build_function_decl(ctxt, fn_die, where_offset, result);
13996 
13997   return fn;
13998 }
13999 
14000 /// Test if a given variable denoted by its DIE and its scope is
14001 /// suppressed by any of the suppression specifications associated to
14002 /// a given context of ELF/DWARF reading.
14003 ///
14004 /// @param ctxt the ELF/DWARF reading content of interest.
14005 ///
14006 /// @param scope of the scope of the variable.
14007 ///
14008 /// @param variable_die the DIE representing the variable.
14009 ///
14010 /// @param is_required_decl_spec if true, means that the @p
14011 /// variable_die being considered is for a variable decl that is a
14012 /// specification for a concrete variable being built.
14013 ///
14014 /// @return true iff @p variable_die is suppressed by at least one
14015 /// suppression specification attached to the @p ctxt.
14016 static bool
variable_is_suppressed(const read_context & ctxt,const scope_decl * scope,Dwarf_Die * variable_die,bool is_required_decl_spec)14017 variable_is_suppressed(const read_context& ctxt,
14018 		       const scope_decl* scope,
14019 		       Dwarf_Die *variable_die,
14020 		       bool is_required_decl_spec)
14021 {
14022   if (variable_die == 0
14023       || (dwarf_tag(variable_die) != DW_TAG_variable
14024 	  && dwarf_tag(variable_die) != DW_TAG_member))
14025     return false;
14026 
14027   string name = die_string_attribute(variable_die, DW_AT_name);
14028   string linkage_name = die_linkage_name(variable_die);
14029   if (linkage_name.empty() && ctxt.die_is_in_c(variable_die))
14030     linkage_name = name;
14031   string qualified_name = build_qualified_name(scope, name);
14032 
14033   // If a non member variable that is a declaration (has no defined
14034   // and exported symbol) and is not the specification of another
14035   // concrete variable, then it's suppressed.  This is a size
14036   // optimization; it removes useless declaration-only variables from
14037   // the IR.
14038   if (!is_class_type(scope) && !is_required_decl_spec)
14039     {
14040       Dwarf_Addr var_addr = 0;
14041       if (!ctxt.get_variable_address(variable_die, var_addr))
14042 	return true;
14043 
14044       elf_symbol_sptr symbol = ctxt.variable_symbol_is_exported(var_addr);
14045       if (!symbol)
14046 	return true;
14047       if (!symbol->is_suppressed())
14048 	return false;
14049 
14050       // Since there is only one symbol in DWARF associated with an elf_symbol,
14051       // we can assume this is the main symbol then. Otherwise the main hinting
14052       // did not work as expected.
14053       ABG_ASSERT(symbol->is_main_symbol());
14054       if (symbol->has_aliases())
14055 	for (elf_symbol_sptr a = symbol->get_next_alias();
14056 	     !a->is_main_symbol(); a = a->get_next_alias())
14057 	  if (!a->is_suppressed())
14058 	    return false;
14059     }
14060 
14061   return suppr::variable_is_suppressed(ctxt, qualified_name,
14062 				       linkage_name,
14063 				       /*require_drop_property=*/true);
14064 }
14065 
14066 /// Test if a type (designated by a given DIE) in a given scope is
14067 /// suppressed by the suppression specifications that are associated
14068 /// to a given read context.
14069 ///
14070 /// @param ctxt the read context to consider.
14071 ///
14072 /// @param scope of the scope of the type DIE to consider.
14073 ///
14074 /// @param type_die the DIE that designates the type to consider.
14075 ///
14076 /// @param type_is_private out parameter.  If this function returns
14077 /// true (the type @p type_die is suppressed) and if the type was
14078 /// suppressed because it's private then this parameter is set to
14079 /// true.
14080 ///
14081 /// @return true iff the type designated by the DIE @p type_die, in
14082 /// the scope @p scope is suppressed by at the suppression
14083 /// specifications associated to the current read context.
14084 static bool
type_is_suppressed(const read_context & ctxt,const scope_decl * scope,Dwarf_Die * type_die,bool & type_is_private)14085 type_is_suppressed(const read_context& ctxt,
14086 		   const scope_decl* scope,
14087 		   Dwarf_Die *type_die,
14088 		   bool &type_is_private)
14089 {
14090   if (type_die == 0
14091       || (dwarf_tag(type_die) != DW_TAG_enumeration_type
14092 	  && dwarf_tag(type_die) != DW_TAG_class_type
14093 	  && dwarf_tag(type_die) != DW_TAG_structure_type
14094 	  && dwarf_tag(type_die) != DW_TAG_union_type))
14095     return false;
14096 
14097   string type_name, linkage_name;
14098   location type_location;
14099   die_loc_and_name(ctxt, type_die, type_location, type_name, linkage_name);
14100   string qualified_name = build_qualified_name(scope, type_name);
14101 
14102   return suppr::type_is_suppressed(ctxt, qualified_name,
14103 				   type_location,
14104 				   type_is_private,
14105 				   /*require_drop_property=*/true);
14106 }
14107 
14108 /// Test if a type (designated by a given DIE) in a given scope is
14109 /// suppressed by the suppression specifications that are associated
14110 /// to a given read context.
14111 ///
14112 /// @param ctxt the read context to consider.
14113 ///
14114 /// @param scope of the scope of the type DIE to consider.
14115 ///
14116 /// @param type_die the DIE that designates the type to consider.
14117 ///
14118 /// @return true iff the type designated by the DIE @p type_die, in
14119 /// the scope @p scope is suppressed by at the suppression
14120 /// specifications associated to the current read context.
14121 static bool
type_is_suppressed(const read_context & ctxt,const scope_decl * scope,Dwarf_Die * type_die)14122 type_is_suppressed(const read_context& ctxt,
14123 		   const scope_decl* scope,
14124 		   Dwarf_Die *type_die)
14125 {
14126   bool type_is_private = false;
14127   return type_is_suppressed(ctxt, scope, type_die, type_is_private);
14128 }
14129 
14130 /// Get the opaque version of a type that was suppressed because it's
14131 /// a private type.
14132 ///
14133 /// The opaque version version of the type is just a declared-only
14134 /// version of the type (class, union or enum type) denoted by @p
14135 /// type_die.
14136 ///
14137 /// @param ctxt the read context in use.
14138 ///
14139 /// @param scope the scope of the type die we are looking at.
14140 ///
14141 /// @param type_die the type DIE we are looking at.
14142 ///
14143 /// @param where_offset the offset of the DIE where we are "logically"
14144 /// positionned at, in the DIE tree.  This is useful when @p die is
14145 /// e.g, DW_TAG_partial_unit that can be included in several places in
14146 /// the DIE tree.
14147 ///
14148 /// @return the opaque version of the type denoted by @p type_die or
14149 /// nil if no opaque version was found.
14150 static type_or_decl_base_sptr
get_opaque_version_of_type(read_context & ctxt,scope_decl * scope,Dwarf_Die * type_die,size_t where_offset)14151 get_opaque_version_of_type(read_context	&ctxt,
14152 			   scope_decl		*scope,
14153 			   Dwarf_Die		*type_die,
14154 			   size_t		where_offset)
14155 {
14156   type_or_decl_base_sptr result;
14157 
14158   if (type_die == 0)
14159     return result;
14160 
14161   unsigned tag = dwarf_tag(type_die);
14162   if (tag != DW_TAG_class_type
14163       && tag != DW_TAG_structure_type
14164       && tag != DW_TAG_union_type
14165       && tag != DW_TAG_enumeration_type)
14166     return result;
14167 
14168   string type_name, linkage_name;
14169   location type_location;
14170   die_loc_and_name(ctxt, type_die, type_location, type_name, linkage_name);
14171   if (!type_location)
14172     return result;
14173 
14174   string qualified_name = build_qualified_name(scope, type_name);
14175 
14176   //
14177   // TODO: also handle declaration-only unions.  To do that, we mostly
14178   // need to adapt add_or_update_union_type to make it schedule
14179   // declaration-only unions for resolution too.
14180   //
14181   if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
14182     {
14183       string_classes_map::const_iterator i =
14184 	ctxt.declaration_only_classes().find(qualified_name);
14185       if (i != ctxt.declaration_only_classes().end())
14186 	result = i->second.back();
14187 
14188       if (!result)
14189 	{
14190 	  // So we didn't find any pre-existing forward-declared-only
14191 	  // class for the class definition that we could return as an
14192 	  // opaque type.  So let's build one.
14193 	  //
14194 	  // TODO: we need to be able to do this for unions too!
14195 	  class_decl_sptr klass(new class_decl(ctxt.env(), type_name,
14196 					       /*alignment=*/0, /*size=*/0,
14197 					       tag == DW_TAG_structure_type,
14198 					       type_location,
14199 					       decl_base::VISIBILITY_DEFAULT));
14200 	  klass->set_is_declaration_only(true);
14201 	  klass->set_is_artificial(die_is_artificial(type_die));
14202 	  add_decl_to_scope(klass, scope);
14203 	  ctxt.associate_die_to_type(type_die, klass, where_offset);
14204 	  ctxt.maybe_schedule_declaration_only_class_for_resolution(klass);
14205 	  result = klass;
14206 	}
14207     }
14208 
14209   if (tag == DW_TAG_enumeration_type)
14210     {
14211       string_enums_map::const_iterator i =
14212 	ctxt.declaration_only_enums().find(qualified_name);
14213       if (i != ctxt.declaration_only_enums().end())
14214 	result = i->second.back();
14215 
14216       if (!result)
14217 	{
14218 	  uint64_t size = 0;
14219 	  if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
14220 	    size *= 8;
14221 	  type_decl_sptr underlying_type =
14222 	    build_enum_underlying_type(ctxt, type_name, size,
14223 				       /*anonymous=*/true);
14224 	  enum_type_decl::enumerators enumeratorz;
14225 	  enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
14226 							    type_location,
14227 							    underlying_type,
14228 							    enumeratorz,
14229 							    linkage_name));
14230 	  enum_type->set_is_artificial(die_is_artificial(type_die));
14231 	  add_decl_to_scope(enum_type, scope);
14232 	  result = enum_type;
14233 	}
14234     }
14235 
14236   return result;
14237 }
14238 
14239 /// Create a function symbol with a given name.
14240 ///
14241 /// @param sym_name the name of the symbol to create.
14242 ///
14243 /// @param env the environment to create the symbol in.
14244 ///
14245 /// @return the newly created symbol.
14246 elf_symbol_sptr
create_default_fn_sym(const string & sym_name,const environment * env)14247 create_default_fn_sym(const string& sym_name, const environment *env)
14248 {
14249   elf_symbol::version ver;
14250   elf_symbol_sptr result =
14251     elf_symbol::create(env,
14252 		       /*symbol index=*/ 0,
14253 		       /*symbol size=*/ 0,
14254 		       sym_name,
14255 		       /*symbol type=*/ elf_symbol::FUNC_TYPE,
14256 		       /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
14257 		       /*symbol is defined=*/ true,
14258 		       /*symbol is common=*/ false,
14259 		       /*symbol version=*/ ver,
14260 		       /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
14261   return result;
14262 }
14263 
14264 /// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
14265 ///
14266 /// @param ctxt the read context to use
14267 ///
14268 /// @param die the DW_TAG_subprogram DIE to read from.
14269 ///
14270 /// @param where_offset the offset of the DIE where we are "logically"
14271 /// positionned at, in the DIE tree.  This is useful when @p die is
14272 /// e.g, DW_TAG_partial_unit that can be included in several places in
14273 /// the DIE tree.
14274 ///
14275 /// @param called_for_public_decl this is set to true if the function
14276 /// was called for a public (function) decl.
14277 static function_decl_sptr
build_function_decl(read_context & ctxt,Dwarf_Die * die,size_t where_offset,function_decl_sptr fn)14278 build_function_decl(read_context&	ctxt,
14279 		    Dwarf_Die*		die,
14280 		    size_t		where_offset,
14281 		    function_decl_sptr	fn)
14282 {
14283   function_decl_sptr result = fn;
14284   if (!die)
14285     return result;
14286   ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
14287 
14288   if (!die_is_public_decl(die))
14289     return result;
14290 
14291   translation_unit_sptr tu = ctxt.cur_transl_unit();
14292   ABG_ASSERT(tu);
14293 
14294   string fname, flinkage_name;
14295   location floc;
14296   die_loc_and_name(ctxt, die, floc, fname, flinkage_name);
14297 
14298   size_t is_inline = die_is_declared_inline(die);
14299   class_or_union_sptr is_method =
14300     is_class_or_union_type(get_scope_for_die(ctxt, die, true, where_offset));
14301 
14302   if (result)
14303     {
14304       // Add the properties that might have been missing from the
14305       // first declaration of the function.  For now, it usually is
14306       // the mangled name that goes missing in the first declarations.
14307       //
14308       // Also note that if 'fn' has just been cloned, the current
14309       // linkage name (of the current DIE) might be different from the
14310       // linkage name of 'fn'.  In that case, update the linkage name
14311       // of 'fn' too.
14312       if (!flinkage_name.empty()
14313 	  && result->get_linkage_name() != flinkage_name)
14314 	result->set_linkage_name(flinkage_name);
14315       if (floc)
14316 	if (!result->get_location())
14317 	  result->set_location(floc);
14318     }
14319   else
14320     {
14321       function_type_sptr fn_type(build_function_type(ctxt, die, is_method,
14322 						     where_offset));
14323       if (!fn_type)
14324 	return result;
14325 
14326       maybe_canonicalize_type(fn_type, ctxt);
14327 
14328       result.reset(is_method
14329 		   ? new method_decl(fname, fn_type,
14330 				     is_inline, floc,
14331 				     flinkage_name)
14332 		   : new function_decl(fname, fn_type,
14333 				       is_inline, floc,
14334 				       flinkage_name));
14335     }
14336 
14337   // Set the symbol of the function.  If the linkage name is not set
14338   // or is wrong, set it to the name of the underlying symbol.
14339   if (!result->get_symbol())
14340     {
14341       elf_symbol_sptr fn_sym;
14342       Dwarf_Addr      fn_addr;
14343       if (ctxt.get_function_address(die, fn_addr))
14344 	{
14345 	  ctxt.symtab()->update_main_symbol(fn_addr,
14346 					    result->get_linkage_name().empty()
14347 					      ? result->get_name()
14348 					      : result->get_linkage_name());
14349 	  fn_sym = ctxt.function_symbol_is_exported(fn_addr);
14350 	}
14351 
14352       if (fn_sym && !ctxt.symbol_already_belongs_to_a_function(fn_sym))
14353 	{
14354 	  result->set_symbol(fn_sym);
14355 	  string linkage_name = result->get_linkage_name();
14356 	  if (linkage_name.empty()
14357 	      || !fn_sym->get_alias_from_name(linkage_name))
14358 	    result->set_linkage_name(fn_sym->get_name());
14359 	  result->set_is_in_public_symbol_table(true);
14360 	}
14361     }
14362 
14363   ctxt.associate_die_to_type(die, result->get_type(), where_offset);
14364 
14365   size_t die_offset = dwarf_dieoffset(die);
14366 
14367   if (fn
14368       && is_member_function(fn)
14369       && get_member_function_is_virtual(fn)
14370       && !result->get_linkage_name().empty())
14371     // This function is a virtual member function which has its
14372     // linkage name *and* and has its underlying symbol correctly set.
14373     // It thus doesn't need any fixup related to elf symbol.  So
14374     // remove it from the set of virtual member functions with linkage
14375     // names and no elf symbol that need to be fixed up.
14376     ctxt.die_function_decl_with_no_symbol_map().erase(die_offset);
14377   return result;
14378 }
14379 
14380 /// Read all @ref abigail::translation_unit possible from the debug info
14381 /// accessible through a DWARF Front End Library handle, and stuff
14382 /// them into a libabigail ABI Corpus.
14383 ///
14384 /// @param ctxt the read context.
14385 ///
14386 /// @return a pointer to the resulting corpus, or NULL if the corpus
14387 /// could not be constructed.
14388 static corpus_sptr
read_debug_info_into_corpus(read_context & ctxt)14389 read_debug_info_into_corpus(read_context& ctxt)
14390 {
14391   ctxt.clear_per_corpus_data();
14392   ctxt.current_corpus(std::make_shared<corpus>(ctxt.env(), ctxt.elf_path()));
14393 
14394   // First set some mundane properties of the corpus gathered from
14395   // ELF.
14396   ctxt.current_corpus()->set_path(ctxt.elf_path());
14397   if (is_linux_kernel(ctxt.elf_handle()))
14398     ctxt.current_corpus()->set_origin(corpus::LINUX_KERNEL_BINARY_ORIGIN);
14399   else
14400     ctxt.current_corpus()->set_origin(corpus::DWARF_ORIGIN);
14401   ctxt.current_corpus()->set_soname(ctxt.dt_soname());
14402   ctxt.current_corpus()->set_needed(ctxt.dt_needed());
14403   ctxt.current_corpus()->set_architecture_name(ctxt.elf_architecture());
14404   if (corpus_group_sptr group = ctxt.current_corpus_group())
14405     group->add_corpus(ctxt.current_corpus());
14406 
14407   // Set symbols information to the corpus.
14408   ctxt.current_corpus()->set_symtab(ctxt.symtab());
14409 
14410   // Get out now if no debug info is found.
14411   if (!ctxt.dwarf())
14412     return ctxt.current_corpus();
14413 
14414   uint8_t address_size = 0;
14415   size_t header_size = 0;
14416 
14417   // Set the set of exported declaration that are defined.
14418   ctxt.exported_decls_builder
14419     (ctxt.current_corpus()->get_exported_decls_builder().get());
14420 
14421 #ifdef WITH_DEBUG_SELF_COMPARISON
14422   if (ctxt.env()->self_comparison_debug_is_on())
14423     ctxt.env()->set_self_comparison_debug_input(ctxt.current_corpus());
14424 #endif
14425 
14426   // Walk all the DIEs of the debug info to build a DIE -> parent map
14427   // useful for get_die_parent() to work.
14428   {
14429     tools_utils::timer t;
14430     if (ctxt.do_log())
14431       {
14432 	cerr << "building die -> parent maps ...";
14433 	t.start();
14434       }
14435 
14436     ctxt.build_die_parent_maps();
14437 
14438     if (ctxt.do_log())
14439       {
14440 	t.stop();
14441 	cerr << " DONE@" << ctxt.current_corpus()->get_path()
14442 	     << ":"
14443 	     << t
14444 	     << "\n";
14445       }
14446   }
14447 
14448   ctxt.env()->canonicalization_is_done(false);
14449 
14450   {
14451     tools_utils::timer t;
14452     if (ctxt.do_log())
14453       {
14454 	cerr << "building the libabigail internal representation ...";
14455 	t.start();
14456       }
14457     // And now walk all the DIEs again to build the libabigail IR.
14458     Dwarf_Half dwarf_version = 0;
14459     for (Dwarf_Off offset = 0, next_offset = 0;
14460 	 (dwarf_next_unit(ctxt.dwarf(), offset, &next_offset, &header_size,
14461 			  &dwarf_version, NULL, &address_size, NULL,
14462 			  NULL, NULL) == 0);
14463 	 offset = next_offset)
14464       {
14465 	Dwarf_Off die_offset = offset + header_size;
14466 	Dwarf_Die unit;
14467 	if (!dwarf_offdie(ctxt.dwarf(), die_offset, &unit)
14468 	    || dwarf_tag(&unit) != DW_TAG_compile_unit)
14469 	  continue;
14470 
14471 	ctxt.dwarf_version(dwarf_version);
14472 
14473 	address_size *= 8;
14474 
14475 	// Build a translation_unit IR node from cu; note that cu must
14476 	// be a DW_TAG_compile_unit die.
14477 	translation_unit_sptr ir_node =
14478 	  build_translation_unit_and_add_to_ir(ctxt, &unit, address_size);
14479 	ABG_ASSERT(ir_node);
14480       }
14481     if (ctxt.do_log())
14482       {
14483 	t.stop();
14484 	cerr << " DONE@" << ctxt.current_corpus()->get_path()
14485 	     << ":"
14486 	     << t
14487 	     << "\n";
14488       }
14489   }
14490 
14491   {
14492     tools_utils::timer t;
14493     if (ctxt.do_log())
14494       {
14495 	cerr << "resolving declaration only classes ...";
14496 	t.start();
14497       }
14498     ctxt.resolve_declaration_only_classes();
14499     if (ctxt.do_log())
14500       {
14501 	t.stop();
14502 	cerr << " DONE@" << ctxt.current_corpus()->get_path()
14503 	     << ":"
14504 	     << t
14505 	     <<"\n";
14506       }
14507   }
14508 
14509   {
14510     tools_utils::timer t;
14511     if (ctxt.do_log())
14512       {
14513 	cerr << "resolving declaration only enums ...";
14514 	t.start();
14515       }
14516     ctxt.resolve_declaration_only_enums();
14517     if (ctxt.do_log())
14518       {
14519 	t.stop();
14520 	cerr << " DONE@" << ctxt.current_corpus()->get_path()
14521 	     << ":"
14522 	     << t
14523 	     <<"\n";
14524       }
14525   }
14526 
14527   {
14528     tools_utils::timer t;
14529     if (ctxt.do_log())
14530       {
14531 	cerr << "fixing up functions with linkage name but "
14532 	     << "no advertised underlying symbols ....";
14533 	t.start();
14534       }
14535     ctxt.fixup_functions_with_no_symbols();
14536     if (ctxt.do_log())
14537       {
14538 	t.stop();
14539 	cerr << " DONE@" << ctxt.current_corpus()->get_path()
14540 	     <<":"
14541 	     << t
14542 	     <<"\n";
14543       }
14544   }
14545 
14546   /// Now, look at the types that needs to be canonicalized after the
14547   /// translation has been constructed (which is just now) and
14548   /// canonicalize them.
14549   ///
14550   /// These types need to be constructed at the end of the translation
14551   /// unit reading phase because some types are modified by some DIEs
14552   /// even after the principal DIE describing the type has been read;
14553   /// this happens for clones of virtual destructors (for instance) or
14554   /// even for some static data members.  We need to do that for types
14555   /// are in the alternate debug info section and for types that in
14556   /// the main debug info section.
14557   {
14558     tools_utils::timer t;
14559     if (ctxt.do_log())
14560       {
14561 	cerr << "perform late type canonicalizing ...\n";
14562 	t.start();
14563       }
14564 
14565     ctxt.perform_late_type_canonicalizing();
14566     if (ctxt.do_log())
14567       {
14568 	t.stop();
14569 	cerr << "late type canonicalizing DONE@"
14570 	     << ctxt.current_corpus()->get_path()
14571 	     << ":"
14572 	     << t
14573 	     << "\n";
14574       }
14575   }
14576 
14577   ctxt.env()->canonicalization_is_done(true);
14578 
14579   {
14580     tools_utils::timer t;
14581     if (ctxt.do_log())
14582       {
14583 	cerr << "sort functions and variables ...";
14584 	t.start();
14585       }
14586     ctxt.current_corpus()->sort_functions();
14587     ctxt.current_corpus()->sort_variables();
14588     if (ctxt.do_log())
14589       {
14590 	t.stop();
14591 	cerr << " DONE@" << ctxt.current_corpus()->get_path()
14592 	     << ":"
14593 	     << t
14594 	     <<" \n";
14595       }
14596   }
14597 
14598 #ifdef WITH_DEBUG_SELF_COMPARISON
14599   if (ctxt.env()->self_comparison_debug_is_on())
14600     ctxt.env()->set_self_comparison_debug_input(ctxt.current_corpus());
14601 #endif
14602 
14603   return ctxt.current_corpus();
14604 }
14605 
14606 /// Canonicalize a type if it's suitable for early canonicalizing, or,
14607 /// if it's not, schedule it for late canonicalization, after the
14608 /// debug info of the current translation unit has been fully read.
14609 ///
14610 /// A (composite) type is deemed suitable for early canonicalizing iff
14611 /// all of its sub-types are canonicalized themselve.  Non composite
14612 /// types are always deemed suitable for early canonicalization.
14613 ///
14614 /// Note that this function doesn't work on *ANONYMOUS* classes,
14615 /// structs, unions or enums because it first does some
14616 /// canonicalization of the DWARF DIE @p die.  That canonicalization
14617 /// is done by looking up @p die by name; and because these are
14618 /// anonymous types, they don't have names! and so that
14619 /// canonicalization fails.  So the type artifact associated to @p
14620 /// die often ends being *NOT* canonicalized.  This later leads to
14621 /// extreme slowness of operation, especially when comparisons are
14622 /// later performed on these anonymous types.
14623 ///
14624 /// So when you have classes, structs, unions, or enums that can be
14625 /// anonymous, please use this overload instead:
14626 ///
14627 ///     void
14628 ///     maybe_canonicalize_type(const Dwarf_Die*	die,
14629 ///				const type_base_sptr&	t,
14630 ///				read_context&		ctxt);
14631 ///
14632 /// It knows how to deal with anonymous types.
14633 ///
14634 /// @p looks up the type artifact
14635 /// associated to @p die.  During that lookup, ; but then those types don't have
14636 /// names because they are anonymous.
14637 ///
14638 /// @param die the type DIE to consider for canonicalization.  Note
14639 /// that this DIE must have been associated with its type using the
14640 /// function read_context::associate_die_to_type() prior to calling
14641 /// this function.
14642 ///
14643 /// @param ctxt the @ref read_context to use.
14644 static void
maybe_canonicalize_type(const Dwarf_Die * die,read_context & ctxt)14645 maybe_canonicalize_type(const Dwarf_Die *die, read_context& ctxt)
14646 {
14647   const die_source source = ctxt.get_die_source(die);
14648 
14649   size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
14650   type_base_sptr t = ctxt.lookup_type_from_die(die);
14651 
14652   if (!t)
14653     return;
14654 
14655   type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
14656   if (is_class_type(peeled_type)
14657       || is_union_type(peeled_type)
14658       || is_function_type(peeled_type)
14659       || is_array_type(peeled_type)
14660       || is_qualified_type(peeled_type)
14661       || is_typedef(t))
14662     // We delay canonicalization of classes/unions or typedef,
14663     // pointers, references and array to classes/unions.  This is
14664     // because the (underlying) class might not be finished yet and we
14665     // might not be able to able detect it here (thinking about
14666     // classes that are work-in-progress, or classes that might be
14667     // later amended by some DWARF construct).  So we err on the safe
14668     // side.  We also delay canonicalization for array and qualified
14669     // types because they can be edited (in particular by
14670     // maybe_strip_qualification) after they are initially built.
14671     ctxt.schedule_type_for_late_canonicalization(die);
14672   else if (is_decl(t) && is_decl(t)->get_is_anonymous())
14673     ctxt.schedule_type_for_late_canonicalization(t);
14674   else if ((is_function_type(t)
14675 	    && ctxt.is_wip_function_type_die_offset(die_offset, source))
14676 	   || type_has_non_canonicalized_subtype(t))
14677     ctxt.schedule_type_for_late_canonicalization(die);
14678   else
14679     canonicalize(t);
14680 }
14681 
14682 /// Canonicalize a type if it's suitable for early canonicalizing, or,
14683 /// if it's not, schedule it for late canonicalization, after the
14684 /// debug info of the current translation unit has been fully read.
14685 ///
14686 /// A (composite) type is deemed suitable for early canonicalizing iff
14687 /// all of its sub-types are canonicalized themselve.  Non composite
14688 /// types are always deemed suitable for early canonicalization.
14689 ///
14690 /// Note that this function nows how to deal with anonymous classes,
14691 /// structs and enums, unlike the overload below:
14692 ///
14693 ///     void maybe_canonicalize_type(const Dwarf_Die *die, read_context& ctxt)
14694 ///
14695 /// The problem, though is that this function is much slower that that
14696 /// overload above because of how the types that are meant for later
14697 /// canonicalization are stored.  So the idea is that this function
14698 /// should be used only for the smallest possible subset of types that
14699 /// are anonymous and thus cannot be handled by the overload above.
14700 ///
14701 /// @param t the type DIE to consider for canonicalization.
14702 ///
14703 /// @param ctxt the @ref read_context to use.
14704 static void
maybe_canonicalize_type(const type_base_sptr & t,read_context & ctxt)14705 maybe_canonicalize_type(const type_base_sptr& t,
14706 			read_context&	ctxt)
14707 {
14708   if (!t)
14709     return;
14710 
14711   type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
14712   if (is_class_type(peeled_type)
14713       || is_union_type(peeled_type)
14714       || is_function_type(peeled_type)
14715       || is_array_type(peeled_type)
14716       || is_qualified_type(peeled_type)
14717       ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
14718     // We delay canonicalization of classes/unions or typedef,
14719     // pointers, references and array to classes/unions.  This is
14720     // because the (underlying) class might not be finished yet and we
14721     // might not be able to able detect it here (thinking about
14722     // classes that are work-in-progress, or classes that might be
14723     // later amended by some DWARF construct).  So we err on the safe
14724     // side.  We also delay canonicalization for array and qualified
14725     // types because they can be edited (in particular by
14726     // maybe_strip_qualification) after they are initially built.
14727     ctxt.schedule_type_for_late_canonicalization(t);
14728   else if (type_has_non_canonicalized_subtype(t))
14729     ctxt.schedule_type_for_late_canonicalization(t);
14730   else
14731     canonicalize(t);
14732 }
14733 
14734 /// If a given decl is a member type declaration, set its access
14735 /// specifier from the DIE that represents it.
14736 ///
14737 /// @param member_type_declaration the member type declaration to
14738 /// consider.
14739 static void
maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,Dwarf_Die * die)14740 maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
14741 				       Dwarf_Die* die)
14742 {
14743   if (is_type(member_type_declaration)
14744       && is_member_decl(member_type_declaration))
14745     {
14746       class_or_union* scope =
14747 	is_class_or_union_type(member_type_declaration->get_scope());
14748       ABG_ASSERT(scope);
14749 
14750       access_specifier access = public_access;
14751       if (class_decl* cl = is_class_type(scope))
14752 	if (!cl->is_struct())
14753 	  access = private_access;
14754 
14755       die_access_specifier(die, access);
14756       set_member_access_specifier(member_type_declaration, access);
14757     }
14758 }
14759 
14760 /// This function tests if a given function which might be intented to
14761 /// be added to a class scope (to become a member function) should be
14762 /// dropped on the floor instead and not be added to the class.
14763 ///
14764 /// This is a subroutine of build_ir_node_from_die.
14765 ///
14766 /// @param fn the function to consider.
14767 ///
14768 /// @param scope the scope the function is intended to be added
14769 /// to. This might be of class type or not.
14770 ///
14771 /// @param fn_die the DWARF die of @p fn.
14772 ///
14773 /// @return true iff @p fn should be dropped on the floor.
14774 static bool
potential_member_fn_should_be_dropped(const function_decl_sptr & fn,Dwarf_Die * fn_die)14775 potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
14776 				      Dwarf_Die *fn_die)
14777 {
14778   if (!fn || fn->get_scope())
14779     return false;
14780 
14781   if (// A function that is not virtual ...
14782       !die_is_virtual(fn_die)
14783       // ... has a linkage name ...
14784       && !fn->get_linkage_name().empty()
14785       // .. and yet has no ELF symbol associated ...
14786       && !fn->get_symbol())
14787     // Should not be added to its class scope.
14788     //
14789     // Why would it? It's not part of the ABI anyway, as it doesn't
14790     // have any ELF symbol associated and is not a virtual member
14791     // function.  It just constitutes bloat in the IR and might even
14792     // induce spurious change reports down the road.
14793     return true;
14794 
14795   return false;
14796 }
14797 
14798 /// Build an IR node from a given DIE and add the node to the current
14799 /// IR being build and held in the read_context.  Doing that is called
14800 /// "emitting an IR node for the DIE".
14801 ///
14802 /// @param ctxt the read context.
14803 ///
14804 /// @param die the DIE to consider.
14805 ///
14806 /// @param scope the scope under which the resulting IR node has to be
14807 /// added.
14808 ///
14809 /// @param called_from_public_decl set to yes if this function is
14810 /// called from the functions used to build a public decl (functions
14811 /// and variables).  In that case, this function accepts building IR
14812 /// nodes representing types.  Otherwise, this function only creates
14813 /// IR nodes representing public decls (functions and variables).
14814 /// This is done to avoid emitting IR nodes for types that are not
14815 /// referenced by public functions or variables.
14816 ///
14817 /// @param where_offset the offset of the DIE where we are "logically"
14818 /// positionned at, in the DIE tree.  This is useful when @p die is
14819 /// e.g, DW_TAG_partial_unit that can be included in several places in
14820 /// the DIE tree.
14821 ///
14822 /// @param is_required_decl_spec if true, it means the ir node to
14823 /// build is for a decl that is a specification for another decl that
14824 /// is concrete.  If you don't know what this is, set it to false.
14825 ///
14826 /// @param is_declaration_only is true if the DIE denoted by @p die is
14827 /// a declaration-only DIE.
14828 ///
14829 /// @return the resulting IR node.
14830 static type_or_decl_base_sptr
build_ir_node_from_die(read_context & ctxt,Dwarf_Die * die,scope_decl * scope,bool called_from_public_decl,size_t where_offset,bool is_declaration_only,bool is_required_decl_spec)14831 build_ir_node_from_die(read_context&	ctxt,
14832 		       Dwarf_Die*	die,
14833 		       scope_decl*	scope,
14834 		       bool		called_from_public_decl,
14835 		       size_t		where_offset,
14836 		       bool		is_declaration_only,
14837 		       bool		is_required_decl_spec)
14838 {
14839   type_or_decl_base_sptr result;
14840 
14841   if (!die || !scope)
14842     return result;
14843 
14844   int tag = dwarf_tag(die);
14845 
14846   if (!called_from_public_decl)
14847     {
14848       if (ctxt.load_all_types() && die_is_type(die))
14849 	/* We were instructed to load debug info for all types,
14850 	   included those that are not reachable from a public
14851 	   declaration.  So load the debug info for this type.  */;
14852       else if (tag != DW_TAG_subprogram
14853 	       && tag != DW_TAG_variable
14854 	       && tag != DW_TAG_member
14855 	       && tag != DW_TAG_namespace)
14856 	return result;
14857     }
14858 
14859   const die_source source_of_die = ctxt.get_die_source(die);
14860 
14861   if ((result = ctxt.lookup_decl_from_die_offset(dwarf_dieoffset(die),
14862 						 source_of_die)))
14863     {
14864       if (ctxt.load_all_types())
14865 	if (called_from_public_decl)
14866 	  if (type_base_sptr t = is_type(result))
14867 	    if (corpus *abi_corpus = scope->get_corpus())
14868 	      abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
14869 
14870       return result;
14871     }
14872 
14873   // This is *the* bit of code that ensures we have the right notion
14874   // of "declared" at any point in a DIE chain formed from
14875   // DW_AT_abstract_origin and DW_AT_specification links. There should
14876   // be no other callers of die_is_declaration_only.
14877   is_declaration_only = is_declaration_only && die_is_declaration_only(die);
14878 
14879   switch (tag)
14880     {
14881       // Type DIEs we support.
14882     case DW_TAG_base_type:
14883       if (type_decl_sptr t = build_type_decl(ctxt, die, where_offset))
14884 	{
14885 	  result =
14886 	    add_decl_to_scope(t, ctxt.cur_transl_unit()->get_global_scope());
14887 	  canonicalize(t);
14888 	}
14889       break;
14890 
14891     case DW_TAG_typedef:
14892       {
14893 	typedef_decl_sptr t = build_typedef_type(ctxt, die,
14894 						 called_from_public_decl,
14895 						 where_offset);
14896 	result = add_decl_to_scope(t, scope);
14897 	if (result)
14898 	  {
14899 	    maybe_set_member_type_access_specifier(is_decl(result), die);
14900 	    maybe_canonicalize_type(die, ctxt);
14901 	  }
14902       }
14903       break;
14904 
14905     case DW_TAG_pointer_type:
14906       {
14907 	pointer_type_def_sptr p =
14908 	  build_pointer_type_def(ctxt, die,
14909 				 called_from_public_decl,
14910 				 where_offset);
14911 	if (p)
14912 	  {
14913 	    result =
14914 	      add_decl_to_scope(p, ctxt.cur_transl_unit()->get_global_scope());
14915 	    ABG_ASSERT(result->get_translation_unit());
14916 	    maybe_canonicalize_type(die, ctxt);
14917 	  }
14918       }
14919       break;
14920 
14921     case DW_TAG_reference_type:
14922     case DW_TAG_rvalue_reference_type:
14923       {
14924 	reference_type_def_sptr r =
14925 	  build_reference_type(ctxt, die,
14926 			       called_from_public_decl,
14927 			       where_offset);
14928 	if (r)
14929 	  {
14930 	    result =
14931 	      add_decl_to_scope(r, ctxt.cur_transl_unit()->get_global_scope());
14932 
14933 	    ctxt.associate_die_to_type(die, r, where_offset);
14934 	    maybe_canonicalize_type(die, ctxt);
14935 	  }
14936       }
14937       break;
14938 
14939     case DW_TAG_const_type:
14940     case DW_TAG_volatile_type:
14941     case DW_TAG_restrict_type:
14942       {
14943 	type_base_sptr q =
14944 	  build_qualified_type(ctxt, die,
14945 			       called_from_public_decl,
14946 			       where_offset);
14947 	if (q)
14948 	  {
14949 	    // Strip some potentially redundant type qualifiers from
14950 	    // the qualified type we just built.
14951 	    decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
14952 							 ctxt);
14953 	    if (!d)
14954 	      d = get_type_declaration(q);
14955 	    ABG_ASSERT(d);
14956 	    type_base_sptr ty = is_type(d);
14957 	    // Associate the die to type ty again because 'ty'might be
14958 	    // different from 'q', because 'ty' is 'q' possibly
14959 	    // stripped from some redundant type qualifier.
14960 	    ctxt.associate_die_to_type(die, ty, where_offset);
14961 	    result =
14962 	      add_decl_to_scope(d, ctxt.cur_transl_unit()->get_global_scope());
14963 	    maybe_canonicalize_type(die, ctxt);
14964 	  }
14965       }
14966       break;
14967 
14968     case DW_TAG_enumeration_type:
14969       {
14970 	bool type_is_private = false;
14971 	bool type_suppressed =
14972 	  type_is_suppressed(ctxt, scope, die, type_is_private);
14973 	if (type_suppressed && type_is_private)
14974 	  {
14975 	    // The type is suppressed because it's private.  If other
14976 	    // non-suppressed and declaration-only instances of this
14977 	    // type exist in the current corpus, then it means those
14978 	    // non-suppressed instances are opaque versions of the
14979 	    // suppressed private type.  Lets return one of these opaque
14980 	    // types then.
14981 	    result = get_opaque_version_of_type(ctxt, scope, die, where_offset);
14982 	    maybe_canonicalize_type(is_type(result), ctxt);
14983 	  }
14984 	else if (!type_suppressed)
14985 	  {
14986 	    enum_type_decl_sptr e = build_enum_type(ctxt, die, scope,
14987 						    where_offset,
14988 						    is_declaration_only);
14989 	    result = add_decl_to_scope(e, scope);
14990 	    if (result)
14991 	      {
14992 		maybe_set_member_type_access_specifier(is_decl(result), die);
14993 		maybe_canonicalize_type(die, ctxt);
14994 	      }
14995 	  }
14996       }
14997       break;
14998 
14999     case DW_TAG_class_type:
15000     case DW_TAG_structure_type:
15001       {
15002 	bool type_is_private = false;
15003 	bool type_suppressed=
15004 	  type_is_suppressed(ctxt, scope, die, type_is_private);
15005 
15006 	if (type_suppressed && type_is_private)
15007 	  {
15008 	    // The type is suppressed because it's private.  If other
15009 	    // non-suppressed and declaration-only instances of this
15010 	    // type exist in the current corpus, then it means those
15011 	    // non-suppressed instances are opaque versions of the
15012 	    // suppressed private type.  Lets return one of these opaque
15013 	    // types then.
15014 	    result = get_opaque_version_of_type(ctxt, scope, die, where_offset);
15015 	    maybe_canonicalize_type(is_type(result), ctxt);
15016 	  }
15017 	else if (!type_suppressed)
15018 	  {
15019 	    Dwarf_Die spec_die;
15020 	    scope_decl_sptr scop;
15021 	    class_decl_sptr klass;
15022 	    if (die_die_attribute(die, DW_AT_specification, spec_die))
15023 	      {
15024 		scope_decl_sptr skope =
15025 		  get_scope_for_die(ctxt, &spec_die,
15026 				    called_from_public_decl,
15027 				    where_offset);
15028 		ABG_ASSERT(skope);
15029 		decl_base_sptr cl =
15030 		  is_decl(build_ir_node_from_die(ctxt, &spec_die,
15031 						 skope.get(),
15032 						 called_from_public_decl,
15033 						 where_offset,
15034 						 is_declaration_only,
15035 						 /*is_required_decl_spec=*/false));
15036 		ABG_ASSERT(cl);
15037 		klass = dynamic_pointer_cast<class_decl>(cl);
15038 		ABG_ASSERT(klass);
15039 
15040 		klass =
15041 		  add_or_update_class_type(ctxt, die,
15042 					   skope.get(),
15043 					   tag == DW_TAG_structure_type,
15044 					   klass,
15045 					   called_from_public_decl,
15046 					   where_offset,
15047 					   is_declaration_only);
15048 	      }
15049 	    else
15050 	      klass =
15051 		add_or_update_class_type(ctxt, die, scope,
15052 					 tag == DW_TAG_structure_type,
15053 					 class_decl_sptr(),
15054 					 called_from_public_decl,
15055 					 where_offset,
15056 					 is_declaration_only);
15057 	    result = klass;
15058 	    if (klass)
15059 	      {
15060 		maybe_set_member_type_access_specifier(klass, die);
15061 		maybe_canonicalize_type(klass, ctxt);
15062 	      }
15063 	  }
15064       }
15065       break;
15066     case DW_TAG_union_type:
15067       if (!type_is_suppressed(ctxt, scope, die))
15068 	{
15069 	  union_decl_sptr union_type =
15070 	    add_or_update_union_type(ctxt, die, scope,
15071 				     union_decl_sptr(),
15072 				     called_from_public_decl,
15073 				     where_offset,
15074 				     is_declaration_only);
15075 	  if (union_type)
15076 	    {
15077 	      maybe_set_member_type_access_specifier(union_type, die);
15078 	      maybe_canonicalize_type(union_type, ctxt);
15079 	    }
15080 	  result = union_type;
15081 	}
15082       break;
15083     case DW_TAG_string_type:
15084       break;
15085     case DW_TAG_subroutine_type:
15086       {
15087 	function_type_sptr f = build_function_type(ctxt, die,
15088 						   class_decl_sptr(),
15089 						   where_offset);
15090 	if (f)
15091 	  {
15092 	    result = f;
15093 	    result->set_is_artificial(false);
15094 	    maybe_canonicalize_type(die, ctxt);
15095 	  }
15096       }
15097       break;
15098     case DW_TAG_array_type:
15099       {
15100 	array_type_def_sptr a = build_array_type(ctxt,
15101 						 die,
15102 						 called_from_public_decl,
15103 						 where_offset);
15104 	if (a)
15105 	  {
15106 	    result =
15107 	      add_decl_to_scope(a, ctxt.cur_transl_unit()->get_global_scope());
15108 	    ctxt.associate_die_to_type(die, a, where_offset);
15109 	    maybe_canonicalize_type(die, ctxt);
15110 	  }
15111 	break;
15112       }
15113     case DW_TAG_subrange_type:
15114       {
15115 	// If we got here, this means the subrange type is a "free
15116 	// form" defined in the global namespace of the current
15117 	// translation unit, like what is found in Ada.
15118 	array_type_def::subrange_sptr s =
15119 	  build_subrange_type(ctxt, die, where_offset);
15120 	if (s)
15121 	  {
15122 	    result =
15123 	      add_decl_to_scope(s, ctxt.cur_transl_unit()->get_global_scope());
15124 	    ctxt.associate_die_to_type(die, s, where_offset);
15125 	    maybe_canonicalize_type(die, ctxt);
15126 	  }
15127       }
15128       break;
15129     case DW_TAG_packed_type:
15130       break;
15131     case DW_TAG_set_type:
15132       break;
15133     case DW_TAG_file_type:
15134       break;
15135     case DW_TAG_ptr_to_member_type:
15136       break;
15137     case DW_TAG_thrown_type:
15138       break;
15139     case DW_TAG_interface_type:
15140       break;
15141     case DW_TAG_unspecified_type:
15142       break;
15143     case DW_TAG_shared_type:
15144       break;
15145 
15146     case DW_TAG_compile_unit:
15147       // We shouldn't reach this point b/c this should be handled by
15148       // build_translation_unit.
15149       ABG_ASSERT_NOT_REACHED;
15150 
15151     case DW_TAG_namespace:
15152     case DW_TAG_module:
15153       result = build_namespace_decl_and_add_to_ir(ctxt, die, where_offset);
15154       break;
15155 
15156     case DW_TAG_variable:
15157     case DW_TAG_member:
15158       {
15159 	Dwarf_Die spec_die;
15160 	bool var_is_cloned = false;
15161 
15162 	if (tag == DW_TAG_member)
15163 	  ABG_ASSERT(!is_c_language(ctxt.cur_transl_unit()->get_language()));
15164 
15165 	if (die_die_attribute(die, DW_AT_specification, spec_die, false)
15166 	    || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
15167 						  spec_die, false)))
15168 	  {
15169 	    scope_decl_sptr spec_scope =
15170 	      get_scope_for_die(ctxt, &spec_die,
15171 				/*called_from_public_decl=*/
15172 				die_is_effectively_public_decl(ctxt, die),
15173 				where_offset);
15174 	    if (spec_scope)
15175 	      {
15176 		decl_base_sptr d =
15177 		  is_decl(build_ir_node_from_die(ctxt, &spec_die,
15178 						 spec_scope.get(),
15179 						 called_from_public_decl,
15180 						 where_offset,
15181 						 is_declaration_only,
15182 						 /*is_required_decl_spec=*/true));
15183 		if (d)
15184 		  {
15185 		    var_decl_sptr m =
15186 		      dynamic_pointer_cast<var_decl>(d);
15187 		    if (var_is_cloned)
15188 		      m = m->clone();
15189 		    m = build_var_decl(ctxt, die, where_offset, m);
15190 		    if (is_data_member(m))
15191 		      {
15192 			set_member_is_static(m, true);
15193 			ctxt.associate_die_to_decl(die, m, where_offset,
15194 						   /*associate_by_repr=*/false);
15195 		      }
15196 		    else
15197 		      {
15198 			ABG_ASSERT(has_scope(m));
15199 			ctxt.var_decls_to_re_add_to_tree().push_back(m);
15200 		      }
15201 		    ABG_ASSERT(m->get_scope());
15202 		    ctxt.maybe_add_var_to_exported_decls(m.get());
15203 		    return m;
15204 		  }
15205 	      }
15206 	  }
15207 	else if (var_decl_sptr v =
15208 		 build_or_get_var_decl_if_not_suppressed(ctxt, scope, die,
15209 							 where_offset,
15210 							 /*result=*/var_decl_sptr(),
15211 							 is_required_decl_spec))
15212 	  {
15213 	    result = add_decl_to_scope(v, scope);
15214 	    ABG_ASSERT(is_decl(result)->get_scope());
15215 	    v = dynamic_pointer_cast<var_decl>(result);
15216 	    ABG_ASSERT(v);
15217 	    ABG_ASSERT(v->get_scope());
15218 	    ctxt.var_decls_to_re_add_to_tree().push_back(v);
15219 	    ctxt.maybe_add_var_to_exported_decls(v.get());
15220 	  }
15221       }
15222       break;
15223 
15224     case DW_TAG_subprogram:
15225       {
15226 	Dwarf_Die spec_die;
15227 	Dwarf_Die abstract_origin_die;
15228 	Dwarf_Die *interface_die = 0, *origin_die = 0;
15229 	scope_decl_sptr interface_scope;
15230 	if (die_is_artificial(die))
15231 	  break;
15232 
15233 	function_decl_sptr fn;
15234 	bool has_spec = die_die_attribute(die, DW_AT_specification,
15235 					  spec_die, true);
15236 	bool has_abstract_origin =
15237 	  die_die_attribute(die, DW_AT_abstract_origin,
15238 			    abstract_origin_die, true);
15239 	if (has_spec || has_abstract_origin)
15240 	  {
15241 	    interface_die =
15242 	      has_spec
15243 	      ? &spec_die
15244 	      : &abstract_origin_die;
15245 	    origin_die =
15246 	      has_abstract_origin
15247 	      ? &abstract_origin_die
15248 	      : &spec_die;
15249 
15250 	    string linkage_name = die_linkage_name(die);
15251 	    string spec_linkage_name = die_linkage_name(interface_die);
15252 
15253 	    interface_scope = get_scope_for_die(ctxt, interface_die,
15254 						called_from_public_decl,
15255 						where_offset);
15256 	    if (interface_scope)
15257 	      {
15258 		decl_base_sptr d;
15259 		class_decl_sptr c = is_class_type(interface_scope);
15260 		if (c && !linkage_name.empty())
15261 		  d = c->find_member_function_sptr(linkage_name);
15262 
15263 		if (!d)
15264 		  d = is_decl(build_ir_node_from_die(ctxt,
15265 						     origin_die,
15266 						     interface_scope.get(),
15267 						     called_from_public_decl,
15268 						     where_offset,
15269 						     is_declaration_only,
15270 						     /*is_required_decl_spec=*/true));
15271 		if (d)
15272 		  {
15273 		    fn = dynamic_pointer_cast<function_decl>(d);
15274 		    if (has_abstract_origin
15275 			&& (linkage_name != spec_linkage_name))
15276 		      // The current DIE has 'd' as abstract orign,
15277 		      // and has a linkage name that is different
15278 		      // from from the linkage name of 'd'.  That
15279 		      // means, the current DIE represents a clone
15280 		      // of 'd'.
15281 		      fn = fn->clone();
15282 		  }
15283 	      }
15284 	  }
15285 	ctxt.scope_stack().push(scope);
15286 
15287 	scope_decl* logical_scope =
15288 	  interface_scope
15289 	  ? interface_scope.get()
15290 	  : scope;
15291 
15292 	result = build_or_get_fn_decl_if_not_suppressed(ctxt, logical_scope,
15293 							die, where_offset,
15294 							is_declaration_only,
15295 							fn);
15296 
15297 	if (result && !fn)
15298 	  {
15299 	    if (potential_member_fn_should_be_dropped(is_function_decl(result),
15300 						      die)
15301 		&& !is_required_decl_spec)
15302 	      {
15303 		result.reset();
15304 		break;
15305 	      }
15306 	    result = add_decl_to_scope(is_decl(result), logical_scope);
15307 	  }
15308 
15309 	fn = is_function_decl(result);
15310 	if (fn && is_member_function(fn))
15311 	  {
15312 	    class_decl_sptr klass(static_cast<class_decl*>(logical_scope),
15313 				  sptr_utils::noop_deleter());
15314 	    ABG_ASSERT(klass);
15315 	    finish_member_function_reading(die, fn, klass, ctxt);
15316 	  }
15317 
15318 	if (fn)
15319 	  {
15320 	    ctxt.maybe_add_fn_to_exported_decls(fn.get());
15321 	    ctxt.associate_die_to_decl(die, fn, where_offset,
15322 				       /*associate_by_repr=*/false);
15323 	    maybe_canonicalize_type(die, ctxt);
15324 	  }
15325 
15326 	ctxt.scope_stack().pop();
15327       }
15328       break;
15329 
15330     case DW_TAG_formal_parameter:
15331       // We should not read this case as it should have been dealt
15332       // with by build_function_decl above.
15333       ABG_ASSERT_NOT_REACHED;
15334 
15335     case DW_TAG_constant:
15336       break;
15337     case DW_TAG_enumerator:
15338       break;
15339 
15340     case DW_TAG_partial_unit:
15341     case DW_TAG_imported_unit:
15342       // For now, the DIEs under these are read lazily when they are
15343       // referenced by a public decl DIE that is under a
15344       // DW_TAG_compile_unit, so we shouldn't get here.
15345       ABG_ASSERT_NOT_REACHED;
15346 
15347       // Other declaration we don't really intend to support yet.
15348     case DW_TAG_dwarf_procedure:
15349     case DW_TAG_imported_declaration:
15350     case DW_TAG_entry_point:
15351     case DW_TAG_label:
15352     case DW_TAG_lexical_block:
15353     case DW_TAG_unspecified_parameters:
15354     case DW_TAG_variant:
15355     case DW_TAG_common_block:
15356     case DW_TAG_common_inclusion:
15357     case DW_TAG_inheritance:
15358     case DW_TAG_inlined_subroutine:
15359     case DW_TAG_with_stmt:
15360     case DW_TAG_access_declaration:
15361     case DW_TAG_catch_block:
15362     case DW_TAG_friend:
15363     case DW_TAG_namelist:
15364     case DW_TAG_namelist_item:
15365     case DW_TAG_template_type_parameter:
15366     case DW_TAG_template_value_parameter:
15367     case DW_TAG_try_block:
15368     case DW_TAG_variant_part:
15369     case DW_TAG_imported_module:
15370     case DW_TAG_condition:
15371     case DW_TAG_type_unit:
15372     case DW_TAG_template_alias:
15373     case DW_TAG_lo_user:
15374     case DW_TAG_MIPS_loop:
15375     case DW_TAG_format_label:
15376     case DW_TAG_function_template:
15377     case DW_TAG_class_template:
15378     case DW_TAG_GNU_BINCL:
15379     case DW_TAG_GNU_EINCL:
15380     case DW_TAG_GNU_template_template_param:
15381     case DW_TAG_GNU_template_parameter_pack:
15382     case DW_TAG_GNU_formal_parameter_pack:
15383     case DW_TAG_GNU_call_site:
15384     case DW_TAG_GNU_call_site_parameter:
15385     case DW_TAG_hi_user:
15386     default:
15387       break;
15388     }
15389 
15390   if (result && tag != DW_TAG_subroutine_type)
15391     ctxt.associate_die_to_decl(die, is_decl(result), where_offset,
15392 			       /*associate_by_repr=*/false);
15393 
15394   if (result)
15395     if (ctxt.load_all_types())
15396       if (called_from_public_decl)
15397 	if (type_base_sptr t = is_type(result))
15398 	  if (corpus *abi_corpus = scope->get_corpus())
15399 	    abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15400 
15401   return result;
15402 }
15403 
15404 ///  Build the IR node for a void type.
15405 ///
15406 ///  @param ctxt the read context to use.
15407 ///
15408 ///  @return the void type node.
15409 static decl_base_sptr
build_ir_node_for_void_type(read_context & ctxt)15410 build_ir_node_for_void_type(read_context& ctxt)
15411 {
15412   ir::environment* env = ctxt.env();
15413   ABG_ASSERT(env);
15414   type_base_sptr t = env->get_void_type();
15415   decl_base_sptr type_declaration = get_type_declaration(t);
15416   if (!has_scope(type_declaration))
15417     add_decl_to_scope(type_declaration,
15418 		      ctxt.cur_transl_unit()->get_global_scope());
15419   canonicalize(t);
15420   return type_declaration;
15421 }
15422 
15423 /// Build the IR node for a variadic parameter type.
15424 ///
15425 /// @param ctxt the read context to use.
15426 ///
15427 /// @return the variadic parameter type.
15428 static decl_base_sptr
build_ir_node_for_variadic_parameter_type(read_context & ctxt)15429 build_ir_node_for_variadic_parameter_type(read_context &ctxt)
15430 {
15431 
15432   ir::environment* env = ctxt.env();
15433   ABG_ASSERT(env);
15434   type_base_sptr t = env->get_variadic_parameter_type();
15435   decl_base_sptr type_declaration = get_type_declaration(t);
15436   if (!has_scope(type_declaration))
15437     add_decl_to_scope(type_declaration,
15438 		      ctxt.cur_transl_unit()->get_global_scope());
15439   canonicalize(t);
15440   return type_declaration;
15441 }
15442 
15443 /// Build an IR node from a given DIE and add the node to the current
15444 /// IR being build and held in the read_context.  Doing that is called
15445 /// "emitting an IR node for the DIE".
15446 ///
15447 /// @param ctxt the read context.
15448 ///
15449 /// @param die the DIE to consider.
15450 ///
15451 /// @param called_from_public_decl set to yes if this function is
15452 /// called from the functions used to build a public decl (functions
15453 /// and variables).  In that case, this function accepts building IR
15454 /// nodes representing types.  Otherwise, this function only creates
15455 /// IR nodes representing public decls (functions and variables).
15456 /// This is done to avoid emitting IR nodes for types that are not
15457 /// referenced by public functions or variables.
15458 ///
15459 /// @param where_offset the offset of the DIE where we are "logically"
15460 /// positionned at, in the DIE tree.  This is useful when @p die is
15461 /// e.g, DW_TAG_partial_unit that can be included in several places in
15462 /// the DIE tree.
15463 ///
15464 /// @return the resulting IR node.
15465 static type_or_decl_base_sptr
build_ir_node_from_die(read_context & ctxt,Dwarf_Die * die,bool called_from_public_decl,size_t where_offset)15466 build_ir_node_from_die(read_context&	ctxt,
15467 		       Dwarf_Die*	die,
15468 		       bool		called_from_public_decl,
15469 		       size_t		where_offset)
15470 {
15471   if (!die)
15472     return decl_base_sptr();
15473 
15474   if (is_c_language(ctxt.cur_transl_unit()->get_language()))
15475     {
15476       const scope_decl_sptr& scop = ctxt.global_scope();
15477       return build_ir_node_from_die(ctxt, die, scop.get(),
15478 				    called_from_public_decl,
15479 				    where_offset,
15480                                     true);
15481     }
15482 
15483   // Normaly, a decl that is meant to be external has a DW_AT_external
15484   // set.  But then some compilers fail to always emit that flag.  For
15485   // instance, for static data members, some compilers won't emit the
15486   // DW_AT_external.  In that case, we assume that if the variable is
15487   // at global or named namespace scope, then we can assume it's
15488   // external.  If the variable doesn't have any ELF symbol associated
15489   // to it, it'll be dropped on the floor anyway.  Those variable
15490   // decls are considered as being "effectively public".
15491   bool consider_as_called_from_public_decl =
15492     called_from_public_decl || die_is_effectively_public_decl(ctxt, die);
15493   scope_decl_sptr scope = get_scope_for_die(ctxt, die,
15494 					    consider_as_called_from_public_decl,
15495 					    where_offset);
15496   return build_ir_node_from_die(ctxt, die, scope.get(),
15497 				called_from_public_decl,
15498 				where_offset,
15499                                 true);
15500 }
15501 
15502 /// Create a dwarf_reader::read_context.
15503 ///
15504 /// @param elf_path the path to the elf file the context is to be used for.
15505 ///
15506 /// @param debug_info_root_paths a pointer to the path to the root
15507 /// directory under which the debug info is to be found for @p
15508 /// elf_path.  Leave this to NULL if the debug info is not in a split
15509 /// file.
15510 ///
15511 /// @param environment the environment used by the current context.
15512 /// This environment contains resources needed by the reader and by
15513 /// the types and declarations that are to be created later.  Note
15514 /// that ABI artifacts that are to be compared all need to be created
15515 /// within the same environment.
15516 ///
15517 /// Please also note that the life time of this environment object
15518 /// must be greater than the life time of the resulting @ref
15519 /// read_context the context uses resources that are allocated in the
15520 /// environment.
15521 ///
15522 /// @param load_all_types if set to false only the types that are
15523 /// reachable from publicly exported declarations (of functions and
15524 /// variables) are read.  If set to true then all types found in the
15525 /// debug information are loaded.
15526 ///
15527 /// @param linux_kernel_mode if set to true, then consider the special
15528 /// linux kernel symbol tables when determining if a symbol is
15529 /// exported or not.
15530 ///
15531 /// @return a smart pointer to the resulting dwarf_reader::read_context.
15532 read_context_sptr
create_read_context(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,ir::environment * environment,bool load_all_types,bool linux_kernel_mode)15533 create_read_context(const std::string&		elf_path,
15534 		    const vector<char**>&	debug_info_root_paths,
15535 		    ir::environment*		environment,
15536 		    bool			load_all_types,
15537 		    bool			linux_kernel_mode)
15538 {
15539   // Create a DWARF Front End Library handle to be used by functions
15540   // of that library.
15541   read_context_sptr result(new read_context(elf_path, debug_info_root_paths,
15542 					    environment, load_all_types,
15543 					    linux_kernel_mode));
15544   return result;
15545 }
15546 
15547 /// Getter for the path to the binary this @ref read_context is for.
15548 ///
15549 /// @return the path to the binary the @ref read_context is for.
15550 const string&
read_context_get_path(const read_context & ctxt)15551 read_context_get_path(const read_context& ctxt)
15552 {return ctxt.elf_path();}
15553 
15554 /// Re-initialize a read_context so that it can re-used to read
15555 /// another binary.
15556 ///
15557 /// @param ctxt the context to re-initialize.
15558 ///
15559 /// @param elf_path the path to the elf file the context is to be used
15560 /// for.
15561 ///
15562 /// @param debug_info_root_path a pointer to the path to the root
15563 /// directory under which the debug info is to be found for @p
15564 /// elf_path.  Leave this to NULL if the debug info is not in a split
15565 /// file.
15566 ///
15567 /// @param environment the environment used by the current context.
15568 /// This environment contains resources needed by the reader and by
15569 /// the types and declarations that are to be created later.  Note
15570 /// that ABI artifacts that are to be compared all need to be created
15571 /// within the same environment.
15572 ///
15573 /// Please also note that the life time of this environment object
15574 /// must be greater than the life time of the resulting @ref
15575 /// read_context the context uses resources that are allocated in the
15576 /// environment.
15577 ///
15578 /// @param load_all_types if set to false only the types that are
15579 /// reachable from publicly exported declarations (of functions and
15580 /// variables) are read.  If set to true then all types found in the
15581 /// debug information are loaded.
15582 ///
15583 /// @param linux_kernel_mode if set to true, then consider the special
15584 /// linux kernel symbol tables when determining if a symbol is
15585 /// exported or not.
15586 ///
15587 /// @return a smart pointer to the resulting dwarf_reader::read_context.
15588 void
reset_read_context(read_context_sptr & ctxt,const std::string & elf_path,const vector<char ** > & debug_info_root_path,ir::environment * environment,bool read_all_types,bool linux_kernel_mode)15589 reset_read_context(read_context_sptr	&ctxt,
15590 		   const std::string&	 elf_path,
15591 		   const vector<char**>& debug_info_root_path,
15592 		   ir::environment*	 environment,
15593 		   bool		 read_all_types,
15594 		   bool		 linux_kernel_mode)
15595 {
15596   if (ctxt)
15597     ctxt->initialize(elf_path, debug_info_root_path, environment,
15598 		     read_all_types, linux_kernel_mode);
15599 }
15600 
15601 /// Add suppressions specifications to the set of suppressions to be
15602 /// used during the construction of the ABI internal representation
15603 /// (the ABI corpus) from ELF and DWARF.
15604 ///
15605 /// During the construction of the ABI corpus, ABI artifacts that
15606 /// match the a given suppression specification are dropped on the
15607 /// floor; that is, they are discarded and won't be part of the final
15608 /// ABI corpus.  This is a way to reduce the amount of data held by
15609 /// the final ABI corpus.
15610 ///
15611 /// Note that the suppression specifications provided to this function
15612 /// are only considered during the construction of the ABI corpus.
15613 /// For instance, they are not taken into account during e.g
15614 /// comparisons of two ABI corpora that might happen later.  If you
15615 /// want to apply suppression specificatins to the comparison (or
15616 /// reporting) of ABI corpora please refer to the documentation of the
15617 /// @ref diff_context type to learn how to set suppressions that are
15618 /// to be used in that context.
15619 ///
15620 /// @param ctxt the context that is going to be used by functions that
15621 /// read ELF and DWARF information to construct and ABI corpus.
15622 ///
15623 /// @param supprs the suppression specifications to be applied during
15624 /// the construction of the ABI corpus.
15625 void
add_read_context_suppressions(read_context & ctxt,const suppr::suppressions_type & supprs)15626 add_read_context_suppressions(read_context& ctxt,
15627 			      const suppr::suppressions_type& supprs)
15628 {
15629   for (suppr::suppressions_type::const_iterator i = supprs.begin();
15630        i != supprs.end();
15631        ++i)
15632     if ((*i)->get_drops_artifact_from_ir())
15633       ctxt.get_suppressions().push_back(*i);
15634 }
15635 
15636 /// Set the @ref corpus_group being created to the current read context.
15637 ///
15638 /// @param ctxt the read_context to consider.
15639 ///
15640 /// @param group the @ref corpus_group to set.
15641 void
set_read_context_corpus_group(read_context & ctxt,corpus_group_sptr & group)15642 set_read_context_corpus_group(read_context& ctxt,
15643 			      corpus_group_sptr& group)
15644 {
15645   ctxt.cur_corpus_group_ = group;
15646 }
15647 
15648 /// Read all @ref abigail::translation_unit possible from the debug info
15649 /// accessible from an elf file, stuff them into a libabigail ABI
15650 /// Corpus and return it.
15651 ///
15652 /// @param ctxt the context to use for reading the elf file.
15653 ///
15654 /// @param resulting_corp a pointer to the resulting abigail::corpus.
15655 ///
15656 /// @return the resulting status.
15657 corpus_sptr
read_corpus_from_elf(read_context & ctxt,status & status)15658 read_corpus_from_elf(read_context& ctxt, status& status)
15659 {
15660   status = STATUS_UNKNOWN;
15661 
15662   // Load debug info from the elf path.
15663   if (!ctxt.load_debug_info())
15664     status |= STATUS_DEBUG_INFO_NOT_FOUND;
15665 
15666   {
15667     string alt_di_path;
15668     if (refers_to_alt_debug_info(ctxt, alt_di_path) && !ctxt.alt_dwarf())
15669       status |= STATUS_ALT_DEBUG_INFO_NOT_FOUND;
15670   }
15671 
15672   ctxt.load_elf_properties();  // DT_SONAME, DT_NEEDED, architecture
15673 
15674   if (!ctxt.symtab() || !ctxt.symtab()->has_symbols())
15675     status |= STATUS_NO_SYMBOLS_FOUND;
15676 
15677   if (// If no elf symbol was found ...
15678       status & STATUS_NO_SYMBOLS_FOUND
15679       // ... or if debug info was found but not the required alternate
15680       // debug info ...
15681       || ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
15682 	  && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
15683     // ... then we cannot handle the binary.
15684     return corpus_sptr();
15685 
15686   // Read the variable and function descriptions from the debug info
15687   // we have, through the dwfl handle.
15688   corpus_sptr corp = read_debug_info_into_corpus(ctxt);
15689 
15690   status |= STATUS_OK;
15691 
15692   return corp;
15693 }
15694 
15695 /// Read a corpus and add it to a given @ref corpus_group.
15696 ///
15697 /// @param ctxt the reading context to consider.
15698 ///
15699 /// @param group the @ref corpus_group to add the new corpus to.
15700 ///
15701 /// @param status output parameter. The status of the read.  It is set
15702 /// by this function upon its completion.
15703 corpus_sptr
read_and_add_corpus_to_group_from_elf(read_context & ctxt,corpus_group & group,status & status)15704 read_and_add_corpus_to_group_from_elf(read_context& ctxt,
15705 				      corpus_group& group,
15706 				      status& status)
15707 {
15708   corpus_sptr result;
15709   corpus_sptr corp = read_corpus_from_elf(ctxt, status);
15710   if (status & STATUS_OK)
15711     {
15712       if (!corp->get_group())
15713 	group.add_corpus(corp);
15714       result = corp;
15715     }
15716 
15717   return result;
15718 }
15719 
15720 /// Read all @ref abigail::translation_unit possible from the debug info
15721 /// accessible from an elf file, stuff them into a libabigail ABI
15722 /// Corpus and return it.
15723 ///
15724 /// @param elf_path the path to the elf file.
15725 ///
15726 /// @param debug_info_root_paths a vector of pointers to root paths
15727 /// under which to look for the debug info of the elf files that are
15728 /// later handled by the Dwfl.  This for cases where the debug info is
15729 /// split into a different file from the binary we want to inspect.
15730 /// On Red Hat compatible systems, this root path is usually
15731 /// /usr/lib/debug by default.  If this argument is set to NULL, then
15732 /// "./debug" and /usr/lib/debug will be searched for sub-directories
15733 /// containing the debug info file.
15734 ///
15735 /// @param environment the environment used by the current context.
15736 /// This environment contains resources needed by the reader and by
15737 /// the types and declarations that are to be created later.  Note
15738 /// that ABI artifacts that are to be compared all need to be created
15739 /// within the same environment.  Also, the lifetime of the
15740 /// environment must be greater than the lifetime of the resulting
15741 /// corpus because the corpus uses resources that are allocated in the
15742 /// environment.
15743 ///
15744 /// @param load_all_types if set to false only the types that are
15745 /// reachable from publicly exported declarations (of functions and
15746 /// variables) are read.  If set to true then all types found in the
15747 /// debug information are loaded.
15748 ///
15749 /// @param resulting_corp a pointer to the resulting abigail::corpus.
15750 ///
15751 /// @return the resulting status.
15752 corpus_sptr
read_corpus_from_elf(const std::string & elf_path,const vector<char ** > & debug_info_root_paths,ir::environment * environment,bool load_all_types,status & status)15753 read_corpus_from_elf(const std::string& elf_path,
15754 		     const vector<char**>& debug_info_root_paths,
15755 		     ir::environment*	environment,
15756 		     bool		load_all_types,
15757 		     status&		status)
15758 {
15759   read_context_sptr c = create_read_context(elf_path,
15760 					    debug_info_root_paths,
15761 					    environment,
15762 					    load_all_types);
15763   read_context& ctxt = *c;
15764   return read_corpus_from_elf(ctxt, status);
15765 }
15766 
15767 /// Look into the symbol tables of a given elf file and see if we find
15768 /// a given symbol.
15769 ///
15770 /// @param env the environment we are operating from.
15771 ///
15772 /// @param elf_path the path to the elf file to consider.
15773 ///
15774 /// @param symbol_name the name of the symbol to look for.
15775 ///
15776 /// @param demangle if true, try to demangle the symbol name found in
15777 /// the symbol table.
15778 ///
15779 /// @param syms the vector of symbols found with the name @p symbol_name.
15780 ///
15781 /// @return true iff the symbol was found among the publicly exported
15782 /// symbols of the ELF file.
15783 bool
lookup_symbol_from_elf(const environment * env,const string & elf_path,const string & symbol_name,bool demangle,vector<elf_symbol_sptr> & syms)15784 lookup_symbol_from_elf(const environment*		env,
15785 		       const string&			elf_path,
15786 		       const string&			symbol_name,
15787 		       bool				demangle,
15788 		       vector<elf_symbol_sptr>&	syms)
15789 
15790 {
15791   if (elf_version(EV_CURRENT) == EV_NONE)
15792     return false;
15793 
15794   int fd = open(elf_path.c_str(), O_RDONLY);
15795   if (fd < 0)
15796     return false;
15797 
15798   struct stat s;
15799   if (fstat(fd, &s))
15800     return false;
15801 
15802   Elf* elf = elf_begin(fd, ELF_C_READ, 0);
15803   if (elf == 0)
15804     return false;
15805 
15806   bool value = lookup_symbol_from_elf(env, elf, symbol_name,
15807 				      demangle, syms);
15808   elf_end(elf);
15809   close(fd);
15810 
15811   return value;
15812 }
15813 
15814 /// Look into the symbol tables of an elf file to see if a public
15815 /// function of a given name is found.
15816 ///
15817 /// @param env the environment we are operating from.
15818 ///
15819 /// @param elf_path the path to the elf file to consider.
15820 ///
15821 /// @param symbol_name the name of the function to look for.
15822 ///
15823 /// @param syms the vector of public function symbols found with the
15824 /// name @p symname.
15825 ///
15826 /// @return true iff a function with symbol name @p symbol_name is
15827 /// found.
15828 bool
lookup_public_function_symbol_from_elf(const environment * env,const string & path,const string & symname,vector<elf_symbol_sptr> & syms)15829 lookup_public_function_symbol_from_elf(const environment*		env,
15830 				       const string&			path,
15831 				       const string&			symname,
15832 				       vector<elf_symbol_sptr>&	syms)
15833 {
15834   if (elf_version(EV_CURRENT) == EV_NONE)
15835     return false;
15836 
15837   int fd = open(path.c_str(), O_RDONLY);
15838   if (fd < 0)
15839     return false;
15840 
15841   struct stat s;
15842   if (fstat(fd, &s))
15843     return false;
15844 
15845   Elf* elf = elf_begin(fd, ELF_C_READ, 0);
15846   if (elf == 0)
15847     return false;
15848 
15849   bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
15850   elf_end(elf);
15851   close(fd);
15852 
15853   return value;
15854 }
15855 
15856 /// Check if the underlying elf file refers to an alternate debug info
15857 /// file associated to it.
15858 ///
15859 /// Note that "alternate debug info sections" is a GNU extension as
15860 /// of DWARF4 and is described at
15861 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
15862 ///
15863 /// @param ctxt the context used to read the elf file.
15864 ///
15865 /// @param alt_di the path to the alternate debug info file.  This is
15866 /// set iff the function returns true.
15867 ///
15868 /// @return true if the ELF file refers to an alternate debug info
15869 /// file.
15870 bool
refers_to_alt_debug_info(const read_context & ctxt,string & alt_di_path)15871 refers_to_alt_debug_info(const read_context&	ctxt,
15872 			 string&		alt_di_path)
15873 {
15874   if (!ctxt.alt_debug_info_path().empty())
15875     {
15876       alt_di_path = ctxt.alt_debug_info_path();
15877       return true;
15878     }
15879   return false;
15880 }
15881 
15882 /// Check if the underlying elf file has an alternate debug info file
15883 /// associated to it.
15884 ///
15885 /// Note that "alternate debug info sections" is a GNU extension as
15886 /// of DWARF4 and is described at
15887 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
15888 ///
15889 /// @param ctxt the read_context to use to handle the underlying elf file.
15890 ///
15891 /// @param has_alt_di out parameter.  This is set to true upon
15892 /// succesful completion of the function iff an alternate debug info
15893 /// file was found, false otherwise.  Note thas this parameter is set
15894 /// only if the function returns STATUS_OK.
15895 ///
15896 /// @param alt_debug_info_path if the function returned STATUS_OK and
15897 /// if @p has been set to true, then this parameter contains the path
15898 /// to the alternate debug info file found.
15899 ///
15900 /// return STATUS_OK upon successful completion, false otherwise.
15901 status
has_alt_debug_info(read_context & ctxt,bool & has_alt_di,string & alt_debug_info_path)15902 has_alt_debug_info(read_context&	ctxt,
15903 		   bool&		has_alt_di,
15904 		   string&		alt_debug_info_path)
15905 {
15906   // Load debug info from the elf path.
15907   if (!ctxt.load_debug_info())
15908     return STATUS_DEBUG_INFO_NOT_FOUND;
15909 
15910   if (ctxt.alt_dwarf())
15911     {
15912       has_alt_di = true;
15913       alt_debug_info_path = ctxt.alt_debug_info_path();
15914     }
15915   else
15916     has_alt_di = false;
15917 
15918   return STATUS_OK;
15919 }
15920 
15921 /// Check if a given elf file has an alternate debug info file
15922 /// associated to it.
15923 ///
15924 /// Note that "alternate debug info sections" is a GNU extension as
15925 /// of DWARF4 and is described at
15926 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1.
15927 ///
15928 /// @param elf_path the path to the elf file to consider.
15929 ///
15930 /// @param a pointer to the root directory under which the split debug info
15931 /// file associated to elf_path is to be found.  This has to be NULL
15932 /// if the debug info file is not in a split file.
15933 ///
15934 /// @param has_alt_di out parameter.  This is set to true upon
15935 /// succesful completion of the function iff an alternate debug info
15936 /// file was found, false otherwise.  Note thas this parameter is set
15937 /// only if the function returns STATUS_OK.
15938 ///
15939 /// @param alt_debug_info_path if the function returned STATUS_OK and
15940 /// if @p has been set to true, then this parameter contains the path
15941 /// to the alternate debug info file found.
15942 ///
15943 /// return STATUS_OK upon successful completion, false otherwise.
15944 status
has_alt_debug_info(const string & elf_path,char ** debug_info_root_path,bool & has_alt_di,string & alt_debug_info_path)15945 has_alt_debug_info(const string&	elf_path,
15946 		   char**		debug_info_root_path,
15947 		   bool&		has_alt_di,
15948 		   string&		alt_debug_info_path)
15949 {
15950   vector<char**> di_roots;
15951   di_roots.push_back(debug_info_root_path);
15952   read_context_sptr c = create_read_context(elf_path, di_roots, 0);
15953   read_context& ctxt = *c;
15954 
15955   // Load debug info from the elf path.
15956   if (!ctxt.load_debug_info())
15957     return STATUS_DEBUG_INFO_NOT_FOUND;
15958 
15959   if (ctxt.alt_dwarf())
15960     {
15961       has_alt_di = true;
15962       alt_debug_info_path = ctxt.alt_debug_info_path();
15963     }
15964   else
15965     has_alt_di = false;
15966 
15967   return STATUS_OK;
15968 }
15969 
15970 /// Fetch the SONAME ELF property from an ELF binary file.
15971 ///
15972 /// @param path The path to the elf file to consider.
15973 ///
15974 /// @param soname out parameter. Set to the SONAME property of the
15975 /// binary file, if it present in the ELF file.
15976 ///
15977 /// return false if an error occured while looking for the SONAME
15978 /// property in the binary, true otherwise.
15979 bool
get_soname_of_elf_file(const string & path,string & soname)15980 get_soname_of_elf_file(const string& path, string &soname)
15981 {
15982 
15983   int fd = open(path.c_str(), O_RDONLY);
15984   if (fd == -1)
15985     return false;
15986 
15987   elf_version (EV_CURRENT);
15988   Elf* elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
15989 
15990   GElf_Ehdr ehdr_mem;
15991   GElf_Ehdr* ehdr = gelf_getehdr (elf, &ehdr_mem);
15992   if (ehdr == NULL)
15993     return false;
15994 
15995   for (int i = 0; i < ehdr->e_phnum; ++i)
15996     {
15997       GElf_Phdr phdr_mem;
15998       GElf_Phdr* phdr = gelf_getphdr (elf, i, &phdr_mem);
15999 
16000       if (phdr != NULL && phdr->p_type == PT_DYNAMIC)
16001         {
16002           Elf_Scn* scn = gelf_offscn (elf, phdr->p_offset);
16003           GElf_Shdr shdr_mem;
16004           GElf_Shdr* shdr = gelf_getshdr (scn, &shdr_mem);
16005           int maxcnt = (shdr != NULL
16006                         ? shdr->sh_size / shdr->sh_entsize : INT_MAX);
16007           ABG_ASSERT (shdr == NULL || shdr->sh_type == SHT_DYNAMIC);
16008           Elf_Data* data = elf_getdata (scn, NULL);
16009           if (data == NULL)
16010             break;
16011 
16012           for (int cnt = 0; cnt < maxcnt; ++cnt)
16013             {
16014               GElf_Dyn dynmem;
16015               GElf_Dyn* dyn = gelf_getdyn (data, cnt, &dynmem);
16016               if (dyn == NULL)
16017                 continue;
16018 
16019               if (dyn->d_tag == DT_NULL)
16020                 break;
16021 
16022               if (dyn->d_tag != DT_SONAME)
16023                 continue;
16024 
16025               soname = elf_strptr (elf, shdr->sh_link, dyn->d_un.d_val);
16026               break;
16027             }
16028           break;
16029         }
16030     }
16031 
16032   elf_end(elf);
16033   close(fd);
16034 
16035   return true;
16036 }
16037 
16038 /// Get the type of a given elf type.
16039 ///
16040 /// @param path the absolute path to the ELF file to analyzed.
16041 ///
16042 /// @param type the kind of the ELF file designated by @p path.
16043 ///
16044 /// @param out parameter.  Is set to the type of ELF file of @p path.
16045 /// This parameter is set iff the function returns true.
16046 ///
16047 /// @return true iff the file could be opened and analyzed.
16048 bool
get_type_of_elf_file(const string & path,elf_type & type)16049 get_type_of_elf_file(const string& path, elf_type& type)
16050 {
16051   int fd = open(path.c_str(), O_RDONLY);
16052   if (fd == -1)
16053     return false;
16054 
16055   elf_version (EV_CURRENT);
16056   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP, NULL);
16057   type = elf_file_type(elf);
16058   elf_end(elf);
16059   close(fd);
16060 
16061   return true;
16062 }
16063 
16064 }// end namespace dwarf_reader
16065 
16066 }// end namespace abigail
16067