* dwarf2read.c (lookup_signatured_type): Remove complaint about
[external/binutils.git] / gdb / dwarf2read.c
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 /* The "aclass" indices for various kinds of computed DWARF symbols.  */
96
97 static int dwarf2_locexpr_index;
98 static int dwarf2_loclist_index;
99 static int dwarf2_locexpr_block_index;
100 static int dwarf2_loclist_block_index;
101
102 struct dwarf2_section_info
103 {
104   asection *asection;
105   gdb_byte *buffer;
106   bfd_size_type size;
107   /* True if we have tried to read this section.  */
108   int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type.  It must be
115    architecture-independent.  */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* Ensure only legit values are used.  */
121 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
122   do { \
123     gdb_assert ((unsigned int) (value) <= 1); \
124     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
125   } while (0)
126
127 /* Ensure only legit values are used.  */
128 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
129   do { \
130     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
131                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
132     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
133   } while (0)
134
135 /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
136 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
137   do { \
138     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
139     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
140   } while (0)
141
142 /* A description of the mapped index.  The file format is described in
143    a comment by the code that writes the index.  */
144 struct mapped_index
145 {
146   /* Index data format version.  */
147   int version;
148
149   /* The total length of the buffer.  */
150   off_t total_size;
151
152   /* A pointer to the address table data.  */
153   const gdb_byte *address_table;
154
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157
158   /* The symbol table, implemented as a hash table.  */
159   const offset_type *symbol_table;
160
161   /* Size in slots, each slot is 2 offset_types.  */
162   offset_type symbol_table_slots;
163
164   /* A pointer to the constant pool.  */
165   const char *constant_pool;
166 };
167
168 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
169 DEF_VEC_P (dwarf2_per_cu_ptr);
170
171 /* Collection of data recorded per objfile.
172    This hangs off of dwarf2_objfile_data_key.  */
173
174 struct dwarf2_per_objfile
175 {
176   struct dwarf2_section_info info;
177   struct dwarf2_section_info abbrev;
178   struct dwarf2_section_info line;
179   struct dwarf2_section_info loc;
180   struct dwarf2_section_info macinfo;
181   struct dwarf2_section_info macro;
182   struct dwarf2_section_info str;
183   struct dwarf2_section_info ranges;
184   struct dwarf2_section_info addr;
185   struct dwarf2_section_info frame;
186   struct dwarf2_section_info eh_frame;
187   struct dwarf2_section_info gdb_index;
188
189   VEC (dwarf2_section_info_def) *types;
190
191   /* Back link.  */
192   struct objfile *objfile;
193
194   /* Table of all the compilation units.  This is used to locate
195      the target compilation unit of a particular reference.  */
196   struct dwarf2_per_cu_data **all_comp_units;
197
198   /* The number of compilation units in ALL_COMP_UNITS.  */
199   int n_comp_units;
200
201   /* The number of .debug_types-related CUs.  */
202   int n_type_units;
203
204   /* The .debug_types-related CUs (TUs).  */
205   struct signatured_type **all_type_units;
206
207   /* The number of entries in all_type_unit_groups.  */
208   int n_type_unit_groups;
209
210   /* Table of type unit groups.
211      This exists to make it easy to iterate over all CUs and TU groups.  */
212   struct type_unit_group **all_type_unit_groups;
213
214   /* Table of struct type_unit_group objects.
215      The hash key is the DW_AT_stmt_list value.  */
216   htab_t type_unit_groups;
217
218   /* A table mapping .debug_types signatures to its signatured_type entry.
219      This is NULL if the .debug_types section hasn't been read in yet.  */
220   htab_t signatured_types;
221
222   /* Type unit statistics, to see how well the scaling improvements
223      are doing.  */
224   struct tu_stats
225   {
226     int nr_uniq_abbrev_tables;
227     int nr_symtabs;
228     int nr_symtab_sharers;
229     int nr_stmt_less_type_units;
230   } tu_stats;
231
232   /* A chain of compilation units that are currently read in, so that
233      they can be freed later.  */
234   struct dwarf2_per_cu_data *read_in_chain;
235
236   /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
237      This is NULL if the table hasn't been allocated yet.  */
238   htab_t dwo_files;
239
240   /* Non-zero if we've check for whether there is a DWP file.  */
241   int dwp_checked;
242
243   /* The DWP file if there is one, or NULL.  */
244   struct dwp_file *dwp_file;
245
246   /* The shared '.dwz' file, if one exists.  This is used when the
247      original data was compressed using 'dwz -m'.  */
248   struct dwz_file *dwz_file;
249
250   /* A flag indicating wether this objfile has a section loaded at a
251      VMA of 0.  */
252   int has_section_at_zero;
253
254   /* True if we are using the mapped index,
255      or we are faking it for OBJF_READNOW's sake.  */
256   unsigned char using_index;
257
258   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
259   struct mapped_index *index_table;
260
261   /* When using index_table, this keeps track of all quick_file_names entries.
262      TUs typically share line table entries with a CU, so we maintain a
263      separate table of all line table entries to support the sharing.
264      Note that while there can be way more TUs than CUs, we've already
265      sorted all the TUs into "type unit groups", grouped by their
266      DW_AT_stmt_list value.  Therefore the only sharing done here is with a
267      CU and its associated TU group if there is one.  */
268   htab_t quick_file_names_table;
269
270   /* Set during partial symbol reading, to prevent queueing of full
271      symbols.  */
272   int reading_partial_symbols;
273
274   /* Table mapping type DIEs to their struct type *.
275      This is NULL if not allocated yet.
276      The mapping is done via (CU/TU signature + DIE offset) -> type.  */
277   htab_t die_type_hash;
278
279   /* The CUs we recently read.  */
280   VEC (dwarf2_per_cu_ptr) *just_read_cus;
281 };
282
283 static struct dwarf2_per_objfile *dwarf2_per_objfile;
284
285 /* Default names of the debugging sections.  */
286
287 /* Note that if the debugging section has been compressed, it might
288    have a name like .zdebug_info.  */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292   { ".debug_info", ".zdebug_info" },
293   { ".debug_abbrev", ".zdebug_abbrev" },
294   { ".debug_line", ".zdebug_line" },
295   { ".debug_loc", ".zdebug_loc" },
296   { ".debug_macinfo", ".zdebug_macinfo" },
297   { ".debug_macro", ".zdebug_macro" },
298   { ".debug_str", ".zdebug_str" },
299   { ".debug_ranges", ".zdebug_ranges" },
300   { ".debug_types", ".zdebug_types" },
301   { ".debug_addr", ".zdebug_addr" },
302   { ".debug_frame", ".zdebug_frame" },
303   { ".eh_frame", NULL },
304   { ".gdb_index", ".zgdb_index" },
305   23
306 };
307
308 /* List of DWO/DWP sections.  */
309
310 static const struct dwop_section_names
311 {
312   struct dwarf2_section_names abbrev_dwo;
313   struct dwarf2_section_names info_dwo;
314   struct dwarf2_section_names line_dwo;
315   struct dwarf2_section_names loc_dwo;
316   struct dwarf2_section_names macinfo_dwo;
317   struct dwarf2_section_names macro_dwo;
318   struct dwarf2_section_names str_dwo;
319   struct dwarf2_section_names str_offsets_dwo;
320   struct dwarf2_section_names types_dwo;
321   struct dwarf2_section_names cu_index;
322   struct dwarf2_section_names tu_index;
323 }
324 dwop_section_names =
325 {
326   { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
327   { ".debug_info.dwo", ".zdebug_info.dwo" },
328   { ".debug_line.dwo", ".zdebug_line.dwo" },
329   { ".debug_loc.dwo", ".zdebug_loc.dwo" },
330   { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
331   { ".debug_macro.dwo", ".zdebug_macro.dwo" },
332   { ".debug_str.dwo", ".zdebug_str.dwo" },
333   { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
334   { ".debug_types.dwo", ".zdebug_types.dwo" },
335   { ".debug_cu_index", ".zdebug_cu_index" },
336   { ".debug_tu_index", ".zdebug_tu_index" },
337 };
338
339 /* local data types */
340
341 /* The data in a compilation unit header, after target2host
342    translation, looks like this.  */
343 struct comp_unit_head
344 {
345   unsigned int length;
346   short version;
347   unsigned char addr_size;
348   unsigned char signed_addr_p;
349   sect_offset abbrev_offset;
350
351   /* Size of file offsets; either 4 or 8.  */
352   unsigned int offset_size;
353
354   /* Size of the length field; either 4 or 12.  */
355   unsigned int initial_length_size;
356
357   /* Offset to the first byte of this compilation unit header in the
358      .debug_info section, for resolving relative reference dies.  */
359   sect_offset offset;
360
361   /* Offset to first die in this cu from the start of the cu.
362      This will be the first byte following the compilation unit header.  */
363   cu_offset first_die_offset;
364 };
365
366 /* Type used for delaying computation of method physnames.
367    See comments for compute_delayed_physnames.  */
368 struct delayed_method_info
369 {
370   /* The type to which the method is attached, i.e., its parent class.  */
371   struct type *type;
372
373   /* The index of the method in the type's function fieldlists.  */
374   int fnfield_index;
375
376   /* The index of the method in the fieldlist.  */
377   int index;
378
379   /* The name of the DIE.  */
380   const char *name;
381
382   /*  The DIE associated with this method.  */
383   struct die_info *die;
384 };
385
386 typedef struct delayed_method_info delayed_method_info;
387 DEF_VEC_O (delayed_method_info);
388
389 /* Internal state when decoding a particular compilation unit.  */
390 struct dwarf2_cu
391 {
392   /* The objfile containing this compilation unit.  */
393   struct objfile *objfile;
394
395   /* The header of the compilation unit.  */
396   struct comp_unit_head header;
397
398   /* Base address of this compilation unit.  */
399   CORE_ADDR base_address;
400
401   /* Non-zero if base_address has been set.  */
402   int base_known;
403
404   /* The language we are debugging.  */
405   enum language language;
406   const struct language_defn *language_defn;
407
408   const char *producer;
409
410   /* The generic symbol table building routines have separate lists for
411      file scope symbols and all all other scopes (local scopes).  So
412      we need to select the right one to pass to add_symbol_to_list().
413      We do it by keeping a pointer to the correct list in list_in_scope.
414
415      FIXME: The original dwarf code just treated the file scope as the
416      first local scope, and all other local scopes as nested local
417      scopes, and worked fine.  Check to see if we really need to
418      distinguish these in buildsym.c.  */
419   struct pending **list_in_scope;
420
421   /* The abbrev table for this CU.
422      Normally this points to the abbrev table in the objfile.
423      But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
424   struct abbrev_table *abbrev_table;
425
426   /* Hash table holding all the loaded partial DIEs
427      with partial_die->offset.SECT_OFF as hash.  */
428   htab_t partial_dies;
429
430   /* Storage for things with the same lifetime as this read-in compilation
431      unit, including partial DIEs.  */
432   struct obstack comp_unit_obstack;
433
434   /* When multiple dwarf2_cu structures are living in memory, this field
435      chains them all together, so that they can be released efficiently.
436      We will probably also want a generation counter so that most-recently-used
437      compilation units are cached...  */
438   struct dwarf2_per_cu_data *read_in_chain;
439
440   /* Backchain to our per_cu entry if the tree has been built.  */
441   struct dwarf2_per_cu_data *per_cu;
442
443   /* How many compilation units ago was this CU last referenced?  */
444   int last_used;
445
446   /* A hash table of DIE cu_offset for following references with
447      die_info->offset.sect_off as hash.  */
448   htab_t die_hash;
449
450   /* Full DIEs if read in.  */
451   struct die_info *dies;
452
453   /* A set of pointers to dwarf2_per_cu_data objects for compilation
454      units referenced by this one.  Only set during full symbol processing;
455      partial symbol tables do not have dependencies.  */
456   htab_t dependencies;
457
458   /* Header data from the line table, during full symbol processing.  */
459   struct line_header *line_header;
460
461   /* A list of methods which need to have physnames computed
462      after all type information has been read.  */
463   VEC (delayed_method_info) *method_list;
464
465   /* To be copied to symtab->call_site_htab.  */
466   htab_t call_site_htab;
467
468   /* Non-NULL if this CU came from a DWO file.
469      There is an invariant here that is important to remember:
470      Except for attributes copied from the top level DIE in the "main"
471      (or "stub") file in preparation for reading the DWO file
472      (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
473      Either there isn't a DWO file (in which case this is NULL and the point
474      is moot), or there is and either we're not going to read it (in which
475      case this is NULL) or there is and we are reading it (in which case this
476      is non-NULL).  */
477   struct dwo_unit *dwo_unit;
478
479   /* The DW_AT_addr_base attribute if present, zero otherwise
480      (zero is a valid value though).
481      Note this value comes from the stub CU/TU's DIE.  */
482   ULONGEST addr_base;
483
484   /* The DW_AT_ranges_base attribute if present, zero otherwise
485      (zero is a valid value though).
486      Note this value comes from the stub CU/TU's DIE.
487      Also note that the value is zero in the non-DWO case so this value can
488      be used without needing to know whether DWO files are in use or not.
489      N.B. This does not apply to DW_AT_ranges appearing in
490      DW_TAG_compile_unit dies.  This is a bit of a wart, consider if ever
491      DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492      DW_AT_ranges_base *would* have to be applied, and we'd have to care
493      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
494   ULONGEST ranges_base;
495
496   /* Mark used when releasing cached dies.  */
497   unsigned int mark : 1;
498
499   /* This CU references .debug_loc.  See the symtab->locations_valid field.
500      This test is imperfect as there may exist optimized debug code not using
501      any location list and still facing inlining issues if handled as
502      unoptimized code.  For a future better test see GCC PR other/32998.  */
503   unsigned int has_loclist : 1;
504
505   /* These cache the results for producer_is_* fields.  CHECKED_PRODUCER is set
506      if all the producer_is_* fields are valid.  This information is cached
507      because profiling CU expansion showed excessive time spent in
508      producer_is_gxx_lt_4_6.  */
509   unsigned int checked_producer : 1;
510   unsigned int producer_is_gxx_lt_4_6 : 1;
511   unsigned int producer_is_gcc_lt_4_3 : 1;
512   unsigned int producer_is_icc : 1;
513
514   /* When set, the file that we're processing is known to have
515      debugging info for C++ namespaces.  GCC 3.3.x did not produce
516      this information, but later versions do.  */
517
518   unsigned int processing_has_namespace_info : 1;
519 };
520
521 /* Persistent data held for a compilation unit, even when not
522    processing it.  We put a pointer to this structure in the
523    read_symtab_private field of the psymtab.  */
524
525 struct dwarf2_per_cu_data
526 {
527   /* The start offset and length of this compilation unit.
528      NOTE: Unlike comp_unit_head.length, this length includes
529      initial_length_size.
530      If the DIE refers to a DWO file, this is always of the original die,
531      not the DWO file.  */
532   sect_offset offset;
533   unsigned int length;
534
535   /* Flag indicating this compilation unit will be read in before
536      any of the current compilation units are processed.  */
537   unsigned int queued : 1;
538
539   /* This flag will be set when reading partial DIEs if we need to load
540      absolutely all DIEs for this compilation unit, instead of just the ones
541      we think are interesting.  It gets set if we look for a DIE in the
542      hash table and don't find it.  */
543   unsigned int load_all_dies : 1;
544
545   /* Non-zero if this CU is from .debug_types.  */
546   unsigned int is_debug_types : 1;
547
548   /* Non-zero if this CU is from the .dwz file.  */
549   unsigned int is_dwz : 1;
550
551   /* The section this CU/TU lives in.
552      If the DIE refers to a DWO file, this is always the original die,
553      not the DWO file.  */
554   struct dwarf2_section_info *section;
555
556   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
557      of the CU cache it gets reset to NULL again.  */
558   struct dwarf2_cu *cu;
559
560   /* The corresponding objfile.
561      Normally we can get the objfile from dwarf2_per_objfile.
562      However we can enter this file with just a "per_cu" handle.  */
563   struct objfile *objfile;
564
565   /* When using partial symbol tables, the 'psymtab' field is active.
566      Otherwise the 'quick' field is active.  */
567   union
568   {
569     /* The partial symbol table associated with this compilation unit,
570        or NULL for unread partial units.  */
571     struct partial_symtab *psymtab;
572
573     /* Data needed by the "quick" functions.  */
574     struct dwarf2_per_cu_quick_data *quick;
575   } v;
576
577   /* The CUs we import using DW_TAG_imported_unit.  This is filled in
578      while reading psymtabs, used to compute the psymtab dependencies,
579      and then cleared.  Then it is filled in again while reading full
580      symbols, and only deleted when the objfile is destroyed.
581
582      This is also used to work around a difference between the way gold
583      generates .gdb_index version <=7 and the way gdb does.  Arguably this
584      is a gold bug.  For symbols coming from TUs, gold records in the index
585      the CU that includes the TU instead of the TU itself.  This breaks
586      dw2_lookup_symbol: It assumes that if the index says symbol X lives
587      in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
588      will find X.  Alas TUs live in their own symtab, so after expanding CU Y
589      we need to look in TU Z to find X.  Fortunately, this is akin to
590      DW_TAG_imported_unit, so we just use the same mechanism: For
591      .gdb_index version <=7 this also records the TUs that the CU referred
592      to.  Concurrently with this change gdb was modified to emit version 8
593      indices so we only pay a price for gold generated indices.  */
594   VEC (dwarf2_per_cu_ptr) *imported_symtabs;
595
596   /* Type units are grouped by their DW_AT_stmt_list entry so that they
597      can share them.  If this is a TU, this points to the containing
598      symtab.  */
599   struct type_unit_group *type_unit_group;
600 };
601
602 /* Entry in the signatured_types hash table.  */
603
604 struct signatured_type
605 {
606   /* The "per_cu" object of this type.
607      N.B.: This is the first member so that it's easy to convert pointers
608      between them.  */
609   struct dwarf2_per_cu_data per_cu;
610
611   /* The type's signature.  */
612   ULONGEST signature;
613
614   /* Offset in the TU of the type's DIE, as read from the TU header.
615      If the definition lives in a DWO file, this value is unusable.  */
616   cu_offset type_offset_in_tu;
617
618   /* Offset in the section of the type's DIE.
619      If the definition lives in a DWO file, this is the offset in the
620      .debug_types.dwo section.
621      The value is zero until the actual value is known.
622      Zero is otherwise not a valid section offset.  */
623   sect_offset type_offset_in_section;
624 };
625
626 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
627    This includes type_unit_group and quick_file_names.  */
628
629 struct stmt_list_hash
630 {
631   /* The DWO unit this table is from or NULL if there is none.  */
632   struct dwo_unit *dwo_unit;
633
634   /* Offset in .debug_line or .debug_line.dwo.  */
635   sect_offset line_offset;
636 };
637
638 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
639    an object of this type.  */
640
641 struct type_unit_group
642 {
643   /* dwarf2read.c's main "handle" on the symtab.
644      To simplify things we create an artificial CU that "includes" all the
645      type units using this stmt_list so that the rest of the code still has
646      a "per_cu" handle on the symtab.
647      This PER_CU is recognized by having no section.  */
648 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
649   struct dwarf2_per_cu_data per_cu;
650
651   union
652   {
653     /* The TUs that share this DW_AT_stmt_list entry.
654        This is added to while parsing type units to build partial symtabs,
655        and is deleted afterwards and not used again.  */
656     VEC (dwarf2_per_cu_ptr) *tus;
657
658     /* When reading the line table in "quick" functions, we need a real TU.
659        Any will do, we know they all share the same DW_AT_stmt_list entry.
660        For simplicity's sake, we pick the first one.  */
661     struct dwarf2_per_cu_data *first_tu;
662   } t;
663
664   /* The primary symtab.
665      Type units in a group needn't all be defined in the same source file,
666      so we create an essentially anonymous symtab as the primary symtab.  */
667   struct symtab *primary_symtab;
668
669   /* The data used to construct the hash key.  */
670   struct stmt_list_hash hash;
671
672   /* The number of symtabs from the line header.
673      The value here must match line_header.num_file_names.  */
674   unsigned int num_symtabs;
675
676   /* The symbol tables for this TU (obtained from the files listed in
677      DW_AT_stmt_list).
678      WARNING: The order of entries here must match the order of entries
679      in the line header.  After the first TU using this type_unit_group, the
680      line header for the subsequent TUs is recreated from this.  This is done
681      because we need to use the same symtabs for each TU using the same
682      DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
683      there's no guarantee the line header doesn't have duplicate entries.  */
684   struct symtab **symtabs;
685 };
686
687 /* These sections are what may appear in a DWO file.  */
688
689 struct dwo_sections
690 {
691   struct dwarf2_section_info abbrev;
692   struct dwarf2_section_info line;
693   struct dwarf2_section_info loc;
694   struct dwarf2_section_info macinfo;
695   struct dwarf2_section_info macro;
696   struct dwarf2_section_info str;
697   struct dwarf2_section_info str_offsets;
698   /* In the case of a virtual DWO file, these two are unused.  */
699   struct dwarf2_section_info info;
700   VEC (dwarf2_section_info_def) *types;
701 };
702
703 /* Common bits of DWO CUs/TUs.  */
704
705 struct dwo_unit
706 {
707   /* Backlink to the containing struct dwo_file.  */
708   struct dwo_file *dwo_file;
709
710   /* The "id" that distinguishes this CU/TU.
711      .debug_info calls this "dwo_id", .debug_types calls this "signature".
712      Since signatures came first, we stick with it for consistency.  */
713   ULONGEST signature;
714
715   /* The section this CU/TU lives in, in the DWO file.  */
716   struct dwarf2_section_info *section;
717
718   /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section.  */
719   sect_offset offset;
720   unsigned int length;
721
722   /* For types, offset in the type's DIE of the type defined by this TU.  */
723   cu_offset type_offset_in_tu;
724 };
725
726 /* Data for one DWO file.
727    This includes virtual DWO files that have been packaged into a
728    DWP file.  */
729
730 struct dwo_file
731 {
732   /* The DW_AT_GNU_dwo_name attribute.  This is the hash key.
733      For virtual DWO files the name is constructed from the section offsets
734      of abbrev,line,loc,str_offsets so that we combine virtual DWO files
735      from related CU+TUs.  */
736   const char *name;
737
738   /* The bfd, when the file is open.  Otherwise this is NULL.
739      This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd.  */
740   bfd *dbfd;
741
742   /* Section info for this file.  */
743   struct dwo_sections sections;
744
745   /* Table of CUs in the file.
746      Each element is a struct dwo_unit.  */
747   htab_t cus;
748
749   /* Table of TUs in the file.
750      Each element is a struct dwo_unit.  */
751   htab_t tus;
752 };
753
754 /* These sections are what may appear in a DWP file.  */
755
756 struct dwp_sections
757 {
758   struct dwarf2_section_info str;
759   struct dwarf2_section_info cu_index;
760   struct dwarf2_section_info tu_index;
761   /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
762      by section number.  We don't need to record them here.  */
763 };
764
765 /* These sections are what may appear in a virtual DWO file.  */
766
767 struct virtual_dwo_sections
768 {
769   struct dwarf2_section_info abbrev;
770   struct dwarf2_section_info line;
771   struct dwarf2_section_info loc;
772   struct dwarf2_section_info macinfo;
773   struct dwarf2_section_info macro;
774   struct dwarf2_section_info str_offsets;
775   /* Each DWP hash table entry records one CU or one TU.
776      That is recorded here, and copied to dwo_unit.section.  */
777   struct dwarf2_section_info info_or_types;
778 };
779
780 /* Contents of DWP hash tables.  */
781
782 struct dwp_hash_table
783 {
784   uint32_t nr_units, nr_slots;
785   const gdb_byte *hash_table, *unit_table, *section_pool;
786 };
787
788 /* Data for one DWP file.  */
789
790 struct dwp_file
791 {
792   /* Name of the file.  */
793   const char *name;
794
795   /* The bfd, when the file is open.  Otherwise this is NULL.  */
796   bfd *dbfd;
797
798   /* Section info for this file.  */
799   struct dwp_sections sections;
800
801   /* Table of CUs in the file. */
802   const struct dwp_hash_table *cus;
803
804   /* Table of TUs in the file.  */
805   const struct dwp_hash_table *tus;
806
807   /* Table of loaded CUs/TUs.  Each entry is a struct dwo_unit *.  */
808   htab_t loaded_cutus;
809
810   /* Table to map ELF section numbers to their sections.  */
811   unsigned int num_sections;
812   asection **elf_sections;
813 };
814
815 /* This represents a '.dwz' file.  */
816
817 struct dwz_file
818 {
819   /* A dwz file can only contain a few sections.  */
820   struct dwarf2_section_info abbrev;
821   struct dwarf2_section_info info;
822   struct dwarf2_section_info str;
823   struct dwarf2_section_info line;
824   struct dwarf2_section_info macro;
825   struct dwarf2_section_info gdb_index;
826
827   /* The dwz's BFD.  */
828   bfd *dwz_bfd;
829 };
830
831 /* Struct used to pass misc. parameters to read_die_and_children, et
832    al.  which are used for both .debug_info and .debug_types dies.
833    All parameters here are unchanging for the life of the call.  This
834    struct exists to abstract away the constant parameters of die reading.  */
835
836 struct die_reader_specs
837 {
838   /* die_section->asection->owner.  */
839   bfd* abfd;
840
841   /* The CU of the DIE we are parsing.  */
842   struct dwarf2_cu *cu;
843
844   /* Non-NULL if reading a DWO file (including one packaged into a DWP).  */
845   struct dwo_file *dwo_file;
846
847   /* The section the die comes from.
848      This is either .debug_info or .debug_types, or the .dwo variants.  */
849   struct dwarf2_section_info *die_section;
850
851   /* die_section->buffer.  */
852   gdb_byte *buffer;
853
854   /* The end of the buffer.  */
855   const gdb_byte *buffer_end;
856 };
857
858 /* Type of function passed to init_cutu_and_read_dies, et.al.  */
859 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
860                                       gdb_byte *info_ptr,
861                                       struct die_info *comp_unit_die,
862                                       int has_children,
863                                       void *data);
864
865 /* The line number information for a compilation unit (found in the
866    .debug_line section) begins with a "statement program header",
867    which contains the following information.  */
868 struct line_header
869 {
870   unsigned int total_length;
871   unsigned short version;
872   unsigned int header_length;
873   unsigned char minimum_instruction_length;
874   unsigned char maximum_ops_per_instruction;
875   unsigned char default_is_stmt;
876   int line_base;
877   unsigned char line_range;
878   unsigned char opcode_base;
879
880   /* standard_opcode_lengths[i] is the number of operands for the
881      standard opcode whose value is i.  This means that
882      standard_opcode_lengths[0] is unused, and the last meaningful
883      element is standard_opcode_lengths[opcode_base - 1].  */
884   unsigned char *standard_opcode_lengths;
885
886   /* The include_directories table.  NOTE!  These strings are not
887      allocated with xmalloc; instead, they are pointers into
888      debug_line_buffer.  If you try to free them, `free' will get
889      indigestion.  */
890   unsigned int num_include_dirs, include_dirs_size;
891   char **include_dirs;
892
893   /* The file_names table.  NOTE!  These strings are not allocated
894      with xmalloc; instead, they are pointers into debug_line_buffer.
895      Don't try to free them directly.  */
896   unsigned int num_file_names, file_names_size;
897   struct file_entry
898   {
899     char *name;
900     unsigned int dir_index;
901     unsigned int mod_time;
902     unsigned int length;
903     int included_p; /* Non-zero if referenced by the Line Number Program.  */
904     struct symtab *symtab; /* The associated symbol table, if any.  */
905   } *file_names;
906
907   /* The start and end of the statement program following this
908      header.  These point into dwarf2_per_objfile->line_buffer.  */
909   gdb_byte *statement_program_start, *statement_program_end;
910 };
911
912 /* When we construct a partial symbol table entry we only
913    need this much information.  */
914 struct partial_die_info
915   {
916     /* Offset of this DIE.  */
917     sect_offset offset;
918
919     /* DWARF-2 tag for this DIE.  */
920     ENUM_BITFIELD(dwarf_tag) tag : 16;
921
922     /* Assorted flags describing the data found in this DIE.  */
923     unsigned int has_children : 1;
924     unsigned int is_external : 1;
925     unsigned int is_declaration : 1;
926     unsigned int has_type : 1;
927     unsigned int has_specification : 1;
928     unsigned int has_pc_info : 1;
929     unsigned int may_be_inlined : 1;
930
931     /* Flag set if the SCOPE field of this structure has been
932        computed.  */
933     unsigned int scope_set : 1;
934
935     /* Flag set if the DIE has a byte_size attribute.  */
936     unsigned int has_byte_size : 1;
937
938     /* Flag set if any of the DIE's children are template arguments.  */
939     unsigned int has_template_arguments : 1;
940
941     /* Flag set if fixup_partial_die has been called on this die.  */
942     unsigned int fixup_called : 1;
943
944     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
945     unsigned int is_dwz : 1;
946
947     /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
948     unsigned int spec_is_dwz : 1;
949
950     /* The name of this DIE.  Normally the value of DW_AT_name, but
951        sometimes a default name for unnamed DIEs.  */
952     const char *name;
953
954     /* The linkage name, if present.  */
955     const char *linkage_name;
956
957     /* The scope to prepend to our children.  This is generally
958        allocated on the comp_unit_obstack, so will disappear
959        when this compilation unit leaves the cache.  */
960     const char *scope;
961
962     /* Some data associated with the partial DIE.  The tag determines
963        which field is live.  */
964     union
965     {
966       /* The location description associated with this DIE, if any.  */
967       struct dwarf_block *locdesc;
968       /* The offset of an import, for DW_TAG_imported_unit.  */
969       sect_offset offset;
970     } d;
971
972     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
973     CORE_ADDR lowpc;
974     CORE_ADDR highpc;
975
976     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
977        DW_AT_sibling, if any.  */
978     /* NOTE: This member isn't strictly necessary, read_partial_die could
979        return DW_AT_sibling values to its caller load_partial_dies.  */
980     gdb_byte *sibling;
981
982     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
983        DW_AT_specification (or DW_AT_abstract_origin or
984        DW_AT_extension).  */
985     sect_offset spec_offset;
986
987     /* Pointers to this DIE's parent, first child, and next sibling,
988        if any.  */
989     struct partial_die_info *die_parent, *die_child, *die_sibling;
990   };
991
992 /* This data structure holds the information of an abbrev.  */
993 struct abbrev_info
994   {
995     unsigned int number;        /* number identifying abbrev */
996     enum dwarf_tag tag;         /* dwarf tag */
997     unsigned short has_children;                /* boolean */
998     unsigned short num_attrs;   /* number of attributes */
999     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
1000     struct abbrev_info *next;   /* next in chain */
1001   };
1002
1003 struct attr_abbrev
1004   {
1005     ENUM_BITFIELD(dwarf_attribute) name : 16;
1006     ENUM_BITFIELD(dwarf_form) form : 16;
1007   };
1008
1009 /* Size of abbrev_table.abbrev_hash_table.  */
1010 #define ABBREV_HASH_SIZE 121
1011
1012 /* Top level data structure to contain an abbreviation table.  */
1013
1014 struct abbrev_table
1015 {
1016   /* Where the abbrev table came from.
1017      This is used as a sanity check when the table is used.  */
1018   sect_offset offset;
1019
1020   /* Storage for the abbrev table.  */
1021   struct obstack abbrev_obstack;
1022
1023   /* Hash table of abbrevs.
1024      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1025      It could be statically allocated, but the previous code didn't so we
1026      don't either.  */
1027   struct abbrev_info **abbrevs;
1028 };
1029
1030 /* Attributes have a name and a value.  */
1031 struct attribute
1032   {
1033     ENUM_BITFIELD(dwarf_attribute) name : 16;
1034     ENUM_BITFIELD(dwarf_form) form : 15;
1035
1036     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
1037        field should be in u.str (existing only for DW_STRING) but it is kept
1038        here for better struct attribute alignment.  */
1039     unsigned int string_is_canonical : 1;
1040
1041     union
1042       {
1043         const char *str;
1044         struct dwarf_block *blk;
1045         ULONGEST unsnd;
1046         LONGEST snd;
1047         CORE_ADDR addr;
1048         struct signatured_type *signatured_type;
1049       }
1050     u;
1051   };
1052
1053 /* This data structure holds a complete die structure.  */
1054 struct die_info
1055   {
1056     /* DWARF-2 tag for this DIE.  */
1057     ENUM_BITFIELD(dwarf_tag) tag : 16;
1058
1059     /* Number of attributes */
1060     unsigned char num_attrs;
1061
1062     /* True if we're presently building the full type name for the
1063        type derived from this DIE.  */
1064     unsigned char building_fullname : 1;
1065
1066     /* Abbrev number */
1067     unsigned int abbrev;
1068
1069     /* Offset in .debug_info or .debug_types section.  */
1070     sect_offset offset;
1071
1072     /* The dies in a compilation unit form an n-ary tree.  PARENT
1073        points to this die's parent; CHILD points to the first child of
1074        this node; and all the children of a given node are chained
1075        together via their SIBLING fields.  */
1076     struct die_info *child;     /* Its first child, if any.  */
1077     struct die_info *sibling;   /* Its next sibling, if any.  */
1078     struct die_info *parent;    /* Its parent, if any.  */
1079
1080     /* An array of attributes, with NUM_ATTRS elements.  There may be
1081        zero, but it's not common and zero-sized arrays are not
1082        sufficiently portable C.  */
1083     struct attribute attrs[1];
1084   };
1085
1086 /* Get at parts of an attribute structure.  */
1087
1088 #define DW_STRING(attr)    ((attr)->u.str)
1089 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1090 #define DW_UNSND(attr)     ((attr)->u.unsnd)
1091 #define DW_BLOCK(attr)     ((attr)->u.blk)
1092 #define DW_SND(attr)       ((attr)->u.snd)
1093 #define DW_ADDR(attr)      ((attr)->u.addr)
1094 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
1095
1096 /* Blocks are a bunch of untyped bytes.  */
1097 struct dwarf_block
1098   {
1099     size_t size;
1100
1101     /* Valid only if SIZE is not zero.  */
1102     gdb_byte *data;
1103   };
1104
1105 #ifndef ATTR_ALLOC_CHUNK
1106 #define ATTR_ALLOC_CHUNK 4
1107 #endif
1108
1109 /* Allocate fields for structs, unions and enums in this size.  */
1110 #ifndef DW_FIELD_ALLOC_CHUNK
1111 #define DW_FIELD_ALLOC_CHUNK 4
1112 #endif
1113
1114 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1115    but this would require a corresponding change in unpack_field_as_long
1116    and friends.  */
1117 static int bits_per_byte = 8;
1118
1119 /* The routines that read and process dies for a C struct or C++ class
1120    pass lists of data member fields and lists of member function fields
1121    in an instance of a field_info structure, as defined below.  */
1122 struct field_info
1123   {
1124     /* List of data member and baseclasses fields.  */
1125     struct nextfield
1126       {
1127         struct nextfield *next;
1128         int accessibility;
1129         int virtuality;
1130         struct field field;
1131       }
1132      *fields, *baseclasses;
1133
1134     /* Number of fields (including baseclasses).  */
1135     int nfields;
1136
1137     /* Number of baseclasses.  */
1138     int nbaseclasses;
1139
1140     /* Set if the accesibility of one of the fields is not public.  */
1141     int non_public_fields;
1142
1143     /* Member function fields array, entries are allocated in the order they
1144        are encountered in the object file.  */
1145     struct nextfnfield
1146       {
1147         struct nextfnfield *next;
1148         struct fn_field fnfield;
1149       }
1150      *fnfields;
1151
1152     /* Member function fieldlist array, contains name of possibly overloaded
1153        member function, number of overloaded member functions and a pointer
1154        to the head of the member function field chain.  */
1155     struct fnfieldlist
1156       {
1157         const char *name;
1158         int length;
1159         struct nextfnfield *head;
1160       }
1161      *fnfieldlists;
1162
1163     /* Number of entries in the fnfieldlists array.  */
1164     int nfnfields;
1165
1166     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
1167        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
1168     struct typedef_field_list
1169       {
1170         struct typedef_field field;
1171         struct typedef_field_list *next;
1172       }
1173     *typedef_field_list;
1174     unsigned typedef_field_list_count;
1175   };
1176
1177 /* One item on the queue of compilation units to read in full symbols
1178    for.  */
1179 struct dwarf2_queue_item
1180 {
1181   struct dwarf2_per_cu_data *per_cu;
1182   enum language pretend_language;
1183   struct dwarf2_queue_item *next;
1184 };
1185
1186 /* The current queue.  */
1187 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1188
1189 /* Loaded secondary compilation units are kept in memory until they
1190    have not been referenced for the processing of this many
1191    compilation units.  Set this to zero to disable caching.  Cache
1192    sizes of up to at least twenty will improve startup time for
1193    typical inter-CU-reference binaries, at an obvious memory cost.  */
1194 static int dwarf2_max_cache_age = 5;
1195 static void
1196 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1197                            struct cmd_list_element *c, const char *value)
1198 {
1199   fprintf_filtered (file, _("The upper bound on the age of cached "
1200                             "dwarf2 compilation units is %s.\n"),
1201                     value);
1202 }
1203
1204
1205 /* Various complaints about symbol reading that don't abort the process.  */
1206
1207 static void
1208 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1209 {
1210   complaint (&symfile_complaints,
1211              _("statement list doesn't fit in .debug_line section"));
1212 }
1213
1214 static void
1215 dwarf2_debug_line_missing_file_complaint (void)
1216 {
1217   complaint (&symfile_complaints,
1218              _(".debug_line section has line data without a file"));
1219 }
1220
1221 static void
1222 dwarf2_debug_line_missing_end_sequence_complaint (void)
1223 {
1224   complaint (&symfile_complaints,
1225              _(".debug_line section has line "
1226                "program sequence without an end"));
1227 }
1228
1229 static void
1230 dwarf2_complex_location_expr_complaint (void)
1231 {
1232   complaint (&symfile_complaints, _("location expression too complex"));
1233 }
1234
1235 static void
1236 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1237                                               int arg3)
1238 {
1239   complaint (&symfile_complaints,
1240              _("const value length mismatch for '%s', got %d, expected %d"),
1241              arg1, arg2, arg3);
1242 }
1243
1244 static void
1245 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1246 {
1247   complaint (&symfile_complaints,
1248              _("debug info runs off end of %s section"
1249                " [in module %s]"),
1250              section->asection->name,
1251              bfd_get_filename (section->asection->owner));
1252 }
1253
1254 static void
1255 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1256 {
1257   complaint (&symfile_complaints,
1258              _("macro debug info contains a "
1259                "malformed macro definition:\n`%s'"),
1260              arg1);
1261 }
1262
1263 static void
1264 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1265 {
1266   complaint (&symfile_complaints,
1267              _("invalid attribute class or form for '%s' in '%s'"),
1268              arg1, arg2);
1269 }
1270
1271 /* local function prototypes */
1272
1273 static void dwarf2_locate_sections (bfd *, asection *, void *);
1274
1275 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1276                                            struct objfile *);
1277
1278 static void dwarf2_find_base_address (struct die_info *die,
1279                                       struct dwarf2_cu *cu);
1280
1281 static struct partial_symtab *create_partial_symtab
1282   (struct dwarf2_per_cu_data *per_cu, const char *name);
1283
1284 static void dwarf2_build_psymtabs_hard (struct objfile *);
1285
1286 static void scan_partial_symbols (struct partial_die_info *,
1287                                   CORE_ADDR *, CORE_ADDR *,
1288                                   int, struct dwarf2_cu *);
1289
1290 static void add_partial_symbol (struct partial_die_info *,
1291                                 struct dwarf2_cu *);
1292
1293 static void add_partial_namespace (struct partial_die_info *pdi,
1294                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
1295                                    int need_pc, struct dwarf2_cu *cu);
1296
1297 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1298                                 CORE_ADDR *highpc, int need_pc,
1299                                 struct dwarf2_cu *cu);
1300
1301 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1302                                      struct dwarf2_cu *cu);
1303
1304 static void add_partial_subprogram (struct partial_die_info *pdi,
1305                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
1306                                     int need_pc, struct dwarf2_cu *cu);
1307
1308 static void dwarf2_read_symtab (struct partial_symtab *,
1309                                 struct objfile *);
1310
1311 static void psymtab_to_symtab_1 (struct partial_symtab *);
1312
1313 static struct abbrev_info *abbrev_table_lookup_abbrev
1314   (const struct abbrev_table *, unsigned int);
1315
1316 static struct abbrev_table *abbrev_table_read_table
1317   (struct dwarf2_section_info *, sect_offset);
1318
1319 static void abbrev_table_free (struct abbrev_table *);
1320
1321 static void abbrev_table_free_cleanup (void *);
1322
1323 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1324                                  struct dwarf2_section_info *);
1325
1326 static void dwarf2_free_abbrev_table (void *);
1327
1328 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1329
1330 static struct partial_die_info *load_partial_dies
1331   (const struct die_reader_specs *, gdb_byte *, int);
1332
1333 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1334                                    struct partial_die_info *,
1335                                    struct abbrev_info *,
1336                                    unsigned int,
1337                                    gdb_byte *);
1338
1339 static struct partial_die_info *find_partial_die (sect_offset, int,
1340                                                   struct dwarf2_cu *);
1341
1342 static void fixup_partial_die (struct partial_die_info *,
1343                                struct dwarf2_cu *);
1344
1345 static gdb_byte *read_attribute (const struct die_reader_specs *,
1346                                  struct attribute *, struct attr_abbrev *,
1347                                  gdb_byte *);
1348
1349 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1350
1351 static int read_1_signed_byte (bfd *, const gdb_byte *);
1352
1353 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1354
1355 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1356
1357 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1358
1359 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1360                                unsigned int *);
1361
1362 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1363
1364 static LONGEST read_checked_initial_length_and_offset
1365   (bfd *, gdb_byte *, const struct comp_unit_head *,
1366    unsigned int *, unsigned int *);
1367
1368 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1369                             unsigned int *);
1370
1371 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1372
1373 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1374                                        sect_offset);
1375
1376 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1377
1378 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1379
1380 static char *read_indirect_string (bfd *, gdb_byte *,
1381                                    const struct comp_unit_head *,
1382                                    unsigned int *);
1383
1384 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1385
1386 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1387
1388 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1389
1390 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1391                                               unsigned int *);
1392
1393 static char *read_str_index (const struct die_reader_specs *reader,
1394                              struct dwarf2_cu *cu, ULONGEST str_index);
1395
1396 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1397
1398 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1399                                       struct dwarf2_cu *);
1400
1401 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1402                                                 unsigned int);
1403
1404 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1405                                struct dwarf2_cu *cu);
1406
1407 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1408
1409 static struct die_info *die_specification (struct die_info *die,
1410                                            struct dwarf2_cu **);
1411
1412 static void free_line_header (struct line_header *lh);
1413
1414 static void add_file_name (struct line_header *, char *, unsigned int,
1415                            unsigned int, unsigned int);
1416
1417 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1418                                                      struct dwarf2_cu *cu);
1419
1420 static void dwarf_decode_lines (struct line_header *, const char *,
1421                                 struct dwarf2_cu *, struct partial_symtab *,
1422                                 int);
1423
1424 static void dwarf2_start_subfile (char *, const char *, const char *);
1425
1426 static void dwarf2_start_symtab (struct dwarf2_cu *,
1427                                  const char *, const char *, CORE_ADDR);
1428
1429 static struct symbol *new_symbol (struct die_info *, struct type *,
1430                                   struct dwarf2_cu *);
1431
1432 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1433                                        struct dwarf2_cu *, struct symbol *);
1434
1435 static void dwarf2_const_value (struct attribute *, struct symbol *,
1436                                 struct dwarf2_cu *);
1437
1438 static void dwarf2_const_value_attr (struct attribute *attr,
1439                                      struct type *type,
1440                                      const char *name,
1441                                      struct obstack *obstack,
1442                                      struct dwarf2_cu *cu, LONGEST *value,
1443                                      gdb_byte **bytes,
1444                                      struct dwarf2_locexpr_baton **baton);
1445
1446 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1447
1448 static int need_gnat_info (struct dwarf2_cu *);
1449
1450 static struct type *die_descriptive_type (struct die_info *,
1451                                           struct dwarf2_cu *);
1452
1453 static void set_descriptive_type (struct type *, struct die_info *,
1454                                   struct dwarf2_cu *);
1455
1456 static struct type *die_containing_type (struct die_info *,
1457                                          struct dwarf2_cu *);
1458
1459 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1460                                      struct dwarf2_cu *);
1461
1462 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1463
1464 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1465
1466 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1467
1468 static char *typename_concat (struct obstack *obs, const char *prefix,
1469                               const char *suffix, int physname,
1470                               struct dwarf2_cu *cu);
1471
1472 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1473
1474 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1475
1476 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1477
1478 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1479
1480 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1481
1482 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1483                                struct dwarf2_cu *, struct partial_symtab *);
1484
1485 static int dwarf2_get_pc_bounds (struct die_info *,
1486                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1487                                  struct partial_symtab *);
1488
1489 static void get_scope_pc_bounds (struct die_info *,
1490                                  CORE_ADDR *, CORE_ADDR *,
1491                                  struct dwarf2_cu *);
1492
1493 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1494                                         CORE_ADDR, struct dwarf2_cu *);
1495
1496 static void dwarf2_add_field (struct field_info *, struct die_info *,
1497                               struct dwarf2_cu *);
1498
1499 static void dwarf2_attach_fields_to_type (struct field_info *,
1500                                           struct type *, struct dwarf2_cu *);
1501
1502 static void dwarf2_add_member_fn (struct field_info *,
1503                                   struct die_info *, struct type *,
1504                                   struct dwarf2_cu *);
1505
1506 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1507                                              struct type *,
1508                                              struct dwarf2_cu *);
1509
1510 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1511
1512 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1513
1514 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1515
1516 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1517
1518 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1519
1520 static struct type *read_module_type (struct die_info *die,
1521                                       struct dwarf2_cu *cu);
1522
1523 static const char *namespace_name (struct die_info *die,
1524                                    int *is_anonymous, struct dwarf2_cu *);
1525
1526 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1527
1528 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1529
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531                                                        struct dwarf2_cu *);
1532
1533 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1534                                                gdb_byte *info_ptr,
1535                                                gdb_byte **new_info_ptr,
1536                                                struct die_info *parent);
1537
1538 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1539                                                gdb_byte *info_ptr,
1540                                                gdb_byte **new_info_ptr,
1541                                                struct die_info *parent);
1542
1543 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1544                                   struct die_info **, gdb_byte *, int *, int);
1545
1546 static gdb_byte *read_full_die (const struct die_reader_specs *,
1547                                 struct die_info **, gdb_byte *, int *);
1548
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1550
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552                                              struct obstack *);
1553
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1555
1556 static const char *dwarf2_full_name (const char *name,
1557                                      struct die_info *die,
1558                                      struct dwarf2_cu *cu);
1559
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561                                     struct dwarf2_cu *cu);
1562
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564                                           struct dwarf2_cu **);
1565
1566 static const char *dwarf_tag_name (unsigned int);
1567
1568 static const char *dwarf_attr_name (unsigned int);
1569
1570 static const char *dwarf_form_name (unsigned int);
1571
1572 static char *dwarf_bool_name (unsigned int);
1573
1574 static const char *dwarf_type_encoding_name (unsigned int);
1575
1576 static struct die_info *sibling_die (struct die_info *);
1577
1578 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1579
1580 static void dump_die_for_error (struct die_info *);
1581
1582 static void dump_die_1 (struct ui_file *, int level, int max_level,
1583                         struct die_info *);
1584
1585 /*static*/ void dump_die (struct die_info *, int max_level);
1586
1587 static void store_in_ref_table (struct die_info *,
1588                                 struct dwarf2_cu *);
1589
1590 static int is_ref_attr (struct attribute *);
1591
1592 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1593
1594 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1595
1596 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1597                                                struct attribute *,
1598                                                struct dwarf2_cu **);
1599
1600 static struct die_info *follow_die_ref (struct die_info *,
1601                                         struct attribute *,
1602                                         struct dwarf2_cu **);
1603
1604 static struct die_info *follow_die_sig (struct die_info *,
1605                                         struct attribute *,
1606                                         struct dwarf2_cu **);
1607
1608 static struct signatured_type *lookup_signatured_type_at_offset
1609     (struct objfile *objfile,
1610      struct dwarf2_section_info *section, sect_offset offset);
1611
1612 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1613
1614 static void read_signatured_type (struct signatured_type *);
1615
1616 static struct type_unit_group *get_type_unit_group
1617     (struct dwarf2_cu *, struct attribute *);
1618
1619 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1620
1621 /* memory allocation interface */
1622
1623 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1624
1625 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1626
1627 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1628                                  const char *, int);
1629
1630 static int attr_form_is_block (struct attribute *);
1631
1632 static int attr_form_is_section_offset (struct attribute *);
1633
1634 static int attr_form_is_constant (struct attribute *);
1635
1636 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1637                                    struct dwarf2_loclist_baton *baton,
1638                                    struct attribute *attr);
1639
1640 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1641                                          struct symbol *sym,
1642                                          struct dwarf2_cu *cu,
1643                                          int is_block);
1644
1645 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1646                                gdb_byte *info_ptr,
1647                                struct abbrev_info *abbrev);
1648
1649 static void free_stack_comp_unit (void *);
1650
1651 static hashval_t partial_die_hash (const void *item);
1652
1653 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1654
1655 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1656   (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1657
1658 static void init_one_comp_unit (struct dwarf2_cu *cu,
1659                                 struct dwarf2_per_cu_data *per_cu);
1660
1661 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1662                                    struct die_info *comp_unit_die,
1663                                    enum language pretend_language);
1664
1665 static void free_heap_comp_unit (void *);
1666
1667 static void free_cached_comp_units (void *);
1668
1669 static void age_cached_comp_units (void);
1670
1671 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1672
1673 static struct type *set_die_type (struct die_info *, struct type *,
1674                                   struct dwarf2_cu *);
1675
1676 static void create_all_comp_units (struct objfile *);
1677
1678 static int create_all_type_units (struct objfile *);
1679
1680 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1681                                  enum language);
1682
1683 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1684                                     enum language);
1685
1686 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1687                                     enum language);
1688
1689 static void dwarf2_add_dependence (struct dwarf2_cu *,
1690                                    struct dwarf2_per_cu_data *);
1691
1692 static void dwarf2_mark (struct dwarf2_cu *);
1693
1694 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1695
1696 static struct type *get_die_type_at_offset (sect_offset,
1697                                             struct dwarf2_per_cu_data *per_cu);
1698
1699 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1700
1701 static void dwarf2_release_queue (void *dummy);
1702
1703 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1704                              enum language pretend_language);
1705
1706 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1707                                   struct dwarf2_per_cu_data *per_cu,
1708                                   enum language pretend_language);
1709
1710 static void process_queue (void);
1711
1712 static void find_file_and_directory (struct die_info *die,
1713                                      struct dwarf2_cu *cu,
1714                                      const char **name, const char **comp_dir);
1715
1716 static char *file_full_name (int file, struct line_header *lh,
1717                              const char *comp_dir);
1718
1719 static gdb_byte *read_and_check_comp_unit_head
1720   (struct comp_unit_head *header,
1721    struct dwarf2_section_info *section,
1722    struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1723    int is_debug_types_section);
1724
1725 static void init_cutu_and_read_dies
1726   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1727    int use_existing_cu, int keep,
1728    die_reader_func_ftype *die_reader_func, void *data);
1729
1730 static void init_cutu_and_read_dies_simple
1731   (struct dwarf2_per_cu_data *this_cu,
1732    die_reader_func_ftype *die_reader_func, void *data);
1733
1734 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1735
1736 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1737
1738 static struct dwo_unit *lookup_dwo_comp_unit
1739   (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1740
1741 static struct dwo_unit *lookup_dwo_type_unit
1742   (struct signatured_type *, const char *, const char *);
1743
1744 static void free_dwo_file_cleanup (void *);
1745
1746 static void process_cu_includes (void);
1747
1748 static void check_producer (struct dwarf2_cu *cu);
1749
1750 #if WORDS_BIGENDIAN
1751
1752 /* Convert VALUE between big- and little-endian.  */
1753 static offset_type
1754 byte_swap (offset_type value)
1755 {
1756   offset_type result;
1757
1758   result = (value & 0xff) << 24;
1759   result |= (value & 0xff00) << 8;
1760   result |= (value & 0xff0000) >> 8;
1761   result |= (value & 0xff000000) >> 24;
1762   return result;
1763 }
1764
1765 #define MAYBE_SWAP(V)  byte_swap (V)
1766
1767 #else
1768 #define MAYBE_SWAP(V) (V)
1769 #endif /* WORDS_BIGENDIAN */
1770
1771 /* The suffix for an index file.  */
1772 #define INDEX_SUFFIX ".gdb-index"
1773
1774 /* Try to locate the sections we need for DWARF 2 debugging
1775    information and return true if we have enough to do something.
1776    NAMES points to the dwarf2 section names, or is NULL if the standard
1777    ELF names are used.  */
1778
1779 int
1780 dwarf2_has_info (struct objfile *objfile,
1781                  const struct dwarf2_debug_sections *names)
1782 {
1783   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1784   if (!dwarf2_per_objfile)
1785     {
1786       /* Initialize per-objfile state.  */
1787       struct dwarf2_per_objfile *data
1788         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1789
1790       memset (data, 0, sizeof (*data));
1791       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1792       dwarf2_per_objfile = data;
1793
1794       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1795                              (void *) names);
1796       dwarf2_per_objfile->objfile = objfile;
1797     }
1798   return (dwarf2_per_objfile->info.asection != NULL
1799           && dwarf2_per_objfile->abbrev.asection != NULL);
1800 }
1801
1802 /* When loading sections, we look either for uncompressed section or for
1803    compressed section names.  */
1804
1805 static int
1806 section_is_p (const char *section_name,
1807               const struct dwarf2_section_names *names)
1808 {
1809   if (names->normal != NULL
1810       && strcmp (section_name, names->normal) == 0)
1811     return 1;
1812   if (names->compressed != NULL
1813       && strcmp (section_name, names->compressed) == 0)
1814     return 1;
1815   return 0;
1816 }
1817
1818 /* This function is mapped across the sections and remembers the
1819    offset and size of each of the debugging sections we are interested
1820    in.  */
1821
1822 static void
1823 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1824 {
1825   const struct dwarf2_debug_sections *names;
1826   flagword aflag = bfd_get_section_flags (abfd, sectp);
1827
1828   if (vnames == NULL)
1829     names = &dwarf2_elf_names;
1830   else
1831     names = (const struct dwarf2_debug_sections *) vnames;
1832
1833   if ((aflag & SEC_HAS_CONTENTS) == 0)
1834     {
1835     }
1836   else if (section_is_p (sectp->name, &names->info))
1837     {
1838       dwarf2_per_objfile->info.asection = sectp;
1839       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1840     }
1841   else if (section_is_p (sectp->name, &names->abbrev))
1842     {
1843       dwarf2_per_objfile->abbrev.asection = sectp;
1844       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1845     }
1846   else if (section_is_p (sectp->name, &names->line))
1847     {
1848       dwarf2_per_objfile->line.asection = sectp;
1849       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1850     }
1851   else if (section_is_p (sectp->name, &names->loc))
1852     {
1853       dwarf2_per_objfile->loc.asection = sectp;
1854       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1855     }
1856   else if (section_is_p (sectp->name, &names->macinfo))
1857     {
1858       dwarf2_per_objfile->macinfo.asection = sectp;
1859       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1860     }
1861   else if (section_is_p (sectp->name, &names->macro))
1862     {
1863       dwarf2_per_objfile->macro.asection = sectp;
1864       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1865     }
1866   else if (section_is_p (sectp->name, &names->str))
1867     {
1868       dwarf2_per_objfile->str.asection = sectp;
1869       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1870     }
1871   else if (section_is_p (sectp->name, &names->addr))
1872     {
1873       dwarf2_per_objfile->addr.asection = sectp;
1874       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1875     }
1876   else if (section_is_p (sectp->name, &names->frame))
1877     {
1878       dwarf2_per_objfile->frame.asection = sectp;
1879       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1880     }
1881   else if (section_is_p (sectp->name, &names->eh_frame))
1882     {
1883       dwarf2_per_objfile->eh_frame.asection = sectp;
1884       dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1885     }
1886   else if (section_is_p (sectp->name, &names->ranges))
1887     {
1888       dwarf2_per_objfile->ranges.asection = sectp;
1889       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1890     }
1891   else if (section_is_p (sectp->name, &names->types))
1892     {
1893       struct dwarf2_section_info type_section;
1894
1895       memset (&type_section, 0, sizeof (type_section));
1896       type_section.asection = sectp;
1897       type_section.size = bfd_get_section_size (sectp);
1898
1899       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1900                      &type_section);
1901     }
1902   else if (section_is_p (sectp->name, &names->gdb_index))
1903     {
1904       dwarf2_per_objfile->gdb_index.asection = sectp;
1905       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1906     }
1907
1908   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1909       && bfd_section_vma (abfd, sectp) == 0)
1910     dwarf2_per_objfile->has_section_at_zero = 1;
1911 }
1912
1913 /* A helper function that decides whether a section is empty,
1914    or not present.  */
1915
1916 static int
1917 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1918 {
1919   return info->asection == NULL || info->size == 0;
1920 }
1921
1922 /* Read the contents of the section INFO.
1923    OBJFILE is the main object file, but not necessarily the file where
1924    the section comes from.  E.g., for DWO files INFO->asection->owner
1925    is the bfd of the DWO file.
1926    If the section is compressed, uncompress it before returning.  */
1927
1928 static void
1929 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1930 {
1931   asection *sectp = info->asection;
1932   bfd *abfd;
1933   gdb_byte *buf, *retbuf;
1934   unsigned char header[4];
1935
1936   if (info->readin)
1937     return;
1938   info->buffer = NULL;
1939   info->readin = 1;
1940
1941   if (dwarf2_section_empty_p (info))
1942     return;
1943
1944   abfd = sectp->owner;
1945
1946   /* If the section has relocations, we must read it ourselves.
1947      Otherwise we attach it to the BFD.  */
1948   if ((sectp->flags & SEC_RELOC) == 0)
1949     {
1950       const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1951
1952       /* We have to cast away const here for historical reasons.
1953          Fixing dwarf2read to be const-correct would be quite nice.  */
1954       info->buffer = (gdb_byte *) bytes;
1955       return;
1956     }
1957
1958   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1959   info->buffer = buf;
1960
1961   /* When debugging .o files, we may need to apply relocations; see
1962      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1963      We never compress sections in .o files, so we only need to
1964      try this when the section is not compressed.  */
1965   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1966   if (retbuf != NULL)
1967     {
1968       info->buffer = retbuf;
1969       return;
1970     }
1971
1972   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1973       || bfd_bread (buf, info->size, abfd) != info->size)
1974     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1975            bfd_get_filename (abfd));
1976 }
1977
1978 /* A helper function that returns the size of a section in a safe way.
1979    If you are positive that the section has been read before using the
1980    size, then it is safe to refer to the dwarf2_section_info object's
1981    "size" field directly.  In other cases, you must call this
1982    function, because for compressed sections the size field is not set
1983    correctly until the section has been read.  */
1984
1985 static bfd_size_type
1986 dwarf2_section_size (struct objfile *objfile,
1987                      struct dwarf2_section_info *info)
1988 {
1989   if (!info->readin)
1990     dwarf2_read_section (objfile, info);
1991   return info->size;
1992 }
1993
1994 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1995    SECTION_NAME.  */
1996
1997 void
1998 dwarf2_get_section_info (struct objfile *objfile,
1999                          enum dwarf2_section_enum sect,
2000                          asection **sectp, gdb_byte **bufp,
2001                          bfd_size_type *sizep)
2002 {
2003   struct dwarf2_per_objfile *data
2004     = objfile_data (objfile, dwarf2_objfile_data_key);
2005   struct dwarf2_section_info *info;
2006
2007   /* We may see an objfile without any DWARF, in which case we just
2008      return nothing.  */
2009   if (data == NULL)
2010     {
2011       *sectp = NULL;
2012       *bufp = NULL;
2013       *sizep = 0;
2014       return;
2015     }
2016   switch (sect)
2017     {
2018     case DWARF2_DEBUG_FRAME:
2019       info = &data->frame;
2020       break;
2021     case DWARF2_EH_FRAME:
2022       info = &data->eh_frame;
2023       break;
2024     default:
2025       gdb_assert_not_reached ("unexpected section");
2026     }
2027
2028   dwarf2_read_section (objfile, info);
2029
2030   *sectp = info->asection;
2031   *bufp = info->buffer;
2032   *sizep = info->size;
2033 }
2034
2035 /* A helper function to find the sections for a .dwz file.  */
2036
2037 static void
2038 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2039 {
2040   struct dwz_file *dwz_file = arg;
2041
2042   /* Note that we only support the standard ELF names, because .dwz
2043      is ELF-only (at the time of writing).  */
2044   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2045     {
2046       dwz_file->abbrev.asection = sectp;
2047       dwz_file->abbrev.size = bfd_get_section_size (sectp);
2048     }
2049   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2050     {
2051       dwz_file->info.asection = sectp;
2052       dwz_file->info.size = bfd_get_section_size (sectp);
2053     }
2054   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2055     {
2056       dwz_file->str.asection = sectp;
2057       dwz_file->str.size = bfd_get_section_size (sectp);
2058     }
2059   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2060     {
2061       dwz_file->line.asection = sectp;
2062       dwz_file->line.size = bfd_get_section_size (sectp);
2063     }
2064   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2065     {
2066       dwz_file->macro.asection = sectp;
2067       dwz_file->macro.size = bfd_get_section_size (sectp);
2068     }
2069   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2070     {
2071       dwz_file->gdb_index.asection = sectp;
2072       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2073     }
2074 }
2075
2076 /* Open the separate '.dwz' debug file, if needed.  Error if the file
2077    cannot be found.  */
2078
2079 static struct dwz_file *
2080 dwarf2_get_dwz_file (void)
2081 {
2082   bfd *abfd, *dwz_bfd;
2083   asection *section;
2084   gdb_byte *data;
2085   struct cleanup *cleanup;
2086   const char *filename;
2087   struct dwz_file *result;
2088
2089   if (dwarf2_per_objfile->dwz_file != NULL)
2090     return dwarf2_per_objfile->dwz_file;
2091
2092   abfd = dwarf2_per_objfile->objfile->obfd;
2093   section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2094   if (section == NULL)
2095     error (_("could not find '.gnu_debugaltlink' section"));
2096   if (!bfd_malloc_and_get_section (abfd, section, &data))
2097     error (_("could not read '.gnu_debugaltlink' section: %s"),
2098            bfd_errmsg (bfd_get_error ()));
2099   cleanup = make_cleanup (xfree, data);
2100
2101   filename = data;
2102   if (!IS_ABSOLUTE_PATH (filename))
2103     {
2104       char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2105       char *rel;
2106
2107       make_cleanup (xfree, abs);
2108       abs = ldirname (abs);
2109       make_cleanup (xfree, abs);
2110
2111       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2112       make_cleanup (xfree, rel);
2113       filename = rel;
2114     }
2115
2116   /* The format is just a NUL-terminated file name, followed by the
2117      build-id.  For now, though, we ignore the build-id.  */
2118   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2119   if (dwz_bfd == NULL)
2120     error (_("could not read '%s': %s"), filename,
2121            bfd_errmsg (bfd_get_error ()));
2122
2123   if (!bfd_check_format (dwz_bfd, bfd_object))
2124     {
2125       gdb_bfd_unref (dwz_bfd);
2126       error (_("file '%s' was not usable: %s"), filename,
2127              bfd_errmsg (bfd_get_error ()));
2128     }
2129
2130   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2131                            struct dwz_file);
2132   result->dwz_bfd = dwz_bfd;
2133
2134   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2135
2136   do_cleanups (cleanup);
2137
2138   dwarf2_per_objfile->dwz_file = result;
2139   return result;
2140 }
2141 \f
2142 /* DWARF quick_symbols_functions support.  */
2143
2144 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2145    unique line tables, so we maintain a separate table of all .debug_line
2146    derived entries to support the sharing.
2147    All the quick functions need is the list of file names.  We discard the
2148    line_header when we're done and don't need to record it here.  */
2149 struct quick_file_names
2150 {
2151   /* The data used to construct the hash key.  */
2152   struct stmt_list_hash hash;
2153
2154   /* The number of entries in file_names, real_names.  */
2155   unsigned int num_file_names;
2156
2157   /* The file names from the line table, after being run through
2158      file_full_name.  */
2159   const char **file_names;
2160
2161   /* The file names from the line table after being run through
2162      gdb_realpath.  These are computed lazily.  */
2163   const char **real_names;
2164 };
2165
2166 /* When using the index (and thus not using psymtabs), each CU has an
2167    object of this type.  This is used to hold information needed by
2168    the various "quick" methods.  */
2169 struct dwarf2_per_cu_quick_data
2170 {
2171   /* The file table.  This can be NULL if there was no file table
2172      or it's currently not read in.
2173      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
2174   struct quick_file_names *file_names;
2175
2176   /* The corresponding symbol table.  This is NULL if symbols for this
2177      CU have not yet been read.  */
2178   struct symtab *symtab;
2179
2180   /* A temporary mark bit used when iterating over all CUs in
2181      expand_symtabs_matching.  */
2182   unsigned int mark : 1;
2183
2184   /* True if we've tried to read the file table and found there isn't one.
2185      There will be no point in trying to read it again next time.  */
2186   unsigned int no_file_data : 1;
2187 };
2188
2189 /* Utility hash function for a stmt_list_hash.  */
2190
2191 static hashval_t
2192 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2193 {
2194   hashval_t v = 0;
2195
2196   if (stmt_list_hash->dwo_unit != NULL)
2197     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2198   v += stmt_list_hash->line_offset.sect_off;
2199   return v;
2200 }
2201
2202 /* Utility equality function for a stmt_list_hash.  */
2203
2204 static int
2205 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2206                     const struct stmt_list_hash *rhs)
2207 {
2208   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2209     return 0;
2210   if (lhs->dwo_unit != NULL
2211       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2212     return 0;
2213
2214   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2215 }
2216
2217 /* Hash function for a quick_file_names.  */
2218
2219 static hashval_t
2220 hash_file_name_entry (const void *e)
2221 {
2222   const struct quick_file_names *file_data = e;
2223
2224   return hash_stmt_list_entry (&file_data->hash);
2225 }
2226
2227 /* Equality function for a quick_file_names.  */
2228
2229 static int
2230 eq_file_name_entry (const void *a, const void *b)
2231 {
2232   const struct quick_file_names *ea = a;
2233   const struct quick_file_names *eb = b;
2234
2235   return eq_stmt_list_entry (&ea->hash, &eb->hash);
2236 }
2237
2238 /* Delete function for a quick_file_names.  */
2239
2240 static void
2241 delete_file_name_entry (void *e)
2242 {
2243   struct quick_file_names *file_data = e;
2244   int i;
2245
2246   for (i = 0; i < file_data->num_file_names; ++i)
2247     {
2248       xfree ((void*) file_data->file_names[i]);
2249       if (file_data->real_names)
2250         xfree ((void*) file_data->real_names[i]);
2251     }
2252
2253   /* The space for the struct itself lives on objfile_obstack,
2254      so we don't free it here.  */
2255 }
2256
2257 /* Create a quick_file_names hash table.  */
2258
2259 static htab_t
2260 create_quick_file_names_table (unsigned int nr_initial_entries)
2261 {
2262   return htab_create_alloc (nr_initial_entries,
2263                             hash_file_name_entry, eq_file_name_entry,
2264                             delete_file_name_entry, xcalloc, xfree);
2265 }
2266
2267 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
2268    have to be created afterwards.  You should call age_cached_comp_units after
2269    processing PER_CU->CU.  dw2_setup must have been already called.  */
2270
2271 static void
2272 load_cu (struct dwarf2_per_cu_data *per_cu)
2273 {
2274   if (per_cu->is_debug_types)
2275     load_full_type_unit (per_cu);
2276   else
2277     load_full_comp_unit (per_cu, language_minimal);
2278
2279   gdb_assert (per_cu->cu != NULL);
2280
2281   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2282 }
2283
2284 /* Read in the symbols for PER_CU.  */
2285
2286 static void
2287 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2288 {
2289   struct cleanup *back_to;
2290
2291   /* Skip type_unit_groups, reading the type units they contain
2292      is handled elsewhere.  */
2293   if (IS_TYPE_UNIT_GROUP (per_cu))
2294     return;
2295
2296   back_to = make_cleanup (dwarf2_release_queue, NULL);
2297
2298   if (dwarf2_per_objfile->using_index
2299       ? per_cu->v.quick->symtab == NULL
2300       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2301     {
2302       queue_comp_unit (per_cu, language_minimal);
2303       load_cu (per_cu);
2304     }
2305
2306   process_queue ();
2307
2308   /* Age the cache, releasing compilation units that have not
2309      been used recently.  */
2310   age_cached_comp_units ();
2311
2312   do_cleanups (back_to);
2313 }
2314
2315 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
2316    the objfile from which this CU came.  Returns the resulting symbol
2317    table.  */
2318
2319 static struct symtab *
2320 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2321 {
2322   gdb_assert (dwarf2_per_objfile->using_index);
2323   if (!per_cu->v.quick->symtab)
2324     {
2325       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2326       increment_reading_symtab ();
2327       dw2_do_instantiate_symtab (per_cu);
2328       process_cu_includes ();
2329       do_cleanups (back_to);
2330     }
2331   return per_cu->v.quick->symtab;
2332 }
2333
2334 /* Return the CU given its index.
2335
2336    This is intended for loops like:
2337
2338    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2339                     + dwarf2_per_objfile->n_type_units); ++i)
2340      {
2341        struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2342
2343        ...;
2344      }
2345 */
2346
2347 static struct dwarf2_per_cu_data *
2348 dw2_get_cu (int index)
2349 {
2350   if (index >= dwarf2_per_objfile->n_comp_units)
2351     {
2352       index -= dwarf2_per_objfile->n_comp_units;
2353       gdb_assert (index < dwarf2_per_objfile->n_type_units);
2354       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2355     }
2356
2357   return dwarf2_per_objfile->all_comp_units[index];
2358 }
2359
2360 /* Return the primary CU given its index.
2361    The difference between this function and dw2_get_cu is in the handling
2362    of type units (TUs).  Here we return the type_unit_group object.
2363
2364    This is intended for loops like:
2365
2366    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2367                     + dwarf2_per_objfile->n_type_unit_groups); ++i)
2368      {
2369        struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2370
2371        ...;
2372      }
2373 */
2374
2375 static struct dwarf2_per_cu_data *
2376 dw2_get_primary_cu (int index)
2377 {
2378   if (index >= dwarf2_per_objfile->n_comp_units)
2379     {
2380       index -= dwarf2_per_objfile->n_comp_units;
2381       gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2382       return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2383     }
2384
2385   return dwarf2_per_objfile->all_comp_units[index];
2386 }
2387
2388 /* A helper for create_cus_from_index that handles a given list of
2389    CUs.  */
2390
2391 static void
2392 create_cus_from_index_list (struct objfile *objfile,
2393                             const gdb_byte *cu_list, offset_type n_elements,
2394                             struct dwarf2_section_info *section,
2395                             int is_dwz,
2396                             int base_offset)
2397 {
2398   offset_type i;
2399
2400   for (i = 0; i < n_elements; i += 2)
2401     {
2402       struct dwarf2_per_cu_data *the_cu;
2403       ULONGEST offset, length;
2404
2405       gdb_static_assert (sizeof (ULONGEST) >= 8);
2406       offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2407       length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2408       cu_list += 2 * 8;
2409
2410       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2411                                struct dwarf2_per_cu_data);
2412       the_cu->offset.sect_off = offset;
2413       the_cu->length = length;
2414       the_cu->objfile = objfile;
2415       the_cu->section = section;
2416       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2417                                         struct dwarf2_per_cu_quick_data);
2418       the_cu->is_dwz = is_dwz;
2419       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2420     }
2421 }
2422
2423 /* Read the CU list from the mapped index, and use it to create all
2424    the CU objects for this objfile.  */
2425
2426 static void
2427 create_cus_from_index (struct objfile *objfile,
2428                        const gdb_byte *cu_list, offset_type cu_list_elements,
2429                        const gdb_byte *dwz_list, offset_type dwz_elements)
2430 {
2431   struct dwz_file *dwz;
2432
2433   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2434   dwarf2_per_objfile->all_comp_units
2435     = obstack_alloc (&objfile->objfile_obstack,
2436                      dwarf2_per_objfile->n_comp_units
2437                      * sizeof (struct dwarf2_per_cu_data *));
2438
2439   create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2440                               &dwarf2_per_objfile->info, 0, 0);
2441
2442   if (dwz_elements == 0)
2443     return;
2444
2445   dwz = dwarf2_get_dwz_file ();
2446   create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2447                               cu_list_elements / 2);
2448 }
2449
2450 /* Create the signatured type hash table from the index.  */
2451
2452 static void
2453 create_signatured_type_table_from_index (struct objfile *objfile,
2454                                          struct dwarf2_section_info *section,
2455                                          const gdb_byte *bytes,
2456                                          offset_type elements)
2457 {
2458   offset_type i;
2459   htab_t sig_types_hash;
2460
2461   dwarf2_per_objfile->n_type_units = elements / 3;
2462   dwarf2_per_objfile->all_type_units
2463     = obstack_alloc (&objfile->objfile_obstack,
2464                      dwarf2_per_objfile->n_type_units
2465                      * sizeof (struct signatured_type *));
2466
2467   sig_types_hash = allocate_signatured_type_table (objfile);
2468
2469   for (i = 0; i < elements; i += 3)
2470     {
2471       struct signatured_type *sig_type;
2472       ULONGEST offset, type_offset_in_tu, signature;
2473       void **slot;
2474
2475       gdb_static_assert (sizeof (ULONGEST) >= 8);
2476       offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2477       type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2478                                                     BFD_ENDIAN_LITTLE);
2479       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2480       bytes += 3 * 8;
2481
2482       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2483                                  struct signatured_type);
2484       sig_type->signature = signature;
2485       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2486       sig_type->per_cu.is_debug_types = 1;
2487       sig_type->per_cu.section = section;
2488       sig_type->per_cu.offset.sect_off = offset;
2489       sig_type->per_cu.objfile = objfile;
2490       sig_type->per_cu.v.quick
2491         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2492                           struct dwarf2_per_cu_quick_data);
2493
2494       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2495       *slot = sig_type;
2496
2497       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2498     }
2499
2500   dwarf2_per_objfile->signatured_types = sig_types_hash;
2501 }
2502
2503 /* Read the address map data from the mapped index, and use it to
2504    populate the objfile's psymtabs_addrmap.  */
2505
2506 static void
2507 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2508 {
2509   const gdb_byte *iter, *end;
2510   struct obstack temp_obstack;
2511   struct addrmap *mutable_map;
2512   struct cleanup *cleanup;
2513   CORE_ADDR baseaddr;
2514
2515   obstack_init (&temp_obstack);
2516   cleanup = make_cleanup_obstack_free (&temp_obstack);
2517   mutable_map = addrmap_create_mutable (&temp_obstack);
2518
2519   iter = index->address_table;
2520   end = iter + index->address_table_size;
2521
2522   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2523
2524   while (iter < end)
2525     {
2526       ULONGEST hi, lo, cu_index;
2527       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2528       iter += 8;
2529       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2530       iter += 8;
2531       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2532       iter += 4;
2533
2534       if (cu_index < dwarf2_per_objfile->n_comp_units)
2535         {
2536           addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2537                              dw2_get_cu (cu_index));
2538         }
2539       else
2540         {
2541           complaint (&symfile_complaints,
2542                      _(".gdb_index address table has invalid CU number %u"),
2543                      (unsigned) cu_index);
2544         }
2545     }
2546
2547   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2548                                                     &objfile->objfile_obstack);
2549   do_cleanups (cleanup);
2550 }
2551
2552 /* The hash function for strings in the mapped index.  This is the same as
2553    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2554    implementation.  This is necessary because the hash function is tied to the
2555    format of the mapped index file.  The hash values do not have to match with
2556    SYMBOL_HASH_NEXT.
2557    
2558    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2559
2560 static hashval_t
2561 mapped_index_string_hash (int index_version, const void *p)
2562 {
2563   const unsigned char *str = (const unsigned char *) p;
2564   hashval_t r = 0;
2565   unsigned char c;
2566
2567   while ((c = *str++) != 0)
2568     {
2569       if (index_version >= 5)
2570         c = tolower (c);
2571       r = r * 67 + c - 113;
2572     }
2573
2574   return r;
2575 }
2576
2577 /* Find a slot in the mapped index INDEX for the object named NAME.
2578    If NAME is found, set *VEC_OUT to point to the CU vector in the
2579    constant pool and return 1.  If NAME cannot be found, return 0.  */
2580
2581 static int
2582 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2583                           offset_type **vec_out)
2584 {
2585   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2586   offset_type hash;
2587   offset_type slot, step;
2588   int (*cmp) (const char *, const char *);
2589
2590   if (current_language->la_language == language_cplus
2591       || current_language->la_language == language_java
2592       || current_language->la_language == language_fortran)
2593     {
2594       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2595          not contain any.  */
2596       const char *paren = strchr (name, '(');
2597
2598       if (paren)
2599         {
2600           char *dup;
2601
2602           dup = xmalloc (paren - name + 1);
2603           memcpy (dup, name, paren - name);
2604           dup[paren - name] = 0;
2605
2606           make_cleanup (xfree, dup);
2607           name = dup;
2608         }
2609     }
2610
2611   /* Index version 4 did not support case insensitive searches.  But the
2612      indices for case insensitive languages are built in lowercase, therefore
2613      simulate our NAME being searched is also lowercased.  */
2614   hash = mapped_index_string_hash ((index->version == 4
2615                                     && case_sensitivity == case_sensitive_off
2616                                     ? 5 : index->version),
2617                                    name);
2618
2619   slot = hash & (index->symbol_table_slots - 1);
2620   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2621   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2622
2623   for (;;)
2624     {
2625       /* Convert a slot number to an offset into the table.  */
2626       offset_type i = 2 * slot;
2627       const char *str;
2628       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2629         {
2630           do_cleanups (back_to);
2631           return 0;
2632         }
2633
2634       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2635       if (!cmp (name, str))
2636         {
2637           *vec_out = (offset_type *) (index->constant_pool
2638                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2639           do_cleanups (back_to);
2640           return 1;
2641         }
2642
2643       slot = (slot + step) & (index->symbol_table_slots - 1);
2644     }
2645 }
2646
2647 /* A helper function that reads the .gdb_index from SECTION and fills
2648    in MAP.  FILENAME is the name of the file containing the section;
2649    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
2650    ok to use deprecated sections.
2651
2652    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2653    out parameters that are filled in with information about the CU and
2654    TU lists in the section.
2655
2656    Returns 1 if all went well, 0 otherwise.  */
2657
2658 static int
2659 read_index_from_section (struct objfile *objfile,
2660                          const char *filename,
2661                          int deprecated_ok,
2662                          struct dwarf2_section_info *section,
2663                          struct mapped_index *map,
2664                          const gdb_byte **cu_list,
2665                          offset_type *cu_list_elements,
2666                          const gdb_byte **types_list,
2667                          offset_type *types_list_elements)
2668 {
2669   char *addr;
2670   offset_type version;
2671   offset_type *metadata;
2672   int i;
2673
2674   if (dwarf2_section_empty_p (section))
2675     return 0;
2676
2677   /* Older elfutils strip versions could keep the section in the main
2678      executable while splitting it for the separate debug info file.  */
2679   if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2680     return 0;
2681
2682   dwarf2_read_section (objfile, section);
2683
2684   addr = section->buffer;
2685   /* Version check.  */
2686   version = MAYBE_SWAP (*(offset_type *) addr);
2687   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2688      causes the index to behave very poorly for certain requests.  Version 3
2689      contained incomplete addrmap.  So, it seems better to just ignore such
2690      indices.  */
2691   if (version < 4)
2692     {
2693       static int warning_printed = 0;
2694       if (!warning_printed)
2695         {
2696           warning (_("Skipping obsolete .gdb_index section in %s."),
2697                    filename);
2698           warning_printed = 1;
2699         }
2700       return 0;
2701     }
2702   /* Index version 4 uses a different hash function than index version
2703      5 and later.
2704
2705      Versions earlier than 6 did not emit psymbols for inlined
2706      functions.  Using these files will cause GDB not to be able to
2707      set breakpoints on inlined functions by name, so we ignore these
2708      indices unless the user has done
2709      "set use-deprecated-index-sections on".  */
2710   if (version < 6 && !deprecated_ok)
2711     {
2712       static int warning_printed = 0;
2713       if (!warning_printed)
2714         {
2715           warning (_("\
2716 Skipping deprecated .gdb_index section in %s.\n\
2717 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2718 to use the section anyway."),
2719                    filename);
2720           warning_printed = 1;
2721         }
2722       return 0;
2723     }
2724   /* Version 7 indices generated by gold refer to the CU for a symbol instead
2725      of the TU (for symbols coming from TUs).  It's just a performance bug, and
2726      we can't distinguish gdb-generated indices from gold-generated ones, so
2727      nothing to do here.  */
2728
2729   /* Indexes with higher version than the one supported by GDB may be no
2730      longer backward compatible.  */
2731   if (version > 8)
2732     return 0;
2733
2734   map->version = version;
2735   map->total_size = section->size;
2736
2737   metadata = (offset_type *) (addr + sizeof (offset_type));
2738
2739   i = 0;
2740   *cu_list = addr + MAYBE_SWAP (metadata[i]);
2741   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2742                        / 8);
2743   ++i;
2744
2745   *types_list = addr + MAYBE_SWAP (metadata[i]);
2746   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2747                            - MAYBE_SWAP (metadata[i]))
2748                           / 8);
2749   ++i;
2750
2751   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2752   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2753                              - MAYBE_SWAP (metadata[i]));
2754   ++i;
2755
2756   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2757   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2758                               - MAYBE_SWAP (metadata[i]))
2759                              / (2 * sizeof (offset_type)));
2760   ++i;
2761
2762   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2763
2764   return 1;
2765 }
2766
2767
2768 /* Read the index file.  If everything went ok, initialize the "quick"
2769    elements of all the CUs and return 1.  Otherwise, return 0.  */
2770
2771 static int
2772 dwarf2_read_index (struct objfile *objfile)
2773 {
2774   struct mapped_index local_map, *map;
2775   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2776   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2777
2778   if (!read_index_from_section (objfile, objfile->name,
2779                                 use_deprecated_index_sections,
2780                                 &dwarf2_per_objfile->gdb_index, &local_map,
2781                                 &cu_list, &cu_list_elements,
2782                                 &types_list, &types_list_elements))
2783     return 0;
2784
2785   /* Don't use the index if it's empty.  */
2786   if (local_map.symbol_table_slots == 0)
2787     return 0;
2788
2789   /* If there is a .dwz file, read it so we can get its CU list as
2790      well.  */
2791   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2792     {
2793       struct dwz_file *dwz = dwarf2_get_dwz_file ();
2794       struct mapped_index dwz_map;
2795       const gdb_byte *dwz_types_ignore;
2796       offset_type dwz_types_elements_ignore;
2797
2798       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2799                                     1,
2800                                     &dwz->gdb_index, &dwz_map,
2801                                     &dwz_list, &dwz_list_elements,
2802                                     &dwz_types_ignore,
2803                                     &dwz_types_elements_ignore))
2804         {
2805           warning (_("could not read '.gdb_index' section from %s; skipping"),
2806                    bfd_get_filename (dwz->dwz_bfd));
2807           return 0;
2808         }
2809     }
2810
2811   create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2812                          dwz_list_elements);
2813
2814   if (types_list_elements)
2815     {
2816       struct dwarf2_section_info *section;
2817
2818       /* We can only handle a single .debug_types when we have an
2819          index.  */
2820       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2821         return 0;
2822
2823       section = VEC_index (dwarf2_section_info_def,
2824                            dwarf2_per_objfile->types, 0);
2825
2826       create_signatured_type_table_from_index (objfile, section, types_list,
2827                                                types_list_elements);
2828     }
2829
2830   create_addrmap_from_index (objfile, &local_map);
2831
2832   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2833   *map = local_map;
2834
2835   dwarf2_per_objfile->index_table = map;
2836   dwarf2_per_objfile->using_index = 1;
2837   dwarf2_per_objfile->quick_file_names_table =
2838     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2839
2840   return 1;
2841 }
2842
2843 /* A helper for the "quick" functions which sets the global
2844    dwarf2_per_objfile according to OBJFILE.  */
2845
2846 static void
2847 dw2_setup (struct objfile *objfile)
2848 {
2849   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2850   gdb_assert (dwarf2_per_objfile);
2851 }
2852
2853 /* die_reader_func for dw2_get_file_names.  */
2854
2855 static void
2856 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2857                            gdb_byte *info_ptr,
2858                            struct die_info *comp_unit_die,
2859                            int has_children,
2860                            void *data)
2861 {
2862   struct dwarf2_cu *cu = reader->cu;
2863   struct dwarf2_per_cu_data *this_cu = cu->per_cu;  
2864   struct objfile *objfile = dwarf2_per_objfile->objfile;
2865   struct dwarf2_per_cu_data *lh_cu;
2866   struct line_header *lh;
2867   struct attribute *attr;
2868   int i;
2869   const char *name, *comp_dir;
2870   void **slot;
2871   struct quick_file_names *qfn;
2872   unsigned int line_offset;
2873
2874   /* Our callers never want to match partial units -- instead they
2875      will match the enclosing full CU.  */
2876   if (comp_unit_die->tag == DW_TAG_partial_unit)
2877     {
2878       this_cu->v.quick->no_file_data = 1;
2879       return;
2880     }
2881
2882   /* If we're reading the line header for TUs, store it in the "per_cu"
2883      for tu_group.  */
2884   if (this_cu->is_debug_types)
2885     {
2886       struct type_unit_group *tu_group = data;
2887
2888       gdb_assert (tu_group != NULL);
2889       lh_cu = &tu_group->per_cu;
2890     }
2891   else
2892     lh_cu = this_cu;
2893
2894   lh = NULL;
2895   slot = NULL;
2896   line_offset = 0;
2897
2898   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2899   if (attr)
2900     {
2901       struct quick_file_names find_entry;
2902
2903       line_offset = DW_UNSND (attr);
2904
2905       /* We may have already read in this line header (TU line header sharing).
2906          If we have we're done.  */
2907       find_entry.hash.dwo_unit = cu->dwo_unit;
2908       find_entry.hash.line_offset.sect_off = line_offset;
2909       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2910                              &find_entry, INSERT);
2911       if (*slot != NULL)
2912         {
2913           lh_cu->v.quick->file_names = *slot;
2914           return;
2915         }
2916
2917       lh = dwarf_decode_line_header (line_offset, cu);
2918     }
2919   if (lh == NULL)
2920     {
2921       lh_cu->v.quick->no_file_data = 1;
2922       return;
2923     }
2924
2925   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2926   qfn->hash.dwo_unit = cu->dwo_unit;
2927   qfn->hash.line_offset.sect_off = line_offset;
2928   gdb_assert (slot != NULL);
2929   *slot = qfn;
2930
2931   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2932
2933   qfn->num_file_names = lh->num_file_names;
2934   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2935                                    lh->num_file_names * sizeof (char *));
2936   for (i = 0; i < lh->num_file_names; ++i)
2937     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2938   qfn->real_names = NULL;
2939
2940   free_line_header (lh);
2941
2942   lh_cu->v.quick->file_names = qfn;
2943 }
2944
2945 /* A helper for the "quick" functions which attempts to read the line
2946    table for THIS_CU.  */
2947
2948 static struct quick_file_names *
2949 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2950 {
2951   /* For TUs this should only be called on the parent group.  */
2952   if (this_cu->is_debug_types)
2953     gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2954
2955   if (this_cu->v.quick->file_names != NULL)
2956     return this_cu->v.quick->file_names;
2957   /* If we know there is no line data, no point in looking again.  */
2958   if (this_cu->v.quick->no_file_data)
2959     return NULL;
2960
2961   /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2962      in the stub for CUs, there's is no need to lookup the DWO file.
2963      However, that's not the case for TUs where DW_AT_stmt_list lives in the
2964      DWO file.  */
2965   if (this_cu->is_debug_types)
2966     {
2967       struct type_unit_group *tu_group = this_cu->type_unit_group;
2968
2969       init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2970                                dw2_get_file_names_reader, tu_group);
2971     }
2972   else
2973     init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2974
2975   if (this_cu->v.quick->no_file_data)
2976     return NULL;
2977   return this_cu->v.quick->file_names;
2978 }
2979
2980 /* A helper for the "quick" functions which computes and caches the
2981    real path for a given file name from the line table.  */
2982
2983 static const char *
2984 dw2_get_real_path (struct objfile *objfile,
2985                    struct quick_file_names *qfn, int index)
2986 {
2987   if (qfn->real_names == NULL)
2988     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2989                                       qfn->num_file_names, sizeof (char *));
2990
2991   if (qfn->real_names[index] == NULL)
2992     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2993
2994   return qfn->real_names[index];
2995 }
2996
2997 static struct symtab *
2998 dw2_find_last_source_symtab (struct objfile *objfile)
2999 {
3000   int index;
3001
3002   dw2_setup (objfile);
3003   index = dwarf2_per_objfile->n_comp_units - 1;
3004   return dw2_instantiate_symtab (dw2_get_cu (index));
3005 }
3006
3007 /* Traversal function for dw2_forget_cached_source_info.  */
3008
3009 static int
3010 dw2_free_cached_file_names (void **slot, void *info)
3011 {
3012   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3013
3014   if (file_data->real_names)
3015     {
3016       int i;
3017
3018       for (i = 0; i < file_data->num_file_names; ++i)
3019         {
3020           xfree ((void*) file_data->real_names[i]);
3021           file_data->real_names[i] = NULL;
3022         }
3023     }
3024
3025   return 1;
3026 }
3027
3028 static void
3029 dw2_forget_cached_source_info (struct objfile *objfile)
3030 {
3031   dw2_setup (objfile);
3032
3033   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3034                           dw2_free_cached_file_names, NULL);
3035 }
3036
3037 /* Helper function for dw2_map_symtabs_matching_filename that expands
3038    the symtabs and calls the iterator.  */
3039
3040 static int
3041 dw2_map_expand_apply (struct objfile *objfile,
3042                       struct dwarf2_per_cu_data *per_cu,
3043                       const char *name, const char *real_path,
3044                       int (*callback) (struct symtab *, void *),
3045                       void *data)
3046 {
3047   struct symtab *last_made = objfile->symtabs;
3048
3049   /* Don't visit already-expanded CUs.  */
3050   if (per_cu->v.quick->symtab)
3051     return 0;
3052
3053   /* This may expand more than one symtab, and we want to iterate over
3054      all of them.  */
3055   dw2_instantiate_symtab (per_cu);
3056
3057   return iterate_over_some_symtabs (name, real_path, callback, data,
3058                                     objfile->symtabs, last_made);
3059 }
3060
3061 /* Implementation of the map_symtabs_matching_filename method.  */
3062
3063 static int
3064 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3065                                    const char *real_path,
3066                                    int (*callback) (struct symtab *, void *),
3067                                    void *data)
3068 {
3069   int i;
3070   const char *name_basename = lbasename (name);
3071
3072   dw2_setup (objfile);
3073
3074   /* The rule is CUs specify all the files, including those used by
3075      any TU, so there's no need to scan TUs here.  */
3076
3077   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3078     {
3079       int j;
3080       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3081       struct quick_file_names *file_data;
3082
3083       /* We only need to look at symtabs not already expanded.  */
3084       if (per_cu->v.quick->symtab)
3085         continue;
3086
3087       file_data = dw2_get_file_names (per_cu);
3088       if (file_data == NULL)
3089         continue;
3090
3091       for (j = 0; j < file_data->num_file_names; ++j)
3092         {
3093           const char *this_name = file_data->file_names[j];
3094           const char *this_real_name;
3095
3096           if (compare_filenames_for_search (this_name, name))
3097             {
3098               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3099                                         callback, data))
3100                 return 1;
3101               continue;
3102             }
3103
3104           /* Before we invoke realpath, which can get expensive when many
3105              files are involved, do a quick comparison of the basenames.  */
3106           if (! basenames_may_differ
3107               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3108             continue;
3109
3110           this_real_name = dw2_get_real_path (objfile, file_data, j);
3111           if (compare_filenames_for_search (this_real_name, name))
3112             {
3113               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3114                                         callback, data))
3115                 return 1;
3116               continue;
3117             }
3118
3119           if (real_path != NULL)
3120             {
3121               gdb_assert (IS_ABSOLUTE_PATH (real_path));
3122               gdb_assert (IS_ABSOLUTE_PATH (name));
3123               if (this_real_name != NULL
3124                   && FILENAME_CMP (real_path, this_real_name) == 0)
3125                 {
3126                   if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3127                                             callback, data))
3128                     return 1;
3129                   continue;
3130                 }
3131             }
3132         }
3133     }
3134
3135   return 0;
3136 }
3137
3138 /* Struct used to manage iterating over all CUs looking for a symbol.  */
3139
3140 struct dw2_symtab_iterator
3141 {
3142   /* The internalized form of .gdb_index.  */
3143   struct mapped_index *index;
3144   /* If non-zero, only look for symbols that match BLOCK_INDEX.  */
3145   int want_specific_block;
3146   /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3147      Unused if !WANT_SPECIFIC_BLOCK.  */
3148   int block_index;
3149   /* The kind of symbol we're looking for.  */
3150   domain_enum domain;
3151   /* The list of CUs from the index entry of the symbol,
3152      or NULL if not found.  */
3153   offset_type *vec;
3154   /* The next element in VEC to look at.  */
3155   int next;
3156   /* The number of elements in VEC, or zero if there is no match.  */
3157   int length;
3158 };
3159
3160 /* Initialize the index symtab iterator ITER.
3161    If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3162    in block BLOCK_INDEX.  Otherwise BLOCK_INDEX is ignored.  */
3163
3164 static void
3165 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3166                       struct mapped_index *index,
3167                       int want_specific_block,
3168                       int block_index,
3169                       domain_enum domain,
3170                       const char *name)
3171 {
3172   iter->index = index;
3173   iter->want_specific_block = want_specific_block;
3174   iter->block_index = block_index;
3175   iter->domain = domain;
3176   iter->next = 0;
3177
3178   if (find_slot_in_mapped_hash (index, name, &iter->vec))
3179     iter->length = MAYBE_SWAP (*iter->vec);
3180   else
3181     {
3182       iter->vec = NULL;
3183       iter->length = 0;
3184     }
3185 }
3186
3187 /* Return the next matching CU or NULL if there are no more.  */
3188
3189 static struct dwarf2_per_cu_data *
3190 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3191 {
3192   for ( ; iter->next < iter->length; ++iter->next)
3193     {
3194       offset_type cu_index_and_attrs =
3195         MAYBE_SWAP (iter->vec[iter->next + 1]);
3196       offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3197       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3198       int want_static = iter->block_index != GLOBAL_BLOCK;
3199       /* This value is only valid for index versions >= 7.  */
3200       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3201       gdb_index_symbol_kind symbol_kind =
3202         GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3203       /* Only check the symbol attributes if they're present.
3204          Indices prior to version 7 don't record them,
3205          and indices >= 7 may elide them for certain symbols
3206          (gold does this).  */
3207       int attrs_valid =
3208         (iter->index->version >= 7
3209          && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3210
3211       /* Skip if already read in.  */
3212       if (per_cu->v.quick->symtab)
3213         continue;
3214
3215       if (attrs_valid
3216           && iter->want_specific_block
3217           && want_static != is_static)
3218         continue;
3219
3220       /* Only check the symbol's kind if it has one.  */
3221       if (attrs_valid)
3222         {
3223           switch (iter->domain)
3224             {
3225             case VAR_DOMAIN:
3226               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3227                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3228                   /* Some types are also in VAR_DOMAIN.  */
3229                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3230                 continue;
3231               break;
3232             case STRUCT_DOMAIN:
3233               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3234                 continue;
3235               break;
3236             case LABEL_DOMAIN:
3237               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3238                 continue;
3239               break;
3240             default:
3241               break;
3242             }
3243         }
3244
3245       ++iter->next;
3246       return per_cu;
3247     }
3248
3249   return NULL;
3250 }
3251
3252 static struct symtab *
3253 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3254                    const char *name, domain_enum domain)
3255 {
3256   struct symtab *stab_best = NULL;
3257   struct mapped_index *index;
3258
3259   dw2_setup (objfile);
3260
3261   index = dwarf2_per_objfile->index_table;
3262
3263   /* index is NULL if OBJF_READNOW.  */
3264   if (index)
3265     {
3266       struct dw2_symtab_iterator iter;
3267       struct dwarf2_per_cu_data *per_cu;
3268
3269       dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3270
3271       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3272         {
3273           struct symbol *sym = NULL;
3274           struct symtab *stab = dw2_instantiate_symtab (per_cu);
3275
3276           /* Some caution must be observed with overloaded functions
3277              and methods, since the index will not contain any overload
3278              information (but NAME might contain it).  */
3279           if (stab->primary)
3280             {
3281               struct blockvector *bv = BLOCKVECTOR (stab);
3282               struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3283
3284               sym = lookup_block_symbol (block, name, domain);
3285             }
3286
3287           if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3288             {
3289               if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3290                 return stab;
3291
3292               stab_best = stab;
3293             }
3294
3295           /* Keep looking through other CUs.  */
3296         }
3297     }
3298
3299   return stab_best;
3300 }
3301
3302 static void
3303 dw2_print_stats (struct objfile *objfile)
3304 {
3305   int i, total, count;
3306
3307   dw2_setup (objfile);
3308   total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3309   count = 0;
3310   for (i = 0; i < total; ++i)
3311     {
3312       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3313
3314       if (!per_cu->v.quick->symtab)
3315         ++count;
3316     }
3317   printf_filtered (_("  Number of read CUs: %d\n"), total - count);
3318   printf_filtered (_("  Number of unread CUs: %d\n"), count);
3319 }
3320
3321 static void
3322 dw2_dump (struct objfile *objfile)
3323 {
3324   /* Nothing worth printing.  */
3325 }
3326
3327 static void
3328 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3329               struct section_offsets *delta)
3330 {
3331   /* There's nothing to relocate here.  */
3332 }
3333
3334 static void
3335 dw2_expand_symtabs_for_function (struct objfile *objfile,
3336                                  const char *func_name)
3337 {
3338   struct mapped_index *index;
3339
3340   dw2_setup (objfile);
3341
3342   index = dwarf2_per_objfile->index_table;
3343
3344   /* index is NULL if OBJF_READNOW.  */
3345   if (index)
3346     {
3347       struct dw2_symtab_iterator iter;
3348       struct dwarf2_per_cu_data *per_cu;
3349
3350       /* Note: It doesn't matter what we pass for block_index here.  */
3351       dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3352                             func_name);
3353
3354       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3355         dw2_instantiate_symtab (per_cu);
3356     }
3357 }
3358
3359 static void
3360 dw2_expand_all_symtabs (struct objfile *objfile)
3361 {
3362   int i;
3363
3364   dw2_setup (objfile);
3365
3366   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3367                    + dwarf2_per_objfile->n_type_units); ++i)
3368     {
3369       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3370
3371       dw2_instantiate_symtab (per_cu);
3372     }
3373 }
3374
3375 static void
3376 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3377                                   const char *fullname)
3378 {
3379   int i;
3380
3381   dw2_setup (objfile);
3382
3383   /* We don't need to consider type units here.
3384      This is only called for examining code, e.g. expand_line_sal.
3385      There can be an order of magnitude (or more) more type units
3386      than comp units, and we avoid them if we can.  */
3387
3388   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3389     {
3390       int j;
3391       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3392       struct quick_file_names *file_data;
3393
3394       /* We only need to look at symtabs not already expanded.  */
3395       if (per_cu->v.quick->symtab)
3396         continue;
3397
3398       file_data = dw2_get_file_names (per_cu);
3399       if (file_data == NULL)
3400         continue;
3401
3402       for (j = 0; j < file_data->num_file_names; ++j)
3403         {
3404           const char *this_fullname = file_data->file_names[j];
3405
3406           if (filename_cmp (this_fullname, fullname) == 0)
3407             {
3408               dw2_instantiate_symtab (per_cu);
3409               break;
3410             }
3411         }
3412     }
3413 }
3414
3415 /* A helper function for dw2_find_symbol_file that finds the primary
3416    file name for a given CU.  This is a die_reader_func.  */
3417
3418 static void
3419 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3420                                  gdb_byte *info_ptr,
3421                                  struct die_info *comp_unit_die,
3422                                  int has_children,
3423                                  void *data)
3424 {
3425   const char **result_ptr = data;
3426   struct dwarf2_cu *cu = reader->cu;
3427   struct attribute *attr;
3428
3429   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3430   if (attr == NULL)
3431     *result_ptr = NULL;
3432   else
3433     *result_ptr = DW_STRING (attr);
3434 }
3435
3436 static const char *
3437 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3438 {
3439   struct dwarf2_per_cu_data *per_cu;
3440   offset_type *vec;
3441   const char *filename;
3442
3443   dw2_setup (objfile);
3444
3445   /* index_table is NULL if OBJF_READNOW.  */
3446   if (!dwarf2_per_objfile->index_table)
3447     {
3448       struct symtab *s;
3449
3450       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3451         {
3452           struct blockvector *bv = BLOCKVECTOR (s);
3453           const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3454           struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3455
3456           if (sym)
3457             {
3458               /* Only file extension of returned filename is recognized.  */
3459               return SYMBOL_SYMTAB (sym)->filename;
3460             }
3461         }
3462       return NULL;
3463     }
3464
3465   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3466                                  name, &vec))
3467     return NULL;
3468
3469   /* Note that this just looks at the very first one named NAME -- but
3470      actually we are looking for a function.  find_main_filename
3471      should be rewritten so that it doesn't require a custom hook.  It
3472      could just use the ordinary symbol tables.  */
3473   /* vec[0] is the length, which must always be >0.  */
3474   per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3475
3476   if (per_cu->v.quick->symtab != NULL)
3477     {
3478       /* Only file extension of returned filename is recognized.  */
3479       return per_cu->v.quick->symtab->filename;
3480     }
3481
3482   init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3483                            dw2_get_primary_filename_reader, &filename);
3484
3485   /* Only file extension of returned filename is recognized.  */
3486   return filename;
3487 }
3488
3489 static void
3490 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3491                           struct objfile *objfile, int global,
3492                           int (*callback) (struct block *,
3493                                            struct symbol *, void *),
3494                           void *data, symbol_compare_ftype *match,
3495                           symbol_compare_ftype *ordered_compare)
3496 {
3497   /* Currently unimplemented; used for Ada.  The function can be called if the
3498      current language is Ada for a non-Ada objfile using GNU index.  As Ada
3499      does not look for non-Ada symbols this function should just return.  */
3500 }
3501
3502 static void
3503 dw2_expand_symtabs_matching
3504   (struct objfile *objfile,
3505    int (*file_matcher) (const char *, void *, int basenames),
3506    int (*name_matcher) (const char *, void *),
3507    enum search_domain kind,
3508    void *data)
3509 {
3510   int i;
3511   offset_type iter;
3512   struct mapped_index *index;
3513
3514   dw2_setup (objfile);
3515
3516   /* index_table is NULL if OBJF_READNOW.  */
3517   if (!dwarf2_per_objfile->index_table)
3518     return;
3519   index = dwarf2_per_objfile->index_table;
3520
3521   if (file_matcher != NULL)
3522     {
3523       struct cleanup *cleanup;
3524       htab_t visited_found, visited_not_found;
3525
3526       visited_found = htab_create_alloc (10,
3527                                          htab_hash_pointer, htab_eq_pointer,
3528                                          NULL, xcalloc, xfree);
3529       cleanup = make_cleanup_htab_delete (visited_found);
3530       visited_not_found = htab_create_alloc (10,
3531                                              htab_hash_pointer, htab_eq_pointer,
3532                                              NULL, xcalloc, xfree);
3533       make_cleanup_htab_delete (visited_not_found);
3534
3535       /* The rule is CUs specify all the files, including those used by
3536          any TU, so there's no need to scan TUs here.  */
3537
3538       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3539         {
3540           int j;
3541           struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3542           struct quick_file_names *file_data;
3543           void **slot;
3544
3545           per_cu->v.quick->mark = 0;
3546
3547           /* We only need to look at symtabs not already expanded.  */
3548           if (per_cu->v.quick->symtab)
3549             continue;
3550
3551           file_data = dw2_get_file_names (per_cu);
3552           if (file_data == NULL)
3553             continue;
3554
3555           if (htab_find (visited_not_found, file_data) != NULL)
3556             continue;
3557           else if (htab_find (visited_found, file_data) != NULL)
3558             {
3559               per_cu->v.quick->mark = 1;
3560               continue;
3561             }
3562
3563           for (j = 0; j < file_data->num_file_names; ++j)
3564             {
3565               const char *this_real_name;
3566
3567               if (file_matcher (file_data->file_names[j], data, 0))
3568                 {
3569                   per_cu->v.quick->mark = 1;
3570                   break;
3571                 }
3572
3573               /* Before we invoke realpath, which can get expensive when many
3574                  files are involved, do a quick comparison of the basenames.  */
3575               if (!basenames_may_differ
3576                   && !file_matcher (lbasename (file_data->file_names[j]),
3577                                     data, 1))
3578                 continue;
3579
3580               this_real_name = dw2_get_real_path (objfile, file_data, j);
3581               if (file_matcher (this_real_name, data, 0))
3582                 {
3583                   per_cu->v.quick->mark = 1;
3584                   break;
3585                 }
3586             }
3587
3588           slot = htab_find_slot (per_cu->v.quick->mark
3589                                  ? visited_found
3590                                  : visited_not_found,
3591                                  file_data, INSERT);
3592           *slot = file_data;
3593         }
3594
3595       do_cleanups (cleanup);
3596     }
3597
3598   for (iter = 0; iter < index->symbol_table_slots; ++iter)
3599     {
3600       offset_type idx = 2 * iter;
3601       const char *name;
3602       offset_type *vec, vec_len, vec_idx;
3603
3604       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3605         continue;
3606
3607       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3608
3609       if (! (*name_matcher) (name, data))
3610         continue;
3611
3612       /* The name was matched, now expand corresponding CUs that were
3613          marked.  */
3614       vec = (offset_type *) (index->constant_pool
3615                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
3616       vec_len = MAYBE_SWAP (vec[0]);
3617       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3618         {
3619           struct dwarf2_per_cu_data *per_cu;
3620           offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3621           gdb_index_symbol_kind symbol_kind =
3622             GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3623           int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3624
3625           /* Don't crash on bad data.  */
3626           if (cu_index >= (dwarf2_per_objfile->n_comp_units
3627                            + dwarf2_per_objfile->n_type_units))
3628             continue;
3629
3630           /* Only check the symbol's kind if it has one.
3631              Indices prior to version 7 don't record it.  */
3632           if (index->version >= 7)
3633             {
3634               switch (kind)
3635                 {
3636                 case VARIABLES_DOMAIN:
3637                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3638                     continue;
3639                   break;
3640                 case FUNCTIONS_DOMAIN:
3641                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3642                     continue;
3643                   break;
3644                 case TYPES_DOMAIN:
3645                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3646                     continue;
3647                   break;
3648                 default:
3649                   break;
3650                 }
3651             }
3652
3653           per_cu = dw2_get_cu (cu_index);
3654           if (file_matcher == NULL || per_cu->v.quick->mark)
3655             dw2_instantiate_symtab (per_cu);
3656         }
3657     }
3658 }
3659
3660 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3661    symtab.  */
3662
3663 static struct symtab *
3664 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3665 {
3666   int i;
3667
3668   if (BLOCKVECTOR (symtab) != NULL
3669       && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3670     return symtab;
3671
3672   if (symtab->includes == NULL)
3673     return NULL;
3674
3675   for (i = 0; symtab->includes[i]; ++i)
3676     {
3677       struct symtab *s = symtab->includes[i];
3678
3679       s = recursively_find_pc_sect_symtab (s, pc);
3680       if (s != NULL)
3681         return s;
3682     }
3683
3684   return NULL;
3685 }
3686
3687 static struct symtab *
3688 dw2_find_pc_sect_symtab (struct objfile *objfile,
3689                          struct minimal_symbol *msymbol,
3690                          CORE_ADDR pc,
3691                          struct obj_section *section,
3692                          int warn_if_readin)
3693 {
3694   struct dwarf2_per_cu_data *data;
3695   struct symtab *result;
3696
3697   dw2_setup (objfile);
3698
3699   if (!objfile->psymtabs_addrmap)
3700     return NULL;
3701
3702   data = addrmap_find (objfile->psymtabs_addrmap, pc);
3703   if (!data)
3704     return NULL;
3705
3706   if (warn_if_readin && data->v.quick->symtab)
3707     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3708              paddress (get_objfile_arch (objfile), pc));
3709
3710   result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3711   gdb_assert (result != NULL);
3712   return result;
3713 }
3714
3715 static void
3716 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3717                           void *data, int need_fullname)
3718 {
3719   int i;
3720   struct cleanup *cleanup;
3721   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3722                                       NULL, xcalloc, xfree);
3723
3724   cleanup = make_cleanup_htab_delete (visited);
3725   dw2_setup (objfile);
3726
3727   /* The rule is CUs specify all the files, including those used by
3728      any TU, so there's no need to scan TUs here.
3729      We can ignore file names coming from already-expanded CUs.  */
3730
3731   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3732     {
3733       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3734
3735       if (per_cu->v.quick->symtab)
3736         {
3737           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3738                                         INSERT);
3739
3740           *slot = per_cu->v.quick->file_names;
3741         }
3742     }
3743
3744   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3745     {
3746       int j;
3747       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3748       struct quick_file_names *file_data;
3749       void **slot;
3750
3751       /* We only need to look at symtabs not already expanded.  */
3752       if (per_cu->v.quick->symtab)
3753         continue;
3754
3755       file_data = dw2_get_file_names (per_cu);
3756       if (file_data == NULL)
3757         continue;
3758
3759       slot = htab_find_slot (visited, file_data, INSERT);
3760       if (*slot)
3761         {
3762           /* Already visited.  */
3763           continue;
3764         }
3765       *slot = file_data;
3766
3767       for (j = 0; j < file_data->num_file_names; ++j)
3768         {
3769           const char *this_real_name;
3770
3771           if (need_fullname)
3772             this_real_name = dw2_get_real_path (objfile, file_data, j);
3773           else
3774             this_real_name = NULL;
3775           (*fun) (file_data->file_names[j], this_real_name, data);
3776         }
3777     }
3778
3779   do_cleanups (cleanup);
3780 }
3781
3782 static int
3783 dw2_has_symbols (struct objfile *objfile)
3784 {
3785   return 1;
3786 }
3787
3788 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3789 {
3790   dw2_has_symbols,
3791   dw2_find_last_source_symtab,
3792   dw2_forget_cached_source_info,
3793   dw2_map_symtabs_matching_filename,
3794   dw2_lookup_symbol,
3795   dw2_print_stats,
3796   dw2_dump,
3797   dw2_relocate,
3798   dw2_expand_symtabs_for_function,
3799   dw2_expand_all_symtabs,
3800   dw2_expand_symtabs_with_fullname,
3801   dw2_find_symbol_file,
3802   dw2_map_matching_symbols,
3803   dw2_expand_symtabs_matching,
3804   dw2_find_pc_sect_symtab,
3805   dw2_map_symbol_filenames
3806 };
3807
3808 /* Initialize for reading DWARF for this objfile.  Return 0 if this
3809    file will use psymtabs, or 1 if using the GNU index.  */
3810
3811 int
3812 dwarf2_initialize_objfile (struct objfile *objfile)
3813 {
3814   /* If we're about to read full symbols, don't bother with the
3815      indices.  In this case we also don't care if some other debug
3816      format is making psymtabs, because they are all about to be
3817      expanded anyway.  */
3818   if ((objfile->flags & OBJF_READNOW))
3819     {
3820       int i;
3821
3822       dwarf2_per_objfile->using_index = 1;
3823       create_all_comp_units (objfile);
3824       create_all_type_units (objfile);
3825       dwarf2_per_objfile->quick_file_names_table =
3826         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3827
3828       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3829                        + dwarf2_per_objfile->n_type_units); ++i)
3830         {
3831           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3832
3833           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3834                                             struct dwarf2_per_cu_quick_data);
3835         }
3836
3837       /* Return 1 so that gdb sees the "quick" functions.  However,
3838          these functions will be no-ops because we will have expanded
3839          all symtabs.  */
3840       return 1;
3841     }
3842
3843   if (dwarf2_read_index (objfile))
3844     return 1;
3845
3846   return 0;
3847 }
3848
3849 \f
3850
3851 /* Build a partial symbol table.  */
3852
3853 void
3854 dwarf2_build_psymtabs (struct objfile *objfile)
3855 {
3856   volatile struct gdb_exception except;
3857
3858   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3859     {
3860       init_psymbol_list (objfile, 1024);
3861     }
3862
3863   TRY_CATCH (except, RETURN_MASK_ERROR)
3864     {
3865       /* This isn't really ideal: all the data we allocate on the
3866          objfile's obstack is still uselessly kept around.  However,
3867          freeing it seems unsafe.  */
3868       struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3869
3870       dwarf2_build_psymtabs_hard (objfile);
3871       discard_cleanups (cleanups);
3872     }
3873   if (except.reason < 0)
3874     exception_print (gdb_stderr, except);
3875 }
3876
3877 /* Return the total length of the CU described by HEADER.  */
3878
3879 static unsigned int
3880 get_cu_length (const struct comp_unit_head *header)
3881 {
3882   return header->initial_length_size + header->length;
3883 }
3884
3885 /* Return TRUE if OFFSET is within CU_HEADER.  */
3886
3887 static inline int
3888 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3889 {
3890   sect_offset bottom = { cu_header->offset.sect_off };
3891   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3892
3893   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3894 }
3895
3896 /* Find the base address of the compilation unit for range lists and
3897    location lists.  It will normally be specified by DW_AT_low_pc.
3898    In DWARF-3 draft 4, the base address could be overridden by
3899    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3900    compilation units with discontinuous ranges.  */
3901
3902 static void
3903 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3904 {
3905   struct attribute *attr;
3906
3907   cu->base_known = 0;
3908   cu->base_address = 0;
3909
3910   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3911   if (attr)
3912     {
3913       cu->base_address = DW_ADDR (attr);
3914       cu->base_known = 1;
3915     }
3916   else
3917     {
3918       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3919       if (attr)
3920         {
3921           cu->base_address = DW_ADDR (attr);
3922           cu->base_known = 1;
3923         }
3924     }
3925 }
3926
3927 /* Read in the comp unit header information from the debug_info at info_ptr.
3928    NOTE: This leaves members offset, first_die_offset to be filled in
3929    by the caller.  */
3930
3931 static gdb_byte *
3932 read_comp_unit_head (struct comp_unit_head *cu_header,
3933                      gdb_byte *info_ptr, bfd *abfd)
3934 {
3935   int signed_addr;
3936   unsigned int bytes_read;
3937
3938   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3939   cu_header->initial_length_size = bytes_read;
3940   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3941   info_ptr += bytes_read;
3942   cu_header->version = read_2_bytes (abfd, info_ptr);
3943   info_ptr += 2;
3944   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3945                                              &bytes_read);
3946   info_ptr += bytes_read;
3947   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3948   info_ptr += 1;
3949   signed_addr = bfd_get_sign_extend_vma (abfd);
3950   if (signed_addr < 0)
3951     internal_error (__FILE__, __LINE__,
3952                     _("read_comp_unit_head: dwarf from non elf file"));
3953   cu_header->signed_addr_p = signed_addr;
3954
3955   return info_ptr;
3956 }
3957
3958 /* Helper function that returns the proper abbrev section for
3959    THIS_CU.  */
3960
3961 static struct dwarf2_section_info *
3962 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3963 {
3964   struct dwarf2_section_info *abbrev;
3965
3966   if (this_cu->is_dwz)
3967     abbrev = &dwarf2_get_dwz_file ()->abbrev;
3968   else
3969     abbrev = &dwarf2_per_objfile->abbrev;
3970
3971   return abbrev;
3972 }
3973
3974 /* Subroutine of read_and_check_comp_unit_head and
3975    read_and_check_type_unit_head to simplify them.
3976    Perform various error checking on the header.  */
3977
3978 static void
3979 error_check_comp_unit_head (struct comp_unit_head *header,
3980                             struct dwarf2_section_info *section,
3981                             struct dwarf2_section_info *abbrev_section)
3982 {
3983   bfd *abfd = section->asection->owner;
3984   const char *filename = bfd_get_filename (abfd);
3985
3986   if (header->version != 2 && header->version != 3 && header->version != 4)
3987     error (_("Dwarf Error: wrong version in compilation unit header "
3988            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3989            filename);
3990
3991   if (header->abbrev_offset.sect_off
3992       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3993     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3994            "(offset 0x%lx + 6) [in module %s]"),
3995            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3996            filename);
3997
3998   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3999      avoid potential 32-bit overflow.  */
4000   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4001       > section->size)
4002     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4003            "(offset 0x%lx + 0) [in module %s]"),
4004            (long) header->length, (long) header->offset.sect_off,
4005            filename);
4006 }
4007
4008 /* Read in a CU/TU header and perform some basic error checking.
4009    The contents of the header are stored in HEADER.
4010    The result is a pointer to the start of the first DIE.  */
4011
4012 static gdb_byte *
4013 read_and_check_comp_unit_head (struct comp_unit_head *header,
4014                                struct dwarf2_section_info *section,
4015                                struct dwarf2_section_info *abbrev_section,
4016                                gdb_byte *info_ptr,
4017                                int is_debug_types_section)
4018 {
4019   gdb_byte *beg_of_comp_unit = info_ptr;
4020   bfd *abfd = section->asection->owner;
4021
4022   header->offset.sect_off = beg_of_comp_unit - section->buffer;
4023
4024   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4025
4026   /* If we're reading a type unit, skip over the signature and
4027      type_offset fields.  */
4028   if (is_debug_types_section)
4029     info_ptr += 8 /*signature*/ + header->offset_size;
4030
4031   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4032
4033   error_check_comp_unit_head (header, section, abbrev_section);
4034
4035   return info_ptr;
4036 }
4037
4038 /* Read in the types comp unit header information from .debug_types entry at
4039    types_ptr.  The result is a pointer to one past the end of the header.  */
4040
4041 static gdb_byte *
4042 read_and_check_type_unit_head (struct comp_unit_head *header,
4043                                struct dwarf2_section_info *section,
4044                                struct dwarf2_section_info *abbrev_section,
4045                                gdb_byte *info_ptr,
4046                                ULONGEST *signature,
4047                                cu_offset *type_offset_in_tu)
4048 {
4049   gdb_byte *beg_of_comp_unit = info_ptr;
4050   bfd *abfd = section->asection->owner;
4051
4052   header->offset.sect_off = beg_of_comp_unit - section->buffer;
4053
4054   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4055
4056   /* If we're reading a type unit, skip over the signature and
4057      type_offset fields.  */
4058   if (signature != NULL)
4059     *signature = read_8_bytes (abfd, info_ptr);
4060   info_ptr += 8;
4061   if (type_offset_in_tu != NULL)
4062     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4063                                                header->offset_size);
4064   info_ptr += header->offset_size;
4065
4066   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4067
4068   error_check_comp_unit_head (header, section, abbrev_section);
4069
4070   return info_ptr;
4071 }
4072
4073 /* Fetch the abbreviation table offset from a comp or type unit header.  */
4074
4075 static sect_offset
4076 read_abbrev_offset (struct dwarf2_section_info *section,
4077                     sect_offset offset)
4078 {
4079   bfd *abfd = section->asection->owner;
4080   gdb_byte *info_ptr;
4081   unsigned int length, initial_length_size, offset_size;
4082   sect_offset abbrev_offset;
4083
4084   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4085   info_ptr = section->buffer + offset.sect_off;
4086   length = read_initial_length (abfd, info_ptr, &initial_length_size);
4087   offset_size = initial_length_size == 4 ? 4 : 8;
4088   info_ptr += initial_length_size + 2 /*version*/;
4089   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4090   return abbrev_offset;
4091 }
4092
4093 /* Allocate a new partial symtab for file named NAME and mark this new
4094    partial symtab as being an include of PST.  */
4095
4096 static void
4097 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
4098                                struct objfile *objfile)
4099 {
4100   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4101
4102   if (!IS_ABSOLUTE_PATH (subpst->filename))
4103     {
4104       /* It shares objfile->objfile_obstack.  */
4105       subpst->dirname = pst->dirname;
4106     }
4107
4108   subpst->section_offsets = pst->section_offsets;
4109   subpst->textlow = 0;
4110   subpst->texthigh = 0;
4111
4112   subpst->dependencies = (struct partial_symtab **)
4113     obstack_alloc (&objfile->objfile_obstack,
4114                    sizeof (struct partial_symtab *));
4115   subpst->dependencies[0] = pst;
4116   subpst->number_of_dependencies = 1;
4117
4118   subpst->globals_offset = 0;
4119   subpst->n_global_syms = 0;
4120   subpst->statics_offset = 0;
4121   subpst->n_static_syms = 0;
4122   subpst->symtab = NULL;
4123   subpst->read_symtab = pst->read_symtab;
4124   subpst->readin = 0;
4125
4126   /* No private part is necessary for include psymtabs.  This property
4127      can be used to differentiate between such include psymtabs and
4128      the regular ones.  */
4129   subpst->read_symtab_private = NULL;
4130 }
4131
4132 /* Read the Line Number Program data and extract the list of files
4133    included by the source file represented by PST.  Build an include
4134    partial symtab for each of these included files.  */
4135
4136 static void
4137 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4138                                struct die_info *die,
4139                                struct partial_symtab *pst)
4140 {
4141   struct line_header *lh = NULL;
4142   struct attribute *attr;
4143
4144   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4145   if (attr)
4146     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4147   if (lh == NULL)
4148     return;  /* No linetable, so no includes.  */
4149
4150   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
4151   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4152
4153   free_line_header (lh);
4154 }
4155
4156 static hashval_t
4157 hash_signatured_type (const void *item)
4158 {
4159   const struct signatured_type *sig_type = item;
4160
4161   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
4162   return sig_type->signature;
4163 }
4164
4165 static int
4166 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4167 {
4168   const struct signatured_type *lhs = item_lhs;
4169   const struct signatured_type *rhs = item_rhs;
4170
4171   return lhs->signature == rhs->signature;
4172 }
4173
4174 /* Allocate a hash table for signatured types.  */
4175
4176 static htab_t
4177 allocate_signatured_type_table (struct objfile *objfile)
4178 {
4179   return htab_create_alloc_ex (41,
4180                                hash_signatured_type,
4181                                eq_signatured_type,
4182                                NULL,
4183                                &objfile->objfile_obstack,
4184                                hashtab_obstack_allocate,
4185                                dummy_obstack_deallocate);
4186 }
4187
4188 /* A helper function to add a signatured type CU to a table.  */
4189
4190 static int
4191 add_signatured_type_cu_to_table (void **slot, void *datum)
4192 {
4193   struct signatured_type *sigt = *slot;
4194   struct signatured_type ***datap = datum;
4195
4196   **datap = sigt;
4197   ++*datap;
4198
4199   return 1;
4200 }
4201
4202 /* Create the hash table of all entries in the .debug_types section.
4203    DWO_FILE is a pointer to the DWO file for .debug_types.dwo,
4204    NULL otherwise.
4205    Note: This function processes DWO files only, not DWP files.
4206    The result is a pointer to the hash table or NULL if there are
4207    no types.  */
4208
4209 static htab_t
4210 create_debug_types_hash_table (struct dwo_file *dwo_file,
4211                                VEC (dwarf2_section_info_def) *types)
4212 {
4213   struct objfile *objfile = dwarf2_per_objfile->objfile;
4214   htab_t types_htab = NULL;
4215   int ix;
4216   struct dwarf2_section_info *section;
4217   struct dwarf2_section_info *abbrev_section;
4218
4219   if (VEC_empty (dwarf2_section_info_def, types))
4220     return NULL;
4221
4222   abbrev_section = (dwo_file != NULL
4223                     ? &dwo_file->sections.abbrev
4224                     : &dwarf2_per_objfile->abbrev);
4225
4226   if (dwarf2_read_debug)
4227     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4228                         dwo_file ? ".dwo" : "",
4229                         bfd_get_filename (abbrev_section->asection->owner));
4230
4231   for (ix = 0;
4232        VEC_iterate (dwarf2_section_info_def, types, ix, section);
4233        ++ix)
4234     {
4235       bfd *abfd;
4236       gdb_byte *info_ptr, *end_ptr;
4237       struct dwarf2_section_info *abbrev_section;
4238
4239       dwarf2_read_section (objfile, section);
4240       info_ptr = section->buffer;
4241
4242       if (info_ptr == NULL)
4243         continue;
4244
4245       /* We can't set abfd until now because the section may be empty or
4246          not present, in which case section->asection will be NULL.  */
4247       abfd = section->asection->owner;
4248
4249       if (dwo_file)
4250         abbrev_section = &dwo_file->sections.abbrev;
4251       else
4252         abbrev_section = &dwarf2_per_objfile->abbrev;
4253
4254       /* We don't use init_cutu_and_read_dies_simple, or some such, here
4255          because we don't need to read any dies: the signature is in the
4256          header.  */
4257
4258       end_ptr = info_ptr + section->size;
4259       while (info_ptr < end_ptr)
4260         {
4261           sect_offset offset;
4262           cu_offset type_offset_in_tu;
4263           ULONGEST signature;
4264           struct signatured_type *sig_type;
4265           struct dwo_unit *dwo_tu;
4266           void **slot;
4267           gdb_byte *ptr = info_ptr;
4268           struct comp_unit_head header;
4269           unsigned int length;
4270
4271           offset.sect_off = ptr - section->buffer;
4272
4273           /* We need to read the type's signature in order to build the hash
4274              table, but we don't need anything else just yet.  */
4275
4276           ptr = read_and_check_type_unit_head (&header, section,
4277                                                abbrev_section, ptr,
4278                                                &signature, &type_offset_in_tu);
4279
4280           length = get_cu_length (&header);
4281
4282           /* Skip dummy type units.  */
4283           if (ptr >= info_ptr + length
4284               || peek_abbrev_code (abfd, ptr) == 0)
4285             {
4286               info_ptr += length;
4287               continue;
4288             }
4289
4290           if (types_htab == NULL)
4291             {
4292               if (dwo_file)
4293                 types_htab = allocate_dwo_unit_table (objfile);
4294               else
4295                 types_htab = allocate_signatured_type_table (objfile);
4296             }
4297
4298           if (dwo_file)
4299             {
4300               sig_type = NULL;
4301               dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4302                                        struct dwo_unit);
4303               dwo_tu->dwo_file = dwo_file;
4304               dwo_tu->signature = signature;
4305               dwo_tu->type_offset_in_tu = type_offset_in_tu;
4306               dwo_tu->section = section;
4307               dwo_tu->offset = offset;
4308               dwo_tu->length = length;
4309             }
4310           else
4311             {
4312               /* N.B.: type_offset is not usable if this type uses a DWO file.
4313                  The real type_offset is in the DWO file.  */
4314               dwo_tu = NULL;
4315               sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4316                                          struct signatured_type);
4317               sig_type->signature = signature;
4318               sig_type->type_offset_in_tu = type_offset_in_tu;
4319               sig_type->per_cu.objfile = objfile;
4320               sig_type->per_cu.is_debug_types = 1;
4321               sig_type->per_cu.section = section;
4322               sig_type->per_cu.offset = offset;
4323               sig_type->per_cu.length = length;
4324             }
4325
4326           slot = htab_find_slot (types_htab,
4327                                  dwo_file ? (void*) dwo_tu : (void *) sig_type,
4328                                  INSERT);
4329           gdb_assert (slot != NULL);
4330           if (*slot != NULL)
4331             {
4332               sect_offset dup_offset;
4333
4334               if (dwo_file)
4335                 {
4336                   const struct dwo_unit *dup_tu = *slot;
4337
4338                   dup_offset = dup_tu->offset;
4339                 }
4340               else
4341                 {
4342                   const struct signatured_type *dup_tu = *slot;
4343
4344                   dup_offset = dup_tu->per_cu.offset;
4345                 }
4346
4347               complaint (&symfile_complaints,
4348                          _("debug type entry at offset 0x%x is duplicate to the "
4349                            "entry at offset 0x%x, signature 0x%s"),
4350                          offset.sect_off, dup_offset.sect_off,
4351                          phex (signature, sizeof (signature)));
4352             }
4353           *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4354
4355           if (dwarf2_read_debug)
4356             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
4357                                 offset.sect_off,
4358                                 phex (signature, sizeof (signature)));
4359
4360           info_ptr += length;
4361         }
4362     }
4363
4364   return types_htab;
4365 }
4366
4367 /* Create the hash table of all entries in the .debug_types section,
4368    and initialize all_type_units.
4369    The result is zero if there is an error (e.g. missing .debug_types section),
4370    otherwise non-zero.  */
4371
4372 static int
4373 create_all_type_units (struct objfile *objfile)
4374 {
4375   htab_t types_htab;
4376   struct signatured_type **iter;
4377
4378   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4379   if (types_htab == NULL)
4380     {
4381       dwarf2_per_objfile->signatured_types = NULL;
4382       return 0;
4383     }
4384
4385   dwarf2_per_objfile->signatured_types = types_htab;
4386
4387   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4388   dwarf2_per_objfile->all_type_units
4389     = obstack_alloc (&objfile->objfile_obstack,
4390                      dwarf2_per_objfile->n_type_units
4391                      * sizeof (struct signatured_type *));
4392   iter = &dwarf2_per_objfile->all_type_units[0];
4393   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4394   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4395               == dwarf2_per_objfile->n_type_units);
4396
4397   return 1;
4398 }
4399
4400 /* Lookup a signature based type for DW_FORM_ref_sig8.
4401    Returns NULL if signature SIG is not present in the table.
4402    It is up to the caller to complain about this.  */
4403
4404 static struct signatured_type *
4405 lookup_signatured_type (ULONGEST sig)
4406 {
4407   struct signatured_type find_entry, *entry;
4408
4409   if (dwarf2_per_objfile->signatured_types == NULL)
4410     return NULL;
4411   find_entry.signature = sig;
4412   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4413   return entry;
4414 }
4415 \f
4416 /* Low level DIE reading support.  */
4417
4418 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
4419
4420 static void
4421 init_cu_die_reader (struct die_reader_specs *reader,
4422                     struct dwarf2_cu *cu,
4423                     struct dwarf2_section_info *section,
4424                     struct dwo_file *dwo_file)
4425 {
4426   gdb_assert (section->readin && section->buffer != NULL);
4427   reader->abfd = section->asection->owner;
4428   reader->cu = cu;
4429   reader->dwo_file = dwo_file;
4430   reader->die_section = section;
4431   reader->buffer = section->buffer;
4432   reader->buffer_end = section->buffer + section->size;
4433 }
4434
4435 /* Initialize a CU (or TU) and read its DIEs.
4436    If the CU defers to a DWO file, read the DWO file as well.
4437
4438    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4439    Otherwise the table specified in the comp unit header is read in and used.
4440    This is an optimization for when we already have the abbrev table.
4441
4442    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4443    Otherwise, a new CU is allocated with xmalloc.
4444
4445    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4446    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.
4447
4448    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4449    linker) then DIE_READER_FUNC will not get called.  */
4450
4451 static void
4452 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4453                          struct abbrev_table *abbrev_table,
4454                          int use_existing_cu, int keep,
4455                          die_reader_func_ftype *die_reader_func,
4456                          void *data)
4457 {
4458   struct objfile *objfile = dwarf2_per_objfile->objfile;
4459   struct dwarf2_section_info *section = this_cu->section;
4460   bfd *abfd = section->asection->owner;
4461   struct dwarf2_cu *cu;
4462   gdb_byte *begin_info_ptr, *info_ptr;
4463   struct die_reader_specs reader;
4464   struct die_info *comp_unit_die;
4465   int has_children;
4466   struct attribute *attr;
4467   struct cleanup *cleanups, *free_cu_cleanup = NULL;
4468   struct signatured_type *sig_type = NULL;
4469   struct dwarf2_section_info *abbrev_section;
4470   /* Non-zero if CU currently points to a DWO file and we need to
4471      reread it.  When this happens we need to reread the skeleton die
4472      before we can reread the DWO file.  */
4473   int rereading_dwo_cu = 0;
4474
4475   if (dwarf2_die_debug)
4476     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4477                         this_cu->is_debug_types ? "type" : "comp",
4478                         this_cu->offset.sect_off);
4479
4480   if (use_existing_cu)
4481     gdb_assert (keep);
4482
4483   cleanups = make_cleanup (null_cleanup, NULL);
4484
4485   /* This is cheap if the section is already read in.  */
4486   dwarf2_read_section (objfile, section);
4487
4488   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4489
4490   abbrev_section = get_abbrev_section_for_cu (this_cu);
4491
4492   if (use_existing_cu && this_cu->cu != NULL)
4493     {
4494       cu = this_cu->cu;
4495
4496       /* If this CU is from a DWO file we need to start over, we need to
4497          refetch the attributes from the skeleton CU.
4498          This could be optimized by retrieving those attributes from when we
4499          were here the first time: the previous comp_unit_die was stored in
4500          comp_unit_obstack.  But there's no data yet that we need this
4501          optimization.  */
4502       if (cu->dwo_unit != NULL)
4503         rereading_dwo_cu = 1;
4504     }
4505   else
4506     {
4507       /* If !use_existing_cu, this_cu->cu must be NULL.  */
4508       gdb_assert (this_cu->cu == NULL);
4509
4510       cu = xmalloc (sizeof (*cu));
4511       init_one_comp_unit (cu, this_cu);
4512
4513       /* If an error occurs while loading, release our storage.  */
4514       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4515     }
4516
4517   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4518     {
4519       /* We already have the header, there's no need to read it in again.  */
4520       info_ptr += cu->header.first_die_offset.cu_off;
4521     }
4522   else
4523     {
4524       if (this_cu->is_debug_types)
4525         {
4526           ULONGEST signature;
4527           cu_offset type_offset_in_tu;
4528
4529           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4530                                                     abbrev_section, info_ptr,
4531                                                     &signature,
4532                                                     &type_offset_in_tu);
4533
4534           /* Since per_cu is the first member of struct signatured_type,
4535              we can go from a pointer to one to a pointer to the other.  */
4536           sig_type = (struct signatured_type *) this_cu;
4537           gdb_assert (sig_type->signature == signature);
4538           gdb_assert (sig_type->type_offset_in_tu.cu_off
4539                       == type_offset_in_tu.cu_off);
4540           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4541
4542           /* LENGTH has not been set yet for type units if we're
4543              using .gdb_index.  */
4544           this_cu->length = get_cu_length (&cu->header);
4545
4546           /* Establish the type offset that can be used to lookup the type.  */
4547           sig_type->type_offset_in_section.sect_off =
4548             this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4549         }
4550       else
4551         {
4552           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4553                                                     abbrev_section,
4554                                                     info_ptr, 0);
4555
4556           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4557           gdb_assert (this_cu->length == get_cu_length (&cu->header));
4558         }
4559     }
4560
4561   /* Skip dummy compilation units.  */
4562   if (info_ptr >= begin_info_ptr + this_cu->length
4563       || peek_abbrev_code (abfd, info_ptr) == 0)
4564     {
4565       do_cleanups (cleanups);
4566       return;
4567     }
4568
4569   /* If we don't have them yet, read the abbrevs for this compilation unit.
4570      And if we need to read them now, make sure they're freed when we're
4571      done.  Note that it's important that if the CU had an abbrev table
4572      on entry we don't free it when we're done: Somewhere up the call stack
4573      it may be in use.  */
4574   if (abbrev_table != NULL)
4575     {
4576       gdb_assert (cu->abbrev_table == NULL);
4577       gdb_assert (cu->header.abbrev_offset.sect_off
4578                   == abbrev_table->offset.sect_off);
4579       cu->abbrev_table = abbrev_table;
4580     }
4581   else if (cu->abbrev_table == NULL)
4582     {
4583       dwarf2_read_abbrevs (cu, abbrev_section);
4584       make_cleanup (dwarf2_free_abbrev_table, cu);
4585     }
4586   else if (rereading_dwo_cu)
4587     {
4588       dwarf2_free_abbrev_table (cu);
4589       dwarf2_read_abbrevs (cu, abbrev_section);
4590     }
4591
4592   /* Read the top level CU/TU die.  */
4593   init_cu_die_reader (&reader, cu, section, NULL);
4594   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4595
4596   /* If we have a DWO stub, process it and then read in the DWO file.
4597      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4598      a DWO CU, that this test will fail.  */
4599   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4600   if (attr)
4601     {
4602       const char *dwo_name = DW_STRING (attr);
4603       const char *comp_dir_string;
4604       struct dwo_unit *dwo_unit;
4605       ULONGEST signature; /* Or dwo_id.  */
4606       struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4607       int i,num_extra_attrs;
4608       struct dwarf2_section_info *dwo_abbrev_section;
4609
4610       if (has_children)
4611         error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4612                  " has children (offset 0x%x) [in module %s]"),
4613                this_cu->offset.sect_off, bfd_get_filename (abfd));
4614
4615       /* These attributes aren't processed until later:
4616          DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4617          However, the attribute is found in the stub which we won't have later.
4618          In order to not impose this complication on the rest of the code,
4619          we read them here and copy them to the DWO CU/TU die.  */
4620
4621       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4622          DWO file.  */
4623       stmt_list = NULL;
4624       if (! this_cu->is_debug_types)
4625         stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4626       low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4627       high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4628       ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4629       comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4630
4631       /* There should be a DW_AT_addr_base attribute here (if needed).
4632          We need the value before we can process DW_FORM_GNU_addr_index.  */
4633       cu->addr_base = 0;
4634       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4635       if (attr)
4636         cu->addr_base = DW_UNSND (attr);
4637
4638       /* There should be a DW_AT_ranges_base attribute here (if needed).
4639          We need the value before we can process DW_AT_ranges.  */
4640       cu->ranges_base = 0;
4641       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4642       if (attr)
4643         cu->ranges_base = DW_UNSND (attr);
4644
4645       if (this_cu->is_debug_types)
4646         {
4647           gdb_assert (sig_type != NULL);
4648           signature = sig_type->signature;
4649         }
4650       else
4651         {
4652           attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4653           if (! attr)
4654             error (_("Dwarf Error: missing dwo_id [in module %s]"),
4655                    dwo_name);
4656           signature = DW_UNSND (attr);
4657         }
4658
4659       /* We may need the comp_dir in order to find the DWO file.  */
4660       comp_dir_string = NULL;
4661       if (comp_dir)
4662         comp_dir_string = DW_STRING (comp_dir);
4663
4664       if (this_cu->is_debug_types)
4665         dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4666       else
4667         dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4668                                          signature);
4669
4670       if (dwo_unit == NULL)
4671         {
4672           error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4673                    " with ID %s [in module %s]"),
4674                  this_cu->offset.sect_off,
4675                  phex (signature, sizeof (signature)),
4676                  objfile->name);
4677         }
4678
4679       /* Set up for reading the DWO CU/TU.  */
4680       cu->dwo_unit = dwo_unit;
4681       section = dwo_unit->section;
4682       dwarf2_read_section (objfile, section);
4683       begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4684       dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4685       init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4686
4687       if (this_cu->is_debug_types)
4688         {
4689           ULONGEST signature;
4690           cu_offset type_offset_in_tu;
4691
4692           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4693                                                     dwo_abbrev_section,
4694                                                     info_ptr,
4695                                                     &signature,
4696                                                     &type_offset_in_tu);
4697           gdb_assert (sig_type->signature == signature);
4698           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4699           /* For DWOs coming from DWP files, we don't know the CU length
4700              nor the type's offset in the TU until now.  */
4701           dwo_unit->length = get_cu_length (&cu->header);
4702           dwo_unit->type_offset_in_tu = type_offset_in_tu;
4703
4704           /* Establish the type offset that can be used to lookup the type.
4705              For DWO files, we don't know it until now.  */
4706           sig_type->type_offset_in_section.sect_off =
4707             dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4708         }
4709       else
4710         {
4711           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4712                                                     dwo_abbrev_section,
4713                                                     info_ptr, 0);
4714           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4715           /* For DWOs coming from DWP files, we don't know the CU length
4716              until now.  */
4717           dwo_unit->length = get_cu_length (&cu->header);
4718         }
4719
4720       /* Discard the original CU's abbrev table, and read the DWO's.  */
4721       if (abbrev_table == NULL)
4722         {
4723           dwarf2_free_abbrev_table (cu);
4724           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4725         }
4726       else
4727         {
4728           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4729           make_cleanup (dwarf2_free_abbrev_table, cu);
4730         }
4731
4732       /* Read in the die, but leave space to copy over the attributes
4733          from the stub.  This has the benefit of simplifying the rest of
4734          the code - all the real work is done here.  */
4735       num_extra_attrs = ((stmt_list != NULL)
4736                          + (low_pc != NULL)
4737                          + (high_pc != NULL)
4738                          + (ranges != NULL)
4739                          + (comp_dir != NULL));
4740       info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4741                                   &has_children, num_extra_attrs);
4742
4743       /* Copy over the attributes from the stub to the DWO die.  */
4744       i = comp_unit_die->num_attrs;
4745       if (stmt_list != NULL)
4746         comp_unit_die->attrs[i++] = *stmt_list;
4747       if (low_pc != NULL)
4748         comp_unit_die->attrs[i++] = *low_pc;
4749       if (high_pc != NULL)
4750         comp_unit_die->attrs[i++] = *high_pc;
4751       if (ranges != NULL)
4752         comp_unit_die->attrs[i++] = *ranges;
4753       if (comp_dir != NULL)
4754         comp_unit_die->attrs[i++] = *comp_dir;
4755       comp_unit_die->num_attrs += num_extra_attrs;
4756
4757       /* Skip dummy compilation units.  */
4758       if (info_ptr >= begin_info_ptr + dwo_unit->length
4759           || peek_abbrev_code (abfd, info_ptr) == 0)
4760         {
4761           do_cleanups (cleanups);
4762           return;
4763         }
4764     }
4765
4766   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4767
4768   if (free_cu_cleanup != NULL)
4769     {
4770       if (keep)
4771         {
4772           /* We've successfully allocated this compilation unit.  Let our
4773              caller clean it up when finished with it.  */
4774           discard_cleanups (free_cu_cleanup);
4775
4776           /* We can only discard free_cu_cleanup and all subsequent cleanups.
4777              So we have to manually free the abbrev table.  */
4778           dwarf2_free_abbrev_table (cu);
4779
4780           /* Link this CU into read_in_chain.  */
4781           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4782           dwarf2_per_objfile->read_in_chain = this_cu;
4783         }
4784       else
4785         do_cleanups (free_cu_cleanup);
4786     }
4787
4788   do_cleanups (cleanups);
4789 }
4790
4791 /* Read CU/TU THIS_CU in section SECTION,
4792    but do not follow DW_AT_GNU_dwo_name if present.
4793    DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
4794    to have already done the lookup to find the DWO/DWP file).
4795
4796    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4797    THIS_CU->is_debug_types, but nothing else.
4798
4799    We fill in THIS_CU->length.
4800
4801    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4802    linker) then DIE_READER_FUNC will not get called.
4803
4804    THIS_CU->cu is always freed when done.
4805    This is done in order to not leave THIS_CU->cu in a state where we have
4806    to care whether it refers to the "main" CU or the DWO CU.  */
4807
4808 static void
4809 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4810                                    struct dwarf2_section_info *abbrev_section,
4811                                    struct dwo_file *dwo_file,
4812                                    die_reader_func_ftype *die_reader_func,
4813                                    void *data)
4814 {
4815   struct objfile *objfile = dwarf2_per_objfile->objfile;
4816   struct dwarf2_section_info *section = this_cu->section;
4817   bfd *abfd = section->asection->owner;
4818   struct dwarf2_cu cu;
4819   gdb_byte *begin_info_ptr, *info_ptr;
4820   struct die_reader_specs reader;
4821   struct cleanup *cleanups;
4822   struct die_info *comp_unit_die;
4823   int has_children;
4824
4825   if (dwarf2_die_debug)
4826     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4827                         this_cu->is_debug_types ? "type" : "comp",
4828                         this_cu->offset.sect_off);
4829
4830   gdb_assert (this_cu->cu == NULL);
4831
4832   /* This is cheap if the section is already read in.  */
4833   dwarf2_read_section (objfile, section);
4834
4835   init_one_comp_unit (&cu, this_cu);
4836
4837   cleanups = make_cleanup (free_stack_comp_unit, &cu);
4838
4839   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4840   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4841                                             abbrev_section, info_ptr,
4842                                             this_cu->is_debug_types);
4843
4844   this_cu->length = get_cu_length (&cu.header);
4845
4846   /* Skip dummy compilation units.  */
4847   if (info_ptr >= begin_info_ptr + this_cu->length
4848       || peek_abbrev_code (abfd, info_ptr) == 0)
4849     {
4850       do_cleanups (cleanups);
4851       return;
4852     }
4853
4854   dwarf2_read_abbrevs (&cu, abbrev_section);
4855   make_cleanup (dwarf2_free_abbrev_table, &cu);
4856
4857   init_cu_die_reader (&reader, &cu, section, dwo_file);
4858   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4859
4860   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4861
4862   do_cleanups (cleanups);
4863 }
4864
4865 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4866    does not lookup the specified DWO file.
4867    This cannot be used to read DWO files.
4868
4869    THIS_CU->cu is always freed when done.
4870    This is done in order to not leave THIS_CU->cu in a state where we have
4871    to care whether it refers to the "main" CU or the DWO CU.
4872    We can revisit this if the data shows there's a performance issue.  */
4873
4874 static void
4875 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4876                                 die_reader_func_ftype *die_reader_func,
4877                                 void *data)
4878 {
4879   init_cutu_and_read_dies_no_follow (this_cu,
4880                                      get_abbrev_section_for_cu (this_cu),
4881                                      NULL,
4882                                      die_reader_func, data);
4883 }
4884 \f
4885 /* Type Unit Groups.
4886
4887    Type Unit Groups are a way to collapse the set of all TUs (type units) into
4888    a more manageable set.  The grouping is done by DW_AT_stmt_list entry
4889    so that all types coming from the same compilation (.o file) are grouped
4890    together.  A future step could be to put the types in the same symtab as
4891    the CU the types ultimately came from.  */
4892
4893 static hashval_t
4894 hash_type_unit_group (const void *item)
4895 {
4896   const struct type_unit_group *tu_group = item;
4897
4898   return hash_stmt_list_entry (&tu_group->hash);
4899 }
4900
4901 static int
4902 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4903 {
4904   const struct type_unit_group *lhs = item_lhs;
4905   const struct type_unit_group *rhs = item_rhs;
4906
4907   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4908 }
4909
4910 /* Allocate a hash table for type unit groups.  */
4911
4912 static htab_t
4913 allocate_type_unit_groups_table (void)
4914 {
4915   return htab_create_alloc_ex (3,
4916                                hash_type_unit_group,
4917                                eq_type_unit_group,
4918                                NULL,
4919                                &dwarf2_per_objfile->objfile->objfile_obstack,
4920                                hashtab_obstack_allocate,
4921                                dummy_obstack_deallocate);
4922 }
4923
4924 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4925    partial symtabs.  We combine several TUs per psymtab to not let the size
4926    of any one psymtab grow too big.  */
4927 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4928 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4929
4930 /* Helper routine for get_type_unit_group.
4931    Create the type_unit_group object used to hold one or more TUs.  */
4932
4933 static struct type_unit_group *
4934 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4935 {
4936   struct objfile *objfile = dwarf2_per_objfile->objfile;
4937   struct dwarf2_per_cu_data *per_cu;
4938   struct type_unit_group *tu_group;
4939
4940   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4941                              struct type_unit_group);
4942   per_cu = &tu_group->per_cu;
4943   per_cu->objfile = objfile;
4944   per_cu->is_debug_types = 1;
4945   per_cu->type_unit_group = tu_group;
4946
4947   if (dwarf2_per_objfile->using_index)
4948     {
4949       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4950                                         struct dwarf2_per_cu_quick_data);
4951       tu_group->t.first_tu = cu->per_cu;
4952     }
4953   else
4954     {
4955       unsigned int line_offset = line_offset_struct.sect_off;
4956       struct partial_symtab *pst;
4957       char *name;
4958
4959       /* Give the symtab a useful name for debug purposes.  */
4960       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4961         name = xstrprintf ("<type_units_%d>",
4962                            (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4963       else
4964         name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4965
4966       pst = create_partial_symtab (per_cu, name);
4967       pst->anonymous = 1;
4968
4969       xfree (name);
4970     }
4971
4972   tu_group->hash.dwo_unit = cu->dwo_unit;
4973   tu_group->hash.line_offset = line_offset_struct;
4974
4975   return tu_group;
4976 }
4977
4978 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4979    STMT_LIST is a DW_AT_stmt_list attribute.  */
4980
4981 static struct type_unit_group *
4982 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4983 {
4984   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4985   struct type_unit_group *tu_group;
4986   void **slot;
4987   unsigned int line_offset;
4988   struct type_unit_group type_unit_group_for_lookup;
4989
4990   if (dwarf2_per_objfile->type_unit_groups == NULL)
4991     {
4992       dwarf2_per_objfile->type_unit_groups =
4993         allocate_type_unit_groups_table ();
4994     }
4995
4996   /* Do we need to create a new group, or can we use an existing one?  */
4997
4998   if (stmt_list)
4999     {
5000       line_offset = DW_UNSND (stmt_list);
5001       ++tu_stats->nr_symtab_sharers;
5002     }
5003   else
5004     {
5005       /* Ugh, no stmt_list.  Rare, but we have to handle it.
5006          We can do various things here like create one group per TU or
5007          spread them over multiple groups to split up the expansion work.
5008          To avoid worst case scenarios (too many groups or too large groups)
5009          we, umm, group them in bunches.  */
5010       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5011                      | (tu_stats->nr_stmt_less_type_units
5012                         / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5013       ++tu_stats->nr_stmt_less_type_units;
5014     }
5015
5016   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5017   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5018   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5019                          &type_unit_group_for_lookup, INSERT);
5020   if (*slot != NULL)
5021     {
5022       tu_group = *slot;
5023       gdb_assert (tu_group != NULL);
5024     }
5025   else
5026     {
5027       sect_offset line_offset_struct;
5028
5029       line_offset_struct.sect_off = line_offset;
5030       tu_group = create_type_unit_group (cu, line_offset_struct);
5031       *slot = tu_group;
5032       ++tu_stats->nr_symtabs;
5033     }
5034
5035   return tu_group;
5036 }
5037
5038 /* Struct used to sort TUs by their abbreviation table offset.  */
5039
5040 struct tu_abbrev_offset
5041 {
5042   struct signatured_type *sig_type;
5043   sect_offset abbrev_offset;
5044 };
5045
5046 /* Helper routine for build_type_unit_groups, passed to qsort.  */
5047
5048 static int
5049 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5050 {
5051   const struct tu_abbrev_offset * const *a = ap;
5052   const struct tu_abbrev_offset * const *b = bp;
5053   unsigned int aoff = (*a)->abbrev_offset.sect_off;
5054   unsigned int boff = (*b)->abbrev_offset.sect_off;
5055
5056   return (aoff > boff) - (aoff < boff);
5057 }
5058
5059 /* A helper function to add a type_unit_group to a table.  */
5060
5061 static int
5062 add_type_unit_group_to_table (void **slot, void *datum)
5063 {
5064   struct type_unit_group *tu_group = *slot;
5065   struct type_unit_group ***datap = datum;
5066
5067   **datap = tu_group;
5068   ++*datap;
5069
5070   return 1;
5071 }
5072
5073 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5074    each one passing FUNC,DATA.
5075
5076    The efficiency is because we sort TUs by the abbrev table they use and
5077    only read each abbrev table once.  In one program there are 200K TUs
5078    sharing 8K abbrev tables.
5079
5080    The main purpose of this function is to support building the
5081    dwarf2_per_objfile->type_unit_groups table.
5082    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5083    can collapse the search space by grouping them by stmt_list.
5084    The savings can be significant, in the same program from above the 200K TUs
5085    share 8K stmt_list tables.
5086
5087    FUNC is expected to call get_type_unit_group, which will create the
5088    struct type_unit_group if necessary and add it to
5089    dwarf2_per_objfile->type_unit_groups.  */
5090
5091 static void
5092 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5093 {
5094   struct objfile *objfile = dwarf2_per_objfile->objfile;
5095   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5096   struct cleanup *cleanups;
5097   struct abbrev_table *abbrev_table;
5098   sect_offset abbrev_offset;
5099   struct tu_abbrev_offset *sorted_by_abbrev;
5100   struct type_unit_group **iter;
5101   int i;
5102
5103   /* It's up to the caller to not call us multiple times.  */
5104   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5105
5106   if (dwarf2_per_objfile->n_type_units == 0)
5107     return;
5108
5109   /* TUs typically share abbrev tables, and there can be way more TUs than
5110      abbrev tables.  Sort by abbrev table to reduce the number of times we
5111      read each abbrev table in.
5112      Alternatives are to punt or to maintain a cache of abbrev tables.
5113      This is simpler and efficient enough for now.
5114
5115      Later we group TUs by their DW_AT_stmt_list value (as this defines the
5116      symtab to use).  Typically TUs with the same abbrev offset have the same
5117      stmt_list value too so in practice this should work well.
5118
5119      The basic algorithm here is:
5120
5121       sort TUs by abbrev table
5122       for each TU with same abbrev table:
5123         read abbrev table if first user
5124         read TU top level DIE
5125           [IWBN if DWO skeletons had DW_AT_stmt_list]
5126         call FUNC  */
5127
5128   if (dwarf2_read_debug)
5129     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5130
5131   /* Sort in a separate table to maintain the order of all_type_units
5132      for .gdb_index: TU indices directly index all_type_units.  */
5133   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5134                               dwarf2_per_objfile->n_type_units);
5135   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5136     {
5137       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5138
5139       sorted_by_abbrev[i].sig_type = sig_type;
5140       sorted_by_abbrev[i].abbrev_offset =
5141         read_abbrev_offset (sig_type->per_cu.section,
5142                             sig_type->per_cu.offset);
5143     }
5144   cleanups = make_cleanup (xfree, sorted_by_abbrev);
5145   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5146          sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5147
5148   /* Note: In the .gdb_index case, get_type_unit_group may have already been
5149      called any number of times, so we don't reset tu_stats here.  */
5150
5151   abbrev_offset.sect_off = ~(unsigned) 0;
5152   abbrev_table = NULL;
5153   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5154
5155   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5156     {
5157       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5158
5159       /* Switch to the next abbrev table if necessary.  */
5160       if (abbrev_table == NULL
5161           || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5162         {
5163           if (abbrev_table != NULL)
5164             {
5165               abbrev_table_free (abbrev_table);
5166               /* Reset to NULL in case abbrev_table_read_table throws
5167                  an error: abbrev_table_free_cleanup will get called.  */
5168               abbrev_table = NULL;
5169             }
5170           abbrev_offset = tu->abbrev_offset;
5171           abbrev_table =
5172             abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5173                                      abbrev_offset);
5174           ++tu_stats->nr_uniq_abbrev_tables;
5175         }
5176
5177       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5178                                func, data);
5179     }
5180
5181   /* Create a vector of pointers to primary type units to make it easy to
5182      iterate over them and CUs.  See dw2_get_primary_cu.  */
5183   dwarf2_per_objfile->n_type_unit_groups =
5184     htab_elements (dwarf2_per_objfile->type_unit_groups);
5185   dwarf2_per_objfile->all_type_unit_groups =
5186     obstack_alloc (&objfile->objfile_obstack,
5187                    dwarf2_per_objfile->n_type_unit_groups
5188                    * sizeof (struct type_unit_group *));
5189   iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5190   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5191                           add_type_unit_group_to_table, &iter);
5192   gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5193               == dwarf2_per_objfile->n_type_unit_groups);
5194
5195   do_cleanups (cleanups);
5196
5197   if (dwarf2_read_debug)
5198     {
5199       fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5200       fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
5201                           dwarf2_per_objfile->n_type_units);
5202       fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
5203                           tu_stats->nr_uniq_abbrev_tables);
5204       fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
5205                           tu_stats->nr_symtabs);
5206       fprintf_unfiltered (gdb_stdlog, "  %d symtab sharers\n",
5207                           tu_stats->nr_symtab_sharers);
5208       fprintf_unfiltered (gdb_stdlog, "  %d type units without a stmt_list\n",
5209                           tu_stats->nr_stmt_less_type_units);
5210     }
5211 }
5212 \f
5213 /* Partial symbol tables.  */
5214
5215 /* Create a psymtab named NAME and assign it to PER_CU.
5216
5217    The caller must fill in the following details:
5218    dirname, textlow, texthigh.  */
5219
5220 static struct partial_symtab *
5221 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5222 {
5223   struct objfile *objfile = per_cu->objfile;
5224   struct partial_symtab *pst;
5225
5226   pst = start_psymtab_common (objfile, objfile->section_offsets,
5227                               name, 0,
5228                               objfile->global_psymbols.next,
5229                               objfile->static_psymbols.next);
5230
5231   pst->psymtabs_addrmap_supported = 1;
5232
5233   /* This is the glue that links PST into GDB's symbol API.  */
5234   pst->read_symtab_private = per_cu;
5235   pst->read_symtab = dwarf2_read_symtab;
5236   per_cu->v.psymtab = pst;
5237
5238   return pst;
5239 }
5240
5241 /* die_reader_func for process_psymtab_comp_unit.  */
5242
5243 static void
5244 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5245                                   gdb_byte *info_ptr,
5246                                   struct die_info *comp_unit_die,
5247                                   int has_children,
5248                                   void *data)
5249 {
5250   struct dwarf2_cu *cu = reader->cu;
5251   struct objfile *objfile = cu->objfile;
5252   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5253   struct attribute *attr;
5254   CORE_ADDR baseaddr;
5255   CORE_ADDR best_lowpc = 0, best_highpc = 0;
5256   struct partial_symtab *pst;
5257   int has_pc_info;
5258   const char *filename;
5259   int *want_partial_unit_ptr = data;
5260
5261   if (comp_unit_die->tag == DW_TAG_partial_unit
5262       && (want_partial_unit_ptr == NULL
5263           || !*want_partial_unit_ptr))
5264     return;
5265
5266   gdb_assert (! per_cu->is_debug_types);
5267
5268   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5269
5270   cu->list_in_scope = &file_symbols;
5271
5272   /* Allocate a new partial symbol table structure.  */
5273   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5274   if (attr == NULL || !DW_STRING (attr))
5275     filename = "";
5276   else
5277     filename = DW_STRING (attr);
5278
5279   pst = create_partial_symtab (per_cu, filename);
5280
5281   /* This must be done before calling dwarf2_build_include_psymtabs.  */
5282   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5283   if (attr != NULL)
5284     pst->dirname = DW_STRING (attr);
5285
5286   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5287
5288   dwarf2_find_base_address (comp_unit_die, cu);
5289
5290   /* Possibly set the default values of LOWPC and HIGHPC from
5291      `DW_AT_ranges'.  */
5292   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5293                                       &best_highpc, cu, pst);
5294   if (has_pc_info == 1 && best_lowpc < best_highpc)
5295     /* Store the contiguous range if it is not empty; it can be empty for
5296        CUs with no code.  */
5297     addrmap_set_empty (objfile->psymtabs_addrmap,
5298                        best_lowpc + baseaddr,
5299                        best_highpc + baseaddr - 1, pst);
5300
5301   /* Check if comp unit has_children.
5302      If so, read the rest of the partial symbols from this comp unit.
5303      If not, there's no more debug_info for this comp unit.  */
5304   if (has_children)
5305     {
5306       struct partial_die_info *first_die;
5307       CORE_ADDR lowpc, highpc;
5308
5309       lowpc = ((CORE_ADDR) -1);
5310       highpc = ((CORE_ADDR) 0);
5311
5312       first_die = load_partial_dies (reader, info_ptr, 1);
5313
5314       scan_partial_symbols (first_die, &lowpc, &highpc,
5315                             ! has_pc_info, cu);
5316
5317       /* If we didn't find a lowpc, set it to highpc to avoid
5318          complaints from `maint check'.  */
5319       if (lowpc == ((CORE_ADDR) -1))
5320         lowpc = highpc;
5321
5322       /* If the compilation unit didn't have an explicit address range,
5323          then use the information extracted from its child dies.  */
5324       if (! has_pc_info)
5325         {
5326           best_lowpc = lowpc;
5327           best_highpc = highpc;
5328         }
5329     }
5330   pst->textlow = best_lowpc + baseaddr;
5331   pst->texthigh = best_highpc + baseaddr;
5332
5333   pst->n_global_syms = objfile->global_psymbols.next -
5334     (objfile->global_psymbols.list + pst->globals_offset);
5335   pst->n_static_syms = objfile->static_psymbols.next -
5336     (objfile->static_psymbols.list + pst->statics_offset);
5337   sort_pst_symbols (objfile, pst);
5338
5339   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5340     {
5341       int i;
5342       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5343       struct dwarf2_per_cu_data *iter;
5344
5345       /* Fill in 'dependencies' here; we fill in 'users' in a
5346          post-pass.  */
5347       pst->number_of_dependencies = len;
5348       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5349                                          len * sizeof (struct symtab *));
5350       for (i = 0;
5351            VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5352                         i, iter);
5353            ++i)
5354         pst->dependencies[i] = iter->v.psymtab;
5355
5356       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5357     }
5358
5359   /* Get the list of files included in the current compilation unit,
5360      and build a psymtab for each of them.  */
5361   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5362
5363   if (dwarf2_read_debug)
5364     {
5365       struct gdbarch *gdbarch = get_objfile_arch (objfile);
5366
5367       fprintf_unfiltered (gdb_stdlog,
5368                           "Psymtab for %s unit @0x%x: %s - %s"
5369                           ", %d global, %d static syms\n",
5370                           per_cu->is_debug_types ? "type" : "comp",
5371                           per_cu->offset.sect_off,
5372                           paddress (gdbarch, pst->textlow),
5373                           paddress (gdbarch, pst->texthigh),
5374                           pst->n_global_syms, pst->n_static_syms);
5375     }
5376 }
5377
5378 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5379    Process compilation unit THIS_CU for a psymtab.  */
5380
5381 static void
5382 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5383                            int want_partial_unit)
5384 {
5385   /* If this compilation unit was already read in, free the
5386      cached copy in order to read it in again.  This is
5387      necessary because we skipped some symbols when we first
5388      read in the compilation unit (see load_partial_dies).
5389      This problem could be avoided, but the benefit is unclear.  */
5390   if (this_cu->cu != NULL)
5391     free_one_cached_comp_unit (this_cu);
5392
5393   gdb_assert (! this_cu->is_debug_types);
5394   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5395                            process_psymtab_comp_unit_reader,
5396                            &want_partial_unit);
5397
5398   /* Age out any secondary CUs.  */
5399   age_cached_comp_units ();
5400 }
5401
5402 /* Reader function for build_type_psymtabs.  */
5403
5404 static void
5405 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5406                             gdb_byte *info_ptr,
5407                             struct die_info *type_unit_die,
5408                             int has_children,
5409                             void *data)
5410 {
5411   struct objfile *objfile = dwarf2_per_objfile->objfile;
5412   struct dwarf2_cu *cu = reader->cu;
5413   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5414   struct type_unit_group *tu_group;
5415   struct attribute *attr;
5416   struct partial_die_info *first_die;
5417   CORE_ADDR lowpc, highpc;
5418   struct partial_symtab *pst;
5419
5420   gdb_assert (data == NULL);
5421
5422   if (! has_children)
5423     return;
5424
5425   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5426   tu_group = get_type_unit_group (cu, attr);
5427
5428   VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5429
5430   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5431   cu->list_in_scope = &file_symbols;
5432   pst = create_partial_symtab (per_cu, "");
5433   pst->anonymous = 1;
5434
5435   first_die = load_partial_dies (reader, info_ptr, 1);
5436
5437   lowpc = (CORE_ADDR) -1;
5438   highpc = (CORE_ADDR) 0;
5439   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5440
5441   pst->n_global_syms = objfile->global_psymbols.next -
5442     (objfile->global_psymbols.list + pst->globals_offset);
5443   pst->n_static_syms = objfile->static_psymbols.next -
5444     (objfile->static_psymbols.list + pst->statics_offset);
5445   sort_pst_symbols (objfile, pst);
5446 }
5447
5448 /* Traversal function for build_type_psymtabs.  */
5449
5450 static int
5451 build_type_psymtab_dependencies (void **slot, void *info)
5452 {
5453   struct objfile *objfile = dwarf2_per_objfile->objfile;
5454   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5455   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5456   struct partial_symtab *pst = per_cu->v.psymtab;
5457   int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5458   struct dwarf2_per_cu_data *iter;
5459   int i;
5460
5461   gdb_assert (len > 0);
5462
5463   pst->number_of_dependencies = len;
5464   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5465                                      len * sizeof (struct psymtab *));
5466   for (i = 0;
5467        VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5468        ++i)
5469     {
5470       pst->dependencies[i] = iter->v.psymtab;
5471       iter->type_unit_group = tu_group;
5472     }
5473
5474   VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5475
5476   return 1;
5477 }
5478
5479 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5480    Build partial symbol tables for the .debug_types comp-units.  */
5481
5482 static void
5483 build_type_psymtabs (struct objfile *objfile)
5484 {
5485   if (! create_all_type_units (objfile))
5486     return;
5487
5488   build_type_unit_groups (build_type_psymtabs_reader, NULL);
5489
5490   /* Now that all TUs have been processed we can fill in the dependencies.  */
5491   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5492                           build_type_psymtab_dependencies, NULL);
5493 }
5494
5495 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
5496
5497 static void
5498 psymtabs_addrmap_cleanup (void *o)
5499 {
5500   struct objfile *objfile = o;
5501
5502   objfile->psymtabs_addrmap = NULL;
5503 }
5504
5505 /* Compute the 'user' field for each psymtab in OBJFILE.  */
5506
5507 static void
5508 set_partial_user (struct objfile *objfile)
5509 {
5510   int i;
5511
5512   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5513     {
5514       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5515       struct partial_symtab *pst = per_cu->v.psymtab;
5516       int j;
5517
5518       if (pst == NULL)
5519         continue;
5520
5521       for (j = 0; j < pst->number_of_dependencies; ++j)
5522         {
5523           /* Set the 'user' field only if it is not already set.  */
5524           if (pst->dependencies[j]->user == NULL)
5525             pst->dependencies[j]->user = pst;
5526         }
5527     }
5528 }
5529
5530 /* Build the partial symbol table by doing a quick pass through the
5531    .debug_info and .debug_abbrev sections.  */
5532
5533 static void
5534 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5535 {
5536   struct cleanup *back_to, *addrmap_cleanup;
5537   struct obstack temp_obstack;
5538   int i;
5539
5540   if (dwarf2_read_debug)
5541     {
5542       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5543                           objfile->name);
5544     }
5545
5546   dwarf2_per_objfile->reading_partial_symbols = 1;
5547
5548   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5549
5550   /* Any cached compilation units will be linked by the per-objfile
5551      read_in_chain.  Make sure to free them when we're done.  */
5552   back_to = make_cleanup (free_cached_comp_units, NULL);
5553
5554   build_type_psymtabs (objfile);
5555
5556   create_all_comp_units (objfile);
5557
5558   /* Create a temporary address map on a temporary obstack.  We later
5559      copy this to the final obstack.  */
5560   obstack_init (&temp_obstack);
5561   make_cleanup_obstack_free (&temp_obstack);
5562   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5563   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5564
5565   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5566     {
5567       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5568
5569       process_psymtab_comp_unit (per_cu, 0);
5570     }
5571
5572   set_partial_user (objfile);
5573
5574   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5575                                                     &objfile->objfile_obstack);
5576   discard_cleanups (addrmap_cleanup);
5577
5578   do_cleanups (back_to);
5579
5580   if (dwarf2_read_debug)
5581     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5582                         objfile->name);
5583 }
5584
5585 /* die_reader_func for load_partial_comp_unit.  */
5586
5587 static void
5588 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5589                                gdb_byte *info_ptr,
5590                                struct die_info *comp_unit_die,
5591                                int has_children,
5592                                void *data)
5593 {
5594   struct dwarf2_cu *cu = reader->cu;
5595
5596   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5597
5598   /* Check if comp unit has_children.
5599      If so, read the rest of the partial symbols from this comp unit.
5600      If not, there's no more debug_info for this comp unit.  */
5601   if (has_children)
5602     load_partial_dies (reader, info_ptr, 0);
5603 }
5604
5605 /* Load the partial DIEs for a secondary CU into memory.
5606    This is also used when rereading a primary CU with load_all_dies.  */
5607
5608 static void
5609 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5610 {
5611   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5612                            load_partial_comp_unit_reader, NULL);
5613 }
5614
5615 static void
5616 read_comp_units_from_section (struct objfile *objfile,
5617                               struct dwarf2_section_info *section,
5618                               unsigned int is_dwz,
5619                               int *n_allocated,
5620                               int *n_comp_units,
5621                               struct dwarf2_per_cu_data ***all_comp_units)
5622 {
5623   gdb_byte *info_ptr;
5624   bfd *abfd = section->asection->owner;
5625
5626   dwarf2_read_section (objfile, section);
5627
5628   info_ptr = section->buffer;
5629
5630   while (info_ptr < section->buffer + section->size)
5631     {
5632       unsigned int length, initial_length_size;
5633       struct dwarf2_per_cu_data *this_cu;
5634       sect_offset offset;
5635
5636       offset.sect_off = info_ptr - section->buffer;
5637
5638       /* Read just enough information to find out where the next
5639          compilation unit is.  */
5640       length = read_initial_length (abfd, info_ptr, &initial_length_size);
5641
5642       /* Save the compilation unit for later lookup.  */
5643       this_cu = obstack_alloc (&objfile->objfile_obstack,
5644                                sizeof (struct dwarf2_per_cu_data));
5645       memset (this_cu, 0, sizeof (*this_cu));
5646       this_cu->offset = offset;
5647       this_cu->length = length + initial_length_size;
5648       this_cu->is_dwz = is_dwz;
5649       this_cu->objfile = objfile;
5650       this_cu->section = section;
5651
5652       if (*n_comp_units == *n_allocated)
5653         {
5654           *n_allocated *= 2;
5655           *all_comp_units = xrealloc (*all_comp_units,
5656                                       *n_allocated
5657                                       * sizeof (struct dwarf2_per_cu_data *));
5658         }
5659       (*all_comp_units)[*n_comp_units] = this_cu;
5660       ++*n_comp_units;
5661
5662       info_ptr = info_ptr + this_cu->length;
5663     }
5664 }
5665
5666 /* Create a list of all compilation units in OBJFILE.
5667    This is only done for -readnow and building partial symtabs.  */
5668
5669 static void
5670 create_all_comp_units (struct objfile *objfile)
5671 {
5672   int n_allocated;
5673   int n_comp_units;
5674   struct dwarf2_per_cu_data **all_comp_units;
5675
5676   n_comp_units = 0;
5677   n_allocated = 10;
5678   all_comp_units = xmalloc (n_allocated
5679                             * sizeof (struct dwarf2_per_cu_data *));
5680
5681   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5682                                 &n_allocated, &n_comp_units, &all_comp_units);
5683
5684   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5685     {
5686       struct dwz_file *dwz = dwarf2_get_dwz_file ();
5687
5688       read_comp_units_from_section (objfile, &dwz->info, 1,
5689                                     &n_allocated, &n_comp_units,
5690                                     &all_comp_units);
5691     }
5692
5693   dwarf2_per_objfile->all_comp_units
5694     = obstack_alloc (&objfile->objfile_obstack,
5695                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5696   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5697           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5698   xfree (all_comp_units);
5699   dwarf2_per_objfile->n_comp_units = n_comp_units;
5700 }
5701
5702 /* Process all loaded DIEs for compilation unit CU, starting at
5703    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
5704    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5705    DW_AT_ranges).  If NEED_PC is set, then this function will set
5706    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5707    and record the covered ranges in the addrmap.  */
5708
5709 static void
5710 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5711                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5712 {
5713   struct partial_die_info *pdi;
5714
5715   /* Now, march along the PDI's, descending into ones which have
5716      interesting children but skipping the children of the other ones,
5717      until we reach the end of the compilation unit.  */
5718
5719   pdi = first_die;
5720
5721   while (pdi != NULL)
5722     {
5723       fixup_partial_die (pdi, cu);
5724
5725       /* Anonymous namespaces or modules have no name but have interesting
5726          children, so we need to look at them.  Ditto for anonymous
5727          enums.  */
5728
5729       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5730           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5731           || pdi->tag == DW_TAG_imported_unit)
5732         {
5733           switch (pdi->tag)
5734             {
5735             case DW_TAG_subprogram:
5736               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5737               break;
5738             case DW_TAG_constant:
5739             case DW_TAG_variable:
5740             case DW_TAG_typedef:
5741             case DW_TAG_union_type:
5742               if (!pdi->is_declaration)
5743                 {
5744                   add_partial_symbol (pdi, cu);
5745                 }
5746               break;
5747             case DW_TAG_class_type:
5748             case DW_TAG_interface_type:
5749             case DW_TAG_structure_type:
5750               if (!pdi->is_declaration)
5751                 {
5752                   add_partial_symbol (pdi, cu);
5753                 }
5754               break;
5755             case DW_TAG_enumeration_type:
5756               if (!pdi->is_declaration)
5757                 add_partial_enumeration (pdi, cu);
5758               break;
5759             case DW_TAG_base_type:
5760             case DW_TAG_subrange_type:
5761               /* File scope base type definitions are added to the partial
5762                  symbol table.  */
5763               add_partial_symbol (pdi, cu);
5764               break;
5765             case DW_TAG_namespace:
5766               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5767               break;
5768             case DW_TAG_module:
5769               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5770               break;
5771             case DW_TAG_imported_unit:
5772               {
5773                 struct dwarf2_per_cu_data *per_cu;
5774
5775                 /* For now we don't handle imported units in type units.  */
5776                 if (cu->per_cu->is_debug_types)
5777                   {
5778                     error (_("Dwarf Error: DW_TAG_imported_unit is not"
5779                              " supported in type units [in module %s]"),
5780                            cu->objfile->name);
5781                   }
5782
5783                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5784                                                            pdi->is_dwz,
5785                                                            cu->objfile);
5786
5787                 /* Go read the partial unit, if needed.  */
5788                 if (per_cu->v.psymtab == NULL)
5789                   process_psymtab_comp_unit (per_cu, 1);
5790
5791                 VEC_safe_push (dwarf2_per_cu_ptr,
5792                                cu->per_cu->imported_symtabs, per_cu);
5793               }
5794               break;
5795             default:
5796               break;
5797             }
5798         }
5799
5800       /* If the die has a sibling, skip to the sibling.  */
5801
5802       pdi = pdi->die_sibling;
5803     }
5804 }
5805
5806 /* Functions used to compute the fully scoped name of a partial DIE.
5807
5808    Normally, this is simple.  For C++, the parent DIE's fully scoped
5809    name is concatenated with "::" and the partial DIE's name.  For
5810    Java, the same thing occurs except that "." is used instead of "::".
5811    Enumerators are an exception; they use the scope of their parent
5812    enumeration type, i.e. the name of the enumeration type is not
5813    prepended to the enumerator.
5814
5815    There are two complexities.  One is DW_AT_specification; in this
5816    case "parent" means the parent of the target of the specification,
5817    instead of the direct parent of the DIE.  The other is compilers
5818    which do not emit DW_TAG_namespace; in this case we try to guess
5819    the fully qualified name of structure types from their members'
5820    linkage names.  This must be done using the DIE's children rather
5821    than the children of any DW_AT_specification target.  We only need
5822    to do this for structures at the top level, i.e. if the target of
5823    any DW_AT_specification (if any; otherwise the DIE itself) does not
5824    have a parent.  */
5825
5826 /* Compute the scope prefix associated with PDI's parent, in
5827    compilation unit CU.  The result will be allocated on CU's
5828    comp_unit_obstack, or a copy of the already allocated PDI->NAME
5829    field.  NULL is returned if no prefix is necessary.  */
5830 static const char *
5831 partial_die_parent_scope (struct partial_die_info *pdi,
5832                           struct dwarf2_cu *cu)
5833 {
5834   const char *grandparent_scope;
5835   struct partial_die_info *parent, *real_pdi;
5836
5837   /* We need to look at our parent DIE; if we have a DW_AT_specification,
5838      then this means the parent of the specification DIE.  */
5839
5840   real_pdi = pdi;
5841   while (real_pdi->has_specification)
5842     real_pdi = find_partial_die (real_pdi->spec_offset,
5843                                  real_pdi->spec_is_dwz, cu);
5844
5845   parent = real_pdi->die_parent;
5846   if (parent == NULL)
5847     return NULL;
5848
5849   if (parent->scope_set)
5850     return parent->scope;
5851
5852   fixup_partial_die (parent, cu);
5853
5854   grandparent_scope = partial_die_parent_scope (parent, cu);
5855
5856   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5857      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5858      Work around this problem here.  */
5859   if (cu->language == language_cplus
5860       && parent->tag == DW_TAG_namespace
5861       && strcmp (parent->name, "::") == 0
5862       && grandparent_scope == NULL)
5863     {
5864       parent->scope = NULL;
5865       parent->scope_set = 1;
5866       return NULL;
5867     }
5868
5869   if (pdi->tag == DW_TAG_enumerator)
5870     /* Enumerators should not get the name of the enumeration as a prefix.  */
5871     parent->scope = grandparent_scope;
5872   else if (parent->tag == DW_TAG_namespace
5873       || parent->tag == DW_TAG_module
5874       || parent->tag == DW_TAG_structure_type
5875       || parent->tag == DW_TAG_class_type
5876       || parent->tag == DW_TAG_interface_type
5877       || parent->tag == DW_TAG_union_type
5878       || parent->tag == DW_TAG_enumeration_type)
5879     {
5880       if (grandparent_scope == NULL)
5881         parent->scope = parent->name;
5882       else
5883         parent->scope = typename_concat (&cu->comp_unit_obstack,
5884                                          grandparent_scope,
5885                                          parent->name, 0, cu);
5886     }
5887   else
5888     {
5889       /* FIXME drow/2004-04-01: What should we be doing with
5890          function-local names?  For partial symbols, we should probably be
5891          ignoring them.  */
5892       complaint (&symfile_complaints,
5893                  _("unhandled containing DIE tag %d for DIE at %d"),
5894                  parent->tag, pdi->offset.sect_off);
5895       parent->scope = grandparent_scope;
5896     }
5897
5898   parent->scope_set = 1;
5899   return parent->scope;
5900 }
5901
5902 /* Return the fully scoped name associated with PDI, from compilation unit
5903    CU.  The result will be allocated with malloc.  */
5904
5905 static char *
5906 partial_die_full_name (struct partial_die_info *pdi,
5907                        struct dwarf2_cu *cu)
5908 {
5909   const char *parent_scope;
5910
5911   /* If this is a template instantiation, we can not work out the
5912      template arguments from partial DIEs.  So, unfortunately, we have
5913      to go through the full DIEs.  At least any work we do building
5914      types here will be reused if full symbols are loaded later.  */
5915   if (pdi->has_template_arguments)
5916     {
5917       fixup_partial_die (pdi, cu);
5918
5919       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5920         {
5921           struct die_info *die;
5922           struct attribute attr;
5923           struct dwarf2_cu *ref_cu = cu;
5924
5925           /* DW_FORM_ref_addr is using section offset.  */
5926           attr.name = 0;
5927           attr.form = DW_FORM_ref_addr;
5928           attr.u.unsnd = pdi->offset.sect_off;
5929           die = follow_die_ref (NULL, &attr, &ref_cu);
5930
5931           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5932         }
5933     }
5934
5935   parent_scope = partial_die_parent_scope (pdi, cu);
5936   if (parent_scope == NULL)
5937     return NULL;
5938   else
5939     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5940 }
5941
5942 static void
5943 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5944 {
5945   struct objfile *objfile = cu->objfile;
5946   CORE_ADDR addr = 0;
5947   const char *actual_name = NULL;
5948   CORE_ADDR baseaddr;
5949   char *built_actual_name;
5950
5951   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5952
5953   built_actual_name = partial_die_full_name (pdi, cu);
5954   if (built_actual_name != NULL)
5955     actual_name = built_actual_name;
5956
5957   if (actual_name == NULL)
5958     actual_name = pdi->name;
5959
5960   switch (pdi->tag)
5961     {
5962     case DW_TAG_subprogram:
5963       if (pdi->is_external || cu->language == language_ada)
5964         {
5965           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5966              of the global scope.  But in Ada, we want to be able to access
5967              nested procedures globally.  So all Ada subprograms are stored
5968              in the global scope.  */
5969           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5970              mst_text, objfile); */
5971           add_psymbol_to_list (actual_name, strlen (actual_name),
5972                                built_actual_name != NULL,
5973                                VAR_DOMAIN, LOC_BLOCK,
5974                                &objfile->global_psymbols,
5975                                0, pdi->lowpc + baseaddr,
5976                                cu->language, objfile);
5977         }
5978       else
5979         {
5980           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5981              mst_file_text, objfile); */
5982           add_psymbol_to_list (actual_name, strlen (actual_name),
5983                                built_actual_name != NULL,
5984                                VAR_DOMAIN, LOC_BLOCK,
5985                                &objfile->static_psymbols,
5986                                0, pdi->lowpc + baseaddr,
5987                                cu->language, objfile);
5988         }
5989       break;
5990     case DW_TAG_constant:
5991       {
5992         struct psymbol_allocation_list *list;
5993
5994         if (pdi->is_external)
5995           list = &objfile->global_psymbols;
5996         else
5997           list = &objfile->static_psymbols;
5998         add_psymbol_to_list (actual_name, strlen (actual_name),
5999                              built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6000                              list, 0, 0, cu->language, objfile);
6001       }
6002       break;
6003     case DW_TAG_variable:
6004       if (pdi->d.locdesc)
6005         addr = decode_locdesc (pdi->d.locdesc, cu);
6006
6007       if (pdi->d.locdesc
6008           && addr == 0
6009           && !dwarf2_per_objfile->has_section_at_zero)
6010         {
6011           /* A global or static variable may also have been stripped
6012              out by the linker if unused, in which case its address
6013              will be nullified; do not add such variables into partial
6014              symbol table then.  */
6015         }
6016       else if (pdi->is_external)
6017         {
6018           /* Global Variable.
6019              Don't enter into the minimal symbol tables as there is
6020              a minimal symbol table entry from the ELF symbols already.
6021              Enter into partial symbol table if it has a location
6022              descriptor or a type.
6023              If the location descriptor is missing, new_symbol will create
6024              a LOC_UNRESOLVED symbol, the address of the variable will then
6025              be determined from the minimal symbol table whenever the variable
6026              is referenced.
6027              The address for the partial symbol table entry is not
6028              used by GDB, but it comes in handy for debugging partial symbol
6029              table building.  */
6030
6031           if (pdi->d.locdesc || pdi->has_type)
6032             add_psymbol_to_list (actual_name, strlen (actual_name),
6033                                  built_actual_name != NULL,
6034                                  VAR_DOMAIN, LOC_STATIC,
6035                                  &objfile->global_psymbols,
6036                                  0, addr + baseaddr,
6037                                  cu->language, objfile);
6038         }
6039       else
6040         {
6041           /* Static Variable.  Skip symbols without location descriptors.  */
6042           if (pdi->d.locdesc == NULL)
6043             {
6044               xfree (built_actual_name);
6045               return;
6046             }
6047           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6048              mst_file_data, objfile); */
6049           add_psymbol_to_list (actual_name, strlen (actual_name),
6050                                built_actual_name != NULL,
6051                                VAR_DOMAIN, LOC_STATIC,
6052                                &objfile->static_psymbols,
6053                                0, addr + baseaddr,
6054                                cu->language, objfile);
6055         }
6056       break;
6057     case DW_TAG_typedef:
6058     case DW_TAG_base_type:
6059     case DW_TAG_subrange_type:
6060       add_psymbol_to_list (actual_name, strlen (actual_name),
6061                            built_actual_name != NULL,
6062                            VAR_DOMAIN, LOC_TYPEDEF,
6063                            &objfile->static_psymbols,
6064                            0, (CORE_ADDR) 0, cu->language, objfile);
6065       break;
6066     case DW_TAG_namespace:
6067       add_psymbol_to_list (actual_name, strlen (actual_name),
6068                            built_actual_name != NULL,
6069                            VAR_DOMAIN, LOC_TYPEDEF,
6070                            &objfile->global_psymbols,
6071                            0, (CORE_ADDR) 0, cu->language, objfile);
6072       break;
6073     case DW_TAG_class_type:
6074     case DW_TAG_interface_type:
6075     case DW_TAG_structure_type:
6076     case DW_TAG_union_type:
6077     case DW_TAG_enumeration_type:
6078       /* Skip external references.  The DWARF standard says in the section
6079          about "Structure, Union, and Class Type Entries": "An incomplete
6080          structure, union or class type is represented by a structure,
6081          union or class entry that does not have a byte size attribute
6082          and that has a DW_AT_declaration attribute."  */
6083       if (!pdi->has_byte_size && pdi->is_declaration)
6084         {
6085           xfree (built_actual_name);
6086           return;
6087         }
6088
6089       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6090          static vs. global.  */
6091       add_psymbol_to_list (actual_name, strlen (actual_name),
6092                            built_actual_name != NULL,
6093                            STRUCT_DOMAIN, LOC_TYPEDEF,
6094                            (cu->language == language_cplus
6095                             || cu->language == language_java)
6096                            ? &objfile->global_psymbols
6097                            : &objfile->static_psymbols,
6098                            0, (CORE_ADDR) 0, cu->language, objfile);
6099
6100       break;
6101     case DW_TAG_enumerator:
6102       add_psymbol_to_list (actual_name, strlen (actual_name),
6103                            built_actual_name != NULL,
6104                            VAR_DOMAIN, LOC_CONST,
6105                            (cu->language == language_cplus
6106                             || cu->language == language_java)
6107                            ? &objfile->global_psymbols
6108                            : &objfile->static_psymbols,
6109                            0, (CORE_ADDR) 0, cu->language, objfile);
6110       break;
6111     default:
6112       break;
6113     }
6114
6115   xfree (built_actual_name);
6116 }
6117
6118 /* Read a partial die corresponding to a namespace; also, add a symbol
6119    corresponding to that namespace to the symbol table.  NAMESPACE is
6120    the name of the enclosing namespace.  */
6121
6122 static void
6123 add_partial_namespace (struct partial_die_info *pdi,
6124                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
6125                        int need_pc, struct dwarf2_cu *cu)
6126 {
6127   /* Add a symbol for the namespace.  */
6128
6129   add_partial_symbol (pdi, cu);
6130
6131   /* Now scan partial symbols in that namespace.  */
6132
6133   if (pdi->has_children)
6134     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6135 }
6136
6137 /* Read a partial die corresponding to a Fortran module.  */
6138
6139 static void
6140 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6141                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6142 {
6143   /* Now scan partial symbols in that module.  */
6144
6145   if (pdi->has_children)
6146     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6147 }
6148
6149 /* Read a partial die corresponding to a subprogram and create a partial
6150    symbol for that subprogram.  When the CU language allows it, this
6151    routine also defines a partial symbol for each nested subprogram
6152    that this subprogram contains.
6153
6154    DIE my also be a lexical block, in which case we simply search
6155    recursively for suprograms defined inside that lexical block.
6156    Again, this is only performed when the CU language allows this
6157    type of definitions.  */
6158
6159 static void
6160 add_partial_subprogram (struct partial_die_info *pdi,
6161                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
6162                         int need_pc, struct dwarf2_cu *cu)
6163 {
6164   if (pdi->tag == DW_TAG_subprogram)
6165     {
6166       if (pdi->has_pc_info)
6167         {
6168           if (pdi->lowpc < *lowpc)
6169             *lowpc = pdi->lowpc;
6170           if (pdi->highpc > *highpc)
6171             *highpc = pdi->highpc;
6172           if (need_pc)
6173             {
6174               CORE_ADDR baseaddr;
6175               struct objfile *objfile = cu->objfile;
6176
6177               baseaddr = ANOFFSET (objfile->section_offsets,
6178                                    SECT_OFF_TEXT (objfile));
6179               addrmap_set_empty (objfile->psymtabs_addrmap,
6180                                  pdi->lowpc + baseaddr,
6181                                  pdi->highpc - 1 + baseaddr,
6182                                  cu->per_cu->v.psymtab);
6183             }
6184         }
6185
6186       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6187         {
6188           if (!pdi->is_declaration)
6189             /* Ignore subprogram DIEs that do not have a name, they are
6190                illegal.  Do not emit a complaint at this point, we will
6191                do so when we convert this psymtab into a symtab.  */
6192             if (pdi->name)
6193               add_partial_symbol (pdi, cu);
6194         }
6195     }
6196
6197   if (! pdi->has_children)
6198     return;
6199
6200   if (cu->language == language_ada)
6201     {
6202       pdi = pdi->die_child;
6203       while (pdi != NULL)
6204         {
6205           fixup_partial_die (pdi, cu);
6206           if (pdi->tag == DW_TAG_subprogram
6207               || pdi->tag == DW_TAG_lexical_block)
6208             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6209           pdi = pdi->die_sibling;
6210         }
6211     }
6212 }
6213
6214 /* Read a partial die corresponding to an enumeration type.  */
6215
6216 static void
6217 add_partial_enumeration (struct partial_die_info *enum_pdi,
6218                          struct dwarf2_cu *cu)
6219 {
6220   struct partial_die_info *pdi;
6221
6222   if (enum_pdi->name != NULL)
6223     add_partial_symbol (enum_pdi, cu);
6224
6225   pdi = enum_pdi->die_child;
6226   while (pdi)
6227     {
6228       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6229         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6230       else
6231         add_partial_symbol (pdi, cu);
6232       pdi = pdi->die_sibling;
6233     }
6234 }
6235
6236 /* Return the initial uleb128 in the die at INFO_PTR.  */
6237
6238 static unsigned int
6239 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6240 {
6241   unsigned int bytes_read;
6242
6243   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6244 }
6245
6246 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6247    Return the corresponding abbrev, or NULL if the number is zero (indicating
6248    an empty DIE).  In either case *BYTES_READ will be set to the length of
6249    the initial number.  */
6250
6251 static struct abbrev_info *
6252 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6253                  struct dwarf2_cu *cu)
6254 {
6255   bfd *abfd = cu->objfile->obfd;
6256   unsigned int abbrev_number;
6257   struct abbrev_info *abbrev;
6258
6259   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6260
6261   if (abbrev_number == 0)
6262     return NULL;
6263
6264   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6265   if (!abbrev)
6266     {
6267       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6268              abbrev_number, bfd_get_filename (abfd));
6269     }
6270
6271   return abbrev;
6272 }
6273
6274 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6275    Returns a pointer to the end of a series of DIEs, terminated by an empty
6276    DIE.  Any children of the skipped DIEs will also be skipped.  */
6277
6278 static gdb_byte *
6279 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6280 {
6281   struct dwarf2_cu *cu = reader->cu;
6282   struct abbrev_info *abbrev;
6283   unsigned int bytes_read;
6284
6285   while (1)
6286     {
6287       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6288       if (abbrev == NULL)
6289         return info_ptr + bytes_read;
6290       else
6291         info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6292     }
6293 }
6294
6295 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6296    INFO_PTR should point just after the initial uleb128 of a DIE, and the
6297    abbrev corresponding to that skipped uleb128 should be passed in
6298    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
6299    children.  */
6300
6301 static gdb_byte *
6302 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6303               struct abbrev_info *abbrev)
6304 {
6305   unsigned int bytes_read;
6306   struct attribute attr;
6307   bfd *abfd = reader->abfd;
6308   struct dwarf2_cu *cu = reader->cu;
6309   gdb_byte *buffer = reader->buffer;
6310   const gdb_byte *buffer_end = reader->buffer_end;
6311   gdb_byte *start_info_ptr = info_ptr;
6312   unsigned int form, i;
6313
6314   for (i = 0; i < abbrev->num_attrs; i++)
6315     {
6316       /* The only abbrev we care about is DW_AT_sibling.  */
6317       if (abbrev->attrs[i].name == DW_AT_sibling)
6318         {
6319           read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6320           if (attr.form == DW_FORM_ref_addr)
6321             complaint (&symfile_complaints,
6322                        _("ignoring absolute DW_AT_sibling"));
6323           else
6324             return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6325         }
6326
6327       /* If it isn't DW_AT_sibling, skip this attribute.  */
6328       form = abbrev->attrs[i].form;
6329     skip_attribute:
6330       switch (form)
6331         {
6332         case DW_FORM_ref_addr:
6333           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6334              and later it is offset sized.  */
6335           if (cu->header.version == 2)
6336             info_ptr += cu->header.addr_size;
6337           else
6338             info_ptr += cu->header.offset_size;
6339           break;
6340         case DW_FORM_GNU_ref_alt:
6341           info_ptr += cu->header.offset_size;
6342           break;
6343         case DW_FORM_addr:
6344           info_ptr += cu->header.addr_size;
6345           break;
6346         case DW_FORM_data1:
6347         case DW_FORM_ref1:
6348         case DW_FORM_flag:
6349           info_ptr += 1;
6350           break;
6351         case DW_FORM_flag_present:
6352           break;
6353         case DW_FORM_data2:
6354         case DW_FORM_ref2:
6355           info_ptr += 2;
6356           break;
6357         case DW_FORM_data4:
6358         case DW_FORM_ref4:
6359           info_ptr += 4;
6360           break;
6361         case DW_FORM_data8:
6362         case DW_FORM_ref8:
6363         case DW_FORM_ref_sig8:
6364           info_ptr += 8;
6365           break;
6366         case DW_FORM_string:
6367           read_direct_string (abfd, info_ptr, &bytes_read);
6368           info_ptr += bytes_read;
6369           break;
6370         case DW_FORM_sec_offset:
6371         case DW_FORM_strp:
6372         case DW_FORM_GNU_strp_alt:
6373           info_ptr += cu->header.offset_size;
6374           break;
6375         case DW_FORM_exprloc:
6376         case DW_FORM_block:
6377           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6378           info_ptr += bytes_read;
6379           break;
6380         case DW_FORM_block1:
6381           info_ptr += 1 + read_1_byte (abfd, info_ptr);
6382           break;
6383         case DW_FORM_block2:
6384           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6385           break;
6386         case DW_FORM_block4:
6387           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6388           break;
6389         case DW_FORM_sdata:
6390         case DW_FORM_udata:
6391         case DW_FORM_ref_udata:
6392         case DW_FORM_GNU_addr_index:
6393         case DW_FORM_GNU_str_index:
6394           info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6395           break;
6396         case DW_FORM_indirect:
6397           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6398           info_ptr += bytes_read;
6399           /* We need to continue parsing from here, so just go back to
6400              the top.  */
6401           goto skip_attribute;
6402
6403         default:
6404           error (_("Dwarf Error: Cannot handle %s "
6405                    "in DWARF reader [in module %s]"),
6406                  dwarf_form_name (form),
6407                  bfd_get_filename (abfd));
6408         }
6409     }
6410
6411   if (abbrev->has_children)
6412     return skip_children (reader, info_ptr);
6413   else
6414     return info_ptr;
6415 }
6416
6417 /* Locate ORIG_PDI's sibling.
6418    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */
6419
6420 static gdb_byte *
6421 locate_pdi_sibling (const struct die_reader_specs *reader,
6422                     struct partial_die_info *orig_pdi,
6423                     gdb_byte *info_ptr)
6424 {
6425   /* Do we know the sibling already?  */
6426
6427   if (orig_pdi->sibling)
6428     return orig_pdi->sibling;
6429
6430   /* Are there any children to deal with?  */
6431
6432   if (!orig_pdi->has_children)
6433     return info_ptr;
6434
6435   /* Skip the children the long way.  */
6436
6437   return skip_children (reader, info_ptr);
6438 }
6439
6440 /* Expand this partial symbol table into a full symbol table.  SELF is
6441    not NULL.  */
6442
6443 static void
6444 dwarf2_read_symtab (struct partial_symtab *self,
6445                     struct objfile *objfile)
6446 {
6447   if (self->readin)
6448     {
6449       warning (_("bug: psymtab for %s is already read in."),
6450                self->filename);
6451     }
6452   else
6453     {
6454       if (info_verbose)
6455         {
6456           printf_filtered (_("Reading in symbols for %s..."),
6457                            self->filename);
6458           gdb_flush (gdb_stdout);
6459         }
6460
6461       /* Restore our global data.  */
6462       dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6463
6464       /* If this psymtab is constructed from a debug-only objfile, the
6465          has_section_at_zero flag will not necessarily be correct.  We
6466          can get the correct value for this flag by looking at the data
6467          associated with the (presumably stripped) associated objfile.  */
6468       if (objfile->separate_debug_objfile_backlink)
6469         {
6470           struct dwarf2_per_objfile *dpo_backlink
6471             = objfile_data (objfile->separate_debug_objfile_backlink,
6472                             dwarf2_objfile_data_key);
6473
6474           dwarf2_per_objfile->has_section_at_zero
6475             = dpo_backlink->has_section_at_zero;
6476         }
6477
6478       dwarf2_per_objfile->reading_partial_symbols = 0;
6479
6480       psymtab_to_symtab_1 (self);
6481
6482       /* Finish up the debug error message.  */
6483       if (info_verbose)
6484         printf_filtered (_("done.\n"));
6485     }
6486
6487   process_cu_includes ();
6488 }
6489 \f
6490 /* Reading in full CUs.  */
6491
6492 /* Add PER_CU to the queue.  */
6493
6494 static void
6495 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6496                  enum language pretend_language)
6497 {
6498   struct dwarf2_queue_item *item;
6499
6500   per_cu->queued = 1;
6501   item = xmalloc (sizeof (*item));
6502   item->per_cu = per_cu;
6503   item->pretend_language = pretend_language;
6504   item->next = NULL;
6505
6506   if (dwarf2_queue == NULL)
6507     dwarf2_queue = item;
6508   else
6509     dwarf2_queue_tail->next = item;
6510
6511   dwarf2_queue_tail = item;
6512 }
6513
6514 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
6515    unit and add it to our queue.
6516    The result is non-zero if PER_CU was queued, otherwise the result is zero
6517    meaning either PER_CU is already queued or it is already loaded.  */
6518
6519 static int
6520 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6521                        struct dwarf2_per_cu_data *per_cu,
6522                        enum language pretend_language)
6523 {
6524   /* We may arrive here during partial symbol reading, if we need full
6525      DIEs to process an unusual case (e.g. template arguments).  Do
6526      not queue PER_CU, just tell our caller to load its DIEs.  */
6527   if (dwarf2_per_objfile->reading_partial_symbols)
6528     {
6529       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6530         return 1;
6531       return 0;
6532     }
6533
6534   /* Mark the dependence relation so that we don't flush PER_CU
6535      too early.  */
6536   dwarf2_add_dependence (this_cu, per_cu);
6537
6538   /* If it's already on the queue, we have nothing to do.  */
6539   if (per_cu->queued)
6540     return 0;
6541
6542   /* If the compilation unit is already loaded, just mark it as
6543      used.  */
6544   if (per_cu->cu != NULL)
6545     {
6546       per_cu->cu->last_used = 0;
6547       return 0;
6548     }
6549
6550   /* Add it to the queue.  */
6551   queue_comp_unit (per_cu, pretend_language);
6552
6553   return 1;
6554 }
6555
6556 /* Process the queue.  */
6557
6558 static void
6559 process_queue (void)
6560 {
6561   struct dwarf2_queue_item *item, *next_item;
6562
6563   if (dwarf2_read_debug)
6564     {
6565       fprintf_unfiltered (gdb_stdlog,
6566                           "Expanding one or more symtabs of objfile %s ...\n",
6567                           dwarf2_per_objfile->objfile->name);
6568     }
6569
6570   /* The queue starts out with one item, but following a DIE reference
6571      may load a new CU, adding it to the end of the queue.  */
6572   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6573     {
6574       if (dwarf2_per_objfile->using_index
6575           ? !item->per_cu->v.quick->symtab
6576           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6577         {
6578           struct dwarf2_per_cu_data *per_cu = item->per_cu;
6579
6580           if (dwarf2_read_debug)
6581             {
6582               fprintf_unfiltered (gdb_stdlog,
6583                                   "Expanding symtab of %s at offset 0x%x\n",
6584                                   per_cu->is_debug_types ? "TU" : "CU",
6585                                   per_cu->offset.sect_off);
6586             }
6587
6588           if (per_cu->is_debug_types)
6589             process_full_type_unit (per_cu, item->pretend_language);
6590           else
6591             process_full_comp_unit (per_cu, item->pretend_language);
6592
6593           if (dwarf2_read_debug)
6594             {
6595               fprintf_unfiltered (gdb_stdlog,
6596                                   "Done expanding %s at offset 0x%x\n",
6597                                   per_cu->is_debug_types ? "TU" : "CU",
6598                                   per_cu->offset.sect_off);
6599             }
6600         }
6601
6602       item->per_cu->queued = 0;
6603       next_item = item->next;
6604       xfree (item);
6605     }
6606
6607   dwarf2_queue_tail = NULL;
6608
6609   if (dwarf2_read_debug)
6610     {
6611       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6612                           dwarf2_per_objfile->objfile->name);
6613     }
6614 }
6615
6616 /* Free all allocated queue entries.  This function only releases anything if
6617    an error was thrown; if the queue was processed then it would have been
6618    freed as we went along.  */
6619
6620 static void
6621 dwarf2_release_queue (void *dummy)
6622 {
6623   struct dwarf2_queue_item *item, *last;
6624
6625   item = dwarf2_queue;
6626   while (item)
6627     {
6628       /* Anything still marked queued is likely to be in an
6629          inconsistent state, so discard it.  */
6630       if (item->per_cu->queued)
6631         {
6632           if (item->per_cu->cu != NULL)
6633             free_one_cached_comp_unit (item->per_cu);
6634           item->per_cu->queued = 0;
6635         }
6636
6637       last = item;
6638       item = item->next;
6639       xfree (last);
6640     }
6641
6642   dwarf2_queue = dwarf2_queue_tail = NULL;
6643 }
6644
6645 /* Read in full symbols for PST, and anything it depends on.  */
6646
6647 static void
6648 psymtab_to_symtab_1 (struct partial_symtab *pst)
6649 {
6650   struct dwarf2_per_cu_data *per_cu;
6651   int i;
6652
6653   if (pst->readin)
6654     return;
6655
6656   for (i = 0; i < pst->number_of_dependencies; i++)
6657     if (!pst->dependencies[i]->readin
6658         && pst->dependencies[i]->user == NULL)
6659       {
6660         /* Inform about additional files that need to be read in.  */
6661         if (info_verbose)
6662           {
6663             /* FIXME: i18n: Need to make this a single string.  */
6664             fputs_filtered (" ", gdb_stdout);
6665             wrap_here ("");
6666             fputs_filtered ("and ", gdb_stdout);
6667             wrap_here ("");
6668             printf_filtered ("%s...", pst->dependencies[i]->filename);
6669             wrap_here ("");     /* Flush output.  */
6670             gdb_flush (gdb_stdout);
6671           }
6672         psymtab_to_symtab_1 (pst->dependencies[i]);
6673       }
6674
6675   per_cu = pst->read_symtab_private;
6676
6677   if (per_cu == NULL)
6678     {
6679       /* It's an include file, no symbols to read for it.
6680          Everything is in the parent symtab.  */
6681       pst->readin = 1;
6682       return;
6683     }
6684
6685   dw2_do_instantiate_symtab (per_cu);
6686 }
6687
6688 /* Trivial hash function for die_info: the hash value of a DIE
6689    is its offset in .debug_info for this objfile.  */
6690
6691 static hashval_t
6692 die_hash (const void *item)
6693 {
6694   const struct die_info *die = item;
6695
6696   return die->offset.sect_off;
6697 }
6698
6699 /* Trivial comparison function for die_info structures: two DIEs
6700    are equal if they have the same offset.  */
6701
6702 static int
6703 die_eq (const void *item_lhs, const void *item_rhs)
6704 {
6705   const struct die_info *die_lhs = item_lhs;
6706   const struct die_info *die_rhs = item_rhs;
6707
6708   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6709 }
6710
6711 /* die_reader_func for load_full_comp_unit.
6712    This is identical to read_signatured_type_reader,
6713    but is kept separate for now.  */
6714
6715 static void
6716 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6717                             gdb_byte *info_ptr,
6718                             struct die_info *comp_unit_die,
6719                             int has_children,
6720                             void *data)
6721 {
6722   struct dwarf2_cu *cu = reader->cu;
6723   enum language *language_ptr = data;
6724
6725   gdb_assert (cu->die_hash == NULL);
6726   cu->die_hash =
6727     htab_create_alloc_ex (cu->header.length / 12,
6728                           die_hash,
6729                           die_eq,
6730                           NULL,
6731                           &cu->comp_unit_obstack,
6732                           hashtab_obstack_allocate,
6733                           dummy_obstack_deallocate);
6734
6735   if (has_children)
6736     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6737                                                   &info_ptr, comp_unit_die);
6738   cu->dies = comp_unit_die;
6739   /* comp_unit_die is not stored in die_hash, no need.  */
6740
6741   /* We try not to read any attributes in this function, because not
6742      all CUs needed for references have been loaded yet, and symbol
6743      table processing isn't initialized.  But we have to set the CU language,
6744      or we won't be able to build types correctly.
6745      Similarly, if we do not read the producer, we can not apply
6746      producer-specific interpretation.  */
6747   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6748 }
6749
6750 /* Load the DIEs associated with PER_CU into memory.  */
6751
6752 static void
6753 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6754                      enum language pretend_language)
6755 {
6756   gdb_assert (! this_cu->is_debug_types);
6757
6758   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6759                            load_full_comp_unit_reader, &pretend_language);
6760 }
6761
6762 /* Add a DIE to the delayed physname list.  */
6763
6764 static void
6765 add_to_method_list (struct type *type, int fnfield_index, int index,
6766                     const char *name, struct die_info *die,
6767                     struct dwarf2_cu *cu)
6768 {
6769   struct delayed_method_info mi;
6770   mi.type = type;
6771   mi.fnfield_index = fnfield_index;
6772   mi.index = index;
6773   mi.name = name;
6774   mi.die = die;
6775   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6776 }
6777
6778 /* A cleanup for freeing the delayed method list.  */
6779
6780 static void
6781 free_delayed_list (void *ptr)
6782 {
6783   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6784   if (cu->method_list != NULL)
6785     {
6786       VEC_free (delayed_method_info, cu->method_list);
6787       cu->method_list = NULL;
6788     }
6789 }
6790
6791 /* Compute the physnames of any methods on the CU's method list.
6792
6793    The computation of method physnames is delayed in order to avoid the
6794    (bad) condition that one of the method's formal parameters is of an as yet
6795    incomplete type.  */
6796
6797 static void
6798 compute_delayed_physnames (struct dwarf2_cu *cu)
6799 {
6800   int i;
6801   struct delayed_method_info *mi;
6802   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6803     {
6804       const char *physname;
6805       struct fn_fieldlist *fn_flp
6806         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6807       physname = dwarf2_physname (mi->name, mi->die, cu);
6808       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6809     }
6810 }
6811
6812 /* Go objects should be embedded in a DW_TAG_module DIE,
6813    and it's not clear if/how imported objects will appear.
6814    To keep Go support simple until that's worked out,
6815    go back through what we've read and create something usable.
6816    We could do this while processing each DIE, and feels kinda cleaner,
6817    but that way is more invasive.
6818    This is to, for example, allow the user to type "p var" or "b main"
6819    without having to specify the package name, and allow lookups
6820    of module.object to work in contexts that use the expression
6821    parser.  */
6822
6823 static void
6824 fixup_go_packaging (struct dwarf2_cu *cu)
6825 {
6826   char *package_name = NULL;
6827   struct pending *list;
6828   int i;
6829
6830   for (list = global_symbols; list != NULL; list = list->next)
6831     {
6832       for (i = 0; i < list->nsyms; ++i)
6833         {
6834           struct symbol *sym = list->symbol[i];
6835
6836           if (SYMBOL_LANGUAGE (sym) == language_go
6837               && SYMBOL_CLASS (sym) == LOC_BLOCK)
6838             {
6839               char *this_package_name = go_symbol_package_name (sym);
6840
6841               if (this_package_name == NULL)
6842                 continue;
6843               if (package_name == NULL)
6844                 package_name = this_package_name;
6845               else
6846                 {
6847                   if (strcmp (package_name, this_package_name) != 0)
6848                     complaint (&symfile_complaints,
6849                                _("Symtab %s has objects from two different Go packages: %s and %s"),
6850                                (SYMBOL_SYMTAB (sym)
6851                           ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
6852                                 : cu->objfile->name),
6853                                this_package_name, package_name);
6854                   xfree (this_package_name);
6855                 }
6856             }
6857         }
6858     }
6859
6860   if (package_name != NULL)
6861     {
6862       struct objfile *objfile = cu->objfile;
6863       const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
6864                                                       package_name,
6865                                                       strlen (package_name));
6866       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6867                                      saved_package_name, objfile);
6868       struct symbol *sym;
6869
6870       TYPE_TAG_NAME (type) = TYPE_NAME (type);
6871
6872       sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6873       SYMBOL_SET_LANGUAGE (sym, language_go);
6874       SYMBOL_SET_NAMES (sym, saved_package_name,
6875                         strlen (saved_package_name), 0, objfile);
6876       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6877          e.g., "main" finds the "main" module and not C's main().  */
6878       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6879       SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
6880       SYMBOL_TYPE (sym) = type;
6881
6882       add_symbol_to_list (sym, &global_symbols);
6883
6884       xfree (package_name);
6885     }
6886 }
6887
6888 /* Return the symtab for PER_CU.  This works properly regardless of
6889    whether we're using the index or psymtabs.  */
6890
6891 static struct symtab *
6892 get_symtab (struct dwarf2_per_cu_data *per_cu)
6893 {
6894   return (dwarf2_per_objfile->using_index
6895           ? per_cu->v.quick->symtab
6896           : per_cu->v.psymtab->symtab);
6897 }
6898
6899 /* A helper function for computing the list of all symbol tables
6900    included by PER_CU.  */
6901
6902 static void
6903 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6904                                 htab_t all_children,
6905                                 struct dwarf2_per_cu_data *per_cu)
6906 {
6907   void **slot;
6908   int ix;
6909   struct dwarf2_per_cu_data *iter;
6910
6911   slot = htab_find_slot (all_children, per_cu, INSERT);
6912   if (*slot != NULL)
6913     {
6914       /* This inclusion and its children have been processed.  */
6915       return;
6916     }
6917
6918   *slot = per_cu;
6919   /* Only add a CU if it has a symbol table.  */
6920   if (get_symtab (per_cu) != NULL)
6921     VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6922
6923   for (ix = 0;
6924        VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
6925        ++ix)
6926     recursively_compute_inclusions (result, all_children, iter);
6927 }
6928
6929 /* Compute the symtab 'includes' fields for the symtab related to
6930    PER_CU.  */
6931
6932 static void
6933 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6934 {
6935   gdb_assert (! per_cu->is_debug_types);
6936
6937   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
6938     {
6939       int ix, len;
6940       struct dwarf2_per_cu_data *iter;
6941       VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6942       htab_t all_children;
6943       struct symtab *symtab = get_symtab (per_cu);
6944
6945       /* If we don't have a symtab, we can just skip this case.  */
6946       if (symtab == NULL)
6947         return;
6948
6949       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6950                                         NULL, xcalloc, xfree);
6951
6952       for (ix = 0;
6953            VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
6954                         ix, iter);
6955            ++ix)
6956         recursively_compute_inclusions (&result_children, all_children, iter);
6957
6958       /* Now we have a transitive closure of all the included CUs, and
6959          for .gdb_index version 7 the included TUs, so we can convert it
6960          to a list of symtabs.  */
6961       len = VEC_length (dwarf2_per_cu_ptr, result_children);
6962       symtab->includes
6963         = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6964                          (len + 1) * sizeof (struct symtab *));
6965       for (ix = 0;
6966            VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6967            ++ix)
6968         symtab->includes[ix] = get_symtab (iter);
6969       symtab->includes[len] = NULL;
6970
6971       VEC_free (dwarf2_per_cu_ptr, result_children);
6972       htab_delete (all_children);
6973     }
6974 }
6975
6976 /* Compute the 'includes' field for the symtabs of all the CUs we just
6977    read.  */
6978
6979 static void
6980 process_cu_includes (void)
6981 {
6982   int ix;
6983   struct dwarf2_per_cu_data *iter;
6984
6985   for (ix = 0;
6986        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6987                     ix, iter);
6988        ++ix)
6989     {
6990       if (! iter->is_debug_types)
6991         compute_symtab_includes (iter);
6992     }
6993
6994   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6995 }
6996
6997 /* Generate full symbol information for PER_CU, whose DIEs have
6998    already been loaded into memory.  */
6999
7000 static void
7001 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7002                         enum language pretend_language)
7003 {
7004   struct dwarf2_cu *cu = per_cu->cu;
7005   struct objfile *objfile = per_cu->objfile;
7006   CORE_ADDR lowpc, highpc;
7007   struct symtab *symtab;
7008   struct cleanup *back_to, *delayed_list_cleanup;
7009   CORE_ADDR baseaddr;
7010   struct block *static_block;
7011
7012   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7013
7014   buildsym_init ();
7015   back_to = make_cleanup (really_free_pendings, NULL);
7016   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7017
7018   cu->list_in_scope = &file_symbols;
7019
7020   cu->language = pretend_language;
7021   cu->language_defn = language_def (cu->language);
7022
7023   /* Do line number decoding in read_file_scope () */
7024   process_die (cu->dies, cu);
7025
7026   /* For now fudge the Go package.  */
7027   if (cu->language == language_go)
7028     fixup_go_packaging (cu);
7029
7030   /* Now that we have processed all the DIEs in the CU, all the types 
7031      should be complete, and it should now be safe to compute all of the
7032      physnames.  */
7033   compute_delayed_physnames (cu);
7034   do_cleanups (delayed_list_cleanup);
7035
7036   /* Some compilers don't define a DW_AT_high_pc attribute for the
7037      compilation unit.  If the DW_AT_high_pc is missing, synthesize
7038      it, by scanning the DIE's below the compilation unit.  */
7039   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7040
7041   static_block
7042     = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
7043                                    per_cu->imported_symtabs != NULL);
7044
7045   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7046      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7047      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
7048      addrmap to help ensure it has an accurate map of pc values belonging to
7049      this comp unit.  */
7050   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7051
7052   symtab = end_symtab_from_static_block (static_block, objfile,
7053                                          SECT_OFF_TEXT (objfile), 0);
7054
7055   if (symtab != NULL)
7056     {
7057       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7058
7059       /* Set symtab language to language from DW_AT_language.  If the
7060          compilation is from a C file generated by language preprocessors, do
7061          not set the language if it was already deduced by start_subfile.  */
7062       if (!(cu->language == language_c && symtab->language != language_c))
7063         symtab->language = cu->language;
7064
7065       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
7066          produce DW_AT_location with location lists but it can be possibly
7067          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
7068          there were bugs in prologue debug info, fixed later in GCC-4.5
7069          by "unwind info for epilogues" patch (which is not directly related).
7070
7071          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7072          needed, it would be wrong due to missing DW_AT_producer there.
7073
7074          Still one can confuse GDB by using non-standard GCC compilation
7075          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7076          */ 
7077       if (cu->has_loclist && gcc_4_minor >= 5)
7078         symtab->locations_valid = 1;
7079
7080       if (gcc_4_minor >= 5)
7081         symtab->epilogue_unwind_valid = 1;
7082
7083       symtab->call_site_htab = cu->call_site_htab;
7084     }
7085
7086   if (dwarf2_per_objfile->using_index)
7087     per_cu->v.quick->symtab = symtab;
7088   else
7089     {
7090       struct partial_symtab *pst = per_cu->v.psymtab;
7091       pst->symtab = symtab;
7092       pst->readin = 1;
7093     }
7094
7095   /* Push it for inclusion processing later.  */
7096   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7097
7098   do_cleanups (back_to);
7099 }
7100
7101 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7102    already been loaded into memory.  */
7103
7104 static void
7105 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7106                         enum language pretend_language)
7107 {
7108   struct dwarf2_cu *cu = per_cu->cu;
7109   struct objfile *objfile = per_cu->objfile;
7110   struct symtab *symtab;
7111   struct cleanup *back_to, *delayed_list_cleanup;
7112
7113   buildsym_init ();
7114   back_to = make_cleanup (really_free_pendings, NULL);
7115   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7116
7117   cu->list_in_scope = &file_symbols;
7118
7119   cu->language = pretend_language;
7120   cu->language_defn = language_def (cu->language);
7121
7122   /* The symbol tables are set up in read_type_unit_scope.  */
7123   process_die (cu->dies, cu);
7124
7125   /* For now fudge the Go package.  */
7126   if (cu->language == language_go)
7127     fixup_go_packaging (cu);
7128
7129   /* Now that we have processed all the DIEs in the CU, all the types 
7130      should be complete, and it should now be safe to compute all of the
7131      physnames.  */
7132   compute_delayed_physnames (cu);
7133   do_cleanups (delayed_list_cleanup);
7134
7135   /* TUs share symbol tables.
7136      If this is the first TU to use this symtab, complete the construction
7137      of it with end_expandable_symtab.  Otherwise, complete the addition of
7138      this TU's symbols to the existing symtab.  */
7139   if (per_cu->type_unit_group->primary_symtab == NULL)
7140     {
7141       symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7142       per_cu->type_unit_group->primary_symtab = symtab;
7143
7144       if (symtab != NULL)
7145         {
7146           /* Set symtab language to language from DW_AT_language.  If the
7147              compilation is from a C file generated by language preprocessors,
7148              do not set the language if it was already deduced by
7149              start_subfile.  */
7150           if (!(cu->language == language_c && symtab->language != language_c))
7151             symtab->language = cu->language;
7152         }
7153     }
7154   else
7155     {
7156       augment_type_symtab (objfile,
7157                            per_cu->type_unit_group->primary_symtab);
7158       symtab = per_cu->type_unit_group->primary_symtab;
7159     }
7160
7161   if (dwarf2_per_objfile->using_index)
7162     per_cu->v.quick->symtab = symtab;
7163   else
7164     {
7165       struct partial_symtab *pst = per_cu->v.psymtab;
7166       pst->symtab = symtab;
7167       pst->readin = 1;
7168     }
7169
7170   do_cleanups (back_to);
7171 }
7172
7173 /* Process an imported unit DIE.  */
7174
7175 static void
7176 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7177 {
7178   struct attribute *attr;
7179
7180   /* For now we don't handle imported units in type units.  */
7181   if (cu->per_cu->is_debug_types)
7182     {
7183       error (_("Dwarf Error: DW_TAG_imported_unit is not"
7184                " supported in type units [in module %s]"),
7185              cu->objfile->name);
7186     }
7187
7188   attr = dwarf2_attr (die, DW_AT_import, cu);
7189   if (attr != NULL)
7190     {
7191       struct dwarf2_per_cu_data *per_cu;
7192       struct symtab *imported_symtab;
7193       sect_offset offset;
7194       int is_dwz;
7195
7196       offset = dwarf2_get_ref_die_offset (attr);
7197       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7198       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7199
7200       /* Queue the unit, if needed.  */
7201       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7202         load_full_comp_unit (per_cu, cu->language);
7203
7204       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7205                      per_cu);
7206     }
7207 }
7208
7209 /* Process a die and its children.  */
7210
7211 static void
7212 process_die (struct die_info *die, struct dwarf2_cu *cu)
7213 {
7214   switch (die->tag)
7215     {
7216     case DW_TAG_padding:
7217       break;
7218     case DW_TAG_compile_unit:
7219     case DW_TAG_partial_unit:
7220       read_file_scope (die, cu);
7221       break;
7222     case DW_TAG_type_unit:
7223       read_type_unit_scope (die, cu);
7224       break;
7225     case DW_TAG_subprogram:
7226     case DW_TAG_inlined_subroutine:
7227       read_func_scope (die, cu);
7228       break;
7229     case DW_TAG_lexical_block:
7230     case DW_TAG_try_block:
7231     case DW_TAG_catch_block:
7232       read_lexical_block_scope (die, cu);
7233       break;
7234     case DW_TAG_GNU_call_site:
7235       read_call_site_scope (die, cu);
7236       break;
7237     case DW_TAG_class_type:
7238     case DW_TAG_interface_type:
7239     case DW_TAG_structure_type:
7240     case DW_TAG_union_type:
7241       process_structure_scope (die, cu);
7242       break;
7243     case DW_TAG_enumeration_type:
7244       process_enumeration_scope (die, cu);
7245       break;
7246
7247     /* These dies have a type, but processing them does not create
7248        a symbol or recurse to process the children.  Therefore we can
7249        read them on-demand through read_type_die.  */
7250     case DW_TAG_subroutine_type:
7251     case DW_TAG_set_type:
7252     case DW_TAG_array_type:
7253     case DW_TAG_pointer_type:
7254     case DW_TAG_ptr_to_member_type:
7255     case DW_TAG_reference_type:
7256     case DW_TAG_string_type:
7257       break;
7258
7259     case DW_TAG_base_type:
7260     case DW_TAG_subrange_type:
7261     case DW_TAG_typedef:
7262       /* Add a typedef symbol for the type definition, if it has a
7263          DW_AT_name.  */
7264       new_symbol (die, read_type_die (die, cu), cu);
7265       break;
7266     case DW_TAG_common_block:
7267       read_common_block (die, cu);
7268       break;
7269     case DW_TAG_common_inclusion:
7270       break;
7271     case DW_TAG_namespace:
7272       cu->processing_has_namespace_info = 1;
7273       read_namespace (die, cu);
7274       break;
7275     case DW_TAG_module:
7276       cu->processing_has_namespace_info = 1;
7277       read_module (die, cu);
7278       break;
7279     case DW_TAG_imported_declaration:
7280     case DW_TAG_imported_module:
7281       cu->processing_has_namespace_info = 1;
7282       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7283                                  || cu->language != language_fortran))
7284         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7285                    dwarf_tag_name (die->tag));
7286       read_import_statement (die, cu);
7287       break;
7288
7289     case DW_TAG_imported_unit:
7290       process_imported_unit_die (die, cu);
7291       break;
7292
7293     default:
7294       new_symbol (die, NULL, cu);
7295       break;
7296     }
7297 }
7298 \f
7299 /* DWARF name computation.  */
7300
7301 /* A helper function for dwarf2_compute_name which determines whether DIE
7302    needs to have the name of the scope prepended to the name listed in the
7303    die.  */
7304
7305 static int
7306 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7307 {
7308   struct attribute *attr;
7309
7310   switch (die->tag)
7311     {
7312     case DW_TAG_namespace:
7313     case DW_TAG_typedef:
7314     case DW_TAG_class_type:
7315     case DW_TAG_interface_type:
7316     case DW_TAG_structure_type:
7317     case DW_TAG_union_type:
7318     case DW_TAG_enumeration_type:
7319     case DW_TAG_enumerator:
7320     case DW_TAG_subprogram:
7321     case DW_TAG_member:
7322       return 1;
7323
7324     case DW_TAG_variable:
7325     case DW_TAG_constant:
7326       /* We only need to prefix "globally" visible variables.  These include
7327          any variable marked with DW_AT_external or any variable that
7328          lives in a namespace.  [Variables in anonymous namespaces
7329          require prefixing, but they are not DW_AT_external.]  */
7330
7331       if (dwarf2_attr (die, DW_AT_specification, cu))
7332         {
7333           struct dwarf2_cu *spec_cu = cu;
7334
7335           return die_needs_namespace (die_specification (die, &spec_cu),
7336                                       spec_cu);
7337         }
7338
7339       attr = dwarf2_attr (die, DW_AT_external, cu);
7340       if (attr == NULL && die->parent->tag != DW_TAG_namespace
7341           && die->parent->tag != DW_TAG_module)
7342         return 0;
7343       /* A variable in a lexical block of some kind does not need a
7344          namespace, even though in C++ such variables may be external
7345          and have a mangled name.  */
7346       if (die->parent->tag ==  DW_TAG_lexical_block
7347           || die->parent->tag ==  DW_TAG_try_block
7348           || die->parent->tag ==  DW_TAG_catch_block
7349           || die->parent->tag == DW_TAG_subprogram)
7350         return 0;
7351       return 1;
7352
7353     default:
7354       return 0;
7355     }
7356 }
7357
7358 /* Retrieve the last character from a mem_file.  */
7359
7360 static void
7361 do_ui_file_peek_last (void *object, const char *buffer, long length)
7362 {
7363   char *last_char_p = (char *) object;
7364
7365   if (length > 0)
7366     *last_char_p = buffer[length - 1];
7367 }
7368
7369 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
7370    compute the physname for the object, which include a method's:
7371    - formal parameters (C++/Java),
7372    - receiver type (Go),
7373    - return type (Java).
7374
7375    The term "physname" is a bit confusing.
7376    For C++, for example, it is the demangled name.
7377    For Go, for example, it's the mangled name.
7378
7379    For Ada, return the DIE's linkage name rather than the fully qualified
7380    name.  PHYSNAME is ignored..
7381
7382    The result is allocated on the objfile_obstack and canonicalized.  */
7383
7384 static const char *
7385 dwarf2_compute_name (const char *name,
7386                      struct die_info *die, struct dwarf2_cu *cu,
7387                      int physname)
7388 {
7389   struct objfile *objfile = cu->objfile;
7390
7391   if (name == NULL)
7392     name = dwarf2_name (die, cu);
7393
7394   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7395      compute it by typename_concat inside GDB.  */
7396   if (cu->language == language_ada
7397       || (cu->language == language_fortran && physname))
7398     {
7399       /* For Ada unit, we prefer the linkage name over the name, as
7400          the former contains the exported name, which the user expects
7401          to be able to reference.  Ideally, we want the user to be able
7402          to reference this entity using either natural or linkage name,
7403          but we haven't started looking at this enhancement yet.  */
7404       struct attribute *attr;
7405
7406       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7407       if (attr == NULL)
7408         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7409       if (attr && DW_STRING (attr))
7410         return DW_STRING (attr);
7411     }
7412
7413   /* These are the only languages we know how to qualify names in.  */
7414   if (name != NULL
7415       && (cu->language == language_cplus || cu->language == language_java
7416           || cu->language == language_fortran))
7417     {
7418       if (die_needs_namespace (die, cu))
7419         {
7420           long length;
7421           const char *prefix;
7422           struct ui_file *buf;
7423
7424           prefix = determine_prefix (die, cu);
7425           buf = mem_fileopen ();
7426           if (*prefix != '\0')
7427             {
7428               char *prefixed_name = typename_concat (NULL, prefix, name,
7429                                                      physname, cu);
7430
7431               fputs_unfiltered (prefixed_name, buf);
7432               xfree (prefixed_name);
7433             }
7434           else
7435             fputs_unfiltered (name, buf);
7436
7437           /* Template parameters may be specified in the DIE's DW_AT_name, or
7438              as children with DW_TAG_template_type_param or
7439              DW_TAG_value_type_param.  If the latter, add them to the name
7440              here.  If the name already has template parameters, then
7441              skip this step; some versions of GCC emit both, and
7442              it is more efficient to use the pre-computed name.
7443
7444              Something to keep in mind about this process: it is very
7445              unlikely, or in some cases downright impossible, to produce
7446              something that will match the mangled name of a function.
7447              If the definition of the function has the same debug info,
7448              we should be able to match up with it anyway.  But fallbacks
7449              using the minimal symbol, for instance to find a method
7450              implemented in a stripped copy of libstdc++, will not work.
7451              If we do not have debug info for the definition, we will have to
7452              match them up some other way.
7453
7454              When we do name matching there is a related problem with function
7455              templates; two instantiated function templates are allowed to
7456              differ only by their return types, which we do not add here.  */
7457
7458           if (cu->language == language_cplus && strchr (name, '<') == NULL)
7459             {
7460               struct attribute *attr;
7461               struct die_info *child;
7462               int first = 1;
7463
7464               die->building_fullname = 1;
7465
7466               for (child = die->child; child != NULL; child = child->sibling)
7467                 {
7468                   struct type *type;
7469                   LONGEST value;
7470                   gdb_byte *bytes;
7471                   struct dwarf2_locexpr_baton *baton;
7472                   struct value *v;
7473
7474                   if (child->tag != DW_TAG_template_type_param
7475                       && child->tag != DW_TAG_template_value_param)
7476                     continue;
7477
7478                   if (first)
7479                     {
7480                       fputs_unfiltered ("<", buf);
7481                       first = 0;
7482                     }
7483                   else
7484                     fputs_unfiltered (", ", buf);
7485
7486                   attr = dwarf2_attr (child, DW_AT_type, cu);
7487                   if (attr == NULL)
7488                     {
7489                       complaint (&symfile_complaints,
7490                                  _("template parameter missing DW_AT_type"));
7491                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
7492                       continue;
7493                     }
7494                   type = die_type (child, cu);
7495
7496                   if (child->tag == DW_TAG_template_type_param)
7497                     {
7498                       c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7499                       continue;
7500                     }
7501
7502                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
7503                   if (attr == NULL)
7504                     {
7505                       complaint (&symfile_complaints,
7506                                  _("template parameter missing "
7507                                    "DW_AT_const_value"));
7508                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
7509                       continue;
7510                     }
7511
7512                   dwarf2_const_value_attr (attr, type, name,
7513                                            &cu->comp_unit_obstack, cu,
7514                                            &value, &bytes, &baton);
7515
7516                   if (TYPE_NOSIGN (type))
7517                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
7518                        changed, this can use value_print instead.  */
7519                     c_printchar (value, type, buf);
7520                   else
7521                     {
7522                       struct value_print_options opts;
7523
7524                       if (baton != NULL)
7525                         v = dwarf2_evaluate_loc_desc (type, NULL,
7526                                                       baton->data,
7527                                                       baton->size,
7528                                                       baton->per_cu);
7529                       else if (bytes != NULL)
7530                         {
7531                           v = allocate_value (type);
7532                           memcpy (value_contents_writeable (v), bytes,
7533                                   TYPE_LENGTH (type));
7534                         }
7535                       else
7536                         v = value_from_longest (type, value);
7537
7538                       /* Specify decimal so that we do not depend on
7539                          the radix.  */
7540                       get_formatted_print_options (&opts, 'd');
7541                       opts.raw = 1;
7542                       value_print (v, buf, &opts);
7543                       release_value (v);
7544                       value_free (v);
7545                     }
7546                 }
7547
7548               die->building_fullname = 0;
7549
7550               if (!first)
7551                 {
7552                   /* Close the argument list, with a space if necessary
7553                      (nested templates).  */
7554                   char last_char = '\0';
7555                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
7556                   if (last_char == '>')
7557                     fputs_unfiltered (" >", buf);
7558                   else
7559                     fputs_unfiltered (">", buf);
7560                 }
7561             }
7562
7563           /* For Java and C++ methods, append formal parameter type
7564              information, if PHYSNAME.  */
7565
7566           if (physname && die->tag == DW_TAG_subprogram
7567               && (cu->language == language_cplus
7568                   || cu->language == language_java))
7569             {
7570               struct type *type = read_type_die (die, cu);
7571
7572               c_type_print_args (type, buf, 1, cu->language,
7573                                  &type_print_raw_options);
7574
7575               if (cu->language == language_java)
7576                 {
7577                   /* For java, we must append the return type to method
7578                      names.  */
7579                   if (die->tag == DW_TAG_subprogram)
7580                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7581                                      0, 0, &type_print_raw_options);
7582                 }
7583               else if (cu->language == language_cplus)
7584                 {
7585                   /* Assume that an artificial first parameter is
7586                      "this", but do not crash if it is not.  RealView
7587                      marks unnamed (and thus unused) parameters as
7588                      artificial; there is no way to differentiate
7589                      the two cases.  */
7590                   if (TYPE_NFIELDS (type) > 0
7591                       && TYPE_FIELD_ARTIFICIAL (type, 0)
7592                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7593                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7594                                                                         0))))
7595                     fputs_unfiltered (" const", buf);
7596                 }
7597             }
7598
7599           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7600                                        &length);
7601           ui_file_delete (buf);
7602
7603           if (cu->language == language_cplus)
7604             {
7605               const char *cname
7606                 = dwarf2_canonicalize_name (name, cu,
7607                                             &objfile->objfile_obstack);
7608
7609               if (cname != NULL)
7610                 name = cname;
7611             }
7612         }
7613     }
7614
7615   return name;
7616 }
7617
7618 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7619    If scope qualifiers are appropriate they will be added.  The result
7620    will be allocated on the objfile_obstack, or NULL if the DIE does
7621    not have a name.  NAME may either be from a previous call to
7622    dwarf2_name or NULL.
7623
7624    The output string will be canonicalized (if C++/Java).  */
7625
7626 static const char *
7627 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7628 {
7629   return dwarf2_compute_name (name, die, cu, 0);
7630 }
7631
7632 /* Construct a physname for the given DIE in CU.  NAME may either be
7633    from a previous call to dwarf2_name or NULL.  The result will be
7634    allocated on the objfile_objstack or NULL if the DIE does not have a
7635    name.
7636
7637    The output string will be canonicalized (if C++/Java).  */
7638
7639 static const char *
7640 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
7641 {
7642   struct objfile *objfile = cu->objfile;
7643   struct attribute *attr;
7644   const char *retval, *mangled = NULL, *canon = NULL;
7645   struct cleanup *back_to;
7646   int need_copy = 1;
7647
7648   /* In this case dwarf2_compute_name is just a shortcut not building anything
7649      on its own.  */
7650   if (!die_needs_namespace (die, cu))
7651     return dwarf2_compute_name (name, die, cu, 1);
7652
7653   back_to = make_cleanup (null_cleanup, NULL);
7654
7655   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7656   if (!attr)
7657     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7658
7659   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7660      has computed.  */
7661   if (attr && DW_STRING (attr))
7662     {
7663       char *demangled;
7664
7665       mangled = DW_STRING (attr);
7666
7667       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7668          type.  It is easier for GDB users to search for such functions as
7669          `name(params)' than `long name(params)'.  In such case the minimal
7670          symbol names do not match the full symbol names but for template
7671          functions there is never a need to look up their definition from their
7672          declaration so the only disadvantage remains the minimal symbol
7673          variant `long name(params)' does not have the proper inferior type.
7674          */
7675
7676       if (cu->language == language_go)
7677         {
7678           /* This is a lie, but we already lie to the caller new_symbol_full.
7679              new_symbol_full assumes we return the mangled name.
7680              This just undoes that lie until things are cleaned up.  */
7681           demangled = NULL;
7682         }
7683       else
7684         {
7685           demangled = cplus_demangle (mangled,
7686                                       (DMGL_PARAMS | DMGL_ANSI
7687                                        | (cu->language == language_java
7688                                           ? DMGL_JAVA | DMGL_RET_POSTFIX
7689                                           : DMGL_RET_DROP)));
7690         }
7691       if (demangled)
7692         {
7693           make_cleanup (xfree, demangled);
7694           canon = demangled;
7695         }
7696       else
7697         {
7698           canon = mangled;
7699           need_copy = 0;
7700         }
7701     }
7702
7703   if (canon == NULL || check_physname)
7704     {
7705       const char *physname = dwarf2_compute_name (name, die, cu, 1);
7706
7707       if (canon != NULL && strcmp (physname, canon) != 0)
7708         {
7709           /* It may not mean a bug in GDB.  The compiler could also
7710              compute DW_AT_linkage_name incorrectly.  But in such case
7711              GDB would need to be bug-to-bug compatible.  */
7712
7713           complaint (&symfile_complaints,
7714                      _("Computed physname <%s> does not match demangled <%s> "
7715                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7716                      physname, canon, mangled, die->offset.sect_off, objfile->name);
7717
7718           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7719              is available here - over computed PHYSNAME.  It is safer
7720              against both buggy GDB and buggy compilers.  */
7721
7722           retval = canon;
7723         }
7724       else
7725         {
7726           retval = physname;
7727           need_copy = 0;
7728         }
7729     }
7730   else
7731     retval = canon;
7732
7733   if (need_copy)
7734     retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
7735
7736   do_cleanups (back_to);
7737   return retval;
7738 }
7739
7740 /* Read the import statement specified by the given die and record it.  */
7741
7742 static void
7743 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7744 {
7745   struct objfile *objfile = cu->objfile;
7746   struct attribute *import_attr;
7747   struct die_info *imported_die, *child_die;
7748   struct dwarf2_cu *imported_cu;
7749   const char *imported_name;
7750   const char *imported_name_prefix;
7751   const char *canonical_name;
7752   const char *import_alias;
7753   const char *imported_declaration = NULL;
7754   const char *import_prefix;
7755   VEC (const_char_ptr) *excludes = NULL;
7756   struct cleanup *cleanups;
7757
7758   import_attr = dwarf2_attr (die, DW_AT_import, cu);
7759   if (import_attr == NULL)
7760     {
7761       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7762                  dwarf_tag_name (die->tag));
7763       return;
7764     }
7765
7766   imported_cu = cu;
7767   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7768   imported_name = dwarf2_name (imported_die, imported_cu);
7769   if (imported_name == NULL)
7770     {
7771       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7772
7773         The import in the following code:
7774         namespace A
7775           {
7776             typedef int B;
7777           }
7778
7779         int main ()
7780           {
7781             using A::B;
7782             B b;
7783             return b;
7784           }
7785
7786         ...
7787          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7788             <52>   DW_AT_decl_file   : 1
7789             <53>   DW_AT_decl_line   : 6
7790             <54>   DW_AT_import      : <0x75>
7791          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7792             <59>   DW_AT_name        : B
7793             <5b>   DW_AT_decl_file   : 1
7794             <5c>   DW_AT_decl_line   : 2
7795             <5d>   DW_AT_type        : <0x6e>
7796         ...
7797          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7798             <76>   DW_AT_byte_size   : 4
7799             <77>   DW_AT_encoding    : 5        (signed)
7800
7801         imports the wrong die ( 0x75 instead of 0x58 ).
7802         This case will be ignored until the gcc bug is fixed.  */
7803       return;
7804     }
7805
7806   /* Figure out the local name after import.  */
7807   import_alias = dwarf2_name (die, cu);
7808
7809   /* Figure out where the statement is being imported to.  */
7810   import_prefix = determine_prefix (die, cu);
7811
7812   /* Figure out what the scope of the imported die is and prepend it
7813      to the name of the imported die.  */
7814   imported_name_prefix = determine_prefix (imported_die, imported_cu);
7815
7816   if (imported_die->tag != DW_TAG_namespace
7817       && imported_die->tag != DW_TAG_module)
7818     {
7819       imported_declaration = imported_name;
7820       canonical_name = imported_name_prefix;
7821     }
7822   else if (strlen (imported_name_prefix) > 0)
7823     canonical_name = obconcat (&objfile->objfile_obstack,
7824                                imported_name_prefix, "::", imported_name,
7825                                (char *) NULL);
7826   else
7827     canonical_name = imported_name;
7828
7829   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7830
7831   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7832     for (child_die = die->child; child_die && child_die->tag;
7833          child_die = sibling_die (child_die))
7834       {
7835         /* DWARF-4: A Fortran use statement with a “rename list” may be
7836            represented by an imported module entry with an import attribute
7837            referring to the module and owned entries corresponding to those
7838            entities that are renamed as part of being imported.  */
7839
7840         if (child_die->tag != DW_TAG_imported_declaration)
7841           {
7842             complaint (&symfile_complaints,
7843                        _("child DW_TAG_imported_declaration expected "
7844                          "- DIE at 0x%x [in module %s]"),
7845                        child_die->offset.sect_off, objfile->name);
7846             continue;
7847           }
7848
7849         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7850         if (import_attr == NULL)
7851           {
7852             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7853                        dwarf_tag_name (child_die->tag));
7854             continue;
7855           }
7856
7857         imported_cu = cu;
7858         imported_die = follow_die_ref_or_sig (child_die, import_attr,
7859                                               &imported_cu);
7860         imported_name = dwarf2_name (imported_die, imported_cu);
7861         if (imported_name == NULL)
7862           {
7863             complaint (&symfile_complaints,
7864                        _("child DW_TAG_imported_declaration has unknown "
7865                          "imported name - DIE at 0x%x [in module %s]"),
7866                        child_die->offset.sect_off, objfile->name);
7867             continue;
7868           }
7869
7870         VEC_safe_push (const_char_ptr, excludes, imported_name);
7871
7872         process_die (child_die, cu);
7873       }
7874
7875   cp_add_using_directive (import_prefix,
7876                           canonical_name,
7877                           import_alias,
7878                           imported_declaration,
7879                           excludes,
7880                           0,
7881                           &objfile->objfile_obstack);
7882
7883   do_cleanups (cleanups);
7884 }
7885
7886 /* Cleanup function for handle_DW_AT_stmt_list.  */
7887
7888 static void
7889 free_cu_line_header (void *arg)
7890 {
7891   struct dwarf2_cu *cu = arg;
7892
7893   free_line_header (cu->line_header);
7894   cu->line_header = NULL;
7895 }
7896
7897 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
7898    directory paths.  GCC SVN r127613 (new option -fdebug-prefix-map) fixed
7899    this, it was first present in GCC release 4.3.0.  */
7900
7901 static int
7902 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
7903 {
7904   if (!cu->checked_producer)
7905     check_producer (cu);
7906
7907   return cu->producer_is_gcc_lt_4_3;
7908 }
7909
7910 static void
7911 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7912                          const char **name, const char **comp_dir)
7913 {
7914   struct attribute *attr;
7915
7916   *name = NULL;
7917   *comp_dir = NULL;
7918
7919   /* Find the filename.  Do not use dwarf2_name here, since the filename
7920      is not a source language identifier.  */
7921   attr = dwarf2_attr (die, DW_AT_name, cu);
7922   if (attr)
7923     {
7924       *name = DW_STRING (attr);
7925     }
7926
7927   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7928   if (attr)
7929     *comp_dir = DW_STRING (attr);
7930   else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
7931            && IS_ABSOLUTE_PATH (*name))
7932     {
7933       char *d = ldirname (*name);
7934
7935       *comp_dir = d;
7936       if (d != NULL)
7937         make_cleanup (xfree, d);
7938     }
7939   if (*comp_dir != NULL)
7940     {
7941       /* Irix 6.2 native cc prepends <machine>.: to the compilation
7942          directory, get rid of it.  */
7943       char *cp = strchr (*comp_dir, ':');
7944
7945       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7946         *comp_dir = cp + 1;
7947     }
7948
7949   if (*name == NULL)
7950     *name = "<unknown>";
7951 }
7952
7953 /* Handle DW_AT_stmt_list for a compilation unit.
7954    DIE is the DW_TAG_compile_unit die for CU.
7955    COMP_DIR is the compilation directory.
7956    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
7957
7958 static void
7959 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7960                         const char *comp_dir)
7961 {
7962   struct attribute *attr;
7963
7964   gdb_assert (! cu->per_cu->is_debug_types);
7965
7966   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7967   if (attr)
7968     {
7969       unsigned int line_offset = DW_UNSND (attr);
7970       struct line_header *line_header
7971         = dwarf_decode_line_header (line_offset, cu);
7972
7973       if (line_header)
7974         {
7975           cu->line_header = line_header;
7976           make_cleanup (free_cu_line_header, cu);
7977           dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7978         }
7979     }
7980 }
7981
7982 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
7983
7984 static void
7985 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7986 {
7987   struct objfile *objfile = dwarf2_per_objfile->objfile;
7988   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7989   CORE_ADDR lowpc = ((CORE_ADDR) -1);
7990   CORE_ADDR highpc = ((CORE_ADDR) 0);
7991   struct attribute *attr;
7992   const char *name = NULL;
7993   const char *comp_dir = NULL;
7994   struct die_info *child_die;
7995   bfd *abfd = objfile->obfd;
7996   CORE_ADDR baseaddr;
7997
7998   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7999
8000   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8001
8002   /* If we didn't find a lowpc, set it to highpc to avoid complaints
8003      from finish_block.  */
8004   if (lowpc == ((CORE_ADDR) -1))
8005     lowpc = highpc;
8006   lowpc += baseaddr;
8007   highpc += baseaddr;
8008
8009   find_file_and_directory (die, cu, &name, &comp_dir);
8010
8011   prepare_one_comp_unit (cu, die, cu->language);
8012
8013   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8014      standardised yet.  As a workaround for the language detection we fall
8015      back to the DW_AT_producer string.  */
8016   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8017     cu->language = language_opencl;
8018
8019   /* Similar hack for Go.  */
8020   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8021     set_cu_language (DW_LANG_Go, cu);
8022
8023   dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8024
8025   /* Decode line number information if present.  We do this before
8026      processing child DIEs, so that the line header table is available
8027      for DW_AT_decl_file.  */
8028   handle_DW_AT_stmt_list (die, cu, comp_dir);
8029
8030   /* Process all dies in compilation unit.  */
8031   if (die->child != NULL)
8032     {
8033       child_die = die->child;
8034       while (child_die && child_die->tag)
8035         {
8036           process_die (child_die, cu);
8037           child_die = sibling_die (child_die);
8038         }
8039     }
8040
8041   /* Decode macro information, if present.  Dwarf 2 macro information
8042      refers to information in the line number info statement program
8043      header, so we can only read it if we've read the header
8044      successfully.  */
8045   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8046   if (attr && cu->line_header)
8047     {
8048       if (dwarf2_attr (die, DW_AT_macro_info, cu))
8049         complaint (&symfile_complaints,
8050                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8051
8052       dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8053     }
8054   else
8055     {
8056       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8057       if (attr && cu->line_header)
8058         {
8059           unsigned int macro_offset = DW_UNSND (attr);
8060
8061           dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8062         }
8063     }
8064
8065   do_cleanups (back_to);
8066 }
8067
8068 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8069    Create the set of symtabs used by this TU, or if this TU is sharing
8070    symtabs with another TU and the symtabs have already been created
8071    then restore those symtabs in the line header.
8072    We don't need the pc/line-number mapping for type units.  */
8073
8074 static void
8075 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8076 {
8077   struct objfile *objfile = dwarf2_per_objfile->objfile;
8078   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8079   struct type_unit_group *tu_group;
8080   int first_time;
8081   struct line_header *lh;
8082   struct attribute *attr;
8083   unsigned int i, line_offset;
8084
8085   gdb_assert (per_cu->is_debug_types);
8086
8087   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8088
8089   /* If we're using .gdb_index (includes -readnow) then
8090      per_cu->s.type_unit_group may not have been set up yet.  */
8091   if (per_cu->type_unit_group == NULL)
8092     per_cu->type_unit_group = get_type_unit_group (cu, attr);
8093   tu_group = per_cu->type_unit_group;
8094
8095   /* If we've already processed this stmt_list there's no real need to
8096      do it again, we could fake it and just recreate the part we need
8097      (file name,index -> symtab mapping).  If data shows this optimization
8098      is useful we can do it then.  */
8099   first_time = tu_group->primary_symtab == NULL;
8100
8101   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8102      debug info.  */
8103   lh = NULL;
8104   if (attr != NULL)
8105     {
8106       line_offset = DW_UNSND (attr);
8107       lh = dwarf_decode_line_header (line_offset, cu);
8108     }
8109   if (lh == NULL)
8110     {
8111       if (first_time)
8112         dwarf2_start_symtab (cu, "", NULL, 0);
8113       else
8114         {
8115           gdb_assert (tu_group->symtabs == NULL);
8116           restart_symtab (0);
8117         }
8118       /* Note: The primary symtab will get allocated at the end.  */
8119       return;
8120     }
8121
8122   cu->line_header = lh;
8123   make_cleanup (free_cu_line_header, cu);
8124
8125   if (first_time)
8126     {
8127       dwarf2_start_symtab (cu, "", NULL, 0);
8128
8129       tu_group->num_symtabs = lh->num_file_names;
8130       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8131
8132       for (i = 0; i < lh->num_file_names; ++i)
8133         {
8134           char *dir = NULL;
8135           struct file_entry *fe = &lh->file_names[i];
8136
8137           if (fe->dir_index)
8138             dir = lh->include_dirs[fe->dir_index - 1];
8139           dwarf2_start_subfile (fe->name, dir, NULL);
8140
8141           /* Note: We don't have to watch for the main subfile here, type units
8142              don't have DW_AT_name.  */
8143
8144           if (current_subfile->symtab == NULL)
8145             {
8146               /* NOTE: start_subfile will recognize when it's been passed
8147                  a file it has already seen.  So we can't assume there's a
8148                  simple mapping from lh->file_names to subfiles,
8149                  lh->file_names may contain dups.  */
8150               current_subfile->symtab = allocate_symtab (current_subfile->name,
8151                                                          objfile);
8152             }
8153
8154           fe->symtab = current_subfile->symtab;
8155           tu_group->symtabs[i] = fe->symtab;
8156         }
8157     }
8158   else
8159     {
8160       restart_symtab (0);
8161
8162       for (i = 0; i < lh->num_file_names; ++i)
8163         {
8164           struct file_entry *fe = &lh->file_names[i];
8165
8166           fe->symtab = tu_group->symtabs[i];
8167         }
8168     }
8169
8170   /* The main symtab is allocated last.  Type units don't have DW_AT_name
8171      so they don't have a "real" (so to speak) symtab anyway.
8172      There is later code that will assign the main symtab to all symbols
8173      that don't have one.  We need to handle the case of a symbol with a
8174      missing symtab (DW_AT_decl_file) anyway.  */
8175 }
8176
8177 /* Process DW_TAG_type_unit.
8178    For TUs we want to skip the first top level sibling if it's not the
8179    actual type being defined by this TU.  In this case the first top
8180    level sibling is there to provide context only.  */
8181
8182 static void
8183 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8184 {
8185   struct die_info *child_die;
8186
8187   prepare_one_comp_unit (cu, die, language_minimal);
8188
8189   /* Initialize (or reinitialize) the machinery for building symtabs.
8190      We do this before processing child DIEs, so that the line header table
8191      is available for DW_AT_decl_file.  */
8192   setup_type_unit_groups (die, cu);
8193
8194   if (die->child != NULL)
8195     {
8196       child_die = die->child;
8197       while (child_die && child_die->tag)
8198         {
8199           process_die (child_die, cu);
8200           child_die = sibling_die (child_die);
8201         }
8202     }
8203 }
8204 \f
8205 /* DWO/DWP files.
8206
8207    http://gcc.gnu.org/wiki/DebugFission
8208    http://gcc.gnu.org/wiki/DebugFissionDWP
8209
8210    To simplify handling of both DWO files ("object" files with the DWARF info)
8211    and DWP files (a file with the DWOs packaged up into one file), we treat
8212    DWP files as having a collection of virtual DWO files.  */
8213
8214 static hashval_t
8215 hash_dwo_file (const void *item)
8216 {
8217   const struct dwo_file *dwo_file = item;
8218
8219   return htab_hash_string (dwo_file->name);
8220 }
8221
8222 static int
8223 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8224 {
8225   const struct dwo_file *lhs = item_lhs;
8226   const struct dwo_file *rhs = item_rhs;
8227
8228   return strcmp (lhs->name, rhs->name) == 0;
8229 }
8230
8231 /* Allocate a hash table for DWO files.  */
8232
8233 static htab_t
8234 allocate_dwo_file_hash_table (void)
8235 {
8236   struct objfile *objfile = dwarf2_per_objfile->objfile;
8237
8238   return htab_create_alloc_ex (41,
8239                                hash_dwo_file,
8240                                eq_dwo_file,
8241                                NULL,
8242                                &objfile->objfile_obstack,
8243                                hashtab_obstack_allocate,
8244                                dummy_obstack_deallocate);
8245 }
8246
8247 /* Lookup DWO file DWO_NAME.  */
8248
8249 static void **
8250 lookup_dwo_file_slot (const char *dwo_name)
8251 {
8252   struct dwo_file find_entry;
8253   void **slot;
8254
8255   if (dwarf2_per_objfile->dwo_files == NULL)
8256     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8257
8258   memset (&find_entry, 0, sizeof (find_entry));
8259   find_entry.name = dwo_name;
8260   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8261
8262   return slot;
8263 }
8264
8265 static hashval_t
8266 hash_dwo_unit (const void *item)
8267 {
8268   const struct dwo_unit *dwo_unit = item;
8269
8270   /* This drops the top 32 bits of the id, but is ok for a hash.  */
8271   return dwo_unit->signature;
8272 }
8273
8274 static int
8275 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8276 {
8277   const struct dwo_unit *lhs = item_lhs;
8278   const struct dwo_unit *rhs = item_rhs;
8279
8280   /* The signature is assumed to be unique within the DWO file.
8281      So while object file CU dwo_id's always have the value zero,
8282      that's OK, assuming each object file DWO file has only one CU,
8283      and that's the rule for now.  */
8284   return lhs->signature == rhs->signature;
8285 }
8286
8287 /* Allocate a hash table for DWO CUs,TUs.
8288    There is one of these tables for each of CUs,TUs for each DWO file.  */
8289
8290 static htab_t
8291 allocate_dwo_unit_table (struct objfile *objfile)
8292 {
8293   /* Start out with a pretty small number.
8294      Generally DWO files contain only one CU and maybe some TUs.  */
8295   return htab_create_alloc_ex (3,
8296                                hash_dwo_unit,
8297                                eq_dwo_unit,
8298                                NULL,
8299                                &objfile->objfile_obstack,
8300                                hashtab_obstack_allocate,
8301                                dummy_obstack_deallocate);
8302 }
8303
8304 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader.  */
8305
8306 struct create_dwo_info_table_data
8307 {
8308   struct dwo_file *dwo_file;
8309   htab_t cu_htab;
8310 };
8311
8312 /* die_reader_func for create_dwo_debug_info_hash_table.  */
8313
8314 static void
8315 create_dwo_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8316                                          gdb_byte *info_ptr,
8317                                          struct die_info *comp_unit_die,
8318                                          int has_children,
8319                                          void *datap)
8320 {
8321   struct dwarf2_cu *cu = reader->cu;
8322   struct objfile *objfile = dwarf2_per_objfile->objfile;
8323   sect_offset offset = cu->per_cu->offset;
8324   struct dwarf2_section_info *section = cu->per_cu->section;
8325   struct create_dwo_info_table_data *data = datap;
8326   struct dwo_file *dwo_file = data->dwo_file;
8327   htab_t cu_htab = data->cu_htab;
8328   void **slot;
8329   struct attribute *attr;
8330   struct dwo_unit *dwo_unit;
8331
8332   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8333   if (attr == NULL)
8334     {
8335       error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8336                " its dwo_id [in module %s]"),
8337              offset.sect_off, dwo_file->name);
8338       return;
8339     }
8340
8341   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8342   dwo_unit->dwo_file = dwo_file;
8343   dwo_unit->signature = DW_UNSND (attr);
8344   dwo_unit->section = section;
8345   dwo_unit->offset = offset;
8346   dwo_unit->length = cu->per_cu->length;
8347
8348   slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8349   gdb_assert (slot != NULL);
8350   if (*slot != NULL)
8351     {
8352       const struct dwo_unit *dup_dwo_unit = *slot;
8353
8354       complaint (&symfile_complaints,
8355                  _("debug entry at offset 0x%x is duplicate to the entry at"
8356                    " offset 0x%x, dwo_id 0x%s [in module %s]"),
8357                  offset.sect_off, dup_dwo_unit->offset.sect_off,
8358                  phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8359                  dwo_file->name);
8360     }
8361   else
8362     *slot = dwo_unit;
8363
8364   if (dwarf2_read_debug)
8365     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id 0x%s\n",
8366                         offset.sect_off,
8367                         phex (dwo_unit->signature,
8368                               sizeof (dwo_unit->signature)));
8369 }
8370
8371 /* Create a hash table to map DWO IDs to their CU entry in
8372    .debug_info.dwo in DWO_FILE.
8373    Note: This function processes DWO files only, not DWP files.  */
8374
8375 static htab_t
8376 create_dwo_debug_info_hash_table (struct dwo_file *dwo_file)
8377 {
8378   struct objfile *objfile = dwarf2_per_objfile->objfile;
8379   struct dwarf2_section_info *section = &dwo_file->sections.info;
8380   bfd *abfd;
8381   htab_t cu_htab;
8382   gdb_byte *info_ptr, *end_ptr;
8383   struct create_dwo_info_table_data create_dwo_info_table_data;
8384
8385   dwarf2_read_section (objfile, section);
8386   info_ptr = section->buffer;
8387
8388   if (info_ptr == NULL)
8389     return NULL;
8390
8391   /* We can't set abfd until now because the section may be empty or
8392      not present, in which case section->asection will be NULL.  */
8393   abfd = section->asection->owner;
8394
8395   if (dwarf2_read_debug)
8396     fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8397                         bfd_get_filename (abfd));
8398
8399   cu_htab = allocate_dwo_unit_table (objfile);
8400
8401   create_dwo_info_table_data.dwo_file = dwo_file;
8402   create_dwo_info_table_data.cu_htab = cu_htab;
8403
8404   end_ptr = info_ptr + section->size;
8405   while (info_ptr < end_ptr)
8406     {
8407       struct dwarf2_per_cu_data per_cu;
8408
8409       memset (&per_cu, 0, sizeof (per_cu));
8410       per_cu.objfile = objfile;
8411       per_cu.is_debug_types = 0;
8412       per_cu.offset.sect_off = info_ptr - section->buffer;
8413       per_cu.section = section;
8414
8415       init_cutu_and_read_dies_no_follow (&per_cu,
8416                                          &dwo_file->sections.abbrev,
8417                                          dwo_file,
8418                                          create_dwo_debug_info_hash_table_reader,
8419                                          &create_dwo_info_table_data);
8420
8421       info_ptr += per_cu.length;
8422     }
8423
8424   return cu_htab;
8425 }
8426
8427 /* DWP file .debug_{cu,tu}_index section format:
8428    [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8429
8430    Both index sections have the same format, and serve to map a 64-bit
8431    signature to a set of section numbers.  Each section begins with a header,
8432    followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8433    indexes, and a pool of 32-bit section numbers.  The index sections will be
8434    aligned at 8-byte boundaries in the file.
8435
8436    The index section header contains two unsigned 32-bit values (using the
8437    byte order of the application binary):
8438
8439     N, the number of compilation units or type units in the index
8440     M, the number of slots in the hash table
8441
8442   (We assume that N and M will not exceed 2^32 - 1.)
8443
8444   The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8445
8446   The hash table begins at offset 8 in the section, and consists of an array
8447   of M 64-bit slots.  Each slot contains a 64-bit signature (using the byte
8448   order of the application binary).  Unused slots in the hash table are 0.
8449   (We rely on the extreme unlikeliness of a signature being exactly 0.)
8450
8451   The parallel table begins immediately after the hash table
8452   (at offset 8 + 8 * M from the beginning of the section), and consists of an
8453   array of 32-bit indexes (using the byte order of the application binary),
8454   corresponding 1-1 with slots in the hash table.  Each entry in the parallel
8455   table contains a 32-bit index into the pool of section numbers.  For unused
8456   hash table slots, the corresponding entry in the parallel table will be 0.
8457
8458   Given a 64-bit compilation unit signature or a type signature S, an entry
8459   in the hash table is located as follows:
8460
8461   1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8462      the low-order k bits all set to 1.
8463
8464   2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8465
8466   3) If the hash table entry at index H matches the signature, use that
8467      entry.  If the hash table entry at index H is unused (all zeroes),
8468      terminate the search: the signature is not present in the table.
8469
8470   4) Let H = (H + H') modulo M. Repeat at Step 3.
8471
8472   Because M > N and H' and M are relatively prime, the search is guaranteed
8473   to stop at an unused slot or find the match.
8474
8475   The pool of section numbers begins immediately following the hash table
8476   (at offset 8 + 12 * M from the beginning of the section).  The pool of
8477   section numbers consists of an array of 32-bit words (using the byte order
8478   of the application binary).  Each item in the array is indexed starting
8479   from 0.  The hash table entry provides the index of the first section
8480   number in the set.  Additional section numbers in the set follow, and the
8481   set is terminated by a 0 entry (section number 0 is not used in ELF).
8482
8483   In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8484   section must be the first entry in the set, and the .debug_abbrev.dwo must
8485   be the second entry. Other members of the set may follow in any order.  */
8486
8487 /* Create a hash table to map DWO IDs to their CU/TU entry in
8488    .debug_{info,types}.dwo in DWP_FILE.
8489    Returns NULL if there isn't one.
8490    Note: This function processes DWP files only, not DWO files.  */
8491
8492 static struct dwp_hash_table *
8493 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8494 {
8495   struct objfile *objfile = dwarf2_per_objfile->objfile;
8496   bfd *dbfd = dwp_file->dbfd;
8497   char *index_ptr, *index_end;
8498   struct dwarf2_section_info *index;
8499   uint32_t version, nr_units, nr_slots;
8500   struct dwp_hash_table *htab;
8501
8502   if (is_debug_types)
8503     index = &dwp_file->sections.tu_index;
8504   else
8505     index = &dwp_file->sections.cu_index;
8506
8507   if (dwarf2_section_empty_p (index))
8508     return NULL;
8509   dwarf2_read_section (objfile, index);
8510
8511   index_ptr = index->buffer;
8512   index_end = index_ptr + index->size;
8513
8514   version = read_4_bytes (dbfd, index_ptr);
8515   index_ptr += 8; /* Skip the unused word.  */
8516   nr_units = read_4_bytes (dbfd, index_ptr);
8517   index_ptr += 4;
8518   nr_slots = read_4_bytes (dbfd, index_ptr);
8519   index_ptr += 4;
8520
8521   if (version != 1)
8522     {
8523       error (_("Dwarf Error: unsupported DWP file version (%u)"
8524                " [in module %s]"),
8525              version, dwp_file->name);
8526     }
8527   if (nr_slots != (nr_slots & -nr_slots))
8528     {
8529       error (_("Dwarf Error: number of slots in DWP hash table (%u)"
8530                " is not power of 2 [in module %s]"),
8531              nr_slots, dwp_file->name);
8532     }
8533
8534   htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8535   htab->nr_units = nr_units;
8536   htab->nr_slots = nr_slots;
8537   htab->hash_table = index_ptr;
8538   htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8539   htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8540
8541   return htab;
8542 }
8543
8544 /* Update SECTIONS with the data from SECTP.
8545
8546    This function is like the other "locate" section routines that are
8547    passed to bfd_map_over_sections, but in this context the sections to
8548    read comes from the DWP hash table, not the full ELF section table.
8549
8550    The result is non-zero for success, or zero if an error was found.  */
8551
8552 static int
8553 locate_virtual_dwo_sections (asection *sectp,
8554                              struct virtual_dwo_sections *sections)
8555 {
8556   const struct dwop_section_names *names = &dwop_section_names;
8557
8558   if (section_is_p (sectp->name, &names->abbrev_dwo))
8559     {
8560       /* There can be only one.  */
8561       if (sections->abbrev.asection != NULL)
8562         return 0;
8563       sections->abbrev.asection = sectp;
8564       sections->abbrev.size = bfd_get_section_size (sectp);
8565     }
8566   else if (section_is_p (sectp->name, &names->info_dwo)
8567            || section_is_p (sectp->name, &names->types_dwo))
8568     {
8569       /* There can be only one.  */
8570       if (sections->info_or_types.asection != NULL)
8571         return 0;
8572       sections->info_or_types.asection = sectp;
8573       sections->info_or_types.size = bfd_get_section_size (sectp);
8574     }
8575   else if (section_is_p (sectp->name, &names->line_dwo))
8576     {
8577       /* There can be only one.  */
8578       if (sections->line.asection != NULL)
8579         return 0;
8580       sections->line.asection = sectp;
8581       sections->line.size = bfd_get_section_size (sectp);
8582     }
8583   else if (section_is_p (sectp->name, &names->loc_dwo))
8584     {
8585       /* There can be only one.  */
8586       if (sections->loc.asection != NULL)
8587         return 0;
8588       sections->loc.asection = sectp;
8589       sections->loc.size = bfd_get_section_size (sectp);
8590     }
8591   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8592     {
8593       /* There can be only one.  */
8594       if (sections->macinfo.asection != NULL)
8595         return 0;
8596       sections->macinfo.asection = sectp;
8597       sections->macinfo.size = bfd_get_section_size (sectp);
8598     }
8599   else if (section_is_p (sectp->name, &names->macro_dwo))
8600     {
8601       /* There can be only one.  */
8602       if (sections->macro.asection != NULL)
8603         return 0;
8604       sections->macro.asection = sectp;
8605       sections->macro.size = bfd_get_section_size (sectp);
8606     }
8607   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8608     {
8609       /* There can be only one.  */
8610       if (sections->str_offsets.asection != NULL)
8611         return 0;
8612       sections->str_offsets.asection = sectp;
8613       sections->str_offsets.size = bfd_get_section_size (sectp);
8614     }
8615   else
8616     {
8617       /* No other kind of section is valid.  */
8618       return 0;
8619     }
8620
8621   return 1;
8622 }
8623
8624 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
8625    HTAB is the hash table from the DWP file.
8626    SECTION_INDEX is the index of the DWO in HTAB.  */
8627
8628 static struct dwo_unit *
8629 create_dwo_in_dwp (struct dwp_file *dwp_file,
8630                    const struct dwp_hash_table *htab,
8631                    uint32_t section_index,
8632                    ULONGEST signature, int is_debug_types)
8633 {
8634   struct objfile *objfile = dwarf2_per_objfile->objfile;
8635   bfd *dbfd = dwp_file->dbfd;
8636   const char *kind = is_debug_types ? "TU" : "CU";
8637   struct dwo_file *dwo_file;
8638   struct dwo_unit *dwo_unit;
8639   struct virtual_dwo_sections sections;
8640   void **dwo_file_slot;
8641   char *virtual_dwo_name;
8642   struct dwarf2_section_info *cutu;
8643   struct cleanup *cleanups;
8644   int i;
8645
8646   if (dwarf2_read_debug)
8647     {
8648       fprintf_unfiltered (gdb_stdlog, "Reading %s %u/0x%s in DWP file: %s\n",
8649                           kind,
8650                           section_index, phex (signature, sizeof (signature)),
8651                           dwp_file->name);
8652     }
8653
8654   /* Fetch the sections of this DWO.
8655      Put a limit on the number of sections we look for so that bad data
8656      doesn't cause us to loop forever.  */
8657
8658 #define MAX_NR_DWO_SECTIONS \
8659   (1 /* .debug_info or .debug_types */ \
8660    + 1 /* .debug_abbrev */ \
8661    + 1 /* .debug_line */ \
8662    + 1 /* .debug_loc */ \
8663    + 1 /* .debug_str_offsets */ \
8664    + 1 /* .debug_macro */ \
8665    + 1 /* .debug_macinfo */ \
8666    + 1 /* trailing zero */)
8667
8668   memset (&sections, 0, sizeof (sections));
8669   cleanups = make_cleanup (null_cleanup, 0);
8670
8671   for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
8672     {
8673       asection *sectp;
8674       uint32_t section_nr =
8675         read_4_bytes (dbfd,
8676                       htab->section_pool
8677                       + (section_index + i) * sizeof (uint32_t));
8678
8679       if (section_nr == 0)
8680         break;
8681       if (section_nr >= dwp_file->num_sections)
8682         {
8683           error (_("Dwarf Error: bad DWP hash table, section number too large"
8684                    " [in module %s]"),
8685                  dwp_file->name);
8686         }
8687
8688       sectp = dwp_file->elf_sections[section_nr];
8689       if (! locate_virtual_dwo_sections (sectp, &sections))
8690         {
8691           error (_("Dwarf Error: bad DWP hash table, invalid section found"
8692                    " [in module %s]"),
8693                  dwp_file->name);
8694         }
8695     }
8696
8697   if (i < 2
8698       || sections.info_or_types.asection == NULL
8699       || sections.abbrev.asection == NULL)
8700     {
8701       error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
8702                " [in module %s]"),
8703              dwp_file->name);
8704     }
8705   if (i == MAX_NR_DWO_SECTIONS)
8706     {
8707       error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
8708                " [in module %s]"),
8709              dwp_file->name);
8710     }
8711
8712   /* It's easier for the rest of the code if we fake a struct dwo_file and
8713      have dwo_unit "live" in that.  At least for now.
8714
8715      The DWP file can be made up of a random collection of CUs and TUs.
8716      However, for each CU + set of TUs that came from the same original DWO
8717      file, we want to combine them back into a virtual DWO file to save space
8718      (fewer struct dwo_file objects to allocated).  Remember that for really
8719      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */
8720
8721   virtual_dwo_name =
8722     xstrprintf ("virtual-dwo/%d-%d-%d-%d",
8723                 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
8724                 sections.line.asection ? sections.line.asection->id : 0,
8725                 sections.loc.asection ? sections.loc.asection->id : 0,
8726                 (sections.str_offsets.asection
8727                 ? sections.str_offsets.asection->id
8728                 : 0));
8729   make_cleanup (xfree, virtual_dwo_name);
8730   /* Can we use an existing virtual DWO file?  */
8731   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name);
8732   /* Create one if necessary.  */
8733   if (*dwo_file_slot == NULL)
8734     {
8735       if (dwarf2_read_debug)
8736         {
8737           fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
8738                               virtual_dwo_name);
8739         }
8740       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8741       dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8742                                       virtual_dwo_name,
8743                                       strlen (virtual_dwo_name));
8744       dwo_file->sections.abbrev = sections.abbrev;
8745       dwo_file->sections.line = sections.line;
8746       dwo_file->sections.loc = sections.loc;
8747       dwo_file->sections.macinfo = sections.macinfo;
8748       dwo_file->sections.macro = sections.macro;
8749       dwo_file->sections.str_offsets = sections.str_offsets;
8750       /* The "str" section is global to the entire DWP file.  */
8751       dwo_file->sections.str = dwp_file->sections.str;
8752       /* The info or types section is assigned later to dwo_unit,
8753          there's no need to record it in dwo_file.
8754          Also, we can't simply record type sections in dwo_file because
8755          we record a pointer into the vector in dwo_unit.  As we collect more
8756          types we'll grow the vector and eventually have to reallocate space
8757          for it, invalidating all the pointers into the current copy.  */
8758       *dwo_file_slot = dwo_file;
8759     }
8760   else
8761     {
8762       if (dwarf2_read_debug)
8763         {
8764           fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
8765                               virtual_dwo_name);
8766         }
8767       dwo_file = *dwo_file_slot;
8768     }
8769   do_cleanups (cleanups);
8770
8771   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8772   dwo_unit->dwo_file = dwo_file;
8773   dwo_unit->signature = signature;
8774   dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
8775                                      sizeof (struct dwarf2_section_info));
8776   *dwo_unit->section = sections.info_or_types;
8777   /* offset, length, type_offset_in_tu are set later.  */
8778
8779   return dwo_unit;
8780 }
8781
8782 /* Lookup the DWO with SIGNATURE in DWP_FILE.  */
8783
8784 static struct dwo_unit *
8785 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
8786                    const struct dwp_hash_table *htab,
8787                    ULONGEST signature, int is_debug_types)
8788 {
8789   bfd *dbfd = dwp_file->dbfd;
8790   uint32_t mask = htab->nr_slots - 1;
8791   uint32_t hash = signature & mask;
8792   uint32_t hash2 = ((signature >> 32) & mask) | 1;
8793   unsigned int i;
8794   void **slot;
8795   struct dwo_unit find_dwo_cu, *dwo_cu;
8796
8797   memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
8798   find_dwo_cu.signature = signature;
8799   slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
8800
8801   if (*slot != NULL)
8802     return *slot;
8803
8804   /* Use a for loop so that we don't loop forever on bad debug info.  */
8805   for (i = 0; i < htab->nr_slots; ++i)
8806     {
8807       ULONGEST signature_in_table;
8808
8809       signature_in_table =
8810         read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
8811       if (signature_in_table == signature)
8812         {
8813           uint32_t section_index =
8814             read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
8815
8816           *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
8817                                      signature, is_debug_types);
8818           return *slot;
8819         }
8820       if (signature_in_table == 0)
8821         return NULL;
8822       hash = (hash + hash2) & mask;
8823     }
8824
8825   error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
8826            " [in module %s]"),
8827          dwp_file->name);
8828 }
8829
8830 /* Subroutine of open_dwop_file to simplify it.
8831    Open the file specified by FILE_NAME and hand it off to BFD for
8832    preliminary analysis.  Return a newly initialized bfd *, which
8833    includes a canonicalized copy of FILE_NAME.
8834    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8835    In case of trouble, return NULL.
8836    NOTE: This function is derived from symfile_bfd_open.  */
8837
8838 static bfd *
8839 try_open_dwop_file (const char *file_name, int is_dwp)
8840 {
8841   bfd *sym_bfd;
8842   int desc, flags;
8843   char *absolute_name;
8844
8845   flags = OPF_TRY_CWD_FIRST;
8846   if (is_dwp)
8847     flags |= OPF_SEARCH_IN_PATH;
8848   desc = openp (debug_file_directory, flags, file_name,
8849                 O_RDONLY | O_BINARY, &absolute_name);
8850   if (desc < 0)
8851     return NULL;
8852
8853   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8854   if (!sym_bfd)
8855     {
8856       xfree (absolute_name);
8857       return NULL;
8858     }
8859   xfree (absolute_name);
8860   bfd_set_cacheable (sym_bfd, 1);
8861
8862   if (!bfd_check_format (sym_bfd, bfd_object))
8863     {
8864       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
8865       return NULL;
8866     }
8867
8868   return sym_bfd;
8869 }
8870
8871 /* Try to open DWO/DWP file FILE_NAME.
8872    COMP_DIR is the DW_AT_comp_dir attribute.
8873    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
8874    The result is the bfd handle of the file.
8875    If there is a problem finding or opening the file, return NULL.
8876    Upon success, the canonicalized path of the file is stored in the bfd,
8877    same as symfile_bfd_open.  */
8878
8879 static bfd *
8880 open_dwop_file (const char *file_name, const char *comp_dir, int is_dwp)
8881 {
8882   bfd *abfd;
8883
8884   if (IS_ABSOLUTE_PATH (file_name))
8885     return try_open_dwop_file (file_name, is_dwp);
8886
8887   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
8888
8889   if (comp_dir != NULL)
8890     {
8891       char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
8892
8893       /* NOTE: If comp_dir is a relative path, this will also try the
8894          search path, which seems useful.  */
8895       abfd = try_open_dwop_file (path_to_try, is_dwp);
8896       xfree (path_to_try);
8897       if (abfd != NULL)
8898         return abfd;
8899     }
8900
8901   /* That didn't work, try debug-file-directory, which, despite its name,
8902      is a list of paths.  */
8903
8904   if (*debug_file_directory == '\0')
8905     return NULL;
8906
8907   return try_open_dwop_file (file_name, is_dwp);
8908 }
8909
8910 /* This function is mapped across the sections and remembers the offset and
8911    size of each of the DWO debugging sections we are interested in.  */
8912
8913 static void
8914 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
8915 {
8916   struct dwo_sections *dwo_sections = dwo_sections_ptr;
8917   const struct dwop_section_names *names = &dwop_section_names;
8918
8919   if (section_is_p (sectp->name, &names->abbrev_dwo))
8920     {
8921       dwo_sections->abbrev.asection = sectp;
8922       dwo_sections->abbrev.size = bfd_get_section_size (sectp);
8923     }
8924   else if (section_is_p (sectp->name, &names->info_dwo))
8925     {
8926       dwo_sections->info.asection = sectp;
8927       dwo_sections->info.size = bfd_get_section_size (sectp);
8928     }
8929   else if (section_is_p (sectp->name, &names->line_dwo))
8930     {
8931       dwo_sections->line.asection = sectp;
8932       dwo_sections->line.size = bfd_get_section_size (sectp);
8933     }
8934   else if (section_is_p (sectp->name, &names->loc_dwo))
8935     {
8936       dwo_sections->loc.asection = sectp;
8937       dwo_sections->loc.size = bfd_get_section_size (sectp);
8938     }
8939   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8940     {
8941       dwo_sections->macinfo.asection = sectp;
8942       dwo_sections->macinfo.size = bfd_get_section_size (sectp);
8943     }
8944   else if (section_is_p (sectp->name, &names->macro_dwo))
8945     {
8946       dwo_sections->macro.asection = sectp;
8947       dwo_sections->macro.size = bfd_get_section_size (sectp);
8948     }
8949   else if (section_is_p (sectp->name, &names->str_dwo))
8950     {
8951       dwo_sections->str.asection = sectp;
8952       dwo_sections->str.size = bfd_get_section_size (sectp);
8953     }
8954   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8955     {
8956       dwo_sections->str_offsets.asection = sectp;
8957       dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
8958     }
8959   else if (section_is_p (sectp->name, &names->types_dwo))
8960     {
8961       struct dwarf2_section_info type_section;
8962
8963       memset (&type_section, 0, sizeof (type_section));
8964       type_section.asection = sectp;
8965       type_section.size = bfd_get_section_size (sectp);
8966       VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
8967                      &type_section);
8968     }
8969 }
8970
8971 /* Initialize the use of the DWO file specified by DWO_NAME.
8972    The result is NULL if DWO_NAME can't be found.  */
8973
8974 static struct dwo_file *
8975 open_and_init_dwo_file (const char *dwo_name, const char *comp_dir)
8976 {
8977   struct objfile *objfile = dwarf2_per_objfile->objfile;
8978   struct dwo_file *dwo_file;
8979   bfd *dbfd;
8980   struct cleanup *cleanups;
8981
8982   dbfd = open_dwop_file (dwo_name, comp_dir, 0);
8983   if (dbfd == NULL)
8984     {
8985       if (dwarf2_read_debug)
8986         fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
8987       return NULL;
8988     }
8989   dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
8990   dwo_file->name = obstack_copy0 (&objfile->objfile_obstack,
8991                                   dwo_name, strlen (dwo_name));
8992   dwo_file->dbfd = dbfd;
8993
8994   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8995
8996   bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
8997
8998   dwo_file->cus = create_dwo_debug_info_hash_table (dwo_file);
8999
9000   dwo_file->tus = create_debug_types_hash_table (dwo_file,
9001                                                  dwo_file->sections.types);
9002
9003   discard_cleanups (cleanups);
9004
9005   if (dwarf2_read_debug)
9006     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9007
9008   return dwo_file;
9009 }
9010
9011 /* This function is mapped across the sections and remembers the offset and
9012    size of each of the DWP debugging sections we are interested in.  */
9013
9014 static void
9015 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9016 {
9017   struct dwp_file *dwp_file = dwp_file_ptr;
9018   const struct dwop_section_names *names = &dwop_section_names;
9019   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9020
9021   /* Record the ELF section number for later lookup: this is what the
9022      .debug_cu_index,.debug_tu_index tables use.  */
9023   gdb_assert (elf_section_nr < dwp_file->num_sections);
9024   dwp_file->elf_sections[elf_section_nr] = sectp;
9025
9026   /* Look for specific sections that we need.  */
9027   if (section_is_p (sectp->name, &names->str_dwo))
9028     {
9029       dwp_file->sections.str.asection = sectp;
9030       dwp_file->sections.str.size = bfd_get_section_size (sectp);
9031     }
9032   else if (section_is_p (sectp->name, &names->cu_index))
9033     {
9034       dwp_file->sections.cu_index.asection = sectp;
9035       dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9036     }
9037   else if (section_is_p (sectp->name, &names->tu_index))
9038     {
9039       dwp_file->sections.tu_index.asection = sectp;
9040       dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9041     }
9042 }
9043
9044 /* Hash function for dwp_file loaded CUs/TUs.  */
9045
9046 static hashval_t
9047 hash_dwp_loaded_cutus (const void *item)
9048 {
9049   const struct dwo_unit *dwo_unit = item;
9050
9051   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
9052   return dwo_unit->signature;
9053 }
9054
9055 /* Equality function for dwp_file loaded CUs/TUs.  */
9056
9057 static int
9058 eq_dwp_loaded_cutus (const void *a, const void *b)
9059 {
9060   const struct dwo_unit *dua = a;
9061   const struct dwo_unit *dub = b;
9062
9063   return dua->signature == dub->signature;
9064 }
9065
9066 /* Allocate a hash table for dwp_file loaded CUs/TUs.  */
9067
9068 static htab_t
9069 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9070 {
9071   return htab_create_alloc_ex (3,
9072                                hash_dwp_loaded_cutus,
9073                                eq_dwp_loaded_cutus,
9074                                NULL,
9075                                &objfile->objfile_obstack,
9076                                hashtab_obstack_allocate,
9077                                dummy_obstack_deallocate);
9078 }
9079
9080 /* Initialize the use of the DWP file for the current objfile.
9081    By convention the name of the DWP file is ${objfile}.dwp.
9082    The result is NULL if it can't be found.  */
9083
9084 static struct dwp_file *
9085 open_and_init_dwp_file (const char *comp_dir)
9086 {
9087   struct objfile *objfile = dwarf2_per_objfile->objfile;
9088   struct dwp_file *dwp_file;
9089   char *dwp_name;
9090   bfd *dbfd;
9091   struct cleanup *cleanups;
9092
9093   dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9094   cleanups = make_cleanup (xfree, dwp_name);
9095
9096   dbfd = open_dwop_file (dwp_name, comp_dir, 1);
9097   if (dbfd == NULL)
9098     {
9099       if (dwarf2_read_debug)
9100         fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9101       do_cleanups (cleanups);
9102       return NULL;
9103     }
9104   dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9105   dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9106                                   dwp_name, strlen (dwp_name));
9107   dwp_file->dbfd = dbfd;
9108   do_cleanups (cleanups);
9109
9110   cleanups = make_cleanup (free_dwo_file_cleanup, dwp_file);
9111
9112   /* +1: section 0 is unused */
9113   dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9114   dwp_file->elf_sections =
9115     OBSTACK_CALLOC (&objfile->objfile_obstack,
9116                     dwp_file->num_sections, asection *);
9117
9118   bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9119
9120   dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9121
9122   dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9123
9124   dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9125
9126   discard_cleanups (cleanups);
9127
9128   if (dwarf2_read_debug)
9129     {
9130       fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9131       fprintf_unfiltered (gdb_stdlog,
9132                           "    %u CUs, %u TUs\n",
9133                           dwp_file->cus ? dwp_file->cus->nr_units : 0,
9134                           dwp_file->tus ? dwp_file->tus->nr_units : 0);
9135     }
9136
9137   return dwp_file;
9138 }
9139
9140 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9141    Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9142    or in the DWP file for the objfile, referenced by THIS_UNIT.
9143    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9144    IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9145
9146    This is called, for example, when wanting to read a variable with a
9147    complex location.  Therefore we don't want to do file i/o for every call.
9148    Therefore we don't want to look for a DWO file on every call.
9149    Therefore we first see if we've already seen SIGNATURE in a DWP file,
9150    then we check if we've already seen DWO_NAME, and only THEN do we check
9151    for a DWO file.
9152
9153    The result is a pointer to the dwo_unit object or NULL if we didn't find it
9154    (dwo_id mismatch or couldn't find the DWO/DWP file).  */
9155
9156 static struct dwo_unit *
9157 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9158                  const char *dwo_name, const char *comp_dir,
9159                  ULONGEST signature, int is_debug_types)
9160 {
9161   struct objfile *objfile = dwarf2_per_objfile->objfile;
9162   const char *kind = is_debug_types ? "TU" : "CU";
9163   void **dwo_file_slot;
9164   struct dwo_file *dwo_file;
9165   struct dwp_file *dwp_file;
9166
9167   /* Have we already read SIGNATURE from a DWP file?  */
9168
9169   if (! dwarf2_per_objfile->dwp_checked)
9170     {
9171       dwarf2_per_objfile->dwp_file = open_and_init_dwp_file (comp_dir);
9172       dwarf2_per_objfile->dwp_checked = 1;
9173     }
9174   dwp_file = dwarf2_per_objfile->dwp_file;
9175
9176   if (dwp_file != NULL)
9177     {
9178       const struct dwp_hash_table *dwp_htab =
9179         is_debug_types ? dwp_file->tus : dwp_file->cus;
9180
9181       if (dwp_htab != NULL)
9182         {
9183           struct dwo_unit *dwo_cutu =
9184             lookup_dwo_in_dwp (dwp_file, dwp_htab, signature, is_debug_types);
9185
9186           if (dwo_cutu != NULL)
9187             {
9188               if (dwarf2_read_debug)
9189                 {
9190                   fprintf_unfiltered (gdb_stdlog,
9191                                       "Virtual DWO %s %s found: @%s\n",
9192                                       kind, hex_string (signature),
9193                                       host_address_to_string (dwo_cutu));
9194                 }
9195               return dwo_cutu;
9196             }
9197         }
9198     }
9199
9200   /* Have we already seen DWO_NAME?  */
9201
9202   dwo_file_slot = lookup_dwo_file_slot (dwo_name);
9203   if (*dwo_file_slot == NULL)
9204     {
9205       /* Read in the file and build a table of the DWOs it contains.  */
9206       *dwo_file_slot = open_and_init_dwo_file (dwo_name, comp_dir);
9207     }
9208   /* NOTE: This will be NULL if unable to open the file.  */
9209   dwo_file = *dwo_file_slot;
9210
9211   if (dwo_file != NULL)
9212     {
9213       htab_t htab = is_debug_types ? dwo_file->tus : dwo_file->cus;
9214
9215       if (htab != NULL)
9216         {
9217           struct dwo_unit find_dwo_cutu, *dwo_cutu;
9218
9219           memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9220           find_dwo_cutu.signature = signature;
9221           dwo_cutu = htab_find (htab, &find_dwo_cutu);
9222
9223           if (dwo_cutu != NULL)
9224             {
9225               if (dwarf2_read_debug)
9226                 {
9227                   fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9228                                       kind, dwo_name, hex_string (signature),
9229                                       host_address_to_string (dwo_cutu));
9230                 }
9231               return dwo_cutu;
9232             }
9233         }
9234     }
9235
9236   /* We didn't find it.  This could mean a dwo_id mismatch, or
9237      someone deleted the DWO/DWP file, or the search path isn't set up
9238      correctly to find the file.  */
9239
9240   if (dwarf2_read_debug)
9241     {
9242       fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9243                           kind, dwo_name, hex_string (signature));
9244     }
9245
9246   complaint (&symfile_complaints,
9247              _("Could not find DWO CU referenced by CU at offset 0x%x"
9248                " [in module %s]"),
9249              this_unit->offset.sect_off, objfile->name);
9250   return NULL;
9251 }
9252
9253 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9254    See lookup_dwo_cutu_unit for details.  */
9255
9256 static struct dwo_unit *
9257 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9258                       const char *dwo_name, const char *comp_dir,
9259                       ULONGEST signature)
9260 {
9261   return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9262 }
9263
9264 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9265    See lookup_dwo_cutu_unit for details.  */
9266
9267 static struct dwo_unit *
9268 lookup_dwo_type_unit (struct signatured_type *this_tu,
9269                       const char *dwo_name, const char *comp_dir)
9270 {
9271   return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9272 }
9273
9274 /* Free all resources associated with DWO_FILE.
9275    Close the DWO file and munmap the sections.
9276    All memory should be on the objfile obstack.  */
9277
9278 static void
9279 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9280 {
9281   int ix;
9282   struct dwarf2_section_info *section;
9283
9284   gdb_bfd_unref (dwo_file->dbfd);
9285
9286   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9287 }
9288
9289 /* Wrapper for free_dwo_file for use in cleanups.  */
9290
9291 static void
9292 free_dwo_file_cleanup (void *arg)
9293 {
9294   struct dwo_file *dwo_file = (struct dwo_file *) arg;
9295   struct objfile *objfile = dwarf2_per_objfile->objfile;
9296
9297   free_dwo_file (dwo_file, objfile);
9298 }
9299
9300 /* Traversal function for free_dwo_files.  */
9301
9302 static int
9303 free_dwo_file_from_slot (void **slot, void *info)
9304 {
9305   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9306   struct objfile *objfile = (struct objfile *) info;
9307
9308   free_dwo_file (dwo_file, objfile);
9309
9310   return 1;
9311 }
9312
9313 /* Free all resources associated with DWO_FILES.  */
9314
9315 static void
9316 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9317 {
9318   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9319 }
9320 \f
9321 /* Read in various DIEs.  */
9322
9323 /* qsort helper for inherit_abstract_dies.  */
9324
9325 static int
9326 unsigned_int_compar (const void *ap, const void *bp)
9327 {
9328   unsigned int a = *(unsigned int *) ap;
9329   unsigned int b = *(unsigned int *) bp;
9330
9331   return (a > b) - (b > a);
9332 }
9333
9334 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9335    Inherit only the children of the DW_AT_abstract_origin DIE not being
9336    already referenced by DW_AT_abstract_origin from the children of the
9337    current DIE.  */
9338
9339 static void
9340 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9341 {
9342   struct die_info *child_die;
9343   unsigned die_children_count;
9344   /* CU offsets which were referenced by children of the current DIE.  */
9345   sect_offset *offsets;
9346   sect_offset *offsets_end, *offsetp;
9347   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
9348   struct die_info *origin_die;
9349   /* Iterator of the ORIGIN_DIE children.  */
9350   struct die_info *origin_child_die;
9351   struct cleanup *cleanups;
9352   struct attribute *attr;
9353   struct dwarf2_cu *origin_cu;
9354   struct pending **origin_previous_list_in_scope;
9355
9356   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9357   if (!attr)
9358     return;
9359
9360   /* Note that following die references may follow to a die in a
9361      different cu.  */
9362
9363   origin_cu = cu;
9364   origin_die = follow_die_ref (die, attr, &origin_cu);
9365
9366   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9367      symbols in.  */
9368   origin_previous_list_in_scope = origin_cu->list_in_scope;
9369   origin_cu->list_in_scope = cu->list_in_scope;
9370
9371   if (die->tag != origin_die->tag
9372       && !(die->tag == DW_TAG_inlined_subroutine
9373            && origin_die->tag == DW_TAG_subprogram))
9374     complaint (&symfile_complaints,
9375                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9376                die->offset.sect_off, origin_die->offset.sect_off);
9377
9378   child_die = die->child;
9379   die_children_count = 0;
9380   while (child_die && child_die->tag)
9381     {
9382       child_die = sibling_die (child_die);
9383       die_children_count++;
9384     }
9385   offsets = xmalloc (sizeof (*offsets) * die_children_count);
9386   cleanups = make_cleanup (xfree, offsets);
9387
9388   offsets_end = offsets;
9389   child_die = die->child;
9390   while (child_die && child_die->tag)
9391     {
9392       /* For each CHILD_DIE, find the corresponding child of
9393          ORIGIN_DIE.  If there is more than one layer of
9394          DW_AT_abstract_origin, follow them all; there shouldn't be,
9395          but GCC versions at least through 4.4 generate this (GCC PR
9396          40573).  */
9397       struct die_info *child_origin_die = child_die;
9398       struct dwarf2_cu *child_origin_cu = cu;
9399
9400       while (1)
9401         {
9402           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9403                               child_origin_cu);
9404           if (attr == NULL)
9405             break;
9406           child_origin_die = follow_die_ref (child_origin_die, attr,
9407                                              &child_origin_cu);
9408         }
9409
9410       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9411          counterpart may exist.  */
9412       if (child_origin_die != child_die)
9413         {
9414           if (child_die->tag != child_origin_die->tag
9415               && !(child_die->tag == DW_TAG_inlined_subroutine
9416                    && child_origin_die->tag == DW_TAG_subprogram))
9417             complaint (&symfile_complaints,
9418                        _("Child DIE 0x%x and its abstract origin 0x%x have "
9419                          "different tags"), child_die->offset.sect_off,
9420                        child_origin_die->offset.sect_off);
9421           if (child_origin_die->parent != origin_die)
9422             complaint (&symfile_complaints,
9423                        _("Child DIE 0x%x and its abstract origin 0x%x have "
9424                          "different parents"), child_die->offset.sect_off,
9425                        child_origin_die->offset.sect_off);
9426           else
9427             *offsets_end++ = child_origin_die->offset;
9428         }
9429       child_die = sibling_die (child_die);
9430     }
9431   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9432          unsigned_int_compar);
9433   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9434     if (offsetp[-1].sect_off == offsetp->sect_off)
9435       complaint (&symfile_complaints,
9436                  _("Multiple children of DIE 0x%x refer "
9437                    "to DIE 0x%x as their abstract origin"),
9438                  die->offset.sect_off, offsetp->sect_off);
9439
9440   offsetp = offsets;
9441   origin_child_die = origin_die->child;
9442   while (origin_child_die && origin_child_die->tag)
9443     {
9444       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
9445       while (offsetp < offsets_end
9446              && offsetp->sect_off < origin_child_die->offset.sect_off)
9447         offsetp++;
9448       if (offsetp >= offsets_end
9449           || offsetp->sect_off > origin_child_die->offset.sect_off)
9450         {
9451           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
9452           process_die (origin_child_die, origin_cu);
9453         }
9454       origin_child_die = sibling_die (origin_child_die);
9455     }
9456   origin_cu->list_in_scope = origin_previous_list_in_scope;
9457
9458   do_cleanups (cleanups);
9459 }
9460
9461 static void
9462 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9463 {
9464   struct objfile *objfile = cu->objfile;
9465   struct context_stack *new;
9466   CORE_ADDR lowpc;
9467   CORE_ADDR highpc;
9468   struct die_info *child_die;
9469   struct attribute *attr, *call_line, *call_file;
9470   const char *name;
9471   CORE_ADDR baseaddr;
9472   struct block *block;
9473   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9474   VEC (symbolp) *template_args = NULL;
9475   struct template_symbol *templ_func = NULL;
9476
9477   if (inlined_func)
9478     {
9479       /* If we do not have call site information, we can't show the
9480          caller of this inlined function.  That's too confusing, so
9481          only use the scope for local variables.  */
9482       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9483       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9484       if (call_line == NULL || call_file == NULL)
9485         {
9486           read_lexical_block_scope (die, cu);
9487           return;
9488         }
9489     }
9490
9491   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9492
9493   name = dwarf2_name (die, cu);
9494
9495   /* Ignore functions with missing or empty names.  These are actually
9496      illegal according to the DWARF standard.  */
9497   if (name == NULL)
9498     {
9499       complaint (&symfile_complaints,
9500                  _("missing name for subprogram DIE at %d"),
9501                  die->offset.sect_off);
9502       return;
9503     }
9504
9505   /* Ignore functions with missing or invalid low and high pc attributes.  */
9506   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9507     {
9508       attr = dwarf2_attr (die, DW_AT_external, cu);
9509       if (!attr || !DW_UNSND (attr))
9510         complaint (&symfile_complaints,
9511                    _("cannot get low and high bounds "
9512                      "for subprogram DIE at %d"),
9513                    die->offset.sect_off);
9514       return;
9515     }
9516
9517   lowpc += baseaddr;
9518   highpc += baseaddr;
9519
9520   /* If we have any template arguments, then we must allocate a
9521      different sort of symbol.  */
9522   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
9523     {
9524       if (child_die->tag == DW_TAG_template_type_param
9525           || child_die->tag == DW_TAG_template_value_param)
9526         {
9527           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
9528                                        struct template_symbol);
9529           templ_func->base.is_cplus_template_function = 1;
9530           break;
9531         }
9532     }
9533
9534   new = push_context (0, lowpc);
9535   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
9536                                (struct symbol *) templ_func);
9537
9538   /* If there is a location expression for DW_AT_frame_base, record
9539      it.  */
9540   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
9541   if (attr)
9542     dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
9543
9544   cu->list_in_scope = &local_symbols;
9545
9546   if (die->child != NULL)
9547     {
9548       child_die = die->child;
9549       while (child_die && child_die->tag)
9550         {
9551           if (child_die->tag == DW_TAG_template_type_param
9552               || child_die->tag == DW_TAG_template_value_param)
9553             {
9554               struct symbol *arg = new_symbol (child_die, NULL, cu);
9555
9556               if (arg != NULL)
9557                 VEC_safe_push (symbolp, template_args, arg);
9558             }
9559           else
9560             process_die (child_die, cu);
9561           child_die = sibling_die (child_die);
9562         }
9563     }
9564
9565   inherit_abstract_dies (die, cu);
9566
9567   /* If we have a DW_AT_specification, we might need to import using
9568      directives from the context of the specification DIE.  See the
9569      comment in determine_prefix.  */
9570   if (cu->language == language_cplus
9571       && dwarf2_attr (die, DW_AT_specification, cu))
9572     {
9573       struct dwarf2_cu *spec_cu = cu;
9574       struct die_info *spec_die = die_specification (die, &spec_cu);
9575
9576       while (spec_die)
9577         {
9578           child_die = spec_die->child;
9579           while (child_die && child_die->tag)
9580             {
9581               if (child_die->tag == DW_TAG_imported_module)
9582                 process_die (child_die, spec_cu);
9583               child_die = sibling_die (child_die);
9584             }
9585
9586           /* In some cases, GCC generates specification DIEs that
9587              themselves contain DW_AT_specification attributes.  */
9588           spec_die = die_specification (spec_die, &spec_cu);
9589         }
9590     }
9591
9592   new = pop_context ();
9593   /* Make a block for the local symbols within.  */
9594   block = finish_block (new->name, &local_symbols, new->old_blocks,
9595                         lowpc, highpc, objfile);
9596
9597   /* For C++, set the block's scope.  */
9598   if ((cu->language == language_cplus || cu->language == language_fortran)
9599       && cu->processing_has_namespace_info)
9600     block_set_scope (block, determine_prefix (die, cu),
9601                      &objfile->objfile_obstack);
9602
9603   /* If we have address ranges, record them.  */
9604   dwarf2_record_block_ranges (die, block, baseaddr, cu);
9605
9606   /* Attach template arguments to function.  */
9607   if (! VEC_empty (symbolp, template_args))
9608     {
9609       gdb_assert (templ_func != NULL);
9610
9611       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
9612       templ_func->template_arguments
9613         = obstack_alloc (&objfile->objfile_obstack,
9614                          (templ_func->n_template_arguments
9615                           * sizeof (struct symbol *)));
9616       memcpy (templ_func->template_arguments,
9617               VEC_address (symbolp, template_args),
9618               (templ_func->n_template_arguments * sizeof (struct symbol *)));
9619       VEC_free (symbolp, template_args);
9620     }
9621
9622   /* In C++, we can have functions nested inside functions (e.g., when
9623      a function declares a class that has methods).  This means that
9624      when we finish processing a function scope, we may need to go
9625      back to building a containing block's symbol lists.  */
9626   local_symbols = new->locals;
9627   using_directives = new->using_directives;
9628
9629   /* If we've finished processing a top-level function, subsequent
9630      symbols go in the file symbol list.  */
9631   if (outermost_context_p ())
9632     cu->list_in_scope = &file_symbols;
9633 }
9634
9635 /* Process all the DIES contained within a lexical block scope.  Start
9636    a new scope, process the dies, and then close the scope.  */
9637
9638 static void
9639 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
9640 {
9641   struct objfile *objfile = cu->objfile;
9642   struct context_stack *new;
9643   CORE_ADDR lowpc, highpc;
9644   struct die_info *child_die;
9645   CORE_ADDR baseaddr;
9646
9647   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9648
9649   /* Ignore blocks with missing or invalid low and high pc attributes.  */
9650   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
9651      as multiple lexical blocks?  Handling children in a sane way would
9652      be nasty.  Might be easier to properly extend generic blocks to
9653      describe ranges.  */
9654   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9655     return;
9656   lowpc += baseaddr;
9657   highpc += baseaddr;
9658
9659   push_context (0, lowpc);
9660   if (die->child != NULL)
9661     {
9662       child_die = die->child;
9663       while (child_die && child_die->tag)
9664         {
9665           process_die (child_die, cu);
9666           child_die = sibling_die (child_die);
9667         }
9668     }
9669   new = pop_context ();
9670
9671   if (local_symbols != NULL || using_directives != NULL)
9672     {
9673       struct block *block
9674         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
9675                         highpc, objfile);
9676
9677       /* Note that recording ranges after traversing children, as we
9678          do here, means that recording a parent's ranges entails
9679          walking across all its children's ranges as they appear in
9680          the address map, which is quadratic behavior.
9681
9682          It would be nicer to record the parent's ranges before
9683          traversing its children, simply overriding whatever you find
9684          there.  But since we don't even decide whether to create a
9685          block until after we've traversed its children, that's hard
9686          to do.  */
9687       dwarf2_record_block_ranges (die, block, baseaddr, cu);
9688     }
9689   local_symbols = new->locals;
9690   using_directives = new->using_directives;
9691 }
9692
9693 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
9694
9695 static void
9696 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
9697 {
9698   struct objfile *objfile = cu->objfile;
9699   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9700   CORE_ADDR pc, baseaddr;
9701   struct attribute *attr;
9702   struct call_site *call_site, call_site_local;
9703   void **slot;
9704   int nparams;
9705   struct die_info *child_die;
9706
9707   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9708
9709   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9710   if (!attr)
9711     {
9712       complaint (&symfile_complaints,
9713                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
9714                    "DIE 0x%x [in module %s]"),
9715                  die->offset.sect_off, objfile->name);
9716       return;
9717     }
9718   pc = DW_ADDR (attr) + baseaddr;
9719
9720   if (cu->call_site_htab == NULL)
9721     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
9722                                                NULL, &objfile->objfile_obstack,
9723                                                hashtab_obstack_allocate, NULL);
9724   call_site_local.pc = pc;
9725   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
9726   if (*slot != NULL)
9727     {
9728       complaint (&symfile_complaints,
9729                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
9730                    "DIE 0x%x [in module %s]"),
9731                  paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
9732       return;
9733     }
9734
9735   /* Count parameters at the caller.  */
9736
9737   nparams = 0;
9738   for (child_die = die->child; child_die && child_die->tag;
9739        child_die = sibling_die (child_die))
9740     {
9741       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9742         {
9743           complaint (&symfile_complaints,
9744                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
9745                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9746                      child_die->tag, child_die->offset.sect_off, objfile->name);
9747           continue;
9748         }
9749
9750       nparams++;
9751     }
9752
9753   call_site = obstack_alloc (&objfile->objfile_obstack,
9754                              (sizeof (*call_site)
9755                               + (sizeof (*call_site->parameter)
9756                                  * (nparams - 1))));
9757   *slot = call_site;
9758   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
9759   call_site->pc = pc;
9760
9761   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
9762     {
9763       struct die_info *func_die;
9764
9765       /* Skip also over DW_TAG_inlined_subroutine.  */
9766       for (func_die = die->parent;
9767            func_die && func_die->tag != DW_TAG_subprogram
9768            && func_die->tag != DW_TAG_subroutine_type;
9769            func_die = func_die->parent);
9770
9771       /* DW_AT_GNU_all_call_sites is a superset
9772          of DW_AT_GNU_all_tail_call_sites.  */
9773       if (func_die
9774           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
9775           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
9776         {
9777           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
9778              not complete.  But keep CALL_SITE for look ups via call_site_htab,
9779              both the initial caller containing the real return address PC and
9780              the final callee containing the current PC of a chain of tail
9781              calls do not need to have the tail call list complete.  But any
9782              function candidate for a virtual tail call frame searched via
9783              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
9784              determined unambiguously.  */
9785         }
9786       else
9787         {
9788           struct type *func_type = NULL;
9789
9790           if (func_die)
9791             func_type = get_die_type (func_die, cu);
9792           if (func_type != NULL)
9793             {
9794               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9795
9796               /* Enlist this call site to the function.  */
9797               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9798               TYPE_TAIL_CALL_LIST (func_type) = call_site;
9799             }
9800           else
9801             complaint (&symfile_complaints,
9802                        _("Cannot find function owning DW_TAG_GNU_call_site "
9803                          "DIE 0x%x [in module %s]"),
9804                        die->offset.sect_off, objfile->name);
9805         }
9806     }
9807
9808   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9809   if (attr == NULL)
9810     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9811   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9812   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9813     /* Keep NULL DWARF_BLOCK.  */;
9814   else if (attr_form_is_block (attr))
9815     {
9816       struct dwarf2_locexpr_baton *dlbaton;
9817
9818       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9819       dlbaton->data = DW_BLOCK (attr)->data;
9820       dlbaton->size = DW_BLOCK (attr)->size;
9821       dlbaton->per_cu = cu->per_cu;
9822
9823       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9824     }
9825   else if (is_ref_attr (attr))
9826     {
9827       struct dwarf2_cu *target_cu = cu;
9828       struct die_info *target_die;
9829
9830       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9831       gdb_assert (target_cu->objfile == objfile);
9832       if (die_is_declaration (target_die, target_cu))
9833         {
9834           const char *target_physname = NULL;
9835           struct attribute *target_attr;
9836
9837           /* Prefer the mangled name; otherwise compute the demangled one.  */
9838           target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
9839           if (target_attr == NULL)
9840             target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
9841                                        target_cu);
9842           if (target_attr != NULL && DW_STRING (target_attr) != NULL)
9843             target_physname = DW_STRING (target_attr);
9844           else
9845             target_physname = dwarf2_physname (NULL, target_die, target_cu);
9846           if (target_physname == NULL)
9847             complaint (&symfile_complaints,
9848                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9849                          "physname, for referencing DIE 0x%x [in module %s]"),
9850                        die->offset.sect_off, objfile->name);
9851           else
9852             SET_FIELD_PHYSNAME (call_site->target, target_physname);
9853         }
9854       else
9855         {
9856           CORE_ADDR lowpc;
9857
9858           /* DW_AT_entry_pc should be preferred.  */
9859           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9860             complaint (&symfile_complaints,
9861                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9862                          "low pc, for referencing DIE 0x%x [in module %s]"),
9863                        die->offset.sect_off, objfile->name);
9864           else
9865             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9866         }
9867     }
9868   else
9869     complaint (&symfile_complaints,
9870                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9871                  "block nor reference, for DIE 0x%x [in module %s]"),
9872                die->offset.sect_off, objfile->name);
9873
9874   call_site->per_cu = cu->per_cu;
9875
9876   for (child_die = die->child;
9877        child_die && child_die->tag;
9878        child_die = sibling_die (child_die))
9879     {
9880       struct call_site_parameter *parameter;
9881       struct attribute *loc, *origin;
9882
9883       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9884         {
9885           /* Already printed the complaint above.  */
9886           continue;
9887         }
9888
9889       gdb_assert (call_site->parameter_count < nparams);
9890       parameter = &call_site->parameter[call_site->parameter_count];
9891
9892       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9893          specifies DW_TAG_formal_parameter.  Value of the data assumed for the
9894          register is contained in DW_AT_GNU_call_site_value.  */
9895
9896       loc = dwarf2_attr (child_die, DW_AT_location, cu);
9897       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9898       if (loc == NULL && origin != NULL && is_ref_attr (origin))
9899         {
9900           sect_offset offset;
9901
9902           parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9903           offset = dwarf2_get_ref_die_offset (origin);
9904           if (!offset_in_cu_p (&cu->header, offset))
9905             {
9906               /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9907                  binding can be done only inside one CU.  Such referenced DIE
9908                  therefore cannot be even moved to DW_TAG_partial_unit.  */
9909               complaint (&symfile_complaints,
9910                          _("DW_AT_abstract_origin offset is not in CU for "
9911                            "DW_TAG_GNU_call_site child DIE 0x%x "
9912                            "[in module %s]"),
9913                          child_die->offset.sect_off, objfile->name);
9914               continue;
9915             }
9916           parameter->u.param_offset.cu_off = (offset.sect_off
9917                                               - cu->header.offset.sect_off);
9918         }
9919       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9920         {
9921           complaint (&symfile_complaints,
9922                      _("No DW_FORM_block* DW_AT_location for "
9923                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9924                      child_die->offset.sect_off, objfile->name);
9925           continue;
9926         }
9927       else
9928         {
9929           parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9930             (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9931           if (parameter->u.dwarf_reg != -1)
9932             parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9933           else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9934                                     &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9935                                              &parameter->u.fb_offset))
9936             parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9937           else
9938             {
9939               complaint (&symfile_complaints,
9940                          _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9941                            "for DW_FORM_block* DW_AT_location is supported for "
9942                            "DW_TAG_GNU_call_site child DIE 0x%x "
9943                            "[in module %s]"),
9944                          child_die->offset.sect_off, objfile->name);
9945               continue;
9946             }
9947         }
9948
9949       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9950       if (!attr_form_is_block (attr))
9951         {
9952           complaint (&symfile_complaints,
9953                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9954                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9955                      child_die->offset.sect_off, objfile->name);
9956           continue;
9957         }
9958       parameter->value = DW_BLOCK (attr)->data;
9959       parameter->value_size = DW_BLOCK (attr)->size;
9960
9961       /* Parameters are not pre-cleared by memset above.  */
9962       parameter->data_value = NULL;
9963       parameter->data_value_size = 0;
9964       call_site->parameter_count++;
9965
9966       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9967       if (attr)
9968         {
9969           if (!attr_form_is_block (attr))
9970             complaint (&symfile_complaints,
9971                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9972                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9973                        child_die->offset.sect_off, objfile->name);
9974           else
9975             {
9976               parameter->data_value = DW_BLOCK (attr)->data;
9977               parameter->data_value_size = DW_BLOCK (attr)->size;
9978             }
9979         }
9980     }
9981 }
9982
9983 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9984    Return 1 if the attributes are present and valid, otherwise, return 0.
9985    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
9986
9987 static int
9988 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9989                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
9990                     struct partial_symtab *ranges_pst)
9991 {
9992   struct objfile *objfile = cu->objfile;
9993   struct comp_unit_head *cu_header = &cu->header;
9994   bfd *obfd = objfile->obfd;
9995   unsigned int addr_size = cu_header->addr_size;
9996   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9997   /* Base address selection entry.  */
9998   CORE_ADDR base;
9999   int found_base;
10000   unsigned int dummy;
10001   gdb_byte *buffer;
10002   CORE_ADDR marker;
10003   int low_set;
10004   CORE_ADDR low = 0;
10005   CORE_ADDR high = 0;
10006   CORE_ADDR baseaddr;
10007
10008   found_base = cu->base_known;
10009   base = cu->base_address;
10010
10011   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10012   if (offset >= dwarf2_per_objfile->ranges.size)
10013     {
10014       complaint (&symfile_complaints,
10015                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
10016                  offset);
10017       return 0;
10018     }
10019   buffer = dwarf2_per_objfile->ranges.buffer + offset;
10020
10021   /* Read in the largest possible address.  */
10022   marker = read_address (obfd, buffer, cu, &dummy);
10023   if ((marker & mask) == mask)
10024     {
10025       /* If we found the largest possible address, then
10026          read the base address.  */
10027       base = read_address (obfd, buffer + addr_size, cu, &dummy);
10028       buffer += 2 * addr_size;
10029       offset += 2 * addr_size;
10030       found_base = 1;
10031     }
10032
10033   low_set = 0;
10034
10035   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10036
10037   while (1)
10038     {
10039       CORE_ADDR range_beginning, range_end;
10040
10041       range_beginning = read_address (obfd, buffer, cu, &dummy);
10042       buffer += addr_size;
10043       range_end = read_address (obfd, buffer, cu, &dummy);
10044       buffer += addr_size;
10045       offset += 2 * addr_size;
10046
10047       /* An end of list marker is a pair of zero addresses.  */
10048       if (range_beginning == 0 && range_end == 0)
10049         /* Found the end of list entry.  */
10050         break;
10051
10052       /* Each base address selection entry is a pair of 2 values.
10053          The first is the largest possible address, the second is
10054          the base address.  Check for a base address here.  */
10055       if ((range_beginning & mask) == mask)
10056         {
10057           /* If we found the largest possible address, then
10058              read the base address.  */
10059           base = read_address (obfd, buffer + addr_size, cu, &dummy);
10060           found_base = 1;
10061           continue;
10062         }
10063
10064       if (!found_base)
10065         {
10066           /* We have no valid base address for the ranges
10067              data.  */
10068           complaint (&symfile_complaints,
10069                      _("Invalid .debug_ranges data (no base address)"));
10070           return 0;
10071         }
10072
10073       if (range_beginning > range_end)
10074         {
10075           /* Inverted range entries are invalid.  */
10076           complaint (&symfile_complaints,
10077                      _("Invalid .debug_ranges data (inverted range)"));
10078           return 0;
10079         }
10080
10081       /* Empty range entries have no effect.  */
10082       if (range_beginning == range_end)
10083         continue;
10084
10085       range_beginning += base;
10086       range_end += base;
10087
10088       /* A not-uncommon case of bad debug info.
10089          Don't pollute the addrmap with bad data.  */
10090       if (range_beginning + baseaddr == 0
10091           && !dwarf2_per_objfile->has_section_at_zero)
10092         {
10093           complaint (&symfile_complaints,
10094                      _(".debug_ranges entry has start address of zero"
10095                        " [in module %s]"), objfile->name);
10096           continue;
10097         }
10098
10099       if (ranges_pst != NULL)
10100         addrmap_set_empty (objfile->psymtabs_addrmap,
10101                            range_beginning + baseaddr,
10102                            range_end - 1 + baseaddr,
10103                            ranges_pst);
10104
10105       /* FIXME: This is recording everything as a low-high
10106          segment of consecutive addresses.  We should have a
10107          data structure for discontiguous block ranges
10108          instead.  */
10109       if (! low_set)
10110         {
10111           low = range_beginning;
10112           high = range_end;
10113           low_set = 1;
10114         }
10115       else
10116         {
10117           if (range_beginning < low)
10118             low = range_beginning;
10119           if (range_end > high)
10120             high = range_end;
10121         }
10122     }
10123
10124   if (! low_set)
10125     /* If the first entry is an end-of-list marker, the range
10126        describes an empty scope, i.e. no instructions.  */
10127     return 0;
10128
10129   if (low_return)
10130     *low_return = low;
10131   if (high_return)
10132     *high_return = high;
10133   return 1;
10134 }
10135
10136 /* Get low and high pc attributes from a die.  Return 1 if the attributes
10137    are present and valid, otherwise, return 0.  Return -1 if the range is
10138    discontinuous, i.e. derived from DW_AT_ranges information.  */
10139
10140 static int
10141 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10142                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
10143                       struct partial_symtab *pst)
10144 {
10145   struct attribute *attr;
10146   struct attribute *attr_high;
10147   CORE_ADDR low = 0;
10148   CORE_ADDR high = 0;
10149   int ret = 0;
10150
10151   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10152   if (attr_high)
10153     {
10154       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10155       if (attr)
10156         {
10157           low = DW_ADDR (attr);
10158           if (attr_high->form == DW_FORM_addr
10159               || attr_high->form == DW_FORM_GNU_addr_index)
10160             high = DW_ADDR (attr_high);
10161           else
10162             high = low + DW_UNSND (attr_high);
10163         }
10164       else
10165         /* Found high w/o low attribute.  */
10166         return 0;
10167
10168       /* Found consecutive range of addresses.  */
10169       ret = 1;
10170     }
10171   else
10172     {
10173       attr = dwarf2_attr (die, DW_AT_ranges, cu);
10174       if (attr != NULL)
10175         {
10176           /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10177              We take advantage of the fact that DW_AT_ranges does not appear
10178              in DW_TAG_compile_unit of DWO files.  */
10179           int need_ranges_base = die->tag != DW_TAG_compile_unit;
10180           unsigned int ranges_offset = (DW_UNSND (attr)
10181                                         + (need_ranges_base
10182                                            ? cu->ranges_base
10183                                            : 0));
10184
10185           /* Value of the DW_AT_ranges attribute is the offset in the
10186              .debug_ranges section.  */
10187           if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10188             return 0;
10189           /* Found discontinuous range of addresses.  */
10190           ret = -1;
10191         }
10192     }
10193
10194   /* read_partial_die has also the strict LOW < HIGH requirement.  */
10195   if (high <= low)
10196     return 0;
10197
10198   /* When using the GNU linker, .gnu.linkonce. sections are used to
10199      eliminate duplicate copies of functions and vtables and such.
10200      The linker will arbitrarily choose one and discard the others.
10201      The AT_*_pc values for such functions refer to local labels in
10202      these sections.  If the section from that file was discarded, the
10203      labels are not in the output, so the relocs get a value of 0.
10204      If this is a discarded function, mark the pc bounds as invalid,
10205      so that GDB will ignore it.  */
10206   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10207     return 0;
10208
10209   *lowpc = low;
10210   if (highpc)
10211     *highpc = high;
10212   return ret;
10213 }
10214
10215 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10216    its low and high PC addresses.  Do nothing if these addresses could not
10217    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
10218    and HIGHPC to the high address if greater than HIGHPC.  */
10219
10220 static void
10221 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10222                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
10223                                  struct dwarf2_cu *cu)
10224 {
10225   CORE_ADDR low, high;
10226   struct die_info *child = die->child;
10227
10228   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10229     {
10230       *lowpc = min (*lowpc, low);
10231       *highpc = max (*highpc, high);
10232     }
10233
10234   /* If the language does not allow nested subprograms (either inside
10235      subprograms or lexical blocks), we're done.  */
10236   if (cu->language != language_ada)
10237     return;
10238
10239   /* Check all the children of the given DIE.  If it contains nested
10240      subprograms, then check their pc bounds.  Likewise, we need to
10241      check lexical blocks as well, as they may also contain subprogram
10242      definitions.  */
10243   while (child && child->tag)
10244     {
10245       if (child->tag == DW_TAG_subprogram
10246           || child->tag == DW_TAG_lexical_block)
10247         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10248       child = sibling_die (child);
10249     }
10250 }
10251
10252 /* Get the low and high pc's represented by the scope DIE, and store
10253    them in *LOWPC and *HIGHPC.  If the correct values can't be
10254    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
10255
10256 static void
10257 get_scope_pc_bounds (struct die_info *die,
10258                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
10259                      struct dwarf2_cu *cu)
10260 {
10261   CORE_ADDR best_low = (CORE_ADDR) -1;
10262   CORE_ADDR best_high = (CORE_ADDR) 0;
10263   CORE_ADDR current_low, current_high;
10264
10265   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10266     {
10267       best_low = current_low;
10268       best_high = current_high;
10269     }
10270   else
10271     {
10272       struct die_info *child = die->child;
10273
10274       while (child && child->tag)
10275         {
10276           switch (child->tag) {
10277           case DW_TAG_subprogram:
10278             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10279             break;
10280           case DW_TAG_namespace:
10281           case DW_TAG_module:
10282             /* FIXME: carlton/2004-01-16: Should we do this for
10283                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
10284                that current GCC's always emit the DIEs corresponding
10285                to definitions of methods of classes as children of a
10286                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10287                the DIEs giving the declarations, which could be
10288                anywhere).  But I don't see any reason why the
10289                standards says that they have to be there.  */
10290             get_scope_pc_bounds (child, &current_low, &current_high, cu);
10291
10292             if (current_low != ((CORE_ADDR) -1))
10293               {
10294                 best_low = min (best_low, current_low);
10295                 best_high = max (best_high, current_high);
10296               }
10297             break;
10298           default:
10299             /* Ignore.  */
10300             break;
10301           }
10302
10303           child = sibling_die (child);
10304         }
10305     }
10306
10307   *lowpc = best_low;
10308   *highpc = best_high;
10309 }
10310
10311 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10312    in DIE.  */
10313
10314 static void
10315 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10316                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10317 {
10318   struct objfile *objfile = cu->objfile;
10319   struct attribute *attr;
10320   struct attribute *attr_high;
10321
10322   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10323   if (attr_high)
10324     {
10325       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10326       if (attr)
10327         {
10328           CORE_ADDR low = DW_ADDR (attr);
10329           CORE_ADDR high;
10330           if (attr_high->form == DW_FORM_addr
10331               || attr_high->form == DW_FORM_GNU_addr_index)
10332             high = DW_ADDR (attr_high);
10333           else
10334             high = low + DW_UNSND (attr_high);
10335
10336           record_block_range (block, baseaddr + low, baseaddr + high - 1);
10337         }
10338     }
10339
10340   attr = dwarf2_attr (die, DW_AT_ranges, cu);
10341   if (attr)
10342     {
10343       bfd *obfd = objfile->obfd;
10344       /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10345          We take advantage of the fact that DW_AT_ranges does not appear
10346          in DW_TAG_compile_unit of DWO files.  */
10347       int need_ranges_base = die->tag != DW_TAG_compile_unit;
10348
10349       /* The value of the DW_AT_ranges attribute is the offset of the
10350          address range list in the .debug_ranges section.  */
10351       unsigned long offset = (DW_UNSND (attr)
10352                               + (need_ranges_base ? cu->ranges_base : 0));
10353       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
10354
10355       /* For some target architectures, but not others, the
10356          read_address function sign-extends the addresses it returns.
10357          To recognize base address selection entries, we need a
10358          mask.  */
10359       unsigned int addr_size = cu->header.addr_size;
10360       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10361
10362       /* The base address, to which the next pair is relative.  Note
10363          that this 'base' is a DWARF concept: most entries in a range
10364          list are relative, to reduce the number of relocs against the
10365          debugging information.  This is separate from this function's
10366          'baseaddr' argument, which GDB uses to relocate debugging
10367          information from a shared library based on the address at
10368          which the library was loaded.  */
10369       CORE_ADDR base = cu->base_address;
10370       int base_known = cu->base_known;
10371
10372       gdb_assert (dwarf2_per_objfile->ranges.readin);
10373       if (offset >= dwarf2_per_objfile->ranges.size)
10374         {
10375           complaint (&symfile_complaints,
10376                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10377                      offset);
10378           return;
10379         }
10380
10381       for (;;)
10382         {
10383           unsigned int bytes_read;
10384           CORE_ADDR start, end;
10385
10386           start = read_address (obfd, buffer, cu, &bytes_read);
10387           buffer += bytes_read;
10388           end = read_address (obfd, buffer, cu, &bytes_read);
10389           buffer += bytes_read;
10390
10391           /* Did we find the end of the range list?  */
10392           if (start == 0 && end == 0)
10393             break;
10394
10395           /* Did we find a base address selection entry?  */
10396           else if ((start & base_select_mask) == base_select_mask)
10397             {
10398               base = end;
10399               base_known = 1;
10400             }
10401
10402           /* We found an ordinary address range.  */
10403           else
10404             {
10405               if (!base_known)
10406                 {
10407                   complaint (&symfile_complaints,
10408                              _("Invalid .debug_ranges data "
10409                                "(no base address)"));
10410                   return;
10411                 }
10412
10413               if (start > end)
10414                 {
10415                   /* Inverted range entries are invalid.  */
10416                   complaint (&symfile_complaints,
10417                              _("Invalid .debug_ranges data "
10418                                "(inverted range)"));
10419                   return;
10420                 }
10421
10422               /* Empty range entries have no effect.  */
10423               if (start == end)
10424                 continue;
10425
10426               start += base + baseaddr;
10427               end += base + baseaddr;
10428
10429               /* A not-uncommon case of bad debug info.
10430                  Don't pollute the addrmap with bad data.  */
10431               if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10432                 {
10433                   complaint (&symfile_complaints,
10434                              _(".debug_ranges entry has start address of zero"
10435                                " [in module %s]"), objfile->name);
10436                   continue;
10437                 }
10438
10439               record_block_range (block, start, end - 1);
10440             }
10441         }
10442     }
10443 }
10444
10445 /* Check whether the producer field indicates either of GCC < 4.6, or the
10446    Intel C/C++ compiler, and cache the result in CU.  */
10447
10448 static void
10449 check_producer (struct dwarf2_cu *cu)
10450 {
10451   const char *cs;
10452   int major, minor, release;
10453
10454   if (cu->producer == NULL)
10455     {
10456       /* For unknown compilers expect their behavior is DWARF version
10457          compliant.
10458
10459          GCC started to support .debug_types sections by -gdwarf-4 since
10460          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
10461          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10462          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10463          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
10464     }
10465   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10466     {
10467       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
10468
10469       cs = &cu->producer[strlen ("GNU ")];
10470       while (*cs && !isdigit (*cs))
10471         cs++;
10472       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10473         {
10474           /* Not recognized as GCC.  */
10475         }
10476       else
10477         {
10478           cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10479           cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10480         }
10481     }
10482   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10483     cu->producer_is_icc = 1;
10484   else
10485     {
10486       /* For other non-GCC compilers, expect their behavior is DWARF version
10487          compliant.  */
10488     }
10489
10490   cu->checked_producer = 1;
10491 }
10492
10493 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10494    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10495    during 4.6.0 experimental.  */
10496
10497 static int
10498 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10499 {
10500   if (!cu->checked_producer)
10501     check_producer (cu);
10502
10503   return cu->producer_is_gxx_lt_4_6;
10504 }
10505
10506 /* Return the default accessibility type if it is not overriden by
10507    DW_AT_accessibility.  */
10508
10509 static enum dwarf_access_attribute
10510 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10511 {
10512   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
10513     {
10514       /* The default DWARF 2 accessibility for members is public, the default
10515          accessibility for inheritance is private.  */
10516
10517       if (die->tag != DW_TAG_inheritance)
10518         return DW_ACCESS_public;
10519       else
10520         return DW_ACCESS_private;
10521     }
10522   else
10523     {
10524       /* DWARF 3+ defines the default accessibility a different way.  The same
10525          rules apply now for DW_TAG_inheritance as for the members and it only
10526          depends on the container kind.  */
10527
10528       if (die->parent->tag == DW_TAG_class_type)
10529         return DW_ACCESS_private;
10530       else
10531         return DW_ACCESS_public;
10532     }
10533 }
10534
10535 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
10536    offset.  If the attribute was not found return 0, otherwise return
10537    1.  If it was found but could not properly be handled, set *OFFSET
10538    to 0.  */
10539
10540 static int
10541 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
10542                              LONGEST *offset)
10543 {
10544   struct attribute *attr;
10545
10546   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
10547   if (attr != NULL)
10548     {
10549       *offset = 0;
10550
10551       /* Note that we do not check for a section offset first here.
10552          This is because DW_AT_data_member_location is new in DWARF 4,
10553          so if we see it, we can assume that a constant form is really
10554          a constant and not a section offset.  */
10555       if (attr_form_is_constant (attr))
10556         *offset = dwarf2_get_attr_constant_value (attr, 0);
10557       else if (attr_form_is_section_offset (attr))
10558         dwarf2_complex_location_expr_complaint ();
10559       else if (attr_form_is_block (attr))
10560         *offset = decode_locdesc (DW_BLOCK (attr), cu);
10561       else
10562         dwarf2_complex_location_expr_complaint ();
10563
10564       return 1;
10565     }
10566
10567   return 0;
10568 }
10569
10570 /* Add an aggregate field to the field list.  */
10571
10572 static void
10573 dwarf2_add_field (struct field_info *fip, struct die_info *die,
10574                   struct dwarf2_cu *cu)
10575 {
10576   struct objfile *objfile = cu->objfile;
10577   struct gdbarch *gdbarch = get_objfile_arch (objfile);
10578   struct nextfield *new_field;
10579   struct attribute *attr;
10580   struct field *fp;
10581   const char *fieldname = "";
10582
10583   /* Allocate a new field list entry and link it in.  */
10584   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
10585   make_cleanup (xfree, new_field);
10586   memset (new_field, 0, sizeof (struct nextfield));
10587
10588   if (die->tag == DW_TAG_inheritance)
10589     {
10590       new_field->next = fip->baseclasses;
10591       fip->baseclasses = new_field;
10592     }
10593   else
10594     {
10595       new_field->next = fip->fields;
10596       fip->fields = new_field;
10597     }
10598   fip->nfields++;
10599
10600   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10601   if (attr)
10602     new_field->accessibility = DW_UNSND (attr);
10603   else
10604     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
10605   if (new_field->accessibility != DW_ACCESS_public)
10606     fip->non_public_fields = 1;
10607
10608   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10609   if (attr)
10610     new_field->virtuality = DW_UNSND (attr);
10611   else
10612     new_field->virtuality = DW_VIRTUALITY_none;
10613
10614   fp = &new_field->field;
10615
10616   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
10617     {
10618       LONGEST offset;
10619
10620       /* Data member other than a C++ static data member.  */
10621
10622       /* Get type of field.  */
10623       fp->type = die_type (die, cu);
10624
10625       SET_FIELD_BITPOS (*fp, 0);
10626
10627       /* Get bit size of field (zero if none).  */
10628       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10629       if (attr)
10630         {
10631           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
10632         }
10633       else
10634         {
10635           FIELD_BITSIZE (*fp) = 0;
10636         }
10637
10638       /* Get bit offset of field.  */
10639       if (handle_data_member_location (die, cu, &offset))
10640         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10641       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
10642       if (attr)
10643         {
10644           if (gdbarch_bits_big_endian (gdbarch))
10645             {
10646               /* For big endian bits, the DW_AT_bit_offset gives the
10647                  additional bit offset from the MSB of the containing
10648                  anonymous object to the MSB of the field.  We don't
10649                  have to do anything special since we don't need to
10650                  know the size of the anonymous object.  */
10651               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
10652             }
10653           else
10654             {
10655               /* For little endian bits, compute the bit offset to the
10656                  MSB of the anonymous object, subtract off the number of
10657                  bits from the MSB of the field to the MSB of the
10658                  object, and then subtract off the number of bits of
10659                  the field itself.  The result is the bit offset of
10660                  the LSB of the field.  */
10661               int anonymous_size;
10662               int bit_offset = DW_UNSND (attr);
10663
10664               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10665               if (attr)
10666                 {
10667                   /* The size of the anonymous object containing
10668                      the bit field is explicit, so use the
10669                      indicated size (in bytes).  */
10670                   anonymous_size = DW_UNSND (attr);
10671                 }
10672               else
10673                 {
10674                   /* The size of the anonymous object containing
10675                      the bit field must be inferred from the type
10676                      attribute of the data member containing the
10677                      bit field.  */
10678                   anonymous_size = TYPE_LENGTH (fp->type);
10679                 }
10680               SET_FIELD_BITPOS (*fp,
10681                                 (FIELD_BITPOS (*fp)
10682                                  + anonymous_size * bits_per_byte
10683                                  - bit_offset - FIELD_BITSIZE (*fp)));
10684             }
10685         }
10686
10687       /* Get name of field.  */
10688       fieldname = dwarf2_name (die, cu);
10689       if (fieldname == NULL)
10690         fieldname = "";
10691
10692       /* The name is already allocated along with this objfile, so we don't
10693          need to duplicate it for the type.  */
10694       fp->name = fieldname;
10695
10696       /* Change accessibility for artificial fields (e.g. virtual table
10697          pointer or virtual base class pointer) to private.  */
10698       if (dwarf2_attr (die, DW_AT_artificial, cu))
10699         {
10700           FIELD_ARTIFICIAL (*fp) = 1;
10701           new_field->accessibility = DW_ACCESS_private;
10702           fip->non_public_fields = 1;
10703         }
10704     }
10705   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
10706     {
10707       /* C++ static member.  */
10708
10709       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
10710          is a declaration, but all versions of G++ as of this writing
10711          (so through at least 3.2.1) incorrectly generate
10712          DW_TAG_variable tags.  */
10713
10714       const char *physname;
10715
10716       /* Get name of field.  */
10717       fieldname = dwarf2_name (die, cu);
10718       if (fieldname == NULL)
10719         return;
10720
10721       attr = dwarf2_attr (die, DW_AT_const_value, cu);
10722       if (attr
10723           /* Only create a symbol if this is an external value.
10724              new_symbol checks this and puts the value in the global symbol
10725              table, which we want.  If it is not external, new_symbol
10726              will try to put the value in cu->list_in_scope which is wrong.  */
10727           && dwarf2_flag_true_p (die, DW_AT_external, cu))
10728         {
10729           /* A static const member, not much different than an enum as far as
10730              we're concerned, except that we can support more types.  */
10731           new_symbol (die, NULL, cu);
10732         }
10733
10734       /* Get physical name.  */
10735       physname = dwarf2_physname (fieldname, die, cu);
10736
10737       /* The name is already allocated along with this objfile, so we don't
10738          need to duplicate it for the type.  */
10739       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
10740       FIELD_TYPE (*fp) = die_type (die, cu);
10741       FIELD_NAME (*fp) = fieldname;
10742     }
10743   else if (die->tag == DW_TAG_inheritance)
10744     {
10745       LONGEST offset;
10746
10747       /* C++ base class field.  */
10748       if (handle_data_member_location (die, cu, &offset))
10749         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
10750       FIELD_BITSIZE (*fp) = 0;
10751       FIELD_TYPE (*fp) = die_type (die, cu);
10752       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
10753       fip->nbaseclasses++;
10754     }
10755 }
10756
10757 /* Add a typedef defined in the scope of the FIP's class.  */
10758
10759 static void
10760 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
10761                     struct dwarf2_cu *cu)
10762 {
10763   struct objfile *objfile = cu->objfile;
10764   struct typedef_field_list *new_field;
10765   struct attribute *attr;
10766   struct typedef_field *fp;
10767   char *fieldname = "";
10768
10769   /* Allocate a new field list entry and link it in.  */
10770   new_field = xzalloc (sizeof (*new_field));
10771   make_cleanup (xfree, new_field);
10772
10773   gdb_assert (die->tag == DW_TAG_typedef);
10774
10775   fp = &new_field->field;
10776
10777   /* Get name of field.  */
10778   fp->name = dwarf2_name (die, cu);
10779   if (fp->name == NULL)
10780     return;
10781
10782   fp->type = read_type_die (die, cu);
10783
10784   new_field->next = fip->typedef_field_list;
10785   fip->typedef_field_list = new_field;
10786   fip->typedef_field_list_count++;
10787 }
10788
10789 /* Create the vector of fields, and attach it to the type.  */
10790
10791 static void
10792 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
10793                               struct dwarf2_cu *cu)
10794 {
10795   int nfields = fip->nfields;
10796
10797   /* Record the field count, allocate space for the array of fields,
10798      and create blank accessibility bitfields if necessary.  */
10799   TYPE_NFIELDS (type) = nfields;
10800   TYPE_FIELDS (type) = (struct field *)
10801     TYPE_ALLOC (type, sizeof (struct field) * nfields);
10802   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
10803
10804   if (fip->non_public_fields && cu->language != language_ada)
10805     {
10806       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10807
10808       TYPE_FIELD_PRIVATE_BITS (type) =
10809         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10810       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
10811
10812       TYPE_FIELD_PROTECTED_BITS (type) =
10813         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10814       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10815
10816       TYPE_FIELD_IGNORE_BITS (type) =
10817         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10818       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10819     }
10820
10821   /* If the type has baseclasses, allocate and clear a bit vector for
10822      TYPE_FIELD_VIRTUAL_BITS.  */
10823   if (fip->nbaseclasses && cu->language != language_ada)
10824     {
10825       int num_bytes = B_BYTES (fip->nbaseclasses);
10826       unsigned char *pointer;
10827
10828       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10829       pointer = TYPE_ALLOC (type, num_bytes);
10830       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10831       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10832       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10833     }
10834
10835   /* Copy the saved-up fields into the field vector.  Start from the head of
10836      the list, adding to the tail of the field array, so that they end up in
10837      the same order in the array in which they were added to the list.  */
10838   while (nfields-- > 0)
10839     {
10840       struct nextfield *fieldp;
10841
10842       if (fip->fields)
10843         {
10844           fieldp = fip->fields;
10845           fip->fields = fieldp->next;
10846         }
10847       else
10848         {
10849           fieldp = fip->baseclasses;
10850           fip->baseclasses = fieldp->next;
10851         }
10852
10853       TYPE_FIELD (type, nfields) = fieldp->field;
10854       switch (fieldp->accessibility)
10855         {
10856         case DW_ACCESS_private:
10857           if (cu->language != language_ada)
10858             SET_TYPE_FIELD_PRIVATE (type, nfields);
10859           break;
10860
10861         case DW_ACCESS_protected:
10862           if (cu->language != language_ada)
10863             SET_TYPE_FIELD_PROTECTED (type, nfields);
10864           break;
10865
10866         case DW_ACCESS_public:
10867           break;
10868
10869         default:
10870           /* Unknown accessibility.  Complain and treat it as public.  */
10871           {
10872             complaint (&symfile_complaints, _("unsupported accessibility %d"),
10873                        fieldp->accessibility);
10874           }
10875           break;
10876         }
10877       if (nfields < fip->nbaseclasses)
10878         {
10879           switch (fieldp->virtuality)
10880             {
10881             case DW_VIRTUALITY_virtual:
10882             case DW_VIRTUALITY_pure_virtual:
10883               if (cu->language == language_ada)
10884                 error (_("unexpected virtuality in component of Ada type"));
10885               SET_TYPE_FIELD_VIRTUAL (type, nfields);
10886               break;
10887             }
10888         }
10889     }
10890 }
10891
10892 /* Return true if this member function is a constructor, false
10893    otherwise.  */
10894
10895 static int
10896 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
10897 {
10898   const char *fieldname;
10899   const char *typename;
10900   int len;
10901
10902   if (die->parent == NULL)
10903     return 0;
10904
10905   if (die->parent->tag != DW_TAG_structure_type
10906       && die->parent->tag != DW_TAG_union_type
10907       && die->parent->tag != DW_TAG_class_type)
10908     return 0;
10909
10910   fieldname = dwarf2_name (die, cu);
10911   typename = dwarf2_name (die->parent, cu);
10912   if (fieldname == NULL || typename == NULL)
10913     return 0;
10914
10915   len = strlen (fieldname);
10916   return (strncmp (fieldname, typename, len) == 0
10917           && (typename[len] == '\0' || typename[len] == '<'));
10918 }
10919
10920 /* Add a member function to the proper fieldlist.  */
10921
10922 static void
10923 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10924                       struct type *type, struct dwarf2_cu *cu)
10925 {
10926   struct objfile *objfile = cu->objfile;
10927   struct attribute *attr;
10928   struct fnfieldlist *flp;
10929   int i;
10930   struct fn_field *fnp;
10931   const char *fieldname;
10932   struct nextfnfield *new_fnfield;
10933   struct type *this_type;
10934   enum dwarf_access_attribute accessibility;
10935
10936   if (cu->language == language_ada)
10937     error (_("unexpected member function in Ada type"));
10938
10939   /* Get name of member function.  */
10940   fieldname = dwarf2_name (die, cu);
10941   if (fieldname == NULL)
10942     return;
10943
10944   /* Look up member function name in fieldlist.  */
10945   for (i = 0; i < fip->nfnfields; i++)
10946     {
10947       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10948         break;
10949     }
10950
10951   /* Create new list element if necessary.  */
10952   if (i < fip->nfnfields)
10953     flp = &fip->fnfieldlists[i];
10954   else
10955     {
10956       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10957         {
10958           fip->fnfieldlists = (struct fnfieldlist *)
10959             xrealloc (fip->fnfieldlists,
10960                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10961                       * sizeof (struct fnfieldlist));
10962           if (fip->nfnfields == 0)
10963             make_cleanup (free_current_contents, &fip->fnfieldlists);
10964         }
10965       flp = &fip->fnfieldlists[fip->nfnfields];
10966       flp->name = fieldname;
10967       flp->length = 0;
10968       flp->head = NULL;
10969       i = fip->nfnfields++;
10970     }
10971
10972   /* Create a new member function field and chain it to the field list
10973      entry.  */
10974   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10975   make_cleanup (xfree, new_fnfield);
10976   memset (new_fnfield, 0, sizeof (struct nextfnfield));
10977   new_fnfield->next = flp->head;
10978   flp->head = new_fnfield;
10979   flp->length++;
10980
10981   /* Fill in the member function field info.  */
10982   fnp = &new_fnfield->fnfield;
10983
10984   /* Delay processing of the physname until later.  */
10985   if (cu->language == language_cplus || cu->language == language_java)
10986     {
10987       add_to_method_list (type, i, flp->length - 1, fieldname,
10988                           die, cu);
10989     }
10990   else
10991     {
10992       const char *physname = dwarf2_physname (fieldname, die, cu);
10993       fnp->physname = physname ? physname : "";
10994     }
10995
10996   fnp->type = alloc_type (objfile);
10997   this_type = read_type_die (die, cu);
10998   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10999     {
11000       int nparams = TYPE_NFIELDS (this_type);
11001
11002       /* TYPE is the domain of this method, and THIS_TYPE is the type
11003            of the method itself (TYPE_CODE_METHOD).  */
11004       smash_to_method_type (fnp->type, type,
11005                             TYPE_TARGET_TYPE (this_type),
11006                             TYPE_FIELDS (this_type),
11007                             TYPE_NFIELDS (this_type),
11008                             TYPE_VARARGS (this_type));
11009
11010       /* Handle static member functions.
11011          Dwarf2 has no clean way to discern C++ static and non-static
11012          member functions.  G++ helps GDB by marking the first
11013          parameter for non-static member functions (which is the this
11014          pointer) as artificial.  We obtain this information from
11015          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
11016       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11017         fnp->voffset = VOFFSET_STATIC;
11018     }
11019   else
11020     complaint (&symfile_complaints, _("member function type missing for '%s'"),
11021                dwarf2_full_name (fieldname, die, cu));
11022
11023   /* Get fcontext from DW_AT_containing_type if present.  */
11024   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11025     fnp->fcontext = die_containing_type (die, cu);
11026
11027   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11028      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
11029
11030   /* Get accessibility.  */
11031   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11032   if (attr)
11033     accessibility = DW_UNSND (attr);
11034   else
11035     accessibility = dwarf2_default_access_attribute (die, cu);
11036   switch (accessibility)
11037     {
11038     case DW_ACCESS_private:
11039       fnp->is_private = 1;
11040       break;
11041     case DW_ACCESS_protected:
11042       fnp->is_protected = 1;
11043       break;
11044     }
11045
11046   /* Check for artificial methods.  */
11047   attr = dwarf2_attr (die, DW_AT_artificial, cu);
11048   if (attr && DW_UNSND (attr) != 0)
11049     fnp->is_artificial = 1;
11050
11051   fnp->is_constructor = dwarf2_is_constructor (die, cu);
11052
11053   /* Get index in virtual function table if it is a virtual member
11054      function.  For older versions of GCC, this is an offset in the
11055      appropriate virtual table, as specified by DW_AT_containing_type.
11056      For everyone else, it is an expression to be evaluated relative
11057      to the object address.  */
11058
11059   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11060   if (attr)
11061     {
11062       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11063         {
11064           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11065             {
11066               /* Old-style GCC.  */
11067               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11068             }
11069           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11070                    || (DW_BLOCK (attr)->size > 1
11071                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11072                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11073             {
11074               struct dwarf_block blk;
11075               int offset;
11076
11077               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11078                         ? 1 : 2);
11079               blk.size = DW_BLOCK (attr)->size - offset;
11080               blk.data = DW_BLOCK (attr)->data + offset;
11081               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11082               if ((fnp->voffset % cu->header.addr_size) != 0)
11083                 dwarf2_complex_location_expr_complaint ();
11084               else
11085                 fnp->voffset /= cu->header.addr_size;
11086               fnp->voffset += 2;
11087             }
11088           else
11089             dwarf2_complex_location_expr_complaint ();
11090
11091           if (!fnp->fcontext)
11092             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11093         }
11094       else if (attr_form_is_section_offset (attr))
11095         {
11096           dwarf2_complex_location_expr_complaint ();
11097         }
11098       else
11099         {
11100           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11101                                                  fieldname);
11102         }
11103     }
11104   else
11105     {
11106       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11107       if (attr && DW_UNSND (attr))
11108         {
11109           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
11110           complaint (&symfile_complaints,
11111                      _("Member function \"%s\" (offset %d) is virtual "
11112                        "but the vtable offset is not specified"),
11113                      fieldname, die->offset.sect_off);
11114           ALLOCATE_CPLUS_STRUCT_TYPE (type);
11115           TYPE_CPLUS_DYNAMIC (type) = 1;
11116         }
11117     }
11118 }
11119
11120 /* Create the vector of member function fields, and attach it to the type.  */
11121
11122 static void
11123 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11124                                  struct dwarf2_cu *cu)
11125 {
11126   struct fnfieldlist *flp;
11127   int i;
11128
11129   if (cu->language == language_ada)
11130     error (_("unexpected member functions in Ada type"));
11131
11132   ALLOCATE_CPLUS_STRUCT_TYPE (type);
11133   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11134     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11135
11136   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11137     {
11138       struct nextfnfield *nfp = flp->head;
11139       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11140       int k;
11141
11142       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11143       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11144       fn_flp->fn_fields = (struct fn_field *)
11145         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11146       for (k = flp->length; (k--, nfp); nfp = nfp->next)
11147         fn_flp->fn_fields[k] = nfp->fnfield;
11148     }
11149
11150   TYPE_NFN_FIELDS (type) = fip->nfnfields;
11151 }
11152
11153 /* Returns non-zero if NAME is the name of a vtable member in CU's
11154    language, zero otherwise.  */
11155 static int
11156 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11157 {
11158   static const char vptr[] = "_vptr";
11159   static const char vtable[] = "vtable";
11160
11161   /* Look for the C++ and Java forms of the vtable.  */
11162   if ((cu->language == language_java
11163        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11164        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11165        && is_cplus_marker (name[sizeof (vptr) - 1])))
11166     return 1;
11167
11168   return 0;
11169 }
11170
11171 /* GCC outputs unnamed structures that are really pointers to member
11172    functions, with the ABI-specified layout.  If TYPE describes
11173    such a structure, smash it into a member function type.
11174
11175    GCC shouldn't do this; it should just output pointer to member DIEs.
11176    This is GCC PR debug/28767.  */
11177
11178 static void
11179 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11180 {
11181   struct type *pfn_type, *domain_type, *new_type;
11182
11183   /* Check for a structure with no name and two children.  */
11184   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11185     return;
11186
11187   /* Check for __pfn and __delta members.  */
11188   if (TYPE_FIELD_NAME (type, 0) == NULL
11189       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11190       || TYPE_FIELD_NAME (type, 1) == NULL
11191       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11192     return;
11193
11194   /* Find the type of the method.  */
11195   pfn_type = TYPE_FIELD_TYPE (type, 0);
11196   if (pfn_type == NULL
11197       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11198       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11199     return;
11200
11201   /* Look for the "this" argument.  */
11202   pfn_type = TYPE_TARGET_TYPE (pfn_type);
11203   if (TYPE_NFIELDS (pfn_type) == 0
11204       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11205       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11206     return;
11207
11208   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11209   new_type = alloc_type (objfile);
11210   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11211                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11212                         TYPE_VARARGS (pfn_type));
11213   smash_to_methodptr_type (type, new_type);
11214 }
11215
11216 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11217    (icc).  */
11218
11219 static int
11220 producer_is_icc (struct dwarf2_cu *cu)
11221 {
11222   if (!cu->checked_producer)
11223     check_producer (cu);
11224
11225   return cu->producer_is_icc;
11226 }
11227
11228 /* Called when we find the DIE that starts a structure or union scope
11229    (definition) to create a type for the structure or union.  Fill in
11230    the type's name and general properties; the members will not be
11231    processed until process_structure_type.
11232
11233    NOTE: we need to call these functions regardless of whether or not the
11234    DIE has a DW_AT_name attribute, since it might be an anonymous
11235    structure or union.  This gets the type entered into our set of
11236    user defined types.
11237
11238    However, if the structure is incomplete (an opaque struct/union)
11239    then suppress creating a symbol table entry for it since gdb only
11240    wants to find the one with the complete definition.  Note that if
11241    it is complete, we just call new_symbol, which does it's own
11242    checking about whether the struct/union is anonymous or not (and
11243    suppresses creating a symbol table entry itself).  */
11244
11245 static struct type *
11246 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11247 {
11248   struct objfile *objfile = cu->objfile;
11249   struct type *type;
11250   struct attribute *attr;
11251   const char *name;
11252
11253   /* If the definition of this type lives in .debug_types, read that type.
11254      Don't follow DW_AT_specification though, that will take us back up
11255      the chain and we want to go down.  */
11256   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11257   if (attr)
11258     {
11259       struct dwarf2_cu *type_cu = cu;
11260       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11261
11262       /* We could just recurse on read_structure_type, but we need to call
11263          get_die_type to ensure only one type for this DIE is created.
11264          This is important, for example, because for c++ classes we need
11265          TYPE_NAME set which is only done by new_symbol.  Blech.  */
11266       type = read_type_die (type_die, type_cu);
11267
11268       /* TYPE_CU may not be the same as CU.
11269          Ensure TYPE is recorded in CU's type_hash table.  */
11270       return set_die_type (die, type, cu);
11271     }
11272
11273   type = alloc_type (objfile);
11274   INIT_CPLUS_SPECIFIC (type);
11275
11276   name = dwarf2_name (die, cu);
11277   if (name != NULL)
11278     {
11279       if (cu->language == language_cplus
11280           || cu->language == language_java)
11281         {
11282           const char *full_name = dwarf2_full_name (name, die, cu);
11283
11284           /* dwarf2_full_name might have already finished building the DIE's
11285              type.  If so, there is no need to continue.  */
11286           if (get_die_type (die, cu) != NULL)
11287             return get_die_type (die, cu);
11288
11289           TYPE_TAG_NAME (type) = full_name;
11290           if (die->tag == DW_TAG_structure_type
11291               || die->tag == DW_TAG_class_type)
11292             TYPE_NAME (type) = TYPE_TAG_NAME (type);
11293         }
11294       else
11295         {
11296           /* The name is already allocated along with this objfile, so
11297              we don't need to duplicate it for the type.  */
11298           TYPE_TAG_NAME (type) = name;
11299           if (die->tag == DW_TAG_class_type)
11300             TYPE_NAME (type) = TYPE_TAG_NAME (type);
11301         }
11302     }
11303
11304   if (die->tag == DW_TAG_structure_type)
11305     {
11306       TYPE_CODE (type) = TYPE_CODE_STRUCT;
11307     }
11308   else if (die->tag == DW_TAG_union_type)
11309     {
11310       TYPE_CODE (type) = TYPE_CODE_UNION;
11311     }
11312   else
11313     {
11314       TYPE_CODE (type) = TYPE_CODE_CLASS;
11315     }
11316
11317   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11318     TYPE_DECLARED_CLASS (type) = 1;
11319
11320   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11321   if (attr)
11322     {
11323       TYPE_LENGTH (type) = DW_UNSND (attr);
11324     }
11325   else
11326     {
11327       TYPE_LENGTH (type) = 0;
11328     }
11329
11330   if (producer_is_icc (cu))
11331     {
11332       /* ICC does not output the required DW_AT_declaration
11333          on incomplete types, but gives them a size of zero.  */
11334     }
11335   else
11336     TYPE_STUB_SUPPORTED (type) = 1;
11337
11338   if (die_is_declaration (die, cu))
11339     TYPE_STUB (type) = 1;
11340   else if (attr == NULL && die->child == NULL
11341            && producer_is_realview (cu->producer))
11342     /* RealView does not output the required DW_AT_declaration
11343        on incomplete types.  */
11344     TYPE_STUB (type) = 1;
11345
11346   /* We need to add the type field to the die immediately so we don't
11347      infinitely recurse when dealing with pointers to the structure
11348      type within the structure itself.  */
11349   set_die_type (die, type, cu);
11350
11351   /* set_die_type should be already done.  */
11352   set_descriptive_type (type, die, cu);
11353
11354   return type;
11355 }
11356
11357 /* Finish creating a structure or union type, including filling in
11358    its members and creating a symbol for it.  */
11359
11360 static void
11361 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11362 {
11363   struct objfile *objfile = cu->objfile;
11364   struct die_info *child_die = die->child;
11365   struct type *type;
11366
11367   type = get_die_type (die, cu);
11368   if (type == NULL)
11369     type = read_structure_type (die, cu);
11370
11371   if (die->child != NULL && ! die_is_declaration (die, cu))
11372     {
11373       struct field_info fi;
11374       struct die_info *child_die;
11375       VEC (symbolp) *template_args = NULL;
11376       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11377
11378       memset (&fi, 0, sizeof (struct field_info));
11379
11380       child_die = die->child;
11381
11382       while (child_die && child_die->tag)
11383         {
11384           if (child_die->tag == DW_TAG_member
11385               || child_die->tag == DW_TAG_variable)
11386             {
11387               /* NOTE: carlton/2002-11-05: A C++ static data member
11388                  should be a DW_TAG_member that is a declaration, but
11389                  all versions of G++ as of this writing (so through at
11390                  least 3.2.1) incorrectly generate DW_TAG_variable
11391                  tags for them instead.  */
11392               dwarf2_add_field (&fi, child_die, cu);
11393             }
11394           else if (child_die->tag == DW_TAG_subprogram)
11395             {
11396               /* C++ member function.  */
11397               dwarf2_add_member_fn (&fi, child_die, type, cu);
11398             }
11399           else if (child_die->tag == DW_TAG_inheritance)
11400             {
11401               /* C++ base class field.  */
11402               dwarf2_add_field (&fi, child_die, cu);
11403             }
11404           else if (child_die->tag == DW_TAG_typedef)
11405             dwarf2_add_typedef (&fi, child_die, cu);
11406           else if (child_die->tag == DW_TAG_template_type_param
11407                    || child_die->tag == DW_TAG_template_value_param)
11408             {
11409               struct symbol *arg = new_symbol (child_die, NULL, cu);
11410
11411               if (arg != NULL)
11412                 VEC_safe_push (symbolp, template_args, arg);
11413             }
11414
11415           child_die = sibling_die (child_die);
11416         }
11417
11418       /* Attach template arguments to type.  */
11419       if (! VEC_empty (symbolp, template_args))
11420         {
11421           ALLOCATE_CPLUS_STRUCT_TYPE (type);
11422           TYPE_N_TEMPLATE_ARGUMENTS (type)
11423             = VEC_length (symbolp, template_args);
11424           TYPE_TEMPLATE_ARGUMENTS (type)
11425             = obstack_alloc (&objfile->objfile_obstack,
11426                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
11427                               * sizeof (struct symbol *)));
11428           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11429                   VEC_address (symbolp, template_args),
11430                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
11431                    * sizeof (struct symbol *)));
11432           VEC_free (symbolp, template_args);
11433         }
11434
11435       /* Attach fields and member functions to the type.  */
11436       if (fi.nfields)
11437         dwarf2_attach_fields_to_type (&fi, type, cu);
11438       if (fi.nfnfields)
11439         {
11440           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11441
11442           /* Get the type which refers to the base class (possibly this
11443              class itself) which contains the vtable pointer for the current
11444              class from the DW_AT_containing_type attribute.  This use of
11445              DW_AT_containing_type is a GNU extension.  */
11446
11447           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11448             {
11449               struct type *t = die_containing_type (die, cu);
11450
11451               TYPE_VPTR_BASETYPE (type) = t;
11452               if (type == t)
11453                 {
11454                   int i;
11455
11456                   /* Our own class provides vtbl ptr.  */
11457                   for (i = TYPE_NFIELDS (t) - 1;
11458                        i >= TYPE_N_BASECLASSES (t);
11459                        --i)
11460                     {
11461                       const char *fieldname = TYPE_FIELD_NAME (t, i);
11462
11463                       if (is_vtable_name (fieldname, cu))
11464                         {
11465                           TYPE_VPTR_FIELDNO (type) = i;
11466                           break;
11467                         }
11468                     }
11469
11470                   /* Complain if virtual function table field not found.  */
11471                   if (i < TYPE_N_BASECLASSES (t))
11472                     complaint (&symfile_complaints,
11473                                _("virtual function table pointer "
11474                                  "not found when defining class '%s'"),
11475                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11476                                "");
11477                 }
11478               else
11479                 {
11480                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11481                 }
11482             }
11483           else if (cu->producer
11484                    && strncmp (cu->producer,
11485                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11486             {
11487               /* The IBM XLC compiler does not provide direct indication
11488                  of the containing type, but the vtable pointer is
11489                  always named __vfp.  */
11490
11491               int i;
11492
11493               for (i = TYPE_NFIELDS (type) - 1;
11494                    i >= TYPE_N_BASECLASSES (type);
11495                    --i)
11496                 {
11497                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11498                     {
11499                       TYPE_VPTR_FIELDNO (type) = i;
11500                       TYPE_VPTR_BASETYPE (type) = type;
11501                       break;
11502                     }
11503                 }
11504             }
11505         }
11506
11507       /* Copy fi.typedef_field_list linked list elements content into the
11508          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
11509       if (fi.typedef_field_list)
11510         {
11511           int i = fi.typedef_field_list_count;
11512
11513           ALLOCATE_CPLUS_STRUCT_TYPE (type);
11514           TYPE_TYPEDEF_FIELD_ARRAY (type)
11515             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11516           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11517
11518           /* Reverse the list order to keep the debug info elements order.  */
11519           while (--i >= 0)
11520             {
11521               struct typedef_field *dest, *src;
11522
11523               dest = &TYPE_TYPEDEF_FIELD (type, i);
11524               src = &fi.typedef_field_list->field;
11525               fi.typedef_field_list = fi.typedef_field_list->next;
11526               *dest = *src;
11527             }
11528         }
11529
11530       do_cleanups (back_to);
11531
11532       if (HAVE_CPLUS_STRUCT (type))
11533         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
11534     }
11535
11536   quirk_gcc_member_function_pointer (type, objfile);
11537
11538   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
11539      snapshots) has been known to create a die giving a declaration
11540      for a class that has, as a child, a die giving a definition for a
11541      nested class.  So we have to process our children even if the
11542      current die is a declaration.  Normally, of course, a declaration
11543      won't have any children at all.  */
11544
11545   while (child_die != NULL && child_die->tag)
11546     {
11547       if (child_die->tag == DW_TAG_member
11548           || child_die->tag == DW_TAG_variable
11549           || child_die->tag == DW_TAG_inheritance
11550           || child_die->tag == DW_TAG_template_value_param
11551           || child_die->tag == DW_TAG_template_type_param)
11552         {
11553           /* Do nothing.  */
11554         }
11555       else
11556         process_die (child_die, cu);
11557
11558       child_die = sibling_die (child_die);
11559     }
11560
11561   /* Do not consider external references.  According to the DWARF standard,
11562      these DIEs are identified by the fact that they have no byte_size
11563      attribute, and a declaration attribute.  */
11564   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
11565       || !die_is_declaration (die, cu))
11566     new_symbol (die, type, cu);
11567 }
11568
11569 /* Given a DW_AT_enumeration_type die, set its type.  We do not
11570    complete the type's fields yet, or create any symbols.  */
11571
11572 static struct type *
11573 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
11574 {
11575   struct objfile *objfile = cu->objfile;
11576   struct type *type;
11577   struct attribute *attr;
11578   const char *name;
11579
11580   /* If the definition of this type lives in .debug_types, read that type.
11581      Don't follow DW_AT_specification though, that will take us back up
11582      the chain and we want to go down.  */
11583   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11584   if (attr)
11585     {
11586       struct dwarf2_cu *type_cu = cu;
11587       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11588
11589       type = read_type_die (type_die, type_cu);
11590
11591       /* TYPE_CU may not be the same as CU.
11592          Ensure TYPE is recorded in CU's type_hash table.  */
11593       return set_die_type (die, type, cu);
11594     }
11595
11596   type = alloc_type (objfile);
11597
11598   TYPE_CODE (type) = TYPE_CODE_ENUM;
11599   name = dwarf2_full_name (NULL, die, cu);
11600   if (name != NULL)
11601     TYPE_TAG_NAME (type) = name;
11602
11603   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11604   if (attr)
11605     {
11606       TYPE_LENGTH (type) = DW_UNSND (attr);
11607     }
11608   else
11609     {
11610       TYPE_LENGTH (type) = 0;
11611     }
11612
11613   /* The enumeration DIE can be incomplete.  In Ada, any type can be
11614      declared as private in the package spec, and then defined only
11615      inside the package body.  Such types are known as Taft Amendment
11616      Types.  When another package uses such a type, an incomplete DIE
11617      may be generated by the compiler.  */
11618   if (die_is_declaration (die, cu))
11619     TYPE_STUB (type) = 1;
11620
11621   return set_die_type (die, type, cu);
11622 }
11623
11624 /* Given a pointer to a die which begins an enumeration, process all
11625    the dies that define the members of the enumeration, and create the
11626    symbol for the enumeration type.
11627
11628    NOTE: We reverse the order of the element list.  */
11629
11630 static void
11631 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
11632 {
11633   struct type *this_type;
11634
11635   this_type = get_die_type (die, cu);
11636   if (this_type == NULL)
11637     this_type = read_enumeration_type (die, cu);
11638
11639   if (die->child != NULL)
11640     {
11641       struct die_info *child_die;
11642       struct symbol *sym;
11643       struct field *fields = NULL;
11644       int num_fields = 0;
11645       int unsigned_enum = 1;
11646       const char *name;
11647       int flag_enum = 1;
11648       ULONGEST mask = 0;
11649
11650       child_die = die->child;
11651       while (child_die && child_die->tag)
11652         {
11653           if (child_die->tag != DW_TAG_enumerator)
11654             {
11655               process_die (child_die, cu);
11656             }
11657           else
11658             {
11659               name = dwarf2_name (child_die, cu);
11660               if (name)
11661                 {
11662                   sym = new_symbol (child_die, this_type, cu);
11663                   if (SYMBOL_VALUE (sym) < 0)
11664                     {
11665                       unsigned_enum = 0;
11666                       flag_enum = 0;
11667                     }
11668                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
11669                     flag_enum = 0;
11670                   else
11671                     mask |= SYMBOL_VALUE (sym);
11672
11673                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
11674                     {
11675                       fields = (struct field *)
11676                         xrealloc (fields,
11677                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
11678                                   * sizeof (struct field));
11679                     }
11680
11681                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
11682                   FIELD_TYPE (fields[num_fields]) = NULL;
11683                   SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
11684                   FIELD_BITSIZE (fields[num_fields]) = 0;
11685
11686                   num_fields++;
11687                 }
11688             }
11689
11690           child_die = sibling_die (child_die);
11691         }
11692
11693       if (num_fields)
11694         {
11695           TYPE_NFIELDS (this_type) = num_fields;
11696           TYPE_FIELDS (this_type) = (struct field *)
11697             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
11698           memcpy (TYPE_FIELDS (this_type), fields,
11699                   sizeof (struct field) * num_fields);
11700           xfree (fields);
11701         }
11702       if (unsigned_enum)
11703         TYPE_UNSIGNED (this_type) = 1;
11704       if (flag_enum)
11705         TYPE_FLAG_ENUM (this_type) = 1;
11706     }
11707
11708   /* If we are reading an enum from a .debug_types unit, and the enum
11709      is a declaration, and the enum is not the signatured type in the
11710      unit, then we do not want to add a symbol for it.  Adding a
11711      symbol would in some cases obscure the true definition of the
11712      enum, giving users an incomplete type when the definition is
11713      actually available.  Note that we do not want to do this for all
11714      enums which are just declarations, because C++0x allows forward
11715      enum declarations.  */
11716   if (cu->per_cu->is_debug_types
11717       && die_is_declaration (die, cu))
11718     {
11719       struct signatured_type *sig_type;
11720
11721       sig_type
11722         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
11723                                             cu->per_cu->section,
11724                                             cu->per_cu->offset);
11725       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
11726       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
11727         return;
11728     }
11729
11730   new_symbol (die, this_type, cu);
11731 }
11732
11733 /* Extract all information from a DW_TAG_array_type DIE and put it in
11734    the DIE's type field.  For now, this only handles one dimensional
11735    arrays.  */
11736
11737 static struct type *
11738 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
11739 {
11740   struct objfile *objfile = cu->objfile;
11741   struct die_info *child_die;
11742   struct type *type;
11743   struct type *element_type, *range_type, *index_type;
11744   struct type **range_types = NULL;
11745   struct attribute *attr;
11746   int ndim = 0;
11747   struct cleanup *back_to;
11748   const char *name;
11749
11750   element_type = die_type (die, cu);
11751
11752   /* The die_type call above may have already set the type for this DIE.  */
11753   type = get_die_type (die, cu);
11754   if (type)
11755     return type;
11756
11757   /* Irix 6.2 native cc creates array types without children for
11758      arrays with unspecified length.  */
11759   if (die->child == NULL)
11760     {
11761       index_type = objfile_type (objfile)->builtin_int;
11762       range_type = create_range_type (NULL, index_type, 0, -1);
11763       type = create_array_type (NULL, element_type, range_type);
11764       return set_die_type (die, type, cu);
11765     }
11766
11767   back_to = make_cleanup (null_cleanup, NULL);
11768   child_die = die->child;
11769   while (child_die && child_die->tag)
11770     {
11771       if (child_die->tag == DW_TAG_subrange_type)
11772         {
11773           struct type *child_type = read_type_die (child_die, cu);
11774
11775           if (child_type != NULL)
11776             {
11777               /* The range type was succesfully read.  Save it for the
11778                  array type creation.  */
11779               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
11780                 {
11781                   range_types = (struct type **)
11782                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
11783                               * sizeof (struct type *));
11784                   if (ndim == 0)
11785                     make_cleanup (free_current_contents, &range_types);
11786                 }
11787               range_types[ndim++] = child_type;
11788             }
11789         }
11790       child_die = sibling_die (child_die);
11791     }
11792
11793   /* Dwarf2 dimensions are output from left to right, create the
11794      necessary array types in backwards order.  */
11795
11796   type = element_type;
11797
11798   if (read_array_order (die, cu) == DW_ORD_col_major)
11799     {
11800       int i = 0;
11801
11802       while (i < ndim)
11803         type = create_array_type (NULL, type, range_types[i++]);
11804     }
11805   else
11806     {
11807       while (ndim-- > 0)
11808         type = create_array_type (NULL, type, range_types[ndim]);
11809     }
11810
11811   /* Understand Dwarf2 support for vector types (like they occur on
11812      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
11813      array type.  This is not part of the Dwarf2/3 standard yet, but a
11814      custom vendor extension.  The main difference between a regular
11815      array and the vector variant is that vectors are passed by value
11816      to functions.  */
11817   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
11818   if (attr)
11819     make_vector_type (type);
11820
11821   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
11822      implementation may choose to implement triple vectors using this
11823      attribute.  */
11824   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11825   if (attr)
11826     {
11827       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
11828         TYPE_LENGTH (type) = DW_UNSND (attr);
11829       else
11830         complaint (&symfile_complaints,
11831                    _("DW_AT_byte_size for array type smaller "
11832                      "than the total size of elements"));
11833     }
11834
11835   name = dwarf2_name (die, cu);
11836   if (name)
11837     TYPE_NAME (type) = name;
11838
11839   /* Install the type in the die.  */
11840   set_die_type (die, type, cu);
11841
11842   /* set_die_type should be already done.  */
11843   set_descriptive_type (type, die, cu);
11844
11845   do_cleanups (back_to);
11846
11847   return type;
11848 }
11849
11850 static enum dwarf_array_dim_ordering
11851 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11852 {
11853   struct attribute *attr;
11854
11855   attr = dwarf2_attr (die, DW_AT_ordering, cu);
11856
11857   if (attr) return DW_SND (attr);
11858
11859   /* GNU F77 is a special case, as at 08/2004 array type info is the
11860      opposite order to the dwarf2 specification, but data is still
11861      laid out as per normal fortran.
11862
11863      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11864      version checking.  */
11865
11866   if (cu->language == language_fortran
11867       && cu->producer && strstr (cu->producer, "GNU F77"))
11868     {
11869       return DW_ORD_row_major;
11870     }
11871
11872   switch (cu->language_defn->la_array_ordering)
11873     {
11874     case array_column_major:
11875       return DW_ORD_col_major;
11876     case array_row_major:
11877     default:
11878       return DW_ORD_row_major;
11879     };
11880 }
11881
11882 /* Extract all information from a DW_TAG_set_type DIE and put it in
11883    the DIE's type field.  */
11884
11885 static struct type *
11886 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11887 {
11888   struct type *domain_type, *set_type;
11889   struct attribute *attr;
11890
11891   domain_type = die_type (die, cu);
11892
11893   /* The die_type call above may have already set the type for this DIE.  */
11894   set_type = get_die_type (die, cu);
11895   if (set_type)
11896     return set_type;
11897
11898   set_type = create_set_type (NULL, domain_type);
11899
11900   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11901   if (attr)
11902     TYPE_LENGTH (set_type) = DW_UNSND (attr);
11903
11904   return set_die_type (die, set_type, cu);
11905 }
11906
11907 /* A helper for read_common_block that creates a locexpr baton.
11908    SYM is the symbol which we are marking as computed.
11909    COMMON_DIE is the DIE for the common block.
11910    COMMON_LOC is the location expression attribute for the common
11911    block itself.
11912    MEMBER_LOC is the location expression attribute for the particular
11913    member of the common block that we are processing.
11914    CU is the CU from which the above come.  */
11915
11916 static void
11917 mark_common_block_symbol_computed (struct symbol *sym,
11918                                    struct die_info *common_die,
11919                                    struct attribute *common_loc,
11920                                    struct attribute *member_loc,
11921                                    struct dwarf2_cu *cu)
11922 {
11923   struct objfile *objfile = dwarf2_per_objfile->objfile;
11924   struct dwarf2_locexpr_baton *baton;
11925   gdb_byte *ptr;
11926   unsigned int cu_off;
11927   enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
11928   LONGEST offset = 0;
11929
11930   gdb_assert (common_loc && member_loc);
11931   gdb_assert (attr_form_is_block (common_loc));
11932   gdb_assert (attr_form_is_block (member_loc)
11933               || attr_form_is_constant (member_loc));
11934
11935   baton = obstack_alloc (&objfile->objfile_obstack,
11936                          sizeof (struct dwarf2_locexpr_baton));
11937   baton->per_cu = cu->per_cu;
11938   gdb_assert (baton->per_cu);
11939
11940   baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
11941
11942   if (attr_form_is_constant (member_loc))
11943     {
11944       offset = dwarf2_get_attr_constant_value (member_loc, 0);
11945       baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
11946     }
11947   else
11948     baton->size += DW_BLOCK (member_loc)->size;
11949
11950   ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
11951   baton->data = ptr;
11952
11953   *ptr++ = DW_OP_call4;
11954   cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
11955   store_unsigned_integer (ptr, 4, byte_order, cu_off);
11956   ptr += 4;
11957
11958   if (attr_form_is_constant (member_loc))
11959     {
11960       *ptr++ = DW_OP_addr;
11961       store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
11962       ptr += cu->header.addr_size;
11963     }
11964   else
11965     {
11966       /* We have to copy the data here, because DW_OP_call4 will only
11967          use a DW_AT_location attribute.  */
11968       memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
11969       ptr += DW_BLOCK (member_loc)->size;
11970     }
11971
11972   *ptr++ = DW_OP_plus;
11973   gdb_assert (ptr - baton->data == baton->size);
11974
11975   SYMBOL_LOCATION_BATON (sym) = baton;
11976   SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
11977 }
11978
11979 /* Create appropriate locally-scoped variables for all the
11980    DW_TAG_common_block entries.  Also create a struct common_block
11981    listing all such variables for `info common'.  COMMON_BLOCK_DOMAIN
11982    is used to sepate the common blocks name namespace from regular
11983    variable names.  */
11984
11985 static void
11986 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11987 {
11988   struct attribute *attr;
11989
11990   attr = dwarf2_attr (die, DW_AT_location, cu);
11991   if (attr)
11992     {
11993       /* Support the .debug_loc offsets.  */
11994       if (attr_form_is_block (attr))
11995         {
11996           /* Ok.  */
11997         }
11998       else if (attr_form_is_section_offset (attr))
11999         {
12000           dwarf2_complex_location_expr_complaint ();
12001           attr = NULL;
12002         }
12003       else
12004         {
12005           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12006                                                  "common block member");
12007           attr = NULL;
12008         }
12009     }
12010
12011   if (die->child != NULL)
12012     {
12013       struct objfile *objfile = cu->objfile;
12014       struct die_info *child_die;
12015       size_t n_entries = 0, size;
12016       struct common_block *common_block;
12017       struct symbol *sym;
12018
12019       for (child_die = die->child;
12020            child_die && child_die->tag;
12021            child_die = sibling_die (child_die))
12022         ++n_entries;
12023
12024       size = (sizeof (struct common_block)
12025               + (n_entries - 1) * sizeof (struct symbol *));
12026       common_block = obstack_alloc (&objfile->objfile_obstack, size);
12027       memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12028       common_block->n_entries = 0;
12029
12030       for (child_die = die->child;
12031            child_die && child_die->tag;
12032            child_die = sibling_die (child_die))
12033         {
12034           /* Create the symbol in the DW_TAG_common_block block in the current
12035              symbol scope.  */
12036           sym = new_symbol (child_die, NULL, cu);
12037           if (sym != NULL)
12038             {
12039               struct attribute *member_loc;
12040
12041               common_block->contents[common_block->n_entries++] = sym;
12042
12043               member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12044                                         cu);
12045               if (member_loc)
12046                 {
12047                   /* GDB has handled this for a long time, but it is
12048                      not specified by DWARF.  It seems to have been
12049                      emitted by gfortran at least as recently as:
12050                      http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057.  */
12051                   complaint (&symfile_complaints,
12052                              _("Variable in common block has "
12053                                "DW_AT_data_member_location "
12054                                "- DIE at 0x%x [in module %s]"),
12055                              child_die->offset.sect_off, cu->objfile->name);
12056
12057                   if (attr_form_is_section_offset (member_loc))
12058                     dwarf2_complex_location_expr_complaint ();
12059                   else if (attr_form_is_constant (member_loc)
12060                            || attr_form_is_block (member_loc))
12061                     {
12062                       if (attr)
12063                         mark_common_block_symbol_computed (sym, die, attr,
12064                                                            member_loc, cu);
12065                     }
12066                   else
12067                     dwarf2_complex_location_expr_complaint ();
12068                 }
12069             }
12070         }
12071
12072       sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12073       SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12074     }
12075 }
12076
12077 /* Create a type for a C++ namespace.  */
12078
12079 static struct type *
12080 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12081 {
12082   struct objfile *objfile = cu->objfile;
12083   const char *previous_prefix, *name;
12084   int is_anonymous;
12085   struct type *type;
12086
12087   /* For extensions, reuse the type of the original namespace.  */
12088   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12089     {
12090       struct die_info *ext_die;
12091       struct dwarf2_cu *ext_cu = cu;
12092
12093       ext_die = dwarf2_extension (die, &ext_cu);
12094       type = read_type_die (ext_die, ext_cu);
12095
12096       /* EXT_CU may not be the same as CU.
12097          Ensure TYPE is recorded in CU's type_hash table.  */
12098       return set_die_type (die, type, cu);
12099     }
12100
12101   name = namespace_name (die, &is_anonymous, cu);
12102
12103   /* Now build the name of the current namespace.  */
12104
12105   previous_prefix = determine_prefix (die, cu);
12106   if (previous_prefix[0] != '\0')
12107     name = typename_concat (&objfile->objfile_obstack,
12108                             previous_prefix, name, 0, cu);
12109
12110   /* Create the type.  */
12111   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12112                     objfile);
12113   TYPE_NAME (type) = name;
12114   TYPE_TAG_NAME (type) = TYPE_NAME (type);
12115
12116   return set_die_type (die, type, cu);
12117 }
12118
12119 /* Read a C++ namespace.  */
12120
12121 static void
12122 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12123 {
12124   struct objfile *objfile = cu->objfile;
12125   int is_anonymous;
12126
12127   /* Add a symbol associated to this if we haven't seen the namespace
12128      before.  Also, add a using directive if it's an anonymous
12129      namespace.  */
12130
12131   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12132     {
12133       struct type *type;
12134
12135       type = read_type_die (die, cu);
12136       new_symbol (die, type, cu);
12137
12138       namespace_name (die, &is_anonymous, cu);
12139       if (is_anonymous)
12140         {
12141           const char *previous_prefix = determine_prefix (die, cu);
12142
12143           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12144                                   NULL, NULL, 0, &objfile->objfile_obstack);
12145         }
12146     }
12147
12148   if (die->child != NULL)
12149     {
12150       struct die_info *child_die = die->child;
12151
12152       while (child_die && child_die->tag)
12153         {
12154           process_die (child_die, cu);
12155           child_die = sibling_die (child_die);
12156         }
12157     }
12158 }
12159
12160 /* Read a Fortran module as type.  This DIE can be only a declaration used for
12161    imported module.  Still we need that type as local Fortran "use ... only"
12162    declaration imports depend on the created type in determine_prefix.  */
12163
12164 static struct type *
12165 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12166 {
12167   struct objfile *objfile = cu->objfile;
12168   const char *module_name;
12169   struct type *type;
12170
12171   module_name = dwarf2_name (die, cu);
12172   if (!module_name)
12173     complaint (&symfile_complaints,
12174                _("DW_TAG_module has no name, offset 0x%x"),
12175                die->offset.sect_off);
12176   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12177
12178   /* determine_prefix uses TYPE_TAG_NAME.  */
12179   TYPE_TAG_NAME (type) = TYPE_NAME (type);
12180
12181   return set_die_type (die, type, cu);
12182 }
12183
12184 /* Read a Fortran module.  */
12185
12186 static void
12187 read_module (struct die_info *die, struct dwarf2_cu *cu)
12188 {
12189   struct die_info *child_die = die->child;
12190
12191   while (child_die && child_die->tag)
12192     {
12193       process_die (child_die, cu);
12194       child_die = sibling_die (child_die);
12195     }
12196 }
12197
12198 /* Return the name of the namespace represented by DIE.  Set
12199    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12200    namespace.  */
12201
12202 static const char *
12203 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12204 {
12205   struct die_info *current_die;
12206   const char *name = NULL;
12207
12208   /* Loop through the extensions until we find a name.  */
12209
12210   for (current_die = die;
12211        current_die != NULL;
12212        current_die = dwarf2_extension (die, &cu))
12213     {
12214       name = dwarf2_name (current_die, cu);
12215       if (name != NULL)
12216         break;
12217     }
12218
12219   /* Is it an anonymous namespace?  */
12220
12221   *is_anonymous = (name == NULL);
12222   if (*is_anonymous)
12223     name = CP_ANONYMOUS_NAMESPACE_STR;
12224
12225   return name;
12226 }
12227
12228 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12229    the user defined type vector.  */
12230
12231 static struct type *
12232 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12233 {
12234   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12235   struct comp_unit_head *cu_header = &cu->header;
12236   struct type *type;
12237   struct attribute *attr_byte_size;
12238   struct attribute *attr_address_class;
12239   int byte_size, addr_class;
12240   struct type *target_type;
12241
12242   target_type = die_type (die, cu);
12243
12244   /* The die_type call above may have already set the type for this DIE.  */
12245   type = get_die_type (die, cu);
12246   if (type)
12247     return type;
12248
12249   type = lookup_pointer_type (target_type);
12250
12251   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12252   if (attr_byte_size)
12253     byte_size = DW_UNSND (attr_byte_size);
12254   else
12255     byte_size = cu_header->addr_size;
12256
12257   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12258   if (attr_address_class)
12259     addr_class = DW_UNSND (attr_address_class);
12260   else
12261     addr_class = DW_ADDR_none;
12262
12263   /* If the pointer size or address class is different than the
12264      default, create a type variant marked as such and set the
12265      length accordingly.  */
12266   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12267     {
12268       if (gdbarch_address_class_type_flags_p (gdbarch))
12269         {
12270           int type_flags;
12271
12272           type_flags = gdbarch_address_class_type_flags
12273                          (gdbarch, byte_size, addr_class);
12274           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12275                       == 0);
12276           type = make_type_with_address_space (type, type_flags);
12277         }
12278       else if (TYPE_LENGTH (type) != byte_size)
12279         {
12280           complaint (&symfile_complaints,
12281                      _("invalid pointer size %d"), byte_size);
12282         }
12283       else
12284         {
12285           /* Should we also complain about unhandled address classes?  */
12286         }
12287     }
12288
12289   TYPE_LENGTH (type) = byte_size;
12290   return set_die_type (die, type, cu);
12291 }
12292
12293 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12294    the user defined type vector.  */
12295
12296 static struct type *
12297 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12298 {
12299   struct type *type;
12300   struct type *to_type;
12301   struct type *domain;
12302
12303   to_type = die_type (die, cu);
12304   domain = die_containing_type (die, cu);
12305
12306   /* The calls above may have already set the type for this DIE.  */
12307   type = get_die_type (die, cu);
12308   if (type)
12309     return type;
12310
12311   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12312     type = lookup_methodptr_type (to_type);
12313   else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12314     {
12315       struct type *new_type = alloc_type (cu->objfile);
12316
12317       smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12318                             TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12319                             TYPE_VARARGS (to_type));
12320       type = lookup_methodptr_type (new_type);
12321     }
12322   else
12323     type = lookup_memberptr_type (to_type, domain);
12324
12325   return set_die_type (die, type, cu);
12326 }
12327
12328 /* Extract all information from a DW_TAG_reference_type DIE and add to
12329    the user defined type vector.  */
12330
12331 static struct type *
12332 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12333 {
12334   struct comp_unit_head *cu_header = &cu->header;
12335   struct type *type, *target_type;
12336   struct attribute *attr;
12337
12338   target_type = die_type (die, cu);
12339
12340   /* The die_type call above may have already set the type for this DIE.  */
12341   type = get_die_type (die, cu);
12342   if (type)
12343     return type;
12344
12345   type = lookup_reference_type (target_type);
12346   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12347   if (attr)
12348     {
12349       TYPE_LENGTH (type) = DW_UNSND (attr);
12350     }
12351   else
12352     {
12353       TYPE_LENGTH (type) = cu_header->addr_size;
12354     }
12355   return set_die_type (die, type, cu);
12356 }
12357
12358 static struct type *
12359 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12360 {
12361   struct type *base_type, *cv_type;
12362
12363   base_type = die_type (die, cu);
12364
12365   /* The die_type call above may have already set the type for this DIE.  */
12366   cv_type = get_die_type (die, cu);
12367   if (cv_type)
12368     return cv_type;
12369
12370   /* In case the const qualifier is applied to an array type, the element type
12371      is so qualified, not the array type (section 6.7.3 of C99).  */
12372   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12373     {
12374       struct type *el_type, *inner_array;
12375
12376       base_type = copy_type (base_type);
12377       inner_array = base_type;
12378
12379       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12380         {
12381           TYPE_TARGET_TYPE (inner_array) =
12382             copy_type (TYPE_TARGET_TYPE (inner_array));
12383           inner_array = TYPE_TARGET_TYPE (inner_array);
12384         }
12385
12386       el_type = TYPE_TARGET_TYPE (inner_array);
12387       TYPE_TARGET_TYPE (inner_array) =
12388         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12389
12390       return set_die_type (die, base_type, cu);
12391     }
12392
12393   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12394   return set_die_type (die, cv_type, cu);
12395 }
12396
12397 static struct type *
12398 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12399 {
12400   struct type *base_type, *cv_type;
12401
12402   base_type = die_type (die, cu);
12403
12404   /* The die_type call above may have already set the type for this DIE.  */
12405   cv_type = get_die_type (die, cu);
12406   if (cv_type)
12407     return cv_type;
12408
12409   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12410   return set_die_type (die, cv_type, cu);
12411 }
12412
12413 /* Handle DW_TAG_restrict_type.  */
12414
12415 static struct type *
12416 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12417 {
12418   struct type *base_type, *cv_type;
12419
12420   base_type = die_type (die, cu);
12421
12422   /* The die_type call above may have already set the type for this DIE.  */
12423   cv_type = get_die_type (die, cu);
12424   if (cv_type)
12425     return cv_type;
12426
12427   cv_type = make_restrict_type (base_type);
12428   return set_die_type (die, cv_type, cu);
12429 }
12430
12431 /* Extract all information from a DW_TAG_string_type DIE and add to
12432    the user defined type vector.  It isn't really a user defined type,
12433    but it behaves like one, with other DIE's using an AT_user_def_type
12434    attribute to reference it.  */
12435
12436 static struct type *
12437 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12438 {
12439   struct objfile *objfile = cu->objfile;
12440   struct gdbarch *gdbarch = get_objfile_arch (objfile);
12441   struct type *type, *range_type, *index_type, *char_type;
12442   struct attribute *attr;
12443   unsigned int length;
12444
12445   attr = dwarf2_attr (die, DW_AT_string_length, cu);
12446   if (attr)
12447     {
12448       length = DW_UNSND (attr);
12449     }
12450   else
12451     {
12452       /* Check for the DW_AT_byte_size attribute.  */
12453       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12454       if (attr)
12455         {
12456           length = DW_UNSND (attr);
12457         }
12458       else
12459         {
12460           length = 1;
12461         }
12462     }
12463
12464   index_type = objfile_type (objfile)->builtin_int;
12465   range_type = create_range_type (NULL, index_type, 1, length);
12466   char_type = language_string_char_type (cu->language_defn, gdbarch);
12467   type = create_string_type (NULL, char_type, range_type);
12468
12469   return set_die_type (die, type, cu);
12470 }
12471
12472 /* Handle DIES due to C code like:
12473
12474    struct foo
12475    {
12476    int (*funcp)(int a, long l);
12477    int b;
12478    };
12479
12480    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
12481
12482 static struct type *
12483 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12484 {
12485   struct objfile *objfile = cu->objfile;
12486   struct type *type;            /* Type that this function returns.  */
12487   struct type *ftype;           /* Function that returns above type.  */
12488   struct attribute *attr;
12489
12490   type = die_type (die, cu);
12491
12492   /* The die_type call above may have already set the type for this DIE.  */
12493   ftype = get_die_type (die, cu);
12494   if (ftype)
12495     return ftype;
12496
12497   ftype = lookup_function_type (type);
12498
12499   /* All functions in C++, Pascal and Java have prototypes.  */
12500   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12501   if ((attr && (DW_UNSND (attr) != 0))
12502       || cu->language == language_cplus
12503       || cu->language == language_java
12504       || cu->language == language_pascal)
12505     TYPE_PROTOTYPED (ftype) = 1;
12506   else if (producer_is_realview (cu->producer))
12507     /* RealView does not emit DW_AT_prototyped.  We can not
12508        distinguish prototyped and unprototyped functions; default to
12509        prototyped, since that is more common in modern code (and
12510        RealView warns about unprototyped functions).  */
12511     TYPE_PROTOTYPED (ftype) = 1;
12512
12513   /* Store the calling convention in the type if it's available in
12514      the subroutine die.  Otherwise set the calling convention to
12515      the default value DW_CC_normal.  */
12516   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
12517   if (attr)
12518     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
12519   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
12520     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
12521   else
12522     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
12523
12524   /* We need to add the subroutine type to the die immediately so
12525      we don't infinitely recurse when dealing with parameters
12526      declared as the same subroutine type.  */
12527   set_die_type (die, ftype, cu);
12528
12529   if (die->child != NULL)
12530     {
12531       struct type *void_type = objfile_type (objfile)->builtin_void;
12532       struct die_info *child_die;
12533       int nparams, iparams;
12534
12535       /* Count the number of parameters.
12536          FIXME: GDB currently ignores vararg functions, but knows about
12537          vararg member functions.  */
12538       nparams = 0;
12539       child_die = die->child;
12540       while (child_die && child_die->tag)
12541         {
12542           if (child_die->tag == DW_TAG_formal_parameter)
12543             nparams++;
12544           else if (child_die->tag == DW_TAG_unspecified_parameters)
12545             TYPE_VARARGS (ftype) = 1;
12546           child_die = sibling_die (child_die);
12547         }
12548
12549       /* Allocate storage for parameters and fill them in.  */
12550       TYPE_NFIELDS (ftype) = nparams;
12551       TYPE_FIELDS (ftype) = (struct field *)
12552         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
12553
12554       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
12555          even if we error out during the parameters reading below.  */
12556       for (iparams = 0; iparams < nparams; iparams++)
12557         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
12558
12559       iparams = 0;
12560       child_die = die->child;
12561       while (child_die && child_die->tag)
12562         {
12563           if (child_die->tag == DW_TAG_formal_parameter)
12564             {
12565               struct type *arg_type;
12566
12567               /* DWARF version 2 has no clean way to discern C++
12568                  static and non-static member functions.  G++ helps
12569                  GDB by marking the first parameter for non-static
12570                  member functions (which is the this pointer) as
12571                  artificial.  We pass this information to
12572                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
12573
12574                  DWARF version 3 added DW_AT_object_pointer, which GCC
12575                  4.5 does not yet generate.  */
12576               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
12577               if (attr)
12578                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
12579               else
12580                 {
12581                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
12582
12583                   /* GCC/43521: In java, the formal parameter
12584                      "this" is sometimes not marked with DW_AT_artificial.  */
12585                   if (cu->language == language_java)
12586                     {
12587                       const char *name = dwarf2_name (child_die, cu);
12588
12589                       if (name && !strcmp (name, "this"))
12590                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
12591                     }
12592                 }
12593               arg_type = die_type (child_die, cu);
12594
12595               /* RealView does not mark THIS as const, which the testsuite
12596                  expects.  GCC marks THIS as const in method definitions,
12597                  but not in the class specifications (GCC PR 43053).  */
12598               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
12599                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
12600                 {
12601                   int is_this = 0;
12602                   struct dwarf2_cu *arg_cu = cu;
12603                   const char *name = dwarf2_name (child_die, cu);
12604
12605                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
12606                   if (attr)
12607                     {
12608                       /* If the compiler emits this, use it.  */
12609                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
12610                         is_this = 1;
12611                     }
12612                   else if (name && strcmp (name, "this") == 0)
12613                     /* Function definitions will have the argument names.  */
12614                     is_this = 1;
12615                   else if (name == NULL && iparams == 0)
12616                     /* Declarations may not have the names, so like
12617                        elsewhere in GDB, assume an artificial first
12618                        argument is "this".  */
12619                     is_this = 1;
12620
12621                   if (is_this)
12622                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
12623                                              arg_type, 0);
12624                 }
12625
12626               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
12627               iparams++;
12628             }
12629           child_die = sibling_die (child_die);
12630         }
12631     }
12632
12633   return ftype;
12634 }
12635
12636 static struct type *
12637 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
12638 {
12639   struct objfile *objfile = cu->objfile;
12640   const char *name = NULL;
12641   struct type *this_type, *target_type;
12642
12643   name = dwarf2_full_name (NULL, die, cu);
12644   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
12645                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
12646   TYPE_NAME (this_type) = name;
12647   set_die_type (die, this_type, cu);
12648   target_type = die_type (die, cu);
12649   if (target_type != this_type)
12650     TYPE_TARGET_TYPE (this_type) = target_type;
12651   else
12652     {
12653       /* Self-referential typedefs are, it seems, not allowed by the DWARF
12654          spec and cause infinite loops in GDB.  */
12655       complaint (&symfile_complaints,
12656                  _("Self-referential DW_TAG_typedef "
12657                    "- DIE at 0x%x [in module %s]"),
12658                  die->offset.sect_off, objfile->name);
12659       TYPE_TARGET_TYPE (this_type) = NULL;
12660     }
12661   return this_type;
12662 }
12663
12664 /* Find a representation of a given base type and install
12665    it in the TYPE field of the die.  */
12666
12667 static struct type *
12668 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
12669 {
12670   struct objfile *objfile = cu->objfile;
12671   struct type *type;
12672   struct attribute *attr;
12673   int encoding = 0, size = 0;
12674   const char *name;
12675   enum type_code code = TYPE_CODE_INT;
12676   int type_flags = 0;
12677   struct type *target_type = NULL;
12678
12679   attr = dwarf2_attr (die, DW_AT_encoding, cu);
12680   if (attr)
12681     {
12682       encoding = DW_UNSND (attr);
12683     }
12684   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12685   if (attr)
12686     {
12687       size = DW_UNSND (attr);
12688     }
12689   name = dwarf2_name (die, cu);
12690   if (!name)
12691     {
12692       complaint (&symfile_complaints,
12693                  _("DW_AT_name missing from DW_TAG_base_type"));
12694     }
12695
12696   switch (encoding)
12697     {
12698       case DW_ATE_address:
12699         /* Turn DW_ATE_address into a void * pointer.  */
12700         code = TYPE_CODE_PTR;
12701         type_flags |= TYPE_FLAG_UNSIGNED;
12702         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
12703         break;
12704       case DW_ATE_boolean:
12705         code = TYPE_CODE_BOOL;
12706         type_flags |= TYPE_FLAG_UNSIGNED;
12707         break;
12708       case DW_ATE_complex_float:
12709         code = TYPE_CODE_COMPLEX;
12710         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
12711         break;
12712       case DW_ATE_decimal_float:
12713         code = TYPE_CODE_DECFLOAT;
12714         break;
12715       case DW_ATE_float:
12716         code = TYPE_CODE_FLT;
12717         break;
12718       case DW_ATE_signed:
12719         break;
12720       case DW_ATE_unsigned:
12721         type_flags |= TYPE_FLAG_UNSIGNED;
12722         if (cu->language == language_fortran
12723             && name
12724             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
12725           code = TYPE_CODE_CHAR;
12726         break;
12727       case DW_ATE_signed_char:
12728         if (cu->language == language_ada || cu->language == language_m2
12729             || cu->language == language_pascal
12730             || cu->language == language_fortran)
12731           code = TYPE_CODE_CHAR;
12732         break;
12733       case DW_ATE_unsigned_char:
12734         if (cu->language == language_ada || cu->language == language_m2
12735             || cu->language == language_pascal
12736             || cu->language == language_fortran)
12737           code = TYPE_CODE_CHAR;
12738         type_flags |= TYPE_FLAG_UNSIGNED;
12739         break;
12740       case DW_ATE_UTF:
12741         /* We just treat this as an integer and then recognize the
12742            type by name elsewhere.  */
12743         break;
12744
12745       default:
12746         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
12747                    dwarf_type_encoding_name (encoding));
12748         break;
12749     }
12750
12751   type = init_type (code, size, type_flags, NULL, objfile);
12752   TYPE_NAME (type) = name;
12753   TYPE_TARGET_TYPE (type) = target_type;
12754
12755   if (name && strcmp (name, "char") == 0)
12756     TYPE_NOSIGN (type) = 1;
12757
12758   return set_die_type (die, type, cu);
12759 }
12760
12761 /* Read the given DW_AT_subrange DIE.  */
12762
12763 static struct type *
12764 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
12765 {
12766   struct type *base_type, *orig_base_type;
12767   struct type *range_type;
12768   struct attribute *attr;
12769   LONGEST low, high;
12770   int low_default_is_valid;
12771   const char *name;
12772   LONGEST negative_mask;
12773
12774   orig_base_type = die_type (die, cu);
12775   /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
12776      whereas the real type might be.  So, we use ORIG_BASE_TYPE when
12777      creating the range type, but we use the result of check_typedef
12778      when examining properties of the type.  */
12779   base_type = check_typedef (orig_base_type);
12780
12781   /* The die_type call above may have already set the type for this DIE.  */
12782   range_type = get_die_type (die, cu);
12783   if (range_type)
12784     return range_type;
12785
12786   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
12787      omitting DW_AT_lower_bound.  */
12788   switch (cu->language)
12789     {
12790     case language_c:
12791     case language_cplus:
12792       low = 0;
12793       low_default_is_valid = 1;
12794       break;
12795     case language_fortran:
12796       low = 1;
12797       low_default_is_valid = 1;
12798       break;
12799     case language_d:
12800     case language_java:
12801     case language_objc:
12802       low = 0;
12803       low_default_is_valid = (cu->header.version >= 4);
12804       break;
12805     case language_ada:
12806     case language_m2:
12807     case language_pascal:
12808       low = 1;
12809       low_default_is_valid = (cu->header.version >= 4);
12810       break;
12811     default:
12812       low = 0;
12813       low_default_is_valid = 0;
12814       break;
12815     }
12816
12817   /* FIXME: For variable sized arrays either of these could be
12818      a variable rather than a constant value.  We'll allow it,
12819      but we don't know how to handle it.  */
12820   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
12821   if (attr)
12822     low = dwarf2_get_attr_constant_value (attr, low);
12823   else if (!low_default_is_valid)
12824     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
12825                                       "- DIE at 0x%x [in module %s]"),
12826                die->offset.sect_off, cu->objfile->name);
12827
12828   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
12829   if (attr)
12830     {
12831       if (attr_form_is_block (attr) || is_ref_attr (attr))
12832         {
12833           /* GCC encodes arrays with unspecified or dynamic length
12834              with a DW_FORM_block1 attribute or a reference attribute.
12835              FIXME: GDB does not yet know how to handle dynamic
12836              arrays properly, treat them as arrays with unspecified
12837              length for now.
12838
12839              FIXME: jimb/2003-09-22: GDB does not really know
12840              how to handle arrays of unspecified length
12841              either; we just represent them as zero-length
12842              arrays.  Choose an appropriate upper bound given
12843              the lower bound we've computed above.  */
12844           high = low - 1;
12845         }
12846       else
12847         high = dwarf2_get_attr_constant_value (attr, 1);
12848     }
12849   else
12850     {
12851       attr = dwarf2_attr (die, DW_AT_count, cu);
12852       if (attr)
12853         {
12854           int count = dwarf2_get_attr_constant_value (attr, 1);
12855           high = low + count - 1;
12856         }
12857       else
12858         {
12859           /* Unspecified array length.  */
12860           high = low - 1;
12861         }
12862     }
12863
12864   /* Dwarf-2 specifications explicitly allows to create subrange types
12865      without specifying a base type.
12866      In that case, the base type must be set to the type of
12867      the lower bound, upper bound or count, in that order, if any of these
12868      three attributes references an object that has a type.
12869      If no base type is found, the Dwarf-2 specifications say that
12870      a signed integer type of size equal to the size of an address should
12871      be used.
12872      For the following C code: `extern char gdb_int [];'
12873      GCC produces an empty range DIE.
12874      FIXME: muller/2010-05-28: Possible references to object for low bound,
12875      high bound or count are not yet handled by this code.  */
12876   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
12877     {
12878       struct objfile *objfile = cu->objfile;
12879       struct gdbarch *gdbarch = get_objfile_arch (objfile);
12880       int addr_size = gdbarch_addr_bit (gdbarch) /8;
12881       struct type *int_type = objfile_type (objfile)->builtin_int;
12882
12883       /* Test "int", "long int", and "long long int" objfile types,
12884          and select the first one having a size above or equal to the
12885          architecture address size.  */
12886       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12887         base_type = int_type;
12888       else
12889         {
12890           int_type = objfile_type (objfile)->builtin_long;
12891           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12892             base_type = int_type;
12893           else
12894             {
12895               int_type = objfile_type (objfile)->builtin_long_long;
12896               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
12897                 base_type = int_type;
12898             }
12899         }
12900     }
12901
12902   negative_mask =
12903     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
12904   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
12905     low |= negative_mask;
12906   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
12907     high |= negative_mask;
12908
12909   range_type = create_range_type (NULL, orig_base_type, low, high);
12910
12911   /* Mark arrays with dynamic length at least as an array of unspecified
12912      length.  GDB could check the boundary but before it gets implemented at
12913      least allow accessing the array elements.  */
12914   if (attr && attr_form_is_block (attr))
12915     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12916
12917   /* Ada expects an empty array on no boundary attributes.  */
12918   if (attr == NULL && cu->language != language_ada)
12919     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
12920
12921   name = dwarf2_name (die, cu);
12922   if (name)
12923     TYPE_NAME (range_type) = name;
12924
12925   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12926   if (attr)
12927     TYPE_LENGTH (range_type) = DW_UNSND (attr);
12928
12929   set_die_type (die, range_type, cu);
12930
12931   /* set_die_type should be already done.  */
12932   set_descriptive_type (range_type, die, cu);
12933
12934   return range_type;
12935 }
12936
12937 static struct type *
12938 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
12939 {
12940   struct type *type;
12941
12942   /* For now, we only support the C meaning of an unspecified type: void.  */
12943
12944   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
12945   TYPE_NAME (type) = dwarf2_name (die, cu);
12946
12947   return set_die_type (die, type, cu);
12948 }
12949
12950 /* Read a single die and all its descendents.  Set the die's sibling
12951    field to NULL; set other fields in the die correctly, and set all
12952    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
12953    location of the info_ptr after reading all of those dies.  PARENT
12954    is the parent of the die in question.  */
12955
12956 static struct die_info *
12957 read_die_and_children (const struct die_reader_specs *reader,
12958                        gdb_byte *info_ptr,
12959                        gdb_byte **new_info_ptr,
12960                        struct die_info *parent)
12961 {
12962   struct die_info *die;
12963   gdb_byte *cur_ptr;
12964   int has_children;
12965
12966   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
12967   if (die == NULL)
12968     {
12969       *new_info_ptr = cur_ptr;
12970       return NULL;
12971     }
12972   store_in_ref_table (die, reader->cu);
12973
12974   if (has_children)
12975     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
12976   else
12977     {
12978       die->child = NULL;
12979       *new_info_ptr = cur_ptr;
12980     }
12981
12982   die->sibling = NULL;
12983   die->parent = parent;
12984   return die;
12985 }
12986
12987 /* Read a die, all of its descendents, and all of its siblings; set
12988    all of the fields of all of the dies correctly.  Arguments are as
12989    in read_die_and_children.  */
12990
12991 static struct die_info *
12992 read_die_and_siblings (const struct die_reader_specs *reader,
12993                        gdb_byte *info_ptr,
12994                        gdb_byte **new_info_ptr,
12995                        struct die_info *parent)
12996 {
12997   struct die_info *first_die, *last_sibling;
12998   gdb_byte *cur_ptr;
12999
13000   cur_ptr = info_ptr;
13001   first_die = last_sibling = NULL;
13002
13003   while (1)
13004     {
13005       struct die_info *die
13006         = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13007
13008       if (die == NULL)
13009         {
13010           *new_info_ptr = cur_ptr;
13011           return first_die;
13012         }
13013
13014       if (!first_die)
13015         first_die = die;
13016       else
13017         last_sibling->sibling = die;
13018
13019       last_sibling = die;
13020     }
13021 }
13022
13023 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13024    attributes.
13025    The caller is responsible for filling in the extra attributes
13026    and updating (*DIEP)->num_attrs.
13027    Set DIEP to point to a newly allocated die with its information,
13028    except for its child, sibling, and parent fields.
13029    Set HAS_CHILDREN to tell whether the die has children or not.  */
13030
13031 static gdb_byte *
13032 read_full_die_1 (const struct die_reader_specs *reader,
13033                  struct die_info **diep, gdb_byte *info_ptr,
13034                  int *has_children, int num_extra_attrs)
13035 {
13036   unsigned int abbrev_number, bytes_read, i;
13037   sect_offset offset;
13038   struct abbrev_info *abbrev;
13039   struct die_info *die;
13040   struct dwarf2_cu *cu = reader->cu;
13041   bfd *abfd = reader->abfd;
13042
13043   offset.sect_off = info_ptr - reader->buffer;
13044   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13045   info_ptr += bytes_read;
13046   if (!abbrev_number)
13047     {
13048       *diep = NULL;
13049       *has_children = 0;
13050       return info_ptr;
13051     }
13052
13053   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13054   if (!abbrev)
13055     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13056            abbrev_number,
13057            bfd_get_filename (abfd));
13058
13059   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13060   die->offset = offset;
13061   die->tag = abbrev->tag;
13062   die->abbrev = abbrev_number;
13063
13064   /* Make the result usable.
13065      The caller needs to update num_attrs after adding the extra
13066      attributes.  */
13067   die->num_attrs = abbrev->num_attrs;
13068
13069   for (i = 0; i < abbrev->num_attrs; ++i)
13070     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13071                                info_ptr);
13072
13073   *diep = die;
13074   *has_children = abbrev->has_children;
13075   return info_ptr;
13076 }
13077
13078 /* Read a die and all its attributes.
13079    Set DIEP to point to a newly allocated die with its information,
13080    except for its child, sibling, and parent fields.
13081    Set HAS_CHILDREN to tell whether the die has children or not.  */
13082
13083 static gdb_byte *
13084 read_full_die (const struct die_reader_specs *reader,
13085                struct die_info **diep, gdb_byte *info_ptr,
13086                int *has_children)
13087 {
13088   return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13089 }
13090 \f
13091 /* Abbreviation tables.
13092
13093    In DWARF version 2, the description of the debugging information is
13094    stored in a separate .debug_abbrev section.  Before we read any
13095    dies from a section we read in all abbreviations and install them
13096    in a hash table.  */
13097
13098 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
13099
13100 static struct abbrev_info *
13101 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13102 {
13103   struct abbrev_info *abbrev;
13104
13105   abbrev = (struct abbrev_info *)
13106     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13107   memset (abbrev, 0, sizeof (struct abbrev_info));
13108   return abbrev;
13109 }
13110
13111 /* Add an abbreviation to the table.  */
13112
13113 static void
13114 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13115                          unsigned int abbrev_number,
13116                          struct abbrev_info *abbrev)
13117 {
13118   unsigned int hash_number;
13119
13120   hash_number = abbrev_number % ABBREV_HASH_SIZE;
13121   abbrev->next = abbrev_table->abbrevs[hash_number];
13122   abbrev_table->abbrevs[hash_number] = abbrev;
13123 }
13124
13125 /* Look up an abbrev in the table.
13126    Returns NULL if the abbrev is not found.  */
13127
13128 static struct abbrev_info *
13129 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13130                             unsigned int abbrev_number)
13131 {
13132   unsigned int hash_number;
13133   struct abbrev_info *abbrev;
13134
13135   hash_number = abbrev_number % ABBREV_HASH_SIZE;
13136   abbrev = abbrev_table->abbrevs[hash_number];
13137
13138   while (abbrev)
13139     {
13140       if (abbrev->number == abbrev_number)
13141         return abbrev;
13142       abbrev = abbrev->next;
13143     }
13144   return NULL;
13145 }
13146
13147 /* Read in an abbrev table.  */
13148
13149 static struct abbrev_table *
13150 abbrev_table_read_table (struct dwarf2_section_info *section,
13151                          sect_offset offset)
13152 {
13153   struct objfile *objfile = dwarf2_per_objfile->objfile;
13154   bfd *abfd = section->asection->owner;
13155   struct abbrev_table *abbrev_table;
13156   gdb_byte *abbrev_ptr;
13157   struct abbrev_info *cur_abbrev;
13158   unsigned int abbrev_number, bytes_read, abbrev_name;
13159   unsigned int abbrev_form;
13160   struct attr_abbrev *cur_attrs;
13161   unsigned int allocated_attrs;
13162
13163   abbrev_table = XMALLOC (struct abbrev_table);
13164   abbrev_table->offset = offset;
13165   obstack_init (&abbrev_table->abbrev_obstack);
13166   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13167                                          (ABBREV_HASH_SIZE
13168                                           * sizeof (struct abbrev_info *)));
13169   memset (abbrev_table->abbrevs, 0,
13170           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13171
13172   dwarf2_read_section (objfile, section);
13173   abbrev_ptr = section->buffer + offset.sect_off;
13174   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13175   abbrev_ptr += bytes_read;
13176
13177   allocated_attrs = ATTR_ALLOC_CHUNK;
13178   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13179
13180   /* Loop until we reach an abbrev number of 0.  */
13181   while (abbrev_number)
13182     {
13183       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13184
13185       /* read in abbrev header */
13186       cur_abbrev->number = abbrev_number;
13187       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13188       abbrev_ptr += bytes_read;
13189       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13190       abbrev_ptr += 1;
13191
13192       /* now read in declarations */
13193       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13194       abbrev_ptr += bytes_read;
13195       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13196       abbrev_ptr += bytes_read;
13197       while (abbrev_name)
13198         {
13199           if (cur_abbrev->num_attrs == allocated_attrs)
13200             {
13201               allocated_attrs += ATTR_ALLOC_CHUNK;
13202               cur_attrs
13203                 = xrealloc (cur_attrs, (allocated_attrs
13204                                         * sizeof (struct attr_abbrev)));
13205             }
13206
13207           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13208           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13209           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13210           abbrev_ptr += bytes_read;
13211           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13212           abbrev_ptr += bytes_read;
13213         }
13214
13215       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13216                                          (cur_abbrev->num_attrs
13217                                           * sizeof (struct attr_abbrev)));
13218       memcpy (cur_abbrev->attrs, cur_attrs,
13219               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13220
13221       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13222
13223       /* Get next abbreviation.
13224          Under Irix6 the abbreviations for a compilation unit are not
13225          always properly terminated with an abbrev number of 0.
13226          Exit loop if we encounter an abbreviation which we have
13227          already read (which means we are about to read the abbreviations
13228          for the next compile unit) or if the end of the abbreviation
13229          table is reached.  */
13230       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13231         break;
13232       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13233       abbrev_ptr += bytes_read;
13234       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13235         break;
13236     }
13237
13238   xfree (cur_attrs);
13239   return abbrev_table;
13240 }
13241
13242 /* Free the resources held by ABBREV_TABLE.  */
13243
13244 static void
13245 abbrev_table_free (struct abbrev_table *abbrev_table)
13246 {
13247   obstack_free (&abbrev_table->abbrev_obstack, NULL);
13248   xfree (abbrev_table);
13249 }
13250
13251 /* Same as abbrev_table_free but as a cleanup.
13252    We pass in a pointer to the pointer to the table so that we can
13253    set the pointer to NULL when we're done.  It also simplifies
13254    build_type_unit_groups.  */
13255
13256 static void
13257 abbrev_table_free_cleanup (void *table_ptr)
13258 {
13259   struct abbrev_table **abbrev_table_ptr = table_ptr;
13260
13261   if (*abbrev_table_ptr != NULL)
13262     abbrev_table_free (*abbrev_table_ptr);
13263   *abbrev_table_ptr = NULL;
13264 }
13265
13266 /* Read the abbrev table for CU from ABBREV_SECTION.  */
13267
13268 static void
13269 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13270                      struct dwarf2_section_info *abbrev_section)
13271 {
13272   cu->abbrev_table =
13273     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13274 }
13275
13276 /* Release the memory used by the abbrev table for a compilation unit.  */
13277
13278 static void
13279 dwarf2_free_abbrev_table (void *ptr_to_cu)
13280 {
13281   struct dwarf2_cu *cu = ptr_to_cu;
13282
13283   abbrev_table_free (cu->abbrev_table);
13284   /* Set this to NULL so that we SEGV if we try to read it later,
13285      and also because free_comp_unit verifies this is NULL.  */
13286   cu->abbrev_table = NULL;
13287 }
13288 \f
13289 /* Returns nonzero if TAG represents a type that we might generate a partial
13290    symbol for.  */
13291
13292 static int
13293 is_type_tag_for_partial (int tag)
13294 {
13295   switch (tag)
13296     {
13297 #if 0
13298     /* Some types that would be reasonable to generate partial symbols for,
13299        that we don't at present.  */
13300     case DW_TAG_array_type:
13301     case DW_TAG_file_type:
13302     case DW_TAG_ptr_to_member_type:
13303     case DW_TAG_set_type:
13304     case DW_TAG_string_type:
13305     case DW_TAG_subroutine_type:
13306 #endif
13307     case DW_TAG_base_type:
13308     case DW_TAG_class_type:
13309     case DW_TAG_interface_type:
13310     case DW_TAG_enumeration_type:
13311     case DW_TAG_structure_type:
13312     case DW_TAG_subrange_type:
13313     case DW_TAG_typedef:
13314     case DW_TAG_union_type:
13315       return 1;
13316     default:
13317       return 0;
13318     }
13319 }
13320
13321 /* Load all DIEs that are interesting for partial symbols into memory.  */
13322
13323 static struct partial_die_info *
13324 load_partial_dies (const struct die_reader_specs *reader,
13325                    gdb_byte *info_ptr, int building_psymtab)
13326 {
13327   struct dwarf2_cu *cu = reader->cu;
13328   struct objfile *objfile = cu->objfile;
13329   struct partial_die_info *part_die;
13330   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13331   struct abbrev_info *abbrev;
13332   unsigned int bytes_read;
13333   unsigned int load_all = 0;
13334   int nesting_level = 1;
13335
13336   parent_die = NULL;
13337   last_die = NULL;
13338
13339   gdb_assert (cu->per_cu != NULL);
13340   if (cu->per_cu->load_all_dies)
13341     load_all = 1;
13342
13343   cu->partial_dies
13344     = htab_create_alloc_ex (cu->header.length / 12,
13345                             partial_die_hash,
13346                             partial_die_eq,
13347                             NULL,
13348                             &cu->comp_unit_obstack,
13349                             hashtab_obstack_allocate,
13350                             dummy_obstack_deallocate);
13351
13352   part_die = obstack_alloc (&cu->comp_unit_obstack,
13353                             sizeof (struct partial_die_info));
13354
13355   while (1)
13356     {
13357       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13358
13359       /* A NULL abbrev means the end of a series of children.  */
13360       if (abbrev == NULL)
13361         {
13362           if (--nesting_level == 0)
13363             {
13364               /* PART_DIE was probably the last thing allocated on the
13365                  comp_unit_obstack, so we could call obstack_free
13366                  here.  We don't do that because the waste is small,
13367                  and will be cleaned up when we're done with this
13368                  compilation unit.  This way, we're also more robust
13369                  against other users of the comp_unit_obstack.  */
13370               return first_die;
13371             }
13372           info_ptr += bytes_read;
13373           last_die = parent_die;
13374           parent_die = parent_die->die_parent;
13375           continue;
13376         }
13377
13378       /* Check for template arguments.  We never save these; if
13379          they're seen, we just mark the parent, and go on our way.  */
13380       if (parent_die != NULL
13381           && cu->language == language_cplus
13382           && (abbrev->tag == DW_TAG_template_type_param
13383               || abbrev->tag == DW_TAG_template_value_param))
13384         {
13385           parent_die->has_template_arguments = 1;
13386
13387           if (!load_all)
13388             {
13389               /* We don't need a partial DIE for the template argument.  */
13390               info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13391               continue;
13392             }
13393         }
13394
13395       /* We only recurse into c++ subprograms looking for template arguments.
13396          Skip their other children.  */
13397       if (!load_all
13398           && cu->language == language_cplus
13399           && parent_die != NULL
13400           && parent_die->tag == DW_TAG_subprogram)
13401         {
13402           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13403           continue;
13404         }
13405
13406       /* Check whether this DIE is interesting enough to save.  Normally
13407          we would not be interested in members here, but there may be
13408          later variables referencing them via DW_AT_specification (for
13409          static members).  */
13410       if (!load_all
13411           && !is_type_tag_for_partial (abbrev->tag)
13412           && abbrev->tag != DW_TAG_constant
13413           && abbrev->tag != DW_TAG_enumerator
13414           && abbrev->tag != DW_TAG_subprogram
13415           && abbrev->tag != DW_TAG_lexical_block
13416           && abbrev->tag != DW_TAG_variable
13417           && abbrev->tag != DW_TAG_namespace
13418           && abbrev->tag != DW_TAG_module
13419           && abbrev->tag != DW_TAG_member
13420           && abbrev->tag != DW_TAG_imported_unit)
13421         {
13422           /* Otherwise we skip to the next sibling, if any.  */
13423           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13424           continue;
13425         }
13426
13427       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13428                                    info_ptr);
13429
13430       /* This two-pass algorithm for processing partial symbols has a
13431          high cost in cache pressure.  Thus, handle some simple cases
13432          here which cover the majority of C partial symbols.  DIEs
13433          which neither have specification tags in them, nor could have
13434          specification tags elsewhere pointing at them, can simply be
13435          processed and discarded.
13436
13437          This segment is also optional; scan_partial_symbols and
13438          add_partial_symbol will handle these DIEs if we chain
13439          them in normally.  When compilers which do not emit large
13440          quantities of duplicate debug information are more common,
13441          this code can probably be removed.  */
13442
13443       /* Any complete simple types at the top level (pretty much all
13444          of them, for a language without namespaces), can be processed
13445          directly.  */
13446       if (parent_die == NULL
13447           && part_die->has_specification == 0
13448           && part_die->is_declaration == 0
13449           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13450               || part_die->tag == DW_TAG_base_type
13451               || part_die->tag == DW_TAG_subrange_type))
13452         {
13453           if (building_psymtab && part_die->name != NULL)
13454             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13455                                  VAR_DOMAIN, LOC_TYPEDEF,
13456                                  &objfile->static_psymbols,
13457                                  0, (CORE_ADDR) 0, cu->language, objfile);
13458           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13459           continue;
13460         }
13461
13462       /* The exception for DW_TAG_typedef with has_children above is
13463          a workaround of GCC PR debug/47510.  In the case of this complaint
13464          type_name_no_tag_or_error will error on such types later.
13465
13466          GDB skipped children of DW_TAG_typedef by the shortcut above and then
13467          it could not find the child DIEs referenced later, this is checked
13468          above.  In correct DWARF DW_TAG_typedef should have no children.  */
13469
13470       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
13471         complaint (&symfile_complaints,
13472                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
13473                      "- DIE at 0x%x [in module %s]"),
13474                    part_die->offset.sect_off, objfile->name);
13475
13476       /* If we're at the second level, and we're an enumerator, and
13477          our parent has no specification (meaning possibly lives in a
13478          namespace elsewhere), then we can add the partial symbol now
13479          instead of queueing it.  */
13480       if (part_die->tag == DW_TAG_enumerator
13481           && parent_die != NULL
13482           && parent_die->die_parent == NULL
13483           && parent_die->tag == DW_TAG_enumeration_type
13484           && parent_die->has_specification == 0)
13485         {
13486           if (part_die->name == NULL)
13487             complaint (&symfile_complaints,
13488                        _("malformed enumerator DIE ignored"));
13489           else if (building_psymtab)
13490             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13491                                  VAR_DOMAIN, LOC_CONST,
13492                                  (cu->language == language_cplus
13493                                   || cu->language == language_java)
13494                                  ? &objfile->global_psymbols
13495                                  : &objfile->static_psymbols,
13496                                  0, (CORE_ADDR) 0, cu->language, objfile);
13497
13498           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13499           continue;
13500         }
13501
13502       /* We'll save this DIE so link it in.  */
13503       part_die->die_parent = parent_die;
13504       part_die->die_sibling = NULL;
13505       part_die->die_child = NULL;
13506
13507       if (last_die && last_die == parent_die)
13508         last_die->die_child = part_die;
13509       else if (last_die)
13510         last_die->die_sibling = part_die;
13511
13512       last_die = part_die;
13513
13514       if (first_die == NULL)
13515         first_die = part_die;
13516
13517       /* Maybe add the DIE to the hash table.  Not all DIEs that we
13518          find interesting need to be in the hash table, because we
13519          also have the parent/sibling/child chains; only those that we
13520          might refer to by offset later during partial symbol reading.
13521
13522          For now this means things that might have be the target of a
13523          DW_AT_specification, DW_AT_abstract_origin, or
13524          DW_AT_extension.  DW_AT_extension will refer only to
13525          namespaces; DW_AT_abstract_origin refers to functions (and
13526          many things under the function DIE, but we do not recurse
13527          into function DIEs during partial symbol reading) and
13528          possibly variables as well; DW_AT_specification refers to
13529          declarations.  Declarations ought to have the DW_AT_declaration
13530          flag.  It happens that GCC forgets to put it in sometimes, but
13531          only for functions, not for types.
13532
13533          Adding more things than necessary to the hash table is harmless
13534          except for the performance cost.  Adding too few will result in
13535          wasted time in find_partial_die, when we reread the compilation
13536          unit with load_all_dies set.  */
13537
13538       if (load_all
13539           || abbrev->tag == DW_TAG_constant
13540           || abbrev->tag == DW_TAG_subprogram
13541           || abbrev->tag == DW_TAG_variable
13542           || abbrev->tag == DW_TAG_namespace
13543           || part_die->is_declaration)
13544         {
13545           void **slot;
13546
13547           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
13548                                            part_die->offset.sect_off, INSERT);
13549           *slot = part_die;
13550         }
13551
13552       part_die = obstack_alloc (&cu->comp_unit_obstack,
13553                                 sizeof (struct partial_die_info));
13554
13555       /* For some DIEs we want to follow their children (if any).  For C
13556          we have no reason to follow the children of structures; for other
13557          languages we have to, so that we can get at method physnames
13558          to infer fully qualified class names, for DW_AT_specification,
13559          and for C++ template arguments.  For C++, we also look one level
13560          inside functions to find template arguments (if the name of the
13561          function does not already contain the template arguments).
13562
13563          For Ada, we need to scan the children of subprograms and lexical
13564          blocks as well because Ada allows the definition of nested
13565          entities that could be interesting for the debugger, such as
13566          nested subprograms for instance.  */
13567       if (last_die->has_children
13568           && (load_all
13569               || last_die->tag == DW_TAG_namespace
13570               || last_die->tag == DW_TAG_module
13571               || last_die->tag == DW_TAG_enumeration_type
13572               || (cu->language == language_cplus
13573                   && last_die->tag == DW_TAG_subprogram
13574                   && (last_die->name == NULL
13575                       || strchr (last_die->name, '<') == NULL))
13576               || (cu->language != language_c
13577                   && (last_die->tag == DW_TAG_class_type
13578                       || last_die->tag == DW_TAG_interface_type
13579                       || last_die->tag == DW_TAG_structure_type
13580                       || last_die->tag == DW_TAG_union_type))
13581               || (cu->language == language_ada
13582                   && (last_die->tag == DW_TAG_subprogram
13583                       || last_die->tag == DW_TAG_lexical_block))))
13584         {
13585           nesting_level++;
13586           parent_die = last_die;
13587           continue;
13588         }
13589
13590       /* Otherwise we skip to the next sibling, if any.  */
13591       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
13592
13593       /* Back to the top, do it again.  */
13594     }
13595 }
13596
13597 /* Read a minimal amount of information into the minimal die structure.  */
13598
13599 static gdb_byte *
13600 read_partial_die (const struct die_reader_specs *reader,
13601                   struct partial_die_info *part_die,
13602                   struct abbrev_info *abbrev, unsigned int abbrev_len,
13603                   gdb_byte *info_ptr)
13604 {
13605   struct dwarf2_cu *cu = reader->cu;
13606   struct objfile *objfile = cu->objfile;
13607   gdb_byte *buffer = reader->buffer;
13608   unsigned int i;
13609   struct attribute attr;
13610   int has_low_pc_attr = 0;
13611   int has_high_pc_attr = 0;
13612   int high_pc_relative = 0;
13613
13614   memset (part_die, 0, sizeof (struct partial_die_info));
13615
13616   part_die->offset.sect_off = info_ptr - buffer;
13617
13618   info_ptr += abbrev_len;
13619
13620   if (abbrev == NULL)
13621     return info_ptr;
13622
13623   part_die->tag = abbrev->tag;
13624   part_die->has_children = abbrev->has_children;
13625
13626   for (i = 0; i < abbrev->num_attrs; ++i)
13627     {
13628       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
13629
13630       /* Store the data if it is of an attribute we want to keep in a
13631          partial symbol table.  */
13632       switch (attr.name)
13633         {
13634         case DW_AT_name:
13635           switch (part_die->tag)
13636             {
13637             case DW_TAG_compile_unit:
13638             case DW_TAG_partial_unit:
13639             case DW_TAG_type_unit:
13640               /* Compilation units have a DW_AT_name that is a filename, not
13641                  a source language identifier.  */
13642             case DW_TAG_enumeration_type:
13643             case DW_TAG_enumerator:
13644               /* These tags always have simple identifiers already; no need
13645                  to canonicalize them.  */
13646               part_die->name = DW_STRING (&attr);
13647               break;
13648             default:
13649               part_die->name
13650                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
13651                                             &objfile->objfile_obstack);
13652               break;
13653             }
13654           break;
13655         case DW_AT_linkage_name:
13656         case DW_AT_MIPS_linkage_name:
13657           /* Note that both forms of linkage name might appear.  We
13658              assume they will be the same, and we only store the last
13659              one we see.  */
13660           if (cu->language == language_ada)
13661             part_die->name = DW_STRING (&attr);
13662           part_die->linkage_name = DW_STRING (&attr);
13663           break;
13664         case DW_AT_low_pc:
13665           has_low_pc_attr = 1;
13666           part_die->lowpc = DW_ADDR (&attr);
13667           break;
13668         case DW_AT_high_pc:
13669           has_high_pc_attr = 1;
13670           if (attr.form == DW_FORM_addr
13671               || attr.form == DW_FORM_GNU_addr_index)
13672             part_die->highpc = DW_ADDR (&attr);
13673           else
13674             {
13675               high_pc_relative = 1;
13676               part_die->highpc = DW_UNSND (&attr);
13677             }
13678           break;
13679         case DW_AT_location:
13680           /* Support the .debug_loc offsets.  */
13681           if (attr_form_is_block (&attr))
13682             {
13683                part_die->d.locdesc = DW_BLOCK (&attr);
13684             }
13685           else if (attr_form_is_section_offset (&attr))
13686             {
13687               dwarf2_complex_location_expr_complaint ();
13688             }
13689           else
13690             {
13691               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13692                                                      "partial symbol information");
13693             }
13694           break;
13695         case DW_AT_external:
13696           part_die->is_external = DW_UNSND (&attr);
13697           break;
13698         case DW_AT_declaration:
13699           part_die->is_declaration = DW_UNSND (&attr);
13700           break;
13701         case DW_AT_type:
13702           part_die->has_type = 1;
13703           break;
13704         case DW_AT_abstract_origin:
13705         case DW_AT_specification:
13706         case DW_AT_extension:
13707           part_die->has_specification = 1;
13708           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
13709           part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13710                                    || cu->per_cu->is_dwz);
13711           break;
13712         case DW_AT_sibling:
13713           /* Ignore absolute siblings, they might point outside of
13714              the current compile unit.  */
13715           if (attr.form == DW_FORM_ref_addr)
13716             complaint (&symfile_complaints,
13717                        _("ignoring absolute DW_AT_sibling"));
13718           else
13719             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
13720           break;
13721         case DW_AT_byte_size:
13722           part_die->has_byte_size = 1;
13723           break;
13724         case DW_AT_calling_convention:
13725           /* DWARF doesn't provide a way to identify a program's source-level
13726              entry point.  DW_AT_calling_convention attributes are only meant
13727              to describe functions' calling conventions.
13728
13729              However, because it's a necessary piece of information in
13730              Fortran, and because DW_CC_program is the only piece of debugging
13731              information whose definition refers to a 'main program' at all,
13732              several compilers have begun marking Fortran main programs with
13733              DW_CC_program --- even when those functions use the standard
13734              calling conventions.
13735
13736              So until DWARF specifies a way to provide this information and
13737              compilers pick up the new representation, we'll support this
13738              practice.  */
13739           if (DW_UNSND (&attr) == DW_CC_program
13740               && cu->language == language_fortran)
13741             {
13742               set_main_name (part_die->name);
13743
13744               /* As this DIE has a static linkage the name would be difficult
13745                  to look up later.  */
13746               language_of_main = language_fortran;
13747             }
13748           break;
13749         case DW_AT_inline:
13750           if (DW_UNSND (&attr) == DW_INL_inlined
13751               || DW_UNSND (&attr) == DW_INL_declared_inlined)
13752             part_die->may_be_inlined = 1;
13753           break;
13754
13755         case DW_AT_import:
13756           if (part_die->tag == DW_TAG_imported_unit)
13757             {
13758               part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
13759               part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
13760                                   || cu->per_cu->is_dwz);
13761             }
13762           break;
13763
13764         default:
13765           break;
13766         }
13767     }
13768
13769   if (high_pc_relative)
13770     part_die->highpc += part_die->lowpc;
13771
13772   if (has_low_pc_attr && has_high_pc_attr)
13773     {
13774       /* When using the GNU linker, .gnu.linkonce. sections are used to
13775          eliminate duplicate copies of functions and vtables and such.
13776          The linker will arbitrarily choose one and discard the others.
13777          The AT_*_pc values for such functions refer to local labels in
13778          these sections.  If the section from that file was discarded, the
13779          labels are not in the output, so the relocs get a value of 0.
13780          If this is a discarded function, mark the pc bounds as invalid,
13781          so that GDB will ignore it.  */
13782       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
13783         {
13784           struct gdbarch *gdbarch = get_objfile_arch (objfile);
13785
13786           complaint (&symfile_complaints,
13787                      _("DW_AT_low_pc %s is zero "
13788                        "for DIE at 0x%x [in module %s]"),
13789                      paddress (gdbarch, part_die->lowpc),
13790                      part_die->offset.sect_off, objfile->name);
13791         }
13792       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
13793       else if (part_die->lowpc >= part_die->highpc)
13794         {
13795           struct gdbarch *gdbarch = get_objfile_arch (objfile);
13796
13797           complaint (&symfile_complaints,
13798                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
13799                        "for DIE at 0x%x [in module %s]"),
13800                      paddress (gdbarch, part_die->lowpc),
13801                      paddress (gdbarch, part_die->highpc),
13802                      part_die->offset.sect_off, objfile->name);
13803         }
13804       else
13805         part_die->has_pc_info = 1;
13806     }
13807
13808   return info_ptr;
13809 }
13810
13811 /* Find a cached partial DIE at OFFSET in CU.  */
13812
13813 static struct partial_die_info *
13814 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
13815 {
13816   struct partial_die_info *lookup_die = NULL;
13817   struct partial_die_info part_die;
13818
13819   part_die.offset = offset;
13820   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
13821                                     offset.sect_off);
13822
13823   return lookup_die;
13824 }
13825
13826 /* Find a partial DIE at OFFSET, which may or may not be in CU,
13827    except in the case of .debug_types DIEs which do not reference
13828    outside their CU (they do however referencing other types via
13829    DW_FORM_ref_sig8).  */
13830
13831 static struct partial_die_info *
13832 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
13833 {
13834   struct objfile *objfile = cu->objfile;
13835   struct dwarf2_per_cu_data *per_cu = NULL;
13836   struct partial_die_info *pd = NULL;
13837
13838   if (offset_in_dwz == cu->per_cu->is_dwz
13839       && offset_in_cu_p (&cu->header, offset))
13840     {
13841       pd = find_partial_die_in_comp_unit (offset, cu);
13842       if (pd != NULL)
13843         return pd;
13844       /* We missed recording what we needed.
13845          Load all dies and try again.  */
13846       per_cu = cu->per_cu;
13847     }
13848   else
13849     {
13850       /* TUs don't reference other CUs/TUs (except via type signatures).  */
13851       if (cu->per_cu->is_debug_types)
13852         {
13853           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
13854                    " external reference to offset 0x%lx [in module %s].\n"),
13855                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
13856                  bfd_get_filename (objfile->obfd));
13857         }
13858       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
13859                                                  objfile);
13860
13861       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
13862         load_partial_comp_unit (per_cu);
13863
13864       per_cu->cu->last_used = 0;
13865       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13866     }
13867
13868   /* If we didn't find it, and not all dies have been loaded,
13869      load them all and try again.  */
13870
13871   if (pd == NULL && per_cu->load_all_dies == 0)
13872     {
13873       per_cu->load_all_dies = 1;
13874
13875       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
13876          THIS_CU->cu may already be in use.  So we can't just free it and
13877          replace its DIEs with the ones we read in.  Instead, we leave those
13878          DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
13879          and clobber THIS_CU->cu->partial_dies with the hash table for the new
13880          set.  */
13881       load_partial_comp_unit (per_cu);
13882
13883       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
13884     }
13885
13886   if (pd == NULL)
13887     internal_error (__FILE__, __LINE__,
13888                     _("could not find partial DIE 0x%x "
13889                       "in cache [from module %s]\n"),
13890                     offset.sect_off, bfd_get_filename (objfile->obfd));
13891   return pd;
13892 }
13893
13894 /* See if we can figure out if the class lives in a namespace.  We do
13895    this by looking for a member function; its demangled name will
13896    contain namespace info, if there is any.  */
13897
13898 static void
13899 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
13900                                   struct dwarf2_cu *cu)
13901 {
13902   /* NOTE: carlton/2003-10-07: Getting the info this way changes
13903      what template types look like, because the demangler
13904      frequently doesn't give the same name as the debug info.  We
13905      could fix this by only using the demangled name to get the
13906      prefix (but see comment in read_structure_type).  */
13907
13908   struct partial_die_info *real_pdi;
13909   struct partial_die_info *child_pdi;
13910
13911   /* If this DIE (this DIE's specification, if any) has a parent, then
13912      we should not do this.  We'll prepend the parent's fully qualified
13913      name when we create the partial symbol.  */
13914
13915   real_pdi = struct_pdi;
13916   while (real_pdi->has_specification)
13917     real_pdi = find_partial_die (real_pdi->spec_offset,
13918                                  real_pdi->spec_is_dwz, cu);
13919
13920   if (real_pdi->die_parent != NULL)
13921     return;
13922
13923   for (child_pdi = struct_pdi->die_child;
13924        child_pdi != NULL;
13925        child_pdi = child_pdi->die_sibling)
13926     {
13927       if (child_pdi->tag == DW_TAG_subprogram
13928           && child_pdi->linkage_name != NULL)
13929         {
13930           char *actual_class_name
13931             = language_class_name_from_physname (cu->language_defn,
13932                                                  child_pdi->linkage_name);
13933           if (actual_class_name != NULL)
13934             {
13935               struct_pdi->name
13936                 = obstack_copy0 (&cu->objfile->objfile_obstack,
13937                                  actual_class_name,
13938                                  strlen (actual_class_name));
13939               xfree (actual_class_name);
13940             }
13941           break;
13942         }
13943     }
13944 }
13945
13946 /* Adjust PART_DIE before generating a symbol for it.  This function
13947    may set the is_external flag or change the DIE's name.  */
13948
13949 static void
13950 fixup_partial_die (struct partial_die_info *part_die,
13951                    struct dwarf2_cu *cu)
13952 {
13953   /* Once we've fixed up a die, there's no point in doing so again.
13954      This also avoids a memory leak if we were to call
13955      guess_partial_die_structure_name multiple times.  */
13956   if (part_die->fixup_called)
13957     return;
13958
13959   /* If we found a reference attribute and the DIE has no name, try
13960      to find a name in the referred to DIE.  */
13961
13962   if (part_die->name == NULL && part_die->has_specification)
13963     {
13964       struct partial_die_info *spec_die;
13965
13966       spec_die = find_partial_die (part_die->spec_offset,
13967                                    part_die->spec_is_dwz, cu);
13968
13969       fixup_partial_die (spec_die, cu);
13970
13971       if (spec_die->name)
13972         {
13973           part_die->name = spec_die->name;
13974
13975           /* Copy DW_AT_external attribute if it is set.  */
13976           if (spec_die->is_external)
13977             part_die->is_external = spec_die->is_external;
13978         }
13979     }
13980
13981   /* Set default names for some unnamed DIEs.  */
13982
13983   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
13984     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
13985
13986   /* If there is no parent die to provide a namespace, and there are
13987      children, see if we can determine the namespace from their linkage
13988      name.  */
13989   if (cu->language == language_cplus
13990       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
13991       && part_die->die_parent == NULL
13992       && part_die->has_children
13993       && (part_die->tag == DW_TAG_class_type
13994           || part_die->tag == DW_TAG_structure_type
13995           || part_die->tag == DW_TAG_union_type))
13996     guess_partial_die_structure_name (part_die, cu);
13997
13998   /* GCC might emit a nameless struct or union that has a linkage
13999      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
14000   if (part_die->name == NULL
14001       && (part_die->tag == DW_TAG_class_type
14002           || part_die->tag == DW_TAG_interface_type
14003           || part_die->tag == DW_TAG_structure_type
14004           || part_die->tag == DW_TAG_union_type)
14005       && part_die->linkage_name != NULL)
14006     {
14007       char *demangled;
14008
14009       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
14010       if (demangled)
14011         {
14012           const char *base;
14013
14014           /* Strip any leading namespaces/classes, keep only the base name.
14015              DW_AT_name for named DIEs does not contain the prefixes.  */
14016           base = strrchr (demangled, ':');
14017           if (base && base > demangled && base[-1] == ':')
14018             base++;
14019           else
14020             base = demangled;
14021
14022           part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14023                                           base, strlen (base));
14024           xfree (demangled);
14025         }
14026     }
14027
14028   part_die->fixup_called = 1;
14029 }
14030
14031 /* Read an attribute value described by an attribute form.  */
14032
14033 static gdb_byte *
14034 read_attribute_value (const struct die_reader_specs *reader,
14035                       struct attribute *attr, unsigned form,
14036                       gdb_byte *info_ptr)
14037 {
14038   struct dwarf2_cu *cu = reader->cu;
14039   bfd *abfd = reader->abfd;
14040   struct comp_unit_head *cu_header = &cu->header;
14041   unsigned int bytes_read;
14042   struct dwarf_block *blk;
14043
14044   attr->form = form;
14045   switch (form)
14046     {
14047     case DW_FORM_ref_addr:
14048       if (cu->header.version == 2)
14049         DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14050       else
14051         DW_UNSND (attr) = read_offset (abfd, info_ptr,
14052                                        &cu->header, &bytes_read);
14053       info_ptr += bytes_read;
14054       break;
14055     case DW_FORM_GNU_ref_alt:
14056       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14057       info_ptr += bytes_read;
14058       break;
14059     case DW_FORM_addr:
14060       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14061       info_ptr += bytes_read;
14062       break;
14063     case DW_FORM_block2:
14064       blk = dwarf_alloc_block (cu);
14065       blk->size = read_2_bytes (abfd, info_ptr);
14066       info_ptr += 2;
14067       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14068       info_ptr += blk->size;
14069       DW_BLOCK (attr) = blk;
14070       break;
14071     case DW_FORM_block4:
14072       blk = dwarf_alloc_block (cu);
14073       blk->size = read_4_bytes (abfd, info_ptr);
14074       info_ptr += 4;
14075       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14076       info_ptr += blk->size;
14077       DW_BLOCK (attr) = blk;
14078       break;
14079     case DW_FORM_data2:
14080       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14081       info_ptr += 2;
14082       break;
14083     case DW_FORM_data4:
14084       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14085       info_ptr += 4;
14086       break;
14087     case DW_FORM_data8:
14088       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14089       info_ptr += 8;
14090       break;
14091     case DW_FORM_sec_offset:
14092       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14093       info_ptr += bytes_read;
14094       break;
14095     case DW_FORM_string:
14096       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14097       DW_STRING_IS_CANONICAL (attr) = 0;
14098       info_ptr += bytes_read;
14099       break;
14100     case DW_FORM_strp:
14101       if (!cu->per_cu->is_dwz)
14102         {
14103           DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14104                                                    &bytes_read);
14105           DW_STRING_IS_CANONICAL (attr) = 0;
14106           info_ptr += bytes_read;
14107           break;
14108         }
14109       /* FALLTHROUGH */
14110     case DW_FORM_GNU_strp_alt:
14111       {
14112         struct dwz_file *dwz = dwarf2_get_dwz_file ();
14113         LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14114                                           &bytes_read);
14115
14116         DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14117         DW_STRING_IS_CANONICAL (attr) = 0;
14118         info_ptr += bytes_read;
14119       }
14120       break;
14121     case DW_FORM_exprloc:
14122     case DW_FORM_block:
14123       blk = dwarf_alloc_block (cu);
14124       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14125       info_ptr += bytes_read;
14126       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14127       info_ptr += blk->size;
14128       DW_BLOCK (attr) = blk;
14129       break;
14130     case DW_FORM_block1:
14131       blk = dwarf_alloc_block (cu);
14132       blk->size = read_1_byte (abfd, info_ptr);
14133       info_ptr += 1;
14134       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14135       info_ptr += blk->size;
14136       DW_BLOCK (attr) = blk;
14137       break;
14138     case DW_FORM_data1:
14139       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14140       info_ptr += 1;
14141       break;
14142     case DW_FORM_flag:
14143       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14144       info_ptr += 1;
14145       break;
14146     case DW_FORM_flag_present:
14147       DW_UNSND (attr) = 1;
14148       break;
14149     case DW_FORM_sdata:
14150       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14151       info_ptr += bytes_read;
14152       break;
14153     case DW_FORM_udata:
14154       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14155       info_ptr += bytes_read;
14156       break;
14157     case DW_FORM_ref1:
14158       DW_UNSND (attr) = (cu->header.offset.sect_off
14159                          + read_1_byte (abfd, info_ptr));
14160       info_ptr += 1;
14161       break;
14162     case DW_FORM_ref2:
14163       DW_UNSND (attr) = (cu->header.offset.sect_off
14164                          + read_2_bytes (abfd, info_ptr));
14165       info_ptr += 2;
14166       break;
14167     case DW_FORM_ref4:
14168       DW_UNSND (attr) = (cu->header.offset.sect_off
14169                          + read_4_bytes (abfd, info_ptr));
14170       info_ptr += 4;
14171       break;
14172     case DW_FORM_ref8:
14173       DW_UNSND (attr) = (cu->header.offset.sect_off
14174                          + read_8_bytes (abfd, info_ptr));
14175       info_ptr += 8;
14176       break;
14177     case DW_FORM_ref_sig8:
14178       /* Convert the signature to something we can record in DW_UNSND
14179          for later lookup.
14180          NOTE: This is NULL if the type wasn't found.  */
14181       DW_SIGNATURED_TYPE (attr) =
14182         lookup_signatured_type (read_8_bytes (abfd, info_ptr));
14183       info_ptr += 8;
14184       break;
14185     case DW_FORM_ref_udata:
14186       DW_UNSND (attr) = (cu->header.offset.sect_off
14187                          + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14188       info_ptr += bytes_read;
14189       break;
14190     case DW_FORM_indirect:
14191       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14192       info_ptr += bytes_read;
14193       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14194       break;
14195     case DW_FORM_GNU_addr_index:
14196       if (reader->dwo_file == NULL)
14197         {
14198           /* For now flag a hard error.
14199              Later we can turn this into a complaint.  */
14200           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14201                  dwarf_form_name (form),
14202                  bfd_get_filename (abfd));
14203         }
14204       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14205       info_ptr += bytes_read;
14206       break;
14207     case DW_FORM_GNU_str_index:
14208       if (reader->dwo_file == NULL)
14209         {
14210           /* For now flag a hard error.
14211              Later we can turn this into a complaint if warranted.  */
14212           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14213                  dwarf_form_name (form),
14214                  bfd_get_filename (abfd));
14215         }
14216       {
14217         ULONGEST str_index =
14218           read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14219
14220         DW_STRING (attr) = read_str_index (reader, cu, str_index);
14221         DW_STRING_IS_CANONICAL (attr) = 0;
14222         info_ptr += bytes_read;
14223       }
14224       break;
14225     default:
14226       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14227              dwarf_form_name (form),
14228              bfd_get_filename (abfd));
14229     }
14230
14231   /* Super hack.  */
14232   if (cu->per_cu->is_dwz && is_ref_attr (attr))
14233     attr->form = DW_FORM_GNU_ref_alt;
14234
14235   /* We have seen instances where the compiler tried to emit a byte
14236      size attribute of -1 which ended up being encoded as an unsigned
14237      0xffffffff.  Although 0xffffffff is technically a valid size value,
14238      an object of this size seems pretty unlikely so we can relatively
14239      safely treat these cases as if the size attribute was invalid and
14240      treat them as zero by default.  */
14241   if (attr->name == DW_AT_byte_size
14242       && form == DW_FORM_data4
14243       && DW_UNSND (attr) >= 0xffffffff)
14244     {
14245       complaint
14246         (&symfile_complaints,
14247          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14248          hex_string (DW_UNSND (attr)));
14249       DW_UNSND (attr) = 0;
14250     }
14251
14252   return info_ptr;
14253 }
14254
14255 /* Read an attribute described by an abbreviated attribute.  */
14256
14257 static gdb_byte *
14258 read_attribute (const struct die_reader_specs *reader,
14259                 struct attribute *attr, struct attr_abbrev *abbrev,
14260                 gdb_byte *info_ptr)
14261 {
14262   attr->name = abbrev->name;
14263   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14264 }
14265
14266 /* Read dwarf information from a buffer.  */
14267
14268 static unsigned int
14269 read_1_byte (bfd *abfd, const gdb_byte *buf)
14270 {
14271   return bfd_get_8 (abfd, buf);
14272 }
14273
14274 static int
14275 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14276 {
14277   return bfd_get_signed_8 (abfd, buf);
14278 }
14279
14280 static unsigned int
14281 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14282 {
14283   return bfd_get_16 (abfd, buf);
14284 }
14285
14286 static int
14287 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14288 {
14289   return bfd_get_signed_16 (abfd, buf);
14290 }
14291
14292 static unsigned int
14293 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14294 {
14295   return bfd_get_32 (abfd, buf);
14296 }
14297
14298 static int
14299 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14300 {
14301   return bfd_get_signed_32 (abfd, buf);
14302 }
14303
14304 static ULONGEST
14305 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14306 {
14307   return bfd_get_64 (abfd, buf);
14308 }
14309
14310 static CORE_ADDR
14311 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
14312               unsigned int *bytes_read)
14313 {
14314   struct comp_unit_head *cu_header = &cu->header;
14315   CORE_ADDR retval = 0;
14316
14317   if (cu_header->signed_addr_p)
14318     {
14319       switch (cu_header->addr_size)
14320         {
14321         case 2:
14322           retval = bfd_get_signed_16 (abfd, buf);
14323           break;
14324         case 4:
14325           retval = bfd_get_signed_32 (abfd, buf);
14326           break;
14327         case 8:
14328           retval = bfd_get_signed_64 (abfd, buf);
14329           break;
14330         default:
14331           internal_error (__FILE__, __LINE__,
14332                           _("read_address: bad switch, signed [in module %s]"),
14333                           bfd_get_filename (abfd));
14334         }
14335     }
14336   else
14337     {
14338       switch (cu_header->addr_size)
14339         {
14340         case 2:
14341           retval = bfd_get_16 (abfd, buf);
14342           break;
14343         case 4:
14344           retval = bfd_get_32 (abfd, buf);
14345           break;
14346         case 8:
14347           retval = bfd_get_64 (abfd, buf);
14348           break;
14349         default:
14350           internal_error (__FILE__, __LINE__,
14351                           _("read_address: bad switch, "
14352                             "unsigned [in module %s]"),
14353                           bfd_get_filename (abfd));
14354         }
14355     }
14356
14357   *bytes_read = cu_header->addr_size;
14358   return retval;
14359 }
14360
14361 /* Read the initial length from a section.  The (draft) DWARF 3
14362    specification allows the initial length to take up either 4 bytes
14363    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
14364    bytes describe the length and all offsets will be 8 bytes in length
14365    instead of 4.
14366
14367    An older, non-standard 64-bit format is also handled by this
14368    function.  The older format in question stores the initial length
14369    as an 8-byte quantity without an escape value.  Lengths greater
14370    than 2^32 aren't very common which means that the initial 4 bytes
14371    is almost always zero.  Since a length value of zero doesn't make
14372    sense for the 32-bit format, this initial zero can be considered to
14373    be an escape value which indicates the presence of the older 64-bit
14374    format.  As written, the code can't detect (old format) lengths
14375    greater than 4GB.  If it becomes necessary to handle lengths
14376    somewhat larger than 4GB, we could allow other small values (such
14377    as the non-sensical values of 1, 2, and 3) to also be used as
14378    escape values indicating the presence of the old format.
14379
14380    The value returned via bytes_read should be used to increment the
14381    relevant pointer after calling read_initial_length().
14382
14383    [ Note:  read_initial_length() and read_offset() are based on the
14384      document entitled "DWARF Debugging Information Format", revision
14385      3, draft 8, dated November 19, 2001.  This document was obtained
14386      from:
14387
14388         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14389
14390      This document is only a draft and is subject to change.  (So beware.)
14391
14392      Details regarding the older, non-standard 64-bit format were
14393      determined empirically by examining 64-bit ELF files produced by
14394      the SGI toolchain on an IRIX 6.5 machine.
14395
14396      - Kevin, July 16, 2002
14397    ] */
14398
14399 static LONGEST
14400 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
14401 {
14402   LONGEST length = bfd_get_32 (abfd, buf);
14403
14404   if (length == 0xffffffff)
14405     {
14406       length = bfd_get_64 (abfd, buf + 4);
14407       *bytes_read = 12;
14408     }
14409   else if (length == 0)
14410     {
14411       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
14412       length = bfd_get_64 (abfd, buf);
14413       *bytes_read = 8;
14414     }
14415   else
14416     {
14417       *bytes_read = 4;
14418     }
14419
14420   return length;
14421 }
14422
14423 /* Cover function for read_initial_length.
14424    Returns the length of the object at BUF, and stores the size of the
14425    initial length in *BYTES_READ and stores the size that offsets will be in
14426    *OFFSET_SIZE.
14427    If the initial length size is not equivalent to that specified in
14428    CU_HEADER then issue a complaint.
14429    This is useful when reading non-comp-unit headers.  */
14430
14431 static LONGEST
14432 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
14433                                         const struct comp_unit_head *cu_header,
14434                                         unsigned int *bytes_read,
14435                                         unsigned int *offset_size)
14436 {
14437   LONGEST length = read_initial_length (abfd, buf, bytes_read);
14438
14439   gdb_assert (cu_header->initial_length_size == 4
14440               || cu_header->initial_length_size == 8
14441               || cu_header->initial_length_size == 12);
14442
14443   if (cu_header->initial_length_size != *bytes_read)
14444     complaint (&symfile_complaints,
14445                _("intermixed 32-bit and 64-bit DWARF sections"));
14446
14447   *offset_size = (*bytes_read == 4) ? 4 : 8;
14448   return length;
14449 }
14450
14451 /* Read an offset from the data stream.  The size of the offset is
14452    given by cu_header->offset_size.  */
14453
14454 static LONGEST
14455 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
14456              unsigned int *bytes_read)
14457 {
14458   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14459
14460   *bytes_read = cu_header->offset_size;
14461   return offset;
14462 }
14463
14464 /* Read an offset from the data stream.  */
14465
14466 static LONGEST
14467 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
14468 {
14469   LONGEST retval = 0;
14470
14471   switch (offset_size)
14472     {
14473     case 4:
14474       retval = bfd_get_32 (abfd, buf);
14475       break;
14476     case 8:
14477       retval = bfd_get_64 (abfd, buf);
14478       break;
14479     default:
14480       internal_error (__FILE__, __LINE__,
14481                       _("read_offset_1: bad switch [in module %s]"),
14482                       bfd_get_filename (abfd));
14483     }
14484
14485   return retval;
14486 }
14487
14488 static gdb_byte *
14489 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
14490 {
14491   /* If the size of a host char is 8 bits, we can return a pointer
14492      to the buffer, otherwise we have to copy the data to a buffer
14493      allocated on the temporary obstack.  */
14494   gdb_assert (HOST_CHAR_BIT == 8);
14495   return buf;
14496 }
14497
14498 static char *
14499 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14500 {
14501   /* If the size of a host char is 8 bits, we can return a pointer
14502      to the string, otherwise we have to copy the string to a buffer
14503      allocated on the temporary obstack.  */
14504   gdb_assert (HOST_CHAR_BIT == 8);
14505   if (*buf == '\0')
14506     {
14507       *bytes_read_ptr = 1;
14508       return NULL;
14509     }
14510   *bytes_read_ptr = strlen ((char *) buf) + 1;
14511   return (char *) buf;
14512 }
14513
14514 static char *
14515 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
14516 {
14517   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
14518   if (dwarf2_per_objfile->str.buffer == NULL)
14519     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
14520            bfd_get_filename (abfd));
14521   if (str_offset >= dwarf2_per_objfile->str.size)
14522     error (_("DW_FORM_strp pointing outside of "
14523              ".debug_str section [in module %s]"),
14524            bfd_get_filename (abfd));
14525   gdb_assert (HOST_CHAR_BIT == 8);
14526   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
14527     return NULL;
14528   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
14529 }
14530
14531 /* Read a string at offset STR_OFFSET in the .debug_str section from
14532    the .dwz file DWZ.  Throw an error if the offset is too large.  If
14533    the string consists of a single NUL byte, return NULL; otherwise
14534    return a pointer to the string.  */
14535
14536 static char *
14537 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
14538 {
14539   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
14540
14541   if (dwz->str.buffer == NULL)
14542     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
14543              "section [in module %s]"),
14544            bfd_get_filename (dwz->dwz_bfd));
14545   if (str_offset >= dwz->str.size)
14546     error (_("DW_FORM_GNU_strp_alt pointing outside of "
14547              ".debug_str section [in module %s]"),
14548            bfd_get_filename (dwz->dwz_bfd));
14549   gdb_assert (HOST_CHAR_BIT == 8);
14550   if (dwz->str.buffer[str_offset] == '\0')
14551     return NULL;
14552   return (char *) (dwz->str.buffer + str_offset);
14553 }
14554
14555 static char *
14556 read_indirect_string (bfd *abfd, gdb_byte *buf,
14557                       const struct comp_unit_head *cu_header,
14558                       unsigned int *bytes_read_ptr)
14559 {
14560   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
14561
14562   return read_indirect_string_at_offset (abfd, str_offset);
14563 }
14564
14565 static ULONGEST
14566 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14567 {
14568   ULONGEST result;
14569   unsigned int num_read;
14570   int i, shift;
14571   unsigned char byte;
14572
14573   result = 0;
14574   shift = 0;
14575   num_read = 0;
14576   i = 0;
14577   while (1)
14578     {
14579       byte = bfd_get_8 (abfd, buf);
14580       buf++;
14581       num_read++;
14582       result |= ((ULONGEST) (byte & 127) << shift);
14583       if ((byte & 128) == 0)
14584         {
14585           break;
14586         }
14587       shift += 7;
14588     }
14589   *bytes_read_ptr = num_read;
14590   return result;
14591 }
14592
14593 static LONGEST
14594 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
14595 {
14596   LONGEST result;
14597   int i, shift, num_read;
14598   unsigned char byte;
14599
14600   result = 0;
14601   shift = 0;
14602   num_read = 0;
14603   i = 0;
14604   while (1)
14605     {
14606       byte = bfd_get_8 (abfd, buf);
14607       buf++;
14608       num_read++;
14609       result |= ((LONGEST) (byte & 127) << shift);
14610       shift += 7;
14611       if ((byte & 128) == 0)
14612         {
14613           break;
14614         }
14615     }
14616   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
14617     result |= -(((LONGEST) 1) << shift);
14618   *bytes_read_ptr = num_read;
14619   return result;
14620 }
14621
14622 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
14623    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
14624    ADDR_SIZE is the size of addresses from the CU header.  */
14625
14626 static CORE_ADDR
14627 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
14628 {
14629   struct objfile *objfile = dwarf2_per_objfile->objfile;
14630   bfd *abfd = objfile->obfd;
14631   const gdb_byte *info_ptr;
14632
14633   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
14634   if (dwarf2_per_objfile->addr.buffer == NULL)
14635     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
14636            objfile->name);
14637   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
14638     error (_("DW_FORM_addr_index pointing outside of "
14639              ".debug_addr section [in module %s]"),
14640            objfile->name);
14641   info_ptr = (dwarf2_per_objfile->addr.buffer
14642               + addr_base + addr_index * addr_size);
14643   if (addr_size == 4)
14644     return bfd_get_32 (abfd, info_ptr);
14645   else
14646     return bfd_get_64 (abfd, info_ptr);
14647 }
14648
14649 /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */
14650
14651 static CORE_ADDR
14652 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
14653 {
14654   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
14655 }
14656
14657 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
14658
14659 static CORE_ADDR
14660 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
14661                              unsigned int *bytes_read)
14662 {
14663   bfd *abfd = cu->objfile->obfd;
14664   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
14665
14666   return read_addr_index (cu, addr_index);
14667 }
14668
14669 /* Data structure to pass results from dwarf2_read_addr_index_reader
14670    back to dwarf2_read_addr_index.  */
14671
14672 struct dwarf2_read_addr_index_data
14673 {
14674   ULONGEST addr_base;
14675   int addr_size;
14676 };
14677
14678 /* die_reader_func for dwarf2_read_addr_index.  */
14679
14680 static void
14681 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
14682                                gdb_byte *info_ptr,
14683                                struct die_info *comp_unit_die,
14684                                int has_children,
14685                                void *data)
14686 {
14687   struct dwarf2_cu *cu = reader->cu;
14688   struct dwarf2_read_addr_index_data *aidata =
14689     (struct dwarf2_read_addr_index_data *) data;
14690
14691   aidata->addr_base = cu->addr_base;
14692   aidata->addr_size = cu->header.addr_size;
14693 }
14694
14695 /* Given an index in .debug_addr, fetch the value.
14696    NOTE: This can be called during dwarf expression evaluation,
14697    long after the debug information has been read, and thus per_cu->cu
14698    may no longer exist.  */
14699
14700 CORE_ADDR
14701 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
14702                         unsigned int addr_index)
14703 {
14704   struct objfile *objfile = per_cu->objfile;
14705   struct dwarf2_cu *cu = per_cu->cu;
14706   ULONGEST addr_base;
14707   int addr_size;
14708
14709   /* This is intended to be called from outside this file.  */
14710   dw2_setup (objfile);
14711
14712   /* We need addr_base and addr_size.
14713      If we don't have PER_CU->cu, we have to get it.
14714      Nasty, but the alternative is storing the needed info in PER_CU,
14715      which at this point doesn't seem justified: it's not clear how frequently
14716      it would get used and it would increase the size of every PER_CU.
14717      Entry points like dwarf2_per_cu_addr_size do a similar thing
14718      so we're not in uncharted territory here.
14719      Alas we need to be a bit more complicated as addr_base is contained
14720      in the DIE.
14721
14722      We don't need to read the entire CU(/TU).
14723      We just need the header and top level die.
14724
14725      IWBN to use the aging mechanism to let us lazily later discard the CU.
14726      For now we skip this optimization.  */
14727
14728   if (cu != NULL)
14729     {
14730       addr_base = cu->addr_base;
14731       addr_size = cu->header.addr_size;
14732     }
14733   else
14734     {
14735       struct dwarf2_read_addr_index_data aidata;
14736
14737       /* Note: We can't use init_cutu_and_read_dies_simple here,
14738          we need addr_base.  */
14739       init_cutu_and_read_dies (per_cu, NULL, 0, 0,
14740                                dwarf2_read_addr_index_reader, &aidata);
14741       addr_base = aidata.addr_base;
14742       addr_size = aidata.addr_size;
14743     }
14744
14745   return read_addr_index_1 (addr_index, addr_base, addr_size);
14746 }
14747
14748 /* Given a DW_AT_str_index, fetch the string.  */
14749
14750 static char *
14751 read_str_index (const struct die_reader_specs *reader,
14752                 struct dwarf2_cu *cu, ULONGEST str_index)
14753 {
14754   struct objfile *objfile = dwarf2_per_objfile->objfile;
14755   const char *dwo_name = objfile->name;
14756   bfd *abfd = objfile->obfd;
14757   struct dwo_sections *sections = &reader->dwo_file->sections;
14758   gdb_byte *info_ptr;
14759   ULONGEST str_offset;
14760
14761   dwarf2_read_section (objfile, &sections->str);
14762   dwarf2_read_section (objfile, &sections->str_offsets);
14763   if (sections->str.buffer == NULL)
14764     error (_("DW_FORM_str_index used without .debug_str.dwo section"
14765              " in CU at offset 0x%lx [in module %s]"),
14766            (long) cu->header.offset.sect_off, dwo_name);
14767   if (sections->str_offsets.buffer == NULL)
14768     error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
14769              " in CU at offset 0x%lx [in module %s]"),
14770            (long) cu->header.offset.sect_off, dwo_name);
14771   if (str_index * cu->header.offset_size >= sections->str_offsets.size)
14772     error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
14773              " section in CU at offset 0x%lx [in module %s]"),
14774            (long) cu->header.offset.sect_off, dwo_name);
14775   info_ptr = (sections->str_offsets.buffer
14776               + str_index * cu->header.offset_size);
14777   if (cu->header.offset_size == 4)
14778     str_offset = bfd_get_32 (abfd, info_ptr);
14779   else
14780     str_offset = bfd_get_64 (abfd, info_ptr);
14781   if (str_offset >= sections->str.size)
14782     error (_("Offset from DW_FORM_str_index pointing outside of"
14783              " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
14784            (long) cu->header.offset.sect_off, dwo_name);
14785   return (char *) (sections->str.buffer + str_offset);
14786 }
14787
14788 /* Return the length of an LEB128 number in BUF.  */
14789
14790 static int
14791 leb128_size (const gdb_byte *buf)
14792 {
14793   const gdb_byte *begin = buf;
14794   gdb_byte byte;
14795
14796   while (1)
14797     {
14798       byte = *buf++;
14799       if ((byte & 128) == 0)
14800         return buf - begin;
14801     }
14802 }
14803
14804 static void
14805 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
14806 {
14807   switch (lang)
14808     {
14809     case DW_LANG_C89:
14810     case DW_LANG_C99:
14811     case DW_LANG_C:
14812       cu->language = language_c;
14813       break;
14814     case DW_LANG_C_plus_plus:
14815       cu->language = language_cplus;
14816       break;
14817     case DW_LANG_D:
14818       cu->language = language_d;
14819       break;
14820     case DW_LANG_Fortran77:
14821     case DW_LANG_Fortran90:
14822     case DW_LANG_Fortran95:
14823       cu->language = language_fortran;
14824       break;
14825     case DW_LANG_Go:
14826       cu->language = language_go;
14827       break;
14828     case DW_LANG_Mips_Assembler:
14829       cu->language = language_asm;
14830       break;
14831     case DW_LANG_Java:
14832       cu->language = language_java;
14833       break;
14834     case DW_LANG_Ada83:
14835     case DW_LANG_Ada95:
14836       cu->language = language_ada;
14837       break;
14838     case DW_LANG_Modula2:
14839       cu->language = language_m2;
14840       break;
14841     case DW_LANG_Pascal83:
14842       cu->language = language_pascal;
14843       break;
14844     case DW_LANG_ObjC:
14845       cu->language = language_objc;
14846       break;
14847     case DW_LANG_Cobol74:
14848     case DW_LANG_Cobol85:
14849     default:
14850       cu->language = language_minimal;
14851       break;
14852     }
14853   cu->language_defn = language_def (cu->language);
14854 }
14855
14856 /* Return the named attribute or NULL if not there.  */
14857
14858 static struct attribute *
14859 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
14860 {
14861   for (;;)
14862     {
14863       unsigned int i;
14864       struct attribute *spec = NULL;
14865
14866       for (i = 0; i < die->num_attrs; ++i)
14867         {
14868           if (die->attrs[i].name == name)
14869             return &die->attrs[i];
14870           if (die->attrs[i].name == DW_AT_specification
14871               || die->attrs[i].name == DW_AT_abstract_origin)
14872             spec = &die->attrs[i];
14873         }
14874
14875       if (!spec)
14876         break;
14877
14878       die = follow_die_ref (die, spec, &cu);
14879     }
14880
14881   return NULL;
14882 }
14883
14884 /* Return the named attribute or NULL if not there,
14885    but do not follow DW_AT_specification, etc.
14886    This is for use in contexts where we're reading .debug_types dies.
14887    Following DW_AT_specification, DW_AT_abstract_origin will take us
14888    back up the chain, and we want to go down.  */
14889
14890 static struct attribute *
14891 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
14892 {
14893   unsigned int i;
14894
14895   for (i = 0; i < die->num_attrs; ++i)
14896     if (die->attrs[i].name == name)
14897       return &die->attrs[i];
14898
14899   return NULL;
14900 }
14901
14902 /* Return non-zero iff the attribute NAME is defined for the given DIE,
14903    and holds a non-zero value.  This function should only be used for
14904    DW_FORM_flag or DW_FORM_flag_present attributes.  */
14905
14906 static int
14907 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
14908 {
14909   struct attribute *attr = dwarf2_attr (die, name, cu);
14910
14911   return (attr && DW_UNSND (attr));
14912 }
14913
14914 static int
14915 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
14916 {
14917   /* A DIE is a declaration if it has a DW_AT_declaration attribute
14918      which value is non-zero.  However, we have to be careful with
14919      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
14920      (via dwarf2_flag_true_p) follows this attribute.  So we may
14921      end up accidently finding a declaration attribute that belongs
14922      to a different DIE referenced by the specification attribute,
14923      even though the given DIE does not have a declaration attribute.  */
14924   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
14925           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
14926 }
14927
14928 /* Return the die giving the specification for DIE, if there is
14929    one.  *SPEC_CU is the CU containing DIE on input, and the CU
14930    containing the return value on output.  If there is no
14931    specification, but there is an abstract origin, that is
14932    returned.  */
14933
14934 static struct die_info *
14935 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
14936 {
14937   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
14938                                              *spec_cu);
14939
14940   if (spec_attr == NULL)
14941     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
14942
14943   if (spec_attr == NULL)
14944     return NULL;
14945   else
14946     return follow_die_ref (die, spec_attr, spec_cu);
14947 }
14948
14949 /* Free the line_header structure *LH, and any arrays and strings it
14950    refers to.
14951    NOTE: This is also used as a "cleanup" function.  */
14952
14953 static void
14954 free_line_header (struct line_header *lh)
14955 {
14956   if (lh->standard_opcode_lengths)
14957     xfree (lh->standard_opcode_lengths);
14958
14959   /* Remember that all the lh->file_names[i].name pointers are
14960      pointers into debug_line_buffer, and don't need to be freed.  */
14961   if (lh->file_names)
14962     xfree (lh->file_names);
14963
14964   /* Similarly for the include directory names.  */
14965   if (lh->include_dirs)
14966     xfree (lh->include_dirs);
14967
14968   xfree (lh);
14969 }
14970
14971 /* Add an entry to LH's include directory table.  */
14972
14973 static void
14974 add_include_dir (struct line_header *lh, char *include_dir)
14975 {
14976   /* Grow the array if necessary.  */
14977   if (lh->include_dirs_size == 0)
14978     {
14979       lh->include_dirs_size = 1; /* for testing */
14980       lh->include_dirs = xmalloc (lh->include_dirs_size
14981                                   * sizeof (*lh->include_dirs));
14982     }
14983   else if (lh->num_include_dirs >= lh->include_dirs_size)
14984     {
14985       lh->include_dirs_size *= 2;
14986       lh->include_dirs = xrealloc (lh->include_dirs,
14987                                    (lh->include_dirs_size
14988                                     * sizeof (*lh->include_dirs)));
14989     }
14990
14991   lh->include_dirs[lh->num_include_dirs++] = include_dir;
14992 }
14993
14994 /* Add an entry to LH's file name table.  */
14995
14996 static void
14997 add_file_name (struct line_header *lh,
14998                char *name,
14999                unsigned int dir_index,
15000                unsigned int mod_time,
15001                unsigned int length)
15002 {
15003   struct file_entry *fe;
15004
15005   /* Grow the array if necessary.  */
15006   if (lh->file_names_size == 0)
15007     {
15008       lh->file_names_size = 1; /* for testing */
15009       lh->file_names = xmalloc (lh->file_names_size
15010                                 * sizeof (*lh->file_names));
15011     }
15012   else if (lh->num_file_names >= lh->file_names_size)
15013     {
15014       lh->file_names_size *= 2;
15015       lh->file_names = xrealloc (lh->file_names,
15016                                  (lh->file_names_size
15017                                   * sizeof (*lh->file_names)));
15018     }
15019
15020   fe = &lh->file_names[lh->num_file_names++];
15021   fe->name = name;
15022   fe->dir_index = dir_index;
15023   fe->mod_time = mod_time;
15024   fe->length = length;
15025   fe->included_p = 0;
15026   fe->symtab = NULL;
15027 }
15028
15029 /* A convenience function to find the proper .debug_line section for a
15030    CU.  */
15031
15032 static struct dwarf2_section_info *
15033 get_debug_line_section (struct dwarf2_cu *cu)
15034 {
15035   struct dwarf2_section_info *section;
15036
15037   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15038      DWO file.  */
15039   if (cu->dwo_unit && cu->per_cu->is_debug_types)
15040     section = &cu->dwo_unit->dwo_file->sections.line;
15041   else if (cu->per_cu->is_dwz)
15042     {
15043       struct dwz_file *dwz = dwarf2_get_dwz_file ();
15044
15045       section = &dwz->line;
15046     }
15047   else
15048     section = &dwarf2_per_objfile->line;
15049
15050   return section;
15051 }
15052
15053 /* Read the statement program header starting at OFFSET in
15054    .debug_line, or .debug_line.dwo.  Return a pointer
15055    to a struct line_header, allocated using xmalloc.
15056
15057    NOTE: the strings in the include directory and file name tables of
15058    the returned object point into the dwarf line section buffer,
15059    and must not be freed.  */
15060
15061 static struct line_header *
15062 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15063 {
15064   struct cleanup *back_to;
15065   struct line_header *lh;
15066   gdb_byte *line_ptr;
15067   unsigned int bytes_read, offset_size;
15068   int i;
15069   char *cur_dir, *cur_file;
15070   struct dwarf2_section_info *section;
15071   bfd *abfd;
15072
15073   section = get_debug_line_section (cu);
15074   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15075   if (section->buffer == NULL)
15076     {
15077       if (cu->dwo_unit && cu->per_cu->is_debug_types)
15078         complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15079       else
15080         complaint (&symfile_complaints, _("missing .debug_line section"));
15081       return 0;
15082     }
15083
15084   /* We can't do this until we know the section is non-empty.
15085      Only then do we know we have such a section.  */
15086   abfd = section->asection->owner;
15087
15088   /* Make sure that at least there's room for the total_length field.
15089      That could be 12 bytes long, but we're just going to fudge that.  */
15090   if (offset + 4 >= section->size)
15091     {
15092       dwarf2_statement_list_fits_in_line_number_section_complaint ();
15093       return 0;
15094     }
15095
15096   lh = xmalloc (sizeof (*lh));
15097   memset (lh, 0, sizeof (*lh));
15098   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15099                           (void *) lh);
15100
15101   line_ptr = section->buffer + offset;
15102
15103   /* Read in the header.  */
15104   lh->total_length =
15105     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15106                                             &bytes_read, &offset_size);
15107   line_ptr += bytes_read;
15108   if (line_ptr + lh->total_length > (section->buffer + section->size))
15109     {
15110       dwarf2_statement_list_fits_in_line_number_section_complaint ();
15111       return 0;
15112     }
15113   lh->statement_program_end = line_ptr + lh->total_length;
15114   lh->version = read_2_bytes (abfd, line_ptr);
15115   line_ptr += 2;
15116   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15117   line_ptr += offset_size;
15118   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15119   line_ptr += 1;
15120   if (lh->version >= 4)
15121     {
15122       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15123       line_ptr += 1;
15124     }
15125   else
15126     lh->maximum_ops_per_instruction = 1;
15127
15128   if (lh->maximum_ops_per_instruction == 0)
15129     {
15130       lh->maximum_ops_per_instruction = 1;
15131       complaint (&symfile_complaints,
15132                  _("invalid maximum_ops_per_instruction "
15133                    "in `.debug_line' section"));
15134     }
15135
15136   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15137   line_ptr += 1;
15138   lh->line_base = read_1_signed_byte (abfd, line_ptr);
15139   line_ptr += 1;
15140   lh->line_range = read_1_byte (abfd, line_ptr);
15141   line_ptr += 1;
15142   lh->opcode_base = read_1_byte (abfd, line_ptr);
15143   line_ptr += 1;
15144   lh->standard_opcode_lengths
15145     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15146
15147   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
15148   for (i = 1; i < lh->opcode_base; ++i)
15149     {
15150       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15151       line_ptr += 1;
15152     }
15153
15154   /* Read directory table.  */
15155   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15156     {
15157       line_ptr += bytes_read;
15158       add_include_dir (lh, cur_dir);
15159     }
15160   line_ptr += bytes_read;
15161
15162   /* Read file name table.  */
15163   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15164     {
15165       unsigned int dir_index, mod_time, length;
15166
15167       line_ptr += bytes_read;
15168       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15169       line_ptr += bytes_read;
15170       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15171       line_ptr += bytes_read;
15172       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15173       line_ptr += bytes_read;
15174
15175       add_file_name (lh, cur_file, dir_index, mod_time, length);
15176     }
15177   line_ptr += bytes_read;
15178   lh->statement_program_start = line_ptr;
15179
15180   if (line_ptr > (section->buffer + section->size))
15181     complaint (&symfile_complaints,
15182                _("line number info header doesn't "
15183                  "fit in `.debug_line' section"));
15184
15185   discard_cleanups (back_to);
15186   return lh;
15187 }
15188
15189 /* Subroutine of dwarf_decode_lines to simplify it.
15190    Return the file name of the psymtab for included file FILE_INDEX
15191    in line header LH of PST.
15192    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15193    If space for the result is malloc'd, it will be freed by a cleanup.
15194    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15195
15196    The function creates dangling cleanup registration.  */
15197
15198 static char *
15199 psymtab_include_file_name (const struct line_header *lh, int file_index,
15200                            const struct partial_symtab *pst,
15201                            const char *comp_dir)
15202 {
15203   const struct file_entry fe = lh->file_names [file_index];
15204   char *include_name = fe.name;
15205   char *include_name_to_compare = include_name;
15206   char *dir_name = NULL;
15207   const char *pst_filename;
15208   char *copied_name = NULL;
15209   int file_is_pst;
15210
15211   if (fe.dir_index)
15212     dir_name = lh->include_dirs[fe.dir_index - 1];
15213
15214   if (!IS_ABSOLUTE_PATH (include_name)
15215       && (dir_name != NULL || comp_dir != NULL))
15216     {
15217       /* Avoid creating a duplicate psymtab for PST.
15218          We do this by comparing INCLUDE_NAME and PST_FILENAME.
15219          Before we do the comparison, however, we need to account
15220          for DIR_NAME and COMP_DIR.
15221          First prepend dir_name (if non-NULL).  If we still don't
15222          have an absolute path prepend comp_dir (if non-NULL).
15223          However, the directory we record in the include-file's
15224          psymtab does not contain COMP_DIR (to match the
15225          corresponding symtab(s)).
15226
15227          Example:
15228
15229          bash$ cd /tmp
15230          bash$ gcc -g ./hello.c
15231          include_name = "hello.c"
15232          dir_name = "."
15233          DW_AT_comp_dir = comp_dir = "/tmp"
15234          DW_AT_name = "./hello.c"  */
15235
15236       if (dir_name != NULL)
15237         {
15238           include_name = concat (dir_name, SLASH_STRING,
15239                                  include_name, (char *)NULL);
15240           include_name_to_compare = include_name;
15241           make_cleanup (xfree, include_name);
15242         }
15243       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15244         {
15245           include_name_to_compare = concat (comp_dir, SLASH_STRING,
15246                                             include_name, (char *)NULL);
15247         }
15248     }
15249
15250   pst_filename = pst->filename;
15251   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15252     {
15253       copied_name = concat (pst->dirname, SLASH_STRING,
15254                             pst_filename, (char *)NULL);
15255       pst_filename = copied_name;
15256     }
15257
15258   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15259
15260   if (include_name_to_compare != include_name)
15261     xfree (include_name_to_compare);
15262   if (copied_name != NULL)
15263     xfree (copied_name);
15264
15265   if (file_is_pst)
15266     return NULL;
15267   return include_name;
15268 }
15269
15270 /* Ignore this record_line request.  */
15271
15272 static void
15273 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15274 {
15275   return;
15276 }
15277
15278 /* Subroutine of dwarf_decode_lines to simplify it.
15279    Process the line number information in LH.  */
15280
15281 static void
15282 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15283                       struct dwarf2_cu *cu, struct partial_symtab *pst)
15284 {
15285   gdb_byte *line_ptr, *extended_end;
15286   gdb_byte *line_end;
15287   unsigned int bytes_read, extended_len;
15288   unsigned char op_code, extended_op, adj_opcode;
15289   CORE_ADDR baseaddr;
15290   struct objfile *objfile = cu->objfile;
15291   bfd *abfd = objfile->obfd;
15292   struct gdbarch *gdbarch = get_objfile_arch (objfile);
15293   const int decode_for_pst_p = (pst != NULL);
15294   struct subfile *last_subfile = NULL;
15295   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15296     = record_line;
15297
15298   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15299
15300   line_ptr = lh->statement_program_start;
15301   line_end = lh->statement_program_end;
15302
15303   /* Read the statement sequences until there's nothing left.  */
15304   while (line_ptr < line_end)
15305     {
15306       /* state machine registers  */
15307       CORE_ADDR address = 0;
15308       unsigned int file = 1;
15309       unsigned int line = 1;
15310       unsigned int column = 0;
15311       int is_stmt = lh->default_is_stmt;
15312       int basic_block = 0;
15313       int end_sequence = 0;
15314       CORE_ADDR addr;
15315       unsigned char op_index = 0;
15316
15317       if (!decode_for_pst_p && lh->num_file_names >= file)
15318         {
15319           /* Start a subfile for the current file of the state machine.  */
15320           /* lh->include_dirs and lh->file_names are 0-based, but the
15321              directory and file name numbers in the statement program
15322              are 1-based.  */
15323           struct file_entry *fe = &lh->file_names[file - 1];
15324           char *dir = NULL;
15325
15326           if (fe->dir_index)
15327             dir = lh->include_dirs[fe->dir_index - 1];
15328
15329           dwarf2_start_subfile (fe->name, dir, comp_dir);
15330         }
15331
15332       /* Decode the table.  */
15333       while (!end_sequence)
15334         {
15335           op_code = read_1_byte (abfd, line_ptr);
15336           line_ptr += 1;
15337           if (line_ptr > line_end)
15338             {
15339               dwarf2_debug_line_missing_end_sequence_complaint ();
15340               break;
15341             }
15342
15343           if (op_code >= lh->opcode_base)
15344             {
15345               /* Special operand.  */
15346               adj_opcode = op_code - lh->opcode_base;
15347               address += (((op_index + (adj_opcode / lh->line_range))
15348                            / lh->maximum_ops_per_instruction)
15349                           * lh->minimum_instruction_length);
15350               op_index = ((op_index + (adj_opcode / lh->line_range))
15351                           % lh->maximum_ops_per_instruction);
15352               line += lh->line_base + (adj_opcode % lh->line_range);
15353               if (lh->num_file_names < file || file == 0)
15354                 dwarf2_debug_line_missing_file_complaint ();
15355               /* For now we ignore lines not starting on an
15356                  instruction boundary.  */
15357               else if (op_index == 0)
15358                 {
15359                   lh->file_names[file - 1].included_p = 1;
15360                   if (!decode_for_pst_p && is_stmt)
15361                     {
15362                       if (last_subfile != current_subfile)
15363                         {
15364                           addr = gdbarch_addr_bits_remove (gdbarch, address);
15365                           if (last_subfile)
15366                             (*p_record_line) (last_subfile, 0, addr);
15367                           last_subfile = current_subfile;
15368                         }
15369                       /* Append row to matrix using current values.  */
15370                       addr = gdbarch_addr_bits_remove (gdbarch, address);
15371                       (*p_record_line) (current_subfile, line, addr);
15372                     }
15373                 }
15374               basic_block = 0;
15375             }
15376           else switch (op_code)
15377             {
15378             case DW_LNS_extended_op:
15379               extended_len = read_unsigned_leb128 (abfd, line_ptr,
15380                                                    &bytes_read);
15381               line_ptr += bytes_read;
15382               extended_end = line_ptr + extended_len;
15383               extended_op = read_1_byte (abfd, line_ptr);
15384               line_ptr += 1;
15385               switch (extended_op)
15386                 {
15387                 case DW_LNE_end_sequence:
15388                   p_record_line = record_line;
15389                   end_sequence = 1;
15390                   break;
15391                 case DW_LNE_set_address:
15392                   address = read_address (abfd, line_ptr, cu, &bytes_read);
15393
15394                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15395                     {
15396                       /* This line table is for a function which has been
15397                          GCd by the linker.  Ignore it.  PR gdb/12528 */
15398
15399                       long line_offset
15400                         = line_ptr - get_debug_line_section (cu)->buffer;
15401
15402                       complaint (&symfile_complaints,
15403                                  _(".debug_line address at offset 0x%lx is 0 "
15404                                    "[in module %s]"),
15405                                  line_offset, objfile->name);
15406                       p_record_line = noop_record_line;
15407                     }
15408
15409                   op_index = 0;
15410                   line_ptr += bytes_read;
15411                   address += baseaddr;
15412                   break;
15413                 case DW_LNE_define_file:
15414                   {
15415                     char *cur_file;
15416                     unsigned int dir_index, mod_time, length;
15417
15418                     cur_file = read_direct_string (abfd, line_ptr,
15419                                                    &bytes_read);
15420                     line_ptr += bytes_read;
15421                     dir_index =
15422                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15423                     line_ptr += bytes_read;
15424                     mod_time =
15425                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15426                     line_ptr += bytes_read;
15427                     length =
15428                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15429                     line_ptr += bytes_read;
15430                     add_file_name (lh, cur_file, dir_index, mod_time, length);
15431                   }
15432                   break;
15433                 case DW_LNE_set_discriminator:
15434                   /* The discriminator is not interesting to the debugger;
15435                      just ignore it.  */
15436                   line_ptr = extended_end;
15437                   break;
15438                 default:
15439                   complaint (&symfile_complaints,
15440                              _("mangled .debug_line section"));
15441                   return;
15442                 }
15443               /* Make sure that we parsed the extended op correctly.  If e.g.
15444                  we expected a different address size than the producer used,
15445                  we may have read the wrong number of bytes.  */
15446               if (line_ptr != extended_end)
15447                 {
15448                   complaint (&symfile_complaints,
15449                              _("mangled .debug_line section"));
15450                   return;
15451                 }
15452               break;
15453             case DW_LNS_copy:
15454               if (lh->num_file_names < file || file == 0)
15455                 dwarf2_debug_line_missing_file_complaint ();
15456               else
15457                 {
15458                   lh->file_names[file - 1].included_p = 1;
15459                   if (!decode_for_pst_p && is_stmt)
15460                     {
15461                       if (last_subfile != current_subfile)
15462                         {
15463                           addr = gdbarch_addr_bits_remove (gdbarch, address);
15464                           if (last_subfile)
15465                             (*p_record_line) (last_subfile, 0, addr);
15466                           last_subfile = current_subfile;
15467                         }
15468                       addr = gdbarch_addr_bits_remove (gdbarch, address);
15469                       (*p_record_line) (current_subfile, line, addr);
15470                     }
15471                 }
15472               basic_block = 0;
15473               break;
15474             case DW_LNS_advance_pc:
15475               {
15476                 CORE_ADDR adjust
15477                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15478
15479                 address += (((op_index + adjust)
15480                              / lh->maximum_ops_per_instruction)
15481                             * lh->minimum_instruction_length);
15482                 op_index = ((op_index + adjust)
15483                             % lh->maximum_ops_per_instruction);
15484                 line_ptr += bytes_read;
15485               }
15486               break;
15487             case DW_LNS_advance_line:
15488               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
15489               line_ptr += bytes_read;
15490               break;
15491             case DW_LNS_set_file:
15492               {
15493                 /* The arrays lh->include_dirs and lh->file_names are
15494                    0-based, but the directory and file name numbers in
15495                    the statement program are 1-based.  */
15496                 struct file_entry *fe;
15497                 char *dir = NULL;
15498
15499                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15500                 line_ptr += bytes_read;
15501                 if (lh->num_file_names < file || file == 0)
15502                   dwarf2_debug_line_missing_file_complaint ();
15503                 else
15504                   {
15505                     fe = &lh->file_names[file - 1];
15506                     if (fe->dir_index)
15507                       dir = lh->include_dirs[fe->dir_index - 1];
15508                     if (!decode_for_pst_p)
15509                       {
15510                         last_subfile = current_subfile;
15511                         dwarf2_start_subfile (fe->name, dir, comp_dir);
15512                       }
15513                   }
15514               }
15515               break;
15516             case DW_LNS_set_column:
15517               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15518               line_ptr += bytes_read;
15519               break;
15520             case DW_LNS_negate_stmt:
15521               is_stmt = (!is_stmt);
15522               break;
15523             case DW_LNS_set_basic_block:
15524               basic_block = 1;
15525               break;
15526             /* Add to the address register of the state machine the
15527                address increment value corresponding to special opcode
15528                255.  I.e., this value is scaled by the minimum
15529                instruction length since special opcode 255 would have
15530                scaled the increment.  */
15531             case DW_LNS_const_add_pc:
15532               {
15533                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
15534
15535                 address += (((op_index + adjust)
15536                              / lh->maximum_ops_per_instruction)
15537                             * lh->minimum_instruction_length);
15538                 op_index = ((op_index + adjust)
15539                             % lh->maximum_ops_per_instruction);
15540               }
15541               break;
15542             case DW_LNS_fixed_advance_pc:
15543               address += read_2_bytes (abfd, line_ptr);
15544               op_index = 0;
15545               line_ptr += 2;
15546               break;
15547             default:
15548               {
15549                 /* Unknown standard opcode, ignore it.  */
15550                 int i;
15551
15552                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
15553                   {
15554                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15555                     line_ptr += bytes_read;
15556                   }
15557               }
15558             }
15559         }
15560       if (lh->num_file_names < file || file == 0)
15561         dwarf2_debug_line_missing_file_complaint ();
15562       else
15563         {
15564           lh->file_names[file - 1].included_p = 1;
15565           if (!decode_for_pst_p)
15566             {
15567               addr = gdbarch_addr_bits_remove (gdbarch, address);
15568               (*p_record_line) (current_subfile, 0, addr);
15569             }
15570         }
15571     }
15572 }
15573
15574 /* Decode the Line Number Program (LNP) for the given line_header
15575    structure and CU.  The actual information extracted and the type
15576    of structures created from the LNP depends on the value of PST.
15577
15578    1. If PST is NULL, then this procedure uses the data from the program
15579       to create all necessary symbol tables, and their linetables.
15580
15581    2. If PST is not NULL, this procedure reads the program to determine
15582       the list of files included by the unit represented by PST, and
15583       builds all the associated partial symbol tables.
15584
15585    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15586    It is used for relative paths in the line table.
15587    NOTE: When processing partial symtabs (pst != NULL),
15588    comp_dir == pst->dirname.
15589
15590    NOTE: It is important that psymtabs have the same file name (via strcmp)
15591    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
15592    symtab we don't use it in the name of the psymtabs we create.
15593    E.g. expand_line_sal requires this when finding psymtabs to expand.
15594    A good testcase for this is mb-inline.exp.  */
15595
15596 static void
15597 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
15598                     struct dwarf2_cu *cu, struct partial_symtab *pst,
15599                     int want_line_info)
15600 {
15601   struct objfile *objfile = cu->objfile;
15602   const int decode_for_pst_p = (pst != NULL);
15603   struct subfile *first_subfile = current_subfile;
15604
15605   if (want_line_info)
15606     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
15607
15608   if (decode_for_pst_p)
15609     {
15610       int file_index;
15611
15612       /* Now that we're done scanning the Line Header Program, we can
15613          create the psymtab of each included file.  */
15614       for (file_index = 0; file_index < lh->num_file_names; file_index++)
15615         if (lh->file_names[file_index].included_p == 1)
15616           {
15617             char *include_name =
15618               psymtab_include_file_name (lh, file_index, pst, comp_dir);
15619             if (include_name != NULL)
15620               dwarf2_create_include_psymtab (include_name, pst, objfile);
15621           }
15622     }
15623   else
15624     {
15625       /* Make sure a symtab is created for every file, even files
15626          which contain only variables (i.e. no code with associated
15627          line numbers).  */
15628       int i;
15629
15630       for (i = 0; i < lh->num_file_names; i++)
15631         {
15632           char *dir = NULL;
15633           struct file_entry *fe;
15634
15635           fe = &lh->file_names[i];
15636           if (fe->dir_index)
15637             dir = lh->include_dirs[fe->dir_index - 1];
15638           dwarf2_start_subfile (fe->name, dir, comp_dir);
15639
15640           /* Skip the main file; we don't need it, and it must be
15641              allocated last, so that it will show up before the
15642              non-primary symtabs in the objfile's symtab list.  */
15643           if (current_subfile == first_subfile)
15644             continue;
15645
15646           if (current_subfile->symtab == NULL)
15647             current_subfile->symtab = allocate_symtab (current_subfile->name,
15648                                                        objfile);
15649           fe->symtab = current_subfile->symtab;
15650         }
15651     }
15652 }
15653
15654 /* Start a subfile for DWARF.  FILENAME is the name of the file and
15655    DIRNAME the name of the source directory which contains FILENAME
15656    or NULL if not known.  COMP_DIR is the compilation directory for the
15657    linetable's compilation unit or NULL if not known.
15658    This routine tries to keep line numbers from identical absolute and
15659    relative file names in a common subfile.
15660
15661    Using the `list' example from the GDB testsuite, which resides in
15662    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
15663    of /srcdir/list0.c yields the following debugging information for list0.c:
15664
15665    DW_AT_name:          /srcdir/list0.c
15666    DW_AT_comp_dir:              /compdir
15667    files.files[0].name: list0.h
15668    files.files[0].dir:  /srcdir
15669    files.files[1].name: list0.c
15670    files.files[1].dir:  /srcdir
15671
15672    The line number information for list0.c has to end up in a single
15673    subfile, so that `break /srcdir/list0.c:1' works as expected.
15674    start_subfile will ensure that this happens provided that we pass the
15675    concatenation of files.files[1].dir and files.files[1].name as the
15676    subfile's name.  */
15677
15678 static void
15679 dwarf2_start_subfile (char *filename, const char *dirname,
15680                       const char *comp_dir)
15681 {
15682   char *fullname;
15683
15684   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
15685      `start_symtab' will always pass the contents of DW_AT_comp_dir as
15686      second argument to start_subfile.  To be consistent, we do the
15687      same here.  In order not to lose the line information directory,
15688      we concatenate it to the filename when it makes sense.
15689      Note that the Dwarf3 standard says (speaking of filenames in line
15690      information): ``The directory index is ignored for file names
15691      that represent full path names''.  Thus ignoring dirname in the
15692      `else' branch below isn't an issue.  */
15693
15694   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
15695     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
15696   else
15697     fullname = filename;
15698
15699   start_subfile (fullname, comp_dir);
15700
15701   if (fullname != filename)
15702     xfree (fullname);
15703 }
15704
15705 /* Start a symtab for DWARF.
15706    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */
15707
15708 static void
15709 dwarf2_start_symtab (struct dwarf2_cu *cu,
15710                      const char *name, const char *comp_dir, CORE_ADDR low_pc)
15711 {
15712   start_symtab (name, comp_dir, low_pc);
15713   record_debugformat ("DWARF 2");
15714   record_producer (cu->producer);
15715
15716   /* We assume that we're processing GCC output.  */
15717   processing_gcc_compilation = 2;
15718
15719   cu->processing_has_namespace_info = 0;
15720 }
15721
15722 static void
15723 var_decode_location (struct attribute *attr, struct symbol *sym,
15724                      struct dwarf2_cu *cu)
15725 {
15726   struct objfile *objfile = cu->objfile;
15727   struct comp_unit_head *cu_header = &cu->header;
15728
15729   /* NOTE drow/2003-01-30: There used to be a comment and some special
15730      code here to turn a symbol with DW_AT_external and a
15731      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
15732      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
15733      with some versions of binutils) where shared libraries could have
15734      relocations against symbols in their debug information - the
15735      minimal symbol would have the right address, but the debug info
15736      would not.  It's no longer necessary, because we will explicitly
15737      apply relocations when we read in the debug information now.  */
15738
15739   /* A DW_AT_location attribute with no contents indicates that a
15740      variable has been optimized away.  */
15741   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
15742     {
15743       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15744       return;
15745     }
15746
15747   /* Handle one degenerate form of location expression specially, to
15748      preserve GDB's previous behavior when section offsets are
15749      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
15750      then mark this symbol as LOC_STATIC.  */
15751
15752   if (attr_form_is_block (attr)
15753       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
15754            && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
15755           || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
15756               && (DW_BLOCK (attr)->size
15757                   == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
15758     {
15759       unsigned int dummy;
15760
15761       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
15762         SYMBOL_VALUE_ADDRESS (sym) =
15763           read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
15764       else
15765         SYMBOL_VALUE_ADDRESS (sym) =
15766           read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
15767       SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
15768       fixup_symbol_section (sym, objfile);
15769       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
15770                                               SYMBOL_SECTION (sym));
15771       return;
15772     }
15773
15774   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
15775      expression evaluator, and use LOC_COMPUTED only when necessary
15776      (i.e. when the value of a register or memory location is
15777      referenced, or a thread-local block, etc.).  Then again, it might
15778      not be worthwhile.  I'm assuming that it isn't unless performance
15779      or memory numbers show me otherwise.  */
15780
15781   dwarf2_symbol_mark_computed (attr, sym, cu, 0);
15782
15783   if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
15784     cu->has_loclist = 1;
15785 }
15786
15787 /* Given a pointer to a DWARF information entry, figure out if we need
15788    to make a symbol table entry for it, and if so, create a new entry
15789    and return a pointer to it.
15790    If TYPE is NULL, determine symbol type from the die, otherwise
15791    used the passed type.
15792    If SPACE is not NULL, use it to hold the new symbol.  If it is
15793    NULL, allocate a new symbol on the objfile's obstack.  */
15794
15795 static struct symbol *
15796 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
15797                  struct symbol *space)
15798 {
15799   struct objfile *objfile = cu->objfile;
15800   struct symbol *sym = NULL;
15801   const char *name;
15802   struct attribute *attr = NULL;
15803   struct attribute *attr2 = NULL;
15804   CORE_ADDR baseaddr;
15805   struct pending **list_to_add = NULL;
15806
15807   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
15808
15809   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15810
15811   name = dwarf2_name (die, cu);
15812   if (name)
15813     {
15814       const char *linkagename;
15815       int suppress_add = 0;
15816
15817       if (space)
15818         sym = space;
15819       else
15820         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
15821       OBJSTAT (objfile, n_syms++);
15822
15823       /* Cache this symbol's name and the name's demangled form (if any).  */
15824       SYMBOL_SET_LANGUAGE (sym, cu->language);
15825       linkagename = dwarf2_physname (name, die, cu);
15826       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
15827
15828       /* Fortran does not have mangling standard and the mangling does differ
15829          between gfortran, iFort etc.  */
15830       if (cu->language == language_fortran
15831           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
15832         symbol_set_demangled_name (&(sym->ginfo),
15833                                    dwarf2_full_name (name, die, cu),
15834                                    NULL);
15835
15836       /* Default assumptions.
15837          Use the passed type or decode it from the die.  */
15838       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15839       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
15840       if (type != NULL)
15841         SYMBOL_TYPE (sym) = type;
15842       else
15843         SYMBOL_TYPE (sym) = die_type (die, cu);
15844       attr = dwarf2_attr (die,
15845                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
15846                           cu);
15847       if (attr)
15848         {
15849           SYMBOL_LINE (sym) = DW_UNSND (attr);
15850         }
15851
15852       attr = dwarf2_attr (die,
15853                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
15854                           cu);
15855       if (attr)
15856         {
15857           int file_index = DW_UNSND (attr);
15858
15859           if (cu->line_header == NULL
15860               || file_index > cu->line_header->num_file_names)
15861             complaint (&symfile_complaints,
15862                        _("file index out of range"));
15863           else if (file_index > 0)
15864             {
15865               struct file_entry *fe;
15866
15867               fe = &cu->line_header->file_names[file_index - 1];
15868               SYMBOL_SYMTAB (sym) = fe->symtab;
15869             }
15870         }
15871
15872       switch (die->tag)
15873         {
15874         case DW_TAG_label:
15875           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
15876           if (attr)
15877             {
15878               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
15879             }
15880           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
15881           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
15882           SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
15883           add_symbol_to_list (sym, cu->list_in_scope);
15884           break;
15885         case DW_TAG_subprogram:
15886           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15887              finish_block.  */
15888           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15889           attr2 = dwarf2_attr (die, DW_AT_external, cu);
15890           if ((attr2 && (DW_UNSND (attr2) != 0))
15891               || cu->language == language_ada)
15892             {
15893               /* Subprograms marked external are stored as a global symbol.
15894                  Ada subprograms, whether marked external or not, are always
15895                  stored as a global symbol, because we want to be able to
15896                  access them globally.  For instance, we want to be able
15897                  to break on a nested subprogram without having to
15898                  specify the context.  */
15899               list_to_add = &global_symbols;
15900             }
15901           else
15902             {
15903               list_to_add = cu->list_in_scope;
15904             }
15905           break;
15906         case DW_TAG_inlined_subroutine:
15907           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
15908              finish_block.  */
15909           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
15910           SYMBOL_INLINED (sym) = 1;
15911           list_to_add = cu->list_in_scope;
15912           break;
15913         case DW_TAG_template_value_param:
15914           suppress_add = 1;
15915           /* Fall through.  */
15916         case DW_TAG_constant:
15917         case DW_TAG_variable:
15918         case DW_TAG_member:
15919           /* Compilation with minimal debug info may result in
15920              variables with missing type entries.  Change the
15921              misleading `void' type to something sensible.  */
15922           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
15923             SYMBOL_TYPE (sym)
15924               = objfile_type (objfile)->nodebug_data_symbol;
15925
15926           attr = dwarf2_attr (die, DW_AT_const_value, cu);
15927           /* In the case of DW_TAG_member, we should only be called for
15928              static const members.  */
15929           if (die->tag == DW_TAG_member)
15930             {
15931               /* dwarf2_add_field uses die_is_declaration,
15932                  so we do the same.  */
15933               gdb_assert (die_is_declaration (die, cu));
15934               gdb_assert (attr);
15935             }
15936           if (attr)
15937             {
15938               dwarf2_const_value (attr, sym, cu);
15939               attr2 = dwarf2_attr (die, DW_AT_external, cu);
15940               if (!suppress_add)
15941                 {
15942                   if (attr2 && (DW_UNSND (attr2) != 0))
15943                     list_to_add = &global_symbols;
15944                   else
15945                     list_to_add = cu->list_in_scope;
15946                 }
15947               break;
15948             }
15949           attr = dwarf2_attr (die, DW_AT_location, cu);
15950           if (attr)
15951             {
15952               var_decode_location (attr, sym, cu);
15953               attr2 = dwarf2_attr (die, DW_AT_external, cu);
15954
15955               /* Fortran explicitly imports any global symbols to the local
15956                  scope by DW_TAG_common_block.  */
15957               if (cu->language == language_fortran && die->parent
15958                   && die->parent->tag == DW_TAG_common_block)
15959                 attr2 = NULL;
15960
15961               if (SYMBOL_CLASS (sym) == LOC_STATIC
15962                   && SYMBOL_VALUE_ADDRESS (sym) == 0
15963                   && !dwarf2_per_objfile->has_section_at_zero)
15964                 {
15965                   /* When a static variable is eliminated by the linker,
15966                      the corresponding debug information is not stripped
15967                      out, but the variable address is set to null;
15968                      do not add such variables into symbol table.  */
15969                 }
15970               else if (attr2 && (DW_UNSND (attr2) != 0))
15971                 {
15972                   /* Workaround gfortran PR debug/40040 - it uses
15973                      DW_AT_location for variables in -fPIC libraries which may
15974                      get overriden by other libraries/executable and get
15975                      a different address.  Resolve it by the minimal symbol
15976                      which may come from inferior's executable using copy
15977                      relocation.  Make this workaround only for gfortran as for
15978                      other compilers GDB cannot guess the minimal symbol
15979                      Fortran mangling kind.  */
15980                   if (cu->language == language_fortran && die->parent
15981                       && die->parent->tag == DW_TAG_module
15982                       && cu->producer
15983                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
15984                     SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
15985
15986                   /* A variable with DW_AT_external is never static,
15987                      but it may be block-scoped.  */
15988                   list_to_add = (cu->list_in_scope == &file_symbols
15989                                  ? &global_symbols : cu->list_in_scope);
15990                 }
15991               else
15992                 list_to_add = cu->list_in_scope;
15993             }
15994           else
15995             {
15996               /* We do not know the address of this symbol.
15997                  If it is an external symbol and we have type information
15998                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
15999                  The address of the variable will then be determined from
16000                  the minimal symbol table whenever the variable is
16001                  referenced.  */
16002               attr2 = dwarf2_attr (die, DW_AT_external, cu);
16003
16004               /* Fortran explicitly imports any global symbols to the local
16005                  scope by DW_TAG_common_block.  */
16006               if (cu->language == language_fortran && die->parent
16007                   && die->parent->tag == DW_TAG_common_block)
16008                 {
16009                   /* SYMBOL_CLASS doesn't matter here because
16010                      read_common_block is going to reset it.  */
16011                   if (!suppress_add)
16012                     list_to_add = cu->list_in_scope;
16013                 }
16014               else if (attr2 && (DW_UNSND (attr2) != 0)
16015                        && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16016                 {
16017                   /* A variable with DW_AT_external is never static, but it
16018                      may be block-scoped.  */
16019                   list_to_add = (cu->list_in_scope == &file_symbols
16020                                  ? &global_symbols : cu->list_in_scope);
16021
16022                   SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16023                 }
16024               else if (!die_is_declaration (die, cu))
16025                 {
16026                   /* Use the default LOC_OPTIMIZED_OUT class.  */
16027                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16028                   if (!suppress_add)
16029                     list_to_add = cu->list_in_scope;
16030                 }
16031             }
16032           break;
16033         case DW_TAG_formal_parameter:
16034           /* If we are inside a function, mark this as an argument.  If
16035              not, we might be looking at an argument to an inlined function
16036              when we do not have enough information to show inlined frames;
16037              pretend it's a local variable in that case so that the user can
16038              still see it.  */
16039           if (context_stack_depth > 0
16040               && context_stack[context_stack_depth - 1].name != NULL)
16041             SYMBOL_IS_ARGUMENT (sym) = 1;
16042           attr = dwarf2_attr (die, DW_AT_location, cu);
16043           if (attr)
16044             {
16045               var_decode_location (attr, sym, cu);
16046             }
16047           attr = dwarf2_attr (die, DW_AT_const_value, cu);
16048           if (attr)
16049             {
16050               dwarf2_const_value (attr, sym, cu);
16051             }
16052
16053           list_to_add = cu->list_in_scope;
16054           break;
16055         case DW_TAG_unspecified_parameters:
16056           /* From varargs functions; gdb doesn't seem to have any
16057              interest in this information, so just ignore it for now.
16058              (FIXME?) */
16059           break;
16060         case DW_TAG_template_type_param:
16061           suppress_add = 1;
16062           /* Fall through.  */
16063         case DW_TAG_class_type:
16064         case DW_TAG_interface_type:
16065         case DW_TAG_structure_type:
16066         case DW_TAG_union_type:
16067         case DW_TAG_set_type:
16068         case DW_TAG_enumeration_type:
16069           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16070           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16071
16072           {
16073             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16074                really ever be static objects: otherwise, if you try
16075                to, say, break of a class's method and you're in a file
16076                which doesn't mention that class, it won't work unless
16077                the check for all static symbols in lookup_symbol_aux
16078                saves you.  See the OtherFileClass tests in
16079                gdb.c++/namespace.exp.  */
16080
16081             if (!suppress_add)
16082               {
16083                 list_to_add = (cu->list_in_scope == &file_symbols
16084                                && (cu->language == language_cplus
16085                                    || cu->language == language_java)
16086                                ? &global_symbols : cu->list_in_scope);
16087
16088                 /* The semantics of C++ state that "struct foo {
16089                    ... }" also defines a typedef for "foo".  A Java
16090                    class declaration also defines a typedef for the
16091                    class.  */
16092                 if (cu->language == language_cplus
16093                     || cu->language == language_java
16094                     || cu->language == language_ada)
16095                   {
16096                     /* The symbol's name is already allocated along
16097                        with this objfile, so we don't need to
16098                        duplicate it for the type.  */
16099                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16100                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16101                   }
16102               }
16103           }
16104           break;
16105         case DW_TAG_typedef:
16106           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16107           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16108           list_to_add = cu->list_in_scope;
16109           break;
16110         case DW_TAG_base_type:
16111         case DW_TAG_subrange_type:
16112           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16113           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16114           list_to_add = cu->list_in_scope;
16115           break;
16116         case DW_TAG_enumerator:
16117           attr = dwarf2_attr (die, DW_AT_const_value, cu);
16118           if (attr)
16119             {
16120               dwarf2_const_value (attr, sym, cu);
16121             }
16122           {
16123             /* NOTE: carlton/2003-11-10: See comment above in the
16124                DW_TAG_class_type, etc. block.  */
16125
16126             list_to_add = (cu->list_in_scope == &file_symbols
16127                            && (cu->language == language_cplus
16128                                || cu->language == language_java)
16129                            ? &global_symbols : cu->list_in_scope);
16130           }
16131           break;
16132         case DW_TAG_namespace:
16133           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16134           list_to_add = &global_symbols;
16135           break;
16136         case DW_TAG_common_block:
16137           SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16138           SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16139           add_symbol_to_list (sym, cu->list_in_scope);
16140           break;
16141         default:
16142           /* Not a tag we recognize.  Hopefully we aren't processing
16143              trash data, but since we must specifically ignore things
16144              we don't recognize, there is nothing else we should do at
16145              this point.  */
16146           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16147                      dwarf_tag_name (die->tag));
16148           break;
16149         }
16150
16151       if (suppress_add)
16152         {
16153           sym->hash_next = objfile->template_symbols;
16154           objfile->template_symbols = sym;
16155           list_to_add = NULL;
16156         }
16157
16158       if (list_to_add != NULL)
16159         add_symbol_to_list (sym, list_to_add);
16160
16161       /* For the benefit of old versions of GCC, check for anonymous
16162          namespaces based on the demangled name.  */
16163       if (!cu->processing_has_namespace_info
16164           && cu->language == language_cplus)
16165         cp_scan_for_anonymous_namespaces (sym, objfile);
16166     }
16167   return (sym);
16168 }
16169
16170 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
16171
16172 static struct symbol *
16173 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16174 {
16175   return new_symbol_full (die, type, cu, NULL);
16176 }
16177
16178 /* Given an attr with a DW_FORM_dataN value in host byte order,
16179    zero-extend it as appropriate for the symbol's type.  The DWARF
16180    standard (v4) is not entirely clear about the meaning of using
16181    DW_FORM_dataN for a constant with a signed type, where the type is
16182    wider than the data.  The conclusion of a discussion on the DWARF
16183    list was that this is unspecified.  We choose to always zero-extend
16184    because that is the interpretation long in use by GCC.  */
16185
16186 static gdb_byte *
16187 dwarf2_const_value_data (struct attribute *attr, struct type *type,
16188                          const char *name, struct obstack *obstack,
16189                          struct dwarf2_cu *cu, LONGEST *value, int bits)
16190 {
16191   struct objfile *objfile = cu->objfile;
16192   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16193                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16194   LONGEST l = DW_UNSND (attr);
16195
16196   if (bits < sizeof (*value) * 8)
16197     {
16198       l &= ((LONGEST) 1 << bits) - 1;
16199       *value = l;
16200     }
16201   else if (bits == sizeof (*value) * 8)
16202     *value = l;
16203   else
16204     {
16205       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16206       store_unsigned_integer (bytes, bits / 8, byte_order, l);
16207       return bytes;
16208     }
16209
16210   return NULL;
16211 }
16212
16213 /* Read a constant value from an attribute.  Either set *VALUE, or if
16214    the value does not fit in *VALUE, set *BYTES - either already
16215    allocated on the objfile obstack, or newly allocated on OBSTACK,
16216    or, set *BATON, if we translated the constant to a location
16217    expression.  */
16218
16219 static void
16220 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16221                          const char *name, struct obstack *obstack,
16222                          struct dwarf2_cu *cu,
16223                          LONGEST *value, gdb_byte **bytes,
16224                          struct dwarf2_locexpr_baton **baton)
16225 {
16226   struct objfile *objfile = cu->objfile;
16227   struct comp_unit_head *cu_header = &cu->header;
16228   struct dwarf_block *blk;
16229   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16230                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16231
16232   *value = 0;
16233   *bytes = NULL;
16234   *baton = NULL;
16235
16236   switch (attr->form)
16237     {
16238     case DW_FORM_addr:
16239     case DW_FORM_GNU_addr_index:
16240       {
16241         gdb_byte *data;
16242
16243         if (TYPE_LENGTH (type) != cu_header->addr_size)
16244           dwarf2_const_value_length_mismatch_complaint (name,
16245                                                         cu_header->addr_size,
16246                                                         TYPE_LENGTH (type));
16247         /* Symbols of this form are reasonably rare, so we just
16248            piggyback on the existing location code rather than writing
16249            a new implementation of symbol_computed_ops.  */
16250         *baton = obstack_alloc (&objfile->objfile_obstack,
16251                                 sizeof (struct dwarf2_locexpr_baton));
16252         (*baton)->per_cu = cu->per_cu;
16253         gdb_assert ((*baton)->per_cu);
16254
16255         (*baton)->size = 2 + cu_header->addr_size;
16256         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
16257         (*baton)->data = data;
16258
16259         data[0] = DW_OP_addr;
16260         store_unsigned_integer (&data[1], cu_header->addr_size,
16261                                 byte_order, DW_ADDR (attr));
16262         data[cu_header->addr_size + 1] = DW_OP_stack_value;
16263       }
16264       break;
16265     case DW_FORM_string:
16266     case DW_FORM_strp:
16267     case DW_FORM_GNU_str_index:
16268     case DW_FORM_GNU_strp_alt:
16269       /* DW_STRING is already allocated on the objfile obstack, point
16270          directly to it.  */
16271       *bytes = (gdb_byte *) DW_STRING (attr);
16272       break;
16273     case DW_FORM_block1:
16274     case DW_FORM_block2:
16275     case DW_FORM_block4:
16276     case DW_FORM_block:
16277     case DW_FORM_exprloc:
16278       blk = DW_BLOCK (attr);
16279       if (TYPE_LENGTH (type) != blk->size)
16280         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16281                                                       TYPE_LENGTH (type));
16282       *bytes = blk->data;
16283       break;
16284
16285       /* The DW_AT_const_value attributes are supposed to carry the
16286          symbol's value "represented as it would be on the target
16287          architecture."  By the time we get here, it's already been
16288          converted to host endianness, so we just need to sign- or
16289          zero-extend it as appropriate.  */
16290     case DW_FORM_data1:
16291       *bytes = dwarf2_const_value_data (attr, type, name,
16292                                         obstack, cu, value, 8);
16293       break;
16294     case DW_FORM_data2:
16295       *bytes = dwarf2_const_value_data (attr, type, name,
16296                                         obstack, cu, value, 16);
16297       break;
16298     case DW_FORM_data4:
16299       *bytes = dwarf2_const_value_data (attr, type, name,
16300                                         obstack, cu, value, 32);
16301       break;
16302     case DW_FORM_data8:
16303       *bytes = dwarf2_const_value_data (attr, type, name,
16304                                         obstack, cu, value, 64);
16305       break;
16306
16307     case DW_FORM_sdata:
16308       *value = DW_SND (attr);
16309       break;
16310
16311     case DW_FORM_udata:
16312       *value = DW_UNSND (attr);
16313       break;
16314
16315     default:
16316       complaint (&symfile_complaints,
16317                  _("unsupported const value attribute form: '%s'"),
16318                  dwarf_form_name (attr->form));
16319       *value = 0;
16320       break;
16321     }
16322 }
16323
16324
16325 /* Copy constant value from an attribute to a symbol.  */
16326
16327 static void
16328 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16329                     struct dwarf2_cu *cu)
16330 {
16331   struct objfile *objfile = cu->objfile;
16332   struct comp_unit_head *cu_header = &cu->header;
16333   LONGEST value;
16334   gdb_byte *bytes;
16335   struct dwarf2_locexpr_baton *baton;
16336
16337   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16338                            SYMBOL_PRINT_NAME (sym),
16339                            &objfile->objfile_obstack, cu,
16340                            &value, &bytes, &baton);
16341
16342   if (baton != NULL)
16343     {
16344       SYMBOL_LOCATION_BATON (sym) = baton;
16345       SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16346     }
16347   else if (bytes != NULL)
16348      {
16349       SYMBOL_VALUE_BYTES (sym) = bytes;
16350       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16351     }
16352   else
16353     {
16354       SYMBOL_VALUE (sym) = value;
16355       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16356     }
16357 }
16358
16359 /* Return the type of the die in question using its DW_AT_type attribute.  */
16360
16361 static struct type *
16362 die_type (struct die_info *die, struct dwarf2_cu *cu)
16363 {
16364   struct attribute *type_attr;
16365
16366   type_attr = dwarf2_attr (die, DW_AT_type, cu);
16367   if (!type_attr)
16368     {
16369       /* A missing DW_AT_type represents a void type.  */
16370       return objfile_type (cu->objfile)->builtin_void;
16371     }
16372
16373   return lookup_die_type (die, type_attr, cu);
16374 }
16375
16376 /* True iff CU's producer generates GNAT Ada auxiliary information
16377    that allows to find parallel types through that information instead
16378    of having to do expensive parallel lookups by type name.  */
16379
16380 static int
16381 need_gnat_info (struct dwarf2_cu *cu)
16382 {
16383   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16384      of GNAT produces this auxiliary information, without any indication
16385      that it is produced.  Part of enhancing the FSF version of GNAT
16386      to produce that information will be to put in place an indicator
16387      that we can use in order to determine whether the descriptive type
16388      info is available or not.  One suggestion that has been made is
16389      to use a new attribute, attached to the CU die.  For now, assume
16390      that the descriptive type info is not available.  */
16391   return 0;
16392 }
16393
16394 /* Return the auxiliary type of the die in question using its
16395    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
16396    attribute is not present.  */
16397
16398 static struct type *
16399 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16400 {
16401   struct attribute *type_attr;
16402
16403   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16404   if (!type_attr)
16405     return NULL;
16406
16407   return lookup_die_type (die, type_attr, cu);
16408 }
16409
16410 /* If DIE has a descriptive_type attribute, then set the TYPE's
16411    descriptive type accordingly.  */
16412
16413 static void
16414 set_descriptive_type (struct type *type, struct die_info *die,
16415                       struct dwarf2_cu *cu)
16416 {
16417   struct type *descriptive_type = die_descriptive_type (die, cu);
16418
16419   if (descriptive_type)
16420     {
16421       ALLOCATE_GNAT_AUX_TYPE (type);
16422       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16423     }
16424 }
16425
16426 /* Return the containing type of the die in question using its
16427    DW_AT_containing_type attribute.  */
16428
16429 static struct type *
16430 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16431 {
16432   struct attribute *type_attr;
16433
16434   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16435   if (!type_attr)
16436     error (_("Dwarf Error: Problem turning containing type into gdb type "
16437              "[in module %s]"), cu->objfile->name);
16438
16439   return lookup_die_type (die, type_attr, cu);
16440 }
16441
16442 /* Look up the type of DIE in CU using its type attribute ATTR.
16443    If there is no type substitute an error marker.  */
16444
16445 static struct type *
16446 lookup_die_type (struct die_info *die, struct attribute *attr,
16447                  struct dwarf2_cu *cu)
16448 {
16449   struct objfile *objfile = cu->objfile;
16450   struct type *this_type;
16451
16452   /* First see if we have it cached.  */
16453
16454   if (attr->form == DW_FORM_GNU_ref_alt)
16455     {
16456       struct dwarf2_per_cu_data *per_cu;
16457       sect_offset offset = dwarf2_get_ref_die_offset (attr);
16458
16459       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
16460       this_type = get_die_type_at_offset (offset, per_cu);
16461     }
16462   else if (is_ref_attr (attr))
16463     {
16464       sect_offset offset = dwarf2_get_ref_die_offset (attr);
16465
16466       this_type = get_die_type_at_offset (offset, cu->per_cu);
16467     }
16468   else if (attr->form == DW_FORM_ref_sig8)
16469     {
16470       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16471
16472       /* sig_type will be NULL if the signatured type is missing from
16473          the debug info.  */
16474       if (sig_type == NULL)
16475         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16476                  "at 0x%x [in module %s]"),
16477                die->offset.sect_off, objfile->name);
16478
16479       gdb_assert (sig_type->per_cu.is_debug_types);
16480       /* If we haven't filled in type_offset_in_section yet, then we
16481          haven't read the type in yet.  */
16482       this_type = NULL;
16483       if (sig_type->type_offset_in_section.sect_off != 0)
16484         {
16485           this_type =
16486             get_die_type_at_offset (sig_type->type_offset_in_section,
16487                                     &sig_type->per_cu);
16488         }
16489     }
16490   else
16491     {
16492       dump_die_for_error (die);
16493       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
16494              dwarf_attr_name (attr->name), objfile->name);
16495     }
16496
16497   /* If not cached we need to read it in.  */
16498
16499   if (this_type == NULL)
16500     {
16501       struct die_info *type_die;
16502       struct dwarf2_cu *type_cu = cu;
16503
16504       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
16505       /* If we found the type now, it's probably because the type came
16506          from an inter-CU reference and the type's CU got expanded before
16507          ours.  */
16508       this_type = get_die_type (type_die, type_cu);
16509       if (this_type == NULL)
16510         this_type = read_type_die_1 (type_die, type_cu);
16511     }
16512
16513   /* If we still don't have a type use an error marker.  */
16514
16515   if (this_type == NULL)
16516     {
16517       char *message, *saved;
16518
16519       /* read_type_die already issued a complaint.  */
16520       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16521                             objfile->name,
16522                             cu->header.offset.sect_off,
16523                             die->offset.sect_off);
16524       saved = obstack_copy0 (&objfile->objfile_obstack,
16525                              message, strlen (message));
16526       xfree (message);
16527
16528       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16529     }
16530
16531   return this_type;
16532 }
16533
16534 /* Return the type in DIE, CU.
16535    Returns NULL for invalid types.
16536
16537    This first does a lookup in the appropriate type_hash table,
16538    and only reads the die in if necessary.
16539
16540    NOTE: This can be called when reading in partial or full symbols.  */
16541
16542 static struct type *
16543 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
16544 {
16545   struct type *this_type;
16546
16547   this_type = get_die_type (die, cu);
16548   if (this_type)
16549     return this_type;
16550
16551   return read_type_die_1 (die, cu);
16552 }
16553
16554 /* Read the type in DIE, CU.
16555    Returns NULL for invalid types.  */
16556
16557 static struct type *
16558 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
16559 {
16560   struct type *this_type = NULL;
16561
16562   switch (die->tag)
16563     {
16564     case DW_TAG_class_type:
16565     case DW_TAG_interface_type:
16566     case DW_TAG_structure_type:
16567     case DW_TAG_union_type:
16568       this_type = read_structure_type (die, cu);
16569       break;
16570     case DW_TAG_enumeration_type:
16571       this_type = read_enumeration_type (die, cu);
16572       break;
16573     case DW_TAG_subprogram:
16574     case DW_TAG_subroutine_type:
16575     case DW_TAG_inlined_subroutine:
16576       this_type = read_subroutine_type (die, cu);
16577       break;
16578     case DW_TAG_array_type:
16579       this_type = read_array_type (die, cu);
16580       break;
16581     case DW_TAG_set_type:
16582       this_type = read_set_type (die, cu);
16583       break;
16584     case DW_TAG_pointer_type:
16585       this_type = read_tag_pointer_type (die, cu);
16586       break;
16587     case DW_TAG_ptr_to_member_type:
16588       this_type = read_tag_ptr_to_member_type (die, cu);
16589       break;
16590     case DW_TAG_reference_type:
16591       this_type = read_tag_reference_type (die, cu);
16592       break;
16593     case DW_TAG_const_type:
16594       this_type = read_tag_const_type (die, cu);
16595       break;
16596     case DW_TAG_volatile_type:
16597       this_type = read_tag_volatile_type (die, cu);
16598       break;
16599     case DW_TAG_restrict_type:
16600       this_type = read_tag_restrict_type (die, cu);
16601       break;
16602     case DW_TAG_string_type:
16603       this_type = read_tag_string_type (die, cu);
16604       break;
16605     case DW_TAG_typedef:
16606       this_type = read_typedef (die, cu);
16607       break;
16608     case DW_TAG_subrange_type:
16609       this_type = read_subrange_type (die, cu);
16610       break;
16611     case DW_TAG_base_type:
16612       this_type = read_base_type (die, cu);
16613       break;
16614     case DW_TAG_unspecified_type:
16615       this_type = read_unspecified_type (die, cu);
16616       break;
16617     case DW_TAG_namespace:
16618       this_type = read_namespace_type (die, cu);
16619       break;
16620     case DW_TAG_module:
16621       this_type = read_module_type (die, cu);
16622       break;
16623     default:
16624       complaint (&symfile_complaints,
16625                  _("unexpected tag in read_type_die: '%s'"),
16626                  dwarf_tag_name (die->tag));
16627       break;
16628     }
16629
16630   return this_type;
16631 }
16632
16633 /* See if we can figure out if the class lives in a namespace.  We do
16634    this by looking for a member function; its demangled name will
16635    contain namespace info, if there is any.
16636    Return the computed name or NULL.
16637    Space for the result is allocated on the objfile's obstack.
16638    This is the full-die version of guess_partial_die_structure_name.
16639    In this case we know DIE has no useful parent.  */
16640
16641 static char *
16642 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
16643 {
16644   struct die_info *spec_die;
16645   struct dwarf2_cu *spec_cu;
16646   struct die_info *child;
16647
16648   spec_cu = cu;
16649   spec_die = die_specification (die, &spec_cu);
16650   if (spec_die != NULL)
16651     {
16652       die = spec_die;
16653       cu = spec_cu;
16654     }
16655
16656   for (child = die->child;
16657        child != NULL;
16658        child = child->sibling)
16659     {
16660       if (child->tag == DW_TAG_subprogram)
16661         {
16662           struct attribute *attr;
16663
16664           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
16665           if (attr == NULL)
16666             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
16667           if (attr != NULL)
16668             {
16669               char *actual_name
16670                 = language_class_name_from_physname (cu->language_defn,
16671                                                      DW_STRING (attr));
16672               char *name = NULL;
16673
16674               if (actual_name != NULL)
16675                 {
16676                   const char *die_name = dwarf2_name (die, cu);
16677
16678                   if (die_name != NULL
16679                       && strcmp (die_name, actual_name) != 0)
16680                     {
16681                       /* Strip off the class name from the full name.
16682                          We want the prefix.  */
16683                       int die_name_len = strlen (die_name);
16684                       int actual_name_len = strlen (actual_name);
16685
16686                       /* Test for '::' as a sanity check.  */
16687                       if (actual_name_len > die_name_len + 2
16688                           && actual_name[actual_name_len
16689                                          - die_name_len - 1] == ':')
16690                         name =
16691                           obstack_copy0 (&cu->objfile->objfile_obstack,
16692                                          actual_name,
16693                                          actual_name_len - die_name_len - 2);
16694                     }
16695                 }
16696               xfree (actual_name);
16697               return name;
16698             }
16699         }
16700     }
16701
16702   return NULL;
16703 }
16704
16705 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
16706    prefix part in such case.  See
16707    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
16708
16709 static char *
16710 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
16711 {
16712   struct attribute *attr;
16713   char *base;
16714
16715   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
16716       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
16717     return NULL;
16718
16719   attr = dwarf2_attr (die, DW_AT_name, cu);
16720   if (attr != NULL && DW_STRING (attr) != NULL)
16721     return NULL;
16722
16723   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16724   if (attr == NULL)
16725     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16726   if (attr == NULL || DW_STRING (attr) == NULL)
16727     return NULL;
16728
16729   /* dwarf2_name had to be already called.  */
16730   gdb_assert (DW_STRING_IS_CANONICAL (attr));
16731
16732   /* Strip the base name, keep any leading namespaces/classes.  */
16733   base = strrchr (DW_STRING (attr), ':');
16734   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
16735     return "";
16736
16737   return obstack_copy0 (&cu->objfile->objfile_obstack,
16738                         DW_STRING (attr), &base[-1] - DW_STRING (attr));
16739 }
16740
16741 /* Return the name of the namespace/class that DIE is defined within,
16742    or "" if we can't tell.  The caller should not xfree the result.
16743
16744    For example, if we're within the method foo() in the following
16745    code:
16746
16747    namespace N {
16748      class C {
16749        void foo () {
16750        }
16751      };
16752    }
16753
16754    then determine_prefix on foo's die will return "N::C".  */
16755
16756 static const char *
16757 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
16758 {
16759   struct die_info *parent, *spec_die;
16760   struct dwarf2_cu *spec_cu;
16761   struct type *parent_type;
16762   char *retval;
16763
16764   if (cu->language != language_cplus && cu->language != language_java
16765       && cu->language != language_fortran)
16766     return "";
16767
16768   retval = anonymous_struct_prefix (die, cu);
16769   if (retval)
16770     return retval;
16771
16772   /* We have to be careful in the presence of DW_AT_specification.
16773      For example, with GCC 3.4, given the code
16774
16775      namespace N {
16776        void foo() {
16777          // Definition of N::foo.
16778        }
16779      }
16780
16781      then we'll have a tree of DIEs like this:
16782
16783      1: DW_TAG_compile_unit
16784        2: DW_TAG_namespace        // N
16785          3: DW_TAG_subprogram     // declaration of N::foo
16786        4: DW_TAG_subprogram       // definition of N::foo
16787             DW_AT_specification   // refers to die #3
16788
16789      Thus, when processing die #4, we have to pretend that we're in
16790      the context of its DW_AT_specification, namely the contex of die
16791      #3.  */
16792   spec_cu = cu;
16793   spec_die = die_specification (die, &spec_cu);
16794   if (spec_die == NULL)
16795     parent = die->parent;
16796   else
16797     {
16798       parent = spec_die->parent;
16799       cu = spec_cu;
16800     }
16801
16802   if (parent == NULL)
16803     return "";
16804   else if (parent->building_fullname)
16805     {
16806       const char *name;
16807       const char *parent_name;
16808
16809       /* It has been seen on RealView 2.2 built binaries,
16810          DW_TAG_template_type_param types actually _defined_ as
16811          children of the parent class:
16812
16813          enum E {};
16814          template class <class Enum> Class{};
16815          Class<enum E> class_e;
16816
16817          1: DW_TAG_class_type (Class)
16818            2: DW_TAG_enumeration_type (E)
16819              3: DW_TAG_enumerator (enum1:0)
16820              3: DW_TAG_enumerator (enum2:1)
16821              ...
16822            2: DW_TAG_template_type_param
16823               DW_AT_type  DW_FORM_ref_udata (E)
16824
16825          Besides being broken debug info, it can put GDB into an
16826          infinite loop.  Consider:
16827
16828          When we're building the full name for Class<E>, we'll start
16829          at Class, and go look over its template type parameters,
16830          finding E.  We'll then try to build the full name of E, and
16831          reach here.  We're now trying to build the full name of E,
16832          and look over the parent DIE for containing scope.  In the
16833          broken case, if we followed the parent DIE of E, we'd again
16834          find Class, and once again go look at its template type
16835          arguments, etc., etc.  Simply don't consider such parent die
16836          as source-level parent of this die (it can't be, the language
16837          doesn't allow it), and break the loop here.  */
16838       name = dwarf2_name (die, cu);
16839       parent_name = dwarf2_name (parent, cu);
16840       complaint (&symfile_complaints,
16841                  _("template param type '%s' defined within parent '%s'"),
16842                  name ? name : "<unknown>",
16843                  parent_name ? parent_name : "<unknown>");
16844       return "";
16845     }
16846   else
16847     switch (parent->tag)
16848       {
16849       case DW_TAG_namespace:
16850         parent_type = read_type_die (parent, cu);
16851         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
16852            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
16853            Work around this problem here.  */
16854         if (cu->language == language_cplus
16855             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
16856           return "";
16857         /* We give a name to even anonymous namespaces.  */
16858         return TYPE_TAG_NAME (parent_type);
16859       case DW_TAG_class_type:
16860       case DW_TAG_interface_type:
16861       case DW_TAG_structure_type:
16862       case DW_TAG_union_type:
16863       case DW_TAG_module:
16864         parent_type = read_type_die (parent, cu);
16865         if (TYPE_TAG_NAME (parent_type) != NULL)
16866           return TYPE_TAG_NAME (parent_type);
16867         else
16868           /* An anonymous structure is only allowed non-static data
16869              members; no typedefs, no member functions, et cetera.
16870              So it does not need a prefix.  */
16871           return "";
16872       case DW_TAG_compile_unit:
16873       case DW_TAG_partial_unit:
16874         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
16875         if (cu->language == language_cplus
16876             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
16877             && die->child != NULL
16878             && (die->tag == DW_TAG_class_type
16879                 || die->tag == DW_TAG_structure_type
16880                 || die->tag == DW_TAG_union_type))
16881           {
16882             char *name = guess_full_die_structure_name (die, cu);
16883             if (name != NULL)
16884               return name;
16885           }
16886         return "";
16887       default:
16888         return determine_prefix (parent, cu);
16889       }
16890 }
16891
16892 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
16893    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
16894    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
16895    an obconcat, otherwise allocate storage for the result.  The CU argument is
16896    used to determine the language and hence, the appropriate separator.  */
16897
16898 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
16899
16900 static char *
16901 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
16902                  int physname, struct dwarf2_cu *cu)
16903 {
16904   const char *lead = "";
16905   const char *sep;
16906
16907   if (suffix == NULL || suffix[0] == '\0'
16908       || prefix == NULL || prefix[0] == '\0')
16909     sep = "";
16910   else if (cu->language == language_java)
16911     sep = ".";
16912   else if (cu->language == language_fortran && physname)
16913     {
16914       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
16915          DW_AT_MIPS_linkage_name is preferred and used instead.  */
16916
16917       lead = "__";
16918       sep = "_MOD_";
16919     }
16920   else
16921     sep = "::";
16922
16923   if (prefix == NULL)
16924     prefix = "";
16925   if (suffix == NULL)
16926     suffix = "";
16927
16928   if (obs == NULL)
16929     {
16930       char *retval
16931         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
16932
16933       strcpy (retval, lead);
16934       strcat (retval, prefix);
16935       strcat (retval, sep);
16936       strcat (retval, suffix);
16937       return retval;
16938     }
16939   else
16940     {
16941       /* We have an obstack.  */
16942       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
16943     }
16944 }
16945
16946 /* Return sibling of die, NULL if no sibling.  */
16947
16948 static struct die_info *
16949 sibling_die (struct die_info *die)
16950 {
16951   return die->sibling;
16952 }
16953
16954 /* Get name of a die, return NULL if not found.  */
16955
16956 static const char *
16957 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
16958                           struct obstack *obstack)
16959 {
16960   if (name && cu->language == language_cplus)
16961     {
16962       char *canon_name = cp_canonicalize_string (name);
16963
16964       if (canon_name != NULL)
16965         {
16966           if (strcmp (canon_name, name) != 0)
16967             name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
16968           xfree (canon_name);
16969         }
16970     }
16971
16972   return name;
16973 }
16974
16975 /* Get name of a die, return NULL if not found.  */
16976
16977 static const char *
16978 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
16979 {
16980   struct attribute *attr;
16981
16982   attr = dwarf2_attr (die, DW_AT_name, cu);
16983   if ((!attr || !DW_STRING (attr))
16984       && die->tag != DW_TAG_class_type
16985       && die->tag != DW_TAG_interface_type
16986       && die->tag != DW_TAG_structure_type
16987       && die->tag != DW_TAG_union_type)
16988     return NULL;
16989
16990   switch (die->tag)
16991     {
16992     case DW_TAG_compile_unit:
16993     case DW_TAG_partial_unit:
16994       /* Compilation units have a DW_AT_name that is a filename, not
16995          a source language identifier.  */
16996     case DW_TAG_enumeration_type:
16997     case DW_TAG_enumerator:
16998       /* These tags always have simple identifiers already; no need
16999          to canonicalize them.  */
17000       return DW_STRING (attr);
17001
17002     case DW_TAG_subprogram:
17003       /* Java constructors will all be named "<init>", so return
17004          the class name when we see this special case.  */
17005       if (cu->language == language_java
17006           && DW_STRING (attr) != NULL
17007           && strcmp (DW_STRING (attr), "<init>") == 0)
17008         {
17009           struct dwarf2_cu *spec_cu = cu;
17010           struct die_info *spec_die;
17011
17012           /* GCJ will output '<init>' for Java constructor names.
17013              For this special case, return the name of the parent class.  */
17014
17015           /* GCJ may output suprogram DIEs with AT_specification set.
17016              If so, use the name of the specified DIE.  */
17017           spec_die = die_specification (die, &spec_cu);
17018           if (spec_die != NULL)
17019             return dwarf2_name (spec_die, spec_cu);
17020
17021           do
17022             {
17023               die = die->parent;
17024               if (die->tag == DW_TAG_class_type)
17025                 return dwarf2_name (die, cu);
17026             }
17027           while (die->tag != DW_TAG_compile_unit
17028                  && die->tag != DW_TAG_partial_unit);
17029         }
17030       break;
17031
17032     case DW_TAG_class_type:
17033     case DW_TAG_interface_type:
17034     case DW_TAG_structure_type:
17035     case DW_TAG_union_type:
17036       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17037          structures or unions.  These were of the form "._%d" in GCC 4.1,
17038          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17039          and GCC 4.4.  We work around this problem by ignoring these.  */
17040       if (attr && DW_STRING (attr)
17041           && (strncmp (DW_STRING (attr), "._", 2) == 0
17042               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17043         return NULL;
17044
17045       /* GCC might emit a nameless typedef that has a linkage name.  See
17046          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
17047       if (!attr || DW_STRING (attr) == NULL)
17048         {
17049           char *demangled = NULL;
17050
17051           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17052           if (attr == NULL)
17053             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17054
17055           if (attr == NULL || DW_STRING (attr) == NULL)
17056             return NULL;
17057
17058           /* Avoid demangling DW_STRING (attr) the second time on a second
17059              call for the same DIE.  */
17060           if (!DW_STRING_IS_CANONICAL (attr))
17061             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
17062
17063           if (demangled)
17064             {
17065               char *base;
17066
17067               /* FIXME: we already did this for the partial symbol... */
17068               DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17069                                                 demangled, strlen (demangled));
17070               DW_STRING_IS_CANONICAL (attr) = 1;
17071               xfree (demangled);
17072
17073               /* Strip any leading namespaces/classes, keep only the base name.
17074                  DW_AT_name for named DIEs does not contain the prefixes.  */
17075               base = strrchr (DW_STRING (attr), ':');
17076               if (base && base > DW_STRING (attr) && base[-1] == ':')
17077                 return &base[1];
17078               else
17079                 return DW_STRING (attr);
17080             }
17081         }
17082       break;
17083
17084     default:
17085       break;
17086     }
17087
17088   if (!DW_STRING_IS_CANONICAL (attr))
17089     {
17090       DW_STRING (attr)
17091         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17092                                     &cu->objfile->objfile_obstack);
17093       DW_STRING_IS_CANONICAL (attr) = 1;
17094     }
17095   return DW_STRING (attr);
17096 }
17097
17098 /* Return the die that this die in an extension of, or NULL if there
17099    is none.  *EXT_CU is the CU containing DIE on input, and the CU
17100    containing the return value on output.  */
17101
17102 static struct die_info *
17103 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17104 {
17105   struct attribute *attr;
17106
17107   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17108   if (attr == NULL)
17109     return NULL;
17110
17111   return follow_die_ref (die, attr, ext_cu);
17112 }
17113
17114 /* Convert a DIE tag into its string name.  */
17115
17116 static const char *
17117 dwarf_tag_name (unsigned tag)
17118 {
17119   const char *name = get_DW_TAG_name (tag);
17120
17121   if (name == NULL)
17122     return "DW_TAG_<unknown>";
17123
17124   return name;
17125 }
17126
17127 /* Convert a DWARF attribute code into its string name.  */
17128
17129 static const char *
17130 dwarf_attr_name (unsigned attr)
17131 {
17132   const char *name;
17133
17134 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17135   if (attr == DW_AT_MIPS_fde)
17136     return "DW_AT_MIPS_fde";
17137 #else
17138   if (attr == DW_AT_HP_block_index)
17139     return "DW_AT_HP_block_index";
17140 #endif
17141
17142   name = get_DW_AT_name (attr);
17143
17144   if (name == NULL)
17145     return "DW_AT_<unknown>";
17146
17147   return name;
17148 }
17149
17150 /* Convert a DWARF value form code into its string name.  */
17151
17152 static const char *
17153 dwarf_form_name (unsigned form)
17154 {
17155   const char *name = get_DW_FORM_name (form);
17156
17157   if (name == NULL)
17158     return "DW_FORM_<unknown>";
17159
17160   return name;
17161 }
17162
17163 static char *
17164 dwarf_bool_name (unsigned mybool)
17165 {
17166   if (mybool)
17167     return "TRUE";
17168   else
17169     return "FALSE";
17170 }
17171
17172 /* Convert a DWARF type code into its string name.  */
17173
17174 static const char *
17175 dwarf_type_encoding_name (unsigned enc)
17176 {
17177   const char *name = get_DW_ATE_name (enc);
17178
17179   if (name == NULL)
17180     return "DW_ATE_<unknown>";
17181
17182   return name;
17183 }
17184
17185 static void
17186 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17187 {
17188   unsigned int i;
17189
17190   print_spaces (indent, f);
17191   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17192            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17193
17194   if (die->parent != NULL)
17195     {
17196       print_spaces (indent, f);
17197       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
17198                           die->parent->offset.sect_off);
17199     }
17200
17201   print_spaces (indent, f);
17202   fprintf_unfiltered (f, "  has children: %s\n",
17203            dwarf_bool_name (die->child != NULL));
17204
17205   print_spaces (indent, f);
17206   fprintf_unfiltered (f, "  attributes:\n");
17207
17208   for (i = 0; i < die->num_attrs; ++i)
17209     {
17210       print_spaces (indent, f);
17211       fprintf_unfiltered (f, "    %s (%s) ",
17212                dwarf_attr_name (die->attrs[i].name),
17213                dwarf_form_name (die->attrs[i].form));
17214
17215       switch (die->attrs[i].form)
17216         {
17217         case DW_FORM_addr:
17218         case DW_FORM_GNU_addr_index:
17219           fprintf_unfiltered (f, "address: ");
17220           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17221           break;
17222         case DW_FORM_block2:
17223         case DW_FORM_block4:
17224         case DW_FORM_block:
17225         case DW_FORM_block1:
17226           fprintf_unfiltered (f, "block: size %s",
17227                               pulongest (DW_BLOCK (&die->attrs[i])->size));
17228           break;
17229         case DW_FORM_exprloc:
17230           fprintf_unfiltered (f, "expression: size %s",
17231                               pulongest (DW_BLOCK (&die->attrs[i])->size));
17232           break;
17233         case DW_FORM_ref_addr:
17234           fprintf_unfiltered (f, "ref address: ");
17235           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17236           break;
17237         case DW_FORM_GNU_ref_alt:
17238           fprintf_unfiltered (f, "alt ref address: ");
17239           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17240           break;
17241         case DW_FORM_ref1:
17242         case DW_FORM_ref2:
17243         case DW_FORM_ref4:
17244         case DW_FORM_ref8:
17245         case DW_FORM_ref_udata:
17246           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17247                               (long) (DW_UNSND (&die->attrs[i])));
17248           break;
17249         case DW_FORM_data1:
17250         case DW_FORM_data2:
17251         case DW_FORM_data4:
17252         case DW_FORM_data8:
17253         case DW_FORM_udata:
17254         case DW_FORM_sdata:
17255           fprintf_unfiltered (f, "constant: %s",
17256                               pulongest (DW_UNSND (&die->attrs[i])));
17257           break;
17258         case DW_FORM_sec_offset:
17259           fprintf_unfiltered (f, "section offset: %s",
17260                               pulongest (DW_UNSND (&die->attrs[i])));
17261           break;
17262         case DW_FORM_ref_sig8:
17263           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
17264             {
17265               struct signatured_type *sig_type =
17266                 DW_SIGNATURED_TYPE (&die->attrs[i]);
17267
17268               fprintf_unfiltered (f, "signatured type: 0x%s, offset 0x%x",
17269                                   hex_string (sig_type->signature),
17270                                   sig_type->per_cu.offset.sect_off);
17271             }
17272           else
17273             fprintf_unfiltered (f, "signatured type, unknown");
17274           break;
17275         case DW_FORM_string:
17276         case DW_FORM_strp:
17277         case DW_FORM_GNU_str_index:
17278         case DW_FORM_GNU_strp_alt:
17279           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17280                    DW_STRING (&die->attrs[i])
17281                    ? DW_STRING (&die->attrs[i]) : "",
17282                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17283           break;
17284         case DW_FORM_flag:
17285           if (DW_UNSND (&die->attrs[i]))
17286             fprintf_unfiltered (f, "flag: TRUE");
17287           else
17288             fprintf_unfiltered (f, "flag: FALSE");
17289           break;
17290         case DW_FORM_flag_present:
17291           fprintf_unfiltered (f, "flag: TRUE");
17292           break;
17293         case DW_FORM_indirect:
17294           /* The reader will have reduced the indirect form to
17295              the "base form" so this form should not occur.  */
17296           fprintf_unfiltered (f, 
17297                               "unexpected attribute form: DW_FORM_indirect");
17298           break;
17299         default:
17300           fprintf_unfiltered (f, "unsupported attribute form: %d.",
17301                    die->attrs[i].form);
17302           break;
17303         }
17304       fprintf_unfiltered (f, "\n");
17305     }
17306 }
17307
17308 static void
17309 dump_die_for_error (struct die_info *die)
17310 {
17311   dump_die_shallow (gdb_stderr, 0, die);
17312 }
17313
17314 static void
17315 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17316 {
17317   int indent = level * 4;
17318
17319   gdb_assert (die != NULL);
17320
17321   if (level >= max_level)
17322     return;
17323
17324   dump_die_shallow (f, indent, die);
17325
17326   if (die->child != NULL)
17327     {
17328       print_spaces (indent, f);
17329       fprintf_unfiltered (f, "  Children:");
17330       if (level + 1 < max_level)
17331         {
17332           fprintf_unfiltered (f, "\n");
17333           dump_die_1 (f, level + 1, max_level, die->child);
17334         }
17335       else
17336         {
17337           fprintf_unfiltered (f,
17338                               " [not printed, max nesting level reached]\n");
17339         }
17340     }
17341
17342   if (die->sibling != NULL && level > 0)
17343     {
17344       dump_die_1 (f, level, max_level, die->sibling);
17345     }
17346 }
17347
17348 /* This is called from the pdie macro in gdbinit.in.
17349    It's not static so gcc will keep a copy callable from gdb.  */
17350
17351 void
17352 dump_die (struct die_info *die, int max_level)
17353 {
17354   dump_die_1 (gdb_stdlog, 0, max_level, die);
17355 }
17356
17357 static void
17358 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17359 {
17360   void **slot;
17361
17362   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17363                                    INSERT);
17364
17365   *slot = die;
17366 }
17367
17368 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17369    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
17370
17371 static int
17372 is_ref_attr (struct attribute *attr)
17373 {
17374   switch (attr->form)
17375     {
17376     case DW_FORM_ref_addr:
17377     case DW_FORM_ref1:
17378     case DW_FORM_ref2:
17379     case DW_FORM_ref4:
17380     case DW_FORM_ref8:
17381     case DW_FORM_ref_udata:
17382     case DW_FORM_GNU_ref_alt:
17383       return 1;
17384     default:
17385       return 0;
17386     }
17387 }
17388
17389 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
17390    required kind.  */
17391
17392 static sect_offset
17393 dwarf2_get_ref_die_offset (struct attribute *attr)
17394 {
17395   sect_offset retval = { DW_UNSND (attr) };
17396
17397   if (is_ref_attr (attr))
17398     return retval;
17399
17400   retval.sect_off = 0;
17401   complaint (&symfile_complaints,
17402              _("unsupported die ref attribute form: '%s'"),
17403              dwarf_form_name (attr->form));
17404   return retval;
17405 }
17406
17407 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
17408  * the value held by the attribute is not constant.  */
17409
17410 static LONGEST
17411 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17412 {
17413   if (attr->form == DW_FORM_sdata)
17414     return DW_SND (attr);
17415   else if (attr->form == DW_FORM_udata
17416            || attr->form == DW_FORM_data1
17417            || attr->form == DW_FORM_data2
17418            || attr->form == DW_FORM_data4
17419            || attr->form == DW_FORM_data8)
17420     return DW_UNSND (attr);
17421   else
17422     {
17423       complaint (&symfile_complaints,
17424                  _("Attribute value is not a constant (%s)"),
17425                  dwarf_form_name (attr->form));
17426       return default_value;
17427     }
17428 }
17429
17430 /* Follow reference or signature attribute ATTR of SRC_DIE.
17431    On entry *REF_CU is the CU of SRC_DIE.
17432    On exit *REF_CU is the CU of the result.  */
17433
17434 static struct die_info *
17435 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17436                        struct dwarf2_cu **ref_cu)
17437 {
17438   struct die_info *die;
17439
17440   if (is_ref_attr (attr))
17441     die = follow_die_ref (src_die, attr, ref_cu);
17442   else if (attr->form == DW_FORM_ref_sig8)
17443     die = follow_die_sig (src_die, attr, ref_cu);
17444   else
17445     {
17446       dump_die_for_error (src_die);
17447       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17448              (*ref_cu)->objfile->name);
17449     }
17450
17451   return die;
17452 }
17453
17454 /* Follow reference OFFSET.
17455    On entry *REF_CU is the CU of the source die referencing OFFSET.
17456    On exit *REF_CU is the CU of the result.
17457    Returns NULL if OFFSET is invalid.  */
17458
17459 static struct die_info *
17460 follow_die_offset (sect_offset offset, int offset_in_dwz,
17461                    struct dwarf2_cu **ref_cu)
17462 {
17463   struct die_info temp_die;
17464   struct dwarf2_cu *target_cu, *cu = *ref_cu;
17465
17466   gdb_assert (cu->per_cu != NULL);
17467
17468   target_cu = cu;
17469
17470   if (cu->per_cu->is_debug_types)
17471     {
17472       /* .debug_types CUs cannot reference anything outside their CU.
17473          If they need to, they have to reference a signatured type via
17474          DW_FORM_ref_sig8.  */
17475       if (! offset_in_cu_p (&cu->header, offset))
17476         return NULL;
17477     }
17478   else if (offset_in_dwz != cu->per_cu->is_dwz
17479            || ! offset_in_cu_p (&cu->header, offset))
17480     {
17481       struct dwarf2_per_cu_data *per_cu;
17482
17483       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
17484                                                  cu->objfile);
17485
17486       /* If necessary, add it to the queue and load its DIEs.  */
17487       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
17488         load_full_comp_unit (per_cu, cu->language);
17489
17490       target_cu = per_cu->cu;
17491     }
17492   else if (cu->dies == NULL)
17493     {
17494       /* We're loading full DIEs during partial symbol reading.  */
17495       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
17496       load_full_comp_unit (cu->per_cu, language_minimal);
17497     }
17498
17499   *ref_cu = target_cu;
17500   temp_die.offset = offset;
17501   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
17502 }
17503
17504 /* Follow reference attribute ATTR of SRC_DIE.
17505    On entry *REF_CU is the CU of SRC_DIE.
17506    On exit *REF_CU is the CU of the result.  */
17507
17508 static struct die_info *
17509 follow_die_ref (struct die_info *src_die, struct attribute *attr,
17510                 struct dwarf2_cu **ref_cu)
17511 {
17512   sect_offset offset = dwarf2_get_ref_die_offset (attr);
17513   struct dwarf2_cu *cu = *ref_cu;
17514   struct die_info *die;
17515
17516   die = follow_die_offset (offset,
17517                            (attr->form == DW_FORM_GNU_ref_alt
17518                             || cu->per_cu->is_dwz),
17519                            ref_cu);
17520   if (!die)
17521     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
17522            "at 0x%x [in module %s]"),
17523            offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
17524
17525   return die;
17526 }
17527
17528 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
17529    Returned value is intended for DW_OP_call*.  Returned
17530    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
17531
17532 struct dwarf2_locexpr_baton
17533 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
17534                                struct dwarf2_per_cu_data *per_cu,
17535                                CORE_ADDR (*get_frame_pc) (void *baton),
17536                                void *baton)
17537 {
17538   struct dwarf2_cu *cu;
17539   struct die_info *die;
17540   struct attribute *attr;
17541   struct dwarf2_locexpr_baton retval;
17542
17543   dw2_setup (per_cu->objfile);
17544
17545   if (per_cu->cu == NULL)
17546     load_cu (per_cu);
17547   cu = per_cu->cu;
17548
17549   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
17550   if (!die)
17551     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
17552            offset.sect_off, per_cu->objfile->name);
17553
17554   attr = dwarf2_attr (die, DW_AT_location, cu);
17555   if (!attr)
17556     {
17557       /* DWARF: "If there is no such attribute, then there is no effect.".
17558          DATA is ignored if SIZE is 0.  */
17559
17560       retval.data = NULL;
17561       retval.size = 0;
17562     }
17563   else if (attr_form_is_section_offset (attr))
17564     {
17565       struct dwarf2_loclist_baton loclist_baton;
17566       CORE_ADDR pc = (*get_frame_pc) (baton);
17567       size_t size;
17568
17569       fill_in_loclist_baton (cu, &loclist_baton, attr);
17570
17571       retval.data = dwarf2_find_location_expression (&loclist_baton,
17572                                                      &size, pc);
17573       retval.size = size;
17574     }
17575   else
17576     {
17577       if (!attr_form_is_block (attr))
17578         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
17579                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
17580                offset.sect_off, per_cu->objfile->name);
17581
17582       retval.data = DW_BLOCK (attr)->data;
17583       retval.size = DW_BLOCK (attr)->size;
17584     }
17585   retval.per_cu = cu->per_cu;
17586
17587   age_cached_comp_units ();
17588
17589   return retval;
17590 }
17591
17592 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
17593    offset.  */
17594
17595 struct dwarf2_locexpr_baton
17596 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
17597                              struct dwarf2_per_cu_data *per_cu,
17598                              CORE_ADDR (*get_frame_pc) (void *baton),
17599                              void *baton)
17600 {
17601   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
17602
17603   return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
17604 }
17605
17606 /* Return the type of the DIE at DIE_OFFSET in the CU named by
17607    PER_CU.  */
17608
17609 struct type *
17610 dwarf2_get_die_type (cu_offset die_offset,
17611                      struct dwarf2_per_cu_data *per_cu)
17612 {
17613   sect_offset die_offset_sect;
17614
17615   dw2_setup (per_cu->objfile);
17616
17617   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
17618   return get_die_type_at_offset (die_offset_sect, per_cu);
17619 }
17620
17621 /* Follow the signature attribute ATTR in SRC_DIE.
17622    On entry *REF_CU is the CU of SRC_DIE.
17623    On exit *REF_CU is the CU of the result.  */
17624
17625 static struct die_info *
17626 follow_die_sig (struct die_info *src_die, struct attribute *attr,
17627                 struct dwarf2_cu **ref_cu)
17628 {
17629   struct objfile *objfile = (*ref_cu)->objfile;
17630   struct die_info temp_die;
17631   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
17632   struct dwarf2_cu *sig_cu;
17633   struct die_info *die;
17634
17635   /* sig_type will be NULL if the signatured type is missing from
17636      the debug info.  */
17637   if (sig_type == NULL)
17638     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
17639              "at 0x%x [in module %s]"),
17640            src_die->offset.sect_off, objfile->name);
17641
17642   /* If necessary, add it to the queue and load its DIEs.  */
17643
17644   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
17645     read_signatured_type (sig_type);
17646
17647   gdb_assert (sig_type->per_cu.cu != NULL);
17648
17649   sig_cu = sig_type->per_cu.cu;
17650   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
17651   temp_die.offset = sig_type->type_offset_in_section;
17652   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
17653                              temp_die.offset.sect_off);
17654   if (die)
17655     {
17656       /* For .gdb_index version 7 keep track of included TUs.
17657          http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
17658       if (dwarf2_per_objfile->index_table != NULL
17659           && dwarf2_per_objfile->index_table->version <= 7)
17660         {
17661           VEC_safe_push (dwarf2_per_cu_ptr,
17662                          (*ref_cu)->per_cu->imported_symtabs,
17663                          sig_cu->per_cu);
17664         }
17665
17666       *ref_cu = sig_cu;
17667       return die;
17668     }
17669
17670   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
17671          "from DIE at 0x%x [in module %s]"),
17672          temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
17673 }
17674
17675 /* Given an offset of a signatured type, return its signatured_type.  */
17676
17677 static struct signatured_type *
17678 lookup_signatured_type_at_offset (struct objfile *objfile,
17679                                   struct dwarf2_section_info *section,
17680                                   sect_offset offset)
17681 {
17682   gdb_byte *info_ptr = section->buffer + offset.sect_off;
17683   unsigned int length, initial_length_size;
17684   unsigned int sig_offset;
17685   struct signatured_type find_entry, *sig_type;
17686
17687   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
17688   sig_offset = (initial_length_size
17689                 + 2 /*version*/
17690                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
17691                 + 1 /*address_size*/);
17692   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
17693   sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
17694
17695   /* This is only used to lookup previously recorded types.
17696      If we didn't find it, it's our bug.  */
17697   gdb_assert (sig_type != NULL);
17698   gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
17699
17700   return sig_type;
17701 }
17702
17703 /* Load the DIEs associated with type unit PER_CU into memory.  */
17704
17705 static void
17706 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
17707 {
17708   struct signatured_type *sig_type;
17709
17710   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
17711   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
17712
17713   /* We have the per_cu, but we need the signatured_type.
17714      Fortunately this is an easy translation.  */
17715   gdb_assert (per_cu->is_debug_types);
17716   sig_type = (struct signatured_type *) per_cu;
17717
17718   gdb_assert (per_cu->cu == NULL);
17719
17720   read_signatured_type (sig_type);
17721
17722   gdb_assert (per_cu->cu != NULL);
17723 }
17724
17725 /* die_reader_func for read_signatured_type.
17726    This is identical to load_full_comp_unit_reader,
17727    but is kept separate for now.  */
17728
17729 static void
17730 read_signatured_type_reader (const struct die_reader_specs *reader,
17731                              gdb_byte *info_ptr,
17732                              struct die_info *comp_unit_die,
17733                              int has_children,
17734                              void *data)
17735 {
17736   struct dwarf2_cu *cu = reader->cu;
17737
17738   gdb_assert (cu->die_hash == NULL);
17739   cu->die_hash =
17740     htab_create_alloc_ex (cu->header.length / 12,
17741                           die_hash,
17742                           die_eq,
17743                           NULL,
17744                           &cu->comp_unit_obstack,
17745                           hashtab_obstack_allocate,
17746                           dummy_obstack_deallocate);
17747
17748   if (has_children)
17749     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
17750                                                   &info_ptr, comp_unit_die);
17751   cu->dies = comp_unit_die;
17752   /* comp_unit_die is not stored in die_hash, no need.  */
17753
17754   /* We try not to read any attributes in this function, because not
17755      all CUs needed for references have been loaded yet, and symbol
17756      table processing isn't initialized.  But we have to set the CU language,
17757      or we won't be able to build types correctly.
17758      Similarly, if we do not read the producer, we can not apply
17759      producer-specific interpretation.  */
17760   prepare_one_comp_unit (cu, cu->dies, language_minimal);
17761 }
17762
17763 /* Read in a signatured type and build its CU and DIEs.
17764    If the type is a stub for the real type in a DWO file,
17765    read in the real type from the DWO file as well.  */
17766
17767 static void
17768 read_signatured_type (struct signatured_type *sig_type)
17769 {
17770   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
17771
17772   gdb_assert (per_cu->is_debug_types);
17773   gdb_assert (per_cu->cu == NULL);
17774
17775   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
17776                            read_signatured_type_reader, NULL);
17777 }
17778
17779 /* Decode simple location descriptions.
17780    Given a pointer to a dwarf block that defines a location, compute
17781    the location and return the value.
17782
17783    NOTE drow/2003-11-18: This function is called in two situations
17784    now: for the address of static or global variables (partial symbols
17785    only) and for offsets into structures which are expected to be
17786    (more or less) constant.  The partial symbol case should go away,
17787    and only the constant case should remain.  That will let this
17788    function complain more accurately.  A few special modes are allowed
17789    without complaint for global variables (for instance, global
17790    register values and thread-local values).
17791
17792    A location description containing no operations indicates that the
17793    object is optimized out.  The return value is 0 for that case.
17794    FIXME drow/2003-11-16: No callers check for this case any more; soon all
17795    callers will only want a very basic result and this can become a
17796    complaint.
17797
17798    Note that stack[0] is unused except as a default error return.  */
17799
17800 static CORE_ADDR
17801 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
17802 {
17803   struct objfile *objfile = cu->objfile;
17804   size_t i;
17805   size_t size = blk->size;
17806   gdb_byte *data = blk->data;
17807   CORE_ADDR stack[64];
17808   int stacki;
17809   unsigned int bytes_read, unsnd;
17810   gdb_byte op;
17811
17812   i = 0;
17813   stacki = 0;
17814   stack[stacki] = 0;
17815   stack[++stacki] = 0;
17816
17817   while (i < size)
17818     {
17819       op = data[i++];
17820       switch (op)
17821         {
17822         case DW_OP_lit0:
17823         case DW_OP_lit1:
17824         case DW_OP_lit2:
17825         case DW_OP_lit3:
17826         case DW_OP_lit4:
17827         case DW_OP_lit5:
17828         case DW_OP_lit6:
17829         case DW_OP_lit7:
17830         case DW_OP_lit8:
17831         case DW_OP_lit9:
17832         case DW_OP_lit10:
17833         case DW_OP_lit11:
17834         case DW_OP_lit12:
17835         case DW_OP_lit13:
17836         case DW_OP_lit14:
17837         case DW_OP_lit15:
17838         case DW_OP_lit16:
17839         case DW_OP_lit17:
17840         case DW_OP_lit18:
17841         case DW_OP_lit19:
17842         case DW_OP_lit20:
17843         case DW_OP_lit21:
17844         case DW_OP_lit22:
17845         case DW_OP_lit23:
17846         case DW_OP_lit24:
17847         case DW_OP_lit25:
17848         case DW_OP_lit26:
17849         case DW_OP_lit27:
17850         case DW_OP_lit28:
17851         case DW_OP_lit29:
17852         case DW_OP_lit30:
17853         case DW_OP_lit31:
17854           stack[++stacki] = op - DW_OP_lit0;
17855           break;
17856
17857         case DW_OP_reg0:
17858         case DW_OP_reg1:
17859         case DW_OP_reg2:
17860         case DW_OP_reg3:
17861         case DW_OP_reg4:
17862         case DW_OP_reg5:
17863         case DW_OP_reg6:
17864         case DW_OP_reg7:
17865         case DW_OP_reg8:
17866         case DW_OP_reg9:
17867         case DW_OP_reg10:
17868         case DW_OP_reg11:
17869         case DW_OP_reg12:
17870         case DW_OP_reg13:
17871         case DW_OP_reg14:
17872         case DW_OP_reg15:
17873         case DW_OP_reg16:
17874         case DW_OP_reg17:
17875         case DW_OP_reg18:
17876         case DW_OP_reg19:
17877         case DW_OP_reg20:
17878         case DW_OP_reg21:
17879         case DW_OP_reg22:
17880         case DW_OP_reg23:
17881         case DW_OP_reg24:
17882         case DW_OP_reg25:
17883         case DW_OP_reg26:
17884         case DW_OP_reg27:
17885         case DW_OP_reg28:
17886         case DW_OP_reg29:
17887         case DW_OP_reg30:
17888         case DW_OP_reg31:
17889           stack[++stacki] = op - DW_OP_reg0;
17890           if (i < size)
17891             dwarf2_complex_location_expr_complaint ();
17892           break;
17893
17894         case DW_OP_regx:
17895           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
17896           i += bytes_read;
17897           stack[++stacki] = unsnd;
17898           if (i < size)
17899             dwarf2_complex_location_expr_complaint ();
17900           break;
17901
17902         case DW_OP_addr:
17903           stack[++stacki] = read_address (objfile->obfd, &data[i],
17904                                           cu, &bytes_read);
17905           i += bytes_read;
17906           break;
17907
17908         case DW_OP_const1u:
17909           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
17910           i += 1;
17911           break;
17912
17913         case DW_OP_const1s:
17914           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
17915           i += 1;
17916           break;
17917
17918         case DW_OP_const2u:
17919           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
17920           i += 2;
17921           break;
17922
17923         case DW_OP_const2s:
17924           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
17925           i += 2;
17926           break;
17927
17928         case DW_OP_const4u:
17929           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
17930           i += 4;
17931           break;
17932
17933         case DW_OP_const4s:
17934           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
17935           i += 4;
17936           break;
17937
17938         case DW_OP_const8u:
17939           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
17940           i += 8;
17941           break;
17942
17943         case DW_OP_constu:
17944           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
17945                                                   &bytes_read);
17946           i += bytes_read;
17947           break;
17948
17949         case DW_OP_consts:
17950           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
17951           i += bytes_read;
17952           break;
17953
17954         case DW_OP_dup:
17955           stack[stacki + 1] = stack[stacki];
17956           stacki++;
17957           break;
17958
17959         case DW_OP_plus:
17960           stack[stacki - 1] += stack[stacki];
17961           stacki--;
17962           break;
17963
17964         case DW_OP_plus_uconst:
17965           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
17966                                                  &bytes_read);
17967           i += bytes_read;
17968           break;
17969
17970         case DW_OP_minus:
17971           stack[stacki - 1] -= stack[stacki];
17972           stacki--;
17973           break;
17974
17975         case DW_OP_deref:
17976           /* If we're not the last op, then we definitely can't encode
17977              this using GDB's address_class enum.  This is valid for partial
17978              global symbols, although the variable's address will be bogus
17979              in the psymtab.  */
17980           if (i < size)
17981             dwarf2_complex_location_expr_complaint ();
17982           break;
17983
17984         case DW_OP_GNU_push_tls_address:
17985           /* The top of the stack has the offset from the beginning
17986              of the thread control block at which the variable is located.  */
17987           /* Nothing should follow this operator, so the top of stack would
17988              be returned.  */
17989           /* This is valid for partial global symbols, but the variable's
17990              address will be bogus in the psymtab.  Make it always at least
17991              non-zero to not look as a variable garbage collected by linker
17992              which have DW_OP_addr 0.  */
17993           if (i < size)
17994             dwarf2_complex_location_expr_complaint ();
17995           stack[stacki]++;
17996           break;
17997
17998         case DW_OP_GNU_uninit:
17999           break;
18000
18001         case DW_OP_GNU_addr_index:
18002         case DW_OP_GNU_const_index:
18003           stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18004                                                          &bytes_read);
18005           i += bytes_read;
18006           break;
18007
18008         default:
18009           {
18010             const char *name = get_DW_OP_name (op);
18011
18012             if (name)
18013               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18014                          name);
18015             else
18016               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18017                          op);
18018           }
18019
18020           return (stack[stacki]);
18021         }
18022
18023       /* Enforce maximum stack depth of SIZE-1 to avoid writing
18024          outside of the allocated space.  Also enforce minimum>0.  */
18025       if (stacki >= ARRAY_SIZE (stack) - 1)
18026         {
18027           complaint (&symfile_complaints,
18028                      _("location description stack overflow"));
18029           return 0;
18030         }
18031
18032       if (stacki <= 0)
18033         {
18034           complaint (&symfile_complaints,
18035                      _("location description stack underflow"));
18036           return 0;
18037         }
18038     }
18039   return (stack[stacki]);
18040 }
18041
18042 /* memory allocation interface */
18043
18044 static struct dwarf_block *
18045 dwarf_alloc_block (struct dwarf2_cu *cu)
18046 {
18047   struct dwarf_block *blk;
18048
18049   blk = (struct dwarf_block *)
18050     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18051   return (blk);
18052 }
18053
18054 static struct die_info *
18055 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18056 {
18057   struct die_info *die;
18058   size_t size = sizeof (struct die_info);
18059
18060   if (num_attrs > 1)
18061     size += (num_attrs - 1) * sizeof (struct attribute);
18062
18063   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18064   memset (die, 0, sizeof (struct die_info));
18065   return (die);
18066 }
18067
18068 \f
18069 /* Macro support.  */
18070
18071 /* Return file name relative to the compilation directory of file number I in
18072    *LH's file name table.  The result is allocated using xmalloc; the caller is
18073    responsible for freeing it.  */
18074
18075 static char *
18076 file_file_name (int file, struct line_header *lh)
18077 {
18078   /* Is the file number a valid index into the line header's file name
18079      table?  Remember that file numbers start with one, not zero.  */
18080   if (1 <= file && file <= lh->num_file_names)
18081     {
18082       struct file_entry *fe = &lh->file_names[file - 1];
18083
18084       if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18085         return xstrdup (fe->name);
18086       return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18087                      fe->name, NULL);
18088     }
18089   else
18090     {
18091       /* The compiler produced a bogus file number.  We can at least
18092          record the macro definitions made in the file, even if we
18093          won't be able to find the file by name.  */
18094       char fake_name[80];
18095
18096       xsnprintf (fake_name, sizeof (fake_name),
18097                  "<bad macro file number %d>", file);
18098
18099       complaint (&symfile_complaints,
18100                  _("bad file number in macro information (%d)"),
18101                  file);
18102
18103       return xstrdup (fake_name);
18104     }
18105 }
18106
18107 /* Return the full name of file number I in *LH's file name table.
18108    Use COMP_DIR as the name of the current directory of the
18109    compilation.  The result is allocated using xmalloc; the caller is
18110    responsible for freeing it.  */
18111 static char *
18112 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18113 {
18114   /* Is the file number a valid index into the line header's file name
18115      table?  Remember that file numbers start with one, not zero.  */
18116   if (1 <= file && file <= lh->num_file_names)
18117     {
18118       char *relative = file_file_name (file, lh);
18119
18120       if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18121         return relative;
18122       return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18123     }
18124   else
18125     return file_file_name (file, lh);
18126 }
18127
18128
18129 static struct macro_source_file *
18130 macro_start_file (int file, int line,
18131                   struct macro_source_file *current_file,
18132                   const char *comp_dir,
18133                   struct line_header *lh, struct objfile *objfile)
18134 {
18135   /* File name relative to the compilation directory of this source file.  */
18136   char *file_name = file_file_name (file, lh);
18137
18138   /* We don't create a macro table for this compilation unit
18139      at all until we actually get a filename.  */
18140   if (! pending_macros)
18141     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18142                                       objfile->per_bfd->macro_cache,
18143                                       comp_dir);
18144
18145   if (! current_file)
18146     {
18147       /* If we have no current file, then this must be the start_file
18148          directive for the compilation unit's main source file.  */
18149       current_file = macro_set_main (pending_macros, file_name);
18150       macro_define_special (pending_macros);
18151     }
18152   else
18153     current_file = macro_include (current_file, line, file_name);
18154
18155   xfree (file_name);
18156
18157   return current_file;
18158 }
18159
18160
18161 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18162    followed by a null byte.  */
18163 static char *
18164 copy_string (const char *buf, int len)
18165 {
18166   char *s = xmalloc (len + 1);
18167
18168   memcpy (s, buf, len);
18169   s[len] = '\0';
18170   return s;
18171 }
18172
18173
18174 static const char *
18175 consume_improper_spaces (const char *p, const char *body)
18176 {
18177   if (*p == ' ')
18178     {
18179       complaint (&symfile_complaints,
18180                  _("macro definition contains spaces "
18181                    "in formal argument list:\n`%s'"),
18182                  body);
18183
18184       while (*p == ' ')
18185         p++;
18186     }
18187
18188   return p;
18189 }
18190
18191
18192 static void
18193 parse_macro_definition (struct macro_source_file *file, int line,
18194                         const char *body)
18195 {
18196   const char *p;
18197
18198   /* The body string takes one of two forms.  For object-like macro
18199      definitions, it should be:
18200
18201         <macro name> " " <definition>
18202
18203      For function-like macro definitions, it should be:
18204
18205         <macro name> "() " <definition>
18206      or
18207         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18208
18209      Spaces may appear only where explicitly indicated, and in the
18210      <definition>.
18211
18212      The Dwarf 2 spec says that an object-like macro's name is always
18213      followed by a space, but versions of GCC around March 2002 omit
18214      the space when the macro's definition is the empty string.
18215
18216      The Dwarf 2 spec says that there should be no spaces between the
18217      formal arguments in a function-like macro's formal argument list,
18218      but versions of GCC around March 2002 include spaces after the
18219      commas.  */
18220
18221
18222   /* Find the extent of the macro name.  The macro name is terminated
18223      by either a space or null character (for an object-like macro) or
18224      an opening paren (for a function-like macro).  */
18225   for (p = body; *p; p++)
18226     if (*p == ' ' || *p == '(')
18227       break;
18228
18229   if (*p == ' ' || *p == '\0')
18230     {
18231       /* It's an object-like macro.  */
18232       int name_len = p - body;
18233       char *name = copy_string (body, name_len);
18234       const char *replacement;
18235
18236       if (*p == ' ')
18237         replacement = body + name_len + 1;
18238       else
18239         {
18240           dwarf2_macro_malformed_definition_complaint (body);
18241           replacement = body + name_len;
18242         }
18243
18244       macro_define_object (file, line, name, replacement);
18245
18246       xfree (name);
18247     }
18248   else if (*p == '(')
18249     {
18250       /* It's a function-like macro.  */
18251       char *name = copy_string (body, p - body);
18252       int argc = 0;
18253       int argv_size = 1;
18254       char **argv = xmalloc (argv_size * sizeof (*argv));
18255
18256       p++;
18257
18258       p = consume_improper_spaces (p, body);
18259
18260       /* Parse the formal argument list.  */
18261       while (*p && *p != ')')
18262         {
18263           /* Find the extent of the current argument name.  */
18264           const char *arg_start = p;
18265
18266           while (*p && *p != ',' && *p != ')' && *p != ' ')
18267             p++;
18268
18269           if (! *p || p == arg_start)
18270             dwarf2_macro_malformed_definition_complaint (body);
18271           else
18272             {
18273               /* Make sure argv has room for the new argument.  */
18274               if (argc >= argv_size)
18275                 {
18276                   argv_size *= 2;
18277                   argv = xrealloc (argv, argv_size * sizeof (*argv));
18278                 }
18279
18280               argv[argc++] = copy_string (arg_start, p - arg_start);
18281             }
18282
18283           p = consume_improper_spaces (p, body);
18284
18285           /* Consume the comma, if present.  */
18286           if (*p == ',')
18287             {
18288               p++;
18289
18290               p = consume_improper_spaces (p, body);
18291             }
18292         }
18293
18294       if (*p == ')')
18295         {
18296           p++;
18297
18298           if (*p == ' ')
18299             /* Perfectly formed definition, no complaints.  */
18300             macro_define_function (file, line, name,
18301                                    argc, (const char **) argv,
18302                                    p + 1);
18303           else if (*p == '\0')
18304             {
18305               /* Complain, but do define it.  */
18306               dwarf2_macro_malformed_definition_complaint (body);
18307               macro_define_function (file, line, name,
18308                                      argc, (const char **) argv,
18309                                      p);
18310             }
18311           else
18312             /* Just complain.  */
18313             dwarf2_macro_malformed_definition_complaint (body);
18314         }
18315       else
18316         /* Just complain.  */
18317         dwarf2_macro_malformed_definition_complaint (body);
18318
18319       xfree (name);
18320       {
18321         int i;
18322
18323         for (i = 0; i < argc; i++)
18324           xfree (argv[i]);
18325       }
18326       xfree (argv);
18327     }
18328   else
18329     dwarf2_macro_malformed_definition_complaint (body);
18330 }
18331
18332 /* Skip some bytes from BYTES according to the form given in FORM.
18333    Returns the new pointer.  */
18334
18335 static gdb_byte *
18336 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
18337                  enum dwarf_form form,
18338                  unsigned int offset_size,
18339                  struct dwarf2_section_info *section)
18340 {
18341   unsigned int bytes_read;
18342
18343   switch (form)
18344     {
18345     case DW_FORM_data1:
18346     case DW_FORM_flag:
18347       ++bytes;
18348       break;
18349
18350     case DW_FORM_data2:
18351       bytes += 2;
18352       break;
18353
18354     case DW_FORM_data4:
18355       bytes += 4;
18356       break;
18357
18358     case DW_FORM_data8:
18359       bytes += 8;
18360       break;
18361
18362     case DW_FORM_string:
18363       read_direct_string (abfd, bytes, &bytes_read);
18364       bytes += bytes_read;
18365       break;
18366
18367     case DW_FORM_sec_offset:
18368     case DW_FORM_strp:
18369     case DW_FORM_GNU_strp_alt:
18370       bytes += offset_size;
18371       break;
18372
18373     case DW_FORM_block:
18374       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
18375       bytes += bytes_read;
18376       break;
18377
18378     case DW_FORM_block1:
18379       bytes += 1 + read_1_byte (abfd, bytes);
18380       break;
18381     case DW_FORM_block2:
18382       bytes += 2 + read_2_bytes (abfd, bytes);
18383       break;
18384     case DW_FORM_block4:
18385       bytes += 4 + read_4_bytes (abfd, bytes);
18386       break;
18387
18388     case DW_FORM_sdata:
18389     case DW_FORM_udata:
18390     case DW_FORM_GNU_addr_index:
18391     case DW_FORM_GNU_str_index:
18392       bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
18393       if (bytes == NULL)
18394         {
18395           dwarf2_section_buffer_overflow_complaint (section);
18396           return NULL;
18397         }
18398       break;
18399
18400     default:
18401       {
18402       complain:
18403         complaint (&symfile_complaints,
18404                    _("invalid form 0x%x in `%s'"),
18405                    form,
18406                    section->asection->name);
18407         return NULL;
18408       }
18409     }
18410
18411   return bytes;
18412 }
18413
18414 /* A helper for dwarf_decode_macros that handles skipping an unknown
18415    opcode.  Returns an updated pointer to the macro data buffer; or,
18416    on error, issues a complaint and returns NULL.  */
18417
18418 static gdb_byte *
18419 skip_unknown_opcode (unsigned int opcode,
18420                      gdb_byte **opcode_definitions,
18421                      gdb_byte *mac_ptr, gdb_byte *mac_end,
18422                      bfd *abfd,
18423                      unsigned int offset_size,
18424                      struct dwarf2_section_info *section)
18425 {
18426   unsigned int bytes_read, i;
18427   unsigned long arg;
18428   gdb_byte *defn;
18429
18430   if (opcode_definitions[opcode] == NULL)
18431     {
18432       complaint (&symfile_complaints,
18433                  _("unrecognized DW_MACFINO opcode 0x%x"),
18434                  opcode);
18435       return NULL;
18436     }
18437
18438   defn = opcode_definitions[opcode];
18439   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
18440   defn += bytes_read;
18441
18442   for (i = 0; i < arg; ++i)
18443     {
18444       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
18445                                  section);
18446       if (mac_ptr == NULL)
18447         {
18448           /* skip_form_bytes already issued the complaint.  */
18449           return NULL;
18450         }
18451     }
18452
18453   return mac_ptr;
18454 }
18455
18456 /* A helper function which parses the header of a macro section.
18457    If the macro section is the extended (for now called "GNU") type,
18458    then this updates *OFFSET_SIZE.  Returns a pointer to just after
18459    the header, or issues a complaint and returns NULL on error.  */
18460
18461 static gdb_byte *
18462 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
18463                           bfd *abfd,
18464                           gdb_byte *mac_ptr,
18465                           unsigned int *offset_size,
18466                           int section_is_gnu)
18467 {
18468   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
18469
18470   if (section_is_gnu)
18471     {
18472       unsigned int version, flags;
18473
18474       version = read_2_bytes (abfd, mac_ptr);
18475       if (version != 4)
18476         {
18477           complaint (&symfile_complaints,
18478                      _("unrecognized version `%d' in .debug_macro section"),
18479                      version);
18480           return NULL;
18481         }
18482       mac_ptr += 2;
18483
18484       flags = read_1_byte (abfd, mac_ptr);
18485       ++mac_ptr;
18486       *offset_size = (flags & 1) ? 8 : 4;
18487
18488       if ((flags & 2) != 0)
18489         /* We don't need the line table offset.  */
18490         mac_ptr += *offset_size;
18491
18492       /* Vendor opcode descriptions.  */
18493       if ((flags & 4) != 0)
18494         {
18495           unsigned int i, count;
18496
18497           count = read_1_byte (abfd, mac_ptr);
18498           ++mac_ptr;
18499           for (i = 0; i < count; ++i)
18500             {
18501               unsigned int opcode, bytes_read;
18502               unsigned long arg;
18503
18504               opcode = read_1_byte (abfd, mac_ptr);
18505               ++mac_ptr;
18506               opcode_definitions[opcode] = mac_ptr;
18507               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18508               mac_ptr += bytes_read;
18509               mac_ptr += arg;
18510             }
18511         }
18512     }
18513
18514   return mac_ptr;
18515 }
18516
18517 /* A helper for dwarf_decode_macros that handles the GNU extensions,
18518    including DW_MACRO_GNU_transparent_include.  */
18519
18520 static void
18521 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
18522                           struct macro_source_file *current_file,
18523                           struct line_header *lh, const char *comp_dir,
18524                           struct dwarf2_section_info *section,
18525                           int section_is_gnu, int section_is_dwz,
18526                           unsigned int offset_size,
18527                           struct objfile *objfile,
18528                           htab_t include_hash)
18529 {
18530   enum dwarf_macro_record_type macinfo_type;
18531   int at_commandline;
18532   gdb_byte *opcode_definitions[256];
18533
18534   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18535                                       &offset_size, section_is_gnu);
18536   if (mac_ptr == NULL)
18537     {
18538       /* We already issued a complaint.  */
18539       return;
18540     }
18541
18542   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
18543      GDB is still reading the definitions from command line.  First
18544      DW_MACINFO_start_file will need to be ignored as it was already executed
18545      to create CURRENT_FILE for the main source holding also the command line
18546      definitions.  On first met DW_MACINFO_start_file this flag is reset to
18547      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
18548
18549   at_commandline = 1;
18550
18551   do
18552     {
18553       /* Do we at least have room for a macinfo type byte?  */
18554       if (mac_ptr >= mac_end)
18555         {
18556           dwarf2_section_buffer_overflow_complaint (section);
18557           break;
18558         }
18559
18560       macinfo_type = read_1_byte (abfd, mac_ptr);
18561       mac_ptr++;
18562
18563       /* Note that we rely on the fact that the corresponding GNU and
18564          DWARF constants are the same.  */
18565       switch (macinfo_type)
18566         {
18567           /* A zero macinfo type indicates the end of the macro
18568              information.  */
18569         case 0:
18570           break;
18571
18572         case DW_MACRO_GNU_define:
18573         case DW_MACRO_GNU_undef:
18574         case DW_MACRO_GNU_define_indirect:
18575         case DW_MACRO_GNU_undef_indirect:
18576         case DW_MACRO_GNU_define_indirect_alt:
18577         case DW_MACRO_GNU_undef_indirect_alt:
18578           {
18579             unsigned int bytes_read;
18580             int line;
18581             char *body;
18582             int is_define;
18583
18584             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18585             mac_ptr += bytes_read;
18586
18587             if (macinfo_type == DW_MACRO_GNU_define
18588                 || macinfo_type == DW_MACRO_GNU_undef)
18589               {
18590                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
18591                 mac_ptr += bytes_read;
18592               }
18593             else
18594               {
18595                 LONGEST str_offset;
18596
18597                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
18598                 mac_ptr += offset_size;
18599
18600                 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
18601                     || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
18602                     || section_is_dwz)
18603                   {
18604                     struct dwz_file *dwz = dwarf2_get_dwz_file ();
18605
18606                     body = read_indirect_string_from_dwz (dwz, str_offset);
18607                   }
18608                 else
18609                   body = read_indirect_string_at_offset (abfd, str_offset);
18610               }
18611
18612             is_define = (macinfo_type == DW_MACRO_GNU_define
18613                          || macinfo_type == DW_MACRO_GNU_define_indirect
18614                          || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
18615             if (! current_file)
18616               {
18617                 /* DWARF violation as no main source is present.  */
18618                 complaint (&symfile_complaints,
18619                            _("debug info with no main source gives macro %s "
18620                              "on line %d: %s"),
18621                            is_define ? _("definition") : _("undefinition"),
18622                            line, body);
18623                 break;
18624               }
18625             if ((line == 0 && !at_commandline)
18626                 || (line != 0 && at_commandline))
18627               complaint (&symfile_complaints,
18628                          _("debug info gives %s macro %s with %s line %d: %s"),
18629                          at_commandline ? _("command-line") : _("in-file"),
18630                          is_define ? _("definition") : _("undefinition"),
18631                          line == 0 ? _("zero") : _("non-zero"), line, body);
18632
18633             if (is_define)
18634               parse_macro_definition (current_file, line, body);
18635             else
18636               {
18637                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
18638                             || macinfo_type == DW_MACRO_GNU_undef_indirect
18639                             || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
18640                 macro_undef (current_file, line, body);
18641               }
18642           }
18643           break;
18644
18645         case DW_MACRO_GNU_start_file:
18646           {
18647             unsigned int bytes_read;
18648             int line, file;
18649
18650             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18651             mac_ptr += bytes_read;
18652             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18653             mac_ptr += bytes_read;
18654
18655             if ((line == 0 && !at_commandline)
18656                 || (line != 0 && at_commandline))
18657               complaint (&symfile_complaints,
18658                          _("debug info gives source %d included "
18659                            "from %s at %s line %d"),
18660                          file, at_commandline ? _("command-line") : _("file"),
18661                          line == 0 ? _("zero") : _("non-zero"), line);
18662
18663             if (at_commandline)
18664               {
18665                 /* This DW_MACRO_GNU_start_file was executed in the
18666                    pass one.  */
18667                 at_commandline = 0;
18668               }
18669             else
18670               current_file = macro_start_file (file, line,
18671                                                current_file, comp_dir,
18672                                                lh, objfile);
18673           }
18674           break;
18675
18676         case DW_MACRO_GNU_end_file:
18677           if (! current_file)
18678             complaint (&symfile_complaints,
18679                        _("macro debug info has an unmatched "
18680                          "`close_file' directive"));
18681           else
18682             {
18683               current_file = current_file->included_by;
18684               if (! current_file)
18685                 {
18686                   enum dwarf_macro_record_type next_type;
18687
18688                   /* GCC circa March 2002 doesn't produce the zero
18689                      type byte marking the end of the compilation
18690                      unit.  Complain if it's not there, but exit no
18691                      matter what.  */
18692
18693                   /* Do we at least have room for a macinfo type byte?  */
18694                   if (mac_ptr >= mac_end)
18695                     {
18696                       dwarf2_section_buffer_overflow_complaint (section);
18697                       return;
18698                     }
18699
18700                   /* We don't increment mac_ptr here, so this is just
18701                      a look-ahead.  */
18702                   next_type = read_1_byte (abfd, mac_ptr);
18703                   if (next_type != 0)
18704                     complaint (&symfile_complaints,
18705                                _("no terminating 0-type entry for "
18706                                  "macros in `.debug_macinfo' section"));
18707
18708                   return;
18709                 }
18710             }
18711           break;
18712
18713         case DW_MACRO_GNU_transparent_include:
18714         case DW_MACRO_GNU_transparent_include_alt:
18715           {
18716             LONGEST offset;
18717             void **slot;
18718             bfd *include_bfd = abfd;
18719             struct dwarf2_section_info *include_section = section;
18720             struct dwarf2_section_info alt_section;
18721             gdb_byte *include_mac_end = mac_end;
18722             int is_dwz = section_is_dwz;
18723             gdb_byte *new_mac_ptr;
18724
18725             offset = read_offset_1 (abfd, mac_ptr, offset_size);
18726             mac_ptr += offset_size;
18727
18728             if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
18729               {
18730                 struct dwz_file *dwz = dwarf2_get_dwz_file ();
18731
18732                 dwarf2_read_section (dwarf2_per_objfile->objfile,
18733                                      &dwz->macro);
18734
18735                 include_bfd = dwz->macro.asection->owner;
18736                 include_section = &dwz->macro;
18737                 include_mac_end = dwz->macro.buffer + dwz->macro.size;
18738                 is_dwz = 1;
18739               }
18740
18741             new_mac_ptr = include_section->buffer + offset;
18742             slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
18743
18744             if (*slot != NULL)
18745               {
18746                 /* This has actually happened; see
18747                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
18748                 complaint (&symfile_complaints,
18749                            _("recursive DW_MACRO_GNU_transparent_include in "
18750                              ".debug_macro section"));
18751               }
18752             else
18753               {
18754                 *slot = new_mac_ptr;
18755
18756                 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
18757                                           include_mac_end, current_file,
18758                                           lh, comp_dir,
18759                                           section, section_is_gnu, is_dwz,
18760                                           offset_size, objfile, include_hash);
18761
18762                 htab_remove_elt (include_hash, new_mac_ptr);
18763               }
18764           }
18765           break;
18766
18767         case DW_MACINFO_vendor_ext:
18768           if (!section_is_gnu)
18769             {
18770               unsigned int bytes_read;
18771               int constant;
18772
18773               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18774               mac_ptr += bytes_read;
18775               read_direct_string (abfd, mac_ptr, &bytes_read);
18776               mac_ptr += bytes_read;
18777
18778               /* We don't recognize any vendor extensions.  */
18779               break;
18780             }
18781           /* FALLTHROUGH */
18782
18783         default:
18784           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18785                                          mac_ptr, mac_end, abfd, offset_size,
18786                                          section);
18787           if (mac_ptr == NULL)
18788             return;
18789           break;
18790         }
18791     } while (macinfo_type != 0);
18792 }
18793
18794 static void
18795 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
18796                      const char *comp_dir, int section_is_gnu)
18797 {
18798   struct objfile *objfile = dwarf2_per_objfile->objfile;
18799   struct line_header *lh = cu->line_header;
18800   bfd *abfd;
18801   gdb_byte *mac_ptr, *mac_end;
18802   struct macro_source_file *current_file = 0;
18803   enum dwarf_macro_record_type macinfo_type;
18804   unsigned int offset_size = cu->header.offset_size;
18805   gdb_byte *opcode_definitions[256];
18806   struct cleanup *cleanup;
18807   htab_t include_hash;
18808   void **slot;
18809   struct dwarf2_section_info *section;
18810   const char *section_name;
18811
18812   if (cu->dwo_unit != NULL)
18813     {
18814       if (section_is_gnu)
18815         {
18816           section = &cu->dwo_unit->dwo_file->sections.macro;
18817           section_name = ".debug_macro.dwo";
18818         }
18819       else
18820         {
18821           section = &cu->dwo_unit->dwo_file->sections.macinfo;
18822           section_name = ".debug_macinfo.dwo";
18823         }
18824     }
18825   else
18826     {
18827       if (section_is_gnu)
18828         {
18829           section = &dwarf2_per_objfile->macro;
18830           section_name = ".debug_macro";
18831         }
18832       else
18833         {
18834           section = &dwarf2_per_objfile->macinfo;
18835           section_name = ".debug_macinfo";
18836         }
18837     }
18838
18839   dwarf2_read_section (objfile, section);
18840   if (section->buffer == NULL)
18841     {
18842       complaint (&symfile_complaints, _("missing %s section"), section_name);
18843       return;
18844     }
18845   abfd = section->asection->owner;
18846
18847   /* First pass: Find the name of the base filename.
18848      This filename is needed in order to process all macros whose definition
18849      (or undefinition) comes from the command line.  These macros are defined
18850      before the first DW_MACINFO_start_file entry, and yet still need to be
18851      associated to the base file.
18852
18853      To determine the base file name, we scan the macro definitions until we
18854      reach the first DW_MACINFO_start_file entry.  We then initialize
18855      CURRENT_FILE accordingly so that any macro definition found before the
18856      first DW_MACINFO_start_file can still be associated to the base file.  */
18857
18858   mac_ptr = section->buffer + offset;
18859   mac_end = section->buffer + section->size;
18860
18861   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
18862                                       &offset_size, section_is_gnu);
18863   if (mac_ptr == NULL)
18864     {
18865       /* We already issued a complaint.  */
18866       return;
18867     }
18868
18869   do
18870     {
18871       /* Do we at least have room for a macinfo type byte?  */
18872       if (mac_ptr >= mac_end)
18873         {
18874           /* Complaint is printed during the second pass as GDB will probably
18875              stop the first pass earlier upon finding
18876              DW_MACINFO_start_file.  */
18877           break;
18878         }
18879
18880       macinfo_type = read_1_byte (abfd, mac_ptr);
18881       mac_ptr++;
18882
18883       /* Note that we rely on the fact that the corresponding GNU and
18884          DWARF constants are the same.  */
18885       switch (macinfo_type)
18886         {
18887           /* A zero macinfo type indicates the end of the macro
18888              information.  */
18889         case 0:
18890           break;
18891
18892         case DW_MACRO_GNU_define:
18893         case DW_MACRO_GNU_undef:
18894           /* Only skip the data by MAC_PTR.  */
18895           {
18896             unsigned int bytes_read;
18897
18898             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18899             mac_ptr += bytes_read;
18900             read_direct_string (abfd, mac_ptr, &bytes_read);
18901             mac_ptr += bytes_read;
18902           }
18903           break;
18904
18905         case DW_MACRO_GNU_start_file:
18906           {
18907             unsigned int bytes_read;
18908             int line, file;
18909
18910             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18911             mac_ptr += bytes_read;
18912             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18913             mac_ptr += bytes_read;
18914
18915             current_file = macro_start_file (file, line, current_file,
18916                                              comp_dir, lh, objfile);
18917           }
18918           break;
18919
18920         case DW_MACRO_GNU_end_file:
18921           /* No data to skip by MAC_PTR.  */
18922           break;
18923
18924         case DW_MACRO_GNU_define_indirect:
18925         case DW_MACRO_GNU_undef_indirect:
18926         case DW_MACRO_GNU_define_indirect_alt:
18927         case DW_MACRO_GNU_undef_indirect_alt:
18928           {
18929             unsigned int bytes_read;
18930
18931             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18932             mac_ptr += bytes_read;
18933             mac_ptr += offset_size;
18934           }
18935           break;
18936
18937         case DW_MACRO_GNU_transparent_include:
18938         case DW_MACRO_GNU_transparent_include_alt:
18939           /* Note that, according to the spec, a transparent include
18940              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
18941              skip this opcode.  */
18942           mac_ptr += offset_size;
18943           break;
18944
18945         case DW_MACINFO_vendor_ext:
18946           /* Only skip the data by MAC_PTR.  */
18947           if (!section_is_gnu)
18948             {
18949               unsigned int bytes_read;
18950
18951               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
18952               mac_ptr += bytes_read;
18953               read_direct_string (abfd, mac_ptr, &bytes_read);
18954               mac_ptr += bytes_read;
18955             }
18956           /* FALLTHROUGH */
18957
18958         default:
18959           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
18960                                          mac_ptr, mac_end, abfd, offset_size,
18961                                          section);
18962           if (mac_ptr == NULL)
18963             return;
18964           break;
18965         }
18966     } while (macinfo_type != 0 && current_file == NULL);
18967
18968   /* Second pass: Process all entries.
18969
18970      Use the AT_COMMAND_LINE flag to determine whether we are still processing
18971      command-line macro definitions/undefinitions.  This flag is unset when we
18972      reach the first DW_MACINFO_start_file entry.  */
18973
18974   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
18975                                     NULL, xcalloc, xfree);
18976   cleanup = make_cleanup_htab_delete (include_hash);
18977   mac_ptr = section->buffer + offset;
18978   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
18979   *slot = mac_ptr;
18980   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
18981                             current_file, lh, comp_dir, section,
18982                             section_is_gnu, 0,
18983                             offset_size, objfile, include_hash);
18984   do_cleanups (cleanup);
18985 }
18986
18987 /* Check if the attribute's form is a DW_FORM_block*
18988    if so return true else false.  */
18989
18990 static int
18991 attr_form_is_block (struct attribute *attr)
18992 {
18993   return (attr == NULL ? 0 :
18994       attr->form == DW_FORM_block1
18995       || attr->form == DW_FORM_block2
18996       || attr->form == DW_FORM_block4
18997       || attr->form == DW_FORM_block
18998       || attr->form == DW_FORM_exprloc);
18999 }
19000
19001 /* Return non-zero if ATTR's value is a section offset --- classes
19002    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19003    You may use DW_UNSND (attr) to retrieve such offsets.
19004
19005    Section 7.5.4, "Attribute Encodings", explains that no attribute
19006    may have a value that belongs to more than one of these classes; it
19007    would be ambiguous if we did, because we use the same forms for all
19008    of them.  */
19009
19010 static int
19011 attr_form_is_section_offset (struct attribute *attr)
19012 {
19013   return (attr->form == DW_FORM_data4
19014           || attr->form == DW_FORM_data8
19015           || attr->form == DW_FORM_sec_offset);
19016 }
19017
19018 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19019    zero otherwise.  When this function returns true, you can apply
19020    dwarf2_get_attr_constant_value to it.
19021
19022    However, note that for some attributes you must check
19023    attr_form_is_section_offset before using this test.  DW_FORM_data4
19024    and DW_FORM_data8 are members of both the constant class, and of
19025    the classes that contain offsets into other debug sections
19026    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
19027    that, if an attribute's can be either a constant or one of the
19028    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19029    taken as section offsets, not constants.  */
19030
19031 static int
19032 attr_form_is_constant (struct attribute *attr)
19033 {
19034   switch (attr->form)
19035     {
19036     case DW_FORM_sdata:
19037     case DW_FORM_udata:
19038     case DW_FORM_data1:
19039     case DW_FORM_data2:
19040     case DW_FORM_data4:
19041     case DW_FORM_data8:
19042       return 1;
19043     default:
19044       return 0;
19045     }
19046 }
19047
19048 /* Return the .debug_loc section to use for CU.
19049    For DWO files use .debug_loc.dwo.  */
19050
19051 static struct dwarf2_section_info *
19052 cu_debug_loc_section (struct dwarf2_cu *cu)
19053 {
19054   if (cu->dwo_unit)
19055     return &cu->dwo_unit->dwo_file->sections.loc;
19056   return &dwarf2_per_objfile->loc;
19057 }
19058
19059 /* A helper function that fills in a dwarf2_loclist_baton.  */
19060
19061 static void
19062 fill_in_loclist_baton (struct dwarf2_cu *cu,
19063                        struct dwarf2_loclist_baton *baton,
19064                        struct attribute *attr)
19065 {
19066   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19067
19068   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19069
19070   baton->per_cu = cu->per_cu;
19071   gdb_assert (baton->per_cu);
19072   /* We don't know how long the location list is, but make sure we
19073      don't run off the edge of the section.  */
19074   baton->size = section->size - DW_UNSND (attr);
19075   baton->data = section->buffer + DW_UNSND (attr);
19076   baton->base_address = cu->base_address;
19077   baton->from_dwo = cu->dwo_unit != NULL;
19078 }
19079
19080 static void
19081 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19082                              struct dwarf2_cu *cu, int is_block)
19083 {
19084   struct objfile *objfile = dwarf2_per_objfile->objfile;
19085   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19086
19087   if (attr_form_is_section_offset (attr)
19088       /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19089          the section.  If so, fall through to the complaint in the
19090          other branch.  */
19091       && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19092     {
19093       struct dwarf2_loclist_baton *baton;
19094
19095       baton = obstack_alloc (&objfile->objfile_obstack,
19096                              sizeof (struct dwarf2_loclist_baton));
19097
19098       fill_in_loclist_baton (cu, baton, attr);
19099
19100       if (cu->base_known == 0)
19101         complaint (&symfile_complaints,
19102                    _("Location list used without "
19103                      "specifying the CU base address."));
19104
19105       SYMBOL_ACLASS_INDEX (sym) = (is_block
19106                                    ? dwarf2_loclist_block_index
19107                                    : dwarf2_loclist_index);
19108       SYMBOL_LOCATION_BATON (sym) = baton;
19109     }
19110   else
19111     {
19112       struct dwarf2_locexpr_baton *baton;
19113
19114       baton = obstack_alloc (&objfile->objfile_obstack,
19115                              sizeof (struct dwarf2_locexpr_baton));
19116       baton->per_cu = cu->per_cu;
19117       gdb_assert (baton->per_cu);
19118
19119       if (attr_form_is_block (attr))
19120         {
19121           /* Note that we're just copying the block's data pointer
19122              here, not the actual data.  We're still pointing into the
19123              info_buffer for SYM's objfile; right now we never release
19124              that buffer, but when we do clean up properly this may
19125              need to change.  */
19126           baton->size = DW_BLOCK (attr)->size;
19127           baton->data = DW_BLOCK (attr)->data;
19128         }
19129       else
19130         {
19131           dwarf2_invalid_attrib_class_complaint ("location description",
19132                                                  SYMBOL_NATURAL_NAME (sym));
19133           baton->size = 0;
19134         }
19135
19136       SYMBOL_ACLASS_INDEX (sym) = (is_block
19137                                    ? dwarf2_locexpr_block_index
19138                                    : dwarf2_locexpr_index);
19139       SYMBOL_LOCATION_BATON (sym) = baton;
19140     }
19141 }
19142
19143 /* Return the OBJFILE associated with the compilation unit CU.  If CU
19144    came from a separate debuginfo file, then the master objfile is
19145    returned.  */
19146
19147 struct objfile *
19148 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19149 {
19150   struct objfile *objfile = per_cu->objfile;
19151
19152   /* Return the master objfile, so that we can report and look up the
19153      correct file containing this variable.  */
19154   if (objfile->separate_debug_objfile_backlink)
19155     objfile = objfile->separate_debug_objfile_backlink;
19156
19157   return objfile;
19158 }
19159
19160 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19161    (CU_HEADERP is unused in such case) or prepare a temporary copy at
19162    CU_HEADERP first.  */
19163
19164 static const struct comp_unit_head *
19165 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19166                        struct dwarf2_per_cu_data *per_cu)
19167 {
19168   gdb_byte *info_ptr;
19169
19170   if (per_cu->cu)
19171     return &per_cu->cu->header;
19172
19173   info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19174
19175   memset (cu_headerp, 0, sizeof (*cu_headerp));
19176   read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19177
19178   return cu_headerp;
19179 }
19180
19181 /* Return the address size given in the compilation unit header for CU.  */
19182
19183 int
19184 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19185 {
19186   struct comp_unit_head cu_header_local;
19187   const struct comp_unit_head *cu_headerp;
19188
19189   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19190
19191   return cu_headerp->addr_size;
19192 }
19193
19194 /* Return the offset size given in the compilation unit header for CU.  */
19195
19196 int
19197 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19198 {
19199   struct comp_unit_head cu_header_local;
19200   const struct comp_unit_head *cu_headerp;
19201
19202   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19203
19204   return cu_headerp->offset_size;
19205 }
19206
19207 /* See its dwarf2loc.h declaration.  */
19208
19209 int
19210 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19211 {
19212   struct comp_unit_head cu_header_local;
19213   const struct comp_unit_head *cu_headerp;
19214
19215   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19216
19217   if (cu_headerp->version == 2)
19218     return cu_headerp->addr_size;
19219   else
19220     return cu_headerp->offset_size;
19221 }
19222
19223 /* Return the text offset of the CU.  The returned offset comes from
19224    this CU's objfile.  If this objfile came from a separate debuginfo
19225    file, then the offset may be different from the corresponding
19226    offset in the parent objfile.  */
19227
19228 CORE_ADDR
19229 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
19230 {
19231   struct objfile *objfile = per_cu->objfile;
19232
19233   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19234 }
19235
19236 /* Locate the .debug_info compilation unit from CU's objfile which contains
19237    the DIE at OFFSET.  Raises an error on failure.  */
19238
19239 static struct dwarf2_per_cu_data *
19240 dwarf2_find_containing_comp_unit (sect_offset offset,
19241                                   unsigned int offset_in_dwz,
19242                                   struct objfile *objfile)
19243 {
19244   struct dwarf2_per_cu_data *this_cu;
19245   int low, high;
19246   const sect_offset *cu_off;
19247
19248   low = 0;
19249   high = dwarf2_per_objfile->n_comp_units - 1;
19250   while (high > low)
19251     {
19252       struct dwarf2_per_cu_data *mid_cu;
19253       int mid = low + (high - low) / 2;
19254
19255       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
19256       cu_off = &mid_cu->offset;
19257       if (mid_cu->is_dwz > offset_in_dwz
19258           || (mid_cu->is_dwz == offset_in_dwz
19259               && cu_off->sect_off >= offset.sect_off))
19260         high = mid;
19261       else
19262         low = mid + 1;
19263     }
19264   gdb_assert (low == high);
19265   this_cu = dwarf2_per_objfile->all_comp_units[low];
19266   cu_off = &this_cu->offset;
19267   if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
19268     {
19269       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
19270         error (_("Dwarf Error: could not find partial DIE containing "
19271                "offset 0x%lx [in module %s]"),
19272                (long) offset.sect_off, bfd_get_filename (objfile->obfd));
19273
19274       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
19275                   <= offset.sect_off);
19276       return dwarf2_per_objfile->all_comp_units[low-1];
19277     }
19278   else
19279     {
19280       this_cu = dwarf2_per_objfile->all_comp_units[low];
19281       if (low == dwarf2_per_objfile->n_comp_units - 1
19282           && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
19283         error (_("invalid dwarf2 offset %u"), offset.sect_off);
19284       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
19285       return this_cu;
19286     }
19287 }
19288
19289 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
19290
19291 static void
19292 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
19293 {
19294   memset (cu, 0, sizeof (*cu));
19295   per_cu->cu = cu;
19296   cu->per_cu = per_cu;
19297   cu->objfile = per_cu->objfile;
19298   obstack_init (&cu->comp_unit_obstack);
19299 }
19300
19301 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
19302
19303 static void
19304 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
19305                        enum language pretend_language)
19306 {
19307   struct attribute *attr;
19308
19309   /* Set the language we're debugging.  */
19310   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
19311   if (attr)
19312     set_cu_language (DW_UNSND (attr), cu);
19313   else
19314     {
19315       cu->language = pretend_language;
19316       cu->language_defn = language_def (cu->language);
19317     }
19318
19319   attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
19320   if (attr)
19321     cu->producer = DW_STRING (attr);
19322 }
19323
19324 /* Release one cached compilation unit, CU.  We unlink it from the tree
19325    of compilation units, but we don't remove it from the read_in_chain;
19326    the caller is responsible for that.
19327    NOTE: DATA is a void * because this function is also used as a
19328    cleanup routine.  */
19329
19330 static void
19331 free_heap_comp_unit (void *data)
19332 {
19333   struct dwarf2_cu *cu = data;
19334
19335   gdb_assert (cu->per_cu != NULL);
19336   cu->per_cu->cu = NULL;
19337   cu->per_cu = NULL;
19338
19339   obstack_free (&cu->comp_unit_obstack, NULL);
19340
19341   xfree (cu);
19342 }
19343
19344 /* This cleanup function is passed the address of a dwarf2_cu on the stack
19345    when we're finished with it.  We can't free the pointer itself, but be
19346    sure to unlink it from the cache.  Also release any associated storage.  */
19347
19348 static void
19349 free_stack_comp_unit (void *data)
19350 {
19351   struct dwarf2_cu *cu = data;
19352
19353   gdb_assert (cu->per_cu != NULL);
19354   cu->per_cu->cu = NULL;
19355   cu->per_cu = NULL;
19356
19357   obstack_free (&cu->comp_unit_obstack, NULL);
19358   cu->partial_dies = NULL;
19359 }
19360
19361 /* Free all cached compilation units.  */
19362
19363 static void
19364 free_cached_comp_units (void *data)
19365 {
19366   struct dwarf2_per_cu_data *per_cu, **last_chain;
19367
19368   per_cu = dwarf2_per_objfile->read_in_chain;
19369   last_chain = &dwarf2_per_objfile->read_in_chain;
19370   while (per_cu != NULL)
19371     {
19372       struct dwarf2_per_cu_data *next_cu;
19373
19374       next_cu = per_cu->cu->read_in_chain;
19375
19376       free_heap_comp_unit (per_cu->cu);
19377       *last_chain = next_cu;
19378
19379       per_cu = next_cu;
19380     }
19381 }
19382
19383 /* Increase the age counter on each cached compilation unit, and free
19384    any that are too old.  */
19385
19386 static void
19387 age_cached_comp_units (void)
19388 {
19389   struct dwarf2_per_cu_data *per_cu, **last_chain;
19390
19391   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
19392   per_cu = dwarf2_per_objfile->read_in_chain;
19393   while (per_cu != NULL)
19394     {
19395       per_cu->cu->last_used ++;
19396       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
19397         dwarf2_mark (per_cu->cu);
19398       per_cu = per_cu->cu->read_in_chain;
19399     }
19400
19401   per_cu = dwarf2_per_objfile->read_in_chain;
19402   last_chain = &dwarf2_per_objfile->read_in_chain;
19403   while (per_cu != NULL)
19404     {
19405       struct dwarf2_per_cu_data *next_cu;
19406
19407       next_cu = per_cu->cu->read_in_chain;
19408
19409       if (!per_cu->cu->mark)
19410         {
19411           free_heap_comp_unit (per_cu->cu);
19412           *last_chain = next_cu;
19413         }
19414       else
19415         last_chain = &per_cu->cu->read_in_chain;
19416
19417       per_cu = next_cu;
19418     }
19419 }
19420
19421 /* Remove a single compilation unit from the cache.  */
19422
19423 static void
19424 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
19425 {
19426   struct dwarf2_per_cu_data *per_cu, **last_chain;
19427
19428   per_cu = dwarf2_per_objfile->read_in_chain;
19429   last_chain = &dwarf2_per_objfile->read_in_chain;
19430   while (per_cu != NULL)
19431     {
19432       struct dwarf2_per_cu_data *next_cu;
19433
19434       next_cu = per_cu->cu->read_in_chain;
19435
19436       if (per_cu == target_per_cu)
19437         {
19438           free_heap_comp_unit (per_cu->cu);
19439           per_cu->cu = NULL;
19440           *last_chain = next_cu;
19441           break;
19442         }
19443       else
19444         last_chain = &per_cu->cu->read_in_chain;
19445
19446       per_cu = next_cu;
19447     }
19448 }
19449
19450 /* Release all extra memory associated with OBJFILE.  */
19451
19452 void
19453 dwarf2_free_objfile (struct objfile *objfile)
19454 {
19455   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19456
19457   if (dwarf2_per_objfile == NULL)
19458     return;
19459
19460   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
19461   free_cached_comp_units (NULL);
19462
19463   if (dwarf2_per_objfile->quick_file_names_table)
19464     htab_delete (dwarf2_per_objfile->quick_file_names_table);
19465
19466   /* Everything else should be on the objfile obstack.  */
19467 }
19468
19469 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
19470    We store these in a hash table separate from the DIEs, and preserve them
19471    when the DIEs are flushed out of cache.
19472
19473    The CU "per_cu" pointer is needed because offset alone is not enough to
19474    uniquely identify the type.  A file may have multiple .debug_types sections,
19475    or the type may come from a DWO file.  We have to use something in
19476    dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
19477    routine, get_die_type_at_offset, from outside this file, and thus won't
19478    necessarily have PER_CU->cu.  Fortunately, PER_CU is stable for the life
19479    of the objfile.  */
19480
19481 struct dwarf2_per_cu_offset_and_type
19482 {
19483   const struct dwarf2_per_cu_data *per_cu;
19484   sect_offset offset;
19485   struct type *type;
19486 };
19487
19488 /* Hash function for a dwarf2_per_cu_offset_and_type.  */
19489
19490 static hashval_t
19491 per_cu_offset_and_type_hash (const void *item)
19492 {
19493   const struct dwarf2_per_cu_offset_and_type *ofs = item;
19494
19495   return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
19496 }
19497
19498 /* Equality function for a dwarf2_per_cu_offset_and_type.  */
19499
19500 static int
19501 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
19502 {
19503   const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
19504   const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
19505
19506   return (ofs_lhs->per_cu == ofs_rhs->per_cu
19507           && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
19508 }
19509
19510 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
19511    table if necessary.  For convenience, return TYPE.
19512
19513    The DIEs reading must have careful ordering to:
19514     * Not cause infite loops trying to read in DIEs as a prerequisite for
19515       reading current DIE.
19516     * Not trying to dereference contents of still incompletely read in types
19517       while reading in other DIEs.
19518     * Enable referencing still incompletely read in types just by a pointer to
19519       the type without accessing its fields.
19520
19521    Therefore caller should follow these rules:
19522      * Try to fetch any prerequisite types we may need to build this DIE type
19523        before building the type and calling set_die_type.
19524      * After building type call set_die_type for current DIE as soon as
19525        possible before fetching more types to complete the current type.
19526      * Make the type as complete as possible before fetching more types.  */
19527
19528 static struct type *
19529 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
19530 {
19531   struct dwarf2_per_cu_offset_and_type **slot, ofs;
19532   struct objfile *objfile = cu->objfile;
19533
19534   /* For Ada types, make sure that the gnat-specific data is always
19535      initialized (if not already set).  There are a few types where
19536      we should not be doing so, because the type-specific area is
19537      already used to hold some other piece of info (eg: TYPE_CODE_FLT
19538      where the type-specific area is used to store the floatformat).
19539      But this is not a problem, because the gnat-specific information
19540      is actually not needed for these types.  */
19541   if (need_gnat_info (cu)
19542       && TYPE_CODE (type) != TYPE_CODE_FUNC
19543       && TYPE_CODE (type) != TYPE_CODE_FLT
19544       && !HAVE_GNAT_AUX_INFO (type))
19545     INIT_GNAT_SPECIFIC (type);
19546
19547   if (dwarf2_per_objfile->die_type_hash == NULL)
19548     {
19549       dwarf2_per_objfile->die_type_hash =
19550         htab_create_alloc_ex (127,
19551                               per_cu_offset_and_type_hash,
19552                               per_cu_offset_and_type_eq,
19553                               NULL,
19554                               &objfile->objfile_obstack,
19555                               hashtab_obstack_allocate,
19556                               dummy_obstack_deallocate);
19557     }
19558
19559   ofs.per_cu = cu->per_cu;
19560   ofs.offset = die->offset;
19561   ofs.type = type;
19562   slot = (struct dwarf2_per_cu_offset_and_type **)
19563     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
19564   if (*slot)
19565     complaint (&symfile_complaints,
19566                _("A problem internal to GDB: DIE 0x%x has type already set"),
19567                die->offset.sect_off);
19568   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
19569   **slot = ofs;
19570   return type;
19571 }
19572
19573 /* Look up the type for the die at OFFSET in the appropriate type_hash
19574    table, or return NULL if the die does not have a saved type.  */
19575
19576 static struct type *
19577 get_die_type_at_offset (sect_offset offset,
19578                         struct dwarf2_per_cu_data *per_cu)
19579 {
19580   struct dwarf2_per_cu_offset_and_type *slot, ofs;
19581
19582   if (dwarf2_per_objfile->die_type_hash == NULL)
19583     return NULL;
19584
19585   ofs.per_cu = per_cu;
19586   ofs.offset = offset;
19587   slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
19588   if (slot)
19589     return slot->type;
19590   else
19591     return NULL;
19592 }
19593
19594 /* Look up the type for DIE in the appropriate type_hash table,
19595    or return NULL if DIE does not have a saved type.  */
19596
19597 static struct type *
19598 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
19599 {
19600   return get_die_type_at_offset (die->offset, cu->per_cu);
19601 }
19602
19603 /* Add a dependence relationship from CU to REF_PER_CU.  */
19604
19605 static void
19606 dwarf2_add_dependence (struct dwarf2_cu *cu,
19607                        struct dwarf2_per_cu_data *ref_per_cu)
19608 {
19609   void **slot;
19610
19611   if (cu->dependencies == NULL)
19612     cu->dependencies
19613       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
19614                               NULL, &cu->comp_unit_obstack,
19615                               hashtab_obstack_allocate,
19616                               dummy_obstack_deallocate);
19617
19618   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
19619   if (*slot == NULL)
19620     *slot = ref_per_cu;
19621 }
19622
19623 /* Subroutine of dwarf2_mark to pass to htab_traverse.
19624    Set the mark field in every compilation unit in the
19625    cache that we must keep because we are keeping CU.  */
19626
19627 static int
19628 dwarf2_mark_helper (void **slot, void *data)
19629 {
19630   struct dwarf2_per_cu_data *per_cu;
19631
19632   per_cu = (struct dwarf2_per_cu_data *) *slot;
19633
19634   /* cu->dependencies references may not yet have been ever read if QUIT aborts
19635      reading of the chain.  As such dependencies remain valid it is not much
19636      useful to track and undo them during QUIT cleanups.  */
19637   if (per_cu->cu == NULL)
19638     return 1;
19639
19640   if (per_cu->cu->mark)
19641     return 1;
19642   per_cu->cu->mark = 1;
19643
19644   if (per_cu->cu->dependencies != NULL)
19645     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
19646
19647   return 1;
19648 }
19649
19650 /* Set the mark field in CU and in every other compilation unit in the
19651    cache that we must keep because we are keeping CU.  */
19652
19653 static void
19654 dwarf2_mark (struct dwarf2_cu *cu)
19655 {
19656   if (cu->mark)
19657     return;
19658   cu->mark = 1;
19659   if (cu->dependencies != NULL)
19660     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
19661 }
19662
19663 static void
19664 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
19665 {
19666   while (per_cu)
19667     {
19668       per_cu->cu->mark = 0;
19669       per_cu = per_cu->cu->read_in_chain;
19670     }
19671 }
19672
19673 /* Trivial hash function for partial_die_info: the hash value of a DIE
19674    is its offset in .debug_info for this objfile.  */
19675
19676 static hashval_t
19677 partial_die_hash (const void *item)
19678 {
19679   const struct partial_die_info *part_die = item;
19680
19681   return part_die->offset.sect_off;
19682 }
19683
19684 /* Trivial comparison function for partial_die_info structures: two DIEs
19685    are equal if they have the same offset.  */
19686
19687 static int
19688 partial_die_eq (const void *item_lhs, const void *item_rhs)
19689 {
19690   const struct partial_die_info *part_die_lhs = item_lhs;
19691   const struct partial_die_info *part_die_rhs = item_rhs;
19692
19693   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
19694 }
19695
19696 static struct cmd_list_element *set_dwarf2_cmdlist;
19697 static struct cmd_list_element *show_dwarf2_cmdlist;
19698
19699 static void
19700 set_dwarf2_cmd (char *args, int from_tty)
19701 {
19702   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
19703 }
19704
19705 static void
19706 show_dwarf2_cmd (char *args, int from_tty)
19707 {
19708   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
19709 }
19710
19711 /* Free data associated with OBJFILE, if necessary.  */
19712
19713 static void
19714 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
19715 {
19716   struct dwarf2_per_objfile *data = d;
19717   int ix;
19718
19719   for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
19720     VEC_free (dwarf2_per_cu_ptr,
19721               dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
19722
19723   for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
19724     VEC_free (dwarf2_per_cu_ptr,
19725               dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
19726
19727   VEC_free (dwarf2_section_info_def, data->types);
19728
19729   if (data->dwo_files)
19730     free_dwo_files (data->dwo_files, objfile);
19731
19732   if (data->dwz_file && data->dwz_file->dwz_bfd)
19733     gdb_bfd_unref (data->dwz_file->dwz_bfd);
19734 }
19735
19736 \f
19737 /* The "save gdb-index" command.  */
19738
19739 /* The contents of the hash table we create when building the string
19740    table.  */
19741 struct strtab_entry
19742 {
19743   offset_type offset;
19744   const char *str;
19745 };
19746
19747 /* Hash function for a strtab_entry.
19748
19749    Function is used only during write_hash_table so no index format backward
19750    compatibility is needed.  */
19751
19752 static hashval_t
19753 hash_strtab_entry (const void *e)
19754 {
19755   const struct strtab_entry *entry = e;
19756   return mapped_index_string_hash (INT_MAX, entry->str);
19757 }
19758
19759 /* Equality function for a strtab_entry.  */
19760
19761 static int
19762 eq_strtab_entry (const void *a, const void *b)
19763 {
19764   const struct strtab_entry *ea = a;
19765   const struct strtab_entry *eb = b;
19766   return !strcmp (ea->str, eb->str);
19767 }
19768
19769 /* Create a strtab_entry hash table.  */
19770
19771 static htab_t
19772 create_strtab (void)
19773 {
19774   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
19775                             xfree, xcalloc, xfree);
19776 }
19777
19778 /* Add a string to the constant pool.  Return the string's offset in
19779    host order.  */
19780
19781 static offset_type
19782 add_string (htab_t table, struct obstack *cpool, const char *str)
19783 {
19784   void **slot;
19785   struct strtab_entry entry;
19786   struct strtab_entry *result;
19787
19788   entry.str = str;
19789   slot = htab_find_slot (table, &entry, INSERT);
19790   if (*slot)
19791     result = *slot;
19792   else
19793     {
19794       result = XNEW (struct strtab_entry);
19795       result->offset = obstack_object_size (cpool);
19796       result->str = str;
19797       obstack_grow_str0 (cpool, str);
19798       *slot = result;
19799     }
19800   return result->offset;
19801 }
19802
19803 /* An entry in the symbol table.  */
19804 struct symtab_index_entry
19805 {
19806   /* The name of the symbol.  */
19807   const char *name;
19808   /* The offset of the name in the constant pool.  */
19809   offset_type index_offset;
19810   /* A sorted vector of the indices of all the CUs that hold an object
19811      of this name.  */
19812   VEC (offset_type) *cu_indices;
19813 };
19814
19815 /* The symbol table.  This is a power-of-2-sized hash table.  */
19816 struct mapped_symtab
19817 {
19818   offset_type n_elements;
19819   offset_type size;
19820   struct symtab_index_entry **data;
19821 };
19822
19823 /* Hash function for a symtab_index_entry.  */
19824
19825 static hashval_t
19826 hash_symtab_entry (const void *e)
19827 {
19828   const struct symtab_index_entry *entry = e;
19829   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
19830                          sizeof (offset_type) * VEC_length (offset_type,
19831                                                             entry->cu_indices),
19832                          0);
19833 }
19834
19835 /* Equality function for a symtab_index_entry.  */
19836
19837 static int
19838 eq_symtab_entry (const void *a, const void *b)
19839 {
19840   const struct symtab_index_entry *ea = a;
19841   const struct symtab_index_entry *eb = b;
19842   int len = VEC_length (offset_type, ea->cu_indices);
19843   if (len != VEC_length (offset_type, eb->cu_indices))
19844     return 0;
19845   return !memcmp (VEC_address (offset_type, ea->cu_indices),
19846                   VEC_address (offset_type, eb->cu_indices),
19847                   sizeof (offset_type) * len);
19848 }
19849
19850 /* Destroy a symtab_index_entry.  */
19851
19852 static void
19853 delete_symtab_entry (void *p)
19854 {
19855   struct symtab_index_entry *entry = p;
19856   VEC_free (offset_type, entry->cu_indices);
19857   xfree (entry);
19858 }
19859
19860 /* Create a hash table holding symtab_index_entry objects.  */
19861
19862 static htab_t
19863 create_symbol_hash_table (void)
19864 {
19865   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
19866                             delete_symtab_entry, xcalloc, xfree);
19867 }
19868
19869 /* Create a new mapped symtab object.  */
19870
19871 static struct mapped_symtab *
19872 create_mapped_symtab (void)
19873 {
19874   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
19875   symtab->n_elements = 0;
19876   symtab->size = 1024;
19877   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19878   return symtab;
19879 }
19880
19881 /* Destroy a mapped_symtab.  */
19882
19883 static void
19884 cleanup_mapped_symtab (void *p)
19885 {
19886   struct mapped_symtab *symtab = p;
19887   /* The contents of the array are freed when the other hash table is
19888      destroyed.  */
19889   xfree (symtab->data);
19890   xfree (symtab);
19891 }
19892
19893 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
19894    the slot.
19895    
19896    Function is used only during write_hash_table so no index format backward
19897    compatibility is needed.  */
19898
19899 static struct symtab_index_entry **
19900 find_slot (struct mapped_symtab *symtab, const char *name)
19901 {
19902   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
19903
19904   index = hash & (symtab->size - 1);
19905   step = ((hash * 17) & (symtab->size - 1)) | 1;
19906
19907   for (;;)
19908     {
19909       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
19910         return &symtab->data[index];
19911       index = (index + step) & (symtab->size - 1);
19912     }
19913 }
19914
19915 /* Expand SYMTAB's hash table.  */
19916
19917 static void
19918 hash_expand (struct mapped_symtab *symtab)
19919 {
19920   offset_type old_size = symtab->size;
19921   offset_type i;
19922   struct symtab_index_entry **old_entries = symtab->data;
19923
19924   symtab->size *= 2;
19925   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
19926
19927   for (i = 0; i < old_size; ++i)
19928     {
19929       if (old_entries[i])
19930         {
19931           struct symtab_index_entry **slot = find_slot (symtab,
19932                                                         old_entries[i]->name);
19933           *slot = old_entries[i];
19934         }
19935     }
19936
19937   xfree (old_entries);
19938 }
19939
19940 /* Add an entry to SYMTAB.  NAME is the name of the symbol.
19941    CU_INDEX is the index of the CU in which the symbol appears.
19942    IS_STATIC is one if the symbol is static, otherwise zero (global).  */
19943
19944 static void
19945 add_index_entry (struct mapped_symtab *symtab, const char *name,
19946                  int is_static, gdb_index_symbol_kind kind,
19947                  offset_type cu_index)
19948 {
19949   struct symtab_index_entry **slot;
19950   offset_type cu_index_and_attrs;
19951
19952   ++symtab->n_elements;
19953   if (4 * symtab->n_elements / 3 >= symtab->size)
19954     hash_expand (symtab);
19955
19956   slot = find_slot (symtab, name);
19957   if (!*slot)
19958     {
19959       *slot = XNEW (struct symtab_index_entry);
19960       (*slot)->name = name;
19961       /* index_offset is set later.  */
19962       (*slot)->cu_indices = NULL;
19963     }
19964
19965   cu_index_and_attrs = 0;
19966   DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
19967   DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
19968   DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
19969
19970   /* We don't want to record an index value twice as we want to avoid the
19971      duplication.
19972      We process all global symbols and then all static symbols
19973      (which would allow us to avoid the duplication by only having to check
19974      the last entry pushed), but a symbol could have multiple kinds in one CU.
19975      To keep things simple we don't worry about the duplication here and
19976      sort and uniqufy the list after we've processed all symbols.  */
19977   VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
19978 }
19979
19980 /* qsort helper routine for uniquify_cu_indices.  */
19981
19982 static int
19983 offset_type_compare (const void *ap, const void *bp)
19984 {
19985   offset_type a = *(offset_type *) ap;
19986   offset_type b = *(offset_type *) bp;
19987
19988   return (a > b) - (b > a);
19989 }
19990
19991 /* Sort and remove duplicates of all symbols' cu_indices lists.  */
19992
19993 static void
19994 uniquify_cu_indices (struct mapped_symtab *symtab)
19995 {
19996   int i;
19997
19998   for (i = 0; i < symtab->size; ++i)
19999     {
20000       struct symtab_index_entry *entry = symtab->data[i];
20001
20002       if (entry
20003           && entry->cu_indices != NULL)
20004         {
20005           unsigned int next_to_insert, next_to_check;
20006           offset_type last_value;
20007
20008           qsort (VEC_address (offset_type, entry->cu_indices),
20009                  VEC_length (offset_type, entry->cu_indices),
20010                  sizeof (offset_type), offset_type_compare);
20011
20012           last_value = VEC_index (offset_type, entry->cu_indices, 0);
20013           next_to_insert = 1;
20014           for (next_to_check = 1;
20015                next_to_check < VEC_length (offset_type, entry->cu_indices);
20016                ++next_to_check)
20017             {
20018               if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20019                   != last_value)
20020                 {
20021                   last_value = VEC_index (offset_type, entry->cu_indices,
20022                                           next_to_check);
20023                   VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20024                                last_value);
20025                   ++next_to_insert;
20026                 }
20027             }
20028           VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20029         }
20030     }
20031 }
20032
20033 /* Add a vector of indices to the constant pool.  */
20034
20035 static offset_type
20036 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20037                       struct symtab_index_entry *entry)
20038 {
20039   void **slot;
20040
20041   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20042   if (!*slot)
20043     {
20044       offset_type len = VEC_length (offset_type, entry->cu_indices);
20045       offset_type val = MAYBE_SWAP (len);
20046       offset_type iter;
20047       int i;
20048
20049       *slot = entry;
20050       entry->index_offset = obstack_object_size (cpool);
20051
20052       obstack_grow (cpool, &val, sizeof (val));
20053       for (i = 0;
20054            VEC_iterate (offset_type, entry->cu_indices, i, iter);
20055            ++i)
20056         {
20057           val = MAYBE_SWAP (iter);
20058           obstack_grow (cpool, &val, sizeof (val));
20059         }
20060     }
20061   else
20062     {
20063       struct symtab_index_entry *old_entry = *slot;
20064       entry->index_offset = old_entry->index_offset;
20065       entry = old_entry;
20066     }
20067   return entry->index_offset;
20068 }
20069
20070 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20071    constant pool entries going into the obstack CPOOL.  */
20072
20073 static void
20074 write_hash_table (struct mapped_symtab *symtab,
20075                   struct obstack *output, struct obstack *cpool)
20076 {
20077   offset_type i;
20078   htab_t symbol_hash_table;
20079   htab_t str_table;
20080
20081   symbol_hash_table = create_symbol_hash_table ();
20082   str_table = create_strtab ();
20083
20084   /* We add all the index vectors to the constant pool first, to
20085      ensure alignment is ok.  */
20086   for (i = 0; i < symtab->size; ++i)
20087     {
20088       if (symtab->data[i])
20089         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20090     }
20091
20092   /* Now write out the hash table.  */
20093   for (i = 0; i < symtab->size; ++i)
20094     {
20095       offset_type str_off, vec_off;
20096
20097       if (symtab->data[i])
20098         {
20099           str_off = add_string (str_table, cpool, symtab->data[i]->name);
20100           vec_off = symtab->data[i]->index_offset;
20101         }
20102       else
20103         {
20104           /* While 0 is a valid constant pool index, it is not valid
20105              to have 0 for both offsets.  */
20106           str_off = 0;
20107           vec_off = 0;
20108         }
20109
20110       str_off = MAYBE_SWAP (str_off);
20111       vec_off = MAYBE_SWAP (vec_off);
20112
20113       obstack_grow (output, &str_off, sizeof (str_off));
20114       obstack_grow (output, &vec_off, sizeof (vec_off));
20115     }
20116
20117   htab_delete (str_table);
20118   htab_delete (symbol_hash_table);
20119 }
20120
20121 /* Struct to map psymtab to CU index in the index file.  */
20122 struct psymtab_cu_index_map
20123 {
20124   struct partial_symtab *psymtab;
20125   unsigned int cu_index;
20126 };
20127
20128 static hashval_t
20129 hash_psymtab_cu_index (const void *item)
20130 {
20131   const struct psymtab_cu_index_map *map = item;
20132
20133   return htab_hash_pointer (map->psymtab);
20134 }
20135
20136 static int
20137 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20138 {
20139   const struct psymtab_cu_index_map *lhs = item_lhs;
20140   const struct psymtab_cu_index_map *rhs = item_rhs;
20141
20142   return lhs->psymtab == rhs->psymtab;
20143 }
20144
20145 /* Helper struct for building the address table.  */
20146 struct addrmap_index_data
20147 {
20148   struct objfile *objfile;
20149   struct obstack *addr_obstack;
20150   htab_t cu_index_htab;
20151
20152   /* Non-zero if the previous_* fields are valid.
20153      We can't write an entry until we see the next entry (since it is only then
20154      that we know the end of the entry).  */
20155   int previous_valid;
20156   /* Index of the CU in the table of all CUs in the index file.  */
20157   unsigned int previous_cu_index;
20158   /* Start address of the CU.  */
20159   CORE_ADDR previous_cu_start;
20160 };
20161
20162 /* Write an address entry to OBSTACK.  */
20163
20164 static void
20165 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20166                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20167 {
20168   offset_type cu_index_to_write;
20169   char addr[8];
20170   CORE_ADDR baseaddr;
20171
20172   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20173
20174   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20175   obstack_grow (obstack, addr, 8);
20176   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20177   obstack_grow (obstack, addr, 8);
20178   cu_index_to_write = MAYBE_SWAP (cu_index);
20179   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20180 }
20181
20182 /* Worker function for traversing an addrmap to build the address table.  */
20183
20184 static int
20185 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20186 {
20187   struct addrmap_index_data *data = datap;
20188   struct partial_symtab *pst = obj;
20189
20190   if (data->previous_valid)
20191     add_address_entry (data->objfile, data->addr_obstack,
20192                        data->previous_cu_start, start_addr,
20193                        data->previous_cu_index);
20194
20195   data->previous_cu_start = start_addr;
20196   if (pst != NULL)
20197     {
20198       struct psymtab_cu_index_map find_map, *map;
20199       find_map.psymtab = pst;
20200       map = htab_find (data->cu_index_htab, &find_map);
20201       gdb_assert (map != NULL);
20202       data->previous_cu_index = map->cu_index;
20203       data->previous_valid = 1;
20204     }
20205   else
20206       data->previous_valid = 0;
20207
20208   return 0;
20209 }
20210
20211 /* Write OBJFILE's address map to OBSTACK.
20212    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20213    in the index file.  */
20214
20215 static void
20216 write_address_map (struct objfile *objfile, struct obstack *obstack,
20217                    htab_t cu_index_htab)
20218 {
20219   struct addrmap_index_data addrmap_index_data;
20220
20221   /* When writing the address table, we have to cope with the fact that
20222      the addrmap iterator only provides the start of a region; we have to
20223      wait until the next invocation to get the start of the next region.  */
20224
20225   addrmap_index_data.objfile = objfile;
20226   addrmap_index_data.addr_obstack = obstack;
20227   addrmap_index_data.cu_index_htab = cu_index_htab;
20228   addrmap_index_data.previous_valid = 0;
20229
20230   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
20231                    &addrmap_index_data);
20232
20233   /* It's highly unlikely the last entry (end address = 0xff...ff)
20234      is valid, but we should still handle it.
20235      The end address is recorded as the start of the next region, but that
20236      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
20237      anyway.  */
20238   if (addrmap_index_data.previous_valid)
20239     add_address_entry (objfile, obstack,
20240                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
20241                        addrmap_index_data.previous_cu_index);
20242 }
20243
20244 /* Return the symbol kind of PSYM.  */
20245
20246 static gdb_index_symbol_kind
20247 symbol_kind (struct partial_symbol *psym)
20248 {
20249   domain_enum domain = PSYMBOL_DOMAIN (psym);
20250   enum address_class aclass = PSYMBOL_CLASS (psym);
20251
20252   switch (domain)
20253     {
20254     case VAR_DOMAIN:
20255       switch (aclass)
20256         {
20257         case LOC_BLOCK:
20258           return GDB_INDEX_SYMBOL_KIND_FUNCTION;
20259         case LOC_TYPEDEF:
20260           return GDB_INDEX_SYMBOL_KIND_TYPE;
20261         case LOC_COMPUTED:
20262         case LOC_CONST_BYTES:
20263         case LOC_OPTIMIZED_OUT:
20264         case LOC_STATIC:
20265           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20266         case LOC_CONST:
20267           /* Note: It's currently impossible to recognize psyms as enum values
20268              short of reading the type info.  For now punt.  */
20269           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
20270         default:
20271           /* There are other LOC_FOO values that one might want to classify
20272              as variables, but dwarf2read.c doesn't currently use them.  */
20273           return GDB_INDEX_SYMBOL_KIND_OTHER;
20274         }
20275     case STRUCT_DOMAIN:
20276       return GDB_INDEX_SYMBOL_KIND_TYPE;
20277     default:
20278       return GDB_INDEX_SYMBOL_KIND_OTHER;
20279     }
20280 }
20281
20282 /* Add a list of partial symbols to SYMTAB.  */
20283
20284 static void
20285 write_psymbols (struct mapped_symtab *symtab,
20286                 htab_t psyms_seen,
20287                 struct partial_symbol **psymp,
20288                 int count,
20289                 offset_type cu_index,
20290                 int is_static)
20291 {
20292   for (; count-- > 0; ++psymp)
20293     {
20294       struct partial_symbol *psym = *psymp;
20295       void **slot;
20296
20297       if (SYMBOL_LANGUAGE (psym) == language_ada)
20298         error (_("Ada is not currently supported by the index"));
20299
20300       /* Only add a given psymbol once.  */
20301       slot = htab_find_slot (psyms_seen, psym, INSERT);
20302       if (!*slot)
20303         {
20304           gdb_index_symbol_kind kind = symbol_kind (psym);
20305
20306           *slot = psym;
20307           add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
20308                            is_static, kind, cu_index);
20309         }
20310     }
20311 }
20312
20313 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
20314    exception if there is an error.  */
20315
20316 static void
20317 write_obstack (FILE *file, struct obstack *obstack)
20318 {
20319   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
20320               file)
20321       != obstack_object_size (obstack))
20322     error (_("couldn't data write to file"));
20323 }
20324
20325 /* Unlink a file if the argument is not NULL.  */
20326
20327 static void
20328 unlink_if_set (void *p)
20329 {
20330   char **filename = p;
20331   if (*filename)
20332     unlink (*filename);
20333 }
20334
20335 /* A helper struct used when iterating over debug_types.  */
20336 struct signatured_type_index_data
20337 {
20338   struct objfile *objfile;
20339   struct mapped_symtab *symtab;
20340   struct obstack *types_list;
20341   htab_t psyms_seen;
20342   int cu_index;
20343 };
20344
20345 /* A helper function that writes a single signatured_type to an
20346    obstack.  */
20347
20348 static int
20349 write_one_signatured_type (void **slot, void *d)
20350 {
20351   struct signatured_type_index_data *info = d;
20352   struct signatured_type *entry = (struct signatured_type *) *slot;
20353   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
20354   struct partial_symtab *psymtab = per_cu->v.psymtab;
20355   gdb_byte val[8];
20356
20357   write_psymbols (info->symtab,
20358                   info->psyms_seen,
20359                   info->objfile->global_psymbols.list
20360                   + psymtab->globals_offset,
20361                   psymtab->n_global_syms, info->cu_index,
20362                   0);
20363   write_psymbols (info->symtab,
20364                   info->psyms_seen,
20365                   info->objfile->static_psymbols.list
20366                   + psymtab->statics_offset,
20367                   psymtab->n_static_syms, info->cu_index,
20368                   1);
20369
20370   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20371                           entry->per_cu.offset.sect_off);
20372   obstack_grow (info->types_list, val, 8);
20373   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20374                           entry->type_offset_in_tu.cu_off);
20375   obstack_grow (info->types_list, val, 8);
20376   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
20377   obstack_grow (info->types_list, val, 8);
20378
20379   ++info->cu_index;
20380
20381   return 1;
20382 }
20383
20384 /* Recurse into all "included" dependencies and write their symbols as
20385    if they appeared in this psymtab.  */
20386
20387 static void
20388 recursively_write_psymbols (struct objfile *objfile,
20389                             struct partial_symtab *psymtab,
20390                             struct mapped_symtab *symtab,
20391                             htab_t psyms_seen,
20392                             offset_type cu_index)
20393 {
20394   int i;
20395
20396   for (i = 0; i < psymtab->number_of_dependencies; ++i)
20397     if (psymtab->dependencies[i]->user != NULL)
20398       recursively_write_psymbols (objfile, psymtab->dependencies[i],
20399                                   symtab, psyms_seen, cu_index);
20400
20401   write_psymbols (symtab,
20402                   psyms_seen,
20403                   objfile->global_psymbols.list + psymtab->globals_offset,
20404                   psymtab->n_global_syms, cu_index,
20405                   0);
20406   write_psymbols (symtab,
20407                   psyms_seen,
20408                   objfile->static_psymbols.list + psymtab->statics_offset,
20409                   psymtab->n_static_syms, cu_index,
20410                   1);
20411 }
20412
20413 /* Create an index file for OBJFILE in the directory DIR.  */
20414
20415 static void
20416 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
20417 {
20418   struct cleanup *cleanup;
20419   char *filename, *cleanup_filename;
20420   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
20421   struct obstack cu_list, types_cu_list;
20422   int i;
20423   FILE *out_file;
20424   struct mapped_symtab *symtab;
20425   offset_type val, size_of_contents, total_len;
20426   struct stat st;
20427   htab_t psyms_seen;
20428   htab_t cu_index_htab;
20429   struct psymtab_cu_index_map *psymtab_cu_index_map;
20430
20431   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
20432     return;
20433
20434   if (dwarf2_per_objfile->using_index)
20435     error (_("Cannot use an index to create the index"));
20436
20437   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
20438     error (_("Cannot make an index when the file has multiple .debug_types sections"));
20439
20440   if (stat (objfile->name, &st) < 0)
20441     perror_with_name (objfile->name);
20442
20443   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
20444                      INDEX_SUFFIX, (char *) NULL);
20445   cleanup = make_cleanup (xfree, filename);
20446
20447   out_file = fopen (filename, "wb");
20448   if (!out_file)
20449     error (_("Can't open `%s' for writing"), filename);
20450
20451   cleanup_filename = filename;
20452   make_cleanup (unlink_if_set, &cleanup_filename);
20453
20454   symtab = create_mapped_symtab ();
20455   make_cleanup (cleanup_mapped_symtab, symtab);
20456
20457   obstack_init (&addr_obstack);
20458   make_cleanup_obstack_free (&addr_obstack);
20459
20460   obstack_init (&cu_list);
20461   make_cleanup_obstack_free (&cu_list);
20462
20463   obstack_init (&types_cu_list);
20464   make_cleanup_obstack_free (&types_cu_list);
20465
20466   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
20467                                   NULL, xcalloc, xfree);
20468   make_cleanup_htab_delete (psyms_seen);
20469
20470   /* While we're scanning CU's create a table that maps a psymtab pointer
20471      (which is what addrmap records) to its index (which is what is recorded
20472      in the index file).  This will later be needed to write the address
20473      table.  */
20474   cu_index_htab = htab_create_alloc (100,
20475                                      hash_psymtab_cu_index,
20476                                      eq_psymtab_cu_index,
20477                                      NULL, xcalloc, xfree);
20478   make_cleanup_htab_delete (cu_index_htab);
20479   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
20480     xmalloc (sizeof (struct psymtab_cu_index_map)
20481              * dwarf2_per_objfile->n_comp_units);
20482   make_cleanup (xfree, psymtab_cu_index_map);
20483
20484   /* The CU list is already sorted, so we don't need to do additional
20485      work here.  Also, the debug_types entries do not appear in
20486      all_comp_units, but only in their own hash table.  */
20487   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
20488     {
20489       struct dwarf2_per_cu_data *per_cu
20490         = dwarf2_per_objfile->all_comp_units[i];
20491       struct partial_symtab *psymtab = per_cu->v.psymtab;
20492       gdb_byte val[8];
20493       struct psymtab_cu_index_map *map;
20494       void **slot;
20495
20496       if (psymtab->user == NULL)
20497         recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
20498
20499       map = &psymtab_cu_index_map[i];
20500       map->psymtab = psymtab;
20501       map->cu_index = i;
20502       slot = htab_find_slot (cu_index_htab, map, INSERT);
20503       gdb_assert (slot != NULL);
20504       gdb_assert (*slot == NULL);
20505       *slot = map;
20506
20507       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
20508                               per_cu->offset.sect_off);
20509       obstack_grow (&cu_list, val, 8);
20510       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
20511       obstack_grow (&cu_list, val, 8);
20512     }
20513
20514   /* Dump the address map.  */
20515   write_address_map (objfile, &addr_obstack, cu_index_htab);
20516
20517   /* Write out the .debug_type entries, if any.  */
20518   if (dwarf2_per_objfile->signatured_types)
20519     {
20520       struct signatured_type_index_data sig_data;
20521
20522       sig_data.objfile = objfile;
20523       sig_data.symtab = symtab;
20524       sig_data.types_list = &types_cu_list;
20525       sig_data.psyms_seen = psyms_seen;
20526       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
20527       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
20528                               write_one_signatured_type, &sig_data);
20529     }
20530
20531   /* Now that we've processed all symbols we can shrink their cu_indices
20532      lists.  */
20533   uniquify_cu_indices (symtab);
20534
20535   obstack_init (&constant_pool);
20536   make_cleanup_obstack_free (&constant_pool);
20537   obstack_init (&symtab_obstack);
20538   make_cleanup_obstack_free (&symtab_obstack);
20539   write_hash_table (symtab, &symtab_obstack, &constant_pool);
20540
20541   obstack_init (&contents);
20542   make_cleanup_obstack_free (&contents);
20543   size_of_contents = 6 * sizeof (offset_type);
20544   total_len = size_of_contents;
20545
20546   /* The version number.  */
20547   val = MAYBE_SWAP (8);
20548   obstack_grow (&contents, &val, sizeof (val));
20549
20550   /* The offset of the CU list from the start of the file.  */
20551   val = MAYBE_SWAP (total_len);
20552   obstack_grow (&contents, &val, sizeof (val));
20553   total_len += obstack_object_size (&cu_list);
20554
20555   /* The offset of the types CU list from the start of the file.  */
20556   val = MAYBE_SWAP (total_len);
20557   obstack_grow (&contents, &val, sizeof (val));
20558   total_len += obstack_object_size (&types_cu_list);
20559
20560   /* The offset of the address table from the start of the file.  */
20561   val = MAYBE_SWAP (total_len);
20562   obstack_grow (&contents, &val, sizeof (val));
20563   total_len += obstack_object_size (&addr_obstack);
20564
20565   /* The offset of the symbol table from the start of the file.  */
20566   val = MAYBE_SWAP (total_len);
20567   obstack_grow (&contents, &val, sizeof (val));
20568   total_len += obstack_object_size (&symtab_obstack);
20569
20570   /* The offset of the constant pool from the start of the file.  */
20571   val = MAYBE_SWAP (total_len);
20572   obstack_grow (&contents, &val, sizeof (val));
20573   total_len += obstack_object_size (&constant_pool);
20574
20575   gdb_assert (obstack_object_size (&contents) == size_of_contents);
20576
20577   write_obstack (out_file, &contents);
20578   write_obstack (out_file, &cu_list);
20579   write_obstack (out_file, &types_cu_list);
20580   write_obstack (out_file, &addr_obstack);
20581   write_obstack (out_file, &symtab_obstack);
20582   write_obstack (out_file, &constant_pool);
20583
20584   fclose (out_file);
20585
20586   /* We want to keep the file, so we set cleanup_filename to NULL
20587      here.  See unlink_if_set.  */
20588   cleanup_filename = NULL;
20589
20590   do_cleanups (cleanup);
20591 }
20592
20593 /* Implementation of the `save gdb-index' command.
20594    
20595    Note that the file format used by this command is documented in the
20596    GDB manual.  Any changes here must be documented there.  */
20597
20598 static void
20599 save_gdb_index_command (char *arg, int from_tty)
20600 {
20601   struct objfile *objfile;
20602
20603   if (!arg || !*arg)
20604     error (_("usage: save gdb-index DIRECTORY"));
20605
20606   ALL_OBJFILES (objfile)
20607   {
20608     struct stat st;
20609
20610     /* If the objfile does not correspond to an actual file, skip it.  */
20611     if (stat (objfile->name, &st) < 0)
20612       continue;
20613
20614     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20615     if (dwarf2_per_objfile)
20616       {
20617         volatile struct gdb_exception except;
20618
20619         TRY_CATCH (except, RETURN_MASK_ERROR)
20620           {
20621             write_psymtabs_to_index (objfile, arg);
20622           }
20623         if (except.reason < 0)
20624           exception_fprintf (gdb_stderr, except,
20625                              _("Error while writing index for `%s': "),
20626                              objfile->name);
20627       }
20628   }
20629 }
20630
20631 \f
20632
20633 int dwarf2_always_disassemble;
20634
20635 static void
20636 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
20637                                 struct cmd_list_element *c, const char *value)
20638 {
20639   fprintf_filtered (file,
20640                     _("Whether to always disassemble "
20641                       "DWARF expressions is %s.\n"),
20642                     value);
20643 }
20644
20645 static void
20646 show_check_physname (struct ui_file *file, int from_tty,
20647                      struct cmd_list_element *c, const char *value)
20648 {
20649   fprintf_filtered (file,
20650                     _("Whether to check \"physname\" is %s.\n"),
20651                     value);
20652 }
20653
20654 void _initialize_dwarf2_read (void);
20655
20656 void
20657 _initialize_dwarf2_read (void)
20658 {
20659   struct cmd_list_element *c;
20660
20661   dwarf2_objfile_data_key
20662     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
20663
20664   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
20665 Set DWARF 2 specific variables.\n\
20666 Configure DWARF 2 variables such as the cache size"),
20667                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
20668                   0/*allow-unknown*/, &maintenance_set_cmdlist);
20669
20670   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
20671 Show DWARF 2 specific variables\n\
20672 Show DWARF 2 variables such as the cache size"),
20673                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
20674                   0/*allow-unknown*/, &maintenance_show_cmdlist);
20675
20676   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
20677                             &dwarf2_max_cache_age, _("\
20678 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
20679 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
20680 A higher limit means that cached compilation units will be stored\n\
20681 in memory longer, and more total memory will be used.  Zero disables\n\
20682 caching, which can slow down startup."),
20683                             NULL,
20684                             show_dwarf2_max_cache_age,
20685                             &set_dwarf2_cmdlist,
20686                             &show_dwarf2_cmdlist);
20687
20688   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
20689                            &dwarf2_always_disassemble, _("\
20690 Set whether `info address' always disassembles DWARF expressions."), _("\
20691 Show whether `info address' always disassembles DWARF expressions."), _("\
20692 When enabled, DWARF expressions are always printed in an assembly-like\n\
20693 syntax.  When disabled, expressions will be printed in a more\n\
20694 conversational style, when possible."),
20695                            NULL,
20696                            show_dwarf2_always_disassemble,
20697                            &set_dwarf2_cmdlist,
20698                            &show_dwarf2_cmdlist);
20699
20700   add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
20701 Set debugging of the dwarf2 reader."), _("\
20702 Show debugging of the dwarf2 reader."), _("\
20703 When enabled, debugging messages are printed during dwarf2 reading\n\
20704 and symtab expansion."),
20705                             NULL,
20706                             NULL,
20707                             &setdebuglist, &showdebuglist);
20708
20709   add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
20710 Set debugging of the dwarf2 DIE reader."), _("\
20711 Show debugging of the dwarf2 DIE reader."), _("\
20712 When enabled (non-zero), DIEs are dumped after they are read in.\n\
20713 The value is the maximum depth to print."),
20714                              NULL,
20715                              NULL,
20716                              &setdebuglist, &showdebuglist);
20717
20718   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
20719 Set cross-checking of \"physname\" code against demangler."), _("\
20720 Show cross-checking of \"physname\" code against demangler."), _("\
20721 When enabled, GDB's internal \"physname\" code is checked against\n\
20722 the demangler."),
20723                            NULL, show_check_physname,
20724                            &setdebuglist, &showdebuglist);
20725
20726   add_setshow_boolean_cmd ("use-deprecated-index-sections",
20727                            no_class, &use_deprecated_index_sections, _("\
20728 Set whether to use deprecated gdb_index sections."), _("\
20729 Show whether to use deprecated gdb_index sections."), _("\
20730 When enabled, deprecated .gdb_index sections are used anyway.\n\
20731 Normally they are ignored either because of a missing feature or\n\
20732 performance issue.\n\
20733 Warning: This option must be enabled before gdb reads the file."),
20734                            NULL,
20735                            NULL,
20736                            &setlist, &showlist);
20737
20738   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
20739                _("\
20740 Save a gdb-index file.\n\
20741 Usage: save gdb-index DIRECTORY"),
20742                &save_cmdlist);
20743   set_cmd_completer (c, filename_completer);
20744
20745   dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
20746                                                         &dwarf2_locexpr_funcs);
20747   dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
20748                                                         &dwarf2_loclist_funcs);
20749
20750   dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
20751                                         &dwarf2_block_frame_base_locexpr_funcs);
20752   dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
20753                                         &dwarf2_block_frame_base_loclist_funcs);
20754 }