gdb/
[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 unsigned 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   flagword aflag = bfd_get_section_flags (abfd, sectp);
1714
1715   if (vnames == NULL)
1716     names = &dwarf2_elf_names;
1717   else
1718     names = (const struct dwarf2_debug_sections *) vnames;
1719
1720   if ((aflag & SEC_HAS_CONTENTS) == 0)
1721     {
1722     }
1723   else if (section_is_p (sectp->name, &names->info))
1724     {
1725       dwarf2_per_objfile->info.asection = sectp;
1726       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1727     }
1728   else if (section_is_p (sectp->name, &names->abbrev))
1729     {
1730       dwarf2_per_objfile->abbrev.asection = sectp;
1731       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1732     }
1733   else if (section_is_p (sectp->name, &names->line))
1734     {
1735       dwarf2_per_objfile->line.asection = sectp;
1736       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1737     }
1738   else if (section_is_p (sectp->name, &names->loc))
1739     {
1740       dwarf2_per_objfile->loc.asection = sectp;
1741       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1742     }
1743   else if (section_is_p (sectp->name, &names->macinfo))
1744     {
1745       dwarf2_per_objfile->macinfo.asection = sectp;
1746       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1747     }
1748   else if (section_is_p (sectp->name, &names->macro))
1749     {
1750       dwarf2_per_objfile->macro.asection = sectp;
1751       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1752     }
1753   else if (section_is_p (sectp->name, &names->str))
1754     {
1755       dwarf2_per_objfile->str.asection = sectp;
1756       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1757     }
1758   else if (section_is_p (sectp->name, &names->addr))
1759     {
1760       dwarf2_per_objfile->addr.asection = sectp;
1761       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1762     }
1763   else if (section_is_p (sectp->name, &names->frame))
1764     {
1765       dwarf2_per_objfile->frame.asection = sectp;
1766       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1767     }
1768   else if (section_is_p (sectp->name, &names->eh_frame))
1769     {
1770       dwarf2_per_objfile->eh_frame.asection = sectp;
1771       dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1772     }
1773   else if (section_is_p (sectp->name, &names->ranges))
1774     {
1775       dwarf2_per_objfile->ranges.asection = sectp;
1776       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1777     }
1778   else if (section_is_p (sectp->name, &names->types))
1779     {
1780       struct dwarf2_section_info type_section;
1781
1782       memset (&type_section, 0, sizeof (type_section));
1783       type_section.asection = sectp;
1784       type_section.size = bfd_get_section_size (sectp);
1785
1786       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1787                      &type_section);
1788     }
1789   else if (section_is_p (sectp->name, &names->gdb_index))
1790     {
1791       dwarf2_per_objfile->gdb_index.asection = sectp;
1792       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1793     }
1794
1795   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1796       && bfd_section_vma (abfd, sectp) == 0)
1797     dwarf2_per_objfile->has_section_at_zero = 1;
1798 }
1799
1800 /* A helper function that decides whether a section is empty,
1801    or not present.  */
1802
1803 static int
1804 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1805 {
1806   return info->asection == NULL || info->size == 0;
1807 }
1808
1809 /* Read the contents of the section INFO.
1810    OBJFILE is the main object file, but not necessarily the file where
1811    the section comes from.  E.g., for DWO files INFO->asection->owner
1812    is the bfd of the DWO file.
1813    If the section is compressed, uncompress it before returning.  */
1814
1815 static void
1816 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1817 {
1818   asection *sectp = info->asection;
1819   bfd *abfd;
1820   gdb_byte *buf, *retbuf;
1821   unsigned char header[4];
1822
1823   if (info->readin)
1824     return;
1825   info->buffer = NULL;
1826   info->readin = 1;
1827
1828   if (dwarf2_section_empty_p (info))
1829     return;
1830
1831   abfd = sectp->owner;
1832
1833   /* If the section has relocations, we must read it ourselves.
1834      Otherwise we attach it to the BFD.  */
1835   if ((sectp->flags & SEC_RELOC) == 0)
1836     {
1837       const gdb_byte *bytes = gdb_bfd_map_section (sectp, &info->size);
1838
1839       /* We have to cast away const here for historical reasons.
1840          Fixing dwarf2read to be const-correct would be quite nice.  */
1841       info->buffer = (gdb_byte *) bytes;
1842       return;
1843     }
1844
1845   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1846   info->buffer = buf;
1847
1848   /* When debugging .o files, we may need to apply relocations; see
1849      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1850      We never compress sections in .o files, so we only need to
1851      try this when the section is not compressed.  */
1852   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1853   if (retbuf != NULL)
1854     {
1855       info->buffer = retbuf;
1856       return;
1857     }
1858
1859   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1860       || bfd_bread (buf, info->size, abfd) != info->size)
1861     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1862            bfd_get_filename (abfd));
1863 }
1864
1865 /* A helper function that returns the size of a section in a safe way.
1866    If you are positive that the section has been read before using the
1867    size, then it is safe to refer to the dwarf2_section_info object's
1868    "size" field directly.  In other cases, you must call this
1869    function, because for compressed sections the size field is not set
1870    correctly until the section has been read.  */
1871
1872 static bfd_size_type
1873 dwarf2_section_size (struct objfile *objfile,
1874                      struct dwarf2_section_info *info)
1875 {
1876   if (!info->readin)
1877     dwarf2_read_section (objfile, info);
1878   return info->size;
1879 }
1880
1881 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1882    SECTION_NAME.  */
1883
1884 void
1885 dwarf2_get_section_info (struct objfile *objfile,
1886                          enum dwarf2_section_enum sect,
1887                          asection **sectp, gdb_byte **bufp,
1888                          bfd_size_type *sizep)
1889 {
1890   struct dwarf2_per_objfile *data
1891     = objfile_data (objfile, dwarf2_objfile_data_key);
1892   struct dwarf2_section_info *info;
1893
1894   /* We may see an objfile without any DWARF, in which case we just
1895      return nothing.  */
1896   if (data == NULL)
1897     {
1898       *sectp = NULL;
1899       *bufp = NULL;
1900       *sizep = 0;
1901       return;
1902     }
1903   switch (sect)
1904     {
1905     case DWARF2_DEBUG_FRAME:
1906       info = &data->frame;
1907       break;
1908     case DWARF2_EH_FRAME:
1909       info = &data->eh_frame;
1910       break;
1911     default:
1912       gdb_assert_not_reached ("unexpected section");
1913     }
1914
1915   dwarf2_read_section (objfile, info);
1916
1917   *sectp = info->asection;
1918   *bufp = info->buffer;
1919   *sizep = info->size;
1920 }
1921
1922 /* A helper function to find the sections for a .dwz file.  */
1923
1924 static void
1925 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1926 {
1927   struct dwz_file *dwz_file = arg;
1928
1929   /* Note that we only support the standard ELF names, because .dwz
1930      is ELF-only (at the time of writing).  */
1931   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1932     {
1933       dwz_file->abbrev.asection = sectp;
1934       dwz_file->abbrev.size = bfd_get_section_size (sectp);
1935     }
1936   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1937     {
1938       dwz_file->info.asection = sectp;
1939       dwz_file->info.size = bfd_get_section_size (sectp);
1940     }
1941   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1942     {
1943       dwz_file->str.asection = sectp;
1944       dwz_file->str.size = bfd_get_section_size (sectp);
1945     }
1946   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1947     {
1948       dwz_file->line.asection = sectp;
1949       dwz_file->line.size = bfd_get_section_size (sectp);
1950     }
1951   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1952     {
1953       dwz_file->macro.asection = sectp;
1954       dwz_file->macro.size = bfd_get_section_size (sectp);
1955     }
1956   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1957     {
1958       dwz_file->gdb_index.asection = sectp;
1959       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
1960     }
1961 }
1962
1963 /* Open the separate '.dwz' debug file, if needed.  Error if the file
1964    cannot be found.  */
1965
1966 static struct dwz_file *
1967 dwarf2_get_dwz_file (void)
1968 {
1969   bfd *abfd, *dwz_bfd;
1970   asection *section;
1971   gdb_byte *data;
1972   struct cleanup *cleanup;
1973   const char *filename;
1974   struct dwz_file *result;
1975
1976   if (dwarf2_per_objfile->dwz_file != NULL)
1977     return dwarf2_per_objfile->dwz_file;
1978
1979   abfd = dwarf2_per_objfile->objfile->obfd;
1980   section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
1981   if (section == NULL)
1982     error (_("could not find '.gnu_debugaltlink' section"));
1983   if (!bfd_malloc_and_get_section (abfd, section, &data))
1984     error (_("could not read '.gnu_debugaltlink' section: %s"),
1985            bfd_errmsg (bfd_get_error ()));
1986   cleanup = make_cleanup (xfree, data);
1987
1988   filename = data;
1989   if (!IS_ABSOLUTE_PATH (filename))
1990     {
1991       char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
1992       char *rel;
1993
1994       make_cleanup (xfree, abs);
1995       abs = ldirname (abs);
1996       make_cleanup (xfree, abs);
1997
1998       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
1999       make_cleanup (xfree, rel);
2000       filename = rel;
2001     }
2002
2003   /* The format is just a NUL-terminated file name, followed by the
2004      build-id.  For now, though, we ignore the build-id.  */
2005   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2006   if (dwz_bfd == NULL)
2007     error (_("could not read '%s': %s"), filename,
2008            bfd_errmsg (bfd_get_error ()));
2009
2010   if (!bfd_check_format (dwz_bfd, bfd_object))
2011     {
2012       gdb_bfd_unref (dwz_bfd);
2013       error (_("file '%s' was not usable: %s"), filename,
2014              bfd_errmsg (bfd_get_error ()));
2015     }
2016
2017   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2018                            struct dwz_file);
2019   result->dwz_bfd = dwz_bfd;
2020
2021   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2022
2023   do_cleanups (cleanup);
2024
2025   return result;
2026 }
2027 \f
2028 /* DWARF quick_symbols_functions support.  */
2029
2030 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2031    unique line tables, so we maintain a separate table of all .debug_line
2032    derived entries to support the sharing.
2033    All the quick functions need is the list of file names.  We discard the
2034    line_header when we're done and don't need to record it here.  */
2035 struct quick_file_names
2036 {
2037   /* The data used to construct the hash key.  */
2038   struct stmt_list_hash hash;
2039
2040   /* The number of entries in file_names, real_names.  */
2041   unsigned int num_file_names;
2042
2043   /* The file names from the line table, after being run through
2044      file_full_name.  */
2045   const char **file_names;
2046
2047   /* The file names from the line table after being run through
2048      gdb_realpath.  These are computed lazily.  */
2049   const char **real_names;
2050 };
2051
2052 /* When using the index (and thus not using psymtabs), each CU has an
2053    object of this type.  This is used to hold information needed by
2054    the various "quick" methods.  */
2055 struct dwarf2_per_cu_quick_data
2056 {
2057   /* The file table.  This can be NULL if there was no file table
2058      or it's currently not read in.
2059      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
2060   struct quick_file_names *file_names;
2061
2062   /* The corresponding symbol table.  This is NULL if symbols for this
2063      CU have not yet been read.  */
2064   struct symtab *symtab;
2065
2066   /* A temporary mark bit used when iterating over all CUs in
2067      expand_symtabs_matching.  */
2068   unsigned int mark : 1;
2069
2070   /* True if we've tried to read the file table and found there isn't one.
2071      There will be no point in trying to read it again next time.  */
2072   unsigned int no_file_data : 1;
2073 };
2074
2075 /* Utility hash function for a stmt_list_hash.  */
2076
2077 static hashval_t
2078 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2079 {
2080   hashval_t v = 0;
2081
2082   if (stmt_list_hash->dwo_unit != NULL)
2083     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2084   v += stmt_list_hash->line_offset.sect_off;
2085   return v;
2086 }
2087
2088 /* Utility equality function for a stmt_list_hash.  */
2089
2090 static int
2091 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2092                     const struct stmt_list_hash *rhs)
2093 {
2094   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2095     return 0;
2096   if (lhs->dwo_unit != NULL
2097       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2098     return 0;
2099
2100   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2101 }
2102
2103 /* Hash function for a quick_file_names.  */
2104
2105 static hashval_t
2106 hash_file_name_entry (const void *e)
2107 {
2108   const struct quick_file_names *file_data = e;
2109
2110   return hash_stmt_list_entry (&file_data->hash);
2111 }
2112
2113 /* Equality function for a quick_file_names.  */
2114
2115 static int
2116 eq_file_name_entry (const void *a, const void *b)
2117 {
2118   const struct quick_file_names *ea = a;
2119   const struct quick_file_names *eb = b;
2120
2121   return eq_stmt_list_entry (&ea->hash, &eb->hash);
2122 }
2123
2124 /* Delete function for a quick_file_names.  */
2125
2126 static void
2127 delete_file_name_entry (void *e)
2128 {
2129   struct quick_file_names *file_data = e;
2130   int i;
2131
2132   for (i = 0; i < file_data->num_file_names; ++i)
2133     {
2134       xfree ((void*) file_data->file_names[i]);
2135       if (file_data->real_names)
2136         xfree ((void*) file_data->real_names[i]);
2137     }
2138
2139   /* The space for the struct itself lives on objfile_obstack,
2140      so we don't free it here.  */
2141 }
2142
2143 /* Create a quick_file_names hash table.  */
2144
2145 static htab_t
2146 create_quick_file_names_table (unsigned int nr_initial_entries)
2147 {
2148   return htab_create_alloc (nr_initial_entries,
2149                             hash_file_name_entry, eq_file_name_entry,
2150                             delete_file_name_entry, xcalloc, xfree);
2151 }
2152
2153 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
2154    have to be created afterwards.  You should call age_cached_comp_units after
2155    processing PER_CU->CU.  dw2_setup must have been already called.  */
2156
2157 static void
2158 load_cu (struct dwarf2_per_cu_data *per_cu)
2159 {
2160   if (per_cu->is_debug_types)
2161     load_full_type_unit (per_cu);
2162   else
2163     load_full_comp_unit (per_cu, language_minimal);
2164
2165   gdb_assert (per_cu->cu != NULL);
2166
2167   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2168 }
2169
2170 /* Read in the symbols for PER_CU.  */
2171
2172 static void
2173 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2174 {
2175   struct cleanup *back_to;
2176
2177   /* Skip type_unit_groups, reading the type units they contain
2178      is handled elsewhere.  */
2179   if (IS_TYPE_UNIT_GROUP (per_cu))
2180     return;
2181
2182   back_to = make_cleanup (dwarf2_release_queue, NULL);
2183
2184   if (dwarf2_per_objfile->using_index
2185       ? per_cu->v.quick->symtab == NULL
2186       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2187     {
2188       queue_comp_unit (per_cu, language_minimal);
2189       load_cu (per_cu);
2190     }
2191
2192   process_queue ();
2193
2194   /* Age the cache, releasing compilation units that have not
2195      been used recently.  */
2196   age_cached_comp_units ();
2197
2198   do_cleanups (back_to);
2199 }
2200
2201 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
2202    the objfile from which this CU came.  Returns the resulting symbol
2203    table.  */
2204
2205 static struct symtab *
2206 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2207 {
2208   gdb_assert (dwarf2_per_objfile->using_index);
2209   if (!per_cu->v.quick->symtab)
2210     {
2211       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2212       increment_reading_symtab ();
2213       dw2_do_instantiate_symtab (per_cu);
2214       process_cu_includes ();
2215       do_cleanups (back_to);
2216     }
2217   return per_cu->v.quick->symtab;
2218 }
2219
2220 /* Return the CU given its index.
2221
2222    This is intended for loops like:
2223
2224    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2225                     + dwarf2_per_objfile->n_type_units); ++i)
2226      {
2227        struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2228
2229        ...;
2230      }
2231 */
2232
2233 static struct dwarf2_per_cu_data *
2234 dw2_get_cu (int index)
2235 {
2236   if (index >= dwarf2_per_objfile->n_comp_units)
2237     {
2238       index -= dwarf2_per_objfile->n_comp_units;
2239       gdb_assert (index < dwarf2_per_objfile->n_type_units);
2240       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2241     }
2242
2243   return dwarf2_per_objfile->all_comp_units[index];
2244 }
2245
2246 /* Return the primary CU given its index.
2247    The difference between this function and dw2_get_cu is in the handling
2248    of type units (TUs).  Here we return the type_unit_group object.
2249
2250    This is intended for loops like:
2251
2252    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2253                     + dwarf2_per_objfile->n_type_unit_groups); ++i)
2254      {
2255        struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2256
2257        ...;
2258      }
2259 */
2260
2261 static struct dwarf2_per_cu_data *
2262 dw2_get_primary_cu (int index)
2263 {
2264   if (index >= dwarf2_per_objfile->n_comp_units)
2265     {
2266       index -= dwarf2_per_objfile->n_comp_units;
2267       gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2268       return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2269     }
2270
2271   return dwarf2_per_objfile->all_comp_units[index];
2272 }
2273
2274 /* A helper function that knows how to read a 64-bit value in a way
2275    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
2276    otherwise.  */
2277
2278 static int
2279 extract_cu_value (const char *bytes, ULONGEST *result)
2280 {
2281   if (sizeof (ULONGEST) < 8)
2282     {
2283       int i;
2284
2285       /* Ignore the upper 4 bytes if they are all zero.  */
2286       for (i = 0; i < 4; ++i)
2287         if (bytes[i + 4] != 0)
2288           return 0;
2289
2290       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2291     }
2292   else
2293     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2294   return 1;
2295 }
2296
2297 /* A helper for create_cus_from_index that handles a given list of
2298    CUs.  */
2299
2300 static int
2301 create_cus_from_index_list (struct objfile *objfile,
2302                             const gdb_byte *cu_list, offset_type n_elements,
2303                             struct dwarf2_section_info *section,
2304                             int is_dwz,
2305                             int base_offset)
2306 {
2307   offset_type i;
2308
2309   for (i = 0; i < n_elements; i += 2)
2310     {
2311       struct dwarf2_per_cu_data *the_cu;
2312       ULONGEST offset, length;
2313
2314       if (!extract_cu_value (cu_list, &offset)
2315           || !extract_cu_value (cu_list + 8, &length))
2316         return 0;
2317       cu_list += 2 * 8;
2318
2319       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2320                                struct dwarf2_per_cu_data);
2321       the_cu->offset.sect_off = offset;
2322       the_cu->length = length;
2323       the_cu->objfile = objfile;
2324       the_cu->info_or_types_section = section;
2325       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2326                                         struct dwarf2_per_cu_quick_data);
2327       the_cu->is_dwz = is_dwz;
2328       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2329     }
2330
2331   return 1;
2332 }
2333
2334 /* Read the CU list from the mapped index, and use it to create all
2335    the CU objects for this objfile.  Return 0 if something went wrong,
2336    1 if everything went ok.  */
2337
2338 static int
2339 create_cus_from_index (struct objfile *objfile,
2340                        const gdb_byte *cu_list, offset_type cu_list_elements,
2341                        const gdb_byte *dwz_list, offset_type dwz_elements)
2342 {
2343   struct dwz_file *dwz;
2344
2345   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2346   dwarf2_per_objfile->all_comp_units
2347     = obstack_alloc (&objfile->objfile_obstack,
2348                      dwarf2_per_objfile->n_comp_units
2349                      * sizeof (struct dwarf2_per_cu_data *));
2350
2351   if (!create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2352                                    &dwarf2_per_objfile->info, 0, 0))
2353     return 0;
2354
2355   if (dwz_elements == 0)
2356     return 1;
2357
2358   dwz = dwarf2_get_dwz_file ();
2359   return create_cus_from_index_list (objfile, dwz_list, dwz_elements,
2360                                      &dwz->info, 1, cu_list_elements / 2);
2361 }
2362
2363 /* Create the signatured type hash table from the index.  */
2364
2365 static int
2366 create_signatured_type_table_from_index (struct objfile *objfile,
2367                                          struct dwarf2_section_info *section,
2368                                          const gdb_byte *bytes,
2369                                          offset_type elements)
2370 {
2371   offset_type i;
2372   htab_t sig_types_hash;
2373
2374   dwarf2_per_objfile->n_type_units = elements / 3;
2375   dwarf2_per_objfile->all_type_units
2376     = obstack_alloc (&objfile->objfile_obstack,
2377                      dwarf2_per_objfile->n_type_units
2378                      * sizeof (struct signatured_type *));
2379
2380   sig_types_hash = allocate_signatured_type_table (objfile);
2381
2382   for (i = 0; i < elements; i += 3)
2383     {
2384       struct signatured_type *sig_type;
2385       ULONGEST offset, type_offset_in_tu, signature;
2386       void **slot;
2387
2388       if (!extract_cu_value (bytes, &offset)
2389           || !extract_cu_value (bytes + 8, &type_offset_in_tu))
2390         return 0;
2391       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2392       bytes += 3 * 8;
2393
2394       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2395                                  struct signatured_type);
2396       sig_type->signature = signature;
2397       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2398       sig_type->per_cu.is_debug_types = 1;
2399       sig_type->per_cu.info_or_types_section = section;
2400       sig_type->per_cu.offset.sect_off = offset;
2401       sig_type->per_cu.objfile = objfile;
2402       sig_type->per_cu.v.quick
2403         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2404                           struct dwarf2_per_cu_quick_data);
2405
2406       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2407       *slot = sig_type;
2408
2409       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2410     }
2411
2412   dwarf2_per_objfile->signatured_types = sig_types_hash;
2413
2414   return 1;
2415 }
2416
2417 /* Read the address map data from the mapped index, and use it to
2418    populate the objfile's psymtabs_addrmap.  */
2419
2420 static void
2421 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2422 {
2423   const gdb_byte *iter, *end;
2424   struct obstack temp_obstack;
2425   struct addrmap *mutable_map;
2426   struct cleanup *cleanup;
2427   CORE_ADDR baseaddr;
2428
2429   obstack_init (&temp_obstack);
2430   cleanup = make_cleanup_obstack_free (&temp_obstack);
2431   mutable_map = addrmap_create_mutable (&temp_obstack);
2432
2433   iter = index->address_table;
2434   end = iter + index->address_table_size;
2435
2436   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2437
2438   while (iter < end)
2439     {
2440       ULONGEST hi, lo, cu_index;
2441       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2442       iter += 8;
2443       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2444       iter += 8;
2445       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2446       iter += 4;
2447       
2448       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2449                          dw2_get_cu (cu_index));
2450     }
2451
2452   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2453                                                     &objfile->objfile_obstack);
2454   do_cleanups (cleanup);
2455 }
2456
2457 /* The hash function for strings in the mapped index.  This is the same as
2458    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2459    implementation.  This is necessary because the hash function is tied to the
2460    format of the mapped index file.  The hash values do not have to match with
2461    SYMBOL_HASH_NEXT.
2462    
2463    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2464
2465 static hashval_t
2466 mapped_index_string_hash (int index_version, const void *p)
2467 {
2468   const unsigned char *str = (const unsigned char *) p;
2469   hashval_t r = 0;
2470   unsigned char c;
2471
2472   while ((c = *str++) != 0)
2473     {
2474       if (index_version >= 5)
2475         c = tolower (c);
2476       r = r * 67 + c - 113;
2477     }
2478
2479   return r;
2480 }
2481
2482 /* Find a slot in the mapped index INDEX for the object named NAME.
2483    If NAME is found, set *VEC_OUT to point to the CU vector in the
2484    constant pool and return 1.  If NAME cannot be found, return 0.  */
2485
2486 static int
2487 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2488                           offset_type **vec_out)
2489 {
2490   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2491   offset_type hash;
2492   offset_type slot, step;
2493   int (*cmp) (const char *, const char *);
2494
2495   if (current_language->la_language == language_cplus
2496       || current_language->la_language == language_java
2497       || current_language->la_language == language_fortran)
2498     {
2499       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2500          not contain any.  */
2501       const char *paren = strchr (name, '(');
2502
2503       if (paren)
2504         {
2505           char *dup;
2506
2507           dup = xmalloc (paren - name + 1);
2508           memcpy (dup, name, paren - name);
2509           dup[paren - name] = 0;
2510
2511           make_cleanup (xfree, dup);
2512           name = dup;
2513         }
2514     }
2515
2516   /* Index version 4 did not support case insensitive searches.  But the
2517      indices for case insensitive languages are built in lowercase, therefore
2518      simulate our NAME being searched is also lowercased.  */
2519   hash = mapped_index_string_hash ((index->version == 4
2520                                     && case_sensitivity == case_sensitive_off
2521                                     ? 5 : index->version),
2522                                    name);
2523
2524   slot = hash & (index->symbol_table_slots - 1);
2525   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2526   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2527
2528   for (;;)
2529     {
2530       /* Convert a slot number to an offset into the table.  */
2531       offset_type i = 2 * slot;
2532       const char *str;
2533       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2534         {
2535           do_cleanups (back_to);
2536           return 0;
2537         }
2538
2539       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2540       if (!cmp (name, str))
2541         {
2542           *vec_out = (offset_type *) (index->constant_pool
2543                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2544           do_cleanups (back_to);
2545           return 1;
2546         }
2547
2548       slot = (slot + step) & (index->symbol_table_slots - 1);
2549     }
2550 }
2551
2552 /* A helper function that reads the .gdb_index from SECTION and fills
2553    in MAP.  FILENAME is the name of the file containing the section;
2554    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
2555    ok to use deprecated sections.
2556
2557    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2558    out parameters that are filled in with information about the CU and
2559    TU lists in the section.
2560
2561    Returns 1 if all went well, 0 otherwise.  */
2562
2563 static int
2564 read_index_from_section (struct objfile *objfile,
2565                          const char *filename,
2566                          int deprecated_ok,
2567                          struct dwarf2_section_info *section,
2568                          struct mapped_index *map,
2569                          const gdb_byte **cu_list,
2570                          offset_type *cu_list_elements,
2571                          const gdb_byte **types_list,
2572                          offset_type *types_list_elements)
2573 {
2574   char *addr;
2575   offset_type version;
2576   offset_type *metadata;
2577   int i;
2578
2579   if (dwarf2_section_empty_p (section))
2580     return 0;
2581
2582   /* Older elfutils strip versions could keep the section in the main
2583      executable while splitting it for the separate debug info file.  */
2584   if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2585     return 0;
2586
2587   dwarf2_read_section (objfile, section);
2588
2589   addr = section->buffer;
2590   /* Version check.  */
2591   version = MAYBE_SWAP (*(offset_type *) addr);
2592   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2593      causes the index to behave very poorly for certain requests.  Version 3
2594      contained incomplete addrmap.  So, it seems better to just ignore such
2595      indices.  */
2596   if (version < 4)
2597     {
2598       static int warning_printed = 0;
2599       if (!warning_printed)
2600         {
2601           warning (_("Skipping obsolete .gdb_index section in %s."),
2602                    filename);
2603           warning_printed = 1;
2604         }
2605       return 0;
2606     }
2607   /* Index version 4 uses a different hash function than index version
2608      5 and later.
2609
2610      Versions earlier than 6 did not emit psymbols for inlined
2611      functions.  Using these files will cause GDB not to be able to
2612      set breakpoints on inlined functions by name, so we ignore these
2613      indices unless the user has done
2614      "set use-deprecated-index-sections on".  */
2615   if (version < 6 && !deprecated_ok)
2616     {
2617       static int warning_printed = 0;
2618       if (!warning_printed)
2619         {
2620           warning (_("\
2621 Skipping deprecated .gdb_index section in %s.\n\
2622 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2623 to use the section anyway."),
2624                    filename);
2625           warning_printed = 1;
2626         }
2627       return 0;
2628     }
2629   /* Indexes with higher version than the one supported by GDB may be no
2630      longer backward compatible.  */
2631   if (version > 7)
2632     return 0;
2633
2634   map->version = version;
2635   map->total_size = section->size;
2636
2637   metadata = (offset_type *) (addr + sizeof (offset_type));
2638
2639   i = 0;
2640   *cu_list = addr + MAYBE_SWAP (metadata[i]);
2641   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2642                        / 8);
2643   ++i;
2644
2645   *types_list = addr + MAYBE_SWAP (metadata[i]);
2646   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2647                            - MAYBE_SWAP (metadata[i]))
2648                           / 8);
2649   ++i;
2650
2651   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2652   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2653                              - MAYBE_SWAP (metadata[i]));
2654   ++i;
2655
2656   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2657   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2658                               - MAYBE_SWAP (metadata[i]))
2659                              / (2 * sizeof (offset_type)));
2660   ++i;
2661
2662   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2663
2664   return 1;
2665 }
2666
2667
2668 /* Read the index file.  If everything went ok, initialize the "quick"
2669    elements of all the CUs and return 1.  Otherwise, return 0.  */
2670
2671 static int
2672 dwarf2_read_index (struct objfile *objfile)
2673 {
2674   struct mapped_index local_map, *map;
2675   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2676   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2677
2678   if (!read_index_from_section (objfile, objfile->name,
2679                                 use_deprecated_index_sections,
2680                                 &dwarf2_per_objfile->gdb_index, &local_map,
2681                                 &cu_list, &cu_list_elements,
2682                                 &types_list, &types_list_elements))
2683     return 0;
2684
2685   /* Don't use the index if it's empty.  */
2686   if (local_map.symbol_table_slots == 0)
2687     return 0;
2688
2689   /* If there is a .dwz file, read it so we can get its CU list as
2690      well.  */
2691   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2692     {
2693       struct dwz_file *dwz = dwarf2_get_dwz_file ();
2694       struct mapped_index dwz_map;
2695       const gdb_byte *dwz_types_ignore;
2696       offset_type dwz_types_elements_ignore;
2697
2698       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2699                                     1,
2700                                     &dwz->gdb_index, &dwz_map,
2701                                     &dwz_list, &dwz_list_elements,
2702                                     &dwz_types_ignore,
2703                                     &dwz_types_elements_ignore))
2704         {
2705           warning (_("could not read '.gdb_index' section from %s; skipping"),
2706                    bfd_get_filename (dwz->dwz_bfd));
2707           return 0;
2708         }
2709     }
2710
2711   if (!create_cus_from_index (objfile, cu_list, cu_list_elements,
2712                               dwz_list, dwz_list_elements))
2713     return 0;
2714
2715   if (types_list_elements)
2716     {
2717       struct dwarf2_section_info *section;
2718
2719       /* We can only handle a single .debug_types when we have an
2720          index.  */
2721       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2722         return 0;
2723
2724       section = VEC_index (dwarf2_section_info_def,
2725                            dwarf2_per_objfile->types, 0);
2726
2727       if (!create_signatured_type_table_from_index (objfile, section,
2728                                                     types_list,
2729                                                     types_list_elements))
2730         return 0;
2731     }
2732
2733   create_addrmap_from_index (objfile, &local_map);
2734
2735   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2736   *map = local_map;
2737
2738   dwarf2_per_objfile->index_table = map;
2739   dwarf2_per_objfile->using_index = 1;
2740   dwarf2_per_objfile->quick_file_names_table =
2741     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2742
2743   return 1;
2744 }
2745
2746 /* A helper for the "quick" functions which sets the global
2747    dwarf2_per_objfile according to OBJFILE.  */
2748
2749 static void
2750 dw2_setup (struct objfile *objfile)
2751 {
2752   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2753   gdb_assert (dwarf2_per_objfile);
2754 }
2755
2756 /* Reader function for dw2_build_type_unit_groups.  */
2757
2758 static void
2759 dw2_build_type_unit_groups_reader (const struct die_reader_specs *reader,
2760                                    gdb_byte *info_ptr,
2761                                    struct die_info *type_unit_die,
2762                                    int has_children,
2763                                    void *data)
2764 {
2765   struct dwarf2_cu *cu = reader->cu;
2766   struct attribute *attr;
2767   struct type_unit_group *tu_group;
2768
2769   gdb_assert (data == NULL);
2770
2771   if (! has_children)
2772     return;
2773
2774   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
2775   /* Call this for its side-effect of creating the associated
2776      struct type_unit_group if it doesn't already exist.  */
2777   tu_group = get_type_unit_group (cu, attr);
2778 }
2779
2780 /* Build dwarf2_per_objfile->type_unit_groups.
2781    This function may be called multiple times.  */
2782
2783 static void
2784 dw2_build_type_unit_groups (void)
2785 {
2786   if (dwarf2_per_objfile->type_unit_groups == NULL)
2787     build_type_unit_groups (dw2_build_type_unit_groups_reader, NULL);
2788 }
2789
2790 /* die_reader_func for dw2_get_file_names.  */
2791
2792 static void
2793 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2794                            gdb_byte *info_ptr,
2795                            struct die_info *comp_unit_die,
2796                            int has_children,
2797                            void *data)
2798 {
2799   struct dwarf2_cu *cu = reader->cu;
2800   struct dwarf2_per_cu_data *this_cu = cu->per_cu;  
2801   struct objfile *objfile = dwarf2_per_objfile->objfile;
2802   struct dwarf2_per_cu_data *lh_cu;
2803   struct line_header *lh;
2804   struct attribute *attr;
2805   int i;
2806   char *name, *comp_dir;
2807   void **slot;
2808   struct quick_file_names *qfn;
2809   unsigned int line_offset;
2810
2811   /* Our callers never want to match partial units -- instead they
2812      will match the enclosing full CU.  */
2813   if (comp_unit_die->tag == DW_TAG_partial_unit)
2814     {
2815       this_cu->v.quick->no_file_data = 1;
2816       return;
2817     }
2818
2819   /* If we're reading the line header for TUs, store it in the "per_cu"
2820      for tu_group.  */
2821   if (this_cu->is_debug_types)
2822     {
2823       struct type_unit_group *tu_group = data;
2824
2825       gdb_assert (tu_group != NULL);
2826       lh_cu = &tu_group->per_cu;
2827     }
2828   else
2829     lh_cu = this_cu;
2830
2831   lh = NULL;
2832   slot = NULL;
2833   line_offset = 0;
2834
2835   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2836   if (attr)
2837     {
2838       struct quick_file_names find_entry;
2839
2840       line_offset = DW_UNSND (attr);
2841
2842       /* We may have already read in this line header (TU line header sharing).
2843          If we have we're done.  */
2844       find_entry.hash.dwo_unit = cu->dwo_unit;
2845       find_entry.hash.line_offset.sect_off = line_offset;
2846       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2847                              &find_entry, INSERT);
2848       if (*slot != NULL)
2849         {
2850           lh_cu->v.quick->file_names = *slot;
2851           return;
2852         }
2853
2854       lh = dwarf_decode_line_header (line_offset, cu);
2855     }
2856   if (lh == NULL)
2857     {
2858       lh_cu->v.quick->no_file_data = 1;
2859       return;
2860     }
2861
2862   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2863   qfn->hash.dwo_unit = cu->dwo_unit;
2864   qfn->hash.line_offset.sect_off = line_offset;
2865   gdb_assert (slot != NULL);
2866   *slot = qfn;
2867
2868   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2869
2870   qfn->num_file_names = lh->num_file_names;
2871   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2872                                    lh->num_file_names * sizeof (char *));
2873   for (i = 0; i < lh->num_file_names; ++i)
2874     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2875   qfn->real_names = NULL;
2876
2877   free_line_header (lh);
2878
2879   lh_cu->v.quick->file_names = qfn;
2880 }
2881
2882 /* A helper for the "quick" functions which attempts to read the line
2883    table for THIS_CU.  */
2884
2885 static struct quick_file_names *
2886 dw2_get_file_names (struct objfile *objfile,
2887                     struct dwarf2_per_cu_data *this_cu)
2888 {
2889   /* For TUs this should only be called on the parent group.  */
2890   if (this_cu->is_debug_types)
2891     gdb_assert (IS_TYPE_UNIT_GROUP (this_cu));
2892
2893   if (this_cu->v.quick->file_names != NULL)
2894     return this_cu->v.quick->file_names;
2895   /* If we know there is no line data, no point in looking again.  */
2896   if (this_cu->v.quick->no_file_data)
2897     return NULL;
2898
2899   /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2900      in the stub for CUs, there's is no need to lookup the DWO file.
2901      However, that's not the case for TUs where DW_AT_stmt_list lives in the
2902      DWO file.  */
2903   if (this_cu->is_debug_types)
2904     {
2905       struct type_unit_group *tu_group = this_cu->s.type_unit_group;
2906
2907       init_cutu_and_read_dies (tu_group->t.first_tu, NULL, 0, 0,
2908                                dw2_get_file_names_reader, tu_group);
2909     }
2910   else
2911     init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2912
2913   if (this_cu->v.quick->no_file_data)
2914     return NULL;
2915   return this_cu->v.quick->file_names;
2916 }
2917
2918 /* A helper for the "quick" functions which computes and caches the
2919    real path for a given file name from the line table.  */
2920
2921 static const char *
2922 dw2_get_real_path (struct objfile *objfile,
2923                    struct quick_file_names *qfn, int index)
2924 {
2925   if (qfn->real_names == NULL)
2926     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2927                                       qfn->num_file_names, sizeof (char *));
2928
2929   if (qfn->real_names[index] == NULL)
2930     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2931
2932   return qfn->real_names[index];
2933 }
2934
2935 static struct symtab *
2936 dw2_find_last_source_symtab (struct objfile *objfile)
2937 {
2938   int index;
2939
2940   dw2_setup (objfile);
2941   index = dwarf2_per_objfile->n_comp_units - 1;
2942   return dw2_instantiate_symtab (dw2_get_cu (index));
2943 }
2944
2945 /* Traversal function for dw2_forget_cached_source_info.  */
2946
2947 static int
2948 dw2_free_cached_file_names (void **slot, void *info)
2949 {
2950   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2951
2952   if (file_data->real_names)
2953     {
2954       int i;
2955
2956       for (i = 0; i < file_data->num_file_names; ++i)
2957         {
2958           xfree ((void*) file_data->real_names[i]);
2959           file_data->real_names[i] = NULL;
2960         }
2961     }
2962
2963   return 1;
2964 }
2965
2966 static void
2967 dw2_forget_cached_source_info (struct objfile *objfile)
2968 {
2969   dw2_setup (objfile);
2970
2971   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2972                           dw2_free_cached_file_names, NULL);
2973 }
2974
2975 /* Helper function for dw2_map_symtabs_matching_filename that expands
2976    the symtabs and calls the iterator.  */
2977
2978 static int
2979 dw2_map_expand_apply (struct objfile *objfile,
2980                       struct dwarf2_per_cu_data *per_cu,
2981                       const char *name,
2982                       const char *full_path, const char *real_path,
2983                       int (*callback) (struct symtab *, void *),
2984                       void *data)
2985 {
2986   struct symtab *last_made = objfile->symtabs;
2987
2988   /* Don't visit already-expanded CUs.  */
2989   if (per_cu->v.quick->symtab)
2990     return 0;
2991
2992   /* This may expand more than one symtab, and we want to iterate over
2993      all of them.  */
2994   dw2_instantiate_symtab (per_cu);
2995
2996   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2997                                     objfile->symtabs, last_made);
2998 }
2999
3000 /* Implementation of the map_symtabs_matching_filename method.  */
3001
3002 static int
3003 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3004                                    const char *full_path, const char *real_path,
3005                                    int (*callback) (struct symtab *, void *),
3006                                    void *data)
3007 {
3008   int i;
3009   const char *name_basename = lbasename (name);
3010   int name_len = strlen (name);
3011   int is_abs = IS_ABSOLUTE_PATH (name);
3012
3013   dw2_setup (objfile);
3014
3015   dw2_build_type_unit_groups ();
3016
3017   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3018                    + dwarf2_per_objfile->n_type_unit_groups); ++i)
3019     {
3020       int j;
3021       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3022       struct quick_file_names *file_data;
3023
3024       /* We only need to look at symtabs not already expanded.  */
3025       if (per_cu->v.quick->symtab)
3026         continue;
3027
3028       file_data = dw2_get_file_names (objfile, per_cu);
3029       if (file_data == NULL)
3030         continue;
3031
3032       for (j = 0; j < file_data->num_file_names; ++j)
3033         {
3034           const char *this_name = file_data->file_names[j];
3035
3036           if (FILENAME_CMP (name, this_name) == 0
3037               || (!is_abs && compare_filenames_for_search (this_name,
3038                                                            name, name_len)))
3039             {
3040               if (dw2_map_expand_apply (objfile, per_cu,
3041                                         name, full_path, real_path,
3042                                         callback, data))
3043                 return 1;
3044             }
3045
3046           /* Before we invoke realpath, which can get expensive when many
3047              files are involved, do a quick comparison of the basenames.  */
3048           if (! basenames_may_differ
3049               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3050             continue;
3051
3052           if (full_path != NULL)
3053             {
3054               const char *this_real_name = dw2_get_real_path (objfile,
3055                                                               file_data, j);
3056
3057               if (this_real_name != NULL
3058                   && (FILENAME_CMP (full_path, this_real_name) == 0
3059                       || (!is_abs
3060                           && compare_filenames_for_search (this_real_name,
3061                                                            name, name_len))))
3062                 {
3063                   if (dw2_map_expand_apply (objfile, per_cu,
3064                                             name, full_path, real_path,
3065                                             callback, data))
3066                     return 1;
3067                 }
3068             }
3069
3070           if (real_path != NULL)
3071             {
3072               const char *this_real_name = dw2_get_real_path (objfile,
3073                                                               file_data, j);
3074
3075               if (this_real_name != NULL
3076                   && (FILENAME_CMP (real_path, this_real_name) == 0
3077                       || (!is_abs
3078                           && compare_filenames_for_search (this_real_name,
3079                                                            name, name_len))))
3080                 {
3081                   if (dw2_map_expand_apply (objfile, per_cu,
3082                                             name, full_path, real_path,
3083                                             callback, data))
3084                     return 1;
3085                 }
3086             }
3087         }
3088     }
3089
3090   return 0;
3091 }
3092
3093 static struct symtab *
3094 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3095                    const char *name, domain_enum domain)
3096 {
3097   /* We do all the work in the pre_expand_symtabs_matching hook
3098      instead.  */
3099   return NULL;
3100 }
3101
3102 /* A helper function that expands all symtabs that hold an object
3103    named NAME.  If WANT_SPECIFIC_BLOCK is non-zero, only look for
3104    symbols in block BLOCK_KIND.  */
3105
3106 static void
3107 dw2_do_expand_symtabs_matching (struct objfile *objfile,
3108                                 int want_specific_block,
3109                                 enum block_enum block_kind,
3110                                 const char *name, domain_enum domain)
3111 {
3112   struct mapped_index *index;
3113
3114   dw2_setup (objfile);
3115
3116   index = dwarf2_per_objfile->index_table;
3117
3118   /* index_table is NULL if OBJF_READNOW.  */
3119   if (index)
3120     {
3121       offset_type *vec;
3122
3123       if (find_slot_in_mapped_hash (index, name, &vec))
3124         {
3125           offset_type i, len = MAYBE_SWAP (*vec);
3126           for (i = 0; i < len; ++i)
3127             {
3128               offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
3129               offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3130               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3131               int want_static = block_kind != GLOBAL_BLOCK;
3132               /* This value is only valid for index versions >= 7.  */
3133               int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3134               gdb_index_symbol_kind symbol_kind =
3135                 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3136
3137               if (want_specific_block
3138                   && index->version >= 7
3139                   && want_static != is_static)
3140                 continue;
3141
3142               /* Only check the symbol's kind if it has one.
3143                  Indices prior to version 7 don't record it.  */
3144               if (index->version >= 7)
3145                 {
3146                   switch (domain)
3147                     {
3148                     case VAR_DOMAIN:
3149                       if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3150                           && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3151                           /* Some types are also in VAR_DOMAIN.  */
3152                           && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3153                         continue;
3154                       break;
3155                     case STRUCT_DOMAIN:
3156                       if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3157                         continue;
3158                       break;
3159                     case LABEL_DOMAIN:
3160                       if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3161                         continue;
3162                       break;
3163                     default:
3164                       break;
3165                     }
3166                 }
3167
3168               dw2_instantiate_symtab (per_cu);
3169             }
3170         }
3171     }
3172 }
3173
3174 static void
3175 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
3176                                  enum block_enum block_kind, const char *name,
3177                                  domain_enum domain)
3178 {
3179   dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
3180 }
3181
3182 static void
3183 dw2_print_stats (struct objfile *objfile)
3184 {
3185   int i, count;
3186
3187   dw2_setup (objfile);
3188   count = 0;
3189   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3190                    + dwarf2_per_objfile->n_type_units); ++i)
3191     {
3192       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3193
3194       if (!per_cu->v.quick->symtab)
3195         ++count;
3196     }
3197   printf_filtered (_("  Number of unread CUs: %d\n"), count);
3198 }
3199
3200 static void
3201 dw2_dump (struct objfile *objfile)
3202 {
3203   /* Nothing worth printing.  */
3204 }
3205
3206 static void
3207 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
3208               struct section_offsets *delta)
3209 {
3210   /* There's nothing to relocate here.  */
3211 }
3212
3213 static void
3214 dw2_expand_symtabs_for_function (struct objfile *objfile,
3215                                  const char *func_name)
3216 {
3217   /* Note: It doesn't matter what we pass for block_kind here.  */
3218   dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
3219                                   VAR_DOMAIN);
3220 }
3221
3222 static void
3223 dw2_expand_all_symtabs (struct objfile *objfile)
3224 {
3225   int i;
3226
3227   dw2_setup (objfile);
3228
3229   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3230                    + dwarf2_per_objfile->n_type_units); ++i)
3231     {
3232       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3233
3234       dw2_instantiate_symtab (per_cu);
3235     }
3236 }
3237
3238 static void
3239 dw2_expand_symtabs_with_filename (struct objfile *objfile,
3240                                   const char *filename)
3241 {
3242   int i;
3243
3244   dw2_setup (objfile);
3245
3246   /* We don't need to consider type units here.
3247      This is only called for examining code, e.g. expand_line_sal.
3248      There can be an order of magnitude (or more) more type units
3249      than comp units, and we avoid them if we can.  */
3250
3251   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3252     {
3253       int j;
3254       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3255       struct quick_file_names *file_data;
3256
3257       /* We only need to look at symtabs not already expanded.  */
3258       if (per_cu->v.quick->symtab)
3259         continue;
3260
3261       file_data = dw2_get_file_names (objfile, per_cu);
3262       if (file_data == NULL)
3263         continue;
3264
3265       for (j = 0; j < file_data->num_file_names; ++j)
3266         {
3267           const char *this_name = file_data->file_names[j];
3268           if (FILENAME_CMP (this_name, filename) == 0)
3269             {
3270               dw2_instantiate_symtab (per_cu);
3271               break;
3272             }
3273         }
3274     }
3275 }
3276
3277 /* A helper function for dw2_find_symbol_file that finds the primary
3278    file name for a given CU.  This is a die_reader_func.  */
3279
3280 static void
3281 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3282                                  gdb_byte *info_ptr,
3283                                  struct die_info *comp_unit_die,
3284                                  int has_children,
3285                                  void *data)
3286 {
3287   const char **result_ptr = data;
3288   struct dwarf2_cu *cu = reader->cu;
3289   struct attribute *attr;
3290
3291   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3292   if (attr == NULL)
3293     *result_ptr = NULL;
3294   else
3295     *result_ptr = DW_STRING (attr);
3296 }
3297
3298 static const char *
3299 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3300 {
3301   struct dwarf2_per_cu_data *per_cu;
3302   offset_type *vec;
3303   struct quick_file_names *file_data;
3304   const char *filename;
3305
3306   dw2_setup (objfile);
3307
3308   /* index_table is NULL if OBJF_READNOW.  */
3309   if (!dwarf2_per_objfile->index_table)
3310     {
3311       struct symtab *s;
3312
3313       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3314         {
3315           struct blockvector *bv = BLOCKVECTOR (s);
3316           const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3317           struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3318
3319           if (sym)
3320             return sym->symtab->filename;
3321         }
3322       return NULL;
3323     }
3324
3325   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3326                                  name, &vec))
3327     return NULL;
3328
3329   /* Note that this just looks at the very first one named NAME -- but
3330      actually we are looking for a function.  find_main_filename
3331      should be rewritten so that it doesn't require a custom hook.  It
3332      could just use the ordinary symbol tables.  */
3333   /* vec[0] is the length, which must always be >0.  */
3334   per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3335
3336   if (per_cu->v.quick->symtab != NULL)
3337     return per_cu->v.quick->symtab->filename;
3338
3339   init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3340                            dw2_get_primary_filename_reader, &filename);
3341
3342   return filename;
3343 }
3344
3345 static void
3346 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3347                           struct objfile *objfile, int global,
3348                           int (*callback) (struct block *,
3349                                            struct symbol *, void *),
3350                           void *data, symbol_compare_ftype *match,
3351                           symbol_compare_ftype *ordered_compare)
3352 {
3353   /* Currently unimplemented; used for Ada.  The function can be called if the
3354      current language is Ada for a non-Ada objfile using GNU index.  As Ada
3355      does not look for non-Ada symbols this function should just return.  */
3356 }
3357
3358 static void
3359 dw2_expand_symtabs_matching
3360   (struct objfile *objfile,
3361    int (*file_matcher) (const char *, void *),
3362    int (*name_matcher) (const char *, void *),
3363    enum search_domain kind,
3364    void *data)
3365 {
3366   int i;
3367   offset_type iter;
3368   struct mapped_index *index;
3369
3370   dw2_setup (objfile);
3371
3372   /* index_table is NULL if OBJF_READNOW.  */
3373   if (!dwarf2_per_objfile->index_table)
3374     return;
3375   index = dwarf2_per_objfile->index_table;
3376
3377   if (file_matcher != NULL)
3378     {
3379       struct cleanup *cleanup;
3380       htab_t visited_found, visited_not_found;
3381
3382       dw2_build_type_unit_groups ();
3383
3384       visited_found = htab_create_alloc (10,
3385                                          htab_hash_pointer, htab_eq_pointer,
3386                                          NULL, xcalloc, xfree);
3387       cleanup = make_cleanup_htab_delete (visited_found);
3388       visited_not_found = htab_create_alloc (10,
3389                                              htab_hash_pointer, htab_eq_pointer,
3390                                              NULL, xcalloc, xfree);
3391       make_cleanup_htab_delete (visited_not_found);
3392
3393       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3394                        + dwarf2_per_objfile->n_type_unit_groups); ++i)
3395         {
3396           int j;
3397           struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3398           struct quick_file_names *file_data;
3399           void **slot;
3400
3401           per_cu->v.quick->mark = 0;
3402
3403           /* We only need to look at symtabs not already expanded.  */
3404           if (per_cu->v.quick->symtab)
3405             continue;
3406
3407           file_data = dw2_get_file_names (objfile, per_cu);
3408           if (file_data == NULL)
3409             continue;
3410
3411           if (htab_find (visited_not_found, file_data) != NULL)
3412             continue;
3413           else if (htab_find (visited_found, file_data) != NULL)
3414             {
3415               per_cu->v.quick->mark = 1;
3416               continue;
3417             }
3418
3419           for (j = 0; j < file_data->num_file_names; ++j)
3420             {
3421               if (file_matcher (file_data->file_names[j], data))
3422                 {
3423                   per_cu->v.quick->mark = 1;
3424                   break;
3425                 }
3426             }
3427
3428           slot = htab_find_slot (per_cu->v.quick->mark
3429                                  ? visited_found
3430                                  : visited_not_found,
3431                                  file_data, INSERT);
3432           *slot = file_data;
3433         }
3434
3435       do_cleanups (cleanup);
3436     }
3437
3438   for (iter = 0; iter < index->symbol_table_slots; ++iter)
3439     {
3440       offset_type idx = 2 * iter;
3441       const char *name;
3442       offset_type *vec, vec_len, vec_idx;
3443
3444       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3445         continue;
3446
3447       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3448
3449       if (! (*name_matcher) (name, data))
3450         continue;
3451
3452       /* The name was matched, now expand corresponding CUs that were
3453          marked.  */
3454       vec = (offset_type *) (index->constant_pool
3455                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
3456       vec_len = MAYBE_SWAP (vec[0]);
3457       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3458         {
3459           struct dwarf2_per_cu_data *per_cu;
3460           offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3461           gdb_index_symbol_kind symbol_kind =
3462             GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3463           int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3464
3465           /* Don't crash on bad data.  */
3466           if (cu_index >= (dwarf2_per_objfile->n_comp_units
3467                            + dwarf2_per_objfile->n_type_units))
3468             continue;
3469
3470           /* Only check the symbol's kind if it has one.
3471              Indices prior to version 7 don't record it.  */
3472           if (index->version >= 7)
3473             {
3474               switch (kind)
3475                 {
3476                 case VARIABLES_DOMAIN:
3477                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3478                     continue;
3479                   break;
3480                 case FUNCTIONS_DOMAIN:
3481                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3482                     continue;
3483                   break;
3484                 case TYPES_DOMAIN:
3485                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3486                     continue;
3487                   break;
3488                 default:
3489                   break;
3490                 }
3491             }
3492
3493           per_cu = dw2_get_cu (cu_index);
3494           if (file_matcher == NULL || per_cu->v.quick->mark)
3495             dw2_instantiate_symtab (per_cu);
3496         }
3497     }
3498 }
3499
3500 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3501    symtab.  */
3502
3503 static struct symtab *
3504 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3505 {
3506   int i;
3507
3508   if (BLOCKVECTOR (symtab) != NULL
3509       && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3510     return symtab;
3511
3512   if (symtab->includes == NULL)
3513     return NULL;
3514
3515   for (i = 0; symtab->includes[i]; ++i)
3516     {
3517       struct symtab *s = symtab->includes[i];
3518
3519       s = recursively_find_pc_sect_symtab (s, pc);
3520       if (s != NULL)
3521         return s;
3522     }
3523
3524   return NULL;
3525 }
3526
3527 static struct symtab *
3528 dw2_find_pc_sect_symtab (struct objfile *objfile,
3529                          struct minimal_symbol *msymbol,
3530                          CORE_ADDR pc,
3531                          struct obj_section *section,
3532                          int warn_if_readin)
3533 {
3534   struct dwarf2_per_cu_data *data;
3535   struct symtab *result;
3536
3537   dw2_setup (objfile);
3538
3539   if (!objfile->psymtabs_addrmap)
3540     return NULL;
3541
3542   data = addrmap_find (objfile->psymtabs_addrmap, pc);
3543   if (!data)
3544     return NULL;
3545
3546   if (warn_if_readin && data->v.quick->symtab)
3547     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3548              paddress (get_objfile_arch (objfile), pc));
3549
3550   result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3551   gdb_assert (result != NULL);
3552   return result;
3553 }
3554
3555 static void
3556 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3557                           void *data, int need_fullname)
3558 {
3559   int i;
3560   struct cleanup *cleanup;
3561   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3562                                       NULL, xcalloc, xfree);
3563
3564   cleanup = make_cleanup_htab_delete (visited);
3565   dw2_setup (objfile);
3566
3567   dw2_build_type_unit_groups ();
3568
3569   /* We can ignore file names coming from already-expanded CUs.  */
3570   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3571                    + dwarf2_per_objfile->n_type_units); ++i)
3572     {
3573       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3574
3575       if (per_cu->v.quick->symtab)
3576         {
3577           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3578                                         INSERT);
3579
3580           *slot = per_cu->v.quick->file_names;
3581         }
3582     }
3583
3584   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3585                    + dwarf2_per_objfile->n_type_unit_groups); ++i)
3586     {
3587       int j;
3588       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3589       struct quick_file_names *file_data;
3590       void **slot;
3591
3592       /* We only need to look at symtabs not already expanded.  */
3593       if (per_cu->v.quick->symtab)
3594         continue;
3595
3596       file_data = dw2_get_file_names (objfile, per_cu);
3597       if (file_data == NULL)
3598         continue;
3599
3600       slot = htab_find_slot (visited, file_data, INSERT);
3601       if (*slot)
3602         {
3603           /* Already visited.  */
3604           continue;
3605         }
3606       *slot = file_data;
3607
3608       for (j = 0; j < file_data->num_file_names; ++j)
3609         {
3610           const char *this_real_name;
3611
3612           if (need_fullname)
3613             this_real_name = dw2_get_real_path (objfile, file_data, j);
3614           else
3615             this_real_name = NULL;
3616           (*fun) (file_data->file_names[j], this_real_name, data);
3617         }
3618     }
3619
3620   do_cleanups (cleanup);
3621 }
3622
3623 static int
3624 dw2_has_symbols (struct objfile *objfile)
3625 {
3626   return 1;
3627 }
3628
3629 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3630 {
3631   dw2_has_symbols,
3632   dw2_find_last_source_symtab,
3633   dw2_forget_cached_source_info,
3634   dw2_map_symtabs_matching_filename,
3635   dw2_lookup_symbol,
3636   dw2_pre_expand_symtabs_matching,
3637   dw2_print_stats,
3638   dw2_dump,
3639   dw2_relocate,
3640   dw2_expand_symtabs_for_function,
3641   dw2_expand_all_symtabs,
3642   dw2_expand_symtabs_with_filename,
3643   dw2_find_symbol_file,
3644   dw2_map_matching_symbols,
3645   dw2_expand_symtabs_matching,
3646   dw2_find_pc_sect_symtab,
3647   dw2_map_symbol_filenames
3648 };
3649
3650 /* Initialize for reading DWARF for this objfile.  Return 0 if this
3651    file will use psymtabs, or 1 if using the GNU index.  */
3652
3653 int
3654 dwarf2_initialize_objfile (struct objfile *objfile)
3655 {
3656   /* If we're about to read full symbols, don't bother with the
3657      indices.  In this case we also don't care if some other debug
3658      format is making psymtabs, because they are all about to be
3659      expanded anyway.  */
3660   if ((objfile->flags & OBJF_READNOW))
3661     {
3662       int i;
3663
3664       dwarf2_per_objfile->using_index = 1;
3665       create_all_comp_units (objfile);
3666       create_all_type_units (objfile);
3667       dwarf2_per_objfile->quick_file_names_table =
3668         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3669
3670       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3671                        + dwarf2_per_objfile->n_type_units); ++i)
3672         {
3673           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3674
3675           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3676                                             struct dwarf2_per_cu_quick_data);
3677         }
3678
3679       /* Return 1 so that gdb sees the "quick" functions.  However,
3680          these functions will be no-ops because we will have expanded
3681          all symtabs.  */
3682       return 1;
3683     }
3684
3685   if (dwarf2_read_index (objfile))
3686     return 1;
3687
3688   return 0;
3689 }
3690
3691 \f
3692
3693 /* Build a partial symbol table.  */
3694
3695 void
3696 dwarf2_build_psymtabs (struct objfile *objfile)
3697 {
3698   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3699     {
3700       init_psymbol_list (objfile, 1024);
3701     }
3702
3703   dwarf2_build_psymtabs_hard (objfile);
3704 }
3705
3706 /* Return the total length of the CU described by HEADER.  */
3707
3708 static unsigned int
3709 get_cu_length (const struct comp_unit_head *header)
3710 {
3711   return header->initial_length_size + header->length;
3712 }
3713
3714 /* Return TRUE if OFFSET is within CU_HEADER.  */
3715
3716 static inline int
3717 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3718 {
3719   sect_offset bottom = { cu_header->offset.sect_off };
3720   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3721
3722   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3723 }
3724
3725 /* Find the base address of the compilation unit for range lists and
3726    location lists.  It will normally be specified by DW_AT_low_pc.
3727    In DWARF-3 draft 4, the base address could be overridden by
3728    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3729    compilation units with discontinuous ranges.  */
3730
3731 static void
3732 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3733 {
3734   struct attribute *attr;
3735
3736   cu->base_known = 0;
3737   cu->base_address = 0;
3738
3739   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3740   if (attr)
3741     {
3742       cu->base_address = DW_ADDR (attr);
3743       cu->base_known = 1;
3744     }
3745   else
3746     {
3747       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3748       if (attr)
3749         {
3750           cu->base_address = DW_ADDR (attr);
3751           cu->base_known = 1;
3752         }
3753     }
3754 }
3755
3756 /* Read in the comp unit header information from the debug_info at info_ptr.
3757    NOTE: This leaves members offset, first_die_offset to be filled in
3758    by the caller.  */
3759
3760 static gdb_byte *
3761 read_comp_unit_head (struct comp_unit_head *cu_header,
3762                      gdb_byte *info_ptr, bfd *abfd)
3763 {
3764   int signed_addr;
3765   unsigned int bytes_read;
3766
3767   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3768   cu_header->initial_length_size = bytes_read;
3769   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3770   info_ptr += bytes_read;
3771   cu_header->version = read_2_bytes (abfd, info_ptr);
3772   info_ptr += 2;
3773   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3774                                              &bytes_read);
3775   info_ptr += bytes_read;
3776   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3777   info_ptr += 1;
3778   signed_addr = bfd_get_sign_extend_vma (abfd);
3779   if (signed_addr < 0)
3780     internal_error (__FILE__, __LINE__,
3781                     _("read_comp_unit_head: dwarf from non elf file"));
3782   cu_header->signed_addr_p = signed_addr;
3783
3784   return info_ptr;
3785 }
3786
3787 /* Helper function that returns the proper abbrev section for
3788    THIS_CU.  */
3789
3790 static struct dwarf2_section_info *
3791 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3792 {
3793   struct dwarf2_section_info *abbrev;
3794
3795   if (this_cu->is_dwz)
3796     abbrev = &dwarf2_get_dwz_file ()->abbrev;
3797   else
3798     abbrev = &dwarf2_per_objfile->abbrev;
3799
3800   return abbrev;
3801 }
3802
3803 /* Subroutine of read_and_check_comp_unit_head and
3804    read_and_check_type_unit_head to simplify them.
3805    Perform various error checking on the header.  */
3806
3807 static void
3808 error_check_comp_unit_head (struct comp_unit_head *header,
3809                             struct dwarf2_section_info *section,
3810                             struct dwarf2_section_info *abbrev_section)
3811 {
3812   bfd *abfd = section->asection->owner;
3813   const char *filename = bfd_get_filename (abfd);
3814
3815   if (header->version != 2 && header->version != 3 && header->version != 4)
3816     error (_("Dwarf Error: wrong version in compilation unit header "
3817            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3818            filename);
3819
3820   if (header->abbrev_offset.sect_off
3821       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
3822     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3823            "(offset 0x%lx + 6) [in module %s]"),
3824            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3825            filename);
3826
3827   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3828      avoid potential 32-bit overflow.  */
3829   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
3830       > section->size)
3831     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3832            "(offset 0x%lx + 0) [in module %s]"),
3833            (long) header->length, (long) header->offset.sect_off,
3834            filename);
3835 }
3836
3837 /* Read in a CU/TU header and perform some basic error checking.
3838    The contents of the header are stored in HEADER.
3839    The result is a pointer to the start of the first DIE.  */
3840
3841 static gdb_byte *
3842 read_and_check_comp_unit_head (struct comp_unit_head *header,
3843                                struct dwarf2_section_info *section,
3844                                struct dwarf2_section_info *abbrev_section,
3845                                gdb_byte *info_ptr,
3846                                int is_debug_types_section)
3847 {
3848   gdb_byte *beg_of_comp_unit = info_ptr;
3849   bfd *abfd = section->asection->owner;
3850
3851   header->offset.sect_off = beg_of_comp_unit - section->buffer;
3852
3853   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3854
3855   /* If we're reading a type unit, skip over the signature and
3856      type_offset fields.  */
3857   if (is_debug_types_section)
3858     info_ptr += 8 /*signature*/ + header->offset_size;
3859
3860   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3861
3862   error_check_comp_unit_head (header, section, abbrev_section);
3863
3864   return info_ptr;
3865 }
3866
3867 /* Read in the types comp unit header information from .debug_types entry at
3868    types_ptr.  The result is a pointer to one past the end of the header.  */
3869
3870 static gdb_byte *
3871 read_and_check_type_unit_head (struct comp_unit_head *header,
3872                                struct dwarf2_section_info *section,
3873                                struct dwarf2_section_info *abbrev_section,
3874                                gdb_byte *info_ptr,
3875                                ULONGEST *signature,
3876                                cu_offset *type_offset_in_tu)
3877 {
3878   gdb_byte *beg_of_comp_unit = info_ptr;
3879   bfd *abfd = section->asection->owner;
3880
3881   header->offset.sect_off = beg_of_comp_unit - section->buffer;
3882
3883   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3884
3885   /* If we're reading a type unit, skip over the signature and
3886      type_offset fields.  */
3887   if (signature != NULL)
3888     *signature = read_8_bytes (abfd, info_ptr);
3889   info_ptr += 8;
3890   if (type_offset_in_tu != NULL)
3891     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3892                                                header->offset_size);
3893   info_ptr += header->offset_size;
3894
3895   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3896
3897   error_check_comp_unit_head (header, section, abbrev_section);
3898
3899   return info_ptr;
3900 }
3901
3902 /* Fetch the abbreviation table offset from a comp or type unit header.  */
3903
3904 static sect_offset
3905 read_abbrev_offset (struct dwarf2_section_info *section,
3906                     sect_offset offset)
3907 {
3908   bfd *abfd = section->asection->owner;
3909   gdb_byte *info_ptr;
3910   unsigned int length, initial_length_size, offset_size;
3911   sect_offset abbrev_offset;
3912
3913   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3914   info_ptr = section->buffer + offset.sect_off;
3915   length = read_initial_length (abfd, info_ptr, &initial_length_size);
3916   offset_size = initial_length_size == 4 ? 4 : 8;
3917   info_ptr += initial_length_size + 2 /*version*/;
3918   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
3919   return abbrev_offset;
3920 }
3921
3922 /* Allocate a new partial symtab for file named NAME and mark this new
3923    partial symtab as being an include of PST.  */
3924
3925 static void
3926 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3927                                struct objfile *objfile)
3928 {
3929   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3930
3931   subpst->section_offsets = pst->section_offsets;
3932   subpst->textlow = 0;
3933   subpst->texthigh = 0;
3934
3935   subpst->dependencies = (struct partial_symtab **)
3936     obstack_alloc (&objfile->objfile_obstack,
3937                    sizeof (struct partial_symtab *));
3938   subpst->dependencies[0] = pst;
3939   subpst->number_of_dependencies = 1;
3940
3941   subpst->globals_offset = 0;
3942   subpst->n_global_syms = 0;
3943   subpst->statics_offset = 0;
3944   subpst->n_static_syms = 0;
3945   subpst->symtab = NULL;
3946   subpst->read_symtab = pst->read_symtab;
3947   subpst->readin = 0;
3948
3949   /* No private part is necessary for include psymtabs.  This property
3950      can be used to differentiate between such include psymtabs and
3951      the regular ones.  */
3952   subpst->read_symtab_private = NULL;
3953 }
3954
3955 /* Read the Line Number Program data and extract the list of files
3956    included by the source file represented by PST.  Build an include
3957    partial symtab for each of these included files.  */
3958
3959 static void
3960 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3961                                struct die_info *die,
3962                                struct partial_symtab *pst)
3963 {
3964   struct line_header *lh = NULL;
3965   struct attribute *attr;
3966
3967   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3968   if (attr)
3969     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
3970   if (lh == NULL)
3971     return;  /* No linetable, so no includes.  */
3972
3973   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3974   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3975
3976   free_line_header (lh);
3977 }
3978
3979 static hashval_t
3980 hash_signatured_type (const void *item)
3981 {
3982   const struct signatured_type *sig_type = item;
3983
3984   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3985   return sig_type->signature;
3986 }
3987
3988 static int
3989 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3990 {
3991   const struct signatured_type *lhs = item_lhs;
3992   const struct signatured_type *rhs = item_rhs;
3993
3994   return lhs->signature == rhs->signature;
3995 }
3996
3997 /* Allocate a hash table for signatured types.  */
3998
3999 static htab_t
4000 allocate_signatured_type_table (struct objfile *objfile)
4001 {
4002   return htab_create_alloc_ex (41,
4003                                hash_signatured_type,
4004                                eq_signatured_type,
4005                                NULL,
4006                                &objfile->objfile_obstack,
4007                                hashtab_obstack_allocate,
4008                                dummy_obstack_deallocate);
4009 }
4010
4011 /* A helper function to add a signatured type CU to a table.  */
4012
4013 static int
4014 add_signatured_type_cu_to_table (void **slot, void *datum)
4015 {
4016   struct signatured_type *sigt = *slot;
4017   struct signatured_type ***datap = datum;
4018
4019   **datap = sigt;
4020   ++*datap;
4021
4022   return 1;
4023 }
4024
4025 /* Create the hash table of all entries in the .debug_types section.
4026    DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
4027    The result is a pointer to the hash table or NULL if there are
4028    no types.  */
4029
4030 static htab_t
4031 create_debug_types_hash_table (struct dwo_file *dwo_file,
4032                                VEC (dwarf2_section_info_def) *types)
4033 {
4034   struct objfile *objfile = dwarf2_per_objfile->objfile;
4035   htab_t types_htab = NULL;
4036   int ix;
4037   struct dwarf2_section_info *section;
4038   struct dwarf2_section_info *abbrev_section;
4039
4040   if (VEC_empty (dwarf2_section_info_def, types))
4041     return NULL;
4042
4043   abbrev_section = (dwo_file != NULL
4044                     ? &dwo_file->sections.abbrev
4045                     : &dwarf2_per_objfile->abbrev);
4046
4047   if (dwarf2_read_debug)
4048     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4049                         dwo_file ? ".dwo" : "",
4050                         bfd_get_filename (abbrev_section->asection->owner));
4051
4052   for (ix = 0;
4053        VEC_iterate (dwarf2_section_info_def, types, ix, section);
4054        ++ix)
4055     {
4056       bfd *abfd;
4057       gdb_byte *info_ptr, *end_ptr;
4058       struct dwarf2_section_info *abbrev_section;
4059
4060       dwarf2_read_section (objfile, section);
4061       info_ptr = section->buffer;
4062
4063       if (info_ptr == NULL)
4064         continue;
4065
4066       /* We can't set abfd until now because the section may be empty or
4067          not present, in which case section->asection will be NULL.  */
4068       abfd = section->asection->owner;
4069
4070       if (dwo_file)
4071         abbrev_section = &dwo_file->sections.abbrev;
4072       else
4073         abbrev_section = &dwarf2_per_objfile->abbrev;
4074
4075       if (types_htab == NULL)
4076         {
4077           if (dwo_file)
4078             types_htab = allocate_dwo_unit_table (objfile);
4079           else
4080             types_htab = allocate_signatured_type_table (objfile);
4081         }
4082
4083       /* We don't use init_cutu_and_read_dies_simple, or some such, here
4084          because we don't need to read any dies: the signature is in the
4085          header.  */
4086
4087       end_ptr = info_ptr + section->size;
4088       while (info_ptr < end_ptr)
4089         {
4090           sect_offset offset;
4091           cu_offset type_offset_in_tu;
4092           ULONGEST signature;
4093           struct signatured_type *sig_type;
4094           struct dwo_unit *dwo_tu;
4095           void **slot;
4096           gdb_byte *ptr = info_ptr;
4097           struct comp_unit_head header;
4098           unsigned int length;
4099
4100           offset.sect_off = ptr - section->buffer;
4101
4102           /* We need to read the type's signature in order to build the hash
4103              table, but we don't need anything else just yet.  */
4104
4105           ptr = read_and_check_type_unit_head (&header, section,
4106                                                abbrev_section, ptr,
4107                                                &signature, &type_offset_in_tu);
4108
4109           length = get_cu_length (&header);
4110
4111           /* Skip dummy type units.  */
4112           if (ptr >= info_ptr + length
4113               || peek_abbrev_code (abfd, ptr) == 0)
4114             {
4115               info_ptr += length;
4116               continue;
4117             }
4118
4119           if (dwo_file)
4120             {
4121               sig_type = NULL;
4122               dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4123                                        struct dwo_unit);
4124               dwo_tu->dwo_file = dwo_file;
4125               dwo_tu->signature = signature;
4126               dwo_tu->type_offset_in_tu = type_offset_in_tu;
4127               dwo_tu->info_or_types_section = section;
4128               dwo_tu->offset = offset;
4129               dwo_tu->length = length;
4130             }
4131           else
4132             {
4133               /* N.B.: type_offset is not usable if this type uses a DWO file.
4134                  The real type_offset is in the DWO file.  */
4135               dwo_tu = NULL;
4136               sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4137                                          struct signatured_type);
4138               sig_type->signature = signature;
4139               sig_type->type_offset_in_tu = type_offset_in_tu;
4140               sig_type->per_cu.objfile = objfile;
4141               sig_type->per_cu.is_debug_types = 1;
4142               sig_type->per_cu.info_or_types_section = section;
4143               sig_type->per_cu.offset = offset;
4144               sig_type->per_cu.length = length;
4145             }
4146
4147           slot = htab_find_slot (types_htab,
4148                                  dwo_file ? (void*) dwo_tu : (void *) sig_type,
4149                                  INSERT);
4150           gdb_assert (slot != NULL);
4151           if (*slot != NULL)
4152             {
4153               sect_offset dup_offset;
4154
4155               if (dwo_file)
4156                 {
4157                   const struct dwo_unit *dup_tu = *slot;
4158
4159                   dup_offset = dup_tu->offset;
4160                 }
4161               else
4162                 {
4163                   const struct signatured_type *dup_tu = *slot;
4164
4165                   dup_offset = dup_tu->per_cu.offset;
4166                 }
4167
4168               complaint (&symfile_complaints,
4169                          _("debug type entry at offset 0x%x is duplicate to the "
4170                            "entry at offset 0x%x, signature 0x%s"),
4171                          offset.sect_off, dup_offset.sect_off,
4172                          phex (signature, sizeof (signature)));
4173             }
4174           *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4175
4176           if (dwarf2_read_debug)
4177             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
4178                                 offset.sect_off,
4179                                 phex (signature, sizeof (signature)));
4180
4181           info_ptr += length;
4182         }
4183     }
4184
4185   return types_htab;
4186 }
4187
4188 /* Create the hash table of all entries in the .debug_types section,
4189    and initialize all_type_units.
4190    The result is zero if there is an error (e.g. missing .debug_types section),
4191    otherwise non-zero.  */
4192
4193 static int
4194 create_all_type_units (struct objfile *objfile)
4195 {
4196   htab_t types_htab;
4197   struct signatured_type **iter;
4198
4199   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4200   if (types_htab == NULL)
4201     {
4202       dwarf2_per_objfile->signatured_types = NULL;
4203       return 0;
4204     }
4205
4206   dwarf2_per_objfile->signatured_types = types_htab;
4207
4208   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4209   dwarf2_per_objfile->all_type_units
4210     = obstack_alloc (&objfile->objfile_obstack,
4211                      dwarf2_per_objfile->n_type_units
4212                      * sizeof (struct signatured_type *));
4213   iter = &dwarf2_per_objfile->all_type_units[0];
4214   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4215   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4216               == dwarf2_per_objfile->n_type_units);
4217
4218   return 1;
4219 }
4220
4221 /* Lookup a signature based type for DW_FORM_ref_sig8.
4222    Returns NULL if signature SIG is not present in the table.  */
4223
4224 static struct signatured_type *
4225 lookup_signatured_type (ULONGEST sig)
4226 {
4227   struct signatured_type find_entry, *entry;
4228
4229   if (dwarf2_per_objfile->signatured_types == NULL)
4230     {
4231       complaint (&symfile_complaints,
4232                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
4233       return NULL;
4234     }
4235
4236   find_entry.signature = sig;
4237   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4238   return entry;
4239 }
4240 \f
4241 /* Low level DIE reading support.  */
4242
4243 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
4244
4245 static void
4246 init_cu_die_reader (struct die_reader_specs *reader,
4247                     struct dwarf2_cu *cu,
4248                     struct dwarf2_section_info *section,
4249                     struct dwo_file *dwo_file)
4250 {
4251   gdb_assert (section->readin && section->buffer != NULL);
4252   reader->abfd = section->asection->owner;
4253   reader->cu = cu;
4254   reader->dwo_file = dwo_file;
4255   reader->die_section = section;
4256   reader->buffer = section->buffer;
4257   reader->buffer_end = section->buffer + section->size;
4258 }
4259
4260 /* Initialize a CU (or TU) and read its DIEs.
4261    If the CU defers to a DWO file, read the DWO file as well.
4262
4263    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4264    Otherwise the table specified in the comp unit header is read in and used.
4265    This is an optimization for when we already have the abbrev table.
4266
4267    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4268    Otherwise, a new CU is allocated with xmalloc.
4269
4270    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4271    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.
4272
4273    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4274    linker) then DIE_READER_FUNC will not get called.  */
4275
4276 static void
4277 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4278                          struct abbrev_table *abbrev_table,
4279                          int use_existing_cu, int keep,
4280                          die_reader_func_ftype *die_reader_func,
4281                          void *data)
4282 {
4283   struct objfile *objfile = dwarf2_per_objfile->objfile;
4284   struct dwarf2_section_info *section = this_cu->info_or_types_section;
4285   bfd *abfd = section->asection->owner;
4286   struct dwarf2_cu *cu;
4287   gdb_byte *begin_info_ptr, *info_ptr;
4288   struct die_reader_specs reader;
4289   struct die_info *comp_unit_die;
4290   int has_children;
4291   struct attribute *attr;
4292   struct cleanup *cleanups, *free_cu_cleanup = NULL;
4293   struct signatured_type *sig_type = NULL;
4294   struct dwarf2_section_info *abbrev_section;
4295   /* Non-zero if CU currently points to a DWO file and we need to
4296      reread it.  When this happens we need to reread the skeleton die
4297      before we can reread the DWO file.  */
4298   int rereading_dwo_cu = 0;
4299
4300   if (dwarf2_die_debug)
4301     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4302                         this_cu->is_debug_types ? "type" : "comp",
4303                         this_cu->offset.sect_off);
4304
4305   if (use_existing_cu)
4306     gdb_assert (keep);
4307
4308   cleanups = make_cleanup (null_cleanup, NULL);
4309
4310   /* This is cheap if the section is already read in.  */
4311   dwarf2_read_section (objfile, section);
4312
4313   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4314
4315   abbrev_section = get_abbrev_section_for_cu (this_cu);
4316
4317   if (use_existing_cu && this_cu->cu != NULL)
4318     {
4319       cu = this_cu->cu;
4320
4321       /* If this CU is from a DWO file we need to start over, we need to
4322          refetch the attributes from the skeleton CU.
4323          This could be optimized by retrieving those attributes from when we
4324          were here the first time: the previous comp_unit_die was stored in
4325          comp_unit_obstack.  But there's no data yet that we need this
4326          optimization.  */
4327       if (cu->dwo_unit != NULL)
4328         rereading_dwo_cu = 1;
4329     }
4330   else
4331     {
4332       /* If !use_existing_cu, this_cu->cu must be NULL.  */
4333       gdb_assert (this_cu->cu == NULL);
4334
4335       cu = xmalloc (sizeof (*cu));
4336       init_one_comp_unit (cu, this_cu);
4337
4338       /* If an error occurs while loading, release our storage.  */
4339       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4340     }
4341
4342   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
4343     {
4344       /* We already have the header, there's no need to read it in again.  */
4345       info_ptr += cu->header.first_die_offset.cu_off;
4346     }
4347   else
4348     {
4349       if (this_cu->is_debug_types)
4350         {
4351           ULONGEST signature;
4352           cu_offset type_offset_in_tu;
4353
4354           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4355                                                     abbrev_section, info_ptr,
4356                                                     &signature,
4357                                                     &type_offset_in_tu);
4358
4359           /* Since per_cu is the first member of struct signatured_type,
4360              we can go from a pointer to one to a pointer to the other.  */
4361           sig_type = (struct signatured_type *) this_cu;
4362           gdb_assert (sig_type->signature == signature);
4363           gdb_assert (sig_type->type_offset_in_tu.cu_off
4364                       == type_offset_in_tu.cu_off);
4365           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4366
4367           /* LENGTH has not been set yet for type units if we're
4368              using .gdb_index.  */
4369           this_cu->length = get_cu_length (&cu->header);
4370
4371           /* Establish the type offset that can be used to lookup the type.  */
4372           sig_type->type_offset_in_section.sect_off =
4373             this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
4374         }
4375       else
4376         {
4377           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4378                                                     abbrev_section,
4379                                                     info_ptr, 0);
4380
4381           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
4382           gdb_assert (this_cu->length == get_cu_length (&cu->header));
4383         }
4384     }
4385
4386   /* Skip dummy compilation units.  */
4387   if (info_ptr >= begin_info_ptr + this_cu->length
4388       || peek_abbrev_code (abfd, info_ptr) == 0)
4389     {
4390       do_cleanups (cleanups);
4391       return;
4392     }
4393
4394   /* If we don't have them yet, read the abbrevs for this compilation unit.
4395      And if we need to read them now, make sure they're freed when we're
4396      done.  Note that it's important that if the CU had an abbrev table
4397      on entry we don't free it when we're done: Somewhere up the call stack
4398      it may be in use.  */
4399   if (abbrev_table != NULL)
4400     {
4401       gdb_assert (cu->abbrev_table == NULL);
4402       gdb_assert (cu->header.abbrev_offset.sect_off
4403                   == abbrev_table->offset.sect_off);
4404       cu->abbrev_table = abbrev_table;
4405     }
4406   else if (cu->abbrev_table == NULL)
4407     {
4408       dwarf2_read_abbrevs (cu, abbrev_section);
4409       make_cleanup (dwarf2_free_abbrev_table, cu);
4410     }
4411   else if (rereading_dwo_cu)
4412     {
4413       dwarf2_free_abbrev_table (cu);
4414       dwarf2_read_abbrevs (cu, abbrev_section);
4415     }
4416
4417   /* Read the top level CU/TU die.  */
4418   init_cu_die_reader (&reader, cu, section, NULL);
4419   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4420
4421   /* If we have a DWO stub, process it and then read in the DWO file.
4422      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4423      a DWO CU, that this test will fail.  */
4424   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4425   if (attr)
4426     {
4427       char *dwo_name = DW_STRING (attr);
4428       const char *comp_dir_string;
4429       struct dwo_unit *dwo_unit;
4430       ULONGEST signature; /* Or dwo_id.  */
4431       struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4432       int i,num_extra_attrs;
4433       struct dwarf2_section_info *dwo_abbrev_section;
4434
4435       if (has_children)
4436         error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4437                  " has children (offset 0x%x) [in module %s]"),
4438                this_cu->offset.sect_off, bfd_get_filename (abfd));
4439
4440       /* These attributes aren't processed until later:
4441          DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4442          However, the attribute is found in the stub which we won't have later.
4443          In order to not impose this complication on the rest of the code,
4444          we read them here and copy them to the DWO CU/TU die.  */
4445
4446       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4447          DWO file.  */
4448       stmt_list = NULL;
4449       if (! this_cu->is_debug_types)
4450         stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4451       low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4452       high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4453       ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
4454       comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4455
4456       /* There should be a DW_AT_addr_base attribute here (if needed).
4457          We need the value before we can process DW_FORM_GNU_addr_index.  */
4458       cu->addr_base = 0;
4459       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4460       if (attr)
4461         cu->addr_base = DW_UNSND (attr);
4462
4463       /* There should be a DW_AT_ranges_base attribute here (if needed).
4464          We need the value before we can process DW_AT_ranges.  */
4465       cu->ranges_base = 0;
4466       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4467       if (attr)
4468         cu->ranges_base = DW_UNSND (attr);
4469
4470       if (this_cu->is_debug_types)
4471         {
4472           gdb_assert (sig_type != NULL);
4473           signature = sig_type->signature;
4474         }
4475       else
4476         {
4477           attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4478           if (! attr)
4479             error (_("Dwarf Error: missing dwo_id [in module %s]"),
4480                    dwo_name);
4481           signature = DW_UNSND (attr);
4482         }
4483
4484       /* We may need the comp_dir in order to find the DWO file.  */
4485       comp_dir_string = NULL;
4486       if (comp_dir)
4487         comp_dir_string = DW_STRING (comp_dir);
4488
4489       if (this_cu->is_debug_types)
4490         dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
4491       else
4492         dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
4493                                          signature);
4494
4495       if (dwo_unit == NULL)
4496         {
4497           error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4498                    " with ID %s [in module %s]"),
4499                  this_cu->offset.sect_off,
4500                  phex (signature, sizeof (signature)),
4501                  objfile->name);
4502         }
4503
4504       /* Set up for reading the DWO CU/TU.  */
4505       cu->dwo_unit = dwo_unit;
4506       section = dwo_unit->info_or_types_section;
4507       begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4508       dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4509       init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4510
4511       if (this_cu->is_debug_types)
4512         {
4513           ULONGEST signature;
4514
4515           info_ptr = read_and_check_type_unit_head (&cu->header, section,
4516                                                     dwo_abbrev_section,
4517                                                     info_ptr,
4518                                                     &signature, NULL);
4519           gdb_assert (sig_type->signature == signature);
4520           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4521           gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4522
4523           /* Establish the type offset that can be used to lookup the type.
4524              For DWO files, we don't know it until now.  */
4525           sig_type->type_offset_in_section.sect_off =
4526             dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4527         }
4528       else
4529         {
4530           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4531                                                     dwo_abbrev_section,
4532                                                     info_ptr, 0);
4533           gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4534           gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
4535         }
4536
4537       /* Discard the original CU's abbrev table, and read the DWO's.  */
4538       if (abbrev_table == NULL)
4539         {
4540           dwarf2_free_abbrev_table (cu);
4541           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4542         }
4543       else
4544         {
4545           dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4546           make_cleanup (dwarf2_free_abbrev_table, cu);
4547         }
4548
4549       /* Read in the die, but leave space to copy over the attributes
4550          from the stub.  This has the benefit of simplifying the rest of
4551          the code - all the real work is done here.  */
4552       num_extra_attrs = ((stmt_list != NULL)
4553                          + (low_pc != NULL)
4554                          + (high_pc != NULL)
4555                          + (ranges != NULL)
4556                          + (comp_dir != NULL));
4557       info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4558                                   &has_children, num_extra_attrs);
4559
4560       /* Copy over the attributes from the stub to the DWO die.  */
4561       i = comp_unit_die->num_attrs;
4562       if (stmt_list != NULL)
4563         comp_unit_die->attrs[i++] = *stmt_list;
4564       if (low_pc != NULL)
4565         comp_unit_die->attrs[i++] = *low_pc;
4566       if (high_pc != NULL)
4567         comp_unit_die->attrs[i++] = *high_pc;
4568       if (ranges != NULL)
4569         comp_unit_die->attrs[i++] = *ranges;
4570       if (comp_dir != NULL)
4571         comp_unit_die->attrs[i++] = *comp_dir;
4572       comp_unit_die->num_attrs += num_extra_attrs;
4573
4574       /* Skip dummy compilation units.  */
4575       if (info_ptr >= begin_info_ptr + dwo_unit->length
4576           || peek_abbrev_code (abfd, info_ptr) == 0)
4577         {
4578           do_cleanups (cleanups);
4579           return;
4580         }
4581     }
4582
4583   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4584
4585   if (free_cu_cleanup != NULL)
4586     {
4587       if (keep)
4588         {
4589           /* We've successfully allocated this compilation unit.  Let our
4590              caller clean it up when finished with it.  */
4591           discard_cleanups (free_cu_cleanup);
4592
4593           /* We can only discard free_cu_cleanup and all subsequent cleanups.
4594              So we have to manually free the abbrev table.  */
4595           dwarf2_free_abbrev_table (cu);
4596
4597           /* Link this CU into read_in_chain.  */
4598           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4599           dwarf2_per_objfile->read_in_chain = this_cu;
4600         }
4601       else
4602         do_cleanups (free_cu_cleanup);
4603     }
4604
4605   do_cleanups (cleanups);
4606 }
4607
4608 /* Read CU/TU THIS_CU in section SECTION,
4609    but do not follow DW_AT_GNU_dwo_name if present.
4610    DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4611    have already done the lookup to find the DWO file).
4612
4613    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
4614    THIS_CU->is_debug_types, but nothing else.
4615
4616    We fill in THIS_CU->length.
4617
4618    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4619    linker) then DIE_READER_FUNC will not get called.
4620
4621    THIS_CU->cu is always freed when done.
4622    This is done in order to not leave THIS_CU->cu in a state where we have
4623    to care whether it refers to the "main" CU or the DWO CU.  */
4624
4625 static void
4626 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4627                                    struct dwarf2_section_info *abbrev_section,
4628                                    struct dwo_file *dwo_file,
4629                                    die_reader_func_ftype *die_reader_func,
4630                                    void *data)
4631 {
4632   struct objfile *objfile = dwarf2_per_objfile->objfile;
4633   struct dwarf2_section_info *section = this_cu->info_or_types_section;
4634   bfd *abfd = section->asection->owner;
4635   struct dwarf2_cu cu;
4636   gdb_byte *begin_info_ptr, *info_ptr;
4637   struct die_reader_specs reader;
4638   struct cleanup *cleanups;
4639   struct die_info *comp_unit_die;
4640   int has_children;
4641
4642   if (dwarf2_die_debug)
4643     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4644                         this_cu->is_debug_types ? "type" : "comp",
4645                         this_cu->offset.sect_off);
4646
4647   gdb_assert (this_cu->cu == NULL);
4648
4649   /* This is cheap if the section is already read in.  */
4650   dwarf2_read_section (objfile, section);
4651
4652   init_one_comp_unit (&cu, this_cu);
4653
4654   cleanups = make_cleanup (free_stack_comp_unit, &cu);
4655
4656   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4657   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4658                                             abbrev_section, info_ptr,
4659                                             this_cu->is_debug_types);
4660
4661   this_cu->length = get_cu_length (&cu.header);
4662
4663   /* Skip dummy compilation units.  */
4664   if (info_ptr >= begin_info_ptr + this_cu->length
4665       || peek_abbrev_code (abfd, info_ptr) == 0)
4666     {
4667       do_cleanups (cleanups);
4668       return;
4669     }
4670
4671   dwarf2_read_abbrevs (&cu, abbrev_section);
4672   make_cleanup (dwarf2_free_abbrev_table, &cu);
4673
4674   init_cu_die_reader (&reader, &cu, section, dwo_file);
4675   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4676
4677   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4678
4679   do_cleanups (cleanups);
4680 }
4681
4682 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4683    does not lookup the specified DWO file.
4684    This cannot be used to read DWO files.
4685
4686    THIS_CU->cu is always freed when done.
4687    This is done in order to not leave THIS_CU->cu in a state where we have
4688    to care whether it refers to the "main" CU or the DWO CU.
4689    We can revisit this if the data shows there's a performance issue.  */
4690
4691 static void
4692 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4693                                 die_reader_func_ftype *die_reader_func,
4694                                 void *data)
4695 {
4696   init_cutu_and_read_dies_no_follow (this_cu,
4697                                      get_abbrev_section_for_cu (this_cu),
4698                                      NULL,
4699                                      die_reader_func, data);
4700 }
4701
4702 /* Create a psymtab named NAME and assign it to PER_CU.
4703
4704    The caller must fill in the following details:
4705    dirname, textlow, texthigh.  */
4706
4707 static struct partial_symtab *
4708 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
4709 {
4710   struct objfile *objfile = per_cu->objfile;
4711   struct partial_symtab *pst;
4712
4713   pst = start_psymtab_common (objfile, objfile->section_offsets,
4714                               name, 0,
4715                               objfile->global_psymbols.next,
4716                               objfile->static_psymbols.next);
4717
4718   pst->psymtabs_addrmap_supported = 1;
4719
4720   /* This is the glue that links PST into GDB's symbol API.  */
4721   pst->read_symtab_private = per_cu;
4722   pst->read_symtab = dwarf2_psymtab_to_symtab;
4723   per_cu->v.psymtab = pst;
4724
4725   return pst;
4726 }
4727
4728 /* die_reader_func for process_psymtab_comp_unit.  */
4729
4730 static void
4731 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4732                                   gdb_byte *info_ptr,
4733                                   struct die_info *comp_unit_die,
4734                                   int has_children,
4735                                   void *data)
4736 {
4737   struct dwarf2_cu *cu = reader->cu;
4738   struct objfile *objfile = cu->objfile;
4739   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4740   struct attribute *attr;
4741   CORE_ADDR baseaddr;
4742   CORE_ADDR best_lowpc = 0, best_highpc = 0;
4743   struct partial_symtab *pst;
4744   int has_pc_info;
4745   const char *filename;
4746   int *want_partial_unit_ptr = data;
4747
4748   if (comp_unit_die->tag == DW_TAG_partial_unit
4749       && (want_partial_unit_ptr == NULL
4750           || !*want_partial_unit_ptr))
4751     return;
4752
4753   gdb_assert (! per_cu->is_debug_types);
4754
4755   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
4756
4757   cu->list_in_scope = &file_symbols;
4758
4759   /* Allocate a new partial symbol table structure.  */
4760   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
4761   if (attr == NULL || !DW_STRING (attr))
4762     filename = "";
4763   else
4764     filename = DW_STRING (attr);
4765
4766   pst = create_partial_symtab (per_cu, filename);
4767
4768   /* This must be done before calling dwarf2_build_include_psymtabs.  */
4769   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4770   if (attr != NULL)
4771     pst->dirname = DW_STRING (attr);
4772
4773   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4774
4775   dwarf2_find_base_address (comp_unit_die, cu);
4776
4777   /* Possibly set the default values of LOWPC and HIGHPC from
4778      `DW_AT_ranges'.  */
4779   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
4780                                       &best_highpc, cu, pst);
4781   if (has_pc_info == 1 && best_lowpc < best_highpc)
4782     /* Store the contiguous range if it is not empty; it can be empty for
4783        CUs with no code.  */
4784     addrmap_set_empty (objfile->psymtabs_addrmap,
4785                        best_lowpc + baseaddr,
4786                        best_highpc + baseaddr - 1, pst);
4787
4788   /* Check if comp unit has_children.
4789      If so, read the rest of the partial symbols from this comp unit.
4790      If not, there's no more debug_info for this comp unit.  */
4791   if (has_children)
4792     {
4793       struct partial_die_info *first_die;
4794       CORE_ADDR lowpc, highpc;
4795
4796       lowpc = ((CORE_ADDR) -1);
4797       highpc = ((CORE_ADDR) 0);
4798
4799       first_die = load_partial_dies (reader, info_ptr, 1);
4800
4801       scan_partial_symbols (first_die, &lowpc, &highpc,
4802                             ! has_pc_info, cu);
4803
4804       /* If we didn't find a lowpc, set it to highpc to avoid
4805          complaints from `maint check'.  */
4806       if (lowpc == ((CORE_ADDR) -1))
4807         lowpc = highpc;
4808
4809       /* If the compilation unit didn't have an explicit address range,
4810          then use the information extracted from its child dies.  */
4811       if (! has_pc_info)
4812         {
4813           best_lowpc = lowpc;
4814           best_highpc = highpc;
4815         }
4816     }
4817   pst->textlow = best_lowpc + baseaddr;
4818   pst->texthigh = best_highpc + baseaddr;
4819
4820   pst->n_global_syms = objfile->global_psymbols.next -
4821     (objfile->global_psymbols.list + pst->globals_offset);
4822   pst->n_static_syms = objfile->static_psymbols.next -
4823     (objfile->static_psymbols.list + pst->statics_offset);
4824   sort_pst_symbols (pst);
4825
4826   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs))
4827     {
4828       int i;
4829       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4830       struct dwarf2_per_cu_data *iter;
4831
4832       /* Fill in 'dependencies' here; we fill in 'users' in a
4833          post-pass.  */
4834       pst->number_of_dependencies = len;
4835       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4836                                          len * sizeof (struct symtab *));
4837       for (i = 0;
4838            VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
4839                         i, iter);
4840            ++i)
4841         pst->dependencies[i] = iter->v.psymtab;
4842
4843       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs);
4844     }
4845
4846   /* Get the list of files included in the current compilation unit,
4847      and build a psymtab for each of them.  */
4848   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
4849
4850   if (dwarf2_read_debug)
4851     {
4852       struct gdbarch *gdbarch = get_objfile_arch (objfile);
4853
4854       fprintf_unfiltered (gdb_stdlog,
4855                           "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4856                           ", %d global, %d static syms\n",
4857                           per_cu->is_debug_types ? "type" : "comp",
4858                           per_cu->offset.sect_off,
4859                           paddress (gdbarch, pst->textlow),
4860                           paddress (gdbarch, pst->texthigh),
4861                           pst->n_global_syms, pst->n_static_syms);
4862     }
4863 }
4864
4865 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4866    Process compilation unit THIS_CU for a psymtab.  */
4867
4868 static void
4869 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4870                            int want_partial_unit)
4871 {
4872   /* If this compilation unit was already read in, free the
4873      cached copy in order to read it in again.  This is
4874      necessary because we skipped some symbols when we first
4875      read in the compilation unit (see load_partial_dies).
4876      This problem could be avoided, but the benefit is unclear.  */
4877   if (this_cu->cu != NULL)
4878     free_one_cached_comp_unit (this_cu);
4879
4880   gdb_assert (! this_cu->is_debug_types);
4881   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
4882                            process_psymtab_comp_unit_reader,
4883                            &want_partial_unit);
4884
4885   /* Age out any secondary CUs.  */
4886   age_cached_comp_units ();
4887 }
4888
4889 static hashval_t
4890 hash_type_unit_group (const void *item)
4891 {
4892   const struct type_unit_group *tu_group = item;
4893
4894   return hash_stmt_list_entry (&tu_group->hash);
4895 }
4896
4897 static int
4898 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
4899 {
4900   const struct type_unit_group *lhs = item_lhs;
4901   const struct type_unit_group *rhs = item_rhs;
4902
4903   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
4904 }
4905
4906 /* Allocate a hash table for type unit groups.  */
4907
4908 static htab_t
4909 allocate_type_unit_groups_table (void)
4910 {
4911   return htab_create_alloc_ex (3,
4912                                hash_type_unit_group,
4913                                eq_type_unit_group,
4914                                NULL,
4915                                &dwarf2_per_objfile->objfile->objfile_obstack,
4916                                hashtab_obstack_allocate,
4917                                dummy_obstack_deallocate);
4918 }
4919
4920 /* Type units that don't have DW_AT_stmt_list are grouped into their own
4921    partial symtabs.  We combine several TUs per psymtab to not let the size
4922    of any one psymtab grow too big.  */
4923 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
4924 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
4925
4926 /* Helper routine for get_type_unit_group.
4927    Create the type_unit_group object used to hold one or more TUs.  */
4928
4929 static struct type_unit_group *
4930 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
4931 {
4932   struct objfile *objfile = dwarf2_per_objfile->objfile;
4933   struct dwarf2_per_cu_data *per_cu;
4934   struct type_unit_group *tu_group;
4935
4936   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4937                              struct type_unit_group);
4938   per_cu = &tu_group->per_cu;
4939   per_cu->objfile = objfile;
4940   per_cu->is_debug_types = 1;
4941   per_cu->s.type_unit_group = tu_group;
4942
4943   if (dwarf2_per_objfile->using_index)
4944     {
4945       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4946                                         struct dwarf2_per_cu_quick_data);
4947       tu_group->t.first_tu = cu->per_cu;
4948     }
4949   else
4950     {
4951       unsigned int line_offset = line_offset_struct.sect_off;
4952       struct partial_symtab *pst;
4953       char *name;
4954
4955       /* Give the symtab a useful name for debug purposes.  */
4956       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
4957         name = xstrprintf ("<type_units_%d>",
4958                            (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
4959       else
4960         name = xstrprintf ("<type_units_at_0x%x>", line_offset);
4961
4962       pst = create_partial_symtab (per_cu, name);
4963       pst->anonymous = 1;
4964
4965       xfree (name);
4966     }
4967
4968   tu_group->hash.dwo_unit = cu->dwo_unit;
4969   tu_group->hash.line_offset = line_offset_struct;
4970
4971   return tu_group;
4972 }
4973
4974 /* Look up the type_unit_group for type unit CU, and create it if necessary.
4975    STMT_LIST is a DW_AT_stmt_list attribute.  */
4976
4977 static struct type_unit_group *
4978 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
4979 {
4980   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
4981   struct type_unit_group *tu_group;
4982   void **slot;
4983   unsigned int line_offset;
4984   struct type_unit_group type_unit_group_for_lookup;
4985
4986   if (dwarf2_per_objfile->type_unit_groups == NULL)
4987     {
4988       dwarf2_per_objfile->type_unit_groups =
4989         allocate_type_unit_groups_table ();
4990     }
4991
4992   /* Do we need to create a new group, or can we use an existing one?  */
4993
4994   if (stmt_list)
4995     {
4996       line_offset = DW_UNSND (stmt_list);
4997       ++tu_stats->nr_symtab_sharers;
4998     }
4999   else
5000     {
5001       /* Ugh, no stmt_list.  Rare, but we have to handle it.
5002          We can do various things here like create one group per TU or
5003          spread them over multiple groups to split up the expansion work.
5004          To avoid worst case scenarios (too many groups or too large groups)
5005          we, umm, group them in bunches.  */
5006       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5007                      | (tu_stats->nr_stmt_less_type_units
5008                         / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5009       ++tu_stats->nr_stmt_less_type_units;
5010     }
5011
5012   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5013   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5014   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5015                          &type_unit_group_for_lookup, INSERT);
5016   if (*slot != NULL)
5017     {
5018       tu_group = *slot;
5019       gdb_assert (tu_group != NULL);
5020     }
5021   else
5022     {
5023       sect_offset line_offset_struct;
5024
5025       line_offset_struct.sect_off = line_offset;
5026       tu_group = create_type_unit_group (cu, line_offset_struct);
5027       *slot = tu_group;
5028       ++tu_stats->nr_symtabs;
5029     }
5030
5031   return tu_group;
5032 }
5033
5034 /* Struct used to sort TUs by their abbreviation table offset.  */
5035
5036 struct tu_abbrev_offset
5037 {
5038   struct signatured_type *sig_type;
5039   sect_offset abbrev_offset;
5040 };
5041
5042 /* Helper routine for build_type_unit_groups, passed to qsort.  */
5043
5044 static int
5045 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5046 {
5047   const struct tu_abbrev_offset * const *a = ap;
5048   const struct tu_abbrev_offset * const *b = bp;
5049   unsigned int aoff = (*a)->abbrev_offset.sect_off;
5050   unsigned int boff = (*b)->abbrev_offset.sect_off;
5051
5052   return (aoff > boff) - (aoff < boff);
5053 }
5054
5055 /* A helper function to add a type_unit_group to a table.  */
5056
5057 static int
5058 add_type_unit_group_to_table (void **slot, void *datum)
5059 {
5060   struct type_unit_group *tu_group = *slot;
5061   struct type_unit_group ***datap = datum;
5062
5063   **datap = tu_group;
5064   ++*datap;
5065
5066   return 1;
5067 }
5068
5069 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5070    each one passing FUNC,DATA.
5071
5072    The efficiency is because we sort TUs by the abbrev table they use and
5073    only read each abbrev table once.  In one program there are 200K TUs
5074    sharing 8K abbrev tables.
5075
5076    The main purpose of this function is to support building the
5077    dwarf2_per_objfile->type_unit_groups table.
5078    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5079    can collapse the search space by grouping them by stmt_list.
5080    The savings can be significant, in the same program from above the 200K TUs
5081    share 8K stmt_list tables.
5082
5083    FUNC is expected to call get_type_unit_group, which will create the
5084    struct type_unit_group if necessary and add it to
5085    dwarf2_per_objfile->type_unit_groups.  */
5086
5087 static void
5088 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5089 {
5090   struct objfile *objfile = dwarf2_per_objfile->objfile;
5091   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5092   struct cleanup *cleanups;
5093   struct abbrev_table *abbrev_table;
5094   sect_offset abbrev_offset;
5095   struct tu_abbrev_offset *sorted_by_abbrev;
5096   struct type_unit_group **iter;
5097   int i;
5098
5099   /* It's up to the caller to not call us multiple times.  */
5100   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5101
5102   if (dwarf2_per_objfile->n_type_units == 0)
5103     return;
5104
5105   /* TUs typically share abbrev tables, and there can be way more TUs than
5106      abbrev tables.  Sort by abbrev table to reduce the number of times we
5107      read each abbrev table in.
5108      Alternatives are to punt or to maintain a cache of abbrev tables.
5109      This is simpler and efficient enough for now.
5110
5111      Later we group TUs by their DW_AT_stmt_list value (as this defines the
5112      symtab to use).  Typically TUs with the same abbrev offset have the same
5113      stmt_list value too so in practice this should work well.
5114
5115      The basic algorithm here is:
5116
5117       sort TUs by abbrev table
5118       for each TU with same abbrev table:
5119         read abbrev table if first user
5120         read TU top level DIE
5121           [IWBN if DWO skeletons had DW_AT_stmt_list]
5122         call FUNC  */
5123
5124   if (dwarf2_read_debug)
5125     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5126
5127   /* Sort in a separate table to maintain the order of all_type_units
5128      for .gdb_index: TU indices directly index all_type_units.  */
5129   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5130                               dwarf2_per_objfile->n_type_units);
5131   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5132     {
5133       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5134
5135       sorted_by_abbrev[i].sig_type = sig_type;
5136       sorted_by_abbrev[i].abbrev_offset =
5137         read_abbrev_offset (sig_type->per_cu.info_or_types_section,
5138                             sig_type->per_cu.offset);
5139     }
5140   cleanups = make_cleanup (xfree, sorted_by_abbrev);
5141   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5142          sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5143
5144   /* Note: In the .gdb_index case, get_type_unit_group may have already been
5145      called any number of times, so we don't reset tu_stats here.  */
5146
5147   abbrev_offset.sect_off = ~(unsigned) 0;
5148   abbrev_table = NULL;
5149   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5150
5151   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5152     {
5153       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5154
5155       /* Switch to the next abbrev table if necessary.  */
5156       if (abbrev_table == NULL
5157           || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5158         {
5159           if (abbrev_table != NULL)
5160             {
5161               abbrev_table_free (abbrev_table);
5162               /* Reset to NULL in case abbrev_table_read_table throws
5163                  an error: abbrev_table_free_cleanup will get called.  */
5164               abbrev_table = NULL;
5165             }
5166           abbrev_offset = tu->abbrev_offset;
5167           abbrev_table =
5168             abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5169                                      abbrev_offset);
5170           ++tu_stats->nr_uniq_abbrev_tables;
5171         }
5172
5173       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5174                                func, data);
5175     }
5176
5177   /* Create a vector of pointers to primary type units to make it easy to
5178      iterate over them and CUs.  See dw2_get_primary_cu.  */
5179   dwarf2_per_objfile->n_type_unit_groups =
5180     htab_elements (dwarf2_per_objfile->type_unit_groups);
5181   dwarf2_per_objfile->all_type_unit_groups =
5182     obstack_alloc (&objfile->objfile_obstack,
5183                    dwarf2_per_objfile->n_type_unit_groups
5184                    * sizeof (struct type_unit_group *));
5185   iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5186   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5187                           add_type_unit_group_to_table, &iter);
5188   gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5189               == dwarf2_per_objfile->n_type_unit_groups);
5190
5191   do_cleanups (cleanups);
5192
5193   if (dwarf2_read_debug)
5194     {
5195       fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5196       fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
5197                           dwarf2_per_objfile->n_type_units);
5198       fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
5199                           tu_stats->nr_uniq_abbrev_tables);
5200       fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
5201                           tu_stats->nr_symtabs);
5202       fprintf_unfiltered (gdb_stdlog, "  %d symtab sharers\n",
5203                           tu_stats->nr_symtab_sharers);
5204       fprintf_unfiltered (gdb_stdlog, "  %d type units without a stmt_list\n",
5205                           tu_stats->nr_stmt_less_type_units);
5206     }
5207 }
5208
5209 /* Reader function for build_type_psymtabs.  */
5210
5211 static void
5212 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5213                             gdb_byte *info_ptr,
5214                             struct die_info *type_unit_die,
5215                             int has_children,
5216                             void *data)
5217 {
5218   struct objfile *objfile = dwarf2_per_objfile->objfile;
5219   struct dwarf2_cu *cu = reader->cu;
5220   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5221   struct type_unit_group *tu_group;
5222   struct attribute *attr;
5223   struct partial_die_info *first_die;
5224   CORE_ADDR lowpc, highpc;
5225   struct partial_symtab *pst;
5226
5227   gdb_assert (data == NULL);
5228
5229   if (! has_children)
5230     return;
5231
5232   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5233   tu_group = get_type_unit_group (cu, attr);
5234
5235   VEC_safe_push (dwarf2_per_cu_ptr, tu_group->t.tus, per_cu);
5236
5237   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5238   cu->list_in_scope = &file_symbols;
5239   pst = create_partial_symtab (per_cu, "");
5240   pst->anonymous = 1;
5241
5242   first_die = load_partial_dies (reader, info_ptr, 1);
5243
5244   lowpc = (CORE_ADDR) -1;
5245   highpc = (CORE_ADDR) 0;
5246   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5247
5248   pst->n_global_syms = objfile->global_psymbols.next -
5249     (objfile->global_psymbols.list + pst->globals_offset);
5250   pst->n_static_syms = objfile->static_psymbols.next -
5251     (objfile->static_psymbols.list + pst->statics_offset);
5252   sort_pst_symbols (pst);
5253 }
5254
5255 /* Traversal function for build_type_psymtabs.  */
5256
5257 static int
5258 build_type_psymtab_dependencies (void **slot, void *info)
5259 {
5260   struct objfile *objfile = dwarf2_per_objfile->objfile;
5261   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5262   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5263   struct partial_symtab *pst = per_cu->v.psymtab;
5264   int len = VEC_length (dwarf2_per_cu_ptr, tu_group->t.tus);
5265   struct dwarf2_per_cu_data *iter;
5266   int i;
5267
5268   gdb_assert (len > 0);
5269
5270   pst->number_of_dependencies = len;
5271   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5272                                      len * sizeof (struct psymtab *));
5273   for (i = 0;
5274        VEC_iterate (dwarf2_per_cu_ptr, tu_group->t.tus, i, iter);
5275        ++i)
5276     {
5277       pst->dependencies[i] = iter->v.psymtab;
5278       iter->s.type_unit_group = tu_group;
5279     }
5280
5281   VEC_free (dwarf2_per_cu_ptr, tu_group->t.tus);
5282
5283   return 1;
5284 }
5285
5286 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5287    Build partial symbol tables for the .debug_types comp-units.  */
5288
5289 static void
5290 build_type_psymtabs (struct objfile *objfile)
5291 {
5292   if (! create_all_type_units (objfile))
5293     return;
5294
5295   build_type_unit_groups (build_type_psymtabs_reader, NULL);
5296
5297   /* Now that all TUs have been processed we can fill in the dependencies.  */
5298   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5299                           build_type_psymtab_dependencies, NULL);
5300 }
5301
5302 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
5303
5304 static void
5305 psymtabs_addrmap_cleanup (void *o)
5306 {
5307   struct objfile *objfile = o;
5308
5309   objfile->psymtabs_addrmap = NULL;
5310 }
5311
5312 /* Compute the 'user' field for each psymtab in OBJFILE.  */
5313
5314 static void
5315 set_partial_user (struct objfile *objfile)
5316 {
5317   int i;
5318
5319   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5320     {
5321       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5322       struct partial_symtab *pst = per_cu->v.psymtab;
5323       int j;
5324
5325       if (pst == NULL)
5326         continue;
5327
5328       for (j = 0; j < pst->number_of_dependencies; ++j)
5329         {
5330           /* Set the 'user' field only if it is not already set.  */
5331           if (pst->dependencies[j]->user == NULL)
5332             pst->dependencies[j]->user = pst;
5333         }
5334     }
5335 }
5336
5337 /* Build the partial symbol table by doing a quick pass through the
5338    .debug_info and .debug_abbrev sections.  */
5339
5340 static void
5341 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5342 {
5343   struct cleanup *back_to, *addrmap_cleanup;
5344   struct obstack temp_obstack;
5345   int i;
5346
5347   if (dwarf2_read_debug)
5348     {
5349       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5350                           objfile->name);
5351     }
5352
5353   dwarf2_per_objfile->reading_partial_symbols = 1;
5354
5355   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5356
5357   /* Any cached compilation units will be linked by the per-objfile
5358      read_in_chain.  Make sure to free them when we're done.  */
5359   back_to = make_cleanup (free_cached_comp_units, NULL);
5360
5361   build_type_psymtabs (objfile);
5362
5363   create_all_comp_units (objfile);
5364
5365   /* Create a temporary address map on a temporary obstack.  We later
5366      copy this to the final obstack.  */
5367   obstack_init (&temp_obstack);
5368   make_cleanup_obstack_free (&temp_obstack);
5369   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5370   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5371
5372   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5373     {
5374       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5375
5376       process_psymtab_comp_unit (per_cu, 0);
5377     }
5378
5379   set_partial_user (objfile);
5380
5381   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5382                                                     &objfile->objfile_obstack);
5383   discard_cleanups (addrmap_cleanup);
5384
5385   do_cleanups (back_to);
5386
5387   if (dwarf2_read_debug)
5388     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
5389                         objfile->name);
5390 }
5391
5392 /* die_reader_func for load_partial_comp_unit.  */
5393
5394 static void
5395 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
5396                                gdb_byte *info_ptr,
5397                                struct die_info *comp_unit_die,
5398                                int has_children,
5399                                void *data)
5400 {
5401   struct dwarf2_cu *cu = reader->cu;
5402
5403   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5404
5405   /* Check if comp unit has_children.
5406      If so, read the rest of the partial symbols from this comp unit.
5407      If not, there's no more debug_info for this comp unit.  */
5408   if (has_children)
5409     load_partial_dies (reader, info_ptr, 0);
5410 }
5411
5412 /* Load the partial DIEs for a secondary CU into memory.
5413    This is also used when rereading a primary CU with load_all_dies.  */
5414
5415 static void
5416 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
5417 {
5418   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
5419                            load_partial_comp_unit_reader, NULL);
5420 }
5421
5422 static void
5423 read_comp_units_from_section (struct objfile *objfile,
5424                               struct dwarf2_section_info *section,
5425                               unsigned int is_dwz,
5426                               int *n_allocated,
5427                               int *n_comp_units,
5428                               struct dwarf2_per_cu_data ***all_comp_units)
5429 {
5430   gdb_byte *info_ptr;
5431   bfd *abfd = section->asection->owner;
5432
5433   dwarf2_read_section (objfile, section);
5434
5435   info_ptr = section->buffer;
5436
5437   while (info_ptr < section->buffer + section->size)
5438     {
5439       unsigned int length, initial_length_size;
5440       struct dwarf2_per_cu_data *this_cu;
5441       sect_offset offset;
5442
5443       offset.sect_off = info_ptr - section->buffer;
5444
5445       /* Read just enough information to find out where the next
5446          compilation unit is.  */
5447       length = read_initial_length (abfd, info_ptr, &initial_length_size);
5448
5449       /* Save the compilation unit for later lookup.  */
5450       this_cu = obstack_alloc (&objfile->objfile_obstack,
5451                                sizeof (struct dwarf2_per_cu_data));
5452       memset (this_cu, 0, sizeof (*this_cu));
5453       this_cu->offset = offset;
5454       this_cu->length = length + initial_length_size;
5455       this_cu->is_dwz = is_dwz;
5456       this_cu->objfile = objfile;
5457       this_cu->info_or_types_section = section;
5458
5459       if (*n_comp_units == *n_allocated)
5460         {
5461           *n_allocated *= 2;
5462           *all_comp_units = xrealloc (*all_comp_units,
5463                                       *n_allocated
5464                                       * sizeof (struct dwarf2_per_cu_data *));
5465         }
5466       (*all_comp_units)[*n_comp_units] = this_cu;
5467       ++*n_comp_units;
5468
5469       info_ptr = info_ptr + this_cu->length;
5470     }
5471 }
5472
5473 /* Create a list of all compilation units in OBJFILE.
5474    This is only done for -readnow and building partial symtabs.  */
5475
5476 static void
5477 create_all_comp_units (struct objfile *objfile)
5478 {
5479   int n_allocated;
5480   int n_comp_units;
5481   struct dwarf2_per_cu_data **all_comp_units;
5482
5483   n_comp_units = 0;
5484   n_allocated = 10;
5485   all_comp_units = xmalloc (n_allocated
5486                             * sizeof (struct dwarf2_per_cu_data *));
5487
5488   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
5489                                 &n_allocated, &n_comp_units, &all_comp_units);
5490
5491   if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
5492     {
5493       struct dwz_file *dwz = dwarf2_get_dwz_file ();
5494
5495       read_comp_units_from_section (objfile, &dwz->info, 1,
5496                                     &n_allocated, &n_comp_units,
5497                                     &all_comp_units);
5498     }
5499
5500   dwarf2_per_objfile->all_comp_units
5501     = obstack_alloc (&objfile->objfile_obstack,
5502                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5503   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
5504           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
5505   xfree (all_comp_units);
5506   dwarf2_per_objfile->n_comp_units = n_comp_units;
5507 }
5508
5509 /* Process all loaded DIEs for compilation unit CU, starting at
5510    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
5511    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
5512    DW_AT_ranges).  If NEED_PC is set, then this function will set
5513    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
5514    and record the covered ranges in the addrmap.  */
5515
5516 static void
5517 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5518                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5519 {
5520   struct partial_die_info *pdi;
5521
5522   /* Now, march along the PDI's, descending into ones which have
5523      interesting children but skipping the children of the other ones,
5524      until we reach the end of the compilation unit.  */
5525
5526   pdi = first_die;
5527
5528   while (pdi != NULL)
5529     {
5530       fixup_partial_die (pdi, cu);
5531
5532       /* Anonymous namespaces or modules have no name but have interesting
5533          children, so we need to look at them.  Ditto for anonymous
5534          enums.  */
5535
5536       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
5537           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
5538           || pdi->tag == DW_TAG_imported_unit)
5539         {
5540           switch (pdi->tag)
5541             {
5542             case DW_TAG_subprogram:
5543               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
5544               break;
5545             case DW_TAG_constant:
5546             case DW_TAG_variable:
5547             case DW_TAG_typedef:
5548             case DW_TAG_union_type:
5549               if (!pdi->is_declaration)
5550                 {
5551                   add_partial_symbol (pdi, cu);
5552                 }
5553               break;
5554             case DW_TAG_class_type:
5555             case DW_TAG_interface_type:
5556             case DW_TAG_structure_type:
5557               if (!pdi->is_declaration)
5558                 {
5559                   add_partial_symbol (pdi, cu);
5560                 }
5561               break;
5562             case DW_TAG_enumeration_type:
5563               if (!pdi->is_declaration)
5564                 add_partial_enumeration (pdi, cu);
5565               break;
5566             case DW_TAG_base_type:
5567             case DW_TAG_subrange_type:
5568               /* File scope base type definitions are added to the partial
5569                  symbol table.  */
5570               add_partial_symbol (pdi, cu);
5571               break;
5572             case DW_TAG_namespace:
5573               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
5574               break;
5575             case DW_TAG_module:
5576               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
5577               break;
5578             case DW_TAG_imported_unit:
5579               {
5580                 struct dwarf2_per_cu_data *per_cu;
5581
5582                 /* For now we don't handle imported units in type units.  */
5583                 if (cu->per_cu->is_debug_types)
5584                   {
5585                     error (_("Dwarf Error: DW_TAG_imported_unit is not"
5586                              " supported in type units [in module %s]"),
5587                            cu->objfile->name);
5588                   }
5589
5590                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
5591                                                            pdi->is_dwz,
5592                                                            cu->objfile);
5593
5594                 /* Go read the partial unit, if needed.  */
5595                 if (per_cu->v.psymtab == NULL)
5596                   process_psymtab_comp_unit (per_cu, 1);
5597
5598                 VEC_safe_push (dwarf2_per_cu_ptr,
5599                                cu->per_cu->s.imported_symtabs, per_cu);
5600               }
5601               break;
5602             default:
5603               break;
5604             }
5605         }
5606
5607       /* If the die has a sibling, skip to the sibling.  */
5608
5609       pdi = pdi->die_sibling;
5610     }
5611 }
5612
5613 /* Functions used to compute the fully scoped name of a partial DIE.
5614
5615    Normally, this is simple.  For C++, the parent DIE's fully scoped
5616    name is concatenated with "::" and the partial DIE's name.  For
5617    Java, the same thing occurs except that "." is used instead of "::".
5618    Enumerators are an exception; they use the scope of their parent
5619    enumeration type, i.e. the name of the enumeration type is not
5620    prepended to the enumerator.
5621
5622    There are two complexities.  One is DW_AT_specification; in this
5623    case "parent" means the parent of the target of the specification,
5624    instead of the direct parent of the DIE.  The other is compilers
5625    which do not emit DW_TAG_namespace; in this case we try to guess
5626    the fully qualified name of structure types from their members'
5627    linkage names.  This must be done using the DIE's children rather
5628    than the children of any DW_AT_specification target.  We only need
5629    to do this for structures at the top level, i.e. if the target of
5630    any DW_AT_specification (if any; otherwise the DIE itself) does not
5631    have a parent.  */
5632
5633 /* Compute the scope prefix associated with PDI's parent, in
5634    compilation unit CU.  The result will be allocated on CU's
5635    comp_unit_obstack, or a copy of the already allocated PDI->NAME
5636    field.  NULL is returned if no prefix is necessary.  */
5637 static char *
5638 partial_die_parent_scope (struct partial_die_info *pdi,
5639                           struct dwarf2_cu *cu)
5640 {
5641   char *grandparent_scope;
5642   struct partial_die_info *parent, *real_pdi;
5643
5644   /* We need to look at our parent DIE; if we have a DW_AT_specification,
5645      then this means the parent of the specification DIE.  */
5646
5647   real_pdi = pdi;
5648   while (real_pdi->has_specification)
5649     real_pdi = find_partial_die (real_pdi->spec_offset,
5650                                  real_pdi->spec_is_dwz, cu);
5651
5652   parent = real_pdi->die_parent;
5653   if (parent == NULL)
5654     return NULL;
5655
5656   if (parent->scope_set)
5657     return parent->scope;
5658
5659   fixup_partial_die (parent, cu);
5660
5661   grandparent_scope = partial_die_parent_scope (parent, cu);
5662
5663   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
5664      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
5665      Work around this problem here.  */
5666   if (cu->language == language_cplus
5667       && parent->tag == DW_TAG_namespace
5668       && strcmp (parent->name, "::") == 0
5669       && grandparent_scope == NULL)
5670     {
5671       parent->scope = NULL;
5672       parent->scope_set = 1;
5673       return NULL;
5674     }
5675
5676   if (pdi->tag == DW_TAG_enumerator)
5677     /* Enumerators should not get the name of the enumeration as a prefix.  */
5678     parent->scope = grandparent_scope;
5679   else if (parent->tag == DW_TAG_namespace
5680       || parent->tag == DW_TAG_module
5681       || parent->tag == DW_TAG_structure_type
5682       || parent->tag == DW_TAG_class_type
5683       || parent->tag == DW_TAG_interface_type
5684       || parent->tag == DW_TAG_union_type
5685       || parent->tag == DW_TAG_enumeration_type)
5686     {
5687       if (grandparent_scope == NULL)
5688         parent->scope = parent->name;
5689       else
5690         parent->scope = typename_concat (&cu->comp_unit_obstack,
5691                                          grandparent_scope,
5692                                          parent->name, 0, cu);
5693     }
5694   else
5695     {
5696       /* FIXME drow/2004-04-01: What should we be doing with
5697          function-local names?  For partial symbols, we should probably be
5698          ignoring them.  */
5699       complaint (&symfile_complaints,
5700                  _("unhandled containing DIE tag %d for DIE at %d"),
5701                  parent->tag, pdi->offset.sect_off);
5702       parent->scope = grandparent_scope;
5703     }
5704
5705   parent->scope_set = 1;
5706   return parent->scope;
5707 }
5708
5709 /* Return the fully scoped name associated with PDI, from compilation unit
5710    CU.  The result will be allocated with malloc.  */
5711
5712 static char *
5713 partial_die_full_name (struct partial_die_info *pdi,
5714                        struct dwarf2_cu *cu)
5715 {
5716   char *parent_scope;
5717
5718   /* If this is a template instantiation, we can not work out the
5719      template arguments from partial DIEs.  So, unfortunately, we have
5720      to go through the full DIEs.  At least any work we do building
5721      types here will be reused if full symbols are loaded later.  */
5722   if (pdi->has_template_arguments)
5723     {
5724       fixup_partial_die (pdi, cu);
5725
5726       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
5727         {
5728           struct die_info *die;
5729           struct attribute attr;
5730           struct dwarf2_cu *ref_cu = cu;
5731
5732           /* DW_FORM_ref_addr is using section offset.  */
5733           attr.name = 0;
5734           attr.form = DW_FORM_ref_addr;
5735           attr.u.unsnd = pdi->offset.sect_off;
5736           die = follow_die_ref (NULL, &attr, &ref_cu);
5737
5738           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
5739         }
5740     }
5741
5742   parent_scope = partial_die_parent_scope (pdi, cu);
5743   if (parent_scope == NULL)
5744     return NULL;
5745   else
5746     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
5747 }
5748
5749 static void
5750 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
5751 {
5752   struct objfile *objfile = cu->objfile;
5753   CORE_ADDR addr = 0;
5754   char *actual_name = NULL;
5755   CORE_ADDR baseaddr;
5756   int built_actual_name = 0;
5757
5758   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5759
5760   actual_name = partial_die_full_name (pdi, cu);
5761   if (actual_name)
5762     built_actual_name = 1;
5763
5764   if (actual_name == NULL)
5765     actual_name = pdi->name;
5766
5767   switch (pdi->tag)
5768     {
5769     case DW_TAG_subprogram:
5770       if (pdi->is_external || cu->language == language_ada)
5771         {
5772           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
5773              of the global scope.  But in Ada, we want to be able to access
5774              nested procedures globally.  So all Ada subprograms are stored
5775              in the global scope.  */
5776           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5777              mst_text, objfile); */
5778           add_psymbol_to_list (actual_name, strlen (actual_name),
5779                                built_actual_name,
5780                                VAR_DOMAIN, LOC_BLOCK,
5781                                &objfile->global_psymbols,
5782                                0, pdi->lowpc + baseaddr,
5783                                cu->language, objfile);
5784         }
5785       else
5786         {
5787           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
5788              mst_file_text, objfile); */
5789           add_psymbol_to_list (actual_name, strlen (actual_name),
5790                                built_actual_name,
5791                                VAR_DOMAIN, LOC_BLOCK,
5792                                &objfile->static_psymbols,
5793                                0, pdi->lowpc + baseaddr,
5794                                cu->language, objfile);
5795         }
5796       break;
5797     case DW_TAG_constant:
5798       {
5799         struct psymbol_allocation_list *list;
5800
5801         if (pdi->is_external)
5802           list = &objfile->global_psymbols;
5803         else
5804           list = &objfile->static_psymbols;
5805         add_psymbol_to_list (actual_name, strlen (actual_name),
5806                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
5807                              list, 0, 0, cu->language, objfile);
5808       }
5809       break;
5810     case DW_TAG_variable:
5811       if (pdi->d.locdesc)
5812         addr = decode_locdesc (pdi->d.locdesc, cu);
5813
5814       if (pdi->d.locdesc
5815           && addr == 0
5816           && !dwarf2_per_objfile->has_section_at_zero)
5817         {
5818           /* A global or static variable may also have been stripped
5819              out by the linker if unused, in which case its address
5820              will be nullified; do not add such variables into partial
5821              symbol table then.  */
5822         }
5823       else if (pdi->is_external)
5824         {
5825           /* Global Variable.
5826              Don't enter into the minimal symbol tables as there is
5827              a minimal symbol table entry from the ELF symbols already.
5828              Enter into partial symbol table if it has a location
5829              descriptor or a type.
5830              If the location descriptor is missing, new_symbol will create
5831              a LOC_UNRESOLVED symbol, the address of the variable will then
5832              be determined from the minimal symbol table whenever the variable
5833              is referenced.
5834              The address for the partial symbol table entry is not
5835              used by GDB, but it comes in handy for debugging partial symbol
5836              table building.  */
5837
5838           if (pdi->d.locdesc || pdi->has_type)
5839             add_psymbol_to_list (actual_name, strlen (actual_name),
5840                                  built_actual_name,
5841                                  VAR_DOMAIN, LOC_STATIC,
5842                                  &objfile->global_psymbols,
5843                                  0, addr + baseaddr,
5844                                  cu->language, objfile);
5845         }
5846       else
5847         {
5848           /* Static Variable.  Skip symbols without location descriptors.  */
5849           if (pdi->d.locdesc == NULL)
5850             {
5851               if (built_actual_name)
5852                 xfree (actual_name);
5853               return;
5854             }
5855           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
5856              mst_file_data, objfile); */
5857           add_psymbol_to_list (actual_name, strlen (actual_name),
5858                                built_actual_name,
5859                                VAR_DOMAIN, LOC_STATIC,
5860                                &objfile->static_psymbols,
5861                                0, addr + baseaddr,
5862                                cu->language, objfile);
5863         }
5864       break;
5865     case DW_TAG_typedef:
5866     case DW_TAG_base_type:
5867     case DW_TAG_subrange_type:
5868       add_psymbol_to_list (actual_name, strlen (actual_name),
5869                            built_actual_name,
5870                            VAR_DOMAIN, LOC_TYPEDEF,
5871                            &objfile->static_psymbols,
5872                            0, (CORE_ADDR) 0, cu->language, objfile);
5873       break;
5874     case DW_TAG_namespace:
5875       add_psymbol_to_list (actual_name, strlen (actual_name),
5876                            built_actual_name,
5877                            VAR_DOMAIN, LOC_TYPEDEF,
5878                            &objfile->global_psymbols,
5879                            0, (CORE_ADDR) 0, cu->language, objfile);
5880       break;
5881     case DW_TAG_class_type:
5882     case DW_TAG_interface_type:
5883     case DW_TAG_structure_type:
5884     case DW_TAG_union_type:
5885     case DW_TAG_enumeration_type:
5886       /* Skip external references.  The DWARF standard says in the section
5887          about "Structure, Union, and Class Type Entries": "An incomplete
5888          structure, union or class type is represented by a structure,
5889          union or class entry that does not have a byte size attribute
5890          and that has a DW_AT_declaration attribute."  */
5891       if (!pdi->has_byte_size && pdi->is_declaration)
5892         {
5893           if (built_actual_name)
5894             xfree (actual_name);
5895           return;
5896         }
5897
5898       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5899          static vs. global.  */
5900       add_psymbol_to_list (actual_name, strlen (actual_name),
5901                            built_actual_name,
5902                            STRUCT_DOMAIN, LOC_TYPEDEF,
5903                            (cu->language == language_cplus
5904                             || cu->language == language_java)
5905                            ? &objfile->global_psymbols
5906                            : &objfile->static_psymbols,
5907                            0, (CORE_ADDR) 0, cu->language, objfile);
5908
5909       break;
5910     case DW_TAG_enumerator:
5911       add_psymbol_to_list (actual_name, strlen (actual_name),
5912                            built_actual_name,
5913                            VAR_DOMAIN, LOC_CONST,
5914                            (cu->language == language_cplus
5915                             || cu->language == language_java)
5916                            ? &objfile->global_psymbols
5917                            : &objfile->static_psymbols,
5918                            0, (CORE_ADDR) 0, cu->language, objfile);
5919       break;
5920     default:
5921       break;
5922     }
5923
5924   if (built_actual_name)
5925     xfree (actual_name);
5926 }
5927
5928 /* Read a partial die corresponding to a namespace; also, add a symbol
5929    corresponding to that namespace to the symbol table.  NAMESPACE is
5930    the name of the enclosing namespace.  */
5931
5932 static void
5933 add_partial_namespace (struct partial_die_info *pdi,
5934                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
5935                        int need_pc, struct dwarf2_cu *cu)
5936 {
5937   /* Add a symbol for the namespace.  */
5938
5939   add_partial_symbol (pdi, cu);
5940
5941   /* Now scan partial symbols in that namespace.  */
5942
5943   if (pdi->has_children)
5944     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5945 }
5946
5947 /* Read a partial die corresponding to a Fortran module.  */
5948
5949 static void
5950 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5951                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5952 {
5953   /* Now scan partial symbols in that module.  */
5954
5955   if (pdi->has_children)
5956     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5957 }
5958
5959 /* Read a partial die corresponding to a subprogram and create a partial
5960    symbol for that subprogram.  When the CU language allows it, this
5961    routine also defines a partial symbol for each nested subprogram
5962    that this subprogram contains.
5963
5964    DIE my also be a lexical block, in which case we simply search
5965    recursively for suprograms defined inside that lexical block.
5966    Again, this is only performed when the CU language allows this
5967    type of definitions.  */
5968
5969 static void
5970 add_partial_subprogram (struct partial_die_info *pdi,
5971                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
5972                         int need_pc, struct dwarf2_cu *cu)
5973 {
5974   if (pdi->tag == DW_TAG_subprogram)
5975     {
5976       if (pdi->has_pc_info)
5977         {
5978           if (pdi->lowpc < *lowpc)
5979             *lowpc = pdi->lowpc;
5980           if (pdi->highpc > *highpc)
5981             *highpc = pdi->highpc;
5982           if (need_pc)
5983             {
5984               CORE_ADDR baseaddr;
5985               struct objfile *objfile = cu->objfile;
5986
5987               baseaddr = ANOFFSET (objfile->section_offsets,
5988                                    SECT_OFF_TEXT (objfile));
5989               addrmap_set_empty (objfile->psymtabs_addrmap,
5990                                  pdi->lowpc + baseaddr,
5991                                  pdi->highpc - 1 + baseaddr,
5992                                  cu->per_cu->v.psymtab);
5993             }
5994         }
5995
5996       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5997         {
5998           if (!pdi->is_declaration)
5999             /* Ignore subprogram DIEs that do not have a name, they are
6000                illegal.  Do not emit a complaint at this point, we will
6001                do so when we convert this psymtab into a symtab.  */
6002             if (pdi->name)
6003               add_partial_symbol (pdi, cu);
6004         }
6005     }
6006
6007   if (! pdi->has_children)
6008     return;
6009
6010   if (cu->language == language_ada)
6011     {
6012       pdi = pdi->die_child;
6013       while (pdi != NULL)
6014         {
6015           fixup_partial_die (pdi, cu);
6016           if (pdi->tag == DW_TAG_subprogram
6017               || pdi->tag == DW_TAG_lexical_block)
6018             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6019           pdi = pdi->die_sibling;
6020         }
6021     }
6022 }
6023
6024 /* Read a partial die corresponding to an enumeration type.  */
6025
6026 static void
6027 add_partial_enumeration (struct partial_die_info *enum_pdi,
6028                          struct dwarf2_cu *cu)
6029 {
6030   struct partial_die_info *pdi;
6031
6032   if (enum_pdi->name != NULL)
6033     add_partial_symbol (enum_pdi, cu);
6034
6035   pdi = enum_pdi->die_child;
6036   while (pdi)
6037     {
6038       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6039         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6040       else
6041         add_partial_symbol (pdi, cu);
6042       pdi = pdi->die_sibling;
6043     }
6044 }
6045
6046 /* Return the initial uleb128 in the die at INFO_PTR.  */
6047
6048 static unsigned int
6049 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
6050 {
6051   unsigned int bytes_read;
6052
6053   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6054 }
6055
6056 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6057    Return the corresponding abbrev, or NULL if the number is zero (indicating
6058    an empty DIE).  In either case *BYTES_READ will be set to the length of
6059    the initial number.  */
6060
6061 static struct abbrev_info *
6062 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
6063                  struct dwarf2_cu *cu)
6064 {
6065   bfd *abfd = cu->objfile->obfd;
6066   unsigned int abbrev_number;
6067   struct abbrev_info *abbrev;
6068
6069   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6070
6071   if (abbrev_number == 0)
6072     return NULL;
6073
6074   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6075   if (!abbrev)
6076     {
6077       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6078              abbrev_number, bfd_get_filename (abfd));
6079     }
6080
6081   return abbrev;
6082 }
6083
6084 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6085    Returns a pointer to the end of a series of DIEs, terminated by an empty
6086    DIE.  Any children of the skipped DIEs will also be skipped.  */
6087
6088 static gdb_byte *
6089 skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
6090 {
6091   struct dwarf2_cu *cu = reader->cu;
6092   struct abbrev_info *abbrev;
6093   unsigned int bytes_read;
6094
6095   while (1)
6096     {
6097       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6098       if (abbrev == NULL)
6099         return info_ptr + bytes_read;
6100       else
6101         info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6102     }
6103 }
6104
6105 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6106    INFO_PTR should point just after the initial uleb128 of a DIE, and the
6107    abbrev corresponding to that skipped uleb128 should be passed in
6108    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
6109    children.  */
6110
6111 static gdb_byte *
6112 skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
6113               struct abbrev_info *abbrev)
6114 {
6115   unsigned int bytes_read;
6116   struct attribute attr;
6117   bfd *abfd = reader->abfd;
6118   struct dwarf2_cu *cu = reader->cu;
6119   gdb_byte *buffer = reader->buffer;
6120   const gdb_byte *buffer_end = reader->buffer_end;
6121   gdb_byte *start_info_ptr = info_ptr;
6122   unsigned int form, i;
6123
6124   for (i = 0; i < abbrev->num_attrs; i++)
6125     {
6126       /* The only abbrev we care about is DW_AT_sibling.  */
6127       if (abbrev->attrs[i].name == DW_AT_sibling)
6128         {
6129           read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6130           if (attr.form == DW_FORM_ref_addr)
6131             complaint (&symfile_complaints,
6132                        _("ignoring absolute DW_AT_sibling"));
6133           else
6134             return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6135         }
6136
6137       /* If it isn't DW_AT_sibling, skip this attribute.  */
6138       form = abbrev->attrs[i].form;
6139     skip_attribute:
6140       switch (form)
6141         {
6142         case DW_FORM_ref_addr:
6143           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6144              and later it is offset sized.  */
6145           if (cu->header.version == 2)
6146             info_ptr += cu->header.addr_size;
6147           else
6148             info_ptr += cu->header.offset_size;
6149           break;
6150         case DW_FORM_GNU_ref_alt:
6151           info_ptr += cu->header.offset_size;
6152           break;
6153         case DW_FORM_addr:
6154           info_ptr += cu->header.addr_size;
6155           break;
6156         case DW_FORM_data1:
6157         case DW_FORM_ref1:
6158         case DW_FORM_flag:
6159           info_ptr += 1;
6160           break;
6161         case DW_FORM_flag_present:
6162           break;
6163         case DW_FORM_data2:
6164         case DW_FORM_ref2:
6165           info_ptr += 2;
6166           break;
6167         case DW_FORM_data4:
6168         case DW_FORM_ref4:
6169           info_ptr += 4;
6170           break;
6171         case DW_FORM_data8:
6172         case DW_FORM_ref8:
6173         case DW_FORM_ref_sig8:
6174           info_ptr += 8;
6175           break;
6176         case DW_FORM_string:
6177           read_direct_string (abfd, info_ptr, &bytes_read);
6178           info_ptr += bytes_read;
6179           break;
6180         case DW_FORM_sec_offset:
6181         case DW_FORM_strp:
6182         case DW_FORM_GNU_strp_alt:
6183           info_ptr += cu->header.offset_size;
6184           break;
6185         case DW_FORM_exprloc:
6186         case DW_FORM_block:
6187           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6188           info_ptr += bytes_read;
6189           break;
6190         case DW_FORM_block1:
6191           info_ptr += 1 + read_1_byte (abfd, info_ptr);
6192           break;
6193         case DW_FORM_block2:
6194           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6195           break;
6196         case DW_FORM_block4:
6197           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6198           break;
6199         case DW_FORM_sdata:
6200         case DW_FORM_udata:
6201         case DW_FORM_ref_udata:
6202         case DW_FORM_GNU_addr_index:
6203         case DW_FORM_GNU_str_index:
6204           info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
6205           break;
6206         case DW_FORM_indirect:
6207           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6208           info_ptr += bytes_read;
6209           /* We need to continue parsing from here, so just go back to
6210              the top.  */
6211           goto skip_attribute;
6212
6213         default:
6214           error (_("Dwarf Error: Cannot handle %s "
6215                    "in DWARF reader [in module %s]"),
6216                  dwarf_form_name (form),
6217                  bfd_get_filename (abfd));
6218         }
6219     }
6220
6221   if (abbrev->has_children)
6222     return skip_children (reader, info_ptr);
6223   else
6224     return info_ptr;
6225 }
6226
6227 /* Locate ORIG_PDI's sibling.
6228    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */
6229
6230 static gdb_byte *
6231 locate_pdi_sibling (const struct die_reader_specs *reader,
6232                     struct partial_die_info *orig_pdi,
6233                     gdb_byte *info_ptr)
6234 {
6235   /* Do we know the sibling already?  */
6236
6237   if (orig_pdi->sibling)
6238     return orig_pdi->sibling;
6239
6240   /* Are there any children to deal with?  */
6241
6242   if (!orig_pdi->has_children)
6243     return info_ptr;
6244
6245   /* Skip the children the long way.  */
6246
6247   return skip_children (reader, info_ptr);
6248 }
6249
6250 /* Expand this partial symbol table into a full symbol table.  */
6251
6252 static void
6253 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
6254 {
6255   if (pst != NULL)
6256     {
6257       if (pst->readin)
6258         {
6259           warning (_("bug: psymtab for %s is already read in."),
6260                    pst->filename);
6261         }
6262       else
6263         {
6264           if (info_verbose)
6265             {
6266               printf_filtered (_("Reading in symbols for %s..."),
6267                                pst->filename);
6268               gdb_flush (gdb_stdout);
6269             }
6270
6271           /* Restore our global data.  */
6272           dwarf2_per_objfile = objfile_data (pst->objfile,
6273                                              dwarf2_objfile_data_key);
6274
6275           /* If this psymtab is constructed from a debug-only objfile, the
6276              has_section_at_zero flag will not necessarily be correct.  We
6277              can get the correct value for this flag by looking at the data
6278              associated with the (presumably stripped) associated objfile.  */
6279           if (pst->objfile->separate_debug_objfile_backlink)
6280             {
6281               struct dwarf2_per_objfile *dpo_backlink
6282                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
6283                                 dwarf2_objfile_data_key);
6284
6285               dwarf2_per_objfile->has_section_at_zero
6286                 = dpo_backlink->has_section_at_zero;
6287             }
6288
6289           dwarf2_per_objfile->reading_partial_symbols = 0;
6290
6291           psymtab_to_symtab_1 (pst);
6292
6293           /* Finish up the debug error message.  */
6294           if (info_verbose)
6295             printf_filtered (_("done.\n"));
6296         }
6297     }
6298
6299   process_cu_includes ();
6300 }
6301 \f
6302 /* Reading in full CUs.  */
6303
6304 /* Add PER_CU to the queue.  */
6305
6306 static void
6307 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6308                  enum language pretend_language)
6309 {
6310   struct dwarf2_queue_item *item;
6311
6312   per_cu->queued = 1;
6313   item = xmalloc (sizeof (*item));
6314   item->per_cu = per_cu;
6315   item->pretend_language = pretend_language;
6316   item->next = NULL;
6317
6318   if (dwarf2_queue == NULL)
6319     dwarf2_queue = item;
6320   else
6321     dwarf2_queue_tail->next = item;
6322
6323   dwarf2_queue_tail = item;
6324 }
6325
6326 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
6327    unit and add it to our queue.
6328    The result is non-zero if PER_CU was queued, otherwise the result is zero
6329    meaning either PER_CU is already queued or it is already loaded.  */
6330
6331 static int
6332 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6333                        struct dwarf2_per_cu_data *per_cu,
6334                        enum language pretend_language)
6335 {
6336   /* We may arrive here during partial symbol reading, if we need full
6337      DIEs to process an unusual case (e.g. template arguments).  Do
6338      not queue PER_CU, just tell our caller to load its DIEs.  */
6339   if (dwarf2_per_objfile->reading_partial_symbols)
6340     {
6341       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6342         return 1;
6343       return 0;
6344     }
6345
6346   /* Mark the dependence relation so that we don't flush PER_CU
6347      too early.  */
6348   dwarf2_add_dependence (this_cu, per_cu);
6349
6350   /* If it's already on the queue, we have nothing to do.  */
6351   if (per_cu->queued)
6352     return 0;
6353
6354   /* If the compilation unit is already loaded, just mark it as
6355      used.  */
6356   if (per_cu->cu != NULL)
6357     {
6358       per_cu->cu->last_used = 0;
6359       return 0;
6360     }
6361
6362   /* Add it to the queue.  */
6363   queue_comp_unit (per_cu, pretend_language);
6364
6365   return 1;
6366 }
6367
6368 /* Process the queue.  */
6369
6370 static void
6371 process_queue (void)
6372 {
6373   struct dwarf2_queue_item *item, *next_item;
6374
6375   if (dwarf2_read_debug)
6376     {
6377       fprintf_unfiltered (gdb_stdlog,
6378                           "Expanding one or more symtabs of objfile %s ...\n",
6379                           dwarf2_per_objfile->objfile->name);
6380     }
6381
6382   /* The queue starts out with one item, but following a DIE reference
6383      may load a new CU, adding it to the end of the queue.  */
6384   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6385     {
6386       if (dwarf2_per_objfile->using_index
6387           ? !item->per_cu->v.quick->symtab
6388           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
6389         {
6390           struct dwarf2_per_cu_data *per_cu = item->per_cu;
6391
6392           if (dwarf2_read_debug)
6393             {
6394               fprintf_unfiltered (gdb_stdlog,
6395                                   "Expanding symtab of %s at offset 0x%x\n",
6396                                   per_cu->is_debug_types ? "TU" : "CU",
6397                                   per_cu->offset.sect_off);
6398             }
6399
6400           if (per_cu->is_debug_types)
6401             process_full_type_unit (per_cu, item->pretend_language);
6402           else
6403             process_full_comp_unit (per_cu, item->pretend_language);
6404
6405           if (dwarf2_read_debug)
6406             {
6407               fprintf_unfiltered (gdb_stdlog,
6408                                   "Done expanding %s at offset 0x%x\n",
6409                                   per_cu->is_debug_types ? "TU" : "CU",
6410                                   per_cu->offset.sect_off);
6411             }
6412         }
6413
6414       item->per_cu->queued = 0;
6415       next_item = item->next;
6416       xfree (item);
6417     }
6418
6419   dwarf2_queue_tail = NULL;
6420
6421   if (dwarf2_read_debug)
6422     {
6423       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
6424                           dwarf2_per_objfile->objfile->name);
6425     }
6426 }
6427
6428 /* Free all allocated queue entries.  This function only releases anything if
6429    an error was thrown; if the queue was processed then it would have been
6430    freed as we went along.  */
6431
6432 static void
6433 dwarf2_release_queue (void *dummy)
6434 {
6435   struct dwarf2_queue_item *item, *last;
6436
6437   item = dwarf2_queue;
6438   while (item)
6439     {
6440       /* Anything still marked queued is likely to be in an
6441          inconsistent state, so discard it.  */
6442       if (item->per_cu->queued)
6443         {
6444           if (item->per_cu->cu != NULL)
6445             free_one_cached_comp_unit (item->per_cu);
6446           item->per_cu->queued = 0;
6447         }
6448
6449       last = item;
6450       item = item->next;
6451       xfree (last);
6452     }
6453
6454   dwarf2_queue = dwarf2_queue_tail = NULL;
6455 }
6456
6457 /* Read in full symbols for PST, and anything it depends on.  */
6458
6459 static void
6460 psymtab_to_symtab_1 (struct partial_symtab *pst)
6461 {
6462   struct dwarf2_per_cu_data *per_cu;
6463   int i;
6464
6465   if (pst->readin)
6466     return;
6467
6468   for (i = 0; i < pst->number_of_dependencies; i++)
6469     if (!pst->dependencies[i]->readin
6470         && pst->dependencies[i]->user == NULL)
6471       {
6472         /* Inform about additional files that need to be read in.  */
6473         if (info_verbose)
6474           {
6475             /* FIXME: i18n: Need to make this a single string.  */
6476             fputs_filtered (" ", gdb_stdout);
6477             wrap_here ("");
6478             fputs_filtered ("and ", gdb_stdout);
6479             wrap_here ("");
6480             printf_filtered ("%s...", pst->dependencies[i]->filename);
6481             wrap_here ("");     /* Flush output.  */
6482             gdb_flush (gdb_stdout);
6483           }
6484         psymtab_to_symtab_1 (pst->dependencies[i]);
6485       }
6486
6487   per_cu = pst->read_symtab_private;
6488
6489   if (per_cu == NULL)
6490     {
6491       /* It's an include file, no symbols to read for it.
6492          Everything is in the parent symtab.  */
6493       pst->readin = 1;
6494       return;
6495     }
6496
6497   dw2_do_instantiate_symtab (per_cu);
6498 }
6499
6500 /* Trivial hash function for die_info: the hash value of a DIE
6501    is its offset in .debug_info for this objfile.  */
6502
6503 static hashval_t
6504 die_hash (const void *item)
6505 {
6506   const struct die_info *die = item;
6507
6508   return die->offset.sect_off;
6509 }
6510
6511 /* Trivial comparison function for die_info structures: two DIEs
6512    are equal if they have the same offset.  */
6513
6514 static int
6515 die_eq (const void *item_lhs, const void *item_rhs)
6516 {
6517   const struct die_info *die_lhs = item_lhs;
6518   const struct die_info *die_rhs = item_rhs;
6519
6520   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
6521 }
6522
6523 /* die_reader_func for load_full_comp_unit.
6524    This is identical to read_signatured_type_reader,
6525    but is kept separate for now.  */
6526
6527 static void
6528 load_full_comp_unit_reader (const struct die_reader_specs *reader,
6529                             gdb_byte *info_ptr,
6530                             struct die_info *comp_unit_die,
6531                             int has_children,
6532                             void *data)
6533 {
6534   struct dwarf2_cu *cu = reader->cu;
6535   enum language *language_ptr = data;
6536
6537   gdb_assert (cu->die_hash == NULL);
6538   cu->die_hash =
6539     htab_create_alloc_ex (cu->header.length / 12,
6540                           die_hash,
6541                           die_eq,
6542                           NULL,
6543                           &cu->comp_unit_obstack,
6544                           hashtab_obstack_allocate,
6545                           dummy_obstack_deallocate);
6546
6547   if (has_children)
6548     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
6549                                                   &info_ptr, comp_unit_die);
6550   cu->dies = comp_unit_die;
6551   /* comp_unit_die is not stored in die_hash, no need.  */
6552
6553   /* We try not to read any attributes in this function, because not
6554      all CUs needed for references have been loaded yet, and symbol
6555      table processing isn't initialized.  But we have to set the CU language,
6556      or we won't be able to build types correctly.
6557      Similarly, if we do not read the producer, we can not apply
6558      producer-specific interpretation.  */
6559   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
6560 }
6561
6562 /* Load the DIEs associated with PER_CU into memory.  */
6563
6564 static void
6565 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
6566                      enum language pretend_language)
6567 {
6568   gdb_assert (! this_cu->is_debug_types);
6569
6570   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6571                            load_full_comp_unit_reader, &pretend_language);
6572 }
6573
6574 /* Add a DIE to the delayed physname list.  */
6575
6576 static void
6577 add_to_method_list (struct type *type, int fnfield_index, int index,
6578                     const char *name, struct die_info *die,
6579                     struct dwarf2_cu *cu)
6580 {
6581   struct delayed_method_info mi;
6582   mi.type = type;
6583   mi.fnfield_index = fnfield_index;
6584   mi.index = index;
6585   mi.name = name;
6586   mi.die = die;
6587   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
6588 }
6589
6590 /* A cleanup for freeing the delayed method list.  */
6591
6592 static void
6593 free_delayed_list (void *ptr)
6594 {
6595   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
6596   if (cu->method_list != NULL)
6597     {
6598       VEC_free (delayed_method_info, cu->method_list);
6599       cu->method_list = NULL;
6600     }
6601 }
6602
6603 /* Compute the physnames of any methods on the CU's method list.
6604
6605    The computation of method physnames is delayed in order to avoid the
6606    (bad) condition that one of the method's formal parameters is of an as yet
6607    incomplete type.  */
6608
6609 static void
6610 compute_delayed_physnames (struct dwarf2_cu *cu)
6611 {
6612   int i;
6613   struct delayed_method_info *mi;
6614   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
6615     {
6616       const char *physname;
6617       struct fn_fieldlist *fn_flp
6618         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
6619       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
6620       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
6621     }
6622 }
6623
6624 /* Go objects should be embedded in a DW_TAG_module DIE,
6625    and it's not clear if/how imported objects will appear.
6626    To keep Go support simple until that's worked out,
6627    go back through what we've read and create something usable.
6628    We could do this while processing each DIE, and feels kinda cleaner,
6629    but that way is more invasive.
6630    This is to, for example, allow the user to type "p var" or "b main"
6631    without having to specify the package name, and allow lookups
6632    of module.object to work in contexts that use the expression
6633    parser.  */
6634
6635 static void
6636 fixup_go_packaging (struct dwarf2_cu *cu)
6637 {
6638   char *package_name = NULL;
6639   struct pending *list;
6640   int i;
6641
6642   for (list = global_symbols; list != NULL; list = list->next)
6643     {
6644       for (i = 0; i < list->nsyms; ++i)
6645         {
6646           struct symbol *sym = list->symbol[i];
6647
6648           if (SYMBOL_LANGUAGE (sym) == language_go
6649               && SYMBOL_CLASS (sym) == LOC_BLOCK)
6650             {
6651               char *this_package_name = go_symbol_package_name (sym);
6652
6653               if (this_package_name == NULL)
6654                 continue;
6655               if (package_name == NULL)
6656                 package_name = this_package_name;
6657               else
6658                 {
6659                   if (strcmp (package_name, this_package_name) != 0)
6660                     complaint (&symfile_complaints,
6661                                _("Symtab %s has objects from two different Go packages: %s and %s"),
6662                                (sym->symtab && sym->symtab->filename
6663                                 ? sym->symtab->filename
6664                                 : cu->objfile->name),
6665                                this_package_name, package_name);
6666                   xfree (this_package_name);
6667                 }
6668             }
6669         }
6670     }
6671
6672   if (package_name != NULL)
6673     {
6674       struct objfile *objfile = cu->objfile;
6675       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
6676                                      package_name, objfile);
6677       struct symbol *sym;
6678
6679       TYPE_TAG_NAME (type) = TYPE_NAME (type);
6680
6681       sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
6682       SYMBOL_SET_LANGUAGE (sym, language_go);
6683       SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
6684       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
6685          e.g., "main" finds the "main" module and not C's main().  */
6686       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6687       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6688       SYMBOL_TYPE (sym) = type;
6689
6690       add_symbol_to_list (sym, &global_symbols);
6691
6692       xfree (package_name);
6693     }
6694 }
6695
6696 static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
6697
6698 /* Return the symtab for PER_CU.  This works properly regardless of
6699    whether we're using the index or psymtabs.  */
6700
6701 static struct symtab *
6702 get_symtab (struct dwarf2_per_cu_data *per_cu)
6703 {
6704   return (dwarf2_per_objfile->using_index
6705           ? per_cu->v.quick->symtab
6706           : per_cu->v.psymtab->symtab);
6707 }
6708
6709 /* A helper function for computing the list of all symbol tables
6710    included by PER_CU.  */
6711
6712 static void
6713 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
6714                                 htab_t all_children,
6715                                 struct dwarf2_per_cu_data *per_cu)
6716 {
6717   void **slot;
6718   int ix;
6719   struct dwarf2_per_cu_data *iter;
6720
6721   slot = htab_find_slot (all_children, per_cu, INSERT);
6722   if (*slot != NULL)
6723     {
6724       /* This inclusion and its children have been processed.  */
6725       return;
6726     }
6727
6728   *slot = per_cu;
6729   /* Only add a CU if it has a symbol table.  */
6730   if (get_symtab (per_cu) != NULL)
6731     VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
6732
6733   for (ix = 0;
6734        VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs, ix, iter);
6735        ++ix)
6736     recursively_compute_inclusions (result, all_children, iter);
6737 }
6738
6739 /* Compute the symtab 'includes' fields for the symtab related to
6740    PER_CU.  */
6741
6742 static void
6743 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
6744 {
6745   gdb_assert (! per_cu->is_debug_types);
6746
6747   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs))
6748     {
6749       int ix, len;
6750       struct dwarf2_per_cu_data *iter;
6751       VEC (dwarf2_per_cu_ptr) *result_children = NULL;
6752       htab_t all_children;
6753       struct symtab *symtab = get_symtab (per_cu);
6754
6755       /* If we don't have a symtab, we can just skip this case.  */
6756       if (symtab == NULL)
6757         return;
6758
6759       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
6760                                         NULL, xcalloc, xfree);
6761
6762       for (ix = 0;
6763            VEC_iterate (dwarf2_per_cu_ptr, per_cu->s.imported_symtabs,
6764                         ix, iter);
6765            ++ix)
6766         recursively_compute_inclusions (&result_children, all_children, iter);
6767
6768       /* Now we have a transitive closure of all the included CUs, so
6769          we can convert it to a list of symtabs.  */
6770       len = VEC_length (dwarf2_per_cu_ptr, result_children);
6771       symtab->includes
6772         = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
6773                          (len + 1) * sizeof (struct symtab *));
6774       for (ix = 0;
6775            VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
6776            ++ix)
6777         symtab->includes[ix] = get_symtab (iter);
6778       symtab->includes[len] = NULL;
6779
6780       VEC_free (dwarf2_per_cu_ptr, result_children);
6781       htab_delete (all_children);
6782     }
6783 }
6784
6785 /* Compute the 'includes' field for the symtabs of all the CUs we just
6786    read.  */
6787
6788 static void
6789 process_cu_includes (void)
6790 {
6791   int ix;
6792   struct dwarf2_per_cu_data *iter;
6793
6794   for (ix = 0;
6795        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
6796                     ix, iter);
6797        ++ix)
6798     {
6799       if (! iter->is_debug_types)
6800         compute_symtab_includes (iter);
6801     }
6802
6803   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
6804 }
6805
6806 /* Generate full symbol information for PER_CU, whose DIEs have
6807    already been loaded into memory.  */
6808
6809 static void
6810 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
6811                         enum language pretend_language)
6812 {
6813   struct dwarf2_cu *cu = per_cu->cu;
6814   struct objfile *objfile = per_cu->objfile;
6815   CORE_ADDR lowpc, highpc;
6816   struct symtab *symtab;
6817   struct cleanup *back_to, *delayed_list_cleanup;
6818   CORE_ADDR baseaddr;
6819   struct block *static_block;
6820
6821   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6822
6823   buildsym_init ();
6824   back_to = make_cleanup (really_free_pendings, NULL);
6825   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6826
6827   cu->list_in_scope = &file_symbols;
6828
6829   cu->language = pretend_language;
6830   cu->language_defn = language_def (cu->language);
6831
6832   /* Do line number decoding in read_file_scope () */
6833   process_die (cu->dies, cu);
6834
6835   /* For now fudge the Go package.  */
6836   if (cu->language == language_go)
6837     fixup_go_packaging (cu);
6838
6839   /* Now that we have processed all the DIEs in the CU, all the types 
6840      should be complete, and it should now be safe to compute all of the
6841      physnames.  */
6842   compute_delayed_physnames (cu);
6843   do_cleanups (delayed_list_cleanup);
6844
6845   /* Some compilers don't define a DW_AT_high_pc attribute for the
6846      compilation unit.  If the DW_AT_high_pc is missing, synthesize
6847      it, by scanning the DIE's below the compilation unit.  */
6848   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
6849
6850   static_block
6851     = end_symtab_get_static_block (highpc + baseaddr, objfile, 0,
6852                                    per_cu->s.imported_symtabs != NULL);
6853
6854   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
6855      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
6856      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
6857      addrmap to help ensure it has an accurate map of pc values belonging to
6858      this comp unit.  */
6859   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
6860
6861   symtab = end_symtab_from_static_block (static_block, objfile,
6862                                          SECT_OFF_TEXT (objfile), 0);
6863
6864   if (symtab != NULL)
6865     {
6866       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
6867
6868       /* Set symtab language to language from DW_AT_language.  If the
6869          compilation is from a C file generated by language preprocessors, do
6870          not set the language if it was already deduced by start_subfile.  */
6871       if (!(cu->language == language_c && symtab->language != language_c))
6872         symtab->language = cu->language;
6873
6874       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
6875          produce DW_AT_location with location lists but it can be possibly
6876          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
6877          there were bugs in prologue debug info, fixed later in GCC-4.5
6878          by "unwind info for epilogues" patch (which is not directly related).
6879
6880          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6881          needed, it would be wrong due to missing DW_AT_producer there.
6882
6883          Still one can confuse GDB by using non-standard GCC compilation
6884          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6885          */ 
6886       if (cu->has_loclist && gcc_4_minor >= 5)
6887         symtab->locations_valid = 1;
6888
6889       if (gcc_4_minor >= 5)
6890         symtab->epilogue_unwind_valid = 1;
6891
6892       symtab->call_site_htab = cu->call_site_htab;
6893     }
6894
6895   if (dwarf2_per_objfile->using_index)
6896     per_cu->v.quick->symtab = symtab;
6897   else
6898     {
6899       struct partial_symtab *pst = per_cu->v.psymtab;
6900       pst->symtab = symtab;
6901       pst->readin = 1;
6902     }
6903
6904   /* Push it for inclusion processing later.  */
6905   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6906
6907   do_cleanups (back_to);
6908 }
6909
6910 /* Generate full symbol information for type unit PER_CU, whose DIEs have
6911    already been loaded into memory.  */
6912
6913 static void
6914 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
6915                         enum language pretend_language)
6916 {
6917   struct dwarf2_cu *cu = per_cu->cu;
6918   struct objfile *objfile = per_cu->objfile;
6919   struct symtab *symtab;
6920   struct cleanup *back_to, *delayed_list_cleanup;
6921
6922   buildsym_init ();
6923   back_to = make_cleanup (really_free_pendings, NULL);
6924   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
6925
6926   cu->list_in_scope = &file_symbols;
6927
6928   cu->language = pretend_language;
6929   cu->language_defn = language_def (cu->language);
6930
6931   /* The symbol tables are set up in read_type_unit_scope.  */
6932   process_die (cu->dies, cu);
6933
6934   /* For now fudge the Go package.  */
6935   if (cu->language == language_go)
6936     fixup_go_packaging (cu);
6937
6938   /* Now that we have processed all the DIEs in the CU, all the types 
6939      should be complete, and it should now be safe to compute all of the
6940      physnames.  */
6941   compute_delayed_physnames (cu);
6942   do_cleanups (delayed_list_cleanup);
6943
6944   /* TUs share symbol tables.
6945      If this is the first TU to use this symtab, complete the construction
6946      of it with end_expandable_symtab.  Otherwise, complete the addition of
6947      this TU's symbols to the existing symtab.  */
6948   if (per_cu->s.type_unit_group->primary_symtab == NULL)
6949     {
6950       symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
6951       per_cu->s.type_unit_group->primary_symtab = symtab;
6952
6953       if (symtab != NULL)
6954         {
6955           /* Set symtab language to language from DW_AT_language.  If the
6956              compilation is from a C file generated by language preprocessors,
6957              do not set the language if it was already deduced by
6958              start_subfile.  */
6959           if (!(cu->language == language_c && symtab->language != language_c))
6960             symtab->language = cu->language;
6961         }
6962     }
6963   else
6964     {
6965       augment_type_symtab (objfile,
6966                            per_cu->s.type_unit_group->primary_symtab);
6967       symtab = per_cu->s.type_unit_group->primary_symtab;
6968     }
6969
6970   if (dwarf2_per_objfile->using_index)
6971     per_cu->v.quick->symtab = symtab;
6972   else
6973     {
6974       struct partial_symtab *pst = per_cu->v.psymtab;
6975       pst->symtab = symtab;
6976       pst->readin = 1;
6977     }
6978
6979   do_cleanups (back_to);
6980 }
6981
6982 /* Process an imported unit DIE.  */
6983
6984 static void
6985 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6986 {
6987   struct attribute *attr;
6988
6989   /* For now we don't handle imported units in type units.  */
6990   if (cu->per_cu->is_debug_types)
6991     {
6992       error (_("Dwarf Error: DW_TAG_imported_unit is not"
6993                " supported in type units [in module %s]"),
6994              cu->objfile->name);
6995     }
6996
6997   attr = dwarf2_attr (die, DW_AT_import, cu);
6998   if (attr != NULL)
6999     {
7000       struct dwarf2_per_cu_data *per_cu;
7001       struct symtab *imported_symtab;
7002       sect_offset offset;
7003       int is_dwz;
7004
7005       offset = dwarf2_get_ref_die_offset (attr);
7006       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7007       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7008
7009       /* Queue the unit, if needed.  */
7010       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7011         load_full_comp_unit (per_cu, cu->language);
7012
7013       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->s.imported_symtabs,
7014                      per_cu);
7015     }
7016 }
7017
7018 /* Process a die and its children.  */
7019
7020 static void
7021 process_die (struct die_info *die, struct dwarf2_cu *cu)
7022 {
7023   switch (die->tag)
7024     {
7025     case DW_TAG_padding:
7026       break;
7027     case DW_TAG_compile_unit:
7028     case DW_TAG_partial_unit:
7029       read_file_scope (die, cu);
7030       break;
7031     case DW_TAG_type_unit:
7032       read_type_unit_scope (die, cu);
7033       break;
7034     case DW_TAG_subprogram:
7035     case DW_TAG_inlined_subroutine:
7036       read_func_scope (die, cu);
7037       break;
7038     case DW_TAG_lexical_block:
7039     case DW_TAG_try_block:
7040     case DW_TAG_catch_block:
7041       read_lexical_block_scope (die, cu);
7042       break;
7043     case DW_TAG_GNU_call_site:
7044       read_call_site_scope (die, cu);
7045       break;
7046     case DW_TAG_class_type:
7047     case DW_TAG_interface_type:
7048     case DW_TAG_structure_type:
7049     case DW_TAG_union_type:
7050       process_structure_scope (die, cu);
7051       break;
7052     case DW_TAG_enumeration_type:
7053       process_enumeration_scope (die, cu);
7054       break;
7055
7056     /* These dies have a type, but processing them does not create
7057        a symbol or recurse to process the children.  Therefore we can
7058        read them on-demand through read_type_die.  */
7059     case DW_TAG_subroutine_type:
7060     case DW_TAG_set_type:
7061     case DW_TAG_array_type:
7062     case DW_TAG_pointer_type:
7063     case DW_TAG_ptr_to_member_type:
7064     case DW_TAG_reference_type:
7065     case DW_TAG_string_type:
7066       break;
7067
7068     case DW_TAG_base_type:
7069     case DW_TAG_subrange_type:
7070     case DW_TAG_typedef:
7071       /* Add a typedef symbol for the type definition, if it has a
7072          DW_AT_name.  */
7073       new_symbol (die, read_type_die (die, cu), cu);
7074       break;
7075     case DW_TAG_common_block:
7076       read_common_block (die, cu);
7077       break;
7078     case DW_TAG_common_inclusion:
7079       break;
7080     case DW_TAG_namespace:
7081       processing_has_namespace_info = 1;
7082       read_namespace (die, cu);
7083       break;
7084     case DW_TAG_module:
7085       processing_has_namespace_info = 1;
7086       read_module (die, cu);
7087       break;
7088     case DW_TAG_imported_declaration:
7089     case DW_TAG_imported_module:
7090       processing_has_namespace_info = 1;
7091       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7092                                  || cu->language != language_fortran))
7093         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7094                    dwarf_tag_name (die->tag));
7095       read_import_statement (die, cu);
7096       break;
7097
7098     case DW_TAG_imported_unit:
7099       process_imported_unit_die (die, cu);
7100       break;
7101
7102     default:
7103       new_symbol (die, NULL, cu);
7104       break;
7105     }
7106 }
7107
7108 /* A helper function for dwarf2_compute_name which determines whether DIE
7109    needs to have the name of the scope prepended to the name listed in the
7110    die.  */
7111
7112 static int
7113 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7114 {
7115   struct attribute *attr;
7116
7117   switch (die->tag)
7118     {
7119     case DW_TAG_namespace:
7120     case DW_TAG_typedef:
7121     case DW_TAG_class_type:
7122     case DW_TAG_interface_type:
7123     case DW_TAG_structure_type:
7124     case DW_TAG_union_type:
7125     case DW_TAG_enumeration_type:
7126     case DW_TAG_enumerator:
7127     case DW_TAG_subprogram:
7128     case DW_TAG_member:
7129       return 1;
7130
7131     case DW_TAG_variable:
7132     case DW_TAG_constant:
7133       /* We only need to prefix "globally" visible variables.  These include
7134          any variable marked with DW_AT_external or any variable that
7135          lives in a namespace.  [Variables in anonymous namespaces
7136          require prefixing, but they are not DW_AT_external.]  */
7137
7138       if (dwarf2_attr (die, DW_AT_specification, cu))
7139         {
7140           struct dwarf2_cu *spec_cu = cu;
7141
7142           return die_needs_namespace (die_specification (die, &spec_cu),
7143                                       spec_cu);
7144         }
7145
7146       attr = dwarf2_attr (die, DW_AT_external, cu);
7147       if (attr == NULL && die->parent->tag != DW_TAG_namespace
7148           && die->parent->tag != DW_TAG_module)
7149         return 0;
7150       /* A variable in a lexical block of some kind does not need a
7151          namespace, even though in C++ such variables may be external
7152          and have a mangled name.  */
7153       if (die->parent->tag ==  DW_TAG_lexical_block
7154           || die->parent->tag ==  DW_TAG_try_block
7155           || die->parent->tag ==  DW_TAG_catch_block
7156           || die->parent->tag == DW_TAG_subprogram)
7157         return 0;
7158       return 1;
7159
7160     default:
7161       return 0;
7162     }
7163 }
7164
7165 /* Retrieve the last character from a mem_file.  */
7166
7167 static void
7168 do_ui_file_peek_last (void *object, const char *buffer, long length)
7169 {
7170   char *last_char_p = (char *) object;
7171
7172   if (length > 0)
7173     *last_char_p = buffer[length - 1];
7174 }
7175
7176 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
7177    compute the physname for the object, which include a method's:
7178    - formal parameters (C++/Java),
7179    - receiver type (Go),
7180    - return type (Java).
7181
7182    The term "physname" is a bit confusing.
7183    For C++, for example, it is the demangled name.
7184    For Go, for example, it's the mangled name.
7185
7186    For Ada, return the DIE's linkage name rather than the fully qualified
7187    name.  PHYSNAME is ignored..
7188
7189    The result is allocated on the objfile_obstack and canonicalized.  */
7190
7191 static const char *
7192 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
7193                      int physname)
7194 {
7195   struct objfile *objfile = cu->objfile;
7196
7197   if (name == NULL)
7198     name = dwarf2_name (die, cu);
7199
7200   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7201      compute it by typename_concat inside GDB.  */
7202   if (cu->language == language_ada
7203       || (cu->language == language_fortran && physname))
7204     {
7205       /* For Ada unit, we prefer the linkage name over the name, as
7206          the former contains the exported name, which the user expects
7207          to be able to reference.  Ideally, we want the user to be able
7208          to reference this entity using either natural or linkage name,
7209          but we haven't started looking at this enhancement yet.  */
7210       struct attribute *attr;
7211
7212       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7213       if (attr == NULL)
7214         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7215       if (attr && DW_STRING (attr))
7216         return DW_STRING (attr);
7217     }
7218
7219   /* These are the only languages we know how to qualify names in.  */
7220   if (name != NULL
7221       && (cu->language == language_cplus || cu->language == language_java
7222           || cu->language == language_fortran))
7223     {
7224       if (die_needs_namespace (die, cu))
7225         {
7226           long length;
7227           const char *prefix;
7228           struct ui_file *buf;
7229
7230           prefix = determine_prefix (die, cu);
7231           buf = mem_fileopen ();
7232           if (*prefix != '\0')
7233             {
7234               char *prefixed_name = typename_concat (NULL, prefix, name,
7235                                                      physname, cu);
7236
7237               fputs_unfiltered (prefixed_name, buf);
7238               xfree (prefixed_name);
7239             }
7240           else
7241             fputs_unfiltered (name, buf);
7242
7243           /* Template parameters may be specified in the DIE's DW_AT_name, or
7244              as children with DW_TAG_template_type_param or
7245              DW_TAG_value_type_param.  If the latter, add them to the name
7246              here.  If the name already has template parameters, then
7247              skip this step; some versions of GCC emit both, and
7248              it is more efficient to use the pre-computed name.
7249
7250              Something to keep in mind about this process: it is very
7251              unlikely, or in some cases downright impossible, to produce
7252              something that will match the mangled name of a function.
7253              If the definition of the function has the same debug info,
7254              we should be able to match up with it anyway.  But fallbacks
7255              using the minimal symbol, for instance to find a method
7256              implemented in a stripped copy of libstdc++, will not work.
7257              If we do not have debug info for the definition, we will have to
7258              match them up some other way.
7259
7260              When we do name matching there is a related problem with function
7261              templates; two instantiated function templates are allowed to
7262              differ only by their return types, which we do not add here.  */
7263
7264           if (cu->language == language_cplus && strchr (name, '<') == NULL)
7265             {
7266               struct attribute *attr;
7267               struct die_info *child;
7268               int first = 1;
7269
7270               die->building_fullname = 1;
7271
7272               for (child = die->child; child != NULL; child = child->sibling)
7273                 {
7274                   struct type *type;
7275                   LONGEST value;
7276                   gdb_byte *bytes;
7277                   struct dwarf2_locexpr_baton *baton;
7278                   struct value *v;
7279
7280                   if (child->tag != DW_TAG_template_type_param
7281                       && child->tag != DW_TAG_template_value_param)
7282                     continue;
7283
7284                   if (first)
7285                     {
7286                       fputs_unfiltered ("<", buf);
7287                       first = 0;
7288                     }
7289                   else
7290                     fputs_unfiltered (", ", buf);
7291
7292                   attr = dwarf2_attr (child, DW_AT_type, cu);
7293                   if (attr == NULL)
7294                     {
7295                       complaint (&symfile_complaints,
7296                                  _("template parameter missing DW_AT_type"));
7297                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
7298                       continue;
7299                     }
7300                   type = die_type (child, cu);
7301
7302                   if (child->tag == DW_TAG_template_type_param)
7303                     {
7304                       c_print_type (type, "", buf, -1, 0);
7305                       continue;
7306                     }
7307
7308                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
7309                   if (attr == NULL)
7310                     {
7311                       complaint (&symfile_complaints,
7312                                  _("template parameter missing "
7313                                    "DW_AT_const_value"));
7314                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
7315                       continue;
7316                     }
7317
7318                   dwarf2_const_value_attr (attr, type, name,
7319                                            &cu->comp_unit_obstack, cu,
7320                                            &value, &bytes, &baton);
7321
7322                   if (TYPE_NOSIGN (type))
7323                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
7324                        changed, this can use value_print instead.  */
7325                     c_printchar (value, type, buf);
7326                   else
7327                     {
7328                       struct value_print_options opts;
7329
7330                       if (baton != NULL)
7331                         v = dwarf2_evaluate_loc_desc (type, NULL,
7332                                                       baton->data,
7333                                                       baton->size,
7334                                                       baton->per_cu);
7335                       else if (bytes != NULL)
7336                         {
7337                           v = allocate_value (type);
7338                           memcpy (value_contents_writeable (v), bytes,
7339                                   TYPE_LENGTH (type));
7340                         }
7341                       else
7342                         v = value_from_longest (type, value);
7343
7344                       /* Specify decimal so that we do not depend on
7345                          the radix.  */
7346                       get_formatted_print_options (&opts, 'd');
7347                       opts.raw = 1;
7348                       value_print (v, buf, &opts);
7349                       release_value (v);
7350                       value_free (v);
7351                     }
7352                 }
7353
7354               die->building_fullname = 0;
7355
7356               if (!first)
7357                 {
7358                   /* Close the argument list, with a space if necessary
7359                      (nested templates).  */
7360                   char last_char = '\0';
7361                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
7362                   if (last_char == '>')
7363                     fputs_unfiltered (" >", buf);
7364                   else
7365                     fputs_unfiltered (">", buf);
7366                 }
7367             }
7368
7369           /* For Java and C++ methods, append formal parameter type
7370              information, if PHYSNAME.  */
7371
7372           if (physname && die->tag == DW_TAG_subprogram
7373               && (cu->language == language_cplus
7374                   || cu->language == language_java))
7375             {
7376               struct type *type = read_type_die (die, cu);
7377
7378               c_type_print_args (type, buf, 1, cu->language);
7379
7380               if (cu->language == language_java)
7381                 {
7382                   /* For java, we must append the return type to method
7383                      names.  */
7384                   if (die->tag == DW_TAG_subprogram)
7385                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
7386                                      0, 0);
7387                 }
7388               else if (cu->language == language_cplus)
7389                 {
7390                   /* Assume that an artificial first parameter is
7391                      "this", but do not crash if it is not.  RealView
7392                      marks unnamed (and thus unused) parameters as
7393                      artificial; there is no way to differentiate
7394                      the two cases.  */
7395                   if (TYPE_NFIELDS (type) > 0
7396                       && TYPE_FIELD_ARTIFICIAL (type, 0)
7397                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
7398                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
7399                                                                         0))))
7400                     fputs_unfiltered (" const", buf);
7401                 }
7402             }
7403
7404           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
7405                                        &length);
7406           ui_file_delete (buf);
7407
7408           if (cu->language == language_cplus)
7409             {
7410               char *cname
7411                 = dwarf2_canonicalize_name (name, cu,
7412                                             &objfile->objfile_obstack);
7413
7414               if (cname != NULL)
7415                 name = cname;
7416             }
7417         }
7418     }
7419
7420   return name;
7421 }
7422
7423 /* Return the fully qualified name of DIE, based on its DW_AT_name.
7424    If scope qualifiers are appropriate they will be added.  The result
7425    will be allocated on the objfile_obstack, or NULL if the DIE does
7426    not have a name.  NAME may either be from a previous call to
7427    dwarf2_name or NULL.
7428
7429    The output string will be canonicalized (if C++/Java).  */
7430
7431 static const char *
7432 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
7433 {
7434   return dwarf2_compute_name (name, die, cu, 0);
7435 }
7436
7437 /* Construct a physname for the given DIE in CU.  NAME may either be
7438    from a previous call to dwarf2_name or NULL.  The result will be
7439    allocated on the objfile_objstack or NULL if the DIE does not have a
7440    name.
7441
7442    The output string will be canonicalized (if C++/Java).  */
7443
7444 static const char *
7445 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
7446 {
7447   struct objfile *objfile = cu->objfile;
7448   struct attribute *attr;
7449   const char *retval, *mangled = NULL, *canon = NULL;
7450   struct cleanup *back_to;
7451   int need_copy = 1;
7452
7453   /* In this case dwarf2_compute_name is just a shortcut not building anything
7454      on its own.  */
7455   if (!die_needs_namespace (die, cu))
7456     return dwarf2_compute_name (name, die, cu, 1);
7457
7458   back_to = make_cleanup (null_cleanup, NULL);
7459
7460   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7461   if (!attr)
7462     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7463
7464   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
7465      has computed.  */
7466   if (attr && DW_STRING (attr))
7467     {
7468       char *demangled;
7469
7470       mangled = DW_STRING (attr);
7471
7472       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
7473          type.  It is easier for GDB users to search for such functions as
7474          `name(params)' than `long name(params)'.  In such case the minimal
7475          symbol names do not match the full symbol names but for template
7476          functions there is never a need to look up their definition from their
7477          declaration so the only disadvantage remains the minimal symbol
7478          variant `long name(params)' does not have the proper inferior type.
7479          */
7480
7481       if (cu->language == language_go)
7482         {
7483           /* This is a lie, but we already lie to the caller new_symbol_full.
7484              new_symbol_full assumes we return the mangled name.
7485              This just undoes that lie until things are cleaned up.  */
7486           demangled = NULL;
7487         }
7488       else
7489         {
7490           demangled = cplus_demangle (mangled,
7491                                       (DMGL_PARAMS | DMGL_ANSI
7492                                        | (cu->language == language_java
7493                                           ? DMGL_JAVA | DMGL_RET_POSTFIX
7494                                           : DMGL_RET_DROP)));
7495         }
7496       if (demangled)
7497         {
7498           make_cleanup (xfree, demangled);
7499           canon = demangled;
7500         }
7501       else
7502         {
7503           canon = mangled;
7504           need_copy = 0;
7505         }
7506     }
7507
7508   if (canon == NULL || check_physname)
7509     {
7510       const char *physname = dwarf2_compute_name (name, die, cu, 1);
7511
7512       if (canon != NULL && strcmp (physname, canon) != 0)
7513         {
7514           /* It may not mean a bug in GDB.  The compiler could also
7515              compute DW_AT_linkage_name incorrectly.  But in such case
7516              GDB would need to be bug-to-bug compatible.  */
7517
7518           complaint (&symfile_complaints,
7519                      _("Computed physname <%s> does not match demangled <%s> "
7520                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
7521                      physname, canon, mangled, die->offset.sect_off, objfile->name);
7522
7523           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
7524              is available here - over computed PHYSNAME.  It is safer
7525              against both buggy GDB and buggy compilers.  */
7526
7527           retval = canon;
7528         }
7529       else
7530         {
7531           retval = physname;
7532           need_copy = 0;
7533         }
7534     }
7535   else
7536     retval = canon;
7537
7538   if (need_copy)
7539     retval = obsavestring (retval, strlen (retval),
7540                            &objfile->objfile_obstack);
7541
7542   do_cleanups (back_to);
7543   return retval;
7544 }
7545
7546 /* Read the import statement specified by the given die and record it.  */
7547
7548 static void
7549 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
7550 {
7551   struct objfile *objfile = cu->objfile;
7552   struct attribute *import_attr;
7553   struct die_info *imported_die, *child_die;
7554   struct dwarf2_cu *imported_cu;
7555   const char *imported_name;
7556   const char *imported_name_prefix;
7557   const char *canonical_name;
7558   const char *import_alias;
7559   const char *imported_declaration = NULL;
7560   const char *import_prefix;
7561   VEC (const_char_ptr) *excludes = NULL;
7562   struct cleanup *cleanups;
7563
7564   char *temp;
7565
7566   import_attr = dwarf2_attr (die, DW_AT_import, cu);
7567   if (import_attr == NULL)
7568     {
7569       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7570                  dwarf_tag_name (die->tag));
7571       return;
7572     }
7573
7574   imported_cu = cu;
7575   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
7576   imported_name = dwarf2_name (imported_die, imported_cu);
7577   if (imported_name == NULL)
7578     {
7579       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
7580
7581         The import in the following code:
7582         namespace A
7583           {
7584             typedef int B;
7585           }
7586
7587         int main ()
7588           {
7589             using A::B;
7590             B b;
7591             return b;
7592           }
7593
7594         ...
7595          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
7596             <52>   DW_AT_decl_file   : 1
7597             <53>   DW_AT_decl_line   : 6
7598             <54>   DW_AT_import      : <0x75>
7599          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
7600             <59>   DW_AT_name        : B
7601             <5b>   DW_AT_decl_file   : 1
7602             <5c>   DW_AT_decl_line   : 2
7603             <5d>   DW_AT_type        : <0x6e>
7604         ...
7605          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
7606             <76>   DW_AT_byte_size   : 4
7607             <77>   DW_AT_encoding    : 5        (signed)
7608
7609         imports the wrong die ( 0x75 instead of 0x58 ).
7610         This case will be ignored until the gcc bug is fixed.  */
7611       return;
7612     }
7613
7614   /* Figure out the local name after import.  */
7615   import_alias = dwarf2_name (die, cu);
7616
7617   /* Figure out where the statement is being imported to.  */
7618   import_prefix = determine_prefix (die, cu);
7619
7620   /* Figure out what the scope of the imported die is and prepend it
7621      to the name of the imported die.  */
7622   imported_name_prefix = determine_prefix (imported_die, imported_cu);
7623
7624   if (imported_die->tag != DW_TAG_namespace
7625       && imported_die->tag != DW_TAG_module)
7626     {
7627       imported_declaration = imported_name;
7628       canonical_name = imported_name_prefix;
7629     }
7630   else if (strlen (imported_name_prefix) > 0)
7631     {
7632       temp = alloca (strlen (imported_name_prefix)
7633                      + 2 + strlen (imported_name) + 1);
7634       strcpy (temp, imported_name_prefix);
7635       strcat (temp, "::");
7636       strcat (temp, imported_name);
7637       canonical_name = temp;
7638     }
7639   else
7640     canonical_name = imported_name;
7641
7642   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
7643
7644   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
7645     for (child_die = die->child; child_die && child_die->tag;
7646          child_die = sibling_die (child_die))
7647       {
7648         /* DWARF-4: A Fortran use statement with a “rename list” may be
7649            represented by an imported module entry with an import attribute
7650            referring to the module and owned entries corresponding to those
7651            entities that are renamed as part of being imported.  */
7652
7653         if (child_die->tag != DW_TAG_imported_declaration)
7654           {
7655             complaint (&symfile_complaints,
7656                        _("child DW_TAG_imported_declaration expected "
7657                          "- DIE at 0x%x [in module %s]"),
7658                        child_die->offset.sect_off, objfile->name);
7659             continue;
7660           }
7661
7662         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
7663         if (import_attr == NULL)
7664           {
7665             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
7666                        dwarf_tag_name (child_die->tag));
7667             continue;
7668           }
7669
7670         imported_cu = cu;
7671         imported_die = follow_die_ref_or_sig (child_die, import_attr,
7672                                               &imported_cu);
7673         imported_name = dwarf2_name (imported_die, imported_cu);
7674         if (imported_name == NULL)
7675           {
7676             complaint (&symfile_complaints,
7677                        _("child DW_TAG_imported_declaration has unknown "
7678                          "imported name - DIE at 0x%x [in module %s]"),
7679                        child_die->offset.sect_off, objfile->name);
7680             continue;
7681           }
7682
7683         VEC_safe_push (const_char_ptr, excludes, imported_name);
7684
7685         process_die (child_die, cu);
7686       }
7687
7688   cp_add_using_directive (import_prefix,
7689                           canonical_name,
7690                           import_alias,
7691                           imported_declaration,
7692                           excludes,
7693                           &objfile->objfile_obstack);
7694
7695   do_cleanups (cleanups);
7696 }
7697
7698 /* Cleanup function for handle_DW_AT_stmt_list.  */
7699
7700 static void
7701 free_cu_line_header (void *arg)
7702 {
7703   struct dwarf2_cu *cu = arg;
7704
7705   free_line_header (cu->line_header);
7706   cu->line_header = NULL;
7707 }
7708
7709 static void
7710 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
7711                          char **name, char **comp_dir)
7712 {
7713   struct attribute *attr;
7714
7715   *name = NULL;
7716   *comp_dir = NULL;
7717
7718   /* Find the filename.  Do not use dwarf2_name here, since the filename
7719      is not a source language identifier.  */
7720   attr = dwarf2_attr (die, DW_AT_name, cu);
7721   if (attr)
7722     {
7723       *name = DW_STRING (attr);
7724     }
7725
7726   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
7727   if (attr)
7728     *comp_dir = DW_STRING (attr);
7729   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
7730     {
7731       *comp_dir = ldirname (*name);
7732       if (*comp_dir != NULL)
7733         make_cleanup (xfree, *comp_dir);
7734     }
7735   if (*comp_dir != NULL)
7736     {
7737       /* Irix 6.2 native cc prepends <machine>.: to the compilation
7738          directory, get rid of it.  */
7739       char *cp = strchr (*comp_dir, ':');
7740
7741       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
7742         *comp_dir = cp + 1;
7743     }
7744
7745   if (*name == NULL)
7746     *name = "<unknown>";
7747 }
7748
7749 /* Handle DW_AT_stmt_list for a compilation unit.
7750    DIE is the DW_TAG_compile_unit die for CU.
7751    COMP_DIR is the compilation directory.
7752    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
7753
7754 static void
7755 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
7756                         const char *comp_dir)
7757 {
7758   struct attribute *attr;
7759
7760   gdb_assert (! cu->per_cu->is_debug_types);
7761
7762   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7763   if (attr)
7764     {
7765       unsigned int line_offset = DW_UNSND (attr);
7766       struct line_header *line_header
7767         = dwarf_decode_line_header (line_offset, cu);
7768
7769       if (line_header)
7770         {
7771           cu->line_header = line_header;
7772           make_cleanup (free_cu_line_header, cu);
7773           dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
7774         }
7775     }
7776 }
7777
7778 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
7779
7780 static void
7781 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
7782 {
7783   struct objfile *objfile = dwarf2_per_objfile->objfile;
7784   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7785   CORE_ADDR lowpc = ((CORE_ADDR) -1);
7786   CORE_ADDR highpc = ((CORE_ADDR) 0);
7787   struct attribute *attr;
7788   char *name = NULL;
7789   char *comp_dir = NULL;
7790   struct die_info *child_die;
7791   bfd *abfd = objfile->obfd;
7792   CORE_ADDR baseaddr;
7793
7794   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7795
7796   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
7797
7798   /* If we didn't find a lowpc, set it to highpc to avoid complaints
7799      from finish_block.  */
7800   if (lowpc == ((CORE_ADDR) -1))
7801     lowpc = highpc;
7802   lowpc += baseaddr;
7803   highpc += baseaddr;
7804
7805   find_file_and_directory (die, cu, &name, &comp_dir);
7806
7807   prepare_one_comp_unit (cu, die, cu->language);
7808
7809   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
7810      standardised yet.  As a workaround for the language detection we fall
7811      back to the DW_AT_producer string.  */
7812   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
7813     cu->language = language_opencl;
7814
7815   /* Similar hack for Go.  */
7816   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
7817     set_cu_language (DW_LANG_Go, cu);
7818
7819   dwarf2_start_symtab (cu, name, comp_dir, lowpc);
7820
7821   /* Decode line number information if present.  We do this before
7822      processing child DIEs, so that the line header table is available
7823      for DW_AT_decl_file.  */
7824   handle_DW_AT_stmt_list (die, cu, comp_dir);
7825
7826   /* Process all dies in compilation unit.  */
7827   if (die->child != NULL)
7828     {
7829       child_die = die->child;
7830       while (child_die && child_die->tag)
7831         {
7832           process_die (child_die, cu);
7833           child_die = sibling_die (child_die);
7834         }
7835     }
7836
7837   /* Decode macro information, if present.  Dwarf 2 macro information
7838      refers to information in the line number info statement program
7839      header, so we can only read it if we've read the header
7840      successfully.  */
7841   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
7842   if (attr && cu->line_header)
7843     {
7844       if (dwarf2_attr (die, DW_AT_macro_info, cu))
7845         complaint (&symfile_complaints,
7846                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
7847
7848       dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
7849     }
7850   else
7851     {
7852       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
7853       if (attr && cu->line_header)
7854         {
7855           unsigned int macro_offset = DW_UNSND (attr);
7856
7857           dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
7858         }
7859     }
7860
7861   do_cleanups (back_to);
7862 }
7863
7864 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
7865    Create the set of symtabs used by this TU, or if this TU is sharing
7866    symtabs with another TU and the symtabs have already been created
7867    then restore those symtabs in the line header.
7868    We don't need the pc/line-number mapping for type units.  */
7869
7870 static void
7871 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
7872 {
7873   struct objfile *objfile = dwarf2_per_objfile->objfile;
7874   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7875   struct type_unit_group *tu_group;
7876   int first_time;
7877   struct line_header *lh;
7878   struct attribute *attr;
7879   unsigned int i, line_offset;
7880
7881   gdb_assert (per_cu->is_debug_types);
7882
7883   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
7884
7885   /* If we're using .gdb_index (includes -readnow) then
7886      per_cu->s.type_unit_group may not have been set up yet.  */
7887   if (per_cu->s.type_unit_group == NULL)
7888     per_cu->s.type_unit_group = get_type_unit_group (cu, attr);
7889   tu_group = per_cu->s.type_unit_group;
7890
7891   /* If we've already processed this stmt_list there's no real need to
7892      do it again, we could fake it and just recreate the part we need
7893      (file name,index -> symtab mapping).  If data shows this optimization
7894      is useful we can do it then.  */
7895   first_time = tu_group->primary_symtab == NULL;
7896
7897   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
7898      debug info.  */
7899   lh = NULL;
7900   if (attr != NULL)
7901     {
7902       line_offset = DW_UNSND (attr);
7903       lh = dwarf_decode_line_header (line_offset, cu);
7904     }
7905   if (lh == NULL)
7906     {
7907       if (first_time)
7908         dwarf2_start_symtab (cu, "", NULL, 0);
7909       else
7910         {
7911           gdb_assert (tu_group->symtabs == NULL);
7912           restart_symtab (0);
7913         }
7914       /* Note: The primary symtab will get allocated at the end.  */
7915       return;
7916     }
7917
7918   cu->line_header = lh;
7919   make_cleanup (free_cu_line_header, cu);
7920
7921   if (first_time)
7922     {
7923       dwarf2_start_symtab (cu, "", NULL, 0);
7924
7925       tu_group->num_symtabs = lh->num_file_names;
7926       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
7927
7928       for (i = 0; i < lh->num_file_names; ++i)
7929         {
7930           char *dir = NULL;
7931           struct file_entry *fe = &lh->file_names[i];
7932
7933           if (fe->dir_index)
7934             dir = lh->include_dirs[fe->dir_index - 1];
7935           dwarf2_start_subfile (fe->name, dir, NULL);
7936
7937           /* Note: We don't have to watch for the main subfile here, type units
7938              don't have DW_AT_name.  */
7939
7940           if (current_subfile->symtab == NULL)
7941             {
7942               /* NOTE: start_subfile will recognize when it's been passed
7943                  a file it has already seen.  So we can't assume there's a
7944                  simple mapping from lh->file_names to subfiles,
7945                  lh->file_names may contain dups.  */
7946               current_subfile->symtab = allocate_symtab (current_subfile->name,
7947                                                          objfile);
7948             }
7949
7950           fe->symtab = current_subfile->symtab;
7951           tu_group->symtabs[i] = fe->symtab;
7952         }
7953     }
7954   else
7955     {
7956       restart_symtab (0);
7957
7958       for (i = 0; i < lh->num_file_names; ++i)
7959         {
7960           struct file_entry *fe = &lh->file_names[i];
7961
7962           fe->symtab = tu_group->symtabs[i];
7963         }
7964     }
7965
7966   /* The main symtab is allocated last.  Type units don't have DW_AT_name
7967      so they don't have a "real" (so to speak) symtab anyway.
7968      There is later code that will assign the main symtab to all symbols
7969      that don't have one.  We need to handle the case of a symbol with a
7970      missing symtab (DW_AT_decl_file) anyway.  */
7971 }
7972
7973 /* Process DW_TAG_type_unit.
7974    For TUs we want to skip the first top level sibling if it's not the
7975    actual type being defined by this TU.  In this case the first top
7976    level sibling is there to provide context only.  */
7977
7978 static void
7979 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
7980 {
7981   struct die_info *child_die;
7982
7983   prepare_one_comp_unit (cu, die, language_minimal);
7984
7985   /* Initialize (or reinitialize) the machinery for building symtabs.
7986      We do this before processing child DIEs, so that the line header table
7987      is available for DW_AT_decl_file.  */
7988   setup_type_unit_groups (die, cu);
7989
7990   if (die->child != NULL)
7991     {
7992       child_die = die->child;
7993       while (child_die && child_die->tag)
7994         {
7995           process_die (child_die, cu);
7996           child_die = sibling_die (child_die);
7997         }
7998     }
7999 }
8000 \f
8001 /* DWO files.  */
8002
8003 static hashval_t
8004 hash_dwo_file (const void *item)
8005 {
8006   const struct dwo_file *dwo_file = item;
8007
8008   return htab_hash_string (dwo_file->dwo_name);
8009 }
8010
8011 static int
8012 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8013 {
8014   const struct dwo_file *lhs = item_lhs;
8015   const struct dwo_file *rhs = item_rhs;
8016
8017   return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
8018 }
8019
8020 /* Allocate a hash table for DWO files.  */
8021
8022 static htab_t
8023 allocate_dwo_file_hash_table (void)
8024 {
8025   struct objfile *objfile = dwarf2_per_objfile->objfile;
8026
8027   return htab_create_alloc_ex (41,
8028                                hash_dwo_file,
8029                                eq_dwo_file,
8030                                NULL,
8031                                &objfile->objfile_obstack,
8032                                hashtab_obstack_allocate,
8033                                dummy_obstack_deallocate);
8034 }
8035
8036 static hashval_t
8037 hash_dwo_unit (const void *item)
8038 {
8039   const struct dwo_unit *dwo_unit = item;
8040
8041   /* This drops the top 32 bits of the id, but is ok for a hash.  */
8042   return dwo_unit->signature;
8043 }
8044
8045 static int
8046 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8047 {
8048   const struct dwo_unit *lhs = item_lhs;
8049   const struct dwo_unit *rhs = item_rhs;
8050
8051   /* The signature is assumed to be unique within the DWO file.
8052      So while object file CU dwo_id's always have the value zero,
8053      that's OK, assuming each object file DWO file has only one CU,
8054      and that's the rule for now.  */
8055   return lhs->signature == rhs->signature;
8056 }
8057
8058 /* Allocate a hash table for DWO CUs,TUs.
8059    There is one of these tables for each of CUs,TUs for each DWO file.  */
8060
8061 static htab_t
8062 allocate_dwo_unit_table (struct objfile *objfile)
8063 {
8064   /* Start out with a pretty small number.
8065      Generally DWO files contain only one CU and maybe some TUs.  */
8066   return htab_create_alloc_ex (3,
8067                                hash_dwo_unit,
8068                                eq_dwo_unit,
8069                                NULL,
8070                                &objfile->objfile_obstack,
8071                                hashtab_obstack_allocate,
8072                                dummy_obstack_deallocate);
8073 }
8074
8075 /* This function is mapped across the sections and remembers the offset and
8076    size of each of the DWO debugging sections we are interested in.  */
8077
8078 static void
8079 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
8080 {
8081   struct dwo_file *dwo_file = dwo_file_ptr;
8082   const struct dwo_section_names *names = &dwo_section_names;
8083
8084   if (section_is_p (sectp->name, &names->abbrev_dwo))
8085     {
8086       dwo_file->sections.abbrev.asection = sectp;
8087       dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
8088     }
8089   else if (section_is_p (sectp->name, &names->info_dwo))
8090     {
8091       dwo_file->sections.info.asection = sectp;
8092       dwo_file->sections.info.size = bfd_get_section_size (sectp);
8093     }
8094   else if (section_is_p (sectp->name, &names->line_dwo))
8095     {
8096       dwo_file->sections.line.asection = sectp;
8097       dwo_file->sections.line.size = bfd_get_section_size (sectp);
8098     }
8099   else if (section_is_p (sectp->name, &names->loc_dwo))
8100     {
8101       dwo_file->sections.loc.asection = sectp;
8102       dwo_file->sections.loc.size = bfd_get_section_size (sectp);
8103     }
8104   else if (section_is_p (sectp->name, &names->macinfo_dwo))
8105     {
8106       dwo_file->sections.macinfo.asection = sectp;
8107       dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
8108     }
8109   else if (section_is_p (sectp->name, &names->macro_dwo))
8110     {
8111       dwo_file->sections.macro.asection = sectp;
8112       dwo_file->sections.macro.size = bfd_get_section_size (sectp);
8113     }
8114   else if (section_is_p (sectp->name, &names->str_dwo))
8115     {
8116       dwo_file->sections.str.asection = sectp;
8117       dwo_file->sections.str.size = bfd_get_section_size (sectp);
8118     }
8119   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
8120     {
8121       dwo_file->sections.str_offsets.asection = sectp;
8122       dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
8123     }
8124   else if (section_is_p (sectp->name, &names->types_dwo))
8125     {
8126       struct dwarf2_section_info type_section;
8127
8128       memset (&type_section, 0, sizeof (type_section));
8129       type_section.asection = sectp;
8130       type_section.size = bfd_get_section_size (sectp);
8131       VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
8132                      &type_section);
8133     }
8134 }
8135
8136 /* Structure used to pass data to create_debug_info_hash_table_reader.  */
8137
8138 struct create_dwo_info_table_data
8139 {
8140   struct dwo_file *dwo_file;
8141   htab_t cu_htab;
8142 };
8143
8144 /* die_reader_func for create_debug_info_hash_table.  */
8145
8146 static void
8147 create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
8148                                      gdb_byte *info_ptr,
8149                                      struct die_info *comp_unit_die,
8150                                      int has_children,
8151                                      void *datap)
8152 {
8153   struct dwarf2_cu *cu = reader->cu;
8154   struct objfile *objfile = dwarf2_per_objfile->objfile;
8155   sect_offset offset = cu->per_cu->offset;
8156   struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
8157   struct create_dwo_info_table_data *data = datap;
8158   struct dwo_file *dwo_file = data->dwo_file;
8159   htab_t cu_htab = data->cu_htab;
8160   void **slot;
8161   struct attribute *attr;
8162   struct dwo_unit *dwo_unit;
8163
8164   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8165   if (attr == NULL)
8166     {
8167       error (_("Dwarf Error: debug entry at offset 0x%x is missing"
8168                " its dwo_id [in module %s]"),
8169              offset.sect_off, dwo_file->dwo_name);
8170       return;
8171     }
8172
8173   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8174   dwo_unit->dwo_file = dwo_file;
8175   dwo_unit->signature = DW_UNSND (attr);
8176   dwo_unit->info_or_types_section = section;
8177   dwo_unit->offset = offset;
8178   dwo_unit->length = cu->per_cu->length;
8179
8180   slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
8181   gdb_assert (slot != NULL);
8182   if (*slot != NULL)
8183     {
8184       const struct dwo_unit *dup_dwo_unit = *slot;
8185
8186       complaint (&symfile_complaints,
8187                  _("debug entry at offset 0x%x is duplicate to the entry at"
8188                    " offset 0x%x, dwo_id 0x%s [in module %s]"),
8189                  offset.sect_off, dup_dwo_unit->offset.sect_off,
8190                  phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
8191                  dwo_file->dwo_name);
8192     }
8193   else
8194     *slot = dwo_unit;
8195
8196   if (dwarf2_read_debug)
8197     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id 0x%s\n",
8198                         offset.sect_off,
8199                         phex (dwo_unit->signature,
8200                               sizeof (dwo_unit->signature)));
8201 }
8202
8203 /* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo.  */
8204
8205 static htab_t
8206 create_debug_info_hash_table (struct dwo_file *dwo_file)
8207 {
8208   struct objfile *objfile = dwarf2_per_objfile->objfile;
8209   struct dwarf2_section_info *section = &dwo_file->sections.info;
8210   bfd *abfd;
8211   htab_t cu_htab;
8212   gdb_byte *info_ptr, *end_ptr;
8213   struct create_dwo_info_table_data create_dwo_info_table_data;
8214
8215   dwarf2_read_section (objfile, section);
8216   info_ptr = section->buffer;
8217
8218   if (info_ptr == NULL)
8219     return NULL;
8220
8221   /* We can't set abfd until now because the section may be empty or
8222      not present, in which case section->asection will be NULL.  */
8223   abfd = section->asection->owner;
8224
8225   if (dwarf2_read_debug)
8226     fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
8227                         bfd_get_filename (abfd));
8228
8229   cu_htab = allocate_dwo_unit_table (objfile);
8230
8231   create_dwo_info_table_data.dwo_file = dwo_file;
8232   create_dwo_info_table_data.cu_htab = cu_htab;
8233
8234   end_ptr = info_ptr + section->size;
8235   while (info_ptr < end_ptr)
8236     {
8237       struct dwarf2_per_cu_data per_cu;
8238
8239       memset (&per_cu, 0, sizeof (per_cu));
8240       per_cu.objfile = objfile;
8241       per_cu.is_debug_types = 0;
8242       per_cu.offset.sect_off = info_ptr - section->buffer;
8243       per_cu.info_or_types_section = section;
8244
8245       init_cutu_and_read_dies_no_follow (&per_cu,
8246                                          &dwo_file->sections.abbrev,
8247                                          dwo_file,
8248                                          create_debug_info_hash_table_reader,
8249                                          &create_dwo_info_table_data);
8250
8251       info_ptr += per_cu.length;
8252     }
8253
8254   return cu_htab;
8255 }
8256
8257 /* Subroutine of open_dwo_file to simplify it.
8258    Open the file specified by FILE_NAME and hand it off to BFD for
8259    preliminary analysis.  Return a newly initialized bfd *, which
8260    includes a canonicalized copy of FILE_NAME.
8261    In case of trouble, return NULL.
8262    NOTE: This function is derived from symfile_bfd_open.  */
8263
8264 static bfd *
8265 try_open_dwo_file (const char *file_name)
8266 {
8267   bfd *sym_bfd;
8268   int desc;
8269   char *absolute_name;
8270
8271   desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
8272                 O_RDONLY | O_BINARY, &absolute_name);
8273   if (desc < 0)
8274     return NULL;
8275
8276   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
8277   if (!sym_bfd)
8278     {
8279       xfree (absolute_name);
8280       return NULL;
8281     }
8282   xfree (absolute_name);
8283   bfd_set_cacheable (sym_bfd, 1);
8284
8285   if (!bfd_check_format (sym_bfd, bfd_object))
8286     {
8287       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
8288       return NULL;
8289     }
8290
8291   return sym_bfd;
8292 }
8293
8294 /* Try to open DWO file DWO_NAME.
8295    COMP_DIR is the DW_AT_comp_dir attribute.
8296    The result is the bfd handle of the file.
8297    If there is a problem finding or opening the file, return NULL.
8298    Upon success, the canonicalized path of the file is stored in the bfd,
8299    same as symfile_bfd_open.  */
8300
8301 static bfd *
8302 open_dwo_file (const char *dwo_name, const char *comp_dir)
8303 {
8304   bfd *abfd;
8305
8306   if (IS_ABSOLUTE_PATH (dwo_name))
8307     return try_open_dwo_file (dwo_name);
8308
8309   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
8310
8311   if (comp_dir != NULL)
8312     {
8313       char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
8314
8315       /* NOTE: If comp_dir is a relative path, this will also try the
8316          search path, which seems useful.  */
8317       abfd = try_open_dwo_file (path_to_try);
8318       xfree (path_to_try);
8319       if (abfd != NULL)
8320         return abfd;
8321     }
8322
8323   /* That didn't work, try debug-file-directory, which, despite its name,
8324      is a list of paths.  */
8325
8326   if (*debug_file_directory == '\0')
8327     return NULL;
8328
8329   return try_open_dwo_file (dwo_name);
8330 }
8331
8332 /* Initialize the use of the DWO file specified by DWO_NAME.  */
8333
8334 static struct dwo_file *
8335 init_dwo_file (const char *dwo_name, const char *comp_dir)
8336 {
8337   struct objfile *objfile = dwarf2_per_objfile->objfile;
8338   struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8339                                               struct dwo_file);
8340   bfd *abfd;
8341   struct cleanup *cleanups;
8342
8343   if (dwarf2_read_debug)
8344     fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
8345
8346   abfd = open_dwo_file (dwo_name, comp_dir);
8347   if (abfd == NULL)
8348     return NULL;
8349   dwo_file->dwo_name = dwo_name;
8350   dwo_file->dwo_bfd = abfd;
8351
8352   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
8353
8354   bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
8355
8356   dwo_file->cus = create_debug_info_hash_table (dwo_file);
8357
8358   dwo_file->tus = create_debug_types_hash_table (dwo_file,
8359                                                  dwo_file->sections.types);
8360
8361   discard_cleanups (cleanups);
8362
8363   return dwo_file;
8364 }
8365
8366 /* Lookup DWO file DWO_NAME.  */
8367
8368 static struct dwo_file *
8369 lookup_dwo_file (char *dwo_name, const char *comp_dir)
8370 {
8371   struct dwo_file *dwo_file;
8372   struct dwo_file find_entry;
8373   void **slot;
8374
8375   if (dwarf2_per_objfile->dwo_files == NULL)
8376     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8377
8378   /* Have we already seen this DWO file?  */
8379   find_entry.dwo_name = dwo_name;
8380   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8381
8382   /* If not, read it in and build a table of the DWOs it contains.  */
8383   if (*slot == NULL)
8384     *slot = init_dwo_file (dwo_name, comp_dir);
8385
8386   /* NOTE: This will be NULL if unable to open the file.  */
8387   dwo_file = *slot;
8388
8389   return dwo_file;
8390 }
8391
8392 /* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
8393    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8394    SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
8395    nomenclature as TUs).
8396    The result is a pointer to the dwo_unit object or NULL if we didn't find it
8397    (dwo_id mismatch or couldn't find the DWO file).  */
8398
8399 static struct dwo_unit *
8400 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
8401                       char *dwo_name, const char *comp_dir,
8402                       ULONGEST signature)
8403 {
8404   struct objfile *objfile = dwarf2_per_objfile->objfile;
8405   struct dwo_file *dwo_file;
8406
8407   dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8408   if (dwo_file == NULL)
8409     return NULL;
8410
8411   /* Look up the DWO using its signature(dwo_id).  */
8412
8413   if (dwo_file->cus != NULL)
8414     {
8415       struct dwo_unit find_dwo_cu, *dwo_cu;
8416
8417       find_dwo_cu.signature = signature;
8418       dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
8419
8420       if (dwo_cu != NULL)
8421         return dwo_cu;
8422     }
8423
8424   /* We didn't find it.  This must mean a dwo_id mismatch.  */
8425
8426   complaint (&symfile_complaints,
8427              _("Could not find DWO CU referenced by CU at offset 0x%x"
8428                " [in module %s]"),
8429              this_cu->offset.sect_off, objfile->name);
8430   return NULL;
8431 }
8432
8433 /* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
8434    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
8435    The result is a pointer to the dwo_unit object or NULL if we didn't find it
8436    (dwo_id mismatch or couldn't find the DWO file).  */
8437
8438 static struct dwo_unit *
8439 lookup_dwo_type_unit (struct signatured_type *this_tu,
8440                       char *dwo_name, const char *comp_dir)
8441 {
8442   struct objfile *objfile = dwarf2_per_objfile->objfile;
8443   struct dwo_file *dwo_file;
8444
8445   dwo_file = lookup_dwo_file (dwo_name, comp_dir);
8446   if (dwo_file == NULL)
8447     return NULL;
8448
8449   /* Look up the DWO using its signature(dwo_id).  */
8450
8451   if (dwo_file->tus != NULL)
8452     {
8453       struct dwo_unit find_dwo_tu, *dwo_tu;
8454
8455       find_dwo_tu.signature = this_tu->signature;
8456       dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
8457
8458       if (dwo_tu != NULL)
8459         return dwo_tu;
8460     }
8461
8462   /* We didn't find it.  This must mean a dwo_id mismatch.  */
8463
8464   complaint (&symfile_complaints,
8465              _("Could not find DWO TU referenced by TU at offset 0x%x"
8466                " [in module %s]"),
8467              this_tu->per_cu.offset.sect_off, objfile->name);
8468   return NULL;
8469 }
8470
8471 /* Free all resources associated with DWO_FILE.
8472    Close the DWO file and munmap the sections.
8473    All memory should be on the objfile obstack.  */
8474
8475 static void
8476 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
8477 {
8478   int ix;
8479   struct dwarf2_section_info *section;
8480
8481   gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
8482   gdb_bfd_unref (dwo_file->dwo_bfd);
8483
8484   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
8485 }
8486
8487 /* Wrapper for free_dwo_file for use in cleanups.  */
8488
8489 static void
8490 free_dwo_file_cleanup (void *arg)
8491 {
8492   struct dwo_file *dwo_file = (struct dwo_file *) arg;
8493   struct objfile *objfile = dwarf2_per_objfile->objfile;
8494
8495   free_dwo_file (dwo_file, objfile);
8496 }
8497
8498 /* Traversal function for free_dwo_files.  */
8499
8500 static int
8501 free_dwo_file_from_slot (void **slot, void *info)
8502 {
8503   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8504   struct objfile *objfile = (struct objfile *) info;
8505
8506   free_dwo_file (dwo_file, objfile);
8507
8508   return 1;
8509 }
8510
8511 /* Free all resources associated with DWO_FILES.  */
8512
8513 static void
8514 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
8515 {
8516   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
8517 }
8518 \f
8519 /* Read in various DIEs.  */
8520
8521 /* qsort helper for inherit_abstract_dies.  */
8522
8523 static int
8524 unsigned_int_compar (const void *ap, const void *bp)
8525 {
8526   unsigned int a = *(unsigned int *) ap;
8527   unsigned int b = *(unsigned int *) bp;
8528
8529   return (a > b) - (b > a);
8530 }
8531
8532 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
8533    Inherit only the children of the DW_AT_abstract_origin DIE not being
8534    already referenced by DW_AT_abstract_origin from the children of the
8535    current DIE.  */
8536
8537 static void
8538 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
8539 {
8540   struct die_info *child_die;
8541   unsigned die_children_count;
8542   /* CU offsets which were referenced by children of the current DIE.  */
8543   sect_offset *offsets;
8544   sect_offset *offsets_end, *offsetp;
8545   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
8546   struct die_info *origin_die;
8547   /* Iterator of the ORIGIN_DIE children.  */
8548   struct die_info *origin_child_die;
8549   struct cleanup *cleanups;
8550   struct attribute *attr;
8551   struct dwarf2_cu *origin_cu;
8552   struct pending **origin_previous_list_in_scope;
8553
8554   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8555   if (!attr)
8556     return;
8557
8558   /* Note that following die references may follow to a die in a
8559      different cu.  */
8560
8561   origin_cu = cu;
8562   origin_die = follow_die_ref (die, attr, &origin_cu);
8563
8564   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
8565      symbols in.  */
8566   origin_previous_list_in_scope = origin_cu->list_in_scope;
8567   origin_cu->list_in_scope = cu->list_in_scope;
8568
8569   if (die->tag != origin_die->tag
8570       && !(die->tag == DW_TAG_inlined_subroutine
8571            && origin_die->tag == DW_TAG_subprogram))
8572     complaint (&symfile_complaints,
8573                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
8574                die->offset.sect_off, origin_die->offset.sect_off);
8575
8576   child_die = die->child;
8577   die_children_count = 0;
8578   while (child_die && child_die->tag)
8579     {
8580       child_die = sibling_die (child_die);
8581       die_children_count++;
8582     }
8583   offsets = xmalloc (sizeof (*offsets) * die_children_count);
8584   cleanups = make_cleanup (xfree, offsets);
8585
8586   offsets_end = offsets;
8587   child_die = die->child;
8588   while (child_die && child_die->tag)
8589     {
8590       /* For each CHILD_DIE, find the corresponding child of
8591          ORIGIN_DIE.  If there is more than one layer of
8592          DW_AT_abstract_origin, follow them all; there shouldn't be,
8593          but GCC versions at least through 4.4 generate this (GCC PR
8594          40573).  */
8595       struct die_info *child_origin_die = child_die;
8596       struct dwarf2_cu *child_origin_cu = cu;
8597
8598       while (1)
8599         {
8600           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
8601                               child_origin_cu);
8602           if (attr == NULL)
8603             break;
8604           child_origin_die = follow_die_ref (child_origin_die, attr,
8605                                              &child_origin_cu);
8606         }
8607
8608       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
8609          counterpart may exist.  */
8610       if (child_origin_die != child_die)
8611         {
8612           if (child_die->tag != child_origin_die->tag
8613               && !(child_die->tag == DW_TAG_inlined_subroutine
8614                    && child_origin_die->tag == DW_TAG_subprogram))
8615             complaint (&symfile_complaints,
8616                        _("Child DIE 0x%x and its abstract origin 0x%x have "
8617                          "different tags"), child_die->offset.sect_off,
8618                        child_origin_die->offset.sect_off);
8619           if (child_origin_die->parent != origin_die)
8620             complaint (&symfile_complaints,
8621                        _("Child DIE 0x%x and its abstract origin 0x%x have "
8622                          "different parents"), child_die->offset.sect_off,
8623                        child_origin_die->offset.sect_off);
8624           else
8625             *offsets_end++ = child_origin_die->offset;
8626         }
8627       child_die = sibling_die (child_die);
8628     }
8629   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
8630          unsigned_int_compar);
8631   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
8632     if (offsetp[-1].sect_off == offsetp->sect_off)
8633       complaint (&symfile_complaints,
8634                  _("Multiple children of DIE 0x%x refer "
8635                    "to DIE 0x%x as their abstract origin"),
8636                  die->offset.sect_off, offsetp->sect_off);
8637
8638   offsetp = offsets;
8639   origin_child_die = origin_die->child;
8640   while (origin_child_die && origin_child_die->tag)
8641     {
8642       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
8643       while (offsetp < offsets_end
8644              && offsetp->sect_off < origin_child_die->offset.sect_off)
8645         offsetp++;
8646       if (offsetp >= offsets_end
8647           || offsetp->sect_off > origin_child_die->offset.sect_off)
8648         {
8649           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
8650           process_die (origin_child_die, origin_cu);
8651         }
8652       origin_child_die = sibling_die (origin_child_die);
8653     }
8654   origin_cu->list_in_scope = origin_previous_list_in_scope;
8655
8656   do_cleanups (cleanups);
8657 }
8658
8659 static void
8660 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
8661 {
8662   struct objfile *objfile = cu->objfile;
8663   struct context_stack *new;
8664   CORE_ADDR lowpc;
8665   CORE_ADDR highpc;
8666   struct die_info *child_die;
8667   struct attribute *attr, *call_line, *call_file;
8668   char *name;
8669   CORE_ADDR baseaddr;
8670   struct block *block;
8671   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
8672   VEC (symbolp) *template_args = NULL;
8673   struct template_symbol *templ_func = NULL;
8674
8675   if (inlined_func)
8676     {
8677       /* If we do not have call site information, we can't show the
8678          caller of this inlined function.  That's too confusing, so
8679          only use the scope for local variables.  */
8680       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
8681       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
8682       if (call_line == NULL || call_file == NULL)
8683         {
8684           read_lexical_block_scope (die, cu);
8685           return;
8686         }
8687     }
8688
8689   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8690
8691   name = dwarf2_name (die, cu);
8692
8693   /* Ignore functions with missing or empty names.  These are actually
8694      illegal according to the DWARF standard.  */
8695   if (name == NULL)
8696     {
8697       complaint (&symfile_complaints,
8698                  _("missing name for subprogram DIE at %d"),
8699                  die->offset.sect_off);
8700       return;
8701     }
8702
8703   /* Ignore functions with missing or invalid low and high pc attributes.  */
8704   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8705     {
8706       attr = dwarf2_attr (die, DW_AT_external, cu);
8707       if (!attr || !DW_UNSND (attr))
8708         complaint (&symfile_complaints,
8709                    _("cannot get low and high bounds "
8710                      "for subprogram DIE at %d"),
8711                    die->offset.sect_off);
8712       return;
8713     }
8714
8715   lowpc += baseaddr;
8716   highpc += baseaddr;
8717
8718   /* If we have any template arguments, then we must allocate a
8719      different sort of symbol.  */
8720   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
8721     {
8722       if (child_die->tag == DW_TAG_template_type_param
8723           || child_die->tag == DW_TAG_template_value_param)
8724         {
8725           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
8726                                        struct template_symbol);
8727           templ_func->base.is_cplus_template_function = 1;
8728           break;
8729         }
8730     }
8731
8732   new = push_context (0, lowpc);
8733   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
8734                                (struct symbol *) templ_func);
8735
8736   /* If there is a location expression for DW_AT_frame_base, record
8737      it.  */
8738   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
8739   if (attr)
8740     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
8741        expression is being recorded directly in the function's symbol
8742        and not in a separate frame-base object.  I guess this hack is
8743        to avoid adding some sort of frame-base adjunct/annex to the
8744        function's symbol :-(.  The problem with doing this is that it
8745        results in a function symbol with a location expression that
8746        has nothing to do with the location of the function, ouch!  The
8747        relationship should be: a function's symbol has-a frame base; a
8748        frame-base has-a location expression.  */
8749     dwarf2_symbol_mark_computed (attr, new->name, cu);
8750
8751   cu->list_in_scope = &local_symbols;
8752
8753   if (die->child != NULL)
8754     {
8755       child_die = die->child;
8756       while (child_die && child_die->tag)
8757         {
8758           if (child_die->tag == DW_TAG_template_type_param
8759               || child_die->tag == DW_TAG_template_value_param)
8760             {
8761               struct symbol *arg = new_symbol (child_die, NULL, cu);
8762
8763               if (arg != NULL)
8764                 VEC_safe_push (symbolp, template_args, arg);
8765             }
8766           else
8767             process_die (child_die, cu);
8768           child_die = sibling_die (child_die);
8769         }
8770     }
8771
8772   inherit_abstract_dies (die, cu);
8773
8774   /* If we have a DW_AT_specification, we might need to import using
8775      directives from the context of the specification DIE.  See the
8776      comment in determine_prefix.  */
8777   if (cu->language == language_cplus
8778       && dwarf2_attr (die, DW_AT_specification, cu))
8779     {
8780       struct dwarf2_cu *spec_cu = cu;
8781       struct die_info *spec_die = die_specification (die, &spec_cu);
8782
8783       while (spec_die)
8784         {
8785           child_die = spec_die->child;
8786           while (child_die && child_die->tag)
8787             {
8788               if (child_die->tag == DW_TAG_imported_module)
8789                 process_die (child_die, spec_cu);
8790               child_die = sibling_die (child_die);
8791             }
8792
8793           /* In some cases, GCC generates specification DIEs that
8794              themselves contain DW_AT_specification attributes.  */
8795           spec_die = die_specification (spec_die, &spec_cu);
8796         }
8797     }
8798
8799   new = pop_context ();
8800   /* Make a block for the local symbols within.  */
8801   block = finish_block (new->name, &local_symbols, new->old_blocks,
8802                         lowpc, highpc, objfile);
8803
8804   /* For C++, set the block's scope.  */
8805   if (cu->language == language_cplus || cu->language == language_fortran)
8806     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
8807                         determine_prefix (die, cu),
8808                         processing_has_namespace_info);
8809
8810   /* If we have address ranges, record them.  */
8811   dwarf2_record_block_ranges (die, block, baseaddr, cu);
8812
8813   /* Attach template arguments to function.  */
8814   if (! VEC_empty (symbolp, template_args))
8815     {
8816       gdb_assert (templ_func != NULL);
8817
8818       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
8819       templ_func->template_arguments
8820         = obstack_alloc (&objfile->objfile_obstack,
8821                          (templ_func->n_template_arguments
8822                           * sizeof (struct symbol *)));
8823       memcpy (templ_func->template_arguments,
8824               VEC_address (symbolp, template_args),
8825               (templ_func->n_template_arguments * sizeof (struct symbol *)));
8826       VEC_free (symbolp, template_args);
8827     }
8828
8829   /* In C++, we can have functions nested inside functions (e.g., when
8830      a function declares a class that has methods).  This means that
8831      when we finish processing a function scope, we may need to go
8832      back to building a containing block's symbol lists.  */
8833   local_symbols = new->locals;
8834   param_symbols = new->params;
8835   using_directives = new->using_directives;
8836
8837   /* If we've finished processing a top-level function, subsequent
8838      symbols go in the file symbol list.  */
8839   if (outermost_context_p ())
8840     cu->list_in_scope = &file_symbols;
8841 }
8842
8843 /* Process all the DIES contained within a lexical block scope.  Start
8844    a new scope, process the dies, and then close the scope.  */
8845
8846 static void
8847 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
8848 {
8849   struct objfile *objfile = cu->objfile;
8850   struct context_stack *new;
8851   CORE_ADDR lowpc, highpc;
8852   struct die_info *child_die;
8853   CORE_ADDR baseaddr;
8854
8855   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8856
8857   /* Ignore blocks with missing or invalid low and high pc attributes.  */
8858   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
8859      as multiple lexical blocks?  Handling children in a sane way would
8860      be nasty.  Might be easier to properly extend generic blocks to
8861      describe ranges.  */
8862   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
8863     return;
8864   lowpc += baseaddr;
8865   highpc += baseaddr;
8866
8867   push_context (0, lowpc);
8868   if (die->child != NULL)
8869     {
8870       child_die = die->child;
8871       while (child_die && child_die->tag)
8872         {
8873           process_die (child_die, cu);
8874           child_die = sibling_die (child_die);
8875         }
8876     }
8877   new = pop_context ();
8878
8879   if (local_symbols != NULL || using_directives != NULL)
8880     {
8881       struct block *block
8882         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
8883                         highpc, objfile);
8884
8885       /* Note that recording ranges after traversing children, as we
8886          do here, means that recording a parent's ranges entails
8887          walking across all its children's ranges as they appear in
8888          the address map, which is quadratic behavior.
8889
8890          It would be nicer to record the parent's ranges before
8891          traversing its children, simply overriding whatever you find
8892          there.  But since we don't even decide whether to create a
8893          block until after we've traversed its children, that's hard
8894          to do.  */
8895       dwarf2_record_block_ranges (die, block, baseaddr, cu);
8896     }
8897   local_symbols = new->locals;
8898   using_directives = new->using_directives;
8899 }
8900
8901 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
8902
8903 static void
8904 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
8905 {
8906   struct objfile *objfile = cu->objfile;
8907   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8908   CORE_ADDR pc, baseaddr;
8909   struct attribute *attr;
8910   struct call_site *call_site, call_site_local;
8911   void **slot;
8912   int nparams;
8913   struct die_info *child_die;
8914
8915   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8916
8917   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8918   if (!attr)
8919     {
8920       complaint (&symfile_complaints,
8921                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
8922                    "DIE 0x%x [in module %s]"),
8923                  die->offset.sect_off, objfile->name);
8924       return;
8925     }
8926   pc = DW_ADDR (attr) + baseaddr;
8927
8928   if (cu->call_site_htab == NULL)
8929     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
8930                                                NULL, &objfile->objfile_obstack,
8931                                                hashtab_obstack_allocate, NULL);
8932   call_site_local.pc = pc;
8933   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
8934   if (*slot != NULL)
8935     {
8936       complaint (&symfile_complaints,
8937                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
8938                    "DIE 0x%x [in module %s]"),
8939                  paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
8940       return;
8941     }
8942
8943   /* Count parameters at the caller.  */
8944
8945   nparams = 0;
8946   for (child_die = die->child; child_die && child_die->tag;
8947        child_die = sibling_die (child_die))
8948     {
8949       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8950         {
8951           complaint (&symfile_complaints,
8952                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
8953                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
8954                      child_die->tag, child_die->offset.sect_off, objfile->name);
8955           continue;
8956         }
8957
8958       nparams++;
8959     }
8960
8961   call_site = obstack_alloc (&objfile->objfile_obstack,
8962                              (sizeof (*call_site)
8963                               + (sizeof (*call_site->parameter)
8964                                  * (nparams - 1))));
8965   *slot = call_site;
8966   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
8967   call_site->pc = pc;
8968
8969   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
8970     {
8971       struct die_info *func_die;
8972
8973       /* Skip also over DW_TAG_inlined_subroutine.  */
8974       for (func_die = die->parent;
8975            func_die && func_die->tag != DW_TAG_subprogram
8976            && func_die->tag != DW_TAG_subroutine_type;
8977            func_die = func_die->parent);
8978
8979       /* DW_AT_GNU_all_call_sites is a superset
8980          of DW_AT_GNU_all_tail_call_sites.  */
8981       if (func_die
8982           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8983           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8984         {
8985           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8986              not complete.  But keep CALL_SITE for look ups via call_site_htab,
8987              both the initial caller containing the real return address PC and
8988              the final callee containing the current PC of a chain of tail
8989              calls do not need to have the tail call list complete.  But any
8990              function candidate for a virtual tail call frame searched via
8991              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8992              determined unambiguously.  */
8993         }
8994       else
8995         {
8996           struct type *func_type = NULL;
8997
8998           if (func_die)
8999             func_type = get_die_type (func_die, cu);
9000           if (func_type != NULL)
9001             {
9002               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
9003
9004               /* Enlist this call site to the function.  */
9005               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
9006               TYPE_TAIL_CALL_LIST (func_type) = call_site;
9007             }
9008           else
9009             complaint (&symfile_complaints,
9010                        _("Cannot find function owning DW_TAG_GNU_call_site "
9011                          "DIE 0x%x [in module %s]"),
9012                        die->offset.sect_off, objfile->name);
9013         }
9014     }
9015
9016   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
9017   if (attr == NULL)
9018     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9019   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9020   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
9021     /* Keep NULL DWARF_BLOCK.  */;
9022   else if (attr_form_is_block (attr))
9023     {
9024       struct dwarf2_locexpr_baton *dlbaton;
9025
9026       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
9027       dlbaton->data = DW_BLOCK (attr)->data;
9028       dlbaton->size = DW_BLOCK (attr)->size;
9029       dlbaton->per_cu = cu->per_cu;
9030
9031       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
9032     }
9033   else if (is_ref_attr (attr))
9034     {
9035       struct dwarf2_cu *target_cu = cu;
9036       struct die_info *target_die;
9037
9038       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
9039       gdb_assert (target_cu->objfile == objfile);
9040       if (die_is_declaration (target_die, target_cu))
9041         {
9042           const char *target_physname;
9043
9044           target_physname = dwarf2_physname (NULL, target_die, target_cu);
9045           if (target_physname == NULL)
9046             complaint (&symfile_complaints,
9047                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9048                          "physname, for referencing DIE 0x%x [in module %s]"),
9049                        die->offset.sect_off, objfile->name);
9050           else
9051             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
9052         }
9053       else
9054         {
9055           CORE_ADDR lowpc;
9056
9057           /* DW_AT_entry_pc should be preferred.  */
9058           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
9059             complaint (&symfile_complaints,
9060                        _("DW_AT_GNU_call_site_target target DIE has invalid "
9061                          "low pc, for referencing DIE 0x%x [in module %s]"),
9062                        die->offset.sect_off, objfile->name);
9063           else
9064             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
9065         }
9066     }
9067   else
9068     complaint (&symfile_complaints,
9069                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
9070                  "block nor reference, for DIE 0x%x [in module %s]"),
9071                die->offset.sect_off, objfile->name);
9072
9073   call_site->per_cu = cu->per_cu;
9074
9075   for (child_die = die->child;
9076        child_die && child_die->tag;
9077        child_die = sibling_die (child_die))
9078     {
9079       struct call_site_parameter *parameter;
9080       struct attribute *loc, *origin;
9081
9082       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
9083         {
9084           /* Already printed the complaint above.  */
9085           continue;
9086         }
9087
9088       gdb_assert (call_site->parameter_count < nparams);
9089       parameter = &call_site->parameter[call_site->parameter_count];
9090
9091       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
9092          specifies DW_TAG_formal_parameter.  Value of the data assumed for the
9093          register is contained in DW_AT_GNU_call_site_value.  */
9094
9095       loc = dwarf2_attr (child_die, DW_AT_location, cu);
9096       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
9097       if (loc == NULL && origin != NULL && is_ref_attr (origin))
9098         {
9099           sect_offset offset;
9100
9101           parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9102           offset = dwarf2_get_ref_die_offset (origin);
9103           if (!offset_in_cu_p (&cu->header, offset))
9104             {
9105               /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
9106                  binding can be done only inside one CU.  Such referenced DIE
9107                  therefore cannot be even moved to DW_TAG_partial_unit.  */
9108               complaint (&symfile_complaints,
9109                          _("DW_AT_abstract_origin offset is not in CU for "
9110                            "DW_TAG_GNU_call_site child DIE 0x%x "
9111                            "[in module %s]"),
9112                          child_die->offset.sect_off, objfile->name);
9113               continue;
9114             }
9115           parameter->u.param_offset.cu_off = (offset.sect_off
9116                                               - cu->header.offset.sect_off);
9117         }
9118       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
9119         {
9120           complaint (&symfile_complaints,
9121                      _("No DW_FORM_block* DW_AT_location for "
9122                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9123                      child_die->offset.sect_off, objfile->name);
9124           continue;
9125         }
9126       else
9127         {
9128           parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9129             (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
9130           if (parameter->u.dwarf_reg != -1)
9131             parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9132           else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
9133                                     &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
9134                                              &parameter->u.fb_offset))
9135             parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
9136           else
9137             {
9138               complaint (&symfile_complaints,
9139                          _("Only single DW_OP_reg or DW_OP_fbreg is supported "
9140                            "for DW_FORM_block* DW_AT_location is supported for "
9141                            "DW_TAG_GNU_call_site child DIE 0x%x "
9142                            "[in module %s]"),
9143                          child_die->offset.sect_off, objfile->name);
9144               continue;
9145             }
9146         }
9147
9148       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
9149       if (!attr_form_is_block (attr))
9150         {
9151           complaint (&symfile_complaints,
9152                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
9153                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9154                      child_die->offset.sect_off, objfile->name);
9155           continue;
9156         }
9157       parameter->value = DW_BLOCK (attr)->data;
9158       parameter->value_size = DW_BLOCK (attr)->size;
9159
9160       /* Parameters are not pre-cleared by memset above.  */
9161       parameter->data_value = NULL;
9162       parameter->data_value_size = 0;
9163       call_site->parameter_count++;
9164
9165       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
9166       if (attr)
9167         {
9168           if (!attr_form_is_block (attr))
9169             complaint (&symfile_complaints,
9170                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
9171                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
9172                        child_die->offset.sect_off, objfile->name);
9173           else
9174             {
9175               parameter->data_value = DW_BLOCK (attr)->data;
9176               parameter->data_value_size = DW_BLOCK (attr)->size;
9177             }
9178         }
9179     }
9180 }
9181
9182 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
9183    Return 1 if the attributes are present and valid, otherwise, return 0.
9184    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
9185
9186 static int
9187 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
9188                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
9189                     struct partial_symtab *ranges_pst)
9190 {
9191   struct objfile *objfile = cu->objfile;
9192   struct comp_unit_head *cu_header = &cu->header;
9193   bfd *obfd = objfile->obfd;
9194   unsigned int addr_size = cu_header->addr_size;
9195   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9196   /* Base address selection entry.  */
9197   CORE_ADDR base;
9198   int found_base;
9199   unsigned int dummy;
9200   gdb_byte *buffer;
9201   CORE_ADDR marker;
9202   int low_set;
9203   CORE_ADDR low = 0;
9204   CORE_ADDR high = 0;
9205   CORE_ADDR baseaddr;
9206
9207   found_base = cu->base_known;
9208   base = cu->base_address;
9209
9210   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
9211   if (offset >= dwarf2_per_objfile->ranges.size)
9212     {
9213       complaint (&symfile_complaints,
9214                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
9215                  offset);
9216       return 0;
9217     }
9218   buffer = dwarf2_per_objfile->ranges.buffer + offset;
9219
9220   /* Read in the largest possible address.  */
9221   marker = read_address (obfd, buffer, cu, &dummy);
9222   if ((marker & mask) == mask)
9223     {
9224       /* If we found the largest possible address, then
9225          read the base address.  */
9226       base = read_address (obfd, buffer + addr_size, cu, &dummy);
9227       buffer += 2 * addr_size;
9228       offset += 2 * addr_size;
9229       found_base = 1;
9230     }
9231
9232   low_set = 0;
9233
9234   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9235
9236   while (1)
9237     {
9238       CORE_ADDR range_beginning, range_end;
9239
9240       range_beginning = read_address (obfd, buffer, cu, &dummy);
9241       buffer += addr_size;
9242       range_end = read_address (obfd, buffer, cu, &dummy);
9243       buffer += addr_size;
9244       offset += 2 * addr_size;
9245
9246       /* An end of list marker is a pair of zero addresses.  */
9247       if (range_beginning == 0 && range_end == 0)
9248         /* Found the end of list entry.  */
9249         break;
9250
9251       /* Each base address selection entry is a pair of 2 values.
9252          The first is the largest possible address, the second is
9253          the base address.  Check for a base address here.  */
9254       if ((range_beginning & mask) == mask)
9255         {
9256           /* If we found the largest possible address, then
9257              read the base address.  */
9258           base = read_address (obfd, buffer + addr_size, cu, &dummy);
9259           found_base = 1;
9260           continue;
9261         }
9262
9263       if (!found_base)
9264         {
9265           /* We have no valid base address for the ranges
9266              data.  */
9267           complaint (&symfile_complaints,
9268                      _("Invalid .debug_ranges data (no base address)"));
9269           return 0;
9270         }
9271
9272       if (range_beginning > range_end)
9273         {
9274           /* Inverted range entries are invalid.  */
9275           complaint (&symfile_complaints,
9276                      _("Invalid .debug_ranges data (inverted range)"));
9277           return 0;
9278         }
9279
9280       /* Empty range entries have no effect.  */
9281       if (range_beginning == range_end)
9282         continue;
9283
9284       range_beginning += base;
9285       range_end += base;
9286
9287       /* A not-uncommon case of bad debug info.
9288          Don't pollute the addrmap with bad data.  */
9289       if (range_beginning + baseaddr == 0
9290           && !dwarf2_per_objfile->has_section_at_zero)
9291         {
9292           complaint (&symfile_complaints,
9293                      _(".debug_ranges entry has start address of zero"
9294                        " [in module %s]"), objfile->name);
9295           continue;
9296         }
9297
9298       if (ranges_pst != NULL)
9299         addrmap_set_empty (objfile->psymtabs_addrmap,
9300                            range_beginning + baseaddr,
9301                            range_end - 1 + baseaddr,
9302                            ranges_pst);
9303
9304       /* FIXME: This is recording everything as a low-high
9305          segment of consecutive addresses.  We should have a
9306          data structure for discontiguous block ranges
9307          instead.  */
9308       if (! low_set)
9309         {
9310           low = range_beginning;
9311           high = range_end;
9312           low_set = 1;
9313         }
9314       else
9315         {
9316           if (range_beginning < low)
9317             low = range_beginning;
9318           if (range_end > high)
9319             high = range_end;
9320         }
9321     }
9322
9323   if (! low_set)
9324     /* If the first entry is an end-of-list marker, the range
9325        describes an empty scope, i.e. no instructions.  */
9326     return 0;
9327
9328   if (low_return)
9329     *low_return = low;
9330   if (high_return)
9331     *high_return = high;
9332   return 1;
9333 }
9334
9335 /* Get low and high pc attributes from a die.  Return 1 if the attributes
9336    are present and valid, otherwise, return 0.  Return -1 if the range is
9337    discontinuous, i.e. derived from DW_AT_ranges information.  */
9338
9339 static int
9340 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
9341                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
9342                       struct partial_symtab *pst)
9343 {
9344   struct attribute *attr;
9345   struct attribute *attr_high;
9346   CORE_ADDR low = 0;
9347   CORE_ADDR high = 0;
9348   int ret = 0;
9349
9350   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9351   if (attr_high)
9352     {
9353       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9354       if (attr)
9355         {
9356           low = DW_ADDR (attr);
9357           if (attr_high->form == DW_FORM_addr
9358               || attr_high->form == DW_FORM_GNU_addr_index)
9359             high = DW_ADDR (attr_high);
9360           else
9361             high = low + DW_UNSND (attr_high);
9362         }
9363       else
9364         /* Found high w/o low attribute.  */
9365         return 0;
9366
9367       /* Found consecutive range of addresses.  */
9368       ret = 1;
9369     }
9370   else
9371     {
9372       attr = dwarf2_attr (die, DW_AT_ranges, cu);
9373       if (attr != NULL)
9374         {
9375           unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
9376
9377           /* Value of the DW_AT_ranges attribute is the offset in the
9378              .debug_ranges section.  */
9379           if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
9380             return 0;
9381           /* Found discontinuous range of addresses.  */
9382           ret = -1;
9383         }
9384     }
9385
9386   /* read_partial_die has also the strict LOW < HIGH requirement.  */
9387   if (high <= low)
9388     return 0;
9389
9390   /* When using the GNU linker, .gnu.linkonce. sections are used to
9391      eliminate duplicate copies of functions and vtables and such.
9392      The linker will arbitrarily choose one and discard the others.
9393      The AT_*_pc values for such functions refer to local labels in
9394      these sections.  If the section from that file was discarded, the
9395      labels are not in the output, so the relocs get a value of 0.
9396      If this is a discarded function, mark the pc bounds as invalid,
9397      so that GDB will ignore it.  */
9398   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
9399     return 0;
9400
9401   *lowpc = low;
9402   if (highpc)
9403     *highpc = high;
9404   return ret;
9405 }
9406
9407 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
9408    its low and high PC addresses.  Do nothing if these addresses could not
9409    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
9410    and HIGHPC to the high address if greater than HIGHPC.  */
9411
9412 static void
9413 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
9414                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
9415                                  struct dwarf2_cu *cu)
9416 {
9417   CORE_ADDR low, high;
9418   struct die_info *child = die->child;
9419
9420   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
9421     {
9422       *lowpc = min (*lowpc, low);
9423       *highpc = max (*highpc, high);
9424     }
9425
9426   /* If the language does not allow nested subprograms (either inside
9427      subprograms or lexical blocks), we're done.  */
9428   if (cu->language != language_ada)
9429     return;
9430
9431   /* Check all the children of the given DIE.  If it contains nested
9432      subprograms, then check their pc bounds.  Likewise, we need to
9433      check lexical blocks as well, as they may also contain subprogram
9434      definitions.  */
9435   while (child && child->tag)
9436     {
9437       if (child->tag == DW_TAG_subprogram
9438           || child->tag == DW_TAG_lexical_block)
9439         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
9440       child = sibling_die (child);
9441     }
9442 }
9443
9444 /* Get the low and high pc's represented by the scope DIE, and store
9445    them in *LOWPC and *HIGHPC.  If the correct values can't be
9446    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
9447
9448 static void
9449 get_scope_pc_bounds (struct die_info *die,
9450                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
9451                      struct dwarf2_cu *cu)
9452 {
9453   CORE_ADDR best_low = (CORE_ADDR) -1;
9454   CORE_ADDR best_high = (CORE_ADDR) 0;
9455   CORE_ADDR current_low, current_high;
9456
9457   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
9458     {
9459       best_low = current_low;
9460       best_high = current_high;
9461     }
9462   else
9463     {
9464       struct die_info *child = die->child;
9465
9466       while (child && child->tag)
9467         {
9468           switch (child->tag) {
9469           case DW_TAG_subprogram:
9470             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
9471             break;
9472           case DW_TAG_namespace:
9473           case DW_TAG_module:
9474             /* FIXME: carlton/2004-01-16: Should we do this for
9475                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
9476                that current GCC's always emit the DIEs corresponding
9477                to definitions of methods of classes as children of a
9478                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
9479                the DIEs giving the declarations, which could be
9480                anywhere).  But I don't see any reason why the
9481                standards says that they have to be there.  */
9482             get_scope_pc_bounds (child, &current_low, &current_high, cu);
9483
9484             if (current_low != ((CORE_ADDR) -1))
9485               {
9486                 best_low = min (best_low, current_low);
9487                 best_high = max (best_high, current_high);
9488               }
9489             break;
9490           default:
9491             /* Ignore.  */
9492             break;
9493           }
9494
9495           child = sibling_die (child);
9496         }
9497     }
9498
9499   *lowpc = best_low;
9500   *highpc = best_high;
9501 }
9502
9503 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
9504    in DIE.  */
9505
9506 static void
9507 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
9508                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
9509 {
9510   struct objfile *objfile = cu->objfile;
9511   struct attribute *attr;
9512   struct attribute *attr_high;
9513
9514   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
9515   if (attr_high)
9516     {
9517       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9518       if (attr)
9519         {
9520           CORE_ADDR low = DW_ADDR (attr);
9521           CORE_ADDR high;
9522           if (attr_high->form == DW_FORM_addr
9523               || attr_high->form == DW_FORM_GNU_addr_index)
9524             high = DW_ADDR (attr_high);
9525           else
9526             high = low + DW_UNSND (attr_high);
9527
9528           record_block_range (block, baseaddr + low, baseaddr + high - 1);
9529         }
9530     }
9531
9532   attr = dwarf2_attr (die, DW_AT_ranges, cu);
9533   if (attr)
9534     {
9535       bfd *obfd = objfile->obfd;
9536
9537       /* The value of the DW_AT_ranges attribute is the offset of the
9538          address range list in the .debug_ranges section.  */
9539       unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
9540       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
9541
9542       /* For some target architectures, but not others, the
9543          read_address function sign-extends the addresses it returns.
9544          To recognize base address selection entries, we need a
9545          mask.  */
9546       unsigned int addr_size = cu->header.addr_size;
9547       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
9548
9549       /* The base address, to which the next pair is relative.  Note
9550          that this 'base' is a DWARF concept: most entries in a range
9551          list are relative, to reduce the number of relocs against the
9552          debugging information.  This is separate from this function's
9553          'baseaddr' argument, which GDB uses to relocate debugging
9554          information from a shared library based on the address at
9555          which the library was loaded.  */
9556       CORE_ADDR base = cu->base_address;
9557       int base_known = cu->base_known;
9558
9559       gdb_assert (dwarf2_per_objfile->ranges.readin);
9560       if (offset >= dwarf2_per_objfile->ranges.size)
9561         {
9562           complaint (&symfile_complaints,
9563                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
9564                      offset);
9565           return;
9566         }
9567
9568       for (;;)
9569         {
9570           unsigned int bytes_read;
9571           CORE_ADDR start, end;
9572
9573           start = read_address (obfd, buffer, cu, &bytes_read);
9574           buffer += bytes_read;
9575           end = read_address (obfd, buffer, cu, &bytes_read);
9576           buffer += bytes_read;
9577
9578           /* Did we find the end of the range list?  */
9579           if (start == 0 && end == 0)
9580             break;
9581
9582           /* Did we find a base address selection entry?  */
9583           else if ((start & base_select_mask) == base_select_mask)
9584             {
9585               base = end;
9586               base_known = 1;
9587             }
9588
9589           /* We found an ordinary address range.  */
9590           else
9591             {
9592               if (!base_known)
9593                 {
9594                   complaint (&symfile_complaints,
9595                              _("Invalid .debug_ranges data "
9596                                "(no base address)"));
9597                   return;
9598                 }
9599
9600               if (start > end)
9601                 {
9602                   /* Inverted range entries are invalid.  */
9603                   complaint (&symfile_complaints,
9604                              _("Invalid .debug_ranges data "
9605                                "(inverted range)"));
9606                   return;
9607                 }
9608
9609               /* Empty range entries have no effect.  */
9610               if (start == end)
9611                 continue;
9612
9613               start += base + baseaddr;
9614               end += base + baseaddr;
9615
9616               /* A not-uncommon case of bad debug info.
9617                  Don't pollute the addrmap with bad data.  */
9618               if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
9619                 {
9620                   complaint (&symfile_complaints,
9621                              _(".debug_ranges entry has start address of zero"
9622                                " [in module %s]"), objfile->name);
9623                   continue;
9624                 }
9625
9626               record_block_range (block, start, end - 1);
9627             }
9628         }
9629     }
9630 }
9631
9632 /* Check whether the producer field indicates either of GCC < 4.6, or the
9633    Intel C/C++ compiler, and cache the result in CU.  */
9634
9635 static void
9636 check_producer (struct dwarf2_cu *cu)
9637 {
9638   const char *cs;
9639   int major, minor, release;
9640
9641   if (cu->producer == NULL)
9642     {
9643       /* For unknown compilers expect their behavior is DWARF version
9644          compliant.
9645
9646          GCC started to support .debug_types sections by -gdwarf-4 since
9647          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
9648          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
9649          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
9650          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
9651     }
9652   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
9653     {
9654       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
9655
9656       cs = &cu->producer[strlen ("GNU ")];
9657       while (*cs && !isdigit (*cs))
9658         cs++;
9659       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
9660         {
9661           /* Not recognized as GCC.  */
9662         }
9663       else
9664         cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
9665     }
9666   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
9667     cu->producer_is_icc = 1;
9668   else
9669     {
9670       /* For other non-GCC compilers, expect their behavior is DWARF version
9671          compliant.  */
9672     }
9673
9674   cu->checked_producer = 1;
9675 }
9676
9677 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
9678    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
9679    during 4.6.0 experimental.  */
9680
9681 static int
9682 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
9683 {
9684   if (!cu->checked_producer)
9685     check_producer (cu);
9686
9687   return cu->producer_is_gxx_lt_4_6;
9688 }
9689
9690 /* Return the default accessibility type if it is not overriden by
9691    DW_AT_accessibility.  */
9692
9693 static enum dwarf_access_attribute
9694 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
9695 {
9696   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
9697     {
9698       /* The default DWARF 2 accessibility for members is public, the default
9699          accessibility for inheritance is private.  */
9700
9701       if (die->tag != DW_TAG_inheritance)
9702         return DW_ACCESS_public;
9703       else
9704         return DW_ACCESS_private;
9705     }
9706   else
9707     {
9708       /* DWARF 3+ defines the default accessibility a different way.  The same
9709          rules apply now for DW_TAG_inheritance as for the members and it only
9710          depends on the container kind.  */
9711
9712       if (die->parent->tag == DW_TAG_class_type)
9713         return DW_ACCESS_private;
9714       else
9715         return DW_ACCESS_public;
9716     }
9717 }
9718
9719 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
9720    offset.  If the attribute was not found return 0, otherwise return
9721    1.  If it was found but could not properly be handled, set *OFFSET
9722    to 0.  */
9723
9724 static int
9725 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
9726                              LONGEST *offset)
9727 {
9728   struct attribute *attr;
9729
9730   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
9731   if (attr != NULL)
9732     {
9733       *offset = 0;
9734
9735       /* Note that we do not check for a section offset first here.
9736          This is because DW_AT_data_member_location is new in DWARF 4,
9737          so if we see it, we can assume that a constant form is really
9738          a constant and not a section offset.  */
9739       if (attr_form_is_constant (attr))
9740         *offset = dwarf2_get_attr_constant_value (attr, 0);
9741       else if (attr_form_is_section_offset (attr))
9742         dwarf2_complex_location_expr_complaint ();
9743       else if (attr_form_is_block (attr))
9744         *offset = decode_locdesc (DW_BLOCK (attr), cu);
9745       else
9746         dwarf2_complex_location_expr_complaint ();
9747
9748       return 1;
9749     }
9750
9751   return 0;
9752 }
9753
9754 /* Add an aggregate field to the field list.  */
9755
9756 static void
9757 dwarf2_add_field (struct field_info *fip, struct die_info *die,
9758                   struct dwarf2_cu *cu)
9759 {
9760   struct objfile *objfile = cu->objfile;
9761   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9762   struct nextfield *new_field;
9763   struct attribute *attr;
9764   struct field *fp;
9765   char *fieldname = "";
9766
9767   /* Allocate a new field list entry and link it in.  */
9768   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
9769   make_cleanup (xfree, new_field);
9770   memset (new_field, 0, sizeof (struct nextfield));
9771
9772   if (die->tag == DW_TAG_inheritance)
9773     {
9774       new_field->next = fip->baseclasses;
9775       fip->baseclasses = new_field;
9776     }
9777   else
9778     {
9779       new_field->next = fip->fields;
9780       fip->fields = new_field;
9781     }
9782   fip->nfields++;
9783
9784   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
9785   if (attr)
9786     new_field->accessibility = DW_UNSND (attr);
9787   else
9788     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
9789   if (new_field->accessibility != DW_ACCESS_public)
9790     fip->non_public_fields = 1;
9791
9792   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9793   if (attr)
9794     new_field->virtuality = DW_UNSND (attr);
9795   else
9796     new_field->virtuality = DW_VIRTUALITY_none;
9797
9798   fp = &new_field->field;
9799
9800   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
9801     {
9802       LONGEST offset;
9803
9804       /* Data member other than a C++ static data member.  */
9805
9806       /* Get type of field.  */
9807       fp->type = die_type (die, cu);
9808
9809       SET_FIELD_BITPOS (*fp, 0);
9810
9811       /* Get bit size of field (zero if none).  */
9812       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9813       if (attr)
9814         {
9815           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
9816         }
9817       else
9818         {
9819           FIELD_BITSIZE (*fp) = 0;
9820         }
9821
9822       /* Get bit offset of field.  */
9823       if (handle_data_member_location (die, cu, &offset))
9824         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9825       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
9826       if (attr)
9827         {
9828           if (gdbarch_bits_big_endian (gdbarch))
9829             {
9830               /* For big endian bits, the DW_AT_bit_offset gives the
9831                  additional bit offset from the MSB of the containing
9832                  anonymous object to the MSB of the field.  We don't
9833                  have to do anything special since we don't need to
9834                  know the size of the anonymous object.  */
9835               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
9836             }
9837           else
9838             {
9839               /* For little endian bits, compute the bit offset to the
9840                  MSB of the anonymous object, subtract off the number of
9841                  bits from the MSB of the field to the MSB of the
9842                  object, and then subtract off the number of bits of
9843                  the field itself.  The result is the bit offset of
9844                  the LSB of the field.  */
9845               int anonymous_size;
9846               int bit_offset = DW_UNSND (attr);
9847
9848               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9849               if (attr)
9850                 {
9851                   /* The size of the anonymous object containing
9852                      the bit field is explicit, so use the
9853                      indicated size (in bytes).  */
9854                   anonymous_size = DW_UNSND (attr);
9855                 }
9856               else
9857                 {
9858                   /* The size of the anonymous object containing
9859                      the bit field must be inferred from the type
9860                      attribute of the data member containing the
9861                      bit field.  */
9862                   anonymous_size = TYPE_LENGTH (fp->type);
9863                 }
9864               SET_FIELD_BITPOS (*fp,
9865                                 (FIELD_BITPOS (*fp)
9866                                  + anonymous_size * bits_per_byte
9867                                  - bit_offset - FIELD_BITSIZE (*fp)));
9868             }
9869         }
9870
9871       /* Get name of field.  */
9872       fieldname = dwarf2_name (die, cu);
9873       if (fieldname == NULL)
9874         fieldname = "";
9875
9876       /* The name is already allocated along with this objfile, so we don't
9877          need to duplicate it for the type.  */
9878       fp->name = fieldname;
9879
9880       /* Change accessibility for artificial fields (e.g. virtual table
9881          pointer or virtual base class pointer) to private.  */
9882       if (dwarf2_attr (die, DW_AT_artificial, cu))
9883         {
9884           FIELD_ARTIFICIAL (*fp) = 1;
9885           new_field->accessibility = DW_ACCESS_private;
9886           fip->non_public_fields = 1;
9887         }
9888     }
9889   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
9890     {
9891       /* C++ static member.  */
9892
9893       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
9894          is a declaration, but all versions of G++ as of this writing
9895          (so through at least 3.2.1) incorrectly generate
9896          DW_TAG_variable tags.  */
9897
9898       const char *physname;
9899
9900       /* Get name of field.  */
9901       fieldname = dwarf2_name (die, cu);
9902       if (fieldname == NULL)
9903         return;
9904
9905       attr = dwarf2_attr (die, DW_AT_const_value, cu);
9906       if (attr
9907           /* Only create a symbol if this is an external value.
9908              new_symbol checks this and puts the value in the global symbol
9909              table, which we want.  If it is not external, new_symbol
9910              will try to put the value in cu->list_in_scope which is wrong.  */
9911           && dwarf2_flag_true_p (die, DW_AT_external, cu))
9912         {
9913           /* A static const member, not much different than an enum as far as
9914              we're concerned, except that we can support more types.  */
9915           new_symbol (die, NULL, cu);
9916         }
9917
9918       /* Get physical name.  */
9919       physname = dwarf2_physname (fieldname, die, cu);
9920
9921       /* The name is already allocated along with this objfile, so we don't
9922          need to duplicate it for the type.  */
9923       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
9924       FIELD_TYPE (*fp) = die_type (die, cu);
9925       FIELD_NAME (*fp) = fieldname;
9926     }
9927   else if (die->tag == DW_TAG_inheritance)
9928     {
9929       LONGEST offset;
9930
9931       /* C++ base class field.  */
9932       if (handle_data_member_location (die, cu, &offset))
9933         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
9934       FIELD_BITSIZE (*fp) = 0;
9935       FIELD_TYPE (*fp) = die_type (die, cu);
9936       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
9937       fip->nbaseclasses++;
9938     }
9939 }
9940
9941 /* Add a typedef defined in the scope of the FIP's class.  */
9942
9943 static void
9944 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
9945                     struct dwarf2_cu *cu)
9946 {
9947   struct objfile *objfile = cu->objfile;
9948   struct typedef_field_list *new_field;
9949   struct attribute *attr;
9950   struct typedef_field *fp;
9951   char *fieldname = "";
9952
9953   /* Allocate a new field list entry and link it in.  */
9954   new_field = xzalloc (sizeof (*new_field));
9955   make_cleanup (xfree, new_field);
9956
9957   gdb_assert (die->tag == DW_TAG_typedef);
9958
9959   fp = &new_field->field;
9960
9961   /* Get name of field.  */
9962   fp->name = dwarf2_name (die, cu);
9963   if (fp->name == NULL)
9964     return;
9965
9966   fp->type = read_type_die (die, cu);
9967
9968   new_field->next = fip->typedef_field_list;
9969   fip->typedef_field_list = new_field;
9970   fip->typedef_field_list_count++;
9971 }
9972
9973 /* Create the vector of fields, and attach it to the type.  */
9974
9975 static void
9976 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
9977                               struct dwarf2_cu *cu)
9978 {
9979   int nfields = fip->nfields;
9980
9981   /* Record the field count, allocate space for the array of fields,
9982      and create blank accessibility bitfields if necessary.  */
9983   TYPE_NFIELDS (type) = nfields;
9984   TYPE_FIELDS (type) = (struct field *)
9985     TYPE_ALLOC (type, sizeof (struct field) * nfields);
9986   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
9987
9988   if (fip->non_public_fields && cu->language != language_ada)
9989     {
9990       ALLOCATE_CPLUS_STRUCT_TYPE (type);
9991
9992       TYPE_FIELD_PRIVATE_BITS (type) =
9993         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9994       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
9995
9996       TYPE_FIELD_PROTECTED_BITS (type) =
9997         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
9998       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
9999
10000       TYPE_FIELD_IGNORE_BITS (type) =
10001         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
10002       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
10003     }
10004
10005   /* If the type has baseclasses, allocate and clear a bit vector for
10006      TYPE_FIELD_VIRTUAL_BITS.  */
10007   if (fip->nbaseclasses && cu->language != language_ada)
10008     {
10009       int num_bytes = B_BYTES (fip->nbaseclasses);
10010       unsigned char *pointer;
10011
10012       ALLOCATE_CPLUS_STRUCT_TYPE (type);
10013       pointer = TYPE_ALLOC (type, num_bytes);
10014       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
10015       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
10016       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
10017     }
10018
10019   /* Copy the saved-up fields into the field vector.  Start from the head of
10020      the list, adding to the tail of the field array, so that they end up in
10021      the same order in the array in which they were added to the list.  */
10022   while (nfields-- > 0)
10023     {
10024       struct nextfield *fieldp;
10025
10026       if (fip->fields)
10027         {
10028           fieldp = fip->fields;
10029           fip->fields = fieldp->next;
10030         }
10031       else
10032         {
10033           fieldp = fip->baseclasses;
10034           fip->baseclasses = fieldp->next;
10035         }
10036
10037       TYPE_FIELD (type, nfields) = fieldp->field;
10038       switch (fieldp->accessibility)
10039         {
10040         case DW_ACCESS_private:
10041           if (cu->language != language_ada)
10042             SET_TYPE_FIELD_PRIVATE (type, nfields);
10043           break;
10044
10045         case DW_ACCESS_protected:
10046           if (cu->language != language_ada)
10047             SET_TYPE_FIELD_PROTECTED (type, nfields);
10048           break;
10049
10050         case DW_ACCESS_public:
10051           break;
10052
10053         default:
10054           /* Unknown accessibility.  Complain and treat it as public.  */
10055           {
10056             complaint (&symfile_complaints, _("unsupported accessibility %d"),
10057                        fieldp->accessibility);
10058           }
10059           break;
10060         }
10061       if (nfields < fip->nbaseclasses)
10062         {
10063           switch (fieldp->virtuality)
10064             {
10065             case DW_VIRTUALITY_virtual:
10066             case DW_VIRTUALITY_pure_virtual:
10067               if (cu->language == language_ada)
10068                 error (_("unexpected virtuality in component of Ada type"));
10069               SET_TYPE_FIELD_VIRTUAL (type, nfields);
10070               break;
10071             }
10072         }
10073     }
10074 }
10075
10076 /* Add a member function to the proper fieldlist.  */
10077
10078 static void
10079 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
10080                       struct type *type, struct dwarf2_cu *cu)
10081 {
10082   struct objfile *objfile = cu->objfile;
10083   struct attribute *attr;
10084   struct fnfieldlist *flp;
10085   int i;
10086   struct fn_field *fnp;
10087   char *fieldname;
10088   struct nextfnfield *new_fnfield;
10089   struct type *this_type;
10090   enum dwarf_access_attribute accessibility;
10091
10092   if (cu->language == language_ada)
10093     error (_("unexpected member function in Ada type"));
10094
10095   /* Get name of member function.  */
10096   fieldname = dwarf2_name (die, cu);
10097   if (fieldname == NULL)
10098     return;
10099
10100   /* Look up member function name in fieldlist.  */
10101   for (i = 0; i < fip->nfnfields; i++)
10102     {
10103       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
10104         break;
10105     }
10106
10107   /* Create new list element if necessary.  */
10108   if (i < fip->nfnfields)
10109     flp = &fip->fnfieldlists[i];
10110   else
10111     {
10112       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
10113         {
10114           fip->fnfieldlists = (struct fnfieldlist *)
10115             xrealloc (fip->fnfieldlists,
10116                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
10117                       * sizeof (struct fnfieldlist));
10118           if (fip->nfnfields == 0)
10119             make_cleanup (free_current_contents, &fip->fnfieldlists);
10120         }
10121       flp = &fip->fnfieldlists[fip->nfnfields];
10122       flp->name = fieldname;
10123       flp->length = 0;
10124       flp->head = NULL;
10125       i = fip->nfnfields++;
10126     }
10127
10128   /* Create a new member function field and chain it to the field list
10129      entry.  */
10130   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
10131   make_cleanup (xfree, new_fnfield);
10132   memset (new_fnfield, 0, sizeof (struct nextfnfield));
10133   new_fnfield->next = flp->head;
10134   flp->head = new_fnfield;
10135   flp->length++;
10136
10137   /* Fill in the member function field info.  */
10138   fnp = &new_fnfield->fnfield;
10139
10140   /* Delay processing of the physname until later.  */
10141   if (cu->language == language_cplus || cu->language == language_java)
10142     {
10143       add_to_method_list (type, i, flp->length - 1, fieldname,
10144                           die, cu);
10145     }
10146   else
10147     {
10148       const char *physname = dwarf2_physname (fieldname, die, cu);
10149       fnp->physname = physname ? physname : "";
10150     }
10151
10152   fnp->type = alloc_type (objfile);
10153   this_type = read_type_die (die, cu);
10154   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
10155     {
10156       int nparams = TYPE_NFIELDS (this_type);
10157
10158       /* TYPE is the domain of this method, and THIS_TYPE is the type
10159            of the method itself (TYPE_CODE_METHOD).  */
10160       smash_to_method_type (fnp->type, type,
10161                             TYPE_TARGET_TYPE (this_type),
10162                             TYPE_FIELDS (this_type),
10163                             TYPE_NFIELDS (this_type),
10164                             TYPE_VARARGS (this_type));
10165
10166       /* Handle static member functions.
10167          Dwarf2 has no clean way to discern C++ static and non-static
10168          member functions.  G++ helps GDB by marking the first
10169          parameter for non-static member functions (which is the this
10170          pointer) as artificial.  We obtain this information from
10171          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
10172       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
10173         fnp->voffset = VOFFSET_STATIC;
10174     }
10175   else
10176     complaint (&symfile_complaints, _("member function type missing for '%s'"),
10177                dwarf2_full_name (fieldname, die, cu));
10178
10179   /* Get fcontext from DW_AT_containing_type if present.  */
10180   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10181     fnp->fcontext = die_containing_type (die, cu);
10182
10183   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
10184      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
10185
10186   /* Get accessibility.  */
10187   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
10188   if (attr)
10189     accessibility = DW_UNSND (attr);
10190   else
10191     accessibility = dwarf2_default_access_attribute (die, cu);
10192   switch (accessibility)
10193     {
10194     case DW_ACCESS_private:
10195       fnp->is_private = 1;
10196       break;
10197     case DW_ACCESS_protected:
10198       fnp->is_protected = 1;
10199       break;
10200     }
10201
10202   /* Check for artificial methods.  */
10203   attr = dwarf2_attr (die, DW_AT_artificial, cu);
10204   if (attr && DW_UNSND (attr) != 0)
10205     fnp->is_artificial = 1;
10206
10207   /* Get index in virtual function table if it is a virtual member
10208      function.  For older versions of GCC, this is an offset in the
10209      appropriate virtual table, as specified by DW_AT_containing_type.
10210      For everyone else, it is an expression to be evaluated relative
10211      to the object address.  */
10212
10213   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
10214   if (attr)
10215     {
10216       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
10217         {
10218           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
10219             {
10220               /* Old-style GCC.  */
10221               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
10222             }
10223           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
10224                    || (DW_BLOCK (attr)->size > 1
10225                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
10226                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
10227             {
10228               struct dwarf_block blk;
10229               int offset;
10230
10231               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
10232                         ? 1 : 2);
10233               blk.size = DW_BLOCK (attr)->size - offset;
10234               blk.data = DW_BLOCK (attr)->data + offset;
10235               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
10236               if ((fnp->voffset % cu->header.addr_size) != 0)
10237                 dwarf2_complex_location_expr_complaint ();
10238               else
10239                 fnp->voffset /= cu->header.addr_size;
10240               fnp->voffset += 2;
10241             }
10242           else
10243             dwarf2_complex_location_expr_complaint ();
10244
10245           if (!fnp->fcontext)
10246             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
10247         }
10248       else if (attr_form_is_section_offset (attr))
10249         {
10250           dwarf2_complex_location_expr_complaint ();
10251         }
10252       else
10253         {
10254           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
10255                                                  fieldname);
10256         }
10257     }
10258   else
10259     {
10260       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
10261       if (attr && DW_UNSND (attr))
10262         {
10263           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
10264           complaint (&symfile_complaints,
10265                      _("Member function \"%s\" (offset %d) is virtual "
10266                        "but the vtable offset is not specified"),
10267                      fieldname, die->offset.sect_off);
10268           ALLOCATE_CPLUS_STRUCT_TYPE (type);
10269           TYPE_CPLUS_DYNAMIC (type) = 1;
10270         }
10271     }
10272 }
10273
10274 /* Create the vector of member function fields, and attach it to the type.  */
10275
10276 static void
10277 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
10278                                  struct dwarf2_cu *cu)
10279 {
10280   struct fnfieldlist *flp;
10281   int i;
10282
10283   if (cu->language == language_ada)
10284     error (_("unexpected member functions in Ada type"));
10285
10286   ALLOCATE_CPLUS_STRUCT_TYPE (type);
10287   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
10288     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
10289
10290   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
10291     {
10292       struct nextfnfield *nfp = flp->head;
10293       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
10294       int k;
10295
10296       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
10297       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
10298       fn_flp->fn_fields = (struct fn_field *)
10299         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
10300       for (k = flp->length; (k--, nfp); nfp = nfp->next)
10301         fn_flp->fn_fields[k] = nfp->fnfield;
10302     }
10303
10304   TYPE_NFN_FIELDS (type) = fip->nfnfields;
10305 }
10306
10307 /* Returns non-zero if NAME is the name of a vtable member in CU's
10308    language, zero otherwise.  */
10309 static int
10310 is_vtable_name (const char *name, struct dwarf2_cu *cu)
10311 {
10312   static const char vptr[] = "_vptr";
10313   static const char vtable[] = "vtable";
10314
10315   /* Look for the C++ and Java forms of the vtable.  */
10316   if ((cu->language == language_java
10317        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
10318        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
10319        && is_cplus_marker (name[sizeof (vptr) - 1])))
10320     return 1;
10321
10322   return 0;
10323 }
10324
10325 /* GCC outputs unnamed structures that are really pointers to member
10326    functions, with the ABI-specified layout.  If TYPE describes
10327    such a structure, smash it into a member function type.
10328
10329    GCC shouldn't do this; it should just output pointer to member DIEs.
10330    This is GCC PR debug/28767.  */
10331
10332 static void
10333 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
10334 {
10335   struct type *pfn_type, *domain_type, *new_type;
10336
10337   /* Check for a structure with no name and two children.  */
10338   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
10339     return;
10340
10341   /* Check for __pfn and __delta members.  */
10342   if (TYPE_FIELD_NAME (type, 0) == NULL
10343       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
10344       || TYPE_FIELD_NAME (type, 1) == NULL
10345       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
10346     return;
10347
10348   /* Find the type of the method.  */
10349   pfn_type = TYPE_FIELD_TYPE (type, 0);
10350   if (pfn_type == NULL
10351       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
10352       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
10353     return;
10354
10355   /* Look for the "this" argument.  */
10356   pfn_type = TYPE_TARGET_TYPE (pfn_type);
10357   if (TYPE_NFIELDS (pfn_type) == 0
10358       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
10359       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
10360     return;
10361
10362   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
10363   new_type = alloc_type (objfile);
10364   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
10365                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
10366                         TYPE_VARARGS (pfn_type));
10367   smash_to_methodptr_type (type, new_type);
10368 }
10369
10370 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
10371    (icc).  */
10372
10373 static int
10374 producer_is_icc (struct dwarf2_cu *cu)
10375 {
10376   if (!cu->checked_producer)
10377     check_producer (cu);
10378
10379   return cu->producer_is_icc;
10380 }
10381
10382 /* Called when we find the DIE that starts a structure or union scope
10383    (definition) to create a type for the structure or union.  Fill in
10384    the type's name and general properties; the members will not be
10385    processed until process_structure_type.
10386
10387    NOTE: we need to call these functions regardless of whether or not the
10388    DIE has a DW_AT_name attribute, since it might be an anonymous
10389    structure or union.  This gets the type entered into our set of
10390    user defined types.
10391
10392    However, if the structure is incomplete (an opaque struct/union)
10393    then suppress creating a symbol table entry for it since gdb only
10394    wants to find the one with the complete definition.  Note that if
10395    it is complete, we just call new_symbol, which does it's own
10396    checking about whether the struct/union is anonymous or not (and
10397    suppresses creating a symbol table entry itself).  */
10398
10399 static struct type *
10400 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
10401 {
10402   struct objfile *objfile = cu->objfile;
10403   struct type *type;
10404   struct attribute *attr;
10405   char *name;
10406
10407   /* If the definition of this type lives in .debug_types, read that type.
10408      Don't follow DW_AT_specification though, that will take us back up
10409      the chain and we want to go down.  */
10410   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10411   if (attr)
10412     {
10413       struct dwarf2_cu *type_cu = cu;
10414       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10415
10416       /* We could just recurse on read_structure_type, but we need to call
10417          get_die_type to ensure only one type for this DIE is created.
10418          This is important, for example, because for c++ classes we need
10419          TYPE_NAME set which is only done by new_symbol.  Blech.  */
10420       type = read_type_die (type_die, type_cu);
10421
10422       /* TYPE_CU may not be the same as CU.
10423          Ensure TYPE is recorded in CU's type_hash table.  */
10424       return set_die_type (die, type, cu);
10425     }
10426
10427   type = alloc_type (objfile);
10428   INIT_CPLUS_SPECIFIC (type);
10429
10430   name = dwarf2_name (die, cu);
10431   if (name != NULL)
10432     {
10433       if (cu->language == language_cplus
10434           || cu->language == language_java)
10435         {
10436           char *full_name = (char *) dwarf2_full_name (name, die, cu);
10437
10438           /* dwarf2_full_name might have already finished building the DIE's
10439              type.  If so, there is no need to continue.  */
10440           if (get_die_type (die, cu) != NULL)
10441             return get_die_type (die, cu);
10442
10443           TYPE_TAG_NAME (type) = full_name;
10444           if (die->tag == DW_TAG_structure_type
10445               || die->tag == DW_TAG_class_type)
10446             TYPE_NAME (type) = TYPE_TAG_NAME (type);
10447         }
10448       else
10449         {
10450           /* The name is already allocated along with this objfile, so
10451              we don't need to duplicate it for the type.  */
10452           TYPE_TAG_NAME (type) = (char *) name;
10453           if (die->tag == DW_TAG_class_type)
10454             TYPE_NAME (type) = TYPE_TAG_NAME (type);
10455         }
10456     }
10457
10458   if (die->tag == DW_TAG_structure_type)
10459     {
10460       TYPE_CODE (type) = TYPE_CODE_STRUCT;
10461     }
10462   else if (die->tag == DW_TAG_union_type)
10463     {
10464       TYPE_CODE (type) = TYPE_CODE_UNION;
10465     }
10466   else
10467     {
10468       TYPE_CODE (type) = TYPE_CODE_CLASS;
10469     }
10470
10471   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
10472     TYPE_DECLARED_CLASS (type) = 1;
10473
10474   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10475   if (attr)
10476     {
10477       TYPE_LENGTH (type) = DW_UNSND (attr);
10478     }
10479   else
10480     {
10481       TYPE_LENGTH (type) = 0;
10482     }
10483
10484   if (producer_is_icc (cu))
10485     {
10486       /* ICC does not output the required DW_AT_declaration
10487          on incomplete types, but gives them a size of zero.  */
10488     }
10489   else
10490     TYPE_STUB_SUPPORTED (type) = 1;
10491
10492   if (die_is_declaration (die, cu))
10493     TYPE_STUB (type) = 1;
10494   else if (attr == NULL && die->child == NULL
10495            && producer_is_realview (cu->producer))
10496     /* RealView does not output the required DW_AT_declaration
10497        on incomplete types.  */
10498     TYPE_STUB (type) = 1;
10499
10500   /* We need to add the type field to the die immediately so we don't
10501      infinitely recurse when dealing with pointers to the structure
10502      type within the structure itself.  */
10503   set_die_type (die, type, cu);
10504
10505   /* set_die_type should be already done.  */
10506   set_descriptive_type (type, die, cu);
10507
10508   return type;
10509 }
10510
10511 /* Finish creating a structure or union type, including filling in
10512    its members and creating a symbol for it.  */
10513
10514 static void
10515 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
10516 {
10517   struct objfile *objfile = cu->objfile;
10518   struct die_info *child_die = die->child;
10519   struct type *type;
10520
10521   type = get_die_type (die, cu);
10522   if (type == NULL)
10523     type = read_structure_type (die, cu);
10524
10525   if (die->child != NULL && ! die_is_declaration (die, cu))
10526     {
10527       struct field_info fi;
10528       struct die_info *child_die;
10529       VEC (symbolp) *template_args = NULL;
10530       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
10531
10532       memset (&fi, 0, sizeof (struct field_info));
10533
10534       child_die = die->child;
10535
10536       while (child_die && child_die->tag)
10537         {
10538           if (child_die->tag == DW_TAG_member
10539               || child_die->tag == DW_TAG_variable)
10540             {
10541               /* NOTE: carlton/2002-11-05: A C++ static data member
10542                  should be a DW_TAG_member that is a declaration, but
10543                  all versions of G++ as of this writing (so through at
10544                  least 3.2.1) incorrectly generate DW_TAG_variable
10545                  tags for them instead.  */
10546               dwarf2_add_field (&fi, child_die, cu);
10547             }
10548           else if (child_die->tag == DW_TAG_subprogram)
10549             {
10550               /* C++ member function.  */
10551               dwarf2_add_member_fn (&fi, child_die, type, cu);
10552             }
10553           else if (child_die->tag == DW_TAG_inheritance)
10554             {
10555               /* C++ base class field.  */
10556               dwarf2_add_field (&fi, child_die, cu);
10557             }
10558           else if (child_die->tag == DW_TAG_typedef)
10559             dwarf2_add_typedef (&fi, child_die, cu);
10560           else if (child_die->tag == DW_TAG_template_type_param
10561                    || child_die->tag == DW_TAG_template_value_param)
10562             {
10563               struct symbol *arg = new_symbol (child_die, NULL, cu);
10564
10565               if (arg != NULL)
10566                 VEC_safe_push (symbolp, template_args, arg);
10567             }
10568
10569           child_die = sibling_die (child_die);
10570         }
10571
10572       /* Attach template arguments to type.  */
10573       if (! VEC_empty (symbolp, template_args))
10574         {
10575           ALLOCATE_CPLUS_STRUCT_TYPE (type);
10576           TYPE_N_TEMPLATE_ARGUMENTS (type)
10577             = VEC_length (symbolp, template_args);
10578           TYPE_TEMPLATE_ARGUMENTS (type)
10579             = obstack_alloc (&objfile->objfile_obstack,
10580                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
10581                               * sizeof (struct symbol *)));
10582           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
10583                   VEC_address (symbolp, template_args),
10584                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
10585                    * sizeof (struct symbol *)));
10586           VEC_free (symbolp, template_args);
10587         }
10588
10589       /* Attach fields and member functions to the type.  */
10590       if (fi.nfields)
10591         dwarf2_attach_fields_to_type (&fi, type, cu);
10592       if (fi.nfnfields)
10593         {
10594           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
10595
10596           /* Get the type which refers to the base class (possibly this
10597              class itself) which contains the vtable pointer for the current
10598              class from the DW_AT_containing_type attribute.  This use of
10599              DW_AT_containing_type is a GNU extension.  */
10600
10601           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
10602             {
10603               struct type *t = die_containing_type (die, cu);
10604
10605               TYPE_VPTR_BASETYPE (type) = t;
10606               if (type == t)
10607                 {
10608                   int i;
10609
10610                   /* Our own class provides vtbl ptr.  */
10611                   for (i = TYPE_NFIELDS (t) - 1;
10612                        i >= TYPE_N_BASECLASSES (t);
10613                        --i)
10614                     {
10615                       const char *fieldname = TYPE_FIELD_NAME (t, i);
10616
10617                       if (is_vtable_name (fieldname, cu))
10618                         {
10619                           TYPE_VPTR_FIELDNO (type) = i;
10620                           break;
10621                         }
10622                     }
10623
10624                   /* Complain if virtual function table field not found.  */
10625                   if (i < TYPE_N_BASECLASSES (t))
10626                     complaint (&symfile_complaints,
10627                                _("virtual function table pointer "
10628                                  "not found when defining class '%s'"),
10629                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
10630                                "");
10631                 }
10632               else
10633                 {
10634                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
10635                 }
10636             }
10637           else if (cu->producer
10638                    && strncmp (cu->producer,
10639                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
10640             {
10641               /* The IBM XLC compiler does not provide direct indication
10642                  of the containing type, but the vtable pointer is
10643                  always named __vfp.  */
10644
10645               int i;
10646
10647               for (i = TYPE_NFIELDS (type) - 1;
10648                    i >= TYPE_N_BASECLASSES (type);
10649                    --i)
10650                 {
10651                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
10652                     {
10653                       TYPE_VPTR_FIELDNO (type) = i;
10654                       TYPE_VPTR_BASETYPE (type) = type;
10655                       break;
10656                     }
10657                 }
10658             }
10659         }
10660
10661       /* Copy fi.typedef_field_list linked list elements content into the
10662          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
10663       if (fi.typedef_field_list)
10664         {
10665           int i = fi.typedef_field_list_count;
10666
10667           ALLOCATE_CPLUS_STRUCT_TYPE (type);
10668           TYPE_TYPEDEF_FIELD_ARRAY (type)
10669             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
10670           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
10671
10672           /* Reverse the list order to keep the debug info elements order.  */
10673           while (--i >= 0)
10674             {
10675               struct typedef_field *dest, *src;
10676
10677               dest = &TYPE_TYPEDEF_FIELD (type, i);
10678               src = &fi.typedef_field_list->field;
10679               fi.typedef_field_list = fi.typedef_field_list->next;
10680               *dest = *src;
10681             }
10682         }
10683
10684       do_cleanups (back_to);
10685
10686       if (HAVE_CPLUS_STRUCT (type))
10687         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
10688     }
10689
10690   quirk_gcc_member_function_pointer (type, objfile);
10691
10692   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
10693      snapshots) has been known to create a die giving a declaration
10694      for a class that has, as a child, a die giving a definition for a
10695      nested class.  So we have to process our children even if the
10696      current die is a declaration.  Normally, of course, a declaration
10697      won't have any children at all.  */
10698
10699   while (child_die != NULL && child_die->tag)
10700     {
10701       if (child_die->tag == DW_TAG_member
10702           || child_die->tag == DW_TAG_variable
10703           || child_die->tag == DW_TAG_inheritance
10704           || child_die->tag == DW_TAG_template_value_param
10705           || child_die->tag == DW_TAG_template_type_param)
10706         {
10707           /* Do nothing.  */
10708         }
10709       else
10710         process_die (child_die, cu);
10711
10712       child_die = sibling_die (child_die);
10713     }
10714
10715   /* Do not consider external references.  According to the DWARF standard,
10716      these DIEs are identified by the fact that they have no byte_size
10717      attribute, and a declaration attribute.  */
10718   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
10719       || !die_is_declaration (die, cu))
10720     new_symbol (die, type, cu);
10721 }
10722
10723 /* Given a DW_AT_enumeration_type die, set its type.  We do not
10724    complete the type's fields yet, or create any symbols.  */
10725
10726 static struct type *
10727 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
10728 {
10729   struct objfile *objfile = cu->objfile;
10730   struct type *type;
10731   struct attribute *attr;
10732   const char *name;
10733
10734   /* If the definition of this type lives in .debug_types, read that type.
10735      Don't follow DW_AT_specification though, that will take us back up
10736      the chain and we want to go down.  */
10737   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
10738   if (attr)
10739     {
10740       struct dwarf2_cu *type_cu = cu;
10741       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
10742
10743       type = read_type_die (type_die, type_cu);
10744
10745       /* TYPE_CU may not be the same as CU.
10746          Ensure TYPE is recorded in CU's type_hash table.  */
10747       return set_die_type (die, type, cu);
10748     }
10749
10750   type = alloc_type (objfile);
10751
10752   TYPE_CODE (type) = TYPE_CODE_ENUM;
10753   name = dwarf2_full_name (NULL, die, cu);
10754   if (name != NULL)
10755     TYPE_TAG_NAME (type) = (char *) name;
10756
10757   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10758   if (attr)
10759     {
10760       TYPE_LENGTH (type) = DW_UNSND (attr);
10761     }
10762   else
10763     {
10764       TYPE_LENGTH (type) = 0;
10765     }
10766
10767   /* The enumeration DIE can be incomplete.  In Ada, any type can be
10768      declared as private in the package spec, and then defined only
10769      inside the package body.  Such types are known as Taft Amendment
10770      Types.  When another package uses such a type, an incomplete DIE
10771      may be generated by the compiler.  */
10772   if (die_is_declaration (die, cu))
10773     TYPE_STUB (type) = 1;
10774
10775   return set_die_type (die, type, cu);
10776 }
10777
10778 /* Given a pointer to a die which begins an enumeration, process all
10779    the dies that define the members of the enumeration, and create the
10780    symbol for the enumeration type.
10781
10782    NOTE: We reverse the order of the element list.  */
10783
10784 static void
10785 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
10786 {
10787   struct type *this_type;
10788
10789   this_type = get_die_type (die, cu);
10790   if (this_type == NULL)
10791     this_type = read_enumeration_type (die, cu);
10792
10793   if (die->child != NULL)
10794     {
10795       struct die_info *child_die;
10796       struct symbol *sym;
10797       struct field *fields = NULL;
10798       int num_fields = 0;
10799       int unsigned_enum = 1;
10800       char *name;
10801       int flag_enum = 1;
10802       ULONGEST mask = 0;
10803
10804       child_die = die->child;
10805       while (child_die && child_die->tag)
10806         {
10807           if (child_die->tag != DW_TAG_enumerator)
10808             {
10809               process_die (child_die, cu);
10810             }
10811           else
10812             {
10813               name = dwarf2_name (child_die, cu);
10814               if (name)
10815                 {
10816                   sym = new_symbol (child_die, this_type, cu);
10817                   if (SYMBOL_VALUE (sym) < 0)
10818                     {
10819                       unsigned_enum = 0;
10820                       flag_enum = 0;
10821                     }
10822                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
10823                     flag_enum = 0;
10824                   else
10825                     mask |= SYMBOL_VALUE (sym);
10826
10827                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
10828                     {
10829                       fields = (struct field *)
10830                         xrealloc (fields,
10831                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
10832                                   * sizeof (struct field));
10833                     }
10834
10835                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
10836                   FIELD_TYPE (fields[num_fields]) = NULL;
10837                   SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
10838                   FIELD_BITSIZE (fields[num_fields]) = 0;
10839
10840                   num_fields++;
10841                 }
10842             }
10843
10844           child_die = sibling_die (child_die);
10845         }
10846
10847       if (num_fields)
10848         {
10849           TYPE_NFIELDS (this_type) = num_fields;
10850           TYPE_FIELDS (this_type) = (struct field *)
10851             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
10852           memcpy (TYPE_FIELDS (this_type), fields,
10853                   sizeof (struct field) * num_fields);
10854           xfree (fields);
10855         }
10856       if (unsigned_enum)
10857         TYPE_UNSIGNED (this_type) = 1;
10858       if (flag_enum)
10859         TYPE_FLAG_ENUM (this_type) = 1;
10860     }
10861
10862   /* If we are reading an enum from a .debug_types unit, and the enum
10863      is a declaration, and the enum is not the signatured type in the
10864      unit, then we do not want to add a symbol for it.  Adding a
10865      symbol would in some cases obscure the true definition of the
10866      enum, giving users an incomplete type when the definition is
10867      actually available.  Note that we do not want to do this for all
10868      enums which are just declarations, because C++0x allows forward
10869      enum declarations.  */
10870   if (cu->per_cu->is_debug_types
10871       && die_is_declaration (die, cu))
10872     {
10873       struct signatured_type *sig_type;
10874
10875       sig_type
10876         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
10877                                             cu->per_cu->info_or_types_section,
10878                                             cu->per_cu->offset);
10879       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
10880       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
10881         return;
10882     }
10883
10884   new_symbol (die, this_type, cu);
10885 }
10886
10887 /* Extract all information from a DW_TAG_array_type DIE and put it in
10888    the DIE's type field.  For now, this only handles one dimensional
10889    arrays.  */
10890
10891 static struct type *
10892 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
10893 {
10894   struct objfile *objfile = cu->objfile;
10895   struct die_info *child_die;
10896   struct type *type;
10897   struct type *element_type, *range_type, *index_type;
10898   struct type **range_types = NULL;
10899   struct attribute *attr;
10900   int ndim = 0;
10901   struct cleanup *back_to;
10902   char *name;
10903
10904   element_type = die_type (die, cu);
10905
10906   /* The die_type call above may have already set the type for this DIE.  */
10907   type = get_die_type (die, cu);
10908   if (type)
10909     return type;
10910
10911   /* Irix 6.2 native cc creates array types without children for
10912      arrays with unspecified length.  */
10913   if (die->child == NULL)
10914     {
10915       index_type = objfile_type (objfile)->builtin_int;
10916       range_type = create_range_type (NULL, index_type, 0, -1);
10917       type = create_array_type (NULL, element_type, range_type);
10918       return set_die_type (die, type, cu);
10919     }
10920
10921   back_to = make_cleanup (null_cleanup, NULL);
10922   child_die = die->child;
10923   while (child_die && child_die->tag)
10924     {
10925       if (child_die->tag == DW_TAG_subrange_type)
10926         {
10927           struct type *child_type = read_type_die (child_die, cu);
10928
10929           if (child_type != NULL)
10930             {
10931               /* The range type was succesfully read.  Save it for the
10932                  array type creation.  */
10933               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
10934                 {
10935                   range_types = (struct type **)
10936                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
10937                               * sizeof (struct type *));
10938                   if (ndim == 0)
10939                     make_cleanup (free_current_contents, &range_types);
10940                 }
10941               range_types[ndim++] = child_type;
10942             }
10943         }
10944       child_die = sibling_die (child_die);
10945     }
10946
10947   /* Dwarf2 dimensions are output from left to right, create the
10948      necessary array types in backwards order.  */
10949
10950   type = element_type;
10951
10952   if (read_array_order (die, cu) == DW_ORD_col_major)
10953     {
10954       int i = 0;
10955
10956       while (i < ndim)
10957         type = create_array_type (NULL, type, range_types[i++]);
10958     }
10959   else
10960     {
10961       while (ndim-- > 0)
10962         type = create_array_type (NULL, type, range_types[ndim]);
10963     }
10964
10965   /* Understand Dwarf2 support for vector types (like they occur on
10966      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
10967      array type.  This is not part of the Dwarf2/3 standard yet, but a
10968      custom vendor extension.  The main difference between a regular
10969      array and the vector variant is that vectors are passed by value
10970      to functions.  */
10971   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
10972   if (attr)
10973     make_vector_type (type);
10974
10975   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
10976      implementation may choose to implement triple vectors using this
10977      attribute.  */
10978   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
10979   if (attr)
10980     {
10981       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
10982         TYPE_LENGTH (type) = DW_UNSND (attr);
10983       else
10984         complaint (&symfile_complaints,
10985                    _("DW_AT_byte_size for array type smaller "
10986                      "than the total size of elements"));
10987     }
10988
10989   name = dwarf2_name (die, cu);
10990   if (name)
10991     TYPE_NAME (type) = name;
10992
10993   /* Install the type in the die.  */
10994   set_die_type (die, type, cu);
10995
10996   /* set_die_type should be already done.  */
10997   set_descriptive_type (type, die, cu);
10998
10999   do_cleanups (back_to);
11000
11001   return type;
11002 }
11003
11004 static enum dwarf_array_dim_ordering
11005 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
11006 {
11007   struct attribute *attr;
11008
11009   attr = dwarf2_attr (die, DW_AT_ordering, cu);
11010
11011   if (attr) return DW_SND (attr);
11012
11013   /* GNU F77 is a special case, as at 08/2004 array type info is the
11014      opposite order to the dwarf2 specification, but data is still
11015      laid out as per normal fortran.
11016
11017      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
11018      version checking.  */
11019
11020   if (cu->language == language_fortran
11021       && cu->producer && strstr (cu->producer, "GNU F77"))
11022     {
11023       return DW_ORD_row_major;
11024     }
11025
11026   switch (cu->language_defn->la_array_ordering)
11027     {
11028     case array_column_major:
11029       return DW_ORD_col_major;
11030     case array_row_major:
11031     default:
11032       return DW_ORD_row_major;
11033     };
11034 }
11035
11036 /* Extract all information from a DW_TAG_set_type DIE and put it in
11037    the DIE's type field.  */
11038
11039 static struct type *
11040 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
11041 {
11042   struct type *domain_type, *set_type;
11043   struct attribute *attr;
11044
11045   domain_type = die_type (die, cu);
11046
11047   /* The die_type call above may have already set the type for this DIE.  */
11048   set_type = get_die_type (die, cu);
11049   if (set_type)
11050     return set_type;
11051
11052   set_type = create_set_type (NULL, domain_type);
11053
11054   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11055   if (attr)
11056     TYPE_LENGTH (set_type) = DW_UNSND (attr);
11057
11058   return set_die_type (die, set_type, cu);
11059 }
11060
11061 /* First cut: install each common block member as a global variable.  */
11062
11063 static void
11064 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
11065 {
11066   struct die_info *child_die;
11067   struct attribute *attr;
11068   struct symbol *sym;
11069   CORE_ADDR base = (CORE_ADDR) 0;
11070
11071   attr = dwarf2_attr (die, DW_AT_location, cu);
11072   if (attr)
11073     {
11074       /* Support the .debug_loc offsets.  */
11075       if (attr_form_is_block (attr))
11076         {
11077           base = decode_locdesc (DW_BLOCK (attr), cu);
11078         }
11079       else if (attr_form_is_section_offset (attr))
11080         {
11081           dwarf2_complex_location_expr_complaint ();
11082         }
11083       else
11084         {
11085           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11086                                                  "common block member");
11087         }
11088     }
11089   if (die->child != NULL)
11090     {
11091       child_die = die->child;
11092       while (child_die && child_die->tag)
11093         {
11094           LONGEST offset;
11095
11096           sym = new_symbol (child_die, NULL, cu);
11097           if (sym != NULL
11098               && handle_data_member_location (child_die, cu, &offset))
11099             {
11100               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
11101               add_symbol_to_list (sym, &global_symbols);
11102             }
11103           child_die = sibling_die (child_die);
11104         }
11105     }
11106 }
11107
11108 /* Create a type for a C++ namespace.  */
11109
11110 static struct type *
11111 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
11112 {
11113   struct objfile *objfile = cu->objfile;
11114   const char *previous_prefix, *name;
11115   int is_anonymous;
11116   struct type *type;
11117
11118   /* For extensions, reuse the type of the original namespace.  */
11119   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
11120     {
11121       struct die_info *ext_die;
11122       struct dwarf2_cu *ext_cu = cu;
11123
11124       ext_die = dwarf2_extension (die, &ext_cu);
11125       type = read_type_die (ext_die, ext_cu);
11126
11127       /* EXT_CU may not be the same as CU.
11128          Ensure TYPE is recorded in CU's type_hash table.  */
11129       return set_die_type (die, type, cu);
11130     }
11131
11132   name = namespace_name (die, &is_anonymous, cu);
11133
11134   /* Now build the name of the current namespace.  */
11135
11136   previous_prefix = determine_prefix (die, cu);
11137   if (previous_prefix[0] != '\0')
11138     name = typename_concat (&objfile->objfile_obstack,
11139                             previous_prefix, name, 0, cu);
11140
11141   /* Create the type.  */
11142   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
11143                     objfile);
11144   TYPE_NAME (type) = (char *) name;
11145   TYPE_TAG_NAME (type) = TYPE_NAME (type);
11146
11147   return set_die_type (die, type, cu);
11148 }
11149
11150 /* Read a C++ namespace.  */
11151
11152 static void
11153 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
11154 {
11155   struct objfile *objfile = cu->objfile;
11156   int is_anonymous;
11157
11158   /* Add a symbol associated to this if we haven't seen the namespace
11159      before.  Also, add a using directive if it's an anonymous
11160      namespace.  */
11161
11162   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
11163     {
11164       struct type *type;
11165
11166       type = read_type_die (die, cu);
11167       new_symbol (die, type, cu);
11168
11169       namespace_name (die, &is_anonymous, cu);
11170       if (is_anonymous)
11171         {
11172           const char *previous_prefix = determine_prefix (die, cu);
11173
11174           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
11175                                   NULL, NULL, &objfile->objfile_obstack);
11176         }
11177     }
11178
11179   if (die->child != NULL)
11180     {
11181       struct die_info *child_die = die->child;
11182
11183       while (child_die && child_die->tag)
11184         {
11185           process_die (child_die, cu);
11186           child_die = sibling_die (child_die);
11187         }
11188     }
11189 }
11190
11191 /* Read a Fortran module as type.  This DIE can be only a declaration used for
11192    imported module.  Still we need that type as local Fortran "use ... only"
11193    declaration imports depend on the created type in determine_prefix.  */
11194
11195 static struct type *
11196 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
11197 {
11198   struct objfile *objfile = cu->objfile;
11199   char *module_name;
11200   struct type *type;
11201
11202   module_name = dwarf2_name (die, cu);
11203   if (!module_name)
11204     complaint (&symfile_complaints,
11205                _("DW_TAG_module has no name, offset 0x%x"),
11206                die->offset.sect_off);
11207   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
11208
11209   /* determine_prefix uses TYPE_TAG_NAME.  */
11210   TYPE_TAG_NAME (type) = TYPE_NAME (type);
11211
11212   return set_die_type (die, type, cu);
11213 }
11214
11215 /* Read a Fortran module.  */
11216
11217 static void
11218 read_module (struct die_info *die, struct dwarf2_cu *cu)
11219 {
11220   struct die_info *child_die = die->child;
11221
11222   while (child_die && child_die->tag)
11223     {
11224       process_die (child_die, cu);
11225       child_die = sibling_die (child_die);
11226     }
11227 }
11228
11229 /* Return the name of the namespace represented by DIE.  Set
11230    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
11231    namespace.  */
11232
11233 static const char *
11234 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
11235 {
11236   struct die_info *current_die;
11237   const char *name = NULL;
11238
11239   /* Loop through the extensions until we find a name.  */
11240
11241   for (current_die = die;
11242        current_die != NULL;
11243        current_die = dwarf2_extension (die, &cu))
11244     {
11245       name = dwarf2_name (current_die, cu);
11246       if (name != NULL)
11247         break;
11248     }
11249
11250   /* Is it an anonymous namespace?  */
11251
11252   *is_anonymous = (name == NULL);
11253   if (*is_anonymous)
11254     name = CP_ANONYMOUS_NAMESPACE_STR;
11255
11256   return name;
11257 }
11258
11259 /* Extract all information from a DW_TAG_pointer_type DIE and add to
11260    the user defined type vector.  */
11261
11262 static struct type *
11263 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
11264 {
11265   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
11266   struct comp_unit_head *cu_header = &cu->header;
11267   struct type *type;
11268   struct attribute *attr_byte_size;
11269   struct attribute *attr_address_class;
11270   int byte_size, addr_class;
11271   struct type *target_type;
11272
11273   target_type = die_type (die, cu);
11274
11275   /* The die_type call above may have already set the type for this DIE.  */
11276   type = get_die_type (die, cu);
11277   if (type)
11278     return type;
11279
11280   type = lookup_pointer_type (target_type);
11281
11282   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
11283   if (attr_byte_size)
11284     byte_size = DW_UNSND (attr_byte_size);
11285   else
11286     byte_size = cu_header->addr_size;
11287
11288   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
11289   if (attr_address_class)
11290     addr_class = DW_UNSND (attr_address_class);
11291   else
11292     addr_class = DW_ADDR_none;
11293
11294   /* If the pointer size or address class is different than the
11295      default, create a type variant marked as such and set the
11296      length accordingly.  */
11297   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
11298     {
11299       if (gdbarch_address_class_type_flags_p (gdbarch))
11300         {
11301           int type_flags;
11302
11303           type_flags = gdbarch_address_class_type_flags
11304                          (gdbarch, byte_size, addr_class);
11305           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
11306                       == 0);
11307           type = make_type_with_address_space (type, type_flags);
11308         }
11309       else if (TYPE_LENGTH (type) != byte_size)
11310         {
11311           complaint (&symfile_complaints,
11312                      _("invalid pointer size %d"), byte_size);
11313         }
11314       else
11315         {
11316           /* Should we also complain about unhandled address classes?  */
11317         }
11318     }
11319
11320   TYPE_LENGTH (type) = byte_size;
11321   return set_die_type (die, type, cu);
11322 }
11323
11324 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
11325    the user defined type vector.  */
11326
11327 static struct type *
11328 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
11329 {
11330   struct type *type;
11331   struct type *to_type;
11332   struct type *domain;
11333
11334   to_type = die_type (die, cu);
11335   domain = die_containing_type (die, cu);
11336
11337   /* The calls above may have already set the type for this DIE.  */
11338   type = get_die_type (die, cu);
11339   if (type)
11340     return type;
11341
11342   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
11343     type = lookup_methodptr_type (to_type);
11344   else
11345     type = lookup_memberptr_type (to_type, domain);
11346
11347   return set_die_type (die, type, cu);
11348 }
11349
11350 /* Extract all information from a DW_TAG_reference_type DIE and add to
11351    the user defined type vector.  */
11352
11353 static struct type *
11354 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
11355 {
11356   struct comp_unit_head *cu_header = &cu->header;
11357   struct type *type, *target_type;
11358   struct attribute *attr;
11359
11360   target_type = die_type (die, cu);
11361
11362   /* The die_type call above may have already set the type for this DIE.  */
11363   type = get_die_type (die, cu);
11364   if (type)
11365     return type;
11366
11367   type = lookup_reference_type (target_type);
11368   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11369   if (attr)
11370     {
11371       TYPE_LENGTH (type) = DW_UNSND (attr);
11372     }
11373   else
11374     {
11375       TYPE_LENGTH (type) = cu_header->addr_size;
11376     }
11377   return set_die_type (die, type, cu);
11378 }
11379
11380 static struct type *
11381 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
11382 {
11383   struct type *base_type, *cv_type;
11384
11385   base_type = die_type (die, cu);
11386
11387   /* The die_type call above may have already set the type for this DIE.  */
11388   cv_type = get_die_type (die, cu);
11389   if (cv_type)
11390     return cv_type;
11391
11392   /* In case the const qualifier is applied to an array type, the element type
11393      is so qualified, not the array type (section 6.7.3 of C99).  */
11394   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
11395     {
11396       struct type *el_type, *inner_array;
11397
11398       base_type = copy_type (base_type);
11399       inner_array = base_type;
11400
11401       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
11402         {
11403           TYPE_TARGET_TYPE (inner_array) =
11404             copy_type (TYPE_TARGET_TYPE (inner_array));
11405           inner_array = TYPE_TARGET_TYPE (inner_array);
11406         }
11407
11408       el_type = TYPE_TARGET_TYPE (inner_array);
11409       TYPE_TARGET_TYPE (inner_array) =
11410         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
11411
11412       return set_die_type (die, base_type, cu);
11413     }
11414
11415   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
11416   return set_die_type (die, cv_type, cu);
11417 }
11418
11419 static struct type *
11420 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
11421 {
11422   struct type *base_type, *cv_type;
11423
11424   base_type = die_type (die, cu);
11425
11426   /* The die_type call above may have already set the type for this DIE.  */
11427   cv_type = get_die_type (die, cu);
11428   if (cv_type)
11429     return cv_type;
11430
11431   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
11432   return set_die_type (die, cv_type, cu);
11433 }
11434
11435 /* Extract all information from a DW_TAG_string_type DIE and add to
11436    the user defined type vector.  It isn't really a user defined type,
11437    but it behaves like one, with other DIE's using an AT_user_def_type
11438    attribute to reference it.  */
11439
11440 static struct type *
11441 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
11442 {
11443   struct objfile *objfile = cu->objfile;
11444   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11445   struct type *type, *range_type, *index_type, *char_type;
11446   struct attribute *attr;
11447   unsigned int length;
11448
11449   attr = dwarf2_attr (die, DW_AT_string_length, cu);
11450   if (attr)
11451     {
11452       length = DW_UNSND (attr);
11453     }
11454   else
11455     {
11456       /* Check for the DW_AT_byte_size attribute.  */
11457       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11458       if (attr)
11459         {
11460           length = DW_UNSND (attr);
11461         }
11462       else
11463         {
11464           length = 1;
11465         }
11466     }
11467
11468   index_type = objfile_type (objfile)->builtin_int;
11469   range_type = create_range_type (NULL, index_type, 1, length);
11470   char_type = language_string_char_type (cu->language_defn, gdbarch);
11471   type = create_string_type (NULL, char_type, range_type);
11472
11473   return set_die_type (die, type, cu);
11474 }
11475
11476 /* Handle DIES due to C code like:
11477
11478    struct foo
11479    {
11480    int (*funcp)(int a, long l);
11481    int b;
11482    };
11483
11484    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
11485
11486 static struct type *
11487 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
11488 {
11489   struct objfile *objfile = cu->objfile;
11490   struct type *type;            /* Type that this function returns.  */
11491   struct type *ftype;           /* Function that returns above type.  */
11492   struct attribute *attr;
11493
11494   type = die_type (die, cu);
11495
11496   /* The die_type call above may have already set the type for this DIE.  */
11497   ftype = get_die_type (die, cu);
11498   if (ftype)
11499     return ftype;
11500
11501   ftype = lookup_function_type (type);
11502
11503   /* All functions in C++, Pascal and Java have prototypes.  */
11504   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
11505   if ((attr && (DW_UNSND (attr) != 0))
11506       || cu->language == language_cplus
11507       || cu->language == language_java
11508       || cu->language == language_pascal)
11509     TYPE_PROTOTYPED (ftype) = 1;
11510   else if (producer_is_realview (cu->producer))
11511     /* RealView does not emit DW_AT_prototyped.  We can not
11512        distinguish prototyped and unprototyped functions; default to
11513        prototyped, since that is more common in modern code (and
11514        RealView warns about unprototyped functions).  */
11515     TYPE_PROTOTYPED (ftype) = 1;
11516
11517   /* Store the calling convention in the type if it's available in
11518      the subroutine die.  Otherwise set the calling convention to
11519      the default value DW_CC_normal.  */
11520   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
11521   if (attr)
11522     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
11523   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
11524     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
11525   else
11526     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
11527
11528   /* We need to add the subroutine type to the die immediately so
11529      we don't infinitely recurse when dealing with parameters
11530      declared as the same subroutine type.  */
11531   set_die_type (die, ftype, cu);
11532
11533   if (die->child != NULL)
11534     {
11535       struct type *void_type = objfile_type (objfile)->builtin_void;
11536       struct die_info *child_die;
11537       int nparams, iparams;
11538
11539       /* Count the number of parameters.
11540          FIXME: GDB currently ignores vararg functions, but knows about
11541          vararg member functions.  */
11542       nparams = 0;
11543       child_die = die->child;
11544       while (child_die && child_die->tag)
11545         {
11546           if (child_die->tag == DW_TAG_formal_parameter)
11547             nparams++;
11548           else if (child_die->tag == DW_TAG_unspecified_parameters)
11549             TYPE_VARARGS (ftype) = 1;
11550           child_die = sibling_die (child_die);
11551         }
11552
11553       /* Allocate storage for parameters and fill them in.  */
11554       TYPE_NFIELDS (ftype) = nparams;
11555       TYPE_FIELDS (ftype) = (struct field *)
11556         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
11557
11558       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
11559          even if we error out during the parameters reading below.  */
11560       for (iparams = 0; iparams < nparams; iparams++)
11561         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
11562
11563       iparams = 0;
11564       child_die = die->child;
11565       while (child_die && child_die->tag)
11566         {
11567           if (child_die->tag == DW_TAG_formal_parameter)
11568             {
11569               struct type *arg_type;
11570
11571               /* DWARF version 2 has no clean way to discern C++
11572                  static and non-static member functions.  G++ helps
11573                  GDB by marking the first parameter for non-static
11574                  member functions (which is the this pointer) as
11575                  artificial.  We pass this information to
11576                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
11577
11578                  DWARF version 3 added DW_AT_object_pointer, which GCC
11579                  4.5 does not yet generate.  */
11580               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
11581               if (attr)
11582                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
11583               else
11584                 {
11585                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
11586
11587                   /* GCC/43521: In java, the formal parameter
11588                      "this" is sometimes not marked with DW_AT_artificial.  */
11589                   if (cu->language == language_java)
11590                     {
11591                       const char *name = dwarf2_name (child_die, cu);
11592
11593                       if (name && !strcmp (name, "this"))
11594                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
11595                     }
11596                 }
11597               arg_type = die_type (child_die, cu);
11598
11599               /* RealView does not mark THIS as const, which the testsuite
11600                  expects.  GCC marks THIS as const in method definitions,
11601                  but not in the class specifications (GCC PR 43053).  */
11602               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
11603                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
11604                 {
11605                   int is_this = 0;
11606                   struct dwarf2_cu *arg_cu = cu;
11607                   const char *name = dwarf2_name (child_die, cu);
11608
11609                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
11610                   if (attr)
11611                     {
11612                       /* If the compiler emits this, use it.  */
11613                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
11614                         is_this = 1;
11615                     }
11616                   else if (name && strcmp (name, "this") == 0)
11617                     /* Function definitions will have the argument names.  */
11618                     is_this = 1;
11619                   else if (name == NULL && iparams == 0)
11620                     /* Declarations may not have the names, so like
11621                        elsewhere in GDB, assume an artificial first
11622                        argument is "this".  */
11623                     is_this = 1;
11624
11625                   if (is_this)
11626                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
11627                                              arg_type, 0);
11628                 }
11629
11630               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
11631               iparams++;
11632             }
11633           child_die = sibling_die (child_die);
11634         }
11635     }
11636
11637   return ftype;
11638 }
11639
11640 static struct type *
11641 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
11642 {
11643   struct objfile *objfile = cu->objfile;
11644   const char *name = NULL;
11645   struct type *this_type, *target_type;
11646
11647   name = dwarf2_full_name (NULL, die, cu);
11648   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
11649                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
11650   TYPE_NAME (this_type) = (char *) name;
11651   set_die_type (die, this_type, cu);
11652   target_type = die_type (die, cu);
11653   if (target_type != this_type)
11654     TYPE_TARGET_TYPE (this_type) = target_type;
11655   else
11656     {
11657       /* Self-referential typedefs are, it seems, not allowed by the DWARF
11658          spec and cause infinite loops in GDB.  */
11659       complaint (&symfile_complaints,
11660                  _("Self-referential DW_TAG_typedef "
11661                    "- DIE at 0x%x [in module %s]"),
11662                  die->offset.sect_off, objfile->name);
11663       TYPE_TARGET_TYPE (this_type) = NULL;
11664     }
11665   return this_type;
11666 }
11667
11668 /* Find a representation of a given base type and install
11669    it in the TYPE field of the die.  */
11670
11671 static struct type *
11672 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
11673 {
11674   struct objfile *objfile = cu->objfile;
11675   struct type *type;
11676   struct attribute *attr;
11677   int encoding = 0, size = 0;
11678   char *name;
11679   enum type_code code = TYPE_CODE_INT;
11680   int type_flags = 0;
11681   struct type *target_type = NULL;
11682
11683   attr = dwarf2_attr (die, DW_AT_encoding, cu);
11684   if (attr)
11685     {
11686       encoding = DW_UNSND (attr);
11687     }
11688   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11689   if (attr)
11690     {
11691       size = DW_UNSND (attr);
11692     }
11693   name = dwarf2_name (die, cu);
11694   if (!name)
11695     {
11696       complaint (&symfile_complaints,
11697                  _("DW_AT_name missing from DW_TAG_base_type"));
11698     }
11699
11700   switch (encoding)
11701     {
11702       case DW_ATE_address:
11703         /* Turn DW_ATE_address into a void * pointer.  */
11704         code = TYPE_CODE_PTR;
11705         type_flags |= TYPE_FLAG_UNSIGNED;
11706         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
11707         break;
11708       case DW_ATE_boolean:
11709         code = TYPE_CODE_BOOL;
11710         type_flags |= TYPE_FLAG_UNSIGNED;
11711         break;
11712       case DW_ATE_complex_float:
11713         code = TYPE_CODE_COMPLEX;
11714         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
11715         break;
11716       case DW_ATE_decimal_float:
11717         code = TYPE_CODE_DECFLOAT;
11718         break;
11719       case DW_ATE_float:
11720         code = TYPE_CODE_FLT;
11721         break;
11722       case DW_ATE_signed:
11723         break;
11724       case DW_ATE_unsigned:
11725         type_flags |= TYPE_FLAG_UNSIGNED;
11726         if (cu->language == language_fortran
11727             && name
11728             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
11729           code = TYPE_CODE_CHAR;
11730         break;
11731       case DW_ATE_signed_char:
11732         if (cu->language == language_ada || cu->language == language_m2
11733             || cu->language == language_pascal
11734             || cu->language == language_fortran)
11735           code = TYPE_CODE_CHAR;
11736         break;
11737       case DW_ATE_unsigned_char:
11738         if (cu->language == language_ada || cu->language == language_m2
11739             || cu->language == language_pascal
11740             || cu->language == language_fortran)
11741           code = TYPE_CODE_CHAR;
11742         type_flags |= TYPE_FLAG_UNSIGNED;
11743         break;
11744       case DW_ATE_UTF:
11745         /* We just treat this as an integer and then recognize the
11746            type by name elsewhere.  */
11747         break;
11748
11749       default:
11750         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
11751                    dwarf_type_encoding_name (encoding));
11752         break;
11753     }
11754
11755   type = init_type (code, size, type_flags, NULL, objfile);
11756   TYPE_NAME (type) = name;
11757   TYPE_TARGET_TYPE (type) = target_type;
11758
11759   if (name && strcmp (name, "char") == 0)
11760     TYPE_NOSIGN (type) = 1;
11761
11762   return set_die_type (die, type, cu);
11763 }
11764
11765 /* Read the given DW_AT_subrange DIE.  */
11766
11767 static struct type *
11768 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
11769 {
11770   struct type *base_type;
11771   struct type *range_type;
11772   struct attribute *attr;
11773   LONGEST low, high;
11774   int low_default_is_valid;
11775   char *name;
11776   LONGEST negative_mask;
11777
11778   base_type = die_type (die, cu);
11779   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
11780   check_typedef (base_type);
11781
11782   /* The die_type call above may have already set the type for this DIE.  */
11783   range_type = get_die_type (die, cu);
11784   if (range_type)
11785     return range_type;
11786
11787   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
11788      omitting DW_AT_lower_bound.  */
11789   switch (cu->language)
11790     {
11791     case language_c:
11792     case language_cplus:
11793       low = 0;
11794       low_default_is_valid = 1;
11795       break;
11796     case language_fortran:
11797       low = 1;
11798       low_default_is_valid = 1;
11799       break;
11800     case language_d:
11801     case language_java:
11802     case language_objc:
11803       low = 0;
11804       low_default_is_valid = (cu->header.version >= 4);
11805       break;
11806     case language_ada:
11807     case language_m2:
11808     case language_pascal:
11809       low = 1;
11810       low_default_is_valid = (cu->header.version >= 4);
11811       break;
11812     default:
11813       low = 0;
11814       low_default_is_valid = 0;
11815       break;
11816     }
11817
11818   /* FIXME: For variable sized arrays either of these could be
11819      a variable rather than a constant value.  We'll allow it,
11820      but we don't know how to handle it.  */
11821   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
11822   if (attr)
11823     low = dwarf2_get_attr_constant_value (attr, low);
11824   else if (!low_default_is_valid)
11825     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
11826                                       "- DIE at 0x%x [in module %s]"),
11827                die->offset.sect_off, cu->objfile->name);
11828
11829   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
11830   if (attr)
11831     {
11832       if (attr_form_is_block (attr) || is_ref_attr (attr))
11833         {
11834           /* GCC encodes arrays with unspecified or dynamic length
11835              with a DW_FORM_block1 attribute or a reference attribute.
11836              FIXME: GDB does not yet know how to handle dynamic
11837              arrays properly, treat them as arrays with unspecified
11838              length for now.
11839
11840              FIXME: jimb/2003-09-22: GDB does not really know
11841              how to handle arrays of unspecified length
11842              either; we just represent them as zero-length
11843              arrays.  Choose an appropriate upper bound given
11844              the lower bound we've computed above.  */
11845           high = low - 1;
11846         }
11847       else
11848         high = dwarf2_get_attr_constant_value (attr, 1);
11849     }
11850   else
11851     {
11852       attr = dwarf2_attr (die, DW_AT_count, cu);
11853       if (attr)
11854         {
11855           int count = dwarf2_get_attr_constant_value (attr, 1);
11856           high = low + count - 1;
11857         }
11858       else
11859         {
11860           /* Unspecified array length.  */
11861           high = low - 1;
11862         }
11863     }
11864
11865   /* Dwarf-2 specifications explicitly allows to create subrange types
11866      without specifying a base type.
11867      In that case, the base type must be set to the type of
11868      the lower bound, upper bound or count, in that order, if any of these
11869      three attributes references an object that has a type.
11870      If no base type is found, the Dwarf-2 specifications say that
11871      a signed integer type of size equal to the size of an address should
11872      be used.
11873      For the following C code: `extern char gdb_int [];'
11874      GCC produces an empty range DIE.
11875      FIXME: muller/2010-05-28: Possible references to object for low bound,
11876      high bound or count are not yet handled by this code.  */
11877   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
11878     {
11879       struct objfile *objfile = cu->objfile;
11880       struct gdbarch *gdbarch = get_objfile_arch (objfile);
11881       int addr_size = gdbarch_addr_bit (gdbarch) /8;
11882       struct type *int_type = objfile_type (objfile)->builtin_int;
11883
11884       /* Test "int", "long int", and "long long int" objfile types,
11885          and select the first one having a size above or equal to the
11886          architecture address size.  */
11887       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11888         base_type = int_type;
11889       else
11890         {
11891           int_type = objfile_type (objfile)->builtin_long;
11892           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11893             base_type = int_type;
11894           else
11895             {
11896               int_type = objfile_type (objfile)->builtin_long_long;
11897               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
11898                 base_type = int_type;
11899             }
11900         }
11901     }
11902
11903   negative_mask =
11904     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
11905   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
11906     low |= negative_mask;
11907   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
11908     high |= negative_mask;
11909
11910   range_type = create_range_type (NULL, base_type, low, high);
11911
11912   /* Mark arrays with dynamic length at least as an array of unspecified
11913      length.  GDB could check the boundary but before it gets implemented at
11914      least allow accessing the array elements.  */
11915   if (attr && attr_form_is_block (attr))
11916     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11917
11918   /* Ada expects an empty array on no boundary attributes.  */
11919   if (attr == NULL && cu->language != language_ada)
11920     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
11921
11922   name = dwarf2_name (die, cu);
11923   if (name)
11924     TYPE_NAME (range_type) = name;
11925
11926   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11927   if (attr)
11928     TYPE_LENGTH (range_type) = DW_UNSND (attr);
11929
11930   set_die_type (die, range_type, cu);
11931
11932   /* set_die_type should be already done.  */
11933   set_descriptive_type (range_type, die, cu);
11934
11935   return range_type;
11936 }
11937
11938 static struct type *
11939 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
11940 {
11941   struct type *type;
11942
11943   /* For now, we only support the C meaning of an unspecified type: void.  */
11944
11945   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
11946   TYPE_NAME (type) = dwarf2_name (die, cu);
11947
11948   return set_die_type (die, type, cu);
11949 }
11950
11951 /* Read a single die and all its descendents.  Set the die's sibling
11952    field to NULL; set other fields in the die correctly, and set all
11953    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
11954    location of the info_ptr after reading all of those dies.  PARENT
11955    is the parent of the die in question.  */
11956
11957 static struct die_info *
11958 read_die_and_children (const struct die_reader_specs *reader,
11959                        gdb_byte *info_ptr,
11960                        gdb_byte **new_info_ptr,
11961                        struct die_info *parent)
11962 {
11963   struct die_info *die;
11964   gdb_byte *cur_ptr;
11965   int has_children;
11966
11967   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
11968   if (die == NULL)
11969     {
11970       *new_info_ptr = cur_ptr;
11971       return NULL;
11972     }
11973   store_in_ref_table (die, reader->cu);
11974
11975   if (has_children)
11976     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
11977   else
11978     {
11979       die->child = NULL;
11980       *new_info_ptr = cur_ptr;
11981     }
11982
11983   die->sibling = NULL;
11984   die->parent = parent;
11985   return die;
11986 }
11987
11988 /* Read a die, all of its descendents, and all of its siblings; set
11989    all of the fields of all of the dies correctly.  Arguments are as
11990    in read_die_and_children.  */
11991
11992 static struct die_info *
11993 read_die_and_siblings (const struct die_reader_specs *reader,
11994                        gdb_byte *info_ptr,
11995                        gdb_byte **new_info_ptr,
11996                        struct die_info *parent)
11997 {
11998   struct die_info *first_die, *last_sibling;
11999   gdb_byte *cur_ptr;
12000
12001   cur_ptr = info_ptr;
12002   first_die = last_sibling = NULL;
12003
12004   while (1)
12005     {
12006       struct die_info *die
12007         = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
12008
12009       if (die == NULL)
12010         {
12011           *new_info_ptr = cur_ptr;
12012           return first_die;
12013         }
12014
12015       if (!first_die)
12016         first_die = die;
12017       else
12018         last_sibling->sibling = die;
12019
12020       last_sibling = die;
12021     }
12022 }
12023
12024 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
12025    attributes.
12026    The caller is responsible for filling in the extra attributes
12027    and updating (*DIEP)->num_attrs.
12028    Set DIEP to point to a newly allocated die with its information,
12029    except for its child, sibling, and parent fields.
12030    Set HAS_CHILDREN to tell whether the die has children or not.  */
12031
12032 static gdb_byte *
12033 read_full_die_1 (const struct die_reader_specs *reader,
12034                  struct die_info **diep, gdb_byte *info_ptr,
12035                  int *has_children, int num_extra_attrs)
12036 {
12037   unsigned int abbrev_number, bytes_read, i;
12038   sect_offset offset;
12039   struct abbrev_info *abbrev;
12040   struct die_info *die;
12041   struct dwarf2_cu *cu = reader->cu;
12042   bfd *abfd = reader->abfd;
12043
12044   offset.sect_off = info_ptr - reader->buffer;
12045   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12046   info_ptr += bytes_read;
12047   if (!abbrev_number)
12048     {
12049       *diep = NULL;
12050       *has_children = 0;
12051       return info_ptr;
12052     }
12053
12054   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
12055   if (!abbrev)
12056     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
12057            abbrev_number,
12058            bfd_get_filename (abfd));
12059
12060   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
12061   die->offset = offset;
12062   die->tag = abbrev->tag;
12063   die->abbrev = abbrev_number;
12064
12065   /* Make the result usable.
12066      The caller needs to update num_attrs after adding the extra
12067      attributes.  */
12068   die->num_attrs = abbrev->num_attrs;
12069
12070   for (i = 0; i < abbrev->num_attrs; ++i)
12071     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
12072                                info_ptr);
12073
12074   *diep = die;
12075   *has_children = abbrev->has_children;
12076   return info_ptr;
12077 }
12078
12079 /* Read a die and all its attributes.
12080    Set DIEP to point to a newly allocated die with its information,
12081    except for its child, sibling, and parent fields.
12082    Set HAS_CHILDREN to tell whether the die has children or not.  */
12083
12084 static gdb_byte *
12085 read_full_die (const struct die_reader_specs *reader,
12086                struct die_info **diep, gdb_byte *info_ptr,
12087                int *has_children)
12088 {
12089   return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
12090 }
12091 \f
12092 /* Abbreviation tables.
12093
12094    In DWARF version 2, the description of the debugging information is
12095    stored in a separate .debug_abbrev section.  Before we read any
12096    dies from a section we read in all abbreviations and install them
12097    in a hash table.  */
12098
12099 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
12100
12101 static struct abbrev_info *
12102 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
12103 {
12104   struct abbrev_info *abbrev;
12105
12106   abbrev = (struct abbrev_info *)
12107     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
12108   memset (abbrev, 0, sizeof (struct abbrev_info));
12109   return abbrev;
12110 }
12111
12112 /* Add an abbreviation to the table.  */
12113
12114 static void
12115 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
12116                          unsigned int abbrev_number,
12117                          struct abbrev_info *abbrev)
12118 {
12119   unsigned int hash_number;
12120
12121   hash_number = abbrev_number % ABBREV_HASH_SIZE;
12122   abbrev->next = abbrev_table->abbrevs[hash_number];
12123   abbrev_table->abbrevs[hash_number] = abbrev;
12124 }
12125
12126 /* Look up an abbrev in the table.
12127    Returns NULL if the abbrev is not found.  */
12128
12129 static struct abbrev_info *
12130 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
12131                             unsigned int abbrev_number)
12132 {
12133   unsigned int hash_number;
12134   struct abbrev_info *abbrev;
12135
12136   hash_number = abbrev_number % ABBREV_HASH_SIZE;
12137   abbrev = abbrev_table->abbrevs[hash_number];
12138
12139   while (abbrev)
12140     {
12141       if (abbrev->number == abbrev_number)
12142         return abbrev;
12143       abbrev = abbrev->next;
12144     }
12145   return NULL;
12146 }
12147
12148 /* Read in an abbrev table.  */
12149
12150 static struct abbrev_table *
12151 abbrev_table_read_table (struct dwarf2_section_info *section,
12152                          sect_offset offset)
12153 {
12154   struct objfile *objfile = dwarf2_per_objfile->objfile;
12155   bfd *abfd = section->asection->owner;
12156   struct abbrev_table *abbrev_table;
12157   gdb_byte *abbrev_ptr;
12158   struct abbrev_info *cur_abbrev;
12159   unsigned int abbrev_number, bytes_read, abbrev_name;
12160   unsigned int abbrev_form;
12161   struct attr_abbrev *cur_attrs;
12162   unsigned int allocated_attrs;
12163
12164   abbrev_table = XMALLOC (struct abbrev_table);
12165   abbrev_table->offset = offset;
12166   obstack_init (&abbrev_table->abbrev_obstack);
12167   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
12168                                          (ABBREV_HASH_SIZE
12169                                           * sizeof (struct abbrev_info *)));
12170   memset (abbrev_table->abbrevs, 0,
12171           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
12172
12173   dwarf2_read_section (objfile, section);
12174   abbrev_ptr = section->buffer + offset.sect_off;
12175   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12176   abbrev_ptr += bytes_read;
12177
12178   allocated_attrs = ATTR_ALLOC_CHUNK;
12179   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
12180
12181   /* Loop until we reach an abbrev number of 0.  */
12182   while (abbrev_number)
12183     {
12184       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
12185
12186       /* read in abbrev header */
12187       cur_abbrev->number = abbrev_number;
12188       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12189       abbrev_ptr += bytes_read;
12190       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
12191       abbrev_ptr += 1;
12192
12193       /* now read in declarations */
12194       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12195       abbrev_ptr += bytes_read;
12196       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12197       abbrev_ptr += bytes_read;
12198       while (abbrev_name)
12199         {
12200           if (cur_abbrev->num_attrs == allocated_attrs)
12201             {
12202               allocated_attrs += ATTR_ALLOC_CHUNK;
12203               cur_attrs
12204                 = xrealloc (cur_attrs, (allocated_attrs
12205                                         * sizeof (struct attr_abbrev)));
12206             }
12207
12208           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
12209           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
12210           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12211           abbrev_ptr += bytes_read;
12212           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12213           abbrev_ptr += bytes_read;
12214         }
12215
12216       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
12217                                          (cur_abbrev->num_attrs
12218                                           * sizeof (struct attr_abbrev)));
12219       memcpy (cur_abbrev->attrs, cur_attrs,
12220               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
12221
12222       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
12223
12224       /* Get next abbreviation.
12225          Under Irix6 the abbreviations for a compilation unit are not
12226          always properly terminated with an abbrev number of 0.
12227          Exit loop if we encounter an abbreviation which we have
12228          already read (which means we are about to read the abbreviations
12229          for the next compile unit) or if the end of the abbreviation
12230          table is reached.  */
12231       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
12232         break;
12233       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
12234       abbrev_ptr += bytes_read;
12235       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
12236         break;
12237     }
12238
12239   xfree (cur_attrs);
12240   return abbrev_table;
12241 }
12242
12243 /* Free the resources held by ABBREV_TABLE.  */
12244
12245 static void
12246 abbrev_table_free (struct abbrev_table *abbrev_table)
12247 {
12248   obstack_free (&abbrev_table->abbrev_obstack, NULL);
12249   xfree (abbrev_table);
12250 }
12251
12252 /* Same as abbrev_table_free but as a cleanup.
12253    We pass in a pointer to the pointer to the table so that we can
12254    set the pointer to NULL when we're done.  It also simplifies
12255    build_type_unit_groups.  */
12256
12257 static void
12258 abbrev_table_free_cleanup (void *table_ptr)
12259 {
12260   struct abbrev_table **abbrev_table_ptr = table_ptr;
12261
12262   if (*abbrev_table_ptr != NULL)
12263     abbrev_table_free (*abbrev_table_ptr);
12264   *abbrev_table_ptr = NULL;
12265 }
12266
12267 /* Read the abbrev table for CU from ABBREV_SECTION.  */
12268
12269 static void
12270 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
12271                      struct dwarf2_section_info *abbrev_section)
12272 {
12273   cu->abbrev_table =
12274     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
12275 }
12276
12277 /* Release the memory used by the abbrev table for a compilation unit.  */
12278
12279 static void
12280 dwarf2_free_abbrev_table (void *ptr_to_cu)
12281 {
12282   struct dwarf2_cu *cu = ptr_to_cu;
12283
12284   abbrev_table_free (cu->abbrev_table);
12285   /* Set this to NULL so that we SEGV if we try to read it later,
12286      and also because free_comp_unit verifies this is NULL.  */
12287   cu->abbrev_table = NULL;
12288 }
12289 \f
12290 /* Returns nonzero if TAG represents a type that we might generate a partial
12291    symbol for.  */
12292
12293 static int
12294 is_type_tag_for_partial (int tag)
12295 {
12296   switch (tag)
12297     {
12298 #if 0
12299     /* Some types that would be reasonable to generate partial symbols for,
12300        that we don't at present.  */
12301     case DW_TAG_array_type:
12302     case DW_TAG_file_type:
12303     case DW_TAG_ptr_to_member_type:
12304     case DW_TAG_set_type:
12305     case DW_TAG_string_type:
12306     case DW_TAG_subroutine_type:
12307 #endif
12308     case DW_TAG_base_type:
12309     case DW_TAG_class_type:
12310     case DW_TAG_interface_type:
12311     case DW_TAG_enumeration_type:
12312     case DW_TAG_structure_type:
12313     case DW_TAG_subrange_type:
12314     case DW_TAG_typedef:
12315     case DW_TAG_union_type:
12316       return 1;
12317     default:
12318       return 0;
12319     }
12320 }
12321
12322 /* Load all DIEs that are interesting for partial symbols into memory.  */
12323
12324 static struct partial_die_info *
12325 load_partial_dies (const struct die_reader_specs *reader,
12326                    gdb_byte *info_ptr, int building_psymtab)
12327 {
12328   struct dwarf2_cu *cu = reader->cu;
12329   struct objfile *objfile = cu->objfile;
12330   struct partial_die_info *part_die;
12331   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
12332   struct abbrev_info *abbrev;
12333   unsigned int bytes_read;
12334   unsigned int load_all = 0;
12335   int nesting_level = 1;
12336
12337   parent_die = NULL;
12338   last_die = NULL;
12339
12340   gdb_assert (cu->per_cu != NULL);
12341   if (cu->per_cu->load_all_dies)
12342     load_all = 1;
12343
12344   cu->partial_dies
12345     = htab_create_alloc_ex (cu->header.length / 12,
12346                             partial_die_hash,
12347                             partial_die_eq,
12348                             NULL,
12349                             &cu->comp_unit_obstack,
12350                             hashtab_obstack_allocate,
12351                             dummy_obstack_deallocate);
12352
12353   part_die = obstack_alloc (&cu->comp_unit_obstack,
12354                             sizeof (struct partial_die_info));
12355
12356   while (1)
12357     {
12358       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
12359
12360       /* A NULL abbrev means the end of a series of children.  */
12361       if (abbrev == NULL)
12362         {
12363           if (--nesting_level == 0)
12364             {
12365               /* PART_DIE was probably the last thing allocated on the
12366                  comp_unit_obstack, so we could call obstack_free
12367                  here.  We don't do that because the waste is small,
12368                  and will be cleaned up when we're done with this
12369                  compilation unit.  This way, we're also more robust
12370                  against other users of the comp_unit_obstack.  */
12371               return first_die;
12372             }
12373           info_ptr += bytes_read;
12374           last_die = parent_die;
12375           parent_die = parent_die->die_parent;
12376           continue;
12377         }
12378
12379       /* Check for template arguments.  We never save these; if
12380          they're seen, we just mark the parent, and go on our way.  */
12381       if (parent_die != NULL
12382           && cu->language == language_cplus
12383           && (abbrev->tag == DW_TAG_template_type_param
12384               || abbrev->tag == DW_TAG_template_value_param))
12385         {
12386           parent_die->has_template_arguments = 1;
12387
12388           if (!load_all)
12389             {
12390               /* We don't need a partial DIE for the template argument.  */
12391               info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12392               continue;
12393             }
12394         }
12395
12396       /* We only recurse into c++ subprograms looking for template arguments.
12397          Skip their other children.  */
12398       if (!load_all
12399           && cu->language == language_cplus
12400           && parent_die != NULL
12401           && parent_die->tag == DW_TAG_subprogram)
12402         {
12403           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12404           continue;
12405         }
12406
12407       /* Check whether this DIE is interesting enough to save.  Normally
12408          we would not be interested in members here, but there may be
12409          later variables referencing them via DW_AT_specification (for
12410          static members).  */
12411       if (!load_all
12412           && !is_type_tag_for_partial (abbrev->tag)
12413           && abbrev->tag != DW_TAG_constant
12414           && abbrev->tag != DW_TAG_enumerator
12415           && abbrev->tag != DW_TAG_subprogram
12416           && abbrev->tag != DW_TAG_lexical_block
12417           && abbrev->tag != DW_TAG_variable
12418           && abbrev->tag != DW_TAG_namespace
12419           && abbrev->tag != DW_TAG_module
12420           && abbrev->tag != DW_TAG_member
12421           && abbrev->tag != DW_TAG_imported_unit)
12422         {
12423           /* Otherwise we skip to the next sibling, if any.  */
12424           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
12425           continue;
12426         }
12427
12428       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
12429                                    info_ptr);
12430
12431       /* This two-pass algorithm for processing partial symbols has a
12432          high cost in cache pressure.  Thus, handle some simple cases
12433          here which cover the majority of C partial symbols.  DIEs
12434          which neither have specification tags in them, nor could have
12435          specification tags elsewhere pointing at them, can simply be
12436          processed and discarded.
12437
12438          This segment is also optional; scan_partial_symbols and
12439          add_partial_symbol will handle these DIEs if we chain
12440          them in normally.  When compilers which do not emit large
12441          quantities of duplicate debug information are more common,
12442          this code can probably be removed.  */
12443
12444       /* Any complete simple types at the top level (pretty much all
12445          of them, for a language without namespaces), can be processed
12446          directly.  */
12447       if (parent_die == NULL
12448           && part_die->has_specification == 0
12449           && part_die->is_declaration == 0
12450           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
12451               || part_die->tag == DW_TAG_base_type
12452               || part_die->tag == DW_TAG_subrange_type))
12453         {
12454           if (building_psymtab && part_die->name != NULL)
12455             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12456                                  VAR_DOMAIN, LOC_TYPEDEF,
12457                                  &objfile->static_psymbols,
12458                                  0, (CORE_ADDR) 0, cu->language, objfile);
12459           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12460           continue;
12461         }
12462
12463       /* The exception for DW_TAG_typedef with has_children above is
12464          a workaround of GCC PR debug/47510.  In the case of this complaint
12465          type_name_no_tag_or_error will error on such types later.
12466
12467          GDB skipped children of DW_TAG_typedef by the shortcut above and then
12468          it could not find the child DIEs referenced later, this is checked
12469          above.  In correct DWARF DW_TAG_typedef should have no children.  */
12470
12471       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
12472         complaint (&symfile_complaints,
12473                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
12474                      "- DIE at 0x%x [in module %s]"),
12475                    part_die->offset.sect_off, objfile->name);
12476
12477       /* If we're at the second level, and we're an enumerator, and
12478          our parent has no specification (meaning possibly lives in a
12479          namespace elsewhere), then we can add the partial symbol now
12480          instead of queueing it.  */
12481       if (part_die->tag == DW_TAG_enumerator
12482           && parent_die != NULL
12483           && parent_die->die_parent == NULL
12484           && parent_die->tag == DW_TAG_enumeration_type
12485           && parent_die->has_specification == 0)
12486         {
12487           if (part_die->name == NULL)
12488             complaint (&symfile_complaints,
12489                        _("malformed enumerator DIE ignored"));
12490           else if (building_psymtab)
12491             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
12492                                  VAR_DOMAIN, LOC_CONST,
12493                                  (cu->language == language_cplus
12494                                   || cu->language == language_java)
12495                                  ? &objfile->global_psymbols
12496                                  : &objfile->static_psymbols,
12497                                  0, (CORE_ADDR) 0, cu->language, objfile);
12498
12499           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
12500           continue;
12501         }
12502
12503       /* We'll save this DIE so link it in.  */
12504       part_die->die_parent = parent_die;
12505       part_die->die_sibling = NULL;
12506       part_die->die_child = NULL;
12507
12508       if (last_die && last_die == parent_die)
12509         last_die->die_child = part_die;
12510       else if (last_die)
12511         last_die->die_sibling = part_die;
12512
12513       last_die = part_die;
12514
12515       if (first_die == NULL)
12516         first_die = part_die;
12517
12518       /* Maybe add the DIE to the hash table.  Not all DIEs that we
12519          find interesting need to be in the hash table, because we
12520          also have the parent/sibling/child chains; only those that we
12521          might refer to by offset later during partial symbol reading.
12522
12523          For now this means things that might have be the target of a
12524          DW_AT_specification, DW_AT_abstract_origin, or
12525          DW_AT_extension.  DW_AT_extension will refer only to
12526          namespaces; DW_AT_abstract_origin refers to functions (and
12527          many things under the function DIE, but we do not recurse
12528          into function DIEs during partial symbol reading) and
12529          possibly variables as well; DW_AT_specification refers to
12530          declarations.  Declarations ought to have the DW_AT_declaration
12531          flag.  It happens that GCC forgets to put it in sometimes, but
12532          only for functions, not for types.
12533
12534          Adding more things than necessary to the hash table is harmless
12535          except for the performance cost.  Adding too few will result in
12536          wasted time in find_partial_die, when we reread the compilation
12537          unit with load_all_dies set.  */
12538
12539       if (load_all
12540           || abbrev->tag == DW_TAG_constant
12541           || abbrev->tag == DW_TAG_subprogram
12542           || abbrev->tag == DW_TAG_variable
12543           || abbrev->tag == DW_TAG_namespace
12544           || part_die->is_declaration)
12545         {
12546           void **slot;
12547
12548           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
12549                                            part_die->offset.sect_off, INSERT);
12550           *slot = part_die;
12551         }
12552
12553       part_die = obstack_alloc (&cu->comp_unit_obstack,
12554                                 sizeof (struct partial_die_info));
12555
12556       /* For some DIEs we want to follow their children (if any).  For C
12557          we have no reason to follow the children of structures; for other
12558          languages we have to, so that we can get at method physnames
12559          to infer fully qualified class names, for DW_AT_specification,
12560          and for C++ template arguments.  For C++, we also look one level
12561          inside functions to find template arguments (if the name of the
12562          function does not already contain the template arguments).
12563
12564          For Ada, we need to scan the children of subprograms and lexical
12565          blocks as well because Ada allows the definition of nested
12566          entities that could be interesting for the debugger, such as
12567          nested subprograms for instance.  */
12568       if (last_die->has_children
12569           && (load_all
12570               || last_die->tag == DW_TAG_namespace
12571               || last_die->tag == DW_TAG_module
12572               || last_die->tag == DW_TAG_enumeration_type
12573               || (cu->language == language_cplus
12574                   && last_die->tag == DW_TAG_subprogram
12575                   && (last_die->name == NULL
12576                       || strchr (last_die->name, '<') == NULL))
12577               || (cu->language != language_c
12578                   && (last_die->tag == DW_TAG_class_type
12579                       || last_die->tag == DW_TAG_interface_type
12580                       || last_die->tag == DW_TAG_structure_type
12581                       || last_die->tag == DW_TAG_union_type))
12582               || (cu->language == language_ada
12583                   && (last_die->tag == DW_TAG_subprogram
12584                       || last_die->tag == DW_TAG_lexical_block))))
12585         {
12586           nesting_level++;
12587           parent_die = last_die;
12588           continue;
12589         }
12590
12591       /* Otherwise we skip to the next sibling, if any.  */
12592       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
12593
12594       /* Back to the top, do it again.  */
12595     }
12596 }
12597
12598 /* Read a minimal amount of information into the minimal die structure.  */
12599
12600 static gdb_byte *
12601 read_partial_die (const struct die_reader_specs *reader,
12602                   struct partial_die_info *part_die,
12603                   struct abbrev_info *abbrev, unsigned int abbrev_len,
12604                   gdb_byte *info_ptr)
12605 {
12606   struct dwarf2_cu *cu = reader->cu;
12607   struct objfile *objfile = cu->objfile;
12608   gdb_byte *buffer = reader->buffer;
12609   unsigned int i;
12610   struct attribute attr;
12611   int has_low_pc_attr = 0;
12612   int has_high_pc_attr = 0;
12613   int high_pc_relative = 0;
12614
12615   memset (part_die, 0, sizeof (struct partial_die_info));
12616
12617   part_die->offset.sect_off = info_ptr - buffer;
12618
12619   info_ptr += abbrev_len;
12620
12621   if (abbrev == NULL)
12622     return info_ptr;
12623
12624   part_die->tag = abbrev->tag;
12625   part_die->has_children = abbrev->has_children;
12626
12627   for (i = 0; i < abbrev->num_attrs; ++i)
12628     {
12629       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
12630
12631       /* Store the data if it is of an attribute we want to keep in a
12632          partial symbol table.  */
12633       switch (attr.name)
12634         {
12635         case DW_AT_name:
12636           switch (part_die->tag)
12637             {
12638             case DW_TAG_compile_unit:
12639             case DW_TAG_partial_unit:
12640             case DW_TAG_type_unit:
12641               /* Compilation units have a DW_AT_name that is a filename, not
12642                  a source language identifier.  */
12643             case DW_TAG_enumeration_type:
12644             case DW_TAG_enumerator:
12645               /* These tags always have simple identifiers already; no need
12646                  to canonicalize them.  */
12647               part_die->name = DW_STRING (&attr);
12648               break;
12649             default:
12650               part_die->name
12651                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
12652                                             &objfile->objfile_obstack);
12653               break;
12654             }
12655           break;
12656         case DW_AT_linkage_name:
12657         case DW_AT_MIPS_linkage_name:
12658           /* Note that both forms of linkage name might appear.  We
12659              assume they will be the same, and we only store the last
12660              one we see.  */
12661           if (cu->language == language_ada)
12662             part_die->name = DW_STRING (&attr);
12663           part_die->linkage_name = DW_STRING (&attr);
12664           break;
12665         case DW_AT_low_pc:
12666           has_low_pc_attr = 1;
12667           part_die->lowpc = DW_ADDR (&attr);
12668           break;
12669         case DW_AT_high_pc:
12670           has_high_pc_attr = 1;
12671           if (attr.form == DW_FORM_addr
12672               || attr.form == DW_FORM_GNU_addr_index)
12673             part_die->highpc = DW_ADDR (&attr);
12674           else
12675             {
12676               high_pc_relative = 1;
12677               part_die->highpc = DW_UNSND (&attr);
12678             }
12679           break;
12680         case DW_AT_location:
12681           /* Support the .debug_loc offsets.  */
12682           if (attr_form_is_block (&attr))
12683             {
12684                part_die->d.locdesc = DW_BLOCK (&attr);
12685             }
12686           else if (attr_form_is_section_offset (&attr))
12687             {
12688               dwarf2_complex_location_expr_complaint ();
12689             }
12690           else
12691             {
12692               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12693                                                      "partial symbol information");
12694             }
12695           break;
12696         case DW_AT_external:
12697           part_die->is_external = DW_UNSND (&attr);
12698           break;
12699         case DW_AT_declaration:
12700           part_die->is_declaration = DW_UNSND (&attr);
12701           break;
12702         case DW_AT_type:
12703           part_die->has_type = 1;
12704           break;
12705         case DW_AT_abstract_origin:
12706         case DW_AT_specification:
12707         case DW_AT_extension:
12708           part_die->has_specification = 1;
12709           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
12710           part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12711                                    || cu->per_cu->is_dwz);
12712           break;
12713         case DW_AT_sibling:
12714           /* Ignore absolute siblings, they might point outside of
12715              the current compile unit.  */
12716           if (attr.form == DW_FORM_ref_addr)
12717             complaint (&symfile_complaints,
12718                        _("ignoring absolute DW_AT_sibling"));
12719           else
12720             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
12721           break;
12722         case DW_AT_byte_size:
12723           part_die->has_byte_size = 1;
12724           break;
12725         case DW_AT_calling_convention:
12726           /* DWARF doesn't provide a way to identify a program's source-level
12727              entry point.  DW_AT_calling_convention attributes are only meant
12728              to describe functions' calling conventions.
12729
12730              However, because it's a necessary piece of information in
12731              Fortran, and because DW_CC_program is the only piece of debugging
12732              information whose definition refers to a 'main program' at all,
12733              several compilers have begun marking Fortran main programs with
12734              DW_CC_program --- even when those functions use the standard
12735              calling conventions.
12736
12737              So until DWARF specifies a way to provide this information and
12738              compilers pick up the new representation, we'll support this
12739              practice.  */
12740           if (DW_UNSND (&attr) == DW_CC_program
12741               && cu->language == language_fortran)
12742             {
12743               set_main_name (part_die->name);
12744
12745               /* As this DIE has a static linkage the name would be difficult
12746                  to look up later.  */
12747               language_of_main = language_fortran;
12748             }
12749           break;
12750         case DW_AT_inline:
12751           if (DW_UNSND (&attr) == DW_INL_inlined
12752               || DW_UNSND (&attr) == DW_INL_declared_inlined)
12753             part_die->may_be_inlined = 1;
12754           break;
12755
12756         case DW_AT_import:
12757           if (part_die->tag == DW_TAG_imported_unit)
12758             {
12759               part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
12760               part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
12761                                   || cu->per_cu->is_dwz);
12762             }
12763           break;
12764
12765         default:
12766           break;
12767         }
12768     }
12769
12770   if (high_pc_relative)
12771     part_die->highpc += part_die->lowpc;
12772
12773   if (has_low_pc_attr && has_high_pc_attr)
12774     {
12775       /* When using the GNU linker, .gnu.linkonce. sections are used to
12776          eliminate duplicate copies of functions and vtables and such.
12777          The linker will arbitrarily choose one and discard the others.
12778          The AT_*_pc values for such functions refer to local labels in
12779          these sections.  If the section from that file was discarded, the
12780          labels are not in the output, so the relocs get a value of 0.
12781          If this is a discarded function, mark the pc bounds as invalid,
12782          so that GDB will ignore it.  */
12783       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
12784         {
12785           struct gdbarch *gdbarch = get_objfile_arch (objfile);
12786
12787           complaint (&symfile_complaints,
12788                      _("DW_AT_low_pc %s is zero "
12789                        "for DIE at 0x%x [in module %s]"),
12790                      paddress (gdbarch, part_die->lowpc),
12791                      part_die->offset.sect_off, objfile->name);
12792         }
12793       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
12794       else if (part_die->lowpc >= part_die->highpc)
12795         {
12796           struct gdbarch *gdbarch = get_objfile_arch (objfile);
12797
12798           complaint (&symfile_complaints,
12799                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
12800                        "for DIE at 0x%x [in module %s]"),
12801                      paddress (gdbarch, part_die->lowpc),
12802                      paddress (gdbarch, part_die->highpc),
12803                      part_die->offset.sect_off, objfile->name);
12804         }
12805       else
12806         part_die->has_pc_info = 1;
12807     }
12808
12809   return info_ptr;
12810 }
12811
12812 /* Find a cached partial DIE at OFFSET in CU.  */
12813
12814 static struct partial_die_info *
12815 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
12816 {
12817   struct partial_die_info *lookup_die = NULL;
12818   struct partial_die_info part_die;
12819
12820   part_die.offset = offset;
12821   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
12822                                     offset.sect_off);
12823
12824   return lookup_die;
12825 }
12826
12827 /* Find a partial DIE at OFFSET, which may or may not be in CU,
12828    except in the case of .debug_types DIEs which do not reference
12829    outside their CU (they do however referencing other types via
12830    DW_FORM_ref_sig8).  */
12831
12832 static struct partial_die_info *
12833 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
12834 {
12835   struct objfile *objfile = cu->objfile;
12836   struct dwarf2_per_cu_data *per_cu = NULL;
12837   struct partial_die_info *pd = NULL;
12838
12839   if (offset_in_dwz == cu->per_cu->is_dwz
12840       && offset_in_cu_p (&cu->header, offset))
12841     {
12842       pd = find_partial_die_in_comp_unit (offset, cu);
12843       if (pd != NULL)
12844         return pd;
12845       /* We missed recording what we needed.
12846          Load all dies and try again.  */
12847       per_cu = cu->per_cu;
12848     }
12849   else
12850     {
12851       /* TUs don't reference other CUs/TUs (except via type signatures).  */
12852       if (cu->per_cu->is_debug_types)
12853         {
12854           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
12855                    " external reference to offset 0x%lx [in module %s].\n"),
12856                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
12857                  bfd_get_filename (objfile->obfd));
12858         }
12859       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
12860                                                  objfile);
12861
12862       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
12863         load_partial_comp_unit (per_cu);
12864
12865       per_cu->cu->last_used = 0;
12866       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12867     }
12868
12869   /* If we didn't find it, and not all dies have been loaded,
12870      load them all and try again.  */
12871
12872   if (pd == NULL && per_cu->load_all_dies == 0)
12873     {
12874       per_cu->load_all_dies = 1;
12875
12876       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
12877          THIS_CU->cu may already be in use.  So we can't just free it and
12878          replace its DIEs with the ones we read in.  Instead, we leave those
12879          DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
12880          and clobber THIS_CU->cu->partial_dies with the hash table for the new
12881          set.  */
12882       load_partial_comp_unit (per_cu);
12883
12884       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
12885     }
12886
12887   if (pd == NULL)
12888     internal_error (__FILE__, __LINE__,
12889                     _("could not find partial DIE 0x%x "
12890                       "in cache [from module %s]\n"),
12891                     offset.sect_off, bfd_get_filename (objfile->obfd));
12892   return pd;
12893 }
12894
12895 /* See if we can figure out if the class lives in a namespace.  We do
12896    this by looking for a member function; its demangled name will
12897    contain namespace info, if there is any.  */
12898
12899 static void
12900 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
12901                                   struct dwarf2_cu *cu)
12902 {
12903   /* NOTE: carlton/2003-10-07: Getting the info this way changes
12904      what template types look like, because the demangler
12905      frequently doesn't give the same name as the debug info.  We
12906      could fix this by only using the demangled name to get the
12907      prefix (but see comment in read_structure_type).  */
12908
12909   struct partial_die_info *real_pdi;
12910   struct partial_die_info *child_pdi;
12911
12912   /* If this DIE (this DIE's specification, if any) has a parent, then
12913      we should not do this.  We'll prepend the parent's fully qualified
12914      name when we create the partial symbol.  */
12915
12916   real_pdi = struct_pdi;
12917   while (real_pdi->has_specification)
12918     real_pdi = find_partial_die (real_pdi->spec_offset,
12919                                  real_pdi->spec_is_dwz, cu);
12920
12921   if (real_pdi->die_parent != NULL)
12922     return;
12923
12924   for (child_pdi = struct_pdi->die_child;
12925        child_pdi != NULL;
12926        child_pdi = child_pdi->die_sibling)
12927     {
12928       if (child_pdi->tag == DW_TAG_subprogram
12929           && child_pdi->linkage_name != NULL)
12930         {
12931           char *actual_class_name
12932             = language_class_name_from_physname (cu->language_defn,
12933                                                  child_pdi->linkage_name);
12934           if (actual_class_name != NULL)
12935             {
12936               struct_pdi->name
12937                 = obsavestring (actual_class_name,
12938                                 strlen (actual_class_name),
12939                                 &cu->objfile->objfile_obstack);
12940               xfree (actual_class_name);
12941             }
12942           break;
12943         }
12944     }
12945 }
12946
12947 /* Adjust PART_DIE before generating a symbol for it.  This function
12948    may set the is_external flag or change the DIE's name.  */
12949
12950 static void
12951 fixup_partial_die (struct partial_die_info *part_die,
12952                    struct dwarf2_cu *cu)
12953 {
12954   /* Once we've fixed up a die, there's no point in doing so again.
12955      This also avoids a memory leak if we were to call
12956      guess_partial_die_structure_name multiple times.  */
12957   if (part_die->fixup_called)
12958     return;
12959
12960   /* If we found a reference attribute and the DIE has no name, try
12961      to find a name in the referred to DIE.  */
12962
12963   if (part_die->name == NULL && part_die->has_specification)
12964     {
12965       struct partial_die_info *spec_die;
12966
12967       spec_die = find_partial_die (part_die->spec_offset,
12968                                    part_die->spec_is_dwz, cu);
12969
12970       fixup_partial_die (spec_die, cu);
12971
12972       if (spec_die->name)
12973         {
12974           part_die->name = spec_die->name;
12975
12976           /* Copy DW_AT_external attribute if it is set.  */
12977           if (spec_die->is_external)
12978             part_die->is_external = spec_die->is_external;
12979         }
12980     }
12981
12982   /* Set default names for some unnamed DIEs.  */
12983
12984   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
12985     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
12986
12987   /* If there is no parent die to provide a namespace, and there are
12988      children, see if we can determine the namespace from their linkage
12989      name.  */
12990   if (cu->language == language_cplus
12991       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12992       && part_die->die_parent == NULL
12993       && part_die->has_children
12994       && (part_die->tag == DW_TAG_class_type
12995           || part_die->tag == DW_TAG_structure_type
12996           || part_die->tag == DW_TAG_union_type))
12997     guess_partial_die_structure_name (part_die, cu);
12998
12999   /* GCC might emit a nameless struct or union that has a linkage
13000      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
13001   if (part_die->name == NULL
13002       && (part_die->tag == DW_TAG_class_type
13003           || part_die->tag == DW_TAG_interface_type
13004           || part_die->tag == DW_TAG_structure_type
13005           || part_die->tag == DW_TAG_union_type)
13006       && part_die->linkage_name != NULL)
13007     {
13008       char *demangled;
13009
13010       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
13011       if (demangled)
13012         {
13013           const char *base;
13014
13015           /* Strip any leading namespaces/classes, keep only the base name.
13016              DW_AT_name for named DIEs does not contain the prefixes.  */
13017           base = strrchr (demangled, ':');
13018           if (base && base > demangled && base[-1] == ':')
13019             base++;
13020           else
13021             base = demangled;
13022
13023           part_die->name = obsavestring (base, strlen (base),
13024                                          &cu->objfile->objfile_obstack);
13025           xfree (demangled);
13026         }
13027     }
13028
13029   part_die->fixup_called = 1;
13030 }
13031
13032 /* Read an attribute value described by an attribute form.  */
13033
13034 static gdb_byte *
13035 read_attribute_value (const struct die_reader_specs *reader,
13036                       struct attribute *attr, unsigned form,
13037                       gdb_byte *info_ptr)
13038 {
13039   struct dwarf2_cu *cu = reader->cu;
13040   bfd *abfd = reader->abfd;
13041   struct comp_unit_head *cu_header = &cu->header;
13042   unsigned int bytes_read;
13043   struct dwarf_block *blk;
13044
13045   attr->form = form;
13046   switch (form)
13047     {
13048     case DW_FORM_ref_addr:
13049       if (cu->header.version == 2)
13050         DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13051       else
13052         DW_UNSND (attr) = read_offset (abfd, info_ptr,
13053                                        &cu->header, &bytes_read);
13054       info_ptr += bytes_read;
13055       break;
13056     case DW_FORM_GNU_ref_alt:
13057       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13058       info_ptr += bytes_read;
13059       break;
13060     case DW_FORM_addr:
13061       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
13062       info_ptr += bytes_read;
13063       break;
13064     case DW_FORM_block2:
13065       blk = dwarf_alloc_block (cu);
13066       blk->size = read_2_bytes (abfd, info_ptr);
13067       info_ptr += 2;
13068       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13069       info_ptr += blk->size;
13070       DW_BLOCK (attr) = blk;
13071       break;
13072     case DW_FORM_block4:
13073       blk = dwarf_alloc_block (cu);
13074       blk->size = read_4_bytes (abfd, info_ptr);
13075       info_ptr += 4;
13076       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13077       info_ptr += blk->size;
13078       DW_BLOCK (attr) = blk;
13079       break;
13080     case DW_FORM_data2:
13081       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
13082       info_ptr += 2;
13083       break;
13084     case DW_FORM_data4:
13085       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
13086       info_ptr += 4;
13087       break;
13088     case DW_FORM_data8:
13089       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
13090       info_ptr += 8;
13091       break;
13092     case DW_FORM_sec_offset:
13093       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
13094       info_ptr += bytes_read;
13095       break;
13096     case DW_FORM_string:
13097       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
13098       DW_STRING_IS_CANONICAL (attr) = 0;
13099       info_ptr += bytes_read;
13100       break;
13101     case DW_FORM_strp:
13102       if (!cu->per_cu->is_dwz)
13103         {
13104           DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
13105                                                    &bytes_read);
13106           DW_STRING_IS_CANONICAL (attr) = 0;
13107           info_ptr += bytes_read;
13108           break;
13109         }
13110       /* FALLTHROUGH */
13111     case DW_FORM_GNU_strp_alt:
13112       {
13113         struct dwz_file *dwz = dwarf2_get_dwz_file ();
13114         LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
13115                                           &bytes_read);
13116
13117         DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
13118         DW_STRING_IS_CANONICAL (attr) = 0;
13119         info_ptr += bytes_read;
13120       }
13121       break;
13122     case DW_FORM_exprloc:
13123     case DW_FORM_block:
13124       blk = dwarf_alloc_block (cu);
13125       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13126       info_ptr += bytes_read;
13127       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13128       info_ptr += blk->size;
13129       DW_BLOCK (attr) = blk;
13130       break;
13131     case DW_FORM_block1:
13132       blk = dwarf_alloc_block (cu);
13133       blk->size = read_1_byte (abfd, info_ptr);
13134       info_ptr += 1;
13135       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
13136       info_ptr += blk->size;
13137       DW_BLOCK (attr) = blk;
13138       break;
13139     case DW_FORM_data1:
13140       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13141       info_ptr += 1;
13142       break;
13143     case DW_FORM_flag:
13144       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
13145       info_ptr += 1;
13146       break;
13147     case DW_FORM_flag_present:
13148       DW_UNSND (attr) = 1;
13149       break;
13150     case DW_FORM_sdata:
13151       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
13152       info_ptr += bytes_read;
13153       break;
13154     case DW_FORM_udata:
13155       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13156       info_ptr += bytes_read;
13157       break;
13158     case DW_FORM_ref1:
13159       DW_UNSND (attr) = (cu->header.offset.sect_off
13160                          + read_1_byte (abfd, info_ptr));
13161       info_ptr += 1;
13162       break;
13163     case DW_FORM_ref2:
13164       DW_UNSND (attr) = (cu->header.offset.sect_off
13165                          + read_2_bytes (abfd, info_ptr));
13166       info_ptr += 2;
13167       break;
13168     case DW_FORM_ref4:
13169       DW_UNSND (attr) = (cu->header.offset.sect_off
13170                          + read_4_bytes (abfd, info_ptr));
13171       info_ptr += 4;
13172       break;
13173     case DW_FORM_ref8:
13174       DW_UNSND (attr) = (cu->header.offset.sect_off
13175                          + read_8_bytes (abfd, info_ptr));
13176       info_ptr += 8;
13177       break;
13178     case DW_FORM_ref_sig8:
13179       /* Convert the signature to something we can record in DW_UNSND
13180          for later lookup.
13181          NOTE: This is NULL if the type wasn't found.  */
13182       DW_SIGNATURED_TYPE (attr) =
13183         lookup_signatured_type (read_8_bytes (abfd, info_ptr));
13184       info_ptr += 8;
13185       break;
13186     case DW_FORM_ref_udata:
13187       DW_UNSND (attr) = (cu->header.offset.sect_off
13188                          + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
13189       info_ptr += bytes_read;
13190       break;
13191     case DW_FORM_indirect:
13192       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13193       info_ptr += bytes_read;
13194       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
13195       break;
13196     case DW_FORM_GNU_addr_index:
13197       if (reader->dwo_file == NULL)
13198         {
13199           /* For now flag a hard error.
13200              Later we can turn this into a complaint.  */
13201           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13202                  dwarf_form_name (form),
13203                  bfd_get_filename (abfd));
13204         }
13205       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
13206       info_ptr += bytes_read;
13207       break;
13208     case DW_FORM_GNU_str_index:
13209       if (reader->dwo_file == NULL)
13210         {
13211           /* For now flag a hard error.
13212              Later we can turn this into a complaint if warranted.  */
13213           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
13214                  dwarf_form_name (form),
13215                  bfd_get_filename (abfd));
13216         }
13217       {
13218         ULONGEST str_index =
13219           read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13220
13221         DW_STRING (attr) = read_str_index (reader, cu, str_index);
13222         DW_STRING_IS_CANONICAL (attr) = 0;
13223         info_ptr += bytes_read;
13224       }
13225       break;
13226     default:
13227       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
13228              dwarf_form_name (form),
13229              bfd_get_filename (abfd));
13230     }
13231
13232   /* Super hack.  */
13233   if (cu->per_cu->is_dwz && is_ref_attr (attr))
13234     attr->form = DW_FORM_GNU_ref_alt;
13235
13236   /* We have seen instances where the compiler tried to emit a byte
13237      size attribute of -1 which ended up being encoded as an unsigned
13238      0xffffffff.  Although 0xffffffff is technically a valid size value,
13239      an object of this size seems pretty unlikely so we can relatively
13240      safely treat these cases as if the size attribute was invalid and
13241      treat them as zero by default.  */
13242   if (attr->name == DW_AT_byte_size
13243       && form == DW_FORM_data4
13244       && DW_UNSND (attr) >= 0xffffffff)
13245     {
13246       complaint
13247         (&symfile_complaints,
13248          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
13249          hex_string (DW_UNSND (attr)));
13250       DW_UNSND (attr) = 0;
13251     }
13252
13253   return info_ptr;
13254 }
13255
13256 /* Read an attribute described by an abbreviated attribute.  */
13257
13258 static gdb_byte *
13259 read_attribute (const struct die_reader_specs *reader,
13260                 struct attribute *attr, struct attr_abbrev *abbrev,
13261                 gdb_byte *info_ptr)
13262 {
13263   attr->name = abbrev->name;
13264   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
13265 }
13266
13267 /* Read dwarf information from a buffer.  */
13268
13269 static unsigned int
13270 read_1_byte (bfd *abfd, gdb_byte *buf)
13271 {
13272   return bfd_get_8 (abfd, buf);
13273 }
13274
13275 static int
13276 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
13277 {
13278   return bfd_get_signed_8 (abfd, buf);
13279 }
13280
13281 static unsigned int
13282 read_2_bytes (bfd *abfd, gdb_byte *buf)
13283 {
13284   return bfd_get_16 (abfd, buf);
13285 }
13286
13287 static int
13288 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
13289 {
13290   return bfd_get_signed_16 (abfd, buf);
13291 }
13292
13293 static unsigned int
13294 read_4_bytes (bfd *abfd, gdb_byte *buf)
13295 {
13296   return bfd_get_32 (abfd, buf);
13297 }
13298
13299 static int
13300 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
13301 {
13302   return bfd_get_signed_32 (abfd, buf);
13303 }
13304
13305 static ULONGEST
13306 read_8_bytes (bfd *abfd, gdb_byte *buf)
13307 {
13308   return bfd_get_64 (abfd, buf);
13309 }
13310
13311 static CORE_ADDR
13312 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
13313               unsigned int *bytes_read)
13314 {
13315   struct comp_unit_head *cu_header = &cu->header;
13316   CORE_ADDR retval = 0;
13317
13318   if (cu_header->signed_addr_p)
13319     {
13320       switch (cu_header->addr_size)
13321         {
13322         case 2:
13323           retval = bfd_get_signed_16 (abfd, buf);
13324           break;
13325         case 4:
13326           retval = bfd_get_signed_32 (abfd, buf);
13327           break;
13328         case 8:
13329           retval = bfd_get_signed_64 (abfd, buf);
13330           break;
13331         default:
13332           internal_error (__FILE__, __LINE__,
13333                           _("read_address: bad switch, signed [in module %s]"),
13334                           bfd_get_filename (abfd));
13335         }
13336     }
13337   else
13338     {
13339       switch (cu_header->addr_size)
13340         {
13341         case 2:
13342           retval = bfd_get_16 (abfd, buf);
13343           break;
13344         case 4:
13345           retval = bfd_get_32 (abfd, buf);
13346           break;
13347         case 8:
13348           retval = bfd_get_64 (abfd, buf);
13349           break;
13350         default:
13351           internal_error (__FILE__, __LINE__,
13352                           _("read_address: bad switch, "
13353                             "unsigned [in module %s]"),
13354                           bfd_get_filename (abfd));
13355         }
13356     }
13357
13358   *bytes_read = cu_header->addr_size;
13359   return retval;
13360 }
13361
13362 /* Read the initial length from a section.  The (draft) DWARF 3
13363    specification allows the initial length to take up either 4 bytes
13364    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
13365    bytes describe the length and all offsets will be 8 bytes in length
13366    instead of 4.
13367
13368    An older, non-standard 64-bit format is also handled by this
13369    function.  The older format in question stores the initial length
13370    as an 8-byte quantity without an escape value.  Lengths greater
13371    than 2^32 aren't very common which means that the initial 4 bytes
13372    is almost always zero.  Since a length value of zero doesn't make
13373    sense for the 32-bit format, this initial zero can be considered to
13374    be an escape value which indicates the presence of the older 64-bit
13375    format.  As written, the code can't detect (old format) lengths
13376    greater than 4GB.  If it becomes necessary to handle lengths
13377    somewhat larger than 4GB, we could allow other small values (such
13378    as the non-sensical values of 1, 2, and 3) to also be used as
13379    escape values indicating the presence of the old format.
13380
13381    The value returned via bytes_read should be used to increment the
13382    relevant pointer after calling read_initial_length().
13383
13384    [ Note:  read_initial_length() and read_offset() are based on the
13385      document entitled "DWARF Debugging Information Format", revision
13386      3, draft 8, dated November 19, 2001.  This document was obtained
13387      from:
13388
13389         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
13390
13391      This document is only a draft and is subject to change.  (So beware.)
13392
13393      Details regarding the older, non-standard 64-bit format were
13394      determined empirically by examining 64-bit ELF files produced by
13395      the SGI toolchain on an IRIX 6.5 machine.
13396
13397      - Kevin, July 16, 2002
13398    ] */
13399
13400 static LONGEST
13401 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
13402 {
13403   LONGEST length = bfd_get_32 (abfd, buf);
13404
13405   if (length == 0xffffffff)
13406     {
13407       length = bfd_get_64 (abfd, buf + 4);
13408       *bytes_read = 12;
13409     }
13410   else if (length == 0)
13411     {
13412       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
13413       length = bfd_get_64 (abfd, buf);
13414       *bytes_read = 8;
13415     }
13416   else
13417     {
13418       *bytes_read = 4;
13419     }
13420
13421   return length;
13422 }
13423
13424 /* Cover function for read_initial_length.
13425    Returns the length of the object at BUF, and stores the size of the
13426    initial length in *BYTES_READ and stores the size that offsets will be in
13427    *OFFSET_SIZE.
13428    If the initial length size is not equivalent to that specified in
13429    CU_HEADER then issue a complaint.
13430    This is useful when reading non-comp-unit headers.  */
13431
13432 static LONGEST
13433 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
13434                                         const struct comp_unit_head *cu_header,
13435                                         unsigned int *bytes_read,
13436                                         unsigned int *offset_size)
13437 {
13438   LONGEST length = read_initial_length (abfd, buf, bytes_read);
13439
13440   gdb_assert (cu_header->initial_length_size == 4
13441               || cu_header->initial_length_size == 8
13442               || cu_header->initial_length_size == 12);
13443
13444   if (cu_header->initial_length_size != *bytes_read)
13445     complaint (&symfile_complaints,
13446                _("intermixed 32-bit and 64-bit DWARF sections"));
13447
13448   *offset_size = (*bytes_read == 4) ? 4 : 8;
13449   return length;
13450 }
13451
13452 /* Read an offset from the data stream.  The size of the offset is
13453    given by cu_header->offset_size.  */
13454
13455 static LONGEST
13456 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
13457              unsigned int *bytes_read)
13458 {
13459   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
13460
13461   *bytes_read = cu_header->offset_size;
13462   return offset;
13463 }
13464
13465 /* Read an offset from the data stream.  */
13466
13467 static LONGEST
13468 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
13469 {
13470   LONGEST retval = 0;
13471
13472   switch (offset_size)
13473     {
13474     case 4:
13475       retval = bfd_get_32 (abfd, buf);
13476       break;
13477     case 8:
13478       retval = bfd_get_64 (abfd, buf);
13479       break;
13480     default:
13481       internal_error (__FILE__, __LINE__,
13482                       _("read_offset_1: bad switch [in module %s]"),
13483                       bfd_get_filename (abfd));
13484     }
13485
13486   return retval;
13487 }
13488
13489 static gdb_byte *
13490 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
13491 {
13492   /* If the size of a host char is 8 bits, we can return a pointer
13493      to the buffer, otherwise we have to copy the data to a buffer
13494      allocated on the temporary obstack.  */
13495   gdb_assert (HOST_CHAR_BIT == 8);
13496   return buf;
13497 }
13498
13499 static char *
13500 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13501 {
13502   /* If the size of a host char is 8 bits, we can return a pointer
13503      to the string, otherwise we have to copy the string to a buffer
13504      allocated on the temporary obstack.  */
13505   gdb_assert (HOST_CHAR_BIT == 8);
13506   if (*buf == '\0')
13507     {
13508       *bytes_read_ptr = 1;
13509       return NULL;
13510     }
13511   *bytes_read_ptr = strlen ((char *) buf) + 1;
13512   return (char *) buf;
13513 }
13514
13515 static char *
13516 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
13517 {
13518   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
13519   if (dwarf2_per_objfile->str.buffer == NULL)
13520     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
13521            bfd_get_filename (abfd));
13522   if (str_offset >= dwarf2_per_objfile->str.size)
13523     error (_("DW_FORM_strp pointing outside of "
13524              ".debug_str section [in module %s]"),
13525            bfd_get_filename (abfd));
13526   gdb_assert (HOST_CHAR_BIT == 8);
13527   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
13528     return NULL;
13529   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
13530 }
13531
13532 /* Read a string at offset STR_OFFSET in the .debug_str section from
13533    the .dwz file DWZ.  Throw an error if the offset is too large.  If
13534    the string consists of a single NUL byte, return NULL; otherwise
13535    return a pointer to the string.  */
13536
13537 static char *
13538 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
13539 {
13540   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
13541
13542   if (dwz->str.buffer == NULL)
13543     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
13544              "section [in module %s]"),
13545            bfd_get_filename (dwz->dwz_bfd));
13546   if (str_offset >= dwz->str.size)
13547     error (_("DW_FORM_GNU_strp_alt pointing outside of "
13548              ".debug_str section [in module %s]"),
13549            bfd_get_filename (dwz->dwz_bfd));
13550   gdb_assert (HOST_CHAR_BIT == 8);
13551   if (dwz->str.buffer[str_offset] == '\0')
13552     return NULL;
13553   return (char *) (dwz->str.buffer + str_offset);
13554 }
13555
13556 static char *
13557 read_indirect_string (bfd *abfd, gdb_byte *buf,
13558                       const struct comp_unit_head *cu_header,
13559                       unsigned int *bytes_read_ptr)
13560 {
13561   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
13562
13563   return read_indirect_string_at_offset (abfd, str_offset);
13564 }
13565
13566 static ULONGEST
13567 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13568 {
13569   ULONGEST result;
13570   unsigned int num_read;
13571   int i, shift;
13572   unsigned char byte;
13573
13574   result = 0;
13575   shift = 0;
13576   num_read = 0;
13577   i = 0;
13578   while (1)
13579     {
13580       byte = bfd_get_8 (abfd, buf);
13581       buf++;
13582       num_read++;
13583       result |= ((ULONGEST) (byte & 127) << shift);
13584       if ((byte & 128) == 0)
13585         {
13586           break;
13587         }
13588       shift += 7;
13589     }
13590   *bytes_read_ptr = num_read;
13591   return result;
13592 }
13593
13594 static LONGEST
13595 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
13596 {
13597   LONGEST result;
13598   int i, shift, num_read;
13599   unsigned char byte;
13600
13601   result = 0;
13602   shift = 0;
13603   num_read = 0;
13604   i = 0;
13605   while (1)
13606     {
13607       byte = bfd_get_8 (abfd, buf);
13608       buf++;
13609       num_read++;
13610       result |= ((LONGEST) (byte & 127) << shift);
13611       shift += 7;
13612       if ((byte & 128) == 0)
13613         {
13614           break;
13615         }
13616     }
13617   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
13618     result |= -(((LONGEST) 1) << shift);
13619   *bytes_read_ptr = num_read;
13620   return result;
13621 }
13622
13623 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
13624    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
13625    ADDR_SIZE is the size of addresses from the CU header.  */
13626
13627 static CORE_ADDR
13628 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
13629 {
13630   struct objfile *objfile = dwarf2_per_objfile->objfile;
13631   bfd *abfd = objfile->obfd;
13632   const gdb_byte *info_ptr;
13633
13634   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
13635   if (dwarf2_per_objfile->addr.buffer == NULL)
13636     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
13637            objfile->name);
13638   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
13639     error (_("DW_FORM_addr_index pointing outside of "
13640              ".debug_addr section [in module %s]"),
13641            objfile->name);
13642   info_ptr = (dwarf2_per_objfile->addr.buffer
13643               + addr_base + addr_index * addr_size);
13644   if (addr_size == 4)
13645     return bfd_get_32 (abfd, info_ptr);
13646   else
13647     return bfd_get_64 (abfd, info_ptr);
13648 }
13649
13650 /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */
13651
13652 static CORE_ADDR
13653 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
13654 {
13655   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
13656 }
13657
13658 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
13659
13660 static CORE_ADDR
13661 read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
13662                              unsigned int *bytes_read)
13663 {
13664   bfd *abfd = cu->objfile->obfd;
13665   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
13666
13667   return read_addr_index (cu, addr_index);
13668 }
13669
13670 /* Data structure to pass results from dwarf2_read_addr_index_reader
13671    back to dwarf2_read_addr_index.  */
13672
13673 struct dwarf2_read_addr_index_data
13674 {
13675   ULONGEST addr_base;
13676   int addr_size;
13677 };
13678
13679 /* die_reader_func for dwarf2_read_addr_index.  */
13680
13681 static void
13682 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
13683                                gdb_byte *info_ptr,
13684                                struct die_info *comp_unit_die,
13685                                int has_children,
13686                                void *data)
13687 {
13688   struct dwarf2_cu *cu = reader->cu;
13689   struct dwarf2_read_addr_index_data *aidata =
13690     (struct dwarf2_read_addr_index_data *) data;
13691
13692   aidata->addr_base = cu->addr_base;
13693   aidata->addr_size = cu->header.addr_size;
13694 }
13695
13696 /* Given an index in .debug_addr, fetch the value.
13697    NOTE: This can be called during dwarf expression evaluation,
13698    long after the debug information has been read, and thus per_cu->cu
13699    may no longer exist.  */
13700
13701 CORE_ADDR
13702 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
13703                         unsigned int addr_index)
13704 {
13705   struct objfile *objfile = per_cu->objfile;
13706   struct dwarf2_cu *cu = per_cu->cu;
13707   ULONGEST addr_base;
13708   int addr_size;
13709
13710   /* This is intended to be called from outside this file.  */
13711   dw2_setup (objfile);
13712
13713   /* We need addr_base and addr_size.
13714      If we don't have PER_CU->cu, we have to get it.
13715      Nasty, but the alternative is storing the needed info in PER_CU,
13716      which at this point doesn't seem justified: it's not clear how frequently
13717      it would get used and it would increase the size of every PER_CU.
13718      Entry points like dwarf2_per_cu_addr_size do a similar thing
13719      so we're not in uncharted territory here.
13720      Alas we need to be a bit more complicated as addr_base is contained
13721      in the DIE.
13722
13723      We don't need to read the entire CU(/TU).
13724      We just need the header and top level die.
13725      IWBN to use the aging mechanism to let us lazily later discard the CU.
13726      See however init_cutu_and_read_dies_simple.  */
13727
13728   if (cu != NULL)
13729     {
13730       addr_base = cu->addr_base;
13731       addr_size = cu->header.addr_size;
13732     }
13733   else
13734     {
13735       struct dwarf2_read_addr_index_data aidata;
13736
13737       init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
13738                                       &aidata);
13739       addr_base = aidata.addr_base;
13740       addr_size = aidata.addr_size;
13741     }
13742
13743   return read_addr_index_1 (addr_index, addr_base, addr_size);
13744 }
13745
13746 /* Given a DW_AT_str_index, fetch the string.  */
13747
13748 static char *
13749 read_str_index (const struct die_reader_specs *reader,
13750                 struct dwarf2_cu *cu, ULONGEST str_index)
13751 {
13752   struct objfile *objfile = dwarf2_per_objfile->objfile;
13753   const char *dwo_name = objfile->name;
13754   bfd *abfd = objfile->obfd;
13755   struct dwo_sections *sections = &reader->dwo_file->sections;
13756   gdb_byte *info_ptr;
13757   ULONGEST str_offset;
13758
13759   dwarf2_read_section (objfile, &sections->str);
13760   dwarf2_read_section (objfile, &sections->str_offsets);
13761   if (sections->str.buffer == NULL)
13762     error (_("DW_FORM_str_index used without .debug_str.dwo section"
13763              " in CU at offset 0x%lx [in module %s]"),
13764            (long) cu->header.offset.sect_off, dwo_name);
13765   if (sections->str_offsets.buffer == NULL)
13766     error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
13767              " in CU at offset 0x%lx [in module %s]"),
13768            (long) cu->header.offset.sect_off, dwo_name);
13769   if (str_index * cu->header.offset_size >= sections->str_offsets.size)
13770     error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
13771              " section in CU at offset 0x%lx [in module %s]"),
13772            (long) cu->header.offset.sect_off, dwo_name);
13773   info_ptr = (sections->str_offsets.buffer
13774               + str_index * cu->header.offset_size);
13775   if (cu->header.offset_size == 4)
13776     str_offset = bfd_get_32 (abfd, info_ptr);
13777   else
13778     str_offset = bfd_get_64 (abfd, info_ptr);
13779   if (str_offset >= sections->str.size)
13780     error (_("Offset from DW_FORM_str_index pointing outside of"
13781              " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
13782            (long) cu->header.offset.sect_off, dwo_name);
13783   return (char *) (sections->str.buffer + str_offset);
13784 }
13785
13786 /* Return the length of an LEB128 number in BUF.  */
13787
13788 static int
13789 leb128_size (const gdb_byte *buf)
13790 {
13791   const gdb_byte *begin = buf;
13792   gdb_byte byte;
13793
13794   while (1)
13795     {
13796       byte = *buf++;
13797       if ((byte & 128) == 0)
13798         return buf - begin;
13799     }
13800 }
13801
13802 static void
13803 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
13804 {
13805   switch (lang)
13806     {
13807     case DW_LANG_C89:
13808     case DW_LANG_C99:
13809     case DW_LANG_C:
13810       cu->language = language_c;
13811       break;
13812     case DW_LANG_C_plus_plus:
13813       cu->language = language_cplus;
13814       break;
13815     case DW_LANG_D:
13816       cu->language = language_d;
13817       break;
13818     case DW_LANG_Fortran77:
13819     case DW_LANG_Fortran90:
13820     case DW_LANG_Fortran95:
13821       cu->language = language_fortran;
13822       break;
13823     case DW_LANG_Go:
13824       cu->language = language_go;
13825       break;
13826     case DW_LANG_Mips_Assembler:
13827       cu->language = language_asm;
13828       break;
13829     case DW_LANG_Java:
13830       cu->language = language_java;
13831       break;
13832     case DW_LANG_Ada83:
13833     case DW_LANG_Ada95:
13834       cu->language = language_ada;
13835       break;
13836     case DW_LANG_Modula2:
13837       cu->language = language_m2;
13838       break;
13839     case DW_LANG_Pascal83:
13840       cu->language = language_pascal;
13841       break;
13842     case DW_LANG_ObjC:
13843       cu->language = language_objc;
13844       break;
13845     case DW_LANG_Cobol74:
13846     case DW_LANG_Cobol85:
13847     default:
13848       cu->language = language_minimal;
13849       break;
13850     }
13851   cu->language_defn = language_def (cu->language);
13852 }
13853
13854 /* Return the named attribute or NULL if not there.  */
13855
13856 static struct attribute *
13857 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
13858 {
13859   for (;;)
13860     {
13861       unsigned int i;
13862       struct attribute *spec = NULL;
13863
13864       for (i = 0; i < die->num_attrs; ++i)
13865         {
13866           if (die->attrs[i].name == name)
13867             return &die->attrs[i];
13868           if (die->attrs[i].name == DW_AT_specification
13869               || die->attrs[i].name == DW_AT_abstract_origin)
13870             spec = &die->attrs[i];
13871         }
13872
13873       if (!spec)
13874         break;
13875
13876       die = follow_die_ref (die, spec, &cu);
13877     }
13878
13879   return NULL;
13880 }
13881
13882 /* Return the named attribute or NULL if not there,
13883    but do not follow DW_AT_specification, etc.
13884    This is for use in contexts where we're reading .debug_types dies.
13885    Following DW_AT_specification, DW_AT_abstract_origin will take us
13886    back up the chain, and we want to go down.  */
13887
13888 static struct attribute *
13889 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
13890 {
13891   unsigned int i;
13892
13893   for (i = 0; i < die->num_attrs; ++i)
13894     if (die->attrs[i].name == name)
13895       return &die->attrs[i];
13896
13897   return NULL;
13898 }
13899
13900 /* Return non-zero iff the attribute NAME is defined for the given DIE,
13901    and holds a non-zero value.  This function should only be used for
13902    DW_FORM_flag or DW_FORM_flag_present attributes.  */
13903
13904 static int
13905 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
13906 {
13907   struct attribute *attr = dwarf2_attr (die, name, cu);
13908
13909   return (attr && DW_UNSND (attr));
13910 }
13911
13912 static int
13913 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
13914 {
13915   /* A DIE is a declaration if it has a DW_AT_declaration attribute
13916      which value is non-zero.  However, we have to be careful with
13917      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
13918      (via dwarf2_flag_true_p) follows this attribute.  So we may
13919      end up accidently finding a declaration attribute that belongs
13920      to a different DIE referenced by the specification attribute,
13921      even though the given DIE does not have a declaration attribute.  */
13922   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
13923           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
13924 }
13925
13926 /* Return the die giving the specification for DIE, if there is
13927    one.  *SPEC_CU is the CU containing DIE on input, and the CU
13928    containing the return value on output.  If there is no
13929    specification, but there is an abstract origin, that is
13930    returned.  */
13931
13932 static struct die_info *
13933 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
13934 {
13935   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
13936                                              *spec_cu);
13937
13938   if (spec_attr == NULL)
13939     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
13940
13941   if (spec_attr == NULL)
13942     return NULL;
13943   else
13944     return follow_die_ref (die, spec_attr, spec_cu);
13945 }
13946
13947 /* Free the line_header structure *LH, and any arrays and strings it
13948    refers to.
13949    NOTE: This is also used as a "cleanup" function.  */
13950
13951 static void
13952 free_line_header (struct line_header *lh)
13953 {
13954   if (lh->standard_opcode_lengths)
13955     xfree (lh->standard_opcode_lengths);
13956
13957   /* Remember that all the lh->file_names[i].name pointers are
13958      pointers into debug_line_buffer, and don't need to be freed.  */
13959   if (lh->file_names)
13960     xfree (lh->file_names);
13961
13962   /* Similarly for the include directory names.  */
13963   if (lh->include_dirs)
13964     xfree (lh->include_dirs);
13965
13966   xfree (lh);
13967 }
13968
13969 /* Add an entry to LH's include directory table.  */
13970
13971 static void
13972 add_include_dir (struct line_header *lh, char *include_dir)
13973 {
13974   /* Grow the array if necessary.  */
13975   if (lh->include_dirs_size == 0)
13976     {
13977       lh->include_dirs_size = 1; /* for testing */
13978       lh->include_dirs = xmalloc (lh->include_dirs_size
13979                                   * sizeof (*lh->include_dirs));
13980     }
13981   else if (lh->num_include_dirs >= lh->include_dirs_size)
13982     {
13983       lh->include_dirs_size *= 2;
13984       lh->include_dirs = xrealloc (lh->include_dirs,
13985                                    (lh->include_dirs_size
13986                                     * sizeof (*lh->include_dirs)));
13987     }
13988
13989   lh->include_dirs[lh->num_include_dirs++] = include_dir;
13990 }
13991
13992 /* Add an entry to LH's file name table.  */
13993
13994 static void
13995 add_file_name (struct line_header *lh,
13996                char *name,
13997                unsigned int dir_index,
13998                unsigned int mod_time,
13999                unsigned int length)
14000 {
14001   struct file_entry *fe;
14002
14003   /* Grow the array if necessary.  */
14004   if (lh->file_names_size == 0)
14005     {
14006       lh->file_names_size = 1; /* for testing */
14007       lh->file_names = xmalloc (lh->file_names_size
14008                                 * sizeof (*lh->file_names));
14009     }
14010   else if (lh->num_file_names >= lh->file_names_size)
14011     {
14012       lh->file_names_size *= 2;
14013       lh->file_names = xrealloc (lh->file_names,
14014                                  (lh->file_names_size
14015                                   * sizeof (*lh->file_names)));
14016     }
14017
14018   fe = &lh->file_names[lh->num_file_names++];
14019   fe->name = name;
14020   fe->dir_index = dir_index;
14021   fe->mod_time = mod_time;
14022   fe->length = length;
14023   fe->included_p = 0;
14024   fe->symtab = NULL;
14025 }
14026
14027 /* A convenience function to find the proper .debug_line section for a
14028    CU.  */
14029
14030 static struct dwarf2_section_info *
14031 get_debug_line_section (struct dwarf2_cu *cu)
14032 {
14033   struct dwarf2_section_info *section;
14034
14035   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
14036      DWO file.  */
14037   if (cu->dwo_unit && cu->per_cu->is_debug_types)
14038     section = &cu->dwo_unit->dwo_file->sections.line;
14039   else if (cu->per_cu->is_dwz)
14040     {
14041       struct dwz_file *dwz = dwarf2_get_dwz_file ();
14042
14043       section = &dwz->line;
14044     }
14045   else
14046     section = &dwarf2_per_objfile->line;
14047
14048   return section;
14049 }
14050
14051 /* Read the statement program header starting at OFFSET in
14052    .debug_line, or .debug_line.dwo.  Return a pointer
14053    to a struct line_header, allocated using xmalloc.
14054
14055    NOTE: the strings in the include directory and file name tables of
14056    the returned object point into the dwarf line section buffer,
14057    and must not be freed.  */
14058
14059 static struct line_header *
14060 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
14061 {
14062   struct cleanup *back_to;
14063   struct line_header *lh;
14064   gdb_byte *line_ptr;
14065   unsigned int bytes_read, offset_size;
14066   int i;
14067   char *cur_dir, *cur_file;
14068   struct dwarf2_section_info *section;
14069   bfd *abfd;
14070
14071   section = get_debug_line_section (cu);
14072   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14073   if (section->buffer == NULL)
14074     {
14075       if (cu->dwo_unit && cu->per_cu->is_debug_types)
14076         complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
14077       else
14078         complaint (&symfile_complaints, _("missing .debug_line section"));
14079       return 0;
14080     }
14081
14082   /* We can't do this until we know the section is non-empty.
14083      Only then do we know we have such a section.  */
14084   abfd = section->asection->owner;
14085
14086   /* Make sure that at least there's room for the total_length field.
14087      That could be 12 bytes long, but we're just going to fudge that.  */
14088   if (offset + 4 >= section->size)
14089     {
14090       dwarf2_statement_list_fits_in_line_number_section_complaint ();
14091       return 0;
14092     }
14093
14094   lh = xmalloc (sizeof (*lh));
14095   memset (lh, 0, sizeof (*lh));
14096   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
14097                           (void *) lh);
14098
14099   line_ptr = section->buffer + offset;
14100
14101   /* Read in the header.  */
14102   lh->total_length =
14103     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
14104                                             &bytes_read, &offset_size);
14105   line_ptr += bytes_read;
14106   if (line_ptr + lh->total_length > (section->buffer + section->size))
14107     {
14108       dwarf2_statement_list_fits_in_line_number_section_complaint ();
14109       return 0;
14110     }
14111   lh->statement_program_end = line_ptr + lh->total_length;
14112   lh->version = read_2_bytes (abfd, line_ptr);
14113   line_ptr += 2;
14114   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
14115   line_ptr += offset_size;
14116   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
14117   line_ptr += 1;
14118   if (lh->version >= 4)
14119     {
14120       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
14121       line_ptr += 1;
14122     }
14123   else
14124     lh->maximum_ops_per_instruction = 1;
14125
14126   if (lh->maximum_ops_per_instruction == 0)
14127     {
14128       lh->maximum_ops_per_instruction = 1;
14129       complaint (&symfile_complaints,
14130                  _("invalid maximum_ops_per_instruction "
14131                    "in `.debug_line' section"));
14132     }
14133
14134   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
14135   line_ptr += 1;
14136   lh->line_base = read_1_signed_byte (abfd, line_ptr);
14137   line_ptr += 1;
14138   lh->line_range = read_1_byte (abfd, line_ptr);
14139   line_ptr += 1;
14140   lh->opcode_base = read_1_byte (abfd, line_ptr);
14141   line_ptr += 1;
14142   lh->standard_opcode_lengths
14143     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
14144
14145   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
14146   for (i = 1; i < lh->opcode_base; ++i)
14147     {
14148       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
14149       line_ptr += 1;
14150     }
14151
14152   /* Read directory table.  */
14153   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14154     {
14155       line_ptr += bytes_read;
14156       add_include_dir (lh, cur_dir);
14157     }
14158   line_ptr += bytes_read;
14159
14160   /* Read file name table.  */
14161   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
14162     {
14163       unsigned int dir_index, mod_time, length;
14164
14165       line_ptr += bytes_read;
14166       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14167       line_ptr += bytes_read;
14168       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14169       line_ptr += bytes_read;
14170       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14171       line_ptr += bytes_read;
14172
14173       add_file_name (lh, cur_file, dir_index, mod_time, length);
14174     }
14175   line_ptr += bytes_read;
14176   lh->statement_program_start = line_ptr;
14177
14178   if (line_ptr > (section->buffer + section->size))
14179     complaint (&symfile_complaints,
14180                _("line number info header doesn't "
14181                  "fit in `.debug_line' section"));
14182
14183   discard_cleanups (back_to);
14184   return lh;
14185 }
14186
14187 /* Subroutine of dwarf_decode_lines to simplify it.
14188    Return the file name of the psymtab for included file FILE_INDEX
14189    in line header LH of PST.
14190    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14191    If space for the result is malloc'd, it will be freed by a cleanup.
14192    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
14193
14194 static char *
14195 psymtab_include_file_name (const struct line_header *lh, int file_index,
14196                            const struct partial_symtab *pst,
14197                            const char *comp_dir)
14198 {
14199   const struct file_entry fe = lh->file_names [file_index];
14200   char *include_name = fe.name;
14201   char *include_name_to_compare = include_name;
14202   char *dir_name = NULL;
14203   const char *pst_filename;
14204   char *copied_name = NULL;
14205   int file_is_pst;
14206
14207   if (fe.dir_index)
14208     dir_name = lh->include_dirs[fe.dir_index - 1];
14209
14210   if (!IS_ABSOLUTE_PATH (include_name)
14211       && (dir_name != NULL || comp_dir != NULL))
14212     {
14213       /* Avoid creating a duplicate psymtab for PST.
14214          We do this by comparing INCLUDE_NAME and PST_FILENAME.
14215          Before we do the comparison, however, we need to account
14216          for DIR_NAME and COMP_DIR.
14217          First prepend dir_name (if non-NULL).  If we still don't
14218          have an absolute path prepend comp_dir (if non-NULL).
14219          However, the directory we record in the include-file's
14220          psymtab does not contain COMP_DIR (to match the
14221          corresponding symtab(s)).
14222
14223          Example:
14224
14225          bash$ cd /tmp
14226          bash$ gcc -g ./hello.c
14227          include_name = "hello.c"
14228          dir_name = "."
14229          DW_AT_comp_dir = comp_dir = "/tmp"
14230          DW_AT_name = "./hello.c"  */
14231
14232       if (dir_name != NULL)
14233         {
14234           include_name = concat (dir_name, SLASH_STRING,
14235                                  include_name, (char *)NULL);
14236           include_name_to_compare = include_name;
14237           make_cleanup (xfree, include_name);
14238         }
14239       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
14240         {
14241           include_name_to_compare = concat (comp_dir, SLASH_STRING,
14242                                             include_name, (char *)NULL);
14243         }
14244     }
14245
14246   pst_filename = pst->filename;
14247   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
14248     {
14249       copied_name = concat (pst->dirname, SLASH_STRING,
14250                             pst_filename, (char *)NULL);
14251       pst_filename = copied_name;
14252     }
14253
14254   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
14255
14256   if (include_name_to_compare != include_name)
14257     xfree (include_name_to_compare);
14258   if (copied_name != NULL)
14259     xfree (copied_name);
14260
14261   if (file_is_pst)
14262     return NULL;
14263   return include_name;
14264 }
14265
14266 /* Ignore this record_line request.  */
14267
14268 static void
14269 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
14270 {
14271   return;
14272 }
14273
14274 /* Subroutine of dwarf_decode_lines to simplify it.
14275    Process the line number information in LH.  */
14276
14277 static void
14278 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
14279                       struct dwarf2_cu *cu, struct partial_symtab *pst)
14280 {
14281   gdb_byte *line_ptr, *extended_end;
14282   gdb_byte *line_end;
14283   unsigned int bytes_read, extended_len;
14284   unsigned char op_code, extended_op, adj_opcode;
14285   CORE_ADDR baseaddr;
14286   struct objfile *objfile = cu->objfile;
14287   bfd *abfd = objfile->obfd;
14288   struct gdbarch *gdbarch = get_objfile_arch (objfile);
14289   const int decode_for_pst_p = (pst != NULL);
14290   struct subfile *last_subfile = NULL;
14291   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
14292     = record_line;
14293
14294   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14295
14296   line_ptr = lh->statement_program_start;
14297   line_end = lh->statement_program_end;
14298
14299   /* Read the statement sequences until there's nothing left.  */
14300   while (line_ptr < line_end)
14301     {
14302       /* state machine registers  */
14303       CORE_ADDR address = 0;
14304       unsigned int file = 1;
14305       unsigned int line = 1;
14306       unsigned int column = 0;
14307       int is_stmt = lh->default_is_stmt;
14308       int basic_block = 0;
14309       int end_sequence = 0;
14310       CORE_ADDR addr;
14311       unsigned char op_index = 0;
14312
14313       if (!decode_for_pst_p && lh->num_file_names >= file)
14314         {
14315           /* Start a subfile for the current file of the state machine.  */
14316           /* lh->include_dirs and lh->file_names are 0-based, but the
14317              directory and file name numbers in the statement program
14318              are 1-based.  */
14319           struct file_entry *fe = &lh->file_names[file - 1];
14320           char *dir = NULL;
14321
14322           if (fe->dir_index)
14323             dir = lh->include_dirs[fe->dir_index - 1];
14324
14325           dwarf2_start_subfile (fe->name, dir, comp_dir);
14326         }
14327
14328       /* Decode the table.  */
14329       while (!end_sequence)
14330         {
14331           op_code = read_1_byte (abfd, line_ptr);
14332           line_ptr += 1;
14333           if (line_ptr > line_end)
14334             {
14335               dwarf2_debug_line_missing_end_sequence_complaint ();
14336               break;
14337             }
14338
14339           if (op_code >= lh->opcode_base)
14340             {
14341               /* Special operand.  */
14342               adj_opcode = op_code - lh->opcode_base;
14343               address += (((op_index + (adj_opcode / lh->line_range))
14344                            / lh->maximum_ops_per_instruction)
14345                           * lh->minimum_instruction_length);
14346               op_index = ((op_index + (adj_opcode / lh->line_range))
14347                           % lh->maximum_ops_per_instruction);
14348               line += lh->line_base + (adj_opcode % lh->line_range);
14349               if (lh->num_file_names < file || file == 0)
14350                 dwarf2_debug_line_missing_file_complaint ();
14351               /* For now we ignore lines not starting on an
14352                  instruction boundary.  */
14353               else if (op_index == 0)
14354                 {
14355                   lh->file_names[file - 1].included_p = 1;
14356                   if (!decode_for_pst_p && is_stmt)
14357                     {
14358                       if (last_subfile != current_subfile)
14359                         {
14360                           addr = gdbarch_addr_bits_remove (gdbarch, address);
14361                           if (last_subfile)
14362                             (*p_record_line) (last_subfile, 0, addr);
14363                           last_subfile = current_subfile;
14364                         }
14365                       /* Append row to matrix using current values.  */
14366                       addr = gdbarch_addr_bits_remove (gdbarch, address);
14367                       (*p_record_line) (current_subfile, line, addr);
14368                     }
14369                 }
14370               basic_block = 0;
14371             }
14372           else switch (op_code)
14373             {
14374             case DW_LNS_extended_op:
14375               extended_len = read_unsigned_leb128 (abfd, line_ptr,
14376                                                    &bytes_read);
14377               line_ptr += bytes_read;
14378               extended_end = line_ptr + extended_len;
14379               extended_op = read_1_byte (abfd, line_ptr);
14380               line_ptr += 1;
14381               switch (extended_op)
14382                 {
14383                 case DW_LNE_end_sequence:
14384                   p_record_line = record_line;
14385                   end_sequence = 1;
14386                   break;
14387                 case DW_LNE_set_address:
14388                   address = read_address (abfd, line_ptr, cu, &bytes_read);
14389
14390                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
14391                     {
14392                       /* This line table is for a function which has been
14393                          GCd by the linker.  Ignore it.  PR gdb/12528 */
14394
14395                       long line_offset
14396                         = line_ptr - get_debug_line_section (cu)->buffer;
14397
14398                       complaint (&symfile_complaints,
14399                                  _(".debug_line address at offset 0x%lx is 0 "
14400                                    "[in module %s]"),
14401                                  line_offset, objfile->name);
14402                       p_record_line = noop_record_line;
14403                     }
14404
14405                   op_index = 0;
14406                   line_ptr += bytes_read;
14407                   address += baseaddr;
14408                   break;
14409                 case DW_LNE_define_file:
14410                   {
14411                     char *cur_file;
14412                     unsigned int dir_index, mod_time, length;
14413
14414                     cur_file = read_direct_string (abfd, line_ptr,
14415                                                    &bytes_read);
14416                     line_ptr += bytes_read;
14417                     dir_index =
14418                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14419                     line_ptr += bytes_read;
14420                     mod_time =
14421                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14422                     line_ptr += bytes_read;
14423                     length =
14424                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14425                     line_ptr += bytes_read;
14426                     add_file_name (lh, cur_file, dir_index, mod_time, length);
14427                   }
14428                   break;
14429                 case DW_LNE_set_discriminator:
14430                   /* The discriminator is not interesting to the debugger;
14431                      just ignore it.  */
14432                   line_ptr = extended_end;
14433                   break;
14434                 default:
14435                   complaint (&symfile_complaints,
14436                              _("mangled .debug_line section"));
14437                   return;
14438                 }
14439               /* Make sure that we parsed the extended op correctly.  If e.g.
14440                  we expected a different address size than the producer used,
14441                  we may have read the wrong number of bytes.  */
14442               if (line_ptr != extended_end)
14443                 {
14444                   complaint (&symfile_complaints,
14445                              _("mangled .debug_line section"));
14446                   return;
14447                 }
14448               break;
14449             case DW_LNS_copy:
14450               if (lh->num_file_names < file || file == 0)
14451                 dwarf2_debug_line_missing_file_complaint ();
14452               else
14453                 {
14454                   lh->file_names[file - 1].included_p = 1;
14455                   if (!decode_for_pst_p && is_stmt)
14456                     {
14457                       if (last_subfile != current_subfile)
14458                         {
14459                           addr = gdbarch_addr_bits_remove (gdbarch, address);
14460                           if (last_subfile)
14461                             (*p_record_line) (last_subfile, 0, addr);
14462                           last_subfile = current_subfile;
14463                         }
14464                       addr = gdbarch_addr_bits_remove (gdbarch, address);
14465                       (*p_record_line) (current_subfile, line, addr);
14466                     }
14467                 }
14468               basic_block = 0;
14469               break;
14470             case DW_LNS_advance_pc:
14471               {
14472                 CORE_ADDR adjust
14473                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14474
14475                 address += (((op_index + adjust)
14476                              / lh->maximum_ops_per_instruction)
14477                             * lh->minimum_instruction_length);
14478                 op_index = ((op_index + adjust)
14479                             % lh->maximum_ops_per_instruction);
14480                 line_ptr += bytes_read;
14481               }
14482               break;
14483             case DW_LNS_advance_line:
14484               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
14485               line_ptr += bytes_read;
14486               break;
14487             case DW_LNS_set_file:
14488               {
14489                 /* The arrays lh->include_dirs and lh->file_names are
14490                    0-based, but the directory and file name numbers in
14491                    the statement program are 1-based.  */
14492                 struct file_entry *fe;
14493                 char *dir = NULL;
14494
14495                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14496                 line_ptr += bytes_read;
14497                 if (lh->num_file_names < file || file == 0)
14498                   dwarf2_debug_line_missing_file_complaint ();
14499                 else
14500                   {
14501                     fe = &lh->file_names[file - 1];
14502                     if (fe->dir_index)
14503                       dir = lh->include_dirs[fe->dir_index - 1];
14504                     if (!decode_for_pst_p)
14505                       {
14506                         last_subfile = current_subfile;
14507                         dwarf2_start_subfile (fe->name, dir, comp_dir);
14508                       }
14509                   }
14510               }
14511               break;
14512             case DW_LNS_set_column:
14513               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14514               line_ptr += bytes_read;
14515               break;
14516             case DW_LNS_negate_stmt:
14517               is_stmt = (!is_stmt);
14518               break;
14519             case DW_LNS_set_basic_block:
14520               basic_block = 1;
14521               break;
14522             /* Add to the address register of the state machine the
14523                address increment value corresponding to special opcode
14524                255.  I.e., this value is scaled by the minimum
14525                instruction length since special opcode 255 would have
14526                scaled the increment.  */
14527             case DW_LNS_const_add_pc:
14528               {
14529                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
14530
14531                 address += (((op_index + adjust)
14532                              / lh->maximum_ops_per_instruction)
14533                             * lh->minimum_instruction_length);
14534                 op_index = ((op_index + adjust)
14535                             % lh->maximum_ops_per_instruction);
14536               }
14537               break;
14538             case DW_LNS_fixed_advance_pc:
14539               address += read_2_bytes (abfd, line_ptr);
14540               op_index = 0;
14541               line_ptr += 2;
14542               break;
14543             default:
14544               {
14545                 /* Unknown standard opcode, ignore it.  */
14546                 int i;
14547
14548                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
14549                   {
14550                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
14551                     line_ptr += bytes_read;
14552                   }
14553               }
14554             }
14555         }
14556       if (lh->num_file_names < file || file == 0)
14557         dwarf2_debug_line_missing_file_complaint ();
14558       else
14559         {
14560           lh->file_names[file - 1].included_p = 1;
14561           if (!decode_for_pst_p)
14562             {
14563               addr = gdbarch_addr_bits_remove (gdbarch, address);
14564               (*p_record_line) (current_subfile, 0, addr);
14565             }
14566         }
14567     }
14568 }
14569
14570 /* Decode the Line Number Program (LNP) for the given line_header
14571    structure and CU.  The actual information extracted and the type
14572    of structures created from the LNP depends on the value of PST.
14573
14574    1. If PST is NULL, then this procedure uses the data from the program
14575       to create all necessary symbol tables, and their linetables.
14576
14577    2. If PST is not NULL, this procedure reads the program to determine
14578       the list of files included by the unit represented by PST, and
14579       builds all the associated partial symbol tables.
14580
14581    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
14582    It is used for relative paths in the line table.
14583    NOTE: When processing partial symtabs (pst != NULL),
14584    comp_dir == pst->dirname.
14585
14586    NOTE: It is important that psymtabs have the same file name (via strcmp)
14587    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
14588    symtab we don't use it in the name of the psymtabs we create.
14589    E.g. expand_line_sal requires this when finding psymtabs to expand.
14590    A good testcase for this is mb-inline.exp.  */
14591
14592 static void
14593 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
14594                     struct dwarf2_cu *cu, struct partial_symtab *pst,
14595                     int want_line_info)
14596 {
14597   struct objfile *objfile = cu->objfile;
14598   const int decode_for_pst_p = (pst != NULL);
14599   struct subfile *first_subfile = current_subfile;
14600
14601   if (want_line_info)
14602     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
14603
14604   if (decode_for_pst_p)
14605     {
14606       int file_index;
14607
14608       /* Now that we're done scanning the Line Header Program, we can
14609          create the psymtab of each included file.  */
14610       for (file_index = 0; file_index < lh->num_file_names; file_index++)
14611         if (lh->file_names[file_index].included_p == 1)
14612           {
14613             char *include_name =
14614               psymtab_include_file_name (lh, file_index, pst, comp_dir);
14615             if (include_name != NULL)
14616               dwarf2_create_include_psymtab (include_name, pst, objfile);
14617           }
14618     }
14619   else
14620     {
14621       /* Make sure a symtab is created for every file, even files
14622          which contain only variables (i.e. no code with associated
14623          line numbers).  */
14624       int i;
14625
14626       for (i = 0; i < lh->num_file_names; i++)
14627         {
14628           char *dir = NULL;
14629           struct file_entry *fe;
14630
14631           fe = &lh->file_names[i];
14632           if (fe->dir_index)
14633             dir = lh->include_dirs[fe->dir_index - 1];
14634           dwarf2_start_subfile (fe->name, dir, comp_dir);
14635
14636           /* Skip the main file; we don't need it, and it must be
14637              allocated last, so that it will show up before the
14638              non-primary symtabs in the objfile's symtab list.  */
14639           if (current_subfile == first_subfile)
14640             continue;
14641
14642           if (current_subfile->symtab == NULL)
14643             current_subfile->symtab = allocate_symtab (current_subfile->name,
14644                                                        objfile);
14645           fe->symtab = current_subfile->symtab;
14646         }
14647     }
14648 }
14649
14650 /* Start a subfile for DWARF.  FILENAME is the name of the file and
14651    DIRNAME the name of the source directory which contains FILENAME
14652    or NULL if not known.  COMP_DIR is the compilation directory for the
14653    linetable's compilation unit or NULL if not known.
14654    This routine tries to keep line numbers from identical absolute and
14655    relative file names in a common subfile.
14656
14657    Using the `list' example from the GDB testsuite, which resides in
14658    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
14659    of /srcdir/list0.c yields the following debugging information for list0.c:
14660
14661    DW_AT_name:          /srcdir/list0.c
14662    DW_AT_comp_dir:              /compdir
14663    files.files[0].name: list0.h
14664    files.files[0].dir:  /srcdir
14665    files.files[1].name: list0.c
14666    files.files[1].dir:  /srcdir
14667
14668    The line number information for list0.c has to end up in a single
14669    subfile, so that `break /srcdir/list0.c:1' works as expected.
14670    start_subfile will ensure that this happens provided that we pass the
14671    concatenation of files.files[1].dir and files.files[1].name as the
14672    subfile's name.  */
14673
14674 static void
14675 dwarf2_start_subfile (char *filename, const char *dirname,
14676                       const char *comp_dir)
14677 {
14678   char *fullname;
14679
14680   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
14681      `start_symtab' will always pass the contents of DW_AT_comp_dir as
14682      second argument to start_subfile.  To be consistent, we do the
14683      same here.  In order not to lose the line information directory,
14684      we concatenate it to the filename when it makes sense.
14685      Note that the Dwarf3 standard says (speaking of filenames in line
14686      information): ``The directory index is ignored for file names
14687      that represent full path names''.  Thus ignoring dirname in the
14688      `else' branch below isn't an issue.  */
14689
14690   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
14691     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
14692   else
14693     fullname = filename;
14694
14695   start_subfile (fullname, comp_dir);
14696
14697   if (fullname != filename)
14698     xfree (fullname);
14699 }
14700
14701 /* Start a symtab for DWARF.
14702    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */
14703
14704 static void
14705 dwarf2_start_symtab (struct dwarf2_cu *cu,
14706                      char *name, char *comp_dir, CORE_ADDR low_pc)
14707 {
14708   start_symtab (name, comp_dir, low_pc);
14709   record_debugformat ("DWARF 2");
14710   record_producer (cu->producer);
14711
14712   /* We assume that we're processing GCC output.  */
14713   processing_gcc_compilation = 2;
14714
14715   processing_has_namespace_info = 0;
14716 }
14717
14718 static void
14719 var_decode_location (struct attribute *attr, struct symbol *sym,
14720                      struct dwarf2_cu *cu)
14721 {
14722   struct objfile *objfile = cu->objfile;
14723   struct comp_unit_head *cu_header = &cu->header;
14724
14725   /* NOTE drow/2003-01-30: There used to be a comment and some special
14726      code here to turn a symbol with DW_AT_external and a
14727      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
14728      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
14729      with some versions of binutils) where shared libraries could have
14730      relocations against symbols in their debug information - the
14731      minimal symbol would have the right address, but the debug info
14732      would not.  It's no longer necessary, because we will explicitly
14733      apply relocations when we read in the debug information now.  */
14734
14735   /* A DW_AT_location attribute with no contents indicates that a
14736      variable has been optimized away.  */
14737   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
14738     {
14739       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14740       return;
14741     }
14742
14743   /* Handle one degenerate form of location expression specially, to
14744      preserve GDB's previous behavior when section offsets are
14745      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
14746      then mark this symbol as LOC_STATIC.  */
14747
14748   if (attr_form_is_block (attr)
14749       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
14750            && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
14751           || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
14752               && (DW_BLOCK (attr)->size
14753                   == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
14754     {
14755       unsigned int dummy;
14756
14757       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
14758         SYMBOL_VALUE_ADDRESS (sym) =
14759           read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
14760       else
14761         SYMBOL_VALUE_ADDRESS (sym) =
14762           read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
14763       SYMBOL_CLASS (sym) = LOC_STATIC;
14764       fixup_symbol_section (sym, objfile);
14765       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
14766                                               SYMBOL_SECTION (sym));
14767       return;
14768     }
14769
14770   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
14771      expression evaluator, and use LOC_COMPUTED only when necessary
14772      (i.e. when the value of a register or memory location is
14773      referenced, or a thread-local block, etc.).  Then again, it might
14774      not be worthwhile.  I'm assuming that it isn't unless performance
14775      or memory numbers show me otherwise.  */
14776
14777   dwarf2_symbol_mark_computed (attr, sym, cu);
14778   SYMBOL_CLASS (sym) = LOC_COMPUTED;
14779
14780   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
14781     cu->has_loclist = 1;
14782 }
14783
14784 /* Given a pointer to a DWARF information entry, figure out if we need
14785    to make a symbol table entry for it, and if so, create a new entry
14786    and return a pointer to it.
14787    If TYPE is NULL, determine symbol type from the die, otherwise
14788    used the passed type.
14789    If SPACE is not NULL, use it to hold the new symbol.  If it is
14790    NULL, allocate a new symbol on the objfile's obstack.  */
14791
14792 static struct symbol *
14793 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
14794                  struct symbol *space)
14795 {
14796   struct objfile *objfile = cu->objfile;
14797   struct symbol *sym = NULL;
14798   char *name;
14799   struct attribute *attr = NULL;
14800   struct attribute *attr2 = NULL;
14801   CORE_ADDR baseaddr;
14802   struct pending **list_to_add = NULL;
14803
14804   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
14805
14806   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14807
14808   name = dwarf2_name (die, cu);
14809   if (name)
14810     {
14811       const char *linkagename;
14812       int suppress_add = 0;
14813
14814       if (space)
14815         sym = space;
14816       else
14817         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
14818       OBJSTAT (objfile, n_syms++);
14819
14820       /* Cache this symbol's name and the name's demangled form (if any).  */
14821       SYMBOL_SET_LANGUAGE (sym, cu->language);
14822       linkagename = dwarf2_physname (name, die, cu);
14823       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
14824
14825       /* Fortran does not have mangling standard and the mangling does differ
14826          between gfortran, iFort etc.  */
14827       if (cu->language == language_fortran
14828           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
14829         symbol_set_demangled_name (&(sym->ginfo),
14830                                    (char *) dwarf2_full_name (name, die, cu),
14831                                    NULL);
14832
14833       /* Default assumptions.
14834          Use the passed type or decode it from the die.  */
14835       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
14836       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
14837       if (type != NULL)
14838         SYMBOL_TYPE (sym) = type;
14839       else
14840         SYMBOL_TYPE (sym) = die_type (die, cu);
14841       attr = dwarf2_attr (die,
14842                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
14843                           cu);
14844       if (attr)
14845         {
14846           SYMBOL_LINE (sym) = DW_UNSND (attr);
14847         }
14848
14849       attr = dwarf2_attr (die,
14850                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
14851                           cu);
14852       if (attr)
14853         {
14854           int file_index = DW_UNSND (attr);
14855
14856           if (cu->line_header == NULL
14857               || file_index > cu->line_header->num_file_names)
14858             complaint (&symfile_complaints,
14859                        _("file index out of range"));
14860           else if (file_index > 0)
14861             {
14862               struct file_entry *fe;
14863
14864               fe = &cu->line_header->file_names[file_index - 1];
14865               SYMBOL_SYMTAB (sym) = fe->symtab;
14866             }
14867         }
14868
14869       switch (die->tag)
14870         {
14871         case DW_TAG_label:
14872           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14873           if (attr)
14874             {
14875               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
14876             }
14877           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
14878           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
14879           SYMBOL_CLASS (sym) = LOC_LABEL;
14880           add_symbol_to_list (sym, cu->list_in_scope);
14881           break;
14882         case DW_TAG_subprogram:
14883           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14884              finish_block.  */
14885           SYMBOL_CLASS (sym) = LOC_BLOCK;
14886           attr2 = dwarf2_attr (die, DW_AT_external, cu);
14887           if ((attr2 && (DW_UNSND (attr2) != 0))
14888               || cu->language == language_ada)
14889             {
14890               /* Subprograms marked external are stored as a global symbol.
14891                  Ada subprograms, whether marked external or not, are always
14892                  stored as a global symbol, because we want to be able to
14893                  access them globally.  For instance, we want to be able
14894                  to break on a nested subprogram without having to
14895                  specify the context.  */
14896               list_to_add = &global_symbols;
14897             }
14898           else
14899             {
14900               list_to_add = cu->list_in_scope;
14901             }
14902           break;
14903         case DW_TAG_inlined_subroutine:
14904           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
14905              finish_block.  */
14906           SYMBOL_CLASS (sym) = LOC_BLOCK;
14907           SYMBOL_INLINED (sym) = 1;
14908           list_to_add = cu->list_in_scope;
14909           break;
14910         case DW_TAG_template_value_param:
14911           suppress_add = 1;
14912           /* Fall through.  */
14913         case DW_TAG_constant:
14914         case DW_TAG_variable:
14915         case DW_TAG_member:
14916           /* Compilation with minimal debug info may result in
14917              variables with missing type entries.  Change the
14918              misleading `void' type to something sensible.  */
14919           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
14920             SYMBOL_TYPE (sym)
14921               = objfile_type (objfile)->nodebug_data_symbol;
14922
14923           attr = dwarf2_attr (die, DW_AT_const_value, cu);
14924           /* In the case of DW_TAG_member, we should only be called for
14925              static const members.  */
14926           if (die->tag == DW_TAG_member)
14927             {
14928               /* dwarf2_add_field uses die_is_declaration,
14929                  so we do the same.  */
14930               gdb_assert (die_is_declaration (die, cu));
14931               gdb_assert (attr);
14932             }
14933           if (attr)
14934             {
14935               dwarf2_const_value (attr, sym, cu);
14936               attr2 = dwarf2_attr (die, DW_AT_external, cu);
14937               if (!suppress_add)
14938                 {
14939                   if (attr2 && (DW_UNSND (attr2) != 0))
14940                     list_to_add = &global_symbols;
14941                   else
14942                     list_to_add = cu->list_in_scope;
14943                 }
14944               break;
14945             }
14946           attr = dwarf2_attr (die, DW_AT_location, cu);
14947           if (attr)
14948             {
14949               var_decode_location (attr, sym, cu);
14950               attr2 = dwarf2_attr (die, DW_AT_external, cu);
14951               if (SYMBOL_CLASS (sym) == LOC_STATIC
14952                   && SYMBOL_VALUE_ADDRESS (sym) == 0
14953                   && !dwarf2_per_objfile->has_section_at_zero)
14954                 {
14955                   /* When a static variable is eliminated by the linker,
14956                      the corresponding debug information is not stripped
14957                      out, but the variable address is set to null;
14958                      do not add such variables into symbol table.  */
14959                 }
14960               else if (attr2 && (DW_UNSND (attr2) != 0))
14961                 {
14962                   /* Workaround gfortran PR debug/40040 - it uses
14963                      DW_AT_location for variables in -fPIC libraries which may
14964                      get overriden by other libraries/executable and get
14965                      a different address.  Resolve it by the minimal symbol
14966                      which may come from inferior's executable using copy
14967                      relocation.  Make this workaround only for gfortran as for
14968                      other compilers GDB cannot guess the minimal symbol
14969                      Fortran mangling kind.  */
14970                   if (cu->language == language_fortran && die->parent
14971                       && die->parent->tag == DW_TAG_module
14972                       && cu->producer
14973                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
14974                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
14975
14976                   /* A variable with DW_AT_external is never static,
14977                      but it may be block-scoped.  */
14978                   list_to_add = (cu->list_in_scope == &file_symbols
14979                                  ? &global_symbols : cu->list_in_scope);
14980                 }
14981               else
14982                 list_to_add = cu->list_in_scope;
14983             }
14984           else
14985             {
14986               /* We do not know the address of this symbol.
14987                  If it is an external symbol and we have type information
14988                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
14989                  The address of the variable will then be determined from
14990                  the minimal symbol table whenever the variable is
14991                  referenced.  */
14992               attr2 = dwarf2_attr (die, DW_AT_external, cu);
14993               if (attr2 && (DW_UNSND (attr2) != 0)
14994                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
14995                 {
14996                   /* A variable with DW_AT_external is never static, but it
14997                      may be block-scoped.  */
14998                   list_to_add = (cu->list_in_scope == &file_symbols
14999                                  ? &global_symbols : cu->list_in_scope);
15000
15001                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
15002                 }
15003               else if (!die_is_declaration (die, cu))
15004                 {
15005                   /* Use the default LOC_OPTIMIZED_OUT class.  */
15006                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
15007                   if (!suppress_add)
15008                     list_to_add = cu->list_in_scope;
15009                 }
15010             }
15011           break;
15012         case DW_TAG_formal_parameter:
15013           /* If we are inside a function, mark this as an argument.  If
15014              not, we might be looking at an argument to an inlined function
15015              when we do not have enough information to show inlined frames;
15016              pretend it's a local variable in that case so that the user can
15017              still see it.  */
15018           if (context_stack_depth > 0
15019               && context_stack[context_stack_depth - 1].name != NULL)
15020             SYMBOL_IS_ARGUMENT (sym) = 1;
15021           attr = dwarf2_attr (die, DW_AT_location, cu);
15022           if (attr)
15023             {
15024               var_decode_location (attr, sym, cu);
15025             }
15026           attr = dwarf2_attr (die, DW_AT_const_value, cu);
15027           if (attr)
15028             {
15029               dwarf2_const_value (attr, sym, cu);
15030             }
15031
15032           list_to_add = cu->list_in_scope;
15033           break;
15034         case DW_TAG_unspecified_parameters:
15035           /* From varargs functions; gdb doesn't seem to have any
15036              interest in this information, so just ignore it for now.
15037              (FIXME?) */
15038           break;
15039         case DW_TAG_template_type_param:
15040           suppress_add = 1;
15041           /* Fall through.  */
15042         case DW_TAG_class_type:
15043         case DW_TAG_interface_type:
15044         case DW_TAG_structure_type:
15045         case DW_TAG_union_type:
15046         case DW_TAG_set_type:
15047         case DW_TAG_enumeration_type:
15048           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15049           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
15050
15051           {
15052             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
15053                really ever be static objects: otherwise, if you try
15054                to, say, break of a class's method and you're in a file
15055                which doesn't mention that class, it won't work unless
15056                the check for all static symbols in lookup_symbol_aux
15057                saves you.  See the OtherFileClass tests in
15058                gdb.c++/namespace.exp.  */
15059
15060             if (!suppress_add)
15061               {
15062                 list_to_add = (cu->list_in_scope == &file_symbols
15063                                && (cu->language == language_cplus
15064                                    || cu->language == language_java)
15065                                ? &global_symbols : cu->list_in_scope);
15066
15067                 /* The semantics of C++ state that "struct foo {
15068                    ... }" also defines a typedef for "foo".  A Java
15069                    class declaration also defines a typedef for the
15070                    class.  */
15071                 if (cu->language == language_cplus
15072                     || cu->language == language_java
15073                     || cu->language == language_ada)
15074                   {
15075                     /* The symbol's name is already allocated along
15076                        with this objfile, so we don't need to
15077                        duplicate it for the type.  */
15078                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
15079                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
15080                   }
15081               }
15082           }
15083           break;
15084         case DW_TAG_typedef:
15085           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15086           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15087           list_to_add = cu->list_in_scope;
15088           break;
15089         case DW_TAG_base_type:
15090         case DW_TAG_subrange_type:
15091           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15092           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
15093           list_to_add = cu->list_in_scope;
15094           break;
15095         case DW_TAG_enumerator:
15096           attr = dwarf2_attr (die, DW_AT_const_value, cu);
15097           if (attr)
15098             {
15099               dwarf2_const_value (attr, sym, cu);
15100             }
15101           {
15102             /* NOTE: carlton/2003-11-10: See comment above in the
15103                DW_TAG_class_type, etc. block.  */
15104
15105             list_to_add = (cu->list_in_scope == &file_symbols
15106                            && (cu->language == language_cplus
15107                                || cu->language == language_java)
15108                            ? &global_symbols : cu->list_in_scope);
15109           }
15110           break;
15111         case DW_TAG_namespace:
15112           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
15113           list_to_add = &global_symbols;
15114           break;
15115         default:
15116           /* Not a tag we recognize.  Hopefully we aren't processing
15117              trash data, but since we must specifically ignore things
15118              we don't recognize, there is nothing else we should do at
15119              this point.  */
15120           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
15121                      dwarf_tag_name (die->tag));
15122           break;
15123         }
15124
15125       if (suppress_add)
15126         {
15127           sym->hash_next = objfile->template_symbols;
15128           objfile->template_symbols = sym;
15129           list_to_add = NULL;
15130         }
15131
15132       if (list_to_add != NULL)
15133         add_symbol_to_list (sym, list_to_add);
15134
15135       /* For the benefit of old versions of GCC, check for anonymous
15136          namespaces based on the demangled name.  */
15137       if (!processing_has_namespace_info
15138           && cu->language == language_cplus)
15139         cp_scan_for_anonymous_namespaces (sym, objfile);
15140     }
15141   return (sym);
15142 }
15143
15144 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
15145
15146 static struct symbol *
15147 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15148 {
15149   return new_symbol_full (die, type, cu, NULL);
15150 }
15151
15152 /* Given an attr with a DW_FORM_dataN value in host byte order,
15153    zero-extend it as appropriate for the symbol's type.  The DWARF
15154    standard (v4) is not entirely clear about the meaning of using
15155    DW_FORM_dataN for a constant with a signed type, where the type is
15156    wider than the data.  The conclusion of a discussion on the DWARF
15157    list was that this is unspecified.  We choose to always zero-extend
15158    because that is the interpretation long in use by GCC.  */
15159
15160 static gdb_byte *
15161 dwarf2_const_value_data (struct attribute *attr, struct type *type,
15162                          const char *name, struct obstack *obstack,
15163                          struct dwarf2_cu *cu, LONGEST *value, int bits)
15164 {
15165   struct objfile *objfile = cu->objfile;
15166   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
15167                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
15168   LONGEST l = DW_UNSND (attr);
15169
15170   if (bits < sizeof (*value) * 8)
15171     {
15172       l &= ((LONGEST) 1 << bits) - 1;
15173       *value = l;
15174     }
15175   else if (bits == sizeof (*value) * 8)
15176     *value = l;
15177   else
15178     {
15179       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
15180       store_unsigned_integer (bytes, bits / 8, byte_order, l);
15181       return bytes;
15182     }
15183
15184   return NULL;
15185 }
15186
15187 /* Read a constant value from an attribute.  Either set *VALUE, or if
15188    the value does not fit in *VALUE, set *BYTES - either already
15189    allocated on the objfile obstack, or newly allocated on OBSTACK,
15190    or, set *BATON, if we translated the constant to a location
15191    expression.  */
15192
15193 static void
15194 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
15195                          const char *name, struct obstack *obstack,
15196                          struct dwarf2_cu *cu,
15197                          LONGEST *value, gdb_byte **bytes,
15198                          struct dwarf2_locexpr_baton **baton)
15199 {
15200   struct objfile *objfile = cu->objfile;
15201   struct comp_unit_head *cu_header = &cu->header;
15202   struct dwarf_block *blk;
15203   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
15204                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
15205
15206   *value = 0;
15207   *bytes = NULL;
15208   *baton = NULL;
15209
15210   switch (attr->form)
15211     {
15212     case DW_FORM_addr:
15213     case DW_FORM_GNU_addr_index:
15214       {
15215         gdb_byte *data;
15216
15217         if (TYPE_LENGTH (type) != cu_header->addr_size)
15218           dwarf2_const_value_length_mismatch_complaint (name,
15219                                                         cu_header->addr_size,
15220                                                         TYPE_LENGTH (type));
15221         /* Symbols of this form are reasonably rare, so we just
15222            piggyback on the existing location code rather than writing
15223            a new implementation of symbol_computed_ops.  */
15224         *baton = obstack_alloc (&objfile->objfile_obstack,
15225                                 sizeof (struct dwarf2_locexpr_baton));
15226         (*baton)->per_cu = cu->per_cu;
15227         gdb_assert ((*baton)->per_cu);
15228
15229         (*baton)->size = 2 + cu_header->addr_size;
15230         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
15231         (*baton)->data = data;
15232
15233         data[0] = DW_OP_addr;
15234         store_unsigned_integer (&data[1], cu_header->addr_size,
15235                                 byte_order, DW_ADDR (attr));
15236         data[cu_header->addr_size + 1] = DW_OP_stack_value;
15237       }
15238       break;
15239     case DW_FORM_string:
15240     case DW_FORM_strp:
15241     case DW_FORM_GNU_str_index:
15242     case DW_FORM_GNU_strp_alt:
15243       /* DW_STRING is already allocated on the objfile obstack, point
15244          directly to it.  */
15245       *bytes = (gdb_byte *) DW_STRING (attr);
15246       break;
15247     case DW_FORM_block1:
15248     case DW_FORM_block2:
15249     case DW_FORM_block4:
15250     case DW_FORM_block:
15251     case DW_FORM_exprloc:
15252       blk = DW_BLOCK (attr);
15253       if (TYPE_LENGTH (type) != blk->size)
15254         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
15255                                                       TYPE_LENGTH (type));
15256       *bytes = blk->data;
15257       break;
15258
15259       /* The DW_AT_const_value attributes are supposed to carry the
15260          symbol's value "represented as it would be on the target
15261          architecture."  By the time we get here, it's already been
15262          converted to host endianness, so we just need to sign- or
15263          zero-extend it as appropriate.  */
15264     case DW_FORM_data1:
15265       *bytes = dwarf2_const_value_data (attr, type, name,
15266                                         obstack, cu, value, 8);
15267       break;
15268     case DW_FORM_data2:
15269       *bytes = dwarf2_const_value_data (attr, type, name,
15270                                         obstack, cu, value, 16);
15271       break;
15272     case DW_FORM_data4:
15273       *bytes = dwarf2_const_value_data (attr, type, name,
15274                                         obstack, cu, value, 32);
15275       break;
15276     case DW_FORM_data8:
15277       *bytes = dwarf2_const_value_data (attr, type, name,
15278                                         obstack, cu, value, 64);
15279       break;
15280
15281     case DW_FORM_sdata:
15282       *value = DW_SND (attr);
15283       break;
15284
15285     case DW_FORM_udata:
15286       *value = DW_UNSND (attr);
15287       break;
15288
15289     default:
15290       complaint (&symfile_complaints,
15291                  _("unsupported const value attribute form: '%s'"),
15292                  dwarf_form_name (attr->form));
15293       *value = 0;
15294       break;
15295     }
15296 }
15297
15298
15299 /* Copy constant value from an attribute to a symbol.  */
15300
15301 static void
15302 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
15303                     struct dwarf2_cu *cu)
15304 {
15305   struct objfile *objfile = cu->objfile;
15306   struct comp_unit_head *cu_header = &cu->header;
15307   LONGEST value;
15308   gdb_byte *bytes;
15309   struct dwarf2_locexpr_baton *baton;
15310
15311   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
15312                            SYMBOL_PRINT_NAME (sym),
15313                            &objfile->objfile_obstack, cu,
15314                            &value, &bytes, &baton);
15315
15316   if (baton != NULL)
15317     {
15318       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15319       SYMBOL_LOCATION_BATON (sym) = baton;
15320       SYMBOL_CLASS (sym) = LOC_COMPUTED;
15321     }
15322   else if (bytes != NULL)
15323      {
15324       SYMBOL_VALUE_BYTES (sym) = bytes;
15325       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
15326     }
15327   else
15328     {
15329       SYMBOL_VALUE (sym) = value;
15330       SYMBOL_CLASS (sym) = LOC_CONST;
15331     }
15332 }
15333
15334 /* Return the type of the die in question using its DW_AT_type attribute.  */
15335
15336 static struct type *
15337 die_type (struct die_info *die, struct dwarf2_cu *cu)
15338 {
15339   struct attribute *type_attr;
15340
15341   type_attr = dwarf2_attr (die, DW_AT_type, cu);
15342   if (!type_attr)
15343     {
15344       /* A missing DW_AT_type represents a void type.  */
15345       return objfile_type (cu->objfile)->builtin_void;
15346     }
15347
15348   return lookup_die_type (die, type_attr, cu);
15349 }
15350
15351 /* True iff CU's producer generates GNAT Ada auxiliary information
15352    that allows to find parallel types through that information instead
15353    of having to do expensive parallel lookups by type name.  */
15354
15355 static int
15356 need_gnat_info (struct dwarf2_cu *cu)
15357 {
15358   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
15359      of GNAT produces this auxiliary information, without any indication
15360      that it is produced.  Part of enhancing the FSF version of GNAT
15361      to produce that information will be to put in place an indicator
15362      that we can use in order to determine whether the descriptive type
15363      info is available or not.  One suggestion that has been made is
15364      to use a new attribute, attached to the CU die.  For now, assume
15365      that the descriptive type info is not available.  */
15366   return 0;
15367 }
15368
15369 /* Return the auxiliary type of the die in question using its
15370    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
15371    attribute is not present.  */
15372
15373 static struct type *
15374 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
15375 {
15376   struct attribute *type_attr;
15377
15378   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
15379   if (!type_attr)
15380     return NULL;
15381
15382   return lookup_die_type (die, type_attr, cu);
15383 }
15384
15385 /* If DIE has a descriptive_type attribute, then set the TYPE's
15386    descriptive type accordingly.  */
15387
15388 static void
15389 set_descriptive_type (struct type *type, struct die_info *die,
15390                       struct dwarf2_cu *cu)
15391 {
15392   struct type *descriptive_type = die_descriptive_type (die, cu);
15393
15394   if (descriptive_type)
15395     {
15396       ALLOCATE_GNAT_AUX_TYPE (type);
15397       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
15398     }
15399 }
15400
15401 /* Return the containing type of the die in question using its
15402    DW_AT_containing_type attribute.  */
15403
15404 static struct type *
15405 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
15406 {
15407   struct attribute *type_attr;
15408
15409   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
15410   if (!type_attr)
15411     error (_("Dwarf Error: Problem turning containing type into gdb type "
15412              "[in module %s]"), cu->objfile->name);
15413
15414   return lookup_die_type (die, type_attr, cu);
15415 }
15416
15417 /* Look up the type of DIE in CU using its type attribute ATTR.
15418    If there is no type substitute an error marker.  */
15419
15420 static struct type *
15421 lookup_die_type (struct die_info *die, struct attribute *attr,
15422                  struct dwarf2_cu *cu)
15423 {
15424   struct objfile *objfile = cu->objfile;
15425   struct type *this_type;
15426
15427   /* First see if we have it cached.  */
15428
15429   if (attr->form == DW_FORM_GNU_ref_alt)
15430     {
15431       struct dwarf2_per_cu_data *per_cu;
15432       sect_offset offset = dwarf2_get_ref_die_offset (attr);
15433
15434       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
15435       this_type = get_die_type_at_offset (offset, per_cu);
15436     }
15437   else if (is_ref_attr (attr))
15438     {
15439       sect_offset offset = dwarf2_get_ref_die_offset (attr);
15440
15441       this_type = get_die_type_at_offset (offset, cu->per_cu);
15442     }
15443   else if (attr->form == DW_FORM_ref_sig8)
15444     {
15445       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15446
15447       /* sig_type will be NULL if the signatured type is missing from
15448          the debug info.  */
15449       if (sig_type == NULL)
15450         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15451                  "at 0x%x [in module %s]"),
15452                die->offset.sect_off, objfile->name);
15453
15454       gdb_assert (sig_type->per_cu.is_debug_types);
15455       /* If we haven't filled in type_offset_in_section yet, then we
15456          haven't read the type in yet.  */
15457       this_type = NULL;
15458       if (sig_type->type_offset_in_section.sect_off != 0)
15459         {
15460           this_type =
15461             get_die_type_at_offset (sig_type->type_offset_in_section,
15462                                     &sig_type->per_cu);
15463         }
15464     }
15465   else
15466     {
15467       dump_die_for_error (die);
15468       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
15469              dwarf_attr_name (attr->name), objfile->name);
15470     }
15471
15472   /* If not cached we need to read it in.  */
15473
15474   if (this_type == NULL)
15475     {
15476       struct die_info *type_die;
15477       struct dwarf2_cu *type_cu = cu;
15478
15479       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
15480       /* If we found the type now, it's probably because the type came
15481          from an inter-CU reference and the type's CU got expanded before
15482          ours.  */
15483       this_type = get_die_type (type_die, type_cu);
15484       if (this_type == NULL)
15485         this_type = read_type_die_1 (type_die, type_cu);
15486     }
15487
15488   /* If we still don't have a type use an error marker.  */
15489
15490   if (this_type == NULL)
15491     {
15492       char *message, *saved;
15493
15494       /* read_type_die already issued a complaint.  */
15495       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
15496                             objfile->name,
15497                             cu->header.offset.sect_off,
15498                             die->offset.sect_off);
15499       saved = obstack_copy0 (&objfile->objfile_obstack,
15500                              message, strlen (message));
15501       xfree (message);
15502
15503       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
15504     }
15505
15506   return this_type;
15507 }
15508
15509 /* Return the type in DIE, CU.
15510    Returns NULL for invalid types.
15511
15512    This first does a lookup in the appropriate type_hash table,
15513    and only reads the die in if necessary.
15514
15515    NOTE: This can be called when reading in partial or full symbols.  */
15516
15517 static struct type *
15518 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
15519 {
15520   struct type *this_type;
15521
15522   this_type = get_die_type (die, cu);
15523   if (this_type)
15524     return this_type;
15525
15526   return read_type_die_1 (die, cu);
15527 }
15528
15529 /* Read the type in DIE, CU.
15530    Returns NULL for invalid types.  */
15531
15532 static struct type *
15533 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
15534 {
15535   struct type *this_type = NULL;
15536
15537   switch (die->tag)
15538     {
15539     case DW_TAG_class_type:
15540     case DW_TAG_interface_type:
15541     case DW_TAG_structure_type:
15542     case DW_TAG_union_type:
15543       this_type = read_structure_type (die, cu);
15544       break;
15545     case DW_TAG_enumeration_type:
15546       this_type = read_enumeration_type (die, cu);
15547       break;
15548     case DW_TAG_subprogram:
15549     case DW_TAG_subroutine_type:
15550     case DW_TAG_inlined_subroutine:
15551       this_type = read_subroutine_type (die, cu);
15552       break;
15553     case DW_TAG_array_type:
15554       this_type = read_array_type (die, cu);
15555       break;
15556     case DW_TAG_set_type:
15557       this_type = read_set_type (die, cu);
15558       break;
15559     case DW_TAG_pointer_type:
15560       this_type = read_tag_pointer_type (die, cu);
15561       break;
15562     case DW_TAG_ptr_to_member_type:
15563       this_type = read_tag_ptr_to_member_type (die, cu);
15564       break;
15565     case DW_TAG_reference_type:
15566       this_type = read_tag_reference_type (die, cu);
15567       break;
15568     case DW_TAG_const_type:
15569       this_type = read_tag_const_type (die, cu);
15570       break;
15571     case DW_TAG_volatile_type:
15572       this_type = read_tag_volatile_type (die, cu);
15573       break;
15574     case DW_TAG_string_type:
15575       this_type = read_tag_string_type (die, cu);
15576       break;
15577     case DW_TAG_typedef:
15578       this_type = read_typedef (die, cu);
15579       break;
15580     case DW_TAG_subrange_type:
15581       this_type = read_subrange_type (die, cu);
15582       break;
15583     case DW_TAG_base_type:
15584       this_type = read_base_type (die, cu);
15585       break;
15586     case DW_TAG_unspecified_type:
15587       this_type = read_unspecified_type (die, cu);
15588       break;
15589     case DW_TAG_namespace:
15590       this_type = read_namespace_type (die, cu);
15591       break;
15592     case DW_TAG_module:
15593       this_type = read_module_type (die, cu);
15594       break;
15595     default:
15596       complaint (&symfile_complaints,
15597                  _("unexpected tag in read_type_die: '%s'"),
15598                  dwarf_tag_name (die->tag));
15599       break;
15600     }
15601
15602   return this_type;
15603 }
15604
15605 /* See if we can figure out if the class lives in a namespace.  We do
15606    this by looking for a member function; its demangled name will
15607    contain namespace info, if there is any.
15608    Return the computed name or NULL.
15609    Space for the result is allocated on the objfile's obstack.
15610    This is the full-die version of guess_partial_die_structure_name.
15611    In this case we know DIE has no useful parent.  */
15612
15613 static char *
15614 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
15615 {
15616   struct die_info *spec_die;
15617   struct dwarf2_cu *spec_cu;
15618   struct die_info *child;
15619
15620   spec_cu = cu;
15621   spec_die = die_specification (die, &spec_cu);
15622   if (spec_die != NULL)
15623     {
15624       die = spec_die;
15625       cu = spec_cu;
15626     }
15627
15628   for (child = die->child;
15629        child != NULL;
15630        child = child->sibling)
15631     {
15632       if (child->tag == DW_TAG_subprogram)
15633         {
15634           struct attribute *attr;
15635
15636           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
15637           if (attr == NULL)
15638             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
15639           if (attr != NULL)
15640             {
15641               char *actual_name
15642                 = language_class_name_from_physname (cu->language_defn,
15643                                                      DW_STRING (attr));
15644               char *name = NULL;
15645
15646               if (actual_name != NULL)
15647                 {
15648                   char *die_name = dwarf2_name (die, cu);
15649
15650                   if (die_name != NULL
15651                       && strcmp (die_name, actual_name) != 0)
15652                     {
15653                       /* Strip off the class name from the full name.
15654                          We want the prefix.  */
15655                       int die_name_len = strlen (die_name);
15656                       int actual_name_len = strlen (actual_name);
15657
15658                       /* Test for '::' as a sanity check.  */
15659                       if (actual_name_len > die_name_len + 2
15660                           && actual_name[actual_name_len
15661                                          - die_name_len - 1] == ':')
15662                         name =
15663                           obsavestring (actual_name,
15664                                         actual_name_len - die_name_len - 2,
15665                                         &cu->objfile->objfile_obstack);
15666                     }
15667                 }
15668               xfree (actual_name);
15669               return name;
15670             }
15671         }
15672     }
15673
15674   return NULL;
15675 }
15676
15677 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
15678    prefix part in such case.  See
15679    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
15680
15681 static char *
15682 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
15683 {
15684   struct attribute *attr;
15685   char *base;
15686
15687   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
15688       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
15689     return NULL;
15690
15691   attr = dwarf2_attr (die, DW_AT_name, cu);
15692   if (attr != NULL && DW_STRING (attr) != NULL)
15693     return NULL;
15694
15695   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
15696   if (attr == NULL)
15697     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
15698   if (attr == NULL || DW_STRING (attr) == NULL)
15699     return NULL;
15700
15701   /* dwarf2_name had to be already called.  */
15702   gdb_assert (DW_STRING_IS_CANONICAL (attr));
15703
15704   /* Strip the base name, keep any leading namespaces/classes.  */
15705   base = strrchr (DW_STRING (attr), ':');
15706   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
15707     return "";
15708
15709   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
15710                        &cu->objfile->objfile_obstack);
15711 }
15712
15713 /* Return the name of the namespace/class that DIE is defined within,
15714    or "" if we can't tell.  The caller should not xfree the result.
15715
15716    For example, if we're within the method foo() in the following
15717    code:
15718
15719    namespace N {
15720      class C {
15721        void foo () {
15722        }
15723      };
15724    }
15725
15726    then determine_prefix on foo's die will return "N::C".  */
15727
15728 static const char *
15729 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
15730 {
15731   struct die_info *parent, *spec_die;
15732   struct dwarf2_cu *spec_cu;
15733   struct type *parent_type;
15734   char *retval;
15735
15736   if (cu->language != language_cplus && cu->language != language_java
15737       && cu->language != language_fortran)
15738     return "";
15739
15740   retval = anonymous_struct_prefix (die, cu);
15741   if (retval)
15742     return retval;
15743
15744   /* We have to be careful in the presence of DW_AT_specification.
15745      For example, with GCC 3.4, given the code
15746
15747      namespace N {
15748        void foo() {
15749          // Definition of N::foo.
15750        }
15751      }
15752
15753      then we'll have a tree of DIEs like this:
15754
15755      1: DW_TAG_compile_unit
15756        2: DW_TAG_namespace        // N
15757          3: DW_TAG_subprogram     // declaration of N::foo
15758        4: DW_TAG_subprogram       // definition of N::foo
15759             DW_AT_specification   // refers to die #3
15760
15761      Thus, when processing die #4, we have to pretend that we're in
15762      the context of its DW_AT_specification, namely the contex of die
15763      #3.  */
15764   spec_cu = cu;
15765   spec_die = die_specification (die, &spec_cu);
15766   if (spec_die == NULL)
15767     parent = die->parent;
15768   else
15769     {
15770       parent = spec_die->parent;
15771       cu = spec_cu;
15772     }
15773
15774   if (parent == NULL)
15775     return "";
15776   else if (parent->building_fullname)
15777     {
15778       const char *name;
15779       const char *parent_name;
15780
15781       /* It has been seen on RealView 2.2 built binaries,
15782          DW_TAG_template_type_param types actually _defined_ as
15783          children of the parent class:
15784
15785          enum E {};
15786          template class <class Enum> Class{};
15787          Class<enum E> class_e;
15788
15789          1: DW_TAG_class_type (Class)
15790            2: DW_TAG_enumeration_type (E)
15791              3: DW_TAG_enumerator (enum1:0)
15792              3: DW_TAG_enumerator (enum2:1)
15793              ...
15794            2: DW_TAG_template_type_param
15795               DW_AT_type  DW_FORM_ref_udata (E)
15796
15797          Besides being broken debug info, it can put GDB into an
15798          infinite loop.  Consider:
15799
15800          When we're building the full name for Class<E>, we'll start
15801          at Class, and go look over its template type parameters,
15802          finding E.  We'll then try to build the full name of E, and
15803          reach here.  We're now trying to build the full name of E,
15804          and look over the parent DIE for containing scope.  In the
15805          broken case, if we followed the parent DIE of E, we'd again
15806          find Class, and once again go look at its template type
15807          arguments, etc., etc.  Simply don't consider such parent die
15808          as source-level parent of this die (it can't be, the language
15809          doesn't allow it), and break the loop here.  */
15810       name = dwarf2_name (die, cu);
15811       parent_name = dwarf2_name (parent, cu);
15812       complaint (&symfile_complaints,
15813                  _("template param type '%s' defined within parent '%s'"),
15814                  name ? name : "<unknown>",
15815                  parent_name ? parent_name : "<unknown>");
15816       return "";
15817     }
15818   else
15819     switch (parent->tag)
15820       {
15821       case DW_TAG_namespace:
15822         parent_type = read_type_die (parent, cu);
15823         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
15824            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
15825            Work around this problem here.  */
15826         if (cu->language == language_cplus
15827             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
15828           return "";
15829         /* We give a name to even anonymous namespaces.  */
15830         return TYPE_TAG_NAME (parent_type);
15831       case DW_TAG_class_type:
15832       case DW_TAG_interface_type:
15833       case DW_TAG_structure_type:
15834       case DW_TAG_union_type:
15835       case DW_TAG_module:
15836         parent_type = read_type_die (parent, cu);
15837         if (TYPE_TAG_NAME (parent_type) != NULL)
15838           return TYPE_TAG_NAME (parent_type);
15839         else
15840           /* An anonymous structure is only allowed non-static data
15841              members; no typedefs, no member functions, et cetera.
15842              So it does not need a prefix.  */
15843           return "";
15844       case DW_TAG_compile_unit:
15845       case DW_TAG_partial_unit:
15846         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
15847         if (cu->language == language_cplus
15848             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15849             && die->child != NULL
15850             && (die->tag == DW_TAG_class_type
15851                 || die->tag == DW_TAG_structure_type
15852                 || die->tag == DW_TAG_union_type))
15853           {
15854             char *name = guess_full_die_structure_name (die, cu);
15855             if (name != NULL)
15856               return name;
15857           }
15858         return "";
15859       default:
15860         return determine_prefix (parent, cu);
15861       }
15862 }
15863
15864 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
15865    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
15866    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
15867    an obconcat, otherwise allocate storage for the result.  The CU argument is
15868    used to determine the language and hence, the appropriate separator.  */
15869
15870 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
15871
15872 static char *
15873 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
15874                  int physname, struct dwarf2_cu *cu)
15875 {
15876   const char *lead = "";
15877   const char *sep;
15878
15879   if (suffix == NULL || suffix[0] == '\0'
15880       || prefix == NULL || prefix[0] == '\0')
15881     sep = "";
15882   else if (cu->language == language_java)
15883     sep = ".";
15884   else if (cu->language == language_fortran && physname)
15885     {
15886       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
15887          DW_AT_MIPS_linkage_name is preferred and used instead.  */
15888
15889       lead = "__";
15890       sep = "_MOD_";
15891     }
15892   else
15893     sep = "::";
15894
15895   if (prefix == NULL)
15896     prefix = "";
15897   if (suffix == NULL)
15898     suffix = "";
15899
15900   if (obs == NULL)
15901     {
15902       char *retval
15903         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
15904
15905       strcpy (retval, lead);
15906       strcat (retval, prefix);
15907       strcat (retval, sep);
15908       strcat (retval, suffix);
15909       return retval;
15910     }
15911   else
15912     {
15913       /* We have an obstack.  */
15914       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
15915     }
15916 }
15917
15918 /* Return sibling of die, NULL if no sibling.  */
15919
15920 static struct die_info *
15921 sibling_die (struct die_info *die)
15922 {
15923   return die->sibling;
15924 }
15925
15926 /* Get name of a die, return NULL if not found.  */
15927
15928 static char *
15929 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
15930                           struct obstack *obstack)
15931 {
15932   if (name && cu->language == language_cplus)
15933     {
15934       char *canon_name = cp_canonicalize_string (name);
15935
15936       if (canon_name != NULL)
15937         {
15938           if (strcmp (canon_name, name) != 0)
15939             name = obsavestring (canon_name, strlen (canon_name),
15940                                  obstack);
15941           xfree (canon_name);
15942         }
15943     }
15944
15945   return name;
15946 }
15947
15948 /* Get name of a die, return NULL if not found.  */
15949
15950 static char *
15951 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
15952 {
15953   struct attribute *attr;
15954
15955   attr = dwarf2_attr (die, DW_AT_name, cu);
15956   if ((!attr || !DW_STRING (attr))
15957       && die->tag != DW_TAG_class_type
15958       && die->tag != DW_TAG_interface_type
15959       && die->tag != DW_TAG_structure_type
15960       && die->tag != DW_TAG_union_type)
15961     return NULL;
15962
15963   switch (die->tag)
15964     {
15965     case DW_TAG_compile_unit:
15966     case DW_TAG_partial_unit:
15967       /* Compilation units have a DW_AT_name that is a filename, not
15968          a source language identifier.  */
15969     case DW_TAG_enumeration_type:
15970     case DW_TAG_enumerator:
15971       /* These tags always have simple identifiers already; no need
15972          to canonicalize them.  */
15973       return DW_STRING (attr);
15974
15975     case DW_TAG_subprogram:
15976       /* Java constructors will all be named "<init>", so return
15977          the class name when we see this special case.  */
15978       if (cu->language == language_java
15979           && DW_STRING (attr) != NULL
15980           && strcmp (DW_STRING (attr), "<init>") == 0)
15981         {
15982           struct dwarf2_cu *spec_cu = cu;
15983           struct die_info *spec_die;
15984
15985           /* GCJ will output '<init>' for Java constructor names.
15986              For this special case, return the name of the parent class.  */
15987
15988           /* GCJ may output suprogram DIEs with AT_specification set.
15989              If so, use the name of the specified DIE.  */
15990           spec_die = die_specification (die, &spec_cu);
15991           if (spec_die != NULL)
15992             return dwarf2_name (spec_die, spec_cu);
15993
15994           do
15995             {
15996               die = die->parent;
15997               if (die->tag == DW_TAG_class_type)
15998                 return dwarf2_name (die, cu);
15999             }
16000           while (die->tag != DW_TAG_compile_unit
16001                  && die->tag != DW_TAG_partial_unit);
16002         }
16003       break;
16004
16005     case DW_TAG_class_type:
16006     case DW_TAG_interface_type:
16007     case DW_TAG_structure_type:
16008     case DW_TAG_union_type:
16009       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
16010          structures or unions.  These were of the form "._%d" in GCC 4.1,
16011          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
16012          and GCC 4.4.  We work around this problem by ignoring these.  */
16013       if (attr && DW_STRING (attr)
16014           && (strncmp (DW_STRING (attr), "._", 2) == 0
16015               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
16016         return NULL;
16017
16018       /* GCC might emit a nameless typedef that has a linkage name.  See
16019          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
16020       if (!attr || DW_STRING (attr) == NULL)
16021         {
16022           char *demangled = NULL;
16023
16024           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
16025           if (attr == NULL)
16026             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
16027
16028           if (attr == NULL || DW_STRING (attr) == NULL)
16029             return NULL;
16030
16031           /* Avoid demangling DW_STRING (attr) the second time on a second
16032              call for the same DIE.  */
16033           if (!DW_STRING_IS_CANONICAL (attr))
16034             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
16035
16036           if (demangled)
16037             {
16038               char *base;
16039
16040               /* FIXME: we already did this for the partial symbol... */
16041               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
16042                                                &cu->objfile->objfile_obstack);
16043               DW_STRING_IS_CANONICAL (attr) = 1;
16044               xfree (demangled);
16045
16046               /* Strip any leading namespaces/classes, keep only the base name.
16047                  DW_AT_name for named DIEs does not contain the prefixes.  */
16048               base = strrchr (DW_STRING (attr), ':');
16049               if (base && base > DW_STRING (attr) && base[-1] == ':')
16050                 return &base[1];
16051               else
16052                 return DW_STRING (attr);
16053             }
16054         }
16055       break;
16056
16057     default:
16058       break;
16059     }
16060
16061   if (!DW_STRING_IS_CANONICAL (attr))
16062     {
16063       DW_STRING (attr)
16064         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
16065                                     &cu->objfile->objfile_obstack);
16066       DW_STRING_IS_CANONICAL (attr) = 1;
16067     }
16068   return DW_STRING (attr);
16069 }
16070
16071 /* Return the die that this die in an extension of, or NULL if there
16072    is none.  *EXT_CU is the CU containing DIE on input, and the CU
16073    containing the return value on output.  */
16074
16075 static struct die_info *
16076 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
16077 {
16078   struct attribute *attr;
16079
16080   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
16081   if (attr == NULL)
16082     return NULL;
16083
16084   return follow_die_ref (die, attr, ext_cu);
16085 }
16086
16087 /* Convert a DIE tag into its string name.  */
16088
16089 static const char *
16090 dwarf_tag_name (unsigned tag)
16091 {
16092   const char *name = get_DW_TAG_name (tag);
16093
16094   if (name == NULL)
16095     return "DW_TAG_<unknown>";
16096
16097   return name;
16098 }
16099
16100 /* Convert a DWARF attribute code into its string name.  */
16101
16102 static const char *
16103 dwarf_attr_name (unsigned attr)
16104 {
16105   const char *name;
16106
16107 #ifdef MIPS /* collides with DW_AT_HP_block_index */
16108   if (attr == DW_AT_MIPS_fde)
16109     return "DW_AT_MIPS_fde";
16110 #else
16111   if (attr == DW_AT_HP_block_index)
16112     return "DW_AT_HP_block_index";
16113 #endif
16114
16115   name = get_DW_AT_name (attr);
16116
16117   if (name == NULL)
16118     return "DW_AT_<unknown>";
16119
16120   return name;
16121 }
16122
16123 /* Convert a DWARF value form code into its string name.  */
16124
16125 static const char *
16126 dwarf_form_name (unsigned form)
16127 {
16128   const char *name = get_DW_FORM_name (form);
16129
16130   if (name == NULL)
16131     return "DW_FORM_<unknown>";
16132
16133   return name;
16134 }
16135
16136 static char *
16137 dwarf_bool_name (unsigned mybool)
16138 {
16139   if (mybool)
16140     return "TRUE";
16141   else
16142     return "FALSE";
16143 }
16144
16145 /* Convert a DWARF type code into its string name.  */
16146
16147 static const char *
16148 dwarf_type_encoding_name (unsigned enc)
16149 {
16150   const char *name = get_DW_ATE_name (enc);
16151
16152   if (name == NULL)
16153     return "DW_ATE_<unknown>";
16154
16155   return name;
16156 }
16157
16158 static void
16159 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
16160 {
16161   unsigned int i;
16162
16163   print_spaces (indent, f);
16164   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
16165            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
16166
16167   if (die->parent != NULL)
16168     {
16169       print_spaces (indent, f);
16170       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
16171                           die->parent->offset.sect_off);
16172     }
16173
16174   print_spaces (indent, f);
16175   fprintf_unfiltered (f, "  has children: %s\n",
16176            dwarf_bool_name (die->child != NULL));
16177
16178   print_spaces (indent, f);
16179   fprintf_unfiltered (f, "  attributes:\n");
16180
16181   for (i = 0; i < die->num_attrs; ++i)
16182     {
16183       print_spaces (indent, f);
16184       fprintf_unfiltered (f, "    %s (%s) ",
16185                dwarf_attr_name (die->attrs[i].name),
16186                dwarf_form_name (die->attrs[i].form));
16187
16188       switch (die->attrs[i].form)
16189         {
16190         case DW_FORM_addr:
16191         case DW_FORM_GNU_addr_index:
16192           fprintf_unfiltered (f, "address: ");
16193           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
16194           break;
16195         case DW_FORM_block2:
16196         case DW_FORM_block4:
16197         case DW_FORM_block:
16198         case DW_FORM_block1:
16199           fprintf_unfiltered (f, "block: size %s",
16200                               pulongest (DW_BLOCK (&die->attrs[i])->size));
16201           break;
16202         case DW_FORM_exprloc:
16203           fprintf_unfiltered (f, "expression: size %s",
16204                               pulongest (DW_BLOCK (&die->attrs[i])->size));
16205           break;
16206         case DW_FORM_ref_addr:
16207           fprintf_unfiltered (f, "ref address: ");
16208           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16209           break;
16210         case DW_FORM_GNU_ref_alt:
16211           fprintf_unfiltered (f, "alt ref address: ");
16212           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
16213           break;
16214         case DW_FORM_ref1:
16215         case DW_FORM_ref2:
16216         case DW_FORM_ref4:
16217         case DW_FORM_ref8:
16218         case DW_FORM_ref_udata:
16219           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
16220                               (long) (DW_UNSND (&die->attrs[i])));
16221           break;
16222         case DW_FORM_data1:
16223         case DW_FORM_data2:
16224         case DW_FORM_data4:
16225         case DW_FORM_data8:
16226         case DW_FORM_udata:
16227         case DW_FORM_sdata:
16228           fprintf_unfiltered (f, "constant: %s",
16229                               pulongest (DW_UNSND (&die->attrs[i])));
16230           break;
16231         case DW_FORM_sec_offset:
16232           fprintf_unfiltered (f, "section offset: %s",
16233                               pulongest (DW_UNSND (&die->attrs[i])));
16234           break;
16235         case DW_FORM_ref_sig8:
16236           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
16237             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
16238                          DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
16239           else
16240             fprintf_unfiltered (f, "signatured type, offset: unknown");
16241           break;
16242         case DW_FORM_string:
16243         case DW_FORM_strp:
16244         case DW_FORM_GNU_str_index:
16245         case DW_FORM_GNU_strp_alt:
16246           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
16247                    DW_STRING (&die->attrs[i])
16248                    ? DW_STRING (&die->attrs[i]) : "",
16249                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
16250           break;
16251         case DW_FORM_flag:
16252           if (DW_UNSND (&die->attrs[i]))
16253             fprintf_unfiltered (f, "flag: TRUE");
16254           else
16255             fprintf_unfiltered (f, "flag: FALSE");
16256           break;
16257         case DW_FORM_flag_present:
16258           fprintf_unfiltered (f, "flag: TRUE");
16259           break;
16260         case DW_FORM_indirect:
16261           /* The reader will have reduced the indirect form to
16262              the "base form" so this form should not occur.  */
16263           fprintf_unfiltered (f, 
16264                               "unexpected attribute form: DW_FORM_indirect");
16265           break;
16266         default:
16267           fprintf_unfiltered (f, "unsupported attribute form: %d.",
16268                    die->attrs[i].form);
16269           break;
16270         }
16271       fprintf_unfiltered (f, "\n");
16272     }
16273 }
16274
16275 static void
16276 dump_die_for_error (struct die_info *die)
16277 {
16278   dump_die_shallow (gdb_stderr, 0, die);
16279 }
16280
16281 static void
16282 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
16283 {
16284   int indent = level * 4;
16285
16286   gdb_assert (die != NULL);
16287
16288   if (level >= max_level)
16289     return;
16290
16291   dump_die_shallow (f, indent, die);
16292
16293   if (die->child != NULL)
16294     {
16295       print_spaces (indent, f);
16296       fprintf_unfiltered (f, "  Children:");
16297       if (level + 1 < max_level)
16298         {
16299           fprintf_unfiltered (f, "\n");
16300           dump_die_1 (f, level + 1, max_level, die->child);
16301         }
16302       else
16303         {
16304           fprintf_unfiltered (f,
16305                               " [not printed, max nesting level reached]\n");
16306         }
16307     }
16308
16309   if (die->sibling != NULL && level > 0)
16310     {
16311       dump_die_1 (f, level, max_level, die->sibling);
16312     }
16313 }
16314
16315 /* This is called from the pdie macro in gdbinit.in.
16316    It's not static so gcc will keep a copy callable from gdb.  */
16317
16318 void
16319 dump_die (struct die_info *die, int max_level)
16320 {
16321   dump_die_1 (gdb_stdlog, 0, max_level, die);
16322 }
16323
16324 static void
16325 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
16326 {
16327   void **slot;
16328
16329   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
16330                                    INSERT);
16331
16332   *slot = die;
16333 }
16334
16335 /* DW_ADDR is always stored already as sect_offset; despite for the forms
16336    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
16337
16338 static int
16339 is_ref_attr (struct attribute *attr)
16340 {
16341   switch (attr->form)
16342     {
16343     case DW_FORM_ref_addr:
16344     case DW_FORM_ref1:
16345     case DW_FORM_ref2:
16346     case DW_FORM_ref4:
16347     case DW_FORM_ref8:
16348     case DW_FORM_ref_udata:
16349     case DW_FORM_GNU_ref_alt:
16350       return 1;
16351     default:
16352       return 0;
16353     }
16354 }
16355
16356 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
16357    required kind.  */
16358
16359 static sect_offset
16360 dwarf2_get_ref_die_offset (struct attribute *attr)
16361 {
16362   sect_offset retval = { DW_UNSND (attr) };
16363
16364   if (is_ref_attr (attr))
16365     return retval;
16366
16367   retval.sect_off = 0;
16368   complaint (&symfile_complaints,
16369              _("unsupported die ref attribute form: '%s'"),
16370              dwarf_form_name (attr->form));
16371   return retval;
16372 }
16373
16374 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
16375  * the value held by the attribute is not constant.  */
16376
16377 static LONGEST
16378 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
16379 {
16380   if (attr->form == DW_FORM_sdata)
16381     return DW_SND (attr);
16382   else if (attr->form == DW_FORM_udata
16383            || attr->form == DW_FORM_data1
16384            || attr->form == DW_FORM_data2
16385            || attr->form == DW_FORM_data4
16386            || attr->form == DW_FORM_data8)
16387     return DW_UNSND (attr);
16388   else
16389     {
16390       complaint (&symfile_complaints,
16391                  _("Attribute value is not a constant (%s)"),
16392                  dwarf_form_name (attr->form));
16393       return default_value;
16394     }
16395 }
16396
16397 /* Follow reference or signature attribute ATTR of SRC_DIE.
16398    On entry *REF_CU is the CU of SRC_DIE.
16399    On exit *REF_CU is the CU of the result.  */
16400
16401 static struct die_info *
16402 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
16403                        struct dwarf2_cu **ref_cu)
16404 {
16405   struct die_info *die;
16406
16407   if (is_ref_attr (attr))
16408     die = follow_die_ref (src_die, attr, ref_cu);
16409   else if (attr->form == DW_FORM_ref_sig8)
16410     die = follow_die_sig (src_die, attr, ref_cu);
16411   else
16412     {
16413       dump_die_for_error (src_die);
16414       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
16415              (*ref_cu)->objfile->name);
16416     }
16417
16418   return die;
16419 }
16420
16421 /* Follow reference OFFSET.
16422    On entry *REF_CU is the CU of the source die referencing OFFSET.
16423    On exit *REF_CU is the CU of the result.
16424    Returns NULL if OFFSET is invalid.  */
16425
16426 static struct die_info *
16427 follow_die_offset (sect_offset offset, int offset_in_dwz,
16428                    struct dwarf2_cu **ref_cu)
16429 {
16430   struct die_info temp_die;
16431   struct dwarf2_cu *target_cu, *cu = *ref_cu;
16432
16433   gdb_assert (cu->per_cu != NULL);
16434
16435   target_cu = cu;
16436
16437   if (cu->per_cu->is_debug_types)
16438     {
16439       /* .debug_types CUs cannot reference anything outside their CU.
16440          If they need to, they have to reference a signatured type via
16441          DW_FORM_ref_sig8.  */
16442       if (! offset_in_cu_p (&cu->header, offset))
16443         return NULL;
16444     }
16445   else if (offset_in_dwz != cu->per_cu->is_dwz
16446            || ! offset_in_cu_p (&cu->header, offset))
16447     {
16448       struct dwarf2_per_cu_data *per_cu;
16449
16450       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
16451                                                  cu->objfile);
16452
16453       /* If necessary, add it to the queue and load its DIEs.  */
16454       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
16455         load_full_comp_unit (per_cu, cu->language);
16456
16457       target_cu = per_cu->cu;
16458     }
16459   else if (cu->dies == NULL)
16460     {
16461       /* We're loading full DIEs during partial symbol reading.  */
16462       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
16463       load_full_comp_unit (cu->per_cu, language_minimal);
16464     }
16465
16466   *ref_cu = target_cu;
16467   temp_die.offset = offset;
16468   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
16469 }
16470
16471 /* Follow reference attribute ATTR of SRC_DIE.
16472    On entry *REF_CU is the CU of SRC_DIE.
16473    On exit *REF_CU is the CU of the result.  */
16474
16475 static struct die_info *
16476 follow_die_ref (struct die_info *src_die, struct attribute *attr,
16477                 struct dwarf2_cu **ref_cu)
16478 {
16479   sect_offset offset = dwarf2_get_ref_die_offset (attr);
16480   struct dwarf2_cu *cu = *ref_cu;
16481   struct die_info *die;
16482
16483   die = follow_die_offset (offset,
16484                            (attr->form == DW_FORM_GNU_ref_alt
16485                             || cu->per_cu->is_dwz),
16486                            ref_cu);
16487   if (!die)
16488     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
16489            "at 0x%x [in module %s]"),
16490            offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
16491
16492   return die;
16493 }
16494
16495 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
16496    Returned value is intended for DW_OP_call*.  Returned
16497    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
16498
16499 struct dwarf2_locexpr_baton
16500 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
16501                                  struct dwarf2_per_cu_data *per_cu,
16502                                  CORE_ADDR (*get_frame_pc) (void *baton),
16503                                  void *baton)
16504 {
16505   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
16506   struct dwarf2_cu *cu;
16507   struct die_info *die;
16508   struct attribute *attr;
16509   struct dwarf2_locexpr_baton retval;
16510
16511   dw2_setup (per_cu->objfile);
16512
16513   if (per_cu->cu == NULL)
16514     load_cu (per_cu);
16515   cu = per_cu->cu;
16516
16517   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
16518   if (!die)
16519     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
16520            offset.sect_off, per_cu->objfile->name);
16521
16522   attr = dwarf2_attr (die, DW_AT_location, cu);
16523   if (!attr)
16524     {
16525       /* DWARF: "If there is no such attribute, then there is no effect.".
16526          DATA is ignored if SIZE is 0.  */
16527
16528       retval.data = NULL;
16529       retval.size = 0;
16530     }
16531   else if (attr_form_is_section_offset (attr))
16532     {
16533       struct dwarf2_loclist_baton loclist_baton;
16534       CORE_ADDR pc = (*get_frame_pc) (baton);
16535       size_t size;
16536
16537       fill_in_loclist_baton (cu, &loclist_baton, attr);
16538
16539       retval.data = dwarf2_find_location_expression (&loclist_baton,
16540                                                      &size, pc);
16541       retval.size = size;
16542     }
16543   else
16544     {
16545       if (!attr_form_is_block (attr))
16546         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
16547                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
16548                offset.sect_off, per_cu->objfile->name);
16549
16550       retval.data = DW_BLOCK (attr)->data;
16551       retval.size = DW_BLOCK (attr)->size;
16552     }
16553   retval.per_cu = cu->per_cu;
16554
16555   age_cached_comp_units ();
16556
16557   return retval;
16558 }
16559
16560 /* Return the type of the DIE at DIE_OFFSET in the CU named by
16561    PER_CU.  */
16562
16563 struct type *
16564 dwarf2_get_die_type (cu_offset die_offset,
16565                      struct dwarf2_per_cu_data *per_cu)
16566 {
16567   sect_offset die_offset_sect;
16568
16569   dw2_setup (per_cu->objfile);
16570
16571   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
16572   return get_die_type_at_offset (die_offset_sect, per_cu);
16573 }
16574
16575 /* Follow the signature attribute ATTR in SRC_DIE.
16576    On entry *REF_CU is the CU of SRC_DIE.
16577    On exit *REF_CU is the CU of the result.  */
16578
16579 static struct die_info *
16580 follow_die_sig (struct die_info *src_die, struct attribute *attr,
16581                 struct dwarf2_cu **ref_cu)
16582 {
16583   struct objfile *objfile = (*ref_cu)->objfile;
16584   struct die_info temp_die;
16585   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
16586   struct dwarf2_cu *sig_cu;
16587   struct die_info *die;
16588
16589   /* sig_type will be NULL if the signatured type is missing from
16590      the debug info.  */
16591   if (sig_type == NULL)
16592     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
16593              "at 0x%x [in module %s]"),
16594            src_die->offset.sect_off, objfile->name);
16595
16596   /* If necessary, add it to the queue and load its DIEs.  */
16597
16598   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
16599     read_signatured_type (sig_type);
16600
16601   gdb_assert (sig_type->per_cu.cu != NULL);
16602
16603   sig_cu = sig_type->per_cu.cu;
16604   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
16605   temp_die.offset = sig_type->type_offset_in_section;
16606   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
16607                              temp_die.offset.sect_off);
16608   if (die)
16609     {
16610       *ref_cu = sig_cu;
16611       return die;
16612     }
16613
16614   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
16615          "from DIE at 0x%x [in module %s]"),
16616          temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
16617 }
16618
16619 /* Given an offset of a signatured type, return its signatured_type.  */
16620
16621 static struct signatured_type *
16622 lookup_signatured_type_at_offset (struct objfile *objfile,
16623                                   struct dwarf2_section_info *section,
16624                                   sect_offset offset)
16625 {
16626   gdb_byte *info_ptr = section->buffer + offset.sect_off;
16627   unsigned int length, initial_length_size;
16628   unsigned int sig_offset;
16629   struct signatured_type find_entry, *sig_type;
16630
16631   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
16632   sig_offset = (initial_length_size
16633                 + 2 /*version*/
16634                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
16635                 + 1 /*address_size*/);
16636   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
16637   sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
16638
16639   /* This is only used to lookup previously recorded types.
16640      If we didn't find it, it's our bug.  */
16641   gdb_assert (sig_type != NULL);
16642   gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
16643
16644   return sig_type;
16645 }
16646
16647 /* Load the DIEs associated with type unit PER_CU into memory.  */
16648
16649 static void
16650 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
16651 {
16652   struct signatured_type *sig_type;
16653
16654   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
16655   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
16656
16657   /* We have the per_cu, but we need the signatured_type.
16658      Fortunately this is an easy translation.  */
16659   gdb_assert (per_cu->is_debug_types);
16660   sig_type = (struct signatured_type *) per_cu;
16661
16662   gdb_assert (per_cu->cu == NULL);
16663
16664   read_signatured_type (sig_type);
16665
16666   gdb_assert (per_cu->cu != NULL);
16667 }
16668
16669 /* die_reader_func for read_signatured_type.
16670    This is identical to load_full_comp_unit_reader,
16671    but is kept separate for now.  */
16672
16673 static void
16674 read_signatured_type_reader (const struct die_reader_specs *reader,
16675                              gdb_byte *info_ptr,
16676                              struct die_info *comp_unit_die,
16677                              int has_children,
16678                              void *data)
16679 {
16680   struct dwarf2_cu *cu = reader->cu;
16681
16682   gdb_assert (cu->die_hash == NULL);
16683   cu->die_hash =
16684     htab_create_alloc_ex (cu->header.length / 12,
16685                           die_hash,
16686                           die_eq,
16687                           NULL,
16688                           &cu->comp_unit_obstack,
16689                           hashtab_obstack_allocate,
16690                           dummy_obstack_deallocate);
16691
16692   if (has_children)
16693     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
16694                                                   &info_ptr, comp_unit_die);
16695   cu->dies = comp_unit_die;
16696   /* comp_unit_die is not stored in die_hash, no need.  */
16697
16698   /* We try not to read any attributes in this function, because not
16699      all CUs needed for references have been loaded yet, and symbol
16700      table processing isn't initialized.  But we have to set the CU language,
16701      or we won't be able to build types correctly.
16702      Similarly, if we do not read the producer, we can not apply
16703      producer-specific interpretation.  */
16704   prepare_one_comp_unit (cu, cu->dies, language_minimal);
16705 }
16706
16707 /* Read in a signatured type and build its CU and DIEs.
16708    If the type is a stub for the real type in a DWO file,
16709    read in the real type from the DWO file as well.  */
16710
16711 static void
16712 read_signatured_type (struct signatured_type *sig_type)
16713 {
16714   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
16715
16716   gdb_assert (per_cu->is_debug_types);
16717   gdb_assert (per_cu->cu == NULL);
16718
16719   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
16720                            read_signatured_type_reader, NULL);
16721 }
16722
16723 /* Decode simple location descriptions.
16724    Given a pointer to a dwarf block that defines a location, compute
16725    the location and return the value.
16726
16727    NOTE drow/2003-11-18: This function is called in two situations
16728    now: for the address of static or global variables (partial symbols
16729    only) and for offsets into structures which are expected to be
16730    (more or less) constant.  The partial symbol case should go away,
16731    and only the constant case should remain.  That will let this
16732    function complain more accurately.  A few special modes are allowed
16733    without complaint for global variables (for instance, global
16734    register values and thread-local values).
16735
16736    A location description containing no operations indicates that the
16737    object is optimized out.  The return value is 0 for that case.
16738    FIXME drow/2003-11-16: No callers check for this case any more; soon all
16739    callers will only want a very basic result and this can become a
16740    complaint.
16741
16742    Note that stack[0] is unused except as a default error return.  */
16743
16744 static CORE_ADDR
16745 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
16746 {
16747   struct objfile *objfile = cu->objfile;
16748   size_t i;
16749   size_t size = blk->size;
16750   gdb_byte *data = blk->data;
16751   CORE_ADDR stack[64];
16752   int stacki;
16753   unsigned int bytes_read, unsnd;
16754   gdb_byte op;
16755
16756   i = 0;
16757   stacki = 0;
16758   stack[stacki] = 0;
16759   stack[++stacki] = 0;
16760
16761   while (i < size)
16762     {
16763       op = data[i++];
16764       switch (op)
16765         {
16766         case DW_OP_lit0:
16767         case DW_OP_lit1:
16768         case DW_OP_lit2:
16769         case DW_OP_lit3:
16770         case DW_OP_lit4:
16771         case DW_OP_lit5:
16772         case DW_OP_lit6:
16773         case DW_OP_lit7:
16774         case DW_OP_lit8:
16775         case DW_OP_lit9:
16776         case DW_OP_lit10:
16777         case DW_OP_lit11:
16778         case DW_OP_lit12:
16779         case DW_OP_lit13:
16780         case DW_OP_lit14:
16781         case DW_OP_lit15:
16782         case DW_OP_lit16:
16783         case DW_OP_lit17:
16784         case DW_OP_lit18:
16785         case DW_OP_lit19:
16786         case DW_OP_lit20:
16787         case DW_OP_lit21:
16788         case DW_OP_lit22:
16789         case DW_OP_lit23:
16790         case DW_OP_lit24:
16791         case DW_OP_lit25:
16792         case DW_OP_lit26:
16793         case DW_OP_lit27:
16794         case DW_OP_lit28:
16795         case DW_OP_lit29:
16796         case DW_OP_lit30:
16797         case DW_OP_lit31:
16798           stack[++stacki] = op - DW_OP_lit0;
16799           break;
16800
16801         case DW_OP_reg0:
16802         case DW_OP_reg1:
16803         case DW_OP_reg2:
16804         case DW_OP_reg3:
16805         case DW_OP_reg4:
16806         case DW_OP_reg5:
16807         case DW_OP_reg6:
16808         case DW_OP_reg7:
16809         case DW_OP_reg8:
16810         case DW_OP_reg9:
16811         case DW_OP_reg10:
16812         case DW_OP_reg11:
16813         case DW_OP_reg12:
16814         case DW_OP_reg13:
16815         case DW_OP_reg14:
16816         case DW_OP_reg15:
16817         case DW_OP_reg16:
16818         case DW_OP_reg17:
16819         case DW_OP_reg18:
16820         case DW_OP_reg19:
16821         case DW_OP_reg20:
16822         case DW_OP_reg21:
16823         case DW_OP_reg22:
16824         case DW_OP_reg23:
16825         case DW_OP_reg24:
16826         case DW_OP_reg25:
16827         case DW_OP_reg26:
16828         case DW_OP_reg27:
16829         case DW_OP_reg28:
16830         case DW_OP_reg29:
16831         case DW_OP_reg30:
16832         case DW_OP_reg31:
16833           stack[++stacki] = op - DW_OP_reg0;
16834           if (i < size)
16835             dwarf2_complex_location_expr_complaint ();
16836           break;
16837
16838         case DW_OP_regx:
16839           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
16840           i += bytes_read;
16841           stack[++stacki] = unsnd;
16842           if (i < size)
16843             dwarf2_complex_location_expr_complaint ();
16844           break;
16845
16846         case DW_OP_addr:
16847           stack[++stacki] = read_address (objfile->obfd, &data[i],
16848                                           cu, &bytes_read);
16849           i += bytes_read;
16850           break;
16851
16852         case DW_OP_const1u:
16853           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
16854           i += 1;
16855           break;
16856
16857         case DW_OP_const1s:
16858           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
16859           i += 1;
16860           break;
16861
16862         case DW_OP_const2u:
16863           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
16864           i += 2;
16865           break;
16866
16867         case DW_OP_const2s:
16868           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
16869           i += 2;
16870           break;
16871
16872         case DW_OP_const4u:
16873           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
16874           i += 4;
16875           break;
16876
16877         case DW_OP_const4s:
16878           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
16879           i += 4;
16880           break;
16881
16882         case DW_OP_const8u:
16883           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
16884           i += 8;
16885           break;
16886
16887         case DW_OP_constu:
16888           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
16889                                                   &bytes_read);
16890           i += bytes_read;
16891           break;
16892
16893         case DW_OP_consts:
16894           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
16895           i += bytes_read;
16896           break;
16897
16898         case DW_OP_dup:
16899           stack[stacki + 1] = stack[stacki];
16900           stacki++;
16901           break;
16902
16903         case DW_OP_plus:
16904           stack[stacki - 1] += stack[stacki];
16905           stacki--;
16906           break;
16907
16908         case DW_OP_plus_uconst:
16909           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
16910                                                  &bytes_read);
16911           i += bytes_read;
16912           break;
16913
16914         case DW_OP_minus:
16915           stack[stacki - 1] -= stack[stacki];
16916           stacki--;
16917           break;
16918
16919         case DW_OP_deref:
16920           /* If we're not the last op, then we definitely can't encode
16921              this using GDB's address_class enum.  This is valid for partial
16922              global symbols, although the variable's address will be bogus
16923              in the psymtab.  */
16924           if (i < size)
16925             dwarf2_complex_location_expr_complaint ();
16926           break;
16927
16928         case DW_OP_GNU_push_tls_address:
16929           /* The top of the stack has the offset from the beginning
16930              of the thread control block at which the variable is located.  */
16931           /* Nothing should follow this operator, so the top of stack would
16932              be returned.  */
16933           /* This is valid for partial global symbols, but the variable's
16934              address will be bogus in the psymtab.  Make it always at least
16935              non-zero to not look as a variable garbage collected by linker
16936              which have DW_OP_addr 0.  */
16937           if (i < size)
16938             dwarf2_complex_location_expr_complaint ();
16939           stack[stacki]++;
16940           break;
16941
16942         case DW_OP_GNU_uninit:
16943           break;
16944
16945         case DW_OP_GNU_addr_index:
16946         case DW_OP_GNU_const_index:
16947           stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
16948                                                          &bytes_read);
16949           i += bytes_read;
16950           break;
16951
16952         default:
16953           {
16954             const char *name = get_DW_OP_name (op);
16955
16956             if (name)
16957               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
16958                          name);
16959             else
16960               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
16961                          op);
16962           }
16963
16964           return (stack[stacki]);
16965         }
16966
16967       /* Enforce maximum stack depth of SIZE-1 to avoid writing
16968          outside of the allocated space.  Also enforce minimum>0.  */
16969       if (stacki >= ARRAY_SIZE (stack) - 1)
16970         {
16971           complaint (&symfile_complaints,
16972                      _("location description stack overflow"));
16973           return 0;
16974         }
16975
16976       if (stacki <= 0)
16977         {
16978           complaint (&symfile_complaints,
16979                      _("location description stack underflow"));
16980           return 0;
16981         }
16982     }
16983   return (stack[stacki]);
16984 }
16985
16986 /* memory allocation interface */
16987
16988 static struct dwarf_block *
16989 dwarf_alloc_block (struct dwarf2_cu *cu)
16990 {
16991   struct dwarf_block *blk;
16992
16993   blk = (struct dwarf_block *)
16994     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
16995   return (blk);
16996 }
16997
16998 static struct die_info *
16999 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
17000 {
17001   struct die_info *die;
17002   size_t size = sizeof (struct die_info);
17003
17004   if (num_attrs > 1)
17005     size += (num_attrs - 1) * sizeof (struct attribute);
17006
17007   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
17008   memset (die, 0, sizeof (struct die_info));
17009   return (die);
17010 }
17011
17012 \f
17013 /* Macro support.  */
17014
17015 /* Return the full name of file number I in *LH's file name table.
17016    Use COMP_DIR as the name of the current directory of the
17017    compilation.  The result is allocated using xmalloc; the caller is
17018    responsible for freeing it.  */
17019 static char *
17020 file_full_name (int file, struct line_header *lh, const char *comp_dir)
17021 {
17022   /* Is the file number a valid index into the line header's file name
17023      table?  Remember that file numbers start with one, not zero.  */
17024   if (1 <= file && file <= lh->num_file_names)
17025     {
17026       struct file_entry *fe = &lh->file_names[file - 1];
17027
17028       if (IS_ABSOLUTE_PATH (fe->name))
17029         return xstrdup (fe->name);
17030       else
17031         {
17032           const char *dir;
17033           int dir_len;
17034           char *full_name;
17035
17036           if (fe->dir_index)
17037             dir = lh->include_dirs[fe->dir_index - 1];
17038           else
17039             dir = comp_dir;
17040
17041           if (dir)
17042             {
17043               dir_len = strlen (dir);
17044               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
17045               strcpy (full_name, dir);
17046               full_name[dir_len] = '/';
17047               strcpy (full_name + dir_len + 1, fe->name);
17048               return full_name;
17049             }
17050           else
17051             return xstrdup (fe->name);
17052         }
17053     }
17054   else
17055     {
17056       /* The compiler produced a bogus file number.  We can at least
17057          record the macro definitions made in the file, even if we
17058          won't be able to find the file by name.  */
17059       char fake_name[80];
17060
17061       sprintf (fake_name, "<bad macro file number %d>", file);
17062
17063       complaint (&symfile_complaints,
17064                  _("bad file number in macro information (%d)"),
17065                  file);
17066
17067       return xstrdup (fake_name);
17068     }
17069 }
17070
17071
17072 static struct macro_source_file *
17073 macro_start_file (int file, int line,
17074                   struct macro_source_file *current_file,
17075                   const char *comp_dir,
17076                   struct line_header *lh, struct objfile *objfile)
17077 {
17078   /* The full name of this source file.  */
17079   char *full_name = file_full_name (file, lh, comp_dir);
17080
17081   /* We don't create a macro table for this compilation unit
17082      at all until we actually get a filename.  */
17083   if (! pending_macros)
17084     pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
17085                                       objfile->per_bfd->macro_cache);
17086
17087   if (! current_file)
17088     {
17089       /* If we have no current file, then this must be the start_file
17090          directive for the compilation unit's main source file.  */
17091       current_file = macro_set_main (pending_macros, full_name);
17092       macro_define_special (pending_macros);
17093     }
17094   else
17095     current_file = macro_include (current_file, line, full_name);
17096
17097   xfree (full_name);
17098
17099   return current_file;
17100 }
17101
17102
17103 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
17104    followed by a null byte.  */
17105 static char *
17106 copy_string (const char *buf, int len)
17107 {
17108   char *s = xmalloc (len + 1);
17109
17110   memcpy (s, buf, len);
17111   s[len] = '\0';
17112   return s;
17113 }
17114
17115
17116 static const char *
17117 consume_improper_spaces (const char *p, const char *body)
17118 {
17119   if (*p == ' ')
17120     {
17121       complaint (&symfile_complaints,
17122                  _("macro definition contains spaces "
17123                    "in formal argument list:\n`%s'"),
17124                  body);
17125
17126       while (*p == ' ')
17127         p++;
17128     }
17129
17130   return p;
17131 }
17132
17133
17134 static void
17135 parse_macro_definition (struct macro_source_file *file, int line,
17136                         const char *body)
17137 {
17138   const char *p;
17139
17140   /* The body string takes one of two forms.  For object-like macro
17141      definitions, it should be:
17142
17143         <macro name> " " <definition>
17144
17145      For function-like macro definitions, it should be:
17146
17147         <macro name> "() " <definition>
17148      or
17149         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
17150
17151      Spaces may appear only where explicitly indicated, and in the
17152      <definition>.
17153
17154      The Dwarf 2 spec says that an object-like macro's name is always
17155      followed by a space, but versions of GCC around March 2002 omit
17156      the space when the macro's definition is the empty string.
17157
17158      The Dwarf 2 spec says that there should be no spaces between the
17159      formal arguments in a function-like macro's formal argument list,
17160      but versions of GCC around March 2002 include spaces after the
17161      commas.  */
17162
17163
17164   /* Find the extent of the macro name.  The macro name is terminated
17165      by either a space or null character (for an object-like macro) or
17166      an opening paren (for a function-like macro).  */
17167   for (p = body; *p; p++)
17168     if (*p == ' ' || *p == '(')
17169       break;
17170
17171   if (*p == ' ' || *p == '\0')
17172     {
17173       /* It's an object-like macro.  */
17174       int name_len = p - body;
17175       char *name = copy_string (body, name_len);
17176       const char *replacement;
17177
17178       if (*p == ' ')
17179         replacement = body + name_len + 1;
17180       else
17181         {
17182           dwarf2_macro_malformed_definition_complaint (body);
17183           replacement = body + name_len;
17184         }
17185
17186       macro_define_object (file, line, name, replacement);
17187
17188       xfree (name);
17189     }
17190   else if (*p == '(')
17191     {
17192       /* It's a function-like macro.  */
17193       char *name = copy_string (body, p - body);
17194       int argc = 0;
17195       int argv_size = 1;
17196       char **argv = xmalloc (argv_size * sizeof (*argv));
17197
17198       p++;
17199
17200       p = consume_improper_spaces (p, body);
17201
17202       /* Parse the formal argument list.  */
17203       while (*p && *p != ')')
17204         {
17205           /* Find the extent of the current argument name.  */
17206           const char *arg_start = p;
17207
17208           while (*p && *p != ',' && *p != ')' && *p != ' ')
17209             p++;
17210
17211           if (! *p || p == arg_start)
17212             dwarf2_macro_malformed_definition_complaint (body);
17213           else
17214             {
17215               /* Make sure argv has room for the new argument.  */
17216               if (argc >= argv_size)
17217                 {
17218                   argv_size *= 2;
17219                   argv = xrealloc (argv, argv_size * sizeof (*argv));
17220                 }
17221
17222               argv[argc++] = copy_string (arg_start, p - arg_start);
17223             }
17224
17225           p = consume_improper_spaces (p, body);
17226
17227           /* Consume the comma, if present.  */
17228           if (*p == ',')
17229             {
17230               p++;
17231
17232               p = consume_improper_spaces (p, body);
17233             }
17234         }
17235
17236       if (*p == ')')
17237         {
17238           p++;
17239
17240           if (*p == ' ')
17241             /* Perfectly formed definition, no complaints.  */
17242             macro_define_function (file, line, name,
17243                                    argc, (const char **) argv,
17244                                    p + 1);
17245           else if (*p == '\0')
17246             {
17247               /* Complain, but do define it.  */
17248               dwarf2_macro_malformed_definition_complaint (body);
17249               macro_define_function (file, line, name,
17250                                      argc, (const char **) argv,
17251                                      p);
17252             }
17253           else
17254             /* Just complain.  */
17255             dwarf2_macro_malformed_definition_complaint (body);
17256         }
17257       else
17258         /* Just complain.  */
17259         dwarf2_macro_malformed_definition_complaint (body);
17260
17261       xfree (name);
17262       {
17263         int i;
17264
17265         for (i = 0; i < argc; i++)
17266           xfree (argv[i]);
17267       }
17268       xfree (argv);
17269     }
17270   else
17271     dwarf2_macro_malformed_definition_complaint (body);
17272 }
17273
17274 /* Skip some bytes from BYTES according to the form given in FORM.
17275    Returns the new pointer.  */
17276
17277 static gdb_byte *
17278 skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
17279                  enum dwarf_form form,
17280                  unsigned int offset_size,
17281                  struct dwarf2_section_info *section)
17282 {
17283   unsigned int bytes_read;
17284
17285   switch (form)
17286     {
17287     case DW_FORM_data1:
17288     case DW_FORM_flag:
17289       ++bytes;
17290       break;
17291
17292     case DW_FORM_data2:
17293       bytes += 2;
17294       break;
17295
17296     case DW_FORM_data4:
17297       bytes += 4;
17298       break;
17299
17300     case DW_FORM_data8:
17301       bytes += 8;
17302       break;
17303
17304     case DW_FORM_string:
17305       read_direct_string (abfd, bytes, &bytes_read);
17306       bytes += bytes_read;
17307       break;
17308
17309     case DW_FORM_sec_offset:
17310     case DW_FORM_strp:
17311     case DW_FORM_GNU_strp_alt:
17312       bytes += offset_size;
17313       break;
17314
17315     case DW_FORM_block:
17316       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
17317       bytes += bytes_read;
17318       break;
17319
17320     case DW_FORM_block1:
17321       bytes += 1 + read_1_byte (abfd, bytes);
17322       break;
17323     case DW_FORM_block2:
17324       bytes += 2 + read_2_bytes (abfd, bytes);
17325       break;
17326     case DW_FORM_block4:
17327       bytes += 4 + read_4_bytes (abfd, bytes);
17328       break;
17329
17330     case DW_FORM_sdata:
17331     case DW_FORM_udata:
17332     case DW_FORM_GNU_addr_index:
17333     case DW_FORM_GNU_str_index:
17334       bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
17335       if (bytes == NULL)
17336         {
17337           dwarf2_section_buffer_overflow_complaint (section);
17338           return NULL;
17339         }
17340       break;
17341
17342     default:
17343       {
17344       complain:
17345         complaint (&symfile_complaints,
17346                    _("invalid form 0x%x in `%s'"),
17347                    form,
17348                    section->asection->name);
17349         return NULL;
17350       }
17351     }
17352
17353   return bytes;
17354 }
17355
17356 /* A helper for dwarf_decode_macros that handles skipping an unknown
17357    opcode.  Returns an updated pointer to the macro data buffer; or,
17358    on error, issues a complaint and returns NULL.  */
17359
17360 static gdb_byte *
17361 skip_unknown_opcode (unsigned int opcode,
17362                      gdb_byte **opcode_definitions,
17363                      gdb_byte *mac_ptr, gdb_byte *mac_end,
17364                      bfd *abfd,
17365                      unsigned int offset_size,
17366                      struct dwarf2_section_info *section)
17367 {
17368   unsigned int bytes_read, i;
17369   unsigned long arg;
17370   gdb_byte *defn;
17371
17372   if (opcode_definitions[opcode] == NULL)
17373     {
17374       complaint (&symfile_complaints,
17375                  _("unrecognized DW_MACFINO opcode 0x%x"),
17376                  opcode);
17377       return NULL;
17378     }
17379
17380   defn = opcode_definitions[opcode];
17381   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
17382   defn += bytes_read;
17383
17384   for (i = 0; i < arg; ++i)
17385     {
17386       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
17387                                  section);
17388       if (mac_ptr == NULL)
17389         {
17390           /* skip_form_bytes already issued the complaint.  */
17391           return NULL;
17392         }
17393     }
17394
17395   return mac_ptr;
17396 }
17397
17398 /* A helper function which parses the header of a macro section.
17399    If the macro section is the extended (for now called "GNU") type,
17400    then this updates *OFFSET_SIZE.  Returns a pointer to just after
17401    the header, or issues a complaint and returns NULL on error.  */
17402
17403 static gdb_byte *
17404 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
17405                           bfd *abfd,
17406                           gdb_byte *mac_ptr,
17407                           unsigned int *offset_size,
17408                           int section_is_gnu)
17409 {
17410   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
17411
17412   if (section_is_gnu)
17413     {
17414       unsigned int version, flags;
17415
17416       version = read_2_bytes (abfd, mac_ptr);
17417       if (version != 4)
17418         {
17419           complaint (&symfile_complaints,
17420                      _("unrecognized version `%d' in .debug_macro section"),
17421                      version);
17422           return NULL;
17423         }
17424       mac_ptr += 2;
17425
17426       flags = read_1_byte (abfd, mac_ptr);
17427       ++mac_ptr;
17428       *offset_size = (flags & 1) ? 8 : 4;
17429
17430       if ((flags & 2) != 0)
17431         /* We don't need the line table offset.  */
17432         mac_ptr += *offset_size;
17433
17434       /* Vendor opcode descriptions.  */
17435       if ((flags & 4) != 0)
17436         {
17437           unsigned int i, count;
17438
17439           count = read_1_byte (abfd, mac_ptr);
17440           ++mac_ptr;
17441           for (i = 0; i < count; ++i)
17442             {
17443               unsigned int opcode, bytes_read;
17444               unsigned long arg;
17445
17446               opcode = read_1_byte (abfd, mac_ptr);
17447               ++mac_ptr;
17448               opcode_definitions[opcode] = mac_ptr;
17449               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17450               mac_ptr += bytes_read;
17451               mac_ptr += arg;
17452             }
17453         }
17454     }
17455
17456   return mac_ptr;
17457 }
17458
17459 /* A helper for dwarf_decode_macros that handles the GNU extensions,
17460    including DW_MACRO_GNU_transparent_include.  */
17461
17462 static void
17463 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
17464                           struct macro_source_file *current_file,
17465                           struct line_header *lh, char *comp_dir,
17466                           struct dwarf2_section_info *section,
17467                           int section_is_gnu, int section_is_dwz,
17468                           unsigned int offset_size,
17469                           struct objfile *objfile,
17470                           htab_t include_hash)
17471 {
17472   enum dwarf_macro_record_type macinfo_type;
17473   int at_commandline;
17474   gdb_byte *opcode_definitions[256];
17475
17476   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
17477                                       &offset_size, section_is_gnu);
17478   if (mac_ptr == NULL)
17479     {
17480       /* We already issued a complaint.  */
17481       return;
17482     }
17483
17484   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
17485      GDB is still reading the definitions from command line.  First
17486      DW_MACINFO_start_file will need to be ignored as it was already executed
17487      to create CURRENT_FILE for the main source holding also the command line
17488      definitions.  On first met DW_MACINFO_start_file this flag is reset to
17489      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
17490
17491   at_commandline = 1;
17492
17493   do
17494     {
17495       /* Do we at least have room for a macinfo type byte?  */
17496       if (mac_ptr >= mac_end)
17497         {
17498           dwarf2_section_buffer_overflow_complaint (section);
17499           break;
17500         }
17501
17502       macinfo_type = read_1_byte (abfd, mac_ptr);
17503       mac_ptr++;
17504
17505       /* Note that we rely on the fact that the corresponding GNU and
17506          DWARF constants are the same.  */
17507       switch (macinfo_type)
17508         {
17509           /* A zero macinfo type indicates the end of the macro
17510              information.  */
17511         case 0:
17512           break;
17513
17514         case DW_MACRO_GNU_define:
17515         case DW_MACRO_GNU_undef:
17516         case DW_MACRO_GNU_define_indirect:
17517         case DW_MACRO_GNU_undef_indirect:
17518         case DW_MACRO_GNU_define_indirect_alt:
17519         case DW_MACRO_GNU_undef_indirect_alt:
17520           {
17521             unsigned int bytes_read;
17522             int line;
17523             char *body;
17524             int is_define;
17525
17526             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17527             mac_ptr += bytes_read;
17528
17529             if (macinfo_type == DW_MACRO_GNU_define
17530                 || macinfo_type == DW_MACRO_GNU_undef)
17531               {
17532                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
17533                 mac_ptr += bytes_read;
17534               }
17535             else
17536               {
17537                 LONGEST str_offset;
17538
17539                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
17540                 mac_ptr += offset_size;
17541
17542                 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
17543                     || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
17544                     || section_is_dwz)
17545                   {
17546                     struct dwz_file *dwz = dwarf2_get_dwz_file ();
17547
17548                     body = read_indirect_string_from_dwz (dwz, str_offset);
17549                   }
17550                 else
17551                   body = read_indirect_string_at_offset (abfd, str_offset);
17552               }
17553
17554             is_define = (macinfo_type == DW_MACRO_GNU_define
17555                          || macinfo_type == DW_MACRO_GNU_define_indirect
17556                          || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
17557             if (! current_file)
17558               {
17559                 /* DWARF violation as no main source is present.  */
17560                 complaint (&symfile_complaints,
17561                            _("debug info with no main source gives macro %s "
17562                              "on line %d: %s"),
17563                            is_define ? _("definition") : _("undefinition"),
17564                            line, body);
17565                 break;
17566               }
17567             if ((line == 0 && !at_commandline)
17568                 || (line != 0 && at_commandline))
17569               complaint (&symfile_complaints,
17570                          _("debug info gives %s macro %s with %s line %d: %s"),
17571                          at_commandline ? _("command-line") : _("in-file"),
17572                          is_define ? _("definition") : _("undefinition"),
17573                          line == 0 ? _("zero") : _("non-zero"), line, body);
17574
17575             if (is_define)
17576               parse_macro_definition (current_file, line, body);
17577             else
17578               {
17579                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
17580                             || macinfo_type == DW_MACRO_GNU_undef_indirect
17581                             || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
17582                 macro_undef (current_file, line, body);
17583               }
17584           }
17585           break;
17586
17587         case DW_MACRO_GNU_start_file:
17588           {
17589             unsigned int bytes_read;
17590             int line, file;
17591
17592             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17593             mac_ptr += bytes_read;
17594             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
17595             mac_ptr += bytes_read;
17596
17597             if ((line == 0 && !at_commandline)
17598                 || (line != 0 && at_commandline))
17599               complaint (&symfile_complaints,
17600                          _("debug info gives source %d included "
17601                            "from %s at %s line %d"),
17602                          file, at_commandline ? _("command-line") : _("file"),
17603                          line == 0 ? _("zero") : _("non-zero"), line);
17604
17605             if (at_commandline)
17606               {
17607                 /* This DW_MACRO_GNU_start_file was executed in the
17608                    pass one.  */
17609                 at_commandline = 0;
17610               }
17611             else
17612               current_file = macro_start_file (file, line,
17613                                                current_file, comp_dir,
17614                                                lh, objfile);
17615           }
17616           break;
17617
17618         case DW_MACRO_GNU_end_file:
17619           if (! current_file)
17620             complaint (&symfile_complaints,
17621                        _("macro debug info has an unmatched "
17622                          "`close_file' directive"));
17623           else
17624             {
17625               current_file = current_file->included_by;
17626               if (! current_file)
17627                 {
17628                   enum dwarf_macro_record_type next_type;
17629
17630                   /* GCC circa March 2002 doesn't produce the zero
17631                      type byte marking the end of the compilation
17632                      unit.  Complain if it's not there, but exit no
17633                      matter what.  */
17634
17635                   /* Do we at least have room for a macinfo type byte?  */
17636                   if (mac_ptr >= mac_end)
17637                     {
17638                       dwarf2_section_buffer_overflow_complaint (section);
17639                       return;
17640                     }
17641
17642                   /* We don't increment mac_ptr here, so this is just
17643                      a look-ahead.  */
17644                   next_type = read_1_byte (abfd, mac_ptr);
17645                   if (next_type != 0)
17646                     complaint (&symfile_complaints,
17647                                _("no terminating 0-type entry for "
17648                                  "macros in `.debug_macinfo' section"));
17649
17650                   return;
17651                 }
17652             }
17653           break;
17654
17655         case DW_MACRO_GNU_transparent_include:
17656         case DW_MACRO_GNU_transparent_include_alt:
17657           {
17658             LONGEST offset;
17659             void **slot;
17660             bfd *include_bfd = abfd;
17661             struct dwarf2_section_info *include_section = section;
17662             struct dwarf2_section_info alt_section;
17663             gdb_byte *include_mac_end = mac_end;
17664             int is_dwz = section_is_dwz;
17665             gdb_byte *new_mac_ptr;
17666
17667             offset = read_offset_1 (abfd, mac_ptr, offset_size);
17668             mac_ptr += offset_size;
17669
17670             if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
17671               {
17672                 struct dwz_file *dwz = dwarf2_get_dwz_file ();
17673
17674                 dwarf2_read_section (dwarf2_per_objfile->objfile,
17675                                      &dwz->macro);
17676
17677                 include_bfd = dwz->macro.asection->owner;
17678                 include_section = &dwz->macro;
17679                 include_mac_end = dwz->macro.buffer + dwz->macro.size;
17680                 is_dwz = 1;
17681               }
17682
17683             new_mac_ptr = include_section->buffer + offset;
17684             slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
17685
17686             if (*slot != NULL)
17687               {
17688                 /* This has actually happened; see
17689                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
17690                 complaint (&symfile_complaints,
17691                            _("recursive DW_MACRO_GNU_transparent_include in "
17692                              ".debug_macro section"));
17693               }
17694             else
17695               {
17696                 *slot = new_mac_ptr;
17697
17698                 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
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, new_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_zuinteger_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 }
This page took 1.155035 seconds and 4 git commands to generate.