xref: /dragonfly/contrib/gdb-7/gdb/dwarf2read.c (revision d4ef6694)
1 /* DWARF 2 debugging format support for GDB.
2 
3    Copyright (C) 1994-2013 Free Software Foundation, Inc.
4 
5    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6    Inc.  with support from Florida State University (under contract
7    with the Ada Joint Program Office), and Silicon Graphics, Inc.
8    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10    support.
11 
12    This file is part of GDB.
13 
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 3 of the License, or
17    (at your option) any later version.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
26 
27 /* FIXME: Various die-reading functions need to be more careful with
28    reading off the end of the section.
29    E.g., load_partial_dies, read_partial_die.  */
30 
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h"	/* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 
72 #include <fcntl.h>
73 #include "gdb_string.h"
74 #include "gdb_assert.h"
75 #include <sys/types.h>
76 
77 typedef struct symbol *symbolp;
78 DEF_VEC_P (symbolp);
79 
80 /* When non-zero, print basic high level tracing messages.
81    This is in contrast to the low level DIE reading of dwarf2_die_debug.  */
82 static int dwarf2_read_debug = 0;
83 
84 /* When non-zero, dump DIEs after they are read in.  */
85 static unsigned int dwarf2_die_debug = 0;
86 
87 /* When non-zero, cross-check physname against demangler.  */
88 static int check_physname = 0;
89 
90 /* When non-zero, do not reject deprecated .gdb_index sections.  */
91 static int use_deprecated_index_sections = 0;
92 
93 static const struct objfile_data *dwarf2_objfile_data_key;
94 
95 struct dwarf2_section_info
96 {
97   asection *asection;
98   gdb_byte *buffer;
99   bfd_size_type size;
100   /* True if we have tried to read this section.  */
101   int readin;
102 };
103 
104 typedef struct dwarf2_section_info dwarf2_section_info_def;
105 DEF_VEC_O (dwarf2_section_info_def);
106 
107 /* All offsets in the index are of this type.  It must be
108    architecture-independent.  */
109 typedef uint32_t offset_type;
110 
111 DEF_VEC_I (offset_type);
112 
113 /* Ensure only legit values are used.  */
114 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
115   do { \
116     gdb_assert ((unsigned int) (value) <= 1); \
117     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
118   } while (0)
119 
120 /* Ensure only legit values are used.  */
121 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
122   do { \
123     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
124                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
125     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
126   } while (0)
127 
128 /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
129 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
130   do { \
131     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
132     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
133   } while (0)
134 
135 /* A description of the mapped index.  The file format is described in
136    a comment by the code that writes the index.  */
137 struct mapped_index
138 {
139   /* Index data format version.  */
140   int version;
141 
142   /* The total length of the buffer.  */
143   off_t total_size;
144 
145   /* A pointer to the address table data.  */
146   const gdb_byte *address_table;
147 
148   /* Size of the address table data in bytes.  */
149   offset_type address_table_size;
150 
151   /* The symbol table, implemented as a hash table.  */
152   const offset_type *symbol_table;
153 
154   /* Size in slots, each slot is 2 offset_types.  */
155   offset_type symbol_table_slots;
156 
157   /* A pointer to the constant pool.  */
158   const char *constant_pool;
159 };
160 
161 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
162 DEF_VEC_P (dwarf2_per_cu_ptr);
163 
164 /* Collection of data recorded per objfile.
165    This hangs off of dwarf2_objfile_data_key.  */
166 
167 struct dwarf2_per_objfile
168 {
169   struct dwarf2_section_info info;
170   struct dwarf2_section_info abbrev;
171   struct dwarf2_section_info line;
172   struct dwarf2_section_info loc;
173   struct dwarf2_section_info macinfo;
174   struct dwarf2_section_info macro;
175   struct dwarf2_section_info str;
176   struct dwarf2_section_info ranges;
177   struct dwarf2_section_info addr;
178   struct dwarf2_section_info frame;
179   struct dwarf2_section_info eh_frame;
180   struct dwarf2_section_info gdb_index;
181 
182   VEC (dwarf2_section_info_def) *types;
183 
184   /* Back link.  */
185   struct objfile *objfile;
186 
187   /* Table of all the compilation units.  This is used to locate
188      the target compilation unit of a particular reference.  */
189   struct dwarf2_per_cu_data **all_comp_units;
190 
191   /* The number of compilation units in ALL_COMP_UNITS.  */
192   int n_comp_units;
193 
194   /* The number of .debug_types-related CUs.  */
195   int n_type_units;
196 
197   /* The .debug_types-related CUs (TUs).  */
198   struct signatured_type **all_type_units;
199 
200   /* The number of entries in all_type_unit_groups.  */
201   int n_type_unit_groups;
202 
203   /* Table of type unit groups.
204      This exists to make it easy to iterate over all CUs and TU groups.  */
205   struct type_unit_group **all_type_unit_groups;
206 
207   /* Table of struct type_unit_group objects.
208      The hash key is the DW_AT_stmt_list value.  */
209   htab_t type_unit_groups;
210 
211   /* A table mapping .debug_types signatures to its signatured_type entry.
212      This is NULL if the .debug_types section hasn't been read in yet.  */
213   htab_t signatured_types;
214 
215   /* Type unit statistics, to see how well the scaling improvements
216      are doing.  */
217   struct tu_stats
218   {
219     int nr_uniq_abbrev_tables;
220     int nr_symtabs;
221     int nr_symtab_sharers;
222     int nr_stmt_less_type_units;
223   } tu_stats;
224 
225   /* A chain of compilation units that are currently read in, so that
226      they can be freed later.  */
227   struct dwarf2_per_cu_data *read_in_chain;
228 
229   /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
230      This is NULL if the table hasn't been allocated yet.  */
231   htab_t dwo_files;
232 
233   /* Non-zero if we've check for whether there is a DWP file.  */
234   int dwp_checked;
235 
236   /* The DWP file if there is one, or NULL.  */
237   struct dwp_file *dwp_file;
238 
239   /* The shared '.dwz' file, if one exists.  This is used when the
240      original data was compressed using 'dwz -m'.  */
241   struct dwz_file *dwz_file;
242 
243   /* A flag indicating wether this objfile has a section loaded at a
244      VMA of 0.  */
245   int has_section_at_zero;
246 
247   /* True if we are using the mapped index,
248      or we are faking it for OBJF_READNOW's sake.  */
249   unsigned char using_index;
250 
251   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
252   struct mapped_index *index_table;
253 
254   /* When using index_table, this keeps track of all quick_file_names entries.
255      TUs typically share line table entries with a CU, so we maintain a
256      separate table of all line table entries to support the sharing.
257      Note that while there can be way more TUs than CUs, we've already
258      sorted all the TUs into "type unit groups", grouped by their
259      DW_AT_stmt_list value.  Therefore the only sharing done here is with a
260      CU and its associated TU group if there is one.  */
261   htab_t quick_file_names_table;
262 
263   /* Set during partial symbol reading, to prevent queueing of full
264      symbols.  */
265   int reading_partial_symbols;
266 
267   /* Table mapping type DIEs to their struct type *.
268      This is NULL if not allocated yet.
269      The mapping is done via (CU/TU signature + DIE offset) -> type.  */
270   htab_t die_type_hash;
271 
272   /* The CUs we recently read.  */
273   VEC (dwarf2_per_cu_ptr) *just_read_cus;
274 };
275 
276 static struct dwarf2_per_objfile *dwarf2_per_objfile;
277 
278 /* Default names of the debugging sections.  */
279 
280 /* Note that if the debugging section has been compressed, it might
281    have a name like .zdebug_info.  */
282 
283 static const struct dwarf2_debug_sections dwarf2_elf_names =
284 {
285   { ".debug_info", ".zdebug_info" },
286   { ".debug_abbrev", ".zdebug_abbrev" },
287   { ".debug_line", ".zdebug_line" },
288   { ".debug_loc", ".zdebug_loc" },
289   { ".debug_macinfo", ".zdebug_macinfo" },
290   { ".debug_macro", ".zdebug_macro" },
291   { ".debug_str", ".zdebug_str" },
292   { ".debug_ranges", ".zdebug_ranges" },
293   { ".debug_types", ".zdebug_types" },
294   { ".debug_addr", ".zdebug_addr" },
295   { ".debug_frame", ".zdebug_frame" },
296   { ".eh_frame", NULL },
297   { ".gdb_index", ".zgdb_index" },
298   23
299 };
300 
301 /* List of DWO/DWP sections.  */
302 
303 static const struct dwop_section_names
304 {
305   struct dwarf2_section_names abbrev_dwo;
306   struct dwarf2_section_names info_dwo;
307   struct dwarf2_section_names line_dwo;
308   struct dwarf2_section_names loc_dwo;
309   struct dwarf2_section_names macinfo_dwo;
310   struct dwarf2_section_names macro_dwo;
311   struct dwarf2_section_names str_dwo;
312   struct dwarf2_section_names str_offsets_dwo;
313   struct dwarf2_section_names types_dwo;
314   struct dwarf2_section_names cu_index;
315   struct dwarf2_section_names tu_index;
316 }
317 dwop_section_names =
318 {
319   { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
320   { ".debug_info.dwo", ".zdebug_info.dwo" },
321   { ".debug_line.dwo", ".zdebug_line.dwo" },
322   { ".debug_loc.dwo", ".zdebug_loc.dwo" },
323   { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
324   { ".debug_macro.dwo", ".zdebug_macro.dwo" },
325   { ".debug_str.dwo", ".zdebug_str.dwo" },
326   { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
327   { ".debug_types.dwo", ".zdebug_types.dwo" },
328   { ".debug_cu_index", ".zdebug_cu_index" },
329   { ".debug_tu_index", ".zdebug_tu_index" },
330 };
331 
332 /* local data types */
333 
334 /* The data in a compilation unit header, after target2host
335    translation, looks like this.  */
336 struct comp_unit_head
337 {
338   unsigned int length;
339   short version;
340   unsigned char addr_size;
341   unsigned char signed_addr_p;
342   sect_offset abbrev_offset;
343 
344   /* Size of file offsets; either 4 or 8.  */
345   unsigned int offset_size;
346 
347   /* Size of the length field; either 4 or 12.  */
348   unsigned int initial_length_size;
349 
350   /* Offset to the first byte of this compilation unit header in the
351      .debug_info section, for resolving relative reference dies.  */
352   sect_offset offset;
353 
354   /* Offset to first die in this cu from the start of the cu.
355      This will be the first byte following the compilation unit header.  */
356   cu_offset first_die_offset;
357 };
358 
359 /* Type used for delaying computation of method physnames.
360    See comments for compute_delayed_physnames.  */
361 struct delayed_method_info
362 {
363   /* The type to which the method is attached, i.e., its parent class.  */
364   struct type *type;
365 
366   /* The index of the method in the type's function fieldlists.  */
367   int fnfield_index;
368 
369   /* The index of the method in the fieldlist.  */
370   int index;
371 
372   /* The name of the DIE.  */
373   const char *name;
374 
375   /*  The DIE associated with this method.  */
376   struct die_info *die;
377 };
378 
379 typedef struct delayed_method_info delayed_method_info;
380 DEF_VEC_O (delayed_method_info);
381 
382 /* Internal state when decoding a particular compilation unit.  */
383 struct dwarf2_cu
384 {
385   /* The objfile containing this compilation unit.  */
386   struct objfile *objfile;
387 
388   /* The header of the compilation unit.  */
389   struct comp_unit_head header;
390 
391   /* Base address of this compilation unit.  */
392   CORE_ADDR base_address;
393 
394   /* Non-zero if base_address has been set.  */
395   int base_known;
396 
397   /* The language we are debugging.  */
398   enum language language;
399   const struct language_defn *language_defn;
400 
401   const char *producer;
402 
403   /* The generic symbol table building routines have separate lists for
404      file scope symbols and all all other scopes (local scopes).  So
405      we need to select the right one to pass to add_symbol_to_list().
406      We do it by keeping a pointer to the correct list in list_in_scope.
407 
408      FIXME: The original dwarf code just treated the file scope as the
409      first local scope, and all other local scopes as nested local
410      scopes, and worked fine.  Check to see if we really need to
411      distinguish these in buildsym.c.  */
412   struct pending **list_in_scope;
413 
414   /* The abbrev table for this CU.
415      Normally this points to the abbrev table in the objfile.
416      But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
417   struct abbrev_table *abbrev_table;
418 
419   /* Hash table holding all the loaded partial DIEs
420      with partial_die->offset.SECT_OFF as hash.  */
421   htab_t partial_dies;
422 
423   /* Storage for things with the same lifetime as this read-in compilation
424      unit, including partial DIEs.  */
425   struct obstack comp_unit_obstack;
426 
427   /* When multiple dwarf2_cu structures are living in memory, this field
428      chains them all together, so that they can be released efficiently.
429      We will probably also want a generation counter so that most-recently-used
430      compilation units are cached...  */
431   struct dwarf2_per_cu_data *read_in_chain;
432 
433   /* Backchain to our per_cu entry if the tree has been built.  */
434   struct dwarf2_per_cu_data *per_cu;
435 
436   /* How many compilation units ago was this CU last referenced?  */
437   int last_used;
438 
439   /* A hash table of DIE cu_offset for following references with
440      die_info->offset.sect_off as hash.  */
441   htab_t die_hash;
442 
443   /* Full DIEs if read in.  */
444   struct die_info *dies;
445 
446   /* A set of pointers to dwarf2_per_cu_data objects for compilation
447      units referenced by this one.  Only set during full symbol processing;
448      partial symbol tables do not have dependencies.  */
449   htab_t dependencies;
450 
451   /* Header data from the line table, during full symbol processing.  */
452   struct line_header *line_header;
453 
454   /* A list of methods which need to have physnames computed
455      after all type information has been read.  */
456   VEC (delayed_method_info) *method_list;
457 
458   /* To be copied to symtab->call_site_htab.  */
459   htab_t call_site_htab;
460 
461   /* Non-NULL if this CU came from a DWO file.
462      There is an invariant here that is important to remember:
463      Except for attributes copied from the top level DIE in the "main"
464      (or "stub") file in preparation for reading the DWO file
465      (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
466      Either there isn't a DWO file (in which case this is NULL and the point
467      is moot), or there is and either we're not going to read it (in which
468      case this is NULL) or there is and we are reading it (in which case this
469      is non-NULL).  */
470   struct dwo_unit *dwo_unit;
471 
472   /* The DW_AT_addr_base attribute if present, zero otherwise
473      (zero is a valid value though).
474      Note this value comes from the stub CU/TU's DIE.  */
475   ULONGEST addr_base;
476 
477   /* The DW_AT_ranges_base attribute if present, zero otherwise
478      (zero is a valid value though).
479      Note this value comes from the stub CU/TU's DIE.
480      Also note that the value is zero in the non-DWO case so this value can
481      be used without needing to know whether DWO files are in use or not.
482      N.B. This does not apply to DW_AT_ranges appearing in
483      DW_TAG_compile_unit dies.  This is a bit of a wart, consider if ever
484      DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
485      DW_AT_ranges_base *would* have to be applied, and we'd have to care
486      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
487   ULONGEST ranges_base;
488 
489   /* Mark used when releasing cached dies.  */
490   unsigned int mark : 1;
491 
492   /* This CU references .debug_loc.  See the symtab->locations_valid field.
493      This test is imperfect as there may exist optimized debug code not using
494      any location list and still facing inlining issues if handled as
495      unoptimized code.  For a future better test see GCC PR other/32998.  */
496   unsigned int has_loclist : 1;
497 
498   /* These cache the results for producer_is_* fields.  CHECKED_PRODUCER is set
499      if all the producer_is_* fields are valid.  This information is cached
500      because profiling CU expansion showed excessive time spent in
501      producer_is_gxx_lt_4_6.  */
502   unsigned int checked_producer : 1;
503   unsigned int producer_is_gxx_lt_4_6 : 1;
504   unsigned int producer_is_gcc_lt_4_3 : 1;
505   unsigned int producer_is_icc : 1;
506 
507   /* When set, the file that we're processing is known to have
508      debugging info for C++ namespaces.  GCC 3.3.x did not produce
509      this information, but later versions do.  */
510 
511   unsigned int processing_has_namespace_info : 1;
512 };
513 
514 /* Persistent data held for a compilation unit, even when not
515    processing it.  We put a pointer to this structure in the
516    read_symtab_private field of the psymtab.  */
517 
518 struct dwarf2_per_cu_data
519 {
520   /* The start offset and length of this compilation unit.
521      NOTE: Unlike comp_unit_head.length, this length includes
522      initial_length_size.
523      If the DIE refers to a DWO file, this is always of the original die,
524      not the DWO file.  */
525   sect_offset offset;
526   unsigned int length;
527 
528   /* Flag indicating this compilation unit will be read in before
529      any of the current compilation units are processed.  */
530   unsigned int queued : 1;
531 
532   /* This flag will be set when reading partial DIEs if we need to load
533      absolutely all DIEs for this compilation unit, instead of just the ones
534      we think are interesting.  It gets set if we look for a DIE in the
535      hash table and don't find it.  */
536   unsigned int load_all_dies : 1;
537 
538   /* Non-zero if this CU is from .debug_types.  */
539   unsigned int is_debug_types : 1;
540 
541   /* Non-zero if this CU is from the .dwz file.  */
542   unsigned int is_dwz : 1;
543 
544   /* The section this CU/TU lives in.
545      If the DIE refers to a DWO file, this is always the original die,
546      not the DWO file.  */
547   struct dwarf2_section_info *info_or_types_section;
548 
549   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
550      of the CU cache it gets reset to NULL again.  */
551   struct dwarf2_cu *cu;
552 
553   /* The corresponding objfile.
554      Normally we can get the objfile from dwarf2_per_objfile.
555      However we can enter this file with just a "per_cu" handle.  */
556   struct objfile *objfile;
557 
558   /* When using partial symbol tables, the 'psymtab' field is active.
559      Otherwise the 'quick' field is active.  */
560   union
561   {
562     /* The partial symbol table associated with this compilation unit,
563        or NULL for unread partial units.  */
564     struct partial_symtab *psymtab;
565 
566     /* Data needed by the "quick" functions.  */
567     struct dwarf2_per_cu_quick_data *quick;
568   } v;
569 
570   /* The CUs we import using DW_TAG_imported_unit.  This is filled in
571      while reading psymtabs, used to compute the psymtab dependencies,
572      and then cleared.  Then it is filled in again while reading full
573      symbols, and only deleted when the objfile is destroyed.
574 
575      This is also used to work around a difference between the way gold
576      generates .gdb_index version <=7 and the way gdb does.  Arguably this
577      is a gold bug.  For symbols coming from TUs, gold records in the index
578      the CU that includes the TU instead of the TU itself.  This breaks
579      dw2_lookup_symbol: It assumes that if the index says symbol X lives
580      in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
581      will find X.  Alas TUs live in their own symtab, so after expanding CU Y
582      we need to look in TU Z to find X.  Fortunately, this is akin to
583      DW_TAG_imported_unit, so we just use the same mechanism: For
584      .gdb_index version <=7 this also records the TUs that the CU referred
585      to.  Concurrently with this change gdb was modified to emit version 8
586      indices so we only pay a price for gold generated indices.  */
587   VEC (dwarf2_per_cu_ptr) *imported_symtabs;
588 
589   /* Type units are grouped by their DW_AT_stmt_list entry so that they
590      can share them.  If this is a TU, this points to the containing
591      symtab.  */
592   struct type_unit_group *type_unit_group;
593 };
594 
595 /* Entry in the signatured_types hash table.  */
596 
597 struct signatured_type
598 {
599   /* The "per_cu" object of this type.
600      N.B.: This is the first member so that it's easy to convert pointers
601      between them.  */
602   struct dwarf2_per_cu_data per_cu;
603 
604   /* The type's signature.  */
605   ULONGEST signature;
606 
607   /* Offset in the TU of the type's DIE, as read from the TU header.
608      If the definition lives in a DWO file, this value is unusable.  */
609   cu_offset type_offset_in_tu;
610 
611   /* Offset in the section of the type's DIE.
612      If the definition lives in a DWO file, this is the offset in the
613      .debug_types.dwo section.
614      The value is zero until the actual value is known.
615      Zero is otherwise not a valid section offset.  */
616   sect_offset type_offset_in_section;
617 };
618 
619 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
620    This includes type_unit_group and quick_file_names.  */
621 
622 struct stmt_list_hash
623 {
624   /* The DWO unit this table is from or NULL if there is none.  */
625   struct dwo_unit *dwo_unit;
626 
627   /* Offset in .debug_line or .debug_line.dwo.  */
628   sect_offset line_offset;
629 };
630 
631 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
632    an object of this type.  */
633 
634 struct type_unit_group
635 {
636   /* dwarf2read.c's main "handle" on the symtab.
637      To simplify things we create an artificial CU that "includes" all the
638      type units using this stmt_list so that the rest of the code still has
639      a "per_cu" handle on the symtab.
640      This PER_CU is recognized by having no section.  */
641 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
642   struct dwarf2_per_cu_data per_cu;
643 
644   union
645   {
646     /* The TUs that share this DW_AT_stmt_list entry.
647        This is added to while parsing type units to build partial symtabs,
648        and is deleted afterwards and not used again.  */
649     VEC (dwarf2_per_cu_ptr) *tus;
650 
651     /* When reading the line table in "quick" functions, we need a real TU.
652        Any will do, we know they all share the same DW_AT_stmt_list entry.
653        For simplicity's sake, we pick the first one.  */
654     struct dwarf2_per_cu_data *first_tu;
655   } t;
656 
657   /* The primary symtab.
658      Type units in a group needn't all be defined in the same source file,
659      so we create an essentially anonymous symtab as the primary symtab.  */
660   struct symtab *primary_symtab;
661 
662   /* The data used to construct the hash key.  */
663   struct stmt_list_hash hash;
664 
665   /* The number of symtabs from the line header.
666      The value here must match line_header.num_file_names.  */
667   unsigned int num_symtabs;
668 
669   /* The symbol tables for this TU (obtained from the files listed in
670      DW_AT_stmt_list).
671      WARNING: The order of entries here must match the order of entries
672      in the line header.  After the first TU using this type_unit_group, the
673      line header for the subsequent TUs is recreated from this.  This is done
674      because we need to use the same symtabs for each TU using the same
675      DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
676      there's no guarantee the line header doesn't have duplicate entries.  */
677   struct symtab **symtabs;
678 };
679 
680 /* These sections are what may appear in a DWO file.  */
681 
682 struct dwo_sections
683 {
684   struct dwarf2_section_info abbrev;
685   struct dwarf2_section_info line;
686   struct dwarf2_section_info loc;
687   struct dwarf2_section_info macinfo;
688   struct dwarf2_section_info macro;
689   struct dwarf2_section_info str;
690   struct dwarf2_section_info str_offsets;
691   /* In the case of a virtual DWO file, these two are unused.  */
692   struct dwarf2_section_info info;
693   VEC (dwarf2_section_info_def) *types;
694 };
695 
696 /* Common bits of DWO CUs/TUs.  */
697 
698 struct dwo_unit
699 {
700   /* Backlink to the containing struct dwo_file.  */
701   struct dwo_file *dwo_file;
702 
703   /* The "id" that distinguishes this CU/TU.
704      .debug_info calls this "dwo_id", .debug_types calls this "signature".
705      Since signatures came first, we stick with it for consistency.  */
706   ULONGEST signature;
707 
708   /* The section this CU/TU lives in, in the DWO file.  */
709   struct dwarf2_section_info *info_or_types_section;
710 
711   /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section.  */
712   sect_offset offset;
713   unsigned int length;
714 
715   /* For types, offset in the type's DIE of the type defined by this TU.  */
716   cu_offset type_offset_in_tu;
717 };
718 
719 /* Data for one DWO file.
720    This includes virtual DWO files that have been packaged into a
721    DWP file.  */
722 
723 struct dwo_file
724 {
725   /* The DW_AT_GNU_dwo_name attribute.  This is the hash key.
726      For virtual DWO files the name is constructed from the section offsets
727      of abbrev,line,loc,str_offsets so that we combine virtual DWO files
728      from related CU+TUs.  */
729   const char *name;
730 
731   /* The bfd, when the file is open.  Otherwise this is NULL.
732      This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd.  */
733   bfd *dbfd;
734 
735   /* Section info for this file.  */
736   struct dwo_sections sections;
737 
738   /* Table of CUs in the file.
739      Each element is a struct dwo_unit.  */
740   htab_t cus;
741 
742   /* Table of TUs in the file.
743      Each element is a struct dwo_unit.  */
744   htab_t tus;
745 };
746 
747 /* These sections are what may appear in a DWP file.  */
748 
749 struct dwp_sections
750 {
751   struct dwarf2_section_info str;
752   struct dwarf2_section_info cu_index;
753   struct dwarf2_section_info tu_index;
754   /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
755      by section number.  We don't need to record them here.  */
756 };
757 
758 /* These sections are what may appear in a virtual DWO file.  */
759 
760 struct virtual_dwo_sections
761 {
762   struct dwarf2_section_info abbrev;
763   struct dwarf2_section_info line;
764   struct dwarf2_section_info loc;
765   struct dwarf2_section_info macinfo;
766   struct dwarf2_section_info macro;
767   struct dwarf2_section_info str_offsets;
768   /* Each DWP hash table entry records one CU or one TU.
769      That is recorded here, and copied to dwo_unit.info_or_types_section.  */
770   struct dwarf2_section_info info_or_types;
771 };
772 
773 /* Contents of DWP hash tables.  */
774 
775 struct dwp_hash_table
776 {
777   uint32_t nr_units, nr_slots;
778   const gdb_byte *hash_table, *unit_table, *section_pool;
779 };
780 
781 /* Data for one DWP file.  */
782 
783 struct dwp_file
784 {
785   /* Name of the file.  */
786   const char *name;
787 
788   /* The bfd, when the file is open.  Otherwise this is NULL.  */
789   bfd *dbfd;
790 
791   /* Section info for this file.  */
792   struct dwp_sections sections;
793 
794   /* Table of CUs in the file. */
795   const struct dwp_hash_table *cus;
796 
797   /* Table of TUs in the file.  */
798   const struct dwp_hash_table *tus;
799 
800   /* Table of loaded CUs/TUs.  Each entry is a struct dwo_unit *.  */
801   htab_t loaded_cutus;
802 
803   /* Table to map ELF section numbers to their sections.  */
804   unsigned int num_sections;
805   asection **elf_sections;
806 };
807 
808 /* This represents a '.dwz' file.  */
809 
810 struct dwz_file
811 {
812   /* A dwz file can only contain a few sections.  */
813   struct dwarf2_section_info abbrev;
814   struct dwarf2_section_info info;
815   struct dwarf2_section_info str;
816   struct dwarf2_section_info line;
817   struct dwarf2_section_info macro;
818   struct dwarf2_section_info gdb_index;
819 
820   /* The dwz's BFD.  */
821   bfd *dwz_bfd;
822 };
823 
824 /* Struct used to pass misc. parameters to read_die_and_children, et
825    al.  which are used for both .debug_info and .debug_types dies.
826    All parameters here are unchanging for the life of the call.  This
827    struct exists to abstract away the constant parameters of die reading.  */
828 
829 struct die_reader_specs
830 {
831   /* die_section->asection->owner.  */
832   bfd* abfd;
833 
834   /* The CU of the DIE we are parsing.  */
835   struct dwarf2_cu *cu;
836 
837   /* Non-NULL if reading a DWO file (including one packaged into a DWP).  */
838   struct dwo_file *dwo_file;
839 
840   /* The section the die comes from.
841      This is either .debug_info or .debug_types, or the .dwo variants.  */
842   struct dwarf2_section_info *die_section;
843 
844   /* die_section->buffer.  */
845   gdb_byte *buffer;
846 
847   /* The end of the buffer.  */
848   const gdb_byte *buffer_end;
849 };
850 
851 /* Type of function passed to init_cutu_and_read_dies, et.al.  */
852 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
853 				      gdb_byte *info_ptr,
854 				      struct die_info *comp_unit_die,
855 				      int has_children,
856 				      void *data);
857 
858 /* The line number information for a compilation unit (found in the
859    .debug_line section) begins with a "statement program header",
860    which contains the following information.  */
861 struct line_header
862 {
863   unsigned int total_length;
864   unsigned short version;
865   unsigned int header_length;
866   unsigned char minimum_instruction_length;
867   unsigned char maximum_ops_per_instruction;
868   unsigned char default_is_stmt;
869   int line_base;
870   unsigned char line_range;
871   unsigned char opcode_base;
872 
873   /* standard_opcode_lengths[i] is the number of operands for the
874      standard opcode whose value is i.  This means that
875      standard_opcode_lengths[0] is unused, and the last meaningful
876      element is standard_opcode_lengths[opcode_base - 1].  */
877   unsigned char *standard_opcode_lengths;
878 
879   /* The include_directories table.  NOTE!  These strings are not
880      allocated with xmalloc; instead, they are pointers into
881      debug_line_buffer.  If you try to free them, `free' will get
882      indigestion.  */
883   unsigned int num_include_dirs, include_dirs_size;
884   char **include_dirs;
885 
886   /* The file_names table.  NOTE!  These strings are not allocated
887      with xmalloc; instead, they are pointers into debug_line_buffer.
888      Don't try to free them directly.  */
889   unsigned int num_file_names, file_names_size;
890   struct file_entry
891   {
892     char *name;
893     unsigned int dir_index;
894     unsigned int mod_time;
895     unsigned int length;
896     int included_p; /* Non-zero if referenced by the Line Number Program.  */
897     struct symtab *symtab; /* The associated symbol table, if any.  */
898   } *file_names;
899 
900   /* The start and end of the statement program following this
901      header.  These point into dwarf2_per_objfile->line_buffer.  */
902   gdb_byte *statement_program_start, *statement_program_end;
903 };
904 
905 /* When we construct a partial symbol table entry we only
906    need this much information.  */
907 struct partial_die_info
908   {
909     /* Offset of this DIE.  */
910     sect_offset offset;
911 
912     /* DWARF-2 tag for this DIE.  */
913     ENUM_BITFIELD(dwarf_tag) tag : 16;
914 
915     /* Assorted flags describing the data found in this DIE.  */
916     unsigned int has_children : 1;
917     unsigned int is_external : 1;
918     unsigned int is_declaration : 1;
919     unsigned int has_type : 1;
920     unsigned int has_specification : 1;
921     unsigned int has_pc_info : 1;
922     unsigned int may_be_inlined : 1;
923 
924     /* Flag set if the SCOPE field of this structure has been
925        computed.  */
926     unsigned int scope_set : 1;
927 
928     /* Flag set if the DIE has a byte_size attribute.  */
929     unsigned int has_byte_size : 1;
930 
931     /* Flag set if any of the DIE's children are template arguments.  */
932     unsigned int has_template_arguments : 1;
933 
934     /* Flag set if fixup_partial_die has been called on this die.  */
935     unsigned int fixup_called : 1;
936 
937     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
938     unsigned int is_dwz : 1;
939 
940     /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
941     unsigned int spec_is_dwz : 1;
942 
943     /* The name of this DIE.  Normally the value of DW_AT_name, but
944        sometimes a default name for unnamed DIEs.  */
945     const char *name;
946 
947     /* The linkage name, if present.  */
948     const char *linkage_name;
949 
950     /* The scope to prepend to our children.  This is generally
951        allocated on the comp_unit_obstack, so will disappear
952        when this compilation unit leaves the cache.  */
953     const char *scope;
954 
955     /* Some data associated with the partial DIE.  The tag determines
956        which field is live.  */
957     union
958     {
959       /* The location description associated with this DIE, if any.  */
960       struct dwarf_block *locdesc;
961       /* The offset of an import, for DW_TAG_imported_unit.  */
962       sect_offset offset;
963     } d;
964 
965     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
966     CORE_ADDR lowpc;
967     CORE_ADDR highpc;
968 
969     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
970        DW_AT_sibling, if any.  */
971     /* NOTE: This member isn't strictly necessary, read_partial_die could
972        return DW_AT_sibling values to its caller load_partial_dies.  */
973     gdb_byte *sibling;
974 
975     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
976        DW_AT_specification (or DW_AT_abstract_origin or
977        DW_AT_extension).  */
978     sect_offset spec_offset;
979 
980     /* Pointers to this DIE's parent, first child, and next sibling,
981        if any.  */
982     struct partial_die_info *die_parent, *die_child, *die_sibling;
983   };
984 
985 /* This data structure holds the information of an abbrev.  */
986 struct abbrev_info
987   {
988     unsigned int number;	/* number identifying abbrev */
989     enum dwarf_tag tag;		/* dwarf tag */
990     unsigned short has_children;		/* boolean */
991     unsigned short num_attrs;	/* number of attributes */
992     struct attr_abbrev *attrs;	/* an array of attribute descriptions */
993     struct abbrev_info *next;	/* next in chain */
994   };
995 
996 struct attr_abbrev
997   {
998     ENUM_BITFIELD(dwarf_attribute) name : 16;
999     ENUM_BITFIELD(dwarf_form) form : 16;
1000   };
1001 
1002 /* Size of abbrev_table.abbrev_hash_table.  */
1003 #define ABBREV_HASH_SIZE 121
1004 
1005 /* Top level data structure to contain an abbreviation table.  */
1006 
1007 struct abbrev_table
1008 {
1009   /* Where the abbrev table came from.
1010      This is used as a sanity check when the table is used.  */
1011   sect_offset offset;
1012 
1013   /* Storage for the abbrev table.  */
1014   struct obstack abbrev_obstack;
1015 
1016   /* Hash table of abbrevs.
1017      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1018      It could be statically allocated, but the previous code didn't so we
1019      don't either.  */
1020   struct abbrev_info **abbrevs;
1021 };
1022 
1023 /* Attributes have a name and a value.  */
1024 struct attribute
1025   {
1026     ENUM_BITFIELD(dwarf_attribute) name : 16;
1027     ENUM_BITFIELD(dwarf_form) form : 15;
1028 
1029     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
1030        field should be in u.str (existing only for DW_STRING) but it is kept
1031        here for better struct attribute alignment.  */
1032     unsigned int string_is_canonical : 1;
1033 
1034     union
1035       {
1036 	const char *str;
1037 	struct dwarf_block *blk;
1038 	ULONGEST unsnd;
1039 	LONGEST snd;
1040 	CORE_ADDR addr;
1041 	struct signatured_type *signatured_type;
1042       }
1043     u;
1044   };
1045 
1046 /* This data structure holds a complete die structure.  */
1047 struct die_info
1048   {
1049     /* DWARF-2 tag for this DIE.  */
1050     ENUM_BITFIELD(dwarf_tag) tag : 16;
1051 
1052     /* Number of attributes */
1053     unsigned char num_attrs;
1054 
1055     /* True if we're presently building the full type name for the
1056        type derived from this DIE.  */
1057     unsigned char building_fullname : 1;
1058 
1059     /* Abbrev number */
1060     unsigned int abbrev;
1061 
1062     /* Offset in .debug_info or .debug_types section.  */
1063     sect_offset offset;
1064 
1065     /* The dies in a compilation unit form an n-ary tree.  PARENT
1066        points to this die's parent; CHILD points to the first child of
1067        this node; and all the children of a given node are chained
1068        together via their SIBLING fields.  */
1069     struct die_info *child;	/* Its first child, if any.  */
1070     struct die_info *sibling;	/* Its next sibling, if any.  */
1071     struct die_info *parent;	/* Its parent, if any.  */
1072 
1073     /* An array of attributes, with NUM_ATTRS elements.  There may be
1074        zero, but it's not common and zero-sized arrays are not
1075        sufficiently portable C.  */
1076     struct attribute attrs[1];
1077   };
1078 
1079 /* Get at parts of an attribute structure.  */
1080 
1081 #define DW_STRING(attr)    ((attr)->u.str)
1082 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1083 #define DW_UNSND(attr)     ((attr)->u.unsnd)
1084 #define DW_BLOCK(attr)     ((attr)->u.blk)
1085 #define DW_SND(attr)       ((attr)->u.snd)
1086 #define DW_ADDR(attr)	   ((attr)->u.addr)
1087 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1088 
1089 /* Blocks are a bunch of untyped bytes.  */
1090 struct dwarf_block
1091   {
1092     size_t size;
1093 
1094     /* Valid only if SIZE is not zero.  */
1095     gdb_byte *data;
1096   };
1097 
1098 #ifndef ATTR_ALLOC_CHUNK
1099 #define ATTR_ALLOC_CHUNK 4
1100 #endif
1101 
1102 /* Allocate fields for structs, unions and enums in this size.  */
1103 #ifndef DW_FIELD_ALLOC_CHUNK
1104 #define DW_FIELD_ALLOC_CHUNK 4
1105 #endif
1106 
1107 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1108    but this would require a corresponding change in unpack_field_as_long
1109    and friends.  */
1110 static int bits_per_byte = 8;
1111 
1112 /* The routines that read and process dies for a C struct or C++ class
1113    pass lists of data member fields and lists of member function fields
1114    in an instance of a field_info structure, as defined below.  */
1115 struct field_info
1116   {
1117     /* List of data member and baseclasses fields.  */
1118     struct nextfield
1119       {
1120 	struct nextfield *next;
1121 	int accessibility;
1122 	int virtuality;
1123 	struct field field;
1124       }
1125      *fields, *baseclasses;
1126 
1127     /* Number of fields (including baseclasses).  */
1128     int nfields;
1129 
1130     /* Number of baseclasses.  */
1131     int nbaseclasses;
1132 
1133     /* Set if the accesibility of one of the fields is not public.  */
1134     int non_public_fields;
1135 
1136     /* Member function fields array, entries are allocated in the order they
1137        are encountered in the object file.  */
1138     struct nextfnfield
1139       {
1140 	struct nextfnfield *next;
1141 	struct fn_field fnfield;
1142       }
1143      *fnfields;
1144 
1145     /* Member function fieldlist array, contains name of possibly overloaded
1146        member function, number of overloaded member functions and a pointer
1147        to the head of the member function field chain.  */
1148     struct fnfieldlist
1149       {
1150 	const char *name;
1151 	int length;
1152 	struct nextfnfield *head;
1153       }
1154      *fnfieldlists;
1155 
1156     /* Number of entries in the fnfieldlists array.  */
1157     int nfnfields;
1158 
1159     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
1160        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
1161     struct typedef_field_list
1162       {
1163 	struct typedef_field field;
1164 	struct typedef_field_list *next;
1165       }
1166     *typedef_field_list;
1167     unsigned typedef_field_list_count;
1168   };
1169 
1170 /* One item on the queue of compilation units to read in full symbols
1171    for.  */
1172 struct dwarf2_queue_item
1173 {
1174   struct dwarf2_per_cu_data *per_cu;
1175   enum language pretend_language;
1176   struct dwarf2_queue_item *next;
1177 };
1178 
1179 /* The current queue.  */
1180 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1181 
1182 /* Loaded secondary compilation units are kept in memory until they
1183    have not been referenced for the processing of this many
1184    compilation units.  Set this to zero to disable caching.  Cache
1185    sizes of up to at least twenty will improve startup time for
1186    typical inter-CU-reference binaries, at an obvious memory cost.  */
1187 static int dwarf2_max_cache_age = 5;
1188 static void
1189 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1190 			   struct cmd_list_element *c, const char *value)
1191 {
1192   fprintf_filtered (file, _("The upper bound on the age of cached "
1193 			    "dwarf2 compilation units is %s.\n"),
1194 		    value);
1195 }
1196 
1197 
1198 /* Various complaints about symbol reading that don't abort the process.  */
1199 
1200 static void
1201 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1202 {
1203   complaint (&symfile_complaints,
1204 	     _("statement list doesn't fit in .debug_line section"));
1205 }
1206 
1207 static void
1208 dwarf2_debug_line_missing_file_complaint (void)
1209 {
1210   complaint (&symfile_complaints,
1211 	     _(".debug_line section has line data without a file"));
1212 }
1213 
1214 static void
1215 dwarf2_debug_line_missing_end_sequence_complaint (void)
1216 {
1217   complaint (&symfile_complaints,
1218 	     _(".debug_line section has line "
1219 	       "program sequence without an end"));
1220 }
1221 
1222 static void
1223 dwarf2_complex_location_expr_complaint (void)
1224 {
1225   complaint (&symfile_complaints, _("location expression too complex"));
1226 }
1227 
1228 static void
1229 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1230 					      int arg3)
1231 {
1232   complaint (&symfile_complaints,
1233 	     _("const value length mismatch for '%s', got %d, expected %d"),
1234 	     arg1, arg2, arg3);
1235 }
1236 
1237 static void
1238 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1239 {
1240   complaint (&symfile_complaints,
1241 	     _("debug info runs off end of %s section"
1242 	       " [in module %s]"),
1243 	     section->asection->name,
1244 	     bfd_get_filename (section->asection->owner));
1245 }
1246 
1247 static void
1248 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1249 {
1250   complaint (&symfile_complaints,
1251 	     _("macro debug info contains a "
1252 	       "malformed macro definition:\n`%s'"),
1253 	     arg1);
1254 }
1255 
1256 static void
1257 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1258 {
1259   complaint (&symfile_complaints,
1260 	     _("invalid attribute class or form for '%s' in '%s'"),
1261 	     arg1, arg2);
1262 }
1263 
1264 /* local function prototypes */
1265 
1266 static void dwarf2_locate_sections (bfd *, asection *, void *);
1267 
1268 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1269                                            struct objfile *);
1270 
1271 static void dwarf2_find_base_address (struct die_info *die,
1272 				      struct dwarf2_cu *cu);
1273 
1274 static void dwarf2_build_psymtabs_hard (struct objfile *);
1275 
1276 static void scan_partial_symbols (struct partial_die_info *,
1277 				  CORE_ADDR *, CORE_ADDR *,
1278 				  int, struct dwarf2_cu *);
1279 
1280 static void add_partial_symbol (struct partial_die_info *,
1281 				struct dwarf2_cu *);
1282 
1283 static void add_partial_namespace (struct partial_die_info *pdi,
1284 				   CORE_ADDR *lowpc, CORE_ADDR *highpc,
1285 				   int need_pc, struct dwarf2_cu *cu);
1286 
1287 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1288 				CORE_ADDR *highpc, int need_pc,
1289 				struct dwarf2_cu *cu);
1290 
1291 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1292 				     struct dwarf2_cu *cu);
1293 
1294 static void add_partial_subprogram (struct partial_die_info *pdi,
1295 				    CORE_ADDR *lowpc, CORE_ADDR *highpc,
1296 				    int need_pc, struct dwarf2_cu *cu);
1297 
1298 static void dwarf2_read_symtab (struct partial_symtab *,
1299 				struct objfile *);
1300 
1301 static void psymtab_to_symtab_1 (struct partial_symtab *);
1302 
1303 static struct abbrev_info *abbrev_table_lookup_abbrev
1304   (const struct abbrev_table *, unsigned int);
1305 
1306 static struct abbrev_table *abbrev_table_read_table
1307   (struct dwarf2_section_info *, sect_offset);
1308 
1309 static void abbrev_table_free (struct abbrev_table *);
1310 
1311 static void abbrev_table_free_cleanup (void *);
1312 
1313 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1314 				 struct dwarf2_section_info *);
1315 
1316 static void dwarf2_free_abbrev_table (void *);
1317 
1318 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1319 
1320 static struct partial_die_info *load_partial_dies
1321   (const struct die_reader_specs *, gdb_byte *, int);
1322 
1323 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1324 				   struct partial_die_info *,
1325 				   struct abbrev_info *,
1326 				   unsigned int,
1327 				   gdb_byte *);
1328 
1329 static struct partial_die_info *find_partial_die (sect_offset, int,
1330 						  struct dwarf2_cu *);
1331 
1332 static void fixup_partial_die (struct partial_die_info *,
1333 			       struct dwarf2_cu *);
1334 
1335 static gdb_byte *read_attribute (const struct die_reader_specs *,
1336 				 struct attribute *, struct attr_abbrev *,
1337 				 gdb_byte *);
1338 
1339 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1340 
1341 static int read_1_signed_byte (bfd *, const gdb_byte *);
1342 
1343 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1344 
1345 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1346 
1347 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1348 
1349 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1350 			       unsigned int *);
1351 
1352 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1353 
1354 static LONGEST read_checked_initial_length_and_offset
1355   (bfd *, gdb_byte *, const struct comp_unit_head *,
1356    unsigned int *, unsigned int *);
1357 
1358 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1359 			    unsigned int *);
1360 
1361 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1362 
1363 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1364 				       sect_offset);
1365 
1366 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1367 
1368 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1369 
1370 static char *read_indirect_string (bfd *, gdb_byte *,
1371                                    const struct comp_unit_head *,
1372                                    unsigned int *);
1373 
1374 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1375 
1376 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1377 
1378 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1379 
1380 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1381 					      unsigned int *);
1382 
1383 static char *read_str_index (const struct die_reader_specs *reader,
1384 			     struct dwarf2_cu *cu, ULONGEST str_index);
1385 
1386 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1387 
1388 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1389 				      struct dwarf2_cu *);
1390 
1391 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1392 						unsigned int);
1393 
1394 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1395                                struct dwarf2_cu *cu);
1396 
1397 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1398 
1399 static struct die_info *die_specification (struct die_info *die,
1400 					   struct dwarf2_cu **);
1401 
1402 static void free_line_header (struct line_header *lh);
1403 
1404 static void add_file_name (struct line_header *, char *, unsigned int,
1405                            unsigned int, unsigned int);
1406 
1407 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1408 						     struct dwarf2_cu *cu);
1409 
1410 static void dwarf_decode_lines (struct line_header *, const char *,
1411 				struct dwarf2_cu *, struct partial_symtab *,
1412 				int);
1413 
1414 static void dwarf2_start_subfile (char *, const char *, const char *);
1415 
1416 static void dwarf2_start_symtab (struct dwarf2_cu *,
1417 				 const char *, const char *, CORE_ADDR);
1418 
1419 static struct symbol *new_symbol (struct die_info *, struct type *,
1420 				  struct dwarf2_cu *);
1421 
1422 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1423 				       struct dwarf2_cu *, struct symbol *);
1424 
1425 static void dwarf2_const_value (struct attribute *, struct symbol *,
1426 				struct dwarf2_cu *);
1427 
1428 static void dwarf2_const_value_attr (struct attribute *attr,
1429 				     struct type *type,
1430 				     const char *name,
1431 				     struct obstack *obstack,
1432 				     struct dwarf2_cu *cu, LONGEST *value,
1433 				     gdb_byte **bytes,
1434 				     struct dwarf2_locexpr_baton **baton);
1435 
1436 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1437 
1438 static int need_gnat_info (struct dwarf2_cu *);
1439 
1440 static struct type *die_descriptive_type (struct die_info *,
1441 					  struct dwarf2_cu *);
1442 
1443 static void set_descriptive_type (struct type *, struct die_info *,
1444 				  struct dwarf2_cu *);
1445 
1446 static struct type *die_containing_type (struct die_info *,
1447 					 struct dwarf2_cu *);
1448 
1449 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1450 				     struct dwarf2_cu *);
1451 
1452 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1453 
1454 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1455 
1456 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1457 
1458 static char *typename_concat (struct obstack *obs, const char *prefix,
1459 			      const char *suffix, int physname,
1460 			      struct dwarf2_cu *cu);
1461 
1462 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1463 
1464 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1465 
1466 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1467 
1468 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1469 
1470 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1471 
1472 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1473 			       struct dwarf2_cu *, struct partial_symtab *);
1474 
1475 static int dwarf2_get_pc_bounds (struct die_info *,
1476 				 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1477 				 struct partial_symtab *);
1478 
1479 static void get_scope_pc_bounds (struct die_info *,
1480 				 CORE_ADDR *, CORE_ADDR *,
1481 				 struct dwarf2_cu *);
1482 
1483 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1484                                         CORE_ADDR, struct dwarf2_cu *);
1485 
1486 static void dwarf2_add_field (struct field_info *, struct die_info *,
1487 			      struct dwarf2_cu *);
1488 
1489 static void dwarf2_attach_fields_to_type (struct field_info *,
1490 					  struct type *, struct dwarf2_cu *);
1491 
1492 static void dwarf2_add_member_fn (struct field_info *,
1493 				  struct die_info *, struct type *,
1494 				  struct dwarf2_cu *);
1495 
1496 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1497 					     struct type *,
1498 					     struct dwarf2_cu *);
1499 
1500 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1501 
1502 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1503 
1504 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1505 
1506 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1507 
1508 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1509 
1510 static struct type *read_module_type (struct die_info *die,
1511 				      struct dwarf2_cu *cu);
1512 
1513 static const char *namespace_name (struct die_info *die,
1514 				   int *is_anonymous, struct dwarf2_cu *);
1515 
1516 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1517 
1518 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1519 
1520 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1521 						       struct dwarf2_cu *);
1522 
1523 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1524 					       gdb_byte *info_ptr,
1525 					       gdb_byte **new_info_ptr,
1526 					       struct die_info *parent);
1527 
1528 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1529 					       gdb_byte *info_ptr,
1530 					       gdb_byte **new_info_ptr,
1531 					       struct die_info *parent);
1532 
1533 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1534 				  struct die_info **, gdb_byte *, int *, int);
1535 
1536 static gdb_byte *read_full_die (const struct die_reader_specs *,
1537 				struct die_info **, gdb_byte *, int *);
1538 
1539 static void process_die (struct die_info *, struct dwarf2_cu *);
1540 
1541 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1542 					     struct obstack *);
1543 
1544 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1545 
1546 static const char *dwarf2_full_name (const char *name,
1547 				     struct die_info *die,
1548 				     struct dwarf2_cu *cu);
1549 
1550 static struct die_info *dwarf2_extension (struct die_info *die,
1551 					  struct dwarf2_cu **);
1552 
1553 static const char *dwarf_tag_name (unsigned int);
1554 
1555 static const char *dwarf_attr_name (unsigned int);
1556 
1557 static const char *dwarf_form_name (unsigned int);
1558 
1559 static char *dwarf_bool_name (unsigned int);
1560 
1561 static const char *dwarf_type_encoding_name (unsigned int);
1562 
1563 static struct die_info *sibling_die (struct die_info *);
1564 
1565 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1566 
1567 static void dump_die_for_error (struct die_info *);
1568 
1569 static void dump_die_1 (struct ui_file *, int level, int max_level,
1570 			struct die_info *);
1571 
1572 /*static*/ void dump_die (struct die_info *, int max_level);
1573 
1574 static void store_in_ref_table (struct die_info *,
1575 				struct dwarf2_cu *);
1576 
1577 static int is_ref_attr (struct attribute *);
1578 
1579 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1580 
1581 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1582 
1583 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1584 					       struct attribute *,
1585 					       struct dwarf2_cu **);
1586 
1587 static struct die_info *follow_die_ref (struct die_info *,
1588 					struct attribute *,
1589 					struct dwarf2_cu **);
1590 
1591 static struct die_info *follow_die_sig (struct die_info *,
1592 					struct attribute *,
1593 					struct dwarf2_cu **);
1594 
1595 static struct signatured_type *lookup_signatured_type_at_offset
1596     (struct objfile *objfile,
1597      struct dwarf2_section_info *section, sect_offset offset);
1598 
1599 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1600 
1601 static void read_signatured_type (struct signatured_type *);
1602 
1603 static struct type_unit_group *get_type_unit_group
1604     (struct dwarf2_cu *, struct attribute *);
1605 
1606 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1607 
1608 /* memory allocation interface */
1609 
1610 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1611 
1612 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1613 
1614 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1615 				 const char *, int);
1616 
1617 static int attr_form_is_block (struct attribute *);
1618 
1619 static int attr_form_is_section_offset (struct attribute *);
1620 
1621 static int attr_form_is_constant (struct attribute *);
1622 
1623 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1624 				   struct dwarf2_loclist_baton *baton,
1625 				   struct attribute *attr);
1626 
1627 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1628 					 struct symbol *sym,
1629 					 struct dwarf2_cu *cu);
1630 
1631 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1632 			       gdb_byte *info_ptr,
1633 			       struct abbrev_info *abbrev);
1634 
1635 static void free_stack_comp_unit (void *);
1636 
1637 static hashval_t partial_die_hash (const void *item);
1638 
1639 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1640 
1641 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1642   (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1643 
1644 static void init_one_comp_unit (struct dwarf2_cu *cu,
1645 				struct dwarf2_per_cu_data *per_cu);
1646 
1647 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1648 				   struct die_info *comp_unit_die,
1649 				   enum language pretend_language);
1650 
1651 static void free_heap_comp_unit (void *);
1652 
1653 static void free_cached_comp_units (void *);
1654 
1655 static void age_cached_comp_units (void);
1656 
1657 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1658 
1659 static struct type *set_die_type (struct die_info *, struct type *,
1660 				  struct dwarf2_cu *);
1661 
1662 static void create_all_comp_units (struct objfile *);
1663 
1664 static int create_all_type_units (struct objfile *);
1665 
1666 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1667 				 enum language);
1668 
1669 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1670 				    enum language);
1671 
1672 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1673 				    enum language);
1674 
1675 static void dwarf2_add_dependence (struct dwarf2_cu *,
1676 				   struct dwarf2_per_cu_data *);
1677 
1678 static void dwarf2_mark (struct dwarf2_cu *);
1679 
1680 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1681 
1682 static struct type *get_die_type_at_offset (sect_offset,
1683 					    struct dwarf2_per_cu_data *per_cu);
1684 
1685 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1686 
1687 static void dwarf2_release_queue (void *dummy);
1688 
1689 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1690 			     enum language pretend_language);
1691 
1692 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1693 				  struct dwarf2_per_cu_data *per_cu,
1694 				  enum language pretend_language);
1695 
1696 static void process_queue (void);
1697 
1698 static void find_file_and_directory (struct die_info *die,
1699 				     struct dwarf2_cu *cu,
1700 				     const char **name, const char **comp_dir);
1701 
1702 static char *file_full_name (int file, struct line_header *lh,
1703 			     const char *comp_dir);
1704 
1705 static gdb_byte *read_and_check_comp_unit_head
1706   (struct comp_unit_head *header,
1707    struct dwarf2_section_info *section,
1708    struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1709    int is_debug_types_section);
1710 
1711 static void init_cutu_and_read_dies
1712   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1713    int use_existing_cu, int keep,
1714    die_reader_func_ftype *die_reader_func, void *data);
1715 
1716 static void init_cutu_and_read_dies_simple
1717   (struct dwarf2_per_cu_data *this_cu,
1718    die_reader_func_ftype *die_reader_func, void *data);
1719 
1720 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1721 
1722 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1723 
1724 static struct dwo_unit *lookup_dwo_comp_unit
1725   (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1726 
1727 static struct dwo_unit *lookup_dwo_type_unit
1728   (struct signatured_type *, const char *, const char *);
1729 
1730 static void free_dwo_file_cleanup (void *);
1731 
1732 static void process_cu_includes (void);
1733 
1734 static void check_producer (struct dwarf2_cu *cu);
1735 
1736 #if WORDS_BIGENDIAN
1737 
1738 /* Convert VALUE between big- and little-endian.  */
1739 static offset_type
1740 byte_swap (offset_type value)
1741 {
1742   offset_type result;
1743 
1744   result = (value & 0xff) << 24;
1745   result |= (value & 0xff00) << 8;
1746   result |= (value & 0xff0000) >> 8;
1747   result |= (value & 0xff000000) >> 24;
1748   return result;
1749 }
1750 
1751 #define MAYBE_SWAP(V)  byte_swap (V)
1752 
1753 #else
1754 #define MAYBE_SWAP(V) (V)
1755 #endif /* WORDS_BIGENDIAN */
1756 
1757 /* The suffix for an index file.  */
1758 #define INDEX_SUFFIX ".gdb-index"
1759 
1760 static const char *dwarf2_physname (const char *name, struct die_info *die,
1761 				    struct dwarf2_cu *cu);
1762 
1763 /* Try to locate the sections we need for DWARF 2 debugging
1764    information and return true if we have enough to do something.
1765    NAMES points to the dwarf2 section names, or is NULL if the standard
1766    ELF names are used.  */
1767 
1768 int
1769 dwarf2_has_info (struct objfile *objfile,
1770                  const struct dwarf2_debug_sections *names)
1771 {
1772   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1773   if (!dwarf2_per_objfile)
1774     {
1775       /* Initialize per-objfile state.  */
1776       struct dwarf2_per_objfile *data
1777 	= obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1778 
1779       memset (data, 0, sizeof (*data));
1780       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1781       dwarf2_per_objfile = data;
1782 
1783       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1784                              (void *) names);
1785       dwarf2_per_objfile->objfile = objfile;
1786     }
1787   return (dwarf2_per_objfile->info.asection != NULL
1788 	  && dwarf2_per_objfile->abbrev.asection != NULL);
1789 }
1790 
1791 /* When loading sections, we look either for uncompressed section or for
1792    compressed section names.  */
1793 
1794 static int
1795 section_is_p (const char *section_name,
1796               const struct dwarf2_section_names *names)
1797 {
1798   if (names->normal != NULL
1799       && strcmp (section_name, names->normal) == 0)
1800     return 1;
1801   if (names->compressed != NULL
1802       && strcmp (section_name, names->compressed) == 0)
1803     return 1;
1804   return 0;
1805 }
1806 
1807 /* This function is mapped across the sections and remembers the
1808    offset and size of each of the debugging sections we are interested
1809    in.  */
1810 
1811 static void
1812 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1813 {
1814   const struct dwarf2_debug_sections *names;
1815   flagword aflag = bfd_get_section_flags (abfd, sectp);
1816 
1817   if (vnames == NULL)
1818     names = &dwarf2_elf_names;
1819   else
1820     names = (const struct dwarf2_debug_sections *) vnames;
1821 
1822   if ((aflag & SEC_HAS_CONTENTS) == 0)
1823     {
1824     }
1825   else if (section_is_p (sectp->name, &names->info))
1826     {
1827       dwarf2_per_objfile->info.asection = sectp;
1828       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1829     }
1830   else if (section_is_p (sectp->name, &names->abbrev))
1831     {
1832       dwarf2_per_objfile->abbrev.asection = sectp;
1833       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1834     }
1835   else if (section_is_p (sectp->name, &names->line))
1836     {
1837       dwarf2_per_objfile->line.asection = sectp;
1838       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1839     }
1840   else if (section_is_p (sectp->name, &names->loc))
1841     {
1842       dwarf2_per_objfile->loc.asection = sectp;
1843       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1844     }
1845   else if (section_is_p (sectp->name, &names->macinfo))
1846     {
1847       dwarf2_per_objfile->macinfo.asection = sectp;
1848       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1849     }
1850   else if (section_is_p (sectp->name, &names->macro))
1851     {
1852       dwarf2_per_objfile->macro.asection = sectp;
1853       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1854     }
1855   else if (section_is_p (sectp->name, &names->str))
1856     {
1857       dwarf2_per_objfile->str.asection = sectp;
1858       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1859     }
1860   else if (section_is_p (sectp->name, &names->addr))
1861     {
1862       dwarf2_per_objfile->addr.asection = sectp;
1863       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1864     }
1865   else if (section_is_p (sectp->name, &names->frame))
1866     {
1867       dwarf2_per_objfile->frame.asection = sectp;
1868       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1869     }
1870   else if (section_is_p (sectp->name, &names->eh_frame))
1871     {
1872       dwarf2_per_objfile->eh_frame.asection = sectp;
1873       dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1874     }
1875   else if (section_is_p (sectp->name, &names->ranges))
1876     {
1877       dwarf2_per_objfile->ranges.asection = sectp;
1878       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1879     }
1880   else if (section_is_p (sectp->name, &names->types))
1881     {
1882       struct dwarf2_section_info type_section;
1883 
1884       memset (&type_section, 0, sizeof (type_section));
1885       type_section.asection = sectp;
1886       type_section.size = bfd_get_section_size (sectp);
1887 
1888       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1889 		     &type_section);
1890     }
1891   else if (section_is_p (sectp->name, &names->gdb_index))
1892     {
1893       dwarf2_per_objfile->gdb_index.asection = sectp;
1894       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1895     }
1896 
1897   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1898       && bfd_section_vma (abfd, sectp) == 0)
1899     dwarf2_per_objfile->has_section_at_zero = 1;
1900 }
1901 
1902 /* A helper function that decides whether a section is empty,
1903    or not present.  */
1904 
1905 static int
1906 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1907 {
1908   return info->asection == NULL || info->size == 0;
1909 }
1910 
1911 /* Read the contents of the section INFO.
1912    OBJFILE is the main object file, but not necessarily the file where
1913    the section comes from.  E.g., for DWO files INFO->asection->owner
1914    is the bfd of the DWO file.
1915    If the section is compressed, uncompress it before returning.  */
1916 
1917 static void
1918 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1919 {
1920   asection *sectp = info->asection;
1921   bfd *abfd;
1922   gdb_byte *buf, *retbuf;
1923   unsigned char header[4];
1924 
1925   if (info->readin)
1926     return;
1927   info->buffer = NULL;
1928   info->readin = 1;
1929 
1930   if (dwarf2_section_empty_p (info))
1931     return;
1932 
1933   abfd = sectp->owner;
1934 
1935   /* If the section has relocations, we must read it ourselves.
1936      Otherwise we attach it to the BFD.  */
1937   if ((sectp->flags & SEC_RELOC) == 0)
1938     {
1939       const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1940 
1941       /* We have to cast away const here for historical reasons.
1942 	 Fixing dwarf2read to be const-correct would be quite nice.  */
1943       info->buffer = (gdb_byte *) bytes;
1944       return;
1945     }
1946 
1947   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1948   info->buffer = buf;
1949 
1950   /* When debugging .o files, we may need to apply relocations; see
1951      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1952      We never compress sections in .o files, so we only need to
1953      try this when the section is not compressed.  */
1954   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1955   if (retbuf != NULL)
1956     {
1957       info->buffer = retbuf;
1958       return;
1959     }
1960 
1961   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1962       || bfd_bread (buf, info->size, abfd) != info->size)
1963     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1964 	   bfd_get_filename (abfd));
1965 }
1966 
1967 /* A helper function that returns the size of a section in a safe way.
1968    If you are positive that the section has been read before using the
1969    size, then it is safe to refer to the dwarf2_section_info object's
1970    "size" field directly.  In other cases, you must call this
1971    function, because for compressed sections the size field is not set
1972    correctly until the section has been read.  */
1973 
1974 static bfd_size_type
1975 dwarf2_section_size (struct objfile *objfile,
1976 		     struct dwarf2_section_info *info)
1977 {
1978   if (!info->readin)
1979     dwarf2_read_section (objfile, info);
1980   return info->size;
1981 }
1982 
1983 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1984    SECTION_NAME.  */
1985 
1986 void
1987 dwarf2_get_section_info (struct objfile *objfile,
1988                          enum dwarf2_section_enum sect,
1989                          asection **sectp, gdb_byte **bufp,
1990                          bfd_size_type *sizep)
1991 {
1992   struct dwarf2_per_objfile *data
1993     = objfile_data (objfile, dwarf2_objfile_data_key);
1994   struct dwarf2_section_info *info;
1995 
1996   /* We may see an objfile without any DWARF, in which case we just
1997      return nothing.  */
1998   if (data == NULL)
1999     {
2000       *sectp = NULL;
2001       *bufp = NULL;
2002       *sizep = 0;
2003       return;
2004     }
2005   switch (sect)
2006     {
2007     case DWARF2_DEBUG_FRAME:
2008       info = &data->frame;
2009       break;
2010     case DWARF2_EH_FRAME:
2011       info = &data->eh_frame;
2012       break;
2013     default:
2014       gdb_assert_not_reached ("unexpected section");
2015     }
2016 
2017   dwarf2_read_section (objfile, info);
2018 
2019   *sectp = info->asection;
2020   *bufp = info->buffer;
2021   *sizep = info->size;
2022 }
2023 
2024 /* A helper function to find the sections for a .dwz file.  */
2025 
2026 static void
2027 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2028 {
2029   struct dwz_file *dwz_file = arg;
2030 
2031   /* Note that we only support the standard ELF names, because .dwz
2032      is ELF-only (at the time of writing).  */
2033   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2034     {
2035       dwz_file->abbrev.asection = sectp;
2036       dwz_file->abbrev.size = bfd_get_section_size (sectp);
2037     }
2038   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2039     {
2040       dwz_file->info.asection = sectp;
2041       dwz_file->info.size = bfd_get_section_size (sectp);
2042     }
2043   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2044     {
2045       dwz_file->str.asection = sectp;
2046       dwz_file->str.size = bfd_get_section_size (sectp);
2047     }
2048   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2049     {
2050       dwz_file->line.asection = sectp;
2051       dwz_file->line.size = bfd_get_section_size (sectp);
2052     }
2053   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2054     {
2055       dwz_file->macro.asection = sectp;
2056       dwz_file->macro.size = bfd_get_section_size (sectp);
2057     }
2058   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2059     {
2060       dwz_file->gdb_index.asection = sectp;
2061       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2062     }
2063 }
2064 
2065 /* Open the separate '.dwz' debug file, if needed.  Error if the file
2066    cannot be found.  */
2067 
2068 static struct dwz_file *
2069 dwarf2_get_dwz_file (void)
2070 {
2071   bfd *abfd, *dwz_bfd;
2072   asection *section;
2073   gdb_byte *data;
2074   struct cleanup *cleanup;
2075   const char *filename;
2076   struct dwz_file *result;
2077 
2078   if (dwarf2_per_objfile->dwz_file != NULL)
2079     return dwarf2_per_objfile->dwz_file;
2080 
2081   abfd = dwarf2_per_objfile->objfile->obfd;
2082   section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2083   if (section == NULL)
2084     error (_("could not find '.gnu_debugaltlink' section"));
2085   if (!bfd_malloc_and_get_section (abfd, section, &data))
2086     error (_("could not read '.gnu_debugaltlink' section: %s"),
2087 	   bfd_errmsg (bfd_get_error ()));
2088   cleanup = make_cleanup (xfree, data);
2089 
2090   filename = data;
2091   if (!IS_ABSOLUTE_PATH (filename))
2092     {
2093       char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2094       char *rel;
2095 
2096       make_cleanup (xfree, abs);
2097       abs = ldirname (abs);
2098       make_cleanup (xfree, abs);
2099 
2100       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2101       make_cleanup (xfree, rel);
2102       filename = rel;
2103     }
2104 
2105   /* The format is just a NUL-terminated file name, followed by the
2106      build-id.  For now, though, we ignore the build-id.  */
2107   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2108   if (dwz_bfd == NULL)
2109     error (_("could not read '%s': %s"), filename,
2110 	   bfd_errmsg (bfd_get_error ()));
2111 
2112   if (!bfd_check_format (dwz_bfd, bfd_object))
2113     {
2114       gdb_bfd_unref (dwz_bfd);
2115       error (_("file '%s' was not usable: %s"), filename,
2116 	     bfd_errmsg (bfd_get_error ()));
2117     }
2118 
2119   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2120 			   struct dwz_file);
2121   result->dwz_bfd = dwz_bfd;
2122 
2123   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2124 
2125   do_cleanups (cleanup);
2126 
2127   dwarf2_per_objfile->dwz_file = result;
2128   return result;
2129 }
2130 
2131 /* DWARF quick_symbols_functions support.  */
2132 
2133 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2134    unique line tables, so we maintain a separate table of all .debug_line
2135    derived entries to support the sharing.
2136    All the quick functions need is the list of file names.  We discard the
2137    line_header when we're done and don't need to record it here.  */
2138 struct quick_file_names
2139 {
2140   /* The data used to construct the hash key.  */
2141   struct stmt_list_hash hash;
2142 
2143   /* The number of entries in file_names, real_names.  */
2144   unsigned int num_file_names;
2145 
2146   /* The file names from the line table, after being run through
2147      file_full_name.  */
2148   const char **file_names;
2149 
2150   /* The file names from the line table after being run through
2151      gdb_realpath.  These are computed lazily.  */
2152   const char **real_names;
2153 };
2154 
2155 /* When using the index (and thus not using psymtabs), each CU has an
2156    object of this type.  This is used to hold information needed by
2157    the various "quick" methods.  */
2158 struct dwarf2_per_cu_quick_data
2159 {
2160   /* The file table.  This can be NULL if there was no file table
2161      or it's currently not read in.
2162      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
2163   struct quick_file_names *file_names;
2164 
2165   /* The corresponding symbol table.  This is NULL if symbols for this
2166      CU have not yet been read.  */
2167   struct symtab *symtab;
2168 
2169   /* A temporary mark bit used when iterating over all CUs in
2170      expand_symtabs_matching.  */
2171   unsigned int mark : 1;
2172 
2173   /* True if we've tried to read the file table and found there isn't one.
2174      There will be no point in trying to read it again next time.  */
2175   unsigned int no_file_data : 1;
2176 };
2177 
2178 /* Utility hash function for a stmt_list_hash.  */
2179 
2180 static hashval_t
2181 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2182 {
2183   hashval_t v = 0;
2184 
2185   if (stmt_list_hash->dwo_unit != NULL)
2186     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2187   v += stmt_list_hash->line_offset.sect_off;
2188   return v;
2189 }
2190 
2191 /* Utility equality function for a stmt_list_hash.  */
2192 
2193 static int
2194 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2195 		    const struct stmt_list_hash *rhs)
2196 {
2197   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2198     return 0;
2199   if (lhs->dwo_unit != NULL
2200       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2201     return 0;
2202 
2203   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2204 }
2205 
2206 /* Hash function for a quick_file_names.  */
2207 
2208 static hashval_t
2209 hash_file_name_entry (const void *e)
2210 {
2211   const struct quick_file_names *file_data = e;
2212 
2213   return hash_stmt_list_entry (&file_data->hash);
2214 }
2215 
2216 /* Equality function for a quick_file_names.  */
2217 
2218 static int
2219 eq_file_name_entry (const void *a, const void *b)
2220 {
2221   const struct quick_file_names *ea = a;
2222   const struct quick_file_names *eb = b;
2223 
2224   return eq_stmt_list_entry (&ea->hash, &eb->hash);
2225 }
2226 
2227 /* Delete function for a quick_file_names.  */
2228 
2229 static void
2230 delete_file_name_entry (void *e)
2231 {
2232   struct quick_file_names *file_data = e;
2233   int i;
2234 
2235   for (i = 0; i < file_data->num_file_names; ++i)
2236     {
2237       xfree ((void*) file_data->file_names[i]);
2238       if (file_data->real_names)
2239 	xfree ((void*) file_data->real_names[i]);
2240     }
2241 
2242   /* The space for the struct itself lives on objfile_obstack,
2243      so we don't free it here.  */
2244 }
2245 
2246 /* Create a quick_file_names hash table.  */
2247 
2248 static htab_t
2249 create_quick_file_names_table (unsigned int nr_initial_entries)
2250 {
2251   return htab_create_alloc (nr_initial_entries,
2252 			    hash_file_name_entry, eq_file_name_entry,
2253 			    delete_file_name_entry, xcalloc, xfree);
2254 }
2255 
2256 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
2257    have to be created afterwards.  You should call age_cached_comp_units after
2258    processing PER_CU->CU.  dw2_setup must have been already called.  */
2259 
2260 static void
2261 load_cu (struct dwarf2_per_cu_data *per_cu)
2262 {
2263   if (per_cu->is_debug_types)
2264     load_full_type_unit (per_cu);
2265   else
2266     load_full_comp_unit (per_cu, language_minimal);
2267 
2268   gdb_assert (per_cu->cu != NULL);
2269 
2270   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2271 }
2272 
2273 /* Read in the symbols for PER_CU.  */
2274 
2275 static void
2276 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2277 {
2278   struct cleanup *back_to;
2279 
2280   /* Skip type_unit_groups, reading the type units they contain
2281      is handled elsewhere.  */
2282   if (IS_TYPE_UNIT_GROUP (per_cu))
2283     return;
2284 
2285   back_to = make_cleanup (dwarf2_release_queue, NULL);
2286 
2287   if (dwarf2_per_objfile->using_index
2288       ? per_cu->v.quick->symtab == NULL
2289       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2290     {
2291       queue_comp_unit (per_cu, language_minimal);
2292       load_cu (per_cu);
2293     }
2294 
2295   process_queue ();
2296 
2297   /* Age the cache, releasing compilation units that have not
2298      been used recently.  */
2299   age_cached_comp_units ();
2300 
2301   do_cleanups (back_to);
2302 }
2303 
2304 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
2305    the objfile from which this CU came.  Returns the resulting symbol
2306    table.  */
2307 
2308 static struct symtab *
2309 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2310 {
2311   gdb_assert (dwarf2_per_objfile->using_index);
2312   if (!per_cu->v.quick->symtab)
2313     {
2314       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2315       increment_reading_symtab ();
2316       dw2_do_instantiate_symtab (per_cu);
2317       process_cu_includes ();
2318       do_cleanups (back_to);
2319     }
2320   return per_cu->v.quick->symtab;
2321 }
2322 
2323 /* Return the CU given its index.
2324 
2325    This is intended for loops like:
2326 
2327    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328 		    + dwarf2_per_objfile->n_type_units); ++i)
2329      {
2330        struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2331 
2332        ...;
2333      }
2334 */
2335 
2336 static struct dwarf2_per_cu_data *
2337 dw2_get_cu (int index)
2338 {
2339   if (index >= dwarf2_per_objfile->n_comp_units)
2340     {
2341       index -= dwarf2_per_objfile->n_comp_units;
2342       gdb_assert (index < dwarf2_per_objfile->n_type_units);
2343       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2344     }
2345 
2346   return dwarf2_per_objfile->all_comp_units[index];
2347 }
2348 
2349 /* Return the primary CU given its index.
2350    The difference between this function and dw2_get_cu is in the handling
2351    of type units (TUs).  Here we return the type_unit_group object.
2352 
2353    This is intended for loops like:
2354 
2355    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2356 		    + dwarf2_per_objfile->n_type_unit_groups); ++i)
2357      {
2358        struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2359 
2360        ...;
2361      }
2362 */
2363 
2364 static struct dwarf2_per_cu_data *
2365 dw2_get_primary_cu (int index)
2366 {
2367   if (index >= dwarf2_per_objfile->n_comp_units)
2368     {
2369       index -= dwarf2_per_objfile->n_comp_units;
2370       gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2371       return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2372     }
2373 
2374   return dwarf2_per_objfile->all_comp_units[index];
2375 }
2376 
2377 /* A helper for create_cus_from_index that handles a given list of
2378    CUs.  */
2379 
2380 static void
2381 create_cus_from_index_list (struct objfile *objfile,
2382 			    const gdb_byte *cu_list, offset_type n_elements,
2383 			    struct dwarf2_section_info *section,
2384 			    int is_dwz,
2385 			    int base_offset)
2386 {
2387   offset_type i;
2388 
2389   for (i = 0; i < n_elements; i += 2)
2390     {
2391       struct dwarf2_per_cu_data *the_cu;
2392       ULONGEST offset, length;
2393 
2394       gdb_static_assert (sizeof (ULONGEST) >= 8);
2395       offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2396       length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2397       cu_list += 2 * 8;
2398 
2399       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2400 			       struct dwarf2_per_cu_data);
2401       the_cu->offset.sect_off = offset;
2402       the_cu->length = length;
2403       the_cu->objfile = objfile;
2404       the_cu->info_or_types_section = section;
2405       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2406 					struct dwarf2_per_cu_quick_data);
2407       the_cu->is_dwz = is_dwz;
2408       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2409     }
2410 }
2411 
2412 /* Read the CU list from the mapped index, and use it to create all
2413    the CU objects for this objfile.  */
2414 
2415 static void
2416 create_cus_from_index (struct objfile *objfile,
2417 		       const gdb_byte *cu_list, offset_type cu_list_elements,
2418 		       const gdb_byte *dwz_list, offset_type dwz_elements)
2419 {
2420   struct dwz_file *dwz;
2421 
2422   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2423   dwarf2_per_objfile->all_comp_units
2424     = obstack_alloc (&objfile->objfile_obstack,
2425 		     dwarf2_per_objfile->n_comp_units
2426 		     * sizeof (struct dwarf2_per_cu_data *));
2427 
2428   create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2429 			      &dwarf2_per_objfile->info, 0, 0);
2430 
2431   if (dwz_elements == 0)
2432     return;
2433 
2434   dwz = dwarf2_get_dwz_file ();
2435   create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2436 			      cu_list_elements / 2);
2437 }
2438 
2439 /* Create the signatured type hash table from the index.  */
2440 
2441 static void
2442 create_signatured_type_table_from_index (struct objfile *objfile,
2443 					 struct dwarf2_section_info *section,
2444 					 const gdb_byte *bytes,
2445 					 offset_type elements)
2446 {
2447   offset_type i;
2448   htab_t sig_types_hash;
2449 
2450   dwarf2_per_objfile->n_type_units = elements / 3;
2451   dwarf2_per_objfile->all_type_units
2452     = obstack_alloc (&objfile->objfile_obstack,
2453 		     dwarf2_per_objfile->n_type_units
2454 		     * sizeof (struct signatured_type *));
2455 
2456   sig_types_hash = allocate_signatured_type_table (objfile);
2457 
2458   for (i = 0; i < elements; i += 3)
2459     {
2460       struct signatured_type *sig_type;
2461       ULONGEST offset, type_offset_in_tu, signature;
2462       void **slot;
2463 
2464       gdb_static_assert (sizeof (ULONGEST) >= 8);
2465       offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2466       type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2467 						    BFD_ENDIAN_LITTLE);
2468       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2469       bytes += 3 * 8;
2470 
2471       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2472 				 struct signatured_type);
2473       sig_type->signature = signature;
2474       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2475       sig_type->per_cu.is_debug_types = 1;
2476       sig_type->per_cu.info_or_types_section = section;
2477       sig_type->per_cu.offset.sect_off = offset;
2478       sig_type->per_cu.objfile = objfile;
2479       sig_type->per_cu.v.quick
2480 	= OBSTACK_ZALLOC (&objfile->objfile_obstack,
2481 			  struct dwarf2_per_cu_quick_data);
2482 
2483       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2484       *slot = sig_type;
2485 
2486       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2487     }
2488 
2489   dwarf2_per_objfile->signatured_types = sig_types_hash;
2490 }
2491 
2492 /* Read the address map data from the mapped index, and use it to
2493    populate the objfile's psymtabs_addrmap.  */
2494 
2495 static void
2496 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2497 {
2498   const gdb_byte *iter, *end;
2499   struct obstack temp_obstack;
2500   struct addrmap *mutable_map;
2501   struct cleanup *cleanup;
2502   CORE_ADDR baseaddr;
2503 
2504   obstack_init (&temp_obstack);
2505   cleanup = make_cleanup_obstack_free (&temp_obstack);
2506   mutable_map = addrmap_create_mutable (&temp_obstack);
2507 
2508   iter = index->address_table;
2509   end = iter + index->address_table_size;
2510 
2511   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2512 
2513   while (iter < end)
2514     {
2515       ULONGEST hi, lo, cu_index;
2516       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2517       iter += 8;
2518       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2519       iter += 8;
2520       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2521       iter += 4;
2522 
2523       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2524 			 dw2_get_cu (cu_index));
2525     }
2526 
2527   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2528 						    &objfile->objfile_obstack);
2529   do_cleanups (cleanup);
2530 }
2531 
2532 /* The hash function for strings in the mapped index.  This is the same as
2533    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2534    implementation.  This is necessary because the hash function is tied to the
2535    format of the mapped index file.  The hash values do not have to match with
2536    SYMBOL_HASH_NEXT.
2537 
2538    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2539 
2540 static hashval_t
2541 mapped_index_string_hash (int index_version, const void *p)
2542 {
2543   const unsigned char *str = (const unsigned char *) p;
2544   hashval_t r = 0;
2545   unsigned char c;
2546 
2547   while ((c = *str++) != 0)
2548     {
2549       if (index_version >= 5)
2550 	c = tolower (c);
2551       r = r * 67 + c - 113;
2552     }
2553 
2554   return r;
2555 }
2556 
2557 /* Find a slot in the mapped index INDEX for the object named NAME.
2558    If NAME is found, set *VEC_OUT to point to the CU vector in the
2559    constant pool and return 1.  If NAME cannot be found, return 0.  */
2560 
2561 static int
2562 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2563 			  offset_type **vec_out)
2564 {
2565   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2566   offset_type hash;
2567   offset_type slot, step;
2568   int (*cmp) (const char *, const char *);
2569 
2570   if (current_language->la_language == language_cplus
2571       || current_language->la_language == language_java
2572       || current_language->la_language == language_fortran)
2573     {
2574       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2575 	 not contain any.  */
2576       const char *paren = strchr (name, '(');
2577 
2578       if (paren)
2579 	{
2580 	  char *dup;
2581 
2582 	  dup = xmalloc (paren - name + 1);
2583 	  memcpy (dup, name, paren - name);
2584 	  dup[paren - name] = 0;
2585 
2586 	  make_cleanup (xfree, dup);
2587 	  name = dup;
2588 	}
2589     }
2590 
2591   /* Index version 4 did not support case insensitive searches.  But the
2592      indices for case insensitive languages are built in lowercase, therefore
2593      simulate our NAME being searched is also lowercased.  */
2594   hash = mapped_index_string_hash ((index->version == 4
2595                                     && case_sensitivity == case_sensitive_off
2596 				    ? 5 : index->version),
2597 				   name);
2598 
2599   slot = hash & (index->symbol_table_slots - 1);
2600   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2601   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2602 
2603   for (;;)
2604     {
2605       /* Convert a slot number to an offset into the table.  */
2606       offset_type i = 2 * slot;
2607       const char *str;
2608       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2609 	{
2610 	  do_cleanups (back_to);
2611 	  return 0;
2612 	}
2613 
2614       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2615       if (!cmp (name, str))
2616 	{
2617 	  *vec_out = (offset_type *) (index->constant_pool
2618 				      + MAYBE_SWAP (index->symbol_table[i + 1]));
2619 	  do_cleanups (back_to);
2620 	  return 1;
2621 	}
2622 
2623       slot = (slot + step) & (index->symbol_table_slots - 1);
2624     }
2625 }
2626 
2627 /* A helper function that reads the .gdb_index from SECTION and fills
2628    in MAP.  FILENAME is the name of the file containing the section;
2629    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
2630    ok to use deprecated sections.
2631 
2632    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2633    out parameters that are filled in with information about the CU and
2634    TU lists in the section.
2635 
2636    Returns 1 if all went well, 0 otherwise.  */
2637 
2638 static int
2639 read_index_from_section (struct objfile *objfile,
2640 			 const char *filename,
2641 			 int deprecated_ok,
2642 			 struct dwarf2_section_info *section,
2643 			 struct mapped_index *map,
2644 			 const gdb_byte **cu_list,
2645 			 offset_type *cu_list_elements,
2646 			 const gdb_byte **types_list,
2647 			 offset_type *types_list_elements)
2648 {
2649   char *addr;
2650   offset_type version;
2651   offset_type *metadata;
2652   int i;
2653 
2654   if (dwarf2_section_empty_p (section))
2655     return 0;
2656 
2657   /* Older elfutils strip versions could keep the section in the main
2658      executable while splitting it for the separate debug info file.  */
2659   if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2660     return 0;
2661 
2662   dwarf2_read_section (objfile, section);
2663 
2664   addr = section->buffer;
2665   /* Version check.  */
2666   version = MAYBE_SWAP (*(offset_type *) addr);
2667   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2668      causes the index to behave very poorly for certain requests.  Version 3
2669      contained incomplete addrmap.  So, it seems better to just ignore such
2670      indices.  */
2671   if (version < 4)
2672     {
2673       static int warning_printed = 0;
2674       if (!warning_printed)
2675 	{
2676 	  warning (_("Skipping obsolete .gdb_index section in %s."),
2677 		   filename);
2678 	  warning_printed = 1;
2679 	}
2680       return 0;
2681     }
2682   /* Index version 4 uses a different hash function than index version
2683      5 and later.
2684 
2685      Versions earlier than 6 did not emit psymbols for inlined
2686      functions.  Using these files will cause GDB not to be able to
2687      set breakpoints on inlined functions by name, so we ignore these
2688      indices unless the user has done
2689      "set use-deprecated-index-sections on".  */
2690   if (version < 6 && !deprecated_ok)
2691     {
2692       static int warning_printed = 0;
2693       if (!warning_printed)
2694 	{
2695 	  warning (_("\
2696 Skipping deprecated .gdb_index section in %s.\n\
2697 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2698 to use the section anyway."),
2699 		   filename);
2700 	  warning_printed = 1;
2701 	}
2702       return 0;
2703     }
2704   /* Version 7 indices generated by gold refer to the CU for a symbol instead
2705      of the TU (for symbols coming from TUs).  It's just a performance bug, and
2706      we can't distinguish gdb-generated indices from gold-generated ones, so
2707      nothing to do here.  */
2708 
2709   /* Indexes with higher version than the one supported by GDB may be no
2710      longer backward compatible.  */
2711   if (version > 8)
2712     return 0;
2713 
2714   map->version = version;
2715   map->total_size = section->size;
2716 
2717   metadata = (offset_type *) (addr + sizeof (offset_type));
2718 
2719   i = 0;
2720   *cu_list = addr + MAYBE_SWAP (metadata[i]);
2721   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2722 		       / 8);
2723   ++i;
2724 
2725   *types_list = addr + MAYBE_SWAP (metadata[i]);
2726   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2727 			   - MAYBE_SWAP (metadata[i]))
2728 			  / 8);
2729   ++i;
2730 
2731   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2732   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2733 			     - MAYBE_SWAP (metadata[i]));
2734   ++i;
2735 
2736   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2737   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2738 			      - MAYBE_SWAP (metadata[i]))
2739 			     / (2 * sizeof (offset_type)));
2740   ++i;
2741 
2742   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2743 
2744   return 1;
2745 }
2746 
2747 
2748 /* Read the index file.  If everything went ok, initialize the "quick"
2749    elements of all the CUs and return 1.  Otherwise, return 0.  */
2750 
2751 static int
2752 dwarf2_read_index (struct objfile *objfile)
2753 {
2754   struct mapped_index local_map, *map;
2755   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2756   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2757 
2758   if (!read_index_from_section (objfile, objfile->name,
2759 				use_deprecated_index_sections,
2760 				&dwarf2_per_objfile->gdb_index, &local_map,
2761 				&cu_list, &cu_list_elements,
2762 				&types_list, &types_list_elements))
2763     return 0;
2764 
2765   /* Don't use the index if it's empty.  */
2766   if (local_map.symbol_table_slots == 0)
2767     return 0;
2768 
2769   /* If there is a .dwz file, read it so we can get its CU list as
2770      well.  */
2771   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2772     {
2773       struct dwz_file *dwz = dwarf2_get_dwz_file ();
2774       struct mapped_index dwz_map;
2775       const gdb_byte *dwz_types_ignore;
2776       offset_type dwz_types_elements_ignore;
2777 
2778       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2779 				    1,
2780 				    &dwz->gdb_index, &dwz_map,
2781 				    &dwz_list, &dwz_list_elements,
2782 				    &dwz_types_ignore,
2783 				    &dwz_types_elements_ignore))
2784 	{
2785 	  warning (_("could not read '.gdb_index' section from %s; skipping"),
2786 		   bfd_get_filename (dwz->dwz_bfd));
2787 	  return 0;
2788 	}
2789     }
2790 
2791   create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2792 			 dwz_list_elements);
2793 
2794   if (types_list_elements)
2795     {
2796       struct dwarf2_section_info *section;
2797 
2798       /* We can only handle a single .debug_types when we have an
2799 	 index.  */
2800       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2801 	return 0;
2802 
2803       section = VEC_index (dwarf2_section_info_def,
2804 			   dwarf2_per_objfile->types, 0);
2805 
2806       create_signatured_type_table_from_index (objfile, section, types_list,
2807 					       types_list_elements);
2808     }
2809 
2810   create_addrmap_from_index (objfile, &local_map);
2811 
2812   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2813   *map = local_map;
2814 
2815   dwarf2_per_objfile->index_table = map;
2816   dwarf2_per_objfile->using_index = 1;
2817   dwarf2_per_objfile->quick_file_names_table =
2818     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2819 
2820   return 1;
2821 }
2822 
2823 /* A helper for the "quick" functions which sets the global
2824    dwarf2_per_objfile according to OBJFILE.  */
2825 
2826 static void
2827 dw2_setup (struct objfile *objfile)
2828 {
2829   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2830   gdb_assert (dwarf2_per_objfile);
2831 }
2832 
2833 /* die_reader_func for dw2_get_file_names.  */
2834 
2835 static void
2836 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2837 			   gdb_byte *info_ptr,
2838 			   struct die_info *comp_unit_die,
2839 			   int has_children,
2840 			   void *data)
2841 {
2842   struct dwarf2_cu *cu = reader->cu;
2843   struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2844   struct objfile *objfile = dwarf2_per_objfile->objfile;
2845   struct dwarf2_per_cu_data *lh_cu;
2846   struct line_header *lh;
2847   struct attribute *attr;
2848   int i;
2849   const char *name, *comp_dir;
2850   void **slot;
2851   struct quick_file_names *qfn;
2852   unsigned int line_offset;
2853 
2854   /* Our callers never want to match partial units -- instead they
2855      will match the enclosing full CU.  */
2856   if (comp_unit_die->tag == DW_TAG_partial_unit)
2857     {
2858       this_cu->v.quick->no_file_data = 1;
2859       return;
2860     }
2861 
2862   /* If we're reading the line header for TUs, store it in the "per_cu"
2863      for tu_group.  */
2864   if (this_cu->is_debug_types)
2865     {
2866       struct type_unit_group *tu_group = data;
2867 
2868       gdb_assert (tu_group != NULL);
2869       lh_cu = &tu_group->per_cu;
2870     }
2871   else
2872     lh_cu = this_cu;
2873 
2874   lh = NULL;
2875   slot = NULL;
2876   line_offset = 0;
2877 
2878   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2879   if (attr)
2880     {
2881       struct quick_file_names find_entry;
2882 
2883       line_offset = DW_UNSND (attr);
2884 
2885       /* We may have already read in this line header (TU line header sharing).
2886 	 If we have we're done.  */
2887       find_entry.hash.dwo_unit = cu->dwo_unit;
2888       find_entry.hash.line_offset.sect_off = line_offset;
2889       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2890 			     &find_entry, INSERT);
2891       if (*slot != NULL)
2892 	{
2893 	  lh_cu->v.quick->file_names = *slot;
2894 	  return;
2895 	}
2896 
2897       lh = dwarf_decode_line_header (line_offset, cu);
2898     }
2899   if (lh == NULL)
2900     {
2901       lh_cu->v.quick->no_file_data = 1;
2902       return;
2903     }
2904 
2905   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2906   qfn->hash.dwo_unit = cu->dwo_unit;
2907   qfn->hash.line_offset.sect_off = line_offset;
2908   gdb_assert (slot != NULL);
2909   *slot = qfn;
2910 
2911   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2912 
2913   qfn->num_file_names = lh->num_file_names;
2914   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2915 				   lh->num_file_names * sizeof (char *));
2916   for (i = 0; i < lh->num_file_names; ++i)
2917     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2918   qfn->real_names = NULL;
2919 
2920   free_line_header (lh);
2921 
2922   lh_cu->v.quick->file_names = qfn;
2923 }
2924 
2925 /* A helper for the "quick" functions which attempts to read the line
2926    table for THIS_CU.  */
2927 
2928 static struct quick_file_names *
2929 dw2_get_file_names (struct objfile *objfile,
2930 		    struct dwarf2_per_cu_data *this_cu)
2931 {
2932   /* For TUs this should only be called on the parent group.  */
2933   if (this_cu->is_debug_types)
2934     gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2935 
2936   if (this_cu->v.quick->file_names != NULL)
2937     return this_cu->v.quick->file_names;
2938   /* If we know there is no line data, no point in looking again.  */
2939   if (this_cu->v.quick->no_file_data)
2940     return NULL;
2941 
2942   /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2943      in the stub for CUs, there's is no need to lookup the DWO file.
2944      However, that's not the case for TUs where DW_AT_stmt_list lives in the
2945      DWO file.  */
2946   if (this_cu->is_debug_types)
2947     {
2948       struct type_unit_group *tu_group = this_cu->type_unit_group;
2949 
2950       init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2951 			       dw2_get_file_names_reader, tu_group);
2952     }
2953   else
2954     init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2955 
2956   if (this_cu->v.quick->no_file_data)
2957     return NULL;
2958   return this_cu->v.quick->file_names;
2959 }
2960 
2961 /* A helper for the "quick" functions which computes and caches the
2962    real path for a given file name from the line table.  */
2963 
2964 static const char *
2965 dw2_get_real_path (struct objfile *objfile,
2966 		   struct quick_file_names *qfn, int index)
2967 {
2968   if (qfn->real_names == NULL)
2969     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2970 				      qfn->num_file_names, sizeof (char *));
2971 
2972   if (qfn->real_names[index] == NULL)
2973     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2974 
2975   return qfn->real_names[index];
2976 }
2977 
2978 static struct symtab *
2979 dw2_find_last_source_symtab (struct objfile *objfile)
2980 {
2981   int index;
2982 
2983   dw2_setup (objfile);
2984   index = dwarf2_per_objfile->n_comp_units - 1;
2985   return dw2_instantiate_symtab (dw2_get_cu (index));
2986 }
2987 
2988 /* Traversal function for dw2_forget_cached_source_info.  */
2989 
2990 static int
2991 dw2_free_cached_file_names (void **slot, void *info)
2992 {
2993   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2994 
2995   if (file_data->real_names)
2996     {
2997       int i;
2998 
2999       for (i = 0; i < file_data->num_file_names; ++i)
3000 	{
3001 	  xfree ((void*) file_data->real_names[i]);
3002 	  file_data->real_names[i] = NULL;
3003 	}
3004     }
3005 
3006   return 1;
3007 }
3008 
3009 static void
3010 dw2_forget_cached_source_info (struct objfile *objfile)
3011 {
3012   dw2_setup (objfile);
3013 
3014   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3015 			  dw2_free_cached_file_names, NULL);
3016 }
3017 
3018 /* Helper function for dw2_map_symtabs_matching_filename that expands
3019    the symtabs and calls the iterator.  */
3020 
3021 static int
3022 dw2_map_expand_apply (struct objfile *objfile,
3023 		      struct dwarf2_per_cu_data *per_cu,
3024 		      const char *name, const char *real_path,
3025 		      int (*callback) (struct symtab *, void *),
3026 		      void *data)
3027 {
3028   struct symtab *last_made = objfile->symtabs;
3029 
3030   /* Don't visit already-expanded CUs.  */
3031   if (per_cu->v.quick->symtab)
3032     return 0;
3033 
3034   /* This may expand more than one symtab, and we want to iterate over
3035      all of them.  */
3036   dw2_instantiate_symtab (per_cu);
3037 
3038   return iterate_over_some_symtabs (name, real_path, callback, data,
3039 				    objfile->symtabs, last_made);
3040 }
3041 
3042 /* Implementation of the map_symtabs_matching_filename method.  */
3043 
3044 static int
3045 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3046 				   const char *real_path,
3047 				   int (*callback) (struct symtab *, void *),
3048 				   void *data)
3049 {
3050   int i;
3051   const char *name_basename = lbasename (name);
3052 
3053   dw2_setup (objfile);
3054 
3055   /* The rule is CUs specify all the files, including those used by
3056      any TU, so there's no need to scan TUs here.  */
3057 
3058   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3059     {
3060       int j;
3061       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3062       struct quick_file_names *file_data;
3063 
3064       /* We only need to look at symtabs not already expanded.  */
3065       if (per_cu->v.quick->symtab)
3066 	continue;
3067 
3068       file_data = dw2_get_file_names (objfile, per_cu);
3069       if (file_data == NULL)
3070 	continue;
3071 
3072       for (j = 0; j < file_data->num_file_names; ++j)
3073 	{
3074 	  const char *this_name = file_data->file_names[j];
3075 	  const char *this_real_name;
3076 
3077 	  if (compare_filenames_for_search (this_name, name))
3078 	    {
3079 	      if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3080 					callback, data))
3081 		return 1;
3082 	    }
3083 
3084 	  /* Before we invoke realpath, which can get expensive when many
3085 	     files are involved, do a quick comparison of the basenames.  */
3086 	  if (! basenames_may_differ
3087 	      && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3088 	    continue;
3089 
3090 	  this_real_name = dw2_get_real_path (objfile, file_data, j);
3091 	  if (compare_filenames_for_search (this_real_name, name))
3092 	    {
3093 	      if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3094 					callback, data))
3095 		return 1;
3096 	    }
3097 
3098 	  if (real_path != NULL)
3099 	    {
3100 	      gdb_assert (IS_ABSOLUTE_PATH (real_path));
3101 	      gdb_assert (IS_ABSOLUTE_PATH (name));
3102 	      if (this_real_name != NULL
3103 		  && FILENAME_CMP (real_path, this_real_name) == 0)
3104 		{
3105 		  if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3106 					    callback, data))
3107 		    return 1;
3108 		}
3109 	    }
3110 	}
3111     }
3112 
3113   return 0;
3114 }
3115 
3116 /* Struct used to manage iterating over all CUs looking for a symbol.  */
3117 
3118 struct dw2_symtab_iterator
3119 {
3120   /* The internalized form of .gdb_index.  */
3121   struct mapped_index *index;
3122   /* If non-zero, only look for symbols that match BLOCK_INDEX.  */
3123   int want_specific_block;
3124   /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3125      Unused if !WANT_SPECIFIC_BLOCK.  */
3126   int block_index;
3127   /* The kind of symbol we're looking for.  */
3128   domain_enum domain;
3129   /* The list of CUs from the index entry of the symbol,
3130      or NULL if not found.  */
3131   offset_type *vec;
3132   /* The next element in VEC to look at.  */
3133   int next;
3134   /* The number of elements in VEC, or zero if there is no match.  */
3135   int length;
3136 };
3137 
3138 /* Initialize the index symtab iterator ITER.
3139    If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3140    in block BLOCK_INDEX.  Otherwise BLOCK_INDEX is ignored.  */
3141 
3142 static void
3143 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3144 		      struct mapped_index *index,
3145 		      int want_specific_block,
3146 		      int block_index,
3147 		      domain_enum domain,
3148 		      const char *name)
3149 {
3150   iter->index = index;
3151   iter->want_specific_block = want_specific_block;
3152   iter->block_index = block_index;
3153   iter->domain = domain;
3154   iter->next = 0;
3155 
3156   if (find_slot_in_mapped_hash (index, name, &iter->vec))
3157     iter->length = MAYBE_SWAP (*iter->vec);
3158   else
3159     {
3160       iter->vec = NULL;
3161       iter->length = 0;
3162     }
3163 }
3164 
3165 /* Return the next matching CU or NULL if there are no more.  */
3166 
3167 static struct dwarf2_per_cu_data *
3168 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3169 {
3170   for ( ; iter->next < iter->length; ++iter->next)
3171     {
3172       offset_type cu_index_and_attrs =
3173 	MAYBE_SWAP (iter->vec[iter->next + 1]);
3174       offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3175       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3176       int want_static = iter->block_index != GLOBAL_BLOCK;
3177       /* This value is only valid for index versions >= 7.  */
3178       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3179       gdb_index_symbol_kind symbol_kind =
3180 	GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3181       /* Only check the symbol attributes if they're present.
3182 	 Indices prior to version 7 don't record them,
3183 	 and indices >= 7 may elide them for certain symbols
3184 	 (gold does this).  */
3185       int attrs_valid =
3186 	(iter->index->version >= 7
3187 	 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3188 
3189       /* Skip if already read in.  */
3190       if (per_cu->v.quick->symtab)
3191 	continue;
3192 
3193       if (attrs_valid
3194 	  && iter->want_specific_block
3195 	  && want_static != is_static)
3196 	continue;
3197 
3198       /* Only check the symbol's kind if it has one.  */
3199       if (attrs_valid)
3200 	{
3201 	  switch (iter->domain)
3202 	    {
3203 	    case VAR_DOMAIN:
3204 	      if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3205 		  && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3206 		  /* Some types are also in VAR_DOMAIN.  */
3207 		  && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3208 		continue;
3209 	      break;
3210 	    case STRUCT_DOMAIN:
3211 	      if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3212 		continue;
3213 	      break;
3214 	    case LABEL_DOMAIN:
3215 	      if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3216 		continue;
3217 	      break;
3218 	    default:
3219 	      break;
3220 	    }
3221 	}
3222 
3223       ++iter->next;
3224       return per_cu;
3225     }
3226 
3227   return NULL;
3228 }
3229 
3230 static struct symtab *
3231 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3232 		   const char *name, domain_enum domain)
3233 {
3234   struct symtab *stab_best = NULL;
3235   struct mapped_index *index;
3236 
3237   dw2_setup (objfile);
3238 
3239   index = dwarf2_per_objfile->index_table;
3240 
3241   /* index is NULL if OBJF_READNOW.  */
3242   if (index)
3243     {
3244       struct dw2_symtab_iterator iter;
3245       struct dwarf2_per_cu_data *per_cu;
3246 
3247       dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3248 
3249       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3250 	{
3251 	  struct symbol *sym = NULL;
3252 	  struct symtab *stab = dw2_instantiate_symtab (per_cu);
3253 
3254 	  /* Some caution must be observed with overloaded functions
3255 	     and methods, since the index will not contain any overload
3256 	     information (but NAME might contain it).  */
3257 	  if (stab->primary)
3258 	    {
3259 	      struct blockvector *bv = BLOCKVECTOR (stab);
3260 	      struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3261 
3262 	      sym = lookup_block_symbol (block, name, domain);
3263 	    }
3264 
3265 	  if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3266 	    {
3267 	      if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3268 		return stab;
3269 
3270 	      stab_best = stab;
3271 	    }
3272 
3273 	  /* Keep looking through other CUs.  */
3274 	}
3275     }
3276 
3277   return stab_best;
3278 }
3279 
3280 static void
3281 dw2_print_stats (struct objfile *objfile)
3282 {
3283   int i, count;
3284 
3285   dw2_setup (objfile);
3286   count = 0;
3287   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3288 		   + dwarf2_per_objfile->n_type_units); ++i)
3289     {
3290       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3291 
3292       if (!per_cu->v.quick->symtab)
3293 	++count;
3294     }
3295   printf_filtered (_("  Number of unread CUs: %d\n"), count);
3296 }
3297 
3298 static void
3299 dw2_dump (struct objfile *objfile)
3300 {
3301   /* Nothing worth printing.  */
3302 }
3303 
3304 static void
3305 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3306 	      struct section_offsets *delta)
3307 {
3308   /* There's nothing to relocate here.  */
3309 }
3310 
3311 static void
3312 dw2_expand_symtabs_for_function (struct objfile *objfile,
3313 				 const char *func_name)
3314 {
3315   struct mapped_index *index;
3316 
3317   dw2_setup (objfile);
3318 
3319   index = dwarf2_per_objfile->index_table;
3320 
3321   /* index is NULL if OBJF_READNOW.  */
3322   if (index)
3323     {
3324       struct dw2_symtab_iterator iter;
3325       struct dwarf2_per_cu_data *per_cu;
3326 
3327       /* Note: It doesn't matter what we pass for block_index here.  */
3328       dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3329 			    func_name);
3330 
3331       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3332 	dw2_instantiate_symtab (per_cu);
3333     }
3334 }
3335 
3336 static void
3337 dw2_expand_all_symtabs (struct objfile *objfile)
3338 {
3339   int i;
3340 
3341   dw2_setup (objfile);
3342 
3343   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3344 		   + dwarf2_per_objfile->n_type_units); ++i)
3345     {
3346       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3347 
3348       dw2_instantiate_symtab (per_cu);
3349     }
3350 }
3351 
3352 static void
3353 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3354 				  const char *fullname)
3355 {
3356   int i;
3357 
3358   dw2_setup (objfile);
3359 
3360   /* We don't need to consider type units here.
3361      This is only called for examining code, e.g. expand_line_sal.
3362      There can be an order of magnitude (or more) more type units
3363      than comp units, and we avoid them if we can.  */
3364 
3365   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3366     {
3367       int j;
3368       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3369       struct quick_file_names *file_data;
3370 
3371       /* We only need to look at symtabs not already expanded.  */
3372       if (per_cu->v.quick->symtab)
3373 	continue;
3374 
3375       file_data = dw2_get_file_names (objfile, per_cu);
3376       if (file_data == NULL)
3377 	continue;
3378 
3379       for (j = 0; j < file_data->num_file_names; ++j)
3380 	{
3381 	  const char *this_fullname = file_data->file_names[j];
3382 
3383 	  if (filename_cmp (this_fullname, fullname) == 0)
3384 	    {
3385 	      dw2_instantiate_symtab (per_cu);
3386 	      break;
3387 	    }
3388 	}
3389     }
3390 }
3391 
3392 /* A helper function for dw2_find_symbol_file that finds the primary
3393    file name for a given CU.  This is a die_reader_func.  */
3394 
3395 static void
3396 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3397 				 gdb_byte *info_ptr,
3398 				 struct die_info *comp_unit_die,
3399 				 int has_children,
3400 				 void *data)
3401 {
3402   const char **result_ptr = data;
3403   struct dwarf2_cu *cu = reader->cu;
3404   struct attribute *attr;
3405 
3406   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3407   if (attr == NULL)
3408     *result_ptr = NULL;
3409   else
3410     *result_ptr = DW_STRING (attr);
3411 }
3412 
3413 static const char *
3414 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3415 {
3416   struct dwarf2_per_cu_data *per_cu;
3417   offset_type *vec;
3418   const char *filename;
3419 
3420   dw2_setup (objfile);
3421 
3422   /* index_table is NULL if OBJF_READNOW.  */
3423   if (!dwarf2_per_objfile->index_table)
3424     {
3425       struct symtab *s;
3426 
3427       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3428 	{
3429 	  struct blockvector *bv = BLOCKVECTOR (s);
3430 	  const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3431 	  struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3432 
3433 	  if (sym)
3434 	    {
3435 	      /* Only file extension of returned filename is recognized.  */
3436 	      return SYMBOL_SYMTAB (sym)->filename;
3437 	    }
3438 	}
3439       return NULL;
3440     }
3441 
3442   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3443 				 name, &vec))
3444     return NULL;
3445 
3446   /* Note that this just looks at the very first one named NAME -- but
3447      actually we are looking for a function.  find_main_filename
3448      should be rewritten so that it doesn't require a custom hook.  It
3449      could just use the ordinary symbol tables.  */
3450   /* vec[0] is the length, which must always be >0.  */
3451   per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3452 
3453   if (per_cu->v.quick->symtab != NULL)
3454     {
3455       /* Only file extension of returned filename is recognized.  */
3456       return per_cu->v.quick->symtab->filename;
3457     }
3458 
3459   init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3460 			   dw2_get_primary_filename_reader, &filename);
3461 
3462   /* Only file extension of returned filename is recognized.  */
3463   return filename;
3464 }
3465 
3466 static void
3467 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3468 			  struct objfile *objfile, int global,
3469 			  int (*callback) (struct block *,
3470 					   struct symbol *, void *),
3471 			  void *data, symbol_compare_ftype *match,
3472 			  symbol_compare_ftype *ordered_compare)
3473 {
3474   /* Currently unimplemented; used for Ada.  The function can be called if the
3475      current language is Ada for a non-Ada objfile using GNU index.  As Ada
3476      does not look for non-Ada symbols this function should just return.  */
3477 }
3478 
3479 static void
3480 dw2_expand_symtabs_matching
3481   (struct objfile *objfile,
3482    int (*file_matcher) (const char *, void *, int basenames),
3483    int (*name_matcher) (const char *, void *),
3484    enum search_domain kind,
3485    void *data)
3486 {
3487   int i;
3488   offset_type iter;
3489   struct mapped_index *index;
3490 
3491   dw2_setup (objfile);
3492 
3493   /* index_table is NULL if OBJF_READNOW.  */
3494   if (!dwarf2_per_objfile->index_table)
3495     return;
3496   index = dwarf2_per_objfile->index_table;
3497 
3498   if (file_matcher != NULL)
3499     {
3500       struct cleanup *cleanup;
3501       htab_t visited_found, visited_not_found;
3502 
3503       visited_found = htab_create_alloc (10,
3504 					 htab_hash_pointer, htab_eq_pointer,
3505 					 NULL, xcalloc, xfree);
3506       cleanup = make_cleanup_htab_delete (visited_found);
3507       visited_not_found = htab_create_alloc (10,
3508 					     htab_hash_pointer, htab_eq_pointer,
3509 					     NULL, xcalloc, xfree);
3510       make_cleanup_htab_delete (visited_not_found);
3511 
3512       /* The rule is CUs specify all the files, including those used by
3513 	 any TU, so there's no need to scan TUs here.  */
3514 
3515       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3516 	{
3517 	  int j;
3518 	  struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3519 	  struct quick_file_names *file_data;
3520 	  void **slot;
3521 
3522 	  per_cu->v.quick->mark = 0;
3523 
3524 	  /* We only need to look at symtabs not already expanded.  */
3525 	  if (per_cu->v.quick->symtab)
3526 	    continue;
3527 
3528 	  file_data = dw2_get_file_names (objfile, per_cu);
3529 	  if (file_data == NULL)
3530 	    continue;
3531 
3532 	  if (htab_find (visited_not_found, file_data) != NULL)
3533 	    continue;
3534 	  else if (htab_find (visited_found, file_data) != NULL)
3535 	    {
3536 	      per_cu->v.quick->mark = 1;
3537 	      continue;
3538 	    }
3539 
3540 	  for (j = 0; j < file_data->num_file_names; ++j)
3541 	    {
3542 	      const char *this_real_name;
3543 
3544 	      if (file_matcher (file_data->file_names[j], data, 0))
3545 		{
3546 		  per_cu->v.quick->mark = 1;
3547 		  break;
3548 		}
3549 
3550 	      /* Before we invoke realpath, which can get expensive when many
3551 		 files are involved, do a quick comparison of the basenames.  */
3552 	      if (!basenames_may_differ
3553 		  && !file_matcher (lbasename (file_data->file_names[j]),
3554 				    data, 1))
3555 		continue;
3556 
3557 	      this_real_name = dw2_get_real_path (objfile, file_data, j);
3558 	      if (file_matcher (this_real_name, data, 0))
3559 		{
3560 		  per_cu->v.quick->mark = 1;
3561 		  break;
3562 		}
3563 	    }
3564 
3565 	  slot = htab_find_slot (per_cu->v.quick->mark
3566 				 ? visited_found
3567 				 : visited_not_found,
3568 				 file_data, INSERT);
3569 	  *slot = file_data;
3570 	}
3571 
3572       do_cleanups (cleanup);
3573     }
3574 
3575   for (iter = 0; iter < index->symbol_table_slots; ++iter)
3576     {
3577       offset_type idx = 2 * iter;
3578       const char *name;
3579       offset_type *vec, vec_len, vec_idx;
3580 
3581       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3582 	continue;
3583 
3584       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3585 
3586       if (! (*name_matcher) (name, data))
3587 	continue;
3588 
3589       /* The name was matched, now expand corresponding CUs that were
3590 	 marked.  */
3591       vec = (offset_type *) (index->constant_pool
3592 			     + MAYBE_SWAP (index->symbol_table[idx + 1]));
3593       vec_len = MAYBE_SWAP (vec[0]);
3594       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3595 	{
3596 	  struct dwarf2_per_cu_data *per_cu;
3597 	  offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3598 	  gdb_index_symbol_kind symbol_kind =
3599 	    GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3600 	  int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3601 
3602 	  /* Don't crash on bad data.  */
3603 	  if (cu_index >= (dwarf2_per_objfile->n_comp_units
3604 			   + dwarf2_per_objfile->n_type_units))
3605 	    continue;
3606 
3607 	  /* Only check the symbol's kind if it has one.
3608 	     Indices prior to version 7 don't record it.  */
3609 	  if (index->version >= 7)
3610 	    {
3611 	      switch (kind)
3612 		{
3613 		case VARIABLES_DOMAIN:
3614 		  if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3615 		    continue;
3616 		  break;
3617 		case FUNCTIONS_DOMAIN:
3618 		  if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3619 		    continue;
3620 		  break;
3621 		case TYPES_DOMAIN:
3622 		  if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3623 		    continue;
3624 		  break;
3625 		default:
3626 		  break;
3627 		}
3628 	    }
3629 
3630 	  per_cu = dw2_get_cu (cu_index);
3631 	  if (file_matcher == NULL || per_cu->v.quick->mark)
3632 	    dw2_instantiate_symtab (per_cu);
3633 	}
3634     }
3635 }
3636 
3637 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3638    symtab.  */
3639 
3640 static struct symtab *
3641 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3642 {
3643   int i;
3644 
3645   if (BLOCKVECTOR (symtab) != NULL
3646       && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3647     return symtab;
3648 
3649   if (symtab->includes == NULL)
3650     return NULL;
3651 
3652   for (i = 0; symtab->includes[i]; ++i)
3653     {
3654       struct symtab *s = symtab->includes[i];
3655 
3656       s = recursively_find_pc_sect_symtab (s, pc);
3657       if (s != NULL)
3658 	return s;
3659     }
3660 
3661   return NULL;
3662 }
3663 
3664 static struct symtab *
3665 dw2_find_pc_sect_symtab (struct objfile *objfile,
3666 			 struct minimal_symbol *msymbol,
3667 			 CORE_ADDR pc,
3668 			 struct obj_section *section,
3669 			 int warn_if_readin)
3670 {
3671   struct dwarf2_per_cu_data *data;
3672   struct symtab *result;
3673 
3674   dw2_setup (objfile);
3675 
3676   if (!objfile->psymtabs_addrmap)
3677     return NULL;
3678 
3679   data = addrmap_find (objfile->psymtabs_addrmap, pc);
3680   if (!data)
3681     return NULL;
3682 
3683   if (warn_if_readin && data->v.quick->symtab)
3684     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3685 	     paddress (get_objfile_arch (objfile), pc));
3686 
3687   result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3688   gdb_assert (result != NULL);
3689   return result;
3690 }
3691 
3692 static void
3693 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3694 			  void *data, int need_fullname)
3695 {
3696   int i;
3697   struct cleanup *cleanup;
3698   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3699 				      NULL, xcalloc, xfree);
3700 
3701   cleanup = make_cleanup_htab_delete (visited);
3702   dw2_setup (objfile);
3703 
3704   /* The rule is CUs specify all the files, including those used by
3705      any TU, so there's no need to scan TUs here.
3706      We can ignore file names coming from already-expanded CUs.  */
3707 
3708   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3709     {
3710       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3711 
3712       if (per_cu->v.quick->symtab)
3713 	{
3714 	  void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3715 					INSERT);
3716 
3717 	  *slot = per_cu->v.quick->file_names;
3718 	}
3719     }
3720 
3721   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3722     {
3723       int j;
3724       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3725       struct quick_file_names *file_data;
3726       void **slot;
3727 
3728       /* We only need to look at symtabs not already expanded.  */
3729       if (per_cu->v.quick->symtab)
3730 	continue;
3731 
3732       file_data = dw2_get_file_names (objfile, per_cu);
3733       if (file_data == NULL)
3734 	continue;
3735 
3736       slot = htab_find_slot (visited, file_data, INSERT);
3737       if (*slot)
3738 	{
3739 	  /* Already visited.  */
3740 	  continue;
3741 	}
3742       *slot = file_data;
3743 
3744       for (j = 0; j < file_data->num_file_names; ++j)
3745 	{
3746 	  const char *this_real_name;
3747 
3748 	  if (need_fullname)
3749 	    this_real_name = dw2_get_real_path (objfile, file_data, j);
3750 	  else
3751 	    this_real_name = NULL;
3752 	  (*fun) (file_data->file_names[j], this_real_name, data);
3753 	}
3754     }
3755 
3756   do_cleanups (cleanup);
3757 }
3758 
3759 static int
3760 dw2_has_symbols (struct objfile *objfile)
3761 {
3762   return 1;
3763 }
3764 
3765 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3766 {
3767   dw2_has_symbols,
3768   dw2_find_last_source_symtab,
3769   dw2_forget_cached_source_info,
3770   dw2_map_symtabs_matching_filename,
3771   dw2_lookup_symbol,
3772   dw2_print_stats,
3773   dw2_dump,
3774   dw2_relocate,
3775   dw2_expand_symtabs_for_function,
3776   dw2_expand_all_symtabs,
3777   dw2_expand_symtabs_with_fullname,
3778   dw2_find_symbol_file,
3779   dw2_map_matching_symbols,
3780   dw2_expand_symtabs_matching,
3781   dw2_find_pc_sect_symtab,
3782   dw2_map_symbol_filenames
3783 };
3784 
3785 /* Initialize for reading DWARF for this objfile.  Return 0 if this
3786    file will use psymtabs, or 1 if using the GNU index.  */
3787 
3788 int
3789 dwarf2_initialize_objfile (struct objfile *objfile)
3790 {
3791   /* If we're about to read full symbols, don't bother with the
3792      indices.  In this case we also don't care if some other debug
3793      format is making psymtabs, because they are all about to be
3794      expanded anyway.  */
3795   if ((objfile->flags & OBJF_READNOW))
3796     {
3797       int i;
3798 
3799       dwarf2_per_objfile->using_index = 1;
3800       create_all_comp_units (objfile);
3801       create_all_type_units (objfile);
3802       dwarf2_per_objfile->quick_file_names_table =
3803 	create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3804 
3805       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3806 		       + dwarf2_per_objfile->n_type_units); ++i)
3807 	{
3808 	  struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3809 
3810 	  per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3811 					    struct dwarf2_per_cu_quick_data);
3812 	}
3813 
3814       /* Return 1 so that gdb sees the "quick" functions.  However,
3815 	 these functions will be no-ops because we will have expanded
3816 	 all symtabs.  */
3817       return 1;
3818     }
3819 
3820   if (dwarf2_read_index (objfile))
3821     return 1;
3822 
3823   return 0;
3824 }
3825 
3826 
3827 
3828 /* Build a partial symbol table.  */
3829 
3830 void
3831 dwarf2_build_psymtabs (struct objfile *objfile)
3832 {
3833   volatile struct gdb_exception except;
3834 
3835   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3836     {
3837       init_psymbol_list (objfile, 1024);
3838     }
3839 
3840   TRY_CATCH (except, RETURN_MASK_ERROR)
3841     {
3842       /* This isn't really ideal: all the data we allocate on the
3843 	 objfile's obstack is still uselessly kept around.  However,
3844 	 freeing it seems unsafe.  */
3845       struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3846 
3847       dwarf2_build_psymtabs_hard (objfile);
3848       discard_cleanups (cleanups);
3849     }
3850   if (except.reason < 0)
3851     exception_print (gdb_stderr, except);
3852 }
3853 
3854 /* Return the total length of the CU described by HEADER.  */
3855 
3856 static unsigned int
3857 get_cu_length (const struct comp_unit_head *header)
3858 {
3859   return header->initial_length_size + header->length;
3860 }
3861 
3862 /* Return TRUE if OFFSET is within CU_HEADER.  */
3863 
3864 static inline int
3865 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3866 {
3867   sect_offset bottom = { cu_header->offset.sect_off };
3868   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3869 
3870   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3871 }
3872 
3873 /* Find the base address of the compilation unit for range lists and
3874    location lists.  It will normally be specified by DW_AT_low_pc.
3875    In DWARF-3 draft 4, the base address could be overridden by
3876    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3877    compilation units with discontinuous ranges.  */
3878 
3879 static void
3880 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3881 {
3882   struct attribute *attr;
3883 
3884   cu->base_known = 0;
3885   cu->base_address = 0;
3886 
3887   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3888   if (attr)
3889     {
3890       cu->base_address = DW_ADDR (attr);
3891       cu->base_known = 1;
3892     }
3893   else
3894     {
3895       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3896       if (attr)
3897 	{
3898 	  cu->base_address = DW_ADDR (attr);
3899 	  cu->base_known = 1;
3900 	}
3901     }
3902 }
3903 
3904 /* Read in the comp unit header information from the debug_info at info_ptr.
3905    NOTE: This leaves members offset, first_die_offset to be filled in
3906    by the caller.  */
3907 
3908 static gdb_byte *
3909 read_comp_unit_head (struct comp_unit_head *cu_header,
3910 		     gdb_byte *info_ptr, bfd *abfd)
3911 {
3912   int signed_addr;
3913   unsigned int bytes_read;
3914 
3915   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3916   cu_header->initial_length_size = bytes_read;
3917   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3918   info_ptr += bytes_read;
3919   cu_header->version = read_2_bytes (abfd, info_ptr);
3920   info_ptr += 2;
3921   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3922 					     &bytes_read);
3923   info_ptr += bytes_read;
3924   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3925   info_ptr += 1;
3926   signed_addr = bfd_get_sign_extend_vma (abfd);
3927   if (signed_addr < 0)
3928     internal_error (__FILE__, __LINE__,
3929 		    _("read_comp_unit_head: dwarf from non elf file"));
3930   cu_header->signed_addr_p = signed_addr;
3931 
3932   return info_ptr;
3933 }
3934 
3935 /* Helper function that returns the proper abbrev section for
3936    THIS_CU.  */
3937 
3938 static struct dwarf2_section_info *
3939 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3940 {
3941   struct dwarf2_section_info *abbrev;
3942 
3943   if (this_cu->is_dwz)
3944     abbrev = &dwarf2_get_dwz_file ()->abbrev;
3945   else
3946     abbrev = &dwarf2_per_objfile->abbrev;
3947 
3948   return abbrev;
3949 }
3950 
3951 /* Subroutine of read_and_check_comp_unit_head and
3952    read_and_check_type_unit_head to simplify them.
3953    Perform various error checking on the header.  */
3954 
3955 static void
3956 error_check_comp_unit_head (struct comp_unit_head *header,
3957 			    struct dwarf2_section_info *section,
3958 			    struct dwarf2_section_info *abbrev_section)
3959 {
3960   bfd *abfd = section->asection->owner;
3961   const char *filename = bfd_get_filename (abfd);
3962 
3963   if (header->version != 2 && header->version != 3 && header->version != 4)
3964     error (_("Dwarf Error: wrong version in compilation unit header "
3965 	   "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3966 	   filename);
3967 
3968   if (header->abbrev_offset.sect_off
3969       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3970     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3971 	   "(offset 0x%lx + 6) [in module %s]"),
3972 	   (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3973 	   filename);
3974 
3975   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3976      avoid potential 32-bit overflow.  */
3977   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3978       > section->size)
3979     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3980 	   "(offset 0x%lx + 0) [in module %s]"),
3981 	   (long) header->length, (long) header->offset.sect_off,
3982 	   filename);
3983 }
3984 
3985 /* Read in a CU/TU header and perform some basic error checking.
3986    The contents of the header are stored in HEADER.
3987    The result is a pointer to the start of the first DIE.  */
3988 
3989 static gdb_byte *
3990 read_and_check_comp_unit_head (struct comp_unit_head *header,
3991 			       struct dwarf2_section_info *section,
3992 			       struct dwarf2_section_info *abbrev_section,
3993 			       gdb_byte *info_ptr,
3994 			       int is_debug_types_section)
3995 {
3996   gdb_byte *beg_of_comp_unit = info_ptr;
3997   bfd *abfd = section->asection->owner;
3998 
3999   header->offset.sect_off = beg_of_comp_unit - section->buffer;
4000 
4001   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4002 
4003   /* If we're reading a type unit, skip over the signature and
4004      type_offset fields.  */
4005   if (is_debug_types_section)
4006     info_ptr += 8 /*signature*/ + header->offset_size;
4007 
4008   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4009 
4010   error_check_comp_unit_head (header, section, abbrev_section);
4011 
4012   return info_ptr;
4013 }
4014 
4015 /* Read in the types comp unit header information from .debug_types entry at
4016    types_ptr.  The result is a pointer to one past the end of the header.  */
4017 
4018 static gdb_byte *
4019 read_and_check_type_unit_head (struct comp_unit_head *header,
4020 			       struct dwarf2_section_info *section,
4021 			       struct dwarf2_section_info *abbrev_section,
4022 			       gdb_byte *info_ptr,
4023 			       ULONGEST *signature,
4024 			       cu_offset *type_offset_in_tu)
4025 {
4026   gdb_byte *beg_of_comp_unit = info_ptr;
4027   bfd *abfd = section->asection->owner;
4028 
4029   header->offset.sect_off = beg_of_comp_unit - section->buffer;
4030 
4031   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4032 
4033   /* If we're reading a type unit, skip over the signature and
4034      type_offset fields.  */
4035   if (signature != NULL)
4036     *signature = read_8_bytes (abfd, info_ptr);
4037   info_ptr += 8;
4038   if (type_offset_in_tu != NULL)
4039     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4040 					       header->offset_size);
4041   info_ptr += header->offset_size;
4042 
4043   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4044 
4045   error_check_comp_unit_head (header, section, abbrev_section);
4046 
4047   return info_ptr;
4048 }
4049 
4050 /* Fetch the abbreviation table offset from a comp or type unit header.  */
4051 
4052 static sect_offset
4053 read_abbrev_offset (struct dwarf2_section_info *section,
4054 		    sect_offset offset)
4055 {
4056   bfd *abfd = section->asection->owner;
4057   gdb_byte *info_ptr;
4058   unsigned int length, initial_length_size, offset_size;
4059   sect_offset abbrev_offset;
4060 
4061   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4062   info_ptr = section->buffer + offset.sect_off;
4063   length = read_initial_length (abfd, info_ptr, &initial_length_size);
4064   offset_size = initial_length_size == 4 ? 4 : 8;
4065   info_ptr += initial_length_size + 2 /*version*/;
4066   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4067   return abbrev_offset;
4068 }
4069 
4070 /* Allocate a new partial symtab for file named NAME and mark this new
4071    partial symtab as being an include of PST.  */
4072 
4073 static void
4074 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4075                                struct objfile *objfile)
4076 {
4077   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4078 
4079   if (!IS_ABSOLUTE_PATH (subpst->filename))
4080     {
4081       /* It shares objfile->objfile_obstack.  */
4082       subpst->dirname = pst->dirname;
4083     }
4084 
4085   subpst->section_offsets = pst->section_offsets;
4086   subpst->textlow = 0;
4087   subpst->texthigh = 0;
4088 
4089   subpst->dependencies = (struct partial_symtab **)
4090     obstack_alloc (&objfile->objfile_obstack,
4091                    sizeof (struct partial_symtab *));
4092   subpst->dependencies[0] = pst;
4093   subpst->number_of_dependencies = 1;
4094 
4095   subpst->globals_offset = 0;
4096   subpst->n_global_syms = 0;
4097   subpst->statics_offset = 0;
4098   subpst->n_static_syms = 0;
4099   subpst->symtab = NULL;
4100   subpst->read_symtab = pst->read_symtab;
4101   subpst->readin = 0;
4102 
4103   /* No private part is necessary for include psymtabs.  This property
4104      can be used to differentiate between such include psymtabs and
4105      the regular ones.  */
4106   subpst->read_symtab_private = NULL;
4107 }
4108 
4109 /* Read the Line Number Program data and extract the list of files
4110    included by the source file represented by PST.  Build an include
4111    partial symtab for each of these included files.  */
4112 
4113 static void
4114 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4115 			       struct die_info *die,
4116 			       struct partial_symtab *pst)
4117 {
4118   struct line_header *lh = NULL;
4119   struct attribute *attr;
4120 
4121   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4122   if (attr)
4123     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4124   if (lh == NULL)
4125     return;  /* No linetable, so no includes.  */
4126 
4127   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
4128   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4129 
4130   free_line_header (lh);
4131 }
4132 
4133 static hashval_t
4134 hash_signatured_type (const void *item)
4135 {
4136   const struct signatured_type *sig_type = item;
4137 
4138   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
4139   return sig_type->signature;
4140 }
4141 
4142 static int
4143 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4144 {
4145   const struct signatured_type *lhs = item_lhs;
4146   const struct signatured_type *rhs = item_rhs;
4147 
4148   return lhs->signature == rhs->signature;
4149 }
4150 
4151 /* Allocate a hash table for signatured types.  */
4152 
4153 static htab_t
4154 allocate_signatured_type_table (struct objfile *objfile)
4155 {
4156   return htab_create_alloc_ex (41,
4157 			       hash_signatured_type,
4158 			       eq_signatured_type,
4159 			       NULL,
4160 			       &objfile->objfile_obstack,
4161 			       hashtab_obstack_allocate,
4162 			       dummy_obstack_deallocate);
4163 }
4164 
4165 /* A helper function to add a signatured type CU to a table.  */
4166 
4167 static int
4168 add_signatured_type_cu_to_table (void **slot, void *datum)
4169 {
4170   struct signatured_type *sigt = *slot;
4171   struct signatured_type ***datap = datum;
4172 
4173   **datap = sigt;
4174   ++*datap;
4175 
4176   return 1;
4177 }
4178 
4179 /* Create the hash table of all entries in the .debug_types section.
4180    DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4181    NULL otherwise.
4182    Note: This function processes DWO files only, not DWP files.
4183    The result is a pointer to the hash table or NULL if there are
4184    no types.  */
4185 
4186 static htab_t
4187 create_debug_types_hash_table (struct dwo_file *dwo_file,
4188 			       VEC (dwarf2_section_info_def) *types)
4189 {
4190   struct objfile *objfile = dwarf2_per_objfile->objfile;
4191   htab_t types_htab = NULL;
4192   int ix;
4193   struct dwarf2_section_info *section;
4194   struct dwarf2_section_info *abbrev_section;
4195 
4196   if (VEC_empty (dwarf2_section_info_def, types))
4197     return NULL;
4198 
4199   abbrev_section = (dwo_file != NULL
4200 		    ? &dwo_file->sections.abbrev
4201 		    : &dwarf2_per_objfile->abbrev);
4202 
4203   if (dwarf2_read_debug)
4204     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4205 			dwo_file ? ".dwo" : "",
4206 			bfd_get_filename (abbrev_section->asection->owner));
4207 
4208   for (ix = 0;
4209        VEC_iterate (dwarf2_section_info_def, types, ix, section);
4210        ++ix)
4211     {
4212       bfd *abfd;
4213       gdb_byte *info_ptr, *end_ptr;
4214       struct dwarf2_section_info *abbrev_section;
4215 
4216       dwarf2_read_section (objfile, section);
4217       info_ptr = section->buffer;
4218 
4219       if (info_ptr == NULL)
4220 	continue;
4221 
4222       /* We can't set abfd until now because the section may be empty or
4223 	 not present, in which case section->asection will be NULL.  */
4224       abfd = section->asection->owner;
4225 
4226       if (dwo_file)
4227 	abbrev_section = &dwo_file->sections.abbrev;
4228       else
4229 	abbrev_section = &dwarf2_per_objfile->abbrev;
4230 
4231       if (types_htab == NULL)
4232 	{
4233 	  if (dwo_file)
4234 	    types_htab = allocate_dwo_unit_table (objfile);
4235 	  else
4236 	    types_htab = allocate_signatured_type_table (objfile);
4237 	}
4238 
4239       /* We don't use init_cutu_and_read_dies_simple, or some such, here
4240 	 because we don't need to read any dies: the signature is in the
4241 	 header.  */
4242 
4243       end_ptr = info_ptr + section->size;
4244       while (info_ptr < end_ptr)
4245 	{
4246 	  sect_offset offset;
4247 	  cu_offset type_offset_in_tu;
4248 	  ULONGEST signature;
4249 	  struct signatured_type *sig_type;
4250 	  struct dwo_unit *dwo_tu;
4251 	  void **slot;
4252 	  gdb_byte *ptr = info_ptr;
4253 	  struct comp_unit_head header;
4254 	  unsigned int length;
4255 
4256 	  offset.sect_off = ptr - section->buffer;
4257 
4258 	  /* We need to read the type's signature in order to build the hash
4259 	     table, but we don't need anything else just yet.  */
4260 
4261 	  ptr = read_and_check_type_unit_head (&header, section,
4262 					       abbrev_section, ptr,
4263 					       &signature, &type_offset_in_tu);
4264 
4265 	  length = get_cu_length (&header);
4266 
4267 	  /* Skip dummy type units.  */
4268 	  if (ptr >= info_ptr + length
4269 	      || peek_abbrev_code (abfd, ptr) == 0)
4270 	    {
4271 	      info_ptr += length;
4272 	      continue;
4273 	    }
4274 
4275 	  if (dwo_file)
4276 	    {
4277 	      sig_type = NULL;
4278 	      dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4279 				       struct dwo_unit);
4280 	      dwo_tu->dwo_file = dwo_file;
4281 	      dwo_tu->signature = signature;
4282 	      dwo_tu->type_offset_in_tu = type_offset_in_tu;
4283 	      dwo_tu->info_or_types_section = section;
4284 	      dwo_tu->offset = offset;
4285 	      dwo_tu->length = length;
4286 	    }
4287 	  else
4288 	    {
4289 	      /* N.B.: type_offset is not usable if this type uses a DWO file.
4290 		 The real type_offset is in the DWO file.  */
4291 	      dwo_tu = NULL;
4292 	      sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4293 					 struct signatured_type);
4294 	      sig_type->signature = signature;
4295 	      sig_type->type_offset_in_tu = type_offset_in_tu;
4296 	      sig_type->per_cu.objfile = objfile;
4297 	      sig_type->per_cu.is_debug_types = 1;
4298 	      sig_type->per_cu.info_or_types_section = section;
4299 	      sig_type->per_cu.offset = offset;
4300 	      sig_type->per_cu.length = length;
4301 	    }
4302 
4303 	  slot = htab_find_slot (types_htab,
4304 				 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4305 				 INSERT);
4306 	  gdb_assert (slot != NULL);
4307 	  if (*slot != NULL)
4308 	    {
4309 	      sect_offset dup_offset;
4310 
4311 	      if (dwo_file)
4312 		{
4313 		  const struct dwo_unit *dup_tu = *slot;
4314 
4315 		  dup_offset = dup_tu->offset;
4316 		}
4317 	      else
4318 		{
4319 		  const struct signatured_type *dup_tu = *slot;
4320 
4321 		  dup_offset = dup_tu->per_cu.offset;
4322 		}
4323 
4324 	      complaint (&symfile_complaints,
4325 			 _("debug type entry at offset 0x%x is duplicate to the "
4326 			   "entry at offset 0x%x, signature 0x%s"),
4327 			 offset.sect_off, dup_offset.sect_off,
4328 			 phex (signature, sizeof (signature)));
4329 	    }
4330 	  *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4331 
4332 	  if (dwarf2_read_debug)
4333 	    fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
4334 				offset.sect_off,
4335 				phex (signature, sizeof (signature)));
4336 
4337 	  info_ptr += length;
4338 	}
4339     }
4340 
4341   return types_htab;
4342 }
4343 
4344 /* Create the hash table of all entries in the .debug_types section,
4345    and initialize all_type_units.
4346    The result is zero if there is an error (e.g. missing .debug_types section),
4347    otherwise non-zero.	*/
4348 
4349 static int
4350 create_all_type_units (struct objfile *objfile)
4351 {
4352   htab_t types_htab;
4353   struct signatured_type **iter;
4354 
4355   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4356   if (types_htab == NULL)
4357     {
4358       dwarf2_per_objfile->signatured_types = NULL;
4359       return 0;
4360     }
4361 
4362   dwarf2_per_objfile->signatured_types = types_htab;
4363 
4364   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4365   dwarf2_per_objfile->all_type_units
4366     = obstack_alloc (&objfile->objfile_obstack,
4367 		     dwarf2_per_objfile->n_type_units
4368 		     * sizeof (struct signatured_type *));
4369   iter = &dwarf2_per_objfile->all_type_units[0];
4370   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4371   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4372 	      == dwarf2_per_objfile->n_type_units);
4373 
4374   return 1;
4375 }
4376 
4377 /* Lookup a signature based type for DW_FORM_ref_sig8.
4378    Returns NULL if signature SIG is not present in the table.  */
4379 
4380 static struct signatured_type *
4381 lookup_signatured_type (ULONGEST sig)
4382 {
4383   struct signatured_type find_entry, *entry;
4384 
4385   if (dwarf2_per_objfile->signatured_types == NULL)
4386     {
4387       complaint (&symfile_complaints,
4388 		 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4389       return NULL;
4390     }
4391 
4392   find_entry.signature = sig;
4393   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4394   return entry;
4395 }
4396 
4397 /* Low level DIE reading support.  */
4398 
4399 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
4400 
4401 static void
4402 init_cu_die_reader (struct die_reader_specs *reader,
4403 		    struct dwarf2_cu *cu,
4404 		    struct dwarf2_section_info *section,
4405 		    struct dwo_file *dwo_file)
4406 {
4407   gdb_assert (section->readin && section->buffer != NULL);
4408   reader->abfd = section->asection->owner;
4409   reader->cu = cu;
4410   reader->dwo_file = dwo_file;
4411   reader->die_section = section;
4412   reader->buffer = section->buffer;
4413   reader->buffer_end = section->buffer + section->size;
4414 }
4415 
4416 /* Initialize a CU (or TU) and read its DIEs.
4417    If the CU defers to a DWO file, read the DWO file as well.
4418 
4419    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4420    Otherwise the table specified in the comp unit header is read in and used.
4421    This is an optimization for when we already have the abbrev table.
4422 
4423    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4424    Otherwise, a new CU is allocated with xmalloc.
4425 
4426    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4427    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.
4428 
4429    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4430    linker) then DIE_READER_FUNC will not get called.  */
4431 
4432 static void
4433 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4434 			 struct abbrev_table *abbrev_table,
4435 			 int use_existing_cu, int keep,
4436 			 die_reader_func_ftype *die_reader_func,
4437 			 void *data)
4438 {
4439   struct objfile *objfile = dwarf2_per_objfile->objfile;
4440   struct dwarf2_section_info *section = this_cu->info_or_types_section;
4441   bfd *abfd = section->asection->owner;
4442   struct dwarf2_cu *cu;
4443   gdb_byte *begin_info_ptr, *info_ptr;
4444   struct die_reader_specs reader;
4445   struct die_info *comp_unit_die;
4446   int has_children;
4447   struct attribute *attr;
4448   struct cleanup *cleanups, *free_cu_cleanup = NULL;
4449   struct signatured_type *sig_type = NULL;
4450   struct dwarf2_section_info *abbrev_section;
4451   /* Non-zero if CU currently points to a DWO file and we need to
4452      reread it.  When this happens we need to reread the skeleton die
4453      before we can reread the DWO file.  */
4454   int rereading_dwo_cu = 0;
4455 
4456   if (dwarf2_die_debug)
4457     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4458 			this_cu->is_debug_types ? "type" : "comp",
4459 			this_cu->offset.sect_off);
4460 
4461   if (use_existing_cu)
4462     gdb_assert (keep);
4463 
4464   cleanups = make_cleanup (null_cleanup, NULL);
4465 
4466   /* This is cheap if the section is already read in.  */
4467   dwarf2_read_section (objfile, section);
4468 
4469   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4470 
4471   abbrev_section = get_abbrev_section_for_cu (this_cu);
4472 
4473   if (use_existing_cu && this_cu->cu != NULL)
4474     {
4475       cu = this_cu->cu;
4476 
4477       /* If this CU is from a DWO file we need to start over, we need to
4478 	 refetch the attributes from the skeleton CU.
4479 	 This could be optimized by retrieving those attributes from when we
4480 	 were here the first time: the previous comp_unit_die was stored in
4481 	 comp_unit_obstack.  But there's no data yet that we need this
4482 	 optimization.  */
4483       if (cu->dwo_unit != NULL)
4484 	rereading_dwo_cu = 1;
4485     }
4486   else
4487     {
4488       /* If !use_existing_cu, this_cu->cu must be NULL.  */
4489       gdb_assert (this_cu->cu == NULL);
4490 
4491       cu = xmalloc (sizeof (*cu));
4492       init_one_comp_unit (cu, this_cu);
4493 
4494       /* If an error occurs while loading, release our storage.  */
4495       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4496     }
4497 
4498   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4499     {
4500       /* We already have the header, there's no need to read it in again.  */
4501       info_ptr += cu->header.first_die_offset.cu_off;
4502     }
4503   else
4504     {
4505       if (this_cu->is_debug_types)
4506 	{
4507 	  ULONGEST signature;
4508 	  cu_offset type_offset_in_tu;
4509 
4510 	  info_ptr = read_and_check_type_unit_head (&cu->header, section,
4511 						    abbrev_section, info_ptr,
4512 						    &signature,
4513 						    &type_offset_in_tu);
4514 
4515 	  /* Since per_cu is the first member of struct signatured_type,
4516 	     we can go from a pointer to one to a pointer to the other.  */
4517 	  sig_type = (struct signatured_type *) this_cu;
4518 	  gdb_assert (sig_type->signature == signature);
4519 	  gdb_assert (sig_type->type_offset_in_tu.cu_off
4520 		      == type_offset_in_tu.cu_off);
4521 	  gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4522 
4523 	  /* LENGTH has not been set yet for type units if we're
4524 	     using .gdb_index.  */
4525 	  this_cu->length = get_cu_length (&cu->header);
4526 
4527 	  /* Establish the type offset that can be used to lookup the type.  */
4528 	  sig_type->type_offset_in_section.sect_off =
4529 	    this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4530 	}
4531       else
4532 	{
4533 	  info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4534 						    abbrev_section,
4535 						    info_ptr, 0);
4536 
4537 	  gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4538 	  gdb_assert (this_cu->length == get_cu_length (&cu->header));
4539 	}
4540     }
4541 
4542   /* Skip dummy compilation units.  */
4543   if (info_ptr >= begin_info_ptr + this_cu->length
4544       || peek_abbrev_code (abfd, info_ptr) == 0)
4545     {
4546       do_cleanups (cleanups);
4547       return;
4548     }
4549 
4550   /* If we don't have them yet, read the abbrevs for this compilation unit.
4551      And if we need to read them now, make sure they're freed when we're
4552      done.  Note that it's important that if the CU had an abbrev table
4553      on entry we don't free it when we're done: Somewhere up the call stack
4554      it may be in use.  */
4555   if (abbrev_table != NULL)
4556     {
4557       gdb_assert (cu->abbrev_table == NULL);
4558       gdb_assert (cu->header.abbrev_offset.sect_off
4559 		  == abbrev_table->offset.sect_off);
4560       cu->abbrev_table = abbrev_table;
4561     }
4562   else if (cu->abbrev_table == NULL)
4563     {
4564       dwarf2_read_abbrevs (cu, abbrev_section);
4565       make_cleanup (dwarf2_free_abbrev_table, cu);
4566     }
4567   else if (rereading_dwo_cu)
4568     {
4569       dwarf2_free_abbrev_table (cu);
4570       dwarf2_read_abbrevs (cu, abbrev_section);
4571     }
4572 
4573   /* Read the top level CU/TU die.  */
4574   init_cu_die_reader (&reader, cu, section, NULL);
4575   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4576 
4577   /* If we have a DWO stub, process it and then read in the DWO file.
4578      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4579      a DWO CU, that this test will fail.  */
4580   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4581   if (attr)
4582     {
4583       const char *dwo_name = DW_STRING (attr);
4584       const char *comp_dir_string;
4585       struct dwo_unit *dwo_unit;
4586       ULONGEST signature; /* Or dwo_id.  */
4587       struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4588       int i,num_extra_attrs;
4589       struct dwarf2_section_info *dwo_abbrev_section;
4590 
4591       if (has_children)
4592 	error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4593 		 " has children (offset 0x%x) [in module %s]"),
4594 	       this_cu->offset.sect_off, bfd_get_filename (abfd));
4595 
4596       /* These attributes aren't processed until later:
4597 	 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4598 	 However, the attribute is found in the stub which we won't have later.
4599 	 In order to not impose this complication on the rest of the code,
4600 	 we read them here and copy them to the DWO CU/TU die.  */
4601 
4602       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4603 	 DWO file.  */
4604       stmt_list = NULL;
4605       if (! this_cu->is_debug_types)
4606 	stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4607       low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4608       high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4609       ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4610       comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4611 
4612       /* There should be a DW_AT_addr_base attribute here (if needed).
4613 	 We need the value before we can process DW_FORM_GNU_addr_index.  */
4614       cu->addr_base = 0;
4615       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4616       if (attr)
4617 	cu->addr_base = DW_UNSND (attr);
4618 
4619       /* There should be a DW_AT_ranges_base attribute here (if needed).
4620 	 We need the value before we can process DW_AT_ranges.  */
4621       cu->ranges_base = 0;
4622       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4623       if (attr)
4624 	cu->ranges_base = DW_UNSND (attr);
4625 
4626       if (this_cu->is_debug_types)
4627 	{
4628 	  gdb_assert (sig_type != NULL);
4629 	  signature = sig_type->signature;
4630 	}
4631       else
4632 	{
4633 	  attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4634 	  if (! attr)
4635 	    error (_("Dwarf Error: missing dwo_id [in module %s]"),
4636 		   dwo_name);
4637 	  signature = DW_UNSND (attr);
4638 	}
4639 
4640       /* We may need the comp_dir in order to find the DWO file.  */
4641       comp_dir_string = NULL;
4642       if (comp_dir)
4643 	comp_dir_string = DW_STRING (comp_dir);
4644 
4645       if (this_cu->is_debug_types)
4646 	dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4647       else
4648 	dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4649 					 signature);
4650 
4651       if (dwo_unit == NULL)
4652 	{
4653 	  error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4654 		   " with ID %s [in module %s]"),
4655 		 this_cu->offset.sect_off,
4656 		 phex (signature, sizeof (signature)),
4657 		 objfile->name);
4658 	}
4659 
4660       /* Set up for reading the DWO CU/TU.  */
4661       cu->dwo_unit = dwo_unit;
4662       section = dwo_unit->info_or_types_section;
4663       dwarf2_read_section (objfile, section);
4664       begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4665       dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4666       init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4667 
4668       if (this_cu->is_debug_types)
4669 	{
4670 	  ULONGEST signature;
4671 	  cu_offset type_offset_in_tu;
4672 
4673 	  info_ptr = read_and_check_type_unit_head (&cu->header, section,
4674 						    dwo_abbrev_section,
4675 						    info_ptr,
4676 						    &signature,
4677 						    &type_offset_in_tu);
4678 	  gdb_assert (sig_type->signature == signature);
4679 	  gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4680 	  /* For DWOs coming from DWP files, we don't know the CU length
4681 	     nor the type's offset in the TU until now.  */
4682 	  dwo_unit->length = get_cu_length (&cu->header);
4683 	  dwo_unit->type_offset_in_tu = type_offset_in_tu;
4684 
4685 	  /* Establish the type offset that can be used to lookup the type.
4686 	     For DWO files, we don't know it until now.  */
4687 	  sig_type->type_offset_in_section.sect_off =
4688 	    dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4689 	}
4690       else
4691 	{
4692 	  info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4693 						    dwo_abbrev_section,
4694 						    info_ptr, 0);
4695 	  gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4696 	  /* For DWOs coming from DWP files, we don't know the CU length
4697 	     until now.  */
4698 	  dwo_unit->length = get_cu_length (&cu->header);
4699 	}
4700 
4701       /* Discard the original CU's abbrev table, and read the DWO's.  */
4702       if (abbrev_table == NULL)
4703 	{
4704 	  dwarf2_free_abbrev_table (cu);
4705 	  dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4706 	}
4707       else
4708 	{
4709 	  dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4710 	  make_cleanup (dwarf2_free_abbrev_table, cu);
4711 	}
4712 
4713       /* Read in the die, but leave space to copy over the attributes
4714 	 from the stub.  This has the benefit of simplifying the rest of
4715 	 the code - all the real work is done here.  */
4716       num_extra_attrs = ((stmt_list != NULL)
4717 			 + (low_pc != NULL)
4718 			 + (high_pc != NULL)
4719 			 + (ranges != NULL)
4720 			 + (comp_dir != NULL));
4721       info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4722 				  &has_children, num_extra_attrs);
4723 
4724       /* Copy over the attributes from the stub to the DWO die.  */
4725       i = comp_unit_die->num_attrs;
4726       if (stmt_list != NULL)
4727 	comp_unit_die->attrs[i++] = *stmt_list;
4728       if (low_pc != NULL)
4729 	comp_unit_die->attrs[i++] = *low_pc;
4730       if (high_pc != NULL)
4731 	comp_unit_die->attrs[i++] = *high_pc;
4732       if (ranges != NULL)
4733 	comp_unit_die->attrs[i++] = *ranges;
4734       if (comp_dir != NULL)
4735 	comp_unit_die->attrs[i++] = *comp_dir;
4736       comp_unit_die->num_attrs += num_extra_attrs;
4737 
4738       /* Skip dummy compilation units.  */
4739       if (info_ptr >= begin_info_ptr + dwo_unit->length
4740 	  || peek_abbrev_code (abfd, info_ptr) == 0)
4741 	{
4742 	  do_cleanups (cleanups);
4743 	  return;
4744 	}
4745     }
4746 
4747   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4748 
4749   if (free_cu_cleanup != NULL)
4750     {
4751       if (keep)
4752 	{
4753 	  /* We've successfully allocated this compilation unit.  Let our
4754 	     caller clean it up when finished with it.  */
4755 	  discard_cleanups (free_cu_cleanup);
4756 
4757 	  /* We can only discard free_cu_cleanup and all subsequent cleanups.
4758 	     So we have to manually free the abbrev table.  */
4759 	  dwarf2_free_abbrev_table (cu);
4760 
4761 	  /* Link this CU into read_in_chain.  */
4762 	  this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4763 	  dwarf2_per_objfile->read_in_chain = this_cu;
4764 	}
4765       else
4766 	do_cleanups (free_cu_cleanup);
4767     }
4768 
4769   do_cleanups (cleanups);
4770 }
4771 
4772 /* Read CU/TU THIS_CU in section SECTION,
4773    but do not follow DW_AT_GNU_dwo_name if present.
4774    DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4775    to have already done the lookup to find the DWO/DWP file).
4776 
4777    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4778    THIS_CU->is_debug_types, but nothing else.
4779 
4780    We fill in THIS_CU->length.
4781 
4782    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4783    linker) then DIE_READER_FUNC will not get called.
4784 
4785    THIS_CU->cu is always freed when done.
4786    This is done in order to not leave THIS_CU->cu in a state where we have
4787    to care whether it refers to the "main" CU or the DWO CU.  */
4788 
4789 static void
4790 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4791 				   struct dwarf2_section_info *abbrev_section,
4792 				   struct dwo_file *dwo_file,
4793 				   die_reader_func_ftype *die_reader_func,
4794 				   void *data)
4795 {
4796   struct objfile *objfile = dwarf2_per_objfile->objfile;
4797   struct dwarf2_section_info *section = this_cu->info_or_types_section;
4798   bfd *abfd = section->asection->owner;
4799   struct dwarf2_cu cu;
4800   gdb_byte *begin_info_ptr, *info_ptr;
4801   struct die_reader_specs reader;
4802   struct cleanup *cleanups;
4803   struct die_info *comp_unit_die;
4804   int has_children;
4805 
4806   if (dwarf2_die_debug)
4807     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4808 			this_cu->is_debug_types ? "type" : "comp",
4809 			this_cu->offset.sect_off);
4810 
4811   gdb_assert (this_cu->cu == NULL);
4812 
4813   /* This is cheap if the section is already read in.  */
4814   dwarf2_read_section (objfile, section);
4815 
4816   init_one_comp_unit (&cu, this_cu);
4817 
4818   cleanups = make_cleanup (free_stack_comp_unit, &cu);
4819 
4820   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4821   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4822 					    abbrev_section, info_ptr,
4823 					    this_cu->is_debug_types);
4824 
4825   this_cu->length = get_cu_length (&cu.header);
4826 
4827   /* Skip dummy compilation units.  */
4828   if (info_ptr >= begin_info_ptr + this_cu->length
4829       || peek_abbrev_code (abfd, info_ptr) == 0)
4830     {
4831       do_cleanups (cleanups);
4832       return;
4833     }
4834 
4835   dwarf2_read_abbrevs (&cu, abbrev_section);
4836   make_cleanup (dwarf2_free_abbrev_table, &cu);
4837 
4838   init_cu_die_reader (&reader, &cu, section, dwo_file);
4839   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4840 
4841   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4842 
4843   do_cleanups (cleanups);
4844 }
4845 
4846 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4847    does not lookup the specified DWO file.
4848    This cannot be used to read DWO files.
4849 
4850    THIS_CU->cu is always freed when done.
4851    This is done in order to not leave THIS_CU->cu in a state where we have
4852    to care whether it refers to the "main" CU or the DWO CU.
4853    We can revisit this if the data shows there's a performance issue.  */
4854 
4855 static void
4856 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4857 				die_reader_func_ftype *die_reader_func,
4858 				void *data)
4859 {
4860   init_cutu_and_read_dies_no_follow (this_cu,
4861 				     get_abbrev_section_for_cu (this_cu),
4862 				     NULL,
4863 				     die_reader_func, data);
4864 }
4865 
4866 /* Create a psymtab named NAME and assign it to PER_CU.
4867 
4868    The caller must fill in the following details:
4869    dirname, textlow, texthigh.  */
4870 
4871 static struct partial_symtab *
4872 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4873 {
4874   struct objfile *objfile = per_cu->objfile;
4875   struct partial_symtab *pst;
4876 
4877   pst = start_psymtab_common (objfile, objfile->section_offsets,
4878 			      name, 0,
4879 			      objfile->global_psymbols.next,
4880 			      objfile->static_psymbols.next);
4881 
4882   pst->psymtabs_addrmap_supported = 1;
4883 
4884   /* This is the glue that links PST into GDB's symbol API.  */
4885   pst->read_symtab_private = per_cu;
4886   pst->read_symtab = dwarf2_read_symtab;
4887   per_cu->v.psymtab = pst;
4888 
4889   return pst;
4890 }
4891 
4892 /* die_reader_func for process_psymtab_comp_unit.  */
4893 
4894 static void
4895 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4896 				  gdb_byte *info_ptr,
4897 				  struct die_info *comp_unit_die,
4898 				  int has_children,
4899 				  void *data)
4900 {
4901   struct dwarf2_cu *cu = reader->cu;
4902   struct objfile *objfile = cu->objfile;
4903   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4904   struct attribute *attr;
4905   CORE_ADDR baseaddr;
4906   CORE_ADDR best_lowpc = 0, best_highpc = 0;
4907   struct partial_symtab *pst;
4908   int has_pc_info;
4909   const char *filename;
4910   int *want_partial_unit_ptr = data;
4911 
4912   if (comp_unit_die->tag == DW_TAG_partial_unit
4913       && (want_partial_unit_ptr == NULL
4914 	  || !*want_partial_unit_ptr))
4915     return;
4916 
4917   gdb_assert (! per_cu->is_debug_types);
4918 
4919   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4920 
4921   cu->list_in_scope = &file_symbols;
4922 
4923   /* Allocate a new partial symbol table structure.  */
4924   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4925   if (attr == NULL || !DW_STRING (attr))
4926     filename = "";
4927   else
4928     filename = DW_STRING (attr);
4929 
4930   pst = create_partial_symtab (per_cu, filename);
4931 
4932   /* This must be done before calling dwarf2_build_include_psymtabs.  */
4933   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4934   if (attr != NULL)
4935     pst->dirname = DW_STRING (attr);
4936 
4937   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4938 
4939   dwarf2_find_base_address (comp_unit_die, cu);
4940 
4941   /* Possibly set the default values of LOWPC and HIGHPC from
4942      `DW_AT_ranges'.  */
4943   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4944 				      &best_highpc, cu, pst);
4945   if (has_pc_info == 1 && best_lowpc < best_highpc)
4946     /* Store the contiguous range if it is not empty; it can be empty for
4947        CUs with no code.  */
4948     addrmap_set_empty (objfile->psymtabs_addrmap,
4949 		       best_lowpc + baseaddr,
4950 		       best_highpc + baseaddr - 1, pst);
4951 
4952   /* Check if comp unit has_children.
4953      If so, read the rest of the partial symbols from this comp unit.
4954      If not, there's no more debug_info for this comp unit.  */
4955   if (has_children)
4956     {
4957       struct partial_die_info *first_die;
4958       CORE_ADDR lowpc, highpc;
4959 
4960       lowpc = ((CORE_ADDR) -1);
4961       highpc = ((CORE_ADDR) 0);
4962 
4963       first_die = load_partial_dies (reader, info_ptr, 1);
4964 
4965       scan_partial_symbols (first_die, &lowpc, &highpc,
4966 			    ! has_pc_info, cu);
4967 
4968       /* If we didn't find a lowpc, set it to highpc to avoid
4969 	 complaints from `maint check'.	 */
4970       if (lowpc == ((CORE_ADDR) -1))
4971 	lowpc = highpc;
4972 
4973       /* If the compilation unit didn't have an explicit address range,
4974 	 then use the information extracted from its child dies.  */
4975       if (! has_pc_info)
4976 	{
4977 	  best_lowpc = lowpc;
4978 	  best_highpc = highpc;
4979 	}
4980     }
4981   pst->textlow = best_lowpc + baseaddr;
4982   pst->texthigh = best_highpc + baseaddr;
4983 
4984   pst->n_global_syms = objfile->global_psymbols.next -
4985     (objfile->global_psymbols.list + pst->globals_offset);
4986   pst->n_static_syms = objfile->static_psymbols.next -
4987     (objfile->static_psymbols.list + pst->statics_offset);
4988   sort_pst_symbols (objfile, pst);
4989 
4990   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4991     {
4992       int i;
4993       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4994       struct dwarf2_per_cu_data *iter;
4995 
4996       /* Fill in 'dependencies' here; we fill in 'users' in a
4997 	 post-pass.  */
4998       pst->number_of_dependencies = len;
4999       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5000 					 len * sizeof (struct symtab *));
5001       for (i = 0;
5002 	   VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5003 			i, iter);
5004 	   ++i)
5005 	pst->dependencies[i] = iter->v.psymtab;
5006 
5007       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5008     }
5009 
5010   /* Get the list of files included in the current compilation unit,
5011      and build a psymtab for each of them.  */
5012   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5013 
5014   if (dwarf2_read_debug)
5015     {
5016       struct gdbarch *gdbarch = get_objfile_arch (objfile);
5017 
5018       fprintf_unfiltered (gdb_stdlog,
5019 			  "Psymtab for %s unit @0x%x: %s - %s"
5020 			  ", %d global, %d static syms\n",
5021 			  per_cu->is_debug_types ? "type" : "comp",
5022 			  per_cu->offset.sect_off,
5023 			  paddress (gdbarch, pst->textlow),
5024 			  paddress (gdbarch, pst->texthigh),
5025 			  pst->n_global_syms, pst->n_static_syms);
5026     }
5027 }
5028 
5029 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5030    Process compilation unit THIS_CU for a psymtab.  */
5031 
5032 static void
5033 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5034 			   int want_partial_unit)
5035 {
5036   /* If this compilation unit was already read in, free the
5037      cached copy in order to read it in again.	This is
5038      necessary because we skipped some symbols when we first
5039      read in the compilation unit (see load_partial_dies).
5040      This problem could be avoided, but the benefit is unclear.  */
5041   if (this_cu->cu != NULL)
5042     free_one_cached_comp_unit (this_cu);
5043 
5044   gdb_assert (! this_cu->is_debug_types);
5045   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5046 			   process_psymtab_comp_unit_reader,
5047 			   &want_partial_unit);
5048 
5049   /* Age out any secondary CUs.  */
5050   age_cached_comp_units ();
5051 }
5052 
5053 static hashval_t
5054 hash_type_unit_group (const void *item)
5055 {
5056   const struct type_unit_group *tu_group = item;
5057 
5058   return hash_stmt_list_entry (&tu_group->hash);
5059 }
5060 
5061 static int
5062 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5063 {
5064   const struct type_unit_group *lhs = item_lhs;
5065   const struct type_unit_group *rhs = item_rhs;
5066 
5067   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5068 }
5069 
5070 /* Allocate a hash table for type unit groups.  */
5071 
5072 static htab_t
5073 allocate_type_unit_groups_table (void)
5074 {
5075   return htab_create_alloc_ex (3,
5076 			       hash_type_unit_group,
5077 			       eq_type_unit_group,
5078 			       NULL,
5079 			       &dwarf2_per_objfile->objfile->objfile_obstack,
5080 			       hashtab_obstack_allocate,
5081 			       dummy_obstack_deallocate);
5082 }
5083 
5084 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5085    partial symtabs.  We combine several TUs per psymtab to not let the size
5086    of any one psymtab grow too big.  */
5087 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5088 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5089 
5090 /* Helper routine for get_type_unit_group.
5091    Create the type_unit_group object used to hold one or more TUs.  */
5092 
5093 static struct type_unit_group *
5094 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5095 {
5096   struct objfile *objfile = dwarf2_per_objfile->objfile;
5097   struct dwarf2_per_cu_data *per_cu;
5098   struct type_unit_group *tu_group;
5099 
5100   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5101 			     struct type_unit_group);
5102   per_cu = &tu_group->per_cu;
5103   per_cu->objfile = objfile;
5104   per_cu->is_debug_types = 1;
5105   per_cu->type_unit_group = tu_group;
5106 
5107   if (dwarf2_per_objfile->using_index)
5108     {
5109       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5110 					struct dwarf2_per_cu_quick_data);
5111       tu_group->t.first_tu = cu->per_cu;
5112     }
5113   else
5114     {
5115       unsigned int line_offset = line_offset_struct.sect_off;
5116       struct partial_symtab *pst;
5117       char *name;
5118 
5119       /* Give the symtab a useful name for debug purposes.  */
5120       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5121 	name = xstrprintf ("<type_units_%d>",
5122 			   (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5123       else
5124 	name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5125 
5126       pst = create_partial_symtab (per_cu, name);
5127       pst->anonymous = 1;
5128 
5129       xfree (name);
5130     }
5131 
5132   tu_group->hash.dwo_unit = cu->dwo_unit;
5133   tu_group->hash.line_offset = line_offset_struct;
5134 
5135   return tu_group;
5136 }
5137 
5138 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5139    STMT_LIST is a DW_AT_stmt_list attribute.  */
5140 
5141 static struct type_unit_group *
5142 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5143 {
5144   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5145   struct type_unit_group *tu_group;
5146   void **slot;
5147   unsigned int line_offset;
5148   struct type_unit_group type_unit_group_for_lookup;
5149 
5150   if (dwarf2_per_objfile->type_unit_groups == NULL)
5151     {
5152       dwarf2_per_objfile->type_unit_groups =
5153 	allocate_type_unit_groups_table ();
5154     }
5155 
5156   /* Do we need to create a new group, or can we use an existing one?  */
5157 
5158   if (stmt_list)
5159     {
5160       line_offset = DW_UNSND (stmt_list);
5161       ++tu_stats->nr_symtab_sharers;
5162     }
5163   else
5164     {
5165       /* Ugh, no stmt_list.  Rare, but we have to handle it.
5166 	 We can do various things here like create one group per TU or
5167 	 spread them over multiple groups to split up the expansion work.
5168 	 To avoid worst case scenarios (too many groups or too large groups)
5169 	 we, umm, group them in bunches.  */
5170       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5171 		     | (tu_stats->nr_stmt_less_type_units
5172 			/ NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5173       ++tu_stats->nr_stmt_less_type_units;
5174     }
5175 
5176   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5177   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5178   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5179 			 &type_unit_group_for_lookup, INSERT);
5180   if (*slot != NULL)
5181     {
5182       tu_group = *slot;
5183       gdb_assert (tu_group != NULL);
5184     }
5185   else
5186     {
5187       sect_offset line_offset_struct;
5188 
5189       line_offset_struct.sect_off = line_offset;
5190       tu_group = create_type_unit_group (cu, line_offset_struct);
5191       *slot = tu_group;
5192       ++tu_stats->nr_symtabs;
5193     }
5194 
5195   return tu_group;
5196 }
5197 
5198 /* Struct used to sort TUs by their abbreviation table offset.  */
5199 
5200 struct tu_abbrev_offset
5201 {
5202   struct signatured_type *sig_type;
5203   sect_offset abbrev_offset;
5204 };
5205 
5206 /* Helper routine for build_type_unit_groups, passed to qsort.  */
5207 
5208 static int
5209 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5210 {
5211   const struct tu_abbrev_offset * const *a = ap;
5212   const struct tu_abbrev_offset * const *b = bp;
5213   unsigned int aoff = (*a)->abbrev_offset.sect_off;
5214   unsigned int boff = (*b)->abbrev_offset.sect_off;
5215 
5216   return (aoff > boff) - (aoff < boff);
5217 }
5218 
5219 /* A helper function to add a type_unit_group to a table.  */
5220 
5221 static int
5222 add_type_unit_group_to_table (void **slot, void *datum)
5223 {
5224   struct type_unit_group *tu_group = *slot;
5225   struct type_unit_group ***datap = datum;
5226 
5227   **datap = tu_group;
5228   ++*datap;
5229 
5230   return 1;
5231 }
5232 
5233 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5234    each one passing FUNC,DATA.
5235 
5236    The efficiency is because we sort TUs by the abbrev table they use and
5237    only read each abbrev table once.  In one program there are 200K TUs
5238    sharing 8K abbrev tables.
5239 
5240    The main purpose of this function is to support building the
5241    dwarf2_per_objfile->type_unit_groups table.
5242    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5243    can collapse the search space by grouping them by stmt_list.
5244    The savings can be significant, in the same program from above the 200K TUs
5245    share 8K stmt_list tables.
5246 
5247    FUNC is expected to call get_type_unit_group, which will create the
5248    struct type_unit_group if necessary and add it to
5249    dwarf2_per_objfile->type_unit_groups.  */
5250 
5251 static void
5252 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5253 {
5254   struct objfile *objfile = dwarf2_per_objfile->objfile;
5255   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5256   struct cleanup *cleanups;
5257   struct abbrev_table *abbrev_table;
5258   sect_offset abbrev_offset;
5259   struct tu_abbrev_offset *sorted_by_abbrev;
5260   struct type_unit_group **iter;
5261   int i;
5262 
5263   /* It's up to the caller to not call us multiple times.  */
5264   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5265 
5266   if (dwarf2_per_objfile->n_type_units == 0)
5267     return;
5268 
5269   /* TUs typically share abbrev tables, and there can be way more TUs than
5270      abbrev tables.  Sort by abbrev table to reduce the number of times we
5271      read each abbrev table in.
5272      Alternatives are to punt or to maintain a cache of abbrev tables.
5273      This is simpler and efficient enough for now.
5274 
5275      Later we group TUs by their DW_AT_stmt_list value (as this defines the
5276      symtab to use).  Typically TUs with the same abbrev offset have the same
5277      stmt_list value too so in practice this should work well.
5278 
5279      The basic algorithm here is:
5280 
5281       sort TUs by abbrev table
5282       for each TU with same abbrev table:
5283 	read abbrev table if first user
5284 	read TU top level DIE
5285 	  [IWBN if DWO skeletons had DW_AT_stmt_list]
5286 	call FUNC  */
5287 
5288   if (dwarf2_read_debug)
5289     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5290 
5291   /* Sort in a separate table to maintain the order of all_type_units
5292      for .gdb_index: TU indices directly index all_type_units.  */
5293   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5294 			      dwarf2_per_objfile->n_type_units);
5295   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5296     {
5297       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5298 
5299       sorted_by_abbrev[i].sig_type = sig_type;
5300       sorted_by_abbrev[i].abbrev_offset =
5301 	read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5302 			    sig_type->per_cu.offset);
5303     }
5304   cleanups = make_cleanup (xfree, sorted_by_abbrev);
5305   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5306 	 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5307 
5308   /* Note: In the .gdb_index case, get_type_unit_group may have already been
5309      called any number of times, so we don't reset tu_stats here.  */
5310 
5311   abbrev_offset.sect_off = ~(unsigned) 0;
5312   abbrev_table = NULL;
5313   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5314 
5315   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5316     {
5317       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5318 
5319       /* Switch to the next abbrev table if necessary.  */
5320       if (abbrev_table == NULL
5321 	  || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5322 	{
5323 	  if (abbrev_table != NULL)
5324 	    {
5325 	      abbrev_table_free (abbrev_table);
5326 	      /* Reset to NULL in case abbrev_table_read_table throws
5327 		 an error: abbrev_table_free_cleanup will get called.  */
5328 	      abbrev_table = NULL;
5329 	    }
5330 	  abbrev_offset = tu->abbrev_offset;
5331 	  abbrev_table =
5332 	    abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5333 				     abbrev_offset);
5334 	  ++tu_stats->nr_uniq_abbrev_tables;
5335 	}
5336 
5337       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5338 			       func, data);
5339     }
5340 
5341   /* Create a vector of pointers to primary type units to make it easy to
5342      iterate over them and CUs.  See dw2_get_primary_cu.  */
5343   dwarf2_per_objfile->n_type_unit_groups =
5344     htab_elements (dwarf2_per_objfile->type_unit_groups);
5345   dwarf2_per_objfile->all_type_unit_groups =
5346     obstack_alloc (&objfile->objfile_obstack,
5347 		   dwarf2_per_objfile->n_type_unit_groups
5348 		   * sizeof (struct type_unit_group *));
5349   iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5350   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5351 			  add_type_unit_group_to_table, &iter);
5352   gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5353 	      == dwarf2_per_objfile->n_type_unit_groups);
5354 
5355   do_cleanups (cleanups);
5356 
5357   if (dwarf2_read_debug)
5358     {
5359       fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5360       fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
5361 			  dwarf2_per_objfile->n_type_units);
5362       fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
5363 			  tu_stats->nr_uniq_abbrev_tables);
5364       fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
5365 			  tu_stats->nr_symtabs);
5366       fprintf_unfiltered (gdb_stdlog, "  %d symtab sharers\n",
5367 			  tu_stats->nr_symtab_sharers);
5368       fprintf_unfiltered (gdb_stdlog, "  %d type units without a stmt_list\n",
5369 			  tu_stats->nr_stmt_less_type_units);
5370     }
5371 }
5372 
5373 /* Reader function for build_type_psymtabs.  */
5374 
5375 static void
5376 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5377 			    gdb_byte *info_ptr,
5378 			    struct die_info *type_unit_die,
5379 			    int has_children,
5380 			    void *data)
5381 {
5382   struct objfile *objfile = dwarf2_per_objfile->objfile;
5383   struct dwarf2_cu *cu = reader->cu;
5384   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5385   struct type_unit_group *tu_group;
5386   struct attribute *attr;
5387   struct partial_die_info *first_die;
5388   CORE_ADDR lowpc, highpc;
5389   struct partial_symtab *pst;
5390 
5391   gdb_assert (data == NULL);
5392 
5393   if (! has_children)
5394     return;
5395 
5396   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5397   tu_group = get_type_unit_group (cu, attr);
5398 
5399   VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5400 
5401   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5402   cu->list_in_scope = &file_symbols;
5403   pst = create_partial_symtab (per_cu, "");
5404   pst->anonymous = 1;
5405 
5406   first_die = load_partial_dies (reader, info_ptr, 1);
5407 
5408   lowpc = (CORE_ADDR) -1;
5409   highpc = (CORE_ADDR) 0;
5410   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5411 
5412   pst->n_global_syms = objfile->global_psymbols.next -
5413     (objfile->global_psymbols.list + pst->globals_offset);
5414   pst->n_static_syms = objfile->static_psymbols.next -
5415     (objfile->static_psymbols.list + pst->statics_offset);
5416   sort_pst_symbols (objfile, pst);
5417 }
5418 
5419 /* Traversal function for build_type_psymtabs.  */
5420 
5421 static int
5422 build_type_psymtab_dependencies (void **slot, void *info)
5423 {
5424   struct objfile *objfile = dwarf2_per_objfile->objfile;
5425   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5426   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5427   struct partial_symtab *pst = per_cu->v.psymtab;
5428   int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5429   struct dwarf2_per_cu_data *iter;
5430   int i;
5431 
5432   gdb_assert (len > 0);
5433 
5434   pst->number_of_dependencies = len;
5435   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5436 				     len * sizeof (struct psymtab *));
5437   for (i = 0;
5438        VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5439        ++i)
5440     {
5441       pst->dependencies[i] = iter->v.psymtab;
5442       iter->type_unit_group = tu_group;
5443     }
5444 
5445   VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5446 
5447   return 1;
5448 }
5449 
5450 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5451    Build partial symbol tables for the .debug_types comp-units.  */
5452 
5453 static void
5454 build_type_psymtabs (struct objfile *objfile)
5455 {
5456   if (! create_all_type_units (objfile))
5457     return;
5458 
5459   build_type_unit_groups (build_type_psymtabs_reader, NULL);
5460 
5461   /* Now that all TUs have been processed we can fill in the dependencies.  */
5462   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5463 			  build_type_psymtab_dependencies, NULL);
5464 }
5465 
5466 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
5467 
5468 static void
5469 psymtabs_addrmap_cleanup (void *o)
5470 {
5471   struct objfile *objfile = o;
5472 
5473   objfile->psymtabs_addrmap = NULL;
5474 }
5475 
5476 /* Compute the 'user' field for each psymtab in OBJFILE.  */
5477 
5478 static void
5479 set_partial_user (struct objfile *objfile)
5480 {
5481   int i;
5482 
5483   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5484     {
5485       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5486       struct partial_symtab *pst = per_cu->v.psymtab;
5487       int j;
5488 
5489       if (pst == NULL)
5490 	continue;
5491 
5492       for (j = 0; j < pst->number_of_dependencies; ++j)
5493 	{
5494 	  /* Set the 'user' field only if it is not already set.  */
5495 	  if (pst->dependencies[j]->user == NULL)
5496 	    pst->dependencies[j]->user = pst;
5497 	}
5498     }
5499 }
5500 
5501 /* Build the partial symbol table by doing a quick pass through the
5502    .debug_info and .debug_abbrev sections.  */
5503 
5504 static void
5505 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5506 {
5507   struct cleanup *back_to, *addrmap_cleanup;
5508   struct obstack temp_obstack;
5509   int i;
5510 
5511   if (dwarf2_read_debug)
5512     {
5513       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5514 			  objfile->name);
5515     }
5516 
5517   dwarf2_per_objfile->reading_partial_symbols = 1;
5518 
5519   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5520 
5521   /* Any cached compilation units will be linked by the per-objfile
5522      read_in_chain.  Make sure to free them when we're done.  */
5523   back_to = make_cleanup (free_cached_comp_units, NULL);
5524 
5525   build_type_psymtabs (objfile);
5526 
5527   create_all_comp_units (objfile);
5528 
5529   /* Create a temporary address map on a temporary obstack.  We later
5530      copy this to the final obstack.  */
5531   obstack_init (&temp_obstack);
5532   make_cleanup_obstack_free (&temp_obstack);
5533   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5534   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5535 
5536   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5537     {
5538       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5539 
5540       process_psymtab_comp_unit (per_cu, 0);
5541     }
5542 
5543   set_partial_user (objfile);
5544 
5545   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5546 						    &objfile->objfile_obstack);
5547   discard_cleanups (addrmap_cleanup);
5548 
5549   do_cleanups (back_to);
5550 
5551   if (dwarf2_read_debug)
5552     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5553 			objfile->name);
5554 }
5555 
5556 /* die_reader_func for load_partial_comp_unit.  */
5557 
5558 static void
5559 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5560 			       gdb_byte *info_ptr,
5561 			       struct die_info *comp_unit_die,
5562 			       int has_children,
5563 			       void *data)
5564 {
5565   struct dwarf2_cu *cu = reader->cu;
5566 
5567   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5568 
5569   /* Check if comp unit has_children.
5570      If so, read the rest of the partial symbols from this comp unit.
5571      If not, there's no more debug_info for this comp unit.  */
5572   if (has_children)
5573     load_partial_dies (reader, info_ptr, 0);
5574 }
5575 
5576 /* Load the partial DIEs for a secondary CU into memory.
5577    This is also used when rereading a primary CU with load_all_dies.  */
5578 
5579 static void
5580 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5581 {
5582   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5583 			   load_partial_comp_unit_reader, NULL);
5584 }
5585 
5586 static void
5587 read_comp_units_from_section (struct objfile *objfile,
5588 			      struct dwarf2_section_info *section,
5589 			      unsigned int is_dwz,
5590 			      int *n_allocated,
5591 			      int *n_comp_units,
5592 			      struct dwarf2_per_cu_data ***all_comp_units)
5593 {
5594   gdb_byte *info_ptr;
5595   bfd *abfd = section->asection->owner;
5596 
5597   dwarf2_read_section (objfile, section);
5598 
5599   info_ptr = section->buffer;
5600 
5601   while (info_ptr < section->buffer + section->size)
5602     {
5603       unsigned int length, initial_length_size;
5604       struct dwarf2_per_cu_data *this_cu;
5605       sect_offset offset;
5606 
5607       offset.sect_off = info_ptr - section->buffer;
5608 
5609       /* Read just enough information to find out where the next
5610 	 compilation unit is.  */
5611       length = read_initial_length (abfd, info_ptr, &initial_length_size);
5612 
5613       /* Save the compilation unit for later lookup.  */
5614       this_cu = obstack_alloc (&objfile->objfile_obstack,
5615 			       sizeof (struct dwarf2_per_cu_data));
5616       memset (this_cu, 0, sizeof (*this_cu));
5617       this_cu->offset = offset;
5618       this_cu->length = length + initial_length_size;
5619       this_cu->is_dwz = is_dwz;
5620       this_cu->objfile = objfile;
5621       this_cu->info_or_types_section = section;
5622 
5623       if (*n_comp_units == *n_allocated)
5624 	{
5625 	  *n_allocated *= 2;
5626 	  *all_comp_units = xrealloc (*all_comp_units,
5627 				      *n_allocated
5628 				      * sizeof (struct dwarf2_per_cu_data *));
5629 	}
5630       (*all_comp_units)[*n_comp_units] = this_cu;
5631       ++*n_comp_units;
5632 
5633       info_ptr = info_ptr + this_cu->length;
5634     }
5635 }
5636 
5637 /* Create a list of all compilation units in OBJFILE.
5638    This is only done for -readnow and building partial symtabs.  */
5639 
5640 static void
5641 create_all_comp_units (struct objfile *objfile)
5642 {
5643   int n_allocated;
5644   int n_comp_units;
5645   struct dwarf2_per_cu_data **all_comp_units;
5646 
5647   n_comp_units = 0;
5648   n_allocated = 10;
5649   all_comp_units = xmalloc (n_allocated
5650 			    * sizeof (struct dwarf2_per_cu_data *));
5651 
5652   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5653 				&n_allocated, &n_comp_units, &all_comp_units);
5654 
5655   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5656     {
5657       struct dwz_file *dwz = dwarf2_get_dwz_file ();
5658 
5659       read_comp_units_from_section (objfile, &dwz->info, 1,
5660 				    &n_allocated, &n_comp_units,
5661 				    &all_comp_units);
5662     }
5663 
5664   dwarf2_per_objfile->all_comp_units
5665     = obstack_alloc (&objfile->objfile_obstack,
5666 		     n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5667   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5668 	  n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5669   xfree (all_comp_units);
5670   dwarf2_per_objfile->n_comp_units = n_comp_units;
5671 }
5672 
5673 /* Process all loaded DIEs for compilation unit CU, starting at
5674    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
5675    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5676    DW_AT_ranges).  If NEED_PC is set, then this function will set
5677    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5678    and record the covered ranges in the addrmap.  */
5679 
5680 static void
5681 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5682 		      CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5683 {
5684   struct partial_die_info *pdi;
5685 
5686   /* Now, march along the PDI's, descending into ones which have
5687      interesting children but skipping the children of the other ones,
5688      until we reach the end of the compilation unit.  */
5689 
5690   pdi = first_die;
5691 
5692   while (pdi != NULL)
5693     {
5694       fixup_partial_die (pdi, cu);
5695 
5696       /* Anonymous namespaces or modules have no name but have interesting
5697 	 children, so we need to look at them.  Ditto for anonymous
5698 	 enums.  */
5699 
5700       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5701 	  || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5702 	  || pdi->tag == DW_TAG_imported_unit)
5703 	{
5704 	  switch (pdi->tag)
5705 	    {
5706 	    case DW_TAG_subprogram:
5707 	      add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5708 	      break;
5709 	    case DW_TAG_constant:
5710 	    case DW_TAG_variable:
5711 	    case DW_TAG_typedef:
5712 	    case DW_TAG_union_type:
5713 	      if (!pdi->is_declaration)
5714 		{
5715 		  add_partial_symbol (pdi, cu);
5716 		}
5717 	      break;
5718 	    case DW_TAG_class_type:
5719 	    case DW_TAG_interface_type:
5720 	    case DW_TAG_structure_type:
5721 	      if (!pdi->is_declaration)
5722 		{
5723 		  add_partial_symbol (pdi, cu);
5724 		}
5725 	      break;
5726 	    case DW_TAG_enumeration_type:
5727 	      if (!pdi->is_declaration)
5728 		add_partial_enumeration (pdi, cu);
5729 	      break;
5730 	    case DW_TAG_base_type:
5731             case DW_TAG_subrange_type:
5732 	      /* File scope base type definitions are added to the partial
5733 	         symbol table.  */
5734 	      add_partial_symbol (pdi, cu);
5735 	      break;
5736 	    case DW_TAG_namespace:
5737 	      add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5738 	      break;
5739 	    case DW_TAG_module:
5740 	      add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5741 	      break;
5742 	    case DW_TAG_imported_unit:
5743 	      {
5744 		struct dwarf2_per_cu_data *per_cu;
5745 
5746 		/* For now we don't handle imported units in type units.  */
5747 		if (cu->per_cu->is_debug_types)
5748 		  {
5749 		    error (_("Dwarf Error: DW_TAG_imported_unit is not"
5750 			     " supported in type units [in module %s]"),
5751 			   cu->objfile->name);
5752 		  }
5753 
5754 		per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5755 							   pdi->is_dwz,
5756 							   cu->objfile);
5757 
5758 		/* Go read the partial unit, if needed.  */
5759 		if (per_cu->v.psymtab == NULL)
5760 		  process_psymtab_comp_unit (per_cu, 1);
5761 
5762 		VEC_safe_push (dwarf2_per_cu_ptr,
5763 			       cu->per_cu->imported_symtabs, per_cu);
5764 	      }
5765 	      break;
5766 	    default:
5767 	      break;
5768 	    }
5769 	}
5770 
5771       /* If the die has a sibling, skip to the sibling.  */
5772 
5773       pdi = pdi->die_sibling;
5774     }
5775 }
5776 
5777 /* Functions used to compute the fully scoped name of a partial DIE.
5778 
5779    Normally, this is simple.  For C++, the parent DIE's fully scoped
5780    name is concatenated with "::" and the partial DIE's name.  For
5781    Java, the same thing occurs except that "." is used instead of "::".
5782    Enumerators are an exception; they use the scope of their parent
5783    enumeration type, i.e. the name of the enumeration type is not
5784    prepended to the enumerator.
5785 
5786    There are two complexities.  One is DW_AT_specification; in this
5787    case "parent" means the parent of the target of the specification,
5788    instead of the direct parent of the DIE.  The other is compilers
5789    which do not emit DW_TAG_namespace; in this case we try to guess
5790    the fully qualified name of structure types from their members'
5791    linkage names.  This must be done using the DIE's children rather
5792    than the children of any DW_AT_specification target.  We only need
5793    to do this for structures at the top level, i.e. if the target of
5794    any DW_AT_specification (if any; otherwise the DIE itself) does not
5795    have a parent.  */
5796 
5797 /* Compute the scope prefix associated with PDI's parent, in
5798    compilation unit CU.  The result will be allocated on CU's
5799    comp_unit_obstack, or a copy of the already allocated PDI->NAME
5800    field.  NULL is returned if no prefix is necessary.  */
5801 static const char *
5802 partial_die_parent_scope (struct partial_die_info *pdi,
5803 			  struct dwarf2_cu *cu)
5804 {
5805   const char *grandparent_scope;
5806   struct partial_die_info *parent, *real_pdi;
5807 
5808   /* We need to look at our parent DIE; if we have a DW_AT_specification,
5809      then this means the parent of the specification DIE.  */
5810 
5811   real_pdi = pdi;
5812   while (real_pdi->has_specification)
5813     real_pdi = find_partial_die (real_pdi->spec_offset,
5814 				 real_pdi->spec_is_dwz, cu);
5815 
5816   parent = real_pdi->die_parent;
5817   if (parent == NULL)
5818     return NULL;
5819 
5820   if (parent->scope_set)
5821     return parent->scope;
5822 
5823   fixup_partial_die (parent, cu);
5824 
5825   grandparent_scope = partial_die_parent_scope (parent, cu);
5826 
5827   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5828      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5829      Work around this problem here.  */
5830   if (cu->language == language_cplus
5831       && parent->tag == DW_TAG_namespace
5832       && strcmp (parent->name, "::") == 0
5833       && grandparent_scope == NULL)
5834     {
5835       parent->scope = NULL;
5836       parent->scope_set = 1;
5837       return NULL;
5838     }
5839 
5840   if (pdi->tag == DW_TAG_enumerator)
5841     /* Enumerators should not get the name of the enumeration as a prefix.  */
5842     parent->scope = grandparent_scope;
5843   else if (parent->tag == DW_TAG_namespace
5844       || parent->tag == DW_TAG_module
5845       || parent->tag == DW_TAG_structure_type
5846       || parent->tag == DW_TAG_class_type
5847       || parent->tag == DW_TAG_interface_type
5848       || parent->tag == DW_TAG_union_type
5849       || parent->tag == DW_TAG_enumeration_type)
5850     {
5851       if (grandparent_scope == NULL)
5852 	parent->scope = parent->name;
5853       else
5854 	parent->scope = typename_concat (&cu->comp_unit_obstack,
5855 					 grandparent_scope,
5856 					 parent->name, 0, cu);
5857     }
5858   else
5859     {
5860       /* FIXME drow/2004-04-01: What should we be doing with
5861 	 function-local names?  For partial symbols, we should probably be
5862 	 ignoring them.  */
5863       complaint (&symfile_complaints,
5864 		 _("unhandled containing DIE tag %d for DIE at %d"),
5865 		 parent->tag, pdi->offset.sect_off);
5866       parent->scope = grandparent_scope;
5867     }
5868 
5869   parent->scope_set = 1;
5870   return parent->scope;
5871 }
5872 
5873 /* Return the fully scoped name associated with PDI, from compilation unit
5874    CU.  The result will be allocated with malloc.  */
5875 
5876 static char *
5877 partial_die_full_name (struct partial_die_info *pdi,
5878 		       struct dwarf2_cu *cu)
5879 {
5880   const char *parent_scope;
5881 
5882   /* If this is a template instantiation, we can not work out the
5883      template arguments from partial DIEs.  So, unfortunately, we have
5884      to go through the full DIEs.  At least any work we do building
5885      types here will be reused if full symbols are loaded later.  */
5886   if (pdi->has_template_arguments)
5887     {
5888       fixup_partial_die (pdi, cu);
5889 
5890       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5891 	{
5892 	  struct die_info *die;
5893 	  struct attribute attr;
5894 	  struct dwarf2_cu *ref_cu = cu;
5895 
5896 	  /* DW_FORM_ref_addr is using section offset.  */
5897 	  attr.name = 0;
5898 	  attr.form = DW_FORM_ref_addr;
5899 	  attr.u.unsnd = pdi->offset.sect_off;
5900 	  die = follow_die_ref (NULL, &attr, &ref_cu);
5901 
5902 	  return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5903 	}
5904     }
5905 
5906   parent_scope = partial_die_parent_scope (pdi, cu);
5907   if (parent_scope == NULL)
5908     return NULL;
5909   else
5910     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5911 }
5912 
5913 static void
5914 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5915 {
5916   struct objfile *objfile = cu->objfile;
5917   CORE_ADDR addr = 0;
5918   const char *actual_name = NULL;
5919   CORE_ADDR baseaddr;
5920   char *built_actual_name;
5921 
5922   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5923 
5924   built_actual_name = partial_die_full_name (pdi, cu);
5925   if (built_actual_name != NULL)
5926     actual_name = built_actual_name;
5927 
5928   if (actual_name == NULL)
5929     actual_name = pdi->name;
5930 
5931   switch (pdi->tag)
5932     {
5933     case DW_TAG_subprogram:
5934       if (pdi->is_external || cu->language == language_ada)
5935 	{
5936           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5937              of the global scope.  But in Ada, we want to be able to access
5938              nested procedures globally.  So all Ada subprograms are stored
5939              in the global scope.  */
5940 	  /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5941 	     mst_text, objfile); */
5942 	  add_psymbol_to_list (actual_name, strlen (actual_name),
5943 			       built_actual_name != NULL,
5944 			       VAR_DOMAIN, LOC_BLOCK,
5945 			       &objfile->global_psymbols,
5946 			       0, pdi->lowpc + baseaddr,
5947 			       cu->language, objfile);
5948 	}
5949       else
5950 	{
5951 	  /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5952 	     mst_file_text, objfile); */
5953 	  add_psymbol_to_list (actual_name, strlen (actual_name),
5954 			       built_actual_name != NULL,
5955 			       VAR_DOMAIN, LOC_BLOCK,
5956 			       &objfile->static_psymbols,
5957 			       0, pdi->lowpc + baseaddr,
5958 			       cu->language, objfile);
5959 	}
5960       break;
5961     case DW_TAG_constant:
5962       {
5963         struct psymbol_allocation_list *list;
5964 
5965 	if (pdi->is_external)
5966 	  list = &objfile->global_psymbols;
5967 	else
5968 	  list = &objfile->static_psymbols;
5969 	add_psymbol_to_list (actual_name, strlen (actual_name),
5970 			     built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
5971 			     list, 0, 0, cu->language, objfile);
5972       }
5973       break;
5974     case DW_TAG_variable:
5975       if (pdi->d.locdesc)
5976 	addr = decode_locdesc (pdi->d.locdesc, cu);
5977 
5978       if (pdi->d.locdesc
5979 	  && addr == 0
5980 	  && !dwarf2_per_objfile->has_section_at_zero)
5981 	{
5982 	  /* A global or static variable may also have been stripped
5983 	     out by the linker if unused, in which case its address
5984 	     will be nullified; do not add such variables into partial
5985 	     symbol table then.  */
5986 	}
5987       else if (pdi->is_external)
5988 	{
5989 	  /* Global Variable.
5990 	     Don't enter into the minimal symbol tables as there is
5991 	     a minimal symbol table entry from the ELF symbols already.
5992 	     Enter into partial symbol table if it has a location
5993 	     descriptor or a type.
5994 	     If the location descriptor is missing, new_symbol will create
5995 	     a LOC_UNRESOLVED symbol, the address of the variable will then
5996 	     be determined from the minimal symbol table whenever the variable
5997 	     is referenced.
5998 	     The address for the partial symbol table entry is not
5999 	     used by GDB, but it comes in handy for debugging partial symbol
6000 	     table building.  */
6001 
6002 	  if (pdi->d.locdesc || pdi->has_type)
6003 	    add_psymbol_to_list (actual_name, strlen (actual_name),
6004 				 built_actual_name != NULL,
6005 				 VAR_DOMAIN, LOC_STATIC,
6006 				 &objfile->global_psymbols,
6007 				 0, addr + baseaddr,
6008 				 cu->language, objfile);
6009 	}
6010       else
6011 	{
6012 	  /* Static Variable.  Skip symbols without location descriptors.  */
6013 	  if (pdi->d.locdesc == NULL)
6014 	    {
6015 	      xfree (built_actual_name);
6016 	      return;
6017 	    }
6018 	  /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6019 	     mst_file_data, objfile); */
6020 	  add_psymbol_to_list (actual_name, strlen (actual_name),
6021 			       built_actual_name != NULL,
6022 			       VAR_DOMAIN, LOC_STATIC,
6023 			       &objfile->static_psymbols,
6024 			       0, addr + baseaddr,
6025 			       cu->language, objfile);
6026 	}
6027       break;
6028     case DW_TAG_typedef:
6029     case DW_TAG_base_type:
6030     case DW_TAG_subrange_type:
6031       add_psymbol_to_list (actual_name, strlen (actual_name),
6032 			   built_actual_name != NULL,
6033 			   VAR_DOMAIN, LOC_TYPEDEF,
6034 			   &objfile->static_psymbols,
6035 			   0, (CORE_ADDR) 0, cu->language, objfile);
6036       break;
6037     case DW_TAG_namespace:
6038       add_psymbol_to_list (actual_name, strlen (actual_name),
6039 			   built_actual_name != NULL,
6040 			   VAR_DOMAIN, LOC_TYPEDEF,
6041 			   &objfile->global_psymbols,
6042 			   0, (CORE_ADDR) 0, cu->language, objfile);
6043       break;
6044     case DW_TAG_class_type:
6045     case DW_TAG_interface_type:
6046     case DW_TAG_structure_type:
6047     case DW_TAG_union_type:
6048     case DW_TAG_enumeration_type:
6049       /* Skip external references.  The DWARF standard says in the section
6050          about "Structure, Union, and Class Type Entries": "An incomplete
6051          structure, union or class type is represented by a structure,
6052          union or class entry that does not have a byte size attribute
6053          and that has a DW_AT_declaration attribute."  */
6054       if (!pdi->has_byte_size && pdi->is_declaration)
6055 	{
6056 	  xfree (built_actual_name);
6057 	  return;
6058 	}
6059 
6060       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6061 	 static vs. global.  */
6062       add_psymbol_to_list (actual_name, strlen (actual_name),
6063 			   built_actual_name != NULL,
6064 			   STRUCT_DOMAIN, LOC_TYPEDEF,
6065 			   (cu->language == language_cplus
6066 			    || cu->language == language_java)
6067 			   ? &objfile->global_psymbols
6068 			   : &objfile->static_psymbols,
6069 			   0, (CORE_ADDR) 0, cu->language, objfile);
6070 
6071       break;
6072     case DW_TAG_enumerator:
6073       add_psymbol_to_list (actual_name, strlen (actual_name),
6074 			   built_actual_name != NULL,
6075 			   VAR_DOMAIN, LOC_CONST,
6076 			   (cu->language == language_cplus
6077 			    || cu->language == language_java)
6078 			   ? &objfile->global_psymbols
6079 			   : &objfile->static_psymbols,
6080 			   0, (CORE_ADDR) 0, cu->language, objfile);
6081       break;
6082     default:
6083       break;
6084     }
6085 
6086   xfree (built_actual_name);
6087 }
6088 
6089 /* Read a partial die corresponding to a namespace; also, add a symbol
6090    corresponding to that namespace to the symbol table.  NAMESPACE is
6091    the name of the enclosing namespace.  */
6092 
6093 static void
6094 add_partial_namespace (struct partial_die_info *pdi,
6095 		       CORE_ADDR *lowpc, CORE_ADDR *highpc,
6096 		       int need_pc, struct dwarf2_cu *cu)
6097 {
6098   /* Add a symbol for the namespace.  */
6099 
6100   add_partial_symbol (pdi, cu);
6101 
6102   /* Now scan partial symbols in that namespace.  */
6103 
6104   if (pdi->has_children)
6105     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6106 }
6107 
6108 /* Read a partial die corresponding to a Fortran module.  */
6109 
6110 static void
6111 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6112 		    CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6113 {
6114   /* Now scan partial symbols in that module.  */
6115 
6116   if (pdi->has_children)
6117     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6118 }
6119 
6120 /* Read a partial die corresponding to a subprogram and create a partial
6121    symbol for that subprogram.  When the CU language allows it, this
6122    routine also defines a partial symbol for each nested subprogram
6123    that this subprogram contains.
6124 
6125    DIE my also be a lexical block, in which case we simply search
6126    recursively for suprograms defined inside that lexical block.
6127    Again, this is only performed when the CU language allows this
6128    type of definitions.  */
6129 
6130 static void
6131 add_partial_subprogram (struct partial_die_info *pdi,
6132 			CORE_ADDR *lowpc, CORE_ADDR *highpc,
6133 			int need_pc, struct dwarf2_cu *cu)
6134 {
6135   if (pdi->tag == DW_TAG_subprogram)
6136     {
6137       if (pdi->has_pc_info)
6138         {
6139           if (pdi->lowpc < *lowpc)
6140             *lowpc = pdi->lowpc;
6141           if (pdi->highpc > *highpc)
6142             *highpc = pdi->highpc;
6143 	  if (need_pc)
6144 	    {
6145 	      CORE_ADDR baseaddr;
6146 	      struct objfile *objfile = cu->objfile;
6147 
6148 	      baseaddr = ANOFFSET (objfile->section_offsets,
6149 				   SECT_OFF_TEXT (objfile));
6150 	      addrmap_set_empty (objfile->psymtabs_addrmap,
6151 				 pdi->lowpc + baseaddr,
6152 				 pdi->highpc - 1 + baseaddr,
6153 				 cu->per_cu->v.psymtab);
6154 	    }
6155         }
6156 
6157       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6158 	{
6159           if (!pdi->is_declaration)
6160 	    /* Ignore subprogram DIEs that do not have a name, they are
6161 	       illegal.  Do not emit a complaint at this point, we will
6162 	       do so when we convert this psymtab into a symtab.  */
6163 	    if (pdi->name)
6164 	      add_partial_symbol (pdi, cu);
6165         }
6166     }
6167 
6168   if (! pdi->has_children)
6169     return;
6170 
6171   if (cu->language == language_ada)
6172     {
6173       pdi = pdi->die_child;
6174       while (pdi != NULL)
6175 	{
6176 	  fixup_partial_die (pdi, cu);
6177 	  if (pdi->tag == DW_TAG_subprogram
6178 	      || pdi->tag == DW_TAG_lexical_block)
6179 	    add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6180 	  pdi = pdi->die_sibling;
6181 	}
6182     }
6183 }
6184 
6185 /* Read a partial die corresponding to an enumeration type.  */
6186 
6187 static void
6188 add_partial_enumeration (struct partial_die_info *enum_pdi,
6189 			 struct dwarf2_cu *cu)
6190 {
6191   struct partial_die_info *pdi;
6192 
6193   if (enum_pdi->name != NULL)
6194     add_partial_symbol (enum_pdi, cu);
6195 
6196   pdi = enum_pdi->die_child;
6197   while (pdi)
6198     {
6199       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6200 	complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6201       else
6202 	add_partial_symbol (pdi, cu);
6203       pdi = pdi->die_sibling;
6204     }
6205 }
6206 
6207 /* Return the initial uleb128 in the die at INFO_PTR.  */
6208 
6209 static unsigned int
6210 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6211 {
6212   unsigned int bytes_read;
6213 
6214   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6215 }
6216 
6217 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6218    Return the corresponding abbrev, or NULL if the number is zero (indicating
6219    an empty DIE).  In either case *BYTES_READ will be set to the length of
6220    the initial number.  */
6221 
6222 static struct abbrev_info *
6223 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6224 		 struct dwarf2_cu *cu)
6225 {
6226   bfd *abfd = cu->objfile->obfd;
6227   unsigned int abbrev_number;
6228   struct abbrev_info *abbrev;
6229 
6230   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6231 
6232   if (abbrev_number == 0)
6233     return NULL;
6234 
6235   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6236   if (!abbrev)
6237     {
6238       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6239 	     abbrev_number, bfd_get_filename (abfd));
6240     }
6241 
6242   return abbrev;
6243 }
6244 
6245 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6246    Returns a pointer to the end of a series of DIEs, terminated by an empty
6247    DIE.  Any children of the skipped DIEs will also be skipped.  */
6248 
6249 static gdb_byte *
6250 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6251 {
6252   struct dwarf2_cu *cu = reader->cu;
6253   struct abbrev_info *abbrev;
6254   unsigned int bytes_read;
6255 
6256   while (1)
6257     {
6258       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6259       if (abbrev == NULL)
6260 	return info_ptr + bytes_read;
6261       else
6262 	info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6263     }
6264 }
6265 
6266 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6267    INFO_PTR should point just after the initial uleb128 of a DIE, and the
6268    abbrev corresponding to that skipped uleb128 should be passed in
6269    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
6270    children.  */
6271 
6272 static gdb_byte *
6273 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6274 	      struct abbrev_info *abbrev)
6275 {
6276   unsigned int bytes_read;
6277   struct attribute attr;
6278   bfd *abfd = reader->abfd;
6279   struct dwarf2_cu *cu = reader->cu;
6280   gdb_byte *buffer = reader->buffer;
6281   const gdb_byte *buffer_end = reader->buffer_end;
6282   gdb_byte *start_info_ptr = info_ptr;
6283   unsigned int form, i;
6284 
6285   for (i = 0; i < abbrev->num_attrs; i++)
6286     {
6287       /* The only abbrev we care about is DW_AT_sibling.  */
6288       if (abbrev->attrs[i].name == DW_AT_sibling)
6289 	{
6290 	  read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6291 	  if (attr.form == DW_FORM_ref_addr)
6292 	    complaint (&symfile_complaints,
6293 		       _("ignoring absolute DW_AT_sibling"));
6294 	  else
6295 	    return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6296 	}
6297 
6298       /* If it isn't DW_AT_sibling, skip this attribute.  */
6299       form = abbrev->attrs[i].form;
6300     skip_attribute:
6301       switch (form)
6302 	{
6303 	case DW_FORM_ref_addr:
6304 	  /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6305 	     and later it is offset sized.  */
6306 	  if (cu->header.version == 2)
6307 	    info_ptr += cu->header.addr_size;
6308 	  else
6309 	    info_ptr += cu->header.offset_size;
6310 	  break;
6311 	case DW_FORM_GNU_ref_alt:
6312 	  info_ptr += cu->header.offset_size;
6313 	  break;
6314 	case DW_FORM_addr:
6315 	  info_ptr += cu->header.addr_size;
6316 	  break;
6317 	case DW_FORM_data1:
6318 	case DW_FORM_ref1:
6319 	case DW_FORM_flag:
6320 	  info_ptr += 1;
6321 	  break;
6322 	case DW_FORM_flag_present:
6323 	  break;
6324 	case DW_FORM_data2:
6325 	case DW_FORM_ref2:
6326 	  info_ptr += 2;
6327 	  break;
6328 	case DW_FORM_data4:
6329 	case DW_FORM_ref4:
6330 	  info_ptr += 4;
6331 	  break;
6332 	case DW_FORM_data8:
6333 	case DW_FORM_ref8:
6334 	case DW_FORM_ref_sig8:
6335 	  info_ptr += 8;
6336 	  break;
6337 	case DW_FORM_string:
6338 	  read_direct_string (abfd, info_ptr, &bytes_read);
6339 	  info_ptr += bytes_read;
6340 	  break;
6341 	case DW_FORM_sec_offset:
6342 	case DW_FORM_strp:
6343 	case DW_FORM_GNU_strp_alt:
6344 	  info_ptr += cu->header.offset_size;
6345 	  break;
6346 	case DW_FORM_exprloc:
6347 	case DW_FORM_block:
6348 	  info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6349 	  info_ptr += bytes_read;
6350 	  break;
6351 	case DW_FORM_block1:
6352 	  info_ptr += 1 + read_1_byte (abfd, info_ptr);
6353 	  break;
6354 	case DW_FORM_block2:
6355 	  info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6356 	  break;
6357 	case DW_FORM_block4:
6358 	  info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6359 	  break;
6360 	case DW_FORM_sdata:
6361 	case DW_FORM_udata:
6362 	case DW_FORM_ref_udata:
6363 	case DW_FORM_GNU_addr_index:
6364 	case DW_FORM_GNU_str_index:
6365 	  info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6366 	  break;
6367 	case DW_FORM_indirect:
6368 	  form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6369 	  info_ptr += bytes_read;
6370 	  /* We need to continue parsing from here, so just go back to
6371 	     the top.  */
6372 	  goto skip_attribute;
6373 
6374 	default:
6375 	  error (_("Dwarf Error: Cannot handle %s "
6376 		   "in DWARF reader [in module %s]"),
6377 		 dwarf_form_name (form),
6378 		 bfd_get_filename (abfd));
6379 	}
6380     }
6381 
6382   if (abbrev->has_children)
6383     return skip_children (reader, info_ptr);
6384   else
6385     return info_ptr;
6386 }
6387 
6388 /* Locate ORIG_PDI's sibling.
6389    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */
6390 
6391 static gdb_byte *
6392 locate_pdi_sibling (const struct die_reader_specs *reader,
6393 		    struct partial_die_info *orig_pdi,
6394 		    gdb_byte *info_ptr)
6395 {
6396   /* Do we know the sibling already?  */
6397 
6398   if (orig_pdi->sibling)
6399     return orig_pdi->sibling;
6400 
6401   /* Are there any children to deal with?  */
6402 
6403   if (!orig_pdi->has_children)
6404     return info_ptr;
6405 
6406   /* Skip the children the long way.  */
6407 
6408   return skip_children (reader, info_ptr);
6409 }
6410 
6411 /* Expand this partial symbol table into a full symbol table.  SELF is
6412    not NULL.  */
6413 
6414 static void
6415 dwarf2_read_symtab (struct partial_symtab *self,
6416 		    struct objfile *objfile)
6417 {
6418   if (self->readin)
6419     {
6420       warning (_("bug: psymtab for %s is already read in."),
6421 	       self->filename);
6422     }
6423   else
6424     {
6425       if (info_verbose)
6426 	{
6427 	  printf_filtered (_("Reading in symbols for %s..."),
6428 			   self->filename);
6429 	  gdb_flush (gdb_stdout);
6430 	}
6431 
6432       /* Restore our global data.  */
6433       dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6434 
6435       /* If this psymtab is constructed from a debug-only objfile, the
6436 	 has_section_at_zero flag will not necessarily be correct.  We
6437 	 can get the correct value for this flag by looking at the data
6438 	 associated with the (presumably stripped) associated objfile.  */
6439       if (objfile->separate_debug_objfile_backlink)
6440 	{
6441 	  struct dwarf2_per_objfile *dpo_backlink
6442 	    = objfile_data (objfile->separate_debug_objfile_backlink,
6443 			    dwarf2_objfile_data_key);
6444 
6445 	  dwarf2_per_objfile->has_section_at_zero
6446 	    = dpo_backlink->has_section_at_zero;
6447 	}
6448 
6449       dwarf2_per_objfile->reading_partial_symbols = 0;
6450 
6451       psymtab_to_symtab_1 (self);
6452 
6453       /* Finish up the debug error message.  */
6454       if (info_verbose)
6455 	printf_filtered (_("done.\n"));
6456     }
6457 
6458   process_cu_includes ();
6459 }
6460 
6461 /* Reading in full CUs.  */
6462 
6463 /* Add PER_CU to the queue.  */
6464 
6465 static void
6466 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6467 		 enum language pretend_language)
6468 {
6469   struct dwarf2_queue_item *item;
6470 
6471   per_cu->queued = 1;
6472   item = xmalloc (sizeof (*item));
6473   item->per_cu = per_cu;
6474   item->pretend_language = pretend_language;
6475   item->next = NULL;
6476 
6477   if (dwarf2_queue == NULL)
6478     dwarf2_queue = item;
6479   else
6480     dwarf2_queue_tail->next = item;
6481 
6482   dwarf2_queue_tail = item;
6483 }
6484 
6485 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
6486    unit and add it to our queue.
6487    The result is non-zero if PER_CU was queued, otherwise the result is zero
6488    meaning either PER_CU is already queued or it is already loaded.  */
6489 
6490 static int
6491 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6492 		       struct dwarf2_per_cu_data *per_cu,
6493 		       enum language pretend_language)
6494 {
6495   /* We may arrive here during partial symbol reading, if we need full
6496      DIEs to process an unusual case (e.g. template arguments).  Do
6497      not queue PER_CU, just tell our caller to load its DIEs.  */
6498   if (dwarf2_per_objfile->reading_partial_symbols)
6499     {
6500       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6501 	return 1;
6502       return 0;
6503     }
6504 
6505   /* Mark the dependence relation so that we don't flush PER_CU
6506      too early.  */
6507   dwarf2_add_dependence (this_cu, per_cu);
6508 
6509   /* If it's already on the queue, we have nothing to do.  */
6510   if (per_cu->queued)
6511     return 0;
6512 
6513   /* If the compilation unit is already loaded, just mark it as
6514      used.  */
6515   if (per_cu->cu != NULL)
6516     {
6517       per_cu->cu->last_used = 0;
6518       return 0;
6519     }
6520 
6521   /* Add it to the queue.  */
6522   queue_comp_unit (per_cu, pretend_language);
6523 
6524   return 1;
6525 }
6526 
6527 /* Process the queue.  */
6528 
6529 static void
6530 process_queue (void)
6531 {
6532   struct dwarf2_queue_item *item, *next_item;
6533 
6534   if (dwarf2_read_debug)
6535     {
6536       fprintf_unfiltered (gdb_stdlog,
6537 			  "Expanding one or more symtabs of objfile %s ...\n",
6538 			  dwarf2_per_objfile->objfile->name);
6539     }
6540 
6541   /* The queue starts out with one item, but following a DIE reference
6542      may load a new CU, adding it to the end of the queue.  */
6543   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6544     {
6545       if (dwarf2_per_objfile->using_index
6546 	  ? !item->per_cu->v.quick->symtab
6547 	  : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6548 	{
6549 	  struct dwarf2_per_cu_data *per_cu = item->per_cu;
6550 
6551 	  if (dwarf2_read_debug)
6552 	    {
6553 	      fprintf_unfiltered (gdb_stdlog,
6554 				  "Expanding symtab of %s at offset 0x%x\n",
6555 				  per_cu->is_debug_types ? "TU" : "CU",
6556 				  per_cu->offset.sect_off);
6557 	    }
6558 
6559 	  if (per_cu->is_debug_types)
6560 	    process_full_type_unit (per_cu, item->pretend_language);
6561 	  else
6562 	    process_full_comp_unit (per_cu, item->pretend_language);
6563 
6564 	  if (dwarf2_read_debug)
6565 	    {
6566 	      fprintf_unfiltered (gdb_stdlog,
6567 				  "Done expanding %s at offset 0x%x\n",
6568 				  per_cu->is_debug_types ? "TU" : "CU",
6569 				  per_cu->offset.sect_off);
6570 	    }
6571 	}
6572 
6573       item->per_cu->queued = 0;
6574       next_item = item->next;
6575       xfree (item);
6576     }
6577 
6578   dwarf2_queue_tail = NULL;
6579 
6580   if (dwarf2_read_debug)
6581     {
6582       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6583 			  dwarf2_per_objfile->objfile->name);
6584     }
6585 }
6586 
6587 /* Free all allocated queue entries.  This function only releases anything if
6588    an error was thrown; if the queue was processed then it would have been
6589    freed as we went along.  */
6590 
6591 static void
6592 dwarf2_release_queue (void *dummy)
6593 {
6594   struct dwarf2_queue_item *item, *last;
6595 
6596   item = dwarf2_queue;
6597   while (item)
6598     {
6599       /* Anything still marked queued is likely to be in an
6600 	 inconsistent state, so discard it.  */
6601       if (item->per_cu->queued)
6602 	{
6603 	  if (item->per_cu->cu != NULL)
6604 	    free_one_cached_comp_unit (item->per_cu);
6605 	  item->per_cu->queued = 0;
6606 	}
6607 
6608       last = item;
6609       item = item->next;
6610       xfree (last);
6611     }
6612 
6613   dwarf2_queue = dwarf2_queue_tail = NULL;
6614 }
6615 
6616 /* Read in full symbols for PST, and anything it depends on.  */
6617 
6618 static void
6619 psymtab_to_symtab_1 (struct partial_symtab *pst)
6620 {
6621   struct dwarf2_per_cu_data *per_cu;
6622   int i;
6623 
6624   if (pst->readin)
6625     return;
6626 
6627   for (i = 0; i < pst->number_of_dependencies; i++)
6628     if (!pst->dependencies[i]->readin
6629 	&& pst->dependencies[i]->user == NULL)
6630       {
6631         /* Inform about additional files that need to be read in.  */
6632         if (info_verbose)
6633           {
6634 	    /* FIXME: i18n: Need to make this a single string.  */
6635             fputs_filtered (" ", gdb_stdout);
6636             wrap_here ("");
6637             fputs_filtered ("and ", gdb_stdout);
6638             wrap_here ("");
6639             printf_filtered ("%s...", pst->dependencies[i]->filename);
6640             wrap_here ("");     /* Flush output.  */
6641             gdb_flush (gdb_stdout);
6642           }
6643         psymtab_to_symtab_1 (pst->dependencies[i]);
6644       }
6645 
6646   per_cu = pst->read_symtab_private;
6647 
6648   if (per_cu == NULL)
6649     {
6650       /* It's an include file, no symbols to read for it.
6651          Everything is in the parent symtab.  */
6652       pst->readin = 1;
6653       return;
6654     }
6655 
6656   dw2_do_instantiate_symtab (per_cu);
6657 }
6658 
6659 /* Trivial hash function for die_info: the hash value of a DIE
6660    is its offset in .debug_info for this objfile.  */
6661 
6662 static hashval_t
6663 die_hash (const void *item)
6664 {
6665   const struct die_info *die = item;
6666 
6667   return die->offset.sect_off;
6668 }
6669 
6670 /* Trivial comparison function for die_info structures: two DIEs
6671    are equal if they have the same offset.  */
6672 
6673 static int
6674 die_eq (const void *item_lhs, const void *item_rhs)
6675 {
6676   const struct die_info *die_lhs = item_lhs;
6677   const struct die_info *die_rhs = item_rhs;
6678 
6679   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6680 }
6681 
6682 /* die_reader_func for load_full_comp_unit.
6683    This is identical to read_signatured_type_reader,
6684    but is kept separate for now.  */
6685 
6686 static void
6687 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6688 			    gdb_byte *info_ptr,
6689 			    struct die_info *comp_unit_die,
6690 			    int has_children,
6691 			    void *data)
6692 {
6693   struct dwarf2_cu *cu = reader->cu;
6694   enum language *language_ptr = data;
6695 
6696   gdb_assert (cu->die_hash == NULL);
6697   cu->die_hash =
6698     htab_create_alloc_ex (cu->header.length / 12,
6699 			  die_hash,
6700 			  die_eq,
6701 			  NULL,
6702 			  &cu->comp_unit_obstack,
6703 			  hashtab_obstack_allocate,
6704 			  dummy_obstack_deallocate);
6705 
6706   if (has_children)
6707     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6708 						  &info_ptr, comp_unit_die);
6709   cu->dies = comp_unit_die;
6710   /* comp_unit_die is not stored in die_hash, no need.  */
6711 
6712   /* We try not to read any attributes in this function, because not
6713      all CUs needed for references have been loaded yet, and symbol
6714      table processing isn't initialized.  But we have to set the CU language,
6715      or we won't be able to build types correctly.
6716      Similarly, if we do not read the producer, we can not apply
6717      producer-specific interpretation.  */
6718   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6719 }
6720 
6721 /* Load the DIEs associated with PER_CU into memory.  */
6722 
6723 static void
6724 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6725 		     enum language pretend_language)
6726 {
6727   gdb_assert (! this_cu->is_debug_types);
6728 
6729   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6730 			   load_full_comp_unit_reader, &pretend_language);
6731 }
6732 
6733 /* Add a DIE to the delayed physname list.  */
6734 
6735 static void
6736 add_to_method_list (struct type *type, int fnfield_index, int index,
6737 		    const char *name, struct die_info *die,
6738 		    struct dwarf2_cu *cu)
6739 {
6740   struct delayed_method_info mi;
6741   mi.type = type;
6742   mi.fnfield_index = fnfield_index;
6743   mi.index = index;
6744   mi.name = name;
6745   mi.die = die;
6746   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6747 }
6748 
6749 /* A cleanup for freeing the delayed method list.  */
6750 
6751 static void
6752 free_delayed_list (void *ptr)
6753 {
6754   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6755   if (cu->method_list != NULL)
6756     {
6757       VEC_free (delayed_method_info, cu->method_list);
6758       cu->method_list = NULL;
6759     }
6760 }
6761 
6762 /* Compute the physnames of any methods on the CU's method list.
6763 
6764    The computation of method physnames is delayed in order to avoid the
6765    (bad) condition that one of the method's formal parameters is of an as yet
6766    incomplete type.  */
6767 
6768 static void
6769 compute_delayed_physnames (struct dwarf2_cu *cu)
6770 {
6771   int i;
6772   struct delayed_method_info *mi;
6773   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6774     {
6775       const char *physname;
6776       struct fn_fieldlist *fn_flp
6777 	= &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6778       physname = dwarf2_physname (mi->name, mi->die, cu);
6779       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6780     }
6781 }
6782 
6783 /* Go objects should be embedded in a DW_TAG_module DIE,
6784    and it's not clear if/how imported objects will appear.
6785    To keep Go support simple until that's worked out,
6786    go back through what we've read and create something usable.
6787    We could do this while processing each DIE, and feels kinda cleaner,
6788    but that way is more invasive.
6789    This is to, for example, allow the user to type "p var" or "b main"
6790    without having to specify the package name, and allow lookups
6791    of module.object to work in contexts that use the expression
6792    parser.  */
6793 
6794 static void
6795 fixup_go_packaging (struct dwarf2_cu *cu)
6796 {
6797   char *package_name = NULL;
6798   struct pending *list;
6799   int i;
6800 
6801   for (list = global_symbols; list != NULL; list = list->next)
6802     {
6803       for (i = 0; i < list->nsyms; ++i)
6804 	{
6805 	  struct symbol *sym = list->symbol[i];
6806 
6807 	  if (SYMBOL_LANGUAGE (sym) == language_go
6808 	      && SYMBOL_CLASS (sym) == LOC_BLOCK)
6809 	    {
6810 	      char *this_package_name = go_symbol_package_name (sym);
6811 
6812 	      if (this_package_name == NULL)
6813 		continue;
6814 	      if (package_name == NULL)
6815 		package_name = this_package_name;
6816 	      else
6817 		{
6818 		  if (strcmp (package_name, this_package_name) != 0)
6819 		    complaint (&symfile_complaints,
6820 			       _("Symtab %s has objects from two different Go packages: %s and %s"),
6821 			       (SYMBOL_SYMTAB (sym)
6822 			  ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6823 				: cu->objfile->name),
6824 			       this_package_name, package_name);
6825 		  xfree (this_package_name);
6826 		}
6827 	    }
6828 	}
6829     }
6830 
6831   if (package_name != NULL)
6832     {
6833       struct objfile *objfile = cu->objfile;
6834       const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6835 						      package_name,
6836 						      strlen (package_name));
6837       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6838 				     saved_package_name, objfile);
6839       struct symbol *sym;
6840 
6841       TYPE_TAG_NAME (type) = TYPE_NAME (type);
6842 
6843       sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6844       SYMBOL_SET_LANGUAGE (sym, language_go);
6845       SYMBOL_SET_NAMES (sym, saved_package_name,
6846 			strlen (saved_package_name), 0, objfile);
6847       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6848 	 e.g., "main" finds the "main" module and not C's main().  */
6849       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6850       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6851       SYMBOL_TYPE (sym) = type;
6852 
6853       add_symbol_to_list (sym, &global_symbols);
6854 
6855       xfree (package_name);
6856     }
6857 }
6858 
6859 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6860 
6861 /* Return the symtab for PER_CU.  This works properly regardless of
6862    whether we're using the index or psymtabs.  */
6863 
6864 static struct symtab *
6865 get_symtab (struct dwarf2_per_cu_data *per_cu)
6866 {
6867   return (dwarf2_per_objfile->using_index
6868 	  ? per_cu->v.quick->symtab
6869 	  : per_cu->v.psymtab->symtab);
6870 }
6871 
6872 /* A helper function for computing the list of all symbol tables
6873    included by PER_CU.  */
6874 
6875 static void
6876 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6877 				htab_t all_children,
6878 				struct dwarf2_per_cu_data *per_cu)
6879 {
6880   void **slot;
6881   int ix;
6882   struct dwarf2_per_cu_data *iter;
6883 
6884   slot = htab_find_slot (all_children, per_cu, INSERT);
6885   if (*slot != NULL)
6886     {
6887       /* This inclusion and its children have been processed.  */
6888       return;
6889     }
6890 
6891   *slot = per_cu;
6892   /* Only add a CU if it has a symbol table.  */
6893   if (get_symtab (per_cu) != NULL)
6894     VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6895 
6896   for (ix = 0;
6897        VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6898        ++ix)
6899     recursively_compute_inclusions (result, all_children, iter);
6900 }
6901 
6902 /* Compute the symtab 'includes' fields for the symtab related to
6903    PER_CU.  */
6904 
6905 static void
6906 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6907 {
6908   gdb_assert (! per_cu->is_debug_types);
6909 
6910   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6911     {
6912       int ix, len;
6913       struct dwarf2_per_cu_data *iter;
6914       VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6915       htab_t all_children;
6916       struct symtab *symtab = get_symtab (per_cu);
6917 
6918       /* If we don't have a symtab, we can just skip this case.  */
6919       if (symtab == NULL)
6920 	return;
6921 
6922       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6923 					NULL, xcalloc, xfree);
6924 
6925       for (ix = 0;
6926 	   VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6927 			ix, iter);
6928 	   ++ix)
6929 	recursively_compute_inclusions (&result_children, all_children, iter);
6930 
6931       /* Now we have a transitive closure of all the included CUs, and
6932 	 for .gdb_index version 7 the included TUs, so we can convert it
6933 	 to a list of symtabs.  */
6934       len = VEC_length (dwarf2_per_cu_ptr, result_children);
6935       symtab->includes
6936 	= obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6937 			 (len + 1) * sizeof (struct symtab *));
6938       for (ix = 0;
6939 	   VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6940 	   ++ix)
6941 	symtab->includes[ix] = get_symtab (iter);
6942       symtab->includes[len] = NULL;
6943 
6944       VEC_free (dwarf2_per_cu_ptr, result_children);
6945       htab_delete (all_children);
6946     }
6947 }
6948 
6949 /* Compute the 'includes' field for the symtabs of all the CUs we just
6950    read.  */
6951 
6952 static void
6953 process_cu_includes (void)
6954 {
6955   int ix;
6956   struct dwarf2_per_cu_data *iter;
6957 
6958   for (ix = 0;
6959        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6960 		    ix, iter);
6961        ++ix)
6962     {
6963       if (! iter->is_debug_types)
6964 	compute_symtab_includes (iter);
6965     }
6966 
6967   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6968 }
6969 
6970 /* Generate full symbol information for PER_CU, whose DIEs have
6971    already been loaded into memory.  */
6972 
6973 static void
6974 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6975 			enum language pretend_language)
6976 {
6977   struct dwarf2_cu *cu = per_cu->cu;
6978   struct objfile *objfile = per_cu->objfile;
6979   CORE_ADDR lowpc, highpc;
6980   struct symtab *symtab;
6981   struct cleanup *back_to, *delayed_list_cleanup;
6982   CORE_ADDR baseaddr;
6983   struct block *static_block;
6984 
6985   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6986 
6987   buildsym_init ();
6988   back_to = make_cleanup (really_free_pendings, NULL);
6989   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6990 
6991   cu->list_in_scope = &file_symbols;
6992 
6993   cu->language = pretend_language;
6994   cu->language_defn = language_def (cu->language);
6995 
6996   /* Do line number decoding in read_file_scope () */
6997   process_die (cu->dies, cu);
6998 
6999   /* For now fudge the Go package.  */
7000   if (cu->language == language_go)
7001     fixup_go_packaging (cu);
7002 
7003   /* Now that we have processed all the DIEs in the CU, all the types
7004      should be complete, and it should now be safe to compute all of the
7005      physnames.  */
7006   compute_delayed_physnames (cu);
7007   do_cleanups (delayed_list_cleanup);
7008 
7009   /* Some compilers don't define a DW_AT_high_pc attribute for the
7010      compilation unit.  If the DW_AT_high_pc is missing, synthesize
7011      it, by scanning the DIE's below the compilation unit.  */
7012   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7013 
7014   static_block
7015     = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7016 				   per_cu->imported_symtabs != NULL);
7017 
7018   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7019      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7020      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
7021      addrmap to help ensure it has an accurate map of pc values belonging to
7022      this comp unit.  */
7023   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7024 
7025   symtab = end_symtab_from_static_block (static_block, objfile,
7026 					 SECT_OFF_TEXT (objfile), 0);
7027 
7028   if (symtab != NULL)
7029     {
7030       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7031 
7032       /* Set symtab language to language from DW_AT_language.  If the
7033 	 compilation is from a C file generated by language preprocessors, do
7034 	 not set the language if it was already deduced by start_subfile.  */
7035       if (!(cu->language == language_c && symtab->language != language_c))
7036 	symtab->language = cu->language;
7037 
7038       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
7039 	 produce DW_AT_location with location lists but it can be possibly
7040 	 invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
7041 	 there were bugs in prologue debug info, fixed later in GCC-4.5
7042 	 by "unwind info for epilogues" patch (which is not directly related).
7043 
7044 	 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7045 	 needed, it would be wrong due to missing DW_AT_producer there.
7046 
7047 	 Still one can confuse GDB by using non-standard GCC compilation
7048 	 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7049 	 */
7050       if (cu->has_loclist && gcc_4_minor >= 5)
7051 	symtab->locations_valid = 1;
7052 
7053       if (gcc_4_minor >= 5)
7054 	symtab->epilogue_unwind_valid = 1;
7055 
7056       symtab->call_site_htab = cu->call_site_htab;
7057     }
7058 
7059   if (dwarf2_per_objfile->using_index)
7060     per_cu->v.quick->symtab = symtab;
7061   else
7062     {
7063       struct partial_symtab *pst = per_cu->v.psymtab;
7064       pst->symtab = symtab;
7065       pst->readin = 1;
7066     }
7067 
7068   /* Push it for inclusion processing later.  */
7069   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7070 
7071   do_cleanups (back_to);
7072 }
7073 
7074 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7075    already been loaded into memory.  */
7076 
7077 static void
7078 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7079 			enum language pretend_language)
7080 {
7081   struct dwarf2_cu *cu = per_cu->cu;
7082   struct objfile *objfile = per_cu->objfile;
7083   struct symtab *symtab;
7084   struct cleanup *back_to, *delayed_list_cleanup;
7085 
7086   buildsym_init ();
7087   back_to = make_cleanup (really_free_pendings, NULL);
7088   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7089 
7090   cu->list_in_scope = &file_symbols;
7091 
7092   cu->language = pretend_language;
7093   cu->language_defn = language_def (cu->language);
7094 
7095   /* The symbol tables are set up in read_type_unit_scope.  */
7096   process_die (cu->dies, cu);
7097 
7098   /* For now fudge the Go package.  */
7099   if (cu->language == language_go)
7100     fixup_go_packaging (cu);
7101 
7102   /* Now that we have processed all the DIEs in the CU, all the types
7103      should be complete, and it should now be safe to compute all of the
7104      physnames.  */
7105   compute_delayed_physnames (cu);
7106   do_cleanups (delayed_list_cleanup);
7107 
7108   /* TUs share symbol tables.
7109      If this is the first TU to use this symtab, complete the construction
7110      of it with end_expandable_symtab.  Otherwise, complete the addition of
7111      this TU's symbols to the existing symtab.  */
7112   if (per_cu->type_unit_group->primary_symtab == NULL)
7113     {
7114       symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7115       per_cu->type_unit_group->primary_symtab = symtab;
7116 
7117       if (symtab != NULL)
7118 	{
7119 	  /* Set symtab language to language from DW_AT_language.  If the
7120 	     compilation is from a C file generated by language preprocessors,
7121 	     do not set the language if it was already deduced by
7122 	     start_subfile.  */
7123 	  if (!(cu->language == language_c && symtab->language != language_c))
7124 	    symtab->language = cu->language;
7125 	}
7126     }
7127   else
7128     {
7129       augment_type_symtab (objfile,
7130 			   per_cu->type_unit_group->primary_symtab);
7131       symtab = per_cu->type_unit_group->primary_symtab;
7132     }
7133 
7134   if (dwarf2_per_objfile->using_index)
7135     per_cu->v.quick->symtab = symtab;
7136   else
7137     {
7138       struct partial_symtab *pst = per_cu->v.psymtab;
7139       pst->symtab = symtab;
7140       pst->readin = 1;
7141     }
7142 
7143   do_cleanups (back_to);
7144 }
7145 
7146 /* Process an imported unit DIE.  */
7147 
7148 static void
7149 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7150 {
7151   struct attribute *attr;
7152 
7153   /* For now we don't handle imported units in type units.  */
7154   if (cu->per_cu->is_debug_types)
7155     {
7156       error (_("Dwarf Error: DW_TAG_imported_unit is not"
7157 	       " supported in type units [in module %s]"),
7158 	     cu->objfile->name);
7159     }
7160 
7161   attr = dwarf2_attr (die, DW_AT_import, cu);
7162   if (attr != NULL)
7163     {
7164       struct dwarf2_per_cu_data *per_cu;
7165       struct symtab *imported_symtab;
7166       sect_offset offset;
7167       int is_dwz;
7168 
7169       offset = dwarf2_get_ref_die_offset (attr);
7170       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7171       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7172 
7173       /* Queue the unit, if needed.  */
7174       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7175 	load_full_comp_unit (per_cu, cu->language);
7176 
7177       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7178 		     per_cu);
7179     }
7180 }
7181 
7182 /* Process a die and its children.  */
7183 
7184 static void
7185 process_die (struct die_info *die, struct dwarf2_cu *cu)
7186 {
7187   switch (die->tag)
7188     {
7189     case DW_TAG_padding:
7190       break;
7191     case DW_TAG_compile_unit:
7192     case DW_TAG_partial_unit:
7193       read_file_scope (die, cu);
7194       break;
7195     case DW_TAG_type_unit:
7196       read_type_unit_scope (die, cu);
7197       break;
7198     case DW_TAG_subprogram:
7199     case DW_TAG_inlined_subroutine:
7200       read_func_scope (die, cu);
7201       break;
7202     case DW_TAG_lexical_block:
7203     case DW_TAG_try_block:
7204     case DW_TAG_catch_block:
7205       read_lexical_block_scope (die, cu);
7206       break;
7207     case DW_TAG_GNU_call_site:
7208       read_call_site_scope (die, cu);
7209       break;
7210     case DW_TAG_class_type:
7211     case DW_TAG_interface_type:
7212     case DW_TAG_structure_type:
7213     case DW_TAG_union_type:
7214       process_structure_scope (die, cu);
7215       break;
7216     case DW_TAG_enumeration_type:
7217       process_enumeration_scope (die, cu);
7218       break;
7219 
7220     /* These dies have a type, but processing them does not create
7221        a symbol or recurse to process the children.  Therefore we can
7222        read them on-demand through read_type_die.  */
7223     case DW_TAG_subroutine_type:
7224     case DW_TAG_set_type:
7225     case DW_TAG_array_type:
7226     case DW_TAG_pointer_type:
7227     case DW_TAG_ptr_to_member_type:
7228     case DW_TAG_reference_type:
7229     case DW_TAG_string_type:
7230       break;
7231 
7232     case DW_TAG_base_type:
7233     case DW_TAG_subrange_type:
7234     case DW_TAG_typedef:
7235       /* Add a typedef symbol for the type definition, if it has a
7236          DW_AT_name.  */
7237       new_symbol (die, read_type_die (die, cu), cu);
7238       break;
7239     case DW_TAG_common_block:
7240       read_common_block (die, cu);
7241       break;
7242     case DW_TAG_common_inclusion:
7243       break;
7244     case DW_TAG_namespace:
7245       cu->processing_has_namespace_info = 1;
7246       read_namespace (die, cu);
7247       break;
7248     case DW_TAG_module:
7249       cu->processing_has_namespace_info = 1;
7250       read_module (die, cu);
7251       break;
7252     case DW_TAG_imported_declaration:
7253     case DW_TAG_imported_module:
7254       cu->processing_has_namespace_info = 1;
7255       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7256 				 || cu->language != language_fortran))
7257 	complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7258 		   dwarf_tag_name (die->tag));
7259       read_import_statement (die, cu);
7260       break;
7261 
7262     case DW_TAG_imported_unit:
7263       process_imported_unit_die (die, cu);
7264       break;
7265 
7266     default:
7267       new_symbol (die, NULL, cu);
7268       break;
7269     }
7270 }
7271 
7272 /* A helper function for dwarf2_compute_name which determines whether DIE
7273    needs to have the name of the scope prepended to the name listed in the
7274    die.  */
7275 
7276 static int
7277 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7278 {
7279   struct attribute *attr;
7280 
7281   switch (die->tag)
7282     {
7283     case DW_TAG_namespace:
7284     case DW_TAG_typedef:
7285     case DW_TAG_class_type:
7286     case DW_TAG_interface_type:
7287     case DW_TAG_structure_type:
7288     case DW_TAG_union_type:
7289     case DW_TAG_enumeration_type:
7290     case DW_TAG_enumerator:
7291     case DW_TAG_subprogram:
7292     case DW_TAG_member:
7293       return 1;
7294 
7295     case DW_TAG_variable:
7296     case DW_TAG_constant:
7297       /* We only need to prefix "globally" visible variables.  These include
7298 	 any variable marked with DW_AT_external or any variable that
7299 	 lives in a namespace.  [Variables in anonymous namespaces
7300 	 require prefixing, but they are not DW_AT_external.]  */
7301 
7302       if (dwarf2_attr (die, DW_AT_specification, cu))
7303 	{
7304 	  struct dwarf2_cu *spec_cu = cu;
7305 
7306 	  return die_needs_namespace (die_specification (die, &spec_cu),
7307 				      spec_cu);
7308 	}
7309 
7310       attr = dwarf2_attr (die, DW_AT_external, cu);
7311       if (attr == NULL && die->parent->tag != DW_TAG_namespace
7312 	  && die->parent->tag != DW_TAG_module)
7313 	return 0;
7314       /* A variable in a lexical block of some kind does not need a
7315 	 namespace, even though in C++ such variables may be external
7316 	 and have a mangled name.  */
7317       if (die->parent->tag ==  DW_TAG_lexical_block
7318 	  || die->parent->tag ==  DW_TAG_try_block
7319 	  || die->parent->tag ==  DW_TAG_catch_block
7320 	  || die->parent->tag == DW_TAG_subprogram)
7321 	return 0;
7322       return 1;
7323 
7324     default:
7325       return 0;
7326     }
7327 }
7328 
7329 /* Retrieve the last character from a mem_file.  */
7330 
7331 static void
7332 do_ui_file_peek_last (void *object, const char *buffer, long length)
7333 {
7334   char *last_char_p = (char *) object;
7335 
7336   if (length > 0)
7337     *last_char_p = buffer[length - 1];
7338 }
7339 
7340 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
7341    compute the physname for the object, which include a method's:
7342    - formal parameters (C++/Java),
7343    - receiver type (Go),
7344    - return type (Java).
7345 
7346    The term "physname" is a bit confusing.
7347    For C++, for example, it is the demangled name.
7348    For Go, for example, it's the mangled name.
7349 
7350    For Ada, return the DIE's linkage name rather than the fully qualified
7351    name.  PHYSNAME is ignored..
7352 
7353    The result is allocated on the objfile_obstack and canonicalized.  */
7354 
7355 static const char *
7356 dwarf2_compute_name (const char *name,
7357 		     struct die_info *die, struct dwarf2_cu *cu,
7358 		     int physname)
7359 {
7360   struct objfile *objfile = cu->objfile;
7361 
7362   if (name == NULL)
7363     name = dwarf2_name (die, cu);
7364 
7365   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7366      compute it by typename_concat inside GDB.  */
7367   if (cu->language == language_ada
7368       || (cu->language == language_fortran && physname))
7369     {
7370       /* For Ada unit, we prefer the linkage name over the name, as
7371 	 the former contains the exported name, which the user expects
7372 	 to be able to reference.  Ideally, we want the user to be able
7373 	 to reference this entity using either natural or linkage name,
7374 	 but we haven't started looking at this enhancement yet.  */
7375       struct attribute *attr;
7376 
7377       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7378       if (attr == NULL)
7379 	attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7380       if (attr && DW_STRING (attr))
7381 	return DW_STRING (attr);
7382     }
7383 
7384   /* These are the only languages we know how to qualify names in.  */
7385   if (name != NULL
7386       && (cu->language == language_cplus || cu->language == language_java
7387 	  || cu->language == language_fortran))
7388     {
7389       if (die_needs_namespace (die, cu))
7390 	{
7391 	  long length;
7392 	  const char *prefix;
7393 	  struct ui_file *buf;
7394 
7395 	  prefix = determine_prefix (die, cu);
7396 	  buf = mem_fileopen ();
7397 	  if (*prefix != '\0')
7398 	    {
7399 	      char *prefixed_name = typename_concat (NULL, prefix, name,
7400 						     physname, cu);
7401 
7402 	      fputs_unfiltered (prefixed_name, buf);
7403 	      xfree (prefixed_name);
7404 	    }
7405 	  else
7406 	    fputs_unfiltered (name, buf);
7407 
7408 	  /* Template parameters may be specified in the DIE's DW_AT_name, or
7409 	     as children with DW_TAG_template_type_param or
7410 	     DW_TAG_value_type_param.  If the latter, add them to the name
7411 	     here.  If the name already has template parameters, then
7412 	     skip this step; some versions of GCC emit both, and
7413 	     it is more efficient to use the pre-computed name.
7414 
7415 	     Something to keep in mind about this process: it is very
7416 	     unlikely, or in some cases downright impossible, to produce
7417 	     something that will match the mangled name of a function.
7418 	     If the definition of the function has the same debug info,
7419 	     we should be able to match up with it anyway.  But fallbacks
7420 	     using the minimal symbol, for instance to find a method
7421 	     implemented in a stripped copy of libstdc++, will not work.
7422 	     If we do not have debug info for the definition, we will have to
7423 	     match them up some other way.
7424 
7425 	     When we do name matching there is a related problem with function
7426 	     templates; two instantiated function templates are allowed to
7427 	     differ only by their return types, which we do not add here.  */
7428 
7429 	  if (cu->language == language_cplus && strchr (name, '<') == NULL)
7430 	    {
7431 	      struct attribute *attr;
7432 	      struct die_info *child;
7433 	      int first = 1;
7434 
7435 	      die->building_fullname = 1;
7436 
7437 	      for (child = die->child; child != NULL; child = child->sibling)
7438 		{
7439 		  struct type *type;
7440 		  LONGEST value;
7441 		  gdb_byte *bytes;
7442 		  struct dwarf2_locexpr_baton *baton;
7443 		  struct value *v;
7444 
7445 		  if (child->tag != DW_TAG_template_type_param
7446 		      && child->tag != DW_TAG_template_value_param)
7447 		    continue;
7448 
7449 		  if (first)
7450 		    {
7451 		      fputs_unfiltered ("<", buf);
7452 		      first = 0;
7453 		    }
7454 		  else
7455 		    fputs_unfiltered (", ", buf);
7456 
7457 		  attr = dwarf2_attr (child, DW_AT_type, cu);
7458 		  if (attr == NULL)
7459 		    {
7460 		      complaint (&symfile_complaints,
7461 				 _("template parameter missing DW_AT_type"));
7462 		      fputs_unfiltered ("UNKNOWN_TYPE", buf);
7463 		      continue;
7464 		    }
7465 		  type = die_type (child, cu);
7466 
7467 		  if (child->tag == DW_TAG_template_type_param)
7468 		    {
7469 		      c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7470 		      continue;
7471 		    }
7472 
7473 		  attr = dwarf2_attr (child, DW_AT_const_value, cu);
7474 		  if (attr == NULL)
7475 		    {
7476 		      complaint (&symfile_complaints,
7477 				 _("template parameter missing "
7478 				   "DW_AT_const_value"));
7479 		      fputs_unfiltered ("UNKNOWN_VALUE", buf);
7480 		      continue;
7481 		    }
7482 
7483 		  dwarf2_const_value_attr (attr, type, name,
7484 					   &cu->comp_unit_obstack, cu,
7485 					   &value, &bytes, &baton);
7486 
7487 		  if (TYPE_NOSIGN (type))
7488 		    /* GDB prints characters as NUMBER 'CHAR'.  If that's
7489 		       changed, this can use value_print instead.  */
7490 		    c_printchar (value, type, buf);
7491 		  else
7492 		    {
7493 		      struct value_print_options opts;
7494 
7495 		      if (baton != NULL)
7496 			v = dwarf2_evaluate_loc_desc (type, NULL,
7497 						      baton->data,
7498 						      baton->size,
7499 						      baton->per_cu);
7500 		      else if (bytes != NULL)
7501 			{
7502 			  v = allocate_value (type);
7503 			  memcpy (value_contents_writeable (v), bytes,
7504 				  TYPE_LENGTH (type));
7505 			}
7506 		      else
7507 			v = value_from_longest (type, value);
7508 
7509 		      /* Specify decimal so that we do not depend on
7510 			 the radix.  */
7511 		      get_formatted_print_options (&opts, 'd');
7512 		      opts.raw = 1;
7513 		      value_print (v, buf, &opts);
7514 		      release_value (v);
7515 		      value_free (v);
7516 		    }
7517 		}
7518 
7519 	      die->building_fullname = 0;
7520 
7521 	      if (!first)
7522 		{
7523 		  /* Close the argument list, with a space if necessary
7524 		     (nested templates).  */
7525 		  char last_char = '\0';
7526 		  ui_file_put (buf, do_ui_file_peek_last, &last_char);
7527 		  if (last_char == '>')
7528 		    fputs_unfiltered (" >", buf);
7529 		  else
7530 		    fputs_unfiltered (">", buf);
7531 		}
7532 	    }
7533 
7534 	  /* For Java and C++ methods, append formal parameter type
7535 	     information, if PHYSNAME.  */
7536 
7537 	  if (physname && die->tag == DW_TAG_subprogram
7538 	      && (cu->language == language_cplus
7539 		  || cu->language == language_java))
7540 	    {
7541 	      struct type *type = read_type_die (die, cu);
7542 
7543 	      c_type_print_args (type, buf, 1, cu->language,
7544 				 &type_print_raw_options);
7545 
7546 	      if (cu->language == language_java)
7547 		{
7548 		  /* For java, we must append the return type to method
7549 		     names.  */
7550 		  if (die->tag == DW_TAG_subprogram)
7551 		    java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7552 				     0, 0, &type_print_raw_options);
7553 		}
7554 	      else if (cu->language == language_cplus)
7555 		{
7556 		  /* Assume that an artificial first parameter is
7557 		     "this", but do not crash if it is not.  RealView
7558 		     marks unnamed (and thus unused) parameters as
7559 		     artificial; there is no way to differentiate
7560 		     the two cases.  */
7561 		  if (TYPE_NFIELDS (type) > 0
7562 		      && TYPE_FIELD_ARTIFICIAL (type, 0)
7563 		      && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7564 		      && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7565 									0))))
7566 		    fputs_unfiltered (" const", buf);
7567 		}
7568 	    }
7569 
7570 	  name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7571 				       &length);
7572 	  ui_file_delete (buf);
7573 
7574 	  if (cu->language == language_cplus)
7575 	    {
7576 	      const char *cname
7577 		= dwarf2_canonicalize_name (name, cu,
7578 					    &objfile->objfile_obstack);
7579 
7580 	      if (cname != NULL)
7581 		name = cname;
7582 	    }
7583 	}
7584     }
7585 
7586   return name;
7587 }
7588 
7589 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7590    If scope qualifiers are appropriate they will be added.  The result
7591    will be allocated on the objfile_obstack, or NULL if the DIE does
7592    not have a name.  NAME may either be from a previous call to
7593    dwarf2_name or NULL.
7594 
7595    The output string will be canonicalized (if C++/Java).  */
7596 
7597 static const char *
7598 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7599 {
7600   return dwarf2_compute_name (name, die, cu, 0);
7601 }
7602 
7603 /* Construct a physname for the given DIE in CU.  NAME may either be
7604    from a previous call to dwarf2_name or NULL.  The result will be
7605    allocated on the objfile_objstack or NULL if the DIE does not have a
7606    name.
7607 
7608    The output string will be canonicalized (if C++/Java).  */
7609 
7610 static const char *
7611 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7612 {
7613   struct objfile *objfile = cu->objfile;
7614   struct attribute *attr;
7615   const char *retval, *mangled = NULL, *canon = NULL;
7616   struct cleanup *back_to;
7617   int need_copy = 1;
7618 
7619   /* In this case dwarf2_compute_name is just a shortcut not building anything
7620      on its own.  */
7621   if (!die_needs_namespace (die, cu))
7622     return dwarf2_compute_name (name, die, cu, 1);
7623 
7624   back_to = make_cleanup (null_cleanup, NULL);
7625 
7626   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7627   if (!attr)
7628     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7629 
7630   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7631      has computed.  */
7632   if (attr && DW_STRING (attr))
7633     {
7634       char *demangled;
7635 
7636       mangled = DW_STRING (attr);
7637 
7638       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7639 	 type.  It is easier for GDB users to search for such functions as
7640 	 `name(params)' than `long name(params)'.  In such case the minimal
7641 	 symbol names do not match the full symbol names but for template
7642 	 functions there is never a need to look up their definition from their
7643 	 declaration so the only disadvantage remains the minimal symbol
7644 	 variant `long name(params)' does not have the proper inferior type.
7645 	 */
7646 
7647       if (cu->language == language_go)
7648 	{
7649 	  /* This is a lie, but we already lie to the caller new_symbol_full.
7650 	     new_symbol_full assumes we return the mangled name.
7651 	     This just undoes that lie until things are cleaned up.  */
7652 	  demangled = NULL;
7653 	}
7654       else
7655 	{
7656 	  demangled = cplus_demangle (mangled,
7657 				      (DMGL_PARAMS | DMGL_ANSI
7658 				       | (cu->language == language_java
7659 					  ? DMGL_JAVA | DMGL_RET_POSTFIX
7660 					  : DMGL_RET_DROP)));
7661 	}
7662       if (demangled)
7663 	{
7664 	  make_cleanup (xfree, demangled);
7665 	  canon = demangled;
7666 	}
7667       else
7668 	{
7669 	  canon = mangled;
7670 	  need_copy = 0;
7671 	}
7672     }
7673 
7674   if (canon == NULL || check_physname)
7675     {
7676       const char *physname = dwarf2_compute_name (name, die, cu, 1);
7677 
7678       if (canon != NULL && strcmp (physname, canon) != 0)
7679 	{
7680 	  /* It may not mean a bug in GDB.  The compiler could also
7681 	     compute DW_AT_linkage_name incorrectly.  But in such case
7682 	     GDB would need to be bug-to-bug compatible.  */
7683 
7684 	  complaint (&symfile_complaints,
7685 		     _("Computed physname <%s> does not match demangled <%s> "
7686 		       "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7687 		     physname, canon, mangled, die->offset.sect_off, objfile->name);
7688 
7689 	  /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7690 	     is available here - over computed PHYSNAME.  It is safer
7691 	     against both buggy GDB and buggy compilers.  */
7692 
7693 	  retval = canon;
7694 	}
7695       else
7696 	{
7697 	  retval = physname;
7698 	  need_copy = 0;
7699 	}
7700     }
7701   else
7702     retval = canon;
7703 
7704   if (need_copy)
7705     retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7706 
7707   do_cleanups (back_to);
7708   return retval;
7709 }
7710 
7711 /* Read the import statement specified by the given die and record it.  */
7712 
7713 static void
7714 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7715 {
7716   struct objfile *objfile = cu->objfile;
7717   struct attribute *import_attr;
7718   struct die_info *imported_die, *child_die;
7719   struct dwarf2_cu *imported_cu;
7720   const char *imported_name;
7721   const char *imported_name_prefix;
7722   const char *canonical_name;
7723   const char *import_alias;
7724   const char *imported_declaration = NULL;
7725   const char *import_prefix;
7726   VEC (const_char_ptr) *excludes = NULL;
7727   struct cleanup *cleanups;
7728 
7729   import_attr = dwarf2_attr (die, DW_AT_import, cu);
7730   if (import_attr == NULL)
7731     {
7732       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7733 		 dwarf_tag_name (die->tag));
7734       return;
7735     }
7736 
7737   imported_cu = cu;
7738   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7739   imported_name = dwarf2_name (imported_die, imported_cu);
7740   if (imported_name == NULL)
7741     {
7742       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7743 
7744         The import in the following code:
7745         namespace A
7746           {
7747             typedef int B;
7748           }
7749 
7750         int main ()
7751           {
7752             using A::B;
7753             B b;
7754             return b;
7755           }
7756 
7757         ...
7758          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7759             <52>   DW_AT_decl_file   : 1
7760             <53>   DW_AT_decl_line   : 6
7761             <54>   DW_AT_import      : <0x75>
7762          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7763             <59>   DW_AT_name        : B
7764             <5b>   DW_AT_decl_file   : 1
7765             <5c>   DW_AT_decl_line   : 2
7766             <5d>   DW_AT_type        : <0x6e>
7767         ...
7768          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7769             <76>   DW_AT_byte_size   : 4
7770             <77>   DW_AT_encoding    : 5        (signed)
7771 
7772         imports the wrong die ( 0x75 instead of 0x58 ).
7773         This case will be ignored until the gcc bug is fixed.  */
7774       return;
7775     }
7776 
7777   /* Figure out the local name after import.  */
7778   import_alias = dwarf2_name (die, cu);
7779 
7780   /* Figure out where the statement is being imported to.  */
7781   import_prefix = determine_prefix (die, cu);
7782 
7783   /* Figure out what the scope of the imported die is and prepend it
7784      to the name of the imported die.  */
7785   imported_name_prefix = determine_prefix (imported_die, imported_cu);
7786 
7787   if (imported_die->tag != DW_TAG_namespace
7788       && imported_die->tag != DW_TAG_module)
7789     {
7790       imported_declaration = imported_name;
7791       canonical_name = imported_name_prefix;
7792     }
7793   else if (strlen (imported_name_prefix) > 0)
7794     canonical_name = obconcat (&objfile->objfile_obstack,
7795 			       imported_name_prefix, "::", imported_name,
7796 			       (char *) NULL);
7797   else
7798     canonical_name = imported_name;
7799 
7800   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7801 
7802   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7803     for (child_die = die->child; child_die && child_die->tag;
7804 	 child_die = sibling_die (child_die))
7805       {
7806 	/* DWARF-4: A Fortran use statement with a “rename list” may be
7807 	   represented by an imported module entry with an import attribute
7808 	   referring to the module and owned entries corresponding to those
7809 	   entities that are renamed as part of being imported.  */
7810 
7811 	if (child_die->tag != DW_TAG_imported_declaration)
7812 	  {
7813 	    complaint (&symfile_complaints,
7814 		       _("child DW_TAG_imported_declaration expected "
7815 			 "- DIE at 0x%x [in module %s]"),
7816 		       child_die->offset.sect_off, objfile->name);
7817 	    continue;
7818 	  }
7819 
7820 	import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7821 	if (import_attr == NULL)
7822 	  {
7823 	    complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7824 		       dwarf_tag_name (child_die->tag));
7825 	    continue;
7826 	  }
7827 
7828 	imported_cu = cu;
7829 	imported_die = follow_die_ref_or_sig (child_die, import_attr,
7830 					      &imported_cu);
7831 	imported_name = dwarf2_name (imported_die, imported_cu);
7832 	if (imported_name == NULL)
7833 	  {
7834 	    complaint (&symfile_complaints,
7835 		       _("child DW_TAG_imported_declaration has unknown "
7836 			 "imported name - DIE at 0x%x [in module %s]"),
7837 		       child_die->offset.sect_off, objfile->name);
7838 	    continue;
7839 	  }
7840 
7841 	VEC_safe_push (const_char_ptr, excludes, imported_name);
7842 
7843 	process_die (child_die, cu);
7844       }
7845 
7846   cp_add_using_directive (import_prefix,
7847                           canonical_name,
7848                           import_alias,
7849                           imported_declaration,
7850 			  excludes,
7851 			  0,
7852                           &objfile->objfile_obstack);
7853 
7854   do_cleanups (cleanups);
7855 }
7856 
7857 /* Cleanup function for handle_DW_AT_stmt_list.  */
7858 
7859 static void
7860 free_cu_line_header (void *arg)
7861 {
7862   struct dwarf2_cu *cu = arg;
7863 
7864   free_line_header (cu->line_header);
7865   cu->line_header = NULL;
7866 }
7867 
7868 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7869    directory paths.  GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7870    this, it was first present in GCC release 4.3.0.  */
7871 
7872 static int
7873 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7874 {
7875   if (!cu->checked_producer)
7876     check_producer (cu);
7877 
7878   return cu->producer_is_gcc_lt_4_3;
7879 }
7880 
7881 static void
7882 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7883 			 const char **name, const char **comp_dir)
7884 {
7885   struct attribute *attr;
7886 
7887   *name = NULL;
7888   *comp_dir = NULL;
7889 
7890   /* Find the filename.  Do not use dwarf2_name here, since the filename
7891      is not a source language identifier.  */
7892   attr = dwarf2_attr (die, DW_AT_name, cu);
7893   if (attr)
7894     {
7895       *name = DW_STRING (attr);
7896     }
7897 
7898   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7899   if (attr)
7900     *comp_dir = DW_STRING (attr);
7901   else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7902 	   && IS_ABSOLUTE_PATH (*name))
7903     {
7904       char *d = ldirname (*name);
7905 
7906       *comp_dir = d;
7907       if (d != NULL)
7908 	make_cleanup (xfree, d);
7909     }
7910   if (*comp_dir != NULL)
7911     {
7912       /* Irix 6.2 native cc prepends <machine>.: to the compilation
7913 	 directory, get rid of it.  */
7914       char *cp = strchr (*comp_dir, ':');
7915 
7916       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7917 	*comp_dir = cp + 1;
7918     }
7919 
7920   if (*name == NULL)
7921     *name = "<unknown>";
7922 }
7923 
7924 /* Handle DW_AT_stmt_list for a compilation unit.
7925    DIE is the DW_TAG_compile_unit die for CU.
7926    COMP_DIR is the compilation directory.
7927    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
7928 
7929 static void
7930 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7931 			const char *comp_dir)
7932 {
7933   struct attribute *attr;
7934 
7935   gdb_assert (! cu->per_cu->is_debug_types);
7936 
7937   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7938   if (attr)
7939     {
7940       unsigned int line_offset = DW_UNSND (attr);
7941       struct line_header *line_header
7942 	= dwarf_decode_line_header (line_offset, cu);
7943 
7944       if (line_header)
7945 	{
7946 	  cu->line_header = line_header;
7947 	  make_cleanup (free_cu_line_header, cu);
7948 	  dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7949 	}
7950     }
7951 }
7952 
7953 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
7954 
7955 static void
7956 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7957 {
7958   struct objfile *objfile = dwarf2_per_objfile->objfile;
7959   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7960   CORE_ADDR lowpc = ((CORE_ADDR) -1);
7961   CORE_ADDR highpc = ((CORE_ADDR) 0);
7962   struct attribute *attr;
7963   const char *name = NULL;
7964   const char *comp_dir = NULL;
7965   struct die_info *child_die;
7966   bfd *abfd = objfile->obfd;
7967   CORE_ADDR baseaddr;
7968 
7969   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7970 
7971   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7972 
7973   /* If we didn't find a lowpc, set it to highpc to avoid complaints
7974      from finish_block.  */
7975   if (lowpc == ((CORE_ADDR) -1))
7976     lowpc = highpc;
7977   lowpc += baseaddr;
7978   highpc += baseaddr;
7979 
7980   find_file_and_directory (die, cu, &name, &comp_dir);
7981 
7982   prepare_one_comp_unit (cu, die, cu->language);
7983 
7984   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7985      standardised yet.  As a workaround for the language detection we fall
7986      back to the DW_AT_producer string.  */
7987   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7988     cu->language = language_opencl;
7989 
7990   /* Similar hack for Go.  */
7991   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7992     set_cu_language (DW_LANG_Go, cu);
7993 
7994   dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7995 
7996   /* Decode line number information if present.  We do this before
7997      processing child DIEs, so that the line header table is available
7998      for DW_AT_decl_file.  */
7999   handle_DW_AT_stmt_list (die, cu, comp_dir);
8000 
8001   /* Process all dies in compilation unit.  */
8002   if (die->child != NULL)
8003     {
8004       child_die = die->child;
8005       while (child_die && child_die->tag)
8006 	{
8007 	  process_die (child_die, cu);
8008 	  child_die = sibling_die (child_die);
8009 	}
8010     }
8011 
8012   /* Decode macro information, if present.  Dwarf 2 macro information
8013      refers to information in the line number info statement program
8014      header, so we can only read it if we've read the header
8015      successfully.  */
8016   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8017   if (attr && cu->line_header)
8018     {
8019       if (dwarf2_attr (die, DW_AT_macro_info, cu))
8020 	complaint (&symfile_complaints,
8021 		   _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8022 
8023       dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8024     }
8025   else
8026     {
8027       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8028       if (attr && cu->line_header)
8029 	{
8030 	  unsigned int macro_offset = DW_UNSND (attr);
8031 
8032 	  dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8033 	}
8034     }
8035 
8036   do_cleanups (back_to);
8037 }
8038 
8039 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8040    Create the set of symtabs used by this TU, or if this TU is sharing
8041    symtabs with another TU and the symtabs have already been created
8042    then restore those symtabs in the line header.
8043    We don't need the pc/line-number mapping for type units.  */
8044 
8045 static void
8046 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8047 {
8048   struct objfile *objfile = dwarf2_per_objfile->objfile;
8049   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8050   struct type_unit_group *tu_group;
8051   int first_time;
8052   struct line_header *lh;
8053   struct attribute *attr;
8054   unsigned int i, line_offset;
8055 
8056   gdb_assert (per_cu->is_debug_types);
8057 
8058   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8059 
8060   /* If we're using .gdb_index (includes -readnow) then
8061      per_cu->s.type_unit_group may not have been set up yet.  */
8062   if (per_cu->type_unit_group == NULL)
8063     per_cu->type_unit_group = get_type_unit_group (cu, attr);
8064   tu_group = per_cu->type_unit_group;
8065 
8066   /* If we've already processed this stmt_list there's no real need to
8067      do it again, we could fake it and just recreate the part we need
8068      (file name,index -> symtab mapping).  If data shows this optimization
8069      is useful we can do it then.  */
8070   first_time = tu_group->primary_symtab == NULL;
8071 
8072   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8073      debug info.  */
8074   lh = NULL;
8075   if (attr != NULL)
8076     {
8077       line_offset = DW_UNSND (attr);
8078       lh = dwarf_decode_line_header (line_offset, cu);
8079     }
8080   if (lh == NULL)
8081     {
8082       if (first_time)
8083 	dwarf2_start_symtab (cu, "", NULL, 0);
8084       else
8085 	{
8086 	  gdb_assert (tu_group->symtabs == NULL);
8087 	  restart_symtab (0);
8088 	}
8089       /* Note: The primary symtab will get allocated at the end.  */
8090       return;
8091     }
8092 
8093   cu->line_header = lh;
8094   make_cleanup (free_cu_line_header, cu);
8095 
8096   if (first_time)
8097     {
8098       dwarf2_start_symtab (cu, "", NULL, 0);
8099 
8100       tu_group->num_symtabs = lh->num_file_names;
8101       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8102 
8103       for (i = 0; i < lh->num_file_names; ++i)
8104 	{
8105 	  char *dir = NULL;
8106 	  struct file_entry *fe = &lh->file_names[i];
8107 
8108 	  if (fe->dir_index)
8109 	    dir = lh->include_dirs[fe->dir_index - 1];
8110 	  dwarf2_start_subfile (fe->name, dir, NULL);
8111 
8112 	  /* Note: We don't have to watch for the main subfile here, type units
8113 	     don't have DW_AT_name.  */
8114 
8115 	  if (current_subfile->symtab == NULL)
8116 	    {
8117 	      /* NOTE: start_subfile will recognize when it's been passed
8118 		 a file it has already seen.  So we can't assume there's a
8119 		 simple mapping from lh->file_names to subfiles,
8120 		 lh->file_names may contain dups.  */
8121 	      current_subfile->symtab = allocate_symtab (current_subfile->name,
8122 							 objfile);
8123 	    }
8124 
8125 	  fe->symtab = current_subfile->symtab;
8126 	  tu_group->symtabs[i] = fe->symtab;
8127 	}
8128     }
8129   else
8130     {
8131       restart_symtab (0);
8132 
8133       for (i = 0; i < lh->num_file_names; ++i)
8134 	{
8135 	  struct file_entry *fe = &lh->file_names[i];
8136 
8137 	  fe->symtab = tu_group->symtabs[i];
8138 	}
8139     }
8140 
8141   /* The main symtab is allocated last.  Type units don't have DW_AT_name
8142      so they don't have a "real" (so to speak) symtab anyway.
8143      There is later code that will assign the main symtab to all symbols
8144      that don't have one.  We need to handle the case of a symbol with a
8145      missing symtab (DW_AT_decl_file) anyway.  */
8146 }
8147 
8148 /* Process DW_TAG_type_unit.
8149    For TUs we want to skip the first top level sibling if it's not the
8150    actual type being defined by this TU.  In this case the first top
8151    level sibling is there to provide context only.  */
8152 
8153 static void
8154 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8155 {
8156   struct die_info *child_die;
8157 
8158   prepare_one_comp_unit (cu, die, language_minimal);
8159 
8160   /* Initialize (or reinitialize) the machinery for building symtabs.
8161      We do this before processing child DIEs, so that the line header table
8162      is available for DW_AT_decl_file.  */
8163   setup_type_unit_groups (die, cu);
8164 
8165   if (die->child != NULL)
8166     {
8167       child_die = die->child;
8168       while (child_die && child_die->tag)
8169 	{
8170 	  process_die (child_die, cu);
8171 	  child_die = sibling_die (child_die);
8172 	}
8173     }
8174 }
8175 
8176 /* DWO/DWP files.
8177 
8178    http://gcc.gnu.org/wiki/DebugFission
8179    http://gcc.gnu.org/wiki/DebugFissionDWP
8180 
8181    To simplify handling of both DWO files ("object" files with the DWARF info)
8182    and DWP files (a file with the DWOs packaged up into one file), we treat
8183    DWP files as having a collection of virtual DWO files.  */
8184 
8185 static hashval_t
8186 hash_dwo_file (const void *item)
8187 {
8188   const struct dwo_file *dwo_file = item;
8189 
8190   return htab_hash_string (dwo_file->name);
8191 }
8192 
8193 static int
8194 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8195 {
8196   const struct dwo_file *lhs = item_lhs;
8197   const struct dwo_file *rhs = item_rhs;
8198 
8199   return strcmp (lhs->name, rhs->name) == 0;
8200 }
8201 
8202 /* Allocate a hash table for DWO files.  */
8203 
8204 static htab_t
8205 allocate_dwo_file_hash_table (void)
8206 {
8207   struct objfile *objfile = dwarf2_per_objfile->objfile;
8208 
8209   return htab_create_alloc_ex (41,
8210 			       hash_dwo_file,
8211 			       eq_dwo_file,
8212 			       NULL,
8213 			       &objfile->objfile_obstack,
8214 			       hashtab_obstack_allocate,
8215 			       dummy_obstack_deallocate);
8216 }
8217 
8218 /* Lookup DWO file DWO_NAME.  */
8219 
8220 static void **
8221 lookup_dwo_file_slot (const char *dwo_name)
8222 {
8223   struct dwo_file find_entry;
8224   void **slot;
8225 
8226   if (dwarf2_per_objfile->dwo_files == NULL)
8227     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8228 
8229   memset (&find_entry, 0, sizeof (find_entry));
8230   find_entry.name = dwo_name;
8231   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8232 
8233   return slot;
8234 }
8235 
8236 static hashval_t
8237 hash_dwo_unit (const void *item)
8238 {
8239   const struct dwo_unit *dwo_unit = item;
8240 
8241   /* This drops the top 32 bits of the id, but is ok for a hash.  */
8242   return dwo_unit->signature;
8243 }
8244 
8245 static int
8246 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8247 {
8248   const struct dwo_unit *lhs = item_lhs;
8249   const struct dwo_unit *rhs = item_rhs;
8250 
8251   /* The signature is assumed to be unique within the DWO file.
8252      So while object file CU dwo_id's always have the value zero,
8253      that's OK, assuming each object file DWO file has only one CU,
8254      and that's the rule for now.  */
8255   return lhs->signature == rhs->signature;
8256 }
8257 
8258 /* Allocate a hash table for DWO CUs,TUs.
8259    There is one of these tables for each of CUs,TUs for each DWO file.  */
8260 
8261 static htab_t
8262 allocate_dwo_unit_table (struct objfile *objfile)
8263 {
8264   /* Start out with a pretty small number.
8265      Generally DWO files contain only one CU and maybe some TUs.  */
8266   return htab_create_alloc_ex (3,
8267 			       hash_dwo_unit,
8268 			       eq_dwo_unit,
8269 			       NULL,
8270 			       &objfile->objfile_obstack,
8271 			       hashtab_obstack_allocate,
8272 			       dummy_obstack_deallocate);
8273 }
8274 
8275 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader.  */
8276 
8277 struct create_dwo_info_table_data
8278 {
8279   struct dwo_file *dwo_file;
8280   htab_t cu_htab;
8281 };
8282 
8283 /* die_reader_func for create_dwo_debug_info_hash_table.  */
8284 
8285 static void
8286 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8287 					 gdb_byte *info_ptr,
8288 					 struct die_info *comp_unit_die,
8289 					 int has_children,
8290 					 void *datap)
8291 {
8292   struct dwarf2_cu *cu = reader->cu;
8293   struct objfile *objfile = dwarf2_per_objfile->objfile;
8294   sect_offset offset = cu->per_cu->offset;
8295   struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8296   struct create_dwo_info_table_data *data = datap;
8297   struct dwo_file *dwo_file = data->dwo_file;
8298   htab_t cu_htab = data->cu_htab;
8299   void **slot;
8300   struct attribute *attr;
8301   struct dwo_unit *dwo_unit;
8302 
8303   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8304   if (attr == NULL)
8305     {
8306       error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8307 	       " its dwo_id [in module %s]"),
8308 	     offset.sect_off, dwo_file->name);
8309       return;
8310     }
8311 
8312   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8313   dwo_unit->dwo_file = dwo_file;
8314   dwo_unit->signature = DW_UNSND (attr);
8315   dwo_unit->info_or_types_section = section;
8316   dwo_unit->offset = offset;
8317   dwo_unit->length = cu->per_cu->length;
8318 
8319   slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8320   gdb_assert (slot != NULL);
8321   if (*slot != NULL)
8322     {
8323       const struct dwo_unit *dup_dwo_unit = *slot;
8324 
8325       complaint (&symfile_complaints,
8326 		 _("debug entry at offset 0x%x is duplicate to the entry at"
8327 		   " offset 0x%x, dwo_id 0x%s [in module %s]"),
8328 		 offset.sect_off, dup_dwo_unit->offset.sect_off,
8329 		 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8330 		 dwo_file->name);
8331     }
8332   else
8333     *slot = dwo_unit;
8334 
8335   if (dwarf2_read_debug)
8336     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id 0x%s\n",
8337 			offset.sect_off,
8338 			phex (dwo_unit->signature,
8339 			      sizeof (dwo_unit->signature)));
8340 }
8341 
8342 /* Create a hash table to map DWO IDs to their CU entry in
8343    .debug_info.dwo in DWO_FILE.
8344    Note: This function processes DWO files only, not DWP files.  */
8345 
8346 static htab_t
8347 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8348 {
8349   struct objfile *objfile = dwarf2_per_objfile->objfile;
8350   struct dwarf2_section_info *section = &dwo_file->sections.info;
8351   bfd *abfd;
8352   htab_t cu_htab;
8353   gdb_byte *info_ptr, *end_ptr;
8354   struct create_dwo_info_table_data create_dwo_info_table_data;
8355 
8356   dwarf2_read_section (objfile, section);
8357   info_ptr = section->buffer;
8358 
8359   if (info_ptr == NULL)
8360     return NULL;
8361 
8362   /* We can't set abfd until now because the section may be empty or
8363      not present, in which case section->asection will be NULL.  */
8364   abfd = section->asection->owner;
8365 
8366   if (dwarf2_read_debug)
8367     fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8368 			bfd_get_filename (abfd));
8369 
8370   cu_htab = allocate_dwo_unit_table (objfile);
8371 
8372   create_dwo_info_table_data.dwo_file = dwo_file;
8373   create_dwo_info_table_data.cu_htab = cu_htab;
8374 
8375   end_ptr = info_ptr + section->size;
8376   while (info_ptr < end_ptr)
8377     {
8378       struct dwarf2_per_cu_data per_cu;
8379 
8380       memset (&per_cu, 0, sizeof (per_cu));
8381       per_cu.objfile = objfile;
8382       per_cu.is_debug_types = 0;
8383       per_cu.offset.sect_off = info_ptr - section->buffer;
8384       per_cu.info_or_types_section = section;
8385 
8386       init_cutu_and_read_dies_no_follow (&per_cu,
8387 					 &dwo_file->sections.abbrev,
8388 					 dwo_file,
8389 					 create_dwo_debug_info_hash_table_reader,
8390 					 &create_dwo_info_table_data);
8391 
8392       info_ptr += per_cu.length;
8393     }
8394 
8395   return cu_htab;
8396 }
8397 
8398 /* DWP file .debug_{cu,tu}_index section format:
8399    [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8400 
8401    Both index sections have the same format, and serve to map a 64-bit
8402    signature to a set of section numbers.  Each section begins with a header,
8403    followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8404    indexes, and a pool of 32-bit section numbers.  The index sections will be
8405    aligned at 8-byte boundaries in the file.
8406 
8407    The index section header contains two unsigned 32-bit values (using the
8408    byte order of the application binary):
8409 
8410     N, the number of compilation units or type units in the index
8411     M, the number of slots in the hash table
8412 
8413   (We assume that N and M will not exceed 2^32 - 1.)
8414 
8415   The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8416 
8417   The hash table begins at offset 8 in the section, and consists of an array
8418   of M 64-bit slots.  Each slot contains a 64-bit signature (using the byte
8419   order of the application binary).  Unused slots in the hash table are 0.
8420   (We rely on the extreme unlikeliness of a signature being exactly 0.)
8421 
8422   The parallel table begins immediately after the hash table
8423   (at offset 8 + 8 * M from the beginning of the section), and consists of an
8424   array of 32-bit indexes (using the byte order of the application binary),
8425   corresponding 1-1 with slots in the hash table.  Each entry in the parallel
8426   table contains a 32-bit index into the pool of section numbers.  For unused
8427   hash table slots, the corresponding entry in the parallel table will be 0.
8428 
8429   Given a 64-bit compilation unit signature or a type signature S, an entry
8430   in the hash table is located as follows:
8431 
8432   1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8433      the low-order k bits all set to 1.
8434 
8435   2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8436 
8437   3) If the hash table entry at index H matches the signature, use that
8438      entry.  If the hash table entry at index H is unused (all zeroes),
8439      terminate the search: the signature is not present in the table.
8440 
8441   4) Let H = (H + H') modulo M. Repeat at Step 3.
8442 
8443   Because M > N and H' and M are relatively prime, the search is guaranteed
8444   to stop at an unused slot or find the match.
8445 
8446   The pool of section numbers begins immediately following the hash table
8447   (at offset 8 + 12 * M from the beginning of the section).  The pool of
8448   section numbers consists of an array of 32-bit words (using the byte order
8449   of the application binary).  Each item in the array is indexed starting
8450   from 0.  The hash table entry provides the index of the first section
8451   number in the set.  Additional section numbers in the set follow, and the
8452   set is terminated by a 0 entry (section number 0 is not used in ELF).
8453 
8454   In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8455   section must be the first entry in the set, and the .debug_abbrev.dwo must
8456   be the second entry. Other members of the set may follow in any order.  */
8457 
8458 /* Create a hash table to map DWO IDs to their CU/TU entry in
8459    .debug_{info,types}.dwo in DWP_FILE.
8460    Returns NULL if there isn't one.
8461    Note: This function processes DWP files only, not DWO files.  */
8462 
8463 static struct dwp_hash_table *
8464 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8465 {
8466   struct objfile *objfile = dwarf2_per_objfile->objfile;
8467   bfd *dbfd = dwp_file->dbfd;
8468   char *index_ptr, *index_end;
8469   struct dwarf2_section_info *index;
8470   uint32_t version, nr_units, nr_slots;
8471   struct dwp_hash_table *htab;
8472 
8473   if (is_debug_types)
8474     index = &dwp_file->sections.tu_index;
8475   else
8476     index = &dwp_file->sections.cu_index;
8477 
8478   if (dwarf2_section_empty_p (index))
8479     return NULL;
8480   dwarf2_read_section (objfile, index);
8481 
8482   index_ptr = index->buffer;
8483   index_end = index_ptr + index->size;
8484 
8485   version = read_4_bytes (dbfd, index_ptr);
8486   index_ptr += 8; /* Skip the unused word.  */
8487   nr_units = read_4_bytes (dbfd, index_ptr);
8488   index_ptr += 4;
8489   nr_slots = read_4_bytes (dbfd, index_ptr);
8490   index_ptr += 4;
8491 
8492   if (version != 1)
8493     {
8494       error (_("Dwarf Error: unsupported DWP file version (%u)"
8495 	       " [in module %s]"),
8496 	     version, dwp_file->name);
8497     }
8498   if (nr_slots != (nr_slots & -nr_slots))
8499     {
8500       error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8501 	       " is not power of 2 [in module %s]"),
8502 	     nr_slots, dwp_file->name);
8503     }
8504 
8505   htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8506   htab->nr_units = nr_units;
8507   htab->nr_slots = nr_slots;
8508   htab->hash_table = index_ptr;
8509   htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8510   htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8511 
8512   return htab;
8513 }
8514 
8515 /* Update SECTIONS with the data from SECTP.
8516 
8517    This function is like the other "locate" section routines that are
8518    passed to bfd_map_over_sections, but in this context the sections to
8519    read comes from the DWP hash table, not the full ELF section table.
8520 
8521    The result is non-zero for success, or zero if an error was found.  */
8522 
8523 static int
8524 locate_virtual_dwo_sections (asection *sectp,
8525 			     struct virtual_dwo_sections *sections)
8526 {
8527   const struct dwop_section_names *names = &dwop_section_names;
8528 
8529   if (section_is_p (sectp->name, &names->abbrev_dwo))
8530     {
8531       /* There can be only one.  */
8532       if (sections->abbrev.asection != NULL)
8533 	return 0;
8534       sections->abbrev.asection = sectp;
8535       sections->abbrev.size = bfd_get_section_size (sectp);
8536     }
8537   else if (section_is_p (sectp->name, &names->info_dwo)
8538 	   || section_is_p (sectp->name, &names->types_dwo))
8539     {
8540       /* There can be only one.  */
8541       if (sections->info_or_types.asection != NULL)
8542 	return 0;
8543       sections->info_or_types.asection = sectp;
8544       sections->info_or_types.size = bfd_get_section_size (sectp);
8545     }
8546   else if (section_is_p (sectp->name, &names->line_dwo))
8547     {
8548       /* There can be only one.  */
8549       if (sections->line.asection != NULL)
8550 	return 0;
8551       sections->line.asection = sectp;
8552       sections->line.size = bfd_get_section_size (sectp);
8553     }
8554   else if (section_is_p (sectp->name, &names->loc_dwo))
8555     {
8556       /* There can be only one.  */
8557       if (sections->loc.asection != NULL)
8558 	return 0;
8559       sections->loc.asection = sectp;
8560       sections->loc.size = bfd_get_section_size (sectp);
8561     }
8562   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8563     {
8564       /* There can be only one.  */
8565       if (sections->macinfo.asection != NULL)
8566 	return 0;
8567       sections->macinfo.asection = sectp;
8568       sections->macinfo.size = bfd_get_section_size (sectp);
8569     }
8570   else if (section_is_p (sectp->name, &names->macro_dwo))
8571     {
8572       /* There can be only one.  */
8573       if (sections->macro.asection != NULL)
8574 	return 0;
8575       sections->macro.asection = sectp;
8576       sections->macro.size = bfd_get_section_size (sectp);
8577     }
8578   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8579     {
8580       /* There can be only one.  */
8581       if (sections->str_offsets.asection != NULL)
8582 	return 0;
8583       sections->str_offsets.asection = sectp;
8584       sections->str_offsets.size = bfd_get_section_size (sectp);
8585     }
8586   else
8587     {
8588       /* No other kind of section is valid.  */
8589       return 0;
8590     }
8591 
8592   return 1;
8593 }
8594 
8595 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8596    HTAB is the hash table from the DWP file.
8597    SECTION_INDEX is the index of the DWO in HTAB.  */
8598 
8599 static struct dwo_unit *
8600 create_dwo_in_dwp (struct dwp_file *dwp_file,
8601 		   const struct dwp_hash_table *htab,
8602 		   uint32_t section_index,
8603 		   ULONGEST signature, int is_debug_types)
8604 {
8605   struct objfile *objfile = dwarf2_per_objfile->objfile;
8606   bfd *dbfd = dwp_file->dbfd;
8607   const char *kind = is_debug_types ? "TU" : "CU";
8608   struct dwo_file *dwo_file;
8609   struct dwo_unit *dwo_unit;
8610   struct virtual_dwo_sections sections;
8611   void **dwo_file_slot;
8612   char *virtual_dwo_name;
8613   struct dwarf2_section_info *cutu;
8614   struct cleanup *cleanups;
8615   int i;
8616 
8617   if (dwarf2_read_debug)
8618     {
8619       fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8620 			  kind,
8621 			  section_index, phex (signature, sizeof (signature)),
8622 			  dwp_file->name);
8623     }
8624 
8625   /* Fetch the sections of this DWO.
8626      Put a limit on the number of sections we look for so that bad data
8627      doesn't cause us to loop forever.  */
8628 
8629 #define MAX_NR_DWO_SECTIONS \
8630   (1 /* .debug_info or .debug_types */ \
8631    + 1 /* .debug_abbrev */ \
8632    + 1 /* .debug_line */ \
8633    + 1 /* .debug_loc */ \
8634    + 1 /* .debug_str_offsets */ \
8635    + 1 /* .debug_macro */ \
8636    + 1 /* .debug_macinfo */ \
8637    + 1 /* trailing zero */)
8638 
8639   memset (&sections, 0, sizeof (sections));
8640   cleanups = make_cleanup (null_cleanup, 0);
8641 
8642   for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8643     {
8644       asection *sectp;
8645       uint32_t section_nr =
8646 	read_4_bytes (dbfd,
8647 		      htab->section_pool
8648 		      + (section_index + i) * sizeof (uint32_t));
8649 
8650       if (section_nr == 0)
8651 	break;
8652       if (section_nr >= dwp_file->num_sections)
8653 	{
8654 	  error (_("Dwarf Error: bad DWP hash table, section number too large"
8655 		   " [in module %s]"),
8656 		 dwp_file->name);
8657 	}
8658 
8659       sectp = dwp_file->elf_sections[section_nr];
8660       if (! locate_virtual_dwo_sections (sectp, &sections))
8661 	{
8662 	  error (_("Dwarf Error: bad DWP hash table, invalid section found"
8663 		   " [in module %s]"),
8664 		 dwp_file->name);
8665 	}
8666     }
8667 
8668   if (i < 2
8669       || sections.info_or_types.asection == NULL
8670       || sections.abbrev.asection == NULL)
8671     {
8672       error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8673 	       " [in module %s]"),
8674 	     dwp_file->name);
8675     }
8676   if (i == MAX_NR_DWO_SECTIONS)
8677     {
8678       error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8679 	       " [in module %s]"),
8680 	     dwp_file->name);
8681     }
8682 
8683   /* It's easier for the rest of the code if we fake a struct dwo_file and
8684      have dwo_unit "live" in that.  At least for now.
8685 
8686      The DWP file can be made up of a random collection of CUs and TUs.
8687      However, for each CU + set of TUs that came from the same original DWO
8688      file, we want to combine them back into a virtual DWO file to save space
8689      (fewer struct dwo_file objects to allocated).  Remember that for really
8690      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */
8691 
8692   virtual_dwo_name =
8693     xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8694 		sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8695 		sections.line.asection ? sections.line.asection->id : 0,
8696 		sections.loc.asection ? sections.loc.asection->id : 0,
8697 		(sections.str_offsets.asection
8698 		? sections.str_offsets.asection->id
8699 		: 0));
8700   make_cleanup (xfree, virtual_dwo_name);
8701   /* Can we use an existing virtual DWO file?  */
8702   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8703   /* Create one if necessary.  */
8704   if (*dwo_file_slot == NULL)
8705     {
8706       if (dwarf2_read_debug)
8707 	{
8708 	  fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8709 			      virtual_dwo_name);
8710 	}
8711       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8712       dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8713 				      virtual_dwo_name,
8714 				      strlen (virtual_dwo_name));
8715       dwo_file->sections.abbrev = sections.abbrev;
8716       dwo_file->sections.line = sections.line;
8717       dwo_file->sections.loc = sections.loc;
8718       dwo_file->sections.macinfo = sections.macinfo;
8719       dwo_file->sections.macro = sections.macro;
8720       dwo_file->sections.str_offsets = sections.str_offsets;
8721       /* The "str" section is global to the entire DWP file.  */
8722       dwo_file->sections.str = dwp_file->sections.str;
8723       /* The info or types section is assigned later to dwo_unit,
8724 	 there's no need to record it in dwo_file.
8725 	 Also, we can't simply record type sections in dwo_file because
8726 	 we record a pointer into the vector in dwo_unit.  As we collect more
8727 	 types we'll grow the vector and eventually have to reallocate space
8728 	 for it, invalidating all the pointers into the current copy.  */
8729       *dwo_file_slot = dwo_file;
8730     }
8731   else
8732     {
8733       if (dwarf2_read_debug)
8734 	{
8735 	  fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8736 			      virtual_dwo_name);
8737 	}
8738       dwo_file = *dwo_file_slot;
8739     }
8740   do_cleanups (cleanups);
8741 
8742   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8743   dwo_unit->dwo_file = dwo_file;
8744   dwo_unit->signature = signature;
8745   dwo_unit->info_or_types_section =
8746     obstack_alloc (&objfile->objfile_obstack,
8747 		   sizeof (struct dwarf2_section_info));
8748   *dwo_unit->info_or_types_section = sections.info_or_types;
8749   /* offset, length, type_offset_in_tu are set later.  */
8750 
8751   return dwo_unit;
8752 }
8753 
8754 /* Lookup the DWO with SIGNATURE in DWP_FILE.  */
8755 
8756 static struct dwo_unit *
8757 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8758 		   const struct dwp_hash_table *htab,
8759 		   ULONGEST signature, int is_debug_types)
8760 {
8761   bfd *dbfd = dwp_file->dbfd;
8762   uint32_t mask = htab->nr_slots - 1;
8763   uint32_t hash = signature & mask;
8764   uint32_t hash2 = ((signature >> 32) & mask) | 1;
8765   unsigned int i;
8766   void **slot;
8767   struct dwo_unit find_dwo_cu, *dwo_cu;
8768 
8769   memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8770   find_dwo_cu.signature = signature;
8771   slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8772 
8773   if (*slot != NULL)
8774     return *slot;
8775 
8776   /* Use a for loop so that we don't loop forever on bad debug info.  */
8777   for (i = 0; i < htab->nr_slots; ++i)
8778     {
8779       ULONGEST signature_in_table;
8780 
8781       signature_in_table =
8782 	read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8783       if (signature_in_table == signature)
8784 	{
8785 	  uint32_t section_index =
8786 	    read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8787 
8788 	  *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8789 				     signature, is_debug_types);
8790 	  return *slot;
8791 	}
8792       if (signature_in_table == 0)
8793 	return NULL;
8794       hash = (hash + hash2) & mask;
8795     }
8796 
8797   error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8798 	   " [in module %s]"),
8799 	 dwp_file->name);
8800 }
8801 
8802 /* Subroutine of open_dwop_file to simplify it.
8803    Open the file specified by FILE_NAME and hand it off to BFD for
8804    preliminary analysis.  Return a newly initialized bfd *, which
8805    includes a canonicalized copy of FILE_NAME.
8806    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8807    In case of trouble, return NULL.
8808    NOTE: This function is derived from symfile_bfd_open.  */
8809 
8810 static bfd *
8811 try_open_dwop_file (const char *file_name, int is_dwp)
8812 {
8813   bfd *sym_bfd;
8814   int desc, flags;
8815   char *absolute_name;
8816 
8817   flags = OPF_TRY_CWD_FIRST;
8818   if (is_dwp)
8819     flags |= OPF_SEARCH_IN_PATH;
8820   desc = openp (debug_file_directory, flags, file_name,
8821 		O_RDONLY | O_BINARY, &absolute_name);
8822   if (desc < 0)
8823     return NULL;
8824 
8825   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8826   if (!sym_bfd)
8827     {
8828       xfree (absolute_name);
8829       return NULL;
8830     }
8831   xfree (absolute_name);
8832   bfd_set_cacheable (sym_bfd, 1);
8833 
8834   if (!bfd_check_format (sym_bfd, bfd_object))
8835     {
8836       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
8837       return NULL;
8838     }
8839 
8840   return sym_bfd;
8841 }
8842 
8843 /* Try to open DWO/DWP file FILE_NAME.
8844    COMP_DIR is the DW_AT_comp_dir attribute.
8845    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8846    The result is the bfd handle of the file.
8847    If there is a problem finding or opening the file, return NULL.
8848    Upon success, the canonicalized path of the file is stored in the bfd,
8849    same as symfile_bfd_open.  */
8850 
8851 static bfd *
8852 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8853 {
8854   bfd *abfd;
8855 
8856   if (IS_ABSOLUTE_PATH (file_name))
8857     return try_open_dwop_file (file_name, is_dwp);
8858 
8859   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
8860 
8861   if (comp_dir != NULL)
8862     {
8863       char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8864 
8865       /* NOTE: If comp_dir is a relative path, this will also try the
8866 	 search path, which seems useful.  */
8867       abfd = try_open_dwop_file (path_to_try, is_dwp);
8868       xfree (path_to_try);
8869       if (abfd != NULL)
8870 	return abfd;
8871     }
8872 
8873   /* That didn't work, try debug-file-directory, which, despite its name,
8874      is a list of paths.  */
8875 
8876   if (*debug_file_directory == '\0')
8877     return NULL;
8878 
8879   return try_open_dwop_file (file_name, is_dwp);
8880 }
8881 
8882 /* This function is mapped across the sections and remembers the offset and
8883    size of each of the DWO debugging sections we are interested in.  */
8884 
8885 static void
8886 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8887 {
8888   struct dwo_sections *dwo_sections = dwo_sections_ptr;
8889   const struct dwop_section_names *names = &dwop_section_names;
8890 
8891   if (section_is_p (sectp->name, &names->abbrev_dwo))
8892     {
8893       dwo_sections->abbrev.asection = sectp;
8894       dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8895     }
8896   else if (section_is_p (sectp->name, &names->info_dwo))
8897     {
8898       dwo_sections->info.asection = sectp;
8899       dwo_sections->info.size = bfd_get_section_size (sectp);
8900     }
8901   else if (section_is_p (sectp->name, &names->line_dwo))
8902     {
8903       dwo_sections->line.asection = sectp;
8904       dwo_sections->line.size = bfd_get_section_size (sectp);
8905     }
8906   else if (section_is_p (sectp->name, &names->loc_dwo))
8907     {
8908       dwo_sections->loc.asection = sectp;
8909       dwo_sections->loc.size = bfd_get_section_size (sectp);
8910     }
8911   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8912     {
8913       dwo_sections->macinfo.asection = sectp;
8914       dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8915     }
8916   else if (section_is_p (sectp->name, &names->macro_dwo))
8917     {
8918       dwo_sections->macro.asection = sectp;
8919       dwo_sections->macro.size = bfd_get_section_size (sectp);
8920     }
8921   else if (section_is_p (sectp->name, &names->str_dwo))
8922     {
8923       dwo_sections->str.asection = sectp;
8924       dwo_sections->str.size = bfd_get_section_size (sectp);
8925     }
8926   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8927     {
8928       dwo_sections->str_offsets.asection = sectp;
8929       dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8930     }
8931   else if (section_is_p (sectp->name, &names->types_dwo))
8932     {
8933       struct dwarf2_section_info type_section;
8934 
8935       memset (&type_section, 0, sizeof (type_section));
8936       type_section.asection = sectp;
8937       type_section.size = bfd_get_section_size (sectp);
8938       VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8939 		     &type_section);
8940     }
8941 }
8942 
8943 /* Initialize the use of the DWO file specified by DWO_NAME.
8944    The result is NULL if DWO_NAME can't be found.  */
8945 
8946 static struct dwo_file *
8947 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8948 {
8949   struct objfile *objfile = dwarf2_per_objfile->objfile;
8950   struct dwo_file *dwo_file;
8951   bfd *dbfd;
8952   struct cleanup *cleanups;
8953 
8954   dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8955   if (dbfd == NULL)
8956     {
8957       if (dwarf2_read_debug)
8958 	fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8959       return NULL;
8960     }
8961   dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8962   dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8963 				  dwo_name, strlen (dwo_name));
8964   dwo_file->dbfd = dbfd;
8965 
8966   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8967 
8968   bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8969 
8970   dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8971 
8972   dwo_file->tus = create_debug_types_hash_table (dwo_file,
8973 						 dwo_file->sections.types);
8974 
8975   discard_cleanups (cleanups);
8976 
8977   if (dwarf2_read_debug)
8978     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
8979 
8980   return dwo_file;
8981 }
8982 
8983 /* This function is mapped across the sections and remembers the offset and
8984    size of each of the DWP debugging sections we are interested in.  */
8985 
8986 static void
8987 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
8988 {
8989   struct dwp_file *dwp_file = dwp_file_ptr;
8990   const struct dwop_section_names *names = &dwop_section_names;
8991   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
8992 
8993   /* Record the ELF section number for later lookup: this is what the
8994      .debug_cu_index,.debug_tu_index tables use.  */
8995   gdb_assert (elf_section_nr < dwp_file->num_sections);
8996   dwp_file->elf_sections[elf_section_nr] = sectp;
8997 
8998   /* Look for specific sections that we need.  */
8999   if (section_is_p (sectp->name, &names->str_dwo))
9000     {
9001       dwp_file->sections.str.asection = sectp;
9002       dwp_file->sections.str.size = bfd_get_section_size (sectp);
9003     }
9004   else if (section_is_p (sectp->name, &names->cu_index))
9005     {
9006       dwp_file->sections.cu_index.asection = sectp;
9007       dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9008     }
9009   else if (section_is_p (sectp->name, &names->tu_index))
9010     {
9011       dwp_file->sections.tu_index.asection = sectp;
9012       dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9013     }
9014 }
9015 
9016 /* Hash function for dwp_file loaded CUs/TUs.  */
9017 
9018 static hashval_t
9019 hash_dwp_loaded_cutus (const void *item)
9020 {
9021   const struct dwo_unit *dwo_unit = item;
9022 
9023   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
9024   return dwo_unit->signature;
9025 }
9026 
9027 /* Equality function for dwp_file loaded CUs/TUs.  */
9028 
9029 static int
9030 eq_dwp_loaded_cutus (const void *a, const void *b)
9031 {
9032   const struct dwo_unit *dua = a;
9033   const struct dwo_unit *dub = b;
9034 
9035   return dua->signature == dub->signature;
9036 }
9037 
9038 /* Allocate a hash table for dwp_file loaded CUs/TUs.  */
9039 
9040 static htab_t
9041 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9042 {
9043   return htab_create_alloc_ex (3,
9044 			       hash_dwp_loaded_cutus,
9045 			       eq_dwp_loaded_cutus,
9046 			       NULL,
9047 			       &objfile->objfile_obstack,
9048 			       hashtab_obstack_allocate,
9049 			       dummy_obstack_deallocate);
9050 }
9051 
9052 /* Initialize the use of the DWP file for the current objfile.
9053    By convention the name of the DWP file is ${objfile}.dwp.
9054    The result is NULL if it can't be found.  */
9055 
9056 static struct dwp_file *
9057 open_and_init_dwp_file (const char *comp_dir)
9058 {
9059   struct objfile *objfile = dwarf2_per_objfile->objfile;
9060   struct dwp_file *dwp_file;
9061   char *dwp_name;
9062   bfd *dbfd;
9063   struct cleanup *cleanups;
9064 
9065   dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9066   cleanups = make_cleanup (xfree, dwp_name);
9067 
9068   dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9069   if (dbfd == NULL)
9070     {
9071       if (dwarf2_read_debug)
9072 	fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9073       do_cleanups (cleanups);
9074       return NULL;
9075     }
9076   dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9077   dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9078 				  dwp_name, strlen (dwp_name));
9079   dwp_file->dbfd = dbfd;
9080   do_cleanups (cleanups);
9081 
9082   /* +1: section 0 is unused */
9083   dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9084   dwp_file->elf_sections =
9085     OBSTACK_CALLOC (&objfile->objfile_obstack,
9086 		    dwp_file->num_sections, asection *);
9087 
9088   bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9089 
9090   dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9091 
9092   dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9093 
9094   dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9095 
9096   if (dwarf2_read_debug)
9097     {
9098       fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9099       fprintf_unfiltered (gdb_stdlog,
9100 			  "    %u CUs, %u TUs\n",
9101 			  dwp_file->cus ? dwp_file->cus->nr_units : 0,
9102 			  dwp_file->tus ? dwp_file->tus->nr_units : 0);
9103     }
9104 
9105   return dwp_file;
9106 }
9107 
9108 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9109    Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9110    or in the DWP file for the objfile, referenced by THIS_UNIT.
9111    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9112    IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9113 
9114    This is called, for example, when wanting to read a variable with a
9115    complex location.  Therefore we don't want to do file i/o for every call.
9116    Therefore we don't want to look for a DWO file on every call.
9117    Therefore we first see if we've already seen SIGNATURE in a DWP file,
9118    then we check if we've already seen DWO_NAME, and only THEN do we check
9119    for a DWO file.
9120 
9121    The result is a pointer to the dwo_unit object or NULL if we didn't find it
9122    (dwo_id mismatch or couldn't find the DWO/DWP file).  */
9123 
9124 static struct dwo_unit *
9125 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9126 		 const char *dwo_name, const char *comp_dir,
9127 		 ULONGEST signature, int is_debug_types)
9128 {
9129   struct objfile *objfile = dwarf2_per_objfile->objfile;
9130   const char *kind = is_debug_types ? "TU" : "CU";
9131   void **dwo_file_slot;
9132   struct dwo_file *dwo_file;
9133   struct dwp_file *dwp_file;
9134 
9135   /* Have we already read SIGNATURE from a DWP file?  */
9136 
9137   if (! dwarf2_per_objfile->dwp_checked)
9138     {
9139       dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9140       dwarf2_per_objfile->dwp_checked = 1;
9141     }
9142   dwp_file = dwarf2_per_objfile->dwp_file;
9143 
9144   if (dwp_file != NULL)
9145     {
9146       const struct dwp_hash_table *dwp_htab =
9147 	is_debug_types ? dwp_file->tus : dwp_file->cus;
9148 
9149       if (dwp_htab != NULL)
9150 	{
9151 	  struct dwo_unit *dwo_cutu =
9152 	    lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9153 
9154 	  if (dwo_cutu != NULL)
9155 	    {
9156 	      if (dwarf2_read_debug)
9157 		{
9158 		  fprintf_unfiltered (gdb_stdlog,
9159 				      "Virtual DWO %s %s found: @%s\n",
9160 				      kind, hex_string (signature),
9161 				      host_address_to_string (dwo_cutu));
9162 		}
9163 	      return dwo_cutu;
9164 	    }
9165 	}
9166     }
9167 
9168   /* Have we already seen DWO_NAME?  */
9169 
9170   dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9171   if (*dwo_file_slot == NULL)
9172     {
9173       /* Read in the file and build a table of the DWOs it contains.  */
9174       *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9175     }
9176   /* NOTE: This will be NULL if unable to open the file.  */
9177   dwo_file = *dwo_file_slot;
9178 
9179   if (dwo_file != NULL)
9180     {
9181       htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9182 
9183       if (htab != NULL)
9184 	{
9185 	  struct dwo_unit find_dwo_cutu, *dwo_cutu;
9186 
9187 	  memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9188 	  find_dwo_cutu.signature = signature;
9189 	  dwo_cutu = htab_find (htab, &find_dwo_cutu);
9190 
9191 	  if (dwo_cutu != NULL)
9192 	    {
9193 	      if (dwarf2_read_debug)
9194 		{
9195 		  fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9196 				      kind, dwo_name, hex_string (signature),
9197 				      host_address_to_string (dwo_cutu));
9198 		}
9199 	      return dwo_cutu;
9200 	    }
9201 	}
9202     }
9203 
9204   /* We didn't find it.  This could mean a dwo_id mismatch, or
9205      someone deleted the DWO/DWP file, or the search path isn't set up
9206      correctly to find the file.  */
9207 
9208   if (dwarf2_read_debug)
9209     {
9210       fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9211 			  kind, dwo_name, hex_string (signature));
9212     }
9213 
9214   complaint (&symfile_complaints,
9215 	     _("Could not find DWO CU referenced by CU at offset 0x%x"
9216 	       " [in module %s]"),
9217 	     this_unit->offset.sect_off, objfile->name);
9218   return NULL;
9219 }
9220 
9221 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9222    See lookup_dwo_cutu_unit for details.  */
9223 
9224 static struct dwo_unit *
9225 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9226 		      const char *dwo_name, const char *comp_dir,
9227 		      ULONGEST signature)
9228 {
9229   return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9230 }
9231 
9232 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9233    See lookup_dwo_cutu_unit for details.  */
9234 
9235 static struct dwo_unit *
9236 lookup_dwo_type_unit (struct signatured_type *this_tu,
9237 		      const char *dwo_name, const char *comp_dir)
9238 {
9239   return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9240 }
9241 
9242 /* Free all resources associated with DWO_FILE.
9243    Close the DWO file and munmap the sections.
9244    All memory should be on the objfile obstack.  */
9245 
9246 static void
9247 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9248 {
9249   int ix;
9250   struct dwarf2_section_info *section;
9251 
9252   /* Note: dbfd is NULL for virtual DWO files.  */
9253   gdb_bfd_unref (dwo_file->dbfd);
9254 
9255   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9256 }
9257 
9258 /* Wrapper for free_dwo_file for use in cleanups.  */
9259 
9260 static void
9261 free_dwo_file_cleanup (void *arg)
9262 {
9263   struct dwo_file *dwo_file = (struct dwo_file *) arg;
9264   struct objfile *objfile = dwarf2_per_objfile->objfile;
9265 
9266   free_dwo_file (dwo_file, objfile);
9267 }
9268 
9269 /* Traversal function for free_dwo_files.  */
9270 
9271 static int
9272 free_dwo_file_from_slot (void **slot, void *info)
9273 {
9274   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9275   struct objfile *objfile = (struct objfile *) info;
9276 
9277   free_dwo_file (dwo_file, objfile);
9278 
9279   return 1;
9280 }
9281 
9282 /* Free all resources associated with DWO_FILES.  */
9283 
9284 static void
9285 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9286 {
9287   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9288 }
9289 
9290 /* Read in various DIEs.  */
9291 
9292 /* qsort helper for inherit_abstract_dies.  */
9293 
9294 static int
9295 unsigned_int_compar (const void *ap, const void *bp)
9296 {
9297   unsigned int a = *(unsigned int *) ap;
9298   unsigned int b = *(unsigned int *) bp;
9299 
9300   return (a > b) - (b > a);
9301 }
9302 
9303 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9304    Inherit only the children of the DW_AT_abstract_origin DIE not being
9305    already referenced by DW_AT_abstract_origin from the children of the
9306    current DIE.  */
9307 
9308 static void
9309 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9310 {
9311   struct die_info *child_die;
9312   unsigned die_children_count;
9313   /* CU offsets which were referenced by children of the current DIE.  */
9314   sect_offset *offsets;
9315   sect_offset *offsets_end, *offsetp;
9316   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
9317   struct die_info *origin_die;
9318   /* Iterator of the ORIGIN_DIE children.  */
9319   struct die_info *origin_child_die;
9320   struct cleanup *cleanups;
9321   struct attribute *attr;
9322   struct dwarf2_cu *origin_cu;
9323   struct pending **origin_previous_list_in_scope;
9324 
9325   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9326   if (!attr)
9327     return;
9328 
9329   /* Note that following die references may follow to a die in a
9330      different cu.  */
9331 
9332   origin_cu = cu;
9333   origin_die = follow_die_ref (die, attr, &origin_cu);
9334 
9335   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9336      symbols in.  */
9337   origin_previous_list_in_scope = origin_cu->list_in_scope;
9338   origin_cu->list_in_scope = cu->list_in_scope;
9339 
9340   if (die->tag != origin_die->tag
9341       && !(die->tag == DW_TAG_inlined_subroutine
9342 	   && origin_die->tag == DW_TAG_subprogram))
9343     complaint (&symfile_complaints,
9344 	       _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9345 	       die->offset.sect_off, origin_die->offset.sect_off);
9346 
9347   child_die = die->child;
9348   die_children_count = 0;
9349   while (child_die && child_die->tag)
9350     {
9351       child_die = sibling_die (child_die);
9352       die_children_count++;
9353     }
9354   offsets = xmalloc (sizeof (*offsets) * die_children_count);
9355   cleanups = make_cleanup (xfree, offsets);
9356 
9357   offsets_end = offsets;
9358   child_die = die->child;
9359   while (child_die && child_die->tag)
9360     {
9361       /* For each CHILD_DIE, find the corresponding child of
9362 	 ORIGIN_DIE.  If there is more than one layer of
9363 	 DW_AT_abstract_origin, follow them all; there shouldn't be,
9364 	 but GCC versions at least through 4.4 generate this (GCC PR
9365 	 40573).  */
9366       struct die_info *child_origin_die = child_die;
9367       struct dwarf2_cu *child_origin_cu = cu;
9368 
9369       while (1)
9370 	{
9371 	  attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9372 			      child_origin_cu);
9373 	  if (attr == NULL)
9374 	    break;
9375 	  child_origin_die = follow_die_ref (child_origin_die, attr,
9376 					     &child_origin_cu);
9377 	}
9378 
9379       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9380 	 counterpart may exist.  */
9381       if (child_origin_die != child_die)
9382 	{
9383 	  if (child_die->tag != child_origin_die->tag
9384 	      && !(child_die->tag == DW_TAG_inlined_subroutine
9385 		   && child_origin_die->tag == DW_TAG_subprogram))
9386 	    complaint (&symfile_complaints,
9387 		       _("Child DIE 0x%x and its abstract origin 0x%x have "
9388 			 "different tags"), child_die->offset.sect_off,
9389 		       child_origin_die->offset.sect_off);
9390 	  if (child_origin_die->parent != origin_die)
9391 	    complaint (&symfile_complaints,
9392 		       _("Child DIE 0x%x and its abstract origin 0x%x have "
9393 			 "different parents"), child_die->offset.sect_off,
9394 		       child_origin_die->offset.sect_off);
9395 	  else
9396 	    *offsets_end++ = child_origin_die->offset;
9397 	}
9398       child_die = sibling_die (child_die);
9399     }
9400   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9401 	 unsigned_int_compar);
9402   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9403     if (offsetp[-1].sect_off == offsetp->sect_off)
9404       complaint (&symfile_complaints,
9405 		 _("Multiple children of DIE 0x%x refer "
9406 		   "to DIE 0x%x as their abstract origin"),
9407 		 die->offset.sect_off, offsetp->sect_off);
9408 
9409   offsetp = offsets;
9410   origin_child_die = origin_die->child;
9411   while (origin_child_die && origin_child_die->tag)
9412     {
9413       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
9414       while (offsetp < offsets_end
9415 	     && offsetp->sect_off < origin_child_die->offset.sect_off)
9416 	offsetp++;
9417       if (offsetp >= offsets_end
9418 	  || offsetp->sect_off > origin_child_die->offset.sect_off)
9419 	{
9420 	  /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
9421 	  process_die (origin_child_die, origin_cu);
9422 	}
9423       origin_child_die = sibling_die (origin_child_die);
9424     }
9425   origin_cu->list_in_scope = origin_previous_list_in_scope;
9426 
9427   do_cleanups (cleanups);
9428 }
9429 
9430 static void
9431 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9432 {
9433   struct objfile *objfile = cu->objfile;
9434   struct context_stack *new;
9435   CORE_ADDR lowpc;
9436   CORE_ADDR highpc;
9437   struct die_info *child_die;
9438   struct attribute *attr, *call_line, *call_file;
9439   const char *name;
9440   CORE_ADDR baseaddr;
9441   struct block *block;
9442   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9443   VEC (symbolp) *template_args = NULL;
9444   struct template_symbol *templ_func = NULL;
9445 
9446   if (inlined_func)
9447     {
9448       /* If we do not have call site information, we can't show the
9449 	 caller of this inlined function.  That's too confusing, so
9450 	 only use the scope for local variables.  */
9451       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9452       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9453       if (call_line == NULL || call_file == NULL)
9454 	{
9455 	  read_lexical_block_scope (die, cu);
9456 	  return;
9457 	}
9458     }
9459 
9460   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9461 
9462   name = dwarf2_name (die, cu);
9463 
9464   /* Ignore functions with missing or empty names.  These are actually
9465      illegal according to the DWARF standard.  */
9466   if (name == NULL)
9467     {
9468       complaint (&symfile_complaints,
9469 		 _("missing name for subprogram DIE at %d"),
9470 		 die->offset.sect_off);
9471       return;
9472     }
9473 
9474   /* Ignore functions with missing or invalid low and high pc attributes.  */
9475   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9476     {
9477       attr = dwarf2_attr (die, DW_AT_external, cu);
9478       if (!attr || !DW_UNSND (attr))
9479 	complaint (&symfile_complaints,
9480 		   _("cannot get low and high bounds "
9481 		     "for subprogram DIE at %d"),
9482 		   die->offset.sect_off);
9483       return;
9484     }
9485 
9486   lowpc += baseaddr;
9487   highpc += baseaddr;
9488 
9489   /* If we have any template arguments, then we must allocate a
9490      different sort of symbol.  */
9491   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9492     {
9493       if (child_die->tag == DW_TAG_template_type_param
9494 	  || child_die->tag == DW_TAG_template_value_param)
9495 	{
9496 	  templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9497 				       struct template_symbol);
9498 	  templ_func->base.is_cplus_template_function = 1;
9499 	  break;
9500 	}
9501     }
9502 
9503   new = push_context (0, lowpc);
9504   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9505 			       (struct symbol *) templ_func);
9506 
9507   /* If there is a location expression for DW_AT_frame_base, record
9508      it.  */
9509   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9510   if (attr)
9511     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
9512        expression is being recorded directly in the function's symbol
9513        and not in a separate frame-base object.  I guess this hack is
9514        to avoid adding some sort of frame-base adjunct/annex to the
9515        function's symbol :-(.  The problem with doing this is that it
9516        results in a function symbol with a location expression that
9517        has nothing to do with the location of the function, ouch!  The
9518        relationship should be: a function's symbol has-a frame base; a
9519        frame-base has-a location expression.  */
9520     dwarf2_symbol_mark_computed (attr, new->name, cu);
9521 
9522   cu->list_in_scope = &local_symbols;
9523 
9524   if (die->child != NULL)
9525     {
9526       child_die = die->child;
9527       while (child_die && child_die->tag)
9528 	{
9529 	  if (child_die->tag == DW_TAG_template_type_param
9530 	      || child_die->tag == DW_TAG_template_value_param)
9531 	    {
9532 	      struct symbol *arg = new_symbol (child_die, NULL, cu);
9533 
9534 	      if (arg != NULL)
9535 		VEC_safe_push (symbolp, template_args, arg);
9536 	    }
9537 	  else
9538 	    process_die (child_die, cu);
9539 	  child_die = sibling_die (child_die);
9540 	}
9541     }
9542 
9543   inherit_abstract_dies (die, cu);
9544 
9545   /* If we have a DW_AT_specification, we might need to import using
9546      directives from the context of the specification DIE.  See the
9547      comment in determine_prefix.  */
9548   if (cu->language == language_cplus
9549       && dwarf2_attr (die, DW_AT_specification, cu))
9550     {
9551       struct dwarf2_cu *spec_cu = cu;
9552       struct die_info *spec_die = die_specification (die, &spec_cu);
9553 
9554       while (spec_die)
9555 	{
9556 	  child_die = spec_die->child;
9557 	  while (child_die && child_die->tag)
9558 	    {
9559 	      if (child_die->tag == DW_TAG_imported_module)
9560 		process_die (child_die, spec_cu);
9561 	      child_die = sibling_die (child_die);
9562 	    }
9563 
9564 	  /* In some cases, GCC generates specification DIEs that
9565 	     themselves contain DW_AT_specification attributes.  */
9566 	  spec_die = die_specification (spec_die, &spec_cu);
9567 	}
9568     }
9569 
9570   new = pop_context ();
9571   /* Make a block for the local symbols within.  */
9572   block = finish_block (new->name, &local_symbols, new->old_blocks,
9573                         lowpc, highpc, objfile);
9574 
9575   /* For C++, set the block's scope.  */
9576   if ((cu->language == language_cplus || cu->language == language_fortran)
9577       && cu->processing_has_namespace_info)
9578     block_set_scope (block, determine_prefix (die, cu),
9579 		     &objfile->objfile_obstack);
9580 
9581   /* If we have address ranges, record them.  */
9582   dwarf2_record_block_ranges (die, block, baseaddr, cu);
9583 
9584   /* Attach template arguments to function.  */
9585   if (! VEC_empty (symbolp, template_args))
9586     {
9587       gdb_assert (templ_func != NULL);
9588 
9589       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9590       templ_func->template_arguments
9591 	= obstack_alloc (&objfile->objfile_obstack,
9592 			 (templ_func->n_template_arguments
9593 			  * sizeof (struct symbol *)));
9594       memcpy (templ_func->template_arguments,
9595 	      VEC_address (symbolp, template_args),
9596 	      (templ_func->n_template_arguments * sizeof (struct symbol *)));
9597       VEC_free (symbolp, template_args);
9598     }
9599 
9600   /* In C++, we can have functions nested inside functions (e.g., when
9601      a function declares a class that has methods).  This means that
9602      when we finish processing a function scope, we may need to go
9603      back to building a containing block's symbol lists.  */
9604   local_symbols = new->locals;
9605   using_directives = new->using_directives;
9606 
9607   /* If we've finished processing a top-level function, subsequent
9608      symbols go in the file symbol list.  */
9609   if (outermost_context_p ())
9610     cu->list_in_scope = &file_symbols;
9611 }
9612 
9613 /* Process all the DIES contained within a lexical block scope.  Start
9614    a new scope, process the dies, and then close the scope.  */
9615 
9616 static void
9617 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9618 {
9619   struct objfile *objfile = cu->objfile;
9620   struct context_stack *new;
9621   CORE_ADDR lowpc, highpc;
9622   struct die_info *child_die;
9623   CORE_ADDR baseaddr;
9624 
9625   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9626 
9627   /* Ignore blocks with missing or invalid low and high pc attributes.  */
9628   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9629      as multiple lexical blocks?  Handling children in a sane way would
9630      be nasty.  Might be easier to properly extend generic blocks to
9631      describe ranges.  */
9632   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9633     return;
9634   lowpc += baseaddr;
9635   highpc += baseaddr;
9636 
9637   push_context (0, lowpc);
9638   if (die->child != NULL)
9639     {
9640       child_die = die->child;
9641       while (child_die && child_die->tag)
9642 	{
9643 	  process_die (child_die, cu);
9644 	  child_die = sibling_die (child_die);
9645 	}
9646     }
9647   new = pop_context ();
9648 
9649   if (local_symbols != NULL || using_directives != NULL)
9650     {
9651       struct block *block
9652         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9653                         highpc, objfile);
9654 
9655       /* Note that recording ranges after traversing children, as we
9656          do here, means that recording a parent's ranges entails
9657          walking across all its children's ranges as they appear in
9658          the address map, which is quadratic behavior.
9659 
9660          It would be nicer to record the parent's ranges before
9661          traversing its children, simply overriding whatever you find
9662          there.  But since we don't even decide whether to create a
9663          block until after we've traversed its children, that's hard
9664          to do.  */
9665       dwarf2_record_block_ranges (die, block, baseaddr, cu);
9666     }
9667   local_symbols = new->locals;
9668   using_directives = new->using_directives;
9669 }
9670 
9671 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
9672 
9673 static void
9674 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9675 {
9676   struct objfile *objfile = cu->objfile;
9677   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9678   CORE_ADDR pc, baseaddr;
9679   struct attribute *attr;
9680   struct call_site *call_site, call_site_local;
9681   void **slot;
9682   int nparams;
9683   struct die_info *child_die;
9684 
9685   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9686 
9687   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9688   if (!attr)
9689     {
9690       complaint (&symfile_complaints,
9691 		 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9692 		   "DIE 0x%x [in module %s]"),
9693 		 die->offset.sect_off, objfile->name);
9694       return;
9695     }
9696   pc = DW_ADDR (attr) + baseaddr;
9697 
9698   if (cu->call_site_htab == NULL)
9699     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9700 					       NULL, &objfile->objfile_obstack,
9701 					       hashtab_obstack_allocate, NULL);
9702   call_site_local.pc = pc;
9703   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9704   if (*slot != NULL)
9705     {
9706       complaint (&symfile_complaints,
9707 		 _("Duplicate PC %s for DW_TAG_GNU_call_site "
9708 		   "DIE 0x%x [in module %s]"),
9709 		 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9710       return;
9711     }
9712 
9713   /* Count parameters at the caller.  */
9714 
9715   nparams = 0;
9716   for (child_die = die->child; child_die && child_die->tag;
9717        child_die = sibling_die (child_die))
9718     {
9719       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9720 	{
9721 	  complaint (&symfile_complaints,
9722 		     _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9723 		       "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9724 		     child_die->tag, child_die->offset.sect_off, objfile->name);
9725 	  continue;
9726 	}
9727 
9728       nparams++;
9729     }
9730 
9731   call_site = obstack_alloc (&objfile->objfile_obstack,
9732 			     (sizeof (*call_site)
9733 			      + (sizeof (*call_site->parameter)
9734 				 * (nparams - 1))));
9735   *slot = call_site;
9736   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9737   call_site->pc = pc;
9738 
9739   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9740     {
9741       struct die_info *func_die;
9742 
9743       /* Skip also over DW_TAG_inlined_subroutine.  */
9744       for (func_die = die->parent;
9745 	   func_die && func_die->tag != DW_TAG_subprogram
9746 	   && func_die->tag != DW_TAG_subroutine_type;
9747 	   func_die = func_die->parent);
9748 
9749       /* DW_AT_GNU_all_call_sites is a superset
9750 	 of DW_AT_GNU_all_tail_call_sites.  */
9751       if (func_die
9752           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9753 	  && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9754 	{
9755 	  /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9756 	     not complete.  But keep CALL_SITE for look ups via call_site_htab,
9757 	     both the initial caller containing the real return address PC and
9758 	     the final callee containing the current PC of a chain of tail
9759 	     calls do not need to have the tail call list complete.  But any
9760 	     function candidate for a virtual tail call frame searched via
9761 	     TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9762 	     determined unambiguously.  */
9763 	}
9764       else
9765 	{
9766 	  struct type *func_type = NULL;
9767 
9768 	  if (func_die)
9769 	    func_type = get_die_type (func_die, cu);
9770 	  if (func_type != NULL)
9771 	    {
9772 	      gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9773 
9774 	      /* Enlist this call site to the function.  */
9775 	      call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9776 	      TYPE_TAIL_CALL_LIST (func_type) = call_site;
9777 	    }
9778 	  else
9779 	    complaint (&symfile_complaints,
9780 		       _("Cannot find function owning DW_TAG_GNU_call_site "
9781 			 "DIE 0x%x [in module %s]"),
9782 		       die->offset.sect_off, objfile->name);
9783 	}
9784     }
9785 
9786   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9787   if (attr == NULL)
9788     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9789   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9790   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9791     /* Keep NULL DWARF_BLOCK.  */;
9792   else if (attr_form_is_block (attr))
9793     {
9794       struct dwarf2_locexpr_baton *dlbaton;
9795 
9796       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9797       dlbaton->data = DW_BLOCK (attr)->data;
9798       dlbaton->size = DW_BLOCK (attr)->size;
9799       dlbaton->per_cu = cu->per_cu;
9800 
9801       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9802     }
9803   else if (is_ref_attr (attr))
9804     {
9805       struct dwarf2_cu *target_cu = cu;
9806       struct die_info *target_die;
9807 
9808       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9809       gdb_assert (target_cu->objfile == objfile);
9810       if (die_is_declaration (target_die, target_cu))
9811 	{
9812 	  const char *target_physname = NULL;
9813 	  struct attribute *target_attr;
9814 
9815 	  /* Prefer the mangled name; otherwise compute the demangled one.  */
9816 	  target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9817 	  if (target_attr == NULL)
9818 	    target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9819 				       target_cu);
9820 	  if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9821 	    target_physname = DW_STRING (target_attr);
9822 	  else
9823 	    target_physname = dwarf2_physname (NULL, target_die, target_cu);
9824 	  if (target_physname == NULL)
9825 	    complaint (&symfile_complaints,
9826 		       _("DW_AT_GNU_call_site_target target DIE has invalid "
9827 		         "physname, for referencing DIE 0x%x [in module %s]"),
9828 		       die->offset.sect_off, objfile->name);
9829 	  else
9830 	    SET_FIELD_PHYSNAME (call_site->target, target_physname);
9831 	}
9832       else
9833 	{
9834 	  CORE_ADDR lowpc;
9835 
9836 	  /* DW_AT_entry_pc should be preferred.  */
9837 	  if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9838 	    complaint (&symfile_complaints,
9839 		       _("DW_AT_GNU_call_site_target target DIE has invalid "
9840 		         "low pc, for referencing DIE 0x%x [in module %s]"),
9841 		       die->offset.sect_off, objfile->name);
9842 	  else
9843 	    SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9844 	}
9845     }
9846   else
9847     complaint (&symfile_complaints,
9848 	       _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9849 		 "block nor reference, for DIE 0x%x [in module %s]"),
9850 	       die->offset.sect_off, objfile->name);
9851 
9852   call_site->per_cu = cu->per_cu;
9853 
9854   for (child_die = die->child;
9855        child_die && child_die->tag;
9856        child_die = sibling_die (child_die))
9857     {
9858       struct call_site_parameter *parameter;
9859       struct attribute *loc, *origin;
9860 
9861       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9862 	{
9863 	  /* Already printed the complaint above.  */
9864 	  continue;
9865 	}
9866 
9867       gdb_assert (call_site->parameter_count < nparams);
9868       parameter = &call_site->parameter[call_site->parameter_count];
9869 
9870       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9871 	 specifies DW_TAG_formal_parameter.  Value of the data assumed for the
9872 	 register is contained in DW_AT_GNU_call_site_value.  */
9873 
9874       loc = dwarf2_attr (child_die, DW_AT_location, cu);
9875       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9876       if (loc == NULL && origin != NULL && is_ref_attr (origin))
9877 	{
9878 	  sect_offset offset;
9879 
9880 	  parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9881 	  offset = dwarf2_get_ref_die_offset (origin);
9882 	  if (!offset_in_cu_p (&cu->header, offset))
9883 	    {
9884 	      /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9885 		 binding can be done only inside one CU.  Such referenced DIE
9886 		 therefore cannot be even moved to DW_TAG_partial_unit.  */
9887 	      complaint (&symfile_complaints,
9888 			 _("DW_AT_abstract_origin offset is not in CU for "
9889 			   "DW_TAG_GNU_call_site child DIE 0x%x "
9890 			   "[in module %s]"),
9891 			 child_die->offset.sect_off, objfile->name);
9892 	      continue;
9893 	    }
9894 	  parameter->u.param_offset.cu_off = (offset.sect_off
9895 	                                      - cu->header.offset.sect_off);
9896 	}
9897       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9898 	{
9899 	  complaint (&symfile_complaints,
9900 		     _("No DW_FORM_block* DW_AT_location for "
9901 		       "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9902 		     child_die->offset.sect_off, objfile->name);
9903 	  continue;
9904 	}
9905       else
9906 	{
9907 	  parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9908 	    (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9909 	  if (parameter->u.dwarf_reg != -1)
9910 	    parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9911 	  else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9912 				    &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9913 					     &parameter->u.fb_offset))
9914 	    parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9915 	  else
9916 	    {
9917 	      complaint (&symfile_complaints,
9918 			 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9919 			   "for DW_FORM_block* DW_AT_location is supported for "
9920 			   "DW_TAG_GNU_call_site child DIE 0x%x "
9921 			   "[in module %s]"),
9922 			 child_die->offset.sect_off, objfile->name);
9923 	      continue;
9924 	    }
9925 	}
9926 
9927       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9928       if (!attr_form_is_block (attr))
9929 	{
9930 	  complaint (&symfile_complaints,
9931 		     _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9932 		       "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9933 		     child_die->offset.sect_off, objfile->name);
9934 	  continue;
9935 	}
9936       parameter->value = DW_BLOCK (attr)->data;
9937       parameter->value_size = DW_BLOCK (attr)->size;
9938 
9939       /* Parameters are not pre-cleared by memset above.  */
9940       parameter->data_value = NULL;
9941       parameter->data_value_size = 0;
9942       call_site->parameter_count++;
9943 
9944       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9945       if (attr)
9946 	{
9947 	  if (!attr_form_is_block (attr))
9948 	    complaint (&symfile_complaints,
9949 		       _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9950 			 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9951 		       child_die->offset.sect_off, objfile->name);
9952 	  else
9953 	    {
9954 	      parameter->data_value = DW_BLOCK (attr)->data;
9955 	      parameter->data_value_size = DW_BLOCK (attr)->size;
9956 	    }
9957 	}
9958     }
9959 }
9960 
9961 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9962    Return 1 if the attributes are present and valid, otherwise, return 0.
9963    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
9964 
9965 static int
9966 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9967 		    CORE_ADDR *high_return, struct dwarf2_cu *cu,
9968 		    struct partial_symtab *ranges_pst)
9969 {
9970   struct objfile *objfile = cu->objfile;
9971   struct comp_unit_head *cu_header = &cu->header;
9972   bfd *obfd = objfile->obfd;
9973   unsigned int addr_size = cu_header->addr_size;
9974   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9975   /* Base address selection entry.  */
9976   CORE_ADDR base;
9977   int found_base;
9978   unsigned int dummy;
9979   gdb_byte *buffer;
9980   CORE_ADDR marker;
9981   int low_set;
9982   CORE_ADDR low = 0;
9983   CORE_ADDR high = 0;
9984   CORE_ADDR baseaddr;
9985 
9986   found_base = cu->base_known;
9987   base = cu->base_address;
9988 
9989   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9990   if (offset >= dwarf2_per_objfile->ranges.size)
9991     {
9992       complaint (&symfile_complaints,
9993 		 _("Offset %d out of bounds for DW_AT_ranges attribute"),
9994 		 offset);
9995       return 0;
9996     }
9997   buffer = dwarf2_per_objfile->ranges.buffer + offset;
9998 
9999   /* Read in the largest possible address.  */
10000   marker = read_address (obfd, buffer, cu, &dummy);
10001   if ((marker & mask) == mask)
10002     {
10003       /* If we found the largest possible address, then
10004 	 read the base address.  */
10005       base = read_address (obfd, buffer + addr_size, cu, &dummy);
10006       buffer += 2 * addr_size;
10007       offset += 2 * addr_size;
10008       found_base = 1;
10009     }
10010 
10011   low_set = 0;
10012 
10013   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10014 
10015   while (1)
10016     {
10017       CORE_ADDR range_beginning, range_end;
10018 
10019       range_beginning = read_address (obfd, buffer, cu, &dummy);
10020       buffer += addr_size;
10021       range_end = read_address (obfd, buffer, cu, &dummy);
10022       buffer += addr_size;
10023       offset += 2 * addr_size;
10024 
10025       /* An end of list marker is a pair of zero addresses.  */
10026       if (range_beginning == 0 && range_end == 0)
10027 	/* Found the end of list entry.  */
10028 	break;
10029 
10030       /* Each base address selection entry is a pair of 2 values.
10031 	 The first is the largest possible address, the second is
10032 	 the base address.  Check for a base address here.  */
10033       if ((range_beginning & mask) == mask)
10034 	{
10035 	  /* If we found the largest possible address, then
10036 	     read the base address.  */
10037 	  base = read_address (obfd, buffer + addr_size, cu, &dummy);
10038 	  found_base = 1;
10039 	  continue;
10040 	}
10041 
10042       if (!found_base)
10043 	{
10044 	  /* We have no valid base address for the ranges
10045 	     data.  */
10046 	  complaint (&symfile_complaints,
10047 		     _("Invalid .debug_ranges data (no base address)"));
10048 	  return 0;
10049 	}
10050 
10051       if (range_beginning > range_end)
10052 	{
10053 	  /* Inverted range entries are invalid.  */
10054 	  complaint (&symfile_complaints,
10055 		     _("Invalid .debug_ranges data (inverted range)"));
10056 	  return 0;
10057 	}
10058 
10059       /* Empty range entries have no effect.  */
10060       if (range_beginning == range_end)
10061 	continue;
10062 
10063       range_beginning += base;
10064       range_end += base;
10065 
10066       /* A not-uncommon case of bad debug info.
10067 	 Don't pollute the addrmap with bad data.  */
10068       if (range_beginning + baseaddr == 0
10069 	  && !dwarf2_per_objfile->has_section_at_zero)
10070 	{
10071 	  complaint (&symfile_complaints,
10072 		     _(".debug_ranges entry has start address of zero"
10073 		       " [in module %s]"), objfile->name);
10074 	  continue;
10075 	}
10076 
10077       if (ranges_pst != NULL)
10078 	addrmap_set_empty (objfile->psymtabs_addrmap,
10079 			   range_beginning + baseaddr,
10080 			   range_end - 1 + baseaddr,
10081 			   ranges_pst);
10082 
10083       /* FIXME: This is recording everything as a low-high
10084 	 segment of consecutive addresses.  We should have a
10085 	 data structure for discontiguous block ranges
10086 	 instead.  */
10087       if (! low_set)
10088 	{
10089 	  low = range_beginning;
10090 	  high = range_end;
10091 	  low_set = 1;
10092 	}
10093       else
10094 	{
10095 	  if (range_beginning < low)
10096 	    low = range_beginning;
10097 	  if (range_end > high)
10098 	    high = range_end;
10099 	}
10100     }
10101 
10102   if (! low_set)
10103     /* If the first entry is an end-of-list marker, the range
10104        describes an empty scope, i.e. no instructions.  */
10105     return 0;
10106 
10107   if (low_return)
10108     *low_return = low;
10109   if (high_return)
10110     *high_return = high;
10111   return 1;
10112 }
10113 
10114 /* Get low and high pc attributes from a die.  Return 1 if the attributes
10115    are present and valid, otherwise, return 0.  Return -1 if the range is
10116    discontinuous, i.e. derived from DW_AT_ranges information.  */
10117 
10118 static int
10119 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10120 		      CORE_ADDR *highpc, struct dwarf2_cu *cu,
10121 		      struct partial_symtab *pst)
10122 {
10123   struct attribute *attr;
10124   struct attribute *attr_high;
10125   CORE_ADDR low = 0;
10126   CORE_ADDR high = 0;
10127   int ret = 0;
10128 
10129   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10130   if (attr_high)
10131     {
10132       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10133       if (attr)
10134         {
10135 	  low = DW_ADDR (attr);
10136 	  if (attr_high->form == DW_FORM_addr
10137 	      || attr_high->form == DW_FORM_GNU_addr_index)
10138 	    high = DW_ADDR (attr_high);
10139 	  else
10140 	    high = low + DW_UNSND (attr_high);
10141 	}
10142       else
10143 	/* Found high w/o low attribute.  */
10144 	return 0;
10145 
10146       /* Found consecutive range of addresses.  */
10147       ret = 1;
10148     }
10149   else
10150     {
10151       attr = dwarf2_attr (die, DW_AT_ranges, cu);
10152       if (attr != NULL)
10153 	{
10154 	  /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10155 	     We take advantage of the fact that DW_AT_ranges does not appear
10156 	     in DW_TAG_compile_unit of DWO files.  */
10157 	  int need_ranges_base = die->tag != DW_TAG_compile_unit;
10158 	  unsigned int ranges_offset = (DW_UNSND (attr)
10159 					+ (need_ranges_base
10160 					   ? cu->ranges_base
10161 					   : 0));
10162 
10163 	  /* Value of the DW_AT_ranges attribute is the offset in the
10164 	     .debug_ranges section.  */
10165 	  if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10166 	    return 0;
10167 	  /* Found discontinuous range of addresses.  */
10168 	  ret = -1;
10169 	}
10170     }
10171 
10172   /* read_partial_die has also the strict LOW < HIGH requirement.  */
10173   if (high <= low)
10174     return 0;
10175 
10176   /* When using the GNU linker, .gnu.linkonce. sections are used to
10177      eliminate duplicate copies of functions and vtables and such.
10178      The linker will arbitrarily choose one and discard the others.
10179      The AT_*_pc values for such functions refer to local labels in
10180      these sections.  If the section from that file was discarded, the
10181      labels are not in the output, so the relocs get a value of 0.
10182      If this is a discarded function, mark the pc bounds as invalid,
10183      so that GDB will ignore it.  */
10184   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10185     return 0;
10186 
10187   *lowpc = low;
10188   if (highpc)
10189     *highpc = high;
10190   return ret;
10191 }
10192 
10193 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10194    its low and high PC addresses.  Do nothing if these addresses could not
10195    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
10196    and HIGHPC to the high address if greater than HIGHPC.  */
10197 
10198 static void
10199 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10200                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
10201                                  struct dwarf2_cu *cu)
10202 {
10203   CORE_ADDR low, high;
10204   struct die_info *child = die->child;
10205 
10206   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10207     {
10208       *lowpc = min (*lowpc, low);
10209       *highpc = max (*highpc, high);
10210     }
10211 
10212   /* If the language does not allow nested subprograms (either inside
10213      subprograms or lexical blocks), we're done.  */
10214   if (cu->language != language_ada)
10215     return;
10216 
10217   /* Check all the children of the given DIE.  If it contains nested
10218      subprograms, then check their pc bounds.  Likewise, we need to
10219      check lexical blocks as well, as they may also contain subprogram
10220      definitions.  */
10221   while (child && child->tag)
10222     {
10223       if (child->tag == DW_TAG_subprogram
10224           || child->tag == DW_TAG_lexical_block)
10225         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10226       child = sibling_die (child);
10227     }
10228 }
10229 
10230 /* Get the low and high pc's represented by the scope DIE, and store
10231    them in *LOWPC and *HIGHPC.  If the correct values can't be
10232    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
10233 
10234 static void
10235 get_scope_pc_bounds (struct die_info *die,
10236 		     CORE_ADDR *lowpc, CORE_ADDR *highpc,
10237 		     struct dwarf2_cu *cu)
10238 {
10239   CORE_ADDR best_low = (CORE_ADDR) -1;
10240   CORE_ADDR best_high = (CORE_ADDR) 0;
10241   CORE_ADDR current_low, current_high;
10242 
10243   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10244     {
10245       best_low = current_low;
10246       best_high = current_high;
10247     }
10248   else
10249     {
10250       struct die_info *child = die->child;
10251 
10252       while (child && child->tag)
10253 	{
10254 	  switch (child->tag) {
10255 	  case DW_TAG_subprogram:
10256             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10257 	    break;
10258 	  case DW_TAG_namespace:
10259 	  case DW_TAG_module:
10260 	    /* FIXME: carlton/2004-01-16: Should we do this for
10261 	       DW_TAG_class_type/DW_TAG_structure_type, too?  I think
10262 	       that current GCC's always emit the DIEs corresponding
10263 	       to definitions of methods of classes as children of a
10264 	       DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10265 	       the DIEs giving the declarations, which could be
10266 	       anywhere).  But I don't see any reason why the
10267 	       standards says that they have to be there.  */
10268 	    get_scope_pc_bounds (child, &current_low, &current_high, cu);
10269 
10270 	    if (current_low != ((CORE_ADDR) -1))
10271 	      {
10272 		best_low = min (best_low, current_low);
10273 		best_high = max (best_high, current_high);
10274 	      }
10275 	    break;
10276 	  default:
10277 	    /* Ignore.  */
10278 	    break;
10279 	  }
10280 
10281 	  child = sibling_die (child);
10282 	}
10283     }
10284 
10285   *lowpc = best_low;
10286   *highpc = best_high;
10287 }
10288 
10289 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10290    in DIE.  */
10291 
10292 static void
10293 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10294                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10295 {
10296   struct objfile *objfile = cu->objfile;
10297   struct attribute *attr;
10298   struct attribute *attr_high;
10299 
10300   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10301   if (attr_high)
10302     {
10303       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10304       if (attr)
10305         {
10306           CORE_ADDR low = DW_ADDR (attr);
10307 	  CORE_ADDR high;
10308 	  if (attr_high->form == DW_FORM_addr
10309 	      || attr_high->form == DW_FORM_GNU_addr_index)
10310 	    high = DW_ADDR (attr_high);
10311 	  else
10312 	    high = low + DW_UNSND (attr_high);
10313 
10314           record_block_range (block, baseaddr + low, baseaddr + high - 1);
10315         }
10316     }
10317 
10318   attr = dwarf2_attr (die, DW_AT_ranges, cu);
10319   if (attr)
10320     {
10321       bfd *obfd = objfile->obfd;
10322       /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10323 	 We take advantage of the fact that DW_AT_ranges does not appear
10324 	 in DW_TAG_compile_unit of DWO files.  */
10325       int need_ranges_base = die->tag != DW_TAG_compile_unit;
10326 
10327       /* The value of the DW_AT_ranges attribute is the offset of the
10328          address range list in the .debug_ranges section.  */
10329       unsigned long offset = (DW_UNSND (attr)
10330 			      + (need_ranges_base ? cu->ranges_base : 0));
10331       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10332 
10333       /* For some target architectures, but not others, the
10334          read_address function sign-extends the addresses it returns.
10335          To recognize base address selection entries, we need a
10336          mask.  */
10337       unsigned int addr_size = cu->header.addr_size;
10338       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10339 
10340       /* The base address, to which the next pair is relative.  Note
10341          that this 'base' is a DWARF concept: most entries in a range
10342          list are relative, to reduce the number of relocs against the
10343          debugging information.  This is separate from this function's
10344          'baseaddr' argument, which GDB uses to relocate debugging
10345          information from a shared library based on the address at
10346          which the library was loaded.  */
10347       CORE_ADDR base = cu->base_address;
10348       int base_known = cu->base_known;
10349 
10350       gdb_assert (dwarf2_per_objfile->ranges.readin);
10351       if (offset >= dwarf2_per_objfile->ranges.size)
10352         {
10353           complaint (&symfile_complaints,
10354                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10355                      offset);
10356           return;
10357         }
10358 
10359       for (;;)
10360         {
10361           unsigned int bytes_read;
10362           CORE_ADDR start, end;
10363 
10364           start = read_address (obfd, buffer, cu, &bytes_read);
10365           buffer += bytes_read;
10366           end = read_address (obfd, buffer, cu, &bytes_read);
10367           buffer += bytes_read;
10368 
10369           /* Did we find the end of the range list?  */
10370           if (start == 0 && end == 0)
10371             break;
10372 
10373           /* Did we find a base address selection entry?  */
10374           else if ((start & base_select_mask) == base_select_mask)
10375             {
10376               base = end;
10377               base_known = 1;
10378             }
10379 
10380           /* We found an ordinary address range.  */
10381           else
10382             {
10383               if (!base_known)
10384                 {
10385                   complaint (&symfile_complaints,
10386 			     _("Invalid .debug_ranges data "
10387 			       "(no base address)"));
10388                   return;
10389                 }
10390 
10391 	      if (start > end)
10392 		{
10393 		  /* Inverted range entries are invalid.  */
10394 		  complaint (&symfile_complaints,
10395 			     _("Invalid .debug_ranges data "
10396 			       "(inverted range)"));
10397 		  return;
10398 		}
10399 
10400 	      /* Empty range entries have no effect.  */
10401 	      if (start == end)
10402 		continue;
10403 
10404 	      start += base + baseaddr;
10405 	      end += base + baseaddr;
10406 
10407 	      /* A not-uncommon case of bad debug info.
10408 		 Don't pollute the addrmap with bad data.  */
10409 	      if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10410 		{
10411 		  complaint (&symfile_complaints,
10412 			     _(".debug_ranges entry has start address of zero"
10413 			       " [in module %s]"), objfile->name);
10414 		  continue;
10415 		}
10416 
10417               record_block_range (block, start, end - 1);
10418             }
10419         }
10420     }
10421 }
10422 
10423 /* Check whether the producer field indicates either of GCC < 4.6, or the
10424    Intel C/C++ compiler, and cache the result in CU.  */
10425 
10426 static void
10427 check_producer (struct dwarf2_cu *cu)
10428 {
10429   const char *cs;
10430   int major, minor, release;
10431 
10432   if (cu->producer == NULL)
10433     {
10434       /* For unknown compilers expect their behavior is DWARF version
10435 	 compliant.
10436 
10437 	 GCC started to support .debug_types sections by -gdwarf-4 since
10438 	 gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
10439 	 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10440 	 combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10441 	 interpreted incorrectly by GDB now - GCC PR debug/48229.  */
10442     }
10443   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10444     {
10445       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
10446 
10447       cs = &cu->producer[strlen ("GNU ")];
10448       while (*cs && !isdigit (*cs))
10449 	cs++;
10450       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10451 	{
10452 	  /* Not recognized as GCC.  */
10453 	}
10454       else
10455 	{
10456 	  cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10457 	  cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10458 	}
10459     }
10460   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10461     cu->producer_is_icc = 1;
10462   else
10463     {
10464       /* For other non-GCC compilers, expect their behavior is DWARF version
10465 	 compliant.  */
10466     }
10467 
10468   cu->checked_producer = 1;
10469 }
10470 
10471 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10472    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10473    during 4.6.0 experimental.  */
10474 
10475 static int
10476 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10477 {
10478   if (!cu->checked_producer)
10479     check_producer (cu);
10480 
10481   return cu->producer_is_gxx_lt_4_6;
10482 }
10483 
10484 /* Return the default accessibility type if it is not overriden by
10485    DW_AT_accessibility.  */
10486 
10487 static enum dwarf_access_attribute
10488 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10489 {
10490   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10491     {
10492       /* The default DWARF 2 accessibility for members is public, the default
10493 	 accessibility for inheritance is private.  */
10494 
10495       if (die->tag != DW_TAG_inheritance)
10496 	return DW_ACCESS_public;
10497       else
10498 	return DW_ACCESS_private;
10499     }
10500   else
10501     {
10502       /* DWARF 3+ defines the default accessibility a different way.  The same
10503 	 rules apply now for DW_TAG_inheritance as for the members and it only
10504 	 depends on the container kind.  */
10505 
10506       if (die->parent->tag == DW_TAG_class_type)
10507 	return DW_ACCESS_private;
10508       else
10509 	return DW_ACCESS_public;
10510     }
10511 }
10512 
10513 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
10514    offset.  If the attribute was not found return 0, otherwise return
10515    1.  If it was found but could not properly be handled, set *OFFSET
10516    to 0.  */
10517 
10518 static int
10519 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10520 			     LONGEST *offset)
10521 {
10522   struct attribute *attr;
10523 
10524   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10525   if (attr != NULL)
10526     {
10527       *offset = 0;
10528 
10529       /* Note that we do not check for a section offset first here.
10530 	 This is because DW_AT_data_member_location is new in DWARF 4,
10531 	 so if we see it, we can assume that a constant form is really
10532 	 a constant and not a section offset.  */
10533       if (attr_form_is_constant (attr))
10534 	*offset = dwarf2_get_attr_constant_value (attr, 0);
10535       else if (attr_form_is_section_offset (attr))
10536 	dwarf2_complex_location_expr_complaint ();
10537       else if (attr_form_is_block (attr))
10538 	*offset = decode_locdesc (DW_BLOCK (attr), cu);
10539       else
10540 	dwarf2_complex_location_expr_complaint ();
10541 
10542       return 1;
10543     }
10544 
10545   return 0;
10546 }
10547 
10548 /* Add an aggregate field to the field list.  */
10549 
10550 static void
10551 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10552 		  struct dwarf2_cu *cu)
10553 {
10554   struct objfile *objfile = cu->objfile;
10555   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10556   struct nextfield *new_field;
10557   struct attribute *attr;
10558   struct field *fp;
10559   const char *fieldname = "";
10560 
10561   /* Allocate a new field list entry and link it in.  */
10562   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10563   make_cleanup (xfree, new_field);
10564   memset (new_field, 0, sizeof (struct nextfield));
10565 
10566   if (die->tag == DW_TAG_inheritance)
10567     {
10568       new_field->next = fip->baseclasses;
10569       fip->baseclasses = new_field;
10570     }
10571   else
10572     {
10573       new_field->next = fip->fields;
10574       fip->fields = new_field;
10575     }
10576   fip->nfields++;
10577 
10578   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10579   if (attr)
10580     new_field->accessibility = DW_UNSND (attr);
10581   else
10582     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10583   if (new_field->accessibility != DW_ACCESS_public)
10584     fip->non_public_fields = 1;
10585 
10586   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10587   if (attr)
10588     new_field->virtuality = DW_UNSND (attr);
10589   else
10590     new_field->virtuality = DW_VIRTUALITY_none;
10591 
10592   fp = &new_field->field;
10593 
10594   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10595     {
10596       LONGEST offset;
10597 
10598       /* Data member other than a C++ static data member.  */
10599 
10600       /* Get type of field.  */
10601       fp->type = die_type (die, cu);
10602 
10603       SET_FIELD_BITPOS (*fp, 0);
10604 
10605       /* Get bit size of field (zero if none).  */
10606       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10607       if (attr)
10608 	{
10609 	  FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10610 	}
10611       else
10612 	{
10613 	  FIELD_BITSIZE (*fp) = 0;
10614 	}
10615 
10616       /* Get bit offset of field.  */
10617       if (handle_data_member_location (die, cu, &offset))
10618 	SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10619       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10620       if (attr)
10621 	{
10622 	  if (gdbarch_bits_big_endian (gdbarch))
10623 	    {
10624 	      /* For big endian bits, the DW_AT_bit_offset gives the
10625 	         additional bit offset from the MSB of the containing
10626 	         anonymous object to the MSB of the field.  We don't
10627 	         have to do anything special since we don't need to
10628 	         know the size of the anonymous object.  */
10629 	      SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10630 	    }
10631 	  else
10632 	    {
10633 	      /* For little endian bits, compute the bit offset to the
10634 	         MSB of the anonymous object, subtract off the number of
10635 	         bits from the MSB of the field to the MSB of the
10636 	         object, and then subtract off the number of bits of
10637 	         the field itself.  The result is the bit offset of
10638 	         the LSB of the field.  */
10639 	      int anonymous_size;
10640 	      int bit_offset = DW_UNSND (attr);
10641 
10642 	      attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10643 	      if (attr)
10644 		{
10645 		  /* The size of the anonymous object containing
10646 		     the bit field is explicit, so use the
10647 		     indicated size (in bytes).  */
10648 		  anonymous_size = DW_UNSND (attr);
10649 		}
10650 	      else
10651 		{
10652 		  /* The size of the anonymous object containing
10653 		     the bit field must be inferred from the type
10654 		     attribute of the data member containing the
10655 		     bit field.  */
10656 		  anonymous_size = TYPE_LENGTH (fp->type);
10657 		}
10658 	      SET_FIELD_BITPOS (*fp,
10659 				(FIELD_BITPOS (*fp)
10660 				 + anonymous_size * bits_per_byte
10661 				 - bit_offset - FIELD_BITSIZE (*fp)));
10662 	    }
10663 	}
10664 
10665       /* Get name of field.  */
10666       fieldname = dwarf2_name (die, cu);
10667       if (fieldname == NULL)
10668 	fieldname = "";
10669 
10670       /* The name is already allocated along with this objfile, so we don't
10671 	 need to duplicate it for the type.  */
10672       fp->name = fieldname;
10673 
10674       /* Change accessibility for artificial fields (e.g. virtual table
10675          pointer or virtual base class pointer) to private.  */
10676       if (dwarf2_attr (die, DW_AT_artificial, cu))
10677 	{
10678 	  FIELD_ARTIFICIAL (*fp) = 1;
10679 	  new_field->accessibility = DW_ACCESS_private;
10680 	  fip->non_public_fields = 1;
10681 	}
10682     }
10683   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10684     {
10685       /* C++ static member.  */
10686 
10687       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10688 	 is a declaration, but all versions of G++ as of this writing
10689 	 (so through at least 3.2.1) incorrectly generate
10690 	 DW_TAG_variable tags.  */
10691 
10692       const char *physname;
10693 
10694       /* Get name of field.  */
10695       fieldname = dwarf2_name (die, cu);
10696       if (fieldname == NULL)
10697 	return;
10698 
10699       attr = dwarf2_attr (die, DW_AT_const_value, cu);
10700       if (attr
10701 	  /* Only create a symbol if this is an external value.
10702 	     new_symbol checks this and puts the value in the global symbol
10703 	     table, which we want.  If it is not external, new_symbol
10704 	     will try to put the value in cu->list_in_scope which is wrong.  */
10705 	  && dwarf2_flag_true_p (die, DW_AT_external, cu))
10706 	{
10707 	  /* A static const member, not much different than an enum as far as
10708 	     we're concerned, except that we can support more types.  */
10709 	  new_symbol (die, NULL, cu);
10710 	}
10711 
10712       /* Get physical name.  */
10713       physname = dwarf2_physname (fieldname, die, cu);
10714 
10715       /* The name is already allocated along with this objfile, so we don't
10716 	 need to duplicate it for the type.  */
10717       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10718       FIELD_TYPE (*fp) = die_type (die, cu);
10719       FIELD_NAME (*fp) = fieldname;
10720     }
10721   else if (die->tag == DW_TAG_inheritance)
10722     {
10723       LONGEST offset;
10724 
10725       /* C++ base class field.  */
10726       if (handle_data_member_location (die, cu, &offset))
10727 	SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10728       FIELD_BITSIZE (*fp) = 0;
10729       FIELD_TYPE (*fp) = die_type (die, cu);
10730       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10731       fip->nbaseclasses++;
10732     }
10733 }
10734 
10735 /* Add a typedef defined in the scope of the FIP's class.  */
10736 
10737 static void
10738 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10739 		    struct dwarf2_cu *cu)
10740 {
10741   struct objfile *objfile = cu->objfile;
10742   struct typedef_field_list *new_field;
10743   struct attribute *attr;
10744   struct typedef_field *fp;
10745   char *fieldname = "";
10746 
10747   /* Allocate a new field list entry and link it in.  */
10748   new_field = xzalloc (sizeof (*new_field));
10749   make_cleanup (xfree, new_field);
10750 
10751   gdb_assert (die->tag == DW_TAG_typedef);
10752 
10753   fp = &new_field->field;
10754 
10755   /* Get name of field.  */
10756   fp->name = dwarf2_name (die, cu);
10757   if (fp->name == NULL)
10758     return;
10759 
10760   fp->type = read_type_die (die, cu);
10761 
10762   new_field->next = fip->typedef_field_list;
10763   fip->typedef_field_list = new_field;
10764   fip->typedef_field_list_count++;
10765 }
10766 
10767 /* Create the vector of fields, and attach it to the type.  */
10768 
10769 static void
10770 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10771 			      struct dwarf2_cu *cu)
10772 {
10773   int nfields = fip->nfields;
10774 
10775   /* Record the field count, allocate space for the array of fields,
10776      and create blank accessibility bitfields if necessary.  */
10777   TYPE_NFIELDS (type) = nfields;
10778   TYPE_FIELDS (type) = (struct field *)
10779     TYPE_ALLOC (type, sizeof (struct field) * nfields);
10780   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10781 
10782   if (fip->non_public_fields && cu->language != language_ada)
10783     {
10784       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10785 
10786       TYPE_FIELD_PRIVATE_BITS (type) =
10787 	(B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10788       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10789 
10790       TYPE_FIELD_PROTECTED_BITS (type) =
10791 	(B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10792       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10793 
10794       TYPE_FIELD_IGNORE_BITS (type) =
10795 	(B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10796       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10797     }
10798 
10799   /* If the type has baseclasses, allocate and clear a bit vector for
10800      TYPE_FIELD_VIRTUAL_BITS.  */
10801   if (fip->nbaseclasses && cu->language != language_ada)
10802     {
10803       int num_bytes = B_BYTES (fip->nbaseclasses);
10804       unsigned char *pointer;
10805 
10806       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10807       pointer = TYPE_ALLOC (type, num_bytes);
10808       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10809       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10810       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10811     }
10812 
10813   /* Copy the saved-up fields into the field vector.  Start from the head of
10814      the list, adding to the tail of the field array, so that they end up in
10815      the same order in the array in which they were added to the list.  */
10816   while (nfields-- > 0)
10817     {
10818       struct nextfield *fieldp;
10819 
10820       if (fip->fields)
10821 	{
10822 	  fieldp = fip->fields;
10823 	  fip->fields = fieldp->next;
10824 	}
10825       else
10826 	{
10827 	  fieldp = fip->baseclasses;
10828 	  fip->baseclasses = fieldp->next;
10829 	}
10830 
10831       TYPE_FIELD (type, nfields) = fieldp->field;
10832       switch (fieldp->accessibility)
10833 	{
10834 	case DW_ACCESS_private:
10835 	  if (cu->language != language_ada)
10836 	    SET_TYPE_FIELD_PRIVATE (type, nfields);
10837 	  break;
10838 
10839 	case DW_ACCESS_protected:
10840 	  if (cu->language != language_ada)
10841 	    SET_TYPE_FIELD_PROTECTED (type, nfields);
10842 	  break;
10843 
10844 	case DW_ACCESS_public:
10845 	  break;
10846 
10847 	default:
10848 	  /* Unknown accessibility.  Complain and treat it as public.  */
10849 	  {
10850 	    complaint (&symfile_complaints, _("unsupported accessibility %d"),
10851 		       fieldp->accessibility);
10852 	  }
10853 	  break;
10854 	}
10855       if (nfields < fip->nbaseclasses)
10856 	{
10857 	  switch (fieldp->virtuality)
10858 	    {
10859 	    case DW_VIRTUALITY_virtual:
10860 	    case DW_VIRTUALITY_pure_virtual:
10861 	      if (cu->language == language_ada)
10862 		error (_("unexpected virtuality in component of Ada type"));
10863 	      SET_TYPE_FIELD_VIRTUAL (type, nfields);
10864 	      break;
10865 	    }
10866 	}
10867     }
10868 }
10869 
10870 /* Return true if this member function is a constructor, false
10871    otherwise.  */
10872 
10873 static int
10874 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10875 {
10876   const char *fieldname;
10877   const char *typename;
10878   int len;
10879 
10880   if (die->parent == NULL)
10881     return 0;
10882 
10883   if (die->parent->tag != DW_TAG_structure_type
10884       && die->parent->tag != DW_TAG_union_type
10885       && die->parent->tag != DW_TAG_class_type)
10886     return 0;
10887 
10888   fieldname = dwarf2_name (die, cu);
10889   typename = dwarf2_name (die->parent, cu);
10890   if (fieldname == NULL || typename == NULL)
10891     return 0;
10892 
10893   len = strlen (fieldname);
10894   return (strncmp (fieldname, typename, len) == 0
10895 	  && (typename[len] == '\0' || typename[len] == '<'));
10896 }
10897 
10898 /* Add a member function to the proper fieldlist.  */
10899 
10900 static void
10901 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10902 		      struct type *type, struct dwarf2_cu *cu)
10903 {
10904   struct objfile *objfile = cu->objfile;
10905   struct attribute *attr;
10906   struct fnfieldlist *flp;
10907   int i;
10908   struct fn_field *fnp;
10909   const char *fieldname;
10910   struct nextfnfield *new_fnfield;
10911   struct type *this_type;
10912   enum dwarf_access_attribute accessibility;
10913 
10914   if (cu->language == language_ada)
10915     error (_("unexpected member function in Ada type"));
10916 
10917   /* Get name of member function.  */
10918   fieldname = dwarf2_name (die, cu);
10919   if (fieldname == NULL)
10920     return;
10921 
10922   /* Look up member function name in fieldlist.  */
10923   for (i = 0; i < fip->nfnfields; i++)
10924     {
10925       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10926 	break;
10927     }
10928 
10929   /* Create new list element if necessary.  */
10930   if (i < fip->nfnfields)
10931     flp = &fip->fnfieldlists[i];
10932   else
10933     {
10934       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10935 	{
10936 	  fip->fnfieldlists = (struct fnfieldlist *)
10937 	    xrealloc (fip->fnfieldlists,
10938 		      (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10939 		      * sizeof (struct fnfieldlist));
10940 	  if (fip->nfnfields == 0)
10941 	    make_cleanup (free_current_contents, &fip->fnfieldlists);
10942 	}
10943       flp = &fip->fnfieldlists[fip->nfnfields];
10944       flp->name = fieldname;
10945       flp->length = 0;
10946       flp->head = NULL;
10947       i = fip->nfnfields++;
10948     }
10949 
10950   /* Create a new member function field and chain it to the field list
10951      entry.  */
10952   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10953   make_cleanup (xfree, new_fnfield);
10954   memset (new_fnfield, 0, sizeof (struct nextfnfield));
10955   new_fnfield->next = flp->head;
10956   flp->head = new_fnfield;
10957   flp->length++;
10958 
10959   /* Fill in the member function field info.  */
10960   fnp = &new_fnfield->fnfield;
10961 
10962   /* Delay processing of the physname until later.  */
10963   if (cu->language == language_cplus || cu->language == language_java)
10964     {
10965       add_to_method_list (type, i, flp->length - 1, fieldname,
10966 			  die, cu);
10967     }
10968   else
10969     {
10970       const char *physname = dwarf2_physname (fieldname, die, cu);
10971       fnp->physname = physname ? physname : "";
10972     }
10973 
10974   fnp->type = alloc_type (objfile);
10975   this_type = read_type_die (die, cu);
10976   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10977     {
10978       int nparams = TYPE_NFIELDS (this_type);
10979 
10980       /* TYPE is the domain of this method, and THIS_TYPE is the type
10981 	   of the method itself (TYPE_CODE_METHOD).  */
10982       smash_to_method_type (fnp->type, type,
10983 			    TYPE_TARGET_TYPE (this_type),
10984 			    TYPE_FIELDS (this_type),
10985 			    TYPE_NFIELDS (this_type),
10986 			    TYPE_VARARGS (this_type));
10987 
10988       /* Handle static member functions.
10989          Dwarf2 has no clean way to discern C++ static and non-static
10990          member functions.  G++ helps GDB by marking the first
10991          parameter for non-static member functions (which is the this
10992          pointer) as artificial.  We obtain this information from
10993          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
10994       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10995 	fnp->voffset = VOFFSET_STATIC;
10996     }
10997   else
10998     complaint (&symfile_complaints, _("member function type missing for '%s'"),
10999 	       dwarf2_full_name (fieldname, die, cu));
11000 
11001   /* Get fcontext from DW_AT_containing_type if present.  */
11002   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11003     fnp->fcontext = die_containing_type (die, cu);
11004 
11005   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11006      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
11007 
11008   /* Get accessibility.  */
11009   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11010   if (attr)
11011     accessibility = DW_UNSND (attr);
11012   else
11013     accessibility = dwarf2_default_access_attribute (die, cu);
11014   switch (accessibility)
11015     {
11016     case DW_ACCESS_private:
11017       fnp->is_private = 1;
11018       break;
11019     case DW_ACCESS_protected:
11020       fnp->is_protected = 1;
11021       break;
11022     }
11023 
11024   /* Check for artificial methods.  */
11025   attr = dwarf2_attr (die, DW_AT_artificial, cu);
11026   if (attr && DW_UNSND (attr) != 0)
11027     fnp->is_artificial = 1;
11028 
11029   fnp->is_constructor = dwarf2_is_constructor (die, cu);
11030 
11031   /* Get index in virtual function table if it is a virtual member
11032      function.  For older versions of GCC, this is an offset in the
11033      appropriate virtual table, as specified by DW_AT_containing_type.
11034      For everyone else, it is an expression to be evaluated relative
11035      to the object address.  */
11036 
11037   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11038   if (attr)
11039     {
11040       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11041         {
11042 	  if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11043 	    {
11044 	      /* Old-style GCC.  */
11045 	      fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11046 	    }
11047 	  else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11048 		   || (DW_BLOCK (attr)->size > 1
11049 		       && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11050 		       && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11051 	    {
11052 	      struct dwarf_block blk;
11053 	      int offset;
11054 
11055 	      offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11056 			? 1 : 2);
11057 	      blk.size = DW_BLOCK (attr)->size - offset;
11058 	      blk.data = DW_BLOCK (attr)->data + offset;
11059 	      fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11060 	      if ((fnp->voffset % cu->header.addr_size) != 0)
11061 		dwarf2_complex_location_expr_complaint ();
11062 	      else
11063 		fnp->voffset /= cu->header.addr_size;
11064 	      fnp->voffset += 2;
11065 	    }
11066 	  else
11067 	    dwarf2_complex_location_expr_complaint ();
11068 
11069 	  if (!fnp->fcontext)
11070 	    fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11071 	}
11072       else if (attr_form_is_section_offset (attr))
11073         {
11074 	  dwarf2_complex_location_expr_complaint ();
11075         }
11076       else
11077         {
11078 	  dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11079 						 fieldname);
11080         }
11081     }
11082   else
11083     {
11084       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11085       if (attr && DW_UNSND (attr))
11086 	{
11087 	  /* GCC does this, as of 2008-08-25; PR debug/37237.  */
11088 	  complaint (&symfile_complaints,
11089 		     _("Member function \"%s\" (offset %d) is virtual "
11090 		       "but the vtable offset is not specified"),
11091 		     fieldname, die->offset.sect_off);
11092 	  ALLOCATE_CPLUS_STRUCT_TYPE (type);
11093 	  TYPE_CPLUS_DYNAMIC (type) = 1;
11094 	}
11095     }
11096 }
11097 
11098 /* Create the vector of member function fields, and attach it to the type.  */
11099 
11100 static void
11101 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11102 				 struct dwarf2_cu *cu)
11103 {
11104   struct fnfieldlist *flp;
11105   int i;
11106 
11107   if (cu->language == language_ada)
11108     error (_("unexpected member functions in Ada type"));
11109 
11110   ALLOCATE_CPLUS_STRUCT_TYPE (type);
11111   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11112     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11113 
11114   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11115     {
11116       struct nextfnfield *nfp = flp->head;
11117       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11118       int k;
11119 
11120       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11121       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11122       fn_flp->fn_fields = (struct fn_field *)
11123 	TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11124       for (k = flp->length; (k--, nfp); nfp = nfp->next)
11125 	fn_flp->fn_fields[k] = nfp->fnfield;
11126     }
11127 
11128   TYPE_NFN_FIELDS (type) = fip->nfnfields;
11129 }
11130 
11131 /* Returns non-zero if NAME is the name of a vtable member in CU's
11132    language, zero otherwise.  */
11133 static int
11134 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11135 {
11136   static const char vptr[] = "_vptr";
11137   static const char vtable[] = "vtable";
11138 
11139   /* Look for the C++ and Java forms of the vtable.  */
11140   if ((cu->language == language_java
11141        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11142        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11143        && is_cplus_marker (name[sizeof (vptr) - 1])))
11144     return 1;
11145 
11146   return 0;
11147 }
11148 
11149 /* GCC outputs unnamed structures that are really pointers to member
11150    functions, with the ABI-specified layout.  If TYPE describes
11151    such a structure, smash it into a member function type.
11152 
11153    GCC shouldn't do this; it should just output pointer to member DIEs.
11154    This is GCC PR debug/28767.  */
11155 
11156 static void
11157 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11158 {
11159   struct type *pfn_type, *domain_type, *new_type;
11160 
11161   /* Check for a structure with no name and two children.  */
11162   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11163     return;
11164 
11165   /* Check for __pfn and __delta members.  */
11166   if (TYPE_FIELD_NAME (type, 0) == NULL
11167       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11168       || TYPE_FIELD_NAME (type, 1) == NULL
11169       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11170     return;
11171 
11172   /* Find the type of the method.  */
11173   pfn_type = TYPE_FIELD_TYPE (type, 0);
11174   if (pfn_type == NULL
11175       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11176       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11177     return;
11178 
11179   /* Look for the "this" argument.  */
11180   pfn_type = TYPE_TARGET_TYPE (pfn_type);
11181   if (TYPE_NFIELDS (pfn_type) == 0
11182       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11183       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11184     return;
11185 
11186   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11187   new_type = alloc_type (objfile);
11188   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11189 			TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11190 			TYPE_VARARGS (pfn_type));
11191   smash_to_methodptr_type (type, new_type);
11192 }
11193 
11194 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11195    (icc).  */
11196 
11197 static int
11198 producer_is_icc (struct dwarf2_cu *cu)
11199 {
11200   if (!cu->checked_producer)
11201     check_producer (cu);
11202 
11203   return cu->producer_is_icc;
11204 }
11205 
11206 /* Called when we find the DIE that starts a structure or union scope
11207    (definition) to create a type for the structure or union.  Fill in
11208    the type's name and general properties; the members will not be
11209    processed until process_structure_type.
11210 
11211    NOTE: we need to call these functions regardless of whether or not the
11212    DIE has a DW_AT_name attribute, since it might be an anonymous
11213    structure or union.  This gets the type entered into our set of
11214    user defined types.
11215 
11216    However, if the structure is incomplete (an opaque struct/union)
11217    then suppress creating a symbol table entry for it since gdb only
11218    wants to find the one with the complete definition.  Note that if
11219    it is complete, we just call new_symbol, which does it's own
11220    checking about whether the struct/union is anonymous or not (and
11221    suppresses creating a symbol table entry itself).  */
11222 
11223 static struct type *
11224 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11225 {
11226   struct objfile *objfile = cu->objfile;
11227   struct type *type;
11228   struct attribute *attr;
11229   const char *name;
11230 
11231   /* If the definition of this type lives in .debug_types, read that type.
11232      Don't follow DW_AT_specification though, that will take us back up
11233      the chain and we want to go down.  */
11234   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11235   if (attr)
11236     {
11237       struct dwarf2_cu *type_cu = cu;
11238       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11239 
11240       /* We could just recurse on read_structure_type, but we need to call
11241 	 get_die_type to ensure only one type for this DIE is created.
11242 	 This is important, for example, because for c++ classes we need
11243 	 TYPE_NAME set which is only done by new_symbol.  Blech.  */
11244       type = read_type_die (type_die, type_cu);
11245 
11246       /* TYPE_CU may not be the same as CU.
11247 	 Ensure TYPE is recorded in CU's type_hash table.  */
11248       return set_die_type (die, type, cu);
11249     }
11250 
11251   type = alloc_type (objfile);
11252   INIT_CPLUS_SPECIFIC (type);
11253 
11254   name = dwarf2_name (die, cu);
11255   if (name != NULL)
11256     {
11257       if (cu->language == language_cplus
11258 	  || cu->language == language_java)
11259 	{
11260 	  const char *full_name = dwarf2_full_name (name, die, cu);
11261 
11262 	  /* dwarf2_full_name might have already finished building the DIE's
11263 	     type.  If so, there is no need to continue.  */
11264 	  if (get_die_type (die, cu) != NULL)
11265 	    return get_die_type (die, cu);
11266 
11267 	  TYPE_TAG_NAME (type) = full_name;
11268 	  if (die->tag == DW_TAG_structure_type
11269 	      || die->tag == DW_TAG_class_type)
11270 	    TYPE_NAME (type) = TYPE_TAG_NAME (type);
11271 	}
11272       else
11273 	{
11274 	  /* The name is already allocated along with this objfile, so
11275 	     we don't need to duplicate it for the type.  */
11276 	  TYPE_TAG_NAME (type) = name;
11277 	  if (die->tag == DW_TAG_class_type)
11278 	    TYPE_NAME (type) = TYPE_TAG_NAME (type);
11279 	}
11280     }
11281 
11282   if (die->tag == DW_TAG_structure_type)
11283     {
11284       TYPE_CODE (type) = TYPE_CODE_STRUCT;
11285     }
11286   else if (die->tag == DW_TAG_union_type)
11287     {
11288       TYPE_CODE (type) = TYPE_CODE_UNION;
11289     }
11290   else
11291     {
11292       TYPE_CODE (type) = TYPE_CODE_CLASS;
11293     }
11294 
11295   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11296     TYPE_DECLARED_CLASS (type) = 1;
11297 
11298   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11299   if (attr)
11300     {
11301       TYPE_LENGTH (type) = DW_UNSND (attr);
11302     }
11303   else
11304     {
11305       TYPE_LENGTH (type) = 0;
11306     }
11307 
11308   if (producer_is_icc (cu))
11309     {
11310       /* ICC does not output the required DW_AT_declaration
11311 	 on incomplete types, but gives them a size of zero.  */
11312     }
11313   else
11314     TYPE_STUB_SUPPORTED (type) = 1;
11315 
11316   if (die_is_declaration (die, cu))
11317     TYPE_STUB (type) = 1;
11318   else if (attr == NULL && die->child == NULL
11319 	   && producer_is_realview (cu->producer))
11320     /* RealView does not output the required DW_AT_declaration
11321        on incomplete types.  */
11322     TYPE_STUB (type) = 1;
11323 
11324   /* We need to add the type field to the die immediately so we don't
11325      infinitely recurse when dealing with pointers to the structure
11326      type within the structure itself.  */
11327   set_die_type (die, type, cu);
11328 
11329   /* set_die_type should be already done.  */
11330   set_descriptive_type (type, die, cu);
11331 
11332   return type;
11333 }
11334 
11335 /* Finish creating a structure or union type, including filling in
11336    its members and creating a symbol for it.  */
11337 
11338 static void
11339 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11340 {
11341   struct objfile *objfile = cu->objfile;
11342   struct die_info *child_die = die->child;
11343   struct type *type;
11344 
11345   type = get_die_type (die, cu);
11346   if (type == NULL)
11347     type = read_structure_type (die, cu);
11348 
11349   if (die->child != NULL && ! die_is_declaration (die, cu))
11350     {
11351       struct field_info fi;
11352       struct die_info *child_die;
11353       VEC (symbolp) *template_args = NULL;
11354       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11355 
11356       memset (&fi, 0, sizeof (struct field_info));
11357 
11358       child_die = die->child;
11359 
11360       while (child_die && child_die->tag)
11361 	{
11362 	  if (child_die->tag == DW_TAG_member
11363 	      || child_die->tag == DW_TAG_variable)
11364 	    {
11365 	      /* NOTE: carlton/2002-11-05: A C++ static data member
11366 		 should be a DW_TAG_member that is a declaration, but
11367 		 all versions of G++ as of this writing (so through at
11368 		 least 3.2.1) incorrectly generate DW_TAG_variable
11369 		 tags for them instead.  */
11370 	      dwarf2_add_field (&fi, child_die, cu);
11371 	    }
11372 	  else if (child_die->tag == DW_TAG_subprogram)
11373 	    {
11374 	      /* C++ member function.  */
11375 	      dwarf2_add_member_fn (&fi, child_die, type, cu);
11376 	    }
11377 	  else if (child_die->tag == DW_TAG_inheritance)
11378 	    {
11379 	      /* C++ base class field.  */
11380 	      dwarf2_add_field (&fi, child_die, cu);
11381 	    }
11382 	  else if (child_die->tag == DW_TAG_typedef)
11383 	    dwarf2_add_typedef (&fi, child_die, cu);
11384 	  else if (child_die->tag == DW_TAG_template_type_param
11385 		   || child_die->tag == DW_TAG_template_value_param)
11386 	    {
11387 	      struct symbol *arg = new_symbol (child_die, NULL, cu);
11388 
11389 	      if (arg != NULL)
11390 		VEC_safe_push (symbolp, template_args, arg);
11391 	    }
11392 
11393 	  child_die = sibling_die (child_die);
11394 	}
11395 
11396       /* Attach template arguments to type.  */
11397       if (! VEC_empty (symbolp, template_args))
11398 	{
11399 	  ALLOCATE_CPLUS_STRUCT_TYPE (type);
11400 	  TYPE_N_TEMPLATE_ARGUMENTS (type)
11401 	    = VEC_length (symbolp, template_args);
11402 	  TYPE_TEMPLATE_ARGUMENTS (type)
11403 	    = obstack_alloc (&objfile->objfile_obstack,
11404 			     (TYPE_N_TEMPLATE_ARGUMENTS (type)
11405 			      * sizeof (struct symbol *)));
11406 	  memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11407 		  VEC_address (symbolp, template_args),
11408 		  (TYPE_N_TEMPLATE_ARGUMENTS (type)
11409 		   * sizeof (struct symbol *)));
11410 	  VEC_free (symbolp, template_args);
11411 	}
11412 
11413       /* Attach fields and member functions to the type.  */
11414       if (fi.nfields)
11415 	dwarf2_attach_fields_to_type (&fi, type, cu);
11416       if (fi.nfnfields)
11417 	{
11418 	  dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11419 
11420 	  /* Get the type which refers to the base class (possibly this
11421 	     class itself) which contains the vtable pointer for the current
11422 	     class from the DW_AT_containing_type attribute.  This use of
11423 	     DW_AT_containing_type is a GNU extension.  */
11424 
11425 	  if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11426 	    {
11427 	      struct type *t = die_containing_type (die, cu);
11428 
11429 	      TYPE_VPTR_BASETYPE (type) = t;
11430 	      if (type == t)
11431 		{
11432 		  int i;
11433 
11434 		  /* Our own class provides vtbl ptr.  */
11435 		  for (i = TYPE_NFIELDS (t) - 1;
11436 		       i >= TYPE_N_BASECLASSES (t);
11437 		       --i)
11438 		    {
11439 		      const char *fieldname = TYPE_FIELD_NAME (t, i);
11440 
11441                       if (is_vtable_name (fieldname, cu))
11442 			{
11443 			  TYPE_VPTR_FIELDNO (type) = i;
11444 			  break;
11445 			}
11446 		    }
11447 
11448 		  /* Complain if virtual function table field not found.  */
11449 		  if (i < TYPE_N_BASECLASSES (t))
11450 		    complaint (&symfile_complaints,
11451 			       _("virtual function table pointer "
11452 				 "not found when defining class '%s'"),
11453 			       TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11454 			       "");
11455 		}
11456 	      else
11457 		{
11458 		  TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11459 		}
11460 	    }
11461 	  else if (cu->producer
11462 		   && strncmp (cu->producer,
11463 			       "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11464 	    {
11465 	      /* The IBM XLC compiler does not provide direct indication
11466 	         of the containing type, but the vtable pointer is
11467 	         always named __vfp.  */
11468 
11469 	      int i;
11470 
11471 	      for (i = TYPE_NFIELDS (type) - 1;
11472 		   i >= TYPE_N_BASECLASSES (type);
11473 		   --i)
11474 		{
11475 		  if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11476 		    {
11477 		      TYPE_VPTR_FIELDNO (type) = i;
11478 		      TYPE_VPTR_BASETYPE (type) = type;
11479 		      break;
11480 		    }
11481 		}
11482 	    }
11483 	}
11484 
11485       /* Copy fi.typedef_field_list linked list elements content into the
11486 	 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
11487       if (fi.typedef_field_list)
11488 	{
11489 	  int i = fi.typedef_field_list_count;
11490 
11491 	  ALLOCATE_CPLUS_STRUCT_TYPE (type);
11492 	  TYPE_TYPEDEF_FIELD_ARRAY (type)
11493 	    = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11494 	  TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11495 
11496 	  /* Reverse the list order to keep the debug info elements order.  */
11497 	  while (--i >= 0)
11498 	    {
11499 	      struct typedef_field *dest, *src;
11500 
11501 	      dest = &TYPE_TYPEDEF_FIELD (type, i);
11502 	      src = &fi.typedef_field_list->field;
11503 	      fi.typedef_field_list = fi.typedef_field_list->next;
11504 	      *dest = *src;
11505 	    }
11506 	}
11507 
11508       do_cleanups (back_to);
11509 
11510       if (HAVE_CPLUS_STRUCT (type))
11511 	TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11512     }
11513 
11514   quirk_gcc_member_function_pointer (type, objfile);
11515 
11516   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11517      snapshots) has been known to create a die giving a declaration
11518      for a class that has, as a child, a die giving a definition for a
11519      nested class.  So we have to process our children even if the
11520      current die is a declaration.  Normally, of course, a declaration
11521      won't have any children at all.  */
11522 
11523   while (child_die != NULL && child_die->tag)
11524     {
11525       if (child_die->tag == DW_TAG_member
11526 	  || child_die->tag == DW_TAG_variable
11527 	  || child_die->tag == DW_TAG_inheritance
11528 	  || child_die->tag == DW_TAG_template_value_param
11529 	  || child_die->tag == DW_TAG_template_type_param)
11530 	{
11531 	  /* Do nothing.  */
11532 	}
11533       else
11534 	process_die (child_die, cu);
11535 
11536       child_die = sibling_die (child_die);
11537     }
11538 
11539   /* Do not consider external references.  According to the DWARF standard,
11540      these DIEs are identified by the fact that they have no byte_size
11541      attribute, and a declaration attribute.  */
11542   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11543       || !die_is_declaration (die, cu))
11544     new_symbol (die, type, cu);
11545 }
11546 
11547 /* Given a DW_AT_enumeration_type die, set its type.  We do not
11548    complete the type's fields yet, or create any symbols.  */
11549 
11550 static struct type *
11551 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11552 {
11553   struct objfile *objfile = cu->objfile;
11554   struct type *type;
11555   struct attribute *attr;
11556   const char *name;
11557 
11558   /* If the definition of this type lives in .debug_types, read that type.
11559      Don't follow DW_AT_specification though, that will take us back up
11560      the chain and we want to go down.  */
11561   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11562   if (attr)
11563     {
11564       struct dwarf2_cu *type_cu = cu;
11565       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11566 
11567       type = read_type_die (type_die, type_cu);
11568 
11569       /* TYPE_CU may not be the same as CU.
11570 	 Ensure TYPE is recorded in CU's type_hash table.  */
11571       return set_die_type (die, type, cu);
11572     }
11573 
11574   type = alloc_type (objfile);
11575 
11576   TYPE_CODE (type) = TYPE_CODE_ENUM;
11577   name = dwarf2_full_name (NULL, die, cu);
11578   if (name != NULL)
11579     TYPE_TAG_NAME (type) = name;
11580 
11581   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11582   if (attr)
11583     {
11584       TYPE_LENGTH (type) = DW_UNSND (attr);
11585     }
11586   else
11587     {
11588       TYPE_LENGTH (type) = 0;
11589     }
11590 
11591   /* The enumeration DIE can be incomplete.  In Ada, any type can be
11592      declared as private in the package spec, and then defined only
11593      inside the package body.  Such types are known as Taft Amendment
11594      Types.  When another package uses such a type, an incomplete DIE
11595      may be generated by the compiler.  */
11596   if (die_is_declaration (die, cu))
11597     TYPE_STUB (type) = 1;
11598 
11599   return set_die_type (die, type, cu);
11600 }
11601 
11602 /* Given a pointer to a die which begins an enumeration, process all
11603    the dies that define the members of the enumeration, and create the
11604    symbol for the enumeration type.
11605 
11606    NOTE: We reverse the order of the element list.  */
11607 
11608 static void
11609 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11610 {
11611   struct type *this_type;
11612 
11613   this_type = get_die_type (die, cu);
11614   if (this_type == NULL)
11615     this_type = read_enumeration_type (die, cu);
11616 
11617   if (die->child != NULL)
11618     {
11619       struct die_info *child_die;
11620       struct symbol *sym;
11621       struct field *fields = NULL;
11622       int num_fields = 0;
11623       int unsigned_enum = 1;
11624       const char *name;
11625       int flag_enum = 1;
11626       ULONGEST mask = 0;
11627 
11628       child_die = die->child;
11629       while (child_die && child_die->tag)
11630 	{
11631 	  if (child_die->tag != DW_TAG_enumerator)
11632 	    {
11633 	      process_die (child_die, cu);
11634 	    }
11635 	  else
11636 	    {
11637 	      name = dwarf2_name (child_die, cu);
11638 	      if (name)
11639 		{
11640 		  sym = new_symbol (child_die, this_type, cu);
11641 		  if (SYMBOL_VALUE (sym) < 0)
11642 		    {
11643 		      unsigned_enum = 0;
11644 		      flag_enum = 0;
11645 		    }
11646 		  else if ((mask & SYMBOL_VALUE (sym)) != 0)
11647 		    flag_enum = 0;
11648 		  else
11649 		    mask |= SYMBOL_VALUE (sym);
11650 
11651 		  if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11652 		    {
11653 		      fields = (struct field *)
11654 			xrealloc (fields,
11655 				  (num_fields + DW_FIELD_ALLOC_CHUNK)
11656 				  * sizeof (struct field));
11657 		    }
11658 
11659 		  FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11660 		  FIELD_TYPE (fields[num_fields]) = NULL;
11661 		  SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11662 		  FIELD_BITSIZE (fields[num_fields]) = 0;
11663 
11664 		  num_fields++;
11665 		}
11666 	    }
11667 
11668 	  child_die = sibling_die (child_die);
11669 	}
11670 
11671       if (num_fields)
11672 	{
11673 	  TYPE_NFIELDS (this_type) = num_fields;
11674 	  TYPE_FIELDS (this_type) = (struct field *)
11675 	    TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11676 	  memcpy (TYPE_FIELDS (this_type), fields,
11677 		  sizeof (struct field) * num_fields);
11678 	  xfree (fields);
11679 	}
11680       if (unsigned_enum)
11681 	TYPE_UNSIGNED (this_type) = 1;
11682       if (flag_enum)
11683 	TYPE_FLAG_ENUM (this_type) = 1;
11684     }
11685 
11686   /* If we are reading an enum from a .debug_types unit, and the enum
11687      is a declaration, and the enum is not the signatured type in the
11688      unit, then we do not want to add a symbol for it.  Adding a
11689      symbol would in some cases obscure the true definition of the
11690      enum, giving users an incomplete type when the definition is
11691      actually available.  Note that we do not want to do this for all
11692      enums which are just declarations, because C++0x allows forward
11693      enum declarations.  */
11694   if (cu->per_cu->is_debug_types
11695       && die_is_declaration (die, cu))
11696     {
11697       struct signatured_type *sig_type;
11698 
11699       sig_type
11700 	= lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11701 					    cu->per_cu->info_or_types_section,
11702 					    cu->per_cu->offset);
11703       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11704       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11705 	return;
11706     }
11707 
11708   new_symbol (die, this_type, cu);
11709 }
11710 
11711 /* Extract all information from a DW_TAG_array_type DIE and put it in
11712    the DIE's type field.  For now, this only handles one dimensional
11713    arrays.  */
11714 
11715 static struct type *
11716 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11717 {
11718   struct objfile *objfile = cu->objfile;
11719   struct die_info *child_die;
11720   struct type *type;
11721   struct type *element_type, *range_type, *index_type;
11722   struct type **range_types = NULL;
11723   struct attribute *attr;
11724   int ndim = 0;
11725   struct cleanup *back_to;
11726   const char *name;
11727 
11728   element_type = die_type (die, cu);
11729 
11730   /* The die_type call above may have already set the type for this DIE.  */
11731   type = get_die_type (die, cu);
11732   if (type)
11733     return type;
11734 
11735   /* Irix 6.2 native cc creates array types without children for
11736      arrays with unspecified length.  */
11737   if (die->child == NULL)
11738     {
11739       index_type = objfile_type (objfile)->builtin_int;
11740       range_type = create_range_type (NULL, index_type, 0, -1);
11741       type = create_array_type (NULL, element_type, range_type);
11742       return set_die_type (die, type, cu);
11743     }
11744 
11745   back_to = make_cleanup (null_cleanup, NULL);
11746   child_die = die->child;
11747   while (child_die && child_die->tag)
11748     {
11749       if (child_die->tag == DW_TAG_subrange_type)
11750 	{
11751 	  struct type *child_type = read_type_die (child_die, cu);
11752 
11753           if (child_type != NULL)
11754             {
11755 	      /* The range type was succesfully read.  Save it for the
11756                  array type creation.  */
11757               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11758                 {
11759                   range_types = (struct type **)
11760                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11761                               * sizeof (struct type *));
11762                   if (ndim == 0)
11763                     make_cleanup (free_current_contents, &range_types);
11764 	        }
11765 	      range_types[ndim++] = child_type;
11766             }
11767 	}
11768       child_die = sibling_die (child_die);
11769     }
11770 
11771   /* Dwarf2 dimensions are output from left to right, create the
11772      necessary array types in backwards order.  */
11773 
11774   type = element_type;
11775 
11776   if (read_array_order (die, cu) == DW_ORD_col_major)
11777     {
11778       int i = 0;
11779 
11780       while (i < ndim)
11781 	type = create_array_type (NULL, type, range_types[i++]);
11782     }
11783   else
11784     {
11785       while (ndim-- > 0)
11786 	type = create_array_type (NULL, type, range_types[ndim]);
11787     }
11788 
11789   /* Understand Dwarf2 support for vector types (like they occur on
11790      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
11791      array type.  This is not part of the Dwarf2/3 standard yet, but a
11792      custom vendor extension.  The main difference between a regular
11793      array and the vector variant is that vectors are passed by value
11794      to functions.  */
11795   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11796   if (attr)
11797     make_vector_type (type);
11798 
11799   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
11800      implementation may choose to implement triple vectors using this
11801      attribute.  */
11802   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11803   if (attr)
11804     {
11805       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11806 	TYPE_LENGTH (type) = DW_UNSND (attr);
11807       else
11808 	complaint (&symfile_complaints,
11809 		   _("DW_AT_byte_size for array type smaller "
11810 		     "than the total size of elements"));
11811     }
11812 
11813   name = dwarf2_name (die, cu);
11814   if (name)
11815     TYPE_NAME (type) = name;
11816 
11817   /* Install the type in the die.  */
11818   set_die_type (die, type, cu);
11819 
11820   /* set_die_type should be already done.  */
11821   set_descriptive_type (type, die, cu);
11822 
11823   do_cleanups (back_to);
11824 
11825   return type;
11826 }
11827 
11828 static enum dwarf_array_dim_ordering
11829 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11830 {
11831   struct attribute *attr;
11832 
11833   attr = dwarf2_attr (die, DW_AT_ordering, cu);
11834 
11835   if (attr) return DW_SND (attr);
11836 
11837   /* GNU F77 is a special case, as at 08/2004 array type info is the
11838      opposite order to the dwarf2 specification, but data is still
11839      laid out as per normal fortran.
11840 
11841      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11842      version checking.  */
11843 
11844   if (cu->language == language_fortran
11845       && cu->producer && strstr (cu->producer, "GNU F77"))
11846     {
11847       return DW_ORD_row_major;
11848     }
11849 
11850   switch (cu->language_defn->la_array_ordering)
11851     {
11852     case array_column_major:
11853       return DW_ORD_col_major;
11854     case array_row_major:
11855     default:
11856       return DW_ORD_row_major;
11857     };
11858 }
11859 
11860 /* Extract all information from a DW_TAG_set_type DIE and put it in
11861    the DIE's type field.  */
11862 
11863 static struct type *
11864 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11865 {
11866   struct type *domain_type, *set_type;
11867   struct attribute *attr;
11868 
11869   domain_type = die_type (die, cu);
11870 
11871   /* The die_type call above may have already set the type for this DIE.  */
11872   set_type = get_die_type (die, cu);
11873   if (set_type)
11874     return set_type;
11875 
11876   set_type = create_set_type (NULL, domain_type);
11877 
11878   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11879   if (attr)
11880     TYPE_LENGTH (set_type) = DW_UNSND (attr);
11881 
11882   return set_die_type (die, set_type, cu);
11883 }
11884 
11885 /* A helper for read_common_block that creates a locexpr baton.
11886    SYM is the symbol which we are marking as computed.
11887    COMMON_DIE is the DIE for the common block.
11888    COMMON_LOC is the location expression attribute for the common
11889    block itself.
11890    MEMBER_LOC is the location expression attribute for the particular
11891    member of the common block that we are processing.
11892    CU is the CU from which the above come.  */
11893 
11894 static void
11895 mark_common_block_symbol_computed (struct symbol *sym,
11896 				   struct die_info *common_die,
11897 				   struct attribute *common_loc,
11898 				   struct attribute *member_loc,
11899 				   struct dwarf2_cu *cu)
11900 {
11901   struct objfile *objfile = dwarf2_per_objfile->objfile;
11902   struct dwarf2_locexpr_baton *baton;
11903   gdb_byte *ptr;
11904   unsigned int cu_off;
11905   enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11906   LONGEST offset = 0;
11907 
11908   gdb_assert (common_loc && member_loc);
11909   gdb_assert (attr_form_is_block (common_loc));
11910   gdb_assert (attr_form_is_block (member_loc)
11911 	      || attr_form_is_constant (member_loc));
11912 
11913   baton = obstack_alloc (&objfile->objfile_obstack,
11914 			 sizeof (struct dwarf2_locexpr_baton));
11915   baton->per_cu = cu->per_cu;
11916   gdb_assert (baton->per_cu);
11917 
11918   baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11919 
11920   if (attr_form_is_constant (member_loc))
11921     {
11922       offset = dwarf2_get_attr_constant_value (member_loc, 0);
11923       baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11924     }
11925   else
11926     baton->size += DW_BLOCK (member_loc)->size;
11927 
11928   ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11929   baton->data = ptr;
11930 
11931   *ptr++ = DW_OP_call4;
11932   cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11933   store_unsigned_integer (ptr, 4, byte_order, cu_off);
11934   ptr += 4;
11935 
11936   if (attr_form_is_constant (member_loc))
11937     {
11938       *ptr++ = DW_OP_addr;
11939       store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11940       ptr += cu->header.addr_size;
11941     }
11942   else
11943     {
11944       /* We have to copy the data here, because DW_OP_call4 will only
11945 	 use a DW_AT_location attribute.  */
11946       memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11947       ptr += DW_BLOCK (member_loc)->size;
11948     }
11949 
11950   *ptr++ = DW_OP_plus;
11951   gdb_assert (ptr - baton->data == baton->size);
11952 
11953   SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11954   SYMBOL_LOCATION_BATON (sym) = baton;
11955   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11956 }
11957 
11958 /* Create appropriate locally-scoped variables for all the
11959    DW_TAG_common_block entries.  Also create a struct common_block
11960    listing all such variables for `info common'.  COMMON_BLOCK_DOMAIN
11961    is used to sepate the common blocks name namespace from regular
11962    variable names.  */
11963 
11964 static void
11965 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11966 {
11967   struct attribute *attr;
11968 
11969   attr = dwarf2_attr (die, DW_AT_location, cu);
11970   if (attr)
11971     {
11972       /* Support the .debug_loc offsets.  */
11973       if (attr_form_is_block (attr))
11974         {
11975 	  /* Ok.  */
11976         }
11977       else if (attr_form_is_section_offset (attr))
11978         {
11979 	  dwarf2_complex_location_expr_complaint ();
11980 	  attr = NULL;
11981         }
11982       else
11983         {
11984 	  dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11985 						 "common block member");
11986 	  attr = NULL;
11987         }
11988     }
11989 
11990   if (die->child != NULL)
11991     {
11992       struct objfile *objfile = cu->objfile;
11993       struct die_info *child_die;
11994       size_t n_entries = 0, size;
11995       struct common_block *common_block;
11996       struct symbol *sym;
11997 
11998       for (child_die = die->child;
11999 	   child_die && child_die->tag;
12000 	   child_die = sibling_die (child_die))
12001 	++n_entries;
12002 
12003       size = (sizeof (struct common_block)
12004 	      + (n_entries - 1) * sizeof (struct symbol *));
12005       common_block = obstack_alloc (&objfile->objfile_obstack, size);
12006       memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12007       common_block->n_entries = 0;
12008 
12009       for (child_die = die->child;
12010 	   child_die && child_die->tag;
12011 	   child_die = sibling_die (child_die))
12012 	{
12013 	  /* Create the symbol in the DW_TAG_common_block block in the current
12014 	     symbol scope.  */
12015 	  sym = new_symbol (child_die, NULL, cu);
12016 	  if (sym != NULL)
12017 	    {
12018 	      struct attribute *member_loc;
12019 
12020 	      common_block->contents[common_block->n_entries++] = sym;
12021 
12022 	      member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12023 					cu);
12024 	      if (member_loc)
12025 		{
12026 		  /* GDB has handled this for a long time, but it is
12027 		     not specified by DWARF.  It seems to have been
12028 		     emitted by gfortran at least as recently as:
12029 		     http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057.  */
12030 		  complaint (&symfile_complaints,
12031 			     _("Variable in common block has "
12032 			       "DW_AT_data_member_location "
12033 			       "- DIE at 0x%x [in module %s]"),
12034 			     child_die->offset.sect_off, cu->objfile->name);
12035 
12036 		  if (attr_form_is_section_offset (member_loc))
12037 		    dwarf2_complex_location_expr_complaint ();
12038 		  else if (attr_form_is_constant (member_loc)
12039 			   || attr_form_is_block (member_loc))
12040 		    {
12041 		      if (attr)
12042 			mark_common_block_symbol_computed (sym, die, attr,
12043 							   member_loc, cu);
12044 		    }
12045 		  else
12046 		    dwarf2_complex_location_expr_complaint ();
12047 		}
12048 	    }
12049 	}
12050 
12051       sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12052       SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12053     }
12054 }
12055 
12056 /* Create a type for a C++ namespace.  */
12057 
12058 static struct type *
12059 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12060 {
12061   struct objfile *objfile = cu->objfile;
12062   const char *previous_prefix, *name;
12063   int is_anonymous;
12064   struct type *type;
12065 
12066   /* For extensions, reuse the type of the original namespace.  */
12067   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12068     {
12069       struct die_info *ext_die;
12070       struct dwarf2_cu *ext_cu = cu;
12071 
12072       ext_die = dwarf2_extension (die, &ext_cu);
12073       type = read_type_die (ext_die, ext_cu);
12074 
12075       /* EXT_CU may not be the same as CU.
12076 	 Ensure TYPE is recorded in CU's type_hash table.  */
12077       return set_die_type (die, type, cu);
12078     }
12079 
12080   name = namespace_name (die, &is_anonymous, cu);
12081 
12082   /* Now build the name of the current namespace.  */
12083 
12084   previous_prefix = determine_prefix (die, cu);
12085   if (previous_prefix[0] != '\0')
12086     name = typename_concat (&objfile->objfile_obstack,
12087 			    previous_prefix, name, 0, cu);
12088 
12089   /* Create the type.  */
12090   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12091 		    objfile);
12092   TYPE_NAME (type) = name;
12093   TYPE_TAG_NAME (type) = TYPE_NAME (type);
12094 
12095   return set_die_type (die, type, cu);
12096 }
12097 
12098 /* Read a C++ namespace.  */
12099 
12100 static void
12101 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12102 {
12103   struct objfile *objfile = cu->objfile;
12104   int is_anonymous;
12105 
12106   /* Add a symbol associated to this if we haven't seen the namespace
12107      before.  Also, add a using directive if it's an anonymous
12108      namespace.  */
12109 
12110   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12111     {
12112       struct type *type;
12113 
12114       type = read_type_die (die, cu);
12115       new_symbol (die, type, cu);
12116 
12117       namespace_name (die, &is_anonymous, cu);
12118       if (is_anonymous)
12119 	{
12120 	  const char *previous_prefix = determine_prefix (die, cu);
12121 
12122 	  cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12123 				  NULL, NULL, 0, &objfile->objfile_obstack);
12124 	}
12125     }
12126 
12127   if (die->child != NULL)
12128     {
12129       struct die_info *child_die = die->child;
12130 
12131       while (child_die && child_die->tag)
12132 	{
12133 	  process_die (child_die, cu);
12134 	  child_die = sibling_die (child_die);
12135 	}
12136     }
12137 }
12138 
12139 /* Read a Fortran module as type.  This DIE can be only a declaration used for
12140    imported module.  Still we need that type as local Fortran "use ... only"
12141    declaration imports depend on the created type in determine_prefix.  */
12142 
12143 static struct type *
12144 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12145 {
12146   struct objfile *objfile = cu->objfile;
12147   const char *module_name;
12148   struct type *type;
12149 
12150   module_name = dwarf2_name (die, cu);
12151   if (!module_name)
12152     complaint (&symfile_complaints,
12153 	       _("DW_TAG_module has no name, offset 0x%x"),
12154                die->offset.sect_off);
12155   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12156 
12157   /* determine_prefix uses TYPE_TAG_NAME.  */
12158   TYPE_TAG_NAME (type) = TYPE_NAME (type);
12159 
12160   return set_die_type (die, type, cu);
12161 }
12162 
12163 /* Read a Fortran module.  */
12164 
12165 static void
12166 read_module (struct die_info *die, struct dwarf2_cu *cu)
12167 {
12168   struct die_info *child_die = die->child;
12169 
12170   while (child_die && child_die->tag)
12171     {
12172       process_die (child_die, cu);
12173       child_die = sibling_die (child_die);
12174     }
12175 }
12176 
12177 /* Return the name of the namespace represented by DIE.  Set
12178    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12179    namespace.  */
12180 
12181 static const char *
12182 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12183 {
12184   struct die_info *current_die;
12185   const char *name = NULL;
12186 
12187   /* Loop through the extensions until we find a name.  */
12188 
12189   for (current_die = die;
12190        current_die != NULL;
12191        current_die = dwarf2_extension (die, &cu))
12192     {
12193       name = dwarf2_name (current_die, cu);
12194       if (name != NULL)
12195 	break;
12196     }
12197 
12198   /* Is it an anonymous namespace?  */
12199 
12200   *is_anonymous = (name == NULL);
12201   if (*is_anonymous)
12202     name = CP_ANONYMOUS_NAMESPACE_STR;
12203 
12204   return name;
12205 }
12206 
12207 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12208    the user defined type vector.  */
12209 
12210 static struct type *
12211 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12212 {
12213   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12214   struct comp_unit_head *cu_header = &cu->header;
12215   struct type *type;
12216   struct attribute *attr_byte_size;
12217   struct attribute *attr_address_class;
12218   int byte_size, addr_class;
12219   struct type *target_type;
12220 
12221   target_type = die_type (die, cu);
12222 
12223   /* The die_type call above may have already set the type for this DIE.  */
12224   type = get_die_type (die, cu);
12225   if (type)
12226     return type;
12227 
12228   type = lookup_pointer_type (target_type);
12229 
12230   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12231   if (attr_byte_size)
12232     byte_size = DW_UNSND (attr_byte_size);
12233   else
12234     byte_size = cu_header->addr_size;
12235 
12236   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12237   if (attr_address_class)
12238     addr_class = DW_UNSND (attr_address_class);
12239   else
12240     addr_class = DW_ADDR_none;
12241 
12242   /* If the pointer size or address class is different than the
12243      default, create a type variant marked as such and set the
12244      length accordingly.  */
12245   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12246     {
12247       if (gdbarch_address_class_type_flags_p (gdbarch))
12248 	{
12249 	  int type_flags;
12250 
12251 	  type_flags = gdbarch_address_class_type_flags
12252 			 (gdbarch, byte_size, addr_class);
12253 	  gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12254 		      == 0);
12255 	  type = make_type_with_address_space (type, type_flags);
12256 	}
12257       else if (TYPE_LENGTH (type) != byte_size)
12258 	{
12259 	  complaint (&symfile_complaints,
12260 		     _("invalid pointer size %d"), byte_size);
12261 	}
12262       else
12263 	{
12264 	  /* Should we also complain about unhandled address classes?  */
12265 	}
12266     }
12267 
12268   TYPE_LENGTH (type) = byte_size;
12269   return set_die_type (die, type, cu);
12270 }
12271 
12272 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12273    the user defined type vector.  */
12274 
12275 static struct type *
12276 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12277 {
12278   struct type *type;
12279   struct type *to_type;
12280   struct type *domain;
12281 
12282   to_type = die_type (die, cu);
12283   domain = die_containing_type (die, cu);
12284 
12285   /* The calls above may have already set the type for this DIE.  */
12286   type = get_die_type (die, cu);
12287   if (type)
12288     return type;
12289 
12290   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12291     type = lookup_methodptr_type (to_type);
12292   else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12293     {
12294       struct type *new_type = alloc_type (cu->objfile);
12295 
12296       smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12297 			    TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12298 			    TYPE_VARARGS (to_type));
12299       type = lookup_methodptr_type (new_type);
12300     }
12301   else
12302     type = lookup_memberptr_type (to_type, domain);
12303 
12304   return set_die_type (die, type, cu);
12305 }
12306 
12307 /* Extract all information from a DW_TAG_reference_type DIE and add to
12308    the user defined type vector.  */
12309 
12310 static struct type *
12311 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12312 {
12313   struct comp_unit_head *cu_header = &cu->header;
12314   struct type *type, *target_type;
12315   struct attribute *attr;
12316 
12317   target_type = die_type (die, cu);
12318 
12319   /* The die_type call above may have already set the type for this DIE.  */
12320   type = get_die_type (die, cu);
12321   if (type)
12322     return type;
12323 
12324   type = lookup_reference_type (target_type);
12325   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12326   if (attr)
12327     {
12328       TYPE_LENGTH (type) = DW_UNSND (attr);
12329     }
12330   else
12331     {
12332       TYPE_LENGTH (type) = cu_header->addr_size;
12333     }
12334   return set_die_type (die, type, cu);
12335 }
12336 
12337 static struct type *
12338 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12339 {
12340   struct type *base_type, *cv_type;
12341 
12342   base_type = die_type (die, cu);
12343 
12344   /* The die_type call above may have already set the type for this DIE.  */
12345   cv_type = get_die_type (die, cu);
12346   if (cv_type)
12347     return cv_type;
12348 
12349   /* In case the const qualifier is applied to an array type, the element type
12350      is so qualified, not the array type (section 6.7.3 of C99).  */
12351   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12352     {
12353       struct type *el_type, *inner_array;
12354 
12355       base_type = copy_type (base_type);
12356       inner_array = base_type;
12357 
12358       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12359 	{
12360 	  TYPE_TARGET_TYPE (inner_array) =
12361 	    copy_type (TYPE_TARGET_TYPE (inner_array));
12362 	  inner_array = TYPE_TARGET_TYPE (inner_array);
12363 	}
12364 
12365       el_type = TYPE_TARGET_TYPE (inner_array);
12366       TYPE_TARGET_TYPE (inner_array) =
12367 	make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12368 
12369       return set_die_type (die, base_type, cu);
12370     }
12371 
12372   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12373   return set_die_type (die, cv_type, cu);
12374 }
12375 
12376 static struct type *
12377 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12378 {
12379   struct type *base_type, *cv_type;
12380 
12381   base_type = die_type (die, cu);
12382 
12383   /* The die_type call above may have already set the type for this DIE.  */
12384   cv_type = get_die_type (die, cu);
12385   if (cv_type)
12386     return cv_type;
12387 
12388   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12389   return set_die_type (die, cv_type, cu);
12390 }
12391 
12392 /* Handle DW_TAG_restrict_type.  */
12393 
12394 static struct type *
12395 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12396 {
12397   struct type *base_type, *cv_type;
12398 
12399   base_type = die_type (die, cu);
12400 
12401   /* The die_type call above may have already set the type for this DIE.  */
12402   cv_type = get_die_type (die, cu);
12403   if (cv_type)
12404     return cv_type;
12405 
12406   cv_type = make_restrict_type (base_type);
12407   return set_die_type (die, cv_type, cu);
12408 }
12409 
12410 /* Extract all information from a DW_TAG_string_type DIE and add to
12411    the user defined type vector.  It isn't really a user defined type,
12412    but it behaves like one, with other DIE's using an AT_user_def_type
12413    attribute to reference it.  */
12414 
12415 static struct type *
12416 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12417 {
12418   struct objfile *objfile = cu->objfile;
12419   struct gdbarch *gdbarch = get_objfile_arch (objfile);
12420   struct type *type, *range_type, *index_type, *char_type;
12421   struct attribute *attr;
12422   unsigned int length;
12423 
12424   attr = dwarf2_attr (die, DW_AT_string_length, cu);
12425   if (attr)
12426     {
12427       length = DW_UNSND (attr);
12428     }
12429   else
12430     {
12431       /* Check for the DW_AT_byte_size attribute.  */
12432       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12433       if (attr)
12434         {
12435           length = DW_UNSND (attr);
12436         }
12437       else
12438         {
12439           length = 1;
12440         }
12441     }
12442 
12443   index_type = objfile_type (objfile)->builtin_int;
12444   range_type = create_range_type (NULL, index_type, 1, length);
12445   char_type = language_string_char_type (cu->language_defn, gdbarch);
12446   type = create_string_type (NULL, char_type, range_type);
12447 
12448   return set_die_type (die, type, cu);
12449 }
12450 
12451 /* Handle DIES due to C code like:
12452 
12453    struct foo
12454    {
12455    int (*funcp)(int a, long l);
12456    int b;
12457    };
12458 
12459    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
12460 
12461 static struct type *
12462 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12463 {
12464   struct objfile *objfile = cu->objfile;
12465   struct type *type;		/* Type that this function returns.  */
12466   struct type *ftype;		/* Function that returns above type.  */
12467   struct attribute *attr;
12468 
12469   type = die_type (die, cu);
12470 
12471   /* The die_type call above may have already set the type for this DIE.  */
12472   ftype = get_die_type (die, cu);
12473   if (ftype)
12474     return ftype;
12475 
12476   ftype = lookup_function_type (type);
12477 
12478   /* All functions in C++, Pascal and Java have prototypes.  */
12479   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12480   if ((attr && (DW_UNSND (attr) != 0))
12481       || cu->language == language_cplus
12482       || cu->language == language_java
12483       || cu->language == language_pascal)
12484     TYPE_PROTOTYPED (ftype) = 1;
12485   else if (producer_is_realview (cu->producer))
12486     /* RealView does not emit DW_AT_prototyped.  We can not
12487        distinguish prototyped and unprototyped functions; default to
12488        prototyped, since that is more common in modern code (and
12489        RealView warns about unprototyped functions).  */
12490     TYPE_PROTOTYPED (ftype) = 1;
12491 
12492   /* Store the calling convention in the type if it's available in
12493      the subroutine die.  Otherwise set the calling convention to
12494      the default value DW_CC_normal.  */
12495   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12496   if (attr)
12497     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12498   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12499     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12500   else
12501     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12502 
12503   /* We need to add the subroutine type to the die immediately so
12504      we don't infinitely recurse when dealing with parameters
12505      declared as the same subroutine type.  */
12506   set_die_type (die, ftype, cu);
12507 
12508   if (die->child != NULL)
12509     {
12510       struct type *void_type = objfile_type (objfile)->builtin_void;
12511       struct die_info *child_die;
12512       int nparams, iparams;
12513 
12514       /* Count the number of parameters.
12515          FIXME: GDB currently ignores vararg functions, but knows about
12516          vararg member functions.  */
12517       nparams = 0;
12518       child_die = die->child;
12519       while (child_die && child_die->tag)
12520 	{
12521 	  if (child_die->tag == DW_TAG_formal_parameter)
12522 	    nparams++;
12523 	  else if (child_die->tag == DW_TAG_unspecified_parameters)
12524 	    TYPE_VARARGS (ftype) = 1;
12525 	  child_die = sibling_die (child_die);
12526 	}
12527 
12528       /* Allocate storage for parameters and fill them in.  */
12529       TYPE_NFIELDS (ftype) = nparams;
12530       TYPE_FIELDS (ftype) = (struct field *)
12531 	TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12532 
12533       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
12534 	 even if we error out during the parameters reading below.  */
12535       for (iparams = 0; iparams < nparams; iparams++)
12536 	TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12537 
12538       iparams = 0;
12539       child_die = die->child;
12540       while (child_die && child_die->tag)
12541 	{
12542 	  if (child_die->tag == DW_TAG_formal_parameter)
12543 	    {
12544 	      struct type *arg_type;
12545 
12546 	      /* DWARF version 2 has no clean way to discern C++
12547 		 static and non-static member functions.  G++ helps
12548 		 GDB by marking the first parameter for non-static
12549 		 member functions (which is the this pointer) as
12550 		 artificial.  We pass this information to
12551 		 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12552 
12553 		 DWARF version 3 added DW_AT_object_pointer, which GCC
12554 		 4.5 does not yet generate.  */
12555 	      attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12556 	      if (attr)
12557 		TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12558 	      else
12559 		{
12560 		  TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12561 
12562 		  /* GCC/43521: In java, the formal parameter
12563 		     "this" is sometimes not marked with DW_AT_artificial.  */
12564 		  if (cu->language == language_java)
12565 		    {
12566 		      const char *name = dwarf2_name (child_die, cu);
12567 
12568 		      if (name && !strcmp (name, "this"))
12569 			TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12570 		    }
12571 		}
12572 	      arg_type = die_type (child_die, cu);
12573 
12574 	      /* RealView does not mark THIS as const, which the testsuite
12575 		 expects.  GCC marks THIS as const in method definitions,
12576 		 but not in the class specifications (GCC PR 43053).  */
12577 	      if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12578 		  && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12579 		{
12580 		  int is_this = 0;
12581 		  struct dwarf2_cu *arg_cu = cu;
12582 		  const char *name = dwarf2_name (child_die, cu);
12583 
12584 		  attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12585 		  if (attr)
12586 		    {
12587 		      /* If the compiler emits this, use it.  */
12588 		      if (follow_die_ref (die, attr, &arg_cu) == child_die)
12589 			is_this = 1;
12590 		    }
12591 		  else if (name && strcmp (name, "this") == 0)
12592 		    /* Function definitions will have the argument names.  */
12593 		    is_this = 1;
12594 		  else if (name == NULL && iparams == 0)
12595 		    /* Declarations may not have the names, so like
12596 		       elsewhere in GDB, assume an artificial first
12597 		       argument is "this".  */
12598 		    is_this = 1;
12599 
12600 		  if (is_this)
12601 		    arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12602 					     arg_type, 0);
12603 		}
12604 
12605 	      TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12606 	      iparams++;
12607 	    }
12608 	  child_die = sibling_die (child_die);
12609 	}
12610     }
12611 
12612   return ftype;
12613 }
12614 
12615 static struct type *
12616 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12617 {
12618   struct objfile *objfile = cu->objfile;
12619   const char *name = NULL;
12620   struct type *this_type, *target_type;
12621 
12622   name = dwarf2_full_name (NULL, die, cu);
12623   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12624 			 TYPE_FLAG_TARGET_STUB, NULL, objfile);
12625   TYPE_NAME (this_type) = name;
12626   set_die_type (die, this_type, cu);
12627   target_type = die_type (die, cu);
12628   if (target_type != this_type)
12629     TYPE_TARGET_TYPE (this_type) = target_type;
12630   else
12631     {
12632       /* Self-referential typedefs are, it seems, not allowed by the DWARF
12633 	 spec and cause infinite loops in GDB.  */
12634       complaint (&symfile_complaints,
12635 		 _("Self-referential DW_TAG_typedef "
12636 		   "- DIE at 0x%x [in module %s]"),
12637 		 die->offset.sect_off, objfile->name);
12638       TYPE_TARGET_TYPE (this_type) = NULL;
12639     }
12640   return this_type;
12641 }
12642 
12643 /* Find a representation of a given base type and install
12644    it in the TYPE field of the die.  */
12645 
12646 static struct type *
12647 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12648 {
12649   struct objfile *objfile = cu->objfile;
12650   struct type *type;
12651   struct attribute *attr;
12652   int encoding = 0, size = 0;
12653   const char *name;
12654   enum type_code code = TYPE_CODE_INT;
12655   int type_flags = 0;
12656   struct type *target_type = NULL;
12657 
12658   attr = dwarf2_attr (die, DW_AT_encoding, cu);
12659   if (attr)
12660     {
12661       encoding = DW_UNSND (attr);
12662     }
12663   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12664   if (attr)
12665     {
12666       size = DW_UNSND (attr);
12667     }
12668   name = dwarf2_name (die, cu);
12669   if (!name)
12670     {
12671       complaint (&symfile_complaints,
12672 		 _("DW_AT_name missing from DW_TAG_base_type"));
12673     }
12674 
12675   switch (encoding)
12676     {
12677       case DW_ATE_address:
12678 	/* Turn DW_ATE_address into a void * pointer.  */
12679 	code = TYPE_CODE_PTR;
12680 	type_flags |= TYPE_FLAG_UNSIGNED;
12681 	target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12682 	break;
12683       case DW_ATE_boolean:
12684 	code = TYPE_CODE_BOOL;
12685 	type_flags |= TYPE_FLAG_UNSIGNED;
12686 	break;
12687       case DW_ATE_complex_float:
12688 	code = TYPE_CODE_COMPLEX;
12689 	target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12690 	break;
12691       case DW_ATE_decimal_float:
12692 	code = TYPE_CODE_DECFLOAT;
12693 	break;
12694       case DW_ATE_float:
12695 	code = TYPE_CODE_FLT;
12696 	break;
12697       case DW_ATE_signed:
12698 	break;
12699       case DW_ATE_unsigned:
12700 	type_flags |= TYPE_FLAG_UNSIGNED;
12701 	if (cu->language == language_fortran
12702 	    && name
12703 	    && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12704 	  code = TYPE_CODE_CHAR;
12705 	break;
12706       case DW_ATE_signed_char:
12707 	if (cu->language == language_ada || cu->language == language_m2
12708 	    || cu->language == language_pascal
12709 	    || cu->language == language_fortran)
12710 	  code = TYPE_CODE_CHAR;
12711 	break;
12712       case DW_ATE_unsigned_char:
12713 	if (cu->language == language_ada || cu->language == language_m2
12714 	    || cu->language == language_pascal
12715 	    || cu->language == language_fortran)
12716 	  code = TYPE_CODE_CHAR;
12717 	type_flags |= TYPE_FLAG_UNSIGNED;
12718 	break;
12719       case DW_ATE_UTF:
12720 	/* We just treat this as an integer and then recognize the
12721 	   type by name elsewhere.  */
12722 	break;
12723 
12724       default:
12725 	complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12726 		   dwarf_type_encoding_name (encoding));
12727 	break;
12728     }
12729 
12730   type = init_type (code, size, type_flags, NULL, objfile);
12731   TYPE_NAME (type) = name;
12732   TYPE_TARGET_TYPE (type) = target_type;
12733 
12734   if (name && strcmp (name, "char") == 0)
12735     TYPE_NOSIGN (type) = 1;
12736 
12737   return set_die_type (die, type, cu);
12738 }
12739 
12740 /* Read the given DW_AT_subrange DIE.  */
12741 
12742 static struct type *
12743 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12744 {
12745   struct type *base_type, *orig_base_type;
12746   struct type *range_type;
12747   struct attribute *attr;
12748   LONGEST low, high;
12749   int low_default_is_valid;
12750   const char *name;
12751   LONGEST negative_mask;
12752 
12753   orig_base_type = die_type (die, cu);
12754   /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12755      whereas the real type might be.  So, we use ORIG_BASE_TYPE when
12756      creating the range type, but we use the result of check_typedef
12757      when examining properties of the type.  */
12758   base_type = check_typedef (orig_base_type);
12759 
12760   /* The die_type call above may have already set the type for this DIE.  */
12761   range_type = get_die_type (die, cu);
12762   if (range_type)
12763     return range_type;
12764 
12765   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12766      omitting DW_AT_lower_bound.  */
12767   switch (cu->language)
12768     {
12769     case language_c:
12770     case language_cplus:
12771       low = 0;
12772       low_default_is_valid = 1;
12773       break;
12774     case language_fortran:
12775       low = 1;
12776       low_default_is_valid = 1;
12777       break;
12778     case language_d:
12779     case language_java:
12780     case language_objc:
12781       low = 0;
12782       low_default_is_valid = (cu->header.version >= 4);
12783       break;
12784     case language_ada:
12785     case language_m2:
12786     case language_pascal:
12787       low = 1;
12788       low_default_is_valid = (cu->header.version >= 4);
12789       break;
12790     default:
12791       low = 0;
12792       low_default_is_valid = 0;
12793       break;
12794     }
12795 
12796   /* FIXME: For variable sized arrays either of these could be
12797      a variable rather than a constant value.  We'll allow it,
12798      but we don't know how to handle it.  */
12799   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12800   if (attr)
12801     low = dwarf2_get_attr_constant_value (attr, low);
12802   else if (!low_default_is_valid)
12803     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12804 				      "- DIE at 0x%x [in module %s]"),
12805 	       die->offset.sect_off, cu->objfile->name);
12806 
12807   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12808   if (attr)
12809     {
12810       if (attr_form_is_block (attr) || is_ref_attr (attr))
12811         {
12812           /* GCC encodes arrays with unspecified or dynamic length
12813              with a DW_FORM_block1 attribute or a reference attribute.
12814              FIXME: GDB does not yet know how to handle dynamic
12815              arrays properly, treat them as arrays with unspecified
12816              length for now.
12817 
12818              FIXME: jimb/2003-09-22: GDB does not really know
12819              how to handle arrays of unspecified length
12820              either; we just represent them as zero-length
12821              arrays.  Choose an appropriate upper bound given
12822              the lower bound we've computed above.  */
12823           high = low - 1;
12824         }
12825       else
12826         high = dwarf2_get_attr_constant_value (attr, 1);
12827     }
12828   else
12829     {
12830       attr = dwarf2_attr (die, DW_AT_count, cu);
12831       if (attr)
12832 	{
12833 	  int count = dwarf2_get_attr_constant_value (attr, 1);
12834 	  high = low + count - 1;
12835 	}
12836       else
12837 	{
12838 	  /* Unspecified array length.  */
12839 	  high = low - 1;
12840 	}
12841     }
12842 
12843   /* Dwarf-2 specifications explicitly allows to create subrange types
12844      without specifying a base type.
12845      In that case, the base type must be set to the type of
12846      the lower bound, upper bound or count, in that order, if any of these
12847      three attributes references an object that has a type.
12848      If no base type is found, the Dwarf-2 specifications say that
12849      a signed integer type of size equal to the size of an address should
12850      be used.
12851      For the following C code: `extern char gdb_int [];'
12852      GCC produces an empty range DIE.
12853      FIXME: muller/2010-05-28: Possible references to object for low bound,
12854      high bound or count are not yet handled by this code.  */
12855   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12856     {
12857       struct objfile *objfile = cu->objfile;
12858       struct gdbarch *gdbarch = get_objfile_arch (objfile);
12859       int addr_size = gdbarch_addr_bit (gdbarch) /8;
12860       struct type *int_type = objfile_type (objfile)->builtin_int;
12861 
12862       /* Test "int", "long int", and "long long int" objfile types,
12863 	 and select the first one having a size above or equal to the
12864 	 architecture address size.  */
12865       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12866 	base_type = int_type;
12867       else
12868 	{
12869 	  int_type = objfile_type (objfile)->builtin_long;
12870 	  if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12871 	    base_type = int_type;
12872 	  else
12873 	    {
12874 	      int_type = objfile_type (objfile)->builtin_long_long;
12875 	      if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12876 		base_type = int_type;
12877 	    }
12878 	}
12879     }
12880 
12881   negative_mask =
12882     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12883   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12884     low |= negative_mask;
12885   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12886     high |= negative_mask;
12887 
12888   range_type = create_range_type (NULL, orig_base_type, low, high);
12889 
12890   /* Mark arrays with dynamic length at least as an array of unspecified
12891      length.  GDB could check the boundary but before it gets implemented at
12892      least allow accessing the array elements.  */
12893   if (attr && attr_form_is_block (attr))
12894     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12895 
12896   /* Ada expects an empty array on no boundary attributes.  */
12897   if (attr == NULL && cu->language != language_ada)
12898     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12899 
12900   name = dwarf2_name (die, cu);
12901   if (name)
12902     TYPE_NAME (range_type) = name;
12903 
12904   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12905   if (attr)
12906     TYPE_LENGTH (range_type) = DW_UNSND (attr);
12907 
12908   set_die_type (die, range_type, cu);
12909 
12910   /* set_die_type should be already done.  */
12911   set_descriptive_type (range_type, die, cu);
12912 
12913   return range_type;
12914 }
12915 
12916 static struct type *
12917 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12918 {
12919   struct type *type;
12920 
12921   /* For now, we only support the C meaning of an unspecified type: void.  */
12922 
12923   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12924   TYPE_NAME (type) = dwarf2_name (die, cu);
12925 
12926   return set_die_type (die, type, cu);
12927 }
12928 
12929 /* Read a single die and all its descendents.  Set the die's sibling
12930    field to NULL; set other fields in the die correctly, and set all
12931    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
12932    location of the info_ptr after reading all of those dies.  PARENT
12933    is the parent of the die in question.  */
12934 
12935 static struct die_info *
12936 read_die_and_children (const struct die_reader_specs *reader,
12937 		       gdb_byte *info_ptr,
12938 		       gdb_byte **new_info_ptr,
12939 		       struct die_info *parent)
12940 {
12941   struct die_info *die;
12942   gdb_byte *cur_ptr;
12943   int has_children;
12944 
12945   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12946   if (die == NULL)
12947     {
12948       *new_info_ptr = cur_ptr;
12949       return NULL;
12950     }
12951   store_in_ref_table (die, reader->cu);
12952 
12953   if (has_children)
12954     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12955   else
12956     {
12957       die->child = NULL;
12958       *new_info_ptr = cur_ptr;
12959     }
12960 
12961   die->sibling = NULL;
12962   die->parent = parent;
12963   return die;
12964 }
12965 
12966 /* Read a die, all of its descendents, and all of its siblings; set
12967    all of the fields of all of the dies correctly.  Arguments are as
12968    in read_die_and_children.  */
12969 
12970 static struct die_info *
12971 read_die_and_siblings (const struct die_reader_specs *reader,
12972 		       gdb_byte *info_ptr,
12973 		       gdb_byte **new_info_ptr,
12974 		       struct die_info *parent)
12975 {
12976   struct die_info *first_die, *last_sibling;
12977   gdb_byte *cur_ptr;
12978 
12979   cur_ptr = info_ptr;
12980   first_die = last_sibling = NULL;
12981 
12982   while (1)
12983     {
12984       struct die_info *die
12985 	= read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12986 
12987       if (die == NULL)
12988 	{
12989 	  *new_info_ptr = cur_ptr;
12990 	  return first_die;
12991 	}
12992 
12993       if (!first_die)
12994 	first_die = die;
12995       else
12996 	last_sibling->sibling = die;
12997 
12998       last_sibling = die;
12999     }
13000 }
13001 
13002 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13003    attributes.
13004    The caller is responsible for filling in the extra attributes
13005    and updating (*DIEP)->num_attrs.
13006    Set DIEP to point to a newly allocated die with its information,
13007    except for its child, sibling, and parent fields.
13008    Set HAS_CHILDREN to tell whether the die has children or not.  */
13009 
13010 static gdb_byte *
13011 read_full_die_1 (const struct die_reader_specs *reader,
13012 		 struct die_info **diep, gdb_byte *info_ptr,
13013 		 int *has_children, int num_extra_attrs)
13014 {
13015   unsigned int abbrev_number, bytes_read, i;
13016   sect_offset offset;
13017   struct abbrev_info *abbrev;
13018   struct die_info *die;
13019   struct dwarf2_cu *cu = reader->cu;
13020   bfd *abfd = reader->abfd;
13021 
13022   offset.sect_off = info_ptr - reader->buffer;
13023   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13024   info_ptr += bytes_read;
13025   if (!abbrev_number)
13026     {
13027       *diep = NULL;
13028       *has_children = 0;
13029       return info_ptr;
13030     }
13031 
13032   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13033   if (!abbrev)
13034     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13035 	   abbrev_number,
13036 	   bfd_get_filename (abfd));
13037 
13038   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13039   die->offset = offset;
13040   die->tag = abbrev->tag;
13041   die->abbrev = abbrev_number;
13042 
13043   /* Make the result usable.
13044      The caller needs to update num_attrs after adding the extra
13045      attributes.  */
13046   die->num_attrs = abbrev->num_attrs;
13047 
13048   for (i = 0; i < abbrev->num_attrs; ++i)
13049     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13050 			       info_ptr);
13051 
13052   *diep = die;
13053   *has_children = abbrev->has_children;
13054   return info_ptr;
13055 }
13056 
13057 /* Read a die and all its attributes.
13058    Set DIEP to point to a newly allocated die with its information,
13059    except for its child, sibling, and parent fields.
13060    Set HAS_CHILDREN to tell whether the die has children or not.  */
13061 
13062 static gdb_byte *
13063 read_full_die (const struct die_reader_specs *reader,
13064 	       struct die_info **diep, gdb_byte *info_ptr,
13065 	       int *has_children)
13066 {
13067   return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13068 }
13069 
13070 /* Abbreviation tables.
13071 
13072    In DWARF version 2, the description of the debugging information is
13073    stored in a separate .debug_abbrev section.  Before we read any
13074    dies from a section we read in all abbreviations and install them
13075    in a hash table.  */
13076 
13077 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
13078 
13079 static struct abbrev_info *
13080 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13081 {
13082   struct abbrev_info *abbrev;
13083 
13084   abbrev = (struct abbrev_info *)
13085     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13086   memset (abbrev, 0, sizeof (struct abbrev_info));
13087   return abbrev;
13088 }
13089 
13090 /* Add an abbreviation to the table.  */
13091 
13092 static void
13093 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13094 			 unsigned int abbrev_number,
13095 			 struct abbrev_info *abbrev)
13096 {
13097   unsigned int hash_number;
13098 
13099   hash_number = abbrev_number % ABBREV_HASH_SIZE;
13100   abbrev->next = abbrev_table->abbrevs[hash_number];
13101   abbrev_table->abbrevs[hash_number] = abbrev;
13102 }
13103 
13104 /* Look up an abbrev in the table.
13105    Returns NULL if the abbrev is not found.  */
13106 
13107 static struct abbrev_info *
13108 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13109 			    unsigned int abbrev_number)
13110 {
13111   unsigned int hash_number;
13112   struct abbrev_info *abbrev;
13113 
13114   hash_number = abbrev_number % ABBREV_HASH_SIZE;
13115   abbrev = abbrev_table->abbrevs[hash_number];
13116 
13117   while (abbrev)
13118     {
13119       if (abbrev->number == abbrev_number)
13120 	return abbrev;
13121       abbrev = abbrev->next;
13122     }
13123   return NULL;
13124 }
13125 
13126 /* Read in an abbrev table.  */
13127 
13128 static struct abbrev_table *
13129 abbrev_table_read_table (struct dwarf2_section_info *section,
13130 			 sect_offset offset)
13131 {
13132   struct objfile *objfile = dwarf2_per_objfile->objfile;
13133   bfd *abfd = section->asection->owner;
13134   struct abbrev_table *abbrev_table;
13135   gdb_byte *abbrev_ptr;
13136   struct abbrev_info *cur_abbrev;
13137   unsigned int abbrev_number, bytes_read, abbrev_name;
13138   unsigned int abbrev_form;
13139   struct attr_abbrev *cur_attrs;
13140   unsigned int allocated_attrs;
13141 
13142   abbrev_table = XMALLOC (struct abbrev_table);
13143   abbrev_table->offset = offset;
13144   obstack_init (&abbrev_table->abbrev_obstack);
13145   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13146 					 (ABBREV_HASH_SIZE
13147 					  * sizeof (struct abbrev_info *)));
13148   memset (abbrev_table->abbrevs, 0,
13149 	  ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13150 
13151   dwarf2_read_section (objfile, section);
13152   abbrev_ptr = section->buffer + offset.sect_off;
13153   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13154   abbrev_ptr += bytes_read;
13155 
13156   allocated_attrs = ATTR_ALLOC_CHUNK;
13157   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13158 
13159   /* Loop until we reach an abbrev number of 0.  */
13160   while (abbrev_number)
13161     {
13162       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13163 
13164       /* read in abbrev header */
13165       cur_abbrev->number = abbrev_number;
13166       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13167       abbrev_ptr += bytes_read;
13168       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13169       abbrev_ptr += 1;
13170 
13171       /* now read in declarations */
13172       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13173       abbrev_ptr += bytes_read;
13174       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13175       abbrev_ptr += bytes_read;
13176       while (abbrev_name)
13177 	{
13178 	  if (cur_abbrev->num_attrs == allocated_attrs)
13179 	    {
13180 	      allocated_attrs += ATTR_ALLOC_CHUNK;
13181 	      cur_attrs
13182 		= xrealloc (cur_attrs, (allocated_attrs
13183 					* sizeof (struct attr_abbrev)));
13184 	    }
13185 
13186 	  cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13187 	  cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13188 	  abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13189 	  abbrev_ptr += bytes_read;
13190 	  abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13191 	  abbrev_ptr += bytes_read;
13192 	}
13193 
13194       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13195 					 (cur_abbrev->num_attrs
13196 					  * sizeof (struct attr_abbrev)));
13197       memcpy (cur_abbrev->attrs, cur_attrs,
13198 	      cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13199 
13200       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13201 
13202       /* Get next abbreviation.
13203          Under Irix6 the abbreviations for a compilation unit are not
13204          always properly terminated with an abbrev number of 0.
13205          Exit loop if we encounter an abbreviation which we have
13206          already read (which means we are about to read the abbreviations
13207          for the next compile unit) or if the end of the abbreviation
13208          table is reached.  */
13209       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13210 	break;
13211       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13212       abbrev_ptr += bytes_read;
13213       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13214 	break;
13215     }
13216 
13217   xfree (cur_attrs);
13218   return abbrev_table;
13219 }
13220 
13221 /* Free the resources held by ABBREV_TABLE.  */
13222 
13223 static void
13224 abbrev_table_free (struct abbrev_table *abbrev_table)
13225 {
13226   obstack_free (&abbrev_table->abbrev_obstack, NULL);
13227   xfree (abbrev_table);
13228 }
13229 
13230 /* Same as abbrev_table_free but as a cleanup.
13231    We pass in a pointer to the pointer to the table so that we can
13232    set the pointer to NULL when we're done.  It also simplifies
13233    build_type_unit_groups.  */
13234 
13235 static void
13236 abbrev_table_free_cleanup (void *table_ptr)
13237 {
13238   struct abbrev_table **abbrev_table_ptr = table_ptr;
13239 
13240   if (*abbrev_table_ptr != NULL)
13241     abbrev_table_free (*abbrev_table_ptr);
13242   *abbrev_table_ptr = NULL;
13243 }
13244 
13245 /* Read the abbrev table for CU from ABBREV_SECTION.  */
13246 
13247 static void
13248 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13249 		     struct dwarf2_section_info *abbrev_section)
13250 {
13251   cu->abbrev_table =
13252     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13253 }
13254 
13255 /* Release the memory used by the abbrev table for a compilation unit.  */
13256 
13257 static void
13258 dwarf2_free_abbrev_table (void *ptr_to_cu)
13259 {
13260   struct dwarf2_cu *cu = ptr_to_cu;
13261 
13262   abbrev_table_free (cu->abbrev_table);
13263   /* Set this to NULL so that we SEGV if we try to read it later,
13264      and also because free_comp_unit verifies this is NULL.  */
13265   cu->abbrev_table = NULL;
13266 }
13267 
13268 /* Returns nonzero if TAG represents a type that we might generate a partial
13269    symbol for.  */
13270 
13271 static int
13272 is_type_tag_for_partial (int tag)
13273 {
13274   switch (tag)
13275     {
13276 #if 0
13277     /* Some types that would be reasonable to generate partial symbols for,
13278        that we don't at present.  */
13279     case DW_TAG_array_type:
13280     case DW_TAG_file_type:
13281     case DW_TAG_ptr_to_member_type:
13282     case DW_TAG_set_type:
13283     case DW_TAG_string_type:
13284     case DW_TAG_subroutine_type:
13285 #endif
13286     case DW_TAG_base_type:
13287     case DW_TAG_class_type:
13288     case DW_TAG_interface_type:
13289     case DW_TAG_enumeration_type:
13290     case DW_TAG_structure_type:
13291     case DW_TAG_subrange_type:
13292     case DW_TAG_typedef:
13293     case DW_TAG_union_type:
13294       return 1;
13295     default:
13296       return 0;
13297     }
13298 }
13299 
13300 /* Load all DIEs that are interesting for partial symbols into memory.  */
13301 
13302 static struct partial_die_info *
13303 load_partial_dies (const struct die_reader_specs *reader,
13304 		   gdb_byte *info_ptr, int building_psymtab)
13305 {
13306   struct dwarf2_cu *cu = reader->cu;
13307   struct objfile *objfile = cu->objfile;
13308   struct partial_die_info *part_die;
13309   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13310   struct abbrev_info *abbrev;
13311   unsigned int bytes_read;
13312   unsigned int load_all = 0;
13313   int nesting_level = 1;
13314 
13315   parent_die = NULL;
13316   last_die = NULL;
13317 
13318   gdb_assert (cu->per_cu != NULL);
13319   if (cu->per_cu->load_all_dies)
13320     load_all = 1;
13321 
13322   cu->partial_dies
13323     = htab_create_alloc_ex (cu->header.length / 12,
13324 			    partial_die_hash,
13325 			    partial_die_eq,
13326 			    NULL,
13327 			    &cu->comp_unit_obstack,
13328 			    hashtab_obstack_allocate,
13329 			    dummy_obstack_deallocate);
13330 
13331   part_die = obstack_alloc (&cu->comp_unit_obstack,
13332 			    sizeof (struct partial_die_info));
13333 
13334   while (1)
13335     {
13336       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13337 
13338       /* A NULL abbrev means the end of a series of children.  */
13339       if (abbrev == NULL)
13340 	{
13341 	  if (--nesting_level == 0)
13342 	    {
13343 	      /* PART_DIE was probably the last thing allocated on the
13344 		 comp_unit_obstack, so we could call obstack_free
13345 		 here.  We don't do that because the waste is small,
13346 		 and will be cleaned up when we're done with this
13347 		 compilation unit.  This way, we're also more robust
13348 		 against other users of the comp_unit_obstack.  */
13349 	      return first_die;
13350 	    }
13351 	  info_ptr += bytes_read;
13352 	  last_die = parent_die;
13353 	  parent_die = parent_die->die_parent;
13354 	  continue;
13355 	}
13356 
13357       /* Check for template arguments.  We never save these; if
13358 	 they're seen, we just mark the parent, and go on our way.  */
13359       if (parent_die != NULL
13360 	  && cu->language == language_cplus
13361 	  && (abbrev->tag == DW_TAG_template_type_param
13362 	      || abbrev->tag == DW_TAG_template_value_param))
13363 	{
13364 	  parent_die->has_template_arguments = 1;
13365 
13366 	  if (!load_all)
13367 	    {
13368 	      /* We don't need a partial DIE for the template argument.  */
13369 	      info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13370 	      continue;
13371 	    }
13372 	}
13373 
13374       /* We only recurse into c++ subprograms looking for template arguments.
13375 	 Skip their other children.  */
13376       if (!load_all
13377 	  && cu->language == language_cplus
13378 	  && parent_die != NULL
13379 	  && parent_die->tag == DW_TAG_subprogram)
13380 	{
13381 	  info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13382 	  continue;
13383 	}
13384 
13385       /* Check whether this DIE is interesting enough to save.  Normally
13386 	 we would not be interested in members here, but there may be
13387 	 later variables referencing them via DW_AT_specification (for
13388 	 static members).  */
13389       if (!load_all
13390 	  && !is_type_tag_for_partial (abbrev->tag)
13391 	  && abbrev->tag != DW_TAG_constant
13392 	  && abbrev->tag != DW_TAG_enumerator
13393 	  && abbrev->tag != DW_TAG_subprogram
13394 	  && abbrev->tag != DW_TAG_lexical_block
13395 	  && abbrev->tag != DW_TAG_variable
13396 	  && abbrev->tag != DW_TAG_namespace
13397 	  && abbrev->tag != DW_TAG_module
13398 	  && abbrev->tag != DW_TAG_member
13399 	  && abbrev->tag != DW_TAG_imported_unit)
13400 	{
13401 	  /* Otherwise we skip to the next sibling, if any.  */
13402 	  info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13403 	  continue;
13404 	}
13405 
13406       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13407 				   info_ptr);
13408 
13409       /* This two-pass algorithm for processing partial symbols has a
13410 	 high cost in cache pressure.  Thus, handle some simple cases
13411 	 here which cover the majority of C partial symbols.  DIEs
13412 	 which neither have specification tags in them, nor could have
13413 	 specification tags elsewhere pointing at them, can simply be
13414 	 processed and discarded.
13415 
13416 	 This segment is also optional; scan_partial_symbols and
13417 	 add_partial_symbol will handle these DIEs if we chain
13418 	 them in normally.  When compilers which do not emit large
13419 	 quantities of duplicate debug information are more common,
13420 	 this code can probably be removed.  */
13421 
13422       /* Any complete simple types at the top level (pretty much all
13423 	 of them, for a language without namespaces), can be processed
13424 	 directly.  */
13425       if (parent_die == NULL
13426 	  && part_die->has_specification == 0
13427 	  && part_die->is_declaration == 0
13428 	  && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13429 	      || part_die->tag == DW_TAG_base_type
13430 	      || part_die->tag == DW_TAG_subrange_type))
13431 	{
13432 	  if (building_psymtab && part_die->name != NULL)
13433 	    add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13434 				 VAR_DOMAIN, LOC_TYPEDEF,
13435 				 &objfile->static_psymbols,
13436 				 0, (CORE_ADDR) 0, cu->language, objfile);
13437 	  info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13438 	  continue;
13439 	}
13440 
13441       /* The exception for DW_TAG_typedef with has_children above is
13442 	 a workaround of GCC PR debug/47510.  In the case of this complaint
13443 	 type_name_no_tag_or_error will error on such types later.
13444 
13445 	 GDB skipped children of DW_TAG_typedef by the shortcut above and then
13446 	 it could not find the child DIEs referenced later, this is checked
13447 	 above.  In correct DWARF DW_TAG_typedef should have no children.  */
13448 
13449       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13450 	complaint (&symfile_complaints,
13451 		   _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13452 		     "- DIE at 0x%x [in module %s]"),
13453 		   part_die->offset.sect_off, objfile->name);
13454 
13455       /* If we're at the second level, and we're an enumerator, and
13456 	 our parent has no specification (meaning possibly lives in a
13457 	 namespace elsewhere), then we can add the partial symbol now
13458 	 instead of queueing it.  */
13459       if (part_die->tag == DW_TAG_enumerator
13460 	  && parent_die != NULL
13461 	  && parent_die->die_parent == NULL
13462 	  && parent_die->tag == DW_TAG_enumeration_type
13463 	  && parent_die->has_specification == 0)
13464 	{
13465 	  if (part_die->name == NULL)
13466 	    complaint (&symfile_complaints,
13467 		       _("malformed enumerator DIE ignored"));
13468 	  else if (building_psymtab)
13469 	    add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13470 				 VAR_DOMAIN, LOC_CONST,
13471 				 (cu->language == language_cplus
13472 				  || cu->language == language_java)
13473 				 ? &objfile->global_psymbols
13474 				 : &objfile->static_psymbols,
13475 				 0, (CORE_ADDR) 0, cu->language, objfile);
13476 
13477 	  info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13478 	  continue;
13479 	}
13480 
13481       /* We'll save this DIE so link it in.  */
13482       part_die->die_parent = parent_die;
13483       part_die->die_sibling = NULL;
13484       part_die->die_child = NULL;
13485 
13486       if (last_die && last_die == parent_die)
13487 	last_die->die_child = part_die;
13488       else if (last_die)
13489 	last_die->die_sibling = part_die;
13490 
13491       last_die = part_die;
13492 
13493       if (first_die == NULL)
13494 	first_die = part_die;
13495 
13496       /* Maybe add the DIE to the hash table.  Not all DIEs that we
13497 	 find interesting need to be in the hash table, because we
13498 	 also have the parent/sibling/child chains; only those that we
13499 	 might refer to by offset later during partial symbol reading.
13500 
13501 	 For now this means things that might have be the target of a
13502 	 DW_AT_specification, DW_AT_abstract_origin, or
13503 	 DW_AT_extension.  DW_AT_extension will refer only to
13504 	 namespaces; DW_AT_abstract_origin refers to functions (and
13505 	 many things under the function DIE, but we do not recurse
13506 	 into function DIEs during partial symbol reading) and
13507 	 possibly variables as well; DW_AT_specification refers to
13508 	 declarations.  Declarations ought to have the DW_AT_declaration
13509 	 flag.  It happens that GCC forgets to put it in sometimes, but
13510 	 only for functions, not for types.
13511 
13512 	 Adding more things than necessary to the hash table is harmless
13513 	 except for the performance cost.  Adding too few will result in
13514 	 wasted time in find_partial_die, when we reread the compilation
13515 	 unit with load_all_dies set.  */
13516 
13517       if (load_all
13518 	  || abbrev->tag == DW_TAG_constant
13519 	  || abbrev->tag == DW_TAG_subprogram
13520 	  || abbrev->tag == DW_TAG_variable
13521 	  || abbrev->tag == DW_TAG_namespace
13522 	  || part_die->is_declaration)
13523 	{
13524 	  void **slot;
13525 
13526 	  slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13527 					   part_die->offset.sect_off, INSERT);
13528 	  *slot = part_die;
13529 	}
13530 
13531       part_die = obstack_alloc (&cu->comp_unit_obstack,
13532 				sizeof (struct partial_die_info));
13533 
13534       /* For some DIEs we want to follow their children (if any).  For C
13535 	 we have no reason to follow the children of structures; for other
13536 	 languages we have to, so that we can get at method physnames
13537 	 to infer fully qualified class names, for DW_AT_specification,
13538 	 and for C++ template arguments.  For C++, we also look one level
13539 	 inside functions to find template arguments (if the name of the
13540 	 function does not already contain the template arguments).
13541 
13542 	 For Ada, we need to scan the children of subprograms and lexical
13543 	 blocks as well because Ada allows the definition of nested
13544 	 entities that could be interesting for the debugger, such as
13545 	 nested subprograms for instance.  */
13546       if (last_die->has_children
13547 	  && (load_all
13548 	      || last_die->tag == DW_TAG_namespace
13549 	      || last_die->tag == DW_TAG_module
13550 	      || last_die->tag == DW_TAG_enumeration_type
13551 	      || (cu->language == language_cplus
13552 		  && last_die->tag == DW_TAG_subprogram
13553 		  && (last_die->name == NULL
13554 		      || strchr (last_die->name, '<') == NULL))
13555 	      || (cu->language != language_c
13556 		  && (last_die->tag == DW_TAG_class_type
13557 		      || last_die->tag == DW_TAG_interface_type
13558 		      || last_die->tag == DW_TAG_structure_type
13559 		      || last_die->tag == DW_TAG_union_type))
13560 	      || (cu->language == language_ada
13561 		  && (last_die->tag == DW_TAG_subprogram
13562 		      || last_die->tag == DW_TAG_lexical_block))))
13563 	{
13564 	  nesting_level++;
13565 	  parent_die = last_die;
13566 	  continue;
13567 	}
13568 
13569       /* Otherwise we skip to the next sibling, if any.  */
13570       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13571 
13572       /* Back to the top, do it again.  */
13573     }
13574 }
13575 
13576 /* Read a minimal amount of information into the minimal die structure.  */
13577 
13578 static gdb_byte *
13579 read_partial_die (const struct die_reader_specs *reader,
13580 		  struct partial_die_info *part_die,
13581 		  struct abbrev_info *abbrev, unsigned int abbrev_len,
13582 		  gdb_byte *info_ptr)
13583 {
13584   struct dwarf2_cu *cu = reader->cu;
13585   struct objfile *objfile = cu->objfile;
13586   gdb_byte *buffer = reader->buffer;
13587   unsigned int i;
13588   struct attribute attr;
13589   int has_low_pc_attr = 0;
13590   int has_high_pc_attr = 0;
13591   int high_pc_relative = 0;
13592 
13593   memset (part_die, 0, sizeof (struct partial_die_info));
13594 
13595   part_die->offset.sect_off = info_ptr - buffer;
13596 
13597   info_ptr += abbrev_len;
13598 
13599   if (abbrev == NULL)
13600     return info_ptr;
13601 
13602   part_die->tag = abbrev->tag;
13603   part_die->has_children = abbrev->has_children;
13604 
13605   for (i = 0; i < abbrev->num_attrs; ++i)
13606     {
13607       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13608 
13609       /* Store the data if it is of an attribute we want to keep in a
13610          partial symbol table.  */
13611       switch (attr.name)
13612 	{
13613 	case DW_AT_name:
13614 	  switch (part_die->tag)
13615 	    {
13616 	    case DW_TAG_compile_unit:
13617 	    case DW_TAG_partial_unit:
13618 	    case DW_TAG_type_unit:
13619 	      /* Compilation units have a DW_AT_name that is a filename, not
13620 		 a source language identifier.  */
13621 	    case DW_TAG_enumeration_type:
13622 	    case DW_TAG_enumerator:
13623 	      /* These tags always have simple identifiers already; no need
13624 		 to canonicalize them.  */
13625 	      part_die->name = DW_STRING (&attr);
13626 	      break;
13627 	    default:
13628 	      part_die->name
13629 		= dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13630 					    &objfile->objfile_obstack);
13631 	      break;
13632 	    }
13633 	  break;
13634 	case DW_AT_linkage_name:
13635 	case DW_AT_MIPS_linkage_name:
13636 	  /* Note that both forms of linkage name might appear.  We
13637 	     assume they will be the same, and we only store the last
13638 	     one we see.  */
13639 	  if (cu->language == language_ada)
13640 	    part_die->name = DW_STRING (&attr);
13641 	  part_die->linkage_name = DW_STRING (&attr);
13642 	  break;
13643 	case DW_AT_low_pc:
13644 	  has_low_pc_attr = 1;
13645 	  part_die->lowpc = DW_ADDR (&attr);
13646 	  break;
13647 	case DW_AT_high_pc:
13648 	  has_high_pc_attr = 1;
13649 	  if (attr.form == DW_FORM_addr
13650 	      || attr.form == DW_FORM_GNU_addr_index)
13651 	    part_die->highpc = DW_ADDR (&attr);
13652 	  else
13653 	    {
13654 	      high_pc_relative = 1;
13655 	      part_die->highpc = DW_UNSND (&attr);
13656 	    }
13657 	  break;
13658 	case DW_AT_location:
13659           /* Support the .debug_loc offsets.  */
13660           if (attr_form_is_block (&attr))
13661             {
13662 	       part_die->d.locdesc = DW_BLOCK (&attr);
13663             }
13664           else if (attr_form_is_section_offset (&attr))
13665             {
13666 	      dwarf2_complex_location_expr_complaint ();
13667             }
13668           else
13669             {
13670 	      dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13671 						     "partial symbol information");
13672             }
13673 	  break;
13674 	case DW_AT_external:
13675 	  part_die->is_external = DW_UNSND (&attr);
13676 	  break;
13677 	case DW_AT_declaration:
13678 	  part_die->is_declaration = DW_UNSND (&attr);
13679 	  break;
13680 	case DW_AT_type:
13681 	  part_die->has_type = 1;
13682 	  break;
13683 	case DW_AT_abstract_origin:
13684 	case DW_AT_specification:
13685 	case DW_AT_extension:
13686 	  part_die->has_specification = 1;
13687 	  part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13688 	  part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13689 				   || cu->per_cu->is_dwz);
13690 	  break;
13691 	case DW_AT_sibling:
13692 	  /* Ignore absolute siblings, they might point outside of
13693 	     the current compile unit.  */
13694 	  if (attr.form == DW_FORM_ref_addr)
13695 	    complaint (&symfile_complaints,
13696 		       _("ignoring absolute DW_AT_sibling"));
13697 	  else
13698 	    part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13699 	  break;
13700         case DW_AT_byte_size:
13701           part_die->has_byte_size = 1;
13702           break;
13703 	case DW_AT_calling_convention:
13704 	  /* DWARF doesn't provide a way to identify a program's source-level
13705 	     entry point.  DW_AT_calling_convention attributes are only meant
13706 	     to describe functions' calling conventions.
13707 
13708 	     However, because it's a necessary piece of information in
13709 	     Fortran, and because DW_CC_program is the only piece of debugging
13710 	     information whose definition refers to a 'main program' at all,
13711 	     several compilers have begun marking Fortran main programs with
13712 	     DW_CC_program --- even when those functions use the standard
13713 	     calling conventions.
13714 
13715 	     So until DWARF specifies a way to provide this information and
13716 	     compilers pick up the new representation, we'll support this
13717 	     practice.  */
13718 	  if (DW_UNSND (&attr) == DW_CC_program
13719 	      && cu->language == language_fortran)
13720 	    {
13721 	      set_main_name (part_die->name);
13722 
13723 	      /* As this DIE has a static linkage the name would be difficult
13724 		 to look up later.  */
13725 	      language_of_main = language_fortran;
13726 	    }
13727 	  break;
13728 	case DW_AT_inline:
13729 	  if (DW_UNSND (&attr) == DW_INL_inlined
13730 	      || DW_UNSND (&attr) == DW_INL_declared_inlined)
13731 	    part_die->may_be_inlined = 1;
13732 	  break;
13733 
13734 	case DW_AT_import:
13735 	  if (part_die->tag == DW_TAG_imported_unit)
13736 	    {
13737 	      part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13738 	      part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13739 				  || cu->per_cu->is_dwz);
13740 	    }
13741 	  break;
13742 
13743 	default:
13744 	  break;
13745 	}
13746     }
13747 
13748   if (high_pc_relative)
13749     part_die->highpc += part_die->lowpc;
13750 
13751   if (has_low_pc_attr && has_high_pc_attr)
13752     {
13753       /* When using the GNU linker, .gnu.linkonce. sections are used to
13754 	 eliminate duplicate copies of functions and vtables and such.
13755 	 The linker will arbitrarily choose one and discard the others.
13756 	 The AT_*_pc values for such functions refer to local labels in
13757 	 these sections.  If the section from that file was discarded, the
13758 	 labels are not in the output, so the relocs get a value of 0.
13759 	 If this is a discarded function, mark the pc bounds as invalid,
13760 	 so that GDB will ignore it.  */
13761       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13762 	{
13763 	  struct gdbarch *gdbarch = get_objfile_arch (objfile);
13764 
13765 	  complaint (&symfile_complaints,
13766 		     _("DW_AT_low_pc %s is zero "
13767 		       "for DIE at 0x%x [in module %s]"),
13768 		     paddress (gdbarch, part_die->lowpc),
13769 		     part_die->offset.sect_off, objfile->name);
13770 	}
13771       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
13772       else if (part_die->lowpc >= part_die->highpc)
13773 	{
13774 	  struct gdbarch *gdbarch = get_objfile_arch (objfile);
13775 
13776 	  complaint (&symfile_complaints,
13777 		     _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13778 		       "for DIE at 0x%x [in module %s]"),
13779 		     paddress (gdbarch, part_die->lowpc),
13780 		     paddress (gdbarch, part_die->highpc),
13781 		     part_die->offset.sect_off, objfile->name);
13782 	}
13783       else
13784 	part_die->has_pc_info = 1;
13785     }
13786 
13787   return info_ptr;
13788 }
13789 
13790 /* Find a cached partial DIE at OFFSET in CU.  */
13791 
13792 static struct partial_die_info *
13793 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13794 {
13795   struct partial_die_info *lookup_die = NULL;
13796   struct partial_die_info part_die;
13797 
13798   part_die.offset = offset;
13799   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13800 				    offset.sect_off);
13801 
13802   return lookup_die;
13803 }
13804 
13805 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13806    except in the case of .debug_types DIEs which do not reference
13807    outside their CU (they do however referencing other types via
13808    DW_FORM_ref_sig8).  */
13809 
13810 static struct partial_die_info *
13811 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13812 {
13813   struct objfile *objfile = cu->objfile;
13814   struct dwarf2_per_cu_data *per_cu = NULL;
13815   struct partial_die_info *pd = NULL;
13816 
13817   if (offset_in_dwz == cu->per_cu->is_dwz
13818       && offset_in_cu_p (&cu->header, offset))
13819     {
13820       pd = find_partial_die_in_comp_unit (offset, cu);
13821       if (pd != NULL)
13822 	return pd;
13823       /* We missed recording what we needed.
13824 	 Load all dies and try again.  */
13825       per_cu = cu->per_cu;
13826     }
13827   else
13828     {
13829       /* TUs don't reference other CUs/TUs (except via type signatures).  */
13830       if (cu->per_cu->is_debug_types)
13831 	{
13832 	  error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13833 		   " external reference to offset 0x%lx [in module %s].\n"),
13834 		 (long) cu->header.offset.sect_off, (long) offset.sect_off,
13835 		 bfd_get_filename (objfile->obfd));
13836 	}
13837       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13838 						 objfile);
13839 
13840       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13841 	load_partial_comp_unit (per_cu);
13842 
13843       per_cu->cu->last_used = 0;
13844       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13845     }
13846 
13847   /* If we didn't find it, and not all dies have been loaded,
13848      load them all and try again.  */
13849 
13850   if (pd == NULL && per_cu->load_all_dies == 0)
13851     {
13852       per_cu->load_all_dies = 1;
13853 
13854       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
13855 	 THIS_CU->cu may already be in use.  So we can't just free it and
13856 	 replace its DIEs with the ones we read in.  Instead, we leave those
13857 	 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13858 	 and clobber THIS_CU->cu->partial_dies with the hash table for the new
13859 	 set.  */
13860       load_partial_comp_unit (per_cu);
13861 
13862       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13863     }
13864 
13865   if (pd == NULL)
13866     internal_error (__FILE__, __LINE__,
13867 		    _("could not find partial DIE 0x%x "
13868 		      "in cache [from module %s]\n"),
13869 		    offset.sect_off, bfd_get_filename (objfile->obfd));
13870   return pd;
13871 }
13872 
13873 /* See if we can figure out if the class lives in a namespace.  We do
13874    this by looking for a member function; its demangled name will
13875    contain namespace info, if there is any.  */
13876 
13877 static void
13878 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13879 				  struct dwarf2_cu *cu)
13880 {
13881   /* NOTE: carlton/2003-10-07: Getting the info this way changes
13882      what template types look like, because the demangler
13883      frequently doesn't give the same name as the debug info.  We
13884      could fix this by only using the demangled name to get the
13885      prefix (but see comment in read_structure_type).  */
13886 
13887   struct partial_die_info *real_pdi;
13888   struct partial_die_info *child_pdi;
13889 
13890   /* If this DIE (this DIE's specification, if any) has a parent, then
13891      we should not do this.  We'll prepend the parent's fully qualified
13892      name when we create the partial symbol.  */
13893 
13894   real_pdi = struct_pdi;
13895   while (real_pdi->has_specification)
13896     real_pdi = find_partial_die (real_pdi->spec_offset,
13897 				 real_pdi->spec_is_dwz, cu);
13898 
13899   if (real_pdi->die_parent != NULL)
13900     return;
13901 
13902   for (child_pdi = struct_pdi->die_child;
13903        child_pdi != NULL;
13904        child_pdi = child_pdi->die_sibling)
13905     {
13906       if (child_pdi->tag == DW_TAG_subprogram
13907 	  && child_pdi->linkage_name != NULL)
13908 	{
13909 	  char *actual_class_name
13910 	    = language_class_name_from_physname (cu->language_defn,
13911 						 child_pdi->linkage_name);
13912 	  if (actual_class_name != NULL)
13913 	    {
13914 	      struct_pdi->name
13915 		= obstack_copy0 (&cu->objfile->objfile_obstack,
13916 				 actual_class_name,
13917 				 strlen (actual_class_name));
13918 	      xfree (actual_class_name);
13919 	    }
13920 	  break;
13921 	}
13922     }
13923 }
13924 
13925 /* Adjust PART_DIE before generating a symbol for it.  This function
13926    may set the is_external flag or change the DIE's name.  */
13927 
13928 static void
13929 fixup_partial_die (struct partial_die_info *part_die,
13930 		   struct dwarf2_cu *cu)
13931 {
13932   /* Once we've fixed up a die, there's no point in doing so again.
13933      This also avoids a memory leak if we were to call
13934      guess_partial_die_structure_name multiple times.  */
13935   if (part_die->fixup_called)
13936     return;
13937 
13938   /* If we found a reference attribute and the DIE has no name, try
13939      to find a name in the referred to DIE.  */
13940 
13941   if (part_die->name == NULL && part_die->has_specification)
13942     {
13943       struct partial_die_info *spec_die;
13944 
13945       spec_die = find_partial_die (part_die->spec_offset,
13946 				   part_die->spec_is_dwz, cu);
13947 
13948       fixup_partial_die (spec_die, cu);
13949 
13950       if (spec_die->name)
13951 	{
13952 	  part_die->name = spec_die->name;
13953 
13954 	  /* Copy DW_AT_external attribute if it is set.  */
13955 	  if (spec_die->is_external)
13956 	    part_die->is_external = spec_die->is_external;
13957 	}
13958     }
13959 
13960   /* Set default names for some unnamed DIEs.  */
13961 
13962   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13963     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13964 
13965   /* If there is no parent die to provide a namespace, and there are
13966      children, see if we can determine the namespace from their linkage
13967      name.  */
13968   if (cu->language == language_cplus
13969       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13970       && part_die->die_parent == NULL
13971       && part_die->has_children
13972       && (part_die->tag == DW_TAG_class_type
13973 	  || part_die->tag == DW_TAG_structure_type
13974 	  || part_die->tag == DW_TAG_union_type))
13975     guess_partial_die_structure_name (part_die, cu);
13976 
13977   /* GCC might emit a nameless struct or union that has a linkage
13978      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
13979   if (part_die->name == NULL
13980       && (part_die->tag == DW_TAG_class_type
13981 	  || part_die->tag == DW_TAG_interface_type
13982 	  || part_die->tag == DW_TAG_structure_type
13983 	  || part_die->tag == DW_TAG_union_type)
13984       && part_die->linkage_name != NULL)
13985     {
13986       char *demangled;
13987 
13988       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13989       if (demangled)
13990 	{
13991 	  const char *base;
13992 
13993 	  /* Strip any leading namespaces/classes, keep only the base name.
13994 	     DW_AT_name for named DIEs does not contain the prefixes.  */
13995 	  base = strrchr (demangled, ':');
13996 	  if (base && base > demangled && base[-1] == ':')
13997 	    base++;
13998 	  else
13999 	    base = demangled;
14000 
14001 	  part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14002 					  base, strlen (base));
14003 	  xfree (demangled);
14004 	}
14005     }
14006 
14007   part_die->fixup_called = 1;
14008 }
14009 
14010 /* Read an attribute value described by an attribute form.  */
14011 
14012 static gdb_byte *
14013 read_attribute_value (const struct die_reader_specs *reader,
14014 		      struct attribute *attr, unsigned form,
14015 		      gdb_byte *info_ptr)
14016 {
14017   struct dwarf2_cu *cu = reader->cu;
14018   bfd *abfd = reader->abfd;
14019   struct comp_unit_head *cu_header = &cu->header;
14020   unsigned int bytes_read;
14021   struct dwarf_block *blk;
14022 
14023   attr->form = form;
14024   switch (form)
14025     {
14026     case DW_FORM_ref_addr:
14027       if (cu->header.version == 2)
14028 	DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14029       else
14030 	DW_UNSND (attr) = read_offset (abfd, info_ptr,
14031 				       &cu->header, &bytes_read);
14032       info_ptr += bytes_read;
14033       break;
14034     case DW_FORM_GNU_ref_alt:
14035       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14036       info_ptr += bytes_read;
14037       break;
14038     case DW_FORM_addr:
14039       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14040       info_ptr += bytes_read;
14041       break;
14042     case DW_FORM_block2:
14043       blk = dwarf_alloc_block (cu);
14044       blk->size = read_2_bytes (abfd, info_ptr);
14045       info_ptr += 2;
14046       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14047       info_ptr += blk->size;
14048       DW_BLOCK (attr) = blk;
14049       break;
14050     case DW_FORM_block4:
14051       blk = dwarf_alloc_block (cu);
14052       blk->size = read_4_bytes (abfd, info_ptr);
14053       info_ptr += 4;
14054       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14055       info_ptr += blk->size;
14056       DW_BLOCK (attr) = blk;
14057       break;
14058     case DW_FORM_data2:
14059       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14060       info_ptr += 2;
14061       break;
14062     case DW_FORM_data4:
14063       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14064       info_ptr += 4;
14065       break;
14066     case DW_FORM_data8:
14067       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14068       info_ptr += 8;
14069       break;
14070     case DW_FORM_sec_offset:
14071       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14072       info_ptr += bytes_read;
14073       break;
14074     case DW_FORM_string:
14075       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14076       DW_STRING_IS_CANONICAL (attr) = 0;
14077       info_ptr += bytes_read;
14078       break;
14079     case DW_FORM_strp:
14080       if (!cu->per_cu->is_dwz)
14081 	{
14082 	  DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14083 						   &bytes_read);
14084 	  DW_STRING_IS_CANONICAL (attr) = 0;
14085 	  info_ptr += bytes_read;
14086 	  break;
14087 	}
14088       /* FALLTHROUGH */
14089     case DW_FORM_GNU_strp_alt:
14090       {
14091 	struct dwz_file *dwz = dwarf2_get_dwz_file ();
14092 	LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14093 					  &bytes_read);
14094 
14095 	DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14096 	DW_STRING_IS_CANONICAL (attr) = 0;
14097 	info_ptr += bytes_read;
14098       }
14099       break;
14100     case DW_FORM_exprloc:
14101     case DW_FORM_block:
14102       blk = dwarf_alloc_block (cu);
14103       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14104       info_ptr += bytes_read;
14105       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14106       info_ptr += blk->size;
14107       DW_BLOCK (attr) = blk;
14108       break;
14109     case DW_FORM_block1:
14110       blk = dwarf_alloc_block (cu);
14111       blk->size = read_1_byte (abfd, info_ptr);
14112       info_ptr += 1;
14113       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14114       info_ptr += blk->size;
14115       DW_BLOCK (attr) = blk;
14116       break;
14117     case DW_FORM_data1:
14118       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14119       info_ptr += 1;
14120       break;
14121     case DW_FORM_flag:
14122       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14123       info_ptr += 1;
14124       break;
14125     case DW_FORM_flag_present:
14126       DW_UNSND (attr) = 1;
14127       break;
14128     case DW_FORM_sdata:
14129       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14130       info_ptr += bytes_read;
14131       break;
14132     case DW_FORM_udata:
14133       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14134       info_ptr += bytes_read;
14135       break;
14136     case DW_FORM_ref1:
14137       DW_UNSND (attr) = (cu->header.offset.sect_off
14138 			 + read_1_byte (abfd, info_ptr));
14139       info_ptr += 1;
14140       break;
14141     case DW_FORM_ref2:
14142       DW_UNSND (attr) = (cu->header.offset.sect_off
14143 			 + read_2_bytes (abfd, info_ptr));
14144       info_ptr += 2;
14145       break;
14146     case DW_FORM_ref4:
14147       DW_UNSND (attr) = (cu->header.offset.sect_off
14148 			 + read_4_bytes (abfd, info_ptr));
14149       info_ptr += 4;
14150       break;
14151     case DW_FORM_ref8:
14152       DW_UNSND (attr) = (cu->header.offset.sect_off
14153 			 + read_8_bytes (abfd, info_ptr));
14154       info_ptr += 8;
14155       break;
14156     case DW_FORM_ref_sig8:
14157       /* Convert the signature to something we can record in DW_UNSND
14158 	 for later lookup.
14159          NOTE: This is NULL if the type wasn't found.  */
14160       DW_SIGNATURED_TYPE (attr) =
14161 	lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14162       info_ptr += 8;
14163       break;
14164     case DW_FORM_ref_udata:
14165       DW_UNSND (attr) = (cu->header.offset.sect_off
14166 			 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14167       info_ptr += bytes_read;
14168       break;
14169     case DW_FORM_indirect:
14170       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14171       info_ptr += bytes_read;
14172       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14173       break;
14174     case DW_FORM_GNU_addr_index:
14175       if (reader->dwo_file == NULL)
14176 	{
14177 	  /* For now flag a hard error.
14178 	     Later we can turn this into a complaint.  */
14179 	  error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14180 		 dwarf_form_name (form),
14181 		 bfd_get_filename (abfd));
14182 	}
14183       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14184       info_ptr += bytes_read;
14185       break;
14186     case DW_FORM_GNU_str_index:
14187       if (reader->dwo_file == NULL)
14188 	{
14189 	  /* For now flag a hard error.
14190 	     Later we can turn this into a complaint if warranted.  */
14191 	  error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14192 		 dwarf_form_name (form),
14193 		 bfd_get_filename (abfd));
14194 	}
14195       {
14196 	ULONGEST str_index =
14197 	  read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14198 
14199 	DW_STRING (attr) = read_str_index (reader, cu, str_index);
14200 	DW_STRING_IS_CANONICAL (attr) = 0;
14201 	info_ptr += bytes_read;
14202       }
14203       break;
14204     default:
14205       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14206 	     dwarf_form_name (form),
14207 	     bfd_get_filename (abfd));
14208     }
14209 
14210   /* Super hack.  */
14211   if (cu->per_cu->is_dwz && is_ref_attr (attr))
14212     attr->form = DW_FORM_GNU_ref_alt;
14213 
14214   /* We have seen instances where the compiler tried to emit a byte
14215      size attribute of -1 which ended up being encoded as an unsigned
14216      0xffffffff.  Although 0xffffffff is technically a valid size value,
14217      an object of this size seems pretty unlikely so we can relatively
14218      safely treat these cases as if the size attribute was invalid and
14219      treat them as zero by default.  */
14220   if (attr->name == DW_AT_byte_size
14221       && form == DW_FORM_data4
14222       && DW_UNSND (attr) >= 0xffffffff)
14223     {
14224       complaint
14225         (&symfile_complaints,
14226          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14227          hex_string (DW_UNSND (attr)));
14228       DW_UNSND (attr) = 0;
14229     }
14230 
14231   return info_ptr;
14232 }
14233 
14234 /* Read an attribute described by an abbreviated attribute.  */
14235 
14236 static gdb_byte *
14237 read_attribute (const struct die_reader_specs *reader,
14238 		struct attribute *attr, struct attr_abbrev *abbrev,
14239 		gdb_byte *info_ptr)
14240 {
14241   attr->name = abbrev->name;
14242   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14243 }
14244 
14245 /* Read dwarf information from a buffer.  */
14246 
14247 static unsigned int
14248 read_1_byte (bfd *abfd, const gdb_byte *buf)
14249 {
14250   return bfd_get_8 (abfd, buf);
14251 }
14252 
14253 static int
14254 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14255 {
14256   return bfd_get_signed_8 (abfd, buf);
14257 }
14258 
14259 static unsigned int
14260 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14261 {
14262   return bfd_get_16 (abfd, buf);
14263 }
14264 
14265 static int
14266 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14267 {
14268   return bfd_get_signed_16 (abfd, buf);
14269 }
14270 
14271 static unsigned int
14272 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14273 {
14274   return bfd_get_32 (abfd, buf);
14275 }
14276 
14277 static int
14278 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14279 {
14280   return bfd_get_signed_32 (abfd, buf);
14281 }
14282 
14283 static ULONGEST
14284 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14285 {
14286   return bfd_get_64 (abfd, buf);
14287 }
14288 
14289 static CORE_ADDR
14290 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14291 	      unsigned int *bytes_read)
14292 {
14293   struct comp_unit_head *cu_header = &cu->header;
14294   CORE_ADDR retval = 0;
14295 
14296   if (cu_header->signed_addr_p)
14297     {
14298       switch (cu_header->addr_size)
14299 	{
14300 	case 2:
14301 	  retval = bfd_get_signed_16 (abfd, buf);
14302 	  break;
14303 	case 4:
14304 	  retval = bfd_get_signed_32 (abfd, buf);
14305 	  break;
14306 	case 8:
14307 	  retval = bfd_get_signed_64 (abfd, buf);
14308 	  break;
14309 	default:
14310 	  internal_error (__FILE__, __LINE__,
14311 			  _("read_address: bad switch, signed [in module %s]"),
14312 			  bfd_get_filename (abfd));
14313 	}
14314     }
14315   else
14316     {
14317       switch (cu_header->addr_size)
14318 	{
14319 	case 2:
14320 	  retval = bfd_get_16 (abfd, buf);
14321 	  break;
14322 	case 4:
14323 	  retval = bfd_get_32 (abfd, buf);
14324 	  break;
14325 	case 8:
14326 	  retval = bfd_get_64 (abfd, buf);
14327 	  break;
14328 	default:
14329 	  internal_error (__FILE__, __LINE__,
14330 			  _("read_address: bad switch, "
14331 			    "unsigned [in module %s]"),
14332 			  bfd_get_filename (abfd));
14333 	}
14334     }
14335 
14336   *bytes_read = cu_header->addr_size;
14337   return retval;
14338 }
14339 
14340 /* Read the initial length from a section.  The (draft) DWARF 3
14341    specification allows the initial length to take up either 4 bytes
14342    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
14343    bytes describe the length and all offsets will be 8 bytes in length
14344    instead of 4.
14345 
14346    An older, non-standard 64-bit format is also handled by this
14347    function.  The older format in question stores the initial length
14348    as an 8-byte quantity without an escape value.  Lengths greater
14349    than 2^32 aren't very common which means that the initial 4 bytes
14350    is almost always zero.  Since a length value of zero doesn't make
14351    sense for the 32-bit format, this initial zero can be considered to
14352    be an escape value which indicates the presence of the older 64-bit
14353    format.  As written, the code can't detect (old format) lengths
14354    greater than 4GB.  If it becomes necessary to handle lengths
14355    somewhat larger than 4GB, we could allow other small values (such
14356    as the non-sensical values of 1, 2, and 3) to also be used as
14357    escape values indicating the presence of the old format.
14358 
14359    The value returned via bytes_read should be used to increment the
14360    relevant pointer after calling read_initial_length().
14361 
14362    [ Note:  read_initial_length() and read_offset() are based on the
14363      document entitled "DWARF Debugging Information Format", revision
14364      3, draft 8, dated November 19, 2001.  This document was obtained
14365      from:
14366 
14367 	http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14368 
14369      This document is only a draft and is subject to change.  (So beware.)
14370 
14371      Details regarding the older, non-standard 64-bit format were
14372      determined empirically by examining 64-bit ELF files produced by
14373      the SGI toolchain on an IRIX 6.5 machine.
14374 
14375      - Kevin, July 16, 2002
14376    ] */
14377 
14378 static LONGEST
14379 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14380 {
14381   LONGEST length = bfd_get_32 (abfd, buf);
14382 
14383   if (length == 0xffffffff)
14384     {
14385       length = bfd_get_64 (abfd, buf + 4);
14386       *bytes_read = 12;
14387     }
14388   else if (length == 0)
14389     {
14390       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
14391       length = bfd_get_64 (abfd, buf);
14392       *bytes_read = 8;
14393     }
14394   else
14395     {
14396       *bytes_read = 4;
14397     }
14398 
14399   return length;
14400 }
14401 
14402 /* Cover function for read_initial_length.
14403    Returns the length of the object at BUF, and stores the size of the
14404    initial length in *BYTES_READ and stores the size that offsets will be in
14405    *OFFSET_SIZE.
14406    If the initial length size is not equivalent to that specified in
14407    CU_HEADER then issue a complaint.
14408    This is useful when reading non-comp-unit headers.  */
14409 
14410 static LONGEST
14411 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14412 					const struct comp_unit_head *cu_header,
14413 					unsigned int *bytes_read,
14414 					unsigned int *offset_size)
14415 {
14416   LONGEST length = read_initial_length (abfd, buf, bytes_read);
14417 
14418   gdb_assert (cu_header->initial_length_size == 4
14419 	      || cu_header->initial_length_size == 8
14420 	      || cu_header->initial_length_size == 12);
14421 
14422   if (cu_header->initial_length_size != *bytes_read)
14423     complaint (&symfile_complaints,
14424 	       _("intermixed 32-bit and 64-bit DWARF sections"));
14425 
14426   *offset_size = (*bytes_read == 4) ? 4 : 8;
14427   return length;
14428 }
14429 
14430 /* Read an offset from the data stream.  The size of the offset is
14431    given by cu_header->offset_size.  */
14432 
14433 static LONGEST
14434 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14435              unsigned int *bytes_read)
14436 {
14437   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14438 
14439   *bytes_read = cu_header->offset_size;
14440   return offset;
14441 }
14442 
14443 /* Read an offset from the data stream.  */
14444 
14445 static LONGEST
14446 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14447 {
14448   LONGEST retval = 0;
14449 
14450   switch (offset_size)
14451     {
14452     case 4:
14453       retval = bfd_get_32 (abfd, buf);
14454       break;
14455     case 8:
14456       retval = bfd_get_64 (abfd, buf);
14457       break;
14458     default:
14459       internal_error (__FILE__, __LINE__,
14460 		      _("read_offset_1: bad switch [in module %s]"),
14461 		      bfd_get_filename (abfd));
14462     }
14463 
14464   return retval;
14465 }
14466 
14467 static gdb_byte *
14468 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14469 {
14470   /* If the size of a host char is 8 bits, we can return a pointer
14471      to the buffer, otherwise we have to copy the data to a buffer
14472      allocated on the temporary obstack.  */
14473   gdb_assert (HOST_CHAR_BIT == 8);
14474   return buf;
14475 }
14476 
14477 static char *
14478 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14479 {
14480   /* If the size of a host char is 8 bits, we can return a pointer
14481      to the string, otherwise we have to copy the string to a buffer
14482      allocated on the temporary obstack.  */
14483   gdb_assert (HOST_CHAR_BIT == 8);
14484   if (*buf == '\0')
14485     {
14486       *bytes_read_ptr = 1;
14487       return NULL;
14488     }
14489   *bytes_read_ptr = strlen ((char *) buf) + 1;
14490   return (char *) buf;
14491 }
14492 
14493 static char *
14494 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14495 {
14496   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14497   if (dwarf2_per_objfile->str.buffer == NULL)
14498     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14499 	   bfd_get_filename (abfd));
14500   if (str_offset >= dwarf2_per_objfile->str.size)
14501     error (_("DW_FORM_strp pointing outside of "
14502 	     ".debug_str section [in module %s]"),
14503 	   bfd_get_filename (abfd));
14504   gdb_assert (HOST_CHAR_BIT == 8);
14505   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14506     return NULL;
14507   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14508 }
14509 
14510 /* Read a string at offset STR_OFFSET in the .debug_str section from
14511    the .dwz file DWZ.  Throw an error if the offset is too large.  If
14512    the string consists of a single NUL byte, return NULL; otherwise
14513    return a pointer to the string.  */
14514 
14515 static char *
14516 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14517 {
14518   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14519 
14520   if (dwz->str.buffer == NULL)
14521     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14522 	     "section [in module %s]"),
14523 	   bfd_get_filename (dwz->dwz_bfd));
14524   if (str_offset >= dwz->str.size)
14525     error (_("DW_FORM_GNU_strp_alt pointing outside of "
14526 	     ".debug_str section [in module %s]"),
14527 	   bfd_get_filename (dwz->dwz_bfd));
14528   gdb_assert (HOST_CHAR_BIT == 8);
14529   if (dwz->str.buffer[str_offset] == '\0')
14530     return NULL;
14531   return (char *) (dwz->str.buffer + str_offset);
14532 }
14533 
14534 static char *
14535 read_indirect_string (bfd *abfd, gdb_byte *buf,
14536 		      const struct comp_unit_head *cu_header,
14537 		      unsigned int *bytes_read_ptr)
14538 {
14539   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14540 
14541   return read_indirect_string_at_offset (abfd, str_offset);
14542 }
14543 
14544 static ULONGEST
14545 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14546 {
14547   ULONGEST result;
14548   unsigned int num_read;
14549   int i, shift;
14550   unsigned char byte;
14551 
14552   result = 0;
14553   shift = 0;
14554   num_read = 0;
14555   i = 0;
14556   while (1)
14557     {
14558       byte = bfd_get_8 (abfd, buf);
14559       buf++;
14560       num_read++;
14561       result |= ((ULONGEST) (byte & 127) << shift);
14562       if ((byte & 128) == 0)
14563 	{
14564 	  break;
14565 	}
14566       shift += 7;
14567     }
14568   *bytes_read_ptr = num_read;
14569   return result;
14570 }
14571 
14572 static LONGEST
14573 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14574 {
14575   LONGEST result;
14576   int i, shift, num_read;
14577   unsigned char byte;
14578 
14579   result = 0;
14580   shift = 0;
14581   num_read = 0;
14582   i = 0;
14583   while (1)
14584     {
14585       byte = bfd_get_8 (abfd, buf);
14586       buf++;
14587       num_read++;
14588       result |= ((LONGEST) (byte & 127) << shift);
14589       shift += 7;
14590       if ((byte & 128) == 0)
14591 	{
14592 	  break;
14593 	}
14594     }
14595   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14596     result |= -(((LONGEST) 1) << shift);
14597   *bytes_read_ptr = num_read;
14598   return result;
14599 }
14600 
14601 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14602    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14603    ADDR_SIZE is the size of addresses from the CU header.  */
14604 
14605 static CORE_ADDR
14606 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14607 {
14608   struct objfile *objfile = dwarf2_per_objfile->objfile;
14609   bfd *abfd = objfile->obfd;
14610   const gdb_byte *info_ptr;
14611 
14612   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14613   if (dwarf2_per_objfile->addr.buffer == NULL)
14614     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14615 	   objfile->name);
14616   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14617     error (_("DW_FORM_addr_index pointing outside of "
14618 	     ".debug_addr section [in module %s]"),
14619 	   objfile->name);
14620   info_ptr = (dwarf2_per_objfile->addr.buffer
14621 	      + addr_base + addr_index * addr_size);
14622   if (addr_size == 4)
14623     return bfd_get_32 (abfd, info_ptr);
14624   else
14625     return bfd_get_64 (abfd, info_ptr);
14626 }
14627 
14628 /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */
14629 
14630 static CORE_ADDR
14631 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14632 {
14633   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14634 }
14635 
14636 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
14637 
14638 static CORE_ADDR
14639 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14640 			     unsigned int *bytes_read)
14641 {
14642   bfd *abfd = cu->objfile->obfd;
14643   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14644 
14645   return read_addr_index (cu, addr_index);
14646 }
14647 
14648 /* Data structure to pass results from dwarf2_read_addr_index_reader
14649    back to dwarf2_read_addr_index.  */
14650 
14651 struct dwarf2_read_addr_index_data
14652 {
14653   ULONGEST addr_base;
14654   int addr_size;
14655 };
14656 
14657 /* die_reader_func for dwarf2_read_addr_index.  */
14658 
14659 static void
14660 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14661 			       gdb_byte *info_ptr,
14662 			       struct die_info *comp_unit_die,
14663 			       int has_children,
14664 			       void *data)
14665 {
14666   struct dwarf2_cu *cu = reader->cu;
14667   struct dwarf2_read_addr_index_data *aidata =
14668     (struct dwarf2_read_addr_index_data *) data;
14669 
14670   aidata->addr_base = cu->addr_base;
14671   aidata->addr_size = cu->header.addr_size;
14672 }
14673 
14674 /* Given an index in .debug_addr, fetch the value.
14675    NOTE: This can be called during dwarf expression evaluation,
14676    long after the debug information has been read, and thus per_cu->cu
14677    may no longer exist.  */
14678 
14679 CORE_ADDR
14680 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14681 			unsigned int addr_index)
14682 {
14683   struct objfile *objfile = per_cu->objfile;
14684   struct dwarf2_cu *cu = per_cu->cu;
14685   ULONGEST addr_base;
14686   int addr_size;
14687 
14688   /* This is intended to be called from outside this file.  */
14689   dw2_setup (objfile);
14690 
14691   /* We need addr_base and addr_size.
14692      If we don't have PER_CU->cu, we have to get it.
14693      Nasty, but the alternative is storing the needed info in PER_CU,
14694      which at this point doesn't seem justified: it's not clear how frequently
14695      it would get used and it would increase the size of every PER_CU.
14696      Entry points like dwarf2_per_cu_addr_size do a similar thing
14697      so we're not in uncharted territory here.
14698      Alas we need to be a bit more complicated as addr_base is contained
14699      in the DIE.
14700 
14701      We don't need to read the entire CU(/TU).
14702      We just need the header and top level die.
14703 
14704      IWBN to use the aging mechanism to let us lazily later discard the CU.
14705      For now we skip this optimization.  */
14706 
14707   if (cu != NULL)
14708     {
14709       addr_base = cu->addr_base;
14710       addr_size = cu->header.addr_size;
14711     }
14712   else
14713     {
14714       struct dwarf2_read_addr_index_data aidata;
14715 
14716       /* Note: We can't use init_cutu_and_read_dies_simple here,
14717 	 we need addr_base.  */
14718       init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14719 			       dwarf2_read_addr_index_reader, &aidata);
14720       addr_base = aidata.addr_base;
14721       addr_size = aidata.addr_size;
14722     }
14723 
14724   return read_addr_index_1 (addr_index, addr_base, addr_size);
14725 }
14726 
14727 /* Given a DW_AT_str_index, fetch the string.  */
14728 
14729 static char *
14730 read_str_index (const struct die_reader_specs *reader,
14731 		struct dwarf2_cu *cu, ULONGEST str_index)
14732 {
14733   struct objfile *objfile = dwarf2_per_objfile->objfile;
14734   const char *dwo_name = objfile->name;
14735   bfd *abfd = objfile->obfd;
14736   struct dwo_sections *sections = &reader->dwo_file->sections;
14737   gdb_byte *info_ptr;
14738   ULONGEST str_offset;
14739 
14740   dwarf2_read_section (objfile, &sections->str);
14741   dwarf2_read_section (objfile, &sections->str_offsets);
14742   if (sections->str.buffer == NULL)
14743     error (_("DW_FORM_str_index used without .debug_str.dwo section"
14744 	     " in CU at offset 0x%lx [in module %s]"),
14745 	   (long) cu->header.offset.sect_off, dwo_name);
14746   if (sections->str_offsets.buffer == NULL)
14747     error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14748 	     " in CU at offset 0x%lx [in module %s]"),
14749 	   (long) cu->header.offset.sect_off, dwo_name);
14750   if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14751     error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14752 	     " section in CU at offset 0x%lx [in module %s]"),
14753 	   (long) cu->header.offset.sect_off, dwo_name);
14754   info_ptr = (sections->str_offsets.buffer
14755 	      + str_index * cu->header.offset_size);
14756   if (cu->header.offset_size == 4)
14757     str_offset = bfd_get_32 (abfd, info_ptr);
14758   else
14759     str_offset = bfd_get_64 (abfd, info_ptr);
14760   if (str_offset >= sections->str.size)
14761     error (_("Offset from DW_FORM_str_index pointing outside of"
14762 	     " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14763 	   (long) cu->header.offset.sect_off, dwo_name);
14764   return (char *) (sections->str.buffer + str_offset);
14765 }
14766 
14767 /* Return the length of an LEB128 number in BUF.  */
14768 
14769 static int
14770 leb128_size (const gdb_byte *buf)
14771 {
14772   const gdb_byte *begin = buf;
14773   gdb_byte byte;
14774 
14775   while (1)
14776     {
14777       byte = *buf++;
14778       if ((byte & 128) == 0)
14779 	return buf - begin;
14780     }
14781 }
14782 
14783 static void
14784 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14785 {
14786   switch (lang)
14787     {
14788     case DW_LANG_C89:
14789     case DW_LANG_C99:
14790     case DW_LANG_C:
14791       cu->language = language_c;
14792       break;
14793     case DW_LANG_C_plus_plus:
14794       cu->language = language_cplus;
14795       break;
14796     case DW_LANG_D:
14797       cu->language = language_d;
14798       break;
14799     case DW_LANG_Fortran77:
14800     case DW_LANG_Fortran90:
14801     case DW_LANG_Fortran95:
14802       cu->language = language_fortran;
14803       break;
14804     case DW_LANG_Go:
14805       cu->language = language_go;
14806       break;
14807     case DW_LANG_Mips_Assembler:
14808       cu->language = language_asm;
14809       break;
14810     case DW_LANG_Java:
14811       cu->language = language_java;
14812       break;
14813     case DW_LANG_Ada83:
14814     case DW_LANG_Ada95:
14815       cu->language = language_ada;
14816       break;
14817     case DW_LANG_Modula2:
14818       cu->language = language_m2;
14819       break;
14820     case DW_LANG_Pascal83:
14821       cu->language = language_pascal;
14822       break;
14823     case DW_LANG_ObjC:
14824       cu->language = language_objc;
14825       break;
14826     case DW_LANG_Cobol74:
14827     case DW_LANG_Cobol85:
14828     default:
14829       cu->language = language_minimal;
14830       break;
14831     }
14832   cu->language_defn = language_def (cu->language);
14833 }
14834 
14835 /* Return the named attribute or NULL if not there.  */
14836 
14837 static struct attribute *
14838 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14839 {
14840   for (;;)
14841     {
14842       unsigned int i;
14843       struct attribute *spec = NULL;
14844 
14845       for (i = 0; i < die->num_attrs; ++i)
14846 	{
14847 	  if (die->attrs[i].name == name)
14848 	    return &die->attrs[i];
14849 	  if (die->attrs[i].name == DW_AT_specification
14850 	      || die->attrs[i].name == DW_AT_abstract_origin)
14851 	    spec = &die->attrs[i];
14852 	}
14853 
14854       if (!spec)
14855 	break;
14856 
14857       die = follow_die_ref (die, spec, &cu);
14858     }
14859 
14860   return NULL;
14861 }
14862 
14863 /* Return the named attribute or NULL if not there,
14864    but do not follow DW_AT_specification, etc.
14865    This is for use in contexts where we're reading .debug_types dies.
14866    Following DW_AT_specification, DW_AT_abstract_origin will take us
14867    back up the chain, and we want to go down.  */
14868 
14869 static struct attribute *
14870 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14871 {
14872   unsigned int i;
14873 
14874   for (i = 0; i < die->num_attrs; ++i)
14875     if (die->attrs[i].name == name)
14876       return &die->attrs[i];
14877 
14878   return NULL;
14879 }
14880 
14881 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14882    and holds a non-zero value.  This function should only be used for
14883    DW_FORM_flag or DW_FORM_flag_present attributes.  */
14884 
14885 static int
14886 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14887 {
14888   struct attribute *attr = dwarf2_attr (die, name, cu);
14889 
14890   return (attr && DW_UNSND (attr));
14891 }
14892 
14893 static int
14894 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14895 {
14896   /* A DIE is a declaration if it has a DW_AT_declaration attribute
14897      which value is non-zero.  However, we have to be careful with
14898      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14899      (via dwarf2_flag_true_p) follows this attribute.  So we may
14900      end up accidently finding a declaration attribute that belongs
14901      to a different DIE referenced by the specification attribute,
14902      even though the given DIE does not have a declaration attribute.  */
14903   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14904 	  && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14905 }
14906 
14907 /* Return the die giving the specification for DIE, if there is
14908    one.  *SPEC_CU is the CU containing DIE on input, and the CU
14909    containing the return value on output.  If there is no
14910    specification, but there is an abstract origin, that is
14911    returned.  */
14912 
14913 static struct die_info *
14914 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14915 {
14916   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14917 					     *spec_cu);
14918 
14919   if (spec_attr == NULL)
14920     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14921 
14922   if (spec_attr == NULL)
14923     return NULL;
14924   else
14925     return follow_die_ref (die, spec_attr, spec_cu);
14926 }
14927 
14928 /* Free the line_header structure *LH, and any arrays and strings it
14929    refers to.
14930    NOTE: This is also used as a "cleanup" function.  */
14931 
14932 static void
14933 free_line_header (struct line_header *lh)
14934 {
14935   if (lh->standard_opcode_lengths)
14936     xfree (lh->standard_opcode_lengths);
14937 
14938   /* Remember that all the lh->file_names[i].name pointers are
14939      pointers into debug_line_buffer, and don't need to be freed.  */
14940   if (lh->file_names)
14941     xfree (lh->file_names);
14942 
14943   /* Similarly for the include directory names.  */
14944   if (lh->include_dirs)
14945     xfree (lh->include_dirs);
14946 
14947   xfree (lh);
14948 }
14949 
14950 /* Add an entry to LH's include directory table.  */
14951 
14952 static void
14953 add_include_dir (struct line_header *lh, char *include_dir)
14954 {
14955   /* Grow the array if necessary.  */
14956   if (lh->include_dirs_size == 0)
14957     {
14958       lh->include_dirs_size = 1; /* for testing */
14959       lh->include_dirs = xmalloc (lh->include_dirs_size
14960                                   * sizeof (*lh->include_dirs));
14961     }
14962   else if (lh->num_include_dirs >= lh->include_dirs_size)
14963     {
14964       lh->include_dirs_size *= 2;
14965       lh->include_dirs = xrealloc (lh->include_dirs,
14966                                    (lh->include_dirs_size
14967                                     * sizeof (*lh->include_dirs)));
14968     }
14969 
14970   lh->include_dirs[lh->num_include_dirs++] = include_dir;
14971 }
14972 
14973 /* Add an entry to LH's file name table.  */
14974 
14975 static void
14976 add_file_name (struct line_header *lh,
14977                char *name,
14978                unsigned int dir_index,
14979                unsigned int mod_time,
14980                unsigned int length)
14981 {
14982   struct file_entry *fe;
14983 
14984   /* Grow the array if necessary.  */
14985   if (lh->file_names_size == 0)
14986     {
14987       lh->file_names_size = 1; /* for testing */
14988       lh->file_names = xmalloc (lh->file_names_size
14989                                 * sizeof (*lh->file_names));
14990     }
14991   else if (lh->num_file_names >= lh->file_names_size)
14992     {
14993       lh->file_names_size *= 2;
14994       lh->file_names = xrealloc (lh->file_names,
14995                                  (lh->file_names_size
14996                                   * sizeof (*lh->file_names)));
14997     }
14998 
14999   fe = &lh->file_names[lh->num_file_names++];
15000   fe->name = name;
15001   fe->dir_index = dir_index;
15002   fe->mod_time = mod_time;
15003   fe->length = length;
15004   fe->included_p = 0;
15005   fe->symtab = NULL;
15006 }
15007 
15008 /* A convenience function to find the proper .debug_line section for a
15009    CU.  */
15010 
15011 static struct dwarf2_section_info *
15012 get_debug_line_section (struct dwarf2_cu *cu)
15013 {
15014   struct dwarf2_section_info *section;
15015 
15016   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15017      DWO file.  */
15018   if (cu->dwo_unit && cu->per_cu->is_debug_types)
15019     section = &cu->dwo_unit->dwo_file->sections.line;
15020   else if (cu->per_cu->is_dwz)
15021     {
15022       struct dwz_file *dwz = dwarf2_get_dwz_file ();
15023 
15024       section = &dwz->line;
15025     }
15026   else
15027     section = &dwarf2_per_objfile->line;
15028 
15029   return section;
15030 }
15031 
15032 /* Read the statement program header starting at OFFSET in
15033    .debug_line, or .debug_line.dwo.  Return a pointer
15034    to a struct line_header, allocated using xmalloc.
15035 
15036    NOTE: the strings in the include directory and file name tables of
15037    the returned object point into the dwarf line section buffer,
15038    and must not be freed.  */
15039 
15040 static struct line_header *
15041 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15042 {
15043   struct cleanup *back_to;
15044   struct line_header *lh;
15045   gdb_byte *line_ptr;
15046   unsigned int bytes_read, offset_size;
15047   int i;
15048   char *cur_dir, *cur_file;
15049   struct dwarf2_section_info *section;
15050   bfd *abfd;
15051 
15052   section = get_debug_line_section (cu);
15053   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15054   if (section->buffer == NULL)
15055     {
15056       if (cu->dwo_unit && cu->per_cu->is_debug_types)
15057 	complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15058       else
15059 	complaint (&symfile_complaints, _("missing .debug_line section"));
15060       return 0;
15061     }
15062 
15063   /* We can't do this until we know the section is non-empty.
15064      Only then do we know we have such a section.  */
15065   abfd = section->asection->owner;
15066 
15067   /* Make sure that at least there's room for the total_length field.
15068      That could be 12 bytes long, but we're just going to fudge that.  */
15069   if (offset + 4 >= section->size)
15070     {
15071       dwarf2_statement_list_fits_in_line_number_section_complaint ();
15072       return 0;
15073     }
15074 
15075   lh = xmalloc (sizeof (*lh));
15076   memset (lh, 0, sizeof (*lh));
15077   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15078                           (void *) lh);
15079 
15080   line_ptr = section->buffer + offset;
15081 
15082   /* Read in the header.  */
15083   lh->total_length =
15084     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15085 					    &bytes_read, &offset_size);
15086   line_ptr += bytes_read;
15087   if (line_ptr + lh->total_length > (section->buffer + section->size))
15088     {
15089       dwarf2_statement_list_fits_in_line_number_section_complaint ();
15090       return 0;
15091     }
15092   lh->statement_program_end = line_ptr + lh->total_length;
15093   lh->version = read_2_bytes (abfd, line_ptr);
15094   line_ptr += 2;
15095   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15096   line_ptr += offset_size;
15097   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15098   line_ptr += 1;
15099   if (lh->version >= 4)
15100     {
15101       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15102       line_ptr += 1;
15103     }
15104   else
15105     lh->maximum_ops_per_instruction = 1;
15106 
15107   if (lh->maximum_ops_per_instruction == 0)
15108     {
15109       lh->maximum_ops_per_instruction = 1;
15110       complaint (&symfile_complaints,
15111 		 _("invalid maximum_ops_per_instruction "
15112 		   "in `.debug_line' section"));
15113     }
15114 
15115   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15116   line_ptr += 1;
15117   lh->line_base = read_1_signed_byte (abfd, line_ptr);
15118   line_ptr += 1;
15119   lh->line_range = read_1_byte (abfd, line_ptr);
15120   line_ptr += 1;
15121   lh->opcode_base = read_1_byte (abfd, line_ptr);
15122   line_ptr += 1;
15123   lh->standard_opcode_lengths
15124     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15125 
15126   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
15127   for (i = 1; i < lh->opcode_base; ++i)
15128     {
15129       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15130       line_ptr += 1;
15131     }
15132 
15133   /* Read directory table.  */
15134   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15135     {
15136       line_ptr += bytes_read;
15137       add_include_dir (lh, cur_dir);
15138     }
15139   line_ptr += bytes_read;
15140 
15141   /* Read file name table.  */
15142   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15143     {
15144       unsigned int dir_index, mod_time, length;
15145 
15146       line_ptr += bytes_read;
15147       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15148       line_ptr += bytes_read;
15149       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15150       line_ptr += bytes_read;
15151       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15152       line_ptr += bytes_read;
15153 
15154       add_file_name (lh, cur_file, dir_index, mod_time, length);
15155     }
15156   line_ptr += bytes_read;
15157   lh->statement_program_start = line_ptr;
15158 
15159   if (line_ptr > (section->buffer + section->size))
15160     complaint (&symfile_complaints,
15161 	       _("line number info header doesn't "
15162 		 "fit in `.debug_line' section"));
15163 
15164   discard_cleanups (back_to);
15165   return lh;
15166 }
15167 
15168 /* Subroutine of dwarf_decode_lines to simplify it.
15169    Return the file name of the psymtab for included file FILE_INDEX
15170    in line header LH of PST.
15171    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15172    If space for the result is malloc'd, it will be freed by a cleanup.
15173    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15174 
15175    The function creates dangling cleanup registration.  */
15176 
15177 static char *
15178 psymtab_include_file_name (const struct line_header *lh, int file_index,
15179 			   const struct partial_symtab *pst,
15180 			   const char *comp_dir)
15181 {
15182   const struct file_entry fe = lh->file_names [file_index];
15183   char *include_name = fe.name;
15184   char *include_name_to_compare = include_name;
15185   char *dir_name = NULL;
15186   const char *pst_filename;
15187   char *copied_name = NULL;
15188   int file_is_pst;
15189 
15190   if (fe.dir_index)
15191     dir_name = lh->include_dirs[fe.dir_index - 1];
15192 
15193   if (!IS_ABSOLUTE_PATH (include_name)
15194       && (dir_name != NULL || comp_dir != NULL))
15195     {
15196       /* Avoid creating a duplicate psymtab for PST.
15197 	 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15198 	 Before we do the comparison, however, we need to account
15199 	 for DIR_NAME and COMP_DIR.
15200 	 First prepend dir_name (if non-NULL).  If we still don't
15201 	 have an absolute path prepend comp_dir (if non-NULL).
15202 	 However, the directory we record in the include-file's
15203 	 psymtab does not contain COMP_DIR (to match the
15204 	 corresponding symtab(s)).
15205 
15206 	 Example:
15207 
15208 	 bash$ cd /tmp
15209 	 bash$ gcc -g ./hello.c
15210 	 include_name = "hello.c"
15211 	 dir_name = "."
15212 	 DW_AT_comp_dir = comp_dir = "/tmp"
15213 	 DW_AT_name = "./hello.c"  */
15214 
15215       if (dir_name != NULL)
15216 	{
15217 	  include_name = concat (dir_name, SLASH_STRING,
15218 				 include_name, (char *)NULL);
15219 	  include_name_to_compare = include_name;
15220 	  make_cleanup (xfree, include_name);
15221 	}
15222       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15223 	{
15224 	  include_name_to_compare = concat (comp_dir, SLASH_STRING,
15225 					    include_name, (char *)NULL);
15226 	}
15227     }
15228 
15229   pst_filename = pst->filename;
15230   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15231     {
15232       copied_name = concat (pst->dirname, SLASH_STRING,
15233 			    pst_filename, (char *)NULL);
15234       pst_filename = copied_name;
15235     }
15236 
15237   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15238 
15239   if (include_name_to_compare != include_name)
15240     xfree (include_name_to_compare);
15241   if (copied_name != NULL)
15242     xfree (copied_name);
15243 
15244   if (file_is_pst)
15245     return NULL;
15246   return include_name;
15247 }
15248 
15249 /* Ignore this record_line request.  */
15250 
15251 static void
15252 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15253 {
15254   return;
15255 }
15256 
15257 /* Subroutine of dwarf_decode_lines to simplify it.
15258    Process the line number information in LH.  */
15259 
15260 static void
15261 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15262 		      struct dwarf2_cu *cu, struct partial_symtab *pst)
15263 {
15264   gdb_byte *line_ptr, *extended_end;
15265   gdb_byte *line_end;
15266   unsigned int bytes_read, extended_len;
15267   unsigned char op_code, extended_op, adj_opcode;
15268   CORE_ADDR baseaddr;
15269   struct objfile *objfile = cu->objfile;
15270   bfd *abfd = objfile->obfd;
15271   struct gdbarch *gdbarch = get_objfile_arch (objfile);
15272   const int decode_for_pst_p = (pst != NULL);
15273   struct subfile *last_subfile = NULL;
15274   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15275     = record_line;
15276 
15277   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15278 
15279   line_ptr = lh->statement_program_start;
15280   line_end = lh->statement_program_end;
15281 
15282   /* Read the statement sequences until there's nothing left.  */
15283   while (line_ptr < line_end)
15284     {
15285       /* state machine registers  */
15286       CORE_ADDR address = 0;
15287       unsigned int file = 1;
15288       unsigned int line = 1;
15289       unsigned int column = 0;
15290       int is_stmt = lh->default_is_stmt;
15291       int basic_block = 0;
15292       int end_sequence = 0;
15293       CORE_ADDR addr;
15294       unsigned char op_index = 0;
15295 
15296       if (!decode_for_pst_p && lh->num_file_names >= file)
15297 	{
15298           /* Start a subfile for the current file of the state machine.  */
15299 	  /* lh->include_dirs and lh->file_names are 0-based, but the
15300 	     directory and file name numbers in the statement program
15301 	     are 1-based.  */
15302           struct file_entry *fe = &lh->file_names[file - 1];
15303           char *dir = NULL;
15304 
15305           if (fe->dir_index)
15306             dir = lh->include_dirs[fe->dir_index - 1];
15307 
15308 	  dwarf2_start_subfile (fe->name, dir, comp_dir);
15309 	}
15310 
15311       /* Decode the table.  */
15312       while (!end_sequence)
15313 	{
15314 	  op_code = read_1_byte (abfd, line_ptr);
15315 	  line_ptr += 1;
15316           if (line_ptr > line_end)
15317             {
15318               dwarf2_debug_line_missing_end_sequence_complaint ();
15319               break;
15320             }
15321 
15322 	  if (op_code >= lh->opcode_base)
15323 	    {
15324 	      /* Special operand.  */
15325 	      adj_opcode = op_code - lh->opcode_base;
15326 	      address += (((op_index + (adj_opcode / lh->line_range))
15327 			   / lh->maximum_ops_per_instruction)
15328 			  * lh->minimum_instruction_length);
15329 	      op_index = ((op_index + (adj_opcode / lh->line_range))
15330 			  % lh->maximum_ops_per_instruction);
15331 	      line += lh->line_base + (adj_opcode % lh->line_range);
15332 	      if (lh->num_file_names < file || file == 0)
15333 		dwarf2_debug_line_missing_file_complaint ();
15334 	      /* For now we ignore lines not starting on an
15335 		 instruction boundary.  */
15336 	      else if (op_index == 0)
15337 		{
15338 		  lh->file_names[file - 1].included_p = 1;
15339 		  if (!decode_for_pst_p && is_stmt)
15340 		    {
15341 		      if (last_subfile != current_subfile)
15342 			{
15343 			  addr = gdbarch_addr_bits_remove (gdbarch, address);
15344 			  if (last_subfile)
15345 			    (*p_record_line) (last_subfile, 0, addr);
15346 			  last_subfile = current_subfile;
15347 			}
15348 		      /* Append row to matrix using current values.  */
15349 		      addr = gdbarch_addr_bits_remove (gdbarch, address);
15350 		      (*p_record_line) (current_subfile, line, addr);
15351 		    }
15352 		}
15353 	      basic_block = 0;
15354 	    }
15355 	  else switch (op_code)
15356 	    {
15357 	    case DW_LNS_extended_op:
15358 	      extended_len = read_unsigned_leb128 (abfd, line_ptr,
15359 						   &bytes_read);
15360 	      line_ptr += bytes_read;
15361 	      extended_end = line_ptr + extended_len;
15362 	      extended_op = read_1_byte (abfd, line_ptr);
15363 	      line_ptr += 1;
15364 	      switch (extended_op)
15365 		{
15366 		case DW_LNE_end_sequence:
15367 		  p_record_line = record_line;
15368 		  end_sequence = 1;
15369 		  break;
15370 		case DW_LNE_set_address:
15371 		  address = read_address (abfd, line_ptr, cu, &bytes_read);
15372 
15373 		  if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15374 		    {
15375 		      /* This line table is for a function which has been
15376 			 GCd by the linker.  Ignore it.  PR gdb/12528 */
15377 
15378 		      long line_offset
15379 			= line_ptr - get_debug_line_section (cu)->buffer;
15380 
15381 		      complaint (&symfile_complaints,
15382 				 _(".debug_line address at offset 0x%lx is 0 "
15383 				   "[in module %s]"),
15384 				 line_offset, objfile->name);
15385 		      p_record_line = noop_record_line;
15386 		    }
15387 
15388 		  op_index = 0;
15389 		  line_ptr += bytes_read;
15390 		  address += baseaddr;
15391 		  break;
15392 		case DW_LNE_define_file:
15393                   {
15394                     char *cur_file;
15395                     unsigned int dir_index, mod_time, length;
15396 
15397                     cur_file = read_direct_string (abfd, line_ptr,
15398 						   &bytes_read);
15399                     line_ptr += bytes_read;
15400                     dir_index =
15401                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15402                     line_ptr += bytes_read;
15403                     mod_time =
15404                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15405                     line_ptr += bytes_read;
15406                     length =
15407                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15408                     line_ptr += bytes_read;
15409                     add_file_name (lh, cur_file, dir_index, mod_time, length);
15410                   }
15411 		  break;
15412 		case DW_LNE_set_discriminator:
15413 		  /* The discriminator is not interesting to the debugger;
15414 		     just ignore it.  */
15415 		  line_ptr = extended_end;
15416 		  break;
15417 		default:
15418 		  complaint (&symfile_complaints,
15419 			     _("mangled .debug_line section"));
15420 		  return;
15421 		}
15422 	      /* Make sure that we parsed the extended op correctly.  If e.g.
15423 		 we expected a different address size than the producer used,
15424 		 we may have read the wrong number of bytes.  */
15425 	      if (line_ptr != extended_end)
15426 		{
15427 		  complaint (&symfile_complaints,
15428 			     _("mangled .debug_line section"));
15429 		  return;
15430 		}
15431 	      break;
15432 	    case DW_LNS_copy:
15433 	      if (lh->num_file_names < file || file == 0)
15434 		dwarf2_debug_line_missing_file_complaint ();
15435 	      else
15436 		{
15437 		  lh->file_names[file - 1].included_p = 1;
15438 		  if (!decode_for_pst_p && is_stmt)
15439 		    {
15440 		      if (last_subfile != current_subfile)
15441 			{
15442 			  addr = gdbarch_addr_bits_remove (gdbarch, address);
15443 			  if (last_subfile)
15444 			    (*p_record_line) (last_subfile, 0, addr);
15445 			  last_subfile = current_subfile;
15446 			}
15447 		      addr = gdbarch_addr_bits_remove (gdbarch, address);
15448 		      (*p_record_line) (current_subfile, line, addr);
15449 		    }
15450 		}
15451 	      basic_block = 0;
15452 	      break;
15453 	    case DW_LNS_advance_pc:
15454 	      {
15455 		CORE_ADDR adjust
15456 		  = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15457 
15458 		address += (((op_index + adjust)
15459 			     / lh->maximum_ops_per_instruction)
15460 			    * lh->minimum_instruction_length);
15461 		op_index = ((op_index + adjust)
15462 			    % lh->maximum_ops_per_instruction);
15463 		line_ptr += bytes_read;
15464 	      }
15465 	      break;
15466 	    case DW_LNS_advance_line:
15467 	      line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15468 	      line_ptr += bytes_read;
15469 	      break;
15470 	    case DW_LNS_set_file:
15471               {
15472                 /* The arrays lh->include_dirs and lh->file_names are
15473                    0-based, but the directory and file name numbers in
15474                    the statement program are 1-based.  */
15475                 struct file_entry *fe;
15476                 char *dir = NULL;
15477 
15478                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15479                 line_ptr += bytes_read;
15480                 if (lh->num_file_names < file || file == 0)
15481                   dwarf2_debug_line_missing_file_complaint ();
15482                 else
15483                   {
15484                     fe = &lh->file_names[file - 1];
15485                     if (fe->dir_index)
15486                       dir = lh->include_dirs[fe->dir_index - 1];
15487                     if (!decode_for_pst_p)
15488                       {
15489                         last_subfile = current_subfile;
15490                         dwarf2_start_subfile (fe->name, dir, comp_dir);
15491                       }
15492                   }
15493               }
15494 	      break;
15495 	    case DW_LNS_set_column:
15496 	      column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15497 	      line_ptr += bytes_read;
15498 	      break;
15499 	    case DW_LNS_negate_stmt:
15500 	      is_stmt = (!is_stmt);
15501 	      break;
15502 	    case DW_LNS_set_basic_block:
15503 	      basic_block = 1;
15504 	      break;
15505 	    /* Add to the address register of the state machine the
15506 	       address increment value corresponding to special opcode
15507 	       255.  I.e., this value is scaled by the minimum
15508 	       instruction length since special opcode 255 would have
15509 	       scaled the increment.  */
15510 	    case DW_LNS_const_add_pc:
15511 	      {
15512 		CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15513 
15514 		address += (((op_index + adjust)
15515 			     / lh->maximum_ops_per_instruction)
15516 			    * lh->minimum_instruction_length);
15517 		op_index = ((op_index + adjust)
15518 			    % lh->maximum_ops_per_instruction);
15519 	      }
15520 	      break;
15521 	    case DW_LNS_fixed_advance_pc:
15522 	      address += read_2_bytes (abfd, line_ptr);
15523 	      op_index = 0;
15524 	      line_ptr += 2;
15525 	      break;
15526 	    default:
15527 	      {
15528 		/* Unknown standard opcode, ignore it.  */
15529 		int i;
15530 
15531 		for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15532 		  {
15533 		    (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15534 		    line_ptr += bytes_read;
15535 		  }
15536 	      }
15537 	    }
15538 	}
15539       if (lh->num_file_names < file || file == 0)
15540         dwarf2_debug_line_missing_file_complaint ();
15541       else
15542         {
15543           lh->file_names[file - 1].included_p = 1;
15544           if (!decode_for_pst_p)
15545 	    {
15546 	      addr = gdbarch_addr_bits_remove (gdbarch, address);
15547 	      (*p_record_line) (current_subfile, 0, addr);
15548 	    }
15549         }
15550     }
15551 }
15552 
15553 /* Decode the Line Number Program (LNP) for the given line_header
15554    structure and CU.  The actual information extracted and the type
15555    of structures created from the LNP depends on the value of PST.
15556 
15557    1. If PST is NULL, then this procedure uses the data from the program
15558       to create all necessary symbol tables, and their linetables.
15559 
15560    2. If PST is not NULL, this procedure reads the program to determine
15561       the list of files included by the unit represented by PST, and
15562       builds all the associated partial symbol tables.
15563 
15564    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15565    It is used for relative paths in the line table.
15566    NOTE: When processing partial symtabs (pst != NULL),
15567    comp_dir == pst->dirname.
15568 
15569    NOTE: It is important that psymtabs have the same file name (via strcmp)
15570    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
15571    symtab we don't use it in the name of the psymtabs we create.
15572    E.g. expand_line_sal requires this when finding psymtabs to expand.
15573    A good testcase for this is mb-inline.exp.  */
15574 
15575 static void
15576 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15577 		    struct dwarf2_cu *cu, struct partial_symtab *pst,
15578 		    int want_line_info)
15579 {
15580   struct objfile *objfile = cu->objfile;
15581   const int decode_for_pst_p = (pst != NULL);
15582   struct subfile *first_subfile = current_subfile;
15583 
15584   if (want_line_info)
15585     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15586 
15587   if (decode_for_pst_p)
15588     {
15589       int file_index;
15590 
15591       /* Now that we're done scanning the Line Header Program, we can
15592          create the psymtab of each included file.  */
15593       for (file_index = 0; file_index < lh->num_file_names; file_index++)
15594         if (lh->file_names[file_index].included_p == 1)
15595           {
15596 	    char *include_name =
15597 	      psymtab_include_file_name (lh, file_index, pst, comp_dir);
15598 	    if (include_name != NULL)
15599               dwarf2_create_include_psymtab (include_name, pst, objfile);
15600           }
15601     }
15602   else
15603     {
15604       /* Make sure a symtab is created for every file, even files
15605 	 which contain only variables (i.e. no code with associated
15606 	 line numbers).  */
15607       int i;
15608 
15609       for (i = 0; i < lh->num_file_names; i++)
15610 	{
15611 	  char *dir = NULL;
15612 	  struct file_entry *fe;
15613 
15614 	  fe = &lh->file_names[i];
15615 	  if (fe->dir_index)
15616 	    dir = lh->include_dirs[fe->dir_index - 1];
15617 	  dwarf2_start_subfile (fe->name, dir, comp_dir);
15618 
15619 	  /* Skip the main file; we don't need it, and it must be
15620 	     allocated last, so that it will show up before the
15621 	     non-primary symtabs in the objfile's symtab list.  */
15622 	  if (current_subfile == first_subfile)
15623 	    continue;
15624 
15625 	  if (current_subfile->symtab == NULL)
15626 	    current_subfile->symtab = allocate_symtab (current_subfile->name,
15627 						       objfile);
15628 	  fe->symtab = current_subfile->symtab;
15629 	}
15630     }
15631 }
15632 
15633 /* Start a subfile for DWARF.  FILENAME is the name of the file and
15634    DIRNAME the name of the source directory which contains FILENAME
15635    or NULL if not known.  COMP_DIR is the compilation directory for the
15636    linetable's compilation unit or NULL if not known.
15637    This routine tries to keep line numbers from identical absolute and
15638    relative file names in a common subfile.
15639 
15640    Using the `list' example from the GDB testsuite, which resides in
15641    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15642    of /srcdir/list0.c yields the following debugging information for list0.c:
15643 
15644    DW_AT_name:          /srcdir/list0.c
15645    DW_AT_comp_dir:              /compdir
15646    files.files[0].name: list0.h
15647    files.files[0].dir:  /srcdir
15648    files.files[1].name: list0.c
15649    files.files[1].dir:  /srcdir
15650 
15651    The line number information for list0.c has to end up in a single
15652    subfile, so that `break /srcdir/list0.c:1' works as expected.
15653    start_subfile will ensure that this happens provided that we pass the
15654    concatenation of files.files[1].dir and files.files[1].name as the
15655    subfile's name.  */
15656 
15657 static void
15658 dwarf2_start_subfile (char *filename, const char *dirname,
15659 		      const char *comp_dir)
15660 {
15661   char *fullname;
15662 
15663   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15664      `start_symtab' will always pass the contents of DW_AT_comp_dir as
15665      second argument to start_subfile.  To be consistent, we do the
15666      same here.  In order not to lose the line information directory,
15667      we concatenate it to the filename when it makes sense.
15668      Note that the Dwarf3 standard says (speaking of filenames in line
15669      information): ``The directory index is ignored for file names
15670      that represent full path names''.  Thus ignoring dirname in the
15671      `else' branch below isn't an issue.  */
15672 
15673   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15674     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15675   else
15676     fullname = filename;
15677 
15678   start_subfile (fullname, comp_dir);
15679 
15680   if (fullname != filename)
15681     xfree (fullname);
15682 }
15683 
15684 /* Start a symtab for DWARF.
15685    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */
15686 
15687 static void
15688 dwarf2_start_symtab (struct dwarf2_cu *cu,
15689 		     const char *name, const char *comp_dir, CORE_ADDR low_pc)
15690 {
15691   start_symtab (name, comp_dir, low_pc);
15692   record_debugformat ("DWARF 2");
15693   record_producer (cu->producer);
15694 
15695   /* We assume that we're processing GCC output.  */
15696   processing_gcc_compilation = 2;
15697 
15698   cu->processing_has_namespace_info = 0;
15699 }
15700 
15701 static void
15702 var_decode_location (struct attribute *attr, struct symbol *sym,
15703 		     struct dwarf2_cu *cu)
15704 {
15705   struct objfile *objfile = cu->objfile;
15706   struct comp_unit_head *cu_header = &cu->header;
15707 
15708   /* NOTE drow/2003-01-30: There used to be a comment and some special
15709      code here to turn a symbol with DW_AT_external and a
15710      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
15711      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15712      with some versions of binutils) where shared libraries could have
15713      relocations against symbols in their debug information - the
15714      minimal symbol would have the right address, but the debug info
15715      would not.  It's no longer necessary, because we will explicitly
15716      apply relocations when we read in the debug information now.  */
15717 
15718   /* A DW_AT_location attribute with no contents indicates that a
15719      variable has been optimized away.  */
15720   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15721     {
15722       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15723       return;
15724     }
15725 
15726   /* Handle one degenerate form of location expression specially, to
15727      preserve GDB's previous behavior when section offsets are
15728      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15729      then mark this symbol as LOC_STATIC.  */
15730 
15731   if (attr_form_is_block (attr)
15732       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15733 	   && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15734 	  || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15735 	      && (DW_BLOCK (attr)->size
15736 		  == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15737     {
15738       unsigned int dummy;
15739 
15740       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15741 	SYMBOL_VALUE_ADDRESS (sym) =
15742 	  read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15743       else
15744 	SYMBOL_VALUE_ADDRESS (sym) =
15745 	  read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15746       SYMBOL_CLASS (sym) = LOC_STATIC;
15747       fixup_symbol_section (sym, objfile);
15748       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15749 					      SYMBOL_SECTION (sym));
15750       return;
15751     }
15752 
15753   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15754      expression evaluator, and use LOC_COMPUTED only when necessary
15755      (i.e. when the value of a register or memory location is
15756      referenced, or a thread-local block, etc.).  Then again, it might
15757      not be worthwhile.  I'm assuming that it isn't unless performance
15758      or memory numbers show me otherwise.  */
15759 
15760   dwarf2_symbol_mark_computed (attr, sym, cu);
15761   SYMBOL_CLASS (sym) = LOC_COMPUTED;
15762 
15763   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
15764     cu->has_loclist = 1;
15765 }
15766 
15767 /* Given a pointer to a DWARF information entry, figure out if we need
15768    to make a symbol table entry for it, and if so, create a new entry
15769    and return a pointer to it.
15770    If TYPE is NULL, determine symbol type from the die, otherwise
15771    used the passed type.
15772    If SPACE is not NULL, use it to hold the new symbol.  If it is
15773    NULL, allocate a new symbol on the objfile's obstack.  */
15774 
15775 static struct symbol *
15776 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15777 		 struct symbol *space)
15778 {
15779   struct objfile *objfile = cu->objfile;
15780   struct symbol *sym = NULL;
15781   const char *name;
15782   struct attribute *attr = NULL;
15783   struct attribute *attr2 = NULL;
15784   CORE_ADDR baseaddr;
15785   struct pending **list_to_add = NULL;
15786 
15787   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15788 
15789   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15790 
15791   name = dwarf2_name (die, cu);
15792   if (name)
15793     {
15794       const char *linkagename;
15795       int suppress_add = 0;
15796 
15797       if (space)
15798 	sym = space;
15799       else
15800 	sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15801       OBJSTAT (objfile, n_syms++);
15802 
15803       /* Cache this symbol's name and the name's demangled form (if any).  */
15804       SYMBOL_SET_LANGUAGE (sym, cu->language);
15805       linkagename = dwarf2_physname (name, die, cu);
15806       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15807 
15808       /* Fortran does not have mangling standard and the mangling does differ
15809 	 between gfortran, iFort etc.  */
15810       if (cu->language == language_fortran
15811           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15812 	symbol_set_demangled_name (&(sym->ginfo),
15813 				   dwarf2_full_name (name, die, cu),
15814 	                           NULL);
15815 
15816       /* Default assumptions.
15817          Use the passed type or decode it from the die.  */
15818       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15819       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
15820       if (type != NULL)
15821 	SYMBOL_TYPE (sym) = type;
15822       else
15823 	SYMBOL_TYPE (sym) = die_type (die, cu);
15824       attr = dwarf2_attr (die,
15825 			  inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15826 			  cu);
15827       if (attr)
15828 	{
15829 	  SYMBOL_LINE (sym) = DW_UNSND (attr);
15830 	}
15831 
15832       attr = dwarf2_attr (die,
15833 			  inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15834 			  cu);
15835       if (attr)
15836 	{
15837 	  int file_index = DW_UNSND (attr);
15838 
15839 	  if (cu->line_header == NULL
15840 	      || file_index > cu->line_header->num_file_names)
15841 	    complaint (&symfile_complaints,
15842 		       _("file index out of range"));
15843 	  else if (file_index > 0)
15844 	    {
15845 	      struct file_entry *fe;
15846 
15847 	      fe = &cu->line_header->file_names[file_index - 1];
15848 	      SYMBOL_SYMTAB (sym) = fe->symtab;
15849 	    }
15850 	}
15851 
15852       switch (die->tag)
15853 	{
15854 	case DW_TAG_label:
15855 	  attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15856 	  if (attr)
15857 	    {
15858 	      SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15859 	    }
15860 	  SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15861 	  SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15862 	  SYMBOL_CLASS (sym) = LOC_LABEL;
15863 	  add_symbol_to_list (sym, cu->list_in_scope);
15864 	  break;
15865 	case DW_TAG_subprogram:
15866 	  /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15867 	     finish_block.  */
15868 	  SYMBOL_CLASS (sym) = LOC_BLOCK;
15869 	  attr2 = dwarf2_attr (die, DW_AT_external, cu);
15870 	  if ((attr2 && (DW_UNSND (attr2) != 0))
15871               || cu->language == language_ada)
15872 	    {
15873               /* Subprograms marked external are stored as a global symbol.
15874                  Ada subprograms, whether marked external or not, are always
15875                  stored as a global symbol, because we want to be able to
15876                  access them globally.  For instance, we want to be able
15877                  to break on a nested subprogram without having to
15878                  specify the context.  */
15879 	      list_to_add = &global_symbols;
15880 	    }
15881 	  else
15882 	    {
15883 	      list_to_add = cu->list_in_scope;
15884 	    }
15885 	  break;
15886 	case DW_TAG_inlined_subroutine:
15887 	  /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15888 	     finish_block.  */
15889 	  SYMBOL_CLASS (sym) = LOC_BLOCK;
15890 	  SYMBOL_INLINED (sym) = 1;
15891 	  list_to_add = cu->list_in_scope;
15892 	  break;
15893 	case DW_TAG_template_value_param:
15894 	  suppress_add = 1;
15895 	  /* Fall through.  */
15896 	case DW_TAG_constant:
15897 	case DW_TAG_variable:
15898 	case DW_TAG_member:
15899 	  /* Compilation with minimal debug info may result in
15900 	     variables with missing type entries.  Change the
15901 	     misleading `void' type to something sensible.  */
15902 	  if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15903 	    SYMBOL_TYPE (sym)
15904 	      = objfile_type (objfile)->nodebug_data_symbol;
15905 
15906 	  attr = dwarf2_attr (die, DW_AT_const_value, cu);
15907 	  /* In the case of DW_TAG_member, we should only be called for
15908 	     static const members.  */
15909 	  if (die->tag == DW_TAG_member)
15910 	    {
15911 	      /* dwarf2_add_field uses die_is_declaration,
15912 		 so we do the same.  */
15913 	      gdb_assert (die_is_declaration (die, cu));
15914 	      gdb_assert (attr);
15915 	    }
15916 	  if (attr)
15917 	    {
15918 	      dwarf2_const_value (attr, sym, cu);
15919 	      attr2 = dwarf2_attr (die, DW_AT_external, cu);
15920 	      if (!suppress_add)
15921 		{
15922 		  if (attr2 && (DW_UNSND (attr2) != 0))
15923 		    list_to_add = &global_symbols;
15924 		  else
15925 		    list_to_add = cu->list_in_scope;
15926 		}
15927 	      break;
15928 	    }
15929 	  attr = dwarf2_attr (die, DW_AT_location, cu);
15930 	  if (attr)
15931 	    {
15932 	      var_decode_location (attr, sym, cu);
15933 	      attr2 = dwarf2_attr (die, DW_AT_external, cu);
15934 
15935 	      /* Fortran explicitly imports any global symbols to the local
15936 		 scope by DW_TAG_common_block.  */
15937 	      if (cu->language == language_fortran && die->parent
15938 		  && die->parent->tag == DW_TAG_common_block)
15939 		attr2 = NULL;
15940 
15941 	      if (SYMBOL_CLASS (sym) == LOC_STATIC
15942 		  && SYMBOL_VALUE_ADDRESS (sym) == 0
15943 		  && !dwarf2_per_objfile->has_section_at_zero)
15944 		{
15945 		  /* When a static variable is eliminated by the linker,
15946 		     the corresponding debug information is not stripped
15947 		     out, but the variable address is set to null;
15948 		     do not add such variables into symbol table.  */
15949 		}
15950 	      else if (attr2 && (DW_UNSND (attr2) != 0))
15951 		{
15952 		  /* Workaround gfortran PR debug/40040 - it uses
15953 		     DW_AT_location for variables in -fPIC libraries which may
15954 		     get overriden by other libraries/executable and get
15955 		     a different address.  Resolve it by the minimal symbol
15956 		     which may come from inferior's executable using copy
15957 		     relocation.  Make this workaround only for gfortran as for
15958 		     other compilers GDB cannot guess the minimal symbol
15959 		     Fortran mangling kind.  */
15960 		  if (cu->language == language_fortran && die->parent
15961 		      && die->parent->tag == DW_TAG_module
15962 		      && cu->producer
15963 		      && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15964 		    SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15965 
15966 		  /* A variable with DW_AT_external is never static,
15967 		     but it may be block-scoped.  */
15968 		  list_to_add = (cu->list_in_scope == &file_symbols
15969 				 ? &global_symbols : cu->list_in_scope);
15970 		}
15971 	      else
15972 		list_to_add = cu->list_in_scope;
15973 	    }
15974 	  else
15975 	    {
15976 	      /* We do not know the address of this symbol.
15977 	         If it is an external symbol and we have type information
15978 	         for it, enter the symbol as a LOC_UNRESOLVED symbol.
15979 	         The address of the variable will then be determined from
15980 	         the minimal symbol table whenever the variable is
15981 	         referenced.  */
15982 	      attr2 = dwarf2_attr (die, DW_AT_external, cu);
15983 
15984 	      /* Fortran explicitly imports any global symbols to the local
15985 		 scope by DW_TAG_common_block.  */
15986 	      if (cu->language == language_fortran && die->parent
15987 		  && die->parent->tag == DW_TAG_common_block)
15988 		{
15989 		  /* SYMBOL_CLASS doesn't matter here because
15990 		     read_common_block is going to reset it.  */
15991 		  if (!suppress_add)
15992 		    list_to_add = cu->list_in_scope;
15993 		}
15994 	      else if (attr2 && (DW_UNSND (attr2) != 0)
15995 		       && dwarf2_attr (die, DW_AT_type, cu) != NULL)
15996 		{
15997 		  /* A variable with DW_AT_external is never static, but it
15998 		     may be block-scoped.  */
15999 		  list_to_add = (cu->list_in_scope == &file_symbols
16000 				 ? &global_symbols : cu->list_in_scope);
16001 
16002 		  SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
16003 		}
16004 	      else if (!die_is_declaration (die, cu))
16005 		{
16006 		  /* Use the default LOC_OPTIMIZED_OUT class.  */
16007 		  gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16008 		  if (!suppress_add)
16009 		    list_to_add = cu->list_in_scope;
16010 		}
16011 	    }
16012 	  break;
16013 	case DW_TAG_formal_parameter:
16014 	  /* If we are inside a function, mark this as an argument.  If
16015 	     not, we might be looking at an argument to an inlined function
16016 	     when we do not have enough information to show inlined frames;
16017 	     pretend it's a local variable in that case so that the user can
16018 	     still see it.  */
16019 	  if (context_stack_depth > 0
16020 	      && context_stack[context_stack_depth - 1].name != NULL)
16021 	    SYMBOL_IS_ARGUMENT (sym) = 1;
16022 	  attr = dwarf2_attr (die, DW_AT_location, cu);
16023 	  if (attr)
16024 	    {
16025 	      var_decode_location (attr, sym, cu);
16026 	    }
16027 	  attr = dwarf2_attr (die, DW_AT_const_value, cu);
16028 	  if (attr)
16029 	    {
16030 	      dwarf2_const_value (attr, sym, cu);
16031 	    }
16032 
16033 	  list_to_add = cu->list_in_scope;
16034 	  break;
16035 	case DW_TAG_unspecified_parameters:
16036 	  /* From varargs functions; gdb doesn't seem to have any
16037 	     interest in this information, so just ignore it for now.
16038 	     (FIXME?) */
16039 	  break;
16040 	case DW_TAG_template_type_param:
16041 	  suppress_add = 1;
16042 	  /* Fall through.  */
16043 	case DW_TAG_class_type:
16044 	case DW_TAG_interface_type:
16045 	case DW_TAG_structure_type:
16046 	case DW_TAG_union_type:
16047 	case DW_TAG_set_type:
16048 	case DW_TAG_enumeration_type:
16049 	  SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16050 	  SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16051 
16052 	  {
16053 	    /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16054 	       really ever be static objects: otherwise, if you try
16055 	       to, say, break of a class's method and you're in a file
16056 	       which doesn't mention that class, it won't work unless
16057 	       the check for all static symbols in lookup_symbol_aux
16058 	       saves you.  See the OtherFileClass tests in
16059 	       gdb.c++/namespace.exp.  */
16060 
16061 	    if (!suppress_add)
16062 	      {
16063 		list_to_add = (cu->list_in_scope == &file_symbols
16064 			       && (cu->language == language_cplus
16065 				   || cu->language == language_java)
16066 			       ? &global_symbols : cu->list_in_scope);
16067 
16068 		/* The semantics of C++ state that "struct foo {
16069 		   ... }" also defines a typedef for "foo".  A Java
16070 		   class declaration also defines a typedef for the
16071 		   class.  */
16072 		if (cu->language == language_cplus
16073 		    || cu->language == language_java
16074 		    || cu->language == language_ada)
16075 		  {
16076 		    /* The symbol's name is already allocated along
16077 		       with this objfile, so we don't need to
16078 		       duplicate it for the type.  */
16079 		    if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16080 		      TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16081 		  }
16082 	      }
16083 	  }
16084 	  break;
16085 	case DW_TAG_typedef:
16086 	  SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16087 	  SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16088 	  list_to_add = cu->list_in_scope;
16089 	  break;
16090 	case DW_TAG_base_type:
16091         case DW_TAG_subrange_type:
16092 	  SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16093 	  SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16094 	  list_to_add = cu->list_in_scope;
16095 	  break;
16096 	case DW_TAG_enumerator:
16097 	  attr = dwarf2_attr (die, DW_AT_const_value, cu);
16098 	  if (attr)
16099 	    {
16100 	      dwarf2_const_value (attr, sym, cu);
16101 	    }
16102 	  {
16103 	    /* NOTE: carlton/2003-11-10: See comment above in the
16104 	       DW_TAG_class_type, etc. block.  */
16105 
16106 	    list_to_add = (cu->list_in_scope == &file_symbols
16107 			   && (cu->language == language_cplus
16108 			       || cu->language == language_java)
16109 			   ? &global_symbols : cu->list_in_scope);
16110 	  }
16111 	  break;
16112 	case DW_TAG_namespace:
16113 	  SYMBOL_CLASS (sym) = LOC_TYPEDEF;
16114 	  list_to_add = &global_symbols;
16115 	  break;
16116 	case DW_TAG_common_block:
16117 	  SYMBOL_CLASS (sym) = LOC_COMMON_BLOCK;
16118 	  SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16119 	  add_symbol_to_list (sym, cu->list_in_scope);
16120 	  break;
16121 	default:
16122 	  /* Not a tag we recognize.  Hopefully we aren't processing
16123 	     trash data, but since we must specifically ignore things
16124 	     we don't recognize, there is nothing else we should do at
16125 	     this point.  */
16126 	  complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16127 		     dwarf_tag_name (die->tag));
16128 	  break;
16129 	}
16130 
16131       if (suppress_add)
16132 	{
16133 	  sym->hash_next = objfile->template_symbols;
16134 	  objfile->template_symbols = sym;
16135 	  list_to_add = NULL;
16136 	}
16137 
16138       if (list_to_add != NULL)
16139 	add_symbol_to_list (sym, list_to_add);
16140 
16141       /* For the benefit of old versions of GCC, check for anonymous
16142 	 namespaces based on the demangled name.  */
16143       if (!cu->processing_has_namespace_info
16144 	  && cu->language == language_cplus)
16145 	cp_scan_for_anonymous_namespaces (sym, objfile);
16146     }
16147   return (sym);
16148 }
16149 
16150 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
16151 
16152 static struct symbol *
16153 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16154 {
16155   return new_symbol_full (die, type, cu, NULL);
16156 }
16157 
16158 /* Given an attr with a DW_FORM_dataN value in host byte order,
16159    zero-extend it as appropriate for the symbol's type.  The DWARF
16160    standard (v4) is not entirely clear about the meaning of using
16161    DW_FORM_dataN for a constant with a signed type, where the type is
16162    wider than the data.  The conclusion of a discussion on the DWARF
16163    list was that this is unspecified.  We choose to always zero-extend
16164    because that is the interpretation long in use by GCC.  */
16165 
16166 static gdb_byte *
16167 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16168 			 const char *name, struct obstack *obstack,
16169 			 struct dwarf2_cu *cu, LONGEST *value, int bits)
16170 {
16171   struct objfile *objfile = cu->objfile;
16172   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16173 				BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16174   LONGEST l = DW_UNSND (attr);
16175 
16176   if (bits < sizeof (*value) * 8)
16177     {
16178       l &= ((LONGEST) 1 << bits) - 1;
16179       *value = l;
16180     }
16181   else if (bits == sizeof (*value) * 8)
16182     *value = l;
16183   else
16184     {
16185       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16186       store_unsigned_integer (bytes, bits / 8, byte_order, l);
16187       return bytes;
16188     }
16189 
16190   return NULL;
16191 }
16192 
16193 /* Read a constant value from an attribute.  Either set *VALUE, or if
16194    the value does not fit in *VALUE, set *BYTES - either already
16195    allocated on the objfile obstack, or newly allocated on OBSTACK,
16196    or, set *BATON, if we translated the constant to a location
16197    expression.  */
16198 
16199 static void
16200 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16201 			 const char *name, struct obstack *obstack,
16202 			 struct dwarf2_cu *cu,
16203 			 LONGEST *value, gdb_byte **bytes,
16204 			 struct dwarf2_locexpr_baton **baton)
16205 {
16206   struct objfile *objfile = cu->objfile;
16207   struct comp_unit_head *cu_header = &cu->header;
16208   struct dwarf_block *blk;
16209   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16210 				BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16211 
16212   *value = 0;
16213   *bytes = NULL;
16214   *baton = NULL;
16215 
16216   switch (attr->form)
16217     {
16218     case DW_FORM_addr:
16219     case DW_FORM_GNU_addr_index:
16220       {
16221 	gdb_byte *data;
16222 
16223 	if (TYPE_LENGTH (type) != cu_header->addr_size)
16224 	  dwarf2_const_value_length_mismatch_complaint (name,
16225 							cu_header->addr_size,
16226 							TYPE_LENGTH (type));
16227 	/* Symbols of this form are reasonably rare, so we just
16228 	   piggyback on the existing location code rather than writing
16229 	   a new implementation of symbol_computed_ops.  */
16230 	*baton = obstack_alloc (&objfile->objfile_obstack,
16231 				sizeof (struct dwarf2_locexpr_baton));
16232 	(*baton)->per_cu = cu->per_cu;
16233 	gdb_assert ((*baton)->per_cu);
16234 
16235 	(*baton)->size = 2 + cu_header->addr_size;
16236 	data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16237 	(*baton)->data = data;
16238 
16239 	data[0] = DW_OP_addr;
16240 	store_unsigned_integer (&data[1], cu_header->addr_size,
16241 				byte_order, DW_ADDR (attr));
16242 	data[cu_header->addr_size + 1] = DW_OP_stack_value;
16243       }
16244       break;
16245     case DW_FORM_string:
16246     case DW_FORM_strp:
16247     case DW_FORM_GNU_str_index:
16248     case DW_FORM_GNU_strp_alt:
16249       /* DW_STRING is already allocated on the objfile obstack, point
16250 	 directly to it.  */
16251       *bytes = (gdb_byte *) DW_STRING (attr);
16252       break;
16253     case DW_FORM_block1:
16254     case DW_FORM_block2:
16255     case DW_FORM_block4:
16256     case DW_FORM_block:
16257     case DW_FORM_exprloc:
16258       blk = DW_BLOCK (attr);
16259       if (TYPE_LENGTH (type) != blk->size)
16260 	dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16261 						      TYPE_LENGTH (type));
16262       *bytes = blk->data;
16263       break;
16264 
16265       /* The DW_AT_const_value attributes are supposed to carry the
16266 	 symbol's value "represented as it would be on the target
16267 	 architecture."  By the time we get here, it's already been
16268 	 converted to host endianness, so we just need to sign- or
16269 	 zero-extend it as appropriate.  */
16270     case DW_FORM_data1:
16271       *bytes = dwarf2_const_value_data (attr, type, name,
16272 					obstack, cu, value, 8);
16273       break;
16274     case DW_FORM_data2:
16275       *bytes = dwarf2_const_value_data (attr, type, name,
16276 					obstack, cu, value, 16);
16277       break;
16278     case DW_FORM_data4:
16279       *bytes = dwarf2_const_value_data (attr, type, name,
16280 					obstack, cu, value, 32);
16281       break;
16282     case DW_FORM_data8:
16283       *bytes = dwarf2_const_value_data (attr, type, name,
16284 					obstack, cu, value, 64);
16285       break;
16286 
16287     case DW_FORM_sdata:
16288       *value = DW_SND (attr);
16289       break;
16290 
16291     case DW_FORM_udata:
16292       *value = DW_UNSND (attr);
16293       break;
16294 
16295     default:
16296       complaint (&symfile_complaints,
16297 		 _("unsupported const value attribute form: '%s'"),
16298 		 dwarf_form_name (attr->form));
16299       *value = 0;
16300       break;
16301     }
16302 }
16303 
16304 
16305 /* Copy constant value from an attribute to a symbol.  */
16306 
16307 static void
16308 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16309 		    struct dwarf2_cu *cu)
16310 {
16311   struct objfile *objfile = cu->objfile;
16312   struct comp_unit_head *cu_header = &cu->header;
16313   LONGEST value;
16314   gdb_byte *bytes;
16315   struct dwarf2_locexpr_baton *baton;
16316 
16317   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16318 			   SYMBOL_PRINT_NAME (sym),
16319 			   &objfile->objfile_obstack, cu,
16320 			   &value, &bytes, &baton);
16321 
16322   if (baton != NULL)
16323     {
16324       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
16325       SYMBOL_LOCATION_BATON (sym) = baton;
16326       SYMBOL_CLASS (sym) = LOC_COMPUTED;
16327     }
16328   else if (bytes != NULL)
16329      {
16330       SYMBOL_VALUE_BYTES (sym) = bytes;
16331       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
16332     }
16333   else
16334     {
16335       SYMBOL_VALUE (sym) = value;
16336       SYMBOL_CLASS (sym) = LOC_CONST;
16337     }
16338 }
16339 
16340 /* Return the type of the die in question using its DW_AT_type attribute.  */
16341 
16342 static struct type *
16343 die_type (struct die_info *die, struct dwarf2_cu *cu)
16344 {
16345   struct attribute *type_attr;
16346 
16347   type_attr = dwarf2_attr (die, DW_AT_type, cu);
16348   if (!type_attr)
16349     {
16350       /* A missing DW_AT_type represents a void type.  */
16351       return objfile_type (cu->objfile)->builtin_void;
16352     }
16353 
16354   return lookup_die_type (die, type_attr, cu);
16355 }
16356 
16357 /* True iff CU's producer generates GNAT Ada auxiliary information
16358    that allows to find parallel types through that information instead
16359    of having to do expensive parallel lookups by type name.  */
16360 
16361 static int
16362 need_gnat_info (struct dwarf2_cu *cu)
16363 {
16364   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16365      of GNAT produces this auxiliary information, without any indication
16366      that it is produced.  Part of enhancing the FSF version of GNAT
16367      to produce that information will be to put in place an indicator
16368      that we can use in order to determine whether the descriptive type
16369      info is available or not.  One suggestion that has been made is
16370      to use a new attribute, attached to the CU die.  For now, assume
16371      that the descriptive type info is not available.  */
16372   return 0;
16373 }
16374 
16375 /* Return the auxiliary type of the die in question using its
16376    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
16377    attribute is not present.  */
16378 
16379 static struct type *
16380 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16381 {
16382   struct attribute *type_attr;
16383 
16384   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16385   if (!type_attr)
16386     return NULL;
16387 
16388   return lookup_die_type (die, type_attr, cu);
16389 }
16390 
16391 /* If DIE has a descriptive_type attribute, then set the TYPE's
16392    descriptive type accordingly.  */
16393 
16394 static void
16395 set_descriptive_type (struct type *type, struct die_info *die,
16396 		      struct dwarf2_cu *cu)
16397 {
16398   struct type *descriptive_type = die_descriptive_type (die, cu);
16399 
16400   if (descriptive_type)
16401     {
16402       ALLOCATE_GNAT_AUX_TYPE (type);
16403       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16404     }
16405 }
16406 
16407 /* Return the containing type of the die in question using its
16408    DW_AT_containing_type attribute.  */
16409 
16410 static struct type *
16411 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16412 {
16413   struct attribute *type_attr;
16414 
16415   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16416   if (!type_attr)
16417     error (_("Dwarf Error: Problem turning containing type into gdb type "
16418 	     "[in module %s]"), cu->objfile->name);
16419 
16420   return lookup_die_type (die, type_attr, cu);
16421 }
16422 
16423 /* Look up the type of DIE in CU using its type attribute ATTR.
16424    If there is no type substitute an error marker.  */
16425 
16426 static struct type *
16427 lookup_die_type (struct die_info *die, struct attribute *attr,
16428 		 struct dwarf2_cu *cu)
16429 {
16430   struct objfile *objfile = cu->objfile;
16431   struct type *this_type;
16432 
16433   /* First see if we have it cached.  */
16434 
16435   if (attr->form == DW_FORM_GNU_ref_alt)
16436     {
16437       struct dwarf2_per_cu_data *per_cu;
16438       sect_offset offset = dwarf2_get_ref_die_offset (attr);
16439 
16440       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16441       this_type = get_die_type_at_offset (offset, per_cu);
16442     }
16443   else if (is_ref_attr (attr))
16444     {
16445       sect_offset offset = dwarf2_get_ref_die_offset (attr);
16446 
16447       this_type = get_die_type_at_offset (offset, cu->per_cu);
16448     }
16449   else if (attr->form == DW_FORM_ref_sig8)
16450     {
16451       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16452 
16453       /* sig_type will be NULL if the signatured type is missing from
16454 	 the debug info.  */
16455       if (sig_type == NULL)
16456 	error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16457 		 "at 0x%x [in module %s]"),
16458 	       die->offset.sect_off, objfile->name);
16459 
16460       gdb_assert (sig_type->per_cu.is_debug_types);
16461       /* If we haven't filled in type_offset_in_section yet, then we
16462 	 haven't read the type in yet.  */
16463       this_type = NULL;
16464       if (sig_type->type_offset_in_section.sect_off != 0)
16465 	{
16466 	  this_type =
16467 	    get_die_type_at_offset (sig_type->type_offset_in_section,
16468 				    &sig_type->per_cu);
16469 	}
16470     }
16471   else
16472     {
16473       dump_die_for_error (die);
16474       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16475 	     dwarf_attr_name (attr->name), objfile->name);
16476     }
16477 
16478   /* If not cached we need to read it in.  */
16479 
16480   if (this_type == NULL)
16481     {
16482       struct die_info *type_die;
16483       struct dwarf2_cu *type_cu = cu;
16484 
16485       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16486       /* If we found the type now, it's probably because the type came
16487 	 from an inter-CU reference and the type's CU got expanded before
16488 	 ours.  */
16489       this_type = get_die_type (type_die, type_cu);
16490       if (this_type == NULL)
16491 	this_type = read_type_die_1 (type_die, type_cu);
16492     }
16493 
16494   /* If we still don't have a type use an error marker.  */
16495 
16496   if (this_type == NULL)
16497     {
16498       char *message, *saved;
16499 
16500       /* read_type_die already issued a complaint.  */
16501       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16502 			    objfile->name,
16503 			    cu->header.offset.sect_off,
16504 			    die->offset.sect_off);
16505       saved = obstack_copy0 (&objfile->objfile_obstack,
16506 			     message, strlen (message));
16507       xfree (message);
16508 
16509       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16510     }
16511 
16512   return this_type;
16513 }
16514 
16515 /* Return the type in DIE, CU.
16516    Returns NULL for invalid types.
16517 
16518    This first does a lookup in the appropriate type_hash table,
16519    and only reads the die in if necessary.
16520 
16521    NOTE: This can be called when reading in partial or full symbols.  */
16522 
16523 static struct type *
16524 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16525 {
16526   struct type *this_type;
16527 
16528   this_type = get_die_type (die, cu);
16529   if (this_type)
16530     return this_type;
16531 
16532   return read_type_die_1 (die, cu);
16533 }
16534 
16535 /* Read the type in DIE, CU.
16536    Returns NULL for invalid types.  */
16537 
16538 static struct type *
16539 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16540 {
16541   struct type *this_type = NULL;
16542 
16543   switch (die->tag)
16544     {
16545     case DW_TAG_class_type:
16546     case DW_TAG_interface_type:
16547     case DW_TAG_structure_type:
16548     case DW_TAG_union_type:
16549       this_type = read_structure_type (die, cu);
16550       break;
16551     case DW_TAG_enumeration_type:
16552       this_type = read_enumeration_type (die, cu);
16553       break;
16554     case DW_TAG_subprogram:
16555     case DW_TAG_subroutine_type:
16556     case DW_TAG_inlined_subroutine:
16557       this_type = read_subroutine_type (die, cu);
16558       break;
16559     case DW_TAG_array_type:
16560       this_type = read_array_type (die, cu);
16561       break;
16562     case DW_TAG_set_type:
16563       this_type = read_set_type (die, cu);
16564       break;
16565     case DW_TAG_pointer_type:
16566       this_type = read_tag_pointer_type (die, cu);
16567       break;
16568     case DW_TAG_ptr_to_member_type:
16569       this_type = read_tag_ptr_to_member_type (die, cu);
16570       break;
16571     case DW_TAG_reference_type:
16572       this_type = read_tag_reference_type (die, cu);
16573       break;
16574     case DW_TAG_const_type:
16575       this_type = read_tag_const_type (die, cu);
16576       break;
16577     case DW_TAG_volatile_type:
16578       this_type = read_tag_volatile_type (die, cu);
16579       break;
16580     case DW_TAG_restrict_type:
16581       this_type = read_tag_restrict_type (die, cu);
16582       break;
16583     case DW_TAG_string_type:
16584       this_type = read_tag_string_type (die, cu);
16585       break;
16586     case DW_TAG_typedef:
16587       this_type = read_typedef (die, cu);
16588       break;
16589     case DW_TAG_subrange_type:
16590       this_type = read_subrange_type (die, cu);
16591       break;
16592     case DW_TAG_base_type:
16593       this_type = read_base_type (die, cu);
16594       break;
16595     case DW_TAG_unspecified_type:
16596       this_type = read_unspecified_type (die, cu);
16597       break;
16598     case DW_TAG_namespace:
16599       this_type = read_namespace_type (die, cu);
16600       break;
16601     case DW_TAG_module:
16602       this_type = read_module_type (die, cu);
16603       break;
16604     default:
16605       complaint (&symfile_complaints,
16606 		 _("unexpected tag in read_type_die: '%s'"),
16607 		 dwarf_tag_name (die->tag));
16608       break;
16609     }
16610 
16611   return this_type;
16612 }
16613 
16614 /* See if we can figure out if the class lives in a namespace.  We do
16615    this by looking for a member function; its demangled name will
16616    contain namespace info, if there is any.
16617    Return the computed name or NULL.
16618    Space for the result is allocated on the objfile's obstack.
16619    This is the full-die version of guess_partial_die_structure_name.
16620    In this case we know DIE has no useful parent.  */
16621 
16622 static char *
16623 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16624 {
16625   struct die_info *spec_die;
16626   struct dwarf2_cu *spec_cu;
16627   struct die_info *child;
16628 
16629   spec_cu = cu;
16630   spec_die = die_specification (die, &spec_cu);
16631   if (spec_die != NULL)
16632     {
16633       die = spec_die;
16634       cu = spec_cu;
16635     }
16636 
16637   for (child = die->child;
16638        child != NULL;
16639        child = child->sibling)
16640     {
16641       if (child->tag == DW_TAG_subprogram)
16642 	{
16643 	  struct attribute *attr;
16644 
16645 	  attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16646 	  if (attr == NULL)
16647 	    attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16648 	  if (attr != NULL)
16649 	    {
16650 	      char *actual_name
16651 		= language_class_name_from_physname (cu->language_defn,
16652 						     DW_STRING (attr));
16653 	      char *name = NULL;
16654 
16655 	      if (actual_name != NULL)
16656 		{
16657 		  const char *die_name = dwarf2_name (die, cu);
16658 
16659 		  if (die_name != NULL
16660 		      && strcmp (die_name, actual_name) != 0)
16661 		    {
16662 		      /* Strip off the class name from the full name.
16663 			 We want the prefix.  */
16664 		      int die_name_len = strlen (die_name);
16665 		      int actual_name_len = strlen (actual_name);
16666 
16667 		      /* Test for '::' as a sanity check.  */
16668 		      if (actual_name_len > die_name_len + 2
16669 			  && actual_name[actual_name_len
16670 					 - die_name_len - 1] == ':')
16671 			name =
16672 			  obstack_copy0 (&cu->objfile->objfile_obstack,
16673 					 actual_name,
16674 					 actual_name_len - die_name_len - 2);
16675 		    }
16676 		}
16677 	      xfree (actual_name);
16678 	      return name;
16679 	    }
16680 	}
16681     }
16682 
16683   return NULL;
16684 }
16685 
16686 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
16687    prefix part in such case.  See
16688    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
16689 
16690 static char *
16691 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16692 {
16693   struct attribute *attr;
16694   char *base;
16695 
16696   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16697       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16698     return NULL;
16699 
16700   attr = dwarf2_attr (die, DW_AT_name, cu);
16701   if (attr != NULL && DW_STRING (attr) != NULL)
16702     return NULL;
16703 
16704   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16705   if (attr == NULL)
16706     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16707   if (attr == NULL || DW_STRING (attr) == NULL)
16708     return NULL;
16709 
16710   /* dwarf2_name had to be already called.  */
16711   gdb_assert (DW_STRING_IS_CANONICAL (attr));
16712 
16713   /* Strip the base name, keep any leading namespaces/classes.  */
16714   base = strrchr (DW_STRING (attr), ':');
16715   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16716     return "";
16717 
16718   return obstack_copy0 (&cu->objfile->objfile_obstack,
16719 			DW_STRING (attr), &base[-1] - DW_STRING (attr));
16720 }
16721 
16722 /* Return the name of the namespace/class that DIE is defined within,
16723    or "" if we can't tell.  The caller should not xfree the result.
16724 
16725    For example, if we're within the method foo() in the following
16726    code:
16727 
16728    namespace N {
16729      class C {
16730        void foo () {
16731        }
16732      };
16733    }
16734 
16735    then determine_prefix on foo's die will return "N::C".  */
16736 
16737 static const char *
16738 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16739 {
16740   struct die_info *parent, *spec_die;
16741   struct dwarf2_cu *spec_cu;
16742   struct type *parent_type;
16743   char *retval;
16744 
16745   if (cu->language != language_cplus && cu->language != language_java
16746       && cu->language != language_fortran)
16747     return "";
16748 
16749   retval = anonymous_struct_prefix (die, cu);
16750   if (retval)
16751     return retval;
16752 
16753   /* We have to be careful in the presence of DW_AT_specification.
16754      For example, with GCC 3.4, given the code
16755 
16756      namespace N {
16757        void foo() {
16758 	 // Definition of N::foo.
16759        }
16760      }
16761 
16762      then we'll have a tree of DIEs like this:
16763 
16764      1: DW_TAG_compile_unit
16765        2: DW_TAG_namespace        // N
16766 	 3: DW_TAG_subprogram     // declaration of N::foo
16767        4: DW_TAG_subprogram       // definition of N::foo
16768 	    DW_AT_specification   // refers to die #3
16769 
16770      Thus, when processing die #4, we have to pretend that we're in
16771      the context of its DW_AT_specification, namely the contex of die
16772      #3.  */
16773   spec_cu = cu;
16774   spec_die = die_specification (die, &spec_cu);
16775   if (spec_die == NULL)
16776     parent = die->parent;
16777   else
16778     {
16779       parent = spec_die->parent;
16780       cu = spec_cu;
16781     }
16782 
16783   if (parent == NULL)
16784     return "";
16785   else if (parent->building_fullname)
16786     {
16787       const char *name;
16788       const char *parent_name;
16789 
16790       /* It has been seen on RealView 2.2 built binaries,
16791 	 DW_TAG_template_type_param types actually _defined_ as
16792 	 children of the parent class:
16793 
16794 	 enum E {};
16795 	 template class <class Enum> Class{};
16796 	 Class<enum E> class_e;
16797 
16798          1: DW_TAG_class_type (Class)
16799            2: DW_TAG_enumeration_type (E)
16800              3: DW_TAG_enumerator (enum1:0)
16801              3: DW_TAG_enumerator (enum2:1)
16802              ...
16803            2: DW_TAG_template_type_param
16804               DW_AT_type  DW_FORM_ref_udata (E)
16805 
16806 	 Besides being broken debug info, it can put GDB into an
16807 	 infinite loop.  Consider:
16808 
16809 	 When we're building the full name for Class<E>, we'll start
16810 	 at Class, and go look over its template type parameters,
16811 	 finding E.  We'll then try to build the full name of E, and
16812 	 reach here.  We're now trying to build the full name of E,
16813 	 and look over the parent DIE for containing scope.  In the
16814 	 broken case, if we followed the parent DIE of E, we'd again
16815 	 find Class, and once again go look at its template type
16816 	 arguments, etc., etc.  Simply don't consider such parent die
16817 	 as source-level parent of this die (it can't be, the language
16818 	 doesn't allow it), and break the loop here.  */
16819       name = dwarf2_name (die, cu);
16820       parent_name = dwarf2_name (parent, cu);
16821       complaint (&symfile_complaints,
16822 		 _("template param type '%s' defined within parent '%s'"),
16823 		 name ? name : "<unknown>",
16824 		 parent_name ? parent_name : "<unknown>");
16825       return "";
16826     }
16827   else
16828     switch (parent->tag)
16829       {
16830       case DW_TAG_namespace:
16831 	parent_type = read_type_die (parent, cu);
16832 	/* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16833 	   DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16834 	   Work around this problem here.  */
16835 	if (cu->language == language_cplus
16836 	    && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16837 	  return "";
16838 	/* We give a name to even anonymous namespaces.  */
16839 	return TYPE_TAG_NAME (parent_type);
16840       case DW_TAG_class_type:
16841       case DW_TAG_interface_type:
16842       case DW_TAG_structure_type:
16843       case DW_TAG_union_type:
16844       case DW_TAG_module:
16845 	parent_type = read_type_die (parent, cu);
16846 	if (TYPE_TAG_NAME (parent_type) != NULL)
16847 	  return TYPE_TAG_NAME (parent_type);
16848 	else
16849 	  /* An anonymous structure is only allowed non-static data
16850 	     members; no typedefs, no member functions, et cetera.
16851 	     So it does not need a prefix.  */
16852 	  return "";
16853       case DW_TAG_compile_unit:
16854       case DW_TAG_partial_unit:
16855 	/* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
16856 	if (cu->language == language_cplus
16857 	    && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16858 	    && die->child != NULL
16859 	    && (die->tag == DW_TAG_class_type
16860 		|| die->tag == DW_TAG_structure_type
16861 		|| die->tag == DW_TAG_union_type))
16862 	  {
16863 	    char *name = guess_full_die_structure_name (die, cu);
16864 	    if (name != NULL)
16865 	      return name;
16866 	  }
16867 	return "";
16868       default:
16869 	return determine_prefix (parent, cu);
16870       }
16871 }
16872 
16873 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16874    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
16875    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
16876    an obconcat, otherwise allocate storage for the result.  The CU argument is
16877    used to determine the language and hence, the appropriate separator.  */
16878 
16879 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
16880 
16881 static char *
16882 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16883                  int physname, struct dwarf2_cu *cu)
16884 {
16885   const char *lead = "";
16886   const char *sep;
16887 
16888   if (suffix == NULL || suffix[0] == '\0'
16889       || prefix == NULL || prefix[0] == '\0')
16890     sep = "";
16891   else if (cu->language == language_java)
16892     sep = ".";
16893   else if (cu->language == language_fortran && physname)
16894     {
16895       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
16896 	 DW_AT_MIPS_linkage_name is preferred and used instead.  */
16897 
16898       lead = "__";
16899       sep = "_MOD_";
16900     }
16901   else
16902     sep = "::";
16903 
16904   if (prefix == NULL)
16905     prefix = "";
16906   if (suffix == NULL)
16907     suffix = "";
16908 
16909   if (obs == NULL)
16910     {
16911       char *retval
16912 	= xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16913 
16914       strcpy (retval, lead);
16915       strcat (retval, prefix);
16916       strcat (retval, sep);
16917       strcat (retval, suffix);
16918       return retval;
16919     }
16920   else
16921     {
16922       /* We have an obstack.  */
16923       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16924     }
16925 }
16926 
16927 /* Return sibling of die, NULL if no sibling.  */
16928 
16929 static struct die_info *
16930 sibling_die (struct die_info *die)
16931 {
16932   return die->sibling;
16933 }
16934 
16935 /* Get name of a die, return NULL if not found.  */
16936 
16937 static const char *
16938 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16939 			  struct obstack *obstack)
16940 {
16941   if (name && cu->language == language_cplus)
16942     {
16943       char *canon_name = cp_canonicalize_string (name);
16944 
16945       if (canon_name != NULL)
16946 	{
16947 	  if (strcmp (canon_name, name) != 0)
16948 	    name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16949 	  xfree (canon_name);
16950 	}
16951     }
16952 
16953   return name;
16954 }
16955 
16956 /* Get name of a die, return NULL if not found.  */
16957 
16958 static const char *
16959 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16960 {
16961   struct attribute *attr;
16962 
16963   attr = dwarf2_attr (die, DW_AT_name, cu);
16964   if ((!attr || !DW_STRING (attr))
16965       && die->tag != DW_TAG_class_type
16966       && die->tag != DW_TAG_interface_type
16967       && die->tag != DW_TAG_structure_type
16968       && die->tag != DW_TAG_union_type)
16969     return NULL;
16970 
16971   switch (die->tag)
16972     {
16973     case DW_TAG_compile_unit:
16974     case DW_TAG_partial_unit:
16975       /* Compilation units have a DW_AT_name that is a filename, not
16976 	 a source language identifier.  */
16977     case DW_TAG_enumeration_type:
16978     case DW_TAG_enumerator:
16979       /* These tags always have simple identifiers already; no need
16980 	 to canonicalize them.  */
16981       return DW_STRING (attr);
16982 
16983     case DW_TAG_subprogram:
16984       /* Java constructors will all be named "<init>", so return
16985 	 the class name when we see this special case.  */
16986       if (cu->language == language_java
16987 	  && DW_STRING (attr) != NULL
16988 	  && strcmp (DW_STRING (attr), "<init>") == 0)
16989 	{
16990 	  struct dwarf2_cu *spec_cu = cu;
16991 	  struct die_info *spec_die;
16992 
16993 	  /* GCJ will output '<init>' for Java constructor names.
16994 	     For this special case, return the name of the parent class.  */
16995 
16996 	  /* GCJ may output suprogram DIEs with AT_specification set.
16997 	     If so, use the name of the specified DIE.  */
16998 	  spec_die = die_specification (die, &spec_cu);
16999 	  if (spec_die != NULL)
17000 	    return dwarf2_name (spec_die, spec_cu);
17001 
17002 	  do
17003 	    {
17004 	      die = die->parent;
17005 	      if (die->tag == DW_TAG_class_type)
17006 		return dwarf2_name (die, cu);
17007 	    }
17008 	  while (die->tag != DW_TAG_compile_unit
17009 		 && die->tag != DW_TAG_partial_unit);
17010 	}
17011       break;
17012 
17013     case DW_TAG_class_type:
17014     case DW_TAG_interface_type:
17015     case DW_TAG_structure_type:
17016     case DW_TAG_union_type:
17017       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17018 	 structures or unions.  These were of the form "._%d" in GCC 4.1,
17019 	 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17020 	 and GCC 4.4.  We work around this problem by ignoring these.  */
17021       if (attr && DW_STRING (attr)
17022 	  && (strncmp (DW_STRING (attr), "._", 2) == 0
17023 	      || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17024 	return NULL;
17025 
17026       /* GCC might emit a nameless typedef that has a linkage name.  See
17027 	 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
17028       if (!attr || DW_STRING (attr) == NULL)
17029 	{
17030 	  char *demangled = NULL;
17031 
17032 	  attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17033 	  if (attr == NULL)
17034 	    attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17035 
17036 	  if (attr == NULL || DW_STRING (attr) == NULL)
17037 	    return NULL;
17038 
17039 	  /* Avoid demangling DW_STRING (attr) the second time on a second
17040 	     call for the same DIE.  */
17041 	  if (!DW_STRING_IS_CANONICAL (attr))
17042 	    demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17043 
17044 	  if (demangled)
17045 	    {
17046 	      char *base;
17047 
17048 	      /* FIXME: we already did this for the partial symbol... */
17049 	      DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17050 						demangled, strlen (demangled));
17051 	      DW_STRING_IS_CANONICAL (attr) = 1;
17052 	      xfree (demangled);
17053 
17054 	      /* Strip any leading namespaces/classes, keep only the base name.
17055 		 DW_AT_name for named DIEs does not contain the prefixes.  */
17056 	      base = strrchr (DW_STRING (attr), ':');
17057 	      if (base && base > DW_STRING (attr) && base[-1] == ':')
17058 		return &base[1];
17059 	      else
17060 		return DW_STRING (attr);
17061 	    }
17062 	}
17063       break;
17064 
17065     default:
17066       break;
17067     }
17068 
17069   if (!DW_STRING_IS_CANONICAL (attr))
17070     {
17071       DW_STRING (attr)
17072 	= dwarf2_canonicalize_name (DW_STRING (attr), cu,
17073 				    &cu->objfile->objfile_obstack);
17074       DW_STRING_IS_CANONICAL (attr) = 1;
17075     }
17076   return DW_STRING (attr);
17077 }
17078 
17079 /* Return the die that this die in an extension of, or NULL if there
17080    is none.  *EXT_CU is the CU containing DIE on input, and the CU
17081    containing the return value on output.  */
17082 
17083 static struct die_info *
17084 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17085 {
17086   struct attribute *attr;
17087 
17088   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17089   if (attr == NULL)
17090     return NULL;
17091 
17092   return follow_die_ref (die, attr, ext_cu);
17093 }
17094 
17095 /* Convert a DIE tag into its string name.  */
17096 
17097 static const char *
17098 dwarf_tag_name (unsigned tag)
17099 {
17100   const char *name = get_DW_TAG_name (tag);
17101 
17102   if (name == NULL)
17103     return "DW_TAG_<unknown>";
17104 
17105   return name;
17106 }
17107 
17108 /* Convert a DWARF attribute code into its string name.  */
17109 
17110 static const char *
17111 dwarf_attr_name (unsigned attr)
17112 {
17113   const char *name;
17114 
17115 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17116   if (attr == DW_AT_MIPS_fde)
17117     return "DW_AT_MIPS_fde";
17118 #else
17119   if (attr == DW_AT_HP_block_index)
17120     return "DW_AT_HP_block_index";
17121 #endif
17122 
17123   name = get_DW_AT_name (attr);
17124 
17125   if (name == NULL)
17126     return "DW_AT_<unknown>";
17127 
17128   return name;
17129 }
17130 
17131 /* Convert a DWARF value form code into its string name.  */
17132 
17133 static const char *
17134 dwarf_form_name (unsigned form)
17135 {
17136   const char *name = get_DW_FORM_name (form);
17137 
17138   if (name == NULL)
17139     return "DW_FORM_<unknown>";
17140 
17141   return name;
17142 }
17143 
17144 static char *
17145 dwarf_bool_name (unsigned mybool)
17146 {
17147   if (mybool)
17148     return "TRUE";
17149   else
17150     return "FALSE";
17151 }
17152 
17153 /* Convert a DWARF type code into its string name.  */
17154 
17155 static const char *
17156 dwarf_type_encoding_name (unsigned enc)
17157 {
17158   const char *name = get_DW_ATE_name (enc);
17159 
17160   if (name == NULL)
17161     return "DW_ATE_<unknown>";
17162 
17163   return name;
17164 }
17165 
17166 static void
17167 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17168 {
17169   unsigned int i;
17170 
17171   print_spaces (indent, f);
17172   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17173 	   dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17174 
17175   if (die->parent != NULL)
17176     {
17177       print_spaces (indent, f);
17178       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
17179 			  die->parent->offset.sect_off);
17180     }
17181 
17182   print_spaces (indent, f);
17183   fprintf_unfiltered (f, "  has children: %s\n",
17184 	   dwarf_bool_name (die->child != NULL));
17185 
17186   print_spaces (indent, f);
17187   fprintf_unfiltered (f, "  attributes:\n");
17188 
17189   for (i = 0; i < die->num_attrs; ++i)
17190     {
17191       print_spaces (indent, f);
17192       fprintf_unfiltered (f, "    %s (%s) ",
17193 	       dwarf_attr_name (die->attrs[i].name),
17194 	       dwarf_form_name (die->attrs[i].form));
17195 
17196       switch (die->attrs[i].form)
17197 	{
17198 	case DW_FORM_addr:
17199 	case DW_FORM_GNU_addr_index:
17200 	  fprintf_unfiltered (f, "address: ");
17201 	  fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17202 	  break;
17203 	case DW_FORM_block2:
17204 	case DW_FORM_block4:
17205 	case DW_FORM_block:
17206 	case DW_FORM_block1:
17207 	  fprintf_unfiltered (f, "block: size %s",
17208 			      pulongest (DW_BLOCK (&die->attrs[i])->size));
17209 	  break;
17210 	case DW_FORM_exprloc:
17211 	  fprintf_unfiltered (f, "expression: size %s",
17212 			      pulongest (DW_BLOCK (&die->attrs[i])->size));
17213 	  break;
17214 	case DW_FORM_ref_addr:
17215 	  fprintf_unfiltered (f, "ref address: ");
17216 	  fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17217 	  break;
17218 	case DW_FORM_GNU_ref_alt:
17219 	  fprintf_unfiltered (f, "alt ref address: ");
17220 	  fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17221 	  break;
17222 	case DW_FORM_ref1:
17223 	case DW_FORM_ref2:
17224 	case DW_FORM_ref4:
17225 	case DW_FORM_ref8:
17226 	case DW_FORM_ref_udata:
17227 	  fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17228 			      (long) (DW_UNSND (&die->attrs[i])));
17229 	  break;
17230 	case DW_FORM_data1:
17231 	case DW_FORM_data2:
17232 	case DW_FORM_data4:
17233 	case DW_FORM_data8:
17234 	case DW_FORM_udata:
17235 	case DW_FORM_sdata:
17236 	  fprintf_unfiltered (f, "constant: %s",
17237 			      pulongest (DW_UNSND (&die->attrs[i])));
17238 	  break;
17239 	case DW_FORM_sec_offset:
17240 	  fprintf_unfiltered (f, "section offset: %s",
17241 			      pulongest (DW_UNSND (&die->attrs[i])));
17242 	  break;
17243 	case DW_FORM_ref_sig8:
17244 	  if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17245 	    fprintf_unfiltered (f, "signatured type, offset: 0x%x",
17246 			 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
17247 	  else
17248 	    fprintf_unfiltered (f, "signatured type, offset: unknown");
17249 	  break;
17250 	case DW_FORM_string:
17251 	case DW_FORM_strp:
17252 	case DW_FORM_GNU_str_index:
17253 	case DW_FORM_GNU_strp_alt:
17254 	  fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17255 		   DW_STRING (&die->attrs[i])
17256 		   ? DW_STRING (&die->attrs[i]) : "",
17257 		   DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17258 	  break;
17259 	case DW_FORM_flag:
17260 	  if (DW_UNSND (&die->attrs[i]))
17261 	    fprintf_unfiltered (f, "flag: TRUE");
17262 	  else
17263 	    fprintf_unfiltered (f, "flag: FALSE");
17264 	  break;
17265 	case DW_FORM_flag_present:
17266 	  fprintf_unfiltered (f, "flag: TRUE");
17267 	  break;
17268 	case DW_FORM_indirect:
17269 	  /* The reader will have reduced the indirect form to
17270 	     the "base form" so this form should not occur.  */
17271 	  fprintf_unfiltered (f,
17272 			      "unexpected attribute form: DW_FORM_indirect");
17273 	  break;
17274 	default:
17275 	  fprintf_unfiltered (f, "unsupported attribute form: %d.",
17276 		   die->attrs[i].form);
17277 	  break;
17278 	}
17279       fprintf_unfiltered (f, "\n");
17280     }
17281 }
17282 
17283 static void
17284 dump_die_for_error (struct die_info *die)
17285 {
17286   dump_die_shallow (gdb_stderr, 0, die);
17287 }
17288 
17289 static void
17290 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17291 {
17292   int indent = level * 4;
17293 
17294   gdb_assert (die != NULL);
17295 
17296   if (level >= max_level)
17297     return;
17298 
17299   dump_die_shallow (f, indent, die);
17300 
17301   if (die->child != NULL)
17302     {
17303       print_spaces (indent, f);
17304       fprintf_unfiltered (f, "  Children:");
17305       if (level + 1 < max_level)
17306 	{
17307 	  fprintf_unfiltered (f, "\n");
17308 	  dump_die_1 (f, level + 1, max_level, die->child);
17309 	}
17310       else
17311 	{
17312 	  fprintf_unfiltered (f,
17313 			      " [not printed, max nesting level reached]\n");
17314 	}
17315     }
17316 
17317   if (die->sibling != NULL && level > 0)
17318     {
17319       dump_die_1 (f, level, max_level, die->sibling);
17320     }
17321 }
17322 
17323 /* This is called from the pdie macro in gdbinit.in.
17324    It's not static so gcc will keep a copy callable from gdb.  */
17325 
17326 void
17327 dump_die (struct die_info *die, int max_level)
17328 {
17329   dump_die_1 (gdb_stdlog, 0, max_level, die);
17330 }
17331 
17332 static void
17333 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17334 {
17335   void **slot;
17336 
17337   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17338 				   INSERT);
17339 
17340   *slot = die;
17341 }
17342 
17343 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17344    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
17345 
17346 static int
17347 is_ref_attr (struct attribute *attr)
17348 {
17349   switch (attr->form)
17350     {
17351     case DW_FORM_ref_addr:
17352     case DW_FORM_ref1:
17353     case DW_FORM_ref2:
17354     case DW_FORM_ref4:
17355     case DW_FORM_ref8:
17356     case DW_FORM_ref_udata:
17357     case DW_FORM_GNU_ref_alt:
17358       return 1;
17359     default:
17360       return 0;
17361     }
17362 }
17363 
17364 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
17365    required kind.  */
17366 
17367 static sect_offset
17368 dwarf2_get_ref_die_offset (struct attribute *attr)
17369 {
17370   sect_offset retval = { DW_UNSND (attr) };
17371 
17372   if (is_ref_attr (attr))
17373     return retval;
17374 
17375   retval.sect_off = 0;
17376   complaint (&symfile_complaints,
17377 	     _("unsupported die ref attribute form: '%s'"),
17378 	     dwarf_form_name (attr->form));
17379   return retval;
17380 }
17381 
17382 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
17383  * the value held by the attribute is not constant.  */
17384 
17385 static LONGEST
17386 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17387 {
17388   if (attr->form == DW_FORM_sdata)
17389     return DW_SND (attr);
17390   else if (attr->form == DW_FORM_udata
17391            || attr->form == DW_FORM_data1
17392            || attr->form == DW_FORM_data2
17393            || attr->form == DW_FORM_data4
17394            || attr->form == DW_FORM_data8)
17395     return DW_UNSND (attr);
17396   else
17397     {
17398       complaint (&symfile_complaints,
17399 		 _("Attribute value is not a constant (%s)"),
17400                  dwarf_form_name (attr->form));
17401       return default_value;
17402     }
17403 }
17404 
17405 /* Follow reference or signature attribute ATTR of SRC_DIE.
17406    On entry *REF_CU is the CU of SRC_DIE.
17407    On exit *REF_CU is the CU of the result.  */
17408 
17409 static struct die_info *
17410 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17411 		       struct dwarf2_cu **ref_cu)
17412 {
17413   struct die_info *die;
17414 
17415   if (is_ref_attr (attr))
17416     die = follow_die_ref (src_die, attr, ref_cu);
17417   else if (attr->form == DW_FORM_ref_sig8)
17418     die = follow_die_sig (src_die, attr, ref_cu);
17419   else
17420     {
17421       dump_die_for_error (src_die);
17422       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17423 	     (*ref_cu)->objfile->name);
17424     }
17425 
17426   return die;
17427 }
17428 
17429 /* Follow reference OFFSET.
17430    On entry *REF_CU is the CU of the source die referencing OFFSET.
17431    On exit *REF_CU is the CU of the result.
17432    Returns NULL if OFFSET is invalid.  */
17433 
17434 static struct die_info *
17435 follow_die_offset (sect_offset offset, int offset_in_dwz,
17436 		   struct dwarf2_cu **ref_cu)
17437 {
17438   struct die_info temp_die;
17439   struct dwarf2_cu *target_cu, *cu = *ref_cu;
17440 
17441   gdb_assert (cu->per_cu != NULL);
17442 
17443   target_cu = cu;
17444 
17445   if (cu->per_cu->is_debug_types)
17446     {
17447       /* .debug_types CUs cannot reference anything outside their CU.
17448 	 If they need to, they have to reference a signatured type via
17449 	 DW_FORM_ref_sig8.  */
17450       if (! offset_in_cu_p (&cu->header, offset))
17451 	return NULL;
17452     }
17453   else if (offset_in_dwz != cu->per_cu->is_dwz
17454 	   || ! offset_in_cu_p (&cu->header, offset))
17455     {
17456       struct dwarf2_per_cu_data *per_cu;
17457 
17458       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17459 						 cu->objfile);
17460 
17461       /* If necessary, add it to the queue and load its DIEs.  */
17462       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17463 	load_full_comp_unit (per_cu, cu->language);
17464 
17465       target_cu = per_cu->cu;
17466     }
17467   else if (cu->dies == NULL)
17468     {
17469       /* We're loading full DIEs during partial symbol reading.  */
17470       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17471       load_full_comp_unit (cu->per_cu, language_minimal);
17472     }
17473 
17474   *ref_cu = target_cu;
17475   temp_die.offset = offset;
17476   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17477 }
17478 
17479 /* Follow reference attribute ATTR of SRC_DIE.
17480    On entry *REF_CU is the CU of SRC_DIE.
17481    On exit *REF_CU is the CU of the result.  */
17482 
17483 static struct die_info *
17484 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17485 		struct dwarf2_cu **ref_cu)
17486 {
17487   sect_offset offset = dwarf2_get_ref_die_offset (attr);
17488   struct dwarf2_cu *cu = *ref_cu;
17489   struct die_info *die;
17490 
17491   die = follow_die_offset (offset,
17492 			   (attr->form == DW_FORM_GNU_ref_alt
17493 			    || cu->per_cu->is_dwz),
17494 			   ref_cu);
17495   if (!die)
17496     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17497 	   "at 0x%x [in module %s]"),
17498 	   offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17499 
17500   return die;
17501 }
17502 
17503 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17504    Returned value is intended for DW_OP_call*.  Returned
17505    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
17506 
17507 struct dwarf2_locexpr_baton
17508 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17509 			       struct dwarf2_per_cu_data *per_cu,
17510 			       CORE_ADDR (*get_frame_pc) (void *baton),
17511 			       void *baton)
17512 {
17513   struct dwarf2_cu *cu;
17514   struct die_info *die;
17515   struct attribute *attr;
17516   struct dwarf2_locexpr_baton retval;
17517 
17518   dw2_setup (per_cu->objfile);
17519 
17520   if (per_cu->cu == NULL)
17521     load_cu (per_cu);
17522   cu = per_cu->cu;
17523 
17524   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17525   if (!die)
17526     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17527 	   offset.sect_off, per_cu->objfile->name);
17528 
17529   attr = dwarf2_attr (die, DW_AT_location, cu);
17530   if (!attr)
17531     {
17532       /* DWARF: "If there is no such attribute, then there is no effect.".
17533 	 DATA is ignored if SIZE is 0.  */
17534 
17535       retval.data = NULL;
17536       retval.size = 0;
17537     }
17538   else if (attr_form_is_section_offset (attr))
17539     {
17540       struct dwarf2_loclist_baton loclist_baton;
17541       CORE_ADDR pc = (*get_frame_pc) (baton);
17542       size_t size;
17543 
17544       fill_in_loclist_baton (cu, &loclist_baton, attr);
17545 
17546       retval.data = dwarf2_find_location_expression (&loclist_baton,
17547 						     &size, pc);
17548       retval.size = size;
17549     }
17550   else
17551     {
17552       if (!attr_form_is_block (attr))
17553 	error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17554 		 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17555 	       offset.sect_off, per_cu->objfile->name);
17556 
17557       retval.data = DW_BLOCK (attr)->data;
17558       retval.size = DW_BLOCK (attr)->size;
17559     }
17560   retval.per_cu = cu->per_cu;
17561 
17562   age_cached_comp_units ();
17563 
17564   return retval;
17565 }
17566 
17567 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17568    offset.  */
17569 
17570 struct dwarf2_locexpr_baton
17571 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17572 			     struct dwarf2_per_cu_data *per_cu,
17573 			     CORE_ADDR (*get_frame_pc) (void *baton),
17574 			     void *baton)
17575 {
17576   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17577 
17578   return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17579 }
17580 
17581 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17582    PER_CU.  */
17583 
17584 struct type *
17585 dwarf2_get_die_type (cu_offset die_offset,
17586 		     struct dwarf2_per_cu_data *per_cu)
17587 {
17588   sect_offset die_offset_sect;
17589 
17590   dw2_setup (per_cu->objfile);
17591 
17592   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17593   return get_die_type_at_offset (die_offset_sect, per_cu);
17594 }
17595 
17596 /* Follow the signature attribute ATTR in SRC_DIE.
17597    On entry *REF_CU is the CU of SRC_DIE.
17598    On exit *REF_CU is the CU of the result.  */
17599 
17600 static struct die_info *
17601 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17602 		struct dwarf2_cu **ref_cu)
17603 {
17604   struct objfile *objfile = (*ref_cu)->objfile;
17605   struct die_info temp_die;
17606   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17607   struct dwarf2_cu *sig_cu;
17608   struct die_info *die;
17609 
17610   /* sig_type will be NULL if the signatured type is missing from
17611      the debug info.  */
17612   if (sig_type == NULL)
17613     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17614 	     "at 0x%x [in module %s]"),
17615 	   src_die->offset.sect_off, objfile->name);
17616 
17617   /* If necessary, add it to the queue and load its DIEs.  */
17618 
17619   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17620     read_signatured_type (sig_type);
17621 
17622   gdb_assert (sig_type->per_cu.cu != NULL);
17623 
17624   sig_cu = sig_type->per_cu.cu;
17625   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17626   temp_die.offset = sig_type->type_offset_in_section;
17627   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17628 			     temp_die.offset.sect_off);
17629   if (die)
17630     {
17631       /* For .gdb_index version 7 keep track of included TUs.
17632 	 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
17633       if (dwarf2_per_objfile->index_table != NULL
17634 	  && dwarf2_per_objfile->index_table->version <= 7)
17635 	{
17636 	  VEC_safe_push (dwarf2_per_cu_ptr,
17637 			 (*ref_cu)->per_cu->imported_symtabs,
17638 			 sig_cu->per_cu);
17639 	}
17640 
17641       *ref_cu = sig_cu;
17642       return die;
17643     }
17644 
17645   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17646 	 "from DIE at 0x%x [in module %s]"),
17647 	 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17648 }
17649 
17650 /* Given an offset of a signatured type, return its signatured_type.  */
17651 
17652 static struct signatured_type *
17653 lookup_signatured_type_at_offset (struct objfile *objfile,
17654 				  struct dwarf2_section_info *section,
17655 				  sect_offset offset)
17656 {
17657   gdb_byte *info_ptr = section->buffer + offset.sect_off;
17658   unsigned int length, initial_length_size;
17659   unsigned int sig_offset;
17660   struct signatured_type find_entry, *sig_type;
17661 
17662   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17663   sig_offset = (initial_length_size
17664 		+ 2 /*version*/
17665 		+ (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17666 		+ 1 /*address_size*/);
17667   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17668   sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17669 
17670   /* This is only used to lookup previously recorded types.
17671      If we didn't find it, it's our bug.  */
17672   gdb_assert (sig_type != NULL);
17673   gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17674 
17675   return sig_type;
17676 }
17677 
17678 /* Load the DIEs associated with type unit PER_CU into memory.  */
17679 
17680 static void
17681 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17682 {
17683   struct signatured_type *sig_type;
17684 
17685   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
17686   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17687 
17688   /* We have the per_cu, but we need the signatured_type.
17689      Fortunately this is an easy translation.  */
17690   gdb_assert (per_cu->is_debug_types);
17691   sig_type = (struct signatured_type *) per_cu;
17692 
17693   gdb_assert (per_cu->cu == NULL);
17694 
17695   read_signatured_type (sig_type);
17696 
17697   gdb_assert (per_cu->cu != NULL);
17698 }
17699 
17700 /* die_reader_func for read_signatured_type.
17701    This is identical to load_full_comp_unit_reader,
17702    but is kept separate for now.  */
17703 
17704 static void
17705 read_signatured_type_reader (const struct die_reader_specs *reader,
17706 			     gdb_byte *info_ptr,
17707 			     struct die_info *comp_unit_die,
17708 			     int has_children,
17709 			     void *data)
17710 {
17711   struct dwarf2_cu *cu = reader->cu;
17712 
17713   gdb_assert (cu->die_hash == NULL);
17714   cu->die_hash =
17715     htab_create_alloc_ex (cu->header.length / 12,
17716 			  die_hash,
17717 			  die_eq,
17718 			  NULL,
17719 			  &cu->comp_unit_obstack,
17720 			  hashtab_obstack_allocate,
17721 			  dummy_obstack_deallocate);
17722 
17723   if (has_children)
17724     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17725 						  &info_ptr, comp_unit_die);
17726   cu->dies = comp_unit_die;
17727   /* comp_unit_die is not stored in die_hash, no need.  */
17728 
17729   /* We try not to read any attributes in this function, because not
17730      all CUs needed for references have been loaded yet, and symbol
17731      table processing isn't initialized.  But we have to set the CU language,
17732      or we won't be able to build types correctly.
17733      Similarly, if we do not read the producer, we can not apply
17734      producer-specific interpretation.  */
17735   prepare_one_comp_unit (cu, cu->dies, language_minimal);
17736 }
17737 
17738 /* Read in a signatured type and build its CU and DIEs.
17739    If the type is a stub for the real type in a DWO file,
17740    read in the real type from the DWO file as well.  */
17741 
17742 static void
17743 read_signatured_type (struct signatured_type *sig_type)
17744 {
17745   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17746 
17747   gdb_assert (per_cu->is_debug_types);
17748   gdb_assert (per_cu->cu == NULL);
17749 
17750   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17751 			   read_signatured_type_reader, NULL);
17752 }
17753 
17754 /* Decode simple location descriptions.
17755    Given a pointer to a dwarf block that defines a location, compute
17756    the location and return the value.
17757 
17758    NOTE drow/2003-11-18: This function is called in two situations
17759    now: for the address of static or global variables (partial symbols
17760    only) and for offsets into structures which are expected to be
17761    (more or less) constant.  The partial symbol case should go away,
17762    and only the constant case should remain.  That will let this
17763    function complain more accurately.  A few special modes are allowed
17764    without complaint for global variables (for instance, global
17765    register values and thread-local values).
17766 
17767    A location description containing no operations indicates that the
17768    object is optimized out.  The return value is 0 for that case.
17769    FIXME drow/2003-11-16: No callers check for this case any more; soon all
17770    callers will only want a very basic result and this can become a
17771    complaint.
17772 
17773    Note that stack[0] is unused except as a default error return.  */
17774 
17775 static CORE_ADDR
17776 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17777 {
17778   struct objfile *objfile = cu->objfile;
17779   size_t i;
17780   size_t size = blk->size;
17781   gdb_byte *data = blk->data;
17782   CORE_ADDR stack[64];
17783   int stacki;
17784   unsigned int bytes_read, unsnd;
17785   gdb_byte op;
17786 
17787   i = 0;
17788   stacki = 0;
17789   stack[stacki] = 0;
17790   stack[++stacki] = 0;
17791 
17792   while (i < size)
17793     {
17794       op = data[i++];
17795       switch (op)
17796 	{
17797 	case DW_OP_lit0:
17798 	case DW_OP_lit1:
17799 	case DW_OP_lit2:
17800 	case DW_OP_lit3:
17801 	case DW_OP_lit4:
17802 	case DW_OP_lit5:
17803 	case DW_OP_lit6:
17804 	case DW_OP_lit7:
17805 	case DW_OP_lit8:
17806 	case DW_OP_lit9:
17807 	case DW_OP_lit10:
17808 	case DW_OP_lit11:
17809 	case DW_OP_lit12:
17810 	case DW_OP_lit13:
17811 	case DW_OP_lit14:
17812 	case DW_OP_lit15:
17813 	case DW_OP_lit16:
17814 	case DW_OP_lit17:
17815 	case DW_OP_lit18:
17816 	case DW_OP_lit19:
17817 	case DW_OP_lit20:
17818 	case DW_OP_lit21:
17819 	case DW_OP_lit22:
17820 	case DW_OP_lit23:
17821 	case DW_OP_lit24:
17822 	case DW_OP_lit25:
17823 	case DW_OP_lit26:
17824 	case DW_OP_lit27:
17825 	case DW_OP_lit28:
17826 	case DW_OP_lit29:
17827 	case DW_OP_lit30:
17828 	case DW_OP_lit31:
17829 	  stack[++stacki] = op - DW_OP_lit0;
17830 	  break;
17831 
17832 	case DW_OP_reg0:
17833 	case DW_OP_reg1:
17834 	case DW_OP_reg2:
17835 	case DW_OP_reg3:
17836 	case DW_OP_reg4:
17837 	case DW_OP_reg5:
17838 	case DW_OP_reg6:
17839 	case DW_OP_reg7:
17840 	case DW_OP_reg8:
17841 	case DW_OP_reg9:
17842 	case DW_OP_reg10:
17843 	case DW_OP_reg11:
17844 	case DW_OP_reg12:
17845 	case DW_OP_reg13:
17846 	case DW_OP_reg14:
17847 	case DW_OP_reg15:
17848 	case DW_OP_reg16:
17849 	case DW_OP_reg17:
17850 	case DW_OP_reg18:
17851 	case DW_OP_reg19:
17852 	case DW_OP_reg20:
17853 	case DW_OP_reg21:
17854 	case DW_OP_reg22:
17855 	case DW_OP_reg23:
17856 	case DW_OP_reg24:
17857 	case DW_OP_reg25:
17858 	case DW_OP_reg26:
17859 	case DW_OP_reg27:
17860 	case DW_OP_reg28:
17861 	case DW_OP_reg29:
17862 	case DW_OP_reg30:
17863 	case DW_OP_reg31:
17864 	  stack[++stacki] = op - DW_OP_reg0;
17865 	  if (i < size)
17866 	    dwarf2_complex_location_expr_complaint ();
17867 	  break;
17868 
17869 	case DW_OP_regx:
17870 	  unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17871 	  i += bytes_read;
17872 	  stack[++stacki] = unsnd;
17873 	  if (i < size)
17874 	    dwarf2_complex_location_expr_complaint ();
17875 	  break;
17876 
17877 	case DW_OP_addr:
17878 	  stack[++stacki] = read_address (objfile->obfd, &data[i],
17879 					  cu, &bytes_read);
17880 	  i += bytes_read;
17881 	  break;
17882 
17883 	case DW_OP_const1u:
17884 	  stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17885 	  i += 1;
17886 	  break;
17887 
17888 	case DW_OP_const1s:
17889 	  stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17890 	  i += 1;
17891 	  break;
17892 
17893 	case DW_OP_const2u:
17894 	  stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17895 	  i += 2;
17896 	  break;
17897 
17898 	case DW_OP_const2s:
17899 	  stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17900 	  i += 2;
17901 	  break;
17902 
17903 	case DW_OP_const4u:
17904 	  stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17905 	  i += 4;
17906 	  break;
17907 
17908 	case DW_OP_const4s:
17909 	  stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17910 	  i += 4;
17911 	  break;
17912 
17913 	case DW_OP_const8u:
17914 	  stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17915 	  i += 8;
17916 	  break;
17917 
17918 	case DW_OP_constu:
17919 	  stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17920 						  &bytes_read);
17921 	  i += bytes_read;
17922 	  break;
17923 
17924 	case DW_OP_consts:
17925 	  stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17926 	  i += bytes_read;
17927 	  break;
17928 
17929 	case DW_OP_dup:
17930 	  stack[stacki + 1] = stack[stacki];
17931 	  stacki++;
17932 	  break;
17933 
17934 	case DW_OP_plus:
17935 	  stack[stacki - 1] += stack[stacki];
17936 	  stacki--;
17937 	  break;
17938 
17939 	case DW_OP_plus_uconst:
17940 	  stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17941 						 &bytes_read);
17942 	  i += bytes_read;
17943 	  break;
17944 
17945 	case DW_OP_minus:
17946 	  stack[stacki - 1] -= stack[stacki];
17947 	  stacki--;
17948 	  break;
17949 
17950 	case DW_OP_deref:
17951 	  /* If we're not the last op, then we definitely can't encode
17952 	     this using GDB's address_class enum.  This is valid for partial
17953 	     global symbols, although the variable's address will be bogus
17954 	     in the psymtab.  */
17955 	  if (i < size)
17956 	    dwarf2_complex_location_expr_complaint ();
17957 	  break;
17958 
17959         case DW_OP_GNU_push_tls_address:
17960 	  /* The top of the stack has the offset from the beginning
17961 	     of the thread control block at which the variable is located.  */
17962 	  /* Nothing should follow this operator, so the top of stack would
17963 	     be returned.  */
17964 	  /* This is valid for partial global symbols, but the variable's
17965 	     address will be bogus in the psymtab.  Make it always at least
17966 	     non-zero to not look as a variable garbage collected by linker
17967 	     which have DW_OP_addr 0.  */
17968 	  if (i < size)
17969 	    dwarf2_complex_location_expr_complaint ();
17970 	  stack[stacki]++;
17971           break;
17972 
17973 	case DW_OP_GNU_uninit:
17974 	  break;
17975 
17976 	case DW_OP_GNU_addr_index:
17977 	case DW_OP_GNU_const_index:
17978 	  stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
17979 							 &bytes_read);
17980 	  i += bytes_read;
17981 	  break;
17982 
17983 	default:
17984 	  {
17985 	    const char *name = get_DW_OP_name (op);
17986 
17987 	    if (name)
17988 	      complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
17989 			 name);
17990 	    else
17991 	      complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
17992 			 op);
17993 	  }
17994 
17995 	  return (stack[stacki]);
17996 	}
17997 
17998       /* Enforce maximum stack depth of SIZE-1 to avoid writing
17999          outside of the allocated space.  Also enforce minimum>0.  */
18000       if (stacki >= ARRAY_SIZE (stack) - 1)
18001 	{
18002 	  complaint (&symfile_complaints,
18003 		     _("location description stack overflow"));
18004 	  return 0;
18005 	}
18006 
18007       if (stacki <= 0)
18008 	{
18009 	  complaint (&symfile_complaints,
18010 		     _("location description stack underflow"));
18011 	  return 0;
18012 	}
18013     }
18014   return (stack[stacki]);
18015 }
18016 
18017 /* memory allocation interface */
18018 
18019 static struct dwarf_block *
18020 dwarf_alloc_block (struct dwarf2_cu *cu)
18021 {
18022   struct dwarf_block *blk;
18023 
18024   blk = (struct dwarf_block *)
18025     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18026   return (blk);
18027 }
18028 
18029 static struct die_info *
18030 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18031 {
18032   struct die_info *die;
18033   size_t size = sizeof (struct die_info);
18034 
18035   if (num_attrs > 1)
18036     size += (num_attrs - 1) * sizeof (struct attribute);
18037 
18038   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18039   memset (die, 0, sizeof (struct die_info));
18040   return (die);
18041 }
18042 
18043 
18044 /* Macro support.  */
18045 
18046 /* Return file name relative to the compilation directory of file number I in
18047    *LH's file name table.  The result is allocated using xmalloc; the caller is
18048    responsible for freeing it.  */
18049 
18050 static char *
18051 file_file_name (int file, struct line_header *lh)
18052 {
18053   /* Is the file number a valid index into the line header's file name
18054      table?  Remember that file numbers start with one, not zero.  */
18055   if (1 <= file && file <= lh->num_file_names)
18056     {
18057       struct file_entry *fe = &lh->file_names[file - 1];
18058 
18059       if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18060         return xstrdup (fe->name);
18061       return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18062 		     fe->name, NULL);
18063     }
18064   else
18065     {
18066       /* The compiler produced a bogus file number.  We can at least
18067          record the macro definitions made in the file, even if we
18068          won't be able to find the file by name.  */
18069       char fake_name[80];
18070 
18071       xsnprintf (fake_name, sizeof (fake_name),
18072 		 "<bad macro file number %d>", file);
18073 
18074       complaint (&symfile_complaints,
18075                  _("bad file number in macro information (%d)"),
18076                  file);
18077 
18078       return xstrdup (fake_name);
18079     }
18080 }
18081 
18082 /* Return the full name of file number I in *LH's file name table.
18083    Use COMP_DIR as the name of the current directory of the
18084    compilation.  The result is allocated using xmalloc; the caller is
18085    responsible for freeing it.  */
18086 static char *
18087 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18088 {
18089   /* Is the file number a valid index into the line header's file name
18090      table?  Remember that file numbers start with one, not zero.  */
18091   if (1 <= file && file <= lh->num_file_names)
18092     {
18093       char *relative = file_file_name (file, lh);
18094 
18095       if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18096 	return relative;
18097       return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18098     }
18099   else
18100     return file_file_name (file, lh);
18101 }
18102 
18103 
18104 static struct macro_source_file *
18105 macro_start_file (int file, int line,
18106                   struct macro_source_file *current_file,
18107                   const char *comp_dir,
18108                   struct line_header *lh, struct objfile *objfile)
18109 {
18110   /* File name relative to the compilation directory of this source file.  */
18111   char *file_name = file_file_name (file, lh);
18112 
18113   /* We don't create a macro table for this compilation unit
18114      at all until we actually get a filename.  */
18115   if (! pending_macros)
18116     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18117 				      objfile->per_bfd->macro_cache,
18118 				      comp_dir);
18119 
18120   if (! current_file)
18121     {
18122       /* If we have no current file, then this must be the start_file
18123 	 directive for the compilation unit's main source file.  */
18124       current_file = macro_set_main (pending_macros, file_name);
18125       macro_define_special (pending_macros);
18126     }
18127   else
18128     current_file = macro_include (current_file, line, file_name);
18129 
18130   xfree (file_name);
18131 
18132   return current_file;
18133 }
18134 
18135 
18136 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18137    followed by a null byte.  */
18138 static char *
18139 copy_string (const char *buf, int len)
18140 {
18141   char *s = xmalloc (len + 1);
18142 
18143   memcpy (s, buf, len);
18144   s[len] = '\0';
18145   return s;
18146 }
18147 
18148 
18149 static const char *
18150 consume_improper_spaces (const char *p, const char *body)
18151 {
18152   if (*p == ' ')
18153     {
18154       complaint (&symfile_complaints,
18155 		 _("macro definition contains spaces "
18156 		   "in formal argument list:\n`%s'"),
18157 		 body);
18158 
18159       while (*p == ' ')
18160         p++;
18161     }
18162 
18163   return p;
18164 }
18165 
18166 
18167 static void
18168 parse_macro_definition (struct macro_source_file *file, int line,
18169                         const char *body)
18170 {
18171   const char *p;
18172 
18173   /* The body string takes one of two forms.  For object-like macro
18174      definitions, it should be:
18175 
18176         <macro name> " " <definition>
18177 
18178      For function-like macro definitions, it should be:
18179 
18180         <macro name> "() " <definition>
18181      or
18182         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18183 
18184      Spaces may appear only where explicitly indicated, and in the
18185      <definition>.
18186 
18187      The Dwarf 2 spec says that an object-like macro's name is always
18188      followed by a space, but versions of GCC around March 2002 omit
18189      the space when the macro's definition is the empty string.
18190 
18191      The Dwarf 2 spec says that there should be no spaces between the
18192      formal arguments in a function-like macro's formal argument list,
18193      but versions of GCC around March 2002 include spaces after the
18194      commas.  */
18195 
18196 
18197   /* Find the extent of the macro name.  The macro name is terminated
18198      by either a space or null character (for an object-like macro) or
18199      an opening paren (for a function-like macro).  */
18200   for (p = body; *p; p++)
18201     if (*p == ' ' || *p == '(')
18202       break;
18203 
18204   if (*p == ' ' || *p == '\0')
18205     {
18206       /* It's an object-like macro.  */
18207       int name_len = p - body;
18208       char *name = copy_string (body, name_len);
18209       const char *replacement;
18210 
18211       if (*p == ' ')
18212         replacement = body + name_len + 1;
18213       else
18214         {
18215 	  dwarf2_macro_malformed_definition_complaint (body);
18216           replacement = body + name_len;
18217         }
18218 
18219       macro_define_object (file, line, name, replacement);
18220 
18221       xfree (name);
18222     }
18223   else if (*p == '(')
18224     {
18225       /* It's a function-like macro.  */
18226       char *name = copy_string (body, p - body);
18227       int argc = 0;
18228       int argv_size = 1;
18229       char **argv = xmalloc (argv_size * sizeof (*argv));
18230 
18231       p++;
18232 
18233       p = consume_improper_spaces (p, body);
18234 
18235       /* Parse the formal argument list.  */
18236       while (*p && *p != ')')
18237         {
18238           /* Find the extent of the current argument name.  */
18239           const char *arg_start = p;
18240 
18241           while (*p && *p != ',' && *p != ')' && *p != ' ')
18242             p++;
18243 
18244           if (! *p || p == arg_start)
18245 	    dwarf2_macro_malformed_definition_complaint (body);
18246           else
18247             {
18248               /* Make sure argv has room for the new argument.  */
18249               if (argc >= argv_size)
18250                 {
18251                   argv_size *= 2;
18252                   argv = xrealloc (argv, argv_size * sizeof (*argv));
18253                 }
18254 
18255               argv[argc++] = copy_string (arg_start, p - arg_start);
18256             }
18257 
18258           p = consume_improper_spaces (p, body);
18259 
18260           /* Consume the comma, if present.  */
18261           if (*p == ',')
18262             {
18263               p++;
18264 
18265               p = consume_improper_spaces (p, body);
18266             }
18267         }
18268 
18269       if (*p == ')')
18270         {
18271           p++;
18272 
18273           if (*p == ' ')
18274             /* Perfectly formed definition, no complaints.  */
18275             macro_define_function (file, line, name,
18276                                    argc, (const char **) argv,
18277                                    p + 1);
18278           else if (*p == '\0')
18279             {
18280               /* Complain, but do define it.  */
18281 	      dwarf2_macro_malformed_definition_complaint (body);
18282               macro_define_function (file, line, name,
18283                                      argc, (const char **) argv,
18284                                      p);
18285             }
18286           else
18287             /* Just complain.  */
18288 	    dwarf2_macro_malformed_definition_complaint (body);
18289         }
18290       else
18291         /* Just complain.  */
18292 	dwarf2_macro_malformed_definition_complaint (body);
18293 
18294       xfree (name);
18295       {
18296         int i;
18297 
18298         for (i = 0; i < argc; i++)
18299           xfree (argv[i]);
18300       }
18301       xfree (argv);
18302     }
18303   else
18304     dwarf2_macro_malformed_definition_complaint (body);
18305 }
18306 
18307 /* Skip some bytes from BYTES according to the form given in FORM.
18308    Returns the new pointer.  */
18309 
18310 static gdb_byte *
18311 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18312 		 enum dwarf_form form,
18313 		 unsigned int offset_size,
18314 		 struct dwarf2_section_info *section)
18315 {
18316   unsigned int bytes_read;
18317 
18318   switch (form)
18319     {
18320     case DW_FORM_data1:
18321     case DW_FORM_flag:
18322       ++bytes;
18323       break;
18324 
18325     case DW_FORM_data2:
18326       bytes += 2;
18327       break;
18328 
18329     case DW_FORM_data4:
18330       bytes += 4;
18331       break;
18332 
18333     case DW_FORM_data8:
18334       bytes += 8;
18335       break;
18336 
18337     case DW_FORM_string:
18338       read_direct_string (abfd, bytes, &bytes_read);
18339       bytes += bytes_read;
18340       break;
18341 
18342     case DW_FORM_sec_offset:
18343     case DW_FORM_strp:
18344     case DW_FORM_GNU_strp_alt:
18345       bytes += offset_size;
18346       break;
18347 
18348     case DW_FORM_block:
18349       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18350       bytes += bytes_read;
18351       break;
18352 
18353     case DW_FORM_block1:
18354       bytes += 1 + read_1_byte (abfd, bytes);
18355       break;
18356     case DW_FORM_block2:
18357       bytes += 2 + read_2_bytes (abfd, bytes);
18358       break;
18359     case DW_FORM_block4:
18360       bytes += 4 + read_4_bytes (abfd, bytes);
18361       break;
18362 
18363     case DW_FORM_sdata:
18364     case DW_FORM_udata:
18365     case DW_FORM_GNU_addr_index:
18366     case DW_FORM_GNU_str_index:
18367       bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18368       if (bytes == NULL)
18369 	{
18370 	  dwarf2_section_buffer_overflow_complaint (section);
18371 	  return NULL;
18372 	}
18373       break;
18374 
18375     default:
18376       {
18377       complain:
18378 	complaint (&symfile_complaints,
18379 		   _("invalid form 0x%x in `%s'"),
18380 		   form,
18381 		   section->asection->name);
18382 	return NULL;
18383       }
18384     }
18385 
18386   return bytes;
18387 }
18388 
18389 /* A helper for dwarf_decode_macros that handles skipping an unknown
18390    opcode.  Returns an updated pointer to the macro data buffer; or,
18391    on error, issues a complaint and returns NULL.  */
18392 
18393 static gdb_byte *
18394 skip_unknown_opcode (unsigned int opcode,
18395 		     gdb_byte **opcode_definitions,
18396 		     gdb_byte *mac_ptr, gdb_byte *mac_end,
18397 		     bfd *abfd,
18398 		     unsigned int offset_size,
18399 		     struct dwarf2_section_info *section)
18400 {
18401   unsigned int bytes_read, i;
18402   unsigned long arg;
18403   gdb_byte *defn;
18404 
18405   if (opcode_definitions[opcode] == NULL)
18406     {
18407       complaint (&symfile_complaints,
18408 		 _("unrecognized DW_MACFINO opcode 0x%x"),
18409 		 opcode);
18410       return NULL;
18411     }
18412 
18413   defn = opcode_definitions[opcode];
18414   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18415   defn += bytes_read;
18416 
18417   for (i = 0; i < arg; ++i)
18418     {
18419       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18420 				 section);
18421       if (mac_ptr == NULL)
18422 	{
18423 	  /* skip_form_bytes already issued the complaint.  */
18424 	  return NULL;
18425 	}
18426     }
18427 
18428   return mac_ptr;
18429 }
18430 
18431 /* A helper function which parses the header of a macro section.
18432    If the macro section is the extended (for now called "GNU") type,
18433    then this updates *OFFSET_SIZE.  Returns a pointer to just after
18434    the header, or issues a complaint and returns NULL on error.  */
18435 
18436 static gdb_byte *
18437 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18438 			  bfd *abfd,
18439 			  gdb_byte *mac_ptr,
18440 			  unsigned int *offset_size,
18441 			  int section_is_gnu)
18442 {
18443   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18444 
18445   if (section_is_gnu)
18446     {
18447       unsigned int version, flags;
18448 
18449       version = read_2_bytes (abfd, mac_ptr);
18450       if (version != 4)
18451 	{
18452 	  complaint (&symfile_complaints,
18453 		     _("unrecognized version `%d' in .debug_macro section"),
18454 		     version);
18455 	  return NULL;
18456 	}
18457       mac_ptr += 2;
18458 
18459       flags = read_1_byte (abfd, mac_ptr);
18460       ++mac_ptr;
18461       *offset_size = (flags & 1) ? 8 : 4;
18462 
18463       if ((flags & 2) != 0)
18464 	/* We don't need the line table offset.  */
18465 	mac_ptr += *offset_size;
18466 
18467       /* Vendor opcode descriptions.  */
18468       if ((flags & 4) != 0)
18469 	{
18470 	  unsigned int i, count;
18471 
18472 	  count = read_1_byte (abfd, mac_ptr);
18473 	  ++mac_ptr;
18474 	  for (i = 0; i < count; ++i)
18475 	    {
18476 	      unsigned int opcode, bytes_read;
18477 	      unsigned long arg;
18478 
18479 	      opcode = read_1_byte (abfd, mac_ptr);
18480 	      ++mac_ptr;
18481 	      opcode_definitions[opcode] = mac_ptr;
18482 	      arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18483 	      mac_ptr += bytes_read;
18484 	      mac_ptr += arg;
18485 	    }
18486 	}
18487     }
18488 
18489   return mac_ptr;
18490 }
18491 
18492 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18493    including DW_MACRO_GNU_transparent_include.  */
18494 
18495 static void
18496 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18497 			  struct macro_source_file *current_file,
18498 			  struct line_header *lh, const char *comp_dir,
18499 			  struct dwarf2_section_info *section,
18500 			  int section_is_gnu, int section_is_dwz,
18501 			  unsigned int offset_size,
18502 			  struct objfile *objfile,
18503 			  htab_t include_hash)
18504 {
18505   enum dwarf_macro_record_type macinfo_type;
18506   int at_commandline;
18507   gdb_byte *opcode_definitions[256];
18508 
18509   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18510 				      &offset_size, section_is_gnu);
18511   if (mac_ptr == NULL)
18512     {
18513       /* We already issued a complaint.  */
18514       return;
18515     }
18516 
18517   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
18518      GDB is still reading the definitions from command line.  First
18519      DW_MACINFO_start_file will need to be ignored as it was already executed
18520      to create CURRENT_FILE for the main source holding also the command line
18521      definitions.  On first met DW_MACINFO_start_file this flag is reset to
18522      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
18523 
18524   at_commandline = 1;
18525 
18526   do
18527     {
18528       /* Do we at least have room for a macinfo type byte?  */
18529       if (mac_ptr >= mac_end)
18530 	{
18531 	  dwarf2_section_buffer_overflow_complaint (section);
18532 	  break;
18533 	}
18534 
18535       macinfo_type = read_1_byte (abfd, mac_ptr);
18536       mac_ptr++;
18537 
18538       /* Note that we rely on the fact that the corresponding GNU and
18539 	 DWARF constants are the same.  */
18540       switch (macinfo_type)
18541 	{
18542 	  /* A zero macinfo type indicates the end of the macro
18543 	     information.  */
18544 	case 0:
18545 	  break;
18546 
18547         case DW_MACRO_GNU_define:
18548         case DW_MACRO_GNU_undef:
18549 	case DW_MACRO_GNU_define_indirect:
18550 	case DW_MACRO_GNU_undef_indirect:
18551 	case DW_MACRO_GNU_define_indirect_alt:
18552 	case DW_MACRO_GNU_undef_indirect_alt:
18553           {
18554             unsigned int bytes_read;
18555             int line;
18556             char *body;
18557 	    int is_define;
18558 
18559 	    line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18560 	    mac_ptr += bytes_read;
18561 
18562 	    if (macinfo_type == DW_MACRO_GNU_define
18563 		|| macinfo_type == DW_MACRO_GNU_undef)
18564 	      {
18565 		body = read_direct_string (abfd, mac_ptr, &bytes_read);
18566 		mac_ptr += bytes_read;
18567 	      }
18568 	    else
18569 	      {
18570 		LONGEST str_offset;
18571 
18572 		str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18573 		mac_ptr += offset_size;
18574 
18575 		if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18576 		    || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18577 		    || section_is_dwz)
18578 		  {
18579 		    struct dwz_file *dwz = dwarf2_get_dwz_file ();
18580 
18581 		    body = read_indirect_string_from_dwz (dwz, str_offset);
18582 		  }
18583 		else
18584 		  body = read_indirect_string_at_offset (abfd, str_offset);
18585 	      }
18586 
18587 	    is_define = (macinfo_type == DW_MACRO_GNU_define
18588 			 || macinfo_type == DW_MACRO_GNU_define_indirect
18589 			 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18590             if (! current_file)
18591 	      {
18592 		/* DWARF violation as no main source is present.  */
18593 		complaint (&symfile_complaints,
18594 			   _("debug info with no main source gives macro %s "
18595 			     "on line %d: %s"),
18596 			   is_define ? _("definition") : _("undefinition"),
18597 			   line, body);
18598 		break;
18599 	      }
18600 	    if ((line == 0 && !at_commandline)
18601 		|| (line != 0 && at_commandline))
18602 	      complaint (&symfile_complaints,
18603 			 _("debug info gives %s macro %s with %s line %d: %s"),
18604 			 at_commandline ? _("command-line") : _("in-file"),
18605 			 is_define ? _("definition") : _("undefinition"),
18606 			 line == 0 ? _("zero") : _("non-zero"), line, body);
18607 
18608 	    if (is_define)
18609 	      parse_macro_definition (current_file, line, body);
18610 	    else
18611 	      {
18612 		gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18613 			    || macinfo_type == DW_MACRO_GNU_undef_indirect
18614 			    || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18615 		macro_undef (current_file, line, body);
18616 	      }
18617           }
18618           break;
18619 
18620         case DW_MACRO_GNU_start_file:
18621           {
18622             unsigned int bytes_read;
18623             int line, file;
18624 
18625             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18626             mac_ptr += bytes_read;
18627             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18628             mac_ptr += bytes_read;
18629 
18630 	    if ((line == 0 && !at_commandline)
18631 		|| (line != 0 && at_commandline))
18632 	      complaint (&symfile_complaints,
18633 			 _("debug info gives source %d included "
18634 			   "from %s at %s line %d"),
18635 			 file, at_commandline ? _("command-line") : _("file"),
18636 			 line == 0 ? _("zero") : _("non-zero"), line);
18637 
18638 	    if (at_commandline)
18639 	      {
18640 		/* This DW_MACRO_GNU_start_file was executed in the
18641 		   pass one.  */
18642 		at_commandline = 0;
18643 	      }
18644 	    else
18645 	      current_file = macro_start_file (file, line,
18646 					       current_file, comp_dir,
18647 					       lh, objfile);
18648           }
18649           break;
18650 
18651         case DW_MACRO_GNU_end_file:
18652           if (! current_file)
18653 	    complaint (&symfile_complaints,
18654 		       _("macro debug info has an unmatched "
18655 			 "`close_file' directive"));
18656           else
18657             {
18658               current_file = current_file->included_by;
18659               if (! current_file)
18660                 {
18661                   enum dwarf_macro_record_type next_type;
18662 
18663                   /* GCC circa March 2002 doesn't produce the zero
18664                      type byte marking the end of the compilation
18665                      unit.  Complain if it's not there, but exit no
18666                      matter what.  */
18667 
18668                   /* Do we at least have room for a macinfo type byte?  */
18669                   if (mac_ptr >= mac_end)
18670                     {
18671 		      dwarf2_section_buffer_overflow_complaint (section);
18672                       return;
18673                     }
18674 
18675                   /* We don't increment mac_ptr here, so this is just
18676                      a look-ahead.  */
18677                   next_type = read_1_byte (abfd, mac_ptr);
18678                   if (next_type != 0)
18679 		    complaint (&symfile_complaints,
18680 			       _("no terminating 0-type entry for "
18681 				 "macros in `.debug_macinfo' section"));
18682 
18683                   return;
18684                 }
18685             }
18686           break;
18687 
18688 	case DW_MACRO_GNU_transparent_include:
18689 	case DW_MACRO_GNU_transparent_include_alt:
18690 	  {
18691 	    LONGEST offset;
18692 	    void **slot;
18693 	    bfd *include_bfd = abfd;
18694 	    struct dwarf2_section_info *include_section = section;
18695 	    struct dwarf2_section_info alt_section;
18696 	    gdb_byte *include_mac_end = mac_end;
18697 	    int is_dwz = section_is_dwz;
18698 	    gdb_byte *new_mac_ptr;
18699 
18700 	    offset = read_offset_1 (abfd, mac_ptr, offset_size);
18701 	    mac_ptr += offset_size;
18702 
18703 	    if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18704 	      {
18705 		struct dwz_file *dwz = dwarf2_get_dwz_file ();
18706 
18707 		dwarf2_read_section (dwarf2_per_objfile->objfile,
18708 				     &dwz->macro);
18709 
18710 		include_bfd = dwz->macro.asection->owner;
18711 		include_section = &dwz->macro;
18712 		include_mac_end = dwz->macro.buffer + dwz->macro.size;
18713 		is_dwz = 1;
18714 	      }
18715 
18716 	    new_mac_ptr = include_section->buffer + offset;
18717 	    slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18718 
18719 	    if (*slot != NULL)
18720 	      {
18721 		/* This has actually happened; see
18722 		   http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
18723 		complaint (&symfile_complaints,
18724 			   _("recursive DW_MACRO_GNU_transparent_include in "
18725 			     ".debug_macro section"));
18726 	      }
18727 	    else
18728 	      {
18729 		*slot = new_mac_ptr;
18730 
18731 		dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18732 					  include_mac_end, current_file,
18733 					  lh, comp_dir,
18734 					  section, section_is_gnu, is_dwz,
18735 					  offset_size, objfile, include_hash);
18736 
18737 		htab_remove_elt (include_hash, new_mac_ptr);
18738 	      }
18739 	  }
18740 	  break;
18741 
18742         case DW_MACINFO_vendor_ext:
18743 	  if (!section_is_gnu)
18744 	    {
18745 	      unsigned int bytes_read;
18746 	      int constant;
18747 
18748 	      constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18749 	      mac_ptr += bytes_read;
18750 	      read_direct_string (abfd, mac_ptr, &bytes_read);
18751 	      mac_ptr += bytes_read;
18752 
18753 	      /* We don't recognize any vendor extensions.  */
18754 	      break;
18755 	    }
18756 	  /* FALLTHROUGH */
18757 
18758 	default:
18759 	  mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18760 					 mac_ptr, mac_end, abfd, offset_size,
18761 					 section);
18762 	  if (mac_ptr == NULL)
18763 	    return;
18764 	  break;
18765         }
18766     } while (macinfo_type != 0);
18767 }
18768 
18769 static void
18770 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18771                      const char *comp_dir, int section_is_gnu)
18772 {
18773   struct objfile *objfile = dwarf2_per_objfile->objfile;
18774   struct line_header *lh = cu->line_header;
18775   bfd *abfd;
18776   gdb_byte *mac_ptr, *mac_end;
18777   struct macro_source_file *current_file = 0;
18778   enum dwarf_macro_record_type macinfo_type;
18779   unsigned int offset_size = cu->header.offset_size;
18780   gdb_byte *opcode_definitions[256];
18781   struct cleanup *cleanup;
18782   htab_t include_hash;
18783   void **slot;
18784   struct dwarf2_section_info *section;
18785   const char *section_name;
18786 
18787   if (cu->dwo_unit != NULL)
18788     {
18789       if (section_is_gnu)
18790 	{
18791 	  section = &cu->dwo_unit->dwo_file->sections.macro;
18792 	  section_name = ".debug_macro.dwo";
18793 	}
18794       else
18795 	{
18796 	  section = &cu->dwo_unit->dwo_file->sections.macinfo;
18797 	  section_name = ".debug_macinfo.dwo";
18798 	}
18799     }
18800   else
18801     {
18802       if (section_is_gnu)
18803 	{
18804 	  section = &dwarf2_per_objfile->macro;
18805 	  section_name = ".debug_macro";
18806 	}
18807       else
18808 	{
18809 	  section = &dwarf2_per_objfile->macinfo;
18810 	  section_name = ".debug_macinfo";
18811 	}
18812     }
18813 
18814   dwarf2_read_section (objfile, section);
18815   if (section->buffer == NULL)
18816     {
18817       complaint (&symfile_complaints, _("missing %s section"), section_name);
18818       return;
18819     }
18820   abfd = section->asection->owner;
18821 
18822   /* First pass: Find the name of the base filename.
18823      This filename is needed in order to process all macros whose definition
18824      (or undefinition) comes from the command line.  These macros are defined
18825      before the first DW_MACINFO_start_file entry, and yet still need to be
18826      associated to the base file.
18827 
18828      To determine the base file name, we scan the macro definitions until we
18829      reach the first DW_MACINFO_start_file entry.  We then initialize
18830      CURRENT_FILE accordingly so that any macro definition found before the
18831      first DW_MACINFO_start_file can still be associated to the base file.  */
18832 
18833   mac_ptr = section->buffer + offset;
18834   mac_end = section->buffer + section->size;
18835 
18836   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18837 				      &offset_size, section_is_gnu);
18838   if (mac_ptr == NULL)
18839     {
18840       /* We already issued a complaint.  */
18841       return;
18842     }
18843 
18844   do
18845     {
18846       /* Do we at least have room for a macinfo type byte?  */
18847       if (mac_ptr >= mac_end)
18848         {
18849 	  /* Complaint is printed during the second pass as GDB will probably
18850 	     stop the first pass earlier upon finding
18851 	     DW_MACINFO_start_file.  */
18852 	  break;
18853         }
18854 
18855       macinfo_type = read_1_byte (abfd, mac_ptr);
18856       mac_ptr++;
18857 
18858       /* Note that we rely on the fact that the corresponding GNU and
18859 	 DWARF constants are the same.  */
18860       switch (macinfo_type)
18861         {
18862           /* A zero macinfo type indicates the end of the macro
18863              information.  */
18864         case 0:
18865 	  break;
18866 
18867 	case DW_MACRO_GNU_define:
18868 	case DW_MACRO_GNU_undef:
18869 	  /* Only skip the data by MAC_PTR.  */
18870 	  {
18871 	    unsigned int bytes_read;
18872 
18873 	    read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18874 	    mac_ptr += bytes_read;
18875 	    read_direct_string (abfd, mac_ptr, &bytes_read);
18876 	    mac_ptr += bytes_read;
18877 	  }
18878 	  break;
18879 
18880 	case DW_MACRO_GNU_start_file:
18881 	  {
18882 	    unsigned int bytes_read;
18883 	    int line, file;
18884 
18885 	    line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18886 	    mac_ptr += bytes_read;
18887 	    file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18888 	    mac_ptr += bytes_read;
18889 
18890 	    current_file = macro_start_file (file, line, current_file,
18891 					     comp_dir, lh, objfile);
18892 	  }
18893 	  break;
18894 
18895 	case DW_MACRO_GNU_end_file:
18896 	  /* No data to skip by MAC_PTR.  */
18897 	  break;
18898 
18899 	case DW_MACRO_GNU_define_indirect:
18900 	case DW_MACRO_GNU_undef_indirect:
18901 	case DW_MACRO_GNU_define_indirect_alt:
18902 	case DW_MACRO_GNU_undef_indirect_alt:
18903 	  {
18904 	    unsigned int bytes_read;
18905 
18906 	    read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18907 	    mac_ptr += bytes_read;
18908 	    mac_ptr += offset_size;
18909 	  }
18910 	  break;
18911 
18912 	case DW_MACRO_GNU_transparent_include:
18913 	case DW_MACRO_GNU_transparent_include_alt:
18914 	  /* Note that, according to the spec, a transparent include
18915 	     chain cannot call DW_MACRO_GNU_start_file.  So, we can just
18916 	     skip this opcode.  */
18917 	  mac_ptr += offset_size;
18918 	  break;
18919 
18920 	case DW_MACINFO_vendor_ext:
18921 	  /* Only skip the data by MAC_PTR.  */
18922 	  if (!section_is_gnu)
18923 	    {
18924 	      unsigned int bytes_read;
18925 
18926 	      read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18927 	      mac_ptr += bytes_read;
18928 	      read_direct_string (abfd, mac_ptr, &bytes_read);
18929 	      mac_ptr += bytes_read;
18930 	    }
18931 	  /* FALLTHROUGH */
18932 
18933 	default:
18934 	  mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18935 					 mac_ptr, mac_end, abfd, offset_size,
18936 					 section);
18937 	  if (mac_ptr == NULL)
18938 	    return;
18939 	  break;
18940 	}
18941     } while (macinfo_type != 0 && current_file == NULL);
18942 
18943   /* Second pass: Process all entries.
18944 
18945      Use the AT_COMMAND_LINE flag to determine whether we are still processing
18946      command-line macro definitions/undefinitions.  This flag is unset when we
18947      reach the first DW_MACINFO_start_file entry.  */
18948 
18949   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18950 				    NULL, xcalloc, xfree);
18951   cleanup = make_cleanup_htab_delete (include_hash);
18952   mac_ptr = section->buffer + offset;
18953   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18954   *slot = mac_ptr;
18955   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18956 			    current_file, lh, comp_dir, section,
18957 			    section_is_gnu, 0,
18958 			    offset_size, objfile, include_hash);
18959   do_cleanups (cleanup);
18960 }
18961 
18962 /* Check if the attribute's form is a DW_FORM_block*
18963    if so return true else false.  */
18964 
18965 static int
18966 attr_form_is_block (struct attribute *attr)
18967 {
18968   return (attr == NULL ? 0 :
18969       attr->form == DW_FORM_block1
18970       || attr->form == DW_FORM_block2
18971       || attr->form == DW_FORM_block4
18972       || attr->form == DW_FORM_block
18973       || attr->form == DW_FORM_exprloc);
18974 }
18975 
18976 /* Return non-zero if ATTR's value is a section offset --- classes
18977    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
18978    You may use DW_UNSND (attr) to retrieve such offsets.
18979 
18980    Section 7.5.4, "Attribute Encodings", explains that no attribute
18981    may have a value that belongs to more than one of these classes; it
18982    would be ambiguous if we did, because we use the same forms for all
18983    of them.  */
18984 
18985 static int
18986 attr_form_is_section_offset (struct attribute *attr)
18987 {
18988   return (attr->form == DW_FORM_data4
18989           || attr->form == DW_FORM_data8
18990 	  || attr->form == DW_FORM_sec_offset);
18991 }
18992 
18993 /* Return non-zero if ATTR's value falls in the 'constant' class, or
18994    zero otherwise.  When this function returns true, you can apply
18995    dwarf2_get_attr_constant_value to it.
18996 
18997    However, note that for some attributes you must check
18998    attr_form_is_section_offset before using this test.  DW_FORM_data4
18999    and DW_FORM_data8 are members of both the constant class, and of
19000    the classes that contain offsets into other debug sections
19001    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
19002    that, if an attribute's can be either a constant or one of the
19003    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19004    taken as section offsets, not constants.  */
19005 
19006 static int
19007 attr_form_is_constant (struct attribute *attr)
19008 {
19009   switch (attr->form)
19010     {
19011     case DW_FORM_sdata:
19012     case DW_FORM_udata:
19013     case DW_FORM_data1:
19014     case DW_FORM_data2:
19015     case DW_FORM_data4:
19016     case DW_FORM_data8:
19017       return 1;
19018     default:
19019       return 0;
19020     }
19021 }
19022 
19023 /* Return the .debug_loc section to use for CU.
19024    For DWO files use .debug_loc.dwo.  */
19025 
19026 static struct dwarf2_section_info *
19027 cu_debug_loc_section (struct dwarf2_cu *cu)
19028 {
19029   if (cu->dwo_unit)
19030     return &cu->dwo_unit->dwo_file->sections.loc;
19031   return &dwarf2_per_objfile->loc;
19032 }
19033 
19034 /* A helper function that fills in a dwarf2_loclist_baton.  */
19035 
19036 static void
19037 fill_in_loclist_baton (struct dwarf2_cu *cu,
19038 		       struct dwarf2_loclist_baton *baton,
19039 		       struct attribute *attr)
19040 {
19041   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19042 
19043   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19044 
19045   baton->per_cu = cu->per_cu;
19046   gdb_assert (baton->per_cu);
19047   /* We don't know how long the location list is, but make sure we
19048      don't run off the edge of the section.  */
19049   baton->size = section->size - DW_UNSND (attr);
19050   baton->data = section->buffer + DW_UNSND (attr);
19051   baton->base_address = cu->base_address;
19052   baton->from_dwo = cu->dwo_unit != NULL;
19053 }
19054 
19055 static void
19056 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19057 			     struct dwarf2_cu *cu)
19058 {
19059   struct objfile *objfile = dwarf2_per_objfile->objfile;
19060   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19061 
19062   if (attr_form_is_section_offset (attr)
19063       /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19064 	 the section.  If so, fall through to the complaint in the
19065 	 other branch.  */
19066       && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19067     {
19068       struct dwarf2_loclist_baton *baton;
19069 
19070       baton = obstack_alloc (&objfile->objfile_obstack,
19071 			     sizeof (struct dwarf2_loclist_baton));
19072 
19073       fill_in_loclist_baton (cu, baton, attr);
19074 
19075       if (cu->base_known == 0)
19076 	complaint (&symfile_complaints,
19077 		   _("Location list used without "
19078 		     "specifying the CU base address."));
19079 
19080       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
19081       SYMBOL_LOCATION_BATON (sym) = baton;
19082     }
19083   else
19084     {
19085       struct dwarf2_locexpr_baton *baton;
19086 
19087       baton = obstack_alloc (&objfile->objfile_obstack,
19088 			     sizeof (struct dwarf2_locexpr_baton));
19089       baton->per_cu = cu->per_cu;
19090       gdb_assert (baton->per_cu);
19091 
19092       if (attr_form_is_block (attr))
19093 	{
19094 	  /* Note that we're just copying the block's data pointer
19095 	     here, not the actual data.  We're still pointing into the
19096 	     info_buffer for SYM's objfile; right now we never release
19097 	     that buffer, but when we do clean up properly this may
19098 	     need to change.  */
19099 	  baton->size = DW_BLOCK (attr)->size;
19100 	  baton->data = DW_BLOCK (attr)->data;
19101 	}
19102       else
19103 	{
19104 	  dwarf2_invalid_attrib_class_complaint ("location description",
19105 						 SYMBOL_NATURAL_NAME (sym));
19106 	  baton->size = 0;
19107 	}
19108 
19109       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
19110       SYMBOL_LOCATION_BATON (sym) = baton;
19111     }
19112 }
19113 
19114 /* Return the OBJFILE associated with the compilation unit CU.  If CU
19115    came from a separate debuginfo file, then the master objfile is
19116    returned.  */
19117 
19118 struct objfile *
19119 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19120 {
19121   struct objfile *objfile = per_cu->objfile;
19122 
19123   /* Return the master objfile, so that we can report and look up the
19124      correct file containing this variable.  */
19125   if (objfile->separate_debug_objfile_backlink)
19126     objfile = objfile->separate_debug_objfile_backlink;
19127 
19128   return objfile;
19129 }
19130 
19131 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19132    (CU_HEADERP is unused in such case) or prepare a temporary copy at
19133    CU_HEADERP first.  */
19134 
19135 static const struct comp_unit_head *
19136 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19137 		       struct dwarf2_per_cu_data *per_cu)
19138 {
19139   gdb_byte *info_ptr;
19140 
19141   if (per_cu->cu)
19142     return &per_cu->cu->header;
19143 
19144   info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
19145 
19146   memset (cu_headerp, 0, sizeof (*cu_headerp));
19147   read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19148 
19149   return cu_headerp;
19150 }
19151 
19152 /* Return the address size given in the compilation unit header for CU.  */
19153 
19154 int
19155 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19156 {
19157   struct comp_unit_head cu_header_local;
19158   const struct comp_unit_head *cu_headerp;
19159 
19160   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19161 
19162   return cu_headerp->addr_size;
19163 }
19164 
19165 /* Return the offset size given in the compilation unit header for CU.  */
19166 
19167 int
19168 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19169 {
19170   struct comp_unit_head cu_header_local;
19171   const struct comp_unit_head *cu_headerp;
19172 
19173   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19174 
19175   return cu_headerp->offset_size;
19176 }
19177 
19178 /* See its dwarf2loc.h declaration.  */
19179 
19180 int
19181 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19182 {
19183   struct comp_unit_head cu_header_local;
19184   const struct comp_unit_head *cu_headerp;
19185 
19186   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19187 
19188   if (cu_headerp->version == 2)
19189     return cu_headerp->addr_size;
19190   else
19191     return cu_headerp->offset_size;
19192 }
19193 
19194 /* Return the text offset of the CU.  The returned offset comes from
19195    this CU's objfile.  If this objfile came from a separate debuginfo
19196    file, then the offset may be different from the corresponding
19197    offset in the parent objfile.  */
19198 
19199 CORE_ADDR
19200 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19201 {
19202   struct objfile *objfile = per_cu->objfile;
19203 
19204   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19205 }
19206 
19207 /* Locate the .debug_info compilation unit from CU's objfile which contains
19208    the DIE at OFFSET.  Raises an error on failure.  */
19209 
19210 static struct dwarf2_per_cu_data *
19211 dwarf2_find_containing_comp_unit (sect_offset offset,
19212 				  unsigned int offset_in_dwz,
19213 				  struct objfile *objfile)
19214 {
19215   struct dwarf2_per_cu_data *this_cu;
19216   int low, high;
19217   const sect_offset *cu_off;
19218 
19219   low = 0;
19220   high = dwarf2_per_objfile->n_comp_units - 1;
19221   while (high > low)
19222     {
19223       struct dwarf2_per_cu_data *mid_cu;
19224       int mid = low + (high - low) / 2;
19225 
19226       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19227       cu_off = &mid_cu->offset;
19228       if (mid_cu->is_dwz > offset_in_dwz
19229 	  || (mid_cu->is_dwz == offset_in_dwz
19230 	      && cu_off->sect_off >= offset.sect_off))
19231 	high = mid;
19232       else
19233 	low = mid + 1;
19234     }
19235   gdb_assert (low == high);
19236   this_cu = dwarf2_per_objfile->all_comp_units[low];
19237   cu_off = &this_cu->offset;
19238   if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19239     {
19240       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19241 	error (_("Dwarf Error: could not find partial DIE containing "
19242 	       "offset 0x%lx [in module %s]"),
19243 	       (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19244 
19245       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19246 		  <= offset.sect_off);
19247       return dwarf2_per_objfile->all_comp_units[low-1];
19248     }
19249   else
19250     {
19251       this_cu = dwarf2_per_objfile->all_comp_units[low];
19252       if (low == dwarf2_per_objfile->n_comp_units - 1
19253 	  && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19254 	error (_("invalid dwarf2 offset %u"), offset.sect_off);
19255       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19256       return this_cu;
19257     }
19258 }
19259 
19260 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
19261 
19262 static void
19263 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19264 {
19265   memset (cu, 0, sizeof (*cu));
19266   per_cu->cu = cu;
19267   cu->per_cu = per_cu;
19268   cu->objfile = per_cu->objfile;
19269   obstack_init (&cu->comp_unit_obstack);
19270 }
19271 
19272 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
19273 
19274 static void
19275 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19276 		       enum language pretend_language)
19277 {
19278   struct attribute *attr;
19279 
19280   /* Set the language we're debugging.  */
19281   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19282   if (attr)
19283     set_cu_language (DW_UNSND (attr), cu);
19284   else
19285     {
19286       cu->language = pretend_language;
19287       cu->language_defn = language_def (cu->language);
19288     }
19289 
19290   attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19291   if (attr)
19292     cu->producer = DW_STRING (attr);
19293 }
19294 
19295 /* Release one cached compilation unit, CU.  We unlink it from the tree
19296    of compilation units, but we don't remove it from the read_in_chain;
19297    the caller is responsible for that.
19298    NOTE: DATA is a void * because this function is also used as a
19299    cleanup routine.  */
19300 
19301 static void
19302 free_heap_comp_unit (void *data)
19303 {
19304   struct dwarf2_cu *cu = data;
19305 
19306   gdb_assert (cu->per_cu != NULL);
19307   cu->per_cu->cu = NULL;
19308   cu->per_cu = NULL;
19309 
19310   obstack_free (&cu->comp_unit_obstack, NULL);
19311 
19312   xfree (cu);
19313 }
19314 
19315 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19316    when we're finished with it.  We can't free the pointer itself, but be
19317    sure to unlink it from the cache.  Also release any associated storage.  */
19318 
19319 static void
19320 free_stack_comp_unit (void *data)
19321 {
19322   struct dwarf2_cu *cu = data;
19323 
19324   gdb_assert (cu->per_cu != NULL);
19325   cu->per_cu->cu = NULL;
19326   cu->per_cu = NULL;
19327 
19328   obstack_free (&cu->comp_unit_obstack, NULL);
19329   cu->partial_dies = NULL;
19330 }
19331 
19332 /* Free all cached compilation units.  */
19333 
19334 static void
19335 free_cached_comp_units (void *data)
19336 {
19337   struct dwarf2_per_cu_data *per_cu, **last_chain;
19338 
19339   per_cu = dwarf2_per_objfile->read_in_chain;
19340   last_chain = &dwarf2_per_objfile->read_in_chain;
19341   while (per_cu != NULL)
19342     {
19343       struct dwarf2_per_cu_data *next_cu;
19344 
19345       next_cu = per_cu->cu->read_in_chain;
19346 
19347       free_heap_comp_unit (per_cu->cu);
19348       *last_chain = next_cu;
19349 
19350       per_cu = next_cu;
19351     }
19352 }
19353 
19354 /* Increase the age counter on each cached compilation unit, and free
19355    any that are too old.  */
19356 
19357 static void
19358 age_cached_comp_units (void)
19359 {
19360   struct dwarf2_per_cu_data *per_cu, **last_chain;
19361 
19362   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19363   per_cu = dwarf2_per_objfile->read_in_chain;
19364   while (per_cu != NULL)
19365     {
19366       per_cu->cu->last_used ++;
19367       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19368 	dwarf2_mark (per_cu->cu);
19369       per_cu = per_cu->cu->read_in_chain;
19370     }
19371 
19372   per_cu = dwarf2_per_objfile->read_in_chain;
19373   last_chain = &dwarf2_per_objfile->read_in_chain;
19374   while (per_cu != NULL)
19375     {
19376       struct dwarf2_per_cu_data *next_cu;
19377 
19378       next_cu = per_cu->cu->read_in_chain;
19379 
19380       if (!per_cu->cu->mark)
19381 	{
19382 	  free_heap_comp_unit (per_cu->cu);
19383 	  *last_chain = next_cu;
19384 	}
19385       else
19386 	last_chain = &per_cu->cu->read_in_chain;
19387 
19388       per_cu = next_cu;
19389     }
19390 }
19391 
19392 /* Remove a single compilation unit from the cache.  */
19393 
19394 static void
19395 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19396 {
19397   struct dwarf2_per_cu_data *per_cu, **last_chain;
19398 
19399   per_cu = dwarf2_per_objfile->read_in_chain;
19400   last_chain = &dwarf2_per_objfile->read_in_chain;
19401   while (per_cu != NULL)
19402     {
19403       struct dwarf2_per_cu_data *next_cu;
19404 
19405       next_cu = per_cu->cu->read_in_chain;
19406 
19407       if (per_cu == target_per_cu)
19408 	{
19409 	  free_heap_comp_unit (per_cu->cu);
19410 	  per_cu->cu = NULL;
19411 	  *last_chain = next_cu;
19412 	  break;
19413 	}
19414       else
19415 	last_chain = &per_cu->cu->read_in_chain;
19416 
19417       per_cu = next_cu;
19418     }
19419 }
19420 
19421 /* Release all extra memory associated with OBJFILE.  */
19422 
19423 void
19424 dwarf2_free_objfile (struct objfile *objfile)
19425 {
19426   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19427 
19428   if (dwarf2_per_objfile == NULL)
19429     return;
19430 
19431   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
19432   free_cached_comp_units (NULL);
19433 
19434   if (dwarf2_per_objfile->quick_file_names_table)
19435     htab_delete (dwarf2_per_objfile->quick_file_names_table);
19436 
19437   /* Everything else should be on the objfile obstack.  */
19438 }
19439 
19440 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19441    We store these in a hash table separate from the DIEs, and preserve them
19442    when the DIEs are flushed out of cache.
19443 
19444    The CU "per_cu" pointer is needed because offset alone is not enough to
19445    uniquely identify the type.  A file may have multiple .debug_types sections,
19446    or the type may come from a DWO file.  We have to use something in
19447    dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19448    routine, get_die_type_at_offset, from outside this file, and thus won't
19449    necessarily have PER_CU->cu.  Fortunately, PER_CU is stable for the life
19450    of the objfile.  */
19451 
19452 struct dwarf2_per_cu_offset_and_type
19453 {
19454   const struct dwarf2_per_cu_data *per_cu;
19455   sect_offset offset;
19456   struct type *type;
19457 };
19458 
19459 /* Hash function for a dwarf2_per_cu_offset_and_type.  */
19460 
19461 static hashval_t
19462 per_cu_offset_and_type_hash (const void *item)
19463 {
19464   const struct dwarf2_per_cu_offset_and_type *ofs = item;
19465 
19466   return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19467 }
19468 
19469 /* Equality function for a dwarf2_per_cu_offset_and_type.  */
19470 
19471 static int
19472 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19473 {
19474   const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19475   const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19476 
19477   return (ofs_lhs->per_cu == ofs_rhs->per_cu
19478 	  && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19479 }
19480 
19481 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
19482    table if necessary.  For convenience, return TYPE.
19483 
19484    The DIEs reading must have careful ordering to:
19485     * Not cause infite loops trying to read in DIEs as a prerequisite for
19486       reading current DIE.
19487     * Not trying to dereference contents of still incompletely read in types
19488       while reading in other DIEs.
19489     * Enable referencing still incompletely read in types just by a pointer to
19490       the type without accessing its fields.
19491 
19492    Therefore caller should follow these rules:
19493      * Try to fetch any prerequisite types we may need to build this DIE type
19494        before building the type and calling set_die_type.
19495      * After building type call set_die_type for current DIE as soon as
19496        possible before fetching more types to complete the current type.
19497      * Make the type as complete as possible before fetching more types.  */
19498 
19499 static struct type *
19500 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19501 {
19502   struct dwarf2_per_cu_offset_and_type **slot, ofs;
19503   struct objfile *objfile = cu->objfile;
19504 
19505   /* For Ada types, make sure that the gnat-specific data is always
19506      initialized (if not already set).  There are a few types where
19507      we should not be doing so, because the type-specific area is
19508      already used to hold some other piece of info (eg: TYPE_CODE_FLT
19509      where the type-specific area is used to store the floatformat).
19510      But this is not a problem, because the gnat-specific information
19511      is actually not needed for these types.  */
19512   if (need_gnat_info (cu)
19513       && TYPE_CODE (type) != TYPE_CODE_FUNC
19514       && TYPE_CODE (type) != TYPE_CODE_FLT
19515       && !HAVE_GNAT_AUX_INFO (type))
19516     INIT_GNAT_SPECIFIC (type);
19517 
19518   if (dwarf2_per_objfile->die_type_hash == NULL)
19519     {
19520       dwarf2_per_objfile->die_type_hash =
19521 	htab_create_alloc_ex (127,
19522 			      per_cu_offset_and_type_hash,
19523 			      per_cu_offset_and_type_eq,
19524 			      NULL,
19525 			      &objfile->objfile_obstack,
19526 			      hashtab_obstack_allocate,
19527 			      dummy_obstack_deallocate);
19528     }
19529 
19530   ofs.per_cu = cu->per_cu;
19531   ofs.offset = die->offset;
19532   ofs.type = type;
19533   slot = (struct dwarf2_per_cu_offset_and_type **)
19534     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19535   if (*slot)
19536     complaint (&symfile_complaints,
19537 	       _("A problem internal to GDB: DIE 0x%x has type already set"),
19538 	       die->offset.sect_off);
19539   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19540   **slot = ofs;
19541   return type;
19542 }
19543 
19544 /* Look up the type for the die at OFFSET in the appropriate type_hash
19545    table, or return NULL if the die does not have a saved type.  */
19546 
19547 static struct type *
19548 get_die_type_at_offset (sect_offset offset,
19549 			struct dwarf2_per_cu_data *per_cu)
19550 {
19551   struct dwarf2_per_cu_offset_and_type *slot, ofs;
19552 
19553   if (dwarf2_per_objfile->die_type_hash == NULL)
19554     return NULL;
19555 
19556   ofs.per_cu = per_cu;
19557   ofs.offset = offset;
19558   slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19559   if (slot)
19560     return slot->type;
19561   else
19562     return NULL;
19563 }
19564 
19565 /* Look up the type for DIE in the appropriate type_hash table,
19566    or return NULL if DIE does not have a saved type.  */
19567 
19568 static struct type *
19569 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19570 {
19571   return get_die_type_at_offset (die->offset, cu->per_cu);
19572 }
19573 
19574 /* Add a dependence relationship from CU to REF_PER_CU.  */
19575 
19576 static void
19577 dwarf2_add_dependence (struct dwarf2_cu *cu,
19578 		       struct dwarf2_per_cu_data *ref_per_cu)
19579 {
19580   void **slot;
19581 
19582   if (cu->dependencies == NULL)
19583     cu->dependencies
19584       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19585 			      NULL, &cu->comp_unit_obstack,
19586 			      hashtab_obstack_allocate,
19587 			      dummy_obstack_deallocate);
19588 
19589   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19590   if (*slot == NULL)
19591     *slot = ref_per_cu;
19592 }
19593 
19594 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19595    Set the mark field in every compilation unit in the
19596    cache that we must keep because we are keeping CU.  */
19597 
19598 static int
19599 dwarf2_mark_helper (void **slot, void *data)
19600 {
19601   struct dwarf2_per_cu_data *per_cu;
19602 
19603   per_cu = (struct dwarf2_per_cu_data *) *slot;
19604 
19605   /* cu->dependencies references may not yet have been ever read if QUIT aborts
19606      reading of the chain.  As such dependencies remain valid it is not much
19607      useful to track and undo them during QUIT cleanups.  */
19608   if (per_cu->cu == NULL)
19609     return 1;
19610 
19611   if (per_cu->cu->mark)
19612     return 1;
19613   per_cu->cu->mark = 1;
19614 
19615   if (per_cu->cu->dependencies != NULL)
19616     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19617 
19618   return 1;
19619 }
19620 
19621 /* Set the mark field in CU and in every other compilation unit in the
19622    cache that we must keep because we are keeping CU.  */
19623 
19624 static void
19625 dwarf2_mark (struct dwarf2_cu *cu)
19626 {
19627   if (cu->mark)
19628     return;
19629   cu->mark = 1;
19630   if (cu->dependencies != NULL)
19631     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19632 }
19633 
19634 static void
19635 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19636 {
19637   while (per_cu)
19638     {
19639       per_cu->cu->mark = 0;
19640       per_cu = per_cu->cu->read_in_chain;
19641     }
19642 }
19643 
19644 /* Trivial hash function for partial_die_info: the hash value of a DIE
19645    is its offset in .debug_info for this objfile.  */
19646 
19647 static hashval_t
19648 partial_die_hash (const void *item)
19649 {
19650   const struct partial_die_info *part_die = item;
19651 
19652   return part_die->offset.sect_off;
19653 }
19654 
19655 /* Trivial comparison function for partial_die_info structures: two DIEs
19656    are equal if they have the same offset.  */
19657 
19658 static int
19659 partial_die_eq (const void *item_lhs, const void *item_rhs)
19660 {
19661   const struct partial_die_info *part_die_lhs = item_lhs;
19662   const struct partial_die_info *part_die_rhs = item_rhs;
19663 
19664   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19665 }
19666 
19667 static struct cmd_list_element *set_dwarf2_cmdlist;
19668 static struct cmd_list_element *show_dwarf2_cmdlist;
19669 
19670 static void
19671 set_dwarf2_cmd (char *args, int from_tty)
19672 {
19673   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19674 }
19675 
19676 static void
19677 show_dwarf2_cmd (char *args, int from_tty)
19678 {
19679   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19680 }
19681 
19682 /* Free data associated with OBJFILE, if necessary.  */
19683 
19684 static void
19685 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19686 {
19687   struct dwarf2_per_objfile *data = d;
19688   int ix;
19689 
19690   for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19691     VEC_free (dwarf2_per_cu_ptr,
19692 	      dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19693 
19694   for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19695     VEC_free (dwarf2_per_cu_ptr,
19696 	      dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19697 
19698   VEC_free (dwarf2_section_info_def, data->types);
19699 
19700   if (data->dwo_files)
19701     free_dwo_files (data->dwo_files, objfile);
19702   if (data->dwp_file)
19703     gdb_bfd_unref (data->dwp_file->dbfd);
19704 
19705   if (data->dwz_file && data->dwz_file->dwz_bfd)
19706     gdb_bfd_unref (data->dwz_file->dwz_bfd);
19707 }
19708 
19709 
19710 /* The "save gdb-index" command.  */
19711 
19712 /* The contents of the hash table we create when building the string
19713    table.  */
19714 struct strtab_entry
19715 {
19716   offset_type offset;
19717   const char *str;
19718 };
19719 
19720 /* Hash function for a strtab_entry.
19721 
19722    Function is used only during write_hash_table so no index format backward
19723    compatibility is needed.  */
19724 
19725 static hashval_t
19726 hash_strtab_entry (const void *e)
19727 {
19728   const struct strtab_entry *entry = e;
19729   return mapped_index_string_hash (INT_MAX, entry->str);
19730 }
19731 
19732 /* Equality function for a strtab_entry.  */
19733 
19734 static int
19735 eq_strtab_entry (const void *a, const void *b)
19736 {
19737   const struct strtab_entry *ea = a;
19738   const struct strtab_entry *eb = b;
19739   return !strcmp (ea->str, eb->str);
19740 }
19741 
19742 /* Create a strtab_entry hash table.  */
19743 
19744 static htab_t
19745 create_strtab (void)
19746 {
19747   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19748 			    xfree, xcalloc, xfree);
19749 }
19750 
19751 /* Add a string to the constant pool.  Return the string's offset in
19752    host order.  */
19753 
19754 static offset_type
19755 add_string (htab_t table, struct obstack *cpool, const char *str)
19756 {
19757   void **slot;
19758   struct strtab_entry entry;
19759   struct strtab_entry *result;
19760 
19761   entry.str = str;
19762   slot = htab_find_slot (table, &entry, INSERT);
19763   if (*slot)
19764     result = *slot;
19765   else
19766     {
19767       result = XNEW (struct strtab_entry);
19768       result->offset = obstack_object_size (cpool);
19769       result->str = str;
19770       obstack_grow_str0 (cpool, str);
19771       *slot = result;
19772     }
19773   return result->offset;
19774 }
19775 
19776 /* An entry in the symbol table.  */
19777 struct symtab_index_entry
19778 {
19779   /* The name of the symbol.  */
19780   const char *name;
19781   /* The offset of the name in the constant pool.  */
19782   offset_type index_offset;
19783   /* A sorted vector of the indices of all the CUs that hold an object
19784      of this name.  */
19785   VEC (offset_type) *cu_indices;
19786 };
19787 
19788 /* The symbol table.  This is a power-of-2-sized hash table.  */
19789 struct mapped_symtab
19790 {
19791   offset_type n_elements;
19792   offset_type size;
19793   struct symtab_index_entry **data;
19794 };
19795 
19796 /* Hash function for a symtab_index_entry.  */
19797 
19798 static hashval_t
19799 hash_symtab_entry (const void *e)
19800 {
19801   const struct symtab_index_entry *entry = e;
19802   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19803 			 sizeof (offset_type) * VEC_length (offset_type,
19804 							    entry->cu_indices),
19805 			 0);
19806 }
19807 
19808 /* Equality function for a symtab_index_entry.  */
19809 
19810 static int
19811 eq_symtab_entry (const void *a, const void *b)
19812 {
19813   const struct symtab_index_entry *ea = a;
19814   const struct symtab_index_entry *eb = b;
19815   int len = VEC_length (offset_type, ea->cu_indices);
19816   if (len != VEC_length (offset_type, eb->cu_indices))
19817     return 0;
19818   return !memcmp (VEC_address (offset_type, ea->cu_indices),
19819 		  VEC_address (offset_type, eb->cu_indices),
19820 		  sizeof (offset_type) * len);
19821 }
19822 
19823 /* Destroy a symtab_index_entry.  */
19824 
19825 static void
19826 delete_symtab_entry (void *p)
19827 {
19828   struct symtab_index_entry *entry = p;
19829   VEC_free (offset_type, entry->cu_indices);
19830   xfree (entry);
19831 }
19832 
19833 /* Create a hash table holding symtab_index_entry objects.  */
19834 
19835 static htab_t
19836 create_symbol_hash_table (void)
19837 {
19838   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19839 			    delete_symtab_entry, xcalloc, xfree);
19840 }
19841 
19842 /* Create a new mapped symtab object.  */
19843 
19844 static struct mapped_symtab *
19845 create_mapped_symtab (void)
19846 {
19847   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19848   symtab->n_elements = 0;
19849   symtab->size = 1024;
19850   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19851   return symtab;
19852 }
19853 
19854 /* Destroy a mapped_symtab.  */
19855 
19856 static void
19857 cleanup_mapped_symtab (void *p)
19858 {
19859   struct mapped_symtab *symtab = p;
19860   /* The contents of the array are freed when the other hash table is
19861      destroyed.  */
19862   xfree (symtab->data);
19863   xfree (symtab);
19864 }
19865 
19866 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
19867    the slot.
19868 
19869    Function is used only during write_hash_table so no index format backward
19870    compatibility is needed.  */
19871 
19872 static struct symtab_index_entry **
19873 find_slot (struct mapped_symtab *symtab, const char *name)
19874 {
19875   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19876 
19877   index = hash & (symtab->size - 1);
19878   step = ((hash * 17) & (symtab->size - 1)) | 1;
19879 
19880   for (;;)
19881     {
19882       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19883 	return &symtab->data[index];
19884       index = (index + step) & (symtab->size - 1);
19885     }
19886 }
19887 
19888 /* Expand SYMTAB's hash table.  */
19889 
19890 static void
19891 hash_expand (struct mapped_symtab *symtab)
19892 {
19893   offset_type old_size = symtab->size;
19894   offset_type i;
19895   struct symtab_index_entry **old_entries = symtab->data;
19896 
19897   symtab->size *= 2;
19898   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19899 
19900   for (i = 0; i < old_size; ++i)
19901     {
19902       if (old_entries[i])
19903 	{
19904 	  struct symtab_index_entry **slot = find_slot (symtab,
19905 							old_entries[i]->name);
19906 	  *slot = old_entries[i];
19907 	}
19908     }
19909 
19910   xfree (old_entries);
19911 }
19912 
19913 /* Add an entry to SYMTAB.  NAME is the name of the symbol.
19914    CU_INDEX is the index of the CU in which the symbol appears.
19915    IS_STATIC is one if the symbol is static, otherwise zero (global).  */
19916 
19917 static void
19918 add_index_entry (struct mapped_symtab *symtab, const char *name,
19919 		 int is_static, gdb_index_symbol_kind kind,
19920 		 offset_type cu_index)
19921 {
19922   struct symtab_index_entry **slot;
19923   offset_type cu_index_and_attrs;
19924 
19925   ++symtab->n_elements;
19926   if (4 * symtab->n_elements / 3 >= symtab->size)
19927     hash_expand (symtab);
19928 
19929   slot = find_slot (symtab, name);
19930   if (!*slot)
19931     {
19932       *slot = XNEW (struct symtab_index_entry);
19933       (*slot)->name = name;
19934       /* index_offset is set later.  */
19935       (*slot)->cu_indices = NULL;
19936     }
19937 
19938   cu_index_and_attrs = 0;
19939   DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19940   DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19941   DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19942 
19943   /* We don't want to record an index value twice as we want to avoid the
19944      duplication.
19945      We process all global symbols and then all static symbols
19946      (which would allow us to avoid the duplication by only having to check
19947      the last entry pushed), but a symbol could have multiple kinds in one CU.
19948      To keep things simple we don't worry about the duplication here and
19949      sort and uniqufy the list after we've processed all symbols.  */
19950   VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19951 }
19952 
19953 /* qsort helper routine for uniquify_cu_indices.  */
19954 
19955 static int
19956 offset_type_compare (const void *ap, const void *bp)
19957 {
19958   offset_type a = *(offset_type *) ap;
19959   offset_type b = *(offset_type *) bp;
19960 
19961   return (a > b) - (b > a);
19962 }
19963 
19964 /* Sort and remove duplicates of all symbols' cu_indices lists.  */
19965 
19966 static void
19967 uniquify_cu_indices (struct mapped_symtab *symtab)
19968 {
19969   int i;
19970 
19971   for (i = 0; i < symtab->size; ++i)
19972     {
19973       struct symtab_index_entry *entry = symtab->data[i];
19974 
19975       if (entry
19976 	  && entry->cu_indices != NULL)
19977 	{
19978 	  unsigned int next_to_insert, next_to_check;
19979 	  offset_type last_value;
19980 
19981 	  qsort (VEC_address (offset_type, entry->cu_indices),
19982 		 VEC_length (offset_type, entry->cu_indices),
19983 		 sizeof (offset_type), offset_type_compare);
19984 
19985 	  last_value = VEC_index (offset_type, entry->cu_indices, 0);
19986 	  next_to_insert = 1;
19987 	  for (next_to_check = 1;
19988 	       next_to_check < VEC_length (offset_type, entry->cu_indices);
19989 	       ++next_to_check)
19990 	    {
19991 	      if (VEC_index (offset_type, entry->cu_indices, next_to_check)
19992 		  != last_value)
19993 		{
19994 		  last_value = VEC_index (offset_type, entry->cu_indices,
19995 					  next_to_check);
19996 		  VEC_replace (offset_type, entry->cu_indices, next_to_insert,
19997 			       last_value);
19998 		  ++next_to_insert;
19999 		}
20000 	    }
20001 	  VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20002 	}
20003     }
20004 }
20005 
20006 /* Add a vector of indices to the constant pool.  */
20007 
20008 static offset_type
20009 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20010 		      struct symtab_index_entry *entry)
20011 {
20012   void **slot;
20013 
20014   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20015   if (!*slot)
20016     {
20017       offset_type len = VEC_length (offset_type, entry->cu_indices);
20018       offset_type val = MAYBE_SWAP (len);
20019       offset_type iter;
20020       int i;
20021 
20022       *slot = entry;
20023       entry->index_offset = obstack_object_size (cpool);
20024 
20025       obstack_grow (cpool, &val, sizeof (val));
20026       for (i = 0;
20027 	   VEC_iterate (offset_type, entry->cu_indices, i, iter);
20028 	   ++i)
20029 	{
20030 	  val = MAYBE_SWAP (iter);
20031 	  obstack_grow (cpool, &val, sizeof (val));
20032 	}
20033     }
20034   else
20035     {
20036       struct symtab_index_entry *old_entry = *slot;
20037       entry->index_offset = old_entry->index_offset;
20038       entry = old_entry;
20039     }
20040   return entry->index_offset;
20041 }
20042 
20043 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20044    constant pool entries going into the obstack CPOOL.  */
20045 
20046 static void
20047 write_hash_table (struct mapped_symtab *symtab,
20048 		  struct obstack *output, struct obstack *cpool)
20049 {
20050   offset_type i;
20051   htab_t symbol_hash_table;
20052   htab_t str_table;
20053 
20054   symbol_hash_table = create_symbol_hash_table ();
20055   str_table = create_strtab ();
20056 
20057   /* We add all the index vectors to the constant pool first, to
20058      ensure alignment is ok.  */
20059   for (i = 0; i < symtab->size; ++i)
20060     {
20061       if (symtab->data[i])
20062 	add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20063     }
20064 
20065   /* Now write out the hash table.  */
20066   for (i = 0; i < symtab->size; ++i)
20067     {
20068       offset_type str_off, vec_off;
20069 
20070       if (symtab->data[i])
20071 	{
20072 	  str_off = add_string (str_table, cpool, symtab->data[i]->name);
20073 	  vec_off = symtab->data[i]->index_offset;
20074 	}
20075       else
20076 	{
20077 	  /* While 0 is a valid constant pool index, it is not valid
20078 	     to have 0 for both offsets.  */
20079 	  str_off = 0;
20080 	  vec_off = 0;
20081 	}
20082 
20083       str_off = MAYBE_SWAP (str_off);
20084       vec_off = MAYBE_SWAP (vec_off);
20085 
20086       obstack_grow (output, &str_off, sizeof (str_off));
20087       obstack_grow (output, &vec_off, sizeof (vec_off));
20088     }
20089 
20090   htab_delete (str_table);
20091   htab_delete (symbol_hash_table);
20092 }
20093 
20094 /* Struct to map psymtab to CU index in the index file.  */
20095 struct psymtab_cu_index_map
20096 {
20097   struct partial_symtab *psymtab;
20098   unsigned int cu_index;
20099 };
20100 
20101 static hashval_t
20102 hash_psymtab_cu_index (const void *item)
20103 {
20104   const struct psymtab_cu_index_map *map = item;
20105 
20106   return htab_hash_pointer (map->psymtab);
20107 }
20108 
20109 static int
20110 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20111 {
20112   const struct psymtab_cu_index_map *lhs = item_lhs;
20113   const struct psymtab_cu_index_map *rhs = item_rhs;
20114 
20115   return lhs->psymtab == rhs->psymtab;
20116 }
20117 
20118 /* Helper struct for building the address table.  */
20119 struct addrmap_index_data
20120 {
20121   struct objfile *objfile;
20122   struct obstack *addr_obstack;
20123   htab_t cu_index_htab;
20124 
20125   /* Non-zero if the previous_* fields are valid.
20126      We can't write an entry until we see the next entry (since it is only then
20127      that we know the end of the entry).  */
20128   int previous_valid;
20129   /* Index of the CU in the table of all CUs in the index file.  */
20130   unsigned int previous_cu_index;
20131   /* Start address of the CU.  */
20132   CORE_ADDR previous_cu_start;
20133 };
20134 
20135 /* Write an address entry to OBSTACK.  */
20136 
20137 static void
20138 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20139 		   CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20140 {
20141   offset_type cu_index_to_write;
20142   char addr[8];
20143   CORE_ADDR baseaddr;
20144 
20145   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20146 
20147   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20148   obstack_grow (obstack, addr, 8);
20149   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20150   obstack_grow (obstack, addr, 8);
20151   cu_index_to_write = MAYBE_SWAP (cu_index);
20152   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20153 }
20154 
20155 /* Worker function for traversing an addrmap to build the address table.  */
20156 
20157 static int
20158 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20159 {
20160   struct addrmap_index_data *data = datap;
20161   struct partial_symtab *pst = obj;
20162 
20163   if (data->previous_valid)
20164     add_address_entry (data->objfile, data->addr_obstack,
20165 		       data->previous_cu_start, start_addr,
20166 		       data->previous_cu_index);
20167 
20168   data->previous_cu_start = start_addr;
20169   if (pst != NULL)
20170     {
20171       struct psymtab_cu_index_map find_map, *map;
20172       find_map.psymtab = pst;
20173       map = htab_find (data->cu_index_htab, &find_map);
20174       gdb_assert (map != NULL);
20175       data->previous_cu_index = map->cu_index;
20176       data->previous_valid = 1;
20177     }
20178   else
20179       data->previous_valid = 0;
20180 
20181   return 0;
20182 }
20183 
20184 /* Write OBJFILE's address map to OBSTACK.
20185    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20186    in the index file.  */
20187 
20188 static void
20189 write_address_map (struct objfile *objfile, struct obstack *obstack,
20190 		   htab_t cu_index_htab)
20191 {
20192   struct addrmap_index_data addrmap_index_data;
20193 
20194   /* When writing the address table, we have to cope with the fact that
20195      the addrmap iterator only provides the start of a region; we have to
20196      wait until the next invocation to get the start of the next region.  */
20197 
20198   addrmap_index_data.objfile = objfile;
20199   addrmap_index_data.addr_obstack = obstack;
20200   addrmap_index_data.cu_index_htab = cu_index_htab;
20201   addrmap_index_data.previous_valid = 0;
20202 
20203   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20204 		   &addrmap_index_data);
20205 
20206   /* It's highly unlikely the last entry (end address = 0xff...ff)
20207      is valid, but we should still handle it.
20208      The end address is recorded as the start of the next region, but that
20209      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
20210      anyway.  */
20211   if (addrmap_index_data.previous_valid)
20212     add_address_entry (objfile, obstack,
20213 		       addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20214 		       addrmap_index_data.previous_cu_index);
20215 }
20216 
20217 /* Return the symbol kind of PSYM.  */
20218 
20219 static gdb_index_symbol_kind
20220 symbol_kind (struct partial_symbol *psym)
20221 {
20222   domain_enum domain = PSYMBOL_DOMAIN (psym);
20223   enum address_class aclass = PSYMBOL_CLASS (psym);
20224 
20225   switch (domain)
20226     {
20227     case VAR_DOMAIN:
20228       switch (aclass)
20229 	{
20230 	case LOC_BLOCK:
20231 	  return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20232 	case LOC_TYPEDEF:
20233 	  return GDB_INDEX_SYMBOL_KIND_TYPE;
20234 	case LOC_COMPUTED:
20235 	case LOC_CONST_BYTES:
20236 	case LOC_OPTIMIZED_OUT:
20237 	case LOC_STATIC:
20238 	  return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20239 	case LOC_CONST:
20240 	  /* Note: It's currently impossible to recognize psyms as enum values
20241 	     short of reading the type info.  For now punt.  */
20242 	  return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20243 	default:
20244 	  /* There are other LOC_FOO values that one might want to classify
20245 	     as variables, but dwarf2read.c doesn't currently use them.  */
20246 	  return GDB_INDEX_SYMBOL_KIND_OTHER;
20247 	}
20248     case STRUCT_DOMAIN:
20249       return GDB_INDEX_SYMBOL_KIND_TYPE;
20250     default:
20251       return GDB_INDEX_SYMBOL_KIND_OTHER;
20252     }
20253 }
20254 
20255 /* Add a list of partial symbols to SYMTAB.  */
20256 
20257 static void
20258 write_psymbols (struct mapped_symtab *symtab,
20259 		htab_t psyms_seen,
20260 		struct partial_symbol **psymp,
20261 		int count,
20262 		offset_type cu_index,
20263 		int is_static)
20264 {
20265   for (; count-- > 0; ++psymp)
20266     {
20267       struct partial_symbol *psym = *psymp;
20268       void **slot;
20269 
20270       if (SYMBOL_LANGUAGE (psym) == language_ada)
20271 	error (_("Ada is not currently supported by the index"));
20272 
20273       /* Only add a given psymbol once.  */
20274       slot = htab_find_slot (psyms_seen, psym, INSERT);
20275       if (!*slot)
20276 	{
20277 	  gdb_index_symbol_kind kind = symbol_kind (psym);
20278 
20279 	  *slot = psym;
20280 	  add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20281 			   is_static, kind, cu_index);
20282 	}
20283     }
20284 }
20285 
20286 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
20287    exception if there is an error.  */
20288 
20289 static void
20290 write_obstack (FILE *file, struct obstack *obstack)
20291 {
20292   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20293 	      file)
20294       != obstack_object_size (obstack))
20295     error (_("couldn't data write to file"));
20296 }
20297 
20298 /* Unlink a file if the argument is not NULL.  */
20299 
20300 static void
20301 unlink_if_set (void *p)
20302 {
20303   char **filename = p;
20304   if (*filename)
20305     unlink (*filename);
20306 }
20307 
20308 /* A helper struct used when iterating over debug_types.  */
20309 struct signatured_type_index_data
20310 {
20311   struct objfile *objfile;
20312   struct mapped_symtab *symtab;
20313   struct obstack *types_list;
20314   htab_t psyms_seen;
20315   int cu_index;
20316 };
20317 
20318 /* A helper function that writes a single signatured_type to an
20319    obstack.  */
20320 
20321 static int
20322 write_one_signatured_type (void **slot, void *d)
20323 {
20324   struct signatured_type_index_data *info = d;
20325   struct signatured_type *entry = (struct signatured_type *) *slot;
20326   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20327   struct partial_symtab *psymtab = per_cu->v.psymtab;
20328   gdb_byte val[8];
20329 
20330   write_psymbols (info->symtab,
20331 		  info->psyms_seen,
20332 		  info->objfile->global_psymbols.list
20333 		  + psymtab->globals_offset,
20334 		  psymtab->n_global_syms, info->cu_index,
20335 		  0);
20336   write_psymbols (info->symtab,
20337 		  info->psyms_seen,
20338 		  info->objfile->static_psymbols.list
20339 		  + psymtab->statics_offset,
20340 		  psymtab->n_static_syms, info->cu_index,
20341 		  1);
20342 
20343   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20344 			  entry->per_cu.offset.sect_off);
20345   obstack_grow (info->types_list, val, 8);
20346   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20347 			  entry->type_offset_in_tu.cu_off);
20348   obstack_grow (info->types_list, val, 8);
20349   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20350   obstack_grow (info->types_list, val, 8);
20351 
20352   ++info->cu_index;
20353 
20354   return 1;
20355 }
20356 
20357 /* Recurse into all "included" dependencies and write their symbols as
20358    if they appeared in this psymtab.  */
20359 
20360 static void
20361 recursively_write_psymbols (struct objfile *objfile,
20362 			    struct partial_symtab *psymtab,
20363 			    struct mapped_symtab *symtab,
20364 			    htab_t psyms_seen,
20365 			    offset_type cu_index)
20366 {
20367   int i;
20368 
20369   for (i = 0; i < psymtab->number_of_dependencies; ++i)
20370     if (psymtab->dependencies[i]->user != NULL)
20371       recursively_write_psymbols (objfile, psymtab->dependencies[i],
20372 				  symtab, psyms_seen, cu_index);
20373 
20374   write_psymbols (symtab,
20375 		  psyms_seen,
20376 		  objfile->global_psymbols.list + psymtab->globals_offset,
20377 		  psymtab->n_global_syms, cu_index,
20378 		  0);
20379   write_psymbols (symtab,
20380 		  psyms_seen,
20381 		  objfile->static_psymbols.list + psymtab->statics_offset,
20382 		  psymtab->n_static_syms, cu_index,
20383 		  1);
20384 }
20385 
20386 /* Create an index file for OBJFILE in the directory DIR.  */
20387 
20388 static void
20389 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20390 {
20391   struct cleanup *cleanup;
20392   char *filename, *cleanup_filename;
20393   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20394   struct obstack cu_list, types_cu_list;
20395   int i;
20396   FILE *out_file;
20397   struct mapped_symtab *symtab;
20398   offset_type val, size_of_contents, total_len;
20399   struct stat st;
20400   htab_t psyms_seen;
20401   htab_t cu_index_htab;
20402   struct psymtab_cu_index_map *psymtab_cu_index_map;
20403 
20404   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20405     return;
20406 
20407   if (dwarf2_per_objfile->using_index)
20408     error (_("Cannot use an index to create the index"));
20409 
20410   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20411     error (_("Cannot make an index when the file has multiple .debug_types sections"));
20412 
20413   if (stat (objfile->name, &st) < 0)
20414     perror_with_name (objfile->name);
20415 
20416   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20417 		     INDEX_SUFFIX, (char *) NULL);
20418   cleanup = make_cleanup (xfree, filename);
20419 
20420   out_file = fopen (filename, "wb");
20421   if (!out_file)
20422     error (_("Can't open `%s' for writing"), filename);
20423 
20424   cleanup_filename = filename;
20425   make_cleanup (unlink_if_set, &cleanup_filename);
20426 
20427   symtab = create_mapped_symtab ();
20428   make_cleanup (cleanup_mapped_symtab, symtab);
20429 
20430   obstack_init (&addr_obstack);
20431   make_cleanup_obstack_free (&addr_obstack);
20432 
20433   obstack_init (&cu_list);
20434   make_cleanup_obstack_free (&cu_list);
20435 
20436   obstack_init (&types_cu_list);
20437   make_cleanup_obstack_free (&types_cu_list);
20438 
20439   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20440 				  NULL, xcalloc, xfree);
20441   make_cleanup_htab_delete (psyms_seen);
20442 
20443   /* While we're scanning CU's create a table that maps a psymtab pointer
20444      (which is what addrmap records) to its index (which is what is recorded
20445      in the index file).  This will later be needed to write the address
20446      table.  */
20447   cu_index_htab = htab_create_alloc (100,
20448 				     hash_psymtab_cu_index,
20449 				     eq_psymtab_cu_index,
20450 				     NULL, xcalloc, xfree);
20451   make_cleanup_htab_delete (cu_index_htab);
20452   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20453     xmalloc (sizeof (struct psymtab_cu_index_map)
20454 	     * dwarf2_per_objfile->n_comp_units);
20455   make_cleanup (xfree, psymtab_cu_index_map);
20456 
20457   /* The CU list is already sorted, so we don't need to do additional
20458      work here.  Also, the debug_types entries do not appear in
20459      all_comp_units, but only in their own hash table.  */
20460   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20461     {
20462       struct dwarf2_per_cu_data *per_cu
20463 	= dwarf2_per_objfile->all_comp_units[i];
20464       struct partial_symtab *psymtab = per_cu->v.psymtab;
20465       gdb_byte val[8];
20466       struct psymtab_cu_index_map *map;
20467       void **slot;
20468 
20469       if (psymtab->user == NULL)
20470 	recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20471 
20472       map = &psymtab_cu_index_map[i];
20473       map->psymtab = psymtab;
20474       map->cu_index = i;
20475       slot = htab_find_slot (cu_index_htab, map, INSERT);
20476       gdb_assert (slot != NULL);
20477       gdb_assert (*slot == NULL);
20478       *slot = map;
20479 
20480       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20481 			      per_cu->offset.sect_off);
20482       obstack_grow (&cu_list, val, 8);
20483       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20484       obstack_grow (&cu_list, val, 8);
20485     }
20486 
20487   /* Dump the address map.  */
20488   write_address_map (objfile, &addr_obstack, cu_index_htab);
20489 
20490   /* Write out the .debug_type entries, if any.  */
20491   if (dwarf2_per_objfile->signatured_types)
20492     {
20493       struct signatured_type_index_data sig_data;
20494 
20495       sig_data.objfile = objfile;
20496       sig_data.symtab = symtab;
20497       sig_data.types_list = &types_cu_list;
20498       sig_data.psyms_seen = psyms_seen;
20499       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20500       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20501 			      write_one_signatured_type, &sig_data);
20502     }
20503 
20504   /* Now that we've processed all symbols we can shrink their cu_indices
20505      lists.  */
20506   uniquify_cu_indices (symtab);
20507 
20508   obstack_init (&constant_pool);
20509   make_cleanup_obstack_free (&constant_pool);
20510   obstack_init (&symtab_obstack);
20511   make_cleanup_obstack_free (&symtab_obstack);
20512   write_hash_table (symtab, &symtab_obstack, &constant_pool);
20513 
20514   obstack_init (&contents);
20515   make_cleanup_obstack_free (&contents);
20516   size_of_contents = 6 * sizeof (offset_type);
20517   total_len = size_of_contents;
20518 
20519   /* The version number.  */
20520   val = MAYBE_SWAP (8);
20521   obstack_grow (&contents, &val, sizeof (val));
20522 
20523   /* The offset of the CU list from the start of the file.  */
20524   val = MAYBE_SWAP (total_len);
20525   obstack_grow (&contents, &val, sizeof (val));
20526   total_len += obstack_object_size (&cu_list);
20527 
20528   /* The offset of the types CU list from the start of the file.  */
20529   val = MAYBE_SWAP (total_len);
20530   obstack_grow (&contents, &val, sizeof (val));
20531   total_len += obstack_object_size (&types_cu_list);
20532 
20533   /* The offset of the address table from the start of the file.  */
20534   val = MAYBE_SWAP (total_len);
20535   obstack_grow (&contents, &val, sizeof (val));
20536   total_len += obstack_object_size (&addr_obstack);
20537 
20538   /* The offset of the symbol table from the start of the file.  */
20539   val = MAYBE_SWAP (total_len);
20540   obstack_grow (&contents, &val, sizeof (val));
20541   total_len += obstack_object_size (&symtab_obstack);
20542 
20543   /* The offset of the constant pool from the start of the file.  */
20544   val = MAYBE_SWAP (total_len);
20545   obstack_grow (&contents, &val, sizeof (val));
20546   total_len += obstack_object_size (&constant_pool);
20547 
20548   gdb_assert (obstack_object_size (&contents) == size_of_contents);
20549 
20550   write_obstack (out_file, &contents);
20551   write_obstack (out_file, &cu_list);
20552   write_obstack (out_file, &types_cu_list);
20553   write_obstack (out_file, &addr_obstack);
20554   write_obstack (out_file, &symtab_obstack);
20555   write_obstack (out_file, &constant_pool);
20556 
20557   fclose (out_file);
20558 
20559   /* We want to keep the file, so we set cleanup_filename to NULL
20560      here.  See unlink_if_set.  */
20561   cleanup_filename = NULL;
20562 
20563   do_cleanups (cleanup);
20564 }
20565 
20566 /* Implementation of the `save gdb-index' command.
20567 
20568    Note that the file format used by this command is documented in the
20569    GDB manual.  Any changes here must be documented there.  */
20570 
20571 static void
20572 save_gdb_index_command (char *arg, int from_tty)
20573 {
20574   struct objfile *objfile;
20575 
20576   if (!arg || !*arg)
20577     error (_("usage: save gdb-index DIRECTORY"));
20578 
20579   ALL_OBJFILES (objfile)
20580   {
20581     struct stat st;
20582 
20583     /* If the objfile does not correspond to an actual file, skip it.  */
20584     if (stat (objfile->name, &st) < 0)
20585       continue;
20586 
20587     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20588     if (dwarf2_per_objfile)
20589       {
20590 	volatile struct gdb_exception except;
20591 
20592 	TRY_CATCH (except, RETURN_MASK_ERROR)
20593 	  {
20594 	    write_psymtabs_to_index (objfile, arg);
20595 	  }
20596 	if (except.reason < 0)
20597 	  exception_fprintf (gdb_stderr, except,
20598 			     _("Error while writing index for `%s': "),
20599 			     objfile->name);
20600       }
20601   }
20602 }
20603 
20604 
20605 
20606 int dwarf2_always_disassemble;
20607 
20608 static void
20609 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20610 				struct cmd_list_element *c, const char *value)
20611 {
20612   fprintf_filtered (file,
20613 		    _("Whether to always disassemble "
20614 		      "DWARF expressions is %s.\n"),
20615 		    value);
20616 }
20617 
20618 static void
20619 show_check_physname (struct ui_file *file, int from_tty,
20620 		     struct cmd_list_element *c, const char *value)
20621 {
20622   fprintf_filtered (file,
20623 		    _("Whether to check \"physname\" is %s.\n"),
20624 		    value);
20625 }
20626 
20627 void _initialize_dwarf2_read (void);
20628 
20629 void
20630 _initialize_dwarf2_read (void)
20631 {
20632   struct cmd_list_element *c;
20633 
20634   dwarf2_objfile_data_key
20635     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20636 
20637   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20638 Set DWARF 2 specific variables.\n\
20639 Configure DWARF 2 variables such as the cache size"),
20640                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20641                   0/*allow-unknown*/, &maintenance_set_cmdlist);
20642 
20643   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20644 Show DWARF 2 specific variables\n\
20645 Show DWARF 2 variables such as the cache size"),
20646                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20647                   0/*allow-unknown*/, &maintenance_show_cmdlist);
20648 
20649   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20650 			    &dwarf2_max_cache_age, _("\
20651 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20652 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20653 A higher limit means that cached compilation units will be stored\n\
20654 in memory longer, and more total memory will be used.  Zero disables\n\
20655 caching, which can slow down startup."),
20656 			    NULL,
20657 			    show_dwarf2_max_cache_age,
20658 			    &set_dwarf2_cmdlist,
20659 			    &show_dwarf2_cmdlist);
20660 
20661   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20662 			   &dwarf2_always_disassemble, _("\
20663 Set whether `info address' always disassembles DWARF expressions."), _("\
20664 Show whether `info address' always disassembles DWARF expressions."), _("\
20665 When enabled, DWARF expressions are always printed in an assembly-like\n\
20666 syntax.  When disabled, expressions will be printed in a more\n\
20667 conversational style, when possible."),
20668 			   NULL,
20669 			   show_dwarf2_always_disassemble,
20670 			   &set_dwarf2_cmdlist,
20671 			   &show_dwarf2_cmdlist);
20672 
20673   add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20674 Set debugging of the dwarf2 reader."), _("\
20675 Show debugging of the dwarf2 reader."), _("\
20676 When enabled, debugging messages are printed during dwarf2 reading\n\
20677 and symtab expansion."),
20678 			    NULL,
20679 			    NULL,
20680 			    &setdebuglist, &showdebuglist);
20681 
20682   add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20683 Set debugging of the dwarf2 DIE reader."), _("\
20684 Show debugging of the dwarf2 DIE reader."), _("\
20685 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20686 The value is the maximum depth to print."),
20687 			     NULL,
20688 			     NULL,
20689 			     &setdebuglist, &showdebuglist);
20690 
20691   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20692 Set cross-checking of \"physname\" code against demangler."), _("\
20693 Show cross-checking of \"physname\" code against demangler."), _("\
20694 When enabled, GDB's internal \"physname\" code is checked against\n\
20695 the demangler."),
20696 			   NULL, show_check_physname,
20697 			   &setdebuglist, &showdebuglist);
20698 
20699   add_setshow_boolean_cmd ("use-deprecated-index-sections",
20700 			   no_class, &use_deprecated_index_sections, _("\
20701 Set whether to use deprecated gdb_index sections."), _("\
20702 Show whether to use deprecated gdb_index sections."), _("\
20703 When enabled, deprecated .gdb_index sections are used anyway.\n\
20704 Normally they are ignored either because of a missing feature or\n\
20705 performance issue.\n\
20706 Warning: This option must be enabled before gdb reads the file."),
20707 			   NULL,
20708 			   NULL,
20709 			   &setlist, &showlist);
20710 
20711   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20712 	       _("\
20713 Save a gdb-index file.\n\
20714 Usage: save gdb-index DIRECTORY"),
20715 	       &save_cmdlist);
20716   set_cmd_completer (c, filename_completer);
20717 }
20718