* dwarf2loc.c (dwarf2_evaluate_loc_desc_full): Expand parameter
[platform/upstream/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994-2012 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 "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h"  /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include "gdbcore.h" /* for gnutarget */
65 #include "gdb/gdb-index.h"
66 #include <ctype.h>
67 #include "gdb_bfd.h"
68
69 #include <fcntl.h>
70 #include "gdb_string.h"
71 #include "gdb_assert.h"
72 #include <sys/types.h>
73
74 typedef struct symbol *symbolp;
75 DEF_VEC_P (symbolp);
76
77 /* When non-zero, print basic high level tracing messages.
78    This is in contrast to the low level DIE reading of dwarf2_die_debug.  */
79 static int dwarf2_read_debug = 0;
80
81 /* When non-zero, dump DIEs after they are read in.  */
82 static int dwarf2_die_debug = 0;
83
84 /* When non-zero, cross-check physname against demangler.  */
85 static int check_physname = 0;
86
87 /* When non-zero, do not reject deprecated .gdb_index sections.  */
88 static int use_deprecated_index_sections = 0;
89
90 /* When set, the file that we're processing is known to have debugging
91    info for C++ namespaces.  GCC 3.3.x did not produce this information,
92    but later versions do.  */
93
94 static int processing_has_namespace_info;
95
96 static const struct objfile_data *dwarf2_objfile_data_key;
97
98 struct dwarf2_section_info
99 {
100   asection *asection;
101   gdb_byte *buffer;
102   bfd_size_type size;
103   /* True if we have tried to read this section.  */
104   int readin;
105 };
106
107 typedef struct dwarf2_section_info dwarf2_section_info_def;
108 DEF_VEC_O (dwarf2_section_info_def);
109
110 /* All offsets in the index are of this type.  It must be
111    architecture-independent.  */
112 typedef uint32_t offset_type;
113
114 DEF_VEC_I (offset_type);
115
116 /* Ensure only legit values are used.  */
117 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
118   do { \
119     gdb_assert ((unsigned int) (value) <= 1); \
120     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
121   } while (0)
122
123 /* Ensure only legit values are used.  */
124 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
125   do { \
126     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
127                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
128     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
129   } while (0)
130
131 /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
132 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
133   do { \
134     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
135     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
136   } while (0)
137
138 /* A description of the mapped index.  The file format is described in
139    a comment by the code that writes the index.  */
140 struct mapped_index
141 {
142   /* Index data format version.  */
143   int version;
144
145   /* The total length of the buffer.  */
146   off_t total_size;
147
148   /* A pointer to the address table data.  */
149   const gdb_byte *address_table;
150
151   /* Size of the address table data in bytes.  */
152   offset_type address_table_size;
153
154   /* The symbol table, implemented as a hash table.  */
155   const offset_type *symbol_table;
156
157   /* Size in slots, each slot is 2 offset_types.  */
158   offset_type symbol_table_slots;
159
160   /* A pointer to the constant pool.  */
161   const char *constant_pool;
162 };
163
164 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
165 DEF_VEC_P (dwarf2_per_cu_ptr);
166
167 /* Collection of data recorded per objfile.
168    This hangs off of dwarf2_objfile_data_key.  */
169
170 struct dwarf2_per_objfile
171 {
172   struct dwarf2_section_info info;
173   struct dwarf2_section_info abbrev;
174   struct dwarf2_section_info line;
175   struct dwarf2_section_info loc;
176   struct dwarf2_section_info macinfo;
177   struct dwarf2_section_info macro;
178   struct dwarf2_section_info str;
179   struct dwarf2_section_info ranges;
180   struct dwarf2_section_info addr;
181   struct dwarf2_section_info frame;
182   struct dwarf2_section_info eh_frame;
183   struct dwarf2_section_info gdb_index;
184
185   VEC (dwarf2_section_info_def) *types;
186
187   /* Back link.  */
188   struct objfile *objfile;
189
190   /* Table of all the compilation units.  This is used to locate
191      the target compilation unit of a particular reference.  */
192   struct dwarf2_per_cu_data **all_comp_units;
193
194   /* The number of compilation units in ALL_COMP_UNITS.  */
195   int n_comp_units;
196
197   /* The number of .debug_types-related CUs.  */
198   int n_type_units;
199
200   /* The .debug_types-related CUs (TUs).  */
201   struct signatured_type **all_type_units;
202
203   /* The number of entries in all_type_unit_groups.  */
204   int n_type_unit_groups;
205
206   /* Table of type unit groups.
207      This exists to make it easy to iterate over all CUs and TU groups.  */
208   struct type_unit_group **all_type_unit_groups;
209
210   /* Table of struct type_unit_group objects.
211      The hash key is the DW_AT_stmt_list value.  */
212   htab_t type_unit_groups;
213
214   /* A table mapping .debug_types signatures to its signatured_type entry.
215      This is NULL if the .debug_types section hasn't been read in yet.  */
216   htab_t signatured_types;
217
218   /* Type unit statistics, to see how well the scaling improvements
219      are doing.  */
220   struct tu_stats
221   {
222     int nr_uniq_abbrev_tables;
223     int nr_symtabs;
224     int nr_symtab_sharers;
225     int nr_stmt_less_type_units;
226   } tu_stats;
227
228   /* A chain of compilation units that are currently read in, so that
229      they can be freed later.  */
230   struct dwarf2_per_cu_data *read_in_chain;
231
232   /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
233      This is NULL if the table hasn't been allocated yet.  */
234   htab_t dwo_files;
235
236   /* The shared '.dwz' file, if one exists.  This is used when the
237      original data was compressed using 'dwz -m'.  */
238   struct dwz_file *dwz_file;
239
240   /* A flag indicating wether this objfile has a section loaded at a
241      VMA of 0.  */
242   int has_section_at_zero;
243
244   /* True if we are using the mapped index,
245      or we are faking it for OBJF_READNOW's sake.  */
246   unsigned char using_index;
247
248   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
249   struct mapped_index *index_table;
250
251   /* When using index_table, this keeps track of all quick_file_names entries.
252      TUs can share line table entries with CUs or other TUs, and there can be
253      a lot more TUs than unique line tables, so we maintain a separate table
254      of all line table entries to support the sharing.  */
255   htab_t quick_file_names_table;
256
257   /* Set during partial symbol reading, to prevent queueing of full
258      symbols.  */
259   int reading_partial_symbols;
260
261   /* Table mapping type DIEs to their struct type *.
262      This is NULL if not allocated yet.
263      The mapping is done via (CU/TU signature + DIE offset) -> type.  */
264   htab_t die_type_hash;
265
266   /* The CUs we recently read.  */
267   VEC (dwarf2_per_cu_ptr) *just_read_cus;
268 };
269
270 static struct dwarf2_per_objfile *dwarf2_per_objfile;
271
272 /* Default names of the debugging sections.  */
273
274 /* Note that if the debugging section has been compressed, it might
275    have a name like .zdebug_info.  */
276
277 static const struct dwarf2_debug_sections dwarf2_elf_names =
278 {
279   { ".debug_info", ".zdebug_info" },
280   { ".debug_abbrev", ".zdebug_abbrev" },
281   { ".debug_line", ".zdebug_line" },
282   { ".debug_loc", ".zdebug_loc" },
283   { ".debug_macinfo", ".zdebug_macinfo" },
284   { ".debug_macro", ".zdebug_macro" },
285   { ".debug_str", ".zdebug_str" },
286   { ".debug_ranges", ".zdebug_ranges" },
287   { ".debug_types", ".zdebug_types" },
288   { ".debug_addr", ".zdebug_addr" },
289   { ".debug_frame", ".zdebug_frame" },
290   { ".eh_frame", NULL },
291   { ".gdb_index", ".zgdb_index" },
292   23
293 };
294
295 /* List of DWO sections.  */
296
297 static const struct dwo_section_names
298 {
299   struct dwarf2_section_names abbrev_dwo;
300   struct dwarf2_section_names info_dwo;
301   struct dwarf2_section_names line_dwo;
302   struct dwarf2_section_names loc_dwo;
303   struct dwarf2_section_names macinfo_dwo;
304   struct dwarf2_section_names macro_dwo;
305   struct dwarf2_section_names str_dwo;
306   struct dwarf2_section_names str_offsets_dwo;
307   struct dwarf2_section_names types_dwo;
308 }
309 dwo_section_names =
310 {
311   { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
312   { ".debug_info.dwo", ".zdebug_info.dwo" },
313   { ".debug_line.dwo", ".zdebug_line.dwo" },
314   { ".debug_loc.dwo", ".zdebug_loc.dwo" },
315   { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
316   { ".debug_macro.dwo", ".zdebug_macro.dwo" },
317   { ".debug_str.dwo", ".zdebug_str.dwo" },
318   { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
319   { ".debug_types.dwo", ".zdebug_types.dwo" },
320 };
321
322 /* local data types */
323
324 /* The data in a compilation unit header, after target2host
325    translation, looks like this.  */
326 struct comp_unit_head
327 {
328   unsigned int length;
329   short version;
330   unsigned char addr_size;
331   unsigned char signed_addr_p;
332   sect_offset abbrev_offset;
333
334   /* Size of file offsets; either 4 or 8.  */
335   unsigned int offset_size;
336
337   /* Size of the length field; either 4 or 12.  */
338   unsigned int initial_length_size;
339
340   /* Offset to the first byte of this compilation unit header in the
341      .debug_info section, for resolving relative reference dies.  */
342   sect_offset offset;
343
344   /* Offset to first die in this cu from the start of the cu.
345      This will be the first byte following the compilation unit header.  */
346   cu_offset first_die_offset;
347 };
348
349 /* Type used for delaying computation of method physnames.
350    See comments for compute_delayed_physnames.  */
351 struct delayed_method_info
352 {
353   /* The type to which the method is attached, i.e., its parent class.  */
354   struct type *type;
355
356   /* The index of the method in the type's function fieldlists.  */
357   int fnfield_index;
358
359   /* The index of the method in the fieldlist.  */
360   int index;
361
362   /* The name of the DIE.  */
363   const char *name;
364
365   /*  The DIE associated with this method.  */
366   struct die_info *die;
367 };
368
369 typedef struct delayed_method_info delayed_method_info;
370 DEF_VEC_O (delayed_method_info);
371
372 /* Internal state when decoding a particular compilation unit.  */
373 struct dwarf2_cu
374 {
375   /* The objfile containing this compilation unit.  */
376   struct objfile *objfile;
377
378   /* The header of the compilation unit.  */
379   struct comp_unit_head header;
380
381   /* Base address of this compilation unit.  */
382   CORE_ADDR base_address;
383
384   /* Non-zero if base_address has been set.  */
385   int base_known;
386
387   /* The language we are debugging.  */
388   enum language language;
389   const struct language_defn *language_defn;
390
391   const char *producer;
392
393   /* The generic symbol table building routines have separate lists for
394      file scope symbols and all all other scopes (local scopes).  So
395      we need to select the right one to pass to add_symbol_to_list().
396      We do it by keeping a pointer to the correct list in list_in_scope.
397
398      FIXME: The original dwarf code just treated the file scope as the
399      first local scope, and all other local scopes as nested local
400      scopes, and worked fine.  Check to see if we really need to
401      distinguish these in buildsym.c.  */
402   struct pending **list_in_scope;
403
404   /* The abbrev table for this CU.
405      Normally this points to the abbrev table in the objfile.
406      But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
407   struct abbrev_table *abbrev_table;
408
409   /* Hash table holding all the loaded partial DIEs
410      with partial_die->offset.SECT_OFF as hash.  */
411   htab_t partial_dies;
412
413   /* Storage for things with the same lifetime as this read-in compilation
414      unit, including partial DIEs.  */
415   struct obstack comp_unit_obstack;
416
417   /* When multiple dwarf2_cu structures are living in memory, this field
418      chains them all together, so that they can be released efficiently.
419      We will probably also want a generation counter so that most-recently-used
420      compilation units are cached...  */
421   struct dwarf2_per_cu_data *read_in_chain;
422
423   /* Backchain to our per_cu entry if the tree has been built.  */
424   struct dwarf2_per_cu_data *per_cu;
425
426   /* How many compilation units ago was this CU last referenced?  */
427   int last_used;
428
429   /* A hash table of DIE cu_offset for following references with
430      die_info->offset.sect_off as hash.  */
431   htab_t die_hash;
432
433   /* Full DIEs if read in.  */
434   struct die_info *dies;
435
436   /* A set of pointers to dwarf2_per_cu_data objects for compilation
437      units referenced by this one.  Only set during full symbol processing;
438      partial symbol tables do not have dependencies.  */
439   htab_t dependencies;
440
441   /* Header data from the line table, during full symbol processing.  */
442   struct line_header *line_header;
443
444   /* A list of methods which need to have physnames computed
445      after all type information has been read.  */
446   VEC (delayed_method_info) *method_list;
447
448   /* To be copied to symtab->call_site_htab.  */
449   htab_t call_site_htab;
450
451   /* Non-NULL if this CU came from a DWO file.
452      There is an invariant here that is important to remember:
453      Except for attributes copied from the top level DIE in the "main"
454      (or "stub") file in preparation for reading the DWO file
455      (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
456      Either there isn't a DWO file (in which case this is NULL and the point
457      is moot), or there is and either we're not going to read it (in which
458      case this is NULL) or there is and we are reading it (in which case this
459      is non-NULL).  */
460   struct dwo_unit *dwo_unit;
461
462   /* The DW_AT_addr_base attribute if present, zero otherwise
463      (zero is a valid value though).
464      Note this value comes from the stub CU/TU's DIE.  */
465   ULONGEST addr_base;
466
467   /* The DW_AT_ranges_base attribute if present, zero otherwise
468      (zero is a valid value though).
469      Note this value comes from the stub CU/TU's DIE.
470      Also note that the value is zero in the non-DWO case so this value can
471      be used without needing to know whether DWO files are in use or not.  */
472   ULONGEST ranges_base;
473
474   /* Mark used when releasing cached dies.  */
475   unsigned int mark : 1;
476
477   /* This CU references .debug_loc.  See the symtab->locations_valid field.
478      This test is imperfect as there may exist optimized debug code not using
479      any location list and still facing inlining issues if handled as
480      unoptimized code.  For a future better test see GCC PR other/32998.  */
481   unsigned int has_loclist : 1;
482
483   /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
484      CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
485      are valid.  This information is cached because profiling CU expansion
486      showed excessive time spent in producer_is_gxx_lt_4_6.  */
487   unsigned int checked_producer : 1;
488   unsigned int producer_is_gxx_lt_4_6 : 1;
489   unsigned int producer_is_icc : 1;
490 };
491
492 /* Persistent data held for a compilation unit, even when not
493    processing it.  We put a pointer to this structure in the
494    read_symtab_private field of the psymtab.  */
495
496 struct dwarf2_per_cu_data
497 {
498   /* The start offset and length of this compilation unit.
499      NOTE: Unlike comp_unit_head.length, this length includes
500      initial_length_size.
501      If the DIE refers to a DWO file, this is always of the original die,
502      not the DWO file.  */
503   sect_offset offset;
504   unsigned int length;
505
506   /* Flag indicating this compilation unit will be read in before
507      any of the current compilation units are processed.  */
508   unsigned int queued : 1;
509
510   /* This flag will be set when reading partial DIEs if we need to load
511      absolutely all DIEs for this compilation unit, instead of just the ones
512      we think are interesting.  It gets set if we look for a DIE in the
513      hash table and don't find it.  */
514   unsigned int load_all_dies : 1;
515
516   /* Non-zero if this CU is from .debug_types.  */
517   unsigned int is_debug_types : 1;
518
519   /* Non-zero if this CU is from the .dwz file.  */
520   unsigned int is_dwz : 1;
521
522   /* The section this CU/TU lives in.
523      If the DIE refers to a DWO file, this is always the original die,
524      not the DWO file.  */
525   struct dwarf2_section_info *info_or_types_section;
526
527   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
528      of the CU cache it gets reset to NULL again.  */
529   struct dwarf2_cu *cu;
530
531   /* The corresponding objfile.
532      Normally we can get the objfile from dwarf2_per_objfile.
533      However we can enter this file with just a "per_cu" handle.  */
534   struct objfile *objfile;
535
536   /* When using partial symbol tables, the 'psymtab' field is active.
537      Otherwise the 'quick' field is active.  */
538   union
539   {
540     /* The partial symbol table associated with this compilation unit,
541        or NULL for unread partial units.  */
542     struct partial_symtab *psymtab;
543
544     /* Data needed by the "quick" functions.  */
545     struct dwarf2_per_cu_quick_data *quick;
546   } v;
547
548   union
549   {
550     /* The CUs we import using DW_TAG_imported_unit.  This is filled in
551        while reading psymtabs, used to compute the psymtab dependencies,
552        and then cleared.  Then it is filled in again while reading full
553        symbols, and only deleted when the objfile is destroyed.  */
554     VEC (dwarf2_per_cu_ptr) *imported_symtabs;
555
556     /* Type units are grouped by their DW_AT_stmt_list entry so that they
557        can share them.  If this is a TU, this points to the containing
558        symtab.  */
559     struct type_unit_group *type_unit_group;
560   } s;
561 };
562
563 /* Entry in the signatured_types hash table.  */
564
565 struct signatured_type
566 {
567   /* The "per_cu" object of this type.
568      N.B.: This is the first member so that it's easy to convert pointers
569      between them.  */
570   struct dwarf2_per_cu_data per_cu;
571
572   /* The type's signature.  */
573   ULONGEST signature;
574
575   /* Offset in the TU of the type's DIE, as read from the TU header.
576      If the definition lives in a DWO file, this value is unusable.  */
577   cu_offset type_offset_in_tu;
578
579   /* Offset in the section of the type's DIE.
580      If the definition lives in a DWO file, this is the offset in the
581      .debug_types.dwo section.
582      The value is zero until the actual value is known.
583      Zero is otherwise not a valid section offset.  */
584   sect_offset type_offset_in_section;
585 };
586
587 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588    This includes type_unit_group and quick_file_names.  */
589
590 struct stmt_list_hash
591 {
592   /* The DWO unit this table is from or NULL if there is none.  */
593   struct dwo_unit *dwo_unit;
594
595   /* Offset in .debug_line or .debug_line.dwo.  */
596   sect_offset line_offset;
597 };
598
599 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600    an object of this type.  */
601
602 struct type_unit_group
603 {
604   /* dwarf2read.c's main "handle" on the symtab.
605      To simplify things we create an artificial CU that "includes" all the
606      type units using this stmt_list so that the rest of the code still has
607      a "per_cu" handle on the symtab.
608      This PER_CU is recognized by having no section.  */
609 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->info_or_types_section == NULL)
610   struct dwarf2_per_cu_data per_cu;
611
612   union
613   {
614     /* The TUs that share this DW_AT_stmt_list entry.
615        This is added to while parsing type units to build partial symtabs,
616        and is deleted afterwards and not used again.  */
617     VEC (dwarf2_per_cu_ptr) *tus;
618
619     /* When reading the line table in "quick" functions, we need a real TU.
620        Any will do, we know they all share the same DW_AT_stmt_list entry.
621        For simplicity's sake, we pick the first one.  */
622     struct dwarf2_per_cu_data *first_tu;
623   } t;
624
625   /* The primary symtab.
626      Type units in a group needn't all be defined in the same source file,
627      so we create an essentially anonymous symtab as the primary symtab.  */
628   struct symtab *primary_symtab;
629
630   /* The data used to construct the hash key.  */
631   struct stmt_list_hash hash;
632
633   /* The number of symtabs from the line header.
634      The value here must match line_header.num_file_names.  */
635   unsigned int num_symtabs;
636
637   /* The symbol tables for this TU (obtained from the files listed in
638      DW_AT_stmt_list).
639      WARNING: The order of entries here must match the order of entries
640      in the line header.  After the first TU using this type_unit_group, the
641      line header for the subsequent TUs is recreated from this.  This is done
642      because we need to use the same symtabs for each TU using the same
643      DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
644      there's no guarantee the line header doesn't have duplicate entries.  */
645   struct symtab **symtabs;
646 };
647
648 /* These sections are what may appear in a "dwo" file.  */
649
650 struct dwo_sections
651 {
652   struct dwarf2_section_info abbrev;
653   struct dwarf2_section_info info;
654   struct dwarf2_section_info line;
655   struct dwarf2_section_info loc;
656   struct dwarf2_section_info macinfo;
657   struct dwarf2_section_info macro;
658   struct dwarf2_section_info str;
659   struct dwarf2_section_info str_offsets;
660   VEC (dwarf2_section_info_def) *types;
661 };
662
663 /* Common bits of DWO CUs/TUs.  */
664
665 struct dwo_unit
666 {
667   /* Backlink to the containing struct dwo_file.  */
668   struct dwo_file *dwo_file;
669
670   /* The "id" that distinguishes this CU/TU.
671      .debug_info calls this "dwo_id", .debug_types calls this "signature".
672      Since signatures came first, we stick with it for consistency.  */
673   ULONGEST signature;
674
675   /* The section this CU/TU lives in, in the DWO file.  */
676   struct dwarf2_section_info *info_or_types_section;
677
678   /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section.  */
679   sect_offset offset;
680   unsigned int length;
681
682   /* For types, offset in the type's DIE of the type defined by this TU.  */
683   cu_offset type_offset_in_tu;
684 };
685
686 /* Data for one DWO file.  */
687
688 struct dwo_file
689 {
690   /* The DW_AT_GNU_dwo_name attribute.
691      We don't manage space for this, it's an attribute.  */
692   const char *dwo_name;
693
694   /* The bfd, when the file is open.  Otherwise this is NULL.  */
695   bfd *dwo_bfd;
696
697   /* Section info for this file.  */
698   struct dwo_sections sections;
699
700   /* Table of CUs in the file.
701      Each element is a struct dwo_unit.  */
702   htab_t cus;
703
704   /* Table of TUs in the file.
705      Each element is a struct dwo_unit.  */
706   htab_t tus;
707 };
708
709 /* This represents a '.dwz' file.  */
710
711 struct dwz_file
712 {
713   /* A dwz file can only contain a few sections.  */
714   struct dwarf2_section_info abbrev;
715   struct dwarf2_section_info info;
716   struct dwarf2_section_info str;
717   struct dwarf2_section_info line;
718   struct dwarf2_section_info macro;
719   struct dwarf2_section_info gdb_index;
720
721   /* The dwz's BFD.  */
722   bfd *dwz_bfd;
723 };
724
725 /* Struct used to pass misc. parameters to read_die_and_children, et
726    al.  which are used for both .debug_info and .debug_types dies.
727    All parameters here are unchanging for the life of the call.  This
728    struct exists to abstract away the constant parameters of die reading.  */
729
730 struct die_reader_specs
731 {
732   /* die_section->asection->owner.  */
733   bfd* abfd;
734
735   /* The CU of the DIE we are parsing.  */
736   struct dwarf2_cu *cu;
737
738   /* Non-NULL if reading a DWO file.  */
739   struct dwo_file *dwo_file;
740
741   /* The section the die comes from.
742      This is either .debug_info or .debug_types, or the .dwo variants.  */
743   struct dwarf2_section_info *die_section;
744
745   /* die_section->buffer.  */
746   gdb_byte *buffer;
747
748   /* The end of the buffer.  */
749   const gdb_byte *buffer_end;
750 };
751
752 /* Type of function passed to init_cutu_and_read_dies, et.al.  */
753 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
754                                       gdb_byte *info_ptr,
755                                       struct die_info *comp_unit_die,
756                                       int has_children,
757                                       void *data);
758
759 /* The line number information for a compilation unit (found in the
760    .debug_line section) begins with a "statement program header",
761    which contains the following information.  */
762 struct line_header
763 {
764   unsigned int total_length;
765   unsigned short version;
766   unsigned int header_length;
767   unsigned char minimum_instruction_length;
768   unsigned char maximum_ops_per_instruction;
769   unsigned char default_is_stmt;
770   int line_base;
771   unsigned char line_range;
772   unsigned char opcode_base;
773
774   /* standard_opcode_lengths[i] is the number of operands for the
775      standard opcode whose value is i.  This means that
776      standard_opcode_lengths[0] is unused, and the last meaningful
777      element is standard_opcode_lengths[opcode_base - 1].  */
778   unsigned char *standard_opcode_lengths;
779
780   /* The include_directories table.  NOTE!  These strings are not
781      allocated with xmalloc; instead, they are pointers into
782      debug_line_buffer.  If you try to free them, `free' will get
783      indigestion.  */
784   unsigned int num_include_dirs, include_dirs_size;
785   char **include_dirs;
786
787   /* The file_names table.  NOTE!  These strings are not allocated
788      with xmalloc; instead, they are pointers into debug_line_buffer.
789      Don't try to free them directly.  */
790   unsigned int num_file_names, file_names_size;
791   struct file_entry
792   {
793     char *name;
794     unsigned int dir_index;
795     unsigned int mod_time;
796     unsigned int length;
797     int included_p; /* Non-zero if referenced by the Line Number Program.  */
798     struct symtab *symtab; /* The associated symbol table, if any.  */
799   } *file_names;
800
801   /* The start and end of the statement program following this
802      header.  These point into dwarf2_per_objfile->line_buffer.  */
803   gdb_byte *statement_program_start, *statement_program_end;
804 };
805
806 /* When we construct a partial symbol table entry we only
807    need this much information.  */
808 struct partial_die_info
809   {
810     /* Offset of this DIE.  */
811     sect_offset offset;
812
813     /* DWARF-2 tag for this DIE.  */
814     ENUM_BITFIELD(dwarf_tag) tag : 16;
815
816     /* Assorted flags describing the data found in this DIE.  */
817     unsigned int has_children : 1;
818     unsigned int is_external : 1;
819     unsigned int is_declaration : 1;
820     unsigned int has_type : 1;
821     unsigned int has_specification : 1;
822     unsigned int has_pc_info : 1;
823     unsigned int may_be_inlined : 1;
824
825     /* Flag set if the SCOPE field of this structure has been
826        computed.  */
827     unsigned int scope_set : 1;
828
829     /* Flag set if the DIE has a byte_size attribute.  */
830     unsigned int has_byte_size : 1;
831
832     /* Flag set if any of the DIE's children are template arguments.  */
833     unsigned int has_template_arguments : 1;
834
835     /* Flag set if fixup_partial_die has been called on this die.  */
836     unsigned int fixup_called : 1;
837
838     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
839     unsigned int is_dwz : 1;
840
841     /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
842     unsigned int spec_is_dwz : 1;
843
844     /* The name of this DIE.  Normally the value of DW_AT_name, but
845        sometimes a default name for unnamed DIEs.  */
846     char *name;
847
848     /* The linkage name, if present.  */
849     const char *linkage_name;
850
851     /* The scope to prepend to our children.  This is generally
852        allocated on the comp_unit_obstack, so will disappear
853        when this compilation unit leaves the cache.  */
854     char *scope;
855
856     /* Some data associated with the partial DIE.  The tag determines
857        which field is live.  */
858     union
859     {
860       /* The location description associated with this DIE, if any.  */
861       struct dwarf_block *locdesc;
862       /* The offset of an import, for DW_TAG_imported_unit.  */
863       sect_offset offset;
864     } d;
865
866     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
867     CORE_ADDR lowpc;
868     CORE_ADDR highpc;
869
870     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
871        DW_AT_sibling, if any.  */
872     /* NOTE: This member isn't strictly necessary, read_partial_die could
873        return DW_AT_sibling values to its caller load_partial_dies.  */
874     gdb_byte *sibling;
875
876     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
877        DW_AT_specification (or DW_AT_abstract_origin or
878        DW_AT_extension).  */
879     sect_offset spec_offset;
880
881     /* Pointers to this DIE's parent, first child, and next sibling,
882        if any.  */
883     struct partial_die_info *die_parent, *die_child, *die_sibling;
884   };
885
886 /* This data structure holds the information of an abbrev.  */
887 struct abbrev_info
888   {
889     unsigned int number;        /* number identifying abbrev */
890     enum dwarf_tag tag;         /* dwarf tag */
891     unsigned short has_children;                /* boolean */
892     unsigned short num_attrs;   /* number of attributes */
893     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
894     struct abbrev_info *next;   /* next in chain */
895   };
896
897 struct attr_abbrev
898   {
899     ENUM_BITFIELD(dwarf_attribute) name : 16;
900     ENUM_BITFIELD(dwarf_form) form : 16;
901   };
902
903 /* Size of abbrev_table.abbrev_hash_table.  */
904 #define ABBREV_HASH_SIZE 121
905
906 /* Top level data structure to contain an abbreviation table.  */
907
908 struct abbrev_table
909 {
910   /* Where the abbrev table came from.
911      This is used as a sanity check when the table is used.  */
912   sect_offset offset;
913
914   /* Storage for the abbrev table.  */
915   struct obstack abbrev_obstack;
916
917   /* Hash table of abbrevs.
918      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
919      It could be statically allocated, but the previous code didn't so we
920      don't either.  */
921   struct abbrev_info **abbrevs;
922 };
923
924 /* Attributes have a name and a value.  */
925 struct attribute
926   {
927     ENUM_BITFIELD(dwarf_attribute) name : 16;
928     ENUM_BITFIELD(dwarf_form) form : 15;
929
930     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
931        field should be in u.str (existing only for DW_STRING) but it is kept
932        here for better struct attribute alignment.  */
933     unsigned int string_is_canonical : 1;
934
935     union
936       {
937         char *str;
938         struct dwarf_block *blk;
939         ULONGEST unsnd;
940         LONGEST snd;
941         CORE_ADDR addr;
942         struct signatured_type *signatured_type;
943       }
944     u;
945   };
946
947 /* This data structure holds a complete die structure.  */
948 struct die_info
949   {
950     /* DWARF-2 tag for this DIE.  */
951     ENUM_BITFIELD(dwarf_tag) tag : 16;
952
953     /* Number of attributes */
954     unsigned char num_attrs;
955
956     /* True if we're presently building the full type name for the
957        type derived from this DIE.  */
958     unsigned char building_fullname : 1;
959
960     /* Abbrev number */
961     unsigned int abbrev;
962
963     /* Offset in .debug_info or .debug_types section.  */
964     sect_offset offset;
965
966     /* The dies in a compilation unit form an n-ary tree.  PARENT
967        points to this die's parent; CHILD points to the first child of
968        this node; and all the children of a given node are chained
969        together via their SIBLING fields.  */
970     struct die_info *child;     /* Its first child, if any.  */
971     struct die_info *sibling;   /* Its next sibling, if any.  */
972     struct die_info *parent;    /* Its parent, if any.  */
973
974     /* An array of attributes, with NUM_ATTRS elements.  There may be
975        zero, but it's not common and zero-sized arrays are not
976        sufficiently portable C.  */
977     struct attribute attrs[1];
978   };
979
980 /* Get at parts of an attribute structure.  */
981
982 #define DW_STRING(attr)    ((attr)->u.str)
983 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
984 #define DW_UNSND(attr)     ((attr)->u.unsnd)
985 #define DW_BLOCK(attr)     ((attr)->u.blk)
986 #define DW_SND(attr)       ((attr)->u.snd)
987 #define DW_ADDR(attr)      ((attr)->u.addr)
988 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
989
990 /* Blocks are a bunch of untyped bytes.  */
991 struct dwarf_block
992   {
993     size_t size;
994
995     /* Valid only if SIZE is not zero.  */
996     gdb_byte *data;
997   };
998
999 #ifndef ATTR_ALLOC_CHUNK
1000 #define ATTR_ALLOC_CHUNK 4
1001 #endif
1002
1003 /* Allocate fields for structs, unions and enums in this size.  */
1004 #ifndef DW_FIELD_ALLOC_CHUNK
1005 #define DW_FIELD_ALLOC_CHUNK 4
1006 #endif
1007
1008 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1009    but this would require a corresponding change in unpack_field_as_long
1010    and friends.  */
1011 static int bits_per_byte = 8;
1012
1013 /* The routines that read and process dies for a C struct or C++ class
1014    pass lists of data member fields and lists of member function fields
1015    in an instance of a field_info structure, as defined below.  */
1016 struct field_info
1017   {
1018     /* List of data member and baseclasses fields.  */
1019     struct nextfield
1020       {
1021         struct nextfield *next;
1022         int accessibility;
1023         int virtuality;
1024         struct field field;
1025       }
1026      *fields, *baseclasses;
1027
1028     /* Number of fields (including baseclasses).  */
1029     int nfields;
1030
1031     /* Number of baseclasses.  */
1032     int nbaseclasses;
1033
1034     /* Set if the accesibility of one of the fields is not public.  */
1035     int non_public_fields;
1036
1037     /* Member function fields array, entries are allocated in the order they
1038        are encountered in the object file.  */
1039     struct nextfnfield
1040       {
1041         struct nextfnfield *next;
1042         struct fn_field fnfield;
1043       }
1044      *fnfields;
1045
1046     /* Member function fieldlist array, contains name of possibly overloaded
1047        member function, number of overloaded member functions and a pointer
1048        to the head of the member function field chain.  */
1049     struct fnfieldlist
1050       {
1051         char *name;
1052         int length;
1053         struct nextfnfield *head;
1054       }
1055      *fnfieldlists;
1056
1057     /* Number of entries in the fnfieldlists array.  */
1058     int nfnfields;
1059
1060     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
1061        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
1062     struct typedef_field_list
1063       {
1064         struct typedef_field field;
1065         struct typedef_field_list *next;
1066       }
1067     *typedef_field_list;
1068     unsigned typedef_field_list_count;
1069   };
1070
1071 /* One item on the queue of compilation units to read in full symbols
1072    for.  */
1073 struct dwarf2_queue_item
1074 {
1075   struct dwarf2_per_cu_data *per_cu;
1076   enum language pretend_language;
1077   struct dwarf2_queue_item *next;
1078 };
1079
1080 /* The current queue.  */
1081 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1082
1083 /* Loaded secondary compilation units are kept in memory until they
1084    have not been referenced for the processing of this many
1085    compilation units.  Set this to zero to disable caching.  Cache
1086    sizes of up to at least twenty will improve startup time for
1087    typical inter-CU-reference binaries, at an obvious memory cost.  */
1088 static int dwarf2_max_cache_age = 5;
1089 static void
1090 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1091                            struct cmd_list_element *c, const char *value)
1092 {
1093   fprintf_filtered (file, _("The upper bound on the age of cached "
1094                             "dwarf2 compilation units is %s.\n"),
1095                     value);
1096 }
1097
1098
1099 /* Various complaints about symbol reading that don't abort the process.  */
1100
1101 static void
1102 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1103 {
1104   complaint (&symfile_complaints,
1105              _("statement list doesn't fit in .debug_line section"));
1106 }
1107
1108 static void
1109 dwarf2_debug_line_missing_file_complaint (void)
1110 {
1111   complaint (&symfile_complaints,
1112              _(".debug_line section has line data without a file"));
1113 }
1114
1115 static void
1116 dwarf2_debug_line_missing_end_sequence_complaint (void)
1117 {
1118   complaint (&symfile_complaints,
1119              _(".debug_line section has line "
1120                "program sequence without an end"));
1121 }
1122
1123 static void
1124 dwarf2_complex_location_expr_complaint (void)
1125 {
1126   complaint (&symfile_complaints, _("location expression too complex"));
1127 }
1128
1129 static void
1130 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1131                                               int arg3)
1132 {
1133   complaint (&symfile_complaints,
1134              _("const value length mismatch for '%s', got %d, expected %d"),
1135              arg1, arg2, arg3);
1136 }
1137
1138 static void
1139 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1140 {
1141   complaint (&symfile_complaints,
1142              _("debug info runs off end of %s section"
1143                " [in module %s]"),
1144              section->asection->name,
1145              bfd_get_filename (section->asection->owner));
1146 }
1147
1148 static void
1149 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1150 {
1151   complaint (&symfile_complaints,
1152              _("macro debug info contains a "
1153                "malformed macro definition:\n`%s'"),
1154              arg1);
1155 }
1156
1157 static void
1158 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1159 {
1160   complaint (&symfile_complaints,
1161              _("invalid attribute class or form for '%s' in '%s'"),
1162              arg1, arg2);
1163 }
1164
1165 /* local function prototypes */
1166
1167 static void dwarf2_locate_sections (bfd *, asection *, void *);
1168
1169 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1170                                            struct objfile *);
1171
1172 static void dwarf2_find_base_address (struct die_info *die,
1173                                       struct dwarf2_cu *cu);
1174
1175 static void dwarf2_build_psymtabs_hard (struct objfile *);
1176
1177 static void scan_partial_symbols (struct partial_die_info *,
1178                                   CORE_ADDR *, CORE_ADDR *,
1179                                   int, struct dwarf2_cu *);
1180
1181 static void add_partial_symbol (struct partial_die_info *,
1182                                 struct dwarf2_cu *);
1183
1184 static void add_partial_namespace (struct partial_die_info *pdi,
1185                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
1186                                    int need_pc, struct dwarf2_cu *cu);
1187
1188 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1189                                 CORE_ADDR *highpc, int need_pc,
1190                                 struct dwarf2_cu *cu);
1191
1192 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1193                                      struct dwarf2_cu *cu);
1194
1195 static void add_partial_subprogram (struct partial_die_info *pdi,
1196                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
1197                                     int need_pc, struct dwarf2_cu *cu);
1198
1199 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
1200
1201 static void psymtab_to_symtab_1 (struct partial_symtab *);
1202
1203 static struct abbrev_info *abbrev_table_lookup_abbrev
1204   (const struct abbrev_table *, unsigned int);
1205
1206 static struct abbrev_table *abbrev_table_read_table
1207   (struct dwarf2_section_info *, sect_offset);
1208
1209 static void abbrev_table_free (struct abbrev_table *);
1210
1211 static void abbrev_table_free_cleanup (void *);
1212
1213 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1214                                  struct dwarf2_section_info *);
1215
1216 static void dwarf2_free_abbrev_table (void *);
1217
1218 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1219
1220 static struct partial_die_info *load_partial_dies
1221   (const struct die_reader_specs *, gdb_byte *, int);
1222
1223 static gdb_byte *read_partial_die (const struct die_reader_specs *,
1224                                    struct partial_die_info *,
1225                                    struct abbrev_info *,
1226                                    unsigned int,
1227                                    gdb_byte *);
1228
1229 static struct partial_die_info *find_partial_die (sect_offset, int,
1230                                                   struct dwarf2_cu *);
1231
1232 static void fixup_partial_die (struct partial_die_info *,
1233                                struct dwarf2_cu *);
1234
1235 static gdb_byte *read_attribute (const struct die_reader_specs *,
1236                                  struct attribute *, struct attr_abbrev *,
1237                                  gdb_byte *);
1238
1239 static unsigned int read_1_byte (bfd *, gdb_byte *);
1240
1241 static int read_1_signed_byte (bfd *, gdb_byte *);
1242
1243 static unsigned int read_2_bytes (bfd *, gdb_byte *);
1244
1245 static unsigned int read_4_bytes (bfd *, gdb_byte *);
1246
1247 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
1248
1249 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
1250                                unsigned int *);
1251
1252 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1253
1254 static LONGEST read_checked_initial_length_and_offset
1255   (bfd *, gdb_byte *, const struct comp_unit_head *,
1256    unsigned int *, unsigned int *);
1257
1258 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
1259                             unsigned int *);
1260
1261 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
1262
1263 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1264                                        sect_offset);
1265
1266 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
1267
1268 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
1269
1270 static char *read_indirect_string (bfd *, gdb_byte *,
1271                                    const struct comp_unit_head *,
1272                                    unsigned int *);
1273
1274 static char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1275
1276 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
1277
1278 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
1279
1280 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1281                                               unsigned int *);
1282
1283 static char *read_str_index (const struct die_reader_specs *reader,
1284                              struct dwarf2_cu *cu, ULONGEST str_index);
1285
1286 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1287
1288 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289                                       struct dwarf2_cu *);
1290
1291 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1292                                                 unsigned int);
1293
1294 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295                                struct dwarf2_cu *cu);
1296
1297 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1298
1299 static struct die_info *die_specification (struct die_info *die,
1300                                            struct dwarf2_cu **);
1301
1302 static void free_line_header (struct line_header *lh);
1303
1304 static void add_file_name (struct line_header *, char *, unsigned int,
1305                            unsigned int, unsigned int);
1306
1307 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1308                                                      struct dwarf2_cu *cu);
1309
1310 static void dwarf_decode_lines (struct line_header *, const char *,
1311                                 struct dwarf2_cu *, struct partial_symtab *,
1312                                 int);
1313
1314 static void dwarf2_start_subfile (char *, const char *, const char *);
1315
1316 static void dwarf2_start_symtab (struct dwarf2_cu *,
1317                                  char *, char *, CORE_ADDR);
1318
1319 static struct symbol *new_symbol (struct die_info *, struct type *,
1320                                   struct dwarf2_cu *);
1321
1322 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1323                                        struct dwarf2_cu *, struct symbol *);
1324
1325 static void dwarf2_const_value (struct attribute *, struct symbol *,
1326                                 struct dwarf2_cu *);
1327
1328 static void dwarf2_const_value_attr (struct attribute *attr,
1329                                      struct type *type,
1330                                      const char *name,
1331                                      struct obstack *obstack,
1332                                      struct dwarf2_cu *cu, LONGEST *value,
1333                                      gdb_byte **bytes,
1334                                      struct dwarf2_locexpr_baton **baton);
1335
1336 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1337
1338 static int need_gnat_info (struct dwarf2_cu *);
1339
1340 static struct type *die_descriptive_type (struct die_info *,
1341                                           struct dwarf2_cu *);
1342
1343 static void set_descriptive_type (struct type *, struct die_info *,
1344                                   struct dwarf2_cu *);
1345
1346 static struct type *die_containing_type (struct die_info *,
1347                                          struct dwarf2_cu *);
1348
1349 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1350                                      struct dwarf2_cu *);
1351
1352 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
1356 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1357
1358 static char *typename_concat (struct obstack *obs, const char *prefix,
1359                               const char *suffix, int physname,
1360                               struct dwarf2_cu *cu);
1361
1362 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1363
1364 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
1366 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1367
1368 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1369
1370 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
1372 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1373                                struct dwarf2_cu *, struct partial_symtab *);
1374
1375 static int dwarf2_get_pc_bounds (struct die_info *,
1376                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1377                                  struct partial_symtab *);
1378
1379 static void get_scope_pc_bounds (struct die_info *,
1380                                  CORE_ADDR *, CORE_ADDR *,
1381                                  struct dwarf2_cu *);
1382
1383 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1384                                         CORE_ADDR, struct dwarf2_cu *);
1385
1386 static void dwarf2_add_field (struct field_info *, struct die_info *,
1387                               struct dwarf2_cu *);
1388
1389 static void dwarf2_attach_fields_to_type (struct field_info *,
1390                                           struct type *, struct dwarf2_cu *);
1391
1392 static void dwarf2_add_member_fn (struct field_info *,
1393                                   struct die_info *, struct type *,
1394                                   struct dwarf2_cu *);
1395
1396 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1397                                              struct type *,
1398                                              struct dwarf2_cu *);
1399
1400 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1401
1402 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1403
1404 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1405
1406 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1407
1408 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1409
1410 static struct type *read_module_type (struct die_info *die,
1411                                       struct dwarf2_cu *cu);
1412
1413 static const char *namespace_name (struct die_info *die,
1414                                    int *is_anonymous, struct dwarf2_cu *);
1415
1416 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1417
1418 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1419
1420 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1421                                                        struct dwarf2_cu *);
1422
1423 static struct die_info *read_die_and_children (const struct die_reader_specs *,
1424                                                gdb_byte *info_ptr,
1425                                                gdb_byte **new_info_ptr,
1426                                                struct die_info *parent);
1427
1428 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1429                                                gdb_byte *info_ptr,
1430                                                gdb_byte **new_info_ptr,
1431                                                struct die_info *parent);
1432
1433 static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1434                                   struct die_info **, gdb_byte *, int *, int);
1435
1436 static gdb_byte *read_full_die (const struct die_reader_specs *,
1437                                 struct die_info **, gdb_byte *, int *);
1438
1439 static void process_die (struct die_info *, struct dwarf2_cu *);
1440
1441 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1442                                        struct obstack *);
1443
1444 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1445
1446 static const char *dwarf2_full_name (char *name,
1447                                      struct die_info *die,
1448                                      struct dwarf2_cu *cu);
1449
1450 static struct die_info *dwarf2_extension (struct die_info *die,
1451                                           struct dwarf2_cu **);
1452
1453 static const char *dwarf_tag_name (unsigned int);
1454
1455 static const char *dwarf_attr_name (unsigned int);
1456
1457 static const char *dwarf_form_name (unsigned int);
1458
1459 static char *dwarf_bool_name (unsigned int);
1460
1461 static const char *dwarf_type_encoding_name (unsigned int);
1462
1463 static struct die_info *sibling_die (struct die_info *);
1464
1465 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1466
1467 static void dump_die_for_error (struct die_info *);
1468
1469 static void dump_die_1 (struct ui_file *, int level, int max_level,
1470                         struct die_info *);
1471
1472 /*static*/ void dump_die (struct die_info *, int max_level);
1473
1474 static void store_in_ref_table (struct die_info *,
1475                                 struct dwarf2_cu *);
1476
1477 static int is_ref_attr (struct attribute *);
1478
1479 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1480
1481 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1482
1483 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1484                                                struct attribute *,
1485                                                struct dwarf2_cu **);
1486
1487 static struct die_info *follow_die_ref (struct die_info *,
1488                                         struct attribute *,
1489                                         struct dwarf2_cu **);
1490
1491 static struct die_info *follow_die_sig (struct die_info *,
1492                                         struct attribute *,
1493                                         struct dwarf2_cu **);
1494
1495 static struct signatured_type *lookup_signatured_type_at_offset
1496     (struct objfile *objfile,
1497      struct dwarf2_section_info *section, sect_offset offset);
1498
1499 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1500
1501 static void read_signatured_type (struct signatured_type *);
1502
1503 static struct type_unit_group *get_type_unit_group
1504     (struct dwarf2_cu *, struct attribute *);
1505
1506 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1507
1508 /* memory allocation interface */
1509
1510 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1511
1512 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1513
1514 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1515                                  char *, int);
1516
1517 static int attr_form_is_block (struct attribute *);
1518
1519 static int attr_form_is_section_offset (struct attribute *);
1520
1521 static int attr_form_is_constant (struct attribute *);
1522
1523 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1524                                    struct dwarf2_loclist_baton *baton,
1525                                    struct attribute *attr);
1526
1527 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1528                                          struct symbol *sym,
1529                                          struct dwarf2_cu *cu);
1530
1531 static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1532                                gdb_byte *info_ptr,
1533                                struct abbrev_info *abbrev);
1534
1535 static void free_stack_comp_unit (void *);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542   (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1543
1544 static void init_one_comp_unit (struct dwarf2_cu *cu,
1545                                 struct dwarf2_per_cu_data *per_cu);
1546
1547 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1548                                    struct die_info *comp_unit_die,
1549                                    enum language pretend_language);
1550
1551 static void free_heap_comp_unit (void *);
1552
1553 static void free_cached_comp_units (void *);
1554
1555 static void age_cached_comp_units (void);
1556
1557 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1558
1559 static struct type *set_die_type (struct die_info *, struct type *,
1560                                   struct dwarf2_cu *);
1561
1562 static void create_all_comp_units (struct objfile *);
1563
1564 static int create_all_type_units (struct objfile *);
1565
1566 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1567                                  enum language);
1568
1569 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1570                                     enum language);
1571
1572 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1573                                     enum language);
1574
1575 static void dwarf2_add_dependence (struct dwarf2_cu *,
1576                                    struct dwarf2_per_cu_data *);
1577
1578 static void dwarf2_mark (struct dwarf2_cu *);
1579
1580 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1581
1582 static struct type *get_die_type_at_offset (sect_offset,
1583                                             struct dwarf2_per_cu_data *per_cu);
1584
1585 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1586
1587 static void dwarf2_release_queue (void *dummy);
1588
1589 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1590                              enum language pretend_language);
1591
1592 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1593                                   struct dwarf2_per_cu_data *per_cu,
1594                                   enum language pretend_language);
1595
1596 static void process_queue (void);
1597
1598 static void find_file_and_directory (struct die_info *die,
1599                                      struct dwarf2_cu *cu,
1600                                      char **name, char **comp_dir);
1601
1602 static char *file_full_name (int file, struct line_header *lh,
1603                              const char *comp_dir);
1604
1605 static gdb_byte *read_and_check_comp_unit_head
1606   (struct comp_unit_head *header,
1607    struct dwarf2_section_info *section,
1608    struct dwarf2_section_info *abbrev_section, gdb_byte *info_ptr,
1609    int is_debug_types_section);
1610
1611 static void init_cutu_and_read_dies
1612   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1613    int use_existing_cu, int keep,
1614    die_reader_func_ftype *die_reader_func, void *data);
1615
1616 static void init_cutu_and_read_dies_simple
1617   (struct dwarf2_per_cu_data *this_cu,
1618    die_reader_func_ftype *die_reader_func, void *data);
1619
1620 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1621
1622 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1623
1624 static struct dwo_unit *lookup_dwo_comp_unit
1625   (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1626
1627 static struct dwo_unit *lookup_dwo_type_unit
1628   (struct signatured_type *, char *, const char *);
1629
1630 static void free_dwo_file_cleanup (void *);
1631
1632 static void process_cu_includes (void);
1633
1634 #if WORDS_BIGENDIAN
1635
1636 /* Convert VALUE between big- and little-endian.  */
1637 static offset_type
1638 byte_swap (offset_type value)
1639 {
1640   offset_type result;
1641
1642   result = (value & 0xff) << 24;
1643   result |= (value & 0xff00) << 8;
1644   result |= (value & 0xff0000) >> 8;
1645   result |= (value & 0xff000000) >> 24;
1646   return result;
1647 }
1648
1649 #define MAYBE_SWAP(V)  byte_swap (V)
1650
1651 #else
1652 #define MAYBE_SWAP(V) (V)
1653 #endif /* WORDS_BIGENDIAN */
1654
1655 /* The suffix for an index file.  */
1656 #define INDEX_SUFFIX ".gdb-index"
1657
1658 static const char *dwarf2_physname (char *name, struct die_info *die,
1659                                     struct dwarf2_cu *cu);
1660
1661 /* Try to locate the sections we need for DWARF 2 debugging
1662    information and return true if we have enough to do something.
1663    NAMES points to the dwarf2 section names, or is NULL if the standard
1664    ELF names are used.  */
1665
1666 int
1667 dwarf2_has_info (struct objfile *objfile,
1668                  const struct dwarf2_debug_sections *names)
1669 {
1670   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1671   if (!dwarf2_per_objfile)
1672     {
1673       /* Initialize per-objfile state.  */
1674       struct dwarf2_per_objfile *data
1675         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1676
1677       memset (data, 0, sizeof (*data));
1678       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1679       dwarf2_per_objfile = data;
1680
1681       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1682                              (void *) names);
1683       dwarf2_per_objfile->objfile = objfile;
1684     }
1685   return (dwarf2_per_objfile->info.asection != NULL
1686           && dwarf2_per_objfile->abbrev.asection != NULL);
1687 }
1688
1689 /* When loading sections, we look either for uncompressed section or for
1690    compressed section names.  */
1691
1692 static int
1693 section_is_p (const char *section_name,
1694               const struct dwarf2_section_names *names)
1695 {
1696   if (names->normal != NULL
1697       && strcmp (section_name, names->normal) == 0)
1698     return 1;
1699   if (names->compressed != NULL
1700       && strcmp (section_name, names->compressed) == 0)
1701     return 1;
1702   return 0;
1703 }
1704
1705 /* This function is mapped across the sections and remembers the
1706    offset and size of each of the debugging sections we are interested
1707    in.  */
1708
1709 static void
1710 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1711 {
1712   const struct dwarf2_debug_sections *names;
1713
1714   if (vnames == NULL)
1715     names = &dwarf2_elf_names;
1716   else
1717     names = (const struct dwarf2_debug_sections *) vnames;
1718
1719   if (section_is_p (sectp->name, &names->info))
1720     {
1721       dwarf2_per_objfile->info.asection = sectp;
1722       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1723     }
1724   else if (section_is_p (sectp->name, &names->abbrev))
1725     {
1726       dwarf2_per_objfile->abbrev.asection = sectp;
1727       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1728     }
1729   else if (section_is_p (sectp->name, &names->line))
1730     {
1731       dwarf2_per_objfile->line.asection = sectp;
1732       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1733     }
1734   else if (section_is_p (sectp->name, &names->loc))
1735     {
1736       dwarf2_per_objfile->loc.asection = sectp;
1737       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1738     }
1739   else if (section_is_p (sectp->name, &names->macinfo))
1740     {
1741       dwarf2_per_objfile->macinfo.asection = sectp;
1742       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1743     }
1744   else if (section_is_p (sectp->name, &names->macro))
1745     {
1746       dwarf2_per_objfile->macro.asection = sectp;
1747       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1748     }
1749   else if (section_is_p (sectp->name, &names->str))
1750     {
1751       dwarf2_per_objfile->str.asection = sectp;
1752       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1753     }
1754   else if (section_is_p (sectp->name, &names->addr))
1755     {
1756       dwarf2_per_objfile->addr.asection = sectp;
1757       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1758     }
1759   else if (section_is_p (sectp->name, &names->frame))
1760     {
1761       dwarf2_per_objfile->frame.asection = sectp;
1762       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1763     }
1764   else if (section_is_p (sectp->name, &names->eh_frame))
1765     {
1766       flagword aflag = bfd_get_section_flags (abfd, sectp);
1767
1768       if (aflag & SEC_HAS_CONTENTS)
1769         {
1770           dwarf2_per_objfile->eh_frame.asection = sectp;
1771           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1772         }
1773     }
1774   else if (section_is_p (sectp->name, &names->ranges))
1775     {
1776       dwarf2_per_objfile->ranges.asection = sectp;
1777       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1778     }
1779   else if (section_is_p (sectp->name, &names->types))
1780     {
1781       struct dwarf2_section_info type_section;
1782
1783       memset (&type_section, 0, sizeof (type_section));
1784       type_section.asection = sectp;
1785       type_section.size = bfd_get_section_size (sectp);
1786
1787       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1788                      &type_section);
1789     }
1790   else if (section_is_p (sectp->name, &names->gdb_index))
1791     {
1792       dwarf2_per_objfile->gdb_index.asection = sectp;
1793       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1794     }
1795
1796   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1797       && bfd_section_vma (abfd, sectp) == 0)
1798     dwarf2_per_objfile->has_section_at_zero = 1;
1799 }
1800
1801 /* A helper function that decides whether a section is empty,
1802    or not present.  */
1803
1804 static int
1805 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1806 {
1807   return info->asection == NULL || info->size == 0;
1808 }
1809
1810 /* Read the contents of the section INFO.
1811    OBJFILE is the main object file, but not necessarily the file where
1812    the section comes from.  E.g., for DWO files INFO->asection->owner
1813    is the bfd of the DWO file.
1814    If the section is compressed, uncompress it before returning.  */
1815
1816 static void
1817 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1818 {
1819   asection *sectp = info->asection;
1820   bfd *abfd;
1821   gdb_byte *buf, *retbuf;
1822   unsigned char header[4];
1823
1824   if (info->readin)
1825     return;
1826   info->buffer = NULL;
1827   info->readin = 1;
1828
1829   if (dwarf2_section_empty_p (info))
1830     return;
1831
1832   abfd = sectp->owner;
1833
1834   /* If the section has relocations, we must read it ourselves.
1835      Otherwise we attach it to the BFD.  */
1836   if ((sectp->flags & SEC_RELOC) == 0)
1837     {
1838       const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1839
1840       /* We have to cast away const here for historical reasons.
1841          Fixing dwarf2read to be const-correct would be quite nice.  */
1842       info->buffer = (gdb_byte *) bytes;
1843       return;
1844     }
1845
1846   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1847   info->buffer = buf;
1848
1849   /* When debugging .o files, we may need to apply relocations; see
1850      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1851      We never compress sections in .o files, so we only need to
1852      try this when the section is not compressed.  */
1853   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1854   if (retbuf != NULL)
1855     {
1856       info->buffer = retbuf;
1857       return;
1858     }
1859
1860   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1861       || bfd_bread (buf, info->size, abfd) != info->size)
1862     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1863            bfd_get_filename (abfd));
1864 }
1865
1866 /* A helper function that returns the size of a section in a safe way.
1867    If you are positive that the section has been read before using the
1868    size, then it is safe to refer to the dwarf2_section_info object's
1869    "size" field directly.  In other cases, you must call this
1870    function, because for compressed sections the size field is not set
1871    correctly until the section has been read.  */
1872
1873 static bfd_size_type
1874 dwarf2_section_size (struct objfile *objfile,
1875                      struct dwarf2_section_info *info)
1876 {
1877   if (!info->readin)
1878     dwarf2_read_section (objfile, info);
1879   return info->size;
1880 }
1881
1882 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1883    SECTION_NAME.  */
1884
1885 void
1886 dwarf2_get_section_info (struct objfile *objfile,
1887                          enum dwarf2_section_enum sect,
1888                          asection **sectp, gdb_byte **bufp,
1889                          bfd_size_type *sizep)
1890 {
1891   struct dwarf2_per_objfile *data
1892     = objfile_data (objfile, dwarf2_objfile_data_key);
1893   struct dwarf2_section_info *info;
1894
1895   /* We may see an objfile without any DWARF, in which case we just
1896      return nothing.  */
1897   if (data == NULL)
1898     {
1899       *sectp = NULL;
1900       *bufp = NULL;
1901       *sizep = 0;
1902       return;
1903     }
1904   switch (sect)
1905     {
1906     case DWARF2_DEBUG_FRAME:
1907       info = &data->frame;
1908       break;
1909     case DWARF2_EH_FRAME:
1910       info = &data->eh_frame;
1911       break;
1912     default:
1913       gdb_assert_not_reached ("unexpected section");
1914     }
1915
1916   dwarf2_read_section (objfile, info);
1917
1918   *sectp = info->asection;
1919   *bufp = info->buffer;
1920   *sizep = info->size;
1921 }
1922
1923 /* A helper function to find the sections for a .dwz file.  */
1924
1925 static void
1926 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1927 {
1928   struct dwz_file *dwz_file = arg;
1929
1930   /* Note that we only support the standard ELF names, because .dwz
1931      is ELF-only (at the time of writing).  */
1932   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1933     {
1934       dwz_file->abbrev.asection = sectp;
1935       dwz_file->abbrev.size = bfd_get_section_size (sectp);
1936     }
1937   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1938     {
1939       dwz_file->info.asection = sectp;
1940       dwz_file->info.size = bfd_get_section_size (sectp);
1941     }
1942   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1943     {
1944       dwz_file->str.asection = sectp;
1945       dwz_file->str.size = bfd_get_section_size (sectp);
1946     }
1947   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1948     {
1949       dwz_file->line.asection = sectp;
1950       dwz_file->line.size = bfd_get_section_size (sectp);
1951     }
1952   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1953     {
1954       dwz_file->macro.asection = sectp;
1955       dwz_file->macro.size = bfd_get_section_size (sectp);
1956     }
1957   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1958     {
1959       dwz_file->gdb_index.asection = sectp;
1960       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1961     }
1962 }
1963
1964 /* Open the separate '.dwz' debug file, if needed.  Error if the file
1965    cannot be found.  */
1966
1967 static struct dwz_file *
1968 dwarf2_get_dwz_file (void)
1969 {
1970   bfd *abfd, *dwz_bfd;
1971   asection *section;
1972   gdb_byte *data;
1973   struct cleanup *cleanup;
1974   const char *filename;
1975   struct dwz_file *result;
1976
1977   if (dwarf2_per_objfile->dwz_file != NULL)
1978     return dwarf2_per_objfile->dwz_file;
1979
1980   abfd = dwarf2_per_objfile->objfile->obfd;
1981   section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1982   if (section == NULL)
1983     error (_("could not find '.gnu_debugaltlink' section"));
1984   if (!bfd_malloc_and_get_section (abfd, section, &data))
1985     error (_("could not read '.gnu_debugaltlink' section: %s"),
1986            bfd_errmsg (bfd_get_error ()));
1987   cleanup = make_cleanup (xfree, data);
1988
1989   filename = data;
1990   if (!IS_ABSOLUTE_PATH (filename))
1991     {
1992       char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
1993       char *rel;
1994
1995       make_cleanup (xfree, abs);
1996       abs = ldirname (abs);
1997       make_cleanup (xfree, abs);
1998
1999       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2000       make_cleanup (xfree, rel);
2001       filename = rel;
2002     }
2003
2004   /* The format is just a NUL-terminated file name, followed by the
2005      build-id.  For now, though, we ignore the build-id.  */
2006   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2007   if (dwz_bfd == NULL)
2008     error (_("could not read '%s': %s"), filename,
2009            bfd_errmsg (bfd_get_error ()));
2010
2011   if (!bfd_check_format (dwz_bfd, bfd_object))
2012     {
2013       gdb_bfd_unref (dwz_bfd);
2014       error (_("file '%s' was not usable: %s"), filename,
2015              bfd_errmsg (bfd_get_error ()));
2016     }
2017
2018   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2019                            struct dwz_file);
2020   result->dwz_bfd = dwz_bfd;
2021
2022   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2023
2024   do_cleanups (cleanup);
2025
2026   return result;
2027 }
2028 \f
2029 /* DWARF quick_symbols_functions support.  */
2030
2031 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2032    unique line tables, so we maintain a separate table of all .debug_line
2033    derived entries to support the sharing.
2034    All the quick functions need is the list of file names.  We discard the
2035    line_header when we're done and don't need to record it here.  */
2036 struct quick_file_names
2037 {
2038   /* The data used to construct the hash key.  */
2039   struct stmt_list_hash hash;
2040
2041   /* The number of entries in file_names, real_names.  */
2042   unsigned int num_file_names;
2043
2044   /* The file names from the line table, after being run through
2045      file_full_name.  */
2046   const char **file_names;
2047
2048   /* The file names from the line table after being run through
2049      gdb_realpath.  These are computed lazily.  */
2050   const char **real_names;
2051 };
2052
2053 /* When using the index (and thus not using psymtabs), each CU has an
2054    object of this type.  This is used to hold information needed by
2055    the various "quick" methods.  */
2056 struct dwarf2_per_cu_quick_data
2057 {
2058   /* The file table.  This can be NULL if there was no file table
2059      or it's currently not read in.
2060      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
2061   struct quick_file_names *file_names;
2062
2063   /* The corresponding symbol table.  This is NULL if symbols for this
2064      CU have not yet been read.  */
2065   struct symtab *symtab;
2066
2067   /* A temporary mark bit used when iterating over all CUs in
2068      expand_symtabs_matching.  */
2069   unsigned int mark : 1;
2070
2071   /* True if we've tried to read the file table and found there isn't one.
2072      There will be no point in trying to read it again next time.  */
2073   unsigned int no_file_data : 1;
2074 };
2075
2076 /* Utility hash function for a stmt_list_hash.  */
2077
2078 static hashval_t
2079 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2080 {
2081   hashval_t v = 0;
2082
2083   if (stmt_list_hash->dwo_unit != NULL)
2084     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2085   v += stmt_list_hash->line_offset.sect_off;
2086   return v;
2087 }
2088
2089 /* Utility equality function for a stmt_list_hash.  */
2090
2091 static int
2092 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2093                     const struct stmt_list_hash *rhs)
2094 {
2095   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2096     return 0;
2097   if (lhs->dwo_unit != NULL
2098       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2099     return 0;
2100
2101   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2102 }
2103
2104 /* Hash function for a quick_file_names.  */
2105
2106 static hashval_t
2107 hash_file_name_entry (const void *e)
2108 {
2109   const struct quick_file_names *file_data = e;
2110
2111   return hash_stmt_list_entry (&file_data->hash);
2112 }
2113
2114 /* Equality function for a quick_file_names.  */
2115
2116 static int
2117 eq_file_name_entry (const void *a, const void *b)
2118 {
2119   const struct quick_file_names *ea = a;
2120   const struct quick_file_names *eb = b;
2121
2122   return eq_stmt_list_entry (&ea->hash, &eb->hash);
2123 }
2124
2125 /* Delete function for a quick_file_names.  */
2126
2127 static void
2128 delete_file_name_entry (void *e)
2129 {
2130   struct quick_file_names *file_data = e;
2131   int i;
2132
2133   for (i = 0; i < file_data->num_file_names; ++i)
2134     {
2135       xfree ((void*) file_data->file_names[i]);
2136       if (file_data->real_names)
2137         xfree ((void*) file_data->real_names[i]);
2138     }
2139
2140   /* The space for the struct itself lives on objfile_obstack,
2141      so we don't free it here.  */
2142 }
2143
2144 /* Create a quick_file_names hash table.  */
2145
2146 static htab_t
2147 create_quick_file_names_table (unsigned int nr_initial_entries)
2148 {
2149   return htab_create_alloc (nr_initial_entries,
2150                             hash_file_name_entry, eq_file_name_entry,
2151                             delete_file_name_entry, xcalloc, xfree);
2152 }
2153
2154 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
2155    have to be created afterwards.  You should call age_cached_comp_units after
2156    processing PER_CU->CU.  dw2_setup must have been already called.  */
2157
2158 static void
2159 load_cu (struct dwarf2_per_cu_data *per_cu)
2160 {
2161   if (per_cu->is_debug_types)
2162     load_full_type_unit (per_cu);
2163   else
2164     load_full_comp_unit (per_cu, language_minimal);
2165
2166   gdb_assert (per_cu->cu != NULL);
2167
2168   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2169 }
2170
2171 /* Read in the symbols for PER_CU.  */
2172
2173 static void
2174 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2175 {
2176   struct cleanup *back_to;
2177
2178   /* Skip type_unit_groups, reading the type units they contain
2179      is handled elsewhere.  */
2180   if (IS_TYPE_UNIT_GROUP (per_cu))
2181     return;
2182
2183   back_to = make_cleanup (dwarf2_release_queue, NULL);
2184
2185   if (dwarf2_per_objfile->using_index
2186       ? per_cu->v.quick->symtab == NULL
2187       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2188     {
2189       queue_comp_unit (per_cu, language_minimal);
2190       load_cu (per_cu);
2191     }
2192
2193   process_queue ();
2194
2195   /* Age the cache, releasing compilation units that have not
2196      been used recently.  */
2197   age_cached_comp_units ();
2198
2199   do_cleanups (back_to);
2200 }
2201
2202 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
2203    the objfile from which this CU came.  Returns the resulting symbol
2204    table.  */
2205
2206 static struct symtab *
2207 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2208 {
2209   gdb_assert (dwarf2_per_objfile->using_index);
2210   if (!per_cu->v.quick->symtab)
2211     {
2212       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2213       increment_reading_symtab ();
2214       dw2_do_instantiate_symtab (per_cu);
2215       process_cu_includes ();
2216       do_cleanups (back_to);
2217     }
2218   return per_cu->v.quick->symtab;
2219 }
2220
2221 /* Return the CU given its index.
2222
2223    This is intended for loops like:
2224
2225    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2226                     + dwarf2_per_objfile->n_type_units); ++i)
2227      {
2228        struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2229
2230        ...;
2231      }
2232 */
2233
2234 static struct dwarf2_per_cu_data *
2235 dw2_get_cu (int index)
2236 {
2237   if (index >= dwarf2_per_objfile->n_comp_units)
2238     {
2239       index -= dwarf2_per_objfile->n_comp_units;
2240       gdb_assert (index < dwarf2_per_objfile->n_type_units);
2241       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2242     }
2243
2244   return dwarf2_per_objfile->all_comp_units[index];
2245 }
2246
2247 /* Return the primary CU given its index.
2248    The difference between this function and dw2_get_cu is in the handling
2249    of type units (TUs).  Here we return the type_unit_group object.
2250
2251    This is intended for loops like:
2252
2253    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2254                     + dwarf2_per_objfile->n_type_unit_groups); ++i)
2255      {
2256        struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2257
2258        ...;
2259      }
2260 */
2261
2262 static struct dwarf2_per_cu_data *
2263 dw2_get_primary_cu (int index)
2264 {
2265   if (index >= dwarf2_per_objfile->n_comp_units)
2266     {
2267       index -= dwarf2_per_objfile->n_comp_units;
2268       gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2269       return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2270     }
2271
2272   return dwarf2_per_objfile->all_comp_units[index];
2273 }
2274
2275 /* A helper function that knows how to read a 64-bit value in a way
2276    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
2277    otherwise.  */
2278
2279 static int
2280 extract_cu_value (const char *bytes, ULONGEST *result)
2281 {
2282   if (sizeof (ULONGEST) < 8)
2283     {
2284       int i;
2285
2286       /* Ignore the upper 4 bytes if they are all zero.  */
2287       for (i = 0; i < 4; ++i)
2288         if (bytes[i + 4] != 0)
2289           return 0;
2290
2291       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2292     }
2293   else
2294     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2295   return 1;
2296 }
2297
2298 /* A helper for create_cus_from_index that handles a given list of
2299    CUs.  */
2300
2301 static int
2302 create_cus_from_index_list (struct objfile *objfile,
2303                             const gdb_byte *cu_list, offset_type n_elements,
2304                             struct dwarf2_section_info *section,
2305                             int is_dwz,
2306                             int base_offset)
2307 {
2308   offset_type i;
2309
2310   for (i = 0; i < n_elements; i += 2)
2311     {
2312       struct dwarf2_per_cu_data *the_cu;
2313       ULONGEST offset, length;
2314
2315       if (!extract_cu_value (cu_list, &offset)
2316           || !extract_cu_value (cu_list + 8, &length))
2317         return 0;
2318       cu_list += 2 * 8;
2319
2320       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2321                                struct dwarf2_per_cu_data);
2322       the_cu->offset.sect_off = offset;
2323       the_cu->length = length;
2324       the_cu->objfile = objfile;
2325       the_cu->info_or_types_section = section;
2326       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2327                                         struct dwarf2_per_cu_quick_data);
2328       the_cu->is_dwz = is_dwz;
2329       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2330     }
2331
2332   return 1;
2333 }
2334
2335 /* Read the CU list from the mapped index, and use it to create all
2336    the CU objects for this objfile.  Return 0 if something went wrong,
2337    1 if everything went ok.  */
2338
2339 static int
2340 create_cus_from_index (struct objfile *objfile,
2341                        const gdb_byte *cu_list, offset_type cu_list_elements,
2342                        const gdb_byte *dwz_list, offset_type dwz_elements)
2343 {
2344   struct dwz_file *dwz;
2345
2346   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2347   dwarf2_per_objfile->all_comp_units
2348     = obstack_alloc (&objfile->objfile_obstack,
2349                      dwarf2_per_objfile->n_comp_units
2350                      * sizeof (struct dwarf2_per_cu_data *));
2351
2352   if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2353                                    &dwarf2_per_objfile->info, 0, 0))
2354     return 0;
2355
2356   if (dwz_elements == 0)
2357     return 1;
2358
2359   dwz = dwarf2_get_dwz_file ();
2360   return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2361                                      &dwz->info, 1, cu_list_elements / 2);
2362 }
2363
2364 /* Create the signatured type hash table from the index.  */
2365
2366 static int
2367 create_signatured_type_table_from_index (struct objfile *objfile,
2368                                          struct dwarf2_section_info *section,
2369                                          const gdb_byte *bytes,
2370                                          offset_type elements)
2371 {
2372   offset_type i;
2373   htab_t sig_types_hash;
2374
2375   dwarf2_per_objfile->n_type_units = elements / 3;
2376   dwarf2_per_objfile->all_type_units
2377     = obstack_alloc (&objfile->objfile_obstack,
2378                      dwarf2_per_objfile->n_type_units
2379                      * sizeof (struct signatured_type *));
2380
2381   sig_types_hash = allocate_signatured_type_table (objfile);
2382
2383   for (i = 0; i < elements; i += 3)
2384     {
2385       struct signatured_type *sig_type;
2386       ULONGEST offset, type_offset_in_tu, signature;
2387       void **slot;
2388
2389       if (!extract_cu_value (bytes, &offset)
2390           || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2391         return 0;
2392       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2393       bytes += 3 * 8;
2394
2395       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2396                                  struct signatured_type);
2397       sig_type->signature = signature;
2398       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2399       sig_type->per_cu.is_debug_types = 1;
2400       sig_type->per_cu.info_or_types_section = section;
2401       sig_type->per_cu.offset.sect_off = offset;
2402       sig_type->per_cu.objfile = objfile;
2403       sig_type->per_cu.v.quick
2404         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2405                           struct dwarf2_per_cu_quick_data);
2406
2407       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2408       *slot = sig_type;
2409
2410       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2411     }
2412
2413   dwarf2_per_objfile->signatured_types = sig_types_hash;
2414
2415   return 1;
2416 }
2417
2418 /* Read the address map data from the mapped index, and use it to
2419    populate the objfile's psymtabs_addrmap.  */
2420
2421 static void
2422 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2423 {
2424   const gdb_byte *iter, *end;
2425   struct obstack temp_obstack;
2426   struct addrmap *mutable_map;
2427   struct cleanup *cleanup;
2428   CORE_ADDR baseaddr;
2429
2430   obstack_init (&temp_obstack);
2431   cleanup = make_cleanup_obstack_free (&temp_obstack);
2432   mutable_map = addrmap_create_mutable (&temp_obstack);
2433
2434   iter = index->address_table;
2435   end = iter + index->address_table_size;
2436
2437   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2438
2439   while (iter < end)
2440     {
2441       ULONGEST hi, lo, cu_index;
2442       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2443       iter += 8;
2444       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2445       iter += 8;
2446       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2447       iter += 4;
2448       
2449       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2450                          dw2_get_cu (cu_index));
2451     }
2452
2453   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2454                                                     &objfile->objfile_obstack);
2455   do_cleanups (cleanup);
2456 }
2457
2458 /* The hash function for strings in the mapped index.  This is the same as
2459    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2460    implementation.  This is necessary because the hash function is tied to the
2461    format of the mapped index file.  The hash values do not have to match with
2462    SYMBOL_HASH_NEXT.
2463    
2464    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2465
2466 static hashval_t
2467 mapped_index_string_hash (int index_version, const void *p)
2468 {
2469   const unsigned char *str = (const unsigned char *) p;
2470   hashval_t r = 0;
2471   unsigned char c;
2472
2473   while ((c = *str++) != 0)
2474     {
2475       if (index_version >= 5)
2476         c = tolower (c);
2477       r = r * 67 + c - 113;
2478     }
2479
2480   return r;
2481 }
2482
2483 /* Find a slot in the mapped index INDEX for the object named NAME.
2484    If NAME is found, set *VEC_OUT to point to the CU vector in the
2485    constant pool and return 1.  If NAME cannot be found, return 0.  */
2486
2487 static int
2488 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2489                           offset_type **vec_out)
2490 {
2491   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2492   offset_type hash;
2493   offset_type slot, step;
2494   int (*cmp) (const char *, const char *);
2495
2496   if (current_language->la_language == language_cplus
2497       || current_language->la_language == language_java
2498       || current_language->la_language == language_fortran)
2499     {
2500       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2501          not contain any.  */
2502       const char *paren = strchr (name, '(');
2503
2504       if (paren)
2505         {
2506           char *dup;
2507
2508           dup = xmalloc (paren - name + 1);
2509           memcpy (dup, name, paren - name);
2510           dup[paren - name] = 0;
2511
2512           make_cleanup (xfree, dup);
2513           name = dup;
2514         }
2515     }
2516
2517   /* Index version 4 did not support case insensitive searches.  But the
2518      indices for case insensitive languages are built in lowercase, therefore
2519      simulate our NAME being searched is also lowercased.  */
2520   hash = mapped_index_string_hash ((index->version == 4
2521                                     && case_sensitivity == case_sensitive_off
2522                                     ? 5 : index->version),
2523                                    name);
2524
2525   slot = hash & (index->symbol_table_slots - 1);
2526   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2527   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2528
2529   for (;;)
2530     {
2531       /* Convert a slot number to an offset into the table.  */
2532       offset_type i = 2 * slot;
2533       const char *str;
2534       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2535         {
2536           do_cleanups (back_to);
2537           return 0;
2538         }
2539
2540       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2541       if (!cmp (name, str))
2542         {
2543           *vec_out = (offset_type *) (index->constant_pool
2544                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2545           do_cleanups (back_to);
2546           return 1;
2547         }
2548
2549       slot = (slot + step) & (index->symbol_table_slots - 1);
2550     }
2551 }
2552
2553 /* A helper function that reads the .gdb_index from SECTION and fills
2554    in MAP.  FILENAME is the name of the file containing the section;
2555    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
2556    ok to use deprecated sections.
2557
2558    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2559    out parameters that are filled in with information about the CU and
2560    TU lists in the section.
2561
2562    Returns 1 if all went well, 0 otherwise.  */
2563
2564 static int
2565 read_index_from_section (struct objfile *objfile,
2566                          const char *filename,
2567                          int deprecated_ok,
2568                          struct dwarf2_section_info *section,
2569                          struct mapped_index *map,
2570                          const gdb_byte **cu_list,
2571                          offset_type *cu_list_elements,
2572                          const gdb_byte **types_list,
2573                          offset_type *types_list_elements)
2574 {
2575   char *addr;
2576   offset_type version;
2577   offset_type *metadata;
2578   int i;
2579
2580   if (dwarf2_section_empty_p (section))
2581     return 0;
2582
2583   /* Older elfutils strip versions could keep the section in the main
2584      executable while splitting it for the separate debug info file.  */
2585   if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2586     return 0;
2587
2588   dwarf2_read_section (objfile, section);
2589
2590   addr = section->buffer;
2591   /* Version check.  */
2592   version = MAYBE_SWAP (*(offset_type *) addr);
2593   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2594      causes the index to behave very poorly for certain requests.  Version 3
2595      contained incomplete addrmap.  So, it seems better to just ignore such
2596      indices.  */
2597   if (version < 4)
2598     {
2599       static int warning_printed = 0;
2600       if (!warning_printed)
2601         {
2602           warning (_("Skipping obsolete .gdb_index section in %s."),
2603                    filename);
2604           warning_printed = 1;
2605         }
2606       return 0;
2607     }
2608   /* Index version 4 uses a different hash function than index version
2609      5 and later.
2610
2611      Versions earlier than 6 did not emit psymbols for inlined
2612      functions.  Using these files will cause GDB not to be able to
2613      set breakpoints on inlined functions by name, so we ignore these
2614      indices unless the user has done
2615      "set use-deprecated-index-sections on".  */
2616   if (version < 6 && !deprecated_ok)
2617     {
2618       static int warning_printed = 0;
2619       if (!warning_printed)
2620         {
2621           warning (_("\
2622 Skipping deprecated .gdb_index section in %s.\n\
2623 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2624 to use the section anyway."),
2625                    filename);
2626           warning_printed = 1;
2627         }
2628       return 0;
2629     }
2630   /* Indexes with higher version than the one supported by GDB may be no
2631      longer backward compatible.  */
2632   if (version > 7)
2633     return 0;
2634
2635   map->version = version;
2636   map->total_size = section->size;
2637
2638   metadata = (offset_type *) (addr + sizeof (offset_type));
2639
2640   i = 0;
2641   *cu_list = addr + MAYBE_SWAP (metadata[i]);
2642   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2643                        / 8);
2644   ++i;
2645
2646   *types_list = addr + MAYBE_SWAP (metadata[i]);
2647   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2648                            - MAYBE_SWAP (metadata[i]))
2649                           / 8);
2650   ++i;
2651
2652   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2653   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2654                              - MAYBE_SWAP (metadata[i]));
2655   ++i;
2656
2657   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2658   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2659                               - MAYBE_SWAP (metadata[i]))
2660                              / (2 * sizeof (offset_type)));
2661   ++i;
2662
2663   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2664
2665   return 1;
2666 }
2667
2668
2669 /* Read the index file.  If everything went ok, initialize the "quick"
2670    elements of all the CUs and return 1.  Otherwise, return 0.  */
2671
2672 static int
2673 dwarf2_read_index (struct objfile *objfile)
2674 {
2675   struct mapped_index local_map, *map;
2676   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2677   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2678
2679   if (!read_index_from_section (objfile, objfile->name,
2680                                 use_deprecated_index_sections,
2681                                 &dwarf2_per_objfile->gdb_index, &local_map,
2682                                 &cu_list, &cu_list_elements,
2683                                 &types_list, &types_list_elements))
2684     return 0;
2685
2686   /* Don't use the index if it's empty.  */
2687   if (local_map.symbol_table_slots == 0)
2688     return 0;
2689
2690   /* If there is a .dwz file, read it so we can get its CU list as
2691      well.  */
2692   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2693     {
2694       struct dwz_file *dwz = dwarf2_get_dwz_file ();
2695       struct mapped_index dwz_map;
2696       const gdb_byte *dwz_types_ignore;
2697       offset_type dwz_types_elements_ignore;
2698
2699       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2700                                     1,
2701                                     &dwz->gdb_index, &dwz_map,
2702                                     &dwz_list, &dwz_list_elements,
2703                                     &dwz_types_ignore,
2704                                     &dwz_types_elements_ignore))
2705         {
2706           warning (_("could not read '.gdb_index' section from %s; skipping"),
2707                    bfd_get_filename (dwz->dwz_bfd));
2708           return 0;
2709         }
2710     }
2711
2712   if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2713                               dwz_list, dwz_list_elements))
2714     return 0;
2715
2716   if (types_list_elements)
2717     {
2718       struct dwarf2_section_info *section;
2719
2720       /* We can only handle a single .debug_types when we have an
2721          index.  */
2722       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2723         return 0;
2724
2725       section = VEC_index (dwarf2_section_info_def,
2726                            dwarf2_per_objfile->types, 0);
2727
2728       if (!create_signatured_type_table_from_index (objfile, section,
2729                                                     types_list,
2730                                                     types_list_elements))
2731         return 0;
2732     }
2733
2734   create_addrmap_from_index (objfile, &local_map);
2735
2736   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2737   *map = local_map;
2738
2739   dwarf2_per_objfile->index_table = map;
2740   dwarf2_per_objfile->using_index = 1;
2741   dwarf2_per_objfile->quick_file_names_table =
2742     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2743
2744   return 1;
2745 }
2746
2747 /* A helper for the "quick" functions which sets the global
2748    dwarf2_per_objfile according to OBJFILE.  */
2749
2750 static void
2751 dw2_setup (struct objfile *objfile)
2752 {
2753   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2754   gdb_assert (dwarf2_per_objfile);
2755 }
2756
2757 /* Reader function for dw2_build_type_unit_groups.  */
2758
2759 static void
2760 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2761                                    gdb_byte *info_ptr,
2762                                    struct die_info *type_unit_die,
2763                                    int has_children,
2764                                    void *data)
2765 {
2766   struct dwarf2_cu *cu = reader->cu;
2767   struct attribute *attr;
2768   struct type_unit_group *tu_group;
2769
2770   gdb_assert (data == NULL);
2771
2772   if (! has_children)
2773     return;
2774
2775   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2776   /* Call this for its side-effect of creating the associated
2777      struct type_unit_group if it doesn't already exist.  */
2778   tu_group = get_type_unit_group (cu, attr);
2779 }
2780
2781 /* Build dwarf2_per_objfile->type_unit_groups.
2782    This function may be called multiple times.  */
2783
2784 static void
2785 dw2_build_type_unit_groups (void)
2786 {
2787   if (dwarf2_per_objfile->type_unit_groups == NULL)
2788     build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2789 }
2790
2791 /* die_reader_func for dw2_get_file_names.  */
2792
2793 static void
2794 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2795                            gdb_byte *info_ptr,
2796                            struct die_info *comp_unit_die,
2797                            int has_children,
2798                            void *data)
2799 {
2800   struct dwarf2_cu *cu = reader->cu;
2801   struct dwarf2_per_cu_data *this_cu = cu->per_cu;  
2802   struct objfile *objfile = dwarf2_per_objfile->objfile;
2803   struct dwarf2_per_cu_data *lh_cu;
2804   struct line_header *lh;
2805   struct attribute *attr;
2806   int i;
2807   char *name, *comp_dir;
2808   void **slot;
2809   struct quick_file_names *qfn;
2810   unsigned int line_offset;
2811
2812   /* Our callers never want to match partial units -- instead they
2813      will match the enclosing full CU.  */
2814   if (comp_unit_die->tag == DW_TAG_partial_unit)
2815     {
2816       this_cu->v.quick->no_file_data = 1;
2817       return;
2818     }
2819
2820   /* If we're reading the line header for TUs, store it in the "per_cu"
2821      for tu_group.  */
2822   if (this_cu->is_debug_types)
2823     {
2824       struct type_unit_group *tu_group = data;
2825
2826       gdb_assert (tu_group != NULL);
2827       lh_cu = &tu_group->per_cu;
2828     }
2829   else
2830     lh_cu = this_cu;
2831
2832   lh = NULL;
2833   slot = NULL;
2834   line_offset = 0;
2835
2836   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2837   if (attr)
2838     {
2839       struct quick_file_names find_entry;
2840
2841       line_offset = DW_UNSND (attr);
2842
2843       /* We may have already read in this line header (TU line header sharing).
2844          If we have we're done.  */
2845       find_entry.hash.dwo_unit = cu->dwo_unit;
2846       find_entry.hash.line_offset.sect_off = line_offset;
2847       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2848                              &find_entry, INSERT);
2849       if (*slot != NULL)
2850         {
2851           lh_cu->v.quick->file_names = *slot;
2852           return;
2853         }
2854
2855       lh = dwarf_decode_line_header (line_offset, cu);
2856     }
2857   if (lh == NULL)
2858     {
2859       lh_cu->v.quick->no_file_data = 1;
2860       return;
2861     }
2862
2863   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2864   qfn->hash.dwo_unit = cu->dwo_unit;
2865   qfn->hash.line_offset.sect_off = line_offset;
2866   gdb_assert (slot != NULL);
2867   *slot = qfn;
2868
2869   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2870
2871   qfn->num_file_names = lh->num_file_names;
2872   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2873                                    lh->num_file_names * sizeof (char *));
2874   for (i = 0; i < lh->num_file_names; ++i)
2875     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2876   qfn->real_names = NULL;
2877
2878   free_line_header (lh);
2879
2880   lh_cu->v.quick->file_names = qfn;
2881 }
2882
2883 /* A helper for the "quick" functions which attempts to read the line
2884    table for THIS_CU.  */
2885
2886 static struct quick_file_names *
2887 dw2_get_file_names (struct objfile *objfile,
2888                     struct dwarf2_per_cu_data *this_cu)
2889 {
2890   /* For TUs this should only be called on the parent group.  */
2891   if (this_cu->is_debug_types)
2892     gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2893
2894   if (this_cu->v.quick->file_names != NULL)
2895     return this_cu->v.quick->file_names;
2896   /* If we know there is no line data, no point in looking again.  */
2897   if (this_cu->v.quick->no_file_data)
2898     return NULL;
2899
2900   /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2901      in the stub for CUs, there's is no need to lookup the DWO file.
2902      However, that's not the case for TUs where DW_AT_stmt_list lives in the
2903      DWO file.  */
2904   if (this_cu->is_debug_types)
2905     {
2906       struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2907
2908       init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2909                                dw2_get_file_names_reader, tu_group);
2910     }
2911   else
2912     init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2913
2914   if (this_cu->v.quick->no_file_data)
2915     return NULL;
2916   return this_cu->v.quick->file_names;
2917 }
2918
2919 /* A helper for the "quick" functions which computes and caches the
2920    real path for a given file name from the line table.  */
2921
2922 static const char *
2923 dw2_get_real_path (struct objfile *objfile,
2924                    struct quick_file_names *qfn, int index)
2925 {
2926   if (qfn->real_names == NULL)
2927     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2928                                       qfn->num_file_names, sizeof (char *));
2929
2930   if (qfn->real_names[index] == NULL)
2931     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2932
2933   return qfn->real_names[index];
2934 }
2935
2936 static struct symtab *
2937 dw2_find_last_source_symtab (struct objfile *objfile)
2938 {
2939   int index;
2940
2941   dw2_setup (objfile);
2942   index = dwarf2_per_objfile->n_comp_units - 1;
2943   return dw2_instantiate_symtab (dw2_get_cu (index));
2944 }
2945
2946 /* Traversal function for dw2_forget_cached_source_info.  */
2947
2948 static int
2949 dw2_free_cached_file_names (void **slot, void *info)
2950 {
2951   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2952
2953   if (file_data->real_names)
2954     {
2955       int i;
2956
2957       for (i = 0; i < file_data->num_file_names; ++i)
2958         {
2959           xfree ((void*) file_data->real_names[i]);
2960           file_data->real_names[i] = NULL;
2961         }
2962     }
2963
2964   return 1;
2965 }
2966
2967 static void
2968 dw2_forget_cached_source_info (struct objfile *objfile)
2969 {
2970   dw2_setup (objfile);
2971
2972   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2973                           dw2_free_cached_file_names, NULL);
2974 }
2975
2976 /* Helper function for dw2_map_symtabs_matching_filename that expands
2977    the symtabs and calls the iterator.  */
2978
2979 static int
2980 dw2_map_expand_apply (struct objfile *objfile,
2981                       struct dwarf2_per_cu_data *per_cu,
2982                       const char *name,
2983                       const char *full_path, const char *real_path,
2984                       int (*callback) (struct symtab *, void *),
2985                       void *data)
2986 {
2987   struct symtab *last_made = objfile->symtabs;
2988
2989   /* Don't visit already-expanded CUs.  */
2990   if (per_cu->v.quick->symtab)
2991     return 0;
2992
2993   /* This may expand more than one symtab, and we want to iterate over
2994      all of them.  */
2995   dw2_instantiate_symtab (per_cu);
2996
2997   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2998                                     objfile->symtabs, last_made);
2999 }
3000
3001 /* Implementation of the map_symtabs_matching_filename method.  */
3002
3003 static int
3004 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3005                                    const char *full_path, const char *real_path,
3006                                    int (*callback) (struct symtab *, void *),
3007                                    void *data)
3008 {
3009   int i;
3010   const char *name_basename = lbasename (name);
3011   int name_len = strlen (name);
3012   int is_abs = IS_ABSOLUTE_PATH (name);
3013
3014   dw2_setup (objfile);
3015
3016   dw2_build_type_unit_groups ();
3017
3018   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3019                    + dwarf2_per_objfile->n_type_unit_groups); ++i)
3020     {
3021       int j;
3022       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3023       struct quick_file_names *file_data;
3024
3025       /* We only need to look at symtabs not already expanded.  */
3026       if (per_cu->v.quick->symtab)
3027         continue;
3028
3029       file_data = dw2_get_file_names (objfile, per_cu);
3030       if (file_data == NULL)
3031         continue;
3032
3033       for (j = 0; j < file_data->num_file_names; ++j)
3034         {
3035           const char *this_name = file_data->file_names[j];
3036
3037           if (FILENAME_CMP (name, this_name) == 0
3038               || (!is_abs && compare_filenames_for_search (this_name,
3039                                                            name, name_len)))
3040             {
3041               if (dw2_map_expand_apply (objfile, per_cu,
3042                                         name, full_path, real_path,
3043                                         callback, data))
3044                 return 1;
3045             }
3046
3047           /* Before we invoke realpath, which can get expensive when many
3048              files are involved, do a quick comparison of the basenames.  */
3049           if (! basenames_may_differ
3050               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3051             continue;
3052
3053           if (full_path != NULL)
3054             {
3055               const char *this_real_name = dw2_get_real_path (objfile,
3056                                                               file_data, j);
3057
3058               if (this_real_name != NULL
3059                   && (FILENAME_CMP (full_path, this_real_name) == 0
3060                       || (!is_abs
3061                           && compare_filenames_for_search (this_real_name,
3062                                                            name, name_len))))
3063                 {
3064                   if (dw2_map_expand_apply (objfile, per_cu,
3065                                             name, full_path, real_path,
3066                                             callback, data))
3067                     return 1;
3068                 }
3069             }
3070
3071           if (real_path != NULL)
3072             {
3073               const char *this_real_name = dw2_get_real_path (objfile,
3074                                                               file_data, j);
3075
3076               if (this_real_name != NULL
3077                   && (FILENAME_CMP (real_path, this_real_name) == 0
3078                       || (!is_abs
3079                           && compare_filenames_for_search (this_real_name,
3080                                                            name, name_len))))
3081                 {
3082                   if (dw2_map_expand_apply (objfile, per_cu,
3083                                             name, full_path, real_path,
3084                                             callback, data))
3085                     return 1;
3086                 }
3087             }
3088         }
3089     }
3090
3091   return 0;
3092 }
3093
3094 static struct symtab *
3095 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3096                    const char *name, domain_enum domain)
3097 {
3098   /* We do all the work in the pre_expand_symtabs_matching hook
3099      instead.  */
3100   return NULL;
3101 }
3102
3103 /* A helper function that expands all symtabs that hold an object
3104    named NAME.  If WANT_SPECIFIC_BLOCK is non-zero, only look for
3105    symbols in block BLOCK_KIND.  */
3106
3107 static void
3108 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3109                                 int want_specific_block,
3110                                 enum block_enum block_kind,
3111                                 const char *name, domain_enum domain)
3112 {
3113   struct mapped_index *index;
3114
3115   dw2_setup (objfile);
3116
3117   index = dwarf2_per_objfile->index_table;
3118
3119   /* index_table is NULL if OBJF_READNOW.  */
3120   if (index)
3121     {
3122       offset_type *vec;
3123
3124       if (find_slot_in_mapped_hash (index, name, &vec))
3125         {
3126           offset_type i, len = MAYBE_SWAP (*vec);
3127           for (i = 0; i < len; ++i)
3128             {
3129               offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3130               offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3131               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3132               int want_static = block_kind != GLOBAL_BLOCK;
3133               /* This value is only valid for index versions >= 7.  */
3134               int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3135               gdb_index_symbol_kind symbol_kind =
3136                 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3137
3138               if (want_specific_block
3139                   && index->version >= 7
3140                   && want_static != is_static)
3141                 continue;
3142
3143               /* Only check the symbol's kind if it has one.
3144                  Indices prior to version 7 don't record it.  */
3145               if (index->version >= 7)
3146                 {
3147                   switch (domain)
3148                     {
3149                     case VAR_DOMAIN:
3150                       if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3151                           && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3152                           /* Some types are also in VAR_DOMAIN.  */
3153                           && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3154                         continue;
3155                       break;
3156                     case STRUCT_DOMAIN:
3157                       if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3158                         continue;
3159                       break;
3160                     case LABEL_DOMAIN:
3161                       if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3162                         continue;
3163                       break;
3164                     default:
3165                       break;
3166                     }
3167                 }
3168
3169               dw2_instantiate_symtab (per_cu);
3170             }
3171         }
3172     }
3173 }
3174
3175 static void
3176 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3177                                  enum block_enum block_kind, const char *name,
3178                                  domain_enum domain)
3179 {
3180   dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3181 }
3182
3183 static void
3184 dw2_print_stats (struct objfile *objfile)
3185 {
3186   int i, count;
3187
3188   dw2_setup (objfile);
3189   count = 0;
3190   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3191                    + dwarf2_per_objfile->n_type_units); ++i)
3192     {
3193       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3194
3195       if (!per_cu->v.quick->symtab)
3196         ++count;
3197     }
3198   printf_filtered (_("  Number of unread CUs: %d\n"), count);
3199 }
3200
3201 static void
3202 dw2_dump (struct objfile *objfile)
3203 {
3204   /* Nothing worth printing.  */
3205 }
3206
3207 static void
3208 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3209               struct section_offsets *delta)
3210 {
3211   /* There's nothing to relocate here.  */
3212 }
3213
3214 static void
3215 dw2_expand_symtabs_for_function (struct objfile *objfile,
3216                                  const char *func_name)
3217 {
3218   /* Note: It doesn't matter what we pass for block_kind here.  */
3219   dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3220                                   VAR_DOMAIN);
3221 }
3222
3223 static void
3224 dw2_expand_all_symtabs (struct objfile *objfile)
3225 {
3226   int i;
3227
3228   dw2_setup (objfile);
3229
3230   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3231                    + dwarf2_per_objfile->n_type_units); ++i)
3232     {
3233       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3234
3235       dw2_instantiate_symtab (per_cu);
3236     }
3237 }
3238
3239 static void
3240 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3241                                   const char *filename)
3242 {
3243   int i;
3244
3245   dw2_setup (objfile);
3246
3247   /* We don't need to consider type units here.
3248      This is only called for examining code, e.g. expand_line_sal.
3249      There can be an order of magnitude (or more) more type units
3250      than comp units, and we avoid them if we can.  */
3251
3252   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3253     {
3254       int j;
3255       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3256       struct quick_file_names *file_data;
3257
3258       /* We only need to look at symtabs not already expanded.  */
3259       if (per_cu->v.quick->symtab)
3260         continue;
3261
3262       file_data = dw2_get_file_names (objfile, per_cu);
3263       if (file_data == NULL)
3264         continue;
3265
3266       for (j = 0; j < file_data->num_file_names; ++j)
3267         {
3268           const char *this_name = file_data->file_names[j];
3269           if (FILENAME_CMP (this_name, filename) == 0)
3270             {
3271               dw2_instantiate_symtab (per_cu);
3272               break;
3273             }
3274         }
3275     }
3276 }
3277
3278 /* A helper function for dw2_find_symbol_file that finds the primary
3279    file name for a given CU.  This is a die_reader_func.  */
3280
3281 static void
3282 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3283                                  gdb_byte *info_ptr,
3284                                  struct die_info *comp_unit_die,
3285                                  int has_children,
3286                                  void *data)
3287 {
3288   const char **result_ptr = data;
3289   struct dwarf2_cu *cu = reader->cu;
3290   struct attribute *attr;
3291
3292   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3293   if (attr == NULL)
3294     *result_ptr = NULL;
3295   else
3296     *result_ptr = DW_STRING (attr);
3297 }
3298
3299 static const char *
3300 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3301 {
3302   struct dwarf2_per_cu_data *per_cu;
3303   offset_type *vec;
3304   struct quick_file_names *file_data;
3305   const char *filename;
3306
3307   dw2_setup (objfile);
3308
3309   /* index_table is NULL if OBJF_READNOW.  */
3310   if (!dwarf2_per_objfile->index_table)
3311     {
3312       struct symtab *s;
3313
3314       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3315         {
3316           struct blockvector *bv = BLOCKVECTOR (s);
3317           const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3318           struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3319
3320           if (sym)
3321             return sym->symtab->filename;
3322         }
3323       return NULL;
3324     }
3325
3326   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3327                                  name, &vec))
3328     return NULL;
3329
3330   /* Note that this just looks at the very first one named NAME -- but
3331      actually we are looking for a function.  find_main_filename
3332      should be rewritten so that it doesn't require a custom hook.  It
3333      could just use the ordinary symbol tables.  */
3334   /* vec[0] is the length, which must always be >0.  */
3335   per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3336
3337   if (per_cu->v.quick->symtab != NULL)
3338     return per_cu->v.quick->symtab->filename;
3339
3340   init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3341                            dw2_get_primary_filename_reader, &filename);
3342
3343   return filename;
3344 }
3345
3346 static void
3347 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3348                           struct objfile *objfile, int global,
3349                           int (*callback) (struct block *,
3350                                            struct symbol *, void *),
3351                           void *data, symbol_compare_ftype *match,
3352                           symbol_compare_ftype *ordered_compare)
3353 {
3354   /* Currently unimplemented; used for Ada.  The function can be called if the
3355      current language is Ada for a non-Ada objfile using GNU index.  As Ada
3356      does not look for non-Ada symbols this function should just return.  */
3357 }
3358
3359 static void
3360 dw2_expand_symtabs_matching
3361   (struct objfile *objfile,
3362    int (*file_matcher) (const char *, void *),
3363    int (*name_matcher) (const char *, void *),
3364    enum search_domain kind,
3365    void *data)
3366 {
3367   int i;
3368   offset_type iter;
3369   struct mapped_index *index;
3370
3371   dw2_setup (objfile);
3372
3373   /* index_table is NULL if OBJF_READNOW.  */
3374   if (!dwarf2_per_objfile->index_table)
3375     return;
3376   index = dwarf2_per_objfile->index_table;
3377
3378   if (file_matcher != NULL)
3379     {
3380       struct cleanup *cleanup;
3381       htab_t visited_found, visited_not_found;
3382
3383       dw2_build_type_unit_groups ();
3384
3385       visited_found = htab_create_alloc (10,
3386                                          htab_hash_pointer, htab_eq_pointer,
3387                                          NULL, xcalloc, xfree);
3388       cleanup = make_cleanup_htab_delete (visited_found);
3389       visited_not_found = htab_create_alloc (10,
3390                                              htab_hash_pointer, htab_eq_pointer,
3391                                              NULL, xcalloc, xfree);
3392       make_cleanup_htab_delete (visited_not_found);
3393
3394       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3395                        + dwarf2_per_objfile->n_type_unit_groups); ++i)
3396         {
3397           int j;
3398           struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3399           struct quick_file_names *file_data;
3400           void **slot;
3401
3402           per_cu->v.quick->mark = 0;
3403
3404           /* We only need to look at symtabs not already expanded.  */
3405           if (per_cu->v.quick->symtab)
3406             continue;
3407
3408           file_data = dw2_get_file_names (objfile, per_cu);
3409           if (file_data == NULL)
3410             continue;
3411
3412           if (htab_find (visited_not_found, file_data) != NULL)
3413             continue;
3414           else if (htab_find (visited_found, file_data) != NULL)
3415             {
3416               per_cu->v.quick->mark = 1;
3417               continue;
3418             }
3419
3420           for (j = 0; j < file_data->num_file_names; ++j)
3421             {
3422               if (file_matcher (file_data->file_names[j], data))
3423                 {
3424                   per_cu->v.quick->mark = 1;
3425                   break;
3426                 }
3427             }
3428
3429           slot = htab_find_slot (per_cu->v.quick->mark
3430                                  ? visited_found
3431                                  : visited_not_found,
3432                                  file_data, INSERT);
3433           *slot = file_data;
3434         }
3435
3436       do_cleanups (cleanup);
3437     }
3438
3439   for (iter = 0; iter < index->symbol_table_slots; ++iter)
3440     {
3441       offset_type idx = 2 * iter;
3442       const char *name;
3443       offset_type *vec, vec_len, vec_idx;
3444
3445       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3446         continue;
3447
3448       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3449
3450       if (! (*name_matcher) (name, data))
3451         continue;
3452
3453       /* The name was matched, now expand corresponding CUs that were
3454          marked.  */
3455       vec = (offset_type *) (index->constant_pool
3456                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
3457       vec_len = MAYBE_SWAP (vec[0]);
3458       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3459         {
3460           struct dwarf2_per_cu_data *per_cu;
3461           offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3462           gdb_index_symbol_kind symbol_kind =
3463             GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3464           int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3465
3466           /* Don't crash on bad data.  */
3467           if (cu_index >= (dwarf2_per_objfile->n_comp_units
3468                            + dwarf2_per_objfile->n_type_units))
3469             continue;
3470
3471           /* Only check the symbol's kind if it has one.
3472              Indices prior to version 7 don't record it.  */
3473           if (index->version >= 7)
3474             {
3475               switch (kind)
3476                 {
3477                 case VARIABLES_DOMAIN:
3478                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3479                     continue;
3480                   break;
3481                 case FUNCTIONS_DOMAIN:
3482                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3483                     continue;
3484                   break;
3485                 case TYPES_DOMAIN:
3486                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3487                     continue;
3488                   break;
3489                 default:
3490                   break;
3491                 }
3492             }
3493
3494           per_cu = dw2_get_cu (cu_index);
3495           if (file_matcher == NULL || per_cu->v.quick->mark)
3496             dw2_instantiate_symtab (per_cu);
3497         }
3498     }
3499 }
3500
3501 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3502    symtab.  */
3503
3504 static struct symtab *
3505 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3506 {
3507   int i;
3508
3509   if (BLOCKVECTOR (symtab) != NULL
3510       && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3511     return symtab;
3512
3513   if (symtab->includes == NULL)
3514     return NULL;
3515
3516   for (i = 0; symtab->includes[i]; ++i)
3517     {
3518       struct symtab *s = symtab->includes[i];
3519
3520       s = recursively_find_pc_sect_symtab (s, pc);
3521       if (s != NULL)
3522         return s;
3523     }
3524
3525   return NULL;
3526 }
3527
3528 static struct symtab *
3529 dw2_find_pc_sect_symtab (struct objfile *objfile,
3530                          struct minimal_symbol *msymbol,
3531                          CORE_ADDR pc,
3532                          struct obj_section *section,
3533                          int warn_if_readin)
3534 {
3535   struct dwarf2_per_cu_data *data;
3536   struct symtab *result;
3537
3538   dw2_setup (objfile);
3539
3540   if (!objfile->psymtabs_addrmap)
3541     return NULL;
3542
3543   data = addrmap_find (objfile->psymtabs_addrmap, pc);
3544   if (!data)
3545     return NULL;
3546
3547   if (warn_if_readin && data->v.quick->symtab)
3548     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3549              paddress (get_objfile_arch (objfile), pc));
3550
3551   result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3552   gdb_assert (result != NULL);
3553   return result;
3554 }
3555
3556 static void
3557 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3558                           void *data, int need_fullname)
3559 {
3560   int i;
3561   struct cleanup *cleanup;
3562   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3563                                       NULL, xcalloc, xfree);
3564
3565   cleanup = make_cleanup_htab_delete (visited);
3566   dw2_setup (objfile);
3567
3568   dw2_build_type_unit_groups ();
3569
3570   /* We can ignore file names coming from already-expanded CUs.  */
3571   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3572                    + dwarf2_per_objfile->n_type_units); ++i)
3573     {
3574       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3575
3576       if (per_cu->v.quick->symtab)
3577         {
3578           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3579                                         INSERT);
3580
3581           *slot = per_cu->v.quick->file_names;
3582         }
3583     }
3584
3585   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3586                    + dwarf2_per_objfile->n_type_unit_groups); ++i)
3587     {
3588       int j;
3589       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3590       struct quick_file_names *file_data;
3591       void **slot;
3592
3593       /* We only need to look at symtabs not already expanded.  */
3594       if (per_cu->v.quick->symtab)
3595         continue;
3596
3597       file_data = dw2_get_file_names (objfile, per_cu);
3598       if (file_data == NULL)
3599         continue;
3600
3601       slot = htab_find_slot (visited, file_data, INSERT);
3602       if (*slot)
3603         {
3604           /* Already visited.  */
3605           continue;
3606         }
3607       *slot = file_data;
3608
3609       for (j = 0; j < file_data->num_file_names; ++j)
3610         {
3611           const char *this_real_name;
3612
3613           if (need_fullname)
3614             this_real_name = dw2_get_real_path (objfile, file_data, j);
3615           else
3616             this_real_name = NULL;
3617           (*fun) (file_data->file_names[j], this_real_name, data);
3618         }
3619     }
3620
3621   do_cleanups (cleanup);
3622 }
3623
3624 static int
3625 dw2_has_symbols (struct objfile *objfile)
3626 {
3627   return 1;
3628 }
3629
3630 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3631 {
3632   dw2_has_symbols,
3633   dw2_find_last_source_symtab,
3634   dw2_forget_cached_source_info,
3635   dw2_map_symtabs_matching_filename,
3636   dw2_lookup_symbol,
3637   dw2_pre_expand_symtabs_matching,
3638   dw2_print_stats,
3639   dw2_dump,
3640   dw2_relocate,
3641   dw2_expand_symtabs_for_function,
3642   dw2_expand_all_symtabs,
3643   dw2_expand_symtabs_with_filename,
3644   dw2_find_symbol_file,
3645   dw2_map_matching_symbols,
3646   dw2_expand_symtabs_matching,
3647   dw2_find_pc_sect_symtab,
3648   dw2_map_symbol_filenames
3649 };
3650
3651 /* Initialize for reading DWARF for this objfile.  Return 0 if this
3652    file will use psymtabs, or 1 if using the GNU index.  */
3653
3654 int
3655 dwarf2_initialize_objfile (struct objfile *objfile)
3656 {
3657   /* If we're about to read full symbols, don't bother with the
3658      indices.  In this case we also don't care if some other debug
3659      format is making psymtabs, because they are all about to be
3660      expanded anyway.  */
3661   if ((objfile->flags & OBJF_READNOW))
3662     {
3663       int i;
3664
3665       dwarf2_per_objfile->using_index = 1;
3666       create_all_comp_units (objfile);
3667       create_all_type_units (objfile);
3668       dwarf2_per_objfile->quick_file_names_table =
3669         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3670
3671       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3672                        + dwarf2_per_objfile->n_type_units); ++i)
3673         {
3674           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3675
3676           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3677                                             struct dwarf2_per_cu_quick_data);
3678         }
3679
3680       /* Return 1 so that gdb sees the "quick" functions.  However,
3681          these functions will be no-ops because we will have expanded
3682          all symtabs.  */
3683       return 1;
3684     }
3685
3686   if (dwarf2_read_index (objfile))
3687     return 1;
3688
3689   return 0;
3690 }
3691
3692 \f
3693
3694 /* Build a partial symbol table.  */
3695
3696 void
3697 dwarf2_build_psymtabs (struct objfile *objfile)
3698 {
3699   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3700     {
3701       init_psymbol_list (objfile, 1024);
3702     }
3703
3704   dwarf2_build_psymtabs_hard (objfile);
3705 }
3706
3707 /* Return the total length of the CU described by HEADER.  */
3708
3709 static unsigned int
3710 get_cu_length (const struct comp_unit_head *header)
3711 {
3712   return header->initial_length_size + header->length;
3713 }
3714
3715 /* Return TRUE if OFFSET is within CU_HEADER.  */
3716
3717 static inline int
3718 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3719 {
3720   sect_offset bottom = { cu_header->offset.sect_off };
3721   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3722
3723   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3724 }
3725
3726 /* Find the base address of the compilation unit for range lists and
3727    location lists.  It will normally be specified by DW_AT_low_pc.
3728    In DWARF-3 draft 4, the base address could be overridden by
3729    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3730    compilation units with discontinuous ranges.  */
3731
3732 static void
3733 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3734 {
3735   struct attribute *attr;
3736
3737   cu->base_known = 0;
3738   cu->base_address = 0;
3739
3740   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3741   if (attr)
3742     {
3743       cu->base_address = DW_ADDR (attr);
3744       cu->base_known = 1;
3745     }
3746   else
3747     {
3748       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3749       if (attr)
3750         {
3751           cu->base_address = DW_ADDR (attr);
3752           cu->base_known = 1;
3753         }
3754     }
3755 }
3756
3757 /* Read in the comp unit header information from the debug_info at info_ptr.
3758    NOTE: This leaves members offset, first_die_offset to be filled in
3759    by the caller.  */
3760
3761 static gdb_byte *
3762 read_comp_unit_head (struct comp_unit_head *cu_header,
3763                      gdb_byte *info_ptr, bfd *abfd)
3764 {
3765   int signed_addr;
3766   unsigned int bytes_read;
3767
3768   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3769   cu_header->initial_length_size = bytes_read;
3770   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3771   info_ptr += bytes_read;
3772   cu_header->version = read_2_bytes (abfd, info_ptr);
3773   info_ptr += 2;
3774   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3775                                              &bytes_read);
3776   info_ptr += bytes_read;
3777   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3778   info_ptr += 1;
3779   signed_addr = bfd_get_sign_extend_vma (abfd);
3780   if (signed_addr < 0)
3781     internal_error (__FILE__, __LINE__,
3782                     _("read_comp_unit_head: dwarf from non elf file"));
3783   cu_header->signed_addr_p = signed_addr;
3784
3785   return info_ptr;
3786 }
3787
3788 /* Helper function that returns the proper abbrev section for
3789    THIS_CU.  */
3790
3791 static struct dwarf2_section_info *
3792 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3793 {
3794   struct dwarf2_section_info *abbrev;
3795
3796   if (this_cu->is_dwz)
3797     abbrev = &dwarf2_get_dwz_file ()->abbrev;
3798   else
3799     abbrev = &dwarf2_per_objfile->abbrev;
3800
3801   return abbrev;
3802 }
3803
3804 /* Subroutine of read_and_check_comp_unit_head and
3805    read_and_check_type_unit_head to simplify them.
3806    Perform various error checking on the header.  */
3807
3808 static void
3809 error_check_comp_unit_head (struct comp_unit_head *header,
3810                             struct dwarf2_section_info *section,
3811                             struct dwarf2_section_info *abbrev_section)
3812 {
3813   bfd *abfd = section->asection->owner;
3814   const char *filename = bfd_get_filename (abfd);
3815
3816   if (header->version != 2 && header->version != 3 && header->version != 4)
3817     error (_("Dwarf Error: wrong version in compilation unit header "
3818            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3819            filename);
3820
3821   if (header->abbrev_offset.sect_off
3822       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3823     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3824            "(offset 0x%lx + 6) [in module %s]"),
3825            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3826            filename);
3827
3828   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3829      avoid potential 32-bit overflow.  */
3830   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3831       > section->size)
3832     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3833            "(offset 0x%lx + 0) [in module %s]"),
3834            (long) header->length, (long) header->offset.sect_off,
3835            filename);
3836 }
3837
3838 /* Read in a CU/TU header and perform some basic error checking.
3839    The contents of the header are stored in HEADER.
3840    The result is a pointer to the start of the first DIE.  */
3841
3842 static gdb_byte *
3843 read_and_check_comp_unit_head (struct comp_unit_head *header,
3844                                struct dwarf2_section_info *section,
3845                                struct dwarf2_section_info *abbrev_section,
3846                                gdb_byte *info_ptr,
3847                                int is_debug_types_section)
3848 {
3849   gdb_byte *beg_of_comp_unit = info_ptr;
3850   bfd *abfd = section->asection->owner;
3851
3852   header->offset.sect_off = beg_of_comp_unit - section->buffer;
3853
3854   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3855
3856   /* If we're reading a type unit, skip over the signature and
3857      type_offset fields.  */
3858   if (is_debug_types_section)
3859     info_ptr += 8 /*signature*/ + header->offset_size;
3860
3861   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3862
3863   error_check_comp_unit_head (header, section, abbrev_section);
3864
3865   return info_ptr;
3866 }
3867
3868 /* Read in the types comp unit header information from .debug_types entry at
3869    types_ptr.  The result is a pointer to one past the end of the header.  */
3870
3871 static gdb_byte *
3872 read_and_check_type_unit_head (struct comp_unit_head *header,
3873                                struct dwarf2_section_info *section,
3874                                struct dwarf2_section_info *abbrev_section,
3875                                gdb_byte *info_ptr,
3876                                ULONGEST *signature,
3877                                cu_offset *type_offset_in_tu)
3878 {
3879   gdb_byte *beg_of_comp_unit = info_ptr;
3880   bfd *abfd = section->asection->owner;
3881
3882   header->offset.sect_off = beg_of_comp_unit - section->buffer;
3883
3884   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3885
3886   /* If we're reading a type unit, skip over the signature and
3887      type_offset fields.  */
3888   if (signature != NULL)
3889     *signature = read_8_bytes (abfd, info_ptr);
3890   info_ptr += 8;
3891   if (type_offset_in_tu != NULL)
3892     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3893                                                header->offset_size);
3894   info_ptr += header->offset_size;
3895
3896   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3897
3898   error_check_comp_unit_head (header, section, abbrev_section);
3899
3900   return info_ptr;
3901 }
3902
3903 /* Fetch the abbreviation table offset from a comp or type unit header.  */
3904
3905 static sect_offset
3906 read_abbrev_offset (struct dwarf2_section_info *section,
3907                     sect_offset offset)
3908 {
3909   bfd *abfd = section->asection->owner;
3910   gdb_byte *info_ptr;
3911   unsigned int length, initial_length_size, offset_size;
3912   sect_offset abbrev_offset;
3913
3914   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3915   info_ptr = section->buffer + offset.sect_off;
3916   length = read_initial_length (abfd, info_ptr, &initial_length_size);
3917   offset_size = initial_length_size == 4 ? 4 : 8;
3918   info_ptr += initial_length_size + 2 /*version*/;
3919   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3920   return abbrev_offset;
3921 }
3922
3923 /* Allocate a new partial symtab for file named NAME and mark this new
3924    partial symtab as being an include of PST.  */
3925
3926 static void
3927 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3928                                struct objfile *objfile)
3929 {
3930   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3931
3932   subpst->section_offsets = pst->section_offsets;
3933   subpst->textlow = 0;
3934   subpst->texthigh = 0;
3935
3936   subpst->dependencies = (struct partial_symtab **)
3937     obstack_alloc (&objfile->objfile_obstack,
3938                    sizeof (struct partial_symtab *));
3939   subpst->dependencies[0] = pst;
3940   subpst->number_of_dependencies = 1;
3941
3942   subpst->globals_offset = 0;
3943   subpst->n_global_syms = 0;
3944   subpst->statics_offset = 0;
3945   subpst->n_static_syms = 0;
3946   subpst->symtab = NULL;
3947   subpst->read_symtab = pst->read_symtab;
3948   subpst->readin = 0;
3949
3950   /* No private part is necessary for include psymtabs.  This property
3951      can be used to differentiate between such include psymtabs and
3952      the regular ones.  */
3953   subpst->read_symtab_private = NULL;
3954 }
3955
3956 /* Read the Line Number Program data and extract the list of files
3957    included by the source file represented by PST.  Build an include
3958    partial symtab for each of these included files.  */
3959
3960 static void
3961 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3962                                struct die_info *die,
3963                                struct partial_symtab *pst)
3964 {
3965   struct line_header *lh = NULL;
3966   struct attribute *attr;
3967
3968   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3969   if (attr)
3970     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3971   if (lh == NULL)
3972     return;  /* No linetable, so no includes.  */
3973
3974   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3975   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3976
3977   free_line_header (lh);
3978 }
3979
3980 static hashval_t
3981 hash_signatured_type (const void *item)
3982 {
3983   const struct signatured_type *sig_type = item;
3984
3985   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3986   return sig_type->signature;
3987 }
3988
3989 static int
3990 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3991 {
3992   const struct signatured_type *lhs = item_lhs;
3993   const struct signatured_type *rhs = item_rhs;
3994
3995   return lhs->signature == rhs->signature;
3996 }
3997
3998 /* Allocate a hash table for signatured types.  */
3999
4000 static htab_t
4001 allocate_signatured_type_table (struct objfile *objfile)
4002 {
4003   return htab_create_alloc_ex (41,
4004                                hash_signatured_type,
4005                                eq_signatured_type,
4006                                NULL,
4007                                &objfile->objfile_obstack,
4008                                hashtab_obstack_allocate,
4009                                dummy_obstack_deallocate);
4010 }
4011
4012 /* A helper function to add a signatured type CU to a table.  */
4013
4014 static int
4015 add_signatured_type_cu_to_table (void **slot, void *datum)
4016 {
4017   struct signatured_type *sigt = *slot;
4018   struct signatured_type ***datap = datum;
4019
4020   **datap = sigt;
4021   ++*datap;
4022
4023   return 1;
4024 }
4025
4026 /* Create the hash table of all entries in the .debug_types section.
4027    DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4028    The result is a pointer to the hash table or NULL if there are
4029    no types.  */
4030
4031 static htab_t
4032 create_debug_types_hash_table (struct dwo_file *dwo_file,
4033                                VEC (dwarf2_section_info_def) *types)
4034 {
4035   struct objfile *objfile = dwarf2_per_objfile->objfile;
4036   htab_t types_htab = NULL;
4037   int ix;
4038   struct dwarf2_section_info *section;
4039   struct dwarf2_section_info *abbrev_section;
4040
4041   if (VEC_empty (dwarf2_section_info_def, types))
4042     return NULL;
4043
4044   abbrev_section = (dwo_file != NULL
4045                     ? &dwo_file->sections.abbrev
4046                     : &dwarf2_per_objfile->abbrev);
4047
4048   if (dwarf2_read_debug)
4049     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4050                         dwo_file ? ".dwo" : "",
4051                         bfd_get_filename (abbrev_section->asection->owner));
4052
4053   for (ix = 0;
4054        VEC_iterate (dwarf2_section_info_def, types, ix, section);
4055        ++ix)
4056     {
4057       bfd *abfd;
4058       gdb_byte *info_ptr, *end_ptr;
4059       struct dwarf2_section_info *abbrev_section;
4060
4061       dwarf2_read_section (objfile, section);
4062       info_ptr = section->buffer;
4063
4064       if (info_ptr == NULL)
4065         continue;
4066
4067       /* We can't set abfd until now because the section may be empty or
4068          not present, in which case section->asection will be NULL.  */
4069       abfd = section->asection->owner;
4070
4071       if (dwo_file)
4072         abbrev_section = &dwo_file->sections.abbrev;
4073       else
4074         abbrev_section = &dwarf2_per_objfile->abbrev;
4075
4076       if (types_htab == NULL)
4077         {
4078           if (dwo_file)
4079             types_htab = allocate_dwo_unit_table (objfile);
4080           else
4081             types_htab = allocate_signatured_type_table (objfile);
4082         }
4083
4084       /* We don't use init_cutu_and_read_dies_simple, or some such, here
4085          because we don't need to read any dies: the signature is in the
4086          header.  */
4087
4088       end_ptr = info_ptr + section->size;
4089       while (info_ptr < end_ptr)
4090         {
4091           sect_offset offset;
4092           cu_offset type_offset_in_tu;
4093           ULONGEST signature;
4094           struct signatured_type *sig_type;
4095           struct dwo_unit *dwo_tu;
4096           void **slot;
4097           gdb_byte *ptr = info_ptr;
4098           struct comp_unit_head header;
4099           unsigned int length;
4100
4101           offset.sect_off = ptr - section->buffer;
4102
4103           /* We need to read the type's signature in order to build the hash
4104              table, but we don't need anything else just yet.  */
4105
4106           ptr = read_and_check_type_unit_head (&header, section,
4107                                                abbrev_section, ptr,
4108                                                &signature, &type_offset_in_tu);
4109
4110           length = get_cu_length (&header);
4111
4112           /* Skip dummy type units.  */
4113           if (ptr >= info_ptr + length
4114               || peek_abbrev_code (abfd, ptr) == 0)
4115             {
4116               info_ptr += length;
4117               continue;
4118             }
4119
4120           if (dwo_file)
4121             {
4122               sig_type = NULL;
4123               dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4124                                        struct dwo_unit);
4125               dwo_tu->dwo_file = dwo_file;
4126               dwo_tu->signature = signature;
4127               dwo_tu->type_offset_in_tu = type_offset_in_tu;
4128               dwo_tu->info_or_types_section = section;
4129               dwo_tu->offset = offset;
4130               dwo_tu->length = length;
4131             }
4132           else
4133             {
4134               /* N.B.: type_offset is not usable if this type uses a DWO file.
4135                  The real type_offset is in the DWO file.  */
4136               dwo_tu = NULL;
4137               sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4138                                          struct signatured_type);
4139               sig_type->signature = signature;
4140               sig_type->type_offset_in_tu = type_offset_in_tu;
4141               sig_type->per_cu.objfile = objfile;
4142               sig_type->per_cu.is_debug_types = 1;
4143               sig_type->per_cu.info_or_types_section = section;
4144               sig_type->per_cu.offset = offset;
4145               sig_type->per_cu.length = length;
4146             }
4147
4148           slot = htab_find_slot (types_htab,
4149                                  dwo_file ? (void*) dwo_tu : (void *) sig_type,
4150                                  INSERT);
4151           gdb_assert (slot != NULL);
4152           if (*slot != NULL)
4153             {
4154               sect_offset dup_offset;
4155
4156               if (dwo_file)
4157                 {
4158                   const struct dwo_unit *dup_tu = *slot;
4159
4160                   dup_offset = dup_tu->offset;
4161                 }
4162               else
4163                 {
4164                   const struct signatured_type *dup_tu = *slot;
4165
4166                   dup_offset = dup_tu->per_cu.offset;
4167                 }
4168
4169               complaint (&symfile_complaints,
4170                          _("debug type entry at offset 0x%x is duplicate to the "
4171                            "entry at offset 0x%x, signature 0x%s"),
4172                          offset.sect_off, dup_offset.sect_off,
4173                          phex (signature, sizeof (signature)));
4174             }
4175           *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4176
4177           if (dwarf2_read_debug)
4178             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
4179                                 offset.sect_off,
4180                                 phex (signature, sizeof (signature)));
4181
4182           info_ptr += length;
4183         }
4184     }
4185
4186   return types_htab;
4187 }
4188
4189 /* Create the hash table of all entries in the .debug_types section,
4190    and initialize all_type_units.
4191    The result is zero if there is an error (e.g. missing .debug_types section),
4192    otherwise non-zero.  */
4193
4194 static int
4195 create_all_type_units (struct objfile *objfile)
4196 {
4197   htab_t types_htab;
4198   struct signatured_type **iter;
4199
4200   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4201   if (types_htab == NULL)
4202     {
4203       dwarf2_per_objfile->signatured_types = NULL;
4204       return 0;
4205     }
4206
4207   dwarf2_per_objfile->signatured_types = types_htab;
4208
4209   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4210   dwarf2_per_objfile->all_type_units
4211     = obstack_alloc (&objfile->objfile_obstack,
4212                      dwarf2_per_objfile->n_type_units
4213                      * sizeof (struct signatured_type *));
4214   iter = &dwarf2_per_objfile->all_type_units[0];
4215   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4216   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4217               == dwarf2_per_objfile->n_type_units);
4218
4219   return 1;
4220 }
4221
4222 /* Lookup a signature based type for DW_FORM_ref_sig8.
4223    Returns NULL if signature SIG is not present in the table.  */
4224
4225 static struct signatured_type *
4226 lookup_signatured_type (ULONGEST sig)
4227 {
4228   struct signatured_type find_entry, *entry;
4229
4230   if (dwarf2_per_objfile->signatured_types == NULL)
4231     {
4232       complaint (&symfile_complaints,
4233                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4234       return NULL;
4235     }
4236
4237   find_entry.signature = sig;
4238   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4239   return entry;
4240 }
4241 \f
4242 /* Low level DIE reading support.  */
4243
4244 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
4245
4246 static void
4247 init_cu_die_reader (struct die_reader_specs *reader,
4248                     struct dwarf2_cu *cu,
4249                     struct dwarf2_section_info *section,
4250                     struct dwo_file *dwo_file)
4251 {
4252   gdb_assert (section->readin && section->buffer != NULL);
4253   reader->abfd = section->asection->owner;
4254   reader->cu = cu;
4255   reader->dwo_file = dwo_file;
4256   reader->die_section = section;
4257   reader->buffer = section->buffer;
4258   reader->buffer_end = section->buffer + section->size;
4259 }
4260
4261 /* Initialize a CU (or TU) and read its DIEs.
4262    If the CU defers to a DWO file, read the DWO file as well.
4263
4264    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4265    Otherwise the table specified in the comp unit header is read in and used.
4266    This is an optimization for when we already have the abbrev table.
4267
4268    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4269    Otherwise, a new CU is allocated with xmalloc.
4270
4271    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4272    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.
4273
4274    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4275    linker) then DIE_READER_FUNC will not get called.  */
4276
4277 static void
4278 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4279                          struct abbrev_table *abbrev_table,
4280                          int use_existing_cu, int keep,
4281                          die_reader_func_ftype *die_reader_func,
4282                          void *data)
4283 {
4284   struct objfile *objfile = dwarf2_per_objfile->objfile;
4285   struct dwarf2_section_info *section = this_cu->info_or_types_section;
4286   bfd *abfd = section->asection->owner;
4287   struct dwarf2_cu *cu;
4288   gdb_byte *begin_info_ptr, *info_ptr;
4289   struct die_reader_specs reader;
4290   struct die_info *comp_unit_die;
4291   int has_children;
4292   struct attribute *attr;
4293   struct cleanup *cleanups, *free_cu_cleanup = NULL;
4294   struct signatured_type *sig_type = NULL;
4295   struct dwarf2_section_info *abbrev_section;
4296   /* Non-zero if CU currently points to a DWO file and we need to
4297      reread it.  When this happens we need to reread the skeleton die
4298      before we can reread the DWO file.  */
4299   int rereading_dwo_cu = 0;
4300
4301   if (dwarf2_die_debug)
4302     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4303                         this_cu->is_debug_types ? "type" : "comp",
4304                         this_cu->offset.sect_off);
4305
4306   if (use_existing_cu)
4307     gdb_assert (keep);
4308
4309   cleanups = make_cleanup (null_cleanup, NULL);
4310
4311   /* This is cheap if the section is already read in.  */
4312   dwarf2_read_section (objfile, section);
4313
4314   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4315
4316   abbrev_section = get_abbrev_section_for_cu (this_cu);
4317
4318   if (use_existing_cu && this_cu->cu != NULL)
4319     {
4320       cu = this_cu->cu;
4321
4322       /* If this CU is from a DWO file we need to start over, we need to
4323          refetch the attributes from the skeleton CU.
4324          This could be optimized by retrieving those attributes from when we
4325          were here the first time: the previous comp_unit_die was stored in
4326          comp_unit_obstack.  But there's no data yet that we need this
4327          optimization.  */
4328       if (cu->dwo_unit != NULL)
4329         rereading_dwo_cu = 1;
4330     }
4331   else
4332     {
4333       /* If !use_existing_cu, this_cu->cu must be NULL.  */
4334       gdb_assert (this_cu->cu == NULL);
4335
4336       cu = xmalloc (sizeof (*cu));
4337       init_one_comp_unit (cu, this_cu);
4338
4339       /* If an error occurs while loading, release our storage.  */
4340       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4341     }
4342
4343   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4344     {
4345       /* We already have the header, there's no need to read it in again.  */
4346       info_ptr += cu->header.first_die_offset.cu_off;
4347     }
4348   else
4349     {
4350       if (this_cu->is_debug_types)
4351         {
4352           ULONGEST signature;
4353           cu_offset type_offset_in_tu;
4354
4355           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4356                                                     abbrev_section, info_ptr,
4357                                                     &signature,
4358                                                     &type_offset_in_tu);
4359
4360           /* Since per_cu is the first member of struct signatured_type,
4361              we can go from a pointer to one to a pointer to the other.  */
4362           sig_type = (struct signatured_type *) this_cu;
4363           gdb_assert (sig_type->signature == signature);
4364           gdb_assert (sig_type->type_offset_in_tu.cu_off
4365                       == type_offset_in_tu.cu_off);
4366           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4367
4368           /* LENGTH has not been set yet for type units if we're
4369              using .gdb_index.  */
4370           this_cu->length = get_cu_length (&cu->header);
4371
4372           /* Establish the type offset that can be used to lookup the type.  */
4373           sig_type->type_offset_in_section.sect_off =
4374             this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4375         }
4376       else
4377         {
4378           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4379                                                     abbrev_section,
4380                                                     info_ptr, 0);
4381
4382           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4383           gdb_assert (this_cu->length == get_cu_length (&cu->header));
4384         }
4385     }
4386
4387   /* Skip dummy compilation units.  */
4388   if (info_ptr >= begin_info_ptr + this_cu->length
4389       || peek_abbrev_code (abfd, info_ptr) == 0)
4390     {
4391       do_cleanups (cleanups);
4392       return;
4393     }
4394
4395   /* If we don't have them yet, read the abbrevs for this compilation unit.
4396      And if we need to read them now, make sure they're freed when we're
4397      done.  Note that it's important that if the CU had an abbrev table
4398      on entry we don't free it when we're done: Somewhere up the call stack
4399      it may be in use.  */
4400   if (abbrev_table != NULL)
4401     {
4402       gdb_assert (cu->abbrev_table == NULL);
4403       gdb_assert (cu->header.abbrev_offset.sect_off
4404                   == abbrev_table->offset.sect_off);
4405       cu->abbrev_table = abbrev_table;
4406     }
4407   else if (cu->abbrev_table == NULL)
4408     {
4409       dwarf2_read_abbrevs (cu, abbrev_section);
4410       make_cleanup (dwarf2_free_abbrev_table, cu);
4411     }
4412   else if (rereading_dwo_cu)
4413     {
4414       dwarf2_free_abbrev_table (cu);
4415       dwarf2_read_abbrevs (cu, abbrev_section);
4416     }
4417
4418   /* Read the top level CU/TU die.  */
4419   init_cu_die_reader (&reader, cu, section, NULL);
4420   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4421
4422   /* If we have a DWO stub, process it and then read in the DWO file.
4423      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4424      a DWO CU, that this test will fail.  */
4425   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4426   if (attr)
4427     {
4428       char *dwo_name = DW_STRING (attr);
4429       const char *comp_dir_string;
4430       struct dwo_unit *dwo_unit;
4431       ULONGEST signature; /* Or dwo_id.  */
4432       struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4433       int i,num_extra_attrs;
4434       struct dwarf2_section_info *dwo_abbrev_section;
4435
4436       if (has_children)
4437         error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4438                  " has children (offset 0x%x) [in module %s]"),
4439                this_cu->offset.sect_off, bfd_get_filename (abfd));
4440
4441       /* These attributes aren't processed until later:
4442          DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4443          However, the attribute is found in the stub which we won't have later.
4444          In order to not impose this complication on the rest of the code,
4445          we read them here and copy them to the DWO CU/TU die.  */
4446
4447       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4448          DWO file.  */
4449       stmt_list = NULL;
4450       if (! this_cu->is_debug_types)
4451         stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4452       low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4453       high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4454       ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4455       comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4456
4457       /* There should be a DW_AT_addr_base attribute here (if needed).
4458          We need the value before we can process DW_FORM_GNU_addr_index.  */
4459       cu->addr_base = 0;
4460       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4461       if (attr)
4462         cu->addr_base = DW_UNSND (attr);
4463
4464       /* There should be a DW_AT_ranges_base attribute here (if needed).
4465          We need the value before we can process DW_AT_ranges.  */
4466       cu->ranges_base = 0;
4467       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4468       if (attr)
4469         cu->ranges_base = DW_UNSND (attr);
4470
4471       if (this_cu->is_debug_types)
4472         {
4473           gdb_assert (sig_type != NULL);
4474           signature = sig_type->signature;
4475         }
4476       else
4477         {
4478           attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4479           if (! attr)
4480             error (_("Dwarf Error: missing dwo_id [in module %s]"),
4481                    dwo_name);
4482           signature = DW_UNSND (attr);
4483         }
4484
4485       /* We may need the comp_dir in order to find the DWO file.  */
4486       comp_dir_string = NULL;
4487       if (comp_dir)
4488         comp_dir_string = DW_STRING (comp_dir);
4489
4490       if (this_cu->is_debug_types)
4491         dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4492       else
4493         dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4494                                          signature);
4495
4496       if (dwo_unit == NULL)
4497         {
4498           error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4499                    " with ID %s [in module %s]"),
4500                  this_cu->offset.sect_off,
4501                  phex (signature, sizeof (signature)),
4502                  objfile->name);
4503         }
4504
4505       /* Set up for reading the DWO CU/TU.  */
4506       cu->dwo_unit = dwo_unit;
4507       section = dwo_unit->info_or_types_section;
4508       begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4509       dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4510       init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4511
4512       if (this_cu->is_debug_types)
4513         {
4514           ULONGEST signature;
4515
4516           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4517                                                     dwo_abbrev_section,
4518                                                     info_ptr,
4519                                                     &signature, NULL);
4520           gdb_assert (sig_type->signature == signature);
4521           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4522           gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4523
4524           /* Establish the type offset that can be used to lookup the type.
4525              For DWO files, we don't know it until now.  */
4526           sig_type->type_offset_in_section.sect_off =
4527             dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4528         }
4529       else
4530         {
4531           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4532                                                     dwo_abbrev_section,
4533                                                     info_ptr, 0);
4534           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4535           gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4536         }
4537
4538       /* Discard the original CU's abbrev table, and read the DWO's.  */
4539       if (abbrev_table == NULL)
4540         {
4541           dwarf2_free_abbrev_table (cu);
4542           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4543         }
4544       else
4545         {
4546           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4547           make_cleanup (dwarf2_free_abbrev_table, cu);
4548         }
4549
4550       /* Read in the die, but leave space to copy over the attributes
4551          from the stub.  This has the benefit of simplifying the rest of
4552          the code - all the real work is done here.  */
4553       num_extra_attrs = ((stmt_list != NULL)
4554                          + (low_pc != NULL)
4555                          + (high_pc != NULL)
4556                          + (ranges != NULL)
4557                          + (comp_dir != NULL));
4558       info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4559                                   &has_children, num_extra_attrs);
4560
4561       /* Copy over the attributes from the stub to the DWO die.  */
4562       i = comp_unit_die->num_attrs;
4563       if (stmt_list != NULL)
4564         comp_unit_die->attrs[i++] = *stmt_list;
4565       if (low_pc != NULL)
4566         comp_unit_die->attrs[i++] = *low_pc;
4567       if (high_pc != NULL)
4568         comp_unit_die->attrs[i++] = *high_pc;
4569       if (ranges != NULL)
4570         comp_unit_die->attrs[i++] = *ranges;
4571       if (comp_dir != NULL)
4572         comp_unit_die->attrs[i++] = *comp_dir;
4573       comp_unit_die->num_attrs += num_extra_attrs;
4574
4575       /* Skip dummy compilation units.  */
4576       if (info_ptr >= begin_info_ptr + dwo_unit->length
4577           || peek_abbrev_code (abfd, info_ptr) == 0)
4578         {
4579           do_cleanups (cleanups);
4580           return;
4581         }
4582     }
4583
4584   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4585
4586   if (free_cu_cleanup != NULL)
4587     {
4588       if (keep)
4589         {
4590           /* We've successfully allocated this compilation unit.  Let our
4591              caller clean it up when finished with it.  */
4592           discard_cleanups (free_cu_cleanup);
4593
4594           /* We can only discard free_cu_cleanup and all subsequent cleanups.
4595              So we have to manually free the abbrev table.  */
4596           dwarf2_free_abbrev_table (cu);
4597
4598           /* Link this CU into read_in_chain.  */
4599           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4600           dwarf2_per_objfile->read_in_chain = this_cu;
4601         }
4602       else
4603         do_cleanups (free_cu_cleanup);
4604     }
4605
4606   do_cleanups (cleanups);
4607 }
4608
4609 /* Read CU/TU THIS_CU in section SECTION,
4610    but do not follow DW_AT_GNU_dwo_name if present.
4611    DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4612    have already done the lookup to find the DWO file).
4613
4614    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4615    THIS_CU->is_debug_types, but nothing else.
4616
4617    We fill in THIS_CU->length.
4618
4619    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4620    linker) then DIE_READER_FUNC will not get called.
4621
4622    THIS_CU->cu is always freed when done.
4623    This is done in order to not leave THIS_CU->cu in a state where we have
4624    to care whether it refers to the "main" CU or the DWO CU.  */
4625
4626 static void
4627 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4628                                    struct dwarf2_section_info *abbrev_section,
4629                                    struct dwo_file *dwo_file,
4630                                    die_reader_func_ftype *die_reader_func,
4631                                    void *data)
4632 {
4633   struct objfile *objfile = dwarf2_per_objfile->objfile;
4634   struct dwarf2_section_info *section = this_cu->info_or_types_section;
4635   bfd *abfd = section->asection->owner;
4636   struct dwarf2_cu cu;
4637   gdb_byte *begin_info_ptr, *info_ptr;
4638   struct die_reader_specs reader;
4639   struct cleanup *cleanups;
4640   struct die_info *comp_unit_die;
4641   int has_children;
4642
4643   if (dwarf2_die_debug)
4644     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4645                         this_cu->is_debug_types ? "type" : "comp",
4646                         this_cu->offset.sect_off);
4647
4648   gdb_assert (this_cu->cu == NULL);
4649
4650   /* This is cheap if the section is already read in.  */
4651   dwarf2_read_section (objfile, section);
4652
4653   init_one_comp_unit (&cu, this_cu);
4654
4655   cleanups = make_cleanup (free_stack_comp_unit, &cu);
4656
4657   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4658   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4659                                             abbrev_section, info_ptr,
4660                                             this_cu->is_debug_types);
4661
4662   this_cu->length = get_cu_length (&cu.header);
4663
4664   /* Skip dummy compilation units.  */
4665   if (info_ptr >= begin_info_ptr + this_cu->length
4666       || peek_abbrev_code (abfd, info_ptr) == 0)
4667     {
4668       do_cleanups (cleanups);
4669       return;
4670     }
4671
4672   dwarf2_read_abbrevs (&cu, abbrev_section);
4673   make_cleanup (dwarf2_free_abbrev_table, &cu);
4674
4675   init_cu_die_reader (&reader, &cu, section, dwo_file);
4676   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4677
4678   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4679
4680   do_cleanups (cleanups);
4681 }
4682
4683 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4684    does not lookup the specified DWO file.
4685    This cannot be used to read DWO files.
4686
4687    THIS_CU->cu is always freed when done.
4688    This is done in order to not leave THIS_CU->cu in a state where we have
4689    to care whether it refers to the "main" CU or the DWO CU.
4690    We can revisit this if the data shows there's a performance issue.  */
4691
4692 static void
4693 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4694                                 die_reader_func_ftype *die_reader_func,
4695                                 void *data)
4696 {
4697   init_cutu_and_read_dies_no_follow (this_cu,
4698                                      get_abbrev_section_for_cu (this_cu),
4699                                      NULL,
4700                                      die_reader_func, data);
4701 }
4702
4703 /* Create a psymtab named NAME and assign it to PER_CU.
4704
4705    The caller must fill in the following details:
4706    dirname, textlow, texthigh.  */
4707
4708 static struct partial_symtab *
4709 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4710 {
4711   struct objfile *objfile = per_cu->objfile;
4712   struct partial_symtab *pst;
4713
4714   pst = start_psymtab_common (objfile, objfile->section_offsets,
4715                               name, 0,
4716                               objfile->global_psymbols.next,
4717                               objfile->static_psymbols.next);
4718
4719   pst->psymtabs_addrmap_supported = 1;
4720
4721   /* This is the glue that links PST into GDB's symbol API.  */
4722   pst->read_symtab_private = per_cu;
4723   pst->read_symtab = dwarf2_psymtab_to_symtab;
4724   per_cu->v.psymtab = pst;
4725
4726   return pst;
4727 }
4728
4729 /* die_reader_func for process_psymtab_comp_unit.  */
4730
4731 static void
4732 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4733                                   gdb_byte *info_ptr,
4734                                   struct die_info *comp_unit_die,
4735                                   int has_children,
4736                                   void *data)
4737 {
4738   struct dwarf2_cu *cu = reader->cu;
4739   struct objfile *objfile = cu->objfile;
4740   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4741   struct attribute *attr;
4742   CORE_ADDR baseaddr;
4743   CORE_ADDR best_lowpc = 0, best_highpc = 0;
4744   struct partial_symtab *pst;
4745   int has_pc_info;
4746   const char *filename;
4747   int *want_partial_unit_ptr = data;
4748
4749   if (comp_unit_die->tag == DW_TAG_partial_unit
4750       && (want_partial_unit_ptr == NULL
4751           || !*want_partial_unit_ptr))
4752     return;
4753
4754   gdb_assert (! per_cu->is_debug_types);
4755
4756   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4757
4758   cu->list_in_scope = &file_symbols;
4759
4760   /* Allocate a new partial symbol table structure.  */
4761   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4762   if (attr == NULL || !DW_STRING (attr))
4763     filename = "";
4764   else
4765     filename = DW_STRING (attr);
4766
4767   pst = create_partial_symtab (per_cu, filename);
4768
4769   /* This must be done before calling dwarf2_build_include_psymtabs.  */
4770   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4771   if (attr != NULL)
4772     pst->dirname = DW_STRING (attr);
4773
4774   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4775
4776   dwarf2_find_base_address (comp_unit_die, cu);
4777
4778   /* Possibly set the default values of LOWPC and HIGHPC from
4779      `DW_AT_ranges'.  */
4780   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4781                                       &best_highpc, cu, pst);
4782   if (has_pc_info == 1 && best_lowpc < best_highpc)
4783     /* Store the contiguous range if it is not empty; it can be empty for
4784        CUs with no code.  */
4785     addrmap_set_empty (objfile->psymtabs_addrmap,
4786                        best_lowpc + baseaddr,
4787                        best_highpc + baseaddr - 1, pst);
4788
4789   /* Check if comp unit has_children.
4790      If so, read the rest of the partial symbols from this comp unit.
4791      If not, there's no more debug_info for this comp unit.  */
4792   if (has_children)
4793     {
4794       struct partial_die_info *first_die;
4795       CORE_ADDR lowpc, highpc;
4796
4797       lowpc = ((CORE_ADDR) -1);
4798       highpc = ((CORE_ADDR) 0);
4799
4800       first_die = load_partial_dies (reader, info_ptr, 1);
4801
4802       scan_partial_symbols (first_die, &lowpc, &highpc,
4803                             ! has_pc_info, cu);
4804
4805       /* If we didn't find a lowpc, set it to highpc to avoid
4806          complaints from `maint check'.  */
4807       if (lowpc == ((CORE_ADDR) -1))
4808         lowpc = highpc;
4809
4810       /* If the compilation unit didn't have an explicit address range,
4811          then use the information extracted from its child dies.  */
4812       if (! has_pc_info)
4813         {
4814           best_lowpc = lowpc;
4815           best_highpc = highpc;
4816         }
4817     }
4818   pst->textlow = best_lowpc + baseaddr;
4819   pst->texthigh = best_highpc + baseaddr;
4820
4821   pst->n_global_syms = objfile->global_psymbols.next -
4822     (objfile->global_psymbols.list + pst->globals_offset);
4823   pst->n_static_syms = objfile->static_psymbols.next -
4824     (objfile->static_psymbols.list + pst->statics_offset);
4825   sort_pst_symbols (pst);
4826
4827   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4828     {
4829       int i;
4830       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4831       struct dwarf2_per_cu_data *iter;
4832
4833       /* Fill in 'dependencies' here; we fill in 'users' in a
4834          post-pass.  */
4835       pst->number_of_dependencies = len;
4836       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4837                                          len * sizeof (struct symtab *));
4838       for (i = 0;
4839            VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4840                         i, iter);
4841            ++i)
4842         pst->dependencies[i] = iter->v.psymtab;
4843
4844       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4845     }
4846
4847   /* Get the list of files included in the current compilation unit,
4848      and build a psymtab for each of them.  */
4849   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4850
4851   if (dwarf2_read_debug)
4852     {
4853       struct gdbarch *gdbarch = get_objfile_arch (objfile);
4854
4855       fprintf_unfiltered (gdb_stdlog,
4856                           "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4857                           ", %d global, %d static syms\n",
4858                           per_cu->is_debug_types ? "type" : "comp",
4859                           per_cu->offset.sect_off,
4860                           paddress (gdbarch, pst->textlow),
4861                           paddress (gdbarch, pst->texthigh),
4862                           pst->n_global_syms, pst->n_static_syms);
4863     }
4864 }
4865
4866 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4867    Process compilation unit THIS_CU for a psymtab.  */
4868
4869 static void
4870 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4871                            int want_partial_unit)
4872 {
4873   /* If this compilation unit was already read in, free the
4874      cached copy in order to read it in again.  This is
4875      necessary because we skipped some symbols when we first
4876      read in the compilation unit (see load_partial_dies).
4877      This problem could be avoided, but the benefit is unclear.  */
4878   if (this_cu->cu != NULL)
4879     free_one_cached_comp_unit (this_cu);
4880
4881   gdb_assert (! this_cu->is_debug_types);
4882   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4883                            process_psymtab_comp_unit_reader,
4884                            &want_partial_unit);
4885
4886   /* Age out any secondary CUs.  */
4887   age_cached_comp_units ();
4888 }
4889
4890 static hashval_t
4891 hash_type_unit_group (const void *item)
4892 {
4893   const struct type_unit_group *tu_group = item;
4894
4895   return hash_stmt_list_entry (&tu_group->hash);
4896 }
4897
4898 static int
4899 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4900 {
4901   const struct type_unit_group *lhs = item_lhs;
4902   const struct type_unit_group *rhs = item_rhs;
4903
4904   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4905 }
4906
4907 /* Allocate a hash table for type unit groups.  */
4908
4909 static htab_t
4910 allocate_type_unit_groups_table (void)
4911 {
4912   return htab_create_alloc_ex (3,
4913                                hash_type_unit_group,
4914                                eq_type_unit_group,
4915                                NULL,
4916                                &dwarf2_per_objfile->objfile->objfile_obstack,
4917                                hashtab_obstack_allocate,
4918                                dummy_obstack_deallocate);
4919 }
4920
4921 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4922    partial symtabs.  We combine several TUs per psymtab to not let the size
4923    of any one psymtab grow too big.  */
4924 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4925 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4926
4927 /* Helper routine for get_type_unit_group.
4928    Create the type_unit_group object used to hold one or more TUs.  */
4929
4930 static struct type_unit_group *
4931 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4932 {
4933   struct objfile *objfile = dwarf2_per_objfile->objfile;
4934   struct dwarf2_per_cu_data *per_cu;
4935   struct type_unit_group *tu_group;
4936
4937   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4938                              struct type_unit_group);
4939   per_cu = &tu_group->per_cu;
4940   per_cu->objfile = objfile;
4941   per_cu->is_debug_types = 1;
4942   per_cu->s.type_unit_group = tu_group;
4943
4944   if (dwarf2_per_objfile->using_index)
4945     {
4946       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4947                                         struct dwarf2_per_cu_quick_data);
4948       tu_group->t.first_tu = cu->per_cu;
4949     }
4950   else
4951     {
4952       unsigned int line_offset = line_offset_struct.sect_off;
4953       struct partial_symtab *pst;
4954       char *name;
4955
4956       /* Give the symtab a useful name for debug purposes.  */
4957       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4958         name = xstrprintf ("<type_units_%d>",
4959                            (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4960       else
4961         name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4962
4963       pst = create_partial_symtab (per_cu, name);
4964       pst->anonymous = 1;
4965
4966       xfree (name);
4967     }
4968
4969   tu_group->hash.dwo_unit = cu->dwo_unit;
4970   tu_group->hash.line_offset = line_offset_struct;
4971
4972   return tu_group;
4973 }
4974
4975 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4976    STMT_LIST is a DW_AT_stmt_list attribute.  */
4977
4978 static struct type_unit_group *
4979 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4980 {
4981   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4982   struct type_unit_group *tu_group;
4983   void **slot;
4984   unsigned int line_offset;
4985   struct type_unit_group type_unit_group_for_lookup;
4986
4987   if (dwarf2_per_objfile->type_unit_groups == NULL)
4988     {
4989       dwarf2_per_objfile->type_unit_groups =
4990         allocate_type_unit_groups_table ();
4991     }
4992
4993   /* Do we need to create a new group, or can we use an existing one?  */
4994
4995   if (stmt_list)
4996     {
4997       line_offset = DW_UNSND (stmt_list);
4998       ++tu_stats->nr_symtab_sharers;
4999     }
5000   else
5001     {
5002       /* Ugh, no stmt_list.  Rare, but we have to handle it.
5003          We can do various things here like create one group per TU or
5004          spread them over multiple groups to split up the expansion work.
5005          To avoid worst case scenarios (too many groups or too large groups)
5006          we, umm, group them in bunches.  */
5007       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5008                      | (tu_stats->nr_stmt_less_type_units
5009                         / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5010       ++tu_stats->nr_stmt_less_type_units;
5011     }
5012
5013   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5014   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5015   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5016                          &type_unit_group_for_lookup, INSERT);
5017   if (*slot != NULL)
5018     {
5019       tu_group = *slot;
5020       gdb_assert (tu_group != NULL);
5021     }
5022   else
5023     {
5024       sect_offset line_offset_struct;
5025
5026       line_offset_struct.sect_off = line_offset;
5027       tu_group = create_type_unit_group (cu, line_offset_struct);
5028       *slot = tu_group;
5029       ++tu_stats->nr_symtabs;
5030     }
5031
5032   return tu_group;
5033 }
5034
5035 /* Struct used to sort TUs by their abbreviation table offset.  */
5036
5037 struct tu_abbrev_offset
5038 {
5039   struct signatured_type *sig_type;
5040   sect_offset abbrev_offset;
5041 };
5042
5043 /* Helper routine for build_type_unit_groups, passed to qsort.  */
5044
5045 static int
5046 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5047 {
5048   const struct tu_abbrev_offset * const *a = ap;
5049   const struct tu_abbrev_offset * const *b = bp;
5050   unsigned int aoff = (*a)->abbrev_offset.sect_off;
5051   unsigned int boff = (*b)->abbrev_offset.sect_off;
5052
5053   return (aoff > boff) - (aoff < boff);
5054 }
5055
5056 /* A helper function to add a type_unit_group to a table.  */
5057
5058 static int
5059 add_type_unit_group_to_table (void **slot, void *datum)
5060 {
5061   struct type_unit_group *tu_group = *slot;
5062   struct type_unit_group ***datap = datum;
5063
5064   **datap = tu_group;
5065   ++*datap;
5066
5067   return 1;
5068 }
5069
5070 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5071    each one passing FUNC,DATA.
5072
5073    The efficiency is because we sort TUs by the abbrev table they use and
5074    only read each abbrev table once.  In one program there are 200K TUs
5075    sharing 8K abbrev tables.
5076
5077    The main purpose of this function is to support building the
5078    dwarf2_per_objfile->type_unit_groups table.
5079    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5080    can collapse the search space by grouping them by stmt_list.
5081    The savings can be significant, in the same program from above the 200K TUs
5082    share 8K stmt_list tables.
5083
5084    FUNC is expected to call get_type_unit_group, which will create the
5085    struct type_unit_group if necessary and add it to
5086    dwarf2_per_objfile->type_unit_groups.  */
5087
5088 static void
5089 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5090 {
5091   struct objfile *objfile = dwarf2_per_objfile->objfile;
5092   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5093   struct cleanup *cleanups;
5094   struct abbrev_table *abbrev_table;
5095   sect_offset abbrev_offset;
5096   struct tu_abbrev_offset *sorted_by_abbrev;
5097   struct type_unit_group **iter;
5098   int i;
5099
5100   /* It's up to the caller to not call us multiple times.  */
5101   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5102
5103   if (dwarf2_per_objfile->n_type_units == 0)
5104     return;
5105
5106   /* TUs typically share abbrev tables, and there can be way more TUs than
5107      abbrev tables.  Sort by abbrev table to reduce the number of times we
5108      read each abbrev table in.
5109      Alternatives are to punt or to maintain a cache of abbrev tables.
5110      This is simpler and efficient enough for now.
5111
5112      Later we group TUs by their DW_AT_stmt_list value (as this defines the
5113      symtab to use).  Typically TUs with the same abbrev offset have the same
5114      stmt_list value too so in practice this should work well.
5115
5116      The basic algorithm here is:
5117
5118       sort TUs by abbrev table
5119       for each TU with same abbrev table:
5120         read abbrev table if first user
5121         read TU top level DIE
5122           [IWBN if DWO skeletons had DW_AT_stmt_list]
5123         call FUNC  */
5124
5125   if (dwarf2_read_debug)
5126     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5127
5128   /* Sort in a separate table to maintain the order of all_type_units
5129      for .gdb_index: TU indices directly index all_type_units.  */
5130   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5131                               dwarf2_per_objfile->n_type_units);
5132   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5133     {
5134       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5135
5136       sorted_by_abbrev[i].sig_type = sig_type;
5137       sorted_by_abbrev[i].abbrev_offset =
5138         read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5139                             sig_type->per_cu.offset);
5140     }
5141   cleanups = make_cleanup (xfree, sorted_by_abbrev);
5142   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5143          sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5144
5145   /* Note: In the .gdb_index case, get_type_unit_group may have already been
5146      called any number of times, so we don't reset tu_stats here.  */
5147
5148   abbrev_offset.sect_off = ~(unsigned) 0;
5149   abbrev_table = NULL;
5150   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5151
5152   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5153     {
5154       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5155
5156       /* Switch to the next abbrev table if necessary.  */
5157       if (abbrev_table == NULL
5158           || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5159         {
5160           if (abbrev_table != NULL)
5161             {
5162               abbrev_table_free (abbrev_table);
5163               /* Reset to NULL in case abbrev_table_read_table throws
5164                  an error: abbrev_table_free_cleanup will get called.  */
5165               abbrev_table = NULL;
5166             }
5167           abbrev_offset = tu->abbrev_offset;
5168           abbrev_table =
5169             abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5170                                      abbrev_offset);
5171           ++tu_stats->nr_uniq_abbrev_tables;
5172         }
5173
5174       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5175                                func, data);
5176     }
5177
5178   /* Create a vector of pointers to primary type units to make it easy to
5179      iterate over them and CUs.  See dw2_get_primary_cu.  */
5180   dwarf2_per_objfile->n_type_unit_groups =
5181     htab_elements (dwarf2_per_objfile->type_unit_groups);
5182   dwarf2_per_objfile->all_type_unit_groups =
5183     obstack_alloc (&objfile->objfile_obstack,
5184                    dwarf2_per_objfile->n_type_unit_groups
5185                    * sizeof (struct type_unit_group *));
5186   iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5187   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5188                           add_type_unit_group_to_table, &iter);
5189   gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5190               == dwarf2_per_objfile->n_type_unit_groups);
5191
5192   do_cleanups (cleanups);
5193
5194   if (dwarf2_read_debug)
5195     {
5196       fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5197       fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
5198                           dwarf2_per_objfile->n_type_units);
5199       fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
5200                           tu_stats->nr_uniq_abbrev_tables);
5201       fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
5202                           tu_stats->nr_symtabs);
5203       fprintf_unfiltered (gdb_stdlog, "  %d symtab sharers\n",
5204                           tu_stats->nr_symtab_sharers);
5205       fprintf_unfiltered (gdb_stdlog, "  %d type units without a stmt_list\n",
5206                           tu_stats->nr_stmt_less_type_units);
5207     }
5208 }
5209
5210 /* Reader function for build_type_psymtabs.  */
5211
5212 static void
5213 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5214                             gdb_byte *info_ptr,
5215                             struct die_info *type_unit_die,
5216                             int has_children,
5217                             void *data)
5218 {
5219   struct objfile *objfile = dwarf2_per_objfile->objfile;
5220   struct dwarf2_cu *cu = reader->cu;
5221   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5222   struct type_unit_group *tu_group;
5223   struct attribute *attr;
5224   struct partial_die_info *first_die;
5225   CORE_ADDR lowpc, highpc;
5226   struct partial_symtab *pst;
5227
5228   gdb_assert (data == NULL);
5229
5230   if (! has_children)
5231     return;
5232
5233   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5234   tu_group = get_type_unit_group (cu, attr);
5235
5236   VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5237
5238   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5239   cu->list_in_scope = &file_symbols;
5240   pst = create_partial_symtab (per_cu, "");
5241   pst->anonymous = 1;
5242
5243   first_die = load_partial_dies (reader, info_ptr, 1);
5244
5245   lowpc = (CORE_ADDR) -1;
5246   highpc = (CORE_ADDR) 0;
5247   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5248
5249   pst->n_global_syms = objfile->global_psymbols.next -
5250     (objfile->global_psymbols.list + pst->globals_offset);
5251   pst->n_static_syms = objfile->static_psymbols.next -
5252     (objfile->static_psymbols.list + pst->statics_offset);
5253   sort_pst_symbols (pst);
5254 }
5255
5256 /* Traversal function for build_type_psymtabs.  */
5257
5258 static int
5259 build_type_psymtab_dependencies (void **slot, void *info)
5260 {
5261   struct objfile *objfile = dwarf2_per_objfile->objfile;
5262   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5263   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5264   struct partial_symtab *pst = per_cu->v.psymtab;
5265   int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5266   struct dwarf2_per_cu_data *iter;
5267   int i;
5268
5269   gdb_assert (len > 0);
5270
5271   pst->number_of_dependencies = len;
5272   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5273                                      len * sizeof (struct psymtab *));
5274   for (i = 0;
5275        VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5276        ++i)
5277     {
5278       pst->dependencies[i] = iter->v.psymtab;
5279       iter->s.type_unit_group = tu_group;
5280     }
5281
5282   VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5283
5284   return 1;
5285 }
5286
5287 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5288    Build partial symbol tables for the .debug_types comp-units.  */
5289
5290 static void
5291 build_type_psymtabs (struct objfile *objfile)
5292 {
5293   if (! create_all_type_units (objfile))
5294     return;
5295
5296   build_type_unit_groups (build_type_psymtabs_reader, NULL);
5297
5298   /* Now that all TUs have been processed we can fill in the dependencies.  */
5299   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5300                           build_type_psymtab_dependencies, NULL);
5301 }
5302
5303 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
5304
5305 static void
5306 psymtabs_addrmap_cleanup (void *o)
5307 {
5308   struct objfile *objfile = o;
5309
5310   objfile->psymtabs_addrmap = NULL;
5311 }
5312
5313 /* Compute the 'user' field for each psymtab in OBJFILE.  */
5314
5315 static void
5316 set_partial_user (struct objfile *objfile)
5317 {
5318   int i;
5319
5320   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5321     {
5322       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5323       struct partial_symtab *pst = per_cu->v.psymtab;
5324       int j;
5325
5326       if (pst == NULL)
5327         continue;
5328
5329       for (j = 0; j < pst->number_of_dependencies; ++j)
5330         {
5331           /* Set the 'user' field only if it is not already set.  */
5332           if (pst->dependencies[j]->user == NULL)
5333             pst->dependencies[j]->user = pst;
5334         }
5335     }
5336 }
5337
5338 /* Build the partial symbol table by doing a quick pass through the
5339    .debug_info and .debug_abbrev sections.  */
5340
5341 static void
5342 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5343 {
5344   struct cleanup *back_to, *addrmap_cleanup;
5345   struct obstack temp_obstack;
5346   int i;
5347
5348   if (dwarf2_read_debug)
5349     {
5350       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5351                           objfile->name);
5352     }
5353
5354   dwarf2_per_objfile->reading_partial_symbols = 1;
5355
5356   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5357
5358   /* Any cached compilation units will be linked by the per-objfile
5359      read_in_chain.  Make sure to free them when we're done.  */
5360   back_to = make_cleanup (free_cached_comp_units, NULL);
5361
5362   build_type_psymtabs (objfile);
5363
5364   create_all_comp_units (objfile);
5365
5366   /* Create a temporary address map on a temporary obstack.  We later
5367      copy this to the final obstack.  */
5368   obstack_init (&temp_obstack);
5369   make_cleanup_obstack_free (&temp_obstack);
5370   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5371   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5372
5373   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5374     {
5375       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5376
5377       process_psymtab_comp_unit (per_cu, 0);
5378     }
5379
5380   set_partial_user (objfile);
5381
5382   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5383                                                     &objfile->objfile_obstack);
5384   discard_cleanups (addrmap_cleanup);
5385
5386   do_cleanups (back_to);
5387
5388   if (dwarf2_read_debug)
5389     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5390                         objfile->name);
5391 }
5392
5393 /* die_reader_func for load_partial_comp_unit.  */
5394
5395 static void
5396 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5397                                gdb_byte *info_ptr,
5398                                struct die_info *comp_unit_die,
5399                                int has_children,
5400                                void *data)
5401 {
5402   struct dwarf2_cu *cu = reader->cu;
5403
5404   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5405
5406   /* Check if comp unit has_children.
5407      If so, read the rest of the partial symbols from this comp unit.
5408      If not, there's no more debug_info for this comp unit.  */
5409   if (has_children)
5410     load_partial_dies (reader, info_ptr, 0);
5411 }
5412
5413 /* Load the partial DIEs for a secondary CU into memory.
5414    This is also used when rereading a primary CU with load_all_dies.  */
5415
5416 static void
5417 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5418 {
5419   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5420                            load_partial_comp_unit_reader, NULL);
5421 }
5422
5423 static void
5424 read_comp_units_from_section (struct objfile *objfile,
5425                               struct dwarf2_section_info *section,
5426                               unsigned int is_dwz,
5427                               int *n_allocated,
5428                               int *n_comp_units,
5429                               struct dwarf2_per_cu_data ***all_comp_units)
5430 {
5431   gdb_byte *info_ptr;
5432   bfd *abfd = section->asection->owner;
5433
5434   dwarf2_read_section (objfile, section);
5435
5436   info_ptr = section->buffer;
5437
5438   while (info_ptr < section->buffer + section->size)
5439     {
5440       unsigned int length, initial_length_size;
5441       struct dwarf2_per_cu_data *this_cu;
5442       sect_offset offset;
5443
5444       offset.sect_off = info_ptr - section->buffer;
5445
5446       /* Read just enough information to find out where the next
5447          compilation unit is.  */
5448       length = read_initial_length (abfd, info_ptr, &initial_length_size);
5449
5450       /* Save the compilation unit for later lookup.  */
5451       this_cu = obstack_alloc (&objfile->objfile_obstack,
5452                                sizeof (struct dwarf2_per_cu_data));
5453       memset (this_cu, 0, sizeof (*this_cu));
5454       this_cu->offset = offset;
5455       this_cu->length = length + initial_length_size;
5456       this_cu->is_dwz = is_dwz;
5457       this_cu->objfile = objfile;
5458       this_cu->info_or_types_section = section;
5459
5460       if (*n_comp_units == *n_allocated)
5461         {
5462           *n_allocated *= 2;
5463           *all_comp_units = xrealloc (*all_comp_units,
5464                                       *n_allocated
5465                                       * sizeof (struct dwarf2_per_cu_data *));
5466         }
5467       (*all_comp_units)[*n_comp_units] = this_cu;
5468       ++*n_comp_units;
5469
5470       info_ptr = info_ptr + this_cu->length;
5471     }
5472 }
5473
5474 /* Create a list of all compilation units in OBJFILE.
5475    This is only done for -readnow and building partial symtabs.  */
5476
5477 static void
5478 create_all_comp_units (struct objfile *objfile)
5479 {
5480   int n_allocated;
5481   int n_comp_units;
5482   struct dwarf2_per_cu_data **all_comp_units;
5483
5484   n_comp_units = 0;
5485   n_allocated = 10;
5486   all_comp_units = xmalloc (n_allocated
5487                             * sizeof (struct dwarf2_per_cu_data *));
5488
5489   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5490                                 &n_allocated, &n_comp_units, &all_comp_units);
5491
5492   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5493     {
5494       struct dwz_file *dwz = dwarf2_get_dwz_file ();
5495
5496       read_comp_units_from_section (objfile, &dwz->info, 1,
5497                                     &n_allocated, &n_comp_units,
5498                                     &all_comp_units);
5499     }
5500
5501   dwarf2_per_objfile->all_comp_units
5502     = obstack_alloc (&objfile->objfile_obstack,
5503                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5504   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5505           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5506   xfree (all_comp_units);
5507   dwarf2_per_objfile->n_comp_units = n_comp_units;
5508 }
5509
5510 /* Process all loaded DIEs for compilation unit CU, starting at
5511    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
5512    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5513    DW_AT_ranges).  If NEED_PC is set, then this function will set
5514    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5515    and record the covered ranges in the addrmap.  */
5516
5517 static void
5518 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5519                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5520 {
5521   struct partial_die_info *pdi;
5522
5523   /* Now, march along the PDI's, descending into ones which have
5524      interesting children but skipping the children of the other ones,
5525      until we reach the end of the compilation unit.  */
5526
5527   pdi = first_die;
5528
5529   while (pdi != NULL)
5530     {
5531       fixup_partial_die (pdi, cu);
5532
5533       /* Anonymous namespaces or modules have no name but have interesting
5534          children, so we need to look at them.  Ditto for anonymous
5535          enums.  */
5536
5537       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5538           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5539           || pdi->tag == DW_TAG_imported_unit)
5540         {
5541           switch (pdi->tag)
5542             {
5543             case DW_TAG_subprogram:
5544               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5545               break;
5546             case DW_TAG_constant:
5547             case DW_TAG_variable:
5548             case DW_TAG_typedef:
5549             case DW_TAG_union_type:
5550               if (!pdi->is_declaration)
5551                 {
5552                   add_partial_symbol (pdi, cu);
5553                 }
5554               break;
5555             case DW_TAG_class_type:
5556             case DW_TAG_interface_type:
5557             case DW_TAG_structure_type:
5558               if (!pdi->is_declaration)
5559                 {
5560                   add_partial_symbol (pdi, cu);
5561                 }
5562               break;
5563             case DW_TAG_enumeration_type:
5564               if (!pdi->is_declaration)
5565                 add_partial_enumeration (pdi, cu);
5566               break;
5567             case DW_TAG_base_type:
5568             case DW_TAG_subrange_type:
5569               /* File scope base type definitions are added to the partial
5570                  symbol table.  */
5571               add_partial_symbol (pdi, cu);
5572               break;
5573             case DW_TAG_namespace:
5574               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5575               break;
5576             case DW_TAG_module:
5577               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5578               break;
5579             case DW_TAG_imported_unit:
5580               {
5581                 struct dwarf2_per_cu_data *per_cu;
5582
5583                 /* For now we don't handle imported units in type units.  */
5584                 if (cu->per_cu->is_debug_types)
5585                   {
5586                     error (_("Dwarf Error: DW_TAG_imported_unit is not"
5587                              " supported in type units [in module %s]"),
5588                            cu->objfile->name);
5589                   }
5590
5591                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5592                                                            pdi->is_dwz,
5593                                                            cu->objfile);
5594
5595                 /* Go read the partial unit, if needed.  */
5596                 if (per_cu->v.psymtab == NULL)
5597                   process_psymtab_comp_unit (per_cu, 1);
5598
5599                 VEC_safe_push (dwarf2_per_cu_ptr,
5600                                cu->per_cu->s.imported_symtabs, per_cu);
5601               }
5602               break;
5603             default:
5604               break;
5605             }
5606         }
5607
5608       /* If the die has a sibling, skip to the sibling.  */
5609
5610       pdi = pdi->die_sibling;
5611     }
5612 }
5613
5614 /* Functions used to compute the fully scoped name of a partial DIE.
5615
5616    Normally, this is simple.  For C++, the parent DIE's fully scoped
5617    name is concatenated with "::" and the partial DIE's name.  For
5618    Java, the same thing occurs except that "." is used instead of "::".
5619    Enumerators are an exception; they use the scope of their parent
5620    enumeration type, i.e. the name of the enumeration type is not
5621    prepended to the enumerator.
5622
5623    There are two complexities.  One is DW_AT_specification; in this
5624    case "parent" means the parent of the target of the specification,
5625    instead of the direct parent of the DIE.  The other is compilers
5626    which do not emit DW_TAG_namespace; in this case we try to guess
5627    the fully qualified name of structure types from their members'
5628    linkage names.  This must be done using the DIE's children rather
5629    than the children of any DW_AT_specification target.  We only need
5630    to do this for structures at the top level, i.e. if the target of
5631    any DW_AT_specification (if any; otherwise the DIE itself) does not
5632    have a parent.  */
5633
5634 /* Compute the scope prefix associated with PDI's parent, in
5635    compilation unit CU.  The result will be allocated on CU's
5636    comp_unit_obstack, or a copy of the already allocated PDI->NAME
5637    field.  NULL is returned if no prefix is necessary.  */
5638 static char *
5639 partial_die_parent_scope (struct partial_die_info *pdi,
5640                           struct dwarf2_cu *cu)
5641 {
5642   char *grandparent_scope;
5643   struct partial_die_info *parent, *real_pdi;
5644
5645   /* We need to look at our parent DIE; if we have a DW_AT_specification,
5646      then this means the parent of the specification DIE.  */
5647
5648   real_pdi = pdi;
5649   while (real_pdi->has_specification)
5650     real_pdi = find_partial_die (real_pdi->spec_offset,
5651                                  real_pdi->spec_is_dwz, cu);
5652
5653   parent = real_pdi->die_parent;
5654   if (parent == NULL)
5655     return NULL;
5656
5657   if (parent->scope_set)
5658     return parent->scope;
5659
5660   fixup_partial_die (parent, cu);
5661
5662   grandparent_scope = partial_die_parent_scope (parent, cu);
5663
5664   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5665      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5666      Work around this problem here.  */
5667   if (cu->language == language_cplus
5668       && parent->tag == DW_TAG_namespace
5669       && strcmp (parent->name, "::") == 0
5670       && grandparent_scope == NULL)
5671     {
5672       parent->scope = NULL;
5673       parent->scope_set = 1;
5674       return NULL;
5675     }
5676
5677   if (pdi->tag == DW_TAG_enumerator)
5678     /* Enumerators should not get the name of the enumeration as a prefix.  */
5679     parent->scope = grandparent_scope;
5680   else if (parent->tag == DW_TAG_namespace
5681       || parent->tag == DW_TAG_module
5682       || parent->tag == DW_TAG_structure_type
5683       || parent->tag == DW_TAG_class_type
5684       || parent->tag == DW_TAG_interface_type
5685       || parent->tag == DW_TAG_union_type
5686       || parent->tag == DW_TAG_enumeration_type)
5687     {
5688       if (grandparent_scope == NULL)
5689         parent->scope = parent->name;
5690       else
5691         parent->scope = typename_concat (&cu->comp_unit_obstack,
5692                                          grandparent_scope,
5693                                          parent->name, 0, cu);
5694     }
5695   else
5696     {
5697       /* FIXME drow/2004-04-01: What should we be doing with
5698          function-local names?  For partial symbols, we should probably be
5699          ignoring them.  */
5700       complaint (&symfile_complaints,
5701                  _("unhandled containing DIE tag %d for DIE at %d"),
5702                  parent->tag, pdi->offset.sect_off);
5703       parent->scope = grandparent_scope;
5704     }
5705
5706   parent->scope_set = 1;
5707   return parent->scope;
5708 }
5709
5710 /* Return the fully scoped name associated with PDI, from compilation unit
5711    CU.  The result will be allocated with malloc.  */
5712
5713 static char *
5714 partial_die_full_name (struct partial_die_info *pdi,
5715                        struct dwarf2_cu *cu)
5716 {
5717   char *parent_scope;
5718
5719   /* If this is a template instantiation, we can not work out the
5720      template arguments from partial DIEs.  So, unfortunately, we have
5721      to go through the full DIEs.  At least any work we do building
5722      types here will be reused if full symbols are loaded later.  */
5723   if (pdi->has_template_arguments)
5724     {
5725       fixup_partial_die (pdi, cu);
5726
5727       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5728         {
5729           struct die_info *die;
5730           struct attribute attr;
5731           struct dwarf2_cu *ref_cu = cu;
5732
5733           /* DW_FORM_ref_addr is using section offset.  */
5734           attr.name = 0;
5735           attr.form = DW_FORM_ref_addr;
5736           attr.u.unsnd = pdi->offset.sect_off;
5737           die = follow_die_ref (NULL, &attr, &ref_cu);
5738
5739           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5740         }
5741     }
5742
5743   parent_scope = partial_die_parent_scope (pdi, cu);
5744   if (parent_scope == NULL)
5745     return NULL;
5746   else
5747     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5748 }
5749
5750 static void
5751 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5752 {
5753   struct objfile *objfile = cu->objfile;
5754   CORE_ADDR addr = 0;
5755   char *actual_name = NULL;
5756   CORE_ADDR baseaddr;
5757   int built_actual_name = 0;
5758
5759   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5760
5761   actual_name = partial_die_full_name (pdi, cu);
5762   if (actual_name)
5763     built_actual_name = 1;
5764
5765   if (actual_name == NULL)
5766     actual_name = pdi->name;
5767
5768   switch (pdi->tag)
5769     {
5770     case DW_TAG_subprogram:
5771       if (pdi->is_external || cu->language == language_ada)
5772         {
5773           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5774              of the global scope.  But in Ada, we want to be able to access
5775              nested procedures globally.  So all Ada subprograms are stored
5776              in the global scope.  */
5777           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5778              mst_text, objfile); */
5779           add_psymbol_to_list (actual_name, strlen (actual_name),
5780                                built_actual_name,
5781                                VAR_DOMAIN, LOC_BLOCK,
5782                                &objfile->global_psymbols,
5783                                0, pdi->lowpc + baseaddr,
5784                                cu->language, objfile);
5785         }
5786       else
5787         {
5788           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5789              mst_file_text, objfile); */
5790           add_psymbol_to_list (actual_name, strlen (actual_name),
5791                                built_actual_name,
5792                                VAR_DOMAIN, LOC_BLOCK,
5793                                &objfile->static_psymbols,
5794                                0, pdi->lowpc + baseaddr,
5795                                cu->language, objfile);
5796         }
5797       break;
5798     case DW_TAG_constant:
5799       {
5800         struct psymbol_allocation_list *list;
5801
5802         if (pdi->is_external)
5803           list = &objfile->global_psymbols;
5804         else
5805           list = &objfile->static_psymbols;
5806         add_psymbol_to_list (actual_name, strlen (actual_name),
5807                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
5808                              list, 0, 0, cu->language, objfile);
5809       }
5810       break;
5811     case DW_TAG_variable:
5812       if (pdi->d.locdesc)
5813         addr = decode_locdesc (pdi->d.locdesc, cu);
5814
5815       if (pdi->d.locdesc
5816           && addr == 0
5817           && !dwarf2_per_objfile->has_section_at_zero)
5818         {
5819           /* A global or static variable may also have been stripped
5820              out by the linker if unused, in which case its address
5821              will be nullified; do not add such variables into partial
5822              symbol table then.  */
5823         }
5824       else if (pdi->is_external)
5825         {
5826           /* Global Variable.
5827              Don't enter into the minimal symbol tables as there is
5828              a minimal symbol table entry from the ELF symbols already.
5829              Enter into partial symbol table if it has a location
5830              descriptor or a type.
5831              If the location descriptor is missing, new_symbol will create
5832              a LOC_UNRESOLVED symbol, the address of the variable will then
5833              be determined from the minimal symbol table whenever the variable
5834              is referenced.
5835              The address for the partial symbol table entry is not
5836              used by GDB, but it comes in handy for debugging partial symbol
5837              table building.  */
5838
5839           if (pdi->d.locdesc || pdi->has_type)
5840             add_psymbol_to_list (actual_name, strlen (actual_name),
5841                                  built_actual_name,
5842                                  VAR_DOMAIN, LOC_STATIC,
5843                                  &objfile->global_psymbols,
5844                                  0, addr + baseaddr,
5845                                  cu->language, objfile);
5846         }
5847       else
5848         {
5849           /* Static Variable.  Skip symbols without location descriptors.  */
5850           if (pdi->d.locdesc == NULL)
5851             {
5852               if (built_actual_name)
5853                 xfree (actual_name);
5854               return;
5855             }
5856           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5857              mst_file_data, objfile); */
5858           add_psymbol_to_list (actual_name, strlen (actual_name),
5859                                built_actual_name,
5860                                VAR_DOMAIN, LOC_STATIC,
5861                                &objfile->static_psymbols,
5862                                0, addr + baseaddr,
5863                                cu->language, objfile);
5864         }
5865       break;
5866     case DW_TAG_typedef:
5867     case DW_TAG_base_type:
5868     case DW_TAG_subrange_type:
5869       add_psymbol_to_list (actual_name, strlen (actual_name),
5870                            built_actual_name,
5871                            VAR_DOMAIN, LOC_TYPEDEF,
5872                            &objfile->static_psymbols,
5873                            0, (CORE_ADDR) 0, cu->language, objfile);
5874       break;
5875     case DW_TAG_namespace:
5876       add_psymbol_to_list (actual_name, strlen (actual_name),
5877                            built_actual_name,
5878                            VAR_DOMAIN, LOC_TYPEDEF,
5879                            &objfile->global_psymbols,
5880                            0, (CORE_ADDR) 0, cu->language, objfile);
5881       break;
5882     case DW_TAG_class_type:
5883     case DW_TAG_interface_type:
5884     case DW_TAG_structure_type:
5885     case DW_TAG_union_type:
5886     case DW_TAG_enumeration_type:
5887       /* Skip external references.  The DWARF standard says in the section
5888          about "Structure, Union, and Class Type Entries": "An incomplete
5889          structure, union or class type is represented by a structure,
5890          union or class entry that does not have a byte size attribute
5891          and that has a DW_AT_declaration attribute."  */
5892       if (!pdi->has_byte_size && pdi->is_declaration)
5893         {
5894           if (built_actual_name)
5895             xfree (actual_name);
5896           return;
5897         }
5898
5899       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5900          static vs. global.  */
5901       add_psymbol_to_list (actual_name, strlen (actual_name),
5902                            built_actual_name,
5903                            STRUCT_DOMAIN, LOC_TYPEDEF,
5904                            (cu->language == language_cplus
5905                             || cu->language == language_java)
5906                            ? &objfile->global_psymbols
5907                            : &objfile->static_psymbols,
5908                            0, (CORE_ADDR) 0, cu->language, objfile);
5909
5910       break;
5911     case DW_TAG_enumerator:
5912       add_psymbol_to_list (actual_name, strlen (actual_name),
5913                            built_actual_name,
5914                            VAR_DOMAIN, LOC_CONST,
5915                            (cu->language == language_cplus
5916                             || cu->language == language_java)
5917                            ? &objfile->global_psymbols
5918                            : &objfile->static_psymbols,
5919                            0, (CORE_ADDR) 0, cu->language, objfile);
5920       break;
5921     default:
5922       break;
5923     }
5924
5925   if (built_actual_name)
5926     xfree (actual_name);
5927 }
5928
5929 /* Read a partial die corresponding to a namespace; also, add a symbol
5930    corresponding to that namespace to the symbol table.  NAMESPACE is
5931    the name of the enclosing namespace.  */
5932
5933 static void
5934 add_partial_namespace (struct partial_die_info *pdi,
5935                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
5936                        int need_pc, struct dwarf2_cu *cu)
5937 {
5938   /* Add a symbol for the namespace.  */
5939
5940   add_partial_symbol (pdi, cu);
5941
5942   /* Now scan partial symbols in that namespace.  */
5943
5944   if (pdi->has_children)
5945     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5946 }
5947
5948 /* Read a partial die corresponding to a Fortran module.  */
5949
5950 static void
5951 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5952                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5953 {
5954   /* Now scan partial symbols in that module.  */
5955
5956   if (pdi->has_children)
5957     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5958 }
5959
5960 /* Read a partial die corresponding to a subprogram and create a partial
5961    symbol for that subprogram.  When the CU language allows it, this
5962    routine also defines a partial symbol for each nested subprogram
5963    that this subprogram contains.
5964
5965    DIE my also be a lexical block, in which case we simply search
5966    recursively for suprograms defined inside that lexical block.
5967    Again, this is only performed when the CU language allows this
5968    type of definitions.  */
5969
5970 static void
5971 add_partial_subprogram (struct partial_die_info *pdi,
5972                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
5973                         int need_pc, struct dwarf2_cu *cu)
5974 {
5975   if (pdi->tag == DW_TAG_subprogram)
5976     {
5977       if (pdi->has_pc_info)
5978         {
5979           if (pdi->lowpc < *lowpc)
5980             *lowpc = pdi->lowpc;
5981           if (pdi->highpc > *highpc)
5982             *highpc = pdi->highpc;
5983           if (need_pc)
5984             {
5985               CORE_ADDR baseaddr;
5986               struct objfile *objfile = cu->objfile;
5987
5988               baseaddr = ANOFFSET (objfile->section_offsets,
5989                                    SECT_OFF_TEXT (objfile));
5990               addrmap_set_empty (objfile->psymtabs_addrmap,
5991                                  pdi->lowpc + baseaddr,
5992                                  pdi->highpc - 1 + baseaddr,
5993                                  cu->per_cu->v.psymtab);
5994             }
5995         }
5996
5997       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5998         {
5999           if (!pdi->is_declaration)
6000             /* Ignore subprogram DIEs that do not have a name, they are
6001                illegal.  Do not emit a complaint at this point, we will
6002                do so when we convert this psymtab into a symtab.  */
6003             if (pdi->name)
6004               add_partial_symbol (pdi, cu);
6005         }
6006     }
6007
6008   if (! pdi->has_children)
6009     return;
6010
6011   if (cu->language == language_ada)
6012     {
6013       pdi = pdi->die_child;
6014       while (pdi != NULL)
6015         {
6016           fixup_partial_die (pdi, cu);
6017           if (pdi->tag == DW_TAG_subprogram
6018               || pdi->tag == DW_TAG_lexical_block)
6019             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6020           pdi = pdi->die_sibling;
6021         }
6022     }
6023 }
6024
6025 /* Read a partial die corresponding to an enumeration type.  */
6026
6027 static void
6028 add_partial_enumeration (struct partial_die_info *enum_pdi,
6029                          struct dwarf2_cu *cu)
6030 {
6031   struct partial_die_info *pdi;
6032
6033   if (enum_pdi->name != NULL)
6034     add_partial_symbol (enum_pdi, cu);
6035
6036   pdi = enum_pdi->die_child;
6037   while (pdi)
6038     {
6039       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6040         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6041       else
6042         add_partial_symbol (pdi, cu);
6043       pdi = pdi->die_sibling;
6044     }
6045 }
6046
6047 /* Return the initial uleb128 in the die at INFO_PTR.  */
6048
6049 static unsigned int
6050 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6051 {
6052   unsigned int bytes_read;
6053
6054   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6055 }
6056
6057 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6058    Return the corresponding abbrev, or NULL if the number is zero (indicating
6059    an empty DIE).  In either case *BYTES_READ will be set to the length of
6060    the initial number.  */
6061
6062 static struct abbrev_info *
6063 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6064                  struct dwarf2_cu *cu)
6065 {
6066   bfd *abfd = cu->objfile->obfd;
6067   unsigned int abbrev_number;
6068   struct abbrev_info *abbrev;
6069
6070   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6071
6072   if (abbrev_number == 0)
6073     return NULL;
6074
6075   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6076   if (!abbrev)
6077     {
6078       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6079              abbrev_number, bfd_get_filename (abfd));
6080     }
6081
6082   return abbrev;
6083 }
6084
6085 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6086    Returns a pointer to the end of a series of DIEs, terminated by an empty
6087    DIE.  Any children of the skipped DIEs will also be skipped.  */
6088
6089 static gdb_byte *
6090 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6091 {
6092   struct dwarf2_cu *cu = reader->cu;
6093   struct abbrev_info *abbrev;
6094   unsigned int bytes_read;
6095
6096   while (1)
6097     {
6098       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6099       if (abbrev == NULL)
6100         return info_ptr + bytes_read;
6101       else
6102         info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6103     }
6104 }
6105
6106 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6107    INFO_PTR should point just after the initial uleb128 of a DIE, and the
6108    abbrev corresponding to that skipped uleb128 should be passed in
6109    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
6110    children.  */
6111
6112 static gdb_byte *
6113 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6114               struct abbrev_info *abbrev)
6115 {
6116   unsigned int bytes_read;
6117   struct attribute attr;
6118   bfd *abfd = reader->abfd;
6119   struct dwarf2_cu *cu = reader->cu;
6120   gdb_byte *buffer = reader->buffer;
6121   const gdb_byte *buffer_end = reader->buffer_end;
6122   gdb_byte *start_info_ptr = info_ptr;
6123   unsigned int form, i;
6124
6125   for (i = 0; i < abbrev->num_attrs; i++)
6126     {
6127       /* The only abbrev we care about is DW_AT_sibling.  */
6128       if (abbrev->attrs[i].name == DW_AT_sibling)
6129         {
6130           read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6131           if (attr.form == DW_FORM_ref_addr)
6132             complaint (&symfile_complaints,
6133                        _("ignoring absolute DW_AT_sibling"));
6134           else
6135             return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6136         }
6137
6138       /* If it isn't DW_AT_sibling, skip this attribute.  */
6139       form = abbrev->attrs[i].form;
6140     skip_attribute:
6141       switch (form)
6142         {
6143         case DW_FORM_ref_addr:
6144           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6145              and later it is offset sized.  */
6146           if (cu->header.version == 2)
6147             info_ptr += cu->header.addr_size;
6148           else
6149             info_ptr += cu->header.offset_size;
6150           break;
6151         case DW_FORM_GNU_ref_alt:
6152           info_ptr += cu->header.offset_size;
6153           break;
6154         case DW_FORM_addr:
6155           info_ptr += cu->header.addr_size;
6156           break;
6157         case DW_FORM_data1:
6158         case DW_FORM_ref1:
6159         case DW_FORM_flag:
6160           info_ptr += 1;
6161           break;
6162         case DW_FORM_flag_present:
6163           break;
6164         case DW_FORM_data2:
6165         case DW_FORM_ref2:
6166           info_ptr += 2;
6167           break;
6168         case DW_FORM_data4:
6169         case DW_FORM_ref4:
6170           info_ptr += 4;
6171           break;
6172         case DW_FORM_data8:
6173         case DW_FORM_ref8:
6174         case DW_FORM_ref_sig8:
6175           info_ptr += 8;
6176           break;
6177         case DW_FORM_string:
6178           read_direct_string (abfd, info_ptr, &bytes_read);
6179           info_ptr += bytes_read;
6180           break;
6181         case DW_FORM_sec_offset:
6182         case DW_FORM_strp:
6183         case DW_FORM_GNU_strp_alt:
6184           info_ptr += cu->header.offset_size;
6185           break;
6186         case DW_FORM_exprloc:
6187         case DW_FORM_block:
6188           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6189           info_ptr += bytes_read;
6190           break;
6191         case DW_FORM_block1:
6192           info_ptr += 1 + read_1_byte (abfd, info_ptr);
6193           break;
6194         case DW_FORM_block2:
6195           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6196           break;
6197         case DW_FORM_block4:
6198           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6199           break;
6200         case DW_FORM_sdata:
6201         case DW_FORM_udata:
6202         case DW_FORM_ref_udata:
6203         case DW_FORM_GNU_addr_index:
6204         case DW_FORM_GNU_str_index:
6205           info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6206           break;
6207         case DW_FORM_indirect:
6208           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6209           info_ptr += bytes_read;
6210           /* We need to continue parsing from here, so just go back to
6211              the top.  */
6212           goto skip_attribute;
6213
6214         default:
6215           error (_("Dwarf Error: Cannot handle %s "
6216                    "in DWARF reader [in module %s]"),
6217                  dwarf_form_name (form),
6218                  bfd_get_filename (abfd));
6219         }
6220     }
6221
6222   if (abbrev->has_children)
6223     return skip_children (reader, info_ptr);
6224   else
6225     return info_ptr;
6226 }
6227
6228 /* Locate ORIG_PDI's sibling.
6229    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */
6230
6231 static gdb_byte *
6232 locate_pdi_sibling (const struct die_reader_specs *reader,
6233                     struct partial_die_info *orig_pdi,
6234                     gdb_byte *info_ptr)
6235 {
6236   /* Do we know the sibling already?  */
6237
6238   if (orig_pdi->sibling)
6239     return orig_pdi->sibling;
6240
6241   /* Are there any children to deal with?  */
6242
6243   if (!orig_pdi->has_children)
6244     return info_ptr;
6245
6246   /* Skip the children the long way.  */
6247
6248   return skip_children (reader, info_ptr);
6249 }
6250
6251 /* Expand this partial symbol table into a full symbol table.  */
6252
6253 static void
6254 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6255 {
6256   if (pst != NULL)
6257     {
6258       if (pst->readin)
6259         {
6260           warning (_("bug: psymtab for %s is already read in."),
6261                    pst->filename);
6262         }
6263       else
6264         {
6265           if (info_verbose)
6266             {
6267               printf_filtered (_("Reading in symbols for %s..."),
6268                                pst->filename);
6269               gdb_flush (gdb_stdout);
6270             }
6271
6272           /* Restore our global data.  */
6273           dwarf2_per_objfile = objfile_data (pst->objfile,
6274                                              dwarf2_objfile_data_key);
6275
6276           /* If this psymtab is constructed from a debug-only objfile, the
6277              has_section_at_zero flag will not necessarily be correct.  We
6278              can get the correct value for this flag by looking at the data
6279              associated with the (presumably stripped) associated objfile.  */
6280           if (pst->objfile->separate_debug_objfile_backlink)
6281             {
6282               struct dwarf2_per_objfile *dpo_backlink
6283                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6284                                 dwarf2_objfile_data_key);
6285
6286               dwarf2_per_objfile->has_section_at_zero
6287                 = dpo_backlink->has_section_at_zero;
6288             }
6289
6290           dwarf2_per_objfile->reading_partial_symbols = 0;
6291
6292           psymtab_to_symtab_1 (pst);
6293
6294           /* Finish up the debug error message.  */
6295           if (info_verbose)
6296             printf_filtered (_("done.\n"));
6297         }
6298     }
6299
6300   process_cu_includes ();
6301 }
6302 \f
6303 /* Reading in full CUs.  */
6304
6305 /* Add PER_CU to the queue.  */
6306
6307 static void
6308 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6309                  enum language pretend_language)
6310 {
6311   struct dwarf2_queue_item *item;
6312
6313   per_cu->queued = 1;
6314   item = xmalloc (sizeof (*item));
6315   item->per_cu = per_cu;
6316   item->pretend_language = pretend_language;
6317   item->next = NULL;
6318
6319   if (dwarf2_queue == NULL)
6320     dwarf2_queue = item;
6321   else
6322     dwarf2_queue_tail->next = item;
6323
6324   dwarf2_queue_tail = item;
6325 }
6326
6327 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
6328    unit and add it to our queue.
6329    The result is non-zero if PER_CU was queued, otherwise the result is zero
6330    meaning either PER_CU is already queued or it is already loaded.  */
6331
6332 static int
6333 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6334                        struct dwarf2_per_cu_data *per_cu,
6335                        enum language pretend_language)
6336 {
6337   /* We may arrive here during partial symbol reading, if we need full
6338      DIEs to process an unusual case (e.g. template arguments).  Do
6339      not queue PER_CU, just tell our caller to load its DIEs.  */
6340   if (dwarf2_per_objfile->reading_partial_symbols)
6341     {
6342       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6343         return 1;
6344       return 0;
6345     }
6346
6347   /* Mark the dependence relation so that we don't flush PER_CU
6348      too early.  */
6349   dwarf2_add_dependence (this_cu, per_cu);
6350
6351   /* If it's already on the queue, we have nothing to do.  */
6352   if (per_cu->queued)
6353     return 0;
6354
6355   /* If the compilation unit is already loaded, just mark it as
6356      used.  */
6357   if (per_cu->cu != NULL)
6358     {
6359       per_cu->cu->last_used = 0;
6360       return 0;
6361     }
6362
6363   /* Add it to the queue.  */
6364   queue_comp_unit (per_cu, pretend_language);
6365
6366   return 1;
6367 }
6368
6369 /* Process the queue.  */
6370
6371 static void
6372 process_queue (void)
6373 {
6374   struct dwarf2_queue_item *item, *next_item;
6375
6376   if (dwarf2_read_debug)
6377     {
6378       fprintf_unfiltered (gdb_stdlog,
6379                           "Expanding one or more symtabs of objfile %s ...\n",
6380                           dwarf2_per_objfile->objfile->name);
6381     }
6382
6383   /* The queue starts out with one item, but following a DIE reference
6384      may load a new CU, adding it to the end of the queue.  */
6385   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6386     {
6387       if (dwarf2_per_objfile->using_index
6388           ? !item->per_cu->v.quick->symtab
6389           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6390         {
6391           struct dwarf2_per_cu_data *per_cu = item->per_cu;
6392
6393           if (dwarf2_read_debug)
6394             {
6395               fprintf_unfiltered (gdb_stdlog,
6396                                   "Expanding symtab of %s at offset 0x%x\n",
6397                                   per_cu->is_debug_types ? "TU" : "CU",
6398                                   per_cu->offset.sect_off);
6399             }
6400
6401           if (per_cu->is_debug_types)
6402             process_full_type_unit (per_cu, item->pretend_language);
6403           else
6404             process_full_comp_unit (per_cu, item->pretend_language);
6405
6406           if (dwarf2_read_debug)
6407             {
6408               fprintf_unfiltered (gdb_stdlog,
6409                                   "Done expanding %s at offset 0x%x\n",
6410                                   per_cu->is_debug_types ? "TU" : "CU",
6411                                   per_cu->offset.sect_off);
6412             }
6413         }
6414
6415       item->per_cu->queued = 0;
6416       next_item = item->next;
6417       xfree (item);
6418     }
6419
6420   dwarf2_queue_tail = NULL;
6421
6422   if (dwarf2_read_debug)
6423     {
6424       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6425                           dwarf2_per_objfile->objfile->name);
6426     }
6427 }
6428
6429 /* Free all allocated queue entries.  This function only releases anything if
6430    an error was thrown; if the queue was processed then it would have been
6431    freed as we went along.  */
6432
6433 static void
6434 dwarf2_release_queue (void *dummy)
6435 {
6436   struct dwarf2_queue_item *item, *last;
6437
6438   item = dwarf2_queue;
6439   while (item)
6440     {
6441       /* Anything still marked queued is likely to be in an
6442          inconsistent state, so discard it.  */
6443       if (item->per_cu->queued)
6444         {
6445           if (item->per_cu->cu != NULL)
6446             free_one_cached_comp_unit (item->per_cu);
6447           item->per_cu->queued = 0;
6448         }
6449
6450       last = item;
6451       item = item->next;
6452       xfree (last);
6453     }
6454
6455   dwarf2_queue = dwarf2_queue_tail = NULL;
6456 }
6457
6458 /* Read in full symbols for PST, and anything it depends on.  */
6459
6460 static void
6461 psymtab_to_symtab_1 (struct partial_symtab *pst)
6462 {
6463   struct dwarf2_per_cu_data *per_cu;
6464   int i;
6465
6466   if (pst->readin)
6467     return;
6468
6469   for (i = 0; i < pst->number_of_dependencies; i++)
6470     if (!pst->dependencies[i]->readin
6471         && pst->dependencies[i]->user == NULL)
6472       {
6473         /* Inform about additional files that need to be read in.  */
6474         if (info_verbose)
6475           {
6476             /* FIXME: i18n: Need to make this a single string.  */
6477             fputs_filtered (" ", gdb_stdout);
6478             wrap_here ("");
6479             fputs_filtered ("and ", gdb_stdout);
6480             wrap_here ("");
6481             printf_filtered ("%s...", pst->dependencies[i]->filename);
6482             wrap_here ("");     /* Flush output.  */
6483             gdb_flush (gdb_stdout);
6484           }
6485         psymtab_to_symtab_1 (pst->dependencies[i]);
6486       }
6487
6488   per_cu = pst->read_symtab_private;
6489
6490   if (per_cu == NULL)
6491     {
6492       /* It's an include file, no symbols to read for it.
6493          Everything is in the parent symtab.  */
6494       pst->readin = 1;
6495       return;
6496     }
6497
6498   dw2_do_instantiate_symtab (per_cu);
6499 }
6500
6501 /* Trivial hash function for die_info: the hash value of a DIE
6502    is its offset in .debug_info for this objfile.  */
6503
6504 static hashval_t
6505 die_hash (const void *item)
6506 {
6507   const struct die_info *die = item;
6508
6509   return die->offset.sect_off;
6510 }
6511
6512 /* Trivial comparison function for die_info structures: two DIEs
6513    are equal if they have the same offset.  */
6514
6515 static int
6516 die_eq (const void *item_lhs, const void *item_rhs)
6517 {
6518   const struct die_info *die_lhs = item_lhs;
6519   const struct die_info *die_rhs = item_rhs;
6520
6521   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6522 }
6523
6524 /* die_reader_func for load_full_comp_unit.
6525    This is identical to read_signatured_type_reader,
6526    but is kept separate for now.  */
6527
6528 static void
6529 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6530                             gdb_byte *info_ptr,
6531                             struct die_info *comp_unit_die,
6532                             int has_children,
6533                             void *data)
6534 {
6535   struct dwarf2_cu *cu = reader->cu;
6536   enum language *language_ptr = data;
6537
6538   gdb_assert (cu->die_hash == NULL);
6539   cu->die_hash =
6540     htab_create_alloc_ex (cu->header.length / 12,
6541                           die_hash,
6542                           die_eq,
6543                           NULL,
6544                           &cu->comp_unit_obstack,
6545                           hashtab_obstack_allocate,
6546                           dummy_obstack_deallocate);
6547
6548   if (has_children)
6549     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6550                                                   &info_ptr, comp_unit_die);
6551   cu->dies = comp_unit_die;
6552   /* comp_unit_die is not stored in die_hash, no need.  */
6553
6554   /* We try not to read any attributes in this function, because not
6555      all CUs needed for references have been loaded yet, and symbol
6556      table processing isn't initialized.  But we have to set the CU language,
6557      or we won't be able to build types correctly.
6558      Similarly, if we do not read the producer, we can not apply
6559      producer-specific interpretation.  */
6560   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6561 }
6562
6563 /* Load the DIEs associated with PER_CU into memory.  */
6564
6565 static void
6566 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6567                      enum language pretend_language)
6568 {
6569   gdb_assert (! this_cu->is_debug_types);
6570
6571   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6572                            load_full_comp_unit_reader, &pretend_language);
6573 }
6574
6575 /* Add a DIE to the delayed physname list.  */
6576
6577 static void
6578 add_to_method_list (struct type *type, int fnfield_index, int index,
6579                     const char *name, struct die_info *die,
6580                     struct dwarf2_cu *cu)
6581 {
6582   struct delayed_method_info mi;
6583   mi.type = type;
6584   mi.fnfield_index = fnfield_index;
6585   mi.index = index;
6586   mi.name = name;
6587   mi.die = die;
6588   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6589 }
6590
6591 /* A cleanup for freeing the delayed method list.  */
6592
6593 static void
6594 free_delayed_list (void *ptr)
6595 {
6596   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6597   if (cu->method_list != NULL)
6598     {
6599       VEC_free (delayed_method_info, cu->method_list);
6600       cu->method_list = NULL;
6601     }
6602 }
6603
6604 /* Compute the physnames of any methods on the CU's method list.
6605
6606    The computation of method physnames is delayed in order to avoid the
6607    (bad) condition that one of the method's formal parameters is of an as yet
6608    incomplete type.  */
6609
6610 static void
6611 compute_delayed_physnames (struct dwarf2_cu *cu)
6612 {
6613   int i;
6614   struct delayed_method_info *mi;
6615   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6616     {
6617       const char *physname;
6618       struct fn_fieldlist *fn_flp
6619         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6620       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6621       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6622     }
6623 }
6624
6625 /* Go objects should be embedded in a DW_TAG_module DIE,
6626    and it's not clear if/how imported objects will appear.
6627    To keep Go support simple until that's worked out,
6628    go back through what we've read and create something usable.
6629    We could do this while processing each DIE, and feels kinda cleaner,
6630    but that way is more invasive.
6631    This is to, for example, allow the user to type "p var" or "b main"
6632    without having to specify the package name, and allow lookups
6633    of module.object to work in contexts that use the expression
6634    parser.  */
6635
6636 static void
6637 fixup_go_packaging (struct dwarf2_cu *cu)
6638 {
6639   char *package_name = NULL;
6640   struct pending *list;
6641   int i;
6642
6643   for (list = global_symbols; list != NULL; list = list->next)
6644     {
6645       for (i = 0; i < list->nsyms; ++i)
6646         {
6647           struct symbol *sym = list->symbol[i];
6648
6649           if (SYMBOL_LANGUAGE (sym) == language_go
6650               && SYMBOL_CLASS (sym) == LOC_BLOCK)
6651             {
6652               char *this_package_name = go_symbol_package_name (sym);
6653
6654               if (this_package_name == NULL)
6655                 continue;
6656               if (package_name == NULL)
6657                 package_name = this_package_name;
6658               else
6659                 {
6660                   if (strcmp (package_name, this_package_name) != 0)
6661                     complaint (&symfile_complaints,
6662                                _("Symtab %s has objects from two different Go packages: %s and %s"),
6663                                (sym->symtab && sym->symtab->filename
6664                                 ? sym->symtab->filename
6665                                 : cu->objfile->name),
6666                                this_package_name, package_name);
6667                   xfree (this_package_name);
6668                 }
6669             }
6670         }
6671     }
6672
6673   if (package_name != NULL)
6674     {
6675       struct objfile *objfile = cu->objfile;
6676       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6677                                      package_name, objfile);
6678       struct symbol *sym;
6679
6680       TYPE_TAG_NAME (type) = TYPE_NAME (type);
6681
6682       sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6683       SYMBOL_SET_LANGUAGE (sym, language_go);
6684       SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6685       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6686          e.g., "main" finds the "main" module and not C's main().  */
6687       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6688       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6689       SYMBOL_TYPE (sym) = type;
6690
6691       add_symbol_to_list (sym, &global_symbols);
6692
6693       xfree (package_name);
6694     }
6695 }
6696
6697 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6698
6699 /* Return the symtab for PER_CU.  This works properly regardless of
6700    whether we're using the index or psymtabs.  */
6701
6702 static struct symtab *
6703 get_symtab (struct dwarf2_per_cu_data *per_cu)
6704 {
6705   return (dwarf2_per_objfile->using_index
6706           ? per_cu->v.quick->symtab
6707           : per_cu->v.psymtab->symtab);
6708 }
6709
6710 /* A helper function for computing the list of all symbol tables
6711    included by PER_CU.  */
6712
6713 static void
6714 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6715                                 htab_t all_children,
6716                                 struct dwarf2_per_cu_data *per_cu)
6717 {
6718   void **slot;
6719   int ix;
6720   struct dwarf2_per_cu_data *iter;
6721
6722   slot = htab_find_slot (all_children, per_cu, INSERT);
6723   if (*slot != NULL)
6724     {
6725       /* This inclusion and its children have been processed.  */
6726       return;
6727     }
6728
6729   *slot = per_cu;
6730   /* Only add a CU if it has a symbol table.  */
6731   if (get_symtab (per_cu) != NULL)
6732     VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6733
6734   for (ix = 0;
6735        VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6736        ++ix)
6737     recursively_compute_inclusions (result, all_children, iter);
6738 }
6739
6740 /* Compute the symtab 'includes' fields for the symtab related to
6741    PER_CU.  */
6742
6743 static void
6744 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6745 {
6746   gdb_assert (! per_cu->is_debug_types);
6747
6748   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6749     {
6750       int ix, len;
6751       struct dwarf2_per_cu_data *iter;
6752       VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6753       htab_t all_children;
6754       struct symtab *symtab = get_symtab (per_cu);
6755
6756       /* If we don't have a symtab, we can just skip this case.  */
6757       if (symtab == NULL)
6758         return;
6759
6760       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6761                                         NULL, xcalloc, xfree);
6762
6763       for (ix = 0;
6764            VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6765                         ix, iter);
6766            ++ix)
6767         recursively_compute_inclusions (&result_children, all_children, iter);
6768
6769       /* Now we have a transitive closure of all the included CUs, so
6770          we can convert it to a list of symtabs.  */
6771       len = VEC_length (dwarf2_per_cu_ptr, result_children);
6772       symtab->includes
6773         = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6774                          (len + 1) * sizeof (struct symtab *));
6775       for (ix = 0;
6776            VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6777            ++ix)
6778         symtab->includes[ix] = get_symtab (iter);
6779       symtab->includes[len] = NULL;
6780
6781       VEC_free (dwarf2_per_cu_ptr, result_children);
6782       htab_delete (all_children);
6783     }
6784 }
6785
6786 /* Compute the 'includes' field for the symtabs of all the CUs we just
6787    read.  */
6788
6789 static void
6790 process_cu_includes (void)
6791 {
6792   int ix;
6793   struct dwarf2_per_cu_data *iter;
6794
6795   for (ix = 0;
6796        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6797                     ix, iter);
6798        ++ix)
6799     {
6800       if (! iter->is_debug_types)
6801         compute_symtab_includes (iter);
6802     }
6803
6804   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6805 }
6806
6807 /* Generate full symbol information for PER_CU, whose DIEs have
6808    already been loaded into memory.  */
6809
6810 static void
6811 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6812                         enum language pretend_language)
6813 {
6814   struct dwarf2_cu *cu = per_cu->cu;
6815   struct objfile *objfile = per_cu->objfile;
6816   CORE_ADDR lowpc, highpc;
6817   struct symtab *symtab;
6818   struct cleanup *back_to, *delayed_list_cleanup;
6819   CORE_ADDR baseaddr;
6820   struct block *static_block;
6821
6822   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6823
6824   buildsym_init ();
6825   back_to = make_cleanup (really_free_pendings, NULL);
6826   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6827
6828   cu->list_in_scope = &file_symbols;
6829
6830   cu->language = pretend_language;
6831   cu->language_defn = language_def (cu->language);
6832
6833   /* Do line number decoding in read_file_scope () */
6834   process_die (cu->dies, cu);
6835
6836   /* For now fudge the Go package.  */
6837   if (cu->language == language_go)
6838     fixup_go_packaging (cu);
6839
6840   /* Now that we have processed all the DIEs in the CU, all the types 
6841      should be complete, and it should now be safe to compute all of the
6842      physnames.  */
6843   compute_delayed_physnames (cu);
6844   do_cleanups (delayed_list_cleanup);
6845
6846   /* Some compilers don't define a DW_AT_high_pc attribute for the
6847      compilation unit.  If the DW_AT_high_pc is missing, synthesize
6848      it, by scanning the DIE's below the compilation unit.  */
6849   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6850
6851   static_block
6852     = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6853                                    per_cu->s.imported_symtabs != NULL);
6854
6855   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6856      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6857      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
6858      addrmap to help ensure it has an accurate map of pc values belonging to
6859      this comp unit.  */
6860   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6861
6862   symtab = end_symtab_from_static_block (static_block, objfile,
6863                                          SECT_OFF_TEXT (objfile), 0);
6864
6865   if (symtab != NULL)
6866     {
6867       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6868
6869       /* Set symtab language to language from DW_AT_language.  If the
6870          compilation is from a C file generated by language preprocessors, do
6871          not set the language if it was already deduced by start_subfile.  */
6872       if (!(cu->language == language_c && symtab->language != language_c))
6873         symtab->language = cu->language;
6874
6875       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
6876          produce DW_AT_location with location lists but it can be possibly
6877          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
6878          there were bugs in prologue debug info, fixed later in GCC-4.5
6879          by "unwind info for epilogues" patch (which is not directly related).
6880
6881          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6882          needed, it would be wrong due to missing DW_AT_producer there.
6883
6884          Still one can confuse GDB by using non-standard GCC compilation
6885          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6886          */ 
6887       if (cu->has_loclist && gcc_4_minor >= 5)
6888         symtab->locations_valid = 1;
6889
6890       if (gcc_4_minor >= 5)
6891         symtab->epilogue_unwind_valid = 1;
6892
6893       symtab->call_site_htab = cu->call_site_htab;
6894     }
6895
6896   if (dwarf2_per_objfile->using_index)
6897     per_cu->v.quick->symtab = symtab;
6898   else
6899     {
6900       struct partial_symtab *pst = per_cu->v.psymtab;
6901       pst->symtab = symtab;
6902       pst->readin = 1;
6903     }
6904
6905   /* Push it for inclusion processing later.  */
6906   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6907
6908   do_cleanups (back_to);
6909 }
6910
6911 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6912    already been loaded into memory.  */
6913
6914 static void
6915 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6916                         enum language pretend_language)
6917 {
6918   struct dwarf2_cu *cu = per_cu->cu;
6919   struct objfile *objfile = per_cu->objfile;
6920   struct symtab *symtab;
6921   struct cleanup *back_to, *delayed_list_cleanup;
6922
6923   buildsym_init ();
6924   back_to = make_cleanup (really_free_pendings, NULL);
6925   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6926
6927   cu->list_in_scope = &file_symbols;
6928
6929   cu->language = pretend_language;
6930   cu->language_defn = language_def (cu->language);
6931
6932   /* The symbol tables are set up in read_type_unit_scope.  */
6933   process_die (cu->dies, cu);
6934
6935   /* For now fudge the Go package.  */
6936   if (cu->language == language_go)
6937     fixup_go_packaging (cu);
6938
6939   /* Now that we have processed all the DIEs in the CU, all the types 
6940      should be complete, and it should now be safe to compute all of the
6941      physnames.  */
6942   compute_delayed_physnames (cu);
6943   do_cleanups (delayed_list_cleanup);
6944
6945   /* TUs share symbol tables.
6946      If this is the first TU to use this symtab, complete the construction
6947      of it with end_expandable_symtab.  Otherwise, complete the addition of
6948      this TU's symbols to the existing symtab.  */
6949   if (per_cu->s.type_unit_group->primary_symtab == NULL)
6950     {
6951       symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6952       per_cu->s.type_unit_group->primary_symtab = symtab;
6953
6954       if (symtab != NULL)
6955         {
6956           /* Set symtab language to language from DW_AT_language.  If the
6957              compilation is from a C file generated by language preprocessors,
6958              do not set the language if it was already deduced by
6959              start_subfile.  */
6960           if (!(cu->language == language_c && symtab->language != language_c))
6961             symtab->language = cu->language;
6962         }
6963     }
6964   else
6965     {
6966       augment_type_symtab (objfile,
6967                            per_cu->s.type_unit_group->primary_symtab);
6968       symtab = per_cu->s.type_unit_group->primary_symtab;
6969     }
6970
6971   if (dwarf2_per_objfile->using_index)
6972     per_cu->v.quick->symtab = symtab;
6973   else
6974     {
6975       struct partial_symtab *pst = per_cu->v.psymtab;
6976       pst->symtab = symtab;
6977       pst->readin = 1;
6978     }
6979
6980   do_cleanups (back_to);
6981 }
6982
6983 /* Process an imported unit DIE.  */
6984
6985 static void
6986 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6987 {
6988   struct attribute *attr;
6989
6990   /* For now we don't handle imported units in type units.  */
6991   if (cu->per_cu->is_debug_types)
6992     {
6993       error (_("Dwarf Error: DW_TAG_imported_unit is not"
6994                " supported in type units [in module %s]"),
6995              cu->objfile->name);
6996     }
6997
6998   attr = dwarf2_attr (die, DW_AT_import, cu);
6999   if (attr != NULL)
7000     {
7001       struct dwarf2_per_cu_data *per_cu;
7002       struct symtab *imported_symtab;
7003       sect_offset offset;
7004       int is_dwz;
7005
7006       offset = dwarf2_get_ref_die_offset (attr);
7007       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7008       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7009
7010       /* Queue the unit, if needed.  */
7011       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7012         load_full_comp_unit (per_cu, cu->language);
7013
7014       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7015                      per_cu);
7016     }
7017 }
7018
7019 /* Process a die and its children.  */
7020
7021 static void
7022 process_die (struct die_info *die, struct dwarf2_cu *cu)
7023 {
7024   switch (die->tag)
7025     {
7026     case DW_TAG_padding:
7027       break;
7028     case DW_TAG_compile_unit:
7029     case DW_TAG_partial_unit:
7030       read_file_scope (die, cu);
7031       break;
7032     case DW_TAG_type_unit:
7033       read_type_unit_scope (die, cu);
7034       break;
7035     case DW_TAG_subprogram:
7036     case DW_TAG_inlined_subroutine:
7037       read_func_scope (die, cu);
7038       break;
7039     case DW_TAG_lexical_block:
7040     case DW_TAG_try_block:
7041     case DW_TAG_catch_block:
7042       read_lexical_block_scope (die, cu);
7043       break;
7044     case DW_TAG_GNU_call_site:
7045       read_call_site_scope (die, cu);
7046       break;
7047     case DW_TAG_class_type:
7048     case DW_TAG_interface_type:
7049     case DW_TAG_structure_type:
7050     case DW_TAG_union_type:
7051       process_structure_scope (die, cu);
7052       break;
7053     case DW_TAG_enumeration_type:
7054       process_enumeration_scope (die, cu);
7055       break;
7056
7057     /* These dies have a type, but processing them does not create
7058        a symbol or recurse to process the children.  Therefore we can
7059        read them on-demand through read_type_die.  */
7060     case DW_TAG_subroutine_type:
7061     case DW_TAG_set_type:
7062     case DW_TAG_array_type:
7063     case DW_TAG_pointer_type:
7064     case DW_TAG_ptr_to_member_type:
7065     case DW_TAG_reference_type:
7066     case DW_TAG_string_type:
7067       break;
7068
7069     case DW_TAG_base_type:
7070     case DW_TAG_subrange_type:
7071     case DW_TAG_typedef:
7072       /* Add a typedef symbol for the type definition, if it has a
7073          DW_AT_name.  */
7074       new_symbol (die, read_type_die (die, cu), cu);
7075       break;
7076     case DW_TAG_common_block:
7077       read_common_block (die, cu);
7078       break;
7079     case DW_TAG_common_inclusion:
7080       break;
7081     case DW_TAG_namespace:
7082       processing_has_namespace_info = 1;
7083       read_namespace (die, cu);
7084       break;
7085     case DW_TAG_module:
7086       processing_has_namespace_info = 1;
7087       read_module (die, cu);
7088       break;
7089     case DW_TAG_imported_declaration:
7090     case DW_TAG_imported_module:
7091       processing_has_namespace_info = 1;
7092       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7093                                  || cu->language != language_fortran))
7094         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7095                    dwarf_tag_name (die->tag));
7096       read_import_statement (die, cu);
7097       break;
7098
7099     case DW_TAG_imported_unit:
7100       process_imported_unit_die (die, cu);
7101       break;
7102
7103     default:
7104       new_symbol (die, NULL, cu);
7105       break;
7106     }
7107 }
7108
7109 /* A helper function for dwarf2_compute_name which determines whether DIE
7110    needs to have the name of the scope prepended to the name listed in the
7111    die.  */
7112
7113 static int
7114 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7115 {
7116   struct attribute *attr;
7117
7118   switch (die->tag)
7119     {
7120     case DW_TAG_namespace:
7121     case DW_TAG_typedef:
7122     case DW_TAG_class_type:
7123     case DW_TAG_interface_type:
7124     case DW_TAG_structure_type:
7125     case DW_TAG_union_type:
7126     case DW_TAG_enumeration_type:
7127     case DW_TAG_enumerator:
7128     case DW_TAG_subprogram:
7129     case DW_TAG_member:
7130       return 1;
7131
7132     case DW_TAG_variable:
7133     case DW_TAG_constant:
7134       /* We only need to prefix "globally" visible variables.  These include
7135          any variable marked with DW_AT_external or any variable that
7136          lives in a namespace.  [Variables in anonymous namespaces
7137          require prefixing, but they are not DW_AT_external.]  */
7138
7139       if (dwarf2_attr (die, DW_AT_specification, cu))
7140         {
7141           struct dwarf2_cu *spec_cu = cu;
7142
7143           return die_needs_namespace (die_specification (die, &spec_cu),
7144                                       spec_cu);
7145         }
7146
7147       attr = dwarf2_attr (die, DW_AT_external, cu);
7148       if (attr == NULL && die->parent->tag != DW_TAG_namespace
7149           && die->parent->tag != DW_TAG_module)
7150         return 0;
7151       /* A variable in a lexical block of some kind does not need a
7152          namespace, even though in C++ such variables may be external
7153          and have a mangled name.  */
7154       if (die->parent->tag ==  DW_TAG_lexical_block
7155           || die->parent->tag ==  DW_TAG_try_block
7156           || die->parent->tag ==  DW_TAG_catch_block
7157           || die->parent->tag == DW_TAG_subprogram)
7158         return 0;
7159       return 1;
7160
7161     default:
7162       return 0;
7163     }
7164 }
7165
7166 /* Retrieve the last character from a mem_file.  */
7167
7168 static void
7169 do_ui_file_peek_last (void *object, const char *buffer, long length)
7170 {
7171   char *last_char_p = (char *) object;
7172
7173   if (length > 0)
7174     *last_char_p = buffer[length - 1];
7175 }
7176
7177 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
7178    compute the physname for the object, which include a method's:
7179    - formal parameters (C++/Java),
7180    - receiver type (Go),
7181    - return type (Java).
7182
7183    The term "physname" is a bit confusing.
7184    For C++, for example, it is the demangled name.
7185    For Go, for example, it's the mangled name.
7186
7187    For Ada, return the DIE's linkage name rather than the fully qualified
7188    name.  PHYSNAME is ignored..
7189
7190    The result is allocated on the objfile_obstack and canonicalized.  */
7191
7192 static const char *
7193 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7194                      int physname)
7195 {
7196   struct objfile *objfile = cu->objfile;
7197
7198   if (name == NULL)
7199     name = dwarf2_name (die, cu);
7200
7201   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7202      compute it by typename_concat inside GDB.  */
7203   if (cu->language == language_ada
7204       || (cu->language == language_fortran && physname))
7205     {
7206       /* For Ada unit, we prefer the linkage name over the name, as
7207          the former contains the exported name, which the user expects
7208          to be able to reference.  Ideally, we want the user to be able
7209          to reference this entity using either natural or linkage name,
7210          but we haven't started looking at this enhancement yet.  */
7211       struct attribute *attr;
7212
7213       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7214       if (attr == NULL)
7215         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7216       if (attr && DW_STRING (attr))
7217         return DW_STRING (attr);
7218     }
7219
7220   /* These are the only languages we know how to qualify names in.  */
7221   if (name != NULL
7222       && (cu->language == language_cplus || cu->language == language_java
7223           || cu->language == language_fortran))
7224     {
7225       if (die_needs_namespace (die, cu))
7226         {
7227           long length;
7228           const char *prefix;
7229           struct ui_file *buf;
7230
7231           prefix = determine_prefix (die, cu);
7232           buf = mem_fileopen ();
7233           if (*prefix != '\0')
7234             {
7235               char *prefixed_name = typename_concat (NULL, prefix, name,
7236                                                      physname, cu);
7237
7238               fputs_unfiltered (prefixed_name, buf);
7239               xfree (prefixed_name);
7240             }
7241           else
7242             fputs_unfiltered (name, buf);
7243
7244           /* Template parameters may be specified in the DIE's DW_AT_name, or
7245              as children with DW_TAG_template_type_param or
7246              DW_TAG_value_type_param.  If the latter, add them to the name
7247              here.  If the name already has template parameters, then
7248              skip this step; some versions of GCC emit both, and
7249              it is more efficient to use the pre-computed name.
7250
7251              Something to keep in mind about this process: it is very
7252              unlikely, or in some cases downright impossible, to produce
7253              something that will match the mangled name of a function.
7254              If the definition of the function has the same debug info,
7255              we should be able to match up with it anyway.  But fallbacks
7256              using the minimal symbol, for instance to find a method
7257              implemented in a stripped copy of libstdc++, will not work.
7258              If we do not have debug info for the definition, we will have to
7259              match them up some other way.
7260
7261              When we do name matching there is a related problem with function
7262              templates; two instantiated function templates are allowed to
7263              differ only by their return types, which we do not add here.  */
7264
7265           if (cu->language == language_cplus && strchr (name, '<') == NULL)
7266             {
7267               struct attribute *attr;
7268               struct die_info *child;
7269               int first = 1;
7270
7271               die->building_fullname = 1;
7272
7273               for (child = die->child; child != NULL; child = child->sibling)
7274                 {
7275                   struct type *type;
7276                   LONGEST value;
7277                   gdb_byte *bytes;
7278                   struct dwarf2_locexpr_baton *baton;
7279                   struct value *v;
7280
7281                   if (child->tag != DW_TAG_template_type_param
7282                       && child->tag != DW_TAG_template_value_param)
7283                     continue;
7284
7285                   if (first)
7286                     {
7287                       fputs_unfiltered ("<", buf);
7288                       first = 0;
7289                     }
7290                   else
7291                     fputs_unfiltered (", ", buf);
7292
7293                   attr = dwarf2_attr (child, DW_AT_type, cu);
7294                   if (attr == NULL)
7295                     {
7296                       complaint (&symfile_complaints,
7297                                  _("template parameter missing DW_AT_type"));
7298                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
7299                       continue;
7300                     }
7301                   type = die_type (child, cu);
7302
7303                   if (child->tag == DW_TAG_template_type_param)
7304                     {
7305                       c_print_type (type, "", buf, -1, 0);
7306                       continue;
7307                     }
7308
7309                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
7310                   if (attr == NULL)
7311                     {
7312                       complaint (&symfile_complaints,
7313                                  _("template parameter missing "
7314                                    "DW_AT_const_value"));
7315                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
7316                       continue;
7317                     }
7318
7319                   dwarf2_const_value_attr (attr, type, name,
7320                                            &cu->comp_unit_obstack, cu,
7321                                            &value, &bytes, &baton);
7322
7323                   if (TYPE_NOSIGN (type))
7324                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
7325                        changed, this can use value_print instead.  */
7326                     c_printchar (value, type, buf);
7327                   else
7328                     {
7329                       struct value_print_options opts;
7330
7331                       if (baton != NULL)
7332                         v = dwarf2_evaluate_loc_desc (type, NULL,
7333                                                       baton->data,
7334                                                       baton->size,
7335                                                       baton->per_cu);
7336                       else if (bytes != NULL)
7337                         {
7338                           v = allocate_value (type);
7339                           memcpy (value_contents_writeable (v), bytes,
7340                                   TYPE_LENGTH (type));
7341                         }
7342                       else
7343                         v = value_from_longest (type, value);
7344
7345                       /* Specify decimal so that we do not depend on
7346                          the radix.  */
7347                       get_formatted_print_options (&opts, 'd');
7348                       opts.raw = 1;
7349                       value_print (v, buf, &opts);
7350                       release_value (v);
7351                       value_free (v);
7352                     }
7353                 }
7354
7355               die->building_fullname = 0;
7356
7357               if (!first)
7358                 {
7359                   /* Close the argument list, with a space if necessary
7360                      (nested templates).  */
7361                   char last_char = '\0';
7362                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
7363                   if (last_char == '>')
7364                     fputs_unfiltered (" >", buf);
7365                   else
7366                     fputs_unfiltered (">", buf);
7367                 }
7368             }
7369
7370           /* For Java and C++ methods, append formal parameter type
7371              information, if PHYSNAME.  */
7372
7373           if (physname && die->tag == DW_TAG_subprogram
7374               && (cu->language == language_cplus
7375                   || cu->language == language_java))
7376             {
7377               struct type *type = read_type_die (die, cu);
7378
7379               c_type_print_args (type, buf, 1, cu->language);
7380
7381               if (cu->language == language_java)
7382                 {
7383                   /* For java, we must append the return type to method
7384                      names.  */
7385                   if (die->tag == DW_TAG_subprogram)
7386                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7387                                      0, 0);
7388                 }
7389               else if (cu->language == language_cplus)
7390                 {
7391                   /* Assume that an artificial first parameter is
7392                      "this", but do not crash if it is not.  RealView
7393                      marks unnamed (and thus unused) parameters as
7394                      artificial; there is no way to differentiate
7395                      the two cases.  */
7396                   if (TYPE_NFIELDS (type) > 0
7397                       && TYPE_FIELD_ARTIFICIAL (type, 0)
7398                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7399                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7400                                                                         0))))
7401                     fputs_unfiltered (" const", buf);
7402                 }
7403             }
7404
7405           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7406                                        &length);
7407           ui_file_delete (buf);
7408
7409           if (cu->language == language_cplus)
7410             {
7411               char *cname
7412                 = dwarf2_canonicalize_name (name, cu,
7413                                             &objfile->objfile_obstack);
7414
7415               if (cname != NULL)
7416                 name = cname;
7417             }
7418         }
7419     }
7420
7421   return name;
7422 }
7423
7424 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7425    If scope qualifiers are appropriate they will be added.  The result
7426    will be allocated on the objfile_obstack, or NULL if the DIE does
7427    not have a name.  NAME may either be from a previous call to
7428    dwarf2_name or NULL.
7429
7430    The output string will be canonicalized (if C++/Java).  */
7431
7432 static const char *
7433 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7434 {
7435   return dwarf2_compute_name (name, die, cu, 0);
7436 }
7437
7438 /* Construct a physname for the given DIE in CU.  NAME may either be
7439    from a previous call to dwarf2_name or NULL.  The result will be
7440    allocated on the objfile_objstack or NULL if the DIE does not have a
7441    name.
7442
7443    The output string will be canonicalized (if C++/Java).  */
7444
7445 static const char *
7446 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7447 {
7448   struct objfile *objfile = cu->objfile;
7449   struct attribute *attr;
7450   const char *retval, *mangled = NULL, *canon = NULL;
7451   struct cleanup *back_to;
7452   int need_copy = 1;
7453
7454   /* In this case dwarf2_compute_name is just a shortcut not building anything
7455      on its own.  */
7456   if (!die_needs_namespace (die, cu))
7457     return dwarf2_compute_name (name, die, cu, 1);
7458
7459   back_to = make_cleanup (null_cleanup, NULL);
7460
7461   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7462   if (!attr)
7463     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7464
7465   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7466      has computed.  */
7467   if (attr && DW_STRING (attr))
7468     {
7469       char *demangled;
7470
7471       mangled = DW_STRING (attr);
7472
7473       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7474          type.  It is easier for GDB users to search for such functions as
7475          `name(params)' than `long name(params)'.  In such case the minimal
7476          symbol names do not match the full symbol names but for template
7477          functions there is never a need to look up their definition from their
7478          declaration so the only disadvantage remains the minimal symbol
7479          variant `long name(params)' does not have the proper inferior type.
7480          */
7481
7482       if (cu->language == language_go)
7483         {
7484           /* This is a lie, but we already lie to the caller new_symbol_full.
7485              new_symbol_full assumes we return the mangled name.
7486              This just undoes that lie until things are cleaned up.  */
7487           demangled = NULL;
7488         }
7489       else
7490         {
7491           demangled = cplus_demangle (mangled,
7492                                       (DMGL_PARAMS | DMGL_ANSI
7493                                        | (cu->language == language_java
7494                                           ? DMGL_JAVA | DMGL_RET_POSTFIX
7495                                           : DMGL_RET_DROP)));
7496         }
7497       if (demangled)
7498         {
7499           make_cleanup (xfree, demangled);
7500           canon = demangled;
7501         }
7502       else
7503         {
7504           canon = mangled;
7505           need_copy = 0;
7506         }
7507     }
7508
7509   if (canon == NULL || check_physname)
7510     {
7511       const char *physname = dwarf2_compute_name (name, die, cu, 1);
7512
7513       if (canon != NULL && strcmp (physname, canon) != 0)
7514         {
7515           /* It may not mean a bug in GDB.  The compiler could also
7516              compute DW_AT_linkage_name incorrectly.  But in such case
7517              GDB would need to be bug-to-bug compatible.  */
7518
7519           complaint (&symfile_complaints,
7520                      _("Computed physname <%s> does not match demangled <%s> "
7521                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7522                      physname, canon, mangled, die->offset.sect_off, objfile->name);
7523
7524           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7525              is available here - over computed PHYSNAME.  It is safer
7526              against both buggy GDB and buggy compilers.  */
7527
7528           retval = canon;
7529         }
7530       else
7531         {
7532           retval = physname;
7533           need_copy = 0;
7534         }
7535     }
7536   else
7537     retval = canon;
7538
7539   if (need_copy)
7540     retval = obsavestring (retval, strlen (retval),
7541                            &objfile->objfile_obstack);
7542
7543   do_cleanups (back_to);
7544   return retval;
7545 }
7546
7547 /* Read the import statement specified by the given die and record it.  */
7548
7549 static void
7550 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7551 {
7552   struct objfile *objfile = cu->objfile;
7553   struct attribute *import_attr;
7554   struct die_info *imported_die, *child_die;
7555   struct dwarf2_cu *imported_cu;
7556   const char *imported_name;
7557   const char *imported_name_prefix;
7558   const char *canonical_name;
7559   const char *import_alias;
7560   const char *imported_declaration = NULL;
7561   const char *import_prefix;
7562   VEC (const_char_ptr) *excludes = NULL;
7563   struct cleanup *cleanups;
7564
7565   char *temp;
7566
7567   import_attr = dwarf2_attr (die, DW_AT_import, cu);
7568   if (import_attr == NULL)
7569     {
7570       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7571                  dwarf_tag_name (die->tag));
7572       return;
7573     }
7574
7575   imported_cu = cu;
7576   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7577   imported_name = dwarf2_name (imported_die, imported_cu);
7578   if (imported_name == NULL)
7579     {
7580       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7581
7582         The import in the following code:
7583         namespace A
7584           {
7585             typedef int B;
7586           }
7587
7588         int main ()
7589           {
7590             using A::B;
7591             B b;
7592             return b;
7593           }
7594
7595         ...
7596          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7597             <52>   DW_AT_decl_file   : 1
7598             <53>   DW_AT_decl_line   : 6
7599             <54>   DW_AT_import      : <0x75>
7600          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7601             <59>   DW_AT_name        : B
7602             <5b>   DW_AT_decl_file   : 1
7603             <5c>   DW_AT_decl_line   : 2
7604             <5d>   DW_AT_type        : <0x6e>
7605         ...
7606          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7607             <76>   DW_AT_byte_size   : 4
7608             <77>   DW_AT_encoding    : 5        (signed)
7609
7610         imports the wrong die ( 0x75 instead of 0x58 ).
7611         This case will be ignored until the gcc bug is fixed.  */
7612       return;
7613     }
7614
7615   /* Figure out the local name after import.  */
7616   import_alias = dwarf2_name (die, cu);
7617
7618   /* Figure out where the statement is being imported to.  */
7619   import_prefix = determine_prefix (die, cu);
7620
7621   /* Figure out what the scope of the imported die is and prepend it
7622      to the name of the imported die.  */
7623   imported_name_prefix = determine_prefix (imported_die, imported_cu);
7624
7625   if (imported_die->tag != DW_TAG_namespace
7626       && imported_die->tag != DW_TAG_module)
7627     {
7628       imported_declaration = imported_name;
7629       canonical_name = imported_name_prefix;
7630     }
7631   else if (strlen (imported_name_prefix) > 0)
7632     {
7633       temp = alloca (strlen (imported_name_prefix)
7634                      + 2 + strlen (imported_name) + 1);
7635       strcpy (temp, imported_name_prefix);
7636       strcat (temp, "::");
7637       strcat (temp, imported_name);
7638       canonical_name = temp;
7639     }
7640   else
7641     canonical_name = imported_name;
7642
7643   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7644
7645   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7646     for (child_die = die->child; child_die && child_die->tag;
7647          child_die = sibling_die (child_die))
7648       {
7649         /* DWARF-4: A Fortran use statement with a “rename list” may be
7650            represented by an imported module entry with an import attribute
7651            referring to the module and owned entries corresponding to those
7652            entities that are renamed as part of being imported.  */
7653
7654         if (child_die->tag != DW_TAG_imported_declaration)
7655           {
7656             complaint (&symfile_complaints,
7657                        _("child DW_TAG_imported_declaration expected "
7658                          "- DIE at 0x%x [in module %s]"),
7659                        child_die->offset.sect_off, objfile->name);
7660             continue;
7661           }
7662
7663         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7664         if (import_attr == NULL)
7665           {
7666             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7667                        dwarf_tag_name (child_die->tag));
7668             continue;
7669           }
7670
7671         imported_cu = cu;
7672         imported_die = follow_die_ref_or_sig (child_die, import_attr,
7673                                               &imported_cu);
7674         imported_name = dwarf2_name (imported_die, imported_cu);
7675         if (imported_name == NULL)
7676           {
7677             complaint (&symfile_complaints,
7678                        _("child DW_TAG_imported_declaration has unknown "
7679                          "imported name - DIE at 0x%x [in module %s]"),
7680                        child_die->offset.sect_off, objfile->name);
7681             continue;
7682           }
7683
7684         VEC_safe_push (const_char_ptr, excludes, imported_name);
7685
7686         process_die (child_die, cu);
7687       }
7688
7689   cp_add_using_directive (import_prefix,
7690                           canonical_name,
7691                           import_alias,
7692                           imported_declaration,
7693                           excludes,
7694                           &objfile->objfile_obstack);
7695
7696   do_cleanups (cleanups);
7697 }
7698
7699 /* Cleanup function for handle_DW_AT_stmt_list.  */
7700
7701 static void
7702 free_cu_line_header (void *arg)
7703 {
7704   struct dwarf2_cu *cu = arg;
7705
7706   free_line_header (cu->line_header);
7707   cu->line_header = NULL;
7708 }
7709
7710 static void
7711 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7712                          char **name, char **comp_dir)
7713 {
7714   struct attribute *attr;
7715
7716   *name = NULL;
7717   *comp_dir = NULL;
7718
7719   /* Find the filename.  Do not use dwarf2_name here, since the filename
7720      is not a source language identifier.  */
7721   attr = dwarf2_attr (die, DW_AT_name, cu);
7722   if (attr)
7723     {
7724       *name = DW_STRING (attr);
7725     }
7726
7727   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7728   if (attr)
7729     *comp_dir = DW_STRING (attr);
7730   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7731     {
7732       *comp_dir = ldirname (*name);
7733       if (*comp_dir != NULL)
7734         make_cleanup (xfree, *comp_dir);
7735     }
7736   if (*comp_dir != NULL)
7737     {
7738       /* Irix 6.2 native cc prepends <machine>.: to the compilation
7739          directory, get rid of it.  */
7740       char *cp = strchr (*comp_dir, ':');
7741
7742       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7743         *comp_dir = cp + 1;
7744     }
7745
7746   if (*name == NULL)
7747     *name = "<unknown>";
7748 }
7749
7750 /* Handle DW_AT_stmt_list for a compilation unit.
7751    DIE is the DW_TAG_compile_unit die for CU.
7752    COMP_DIR is the compilation directory.
7753    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
7754
7755 static void
7756 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7757                         const char *comp_dir)
7758 {
7759   struct attribute *attr;
7760
7761   gdb_assert (! cu->per_cu->is_debug_types);
7762
7763   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7764   if (attr)
7765     {
7766       unsigned int line_offset = DW_UNSND (attr);
7767       struct line_header *line_header
7768         = dwarf_decode_line_header (line_offset, cu);
7769
7770       if (line_header)
7771         {
7772           cu->line_header = line_header;
7773           make_cleanup (free_cu_line_header, cu);
7774           dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7775         }
7776     }
7777 }
7778
7779 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
7780
7781 static void
7782 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7783 {
7784   struct objfile *objfile = dwarf2_per_objfile->objfile;
7785   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7786   CORE_ADDR lowpc = ((CORE_ADDR) -1);
7787   CORE_ADDR highpc = ((CORE_ADDR) 0);
7788   struct attribute *attr;
7789   char *name = NULL;
7790   char *comp_dir = NULL;
7791   struct die_info *child_die;
7792   bfd *abfd = objfile->obfd;
7793   CORE_ADDR baseaddr;
7794
7795   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7796
7797   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7798
7799   /* If we didn't find a lowpc, set it to highpc to avoid complaints
7800      from finish_block.  */
7801   if (lowpc == ((CORE_ADDR) -1))
7802     lowpc = highpc;
7803   lowpc += baseaddr;
7804   highpc += baseaddr;
7805
7806   find_file_and_directory (die, cu, &name, &comp_dir);
7807
7808   prepare_one_comp_unit (cu, die, cu->language);
7809
7810   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7811      standardised yet.  As a workaround for the language detection we fall
7812      back to the DW_AT_producer string.  */
7813   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7814     cu->language = language_opencl;
7815
7816   /* Similar hack for Go.  */
7817   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7818     set_cu_language (DW_LANG_Go, cu);
7819
7820   dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7821
7822   /* Decode line number information if present.  We do this before
7823      processing child DIEs, so that the line header table is available
7824      for DW_AT_decl_file.  */
7825   handle_DW_AT_stmt_list (die, cu, comp_dir);
7826
7827   /* Process all dies in compilation unit.  */
7828   if (die->child != NULL)
7829     {
7830       child_die = die->child;
7831       while (child_die && child_die->tag)
7832         {
7833           process_die (child_die, cu);
7834           child_die = sibling_die (child_die);
7835         }
7836     }
7837
7838   /* Decode macro information, if present.  Dwarf 2 macro information
7839      refers to information in the line number info statement program
7840      header, so we can only read it if we've read the header
7841      successfully.  */
7842   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7843   if (attr && cu->line_header)
7844     {
7845       if (dwarf2_attr (die, DW_AT_macro_info, cu))
7846         complaint (&symfile_complaints,
7847                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7848
7849       dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7850     }
7851   else
7852     {
7853       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7854       if (attr && cu->line_header)
7855         {
7856           unsigned int macro_offset = DW_UNSND (attr);
7857
7858           dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7859         }
7860     }
7861
7862   do_cleanups (back_to);
7863 }
7864
7865 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7866    Create the set of symtabs used by this TU, or if this TU is sharing
7867    symtabs with another TU and the symtabs have already been created
7868    then restore those symtabs in the line header.
7869    We don't need the pc/line-number mapping for type units.  */
7870
7871 static void
7872 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7873 {
7874   struct objfile *objfile = dwarf2_per_objfile->objfile;
7875   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7876   struct type_unit_group *tu_group;
7877   int first_time;
7878   struct line_header *lh;
7879   struct attribute *attr;
7880   unsigned int i, line_offset;
7881
7882   gdb_assert (per_cu->is_debug_types);
7883
7884   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7885
7886   /* If we're using .gdb_index (includes -readnow) then
7887      per_cu->s.type_unit_group may not have been set up yet.  */
7888   if (per_cu->s.type_unit_group == NULL)
7889     per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7890   tu_group = per_cu->s.type_unit_group;
7891
7892   /* If we've already processed this stmt_list there's no real need to
7893      do it again, we could fake it and just recreate the part we need
7894      (file name,index -> symtab mapping).  If data shows this optimization
7895      is useful we can do it then.  */
7896   first_time = tu_group->primary_symtab == NULL;
7897
7898   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7899      debug info.  */
7900   lh = NULL;
7901   if (attr != NULL)
7902     {
7903       line_offset = DW_UNSND (attr);
7904       lh = dwarf_decode_line_header (line_offset, cu);
7905     }
7906   if (lh == NULL)
7907     {
7908       if (first_time)
7909         dwarf2_start_symtab (cu, "", NULL, 0);
7910       else
7911         {
7912           gdb_assert (tu_group->symtabs == NULL);
7913           restart_symtab (0);
7914         }
7915       /* Note: The primary symtab will get allocated at the end.  */
7916       return;
7917     }
7918
7919   cu->line_header = lh;
7920   make_cleanup (free_cu_line_header, cu);
7921
7922   if (first_time)
7923     {
7924       dwarf2_start_symtab (cu, "", NULL, 0);
7925
7926       tu_group->num_symtabs = lh->num_file_names;
7927       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7928
7929       for (i = 0; i < lh->num_file_names; ++i)
7930         {
7931           char *dir = NULL;
7932           struct file_entry *fe = &lh->file_names[i];
7933
7934           if (fe->dir_index)
7935             dir = lh->include_dirs[fe->dir_index - 1];
7936           dwarf2_start_subfile (fe->name, dir, NULL);
7937
7938           /* Note: We don't have to watch for the main subfile here, type units
7939              don't have DW_AT_name.  */
7940
7941           if (current_subfile->symtab == NULL)
7942             {
7943               /* NOTE: start_subfile will recognize when it's been passed
7944                  a file it has already seen.  So we can't assume there's a
7945                  simple mapping from lh->file_names to subfiles,
7946                  lh->file_names may contain dups.  */
7947               current_subfile->symtab = allocate_symtab (current_subfile->name,
7948                                                          objfile);
7949             }
7950
7951           fe->symtab = current_subfile->symtab;
7952           tu_group->symtabs[i] = fe->symtab;
7953         }
7954     }
7955   else
7956     {
7957       restart_symtab (0);
7958
7959       for (i = 0; i < lh->num_file_names; ++i)
7960         {
7961           struct file_entry *fe = &lh->file_names[i];
7962
7963           fe->symtab = tu_group->symtabs[i];
7964         }
7965     }
7966
7967   /* The main symtab is allocated last.  Type units don't have DW_AT_name
7968      so they don't have a "real" (so to speak) symtab anyway.
7969      There is later code that will assign the main symtab to all symbols
7970      that don't have one.  We need to handle the case of a symbol with a
7971      missing symtab (DW_AT_decl_file) anyway.  */
7972 }
7973
7974 /* Process DW_TAG_type_unit.
7975    For TUs we want to skip the first top level sibling if it's not the
7976    actual type being defined by this TU.  In this case the first top
7977    level sibling is there to provide context only.  */
7978
7979 static void
7980 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7981 {
7982   struct die_info *child_die;
7983
7984   prepare_one_comp_unit (cu, die, language_minimal);
7985
7986   /* Initialize (or reinitialize) the machinery for building symtabs.
7987      We do this before processing child DIEs, so that the line header table
7988      is available for DW_AT_decl_file.  */
7989   setup_type_unit_groups (die, cu);
7990
7991   if (die->child != NULL)
7992     {
7993       child_die = die->child;
7994       while (child_die && child_die->tag)
7995         {
7996           process_die (child_die, cu);
7997           child_die = sibling_die (child_die);
7998         }
7999     }
8000 }
8001 \f
8002 /* DWO files.  */
8003
8004 static hashval_t
8005 hash_dwo_file (const void *item)
8006 {
8007   const struct dwo_file *dwo_file = item;
8008
8009   return htab_hash_string (dwo_file->dwo_name);
8010 }
8011
8012 static int
8013 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8014 {
8015   const struct dwo_file *lhs = item_lhs;
8016   const struct dwo_file *rhs = item_rhs;
8017
8018   return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8019 }
8020
8021 /* Allocate a hash table for DWO files.  */
8022
8023 static htab_t
8024 allocate_dwo_file_hash_table (void)
8025 {
8026   struct objfile *objfile = dwarf2_per_objfile->objfile;
8027
8028   return htab_create_alloc_ex (41,
8029                                hash_dwo_file,
8030                                eq_dwo_file,
8031                                NULL,
8032                                &objfile->objfile_obstack,
8033                                hashtab_obstack_allocate,
8034                                dummy_obstack_deallocate);
8035 }
8036
8037 static hashval_t
8038 hash_dwo_unit (const void *item)
8039 {
8040   const struct dwo_unit *dwo_unit = item;
8041
8042   /* This drops the top 32 bits of the id, but is ok for a hash.  */
8043   return dwo_unit->signature;
8044 }
8045
8046 static int
8047 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8048 {
8049   const struct dwo_unit *lhs = item_lhs;
8050   const struct dwo_unit *rhs = item_rhs;
8051
8052   /* The signature is assumed to be unique within the DWO file.
8053      So while object file CU dwo_id's always have the value zero,
8054      that's OK, assuming each object file DWO file has only one CU,
8055      and that's the rule for now.  */
8056   return lhs->signature == rhs->signature;
8057 }
8058
8059 /* Allocate a hash table for DWO CUs,TUs.
8060    There is one of these tables for each of CUs,TUs for each DWO file.  */
8061
8062 static htab_t
8063 allocate_dwo_unit_table (struct objfile *objfile)
8064 {
8065   /* Start out with a pretty small number.
8066      Generally DWO files contain only one CU and maybe some TUs.  */
8067   return htab_create_alloc_ex (3,
8068                                hash_dwo_unit,
8069                                eq_dwo_unit,
8070                                NULL,
8071                                &objfile->objfile_obstack,
8072                                hashtab_obstack_allocate,
8073                                dummy_obstack_deallocate);
8074 }
8075
8076 /* This function is mapped across the sections and remembers the offset and
8077    size of each of the DWO debugging sections we are interested in.  */
8078
8079 static void
8080 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8081 {
8082   struct dwo_file *dwo_file = dwo_file_ptr;
8083   const struct dwo_section_names *names = &dwo_section_names;
8084
8085   if (section_is_p (sectp->name, &names->abbrev_dwo))
8086     {
8087       dwo_file->sections.abbrev.asection = sectp;
8088       dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8089     }
8090   else if (section_is_p (sectp->name, &names->info_dwo))
8091     {
8092       dwo_file->sections.info.asection = sectp;
8093       dwo_file->sections.info.size = bfd_get_section_size (sectp);
8094     }
8095   else if (section_is_p (sectp->name, &names->line_dwo))
8096     {
8097       dwo_file->sections.line.asection = sectp;
8098       dwo_file->sections.line.size = bfd_get_section_size (sectp);
8099     }
8100   else if (section_is_p (sectp->name, &names->loc_dwo))
8101     {
8102       dwo_file->sections.loc.asection = sectp;
8103       dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8104     }
8105   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8106     {
8107       dwo_file->sections.macinfo.asection = sectp;
8108       dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8109     }
8110   else if (section_is_p (sectp->name, &names->macro_dwo))
8111     {
8112       dwo_file->sections.macro.asection = sectp;
8113       dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8114     }
8115   else if (section_is_p (sectp->name, &names->str_dwo))
8116     {
8117       dwo_file->sections.str.asection = sectp;
8118       dwo_file->sections.str.size = bfd_get_section_size (sectp);
8119     }
8120   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8121     {
8122       dwo_file->sections.str_offsets.asection = sectp;
8123       dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8124     }
8125   else if (section_is_p (sectp->name, &names->types_dwo))
8126     {
8127       struct dwarf2_section_info type_section;
8128
8129       memset (&type_section, 0, sizeof (type_section));
8130       type_section.asection = sectp;
8131       type_section.size = bfd_get_section_size (sectp);
8132       VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8133                      &type_section);
8134     }
8135 }
8136
8137 /* Structure used to pass data to create_debug_info_hash_table_reader.  */
8138
8139 struct create_dwo_info_table_data
8140 {
8141   struct dwo_file *dwo_file;
8142   htab_t cu_htab;
8143 };
8144
8145 /* die_reader_func for create_debug_info_hash_table.  */
8146
8147 static void
8148 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8149                                      gdb_byte *info_ptr,
8150                                      struct die_info *comp_unit_die,
8151                                      int has_children,
8152                                      void *datap)
8153 {
8154   struct dwarf2_cu *cu = reader->cu;
8155   struct objfile *objfile = dwarf2_per_objfile->objfile;
8156   sect_offset offset = cu->per_cu->offset;
8157   struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8158   struct create_dwo_info_table_data *data = datap;
8159   struct dwo_file *dwo_file = data->dwo_file;
8160   htab_t cu_htab = data->cu_htab;
8161   void **slot;
8162   struct attribute *attr;
8163   struct dwo_unit *dwo_unit;
8164
8165   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8166   if (attr == NULL)
8167     {
8168       error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8169                " its dwo_id [in module %s]"),
8170              offset.sect_off, dwo_file->dwo_name);
8171       return;
8172     }
8173
8174   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8175   dwo_unit->dwo_file = dwo_file;
8176   dwo_unit->signature = DW_UNSND (attr);
8177   dwo_unit->info_or_types_section = section;
8178   dwo_unit->offset = offset;
8179   dwo_unit->length = cu->per_cu->length;
8180
8181   slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8182   gdb_assert (slot != NULL);
8183   if (*slot != NULL)
8184     {
8185       const struct dwo_unit *dup_dwo_unit = *slot;
8186
8187       complaint (&symfile_complaints,
8188                  _("debug entry at offset 0x%x is duplicate to the entry at"
8189                    " offset 0x%x, dwo_id 0x%s [in module %s]"),
8190                  offset.sect_off, dup_dwo_unit->offset.sect_off,
8191                  phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8192                  dwo_file->dwo_name);
8193     }
8194   else
8195     *slot = dwo_unit;
8196
8197   if (dwarf2_read_debug)
8198     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id 0x%s\n",
8199                         offset.sect_off,
8200                         phex (dwo_unit->signature,
8201                               sizeof (dwo_unit->signature)));
8202 }
8203
8204 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo.  */
8205
8206 static htab_t
8207 create_debug_info_hash_table (struct dwo_file *dwo_file)
8208 {
8209   struct objfile *objfile = dwarf2_per_objfile->objfile;
8210   struct dwarf2_section_info *section = &dwo_file->sections.info;
8211   bfd *abfd;
8212   htab_t cu_htab;
8213   gdb_byte *info_ptr, *end_ptr;
8214   struct create_dwo_info_table_data create_dwo_info_table_data;
8215
8216   dwarf2_read_section (objfile, section);
8217   info_ptr = section->buffer;
8218
8219   if (info_ptr == NULL)
8220     return NULL;
8221
8222   /* We can't set abfd until now because the section may be empty or
8223      not present, in which case section->asection will be NULL.  */
8224   abfd = section->asection->owner;
8225
8226   if (dwarf2_read_debug)
8227     fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8228                         bfd_get_filename (abfd));
8229
8230   cu_htab = allocate_dwo_unit_table (objfile);
8231
8232   create_dwo_info_table_data.dwo_file = dwo_file;
8233   create_dwo_info_table_data.cu_htab = cu_htab;
8234
8235   end_ptr = info_ptr + section->size;
8236   while (info_ptr < end_ptr)
8237     {
8238       struct dwarf2_per_cu_data per_cu;
8239
8240       memset (&per_cu, 0, sizeof (per_cu));
8241       per_cu.objfile = objfile;
8242       per_cu.is_debug_types = 0;
8243       per_cu.offset.sect_off = info_ptr - section->buffer;
8244       per_cu.info_or_types_section = section;
8245
8246       init_cutu_and_read_dies_no_follow (&per_cu,
8247                                          &dwo_file->sections.abbrev,
8248                                          dwo_file,
8249                                          create_debug_info_hash_table_reader,
8250                                          &create_dwo_info_table_data);
8251
8252       info_ptr += per_cu.length;
8253     }
8254
8255   return cu_htab;
8256 }
8257
8258 /* Subroutine of open_dwo_file to simplify it.
8259    Open the file specified by FILE_NAME and hand it off to BFD for
8260    preliminary analysis.  Return a newly initialized bfd *, which
8261    includes a canonicalized copy of FILE_NAME.
8262    In case of trouble, return NULL.
8263    NOTE: This function is derived from symfile_bfd_open.  */
8264
8265 static bfd *
8266 try_open_dwo_file (const char *file_name)
8267 {
8268   bfd *sym_bfd;
8269   int desc;
8270   char *absolute_name;
8271
8272   desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8273                 O_RDONLY | O_BINARY, &absolute_name);
8274   if (desc < 0)
8275     return NULL;
8276
8277   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8278   if (!sym_bfd)
8279     {
8280       xfree (absolute_name);
8281       return NULL;
8282     }
8283   xfree (absolute_name);
8284   bfd_set_cacheable (sym_bfd, 1);
8285
8286   if (!bfd_check_format (sym_bfd, bfd_object))
8287     {
8288       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
8289       return NULL;
8290     }
8291
8292   return sym_bfd;
8293 }
8294
8295 /* Try to open DWO file DWO_NAME.
8296    COMP_DIR is the DW_AT_comp_dir attribute.
8297    The result is the bfd handle of the file.
8298    If there is a problem finding or opening the file, return NULL.
8299    Upon success, the canonicalized path of the file is stored in the bfd,
8300    same as symfile_bfd_open.  */
8301
8302 static bfd *
8303 open_dwo_file (const char *dwo_name, const char *comp_dir)
8304 {
8305   bfd *abfd;
8306
8307   if (IS_ABSOLUTE_PATH (dwo_name))
8308     return try_open_dwo_file (dwo_name);
8309
8310   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
8311
8312   if (comp_dir != NULL)
8313     {
8314       char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8315
8316       /* NOTE: If comp_dir is a relative path, this will also try the
8317          search path, which seems useful.  */
8318       abfd = try_open_dwo_file (path_to_try);
8319       xfree (path_to_try);
8320       if (abfd != NULL)
8321         return abfd;
8322     }
8323
8324   /* That didn't work, try debug-file-directory, which, despite its name,
8325      is a list of paths.  */
8326
8327   if (*debug_file_directory == '\0')
8328     return NULL;
8329
8330   return try_open_dwo_file (dwo_name);
8331 }
8332
8333 /* Initialize the use of the DWO file specified by DWO_NAME.  */
8334
8335 static struct dwo_file *
8336 init_dwo_file (const char *dwo_name, const char *comp_dir)
8337 {
8338   struct objfile *objfile = dwarf2_per_objfile->objfile;
8339   struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8340                                               struct dwo_file);
8341   bfd *abfd;
8342   struct cleanup *cleanups;
8343
8344   if (dwarf2_read_debug)
8345     fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8346
8347   abfd = open_dwo_file (dwo_name, comp_dir);
8348   if (abfd == NULL)
8349     return NULL;
8350   dwo_file->dwo_name = dwo_name;
8351   dwo_file->dwo_bfd = abfd;
8352
8353   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8354
8355   bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8356
8357   dwo_file->cus = create_debug_info_hash_table (dwo_file);
8358
8359   dwo_file->tus = create_debug_types_hash_table (dwo_file,
8360                                                  dwo_file->sections.types);
8361
8362   discard_cleanups (cleanups);
8363
8364   return dwo_file;
8365 }
8366
8367 /* Lookup DWO file DWO_NAME.  */
8368
8369 static struct dwo_file *
8370 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8371 {
8372   struct dwo_file *dwo_file;
8373   struct dwo_file find_entry;
8374   void **slot;
8375
8376   if (dwarf2_per_objfile->dwo_files == NULL)
8377     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8378
8379   /* Have we already seen this DWO file?  */
8380   find_entry.dwo_name = dwo_name;
8381   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8382
8383   /* If not, read it in and build a table of the DWOs it contains.  */
8384   if (*slot == NULL)
8385     *slot = init_dwo_file (dwo_name, comp_dir);
8386
8387   /* NOTE: This will be NULL if unable to open the file.  */
8388   dwo_file = *slot;
8389
8390   return dwo_file;
8391 }
8392
8393 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8394    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8395    SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8396    nomenclature as TUs).
8397    The result is a pointer to the dwo_unit object or NULL if we didn't find it
8398    (dwo_id mismatch or couldn't find the DWO file).  */
8399
8400 static struct dwo_unit *
8401 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8402                       char *dwo_name, const char *comp_dir,
8403                       ULONGEST signature)
8404 {
8405   struct objfile *objfile = dwarf2_per_objfile->objfile;
8406   struct dwo_file *dwo_file;
8407
8408   dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8409   if (dwo_file == NULL)
8410     return NULL;
8411
8412   /* Look up the DWO using its signature(dwo_id).  */
8413
8414   if (dwo_file->cus != NULL)
8415     {
8416       struct dwo_unit find_dwo_cu, *dwo_cu;
8417
8418       find_dwo_cu.signature = signature;
8419       dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8420
8421       if (dwo_cu != NULL)
8422         return dwo_cu;
8423     }
8424
8425   /* We didn't find it.  This must mean a dwo_id mismatch.  */
8426
8427   complaint (&symfile_complaints,
8428              _("Could not find DWO CU referenced by CU at offset 0x%x"
8429                " [in module %s]"),
8430              this_cu->offset.sect_off, objfile->name);
8431   return NULL;
8432 }
8433
8434 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8435    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8436    The result is a pointer to the dwo_unit object or NULL if we didn't find it
8437    (dwo_id mismatch or couldn't find the DWO file).  */
8438
8439 static struct dwo_unit *
8440 lookup_dwo_type_unit (struct signatured_type *this_tu,
8441                       char *dwo_name, const char *comp_dir)
8442 {
8443   struct objfile *objfile = dwarf2_per_objfile->objfile;
8444   struct dwo_file *dwo_file;
8445
8446   dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8447   if (dwo_file == NULL)
8448     return NULL;
8449
8450   /* Look up the DWO using its signature(dwo_id).  */
8451
8452   if (dwo_file->tus != NULL)
8453     {
8454       struct dwo_unit find_dwo_tu, *dwo_tu;
8455
8456       find_dwo_tu.signature = this_tu->signature;
8457       dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8458
8459       if (dwo_tu != NULL)
8460         return dwo_tu;
8461     }
8462
8463   /* We didn't find it.  This must mean a dwo_id mismatch.  */
8464
8465   complaint (&symfile_complaints,
8466              _("Could not find DWO TU referenced by TU at offset 0x%x"
8467                " [in module %s]"),
8468              this_tu->per_cu.offset.sect_off, objfile->name);
8469   return NULL;
8470 }
8471
8472 /* Free all resources associated with DWO_FILE.
8473    Close the DWO file and munmap the sections.
8474    All memory should be on the objfile obstack.  */
8475
8476 static void
8477 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8478 {
8479   int ix;
8480   struct dwarf2_section_info *section;
8481
8482   gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8483   gdb_bfd_unref (dwo_file->dwo_bfd);
8484
8485   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8486 }
8487
8488 /* Wrapper for free_dwo_file for use in cleanups.  */
8489
8490 static void
8491 free_dwo_file_cleanup (void *arg)
8492 {
8493   struct dwo_file *dwo_file = (struct dwo_file *) arg;
8494   struct objfile *objfile = dwarf2_per_objfile->objfile;
8495
8496   free_dwo_file (dwo_file, objfile);
8497 }
8498
8499 /* Traversal function for free_dwo_files.  */
8500
8501 static int
8502 free_dwo_file_from_slot (void **slot, void *info)
8503 {
8504   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8505   struct objfile *objfile = (struct objfile *) info;
8506
8507   free_dwo_file (dwo_file, objfile);
8508
8509   return 1;
8510 }
8511
8512 /* Free all resources associated with DWO_FILES.  */
8513
8514 static void
8515 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8516 {
8517   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8518 }
8519 \f
8520 /* Read in various DIEs.  */
8521
8522 /* qsort helper for inherit_abstract_dies.  */
8523
8524 static int
8525 unsigned_int_compar (const void *ap, const void *bp)
8526 {
8527   unsigned int a = *(unsigned int *) ap;
8528   unsigned int b = *(unsigned int *) bp;
8529
8530   return (a > b) - (b > a);
8531 }
8532
8533 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8534    Inherit only the children of the DW_AT_abstract_origin DIE not being
8535    already referenced by DW_AT_abstract_origin from the children of the
8536    current DIE.  */
8537
8538 static void
8539 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8540 {
8541   struct die_info *child_die;
8542   unsigned die_children_count;
8543   /* CU offsets which were referenced by children of the current DIE.  */
8544   sect_offset *offsets;
8545   sect_offset *offsets_end, *offsetp;
8546   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
8547   struct die_info *origin_die;
8548   /* Iterator of the ORIGIN_DIE children.  */
8549   struct die_info *origin_child_die;
8550   struct cleanup *cleanups;
8551   struct attribute *attr;
8552   struct dwarf2_cu *origin_cu;
8553   struct pending **origin_previous_list_in_scope;
8554
8555   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8556   if (!attr)
8557     return;
8558
8559   /* Note that following die references may follow to a die in a
8560      different cu.  */
8561
8562   origin_cu = cu;
8563   origin_die = follow_die_ref (die, attr, &origin_cu);
8564
8565   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8566      symbols in.  */
8567   origin_previous_list_in_scope = origin_cu->list_in_scope;
8568   origin_cu->list_in_scope = cu->list_in_scope;
8569
8570   if (die->tag != origin_die->tag
8571       && !(die->tag == DW_TAG_inlined_subroutine
8572            && origin_die->tag == DW_TAG_subprogram))
8573     complaint (&symfile_complaints,
8574                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8575                die->offset.sect_off, origin_die->offset.sect_off);
8576
8577   child_die = die->child;
8578   die_children_count = 0;
8579   while (child_die && child_die->tag)
8580     {
8581       child_die = sibling_die (child_die);
8582       die_children_count++;
8583     }
8584   offsets = xmalloc (sizeof (*offsets) * die_children_count);
8585   cleanups = make_cleanup (xfree, offsets);
8586
8587   offsets_end = offsets;
8588   child_die = die->child;
8589   while (child_die && child_die->tag)
8590     {
8591       /* For each CHILD_DIE, find the corresponding child of
8592          ORIGIN_DIE.  If there is more than one layer of
8593          DW_AT_abstract_origin, follow them all; there shouldn't be,
8594          but GCC versions at least through 4.4 generate this (GCC PR
8595          40573).  */
8596       struct die_info *child_origin_die = child_die;
8597       struct dwarf2_cu *child_origin_cu = cu;
8598
8599       while (1)
8600         {
8601           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8602                               child_origin_cu);
8603           if (attr == NULL)
8604             break;
8605           child_origin_die = follow_die_ref (child_origin_die, attr,
8606                                              &child_origin_cu);
8607         }
8608
8609       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8610          counterpart may exist.  */
8611       if (child_origin_die != child_die)
8612         {
8613           if (child_die->tag != child_origin_die->tag
8614               && !(child_die->tag == DW_TAG_inlined_subroutine
8615                    && child_origin_die->tag == DW_TAG_subprogram))
8616             complaint (&symfile_complaints,
8617                        _("Child DIE 0x%x and its abstract origin 0x%x have "
8618                          "different tags"), child_die->offset.sect_off,
8619                        child_origin_die->offset.sect_off);
8620           if (child_origin_die->parent != origin_die)
8621             complaint (&symfile_complaints,
8622                        _("Child DIE 0x%x and its abstract origin 0x%x have "
8623                          "different parents"), child_die->offset.sect_off,
8624                        child_origin_die->offset.sect_off);
8625           else
8626             *offsets_end++ = child_origin_die->offset;
8627         }
8628       child_die = sibling_die (child_die);
8629     }
8630   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8631          unsigned_int_compar);
8632   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8633     if (offsetp[-1].sect_off == offsetp->sect_off)
8634       complaint (&symfile_complaints,
8635                  _("Multiple children of DIE 0x%x refer "
8636                    "to DIE 0x%x as their abstract origin"),
8637                  die->offset.sect_off, offsetp->sect_off);
8638
8639   offsetp = offsets;
8640   origin_child_die = origin_die->child;
8641   while (origin_child_die && origin_child_die->tag)
8642     {
8643       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
8644       while (offsetp < offsets_end
8645              && offsetp->sect_off < origin_child_die->offset.sect_off)
8646         offsetp++;
8647       if (offsetp >= offsets_end
8648           || offsetp->sect_off > origin_child_die->offset.sect_off)
8649         {
8650           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
8651           process_die (origin_child_die, origin_cu);
8652         }
8653       origin_child_die = sibling_die (origin_child_die);
8654     }
8655   origin_cu->list_in_scope = origin_previous_list_in_scope;
8656
8657   do_cleanups (cleanups);
8658 }
8659
8660 static void
8661 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8662 {
8663   struct objfile *objfile = cu->objfile;
8664   struct context_stack *new;
8665   CORE_ADDR lowpc;
8666   CORE_ADDR highpc;
8667   struct die_info *child_die;
8668   struct attribute *attr, *call_line, *call_file;
8669   char *name;
8670   CORE_ADDR baseaddr;
8671   struct block *block;
8672   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8673   VEC (symbolp) *template_args = NULL;
8674   struct template_symbol *templ_func = NULL;
8675
8676   if (inlined_func)
8677     {
8678       /* If we do not have call site information, we can't show the
8679          caller of this inlined function.  That's too confusing, so
8680          only use the scope for local variables.  */
8681       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8682       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8683       if (call_line == NULL || call_file == NULL)
8684         {
8685           read_lexical_block_scope (die, cu);
8686           return;
8687         }
8688     }
8689
8690   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8691
8692   name = dwarf2_name (die, cu);
8693
8694   /* Ignore functions with missing or empty names.  These are actually
8695      illegal according to the DWARF standard.  */
8696   if (name == NULL)
8697     {
8698       complaint (&symfile_complaints,
8699                  _("missing name for subprogram DIE at %d"),
8700                  die->offset.sect_off);
8701       return;
8702     }
8703
8704   /* Ignore functions with missing or invalid low and high pc attributes.  */
8705   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8706     {
8707       attr = dwarf2_attr (die, DW_AT_external, cu);
8708       if (!attr || !DW_UNSND (attr))
8709         complaint (&symfile_complaints,
8710                    _("cannot get low and high bounds "
8711                      "for subprogram DIE at %d"),
8712                    die->offset.sect_off);
8713       return;
8714     }
8715
8716   lowpc += baseaddr;
8717   highpc += baseaddr;
8718
8719   /* If we have any template arguments, then we must allocate a
8720      different sort of symbol.  */
8721   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8722     {
8723       if (child_die->tag == DW_TAG_template_type_param
8724           || child_die->tag == DW_TAG_template_value_param)
8725         {
8726           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8727                                        struct template_symbol);
8728           templ_func->base.is_cplus_template_function = 1;
8729           break;
8730         }
8731     }
8732
8733   new = push_context (0, lowpc);
8734   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8735                                (struct symbol *) templ_func);
8736
8737   /* If there is a location expression for DW_AT_frame_base, record
8738      it.  */
8739   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8740   if (attr)
8741     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8742        expression is being recorded directly in the function's symbol
8743        and not in a separate frame-base object.  I guess this hack is
8744        to avoid adding some sort of frame-base adjunct/annex to the
8745        function's symbol :-(.  The problem with doing this is that it
8746        results in a function symbol with a location expression that
8747        has nothing to do with the location of the function, ouch!  The
8748        relationship should be: a function's symbol has-a frame base; a
8749        frame-base has-a location expression.  */
8750     dwarf2_symbol_mark_computed (attr, new->name, cu);
8751
8752   cu->list_in_scope = &local_symbols;
8753
8754   if (die->child != NULL)
8755     {
8756       child_die = die->child;
8757       while (child_die && child_die->tag)
8758         {
8759           if (child_die->tag == DW_TAG_template_type_param
8760               || child_die->tag == DW_TAG_template_value_param)
8761             {
8762               struct symbol *arg = new_symbol (child_die, NULL, cu);
8763
8764               if (arg != NULL)
8765                 VEC_safe_push (symbolp, template_args, arg);
8766             }
8767           else
8768             process_die (child_die, cu);
8769           child_die = sibling_die (child_die);
8770         }
8771     }
8772
8773   inherit_abstract_dies (die, cu);
8774
8775   /* If we have a DW_AT_specification, we might need to import using
8776      directives from the context of the specification DIE.  See the
8777      comment in determine_prefix.  */
8778   if (cu->language == language_cplus
8779       && dwarf2_attr (die, DW_AT_specification, cu))
8780     {
8781       struct dwarf2_cu *spec_cu = cu;
8782       struct die_info *spec_die = die_specification (die, &spec_cu);
8783
8784       while (spec_die)
8785         {
8786           child_die = spec_die->child;
8787           while (child_die && child_die->tag)
8788             {
8789               if (child_die->tag == DW_TAG_imported_module)
8790                 process_die (child_die, spec_cu);
8791               child_die = sibling_die (child_die);
8792             }
8793
8794           /* In some cases, GCC generates specification DIEs that
8795              themselves contain DW_AT_specification attributes.  */
8796           spec_die = die_specification (spec_die, &spec_cu);
8797         }
8798     }
8799
8800   new = pop_context ();
8801   /* Make a block for the local symbols within.  */
8802   block = finish_block (new->name, &local_symbols, new->old_blocks,
8803                         lowpc, highpc, objfile);
8804
8805   /* For C++, set the block's scope.  */
8806   if (cu->language == language_cplus || cu->language == language_fortran)
8807     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8808                         determine_prefix (die, cu),
8809                         processing_has_namespace_info);
8810
8811   /* If we have address ranges, record them.  */
8812   dwarf2_record_block_ranges (die, block, baseaddr, cu);
8813
8814   /* Attach template arguments to function.  */
8815   if (! VEC_empty (symbolp, template_args))
8816     {
8817       gdb_assert (templ_func != NULL);
8818
8819       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8820       templ_func->template_arguments
8821         = obstack_alloc (&objfile->objfile_obstack,
8822                          (templ_func->n_template_arguments
8823                           * sizeof (struct symbol *)));
8824       memcpy (templ_func->template_arguments,
8825               VEC_address (symbolp, template_args),
8826               (templ_func->n_template_arguments * sizeof (struct symbol *)));
8827       VEC_free (symbolp, template_args);
8828     }
8829
8830   /* In C++, we can have functions nested inside functions (e.g., when
8831      a function declares a class that has methods).  This means that
8832      when we finish processing a function scope, we may need to go
8833      back to building a containing block's symbol lists.  */
8834   local_symbols = new->locals;
8835   param_symbols = new->params;
8836   using_directives = new->using_directives;
8837
8838   /* If we've finished processing a top-level function, subsequent
8839      symbols go in the file symbol list.  */
8840   if (outermost_context_p ())
8841     cu->list_in_scope = &file_symbols;
8842 }
8843
8844 /* Process all the DIES contained within a lexical block scope.  Start
8845    a new scope, process the dies, and then close the scope.  */
8846
8847 static void
8848 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8849 {
8850   struct objfile *objfile = cu->objfile;
8851   struct context_stack *new;
8852   CORE_ADDR lowpc, highpc;
8853   struct die_info *child_die;
8854   CORE_ADDR baseaddr;
8855
8856   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8857
8858   /* Ignore blocks with missing or invalid low and high pc attributes.  */
8859   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8860      as multiple lexical blocks?  Handling children in a sane way would
8861      be nasty.  Might be easier to properly extend generic blocks to
8862      describe ranges.  */
8863   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8864     return;
8865   lowpc += baseaddr;
8866   highpc += baseaddr;
8867
8868   push_context (0, lowpc);
8869   if (die->child != NULL)
8870     {
8871       child_die = die->child;
8872       while (child_die && child_die->tag)
8873         {
8874           process_die (child_die, cu);
8875           child_die = sibling_die (child_die);
8876         }
8877     }
8878   new = pop_context ();
8879
8880   if (local_symbols != NULL || using_directives != NULL)
8881     {
8882       struct block *block
8883         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8884                         highpc, objfile);
8885
8886       /* Note that recording ranges after traversing children, as we
8887          do here, means that recording a parent's ranges entails
8888          walking across all its children's ranges as they appear in
8889          the address map, which is quadratic behavior.
8890
8891          It would be nicer to record the parent's ranges before
8892          traversing its children, simply overriding whatever you find
8893          there.  But since we don't even decide whether to create a
8894          block until after we've traversed its children, that's hard
8895          to do.  */
8896       dwarf2_record_block_ranges (die, block, baseaddr, cu);
8897     }
8898   local_symbols = new->locals;
8899   using_directives = new->using_directives;
8900 }
8901
8902 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
8903
8904 static void
8905 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8906 {
8907   struct objfile *objfile = cu->objfile;
8908   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8909   CORE_ADDR pc, baseaddr;
8910   struct attribute *attr;
8911   struct call_site *call_site, call_site_local;
8912   void **slot;
8913   int nparams;
8914   struct die_info *child_die;
8915
8916   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8917
8918   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8919   if (!attr)
8920     {
8921       complaint (&symfile_complaints,
8922                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8923                    "DIE 0x%x [in module %s]"),
8924                  die->offset.sect_off, objfile->name);
8925       return;
8926     }
8927   pc = DW_ADDR (attr) + baseaddr;
8928
8929   if (cu->call_site_htab == NULL)
8930     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8931                                                NULL, &objfile->objfile_obstack,
8932                                                hashtab_obstack_allocate, NULL);
8933   call_site_local.pc = pc;
8934   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8935   if (*slot != NULL)
8936     {
8937       complaint (&symfile_complaints,
8938                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
8939                    "DIE 0x%x [in module %s]"),
8940                  paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8941       return;
8942     }
8943
8944   /* Count parameters at the caller.  */
8945
8946   nparams = 0;
8947   for (child_die = die->child; child_die && child_die->tag;
8948        child_die = sibling_die (child_die))
8949     {
8950       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8951         {
8952           complaint (&symfile_complaints,
8953                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8954                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8955                      child_die->tag, child_die->offset.sect_off, objfile->name);
8956           continue;
8957         }
8958
8959       nparams++;
8960     }
8961
8962   call_site = obstack_alloc (&objfile->objfile_obstack,
8963                              (sizeof (*call_site)
8964                               + (sizeof (*call_site->parameter)
8965                                  * (nparams - 1))));
8966   *slot = call_site;
8967   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8968   call_site->pc = pc;
8969
8970   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8971     {
8972       struct die_info *func_die;
8973
8974       /* Skip also over DW_TAG_inlined_subroutine.  */
8975       for (func_die = die->parent;
8976            func_die && func_die->tag != DW_TAG_subprogram
8977            && func_die->tag != DW_TAG_subroutine_type;
8978            func_die = func_die->parent);
8979
8980       /* DW_AT_GNU_all_call_sites is a superset
8981          of DW_AT_GNU_all_tail_call_sites.  */
8982       if (func_die
8983           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8984           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8985         {
8986           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8987              not complete.  But keep CALL_SITE for look ups via call_site_htab,
8988              both the initial caller containing the real return address PC and
8989              the final callee containing the current PC of a chain of tail
8990              calls do not need to have the tail call list complete.  But any
8991              function candidate for a virtual tail call frame searched via
8992              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8993              determined unambiguously.  */
8994         }
8995       else
8996         {
8997           struct type *func_type = NULL;
8998
8999           if (func_die)
9000             func_type = get_die_type (func_die, cu);
9001           if (func_type != NULL)
9002             {
9003               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9004
9005               /* Enlist this call site to the function.  */
9006               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9007               TYPE_TAIL_CALL_LIST (func_type) = call_site;
9008             }
9009           else
9010             complaint (&symfile_complaints,
9011                        _("Cannot find function owning DW_TAG_GNU_call_site "
9012                          "DIE 0x%x [in module %s]"),
9013                        die->offset.sect_off, objfile->name);
9014         }
9015     }
9016
9017   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9018   if (attr == NULL)
9019     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9020   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9021   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9022     /* Keep NULL DWARF_BLOCK.  */;
9023   else if (attr_form_is_block (attr))
9024     {
9025       struct dwarf2_locexpr_baton *dlbaton;
9026
9027       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9028       dlbaton->data = DW_BLOCK (attr)->data;
9029       dlbaton->size = DW_BLOCK (attr)->size;
9030       dlbaton->per_cu = cu->per_cu;
9031
9032       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9033     }
9034   else if (is_ref_attr (attr))
9035     {
9036       struct dwarf2_cu *target_cu = cu;
9037       struct die_info *target_die;
9038
9039       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9040       gdb_assert (target_cu->objfile == objfile);
9041       if (die_is_declaration (target_die, target_cu))
9042         {
9043           const char *target_physname;
9044
9045           target_physname = dwarf2_physname (NULL, target_die, target_cu);
9046           if (target_physname == NULL)
9047             complaint (&symfile_complaints,
9048                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9049                          "physname, for referencing DIE 0x%x [in module %s]"),
9050                        die->offset.sect_off, objfile->name);
9051           else
9052             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9053         }
9054       else
9055         {
9056           CORE_ADDR lowpc;
9057
9058           /* DW_AT_entry_pc should be preferred.  */
9059           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9060             complaint (&symfile_complaints,
9061                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9062                          "low pc, for referencing DIE 0x%x [in module %s]"),
9063                        die->offset.sect_off, objfile->name);
9064           else
9065             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9066         }
9067     }
9068   else
9069     complaint (&symfile_complaints,
9070                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9071                  "block nor reference, for DIE 0x%x [in module %s]"),
9072                die->offset.sect_off, objfile->name);
9073
9074   call_site->per_cu = cu->per_cu;
9075
9076   for (child_die = die->child;
9077        child_die && child_die->tag;
9078        child_die = sibling_die (child_die))
9079     {
9080       struct call_site_parameter *parameter;
9081       struct attribute *loc, *origin;
9082
9083       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9084         {
9085           /* Already printed the complaint above.  */
9086           continue;
9087         }
9088
9089       gdb_assert (call_site->parameter_count < nparams);
9090       parameter = &call_site->parameter[call_site->parameter_count];
9091
9092       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9093          specifies DW_TAG_formal_parameter.  Value of the data assumed for the
9094          register is contained in DW_AT_GNU_call_site_value.  */
9095
9096       loc = dwarf2_attr (child_die, DW_AT_location, cu);
9097       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9098       if (loc == NULL && origin != NULL && is_ref_attr (origin))
9099         {
9100           sect_offset offset;
9101
9102           parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9103           offset = dwarf2_get_ref_die_offset (origin);
9104           if (!offset_in_cu_p (&cu->header, offset))
9105             {
9106               /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9107                  binding can be done only inside one CU.  Such referenced DIE
9108                  therefore cannot be even moved to DW_TAG_partial_unit.  */
9109               complaint (&symfile_complaints,
9110                          _("DW_AT_abstract_origin offset is not in CU for "
9111                            "DW_TAG_GNU_call_site child DIE 0x%x "
9112                            "[in module %s]"),
9113                          child_die->offset.sect_off, objfile->name);
9114               continue;
9115             }
9116           parameter->u.param_offset.cu_off = (offset.sect_off
9117                                               - cu->header.offset.sect_off);
9118         }
9119       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9120         {
9121           complaint (&symfile_complaints,
9122                      _("No DW_FORM_block* DW_AT_location for "
9123                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9124                      child_die->offset.sect_off, objfile->name);
9125           continue;
9126         }
9127       else
9128         {
9129           parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9130             (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9131           if (parameter->u.dwarf_reg != -1)
9132             parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9133           else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9134                                     &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9135                                              &parameter->u.fb_offset))
9136             parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9137           else
9138             {
9139               complaint (&symfile_complaints,
9140                          _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9141                            "for DW_FORM_block* DW_AT_location is supported for "
9142                            "DW_TAG_GNU_call_site child DIE 0x%x "
9143                            "[in module %s]"),
9144                          child_die->offset.sect_off, objfile->name);
9145               continue;
9146             }
9147         }
9148
9149       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9150       if (!attr_form_is_block (attr))
9151         {
9152           complaint (&symfile_complaints,
9153                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9154                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9155                      child_die->offset.sect_off, objfile->name);
9156           continue;
9157         }
9158       parameter->value = DW_BLOCK (attr)->data;
9159       parameter->value_size = DW_BLOCK (attr)->size;
9160
9161       /* Parameters are not pre-cleared by memset above.  */
9162       parameter->data_value = NULL;
9163       parameter->data_value_size = 0;
9164       call_site->parameter_count++;
9165
9166       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9167       if (attr)
9168         {
9169           if (!attr_form_is_block (attr))
9170             complaint (&symfile_complaints,
9171                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9172                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9173                        child_die->offset.sect_off, objfile->name);
9174           else
9175             {
9176               parameter->data_value = DW_BLOCK (attr)->data;
9177               parameter->data_value_size = DW_BLOCK (attr)->size;
9178             }
9179         }
9180     }
9181 }
9182
9183 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9184    Return 1 if the attributes are present and valid, otherwise, return 0.
9185    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
9186
9187 static int
9188 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9189                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
9190                     struct partial_symtab *ranges_pst)
9191 {
9192   struct objfile *objfile = cu->objfile;
9193   struct comp_unit_head *cu_header = &cu->header;
9194   bfd *obfd = objfile->obfd;
9195   unsigned int addr_size = cu_header->addr_size;
9196   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9197   /* Base address selection entry.  */
9198   CORE_ADDR base;
9199   int found_base;
9200   unsigned int dummy;
9201   gdb_byte *buffer;
9202   CORE_ADDR marker;
9203   int low_set;
9204   CORE_ADDR low = 0;
9205   CORE_ADDR high = 0;
9206   CORE_ADDR baseaddr;
9207
9208   found_base = cu->base_known;
9209   base = cu->base_address;
9210
9211   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9212   if (offset >= dwarf2_per_objfile->ranges.size)
9213     {
9214       complaint (&symfile_complaints,
9215                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
9216                  offset);
9217       return 0;
9218     }
9219   buffer = dwarf2_per_objfile->ranges.buffer + offset;
9220
9221   /* Read in the largest possible address.  */
9222   marker = read_address (obfd, buffer, cu, &dummy);
9223   if ((marker & mask) == mask)
9224     {
9225       /* If we found the largest possible address, then
9226          read the base address.  */
9227       base = read_address (obfd, buffer + addr_size, cu, &dummy);
9228       buffer += 2 * addr_size;
9229       offset += 2 * addr_size;
9230       found_base = 1;
9231     }
9232
9233   low_set = 0;
9234
9235   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9236
9237   while (1)
9238     {
9239       CORE_ADDR range_beginning, range_end;
9240
9241       range_beginning = read_address (obfd, buffer, cu, &dummy);
9242       buffer += addr_size;
9243       range_end = read_address (obfd, buffer, cu, &dummy);
9244       buffer += addr_size;
9245       offset += 2 * addr_size;
9246
9247       /* An end of list marker is a pair of zero addresses.  */
9248       if (range_beginning == 0 && range_end == 0)
9249         /* Found the end of list entry.  */
9250         break;
9251
9252       /* Each base address selection entry is a pair of 2 values.
9253          The first is the largest possible address, the second is
9254          the base address.  Check for a base address here.  */
9255       if ((range_beginning & mask) == mask)
9256         {
9257           /* If we found the largest possible address, then
9258              read the base address.  */
9259           base = read_address (obfd, buffer + addr_size, cu, &dummy);
9260           found_base = 1;
9261           continue;
9262         }
9263
9264       if (!found_base)
9265         {
9266           /* We have no valid base address for the ranges
9267              data.  */
9268           complaint (&symfile_complaints,
9269                      _("Invalid .debug_ranges data (no base address)"));
9270           return 0;
9271         }
9272
9273       if (range_beginning > range_end)
9274         {
9275           /* Inverted range entries are invalid.  */
9276           complaint (&symfile_complaints,
9277                      _("Invalid .debug_ranges data (inverted range)"));
9278           return 0;
9279         }
9280
9281       /* Empty range entries have no effect.  */
9282       if (range_beginning == range_end)
9283         continue;
9284
9285       range_beginning += base;
9286       range_end += base;
9287
9288       /* A not-uncommon case of bad debug info.
9289          Don't pollute the addrmap with bad data.  */
9290       if (range_beginning + baseaddr == 0
9291           && !dwarf2_per_objfile->has_section_at_zero)
9292         {
9293           complaint (&symfile_complaints,
9294                      _(".debug_ranges entry has start address of zero"
9295                        " [in module %s]"), objfile->name);
9296           continue;
9297         }
9298
9299       if (ranges_pst != NULL)
9300         addrmap_set_empty (objfile->psymtabs_addrmap,
9301                            range_beginning + baseaddr,
9302                            range_end - 1 + baseaddr,
9303                            ranges_pst);
9304
9305       /* FIXME: This is recording everything as a low-high
9306          segment of consecutive addresses.  We should have a
9307          data structure for discontiguous block ranges
9308          instead.  */
9309       if (! low_set)
9310         {
9311           low = range_beginning;
9312           high = range_end;
9313           low_set = 1;
9314         }
9315       else
9316         {
9317           if (range_beginning < low)
9318             low = range_beginning;
9319           if (range_end > high)
9320             high = range_end;
9321         }
9322     }
9323
9324   if (! low_set)
9325     /* If the first entry is an end-of-list marker, the range
9326        describes an empty scope, i.e. no instructions.  */
9327     return 0;
9328
9329   if (low_return)
9330     *low_return = low;
9331   if (high_return)
9332     *high_return = high;
9333   return 1;
9334 }
9335
9336 /* Get low and high pc attributes from a die.  Return 1 if the attributes
9337    are present and valid, otherwise, return 0.  Return -1 if the range is
9338    discontinuous, i.e. derived from DW_AT_ranges information.  */
9339
9340 static int
9341 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9342                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
9343                       struct partial_symtab *pst)
9344 {
9345   struct attribute *attr;
9346   struct attribute *attr_high;
9347   CORE_ADDR low = 0;
9348   CORE_ADDR high = 0;
9349   int ret = 0;
9350
9351   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9352   if (attr_high)
9353     {
9354       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9355       if (attr)
9356         {
9357           low = DW_ADDR (attr);
9358           if (attr_high->form == DW_FORM_addr
9359               || attr_high->form == DW_FORM_GNU_addr_index)
9360             high = DW_ADDR (attr_high);
9361           else
9362             high = low + DW_UNSND (attr_high);
9363         }
9364       else
9365         /* Found high w/o low attribute.  */
9366         return 0;
9367
9368       /* Found consecutive range of addresses.  */
9369       ret = 1;
9370     }
9371   else
9372     {
9373       attr = dwarf2_attr (die, DW_AT_ranges, cu);
9374       if (attr != NULL)
9375         {
9376           unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9377
9378           /* Value of the DW_AT_ranges attribute is the offset in the
9379              .debug_ranges section.  */
9380           if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9381             return 0;
9382           /* Found discontinuous range of addresses.  */
9383           ret = -1;
9384         }
9385     }
9386
9387   /* read_partial_die has also the strict LOW < HIGH requirement.  */
9388   if (high <= low)
9389     return 0;
9390
9391   /* When using the GNU linker, .gnu.linkonce. sections are used to
9392      eliminate duplicate copies of functions and vtables and such.
9393      The linker will arbitrarily choose one and discard the others.
9394      The AT_*_pc values for such functions refer to local labels in
9395      these sections.  If the section from that file was discarded, the
9396      labels are not in the output, so the relocs get a value of 0.
9397      If this is a discarded function, mark the pc bounds as invalid,
9398      so that GDB will ignore it.  */
9399   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9400     return 0;
9401
9402   *lowpc = low;
9403   if (highpc)
9404     *highpc = high;
9405   return ret;
9406 }
9407
9408 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9409    its low and high PC addresses.  Do nothing if these addresses could not
9410    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
9411    and HIGHPC to the high address if greater than HIGHPC.  */
9412
9413 static void
9414 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9415                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
9416                                  struct dwarf2_cu *cu)
9417 {
9418   CORE_ADDR low, high;
9419   struct die_info *child = die->child;
9420
9421   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9422     {
9423       *lowpc = min (*lowpc, low);
9424       *highpc = max (*highpc, high);
9425     }
9426
9427   /* If the language does not allow nested subprograms (either inside
9428      subprograms or lexical blocks), we're done.  */
9429   if (cu->language != language_ada)
9430     return;
9431
9432   /* Check all the children of the given DIE.  If it contains nested
9433      subprograms, then check their pc bounds.  Likewise, we need to
9434      check lexical blocks as well, as they may also contain subprogram
9435      definitions.  */
9436   while (child && child->tag)
9437     {
9438       if (child->tag == DW_TAG_subprogram
9439           || child->tag == DW_TAG_lexical_block)
9440         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9441       child = sibling_die (child);
9442     }
9443 }
9444
9445 /* Get the low and high pc's represented by the scope DIE, and store
9446    them in *LOWPC and *HIGHPC.  If the correct values can't be
9447    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
9448
9449 static void
9450 get_scope_pc_bounds (struct die_info *die,
9451                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
9452                      struct dwarf2_cu *cu)
9453 {
9454   CORE_ADDR best_low = (CORE_ADDR) -1;
9455   CORE_ADDR best_high = (CORE_ADDR) 0;
9456   CORE_ADDR current_low, current_high;
9457
9458   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9459     {
9460       best_low = current_low;
9461       best_high = current_high;
9462     }
9463   else
9464     {
9465       struct die_info *child = die->child;
9466
9467       while (child && child->tag)
9468         {
9469           switch (child->tag) {
9470           case DW_TAG_subprogram:
9471             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9472             break;
9473           case DW_TAG_namespace:
9474           case DW_TAG_module:
9475             /* FIXME: carlton/2004-01-16: Should we do this for
9476                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
9477                that current GCC's always emit the DIEs corresponding
9478                to definitions of methods of classes as children of a
9479                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9480                the DIEs giving the declarations, which could be
9481                anywhere).  But I don't see any reason why the
9482                standards says that they have to be there.  */
9483             get_scope_pc_bounds (child, &current_low, &current_high, cu);
9484
9485             if (current_low != ((CORE_ADDR) -1))
9486               {
9487                 best_low = min (best_low, current_low);
9488                 best_high = max (best_high, current_high);
9489               }
9490             break;
9491           default:
9492             /* Ignore.  */
9493             break;
9494           }
9495
9496           child = sibling_die (child);
9497         }
9498     }
9499
9500   *lowpc = best_low;
9501   *highpc = best_high;
9502 }
9503
9504 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9505    in DIE.  */
9506
9507 static void
9508 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9509                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9510 {
9511   struct objfile *objfile = cu->objfile;
9512   struct attribute *attr;
9513   struct attribute *attr_high;
9514
9515   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9516   if (attr_high)
9517     {
9518       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9519       if (attr)
9520         {
9521           CORE_ADDR low = DW_ADDR (attr);
9522           CORE_ADDR high;
9523           if (attr_high->form == DW_FORM_addr
9524               || attr_high->form == DW_FORM_GNU_addr_index)
9525             high = DW_ADDR (attr_high);
9526           else
9527             high = low + DW_UNSND (attr_high);
9528
9529           record_block_range (block, baseaddr + low, baseaddr + high - 1);
9530         }
9531     }
9532
9533   attr = dwarf2_attr (die, DW_AT_ranges, cu);
9534   if (attr)
9535     {
9536       bfd *obfd = objfile->obfd;
9537
9538       /* The value of the DW_AT_ranges attribute is the offset of the
9539          address range list in the .debug_ranges section.  */
9540       unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9541       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9542
9543       /* For some target architectures, but not others, the
9544          read_address function sign-extends the addresses it returns.
9545          To recognize base address selection entries, we need a
9546          mask.  */
9547       unsigned int addr_size = cu->header.addr_size;
9548       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9549
9550       /* The base address, to which the next pair is relative.  Note
9551          that this 'base' is a DWARF concept: most entries in a range
9552          list are relative, to reduce the number of relocs against the
9553          debugging information.  This is separate from this function's
9554          'baseaddr' argument, which GDB uses to relocate debugging
9555          information from a shared library based on the address at
9556          which the library was loaded.  */
9557       CORE_ADDR base = cu->base_address;
9558       int base_known = cu->base_known;
9559
9560       gdb_assert (dwarf2_per_objfile->ranges.readin);
9561       if (offset >= dwarf2_per_objfile->ranges.size)
9562         {
9563           complaint (&symfile_complaints,
9564                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9565                      offset);
9566           return;
9567         }
9568
9569       for (;;)
9570         {
9571           unsigned int bytes_read;
9572           CORE_ADDR start, end;
9573
9574           start = read_address (obfd, buffer, cu, &bytes_read);
9575           buffer += bytes_read;
9576           end = read_address (obfd, buffer, cu, &bytes_read);
9577           buffer += bytes_read;
9578
9579           /* Did we find the end of the range list?  */
9580           if (start == 0 && end == 0)
9581             break;
9582
9583           /* Did we find a base address selection entry?  */
9584           else if ((start & base_select_mask) == base_select_mask)
9585             {
9586               base = end;
9587               base_known = 1;
9588             }
9589
9590           /* We found an ordinary address range.  */
9591           else
9592             {
9593               if (!base_known)
9594                 {
9595                   complaint (&symfile_complaints,
9596                              _("Invalid .debug_ranges data "
9597                                "(no base address)"));
9598                   return;
9599                 }
9600
9601               if (start > end)
9602                 {
9603                   /* Inverted range entries are invalid.  */
9604                   complaint (&symfile_complaints,
9605                              _("Invalid .debug_ranges data "
9606                                "(inverted range)"));
9607                   return;
9608                 }
9609
9610               /* Empty range entries have no effect.  */
9611               if (start == end)
9612                 continue;
9613
9614               start += base + baseaddr;
9615               end += base + baseaddr;
9616
9617               /* A not-uncommon case of bad debug info.
9618                  Don't pollute the addrmap with bad data.  */
9619               if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9620                 {
9621                   complaint (&symfile_complaints,
9622                              _(".debug_ranges entry has start address of zero"
9623                                " [in module %s]"), objfile->name);
9624                   continue;
9625                 }
9626
9627               record_block_range (block, start, end - 1);
9628             }
9629         }
9630     }
9631 }
9632
9633 /* Check whether the producer field indicates either of GCC < 4.6, or the
9634    Intel C/C++ compiler, and cache the result in CU.  */
9635
9636 static void
9637 check_producer (struct dwarf2_cu *cu)
9638 {
9639   const char *cs;
9640   int major, minor, release;
9641
9642   if (cu->producer == NULL)
9643     {
9644       /* For unknown compilers expect their behavior is DWARF version
9645          compliant.
9646
9647          GCC started to support .debug_types sections by -gdwarf-4 since
9648          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
9649          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9650          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9651          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
9652     }
9653   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9654     {
9655       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
9656
9657       cs = &cu->producer[strlen ("GNU ")];
9658       while (*cs && !isdigit (*cs))
9659         cs++;
9660       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9661         {
9662           /* Not recognized as GCC.  */
9663         }
9664       else
9665         cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9666     }
9667   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9668     cu->producer_is_icc = 1;
9669   else
9670     {
9671       /* For other non-GCC compilers, expect their behavior is DWARF version
9672          compliant.  */
9673     }
9674
9675   cu->checked_producer = 1;
9676 }
9677
9678 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9679    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9680    during 4.6.0 experimental.  */
9681
9682 static int
9683 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9684 {
9685   if (!cu->checked_producer)
9686     check_producer (cu);
9687
9688   return cu->producer_is_gxx_lt_4_6;
9689 }
9690
9691 /* Return the default accessibility type if it is not overriden by
9692    DW_AT_accessibility.  */
9693
9694 static enum dwarf_access_attribute
9695 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9696 {
9697   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9698     {
9699       /* The default DWARF 2 accessibility for members is public, the default
9700          accessibility for inheritance is private.  */
9701
9702       if (die->tag != DW_TAG_inheritance)
9703         return DW_ACCESS_public;
9704       else
9705         return DW_ACCESS_private;
9706     }
9707   else
9708     {
9709       /* DWARF 3+ defines the default accessibility a different way.  The same
9710          rules apply now for DW_TAG_inheritance as for the members and it only
9711          depends on the container kind.  */
9712
9713       if (die->parent->tag == DW_TAG_class_type)
9714         return DW_ACCESS_private;
9715       else
9716         return DW_ACCESS_public;
9717     }
9718 }
9719
9720 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
9721    offset.  If the attribute was not found return 0, otherwise return
9722    1.  If it was found but could not properly be handled, set *OFFSET
9723    to 0.  */
9724
9725 static int
9726 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9727                              LONGEST *offset)
9728 {
9729   struct attribute *attr;
9730
9731   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9732   if (attr != NULL)
9733     {
9734       *offset = 0;
9735
9736       /* Note that we do not check for a section offset first here.
9737          This is because DW_AT_data_member_location is new in DWARF 4,
9738          so if we see it, we can assume that a constant form is really
9739          a constant and not a section offset.  */
9740       if (attr_form_is_constant (attr))
9741         *offset = dwarf2_get_attr_constant_value (attr, 0);
9742       else if (attr_form_is_section_offset (attr))
9743         dwarf2_complex_location_expr_complaint ();
9744       else if (attr_form_is_block (attr))
9745         *offset = decode_locdesc (DW_BLOCK (attr), cu);
9746       else
9747         dwarf2_complex_location_expr_complaint ();
9748
9749       return 1;
9750     }
9751
9752   return 0;
9753 }
9754
9755 /* Add an aggregate field to the field list.  */
9756
9757 static void
9758 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9759                   struct dwarf2_cu *cu)
9760 {
9761   struct objfile *objfile = cu->objfile;
9762   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9763   struct nextfield *new_field;
9764   struct attribute *attr;
9765   struct field *fp;
9766   char *fieldname = "";
9767
9768   /* Allocate a new field list entry and link it in.  */
9769   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9770   make_cleanup (xfree, new_field);
9771   memset (new_field, 0, sizeof (struct nextfield));
9772
9773   if (die->tag == DW_TAG_inheritance)
9774     {
9775       new_field->next = fip->baseclasses;
9776       fip->baseclasses = new_field;
9777     }
9778   else
9779     {
9780       new_field->next = fip->fields;
9781       fip->fields = new_field;
9782     }
9783   fip->nfields++;
9784
9785   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9786   if (attr)
9787     new_field->accessibility = DW_UNSND (attr);
9788   else
9789     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9790   if (new_field->accessibility != DW_ACCESS_public)
9791     fip->non_public_fields = 1;
9792
9793   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9794   if (attr)
9795     new_field->virtuality = DW_UNSND (attr);
9796   else
9797     new_field->virtuality = DW_VIRTUALITY_none;
9798
9799   fp = &new_field->field;
9800
9801   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9802     {
9803       LONGEST offset;
9804
9805       /* Data member other than a C++ static data member.  */
9806
9807       /* Get type of field.  */
9808       fp->type = die_type (die, cu);
9809
9810       SET_FIELD_BITPOS (*fp, 0);
9811
9812       /* Get bit size of field (zero if none).  */
9813       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9814       if (attr)
9815         {
9816           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9817         }
9818       else
9819         {
9820           FIELD_BITSIZE (*fp) = 0;
9821         }
9822
9823       /* Get bit offset of field.  */
9824       if (handle_data_member_location (die, cu, &offset))
9825         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9826       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9827       if (attr)
9828         {
9829           if (gdbarch_bits_big_endian (gdbarch))
9830             {
9831               /* For big endian bits, the DW_AT_bit_offset gives the
9832                  additional bit offset from the MSB of the containing
9833                  anonymous object to the MSB of the field.  We don't
9834                  have to do anything special since we don't need to
9835                  know the size of the anonymous object.  */
9836               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9837             }
9838           else
9839             {
9840               /* For little endian bits, compute the bit offset to the
9841                  MSB of the anonymous object, subtract off the number of
9842                  bits from the MSB of the field to the MSB of the
9843                  object, and then subtract off the number of bits of
9844                  the field itself.  The result is the bit offset of
9845                  the LSB of the field.  */
9846               int anonymous_size;
9847               int bit_offset = DW_UNSND (attr);
9848
9849               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9850               if (attr)
9851                 {
9852                   /* The size of the anonymous object containing
9853                      the bit field is explicit, so use the
9854                      indicated size (in bytes).  */
9855                   anonymous_size = DW_UNSND (attr);
9856                 }
9857               else
9858                 {
9859                   /* The size of the anonymous object containing
9860                      the bit field must be inferred from the type
9861                      attribute of the data member containing the
9862                      bit field.  */
9863                   anonymous_size = TYPE_LENGTH (fp->type);
9864                 }
9865               SET_FIELD_BITPOS (*fp,
9866                                 (FIELD_BITPOS (*fp)
9867                                  + anonymous_size * bits_per_byte
9868                                  - bit_offset - FIELD_BITSIZE (*fp)));
9869             }
9870         }
9871
9872       /* Get name of field.  */
9873       fieldname = dwarf2_name (die, cu);
9874       if (fieldname == NULL)
9875         fieldname = "";
9876
9877       /* The name is already allocated along with this objfile, so we don't
9878          need to duplicate it for the type.  */
9879       fp->name = fieldname;
9880
9881       /* Change accessibility for artificial fields (e.g. virtual table
9882          pointer or virtual base class pointer) to private.  */
9883       if (dwarf2_attr (die, DW_AT_artificial, cu))
9884         {
9885           FIELD_ARTIFICIAL (*fp) = 1;
9886           new_field->accessibility = DW_ACCESS_private;
9887           fip->non_public_fields = 1;
9888         }
9889     }
9890   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9891     {
9892       /* C++ static member.  */
9893
9894       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9895          is a declaration, but all versions of G++ as of this writing
9896          (so through at least 3.2.1) incorrectly generate
9897          DW_TAG_variable tags.  */
9898
9899       const char *physname;
9900
9901       /* Get name of field.  */
9902       fieldname = dwarf2_name (die, cu);
9903       if (fieldname == NULL)
9904         return;
9905
9906       attr = dwarf2_attr (die, DW_AT_const_value, cu);
9907       if (attr
9908           /* Only create a symbol if this is an external value.
9909              new_symbol checks this and puts the value in the global symbol
9910              table, which we want.  If it is not external, new_symbol
9911              will try to put the value in cu->list_in_scope which is wrong.  */
9912           && dwarf2_flag_true_p (die, DW_AT_external, cu))
9913         {
9914           /* A static const member, not much different than an enum as far as
9915              we're concerned, except that we can support more types.  */
9916           new_symbol (die, NULL, cu);
9917         }
9918
9919       /* Get physical name.  */
9920       physname = dwarf2_physname (fieldname, die, cu);
9921
9922       /* The name is already allocated along with this objfile, so we don't
9923          need to duplicate it for the type.  */
9924       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9925       FIELD_TYPE (*fp) = die_type (die, cu);
9926       FIELD_NAME (*fp) = fieldname;
9927     }
9928   else if (die->tag == DW_TAG_inheritance)
9929     {
9930       LONGEST offset;
9931
9932       /* C++ base class field.  */
9933       if (handle_data_member_location (die, cu, &offset))
9934         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9935       FIELD_BITSIZE (*fp) = 0;
9936       FIELD_TYPE (*fp) = die_type (die, cu);
9937       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9938       fip->nbaseclasses++;
9939     }
9940 }
9941
9942 /* Add a typedef defined in the scope of the FIP's class.  */
9943
9944 static void
9945 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9946                     struct dwarf2_cu *cu)
9947 {
9948   struct objfile *objfile = cu->objfile;
9949   struct typedef_field_list *new_field;
9950   struct attribute *attr;
9951   struct typedef_field *fp;
9952   char *fieldname = "";
9953
9954   /* Allocate a new field list entry and link it in.  */
9955   new_field = xzalloc (sizeof (*new_field));
9956   make_cleanup (xfree, new_field);
9957
9958   gdb_assert (die->tag == DW_TAG_typedef);
9959
9960   fp = &new_field->field;
9961
9962   /* Get name of field.  */
9963   fp->name = dwarf2_name (die, cu);
9964   if (fp->name == NULL)
9965     return;
9966
9967   fp->type = read_type_die (die, cu);
9968
9969   new_field->next = fip->typedef_field_list;
9970   fip->typedef_field_list = new_field;
9971   fip->typedef_field_list_count++;
9972 }
9973
9974 /* Create the vector of fields, and attach it to the type.  */
9975
9976 static void
9977 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9978                               struct dwarf2_cu *cu)
9979 {
9980   int nfields = fip->nfields;
9981
9982   /* Record the field count, allocate space for the array of fields,
9983      and create blank accessibility bitfields if necessary.  */
9984   TYPE_NFIELDS (type) = nfields;
9985   TYPE_FIELDS (type) = (struct field *)
9986     TYPE_ALLOC (type, sizeof (struct field) * nfields);
9987   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9988
9989   if (fip->non_public_fields && cu->language != language_ada)
9990     {
9991       ALLOCATE_CPLUS_STRUCT_TYPE (type);
9992
9993       TYPE_FIELD_PRIVATE_BITS (type) =
9994         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9995       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9996
9997       TYPE_FIELD_PROTECTED_BITS (type) =
9998         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9999       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
10000
10001       TYPE_FIELD_IGNORE_BITS (type) =
10002         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10003       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10004     }
10005
10006   /* If the type has baseclasses, allocate and clear a bit vector for
10007      TYPE_FIELD_VIRTUAL_BITS.  */
10008   if (fip->nbaseclasses && cu->language != language_ada)
10009     {
10010       int num_bytes = B_BYTES (fip->nbaseclasses);
10011       unsigned char *pointer;
10012
10013       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10014       pointer = TYPE_ALLOC (type, num_bytes);
10015       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10016       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10017       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10018     }
10019
10020   /* Copy the saved-up fields into the field vector.  Start from the head of
10021      the list, adding to the tail of the field array, so that they end up in
10022      the same order in the array in which they were added to the list.  */
10023   while (nfields-- > 0)
10024     {
10025       struct nextfield *fieldp;
10026
10027       if (fip->fields)
10028         {
10029           fieldp = fip->fields;
10030           fip->fields = fieldp->next;
10031         }
10032       else
10033         {
10034           fieldp = fip->baseclasses;
10035           fip->baseclasses = fieldp->next;
10036         }
10037
10038       TYPE_FIELD (type, nfields) = fieldp->field;
10039       switch (fieldp->accessibility)
10040         {
10041         case DW_ACCESS_private:
10042           if (cu->language != language_ada)
10043             SET_TYPE_FIELD_PRIVATE (type, nfields);
10044           break;
10045
10046         case DW_ACCESS_protected:
10047           if (cu->language != language_ada)
10048             SET_TYPE_FIELD_PROTECTED (type, nfields);
10049           break;
10050
10051         case DW_ACCESS_public:
10052           break;
10053
10054         default:
10055           /* Unknown accessibility.  Complain and treat it as public.  */
10056           {
10057             complaint (&symfile_complaints, _("unsupported accessibility %d"),
10058                        fieldp->accessibility);
10059           }
10060           break;
10061         }
10062       if (nfields < fip->nbaseclasses)
10063         {
10064           switch (fieldp->virtuality)
10065             {
10066             case DW_VIRTUALITY_virtual:
10067             case DW_VIRTUALITY_pure_virtual:
10068               if (cu->language == language_ada)
10069                 error (_("unexpected virtuality in component of Ada type"));
10070               SET_TYPE_FIELD_VIRTUAL (type, nfields);
10071               break;
10072             }
10073         }
10074     }
10075 }
10076
10077 /* Add a member function to the proper fieldlist.  */
10078
10079 static void
10080 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10081                       struct type *type, struct dwarf2_cu *cu)
10082 {
10083   struct objfile *objfile = cu->objfile;
10084   struct attribute *attr;
10085   struct fnfieldlist *flp;
10086   int i;
10087   struct fn_field *fnp;
10088   char *fieldname;
10089   struct nextfnfield *new_fnfield;
10090   struct type *this_type;
10091   enum dwarf_access_attribute accessibility;
10092
10093   if (cu->language == language_ada)
10094     error (_("unexpected member function in Ada type"));
10095
10096   /* Get name of member function.  */
10097   fieldname = dwarf2_name (die, cu);
10098   if (fieldname == NULL)
10099     return;
10100
10101   /* Look up member function name in fieldlist.  */
10102   for (i = 0; i < fip->nfnfields; i++)
10103     {
10104       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10105         break;
10106     }
10107
10108   /* Create new list element if necessary.  */
10109   if (i < fip->nfnfields)
10110     flp = &fip->fnfieldlists[i];
10111   else
10112     {
10113       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10114         {
10115           fip->fnfieldlists = (struct fnfieldlist *)
10116             xrealloc (fip->fnfieldlists,
10117                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10118                       * sizeof (struct fnfieldlist));
10119           if (fip->nfnfields == 0)
10120             make_cleanup (free_current_contents, &fip->fnfieldlists);
10121         }
10122       flp = &fip->fnfieldlists[fip->nfnfields];
10123       flp->name = fieldname;
10124       flp->length = 0;
10125       flp->head = NULL;
10126       i = fip->nfnfields++;
10127     }
10128
10129   /* Create a new member function field and chain it to the field list
10130      entry.  */
10131   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10132   make_cleanup (xfree, new_fnfield);
10133   memset (new_fnfield, 0, sizeof (struct nextfnfield));
10134   new_fnfield->next = flp->head;
10135   flp->head = new_fnfield;
10136   flp->length++;
10137
10138   /* Fill in the member function field info.  */
10139   fnp = &new_fnfield->fnfield;
10140
10141   /* Delay processing of the physname until later.  */
10142   if (cu->language == language_cplus || cu->language == language_java)
10143     {
10144       add_to_method_list (type, i, flp->length - 1, fieldname,
10145                           die, cu);
10146     }
10147   else
10148     {
10149       const char *physname = dwarf2_physname (fieldname, die, cu);
10150       fnp->physname = physname ? physname : "";
10151     }
10152
10153   fnp->type = alloc_type (objfile);
10154   this_type = read_type_die (die, cu);
10155   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10156     {
10157       int nparams = TYPE_NFIELDS (this_type);
10158
10159       /* TYPE is the domain of this method, and THIS_TYPE is the type
10160            of the method itself (TYPE_CODE_METHOD).  */
10161       smash_to_method_type (fnp->type, type,
10162                             TYPE_TARGET_TYPE (this_type),
10163                             TYPE_FIELDS (this_type),
10164                             TYPE_NFIELDS (this_type),
10165                             TYPE_VARARGS (this_type));
10166
10167       /* Handle static member functions.
10168          Dwarf2 has no clean way to discern C++ static and non-static
10169          member functions.  G++ helps GDB by marking the first
10170          parameter for non-static member functions (which is the this
10171          pointer) as artificial.  We obtain this information from
10172          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
10173       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10174         fnp->voffset = VOFFSET_STATIC;
10175     }
10176   else
10177     complaint (&symfile_complaints, _("member function type missing for '%s'"),
10178                dwarf2_full_name (fieldname, die, cu));
10179
10180   /* Get fcontext from DW_AT_containing_type if present.  */
10181   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10182     fnp->fcontext = die_containing_type (die, cu);
10183
10184   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10185      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
10186
10187   /* Get accessibility.  */
10188   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10189   if (attr)
10190     accessibility = DW_UNSND (attr);
10191   else
10192     accessibility = dwarf2_default_access_attribute (die, cu);
10193   switch (accessibility)
10194     {
10195     case DW_ACCESS_private:
10196       fnp->is_private = 1;
10197       break;
10198     case DW_ACCESS_protected:
10199       fnp->is_protected = 1;
10200       break;
10201     }
10202
10203   /* Check for artificial methods.  */
10204   attr = dwarf2_attr (die, DW_AT_artificial, cu);
10205   if (attr && DW_UNSND (attr) != 0)
10206     fnp->is_artificial = 1;
10207
10208   /* Get index in virtual function table if it is a virtual member
10209      function.  For older versions of GCC, this is an offset in the
10210      appropriate virtual table, as specified by DW_AT_containing_type.
10211      For everyone else, it is an expression to be evaluated relative
10212      to the object address.  */
10213
10214   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10215   if (attr)
10216     {
10217       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10218         {
10219           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10220             {
10221               /* Old-style GCC.  */
10222               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10223             }
10224           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10225                    || (DW_BLOCK (attr)->size > 1
10226                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10227                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10228             {
10229               struct dwarf_block blk;
10230               int offset;
10231
10232               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10233                         ? 1 : 2);
10234               blk.size = DW_BLOCK (attr)->size - offset;
10235               blk.data = DW_BLOCK (attr)->data + offset;
10236               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10237               if ((fnp->voffset % cu->header.addr_size) != 0)
10238                 dwarf2_complex_location_expr_complaint ();
10239               else
10240                 fnp->voffset /= cu->header.addr_size;
10241               fnp->voffset += 2;
10242             }
10243           else
10244             dwarf2_complex_location_expr_complaint ();
10245
10246           if (!fnp->fcontext)
10247             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10248         }
10249       else if (attr_form_is_section_offset (attr))
10250         {
10251           dwarf2_complex_location_expr_complaint ();
10252         }
10253       else
10254         {
10255           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10256                                                  fieldname);
10257         }
10258     }
10259   else
10260     {
10261       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10262       if (attr && DW_UNSND (attr))
10263         {
10264           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
10265           complaint (&symfile_complaints,
10266                      _("Member function \"%s\" (offset %d) is virtual "
10267                        "but the vtable offset is not specified"),
10268                      fieldname, die->offset.sect_off);
10269           ALLOCATE_CPLUS_STRUCT_TYPE (type);
10270           TYPE_CPLUS_DYNAMIC (type) = 1;
10271         }
10272     }
10273 }
10274
10275 /* Create the vector of member function fields, and attach it to the type.  */
10276
10277 static void
10278 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10279                                  struct dwarf2_cu *cu)
10280 {
10281   struct fnfieldlist *flp;
10282   int i;
10283
10284   if (cu->language == language_ada)
10285     error (_("unexpected member functions in Ada type"));
10286
10287   ALLOCATE_CPLUS_STRUCT_TYPE (type);
10288   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10289     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10290
10291   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10292     {
10293       struct nextfnfield *nfp = flp->head;
10294       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10295       int k;
10296
10297       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10298       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10299       fn_flp->fn_fields = (struct fn_field *)
10300         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10301       for (k = flp->length; (k--, nfp); nfp = nfp->next)
10302         fn_flp->fn_fields[k] = nfp->fnfield;
10303     }
10304
10305   TYPE_NFN_FIELDS (type) = fip->nfnfields;
10306 }
10307
10308 /* Returns non-zero if NAME is the name of a vtable member in CU's
10309    language, zero otherwise.  */
10310 static int
10311 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10312 {
10313   static const char vptr[] = "_vptr";
10314   static const char vtable[] = "vtable";
10315
10316   /* Look for the C++ and Java forms of the vtable.  */
10317   if ((cu->language == language_java
10318        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10319        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10320        && is_cplus_marker (name[sizeof (vptr) - 1])))
10321     return 1;
10322
10323   return 0;
10324 }
10325
10326 /* GCC outputs unnamed structures that are really pointers to member
10327    functions, with the ABI-specified layout.  If TYPE describes
10328    such a structure, smash it into a member function type.
10329
10330    GCC shouldn't do this; it should just output pointer to member DIEs.
10331    This is GCC PR debug/28767.  */
10332
10333 static void
10334 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10335 {
10336   struct type *pfn_type, *domain_type, *new_type;
10337
10338   /* Check for a structure with no name and two children.  */
10339   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10340     return;
10341
10342   /* Check for __pfn and __delta members.  */
10343   if (TYPE_FIELD_NAME (type, 0) == NULL
10344       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10345       || TYPE_FIELD_NAME (type, 1) == NULL
10346       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10347     return;
10348
10349   /* Find the type of the method.  */
10350   pfn_type = TYPE_FIELD_TYPE (type, 0);
10351   if (pfn_type == NULL
10352       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10353       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10354     return;
10355
10356   /* Look for the "this" argument.  */
10357   pfn_type = TYPE_TARGET_TYPE (pfn_type);
10358   if (TYPE_NFIELDS (pfn_type) == 0
10359       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10360       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10361     return;
10362
10363   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10364   new_type = alloc_type (objfile);
10365   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10366                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10367                         TYPE_VARARGS (pfn_type));
10368   smash_to_methodptr_type (type, new_type);
10369 }
10370
10371 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10372    (icc).  */
10373
10374 static int
10375 producer_is_icc (struct dwarf2_cu *cu)
10376 {
10377   if (!cu->checked_producer)
10378     check_producer (cu);
10379
10380   return cu->producer_is_icc;
10381 }
10382
10383 /* Called when we find the DIE that starts a structure or union scope
10384    (definition) to create a type for the structure or union.  Fill in
10385    the type's name and general properties; the members will not be
10386    processed until process_structure_type.
10387
10388    NOTE: we need to call these functions regardless of whether or not the
10389    DIE has a DW_AT_name attribute, since it might be an anonymous
10390    structure or union.  This gets the type entered into our set of
10391    user defined types.
10392
10393    However, if the structure is incomplete (an opaque struct/union)
10394    then suppress creating a symbol table entry for it since gdb only
10395    wants to find the one with the complete definition.  Note that if
10396    it is complete, we just call new_symbol, which does it's own
10397    checking about whether the struct/union is anonymous or not (and
10398    suppresses creating a symbol table entry itself).  */
10399
10400 static struct type *
10401 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10402 {
10403   struct objfile *objfile = cu->objfile;
10404   struct type *type;
10405   struct attribute *attr;
10406   char *name;
10407
10408   /* If the definition of this type lives in .debug_types, read that type.
10409      Don't follow DW_AT_specification though, that will take us back up
10410      the chain and we want to go down.  */
10411   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10412   if (attr)
10413     {
10414       struct dwarf2_cu *type_cu = cu;
10415       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10416
10417       /* We could just recurse on read_structure_type, but we need to call
10418          get_die_type to ensure only one type for this DIE is created.
10419          This is important, for example, because for c++ classes we need
10420          TYPE_NAME set which is only done by new_symbol.  Blech.  */
10421       type = read_type_die (type_die, type_cu);
10422
10423       /* TYPE_CU may not be the same as CU.
10424          Ensure TYPE is recorded in CU's type_hash table.  */
10425       return set_die_type (die, type, cu);
10426     }
10427
10428   type = alloc_type (objfile);
10429   INIT_CPLUS_SPECIFIC (type);
10430
10431   name = dwarf2_name (die, cu);
10432   if (name != NULL)
10433     {
10434       if (cu->language == language_cplus
10435           || cu->language == language_java)
10436         {
10437           char *full_name = (char *) dwarf2_full_name (name, die, cu);
10438
10439           /* dwarf2_full_name might have already finished building the DIE's
10440              type.  If so, there is no need to continue.  */
10441           if (get_die_type (die, cu) != NULL)
10442             return get_die_type (die, cu);
10443
10444           TYPE_TAG_NAME (type) = full_name;
10445           if (die->tag == DW_TAG_structure_type
10446               || die->tag == DW_TAG_class_type)
10447             TYPE_NAME (type) = TYPE_TAG_NAME (type);
10448         }
10449       else
10450         {
10451           /* The name is already allocated along with this objfile, so
10452              we don't need to duplicate it for the type.  */
10453           TYPE_TAG_NAME (type) = (char *) name;
10454           if (die->tag == DW_TAG_class_type)
10455             TYPE_NAME (type) = TYPE_TAG_NAME (type);
10456         }
10457     }
10458
10459   if (die->tag == DW_TAG_structure_type)
10460     {
10461       TYPE_CODE (type) = TYPE_CODE_STRUCT;
10462     }
10463   else if (die->tag == DW_TAG_union_type)
10464     {
10465       TYPE_CODE (type) = TYPE_CODE_UNION;
10466     }
10467   else
10468     {
10469       TYPE_CODE (type) = TYPE_CODE_CLASS;
10470     }
10471
10472   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10473     TYPE_DECLARED_CLASS (type) = 1;
10474
10475   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10476   if (attr)
10477     {
10478       TYPE_LENGTH (type) = DW_UNSND (attr);
10479     }
10480   else
10481     {
10482       TYPE_LENGTH (type) = 0;
10483     }
10484
10485   if (producer_is_icc (cu))
10486     {
10487       /* ICC does not output the required DW_AT_declaration
10488          on incomplete types, but gives them a size of zero.  */
10489     }
10490   else
10491     TYPE_STUB_SUPPORTED (type) = 1;
10492
10493   if (die_is_declaration (die, cu))
10494     TYPE_STUB (type) = 1;
10495   else if (attr == NULL && die->child == NULL
10496            && producer_is_realview (cu->producer))
10497     /* RealView does not output the required DW_AT_declaration
10498        on incomplete types.  */
10499     TYPE_STUB (type) = 1;
10500
10501   /* We need to add the type field to the die immediately so we don't
10502      infinitely recurse when dealing with pointers to the structure
10503      type within the structure itself.  */
10504   set_die_type (die, type, cu);
10505
10506   /* set_die_type should be already done.  */
10507   set_descriptive_type (type, die, cu);
10508
10509   return type;
10510 }
10511
10512 /* Finish creating a structure or union type, including filling in
10513    its members and creating a symbol for it.  */
10514
10515 static void
10516 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10517 {
10518   struct objfile *objfile = cu->objfile;
10519   struct die_info *child_die = die->child;
10520   struct type *type;
10521
10522   type = get_die_type (die, cu);
10523   if (type == NULL)
10524     type = read_structure_type (die, cu);
10525
10526   if (die->child != NULL && ! die_is_declaration (die, cu))
10527     {
10528       struct field_info fi;
10529       struct die_info *child_die;
10530       VEC (symbolp) *template_args = NULL;
10531       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10532
10533       memset (&fi, 0, sizeof (struct field_info));
10534
10535       child_die = die->child;
10536
10537       while (child_die && child_die->tag)
10538         {
10539           if (child_die->tag == DW_TAG_member
10540               || child_die->tag == DW_TAG_variable)
10541             {
10542               /* NOTE: carlton/2002-11-05: A C++ static data member
10543                  should be a DW_TAG_member that is a declaration, but
10544                  all versions of G++ as of this writing (so through at
10545                  least 3.2.1) incorrectly generate DW_TAG_variable
10546                  tags for them instead.  */
10547               dwarf2_add_field (&fi, child_die, cu);
10548             }
10549           else if (child_die->tag == DW_TAG_subprogram)
10550             {
10551               /* C++ member function.  */
10552               dwarf2_add_member_fn (&fi, child_die, type, cu);
10553             }
10554           else if (child_die->tag == DW_TAG_inheritance)
10555             {
10556               /* C++ base class field.  */
10557               dwarf2_add_field (&fi, child_die, cu);
10558             }
10559           else if (child_die->tag == DW_TAG_typedef)
10560             dwarf2_add_typedef (&fi, child_die, cu);
10561           else if (child_die->tag == DW_TAG_template_type_param
10562                    || child_die->tag == DW_TAG_template_value_param)
10563             {
10564               struct symbol *arg = new_symbol (child_die, NULL, cu);
10565
10566               if (arg != NULL)
10567                 VEC_safe_push (symbolp, template_args, arg);
10568             }
10569
10570           child_die = sibling_die (child_die);
10571         }
10572
10573       /* Attach template arguments to type.  */
10574       if (! VEC_empty (symbolp, template_args))
10575         {
10576           ALLOCATE_CPLUS_STRUCT_TYPE (type);
10577           TYPE_N_TEMPLATE_ARGUMENTS (type)
10578             = VEC_length (symbolp, template_args);
10579           TYPE_TEMPLATE_ARGUMENTS (type)
10580             = obstack_alloc (&objfile->objfile_obstack,
10581                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
10582                               * sizeof (struct symbol *)));
10583           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10584                   VEC_address (symbolp, template_args),
10585                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
10586                    * sizeof (struct symbol *)));
10587           VEC_free (symbolp, template_args);
10588         }
10589
10590       /* Attach fields and member functions to the type.  */
10591       if (fi.nfields)
10592         dwarf2_attach_fields_to_type (&fi, type, cu);
10593       if (fi.nfnfields)
10594         {
10595           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10596
10597           /* Get the type which refers to the base class (possibly this
10598              class itself) which contains the vtable pointer for the current
10599              class from the DW_AT_containing_type attribute.  This use of
10600              DW_AT_containing_type is a GNU extension.  */
10601
10602           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10603             {
10604               struct type *t = die_containing_type (die, cu);
10605
10606               TYPE_VPTR_BASETYPE (type) = t;
10607               if (type == t)
10608                 {
10609                   int i;
10610
10611                   /* Our own class provides vtbl ptr.  */
10612                   for (i = TYPE_NFIELDS (t) - 1;
10613                        i >= TYPE_N_BASECLASSES (t);
10614                        --i)
10615                     {
10616                       const char *fieldname = TYPE_FIELD_NAME (t, i);
10617
10618                       if (is_vtable_name (fieldname, cu))
10619                         {
10620                           TYPE_VPTR_FIELDNO (type) = i;
10621                           break;
10622                         }
10623                     }
10624
10625                   /* Complain if virtual function table field not found.  */
10626                   if (i < TYPE_N_BASECLASSES (t))
10627                     complaint (&symfile_complaints,
10628                                _("virtual function table pointer "
10629                                  "not found when defining class '%s'"),
10630                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10631                                "");
10632                 }
10633               else
10634                 {
10635                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10636                 }
10637             }
10638           else if (cu->producer
10639                    && strncmp (cu->producer,
10640                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10641             {
10642               /* The IBM XLC compiler does not provide direct indication
10643                  of the containing type, but the vtable pointer is
10644                  always named __vfp.  */
10645
10646               int i;
10647
10648               for (i = TYPE_NFIELDS (type) - 1;
10649                    i >= TYPE_N_BASECLASSES (type);
10650                    --i)
10651                 {
10652                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10653                     {
10654                       TYPE_VPTR_FIELDNO (type) = i;
10655                       TYPE_VPTR_BASETYPE (type) = type;
10656                       break;
10657                     }
10658                 }
10659             }
10660         }
10661
10662       /* Copy fi.typedef_field_list linked list elements content into the
10663          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
10664       if (fi.typedef_field_list)
10665         {
10666           int i = fi.typedef_field_list_count;
10667
10668           ALLOCATE_CPLUS_STRUCT_TYPE (type);
10669           TYPE_TYPEDEF_FIELD_ARRAY (type)
10670             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10671           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10672
10673           /* Reverse the list order to keep the debug info elements order.  */
10674           while (--i >= 0)
10675             {
10676               struct typedef_field *dest, *src;
10677
10678               dest = &TYPE_TYPEDEF_FIELD (type, i);
10679               src = &fi.typedef_field_list->field;
10680               fi.typedef_field_list = fi.typedef_field_list->next;
10681               *dest = *src;
10682             }
10683         }
10684
10685       do_cleanups (back_to);
10686
10687       if (HAVE_CPLUS_STRUCT (type))
10688         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10689     }
10690
10691   quirk_gcc_member_function_pointer (type, objfile);
10692
10693   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10694      snapshots) has been known to create a die giving a declaration
10695      for a class that has, as a child, a die giving a definition for a
10696      nested class.  So we have to process our children even if the
10697      current die is a declaration.  Normally, of course, a declaration
10698      won't have any children at all.  */
10699
10700   while (child_die != NULL && child_die->tag)
10701     {
10702       if (child_die->tag == DW_TAG_member
10703           || child_die->tag == DW_TAG_variable
10704           || child_die->tag == DW_TAG_inheritance
10705           || child_die->tag == DW_TAG_template_value_param
10706           || child_die->tag == DW_TAG_template_type_param)
10707         {
10708           /* Do nothing.  */
10709         }
10710       else
10711         process_die (child_die, cu);
10712
10713       child_die = sibling_die (child_die);
10714     }
10715
10716   /* Do not consider external references.  According to the DWARF standard,
10717      these DIEs are identified by the fact that they have no byte_size
10718      attribute, and a declaration attribute.  */
10719   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10720       || !die_is_declaration (die, cu))
10721     new_symbol (die, type, cu);
10722 }
10723
10724 /* Given a DW_AT_enumeration_type die, set its type.  We do not
10725    complete the type's fields yet, or create any symbols.  */
10726
10727 static struct type *
10728 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10729 {
10730   struct objfile *objfile = cu->objfile;
10731   struct type *type;
10732   struct attribute *attr;
10733   const char *name;
10734
10735   /* If the definition of this type lives in .debug_types, read that type.
10736      Don't follow DW_AT_specification though, that will take us back up
10737      the chain and we want to go down.  */
10738   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10739   if (attr)
10740     {
10741       struct dwarf2_cu *type_cu = cu;
10742       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10743
10744       type = read_type_die (type_die, type_cu);
10745
10746       /* TYPE_CU may not be the same as CU.
10747          Ensure TYPE is recorded in CU's type_hash table.  */
10748       return set_die_type (die, type, cu);
10749     }
10750
10751   type = alloc_type (objfile);
10752
10753   TYPE_CODE (type) = TYPE_CODE_ENUM;
10754   name = dwarf2_full_name (NULL, die, cu);
10755   if (name != NULL)
10756     TYPE_TAG_NAME (type) = (char *) name;
10757
10758   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10759   if (attr)
10760     {
10761       TYPE_LENGTH (type) = DW_UNSND (attr);
10762     }
10763   else
10764     {
10765       TYPE_LENGTH (type) = 0;
10766     }
10767
10768   /* The enumeration DIE can be incomplete.  In Ada, any type can be
10769      declared as private in the package spec, and then defined only
10770      inside the package body.  Such types are known as Taft Amendment
10771      Types.  When another package uses such a type, an incomplete DIE
10772      may be generated by the compiler.  */
10773   if (die_is_declaration (die, cu))
10774     TYPE_STUB (type) = 1;
10775
10776   return set_die_type (die, type, cu);
10777 }
10778
10779 /* Given a pointer to a die which begins an enumeration, process all
10780    the dies that define the members of the enumeration, and create the
10781    symbol for the enumeration type.
10782
10783    NOTE: We reverse the order of the element list.  */
10784
10785 static void
10786 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10787 {
10788   struct type *this_type;
10789
10790   this_type = get_die_type (die, cu);
10791   if (this_type == NULL)
10792     this_type = read_enumeration_type (die, cu);
10793
10794   if (die->child != NULL)
10795     {
10796       struct die_info *child_die;
10797       struct symbol *sym;
10798       struct field *fields = NULL;
10799       int num_fields = 0;
10800       int unsigned_enum = 1;
10801       char *name;
10802       int flag_enum = 1;
10803       ULONGEST mask = 0;
10804
10805       child_die = die->child;
10806       while (child_die && child_die->tag)
10807         {
10808           if (child_die->tag != DW_TAG_enumerator)
10809             {
10810               process_die (child_die, cu);
10811             }
10812           else
10813             {
10814               name = dwarf2_name (child_die, cu);
10815               if (name)
10816                 {
10817                   sym = new_symbol (child_die, this_type, cu);
10818                   if (SYMBOL_VALUE (sym) < 0)
10819                     {
10820                       unsigned_enum = 0;
10821                       flag_enum = 0;
10822                     }
10823                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
10824                     flag_enum = 0;
10825                   else
10826                     mask |= SYMBOL_VALUE (sym);
10827
10828                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10829                     {
10830                       fields = (struct field *)
10831                         xrealloc (fields,
10832                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
10833                                   * sizeof (struct field));
10834                     }
10835
10836                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10837                   FIELD_TYPE (fields[num_fields]) = NULL;
10838                   SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10839                   FIELD_BITSIZE (fields[num_fields]) = 0;
10840
10841                   num_fields++;
10842                 }
10843             }
10844
10845           child_die = sibling_die (child_die);
10846         }
10847
10848       if (num_fields)
10849         {
10850           TYPE_NFIELDS (this_type) = num_fields;
10851           TYPE_FIELDS (this_type) = (struct field *)
10852             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10853           memcpy (TYPE_FIELDS (this_type), fields,
10854                   sizeof (struct field) * num_fields);
10855           xfree (fields);
10856         }
10857       if (unsigned_enum)
10858         TYPE_UNSIGNED (this_type) = 1;
10859       if (flag_enum)
10860         TYPE_FLAG_ENUM (this_type) = 1;
10861     }
10862
10863   /* If we are reading an enum from a .debug_types unit, and the enum
10864      is a declaration, and the enum is not the signatured type in the
10865      unit, then we do not want to add a symbol for it.  Adding a
10866      symbol would in some cases obscure the true definition of the
10867      enum, giving users an incomplete type when the definition is
10868      actually available.  Note that we do not want to do this for all
10869      enums which are just declarations, because C++0x allows forward
10870      enum declarations.  */
10871   if (cu->per_cu->is_debug_types
10872       && die_is_declaration (die, cu))
10873     {
10874       struct signatured_type *sig_type;
10875
10876       sig_type
10877         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10878                                             cu->per_cu->info_or_types_section,
10879                                             cu->per_cu->offset);
10880       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10881       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10882         return;
10883     }
10884
10885   new_symbol (die, this_type, cu);
10886 }
10887
10888 /* Extract all information from a DW_TAG_array_type DIE and put it in
10889    the DIE's type field.  For now, this only handles one dimensional
10890    arrays.  */
10891
10892 static struct type *
10893 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10894 {
10895   struct objfile *objfile = cu->objfile;
10896   struct die_info *child_die;
10897   struct type *type;
10898   struct type *element_type, *range_type, *index_type;
10899   struct type **range_types = NULL;
10900   struct attribute *attr;
10901   int ndim = 0;
10902   struct cleanup *back_to;
10903   char *name;
10904
10905   element_type = die_type (die, cu);
10906
10907   /* The die_type call above may have already set the type for this DIE.  */
10908   type = get_die_type (die, cu);
10909   if (type)
10910     return type;
10911
10912   /* Irix 6.2 native cc creates array types without children for
10913      arrays with unspecified length.  */
10914   if (die->child == NULL)
10915     {
10916       index_type = objfile_type (objfile)->builtin_int;
10917       range_type = create_range_type (NULL, index_type, 0, -1);
10918       type = create_array_type (NULL, element_type, range_type);
10919       return set_die_type (die, type, cu);
10920     }
10921
10922   back_to = make_cleanup (null_cleanup, NULL);
10923   child_die = die->child;
10924   while (child_die && child_die->tag)
10925     {
10926       if (child_die->tag == DW_TAG_subrange_type)
10927         {
10928           struct type *child_type = read_type_die (child_die, cu);
10929
10930           if (child_type != NULL)
10931             {
10932               /* The range type was succesfully read.  Save it for the
10933                  array type creation.  */
10934               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10935                 {
10936                   range_types = (struct type **)
10937                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10938                               * sizeof (struct type *));
10939                   if (ndim == 0)
10940                     make_cleanup (free_current_contents, &range_types);
10941                 }
10942               range_types[ndim++] = child_type;
10943             }
10944         }
10945       child_die = sibling_die (child_die);
10946     }
10947
10948   /* Dwarf2 dimensions are output from left to right, create the
10949      necessary array types in backwards order.  */
10950
10951   type = element_type;
10952
10953   if (read_array_order (die, cu) == DW_ORD_col_major)
10954     {
10955       int i = 0;
10956
10957       while (i < ndim)
10958         type = create_array_type (NULL, type, range_types[i++]);
10959     }
10960   else
10961     {
10962       while (ndim-- > 0)
10963         type = create_array_type (NULL, type, range_types[ndim]);
10964     }
10965
10966   /* Understand Dwarf2 support for vector types (like they occur on
10967      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
10968      array type.  This is not part of the Dwarf2/3 standard yet, but a
10969      custom vendor extension.  The main difference between a regular
10970      array and the vector variant is that vectors are passed by value
10971      to functions.  */
10972   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10973   if (attr)
10974     make_vector_type (type);
10975
10976   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
10977      implementation may choose to implement triple vectors using this
10978      attribute.  */
10979   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10980   if (attr)
10981     {
10982       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10983         TYPE_LENGTH (type) = DW_UNSND (attr);
10984       else
10985         complaint (&symfile_complaints,
10986                    _("DW_AT_byte_size for array type smaller "
10987                      "than the total size of elements"));
10988     }
10989
10990   name = dwarf2_name (die, cu);
10991   if (name)
10992     TYPE_NAME (type) = name;
10993
10994   /* Install the type in the die.  */
10995   set_die_type (die, type, cu);
10996
10997   /* set_die_type should be already done.  */
10998   set_descriptive_type (type, die, cu);
10999
11000   do_cleanups (back_to);
11001
11002   return type;
11003 }
11004
11005 static enum dwarf_array_dim_ordering
11006 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11007 {
11008   struct attribute *attr;
11009
11010   attr = dwarf2_attr (die, DW_AT_ordering, cu);
11011
11012   if (attr) return DW_SND (attr);
11013
11014   /* GNU F77 is a special case, as at 08/2004 array type info is the
11015      opposite order to the dwarf2 specification, but data is still
11016      laid out as per normal fortran.
11017
11018      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11019      version checking.  */
11020
11021   if (cu->language == language_fortran
11022       && cu->producer && strstr (cu->producer, "GNU F77"))
11023     {
11024       return DW_ORD_row_major;
11025     }
11026
11027   switch (cu->language_defn->la_array_ordering)
11028     {
11029     case array_column_major:
11030       return DW_ORD_col_major;
11031     case array_row_major:
11032     default:
11033       return DW_ORD_row_major;
11034     };
11035 }
11036
11037 /* Extract all information from a DW_TAG_set_type DIE and put it in
11038    the DIE's type field.  */
11039
11040 static struct type *
11041 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11042 {
11043   struct type *domain_type, *set_type;
11044   struct attribute *attr;
11045
11046   domain_type = die_type (die, cu);
11047
11048   /* The die_type call above may have already set the type for this DIE.  */
11049   set_type = get_die_type (die, cu);
11050   if (set_type)
11051     return set_type;
11052
11053   set_type = create_set_type (NULL, domain_type);
11054
11055   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11056   if (attr)
11057     TYPE_LENGTH (set_type) = DW_UNSND (attr);
11058
11059   return set_die_type (die, set_type, cu);
11060 }
11061
11062 /* First cut: install each common block member as a global variable.  */
11063
11064 static void
11065 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11066 {
11067   struct die_info *child_die;
11068   struct attribute *attr;
11069   struct symbol *sym;
11070   CORE_ADDR base = (CORE_ADDR) 0;
11071
11072   attr = dwarf2_attr (die, DW_AT_location, cu);
11073   if (attr)
11074     {
11075       /* Support the .debug_loc offsets.  */
11076       if (attr_form_is_block (attr))
11077         {
11078           base = decode_locdesc (DW_BLOCK (attr), cu);
11079         }
11080       else if (attr_form_is_section_offset (attr))
11081         {
11082           dwarf2_complex_location_expr_complaint ();
11083         }
11084       else
11085         {
11086           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11087                                                  "common block member");
11088         }
11089     }
11090   if (die->child != NULL)
11091     {
11092       child_die = die->child;
11093       while (child_die && child_die->tag)
11094         {
11095           LONGEST offset;
11096
11097           sym = new_symbol (child_die, NULL, cu);
11098           if (sym != NULL
11099               && handle_data_member_location (child_die, cu, &offset))
11100             {
11101               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
11102               add_symbol_to_list (sym, &global_symbols);
11103             }
11104           child_die = sibling_die (child_die);
11105         }
11106     }
11107 }
11108
11109 /* Create a type for a C++ namespace.  */
11110
11111 static struct type *
11112 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11113 {
11114   struct objfile *objfile = cu->objfile;
11115   const char *previous_prefix, *name;
11116   int is_anonymous;
11117   struct type *type;
11118
11119   /* For extensions, reuse the type of the original namespace.  */
11120   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11121     {
11122       struct die_info *ext_die;
11123       struct dwarf2_cu *ext_cu = cu;
11124
11125       ext_die = dwarf2_extension (die, &ext_cu);
11126       type = read_type_die (ext_die, ext_cu);
11127
11128       /* EXT_CU may not be the same as CU.
11129          Ensure TYPE is recorded in CU's type_hash table.  */
11130       return set_die_type (die, type, cu);
11131     }
11132
11133   name = namespace_name (die, &is_anonymous, cu);
11134
11135   /* Now build the name of the current namespace.  */
11136
11137   previous_prefix = determine_prefix (die, cu);
11138   if (previous_prefix[0] != '\0')
11139     name = typename_concat (&objfile->objfile_obstack,
11140                             previous_prefix, name, 0, cu);
11141
11142   /* Create the type.  */
11143   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11144                     objfile);
11145   TYPE_NAME (type) = (char *) name;
11146   TYPE_TAG_NAME (type) = TYPE_NAME (type);
11147
11148   return set_die_type (die, type, cu);
11149 }
11150
11151 /* Read a C++ namespace.  */
11152
11153 static void
11154 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11155 {
11156   struct objfile *objfile = cu->objfile;
11157   int is_anonymous;
11158
11159   /* Add a symbol associated to this if we haven't seen the namespace
11160      before.  Also, add a using directive if it's an anonymous
11161      namespace.  */
11162
11163   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11164     {
11165       struct type *type;
11166
11167       type = read_type_die (die, cu);
11168       new_symbol (die, type, cu);
11169
11170       namespace_name (die, &is_anonymous, cu);
11171       if (is_anonymous)
11172         {
11173           const char *previous_prefix = determine_prefix (die, cu);
11174
11175           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11176                                   NULL, NULL, &objfile->objfile_obstack);
11177         }
11178     }
11179
11180   if (die->child != NULL)
11181     {
11182       struct die_info *child_die = die->child;
11183
11184       while (child_die && child_die->tag)
11185         {
11186           process_die (child_die, cu);
11187           child_die = sibling_die (child_die);
11188         }
11189     }
11190 }
11191
11192 /* Read a Fortran module as type.  This DIE can be only a declaration used for
11193    imported module.  Still we need that type as local Fortran "use ... only"
11194    declaration imports depend on the created type in determine_prefix.  */
11195
11196 static struct type *
11197 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11198 {
11199   struct objfile *objfile = cu->objfile;
11200   char *module_name;
11201   struct type *type;
11202
11203   module_name = dwarf2_name (die, cu);
11204   if (!module_name)
11205     complaint (&symfile_complaints,
11206                _("DW_TAG_module has no name, offset 0x%x"),
11207                die->offset.sect_off);
11208   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11209
11210   /* determine_prefix uses TYPE_TAG_NAME.  */
11211   TYPE_TAG_NAME (type) = TYPE_NAME (type);
11212
11213   return set_die_type (die, type, cu);
11214 }
11215
11216 /* Read a Fortran module.  */
11217
11218 static void
11219 read_module (struct die_info *die, struct dwarf2_cu *cu)
11220 {
11221   struct die_info *child_die = die->child;
11222
11223   while (child_die && child_die->tag)
11224     {
11225       process_die (child_die, cu);
11226       child_die = sibling_die (child_die);
11227     }
11228 }
11229
11230 /* Return the name of the namespace represented by DIE.  Set
11231    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11232    namespace.  */
11233
11234 static const char *
11235 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11236 {
11237   struct die_info *current_die;
11238   const char *name = NULL;
11239
11240   /* Loop through the extensions until we find a name.  */
11241
11242   for (current_die = die;
11243        current_die != NULL;
11244        current_die = dwarf2_extension (die, &cu))
11245     {
11246       name = dwarf2_name (current_die, cu);
11247       if (name != NULL)
11248         break;
11249     }
11250
11251   /* Is it an anonymous namespace?  */
11252
11253   *is_anonymous = (name == NULL);
11254   if (*is_anonymous)
11255     name = CP_ANONYMOUS_NAMESPACE_STR;
11256
11257   return name;
11258 }
11259
11260 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11261    the user defined type vector.  */
11262
11263 static struct type *
11264 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11265 {
11266   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11267   struct comp_unit_head *cu_header = &cu->header;
11268   struct type *type;
11269   struct attribute *attr_byte_size;
11270   struct attribute *attr_address_class;
11271   int byte_size, addr_class;
11272   struct type *target_type;
11273
11274   target_type = die_type (die, cu);
11275
11276   /* The die_type call above may have already set the type for this DIE.  */
11277   type = get_die_type (die, cu);
11278   if (type)
11279     return type;
11280
11281   type = lookup_pointer_type (target_type);
11282
11283   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11284   if (attr_byte_size)
11285     byte_size = DW_UNSND (attr_byte_size);
11286   else
11287     byte_size = cu_header->addr_size;
11288
11289   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11290   if (attr_address_class)
11291     addr_class = DW_UNSND (attr_address_class);
11292   else
11293     addr_class = DW_ADDR_none;
11294
11295   /* If the pointer size or address class is different than the
11296      default, create a type variant marked as such and set the
11297      length accordingly.  */
11298   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11299     {
11300       if (gdbarch_address_class_type_flags_p (gdbarch))
11301         {
11302           int type_flags;
11303
11304           type_flags = gdbarch_address_class_type_flags
11305                          (gdbarch, byte_size, addr_class);
11306           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11307                       == 0);
11308           type = make_type_with_address_space (type, type_flags);
11309         }
11310       else if (TYPE_LENGTH (type) != byte_size)
11311         {
11312           complaint (&symfile_complaints,
11313                      _("invalid pointer size %d"), byte_size);
11314         }
11315       else
11316         {
11317           /* Should we also complain about unhandled address classes?  */
11318         }
11319     }
11320
11321   TYPE_LENGTH (type) = byte_size;
11322   return set_die_type (die, type, cu);
11323 }
11324
11325 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11326    the user defined type vector.  */
11327
11328 static struct type *
11329 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11330 {
11331   struct type *type;
11332   struct type *to_type;
11333   struct type *domain;
11334
11335   to_type = die_type (die, cu);
11336   domain = die_containing_type (die, cu);
11337
11338   /* The calls above may have already set the type for this DIE.  */
11339   type = get_die_type (die, cu);
11340   if (type)
11341     return type;
11342
11343   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11344     type = lookup_methodptr_type (to_type);
11345   else
11346     type = lookup_memberptr_type (to_type, domain);
11347
11348   return set_die_type (die, type, cu);
11349 }
11350
11351 /* Extract all information from a DW_TAG_reference_type DIE and add to
11352    the user defined type vector.  */
11353
11354 static struct type *
11355 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11356 {
11357   struct comp_unit_head *cu_header = &cu->header;
11358   struct type *type, *target_type;
11359   struct attribute *attr;
11360
11361   target_type = die_type (die, cu);
11362
11363   /* The die_type call above may have already set the type for this DIE.  */
11364   type = get_die_type (die, cu);
11365   if (type)
11366     return type;
11367
11368   type = lookup_reference_type (target_type);
11369   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11370   if (attr)
11371     {
11372       TYPE_LENGTH (type) = DW_UNSND (attr);
11373     }
11374   else
11375     {
11376       TYPE_LENGTH (type) = cu_header->addr_size;
11377     }
11378   return set_die_type (die, type, cu);
11379 }
11380
11381 static struct type *
11382 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11383 {
11384   struct type *base_type, *cv_type;
11385
11386   base_type = die_type (die, cu);
11387
11388   /* The die_type call above may have already set the type for this DIE.  */
11389   cv_type = get_die_type (die, cu);
11390   if (cv_type)
11391     return cv_type;
11392
11393   /* In case the const qualifier is applied to an array type, the element type
11394      is so qualified, not the array type (section 6.7.3 of C99).  */
11395   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11396     {
11397       struct type *el_type, *inner_array;
11398
11399       base_type = copy_type (base_type);
11400       inner_array = base_type;
11401
11402       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11403         {
11404           TYPE_TARGET_TYPE (inner_array) =
11405             copy_type (TYPE_TARGET_TYPE (inner_array));
11406           inner_array = TYPE_TARGET_TYPE (inner_array);
11407         }
11408
11409       el_type = TYPE_TARGET_TYPE (inner_array);
11410       TYPE_TARGET_TYPE (inner_array) =
11411         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11412
11413       return set_die_type (die, base_type, cu);
11414     }
11415
11416   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11417   return set_die_type (die, cv_type, cu);
11418 }
11419
11420 static struct type *
11421 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11422 {
11423   struct type *base_type, *cv_type;
11424
11425   base_type = die_type (die, cu);
11426
11427   /* The die_type call above may have already set the type for this DIE.  */
11428   cv_type = get_die_type (die, cu);
11429   if (cv_type)
11430     return cv_type;
11431
11432   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11433   return set_die_type (die, cv_type, cu);
11434 }
11435
11436 /* Extract all information from a DW_TAG_string_type DIE and add to
11437    the user defined type vector.  It isn't really a user defined type,
11438    but it behaves like one, with other DIE's using an AT_user_def_type
11439    attribute to reference it.  */
11440
11441 static struct type *
11442 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11443 {
11444   struct objfile *objfile = cu->objfile;
11445   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11446   struct type *type, *range_type, *index_type, *char_type;
11447   struct attribute *attr;
11448   unsigned int length;
11449
11450   attr = dwarf2_attr (die, DW_AT_string_length, cu);
11451   if (attr)
11452     {
11453       length = DW_UNSND (attr);
11454     }
11455   else
11456     {
11457       /* Check for the DW_AT_byte_size attribute.  */
11458       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11459       if (attr)
11460         {
11461           length = DW_UNSND (attr);
11462         }
11463       else
11464         {
11465           length = 1;
11466         }
11467     }
11468
11469   index_type = objfile_type (objfile)->builtin_int;
11470   range_type = create_range_type (NULL, index_type, 1, length);
11471   char_type = language_string_char_type (cu->language_defn, gdbarch);
11472   type = create_string_type (NULL, char_type, range_type);
11473
11474   return set_die_type (die, type, cu);
11475 }
11476
11477 /* Handle DIES due to C code like:
11478
11479    struct foo
11480    {
11481    int (*funcp)(int a, long l);
11482    int b;
11483    };
11484
11485    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
11486
11487 static struct type *
11488 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11489 {
11490   struct objfile *objfile = cu->objfile;
11491   struct type *type;            /* Type that this function returns.  */
11492   struct type *ftype;           /* Function that returns above type.  */
11493   struct attribute *attr;
11494
11495   type = die_type (die, cu);
11496
11497   /* The die_type call above may have already set the type for this DIE.  */
11498   ftype = get_die_type (die, cu);
11499   if (ftype)
11500     return ftype;
11501
11502   ftype = lookup_function_type (type);
11503
11504   /* All functions in C++, Pascal and Java have prototypes.  */
11505   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11506   if ((attr && (DW_UNSND (attr) != 0))
11507       || cu->language == language_cplus
11508       || cu->language == language_java
11509       || cu->language == language_pascal)
11510     TYPE_PROTOTYPED (ftype) = 1;
11511   else if (producer_is_realview (cu->producer))
11512     /* RealView does not emit DW_AT_prototyped.  We can not
11513        distinguish prototyped and unprototyped functions; default to
11514        prototyped, since that is more common in modern code (and
11515        RealView warns about unprototyped functions).  */
11516     TYPE_PROTOTYPED (ftype) = 1;
11517
11518   /* Store the calling convention in the type if it's available in
11519      the subroutine die.  Otherwise set the calling convention to
11520      the default value DW_CC_normal.  */
11521   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11522   if (attr)
11523     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11524   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11525     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11526   else
11527     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11528
11529   /* We need to add the subroutine type to the die immediately so
11530      we don't infinitely recurse when dealing with parameters
11531      declared as the same subroutine type.  */
11532   set_die_type (die, ftype, cu);
11533
11534   if (die->child != NULL)
11535     {
11536       struct type *void_type = objfile_type (objfile)->builtin_void;
11537       struct die_info *child_die;
11538       int nparams, iparams;
11539
11540       /* Count the number of parameters.
11541          FIXME: GDB currently ignores vararg functions, but knows about
11542          vararg member functions.  */
11543       nparams = 0;
11544       child_die = die->child;
11545       while (child_die && child_die->tag)
11546         {
11547           if (child_die->tag == DW_TAG_formal_parameter)
11548             nparams++;
11549           else if (child_die->tag == DW_TAG_unspecified_parameters)
11550             TYPE_VARARGS (ftype) = 1;
11551           child_die = sibling_die (child_die);
11552         }
11553
11554       /* Allocate storage for parameters and fill them in.  */
11555       TYPE_NFIELDS (ftype) = nparams;
11556       TYPE_FIELDS (ftype) = (struct field *)
11557         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11558
11559       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
11560          even if we error out during the parameters reading below.  */
11561       for (iparams = 0; iparams < nparams; iparams++)
11562         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11563
11564       iparams = 0;
11565       child_die = die->child;
11566       while (child_die && child_die->tag)
11567         {
11568           if (child_die->tag == DW_TAG_formal_parameter)
11569             {
11570               struct type *arg_type;
11571
11572               /* DWARF version 2 has no clean way to discern C++
11573                  static and non-static member functions.  G++ helps
11574                  GDB by marking the first parameter for non-static
11575                  member functions (which is the this pointer) as
11576                  artificial.  We pass this information to
11577                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11578
11579                  DWARF version 3 added DW_AT_object_pointer, which GCC
11580                  4.5 does not yet generate.  */
11581               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11582               if (attr)
11583                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11584               else
11585                 {
11586                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11587
11588                   /* GCC/43521: In java, the formal parameter
11589                      "this" is sometimes not marked with DW_AT_artificial.  */
11590                   if (cu->language == language_java)
11591                     {
11592                       const char *name = dwarf2_name (child_die, cu);
11593
11594                       if (name && !strcmp (name, "this"))
11595                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11596                     }
11597                 }
11598               arg_type = die_type (child_die, cu);
11599
11600               /* RealView does not mark THIS as const, which the testsuite
11601                  expects.  GCC marks THIS as const in method definitions,
11602                  but not in the class specifications (GCC PR 43053).  */
11603               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11604                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11605                 {
11606                   int is_this = 0;
11607                   struct dwarf2_cu *arg_cu = cu;
11608                   const char *name = dwarf2_name (child_die, cu);
11609
11610                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11611                   if (attr)
11612                     {
11613                       /* If the compiler emits this, use it.  */
11614                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
11615                         is_this = 1;
11616                     }
11617                   else if (name && strcmp (name, "this") == 0)
11618                     /* Function definitions will have the argument names.  */
11619                     is_this = 1;
11620                   else if (name == NULL && iparams == 0)
11621                     /* Declarations may not have the names, so like
11622                        elsewhere in GDB, assume an artificial first
11623                        argument is "this".  */
11624                     is_this = 1;
11625
11626                   if (is_this)
11627                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11628                                              arg_type, 0);
11629                 }
11630
11631               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11632               iparams++;
11633             }
11634           child_die = sibling_die (child_die);
11635         }
11636     }
11637
11638   return ftype;
11639 }
11640
11641 static struct type *
11642 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11643 {
11644   struct objfile *objfile = cu->objfile;
11645   const char *name = NULL;
11646   struct type *this_type, *target_type;
11647
11648   name = dwarf2_full_name (NULL, die, cu);
11649   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11650                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
11651   TYPE_NAME (this_type) = (char *) name;
11652   set_die_type (die, this_type, cu);
11653   target_type = die_type (die, cu);
11654   if (target_type != this_type)
11655     TYPE_TARGET_TYPE (this_type) = target_type;
11656   else
11657     {
11658       /* Self-referential typedefs are, it seems, not allowed by the DWARF
11659          spec and cause infinite loops in GDB.  */
11660       complaint (&symfile_complaints,
11661                  _("Self-referential DW_TAG_typedef "
11662                    "- DIE at 0x%x [in module %s]"),
11663                  die->offset.sect_off, objfile->name);
11664       TYPE_TARGET_TYPE (this_type) = NULL;
11665     }
11666   return this_type;
11667 }
11668
11669 /* Find a representation of a given base type and install
11670    it in the TYPE field of the die.  */
11671
11672 static struct type *
11673 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11674 {
11675   struct objfile *objfile = cu->objfile;
11676   struct type *type;
11677   struct attribute *attr;
11678   int encoding = 0, size = 0;
11679   char *name;
11680   enum type_code code = TYPE_CODE_INT;
11681   int type_flags = 0;
11682   struct type *target_type = NULL;
11683
11684   attr = dwarf2_attr (die, DW_AT_encoding, cu);
11685   if (attr)
11686     {
11687       encoding = DW_UNSND (attr);
11688     }
11689   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11690   if (attr)
11691     {
11692       size = DW_UNSND (attr);
11693     }
11694   name = dwarf2_name (die, cu);
11695   if (!name)
11696     {
11697       complaint (&symfile_complaints,
11698                  _("DW_AT_name missing from DW_TAG_base_type"));
11699     }
11700
11701   switch (encoding)
11702     {
11703       case DW_ATE_address:
11704         /* Turn DW_ATE_address into a void * pointer.  */
11705         code = TYPE_CODE_PTR;
11706         type_flags |= TYPE_FLAG_UNSIGNED;
11707         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11708         break;
11709       case DW_ATE_boolean:
11710         code = TYPE_CODE_BOOL;
11711         type_flags |= TYPE_FLAG_UNSIGNED;
11712         break;
11713       case DW_ATE_complex_float:
11714         code = TYPE_CODE_COMPLEX;
11715         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11716         break;
11717       case DW_ATE_decimal_float:
11718         code = TYPE_CODE_DECFLOAT;
11719         break;
11720       case DW_ATE_float:
11721         code = TYPE_CODE_FLT;
11722         break;
11723       case DW_ATE_signed:
11724         break;
11725       case DW_ATE_unsigned:
11726         type_flags |= TYPE_FLAG_UNSIGNED;
11727         if (cu->language == language_fortran
11728             && name
11729             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11730           code = TYPE_CODE_CHAR;
11731         break;
11732       case DW_ATE_signed_char:
11733         if (cu->language == language_ada || cu->language == language_m2
11734             || cu->language == language_pascal
11735             || cu->language == language_fortran)
11736           code = TYPE_CODE_CHAR;
11737         break;
11738       case DW_ATE_unsigned_char:
11739         if (cu->language == language_ada || cu->language == language_m2
11740             || cu->language == language_pascal
11741             || cu->language == language_fortran)
11742           code = TYPE_CODE_CHAR;
11743         type_flags |= TYPE_FLAG_UNSIGNED;
11744         break;
11745       case DW_ATE_UTF:
11746         /* We just treat this as an integer and then recognize the
11747            type by name elsewhere.  */
11748         break;
11749
11750       default:
11751         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11752                    dwarf_type_encoding_name (encoding));
11753         break;
11754     }
11755
11756   type = init_type (code, size, type_flags, NULL, objfile);
11757   TYPE_NAME (type) = name;
11758   TYPE_TARGET_TYPE (type) = target_type;
11759
11760   if (name && strcmp (name, "char") == 0)
11761     TYPE_NOSIGN (type) = 1;
11762
11763   return set_die_type (die, type, cu);
11764 }
11765
11766 /* Read the given DW_AT_subrange DIE.  */
11767
11768 static struct type *
11769 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11770 {
11771   struct type *base_type;
11772   struct type *range_type;
11773   struct attribute *attr;
11774   LONGEST low, high;
11775   int low_default_is_valid;
11776   char *name;
11777   LONGEST negative_mask;
11778
11779   base_type = die_type (die, cu);
11780   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
11781   check_typedef (base_type);
11782
11783   /* The die_type call above may have already set the type for this DIE.  */
11784   range_type = get_die_type (die, cu);
11785   if (range_type)
11786     return range_type;
11787
11788   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11789      omitting DW_AT_lower_bound.  */
11790   switch (cu->language)
11791     {
11792     case language_c:
11793     case language_cplus:
11794       low = 0;
11795       low_default_is_valid = 1;
11796       break;
11797     case language_fortran:
11798       low = 1;
11799       low_default_is_valid = 1;
11800       break;
11801     case language_d:
11802     case language_java:
11803     case language_objc:
11804       low = 0;
11805       low_default_is_valid = (cu->header.version >= 4);
11806       break;
11807     case language_ada:
11808     case language_m2:
11809     case language_pascal:
11810       low = 1;
11811       low_default_is_valid = (cu->header.version >= 4);
11812       break;
11813     default:
11814       low = 0;
11815       low_default_is_valid = 0;
11816       break;
11817     }
11818
11819   /* FIXME: For variable sized arrays either of these could be
11820      a variable rather than a constant value.  We'll allow it,
11821      but we don't know how to handle it.  */
11822   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11823   if (attr)
11824     low = dwarf2_get_attr_constant_value (attr, low);
11825   else if (!low_default_is_valid)
11826     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11827                                       "- DIE at 0x%x [in module %s]"),
11828                die->offset.sect_off, cu->objfile->name);
11829
11830   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11831   if (attr)
11832     {
11833       if (attr_form_is_block (attr) || is_ref_attr (attr))
11834         {
11835           /* GCC encodes arrays with unspecified or dynamic length
11836              with a DW_FORM_block1 attribute or a reference attribute.
11837              FIXME: GDB does not yet know how to handle dynamic
11838              arrays properly, treat them as arrays with unspecified
11839              length for now.
11840
11841              FIXME: jimb/2003-09-22: GDB does not really know
11842              how to handle arrays of unspecified length
11843              either; we just represent them as zero-length
11844              arrays.  Choose an appropriate upper bound given
11845              the lower bound we've computed above.  */
11846           high = low - 1;
11847         }
11848       else
11849         high = dwarf2_get_attr_constant_value (attr, 1);
11850     }
11851   else
11852     {
11853       attr = dwarf2_attr (die, DW_AT_count, cu);
11854       if (attr)
11855         {
11856           int count = dwarf2_get_attr_constant_value (attr, 1);
11857           high = low + count - 1;
11858         }
11859       else
11860         {
11861           /* Unspecified array length.  */
11862           high = low - 1;
11863         }
11864     }
11865
11866   /* Dwarf-2 specifications explicitly allows to create subrange types
11867      without specifying a base type.
11868      In that case, the base type must be set to the type of
11869      the lower bound, upper bound or count, in that order, if any of these
11870      three attributes references an object that has a type.
11871      If no base type is found, the Dwarf-2 specifications say that
11872      a signed integer type of size equal to the size of an address should
11873      be used.
11874      For the following C code: `extern char gdb_int [];'
11875      GCC produces an empty range DIE.
11876      FIXME: muller/2010-05-28: Possible references to object for low bound,
11877      high bound or count are not yet handled by this code.  */
11878   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11879     {
11880       struct objfile *objfile = cu->objfile;
11881       struct gdbarch *gdbarch = get_objfile_arch (objfile);
11882       int addr_size = gdbarch_addr_bit (gdbarch) /8;
11883       struct type *int_type = objfile_type (objfile)->builtin_int;
11884
11885       /* Test "int", "long int", and "long long int" objfile types,
11886          and select the first one having a size above or equal to the
11887          architecture address size.  */
11888       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11889         base_type = int_type;
11890       else
11891         {
11892           int_type = objfile_type (objfile)->builtin_long;
11893           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11894             base_type = int_type;
11895           else
11896             {
11897               int_type = objfile_type (objfile)->builtin_long_long;
11898               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11899                 base_type = int_type;
11900             }
11901         }
11902     }
11903
11904   negative_mask =
11905     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11906   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11907     low |= negative_mask;
11908   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11909     high |= negative_mask;
11910
11911   range_type = create_range_type (NULL, base_type, low, high);
11912
11913   /* Mark arrays with dynamic length at least as an array of unspecified
11914      length.  GDB could check the boundary but before it gets implemented at
11915      least allow accessing the array elements.  */
11916   if (attr && attr_form_is_block (attr))
11917     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11918
11919   /* Ada expects an empty array on no boundary attributes.  */
11920   if (attr == NULL && cu->language != language_ada)
11921     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11922
11923   name = dwarf2_name (die, cu);
11924   if (name)
11925     TYPE_NAME (range_type) = name;
11926
11927   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11928   if (attr)
11929     TYPE_LENGTH (range_type) = DW_UNSND (attr);
11930
11931   set_die_type (die, range_type, cu);
11932
11933   /* set_die_type should be already done.  */
11934   set_descriptive_type (range_type, die, cu);
11935
11936   return range_type;
11937 }
11938
11939 static struct type *
11940 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11941 {
11942   struct type *type;
11943
11944   /* For now, we only support the C meaning of an unspecified type: void.  */
11945
11946   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11947   TYPE_NAME (type) = dwarf2_name (die, cu);
11948
11949   return set_die_type (die, type, cu);
11950 }
11951
11952 /* Read a single die and all its descendents.  Set the die's sibling
11953    field to NULL; set other fields in the die correctly, and set all
11954    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
11955    location of the info_ptr after reading all of those dies.  PARENT
11956    is the parent of the die in question.  */
11957
11958 static struct die_info *
11959 read_die_and_children (const struct die_reader_specs *reader,
11960                        gdb_byte *info_ptr,
11961                        gdb_byte **new_info_ptr,
11962                        struct die_info *parent)
11963 {
11964   struct die_info *die;
11965   gdb_byte *cur_ptr;
11966   int has_children;
11967
11968   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11969   if (die == NULL)
11970     {
11971       *new_info_ptr = cur_ptr;
11972       return NULL;
11973     }
11974   store_in_ref_table (die, reader->cu);
11975
11976   if (has_children)
11977     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11978   else
11979     {
11980       die->child = NULL;
11981       *new_info_ptr = cur_ptr;
11982     }
11983
11984   die->sibling = NULL;
11985   die->parent = parent;
11986   return die;
11987 }
11988
11989 /* Read a die, all of its descendents, and all of its siblings; set
11990    all of the fields of all of the dies correctly.  Arguments are as
11991    in read_die_and_children.  */
11992
11993 static struct die_info *
11994 read_die_and_siblings (const struct die_reader_specs *reader,
11995                        gdb_byte *info_ptr,
11996                        gdb_byte **new_info_ptr,
11997                        struct die_info *parent)
11998 {
11999   struct die_info *first_die, *last_sibling;
12000   gdb_byte *cur_ptr;
12001
12002   cur_ptr = info_ptr;
12003   first_die = last_sibling = NULL;
12004
12005   while (1)
12006     {
12007       struct die_info *die
12008         = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12009
12010       if (die == NULL)
12011         {
12012           *new_info_ptr = cur_ptr;
12013           return first_die;
12014         }
12015
12016       if (!first_die)
12017         first_die = die;
12018       else
12019         last_sibling->sibling = die;
12020
12021       last_sibling = die;
12022     }
12023 }
12024
12025 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12026    attributes.
12027    The caller is responsible for filling in the extra attributes
12028    and updating (*DIEP)->num_attrs.
12029    Set DIEP to point to a newly allocated die with its information,
12030    except for its child, sibling, and parent fields.
12031    Set HAS_CHILDREN to tell whether the die has children or not.  */
12032
12033 static gdb_byte *
12034 read_full_die_1 (const struct die_reader_specs *reader,
12035                  struct die_info **diep, gdb_byte *info_ptr,
12036                  int *has_children, int num_extra_attrs)
12037 {
12038   unsigned int abbrev_number, bytes_read, i;
12039   sect_offset offset;
12040   struct abbrev_info *abbrev;
12041   struct die_info *die;
12042   struct dwarf2_cu *cu = reader->cu;
12043   bfd *abfd = reader->abfd;
12044
12045   offset.sect_off = info_ptr - reader->buffer;
12046   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12047   info_ptr += bytes_read;
12048   if (!abbrev_number)
12049     {
12050       *diep = NULL;
12051       *has_children = 0;
12052       return info_ptr;
12053     }
12054
12055   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12056   if (!abbrev)
12057     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12058            abbrev_number,
12059            bfd_get_filename (abfd));
12060
12061   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12062   die->offset = offset;
12063   die->tag = abbrev->tag;
12064   die->abbrev = abbrev_number;
12065
12066   /* Make the result usable.
12067      The caller needs to update num_attrs after adding the extra
12068      attributes.  */
12069   die->num_attrs = abbrev->num_attrs;
12070
12071   for (i = 0; i < abbrev->num_attrs; ++i)
12072     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12073                                info_ptr);
12074
12075   *diep = die;
12076   *has_children = abbrev->has_children;
12077   return info_ptr;
12078 }
12079
12080 /* Read a die and all its attributes.
12081    Set DIEP to point to a newly allocated die with its information,
12082    except for its child, sibling, and parent fields.
12083    Set HAS_CHILDREN to tell whether the die has children or not.  */
12084
12085 static gdb_byte *
12086 read_full_die (const struct die_reader_specs *reader,
12087                struct die_info **diep, gdb_byte *info_ptr,
12088                int *has_children)
12089 {
12090   return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12091 }
12092 \f
12093 /* Abbreviation tables.
12094
12095    In DWARF version 2, the description of the debugging information is
12096    stored in a separate .debug_abbrev section.  Before we read any
12097    dies from a section we read in all abbreviations and install them
12098    in a hash table.  */
12099
12100 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
12101
12102 static struct abbrev_info *
12103 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12104 {
12105   struct abbrev_info *abbrev;
12106
12107   abbrev = (struct abbrev_info *)
12108     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12109   memset (abbrev, 0, sizeof (struct abbrev_info));
12110   return abbrev;
12111 }
12112
12113 /* Add an abbreviation to the table.  */
12114
12115 static void
12116 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12117                          unsigned int abbrev_number,
12118                          struct abbrev_info *abbrev)
12119 {
12120   unsigned int hash_number;
12121
12122   hash_number = abbrev_number % ABBREV_HASH_SIZE;
12123   abbrev->next = abbrev_table->abbrevs[hash_number];
12124   abbrev_table->abbrevs[hash_number] = abbrev;
12125 }
12126
12127 /* Look up an abbrev in the table.
12128    Returns NULL if the abbrev is not found.  */
12129
12130 static struct abbrev_info *
12131 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12132                             unsigned int abbrev_number)
12133 {
12134   unsigned int hash_number;
12135   struct abbrev_info *abbrev;
12136
12137   hash_number = abbrev_number % ABBREV_HASH_SIZE;
12138   abbrev = abbrev_table->abbrevs[hash_number];
12139
12140   while (abbrev)
12141     {
12142       if (abbrev->number == abbrev_number)
12143         return abbrev;
12144       abbrev = abbrev->next;
12145     }
12146   return NULL;
12147 }
12148
12149 /* Read in an abbrev table.  */
12150
12151 static struct abbrev_table *
12152 abbrev_table_read_table (struct dwarf2_section_info *section,
12153                          sect_offset offset)
12154 {
12155   struct objfile *objfile = dwarf2_per_objfile->objfile;
12156   bfd *abfd = section->asection->owner;
12157   struct abbrev_table *abbrev_table;
12158   gdb_byte *abbrev_ptr;
12159   struct abbrev_info *cur_abbrev;
12160   unsigned int abbrev_number, bytes_read, abbrev_name;
12161   unsigned int abbrev_form;
12162   struct attr_abbrev *cur_attrs;
12163   unsigned int allocated_attrs;
12164
12165   abbrev_table = XMALLOC (struct abbrev_table);
12166   abbrev_table->offset = offset;
12167   obstack_init (&abbrev_table->abbrev_obstack);
12168   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12169                                          (ABBREV_HASH_SIZE
12170                                           * sizeof (struct abbrev_info *)));
12171   memset (abbrev_table->abbrevs, 0,
12172           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12173
12174   dwarf2_read_section (objfile, section);
12175   abbrev_ptr = section->buffer + offset.sect_off;
12176   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12177   abbrev_ptr += bytes_read;
12178
12179   allocated_attrs = ATTR_ALLOC_CHUNK;
12180   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12181
12182   /* Loop until we reach an abbrev number of 0.  */
12183   while (abbrev_number)
12184     {
12185       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12186
12187       /* read in abbrev header */
12188       cur_abbrev->number = abbrev_number;
12189       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12190       abbrev_ptr += bytes_read;
12191       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12192       abbrev_ptr += 1;
12193
12194       /* now read in declarations */
12195       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12196       abbrev_ptr += bytes_read;
12197       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12198       abbrev_ptr += bytes_read;
12199       while (abbrev_name)
12200         {
12201           if (cur_abbrev->num_attrs == allocated_attrs)
12202             {
12203               allocated_attrs += ATTR_ALLOC_CHUNK;
12204               cur_attrs
12205                 = xrealloc (cur_attrs, (allocated_attrs
12206                                         * sizeof (struct attr_abbrev)));
12207             }
12208
12209           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12210           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12211           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12212           abbrev_ptr += bytes_read;
12213           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12214           abbrev_ptr += bytes_read;
12215         }
12216
12217       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12218                                          (cur_abbrev->num_attrs
12219                                           * sizeof (struct attr_abbrev)));
12220       memcpy (cur_abbrev->attrs, cur_attrs,
12221               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12222
12223       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12224
12225       /* Get next abbreviation.
12226          Under Irix6 the abbreviations for a compilation unit are not
12227          always properly terminated with an abbrev number of 0.
12228          Exit loop if we encounter an abbreviation which we have
12229          already read (which means we are about to read the abbreviations
12230          for the next compile unit) or if the end of the abbreviation
12231          table is reached.  */
12232       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12233         break;
12234       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12235       abbrev_ptr += bytes_read;
12236       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12237         break;
12238     }
12239
12240   xfree (cur_attrs);
12241   return abbrev_table;
12242 }
12243
12244 /* Free the resources held by ABBREV_TABLE.  */
12245
12246 static void
12247 abbrev_table_free (struct abbrev_table *abbrev_table)
12248 {
12249   obstack_free (&abbrev_table->abbrev_obstack, NULL);
12250   xfree (abbrev_table);
12251 }
12252
12253 /* Same as abbrev_table_free but as a cleanup.
12254    We pass in a pointer to the pointer to the table so that we can
12255    set the pointer to NULL when we're done.  It also simplifies
12256    build_type_unit_groups.  */
12257
12258 static void
12259 abbrev_table_free_cleanup (void *table_ptr)
12260 {
12261   struct abbrev_table **abbrev_table_ptr = table_ptr;
12262
12263   if (*abbrev_table_ptr != NULL)
12264     abbrev_table_free (*abbrev_table_ptr);
12265   *abbrev_table_ptr = NULL;
12266 }
12267
12268 /* Read the abbrev table for CU from ABBREV_SECTION.  */
12269
12270 static void
12271 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12272                      struct dwarf2_section_info *abbrev_section)
12273 {
12274   cu->abbrev_table =
12275     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12276 }
12277
12278 /* Release the memory used by the abbrev table for a compilation unit.  */
12279
12280 static void
12281 dwarf2_free_abbrev_table (void *ptr_to_cu)
12282 {
12283   struct dwarf2_cu *cu = ptr_to_cu;
12284
12285   abbrev_table_free (cu->abbrev_table);
12286   /* Set this to NULL so that we SEGV if we try to read it later,
12287      and also because free_comp_unit verifies this is NULL.  */
12288   cu->abbrev_table = NULL;
12289 }
12290 \f
12291 /* Returns nonzero if TAG represents a type that we might generate a partial
12292    symbol for.  */
12293
12294 static int
12295 is_type_tag_for_partial (int tag)
12296 {
12297   switch (tag)
12298     {
12299 #if 0
12300     /* Some types that would be reasonable to generate partial symbols for,
12301        that we don't at present.  */
12302     case DW_TAG_array_type:
12303     case DW_TAG_file_type:
12304     case DW_TAG_ptr_to_member_type:
12305     case DW_TAG_set_type:
12306     case DW_TAG_string_type:
12307     case DW_TAG_subroutine_type:
12308 #endif
12309     case DW_TAG_base_type:
12310     case DW_TAG_class_type:
12311     case DW_TAG_interface_type:
12312     case DW_TAG_enumeration_type:
12313     case DW_TAG_structure_type:
12314     case DW_TAG_subrange_type:
12315     case DW_TAG_typedef:
12316     case DW_TAG_union_type:
12317       return 1;
12318     default:
12319       return 0;
12320     }
12321 }
12322
12323 /* Load all DIEs that are interesting for partial symbols into memory.  */
12324
12325 static struct partial_die_info *
12326 load_partial_dies (const struct die_reader_specs *reader,
12327                    gdb_byte *info_ptr, int building_psymtab)
12328 {
12329   struct dwarf2_cu *cu = reader->cu;
12330   struct objfile *objfile = cu->objfile;
12331   struct partial_die_info *part_die;
12332   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12333   struct abbrev_info *abbrev;
12334   unsigned int bytes_read;
12335   unsigned int load_all = 0;
12336   int nesting_level = 1;
12337
12338   parent_die = NULL;
12339   last_die = NULL;
12340
12341   gdb_assert (cu->per_cu != NULL);
12342   if (cu->per_cu->load_all_dies)
12343     load_all = 1;
12344
12345   cu->partial_dies
12346     = htab_create_alloc_ex (cu->header.length / 12,
12347                             partial_die_hash,
12348                             partial_die_eq,
12349                             NULL,
12350                             &cu->comp_unit_obstack,
12351                             hashtab_obstack_allocate,
12352                             dummy_obstack_deallocate);
12353
12354   part_die = obstack_alloc (&cu->comp_unit_obstack,
12355                             sizeof (struct partial_die_info));
12356
12357   while (1)
12358     {
12359       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12360
12361       /* A NULL abbrev means the end of a series of children.  */
12362       if (abbrev == NULL)
12363         {
12364           if (--nesting_level == 0)
12365             {
12366               /* PART_DIE was probably the last thing allocated on the
12367                  comp_unit_obstack, so we could call obstack_free
12368                  here.  We don't do that because the waste is small,
12369                  and will be cleaned up when we're done with this
12370                  compilation unit.  This way, we're also more robust
12371                  against other users of the comp_unit_obstack.  */
12372               return first_die;
12373             }
12374           info_ptr += bytes_read;
12375           last_die = parent_die;
12376           parent_die = parent_die->die_parent;
12377           continue;
12378         }
12379
12380       /* Check for template arguments.  We never save these; if
12381          they're seen, we just mark the parent, and go on our way.  */
12382       if (parent_die != NULL
12383           && cu->language == language_cplus
12384           && (abbrev->tag == DW_TAG_template_type_param
12385               || abbrev->tag == DW_TAG_template_value_param))
12386         {
12387           parent_die->has_template_arguments = 1;
12388
12389           if (!load_all)
12390             {
12391               /* We don't need a partial DIE for the template argument.  */
12392               info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12393               continue;
12394             }
12395         }
12396
12397       /* We only recurse into c++ subprograms looking for template arguments.
12398          Skip their other children.  */
12399       if (!load_all
12400           && cu->language == language_cplus
12401           && parent_die != NULL
12402           && parent_die->tag == DW_TAG_subprogram)
12403         {
12404           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12405           continue;
12406         }
12407
12408       /* Check whether this DIE is interesting enough to save.  Normally
12409          we would not be interested in members here, but there may be
12410          later variables referencing them via DW_AT_specification (for
12411          static members).  */
12412       if (!load_all
12413           && !is_type_tag_for_partial (abbrev->tag)
12414           && abbrev->tag != DW_TAG_constant
12415           && abbrev->tag != DW_TAG_enumerator
12416           && abbrev->tag != DW_TAG_subprogram
12417           && abbrev->tag != DW_TAG_lexical_block
12418           && abbrev->tag != DW_TAG_variable
12419           && abbrev->tag != DW_TAG_namespace
12420           && abbrev->tag != DW_TAG_module
12421           && abbrev->tag != DW_TAG_member
12422           && abbrev->tag != DW_TAG_imported_unit)
12423         {
12424           /* Otherwise we skip to the next sibling, if any.  */
12425           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12426           continue;
12427         }
12428
12429       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12430                                    info_ptr);
12431
12432       /* This two-pass algorithm for processing partial symbols has a
12433          high cost in cache pressure.  Thus, handle some simple cases
12434          here which cover the majority of C partial symbols.  DIEs
12435          which neither have specification tags in them, nor could have
12436          specification tags elsewhere pointing at them, can simply be
12437          processed and discarded.
12438
12439          This segment is also optional; scan_partial_symbols and
12440          add_partial_symbol will handle these DIEs if we chain
12441          them in normally.  When compilers which do not emit large
12442          quantities of duplicate debug information are more common,
12443          this code can probably be removed.  */
12444
12445       /* Any complete simple types at the top level (pretty much all
12446          of them, for a language without namespaces), can be processed
12447          directly.  */
12448       if (parent_die == NULL
12449           && part_die->has_specification == 0
12450           && part_die->is_declaration == 0
12451           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12452               || part_die->tag == DW_TAG_base_type
12453               || part_die->tag == DW_TAG_subrange_type))
12454         {
12455           if (building_psymtab && part_die->name != NULL)
12456             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12457                                  VAR_DOMAIN, LOC_TYPEDEF,
12458                                  &objfile->static_psymbols,
12459                                  0, (CORE_ADDR) 0, cu->language, objfile);
12460           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12461           continue;
12462         }
12463
12464       /* The exception for DW_TAG_typedef with has_children above is
12465          a workaround of GCC PR debug/47510.  In the case of this complaint
12466          type_name_no_tag_or_error will error on such types later.
12467
12468          GDB skipped children of DW_TAG_typedef by the shortcut above and then
12469          it could not find the child DIEs referenced later, this is checked
12470          above.  In correct DWARF DW_TAG_typedef should have no children.  */
12471
12472       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12473         complaint (&symfile_complaints,
12474                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12475                      "- DIE at 0x%x [in module %s]"),
12476                    part_die->offset.sect_off, objfile->name);
12477
12478       /* If we're at the second level, and we're an enumerator, and
12479          our parent has no specification (meaning possibly lives in a
12480          namespace elsewhere), then we can add the partial symbol now
12481          instead of queueing it.  */
12482       if (part_die->tag == DW_TAG_enumerator
12483           && parent_die != NULL
12484           && parent_die->die_parent == NULL
12485           && parent_die->tag == DW_TAG_enumeration_type
12486           && parent_die->has_specification == 0)
12487         {
12488           if (part_die->name == NULL)
12489             complaint (&symfile_complaints,
12490                        _("malformed enumerator DIE ignored"));
12491           else if (building_psymtab)
12492             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12493                                  VAR_DOMAIN, LOC_CONST,
12494                                  (cu->language == language_cplus
12495                                   || cu->language == language_java)
12496                                  ? &objfile->global_psymbols
12497                                  : &objfile->static_psymbols,
12498                                  0, (CORE_ADDR) 0, cu->language, objfile);
12499
12500           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12501           continue;
12502         }
12503
12504       /* We'll save this DIE so link it in.  */
12505       part_die->die_parent = parent_die;
12506       part_die->die_sibling = NULL;
12507       part_die->die_child = NULL;
12508
12509       if (last_die && last_die == parent_die)
12510         last_die->die_child = part_die;
12511       else if (last_die)
12512         last_die->die_sibling = part_die;
12513
12514       last_die = part_die;
12515
12516       if (first_die == NULL)
12517         first_die = part_die;
12518
12519       /* Maybe add the DIE to the hash table.  Not all DIEs that we
12520          find interesting need to be in the hash table, because we
12521          also have the parent/sibling/child chains; only those that we
12522          might refer to by offset later during partial symbol reading.
12523
12524          For now this means things that might have be the target of a
12525          DW_AT_specification, DW_AT_abstract_origin, or
12526          DW_AT_extension.  DW_AT_extension will refer only to
12527          namespaces; DW_AT_abstract_origin refers to functions (and
12528          many things under the function DIE, but we do not recurse
12529          into function DIEs during partial symbol reading) and
12530          possibly variables as well; DW_AT_specification refers to
12531          declarations.  Declarations ought to have the DW_AT_declaration
12532          flag.  It happens that GCC forgets to put it in sometimes, but
12533          only for functions, not for types.
12534
12535          Adding more things than necessary to the hash table is harmless
12536          except for the performance cost.  Adding too few will result in
12537          wasted time in find_partial_die, when we reread the compilation
12538          unit with load_all_dies set.  */
12539
12540       if (load_all
12541           || abbrev->tag == DW_TAG_constant
12542           || abbrev->tag == DW_TAG_subprogram
12543           || abbrev->tag == DW_TAG_variable
12544           || abbrev->tag == DW_TAG_namespace
12545           || part_die->is_declaration)
12546         {
12547           void **slot;
12548
12549           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12550                                            part_die->offset.sect_off, INSERT);
12551           *slot = part_die;
12552         }
12553
12554       part_die = obstack_alloc (&cu->comp_unit_obstack,
12555                                 sizeof (struct partial_die_info));
12556
12557       /* For some DIEs we want to follow their children (if any).  For C
12558          we have no reason to follow the children of structures; for other
12559          languages we have to, so that we can get at method physnames
12560          to infer fully qualified class names, for DW_AT_specification,
12561          and for C++ template arguments.  For C++, we also look one level
12562          inside functions to find template arguments (if the name of the
12563          function does not already contain the template arguments).
12564
12565          For Ada, we need to scan the children of subprograms and lexical
12566          blocks as well because Ada allows the definition of nested
12567          entities that could be interesting for the debugger, such as
12568          nested subprograms for instance.  */
12569       if (last_die->has_children
12570           && (load_all
12571               || last_die->tag == DW_TAG_namespace
12572               || last_die->tag == DW_TAG_module
12573               || last_die->tag == DW_TAG_enumeration_type
12574               || (cu->language == language_cplus
12575                   && last_die->tag == DW_TAG_subprogram
12576                   && (last_die->name == NULL
12577                       || strchr (last_die->name, '<') == NULL))
12578               || (cu->language != language_c
12579                   && (last_die->tag == DW_TAG_class_type
12580                       || last_die->tag == DW_TAG_interface_type
12581                       || last_die->tag == DW_TAG_structure_type
12582                       || last_die->tag == DW_TAG_union_type))
12583               || (cu->language == language_ada
12584                   && (last_die->tag == DW_TAG_subprogram
12585                       || last_die->tag == DW_TAG_lexical_block))))
12586         {
12587           nesting_level++;
12588           parent_die = last_die;
12589           continue;
12590         }
12591
12592       /* Otherwise we skip to the next sibling, if any.  */
12593       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12594
12595       /* Back to the top, do it again.  */
12596     }
12597 }
12598
12599 /* Read a minimal amount of information into the minimal die structure.  */
12600
12601 static gdb_byte *
12602 read_partial_die (const struct die_reader_specs *reader,
12603                   struct partial_die_info *part_die,
12604                   struct abbrev_info *abbrev, unsigned int abbrev_len,
12605                   gdb_byte *info_ptr)
12606 {
12607   struct dwarf2_cu *cu = reader->cu;
12608   struct objfile *objfile = cu->objfile;
12609   gdb_byte *buffer = reader->buffer;
12610   unsigned int i;
12611   struct attribute attr;
12612   int has_low_pc_attr = 0;
12613   int has_high_pc_attr = 0;
12614   int high_pc_relative = 0;
12615
12616   memset (part_die, 0, sizeof (struct partial_die_info));
12617
12618   part_die->offset.sect_off = info_ptr - buffer;
12619
12620   info_ptr += abbrev_len;
12621
12622   if (abbrev == NULL)
12623     return info_ptr;
12624
12625   part_die->tag = abbrev->tag;
12626   part_die->has_children = abbrev->has_children;
12627
12628   for (i = 0; i < abbrev->num_attrs; ++i)
12629     {
12630       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12631
12632       /* Store the data if it is of an attribute we want to keep in a
12633          partial symbol table.  */
12634       switch (attr.name)
12635         {
12636         case DW_AT_name:
12637           switch (part_die->tag)
12638             {
12639             case DW_TAG_compile_unit:
12640             case DW_TAG_partial_unit:
12641             case DW_TAG_type_unit:
12642               /* Compilation units have a DW_AT_name that is a filename, not
12643                  a source language identifier.  */
12644             case DW_TAG_enumeration_type:
12645             case DW_TAG_enumerator:
12646               /* These tags always have simple identifiers already; no need
12647                  to canonicalize them.  */
12648               part_die->name = DW_STRING (&attr);
12649               break;
12650             default:
12651               part_die->name
12652                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12653                                             &objfile->objfile_obstack);
12654               break;
12655             }
12656           break;
12657         case DW_AT_linkage_name:
12658         case DW_AT_MIPS_linkage_name:
12659           /* Note that both forms of linkage name might appear.  We
12660              assume they will be the same, and we only store the last
12661              one we see.  */
12662           if (cu->language == language_ada)
12663             part_die->name = DW_STRING (&attr);
12664           part_die->linkage_name = DW_STRING (&attr);
12665           break;
12666         case DW_AT_low_pc:
12667           has_low_pc_attr = 1;
12668           part_die->lowpc = DW_ADDR (&attr);
12669           break;
12670         case DW_AT_high_pc:
12671           has_high_pc_attr = 1;
12672           if (attr.form == DW_FORM_addr
12673               || attr.form == DW_FORM_GNU_addr_index)
12674             part_die->highpc = DW_ADDR (&attr);
12675           else
12676             {
12677               high_pc_relative = 1;
12678               part_die->highpc = DW_UNSND (&attr);
12679             }
12680           break;
12681         case DW_AT_location:
12682           /* Support the .debug_loc offsets.  */
12683           if (attr_form_is_block (&attr))
12684             {
12685                part_die->d.locdesc = DW_BLOCK (&attr);
12686             }
12687           else if (attr_form_is_section_offset (&attr))
12688             {
12689               dwarf2_complex_location_expr_complaint ();
12690             }
12691           else
12692             {
12693               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12694                                                      "partial symbol information");
12695             }
12696           break;
12697         case DW_AT_external:
12698           part_die->is_external = DW_UNSND (&attr);
12699           break;
12700         case DW_AT_declaration:
12701           part_die->is_declaration = DW_UNSND (&attr);
12702           break;
12703         case DW_AT_type:
12704           part_die->has_type = 1;
12705           break;
12706         case DW_AT_abstract_origin:
12707         case DW_AT_specification:
12708         case DW_AT_extension:
12709           part_die->has_specification = 1;
12710           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12711           part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12712                                    || cu->per_cu->is_dwz);
12713           break;
12714         case DW_AT_sibling:
12715           /* Ignore absolute siblings, they might point outside of
12716              the current compile unit.  */
12717           if (attr.form == DW_FORM_ref_addr)
12718             complaint (&symfile_complaints,
12719                        _("ignoring absolute DW_AT_sibling"));
12720           else
12721             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12722           break;
12723         case DW_AT_byte_size:
12724           part_die->has_byte_size = 1;
12725           break;
12726         case DW_AT_calling_convention:
12727           /* DWARF doesn't provide a way to identify a program's source-level
12728              entry point.  DW_AT_calling_convention attributes are only meant
12729              to describe functions' calling conventions.
12730
12731              However, because it's a necessary piece of information in
12732              Fortran, and because DW_CC_program is the only piece of debugging
12733              information whose definition refers to a 'main program' at all,
12734              several compilers have begun marking Fortran main programs with
12735              DW_CC_program --- even when those functions use the standard
12736              calling conventions.
12737
12738              So until DWARF specifies a way to provide this information and
12739              compilers pick up the new representation, we'll support this
12740              practice.  */
12741           if (DW_UNSND (&attr) == DW_CC_program
12742               && cu->language == language_fortran)
12743             {
12744               set_main_name (part_die->name);
12745
12746               /* As this DIE has a static linkage the name would be difficult
12747                  to look up later.  */
12748               language_of_main = language_fortran;
12749             }
12750           break;
12751         case DW_AT_inline:
12752           if (DW_UNSND (&attr) == DW_INL_inlined
12753               || DW_UNSND (&attr) == DW_INL_declared_inlined)
12754             part_die->may_be_inlined = 1;
12755           break;
12756
12757         case DW_AT_import:
12758           if (part_die->tag == DW_TAG_imported_unit)
12759             {
12760               part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12761               part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12762                                   || cu->per_cu->is_dwz);
12763             }
12764           break;
12765
12766         default:
12767           break;
12768         }
12769     }
12770
12771   if (high_pc_relative)
12772     part_die->highpc += part_die->lowpc;
12773
12774   if (has_low_pc_attr && has_high_pc_attr)
12775     {
12776       /* When using the GNU linker, .gnu.linkonce. sections are used to
12777          eliminate duplicate copies of functions and vtables and such.
12778          The linker will arbitrarily choose one and discard the others.
12779          The AT_*_pc values for such functions refer to local labels in
12780          these sections.  If the section from that file was discarded, the
12781          labels are not in the output, so the relocs get a value of 0.
12782          If this is a discarded function, mark the pc bounds as invalid,
12783          so that GDB will ignore it.  */
12784       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12785         {
12786           struct gdbarch *gdbarch = get_objfile_arch (objfile);
12787
12788           complaint (&symfile_complaints,
12789                      _("DW_AT_low_pc %s is zero "
12790                        "for DIE at 0x%x [in module %s]"),
12791                      paddress (gdbarch, part_die->lowpc),
12792                      part_die->offset.sect_off, objfile->name);
12793         }
12794       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
12795       else if (part_die->lowpc >= part_die->highpc)
12796         {
12797           struct gdbarch *gdbarch = get_objfile_arch (objfile);
12798
12799           complaint (&symfile_complaints,
12800                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12801                        "for DIE at 0x%x [in module %s]"),
12802                      paddress (gdbarch, part_die->lowpc),
12803                      paddress (gdbarch, part_die->highpc),
12804                      part_die->offset.sect_off, objfile->name);
12805         }
12806       else
12807         part_die->has_pc_info = 1;
12808     }
12809
12810   return info_ptr;
12811 }
12812
12813 /* Find a cached partial DIE at OFFSET in CU.  */
12814
12815 static struct partial_die_info *
12816 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12817 {
12818   struct partial_die_info *lookup_die = NULL;
12819   struct partial_die_info part_die;
12820
12821   part_die.offset = offset;
12822   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12823                                     offset.sect_off);
12824
12825   return lookup_die;
12826 }
12827
12828 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12829    except in the case of .debug_types DIEs which do not reference
12830    outside their CU (they do however referencing other types via
12831    DW_FORM_ref_sig8).  */
12832
12833 static struct partial_die_info *
12834 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
12835 {
12836   struct objfile *objfile = cu->objfile;
12837   struct dwarf2_per_cu_data *per_cu = NULL;
12838   struct partial_die_info *pd = NULL;
12839
12840   if (offset_in_dwz == cu->per_cu->is_dwz
12841       && offset_in_cu_p (&cu->header, offset))
12842     {
12843       pd = find_partial_die_in_comp_unit (offset, cu);
12844       if (pd != NULL)
12845         return pd;
12846       /* We missed recording what we needed.
12847          Load all dies and try again.  */
12848       per_cu = cu->per_cu;
12849     }
12850   else
12851     {
12852       /* TUs don't reference other CUs/TUs (except via type signatures).  */
12853       if (cu->per_cu->is_debug_types)
12854         {
12855           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12856                    " external reference to offset 0x%lx [in module %s].\n"),
12857                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
12858                  bfd_get_filename (objfile->obfd));
12859         }
12860       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12861                                                  objfile);
12862
12863       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12864         load_partial_comp_unit (per_cu);
12865
12866       per_cu->cu->last_used = 0;
12867       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12868     }
12869
12870   /* If we didn't find it, and not all dies have been loaded,
12871      load them all and try again.  */
12872
12873   if (pd == NULL && per_cu->load_all_dies == 0)
12874     {
12875       per_cu->load_all_dies = 1;
12876
12877       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
12878          THIS_CU->cu may already be in use.  So we can't just free it and
12879          replace its DIEs with the ones we read in.  Instead, we leave those
12880          DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12881          and clobber THIS_CU->cu->partial_dies with the hash table for the new
12882          set.  */
12883       load_partial_comp_unit (per_cu);
12884
12885       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12886     }
12887
12888   if (pd == NULL)
12889     internal_error (__FILE__, __LINE__,
12890                     _("could not find partial DIE 0x%x "
12891                       "in cache [from module %s]\n"),
12892                     offset.sect_off, bfd_get_filename (objfile->obfd));
12893   return pd;
12894 }
12895
12896 /* See if we can figure out if the class lives in a namespace.  We do
12897    this by looking for a member function; its demangled name will
12898    contain namespace info, if there is any.  */
12899
12900 static void
12901 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12902                                   struct dwarf2_cu *cu)
12903 {
12904   /* NOTE: carlton/2003-10-07: Getting the info this way changes
12905      what template types look like, because the demangler
12906      frequently doesn't give the same name as the debug info.  We
12907      could fix this by only using the demangled name to get the
12908      prefix (but see comment in read_structure_type).  */
12909
12910   struct partial_die_info *real_pdi;
12911   struct partial_die_info *child_pdi;
12912
12913   /* If this DIE (this DIE's specification, if any) has a parent, then
12914      we should not do this.  We'll prepend the parent's fully qualified
12915      name when we create the partial symbol.  */
12916
12917   real_pdi = struct_pdi;
12918   while (real_pdi->has_specification)
12919     real_pdi = find_partial_die (real_pdi->spec_offset,
12920                                  real_pdi->spec_is_dwz, cu);
12921
12922   if (real_pdi->die_parent != NULL)
12923     return;
12924
12925   for (child_pdi = struct_pdi->die_child;
12926        child_pdi != NULL;
12927        child_pdi = child_pdi->die_sibling)
12928     {
12929       if (child_pdi->tag == DW_TAG_subprogram
12930           && child_pdi->linkage_name != NULL)
12931         {
12932           char *actual_class_name
12933             = language_class_name_from_physname (cu->language_defn,
12934                                                  child_pdi->linkage_name);
12935           if (actual_class_name != NULL)
12936             {
12937               struct_pdi->name
12938                 = obsavestring (actual_class_name,
12939                                 strlen (actual_class_name),
12940                                 &cu->objfile->objfile_obstack);
12941               xfree (actual_class_name);
12942             }
12943           break;
12944         }
12945     }
12946 }
12947
12948 /* Adjust PART_DIE before generating a symbol for it.  This function
12949    may set the is_external flag or change the DIE's name.  */
12950
12951 static void
12952 fixup_partial_die (struct partial_die_info *part_die,
12953                    struct dwarf2_cu *cu)
12954 {
12955   /* Once we've fixed up a die, there's no point in doing so again.
12956      This also avoids a memory leak if we were to call
12957      guess_partial_die_structure_name multiple times.  */
12958   if (part_die->fixup_called)
12959     return;
12960
12961   /* If we found a reference attribute and the DIE has no name, try
12962      to find a name in the referred to DIE.  */
12963
12964   if (part_die->name == NULL && part_die->has_specification)
12965     {
12966       struct partial_die_info *spec_die;
12967
12968       spec_die = find_partial_die (part_die->spec_offset,
12969                                    part_die->spec_is_dwz, cu);
12970
12971       fixup_partial_die (spec_die, cu);
12972
12973       if (spec_die->name)
12974         {
12975           part_die->name = spec_die->name;
12976
12977           /* Copy DW_AT_external attribute if it is set.  */
12978           if (spec_die->is_external)
12979             part_die->is_external = spec_die->is_external;
12980         }
12981     }
12982
12983   /* Set default names for some unnamed DIEs.  */
12984
12985   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12986     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12987
12988   /* If there is no parent die to provide a namespace, and there are
12989      children, see if we can determine the namespace from their linkage
12990      name.  */
12991   if (cu->language == language_cplus
12992       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12993       && part_die->die_parent == NULL
12994       && part_die->has_children
12995       && (part_die->tag == DW_TAG_class_type
12996           || part_die->tag == DW_TAG_structure_type
12997           || part_die->tag == DW_TAG_union_type))
12998     guess_partial_die_structure_name (part_die, cu);
12999
13000   /* GCC might emit a nameless struct or union that has a linkage
13001      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
13002   if (part_die->name == NULL
13003       && (part_die->tag == DW_TAG_class_type
13004           || part_die->tag == DW_TAG_interface_type
13005           || part_die->tag == DW_TAG_structure_type
13006           || part_die->tag == DW_TAG_union_type)
13007       && part_die->linkage_name != NULL)
13008     {
13009       char *demangled;
13010
13011       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13012       if (demangled)
13013         {
13014           const char *base;
13015
13016           /* Strip any leading namespaces/classes, keep only the base name.
13017              DW_AT_name for named DIEs does not contain the prefixes.  */
13018           base = strrchr (demangled, ':');
13019           if (base && base > demangled && base[-1] == ':')
13020             base++;
13021           else
13022             base = demangled;
13023
13024           part_die->name = obsavestring (base, strlen (base),
13025                                          &cu->objfile->objfile_obstack);
13026           xfree (demangled);
13027         }
13028     }
13029
13030   part_die->fixup_called = 1;
13031 }
13032
13033 /* Read an attribute value described by an attribute form.  */
13034
13035 static gdb_byte *
13036 read_attribute_value (const struct die_reader_specs *reader,
13037                       struct attribute *attr, unsigned form,
13038                       gdb_byte *info_ptr)
13039 {
13040   struct dwarf2_cu *cu = reader->cu;
13041   bfd *abfd = reader->abfd;
13042   struct comp_unit_head *cu_header = &cu->header;
13043   unsigned int bytes_read;
13044   struct dwarf_block *blk;
13045
13046   attr->form = form;
13047   switch (form)
13048     {
13049     case DW_FORM_ref_addr:
13050       if (cu->header.version == 2)
13051         DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13052       else
13053         DW_UNSND (attr) = read_offset (abfd, info_ptr,
13054                                        &cu->header, &bytes_read);
13055       info_ptr += bytes_read;
13056       break;
13057     case DW_FORM_GNU_ref_alt:
13058       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13059       info_ptr += bytes_read;
13060       break;
13061     case DW_FORM_addr:
13062       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13063       info_ptr += bytes_read;
13064       break;
13065     case DW_FORM_block2:
13066       blk = dwarf_alloc_block (cu);
13067       blk->size = read_2_bytes (abfd, info_ptr);
13068       info_ptr += 2;
13069       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13070       info_ptr += blk->size;
13071       DW_BLOCK (attr) = blk;
13072       break;
13073     case DW_FORM_block4:
13074       blk = dwarf_alloc_block (cu);
13075       blk->size = read_4_bytes (abfd, info_ptr);
13076       info_ptr += 4;
13077       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13078       info_ptr += blk->size;
13079       DW_BLOCK (attr) = blk;
13080       break;
13081     case DW_FORM_data2:
13082       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13083       info_ptr += 2;
13084       break;
13085     case DW_FORM_data4:
13086       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13087       info_ptr += 4;
13088       break;
13089     case DW_FORM_data8:
13090       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13091       info_ptr += 8;
13092       break;
13093     case DW_FORM_sec_offset:
13094       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13095       info_ptr += bytes_read;
13096       break;
13097     case DW_FORM_string:
13098       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13099       DW_STRING_IS_CANONICAL (attr) = 0;
13100       info_ptr += bytes_read;
13101       break;
13102     case DW_FORM_strp:
13103       if (!cu->per_cu->is_dwz)
13104         {
13105           DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13106                                                    &bytes_read);
13107           DW_STRING_IS_CANONICAL (attr) = 0;
13108           info_ptr += bytes_read;
13109           break;
13110         }
13111       /* FALLTHROUGH */
13112     case DW_FORM_GNU_strp_alt:
13113       {
13114         struct dwz_file *dwz = dwarf2_get_dwz_file ();
13115         LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13116                                           &bytes_read);
13117
13118         DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13119         DW_STRING_IS_CANONICAL (attr) = 0;
13120         info_ptr += bytes_read;
13121       }
13122       break;
13123     case DW_FORM_exprloc:
13124     case DW_FORM_block:
13125       blk = dwarf_alloc_block (cu);
13126       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13127       info_ptr += bytes_read;
13128       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13129       info_ptr += blk->size;
13130       DW_BLOCK (attr) = blk;
13131       break;
13132     case DW_FORM_block1:
13133       blk = dwarf_alloc_block (cu);
13134       blk->size = read_1_byte (abfd, info_ptr);
13135       info_ptr += 1;
13136       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13137       info_ptr += blk->size;
13138       DW_BLOCK (attr) = blk;
13139       break;
13140     case DW_FORM_data1:
13141       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13142       info_ptr += 1;
13143       break;
13144     case DW_FORM_flag:
13145       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13146       info_ptr += 1;
13147       break;
13148     case DW_FORM_flag_present:
13149       DW_UNSND (attr) = 1;
13150       break;
13151     case DW_FORM_sdata:
13152       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13153       info_ptr += bytes_read;
13154       break;
13155     case DW_FORM_udata:
13156       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13157       info_ptr += bytes_read;
13158       break;
13159     case DW_FORM_ref1:
13160       DW_UNSND (attr) = (cu->header.offset.sect_off
13161                          + read_1_byte (abfd, info_ptr));
13162       info_ptr += 1;
13163       break;
13164     case DW_FORM_ref2:
13165       DW_UNSND (attr) = (cu->header.offset.sect_off
13166                          + read_2_bytes (abfd, info_ptr));
13167       info_ptr += 2;
13168       break;
13169     case DW_FORM_ref4:
13170       DW_UNSND (attr) = (cu->header.offset.sect_off
13171                          + read_4_bytes (abfd, info_ptr));
13172       info_ptr += 4;
13173       break;
13174     case DW_FORM_ref8:
13175       DW_UNSND (attr) = (cu->header.offset.sect_off
13176                          + read_8_bytes (abfd, info_ptr));
13177       info_ptr += 8;
13178       break;
13179     case DW_FORM_ref_sig8:
13180       /* Convert the signature to something we can record in DW_UNSND
13181          for later lookup.
13182          NOTE: This is NULL if the type wasn't found.  */
13183       DW_SIGNATURED_TYPE (attr) =
13184         lookup_signatured_type (read_8_bytes (abfd, info_ptr));
13185       info_ptr += 8;
13186       break;
13187     case DW_FORM_ref_udata:
13188       DW_UNSND (attr) = (cu->header.offset.sect_off
13189                          + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
13190       info_ptr += bytes_read;
13191       break;
13192     case DW_FORM_indirect:
13193       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13194       info_ptr += bytes_read;
13195       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
13196       break;
13197     case DW_FORM_GNU_addr_index:
13198       if (reader->dwo_file == NULL)
13199         {
13200           /* For now flag a hard error.
13201              Later we can turn this into a complaint.  */
13202           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13203                  dwarf_form_name (form),
13204                  bfd_get_filename (abfd));
13205         }
13206       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13207       info_ptr += bytes_read;
13208       break;
13209     case DW_FORM_GNU_str_index:
13210       if (reader->dwo_file == NULL)
13211         {
13212           /* For now flag a hard error.
13213              Later we can turn this into a complaint if warranted.  */
13214           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13215                  dwarf_form_name (form),
13216                  bfd_get_filename (abfd));
13217         }
13218       {
13219         ULONGEST str_index =
13220           read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13221
13222         DW_STRING (attr) = read_str_index (reader, cu, str_index);
13223         DW_STRING_IS_CANONICAL (attr) = 0;
13224         info_ptr += bytes_read;
13225       }
13226       break;
13227     default:
13228       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13229              dwarf_form_name (form),
13230              bfd_get_filename (abfd));
13231     }
13232
13233   /* Super hack.  */
13234   if (cu->per_cu->is_dwz && is_ref_attr (attr))
13235     attr->form = DW_FORM_GNU_ref_alt;
13236
13237   /* We have seen instances where the compiler tried to emit a byte
13238      size attribute of -1 which ended up being encoded as an unsigned
13239      0xffffffff.  Although 0xffffffff is technically a valid size value,
13240      an object of this size seems pretty unlikely so we can relatively
13241      safely treat these cases as if the size attribute was invalid and
13242      treat them as zero by default.  */
13243   if (attr->name == DW_AT_byte_size
13244       && form == DW_FORM_data4
13245       && DW_UNSND (attr) >= 0xffffffff)
13246     {
13247       complaint
13248         (&symfile_complaints,
13249          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13250          hex_string (DW_UNSND (attr)));
13251       DW_UNSND (attr) = 0;
13252     }
13253
13254   return info_ptr;
13255 }
13256
13257 /* Read an attribute described by an abbreviated attribute.  */
13258
13259 static gdb_byte *
13260 read_attribute (const struct die_reader_specs *reader,
13261                 struct attribute *attr, struct attr_abbrev *abbrev,
13262                 gdb_byte *info_ptr)
13263 {
13264   attr->name = abbrev->name;
13265   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13266 }
13267
13268 /* Read dwarf information from a buffer.  */
13269
13270 static unsigned int
13271 read_1_byte (bfd *abfd, gdb_byte *buf)
13272 {
13273   return bfd_get_8 (abfd, buf);
13274 }
13275
13276 static int
13277 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13278 {
13279   return bfd_get_signed_8 (abfd, buf);
13280 }
13281
13282 static unsigned int
13283 read_2_bytes (bfd *abfd, gdb_byte *buf)
13284 {
13285   return bfd_get_16 (abfd, buf);
13286 }
13287
13288 static int
13289 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13290 {
13291   return bfd_get_signed_16 (abfd, buf);
13292 }
13293
13294 static unsigned int
13295 read_4_bytes (bfd *abfd, gdb_byte *buf)
13296 {
13297   return bfd_get_32 (abfd, buf);
13298 }
13299
13300 static int
13301 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13302 {
13303   return bfd_get_signed_32 (abfd, buf);
13304 }
13305
13306 static ULONGEST
13307 read_8_bytes (bfd *abfd, gdb_byte *buf)
13308 {
13309   return bfd_get_64 (abfd, buf);
13310 }
13311
13312 static CORE_ADDR
13313 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13314               unsigned int *bytes_read)
13315 {
13316   struct comp_unit_head *cu_header = &cu->header;
13317   CORE_ADDR retval = 0;
13318
13319   if (cu_header->signed_addr_p)
13320     {
13321       switch (cu_header->addr_size)
13322         {
13323         case 2:
13324           retval = bfd_get_signed_16 (abfd, buf);
13325           break;
13326         case 4:
13327           retval = bfd_get_signed_32 (abfd, buf);
13328           break;
13329         case 8:
13330           retval = bfd_get_signed_64 (abfd, buf);
13331           break;
13332         default:
13333           internal_error (__FILE__, __LINE__,
13334                           _("read_address: bad switch, signed [in module %s]"),
13335                           bfd_get_filename (abfd));
13336         }
13337     }
13338   else
13339     {
13340       switch (cu_header->addr_size)
13341         {
13342         case 2:
13343           retval = bfd_get_16 (abfd, buf);
13344           break;
13345         case 4:
13346           retval = bfd_get_32 (abfd, buf);
13347           break;
13348         case 8:
13349           retval = bfd_get_64 (abfd, buf);
13350           break;
13351         default:
13352           internal_error (__FILE__, __LINE__,
13353                           _("read_address: bad switch, "
13354                             "unsigned [in module %s]"),
13355                           bfd_get_filename (abfd));
13356         }
13357     }
13358
13359   *bytes_read = cu_header->addr_size;
13360   return retval;
13361 }
13362
13363 /* Read the initial length from a section.  The (draft) DWARF 3
13364    specification allows the initial length to take up either 4 bytes
13365    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
13366    bytes describe the length and all offsets will be 8 bytes in length
13367    instead of 4.
13368
13369    An older, non-standard 64-bit format is also handled by this
13370    function.  The older format in question stores the initial length
13371    as an 8-byte quantity without an escape value.  Lengths greater
13372    than 2^32 aren't very common which means that the initial 4 bytes
13373    is almost always zero.  Since a length value of zero doesn't make
13374    sense for the 32-bit format, this initial zero can be considered to
13375    be an escape value which indicates the presence of the older 64-bit
13376    format.  As written, the code can't detect (old format) lengths
13377    greater than 4GB.  If it becomes necessary to handle lengths
13378    somewhat larger than 4GB, we could allow other small values (such
13379    as the non-sensical values of 1, 2, and 3) to also be used as
13380    escape values indicating the presence of the old format.
13381
13382    The value returned via bytes_read should be used to increment the
13383    relevant pointer after calling read_initial_length().
13384
13385    [ Note:  read_initial_length() and read_offset() are based on the
13386      document entitled "DWARF Debugging Information Format", revision
13387      3, draft 8, dated November 19, 2001.  This document was obtained
13388      from:
13389
13390         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13391
13392      This document is only a draft and is subject to change.  (So beware.)
13393
13394      Details regarding the older, non-standard 64-bit format were
13395      determined empirically by examining 64-bit ELF files produced by
13396      the SGI toolchain on an IRIX 6.5 machine.
13397
13398      - Kevin, July 16, 2002
13399    ] */
13400
13401 static LONGEST
13402 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13403 {
13404   LONGEST length = bfd_get_32 (abfd, buf);
13405
13406   if (length == 0xffffffff)
13407     {
13408       length = bfd_get_64 (abfd, buf + 4);
13409       *bytes_read = 12;
13410     }
13411   else if (length == 0)
13412     {
13413       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
13414       length = bfd_get_64 (abfd, buf);
13415       *bytes_read = 8;
13416     }
13417   else
13418     {
13419       *bytes_read = 4;
13420     }
13421
13422   return length;
13423 }
13424
13425 /* Cover function for read_initial_length.
13426    Returns the length of the object at BUF, and stores the size of the
13427    initial length in *BYTES_READ and stores the size that offsets will be in
13428    *OFFSET_SIZE.
13429    If the initial length size is not equivalent to that specified in
13430    CU_HEADER then issue a complaint.
13431    This is useful when reading non-comp-unit headers.  */
13432
13433 static LONGEST
13434 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13435                                         const struct comp_unit_head *cu_header,
13436                                         unsigned int *bytes_read,
13437                                         unsigned int *offset_size)
13438 {
13439   LONGEST length = read_initial_length (abfd, buf, bytes_read);
13440
13441   gdb_assert (cu_header->initial_length_size == 4
13442               || cu_header->initial_length_size == 8
13443               || cu_header->initial_length_size == 12);
13444
13445   if (cu_header->initial_length_size != *bytes_read)
13446     complaint (&symfile_complaints,
13447                _("intermixed 32-bit and 64-bit DWARF sections"));
13448
13449   *offset_size = (*bytes_read == 4) ? 4 : 8;
13450   return length;
13451 }
13452
13453 /* Read an offset from the data stream.  The size of the offset is
13454    given by cu_header->offset_size.  */
13455
13456 static LONGEST
13457 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13458              unsigned int *bytes_read)
13459 {
13460   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13461
13462   *bytes_read = cu_header->offset_size;
13463   return offset;
13464 }
13465
13466 /* Read an offset from the data stream.  */
13467
13468 static LONGEST
13469 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13470 {
13471   LONGEST retval = 0;
13472
13473   switch (offset_size)
13474     {
13475     case 4:
13476       retval = bfd_get_32 (abfd, buf);
13477       break;
13478     case 8:
13479       retval = bfd_get_64 (abfd, buf);
13480       break;
13481     default:
13482       internal_error (__FILE__, __LINE__,
13483                       _("read_offset_1: bad switch [in module %s]"),
13484                       bfd_get_filename (abfd));
13485     }
13486
13487   return retval;
13488 }
13489
13490 static gdb_byte *
13491 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13492 {
13493   /* If the size of a host char is 8 bits, we can return a pointer
13494      to the buffer, otherwise we have to copy the data to a buffer
13495      allocated on the temporary obstack.  */
13496   gdb_assert (HOST_CHAR_BIT == 8);
13497   return buf;
13498 }
13499
13500 static char *
13501 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13502 {
13503   /* If the size of a host char is 8 bits, we can return a pointer
13504      to the string, otherwise we have to copy the string to a buffer
13505      allocated on the temporary obstack.  */
13506   gdb_assert (HOST_CHAR_BIT == 8);
13507   if (*buf == '\0')
13508     {
13509       *bytes_read_ptr = 1;
13510       return NULL;
13511     }
13512   *bytes_read_ptr = strlen ((char *) buf) + 1;
13513   return (char *) buf;
13514 }
13515
13516 static char *
13517 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13518 {
13519   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13520   if (dwarf2_per_objfile->str.buffer == NULL)
13521     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13522            bfd_get_filename (abfd));
13523   if (str_offset >= dwarf2_per_objfile->str.size)
13524     error (_("DW_FORM_strp pointing outside of "
13525              ".debug_str section [in module %s]"),
13526            bfd_get_filename (abfd));
13527   gdb_assert (HOST_CHAR_BIT == 8);
13528   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13529     return NULL;
13530   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13531 }
13532
13533 /* Read a string at offset STR_OFFSET in the .debug_str section from
13534    the .dwz file DWZ.  Throw an error if the offset is too large.  If
13535    the string consists of a single NUL byte, return NULL; otherwise
13536    return a pointer to the string.  */
13537
13538 static char *
13539 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13540 {
13541   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13542
13543   if (dwz->str.buffer == NULL)
13544     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13545              "section [in module %s]"),
13546            bfd_get_filename (dwz->dwz_bfd));
13547   if (str_offset >= dwz->str.size)
13548     error (_("DW_FORM_GNU_strp_alt pointing outside of "
13549              ".debug_str section [in module %s]"),
13550            bfd_get_filename (dwz->dwz_bfd));
13551   gdb_assert (HOST_CHAR_BIT == 8);
13552   if (dwz->str.buffer[str_offset] == '\0')
13553     return NULL;
13554   return (char *) (dwz->str.buffer + str_offset);
13555 }
13556
13557 static char *
13558 read_indirect_string (bfd *abfd, gdb_byte *buf,
13559                       const struct comp_unit_head *cu_header,
13560                       unsigned int *bytes_read_ptr)
13561 {
13562   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13563
13564   return read_indirect_string_at_offset (abfd, str_offset);
13565 }
13566
13567 static ULONGEST
13568 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13569 {
13570   ULONGEST result;
13571   unsigned int num_read;
13572   int i, shift;
13573   unsigned char byte;
13574
13575   result = 0;
13576   shift = 0;
13577   num_read = 0;
13578   i = 0;
13579   while (1)
13580     {
13581       byte = bfd_get_8 (abfd, buf);
13582       buf++;
13583       num_read++;
13584       result |= ((ULONGEST) (byte & 127) << shift);
13585       if ((byte & 128) == 0)
13586         {
13587           break;
13588         }
13589       shift += 7;
13590     }
13591   *bytes_read_ptr = num_read;
13592   return result;
13593 }
13594
13595 static LONGEST
13596 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13597 {
13598   LONGEST result;
13599   int i, shift, num_read;
13600   unsigned char byte;
13601
13602   result = 0;
13603   shift = 0;
13604   num_read = 0;
13605   i = 0;
13606   while (1)
13607     {
13608       byte = bfd_get_8 (abfd, buf);
13609       buf++;
13610       num_read++;
13611       result |= ((LONGEST) (byte & 127) << shift);
13612       shift += 7;
13613       if ((byte & 128) == 0)
13614         {
13615           break;
13616         }
13617     }
13618   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13619     result |= -(((LONGEST) 1) << shift);
13620   *bytes_read_ptr = num_read;
13621   return result;
13622 }
13623
13624 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13625    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13626    ADDR_SIZE is the size of addresses from the CU header.  */
13627
13628 static CORE_ADDR
13629 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13630 {
13631   struct objfile *objfile = dwarf2_per_objfile->objfile;
13632   bfd *abfd = objfile->obfd;
13633   const gdb_byte *info_ptr;
13634
13635   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13636   if (dwarf2_per_objfile->addr.buffer == NULL)
13637     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13638            objfile->name);
13639   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13640     error (_("DW_FORM_addr_index pointing outside of "
13641              ".debug_addr section [in module %s]"),
13642            objfile->name);
13643   info_ptr = (dwarf2_per_objfile->addr.buffer
13644               + addr_base + addr_index * addr_size);
13645   if (addr_size == 4)
13646     return bfd_get_32 (abfd, info_ptr);
13647   else
13648     return bfd_get_64 (abfd, info_ptr);
13649 }
13650
13651 /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */
13652
13653 static CORE_ADDR
13654 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13655 {
13656   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13657 }
13658
13659 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
13660
13661 static CORE_ADDR
13662 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13663                              unsigned int *bytes_read)
13664 {
13665   bfd *abfd = cu->objfile->obfd;
13666   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13667
13668   return read_addr_index (cu, addr_index);
13669 }
13670
13671 /* Data structure to pass results from dwarf2_read_addr_index_reader
13672    back to dwarf2_read_addr_index.  */
13673
13674 struct dwarf2_read_addr_index_data
13675 {
13676   ULONGEST addr_base;
13677   int addr_size;
13678 };
13679
13680 /* die_reader_func for dwarf2_read_addr_index.  */
13681
13682 static void
13683 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13684                                gdb_byte *info_ptr,
13685                                struct die_info *comp_unit_die,
13686                                int has_children,
13687                                void *data)
13688 {
13689   struct dwarf2_cu *cu = reader->cu;
13690   struct dwarf2_read_addr_index_data *aidata =
13691     (struct dwarf2_read_addr_index_data *) data;
13692
13693   aidata->addr_base = cu->addr_base;
13694   aidata->addr_size = cu->header.addr_size;
13695 }
13696
13697 /* Given an index in .debug_addr, fetch the value.
13698    NOTE: This can be called during dwarf expression evaluation,
13699    long after the debug information has been read, and thus per_cu->cu
13700    may no longer exist.  */
13701
13702 CORE_ADDR
13703 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13704                         unsigned int addr_index)
13705 {
13706   struct objfile *objfile = per_cu->objfile;
13707   struct dwarf2_cu *cu = per_cu->cu;
13708   ULONGEST addr_base;
13709   int addr_size;
13710
13711   /* This is intended to be called from outside this file.  */
13712   dw2_setup (objfile);
13713
13714   /* We need addr_base and addr_size.
13715      If we don't have PER_CU->cu, we have to get it.
13716      Nasty, but the alternative is storing the needed info in PER_CU,
13717      which at this point doesn't seem justified: it's not clear how frequently
13718      it would get used and it would increase the size of every PER_CU.
13719      Entry points like dwarf2_per_cu_addr_size do a similar thing
13720      so we're not in uncharted territory here.
13721      Alas we need to be a bit more complicated as addr_base is contained
13722      in the DIE.
13723
13724      We don't need to read the entire CU(/TU).
13725      We just need the header and top level die.
13726      IWBN to use the aging mechanism to let us lazily later discard the CU.
13727      See however init_cutu_and_read_dies_simple.  */
13728
13729   if (cu != NULL)
13730     {
13731       addr_base = cu->addr_base;
13732       addr_size = cu->header.addr_size;
13733     }
13734   else
13735     {
13736       struct dwarf2_read_addr_index_data aidata;
13737
13738       init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13739                                       &aidata);
13740       addr_base = aidata.addr_base;
13741       addr_size = aidata.addr_size;
13742     }
13743
13744   return read_addr_index_1 (addr_index, addr_base, addr_size);
13745 }
13746
13747 /* Given a DW_AT_str_index, fetch the string.  */
13748
13749 static char *
13750 read_str_index (const struct die_reader_specs *reader,
13751                 struct dwarf2_cu *cu, ULONGEST str_index)
13752 {
13753   struct objfile *objfile = dwarf2_per_objfile->objfile;
13754   const char *dwo_name = objfile->name;
13755   bfd *abfd = objfile->obfd;
13756   struct dwo_sections *sections = &reader->dwo_file->sections;
13757   gdb_byte *info_ptr;
13758   ULONGEST str_offset;
13759
13760   dwarf2_read_section (objfile, &sections->str);
13761   dwarf2_read_section (objfile, &sections->str_offsets);
13762   if (sections->str.buffer == NULL)
13763     error (_("DW_FORM_str_index used without .debug_str.dwo section"
13764              " in CU at offset 0x%lx [in module %s]"),
13765            (long) cu->header.offset.sect_off, dwo_name);
13766   if (sections->str_offsets.buffer == NULL)
13767     error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13768              " in CU at offset 0x%lx [in module %s]"),
13769            (long) cu->header.offset.sect_off, dwo_name);
13770   if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13771     error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13772              " section in CU at offset 0x%lx [in module %s]"),
13773            (long) cu->header.offset.sect_off, dwo_name);
13774   info_ptr = (sections->str_offsets.buffer
13775               + str_index * cu->header.offset_size);
13776   if (cu->header.offset_size == 4)
13777     str_offset = bfd_get_32 (abfd, info_ptr);
13778   else
13779     str_offset = bfd_get_64 (abfd, info_ptr);
13780   if (str_offset >= sections->str.size)
13781     error (_("Offset from DW_FORM_str_index pointing outside of"
13782              " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13783            (long) cu->header.offset.sect_off, dwo_name);
13784   return (char *) (sections->str.buffer + str_offset);
13785 }
13786
13787 /* Return the length of an LEB128 number in BUF.  */
13788
13789 static int
13790 leb128_size (const gdb_byte *buf)
13791 {
13792   const gdb_byte *begin = buf;
13793   gdb_byte byte;
13794
13795   while (1)
13796     {
13797       byte = *buf++;
13798       if ((byte & 128) == 0)
13799         return buf - begin;
13800     }
13801 }
13802
13803 static void
13804 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13805 {
13806   switch (lang)
13807     {
13808     case DW_LANG_C89:
13809     case DW_LANG_C99:
13810     case DW_LANG_C:
13811       cu->language = language_c;
13812       break;
13813     case DW_LANG_C_plus_plus:
13814       cu->language = language_cplus;
13815       break;
13816     case DW_LANG_D:
13817       cu->language = language_d;
13818       break;
13819     case DW_LANG_Fortran77:
13820     case DW_LANG_Fortran90:
13821     case DW_LANG_Fortran95:
13822       cu->language = language_fortran;
13823       break;
13824     case DW_LANG_Go:
13825       cu->language = language_go;
13826       break;
13827     case DW_LANG_Mips_Assembler:
13828       cu->language = language_asm;
13829       break;
13830     case DW_LANG_Java:
13831       cu->language = language_java;
13832       break;
13833     case DW_LANG_Ada83:
13834     case DW_LANG_Ada95:
13835       cu->language = language_ada;
13836       break;
13837     case DW_LANG_Modula2:
13838       cu->language = language_m2;
13839       break;
13840     case DW_LANG_Pascal83:
13841       cu->language = language_pascal;
13842       break;
13843     case DW_LANG_ObjC:
13844       cu->language = language_objc;
13845       break;
13846     case DW_LANG_Cobol74:
13847     case DW_LANG_Cobol85:
13848     default:
13849       cu->language = language_minimal;
13850       break;
13851     }
13852   cu->language_defn = language_def (cu->language);
13853 }
13854
13855 /* Return the named attribute or NULL if not there.  */
13856
13857 static struct attribute *
13858 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13859 {
13860   for (;;)
13861     {
13862       unsigned int i;
13863       struct attribute *spec = NULL;
13864
13865       for (i = 0; i < die->num_attrs; ++i)
13866         {
13867           if (die->attrs[i].name == name)
13868             return &die->attrs[i];
13869           if (die->attrs[i].name == DW_AT_specification
13870               || die->attrs[i].name == DW_AT_abstract_origin)
13871             spec = &die->attrs[i];
13872         }
13873
13874       if (!spec)
13875         break;
13876
13877       die = follow_die_ref (die, spec, &cu);
13878     }
13879
13880   return NULL;
13881 }
13882
13883 /* Return the named attribute or NULL if not there,
13884    but do not follow DW_AT_specification, etc.
13885    This is for use in contexts where we're reading .debug_types dies.
13886    Following DW_AT_specification, DW_AT_abstract_origin will take us
13887    back up the chain, and we want to go down.  */
13888
13889 static struct attribute *
13890 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13891 {
13892   unsigned int i;
13893
13894   for (i = 0; i < die->num_attrs; ++i)
13895     if (die->attrs[i].name == name)
13896       return &die->attrs[i];
13897
13898   return NULL;
13899 }
13900
13901 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13902    and holds a non-zero value.  This function should only be used for
13903    DW_FORM_flag or DW_FORM_flag_present attributes.  */
13904
13905 static int
13906 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13907 {
13908   struct attribute *attr = dwarf2_attr (die, name, cu);
13909
13910   return (attr && DW_UNSND (attr));
13911 }
13912
13913 static int
13914 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13915 {
13916   /* A DIE is a declaration if it has a DW_AT_declaration attribute
13917      which value is non-zero.  However, we have to be careful with
13918      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13919      (via dwarf2_flag_true_p) follows this attribute.  So we may
13920      end up accidently finding a declaration attribute that belongs
13921      to a different DIE referenced by the specification attribute,
13922      even though the given DIE does not have a declaration attribute.  */
13923   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13924           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13925 }
13926
13927 /* Return the die giving the specification for DIE, if there is
13928    one.  *SPEC_CU is the CU containing DIE on input, and the CU
13929    containing the return value on output.  If there is no
13930    specification, but there is an abstract origin, that is
13931    returned.  */
13932
13933 static struct die_info *
13934 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13935 {
13936   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13937                                              *spec_cu);
13938
13939   if (spec_attr == NULL)
13940     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13941
13942   if (spec_attr == NULL)
13943     return NULL;
13944   else
13945     return follow_die_ref (die, spec_attr, spec_cu);
13946 }
13947
13948 /* Free the line_header structure *LH, and any arrays and strings it
13949    refers to.
13950    NOTE: This is also used as a "cleanup" function.  */
13951
13952 static void
13953 free_line_header (struct line_header *lh)
13954 {
13955   if (lh->standard_opcode_lengths)
13956     xfree (lh->standard_opcode_lengths);
13957
13958   /* Remember that all the lh->file_names[i].name pointers are
13959      pointers into debug_line_buffer, and don't need to be freed.  */
13960   if (lh->file_names)
13961     xfree (lh->file_names);
13962
13963   /* Similarly for the include directory names.  */
13964   if (lh->include_dirs)
13965     xfree (lh->include_dirs);
13966
13967   xfree (lh);
13968 }
13969
13970 /* Add an entry to LH's include directory table.  */
13971
13972 static void
13973 add_include_dir (struct line_header *lh, char *include_dir)
13974 {
13975   /* Grow the array if necessary.  */
13976   if (lh->include_dirs_size == 0)
13977     {
13978       lh->include_dirs_size = 1; /* for testing */
13979       lh->include_dirs = xmalloc (lh->include_dirs_size
13980                                   * sizeof (*lh->include_dirs));
13981     }
13982   else if (lh->num_include_dirs >= lh->include_dirs_size)
13983     {
13984       lh->include_dirs_size *= 2;
13985       lh->include_dirs = xrealloc (lh->include_dirs,
13986                                    (lh->include_dirs_size
13987                                     * sizeof (*lh->include_dirs)));
13988     }
13989
13990   lh->include_dirs[lh->num_include_dirs++] = include_dir;
13991 }
13992
13993 /* Add an entry to LH's file name table.  */
13994
13995 static void
13996 add_file_name (struct line_header *lh,
13997                char *name,
13998                unsigned int dir_index,
13999                unsigned int mod_time,
14000                unsigned int length)
14001 {
14002   struct file_entry *fe;
14003
14004   /* Grow the array if necessary.  */
14005   if (lh->file_names_size == 0)
14006     {
14007       lh->file_names_size = 1; /* for testing */
14008       lh->file_names = xmalloc (lh->file_names_size
14009                                 * sizeof (*lh->file_names));
14010     }
14011   else if (lh->num_file_names >= lh->file_names_size)
14012     {
14013       lh->file_names_size *= 2;
14014       lh->file_names = xrealloc (lh->file_names,
14015                                  (lh->file_names_size
14016                                   * sizeof (*lh->file_names)));
14017     }
14018
14019   fe = &lh->file_names[lh->num_file_names++];
14020   fe->name = name;
14021   fe->dir_index = dir_index;
14022   fe->mod_time = mod_time;
14023   fe->length = length;
14024   fe->included_p = 0;
14025   fe->symtab = NULL;
14026 }
14027
14028 /* A convenience function to find the proper .debug_line section for a
14029    CU.  */
14030
14031 static struct dwarf2_section_info *
14032 get_debug_line_section (struct dwarf2_cu *cu)
14033 {
14034   struct dwarf2_section_info *section;
14035
14036   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14037      DWO file.  */
14038   if (cu->dwo_unit && cu->per_cu->is_debug_types)
14039     section = &cu->dwo_unit->dwo_file->sections.line;
14040   else if (cu->per_cu->is_dwz)
14041     {
14042       struct dwz_file *dwz = dwarf2_get_dwz_file ();
14043
14044       section = &dwz->line;
14045     }
14046   else
14047     section = &dwarf2_per_objfile->line;
14048
14049   return section;
14050 }
14051
14052 /* Read the statement program header starting at OFFSET in
14053    .debug_line, or .debug_line.dwo.  Return a pointer
14054    to a struct line_header, allocated using xmalloc.
14055
14056    NOTE: the strings in the include directory and file name tables of
14057    the returned object point into the dwarf line section buffer,
14058    and must not be freed.  */
14059
14060 static struct line_header *
14061 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14062 {
14063   struct cleanup *back_to;
14064   struct line_header *lh;
14065   gdb_byte *line_ptr;
14066   unsigned int bytes_read, offset_size;
14067   int i;
14068   char *cur_dir, *cur_file;
14069   struct dwarf2_section_info *section;
14070   bfd *abfd;
14071
14072   section = get_debug_line_section (cu);
14073   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14074   if (section->buffer == NULL)
14075     {
14076       if (cu->dwo_unit && cu->per_cu->is_debug_types)
14077         complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14078       else
14079         complaint (&symfile_complaints, _("missing .debug_line section"));
14080       return 0;
14081     }
14082
14083   /* We can't do this until we know the section is non-empty.
14084      Only then do we know we have such a section.  */
14085   abfd = section->asection->owner;
14086
14087   /* Make sure that at least there's room for the total_length field.
14088      That could be 12 bytes long, but we're just going to fudge that.  */
14089   if (offset + 4 >= section->size)
14090     {
14091       dwarf2_statement_list_fits_in_line_number_section_complaint ();
14092       return 0;
14093     }
14094
14095   lh = xmalloc (sizeof (*lh));
14096   memset (lh, 0, sizeof (*lh));
14097   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14098                           (void *) lh);
14099
14100   line_ptr = section->buffer + offset;
14101
14102   /* Read in the header.  */
14103   lh->total_length =
14104     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14105                                             &bytes_read, &offset_size);
14106   line_ptr += bytes_read;
14107   if (line_ptr + lh->total_length > (section->buffer + section->size))
14108     {
14109       dwarf2_statement_list_fits_in_line_number_section_complaint ();
14110       return 0;
14111     }
14112   lh->statement_program_end = line_ptr + lh->total_length;
14113   lh->version = read_2_bytes (abfd, line_ptr);
14114   line_ptr += 2;
14115   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14116   line_ptr += offset_size;
14117   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14118   line_ptr += 1;
14119   if (lh->version >= 4)
14120     {
14121       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14122       line_ptr += 1;
14123     }
14124   else
14125     lh->maximum_ops_per_instruction = 1;
14126
14127   if (lh->maximum_ops_per_instruction == 0)
14128     {
14129       lh->maximum_ops_per_instruction = 1;
14130       complaint (&symfile_complaints,
14131                  _("invalid maximum_ops_per_instruction "
14132                    "in `.debug_line' section"));
14133     }
14134
14135   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14136   line_ptr += 1;
14137   lh->line_base = read_1_signed_byte (abfd, line_ptr);
14138   line_ptr += 1;
14139   lh->line_range = read_1_byte (abfd, line_ptr);
14140   line_ptr += 1;
14141   lh->opcode_base = read_1_byte (abfd, line_ptr);
14142   line_ptr += 1;
14143   lh->standard_opcode_lengths
14144     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14145
14146   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
14147   for (i = 1; i < lh->opcode_base; ++i)
14148     {
14149       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14150       line_ptr += 1;
14151     }
14152
14153   /* Read directory table.  */
14154   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14155     {
14156       line_ptr += bytes_read;
14157       add_include_dir (lh, cur_dir);
14158     }
14159   line_ptr += bytes_read;
14160
14161   /* Read file name table.  */
14162   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14163     {
14164       unsigned int dir_index, mod_time, length;
14165
14166       line_ptr += bytes_read;
14167       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14168       line_ptr += bytes_read;
14169       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14170       line_ptr += bytes_read;
14171       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14172       line_ptr += bytes_read;
14173
14174       add_file_name (lh, cur_file, dir_index, mod_time, length);
14175     }
14176   line_ptr += bytes_read;
14177   lh->statement_program_start = line_ptr;
14178
14179   if (line_ptr > (section->buffer + section->size))
14180     complaint (&symfile_complaints,
14181                _("line number info header doesn't "
14182                  "fit in `.debug_line' section"));
14183
14184   discard_cleanups (back_to);
14185   return lh;
14186 }
14187
14188 /* Subroutine of dwarf_decode_lines to simplify it.
14189    Return the file name of the psymtab for included file FILE_INDEX
14190    in line header LH of PST.
14191    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14192    If space for the result is malloc'd, it will be freed by a cleanup.
14193    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
14194
14195 static char *
14196 psymtab_include_file_name (const struct line_header *lh, int file_index,
14197                            const struct partial_symtab *pst,
14198                            const char *comp_dir)
14199 {
14200   const struct file_entry fe = lh->file_names [file_index];
14201   char *include_name = fe.name;
14202   char *include_name_to_compare = include_name;
14203   char *dir_name = NULL;
14204   const char *pst_filename;
14205   char *copied_name = NULL;
14206   int file_is_pst;
14207
14208   if (fe.dir_index)
14209     dir_name = lh->include_dirs[fe.dir_index - 1];
14210
14211   if (!IS_ABSOLUTE_PATH (include_name)
14212       && (dir_name != NULL || comp_dir != NULL))
14213     {
14214       /* Avoid creating a duplicate psymtab for PST.
14215          We do this by comparing INCLUDE_NAME and PST_FILENAME.
14216          Before we do the comparison, however, we need to account
14217          for DIR_NAME and COMP_DIR.
14218          First prepend dir_name (if non-NULL).  If we still don't
14219          have an absolute path prepend comp_dir (if non-NULL).
14220          However, the directory we record in the include-file's
14221          psymtab does not contain COMP_DIR (to match the
14222          corresponding symtab(s)).
14223
14224          Example:
14225
14226          bash$ cd /tmp
14227          bash$ gcc -g ./hello.c
14228          include_name = "hello.c"
14229          dir_name = "."
14230          DW_AT_comp_dir = comp_dir = "/tmp"
14231          DW_AT_name = "./hello.c"  */
14232
14233       if (dir_name != NULL)
14234         {
14235           include_name = concat (dir_name, SLASH_STRING,
14236                                  include_name, (char *)NULL);
14237           include_name_to_compare = include_name;
14238           make_cleanup (xfree, include_name);
14239         }
14240       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14241         {
14242           include_name_to_compare = concat (comp_dir, SLASH_STRING,
14243                                             include_name, (char *)NULL);
14244         }
14245     }
14246
14247   pst_filename = pst->filename;
14248   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14249     {
14250       copied_name = concat (pst->dirname, SLASH_STRING,
14251                             pst_filename, (char *)NULL);
14252       pst_filename = copied_name;
14253     }
14254
14255   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14256
14257   if (include_name_to_compare != include_name)
14258     xfree (include_name_to_compare);
14259   if (copied_name != NULL)
14260     xfree (copied_name);
14261
14262   if (file_is_pst)
14263     return NULL;
14264   return include_name;
14265 }
14266
14267 /* Ignore this record_line request.  */
14268
14269 static void
14270 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14271 {
14272   return;
14273 }
14274
14275 /* Subroutine of dwarf_decode_lines to simplify it.
14276    Process the line number information in LH.  */
14277
14278 static void
14279 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14280                       struct dwarf2_cu *cu, struct partial_symtab *pst)
14281 {
14282   gdb_byte *line_ptr, *extended_end;
14283   gdb_byte *line_end;
14284   unsigned int bytes_read, extended_len;
14285   unsigned char op_code, extended_op, adj_opcode;
14286   CORE_ADDR baseaddr;
14287   struct objfile *objfile = cu->objfile;
14288   bfd *abfd = objfile->obfd;
14289   struct gdbarch *gdbarch = get_objfile_arch (objfile);
14290   const int decode_for_pst_p = (pst != NULL);
14291   struct subfile *last_subfile = NULL;
14292   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14293     = record_line;
14294
14295   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14296
14297   line_ptr = lh->statement_program_start;
14298   line_end = lh->statement_program_end;
14299
14300   /* Read the statement sequences until there's nothing left.  */
14301   while (line_ptr < line_end)
14302     {
14303       /* state machine registers  */
14304       CORE_ADDR address = 0;
14305       unsigned int file = 1;
14306       unsigned int line = 1;
14307       unsigned int column = 0;
14308       int is_stmt = lh->default_is_stmt;
14309       int basic_block = 0;
14310       int end_sequence = 0;
14311       CORE_ADDR addr;
14312       unsigned char op_index = 0;
14313
14314       if (!decode_for_pst_p && lh->num_file_names >= file)
14315         {
14316           /* Start a subfile for the current file of the state machine.  */
14317           /* lh->include_dirs and lh->file_names are 0-based, but the
14318              directory and file name numbers in the statement program
14319              are 1-based.  */
14320           struct file_entry *fe = &lh->file_names[file - 1];
14321           char *dir = NULL;
14322
14323           if (fe->dir_index)
14324             dir = lh->include_dirs[fe->dir_index - 1];
14325
14326           dwarf2_start_subfile (fe->name, dir, comp_dir);
14327         }
14328
14329       /* Decode the table.  */
14330       while (!end_sequence)
14331         {
14332           op_code = read_1_byte (abfd, line_ptr);
14333           line_ptr += 1;
14334           if (line_ptr > line_end)
14335             {
14336               dwarf2_debug_line_missing_end_sequence_complaint ();
14337               break;
14338             }
14339
14340           if (op_code >= lh->opcode_base)
14341             {
14342               /* Special operand.  */
14343               adj_opcode = op_code - lh->opcode_base;
14344               address += (((op_index + (adj_opcode / lh->line_range))
14345                            / lh->maximum_ops_per_instruction)
14346                           * lh->minimum_instruction_length);
14347               op_index = ((op_index + (adj_opcode / lh->line_range))
14348                           % lh->maximum_ops_per_instruction);
14349               line += lh->line_base + (adj_opcode % lh->line_range);
14350               if (lh->num_file_names < file || file == 0)
14351                 dwarf2_debug_line_missing_file_complaint ();
14352               /* For now we ignore lines not starting on an
14353                  instruction boundary.  */
14354               else if (op_index == 0)
14355                 {
14356                   lh->file_names[file - 1].included_p = 1;
14357                   if (!decode_for_pst_p && is_stmt)
14358                     {
14359                       if (last_subfile != current_subfile)
14360                         {
14361                           addr = gdbarch_addr_bits_remove (gdbarch, address);
14362                           if (last_subfile)
14363                             (*p_record_line) (last_subfile, 0, addr);
14364                           last_subfile = current_subfile;
14365                         }
14366                       /* Append row to matrix using current values.  */
14367                       addr = gdbarch_addr_bits_remove (gdbarch, address);
14368                       (*p_record_line) (current_subfile, line, addr);
14369                     }
14370                 }
14371               basic_block = 0;
14372             }
14373           else switch (op_code)
14374             {
14375             case DW_LNS_extended_op:
14376               extended_len = read_unsigned_leb128 (abfd, line_ptr,
14377                                                    &bytes_read);
14378               line_ptr += bytes_read;
14379               extended_end = line_ptr + extended_len;
14380               extended_op = read_1_byte (abfd, line_ptr);
14381               line_ptr += 1;
14382               switch (extended_op)
14383                 {
14384                 case DW_LNE_end_sequence:
14385                   p_record_line = record_line;
14386                   end_sequence = 1;
14387                   break;
14388                 case DW_LNE_set_address:
14389                   address = read_address (abfd, line_ptr, cu, &bytes_read);
14390
14391                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14392                     {
14393                       /* This line table is for a function which has been
14394                          GCd by the linker.  Ignore it.  PR gdb/12528 */
14395
14396                       long line_offset
14397                         = line_ptr - get_debug_line_section (cu)->buffer;
14398
14399                       complaint (&symfile_complaints,
14400                                  _(".debug_line address at offset 0x%lx is 0 "
14401                                    "[in module %s]"),
14402                                  line_offset, objfile->name);
14403                       p_record_line = noop_record_line;
14404                     }
14405
14406                   op_index = 0;
14407                   line_ptr += bytes_read;
14408                   address += baseaddr;
14409                   break;
14410                 case DW_LNE_define_file:
14411                   {
14412                     char *cur_file;
14413                     unsigned int dir_index, mod_time, length;
14414
14415                     cur_file = read_direct_string (abfd, line_ptr,
14416                                                    &bytes_read);
14417                     line_ptr += bytes_read;
14418                     dir_index =
14419                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14420                     line_ptr += bytes_read;
14421                     mod_time =
14422                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14423                     line_ptr += bytes_read;
14424                     length =
14425                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14426                     line_ptr += bytes_read;
14427                     add_file_name (lh, cur_file, dir_index, mod_time, length);
14428                   }
14429                   break;
14430                 case DW_LNE_set_discriminator:
14431                   /* The discriminator is not interesting to the debugger;
14432                      just ignore it.  */
14433                   line_ptr = extended_end;
14434                   break;
14435                 default:
14436                   complaint (&symfile_complaints,
14437                              _("mangled .debug_line section"));
14438                   return;
14439                 }
14440               /* Make sure that we parsed the extended op correctly.  If e.g.
14441                  we expected a different address size than the producer used,
14442                  we may have read the wrong number of bytes.  */
14443               if (line_ptr != extended_end)
14444                 {
14445                   complaint (&symfile_complaints,
14446                              _("mangled .debug_line section"));
14447                   return;
14448                 }
14449               break;
14450             case DW_LNS_copy:
14451               if (lh->num_file_names < file || file == 0)
14452                 dwarf2_debug_line_missing_file_complaint ();
14453               else
14454                 {
14455                   lh->file_names[file - 1].included_p = 1;
14456                   if (!decode_for_pst_p && is_stmt)
14457                     {
14458                       if (last_subfile != current_subfile)
14459                         {
14460                           addr = gdbarch_addr_bits_remove (gdbarch, address);
14461                           if (last_subfile)
14462                             (*p_record_line) (last_subfile, 0, addr);
14463                           last_subfile = current_subfile;
14464                         }
14465                       addr = gdbarch_addr_bits_remove (gdbarch, address);
14466                       (*p_record_line) (current_subfile, line, addr);
14467                     }
14468                 }
14469               basic_block = 0;
14470               break;
14471             case DW_LNS_advance_pc:
14472               {
14473                 CORE_ADDR adjust
14474                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14475
14476                 address += (((op_index + adjust)
14477                              / lh->maximum_ops_per_instruction)
14478                             * lh->minimum_instruction_length);
14479                 op_index = ((op_index + adjust)
14480                             % lh->maximum_ops_per_instruction);
14481                 line_ptr += bytes_read;
14482               }
14483               break;
14484             case DW_LNS_advance_line:
14485               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14486               line_ptr += bytes_read;
14487               break;
14488             case DW_LNS_set_file:
14489               {
14490                 /* The arrays lh->include_dirs and lh->file_names are
14491                    0-based, but the directory and file name numbers in
14492                    the statement program are 1-based.  */
14493                 struct file_entry *fe;
14494                 char *dir = NULL;
14495
14496                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14497                 line_ptr += bytes_read;
14498                 if (lh->num_file_names < file || file == 0)
14499                   dwarf2_debug_line_missing_file_complaint ();
14500                 else
14501                   {
14502                     fe = &lh->file_names[file - 1];
14503                     if (fe->dir_index)
14504                       dir = lh->include_dirs[fe->dir_index - 1];
14505                     if (!decode_for_pst_p)
14506                       {
14507                         last_subfile = current_subfile;
14508                         dwarf2_start_subfile (fe->name, dir, comp_dir);
14509                       }
14510                   }
14511               }
14512               break;
14513             case DW_LNS_set_column:
14514               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14515               line_ptr += bytes_read;
14516               break;
14517             case DW_LNS_negate_stmt:
14518               is_stmt = (!is_stmt);
14519               break;
14520             case DW_LNS_set_basic_block:
14521               basic_block = 1;
14522               break;
14523             /* Add to the address register of the state machine the
14524                address increment value corresponding to special opcode
14525                255.  I.e., this value is scaled by the minimum
14526                instruction length since special opcode 255 would have
14527                scaled the increment.  */
14528             case DW_LNS_const_add_pc:
14529               {
14530                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14531
14532                 address += (((op_index + adjust)
14533                              / lh->maximum_ops_per_instruction)
14534                             * lh->minimum_instruction_length);
14535                 op_index = ((op_index + adjust)
14536                             % lh->maximum_ops_per_instruction);
14537               }
14538               break;
14539             case DW_LNS_fixed_advance_pc:
14540               address += read_2_bytes (abfd, line_ptr);
14541               op_index = 0;
14542               line_ptr += 2;
14543               break;
14544             default:
14545               {
14546                 /* Unknown standard opcode, ignore it.  */
14547                 int i;
14548
14549                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14550                   {
14551                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14552                     line_ptr += bytes_read;
14553                   }
14554               }
14555             }
14556         }
14557       if (lh->num_file_names < file || file == 0)
14558         dwarf2_debug_line_missing_file_complaint ();
14559       else
14560         {
14561           lh->file_names[file - 1].included_p = 1;
14562           if (!decode_for_pst_p)
14563             {
14564               addr = gdbarch_addr_bits_remove (gdbarch, address);
14565               (*p_record_line) (current_subfile, 0, addr);
14566             }
14567         }
14568     }
14569 }
14570
14571 /* Decode the Line Number Program (LNP) for the given line_header
14572    structure and CU.  The actual information extracted and the type
14573    of structures created from the LNP depends on the value of PST.
14574
14575    1. If PST is NULL, then this procedure uses the data from the program
14576       to create all necessary symbol tables, and their linetables.
14577
14578    2. If PST is not NULL, this procedure reads the program to determine
14579       the list of files included by the unit represented by PST, and
14580       builds all the associated partial symbol tables.
14581
14582    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14583    It is used for relative paths in the line table.
14584    NOTE: When processing partial symtabs (pst != NULL),
14585    comp_dir == pst->dirname.
14586
14587    NOTE: It is important that psymtabs have the same file name (via strcmp)
14588    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
14589    symtab we don't use it in the name of the psymtabs we create.
14590    E.g. expand_line_sal requires this when finding psymtabs to expand.
14591    A good testcase for this is mb-inline.exp.  */
14592
14593 static void
14594 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14595                     struct dwarf2_cu *cu, struct partial_symtab *pst,
14596                     int want_line_info)
14597 {
14598   struct objfile *objfile = cu->objfile;
14599   const int decode_for_pst_p = (pst != NULL);
14600   struct subfile *first_subfile = current_subfile;
14601
14602   if (want_line_info)
14603     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14604
14605   if (decode_for_pst_p)
14606     {
14607       int file_index;
14608
14609       /* Now that we're done scanning the Line Header Program, we can
14610          create the psymtab of each included file.  */
14611       for (file_index = 0; file_index < lh->num_file_names; file_index++)
14612         if (lh->file_names[file_index].included_p == 1)
14613           {
14614             char *include_name =
14615               psymtab_include_file_name (lh, file_index, pst, comp_dir);
14616             if (include_name != NULL)
14617               dwarf2_create_include_psymtab (include_name, pst, objfile);
14618           }
14619     }
14620   else
14621     {
14622       /* Make sure a symtab is created for every file, even files
14623          which contain only variables (i.e. no code with associated
14624          line numbers).  */
14625       int i;
14626
14627       for (i = 0; i < lh->num_file_names; i++)
14628         {
14629           char *dir = NULL;
14630           struct file_entry *fe;
14631
14632           fe = &lh->file_names[i];
14633           if (fe->dir_index)
14634             dir = lh->include_dirs[fe->dir_index - 1];
14635           dwarf2_start_subfile (fe->name, dir, comp_dir);
14636
14637           /* Skip the main file; we don't need it, and it must be
14638              allocated last, so that it will show up before the
14639              non-primary symtabs in the objfile's symtab list.  */
14640           if (current_subfile == first_subfile)
14641             continue;
14642
14643           if (current_subfile->symtab == NULL)
14644             current_subfile->symtab = allocate_symtab (current_subfile->name,
14645                                                        objfile);
14646           fe->symtab = current_subfile->symtab;
14647         }
14648     }
14649 }
14650
14651 /* Start a subfile for DWARF.  FILENAME is the name of the file and
14652    DIRNAME the name of the source directory which contains FILENAME
14653    or NULL if not known.  COMP_DIR is the compilation directory for the
14654    linetable's compilation unit or NULL if not known.
14655    This routine tries to keep line numbers from identical absolute and
14656    relative file names in a common subfile.
14657
14658    Using the `list' example from the GDB testsuite, which resides in
14659    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14660    of /srcdir/list0.c yields the following debugging information for list0.c:
14661
14662    DW_AT_name:          /srcdir/list0.c
14663    DW_AT_comp_dir:              /compdir
14664    files.files[0].name: list0.h
14665    files.files[0].dir:  /srcdir
14666    files.files[1].name: list0.c
14667    files.files[1].dir:  /srcdir
14668
14669    The line number information for list0.c has to end up in a single
14670    subfile, so that `break /srcdir/list0.c:1' works as expected.
14671    start_subfile will ensure that this happens provided that we pass the
14672    concatenation of files.files[1].dir and files.files[1].name as the
14673    subfile's name.  */
14674
14675 static void
14676 dwarf2_start_subfile (char *filename, const char *dirname,
14677                       const char *comp_dir)
14678 {
14679   char *fullname;
14680
14681   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14682      `start_symtab' will always pass the contents of DW_AT_comp_dir as
14683      second argument to start_subfile.  To be consistent, we do the
14684      same here.  In order not to lose the line information directory,
14685      we concatenate it to the filename when it makes sense.
14686      Note that the Dwarf3 standard says (speaking of filenames in line
14687      information): ``The directory index is ignored for file names
14688      that represent full path names''.  Thus ignoring dirname in the
14689      `else' branch below isn't an issue.  */
14690
14691   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14692     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14693   else
14694     fullname = filename;
14695
14696   start_subfile (fullname, comp_dir);
14697
14698   if (fullname != filename)
14699     xfree (fullname);
14700 }
14701
14702 /* Start a symtab for DWARF.
14703    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */
14704
14705 static void
14706 dwarf2_start_symtab (struct dwarf2_cu *cu,
14707                      char *name, char *comp_dir, CORE_ADDR low_pc)
14708 {
14709   start_symtab (name, comp_dir, low_pc);
14710   record_debugformat ("DWARF 2");
14711   record_producer (cu->producer);
14712
14713   /* We assume that we're processing GCC output.  */
14714   processing_gcc_compilation = 2;
14715
14716   processing_has_namespace_info = 0;
14717 }
14718
14719 static void
14720 var_decode_location (struct attribute *attr, struct symbol *sym,
14721                      struct dwarf2_cu *cu)
14722 {
14723   struct objfile *objfile = cu->objfile;
14724   struct comp_unit_head *cu_header = &cu->header;
14725
14726   /* NOTE drow/2003-01-30: There used to be a comment and some special
14727      code here to turn a symbol with DW_AT_external and a
14728      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
14729      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14730      with some versions of binutils) where shared libraries could have
14731      relocations against symbols in their debug information - the
14732      minimal symbol would have the right address, but the debug info
14733      would not.  It's no longer necessary, because we will explicitly
14734      apply relocations when we read in the debug information now.  */
14735
14736   /* A DW_AT_location attribute with no contents indicates that a
14737      variable has been optimized away.  */
14738   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14739     {
14740       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14741       return;
14742     }
14743
14744   /* Handle one degenerate form of location expression specially, to
14745      preserve GDB's previous behavior when section offsets are
14746      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14747      then mark this symbol as LOC_STATIC.  */
14748
14749   if (attr_form_is_block (attr)
14750       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14751            && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14752           || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14753               && (DW_BLOCK (attr)->size
14754                   == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14755     {
14756       unsigned int dummy;
14757
14758       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14759         SYMBOL_VALUE_ADDRESS (sym) =
14760           read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14761       else
14762         SYMBOL_VALUE_ADDRESS (sym) =
14763           read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14764       SYMBOL_CLASS (sym) = LOC_STATIC;
14765       fixup_symbol_section (sym, objfile);
14766       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14767                                               SYMBOL_SECTION (sym));
14768       return;
14769     }
14770
14771   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14772      expression evaluator, and use LOC_COMPUTED only when necessary
14773      (i.e. when the value of a register or memory location is
14774      referenced, or a thread-local block, etc.).  Then again, it might
14775      not be worthwhile.  I'm assuming that it isn't unless performance
14776      or memory numbers show me otherwise.  */
14777
14778   dwarf2_symbol_mark_computed (attr, sym, cu);
14779   SYMBOL_CLASS (sym) = LOC_COMPUTED;
14780
14781   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14782     cu->has_loclist = 1;
14783 }
14784
14785 /* Given a pointer to a DWARF information entry, figure out if we need
14786    to make a symbol table entry for it, and if so, create a new entry
14787    and return a pointer to it.
14788    If TYPE is NULL, determine symbol type from the die, otherwise
14789    used the passed type.
14790    If SPACE is not NULL, use it to hold the new symbol.  If it is
14791    NULL, allocate a new symbol on the objfile's obstack.  */
14792
14793 static struct symbol *
14794 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14795                  struct symbol *space)
14796 {
14797   struct objfile *objfile = cu->objfile;
14798   struct symbol *sym = NULL;
14799   char *name;
14800   struct attribute *attr = NULL;
14801   struct attribute *attr2 = NULL;
14802   CORE_ADDR baseaddr;
14803   struct pending **list_to_add = NULL;
14804
14805   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14806
14807   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14808
14809   name = dwarf2_name (die, cu);
14810   if (name)
14811     {
14812       const char *linkagename;
14813       int suppress_add = 0;
14814
14815       if (space)
14816         sym = space;
14817       else
14818         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14819       OBJSTAT (objfile, n_syms++);
14820
14821       /* Cache this symbol's name and the name's demangled form (if any).  */
14822       SYMBOL_SET_LANGUAGE (sym, cu->language);
14823       linkagename = dwarf2_physname (name, die, cu);
14824       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14825
14826       /* Fortran does not have mangling standard and the mangling does differ
14827          between gfortran, iFort etc.  */
14828       if (cu->language == language_fortran
14829           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14830         symbol_set_demangled_name (&(sym->ginfo),
14831                                    (char *) dwarf2_full_name (name, die, cu),
14832                                    NULL);
14833
14834       /* Default assumptions.
14835          Use the passed type or decode it from the die.  */
14836       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14837       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14838       if (type != NULL)
14839         SYMBOL_TYPE (sym) = type;
14840       else
14841         SYMBOL_TYPE (sym) = die_type (die, cu);
14842       attr = dwarf2_attr (die,
14843                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14844                           cu);
14845       if (attr)
14846         {
14847           SYMBOL_LINE (sym) = DW_UNSND (attr);
14848         }
14849
14850       attr = dwarf2_attr (die,
14851                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14852                           cu);
14853       if (attr)
14854         {
14855           int file_index = DW_UNSND (attr);
14856
14857           if (cu->line_header == NULL
14858               || file_index > cu->line_header->num_file_names)
14859             complaint (&symfile_complaints,
14860                        _("file index out of range"));
14861           else if (file_index > 0)
14862             {
14863               struct file_entry *fe;
14864
14865               fe = &cu->line_header->file_names[file_index - 1];
14866               SYMBOL_SYMTAB (sym) = fe->symtab;
14867             }
14868         }
14869
14870       switch (die->tag)
14871         {
14872         case DW_TAG_label:
14873           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14874           if (attr)
14875             {
14876               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14877             }
14878           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14879           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14880           SYMBOL_CLASS (sym) = LOC_LABEL;
14881           add_symbol_to_list (sym, cu->list_in_scope);
14882           break;
14883         case DW_TAG_subprogram:
14884           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14885              finish_block.  */
14886           SYMBOL_CLASS (sym) = LOC_BLOCK;
14887           attr2 = dwarf2_attr (die, DW_AT_external, cu);
14888           if ((attr2 && (DW_UNSND (attr2) != 0))
14889               || cu->language == language_ada)
14890             {
14891               /* Subprograms marked external are stored as a global symbol.
14892                  Ada subprograms, whether marked external or not, are always
14893                  stored as a global symbol, because we want to be able to
14894                  access them globally.  For instance, we want to be able
14895                  to break on a nested subprogram without having to
14896                  specify the context.  */
14897               list_to_add = &global_symbols;
14898             }
14899           else
14900             {
14901               list_to_add = cu->list_in_scope;
14902             }
14903           break;
14904         case DW_TAG_inlined_subroutine:
14905           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14906              finish_block.  */
14907           SYMBOL_CLASS (sym) = LOC_BLOCK;
14908           SYMBOL_INLINED (sym) = 1;
14909           list_to_add = cu->list_in_scope;
14910           break;
14911         case DW_TAG_template_value_param:
14912           suppress_add = 1;
14913           /* Fall through.  */
14914         case DW_TAG_constant:
14915         case DW_TAG_variable:
14916         case DW_TAG_member:
14917           /* Compilation with minimal debug info may result in
14918              variables with missing type entries.  Change the
14919              misleading `void' type to something sensible.  */
14920           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14921             SYMBOL_TYPE (sym)
14922               = objfile_type (objfile)->nodebug_data_symbol;
14923
14924           attr = dwarf2_attr (die, DW_AT_const_value, cu);
14925           /* In the case of DW_TAG_member, we should only be called for
14926              static const members.  */
14927           if (die->tag == DW_TAG_member)
14928             {
14929               /* dwarf2_add_field uses die_is_declaration,
14930                  so we do the same.  */
14931               gdb_assert (die_is_declaration (die, cu));
14932               gdb_assert (attr);
14933             }
14934           if (attr)
14935             {
14936               dwarf2_const_value (attr, sym, cu);
14937               attr2 = dwarf2_attr (die, DW_AT_external, cu);
14938               if (!suppress_add)
14939                 {
14940                   if (attr2 && (DW_UNSND (attr2) != 0))
14941                     list_to_add = &global_symbols;
14942                   else
14943                     list_to_add = cu->list_in_scope;
14944                 }
14945               break;
14946             }
14947           attr = dwarf2_attr (die, DW_AT_location, cu);
14948           if (attr)
14949             {
14950               var_decode_location (attr, sym, cu);
14951               attr2 = dwarf2_attr (die, DW_AT_external, cu);
14952               if (SYMBOL_CLASS (sym) == LOC_STATIC
14953                   && SYMBOL_VALUE_ADDRESS (sym) == 0
14954                   && !dwarf2_per_objfile->has_section_at_zero)
14955                 {
14956                   /* When a static variable is eliminated by the linker,
14957                      the corresponding debug information is not stripped
14958                      out, but the variable address is set to null;
14959                      do not add such variables into symbol table.  */
14960                 }
14961               else if (attr2 && (DW_UNSND (attr2) != 0))
14962                 {
14963                   /* Workaround gfortran PR debug/40040 - it uses
14964                      DW_AT_location for variables in -fPIC libraries which may
14965                      get overriden by other libraries/executable and get
14966                      a different address.  Resolve it by the minimal symbol
14967                      which may come from inferior's executable using copy
14968                      relocation.  Make this workaround only for gfortran as for
14969                      other compilers GDB cannot guess the minimal symbol
14970                      Fortran mangling kind.  */
14971                   if (cu->language == language_fortran && die->parent
14972                       && die->parent->tag == DW_TAG_module
14973                       && cu->producer
14974                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14975                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14976
14977                   /* A variable with DW_AT_external is never static,
14978                      but it may be block-scoped.  */
14979                   list_to_add = (cu->list_in_scope == &file_symbols
14980                                  ? &global_symbols : cu->list_in_scope);
14981                 }
14982               else
14983                 list_to_add = cu->list_in_scope;
14984             }
14985           else
14986             {
14987               /* We do not know the address of this symbol.
14988                  If it is an external symbol and we have type information
14989                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
14990                  The address of the variable will then be determined from
14991                  the minimal symbol table whenever the variable is
14992                  referenced.  */
14993               attr2 = dwarf2_attr (die, DW_AT_external, cu);
14994               if (attr2 && (DW_UNSND (attr2) != 0)
14995                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
14996                 {
14997                   /* A variable with DW_AT_external is never static, but it
14998                      may be block-scoped.  */
14999                   list_to_add = (cu->list_in_scope == &file_symbols
15000                                  ? &global_symbols : cu->list_in_scope);
15001
15002                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15003                 }
15004               else if (!die_is_declaration (die, cu))
15005                 {
15006                   /* Use the default LOC_OPTIMIZED_OUT class.  */
15007                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15008                   if (!suppress_add)
15009                     list_to_add = cu->list_in_scope;
15010                 }
15011             }
15012           break;
15013         case DW_TAG_formal_parameter:
15014           /* If we are inside a function, mark this as an argument.  If
15015              not, we might be looking at an argument to an inlined function
15016              when we do not have enough information to show inlined frames;
15017              pretend it's a local variable in that case so that the user can
15018              still see it.  */
15019           if (context_stack_depth > 0
15020               && context_stack[context_stack_depth - 1].name != NULL)
15021             SYMBOL_IS_ARGUMENT (sym) = 1;
15022           attr = dwarf2_attr (die, DW_AT_location, cu);
15023           if (attr)
15024             {
15025               var_decode_location (attr, sym, cu);
15026             }
15027           attr = dwarf2_attr (die, DW_AT_const_value, cu);
15028           if (attr)
15029             {
15030               dwarf2_const_value (attr, sym, cu);
15031             }
15032
15033           list_to_add = cu->list_in_scope;
15034           break;
15035         case DW_TAG_unspecified_parameters:
15036           /* From varargs functions; gdb doesn't seem to have any
15037              interest in this information, so just ignore it for now.
15038              (FIXME?) */
15039           break;
15040         case DW_TAG_template_type_param:
15041           suppress_add = 1;
15042           /* Fall through.  */
15043         case DW_TAG_class_type:
15044         case DW_TAG_interface_type:
15045         case DW_TAG_structure_type:
15046         case DW_TAG_union_type:
15047         case DW_TAG_set_type:
15048         case DW_TAG_enumeration_type:
15049           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15050           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15051
15052           {
15053             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15054                really ever be static objects: otherwise, if you try
15055                to, say, break of a class's method and you're in a file
15056                which doesn't mention that class, it won't work unless
15057                the check for all static symbols in lookup_symbol_aux
15058                saves you.  See the OtherFileClass tests in
15059                gdb.c++/namespace.exp.  */
15060
15061             if (!suppress_add)
15062               {
15063                 list_to_add = (cu->list_in_scope == &file_symbols
15064                                && (cu->language == language_cplus
15065                                    || cu->language == language_java)
15066                                ? &global_symbols : cu->list_in_scope);
15067
15068                 /* The semantics of C++ state that "struct foo {
15069                    ... }" also defines a typedef for "foo".  A Java
15070                    class declaration also defines a typedef for the
15071                    class.  */
15072                 if (cu->language == language_cplus
15073                     || cu->language == language_java
15074                     || cu->language == language_ada)
15075                   {
15076                     /* The symbol's name is already allocated along
15077                        with this objfile, so we don't need to
15078                        duplicate it for the type.  */
15079                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15080                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15081                   }
15082               }
15083           }
15084           break;
15085         case DW_TAG_typedef:
15086           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15087           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15088           list_to_add = cu->list_in_scope;
15089           break;
15090         case DW_TAG_base_type:
15091         case DW_TAG_subrange_type:
15092           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15093           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15094           list_to_add = cu->list_in_scope;
15095           break;
15096         case DW_TAG_enumerator:
15097           attr = dwarf2_attr (die, DW_AT_const_value, cu);
15098           if (attr)
15099             {
15100               dwarf2_const_value (attr, sym, cu);
15101             }
15102           {
15103             /* NOTE: carlton/2003-11-10: See comment above in the
15104                DW_TAG_class_type, etc. block.  */
15105
15106             list_to_add = (cu->list_in_scope == &file_symbols
15107                            && (cu->language == language_cplus
15108                                || cu->language == language_java)
15109                            ? &global_symbols : cu->list_in_scope);
15110           }
15111           break;
15112         case DW_TAG_namespace:
15113           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15114           list_to_add = &global_symbols;
15115           break;
15116         default:
15117           /* Not a tag we recognize.  Hopefully we aren't processing
15118              trash data, but since we must specifically ignore things
15119              we don't recognize, there is nothing else we should do at
15120              this point.  */
15121           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15122                      dwarf_tag_name (die->tag));
15123           break;
15124         }
15125
15126       if (suppress_add)
15127         {
15128           sym->hash_next = objfile->template_symbols;
15129           objfile->template_symbols = sym;
15130           list_to_add = NULL;
15131         }
15132
15133       if (list_to_add != NULL)
15134         add_symbol_to_list (sym, list_to_add);
15135
15136       /* For the benefit of old versions of GCC, check for anonymous
15137          namespaces based on the demangled name.  */
15138       if (!processing_has_namespace_info
15139           && cu->language == language_cplus)
15140         cp_scan_for_anonymous_namespaces (sym, objfile);
15141     }
15142   return (sym);
15143 }
15144
15145 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
15146
15147 static struct symbol *
15148 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15149 {
15150   return new_symbol_full (die, type, cu, NULL);
15151 }
15152
15153 /* Given an attr with a DW_FORM_dataN value in host byte order,
15154    zero-extend it as appropriate for the symbol's type.  The DWARF
15155    standard (v4) is not entirely clear about the meaning of using
15156    DW_FORM_dataN for a constant with a signed type, where the type is
15157    wider than the data.  The conclusion of a discussion on the DWARF
15158    list was that this is unspecified.  We choose to always zero-extend
15159    because that is the interpretation long in use by GCC.  */
15160
15161 static gdb_byte *
15162 dwarf2_const_value_data (struct attribute *attr, struct type *type,
15163                          const char *name, struct obstack *obstack,
15164                          struct dwarf2_cu *cu, LONGEST *value, int bits)
15165 {
15166   struct objfile *objfile = cu->objfile;
15167   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15168                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
15169   LONGEST l = DW_UNSND (attr);
15170
15171   if (bits < sizeof (*value) * 8)
15172     {
15173       l &= ((LONGEST) 1 << bits) - 1;
15174       *value = l;
15175     }
15176   else if (bits == sizeof (*value) * 8)
15177     *value = l;
15178   else
15179     {
15180       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15181       store_unsigned_integer (bytes, bits / 8, byte_order, l);
15182       return bytes;
15183     }
15184
15185   return NULL;
15186 }
15187
15188 /* Read a constant value from an attribute.  Either set *VALUE, or if
15189    the value does not fit in *VALUE, set *BYTES - either already
15190    allocated on the objfile obstack, or newly allocated on OBSTACK,
15191    or, set *BATON, if we translated the constant to a location
15192    expression.  */
15193
15194 static void
15195 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15196                          const char *name, struct obstack *obstack,
15197                          struct dwarf2_cu *cu,
15198                          LONGEST *value, gdb_byte **bytes,
15199                          struct dwarf2_locexpr_baton **baton)
15200 {
15201   struct objfile *objfile = cu->objfile;
15202   struct comp_unit_head *cu_header = &cu->header;
15203   struct dwarf_block *blk;
15204   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15205                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15206
15207   *value = 0;
15208   *bytes = NULL;
15209   *baton = NULL;
15210
15211   switch (attr->form)
15212     {
15213     case DW_FORM_addr:
15214     case DW_FORM_GNU_addr_index:
15215       {
15216         gdb_byte *data;
15217
15218         if (TYPE_LENGTH (type) != cu_header->addr_size)
15219           dwarf2_const_value_length_mismatch_complaint (name,
15220                                                         cu_header->addr_size,
15221                                                         TYPE_LENGTH (type));
15222         /* Symbols of this form are reasonably rare, so we just
15223            piggyback on the existing location code rather than writing
15224            a new implementation of symbol_computed_ops.  */
15225         *baton = obstack_alloc (&objfile->objfile_obstack,
15226                                 sizeof (struct dwarf2_locexpr_baton));
15227         (*baton)->per_cu = cu->per_cu;
15228         gdb_assert ((*baton)->per_cu);
15229
15230         (*baton)->size = 2 + cu_header->addr_size;
15231         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15232         (*baton)->data = data;
15233
15234         data[0] = DW_OP_addr;
15235         store_unsigned_integer (&data[1], cu_header->addr_size,
15236                                 byte_order, DW_ADDR (attr));
15237         data[cu_header->addr_size + 1] = DW_OP_stack_value;
15238       }
15239       break;
15240     case DW_FORM_string:
15241     case DW_FORM_strp:
15242     case DW_FORM_GNU_str_index:
15243     case DW_FORM_GNU_strp_alt:
15244       /* DW_STRING is already allocated on the objfile obstack, point
15245          directly to it.  */
15246       *bytes = (gdb_byte *) DW_STRING (attr);
15247       break;
15248     case DW_FORM_block1:
15249     case DW_FORM_block2:
15250     case DW_FORM_block4:
15251     case DW_FORM_block:
15252     case DW_FORM_exprloc:
15253       blk = DW_BLOCK (attr);
15254       if (TYPE_LENGTH (type) != blk->size)
15255         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15256                                                       TYPE_LENGTH (type));
15257       *bytes = blk->data;
15258       break;
15259
15260       /* The DW_AT_const_value attributes are supposed to carry the
15261          symbol's value "represented as it would be on the target
15262          architecture."  By the time we get here, it's already been
15263          converted to host endianness, so we just need to sign- or
15264          zero-extend it as appropriate.  */
15265     case DW_FORM_data1:
15266       *bytes = dwarf2_const_value_data (attr, type, name,
15267                                         obstack, cu, value, 8);
15268       break;
15269     case DW_FORM_data2:
15270       *bytes = dwarf2_const_value_data (attr, type, name,
15271                                         obstack, cu, value, 16);
15272       break;
15273     case DW_FORM_data4:
15274       *bytes = dwarf2_const_value_data (attr, type, name,
15275                                         obstack, cu, value, 32);
15276       break;
15277     case DW_FORM_data8:
15278       *bytes = dwarf2_const_value_data (attr, type, name,
15279                                         obstack, cu, value, 64);
15280       break;
15281
15282     case DW_FORM_sdata:
15283       *value = DW_SND (attr);
15284       break;
15285
15286     case DW_FORM_udata:
15287       *value = DW_UNSND (attr);
15288       break;
15289
15290     default:
15291       complaint (&symfile_complaints,
15292                  _("unsupported const value attribute form: '%s'"),
15293                  dwarf_form_name (attr->form));
15294       *value = 0;
15295       break;
15296     }
15297 }
15298
15299
15300 /* Copy constant value from an attribute to a symbol.  */
15301
15302 static void
15303 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15304                     struct dwarf2_cu *cu)
15305 {
15306   struct objfile *objfile = cu->objfile;
15307   struct comp_unit_head *cu_header = &cu->header;
15308   LONGEST value;
15309   gdb_byte *bytes;
15310   struct dwarf2_locexpr_baton *baton;
15311
15312   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15313                            SYMBOL_PRINT_NAME (sym),
15314                            &objfile->objfile_obstack, cu,
15315                            &value, &bytes, &baton);
15316
15317   if (baton != NULL)
15318     {
15319       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15320       SYMBOL_LOCATION_BATON (sym) = baton;
15321       SYMBOL_CLASS (sym) = LOC_COMPUTED;
15322     }
15323   else if (bytes != NULL)
15324      {
15325       SYMBOL_VALUE_BYTES (sym) = bytes;
15326       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15327     }
15328   else
15329     {
15330       SYMBOL_VALUE (sym) = value;
15331       SYMBOL_CLASS (sym) = LOC_CONST;
15332     }
15333 }
15334
15335 /* Return the type of the die in question using its DW_AT_type attribute.  */
15336
15337 static struct type *
15338 die_type (struct die_info *die, struct dwarf2_cu *cu)
15339 {
15340   struct attribute *type_attr;
15341
15342   type_attr = dwarf2_attr (die, DW_AT_type, cu);
15343   if (!type_attr)
15344     {
15345       /* A missing DW_AT_type represents a void type.  */
15346       return objfile_type (cu->objfile)->builtin_void;
15347     }
15348
15349   return lookup_die_type (die, type_attr, cu);
15350 }
15351
15352 /* True iff CU's producer generates GNAT Ada auxiliary information
15353    that allows to find parallel types through that information instead
15354    of having to do expensive parallel lookups by type name.  */
15355
15356 static int
15357 need_gnat_info (struct dwarf2_cu *cu)
15358 {
15359   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15360      of GNAT produces this auxiliary information, without any indication
15361      that it is produced.  Part of enhancing the FSF version of GNAT
15362      to produce that information will be to put in place an indicator
15363      that we can use in order to determine whether the descriptive type
15364      info is available or not.  One suggestion that has been made is
15365      to use a new attribute, attached to the CU die.  For now, assume
15366      that the descriptive type info is not available.  */
15367   return 0;
15368 }
15369
15370 /* Return the auxiliary type of the die in question using its
15371    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
15372    attribute is not present.  */
15373
15374 static struct type *
15375 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15376 {
15377   struct attribute *type_attr;
15378
15379   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15380   if (!type_attr)
15381     return NULL;
15382
15383   return lookup_die_type (die, type_attr, cu);
15384 }
15385
15386 /* If DIE has a descriptive_type attribute, then set the TYPE's
15387    descriptive type accordingly.  */
15388
15389 static void
15390 set_descriptive_type (struct type *type, struct die_info *die,
15391                       struct dwarf2_cu *cu)
15392 {
15393   struct type *descriptive_type = die_descriptive_type (die, cu);
15394
15395   if (descriptive_type)
15396     {
15397       ALLOCATE_GNAT_AUX_TYPE (type);
15398       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15399     }
15400 }
15401
15402 /* Return the containing type of the die in question using its
15403    DW_AT_containing_type attribute.  */
15404
15405 static struct type *
15406 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15407 {
15408   struct attribute *type_attr;
15409
15410   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15411   if (!type_attr)
15412     error (_("Dwarf Error: Problem turning containing type into gdb type "
15413              "[in module %s]"), cu->objfile->name);
15414
15415   return lookup_die_type (die, type_attr, cu);
15416 }
15417
15418 /* Look up the type of DIE in CU using its type attribute ATTR.
15419    If there is no type substitute an error marker.  */
15420
15421 static struct type *
15422 lookup_die_type (struct die_info *die, struct attribute *attr,
15423                  struct dwarf2_cu *cu)
15424 {
15425   struct objfile *objfile = cu->objfile;
15426   struct type *this_type;
15427
15428   /* First see if we have it cached.  */
15429
15430   if (attr->form == DW_FORM_GNU_ref_alt)
15431     {
15432       struct dwarf2_per_cu_data *per_cu;
15433       sect_offset offset = dwarf2_get_ref_die_offset (attr);
15434
15435       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15436       this_type = get_die_type_at_offset (offset, per_cu);
15437     }
15438   else if (is_ref_attr (attr))
15439     {
15440       sect_offset offset = dwarf2_get_ref_die_offset (attr);
15441
15442       this_type = get_die_type_at_offset (offset, cu->per_cu);
15443     }
15444   else if (attr->form == DW_FORM_ref_sig8)
15445     {
15446       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15447
15448       /* sig_type will be NULL if the signatured type is missing from
15449          the debug info.  */
15450       if (sig_type == NULL)
15451         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15452                  "at 0x%x [in module %s]"),
15453                die->offset.sect_off, objfile->name);
15454
15455       gdb_assert (sig_type->per_cu.is_debug_types);
15456       /* If we haven't filled in type_offset_in_section yet, then we
15457          haven't read the type in yet.  */
15458       this_type = NULL;
15459       if (sig_type->type_offset_in_section.sect_off != 0)
15460         {
15461           this_type =
15462             get_die_type_at_offset (sig_type->type_offset_in_section,
15463                                     &sig_type->per_cu);
15464         }
15465     }
15466   else
15467     {
15468       dump_die_for_error (die);
15469       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15470              dwarf_attr_name (attr->name), objfile->name);
15471     }
15472
15473   /* If not cached we need to read it in.  */
15474
15475   if (this_type == NULL)
15476     {
15477       struct die_info *type_die;
15478       struct dwarf2_cu *type_cu = cu;
15479
15480       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15481       /* If we found the type now, it's probably because the type came
15482          from an inter-CU reference and the type's CU got expanded before
15483          ours.  */
15484       this_type = get_die_type (type_die, type_cu);
15485       if (this_type == NULL)
15486         this_type = read_type_die_1 (type_die, type_cu);
15487     }
15488
15489   /* If we still don't have a type use an error marker.  */
15490
15491   if (this_type == NULL)
15492     {
15493       char *message, *saved;
15494
15495       /* read_type_die already issued a complaint.  */
15496       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15497                             objfile->name,
15498                             cu->header.offset.sect_off,
15499                             die->offset.sect_off);
15500       saved = obstack_copy0 (&objfile->objfile_obstack,
15501                              message, strlen (message));
15502       xfree (message);
15503
15504       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15505     }
15506
15507   return this_type;
15508 }
15509
15510 /* Return the type in DIE, CU.
15511    Returns NULL for invalid types.
15512
15513    This first does a lookup in the appropriate type_hash table,
15514    and only reads the die in if necessary.
15515
15516    NOTE: This can be called when reading in partial or full symbols.  */
15517
15518 static struct type *
15519 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15520 {
15521   struct type *this_type;
15522
15523   this_type = get_die_type (die, cu);
15524   if (this_type)
15525     return this_type;
15526
15527   return read_type_die_1 (die, cu);
15528 }
15529
15530 /* Read the type in DIE, CU.
15531    Returns NULL for invalid types.  */
15532
15533 static struct type *
15534 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15535 {
15536   struct type *this_type = NULL;
15537
15538   switch (die->tag)
15539     {
15540     case DW_TAG_class_type:
15541     case DW_TAG_interface_type:
15542     case DW_TAG_structure_type:
15543     case DW_TAG_union_type:
15544       this_type = read_structure_type (die, cu);
15545       break;
15546     case DW_TAG_enumeration_type:
15547       this_type = read_enumeration_type (die, cu);
15548       break;
15549     case DW_TAG_subprogram:
15550     case DW_TAG_subroutine_type:
15551     case DW_TAG_inlined_subroutine:
15552       this_type = read_subroutine_type (die, cu);
15553       break;
15554     case DW_TAG_array_type:
15555       this_type = read_array_type (die, cu);
15556       break;
15557     case DW_TAG_set_type:
15558       this_type = read_set_type (die, cu);
15559       break;
15560     case DW_TAG_pointer_type:
15561       this_type = read_tag_pointer_type (die, cu);
15562       break;
15563     case DW_TAG_ptr_to_member_type:
15564       this_type = read_tag_ptr_to_member_type (die, cu);
15565       break;
15566     case DW_TAG_reference_type:
15567       this_type = read_tag_reference_type (die, cu);
15568       break;
15569     case DW_TAG_const_type:
15570       this_type = read_tag_const_type (die, cu);
15571       break;
15572     case DW_TAG_volatile_type:
15573       this_type = read_tag_volatile_type (die, cu);
15574       break;
15575     case DW_TAG_string_type:
15576       this_type = read_tag_string_type (die, cu);
15577       break;
15578     case DW_TAG_typedef:
15579       this_type = read_typedef (die, cu);
15580       break;
15581     case DW_TAG_subrange_type:
15582       this_type = read_subrange_type (die, cu);
15583       break;
15584     case DW_TAG_base_type:
15585       this_type = read_base_type (die, cu);
15586       break;
15587     case DW_TAG_unspecified_type:
15588       this_type = read_unspecified_type (die, cu);
15589       break;
15590     case DW_TAG_namespace:
15591       this_type = read_namespace_type (die, cu);
15592       break;
15593     case DW_TAG_module:
15594       this_type = read_module_type (die, cu);
15595       break;
15596     default:
15597       complaint (&symfile_complaints,
15598                  _("unexpected tag in read_type_die: '%s'"),
15599                  dwarf_tag_name (die->tag));
15600       break;
15601     }
15602
15603   return this_type;
15604 }
15605
15606 /* See if we can figure out if the class lives in a namespace.  We do
15607    this by looking for a member function; its demangled name will
15608    contain namespace info, if there is any.
15609    Return the computed name or NULL.
15610    Space for the result is allocated on the objfile's obstack.
15611    This is the full-die version of guess_partial_die_structure_name.
15612    In this case we know DIE has no useful parent.  */
15613
15614 static char *
15615 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15616 {
15617   struct die_info *spec_die;
15618   struct dwarf2_cu *spec_cu;
15619   struct die_info *child;
15620
15621   spec_cu = cu;
15622   spec_die = die_specification (die, &spec_cu);
15623   if (spec_die != NULL)
15624     {
15625       die = spec_die;
15626       cu = spec_cu;
15627     }
15628
15629   for (child = die->child;
15630        child != NULL;
15631        child = child->sibling)
15632     {
15633       if (child->tag == DW_TAG_subprogram)
15634         {
15635           struct attribute *attr;
15636
15637           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15638           if (attr == NULL)
15639             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15640           if (attr != NULL)
15641             {
15642               char *actual_name
15643                 = language_class_name_from_physname (cu->language_defn,
15644                                                      DW_STRING (attr));
15645               char *name = NULL;
15646
15647               if (actual_name != NULL)
15648                 {
15649                   char *die_name = dwarf2_name (die, cu);
15650
15651                   if (die_name != NULL
15652                       && strcmp (die_name, actual_name) != 0)
15653                     {
15654                       /* Strip off the class name from the full name.
15655                          We want the prefix.  */
15656                       int die_name_len = strlen (die_name);
15657                       int actual_name_len = strlen (actual_name);
15658
15659                       /* Test for '::' as a sanity check.  */
15660                       if (actual_name_len > die_name_len + 2
15661                           && actual_name[actual_name_len
15662                                          - die_name_len - 1] == ':')
15663                         name =
15664                           obsavestring (actual_name,
15665                                         actual_name_len - die_name_len - 2,
15666                                         &cu->objfile->objfile_obstack);
15667                     }
15668                 }
15669               xfree (actual_name);
15670               return name;
15671             }
15672         }
15673     }
15674
15675   return NULL;
15676 }
15677
15678 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
15679    prefix part in such case.  See
15680    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
15681
15682 static char *
15683 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15684 {
15685   struct attribute *attr;
15686   char *base;
15687
15688   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15689       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15690     return NULL;
15691
15692   attr = dwarf2_attr (die, DW_AT_name, cu);
15693   if (attr != NULL && DW_STRING (attr) != NULL)
15694     return NULL;
15695
15696   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15697   if (attr == NULL)
15698     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15699   if (attr == NULL || DW_STRING (attr) == NULL)
15700     return NULL;
15701
15702   /* dwarf2_name had to be already called.  */
15703   gdb_assert (DW_STRING_IS_CANONICAL (attr));
15704
15705   /* Strip the base name, keep any leading namespaces/classes.  */
15706   base = strrchr (DW_STRING (attr), ':');
15707   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15708     return "";
15709
15710   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15711                        &cu->objfile->objfile_obstack);
15712 }
15713
15714 /* Return the name of the namespace/class that DIE is defined within,
15715    or "" if we can't tell.  The caller should not xfree the result.
15716
15717    For example, if we're within the method foo() in the following
15718    code:
15719
15720    namespace N {
15721      class C {
15722        void foo () {
15723        }
15724      };
15725    }
15726
15727    then determine_prefix on foo's die will return "N::C".  */
15728
15729 static const char *
15730 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15731 {
15732   struct die_info *parent, *spec_die;
15733   struct dwarf2_cu *spec_cu;
15734   struct type *parent_type;
15735   char *retval;
15736
15737   if (cu->language != language_cplus && cu->language != language_java
15738       && cu->language != language_fortran)
15739     return "";
15740
15741   retval = anonymous_struct_prefix (die, cu);
15742   if (retval)
15743     return retval;
15744
15745   /* We have to be careful in the presence of DW_AT_specification.
15746      For example, with GCC 3.4, given the code
15747
15748      namespace N {
15749        void foo() {
15750          // Definition of N::foo.
15751        }
15752      }
15753
15754      then we'll have a tree of DIEs like this:
15755
15756      1: DW_TAG_compile_unit
15757        2: DW_TAG_namespace        // N
15758          3: DW_TAG_subprogram     // declaration of N::foo
15759        4: DW_TAG_subprogram       // definition of N::foo
15760             DW_AT_specification   // refers to die #3
15761
15762      Thus, when processing die #4, we have to pretend that we're in
15763      the context of its DW_AT_specification, namely the contex of die
15764      #3.  */
15765   spec_cu = cu;
15766   spec_die = die_specification (die, &spec_cu);
15767   if (spec_die == NULL)
15768     parent = die->parent;
15769   else
15770     {
15771       parent = spec_die->parent;
15772       cu = spec_cu;
15773     }
15774
15775   if (parent == NULL)
15776     return "";
15777   else if (parent->building_fullname)
15778     {
15779       const char *name;
15780       const char *parent_name;
15781
15782       /* It has been seen on RealView 2.2 built binaries,
15783          DW_TAG_template_type_param types actually _defined_ as
15784          children of the parent class:
15785
15786          enum E {};
15787          template class <class Enum> Class{};
15788          Class<enum E> class_e;
15789
15790          1: DW_TAG_class_type (Class)
15791            2: DW_TAG_enumeration_type (E)
15792              3: DW_TAG_enumerator (enum1:0)
15793              3: DW_TAG_enumerator (enum2:1)
15794              ...
15795            2: DW_TAG_template_type_param
15796               DW_AT_type  DW_FORM_ref_udata (E)
15797
15798          Besides being broken debug info, it can put GDB into an
15799          infinite loop.  Consider:
15800
15801          When we're building the full name for Class<E>, we'll start
15802          at Class, and go look over its template type parameters,
15803          finding E.  We'll then try to build the full name of E, and
15804          reach here.  We're now trying to build the full name of E,
15805          and look over the parent DIE for containing scope.  In the
15806          broken case, if we followed the parent DIE of E, we'd again
15807          find Class, and once again go look at its template type
15808          arguments, etc., etc.  Simply don't consider such parent die
15809          as source-level parent of this die (it can't be, the language
15810          doesn't allow it), and break the loop here.  */
15811       name = dwarf2_name (die, cu);
15812       parent_name = dwarf2_name (parent, cu);
15813       complaint (&symfile_complaints,
15814                  _("template param type '%s' defined within parent '%s'"),
15815                  name ? name : "<unknown>",
15816                  parent_name ? parent_name : "<unknown>");
15817       return "";
15818     }
15819   else
15820     switch (parent->tag)
15821       {
15822       case DW_TAG_namespace:
15823         parent_type = read_type_die (parent, cu);
15824         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15825            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15826            Work around this problem here.  */
15827         if (cu->language == language_cplus
15828             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15829           return "";
15830         /* We give a name to even anonymous namespaces.  */
15831         return TYPE_TAG_NAME (parent_type);
15832       case DW_TAG_class_type:
15833       case DW_TAG_interface_type:
15834       case DW_TAG_structure_type:
15835       case DW_TAG_union_type:
15836       case DW_TAG_module:
15837         parent_type = read_type_die (parent, cu);
15838         if (TYPE_TAG_NAME (parent_type) != NULL)
15839           return TYPE_TAG_NAME (parent_type);
15840         else
15841           /* An anonymous structure is only allowed non-static data
15842              members; no typedefs, no member functions, et cetera.
15843              So it does not need a prefix.  */
15844           return "";
15845       case DW_TAG_compile_unit:
15846       case DW_TAG_partial_unit:
15847         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
15848         if (cu->language == language_cplus
15849             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15850             && die->child != NULL
15851             && (die->tag == DW_TAG_class_type
15852                 || die->tag == DW_TAG_structure_type
15853                 || die->tag == DW_TAG_union_type))
15854           {
15855             char *name = guess_full_die_structure_name (die, cu);
15856             if (name != NULL)
15857               return name;
15858           }
15859         return "";
15860       default:
15861         return determine_prefix (parent, cu);
15862       }
15863 }
15864
15865 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15866    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
15867    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
15868    an obconcat, otherwise allocate storage for the result.  The CU argument is
15869    used to determine the language and hence, the appropriate separator.  */
15870
15871 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
15872
15873 static char *
15874 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15875                  int physname, struct dwarf2_cu *cu)
15876 {
15877   const char *lead = "";
15878   const char *sep;
15879
15880   if (suffix == NULL || suffix[0] == '\0'
15881       || prefix == NULL || prefix[0] == '\0')
15882     sep = "";
15883   else if (cu->language == language_java)
15884     sep = ".";
15885   else if (cu->language == language_fortran && physname)
15886     {
15887       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
15888          DW_AT_MIPS_linkage_name is preferred and used instead.  */
15889
15890       lead = "__";
15891       sep = "_MOD_";
15892     }
15893   else
15894     sep = "::";
15895
15896   if (prefix == NULL)
15897     prefix = "";
15898   if (suffix == NULL)
15899     suffix = "";
15900
15901   if (obs == NULL)
15902     {
15903       char *retval
15904         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15905
15906       strcpy (retval, lead);
15907       strcat (retval, prefix);
15908       strcat (retval, sep);
15909       strcat (retval, suffix);
15910       return retval;
15911     }
15912   else
15913     {
15914       /* We have an obstack.  */
15915       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15916     }
15917 }
15918
15919 /* Return sibling of die, NULL if no sibling.  */
15920
15921 static struct die_info *
15922 sibling_die (struct die_info *die)
15923 {
15924   return die->sibling;
15925 }
15926
15927 /* Get name of a die, return NULL if not found.  */
15928
15929 static char *
15930 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15931                           struct obstack *obstack)
15932 {
15933   if (name && cu->language == language_cplus)
15934     {
15935       char *canon_name = cp_canonicalize_string (name);
15936
15937       if (canon_name != NULL)
15938         {
15939           if (strcmp (canon_name, name) != 0)
15940             name = obsavestring (canon_name, strlen (canon_name),
15941                                  obstack);
15942           xfree (canon_name);
15943         }
15944     }
15945
15946   return name;
15947 }
15948
15949 /* Get name of a die, return NULL if not found.  */
15950
15951 static char *
15952 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15953 {
15954   struct attribute *attr;
15955
15956   attr = dwarf2_attr (die, DW_AT_name, cu);
15957   if ((!attr || !DW_STRING (attr))
15958       && die->tag != DW_TAG_class_type
15959       && die->tag != DW_TAG_interface_type
15960       && die->tag != DW_TAG_structure_type
15961       && die->tag != DW_TAG_union_type)
15962     return NULL;
15963
15964   switch (die->tag)
15965     {
15966     case DW_TAG_compile_unit:
15967     case DW_TAG_partial_unit:
15968       /* Compilation units have a DW_AT_name that is a filename, not
15969          a source language identifier.  */
15970     case DW_TAG_enumeration_type:
15971     case DW_TAG_enumerator:
15972       /* These tags always have simple identifiers already; no need
15973          to canonicalize them.  */
15974       return DW_STRING (attr);
15975
15976     case DW_TAG_subprogram:
15977       /* Java constructors will all be named "<init>", so return
15978          the class name when we see this special case.  */
15979       if (cu->language == language_java
15980           && DW_STRING (attr) != NULL
15981           && strcmp (DW_STRING (attr), "<init>") == 0)
15982         {
15983           struct dwarf2_cu *spec_cu = cu;
15984           struct die_info *spec_die;
15985
15986           /* GCJ will output '<init>' for Java constructor names.
15987              For this special case, return the name of the parent class.  */
15988
15989           /* GCJ may output suprogram DIEs with AT_specification set.
15990              If so, use the name of the specified DIE.  */
15991           spec_die = die_specification (die, &spec_cu);
15992           if (spec_die != NULL)
15993             return dwarf2_name (spec_die, spec_cu);
15994
15995           do
15996             {
15997               die = die->parent;
15998               if (die->tag == DW_TAG_class_type)
15999                 return dwarf2_name (die, cu);
16000             }
16001           while (die->tag != DW_TAG_compile_unit
16002                  && die->tag != DW_TAG_partial_unit);
16003         }
16004       break;
16005
16006     case DW_TAG_class_type:
16007     case DW_TAG_interface_type:
16008     case DW_TAG_structure_type:
16009     case DW_TAG_union_type:
16010       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16011          structures or unions.  These were of the form "._%d" in GCC 4.1,
16012          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16013          and GCC 4.4.  We work around this problem by ignoring these.  */
16014       if (attr && DW_STRING (attr)
16015           && (strncmp (DW_STRING (attr), "._", 2) == 0
16016               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16017         return NULL;
16018
16019       /* GCC might emit a nameless typedef that has a linkage name.  See
16020          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
16021       if (!attr || DW_STRING (attr) == NULL)
16022         {
16023           char *demangled = NULL;
16024
16025           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16026           if (attr == NULL)
16027             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16028
16029           if (attr == NULL || DW_STRING (attr) == NULL)
16030             return NULL;
16031
16032           /* Avoid demangling DW_STRING (attr) the second time on a second
16033              call for the same DIE.  */
16034           if (!DW_STRING_IS_CANONICAL (attr))
16035             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16036
16037           if (demangled)
16038             {
16039               char *base;
16040
16041               /* FIXME: we already did this for the partial symbol... */
16042               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16043                                                &cu->objfile->objfile_obstack);
16044               DW_STRING_IS_CANONICAL (attr) = 1;
16045               xfree (demangled);
16046
16047               /* Strip any leading namespaces/classes, keep only the base name.
16048                  DW_AT_name for named DIEs does not contain the prefixes.  */
16049               base = strrchr (DW_STRING (attr), ':');
16050               if (base && base > DW_STRING (attr) && base[-1] == ':')
16051                 return &base[1];
16052               else
16053                 return DW_STRING (attr);
16054             }
16055         }
16056       break;
16057
16058     default:
16059       break;
16060     }
16061
16062   if (!DW_STRING_IS_CANONICAL (attr))
16063     {
16064       DW_STRING (attr)
16065         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16066                                     &cu->objfile->objfile_obstack);
16067       DW_STRING_IS_CANONICAL (attr) = 1;
16068     }
16069   return DW_STRING (attr);
16070 }
16071
16072 /* Return the die that this die in an extension of, or NULL if there
16073    is none.  *EXT_CU is the CU containing DIE on input, and the CU
16074    containing the return value on output.  */
16075
16076 static struct die_info *
16077 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16078 {
16079   struct attribute *attr;
16080
16081   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16082   if (attr == NULL)
16083     return NULL;
16084
16085   return follow_die_ref (die, attr, ext_cu);
16086 }
16087
16088 /* Convert a DIE tag into its string name.  */
16089
16090 static const char *
16091 dwarf_tag_name (unsigned tag)
16092 {
16093   const char *name = get_DW_TAG_name (tag);
16094
16095   if (name == NULL)
16096     return "DW_TAG_<unknown>";
16097
16098   return name;
16099 }
16100
16101 /* Convert a DWARF attribute code into its string name.  */
16102
16103 static const char *
16104 dwarf_attr_name (unsigned attr)
16105 {
16106   const char *name;
16107
16108 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16109   if (attr == DW_AT_MIPS_fde)
16110     return "DW_AT_MIPS_fde";
16111 #else
16112   if (attr == DW_AT_HP_block_index)
16113     return "DW_AT_HP_block_index";
16114 #endif
16115
16116   name = get_DW_AT_name (attr);
16117
16118   if (name == NULL)
16119     return "DW_AT_<unknown>";
16120
16121   return name;
16122 }
16123
16124 /* Convert a DWARF value form code into its string name.  */
16125
16126 static const char *
16127 dwarf_form_name (unsigned form)
16128 {
16129   const char *name = get_DW_FORM_name (form);
16130
16131   if (name == NULL)
16132     return "DW_FORM_<unknown>";
16133
16134   return name;
16135 }
16136
16137 static char *
16138 dwarf_bool_name (unsigned mybool)
16139 {
16140   if (mybool)
16141     return "TRUE";
16142   else
16143     return "FALSE";
16144 }
16145
16146 /* Convert a DWARF type code into its string name.  */
16147
16148 static const char *
16149 dwarf_type_encoding_name (unsigned enc)
16150 {
16151   const char *name = get_DW_ATE_name (enc);
16152
16153   if (name == NULL)
16154     return "DW_ATE_<unknown>";
16155
16156   return name;
16157 }
16158
16159 static void
16160 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
16161 {
16162   unsigned int i;
16163
16164   print_spaces (indent, f);
16165   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
16166            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
16167
16168   if (die->parent != NULL)
16169     {
16170       print_spaces (indent, f);
16171       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
16172                           die->parent->offset.sect_off);
16173     }
16174
16175   print_spaces (indent, f);
16176   fprintf_unfiltered (f, "  has children: %s\n",
16177            dwarf_bool_name (die->child != NULL));
16178
16179   print_spaces (indent, f);
16180   fprintf_unfiltered (f, "  attributes:\n");
16181
16182   for (i = 0; i < die->num_attrs; ++i)
16183     {
16184       print_spaces (indent, f);
16185       fprintf_unfiltered (f, "    %s (%s) ",
16186                dwarf_attr_name (die->attrs[i].name),
16187                dwarf_form_name (die->attrs[i].form));
16188
16189       switch (die->attrs[i].form)
16190         {
16191         case DW_FORM_addr:
16192         case DW_FORM_GNU_addr_index:
16193           fprintf_unfiltered (f, "address: ");
16194           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
16195           break;
16196         case DW_FORM_block2:
16197         case DW_FORM_block4:
16198         case DW_FORM_block:
16199         case DW_FORM_block1:
16200           fprintf_unfiltered (f, "block: size %s",
16201                               pulongest (DW_BLOCK (&die->attrs[i])->size));
16202           break;
16203         case DW_FORM_exprloc:
16204           fprintf_unfiltered (f, "expression: size %s",
16205                               pulongest (DW_BLOCK (&die->attrs[i])->size));
16206           break;
16207         case DW_FORM_ref_addr:
16208           fprintf_unfiltered (f, "ref address: ");
16209           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16210           break;
16211         case DW_FORM_GNU_ref_alt:
16212           fprintf_unfiltered (f, "alt ref address: ");
16213           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16214           break;
16215         case DW_FORM_ref1:
16216         case DW_FORM_ref2:
16217         case DW_FORM_ref4:
16218         case DW_FORM_ref8:
16219         case DW_FORM_ref_udata:
16220           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
16221                               (long) (DW_UNSND (&die->attrs[i])));
16222           break;
16223         case DW_FORM_data1:
16224         case DW_FORM_data2:
16225         case DW_FORM_data4:
16226         case DW_FORM_data8:
16227         case DW_FORM_udata:
16228         case DW_FORM_sdata:
16229           fprintf_unfiltered (f, "constant: %s",
16230                               pulongest (DW_UNSND (&die->attrs[i])));
16231           break;
16232         case DW_FORM_sec_offset:
16233           fprintf_unfiltered (f, "section offset: %s",
16234                               pulongest (DW_UNSND (&die->attrs[i])));
16235           break;
16236         case DW_FORM_ref_sig8:
16237           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16238             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
16239                          DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
16240           else
16241             fprintf_unfiltered (f, "signatured type, offset: unknown");
16242           break;
16243         case DW_FORM_string:
16244         case DW_FORM_strp:
16245         case DW_FORM_GNU_str_index:
16246         case DW_FORM_GNU_strp_alt:
16247           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
16248                    DW_STRING (&die->attrs[i])
16249                    ? DW_STRING (&die->attrs[i]) : "",
16250                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
16251           break;
16252         case DW_FORM_flag:
16253           if (DW_UNSND (&die->attrs[i]))
16254             fprintf_unfiltered (f, "flag: TRUE");
16255           else
16256             fprintf_unfiltered (f, "flag: FALSE");
16257           break;
16258         case DW_FORM_flag_present:
16259           fprintf_unfiltered (f, "flag: TRUE");
16260           break;
16261         case DW_FORM_indirect:
16262           /* The reader will have reduced the indirect form to
16263              the "base form" so this form should not occur.  */
16264           fprintf_unfiltered (f, 
16265                               "unexpected attribute form: DW_FORM_indirect");
16266           break;
16267         default:
16268           fprintf_unfiltered (f, "unsupported attribute form: %d.",
16269                    die->attrs[i].form);
16270           break;
16271         }
16272       fprintf_unfiltered (f, "\n");
16273     }
16274 }
16275
16276 static void
16277 dump_die_for_error (struct die_info *die)
16278 {
16279   dump_die_shallow (gdb_stderr, 0, die);
16280 }
16281
16282 static void
16283 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16284 {
16285   int indent = level * 4;
16286
16287   gdb_assert (die != NULL);
16288
16289   if (level >= max_level)
16290     return;
16291
16292   dump_die_shallow (f, indent, die);
16293
16294   if (die->child != NULL)
16295     {
16296       print_spaces (indent, f);
16297       fprintf_unfiltered (f, "  Children:");
16298       if (level + 1 < max_level)
16299         {
16300           fprintf_unfiltered (f, "\n");
16301           dump_die_1 (f, level + 1, max_level, die->child);
16302         }
16303       else
16304         {
16305           fprintf_unfiltered (f,
16306                               " [not printed, max nesting level reached]\n");
16307         }
16308     }
16309
16310   if (die->sibling != NULL && level > 0)
16311     {
16312       dump_die_1 (f, level, max_level, die->sibling);
16313     }
16314 }
16315
16316 /* This is called from the pdie macro in gdbinit.in.
16317    It's not static so gcc will keep a copy callable from gdb.  */
16318
16319 void
16320 dump_die (struct die_info *die, int max_level)
16321 {
16322   dump_die_1 (gdb_stdlog, 0, max_level, die);
16323 }
16324
16325 static void
16326 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16327 {
16328   void **slot;
16329
16330   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16331                                    INSERT);
16332
16333   *slot = die;
16334 }
16335
16336 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16337    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
16338
16339 static int
16340 is_ref_attr (struct attribute *attr)
16341 {
16342   switch (attr->form)
16343     {
16344     case DW_FORM_ref_addr:
16345     case DW_FORM_ref1:
16346     case DW_FORM_ref2:
16347     case DW_FORM_ref4:
16348     case DW_FORM_ref8:
16349     case DW_FORM_ref_udata:
16350     case DW_FORM_GNU_ref_alt:
16351       return 1;
16352     default:
16353       return 0;
16354     }
16355 }
16356
16357 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
16358    required kind.  */
16359
16360 static sect_offset
16361 dwarf2_get_ref_die_offset (struct attribute *attr)
16362 {
16363   sect_offset retval = { DW_UNSND (attr) };
16364
16365   if (is_ref_attr (attr))
16366     return retval;
16367
16368   retval.sect_off = 0;
16369   complaint (&symfile_complaints,
16370              _("unsupported die ref attribute form: '%s'"),
16371              dwarf_form_name (attr->form));
16372   return retval;
16373 }
16374
16375 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
16376  * the value held by the attribute is not constant.  */
16377
16378 static LONGEST
16379 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16380 {
16381   if (attr->form == DW_FORM_sdata)
16382     return DW_SND (attr);
16383   else if (attr->form == DW_FORM_udata
16384            || attr->form == DW_FORM_data1
16385            || attr->form == DW_FORM_data2
16386            || attr->form == DW_FORM_data4
16387            || attr->form == DW_FORM_data8)
16388     return DW_UNSND (attr);
16389   else
16390     {
16391       complaint (&symfile_complaints,
16392                  _("Attribute value is not a constant (%s)"),
16393                  dwarf_form_name (attr->form));
16394       return default_value;
16395     }
16396 }
16397
16398 /* Follow reference or signature attribute ATTR of SRC_DIE.
16399    On entry *REF_CU is the CU of SRC_DIE.
16400    On exit *REF_CU is the CU of the result.  */
16401
16402 static struct die_info *
16403 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16404                        struct dwarf2_cu **ref_cu)
16405 {
16406   struct die_info *die;
16407
16408   if (is_ref_attr (attr))
16409     die = follow_die_ref (src_die, attr, ref_cu);
16410   else if (attr->form == DW_FORM_ref_sig8)
16411     die = follow_die_sig (src_die, attr, ref_cu);
16412   else
16413     {
16414       dump_die_for_error (src_die);
16415       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16416              (*ref_cu)->objfile->name);
16417     }
16418
16419   return die;
16420 }
16421
16422 /* Follow reference OFFSET.
16423    On entry *REF_CU is the CU of the source die referencing OFFSET.
16424    On exit *REF_CU is the CU of the result.
16425    Returns NULL if OFFSET is invalid.  */
16426
16427 static struct die_info *
16428 follow_die_offset (sect_offset offset, int offset_in_dwz,
16429                    struct dwarf2_cu **ref_cu)
16430 {
16431   struct die_info temp_die;
16432   struct dwarf2_cu *target_cu, *cu = *ref_cu;
16433
16434   gdb_assert (cu->per_cu != NULL);
16435
16436   target_cu = cu;
16437
16438   if (cu->per_cu->is_debug_types)
16439     {
16440       /* .debug_types CUs cannot reference anything outside their CU.
16441          If they need to, they have to reference a signatured type via
16442          DW_FORM_ref_sig8.  */
16443       if (! offset_in_cu_p (&cu->header, offset))
16444         return NULL;
16445     }
16446   else if (offset_in_dwz != cu->per_cu->is_dwz
16447            || ! offset_in_cu_p (&cu->header, offset))
16448     {
16449       struct dwarf2_per_cu_data *per_cu;
16450
16451       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16452                                                  cu->objfile);
16453
16454       /* If necessary, add it to the queue and load its DIEs.  */
16455       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16456         load_full_comp_unit (per_cu, cu->language);
16457
16458       target_cu = per_cu->cu;
16459     }
16460   else if (cu->dies == NULL)
16461     {
16462       /* We're loading full DIEs during partial symbol reading.  */
16463       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16464       load_full_comp_unit (cu->per_cu, language_minimal);
16465     }
16466
16467   *ref_cu = target_cu;
16468   temp_die.offset = offset;
16469   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16470 }
16471
16472 /* Follow reference attribute ATTR of SRC_DIE.
16473    On entry *REF_CU is the CU of SRC_DIE.
16474    On exit *REF_CU is the CU of the result.  */
16475
16476 static struct die_info *
16477 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16478                 struct dwarf2_cu **ref_cu)
16479 {
16480   sect_offset offset = dwarf2_get_ref_die_offset (attr);
16481   struct dwarf2_cu *cu = *ref_cu;
16482   struct die_info *die;
16483
16484   die = follow_die_offset (offset,
16485                            (attr->form == DW_FORM_GNU_ref_alt
16486                             || cu->per_cu->is_dwz),
16487                            ref_cu);
16488   if (!die)
16489     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16490            "at 0x%x [in module %s]"),
16491            offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16492
16493   return die;
16494 }
16495
16496 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16497    Returned value is intended for DW_OP_call*.  Returned
16498    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
16499
16500 struct dwarf2_locexpr_baton
16501 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16502                                  struct dwarf2_per_cu_data *per_cu,
16503                                  CORE_ADDR (*get_frame_pc) (void *baton),
16504                                  void *baton)
16505 {
16506   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16507   struct dwarf2_cu *cu;
16508   struct die_info *die;
16509   struct attribute *attr;
16510   struct dwarf2_locexpr_baton retval;
16511
16512   dw2_setup (per_cu->objfile);
16513
16514   if (per_cu->cu == NULL)
16515     load_cu (per_cu);
16516   cu = per_cu->cu;
16517
16518   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
16519   if (!die)
16520     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16521            offset.sect_off, per_cu->objfile->name);
16522
16523   attr = dwarf2_attr (die, DW_AT_location, cu);
16524   if (!attr)
16525     {
16526       /* DWARF: "If there is no such attribute, then there is no effect.".
16527          DATA is ignored if SIZE is 0.  */
16528
16529       retval.data = NULL;
16530       retval.size = 0;
16531     }
16532   else if (attr_form_is_section_offset (attr))
16533     {
16534       struct dwarf2_loclist_baton loclist_baton;
16535       CORE_ADDR pc = (*get_frame_pc) (baton);
16536       size_t size;
16537
16538       fill_in_loclist_baton (cu, &loclist_baton, attr);
16539
16540       retval.data = dwarf2_find_location_expression (&loclist_baton,
16541                                                      &size, pc);
16542       retval.size = size;
16543     }
16544   else
16545     {
16546       if (!attr_form_is_block (attr))
16547         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16548                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16549                offset.sect_off, per_cu->objfile->name);
16550
16551       retval.data = DW_BLOCK (attr)->data;
16552       retval.size = DW_BLOCK (attr)->size;
16553     }
16554   retval.per_cu = cu->per_cu;
16555
16556   age_cached_comp_units ();
16557
16558   return retval;
16559 }
16560
16561 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16562    PER_CU.  */
16563
16564 struct type *
16565 dwarf2_get_die_type (cu_offset die_offset,
16566                      struct dwarf2_per_cu_data *per_cu)
16567 {
16568   sect_offset die_offset_sect;
16569
16570   dw2_setup (per_cu->objfile);
16571
16572   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16573   return get_die_type_at_offset (die_offset_sect, per_cu);
16574 }
16575
16576 /* Follow the signature attribute ATTR in SRC_DIE.
16577    On entry *REF_CU is the CU of SRC_DIE.
16578    On exit *REF_CU is the CU of the result.  */
16579
16580 static struct die_info *
16581 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16582                 struct dwarf2_cu **ref_cu)
16583 {
16584   struct objfile *objfile = (*ref_cu)->objfile;
16585   struct die_info temp_die;
16586   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16587   struct dwarf2_cu *sig_cu;
16588   struct die_info *die;
16589
16590   /* sig_type will be NULL if the signatured type is missing from
16591      the debug info.  */
16592   if (sig_type == NULL)
16593     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16594              "at 0x%x [in module %s]"),
16595            src_die->offset.sect_off, objfile->name);
16596
16597   /* If necessary, add it to the queue and load its DIEs.  */
16598
16599   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16600     read_signatured_type (sig_type);
16601
16602   gdb_assert (sig_type->per_cu.cu != NULL);
16603
16604   sig_cu = sig_type->per_cu.cu;
16605   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16606   temp_die.offset = sig_type->type_offset_in_section;
16607   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16608                              temp_die.offset.sect_off);
16609   if (die)
16610     {
16611       *ref_cu = sig_cu;
16612       return die;
16613     }
16614
16615   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16616          "from DIE at 0x%x [in module %s]"),
16617          temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16618 }
16619
16620 /* Given an offset of a signatured type, return its signatured_type.  */
16621
16622 static struct signatured_type *
16623 lookup_signatured_type_at_offset (struct objfile *objfile,
16624                                   struct dwarf2_section_info *section,
16625                                   sect_offset offset)
16626 {
16627   gdb_byte *info_ptr = section->buffer + offset.sect_off;
16628   unsigned int length, initial_length_size;
16629   unsigned int sig_offset;
16630   struct signatured_type find_entry, *sig_type;
16631
16632   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16633   sig_offset = (initial_length_size
16634                 + 2 /*version*/
16635                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16636                 + 1 /*address_size*/);
16637   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16638   sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16639
16640   /* This is only used to lookup previously recorded types.
16641      If we didn't find it, it's our bug.  */
16642   gdb_assert (sig_type != NULL);
16643   gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16644
16645   return sig_type;
16646 }
16647
16648 /* Load the DIEs associated with type unit PER_CU into memory.  */
16649
16650 static void
16651 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16652 {
16653   struct signatured_type *sig_type;
16654
16655   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
16656   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16657
16658   /* We have the per_cu, but we need the signatured_type.
16659      Fortunately this is an easy translation.  */
16660   gdb_assert (per_cu->is_debug_types);
16661   sig_type = (struct signatured_type *) per_cu;
16662
16663   gdb_assert (per_cu->cu == NULL);
16664
16665   read_signatured_type (sig_type);
16666
16667   gdb_assert (per_cu->cu != NULL);
16668 }
16669
16670 /* die_reader_func for read_signatured_type.
16671    This is identical to load_full_comp_unit_reader,
16672    but is kept separate for now.  */
16673
16674 static void
16675 read_signatured_type_reader (const struct die_reader_specs *reader,
16676                              gdb_byte *info_ptr,
16677                              struct die_info *comp_unit_die,
16678                              int has_children,
16679                              void *data)
16680 {
16681   struct dwarf2_cu *cu = reader->cu;
16682
16683   gdb_assert (cu->die_hash == NULL);
16684   cu->die_hash =
16685     htab_create_alloc_ex (cu->header.length / 12,
16686                           die_hash,
16687                           die_eq,
16688                           NULL,
16689                           &cu->comp_unit_obstack,
16690                           hashtab_obstack_allocate,
16691                           dummy_obstack_deallocate);
16692
16693   if (has_children)
16694     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16695                                                   &info_ptr, comp_unit_die);
16696   cu->dies = comp_unit_die;
16697   /* comp_unit_die is not stored in die_hash, no need.  */
16698
16699   /* We try not to read any attributes in this function, because not
16700      all CUs needed for references have been loaded yet, and symbol
16701      table processing isn't initialized.  But we have to set the CU language,
16702      or we won't be able to build types correctly.
16703      Similarly, if we do not read the producer, we can not apply
16704      producer-specific interpretation.  */
16705   prepare_one_comp_unit (cu, cu->dies, language_minimal);
16706 }
16707
16708 /* Read in a signatured type and build its CU and DIEs.
16709    If the type is a stub for the real type in a DWO file,
16710    read in the real type from the DWO file as well.  */
16711
16712 static void
16713 read_signatured_type (struct signatured_type *sig_type)
16714 {
16715   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16716
16717   gdb_assert (per_cu->is_debug_types);
16718   gdb_assert (per_cu->cu == NULL);
16719
16720   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16721                            read_signatured_type_reader, NULL);
16722 }
16723
16724 /* Decode simple location descriptions.
16725    Given a pointer to a dwarf block that defines a location, compute
16726    the location and return the value.
16727
16728    NOTE drow/2003-11-18: This function is called in two situations
16729    now: for the address of static or global variables (partial symbols
16730    only) and for offsets into structures which are expected to be
16731    (more or less) constant.  The partial symbol case should go away,
16732    and only the constant case should remain.  That will let this
16733    function complain more accurately.  A few special modes are allowed
16734    without complaint for global variables (for instance, global
16735    register values and thread-local values).
16736
16737    A location description containing no operations indicates that the
16738    object is optimized out.  The return value is 0 for that case.
16739    FIXME drow/2003-11-16: No callers check for this case any more; soon all
16740    callers will only want a very basic result and this can become a
16741    complaint.
16742
16743    Note that stack[0] is unused except as a default error return.  */
16744
16745 static CORE_ADDR
16746 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16747 {
16748   struct objfile *objfile = cu->objfile;
16749   size_t i;
16750   size_t size = blk->size;
16751   gdb_byte *data = blk->data;
16752   CORE_ADDR stack[64];
16753   int stacki;
16754   unsigned int bytes_read, unsnd;
16755   gdb_byte op;
16756
16757   i = 0;
16758   stacki = 0;
16759   stack[stacki] = 0;
16760   stack[++stacki] = 0;
16761
16762   while (i < size)
16763     {
16764       op = data[i++];
16765       switch (op)
16766         {
16767         case DW_OP_lit0:
16768         case DW_OP_lit1:
16769         case DW_OP_lit2:
16770         case DW_OP_lit3:
16771         case DW_OP_lit4:
16772         case DW_OP_lit5:
16773         case DW_OP_lit6:
16774         case DW_OP_lit7:
16775         case DW_OP_lit8:
16776         case DW_OP_lit9:
16777         case DW_OP_lit10:
16778         case DW_OP_lit11:
16779         case DW_OP_lit12:
16780         case DW_OP_lit13:
16781         case DW_OP_lit14:
16782         case DW_OP_lit15:
16783         case DW_OP_lit16:
16784         case DW_OP_lit17:
16785         case DW_OP_lit18:
16786         case DW_OP_lit19:
16787         case DW_OP_lit20:
16788         case DW_OP_lit21:
16789         case DW_OP_lit22:
16790         case DW_OP_lit23:
16791         case DW_OP_lit24:
16792         case DW_OP_lit25:
16793         case DW_OP_lit26:
16794         case DW_OP_lit27:
16795         case DW_OP_lit28:
16796         case DW_OP_lit29:
16797         case DW_OP_lit30:
16798         case DW_OP_lit31:
16799           stack[++stacki] = op - DW_OP_lit0;
16800           break;
16801
16802         case DW_OP_reg0:
16803         case DW_OP_reg1:
16804         case DW_OP_reg2:
16805         case DW_OP_reg3:
16806         case DW_OP_reg4:
16807         case DW_OP_reg5:
16808         case DW_OP_reg6:
16809         case DW_OP_reg7:
16810         case DW_OP_reg8:
16811         case DW_OP_reg9:
16812         case DW_OP_reg10:
16813         case DW_OP_reg11:
16814         case DW_OP_reg12:
16815         case DW_OP_reg13:
16816         case DW_OP_reg14:
16817         case DW_OP_reg15:
16818         case DW_OP_reg16:
16819         case DW_OP_reg17:
16820         case DW_OP_reg18:
16821         case DW_OP_reg19:
16822         case DW_OP_reg20:
16823         case DW_OP_reg21:
16824         case DW_OP_reg22:
16825         case DW_OP_reg23:
16826         case DW_OP_reg24:
16827         case DW_OP_reg25:
16828         case DW_OP_reg26:
16829         case DW_OP_reg27:
16830         case DW_OP_reg28:
16831         case DW_OP_reg29:
16832         case DW_OP_reg30:
16833         case DW_OP_reg31:
16834           stack[++stacki] = op - DW_OP_reg0;
16835           if (i < size)
16836             dwarf2_complex_location_expr_complaint ();
16837           break;
16838
16839         case DW_OP_regx:
16840           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16841           i += bytes_read;
16842           stack[++stacki] = unsnd;
16843           if (i < size)
16844             dwarf2_complex_location_expr_complaint ();
16845           break;
16846
16847         case DW_OP_addr:
16848           stack[++stacki] = read_address (objfile->obfd, &data[i],
16849                                           cu, &bytes_read);
16850           i += bytes_read;
16851           break;
16852
16853         case DW_OP_const1u:
16854           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16855           i += 1;
16856           break;
16857
16858         case DW_OP_const1s:
16859           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16860           i += 1;
16861           break;
16862
16863         case DW_OP_const2u:
16864           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16865           i += 2;
16866           break;
16867
16868         case DW_OP_const2s:
16869           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16870           i += 2;
16871           break;
16872
16873         case DW_OP_const4u:
16874           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16875           i += 4;
16876           break;
16877
16878         case DW_OP_const4s:
16879           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16880           i += 4;
16881           break;
16882
16883         case DW_OP_const8u:
16884           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16885           i += 8;
16886           break;
16887
16888         case DW_OP_constu:
16889           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16890                                                   &bytes_read);
16891           i += bytes_read;
16892           break;
16893
16894         case DW_OP_consts:
16895           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16896           i += bytes_read;
16897           break;
16898
16899         case DW_OP_dup:
16900           stack[stacki + 1] = stack[stacki];
16901           stacki++;
16902           break;
16903
16904         case DW_OP_plus:
16905           stack[stacki - 1] += stack[stacki];
16906           stacki--;
16907           break;
16908
16909         case DW_OP_plus_uconst:
16910           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16911                                                  &bytes_read);
16912           i += bytes_read;
16913           break;
16914
16915         case DW_OP_minus:
16916           stack[stacki - 1] -= stack[stacki];
16917           stacki--;
16918           break;
16919
16920         case DW_OP_deref:
16921           /* If we're not the last op, then we definitely can't encode
16922              this using GDB's address_class enum.  This is valid for partial
16923              global symbols, although the variable's address will be bogus
16924              in the psymtab.  */
16925           if (i < size)
16926             dwarf2_complex_location_expr_complaint ();
16927           break;
16928
16929         case DW_OP_GNU_push_tls_address:
16930           /* The top of the stack has the offset from the beginning
16931              of the thread control block at which the variable is located.  */
16932           /* Nothing should follow this operator, so the top of stack would
16933              be returned.  */
16934           /* This is valid for partial global symbols, but the variable's
16935              address will be bogus in the psymtab.  Make it always at least
16936              non-zero to not look as a variable garbage collected by linker
16937              which have DW_OP_addr 0.  */
16938           if (i < size)
16939             dwarf2_complex_location_expr_complaint ();
16940           stack[stacki]++;
16941           break;
16942
16943         case DW_OP_GNU_uninit:
16944           break;
16945
16946         case DW_OP_GNU_addr_index:
16947         case DW_OP_GNU_const_index:
16948           stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16949                                                          &bytes_read);
16950           i += bytes_read;
16951           break;
16952
16953         default:
16954           {
16955             const char *name = get_DW_OP_name (op);
16956
16957             if (name)
16958               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16959                          name);
16960             else
16961               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16962                          op);
16963           }
16964
16965           return (stack[stacki]);
16966         }
16967
16968       /* Enforce maximum stack depth of SIZE-1 to avoid writing
16969          outside of the allocated space.  Also enforce minimum>0.  */
16970       if (stacki >= ARRAY_SIZE (stack) - 1)
16971         {
16972           complaint (&symfile_complaints,
16973                      _("location description stack overflow"));
16974           return 0;
16975         }
16976
16977       if (stacki <= 0)
16978         {
16979           complaint (&symfile_complaints,
16980                      _("location description stack underflow"));
16981           return 0;
16982         }
16983     }
16984   return (stack[stacki]);
16985 }
16986
16987 /* memory allocation interface */
16988
16989 static struct dwarf_block *
16990 dwarf_alloc_block (struct dwarf2_cu *cu)
16991 {
16992   struct dwarf_block *blk;
16993
16994   blk = (struct dwarf_block *)
16995     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16996   return (blk);
16997 }
16998
16999 static struct die_info *
17000 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17001 {
17002   struct die_info *die;
17003   size_t size = sizeof (struct die_info);
17004
17005   if (num_attrs > 1)
17006     size += (num_attrs - 1) * sizeof (struct attribute);
17007
17008   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17009   memset (die, 0, sizeof (struct die_info));
17010   return (die);
17011 }
17012
17013 \f
17014 /* Macro support.  */
17015
17016 /* Return the full name of file number I in *LH's file name table.
17017    Use COMP_DIR as the name of the current directory of the
17018    compilation.  The result is allocated using xmalloc; the caller is
17019    responsible for freeing it.  */
17020 static char *
17021 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17022 {
17023   /* Is the file number a valid index into the line header's file name
17024      table?  Remember that file numbers start with one, not zero.  */
17025   if (1 <= file && file <= lh->num_file_names)
17026     {
17027       struct file_entry *fe = &lh->file_names[file - 1];
17028
17029       if (IS_ABSOLUTE_PATH (fe->name))
17030         return xstrdup (fe->name);
17031       else
17032         {
17033           const char *dir;
17034           int dir_len;
17035           char *full_name;
17036
17037           if (fe->dir_index)
17038             dir = lh->include_dirs[fe->dir_index - 1];
17039           else
17040             dir = comp_dir;
17041
17042           if (dir)
17043             {
17044               dir_len = strlen (dir);
17045               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17046               strcpy (full_name, dir);
17047               full_name[dir_len] = '/';
17048               strcpy (full_name + dir_len + 1, fe->name);
17049               return full_name;
17050             }
17051           else
17052             return xstrdup (fe->name);
17053         }
17054     }
17055   else
17056     {
17057       /* The compiler produced a bogus file number.  We can at least
17058          record the macro definitions made in the file, even if we
17059          won't be able to find the file by name.  */
17060       char fake_name[80];
17061
17062       sprintf (fake_name, "<bad macro file number %d>", file);
17063
17064       complaint (&symfile_complaints,
17065                  _("bad file number in macro information (%d)"),
17066                  file);
17067
17068       return xstrdup (fake_name);
17069     }
17070 }
17071
17072
17073 static struct macro_source_file *
17074 macro_start_file (int file, int line,
17075                   struct macro_source_file *current_file,
17076                   const char *comp_dir,
17077                   struct line_header *lh, struct objfile *objfile)
17078 {
17079   /* The full name of this source file.  */
17080   char *full_name = file_full_name (file, lh, comp_dir);
17081
17082   /* We don't create a macro table for this compilation unit
17083      at all until we actually get a filename.  */
17084   if (! pending_macros)
17085     pending_macros = new_macro_table (&objfile->objfile_obstack,
17086                                       objfile->macro_cache);
17087
17088   if (! current_file)
17089     {
17090       /* If we have no current file, then this must be the start_file
17091          directive for the compilation unit's main source file.  */
17092       current_file = macro_set_main (pending_macros, full_name);
17093       macro_define_special (pending_macros);
17094     }
17095   else
17096     current_file = macro_include (current_file, line, full_name);
17097
17098   xfree (full_name);
17099
17100   return current_file;
17101 }
17102
17103
17104 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17105    followed by a null byte.  */
17106 static char *
17107 copy_string (const char *buf, int len)
17108 {
17109   char *s = xmalloc (len + 1);
17110
17111   memcpy (s, buf, len);
17112   s[len] = '\0';
17113   return s;
17114 }
17115
17116
17117 static const char *
17118 consume_improper_spaces (const char *p, const char *body)
17119 {
17120   if (*p == ' ')
17121     {
17122       complaint (&symfile_complaints,
17123                  _("macro definition contains spaces "
17124                    "in formal argument list:\n`%s'"),
17125                  body);
17126
17127       while (*p == ' ')
17128         p++;
17129     }
17130
17131   return p;
17132 }
17133
17134
17135 static void
17136 parse_macro_definition (struct macro_source_file *file, int line,
17137                         const char *body)
17138 {
17139   const char *p;
17140
17141   /* The body string takes one of two forms.  For object-like macro
17142      definitions, it should be:
17143
17144         <macro name> " " <definition>
17145
17146      For function-like macro definitions, it should be:
17147
17148         <macro name> "() " <definition>
17149      or
17150         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17151
17152      Spaces may appear only where explicitly indicated, and in the
17153      <definition>.
17154
17155      The Dwarf 2 spec says that an object-like macro's name is always
17156      followed by a space, but versions of GCC around March 2002 omit
17157      the space when the macro's definition is the empty string.
17158
17159      The Dwarf 2 spec says that there should be no spaces between the
17160      formal arguments in a function-like macro's formal argument list,
17161      but versions of GCC around March 2002 include spaces after the
17162      commas.  */
17163
17164
17165   /* Find the extent of the macro name.  The macro name is terminated
17166      by either a space or null character (for an object-like macro) or
17167      an opening paren (for a function-like macro).  */
17168   for (p = body; *p; p++)
17169     if (*p == ' ' || *p == '(')
17170       break;
17171
17172   if (*p == ' ' || *p == '\0')
17173     {
17174       /* It's an object-like macro.  */
17175       int name_len = p - body;
17176       char *name = copy_string (body, name_len);
17177       const char *replacement;
17178
17179       if (*p == ' ')
17180         replacement = body + name_len + 1;
17181       else
17182         {
17183           dwarf2_macro_malformed_definition_complaint (body);
17184           replacement = body + name_len;
17185         }
17186
17187       macro_define_object (file, line, name, replacement);
17188
17189       xfree (name);
17190     }
17191   else if (*p == '(')
17192     {
17193       /* It's a function-like macro.  */
17194       char *name = copy_string (body, p - body);
17195       int argc = 0;
17196       int argv_size = 1;
17197       char **argv = xmalloc (argv_size * sizeof (*argv));
17198
17199       p++;
17200
17201       p = consume_improper_spaces (p, body);
17202
17203       /* Parse the formal argument list.  */
17204       while (*p && *p != ')')
17205         {
17206           /* Find the extent of the current argument name.  */
17207           const char *arg_start = p;
17208
17209           while (*p && *p != ',' && *p != ')' && *p != ' ')
17210             p++;
17211
17212           if (! *p || p == arg_start)
17213             dwarf2_macro_malformed_definition_complaint (body);
17214           else
17215             {
17216               /* Make sure argv has room for the new argument.  */
17217               if (argc >= argv_size)
17218                 {
17219                   argv_size *= 2;
17220                   argv = xrealloc (argv, argv_size * sizeof (*argv));
17221                 }
17222
17223               argv[argc++] = copy_string (arg_start, p - arg_start);
17224             }
17225
17226           p = consume_improper_spaces (p, body);
17227
17228           /* Consume the comma, if present.  */
17229           if (*p == ',')
17230             {
17231               p++;
17232
17233               p = consume_improper_spaces (p, body);
17234             }
17235         }
17236
17237       if (*p == ')')
17238         {
17239           p++;
17240
17241           if (*p == ' ')
17242             /* Perfectly formed definition, no complaints.  */
17243             macro_define_function (file, line, name,
17244                                    argc, (const char **) argv,
17245                                    p + 1);
17246           else if (*p == '\0')
17247             {
17248               /* Complain, but do define it.  */
17249               dwarf2_macro_malformed_definition_complaint (body);
17250               macro_define_function (file, line, name,
17251                                      argc, (const char **) argv,
17252                                      p);
17253             }
17254           else
17255             /* Just complain.  */
17256             dwarf2_macro_malformed_definition_complaint (body);
17257         }
17258       else
17259         /* Just complain.  */
17260         dwarf2_macro_malformed_definition_complaint (body);
17261
17262       xfree (name);
17263       {
17264         int i;
17265
17266         for (i = 0; i < argc; i++)
17267           xfree (argv[i]);
17268       }
17269       xfree (argv);
17270     }
17271   else
17272     dwarf2_macro_malformed_definition_complaint (body);
17273 }
17274
17275 /* Skip some bytes from BYTES according to the form given in FORM.
17276    Returns the new pointer.  */
17277
17278 static gdb_byte *
17279 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17280                  enum dwarf_form form,
17281                  unsigned int offset_size,
17282                  struct dwarf2_section_info *section)
17283 {
17284   unsigned int bytes_read;
17285
17286   switch (form)
17287     {
17288     case DW_FORM_data1:
17289     case DW_FORM_flag:
17290       ++bytes;
17291       break;
17292
17293     case DW_FORM_data2:
17294       bytes += 2;
17295       break;
17296
17297     case DW_FORM_data4:
17298       bytes += 4;
17299       break;
17300
17301     case DW_FORM_data8:
17302       bytes += 8;
17303       break;
17304
17305     case DW_FORM_string:
17306       read_direct_string (abfd, bytes, &bytes_read);
17307       bytes += bytes_read;
17308       break;
17309
17310     case DW_FORM_sec_offset:
17311     case DW_FORM_strp:
17312     case DW_FORM_GNU_strp_alt:
17313       bytes += offset_size;
17314       break;
17315
17316     case DW_FORM_block:
17317       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17318       bytes += bytes_read;
17319       break;
17320
17321     case DW_FORM_block1:
17322       bytes += 1 + read_1_byte (abfd, bytes);
17323       break;
17324     case DW_FORM_block2:
17325       bytes += 2 + read_2_bytes (abfd, bytes);
17326       break;
17327     case DW_FORM_block4:
17328       bytes += 4 + read_4_bytes (abfd, bytes);
17329       break;
17330
17331     case DW_FORM_sdata:
17332     case DW_FORM_udata:
17333     case DW_FORM_GNU_addr_index:
17334     case DW_FORM_GNU_str_index:
17335       bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17336       if (bytes == NULL)
17337         {
17338           dwarf2_section_buffer_overflow_complaint (section);
17339           return NULL;
17340         }
17341       break;
17342
17343     default:
17344       {
17345       complain:
17346         complaint (&symfile_complaints,
17347                    _("invalid form 0x%x in `%s'"),
17348                    form,
17349                    section->asection->name);
17350         return NULL;
17351       }
17352     }
17353
17354   return bytes;
17355 }
17356
17357 /* A helper for dwarf_decode_macros that handles skipping an unknown
17358    opcode.  Returns an updated pointer to the macro data buffer; or,
17359    on error, issues a complaint and returns NULL.  */
17360
17361 static gdb_byte *
17362 skip_unknown_opcode (unsigned int opcode,
17363                      gdb_byte **opcode_definitions,
17364                      gdb_byte *mac_ptr, gdb_byte *mac_end,
17365                      bfd *abfd,
17366                      unsigned int offset_size,
17367                      struct dwarf2_section_info *section)
17368 {
17369   unsigned int bytes_read, i;
17370   unsigned long arg;
17371   gdb_byte *defn;
17372
17373   if (opcode_definitions[opcode] == NULL)
17374     {
17375       complaint (&symfile_complaints,
17376                  _("unrecognized DW_MACFINO opcode 0x%x"),
17377                  opcode);
17378       return NULL;
17379     }
17380
17381   defn = opcode_definitions[opcode];
17382   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17383   defn += bytes_read;
17384
17385   for (i = 0; i < arg; ++i)
17386     {
17387       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17388                                  section);
17389       if (mac_ptr == NULL)
17390         {
17391           /* skip_form_bytes already issued the complaint.  */
17392           return NULL;
17393         }
17394     }
17395
17396   return mac_ptr;
17397 }
17398
17399 /* A helper function which parses the header of a macro section.
17400    If the macro section is the extended (for now called "GNU") type,
17401    then this updates *OFFSET_SIZE.  Returns a pointer to just after
17402    the header, or issues a complaint and returns NULL on error.  */
17403
17404 static gdb_byte *
17405 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17406                           bfd *abfd,
17407                           gdb_byte *mac_ptr,
17408                           unsigned int *offset_size,
17409                           int section_is_gnu)
17410 {
17411   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17412
17413   if (section_is_gnu)
17414     {
17415       unsigned int version, flags;
17416
17417       version = read_2_bytes (abfd, mac_ptr);
17418       if (version != 4)
17419         {
17420           complaint (&symfile_complaints,
17421                      _("unrecognized version `%d' in .debug_macro section"),
17422                      version);
17423           return NULL;
17424         }
17425       mac_ptr += 2;
17426
17427       flags = read_1_byte (abfd, mac_ptr);
17428       ++mac_ptr;
17429       *offset_size = (flags & 1) ? 8 : 4;
17430
17431       if ((flags & 2) != 0)
17432         /* We don't need the line table offset.  */
17433         mac_ptr += *offset_size;
17434
17435       /* Vendor opcode descriptions.  */
17436       if ((flags & 4) != 0)
17437         {
17438           unsigned int i, count;
17439
17440           count = read_1_byte (abfd, mac_ptr);
17441           ++mac_ptr;
17442           for (i = 0; i < count; ++i)
17443             {
17444               unsigned int opcode, bytes_read;
17445               unsigned long arg;
17446
17447               opcode = read_1_byte (abfd, mac_ptr);
17448               ++mac_ptr;
17449               opcode_definitions[opcode] = mac_ptr;
17450               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17451               mac_ptr += bytes_read;
17452               mac_ptr += arg;
17453             }
17454         }
17455     }
17456
17457   return mac_ptr;
17458 }
17459
17460 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17461    including DW_MACRO_GNU_transparent_include.  */
17462
17463 static void
17464 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17465                           struct macro_source_file *current_file,
17466                           struct line_header *lh, char *comp_dir,
17467                           struct dwarf2_section_info *section,
17468                           int section_is_gnu, int section_is_dwz,
17469                           unsigned int offset_size,
17470                           struct objfile *objfile,
17471                           htab_t include_hash)
17472 {
17473   enum dwarf_macro_record_type macinfo_type;
17474   int at_commandline;
17475   gdb_byte *opcode_definitions[256];
17476
17477   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17478                                       &offset_size, section_is_gnu);
17479   if (mac_ptr == NULL)
17480     {
17481       /* We already issued a complaint.  */
17482       return;
17483     }
17484
17485   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
17486      GDB is still reading the definitions from command line.  First
17487      DW_MACINFO_start_file will need to be ignored as it was already executed
17488      to create CURRENT_FILE for the main source holding also the command line
17489      definitions.  On first met DW_MACINFO_start_file this flag is reset to
17490      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
17491
17492   at_commandline = 1;
17493
17494   do
17495     {
17496       /* Do we at least have room for a macinfo type byte?  */
17497       if (mac_ptr >= mac_end)
17498         {
17499           dwarf2_section_buffer_overflow_complaint (section);
17500           break;
17501         }
17502
17503       macinfo_type = read_1_byte (abfd, mac_ptr);
17504       mac_ptr++;
17505
17506       /* Note that we rely on the fact that the corresponding GNU and
17507          DWARF constants are the same.  */
17508       switch (macinfo_type)
17509         {
17510           /* A zero macinfo type indicates the end of the macro
17511              information.  */
17512         case 0:
17513           break;
17514
17515         case DW_MACRO_GNU_define:
17516         case DW_MACRO_GNU_undef:
17517         case DW_MACRO_GNU_define_indirect:
17518         case DW_MACRO_GNU_undef_indirect:
17519         case DW_MACRO_GNU_define_indirect_alt:
17520         case DW_MACRO_GNU_undef_indirect_alt:
17521           {
17522             unsigned int bytes_read;
17523             int line;
17524             char *body;
17525             int is_define;
17526
17527             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17528             mac_ptr += bytes_read;
17529
17530             if (macinfo_type == DW_MACRO_GNU_define
17531                 || macinfo_type == DW_MACRO_GNU_undef)
17532               {
17533                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17534                 mac_ptr += bytes_read;
17535               }
17536             else
17537               {
17538                 LONGEST str_offset;
17539
17540                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17541                 mac_ptr += offset_size;
17542
17543                 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
17544                     || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17545                     || section_is_dwz)
17546                   {
17547                     struct dwz_file *dwz = dwarf2_get_dwz_file ();
17548
17549                     body = read_indirect_string_from_dwz (dwz, str_offset);
17550                   }
17551                 else
17552                   body = read_indirect_string_at_offset (abfd, str_offset);
17553               }
17554
17555             is_define = (macinfo_type == DW_MACRO_GNU_define
17556                          || macinfo_type == DW_MACRO_GNU_define_indirect
17557                          || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
17558             if (! current_file)
17559               {
17560                 /* DWARF violation as no main source is present.  */
17561                 complaint (&symfile_complaints,
17562                            _("debug info with no main source gives macro %s "
17563                              "on line %d: %s"),
17564                            is_define ? _("definition") : _("undefinition"),
17565                            line, body);
17566                 break;
17567               }
17568             if ((line == 0 && !at_commandline)
17569                 || (line != 0 && at_commandline))
17570               complaint (&symfile_complaints,
17571                          _("debug info gives %s macro %s with %s line %d: %s"),
17572                          at_commandline ? _("command-line") : _("in-file"),
17573                          is_define ? _("definition") : _("undefinition"),
17574                          line == 0 ? _("zero") : _("non-zero"), line, body);
17575
17576             if (is_define)
17577               parse_macro_definition (current_file, line, body);
17578             else
17579               {
17580                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17581                             || macinfo_type == DW_MACRO_GNU_undef_indirect
17582                             || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
17583                 macro_undef (current_file, line, body);
17584               }
17585           }
17586           break;
17587
17588         case DW_MACRO_GNU_start_file:
17589           {
17590             unsigned int bytes_read;
17591             int line, file;
17592
17593             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17594             mac_ptr += bytes_read;
17595             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17596             mac_ptr += bytes_read;
17597
17598             if ((line == 0 && !at_commandline)
17599                 || (line != 0 && at_commandline))
17600               complaint (&symfile_complaints,
17601                          _("debug info gives source %d included "
17602                            "from %s at %s line %d"),
17603                          file, at_commandline ? _("command-line") : _("file"),
17604                          line == 0 ? _("zero") : _("non-zero"), line);
17605
17606             if (at_commandline)
17607               {
17608                 /* This DW_MACRO_GNU_start_file was executed in the
17609                    pass one.  */
17610                 at_commandline = 0;
17611               }
17612             else
17613               current_file = macro_start_file (file, line,
17614                                                current_file, comp_dir,
17615                                                lh, objfile);
17616           }
17617           break;
17618
17619         case DW_MACRO_GNU_end_file:
17620           if (! current_file)
17621             complaint (&symfile_complaints,
17622                        _("macro debug info has an unmatched "
17623                          "`close_file' directive"));
17624           else
17625             {
17626               current_file = current_file->included_by;
17627               if (! current_file)
17628                 {
17629                   enum dwarf_macro_record_type next_type;
17630
17631                   /* GCC circa March 2002 doesn't produce the zero
17632                      type byte marking the end of the compilation
17633                      unit.  Complain if it's not there, but exit no
17634                      matter what.  */
17635
17636                   /* Do we at least have room for a macinfo type byte?  */
17637                   if (mac_ptr >= mac_end)
17638                     {
17639                       dwarf2_section_buffer_overflow_complaint (section);
17640                       return;
17641                     }
17642
17643                   /* We don't increment mac_ptr here, so this is just
17644                      a look-ahead.  */
17645                   next_type = read_1_byte (abfd, mac_ptr);
17646                   if (next_type != 0)
17647                     complaint (&symfile_complaints,
17648                                _("no terminating 0-type entry for "
17649                                  "macros in `.debug_macinfo' section"));
17650
17651                   return;
17652                 }
17653             }
17654           break;
17655
17656         case DW_MACRO_GNU_transparent_include:
17657         case DW_MACRO_GNU_transparent_include_alt:
17658           {
17659             LONGEST offset;
17660             void **slot;
17661
17662             offset = read_offset_1 (abfd, mac_ptr, offset_size);
17663             mac_ptr += offset_size;
17664
17665             slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17666             if (*slot != NULL)
17667               {
17668                 /* This has actually happened; see
17669                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
17670                 complaint (&symfile_complaints,
17671                            _("recursive DW_MACRO_GNU_transparent_include in "
17672                              ".debug_macro section"));
17673               }
17674             else
17675               {
17676                 bfd *include_bfd = abfd;
17677                 struct dwarf2_section_info *include_section = section;
17678                 struct dwarf2_section_info alt_section;
17679                 gdb_byte *include_mac_end = mac_end;
17680                 int is_dwz = section_is_dwz;
17681
17682                 *slot = mac_ptr;
17683
17684                 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17685                   {
17686                     struct dwz_file *dwz = dwarf2_get_dwz_file ();
17687
17688                     dwarf2_read_section (dwarf2_per_objfile->objfile,
17689                                          &dwz->macro);
17690
17691                     include_bfd = dwz->macro.asection->owner;
17692                     include_section = &dwz->macro;
17693                     include_mac_end = dwz->macro.buffer + dwz->macro.size;
17694                     is_dwz = 1;
17695                   }
17696
17697                 dwarf_decode_macro_bytes (include_bfd,
17698                                           include_section->buffer + offset,
17699                                           include_mac_end, current_file,
17700                                           lh, comp_dir,
17701                                           section, section_is_gnu, is_dwz,
17702                                           offset_size, objfile, include_hash);
17703
17704                 htab_remove_elt (include_hash, mac_ptr);
17705               }
17706           }
17707           break;
17708
17709         case DW_MACINFO_vendor_ext:
17710           if (!section_is_gnu)
17711             {
17712               unsigned int bytes_read;
17713               int constant;
17714
17715               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17716               mac_ptr += bytes_read;
17717               read_direct_string (abfd, mac_ptr, &bytes_read);
17718               mac_ptr += bytes_read;
17719
17720               /* We don't recognize any vendor extensions.  */
17721               break;
17722             }
17723           /* FALLTHROUGH */
17724
17725         default:
17726           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17727                                          mac_ptr, mac_end, abfd, offset_size,
17728                                          section);
17729           if (mac_ptr == NULL)
17730             return;
17731           break;
17732         }
17733     } while (macinfo_type != 0);
17734 }
17735
17736 static void
17737 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
17738                      char *comp_dir, int section_is_gnu)
17739 {
17740   struct objfile *objfile = dwarf2_per_objfile->objfile;
17741   struct line_header *lh = cu->line_header;
17742   bfd *abfd;
17743   gdb_byte *mac_ptr, *mac_end;
17744   struct macro_source_file *current_file = 0;
17745   enum dwarf_macro_record_type macinfo_type;
17746   unsigned int offset_size = cu->header.offset_size;
17747   gdb_byte *opcode_definitions[256];
17748   struct cleanup *cleanup;
17749   htab_t include_hash;
17750   void **slot;
17751   struct dwarf2_section_info *section;
17752   const char *section_name;
17753
17754   if (cu->dwo_unit != NULL)
17755     {
17756       if (section_is_gnu)
17757         {
17758           section = &cu->dwo_unit->dwo_file->sections.macro;
17759           section_name = ".debug_macro.dwo";
17760         }
17761       else
17762         {
17763           section = &cu->dwo_unit->dwo_file->sections.macinfo;
17764           section_name = ".debug_macinfo.dwo";
17765         }
17766     }
17767   else
17768     {
17769       if (section_is_gnu)
17770         {
17771           section = &dwarf2_per_objfile->macro;
17772           section_name = ".debug_macro";
17773         }
17774       else
17775         {
17776           section = &dwarf2_per_objfile->macinfo;
17777           section_name = ".debug_macinfo";
17778         }
17779     }
17780
17781   dwarf2_read_section (objfile, section);
17782   if (section->buffer == NULL)
17783     {
17784       complaint (&symfile_complaints, _("missing %s section"), section_name);
17785       return;
17786     }
17787   abfd = section->asection->owner;
17788
17789   /* First pass: Find the name of the base filename.
17790      This filename is needed in order to process all macros whose definition
17791      (or undefinition) comes from the command line.  These macros are defined
17792      before the first DW_MACINFO_start_file entry, and yet still need to be
17793      associated to the base file.
17794
17795      To determine the base file name, we scan the macro definitions until we
17796      reach the first DW_MACINFO_start_file entry.  We then initialize
17797      CURRENT_FILE accordingly so that any macro definition found before the
17798      first DW_MACINFO_start_file can still be associated to the base file.  */
17799
17800   mac_ptr = section->buffer + offset;
17801   mac_end = section->buffer + section->size;
17802
17803   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17804                                       &offset_size, section_is_gnu);
17805   if (mac_ptr == NULL)
17806     {
17807       /* We already issued a complaint.  */
17808       return;
17809     }
17810
17811   do
17812     {
17813       /* Do we at least have room for a macinfo type byte?  */
17814       if (mac_ptr >= mac_end)
17815         {
17816           /* Complaint is printed during the second pass as GDB will probably
17817              stop the first pass earlier upon finding
17818              DW_MACINFO_start_file.  */
17819           break;
17820         }
17821
17822       macinfo_type = read_1_byte (abfd, mac_ptr);
17823       mac_ptr++;
17824
17825       /* Note that we rely on the fact that the corresponding GNU and
17826          DWARF constants are the same.  */
17827       switch (macinfo_type)
17828         {
17829           /* A zero macinfo type indicates the end of the macro
17830              information.  */
17831         case 0:
17832           break;
17833
17834         case DW_MACRO_GNU_define:
17835         case DW_MACRO_GNU_undef:
17836           /* Only skip the data by MAC_PTR.  */
17837           {
17838             unsigned int bytes_read;
17839
17840             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17841             mac_ptr += bytes_read;
17842             read_direct_string (abfd, mac_ptr, &bytes_read);
17843             mac_ptr += bytes_read;
17844           }
17845           break;
17846
17847         case DW_MACRO_GNU_start_file:
17848           {
17849             unsigned int bytes_read;
17850             int line, file;
17851
17852             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17853             mac_ptr += bytes_read;
17854             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17855             mac_ptr += bytes_read;
17856
17857             current_file = macro_start_file (file, line, current_file,
17858                                              comp_dir, lh, objfile);
17859           }
17860           break;
17861
17862         case DW_MACRO_GNU_end_file:
17863           /* No data to skip by MAC_PTR.  */
17864           break;
17865
17866         case DW_MACRO_GNU_define_indirect:
17867         case DW_MACRO_GNU_undef_indirect:
17868         case DW_MACRO_GNU_define_indirect_alt:
17869         case DW_MACRO_GNU_undef_indirect_alt:
17870           {
17871             unsigned int bytes_read;
17872
17873             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17874             mac_ptr += bytes_read;
17875             mac_ptr += offset_size;
17876           }
17877           break;
17878
17879         case DW_MACRO_GNU_transparent_include:
17880         case DW_MACRO_GNU_transparent_include_alt:
17881           /* Note that, according to the spec, a transparent include
17882              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
17883              skip this opcode.  */
17884           mac_ptr += offset_size;
17885           break;
17886
17887         case DW_MACINFO_vendor_ext:
17888           /* Only skip the data by MAC_PTR.  */
17889           if (!section_is_gnu)
17890             {
17891               unsigned int bytes_read;
17892
17893               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17894               mac_ptr += bytes_read;
17895               read_direct_string (abfd, mac_ptr, &bytes_read);
17896               mac_ptr += bytes_read;
17897             }
17898           /* FALLTHROUGH */
17899
17900         default:
17901           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
17902                                          mac_ptr, mac_end, abfd, offset_size,
17903                                          section);
17904           if (mac_ptr == NULL)
17905             return;
17906           break;
17907         }
17908     } while (macinfo_type != 0 && current_file == NULL);
17909
17910   /* Second pass: Process all entries.
17911
17912      Use the AT_COMMAND_LINE flag to determine whether we are still processing
17913      command-line macro definitions/undefinitions.  This flag is unset when we
17914      reach the first DW_MACINFO_start_file entry.  */
17915
17916   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
17917                                     NULL, xcalloc, xfree);
17918   cleanup = make_cleanup_htab_delete (include_hash);
17919   mac_ptr = section->buffer + offset;
17920   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
17921   *slot = mac_ptr;
17922   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
17923                             current_file, lh, comp_dir, section,
17924                             section_is_gnu, 0,
17925                             offset_size, objfile, include_hash);
17926   do_cleanups (cleanup);
17927 }
17928
17929 /* Check if the attribute's form is a DW_FORM_block*
17930    if so return true else false.  */
17931
17932 static int
17933 attr_form_is_block (struct attribute *attr)
17934 {
17935   return (attr == NULL ? 0 :
17936       attr->form == DW_FORM_block1
17937       || attr->form == DW_FORM_block2
17938       || attr->form == DW_FORM_block4
17939       || attr->form == DW_FORM_block
17940       || attr->form == DW_FORM_exprloc);
17941 }
17942
17943 /* Return non-zero if ATTR's value is a section offset --- classes
17944    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
17945    You may use DW_UNSND (attr) to retrieve such offsets.
17946
17947    Section 7.5.4, "Attribute Encodings", explains that no attribute
17948    may have a value that belongs to more than one of these classes; it
17949    would be ambiguous if we did, because we use the same forms for all
17950    of them.  */
17951
17952 static int
17953 attr_form_is_section_offset (struct attribute *attr)
17954 {
17955   return (attr->form == DW_FORM_data4
17956           || attr->form == DW_FORM_data8
17957           || attr->form == DW_FORM_sec_offset);
17958 }
17959
17960 /* Return non-zero if ATTR's value falls in the 'constant' class, or
17961    zero otherwise.  When this function returns true, you can apply
17962    dwarf2_get_attr_constant_value to it.
17963
17964    However, note that for some attributes you must check
17965    attr_form_is_section_offset before using this test.  DW_FORM_data4
17966    and DW_FORM_data8 are members of both the constant class, and of
17967    the classes that contain offsets into other debug sections
17968    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
17969    that, if an attribute's can be either a constant or one of the
17970    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
17971    taken as section offsets, not constants.  */
17972
17973 static int
17974 attr_form_is_constant (struct attribute *attr)
17975 {
17976   switch (attr->form)
17977     {
17978     case DW_FORM_sdata:
17979     case DW_FORM_udata:
17980     case DW_FORM_data1:
17981     case DW_FORM_data2:
17982     case DW_FORM_data4:
17983     case DW_FORM_data8:
17984       return 1;
17985     default:
17986       return 0;
17987     }
17988 }
17989
17990 /* Return the .debug_loc section to use for CU.
17991    For DWO files use .debug_loc.dwo.  */
17992
17993 static struct dwarf2_section_info *
17994 cu_debug_loc_section (struct dwarf2_cu *cu)
17995 {
17996   if (cu->dwo_unit)
17997     return &cu->dwo_unit->dwo_file->sections.loc;
17998   return &dwarf2_per_objfile->loc;
17999 }
18000
18001 /* A helper function that fills in a dwarf2_loclist_baton.  */
18002
18003 static void
18004 fill_in_loclist_baton (struct dwarf2_cu *cu,
18005                        struct dwarf2_loclist_baton *baton,
18006                        struct attribute *attr)
18007 {
18008   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18009
18010   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
18011
18012   baton->per_cu = cu->per_cu;
18013   gdb_assert (baton->per_cu);
18014   /* We don't know how long the location list is, but make sure we
18015      don't run off the edge of the section.  */
18016   baton->size = section->size - DW_UNSND (attr);
18017   baton->data = section->buffer + DW_UNSND (attr);
18018   baton->base_address = cu->base_address;
18019   baton->from_dwo = cu->dwo_unit != NULL;
18020 }
18021
18022 static void
18023 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
18024                              struct dwarf2_cu *cu)
18025 {
18026   struct objfile *objfile = dwarf2_per_objfile->objfile;
18027   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18028
18029   if (attr_form_is_section_offset (attr)
18030       /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
18031          the section.  If so, fall through to the complaint in the
18032          other branch.  */
18033       && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
18034     {
18035       struct dwarf2_loclist_baton *baton;
18036
18037       baton = obstack_alloc (&objfile->objfile_obstack,
18038                              sizeof (struct dwarf2_loclist_baton));
18039
18040       fill_in_loclist_baton (cu, baton, attr);
18041
18042       if (cu->base_known == 0)
18043         complaint (&symfile_complaints,
18044                    _("Location list used without "
18045                      "specifying the CU base address."));
18046
18047       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
18048       SYMBOL_LOCATION_BATON (sym) = baton;
18049     }
18050   else
18051     {
18052       struct dwarf2_locexpr_baton *baton;
18053
18054       baton = obstack_alloc (&objfile->objfile_obstack,
18055                              sizeof (struct dwarf2_locexpr_baton));
18056       baton->per_cu = cu->per_cu;
18057       gdb_assert (baton->per_cu);
18058
18059       if (attr_form_is_block (attr))
18060         {
18061           /* Note that we're just copying the block's data pointer
18062              here, not the actual data.  We're still pointing into the
18063              info_buffer for SYM's objfile; right now we never release
18064              that buffer, but when we do clean up properly this may
18065              need to change.  */
18066           baton->size = DW_BLOCK (attr)->size;
18067           baton->data = DW_BLOCK (attr)->data;
18068         }
18069       else
18070         {
18071           dwarf2_invalid_attrib_class_complaint ("location description",
18072                                                  SYMBOL_NATURAL_NAME (sym));
18073           baton->size = 0;
18074         }
18075
18076       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
18077       SYMBOL_LOCATION_BATON (sym) = baton;
18078     }
18079 }
18080
18081 /* Return the OBJFILE associated with the compilation unit CU.  If CU
18082    came from a separate debuginfo file, then the master objfile is
18083    returned.  */
18084
18085 struct objfile *
18086 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
18087 {
18088   struct objfile *objfile = per_cu->objfile;
18089
18090   /* Return the master objfile, so that we can report and look up the
18091      correct file containing this variable.  */
18092   if (objfile->separate_debug_objfile_backlink)
18093     objfile = objfile->separate_debug_objfile_backlink;
18094
18095   return objfile;
18096 }
18097
18098 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
18099    (CU_HEADERP is unused in such case) or prepare a temporary copy at
18100    CU_HEADERP first.  */
18101
18102 static const struct comp_unit_head *
18103 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
18104                        struct dwarf2_per_cu_data *per_cu)
18105 {
18106   gdb_byte *info_ptr;
18107
18108   if (per_cu->cu)
18109     return &per_cu->cu->header;
18110
18111   info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
18112
18113   memset (cu_headerp, 0, sizeof (*cu_headerp));
18114   read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
18115
18116   return cu_headerp;
18117 }
18118
18119 /* Return the address size given in the compilation unit header for CU.  */
18120
18121 int
18122 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
18123 {
18124   struct comp_unit_head cu_header_local;
18125   const struct comp_unit_head *cu_headerp;
18126
18127   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18128
18129   return cu_headerp->addr_size;
18130 }
18131
18132 /* Return the offset size given in the compilation unit header for CU.  */
18133
18134 int
18135 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
18136 {
18137   struct comp_unit_head cu_header_local;
18138   const struct comp_unit_head *cu_headerp;
18139
18140   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18141
18142   return cu_headerp->offset_size;
18143 }
18144
18145 /* See its dwarf2loc.h declaration.  */
18146
18147 int
18148 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
18149 {
18150   struct comp_unit_head cu_header_local;
18151   const struct comp_unit_head *cu_headerp;
18152
18153   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
18154
18155   if (cu_headerp->version == 2)
18156     return cu_headerp->addr_size;
18157   else
18158     return cu_headerp->offset_size;
18159 }
18160
18161 /* Return the text offset of the CU.  The returned offset comes from
18162    this CU's objfile.  If this objfile came from a separate debuginfo
18163    file, then the offset may be different from the corresponding
18164    offset in the parent objfile.  */
18165
18166 CORE_ADDR
18167 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
18168 {
18169   struct objfile *objfile = per_cu->objfile;
18170
18171   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
18172 }
18173
18174 /* Locate the .debug_info compilation unit from CU's objfile which contains
18175    the DIE at OFFSET.  Raises an error on failure.  */
18176
18177 static struct dwarf2_per_cu_data *
18178 dwarf2_find_containing_comp_unit (sect_offset offset,
18179                                   unsigned int offset_in_dwz,
18180                                   struct objfile *objfile)
18181 {
18182   struct dwarf2_per_cu_data *this_cu;
18183   int low, high;
18184   const sect_offset *cu_off;
18185
18186   low = 0;
18187   high = dwarf2_per_objfile->n_comp_units - 1;
18188   while (high > low)
18189     {
18190       struct dwarf2_per_cu_data *mid_cu;
18191       int mid = low + (high - low) / 2;
18192
18193       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
18194       cu_off = &mid_cu->offset;
18195       if (mid_cu->is_dwz > offset_in_dwz
18196           || (mid_cu->is_dwz == offset_in_dwz
18197               && cu_off->sect_off >= offset.sect_off))
18198         high = mid;
18199       else
18200         low = mid + 1;
18201     }
18202   gdb_assert (low == high);
18203   this_cu = dwarf2_per_objfile->all_comp_units[low];
18204   cu_off = &this_cu->offset;
18205   if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
18206     {
18207       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
18208         error (_("Dwarf Error: could not find partial DIE containing "
18209                "offset 0x%lx [in module %s]"),
18210                (long) offset.sect_off, bfd_get_filename (objfile->obfd));
18211
18212       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
18213                   <= offset.sect_off);
18214       return dwarf2_per_objfile->all_comp_units[low-1];
18215     }
18216   else
18217     {
18218       this_cu = dwarf2_per_objfile->all_comp_units[low];
18219       if (low == dwarf2_per_objfile->n_comp_units - 1
18220           && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
18221         error (_("invalid dwarf2 offset %u"), offset.sect_off);
18222       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
18223       return this_cu;
18224     }
18225 }
18226
18227 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
18228
18229 static void
18230 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
18231 {
18232   memset (cu, 0, sizeof (*cu));
18233   per_cu->cu = cu;
18234   cu->per_cu = per_cu;
18235   cu->objfile = per_cu->objfile;
18236   obstack_init (&cu->comp_unit_obstack);
18237 }
18238
18239 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
18240
18241 static void
18242 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
18243                        enum language pretend_language)
18244 {
18245   struct attribute *attr;
18246
18247   /* Set the language we're debugging.  */
18248   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
18249   if (attr)
18250     set_cu_language (DW_UNSND (attr), cu);
18251   else
18252     {
18253       cu->language = pretend_language;
18254       cu->language_defn = language_def (cu->language);
18255     }
18256
18257   attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
18258   if (attr)
18259     cu->producer = DW_STRING (attr);
18260 }
18261
18262 /* Release one cached compilation unit, CU.  We unlink it from the tree
18263    of compilation units, but we don't remove it from the read_in_chain;
18264    the caller is responsible for that.
18265    NOTE: DATA is a void * because this function is also used as a
18266    cleanup routine.  */
18267
18268 static void
18269 free_heap_comp_unit (void *data)
18270 {
18271   struct dwarf2_cu *cu = data;
18272
18273   gdb_assert (cu->per_cu != NULL);
18274   cu->per_cu->cu = NULL;
18275   cu->per_cu = NULL;
18276
18277   obstack_free (&cu->comp_unit_obstack, NULL);
18278
18279   xfree (cu);
18280 }
18281
18282 /* This cleanup function is passed the address of a dwarf2_cu on the stack
18283    when we're finished with it.  We can't free the pointer itself, but be
18284    sure to unlink it from the cache.  Also release any associated storage.  */
18285
18286 static void
18287 free_stack_comp_unit (void *data)
18288 {
18289   struct dwarf2_cu *cu = data;
18290
18291   gdb_assert (cu->per_cu != NULL);
18292   cu->per_cu->cu = NULL;
18293   cu->per_cu = NULL;
18294
18295   obstack_free (&cu->comp_unit_obstack, NULL);
18296   cu->partial_dies = NULL;
18297 }
18298
18299 /* Free all cached compilation units.  */
18300
18301 static void
18302 free_cached_comp_units (void *data)
18303 {
18304   struct dwarf2_per_cu_data *per_cu, **last_chain;
18305
18306   per_cu = dwarf2_per_objfile->read_in_chain;
18307   last_chain = &dwarf2_per_objfile->read_in_chain;
18308   while (per_cu != NULL)
18309     {
18310       struct dwarf2_per_cu_data *next_cu;
18311
18312       next_cu = per_cu->cu->read_in_chain;
18313
18314       free_heap_comp_unit (per_cu->cu);
18315       *last_chain = next_cu;
18316
18317       per_cu = next_cu;
18318     }
18319 }
18320
18321 /* Increase the age counter on each cached compilation unit, and free
18322    any that are too old.  */
18323
18324 static void
18325 age_cached_comp_units (void)
18326 {
18327   struct dwarf2_per_cu_data *per_cu, **last_chain;
18328
18329   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
18330   per_cu = dwarf2_per_objfile->read_in_chain;
18331   while (per_cu != NULL)
18332     {
18333       per_cu->cu->last_used ++;
18334       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
18335         dwarf2_mark (per_cu->cu);
18336       per_cu = per_cu->cu->read_in_chain;
18337     }
18338
18339   per_cu = dwarf2_per_objfile->read_in_chain;
18340   last_chain = &dwarf2_per_objfile->read_in_chain;
18341   while (per_cu != NULL)
18342     {
18343       struct dwarf2_per_cu_data *next_cu;
18344
18345       next_cu = per_cu->cu->read_in_chain;
18346
18347       if (!per_cu->cu->mark)
18348         {
18349           free_heap_comp_unit (per_cu->cu);
18350           *last_chain = next_cu;
18351         }
18352       else
18353         last_chain = &per_cu->cu->read_in_chain;
18354
18355       per_cu = next_cu;
18356     }
18357 }
18358
18359 /* Remove a single compilation unit from the cache.  */
18360
18361 static void
18362 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
18363 {
18364   struct dwarf2_per_cu_data *per_cu, **last_chain;
18365
18366   per_cu = dwarf2_per_objfile->read_in_chain;
18367   last_chain = &dwarf2_per_objfile->read_in_chain;
18368   while (per_cu != NULL)
18369     {
18370       struct dwarf2_per_cu_data *next_cu;
18371
18372       next_cu = per_cu->cu->read_in_chain;
18373
18374       if (per_cu == target_per_cu)
18375         {
18376           free_heap_comp_unit (per_cu->cu);
18377           per_cu->cu = NULL;
18378           *last_chain = next_cu;
18379           break;
18380         }
18381       else
18382         last_chain = &per_cu->cu->read_in_chain;
18383
18384       per_cu = next_cu;
18385     }
18386 }
18387
18388 /* Release all extra memory associated with OBJFILE.  */
18389
18390 void
18391 dwarf2_free_objfile (struct objfile *objfile)
18392 {
18393   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18394
18395   if (dwarf2_per_objfile == NULL)
18396     return;
18397
18398   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
18399   free_cached_comp_units (NULL);
18400
18401   if (dwarf2_per_objfile->quick_file_names_table)
18402     htab_delete (dwarf2_per_objfile->quick_file_names_table);
18403
18404   /* Everything else should be on the objfile obstack.  */
18405 }
18406
18407 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
18408    We store these in a hash table separate from the DIEs, and preserve them
18409    when the DIEs are flushed out of cache.
18410
18411    The CU "per_cu" pointer is needed because offset alone is not enough to
18412    uniquely identify the type.  A file may have multiple .debug_types sections,
18413    or the type may come from a DWO file.  We have to use something in
18414    dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
18415    routine, get_die_type_at_offset, from outside this file, and thus won't
18416    necessarily have PER_CU->cu.  Fortunately, PER_CU is stable for the life
18417    of the objfile.  */
18418
18419 struct dwarf2_per_cu_offset_and_type
18420 {
18421   const struct dwarf2_per_cu_data *per_cu;
18422   sect_offset offset;
18423   struct type *type;
18424 };
18425
18426 /* Hash function for a dwarf2_per_cu_offset_and_type.  */
18427
18428 static hashval_t
18429 per_cu_offset_and_type_hash (const void *item)
18430 {
18431   const struct dwarf2_per_cu_offset_and_type *ofs = item;
18432
18433   return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
18434 }
18435
18436 /* Equality function for a dwarf2_per_cu_offset_and_type.  */
18437
18438 static int
18439 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
18440 {
18441   const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
18442   const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
18443
18444   return (ofs_lhs->per_cu == ofs_rhs->per_cu
18445           && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
18446 }
18447
18448 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
18449    table if necessary.  For convenience, return TYPE.
18450
18451    The DIEs reading must have careful ordering to:
18452     * Not cause infite loops trying to read in DIEs as a prerequisite for
18453       reading current DIE.
18454     * Not trying to dereference contents of still incompletely read in types
18455       while reading in other DIEs.
18456     * Enable referencing still incompletely read in types just by a pointer to
18457       the type without accessing its fields.
18458
18459    Therefore caller should follow these rules:
18460      * Try to fetch any prerequisite types we may need to build this DIE type
18461        before building the type and calling set_die_type.
18462      * After building type call set_die_type for current DIE as soon as
18463        possible before fetching more types to complete the current type.
18464      * Make the type as complete as possible before fetching more types.  */
18465
18466 static struct type *
18467 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
18468 {
18469   struct dwarf2_per_cu_offset_and_type **slot, ofs;
18470   struct objfile *objfile = cu->objfile;
18471
18472   /* For Ada types, make sure that the gnat-specific data is always
18473      initialized (if not already set).  There are a few types where
18474      we should not be doing so, because the type-specific area is
18475      already used to hold some other piece of info (eg: TYPE_CODE_FLT
18476      where the type-specific area is used to store the floatformat).
18477      But this is not a problem, because the gnat-specific information
18478      is actually not needed for these types.  */
18479   if (need_gnat_info (cu)
18480       && TYPE_CODE (type) != TYPE_CODE_FUNC
18481       && TYPE_CODE (type) != TYPE_CODE_FLT
18482       && !HAVE_GNAT_AUX_INFO (type))
18483     INIT_GNAT_SPECIFIC (type);
18484
18485   if (dwarf2_per_objfile->die_type_hash == NULL)
18486     {
18487       dwarf2_per_objfile->die_type_hash =
18488         htab_create_alloc_ex (127,
18489                               per_cu_offset_and_type_hash,
18490                               per_cu_offset_and_type_eq,
18491                               NULL,
18492                               &objfile->objfile_obstack,
18493                               hashtab_obstack_allocate,
18494                               dummy_obstack_deallocate);
18495     }
18496
18497   ofs.per_cu = cu->per_cu;
18498   ofs.offset = die->offset;
18499   ofs.type = type;
18500   slot = (struct dwarf2_per_cu_offset_and_type **)
18501     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
18502   if (*slot)
18503     complaint (&symfile_complaints,
18504                _("A problem internal to GDB: DIE 0x%x has type already set"),
18505                die->offset.sect_off);
18506   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
18507   **slot = ofs;
18508   return type;
18509 }
18510
18511 /* Look up the type for the die at OFFSET in the appropriate type_hash
18512    table, or return NULL if the die does not have a saved type.  */
18513
18514 static struct type *
18515 get_die_type_at_offset (sect_offset offset,
18516                         struct dwarf2_per_cu_data *per_cu)
18517 {
18518   struct dwarf2_per_cu_offset_and_type *slot, ofs;
18519
18520   if (dwarf2_per_objfile->die_type_hash == NULL)
18521     return NULL;
18522
18523   ofs.per_cu = per_cu;
18524   ofs.offset = offset;
18525   slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
18526   if (slot)
18527     return slot->type;
18528   else
18529     return NULL;
18530 }
18531
18532 /* Look up the type for DIE in the appropriate type_hash table,
18533    or return NULL if DIE does not have a saved type.  */
18534
18535 static struct type *
18536 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
18537 {
18538   return get_die_type_at_offset (die->offset, cu->per_cu);
18539 }
18540
18541 /* Add a dependence relationship from CU to REF_PER_CU.  */
18542
18543 static void
18544 dwarf2_add_dependence (struct dwarf2_cu *cu,
18545                        struct dwarf2_per_cu_data *ref_per_cu)
18546 {
18547   void **slot;
18548
18549   if (cu->dependencies == NULL)
18550     cu->dependencies
18551       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
18552                               NULL, &cu->comp_unit_obstack,
18553                               hashtab_obstack_allocate,
18554                               dummy_obstack_deallocate);
18555
18556   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
18557   if (*slot == NULL)
18558     *slot = ref_per_cu;
18559 }
18560
18561 /* Subroutine of dwarf2_mark to pass to htab_traverse.
18562    Set the mark field in every compilation unit in the
18563    cache that we must keep because we are keeping CU.  */
18564
18565 static int
18566 dwarf2_mark_helper (void **slot, void *data)
18567 {
18568   struct dwarf2_per_cu_data *per_cu;
18569
18570   per_cu = (struct dwarf2_per_cu_data *) *slot;
18571
18572   /* cu->dependencies references may not yet have been ever read if QUIT aborts
18573      reading of the chain.  As such dependencies remain valid it is not much
18574      useful to track and undo them during QUIT cleanups.  */
18575   if (per_cu->cu == NULL)
18576     return 1;
18577
18578   if (per_cu->cu->mark)
18579     return 1;
18580   per_cu->cu->mark = 1;
18581
18582   if (per_cu->cu->dependencies != NULL)
18583     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
18584
18585   return 1;
18586 }
18587
18588 /* Set the mark field in CU and in every other compilation unit in the
18589    cache that we must keep because we are keeping CU.  */
18590
18591 static void
18592 dwarf2_mark (struct dwarf2_cu *cu)
18593 {
18594   if (cu->mark)
18595     return;
18596   cu->mark = 1;
18597   if (cu->dependencies != NULL)
18598     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
18599 }
18600
18601 static void
18602 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
18603 {
18604   while (per_cu)
18605     {
18606       per_cu->cu->mark = 0;
18607       per_cu = per_cu->cu->read_in_chain;
18608     }
18609 }
18610
18611 /* Trivial hash function for partial_die_info: the hash value of a DIE
18612    is its offset in .debug_info for this objfile.  */
18613
18614 static hashval_t
18615 partial_die_hash (const void *item)
18616 {
18617   const struct partial_die_info *part_die = item;
18618
18619   return part_die->offset.sect_off;
18620 }
18621
18622 /* Trivial comparison function for partial_die_info structures: two DIEs
18623    are equal if they have the same offset.  */
18624
18625 static int
18626 partial_die_eq (const void *item_lhs, const void *item_rhs)
18627 {
18628   const struct partial_die_info *part_die_lhs = item_lhs;
18629   const struct partial_die_info *part_die_rhs = item_rhs;
18630
18631   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
18632 }
18633
18634 static struct cmd_list_element *set_dwarf2_cmdlist;
18635 static struct cmd_list_element *show_dwarf2_cmdlist;
18636
18637 static void
18638 set_dwarf2_cmd (char *args, int from_tty)
18639 {
18640   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
18641 }
18642
18643 static void
18644 show_dwarf2_cmd (char *args, int from_tty)
18645 {
18646   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
18647 }
18648
18649 /* Free data associated with OBJFILE, if necessary.  */
18650
18651 static void
18652 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
18653 {
18654   struct dwarf2_per_objfile *data = d;
18655   int ix;
18656
18657   for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
18658     VEC_free (dwarf2_per_cu_ptr,
18659               dwarf2_per_objfile->all_comp_units[ix]->s.imported_symtabs);
18660
18661   VEC_free (dwarf2_section_info_def, data->types);
18662
18663   if (data->dwo_files)
18664     free_dwo_files (data->dwo_files, objfile);
18665
18666   if (data->dwz_file && data->dwz_file->dwz_bfd)
18667     gdb_bfd_unref (data->dwz_file->dwz_bfd);
18668 }
18669
18670 \f
18671 /* The "save gdb-index" command.  */
18672
18673 /* The contents of the hash table we create when building the string
18674    table.  */
18675 struct strtab_entry
18676 {
18677   offset_type offset;
18678   const char *str;
18679 };
18680
18681 /* Hash function for a strtab_entry.
18682
18683    Function is used only during write_hash_table so no index format backward
18684    compatibility is needed.  */
18685
18686 static hashval_t
18687 hash_strtab_entry (const void *e)
18688 {
18689   const struct strtab_entry *entry = e;
18690   return mapped_index_string_hash (INT_MAX, entry->str);
18691 }
18692
18693 /* Equality function for a strtab_entry.  */
18694
18695 static int
18696 eq_strtab_entry (const void *a, const void *b)
18697 {
18698   const struct strtab_entry *ea = a;
18699   const struct strtab_entry *eb = b;
18700   return !strcmp (ea->str, eb->str);
18701 }
18702
18703 /* Create a strtab_entry hash table.  */
18704
18705 static htab_t
18706 create_strtab (void)
18707 {
18708   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
18709                             xfree, xcalloc, xfree);
18710 }
18711
18712 /* Add a string to the constant pool.  Return the string's offset in
18713    host order.  */
18714
18715 static offset_type
18716 add_string (htab_t table, struct obstack *cpool, const char *str)
18717 {
18718   void **slot;
18719   struct strtab_entry entry;
18720   struct strtab_entry *result;
18721
18722   entry.str = str;
18723   slot = htab_find_slot (table, &entry, INSERT);
18724   if (*slot)
18725     result = *slot;
18726   else
18727     {
18728       result = XNEW (struct strtab_entry);
18729       result->offset = obstack_object_size (cpool);
18730       result->str = str;
18731       obstack_grow_str0 (cpool, str);
18732       *slot = result;
18733     }
18734   return result->offset;
18735 }
18736
18737 /* An entry in the symbol table.  */
18738 struct symtab_index_entry
18739 {
18740   /* The name of the symbol.  */
18741   const char *name;
18742   /* The offset of the name in the constant pool.  */
18743   offset_type index_offset;
18744   /* A sorted vector of the indices of all the CUs that hold an object
18745      of this name.  */
18746   VEC (offset_type) *cu_indices;
18747 };
18748
18749 /* The symbol table.  This is a power-of-2-sized hash table.  */
18750 struct mapped_symtab
18751 {
18752   offset_type n_elements;
18753   offset_type size;
18754   struct symtab_index_entry **data;
18755 };
18756
18757 /* Hash function for a symtab_index_entry.  */
18758
18759 static hashval_t
18760 hash_symtab_entry (const void *e)
18761 {
18762   const struct symtab_index_entry *entry = e;
18763   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
18764                          sizeof (offset_type) * VEC_length (offset_type,
18765                                                             entry->cu_indices),
18766                          0);
18767 }
18768
18769 /* Equality function for a symtab_index_entry.  */
18770
18771 static int
18772 eq_symtab_entry (const void *a, const void *b)
18773 {
18774   const struct symtab_index_entry *ea = a;
18775   const struct symtab_index_entry *eb = b;
18776   int len = VEC_length (offset_type, ea->cu_indices);
18777   if (len != VEC_length (offset_type, eb->cu_indices))
18778     return 0;
18779   return !memcmp (VEC_address (offset_type, ea->cu_indices),
18780                   VEC_address (offset_type, eb->cu_indices),
18781                   sizeof (offset_type) * len);
18782 }
18783
18784 /* Destroy a symtab_index_entry.  */
18785
18786 static void
18787 delete_symtab_entry (void *p)
18788 {
18789   struct symtab_index_entry *entry = p;
18790   VEC_free (offset_type, entry->cu_indices);
18791   xfree (entry);
18792 }
18793
18794 /* Create a hash table holding symtab_index_entry objects.  */
18795
18796 static htab_t
18797 create_symbol_hash_table (void)
18798 {
18799   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
18800                             delete_symtab_entry, xcalloc, xfree);
18801 }
18802
18803 /* Create a new mapped symtab object.  */
18804
18805 static struct mapped_symtab *
18806 create_mapped_symtab (void)
18807 {
18808   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
18809   symtab->n_elements = 0;
18810   symtab->size = 1024;
18811   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18812   return symtab;
18813 }
18814
18815 /* Destroy a mapped_symtab.  */
18816
18817 static void
18818 cleanup_mapped_symtab (void *p)
18819 {
18820   struct mapped_symtab *symtab = p;
18821   /* The contents of the array are freed when the other hash table is
18822      destroyed.  */
18823   xfree (symtab->data);
18824   xfree (symtab);
18825 }
18826
18827 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
18828    the slot.
18829    
18830    Function is used only during write_hash_table so no index format backward
18831    compatibility is needed.  */
18832
18833 static struct symtab_index_entry **
18834 find_slot (struct mapped_symtab *symtab, const char *name)
18835 {
18836   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
18837
18838   index = hash & (symtab->size - 1);
18839   step = ((hash * 17) & (symtab->size - 1)) | 1;
18840
18841   for (;;)
18842     {
18843       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
18844         return &symtab->data[index];
18845       index = (index + step) & (symtab->size - 1);
18846     }
18847 }
18848
18849 /* Expand SYMTAB's hash table.  */
18850
18851 static void
18852 hash_expand (struct mapped_symtab *symtab)
18853 {
18854   offset_type old_size = symtab->size;
18855   offset_type i;
18856   struct symtab_index_entry **old_entries = symtab->data;
18857
18858   symtab->size *= 2;
18859   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
18860
18861   for (i = 0; i < old_size; ++i)
18862     {
18863       if (old_entries[i])
18864         {
18865           struct symtab_index_entry **slot = find_slot (symtab,
18866                                                         old_entries[i]->name);
18867           *slot = old_entries[i];
18868         }
18869     }
18870
18871   xfree (old_entries);
18872 }
18873
18874 /* Add an entry to SYMTAB.  NAME is the name of the symbol.
18875    CU_INDEX is the index of the CU in which the symbol appears.
18876    IS_STATIC is one if the symbol is static, otherwise zero (global).  */
18877
18878 static void
18879 add_index_entry (struct mapped_symtab *symtab, const char *name,
18880                  int is_static, gdb_index_symbol_kind kind,
18881                  offset_type cu_index)
18882 {
18883   struct symtab_index_entry **slot;
18884   offset_type cu_index_and_attrs;
18885
18886   ++symtab->n_elements;
18887   if (4 * symtab->n_elements / 3 >= symtab->size)
18888     hash_expand (symtab);
18889
18890   slot = find_slot (symtab, name);
18891   if (!*slot)
18892     {
18893       *slot = XNEW (struct symtab_index_entry);
18894       (*slot)->name = name;
18895       /* index_offset is set later.  */
18896       (*slot)->cu_indices = NULL;
18897     }
18898
18899   cu_index_and_attrs = 0;
18900   DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
18901   DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
18902   DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
18903
18904   /* We don't want to record an index value twice as we want to avoid the
18905      duplication.
18906      We process all global symbols and then all static symbols
18907      (which would allow us to avoid the duplication by only having to check
18908      the last entry pushed), but a symbol could have multiple kinds in one CU.
18909      To keep things simple we don't worry about the duplication here and
18910      sort and uniqufy the list after we've processed all symbols.  */
18911   VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
18912 }
18913
18914 /* qsort helper routine for uniquify_cu_indices.  */
18915
18916 static int
18917 offset_type_compare (const void *ap, const void *bp)
18918 {
18919   offset_type a = *(offset_type *) ap;
18920   offset_type b = *(offset_type *) bp;
18921
18922   return (a > b) - (b > a);
18923 }
18924
18925 /* Sort and remove duplicates of all symbols' cu_indices lists.  */
18926
18927 static void
18928 uniquify_cu_indices (struct mapped_symtab *symtab)
18929 {
18930   int i;
18931
18932   for (i = 0; i < symtab->size; ++i)
18933     {
18934       struct symtab_index_entry *entry = symtab->data[i];
18935
18936       if (entry
18937           && entry->cu_indices != NULL)
18938         {
18939           unsigned int next_to_insert, next_to_check;
18940           offset_type last_value;
18941
18942           qsort (VEC_address (offset_type, entry->cu_indices),
18943                  VEC_length (offset_type, entry->cu_indices),
18944                  sizeof (offset_type), offset_type_compare);
18945
18946           last_value = VEC_index (offset_type, entry->cu_indices, 0);
18947           next_to_insert = 1;
18948           for (next_to_check = 1;
18949                next_to_check < VEC_length (offset_type, entry->cu_indices);
18950                ++next_to_check)
18951             {
18952               if (VEC_index (offset_type, entry->cu_indices, next_to_check)
18953                   != last_value)
18954                 {
18955                   last_value = VEC_index (offset_type, entry->cu_indices,
18956                                           next_to_check);
18957                   VEC_replace (offset_type, entry->cu_indices, next_to_insert,
18958                                last_value);
18959                   ++next_to_insert;
18960                 }
18961             }
18962           VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
18963         }
18964     }
18965 }
18966
18967 /* Add a vector of indices to the constant pool.  */
18968
18969 static offset_type
18970 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
18971                       struct symtab_index_entry *entry)
18972 {
18973   void **slot;
18974
18975   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
18976   if (!*slot)
18977     {
18978       offset_type len = VEC_length (offset_type, entry->cu_indices);
18979       offset_type val = MAYBE_SWAP (len);
18980       offset_type iter;
18981       int i;
18982
18983       *slot = entry;
18984       entry->index_offset = obstack_object_size (cpool);
18985
18986       obstack_grow (cpool, &val, sizeof (val));
18987       for (i = 0;
18988            VEC_iterate (offset_type, entry->cu_indices, i, iter);
18989            ++i)
18990         {
18991           val = MAYBE_SWAP (iter);
18992           obstack_grow (cpool, &val, sizeof (val));
18993         }
18994     }
18995   else
18996     {
18997       struct symtab_index_entry *old_entry = *slot;
18998       entry->index_offset = old_entry->index_offset;
18999       entry = old_entry;
19000     }
19001   return entry->index_offset;
19002 }
19003
19004 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
19005    constant pool entries going into the obstack CPOOL.  */
19006
19007 static void
19008 write_hash_table (struct mapped_symtab *symtab,
19009                   struct obstack *output, struct obstack *cpool)
19010 {
19011   offset_type i;
19012   htab_t symbol_hash_table;
19013   htab_t str_table;
19014
19015   symbol_hash_table = create_symbol_hash_table ();
19016   str_table = create_strtab ();
19017
19018   /* We add all the index vectors to the constant pool first, to
19019      ensure alignment is ok.  */
19020   for (i = 0; i < symtab->size; ++i)
19021     {
19022       if (symtab->data[i])
19023         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
19024     }
19025
19026   /* Now write out the hash table.  */
19027   for (i = 0; i < symtab->size; ++i)
19028     {
19029       offset_type str_off, vec_off;
19030
19031       if (symtab->data[i])
19032         {
19033           str_off = add_string (str_table, cpool, symtab->data[i]->name);
19034           vec_off = symtab->data[i]->index_offset;
19035         }
19036       else
19037         {
19038           /* While 0 is a valid constant pool index, it is not valid
19039              to have 0 for both offsets.  */
19040           str_off = 0;
19041           vec_off = 0;
19042         }
19043
19044       str_off = MAYBE_SWAP (str_off);
19045       vec_off = MAYBE_SWAP (vec_off);
19046
19047       obstack_grow (output, &str_off, sizeof (str_off));
19048       obstack_grow (output, &vec_off, sizeof (vec_off));
19049     }
19050
19051   htab_delete (str_table);
19052   htab_delete (symbol_hash_table);
19053 }
19054
19055 /* Struct to map psymtab to CU index in the index file.  */
19056 struct psymtab_cu_index_map
19057 {
19058   struct partial_symtab *psymtab;
19059   unsigned int cu_index;
19060 };
19061
19062 static hashval_t
19063 hash_psymtab_cu_index (const void *item)
19064 {
19065   const struct psymtab_cu_index_map *map = item;
19066
19067   return htab_hash_pointer (map->psymtab);
19068 }
19069
19070 static int
19071 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
19072 {
19073   const struct psymtab_cu_index_map *lhs = item_lhs;
19074   const struct psymtab_cu_index_map *rhs = item_rhs;
19075
19076   return lhs->psymtab == rhs->psymtab;
19077 }
19078
19079 /* Helper struct for building the address table.  */
19080 struct addrmap_index_data
19081 {
19082   struct objfile *objfile;
19083   struct obstack *addr_obstack;
19084   htab_t cu_index_htab;
19085
19086   /* Non-zero if the previous_* fields are valid.
19087      We can't write an entry until we see the next entry (since it is only then
19088      that we know the end of the entry).  */
19089   int previous_valid;
19090   /* Index of the CU in the table of all CUs in the index file.  */
19091   unsigned int previous_cu_index;
19092   /* Start address of the CU.  */
19093   CORE_ADDR previous_cu_start;
19094 };
19095
19096 /* Write an address entry to OBSTACK.  */
19097
19098 static void
19099 add_address_entry (struct objfile *objfile, struct obstack *obstack,
19100                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
19101 {
19102   offset_type cu_index_to_write;
19103   char addr[8];
19104   CORE_ADDR baseaddr;
19105
19106   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
19107
19108   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
19109   obstack_grow (obstack, addr, 8);
19110   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
19111   obstack_grow (obstack, addr, 8);
19112   cu_index_to_write = MAYBE_SWAP (cu_index);
19113   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
19114 }
19115
19116 /* Worker function for traversing an addrmap to build the address table.  */
19117
19118 static int
19119 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
19120 {
19121   struct addrmap_index_data *data = datap;
19122   struct partial_symtab *pst = obj;
19123
19124   if (data->previous_valid)
19125     add_address_entry (data->objfile, data->addr_obstack,
19126                        data->previous_cu_start, start_addr,
19127                        data->previous_cu_index);
19128
19129   data->previous_cu_start = start_addr;
19130   if (pst != NULL)
19131     {
19132       struct psymtab_cu_index_map find_map, *map;
19133       find_map.psymtab = pst;
19134       map = htab_find (data->cu_index_htab, &find_map);
19135       gdb_assert (map != NULL);
19136       data->previous_cu_index = map->cu_index;
19137       data->previous_valid = 1;
19138     }
19139   else
19140       data->previous_valid = 0;
19141
19142   return 0;
19143 }
19144
19145 /* Write OBJFILE's address map to OBSTACK.
19146    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
19147    in the index file.  */
19148
19149 static void
19150 write_address_map (struct objfile *objfile, struct obstack *obstack,
19151                    htab_t cu_index_htab)
19152 {
19153   struct addrmap_index_data addrmap_index_data;
19154
19155   /* When writing the address table, we have to cope with the fact that
19156      the addrmap iterator only provides the start of a region; we have to
19157      wait until the next invocation to get the start of the next region.  */
19158
19159   addrmap_index_data.objfile = objfile;
19160   addrmap_index_data.addr_obstack = obstack;
19161   addrmap_index_data.cu_index_htab = cu_index_htab;
19162   addrmap_index_data.previous_valid = 0;
19163
19164   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
19165                    &addrmap_index_data);
19166
19167   /* It's highly unlikely the last entry (end address = 0xff...ff)
19168      is valid, but we should still handle it.
19169      The end address is recorded as the start of the next region, but that
19170      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
19171      anyway.  */
19172   if (addrmap_index_data.previous_valid)
19173     add_address_entry (objfile, obstack,
19174                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
19175                        addrmap_index_data.previous_cu_index);
19176 }
19177
19178 /* Return the symbol kind of PSYM.  */
19179
19180 static gdb_index_symbol_kind
19181 symbol_kind (struct partial_symbol *psym)
19182 {
19183   domain_enum domain = PSYMBOL_DOMAIN (psym);
19184   enum address_class aclass = PSYMBOL_CLASS (psym);
19185
19186   switch (domain)
19187     {
19188     case VAR_DOMAIN:
19189       switch (aclass)
19190         {
19191         case LOC_BLOCK:
19192           return GDB_INDEX_SYMBOL_KIND_FUNCTION;
19193         case LOC_TYPEDEF:
19194           return GDB_INDEX_SYMBOL_KIND_TYPE;
19195         case LOC_COMPUTED:
19196         case LOC_CONST_BYTES:
19197         case LOC_OPTIMIZED_OUT:
19198         case LOC_STATIC:
19199           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19200         case LOC_CONST:
19201           /* Note: It's currently impossible to recognize psyms as enum values
19202              short of reading the type info.  For now punt.  */
19203           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
19204         default:
19205           /* There are other LOC_FOO values that one might want to classify
19206              as variables, but dwarf2read.c doesn't currently use them.  */
19207           return GDB_INDEX_SYMBOL_KIND_OTHER;
19208         }
19209     case STRUCT_DOMAIN:
19210       return GDB_INDEX_SYMBOL_KIND_TYPE;
19211     default:
19212       return GDB_INDEX_SYMBOL_KIND_OTHER;
19213     }
19214 }
19215
19216 /* Add a list of partial symbols to SYMTAB.  */
19217
19218 static void
19219 write_psymbols (struct mapped_symtab *symtab,
19220                 htab_t psyms_seen,
19221                 struct partial_symbol **psymp,
19222                 int count,
19223                 offset_type cu_index,
19224                 int is_static)
19225 {
19226   for (; count-- > 0; ++psymp)
19227     {
19228       struct partial_symbol *psym = *psymp;
19229       void **slot;
19230
19231       if (SYMBOL_LANGUAGE (psym) == language_ada)
19232         error (_("Ada is not currently supported by the index"));
19233
19234       /* Only add a given psymbol once.  */
19235       slot = htab_find_slot (psyms_seen, psym, INSERT);
19236       if (!*slot)
19237         {
19238           gdb_index_symbol_kind kind = symbol_kind (psym);
19239
19240           *slot = psym;
19241           add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
19242                            is_static, kind, cu_index);
19243         }
19244     }
19245 }
19246
19247 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
19248    exception if there is an error.  */
19249
19250 static void
19251 write_obstack (FILE *file, struct obstack *obstack)
19252 {
19253   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
19254               file)
19255       != obstack_object_size (obstack))
19256     error (_("couldn't data write to file"));
19257 }
19258
19259 /* Unlink a file if the argument is not NULL.  */
19260
19261 static void
19262 unlink_if_set (void *p)
19263 {
19264   char **filename = p;
19265   if (*filename)
19266     unlink (*filename);
19267 }
19268
19269 /* A helper struct used when iterating over debug_types.  */
19270 struct signatured_type_index_data
19271 {
19272   struct objfile *objfile;
19273   struct mapped_symtab *symtab;
19274   struct obstack *types_list;
19275   htab_t psyms_seen;
19276   int cu_index;
19277 };
19278
19279 /* A helper function that writes a single signatured_type to an
19280    obstack.  */
19281
19282 static int
19283 write_one_signatured_type (void **slot, void *d)
19284 {
19285   struct signatured_type_index_data *info = d;
19286   struct signatured_type *entry = (struct signatured_type *) *slot;
19287   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
19288   struct partial_symtab *psymtab = per_cu->v.psymtab;
19289   gdb_byte val[8];
19290
19291   write_psymbols (info->symtab,
19292                   info->psyms_seen,
19293                   info->objfile->global_psymbols.list
19294                   + psymtab->globals_offset,
19295                   psymtab->n_global_syms, info->cu_index,
19296                   0);
19297   write_psymbols (info->symtab,
19298                   info->psyms_seen,
19299                   info->objfile->static_psymbols.list
19300                   + psymtab->statics_offset,
19301                   psymtab->n_static_syms, info->cu_index,
19302                   1);
19303
19304   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19305                           entry->per_cu.offset.sect_off);
19306   obstack_grow (info->types_list, val, 8);
19307   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19308                           entry->type_offset_in_tu.cu_off);
19309   obstack_grow (info->types_list, val, 8);
19310   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
19311   obstack_grow (info->types_list, val, 8);
19312
19313   ++info->cu_index;
19314
19315   return 1;
19316 }
19317
19318 /* Recurse into all "included" dependencies and write their symbols as
19319    if they appeared in this psymtab.  */
19320
19321 static void
19322 recursively_write_psymbols (struct objfile *objfile,
19323                             struct partial_symtab *psymtab,
19324                             struct mapped_symtab *symtab,
19325                             htab_t psyms_seen,
19326                             offset_type cu_index)
19327 {
19328   int i;
19329
19330   for (i = 0; i < psymtab->number_of_dependencies; ++i)
19331     if (psymtab->dependencies[i]->user != NULL)
19332       recursively_write_psymbols (objfile, psymtab->dependencies[i],
19333                                   symtab, psyms_seen, cu_index);
19334
19335   write_psymbols (symtab,
19336                   psyms_seen,
19337                   objfile->global_psymbols.list + psymtab->globals_offset,
19338                   psymtab->n_global_syms, cu_index,
19339                   0);
19340   write_psymbols (symtab,
19341                   psyms_seen,
19342                   objfile->static_psymbols.list + psymtab->statics_offset,
19343                   psymtab->n_static_syms, cu_index,
19344                   1);
19345 }
19346
19347 /* Create an index file for OBJFILE in the directory DIR.  */
19348
19349 static void
19350 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
19351 {
19352   struct cleanup *cleanup;
19353   char *filename, *cleanup_filename;
19354   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
19355   struct obstack cu_list, types_cu_list;
19356   int i;
19357   FILE *out_file;
19358   struct mapped_symtab *symtab;
19359   offset_type val, size_of_contents, total_len;
19360   struct stat st;
19361   htab_t psyms_seen;
19362   htab_t cu_index_htab;
19363   struct psymtab_cu_index_map *psymtab_cu_index_map;
19364
19365   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
19366     return;
19367
19368   if (dwarf2_per_objfile->using_index)
19369     error (_("Cannot use an index to create the index"));
19370
19371   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
19372     error (_("Cannot make an index when the file has multiple .debug_types sections"));
19373
19374   if (stat (objfile->name, &st) < 0)
19375     perror_with_name (objfile->name);
19376
19377   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
19378                      INDEX_SUFFIX, (char *) NULL);
19379   cleanup = make_cleanup (xfree, filename);
19380
19381   out_file = fopen (filename, "wb");
19382   if (!out_file)
19383     error (_("Can't open `%s' for writing"), filename);
19384
19385   cleanup_filename = filename;
19386   make_cleanup (unlink_if_set, &cleanup_filename);
19387
19388   symtab = create_mapped_symtab ();
19389   make_cleanup (cleanup_mapped_symtab, symtab);
19390
19391   obstack_init (&addr_obstack);
19392   make_cleanup_obstack_free (&addr_obstack);
19393
19394   obstack_init (&cu_list);
19395   make_cleanup_obstack_free (&cu_list);
19396
19397   obstack_init (&types_cu_list);
19398   make_cleanup_obstack_free (&types_cu_list);
19399
19400   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
19401                                   NULL, xcalloc, xfree);
19402   make_cleanup_htab_delete (psyms_seen);
19403
19404   /* While we're scanning CU's create a table that maps a psymtab pointer
19405      (which is what addrmap records) to its index (which is what is recorded
19406      in the index file).  This will later be needed to write the address
19407      table.  */
19408   cu_index_htab = htab_create_alloc (100,
19409                                      hash_psymtab_cu_index,
19410                                      eq_psymtab_cu_index,
19411                                      NULL, xcalloc, xfree);
19412   make_cleanup_htab_delete (cu_index_htab);
19413   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
19414     xmalloc (sizeof (struct psymtab_cu_index_map)
19415              * dwarf2_per_objfile->n_comp_units);
19416   make_cleanup (xfree, psymtab_cu_index_map);
19417
19418   /* The CU list is already sorted, so we don't need to do additional
19419      work here.  Also, the debug_types entries do not appear in
19420      all_comp_units, but only in their own hash table.  */
19421   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
19422     {
19423       struct dwarf2_per_cu_data *per_cu
19424         = dwarf2_per_objfile->all_comp_units[i];
19425       struct partial_symtab *psymtab = per_cu->v.psymtab;
19426       gdb_byte val[8];
19427       struct psymtab_cu_index_map *map;
19428       void **slot;
19429
19430       if (psymtab->user == NULL)
19431         recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
19432
19433       map = &psymtab_cu_index_map[i];
19434       map->psymtab = psymtab;
19435       map->cu_index = i;
19436       slot = htab_find_slot (cu_index_htab, map, INSERT);
19437       gdb_assert (slot != NULL);
19438       gdb_assert (*slot == NULL);
19439       *slot = map;
19440
19441       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
19442                               per_cu->offset.sect_off);
19443       obstack_grow (&cu_list, val, 8);
19444       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
19445       obstack_grow (&cu_list, val, 8);
19446     }
19447
19448   /* Dump the address map.  */
19449   write_address_map (objfile, &addr_obstack, cu_index_htab);
19450
19451   /* Write out the .debug_type entries, if any.  */
19452   if (dwarf2_per_objfile->signatured_types)
19453     {
19454       struct signatured_type_index_data sig_data;
19455
19456       sig_data.objfile = objfile;
19457       sig_data.symtab = symtab;
19458       sig_data.types_list = &types_cu_list;
19459       sig_data.psyms_seen = psyms_seen;
19460       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
19461       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
19462                               write_one_signatured_type, &sig_data);
19463     }
19464
19465   /* Now that we've processed all symbols we can shrink their cu_indices
19466      lists.  */
19467   uniquify_cu_indices (symtab);
19468
19469   obstack_init (&constant_pool);
19470   make_cleanup_obstack_free (&constant_pool);
19471   obstack_init (&symtab_obstack);
19472   make_cleanup_obstack_free (&symtab_obstack);
19473   write_hash_table (symtab, &symtab_obstack, &constant_pool);
19474
19475   obstack_init (&contents);
19476   make_cleanup_obstack_free (&contents);
19477   size_of_contents = 6 * sizeof (offset_type);
19478   total_len = size_of_contents;
19479
19480   /* The version number.  */
19481   val = MAYBE_SWAP (7);
19482   obstack_grow (&contents, &val, sizeof (val));
19483
19484   /* The offset of the CU list from the start of the file.  */
19485   val = MAYBE_SWAP (total_len);
19486   obstack_grow (&contents, &val, sizeof (val));
19487   total_len += obstack_object_size (&cu_list);
19488
19489   /* The offset of the types CU list from the start of the file.  */
19490   val = MAYBE_SWAP (total_len);
19491   obstack_grow (&contents, &val, sizeof (val));
19492   total_len += obstack_object_size (&types_cu_list);
19493
19494   /* The offset of the address table from the start of the file.  */
19495   val = MAYBE_SWAP (total_len);
19496   obstack_grow (&contents, &val, sizeof (val));
19497   total_len += obstack_object_size (&addr_obstack);
19498
19499   /* The offset of the symbol table from the start of the file.  */
19500   val = MAYBE_SWAP (total_len);
19501   obstack_grow (&contents, &val, sizeof (val));
19502   total_len += obstack_object_size (&symtab_obstack);
19503
19504   /* The offset of the constant pool from the start of the file.  */
19505   val = MAYBE_SWAP (total_len);
19506   obstack_grow (&contents, &val, sizeof (val));
19507   total_len += obstack_object_size (&constant_pool);
19508
19509   gdb_assert (obstack_object_size (&contents) == size_of_contents);
19510
19511   write_obstack (out_file, &contents);
19512   write_obstack (out_file, &cu_list);
19513   write_obstack (out_file, &types_cu_list);
19514   write_obstack (out_file, &addr_obstack);
19515   write_obstack (out_file, &symtab_obstack);
19516   write_obstack (out_file, &constant_pool);
19517
19518   fclose (out_file);
19519
19520   /* We want to keep the file, so we set cleanup_filename to NULL
19521      here.  See unlink_if_set.  */
19522   cleanup_filename = NULL;
19523
19524   do_cleanups (cleanup);
19525 }
19526
19527 /* Implementation of the `save gdb-index' command.
19528    
19529    Note that the file format used by this command is documented in the
19530    GDB manual.  Any changes here must be documented there.  */
19531
19532 static void
19533 save_gdb_index_command (char *arg, int from_tty)
19534 {
19535   struct objfile *objfile;
19536
19537   if (!arg || !*arg)
19538     error (_("usage: save gdb-index DIRECTORY"));
19539
19540   ALL_OBJFILES (objfile)
19541   {
19542     struct stat st;
19543
19544     /* If the objfile does not correspond to an actual file, skip it.  */
19545     if (stat (objfile->name, &st) < 0)
19546       continue;
19547
19548     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
19549     if (dwarf2_per_objfile)
19550       {
19551         volatile struct gdb_exception except;
19552
19553         TRY_CATCH (except, RETURN_MASK_ERROR)
19554           {
19555             write_psymtabs_to_index (objfile, arg);
19556           }
19557         if (except.reason < 0)
19558           exception_fprintf (gdb_stderr, except,
19559                              _("Error while writing index for `%s': "),
19560                              objfile->name);
19561       }
19562   }
19563 }
19564
19565 \f
19566
19567 int dwarf2_always_disassemble;
19568
19569 static void
19570 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
19571                                 struct cmd_list_element *c, const char *value)
19572 {
19573   fprintf_filtered (file,
19574                     _("Whether to always disassemble "
19575                       "DWARF expressions is %s.\n"),
19576                     value);
19577 }
19578
19579 static void
19580 show_check_physname (struct ui_file *file, int from_tty,
19581                      struct cmd_list_element *c, const char *value)
19582 {
19583   fprintf_filtered (file,
19584                     _("Whether to check \"physname\" is %s.\n"),
19585                     value);
19586 }
19587
19588 void _initialize_dwarf2_read (void);
19589
19590 void
19591 _initialize_dwarf2_read (void)
19592 {
19593   struct cmd_list_element *c;
19594
19595   dwarf2_objfile_data_key
19596     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
19597
19598   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
19599 Set DWARF 2 specific variables.\n\
19600 Configure DWARF 2 variables such as the cache size"),
19601                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
19602                   0/*allow-unknown*/, &maintenance_set_cmdlist);
19603
19604   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
19605 Show DWARF 2 specific variables\n\
19606 Show DWARF 2 variables such as the cache size"),
19607                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
19608                   0/*allow-unknown*/, &maintenance_show_cmdlist);
19609
19610   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
19611                             &dwarf2_max_cache_age, _("\
19612 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
19613 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
19614 A higher limit means that cached compilation units will be stored\n\
19615 in memory longer, and more total memory will be used.  Zero disables\n\
19616 caching, which can slow down startup."),
19617                             NULL,
19618                             show_dwarf2_max_cache_age,
19619                             &set_dwarf2_cmdlist,
19620                             &show_dwarf2_cmdlist);
19621
19622   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
19623                            &dwarf2_always_disassemble, _("\
19624 Set whether `info address' always disassembles DWARF expressions."), _("\
19625 Show whether `info address' always disassembles DWARF expressions."), _("\
19626 When enabled, DWARF expressions are always printed in an assembly-like\n\
19627 syntax.  When disabled, expressions will be printed in a more\n\
19628 conversational style, when possible."),
19629                            NULL,
19630                            show_dwarf2_always_disassemble,
19631                            &set_dwarf2_cmdlist,
19632                            &show_dwarf2_cmdlist);
19633
19634   add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
19635 Set debugging of the dwarf2 reader."), _("\
19636 Show debugging of the dwarf2 reader."), _("\
19637 When enabled, debugging messages are printed during dwarf2 reading\n\
19638 and symtab expansion."),
19639                             NULL,
19640                             NULL,
19641                             &setdebuglist, &showdebuglist);
19642
19643   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
19644 Set debugging of the dwarf2 DIE reader."), _("\
19645 Show debugging of the dwarf2 DIE reader."), _("\
19646 When enabled (non-zero), DIEs are dumped after they are read in.\n\
19647 The value is the maximum depth to print."),
19648                             NULL,
19649                             NULL,
19650                             &setdebuglist, &showdebuglist);
19651
19652   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
19653 Set cross-checking of \"physname\" code against demangler."), _("\
19654 Show cross-checking of \"physname\" code against demangler."), _("\
19655 When enabled, GDB's internal \"physname\" code is checked against\n\
19656 the demangler."),
19657                            NULL, show_check_physname,
19658                            &setdebuglist, &showdebuglist);
19659
19660   add_setshow_boolean_cmd ("use-deprecated-index-sections",
19661                            no_class, &use_deprecated_index_sections, _("\
19662 Set whether to use deprecated gdb_index sections."), _("\
19663 Show whether to use deprecated gdb_index sections."), _("\
19664 When enabled, deprecated .gdb_index sections are used anyway.\n\
19665 Normally they are ignored either because of a missing feature or\n\
19666 performance issue.\n\
19667 Warning: This option must be enabled before gdb reads the file."),
19668                            NULL,
19669                            NULL,
19670                            &setlist, &showlist);
19671
19672   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
19673                _("\
19674 Save a gdb-index file.\n\
19675 Usage: save gdb-index DIRECTORY"),
19676                &save_cmdlist);
19677   set_cmd_completer (c, filename_completer);
19678 }