gdb/
[external/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 "valprint.h"
63 #include <ctype.h>
64
65 #include <fcntl.h>
66 #include "gdb_string.h"
67 #include "gdb_assert.h"
68 #include <sys/types.h>
69 #ifdef HAVE_ZLIB_H
70 #include <zlib.h>
71 #endif
72 #ifdef HAVE_MMAP
73 #include <sys/mman.h>
74 #ifndef MAP_FAILED
75 #define MAP_FAILED ((void *) -1)
76 #endif
77 #endif
78
79 typedef struct symbol *symbolp;
80 DEF_VEC_P (symbolp);
81
82 /* When non-zero, dump DIEs after they are read in.  */
83 static int dwarf2_die_debug = 0;
84
85 /* When non-zero, cross-check physname against demangler.  */
86 static int check_physname = 0;
87
88 /* When non-zero, do not reject deprecated .gdb_index sections.  */
89 int use_deprecated_index_sections = 0;
90
91 static int pagesize;
92
93 /* When set, the file that we're processing is known to have debugging
94    info for C++ namespaces.  GCC 3.3.x did not produce this information,
95    but later versions do.  */
96
97 static int processing_has_namespace_info;
98
99 static const struct objfile_data *dwarf2_objfile_data_key;
100
101 struct dwarf2_section_info
102 {
103   asection *asection;
104   gdb_byte *buffer;
105   bfd_size_type size;
106   /* Not NULL if the section was actually mmapped.  */
107   void *map_addr;
108   /* Page aligned size of mmapped area.  */
109   bfd_size_type map_len;
110   /* True if we have tried to read this section.  */
111   int readin;
112 };
113
114 typedef struct dwarf2_section_info dwarf2_section_info_def;
115 DEF_VEC_O (dwarf2_section_info_def);
116
117 /* All offsets in the index are of this type.  It must be
118    architecture-independent.  */
119 typedef uint32_t offset_type;
120
121 DEF_VEC_I (offset_type);
122
123 /* A description of the mapped index.  The file format is described in
124    a comment by the code that writes the index.  */
125 struct mapped_index
126 {
127   /* Index data format version.  */
128   int version;
129
130   /* The total length of the buffer.  */
131   off_t total_size;
132
133   /* A pointer to the address table data.  */
134   const gdb_byte *address_table;
135
136   /* Size of the address table data in bytes.  */
137   offset_type address_table_size;
138
139   /* The symbol table, implemented as a hash table.  */
140   const offset_type *symbol_table;
141
142   /* Size in slots, each slot is 2 offset_types.  */
143   offset_type symbol_table_slots;
144
145   /* A pointer to the constant pool.  */
146   const char *constant_pool;
147 };
148
149 /* Collection of data recorded per objfile.
150    This hangs off of dwarf2_objfile_data_key.  */
151
152 struct dwarf2_per_objfile
153 {
154   struct dwarf2_section_info info;
155   struct dwarf2_section_info abbrev;
156   struct dwarf2_section_info line;
157   struct dwarf2_section_info loc;
158   struct dwarf2_section_info macinfo;
159   struct dwarf2_section_info macro;
160   struct dwarf2_section_info str;
161   struct dwarf2_section_info ranges;
162   struct dwarf2_section_info frame;
163   struct dwarf2_section_info eh_frame;
164   struct dwarf2_section_info gdb_index;
165
166   VEC (dwarf2_section_info_def) *types;
167
168   /* Back link.  */
169   struct objfile *objfile;
170
171   /* Table of all the compilation units.  This is used to locate
172      the target compilation unit of a particular reference.  */
173   struct dwarf2_per_cu_data **all_comp_units;
174
175   /* The number of compilation units in ALL_COMP_UNITS.  */
176   int n_comp_units;
177
178   /* The number of .debug_types-related CUs.  */
179   int n_type_units;
180
181   /* The .debug_types-related CUs (TUs).  */
182   struct dwarf2_per_cu_data **all_type_units;
183
184   /* A chain of compilation units that are currently read in, so that
185      they can be freed later.  */
186   struct dwarf2_per_cu_data *read_in_chain;
187
188   /* A table mapping .debug_types signatures to its signatured_type entry.
189      This is NULL if the .debug_types section hasn't been read in yet.  */
190   htab_t signatured_types;
191
192   /* A flag indicating wether this objfile has a section loaded at a
193      VMA of 0.  */
194   int has_section_at_zero;
195
196   /* True if we are using the mapped index,
197      or we are faking it for OBJF_READNOW's sake.  */
198   unsigned char using_index;
199
200   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
201   struct mapped_index *index_table;
202
203   /* When using index_table, this keeps track of all quick_file_names entries.
204      TUs can share line table entries with CUs or other TUs, and there can be
205      a lot more TUs than unique line tables, so we maintain a separate table
206      of all line table entries to support the sharing.  */
207   htab_t quick_file_names_table;
208
209   /* Set during partial symbol reading, to prevent queueing of full
210      symbols.  */
211   int reading_partial_symbols;
212
213   /* Table mapping type .debug_info DIE offsets to types.
214      This is NULL if not allocated yet.
215      It (currently) makes sense to allocate debug_types_type_hash lazily.
216      To keep things simple we allocate both lazily.  */
217   htab_t debug_info_type_hash;
218
219   /* Table mapping type .debug_types DIE offsets to types.
220      This is NULL if not allocated yet.  */
221   htab_t debug_types_type_hash;
222 };
223
224 static struct dwarf2_per_objfile *dwarf2_per_objfile;
225
226 /* Default names of the debugging sections.  */
227
228 /* Note that if the debugging section has been compressed, it might
229    have a name like .zdebug_info.  */
230
231 static const struct dwarf2_debug_sections dwarf2_elf_names =
232 {
233   { ".debug_info", ".zdebug_info" },
234   { ".debug_abbrev", ".zdebug_abbrev" },
235   { ".debug_line", ".zdebug_line" },
236   { ".debug_loc", ".zdebug_loc" },
237   { ".debug_macinfo", ".zdebug_macinfo" },
238   { ".debug_macro", ".zdebug_macro" },
239   { ".debug_str", ".zdebug_str" },
240   { ".debug_ranges", ".zdebug_ranges" },
241   { ".debug_types", ".zdebug_types" },
242   { ".debug_frame", ".zdebug_frame" },
243   { ".eh_frame", NULL },
244   { ".gdb_index", ".zgdb_index" },
245   23
246 };
247
248 /* local data types */
249
250 /* We hold several abbreviation tables in memory at the same time.  */
251 #ifndef ABBREV_HASH_SIZE
252 #define ABBREV_HASH_SIZE 121
253 #endif
254
255 /* The data in a compilation unit header, after target2host
256    translation, looks like this.  */
257 struct comp_unit_head
258 {
259   unsigned int length;
260   short version;
261   unsigned char addr_size;
262   unsigned char signed_addr_p;
263   unsigned int abbrev_offset;
264
265   /* Size of file offsets; either 4 or 8.  */
266   unsigned int offset_size;
267
268   /* Size of the length field; either 4 or 12.  */
269   unsigned int initial_length_size;
270
271   /* Offset to the first byte of this compilation unit header in the
272      .debug_info section, for resolving relative reference dies.  */
273   unsigned int offset;
274
275   /* Offset to first die in this cu from the start of the cu.
276      This will be the first byte following the compilation unit header.  */
277   unsigned int first_die_offset;
278 };
279
280 /* Type used for delaying computation of method physnames.
281    See comments for compute_delayed_physnames.  */
282 struct delayed_method_info
283 {
284   /* The type to which the method is attached, i.e., its parent class.  */
285   struct type *type;
286
287   /* The index of the method in the type's function fieldlists.  */
288   int fnfield_index;
289
290   /* The index of the method in the fieldlist.  */
291   int index;
292
293   /* The name of the DIE.  */
294   const char *name;
295
296   /*  The DIE associated with this method.  */
297   struct die_info *die;
298 };
299
300 typedef struct delayed_method_info delayed_method_info;
301 DEF_VEC_O (delayed_method_info);
302
303 /* Internal state when decoding a particular compilation unit.  */
304 struct dwarf2_cu
305 {
306   /* The objfile containing this compilation unit.  */
307   struct objfile *objfile;
308
309   /* The header of the compilation unit.  */
310   struct comp_unit_head header;
311
312   /* Base address of this compilation unit.  */
313   CORE_ADDR base_address;
314
315   /* Non-zero if base_address has been set.  */
316   int base_known;
317
318   /* The language we are debugging.  */
319   enum language language;
320   const struct language_defn *language_defn;
321
322   const char *producer;
323
324   /* The generic symbol table building routines have separate lists for
325      file scope symbols and all all other scopes (local scopes).  So
326      we need to select the right one to pass to add_symbol_to_list().
327      We do it by keeping a pointer to the correct list in list_in_scope.
328
329      FIXME: The original dwarf code just treated the file scope as the
330      first local scope, and all other local scopes as nested local
331      scopes, and worked fine.  Check to see if we really need to
332      distinguish these in buildsym.c.  */
333   struct pending **list_in_scope;
334
335   /* DWARF abbreviation table associated with this compilation unit.  */
336   struct abbrev_info **dwarf2_abbrevs;
337
338   /* Storage for the abbrev table.  */
339   struct obstack abbrev_obstack;
340
341   /* Hash table holding all the loaded partial DIEs.  */
342   htab_t partial_dies;
343
344   /* Storage for things with the same lifetime as this read-in compilation
345      unit, including partial DIEs.  */
346   struct obstack comp_unit_obstack;
347
348   /* When multiple dwarf2_cu structures are living in memory, this field
349      chains them all together, so that they can be released efficiently.
350      We will probably also want a generation counter so that most-recently-used
351      compilation units are cached...  */
352   struct dwarf2_per_cu_data *read_in_chain;
353
354   /* Backchain to our per_cu entry if the tree has been built.  */
355   struct dwarf2_per_cu_data *per_cu;
356
357   /* How many compilation units ago was this CU last referenced?  */
358   int last_used;
359
360   /* A hash table of die offsets for following references.  */
361   htab_t die_hash;
362
363   /* Full DIEs if read in.  */
364   struct die_info *dies;
365
366   /* A set of pointers to dwarf2_per_cu_data objects for compilation
367      units referenced by this one.  Only set during full symbol processing;
368      partial symbol tables do not have dependencies.  */
369   htab_t dependencies;
370
371   /* Header data from the line table, during full symbol processing.  */
372   struct line_header *line_header;
373
374   /* A list of methods which need to have physnames computed
375      after all type information has been read.  */
376   VEC (delayed_method_info) *method_list;
377
378   /* To be copied to symtab->call_site_htab.  */
379   htab_t call_site_htab;
380
381   /* Mark used when releasing cached dies.  */
382   unsigned int mark : 1;
383
384   /* This CU references .debug_loc.  See the symtab->locations_valid field.
385      This test is imperfect as there may exist optimized debug code not using
386      any location list and still facing inlining issues if handled as
387      unoptimized code.  For a future better test see GCC PR other/32998.  */
388   unsigned int has_loclist : 1;
389
390   /* These cache the results of producer_is_gxx_lt_4_6.
391      CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid.  This
392      information is cached because profiling CU expansion showed
393      excessive time spent in producer_is_gxx_lt_4_6.  */
394   unsigned int checked_producer : 1;
395   unsigned int producer_is_gxx_lt_4_6 : 1;
396 };
397
398 /* Persistent data held for a compilation unit, even when not
399    processing it.  We put a pointer to this structure in the
400    read_symtab_private field of the psymtab.  */
401
402 struct dwarf2_per_cu_data
403 {
404   /* The start offset and length of this compilation unit.  2**29-1
405      bytes should suffice to store the length of any compilation unit
406      - if it doesn't, GDB will fall over anyway.
407      NOTE: Unlike comp_unit_head.length, this length includes
408      initial_length_size.  */
409   unsigned int offset;
410   unsigned int length : 29;
411
412   /* Flag indicating this compilation unit will be read in before
413      any of the current compilation units are processed.  */
414   unsigned int queued : 1;
415
416   /* This flag will be set if we need to load absolutely all DIEs
417      for this compilation unit, instead of just the ones we think
418      are interesting.  It gets set if we look for a DIE in the
419      hash table and don't find it.  */
420   unsigned int load_all_dies : 1;
421
422   /* Non-null if this CU is from .debug_types; in which case it points
423      to the section.  Otherwise it's from .debug_info.  */
424   struct dwarf2_section_info *debug_types_section;
425
426   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
427      of the CU cache it gets reset to NULL again.  */
428   struct dwarf2_cu *cu;
429
430   /* The corresponding objfile.
431      Normally we can get the objfile from dwarf2_per_objfile.
432      However we can enter this file with just a "per_cu" handle.  */
433   struct objfile *objfile;
434
435   /* When using partial symbol tables, the 'psymtab' field is active.
436      Otherwise the 'quick' field is active.  */
437   union
438   {
439     /* The partial symbol table associated with this compilation unit,
440        or NULL for partial units (which do not have an associated
441        symtab).  */
442     struct partial_symtab *psymtab;
443
444     /* Data needed by the "quick" functions.  */
445     struct dwarf2_per_cu_quick_data *quick;
446   } v;
447 };
448
449 /* Entry in the signatured_types hash table.  */
450
451 struct signatured_type
452 {
453   ULONGEST signature;
454
455   /* Offset in .debug_types of the type defined by this TU.  */
456   unsigned int type_offset;
457
458   /* The CU(/TU) of this type.  */
459   struct dwarf2_per_cu_data per_cu;
460 };
461
462 /* Struct used to pass misc. parameters to read_die_and_children, et
463    al.  which are used for both .debug_info and .debug_types dies.
464    All parameters here are unchanging for the life of the call.  This
465    struct exists to abstract away the constant parameters of die
466    reading.  */
467
468 struct die_reader_specs
469 {
470   /* The bfd of this objfile.  */
471   bfd* abfd;
472
473   /* The CU of the DIE we are parsing.  */
474   struct dwarf2_cu *cu;
475
476   /* Pointer to start of section buffer.
477      This is either the start of .debug_info or .debug_types.  */
478   const gdb_byte *buffer;
479 };
480
481 /* The line number information for a compilation unit (found in the
482    .debug_line section) begins with a "statement program header",
483    which contains the following information.  */
484 struct line_header
485 {
486   unsigned int total_length;
487   unsigned short version;
488   unsigned int header_length;
489   unsigned char minimum_instruction_length;
490   unsigned char maximum_ops_per_instruction;
491   unsigned char default_is_stmt;
492   int line_base;
493   unsigned char line_range;
494   unsigned char opcode_base;
495
496   /* standard_opcode_lengths[i] is the number of operands for the
497      standard opcode whose value is i.  This means that
498      standard_opcode_lengths[0] is unused, and the last meaningful
499      element is standard_opcode_lengths[opcode_base - 1].  */
500   unsigned char *standard_opcode_lengths;
501
502   /* The include_directories table.  NOTE!  These strings are not
503      allocated with xmalloc; instead, they are pointers into
504      debug_line_buffer.  If you try to free them, `free' will get
505      indigestion.  */
506   unsigned int num_include_dirs, include_dirs_size;
507   char **include_dirs;
508
509   /* The file_names table.  NOTE!  These strings are not allocated
510      with xmalloc; instead, they are pointers into debug_line_buffer.
511      Don't try to free them directly.  */
512   unsigned int num_file_names, file_names_size;
513   struct file_entry
514   {
515     char *name;
516     unsigned int dir_index;
517     unsigned int mod_time;
518     unsigned int length;
519     int included_p; /* Non-zero if referenced by the Line Number Program.  */
520     struct symtab *symtab; /* The associated symbol table, if any.  */
521   } *file_names;
522
523   /* The start and end of the statement program following this
524      header.  These point into dwarf2_per_objfile->line_buffer.  */
525   gdb_byte *statement_program_start, *statement_program_end;
526 };
527
528 /* When we construct a partial symbol table entry we only
529    need this much information.  */
530 struct partial_die_info
531   {
532     /* Offset of this DIE.  */
533     unsigned int offset;
534
535     /* DWARF-2 tag for this DIE.  */
536     ENUM_BITFIELD(dwarf_tag) tag : 16;
537
538     /* Assorted flags describing the data found in this DIE.  */
539     unsigned int has_children : 1;
540     unsigned int is_external : 1;
541     unsigned int is_declaration : 1;
542     unsigned int has_type : 1;
543     unsigned int has_specification : 1;
544     unsigned int has_pc_info : 1;
545     unsigned int may_be_inlined : 1;
546
547     /* Flag set if the SCOPE field of this structure has been
548        computed.  */
549     unsigned int scope_set : 1;
550
551     /* Flag set if the DIE has a byte_size attribute.  */
552     unsigned int has_byte_size : 1;
553
554     /* Flag set if any of the DIE's children are template arguments.  */
555     unsigned int has_template_arguments : 1;
556
557     /* Flag set if fixup_partial_die has been called on this die.  */
558     unsigned int fixup_called : 1;
559
560     /* The name of this DIE.  Normally the value of DW_AT_name, but
561        sometimes a default name for unnamed DIEs.  */
562     char *name;
563
564     /* The linkage name, if present.  */
565     const char *linkage_name;
566
567     /* The scope to prepend to our children.  This is generally
568        allocated on the comp_unit_obstack, so will disappear
569        when this compilation unit leaves the cache.  */
570     char *scope;
571
572     /* The location description associated with this DIE, if any.  */
573     struct dwarf_block *locdesc;
574
575     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
576     CORE_ADDR lowpc;
577     CORE_ADDR highpc;
578
579     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
580        DW_AT_sibling, if any.  */
581     /* NOTE: This member isn't strictly necessary, read_partial_die could
582        return DW_AT_sibling values to its caller load_partial_dies.  */
583     gdb_byte *sibling;
584
585     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
586        DW_AT_specification (or DW_AT_abstract_origin or
587        DW_AT_extension).  */
588     unsigned int spec_offset;
589
590     /* Pointers to this DIE's parent, first child, and next sibling,
591        if any.  */
592     struct partial_die_info *die_parent, *die_child, *die_sibling;
593   };
594
595 /* This data structure holds the information of an abbrev.  */
596 struct abbrev_info
597   {
598     unsigned int number;        /* number identifying abbrev */
599     enum dwarf_tag tag;         /* dwarf tag */
600     unsigned short has_children;                /* boolean */
601     unsigned short num_attrs;   /* number of attributes */
602     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
603     struct abbrev_info *next;   /* next in chain */
604   };
605
606 struct attr_abbrev
607   {
608     ENUM_BITFIELD(dwarf_attribute) name : 16;
609     ENUM_BITFIELD(dwarf_form) form : 16;
610   };
611
612 /* Attributes have a name and a value.  */
613 struct attribute
614   {
615     ENUM_BITFIELD(dwarf_attribute) name : 16;
616     ENUM_BITFIELD(dwarf_form) form : 15;
617
618     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
619        field should be in u.str (existing only for DW_STRING) but it is kept
620        here for better struct attribute alignment.  */
621     unsigned int string_is_canonical : 1;
622
623     union
624       {
625         char *str;
626         struct dwarf_block *blk;
627         ULONGEST unsnd;
628         LONGEST snd;
629         CORE_ADDR addr;
630         struct signatured_type *signatured_type;
631       }
632     u;
633   };
634
635 /* This data structure holds a complete die structure.  */
636 struct die_info
637   {
638     /* DWARF-2 tag for this DIE.  */
639     ENUM_BITFIELD(dwarf_tag) tag : 16;
640
641     /* Number of attributes */
642     unsigned char num_attrs;
643
644     /* True if we're presently building the full type name for the
645        type derived from this DIE.  */
646     unsigned char building_fullname : 1;
647
648     /* Abbrev number */
649     unsigned int abbrev;
650
651     /* Offset in .debug_info or .debug_types section.  */
652     unsigned int offset;
653
654     /* The dies in a compilation unit form an n-ary tree.  PARENT
655        points to this die's parent; CHILD points to the first child of
656        this node; and all the children of a given node are chained
657        together via their SIBLING fields.  */
658     struct die_info *child;     /* Its first child, if any.  */
659     struct die_info *sibling;   /* Its next sibling, if any.  */
660     struct die_info *parent;    /* Its parent, if any.  */
661
662     /* An array of attributes, with NUM_ATTRS elements.  There may be
663        zero, but it's not common and zero-sized arrays are not
664        sufficiently portable C.  */
665     struct attribute attrs[1];
666   };
667
668 /* Get at parts of an attribute structure.  */
669
670 #define DW_STRING(attr)    ((attr)->u.str)
671 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
672 #define DW_UNSND(attr)     ((attr)->u.unsnd)
673 #define DW_BLOCK(attr)     ((attr)->u.blk)
674 #define DW_SND(attr)       ((attr)->u.snd)
675 #define DW_ADDR(attr)      ((attr)->u.addr)
676 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
677
678 /* Blocks are a bunch of untyped bytes.  */
679 struct dwarf_block
680   {
681     unsigned int size;
682
683     /* Valid only if SIZE is not zero.  */
684     gdb_byte *data;
685   };
686
687 #ifndef ATTR_ALLOC_CHUNK
688 #define ATTR_ALLOC_CHUNK 4
689 #endif
690
691 /* Allocate fields for structs, unions and enums in this size.  */
692 #ifndef DW_FIELD_ALLOC_CHUNK
693 #define DW_FIELD_ALLOC_CHUNK 4
694 #endif
695
696 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
697    but this would require a corresponding change in unpack_field_as_long
698    and friends.  */
699 static int bits_per_byte = 8;
700
701 /* The routines that read and process dies for a C struct or C++ class
702    pass lists of data member fields and lists of member function fields
703    in an instance of a field_info structure, as defined below.  */
704 struct field_info
705   {
706     /* List of data member and baseclasses fields.  */
707     struct nextfield
708       {
709         struct nextfield *next;
710         int accessibility;
711         int virtuality;
712         struct field field;
713       }
714      *fields, *baseclasses;
715
716     /* Number of fields (including baseclasses).  */
717     int nfields;
718
719     /* Number of baseclasses.  */
720     int nbaseclasses;
721
722     /* Set if the accesibility of one of the fields is not public.  */
723     int non_public_fields;
724
725     /* Member function fields array, entries are allocated in the order they
726        are encountered in the object file.  */
727     struct nextfnfield
728       {
729         struct nextfnfield *next;
730         struct fn_field fnfield;
731       }
732      *fnfields;
733
734     /* Member function fieldlist array, contains name of possibly overloaded
735        member function, number of overloaded member functions and a pointer
736        to the head of the member function field chain.  */
737     struct fnfieldlist
738       {
739         char *name;
740         int length;
741         struct nextfnfield *head;
742       }
743      *fnfieldlists;
744
745     /* Number of entries in the fnfieldlists array.  */
746     int nfnfields;
747
748     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
749        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
750     struct typedef_field_list
751       {
752         struct typedef_field field;
753         struct typedef_field_list *next;
754       }
755     *typedef_field_list;
756     unsigned typedef_field_list_count;
757   };
758
759 /* One item on the queue of compilation units to read in full symbols
760    for.  */
761 struct dwarf2_queue_item
762 {
763   struct dwarf2_per_cu_data *per_cu;
764   struct dwarf2_queue_item *next;
765 };
766
767 /* The current queue.  */
768 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
769
770 /* Loaded secondary compilation units are kept in memory until they
771    have not been referenced for the processing of this many
772    compilation units.  Set this to zero to disable caching.  Cache
773    sizes of up to at least twenty will improve startup time for
774    typical inter-CU-reference binaries, at an obvious memory cost.  */
775 static int dwarf2_max_cache_age = 5;
776 static void
777 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
778                            struct cmd_list_element *c, const char *value)
779 {
780   fprintf_filtered (file, _("The upper bound on the age of cached "
781                             "dwarf2 compilation units is %s.\n"),
782                     value);
783 }
784
785
786 /* Various complaints about symbol reading that don't abort the process.  */
787
788 static void
789 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
790 {
791   complaint (&symfile_complaints,
792              _("statement list doesn't fit in .debug_line section"));
793 }
794
795 static void
796 dwarf2_debug_line_missing_file_complaint (void)
797 {
798   complaint (&symfile_complaints,
799              _(".debug_line section has line data without a file"));
800 }
801
802 static void
803 dwarf2_debug_line_missing_end_sequence_complaint (void)
804 {
805   complaint (&symfile_complaints,
806              _(".debug_line section has line "
807                "program sequence without an end"));
808 }
809
810 static void
811 dwarf2_complex_location_expr_complaint (void)
812 {
813   complaint (&symfile_complaints, _("location expression too complex"));
814 }
815
816 static void
817 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
818                                               int arg3)
819 {
820   complaint (&symfile_complaints,
821              _("const value length mismatch for '%s', got %d, expected %d"),
822              arg1, arg2, arg3);
823 }
824
825 static void
826 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
827 {
828   complaint (&symfile_complaints,
829              _("macro info runs off end of `%s' section"),
830              section->asection->name);
831 }
832
833 static void
834 dwarf2_macro_malformed_definition_complaint (const char *arg1)
835 {
836   complaint (&symfile_complaints,
837              _("macro debug info contains a "
838                "malformed macro definition:\n`%s'"),
839              arg1);
840 }
841
842 static void
843 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
844 {
845   complaint (&symfile_complaints,
846              _("invalid attribute class or form for '%s' in '%s'"),
847              arg1, arg2);
848 }
849
850 /* local function prototypes */
851
852 static void dwarf2_locate_sections (bfd *, asection *, void *);
853
854 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
855                                            struct objfile *);
856
857 static void dwarf2_find_base_address (struct die_info *die,
858                                       struct dwarf2_cu *cu);
859
860 static void dwarf2_build_psymtabs_hard (struct objfile *);
861
862 static void scan_partial_symbols (struct partial_die_info *,
863                                   CORE_ADDR *, CORE_ADDR *,
864                                   int, struct dwarf2_cu *);
865
866 static void add_partial_symbol (struct partial_die_info *,
867                                 struct dwarf2_cu *);
868
869 static void add_partial_namespace (struct partial_die_info *pdi,
870                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
871                                    int need_pc, struct dwarf2_cu *cu);
872
873 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
874                                 CORE_ADDR *highpc, int need_pc,
875                                 struct dwarf2_cu *cu);
876
877 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
878                                      struct dwarf2_cu *cu);
879
880 static void add_partial_subprogram (struct partial_die_info *pdi,
881                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
882                                     int need_pc, struct dwarf2_cu *cu);
883
884 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
885                                      gdb_byte *buffer, gdb_byte *info_ptr,
886                                      bfd *abfd, struct dwarf2_cu *cu);
887
888 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
889
890 static void psymtab_to_symtab_1 (struct partial_symtab *);
891
892 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
893
894 static void dwarf2_free_abbrev_table (void *);
895
896 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
897
898 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
899                                             struct dwarf2_cu *);
900
901 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
902                                                  struct dwarf2_cu *);
903
904 static struct partial_die_info *load_partial_dies (bfd *,
905                                                    gdb_byte *, gdb_byte *,
906                                                    int, struct dwarf2_cu *);
907
908 static gdb_byte *read_partial_die (struct partial_die_info *,
909                                    struct abbrev_info *abbrev,
910                                    unsigned int, bfd *,
911                                    gdb_byte *, gdb_byte *,
912                                    struct dwarf2_cu *);
913
914 static struct partial_die_info *find_partial_die (unsigned int,
915                                                   struct dwarf2_cu *);
916
917 static void fixup_partial_die (struct partial_die_info *,
918                                struct dwarf2_cu *);
919
920 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
921                                  bfd *, gdb_byte *, struct dwarf2_cu *);
922
923 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
924                                        bfd *, gdb_byte *, struct dwarf2_cu *);
925
926 static unsigned int read_1_byte (bfd *, gdb_byte *);
927
928 static int read_1_signed_byte (bfd *, gdb_byte *);
929
930 static unsigned int read_2_bytes (bfd *, gdb_byte *);
931
932 static unsigned int read_4_bytes (bfd *, gdb_byte *);
933
934 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
935
936 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
937                                unsigned int *);
938
939 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
940
941 static LONGEST read_checked_initial_length_and_offset
942   (bfd *, gdb_byte *, const struct comp_unit_head *,
943    unsigned int *, unsigned int *);
944
945 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
946                             unsigned int *);
947
948 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
949
950 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
951
952 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
953
954 static char *read_indirect_string (bfd *, gdb_byte *,
955                                    const struct comp_unit_head *,
956                                    unsigned int *);
957
958 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
959
960 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
961
962 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
963
964 static void set_cu_language (unsigned int, struct dwarf2_cu *);
965
966 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
967                                       struct dwarf2_cu *);
968
969 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
970                                                 unsigned int,
971                                                 struct dwarf2_cu *);
972
973 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
974                                struct dwarf2_cu *cu);
975
976 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
977
978 static struct die_info *die_specification (struct die_info *die,
979                                            struct dwarf2_cu **);
980
981 static void free_line_header (struct line_header *lh);
982
983 static void add_file_name (struct line_header *, char *, unsigned int,
984                            unsigned int, unsigned int);
985
986 static struct line_header *(dwarf_decode_line_header
987                             (unsigned int offset,
988                              bfd *abfd, struct dwarf2_cu *cu));
989
990 static void dwarf_decode_lines (struct line_header *, const char *,
991                                 struct dwarf2_cu *, struct partial_symtab *,
992                                 int);
993
994 static void dwarf2_start_subfile (char *, const char *, const char *);
995
996 static struct symbol *new_symbol (struct die_info *, struct type *,
997                                   struct dwarf2_cu *);
998
999 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1000                                        struct dwarf2_cu *, struct symbol *);
1001
1002 static void dwarf2_const_value (struct attribute *, struct symbol *,
1003                                 struct dwarf2_cu *);
1004
1005 static void dwarf2_const_value_attr (struct attribute *attr,
1006                                      struct type *type,
1007                                      const char *name,
1008                                      struct obstack *obstack,
1009                                      struct dwarf2_cu *cu, long *value,
1010                                      gdb_byte **bytes,
1011                                      struct dwarf2_locexpr_baton **baton);
1012
1013 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1014
1015 static int need_gnat_info (struct dwarf2_cu *);
1016
1017 static struct type *die_descriptive_type (struct die_info *,
1018                                           struct dwarf2_cu *);
1019
1020 static void set_descriptive_type (struct type *, struct die_info *,
1021                                   struct dwarf2_cu *);
1022
1023 static struct type *die_containing_type (struct die_info *,
1024                                          struct dwarf2_cu *);
1025
1026 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1027                                      struct dwarf2_cu *);
1028
1029 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1030
1031 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1032
1033 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1034
1035 static char *typename_concat (struct obstack *obs, const char *prefix,
1036                               const char *suffix, int physname,
1037                               struct dwarf2_cu *cu);
1038
1039 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1040
1041 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1046
1047 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1048
1049 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1050                                struct dwarf2_cu *, struct partial_symtab *);
1051
1052 static int dwarf2_get_pc_bounds (struct die_info *,
1053                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1054                                  struct partial_symtab *);
1055
1056 static void get_scope_pc_bounds (struct die_info *,
1057                                  CORE_ADDR *, CORE_ADDR *,
1058                                  struct dwarf2_cu *);
1059
1060 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1061                                         CORE_ADDR, struct dwarf2_cu *);
1062
1063 static void dwarf2_add_field (struct field_info *, struct die_info *,
1064                               struct dwarf2_cu *);
1065
1066 static void dwarf2_attach_fields_to_type (struct field_info *,
1067                                           struct type *, struct dwarf2_cu *);
1068
1069 static void dwarf2_add_member_fn (struct field_info *,
1070                                   struct die_info *, struct type *,
1071                                   struct dwarf2_cu *);
1072
1073 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1074                                              struct type *,
1075                                              struct dwarf2_cu *);
1076
1077 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1082
1083 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1084
1085 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1086
1087 static struct type *read_module_type (struct die_info *die,
1088                                       struct dwarf2_cu *cu);
1089
1090 static const char *namespace_name (struct die_info *die,
1091                                    int *is_anonymous, struct dwarf2_cu *);
1092
1093 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1094
1095 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1096
1097 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1098                                                        struct dwarf2_cu *);
1099
1100 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1101
1102 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1103                                                  gdb_byte *info_ptr,
1104                                                  gdb_byte **new_info_ptr,
1105                                                  struct die_info *parent);
1106
1107 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1108                                                gdb_byte *info_ptr,
1109                                                gdb_byte **new_info_ptr,
1110                                                struct die_info *parent);
1111
1112 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1113                                                gdb_byte *info_ptr,
1114                                                gdb_byte **new_info_ptr,
1115                                                struct die_info *parent);
1116
1117 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1118                                 struct die_info **, gdb_byte *,
1119                                 int *);
1120
1121 static void process_die (struct die_info *, struct dwarf2_cu *);
1122
1123 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1124                                        struct obstack *);
1125
1126 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1127
1128 static const char *dwarf2_full_name (char *name,
1129                                      struct die_info *die,
1130                                      struct dwarf2_cu *cu);
1131
1132 static struct die_info *dwarf2_extension (struct die_info *die,
1133                                           struct dwarf2_cu **);
1134
1135 static char *dwarf_tag_name (unsigned int);
1136
1137 static char *dwarf_attr_name (unsigned int);
1138
1139 static char *dwarf_form_name (unsigned int);
1140
1141 static char *dwarf_bool_name (unsigned int);
1142
1143 static char *dwarf_type_encoding_name (unsigned int);
1144
1145 #if 0
1146 static char *dwarf_cfi_name (unsigned int);
1147 #endif
1148
1149 static struct die_info *sibling_die (struct die_info *);
1150
1151 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1152
1153 static void dump_die_for_error (struct die_info *);
1154
1155 static void dump_die_1 (struct ui_file *, int level, int max_level,
1156                         struct die_info *);
1157
1158 /*static*/ void dump_die (struct die_info *, int max_level);
1159
1160 static void store_in_ref_table (struct die_info *,
1161                                 struct dwarf2_cu *);
1162
1163 static int is_ref_attr (struct attribute *);
1164
1165 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1166
1167 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1168
1169 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1170                                                struct attribute *,
1171                                                struct dwarf2_cu **);
1172
1173 static struct die_info *follow_die_ref (struct die_info *,
1174                                         struct attribute *,
1175                                         struct dwarf2_cu **);
1176
1177 static struct die_info *follow_die_sig (struct die_info *,
1178                                         struct attribute *,
1179                                         struct dwarf2_cu **);
1180
1181 static struct signatured_type *lookup_signatured_type_at_offset
1182     (struct objfile *objfile,
1183      struct dwarf2_section_info *section,
1184      unsigned int offset);
1185
1186 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1187
1188 static void read_signatured_type (struct signatured_type *type_sig);
1189
1190 /* memory allocation interface */
1191
1192 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1193
1194 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1195
1196 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1197
1198 static void dwarf_decode_macros (struct line_header *, unsigned int,
1199                                  char *, bfd *, struct dwarf2_cu *,
1200                                  struct dwarf2_section_info *,
1201                                  int);
1202
1203 static int attr_form_is_block (struct attribute *);
1204
1205 static int attr_form_is_section_offset (struct attribute *);
1206
1207 static int attr_form_is_constant (struct attribute *);
1208
1209 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1210                                    struct dwarf2_loclist_baton *baton,
1211                                    struct attribute *attr);
1212
1213 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1214                                          struct symbol *sym,
1215                                          struct dwarf2_cu *cu);
1216
1217 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1218                                struct abbrev_info *abbrev,
1219                                struct dwarf2_cu *cu);
1220
1221 static void free_stack_comp_unit (void *);
1222
1223 static hashval_t partial_die_hash (const void *item);
1224
1225 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1226
1227 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1228   (unsigned int offset, struct objfile *objfile);
1229
1230 static void init_one_comp_unit (struct dwarf2_cu *cu,
1231                                 struct dwarf2_per_cu_data *per_cu);
1232
1233 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1234                                    struct die_info *comp_unit_die);
1235
1236 static void free_heap_comp_unit (void *);
1237
1238 static void free_cached_comp_units (void *);
1239
1240 static void age_cached_comp_units (void);
1241
1242 static void free_one_cached_comp_unit (void *);
1243
1244 static struct type *set_die_type (struct die_info *, struct type *,
1245                                   struct dwarf2_cu *);
1246
1247 static void create_all_comp_units (struct objfile *);
1248
1249 static int create_debug_types_hash_table (struct objfile *objfile);
1250
1251 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1252
1253 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1254
1255 static void dwarf2_add_dependence (struct dwarf2_cu *,
1256                                    struct dwarf2_per_cu_data *);
1257
1258 static void dwarf2_mark (struct dwarf2_cu *);
1259
1260 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1261
1262 static struct type *get_die_type_at_offset (unsigned int,
1263                                             struct dwarf2_per_cu_data *per_cu);
1264
1265 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1266
1267 static void dwarf2_release_queue (void *dummy);
1268
1269 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1270
1271 static void process_queue (void);
1272
1273 static void find_file_and_directory (struct die_info *die,
1274                                      struct dwarf2_cu *cu,
1275                                      char **name, char **comp_dir);
1276
1277 static char *file_full_name (int file, struct line_header *lh,
1278                              const char *comp_dir);
1279
1280 static gdb_byte *read_and_check_comp_unit_head
1281   (struct comp_unit_head *header,
1282    struct dwarf2_section_info *section, gdb_byte *info_ptr,
1283    int is_debug_types_section);
1284
1285 static void init_cu_die_reader (struct die_reader_specs *reader,
1286                                 struct dwarf2_cu *cu);
1287
1288 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1289
1290 #if WORDS_BIGENDIAN
1291
1292 /* Convert VALUE between big- and little-endian.  */
1293 static offset_type
1294 byte_swap (offset_type value)
1295 {
1296   offset_type result;
1297
1298   result = (value & 0xff) << 24;
1299   result |= (value & 0xff00) << 8;
1300   result |= (value & 0xff0000) >> 8;
1301   result |= (value & 0xff000000) >> 24;
1302   return result;
1303 }
1304
1305 #define MAYBE_SWAP(V)  byte_swap (V)
1306
1307 #else
1308 #define MAYBE_SWAP(V) (V)
1309 #endif /* WORDS_BIGENDIAN */
1310
1311 /* The suffix for an index file.  */
1312 #define INDEX_SUFFIX ".gdb-index"
1313
1314 static const char *dwarf2_physname (char *name, struct die_info *die,
1315                                     struct dwarf2_cu *cu);
1316
1317 /* Try to locate the sections we need for DWARF 2 debugging
1318    information and return true if we have enough to do something.
1319    NAMES points to the dwarf2 section names, or is NULL if the standard
1320    ELF names are used.  */
1321
1322 int
1323 dwarf2_has_info (struct objfile *objfile,
1324                  const struct dwarf2_debug_sections *names)
1325 {
1326   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1327   if (!dwarf2_per_objfile)
1328     {
1329       /* Initialize per-objfile state.  */
1330       struct dwarf2_per_objfile *data
1331         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1332
1333       memset (data, 0, sizeof (*data));
1334       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1335       dwarf2_per_objfile = data;
1336
1337       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1338                              (void *) names);
1339       dwarf2_per_objfile->objfile = objfile;
1340     }
1341   return (dwarf2_per_objfile->info.asection != NULL
1342           && dwarf2_per_objfile->abbrev.asection != NULL);
1343 }
1344
1345 /* When loading sections, we look either for uncompressed section or for
1346    compressed section names.  */
1347
1348 static int
1349 section_is_p (const char *section_name,
1350               const struct dwarf2_section_names *names)
1351 {
1352   if (names->normal != NULL
1353       && strcmp (section_name, names->normal) == 0)
1354     return 1;
1355   if (names->compressed != NULL
1356       && strcmp (section_name, names->compressed) == 0)
1357     return 1;
1358   return 0;
1359 }
1360
1361 /* This function is mapped across the sections and remembers the
1362    offset and size of each of the debugging sections we are interested
1363    in.  */
1364
1365 static void
1366 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1367 {
1368   const struct dwarf2_debug_sections *names;
1369
1370   if (vnames == NULL)
1371     names = &dwarf2_elf_names;
1372   else
1373     names = (const struct dwarf2_debug_sections *) vnames;
1374
1375   if (section_is_p (sectp->name, &names->info))
1376     {
1377       dwarf2_per_objfile->info.asection = sectp;
1378       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1379     }
1380   else if (section_is_p (sectp->name, &names->abbrev))
1381     {
1382       dwarf2_per_objfile->abbrev.asection = sectp;
1383       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1384     }
1385   else if (section_is_p (sectp->name, &names->line))
1386     {
1387       dwarf2_per_objfile->line.asection = sectp;
1388       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1389     }
1390   else if (section_is_p (sectp->name, &names->loc))
1391     {
1392       dwarf2_per_objfile->loc.asection = sectp;
1393       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1394     }
1395   else if (section_is_p (sectp->name, &names->macinfo))
1396     {
1397       dwarf2_per_objfile->macinfo.asection = sectp;
1398       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1399     }
1400   else if (section_is_p (sectp->name, &names->macro))
1401     {
1402       dwarf2_per_objfile->macro.asection = sectp;
1403       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1404     }
1405   else if (section_is_p (sectp->name, &names->str))
1406     {
1407       dwarf2_per_objfile->str.asection = sectp;
1408       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1409     }
1410   else if (section_is_p (sectp->name, &names->frame))
1411     {
1412       dwarf2_per_objfile->frame.asection = sectp;
1413       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1414     }
1415   else if (section_is_p (sectp->name, &names->eh_frame))
1416     {
1417       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1418
1419       if (aflag & SEC_HAS_CONTENTS)
1420         {
1421           dwarf2_per_objfile->eh_frame.asection = sectp;
1422           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1423         }
1424     }
1425   else if (section_is_p (sectp->name, &names->ranges))
1426     {
1427       dwarf2_per_objfile->ranges.asection = sectp;
1428       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1429     }
1430   else if (section_is_p (sectp->name, &names->types))
1431     {
1432       struct dwarf2_section_info type_section;
1433
1434       memset (&type_section, 0, sizeof (type_section));
1435       type_section.asection = sectp;
1436       type_section.size = bfd_get_section_size (sectp);
1437
1438       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1439                      &type_section);
1440     }
1441   else if (section_is_p (sectp->name, &names->gdb_index))
1442     {
1443       dwarf2_per_objfile->gdb_index.asection = sectp;
1444       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1445     }
1446
1447   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1448       && bfd_section_vma (abfd, sectp) == 0)
1449     dwarf2_per_objfile->has_section_at_zero = 1;
1450 }
1451
1452 /* Decompress a section that was compressed using zlib.  Store the
1453    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1454
1455 static void
1456 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1457                          gdb_byte **outbuf, bfd_size_type *outsize)
1458 {
1459   bfd *abfd = objfile->obfd;
1460 #ifndef HAVE_ZLIB_H
1461   error (_("Support for zlib-compressed DWARF data (from '%s') "
1462            "is disabled in this copy of GDB"),
1463          bfd_get_filename (abfd));
1464 #else
1465   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1466   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1467   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1468   bfd_size_type uncompressed_size;
1469   gdb_byte *uncompressed_buffer;
1470   z_stream strm;
1471   int rc;
1472   int header_size = 12;
1473
1474   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1475       || bfd_bread (compressed_buffer,
1476                     compressed_size, abfd) != compressed_size)
1477     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1478            bfd_get_filename (abfd));
1479
1480   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1481      by the uncompressed section size, 8 bytes in big-endian order.  */
1482   if (compressed_size < header_size
1483       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1484     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1485            bfd_get_filename (abfd));
1486   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1487   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1488   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1489   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1490   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1491   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1492   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1493   uncompressed_size += compressed_buffer[11];
1494
1495   /* It is possible the section consists of several compressed
1496      buffers concatenated together, so we uncompress in a loop.  */
1497   strm.zalloc = NULL;
1498   strm.zfree = NULL;
1499   strm.opaque = NULL;
1500   strm.avail_in = compressed_size - header_size;
1501   strm.next_in = (Bytef*) compressed_buffer + header_size;
1502   strm.avail_out = uncompressed_size;
1503   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1504                                        uncompressed_size);
1505   rc = inflateInit (&strm);
1506   while (strm.avail_in > 0)
1507     {
1508       if (rc != Z_OK)
1509         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1510                bfd_get_filename (abfd), rc);
1511       strm.next_out = ((Bytef*) uncompressed_buffer
1512                        + (uncompressed_size - strm.avail_out));
1513       rc = inflate (&strm, Z_FINISH);
1514       if (rc != Z_STREAM_END)
1515         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1516                bfd_get_filename (abfd), rc);
1517       rc = inflateReset (&strm);
1518     }
1519   rc = inflateEnd (&strm);
1520   if (rc != Z_OK
1521       || strm.avail_out != 0)
1522     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1523            bfd_get_filename (abfd), rc);
1524
1525   do_cleanups (cleanup);
1526   *outbuf = uncompressed_buffer;
1527   *outsize = uncompressed_size;
1528 #endif
1529 }
1530
1531 /* A helper function that decides whether a section is empty.  */
1532
1533 static int
1534 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1535 {
1536   return info->asection == NULL || info->size == 0;
1537 }
1538
1539 /* Read the contents of the section INFO from object file specified by
1540    OBJFILE, store info about the section into INFO.
1541    If the section is compressed, uncompress it before returning.  */
1542
1543 static void
1544 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1545 {
1546   bfd *abfd = objfile->obfd;
1547   asection *sectp = info->asection;
1548   gdb_byte *buf, *retbuf;
1549   unsigned char header[4];
1550
1551   if (info->readin)
1552     return;
1553   info->buffer = NULL;
1554   info->map_addr = NULL;
1555   info->readin = 1;
1556
1557   if (dwarf2_section_empty_p (info))
1558     return;
1559
1560   /* Check if the file has a 4-byte header indicating compression.  */
1561   if (info->size > sizeof (header)
1562       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1563       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1564     {
1565       /* Upon decompression, update the buffer and its size.  */
1566       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1567         {
1568           zlib_decompress_section (objfile, sectp, &info->buffer,
1569                                    &info->size);
1570           return;
1571         }
1572     }
1573
1574 #ifdef HAVE_MMAP
1575   if (pagesize == 0)
1576     pagesize = getpagesize ();
1577
1578   /* Only try to mmap sections which are large enough: we don't want to
1579      waste space due to fragmentation.  Also, only try mmap for sections
1580      without relocations.  */
1581
1582   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1583     {
1584       info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1585                          MAP_PRIVATE, sectp->filepos,
1586                          &info->map_addr, &info->map_len);
1587
1588       if ((caddr_t)info->buffer != MAP_FAILED)
1589         {
1590 #if HAVE_POSIX_MADVISE
1591           posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1592 #endif
1593           return;
1594         }
1595     }
1596 #endif
1597
1598   /* If we get here, we are a normal, not-compressed section.  */
1599   info->buffer = buf
1600     = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602   /* When debugging .o files, we may need to apply relocations; see
1603      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604      We never compress sections in .o files, so we only need to
1605      try this when the section is not compressed.  */
1606   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1607   if (retbuf != NULL)
1608     {
1609       info->buffer = retbuf;
1610       return;
1611     }
1612
1613   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614       || bfd_bread (buf, info->size, abfd) != info->size)
1615     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616            bfd_get_filename (abfd));
1617 }
1618
1619 /* A helper function that returns the size of a section in a safe way.
1620    If you are positive that the section has been read before using the
1621    size, then it is safe to refer to the dwarf2_section_info object's
1622    "size" field directly.  In other cases, you must call this
1623    function, because for compressed sections the size field is not set
1624    correctly until the section has been read.  */
1625
1626 static bfd_size_type
1627 dwarf2_section_size (struct objfile *objfile,
1628                      struct dwarf2_section_info *info)
1629 {
1630   if (!info->readin)
1631     dwarf2_read_section (objfile, info);
1632   return info->size;
1633 }
1634
1635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1636    SECTION_NAME.  */
1637
1638 void
1639 dwarf2_get_section_info (struct objfile *objfile,
1640                          enum dwarf2_section_enum sect,
1641                          asection **sectp, gdb_byte **bufp,
1642                          bfd_size_type *sizep)
1643 {
1644   struct dwarf2_per_objfile *data
1645     = objfile_data (objfile, dwarf2_objfile_data_key);
1646   struct dwarf2_section_info *info;
1647
1648   /* We may see an objfile without any DWARF, in which case we just
1649      return nothing.  */
1650   if (data == NULL)
1651     {
1652       *sectp = NULL;
1653       *bufp = NULL;
1654       *sizep = 0;
1655       return;
1656     }
1657   switch (sect)
1658     {
1659     case DWARF2_DEBUG_FRAME:
1660       info = &data->frame;
1661       break;
1662     case DWARF2_EH_FRAME:
1663       info = &data->eh_frame;
1664       break;
1665     default:
1666       gdb_assert_not_reached ("unexpected section");
1667     }
1668
1669   dwarf2_read_section (objfile, info);
1670
1671   *sectp = info->asection;
1672   *bufp = info->buffer;
1673   *sizep = info->size;
1674 }
1675
1676 \f
1677 /* DWARF quick_symbols_functions support.  */
1678
1679 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1680    unique line tables, so we maintain a separate table of all .debug_line
1681    derived entries to support the sharing.
1682    All the quick functions need is the list of file names.  We discard the
1683    line_header when we're done and don't need to record it here.  */
1684 struct quick_file_names
1685 {
1686   /* The offset in .debug_line of the line table.  We hash on this.  */
1687   unsigned int offset;
1688
1689   /* The number of entries in file_names, real_names.  */
1690   unsigned int num_file_names;
1691
1692   /* The file names from the line table, after being run through
1693      file_full_name.  */
1694   const char **file_names;
1695
1696   /* The file names from the line table after being run through
1697      gdb_realpath.  These are computed lazily.  */
1698   const char **real_names;
1699 };
1700
1701 /* When using the index (and thus not using psymtabs), each CU has an
1702    object of this type.  This is used to hold information needed by
1703    the various "quick" methods.  */
1704 struct dwarf2_per_cu_quick_data
1705 {
1706   /* The file table.  This can be NULL if there was no file table
1707      or it's currently not read in.
1708      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
1709   struct quick_file_names *file_names;
1710
1711   /* The corresponding symbol table.  This is NULL if symbols for this
1712      CU have not yet been read.  */
1713   struct symtab *symtab;
1714
1715   /* A temporary mark bit used when iterating over all CUs in
1716      expand_symtabs_matching.  */
1717   unsigned int mark : 1;
1718
1719   /* True if we've tried to read the file table and found there isn't one.
1720      There will be no point in trying to read it again next time.  */
1721   unsigned int no_file_data : 1;
1722 };
1723
1724 /* Hash function for a quick_file_names.  */
1725
1726 static hashval_t
1727 hash_file_name_entry (const void *e)
1728 {
1729   const struct quick_file_names *file_data = e;
1730
1731   return file_data->offset;
1732 }
1733
1734 /* Equality function for a quick_file_names.  */
1735
1736 static int
1737 eq_file_name_entry (const void *a, const void *b)
1738 {
1739   const struct quick_file_names *ea = a;
1740   const struct quick_file_names *eb = b;
1741
1742   return ea->offset == eb->offset;
1743 }
1744
1745 /* Delete function for a quick_file_names.  */
1746
1747 static void
1748 delete_file_name_entry (void *e)
1749 {
1750   struct quick_file_names *file_data = e;
1751   int i;
1752
1753   for (i = 0; i < file_data->num_file_names; ++i)
1754     {
1755       xfree ((void*) file_data->file_names[i]);
1756       if (file_data->real_names)
1757         xfree ((void*) file_data->real_names[i]);
1758     }
1759
1760   /* The space for the struct itself lives on objfile_obstack,
1761      so we don't free it here.  */
1762 }
1763
1764 /* Create a quick_file_names hash table.  */
1765
1766 static htab_t
1767 create_quick_file_names_table (unsigned int nr_initial_entries)
1768 {
1769   return htab_create_alloc (nr_initial_entries,
1770                             hash_file_name_entry, eq_file_name_entry,
1771                             delete_file_name_entry, xcalloc, xfree);
1772 }
1773
1774 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
1775    have to be created afterwards.  You should call age_cached_comp_units after
1776    processing PER_CU->CU.  dw2_setup must have been already called.  */
1777
1778 static void
1779 load_cu (struct dwarf2_per_cu_data *per_cu)
1780 {
1781   if (per_cu->debug_types_section)
1782     load_full_type_unit (per_cu);
1783   else
1784     load_full_comp_unit (per_cu);
1785
1786   gdb_assert (per_cu->cu != NULL);
1787
1788   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1789 }
1790
1791 /* Read in the symbols for PER_CU.  */
1792
1793 static void
1794 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1795 {
1796   struct cleanup *back_to;
1797
1798   back_to = make_cleanup (dwarf2_release_queue, NULL);
1799
1800   queue_comp_unit (per_cu);
1801
1802   load_cu (per_cu);
1803
1804   process_queue ();
1805
1806   /* Age the cache, releasing compilation units that have not
1807      been used recently.  */
1808   age_cached_comp_units ();
1809
1810   do_cleanups (back_to);
1811 }
1812
1813 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1814    the objfile from which this CU came.  Returns the resulting symbol
1815    table.  */
1816
1817 static struct symtab *
1818 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1819 {
1820   if (!per_cu->v.quick->symtab)
1821     {
1822       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1823       increment_reading_symtab ();
1824       dw2_do_instantiate_symtab (per_cu);
1825       do_cleanups (back_to);
1826     }
1827   return per_cu->v.quick->symtab;
1828 }
1829
1830 /* Return the CU given its index.  */
1831
1832 static struct dwarf2_per_cu_data *
1833 dw2_get_cu (int index)
1834 {
1835   if (index >= dwarf2_per_objfile->n_comp_units)
1836     {
1837       index -= dwarf2_per_objfile->n_comp_units;
1838       return dwarf2_per_objfile->all_type_units[index];
1839     }
1840   return dwarf2_per_objfile->all_comp_units[index];
1841 }
1842
1843 /* A helper function that knows how to read a 64-bit value in a way
1844    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1845    otherwise.  */
1846
1847 static int
1848 extract_cu_value (const char *bytes, ULONGEST *result)
1849 {
1850   if (sizeof (ULONGEST) < 8)
1851     {
1852       int i;
1853
1854       /* Ignore the upper 4 bytes if they are all zero.  */
1855       for (i = 0; i < 4; ++i)
1856         if (bytes[i + 4] != 0)
1857           return 0;
1858
1859       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1860     }
1861   else
1862     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1863   return 1;
1864 }
1865
1866 /* Read the CU list from the mapped index, and use it to create all
1867    the CU objects for this objfile.  Return 0 if something went wrong,
1868    1 if everything went ok.  */
1869
1870 static int
1871 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1872                        offset_type cu_list_elements)
1873 {
1874   offset_type i;
1875
1876   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1877   dwarf2_per_objfile->all_comp_units
1878     = obstack_alloc (&objfile->objfile_obstack,
1879                      dwarf2_per_objfile->n_comp_units
1880                      * sizeof (struct dwarf2_per_cu_data *));
1881
1882   for (i = 0; i < cu_list_elements; i += 2)
1883     {
1884       struct dwarf2_per_cu_data *the_cu;
1885       ULONGEST offset, length;
1886
1887       if (!extract_cu_value (cu_list, &offset)
1888           || !extract_cu_value (cu_list + 8, &length))
1889         return 0;
1890       cu_list += 2 * 8;
1891
1892       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1893                                struct dwarf2_per_cu_data);
1894       the_cu->offset = offset;
1895       the_cu->length = length;
1896       the_cu->objfile = objfile;
1897       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1898                                         struct dwarf2_per_cu_quick_data);
1899       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1900     }
1901
1902   return 1;
1903 }
1904
1905 /* Create the signatured type hash table from the index.  */
1906
1907 static int
1908 create_signatured_type_table_from_index (struct objfile *objfile,
1909                                          struct dwarf2_section_info *section,
1910                                          const gdb_byte *bytes,
1911                                          offset_type elements)
1912 {
1913   offset_type i;
1914   htab_t sig_types_hash;
1915
1916   dwarf2_per_objfile->n_type_units = elements / 3;
1917   dwarf2_per_objfile->all_type_units
1918     = obstack_alloc (&objfile->objfile_obstack,
1919                      dwarf2_per_objfile->n_type_units
1920                      * sizeof (struct dwarf2_per_cu_data *));
1921
1922   sig_types_hash = allocate_signatured_type_table (objfile);
1923
1924   for (i = 0; i < elements; i += 3)
1925     {
1926       struct signatured_type *type_sig;
1927       ULONGEST offset, type_offset, signature;
1928       void **slot;
1929
1930       if (!extract_cu_value (bytes, &offset)
1931           || !extract_cu_value (bytes + 8, &type_offset))
1932         return 0;
1933       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1934       bytes += 3 * 8;
1935
1936       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1937                                  struct signatured_type);
1938       type_sig->signature = signature;
1939       type_sig->type_offset = type_offset;
1940       type_sig->per_cu.debug_types_section = section;
1941       type_sig->per_cu.offset = offset;
1942       type_sig->per_cu.objfile = objfile;
1943       type_sig->per_cu.v.quick
1944         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945                           struct dwarf2_per_cu_quick_data);
1946
1947       slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1948       *slot = type_sig;
1949
1950       dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1951     }
1952
1953   dwarf2_per_objfile->signatured_types = sig_types_hash;
1954
1955   return 1;
1956 }
1957
1958 /* Read the address map data from the mapped index, and use it to
1959    populate the objfile's psymtabs_addrmap.  */
1960
1961 static void
1962 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1963 {
1964   const gdb_byte *iter, *end;
1965   struct obstack temp_obstack;
1966   struct addrmap *mutable_map;
1967   struct cleanup *cleanup;
1968   CORE_ADDR baseaddr;
1969
1970   obstack_init (&temp_obstack);
1971   cleanup = make_cleanup_obstack_free (&temp_obstack);
1972   mutable_map = addrmap_create_mutable (&temp_obstack);
1973
1974   iter = index->address_table;
1975   end = iter + index->address_table_size;
1976
1977   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1978
1979   while (iter < end)
1980     {
1981       ULONGEST hi, lo, cu_index;
1982       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1983       iter += 8;
1984       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985       iter += 8;
1986       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1987       iter += 4;
1988       
1989       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1990                          dw2_get_cu (cu_index));
1991     }
1992
1993   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1994                                                     &objfile->objfile_obstack);
1995   do_cleanups (cleanup);
1996 }
1997
1998 /* The hash function for strings in the mapped index.  This is the same as
1999    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2000    implementation.  This is necessary because the hash function is tied to the
2001    format of the mapped index file.  The hash values do not have to match with
2002    SYMBOL_HASH_NEXT.
2003    
2004    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
2005
2006 static hashval_t
2007 mapped_index_string_hash (int index_version, const void *p)
2008 {
2009   const unsigned char *str = (const unsigned char *) p;
2010   hashval_t r = 0;
2011   unsigned char c;
2012
2013   while ((c = *str++) != 0)
2014     {
2015       if (index_version >= 5)
2016         c = tolower (c);
2017       r = r * 67 + c - 113;
2018     }
2019
2020   return r;
2021 }
2022
2023 /* Find a slot in the mapped index INDEX for the object named NAME.
2024    If NAME is found, set *VEC_OUT to point to the CU vector in the
2025    constant pool and return 1.  If NAME cannot be found, return 0.  */
2026
2027 static int
2028 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2029                           offset_type **vec_out)
2030 {
2031   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2032   offset_type hash;
2033   offset_type slot, step;
2034   int (*cmp) (const char *, const char *);
2035
2036   if (current_language->la_language == language_cplus
2037       || current_language->la_language == language_java
2038       || current_language->la_language == language_fortran)
2039     {
2040       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
2041          not contain any.  */
2042       const char *paren = strchr (name, '(');
2043
2044       if (paren)
2045         {
2046           char *dup;
2047
2048           dup = xmalloc (paren - name + 1);
2049           memcpy (dup, name, paren - name);
2050           dup[paren - name] = 0;
2051
2052           make_cleanup (xfree, dup);
2053           name = dup;
2054         }
2055     }
2056
2057   /* Index version 4 did not support case insensitive searches.  But the
2058      indices for case insensitive languages are built in lowercase, therefore
2059      simulate our NAME being searched is also lowercased.  */
2060   hash = mapped_index_string_hash ((index->version == 4
2061                                     && case_sensitivity == case_sensitive_off
2062                                     ? 5 : index->version),
2063                                    name);
2064
2065   slot = hash & (index->symbol_table_slots - 1);
2066   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2067   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2068
2069   for (;;)
2070     {
2071       /* Convert a slot number to an offset into the table.  */
2072       offset_type i = 2 * slot;
2073       const char *str;
2074       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2075         {
2076           do_cleanups (back_to);
2077           return 0;
2078         }
2079
2080       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2081       if (!cmp (name, str))
2082         {
2083           *vec_out = (offset_type *) (index->constant_pool
2084                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
2085           do_cleanups (back_to);
2086           return 1;
2087         }
2088
2089       slot = (slot + step) & (index->symbol_table_slots - 1);
2090     }
2091 }
2092
2093 /* Read the index file.  If everything went ok, initialize the "quick"
2094    elements of all the CUs and return 1.  Otherwise, return 0.  */
2095
2096 static int
2097 dwarf2_read_index (struct objfile *objfile)
2098 {
2099   char *addr;
2100   struct mapped_index *map;
2101   offset_type *metadata;
2102   const gdb_byte *cu_list;
2103   const gdb_byte *types_list = NULL;
2104   offset_type version, cu_list_elements;
2105   offset_type types_list_elements = 0;
2106   int i;
2107
2108   if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2109     return 0;
2110
2111   /* Older elfutils strip versions could keep the section in the main
2112      executable while splitting it for the separate debug info file.  */
2113   if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2114        & SEC_HAS_CONTENTS) == 0)
2115     return 0;
2116
2117   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2118
2119   addr = dwarf2_per_objfile->gdb_index.buffer;
2120   /* Version check.  */
2121   version = MAYBE_SWAP (*(offset_type *) addr);
2122   /* Versions earlier than 3 emitted every copy of a psymbol.  This
2123      causes the index to behave very poorly for certain requests.  Version 3
2124      contained incomplete addrmap.  So, it seems better to just ignore such
2125      indices.  */
2126   if (version < 4)
2127     {
2128       static int warning_printed = 0;
2129       if (!warning_printed)
2130         {
2131           warning (_("Skipping obsolete .gdb_index section in %s."),
2132                    objfile->name);
2133           warning_printed = 1;
2134         }
2135       return 0;
2136     }
2137   /* Index version 4 uses a different hash function than index version
2138      5 and later.
2139
2140      Versions earlier than 6 did not emit psymbols for inlined
2141      functions.  Using these files will cause GDB not to be able to
2142      set breakpoints on inlined functions by name, so we ignore these
2143      indices unless the --use-deprecated-index-sections command line
2144      option was supplied.  */
2145   if (version < 6 && !use_deprecated_index_sections)
2146     {
2147       static int warning_printed = 0;
2148       if (!warning_printed)
2149         {
2150           warning (_("Skipping deprecated .gdb_index section in %s, pass "
2151                      "--use-deprecated-index-sections to use them anyway"),
2152                    objfile->name);
2153           warning_printed = 1;
2154         }
2155       return 0;
2156     }
2157   /* Indexes with higher version than the one supported by GDB may be no
2158      longer backward compatible.  */
2159   if (version > 6)
2160     return 0;
2161
2162   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2163   map->version = version;
2164   map->total_size = dwarf2_per_objfile->gdb_index.size;
2165
2166   metadata = (offset_type *) (addr + sizeof (offset_type));
2167
2168   i = 0;
2169   cu_list = addr + MAYBE_SWAP (metadata[i]);
2170   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2171                       / 8);
2172   ++i;
2173
2174   types_list = addr + MAYBE_SWAP (metadata[i]);
2175   types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2176                           - MAYBE_SWAP (metadata[i]))
2177                          / 8);
2178   ++i;
2179
2180   map->address_table = addr + MAYBE_SWAP (metadata[i]);
2181   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2182                              - MAYBE_SWAP (metadata[i]));
2183   ++i;
2184
2185   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2186   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2187                               - MAYBE_SWAP (metadata[i]))
2188                              / (2 * sizeof (offset_type)));
2189   ++i;
2190
2191   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2192
2193   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2194     return 0;
2195
2196   if (types_list_elements)
2197     {
2198       struct dwarf2_section_info *section;
2199
2200       /* We can only handle a single .debug_types when we have an
2201          index.  */
2202       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2203         return 0;
2204
2205       section = VEC_index (dwarf2_section_info_def,
2206                            dwarf2_per_objfile->types, 0);
2207
2208       if (!create_signatured_type_table_from_index (objfile, section,
2209                                                     types_list,
2210                                                     types_list_elements))
2211         return 0;
2212     }
2213
2214   create_addrmap_from_index (objfile, map);
2215
2216   dwarf2_per_objfile->index_table = map;
2217   dwarf2_per_objfile->using_index = 1;
2218   dwarf2_per_objfile->quick_file_names_table =
2219     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2220
2221   return 1;
2222 }
2223
2224 /* A helper for the "quick" functions which sets the global
2225    dwarf2_per_objfile according to OBJFILE.  */
2226
2227 static void
2228 dw2_setup (struct objfile *objfile)
2229 {
2230   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2231   gdb_assert (dwarf2_per_objfile);
2232 }
2233
2234 /* A helper for the "quick" functions which attempts to read the line
2235    table for THIS_CU.  */
2236
2237 static struct quick_file_names *
2238 dw2_get_file_names (struct objfile *objfile,
2239                     struct dwarf2_per_cu_data *this_cu)
2240 {
2241   bfd *abfd = objfile->obfd;
2242   struct line_header *lh;
2243   struct attribute *attr;
2244   struct cleanup *cleanups;
2245   struct die_info *comp_unit_die;
2246   struct dwarf2_section_info* sec;
2247   gdb_byte *info_ptr;
2248   int has_children, i;
2249   struct dwarf2_cu cu;
2250   unsigned int bytes_read;
2251   struct die_reader_specs reader_specs;
2252   char *name, *comp_dir;
2253   void **slot;
2254   struct quick_file_names *qfn;
2255   unsigned int line_offset;
2256
2257   if (this_cu->v.quick->file_names != NULL)
2258     return this_cu->v.quick->file_names;
2259   /* If we know there is no line data, no point in looking again.  */
2260   if (this_cu->v.quick->no_file_data)
2261     return NULL;
2262
2263   init_one_comp_unit (&cu, this_cu);
2264   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2265
2266   if (this_cu->debug_types_section)
2267     sec = this_cu->debug_types_section;
2268   else
2269     sec = &dwarf2_per_objfile->info;
2270   dwarf2_read_section (objfile, sec);
2271   info_ptr = sec->buffer + this_cu->offset;
2272
2273   info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2274                                             this_cu->debug_types_section != NULL);
2275
2276   /* Skip dummy compilation units.  */
2277   if (info_ptr >= (sec->buffer + sec->size)
2278       || peek_abbrev_code (abfd, info_ptr) == 0)
2279     {
2280       do_cleanups (cleanups);
2281       return NULL;
2282     }
2283
2284   dwarf2_read_abbrevs (&cu);
2285   make_cleanup (dwarf2_free_abbrev_table, &cu);
2286
2287   init_cu_die_reader (&reader_specs, &cu);
2288   read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2289                  &has_children);
2290
2291   lh = NULL;
2292   slot = NULL;
2293   line_offset = 0;
2294   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2295   if (attr)
2296     {
2297       struct quick_file_names find_entry;
2298
2299       line_offset = DW_UNSND (attr);
2300
2301       /* We may have already read in this line header (TU line header sharing).
2302          If we have we're done.  */
2303       find_entry.offset = line_offset;
2304       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2305                              &find_entry, INSERT);
2306       if (*slot != NULL)
2307         {
2308           do_cleanups (cleanups);
2309           this_cu->v.quick->file_names = *slot;
2310           return *slot;
2311         }
2312
2313       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2314     }
2315   if (lh == NULL)
2316     {
2317       do_cleanups (cleanups);
2318       this_cu->v.quick->no_file_data = 1;
2319       return NULL;
2320     }
2321
2322   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2323   qfn->offset = line_offset;
2324   gdb_assert (slot != NULL);
2325   *slot = qfn;
2326
2327   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2328
2329   qfn->num_file_names = lh->num_file_names;
2330   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2331                                    lh->num_file_names * sizeof (char *));
2332   for (i = 0; i < lh->num_file_names; ++i)
2333     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2334   qfn->real_names = NULL;
2335
2336   free_line_header (lh);
2337   do_cleanups (cleanups);
2338
2339   this_cu->v.quick->file_names = qfn;
2340   return qfn;
2341 }
2342
2343 /* A helper for the "quick" functions which computes and caches the
2344    real path for a given file name from the line table.  */
2345
2346 static const char *
2347 dw2_get_real_path (struct objfile *objfile,
2348                    struct quick_file_names *qfn, int index)
2349 {
2350   if (qfn->real_names == NULL)
2351     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2352                                       qfn->num_file_names, sizeof (char *));
2353
2354   if (qfn->real_names[index] == NULL)
2355     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2356
2357   return qfn->real_names[index];
2358 }
2359
2360 static struct symtab *
2361 dw2_find_last_source_symtab (struct objfile *objfile)
2362 {
2363   int index;
2364
2365   dw2_setup (objfile);
2366   index = dwarf2_per_objfile->n_comp_units - 1;
2367   return dw2_instantiate_symtab (dw2_get_cu (index));
2368 }
2369
2370 /* Traversal function for dw2_forget_cached_source_info.  */
2371
2372 static int
2373 dw2_free_cached_file_names (void **slot, void *info)
2374 {
2375   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2376
2377   if (file_data->real_names)
2378     {
2379       int i;
2380
2381       for (i = 0; i < file_data->num_file_names; ++i)
2382         {
2383           xfree ((void*) file_data->real_names[i]);
2384           file_data->real_names[i] = NULL;
2385         }
2386     }
2387
2388   return 1;
2389 }
2390
2391 static void
2392 dw2_forget_cached_source_info (struct objfile *objfile)
2393 {
2394   dw2_setup (objfile);
2395
2396   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2397                           dw2_free_cached_file_names, NULL);
2398 }
2399
2400 /* Helper function for dw2_map_symtabs_matching_filename that expands
2401    the symtabs and calls the iterator.  */
2402
2403 static int
2404 dw2_map_expand_apply (struct objfile *objfile,
2405                       struct dwarf2_per_cu_data *per_cu,
2406                       const char *name,
2407                       const char *full_path, const char *real_path,
2408                       int (*callback) (struct symtab *, void *),
2409                       void *data)
2410 {
2411   struct symtab *last_made = objfile->symtabs;
2412
2413   /* Don't visit already-expanded CUs.  */
2414   if (per_cu->v.quick->symtab)
2415     return 0;
2416
2417   /* This may expand more than one symtab, and we want to iterate over
2418      all of them.  */
2419   dw2_instantiate_symtab (per_cu);
2420
2421   return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2422                                     objfile->symtabs, last_made);
2423 }
2424
2425 /* Implementation of the map_symtabs_matching_filename method.  */
2426
2427 static int
2428 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2429                                    const char *full_path, const char *real_path,
2430                                    int (*callback) (struct symtab *, void *),
2431                                    void *data)
2432 {
2433   int i;
2434   const char *name_basename = lbasename (name);
2435   int name_len = strlen (name);
2436   int is_abs = IS_ABSOLUTE_PATH (name);
2437
2438   dw2_setup (objfile);
2439
2440   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2441                    + dwarf2_per_objfile->n_type_units); ++i)
2442     {
2443       int j;
2444       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2445       struct quick_file_names *file_data;
2446
2447       /* We only need to look at symtabs not already expanded.  */
2448       if (per_cu->v.quick->symtab)
2449         continue;
2450
2451       file_data = dw2_get_file_names (objfile, per_cu);
2452       if (file_data == NULL)
2453         continue;
2454
2455       for (j = 0; j < file_data->num_file_names; ++j)
2456         {
2457           const char *this_name = file_data->file_names[j];
2458
2459           if (FILENAME_CMP (name, this_name) == 0
2460               || (!is_abs && compare_filenames_for_search (this_name,
2461                                                            name, name_len)))
2462             {
2463               if (dw2_map_expand_apply (objfile, per_cu,
2464                                         name, full_path, real_path,
2465                                         callback, data))
2466                 return 1;
2467             }
2468
2469           /* Before we invoke realpath, which can get expensive when many
2470              files are involved, do a quick comparison of the basenames.  */
2471           if (! basenames_may_differ
2472               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2473             continue;
2474
2475           if (full_path != NULL)
2476             {
2477               const char *this_real_name = dw2_get_real_path (objfile,
2478                                                               file_data, j);
2479
2480               if (this_real_name != NULL
2481                   && (FILENAME_CMP (full_path, this_real_name) == 0
2482                       || (!is_abs
2483                           && compare_filenames_for_search (this_real_name,
2484                                                            name, name_len))))
2485                 {
2486                   if (dw2_map_expand_apply (objfile, per_cu,
2487                                             name, full_path, real_path,
2488                                             callback, data))
2489                     return 1;
2490                 }
2491             }
2492
2493           if (real_path != NULL)
2494             {
2495               const char *this_real_name = dw2_get_real_path (objfile,
2496                                                               file_data, j);
2497
2498               if (this_real_name != NULL
2499                   && (FILENAME_CMP (real_path, this_real_name) == 0
2500                       || (!is_abs
2501                           && compare_filenames_for_search (this_real_name,
2502                                                            name, name_len))))
2503                 {
2504                   if (dw2_map_expand_apply (objfile, per_cu,
2505                                             name, full_path, real_path,
2506                                             callback, data))
2507                     return 1;
2508                 }
2509             }
2510         }
2511     }
2512
2513   return 0;
2514 }
2515
2516 static struct symtab *
2517 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2518                    const char *name, domain_enum domain)
2519 {
2520   /* We do all the work in the pre_expand_symtabs_matching hook
2521      instead.  */
2522   return NULL;
2523 }
2524
2525 /* A helper function that expands all symtabs that hold an object
2526    named NAME.  */
2527
2528 static void
2529 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2530 {
2531   dw2_setup (objfile);
2532
2533   /* index_table is NULL if OBJF_READNOW.  */
2534   if (dwarf2_per_objfile->index_table)
2535     {
2536       offset_type *vec;
2537
2538       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2539                                     name, &vec))
2540         {
2541           offset_type i, len = MAYBE_SWAP (*vec);
2542           for (i = 0; i < len; ++i)
2543             {
2544               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2545               struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2546
2547               dw2_instantiate_symtab (per_cu);
2548             }
2549         }
2550     }
2551 }
2552
2553 static void
2554 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2555                                  enum block_enum block_kind, const char *name,
2556                                  domain_enum domain)
2557 {
2558   dw2_do_expand_symtabs_matching (objfile, name);
2559 }
2560
2561 static void
2562 dw2_print_stats (struct objfile *objfile)
2563 {
2564   int i, count;
2565
2566   dw2_setup (objfile);
2567   count = 0;
2568   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2569                    + dwarf2_per_objfile->n_type_units); ++i)
2570     {
2571       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2572
2573       if (!per_cu->v.quick->symtab)
2574         ++count;
2575     }
2576   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2577 }
2578
2579 static void
2580 dw2_dump (struct objfile *objfile)
2581 {
2582   /* Nothing worth printing.  */
2583 }
2584
2585 static void
2586 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2587               struct section_offsets *delta)
2588 {
2589   /* There's nothing to relocate here.  */
2590 }
2591
2592 static void
2593 dw2_expand_symtabs_for_function (struct objfile *objfile,
2594                                  const char *func_name)
2595 {
2596   dw2_do_expand_symtabs_matching (objfile, func_name);
2597 }
2598
2599 static void
2600 dw2_expand_all_symtabs (struct objfile *objfile)
2601 {
2602   int i;
2603
2604   dw2_setup (objfile);
2605
2606   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2607                    + dwarf2_per_objfile->n_type_units); ++i)
2608     {
2609       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2610
2611       dw2_instantiate_symtab (per_cu);
2612     }
2613 }
2614
2615 static void
2616 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2617                                   const char *filename)
2618 {
2619   int i;
2620
2621   dw2_setup (objfile);
2622
2623   /* We don't need to consider type units here.
2624      This is only called for examining code, e.g. expand_line_sal.
2625      There can be an order of magnitude (or more) more type units
2626      than comp units, and we avoid them if we can.  */
2627
2628   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2629     {
2630       int j;
2631       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2632       struct quick_file_names *file_data;
2633
2634       /* We only need to look at symtabs not already expanded.  */
2635       if (per_cu->v.quick->symtab)
2636         continue;
2637
2638       file_data = dw2_get_file_names (objfile, per_cu);
2639       if (file_data == NULL)
2640         continue;
2641
2642       for (j = 0; j < file_data->num_file_names; ++j)
2643         {
2644           const char *this_name = file_data->file_names[j];
2645           if (FILENAME_CMP (this_name, filename) == 0)
2646             {
2647               dw2_instantiate_symtab (per_cu);
2648               break;
2649             }
2650         }
2651     }
2652 }
2653
2654 static const char *
2655 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2656 {
2657   struct dwarf2_per_cu_data *per_cu;
2658   offset_type *vec;
2659   struct quick_file_names *file_data;
2660
2661   dw2_setup (objfile);
2662
2663   /* index_table is NULL if OBJF_READNOW.  */
2664   if (!dwarf2_per_objfile->index_table)
2665     {
2666       struct symtab *s;
2667
2668       ALL_OBJFILE_SYMTABS (objfile, s)
2669         if (s->primary)
2670           {
2671             struct blockvector *bv = BLOCKVECTOR (s);
2672             const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2673             struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2674
2675             if (sym)
2676               return sym->symtab->filename;
2677           }
2678       return NULL;
2679     }
2680
2681   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2682                                  name, &vec))
2683     return NULL;
2684
2685   /* Note that this just looks at the very first one named NAME -- but
2686      actually we are looking for a function.  find_main_filename
2687      should be rewritten so that it doesn't require a custom hook.  It
2688      could just use the ordinary symbol tables.  */
2689   /* vec[0] is the length, which must always be >0.  */
2690   per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2691
2692   file_data = dw2_get_file_names (objfile, per_cu);
2693   if (file_data == NULL)
2694     return NULL;
2695
2696   return file_data->file_names[file_data->num_file_names - 1];
2697 }
2698
2699 static void
2700 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2701                           struct objfile *objfile, int global,
2702                           int (*callback) (struct block *,
2703                                            struct symbol *, void *),
2704                           void *data, symbol_compare_ftype *match,
2705                           symbol_compare_ftype *ordered_compare)
2706 {
2707   /* Currently unimplemented; used for Ada.  The function can be called if the
2708      current language is Ada for a non-Ada objfile using GNU index.  As Ada
2709      does not look for non-Ada symbols this function should just return.  */
2710 }
2711
2712 static void
2713 dw2_expand_symtabs_matching
2714   (struct objfile *objfile,
2715    int (*file_matcher) (const char *, void *),
2716    int (*name_matcher) (const char *, void *),
2717    enum search_domain kind,
2718    void *data)
2719 {
2720   int i;
2721   offset_type iter;
2722   struct mapped_index *index;
2723
2724   dw2_setup (objfile);
2725
2726   /* index_table is NULL if OBJF_READNOW.  */
2727   if (!dwarf2_per_objfile->index_table)
2728     return;
2729   index = dwarf2_per_objfile->index_table;
2730
2731   if (file_matcher != NULL)
2732     {
2733       struct cleanup *cleanup;
2734       htab_t visited_found, visited_not_found;
2735
2736       visited_found = htab_create_alloc (10,
2737                                          htab_hash_pointer, htab_eq_pointer,
2738                                          NULL, xcalloc, xfree);
2739       cleanup = make_cleanup_htab_delete (visited_found);
2740       visited_not_found = htab_create_alloc (10,
2741                                              htab_hash_pointer, htab_eq_pointer,
2742                                              NULL, xcalloc, xfree);
2743       make_cleanup_htab_delete (visited_not_found);
2744
2745       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2746                        + dwarf2_per_objfile->n_type_units); ++i)
2747         {
2748           int j;
2749           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2750           struct quick_file_names *file_data;
2751           void **slot;
2752
2753           per_cu->v.quick->mark = 0;
2754
2755           /* We only need to look at symtabs not already expanded.  */
2756           if (per_cu->v.quick->symtab)
2757             continue;
2758
2759           file_data = dw2_get_file_names (objfile, per_cu);
2760           if (file_data == NULL)
2761             continue;
2762
2763           if (htab_find (visited_not_found, file_data) != NULL)
2764             continue;
2765           else if (htab_find (visited_found, file_data) != NULL)
2766             {
2767               per_cu->v.quick->mark = 1;
2768               continue;
2769             }
2770
2771           for (j = 0; j < file_data->num_file_names; ++j)
2772             {
2773               if (file_matcher (file_data->file_names[j], data))
2774                 {
2775                   per_cu->v.quick->mark = 1;
2776                   break;
2777                 }
2778             }
2779
2780           slot = htab_find_slot (per_cu->v.quick->mark
2781                                  ? visited_found
2782                                  : visited_not_found,
2783                                  file_data, INSERT);
2784           *slot = file_data;
2785         }
2786
2787       do_cleanups (cleanup);
2788     }
2789
2790   for (iter = 0; iter < index->symbol_table_slots; ++iter)
2791     {
2792       offset_type idx = 2 * iter;
2793       const char *name;
2794       offset_type *vec, vec_len, vec_idx;
2795
2796       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2797         continue;
2798
2799       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2800
2801       if (! (*name_matcher) (name, data))
2802         continue;
2803
2804       /* The name was matched, now expand corresponding CUs that were
2805          marked.  */
2806       vec = (offset_type *) (index->constant_pool
2807                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
2808       vec_len = MAYBE_SWAP (vec[0]);
2809       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2810         {
2811           struct dwarf2_per_cu_data *per_cu;
2812
2813           per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2814           if (file_matcher == NULL || per_cu->v.quick->mark)
2815             dw2_instantiate_symtab (per_cu);
2816         }
2817     }
2818 }
2819
2820 static struct symtab *
2821 dw2_find_pc_sect_symtab (struct objfile *objfile,
2822                          struct minimal_symbol *msymbol,
2823                          CORE_ADDR pc,
2824                          struct obj_section *section,
2825                          int warn_if_readin)
2826 {
2827   struct dwarf2_per_cu_data *data;
2828
2829   dw2_setup (objfile);
2830
2831   if (!objfile->psymtabs_addrmap)
2832     return NULL;
2833
2834   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2835   if (!data)
2836     return NULL;
2837
2838   if (warn_if_readin && data->v.quick->symtab)
2839     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2840              paddress (get_objfile_arch (objfile), pc));
2841
2842   return dw2_instantiate_symtab (data);
2843 }
2844
2845 static void
2846 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2847                           void *data, int need_fullname)
2848 {
2849   int i;
2850   struct cleanup *cleanup;
2851   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2852                                       NULL, xcalloc, xfree);
2853
2854   cleanup = make_cleanup_htab_delete (visited);
2855   dw2_setup (objfile);
2856
2857   /* We can ignore file names coming from already-expanded CUs.  */
2858   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2859                    + dwarf2_per_objfile->n_type_units); ++i)
2860     {
2861       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2862
2863       if (per_cu->v.quick->symtab)
2864         {
2865           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2866                                         INSERT);
2867
2868           *slot = per_cu->v.quick->file_names;
2869         }
2870     }
2871
2872   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2873                    + dwarf2_per_objfile->n_type_units); ++i)
2874     {
2875       int j;
2876       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2877       struct quick_file_names *file_data;
2878       void **slot;
2879
2880       /* We only need to look at symtabs not already expanded.  */
2881       if (per_cu->v.quick->symtab)
2882         continue;
2883
2884       file_data = dw2_get_file_names (objfile, per_cu);
2885       if (file_data == NULL)
2886         continue;
2887
2888       slot = htab_find_slot (visited, file_data, INSERT);
2889       if (*slot)
2890         {
2891           /* Already visited.  */
2892           continue;
2893         }
2894       *slot = file_data;
2895
2896       for (j = 0; j < file_data->num_file_names; ++j)
2897         {
2898           const char *this_real_name;
2899
2900           if (need_fullname)
2901             this_real_name = dw2_get_real_path (objfile, file_data, j);
2902           else
2903             this_real_name = NULL;
2904           (*fun) (file_data->file_names[j], this_real_name, data);
2905         }
2906     }
2907
2908   do_cleanups (cleanup);
2909 }
2910
2911 static int
2912 dw2_has_symbols (struct objfile *objfile)
2913 {
2914   return 1;
2915 }
2916
2917 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2918 {
2919   dw2_has_symbols,
2920   dw2_find_last_source_symtab,
2921   dw2_forget_cached_source_info,
2922   dw2_map_symtabs_matching_filename,
2923   dw2_lookup_symbol,
2924   dw2_pre_expand_symtabs_matching,
2925   dw2_print_stats,
2926   dw2_dump,
2927   dw2_relocate,
2928   dw2_expand_symtabs_for_function,
2929   dw2_expand_all_symtabs,
2930   dw2_expand_symtabs_with_filename,
2931   dw2_find_symbol_file,
2932   dw2_map_matching_symbols,
2933   dw2_expand_symtabs_matching,
2934   dw2_find_pc_sect_symtab,
2935   dw2_map_symbol_filenames
2936 };
2937
2938 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2939    file will use psymtabs, or 1 if using the GNU index.  */
2940
2941 int
2942 dwarf2_initialize_objfile (struct objfile *objfile)
2943 {
2944   /* If we're about to read full symbols, don't bother with the
2945      indices.  In this case we also don't care if some other debug
2946      format is making psymtabs, because they are all about to be
2947      expanded anyway.  */
2948   if ((objfile->flags & OBJF_READNOW))
2949     {
2950       int i;
2951
2952       dwarf2_per_objfile->using_index = 1;
2953       create_all_comp_units (objfile);
2954       create_debug_types_hash_table (objfile);
2955       dwarf2_per_objfile->quick_file_names_table =
2956         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2957
2958       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2959                        + dwarf2_per_objfile->n_type_units); ++i)
2960         {
2961           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2962
2963           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964                                             struct dwarf2_per_cu_quick_data);
2965         }
2966
2967       /* Return 1 so that gdb sees the "quick" functions.  However,
2968          these functions will be no-ops because we will have expanded
2969          all symtabs.  */
2970       return 1;
2971     }
2972
2973   if (dwarf2_read_index (objfile))
2974     return 1;
2975
2976   return 0;
2977 }
2978
2979 \f
2980
2981 /* Build a partial symbol table.  */
2982
2983 void
2984 dwarf2_build_psymtabs (struct objfile *objfile)
2985 {
2986   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2987     {
2988       init_psymbol_list (objfile, 1024);
2989     }
2990
2991   dwarf2_build_psymtabs_hard (objfile);
2992 }
2993
2994 /* Return TRUE if OFFSET is within CU_HEADER.  */
2995
2996 static inline int
2997 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2998 {
2999   unsigned int bottom = cu_header->offset;
3000   unsigned int top = (cu_header->offset
3001                       + cu_header->length
3002                       + cu_header->initial_length_size);
3003
3004   return (offset >= bottom && offset < top);
3005 }
3006
3007 /* Read in the comp unit header information from the debug_info at info_ptr.
3008    NOTE: This leaves members offset, first_die_offset to be filled in
3009    by the caller.  */
3010
3011 static gdb_byte *
3012 read_comp_unit_head (struct comp_unit_head *cu_header,
3013                      gdb_byte *info_ptr, bfd *abfd)
3014 {
3015   int signed_addr;
3016   unsigned int bytes_read;
3017
3018   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3019   cu_header->initial_length_size = bytes_read;
3020   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3021   info_ptr += bytes_read;
3022   cu_header->version = read_2_bytes (abfd, info_ptr);
3023   info_ptr += 2;
3024   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
3025                                           &bytes_read);
3026   info_ptr += bytes_read;
3027   cu_header->addr_size = read_1_byte (abfd, info_ptr);
3028   info_ptr += 1;
3029   signed_addr = bfd_get_sign_extend_vma (abfd);
3030   if (signed_addr < 0)
3031     internal_error (__FILE__, __LINE__,
3032                     _("read_comp_unit_head: dwarf from non elf file"));
3033   cu_header->signed_addr_p = signed_addr;
3034
3035   return info_ptr;
3036 }
3037
3038 /* Subroutine of read_and_check_comp_unit_head and
3039    read_and_check_type_unit_head to simplify them.
3040    Perform various error checking on the header.  */
3041
3042 static void
3043 error_check_comp_unit_head (struct comp_unit_head *header,
3044                             struct dwarf2_section_info *section)
3045 {
3046   bfd *abfd = section->asection->owner;
3047   const char *filename = bfd_get_filename (abfd);
3048
3049   if (header->version != 2 && header->version != 3 && header->version != 4)
3050     error (_("Dwarf Error: wrong version in compilation unit header "
3051            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3052            filename);
3053
3054   if (header->abbrev_offset
3055       >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3056                               &dwarf2_per_objfile->abbrev))
3057     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3058            "(offset 0x%lx + 6) [in module %s]"),
3059            (long) header->abbrev_offset, (long) header->offset,
3060            filename);
3061
3062   /* Cast to unsigned long to use 64-bit arithmetic when possible to
3063      avoid potential 32-bit overflow.  */
3064   if (((unsigned long) header->offset
3065        + header->length + header->initial_length_size)
3066       > section->size)
3067     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3068            "(offset 0x%lx + 0) [in module %s]"),
3069            (long) header->length, (long) header->offset,
3070            filename);
3071 }
3072
3073 /* Read in a CU/TU header and perform some basic error checking.
3074    The contents of the header are stored in HEADER.
3075    The result is a pointer to the start of the first DIE.  */
3076
3077 static gdb_byte *
3078 read_and_check_comp_unit_head (struct comp_unit_head *header,
3079                                struct dwarf2_section_info *section,
3080                                gdb_byte *info_ptr,
3081                                int is_debug_types_section)
3082 {
3083   gdb_byte *beg_of_comp_unit = info_ptr;
3084   bfd *abfd = section->asection->owner;
3085
3086   header->offset = beg_of_comp_unit - section->buffer;
3087
3088   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3089
3090   /* If we're reading a type unit, skip over the signature and
3091      type_offset fields.  */
3092   if (is_debug_types_section)
3093     info_ptr += 8 /*signature*/ + header->offset_size;
3094
3095   header->first_die_offset = info_ptr - beg_of_comp_unit;
3096
3097   error_check_comp_unit_head (header, section);
3098
3099   return info_ptr;
3100 }
3101
3102 /* Read in the types comp unit header information from .debug_types entry at
3103    types_ptr.  The result is a pointer to one past the end of the header.  */
3104
3105 static gdb_byte *
3106 read_and_check_type_unit_head (struct comp_unit_head *header,
3107                                struct dwarf2_section_info *section,
3108                                gdb_byte *info_ptr,
3109                                ULONGEST *signature, unsigned int *type_offset)
3110 {
3111   gdb_byte *beg_of_comp_unit = info_ptr;
3112   bfd *abfd = section->asection->owner;
3113
3114   header->offset = beg_of_comp_unit - section->buffer;
3115
3116   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3117
3118   /* If we're reading a type unit, skip over the signature and
3119      type_offset fields.  */
3120   if (signature != NULL)
3121     *signature = read_8_bytes (abfd, info_ptr);
3122   info_ptr += 8;
3123   if (type_offset != NULL)
3124     *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3125   info_ptr += header->offset_size;
3126
3127   header->first_die_offset = info_ptr - beg_of_comp_unit;
3128
3129   error_check_comp_unit_head (header, section);
3130
3131   return info_ptr;
3132 }
3133
3134 /* Allocate a new partial symtab for file named NAME and mark this new
3135    partial symtab as being an include of PST.  */
3136
3137 static void
3138 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3139                                struct objfile *objfile)
3140 {
3141   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3142
3143   subpst->section_offsets = pst->section_offsets;
3144   subpst->textlow = 0;
3145   subpst->texthigh = 0;
3146
3147   subpst->dependencies = (struct partial_symtab **)
3148     obstack_alloc (&objfile->objfile_obstack,
3149                    sizeof (struct partial_symtab *));
3150   subpst->dependencies[0] = pst;
3151   subpst->number_of_dependencies = 1;
3152
3153   subpst->globals_offset = 0;
3154   subpst->n_global_syms = 0;
3155   subpst->statics_offset = 0;
3156   subpst->n_static_syms = 0;
3157   subpst->symtab = NULL;
3158   subpst->read_symtab = pst->read_symtab;
3159   subpst->readin = 0;
3160
3161   /* No private part is necessary for include psymtabs.  This property
3162      can be used to differentiate between such include psymtabs and
3163      the regular ones.  */
3164   subpst->read_symtab_private = NULL;
3165 }
3166
3167 /* Read the Line Number Program data and extract the list of files
3168    included by the source file represented by PST.  Build an include
3169    partial symtab for each of these included files.  */
3170
3171 static void
3172 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3173                                struct die_info *die,
3174                                struct partial_symtab *pst)
3175 {
3176   struct objfile *objfile = cu->objfile;
3177   bfd *abfd = objfile->obfd;
3178   struct line_header *lh = NULL;
3179   struct attribute *attr;
3180
3181   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3182   if (attr)
3183     {
3184       unsigned int line_offset = DW_UNSND (attr);
3185
3186       lh = dwarf_decode_line_header (line_offset, abfd, cu);
3187     }
3188   if (lh == NULL)
3189     return;  /* No linetable, so no includes.  */
3190
3191   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
3192   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3193
3194   free_line_header (lh);
3195 }
3196
3197 static hashval_t
3198 hash_type_signature (const void *item)
3199 {
3200   const struct signatured_type *type_sig = item;
3201
3202   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
3203   return type_sig->signature;
3204 }
3205
3206 static int
3207 eq_type_signature (const void *item_lhs, const void *item_rhs)
3208 {
3209   const struct signatured_type *lhs = item_lhs;
3210   const struct signatured_type *rhs = item_rhs;
3211
3212   return lhs->signature == rhs->signature;
3213 }
3214
3215 /* Allocate a hash table for signatured types.  */
3216
3217 static htab_t
3218 allocate_signatured_type_table (struct objfile *objfile)
3219 {
3220   return htab_create_alloc_ex (41,
3221                                hash_type_signature,
3222                                eq_type_signature,
3223                                NULL,
3224                                &objfile->objfile_obstack,
3225                                hashtab_obstack_allocate,
3226                                dummy_obstack_deallocate);
3227 }
3228
3229 /* A helper function to add a signatured type CU to a table.  */
3230
3231 static int
3232 add_signatured_type_cu_to_table (void **slot, void *datum)
3233 {
3234   struct signatured_type *sigt = *slot;
3235   struct dwarf2_per_cu_data ***datap = datum;
3236
3237   **datap = &sigt->per_cu;
3238   ++*datap;
3239
3240   return 1;
3241 }
3242
3243 /* Create the hash table of all entries in the .debug_types section(s).
3244    The result is zero if there is an error (e.g. missing .debug_types section),
3245    otherwise non-zero.  */
3246
3247 static int
3248 create_debug_types_hash_table (struct objfile *objfile)
3249 {
3250   htab_t types_htab = NULL;
3251   struct dwarf2_per_cu_data **iter;
3252   int ix;
3253   struct dwarf2_section_info *section;
3254
3255   if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3256     {
3257       dwarf2_per_objfile->signatured_types = NULL;
3258       return 0;
3259     }
3260
3261   for (ix = 0;
3262        VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3263                     ix, section);
3264        ++ix)
3265     {
3266       gdb_byte *info_ptr, *end_ptr;
3267
3268       dwarf2_read_section (objfile, section);
3269       info_ptr = section->buffer;
3270
3271       if (info_ptr == NULL)
3272         continue;
3273
3274       if (types_htab == NULL)
3275         types_htab = allocate_signatured_type_table (objfile);
3276
3277       if (dwarf2_die_debug)
3278         fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3279
3280       end_ptr = info_ptr + section->size;
3281       while (info_ptr < end_ptr)
3282         {
3283           unsigned int offset;
3284           unsigned int type_offset;
3285           ULONGEST signature;
3286           struct signatured_type *type_sig;
3287           void **slot;
3288           gdb_byte *ptr = info_ptr;
3289           struct comp_unit_head header;
3290
3291           offset = ptr - section->buffer;
3292
3293           /* We need to read the type's signature in order to build the hash
3294              table, but we don't need anything else just yet.  */
3295
3296           ptr = read_and_check_type_unit_head (&header, section, ptr,
3297                                                &signature, &type_offset);
3298
3299           /* Skip dummy type units.  */
3300           if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3301             {
3302               info_ptr = info_ptr + header.initial_length_size + header.length;
3303               continue;
3304             }
3305
3306           type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3307           memset (type_sig, 0, sizeof (*type_sig));
3308           type_sig->signature = signature;
3309           type_sig->type_offset = type_offset;
3310           type_sig->per_cu.objfile = objfile;
3311           type_sig->per_cu.debug_types_section = section;
3312           type_sig->per_cu.offset = offset;
3313
3314           slot = htab_find_slot (types_htab, type_sig, INSERT);
3315           gdb_assert (slot != NULL);
3316           if (*slot != NULL)
3317             {
3318               const struct signatured_type *dup_sig = *slot;
3319
3320               complaint (&symfile_complaints,
3321                          _("debug type entry at offset 0x%x is duplicate to the "
3322                            "entry at offset 0x%x, signature 0x%s"),
3323                          offset, dup_sig->per_cu.offset,
3324                          phex (signature, sizeof (signature)));
3325               gdb_assert (signature == dup_sig->signature);
3326             }
3327           *slot = type_sig;
3328
3329           if (dwarf2_die_debug)
3330             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
3331                                 offset, phex (signature, sizeof (signature)));
3332
3333           info_ptr = info_ptr + header.initial_length_size + header.length;
3334         }
3335     }
3336
3337   dwarf2_per_objfile->signatured_types = types_htab;
3338
3339   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3340   dwarf2_per_objfile->all_type_units
3341     = obstack_alloc (&objfile->objfile_obstack,
3342                      dwarf2_per_objfile->n_type_units
3343                      * sizeof (struct dwarf2_per_cu_data *));
3344   iter = &dwarf2_per_objfile->all_type_units[0];
3345   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3346   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3347               == dwarf2_per_objfile->n_type_units);
3348
3349   return 1;
3350 }
3351
3352 /* Lookup a signature based type.
3353    Returns NULL if SIG is not present in the table.  */
3354
3355 static struct signatured_type *
3356 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3357 {
3358   struct signatured_type find_entry, *entry;
3359
3360   if (dwarf2_per_objfile->signatured_types == NULL)
3361     {
3362       complaint (&symfile_complaints,
3363                  _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3364       return 0;
3365     }
3366
3367   find_entry.signature = sig;
3368   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3369   return entry;
3370 }
3371
3372 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
3373
3374 static void
3375 init_cu_die_reader (struct die_reader_specs *reader,
3376                     struct dwarf2_cu *cu)
3377 {
3378   reader->abfd = cu->objfile->obfd;
3379   reader->cu = cu;
3380   if (cu->per_cu->debug_types_section)
3381     {
3382       gdb_assert (cu->per_cu->debug_types_section->readin);
3383       reader->buffer = cu->per_cu->debug_types_section->buffer;
3384     }
3385   else
3386     {
3387       gdb_assert (dwarf2_per_objfile->info.readin);
3388       reader->buffer = dwarf2_per_objfile->info.buffer;
3389     }
3390 }
3391
3392 /* Find the base address of the compilation unit for range lists and
3393    location lists.  It will normally be specified by DW_AT_low_pc.
3394    In DWARF-3 draft 4, the base address could be overridden by
3395    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
3396    compilation units with discontinuous ranges.  */
3397
3398 static void
3399 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3400 {
3401   struct attribute *attr;
3402
3403   cu->base_known = 0;
3404   cu->base_address = 0;
3405
3406   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3407   if (attr)
3408     {
3409       cu->base_address = DW_ADDR (attr);
3410       cu->base_known = 1;
3411     }
3412   else
3413     {
3414       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3415       if (attr)
3416         {
3417           cu->base_address = DW_ADDR (attr);
3418           cu->base_known = 1;
3419         }
3420     }
3421 }
3422
3423 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3424    to combine the common parts.
3425    Process compilation unit THIS_CU for a psymtab.
3426    SECTION is the section the CU/TU comes from,
3427    either .debug_info or .debug_types.  */
3428
3429 static void
3430 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3431                            struct dwarf2_section_info *section,
3432                            int is_debug_types_section)
3433 {
3434   struct objfile *objfile = this_cu->objfile;
3435   bfd *abfd = objfile->obfd;
3436   gdb_byte *buffer = section->buffer;
3437   gdb_byte *info_ptr = buffer + this_cu->offset;
3438   unsigned int buffer_size = section->size;
3439   gdb_byte *beg_of_comp_unit = info_ptr;
3440   struct die_info *comp_unit_die;
3441   struct partial_symtab *pst;
3442   CORE_ADDR baseaddr;
3443   struct cleanup *back_to_inner;
3444   struct dwarf2_cu cu;
3445   int has_children, has_pc_info;
3446   struct attribute *attr;
3447   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3448   struct die_reader_specs reader_specs;
3449   const char *filename;
3450
3451   /* If this compilation unit was already read in, free the
3452      cached copy in order to read it in again.  This is
3453      necessary because we skipped some symbols when we first
3454      read in the compilation unit (see load_partial_dies).
3455      This problem could be avoided, but the benefit is
3456      unclear.  */
3457   if (this_cu->cu != NULL)
3458     free_one_cached_comp_unit (this_cu->cu);
3459
3460   /* Note that this is a pointer to our stack frame, being
3461      added to a global data structure.  It will be cleaned up
3462      in free_stack_comp_unit when we finish with this
3463      compilation unit.  */
3464   init_one_comp_unit (&cu, this_cu);
3465   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3466
3467   info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3468                                             is_debug_types_section);
3469
3470   /* Skip dummy compilation units.  */
3471   if (info_ptr >= buffer + buffer_size
3472       || peek_abbrev_code (abfd, info_ptr) == 0)
3473     {
3474       do_cleanups (back_to_inner);
3475       return;
3476     }
3477
3478   cu.list_in_scope = &file_symbols;
3479
3480   /* Read the abbrevs for this compilation unit into a table.  */
3481   dwarf2_read_abbrevs (&cu);
3482   make_cleanup (dwarf2_free_abbrev_table, &cu);
3483
3484   /* Read the compilation unit die.  */
3485   init_cu_die_reader (&reader_specs, &cu);
3486   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3487                             &has_children);
3488
3489   if (is_debug_types_section)
3490     {
3491       /* LENGTH has not been set yet for type units.  */
3492       gdb_assert (this_cu->offset == cu.header.offset);
3493       this_cu->length = cu.header.length + cu.header.initial_length_size;
3494     }
3495   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3496     {
3497       do_cleanups (back_to_inner);
3498       return;
3499     }
3500
3501   prepare_one_comp_unit (&cu, comp_unit_die);
3502
3503   /* Allocate a new partial symbol table structure.  */
3504   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3505   if (attr == NULL || !DW_STRING (attr))
3506     filename = "";
3507   else
3508     filename = DW_STRING (attr);
3509   pst = start_psymtab_common (objfile, objfile->section_offsets,
3510                               filename,
3511                               /* TEXTLOW and TEXTHIGH are set below.  */
3512                               0,
3513                               objfile->global_psymbols.next,
3514                               objfile->static_psymbols.next);
3515   pst->psymtabs_addrmap_supported = 1;
3516
3517   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3518   if (attr != NULL)
3519     pst->dirname = DW_STRING (attr);
3520
3521   pst->read_symtab_private = this_cu;
3522
3523   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3524
3525   /* Store the function that reads in the rest of the symbol table.  */
3526   pst->read_symtab = dwarf2_psymtab_to_symtab;
3527
3528   this_cu->v.psymtab = pst;
3529
3530   dwarf2_find_base_address (comp_unit_die, &cu);
3531
3532   /* Possibly set the default values of LOWPC and HIGHPC from
3533      `DW_AT_ranges'.  */
3534   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3535                                       &best_highpc, &cu, pst);
3536   if (has_pc_info == 1 && best_lowpc < best_highpc)
3537     /* Store the contiguous range if it is not empty; it can be empty for
3538        CUs with no code.  */
3539     addrmap_set_empty (objfile->psymtabs_addrmap,
3540                        best_lowpc + baseaddr,
3541                        best_highpc + baseaddr - 1, pst);
3542
3543   /* Check if comp unit has_children.
3544      If so, read the rest of the partial symbols from this comp unit.
3545      If not, there's no more debug_info for this comp unit.  */
3546   if (has_children)
3547     {
3548       struct partial_die_info *first_die;
3549       CORE_ADDR lowpc, highpc;
3550
3551       lowpc = ((CORE_ADDR) -1);
3552       highpc = ((CORE_ADDR) 0);
3553
3554       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3555
3556       scan_partial_symbols (first_die, &lowpc, &highpc,
3557                             ! has_pc_info, &cu);
3558
3559       /* If we didn't find a lowpc, set it to highpc to avoid
3560          complaints from `maint check'.  */
3561       if (lowpc == ((CORE_ADDR) -1))
3562         lowpc = highpc;
3563
3564       /* If the compilation unit didn't have an explicit address range,
3565          then use the information extracted from its child dies.  */
3566       if (! has_pc_info)
3567         {
3568           best_lowpc = lowpc;
3569           best_highpc = highpc;
3570         }
3571     }
3572   pst->textlow = best_lowpc + baseaddr;
3573   pst->texthigh = best_highpc + baseaddr;
3574
3575   pst->n_global_syms = objfile->global_psymbols.next -
3576     (objfile->global_psymbols.list + pst->globals_offset);
3577   pst->n_static_syms = objfile->static_psymbols.next -
3578     (objfile->static_psymbols.list + pst->statics_offset);
3579   sort_pst_symbols (pst);
3580
3581   if (is_debug_types_section)
3582     {
3583       /* It's not clear we want to do anything with stmt lists here.
3584          Waiting to see what gcc ultimately does.  */
3585     }
3586   else
3587     {
3588       /* Get the list of files included in the current compilation unit,
3589          and build a psymtab for each of them.  */
3590       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3591     }
3592
3593   do_cleanups (back_to_inner);
3594 }
3595
3596 /* Traversal function for htab_traverse_noresize.
3597    Process one .debug_types comp-unit.  */
3598
3599 static int
3600 process_type_comp_unit (void **slot, void *info)
3601 {
3602   struct signatured_type *entry = (struct signatured_type *) *slot;
3603   struct dwarf2_per_cu_data *this_cu;
3604
3605   gdb_assert (info == NULL);
3606   this_cu = &entry->per_cu;
3607
3608   gdb_assert (this_cu->debug_types_section->readin);
3609   process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3610
3611   return 1;
3612 }
3613
3614 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3615    Build partial symbol tables for the .debug_types comp-units.  */
3616
3617 static void
3618 build_type_psymtabs (struct objfile *objfile)
3619 {
3620   if (! create_debug_types_hash_table (objfile))
3621     return;
3622
3623   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3624                           process_type_comp_unit, NULL);
3625 }
3626
3627 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3628
3629 static void
3630 psymtabs_addrmap_cleanup (void *o)
3631 {
3632   struct objfile *objfile = o;
3633
3634   objfile->psymtabs_addrmap = NULL;
3635 }
3636
3637 /* Build the partial symbol table by doing a quick pass through the
3638    .debug_info and .debug_abbrev sections.  */
3639
3640 static void
3641 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3642 {
3643   struct cleanup *back_to, *addrmap_cleanup;
3644   struct obstack temp_obstack;
3645   int i;
3646
3647   dwarf2_per_objfile->reading_partial_symbols = 1;
3648
3649   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3650
3651   /* Any cached compilation units will be linked by the per-objfile
3652      read_in_chain.  Make sure to free them when we're done.  */
3653   back_to = make_cleanup (free_cached_comp_units, NULL);
3654
3655   build_type_psymtabs (objfile);
3656
3657   create_all_comp_units (objfile);
3658
3659   /* Create a temporary address map on a temporary obstack.  We later
3660      copy this to the final obstack.  */
3661   obstack_init (&temp_obstack);
3662   make_cleanup_obstack_free (&temp_obstack);
3663   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3664   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3665
3666   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3667     {
3668       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3669
3670       process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3671     }
3672
3673   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3674                                                     &objfile->objfile_obstack);
3675   discard_cleanups (addrmap_cleanup);
3676
3677   do_cleanups (back_to);
3678 }
3679
3680 /* Load the partial DIEs for a secondary CU into memory.  */
3681
3682 static void
3683 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3684 {
3685   struct objfile *objfile = this_cu->objfile;
3686   bfd *abfd = objfile->obfd;
3687   gdb_byte *info_ptr;
3688   struct die_info *comp_unit_die;
3689   struct dwarf2_cu *cu;
3690   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3691   int has_children;
3692   struct die_reader_specs reader_specs;
3693   int read_cu = 0;
3694   struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3695
3696   gdb_assert (! this_cu->debug_types_section);
3697
3698   gdb_assert (section->readin);
3699   info_ptr = section->buffer + this_cu->offset;
3700
3701   if (this_cu->cu == NULL)
3702     {
3703       cu = xmalloc (sizeof (*cu));
3704       init_one_comp_unit (cu, this_cu);
3705
3706       read_cu = 1;
3707
3708       /* If an error occurs while loading, release our storage.  */
3709       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3710
3711       info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3712                                                 0);
3713
3714       /* Skip dummy compilation units.  */
3715       if (info_ptr >= (section->buffer + section->size)
3716           || peek_abbrev_code (abfd, info_ptr) == 0)
3717         {
3718           do_cleanups (free_cu_cleanup);
3719           return;
3720         }
3721     }
3722   else
3723     {
3724       cu = this_cu->cu;
3725       info_ptr += cu->header.first_die_offset;
3726     }
3727
3728   /* Read the abbrevs for this compilation unit into a table.  */
3729   gdb_assert (cu->dwarf2_abbrevs == NULL);
3730   dwarf2_read_abbrevs (cu);
3731   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3732
3733   /* Read the compilation unit die.  */
3734   init_cu_die_reader (&reader_specs, cu);
3735   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3736                             &has_children);
3737
3738   prepare_one_comp_unit (cu, comp_unit_die);
3739
3740   /* Check if comp unit has_children.
3741      If so, read the rest of the partial symbols from this comp unit.
3742      If not, there's no more debug_info for this comp unit.  */
3743   if (has_children)
3744     load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3745
3746   do_cleanups (free_abbrevs_cleanup);
3747
3748   if (read_cu)
3749     {
3750       /* We've successfully allocated this compilation unit.  Let our
3751          caller clean it up when finished with it.  */
3752       discard_cleanups (free_cu_cleanup);
3753
3754       /* Link this CU into read_in_chain.  */
3755       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3756       dwarf2_per_objfile->read_in_chain = this_cu;
3757     }
3758 }
3759
3760 /* Create a list of all compilation units in OBJFILE.
3761    This is only done for -readnow and building partial symtabs.  */
3762
3763 static void
3764 create_all_comp_units (struct objfile *objfile)
3765 {
3766   int n_allocated;
3767   int n_comp_units;
3768   struct dwarf2_per_cu_data **all_comp_units;
3769   gdb_byte *info_ptr;
3770
3771   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3772   info_ptr = dwarf2_per_objfile->info.buffer;
3773
3774   n_comp_units = 0;
3775   n_allocated = 10;
3776   all_comp_units = xmalloc (n_allocated
3777                             * sizeof (struct dwarf2_per_cu_data *));
3778
3779   while (info_ptr < dwarf2_per_objfile->info.buffer
3780          + dwarf2_per_objfile->info.size)
3781     {
3782       unsigned int length, initial_length_size;
3783       struct dwarf2_per_cu_data *this_cu;
3784       unsigned int offset;
3785
3786       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3787
3788       /* Read just enough information to find out where the next
3789          compilation unit is.  */
3790       length = read_initial_length (objfile->obfd, info_ptr,
3791                                     &initial_length_size);
3792
3793       /* Save the compilation unit for later lookup.  */
3794       this_cu = obstack_alloc (&objfile->objfile_obstack,
3795                                sizeof (struct dwarf2_per_cu_data));
3796       memset (this_cu, 0, sizeof (*this_cu));
3797       this_cu->offset = offset;
3798       this_cu->length = length + initial_length_size;
3799       this_cu->objfile = objfile;
3800
3801       if (n_comp_units == n_allocated)
3802         {
3803           n_allocated *= 2;
3804           all_comp_units = xrealloc (all_comp_units,
3805                                      n_allocated
3806                                      * sizeof (struct dwarf2_per_cu_data *));
3807         }
3808       all_comp_units[n_comp_units++] = this_cu;
3809
3810       info_ptr = info_ptr + this_cu->length;
3811     }
3812
3813   dwarf2_per_objfile->all_comp_units
3814     = obstack_alloc (&objfile->objfile_obstack,
3815                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3816   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3817           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3818   xfree (all_comp_units);
3819   dwarf2_per_objfile->n_comp_units = n_comp_units;
3820 }
3821
3822 /* Process all loaded DIEs for compilation unit CU, starting at
3823    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3824    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3825    DW_AT_ranges).  If NEED_PC is set, then this function will set
3826    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3827    and record the covered ranges in the addrmap.  */
3828
3829 static void
3830 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3831                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3832 {
3833   struct partial_die_info *pdi;
3834
3835   /* Now, march along the PDI's, descending into ones which have
3836      interesting children but skipping the children of the other ones,
3837      until we reach the end of the compilation unit.  */
3838
3839   pdi = first_die;
3840
3841   while (pdi != NULL)
3842     {
3843       fixup_partial_die (pdi, cu);
3844
3845       /* Anonymous namespaces or modules have no name but have interesting
3846          children, so we need to look at them.  Ditto for anonymous
3847          enums.  */
3848
3849       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3850           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3851         {
3852           switch (pdi->tag)
3853             {
3854             case DW_TAG_subprogram:
3855               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3856               break;
3857             case DW_TAG_constant:
3858             case DW_TAG_variable:
3859             case DW_TAG_typedef:
3860             case DW_TAG_union_type:
3861               if (!pdi->is_declaration)
3862                 {
3863                   add_partial_symbol (pdi, cu);
3864                 }
3865               break;
3866             case DW_TAG_class_type:
3867             case DW_TAG_interface_type:
3868             case DW_TAG_structure_type:
3869               if (!pdi->is_declaration)
3870                 {
3871                   add_partial_symbol (pdi, cu);
3872                 }
3873               break;
3874             case DW_TAG_enumeration_type:
3875               if (!pdi->is_declaration)
3876                 add_partial_enumeration (pdi, cu);
3877               break;
3878             case DW_TAG_base_type:
3879             case DW_TAG_subrange_type:
3880               /* File scope base type definitions are added to the partial
3881                  symbol table.  */
3882               add_partial_symbol (pdi, cu);
3883               break;
3884             case DW_TAG_namespace:
3885               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3886               break;
3887             case DW_TAG_module:
3888               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3889               break;
3890             default:
3891               break;
3892             }
3893         }
3894
3895       /* If the die has a sibling, skip to the sibling.  */
3896
3897       pdi = pdi->die_sibling;
3898     }
3899 }
3900
3901 /* Functions used to compute the fully scoped name of a partial DIE.
3902
3903    Normally, this is simple.  For C++, the parent DIE's fully scoped
3904    name is concatenated with "::" and the partial DIE's name.  For
3905    Java, the same thing occurs except that "." is used instead of "::".
3906    Enumerators are an exception; they use the scope of their parent
3907    enumeration type, i.e. the name of the enumeration type is not
3908    prepended to the enumerator.
3909
3910    There are two complexities.  One is DW_AT_specification; in this
3911    case "parent" means the parent of the target of the specification,
3912    instead of the direct parent of the DIE.  The other is compilers
3913    which do not emit DW_TAG_namespace; in this case we try to guess
3914    the fully qualified name of structure types from their members'
3915    linkage names.  This must be done using the DIE's children rather
3916    than the children of any DW_AT_specification target.  We only need
3917    to do this for structures at the top level, i.e. if the target of
3918    any DW_AT_specification (if any; otherwise the DIE itself) does not
3919    have a parent.  */
3920
3921 /* Compute the scope prefix associated with PDI's parent, in
3922    compilation unit CU.  The result will be allocated on CU's
3923    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3924    field.  NULL is returned if no prefix is necessary.  */
3925 static char *
3926 partial_die_parent_scope (struct partial_die_info *pdi,
3927                           struct dwarf2_cu *cu)
3928 {
3929   char *grandparent_scope;
3930   struct partial_die_info *parent, *real_pdi;
3931
3932   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3933      then this means the parent of the specification DIE.  */
3934
3935   real_pdi = pdi;
3936   while (real_pdi->has_specification)
3937     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3938
3939   parent = real_pdi->die_parent;
3940   if (parent == NULL)
3941     return NULL;
3942
3943   if (parent->scope_set)
3944     return parent->scope;
3945
3946   fixup_partial_die (parent, cu);
3947
3948   grandparent_scope = partial_die_parent_scope (parent, cu);
3949
3950   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3951      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3952      Work around this problem here.  */
3953   if (cu->language == language_cplus
3954       && parent->tag == DW_TAG_namespace
3955       && strcmp (parent->name, "::") == 0
3956       && grandparent_scope == NULL)
3957     {
3958       parent->scope = NULL;
3959       parent->scope_set = 1;
3960       return NULL;
3961     }
3962
3963   if (pdi->tag == DW_TAG_enumerator)
3964     /* Enumerators should not get the name of the enumeration as a prefix.  */
3965     parent->scope = grandparent_scope;
3966   else if (parent->tag == DW_TAG_namespace
3967       || parent->tag == DW_TAG_module
3968       || parent->tag == DW_TAG_structure_type
3969       || parent->tag == DW_TAG_class_type
3970       || parent->tag == DW_TAG_interface_type
3971       || parent->tag == DW_TAG_union_type
3972       || parent->tag == DW_TAG_enumeration_type)
3973     {
3974       if (grandparent_scope == NULL)
3975         parent->scope = parent->name;
3976       else
3977         parent->scope = typename_concat (&cu->comp_unit_obstack,
3978                                          grandparent_scope,
3979                                          parent->name, 0, cu);
3980     }
3981   else
3982     {
3983       /* FIXME drow/2004-04-01: What should we be doing with
3984          function-local names?  For partial symbols, we should probably be
3985          ignoring them.  */
3986       complaint (&symfile_complaints,
3987                  _("unhandled containing DIE tag %d for DIE at %d"),
3988                  parent->tag, pdi->offset);
3989       parent->scope = grandparent_scope;
3990     }
3991
3992   parent->scope_set = 1;
3993   return parent->scope;
3994 }
3995
3996 /* Return the fully scoped name associated with PDI, from compilation unit
3997    CU.  The result will be allocated with malloc.  */
3998 static char *
3999 partial_die_full_name (struct partial_die_info *pdi,
4000                        struct dwarf2_cu *cu)
4001 {
4002   char *parent_scope;
4003
4004   /* If this is a template instantiation, we can not work out the
4005      template arguments from partial DIEs.  So, unfortunately, we have
4006      to go through the full DIEs.  At least any work we do building
4007      types here will be reused if full symbols are loaded later.  */
4008   if (pdi->has_template_arguments)
4009     {
4010       fixup_partial_die (pdi, cu);
4011
4012       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4013         {
4014           struct die_info *die;
4015           struct attribute attr;
4016           struct dwarf2_cu *ref_cu = cu;
4017
4018           attr.name = 0;
4019           attr.form = DW_FORM_ref_addr;
4020           attr.u.addr = pdi->offset;
4021           die = follow_die_ref (NULL, &attr, &ref_cu);
4022
4023           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4024         }
4025     }
4026
4027   parent_scope = partial_die_parent_scope (pdi, cu);
4028   if (parent_scope == NULL)
4029     return NULL;
4030   else
4031     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4032 }
4033
4034 static void
4035 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4036 {
4037   struct objfile *objfile = cu->objfile;
4038   CORE_ADDR addr = 0;
4039   char *actual_name = NULL;
4040   CORE_ADDR baseaddr;
4041   int built_actual_name = 0;
4042
4043   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4044
4045   actual_name = partial_die_full_name (pdi, cu);
4046   if (actual_name)
4047     built_actual_name = 1;
4048
4049   if (actual_name == NULL)
4050     actual_name = pdi->name;
4051
4052   switch (pdi->tag)
4053     {
4054     case DW_TAG_subprogram:
4055       if (pdi->is_external || cu->language == language_ada)
4056         {
4057           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4058              of the global scope.  But in Ada, we want to be able to access
4059              nested procedures globally.  So all Ada subprograms are stored
4060              in the global scope.  */
4061           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4062              mst_text, objfile); */
4063           add_psymbol_to_list (actual_name, strlen (actual_name),
4064                                built_actual_name,
4065                                VAR_DOMAIN, LOC_BLOCK,
4066                                &objfile->global_psymbols,
4067                                0, pdi->lowpc + baseaddr,
4068                                cu->language, objfile);
4069         }
4070       else
4071         {
4072           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4073              mst_file_text, objfile); */
4074           add_psymbol_to_list (actual_name, strlen (actual_name),
4075                                built_actual_name,
4076                                VAR_DOMAIN, LOC_BLOCK,
4077                                &objfile->static_psymbols,
4078                                0, pdi->lowpc + baseaddr,
4079                                cu->language, objfile);
4080         }
4081       break;
4082     case DW_TAG_constant:
4083       {
4084         struct psymbol_allocation_list *list;
4085
4086         if (pdi->is_external)
4087           list = &objfile->global_psymbols;
4088         else
4089           list = &objfile->static_psymbols;
4090         add_psymbol_to_list (actual_name, strlen (actual_name),
4091                              built_actual_name, VAR_DOMAIN, LOC_STATIC,
4092                              list, 0, 0, cu->language, objfile);
4093       }
4094       break;
4095     case DW_TAG_variable:
4096       if (pdi->locdesc)
4097         addr = decode_locdesc (pdi->locdesc, cu);
4098
4099       if (pdi->locdesc
4100           && addr == 0
4101           && !dwarf2_per_objfile->has_section_at_zero)
4102         {
4103           /* A global or static variable may also have been stripped
4104              out by the linker if unused, in which case its address
4105              will be nullified; do not add such variables into partial
4106              symbol table then.  */
4107         }
4108       else if (pdi->is_external)
4109         {
4110           /* Global Variable.
4111              Don't enter into the minimal symbol tables as there is
4112              a minimal symbol table entry from the ELF symbols already.
4113              Enter into partial symbol table if it has a location
4114              descriptor or a type.
4115              If the location descriptor is missing, new_symbol will create
4116              a LOC_UNRESOLVED symbol, the address of the variable will then
4117              be determined from the minimal symbol table whenever the variable
4118              is referenced.
4119              The address for the partial symbol table entry is not
4120              used by GDB, but it comes in handy for debugging partial symbol
4121              table building.  */
4122
4123           if (pdi->locdesc || pdi->has_type)
4124             add_psymbol_to_list (actual_name, strlen (actual_name),
4125                                  built_actual_name,
4126                                  VAR_DOMAIN, LOC_STATIC,
4127                                  &objfile->global_psymbols,
4128                                  0, addr + baseaddr,
4129                                  cu->language, objfile);
4130         }
4131       else
4132         {
4133           /* Static Variable.  Skip symbols without location descriptors.  */
4134           if (pdi->locdesc == NULL)
4135             {
4136               if (built_actual_name)
4137                 xfree (actual_name);
4138               return;
4139             }
4140           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4141              mst_file_data, objfile); */
4142           add_psymbol_to_list (actual_name, strlen (actual_name),
4143                                built_actual_name,
4144                                VAR_DOMAIN, LOC_STATIC,
4145                                &objfile->static_psymbols,
4146                                0, addr + baseaddr,
4147                                cu->language, objfile);
4148         }
4149       break;
4150     case DW_TAG_typedef:
4151     case DW_TAG_base_type:
4152     case DW_TAG_subrange_type:
4153       add_psymbol_to_list (actual_name, strlen (actual_name),
4154                            built_actual_name,
4155                            VAR_DOMAIN, LOC_TYPEDEF,
4156                            &objfile->static_psymbols,
4157                            0, (CORE_ADDR) 0, cu->language, objfile);
4158       break;
4159     case DW_TAG_namespace:
4160       add_psymbol_to_list (actual_name, strlen (actual_name),
4161                            built_actual_name,
4162                            VAR_DOMAIN, LOC_TYPEDEF,
4163                            &objfile->global_psymbols,
4164                            0, (CORE_ADDR) 0, cu->language, objfile);
4165       break;
4166     case DW_TAG_class_type:
4167     case DW_TAG_interface_type:
4168     case DW_TAG_structure_type:
4169     case DW_TAG_union_type:
4170     case DW_TAG_enumeration_type:
4171       /* Skip external references.  The DWARF standard says in the section
4172          about "Structure, Union, and Class Type Entries": "An incomplete
4173          structure, union or class type is represented by a structure,
4174          union or class entry that does not have a byte size attribute
4175          and that has a DW_AT_declaration attribute."  */
4176       if (!pdi->has_byte_size && pdi->is_declaration)
4177         {
4178           if (built_actual_name)
4179             xfree (actual_name);
4180           return;
4181         }
4182
4183       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4184          static vs. global.  */
4185       add_psymbol_to_list (actual_name, strlen (actual_name),
4186                            built_actual_name,
4187                            STRUCT_DOMAIN, LOC_TYPEDEF,
4188                            (cu->language == language_cplus
4189                             || cu->language == language_java)
4190                            ? &objfile->global_psymbols
4191                            : &objfile->static_psymbols,
4192                            0, (CORE_ADDR) 0, cu->language, objfile);
4193
4194       break;
4195     case DW_TAG_enumerator:
4196       add_psymbol_to_list (actual_name, strlen (actual_name),
4197                            built_actual_name,
4198                            VAR_DOMAIN, LOC_CONST,
4199                            (cu->language == language_cplus
4200                             || cu->language == language_java)
4201                            ? &objfile->global_psymbols
4202                            : &objfile->static_psymbols,
4203                            0, (CORE_ADDR) 0, cu->language, objfile);
4204       break;
4205     default:
4206       break;
4207     }
4208
4209   if (built_actual_name)
4210     xfree (actual_name);
4211 }
4212
4213 /* Read a partial die corresponding to a namespace; also, add a symbol
4214    corresponding to that namespace to the symbol table.  NAMESPACE is
4215    the name of the enclosing namespace.  */
4216
4217 static void
4218 add_partial_namespace (struct partial_die_info *pdi,
4219                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
4220                        int need_pc, struct dwarf2_cu *cu)
4221 {
4222   /* Add a symbol for the namespace.  */
4223
4224   add_partial_symbol (pdi, cu);
4225
4226   /* Now scan partial symbols in that namespace.  */
4227
4228   if (pdi->has_children)
4229     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4230 }
4231
4232 /* Read a partial die corresponding to a Fortran module.  */
4233
4234 static void
4235 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4236                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4237 {
4238   /* Now scan partial symbols in that module.  */
4239
4240   if (pdi->has_children)
4241     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4242 }
4243
4244 /* Read a partial die corresponding to a subprogram and create a partial
4245    symbol for that subprogram.  When the CU language allows it, this
4246    routine also defines a partial symbol for each nested subprogram
4247    that this subprogram contains.
4248
4249    DIE my also be a lexical block, in which case we simply search
4250    recursively for suprograms defined inside that lexical block.
4251    Again, this is only performed when the CU language allows this
4252    type of definitions.  */
4253
4254 static void
4255 add_partial_subprogram (struct partial_die_info *pdi,
4256                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
4257                         int need_pc, struct dwarf2_cu *cu)
4258 {
4259   if (pdi->tag == DW_TAG_subprogram)
4260     {
4261       if (pdi->has_pc_info)
4262         {
4263           if (pdi->lowpc < *lowpc)
4264             *lowpc = pdi->lowpc;
4265           if (pdi->highpc > *highpc)
4266             *highpc = pdi->highpc;
4267           if (need_pc)
4268             {
4269               CORE_ADDR baseaddr;
4270               struct objfile *objfile = cu->objfile;
4271
4272               baseaddr = ANOFFSET (objfile->section_offsets,
4273                                    SECT_OFF_TEXT (objfile));
4274               addrmap_set_empty (objfile->psymtabs_addrmap,
4275                                  pdi->lowpc + baseaddr,
4276                                  pdi->highpc - 1 + baseaddr,
4277                                  cu->per_cu->v.psymtab);
4278             }
4279         }
4280
4281       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4282         {
4283           if (!pdi->is_declaration)
4284             /* Ignore subprogram DIEs that do not have a name, they are
4285                illegal.  Do not emit a complaint at this point, we will
4286                do so when we convert this psymtab into a symtab.  */
4287             if (pdi->name)
4288               add_partial_symbol (pdi, cu);
4289         }
4290     }
4291
4292   if (! pdi->has_children)
4293     return;
4294
4295   if (cu->language == language_ada)
4296     {
4297       pdi = pdi->die_child;
4298       while (pdi != NULL)
4299         {
4300           fixup_partial_die (pdi, cu);
4301           if (pdi->tag == DW_TAG_subprogram
4302               || pdi->tag == DW_TAG_lexical_block)
4303             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4304           pdi = pdi->die_sibling;
4305         }
4306     }
4307 }
4308
4309 /* Read a partial die corresponding to an enumeration type.  */
4310
4311 static void
4312 add_partial_enumeration (struct partial_die_info *enum_pdi,
4313                          struct dwarf2_cu *cu)
4314 {
4315   struct partial_die_info *pdi;
4316
4317   if (enum_pdi->name != NULL)
4318     add_partial_symbol (enum_pdi, cu);
4319
4320   pdi = enum_pdi->die_child;
4321   while (pdi)
4322     {
4323       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4324         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4325       else
4326         add_partial_symbol (pdi, cu);
4327       pdi = pdi->die_sibling;
4328     }
4329 }
4330
4331 /* Return the initial uleb128 in the die at INFO_PTR.  */
4332
4333 static unsigned int
4334 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4335 {
4336   unsigned int bytes_read;
4337
4338   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4339 }
4340
4341 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4342    Return the corresponding abbrev, or NULL if the number is zero (indicating
4343    an empty DIE).  In either case *BYTES_READ will be set to the length of
4344    the initial number.  */
4345
4346 static struct abbrev_info *
4347 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4348                  struct dwarf2_cu *cu)
4349 {
4350   bfd *abfd = cu->objfile->obfd;
4351   unsigned int abbrev_number;
4352   struct abbrev_info *abbrev;
4353
4354   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4355
4356   if (abbrev_number == 0)
4357     return NULL;
4358
4359   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4360   if (!abbrev)
4361     {
4362       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4363              abbrev_number, bfd_get_filename (abfd));
4364     }
4365
4366   return abbrev;
4367 }
4368
4369 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4370    Returns a pointer to the end of a series of DIEs, terminated by an empty
4371    DIE.  Any children of the skipped DIEs will also be skipped.  */
4372
4373 static gdb_byte *
4374 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4375 {
4376   struct abbrev_info *abbrev;
4377   unsigned int bytes_read;
4378
4379   while (1)
4380     {
4381       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4382       if (abbrev == NULL)
4383         return info_ptr + bytes_read;
4384       else
4385         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4386     }
4387 }
4388
4389 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4390    INFO_PTR should point just after the initial uleb128 of a DIE, and the
4391    abbrev corresponding to that skipped uleb128 should be passed in
4392    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
4393    children.  */
4394
4395 static gdb_byte *
4396 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4397               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4398 {
4399   unsigned int bytes_read;
4400   struct attribute attr;
4401   bfd *abfd = cu->objfile->obfd;
4402   unsigned int form, i;
4403
4404   for (i = 0; i < abbrev->num_attrs; i++)
4405     {
4406       /* The only abbrev we care about is DW_AT_sibling.  */
4407       if (abbrev->attrs[i].name == DW_AT_sibling)
4408         {
4409           read_attribute (&attr, &abbrev->attrs[i],
4410                           abfd, info_ptr, cu);
4411           if (attr.form == DW_FORM_ref_addr)
4412             complaint (&symfile_complaints,
4413                        _("ignoring absolute DW_AT_sibling"));
4414           else
4415             return buffer + dwarf2_get_ref_die_offset (&attr);
4416         }
4417
4418       /* If it isn't DW_AT_sibling, skip this attribute.  */
4419       form = abbrev->attrs[i].form;
4420     skip_attribute:
4421       switch (form)
4422         {
4423         case DW_FORM_ref_addr:
4424           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4425              and later it is offset sized.  */
4426           if (cu->header.version == 2)
4427             info_ptr += cu->header.addr_size;
4428           else
4429             info_ptr += cu->header.offset_size;
4430           break;
4431         case DW_FORM_addr:
4432           info_ptr += cu->header.addr_size;
4433           break;
4434         case DW_FORM_data1:
4435         case DW_FORM_ref1:
4436         case DW_FORM_flag:
4437           info_ptr += 1;
4438           break;
4439         case DW_FORM_flag_present:
4440           break;
4441         case DW_FORM_data2:
4442         case DW_FORM_ref2:
4443           info_ptr += 2;
4444           break;
4445         case DW_FORM_data4:
4446         case DW_FORM_ref4:
4447           info_ptr += 4;
4448           break;
4449         case DW_FORM_data8:
4450         case DW_FORM_ref8:
4451         case DW_FORM_ref_sig8:
4452           info_ptr += 8;
4453           break;
4454         case DW_FORM_string:
4455           read_direct_string (abfd, info_ptr, &bytes_read);
4456           info_ptr += bytes_read;
4457           break;
4458         case DW_FORM_sec_offset:
4459         case DW_FORM_strp:
4460           info_ptr += cu->header.offset_size;
4461           break;
4462         case DW_FORM_exprloc:
4463         case DW_FORM_block:
4464           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4465           info_ptr += bytes_read;
4466           break;
4467         case DW_FORM_block1:
4468           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4469           break;
4470         case DW_FORM_block2:
4471           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4472           break;
4473         case DW_FORM_block4:
4474           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4475           break;
4476         case DW_FORM_sdata:
4477         case DW_FORM_udata:
4478         case DW_FORM_ref_udata:
4479           info_ptr = skip_leb128 (abfd, info_ptr);
4480           break;
4481         case DW_FORM_indirect:
4482           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4483           info_ptr += bytes_read;
4484           /* We need to continue parsing from here, so just go back to
4485              the top.  */
4486           goto skip_attribute;
4487
4488         default:
4489           error (_("Dwarf Error: Cannot handle %s "
4490                    "in DWARF reader [in module %s]"),
4491                  dwarf_form_name (form),
4492                  bfd_get_filename (abfd));
4493         }
4494     }
4495
4496   if (abbrev->has_children)
4497     return skip_children (buffer, info_ptr, cu);
4498   else
4499     return info_ptr;
4500 }
4501
4502 /* Locate ORIG_PDI's sibling.
4503    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4504    in BUFFER.  */
4505
4506 static gdb_byte *
4507 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4508                     gdb_byte *buffer, gdb_byte *info_ptr,
4509                     bfd *abfd, struct dwarf2_cu *cu)
4510 {
4511   /* Do we know the sibling already?  */
4512
4513   if (orig_pdi->sibling)
4514     return orig_pdi->sibling;
4515
4516   /* Are there any children to deal with?  */
4517
4518   if (!orig_pdi->has_children)
4519     return info_ptr;
4520
4521   /* Skip the children the long way.  */
4522
4523   return skip_children (buffer, info_ptr, cu);
4524 }
4525
4526 /* Expand this partial symbol table into a full symbol table.  */
4527
4528 static void
4529 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4530 {
4531   if (pst != NULL)
4532     {
4533       if (pst->readin)
4534         {
4535           warning (_("bug: psymtab for %s is already read in."),
4536                    pst->filename);
4537         }
4538       else
4539         {
4540           if (info_verbose)
4541             {
4542               printf_filtered (_("Reading in symbols for %s..."),
4543                                pst->filename);
4544               gdb_flush (gdb_stdout);
4545             }
4546
4547           /* Restore our global data.  */
4548           dwarf2_per_objfile = objfile_data (pst->objfile,
4549                                              dwarf2_objfile_data_key);
4550
4551           /* If this psymtab is constructed from a debug-only objfile, the
4552              has_section_at_zero flag will not necessarily be correct.  We
4553              can get the correct value for this flag by looking at the data
4554              associated with the (presumably stripped) associated objfile.  */
4555           if (pst->objfile->separate_debug_objfile_backlink)
4556             {
4557               struct dwarf2_per_objfile *dpo_backlink
4558                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4559                                 dwarf2_objfile_data_key);
4560
4561               dwarf2_per_objfile->has_section_at_zero
4562                 = dpo_backlink->has_section_at_zero;
4563             }
4564
4565           dwarf2_per_objfile->reading_partial_symbols = 0;
4566
4567           psymtab_to_symtab_1 (pst);
4568
4569           /* Finish up the debug error message.  */
4570           if (info_verbose)
4571             printf_filtered (_("done.\n"));
4572         }
4573     }
4574 }
4575 \f
4576 /* Reading in full CUs.  */
4577
4578 /* Add PER_CU to the queue.  */
4579
4580 static void
4581 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4582 {
4583   struct dwarf2_queue_item *item;
4584
4585   per_cu->queued = 1;
4586   item = xmalloc (sizeof (*item));
4587   item->per_cu = per_cu;
4588   item->next = NULL;
4589
4590   if (dwarf2_queue == NULL)
4591     dwarf2_queue = item;
4592   else
4593     dwarf2_queue_tail->next = item;
4594
4595   dwarf2_queue_tail = item;
4596 }
4597
4598 /* Process the queue.  */
4599
4600 static void
4601 process_queue (void)
4602 {
4603   struct dwarf2_queue_item *item, *next_item;
4604
4605   /* The queue starts out with one item, but following a DIE reference
4606      may load a new CU, adding it to the end of the queue.  */
4607   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4608     {
4609       if (dwarf2_per_objfile->using_index
4610           ? !item->per_cu->v.quick->symtab
4611           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4612         process_full_comp_unit (item->per_cu);
4613
4614       item->per_cu->queued = 0;
4615       next_item = item->next;
4616       xfree (item);
4617     }
4618
4619   dwarf2_queue_tail = NULL;
4620 }
4621
4622 /* Free all allocated queue entries.  This function only releases anything if
4623    an error was thrown; if the queue was processed then it would have been
4624    freed as we went along.  */
4625
4626 static void
4627 dwarf2_release_queue (void *dummy)
4628 {
4629   struct dwarf2_queue_item *item, *last;
4630
4631   item = dwarf2_queue;
4632   while (item)
4633     {
4634       /* Anything still marked queued is likely to be in an
4635          inconsistent state, so discard it.  */
4636       if (item->per_cu->queued)
4637         {
4638           if (item->per_cu->cu != NULL)
4639             free_one_cached_comp_unit (item->per_cu->cu);
4640           item->per_cu->queued = 0;
4641         }
4642
4643       last = item;
4644       item = item->next;
4645       xfree (last);
4646     }
4647
4648   dwarf2_queue = dwarf2_queue_tail = NULL;
4649 }
4650
4651 /* Read in full symbols for PST, and anything it depends on.  */
4652
4653 static void
4654 psymtab_to_symtab_1 (struct partial_symtab *pst)
4655 {
4656   struct dwarf2_per_cu_data *per_cu;
4657   struct cleanup *back_to;
4658   int i;
4659
4660   for (i = 0; i < pst->number_of_dependencies; i++)
4661     if (!pst->dependencies[i]->readin)
4662       {
4663         /* Inform about additional files that need to be read in.  */
4664         if (info_verbose)
4665           {
4666             /* FIXME: i18n: Need to make this a single string.  */
4667             fputs_filtered (" ", gdb_stdout);
4668             wrap_here ("");
4669             fputs_filtered ("and ", gdb_stdout);
4670             wrap_here ("");
4671             printf_filtered ("%s...", pst->dependencies[i]->filename);
4672             wrap_here ("");     /* Flush output.  */
4673             gdb_flush (gdb_stdout);
4674           }
4675         psymtab_to_symtab_1 (pst->dependencies[i]);
4676       }
4677
4678   per_cu = pst->read_symtab_private;
4679
4680   if (per_cu == NULL)
4681     {
4682       /* It's an include file, no symbols to read for it.
4683          Everything is in the parent symtab.  */
4684       pst->readin = 1;
4685       return;
4686     }
4687
4688   dw2_do_instantiate_symtab (per_cu);
4689 }
4690
4691 /* Load the DIEs associated with PER_CU into memory.  */
4692
4693 static void
4694 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4695 {
4696   struct objfile *objfile = per_cu->objfile;
4697   bfd *abfd = objfile->obfd;
4698   struct dwarf2_cu *cu;
4699   unsigned int offset;
4700   gdb_byte *info_ptr, *beg_of_comp_unit;
4701   struct cleanup *free_cu_cleanup = NULL;
4702   struct attribute *attr;
4703   int read_cu = 0;
4704
4705   gdb_assert (! per_cu->debug_types_section);
4706
4707   /* Set local variables from the partial symbol table info.  */
4708   offset = per_cu->offset;
4709
4710   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4711   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4712   beg_of_comp_unit = info_ptr;
4713
4714   if (per_cu->cu == NULL)
4715     {
4716       cu = xmalloc (sizeof (*cu));
4717       init_one_comp_unit (cu, per_cu);
4718
4719       read_cu = 1;
4720
4721       /* If an error occurs while loading, release our storage.  */
4722       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4723
4724       /* Read in the comp_unit header.  */
4725       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4726
4727       /* Skip dummy compilation units.  */
4728       if (info_ptr >= (dwarf2_per_objfile->info.buffer
4729                        + dwarf2_per_objfile->info.size)
4730           || peek_abbrev_code (abfd, info_ptr) == 0)
4731         {
4732           do_cleanups (free_cu_cleanup);
4733           return;
4734         }
4735
4736       /* Complete the cu_header.  */
4737       cu->header.offset = offset;
4738       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4739     }
4740   else
4741     {
4742       cu = per_cu->cu;
4743       info_ptr += cu->header.first_die_offset;
4744     }
4745
4746   cu->dies = read_comp_unit (info_ptr, cu);
4747
4748   /* We try not to read any attributes in this function, because not
4749      all CUs needed for references have been loaded yet, and symbol
4750      table processing isn't initialized.  But we have to set the CU language,
4751      or we won't be able to build types correctly.  */
4752   prepare_one_comp_unit (cu, cu->dies);
4753
4754   /* Similarly, if we do not read the producer, we can not apply
4755      producer-specific interpretation.  */
4756   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4757   if (attr)
4758     cu->producer = DW_STRING (attr);
4759
4760   if (read_cu)
4761     {
4762       /* We've successfully allocated this compilation unit.  Let our
4763          caller clean it up when finished with it.  */
4764       discard_cleanups (free_cu_cleanup);
4765
4766       /* Link this CU into read_in_chain.  */
4767       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4768       dwarf2_per_objfile->read_in_chain = per_cu;
4769     }
4770 }
4771
4772 /* Add a DIE to the delayed physname list.  */
4773
4774 static void
4775 add_to_method_list (struct type *type, int fnfield_index, int index,
4776                     const char *name, struct die_info *die,
4777                     struct dwarf2_cu *cu)
4778 {
4779   struct delayed_method_info mi;
4780   mi.type = type;
4781   mi.fnfield_index = fnfield_index;
4782   mi.index = index;
4783   mi.name = name;
4784   mi.die = die;
4785   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4786 }
4787
4788 /* A cleanup for freeing the delayed method list.  */
4789
4790 static void
4791 free_delayed_list (void *ptr)
4792 {
4793   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4794   if (cu->method_list != NULL)
4795     {
4796       VEC_free (delayed_method_info, cu->method_list);
4797       cu->method_list = NULL;
4798     }
4799 }
4800
4801 /* Compute the physnames of any methods on the CU's method list.
4802
4803    The computation of method physnames is delayed in order to avoid the
4804    (bad) condition that one of the method's formal parameters is of an as yet
4805    incomplete type.  */
4806
4807 static void
4808 compute_delayed_physnames (struct dwarf2_cu *cu)
4809 {
4810   int i;
4811   struct delayed_method_info *mi;
4812   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4813     {
4814       const char *physname;
4815       struct fn_fieldlist *fn_flp
4816         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4817       physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4818       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4819     }
4820 }
4821
4822 /* Generate full symbol information for PER_CU, whose DIEs have
4823    already been loaded into memory.  */
4824
4825 static void
4826 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4827 {
4828   struct dwarf2_cu *cu = per_cu->cu;
4829   struct objfile *objfile = per_cu->objfile;
4830   CORE_ADDR lowpc, highpc;
4831   struct symtab *symtab;
4832   struct cleanup *back_to, *delayed_list_cleanup;
4833   CORE_ADDR baseaddr;
4834
4835   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4836
4837   buildsym_init ();
4838   back_to = make_cleanup (really_free_pendings, NULL);
4839   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4840
4841   cu->list_in_scope = &file_symbols;
4842
4843   /* Do line number decoding in read_file_scope () */
4844   process_die (cu->dies, cu);
4845
4846   /* Now that we have processed all the DIEs in the CU, all the types 
4847      should be complete, and it should now be safe to compute all of the
4848      physnames.  */
4849   compute_delayed_physnames (cu);
4850   do_cleanups (delayed_list_cleanup);
4851
4852   /* Some compilers don't define a DW_AT_high_pc attribute for the
4853      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4854      it, by scanning the DIE's below the compilation unit.  */
4855   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4856
4857   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4858
4859   if (symtab != NULL)
4860     {
4861       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4862
4863       /* Set symtab language to language from DW_AT_language.  If the
4864          compilation is from a C file generated by language preprocessors, do
4865          not set the language if it was already deduced by start_subfile.  */
4866       if (!(cu->language == language_c && symtab->language != language_c))
4867         symtab->language = cu->language;
4868
4869       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
4870          produce DW_AT_location with location lists but it can be possibly
4871          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
4872          there were bugs in prologue debug info, fixed later in GCC-4.5
4873          by "unwind info for epilogues" patch (which is not directly related).
4874
4875          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4876          needed, it would be wrong due to missing DW_AT_producer there.
4877
4878          Still one can confuse GDB by using non-standard GCC compilation
4879          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4880          */ 
4881       if (cu->has_loclist && gcc_4_minor >= 5)
4882         symtab->locations_valid = 1;
4883
4884       if (gcc_4_minor >= 5)
4885         symtab->epilogue_unwind_valid = 1;
4886
4887       symtab->call_site_htab = cu->call_site_htab;
4888     }
4889
4890   if (dwarf2_per_objfile->using_index)
4891     per_cu->v.quick->symtab = symtab;
4892   else
4893     {
4894       struct partial_symtab *pst = per_cu->v.psymtab;
4895       pst->symtab = symtab;
4896       pst->readin = 1;
4897     }
4898
4899   do_cleanups (back_to);
4900 }
4901
4902 /* Process a die and its children.  */
4903
4904 static void
4905 process_die (struct die_info *die, struct dwarf2_cu *cu)
4906 {
4907   switch (die->tag)
4908     {
4909     case DW_TAG_padding:
4910       break;
4911     case DW_TAG_compile_unit:
4912       read_file_scope (die, cu);
4913       break;
4914     case DW_TAG_type_unit:
4915       read_type_unit_scope (die, cu);
4916       break;
4917     case DW_TAG_subprogram:
4918     case DW_TAG_inlined_subroutine:
4919       read_func_scope (die, cu);
4920       break;
4921     case DW_TAG_lexical_block:
4922     case DW_TAG_try_block:
4923     case DW_TAG_catch_block:
4924       read_lexical_block_scope (die, cu);
4925       break;
4926     case DW_TAG_GNU_call_site:
4927       read_call_site_scope (die, cu);
4928       break;
4929     case DW_TAG_class_type:
4930     case DW_TAG_interface_type:
4931     case DW_TAG_structure_type:
4932     case DW_TAG_union_type:
4933       process_structure_scope (die, cu);
4934       break;
4935     case DW_TAG_enumeration_type:
4936       process_enumeration_scope (die, cu);
4937       break;
4938
4939     /* These dies have a type, but processing them does not create
4940        a symbol or recurse to process the children.  Therefore we can
4941        read them on-demand through read_type_die.  */
4942     case DW_TAG_subroutine_type:
4943     case DW_TAG_set_type:
4944     case DW_TAG_array_type:
4945     case DW_TAG_pointer_type:
4946     case DW_TAG_ptr_to_member_type:
4947     case DW_TAG_reference_type:
4948     case DW_TAG_string_type:
4949       break;
4950
4951     case DW_TAG_base_type:
4952     case DW_TAG_subrange_type:
4953     case DW_TAG_typedef:
4954       /* Add a typedef symbol for the type definition, if it has a
4955          DW_AT_name.  */
4956       new_symbol (die, read_type_die (die, cu), cu);
4957       break;
4958     case DW_TAG_common_block:
4959       read_common_block (die, cu);
4960       break;
4961     case DW_TAG_common_inclusion:
4962       break;
4963     case DW_TAG_namespace:
4964       processing_has_namespace_info = 1;
4965       read_namespace (die, cu);
4966       break;
4967     case DW_TAG_module:
4968       processing_has_namespace_info = 1;
4969       read_module (die, cu);
4970       break;
4971     case DW_TAG_imported_declaration:
4972     case DW_TAG_imported_module:
4973       processing_has_namespace_info = 1;
4974       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4975                                  || cu->language != language_fortran))
4976         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4977                    dwarf_tag_name (die->tag));
4978       read_import_statement (die, cu);
4979       break;
4980     default:
4981       new_symbol (die, NULL, cu);
4982       break;
4983     }
4984 }
4985
4986 /* A helper function for dwarf2_compute_name which determines whether DIE
4987    needs to have the name of the scope prepended to the name listed in the
4988    die.  */
4989
4990 static int
4991 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4992 {
4993   struct attribute *attr;
4994
4995   switch (die->tag)
4996     {
4997     case DW_TAG_namespace:
4998     case DW_TAG_typedef:
4999     case DW_TAG_class_type:
5000     case DW_TAG_interface_type:
5001     case DW_TAG_structure_type:
5002     case DW_TAG_union_type:
5003     case DW_TAG_enumeration_type:
5004     case DW_TAG_enumerator:
5005     case DW_TAG_subprogram:
5006     case DW_TAG_member:
5007       return 1;
5008
5009     case DW_TAG_variable:
5010     case DW_TAG_constant:
5011       /* We only need to prefix "globally" visible variables.  These include
5012          any variable marked with DW_AT_external or any variable that
5013          lives in a namespace.  [Variables in anonymous namespaces
5014          require prefixing, but they are not DW_AT_external.]  */
5015
5016       if (dwarf2_attr (die, DW_AT_specification, cu))
5017         {
5018           struct dwarf2_cu *spec_cu = cu;
5019
5020           return die_needs_namespace (die_specification (die, &spec_cu),
5021                                       spec_cu);
5022         }
5023
5024       attr = dwarf2_attr (die, DW_AT_external, cu);
5025       if (attr == NULL && die->parent->tag != DW_TAG_namespace
5026           && die->parent->tag != DW_TAG_module)
5027         return 0;
5028       /* A variable in a lexical block of some kind does not need a
5029          namespace, even though in C++ such variables may be external
5030          and have a mangled name.  */
5031       if (die->parent->tag ==  DW_TAG_lexical_block
5032           || die->parent->tag ==  DW_TAG_try_block
5033           || die->parent->tag ==  DW_TAG_catch_block
5034           || die->parent->tag == DW_TAG_subprogram)
5035         return 0;
5036       return 1;
5037
5038     default:
5039       return 0;
5040     }
5041 }
5042
5043 /* Retrieve the last character from a mem_file.  */
5044
5045 static void
5046 do_ui_file_peek_last (void *object, const char *buffer, long length)
5047 {
5048   char *last_char_p = (char *) object;
5049
5050   if (length > 0)
5051     *last_char_p = buffer[length - 1];
5052 }
5053
5054 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
5055    compute the physname for the object, which include a method's
5056    formal parameters (C++/Java) and return type (Java).
5057
5058    For Ada, return the DIE's linkage name rather than the fully qualified
5059    name.  PHYSNAME is ignored..
5060
5061    The result is allocated on the objfile_obstack and canonicalized.  */
5062
5063 static const char *
5064 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5065                      int physname)
5066 {
5067   struct objfile *objfile = cu->objfile;
5068
5069   if (name == NULL)
5070     name = dwarf2_name (die, cu);
5071
5072   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5073      compute it by typename_concat inside GDB.  */
5074   if (cu->language == language_ada
5075       || (cu->language == language_fortran && physname))
5076     {
5077       /* For Ada unit, we prefer the linkage name over the name, as
5078          the former contains the exported name, which the user expects
5079          to be able to reference.  Ideally, we want the user to be able
5080          to reference this entity using either natural or linkage name,
5081          but we haven't started looking at this enhancement yet.  */
5082       struct attribute *attr;
5083
5084       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5085       if (attr == NULL)
5086         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5087       if (attr && DW_STRING (attr))
5088         return DW_STRING (attr);
5089     }
5090
5091   /* These are the only languages we know how to qualify names in.  */
5092   if (name != NULL
5093       && (cu->language == language_cplus || cu->language == language_java
5094           || cu->language == language_fortran))
5095     {
5096       if (die_needs_namespace (die, cu))
5097         {
5098           long length;
5099           const char *prefix;
5100           struct ui_file *buf;
5101
5102           prefix = determine_prefix (die, cu);
5103           buf = mem_fileopen ();
5104           if (*prefix != '\0')
5105             {
5106               char *prefixed_name = typename_concat (NULL, prefix, name,
5107                                                      physname, cu);
5108
5109               fputs_unfiltered (prefixed_name, buf);
5110               xfree (prefixed_name);
5111             }
5112           else
5113             fputs_unfiltered (name, buf);
5114
5115           /* Template parameters may be specified in the DIE's DW_AT_name, or
5116              as children with DW_TAG_template_type_param or
5117              DW_TAG_value_type_param.  If the latter, add them to the name
5118              here.  If the name already has template parameters, then
5119              skip this step; some versions of GCC emit both, and
5120              it is more efficient to use the pre-computed name.
5121
5122              Something to keep in mind about this process: it is very
5123              unlikely, or in some cases downright impossible, to produce
5124              something that will match the mangled name of a function.
5125              If the definition of the function has the same debug info,
5126              we should be able to match up with it anyway.  But fallbacks
5127              using the minimal symbol, for instance to find a method
5128              implemented in a stripped copy of libstdc++, will not work.
5129              If we do not have debug info for the definition, we will have to
5130              match them up some other way.
5131
5132              When we do name matching there is a related problem with function
5133              templates; two instantiated function templates are allowed to
5134              differ only by their return types, which we do not add here.  */
5135
5136           if (cu->language == language_cplus && strchr (name, '<') == NULL)
5137             {
5138               struct attribute *attr;
5139               struct die_info *child;
5140               int first = 1;
5141
5142               die->building_fullname = 1;
5143
5144               for (child = die->child; child != NULL; child = child->sibling)
5145                 {
5146                   struct type *type;
5147                   long value;
5148                   gdb_byte *bytes;
5149                   struct dwarf2_locexpr_baton *baton;
5150                   struct value *v;
5151
5152                   if (child->tag != DW_TAG_template_type_param
5153                       && child->tag != DW_TAG_template_value_param)
5154                     continue;
5155
5156                   if (first)
5157                     {
5158                       fputs_unfiltered ("<", buf);
5159                       first = 0;
5160                     }
5161                   else
5162                     fputs_unfiltered (", ", buf);
5163
5164                   attr = dwarf2_attr (child, DW_AT_type, cu);
5165                   if (attr == NULL)
5166                     {
5167                       complaint (&symfile_complaints,
5168                                  _("template parameter missing DW_AT_type"));
5169                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
5170                       continue;
5171                     }
5172                   type = die_type (child, cu);
5173
5174                   if (child->tag == DW_TAG_template_type_param)
5175                     {
5176                       c_print_type (type, "", buf, -1, 0);
5177                       continue;
5178                     }
5179
5180                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
5181                   if (attr == NULL)
5182                     {
5183                       complaint (&symfile_complaints,
5184                                  _("template parameter missing "
5185                                    "DW_AT_const_value"));
5186                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
5187                       continue;
5188                     }
5189
5190                   dwarf2_const_value_attr (attr, type, name,
5191                                            &cu->comp_unit_obstack, cu,
5192                                            &value, &bytes, &baton);
5193
5194                   if (TYPE_NOSIGN (type))
5195                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
5196                        changed, this can use value_print instead.  */
5197                     c_printchar (value, type, buf);
5198                   else
5199                     {
5200                       struct value_print_options opts;
5201
5202                       if (baton != NULL)
5203                         v = dwarf2_evaluate_loc_desc (type, NULL,
5204                                                       baton->data,
5205                                                       baton->size,
5206                                                       baton->per_cu);
5207                       else if (bytes != NULL)
5208                         {
5209                           v = allocate_value (type);
5210                           memcpy (value_contents_writeable (v), bytes,
5211                                   TYPE_LENGTH (type));
5212                         }
5213                       else
5214                         v = value_from_longest (type, value);
5215
5216                       /* Specify decimal so that we do not depend on
5217                          the radix.  */
5218                       get_formatted_print_options (&opts, 'd');
5219                       opts.raw = 1;
5220                       value_print (v, buf, &opts);
5221                       release_value (v);
5222                       value_free (v);
5223                     }
5224                 }
5225
5226               die->building_fullname = 0;
5227
5228               if (!first)
5229                 {
5230                   /* Close the argument list, with a space if necessary
5231                      (nested templates).  */
5232                   char last_char = '\0';
5233                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
5234                   if (last_char == '>')
5235                     fputs_unfiltered (" >", buf);
5236                   else
5237                     fputs_unfiltered (">", buf);
5238                 }
5239             }
5240
5241           /* For Java and C++ methods, append formal parameter type
5242              information, if PHYSNAME.  */
5243
5244           if (physname && die->tag == DW_TAG_subprogram
5245               && (cu->language == language_cplus
5246                   || cu->language == language_java))
5247             {
5248               struct type *type = read_type_die (die, cu);
5249
5250               c_type_print_args (type, buf, 1, cu->language);
5251
5252               if (cu->language == language_java)
5253                 {
5254                   /* For java, we must append the return type to method
5255                      names.  */
5256                   if (die->tag == DW_TAG_subprogram)
5257                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5258                                      0, 0);
5259                 }
5260               else if (cu->language == language_cplus)
5261                 {
5262                   /* Assume that an artificial first parameter is
5263                      "this", but do not crash if it is not.  RealView
5264                      marks unnamed (and thus unused) parameters as
5265                      artificial; there is no way to differentiate
5266                      the two cases.  */
5267                   if (TYPE_NFIELDS (type) > 0
5268                       && TYPE_FIELD_ARTIFICIAL (type, 0)
5269                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5270                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5271                                                                         0))))
5272                     fputs_unfiltered (" const", buf);
5273                 }
5274             }
5275
5276           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5277                                        &length);
5278           ui_file_delete (buf);
5279
5280           if (cu->language == language_cplus)
5281             {
5282               char *cname
5283                 = dwarf2_canonicalize_name (name, cu,
5284                                             &objfile->objfile_obstack);
5285
5286               if (cname != NULL)
5287                 name = cname;
5288             }
5289         }
5290     }
5291
5292   return name;
5293 }
5294
5295 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5296    If scope qualifiers are appropriate they will be added.  The result
5297    will be allocated on the objfile_obstack, or NULL if the DIE does
5298    not have a name.  NAME may either be from a previous call to
5299    dwarf2_name or NULL.
5300
5301    The output string will be canonicalized (if C++/Java).  */
5302
5303 static const char *
5304 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5305 {
5306   return dwarf2_compute_name (name, die, cu, 0);
5307 }
5308
5309 /* Construct a physname for the given DIE in CU.  NAME may either be
5310    from a previous call to dwarf2_name or NULL.  The result will be
5311    allocated on the objfile_objstack or NULL if the DIE does not have a
5312    name.
5313
5314    The output string will be canonicalized (if C++/Java).  */
5315
5316 static const char *
5317 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5318 {
5319   struct objfile *objfile = cu->objfile;
5320   struct attribute *attr;
5321   const char *retval, *mangled = NULL, *canon = NULL;
5322   struct cleanup *back_to;
5323   int need_copy = 1;
5324
5325   /* In this case dwarf2_compute_name is just a shortcut not building anything
5326      on its own.  */
5327   if (!die_needs_namespace (die, cu))
5328     return dwarf2_compute_name (name, die, cu, 1);
5329
5330   back_to = make_cleanup (null_cleanup, NULL);
5331
5332   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5333   if (!attr)
5334     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5335
5336   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5337      has computed.  */
5338   if (attr && DW_STRING (attr))
5339     {
5340       char *demangled;
5341
5342       mangled = DW_STRING (attr);
5343
5344       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5345          type.  It is easier for GDB users to search for such functions as
5346          `name(params)' than `long name(params)'.  In such case the minimal
5347          symbol names do not match the full symbol names but for template
5348          functions there is never a need to look up their definition from their
5349          declaration so the only disadvantage remains the minimal symbol
5350          variant `long name(params)' does not have the proper inferior type.
5351          */
5352
5353       demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5354                                             | (cu->language == language_java
5355                                                ? DMGL_JAVA | DMGL_RET_POSTFIX
5356                                                : DMGL_RET_DROP)));
5357       if (demangled)
5358         {
5359           make_cleanup (xfree, demangled);
5360           canon = demangled;
5361         }
5362       else
5363         {
5364           canon = mangled;
5365           need_copy = 0;
5366         }
5367     }
5368
5369   if (canon == NULL || check_physname)
5370     {
5371       const char *physname = dwarf2_compute_name (name, die, cu, 1);
5372
5373       if (canon != NULL && strcmp (physname, canon) != 0)
5374         {
5375           /* It may not mean a bug in GDB.  The compiler could also
5376              compute DW_AT_linkage_name incorrectly.  But in such case
5377              GDB would need to be bug-to-bug compatible.  */
5378
5379           complaint (&symfile_complaints,
5380                      _("Computed physname <%s> does not match demangled <%s> "
5381                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5382                      physname, canon, mangled, die->offset, objfile->name);
5383
5384           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5385              is available here - over computed PHYSNAME.  It is safer
5386              against both buggy GDB and buggy compilers.  */
5387
5388           retval = canon;
5389         }
5390       else
5391         {
5392           retval = physname;
5393           need_copy = 0;
5394         }
5395     }
5396   else
5397     retval = canon;
5398
5399   if (need_copy)
5400     retval = obsavestring (retval, strlen (retval),
5401                            &objfile->objfile_obstack);
5402
5403   do_cleanups (back_to);
5404   return retval;
5405 }
5406
5407 /* Read the import statement specified by the given die and record it.  */
5408
5409 static void
5410 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5411 {
5412   struct objfile *objfile = cu->objfile;
5413   struct attribute *import_attr;
5414   struct die_info *imported_die, *child_die;
5415   struct dwarf2_cu *imported_cu;
5416   const char *imported_name;
5417   const char *imported_name_prefix;
5418   const char *canonical_name;
5419   const char *import_alias;
5420   const char *imported_declaration = NULL;
5421   const char *import_prefix;
5422   VEC (const_char_ptr) *excludes = NULL;
5423   struct cleanup *cleanups;
5424
5425   char *temp;
5426
5427   import_attr = dwarf2_attr (die, DW_AT_import, cu);
5428   if (import_attr == NULL)
5429     {
5430       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5431                  dwarf_tag_name (die->tag));
5432       return;
5433     }
5434
5435   imported_cu = cu;
5436   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5437   imported_name = dwarf2_name (imported_die, imported_cu);
5438   if (imported_name == NULL)
5439     {
5440       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5441
5442         The import in the following code:
5443         namespace A
5444           {
5445             typedef int B;
5446           }
5447
5448         int main ()
5449           {
5450             using A::B;
5451             B b;
5452             return b;
5453           }
5454
5455         ...
5456          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5457             <52>   DW_AT_decl_file   : 1
5458             <53>   DW_AT_decl_line   : 6
5459             <54>   DW_AT_import      : <0x75>
5460          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5461             <59>   DW_AT_name        : B
5462             <5b>   DW_AT_decl_file   : 1
5463             <5c>   DW_AT_decl_line   : 2
5464             <5d>   DW_AT_type        : <0x6e>
5465         ...
5466          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5467             <76>   DW_AT_byte_size   : 4
5468             <77>   DW_AT_encoding    : 5        (signed)
5469
5470         imports the wrong die ( 0x75 instead of 0x58 ).
5471         This case will be ignored until the gcc bug is fixed.  */
5472       return;
5473     }
5474
5475   /* Figure out the local name after import.  */
5476   import_alias = dwarf2_name (die, cu);
5477
5478   /* Figure out where the statement is being imported to.  */
5479   import_prefix = determine_prefix (die, cu);
5480
5481   /* Figure out what the scope of the imported die is and prepend it
5482      to the name of the imported die.  */
5483   imported_name_prefix = determine_prefix (imported_die, imported_cu);
5484
5485   if (imported_die->tag != DW_TAG_namespace
5486       && imported_die->tag != DW_TAG_module)
5487     {
5488       imported_declaration = imported_name;
5489       canonical_name = imported_name_prefix;
5490     }
5491   else if (strlen (imported_name_prefix) > 0)
5492     {
5493       temp = alloca (strlen (imported_name_prefix)
5494                      + 2 + strlen (imported_name) + 1);
5495       strcpy (temp, imported_name_prefix);
5496       strcat (temp, "::");
5497       strcat (temp, imported_name);
5498       canonical_name = temp;
5499     }
5500   else
5501     canonical_name = imported_name;
5502
5503   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5504
5505   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5506     for (child_die = die->child; child_die && child_die->tag;
5507          child_die = sibling_die (child_die))
5508       {
5509         /* DWARF-4: A Fortran use statement with a “rename list” may be
5510            represented by an imported module entry with an import attribute
5511            referring to the module and owned entries corresponding to those
5512            entities that are renamed as part of being imported.  */
5513
5514         if (child_die->tag != DW_TAG_imported_declaration)
5515           {
5516             complaint (&symfile_complaints,
5517                        _("child DW_TAG_imported_declaration expected "
5518                          "- DIE at 0x%x [in module %s]"),
5519                        child_die->offset, objfile->name);
5520             continue;
5521           }
5522
5523         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5524         if (import_attr == NULL)
5525           {
5526             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5527                        dwarf_tag_name (child_die->tag));
5528             continue;
5529           }
5530
5531         imported_cu = cu;
5532         imported_die = follow_die_ref_or_sig (child_die, import_attr,
5533                                               &imported_cu);
5534         imported_name = dwarf2_name (imported_die, imported_cu);
5535         if (imported_name == NULL)
5536           {
5537             complaint (&symfile_complaints,
5538                        _("child DW_TAG_imported_declaration has unknown "
5539                          "imported name - DIE at 0x%x [in module %s]"),
5540                        child_die->offset, objfile->name);
5541             continue;
5542           }
5543
5544         VEC_safe_push (const_char_ptr, excludes, imported_name);
5545
5546         process_die (child_die, cu);
5547       }
5548
5549   cp_add_using_directive (import_prefix,
5550                           canonical_name,
5551                           import_alias,
5552                           imported_declaration,
5553                           excludes,
5554                           &objfile->objfile_obstack);
5555
5556   do_cleanups (cleanups);
5557 }
5558
5559 /* Cleanup function for read_file_scope.  */
5560
5561 static void
5562 free_cu_line_header (void *arg)
5563 {
5564   struct dwarf2_cu *cu = arg;
5565
5566   free_line_header (cu->line_header);
5567   cu->line_header = NULL;
5568 }
5569
5570 static void
5571 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5572                          char **name, char **comp_dir)
5573 {
5574   struct attribute *attr;
5575
5576   *name = NULL;
5577   *comp_dir = NULL;
5578
5579   /* Find the filename.  Do not use dwarf2_name here, since the filename
5580      is not a source language identifier.  */
5581   attr = dwarf2_attr (die, DW_AT_name, cu);
5582   if (attr)
5583     {
5584       *name = DW_STRING (attr);
5585     }
5586
5587   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5588   if (attr)
5589     *comp_dir = DW_STRING (attr);
5590   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5591     {
5592       *comp_dir = ldirname (*name);
5593       if (*comp_dir != NULL)
5594         make_cleanup (xfree, *comp_dir);
5595     }
5596   if (*comp_dir != NULL)
5597     {
5598       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5599          directory, get rid of it.  */
5600       char *cp = strchr (*comp_dir, ':');
5601
5602       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5603         *comp_dir = cp + 1;
5604     }
5605
5606   if (*name == NULL)
5607     *name = "<unknown>";
5608 }
5609
5610 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5611    DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5612    COMP_DIR is the compilation directory.
5613    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
5614
5615 static void
5616 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5617                         const char *comp_dir, int want_line_info)
5618 {
5619   struct attribute *attr;
5620   struct objfile *objfile = cu->objfile;
5621   bfd *abfd = objfile->obfd;
5622
5623   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5624   if (attr)
5625     {
5626       unsigned int line_offset = DW_UNSND (attr);
5627       struct line_header *line_header
5628         = dwarf_decode_line_header (line_offset, abfd, cu);
5629
5630       if (line_header)
5631         {
5632           cu->line_header = line_header;
5633           make_cleanup (free_cu_line_header, cu);
5634           dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5635         }
5636     }
5637 }
5638
5639 /* Process DW_TAG_compile_unit.  */
5640
5641 static void
5642 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5643 {
5644   struct objfile *objfile = cu->objfile;
5645   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5646   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5647   CORE_ADDR highpc = ((CORE_ADDR) 0);
5648   struct attribute *attr;
5649   char *name = NULL;
5650   char *comp_dir = NULL;
5651   struct die_info *child_die;
5652   bfd *abfd = objfile->obfd;
5653   CORE_ADDR baseaddr;
5654
5655   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5656
5657   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5658
5659   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5660      from finish_block.  */
5661   if (lowpc == ((CORE_ADDR) -1))
5662     lowpc = highpc;
5663   lowpc += baseaddr;
5664   highpc += baseaddr;
5665
5666   find_file_and_directory (die, cu, &name, &comp_dir);
5667
5668   attr = dwarf2_attr (die, DW_AT_language, cu);
5669   if (attr)
5670     {
5671       set_cu_language (DW_UNSND (attr), cu);
5672     }
5673
5674   attr = dwarf2_attr (die, DW_AT_producer, cu);
5675   if (attr)
5676     cu->producer = DW_STRING (attr);
5677
5678   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5679      standardised yet.  As a workaround for the language detection we fall
5680      back to the DW_AT_producer string.  */
5681   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5682     cu->language = language_opencl;
5683
5684   /* We assume that we're processing GCC output.  */
5685   processing_gcc_compilation = 2;
5686
5687   processing_has_namespace_info = 0;
5688
5689   start_symtab (name, comp_dir, lowpc);
5690   record_debugformat ("DWARF 2");
5691   record_producer (cu->producer);
5692
5693   /* Decode line number information if present.  We do this before
5694      processing child DIEs, so that the line header table is available
5695      for DW_AT_decl_file.  */
5696   handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5697
5698   /* Process all dies in compilation unit.  */
5699   if (die->child != NULL)
5700     {
5701       child_die = die->child;
5702       while (child_die && child_die->tag)
5703         {
5704           process_die (child_die, cu);
5705           child_die = sibling_die (child_die);
5706         }
5707     }
5708
5709   /* Decode macro information, if present.  Dwarf 2 macro information
5710      refers to information in the line number info statement program
5711      header, so we can only read it if we've read the header
5712      successfully.  */
5713   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5714   if (attr && cu->line_header)
5715     {
5716       if (dwarf2_attr (die, DW_AT_macro_info, cu))
5717         complaint (&symfile_complaints,
5718                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5719
5720       dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5721                            comp_dir, abfd, cu,
5722                            &dwarf2_per_objfile->macro, 1);
5723     }
5724   else
5725     {
5726       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5727       if (attr && cu->line_header)
5728         {
5729           unsigned int macro_offset = DW_UNSND (attr);
5730
5731           dwarf_decode_macros (cu->line_header, macro_offset,
5732                                comp_dir, abfd, cu,
5733                                &dwarf2_per_objfile->macinfo, 0);
5734         }
5735     }
5736
5737   do_cleanups (back_to);
5738 }
5739
5740 /* Process DW_TAG_type_unit.
5741    For TUs we want to skip the first top level sibling if it's not the
5742    actual type being defined by this TU.  In this case the first top
5743    level sibling is there to provide context only.  */
5744
5745 static void
5746 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5747 {
5748   struct objfile *objfile = cu->objfile;
5749   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5750   CORE_ADDR lowpc;
5751   struct attribute *attr;
5752   char *name = NULL;
5753   char *comp_dir = NULL;
5754   struct die_info *child_die;
5755   bfd *abfd = objfile->obfd;
5756
5757   /* start_symtab needs a low pc, but we don't really have one.
5758      Do what read_file_scope would do in the absence of such info.  */
5759   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5760
5761   /* Find the filename.  Do not use dwarf2_name here, since the filename
5762      is not a source language identifier.  */
5763   attr = dwarf2_attr (die, DW_AT_name, cu);
5764   if (attr)
5765     name = DW_STRING (attr);
5766
5767   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5768   if (attr)
5769     comp_dir = DW_STRING (attr);
5770   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5771     {
5772       comp_dir = ldirname (name);
5773       if (comp_dir != NULL)
5774         make_cleanup (xfree, comp_dir);
5775     }
5776
5777   if (name == NULL)
5778     name = "<unknown>";
5779
5780   attr = dwarf2_attr (die, DW_AT_language, cu);
5781   if (attr)
5782     set_cu_language (DW_UNSND (attr), cu);
5783
5784   /* This isn't technically needed today.  It is done for symmetry
5785      with read_file_scope.  */
5786   attr = dwarf2_attr (die, DW_AT_producer, cu);
5787   if (attr)
5788     cu->producer = DW_STRING (attr);
5789
5790   /* We assume that we're processing GCC output.  */
5791   processing_gcc_compilation = 2;
5792
5793   processing_has_namespace_info = 0;
5794
5795   start_symtab (name, comp_dir, lowpc);
5796   record_debugformat ("DWARF 2");
5797   record_producer (cu->producer);
5798
5799   /* Decode line number information if present.  We do this before
5800      processing child DIEs, so that the line header table is available
5801      for DW_AT_decl_file.
5802      We don't need the pc/line-number mapping for type units.  */
5803   handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5804
5805   /* Process the dies in the type unit.  */
5806   if (die->child == NULL)
5807     {
5808       dump_die_for_error (die);
5809       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5810              bfd_get_filename (abfd));
5811     }
5812
5813   child_die = die->child;
5814
5815   while (child_die && child_die->tag)
5816     {
5817       process_die (child_die, cu);
5818
5819       child_die = sibling_die (child_die);
5820     }
5821
5822   do_cleanups (back_to);
5823 }
5824
5825 /* qsort helper for inherit_abstract_dies.  */
5826
5827 static int
5828 unsigned_int_compar (const void *ap, const void *bp)
5829 {
5830   unsigned int a = *(unsigned int *) ap;
5831   unsigned int b = *(unsigned int *) bp;
5832
5833   return (a > b) - (b > a);
5834 }
5835
5836 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5837    Inherit only the children of the DW_AT_abstract_origin DIE not being
5838    already referenced by DW_AT_abstract_origin from the children of the
5839    current DIE.  */
5840
5841 static void
5842 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5843 {
5844   struct die_info *child_die;
5845   unsigned die_children_count;
5846   /* CU offsets which were referenced by children of the current DIE.  */
5847   unsigned *offsets;
5848   unsigned *offsets_end, *offsetp;
5849   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5850   struct die_info *origin_die;
5851   /* Iterator of the ORIGIN_DIE children.  */
5852   struct die_info *origin_child_die;
5853   struct cleanup *cleanups;
5854   struct attribute *attr;
5855   struct dwarf2_cu *origin_cu;
5856   struct pending **origin_previous_list_in_scope;
5857
5858   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5859   if (!attr)
5860     return;
5861
5862   /* Note that following die references may follow to a die in a
5863      different cu.  */
5864
5865   origin_cu = cu;
5866   origin_die = follow_die_ref (die, attr, &origin_cu);
5867
5868   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5869      symbols in.  */
5870   origin_previous_list_in_scope = origin_cu->list_in_scope;
5871   origin_cu->list_in_scope = cu->list_in_scope;
5872
5873   if (die->tag != origin_die->tag
5874       && !(die->tag == DW_TAG_inlined_subroutine
5875            && origin_die->tag == DW_TAG_subprogram))
5876     complaint (&symfile_complaints,
5877                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5878                die->offset, origin_die->offset);
5879
5880   child_die = die->child;
5881   die_children_count = 0;
5882   while (child_die && child_die->tag)
5883     {
5884       child_die = sibling_die (child_die);
5885       die_children_count++;
5886     }
5887   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5888   cleanups = make_cleanup (xfree, offsets);
5889
5890   offsets_end = offsets;
5891   child_die = die->child;
5892   while (child_die && child_die->tag)
5893     {
5894       /* For each CHILD_DIE, find the corresponding child of
5895          ORIGIN_DIE.  If there is more than one layer of
5896          DW_AT_abstract_origin, follow them all; there shouldn't be,
5897          but GCC versions at least through 4.4 generate this (GCC PR
5898          40573).  */
5899       struct die_info *child_origin_die = child_die;
5900       struct dwarf2_cu *child_origin_cu = cu;
5901
5902       while (1)
5903         {
5904           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5905                               child_origin_cu);
5906           if (attr == NULL)
5907             break;
5908           child_origin_die = follow_die_ref (child_origin_die, attr,
5909                                              &child_origin_cu);
5910         }
5911
5912       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5913          counterpart may exist.  */
5914       if (child_origin_die != child_die)
5915         {
5916           if (child_die->tag != child_origin_die->tag
5917               && !(child_die->tag == DW_TAG_inlined_subroutine
5918                    && child_origin_die->tag == DW_TAG_subprogram))
5919             complaint (&symfile_complaints,
5920                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5921                          "different tags"), child_die->offset,
5922                        child_origin_die->offset);
5923           if (child_origin_die->parent != origin_die)
5924             complaint (&symfile_complaints,
5925                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5926                          "different parents"), child_die->offset,
5927                        child_origin_die->offset);
5928           else
5929             *offsets_end++ = child_origin_die->offset;
5930         }
5931       child_die = sibling_die (child_die);
5932     }
5933   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5934          unsigned_int_compar);
5935   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5936     if (offsetp[-1] == *offsetp)
5937       complaint (&symfile_complaints,
5938                  _("Multiple children of DIE 0x%x refer "
5939                    "to DIE 0x%x as their abstract origin"),
5940                  die->offset, *offsetp);
5941
5942   offsetp = offsets;
5943   origin_child_die = origin_die->child;
5944   while (origin_child_die && origin_child_die->tag)
5945     {
5946       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5947       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5948         offsetp++;
5949       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5950         {
5951           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5952           process_die (origin_child_die, origin_cu);
5953         }
5954       origin_child_die = sibling_die (origin_child_die);
5955     }
5956   origin_cu->list_in_scope = origin_previous_list_in_scope;
5957
5958   do_cleanups (cleanups);
5959 }
5960
5961 static void
5962 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5963 {
5964   struct objfile *objfile = cu->objfile;
5965   struct context_stack *new;
5966   CORE_ADDR lowpc;
5967   CORE_ADDR highpc;
5968   struct die_info *child_die;
5969   struct attribute *attr, *call_line, *call_file;
5970   char *name;
5971   CORE_ADDR baseaddr;
5972   struct block *block;
5973   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5974   VEC (symbolp) *template_args = NULL;
5975   struct template_symbol *templ_func = NULL;
5976
5977   if (inlined_func)
5978     {
5979       /* If we do not have call site information, we can't show the
5980          caller of this inlined function.  That's too confusing, so
5981          only use the scope for local variables.  */
5982       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5983       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5984       if (call_line == NULL || call_file == NULL)
5985         {
5986           read_lexical_block_scope (die, cu);
5987           return;
5988         }
5989     }
5990
5991   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5992
5993   name = dwarf2_name (die, cu);
5994
5995   /* Ignore functions with missing or empty names.  These are actually
5996      illegal according to the DWARF standard.  */
5997   if (name == NULL)
5998     {
5999       complaint (&symfile_complaints,
6000                  _("missing name for subprogram DIE at %d"), die->offset);
6001       return;
6002     }
6003
6004   /* Ignore functions with missing or invalid low and high pc attributes.  */
6005   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6006     {
6007       attr = dwarf2_attr (die, DW_AT_external, cu);
6008       if (!attr || !DW_UNSND (attr))
6009         complaint (&symfile_complaints,
6010                    _("cannot get low and high bounds "
6011                      "for subprogram DIE at %d"),
6012                    die->offset);
6013       return;
6014     }
6015
6016   lowpc += baseaddr;
6017   highpc += baseaddr;
6018
6019   /* If we have any template arguments, then we must allocate a
6020      different sort of symbol.  */
6021   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6022     {
6023       if (child_die->tag == DW_TAG_template_type_param
6024           || child_die->tag == DW_TAG_template_value_param)
6025         {
6026           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6027                                        struct template_symbol);
6028           templ_func->base.is_cplus_template_function = 1;
6029           break;
6030         }
6031     }
6032
6033   new = push_context (0, lowpc);
6034   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6035                                (struct symbol *) templ_func);
6036
6037   /* If there is a location expression for DW_AT_frame_base, record
6038      it.  */
6039   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6040   if (attr)
6041     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6042        expression is being recorded directly in the function's symbol
6043        and not in a separate frame-base object.  I guess this hack is
6044        to avoid adding some sort of frame-base adjunct/annex to the
6045        function's symbol :-(.  The problem with doing this is that it
6046        results in a function symbol with a location expression that
6047        has nothing to do with the location of the function, ouch!  The
6048        relationship should be: a function's symbol has-a frame base; a
6049        frame-base has-a location expression.  */
6050     dwarf2_symbol_mark_computed (attr, new->name, cu);
6051
6052   cu->list_in_scope = &local_symbols;
6053
6054   if (die->child != NULL)
6055     {
6056       child_die = die->child;
6057       while (child_die && child_die->tag)
6058         {
6059           if (child_die->tag == DW_TAG_template_type_param
6060               || child_die->tag == DW_TAG_template_value_param)
6061             {
6062               struct symbol *arg = new_symbol (child_die, NULL, cu);
6063
6064               if (arg != NULL)
6065                 VEC_safe_push (symbolp, template_args, arg);
6066             }
6067           else
6068             process_die (child_die, cu);
6069           child_die = sibling_die (child_die);
6070         }
6071     }
6072
6073   inherit_abstract_dies (die, cu);
6074
6075   /* If we have a DW_AT_specification, we might need to import using
6076      directives from the context of the specification DIE.  See the
6077      comment in determine_prefix.  */
6078   if (cu->language == language_cplus
6079       && dwarf2_attr (die, DW_AT_specification, cu))
6080     {
6081       struct dwarf2_cu *spec_cu = cu;
6082       struct die_info *spec_die = die_specification (die, &spec_cu);
6083
6084       while (spec_die)
6085         {
6086           child_die = spec_die->child;
6087           while (child_die && child_die->tag)
6088             {
6089               if (child_die->tag == DW_TAG_imported_module)
6090                 process_die (child_die, spec_cu);
6091               child_die = sibling_die (child_die);
6092             }
6093
6094           /* In some cases, GCC generates specification DIEs that
6095              themselves contain DW_AT_specification attributes.  */
6096           spec_die = die_specification (spec_die, &spec_cu);
6097         }
6098     }
6099
6100   new = pop_context ();
6101   /* Make a block for the local symbols within.  */
6102   block = finish_block (new->name, &local_symbols, new->old_blocks,
6103                         lowpc, highpc, objfile);
6104
6105   /* For C++, set the block's scope.  */
6106   if (cu->language == language_cplus || cu->language == language_fortran)
6107     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6108                         determine_prefix (die, cu),
6109                         processing_has_namespace_info);
6110
6111   /* If we have address ranges, record them.  */
6112   dwarf2_record_block_ranges (die, block, baseaddr, cu);
6113
6114   /* Attach template arguments to function.  */
6115   if (! VEC_empty (symbolp, template_args))
6116     {
6117       gdb_assert (templ_func != NULL);
6118
6119       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6120       templ_func->template_arguments
6121         = obstack_alloc (&objfile->objfile_obstack,
6122                          (templ_func->n_template_arguments
6123                           * sizeof (struct symbol *)));
6124       memcpy (templ_func->template_arguments,
6125               VEC_address (symbolp, template_args),
6126               (templ_func->n_template_arguments * sizeof (struct symbol *)));
6127       VEC_free (symbolp, template_args);
6128     }
6129
6130   /* In C++, we can have functions nested inside functions (e.g., when
6131      a function declares a class that has methods).  This means that
6132      when we finish processing a function scope, we may need to go
6133      back to building a containing block's symbol lists.  */
6134   local_symbols = new->locals;
6135   param_symbols = new->params;
6136   using_directives = new->using_directives;
6137
6138   /* If we've finished processing a top-level function, subsequent
6139      symbols go in the file symbol list.  */
6140   if (outermost_context_p ())
6141     cu->list_in_scope = &file_symbols;
6142 }
6143
6144 /* Process all the DIES contained within a lexical block scope.  Start
6145    a new scope, process the dies, and then close the scope.  */
6146
6147 static void
6148 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6149 {
6150   struct objfile *objfile = cu->objfile;
6151   struct context_stack *new;
6152   CORE_ADDR lowpc, highpc;
6153   struct die_info *child_die;
6154   CORE_ADDR baseaddr;
6155
6156   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6157
6158   /* Ignore blocks with missing or invalid low and high pc attributes.  */
6159   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6160      as multiple lexical blocks?  Handling children in a sane way would
6161      be nasty.  Might be easier to properly extend generic blocks to
6162      describe ranges.  */
6163   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6164     return;
6165   lowpc += baseaddr;
6166   highpc += baseaddr;
6167
6168   push_context (0, lowpc);
6169   if (die->child != NULL)
6170     {
6171       child_die = die->child;
6172       while (child_die && child_die->tag)
6173         {
6174           process_die (child_die, cu);
6175           child_die = sibling_die (child_die);
6176         }
6177     }
6178   new = pop_context ();
6179
6180   if (local_symbols != NULL || using_directives != NULL)
6181     {
6182       struct block *block
6183         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6184                         highpc, objfile);
6185
6186       /* Note that recording ranges after traversing children, as we
6187          do here, means that recording a parent's ranges entails
6188          walking across all its children's ranges as they appear in
6189          the address map, which is quadratic behavior.
6190
6191          It would be nicer to record the parent's ranges before
6192          traversing its children, simply overriding whatever you find
6193          there.  But since we don't even decide whether to create a
6194          block until after we've traversed its children, that's hard
6195          to do.  */
6196       dwarf2_record_block_ranges (die, block, baseaddr, cu);
6197     }
6198   local_symbols = new->locals;
6199   using_directives = new->using_directives;
6200 }
6201
6202 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
6203
6204 static void
6205 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6206 {
6207   struct objfile *objfile = cu->objfile;
6208   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6209   CORE_ADDR pc, baseaddr;
6210   struct attribute *attr;
6211   struct call_site *call_site, call_site_local;
6212   void **slot;
6213   int nparams;
6214   struct die_info *child_die;
6215
6216   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6217
6218   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6219   if (!attr)
6220     {
6221       complaint (&symfile_complaints,
6222                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6223                    "DIE 0x%x [in module %s]"),
6224                  die->offset, objfile->name);
6225       return;
6226     }
6227   pc = DW_ADDR (attr) + baseaddr;
6228
6229   if (cu->call_site_htab == NULL)
6230     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6231                                                NULL, &objfile->objfile_obstack,
6232                                                hashtab_obstack_allocate, NULL);
6233   call_site_local.pc = pc;
6234   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6235   if (*slot != NULL)
6236     {
6237       complaint (&symfile_complaints,
6238                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
6239                    "DIE 0x%x [in module %s]"),
6240                  paddress (gdbarch, pc), die->offset, objfile->name);
6241       return;
6242     }
6243
6244   /* Count parameters at the caller.  */
6245
6246   nparams = 0;
6247   for (child_die = die->child; child_die && child_die->tag;
6248        child_die = sibling_die (child_die))
6249     {
6250       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6251         {
6252           complaint (&symfile_complaints,
6253                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6254                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6255                      child_die->tag, child_die->offset, objfile->name);
6256           continue;
6257         }
6258
6259       nparams++;
6260     }
6261
6262   call_site = obstack_alloc (&objfile->objfile_obstack,
6263                              (sizeof (*call_site)
6264                               + (sizeof (*call_site->parameter)
6265                                  * (nparams - 1))));
6266   *slot = call_site;
6267   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6268   call_site->pc = pc;
6269
6270   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6271     {
6272       struct die_info *func_die;
6273
6274       /* Skip also over DW_TAG_inlined_subroutine.  */
6275       for (func_die = die->parent;
6276            func_die && func_die->tag != DW_TAG_subprogram
6277            && func_die->tag != DW_TAG_subroutine_type;
6278            func_die = func_die->parent);
6279
6280       /* DW_AT_GNU_all_call_sites is a superset
6281          of DW_AT_GNU_all_tail_call_sites.  */
6282       if (func_die
6283           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6284           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6285         {
6286           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6287              not complete.  But keep CALL_SITE for look ups via call_site_htab,
6288              both the initial caller containing the real return address PC and
6289              the final callee containing the current PC of a chain of tail
6290              calls do not need to have the tail call list complete.  But any
6291              function candidate for a virtual tail call frame searched via
6292              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6293              determined unambiguously.  */
6294         }
6295       else
6296         {
6297           struct type *func_type = NULL;
6298
6299           if (func_die)
6300             func_type = get_die_type (func_die, cu);
6301           if (func_type != NULL)
6302             {
6303               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6304
6305               /* Enlist this call site to the function.  */
6306               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6307               TYPE_TAIL_CALL_LIST (func_type) = call_site;
6308             }
6309           else
6310             complaint (&symfile_complaints,
6311                        _("Cannot find function owning DW_TAG_GNU_call_site "
6312                          "DIE 0x%x [in module %s]"),
6313                        die->offset, objfile->name);
6314         }
6315     }
6316
6317   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6318   if (attr == NULL)
6319     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6320   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6321   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6322     /* Keep NULL DWARF_BLOCK.  */;
6323   else if (attr_form_is_block (attr))
6324     {
6325       struct dwarf2_locexpr_baton *dlbaton;
6326
6327       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6328       dlbaton->data = DW_BLOCK (attr)->data;
6329       dlbaton->size = DW_BLOCK (attr)->size;
6330       dlbaton->per_cu = cu->per_cu;
6331
6332       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6333     }
6334   else if (is_ref_attr (attr))
6335     {
6336       struct dwarf2_cu *target_cu = cu;
6337       struct die_info *target_die;
6338
6339       target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6340       gdb_assert (target_cu->objfile == objfile);
6341       if (die_is_declaration (target_die, target_cu))
6342         {
6343           const char *target_physname;
6344
6345           target_physname = dwarf2_physname (NULL, target_die, target_cu);
6346           if (target_physname == NULL)
6347             complaint (&symfile_complaints,
6348                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6349                          "physname, for referencing DIE 0x%x [in module %s]"),
6350                        die->offset, objfile->name);
6351           else
6352             SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6353         }
6354       else
6355         {
6356           CORE_ADDR lowpc;
6357
6358           /* DW_AT_entry_pc should be preferred.  */
6359           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6360             complaint (&symfile_complaints,
6361                        _("DW_AT_GNU_call_site_target target DIE has invalid "
6362                          "low pc, for referencing DIE 0x%x [in module %s]"),
6363                        die->offset, objfile->name);
6364           else
6365             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6366         }
6367     }
6368   else
6369     complaint (&symfile_complaints,
6370                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6371                  "block nor reference, for DIE 0x%x [in module %s]"),
6372                die->offset, objfile->name);
6373
6374   call_site->per_cu = cu->per_cu;
6375
6376   for (child_die = die->child;
6377        child_die && child_die->tag;
6378        child_die = sibling_die (child_die))
6379     {
6380       struct dwarf2_locexpr_baton *dlbaton;
6381       struct call_site_parameter *parameter;
6382
6383       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6384         {
6385           /* Already printed the complaint above.  */
6386           continue;
6387         }
6388
6389       gdb_assert (call_site->parameter_count < nparams);
6390       parameter = &call_site->parameter[call_site->parameter_count];
6391
6392       /* DW_AT_location specifies the register number.  Value of the data
6393          assumed for the register is contained in DW_AT_GNU_call_site_value.  */
6394
6395       attr = dwarf2_attr (child_die, DW_AT_location, cu);
6396       if (!attr || !attr_form_is_block (attr))
6397         {
6398           complaint (&symfile_complaints,
6399                      _("No DW_FORM_block* DW_AT_location for "
6400                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6401                      child_die->offset, objfile->name);
6402           continue;
6403         }
6404       parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6405                                  &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6406       if (parameter->dwarf_reg == -1
6407           && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6408                                   &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6409                                         &parameter->fb_offset))
6410         {
6411           complaint (&symfile_complaints,
6412                      _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6413                        "for DW_FORM_block* DW_AT_location for "
6414                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6415                      child_die->offset, objfile->name);
6416           continue;
6417         }
6418
6419       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6420       if (!attr_form_is_block (attr))
6421         {
6422           complaint (&symfile_complaints,
6423                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6424                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6425                      child_die->offset, objfile->name);
6426           continue;
6427         }
6428       parameter->value = DW_BLOCK (attr)->data;
6429       parameter->value_size = DW_BLOCK (attr)->size;
6430
6431       /* Parameters are not pre-cleared by memset above.  */
6432       parameter->data_value = NULL;
6433       parameter->data_value_size = 0;
6434       call_site->parameter_count++;
6435
6436       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6437       if (attr)
6438         {
6439           if (!attr_form_is_block (attr))
6440             complaint (&symfile_complaints,
6441                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6442                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6443                        child_die->offset, objfile->name);
6444           else
6445             {
6446               parameter->data_value = DW_BLOCK (attr)->data;
6447               parameter->data_value_size = DW_BLOCK (attr)->size;
6448             }
6449         }
6450     }
6451 }
6452
6453 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6454    Return 1 if the attributes are present and valid, otherwise, return 0.
6455    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
6456
6457 static int
6458 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6459                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
6460                     struct partial_symtab *ranges_pst)
6461 {
6462   struct objfile *objfile = cu->objfile;
6463   struct comp_unit_head *cu_header = &cu->header;
6464   bfd *obfd = objfile->obfd;
6465   unsigned int addr_size = cu_header->addr_size;
6466   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6467   /* Base address selection entry.  */
6468   CORE_ADDR base;
6469   int found_base;
6470   unsigned int dummy;
6471   gdb_byte *buffer;
6472   CORE_ADDR marker;
6473   int low_set;
6474   CORE_ADDR low = 0;
6475   CORE_ADDR high = 0;
6476   CORE_ADDR baseaddr;
6477
6478   found_base = cu->base_known;
6479   base = cu->base_address;
6480
6481   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6482   if (offset >= dwarf2_per_objfile->ranges.size)
6483     {
6484       complaint (&symfile_complaints,
6485                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
6486                  offset);
6487       return 0;
6488     }
6489   buffer = dwarf2_per_objfile->ranges.buffer + offset;
6490
6491   /* Read in the largest possible address.  */
6492   marker = read_address (obfd, buffer, cu, &dummy);
6493   if ((marker & mask) == mask)
6494     {
6495       /* If we found the largest possible address, then
6496          read the base address.  */
6497       base = read_address (obfd, buffer + addr_size, cu, &dummy);
6498       buffer += 2 * addr_size;
6499       offset += 2 * addr_size;
6500       found_base = 1;
6501     }
6502
6503   low_set = 0;
6504
6505   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6506
6507   while (1)
6508     {
6509       CORE_ADDR range_beginning, range_end;
6510
6511       range_beginning = read_address (obfd, buffer, cu, &dummy);
6512       buffer += addr_size;
6513       range_end = read_address (obfd, buffer, cu, &dummy);
6514       buffer += addr_size;
6515       offset += 2 * addr_size;
6516
6517       /* An end of list marker is a pair of zero addresses.  */
6518       if (range_beginning == 0 && range_end == 0)
6519         /* Found the end of list entry.  */
6520         break;
6521
6522       /* Each base address selection entry is a pair of 2 values.
6523          The first is the largest possible address, the second is
6524          the base address.  Check for a base address here.  */
6525       if ((range_beginning & mask) == mask)
6526         {
6527           /* If we found the largest possible address, then
6528              read the base address.  */
6529           base = read_address (obfd, buffer + addr_size, cu, &dummy);
6530           found_base = 1;
6531           continue;
6532         }
6533
6534       if (!found_base)
6535         {
6536           /* We have no valid base address for the ranges
6537              data.  */
6538           complaint (&symfile_complaints,
6539                      _("Invalid .debug_ranges data (no base address)"));
6540           return 0;
6541         }
6542
6543       if (range_beginning > range_end)
6544         {
6545           /* Inverted range entries are invalid.  */
6546           complaint (&symfile_complaints,
6547                      _("Invalid .debug_ranges data (inverted range)"));
6548           return 0;
6549         }
6550
6551       /* Empty range entries have no effect.  */
6552       if (range_beginning == range_end)
6553         continue;
6554
6555       range_beginning += base;
6556       range_end += base;
6557
6558       if (ranges_pst != NULL)
6559         addrmap_set_empty (objfile->psymtabs_addrmap,
6560                            range_beginning + baseaddr,
6561                            range_end - 1 + baseaddr,
6562                            ranges_pst);
6563
6564       /* FIXME: This is recording everything as a low-high
6565          segment of consecutive addresses.  We should have a
6566          data structure for discontiguous block ranges
6567          instead.  */
6568       if (! low_set)
6569         {
6570           low = range_beginning;
6571           high = range_end;
6572           low_set = 1;
6573         }
6574       else
6575         {
6576           if (range_beginning < low)
6577             low = range_beginning;
6578           if (range_end > high)
6579             high = range_end;
6580         }
6581     }
6582
6583   if (! low_set)
6584     /* If the first entry is an end-of-list marker, the range
6585        describes an empty scope, i.e. no instructions.  */
6586     return 0;
6587
6588   if (low_return)
6589     *low_return = low;
6590   if (high_return)
6591     *high_return = high;
6592   return 1;
6593 }
6594
6595 /* Get low and high pc attributes from a die.  Return 1 if the attributes
6596    are present and valid, otherwise, return 0.  Return -1 if the range is
6597    discontinuous, i.e. derived from DW_AT_ranges information.  */
6598 static int
6599 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6600                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
6601                       struct partial_symtab *pst)
6602 {
6603   struct attribute *attr;
6604   CORE_ADDR low = 0;
6605   CORE_ADDR high = 0;
6606   int ret = 0;
6607
6608   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6609   if (attr)
6610     {
6611       high = DW_ADDR (attr);
6612       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6613       if (attr)
6614         low = DW_ADDR (attr);
6615       else
6616         /* Found high w/o low attribute.  */
6617         return 0;
6618
6619       /* Found consecutive range of addresses.  */
6620       ret = 1;
6621     }
6622   else
6623     {
6624       attr = dwarf2_attr (die, DW_AT_ranges, cu);
6625       if (attr != NULL)
6626         {
6627           /* Value of the DW_AT_ranges attribute is the offset in the
6628              .debug_ranges section.  */
6629           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6630             return 0;
6631           /* Found discontinuous range of addresses.  */
6632           ret = -1;
6633         }
6634     }
6635
6636   /* read_partial_die has also the strict LOW < HIGH requirement.  */
6637   if (high <= low)
6638     return 0;
6639
6640   /* When using the GNU linker, .gnu.linkonce. sections are used to
6641      eliminate duplicate copies of functions and vtables and such.
6642      The linker will arbitrarily choose one and discard the others.
6643      The AT_*_pc values for such functions refer to local labels in
6644      these sections.  If the section from that file was discarded, the
6645      labels are not in the output, so the relocs get a value of 0.
6646      If this is a discarded function, mark the pc bounds as invalid,
6647      so that GDB will ignore it.  */
6648   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6649     return 0;
6650
6651   *lowpc = low;
6652   if (highpc)
6653     *highpc = high;
6654   return ret;
6655 }
6656
6657 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6658    its low and high PC addresses.  Do nothing if these addresses could not
6659    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
6660    and HIGHPC to the high address if greater than HIGHPC.  */
6661
6662 static void
6663 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6664                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
6665                                  struct dwarf2_cu *cu)
6666 {
6667   CORE_ADDR low, high;
6668   struct die_info *child = die->child;
6669
6670   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6671     {
6672       *lowpc = min (*lowpc, low);
6673       *highpc = max (*highpc, high);
6674     }
6675
6676   /* If the language does not allow nested subprograms (either inside
6677      subprograms or lexical blocks), we're done.  */
6678   if (cu->language != language_ada)
6679     return;
6680
6681   /* Check all the children of the given DIE.  If it contains nested
6682      subprograms, then check their pc bounds.  Likewise, we need to
6683      check lexical blocks as well, as they may also contain subprogram
6684      definitions.  */
6685   while (child && child->tag)
6686     {
6687       if (child->tag == DW_TAG_subprogram
6688           || child->tag == DW_TAG_lexical_block)
6689         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6690       child = sibling_die (child);
6691     }
6692 }
6693
6694 /* Get the low and high pc's represented by the scope DIE, and store
6695    them in *LOWPC and *HIGHPC.  If the correct values can't be
6696    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
6697
6698 static void
6699 get_scope_pc_bounds (struct die_info *die,
6700                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
6701                      struct dwarf2_cu *cu)
6702 {
6703   CORE_ADDR best_low = (CORE_ADDR) -1;
6704   CORE_ADDR best_high = (CORE_ADDR) 0;
6705   CORE_ADDR current_low, current_high;
6706
6707   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6708     {
6709       best_low = current_low;
6710       best_high = current_high;
6711     }
6712   else
6713     {
6714       struct die_info *child = die->child;
6715
6716       while (child && child->tag)
6717         {
6718           switch (child->tag) {
6719           case DW_TAG_subprogram:
6720             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6721             break;
6722           case DW_TAG_namespace:
6723           case DW_TAG_module:
6724             /* FIXME: carlton/2004-01-16: Should we do this for
6725                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
6726                that current GCC's always emit the DIEs corresponding
6727                to definitions of methods of classes as children of a
6728                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6729                the DIEs giving the declarations, which could be
6730                anywhere).  But I don't see any reason why the
6731                standards says that they have to be there.  */
6732             get_scope_pc_bounds (child, &current_low, &current_high, cu);
6733
6734             if (current_low != ((CORE_ADDR) -1))
6735               {
6736                 best_low = min (best_low, current_low);
6737                 best_high = max (best_high, current_high);
6738               }
6739             break;
6740           default:
6741             /* Ignore.  */
6742             break;
6743           }
6744
6745           child = sibling_die (child);
6746         }
6747     }
6748
6749   *lowpc = best_low;
6750   *highpc = best_high;
6751 }
6752
6753 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6754    in DIE.  */
6755 static void
6756 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6757                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6758 {
6759   struct objfile *objfile = cu->objfile;
6760   struct attribute *attr;
6761
6762   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6763   if (attr)
6764     {
6765       CORE_ADDR high = DW_ADDR (attr);
6766
6767       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6768       if (attr)
6769         {
6770           CORE_ADDR low = DW_ADDR (attr);
6771
6772           record_block_range (block, baseaddr + low, baseaddr + high - 1);
6773         }
6774     }
6775
6776   attr = dwarf2_attr (die, DW_AT_ranges, cu);
6777   if (attr)
6778     {
6779       bfd *obfd = objfile->obfd;
6780
6781       /* The value of the DW_AT_ranges attribute is the offset of the
6782          address range list in the .debug_ranges section.  */
6783       unsigned long offset = DW_UNSND (attr);
6784       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6785
6786       /* For some target architectures, but not others, the
6787          read_address function sign-extends the addresses it returns.
6788          To recognize base address selection entries, we need a
6789          mask.  */
6790       unsigned int addr_size = cu->header.addr_size;
6791       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6792
6793       /* The base address, to which the next pair is relative.  Note
6794          that this 'base' is a DWARF concept: most entries in a range
6795          list are relative, to reduce the number of relocs against the
6796          debugging information.  This is separate from this function's
6797          'baseaddr' argument, which GDB uses to relocate debugging
6798          information from a shared library based on the address at
6799          which the library was loaded.  */
6800       CORE_ADDR base = cu->base_address;
6801       int base_known = cu->base_known;
6802
6803       gdb_assert (dwarf2_per_objfile->ranges.readin);
6804       if (offset >= dwarf2_per_objfile->ranges.size)
6805         {
6806           complaint (&symfile_complaints,
6807                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6808                      offset);
6809           return;
6810         }
6811
6812       for (;;)
6813         {
6814           unsigned int bytes_read;
6815           CORE_ADDR start, end;
6816
6817           start = read_address (obfd, buffer, cu, &bytes_read);
6818           buffer += bytes_read;
6819           end = read_address (obfd, buffer, cu, &bytes_read);
6820           buffer += bytes_read;
6821
6822           /* Did we find the end of the range list?  */
6823           if (start == 0 && end == 0)
6824             break;
6825
6826           /* Did we find a base address selection entry?  */
6827           else if ((start & base_select_mask) == base_select_mask)
6828             {
6829               base = end;
6830               base_known = 1;
6831             }
6832
6833           /* We found an ordinary address range.  */
6834           else
6835             {
6836               if (!base_known)
6837                 {
6838                   complaint (&symfile_complaints,
6839                              _("Invalid .debug_ranges data "
6840                                "(no base address)"));
6841                   return;
6842                 }
6843
6844               if (start > end)
6845                 {
6846                   /* Inverted range entries are invalid.  */
6847                   complaint (&symfile_complaints,
6848                              _("Invalid .debug_ranges data "
6849                                "(inverted range)"));
6850                   return;
6851                 }
6852
6853               /* Empty range entries have no effect.  */
6854               if (start == end)
6855                 continue;
6856
6857               record_block_range (block,
6858                                   baseaddr + base + start,
6859                                   baseaddr + base + end - 1);
6860             }
6861         }
6862     }
6863 }
6864
6865 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6866    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6867    during 4.6.0 experimental.  */
6868
6869 static int
6870 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6871 {
6872   const char *cs;
6873   int major, minor, release;
6874   int result = 0;
6875
6876   if (cu->producer == NULL)
6877     {
6878       /* For unknown compilers expect their behavior is DWARF version
6879          compliant.
6880
6881          GCC started to support .debug_types sections by -gdwarf-4 since
6882          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
6883          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6884          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6885          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
6886
6887       return 0;
6888     }
6889
6890   if (cu->checked_producer)
6891     return cu->producer_is_gxx_lt_4_6;
6892
6893   /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
6894
6895   if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6896     {
6897       /* For non-GCC compilers expect their behavior is DWARF version
6898          compliant.  */
6899     }
6900   else
6901     {
6902       cs = &cu->producer[strlen ("GNU ")];
6903       while (*cs && !isdigit (*cs))
6904         cs++;
6905       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6906         {
6907           /* Not recognized as GCC.  */
6908         }
6909       else
6910         result = major < 4 || (major == 4 && minor < 6);
6911     }
6912
6913   cu->checked_producer = 1;
6914   cu->producer_is_gxx_lt_4_6 = result;
6915
6916   return result;
6917 }
6918
6919 /* Return the default accessibility type if it is not overriden by
6920    DW_AT_accessibility.  */
6921
6922 static enum dwarf_access_attribute
6923 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6924 {
6925   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6926     {
6927       /* The default DWARF 2 accessibility for members is public, the default
6928          accessibility for inheritance is private.  */
6929
6930       if (die->tag != DW_TAG_inheritance)
6931         return DW_ACCESS_public;
6932       else
6933         return DW_ACCESS_private;
6934     }
6935   else
6936     {
6937       /* DWARF 3+ defines the default accessibility a different way.  The same
6938          rules apply now for DW_TAG_inheritance as for the members and it only
6939          depends on the container kind.  */
6940
6941       if (die->parent->tag == DW_TAG_class_type)
6942         return DW_ACCESS_private;
6943       else
6944         return DW_ACCESS_public;
6945     }
6946 }
6947
6948 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
6949    offset.  If the attribute was not found return 0, otherwise return
6950    1.  If it was found but could not properly be handled, set *OFFSET
6951    to 0.  */
6952
6953 static int
6954 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6955                              LONGEST *offset)
6956 {
6957   struct attribute *attr;
6958
6959   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6960   if (attr != NULL)
6961     {
6962       *offset = 0;
6963
6964       /* Note that we do not check for a section offset first here.
6965          This is because DW_AT_data_member_location is new in DWARF 4,
6966          so if we see it, we can assume that a constant form is really
6967          a constant and not a section offset.  */
6968       if (attr_form_is_constant (attr))
6969         *offset = dwarf2_get_attr_constant_value (attr, 0);
6970       else if (attr_form_is_section_offset (attr))
6971         dwarf2_complex_location_expr_complaint ();
6972       else if (attr_form_is_block (attr))
6973         *offset = decode_locdesc (DW_BLOCK (attr), cu);
6974       else
6975         dwarf2_complex_location_expr_complaint ();
6976
6977       return 1;
6978     }
6979
6980   return 0;
6981 }
6982
6983 /* Add an aggregate field to the field list.  */
6984
6985 static void
6986 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6987                   struct dwarf2_cu *cu)
6988 {
6989   struct objfile *objfile = cu->objfile;
6990   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6991   struct nextfield *new_field;
6992   struct attribute *attr;
6993   struct field *fp;
6994   char *fieldname = "";
6995
6996   /* Allocate a new field list entry and link it in.  */
6997   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6998   make_cleanup (xfree, new_field);
6999   memset (new_field, 0, sizeof (struct nextfield));
7000
7001   if (die->tag == DW_TAG_inheritance)
7002     {
7003       new_field->next = fip->baseclasses;
7004       fip->baseclasses = new_field;
7005     }
7006   else
7007     {
7008       new_field->next = fip->fields;
7009       fip->fields = new_field;
7010     }
7011   fip->nfields++;
7012
7013   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7014   if (attr)
7015     new_field->accessibility = DW_UNSND (attr);
7016   else
7017     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7018   if (new_field->accessibility != DW_ACCESS_public)
7019     fip->non_public_fields = 1;
7020
7021   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7022   if (attr)
7023     new_field->virtuality = DW_UNSND (attr);
7024   else
7025     new_field->virtuality = DW_VIRTUALITY_none;
7026
7027   fp = &new_field->field;
7028
7029   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7030     {
7031       LONGEST offset;
7032
7033       /* Data member other than a C++ static data member.  */
7034
7035       /* Get type of field.  */
7036       fp->type = die_type (die, cu);
7037
7038       SET_FIELD_BITPOS (*fp, 0);
7039
7040       /* Get bit size of field (zero if none).  */
7041       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7042       if (attr)
7043         {
7044           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7045         }
7046       else
7047         {
7048           FIELD_BITSIZE (*fp) = 0;
7049         }
7050
7051       /* Get bit offset of field.  */
7052       if (handle_data_member_location (die, cu, &offset))
7053         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7054       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7055       if (attr)
7056         {
7057           if (gdbarch_bits_big_endian (gdbarch))
7058             {
7059               /* For big endian bits, the DW_AT_bit_offset gives the
7060                  additional bit offset from the MSB of the containing
7061                  anonymous object to the MSB of the field.  We don't
7062                  have to do anything special since we don't need to
7063                  know the size of the anonymous object.  */
7064               FIELD_BITPOS (*fp) += DW_UNSND (attr);
7065             }
7066           else
7067             {
7068               /* For little endian bits, compute the bit offset to the
7069                  MSB of the anonymous object, subtract off the number of
7070                  bits from the MSB of the field to the MSB of the
7071                  object, and then subtract off the number of bits of
7072                  the field itself.  The result is the bit offset of
7073                  the LSB of the field.  */
7074               int anonymous_size;
7075               int bit_offset = DW_UNSND (attr);
7076
7077               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7078               if (attr)
7079                 {
7080                   /* The size of the anonymous object containing
7081                      the bit field is explicit, so use the
7082                      indicated size (in bytes).  */
7083                   anonymous_size = DW_UNSND (attr);
7084                 }
7085               else
7086                 {
7087                   /* The size of the anonymous object containing
7088                      the bit field must be inferred from the type
7089                      attribute of the data member containing the
7090                      bit field.  */
7091                   anonymous_size = TYPE_LENGTH (fp->type);
7092                 }
7093               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7094                 - bit_offset - FIELD_BITSIZE (*fp);
7095             }
7096         }
7097
7098       /* Get name of field.  */
7099       fieldname = dwarf2_name (die, cu);
7100       if (fieldname == NULL)
7101         fieldname = "";
7102
7103       /* The name is already allocated along with this objfile, so we don't
7104          need to duplicate it for the type.  */
7105       fp->name = fieldname;
7106
7107       /* Change accessibility for artificial fields (e.g. virtual table
7108          pointer or virtual base class pointer) to private.  */
7109       if (dwarf2_attr (die, DW_AT_artificial, cu))
7110         {
7111           FIELD_ARTIFICIAL (*fp) = 1;
7112           new_field->accessibility = DW_ACCESS_private;
7113           fip->non_public_fields = 1;
7114         }
7115     }
7116   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7117     {
7118       /* C++ static member.  */
7119
7120       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7121          is a declaration, but all versions of G++ as of this writing
7122          (so through at least 3.2.1) incorrectly generate
7123          DW_TAG_variable tags.  */
7124
7125       const char *physname;
7126
7127       /* Get name of field.  */
7128       fieldname = dwarf2_name (die, cu);
7129       if (fieldname == NULL)
7130         return;
7131
7132       attr = dwarf2_attr (die, DW_AT_const_value, cu);
7133       if (attr
7134           /* Only create a symbol if this is an external value.
7135              new_symbol checks this and puts the value in the global symbol
7136              table, which we want.  If it is not external, new_symbol
7137              will try to put the value in cu->list_in_scope which is wrong.  */
7138           && dwarf2_flag_true_p (die, DW_AT_external, cu))
7139         {
7140           /* A static const member, not much different than an enum as far as
7141              we're concerned, except that we can support more types.  */
7142           new_symbol (die, NULL, cu);
7143         }
7144
7145       /* Get physical name.  */
7146       physname = dwarf2_physname (fieldname, die, cu);
7147
7148       /* The name is already allocated along with this objfile, so we don't
7149          need to duplicate it for the type.  */
7150       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7151       FIELD_TYPE (*fp) = die_type (die, cu);
7152       FIELD_NAME (*fp) = fieldname;
7153     }
7154   else if (die->tag == DW_TAG_inheritance)
7155     {
7156       LONGEST offset;
7157
7158       /* C++ base class field.  */
7159       if (handle_data_member_location (die, cu, &offset))
7160         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7161       FIELD_BITSIZE (*fp) = 0;
7162       FIELD_TYPE (*fp) = die_type (die, cu);
7163       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7164       fip->nbaseclasses++;
7165     }
7166 }
7167
7168 /* Add a typedef defined in the scope of the FIP's class.  */
7169
7170 static void
7171 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7172                     struct dwarf2_cu *cu)
7173 {
7174   struct objfile *objfile = cu->objfile;
7175   struct typedef_field_list *new_field;
7176   struct attribute *attr;
7177   struct typedef_field *fp;
7178   char *fieldname = "";
7179
7180   /* Allocate a new field list entry and link it in.  */
7181   new_field = xzalloc (sizeof (*new_field));
7182   make_cleanup (xfree, new_field);
7183
7184   gdb_assert (die->tag == DW_TAG_typedef);
7185
7186   fp = &new_field->field;
7187
7188   /* Get name of field.  */
7189   fp->name = dwarf2_name (die, cu);
7190   if (fp->name == NULL)
7191     return;
7192
7193   fp->type = read_type_die (die, cu);
7194
7195   new_field->next = fip->typedef_field_list;
7196   fip->typedef_field_list = new_field;
7197   fip->typedef_field_list_count++;
7198 }
7199
7200 /* Create the vector of fields, and attach it to the type.  */
7201
7202 static void
7203 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7204                               struct dwarf2_cu *cu)
7205 {
7206   int nfields = fip->nfields;
7207
7208   /* Record the field count, allocate space for the array of fields,
7209      and create blank accessibility bitfields if necessary.  */
7210   TYPE_NFIELDS (type) = nfields;
7211   TYPE_FIELDS (type) = (struct field *)
7212     TYPE_ALLOC (type, sizeof (struct field) * nfields);
7213   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7214
7215   if (fip->non_public_fields && cu->language != language_ada)
7216     {
7217       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7218
7219       TYPE_FIELD_PRIVATE_BITS (type) =
7220         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7221       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7222
7223       TYPE_FIELD_PROTECTED_BITS (type) =
7224         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7225       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7226
7227       TYPE_FIELD_IGNORE_BITS (type) =
7228         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7229       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7230     }
7231
7232   /* If the type has baseclasses, allocate and clear a bit vector for
7233      TYPE_FIELD_VIRTUAL_BITS.  */
7234   if (fip->nbaseclasses && cu->language != language_ada)
7235     {
7236       int num_bytes = B_BYTES (fip->nbaseclasses);
7237       unsigned char *pointer;
7238
7239       ALLOCATE_CPLUS_STRUCT_TYPE (type);
7240       pointer = TYPE_ALLOC (type, num_bytes);
7241       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7242       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7243       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7244     }
7245
7246   /* Copy the saved-up fields into the field vector.  Start from the head of
7247      the list, adding to the tail of the field array, so that they end up in
7248      the same order in the array in which they were added to the list.  */
7249   while (nfields-- > 0)
7250     {
7251       struct nextfield *fieldp;
7252
7253       if (fip->fields)
7254         {
7255           fieldp = fip->fields;
7256           fip->fields = fieldp->next;
7257         }
7258       else
7259         {
7260           fieldp = fip->baseclasses;
7261           fip->baseclasses = fieldp->next;
7262         }
7263
7264       TYPE_FIELD (type, nfields) = fieldp->field;
7265       switch (fieldp->accessibility)
7266         {
7267         case DW_ACCESS_private:
7268           if (cu->language != language_ada)
7269             SET_TYPE_FIELD_PRIVATE (type, nfields);
7270           break;
7271
7272         case DW_ACCESS_protected:
7273           if (cu->language != language_ada)
7274             SET_TYPE_FIELD_PROTECTED (type, nfields);
7275           break;
7276
7277         case DW_ACCESS_public:
7278           break;
7279
7280         default:
7281           /* Unknown accessibility.  Complain and treat it as public.  */
7282           {
7283             complaint (&symfile_complaints, _("unsupported accessibility %d"),
7284                        fieldp->accessibility);
7285           }
7286           break;
7287         }
7288       if (nfields < fip->nbaseclasses)
7289         {
7290           switch (fieldp->virtuality)
7291             {
7292             case DW_VIRTUALITY_virtual:
7293             case DW_VIRTUALITY_pure_virtual:
7294               if (cu->language == language_ada)
7295                 error (_("unexpected virtuality in component of Ada type"));
7296               SET_TYPE_FIELD_VIRTUAL (type, nfields);
7297               break;
7298             }
7299         }
7300     }
7301 }
7302
7303 /* Add a member function to the proper fieldlist.  */
7304
7305 static void
7306 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7307                       struct type *type, struct dwarf2_cu *cu)
7308 {
7309   struct objfile *objfile = cu->objfile;
7310   struct attribute *attr;
7311   struct fnfieldlist *flp;
7312   int i;
7313   struct fn_field *fnp;
7314   char *fieldname;
7315   struct nextfnfield *new_fnfield;
7316   struct type *this_type;
7317   enum dwarf_access_attribute accessibility;
7318
7319   if (cu->language == language_ada)
7320     error (_("unexpected member function in Ada type"));
7321
7322   /* Get name of member function.  */
7323   fieldname = dwarf2_name (die, cu);
7324   if (fieldname == NULL)
7325     return;
7326
7327   /* Look up member function name in fieldlist.  */
7328   for (i = 0; i < fip->nfnfields; i++)
7329     {
7330       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7331         break;
7332     }
7333
7334   /* Create new list element if necessary.  */
7335   if (i < fip->nfnfields)
7336     flp = &fip->fnfieldlists[i];
7337   else
7338     {
7339       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7340         {
7341           fip->fnfieldlists = (struct fnfieldlist *)
7342             xrealloc (fip->fnfieldlists,
7343                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7344                       * sizeof (struct fnfieldlist));
7345           if (fip->nfnfields == 0)
7346             make_cleanup (free_current_contents, &fip->fnfieldlists);
7347         }
7348       flp = &fip->fnfieldlists[fip->nfnfields];
7349       flp->name = fieldname;
7350       flp->length = 0;
7351       flp->head = NULL;
7352       i = fip->nfnfields++;
7353     }
7354
7355   /* Create a new member function field and chain it to the field list
7356      entry.  */
7357   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7358   make_cleanup (xfree, new_fnfield);
7359   memset (new_fnfield, 0, sizeof (struct nextfnfield));
7360   new_fnfield->next = flp->head;
7361   flp->head = new_fnfield;
7362   flp->length++;
7363
7364   /* Fill in the member function field info.  */
7365   fnp = &new_fnfield->fnfield;
7366
7367   /* Delay processing of the physname until later.  */
7368   if (cu->language == language_cplus || cu->language == language_java)
7369     {
7370       add_to_method_list (type, i, flp->length - 1, fieldname,
7371                           die, cu);
7372     }
7373   else
7374     {
7375       const char *physname = dwarf2_physname (fieldname, die, cu);
7376       fnp->physname = physname ? physname : "";
7377     }
7378
7379   fnp->type = alloc_type (objfile);
7380   this_type = read_type_die (die, cu);
7381   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7382     {
7383       int nparams = TYPE_NFIELDS (this_type);
7384
7385       /* TYPE is the domain of this method, and THIS_TYPE is the type
7386            of the method itself (TYPE_CODE_METHOD).  */
7387       smash_to_method_type (fnp->type, type,
7388                             TYPE_TARGET_TYPE (this_type),
7389                             TYPE_FIELDS (this_type),
7390                             TYPE_NFIELDS (this_type),
7391                             TYPE_VARARGS (this_type));
7392
7393       /* Handle static member functions.
7394          Dwarf2 has no clean way to discern C++ static and non-static
7395          member functions.  G++ helps GDB by marking the first
7396          parameter for non-static member functions (which is the this
7397          pointer) as artificial.  We obtain this information from
7398          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
7399       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7400         fnp->voffset = VOFFSET_STATIC;
7401     }
7402   else
7403     complaint (&symfile_complaints, _("member function type missing for '%s'"),
7404                dwarf2_full_name (fieldname, die, cu));
7405
7406   /* Get fcontext from DW_AT_containing_type if present.  */
7407   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7408     fnp->fcontext = die_containing_type (die, cu);
7409
7410   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7411      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
7412
7413   /* Get accessibility.  */
7414   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7415   if (attr)
7416     accessibility = DW_UNSND (attr);
7417   else
7418     accessibility = dwarf2_default_access_attribute (die, cu);
7419   switch (accessibility)
7420     {
7421     case DW_ACCESS_private:
7422       fnp->is_private = 1;
7423       break;
7424     case DW_ACCESS_protected:
7425       fnp->is_protected = 1;
7426       break;
7427     }
7428
7429   /* Check for artificial methods.  */
7430   attr = dwarf2_attr (die, DW_AT_artificial, cu);
7431   if (attr && DW_UNSND (attr) != 0)
7432     fnp->is_artificial = 1;
7433
7434   /* Get index in virtual function table if it is a virtual member
7435      function.  For older versions of GCC, this is an offset in the
7436      appropriate virtual table, as specified by DW_AT_containing_type.
7437      For everyone else, it is an expression to be evaluated relative
7438      to the object address.  */
7439
7440   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7441   if (attr)
7442     {
7443       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7444         {
7445           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7446             {
7447               /* Old-style GCC.  */
7448               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7449             }
7450           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7451                    || (DW_BLOCK (attr)->size > 1
7452                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7453                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7454             {
7455               struct dwarf_block blk;
7456               int offset;
7457
7458               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7459                         ? 1 : 2);
7460               blk.size = DW_BLOCK (attr)->size - offset;
7461               blk.data = DW_BLOCK (attr)->data + offset;
7462               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7463               if ((fnp->voffset % cu->header.addr_size) != 0)
7464                 dwarf2_complex_location_expr_complaint ();
7465               else
7466                 fnp->voffset /= cu->header.addr_size;
7467               fnp->voffset += 2;
7468             }
7469           else
7470             dwarf2_complex_location_expr_complaint ();
7471
7472           if (!fnp->fcontext)
7473             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7474         }
7475       else if (attr_form_is_section_offset (attr))
7476         {
7477           dwarf2_complex_location_expr_complaint ();
7478         }
7479       else
7480         {
7481           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7482                                                  fieldname);
7483         }
7484     }
7485   else
7486     {
7487       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7488       if (attr && DW_UNSND (attr))
7489         {
7490           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
7491           complaint (&symfile_complaints,
7492                      _("Member function \"%s\" (offset %d) is virtual "
7493                        "but the vtable offset is not specified"),
7494                      fieldname, die->offset);
7495           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7496           TYPE_CPLUS_DYNAMIC (type) = 1;
7497         }
7498     }
7499 }
7500
7501 /* Create the vector of member function fields, and attach it to the type.  */
7502
7503 static void
7504 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7505                                  struct dwarf2_cu *cu)
7506 {
7507   struct fnfieldlist *flp;
7508   int i;
7509
7510   if (cu->language == language_ada)
7511     error (_("unexpected member functions in Ada type"));
7512
7513   ALLOCATE_CPLUS_STRUCT_TYPE (type);
7514   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7515     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7516
7517   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7518     {
7519       struct nextfnfield *nfp = flp->head;
7520       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7521       int k;
7522
7523       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7524       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7525       fn_flp->fn_fields = (struct fn_field *)
7526         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7527       for (k = flp->length; (k--, nfp); nfp = nfp->next)
7528         fn_flp->fn_fields[k] = nfp->fnfield;
7529     }
7530
7531   TYPE_NFN_FIELDS (type) = fip->nfnfields;
7532 }
7533
7534 /* Returns non-zero if NAME is the name of a vtable member in CU's
7535    language, zero otherwise.  */
7536 static int
7537 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7538 {
7539   static const char vptr[] = "_vptr";
7540   static const char vtable[] = "vtable";
7541
7542   /* Look for the C++ and Java forms of the vtable.  */
7543   if ((cu->language == language_java
7544        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7545        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7546        && is_cplus_marker (name[sizeof (vptr) - 1])))
7547     return 1;
7548
7549   return 0;
7550 }
7551
7552 /* GCC outputs unnamed structures that are really pointers to member
7553    functions, with the ABI-specified layout.  If TYPE describes
7554    such a structure, smash it into a member function type.
7555
7556    GCC shouldn't do this; it should just output pointer to member DIEs.
7557    This is GCC PR debug/28767.  */
7558
7559 static void
7560 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7561 {
7562   struct type *pfn_type, *domain_type, *new_type;
7563
7564   /* Check for a structure with no name and two children.  */
7565   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7566     return;
7567
7568   /* Check for __pfn and __delta members.  */
7569   if (TYPE_FIELD_NAME (type, 0) == NULL
7570       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7571       || TYPE_FIELD_NAME (type, 1) == NULL
7572       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7573     return;
7574
7575   /* Find the type of the method.  */
7576   pfn_type = TYPE_FIELD_TYPE (type, 0);
7577   if (pfn_type == NULL
7578       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7579       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7580     return;
7581
7582   /* Look for the "this" argument.  */
7583   pfn_type = TYPE_TARGET_TYPE (pfn_type);
7584   if (TYPE_NFIELDS (pfn_type) == 0
7585       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7586       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7587     return;
7588
7589   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7590   new_type = alloc_type (objfile);
7591   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7592                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7593                         TYPE_VARARGS (pfn_type));
7594   smash_to_methodptr_type (type, new_type);
7595 }
7596
7597 /* Called when we find the DIE that starts a structure or union scope
7598    (definition) to create a type for the structure or union.  Fill in
7599    the type's name and general properties; the members will not be
7600    processed until process_structure_type.
7601
7602    NOTE: we need to call these functions regardless of whether or not the
7603    DIE has a DW_AT_name attribute, since it might be an anonymous
7604    structure or union.  This gets the type entered into our set of
7605    user defined types.
7606
7607    However, if the structure is incomplete (an opaque struct/union)
7608    then suppress creating a symbol table entry for it since gdb only
7609    wants to find the one with the complete definition.  Note that if
7610    it is complete, we just call new_symbol, which does it's own
7611    checking about whether the struct/union is anonymous or not (and
7612    suppresses creating a symbol table entry itself).  */
7613
7614 static struct type *
7615 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7616 {
7617   struct objfile *objfile = cu->objfile;
7618   struct type *type;
7619   struct attribute *attr;
7620   char *name;
7621
7622   /* If the definition of this type lives in .debug_types, read that type.
7623      Don't follow DW_AT_specification though, that will take us back up
7624      the chain and we want to go down.  */
7625   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7626   if (attr)
7627     {
7628       struct dwarf2_cu *type_cu = cu;
7629       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7630
7631       /* We could just recurse on read_structure_type, but we need to call
7632          get_die_type to ensure only one type for this DIE is created.
7633          This is important, for example, because for c++ classes we need
7634          TYPE_NAME set which is only done by new_symbol.  Blech.  */
7635       type = read_type_die (type_die, type_cu);
7636
7637       /* TYPE_CU may not be the same as CU.
7638          Ensure TYPE is recorded in CU's type_hash table.  */
7639       return set_die_type (die, type, cu);
7640     }
7641
7642   type = alloc_type (objfile);
7643   INIT_CPLUS_SPECIFIC (type);
7644
7645   name = dwarf2_name (die, cu);
7646   if (name != NULL)
7647     {
7648       if (cu->language == language_cplus
7649           || cu->language == language_java)
7650         {
7651           char *full_name = (char *) dwarf2_full_name (name, die, cu);
7652
7653           /* dwarf2_full_name might have already finished building the DIE's
7654              type.  If so, there is no need to continue.  */
7655           if (get_die_type (die, cu) != NULL)
7656             return get_die_type (die, cu);
7657
7658           TYPE_TAG_NAME (type) = full_name;
7659           if (die->tag == DW_TAG_structure_type
7660               || die->tag == DW_TAG_class_type)
7661             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7662         }
7663       else
7664         {
7665           /* The name is already allocated along with this objfile, so
7666              we don't need to duplicate it for the type.  */
7667           TYPE_TAG_NAME (type) = (char *) name;
7668           if (die->tag == DW_TAG_class_type)
7669             TYPE_NAME (type) = TYPE_TAG_NAME (type);
7670         }
7671     }
7672
7673   if (die->tag == DW_TAG_structure_type)
7674     {
7675       TYPE_CODE (type) = TYPE_CODE_STRUCT;
7676     }
7677   else if (die->tag == DW_TAG_union_type)
7678     {
7679       TYPE_CODE (type) = TYPE_CODE_UNION;
7680     }
7681   else
7682     {
7683       TYPE_CODE (type) = TYPE_CODE_CLASS;
7684     }
7685
7686   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7687     TYPE_DECLARED_CLASS (type) = 1;
7688
7689   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7690   if (attr)
7691     {
7692       TYPE_LENGTH (type) = DW_UNSND (attr);
7693     }
7694   else
7695     {
7696       TYPE_LENGTH (type) = 0;
7697     }
7698
7699   TYPE_STUB_SUPPORTED (type) = 1;
7700   if (die_is_declaration (die, cu))
7701     TYPE_STUB (type) = 1;
7702   else if (attr == NULL && die->child == NULL
7703            && producer_is_realview (cu->producer))
7704     /* RealView does not output the required DW_AT_declaration
7705        on incomplete types.  */
7706     TYPE_STUB (type) = 1;
7707
7708   /* We need to add the type field to the die immediately so we don't
7709      infinitely recurse when dealing with pointers to the structure
7710      type within the structure itself.  */
7711   set_die_type (die, type, cu);
7712
7713   /* set_die_type should be already done.  */
7714   set_descriptive_type (type, die, cu);
7715
7716   return type;
7717 }
7718
7719 /* Finish creating a structure or union type, including filling in
7720    its members and creating a symbol for it.  */
7721
7722 static void
7723 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7724 {
7725   struct objfile *objfile = cu->objfile;
7726   struct die_info *child_die = die->child;
7727   struct type *type;
7728
7729   type = get_die_type (die, cu);
7730   if (type == NULL)
7731     type = read_structure_type (die, cu);
7732
7733   if (die->child != NULL && ! die_is_declaration (die, cu))
7734     {
7735       struct field_info fi;
7736       struct die_info *child_die;
7737       VEC (symbolp) *template_args = NULL;
7738       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7739
7740       memset (&fi, 0, sizeof (struct field_info));
7741
7742       child_die = die->child;
7743
7744       while (child_die && child_die->tag)
7745         {
7746           if (child_die->tag == DW_TAG_member
7747               || child_die->tag == DW_TAG_variable)
7748             {
7749               /* NOTE: carlton/2002-11-05: A C++ static data member
7750                  should be a DW_TAG_member that is a declaration, but
7751                  all versions of G++ as of this writing (so through at
7752                  least 3.2.1) incorrectly generate DW_TAG_variable
7753                  tags for them instead.  */
7754               dwarf2_add_field (&fi, child_die, cu);
7755             }
7756           else if (child_die->tag == DW_TAG_subprogram)
7757             {
7758               /* C++ member function.  */
7759               dwarf2_add_member_fn (&fi, child_die, type, cu);
7760             }
7761           else if (child_die->tag == DW_TAG_inheritance)
7762             {
7763               /* C++ base class field.  */
7764               dwarf2_add_field (&fi, child_die, cu);
7765             }
7766           else if (child_die->tag == DW_TAG_typedef)
7767             dwarf2_add_typedef (&fi, child_die, cu);
7768           else if (child_die->tag == DW_TAG_template_type_param
7769                    || child_die->tag == DW_TAG_template_value_param)
7770             {
7771               struct symbol *arg = new_symbol (child_die, NULL, cu);
7772
7773               if (arg != NULL)
7774                 VEC_safe_push (symbolp, template_args, arg);
7775             }
7776
7777           child_die = sibling_die (child_die);
7778         }
7779
7780       /* Attach template arguments to type.  */
7781       if (! VEC_empty (symbolp, template_args))
7782         {
7783           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7784           TYPE_N_TEMPLATE_ARGUMENTS (type)
7785             = VEC_length (symbolp, template_args);
7786           TYPE_TEMPLATE_ARGUMENTS (type)
7787             = obstack_alloc (&objfile->objfile_obstack,
7788                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
7789                               * sizeof (struct symbol *)));
7790           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7791                   VEC_address (symbolp, template_args),
7792                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
7793                    * sizeof (struct symbol *)));
7794           VEC_free (symbolp, template_args);
7795         }
7796
7797       /* Attach fields and member functions to the type.  */
7798       if (fi.nfields)
7799         dwarf2_attach_fields_to_type (&fi, type, cu);
7800       if (fi.nfnfields)
7801         {
7802           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7803
7804           /* Get the type which refers to the base class (possibly this
7805              class itself) which contains the vtable pointer for the current
7806              class from the DW_AT_containing_type attribute.  This use of
7807              DW_AT_containing_type is a GNU extension.  */
7808
7809           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7810             {
7811               struct type *t = die_containing_type (die, cu);
7812
7813               TYPE_VPTR_BASETYPE (type) = t;
7814               if (type == t)
7815                 {
7816                   int i;
7817
7818                   /* Our own class provides vtbl ptr.  */
7819                   for (i = TYPE_NFIELDS (t) - 1;
7820                        i >= TYPE_N_BASECLASSES (t);
7821                        --i)
7822                     {
7823                       const char *fieldname = TYPE_FIELD_NAME (t, i);
7824
7825                       if (is_vtable_name (fieldname, cu))
7826                         {
7827                           TYPE_VPTR_FIELDNO (type) = i;
7828                           break;
7829                         }
7830                     }
7831
7832                   /* Complain if virtual function table field not found.  */
7833                   if (i < TYPE_N_BASECLASSES (t))
7834                     complaint (&symfile_complaints,
7835                                _("virtual function table pointer "
7836                                  "not found when defining class '%s'"),
7837                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7838                                "");
7839                 }
7840               else
7841                 {
7842                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7843                 }
7844             }
7845           else if (cu->producer
7846                    && strncmp (cu->producer,
7847                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7848             {
7849               /* The IBM XLC compiler does not provide direct indication
7850                  of the containing type, but the vtable pointer is
7851                  always named __vfp.  */
7852
7853               int i;
7854
7855               for (i = TYPE_NFIELDS (type) - 1;
7856                    i >= TYPE_N_BASECLASSES (type);
7857                    --i)
7858                 {
7859                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7860                     {
7861                       TYPE_VPTR_FIELDNO (type) = i;
7862                       TYPE_VPTR_BASETYPE (type) = type;
7863                       break;
7864                     }
7865                 }
7866             }
7867         }
7868
7869       /* Copy fi.typedef_field_list linked list elements content into the
7870          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
7871       if (fi.typedef_field_list)
7872         {
7873           int i = fi.typedef_field_list_count;
7874
7875           ALLOCATE_CPLUS_STRUCT_TYPE (type);
7876           TYPE_TYPEDEF_FIELD_ARRAY (type)
7877             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7878           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7879
7880           /* Reverse the list order to keep the debug info elements order.  */
7881           while (--i >= 0)
7882             {
7883               struct typedef_field *dest, *src;
7884
7885               dest = &TYPE_TYPEDEF_FIELD (type, i);
7886               src = &fi.typedef_field_list->field;
7887               fi.typedef_field_list = fi.typedef_field_list->next;
7888               *dest = *src;
7889             }
7890         }
7891
7892       do_cleanups (back_to);
7893
7894       if (HAVE_CPLUS_STRUCT (type))
7895         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7896     }
7897
7898   quirk_gcc_member_function_pointer (type, objfile);
7899
7900   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7901      snapshots) has been known to create a die giving a declaration
7902      for a class that has, as a child, a die giving a definition for a
7903      nested class.  So we have to process our children even if the
7904      current die is a declaration.  Normally, of course, a declaration
7905      won't have any children at all.  */
7906
7907   while (child_die != NULL && child_die->tag)
7908     {
7909       if (child_die->tag == DW_TAG_member
7910           || child_die->tag == DW_TAG_variable
7911           || child_die->tag == DW_TAG_inheritance
7912           || child_die->tag == DW_TAG_template_value_param
7913           || child_die->tag == DW_TAG_template_type_param)
7914         {
7915           /* Do nothing.  */
7916         }
7917       else
7918         process_die (child_die, cu);
7919
7920       child_die = sibling_die (child_die);
7921     }
7922
7923   /* Do not consider external references.  According to the DWARF standard,
7924      these DIEs are identified by the fact that they have no byte_size
7925      attribute, and a declaration attribute.  */
7926   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7927       || !die_is_declaration (die, cu))
7928     new_symbol (die, type, cu);
7929 }
7930
7931 /* Given a DW_AT_enumeration_type die, set its type.  We do not
7932    complete the type's fields yet, or create any symbols.  */
7933
7934 static struct type *
7935 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7936 {
7937   struct objfile *objfile = cu->objfile;
7938   struct type *type;
7939   struct attribute *attr;
7940   const char *name;
7941
7942   /* If the definition of this type lives in .debug_types, read that type.
7943      Don't follow DW_AT_specification though, that will take us back up
7944      the chain and we want to go down.  */
7945   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7946   if (attr)
7947     {
7948       struct dwarf2_cu *type_cu = cu;
7949       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7950
7951       type = read_type_die (type_die, type_cu);
7952
7953       /* TYPE_CU may not be the same as CU.
7954          Ensure TYPE is recorded in CU's type_hash table.  */
7955       return set_die_type (die, type, cu);
7956     }
7957
7958   type = alloc_type (objfile);
7959
7960   TYPE_CODE (type) = TYPE_CODE_ENUM;
7961   name = dwarf2_full_name (NULL, die, cu);
7962   if (name != NULL)
7963     TYPE_TAG_NAME (type) = (char *) name;
7964
7965   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7966   if (attr)
7967     {
7968       TYPE_LENGTH (type) = DW_UNSND (attr);
7969     }
7970   else
7971     {
7972       TYPE_LENGTH (type) = 0;
7973     }
7974
7975   /* The enumeration DIE can be incomplete.  In Ada, any type can be
7976      declared as private in the package spec, and then defined only
7977      inside the package body.  Such types are known as Taft Amendment
7978      Types.  When another package uses such a type, an incomplete DIE
7979      may be generated by the compiler.  */
7980   if (die_is_declaration (die, cu))
7981     TYPE_STUB (type) = 1;
7982
7983   return set_die_type (die, type, cu);
7984 }
7985
7986 /* Given a pointer to a die which begins an enumeration, process all
7987    the dies that define the members of the enumeration, and create the
7988    symbol for the enumeration type.
7989
7990    NOTE: We reverse the order of the element list.  */
7991
7992 static void
7993 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7994 {
7995   struct type *this_type;
7996
7997   this_type = get_die_type (die, cu);
7998   if (this_type == NULL)
7999     this_type = read_enumeration_type (die, cu);
8000
8001   if (die->child != NULL)
8002     {
8003       struct die_info *child_die;
8004       struct symbol *sym;
8005       struct field *fields = NULL;
8006       int num_fields = 0;
8007       int unsigned_enum = 1;
8008       char *name;
8009       int flag_enum = 1;
8010       ULONGEST mask = 0;
8011
8012       child_die = die->child;
8013       while (child_die && child_die->tag)
8014         {
8015           if (child_die->tag != DW_TAG_enumerator)
8016             {
8017               process_die (child_die, cu);
8018             }
8019           else
8020             {
8021               name = dwarf2_name (child_die, cu);
8022               if (name)
8023                 {
8024                   sym = new_symbol (child_die, this_type, cu);
8025                   if (SYMBOL_VALUE (sym) < 0)
8026                     {
8027                       unsigned_enum = 0;
8028                       flag_enum = 0;
8029                     }
8030                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
8031                     flag_enum = 0;
8032                   else
8033                     mask |= SYMBOL_VALUE (sym);
8034
8035                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8036                     {
8037                       fields = (struct field *)
8038                         xrealloc (fields,
8039                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
8040                                   * sizeof (struct field));
8041                     }
8042
8043                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8044                   FIELD_TYPE (fields[num_fields]) = NULL;
8045                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8046                   FIELD_BITSIZE (fields[num_fields]) = 0;
8047
8048                   num_fields++;
8049                 }
8050             }
8051
8052           child_die = sibling_die (child_die);
8053         }
8054
8055       if (num_fields)
8056         {
8057           TYPE_NFIELDS (this_type) = num_fields;
8058           TYPE_FIELDS (this_type) = (struct field *)
8059             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8060           memcpy (TYPE_FIELDS (this_type), fields,
8061                   sizeof (struct field) * num_fields);
8062           xfree (fields);
8063         }
8064       if (unsigned_enum)
8065         TYPE_UNSIGNED (this_type) = 1;
8066       if (flag_enum)
8067         TYPE_FLAG_ENUM (this_type) = 1;
8068     }
8069
8070   /* If we are reading an enum from a .debug_types unit, and the enum
8071      is a declaration, and the enum is not the signatured type in the
8072      unit, then we do not want to add a symbol for it.  Adding a
8073      symbol would in some cases obscure the true definition of the
8074      enum, giving users an incomplete type when the definition is
8075      actually available.  Note that we do not want to do this for all
8076      enums which are just declarations, because C++0x allows forward
8077      enum declarations.  */
8078   if (cu->per_cu->debug_types_section
8079       && die_is_declaration (die, cu))
8080     {
8081       struct signatured_type *type_sig;
8082
8083       type_sig
8084         = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8085                                             cu->per_cu->debug_types_section,
8086                                             cu->per_cu->offset);
8087       if (type_sig->per_cu.offset + type_sig->type_offset
8088           != die->offset)
8089         return;
8090     }
8091
8092   new_symbol (die, this_type, cu);
8093 }
8094
8095 /* Extract all information from a DW_TAG_array_type DIE and put it in
8096    the DIE's type field.  For now, this only handles one dimensional
8097    arrays.  */
8098
8099 static struct type *
8100 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8101 {
8102   struct objfile *objfile = cu->objfile;
8103   struct die_info *child_die;
8104   struct type *type;
8105   struct type *element_type, *range_type, *index_type;
8106   struct type **range_types = NULL;
8107   struct attribute *attr;
8108   int ndim = 0;
8109   struct cleanup *back_to;
8110   char *name;
8111
8112   element_type = die_type (die, cu);
8113
8114   /* The die_type call above may have already set the type for this DIE.  */
8115   type = get_die_type (die, cu);
8116   if (type)
8117     return type;
8118
8119   /* Irix 6.2 native cc creates array types without children for
8120      arrays with unspecified length.  */
8121   if (die->child == NULL)
8122     {
8123       index_type = objfile_type (objfile)->builtin_int;
8124       range_type = create_range_type (NULL, index_type, 0, -1);
8125       type = create_array_type (NULL, element_type, range_type);
8126       return set_die_type (die, type, cu);
8127     }
8128
8129   back_to = make_cleanup (null_cleanup, NULL);
8130   child_die = die->child;
8131   while (child_die && child_die->tag)
8132     {
8133       if (child_die->tag == DW_TAG_subrange_type)
8134         {
8135           struct type *child_type = read_type_die (child_die, cu);
8136
8137           if (child_type != NULL)
8138             {
8139               /* The range type was succesfully read.  Save it for the
8140                  array type creation.  */
8141               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8142                 {
8143                   range_types = (struct type **)
8144                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8145                               * sizeof (struct type *));
8146                   if (ndim == 0)
8147                     make_cleanup (free_current_contents, &range_types);
8148                 }
8149               range_types[ndim++] = child_type;
8150             }
8151         }
8152       child_die = sibling_die (child_die);
8153     }
8154
8155   /* Dwarf2 dimensions are output from left to right, create the
8156      necessary array types in backwards order.  */
8157
8158   type = element_type;
8159
8160   if (read_array_order (die, cu) == DW_ORD_col_major)
8161     {
8162       int i = 0;
8163
8164       while (i < ndim)
8165         type = create_array_type (NULL, type, range_types[i++]);
8166     }
8167   else
8168     {
8169       while (ndim-- > 0)
8170         type = create_array_type (NULL, type, range_types[ndim]);
8171     }
8172
8173   /* Understand Dwarf2 support for vector types (like they occur on
8174      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
8175      array type.  This is not part of the Dwarf2/3 standard yet, but a
8176      custom vendor extension.  The main difference between a regular
8177      array and the vector variant is that vectors are passed by value
8178      to functions.  */
8179   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8180   if (attr)
8181     make_vector_type (type);
8182
8183   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
8184      implementation may choose to implement triple vectors using this
8185      attribute.  */
8186   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8187   if (attr)
8188     {
8189       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8190         TYPE_LENGTH (type) = DW_UNSND (attr);
8191       else
8192         complaint (&symfile_complaints,
8193                    _("DW_AT_byte_size for array type smaller "
8194                      "than the total size of elements"));
8195     }
8196
8197   name = dwarf2_name (die, cu);
8198   if (name)
8199     TYPE_NAME (type) = name;
8200
8201   /* Install the type in the die.  */
8202   set_die_type (die, type, cu);
8203
8204   /* set_die_type should be already done.  */
8205   set_descriptive_type (type, die, cu);
8206
8207   do_cleanups (back_to);
8208
8209   return type;
8210 }
8211
8212 static enum dwarf_array_dim_ordering
8213 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8214 {
8215   struct attribute *attr;
8216
8217   attr = dwarf2_attr (die, DW_AT_ordering, cu);
8218
8219   if (attr) return DW_SND (attr);
8220
8221   /* GNU F77 is a special case, as at 08/2004 array type info is the
8222      opposite order to the dwarf2 specification, but data is still
8223      laid out as per normal fortran.
8224
8225      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8226      version checking.  */
8227
8228   if (cu->language == language_fortran
8229       && cu->producer && strstr (cu->producer, "GNU F77"))
8230     {
8231       return DW_ORD_row_major;
8232     }
8233
8234   switch (cu->language_defn->la_array_ordering)
8235     {
8236     case array_column_major:
8237       return DW_ORD_col_major;
8238     case array_row_major:
8239     default:
8240       return DW_ORD_row_major;
8241     };
8242 }
8243
8244 /* Extract all information from a DW_TAG_set_type DIE and put it in
8245    the DIE's type field.  */
8246
8247 static struct type *
8248 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8249 {
8250   struct type *domain_type, *set_type;
8251   struct attribute *attr;
8252
8253   domain_type = die_type (die, cu);
8254
8255   /* The die_type call above may have already set the type for this DIE.  */
8256   set_type = get_die_type (die, cu);
8257   if (set_type)
8258     return set_type;
8259
8260   set_type = create_set_type (NULL, domain_type);
8261
8262   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8263   if (attr)
8264     TYPE_LENGTH (set_type) = DW_UNSND (attr);
8265
8266   return set_die_type (die, set_type, cu);
8267 }
8268
8269 /* First cut: install each common block member as a global variable.  */
8270
8271 static void
8272 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8273 {
8274   struct die_info *child_die;
8275   struct attribute *attr;
8276   struct symbol *sym;
8277   CORE_ADDR base = (CORE_ADDR) 0;
8278
8279   attr = dwarf2_attr (die, DW_AT_location, cu);
8280   if (attr)
8281     {
8282       /* Support the .debug_loc offsets.  */
8283       if (attr_form_is_block (attr))
8284         {
8285           base = decode_locdesc (DW_BLOCK (attr), cu);
8286         }
8287       else if (attr_form_is_section_offset (attr))
8288         {
8289           dwarf2_complex_location_expr_complaint ();
8290         }
8291       else
8292         {
8293           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8294                                                  "common block member");
8295         }
8296     }
8297   if (die->child != NULL)
8298     {
8299       child_die = die->child;
8300       while (child_die && child_die->tag)
8301         {
8302           LONGEST offset;
8303
8304           sym = new_symbol (child_die, NULL, cu);
8305           if (sym != NULL
8306               && handle_data_member_location (child_die, cu, &offset))
8307             {
8308               SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8309               add_symbol_to_list (sym, &global_symbols);
8310             }
8311           child_die = sibling_die (child_die);
8312         }
8313     }
8314 }
8315
8316 /* Create a type for a C++ namespace.  */
8317
8318 static struct type *
8319 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8320 {
8321   struct objfile *objfile = cu->objfile;
8322   const char *previous_prefix, *name;
8323   int is_anonymous;
8324   struct type *type;
8325
8326   /* For extensions, reuse the type of the original namespace.  */
8327   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8328     {
8329       struct die_info *ext_die;
8330       struct dwarf2_cu *ext_cu = cu;
8331
8332       ext_die = dwarf2_extension (die, &ext_cu);
8333       type = read_type_die (ext_die, ext_cu);
8334
8335       /* EXT_CU may not be the same as CU.
8336          Ensure TYPE is recorded in CU's type_hash table.  */
8337       return set_die_type (die, type, cu);
8338     }
8339
8340   name = namespace_name (die, &is_anonymous, cu);
8341
8342   /* Now build the name of the current namespace.  */
8343
8344   previous_prefix = determine_prefix (die, cu);
8345   if (previous_prefix[0] != '\0')
8346     name = typename_concat (&objfile->objfile_obstack,
8347                             previous_prefix, name, 0, cu);
8348
8349   /* Create the type.  */
8350   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8351                     objfile);
8352   TYPE_NAME (type) = (char *) name;
8353   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8354
8355   return set_die_type (die, type, cu);
8356 }
8357
8358 /* Read a C++ namespace.  */
8359
8360 static void
8361 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8362 {
8363   struct objfile *objfile = cu->objfile;
8364   int is_anonymous;
8365
8366   /* Add a symbol associated to this if we haven't seen the namespace
8367      before.  Also, add a using directive if it's an anonymous
8368      namespace.  */
8369
8370   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8371     {
8372       struct type *type;
8373
8374       type = read_type_die (die, cu);
8375       new_symbol (die, type, cu);
8376
8377       namespace_name (die, &is_anonymous, cu);
8378       if (is_anonymous)
8379         {
8380           const char *previous_prefix = determine_prefix (die, cu);
8381
8382           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8383                                   NULL, NULL, &objfile->objfile_obstack);
8384         }
8385     }
8386
8387   if (die->child != NULL)
8388     {
8389       struct die_info *child_die = die->child;
8390
8391       while (child_die && child_die->tag)
8392         {
8393           process_die (child_die, cu);
8394           child_die = sibling_die (child_die);
8395         }
8396     }
8397 }
8398
8399 /* Read a Fortran module as type.  This DIE can be only a declaration used for
8400    imported module.  Still we need that type as local Fortran "use ... only"
8401    declaration imports depend on the created type in determine_prefix.  */
8402
8403 static struct type *
8404 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8405 {
8406   struct objfile *objfile = cu->objfile;
8407   char *module_name;
8408   struct type *type;
8409
8410   module_name = dwarf2_name (die, cu);
8411   if (!module_name)
8412     complaint (&symfile_complaints,
8413                _("DW_TAG_module has no name, offset 0x%x"),
8414                die->offset);
8415   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8416
8417   /* determine_prefix uses TYPE_TAG_NAME.  */
8418   TYPE_TAG_NAME (type) = TYPE_NAME (type);
8419
8420   return set_die_type (die, type, cu);
8421 }
8422
8423 /* Read a Fortran module.  */
8424
8425 static void
8426 read_module (struct die_info *die, struct dwarf2_cu *cu)
8427 {
8428   struct die_info *child_die = die->child;
8429
8430   while (child_die && child_die->tag)
8431     {
8432       process_die (child_die, cu);
8433       child_die = sibling_die (child_die);
8434     }
8435 }
8436
8437 /* Return the name of the namespace represented by DIE.  Set
8438    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8439    namespace.  */
8440
8441 static const char *
8442 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8443 {
8444   struct die_info *current_die;
8445   const char *name = NULL;
8446
8447   /* Loop through the extensions until we find a name.  */
8448
8449   for (current_die = die;
8450        current_die != NULL;
8451        current_die = dwarf2_extension (die, &cu))
8452     {
8453       name = dwarf2_name (current_die, cu);
8454       if (name != NULL)
8455         break;
8456     }
8457
8458   /* Is it an anonymous namespace?  */
8459
8460   *is_anonymous = (name == NULL);
8461   if (*is_anonymous)
8462     name = CP_ANONYMOUS_NAMESPACE_STR;
8463
8464   return name;
8465 }
8466
8467 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8468    the user defined type vector.  */
8469
8470 static struct type *
8471 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8472 {
8473   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8474   struct comp_unit_head *cu_header = &cu->header;
8475   struct type *type;
8476   struct attribute *attr_byte_size;
8477   struct attribute *attr_address_class;
8478   int byte_size, addr_class;
8479   struct type *target_type;
8480
8481   target_type = die_type (die, cu);
8482
8483   /* The die_type call above may have already set the type for this DIE.  */
8484   type = get_die_type (die, cu);
8485   if (type)
8486     return type;
8487
8488   type = lookup_pointer_type (target_type);
8489
8490   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8491   if (attr_byte_size)
8492     byte_size = DW_UNSND (attr_byte_size);
8493   else
8494     byte_size = cu_header->addr_size;
8495
8496   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8497   if (attr_address_class)
8498     addr_class = DW_UNSND (attr_address_class);
8499   else
8500     addr_class = DW_ADDR_none;
8501
8502   /* If the pointer size or address class is different than the
8503      default, create a type variant marked as such and set the
8504      length accordingly.  */
8505   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8506     {
8507       if (gdbarch_address_class_type_flags_p (gdbarch))
8508         {
8509           int type_flags;
8510
8511           type_flags = gdbarch_address_class_type_flags
8512                          (gdbarch, byte_size, addr_class);
8513           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8514                       == 0);
8515           type = make_type_with_address_space (type, type_flags);
8516         }
8517       else if (TYPE_LENGTH (type) != byte_size)
8518         {
8519           complaint (&symfile_complaints,
8520                      _("invalid pointer size %d"), byte_size);
8521         }
8522       else
8523         {
8524           /* Should we also complain about unhandled address classes?  */
8525         }
8526     }
8527
8528   TYPE_LENGTH (type) = byte_size;
8529   return set_die_type (die, type, cu);
8530 }
8531
8532 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8533    the user defined type vector.  */
8534
8535 static struct type *
8536 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8537 {
8538   struct type *type;
8539   struct type *to_type;
8540   struct type *domain;
8541
8542   to_type = die_type (die, cu);
8543   domain = die_containing_type (die, cu);
8544
8545   /* The calls above may have already set the type for this DIE.  */
8546   type = get_die_type (die, cu);
8547   if (type)
8548     return type;
8549
8550   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8551     type = lookup_methodptr_type (to_type);
8552   else
8553     type = lookup_memberptr_type (to_type, domain);
8554
8555   return set_die_type (die, type, cu);
8556 }
8557
8558 /* Extract all information from a DW_TAG_reference_type DIE and add to
8559    the user defined type vector.  */
8560
8561 static struct type *
8562 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8563 {
8564   struct comp_unit_head *cu_header = &cu->header;
8565   struct type *type, *target_type;
8566   struct attribute *attr;
8567
8568   target_type = die_type (die, cu);
8569
8570   /* The die_type call above may have already set the type for this DIE.  */
8571   type = get_die_type (die, cu);
8572   if (type)
8573     return type;
8574
8575   type = lookup_reference_type (target_type);
8576   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8577   if (attr)
8578     {
8579       TYPE_LENGTH (type) = DW_UNSND (attr);
8580     }
8581   else
8582     {
8583       TYPE_LENGTH (type) = cu_header->addr_size;
8584     }
8585   return set_die_type (die, type, cu);
8586 }
8587
8588 static struct type *
8589 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8590 {
8591   struct type *base_type, *cv_type;
8592
8593   base_type = die_type (die, cu);
8594
8595   /* The die_type call above may have already set the type for this DIE.  */
8596   cv_type = get_die_type (die, cu);
8597   if (cv_type)
8598     return cv_type;
8599
8600   /* In case the const qualifier is applied to an array type, the element type
8601      is so qualified, not the array type (section 6.7.3 of C99).  */
8602   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8603     {
8604       struct type *el_type, *inner_array;
8605
8606       base_type = copy_type (base_type);
8607       inner_array = base_type;
8608
8609       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8610         {
8611           TYPE_TARGET_TYPE (inner_array) =
8612             copy_type (TYPE_TARGET_TYPE (inner_array));
8613           inner_array = TYPE_TARGET_TYPE (inner_array);
8614         }
8615
8616       el_type = TYPE_TARGET_TYPE (inner_array);
8617       TYPE_TARGET_TYPE (inner_array) =
8618         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8619
8620       return set_die_type (die, base_type, cu);
8621     }
8622
8623   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8624   return set_die_type (die, cv_type, cu);
8625 }
8626
8627 static struct type *
8628 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8629 {
8630   struct type *base_type, *cv_type;
8631
8632   base_type = die_type (die, cu);
8633
8634   /* The die_type call above may have already set the type for this DIE.  */
8635   cv_type = get_die_type (die, cu);
8636   if (cv_type)
8637     return cv_type;
8638
8639   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8640   return set_die_type (die, cv_type, cu);
8641 }
8642
8643 /* Extract all information from a DW_TAG_string_type DIE and add to
8644    the user defined type vector.  It isn't really a user defined type,
8645    but it behaves like one, with other DIE's using an AT_user_def_type
8646    attribute to reference it.  */
8647
8648 static struct type *
8649 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8650 {
8651   struct objfile *objfile = cu->objfile;
8652   struct gdbarch *gdbarch = get_objfile_arch (objfile);
8653   struct type *type, *range_type, *index_type, *char_type;
8654   struct attribute *attr;
8655   unsigned int length;
8656
8657   attr = dwarf2_attr (die, DW_AT_string_length, cu);
8658   if (attr)
8659     {
8660       length = DW_UNSND (attr);
8661     }
8662   else
8663     {
8664       /* Check for the DW_AT_byte_size attribute.  */
8665       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8666       if (attr)
8667         {
8668           length = DW_UNSND (attr);
8669         }
8670       else
8671         {
8672           length = 1;
8673         }
8674     }
8675
8676   index_type = objfile_type (objfile)->builtin_int;
8677   range_type = create_range_type (NULL, index_type, 1, length);
8678   char_type = language_string_char_type (cu->language_defn, gdbarch);
8679   type = create_string_type (NULL, char_type, range_type);
8680
8681   return set_die_type (die, type, cu);
8682 }
8683
8684 /* Handle DIES due to C code like:
8685
8686    struct foo
8687    {
8688    int (*funcp)(int a, long l);
8689    int b;
8690    };
8691
8692    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
8693
8694 static struct type *
8695 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8696 {
8697   struct objfile *objfile = cu->objfile;
8698   struct type *type;            /* Type that this function returns.  */
8699   struct type *ftype;           /* Function that returns above type.  */
8700   struct attribute *attr;
8701
8702   type = die_type (die, cu);
8703
8704   /* The die_type call above may have already set the type for this DIE.  */
8705   ftype = get_die_type (die, cu);
8706   if (ftype)
8707     return ftype;
8708
8709   ftype = lookup_function_type (type);
8710
8711   /* All functions in C++, Pascal and Java have prototypes.  */
8712   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8713   if ((attr && (DW_UNSND (attr) != 0))
8714       || cu->language == language_cplus
8715       || cu->language == language_java
8716       || cu->language == language_pascal)
8717     TYPE_PROTOTYPED (ftype) = 1;
8718   else if (producer_is_realview (cu->producer))
8719     /* RealView does not emit DW_AT_prototyped.  We can not
8720        distinguish prototyped and unprototyped functions; default to
8721        prototyped, since that is more common in modern code (and
8722        RealView warns about unprototyped functions).  */
8723     TYPE_PROTOTYPED (ftype) = 1;
8724
8725   /* Store the calling convention in the type if it's available in
8726      the subroutine die.  Otherwise set the calling convention to
8727      the default value DW_CC_normal.  */
8728   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8729   if (attr)
8730     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8731   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8732     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8733   else
8734     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8735
8736   /* We need to add the subroutine type to the die immediately so
8737      we don't infinitely recurse when dealing with parameters
8738      declared as the same subroutine type.  */
8739   set_die_type (die, ftype, cu);
8740
8741   if (die->child != NULL)
8742     {
8743       struct type *void_type = objfile_type (objfile)->builtin_void;
8744       struct die_info *child_die;
8745       int nparams, iparams;
8746
8747       /* Count the number of parameters.
8748          FIXME: GDB currently ignores vararg functions, but knows about
8749          vararg member functions.  */
8750       nparams = 0;
8751       child_die = die->child;
8752       while (child_die && child_die->tag)
8753         {
8754           if (child_die->tag == DW_TAG_formal_parameter)
8755             nparams++;
8756           else if (child_die->tag == DW_TAG_unspecified_parameters)
8757             TYPE_VARARGS (ftype) = 1;
8758           child_die = sibling_die (child_die);
8759         }
8760
8761       /* Allocate storage for parameters and fill them in.  */
8762       TYPE_NFIELDS (ftype) = nparams;
8763       TYPE_FIELDS (ftype) = (struct field *)
8764         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8765
8766       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
8767          even if we error out during the parameters reading below.  */
8768       for (iparams = 0; iparams < nparams; iparams++)
8769         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8770
8771       iparams = 0;
8772       child_die = die->child;
8773       while (child_die && child_die->tag)
8774         {
8775           if (child_die->tag == DW_TAG_formal_parameter)
8776             {
8777               struct type *arg_type;
8778
8779               /* DWARF version 2 has no clean way to discern C++
8780                  static and non-static member functions.  G++ helps
8781                  GDB by marking the first parameter for non-static
8782                  member functions (which is the this pointer) as
8783                  artificial.  We pass this information to
8784                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8785
8786                  DWARF version 3 added DW_AT_object_pointer, which GCC
8787                  4.5 does not yet generate.  */
8788               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8789               if (attr)
8790                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8791               else
8792                 {
8793                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8794
8795                   /* GCC/43521: In java, the formal parameter
8796                      "this" is sometimes not marked with DW_AT_artificial.  */
8797                   if (cu->language == language_java)
8798                     {
8799                       const char *name = dwarf2_name (child_die, cu);
8800
8801                       if (name && !strcmp (name, "this"))
8802                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8803                     }
8804                 }
8805               arg_type = die_type (child_die, cu);
8806
8807               /* RealView does not mark THIS as const, which the testsuite
8808                  expects.  GCC marks THIS as const in method definitions,
8809                  but not in the class specifications (GCC PR 43053).  */
8810               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8811                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8812                 {
8813                   int is_this = 0;
8814                   struct dwarf2_cu *arg_cu = cu;
8815                   const char *name = dwarf2_name (child_die, cu);
8816
8817                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8818                   if (attr)
8819                     {
8820                       /* If the compiler emits this, use it.  */
8821                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
8822                         is_this = 1;
8823                     }
8824                   else if (name && strcmp (name, "this") == 0)
8825                     /* Function definitions will have the argument names.  */
8826                     is_this = 1;
8827                   else if (name == NULL && iparams == 0)
8828                     /* Declarations may not have the names, so like
8829                        elsewhere in GDB, assume an artificial first
8830                        argument is "this".  */
8831                     is_this = 1;
8832
8833                   if (is_this)
8834                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8835                                              arg_type, 0);
8836                 }
8837
8838               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8839               iparams++;
8840             }
8841           child_die = sibling_die (child_die);
8842         }
8843     }
8844
8845   return ftype;
8846 }
8847
8848 static struct type *
8849 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8850 {
8851   struct objfile *objfile = cu->objfile;
8852   const char *name = NULL;
8853   struct type *this_type, *target_type;
8854
8855   name = dwarf2_full_name (NULL, die, cu);
8856   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8857                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
8858   TYPE_NAME (this_type) = (char *) name;
8859   set_die_type (die, this_type, cu);
8860   target_type = die_type (die, cu);
8861   if (target_type != this_type)
8862     TYPE_TARGET_TYPE (this_type) = target_type;
8863   else
8864     {
8865       /* Self-referential typedefs are, it seems, not allowed by the DWARF
8866          spec and cause infinite loops in GDB.  */
8867       complaint (&symfile_complaints,
8868                  _("Self-referential DW_TAG_typedef "
8869                    "- DIE at 0x%x [in module %s]"),
8870                  die->offset, objfile->name);
8871       TYPE_TARGET_TYPE (this_type) = NULL;
8872     }
8873   return this_type;
8874 }
8875
8876 /* Find a representation of a given base type and install
8877    it in the TYPE field of the die.  */
8878
8879 static struct type *
8880 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8881 {
8882   struct objfile *objfile = cu->objfile;
8883   struct type *type;
8884   struct attribute *attr;
8885   int encoding = 0, size = 0;
8886   char *name;
8887   enum type_code code = TYPE_CODE_INT;
8888   int type_flags = 0;
8889   struct type *target_type = NULL;
8890
8891   attr = dwarf2_attr (die, DW_AT_encoding, cu);
8892   if (attr)
8893     {
8894       encoding = DW_UNSND (attr);
8895     }
8896   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8897   if (attr)
8898     {
8899       size = DW_UNSND (attr);
8900     }
8901   name = dwarf2_name (die, cu);
8902   if (!name)
8903     {
8904       complaint (&symfile_complaints,
8905                  _("DW_AT_name missing from DW_TAG_base_type"));
8906     }
8907
8908   switch (encoding)
8909     {
8910       case DW_ATE_address:
8911         /* Turn DW_ATE_address into a void * pointer.  */
8912         code = TYPE_CODE_PTR;
8913         type_flags |= TYPE_FLAG_UNSIGNED;
8914         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8915         break;
8916       case DW_ATE_boolean:
8917         code = TYPE_CODE_BOOL;
8918         type_flags |= TYPE_FLAG_UNSIGNED;
8919         break;
8920       case DW_ATE_complex_float:
8921         code = TYPE_CODE_COMPLEX;
8922         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8923         break;
8924       case DW_ATE_decimal_float:
8925         code = TYPE_CODE_DECFLOAT;
8926         break;
8927       case DW_ATE_float:
8928         code = TYPE_CODE_FLT;
8929         break;
8930       case DW_ATE_signed:
8931         break;
8932       case DW_ATE_unsigned:
8933         type_flags |= TYPE_FLAG_UNSIGNED;
8934         if (cu->language == language_fortran
8935             && name
8936             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8937           code = TYPE_CODE_CHAR;
8938         break;
8939       case DW_ATE_signed_char:
8940         if (cu->language == language_ada || cu->language == language_m2
8941             || cu->language == language_pascal
8942             || cu->language == language_fortran)
8943           code = TYPE_CODE_CHAR;
8944         break;
8945       case DW_ATE_unsigned_char:
8946         if (cu->language == language_ada || cu->language == language_m2
8947             || cu->language == language_pascal
8948             || cu->language == language_fortran)
8949           code = TYPE_CODE_CHAR;
8950         type_flags |= TYPE_FLAG_UNSIGNED;
8951         break;
8952       case DW_ATE_UTF:
8953         /* We just treat this as an integer and then recognize the
8954            type by name elsewhere.  */
8955         break;
8956
8957       default:
8958         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8959                    dwarf_type_encoding_name (encoding));
8960         break;
8961     }
8962
8963   type = init_type (code, size, type_flags, NULL, objfile);
8964   TYPE_NAME (type) = name;
8965   TYPE_TARGET_TYPE (type) = target_type;
8966
8967   if (name && strcmp (name, "char") == 0)
8968     TYPE_NOSIGN (type) = 1;
8969
8970   return set_die_type (die, type, cu);
8971 }
8972
8973 /* Read the given DW_AT_subrange DIE.  */
8974
8975 static struct type *
8976 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8977 {
8978   struct type *base_type;
8979   struct type *range_type;
8980   struct attribute *attr;
8981   LONGEST low = 0;
8982   LONGEST high = -1;
8983   char *name;
8984   LONGEST negative_mask;
8985
8986   base_type = die_type (die, cu);
8987   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
8988   check_typedef (base_type);
8989
8990   /* The die_type call above may have already set the type for this DIE.  */
8991   range_type = get_die_type (die, cu);
8992   if (range_type)
8993     return range_type;
8994
8995   if (cu->language == language_fortran)
8996     {
8997       /* FORTRAN implies a lower bound of 1, if not given.  */
8998       low = 1;
8999     }
9000
9001   /* FIXME: For variable sized arrays either of these could be
9002      a variable rather than a constant value.  We'll allow it,
9003      but we don't know how to handle it.  */
9004   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
9005   if (attr)
9006     low = dwarf2_get_attr_constant_value (attr, 0);
9007
9008   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
9009   if (attr)
9010     {
9011       if (attr_form_is_block (attr) || is_ref_attr (attr))
9012         {
9013           /* GCC encodes arrays with unspecified or dynamic length
9014              with a DW_FORM_block1 attribute or a reference attribute.
9015              FIXME: GDB does not yet know how to handle dynamic
9016              arrays properly, treat them as arrays with unspecified
9017              length for now.
9018
9019              FIXME: jimb/2003-09-22: GDB does not really know
9020              how to handle arrays of unspecified length
9021              either; we just represent them as zero-length
9022              arrays.  Choose an appropriate upper bound given
9023              the lower bound we've computed above.  */
9024           high = low - 1;
9025         }
9026       else
9027         high = dwarf2_get_attr_constant_value (attr, 1);
9028     }
9029   else
9030     {
9031       attr = dwarf2_attr (die, DW_AT_count, cu);
9032       if (attr)
9033         {
9034           int count = dwarf2_get_attr_constant_value (attr, 1);
9035           high = low + count - 1;
9036         }
9037       else
9038         {
9039           /* Unspecified array length.  */
9040           high = low - 1;
9041         }
9042     }
9043
9044   /* Dwarf-2 specifications explicitly allows to create subrange types
9045      without specifying a base type.
9046      In that case, the base type must be set to the type of
9047      the lower bound, upper bound or count, in that order, if any of these
9048      three attributes references an object that has a type.
9049      If no base type is found, the Dwarf-2 specifications say that
9050      a signed integer type of size equal to the size of an address should
9051      be used.
9052      For the following C code: `extern char gdb_int [];'
9053      GCC produces an empty range DIE.
9054      FIXME: muller/2010-05-28: Possible references to object for low bound,
9055      high bound or count are not yet handled by this code.  */
9056   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9057     {
9058       struct objfile *objfile = cu->objfile;
9059       struct gdbarch *gdbarch = get_objfile_arch (objfile);
9060       int addr_size = gdbarch_addr_bit (gdbarch) /8;
9061       struct type *int_type = objfile_type (objfile)->builtin_int;
9062
9063       /* Test "int", "long int", and "long long int" objfile types,
9064          and select the first one having a size above or equal to the
9065          architecture address size.  */
9066       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9067         base_type = int_type;
9068       else
9069         {
9070           int_type = objfile_type (objfile)->builtin_long;
9071           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9072             base_type = int_type;
9073           else
9074             {
9075               int_type = objfile_type (objfile)->builtin_long_long;
9076               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9077                 base_type = int_type;
9078             }
9079         }
9080     }
9081
9082   negative_mask =
9083     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9084   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9085     low |= negative_mask;
9086   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9087     high |= negative_mask;
9088
9089   range_type = create_range_type (NULL, base_type, low, high);
9090
9091   /* Mark arrays with dynamic length at least as an array of unspecified
9092      length.  GDB could check the boundary but before it gets implemented at
9093      least allow accessing the array elements.  */
9094   if (attr && attr_form_is_block (attr))
9095     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9096
9097   /* Ada expects an empty array on no boundary attributes.  */
9098   if (attr == NULL && cu->language != language_ada)
9099     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9100
9101   name = dwarf2_name (die, cu);
9102   if (name)
9103     TYPE_NAME (range_type) = name;
9104
9105   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9106   if (attr)
9107     TYPE_LENGTH (range_type) = DW_UNSND (attr);
9108
9109   set_die_type (die, range_type, cu);
9110
9111   /* set_die_type should be already done.  */
9112   set_descriptive_type (range_type, die, cu);
9113
9114   return range_type;
9115 }
9116
9117 static struct type *
9118 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9119 {
9120   struct type *type;
9121
9122   /* For now, we only support the C meaning of an unspecified type: void.  */
9123
9124   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9125   TYPE_NAME (type) = dwarf2_name (die, cu);
9126
9127   return set_die_type (die, type, cu);
9128 }
9129
9130 /* Trivial hash function for die_info: the hash value of a DIE
9131    is its offset in .debug_info for this objfile.  */
9132
9133 static hashval_t
9134 die_hash (const void *item)
9135 {
9136   const struct die_info *die = item;
9137
9138   return die->offset;
9139 }
9140
9141 /* Trivial comparison function for die_info structures: two DIEs
9142    are equal if they have the same offset.  */
9143
9144 static int
9145 die_eq (const void *item_lhs, const void *item_rhs)
9146 {
9147   const struct die_info *die_lhs = item_lhs;
9148   const struct die_info *die_rhs = item_rhs;
9149
9150   return die_lhs->offset == die_rhs->offset;
9151 }
9152
9153 /* Read a whole compilation unit into a linked list of dies.  */
9154
9155 static struct die_info *
9156 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9157 {
9158   struct die_reader_specs reader_specs;
9159   int read_abbrevs = 0;
9160   struct cleanup *back_to = NULL;
9161   struct die_info *die;
9162
9163   if (cu->dwarf2_abbrevs == NULL)
9164     {
9165       dwarf2_read_abbrevs (cu);
9166       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9167       read_abbrevs = 1;
9168     }
9169
9170   gdb_assert (cu->die_hash == NULL);
9171   cu->die_hash
9172     = htab_create_alloc_ex (cu->header.length / 12,
9173                             die_hash,
9174                             die_eq,
9175                             NULL,
9176                             &cu->comp_unit_obstack,
9177                             hashtab_obstack_allocate,
9178                             dummy_obstack_deallocate);
9179
9180   init_cu_die_reader (&reader_specs, cu);
9181
9182   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9183
9184   if (read_abbrevs)
9185     do_cleanups (back_to);
9186
9187   return die;
9188 }
9189
9190 /* Main entry point for reading a DIE and all children.
9191    Read the DIE and dump it if requested.  */
9192
9193 static struct die_info *
9194 read_die_and_children (const struct die_reader_specs *reader,
9195                        gdb_byte *info_ptr,
9196                        gdb_byte **new_info_ptr,
9197                        struct die_info *parent)
9198 {
9199   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9200                                                      new_info_ptr, parent);
9201
9202   if (dwarf2_die_debug)
9203     {
9204       fprintf_unfiltered (gdb_stdlog,
9205                           "\nRead die from %s of %s:\n",
9206                           (reader->cu->per_cu->debug_types_section
9207                            ? ".debug_types"
9208                            : ".debug_info"),
9209                           reader->abfd->filename);
9210       dump_die (result, dwarf2_die_debug);
9211     }
9212
9213   return result;
9214 }
9215
9216 /* Read a single die and all its descendents.  Set the die's sibling
9217    field to NULL; set other fields in the die correctly, and set all
9218    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
9219    location of the info_ptr after reading all of those dies.  PARENT
9220    is the parent of the die in question.  */
9221
9222 static struct die_info *
9223 read_die_and_children_1 (const struct die_reader_specs *reader,
9224                          gdb_byte *info_ptr,
9225                          gdb_byte **new_info_ptr,
9226                          struct die_info *parent)
9227 {
9228   struct die_info *die;
9229   gdb_byte *cur_ptr;
9230   int has_children;
9231
9232   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9233   if (die == NULL)
9234     {
9235       *new_info_ptr = cur_ptr;
9236       return NULL;
9237     }
9238   store_in_ref_table (die, reader->cu);
9239
9240   if (has_children)
9241     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9242   else
9243     {
9244       die->child = NULL;
9245       *new_info_ptr = cur_ptr;
9246     }
9247
9248   die->sibling = NULL;
9249   die->parent = parent;
9250   return die;
9251 }
9252
9253 /* Read a die, all of its descendents, and all of its siblings; set
9254    all of the fields of all of the dies correctly.  Arguments are as
9255    in read_die_and_children.  */
9256
9257 static struct die_info *
9258 read_die_and_siblings (const struct die_reader_specs *reader,
9259                        gdb_byte *info_ptr,
9260                        gdb_byte **new_info_ptr,
9261                        struct die_info *parent)
9262 {
9263   struct die_info *first_die, *last_sibling;
9264   gdb_byte *cur_ptr;
9265
9266   cur_ptr = info_ptr;
9267   first_die = last_sibling = NULL;
9268
9269   while (1)
9270     {
9271       struct die_info *die
9272         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9273
9274       if (die == NULL)
9275         {
9276           *new_info_ptr = cur_ptr;
9277           return first_die;
9278         }
9279
9280       if (!first_die)
9281         first_die = die;
9282       else
9283         last_sibling->sibling = die;
9284
9285       last_sibling = die;
9286     }
9287 }
9288
9289 /* Read the die from the .debug_info section buffer.  Set DIEP to
9290    point to a newly allocated die with its information, except for its
9291    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
9292    whether the die has children or not.  */
9293
9294 static gdb_byte *
9295 read_full_die (const struct die_reader_specs *reader,
9296                struct die_info **diep, gdb_byte *info_ptr,
9297                int *has_children)
9298 {
9299   unsigned int abbrev_number, bytes_read, i, offset;
9300   struct abbrev_info *abbrev;
9301   struct die_info *die;
9302   struct dwarf2_cu *cu = reader->cu;
9303   bfd *abfd = reader->abfd;
9304
9305   offset = info_ptr - reader->buffer;
9306   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9307   info_ptr += bytes_read;
9308   if (!abbrev_number)
9309     {
9310       *diep = NULL;
9311       *has_children = 0;
9312       return info_ptr;
9313     }
9314
9315   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9316   if (!abbrev)
9317     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9318            abbrev_number,
9319            bfd_get_filename (abfd));
9320
9321   die = dwarf_alloc_die (cu, abbrev->num_attrs);
9322   die->offset = offset;
9323   die->tag = abbrev->tag;
9324   die->abbrev = abbrev_number;
9325
9326   die->num_attrs = abbrev->num_attrs;
9327
9328   for (i = 0; i < abbrev->num_attrs; ++i)
9329     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9330                                abfd, info_ptr, cu);
9331
9332   *diep = die;
9333   *has_children = abbrev->has_children;
9334   return info_ptr;
9335 }
9336
9337 /* In DWARF version 2, the description of the debugging information is
9338    stored in a separate .debug_abbrev section.  Before we read any
9339    dies from a section we read in all abbreviations and install them
9340    in a hash table.  This function also sets flags in CU describing
9341    the data found in the abbrev table.  */
9342
9343 static void
9344 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9345 {
9346   bfd *abfd = cu->objfile->obfd;
9347   struct comp_unit_head *cu_header = &cu->header;
9348   gdb_byte *abbrev_ptr;
9349   struct abbrev_info *cur_abbrev;
9350   unsigned int abbrev_number, bytes_read, abbrev_name;
9351   unsigned int abbrev_form, hash_number;
9352   struct attr_abbrev *cur_attrs;
9353   unsigned int allocated_attrs;
9354
9355   /* Initialize dwarf2 abbrevs.  */
9356   obstack_init (&cu->abbrev_obstack);
9357   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9358                                       (ABBREV_HASH_SIZE
9359                                        * sizeof (struct abbrev_info *)));
9360   memset (cu->dwarf2_abbrevs, 0,
9361           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9362
9363   dwarf2_read_section (dwarf2_per_objfile->objfile,
9364                        &dwarf2_per_objfile->abbrev);
9365   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9366   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9367   abbrev_ptr += bytes_read;
9368
9369   allocated_attrs = ATTR_ALLOC_CHUNK;
9370   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9371
9372   /* Loop until we reach an abbrev number of 0.  */
9373   while (abbrev_number)
9374     {
9375       cur_abbrev = dwarf_alloc_abbrev (cu);
9376
9377       /* read in abbrev header */
9378       cur_abbrev->number = abbrev_number;
9379       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9380       abbrev_ptr += bytes_read;
9381       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9382       abbrev_ptr += 1;
9383
9384       /* now read in declarations */
9385       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9386       abbrev_ptr += bytes_read;
9387       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9388       abbrev_ptr += bytes_read;
9389       while (abbrev_name)
9390         {
9391           if (cur_abbrev->num_attrs == allocated_attrs)
9392             {
9393               allocated_attrs += ATTR_ALLOC_CHUNK;
9394               cur_attrs
9395                 = xrealloc (cur_attrs, (allocated_attrs
9396                                         * sizeof (struct attr_abbrev)));
9397             }
9398
9399           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9400           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9401           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9402           abbrev_ptr += bytes_read;
9403           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9404           abbrev_ptr += bytes_read;
9405         }
9406
9407       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9408                                          (cur_abbrev->num_attrs
9409                                           * sizeof (struct attr_abbrev)));
9410       memcpy (cur_abbrev->attrs, cur_attrs,
9411               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9412
9413       hash_number = abbrev_number % ABBREV_HASH_SIZE;
9414       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9415       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9416
9417       /* Get next abbreviation.
9418          Under Irix6 the abbreviations for a compilation unit are not
9419          always properly terminated with an abbrev number of 0.
9420          Exit loop if we encounter an abbreviation which we have
9421          already read (which means we are about to read the abbreviations
9422          for the next compile unit) or if the end of the abbreviation
9423          table is reached.  */
9424       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9425           >= dwarf2_per_objfile->abbrev.size)
9426         break;
9427       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9428       abbrev_ptr += bytes_read;
9429       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9430         break;
9431     }
9432
9433   xfree (cur_attrs);
9434 }
9435
9436 /* Release the memory used by the abbrev table for a compilation unit.  */
9437
9438 static void
9439 dwarf2_free_abbrev_table (void *ptr_to_cu)
9440 {
9441   struct dwarf2_cu *cu = ptr_to_cu;
9442
9443   obstack_free (&cu->abbrev_obstack, NULL);
9444   cu->dwarf2_abbrevs = NULL;
9445 }
9446
9447 /* Lookup an abbrev_info structure in the abbrev hash table.  */
9448
9449 static struct abbrev_info *
9450 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9451 {
9452   unsigned int hash_number;
9453   struct abbrev_info *abbrev;
9454
9455   hash_number = number % ABBREV_HASH_SIZE;
9456   abbrev = cu->dwarf2_abbrevs[hash_number];
9457
9458   while (abbrev)
9459     {
9460       if (abbrev->number == number)
9461         return abbrev;
9462       else
9463         abbrev = abbrev->next;
9464     }
9465   return NULL;
9466 }
9467
9468 /* Returns nonzero if TAG represents a type that we might generate a partial
9469    symbol for.  */
9470
9471 static int
9472 is_type_tag_for_partial (int tag)
9473 {
9474   switch (tag)
9475     {
9476 #if 0
9477     /* Some types that would be reasonable to generate partial symbols for,
9478        that we don't at present.  */
9479     case DW_TAG_array_type:
9480     case DW_TAG_file_type:
9481     case DW_TAG_ptr_to_member_type:
9482     case DW_TAG_set_type:
9483     case DW_TAG_string_type:
9484     case DW_TAG_subroutine_type:
9485 #endif
9486     case DW_TAG_base_type:
9487     case DW_TAG_class_type:
9488     case DW_TAG_interface_type:
9489     case DW_TAG_enumeration_type:
9490     case DW_TAG_structure_type:
9491     case DW_TAG_subrange_type:
9492     case DW_TAG_typedef:
9493     case DW_TAG_union_type:
9494       return 1;
9495     default:
9496       return 0;
9497     }
9498 }
9499
9500 /* Load all DIEs that are interesting for partial symbols into memory.  */
9501
9502 static struct partial_die_info *
9503 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9504                    int building_psymtab, struct dwarf2_cu *cu)
9505 {
9506   struct objfile *objfile = cu->objfile;
9507   struct partial_die_info *part_die;
9508   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9509   struct abbrev_info *abbrev;
9510   unsigned int bytes_read;
9511   unsigned int load_all = 0;
9512
9513   int nesting_level = 1;
9514
9515   parent_die = NULL;
9516   last_die = NULL;
9517
9518   if (cu->per_cu && cu->per_cu->load_all_dies)
9519     load_all = 1;
9520
9521   cu->partial_dies
9522     = htab_create_alloc_ex (cu->header.length / 12,
9523                             partial_die_hash,
9524                             partial_die_eq,
9525                             NULL,
9526                             &cu->comp_unit_obstack,
9527                             hashtab_obstack_allocate,
9528                             dummy_obstack_deallocate);
9529
9530   part_die = obstack_alloc (&cu->comp_unit_obstack,
9531                             sizeof (struct partial_die_info));
9532
9533   while (1)
9534     {
9535       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9536
9537       /* A NULL abbrev means the end of a series of children.  */
9538       if (abbrev == NULL)
9539         {
9540           if (--nesting_level == 0)
9541             {
9542               /* PART_DIE was probably the last thing allocated on the
9543                  comp_unit_obstack, so we could call obstack_free
9544                  here.  We don't do that because the waste is small,
9545                  and will be cleaned up when we're done with this
9546                  compilation unit.  This way, we're also more robust
9547                  against other users of the comp_unit_obstack.  */
9548               return first_die;
9549             }
9550           info_ptr += bytes_read;
9551           last_die = parent_die;
9552           parent_die = parent_die->die_parent;
9553           continue;
9554         }
9555
9556       /* Check for template arguments.  We never save these; if
9557          they're seen, we just mark the parent, and go on our way.  */
9558       if (parent_die != NULL
9559           && cu->language == language_cplus
9560           && (abbrev->tag == DW_TAG_template_type_param
9561               || abbrev->tag == DW_TAG_template_value_param))
9562         {
9563           parent_die->has_template_arguments = 1;
9564
9565           if (!load_all)
9566             {
9567               /* We don't need a partial DIE for the template argument.  */
9568               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9569                                        cu);
9570               continue;
9571             }
9572         }
9573
9574       /* We only recurse into subprograms looking for template arguments.
9575          Skip their other children.  */
9576       if (!load_all
9577           && cu->language == language_cplus
9578           && parent_die != NULL
9579           && parent_die->tag == DW_TAG_subprogram)
9580         {
9581           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9582           continue;
9583         }
9584
9585       /* Check whether this DIE is interesting enough to save.  Normally
9586          we would not be interested in members here, but there may be
9587          later variables referencing them via DW_AT_specification (for
9588          static members).  */
9589       if (!load_all
9590           && !is_type_tag_for_partial (abbrev->tag)
9591           && abbrev->tag != DW_TAG_constant
9592           && abbrev->tag != DW_TAG_enumerator
9593           && abbrev->tag != DW_TAG_subprogram
9594           && abbrev->tag != DW_TAG_lexical_block
9595           && abbrev->tag != DW_TAG_variable
9596           && abbrev->tag != DW_TAG_namespace
9597           && abbrev->tag != DW_TAG_module
9598           && abbrev->tag != DW_TAG_member)
9599         {
9600           /* Otherwise we skip to the next sibling, if any.  */
9601           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9602           continue;
9603         }
9604
9605       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9606                                    buffer, info_ptr, cu);
9607
9608       /* This two-pass algorithm for processing partial symbols has a
9609          high cost in cache pressure.  Thus, handle some simple cases
9610          here which cover the majority of C partial symbols.  DIEs
9611          which neither have specification tags in them, nor could have
9612          specification tags elsewhere pointing at them, can simply be
9613          processed and discarded.
9614
9615          This segment is also optional; scan_partial_symbols and
9616          add_partial_symbol will handle these DIEs if we chain
9617          them in normally.  When compilers which do not emit large
9618          quantities of duplicate debug information are more common,
9619          this code can probably be removed.  */
9620
9621       /* Any complete simple types at the top level (pretty much all
9622          of them, for a language without namespaces), can be processed
9623          directly.  */
9624       if (parent_die == NULL
9625           && part_die->has_specification == 0
9626           && part_die->is_declaration == 0
9627           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9628               || part_die->tag == DW_TAG_base_type
9629               || part_die->tag == DW_TAG_subrange_type))
9630         {
9631           if (building_psymtab && part_die->name != NULL)
9632             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9633                                  VAR_DOMAIN, LOC_TYPEDEF,
9634                                  &objfile->static_psymbols,
9635                                  0, (CORE_ADDR) 0, cu->language, objfile);
9636           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9637           continue;
9638         }
9639
9640       /* The exception for DW_TAG_typedef with has_children above is
9641          a workaround of GCC PR debug/47510.  In the case of this complaint
9642          type_name_no_tag_or_error will error on such types later.
9643
9644          GDB skipped children of DW_TAG_typedef by the shortcut above and then
9645          it could not find the child DIEs referenced later, this is checked
9646          above.  In correct DWARF DW_TAG_typedef should have no children.  */
9647
9648       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9649         complaint (&symfile_complaints,
9650                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9651                      "- DIE at 0x%x [in module %s]"),
9652                    part_die->offset, objfile->name);
9653
9654       /* If we're at the second level, and we're an enumerator, and
9655          our parent has no specification (meaning possibly lives in a
9656          namespace elsewhere), then we can add the partial symbol now
9657          instead of queueing it.  */
9658       if (part_die->tag == DW_TAG_enumerator
9659           && parent_die != NULL
9660           && parent_die->die_parent == NULL
9661           && parent_die->tag == DW_TAG_enumeration_type
9662           && parent_die->has_specification == 0)
9663         {
9664           if (part_die->name == NULL)
9665             complaint (&symfile_complaints,
9666                        _("malformed enumerator DIE ignored"));
9667           else if (building_psymtab)
9668             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9669                                  VAR_DOMAIN, LOC_CONST,
9670                                  (cu->language == language_cplus
9671                                   || cu->language == language_java)
9672                                  ? &objfile->global_psymbols
9673                                  : &objfile->static_psymbols,
9674                                  0, (CORE_ADDR) 0, cu->language, objfile);
9675
9676           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9677           continue;
9678         }
9679
9680       /* We'll save this DIE so link it in.  */
9681       part_die->die_parent = parent_die;
9682       part_die->die_sibling = NULL;
9683       part_die->die_child = NULL;
9684
9685       if (last_die && last_die == parent_die)
9686         last_die->die_child = part_die;
9687       else if (last_die)
9688         last_die->die_sibling = part_die;
9689
9690       last_die = part_die;
9691
9692       if (first_die == NULL)
9693         first_die = part_die;
9694
9695       /* Maybe add the DIE to the hash table.  Not all DIEs that we
9696          find interesting need to be in the hash table, because we
9697          also have the parent/sibling/child chains; only those that we
9698          might refer to by offset later during partial symbol reading.
9699
9700          For now this means things that might have be the target of a
9701          DW_AT_specification, DW_AT_abstract_origin, or
9702          DW_AT_extension.  DW_AT_extension will refer only to
9703          namespaces; DW_AT_abstract_origin refers to functions (and
9704          many things under the function DIE, but we do not recurse
9705          into function DIEs during partial symbol reading) and
9706          possibly variables as well; DW_AT_specification refers to
9707          declarations.  Declarations ought to have the DW_AT_declaration
9708          flag.  It happens that GCC forgets to put it in sometimes, but
9709          only for functions, not for types.
9710
9711          Adding more things than necessary to the hash table is harmless
9712          except for the performance cost.  Adding too few will result in
9713          wasted time in find_partial_die, when we reread the compilation
9714          unit with load_all_dies set.  */
9715
9716       if (load_all
9717           || abbrev->tag == DW_TAG_constant
9718           || abbrev->tag == DW_TAG_subprogram
9719           || abbrev->tag == DW_TAG_variable
9720           || abbrev->tag == DW_TAG_namespace
9721           || part_die->is_declaration)
9722         {
9723           void **slot;
9724
9725           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9726                                            part_die->offset, INSERT);
9727           *slot = part_die;
9728         }
9729
9730       part_die = obstack_alloc (&cu->comp_unit_obstack,
9731                                 sizeof (struct partial_die_info));
9732
9733       /* For some DIEs we want to follow their children (if any).  For C
9734          we have no reason to follow the children of structures; for other
9735          languages we have to, so that we can get at method physnames
9736          to infer fully qualified class names, for DW_AT_specification,
9737          and for C++ template arguments.  For C++, we also look one level
9738          inside functions to find template arguments (if the name of the
9739          function does not already contain the template arguments).
9740
9741          For Ada, we need to scan the children of subprograms and lexical
9742          blocks as well because Ada allows the definition of nested
9743          entities that could be interesting for the debugger, such as
9744          nested subprograms for instance.  */
9745       if (last_die->has_children
9746           && (load_all
9747               || last_die->tag == DW_TAG_namespace
9748               || last_die->tag == DW_TAG_module
9749               || last_die->tag == DW_TAG_enumeration_type
9750               || (cu->language == language_cplus
9751                   && last_die->tag == DW_TAG_subprogram
9752                   && (last_die->name == NULL
9753                       || strchr (last_die->name, '<') == NULL))
9754               || (cu->language != language_c
9755                   && (last_die->tag == DW_TAG_class_type
9756                       || last_die->tag == DW_TAG_interface_type
9757                       || last_die->tag == DW_TAG_structure_type
9758                       || last_die->tag == DW_TAG_union_type))
9759               || (cu->language == language_ada
9760                   && (last_die->tag == DW_TAG_subprogram
9761                       || last_die->tag == DW_TAG_lexical_block))))
9762         {
9763           nesting_level++;
9764           parent_die = last_die;
9765           continue;
9766         }
9767
9768       /* Otherwise we skip to the next sibling, if any.  */
9769       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9770
9771       /* Back to the top, do it again.  */
9772     }
9773 }
9774
9775 /* Read a minimal amount of information into the minimal die structure.  */
9776
9777 static gdb_byte *
9778 read_partial_die (struct partial_die_info *part_die,
9779                   struct abbrev_info *abbrev,
9780                   unsigned int abbrev_len, bfd *abfd,
9781                   gdb_byte *buffer, gdb_byte *info_ptr,
9782                   struct dwarf2_cu *cu)
9783 {
9784   struct objfile *objfile = cu->objfile;
9785   unsigned int i;
9786   struct attribute attr;
9787   int has_low_pc_attr = 0;
9788   int has_high_pc_attr = 0;
9789
9790   memset (part_die, 0, sizeof (struct partial_die_info));
9791
9792   part_die->offset = info_ptr - buffer;
9793
9794   info_ptr += abbrev_len;
9795
9796   if (abbrev == NULL)
9797     return info_ptr;
9798
9799   part_die->tag = abbrev->tag;
9800   part_die->has_children = abbrev->has_children;
9801
9802   for (i = 0; i < abbrev->num_attrs; ++i)
9803     {
9804       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9805
9806       /* Store the data if it is of an attribute we want to keep in a
9807          partial symbol table.  */
9808       switch (attr.name)
9809         {
9810         case DW_AT_name:
9811           switch (part_die->tag)
9812             {
9813             case DW_TAG_compile_unit:
9814             case DW_TAG_type_unit:
9815               /* Compilation units have a DW_AT_name that is a filename, not
9816                  a source language identifier.  */
9817             case DW_TAG_enumeration_type:
9818             case DW_TAG_enumerator:
9819               /* These tags always have simple identifiers already; no need
9820                  to canonicalize them.  */
9821               part_die->name = DW_STRING (&attr);
9822               break;
9823             default:
9824               part_die->name
9825                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9826                                             &objfile->objfile_obstack);
9827               break;
9828             }
9829           break;
9830         case DW_AT_linkage_name:
9831         case DW_AT_MIPS_linkage_name:
9832           /* Note that both forms of linkage name might appear.  We
9833              assume they will be the same, and we only store the last
9834              one we see.  */
9835           if (cu->language == language_ada)
9836             part_die->name = DW_STRING (&attr);
9837           part_die->linkage_name = DW_STRING (&attr);
9838           break;
9839         case DW_AT_low_pc:
9840           has_low_pc_attr = 1;
9841           part_die->lowpc = DW_ADDR (&attr);
9842           break;
9843         case DW_AT_high_pc:
9844           has_high_pc_attr = 1;
9845           part_die->highpc = DW_ADDR (&attr);
9846           break;
9847         case DW_AT_location:
9848           /* Support the .debug_loc offsets.  */
9849           if (attr_form_is_block (&attr))
9850             {
9851                part_die->locdesc = DW_BLOCK (&attr);
9852             }
9853           else if (attr_form_is_section_offset (&attr))
9854             {
9855               dwarf2_complex_location_expr_complaint ();
9856             }
9857           else
9858             {
9859               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9860                                                      "partial symbol information");
9861             }
9862           break;
9863         case DW_AT_external:
9864           part_die->is_external = DW_UNSND (&attr);
9865           break;
9866         case DW_AT_declaration:
9867           part_die->is_declaration = DW_UNSND (&attr);
9868           break;
9869         case DW_AT_type:
9870           part_die->has_type = 1;
9871           break;
9872         case DW_AT_abstract_origin:
9873         case DW_AT_specification:
9874         case DW_AT_extension:
9875           part_die->has_specification = 1;
9876           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9877           break;
9878         case DW_AT_sibling:
9879           /* Ignore absolute siblings, they might point outside of
9880              the current compile unit.  */
9881           if (attr.form == DW_FORM_ref_addr)
9882             complaint (&symfile_complaints,
9883                        _("ignoring absolute DW_AT_sibling"));
9884           else
9885             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9886           break;
9887         case DW_AT_byte_size:
9888           part_die->has_byte_size = 1;
9889           break;
9890         case DW_AT_calling_convention:
9891           /* DWARF doesn't provide a way to identify a program's source-level
9892              entry point.  DW_AT_calling_convention attributes are only meant
9893              to describe functions' calling conventions.
9894
9895              However, because it's a necessary piece of information in
9896              Fortran, and because DW_CC_program is the only piece of debugging
9897              information whose definition refers to a 'main program' at all,
9898              several compilers have begun marking Fortran main programs with
9899              DW_CC_program --- even when those functions use the standard
9900              calling conventions.
9901
9902              So until DWARF specifies a way to provide this information and
9903              compilers pick up the new representation, we'll support this
9904              practice.  */
9905           if (DW_UNSND (&attr) == DW_CC_program
9906               && cu->language == language_fortran)
9907             {
9908               set_main_name (part_die->name);
9909
9910               /* As this DIE has a static linkage the name would be difficult
9911                  to look up later.  */
9912               language_of_main = language_fortran;
9913             }
9914           break;
9915         case DW_AT_inline:
9916           if (DW_UNSND (&attr) == DW_INL_inlined
9917               || DW_UNSND (&attr) == DW_INL_declared_inlined)
9918             part_die->may_be_inlined = 1;
9919           break;
9920         default:
9921           break;
9922         }
9923     }
9924
9925   if (has_low_pc_attr && has_high_pc_attr)
9926     {
9927       /* When using the GNU linker, .gnu.linkonce. sections are used to
9928          eliminate duplicate copies of functions and vtables and such.
9929          The linker will arbitrarily choose one and discard the others.
9930          The AT_*_pc values for such functions refer to local labels in
9931          these sections.  If the section from that file was discarded, the
9932          labels are not in the output, so the relocs get a value of 0.
9933          If this is a discarded function, mark the pc bounds as invalid,
9934          so that GDB will ignore it.  */
9935       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9936         {
9937           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9938
9939           complaint (&symfile_complaints,
9940                      _("DW_AT_low_pc %s is zero "
9941                        "for DIE at 0x%x [in module %s]"),
9942                      paddress (gdbarch, part_die->lowpc),
9943                      part_die->offset, objfile->name);
9944         }
9945       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
9946       else if (part_die->lowpc >= part_die->highpc)
9947         {
9948           struct gdbarch *gdbarch = get_objfile_arch (objfile);
9949
9950           complaint (&symfile_complaints,
9951                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9952                        "for DIE at 0x%x [in module %s]"),
9953                      paddress (gdbarch, part_die->lowpc),
9954                      paddress (gdbarch, part_die->highpc),
9955                      part_die->offset, objfile->name);
9956         }
9957       else
9958         part_die->has_pc_info = 1;
9959     }
9960
9961   return info_ptr;
9962 }
9963
9964 /* Find a cached partial DIE at OFFSET in CU.  */
9965
9966 static struct partial_die_info *
9967 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9968 {
9969   struct partial_die_info *lookup_die = NULL;
9970   struct partial_die_info part_die;
9971
9972   part_die.offset = offset;
9973   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9974
9975   return lookup_die;
9976 }
9977
9978 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9979    except in the case of .debug_types DIEs which do not reference
9980    outside their CU (they do however referencing other types via
9981    DW_FORM_ref_sig8).  */
9982
9983 static struct partial_die_info *
9984 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9985 {
9986   struct objfile *objfile = cu->objfile;
9987   struct dwarf2_per_cu_data *per_cu = NULL;
9988   struct partial_die_info *pd = NULL;
9989
9990   if (cu->per_cu->debug_types_section)
9991     {
9992       pd = find_partial_die_in_comp_unit (offset, cu);
9993       if (pd != NULL)
9994         return pd;
9995       goto not_found;
9996     }
9997
9998   if (offset_in_cu_p (&cu->header, offset))
9999     {
10000       pd = find_partial_die_in_comp_unit (offset, cu);
10001       if (pd != NULL)
10002         return pd;
10003     }
10004
10005   per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10006
10007   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10008     load_partial_comp_unit (per_cu);
10009
10010   per_cu->cu->last_used = 0;
10011   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10012
10013   if (pd == NULL && per_cu->load_all_dies == 0)
10014     {
10015       struct cleanup *back_to;
10016       struct partial_die_info comp_unit_die;
10017       struct abbrev_info *abbrev;
10018       unsigned int bytes_read;
10019       char *info_ptr;
10020
10021       per_cu->load_all_dies = 1;
10022
10023       /* Re-read the DIEs.  */
10024       back_to = make_cleanup (null_cleanup, 0);
10025       if (per_cu->cu->dwarf2_abbrevs == NULL)
10026         {
10027           dwarf2_read_abbrevs (per_cu->cu);
10028           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10029         }
10030       info_ptr = (dwarf2_per_objfile->info.buffer
10031                   + per_cu->cu->header.offset
10032                   + per_cu->cu->header.first_die_offset);
10033       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10034       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10035                                    objfile->obfd,
10036                                    dwarf2_per_objfile->info.buffer, info_ptr,
10037                                    per_cu->cu);
10038       if (comp_unit_die.has_children)
10039         load_partial_dies (objfile->obfd,
10040                            dwarf2_per_objfile->info.buffer, info_ptr,
10041                            0, per_cu->cu);
10042       do_cleanups (back_to);
10043
10044       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10045     }
10046
10047  not_found:
10048
10049   if (pd == NULL)
10050     internal_error (__FILE__, __LINE__,
10051                     _("could not find partial DIE 0x%x "
10052                       "in cache [from module %s]\n"),
10053                     offset, bfd_get_filename (objfile->obfd));
10054   return pd;
10055 }
10056
10057 /* See if we can figure out if the class lives in a namespace.  We do
10058    this by looking for a member function; its demangled name will
10059    contain namespace info, if there is any.  */
10060
10061 static void
10062 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10063                                   struct dwarf2_cu *cu)
10064 {
10065   /* NOTE: carlton/2003-10-07: Getting the info this way changes
10066      what template types look like, because the demangler
10067      frequently doesn't give the same name as the debug info.  We
10068      could fix this by only using the demangled name to get the
10069      prefix (but see comment in read_structure_type).  */
10070
10071   struct partial_die_info *real_pdi;
10072   struct partial_die_info *child_pdi;
10073
10074   /* If this DIE (this DIE's specification, if any) has a parent, then
10075      we should not do this.  We'll prepend the parent's fully qualified
10076      name when we create the partial symbol.  */
10077
10078   real_pdi = struct_pdi;
10079   while (real_pdi->has_specification)
10080     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10081
10082   if (real_pdi->die_parent != NULL)
10083     return;
10084
10085   for (child_pdi = struct_pdi->die_child;
10086        child_pdi != NULL;
10087        child_pdi = child_pdi->die_sibling)
10088     {
10089       if (child_pdi->tag == DW_TAG_subprogram
10090           && child_pdi->linkage_name != NULL)
10091         {
10092           char *actual_class_name
10093             = language_class_name_from_physname (cu->language_defn,
10094                                                  child_pdi->linkage_name);
10095           if (actual_class_name != NULL)
10096             {
10097               struct_pdi->name
10098                 = obsavestring (actual_class_name,
10099                                 strlen (actual_class_name),
10100                                 &cu->objfile->objfile_obstack);
10101               xfree (actual_class_name);
10102             }
10103           break;
10104         }
10105     }
10106 }
10107
10108 /* Adjust PART_DIE before generating a symbol for it.  This function
10109    may set the is_external flag or change the DIE's name.  */
10110
10111 static void
10112 fixup_partial_die (struct partial_die_info *part_die,
10113                    struct dwarf2_cu *cu)
10114 {
10115   /* Once we've fixed up a die, there's no point in doing so again.
10116      This also avoids a memory leak if we were to call
10117      guess_partial_die_structure_name multiple times.  */
10118   if (part_die->fixup_called)
10119     return;
10120
10121   /* If we found a reference attribute and the DIE has no name, try
10122      to find a name in the referred to DIE.  */
10123
10124   if (part_die->name == NULL && part_die->has_specification)
10125     {
10126       struct partial_die_info *spec_die;
10127
10128       spec_die = find_partial_die (part_die->spec_offset, cu);
10129
10130       fixup_partial_die (spec_die, cu);
10131
10132       if (spec_die->name)
10133         {
10134           part_die->name = spec_die->name;
10135
10136           /* Copy DW_AT_external attribute if it is set.  */
10137           if (spec_die->is_external)
10138             part_die->is_external = spec_die->is_external;
10139         }
10140     }
10141
10142   /* Set default names for some unnamed DIEs.  */
10143
10144   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10145     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10146
10147   /* If there is no parent die to provide a namespace, and there are
10148      children, see if we can determine the namespace from their linkage
10149      name.  */
10150   if (cu->language == language_cplus
10151       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10152       && part_die->die_parent == NULL
10153       && part_die->has_children
10154       && (part_die->tag == DW_TAG_class_type
10155           || part_die->tag == DW_TAG_structure_type
10156           || part_die->tag == DW_TAG_union_type))
10157     guess_partial_die_structure_name (part_die, cu);
10158
10159   /* GCC might emit a nameless struct or union that has a linkage
10160      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
10161   if (part_die->name == NULL
10162       && (part_die->tag == DW_TAG_class_type
10163           || part_die->tag == DW_TAG_interface_type
10164           || part_die->tag == DW_TAG_structure_type
10165           || part_die->tag == DW_TAG_union_type)
10166       && part_die->linkage_name != NULL)
10167     {
10168       char *demangled;
10169
10170       demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10171       if (demangled)
10172         {
10173           const char *base;
10174
10175           /* Strip any leading namespaces/classes, keep only the base name.
10176              DW_AT_name for named DIEs does not contain the prefixes.  */
10177           base = strrchr (demangled, ':');
10178           if (base && base > demangled && base[-1] == ':')
10179             base++;
10180           else
10181             base = demangled;
10182
10183           part_die->name = obsavestring (base, strlen (base),
10184                                          &cu->objfile->objfile_obstack);
10185           xfree (demangled);
10186         }
10187     }
10188
10189   part_die->fixup_called = 1;
10190 }
10191
10192 /* Read an attribute value described by an attribute form.  */
10193
10194 static gdb_byte *
10195 read_attribute_value (struct attribute *attr, unsigned form,
10196                       bfd *abfd, gdb_byte *info_ptr,
10197                       struct dwarf2_cu *cu)
10198 {
10199   struct comp_unit_head *cu_header = &cu->header;
10200   unsigned int bytes_read;
10201   struct dwarf_block *blk;
10202
10203   attr->form = form;
10204   switch (form)
10205     {
10206     case DW_FORM_ref_addr:
10207       if (cu->header.version == 2)
10208         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10209       else
10210         DW_ADDR (attr) = read_offset (abfd, info_ptr,
10211                                       &cu->header, &bytes_read);
10212       info_ptr += bytes_read;
10213       break;
10214     case DW_FORM_addr:
10215       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10216       info_ptr += bytes_read;
10217       break;
10218     case DW_FORM_block2:
10219       blk = dwarf_alloc_block (cu);
10220       blk->size = read_2_bytes (abfd, info_ptr);
10221       info_ptr += 2;
10222       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10223       info_ptr += blk->size;
10224       DW_BLOCK (attr) = blk;
10225       break;
10226     case DW_FORM_block4:
10227       blk = dwarf_alloc_block (cu);
10228       blk->size = read_4_bytes (abfd, info_ptr);
10229       info_ptr += 4;
10230       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10231       info_ptr += blk->size;
10232       DW_BLOCK (attr) = blk;
10233       break;
10234     case DW_FORM_data2:
10235       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10236       info_ptr += 2;
10237       break;
10238     case DW_FORM_data4:
10239       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10240       info_ptr += 4;
10241       break;
10242     case DW_FORM_data8:
10243       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10244       info_ptr += 8;
10245       break;
10246     case DW_FORM_sec_offset:
10247       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10248       info_ptr += bytes_read;
10249       break;
10250     case DW_FORM_string:
10251       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10252       DW_STRING_IS_CANONICAL (attr) = 0;
10253       info_ptr += bytes_read;
10254       break;
10255     case DW_FORM_strp:
10256       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10257                                                &bytes_read);
10258       DW_STRING_IS_CANONICAL (attr) = 0;
10259       info_ptr += bytes_read;
10260       break;
10261     case DW_FORM_exprloc:
10262     case DW_FORM_block:
10263       blk = dwarf_alloc_block (cu);
10264       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10265       info_ptr += bytes_read;
10266       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10267       info_ptr += blk->size;
10268       DW_BLOCK (attr) = blk;
10269       break;
10270     case DW_FORM_block1:
10271       blk = dwarf_alloc_block (cu);
10272       blk->size = read_1_byte (abfd, info_ptr);
10273       info_ptr += 1;
10274       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10275       info_ptr += blk->size;
10276       DW_BLOCK (attr) = blk;
10277       break;
10278     case DW_FORM_data1:
10279       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10280       info_ptr += 1;
10281       break;
10282     case DW_FORM_flag:
10283       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10284       info_ptr += 1;
10285       break;
10286     case DW_FORM_flag_present:
10287       DW_UNSND (attr) = 1;
10288       break;
10289     case DW_FORM_sdata:
10290       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10291       info_ptr += bytes_read;
10292       break;
10293     case DW_FORM_udata:
10294       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10295       info_ptr += bytes_read;
10296       break;
10297     case DW_FORM_ref1:
10298       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10299       info_ptr += 1;
10300       break;
10301     case DW_FORM_ref2:
10302       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10303       info_ptr += 2;
10304       break;
10305     case DW_FORM_ref4:
10306       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10307       info_ptr += 4;
10308       break;
10309     case DW_FORM_ref8:
10310       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10311       info_ptr += 8;
10312       break;
10313     case DW_FORM_ref_sig8:
10314       /* Convert the signature to something we can record in DW_UNSND
10315          for later lookup.
10316          NOTE: This is NULL if the type wasn't found.  */
10317       DW_SIGNATURED_TYPE (attr) =
10318         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10319       info_ptr += 8;
10320       break;
10321     case DW_FORM_ref_udata:
10322       DW_ADDR (attr) = (cu->header.offset
10323                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10324       info_ptr += bytes_read;
10325       break;
10326     case DW_FORM_indirect:
10327       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10328       info_ptr += bytes_read;
10329       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10330       break;
10331     default:
10332       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10333              dwarf_form_name (form),
10334              bfd_get_filename (abfd));
10335     }
10336
10337   /* We have seen instances where the compiler tried to emit a byte
10338      size attribute of -1 which ended up being encoded as an unsigned
10339      0xffffffff.  Although 0xffffffff is technically a valid size value,
10340      an object of this size seems pretty unlikely so we can relatively
10341      safely treat these cases as if the size attribute was invalid and
10342      treat them as zero by default.  */
10343   if (attr->name == DW_AT_byte_size
10344       && form == DW_FORM_data4
10345       && DW_UNSND (attr) >= 0xffffffff)
10346     {
10347       complaint
10348         (&symfile_complaints,
10349          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10350          hex_string (DW_UNSND (attr)));
10351       DW_UNSND (attr) = 0;
10352     }
10353
10354   return info_ptr;
10355 }
10356
10357 /* Read an attribute described by an abbreviated attribute.  */
10358
10359 static gdb_byte *
10360 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10361                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10362 {
10363   attr->name = abbrev->name;
10364   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10365 }
10366
10367 /* Read dwarf information from a buffer.  */
10368
10369 static unsigned int
10370 read_1_byte (bfd *abfd, gdb_byte *buf)
10371 {
10372   return bfd_get_8 (abfd, buf);
10373 }
10374
10375 static int
10376 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10377 {
10378   return bfd_get_signed_8 (abfd, buf);
10379 }
10380
10381 static unsigned int
10382 read_2_bytes (bfd *abfd, gdb_byte *buf)
10383 {
10384   return bfd_get_16 (abfd, buf);
10385 }
10386
10387 static int
10388 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10389 {
10390   return bfd_get_signed_16 (abfd, buf);
10391 }
10392
10393 static unsigned int
10394 read_4_bytes (bfd *abfd, gdb_byte *buf)
10395 {
10396   return bfd_get_32 (abfd, buf);
10397 }
10398
10399 static int
10400 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10401 {
10402   return bfd_get_signed_32 (abfd, buf);
10403 }
10404
10405 static ULONGEST
10406 read_8_bytes (bfd *abfd, gdb_byte *buf)
10407 {
10408   return bfd_get_64 (abfd, buf);
10409 }
10410
10411 static CORE_ADDR
10412 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10413               unsigned int *bytes_read)
10414 {
10415   struct comp_unit_head *cu_header = &cu->header;
10416   CORE_ADDR retval = 0;
10417
10418   if (cu_header->signed_addr_p)
10419     {
10420       switch (cu_header->addr_size)
10421         {
10422         case 2:
10423           retval = bfd_get_signed_16 (abfd, buf);
10424           break;
10425         case 4:
10426           retval = bfd_get_signed_32 (abfd, buf);
10427           break;
10428         case 8:
10429           retval = bfd_get_signed_64 (abfd, buf);
10430           break;
10431         default:
10432           internal_error (__FILE__, __LINE__,
10433                           _("read_address: bad switch, signed [in module %s]"),
10434                           bfd_get_filename (abfd));
10435         }
10436     }
10437   else
10438     {
10439       switch (cu_header->addr_size)
10440         {
10441         case 2:
10442           retval = bfd_get_16 (abfd, buf);
10443           break;
10444         case 4:
10445           retval = bfd_get_32 (abfd, buf);
10446           break;
10447         case 8:
10448           retval = bfd_get_64 (abfd, buf);
10449           break;
10450         default:
10451           internal_error (__FILE__, __LINE__,
10452                           _("read_address: bad switch, "
10453                             "unsigned [in module %s]"),
10454                           bfd_get_filename (abfd));
10455         }
10456     }
10457
10458   *bytes_read = cu_header->addr_size;
10459   return retval;
10460 }
10461
10462 /* Read the initial length from a section.  The (draft) DWARF 3
10463    specification allows the initial length to take up either 4 bytes
10464    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
10465    bytes describe the length and all offsets will be 8 bytes in length
10466    instead of 4.
10467
10468    An older, non-standard 64-bit format is also handled by this
10469    function.  The older format in question stores the initial length
10470    as an 8-byte quantity without an escape value.  Lengths greater
10471    than 2^32 aren't very common which means that the initial 4 bytes
10472    is almost always zero.  Since a length value of zero doesn't make
10473    sense for the 32-bit format, this initial zero can be considered to
10474    be an escape value which indicates the presence of the older 64-bit
10475    format.  As written, the code can't detect (old format) lengths
10476    greater than 4GB.  If it becomes necessary to handle lengths
10477    somewhat larger than 4GB, we could allow other small values (such
10478    as the non-sensical values of 1, 2, and 3) to also be used as
10479    escape values indicating the presence of the old format.
10480
10481    The value returned via bytes_read should be used to increment the
10482    relevant pointer after calling read_initial_length().
10483
10484    [ Note:  read_initial_length() and read_offset() are based on the
10485      document entitled "DWARF Debugging Information Format", revision
10486      3, draft 8, dated November 19, 2001.  This document was obtained
10487      from:
10488
10489         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10490
10491      This document is only a draft and is subject to change.  (So beware.)
10492
10493      Details regarding the older, non-standard 64-bit format were
10494      determined empirically by examining 64-bit ELF files produced by
10495      the SGI toolchain on an IRIX 6.5 machine.
10496
10497      - Kevin, July 16, 2002
10498    ] */
10499
10500 static LONGEST
10501 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10502 {
10503   LONGEST length = bfd_get_32 (abfd, buf);
10504
10505   if (length == 0xffffffff)
10506     {
10507       length = bfd_get_64 (abfd, buf + 4);
10508       *bytes_read = 12;
10509     }
10510   else if (length == 0)
10511     {
10512       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
10513       length = bfd_get_64 (abfd, buf);
10514       *bytes_read = 8;
10515     }
10516   else
10517     {
10518       *bytes_read = 4;
10519     }
10520
10521   return length;
10522 }
10523
10524 /* Cover function for read_initial_length.
10525    Returns the length of the object at BUF, and stores the size of the
10526    initial length in *BYTES_READ and stores the size that offsets will be in
10527    *OFFSET_SIZE.
10528    If the initial length size is not equivalent to that specified in
10529    CU_HEADER then issue a complaint.
10530    This is useful when reading non-comp-unit headers.  */
10531
10532 static LONGEST
10533 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10534                                         const struct comp_unit_head *cu_header,
10535                                         unsigned int *bytes_read,
10536                                         unsigned int *offset_size)
10537 {
10538   LONGEST length = read_initial_length (abfd, buf, bytes_read);
10539
10540   gdb_assert (cu_header->initial_length_size == 4
10541               || cu_header->initial_length_size == 8
10542               || cu_header->initial_length_size == 12);
10543
10544   if (cu_header->initial_length_size != *bytes_read)
10545     complaint (&symfile_complaints,
10546                _("intermixed 32-bit and 64-bit DWARF sections"));
10547
10548   *offset_size = (*bytes_read == 4) ? 4 : 8;
10549   return length;
10550 }
10551
10552 /* Read an offset from the data stream.  The size of the offset is
10553    given by cu_header->offset_size.  */
10554
10555 static LONGEST
10556 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10557              unsigned int *bytes_read)
10558 {
10559   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10560
10561   *bytes_read = cu_header->offset_size;
10562   return offset;
10563 }
10564
10565 /* Read an offset from the data stream.  */
10566
10567 static LONGEST
10568 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10569 {
10570   LONGEST retval = 0;
10571
10572   switch (offset_size)
10573     {
10574     case 4:
10575       retval = bfd_get_32 (abfd, buf);
10576       break;
10577     case 8:
10578       retval = bfd_get_64 (abfd, buf);
10579       break;
10580     default:
10581       internal_error (__FILE__, __LINE__,
10582                       _("read_offset_1: bad switch [in module %s]"),
10583                       bfd_get_filename (abfd));
10584     }
10585
10586   return retval;
10587 }
10588
10589 static gdb_byte *
10590 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10591 {
10592   /* If the size of a host char is 8 bits, we can return a pointer
10593      to the buffer, otherwise we have to copy the data to a buffer
10594      allocated on the temporary obstack.  */
10595   gdb_assert (HOST_CHAR_BIT == 8);
10596   return buf;
10597 }
10598
10599 static char *
10600 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10601 {
10602   /* If the size of a host char is 8 bits, we can return a pointer
10603      to the string, otherwise we have to copy the string to a buffer
10604      allocated on the temporary obstack.  */
10605   gdb_assert (HOST_CHAR_BIT == 8);
10606   if (*buf == '\0')
10607     {
10608       *bytes_read_ptr = 1;
10609       return NULL;
10610     }
10611   *bytes_read_ptr = strlen ((char *) buf) + 1;
10612   return (char *) buf;
10613 }
10614
10615 static char *
10616 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10617 {
10618   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10619   if (dwarf2_per_objfile->str.buffer == NULL)
10620     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10621            bfd_get_filename (abfd));
10622   if (str_offset >= dwarf2_per_objfile->str.size)
10623     error (_("DW_FORM_strp pointing outside of "
10624              ".debug_str section [in module %s]"),
10625            bfd_get_filename (abfd));
10626   gdb_assert (HOST_CHAR_BIT == 8);
10627   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10628     return NULL;
10629   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10630 }
10631
10632 static char *
10633 read_indirect_string (bfd *abfd, gdb_byte *buf,
10634                       const struct comp_unit_head *cu_header,
10635                       unsigned int *bytes_read_ptr)
10636 {
10637   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10638
10639   return read_indirect_string_at_offset (abfd, str_offset);
10640 }
10641
10642 static unsigned long
10643 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10644 {
10645   unsigned long result;
10646   unsigned int num_read;
10647   int i, shift;
10648   unsigned char byte;
10649
10650   result = 0;
10651   shift = 0;
10652   num_read = 0;
10653   i = 0;
10654   while (1)
10655     {
10656       byte = bfd_get_8 (abfd, buf);
10657       buf++;
10658       num_read++;
10659       result |= ((unsigned long)(byte & 127) << shift);
10660       if ((byte & 128) == 0)
10661         {
10662           break;
10663         }
10664       shift += 7;
10665     }
10666   *bytes_read_ptr = num_read;
10667   return result;
10668 }
10669
10670 static long
10671 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10672 {
10673   long result;
10674   int i, shift, num_read;
10675   unsigned char byte;
10676
10677   result = 0;
10678   shift = 0;
10679   num_read = 0;
10680   i = 0;
10681   while (1)
10682     {
10683       byte = bfd_get_8 (abfd, buf);
10684       buf++;
10685       num_read++;
10686       result |= ((long)(byte & 127) << shift);
10687       shift += 7;
10688       if ((byte & 128) == 0)
10689         {
10690           break;
10691         }
10692     }
10693   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10694     result |= -(((long)1) << shift);
10695   *bytes_read_ptr = num_read;
10696   return result;
10697 }
10698
10699 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
10700
10701 static gdb_byte *
10702 skip_leb128 (bfd *abfd, gdb_byte *buf)
10703 {
10704   int byte;
10705
10706   while (1)
10707     {
10708       byte = bfd_get_8 (abfd, buf);
10709       buf++;
10710       if ((byte & 128) == 0)
10711         return buf;
10712     }
10713 }
10714
10715 static void
10716 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10717 {
10718   switch (lang)
10719     {
10720     case DW_LANG_C89:
10721     case DW_LANG_C99:
10722     case DW_LANG_C:
10723       cu->language = language_c;
10724       break;
10725     case DW_LANG_C_plus_plus:
10726       cu->language = language_cplus;
10727       break;
10728     case DW_LANG_D:
10729       cu->language = language_d;
10730       break;
10731     case DW_LANG_Fortran77:
10732     case DW_LANG_Fortran90:
10733     case DW_LANG_Fortran95:
10734       cu->language = language_fortran;
10735       break;
10736     case DW_LANG_Mips_Assembler:
10737       cu->language = language_asm;
10738       break;
10739     case DW_LANG_Java:
10740       cu->language = language_java;
10741       break;
10742     case DW_LANG_Ada83:
10743     case DW_LANG_Ada95:
10744       cu->language = language_ada;
10745       break;
10746     case DW_LANG_Modula2:
10747       cu->language = language_m2;
10748       break;
10749     case DW_LANG_Pascal83:
10750       cu->language = language_pascal;
10751       break;
10752     case DW_LANG_ObjC:
10753       cu->language = language_objc;
10754       break;
10755     case DW_LANG_Cobol74:
10756     case DW_LANG_Cobol85:
10757     default:
10758       cu->language = language_minimal;
10759       break;
10760     }
10761   cu->language_defn = language_def (cu->language);
10762 }
10763
10764 /* Return the named attribute or NULL if not there.  */
10765
10766 static struct attribute *
10767 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10768 {
10769   for (;;)
10770     {
10771       unsigned int i;
10772       struct attribute *spec = NULL;
10773
10774       for (i = 0; i < die->num_attrs; ++i)
10775         {
10776           if (die->attrs[i].name == name)
10777             return &die->attrs[i];
10778           if (die->attrs[i].name == DW_AT_specification
10779               || die->attrs[i].name == DW_AT_abstract_origin)
10780             spec = &die->attrs[i];
10781         }
10782
10783       if (!spec)
10784         break;
10785
10786       die = follow_die_ref (die, spec, &cu);
10787     }
10788
10789   return NULL;
10790 }
10791
10792 /* Return the named attribute or NULL if not there,
10793    but do not follow DW_AT_specification, etc.
10794    This is for use in contexts where we're reading .debug_types dies.
10795    Following DW_AT_specification, DW_AT_abstract_origin will take us
10796    back up the chain, and we want to go down.  */
10797
10798 static struct attribute *
10799 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10800                        struct dwarf2_cu *cu)
10801 {
10802   unsigned int i;
10803
10804   for (i = 0; i < die->num_attrs; ++i)
10805     if (die->attrs[i].name == name)
10806       return &die->attrs[i];
10807
10808   return NULL;
10809 }
10810
10811 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10812    and holds a non-zero value.  This function should only be used for
10813    DW_FORM_flag or DW_FORM_flag_present attributes.  */
10814
10815 static int
10816 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10817 {
10818   struct attribute *attr = dwarf2_attr (die, name, cu);
10819
10820   return (attr && DW_UNSND (attr));
10821 }
10822
10823 static int
10824 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10825 {
10826   /* A DIE is a declaration if it has a DW_AT_declaration attribute
10827      which value is non-zero.  However, we have to be careful with
10828      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10829      (via dwarf2_flag_true_p) follows this attribute.  So we may
10830      end up accidently finding a declaration attribute that belongs
10831      to a different DIE referenced by the specification attribute,
10832      even though the given DIE does not have a declaration attribute.  */
10833   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10834           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10835 }
10836
10837 /* Return the die giving the specification for DIE, if there is
10838    one.  *SPEC_CU is the CU containing DIE on input, and the CU
10839    containing the return value on output.  If there is no
10840    specification, but there is an abstract origin, that is
10841    returned.  */
10842
10843 static struct die_info *
10844 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10845 {
10846   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10847                                              *spec_cu);
10848
10849   if (spec_attr == NULL)
10850     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10851
10852   if (spec_attr == NULL)
10853     return NULL;
10854   else
10855     return follow_die_ref (die, spec_attr, spec_cu);
10856 }
10857
10858 /* Free the line_header structure *LH, and any arrays and strings it
10859    refers to.
10860    NOTE: This is also used as a "cleanup" function.  */
10861
10862 static void
10863 free_line_header (struct line_header *lh)
10864 {
10865   if (lh->standard_opcode_lengths)
10866     xfree (lh->standard_opcode_lengths);
10867
10868   /* Remember that all the lh->file_names[i].name pointers are
10869      pointers into debug_line_buffer, and don't need to be freed.  */
10870   if (lh->file_names)
10871     xfree (lh->file_names);
10872
10873   /* Similarly for the include directory names.  */
10874   if (lh->include_dirs)
10875     xfree (lh->include_dirs);
10876
10877   xfree (lh);
10878 }
10879
10880 /* Add an entry to LH's include directory table.  */
10881
10882 static void
10883 add_include_dir (struct line_header *lh, char *include_dir)
10884 {
10885   /* Grow the array if necessary.  */
10886   if (lh->include_dirs_size == 0)
10887     {
10888       lh->include_dirs_size = 1; /* for testing */
10889       lh->include_dirs = xmalloc (lh->include_dirs_size
10890                                   * sizeof (*lh->include_dirs));
10891     }
10892   else if (lh->num_include_dirs >= lh->include_dirs_size)
10893     {
10894       lh->include_dirs_size *= 2;
10895       lh->include_dirs = xrealloc (lh->include_dirs,
10896                                    (lh->include_dirs_size
10897                                     * sizeof (*lh->include_dirs)));
10898     }
10899
10900   lh->include_dirs[lh->num_include_dirs++] = include_dir;
10901 }
10902
10903 /* Add an entry to LH's file name table.  */
10904
10905 static void
10906 add_file_name (struct line_header *lh,
10907                char *name,
10908                unsigned int dir_index,
10909                unsigned int mod_time,
10910                unsigned int length)
10911 {
10912   struct file_entry *fe;
10913
10914   /* Grow the array if necessary.  */
10915   if (lh->file_names_size == 0)
10916     {
10917       lh->file_names_size = 1; /* for testing */
10918       lh->file_names = xmalloc (lh->file_names_size
10919                                 * sizeof (*lh->file_names));
10920     }
10921   else if (lh->num_file_names >= lh->file_names_size)
10922     {
10923       lh->file_names_size *= 2;
10924       lh->file_names = xrealloc (lh->file_names,
10925                                  (lh->file_names_size
10926                                   * sizeof (*lh->file_names)));
10927     }
10928
10929   fe = &lh->file_names[lh->num_file_names++];
10930   fe->name = name;
10931   fe->dir_index = dir_index;
10932   fe->mod_time = mod_time;
10933   fe->length = length;
10934   fe->included_p = 0;
10935   fe->symtab = NULL;
10936 }
10937
10938 /* Read the statement program header starting at OFFSET in
10939    .debug_line, according to the endianness of ABFD.  Return a pointer
10940    to a struct line_header, allocated using xmalloc.
10941
10942    NOTE: the strings in the include directory and file name tables of
10943    the returned object point into debug_line_buffer, and must not be
10944    freed.  */
10945
10946 static struct line_header *
10947 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10948                           struct dwarf2_cu *cu)
10949 {
10950   struct cleanup *back_to;
10951   struct line_header *lh;
10952   gdb_byte *line_ptr;
10953   unsigned int bytes_read, offset_size;
10954   int i;
10955   char *cur_dir, *cur_file;
10956
10957   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10958   if (dwarf2_per_objfile->line.buffer == NULL)
10959     {
10960       complaint (&symfile_complaints, _("missing .debug_line section"));
10961       return 0;
10962     }
10963
10964   /* Make sure that at least there's room for the total_length field.
10965      That could be 12 bytes long, but we're just going to fudge that.  */
10966   if (offset + 4 >= dwarf2_per_objfile->line.size)
10967     {
10968       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10969       return 0;
10970     }
10971
10972   lh = xmalloc (sizeof (*lh));
10973   memset (lh, 0, sizeof (*lh));
10974   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10975                           (void *) lh);
10976
10977   line_ptr = dwarf2_per_objfile->line.buffer + offset;
10978
10979   /* Read in the header.  */
10980   lh->total_length =
10981     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10982                                             &bytes_read, &offset_size);
10983   line_ptr += bytes_read;
10984   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10985                                      + dwarf2_per_objfile->line.size))
10986     {
10987       dwarf2_statement_list_fits_in_line_number_section_complaint ();
10988       return 0;
10989     }
10990   lh->statement_program_end = line_ptr + lh->total_length;
10991   lh->version = read_2_bytes (abfd, line_ptr);
10992   line_ptr += 2;
10993   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10994   line_ptr += offset_size;
10995   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10996   line_ptr += 1;
10997   if (lh->version >= 4)
10998     {
10999       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11000       line_ptr += 1;
11001     }
11002   else
11003     lh->maximum_ops_per_instruction = 1;
11004
11005   if (lh->maximum_ops_per_instruction == 0)
11006     {
11007       lh->maximum_ops_per_instruction = 1;
11008       complaint (&symfile_complaints,
11009                  _("invalid maximum_ops_per_instruction "
11010                    "in `.debug_line' section"));
11011     }
11012
11013   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11014   line_ptr += 1;
11015   lh->line_base = read_1_signed_byte (abfd, line_ptr);
11016   line_ptr += 1;
11017   lh->line_range = read_1_byte (abfd, line_ptr);
11018   line_ptr += 1;
11019   lh->opcode_base = read_1_byte (abfd, line_ptr);
11020   line_ptr += 1;
11021   lh->standard_opcode_lengths
11022     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11023
11024   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
11025   for (i = 1; i < lh->opcode_base; ++i)
11026     {
11027       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11028       line_ptr += 1;
11029     }
11030
11031   /* Read directory table.  */
11032   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11033     {
11034       line_ptr += bytes_read;
11035       add_include_dir (lh, cur_dir);
11036     }
11037   line_ptr += bytes_read;
11038
11039   /* Read file name table.  */
11040   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11041     {
11042       unsigned int dir_index, mod_time, length;
11043
11044       line_ptr += bytes_read;
11045       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11046       line_ptr += bytes_read;
11047       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11048       line_ptr += bytes_read;
11049       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11050       line_ptr += bytes_read;
11051
11052       add_file_name (lh, cur_file, dir_index, mod_time, length);
11053     }
11054   line_ptr += bytes_read;
11055   lh->statement_program_start = line_ptr;
11056
11057   if (line_ptr > (dwarf2_per_objfile->line.buffer
11058                   + dwarf2_per_objfile->line.size))
11059     complaint (&symfile_complaints,
11060                _("line number info header doesn't "
11061                  "fit in `.debug_line' section"));
11062
11063   discard_cleanups (back_to);
11064   return lh;
11065 }
11066
11067 /* Subroutine of dwarf_decode_lines to simplify it.
11068    Return the file name of the psymtab for included file FILE_INDEX
11069    in line header LH of PST.
11070    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11071    If space for the result is malloc'd, it will be freed by a cleanup.
11072    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.  */
11073
11074 static char *
11075 psymtab_include_file_name (const struct line_header *lh, int file_index,
11076                            const struct partial_symtab *pst,
11077                            const char *comp_dir)
11078 {
11079   const struct file_entry fe = lh->file_names [file_index];
11080   char *include_name = fe.name;
11081   char *include_name_to_compare = include_name;
11082   char *dir_name = NULL;
11083   const char *pst_filename;
11084   char *copied_name = NULL;
11085   int file_is_pst;
11086
11087   if (fe.dir_index)
11088     dir_name = lh->include_dirs[fe.dir_index - 1];
11089
11090   if (!IS_ABSOLUTE_PATH (include_name)
11091       && (dir_name != NULL || comp_dir != NULL))
11092     {
11093       /* Avoid creating a duplicate psymtab for PST.
11094          We do this by comparing INCLUDE_NAME and PST_FILENAME.
11095          Before we do the comparison, however, we need to account
11096          for DIR_NAME and COMP_DIR.
11097          First prepend dir_name (if non-NULL).  If we still don't
11098          have an absolute path prepend comp_dir (if non-NULL).
11099          However, the directory we record in the include-file's
11100          psymtab does not contain COMP_DIR (to match the
11101          corresponding symtab(s)).
11102
11103          Example:
11104
11105          bash$ cd /tmp
11106          bash$ gcc -g ./hello.c
11107          include_name = "hello.c"
11108          dir_name = "."
11109          DW_AT_comp_dir = comp_dir = "/tmp"
11110          DW_AT_name = "./hello.c"  */
11111
11112       if (dir_name != NULL)
11113         {
11114           include_name = concat (dir_name, SLASH_STRING,
11115                                  include_name, (char *)NULL);
11116           include_name_to_compare = include_name;
11117           make_cleanup (xfree, include_name);
11118         }
11119       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11120         {
11121           include_name_to_compare = concat (comp_dir, SLASH_STRING,
11122                                             include_name, (char *)NULL);
11123         }
11124     }
11125
11126   pst_filename = pst->filename;
11127   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11128     {
11129       copied_name = concat (pst->dirname, SLASH_STRING,
11130                             pst_filename, (char *)NULL);
11131       pst_filename = copied_name;
11132     }
11133
11134   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11135
11136   if (include_name_to_compare != include_name)
11137     xfree (include_name_to_compare);
11138   if (copied_name != NULL)
11139     xfree (copied_name);
11140
11141   if (file_is_pst)
11142     return NULL;
11143   return include_name;
11144 }
11145
11146 /* Ignore this record_line request.  */
11147
11148 static void
11149 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11150 {
11151   return;
11152 }
11153
11154 /* Subroutine of dwarf_decode_lines to simplify it.
11155    Process the line number information in LH.  */
11156
11157 static void
11158 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11159                       struct dwarf2_cu *cu, struct partial_symtab *pst)
11160 {
11161   gdb_byte *line_ptr, *extended_end;
11162   gdb_byte *line_end;
11163   unsigned int bytes_read, extended_len;
11164   unsigned char op_code, extended_op, adj_opcode;
11165   CORE_ADDR baseaddr;
11166   struct objfile *objfile = cu->objfile;
11167   bfd *abfd = objfile->obfd;
11168   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11169   const int decode_for_pst_p = (pst != NULL);
11170   struct subfile *last_subfile = NULL;
11171   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11172     = record_line;
11173
11174   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11175
11176   line_ptr = lh->statement_program_start;
11177   line_end = lh->statement_program_end;
11178
11179   /* Read the statement sequences until there's nothing left.  */
11180   while (line_ptr < line_end)
11181     {
11182       /* state machine registers  */
11183       CORE_ADDR address = 0;
11184       unsigned int file = 1;
11185       unsigned int line = 1;
11186       unsigned int column = 0;
11187       int is_stmt = lh->default_is_stmt;
11188       int basic_block = 0;
11189       int end_sequence = 0;
11190       CORE_ADDR addr;
11191       unsigned char op_index = 0;
11192
11193       if (!decode_for_pst_p && lh->num_file_names >= file)
11194         {
11195           /* Start a subfile for the current file of the state machine.  */
11196           /* lh->include_dirs and lh->file_names are 0-based, but the
11197              directory and file name numbers in the statement program
11198              are 1-based.  */
11199           struct file_entry *fe = &lh->file_names[file - 1];
11200           char *dir = NULL;
11201
11202           if (fe->dir_index)
11203             dir = lh->include_dirs[fe->dir_index - 1];
11204
11205           dwarf2_start_subfile (fe->name, dir, comp_dir);
11206         }
11207
11208       /* Decode the table.  */
11209       while (!end_sequence)
11210         {
11211           op_code = read_1_byte (abfd, line_ptr);
11212           line_ptr += 1;
11213           if (line_ptr > line_end)
11214             {
11215               dwarf2_debug_line_missing_end_sequence_complaint ();
11216               break;
11217             }
11218
11219           if (op_code >= lh->opcode_base)
11220             {
11221               /* Special operand.  */
11222               adj_opcode = op_code - lh->opcode_base;
11223               address += (((op_index + (adj_opcode / lh->line_range))
11224                            / lh->maximum_ops_per_instruction)
11225                           * lh->minimum_instruction_length);
11226               op_index = ((op_index + (adj_opcode / lh->line_range))
11227                           % lh->maximum_ops_per_instruction);
11228               line += lh->line_base + (adj_opcode % lh->line_range);
11229               if (lh->num_file_names < file || file == 0)
11230                 dwarf2_debug_line_missing_file_complaint ();
11231               /* For now we ignore lines not starting on an
11232                  instruction boundary.  */
11233               else if (op_index == 0)
11234                 {
11235                   lh->file_names[file - 1].included_p = 1;
11236                   if (!decode_for_pst_p && is_stmt)
11237                     {
11238                       if (last_subfile != current_subfile)
11239                         {
11240                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11241                           if (last_subfile)
11242                             (*p_record_line) (last_subfile, 0, addr);
11243                           last_subfile = current_subfile;
11244                         }
11245                       /* Append row to matrix using current values.  */
11246                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11247                       (*p_record_line) (current_subfile, line, addr);
11248                     }
11249                 }
11250               basic_block = 0;
11251             }
11252           else switch (op_code)
11253             {
11254             case DW_LNS_extended_op:
11255               extended_len = read_unsigned_leb128 (abfd, line_ptr,
11256                                                    &bytes_read);
11257               line_ptr += bytes_read;
11258               extended_end = line_ptr + extended_len;
11259               extended_op = read_1_byte (abfd, line_ptr);
11260               line_ptr += 1;
11261               switch (extended_op)
11262                 {
11263                 case DW_LNE_end_sequence:
11264                   p_record_line = record_line;
11265                   end_sequence = 1;
11266                   break;
11267                 case DW_LNE_set_address:
11268                   address = read_address (abfd, line_ptr, cu, &bytes_read);
11269
11270                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11271                     {
11272                       /* This line table is for a function which has been
11273                          GCd by the linker.  Ignore it.  PR gdb/12528 */
11274
11275                       long line_offset
11276                         = line_ptr - dwarf2_per_objfile->line.buffer;
11277
11278                       complaint (&symfile_complaints,
11279                                  _(".debug_line address at offset 0x%lx is 0 "
11280                                    "[in module %s]"),
11281                                  line_offset, objfile->name);
11282                       p_record_line = noop_record_line;
11283                     }
11284
11285                   op_index = 0;
11286                   line_ptr += bytes_read;
11287                   address += baseaddr;
11288                   break;
11289                 case DW_LNE_define_file:
11290                   {
11291                     char *cur_file;
11292                     unsigned int dir_index, mod_time, length;
11293
11294                     cur_file = read_direct_string (abfd, line_ptr,
11295                                                    &bytes_read);
11296                     line_ptr += bytes_read;
11297                     dir_index =
11298                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11299                     line_ptr += bytes_read;
11300                     mod_time =
11301                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11302                     line_ptr += bytes_read;
11303                     length =
11304                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11305                     line_ptr += bytes_read;
11306                     add_file_name (lh, cur_file, dir_index, mod_time, length);
11307                   }
11308                   break;
11309                 case DW_LNE_set_discriminator:
11310                   /* The discriminator is not interesting to the debugger;
11311                      just ignore it.  */
11312                   line_ptr = extended_end;
11313                   break;
11314                 default:
11315                   complaint (&symfile_complaints,
11316                              _("mangled .debug_line section"));
11317                   return;
11318                 }
11319               /* Make sure that we parsed the extended op correctly.  If e.g.
11320                  we expected a different address size than the producer used,
11321                  we may have read the wrong number of bytes.  */
11322               if (line_ptr != extended_end)
11323                 {
11324                   complaint (&symfile_complaints,
11325                              _("mangled .debug_line section"));
11326                   return;
11327                 }
11328               break;
11329             case DW_LNS_copy:
11330               if (lh->num_file_names < file || file == 0)
11331                 dwarf2_debug_line_missing_file_complaint ();
11332               else
11333                 {
11334                   lh->file_names[file - 1].included_p = 1;
11335                   if (!decode_for_pst_p && is_stmt)
11336                     {
11337                       if (last_subfile != current_subfile)
11338                         {
11339                           addr = gdbarch_addr_bits_remove (gdbarch, address);
11340                           if (last_subfile)
11341                             (*p_record_line) (last_subfile, 0, addr);
11342                           last_subfile = current_subfile;
11343                         }
11344                       addr = gdbarch_addr_bits_remove (gdbarch, address);
11345                       (*p_record_line) (current_subfile, line, addr);
11346                     }
11347                 }
11348               basic_block = 0;
11349               break;
11350             case DW_LNS_advance_pc:
11351               {
11352                 CORE_ADDR adjust
11353                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11354
11355                 address += (((op_index + adjust)
11356                              / lh->maximum_ops_per_instruction)
11357                             * lh->minimum_instruction_length);
11358                 op_index = ((op_index + adjust)
11359                             % lh->maximum_ops_per_instruction);
11360                 line_ptr += bytes_read;
11361               }
11362               break;
11363             case DW_LNS_advance_line:
11364               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11365               line_ptr += bytes_read;
11366               break;
11367             case DW_LNS_set_file:
11368               {
11369                 /* The arrays lh->include_dirs and lh->file_names are
11370                    0-based, but the directory and file name numbers in
11371                    the statement program are 1-based.  */
11372                 struct file_entry *fe;
11373                 char *dir = NULL;
11374
11375                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11376                 line_ptr += bytes_read;
11377                 if (lh->num_file_names < file || file == 0)
11378                   dwarf2_debug_line_missing_file_complaint ();
11379                 else
11380                   {
11381                     fe = &lh->file_names[file - 1];
11382                     if (fe->dir_index)
11383                       dir = lh->include_dirs[fe->dir_index - 1];
11384                     if (!decode_for_pst_p)
11385                       {
11386                         last_subfile = current_subfile;
11387                         dwarf2_start_subfile (fe->name, dir, comp_dir);
11388                       }
11389                   }
11390               }
11391               break;
11392             case DW_LNS_set_column:
11393               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11394               line_ptr += bytes_read;
11395               break;
11396             case DW_LNS_negate_stmt:
11397               is_stmt = (!is_stmt);
11398               break;
11399             case DW_LNS_set_basic_block:
11400               basic_block = 1;
11401               break;
11402             /* Add to the address register of the state machine the
11403                address increment value corresponding to special opcode
11404                255.  I.e., this value is scaled by the minimum
11405                instruction length since special opcode 255 would have
11406                scaled the increment.  */
11407             case DW_LNS_const_add_pc:
11408               {
11409                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11410
11411                 address += (((op_index + adjust)
11412                              / lh->maximum_ops_per_instruction)
11413                             * lh->minimum_instruction_length);
11414                 op_index = ((op_index + adjust)
11415                             % lh->maximum_ops_per_instruction);
11416               }
11417               break;
11418             case DW_LNS_fixed_advance_pc:
11419               address += read_2_bytes (abfd, line_ptr);
11420               op_index = 0;
11421               line_ptr += 2;
11422               break;
11423             default:
11424               {
11425                 /* Unknown standard opcode, ignore it.  */
11426                 int i;
11427
11428                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11429                   {
11430                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11431                     line_ptr += bytes_read;
11432                   }
11433               }
11434             }
11435         }
11436       if (lh->num_file_names < file || file == 0)
11437         dwarf2_debug_line_missing_file_complaint ();
11438       else
11439         {
11440           lh->file_names[file - 1].included_p = 1;
11441           if (!decode_for_pst_p)
11442             {
11443               addr = gdbarch_addr_bits_remove (gdbarch, address);
11444               (*p_record_line) (current_subfile, 0, addr);
11445             }
11446         }
11447     }
11448 }
11449
11450 /* Decode the Line Number Program (LNP) for the given line_header
11451    structure and CU.  The actual information extracted and the type
11452    of structures created from the LNP depends on the value of PST.
11453
11454    1. If PST is NULL, then this procedure uses the data from the program
11455       to create all necessary symbol tables, and their linetables.
11456
11457    2. If PST is not NULL, this procedure reads the program to determine
11458       the list of files included by the unit represented by PST, and
11459       builds all the associated partial symbol tables.
11460
11461    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11462    It is used for relative paths in the line table.
11463    NOTE: When processing partial symtabs (pst != NULL),
11464    comp_dir == pst->dirname.
11465
11466    NOTE: It is important that psymtabs have the same file name (via strcmp)
11467    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
11468    symtab we don't use it in the name of the psymtabs we create.
11469    E.g. expand_line_sal requires this when finding psymtabs to expand.
11470    A good testcase for this is mb-inline.exp.  */
11471
11472 static void
11473 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11474                     struct dwarf2_cu *cu, struct partial_symtab *pst,
11475                     int want_line_info)
11476 {
11477   struct objfile *objfile = cu->objfile;
11478   const int decode_for_pst_p = (pst != NULL);
11479   struct subfile *first_subfile = current_subfile;
11480
11481   if (want_line_info)
11482     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11483
11484   if (decode_for_pst_p)
11485     {
11486       int file_index;
11487
11488       /* Now that we're done scanning the Line Header Program, we can
11489          create the psymtab of each included file.  */
11490       for (file_index = 0; file_index < lh->num_file_names; file_index++)
11491         if (lh->file_names[file_index].included_p == 1)
11492           {
11493             char *include_name =
11494               psymtab_include_file_name (lh, file_index, pst, comp_dir);
11495             if (include_name != NULL)
11496               dwarf2_create_include_psymtab (include_name, pst, objfile);
11497           }
11498     }
11499   else
11500     {
11501       /* Make sure a symtab is created for every file, even files
11502          which contain only variables (i.e. no code with associated
11503          line numbers).  */
11504       int i;
11505
11506       for (i = 0; i < lh->num_file_names; i++)
11507         {
11508           char *dir = NULL;
11509           struct file_entry *fe;
11510
11511           fe = &lh->file_names[i];
11512           if (fe->dir_index)
11513             dir = lh->include_dirs[fe->dir_index - 1];
11514           dwarf2_start_subfile (fe->name, dir, comp_dir);
11515
11516           /* Skip the main file; we don't need it, and it must be
11517              allocated last, so that it will show up before the
11518              non-primary symtabs in the objfile's symtab list.  */
11519           if (current_subfile == first_subfile)
11520             continue;
11521
11522           if (current_subfile->symtab == NULL)
11523             current_subfile->symtab = allocate_symtab (current_subfile->name,
11524                                                        objfile);
11525           fe->symtab = current_subfile->symtab;
11526         }
11527     }
11528 }
11529
11530 /* Start a subfile for DWARF.  FILENAME is the name of the file and
11531    DIRNAME the name of the source directory which contains FILENAME
11532    or NULL if not known.  COMP_DIR is the compilation directory for the
11533    linetable's compilation unit or NULL if not known.
11534    This routine tries to keep line numbers from identical absolute and
11535    relative file names in a common subfile.
11536
11537    Using the `list' example from the GDB testsuite, which resides in
11538    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11539    of /srcdir/list0.c yields the following debugging information for list0.c:
11540
11541    DW_AT_name:          /srcdir/list0.c
11542    DW_AT_comp_dir:              /compdir
11543    files.files[0].name: list0.h
11544    files.files[0].dir:  /srcdir
11545    files.files[1].name: list0.c
11546    files.files[1].dir:  /srcdir
11547
11548    The line number information for list0.c has to end up in a single
11549    subfile, so that `break /srcdir/list0.c:1' works as expected.
11550    start_subfile will ensure that this happens provided that we pass the
11551    concatenation of files.files[1].dir and files.files[1].name as the
11552    subfile's name.  */
11553
11554 static void
11555 dwarf2_start_subfile (char *filename, const char *dirname,
11556                       const char *comp_dir)
11557 {
11558   char *fullname;
11559
11560   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11561      `start_symtab' will always pass the contents of DW_AT_comp_dir as
11562      second argument to start_subfile.  To be consistent, we do the
11563      same here.  In order not to lose the line information directory,
11564      we concatenate it to the filename when it makes sense.
11565      Note that the Dwarf3 standard says (speaking of filenames in line
11566      information): ``The directory index is ignored for file names
11567      that represent full path names''.  Thus ignoring dirname in the
11568      `else' branch below isn't an issue.  */
11569
11570   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11571     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11572   else
11573     fullname = filename;
11574
11575   start_subfile (fullname, comp_dir);
11576
11577   if (fullname != filename)
11578     xfree (fullname);
11579 }
11580
11581 static void
11582 var_decode_location (struct attribute *attr, struct symbol *sym,
11583                      struct dwarf2_cu *cu)
11584 {
11585   struct objfile *objfile = cu->objfile;
11586   struct comp_unit_head *cu_header = &cu->header;
11587
11588   /* NOTE drow/2003-01-30: There used to be a comment and some special
11589      code here to turn a symbol with DW_AT_external and a
11590      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
11591      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11592      with some versions of binutils) where shared libraries could have
11593      relocations against symbols in their debug information - the
11594      minimal symbol would have the right address, but the debug info
11595      would not.  It's no longer necessary, because we will explicitly
11596      apply relocations when we read in the debug information now.  */
11597
11598   /* A DW_AT_location attribute with no contents indicates that a
11599      variable has been optimized away.  */
11600   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11601     {
11602       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11603       return;
11604     }
11605
11606   /* Handle one degenerate form of location expression specially, to
11607      preserve GDB's previous behavior when section offsets are
11608      specified.  If this is just a DW_OP_addr then mark this symbol
11609      as LOC_STATIC.  */
11610
11611   if (attr_form_is_block (attr)
11612       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11613       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11614     {
11615       unsigned int dummy;
11616
11617       SYMBOL_VALUE_ADDRESS (sym) =
11618         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11619       SYMBOL_CLASS (sym) = LOC_STATIC;
11620       fixup_symbol_section (sym, objfile);
11621       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11622                                               SYMBOL_SECTION (sym));
11623       return;
11624     }
11625
11626   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11627      expression evaluator, and use LOC_COMPUTED only when necessary
11628      (i.e. when the value of a register or memory location is
11629      referenced, or a thread-local block, etc.).  Then again, it might
11630      not be worthwhile.  I'm assuming that it isn't unless performance
11631      or memory numbers show me otherwise.  */
11632
11633   dwarf2_symbol_mark_computed (attr, sym, cu);
11634   SYMBOL_CLASS (sym) = LOC_COMPUTED;
11635
11636   if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11637     cu->has_loclist = 1;
11638 }
11639
11640 /* Given a pointer to a DWARF information entry, figure out if we need
11641    to make a symbol table entry for it, and if so, create a new entry
11642    and return a pointer to it.
11643    If TYPE is NULL, determine symbol type from the die, otherwise
11644    used the passed type.
11645    If SPACE is not NULL, use it to hold the new symbol.  If it is
11646    NULL, allocate a new symbol on the objfile's obstack.  */
11647
11648 static struct symbol *
11649 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11650                  struct symbol *space)
11651 {
11652   struct objfile *objfile = cu->objfile;
11653   struct symbol *sym = NULL;
11654   char *name;
11655   struct attribute *attr = NULL;
11656   struct attribute *attr2 = NULL;
11657   CORE_ADDR baseaddr;
11658   struct pending **list_to_add = NULL;
11659
11660   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11661
11662   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11663
11664   name = dwarf2_name (die, cu);
11665   if (name)
11666     {
11667       const char *linkagename;
11668       int suppress_add = 0;
11669
11670       if (space)
11671         sym = space;
11672       else
11673         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11674       OBJSTAT (objfile, n_syms++);
11675
11676       /* Cache this symbol's name and the name's demangled form (if any).  */
11677       SYMBOL_SET_LANGUAGE (sym, cu->language);
11678       linkagename = dwarf2_physname (name, die, cu);
11679       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11680
11681       /* Fortran does not have mangling standard and the mangling does differ
11682          between gfortran, iFort etc.  */
11683       if (cu->language == language_fortran
11684           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11685         symbol_set_demangled_name (&(sym->ginfo),
11686                                    (char *) dwarf2_full_name (name, die, cu),
11687                                    NULL);
11688
11689       /* Default assumptions.
11690          Use the passed type or decode it from the die.  */
11691       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11692       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11693       if (type != NULL)
11694         SYMBOL_TYPE (sym) = type;
11695       else
11696         SYMBOL_TYPE (sym) = die_type (die, cu);
11697       attr = dwarf2_attr (die,
11698                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11699                           cu);
11700       if (attr)
11701         {
11702           SYMBOL_LINE (sym) = DW_UNSND (attr);
11703         }
11704
11705       attr = dwarf2_attr (die,
11706                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11707                           cu);
11708       if (attr)
11709         {
11710           int file_index = DW_UNSND (attr);
11711
11712           if (cu->line_header == NULL
11713               || file_index > cu->line_header->num_file_names)
11714             complaint (&symfile_complaints,
11715                        _("file index out of range"));
11716           else if (file_index > 0)
11717             {
11718               struct file_entry *fe;
11719
11720               fe = &cu->line_header->file_names[file_index - 1];
11721               SYMBOL_SYMTAB (sym) = fe->symtab;
11722             }
11723         }
11724
11725       switch (die->tag)
11726         {
11727         case DW_TAG_label:
11728           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11729           if (attr)
11730             {
11731               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11732             }
11733           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11734           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11735           SYMBOL_CLASS (sym) = LOC_LABEL;
11736           add_symbol_to_list (sym, cu->list_in_scope);
11737           break;
11738         case DW_TAG_subprogram:
11739           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11740              finish_block.  */
11741           SYMBOL_CLASS (sym) = LOC_BLOCK;
11742           attr2 = dwarf2_attr (die, DW_AT_external, cu);
11743           if ((attr2 && (DW_UNSND (attr2) != 0))
11744               || cu->language == language_ada)
11745             {
11746               /* Subprograms marked external are stored as a global symbol.
11747                  Ada subprograms, whether marked external or not, are always
11748                  stored as a global symbol, because we want to be able to
11749                  access them globally.  For instance, we want to be able
11750                  to break on a nested subprogram without having to
11751                  specify the context.  */
11752               list_to_add = &global_symbols;
11753             }
11754           else
11755             {
11756               list_to_add = cu->list_in_scope;
11757             }
11758           break;
11759         case DW_TAG_inlined_subroutine:
11760           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11761              finish_block.  */
11762           SYMBOL_CLASS (sym) = LOC_BLOCK;
11763           SYMBOL_INLINED (sym) = 1;
11764           list_to_add = cu->list_in_scope;
11765           break;
11766         case DW_TAG_template_value_param:
11767           suppress_add = 1;
11768           /* Fall through.  */
11769         case DW_TAG_constant:
11770         case DW_TAG_variable:
11771         case DW_TAG_member:
11772           /* Compilation with minimal debug info may result in
11773              variables with missing type entries.  Change the
11774              misleading `void' type to something sensible.  */
11775           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11776             SYMBOL_TYPE (sym)
11777               = objfile_type (objfile)->nodebug_data_symbol;
11778
11779           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11780           /* In the case of DW_TAG_member, we should only be called for
11781              static const members.  */
11782           if (die->tag == DW_TAG_member)
11783             {
11784               /* dwarf2_add_field uses die_is_declaration,
11785                  so we do the same.  */
11786               gdb_assert (die_is_declaration (die, cu));
11787               gdb_assert (attr);
11788             }
11789           if (attr)
11790             {
11791               dwarf2_const_value (attr, sym, cu);
11792               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11793               if (!suppress_add)
11794                 {
11795                   if (attr2 && (DW_UNSND (attr2) != 0))
11796                     list_to_add = &global_symbols;
11797                   else
11798                     list_to_add = cu->list_in_scope;
11799                 }
11800               break;
11801             }
11802           attr = dwarf2_attr (die, DW_AT_location, cu);
11803           if (attr)
11804             {
11805               var_decode_location (attr, sym, cu);
11806               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11807               if (SYMBOL_CLASS (sym) == LOC_STATIC
11808                   && SYMBOL_VALUE_ADDRESS (sym) == 0
11809                   && !dwarf2_per_objfile->has_section_at_zero)
11810                 {
11811                   /* When a static variable is eliminated by the linker,
11812                      the corresponding debug information is not stripped
11813                      out, but the variable address is set to null;
11814                      do not add such variables into symbol table.  */
11815                 }
11816               else if (attr2 && (DW_UNSND (attr2) != 0))
11817                 {
11818                   /* Workaround gfortran PR debug/40040 - it uses
11819                      DW_AT_location for variables in -fPIC libraries which may
11820                      get overriden by other libraries/executable and get
11821                      a different address.  Resolve it by the minimal symbol
11822                      which may come from inferior's executable using copy
11823                      relocation.  Make this workaround only for gfortran as for
11824                      other compilers GDB cannot guess the minimal symbol
11825                      Fortran mangling kind.  */
11826                   if (cu->language == language_fortran && die->parent
11827                       && die->parent->tag == DW_TAG_module
11828                       && cu->producer
11829                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11830                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11831
11832                   /* A variable with DW_AT_external is never static,
11833                      but it may be block-scoped.  */
11834                   list_to_add = (cu->list_in_scope == &file_symbols
11835                                  ? &global_symbols : cu->list_in_scope);
11836                 }
11837               else
11838                 list_to_add = cu->list_in_scope;
11839             }
11840           else
11841             {
11842               /* We do not know the address of this symbol.
11843                  If it is an external symbol and we have type information
11844                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
11845                  The address of the variable will then be determined from
11846                  the minimal symbol table whenever the variable is
11847                  referenced.  */
11848               attr2 = dwarf2_attr (die, DW_AT_external, cu);
11849               if (attr2 && (DW_UNSND (attr2) != 0)
11850                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11851                 {
11852                   /* A variable with DW_AT_external is never static, but it
11853                      may be block-scoped.  */
11854                   list_to_add = (cu->list_in_scope == &file_symbols
11855                                  ? &global_symbols : cu->list_in_scope);
11856
11857                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11858                 }
11859               else if (!die_is_declaration (die, cu))
11860                 {
11861                   /* Use the default LOC_OPTIMIZED_OUT class.  */
11862                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11863                   if (!suppress_add)
11864                     list_to_add = cu->list_in_scope;
11865                 }
11866             }
11867           break;
11868         case DW_TAG_formal_parameter:
11869           /* If we are inside a function, mark this as an argument.  If
11870              not, we might be looking at an argument to an inlined function
11871              when we do not have enough information to show inlined frames;
11872              pretend it's a local variable in that case so that the user can
11873              still see it.  */
11874           if (context_stack_depth > 0
11875               && context_stack[context_stack_depth - 1].name != NULL)
11876             SYMBOL_IS_ARGUMENT (sym) = 1;
11877           attr = dwarf2_attr (die, DW_AT_location, cu);
11878           if (attr)
11879             {
11880               var_decode_location (attr, sym, cu);
11881             }
11882           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11883           if (attr)
11884             {
11885               dwarf2_const_value (attr, sym, cu);
11886             }
11887
11888           list_to_add = cu->list_in_scope;
11889           break;
11890         case DW_TAG_unspecified_parameters:
11891           /* From varargs functions; gdb doesn't seem to have any
11892              interest in this information, so just ignore it for now.
11893              (FIXME?) */
11894           break;
11895         case DW_TAG_template_type_param:
11896           suppress_add = 1;
11897           /* Fall through.  */
11898         case DW_TAG_class_type:
11899         case DW_TAG_interface_type:
11900         case DW_TAG_structure_type:
11901         case DW_TAG_union_type:
11902         case DW_TAG_set_type:
11903         case DW_TAG_enumeration_type:
11904           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11905           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11906
11907           {
11908             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11909                really ever be static objects: otherwise, if you try
11910                to, say, break of a class's method and you're in a file
11911                which doesn't mention that class, it won't work unless
11912                the check for all static symbols in lookup_symbol_aux
11913                saves you.  See the OtherFileClass tests in
11914                gdb.c++/namespace.exp.  */
11915
11916             if (!suppress_add)
11917               {
11918                 list_to_add = (cu->list_in_scope == &file_symbols
11919                                && (cu->language == language_cplus
11920                                    || cu->language == language_java)
11921                                ? &global_symbols : cu->list_in_scope);
11922
11923                 /* The semantics of C++ state that "struct foo {
11924                    ... }" also defines a typedef for "foo".  A Java
11925                    class declaration also defines a typedef for the
11926                    class.  */
11927                 if (cu->language == language_cplus
11928                     || cu->language == language_java
11929                     || cu->language == language_ada)
11930                   {
11931                     /* The symbol's name is already allocated along
11932                        with this objfile, so we don't need to
11933                        duplicate it for the type.  */
11934                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11935                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11936                   }
11937               }
11938           }
11939           break;
11940         case DW_TAG_typedef:
11941           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11942           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11943           list_to_add = cu->list_in_scope;
11944           break;
11945         case DW_TAG_base_type:
11946         case DW_TAG_subrange_type:
11947           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11948           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11949           list_to_add = cu->list_in_scope;
11950           break;
11951         case DW_TAG_enumerator:
11952           attr = dwarf2_attr (die, DW_AT_const_value, cu);
11953           if (attr)
11954             {
11955               dwarf2_const_value (attr, sym, cu);
11956             }
11957           {
11958             /* NOTE: carlton/2003-11-10: See comment above in the
11959                DW_TAG_class_type, etc. block.  */
11960
11961             list_to_add = (cu->list_in_scope == &file_symbols
11962                            && (cu->language == language_cplus
11963                                || cu->language == language_java)
11964                            ? &global_symbols : cu->list_in_scope);
11965           }
11966           break;
11967         case DW_TAG_namespace:
11968           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11969           list_to_add = &global_symbols;
11970           break;
11971         default:
11972           /* Not a tag we recognize.  Hopefully we aren't processing
11973              trash data, but since we must specifically ignore things
11974              we don't recognize, there is nothing else we should do at
11975              this point.  */
11976           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11977                      dwarf_tag_name (die->tag));
11978           break;
11979         }
11980
11981       if (suppress_add)
11982         {
11983           sym->hash_next = objfile->template_symbols;
11984           objfile->template_symbols = sym;
11985           list_to_add = NULL;
11986         }
11987
11988       if (list_to_add != NULL)
11989         add_symbol_to_list (sym, list_to_add);
11990
11991       /* For the benefit of old versions of GCC, check for anonymous
11992          namespaces based on the demangled name.  */
11993       if (!processing_has_namespace_info
11994           && cu->language == language_cplus)
11995         cp_scan_for_anonymous_namespaces (sym, objfile);
11996     }
11997   return (sym);
11998 }
11999
12000 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
12001
12002 static struct symbol *
12003 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12004 {
12005   return new_symbol_full (die, type, cu, NULL);
12006 }
12007
12008 /* Given an attr with a DW_FORM_dataN value in host byte order,
12009    zero-extend it as appropriate for the symbol's type.  The DWARF
12010    standard (v4) is not entirely clear about the meaning of using
12011    DW_FORM_dataN for a constant with a signed type, where the type is
12012    wider than the data.  The conclusion of a discussion on the DWARF
12013    list was that this is unspecified.  We choose to always zero-extend
12014    because that is the interpretation long in use by GCC.  */
12015
12016 static gdb_byte *
12017 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12018                          const char *name, struct obstack *obstack,
12019                          struct dwarf2_cu *cu, long *value, int bits)
12020 {
12021   struct objfile *objfile = cu->objfile;
12022   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12023                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12024   LONGEST l = DW_UNSND (attr);
12025
12026   if (bits < sizeof (*value) * 8)
12027     {
12028       l &= ((LONGEST) 1 << bits) - 1;
12029       *value = l;
12030     }
12031   else if (bits == sizeof (*value) * 8)
12032     *value = l;
12033   else
12034     {
12035       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12036       store_unsigned_integer (bytes, bits / 8, byte_order, l);
12037       return bytes;
12038     }
12039
12040   return NULL;
12041 }
12042
12043 /* Read a constant value from an attribute.  Either set *VALUE, or if
12044    the value does not fit in *VALUE, set *BYTES - either already
12045    allocated on the objfile obstack, or newly allocated on OBSTACK,
12046    or, set *BATON, if we translated the constant to a location
12047    expression.  */
12048
12049 static void
12050 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12051                          const char *name, struct obstack *obstack,
12052                          struct dwarf2_cu *cu,
12053                          long *value, gdb_byte **bytes,
12054                          struct dwarf2_locexpr_baton **baton)
12055 {
12056   struct objfile *objfile = cu->objfile;
12057   struct comp_unit_head *cu_header = &cu->header;
12058   struct dwarf_block *blk;
12059   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12060                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12061
12062   *value = 0;
12063   *bytes = NULL;
12064   *baton = NULL;
12065
12066   switch (attr->form)
12067     {
12068     case DW_FORM_addr:
12069       {
12070         gdb_byte *data;
12071
12072         if (TYPE_LENGTH (type) != cu_header->addr_size)
12073           dwarf2_const_value_length_mismatch_complaint (name,
12074                                                         cu_header->addr_size,
12075                                                         TYPE_LENGTH (type));
12076         /* Symbols of this form are reasonably rare, so we just
12077            piggyback on the existing location code rather than writing
12078            a new implementation of symbol_computed_ops.  */
12079         *baton = obstack_alloc (&objfile->objfile_obstack,
12080                                 sizeof (struct dwarf2_locexpr_baton));
12081         (*baton)->per_cu = cu->per_cu;
12082         gdb_assert ((*baton)->per_cu);
12083
12084         (*baton)->size = 2 + cu_header->addr_size;
12085         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12086         (*baton)->data = data;
12087
12088         data[0] = DW_OP_addr;
12089         store_unsigned_integer (&data[1], cu_header->addr_size,
12090                                 byte_order, DW_ADDR (attr));
12091         data[cu_header->addr_size + 1] = DW_OP_stack_value;
12092       }
12093       break;
12094     case DW_FORM_string:
12095     case DW_FORM_strp:
12096       /* DW_STRING is already allocated on the objfile obstack, point
12097          directly to it.  */
12098       *bytes = (gdb_byte *) DW_STRING (attr);
12099       break;
12100     case DW_FORM_block1:
12101     case DW_FORM_block2:
12102     case DW_FORM_block4:
12103     case DW_FORM_block:
12104     case DW_FORM_exprloc:
12105       blk = DW_BLOCK (attr);
12106       if (TYPE_LENGTH (type) != blk->size)
12107         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12108                                                       TYPE_LENGTH (type));
12109       *bytes = blk->data;
12110       break;
12111
12112       /* The DW_AT_const_value attributes are supposed to carry the
12113          symbol's value "represented as it would be on the target
12114          architecture."  By the time we get here, it's already been
12115          converted to host endianness, so we just need to sign- or
12116          zero-extend it as appropriate.  */
12117     case DW_FORM_data1:
12118       *bytes = dwarf2_const_value_data (attr, type, name,
12119                                         obstack, cu, value, 8);
12120       break;
12121     case DW_FORM_data2:
12122       *bytes = dwarf2_const_value_data (attr, type, name,
12123                                         obstack, cu, value, 16);
12124       break;
12125     case DW_FORM_data4:
12126       *bytes = dwarf2_const_value_data (attr, type, name,
12127                                         obstack, cu, value, 32);
12128       break;
12129     case DW_FORM_data8:
12130       *bytes = dwarf2_const_value_data (attr, type, name,
12131                                         obstack, cu, value, 64);
12132       break;
12133
12134     case DW_FORM_sdata:
12135       *value = DW_SND (attr);
12136       break;
12137
12138     case DW_FORM_udata:
12139       *value = DW_UNSND (attr);
12140       break;
12141
12142     default:
12143       complaint (&symfile_complaints,
12144                  _("unsupported const value attribute form: '%s'"),
12145                  dwarf_form_name (attr->form));
12146       *value = 0;
12147       break;
12148     }
12149 }
12150
12151
12152 /* Copy constant value from an attribute to a symbol.  */
12153
12154 static void
12155 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12156                     struct dwarf2_cu *cu)
12157 {
12158   struct objfile *objfile = cu->objfile;
12159   struct comp_unit_head *cu_header = &cu->header;
12160   long value;
12161   gdb_byte *bytes;
12162   struct dwarf2_locexpr_baton *baton;
12163
12164   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12165                            SYMBOL_PRINT_NAME (sym),
12166                            &objfile->objfile_obstack, cu,
12167                            &value, &bytes, &baton);
12168
12169   if (baton != NULL)
12170     {
12171       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12172       SYMBOL_LOCATION_BATON (sym) = baton;
12173       SYMBOL_CLASS (sym) = LOC_COMPUTED;
12174     }
12175   else if (bytes != NULL)
12176      {
12177       SYMBOL_VALUE_BYTES (sym) = bytes;
12178       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12179     }
12180   else
12181     {
12182       SYMBOL_VALUE (sym) = value;
12183       SYMBOL_CLASS (sym) = LOC_CONST;
12184     }
12185 }
12186
12187 /* Return the type of the die in question using its DW_AT_type attribute.  */
12188
12189 static struct type *
12190 die_type (struct die_info *die, struct dwarf2_cu *cu)
12191 {
12192   struct attribute *type_attr;
12193
12194   type_attr = dwarf2_attr (die, DW_AT_type, cu);
12195   if (!type_attr)
12196     {
12197       /* A missing DW_AT_type represents a void type.  */
12198       return objfile_type (cu->objfile)->builtin_void;
12199     }
12200
12201   return lookup_die_type (die, type_attr, cu);
12202 }
12203
12204 /* True iff CU's producer generates GNAT Ada auxiliary information
12205    that allows to find parallel types through that information instead
12206    of having to do expensive parallel lookups by type name.  */
12207
12208 static int
12209 need_gnat_info (struct dwarf2_cu *cu)
12210 {
12211   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12212      of GNAT produces this auxiliary information, without any indication
12213      that it is produced.  Part of enhancing the FSF version of GNAT
12214      to produce that information will be to put in place an indicator
12215      that we can use in order to determine whether the descriptive type
12216      info is available or not.  One suggestion that has been made is
12217      to use a new attribute, attached to the CU die.  For now, assume
12218      that the descriptive type info is not available.  */
12219   return 0;
12220 }
12221
12222 /* Return the auxiliary type of the die in question using its
12223    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
12224    attribute is not present.  */
12225
12226 static struct type *
12227 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12228 {
12229   struct attribute *type_attr;
12230
12231   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12232   if (!type_attr)
12233     return NULL;
12234
12235   return lookup_die_type (die, type_attr, cu);
12236 }
12237
12238 /* If DIE has a descriptive_type attribute, then set the TYPE's
12239    descriptive type accordingly.  */
12240
12241 static void
12242 set_descriptive_type (struct type *type, struct die_info *die,
12243                       struct dwarf2_cu *cu)
12244 {
12245   struct type *descriptive_type = die_descriptive_type (die, cu);
12246
12247   if (descriptive_type)
12248     {
12249       ALLOCATE_GNAT_AUX_TYPE (type);
12250       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12251     }
12252 }
12253
12254 /* Return the containing type of the die in question using its
12255    DW_AT_containing_type attribute.  */
12256
12257 static struct type *
12258 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12259 {
12260   struct attribute *type_attr;
12261
12262   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12263   if (!type_attr)
12264     error (_("Dwarf Error: Problem turning containing type into gdb type "
12265              "[in module %s]"), cu->objfile->name);
12266
12267   return lookup_die_type (die, type_attr, cu);
12268 }
12269
12270 /* Look up the type of DIE in CU using its type attribute ATTR.
12271    If there is no type substitute an error marker.  */
12272
12273 static struct type *
12274 lookup_die_type (struct die_info *die, struct attribute *attr,
12275                  struct dwarf2_cu *cu)
12276 {
12277   struct objfile *objfile = cu->objfile;
12278   struct type *this_type;
12279
12280   /* First see if we have it cached.  */
12281
12282   if (is_ref_attr (attr))
12283     {
12284       unsigned int offset = dwarf2_get_ref_die_offset (attr);
12285
12286       this_type = get_die_type_at_offset (offset, cu->per_cu);
12287     }
12288   else if (attr->form == DW_FORM_ref_sig8)
12289     {
12290       struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12291       struct dwarf2_cu *sig_cu;
12292       unsigned int offset;
12293
12294       /* sig_type will be NULL if the signatured type is missing from
12295          the debug info.  */
12296       if (sig_type == NULL)
12297         error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12298                  "at 0x%x [in module %s]"),
12299                die->offset, objfile->name);
12300
12301       gdb_assert (sig_type->per_cu.debug_types_section);
12302       offset = sig_type->per_cu.offset + sig_type->type_offset;
12303       this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12304     }
12305   else
12306     {
12307       dump_die_for_error (die);
12308       error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12309              dwarf_attr_name (attr->name), objfile->name);
12310     }
12311
12312   /* If not cached we need to read it in.  */
12313
12314   if (this_type == NULL)
12315     {
12316       struct die_info *type_die;
12317       struct dwarf2_cu *type_cu = cu;
12318
12319       type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12320       /* If the type is cached, we should have found it above.  */
12321       gdb_assert (get_die_type (type_die, type_cu) == NULL);
12322       this_type = read_type_die_1 (type_die, type_cu);
12323     }
12324
12325   /* If we still don't have a type use an error marker.  */
12326
12327   if (this_type == NULL)
12328     {
12329       char *message, *saved;
12330
12331       /* read_type_die already issued a complaint.  */
12332       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12333                             objfile->name,
12334                             cu->header.offset,
12335                             die->offset);
12336       saved = obstack_copy0 (&objfile->objfile_obstack,
12337                              message, strlen (message));
12338       xfree (message);
12339
12340       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12341     }
12342
12343   return this_type;
12344 }
12345
12346 /* Return the type in DIE, CU.
12347    Returns NULL for invalid types.
12348
12349    This first does a lookup in the appropriate type_hash table,
12350    and only reads the die in if necessary.
12351
12352    NOTE: This can be called when reading in partial or full symbols.  */
12353
12354 static struct type *
12355 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12356 {
12357   struct type *this_type;
12358
12359   this_type = get_die_type (die, cu);
12360   if (this_type)
12361     return this_type;
12362
12363   return read_type_die_1 (die, cu);
12364 }
12365
12366 /* Read the type in DIE, CU.
12367    Returns NULL for invalid types.  */
12368
12369 static struct type *
12370 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12371 {
12372   struct type *this_type = NULL;
12373
12374   switch (die->tag)
12375     {
12376     case DW_TAG_class_type:
12377     case DW_TAG_interface_type:
12378     case DW_TAG_structure_type:
12379     case DW_TAG_union_type:
12380       this_type = read_structure_type (die, cu);
12381       break;
12382     case DW_TAG_enumeration_type:
12383       this_type = read_enumeration_type (die, cu);
12384       break;
12385     case DW_TAG_subprogram:
12386     case DW_TAG_subroutine_type:
12387     case DW_TAG_inlined_subroutine:
12388       this_type = read_subroutine_type (die, cu);
12389       break;
12390     case DW_TAG_array_type:
12391       this_type = read_array_type (die, cu);
12392       break;
12393     case DW_TAG_set_type:
12394       this_type = read_set_type (die, cu);
12395       break;
12396     case DW_TAG_pointer_type:
12397       this_type = read_tag_pointer_type (die, cu);
12398       break;
12399     case DW_TAG_ptr_to_member_type:
12400       this_type = read_tag_ptr_to_member_type (die, cu);
12401       break;
12402     case DW_TAG_reference_type:
12403       this_type = read_tag_reference_type (die, cu);
12404       break;
12405     case DW_TAG_const_type:
12406       this_type = read_tag_const_type (die, cu);
12407       break;
12408     case DW_TAG_volatile_type:
12409       this_type = read_tag_volatile_type (die, cu);
12410       break;
12411     case DW_TAG_string_type:
12412       this_type = read_tag_string_type (die, cu);
12413       break;
12414     case DW_TAG_typedef:
12415       this_type = read_typedef (die, cu);
12416       break;
12417     case DW_TAG_subrange_type:
12418       this_type = read_subrange_type (die, cu);
12419       break;
12420     case DW_TAG_base_type:
12421       this_type = read_base_type (die, cu);
12422       break;
12423     case DW_TAG_unspecified_type:
12424       this_type = read_unspecified_type (die, cu);
12425       break;
12426     case DW_TAG_namespace:
12427       this_type = read_namespace_type (die, cu);
12428       break;
12429     case DW_TAG_module:
12430       this_type = read_module_type (die, cu);
12431       break;
12432     default:
12433       complaint (&symfile_complaints,
12434                  _("unexpected tag in read_type_die: '%s'"),
12435                  dwarf_tag_name (die->tag));
12436       break;
12437     }
12438
12439   return this_type;
12440 }
12441
12442 /* See if we can figure out if the class lives in a namespace.  We do
12443    this by looking for a member function; its demangled name will
12444    contain namespace info, if there is any.
12445    Return the computed name or NULL.
12446    Space for the result is allocated on the objfile's obstack.
12447    This is the full-die version of guess_partial_die_structure_name.
12448    In this case we know DIE has no useful parent.  */
12449
12450 static char *
12451 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12452 {
12453   struct die_info *spec_die;
12454   struct dwarf2_cu *spec_cu;
12455   struct die_info *child;
12456
12457   spec_cu = cu;
12458   spec_die = die_specification (die, &spec_cu);
12459   if (spec_die != NULL)
12460     {
12461       die = spec_die;
12462       cu = spec_cu;
12463     }
12464
12465   for (child = die->child;
12466        child != NULL;
12467        child = child->sibling)
12468     {
12469       if (child->tag == DW_TAG_subprogram)
12470         {
12471           struct attribute *attr;
12472
12473           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12474           if (attr == NULL)
12475             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12476           if (attr != NULL)
12477             {
12478               char *actual_name
12479                 = language_class_name_from_physname (cu->language_defn,
12480                                                      DW_STRING (attr));
12481               char *name = NULL;
12482
12483               if (actual_name != NULL)
12484                 {
12485                   char *die_name = dwarf2_name (die, cu);
12486
12487                   if (die_name != NULL
12488                       && strcmp (die_name, actual_name) != 0)
12489                     {
12490                       /* Strip off the class name from the full name.
12491                          We want the prefix.  */
12492                       int die_name_len = strlen (die_name);
12493                       int actual_name_len = strlen (actual_name);
12494
12495                       /* Test for '::' as a sanity check.  */
12496                       if (actual_name_len > die_name_len + 2
12497                           && actual_name[actual_name_len
12498                                          - die_name_len - 1] == ':')
12499                         name =
12500                           obsavestring (actual_name,
12501                                         actual_name_len - die_name_len - 2,
12502                                         &cu->objfile->objfile_obstack);
12503                     }
12504                 }
12505               xfree (actual_name);
12506               return name;
12507             }
12508         }
12509     }
12510
12511   return NULL;
12512 }
12513
12514 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
12515    prefix part in such case.  See
12516    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12517
12518 static char *
12519 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12520 {
12521   struct attribute *attr;
12522   char *base;
12523
12524   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12525       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12526     return NULL;
12527
12528   attr = dwarf2_attr (die, DW_AT_name, cu);
12529   if (attr != NULL && DW_STRING (attr) != NULL)
12530     return NULL;
12531
12532   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12533   if (attr == NULL)
12534     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12535   if (attr == NULL || DW_STRING (attr) == NULL)
12536     return NULL;
12537
12538   /* dwarf2_name had to be already called.  */
12539   gdb_assert (DW_STRING_IS_CANONICAL (attr));
12540
12541   /* Strip the base name, keep any leading namespaces/classes.  */
12542   base = strrchr (DW_STRING (attr), ':');
12543   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12544     return "";
12545
12546   return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12547                        &cu->objfile->objfile_obstack);
12548 }
12549
12550 /* Return the name of the namespace/class that DIE is defined within,
12551    or "" if we can't tell.  The caller should not xfree the result.
12552
12553    For example, if we're within the method foo() in the following
12554    code:
12555
12556    namespace N {
12557      class C {
12558        void foo () {
12559        }
12560      };
12561    }
12562
12563    then determine_prefix on foo's die will return "N::C".  */
12564
12565 static const char *
12566 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12567 {
12568   struct die_info *parent, *spec_die;
12569   struct dwarf2_cu *spec_cu;
12570   struct type *parent_type;
12571   char *retval;
12572
12573   if (cu->language != language_cplus && cu->language != language_java
12574       && cu->language != language_fortran)
12575     return "";
12576
12577   retval = anonymous_struct_prefix (die, cu);
12578   if (retval)
12579     return retval;
12580
12581   /* We have to be careful in the presence of DW_AT_specification.
12582      For example, with GCC 3.4, given the code
12583
12584      namespace N {
12585        void foo() {
12586          // Definition of N::foo.
12587        }
12588      }
12589
12590      then we'll have a tree of DIEs like this:
12591
12592      1: DW_TAG_compile_unit
12593        2: DW_TAG_namespace        // N
12594          3: DW_TAG_subprogram     // declaration of N::foo
12595        4: DW_TAG_subprogram       // definition of N::foo
12596             DW_AT_specification   // refers to die #3
12597
12598      Thus, when processing die #4, we have to pretend that we're in
12599      the context of its DW_AT_specification, namely the contex of die
12600      #3.  */
12601   spec_cu = cu;
12602   spec_die = die_specification (die, &spec_cu);
12603   if (spec_die == NULL)
12604     parent = die->parent;
12605   else
12606     {
12607       parent = spec_die->parent;
12608       cu = spec_cu;
12609     }
12610
12611   if (parent == NULL)
12612     return "";
12613   else if (parent->building_fullname)
12614     {
12615       const char *name;
12616       const char *parent_name;
12617
12618       /* It has been seen on RealView 2.2 built binaries,
12619          DW_TAG_template_type_param types actually _defined_ as
12620          children of the parent class:
12621
12622          enum E {};
12623          template class <class Enum> Class{};
12624          Class<enum E> class_e;
12625
12626          1: DW_TAG_class_type (Class)
12627            2: DW_TAG_enumeration_type (E)
12628              3: DW_TAG_enumerator (enum1:0)
12629              3: DW_TAG_enumerator (enum2:1)
12630              ...
12631            2: DW_TAG_template_type_param
12632               DW_AT_type  DW_FORM_ref_udata (E)
12633
12634          Besides being broken debug info, it can put GDB into an
12635          infinite loop.  Consider:
12636
12637          When we're building the full name for Class<E>, we'll start
12638          at Class, and go look over its template type parameters,
12639          finding E.  We'll then try to build the full name of E, and
12640          reach here.  We're now trying to build the full name of E,
12641          and look over the parent DIE for containing scope.  In the
12642          broken case, if we followed the parent DIE of E, we'd again
12643          find Class, and once again go look at its template type
12644          arguments, etc., etc.  Simply don't consider such parent die
12645          as source-level parent of this die (it can't be, the language
12646          doesn't allow it), and break the loop here.  */
12647       name = dwarf2_name (die, cu);
12648       parent_name = dwarf2_name (parent, cu);
12649       complaint (&symfile_complaints,
12650                  _("template param type '%s' defined within parent '%s'"),
12651                  name ? name : "<unknown>",
12652                  parent_name ? parent_name : "<unknown>");
12653       return "";
12654     }
12655   else
12656     switch (parent->tag)
12657       {
12658       case DW_TAG_namespace:
12659         parent_type = read_type_die (parent, cu);
12660         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12661            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12662            Work around this problem here.  */
12663         if (cu->language == language_cplus
12664             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12665           return "";
12666         /* We give a name to even anonymous namespaces.  */
12667         return TYPE_TAG_NAME (parent_type);
12668       case DW_TAG_class_type:
12669       case DW_TAG_interface_type:
12670       case DW_TAG_structure_type:
12671       case DW_TAG_union_type:
12672       case DW_TAG_module:
12673         parent_type = read_type_die (parent, cu);
12674         if (TYPE_TAG_NAME (parent_type) != NULL)
12675           return TYPE_TAG_NAME (parent_type);
12676         else
12677           /* An anonymous structure is only allowed non-static data
12678              members; no typedefs, no member functions, et cetera.
12679              So it does not need a prefix.  */
12680           return "";
12681       case DW_TAG_compile_unit:
12682         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
12683         if (cu->language == language_cplus
12684             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12685             && die->child != NULL
12686             && (die->tag == DW_TAG_class_type
12687                 || die->tag == DW_TAG_structure_type
12688                 || die->tag == DW_TAG_union_type))
12689           {
12690             char *name = guess_full_die_structure_name (die, cu);
12691             if (name != NULL)
12692               return name;
12693           }
12694         return "";
12695       default:
12696         return determine_prefix (parent, cu);
12697       }
12698 }
12699
12700 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12701    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
12702    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
12703    an obconcat, otherwise allocate storage for the result.  The CU argument is
12704    used to determine the language and hence, the appropriate separator.  */
12705
12706 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
12707
12708 static char *
12709 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12710                  int physname, struct dwarf2_cu *cu)
12711 {
12712   const char *lead = "";
12713   const char *sep;
12714
12715   if (suffix == NULL || suffix[0] == '\0'
12716       || prefix == NULL || prefix[0] == '\0')
12717     sep = "";
12718   else if (cu->language == language_java)
12719     sep = ".";
12720   else if (cu->language == language_fortran && physname)
12721     {
12722       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
12723          DW_AT_MIPS_linkage_name is preferred and used instead.  */
12724
12725       lead = "__";
12726       sep = "_MOD_";
12727     }
12728   else
12729     sep = "::";
12730
12731   if (prefix == NULL)
12732     prefix = "";
12733   if (suffix == NULL)
12734     suffix = "";
12735
12736   if (obs == NULL)
12737     {
12738       char *retval
12739         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12740
12741       strcpy (retval, lead);
12742       strcat (retval, prefix);
12743       strcat (retval, sep);
12744       strcat (retval, suffix);
12745       return retval;
12746     }
12747   else
12748     {
12749       /* We have an obstack.  */
12750       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12751     }
12752 }
12753
12754 /* Return sibling of die, NULL if no sibling.  */
12755
12756 static struct die_info *
12757 sibling_die (struct die_info *die)
12758 {
12759   return die->sibling;
12760 }
12761
12762 /* Get name of a die, return NULL if not found.  */
12763
12764 static char *
12765 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12766                           struct obstack *obstack)
12767 {
12768   if (name && cu->language == language_cplus)
12769     {
12770       char *canon_name = cp_canonicalize_string (name);
12771
12772       if (canon_name != NULL)
12773         {
12774           if (strcmp (canon_name, name) != 0)
12775             name = obsavestring (canon_name, strlen (canon_name),
12776                                  obstack);
12777           xfree (canon_name);
12778         }
12779     }
12780
12781   return name;
12782 }
12783
12784 /* Get name of a die, return NULL if not found.  */
12785
12786 static char *
12787 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12788 {
12789   struct attribute *attr;
12790
12791   attr = dwarf2_attr (die, DW_AT_name, cu);
12792   if ((!attr || !DW_STRING (attr))
12793       && die->tag != DW_TAG_class_type
12794       && die->tag != DW_TAG_interface_type
12795       && die->tag != DW_TAG_structure_type
12796       && die->tag != DW_TAG_union_type)
12797     return NULL;
12798
12799   switch (die->tag)
12800     {
12801     case DW_TAG_compile_unit:
12802       /* Compilation units have a DW_AT_name that is a filename, not
12803          a source language identifier.  */
12804     case DW_TAG_enumeration_type:
12805     case DW_TAG_enumerator:
12806       /* These tags always have simple identifiers already; no need
12807          to canonicalize them.  */
12808       return DW_STRING (attr);
12809
12810     case DW_TAG_subprogram:
12811       /* Java constructors will all be named "<init>", so return
12812          the class name when we see this special case.  */
12813       if (cu->language == language_java
12814           && DW_STRING (attr) != NULL
12815           && strcmp (DW_STRING (attr), "<init>") == 0)
12816         {
12817           struct dwarf2_cu *spec_cu = cu;
12818           struct die_info *spec_die;
12819
12820           /* GCJ will output '<init>' for Java constructor names.
12821              For this special case, return the name of the parent class.  */
12822
12823           /* GCJ may output suprogram DIEs with AT_specification set.
12824              If so, use the name of the specified DIE.  */
12825           spec_die = die_specification (die, &spec_cu);
12826           if (spec_die != NULL)
12827             return dwarf2_name (spec_die, spec_cu);
12828
12829           do
12830             {
12831               die = die->parent;
12832               if (die->tag == DW_TAG_class_type)
12833                 return dwarf2_name (die, cu);
12834             }
12835           while (die->tag != DW_TAG_compile_unit);
12836         }
12837       break;
12838
12839     case DW_TAG_class_type:
12840     case DW_TAG_interface_type:
12841     case DW_TAG_structure_type:
12842     case DW_TAG_union_type:
12843       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12844          structures or unions.  These were of the form "._%d" in GCC 4.1,
12845          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12846          and GCC 4.4.  We work around this problem by ignoring these.  */
12847       if (attr && DW_STRING (attr)
12848           && (strncmp (DW_STRING (attr), "._", 2) == 0
12849               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12850         return NULL;
12851
12852       /* GCC might emit a nameless typedef that has a linkage name.  See
12853          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
12854       if (!attr || DW_STRING (attr) == NULL)
12855         {
12856           char *demangled = NULL;
12857
12858           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12859           if (attr == NULL)
12860             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12861
12862           if (attr == NULL || DW_STRING (attr) == NULL)
12863             return NULL;
12864
12865           /* Avoid demangling DW_STRING (attr) the second time on a second
12866              call for the same DIE.  */
12867           if (!DW_STRING_IS_CANONICAL (attr))
12868             demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12869
12870           if (demangled)
12871             {
12872               char *base;
12873
12874               /* FIXME: we already did this for the partial symbol... */
12875               DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12876                                                &cu->objfile->objfile_obstack);
12877               DW_STRING_IS_CANONICAL (attr) = 1;
12878               xfree (demangled);
12879
12880               /* Strip any leading namespaces/classes, keep only the base name.
12881                  DW_AT_name for named DIEs does not contain the prefixes.  */
12882               base = strrchr (DW_STRING (attr), ':');
12883               if (base && base > DW_STRING (attr) && base[-1] == ':')
12884                 return &base[1];
12885               else
12886                 return DW_STRING (attr);
12887             }
12888         }
12889       break;
12890
12891     default:
12892       break;
12893     }
12894
12895   if (!DW_STRING_IS_CANONICAL (attr))
12896     {
12897       DW_STRING (attr)
12898         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12899                                     &cu->objfile->objfile_obstack);
12900       DW_STRING_IS_CANONICAL (attr) = 1;
12901     }
12902   return DW_STRING (attr);
12903 }
12904
12905 /* Return the die that this die in an extension of, or NULL if there
12906    is none.  *EXT_CU is the CU containing DIE on input, and the CU
12907    containing the return value on output.  */
12908
12909 static struct die_info *
12910 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12911 {
12912   struct attribute *attr;
12913
12914   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12915   if (attr == NULL)
12916     return NULL;
12917
12918   return follow_die_ref (die, attr, ext_cu);
12919 }
12920
12921 /* Convert a DIE tag into its string name.  */
12922
12923 static char *
12924 dwarf_tag_name (unsigned tag)
12925 {
12926   switch (tag)
12927     {
12928     case DW_TAG_padding:
12929       return "DW_TAG_padding";
12930     case DW_TAG_array_type:
12931       return "DW_TAG_array_type";
12932     case DW_TAG_class_type:
12933       return "DW_TAG_class_type";
12934     case DW_TAG_entry_point:
12935       return "DW_TAG_entry_point";
12936     case DW_TAG_enumeration_type:
12937       return "DW_TAG_enumeration_type";
12938     case DW_TAG_formal_parameter:
12939       return "DW_TAG_formal_parameter";
12940     case DW_TAG_imported_declaration:
12941       return "DW_TAG_imported_declaration";
12942     case DW_TAG_label:
12943       return "DW_TAG_label";
12944     case DW_TAG_lexical_block:
12945       return "DW_TAG_lexical_block";
12946     case DW_TAG_member:
12947       return "DW_TAG_member";
12948     case DW_TAG_pointer_type:
12949       return "DW_TAG_pointer_type";
12950     case DW_TAG_reference_type:
12951       return "DW_TAG_reference_type";
12952     case DW_TAG_compile_unit:
12953       return "DW_TAG_compile_unit";
12954     case DW_TAG_string_type:
12955       return "DW_TAG_string_type";
12956     case DW_TAG_structure_type:
12957       return "DW_TAG_structure_type";
12958     case DW_TAG_subroutine_type:
12959       return "DW_TAG_subroutine_type";
12960     case DW_TAG_typedef:
12961       return "DW_TAG_typedef";
12962     case DW_TAG_union_type:
12963       return "DW_TAG_union_type";
12964     case DW_TAG_unspecified_parameters:
12965       return "DW_TAG_unspecified_parameters";
12966     case DW_TAG_variant:
12967       return "DW_TAG_variant";
12968     case DW_TAG_common_block:
12969       return "DW_TAG_common_block";
12970     case DW_TAG_common_inclusion:
12971       return "DW_TAG_common_inclusion";
12972     case DW_TAG_inheritance:
12973       return "DW_TAG_inheritance";
12974     case DW_TAG_inlined_subroutine:
12975       return "DW_TAG_inlined_subroutine";
12976     case DW_TAG_module:
12977       return "DW_TAG_module";
12978     case DW_TAG_ptr_to_member_type:
12979       return "DW_TAG_ptr_to_member_type";
12980     case DW_TAG_set_type:
12981       return "DW_TAG_set_type";
12982     case DW_TAG_subrange_type:
12983       return "DW_TAG_subrange_type";
12984     case DW_TAG_with_stmt:
12985       return "DW_TAG_with_stmt";
12986     case DW_TAG_access_declaration:
12987       return "DW_TAG_access_declaration";
12988     case DW_TAG_base_type:
12989       return "DW_TAG_base_type";
12990     case DW_TAG_catch_block:
12991       return "DW_TAG_catch_block";
12992     case DW_TAG_const_type:
12993       return "DW_TAG_const_type";
12994     case DW_TAG_constant:
12995       return "DW_TAG_constant";
12996     case DW_TAG_enumerator:
12997       return "DW_TAG_enumerator";
12998     case DW_TAG_file_type:
12999       return "DW_TAG_file_type";
13000     case DW_TAG_friend:
13001       return "DW_TAG_friend";
13002     case DW_TAG_namelist:
13003       return "DW_TAG_namelist";
13004     case DW_TAG_namelist_item:
13005       return "DW_TAG_namelist_item";
13006     case DW_TAG_packed_type:
13007       return "DW_TAG_packed_type";
13008     case DW_TAG_subprogram:
13009       return "DW_TAG_subprogram";
13010     case DW_TAG_template_type_param:
13011       return "DW_TAG_template_type_param";
13012     case DW_TAG_template_value_param:
13013       return "DW_TAG_template_value_param";
13014     case DW_TAG_thrown_type:
13015       return "DW_TAG_thrown_type";
13016     case DW_TAG_try_block:
13017       return "DW_TAG_try_block";
13018     case DW_TAG_variant_part:
13019       return "DW_TAG_variant_part";
13020     case DW_TAG_variable:
13021       return "DW_TAG_variable";
13022     case DW_TAG_volatile_type:
13023       return "DW_TAG_volatile_type";
13024     case DW_TAG_dwarf_procedure:
13025       return "DW_TAG_dwarf_procedure";
13026     case DW_TAG_restrict_type:
13027       return "DW_TAG_restrict_type";
13028     case DW_TAG_interface_type:
13029       return "DW_TAG_interface_type";
13030     case DW_TAG_namespace:
13031       return "DW_TAG_namespace";
13032     case DW_TAG_imported_module:
13033       return "DW_TAG_imported_module";
13034     case DW_TAG_unspecified_type:
13035       return "DW_TAG_unspecified_type";
13036     case DW_TAG_partial_unit:
13037       return "DW_TAG_partial_unit";
13038     case DW_TAG_imported_unit:
13039       return "DW_TAG_imported_unit";
13040     case DW_TAG_condition:
13041       return "DW_TAG_condition";
13042     case DW_TAG_shared_type:
13043       return "DW_TAG_shared_type";
13044     case DW_TAG_type_unit:
13045       return "DW_TAG_type_unit";
13046     case DW_TAG_MIPS_loop:
13047       return "DW_TAG_MIPS_loop";
13048     case DW_TAG_HP_array_descriptor:
13049       return "DW_TAG_HP_array_descriptor";
13050     case DW_TAG_format_label:
13051       return "DW_TAG_format_label";
13052     case DW_TAG_function_template:
13053       return "DW_TAG_function_template";
13054     case DW_TAG_class_template:
13055       return "DW_TAG_class_template";
13056     case DW_TAG_GNU_BINCL:
13057       return "DW_TAG_GNU_BINCL";
13058     case DW_TAG_GNU_EINCL:
13059       return "DW_TAG_GNU_EINCL";
13060     case DW_TAG_upc_shared_type:
13061       return "DW_TAG_upc_shared_type";
13062     case DW_TAG_upc_strict_type:
13063       return "DW_TAG_upc_strict_type";
13064     case DW_TAG_upc_relaxed_type:
13065       return "DW_TAG_upc_relaxed_type";
13066     case DW_TAG_PGI_kanji_type:
13067       return "DW_TAG_PGI_kanji_type";
13068     case DW_TAG_PGI_interface_block:
13069       return "DW_TAG_PGI_interface_block";
13070     case DW_TAG_GNU_call_site:
13071       return "DW_TAG_GNU_call_site";
13072     default:
13073       return "DW_TAG_<unknown>";
13074     }
13075 }
13076
13077 /* Convert a DWARF attribute code into its string name.  */
13078
13079 static char *
13080 dwarf_attr_name (unsigned attr)
13081 {
13082   switch (attr)
13083     {
13084     case DW_AT_sibling:
13085       return "DW_AT_sibling";
13086     case DW_AT_location:
13087       return "DW_AT_location";
13088     case DW_AT_name:
13089       return "DW_AT_name";
13090     case DW_AT_ordering:
13091       return "DW_AT_ordering";
13092     case DW_AT_subscr_data:
13093       return "DW_AT_subscr_data";
13094     case DW_AT_byte_size:
13095       return "DW_AT_byte_size";
13096     case DW_AT_bit_offset:
13097       return "DW_AT_bit_offset";
13098     case DW_AT_bit_size:
13099       return "DW_AT_bit_size";
13100     case DW_AT_element_list:
13101       return "DW_AT_element_list";
13102     case DW_AT_stmt_list:
13103       return "DW_AT_stmt_list";
13104     case DW_AT_low_pc:
13105       return "DW_AT_low_pc";
13106     case DW_AT_high_pc:
13107       return "DW_AT_high_pc";
13108     case DW_AT_language:
13109       return "DW_AT_language";
13110     case DW_AT_member:
13111       return "DW_AT_member";
13112     case DW_AT_discr:
13113       return "DW_AT_discr";
13114     case DW_AT_discr_value:
13115       return "DW_AT_discr_value";
13116     case DW_AT_visibility:
13117       return "DW_AT_visibility";
13118     case DW_AT_import:
13119       return "DW_AT_import";
13120     case DW_AT_string_length:
13121       return "DW_AT_string_length";
13122     case DW_AT_common_reference:
13123       return "DW_AT_common_reference";
13124     case DW_AT_comp_dir:
13125       return "DW_AT_comp_dir";
13126     case DW_AT_const_value:
13127       return "DW_AT_const_value";
13128     case DW_AT_containing_type:
13129       return "DW_AT_containing_type";
13130     case DW_AT_default_value:
13131       return "DW_AT_default_value";
13132     case DW_AT_inline:
13133       return "DW_AT_inline";
13134     case DW_AT_is_optional:
13135       return "DW_AT_is_optional";
13136     case DW_AT_lower_bound:
13137       return "DW_AT_lower_bound";
13138     case DW_AT_producer:
13139       return "DW_AT_producer";
13140     case DW_AT_prototyped:
13141       return "DW_AT_prototyped";
13142     case DW_AT_return_addr:
13143       return "DW_AT_return_addr";
13144     case DW_AT_start_scope:
13145       return "DW_AT_start_scope";
13146     case DW_AT_bit_stride:
13147       return "DW_AT_bit_stride";
13148     case DW_AT_upper_bound:
13149       return "DW_AT_upper_bound";
13150     case DW_AT_abstract_origin:
13151       return "DW_AT_abstract_origin";
13152     case DW_AT_accessibility:
13153       return "DW_AT_accessibility";
13154     case DW_AT_address_class:
13155       return "DW_AT_address_class";
13156     case DW_AT_artificial:
13157       return "DW_AT_artificial";
13158     case DW_AT_base_types:
13159       return "DW_AT_base_types";
13160     case DW_AT_calling_convention:
13161       return "DW_AT_calling_convention";
13162     case DW_AT_count:
13163       return "DW_AT_count";
13164     case DW_AT_data_member_location:
13165       return "DW_AT_data_member_location";
13166     case DW_AT_decl_column:
13167       return "DW_AT_decl_column";
13168     case DW_AT_decl_file:
13169       return "DW_AT_decl_file";
13170     case DW_AT_decl_line:
13171       return "DW_AT_decl_line";
13172     case DW_AT_declaration:
13173       return "DW_AT_declaration";
13174     case DW_AT_discr_list:
13175       return "DW_AT_discr_list";
13176     case DW_AT_encoding:
13177       return "DW_AT_encoding";
13178     case DW_AT_external:
13179       return "DW_AT_external";
13180     case DW_AT_frame_base:
13181       return "DW_AT_frame_base";
13182     case DW_AT_friend:
13183       return "DW_AT_friend";
13184     case DW_AT_identifier_case:
13185       return "DW_AT_identifier_case";
13186     case DW_AT_macro_info:
13187       return "DW_AT_macro_info";
13188     case DW_AT_namelist_items:
13189       return "DW_AT_namelist_items";
13190     case DW_AT_priority:
13191       return "DW_AT_priority";
13192     case DW_AT_segment:
13193       return "DW_AT_segment";
13194     case DW_AT_specification:
13195       return "DW_AT_specification";
13196     case DW_AT_static_link:
13197       return "DW_AT_static_link";
13198     case DW_AT_type:
13199       return "DW_AT_type";
13200     case DW_AT_use_location:
13201       return "DW_AT_use_location";
13202     case DW_AT_variable_parameter:
13203       return "DW_AT_variable_parameter";
13204     case DW_AT_virtuality:
13205       return "DW_AT_virtuality";
13206     case DW_AT_vtable_elem_location:
13207       return "DW_AT_vtable_elem_location";
13208     /* DWARF 3 values.  */
13209     case DW_AT_allocated:
13210       return "DW_AT_allocated";
13211     case DW_AT_associated:
13212       return "DW_AT_associated";
13213     case DW_AT_data_location:
13214       return "DW_AT_data_location";
13215     case DW_AT_byte_stride:
13216       return "DW_AT_byte_stride";
13217     case DW_AT_entry_pc:
13218       return "DW_AT_entry_pc";
13219     case DW_AT_use_UTF8:
13220       return "DW_AT_use_UTF8";
13221     case DW_AT_extension:
13222       return "DW_AT_extension";
13223     case DW_AT_ranges:
13224       return "DW_AT_ranges";
13225     case DW_AT_trampoline:
13226       return "DW_AT_trampoline";
13227     case DW_AT_call_column:
13228       return "DW_AT_call_column";
13229     case DW_AT_call_file:
13230       return "DW_AT_call_file";
13231     case DW_AT_call_line:
13232       return "DW_AT_call_line";
13233     case DW_AT_description:
13234       return "DW_AT_description";
13235     case DW_AT_binary_scale:
13236       return "DW_AT_binary_scale";
13237     case DW_AT_decimal_scale:
13238       return "DW_AT_decimal_scale";
13239     case DW_AT_small:
13240       return "DW_AT_small";
13241     case DW_AT_decimal_sign:
13242       return "DW_AT_decimal_sign";
13243     case DW_AT_digit_count:
13244       return "DW_AT_digit_count";
13245     case DW_AT_picture_string:
13246       return "DW_AT_picture_string";
13247     case DW_AT_mutable:
13248       return "DW_AT_mutable";
13249     case DW_AT_threads_scaled:
13250       return "DW_AT_threads_scaled";
13251     case DW_AT_explicit:
13252       return "DW_AT_explicit";
13253     case DW_AT_object_pointer:
13254       return "DW_AT_object_pointer";
13255     case DW_AT_endianity:
13256       return "DW_AT_endianity";
13257     case DW_AT_elemental:
13258       return "DW_AT_elemental";
13259     case DW_AT_pure:
13260       return "DW_AT_pure";
13261     case DW_AT_recursive:
13262       return "DW_AT_recursive";
13263     /* DWARF 4 values.  */
13264     case DW_AT_signature:
13265       return "DW_AT_signature";
13266     case DW_AT_linkage_name:
13267       return "DW_AT_linkage_name";
13268     /* SGI/MIPS extensions.  */
13269 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13270     case DW_AT_MIPS_fde:
13271       return "DW_AT_MIPS_fde";
13272 #endif
13273     case DW_AT_MIPS_loop_begin:
13274       return "DW_AT_MIPS_loop_begin";
13275     case DW_AT_MIPS_tail_loop_begin:
13276       return "DW_AT_MIPS_tail_loop_begin";
13277     case DW_AT_MIPS_epilog_begin:
13278       return "DW_AT_MIPS_epilog_begin";
13279     case DW_AT_MIPS_loop_unroll_factor:
13280       return "DW_AT_MIPS_loop_unroll_factor";
13281     case DW_AT_MIPS_software_pipeline_depth:
13282       return "DW_AT_MIPS_software_pipeline_depth";
13283     case DW_AT_MIPS_linkage_name:
13284       return "DW_AT_MIPS_linkage_name";
13285     case DW_AT_MIPS_stride:
13286       return "DW_AT_MIPS_stride";
13287     case DW_AT_MIPS_abstract_name:
13288       return "DW_AT_MIPS_abstract_name";
13289     case DW_AT_MIPS_clone_origin:
13290       return "DW_AT_MIPS_clone_origin";
13291     case DW_AT_MIPS_has_inlines:
13292       return "DW_AT_MIPS_has_inlines";
13293     /* HP extensions.  */
13294 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13295     case DW_AT_HP_block_index:
13296       return "DW_AT_HP_block_index";
13297 #endif
13298     case DW_AT_HP_unmodifiable:
13299       return "DW_AT_HP_unmodifiable";
13300     case DW_AT_HP_actuals_stmt_list:
13301       return "DW_AT_HP_actuals_stmt_list";
13302     case DW_AT_HP_proc_per_section:
13303       return "DW_AT_HP_proc_per_section";
13304     case DW_AT_HP_raw_data_ptr:
13305       return "DW_AT_HP_raw_data_ptr";
13306     case DW_AT_HP_pass_by_reference:
13307       return "DW_AT_HP_pass_by_reference";
13308     case DW_AT_HP_opt_level:
13309       return "DW_AT_HP_opt_level";
13310     case DW_AT_HP_prof_version_id:
13311       return "DW_AT_HP_prof_version_id";
13312     case DW_AT_HP_opt_flags:
13313       return "DW_AT_HP_opt_flags";
13314     case DW_AT_HP_cold_region_low_pc:
13315       return "DW_AT_HP_cold_region_low_pc";
13316     case DW_AT_HP_cold_region_high_pc:
13317       return "DW_AT_HP_cold_region_high_pc";
13318     case DW_AT_HP_all_variables_modifiable:
13319       return "DW_AT_HP_all_variables_modifiable";
13320     case DW_AT_HP_linkage_name:
13321       return "DW_AT_HP_linkage_name";
13322     case DW_AT_HP_prof_flags:
13323       return "DW_AT_HP_prof_flags";
13324     /* GNU extensions.  */
13325     case DW_AT_sf_names:
13326       return "DW_AT_sf_names";
13327     case DW_AT_src_info:
13328       return "DW_AT_src_info";
13329     case DW_AT_mac_info:
13330       return "DW_AT_mac_info";
13331     case DW_AT_src_coords:
13332       return "DW_AT_src_coords";
13333     case DW_AT_body_begin:
13334       return "DW_AT_body_begin";
13335     case DW_AT_body_end:
13336       return "DW_AT_body_end";
13337     case DW_AT_GNU_vector:
13338       return "DW_AT_GNU_vector";
13339     case DW_AT_GNU_odr_signature:
13340       return "DW_AT_GNU_odr_signature";
13341     /* VMS extensions.  */
13342     case DW_AT_VMS_rtnbeg_pd_address:
13343       return "DW_AT_VMS_rtnbeg_pd_address";
13344     /* UPC extension.  */
13345     case DW_AT_upc_threads_scaled:
13346       return "DW_AT_upc_threads_scaled";
13347     /* PGI (STMicroelectronics) extensions.  */
13348     case DW_AT_PGI_lbase:
13349       return "DW_AT_PGI_lbase";
13350     case DW_AT_PGI_soffset:
13351       return "DW_AT_PGI_soffset";
13352     case DW_AT_PGI_lstride:
13353       return "DW_AT_PGI_lstride";
13354     default:
13355       return "DW_AT_<unknown>";
13356     }
13357 }
13358
13359 /* Convert a DWARF value form code into its string name.  */
13360
13361 static char *
13362 dwarf_form_name (unsigned form)
13363 {
13364   switch (form)
13365     {
13366     case DW_FORM_addr:
13367       return "DW_FORM_addr";
13368     case DW_FORM_block2:
13369       return "DW_FORM_block2";
13370     case DW_FORM_block4:
13371       return "DW_FORM_block4";
13372     case DW_FORM_data2:
13373       return "DW_FORM_data2";
13374     case DW_FORM_data4:
13375       return "DW_FORM_data4";
13376     case DW_FORM_data8:
13377       return "DW_FORM_data8";
13378     case DW_FORM_string:
13379       return "DW_FORM_string";
13380     case DW_FORM_block:
13381       return "DW_FORM_block";
13382     case DW_FORM_block1:
13383       return "DW_FORM_block1";
13384     case DW_FORM_data1:
13385       return "DW_FORM_data1";
13386     case DW_FORM_flag:
13387       return "DW_FORM_flag";
13388     case DW_FORM_sdata:
13389       return "DW_FORM_sdata";
13390     case DW_FORM_strp:
13391       return "DW_FORM_strp";
13392     case DW_FORM_udata:
13393       return "DW_FORM_udata";
13394     case DW_FORM_ref_addr:
13395       return "DW_FORM_ref_addr";
13396     case DW_FORM_ref1:
13397       return "DW_FORM_ref1";
13398     case DW_FORM_ref2:
13399       return "DW_FORM_ref2";
13400     case DW_FORM_ref4:
13401       return "DW_FORM_ref4";
13402     case DW_FORM_ref8:
13403       return "DW_FORM_ref8";
13404     case DW_FORM_ref_udata:
13405       return "DW_FORM_ref_udata";
13406     case DW_FORM_indirect:
13407       return "DW_FORM_indirect";
13408     case DW_FORM_sec_offset:
13409       return "DW_FORM_sec_offset";
13410     case DW_FORM_exprloc:
13411       return "DW_FORM_exprloc";
13412     case DW_FORM_flag_present:
13413       return "DW_FORM_flag_present";
13414     case DW_FORM_ref_sig8:
13415       return "DW_FORM_ref_sig8";
13416     default:
13417       return "DW_FORM_<unknown>";
13418     }
13419 }
13420
13421 /* Convert a DWARF stack opcode into its string name.  */
13422
13423 const char *
13424 dwarf_stack_op_name (unsigned op)
13425 {
13426   switch (op)
13427     {
13428     case DW_OP_addr:
13429       return "DW_OP_addr";
13430     case DW_OP_deref:
13431       return "DW_OP_deref";
13432     case DW_OP_const1u:
13433       return "DW_OP_const1u";
13434     case DW_OP_const1s:
13435       return "DW_OP_const1s";
13436     case DW_OP_const2u:
13437       return "DW_OP_const2u";
13438     case DW_OP_const2s:
13439       return "DW_OP_const2s";
13440     case DW_OP_const4u:
13441       return "DW_OP_const4u";
13442     case DW_OP_const4s:
13443       return "DW_OP_const4s";
13444     case DW_OP_const8u:
13445       return "DW_OP_const8u";
13446     case DW_OP_const8s:
13447       return "DW_OP_const8s";
13448     case DW_OP_constu:
13449       return "DW_OP_constu";
13450     case DW_OP_consts:
13451       return "DW_OP_consts";
13452     case DW_OP_dup:
13453       return "DW_OP_dup";
13454     case DW_OP_drop:
13455       return "DW_OP_drop";
13456     case DW_OP_over:
13457       return "DW_OP_over";
13458     case DW_OP_pick:
13459       return "DW_OP_pick";
13460     case DW_OP_swap:
13461       return "DW_OP_swap";
13462     case DW_OP_rot:
13463       return "DW_OP_rot";
13464     case DW_OP_xderef:
13465       return "DW_OP_xderef";
13466     case DW_OP_abs:
13467       return "DW_OP_abs";
13468     case DW_OP_and:
13469       return "DW_OP_and";
13470     case DW_OP_div:
13471       return "DW_OP_div";
13472     case DW_OP_minus:
13473       return "DW_OP_minus";
13474     case DW_OP_mod:
13475       return "DW_OP_mod";
13476     case DW_OP_mul:
13477       return "DW_OP_mul";
13478     case DW_OP_neg:
13479       return "DW_OP_neg";
13480     case DW_OP_not:
13481       return "DW_OP_not";
13482     case DW_OP_or:
13483       return "DW_OP_or";
13484     case DW_OP_plus:
13485       return "DW_OP_plus";
13486     case DW_OP_plus_uconst:
13487       return "DW_OP_plus_uconst";
13488     case DW_OP_shl:
13489       return "DW_OP_shl";
13490     case DW_OP_shr:
13491       return "DW_OP_shr";
13492     case DW_OP_shra:
13493       return "DW_OP_shra";
13494     case DW_OP_xor:
13495       return "DW_OP_xor";
13496     case DW_OP_bra:
13497       return "DW_OP_bra";
13498     case DW_OP_eq:
13499       return "DW_OP_eq";
13500     case DW_OP_ge:
13501       return "DW_OP_ge";
13502     case DW_OP_gt:
13503       return "DW_OP_gt";
13504     case DW_OP_le:
13505       return "DW_OP_le";
13506     case DW_OP_lt:
13507       return "DW_OP_lt";
13508     case DW_OP_ne:
13509       return "DW_OP_ne";
13510     case DW_OP_skip:
13511       return "DW_OP_skip";
13512     case DW_OP_lit0:
13513       return "DW_OP_lit0";
13514     case DW_OP_lit1:
13515       return "DW_OP_lit1";
13516     case DW_OP_lit2:
13517       return "DW_OP_lit2";
13518     case DW_OP_lit3:
13519       return "DW_OP_lit3";
13520     case DW_OP_lit4:
13521       return "DW_OP_lit4";
13522     case DW_OP_lit5:
13523       return "DW_OP_lit5";
13524     case DW_OP_lit6:
13525       return "DW_OP_lit6";
13526     case DW_OP_lit7:
13527       return "DW_OP_lit7";
13528     case DW_OP_lit8:
13529       return "DW_OP_lit8";
13530     case DW_OP_lit9:
13531       return "DW_OP_lit9";
13532     case DW_OP_lit10:
13533       return "DW_OP_lit10";
13534     case DW_OP_lit11:
13535       return "DW_OP_lit11";
13536     case DW_OP_lit12:
13537       return "DW_OP_lit12";
13538     case DW_OP_lit13:
13539       return "DW_OP_lit13";
13540     case DW_OP_lit14:
13541       return "DW_OP_lit14";
13542     case DW_OP_lit15:
13543       return "DW_OP_lit15";
13544     case DW_OP_lit16:
13545       return "DW_OP_lit16";
13546     case DW_OP_lit17:
13547       return "DW_OP_lit17";
13548     case DW_OP_lit18:
13549       return "DW_OP_lit18";
13550     case DW_OP_lit19:
13551       return "DW_OP_lit19";
13552     case DW_OP_lit20:
13553       return "DW_OP_lit20";
13554     case DW_OP_lit21:
13555       return "DW_OP_lit21";
13556     case DW_OP_lit22:
13557       return "DW_OP_lit22";
13558     case DW_OP_lit23:
13559       return "DW_OP_lit23";
13560     case DW_OP_lit24:
13561       return "DW_OP_lit24";
13562     case DW_OP_lit25:
13563       return "DW_OP_lit25";
13564     case DW_OP_lit26:
13565       return "DW_OP_lit26";
13566     case DW_OP_lit27:
13567       return "DW_OP_lit27";
13568     case DW_OP_lit28:
13569       return "DW_OP_lit28";
13570     case DW_OP_lit29:
13571       return "DW_OP_lit29";
13572     case DW_OP_lit30:
13573       return "DW_OP_lit30";
13574     case DW_OP_lit31:
13575       return "DW_OP_lit31";
13576     case DW_OP_reg0:
13577       return "DW_OP_reg0";
13578     case DW_OP_reg1:
13579       return "DW_OP_reg1";
13580     case DW_OP_reg2:
13581       return "DW_OP_reg2";
13582     case DW_OP_reg3:
13583       return "DW_OP_reg3";
13584     case DW_OP_reg4:
13585       return "DW_OP_reg4";
13586     case DW_OP_reg5:
13587       return "DW_OP_reg5";
13588     case DW_OP_reg6:
13589       return "DW_OP_reg6";
13590     case DW_OP_reg7:
13591       return "DW_OP_reg7";
13592     case DW_OP_reg8:
13593       return "DW_OP_reg8";
13594     case DW_OP_reg9:
13595       return "DW_OP_reg9";
13596     case DW_OP_reg10:
13597       return "DW_OP_reg10";
13598     case DW_OP_reg11:
13599       return "DW_OP_reg11";
13600     case DW_OP_reg12:
13601       return "DW_OP_reg12";
13602     case DW_OP_reg13:
13603       return "DW_OP_reg13";
13604     case DW_OP_reg14:
13605       return "DW_OP_reg14";
13606     case DW_OP_reg15:
13607       return "DW_OP_reg15";
13608     case DW_OP_reg16:
13609       return "DW_OP_reg16";
13610     case DW_OP_reg17:
13611       return "DW_OP_reg17";
13612     case DW_OP_reg18:
13613       return "DW_OP_reg18";
13614     case DW_OP_reg19:
13615       return "DW_OP_reg19";
13616     case DW_OP_reg20:
13617       return "DW_OP_reg20";
13618     case DW_OP_reg21:
13619       return "DW_OP_reg21";
13620     case DW_OP_reg22:
13621       return "DW_OP_reg22";
13622     case DW_OP_reg23:
13623       return "DW_OP_reg23";
13624     case DW_OP_reg24:
13625       return "DW_OP_reg24";
13626     case DW_OP_reg25:
13627       return "DW_OP_reg25";
13628     case DW_OP_reg26:
13629       return "DW_OP_reg26";
13630     case DW_OP_reg27:
13631       return "DW_OP_reg27";
13632     case DW_OP_reg28:
13633       return "DW_OP_reg28";
13634     case DW_OP_reg29:
13635       return "DW_OP_reg29";
13636     case DW_OP_reg30:
13637       return "DW_OP_reg30";
13638     case DW_OP_reg31:
13639       return "DW_OP_reg31";
13640     case DW_OP_breg0:
13641       return "DW_OP_breg0";
13642     case DW_OP_breg1:
13643       return "DW_OP_breg1";
13644     case DW_OP_breg2:
13645       return "DW_OP_breg2";
13646     case DW_OP_breg3:
13647       return "DW_OP_breg3";
13648     case DW_OP_breg4:
13649       return "DW_OP_breg4";
13650     case DW_OP_breg5:
13651       return "DW_OP_breg5";
13652     case DW_OP_breg6:
13653       return "DW_OP_breg6";
13654     case DW_OP_breg7:
13655       return "DW_OP_breg7";
13656     case DW_OP_breg8:
13657       return "DW_OP_breg8";
13658     case DW_OP_breg9:
13659       return "DW_OP_breg9";
13660     case DW_OP_breg10:
13661       return "DW_OP_breg10";
13662     case DW_OP_breg11:
13663       return "DW_OP_breg11";
13664     case DW_OP_breg12:
13665       return "DW_OP_breg12";
13666     case DW_OP_breg13:
13667       return "DW_OP_breg13";
13668     case DW_OP_breg14:
13669       return "DW_OP_breg14";
13670     case DW_OP_breg15:
13671       return "DW_OP_breg15";
13672     case DW_OP_breg16:
13673       return "DW_OP_breg16";
13674     case DW_OP_breg17:
13675       return "DW_OP_breg17";
13676     case DW_OP_breg18:
13677       return "DW_OP_breg18";
13678     case DW_OP_breg19:
13679       return "DW_OP_breg19";
13680     case DW_OP_breg20:
13681       return "DW_OP_breg20";
13682     case DW_OP_breg21:
13683       return "DW_OP_breg21";
13684     case DW_OP_breg22:
13685       return "DW_OP_breg22";
13686     case DW_OP_breg23:
13687       return "DW_OP_breg23";
13688     case DW_OP_breg24:
13689       return "DW_OP_breg24";
13690     case DW_OP_breg25:
13691       return "DW_OP_breg25";
13692     case DW_OP_breg26:
13693       return "DW_OP_breg26";
13694     case DW_OP_breg27:
13695       return "DW_OP_breg27";
13696     case DW_OP_breg28:
13697       return "DW_OP_breg28";
13698     case DW_OP_breg29:
13699       return "DW_OP_breg29";
13700     case DW_OP_breg30:
13701       return "DW_OP_breg30";
13702     case DW_OP_breg31:
13703       return "DW_OP_breg31";
13704     case DW_OP_regx:
13705       return "DW_OP_regx";
13706     case DW_OP_fbreg:
13707       return "DW_OP_fbreg";
13708     case DW_OP_bregx:
13709       return "DW_OP_bregx";
13710     case DW_OP_piece:
13711       return "DW_OP_piece";
13712     case DW_OP_deref_size:
13713       return "DW_OP_deref_size";
13714     case DW_OP_xderef_size:
13715       return "DW_OP_xderef_size";
13716     case DW_OP_nop:
13717       return "DW_OP_nop";
13718     /* DWARF 3 extensions.  */
13719     case DW_OP_push_object_address:
13720       return "DW_OP_push_object_address";
13721     case DW_OP_call2:
13722       return "DW_OP_call2";
13723     case DW_OP_call4:
13724       return "DW_OP_call4";
13725     case DW_OP_call_ref:
13726       return "DW_OP_call_ref";
13727     case DW_OP_form_tls_address:
13728       return "DW_OP_form_tls_address";
13729     case DW_OP_call_frame_cfa:
13730       return "DW_OP_call_frame_cfa";
13731     case DW_OP_bit_piece:
13732       return "DW_OP_bit_piece";
13733     /* DWARF 4 extensions.  */
13734     case DW_OP_implicit_value:
13735       return "DW_OP_implicit_value";
13736     case DW_OP_stack_value:
13737       return "DW_OP_stack_value";
13738     /* GNU extensions.  */
13739     case DW_OP_GNU_push_tls_address:
13740       return "DW_OP_GNU_push_tls_address";
13741     case DW_OP_GNU_uninit:
13742       return "DW_OP_GNU_uninit";
13743     case DW_OP_GNU_encoded_addr:
13744       return "DW_OP_GNU_encoded_addr";
13745     case DW_OP_GNU_implicit_pointer:
13746       return "DW_OP_GNU_implicit_pointer";
13747     case DW_OP_GNU_entry_value:
13748       return "DW_OP_GNU_entry_value";
13749     case DW_OP_GNU_const_type:
13750       return "DW_OP_GNU_const_type";
13751     case DW_OP_GNU_regval_type:
13752       return "DW_OP_GNU_regval_type";
13753     case DW_OP_GNU_deref_type:
13754       return "DW_OP_GNU_deref_type";
13755     case DW_OP_GNU_convert:
13756       return "DW_OP_GNU_convert";
13757     case DW_OP_GNU_reinterpret:
13758       return "DW_OP_GNU_reinterpret";
13759     case DW_OP_GNU_parameter_ref:
13760       return "DW_OP_GNU_parameter_ref";
13761     default:
13762       return NULL;
13763     }
13764 }
13765
13766 static char *
13767 dwarf_bool_name (unsigned mybool)
13768 {
13769   if (mybool)
13770     return "TRUE";
13771   else
13772     return "FALSE";
13773 }
13774
13775 /* Convert a DWARF type code into its string name.  */
13776
13777 static char *
13778 dwarf_type_encoding_name (unsigned enc)
13779 {
13780   switch (enc)
13781     {
13782     case DW_ATE_void:
13783       return "DW_ATE_void";
13784     case DW_ATE_address:
13785       return "DW_ATE_address";
13786     case DW_ATE_boolean:
13787       return "DW_ATE_boolean";
13788     case DW_ATE_complex_float:
13789       return "DW_ATE_complex_float";
13790     case DW_ATE_float:
13791       return "DW_ATE_float";
13792     case DW_ATE_signed:
13793       return "DW_ATE_signed";
13794     case DW_ATE_signed_char:
13795       return "DW_ATE_signed_char";
13796     case DW_ATE_unsigned:
13797       return "DW_ATE_unsigned";
13798     case DW_ATE_unsigned_char:
13799       return "DW_ATE_unsigned_char";
13800     /* DWARF 3.  */
13801     case DW_ATE_imaginary_float:
13802       return "DW_ATE_imaginary_float";
13803     case DW_ATE_packed_decimal:
13804       return "DW_ATE_packed_decimal";
13805     case DW_ATE_numeric_string:
13806       return "DW_ATE_numeric_string";
13807     case DW_ATE_edited:
13808       return "DW_ATE_edited";
13809     case DW_ATE_signed_fixed:
13810       return "DW_ATE_signed_fixed";
13811     case DW_ATE_unsigned_fixed:
13812       return "DW_ATE_unsigned_fixed";
13813     case DW_ATE_decimal_float:
13814       return "DW_ATE_decimal_float";
13815     /* DWARF 4.  */
13816     case DW_ATE_UTF:
13817       return "DW_ATE_UTF";
13818     /* HP extensions.  */
13819     case DW_ATE_HP_float80:
13820       return "DW_ATE_HP_float80";
13821     case DW_ATE_HP_complex_float80:
13822       return "DW_ATE_HP_complex_float80";
13823     case DW_ATE_HP_float128:
13824       return "DW_ATE_HP_float128";
13825     case DW_ATE_HP_complex_float128:
13826       return "DW_ATE_HP_complex_float128";
13827     case DW_ATE_HP_floathpintel:
13828       return "DW_ATE_HP_floathpintel";
13829     case DW_ATE_HP_imaginary_float80:
13830       return "DW_ATE_HP_imaginary_float80";
13831     case DW_ATE_HP_imaginary_float128:
13832       return "DW_ATE_HP_imaginary_float128";
13833     default:
13834       return "DW_ATE_<unknown>";
13835     }
13836 }
13837
13838 /* Convert a DWARF call frame info operation to its string name.  */
13839
13840 #if 0
13841 static char *
13842 dwarf_cfi_name (unsigned cfi_opc)
13843 {
13844   switch (cfi_opc)
13845     {
13846     case DW_CFA_advance_loc:
13847       return "DW_CFA_advance_loc";
13848     case DW_CFA_offset:
13849       return "DW_CFA_offset";
13850     case DW_CFA_restore:
13851       return "DW_CFA_restore";
13852     case DW_CFA_nop:
13853       return "DW_CFA_nop";
13854     case DW_CFA_set_loc:
13855       return "DW_CFA_set_loc";
13856     case DW_CFA_advance_loc1:
13857       return "DW_CFA_advance_loc1";
13858     case DW_CFA_advance_loc2:
13859       return "DW_CFA_advance_loc2";
13860     case DW_CFA_advance_loc4:
13861       return "DW_CFA_advance_loc4";
13862     case DW_CFA_offset_extended:
13863       return "DW_CFA_offset_extended";
13864     case DW_CFA_restore_extended:
13865       return "DW_CFA_restore_extended";
13866     case DW_CFA_undefined:
13867       return "DW_CFA_undefined";
13868     case DW_CFA_same_value:
13869       return "DW_CFA_same_value";
13870     case DW_CFA_register:
13871       return "DW_CFA_register";
13872     case DW_CFA_remember_state:
13873       return "DW_CFA_remember_state";
13874     case DW_CFA_restore_state:
13875       return "DW_CFA_restore_state";
13876     case DW_CFA_def_cfa:
13877       return "DW_CFA_def_cfa";
13878     case DW_CFA_def_cfa_register:
13879       return "DW_CFA_def_cfa_register";
13880     case DW_CFA_def_cfa_offset:
13881       return "DW_CFA_def_cfa_offset";
13882     /* DWARF 3.  */
13883     case DW_CFA_def_cfa_expression:
13884       return "DW_CFA_def_cfa_expression";
13885     case DW_CFA_expression:
13886       return "DW_CFA_expression";
13887     case DW_CFA_offset_extended_sf:
13888       return "DW_CFA_offset_extended_sf";
13889     case DW_CFA_def_cfa_sf:
13890       return "DW_CFA_def_cfa_sf";
13891     case DW_CFA_def_cfa_offset_sf:
13892       return "DW_CFA_def_cfa_offset_sf";
13893     case DW_CFA_val_offset:
13894       return "DW_CFA_val_offset";
13895     case DW_CFA_val_offset_sf:
13896       return "DW_CFA_val_offset_sf";
13897     case DW_CFA_val_expression:
13898       return "DW_CFA_val_expression";
13899     /* SGI/MIPS specific.  */
13900     case DW_CFA_MIPS_advance_loc8:
13901       return "DW_CFA_MIPS_advance_loc8";
13902     /* GNU extensions.  */
13903     case DW_CFA_GNU_window_save:
13904       return "DW_CFA_GNU_window_save";
13905     case DW_CFA_GNU_args_size:
13906       return "DW_CFA_GNU_args_size";
13907     case DW_CFA_GNU_negative_offset_extended:
13908       return "DW_CFA_GNU_negative_offset_extended";
13909     default:
13910       return "DW_CFA_<unknown>";
13911     }
13912 }
13913 #endif
13914
13915 static void
13916 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13917 {
13918   unsigned int i;
13919
13920   print_spaces (indent, f);
13921   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13922            dwarf_tag_name (die->tag), die->abbrev, die->offset);
13923
13924   if (die->parent != NULL)
13925     {
13926       print_spaces (indent, f);
13927       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
13928                           die->parent->offset);
13929     }
13930
13931   print_spaces (indent, f);
13932   fprintf_unfiltered (f, "  has children: %s\n",
13933            dwarf_bool_name (die->child != NULL));
13934
13935   print_spaces (indent, f);
13936   fprintf_unfiltered (f, "  attributes:\n");
13937
13938   for (i = 0; i < die->num_attrs; ++i)
13939     {
13940       print_spaces (indent, f);
13941       fprintf_unfiltered (f, "    %s (%s) ",
13942                dwarf_attr_name (die->attrs[i].name),
13943                dwarf_form_name (die->attrs[i].form));
13944
13945       switch (die->attrs[i].form)
13946         {
13947         case DW_FORM_ref_addr:
13948         case DW_FORM_addr:
13949           fprintf_unfiltered (f, "address: ");
13950           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13951           break;
13952         case DW_FORM_block2:
13953         case DW_FORM_block4:
13954         case DW_FORM_block:
13955         case DW_FORM_block1:
13956           fprintf_unfiltered (f, "block: size %d",
13957                               DW_BLOCK (&die->attrs[i])->size);
13958           break;
13959         case DW_FORM_exprloc:
13960           fprintf_unfiltered (f, "expression: size %u",
13961                               DW_BLOCK (&die->attrs[i])->size);
13962           break;
13963         case DW_FORM_ref1:
13964         case DW_FORM_ref2:
13965         case DW_FORM_ref4:
13966           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13967                               (long) (DW_ADDR (&die->attrs[i])));
13968           break;
13969         case DW_FORM_data1:
13970         case DW_FORM_data2:
13971         case DW_FORM_data4:
13972         case DW_FORM_data8:
13973         case DW_FORM_udata:
13974         case DW_FORM_sdata:
13975           fprintf_unfiltered (f, "constant: %s",
13976                               pulongest (DW_UNSND (&die->attrs[i])));
13977           break;
13978         case DW_FORM_sec_offset:
13979           fprintf_unfiltered (f, "section offset: %s",
13980                               pulongest (DW_UNSND (&die->attrs[i])));
13981           break;
13982         case DW_FORM_ref_sig8:
13983           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13984             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13985                           DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13986           else
13987             fprintf_unfiltered (f, "signatured type, offset: unknown");
13988           break;
13989         case DW_FORM_string:
13990         case DW_FORM_strp:
13991           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13992                    DW_STRING (&die->attrs[i])
13993                    ? DW_STRING (&die->attrs[i]) : "",
13994                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13995           break;
13996         case DW_FORM_flag:
13997           if (DW_UNSND (&die->attrs[i]))
13998             fprintf_unfiltered (f, "flag: TRUE");
13999           else
14000             fprintf_unfiltered (f, "flag: FALSE");
14001           break;
14002         case DW_FORM_flag_present:
14003           fprintf_unfiltered (f, "flag: TRUE");
14004           break;
14005         case DW_FORM_indirect:
14006           /* The reader will have reduced the indirect form to
14007              the "base form" so this form should not occur.  */
14008           fprintf_unfiltered (f, 
14009                               "unexpected attribute form: DW_FORM_indirect");
14010           break;
14011         default:
14012           fprintf_unfiltered (f, "unsupported attribute form: %d.",
14013                    die->attrs[i].form);
14014           break;
14015         }
14016       fprintf_unfiltered (f, "\n");
14017     }
14018 }
14019
14020 static void
14021 dump_die_for_error (struct die_info *die)
14022 {
14023   dump_die_shallow (gdb_stderr, 0, die);
14024 }
14025
14026 static void
14027 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14028 {
14029   int indent = level * 4;
14030
14031   gdb_assert (die != NULL);
14032
14033   if (level >= max_level)
14034     return;
14035
14036   dump_die_shallow (f, indent, die);
14037
14038   if (die->child != NULL)
14039     {
14040       print_spaces (indent, f);
14041       fprintf_unfiltered (f, "  Children:");
14042       if (level + 1 < max_level)
14043         {
14044           fprintf_unfiltered (f, "\n");
14045           dump_die_1 (f, level + 1, max_level, die->child);
14046         }
14047       else
14048         {
14049           fprintf_unfiltered (f,
14050                               " [not printed, max nesting level reached]\n");
14051         }
14052     }
14053
14054   if (die->sibling != NULL && level > 0)
14055     {
14056       dump_die_1 (f, level, max_level, die->sibling);
14057     }
14058 }
14059
14060 /* This is called from the pdie macro in gdbinit.in.
14061    It's not static so gcc will keep a copy callable from gdb.  */
14062
14063 void
14064 dump_die (struct die_info *die, int max_level)
14065 {
14066   dump_die_1 (gdb_stdlog, 0, max_level, die);
14067 }
14068
14069 static void
14070 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14071 {
14072   void **slot;
14073
14074   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14075
14076   *slot = die;
14077 }
14078
14079 static int
14080 is_ref_attr (struct attribute *attr)
14081 {
14082   switch (attr->form)
14083     {
14084     case DW_FORM_ref_addr:
14085     case DW_FORM_ref1:
14086     case DW_FORM_ref2:
14087     case DW_FORM_ref4:
14088     case DW_FORM_ref8:
14089     case DW_FORM_ref_udata:
14090       return 1;
14091     default:
14092       return 0;
14093     }
14094 }
14095
14096 static unsigned int
14097 dwarf2_get_ref_die_offset (struct attribute *attr)
14098 {
14099   if (is_ref_attr (attr))
14100     return DW_ADDR (attr);
14101
14102   complaint (&symfile_complaints,
14103              _("unsupported die ref attribute form: '%s'"),
14104              dwarf_form_name (attr->form));
14105   return 0;
14106 }
14107
14108 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
14109  * the value held by the attribute is not constant.  */
14110
14111 static LONGEST
14112 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14113 {
14114   if (attr->form == DW_FORM_sdata)
14115     return DW_SND (attr);
14116   else if (attr->form == DW_FORM_udata
14117            || attr->form == DW_FORM_data1
14118            || attr->form == DW_FORM_data2
14119            || attr->form == DW_FORM_data4
14120            || attr->form == DW_FORM_data8)
14121     return DW_UNSND (attr);
14122   else
14123     {
14124       complaint (&symfile_complaints,
14125                  _("Attribute value is not a constant (%s)"),
14126                  dwarf_form_name (attr->form));
14127       return default_value;
14128     }
14129 }
14130
14131 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
14132    unit and add it to our queue.
14133    The result is non-zero if PER_CU was queued, otherwise the result is zero
14134    meaning either PER_CU is already queued or it is already loaded.  */
14135
14136 static int
14137 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14138                        struct dwarf2_per_cu_data *per_cu)
14139 {
14140   /* We may arrive here during partial symbol reading, if we need full
14141      DIEs to process an unusual case (e.g. template arguments).  Do
14142      not queue PER_CU, just tell our caller to load its DIEs.  */
14143   if (dwarf2_per_objfile->reading_partial_symbols)
14144     {
14145       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14146         return 1;
14147       return 0;
14148     }
14149
14150   /* Mark the dependence relation so that we don't flush PER_CU
14151      too early.  */
14152   dwarf2_add_dependence (this_cu, per_cu);
14153
14154   /* If it's already on the queue, we have nothing to do.  */
14155   if (per_cu->queued)
14156     return 0;
14157
14158   /* If the compilation unit is already loaded, just mark it as
14159      used.  */
14160   if (per_cu->cu != NULL)
14161     {
14162       per_cu->cu->last_used = 0;
14163       return 0;
14164     }
14165
14166   /* Add it to the queue.  */
14167   queue_comp_unit (per_cu);
14168
14169   return 1;
14170 }
14171
14172 /* Follow reference or signature attribute ATTR of SRC_DIE.
14173    On entry *REF_CU is the CU of SRC_DIE.
14174    On exit *REF_CU is the CU of the result.  */
14175
14176 static struct die_info *
14177 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14178                        struct dwarf2_cu **ref_cu)
14179 {
14180   struct die_info *die;
14181
14182   if (is_ref_attr (attr))
14183     die = follow_die_ref (src_die, attr, ref_cu);
14184   else if (attr->form == DW_FORM_ref_sig8)
14185     die = follow_die_sig (src_die, attr, ref_cu);
14186   else
14187     {
14188       dump_die_for_error (src_die);
14189       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14190              (*ref_cu)->objfile->name);
14191     }
14192
14193   return die;
14194 }
14195
14196 /* Follow reference OFFSET.
14197    On entry *REF_CU is the CU of the source die referencing OFFSET.
14198    On exit *REF_CU is the CU of the result.
14199    Returns NULL if OFFSET is invalid.  */
14200
14201 static struct die_info *
14202 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14203 {
14204   struct die_info temp_die;
14205   struct dwarf2_cu *target_cu, *cu = *ref_cu;
14206
14207   gdb_assert (cu->per_cu != NULL);
14208
14209   target_cu = cu;
14210
14211   if (cu->per_cu->debug_types_section)
14212     {
14213       /* .debug_types CUs cannot reference anything outside their CU.
14214          If they need to, they have to reference a signatured type via
14215          DW_FORM_ref_sig8.  */
14216       if (! offset_in_cu_p (&cu->header, offset))
14217         return NULL;
14218     }
14219   else if (! offset_in_cu_p (&cu->header, offset))
14220     {
14221       struct dwarf2_per_cu_data *per_cu;
14222
14223       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14224
14225       /* If necessary, add it to the queue and load its DIEs.  */
14226       if (maybe_queue_comp_unit (cu, per_cu))
14227         load_full_comp_unit (per_cu);
14228
14229       target_cu = per_cu->cu;
14230     }
14231   else if (cu->dies == NULL)
14232     {
14233       /* We're loading full DIEs during partial symbol reading.  */
14234       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14235       load_full_comp_unit (cu->per_cu);
14236     }
14237
14238   *ref_cu = target_cu;
14239   temp_die.offset = offset;
14240   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14241 }
14242
14243 /* Follow reference attribute ATTR of SRC_DIE.
14244    On entry *REF_CU is the CU of SRC_DIE.
14245    On exit *REF_CU is the CU of the result.  */
14246
14247 static struct die_info *
14248 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14249                 struct dwarf2_cu **ref_cu)
14250 {
14251   unsigned int offset = dwarf2_get_ref_die_offset (attr);
14252   struct dwarf2_cu *cu = *ref_cu;
14253   struct die_info *die;
14254
14255   die = follow_die_offset (offset, ref_cu);
14256   if (!die)
14257     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14258            "at 0x%x [in module %s]"),
14259            offset, src_die->offset, cu->objfile->name);
14260
14261   return die;
14262 }
14263
14264 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14265    Returned value is intended for DW_OP_call*.  Returned
14266    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
14267
14268 struct dwarf2_locexpr_baton
14269 dwarf2_fetch_die_location_block (unsigned int offset_in_cu,
14270                                  struct dwarf2_per_cu_data *per_cu,
14271                                  CORE_ADDR (*get_frame_pc) (void *baton),
14272                                  void *baton)
14273 {
14274   unsigned int offset = per_cu->offset + offset_in_cu;
14275   struct dwarf2_cu *cu;
14276   struct die_info *die;
14277   struct attribute *attr;
14278   struct dwarf2_locexpr_baton retval;
14279
14280   dw2_setup (per_cu->objfile);
14281
14282   if (per_cu->cu == NULL)
14283     load_cu (per_cu);
14284   cu = per_cu->cu;
14285
14286   die = follow_die_offset (offset, &cu);
14287   if (!die)
14288     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14289            offset, per_cu->objfile->name);
14290
14291   attr = dwarf2_attr (die, DW_AT_location, cu);
14292   if (!attr)
14293     {
14294       /* DWARF: "If there is no such attribute, then there is no effect.".
14295          DATA is ignored if SIZE is 0.  */
14296
14297       retval.data = NULL;
14298       retval.size = 0;
14299     }
14300   else if (attr_form_is_section_offset (attr))
14301     {
14302       struct dwarf2_loclist_baton loclist_baton;
14303       CORE_ADDR pc = (*get_frame_pc) (baton);
14304       size_t size;
14305
14306       fill_in_loclist_baton (cu, &loclist_baton, attr);
14307
14308       retval.data = dwarf2_find_location_expression (&loclist_baton,
14309                                                      &size, pc);
14310       retval.size = size;
14311     }
14312   else
14313     {
14314       if (!attr_form_is_block (attr))
14315         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14316                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14317                offset, per_cu->objfile->name);
14318
14319       retval.data = DW_BLOCK (attr)->data;
14320       retval.size = DW_BLOCK (attr)->size;
14321     }
14322   retval.per_cu = cu->per_cu;
14323
14324   age_cached_comp_units ();
14325
14326   return retval;
14327 }
14328
14329 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14330    PER_CU.  */
14331
14332 struct type *
14333 dwarf2_get_die_type (unsigned int die_offset,
14334                      struct dwarf2_per_cu_data *per_cu)
14335 {
14336   dw2_setup (per_cu->objfile);
14337   return get_die_type_at_offset (per_cu->offset + die_offset, per_cu);
14338 }
14339
14340 /* Follow the signature attribute ATTR in SRC_DIE.
14341    On entry *REF_CU is the CU of SRC_DIE.
14342    On exit *REF_CU is the CU of the result.  */
14343
14344 static struct die_info *
14345 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14346                 struct dwarf2_cu **ref_cu)
14347 {
14348   struct objfile *objfile = (*ref_cu)->objfile;
14349   struct die_info temp_die;
14350   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14351   struct dwarf2_cu *sig_cu;
14352   struct die_info *die;
14353
14354   /* sig_type will be NULL if the signatured type is missing from
14355      the debug info.  */
14356   if (sig_type == NULL)
14357     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14358              "at 0x%x [in module %s]"),
14359            src_die->offset, objfile->name);
14360
14361   /* If necessary, add it to the queue and load its DIEs.  */
14362
14363   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14364     read_signatured_type (sig_type);
14365
14366   gdb_assert (sig_type->per_cu.cu != NULL);
14367
14368   sig_cu = sig_type->per_cu.cu;
14369   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14370   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14371   if (die)
14372     {
14373       *ref_cu = sig_cu;
14374       return die;
14375     }
14376
14377   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14378          "from DIE at 0x%x [in module %s]"),
14379          sig_type->type_offset, src_die->offset, objfile->name);
14380 }
14381
14382 /* Given an offset of a signatured type, return its signatured_type.  */
14383
14384 static struct signatured_type *
14385 lookup_signatured_type_at_offset (struct objfile *objfile,
14386                                   struct dwarf2_section_info *section,
14387                                   unsigned int offset)
14388 {
14389   gdb_byte *info_ptr = section->buffer + offset;
14390   unsigned int length, initial_length_size;
14391   unsigned int sig_offset;
14392   struct signatured_type find_entry, *type_sig;
14393
14394   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14395   sig_offset = (initial_length_size
14396                 + 2 /*version*/
14397                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14398                 + 1 /*address_size*/);
14399   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14400   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14401
14402   /* This is only used to lookup previously recorded types.
14403      If we didn't find it, it's our bug.  */
14404   gdb_assert (type_sig != NULL);
14405   gdb_assert (offset == type_sig->per_cu.offset);
14406
14407   return type_sig;
14408 }
14409
14410 /* Load the DIEs associated with type unit PER_CU into memory.  */
14411
14412 static void
14413 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14414 {
14415   struct objfile *objfile = per_cu->objfile;
14416   struct dwarf2_section_info *sect = per_cu->debug_types_section;
14417   unsigned int offset = per_cu->offset;
14418   struct signatured_type *type_sig;
14419
14420   dwarf2_read_section (objfile, sect);
14421
14422   /* We have the section offset, but we need the signature to do the
14423      hash table lookup.  */
14424   /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14425      the signature to assert we found the right one.
14426      Ok, but it's a lot of work.  We should simplify things so any needed
14427      assert doesn't require all this clumsiness.  */
14428   type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14429
14430   gdb_assert (type_sig->per_cu.cu == NULL);
14431
14432   read_signatured_type (type_sig);
14433
14434   gdb_assert (type_sig->per_cu.cu != NULL);
14435 }
14436
14437 /* Read in a signatured type and build its CU and DIEs.  */
14438
14439 static void
14440 read_signatured_type (struct signatured_type *type_sig)
14441 {
14442   struct objfile *objfile = type_sig->per_cu.objfile;
14443   gdb_byte *types_ptr;
14444   struct die_reader_specs reader_specs;
14445   struct dwarf2_cu *cu;
14446   ULONGEST signature;
14447   struct cleanup *back_to, *free_cu_cleanup;
14448   struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14449
14450   dwarf2_read_section (objfile, section);
14451   types_ptr = section->buffer + type_sig->per_cu.offset;
14452
14453   gdb_assert (type_sig->per_cu.cu == NULL);
14454
14455   cu = xmalloc (sizeof (*cu));
14456   init_one_comp_unit (cu, &type_sig->per_cu);
14457
14458   /* If an error occurs while loading, release our storage.  */
14459   free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14460
14461   types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14462                                              &signature, NULL);
14463   gdb_assert (signature == type_sig->signature);
14464
14465   cu->die_hash
14466     = htab_create_alloc_ex (cu->header.length / 12,
14467                             die_hash,
14468                             die_eq,
14469                             NULL,
14470                             &cu->comp_unit_obstack,
14471                             hashtab_obstack_allocate,
14472                             dummy_obstack_deallocate);
14473
14474   dwarf2_read_abbrevs (cu);
14475   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14476
14477   init_cu_die_reader (&reader_specs, cu);
14478
14479   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14480                                     NULL /*parent*/);
14481
14482   /* We try not to read any attributes in this function, because not
14483      all CUs needed for references have been loaded yet, and symbol
14484      table processing isn't initialized.  But we have to set the CU language,
14485      or we won't be able to build types correctly.  */
14486   prepare_one_comp_unit (cu, cu->dies);
14487
14488   do_cleanups (back_to);
14489
14490   /* We've successfully allocated this compilation unit.  Let our caller
14491      clean it up when finished with it.  */
14492   discard_cleanups (free_cu_cleanup);
14493
14494   /* Link this TU into read_in_chain.  */
14495   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14496   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14497 }
14498
14499 /* Decode simple location descriptions.
14500    Given a pointer to a dwarf block that defines a location, compute
14501    the location and return the value.
14502
14503    NOTE drow/2003-11-18: This function is called in two situations
14504    now: for the address of static or global variables (partial symbols
14505    only) and for offsets into structures which are expected to be
14506    (more or less) constant.  The partial symbol case should go away,
14507    and only the constant case should remain.  That will let this
14508    function complain more accurately.  A few special modes are allowed
14509    without complaint for global variables (for instance, global
14510    register values and thread-local values).
14511
14512    A location description containing no operations indicates that the
14513    object is optimized out.  The return value is 0 for that case.
14514    FIXME drow/2003-11-16: No callers check for this case any more; soon all
14515    callers will only want a very basic result and this can become a
14516    complaint.
14517
14518    Note that stack[0] is unused except as a default error return.  */
14519
14520 static CORE_ADDR
14521 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14522 {
14523   struct objfile *objfile = cu->objfile;
14524   int i;
14525   int size = blk->size;
14526   gdb_byte *data = blk->data;
14527   CORE_ADDR stack[64];
14528   int stacki;
14529   unsigned int bytes_read, unsnd;
14530   gdb_byte op;
14531
14532   i = 0;
14533   stacki = 0;
14534   stack[stacki] = 0;
14535   stack[++stacki] = 0;
14536
14537   while (i < size)
14538     {
14539       op = data[i++];
14540       switch (op)
14541         {
14542         case DW_OP_lit0:
14543         case DW_OP_lit1:
14544         case DW_OP_lit2:
14545         case DW_OP_lit3:
14546         case DW_OP_lit4:
14547         case DW_OP_lit5:
14548         case DW_OP_lit6:
14549         case DW_OP_lit7:
14550         case DW_OP_lit8:
14551         case DW_OP_lit9:
14552         case DW_OP_lit10:
14553         case DW_OP_lit11:
14554         case DW_OP_lit12:
14555         case DW_OP_lit13:
14556         case DW_OP_lit14:
14557         case DW_OP_lit15:
14558         case DW_OP_lit16:
14559         case DW_OP_lit17:
14560         case DW_OP_lit18:
14561         case DW_OP_lit19:
14562         case DW_OP_lit20:
14563         case DW_OP_lit21:
14564         case DW_OP_lit22:
14565         case DW_OP_lit23:
14566         case DW_OP_lit24:
14567         case DW_OP_lit25:
14568         case DW_OP_lit26:
14569         case DW_OP_lit27:
14570         case DW_OP_lit28:
14571         case DW_OP_lit29:
14572         case DW_OP_lit30:
14573         case DW_OP_lit31:
14574           stack[++stacki] = op - DW_OP_lit0;
14575           break;
14576
14577         case DW_OP_reg0:
14578         case DW_OP_reg1:
14579         case DW_OP_reg2:
14580         case DW_OP_reg3:
14581         case DW_OP_reg4:
14582         case DW_OP_reg5:
14583         case DW_OP_reg6:
14584         case DW_OP_reg7:
14585         case DW_OP_reg8:
14586         case DW_OP_reg9:
14587         case DW_OP_reg10:
14588         case DW_OP_reg11:
14589         case DW_OP_reg12:
14590         case DW_OP_reg13:
14591         case DW_OP_reg14:
14592         case DW_OP_reg15:
14593         case DW_OP_reg16:
14594         case DW_OP_reg17:
14595         case DW_OP_reg18:
14596         case DW_OP_reg19:
14597         case DW_OP_reg20:
14598         case DW_OP_reg21:
14599         case DW_OP_reg22:
14600         case DW_OP_reg23:
14601         case DW_OP_reg24:
14602         case DW_OP_reg25:
14603         case DW_OP_reg26:
14604         case DW_OP_reg27:
14605         case DW_OP_reg28:
14606         case DW_OP_reg29:
14607         case DW_OP_reg30:
14608         case DW_OP_reg31:
14609           stack[++stacki] = op - DW_OP_reg0;
14610           if (i < size)
14611             dwarf2_complex_location_expr_complaint ();
14612           break;
14613
14614         case DW_OP_regx:
14615           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14616           i += bytes_read;
14617           stack[++stacki] = unsnd;
14618           if (i < size)
14619             dwarf2_complex_location_expr_complaint ();
14620           break;
14621
14622         case DW_OP_addr:
14623           stack[++stacki] = read_address (objfile->obfd, &data[i],
14624                                           cu, &bytes_read);
14625           i += bytes_read;
14626           break;
14627
14628         case DW_OP_const1u:
14629           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14630           i += 1;
14631           break;
14632
14633         case DW_OP_const1s:
14634           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14635           i += 1;
14636           break;
14637
14638         case DW_OP_const2u:
14639           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14640           i += 2;
14641           break;
14642
14643         case DW_OP_const2s:
14644           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14645           i += 2;
14646           break;
14647
14648         case DW_OP_const4u:
14649           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14650           i += 4;
14651           break;
14652
14653         case DW_OP_const4s:
14654           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14655           i += 4;
14656           break;
14657
14658         case DW_OP_const8u:
14659           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14660           i += 8;
14661           break;
14662
14663         case DW_OP_constu:
14664           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14665                                                   &bytes_read);
14666           i += bytes_read;
14667           break;
14668
14669         case DW_OP_consts:
14670           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14671           i += bytes_read;
14672           break;
14673
14674         case DW_OP_dup:
14675           stack[stacki + 1] = stack[stacki];
14676           stacki++;
14677           break;
14678
14679         case DW_OP_plus:
14680           stack[stacki - 1] += stack[stacki];
14681           stacki--;
14682           break;
14683
14684         case DW_OP_plus_uconst:
14685           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14686                                                  &bytes_read);
14687           i += bytes_read;
14688           break;
14689
14690         case DW_OP_minus:
14691           stack[stacki - 1] -= stack[stacki];
14692           stacki--;
14693           break;
14694
14695         case DW_OP_deref:
14696           /* If we're not the last op, then we definitely can't encode
14697              this using GDB's address_class enum.  This is valid for partial
14698              global symbols, although the variable's address will be bogus
14699              in the psymtab.  */
14700           if (i < size)
14701             dwarf2_complex_location_expr_complaint ();
14702           break;
14703
14704         case DW_OP_GNU_push_tls_address:
14705           /* The top of the stack has the offset from the beginning
14706              of the thread control block at which the variable is located.  */
14707           /* Nothing should follow this operator, so the top of stack would
14708              be returned.  */
14709           /* This is valid for partial global symbols, but the variable's
14710              address will be bogus in the psymtab.  Make it always at least
14711              non-zero to not look as a variable garbage collected by linker
14712              which have DW_OP_addr 0.  */
14713           if (i < size)
14714             dwarf2_complex_location_expr_complaint ();
14715           stack[stacki]++;
14716           break;
14717
14718         case DW_OP_GNU_uninit:
14719           break;
14720
14721         default:
14722           {
14723             const char *name = dwarf_stack_op_name (op);
14724
14725             if (name)
14726               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14727                          name);
14728             else
14729               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14730                          op);
14731           }
14732
14733           return (stack[stacki]);
14734         }
14735
14736       /* Enforce maximum stack depth of SIZE-1 to avoid writing
14737          outside of the allocated space.  Also enforce minimum>0.  */
14738       if (stacki >= ARRAY_SIZE (stack) - 1)
14739         {
14740           complaint (&symfile_complaints,
14741                      _("location description stack overflow"));
14742           return 0;
14743         }
14744
14745       if (stacki <= 0)
14746         {
14747           complaint (&symfile_complaints,
14748                      _("location description stack underflow"));
14749           return 0;
14750         }
14751     }
14752   return (stack[stacki]);
14753 }
14754
14755 /* memory allocation interface */
14756
14757 static struct dwarf_block *
14758 dwarf_alloc_block (struct dwarf2_cu *cu)
14759 {
14760   struct dwarf_block *blk;
14761
14762   blk = (struct dwarf_block *)
14763     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14764   return (blk);
14765 }
14766
14767 static struct abbrev_info *
14768 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14769 {
14770   struct abbrev_info *abbrev;
14771
14772   abbrev = (struct abbrev_info *)
14773     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14774   memset (abbrev, 0, sizeof (struct abbrev_info));
14775   return (abbrev);
14776 }
14777
14778 static struct die_info *
14779 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14780 {
14781   struct die_info *die;
14782   size_t size = sizeof (struct die_info);
14783
14784   if (num_attrs > 1)
14785     size += (num_attrs - 1) * sizeof (struct attribute);
14786
14787   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14788   memset (die, 0, sizeof (struct die_info));
14789   return (die);
14790 }
14791
14792 \f
14793 /* Macro support.  */
14794
14795 /* Return the full name of file number I in *LH's file name table.
14796    Use COMP_DIR as the name of the current directory of the
14797    compilation.  The result is allocated using xmalloc; the caller is
14798    responsible for freeing it.  */
14799 static char *
14800 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14801 {
14802   /* Is the file number a valid index into the line header's file name
14803      table?  Remember that file numbers start with one, not zero.  */
14804   if (1 <= file && file <= lh->num_file_names)
14805     {
14806       struct file_entry *fe = &lh->file_names[file - 1];
14807
14808       if (IS_ABSOLUTE_PATH (fe->name))
14809         return xstrdup (fe->name);
14810       else
14811         {
14812           const char *dir;
14813           int dir_len;
14814           char *full_name;
14815
14816           if (fe->dir_index)
14817             dir = lh->include_dirs[fe->dir_index - 1];
14818           else
14819             dir = comp_dir;
14820
14821           if (dir)
14822             {
14823               dir_len = strlen (dir);
14824               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14825               strcpy (full_name, dir);
14826               full_name[dir_len] = '/';
14827               strcpy (full_name + dir_len + 1, fe->name);
14828               return full_name;
14829             }
14830           else
14831             return xstrdup (fe->name);
14832         }
14833     }
14834   else
14835     {
14836       /* The compiler produced a bogus file number.  We can at least
14837          record the macro definitions made in the file, even if we
14838          won't be able to find the file by name.  */
14839       char fake_name[80];
14840
14841       sprintf (fake_name, "<bad macro file number %d>", file);
14842
14843       complaint (&symfile_complaints,
14844                  _("bad file number in macro information (%d)"),
14845                  file);
14846
14847       return xstrdup (fake_name);
14848     }
14849 }
14850
14851
14852 static struct macro_source_file *
14853 macro_start_file (int file, int line,
14854                   struct macro_source_file *current_file,
14855                   const char *comp_dir,
14856                   struct line_header *lh, struct objfile *objfile)
14857 {
14858   /* The full name of this source file.  */
14859   char *full_name = file_full_name (file, lh, comp_dir);
14860
14861   /* We don't create a macro table for this compilation unit
14862      at all until we actually get a filename.  */
14863   if (! pending_macros)
14864     pending_macros = new_macro_table (&objfile->objfile_obstack,
14865                                       objfile->macro_cache);
14866
14867   if (! current_file)
14868     /* If we have no current file, then this must be the start_file
14869        directive for the compilation unit's main source file.  */
14870     current_file = macro_set_main (pending_macros, full_name);
14871   else
14872     current_file = macro_include (current_file, line, full_name);
14873
14874   xfree (full_name);
14875
14876   return current_file;
14877 }
14878
14879
14880 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14881    followed by a null byte.  */
14882 static char *
14883 copy_string (const char *buf, int len)
14884 {
14885   char *s = xmalloc (len + 1);
14886
14887   memcpy (s, buf, len);
14888   s[len] = '\0';
14889   return s;
14890 }
14891
14892
14893 static const char *
14894 consume_improper_spaces (const char *p, const char *body)
14895 {
14896   if (*p == ' ')
14897     {
14898       complaint (&symfile_complaints,
14899                  _("macro definition contains spaces "
14900                    "in formal argument list:\n`%s'"),
14901                  body);
14902
14903       while (*p == ' ')
14904         p++;
14905     }
14906
14907   return p;
14908 }
14909
14910
14911 static void
14912 parse_macro_definition (struct macro_source_file *file, int line,
14913                         const char *body)
14914 {
14915   const char *p;
14916
14917   /* The body string takes one of two forms.  For object-like macro
14918      definitions, it should be:
14919
14920         <macro name> " " <definition>
14921
14922      For function-like macro definitions, it should be:
14923
14924         <macro name> "() " <definition>
14925      or
14926         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14927
14928      Spaces may appear only where explicitly indicated, and in the
14929      <definition>.
14930
14931      The Dwarf 2 spec says that an object-like macro's name is always
14932      followed by a space, but versions of GCC around March 2002 omit
14933      the space when the macro's definition is the empty string.
14934
14935      The Dwarf 2 spec says that there should be no spaces between the
14936      formal arguments in a function-like macro's formal argument list,
14937      but versions of GCC around March 2002 include spaces after the
14938      commas.  */
14939
14940
14941   /* Find the extent of the macro name.  The macro name is terminated
14942      by either a space or null character (for an object-like macro) or
14943      an opening paren (for a function-like macro).  */
14944   for (p = body; *p; p++)
14945     if (*p == ' ' || *p == '(')
14946       break;
14947
14948   if (*p == ' ' || *p == '\0')
14949     {
14950       /* It's an object-like macro.  */
14951       int name_len = p - body;
14952       char *name = copy_string (body, name_len);
14953       const char *replacement;
14954
14955       if (*p == ' ')
14956         replacement = body + name_len + 1;
14957       else
14958         {
14959           dwarf2_macro_malformed_definition_complaint (body);
14960           replacement = body + name_len;
14961         }
14962
14963       macro_define_object (file, line, name, replacement);
14964
14965       xfree (name);
14966     }
14967   else if (*p == '(')
14968     {
14969       /* It's a function-like macro.  */
14970       char *name = copy_string (body, p - body);
14971       int argc = 0;
14972       int argv_size = 1;
14973       char **argv = xmalloc (argv_size * sizeof (*argv));
14974
14975       p++;
14976
14977       p = consume_improper_spaces (p, body);
14978
14979       /* Parse the formal argument list.  */
14980       while (*p && *p != ')')
14981         {
14982           /* Find the extent of the current argument name.  */
14983           const char *arg_start = p;
14984
14985           while (*p && *p != ',' && *p != ')' && *p != ' ')
14986             p++;
14987
14988           if (! *p || p == arg_start)
14989             dwarf2_macro_malformed_definition_complaint (body);
14990           else
14991             {
14992               /* Make sure argv has room for the new argument.  */
14993               if (argc >= argv_size)
14994                 {
14995                   argv_size *= 2;
14996                   argv = xrealloc (argv, argv_size * sizeof (*argv));
14997                 }
14998
14999               argv[argc++] = copy_string (arg_start, p - arg_start);
15000             }
15001
15002           p = consume_improper_spaces (p, body);
15003
15004           /* Consume the comma, if present.  */
15005           if (*p == ',')
15006             {
15007               p++;
15008
15009               p = consume_improper_spaces (p, body);
15010             }
15011         }
15012
15013       if (*p == ')')
15014         {
15015           p++;
15016
15017           if (*p == ' ')
15018             /* Perfectly formed definition, no complaints.  */
15019             macro_define_function (file, line, name,
15020                                    argc, (const char **) argv,
15021                                    p + 1);
15022           else if (*p == '\0')
15023             {
15024               /* Complain, but do define it.  */
15025               dwarf2_macro_malformed_definition_complaint (body);
15026               macro_define_function (file, line, name,
15027                                      argc, (const char **) argv,
15028                                      p);
15029             }
15030           else
15031             /* Just complain.  */
15032             dwarf2_macro_malformed_definition_complaint (body);
15033         }
15034       else
15035         /* Just complain.  */
15036         dwarf2_macro_malformed_definition_complaint (body);
15037
15038       xfree (name);
15039       {
15040         int i;
15041
15042         for (i = 0; i < argc; i++)
15043           xfree (argv[i]);
15044       }
15045       xfree (argv);
15046     }
15047   else
15048     dwarf2_macro_malformed_definition_complaint (body);
15049 }
15050
15051 /* Skip some bytes from BYTES according to the form given in FORM.
15052    Returns the new pointer.  */
15053
15054 static gdb_byte *
15055 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15056                  enum dwarf_form form,
15057                  unsigned int offset_size,
15058                  struct dwarf2_section_info *section)
15059 {
15060   unsigned int bytes_read;
15061
15062   switch (form)
15063     {
15064     case DW_FORM_data1:
15065     case DW_FORM_flag:
15066       ++bytes;
15067       break;
15068
15069     case DW_FORM_data2:
15070       bytes += 2;
15071       break;
15072
15073     case DW_FORM_data4:
15074       bytes += 4;
15075       break;
15076
15077     case DW_FORM_data8:
15078       bytes += 8;
15079       break;
15080
15081     case DW_FORM_string:
15082       read_direct_string (abfd, bytes, &bytes_read);
15083       bytes += bytes_read;
15084       break;
15085
15086     case DW_FORM_sec_offset:
15087     case DW_FORM_strp:
15088       bytes += offset_size;
15089       break;
15090
15091     case DW_FORM_block:
15092       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15093       bytes += bytes_read;
15094       break;
15095
15096     case DW_FORM_block1:
15097       bytes += 1 + read_1_byte (abfd, bytes);
15098       break;
15099     case DW_FORM_block2:
15100       bytes += 2 + read_2_bytes (abfd, bytes);
15101       break;
15102     case DW_FORM_block4:
15103       bytes += 4 + read_4_bytes (abfd, bytes);
15104       break;
15105
15106     case DW_FORM_sdata:
15107     case DW_FORM_udata:
15108       bytes = skip_leb128 (abfd, bytes);
15109       break;
15110
15111     default:
15112       {
15113       complain:
15114         complaint (&symfile_complaints,
15115                    _("invalid form 0x%x in `%s'"),
15116                    form,
15117                    section->asection->name);
15118         return NULL;
15119       }
15120     }
15121
15122   return bytes;
15123 }
15124
15125 /* A helper for dwarf_decode_macros that handles skipping an unknown
15126    opcode.  Returns an updated pointer to the macro data buffer; or,
15127    on error, issues a complaint and returns NULL.  */
15128
15129 static gdb_byte *
15130 skip_unknown_opcode (unsigned int opcode,
15131                      gdb_byte **opcode_definitions,
15132                      gdb_byte *mac_ptr,
15133                      bfd *abfd,
15134                      unsigned int offset_size,
15135                      struct dwarf2_section_info *section)
15136 {
15137   unsigned int bytes_read, i;
15138   unsigned long arg;
15139   gdb_byte *defn;
15140
15141   if (opcode_definitions[opcode] == NULL)
15142     {
15143       complaint (&symfile_complaints,
15144                  _("unrecognized DW_MACFINO opcode 0x%x"),
15145                  opcode);
15146       return NULL;
15147     }
15148
15149   defn = opcode_definitions[opcode];
15150   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15151   defn += bytes_read;
15152
15153   for (i = 0; i < arg; ++i)
15154     {
15155       mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15156       if (mac_ptr == NULL)
15157         {
15158           /* skip_form_bytes already issued the complaint.  */
15159           return NULL;
15160         }
15161     }
15162
15163   return mac_ptr;
15164 }
15165
15166 /* A helper function which parses the header of a macro section.
15167    If the macro section is the extended (for now called "GNU") type,
15168    then this updates *OFFSET_SIZE.  Returns a pointer to just after
15169    the header, or issues a complaint and returns NULL on error.  */
15170
15171 static gdb_byte *
15172 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15173                           bfd *abfd,
15174                           gdb_byte *mac_ptr,
15175                           unsigned int *offset_size,
15176                           int section_is_gnu)
15177 {
15178   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15179
15180   if (section_is_gnu)
15181     {
15182       unsigned int version, flags;
15183
15184       version = read_2_bytes (abfd, mac_ptr);
15185       if (version != 4)
15186         {
15187           complaint (&symfile_complaints,
15188                      _("unrecognized version `%d' in .debug_macro section"),
15189                      version);
15190           return NULL;
15191         }
15192       mac_ptr += 2;
15193
15194       flags = read_1_byte (abfd, mac_ptr);
15195       ++mac_ptr;
15196       *offset_size = (flags & 1) ? 8 : 4;
15197
15198       if ((flags & 2) != 0)
15199         /* We don't need the line table offset.  */
15200         mac_ptr += *offset_size;
15201
15202       /* Vendor opcode descriptions.  */
15203       if ((flags & 4) != 0)
15204         {
15205           unsigned int i, count;
15206
15207           count = read_1_byte (abfd, mac_ptr);
15208           ++mac_ptr;
15209           for (i = 0; i < count; ++i)
15210             {
15211               unsigned int opcode, bytes_read;
15212               unsigned long arg;
15213
15214               opcode = read_1_byte (abfd, mac_ptr);
15215               ++mac_ptr;
15216               opcode_definitions[opcode] = mac_ptr;
15217               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15218               mac_ptr += bytes_read;
15219               mac_ptr += arg;
15220             }
15221         }
15222     }
15223
15224   return mac_ptr;
15225 }
15226
15227 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15228    including DW_MACRO_GNU_transparent_include.  */
15229
15230 static void
15231 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15232                           struct macro_source_file *current_file,
15233                           struct line_header *lh, char *comp_dir,
15234                           struct dwarf2_section_info *section,
15235                           int section_is_gnu,
15236                           unsigned int offset_size,
15237                           struct objfile *objfile,
15238                           htab_t include_hash)
15239 {
15240   enum dwarf_macro_record_type macinfo_type;
15241   int at_commandline;
15242   gdb_byte *opcode_definitions[256];
15243
15244   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15245                                       &offset_size, section_is_gnu);
15246   if (mac_ptr == NULL)
15247     {
15248       /* We already issued a complaint.  */
15249       return;
15250     }
15251
15252   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
15253      GDB is still reading the definitions from command line.  First
15254      DW_MACINFO_start_file will need to be ignored as it was already executed
15255      to create CURRENT_FILE for the main source holding also the command line
15256      definitions.  On first met DW_MACINFO_start_file this flag is reset to
15257      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
15258
15259   at_commandline = 1;
15260
15261   do
15262     {
15263       /* Do we at least have room for a macinfo type byte?  */
15264       if (mac_ptr >= mac_end)
15265         {
15266           dwarf2_macros_too_long_complaint (section);
15267           break;
15268         }
15269
15270       macinfo_type = read_1_byte (abfd, mac_ptr);
15271       mac_ptr++;
15272
15273       /* Note that we rely on the fact that the corresponding GNU and
15274          DWARF constants are the same.  */
15275       switch (macinfo_type)
15276         {
15277           /* A zero macinfo type indicates the end of the macro
15278              information.  */
15279         case 0:
15280           break;
15281
15282         case DW_MACRO_GNU_define:
15283         case DW_MACRO_GNU_undef:
15284         case DW_MACRO_GNU_define_indirect:
15285         case DW_MACRO_GNU_undef_indirect:
15286           {
15287             unsigned int bytes_read;
15288             int line;
15289             char *body;
15290             int is_define;
15291
15292             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15293             mac_ptr += bytes_read;
15294
15295             if (macinfo_type == DW_MACRO_GNU_define
15296                 || macinfo_type == DW_MACRO_GNU_undef)
15297               {
15298                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15299                 mac_ptr += bytes_read;
15300               }
15301             else
15302               {
15303                 LONGEST str_offset;
15304
15305                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15306                 mac_ptr += offset_size;
15307
15308                 body = read_indirect_string_at_offset (abfd, str_offset);
15309               }
15310
15311             is_define = (macinfo_type == DW_MACRO_GNU_define
15312                          || macinfo_type == DW_MACRO_GNU_define_indirect);
15313             if (! current_file)
15314               {
15315                 /* DWARF violation as no main source is present.  */
15316                 complaint (&symfile_complaints,
15317                            _("debug info with no main source gives macro %s "
15318                              "on line %d: %s"),
15319                            is_define ? _("definition") : _("undefinition"),
15320                            line, body);
15321                 break;
15322               }
15323             if ((line == 0 && !at_commandline)
15324                 || (line != 0 && at_commandline))
15325               complaint (&symfile_complaints,
15326                          _("debug info gives %s macro %s with %s line %d: %s"),
15327                          at_commandline ? _("command-line") : _("in-file"),
15328                          is_define ? _("definition") : _("undefinition"),
15329                          line == 0 ? _("zero") : _("non-zero"), line, body);
15330
15331             if (is_define)
15332               parse_macro_definition (current_file, line, body);
15333             else
15334               {
15335                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15336                             || macinfo_type == DW_MACRO_GNU_undef_indirect);
15337                 macro_undef (current_file, line, body);
15338               }
15339           }
15340           break;
15341
15342         case DW_MACRO_GNU_start_file:
15343           {
15344             unsigned int bytes_read;
15345             int line, file;
15346
15347             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15348             mac_ptr += bytes_read;
15349             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15350             mac_ptr += bytes_read;
15351
15352             if ((line == 0 && !at_commandline)
15353                 || (line != 0 && at_commandline))
15354               complaint (&symfile_complaints,
15355                          _("debug info gives source %d included "
15356                            "from %s at %s line %d"),
15357                          file, at_commandline ? _("command-line") : _("file"),
15358                          line == 0 ? _("zero") : _("non-zero"), line);
15359
15360             if (at_commandline)
15361               {
15362                 /* This DW_MACRO_GNU_start_file was executed in the
15363                    pass one.  */
15364                 at_commandline = 0;
15365               }
15366             else
15367               current_file = macro_start_file (file, line,
15368                                                current_file, comp_dir,
15369                                                lh, objfile);
15370           }
15371           break;
15372
15373         case DW_MACRO_GNU_end_file:
15374           if (! current_file)
15375             complaint (&symfile_complaints,
15376                        _("macro debug info has an unmatched "
15377                          "`close_file' directive"));
15378           else
15379             {
15380               current_file = current_file->included_by;
15381               if (! current_file)
15382                 {
15383                   enum dwarf_macro_record_type next_type;
15384
15385                   /* GCC circa March 2002 doesn't produce the zero
15386                      type byte marking the end of the compilation
15387                      unit.  Complain if it's not there, but exit no
15388                      matter what.  */
15389
15390                   /* Do we at least have room for a macinfo type byte?  */
15391                   if (mac_ptr >= mac_end)
15392                     {
15393                       dwarf2_macros_too_long_complaint (section);
15394                       return;
15395                     }
15396
15397                   /* We don't increment mac_ptr here, so this is just
15398                      a look-ahead.  */
15399                   next_type = read_1_byte (abfd, mac_ptr);
15400                   if (next_type != 0)
15401                     complaint (&symfile_complaints,
15402                                _("no terminating 0-type entry for "
15403                                  "macros in `.debug_macinfo' section"));
15404
15405                   return;
15406                 }
15407             }
15408           break;
15409
15410         case DW_MACRO_GNU_transparent_include:
15411           {
15412             LONGEST offset;
15413             void **slot;
15414
15415             offset = read_offset_1 (abfd, mac_ptr, offset_size);
15416             mac_ptr += offset_size;
15417
15418             slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15419             if (*slot != NULL)
15420               {
15421                 /* This has actually happened; see
15422                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
15423                 complaint (&symfile_complaints,
15424                            _("recursive DW_MACRO_GNU_transparent_include in "
15425                              ".debug_macro section"));
15426               }
15427             else
15428               {
15429                 *slot = mac_ptr;
15430
15431                 dwarf_decode_macro_bytes (abfd,
15432                                           section->buffer + offset,
15433                                           mac_end, current_file,
15434                                           lh, comp_dir,
15435                                           section, section_is_gnu,
15436                                           offset_size, objfile, include_hash);
15437
15438                 htab_remove_elt (include_hash, mac_ptr);
15439               }
15440           }
15441           break;
15442
15443         case DW_MACINFO_vendor_ext:
15444           if (!section_is_gnu)
15445             {
15446               unsigned int bytes_read;
15447               int constant;
15448
15449               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15450               mac_ptr += bytes_read;
15451               read_direct_string (abfd, mac_ptr, &bytes_read);
15452               mac_ptr += bytes_read;
15453
15454               /* We don't recognize any vendor extensions.  */
15455               break;
15456             }
15457           /* FALLTHROUGH */
15458
15459         default:
15460           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15461                                          mac_ptr, abfd, offset_size,
15462                                          section);
15463           if (mac_ptr == NULL)
15464             return;
15465           break;
15466         }
15467     } while (macinfo_type != 0);
15468 }
15469
15470 static void
15471 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15472                      char *comp_dir, bfd *abfd,
15473                      struct dwarf2_cu *cu,
15474                      struct dwarf2_section_info *section,
15475                      int section_is_gnu)
15476 {
15477   struct objfile *objfile = dwarf2_per_objfile->objfile;
15478   gdb_byte *mac_ptr, *mac_end;
15479   struct macro_source_file *current_file = 0;
15480   enum dwarf_macro_record_type macinfo_type;
15481   unsigned int offset_size = cu->header.offset_size;
15482   gdb_byte *opcode_definitions[256];
15483   struct cleanup *cleanup;
15484   htab_t include_hash;
15485   void **slot;
15486
15487   dwarf2_read_section (objfile, section);
15488   if (section->buffer == NULL)
15489     {
15490       complaint (&symfile_complaints, _("missing %s section"),
15491                  section->asection->name);
15492       return;
15493     }
15494
15495   /* First pass: Find the name of the base filename.
15496      This filename is needed in order to process all macros whose definition
15497      (or undefinition) comes from the command line.  These macros are defined
15498      before the first DW_MACINFO_start_file entry, and yet still need to be
15499      associated to the base file.
15500
15501      To determine the base file name, we scan the macro definitions until we
15502      reach the first DW_MACINFO_start_file entry.  We then initialize
15503      CURRENT_FILE accordingly so that any macro definition found before the
15504      first DW_MACINFO_start_file can still be associated to the base file.  */
15505
15506   mac_ptr = section->buffer + offset;
15507   mac_end = section->buffer + section->size;
15508
15509   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15510                                       &offset_size, section_is_gnu);
15511   if (mac_ptr == NULL)
15512     {
15513       /* We already issued a complaint.  */
15514       return;
15515     }
15516
15517   do
15518     {
15519       /* Do we at least have room for a macinfo type byte?  */
15520       if (mac_ptr >= mac_end)
15521         {
15522           /* Complaint is printed during the second pass as GDB will probably
15523              stop the first pass earlier upon finding
15524              DW_MACINFO_start_file.  */
15525           break;
15526         }
15527
15528       macinfo_type = read_1_byte (abfd, mac_ptr);
15529       mac_ptr++;
15530
15531       /* Note that we rely on the fact that the corresponding GNU and
15532          DWARF constants are the same.  */
15533       switch (macinfo_type)
15534         {
15535           /* A zero macinfo type indicates the end of the macro
15536              information.  */
15537         case 0:
15538           break;
15539
15540         case DW_MACRO_GNU_define:
15541         case DW_MACRO_GNU_undef:
15542           /* Only skip the data by MAC_PTR.  */
15543           {
15544             unsigned int bytes_read;
15545
15546             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15547             mac_ptr += bytes_read;
15548             read_direct_string (abfd, mac_ptr, &bytes_read);
15549             mac_ptr += bytes_read;
15550           }
15551           break;
15552
15553         case DW_MACRO_GNU_start_file:
15554           {
15555             unsigned int bytes_read;
15556             int line, file;
15557
15558             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15559             mac_ptr += bytes_read;
15560             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15561             mac_ptr += bytes_read;
15562
15563             current_file = macro_start_file (file, line, current_file,
15564                                              comp_dir, lh, objfile);
15565           }
15566           break;
15567
15568         case DW_MACRO_GNU_end_file:
15569           /* No data to skip by MAC_PTR.  */
15570           break;
15571
15572         case DW_MACRO_GNU_define_indirect:
15573         case DW_MACRO_GNU_undef_indirect:
15574           {
15575             unsigned int bytes_read;
15576
15577             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15578             mac_ptr += bytes_read;
15579             mac_ptr += offset_size;
15580           }
15581           break;
15582
15583         case DW_MACRO_GNU_transparent_include:
15584           /* Note that, according to the spec, a transparent include
15585              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
15586              skip this opcode.  */
15587           mac_ptr += offset_size;
15588           break;
15589
15590         case DW_MACINFO_vendor_ext:
15591           /* Only skip the data by MAC_PTR.  */
15592           if (!section_is_gnu)
15593             {
15594               unsigned int bytes_read;
15595
15596               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15597               mac_ptr += bytes_read;
15598               read_direct_string (abfd, mac_ptr, &bytes_read);
15599               mac_ptr += bytes_read;
15600             }
15601           /* FALLTHROUGH */
15602
15603         default:
15604           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15605                                          mac_ptr, abfd, offset_size,
15606                                          section);
15607           if (mac_ptr == NULL)
15608             return;
15609           break;
15610         }
15611     } while (macinfo_type != 0 && current_file == NULL);
15612
15613   /* Second pass: Process all entries.
15614
15615      Use the AT_COMMAND_LINE flag to determine whether we are still processing
15616      command-line macro definitions/undefinitions.  This flag is unset when we
15617      reach the first DW_MACINFO_start_file entry.  */
15618
15619   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15620                                     NULL, xcalloc, xfree);
15621   cleanup = make_cleanup_htab_delete (include_hash);
15622   mac_ptr = section->buffer + offset;
15623   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15624   *slot = mac_ptr;
15625   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15626                             current_file, lh, comp_dir, section, section_is_gnu,
15627                             offset_size, objfile, include_hash);
15628   do_cleanups (cleanup);
15629 }
15630
15631 /* Check if the attribute's form is a DW_FORM_block*
15632    if so return true else false.  */
15633 static int
15634 attr_form_is_block (struct attribute *attr)
15635 {
15636   return (attr == NULL ? 0 :
15637       attr->form == DW_FORM_block1
15638       || attr->form == DW_FORM_block2
15639       || attr->form == DW_FORM_block4
15640       || attr->form == DW_FORM_block
15641       || attr->form == DW_FORM_exprloc);
15642 }
15643
15644 /* Return non-zero if ATTR's value is a section offset --- classes
15645    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15646    You may use DW_UNSND (attr) to retrieve such offsets.
15647
15648    Section 7.5.4, "Attribute Encodings", explains that no attribute
15649    may have a value that belongs to more than one of these classes; it
15650    would be ambiguous if we did, because we use the same forms for all
15651    of them.  */
15652 static int
15653 attr_form_is_section_offset (struct attribute *attr)
15654 {
15655   return (attr->form == DW_FORM_data4
15656           || attr->form == DW_FORM_data8
15657           || attr->form == DW_FORM_sec_offset);
15658 }
15659
15660
15661 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15662    zero otherwise.  When this function returns true, you can apply
15663    dwarf2_get_attr_constant_value to it.
15664
15665    However, note that for some attributes you must check
15666    attr_form_is_section_offset before using this test.  DW_FORM_data4
15667    and DW_FORM_data8 are members of both the constant class, and of
15668    the classes that contain offsets into other debug sections
15669    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
15670    that, if an attribute's can be either a constant or one of the
15671    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15672    taken as section offsets, not constants.  */
15673 static int
15674 attr_form_is_constant (struct attribute *attr)
15675 {
15676   switch (attr->form)
15677     {
15678     case DW_FORM_sdata:
15679     case DW_FORM_udata:
15680     case DW_FORM_data1:
15681     case DW_FORM_data2:
15682     case DW_FORM_data4:
15683     case DW_FORM_data8:
15684       return 1;
15685     default:
15686       return 0;
15687     }
15688 }
15689
15690 /* A helper function that fills in a dwarf2_loclist_baton.  */
15691
15692 static void
15693 fill_in_loclist_baton (struct dwarf2_cu *cu,
15694                        struct dwarf2_loclist_baton *baton,
15695                        struct attribute *attr)
15696 {
15697   dwarf2_read_section (dwarf2_per_objfile->objfile,
15698                        &dwarf2_per_objfile->loc);
15699
15700   baton->per_cu = cu->per_cu;
15701   gdb_assert (baton->per_cu);
15702   /* We don't know how long the location list is, but make sure we
15703      don't run off the edge of the section.  */
15704   baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15705   baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15706   baton->base_address = cu->base_address;
15707 }
15708
15709 static void
15710 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15711                              struct dwarf2_cu *cu)
15712 {
15713   struct objfile *objfile = dwarf2_per_objfile->objfile;
15714
15715   if (attr_form_is_section_offset (attr)
15716       /* ".debug_loc" may not exist at all, or the offset may be outside
15717          the section.  If so, fall through to the complaint in the
15718          other branch.  */
15719       && DW_UNSND (attr) < dwarf2_section_size (objfile,
15720                                                 &dwarf2_per_objfile->loc))
15721     {
15722       struct dwarf2_loclist_baton *baton;
15723
15724       baton = obstack_alloc (&objfile->objfile_obstack,
15725                              sizeof (struct dwarf2_loclist_baton));
15726
15727       fill_in_loclist_baton (cu, baton, attr);
15728
15729       if (cu->base_known == 0)
15730         complaint (&symfile_complaints,
15731                    _("Location list used without "
15732                      "specifying the CU base address."));
15733
15734       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15735       SYMBOL_LOCATION_BATON (sym) = baton;
15736     }
15737   else
15738     {
15739       struct dwarf2_locexpr_baton *baton;
15740
15741       baton = obstack_alloc (&objfile->objfile_obstack,
15742                              sizeof (struct dwarf2_locexpr_baton));
15743       baton->per_cu = cu->per_cu;
15744       gdb_assert (baton->per_cu);
15745
15746       if (attr_form_is_block (attr))
15747         {
15748           /* Note that we're just copying the block's data pointer
15749              here, not the actual data.  We're still pointing into the
15750              info_buffer for SYM's objfile; right now we never release
15751              that buffer, but when we do clean up properly this may
15752              need to change.  */
15753           baton->size = DW_BLOCK (attr)->size;
15754           baton->data = DW_BLOCK (attr)->data;
15755         }
15756       else
15757         {
15758           dwarf2_invalid_attrib_class_complaint ("location description",
15759                                                  SYMBOL_NATURAL_NAME (sym));
15760           baton->size = 0;
15761         }
15762
15763       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15764       SYMBOL_LOCATION_BATON (sym) = baton;
15765     }
15766 }
15767
15768 /* Return the OBJFILE associated with the compilation unit CU.  If CU
15769    came from a separate debuginfo file, then the master objfile is
15770    returned.  */
15771
15772 struct objfile *
15773 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15774 {
15775   struct objfile *objfile = per_cu->objfile;
15776
15777   /* Return the master objfile, so that we can report and look up the
15778      correct file containing this variable.  */
15779   if (objfile->separate_debug_objfile_backlink)
15780     objfile = objfile->separate_debug_objfile_backlink;
15781
15782   return objfile;
15783 }
15784
15785 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15786    (CU_HEADERP is unused in such case) or prepare a temporary copy at
15787    CU_HEADERP first.  */
15788
15789 static const struct comp_unit_head *
15790 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15791                        struct dwarf2_per_cu_data *per_cu)
15792 {
15793   struct objfile *objfile;
15794   struct dwarf2_per_objfile *per_objfile;
15795   gdb_byte *info_ptr;
15796
15797   if (per_cu->cu)
15798     return &per_cu->cu->header;
15799
15800   objfile = per_cu->objfile;
15801   per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15802   info_ptr = per_objfile->info.buffer + per_cu->offset;
15803
15804   memset (cu_headerp, 0, sizeof (*cu_headerp));
15805   read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15806
15807   return cu_headerp;
15808 }
15809
15810 /* Return the address size given in the compilation unit header for CU.  */
15811
15812 int
15813 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15814 {
15815   struct comp_unit_head cu_header_local;
15816   const struct comp_unit_head *cu_headerp;
15817
15818   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15819
15820   return cu_headerp->addr_size;
15821 }
15822
15823 /* Return the offset size given in the compilation unit header for CU.  */
15824
15825 int
15826 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15827 {
15828   struct comp_unit_head cu_header_local;
15829   const struct comp_unit_head *cu_headerp;
15830
15831   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15832
15833   return cu_headerp->offset_size;
15834 }
15835
15836 /* See its dwarf2loc.h declaration.  */
15837
15838 int
15839 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15840 {
15841   struct comp_unit_head cu_header_local;
15842   const struct comp_unit_head *cu_headerp;
15843
15844   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15845
15846   if (cu_headerp->version == 2)
15847     return cu_headerp->addr_size;
15848   else
15849     return cu_headerp->offset_size;
15850 }
15851
15852 /* Return the text offset of the CU.  The returned offset comes from
15853    this CU's objfile.  If this objfile came from a separate debuginfo
15854    file, then the offset may be different from the corresponding
15855    offset in the parent objfile.  */
15856
15857 CORE_ADDR
15858 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15859 {
15860   struct objfile *objfile = per_cu->objfile;
15861
15862   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15863 }
15864
15865 /* Locate the .debug_info compilation unit from CU's objfile which contains
15866    the DIE at OFFSET.  Raises an error on failure.  */
15867
15868 static struct dwarf2_per_cu_data *
15869 dwarf2_find_containing_comp_unit (unsigned int offset,
15870                                   struct objfile *objfile)
15871 {
15872   struct dwarf2_per_cu_data *this_cu;
15873   int low, high;
15874
15875   low = 0;
15876   high = dwarf2_per_objfile->n_comp_units - 1;
15877   while (high > low)
15878     {
15879       int mid = low + (high - low) / 2;
15880
15881       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15882         high = mid;
15883       else
15884         low = mid + 1;
15885     }
15886   gdb_assert (low == high);
15887   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15888     {
15889       if (low == 0)
15890         error (_("Dwarf Error: could not find partial DIE containing "
15891                "offset 0x%lx [in module %s]"),
15892                (long) offset, bfd_get_filename (objfile->obfd));
15893
15894       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15895       return dwarf2_per_objfile->all_comp_units[low-1];
15896     }
15897   else
15898     {
15899       this_cu = dwarf2_per_objfile->all_comp_units[low];
15900       if (low == dwarf2_per_objfile->n_comp_units - 1
15901           && offset >= this_cu->offset + this_cu->length)
15902         error (_("invalid dwarf2 offset %u"), offset);
15903       gdb_assert (offset < this_cu->offset + this_cu->length);
15904       return this_cu;
15905     }
15906 }
15907
15908 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
15909
15910 static void
15911 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15912 {
15913   memset (cu, 0, sizeof (*cu));
15914   per_cu->cu = cu;
15915   cu->per_cu = per_cu;
15916   cu->objfile = per_cu->objfile;
15917   obstack_init (&cu->comp_unit_obstack);
15918 }
15919
15920 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
15921
15922 static void
15923 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15924 {
15925   struct attribute *attr;
15926
15927   /* Set the language we're debugging.  */
15928   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15929   if (attr)
15930     set_cu_language (DW_UNSND (attr), cu);
15931   else
15932     {
15933       cu->language = language_minimal;
15934       cu->language_defn = language_def (cu->language);
15935     }
15936 }
15937
15938 /* Release one cached compilation unit, CU.  We unlink it from the tree
15939    of compilation units, but we don't remove it from the read_in_chain;
15940    the caller is responsible for that.
15941    NOTE: DATA is a void * because this function is also used as a
15942    cleanup routine.  */
15943
15944 static void
15945 free_heap_comp_unit (void *data)
15946 {
15947   struct dwarf2_cu *cu = data;
15948
15949   gdb_assert (cu->per_cu != NULL);
15950   cu->per_cu->cu = NULL;
15951   cu->per_cu = NULL;
15952
15953   obstack_free (&cu->comp_unit_obstack, NULL);
15954
15955   xfree (cu);
15956 }
15957
15958 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15959    when we're finished with it.  We can't free the pointer itself, but be
15960    sure to unlink it from the cache.  Also release any associated storage
15961    and perform cache maintenance.
15962
15963    Only used during partial symbol parsing.  */
15964
15965 static void
15966 free_stack_comp_unit (void *data)
15967 {
15968   struct dwarf2_cu *cu = data;
15969
15970   gdb_assert (cu->per_cu != NULL);
15971   cu->per_cu->cu = NULL;
15972   cu->per_cu = NULL;
15973
15974   obstack_free (&cu->comp_unit_obstack, NULL);
15975   cu->partial_dies = NULL;
15976
15977   /* The previous code only did this if per_cu != NULL.
15978      But that would always succeed, so now we just unconditionally do
15979      the aging.  This seems like the wrong place to do such aging,
15980      but cleaning that up is left for later.  */
15981   age_cached_comp_units ();
15982 }
15983
15984 /* Free all cached compilation units.  */
15985
15986 static void
15987 free_cached_comp_units (void *data)
15988 {
15989   struct dwarf2_per_cu_data *per_cu, **last_chain;
15990
15991   per_cu = dwarf2_per_objfile->read_in_chain;
15992   last_chain = &dwarf2_per_objfile->read_in_chain;
15993   while (per_cu != NULL)
15994     {
15995       struct dwarf2_per_cu_data *next_cu;
15996
15997       next_cu = per_cu->cu->read_in_chain;
15998
15999       free_heap_comp_unit (per_cu->cu);
16000       *last_chain = next_cu;
16001
16002       per_cu = next_cu;
16003     }
16004 }
16005
16006 /* Increase the age counter on each cached compilation unit, and free
16007    any that are too old.  */
16008
16009 static void
16010 age_cached_comp_units (void)
16011 {
16012   struct dwarf2_per_cu_data *per_cu, **last_chain;
16013
16014   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16015   per_cu = dwarf2_per_objfile->read_in_chain;
16016   while (per_cu != NULL)
16017     {
16018       per_cu->cu->last_used ++;
16019       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16020         dwarf2_mark (per_cu->cu);
16021       per_cu = per_cu->cu->read_in_chain;
16022     }
16023
16024   per_cu = dwarf2_per_objfile->read_in_chain;
16025   last_chain = &dwarf2_per_objfile->read_in_chain;
16026   while (per_cu != NULL)
16027     {
16028       struct dwarf2_per_cu_data *next_cu;
16029
16030       next_cu = per_cu->cu->read_in_chain;
16031
16032       if (!per_cu->cu->mark)
16033         {
16034           free_heap_comp_unit (per_cu->cu);
16035           *last_chain = next_cu;
16036         }
16037       else
16038         last_chain = &per_cu->cu->read_in_chain;
16039
16040       per_cu = next_cu;
16041     }
16042 }
16043
16044 /* Remove a single compilation unit from the cache.  */
16045
16046 static void
16047 free_one_cached_comp_unit (void *target_cu)
16048 {
16049   struct dwarf2_per_cu_data *per_cu, **last_chain;
16050
16051   per_cu = dwarf2_per_objfile->read_in_chain;
16052   last_chain = &dwarf2_per_objfile->read_in_chain;
16053   while (per_cu != NULL)
16054     {
16055       struct dwarf2_per_cu_data *next_cu;
16056
16057       next_cu = per_cu->cu->read_in_chain;
16058
16059       if (per_cu->cu == target_cu)
16060         {
16061           free_heap_comp_unit (per_cu->cu);
16062           *last_chain = next_cu;
16063           break;
16064         }
16065       else
16066         last_chain = &per_cu->cu->read_in_chain;
16067
16068       per_cu = next_cu;
16069     }
16070 }
16071
16072 /* Release all extra memory associated with OBJFILE.  */
16073
16074 void
16075 dwarf2_free_objfile (struct objfile *objfile)
16076 {
16077   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16078
16079   if (dwarf2_per_objfile == NULL)
16080     return;
16081
16082   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
16083   free_cached_comp_units (NULL);
16084
16085   if (dwarf2_per_objfile->quick_file_names_table)
16086     htab_delete (dwarf2_per_objfile->quick_file_names_table);
16087
16088   /* Everything else should be on the objfile obstack.  */
16089 }
16090
16091 /* A pair of DIE offset and GDB type pointer.  We store these
16092    in a hash table separate from the DIEs, and preserve them
16093    when the DIEs are flushed out of cache.  */
16094
16095 struct dwarf2_offset_and_type
16096 {
16097   unsigned int offset;
16098   struct type *type;
16099 };
16100
16101 /* Hash function for a dwarf2_offset_and_type.  */
16102
16103 static hashval_t
16104 offset_and_type_hash (const void *item)
16105 {
16106   const struct dwarf2_offset_and_type *ofs = item;
16107
16108   return ofs->offset;
16109 }
16110
16111 /* Equality function for a dwarf2_offset_and_type.  */
16112
16113 static int
16114 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16115 {
16116   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16117   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16118
16119   return ofs_lhs->offset == ofs_rhs->offset;
16120 }
16121
16122 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
16123    table if necessary.  For convenience, return TYPE.
16124
16125    The DIEs reading must have careful ordering to:
16126     * Not cause infite loops trying to read in DIEs as a prerequisite for
16127       reading current DIE.
16128     * Not trying to dereference contents of still incompletely read in types
16129       while reading in other DIEs.
16130     * Enable referencing still incompletely read in types just by a pointer to
16131       the type without accessing its fields.
16132
16133    Therefore caller should follow these rules:
16134      * Try to fetch any prerequisite types we may need to build this DIE type
16135        before building the type and calling set_die_type.
16136      * After building type call set_die_type for current DIE as soon as
16137        possible before fetching more types to complete the current type.
16138      * Make the type as complete as possible before fetching more types.  */
16139
16140 static struct type *
16141 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16142 {
16143   struct dwarf2_offset_and_type **slot, ofs;
16144   struct objfile *objfile = cu->objfile;
16145   htab_t *type_hash_ptr;
16146
16147   /* For Ada types, make sure that the gnat-specific data is always
16148      initialized (if not already set).  There are a few types where
16149      we should not be doing so, because the type-specific area is
16150      already used to hold some other piece of info (eg: TYPE_CODE_FLT
16151      where the type-specific area is used to store the floatformat).
16152      But this is not a problem, because the gnat-specific information
16153      is actually not needed for these types.  */
16154   if (need_gnat_info (cu)
16155       && TYPE_CODE (type) != TYPE_CODE_FUNC
16156       && TYPE_CODE (type) != TYPE_CODE_FLT
16157       && !HAVE_GNAT_AUX_INFO (type))
16158     INIT_GNAT_SPECIFIC (type);
16159
16160   if (cu->per_cu->debug_types_section)
16161     type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16162   else
16163     type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16164
16165   if (*type_hash_ptr == NULL)
16166     {
16167       *type_hash_ptr
16168         = htab_create_alloc_ex (127,
16169                                 offset_and_type_hash,
16170                                 offset_and_type_eq,
16171                                 NULL,
16172                                 &objfile->objfile_obstack,
16173                                 hashtab_obstack_allocate,
16174                                 dummy_obstack_deallocate);
16175     }
16176
16177   ofs.offset = die->offset;
16178   ofs.type = type;
16179   slot = (struct dwarf2_offset_and_type **)
16180     htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16181   if (*slot)
16182     complaint (&symfile_complaints,
16183                _("A problem internal to GDB: DIE 0x%x has type already set"),
16184                die->offset);
16185   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16186   **slot = ofs;
16187   return type;
16188 }
16189
16190 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16191    table, or return NULL if the die does not have a saved type.  */
16192
16193 static struct type *
16194 get_die_type_at_offset (unsigned int offset,
16195                         struct dwarf2_per_cu_data *per_cu)
16196 {
16197   struct dwarf2_offset_and_type *slot, ofs;
16198   htab_t type_hash;
16199
16200   if (per_cu->debug_types_section)
16201     type_hash = dwarf2_per_objfile->debug_types_type_hash;
16202   else
16203     type_hash = dwarf2_per_objfile->debug_info_type_hash;
16204   if (type_hash == NULL)
16205     return NULL;
16206
16207   ofs.offset = offset;
16208   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16209   if (slot)
16210     return slot->type;
16211   else
16212     return NULL;
16213 }
16214
16215 /* Look up the type for DIE in the appropriate type_hash table,
16216    or return NULL if DIE does not have a saved type.  */
16217
16218 static struct type *
16219 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16220 {
16221   return get_die_type_at_offset (die->offset, cu->per_cu);
16222 }
16223
16224 /* Add a dependence relationship from CU to REF_PER_CU.  */
16225
16226 static void
16227 dwarf2_add_dependence (struct dwarf2_cu *cu,
16228                        struct dwarf2_per_cu_data *ref_per_cu)
16229 {
16230   void **slot;
16231
16232   if (cu->dependencies == NULL)
16233     cu->dependencies
16234       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16235                               NULL, &cu->comp_unit_obstack,
16236                               hashtab_obstack_allocate,
16237                               dummy_obstack_deallocate);
16238
16239   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16240   if (*slot == NULL)
16241     *slot = ref_per_cu;
16242 }
16243
16244 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16245    Set the mark field in every compilation unit in the
16246    cache that we must keep because we are keeping CU.  */
16247
16248 static int
16249 dwarf2_mark_helper (void **slot, void *data)
16250 {
16251   struct dwarf2_per_cu_data *per_cu;
16252
16253   per_cu = (struct dwarf2_per_cu_data *) *slot;
16254
16255   /* cu->dependencies references may not yet have been ever read if QUIT aborts
16256      reading of the chain.  As such dependencies remain valid it is not much
16257      useful to track and undo them during QUIT cleanups.  */
16258   if (per_cu->cu == NULL)
16259     return 1;
16260
16261   if (per_cu->cu->mark)
16262     return 1;
16263   per_cu->cu->mark = 1;
16264
16265   if (per_cu->cu->dependencies != NULL)
16266     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16267
16268   return 1;
16269 }
16270
16271 /* Set the mark field in CU and in every other compilation unit in the
16272    cache that we must keep because we are keeping CU.  */
16273
16274 static void
16275 dwarf2_mark (struct dwarf2_cu *cu)
16276 {
16277   if (cu->mark)
16278     return;
16279   cu->mark = 1;
16280   if (cu->dependencies != NULL)
16281     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16282 }
16283
16284 static void
16285 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16286 {
16287   while (per_cu)
16288     {
16289       per_cu->cu->mark = 0;
16290       per_cu = per_cu->cu->read_in_chain;
16291     }
16292 }
16293
16294 /* Trivial hash function for partial_die_info: the hash value of a DIE
16295    is its offset in .debug_info for this objfile.  */
16296
16297 static hashval_t
16298 partial_die_hash (const void *item)
16299 {
16300   const struct partial_die_info *part_die = item;
16301
16302   return part_die->offset;
16303 }
16304
16305 /* Trivial comparison function for partial_die_info structures: two DIEs
16306    are equal if they have the same offset.  */
16307
16308 static int
16309 partial_die_eq (const void *item_lhs, const void *item_rhs)
16310 {
16311   const struct partial_die_info *part_die_lhs = item_lhs;
16312   const struct partial_die_info *part_die_rhs = item_rhs;
16313
16314   return part_die_lhs->offset == part_die_rhs->offset;
16315 }
16316
16317 static struct cmd_list_element *set_dwarf2_cmdlist;
16318 static struct cmd_list_element *show_dwarf2_cmdlist;
16319
16320 static void
16321 set_dwarf2_cmd (char *args, int from_tty)
16322 {
16323   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16324 }
16325
16326 static void
16327 show_dwarf2_cmd (char *args, int from_tty)
16328 {
16329   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16330 }
16331
16332 /* If section described by INFO was mmapped, munmap it now.  */
16333
16334 static void
16335 munmap_section_buffer (struct dwarf2_section_info *info)
16336 {
16337   if (info->map_addr != NULL)
16338     {
16339 #ifdef HAVE_MMAP
16340       int res;
16341
16342       res = munmap (info->map_addr, info->map_len);
16343       gdb_assert (res == 0);
16344 #else
16345       /* Without HAVE_MMAP, we should never be here to begin with.  */
16346       gdb_assert_not_reached ("no mmap support");
16347 #endif
16348     }
16349 }
16350
16351 /* munmap debug sections for OBJFILE, if necessary.  */
16352
16353 static void
16354 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16355 {
16356   struct dwarf2_per_objfile *data = d;
16357   int ix;
16358   struct dwarf2_section_info *section;
16359
16360   /* This is sorted according to the order they're defined in to make it easier
16361      to keep in sync.  */
16362   munmap_section_buffer (&data->info);
16363   munmap_section_buffer (&data->abbrev);
16364   munmap_section_buffer (&data->line);
16365   munmap_section_buffer (&data->loc);
16366   munmap_section_buffer (&data->macinfo);
16367   munmap_section_buffer (&data->macro);
16368   munmap_section_buffer (&data->str);
16369   munmap_section_buffer (&data->ranges);
16370   munmap_section_buffer (&data->frame);
16371   munmap_section_buffer (&data->eh_frame);
16372   munmap_section_buffer (&data->gdb_index);
16373
16374   for (ix = 0;
16375        VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16376        ++ix)
16377     munmap_section_buffer (section);
16378
16379   VEC_free (dwarf2_section_info_def, data->types);
16380 }
16381
16382 \f
16383 /* The "save gdb-index" command.  */
16384
16385 /* The contents of the hash table we create when building the string
16386    table.  */
16387 struct strtab_entry
16388 {
16389   offset_type offset;
16390   const char *str;
16391 };
16392
16393 /* Hash function for a strtab_entry.
16394
16395    Function is used only during write_hash_table so no index format backward
16396    compatibility is needed.  */
16397
16398 static hashval_t
16399 hash_strtab_entry (const void *e)
16400 {
16401   const struct strtab_entry *entry = e;
16402   return mapped_index_string_hash (INT_MAX, entry->str);
16403 }
16404
16405 /* Equality function for a strtab_entry.  */
16406
16407 static int
16408 eq_strtab_entry (const void *a, const void *b)
16409 {
16410   const struct strtab_entry *ea = a;
16411   const struct strtab_entry *eb = b;
16412   return !strcmp (ea->str, eb->str);
16413 }
16414
16415 /* Create a strtab_entry hash table.  */
16416
16417 static htab_t
16418 create_strtab (void)
16419 {
16420   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16421                             xfree, xcalloc, xfree);
16422 }
16423
16424 /* Add a string to the constant pool.  Return the string's offset in
16425    host order.  */
16426
16427 static offset_type
16428 add_string (htab_t table, struct obstack *cpool, const char *str)
16429 {
16430   void **slot;
16431   struct strtab_entry entry;
16432   struct strtab_entry *result;
16433
16434   entry.str = str;
16435   slot = htab_find_slot (table, &entry, INSERT);
16436   if (*slot)
16437     result = *slot;
16438   else
16439     {
16440       result = XNEW (struct strtab_entry);
16441       result->offset = obstack_object_size (cpool);
16442       result->str = str;
16443       obstack_grow_str0 (cpool, str);
16444       *slot = result;
16445     }
16446   return result->offset;
16447 }
16448
16449 /* An entry in the symbol table.  */
16450 struct symtab_index_entry
16451 {
16452   /* The name of the symbol.  */
16453   const char *name;
16454   /* The offset of the name in the constant pool.  */
16455   offset_type index_offset;
16456   /* A sorted vector of the indices of all the CUs that hold an object
16457      of this name.  */
16458   VEC (offset_type) *cu_indices;
16459 };
16460
16461 /* The symbol table.  This is a power-of-2-sized hash table.  */
16462 struct mapped_symtab
16463 {
16464   offset_type n_elements;
16465   offset_type size;
16466   struct symtab_index_entry **data;
16467 };
16468
16469 /* Hash function for a symtab_index_entry.  */
16470
16471 static hashval_t
16472 hash_symtab_entry (const void *e)
16473 {
16474   const struct symtab_index_entry *entry = e;
16475   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16476                          sizeof (offset_type) * VEC_length (offset_type,
16477                                                             entry->cu_indices),
16478                          0);
16479 }
16480
16481 /* Equality function for a symtab_index_entry.  */
16482
16483 static int
16484 eq_symtab_entry (const void *a, const void *b)
16485 {
16486   const struct symtab_index_entry *ea = a;
16487   const struct symtab_index_entry *eb = b;
16488   int len = VEC_length (offset_type, ea->cu_indices);
16489   if (len != VEC_length (offset_type, eb->cu_indices))
16490     return 0;
16491   return !memcmp (VEC_address (offset_type, ea->cu_indices),
16492                   VEC_address (offset_type, eb->cu_indices),
16493                   sizeof (offset_type) * len);
16494 }
16495
16496 /* Destroy a symtab_index_entry.  */
16497
16498 static void
16499 delete_symtab_entry (void *p)
16500 {
16501   struct symtab_index_entry *entry = p;
16502   VEC_free (offset_type, entry->cu_indices);
16503   xfree (entry);
16504 }
16505
16506 /* Create a hash table holding symtab_index_entry objects.  */
16507
16508 static htab_t
16509 create_symbol_hash_table (void)
16510 {
16511   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16512                             delete_symtab_entry, xcalloc, xfree);
16513 }
16514
16515 /* Create a new mapped symtab object.  */
16516
16517 static struct mapped_symtab *
16518 create_mapped_symtab (void)
16519 {
16520   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16521   symtab->n_elements = 0;
16522   symtab->size = 1024;
16523   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16524   return symtab;
16525 }
16526
16527 /* Destroy a mapped_symtab.  */
16528
16529 static void
16530 cleanup_mapped_symtab (void *p)
16531 {
16532   struct mapped_symtab *symtab = p;
16533   /* The contents of the array are freed when the other hash table is
16534      destroyed.  */
16535   xfree (symtab->data);
16536   xfree (symtab);
16537 }
16538
16539 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
16540    the slot.
16541    
16542    Function is used only during write_hash_table so no index format backward
16543    compatibility is needed.  */
16544
16545 static struct symtab_index_entry **
16546 find_slot (struct mapped_symtab *symtab, const char *name)
16547 {
16548   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16549
16550   index = hash & (symtab->size - 1);
16551   step = ((hash * 17) & (symtab->size - 1)) | 1;
16552
16553   for (;;)
16554     {
16555       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16556         return &symtab->data[index];
16557       index = (index + step) & (symtab->size - 1);
16558     }
16559 }
16560
16561 /* Expand SYMTAB's hash table.  */
16562
16563 static void
16564 hash_expand (struct mapped_symtab *symtab)
16565 {
16566   offset_type old_size = symtab->size;
16567   offset_type i;
16568   struct symtab_index_entry **old_entries = symtab->data;
16569
16570   symtab->size *= 2;
16571   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16572
16573   for (i = 0; i < old_size; ++i)
16574     {
16575       if (old_entries[i])
16576         {
16577           struct symtab_index_entry **slot = find_slot (symtab,
16578                                                         old_entries[i]->name);
16579           *slot = old_entries[i];
16580         }
16581     }
16582
16583   xfree (old_entries);
16584 }
16585
16586 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
16587    is the index of the CU in which the symbol appears.  */
16588
16589 static void
16590 add_index_entry (struct mapped_symtab *symtab, const char *name,
16591                  offset_type cu_index)
16592 {
16593   struct symtab_index_entry **slot;
16594
16595   ++symtab->n_elements;
16596   if (4 * symtab->n_elements / 3 >= symtab->size)
16597     hash_expand (symtab);
16598
16599   slot = find_slot (symtab, name);
16600   if (!*slot)
16601     {
16602       *slot = XNEW (struct symtab_index_entry);
16603       (*slot)->name = name;
16604       (*slot)->cu_indices = NULL;
16605     }
16606   /* Don't push an index twice.  Due to how we add entries we only
16607      have to check the last one.  */ 
16608   if (VEC_empty (offset_type, (*slot)->cu_indices)
16609       || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16610     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16611 }
16612
16613 /* Add a vector of indices to the constant pool.  */
16614
16615 static offset_type
16616 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16617                       struct symtab_index_entry *entry)
16618 {
16619   void **slot;
16620
16621   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16622   if (!*slot)
16623     {
16624       offset_type len = VEC_length (offset_type, entry->cu_indices);
16625       offset_type val = MAYBE_SWAP (len);
16626       offset_type iter;
16627       int i;
16628
16629       *slot = entry;
16630       entry->index_offset = obstack_object_size (cpool);
16631
16632       obstack_grow (cpool, &val, sizeof (val));
16633       for (i = 0;
16634            VEC_iterate (offset_type, entry->cu_indices, i, iter);
16635            ++i)
16636         {
16637           val = MAYBE_SWAP (iter);
16638           obstack_grow (cpool, &val, sizeof (val));
16639         }
16640     }
16641   else
16642     {
16643       struct symtab_index_entry *old_entry = *slot;
16644       entry->index_offset = old_entry->index_offset;
16645       entry = old_entry;
16646     }
16647   return entry->index_offset;
16648 }
16649
16650 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16651    constant pool entries going into the obstack CPOOL.  */
16652
16653 static void
16654 write_hash_table (struct mapped_symtab *symtab,
16655                   struct obstack *output, struct obstack *cpool)
16656 {
16657   offset_type i;
16658   htab_t symbol_hash_table;
16659   htab_t str_table;
16660
16661   symbol_hash_table = create_symbol_hash_table ();
16662   str_table = create_strtab ();
16663
16664   /* We add all the index vectors to the constant pool first, to
16665      ensure alignment is ok.  */
16666   for (i = 0; i < symtab->size; ++i)
16667     {
16668       if (symtab->data[i])
16669         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16670     }
16671
16672   /* Now write out the hash table.  */
16673   for (i = 0; i < symtab->size; ++i)
16674     {
16675       offset_type str_off, vec_off;
16676
16677       if (symtab->data[i])
16678         {
16679           str_off = add_string (str_table, cpool, symtab->data[i]->name);
16680           vec_off = symtab->data[i]->index_offset;
16681         }
16682       else
16683         {
16684           /* While 0 is a valid constant pool index, it is not valid
16685              to have 0 for both offsets.  */
16686           str_off = 0;
16687           vec_off = 0;
16688         }
16689
16690       str_off = MAYBE_SWAP (str_off);
16691       vec_off = MAYBE_SWAP (vec_off);
16692
16693       obstack_grow (output, &str_off, sizeof (str_off));
16694       obstack_grow (output, &vec_off, sizeof (vec_off));
16695     }
16696
16697   htab_delete (str_table);
16698   htab_delete (symbol_hash_table);
16699 }
16700
16701 /* Struct to map psymtab to CU index in the index file.  */
16702 struct psymtab_cu_index_map
16703 {
16704   struct partial_symtab *psymtab;
16705   unsigned int cu_index;
16706 };
16707
16708 static hashval_t
16709 hash_psymtab_cu_index (const void *item)
16710 {
16711   const struct psymtab_cu_index_map *map = item;
16712
16713   return htab_hash_pointer (map->psymtab);
16714 }
16715
16716 static int
16717 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16718 {
16719   const struct psymtab_cu_index_map *lhs = item_lhs;
16720   const struct psymtab_cu_index_map *rhs = item_rhs;
16721
16722   return lhs->psymtab == rhs->psymtab;
16723 }
16724
16725 /* Helper struct for building the address table.  */
16726 struct addrmap_index_data
16727 {
16728   struct objfile *objfile;
16729   struct obstack *addr_obstack;
16730   htab_t cu_index_htab;
16731
16732   /* Non-zero if the previous_* fields are valid.
16733      We can't write an entry until we see the next entry (since it is only then
16734      that we know the end of the entry).  */
16735   int previous_valid;
16736   /* Index of the CU in the table of all CUs in the index file.  */
16737   unsigned int previous_cu_index;
16738   /* Start address of the CU.  */
16739   CORE_ADDR previous_cu_start;
16740 };
16741
16742 /* Write an address entry to OBSTACK.  */
16743
16744 static void
16745 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16746                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16747 {
16748   offset_type cu_index_to_write;
16749   char addr[8];
16750   CORE_ADDR baseaddr;
16751
16752   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16753
16754   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16755   obstack_grow (obstack, addr, 8);
16756   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16757   obstack_grow (obstack, addr, 8);
16758   cu_index_to_write = MAYBE_SWAP (cu_index);
16759   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16760 }
16761
16762 /* Worker function for traversing an addrmap to build the address table.  */
16763
16764 static int
16765 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16766 {
16767   struct addrmap_index_data *data = datap;
16768   struct partial_symtab *pst = obj;
16769   offset_type cu_index;
16770   void **slot;
16771
16772   if (data->previous_valid)
16773     add_address_entry (data->objfile, data->addr_obstack,
16774                        data->previous_cu_start, start_addr,
16775                        data->previous_cu_index);
16776
16777   data->previous_cu_start = start_addr;
16778   if (pst != NULL)
16779     {
16780       struct psymtab_cu_index_map find_map, *map;
16781       find_map.psymtab = pst;
16782       map = htab_find (data->cu_index_htab, &find_map);
16783       gdb_assert (map != NULL);
16784       data->previous_cu_index = map->cu_index;
16785       data->previous_valid = 1;
16786     }
16787   else
16788       data->previous_valid = 0;
16789
16790   return 0;
16791 }
16792
16793 /* Write OBJFILE's address map to OBSTACK.
16794    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16795    in the index file.  */
16796
16797 static void
16798 write_address_map (struct objfile *objfile, struct obstack *obstack,
16799                    htab_t cu_index_htab)
16800 {
16801   struct addrmap_index_data addrmap_index_data;
16802
16803   /* When writing the address table, we have to cope with the fact that
16804      the addrmap iterator only provides the start of a region; we have to
16805      wait until the next invocation to get the start of the next region.  */
16806
16807   addrmap_index_data.objfile = objfile;
16808   addrmap_index_data.addr_obstack = obstack;
16809   addrmap_index_data.cu_index_htab = cu_index_htab;
16810   addrmap_index_data.previous_valid = 0;
16811
16812   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16813                    &addrmap_index_data);
16814
16815   /* It's highly unlikely the last entry (end address = 0xff...ff)
16816      is valid, but we should still handle it.
16817      The end address is recorded as the start of the next region, but that
16818      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
16819      anyway.  */
16820   if (addrmap_index_data.previous_valid)
16821     add_address_entry (objfile, obstack,
16822                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16823                        addrmap_index_data.previous_cu_index);
16824 }
16825
16826 /* Add a list of partial symbols to SYMTAB.  */
16827
16828 static void
16829 write_psymbols (struct mapped_symtab *symtab,
16830                 htab_t psyms_seen,
16831                 struct partial_symbol **psymp,
16832                 int count,
16833                 offset_type cu_index,
16834                 int is_static)
16835 {
16836   for (; count-- > 0; ++psymp)
16837     {
16838       void **slot, *lookup;
16839
16840       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16841         error (_("Ada is not currently supported by the index"));
16842
16843       /* We only want to add a given psymbol once.  However, we also
16844          want to account for whether it is global or static.  So, we
16845          may add it twice, using slightly different values.  */
16846       if (is_static)
16847         {
16848           uintptr_t val = 1 | (uintptr_t) *psymp;
16849
16850           lookup = (void *) val;
16851         }
16852       else
16853         lookup = *psymp;
16854
16855       /* Only add a given psymbol once.  */
16856       slot = htab_find_slot (psyms_seen, lookup, INSERT);
16857       if (!*slot)
16858         {
16859           *slot = lookup;
16860           add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16861         }
16862     }
16863 }
16864
16865 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
16866    exception if there is an error.  */
16867
16868 static void
16869 write_obstack (FILE *file, struct obstack *obstack)
16870 {
16871   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16872               file)
16873       != obstack_object_size (obstack))
16874     error (_("couldn't data write to file"));
16875 }
16876
16877 /* Unlink a file if the argument is not NULL.  */
16878
16879 static void
16880 unlink_if_set (void *p)
16881 {
16882   char **filename = p;
16883   if (*filename)
16884     unlink (*filename);
16885 }
16886
16887 /* A helper struct used when iterating over debug_types.  */
16888 struct signatured_type_index_data
16889 {
16890   struct objfile *objfile;
16891   struct mapped_symtab *symtab;
16892   struct obstack *types_list;
16893   htab_t psyms_seen;
16894   int cu_index;
16895 };
16896
16897 /* A helper function that writes a single signatured_type to an
16898    obstack.  */
16899
16900 static int
16901 write_one_signatured_type (void **slot, void *d)
16902 {
16903   struct signatured_type_index_data *info = d;
16904   struct signatured_type *entry = (struct signatured_type *) *slot;
16905   struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16906   struct partial_symtab *psymtab = per_cu->v.psymtab;
16907   gdb_byte val[8];
16908
16909   write_psymbols (info->symtab,
16910                   info->psyms_seen,
16911                   info->objfile->global_psymbols.list
16912                   + psymtab->globals_offset,
16913                   psymtab->n_global_syms, info->cu_index,
16914                   0);
16915   write_psymbols (info->symtab,
16916                   info->psyms_seen,
16917                   info->objfile->static_psymbols.list
16918                   + psymtab->statics_offset,
16919                   psymtab->n_static_syms, info->cu_index,
16920                   1);
16921
16922   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16923   obstack_grow (info->types_list, val, 8);
16924   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16925   obstack_grow (info->types_list, val, 8);
16926   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16927   obstack_grow (info->types_list, val, 8);
16928
16929   ++info->cu_index;
16930
16931   return 1;
16932 }
16933
16934 /* Create an index file for OBJFILE in the directory DIR.  */
16935
16936 static void
16937 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16938 {
16939   struct cleanup *cleanup;
16940   char *filename, *cleanup_filename;
16941   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16942   struct obstack cu_list, types_cu_list;
16943   int i;
16944   FILE *out_file;
16945   struct mapped_symtab *symtab;
16946   offset_type val, size_of_contents, total_len;
16947   struct stat st;
16948   char buf[8];
16949   htab_t psyms_seen;
16950   htab_t cu_index_htab;
16951   struct psymtab_cu_index_map *psymtab_cu_index_map;
16952
16953   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16954     return;
16955
16956   if (dwarf2_per_objfile->using_index)
16957     error (_("Cannot use an index to create the index"));
16958
16959   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16960     error (_("Cannot make an index when the file has multiple .debug_types sections"));
16961
16962   if (stat (objfile->name, &st) < 0)
16963     perror_with_name (objfile->name);
16964
16965   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16966                      INDEX_SUFFIX, (char *) NULL);
16967   cleanup = make_cleanup (xfree, filename);
16968
16969   out_file = fopen (filename, "wb");
16970   if (!out_file)
16971     error (_("Can't open `%s' for writing"), filename);
16972
16973   cleanup_filename = filename;
16974   make_cleanup (unlink_if_set, &cleanup_filename);
16975
16976   symtab = create_mapped_symtab ();
16977   make_cleanup (cleanup_mapped_symtab, symtab);
16978
16979   obstack_init (&addr_obstack);
16980   make_cleanup_obstack_free (&addr_obstack);
16981
16982   obstack_init (&cu_list);
16983   make_cleanup_obstack_free (&cu_list);
16984
16985   obstack_init (&types_cu_list);
16986   make_cleanup_obstack_free (&types_cu_list);
16987
16988   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16989                                   NULL, xcalloc, xfree);
16990   make_cleanup_htab_delete (psyms_seen);
16991
16992   /* While we're scanning CU's create a table that maps a psymtab pointer
16993      (which is what addrmap records) to its index (which is what is recorded
16994      in the index file).  This will later be needed to write the address
16995      table.  */
16996   cu_index_htab = htab_create_alloc (100,
16997                                      hash_psymtab_cu_index,
16998                                      eq_psymtab_cu_index,
16999                                      NULL, xcalloc, xfree);
17000   make_cleanup_htab_delete (cu_index_htab);
17001   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17002     xmalloc (sizeof (struct psymtab_cu_index_map)
17003              * dwarf2_per_objfile->n_comp_units);
17004   make_cleanup (xfree, psymtab_cu_index_map);
17005
17006   /* The CU list is already sorted, so we don't need to do additional
17007      work here.  Also, the debug_types entries do not appear in
17008      all_comp_units, but only in their own hash table.  */
17009   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17010     {
17011       struct dwarf2_per_cu_data *per_cu
17012         = dwarf2_per_objfile->all_comp_units[i];
17013       struct partial_symtab *psymtab = per_cu->v.psymtab;
17014       gdb_byte val[8];
17015       struct psymtab_cu_index_map *map;
17016       void **slot;
17017
17018       write_psymbols (symtab,
17019                       psyms_seen,
17020                       objfile->global_psymbols.list + psymtab->globals_offset,
17021                       psymtab->n_global_syms, i,
17022                       0);
17023       write_psymbols (symtab,
17024                       psyms_seen,
17025                       objfile->static_psymbols.list + psymtab->statics_offset,
17026                       psymtab->n_static_syms, i,
17027                       1);
17028
17029       map = &psymtab_cu_index_map[i];
17030       map->psymtab = psymtab;
17031       map->cu_index = i;
17032       slot = htab_find_slot (cu_index_htab, map, INSERT);
17033       gdb_assert (slot != NULL);
17034       gdb_assert (*slot == NULL);
17035       *slot = map;
17036
17037       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
17038       obstack_grow (&cu_list, val, 8);
17039       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17040       obstack_grow (&cu_list, val, 8);
17041     }
17042
17043   /* Dump the address map.  */
17044   write_address_map (objfile, &addr_obstack, cu_index_htab);
17045
17046   /* Write out the .debug_type entries, if any.  */
17047   if (dwarf2_per_objfile->signatured_types)
17048     {
17049       struct signatured_type_index_data sig_data;
17050
17051       sig_data.objfile = objfile;
17052       sig_data.symtab = symtab;
17053       sig_data.types_list = &types_cu_list;
17054       sig_data.psyms_seen = psyms_seen;
17055       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17056       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17057                               write_one_signatured_type, &sig_data);
17058     }
17059
17060   obstack_init (&constant_pool);
17061   make_cleanup_obstack_free (&constant_pool);
17062   obstack_init (&symtab_obstack);
17063   make_cleanup_obstack_free (&symtab_obstack);
17064   write_hash_table (symtab, &symtab_obstack, &constant_pool);
17065
17066   obstack_init (&contents);
17067   make_cleanup_obstack_free (&contents);
17068   size_of_contents = 6 * sizeof (offset_type);
17069   total_len = size_of_contents;
17070
17071   /* The version number.  */
17072   val = MAYBE_SWAP (6);
17073   obstack_grow (&contents, &val, sizeof (val));
17074
17075   /* The offset of the CU list from the start of the file.  */
17076   val = MAYBE_SWAP (total_len);
17077   obstack_grow (&contents, &val, sizeof (val));
17078   total_len += obstack_object_size (&cu_list);
17079
17080   /* The offset of the types CU list from the start of the file.  */
17081   val = MAYBE_SWAP (total_len);
17082   obstack_grow (&contents, &val, sizeof (val));
17083   total_len += obstack_object_size (&types_cu_list);
17084
17085   /* The offset of the address table from the start of the file.  */
17086   val = MAYBE_SWAP (total_len);
17087   obstack_grow (&contents, &val, sizeof (val));
17088   total_len += obstack_object_size (&addr_obstack);
17089
17090   /* The offset of the symbol table from the start of the file.  */
17091   val = MAYBE_SWAP (total_len);
17092   obstack_grow (&contents, &val, sizeof (val));
17093   total_len += obstack_object_size (&symtab_obstack);
17094
17095   /* The offset of the constant pool from the start of the file.  */
17096   val = MAYBE_SWAP (total_len);
17097   obstack_grow (&contents, &val, sizeof (val));
17098   total_len += obstack_object_size (&constant_pool);
17099
17100   gdb_assert (obstack_object_size (&contents) == size_of_contents);
17101
17102   write_obstack (out_file, &contents);
17103   write_obstack (out_file, &cu_list);
17104   write_obstack (out_file, &types_cu_list);
17105   write_obstack (out_file, &addr_obstack);
17106   write_obstack (out_file, &symtab_obstack);
17107   write_obstack (out_file, &constant_pool);
17108
17109   fclose (out_file);
17110
17111   /* We want to keep the file, so we set cleanup_filename to NULL
17112      here.  See unlink_if_set.  */
17113   cleanup_filename = NULL;
17114
17115   do_cleanups (cleanup);
17116 }
17117
17118 /* Implementation of the `save gdb-index' command.
17119    
17120    Note that the file format used by this command is documented in the
17121    GDB manual.  Any changes here must be documented there.  */
17122
17123 static void
17124 save_gdb_index_command (char *arg, int from_tty)
17125 {
17126   struct objfile *objfile;
17127
17128   if (!arg || !*arg)
17129     error (_("usage: save gdb-index DIRECTORY"));
17130
17131   ALL_OBJFILES (objfile)
17132   {
17133     struct stat st;
17134
17135     /* If the objfile does not correspond to an actual file, skip it.  */
17136     if (stat (objfile->name, &st) < 0)
17137       continue;
17138
17139     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17140     if (dwarf2_per_objfile)
17141       {
17142         volatile struct gdb_exception except;
17143
17144         TRY_CATCH (except, RETURN_MASK_ERROR)
17145           {
17146             write_psymtabs_to_index (objfile, arg);
17147           }
17148         if (except.reason < 0)
17149           exception_fprintf (gdb_stderr, except,
17150                              _("Error while writing index for `%s': "),
17151                              objfile->name);
17152       }
17153   }
17154 }
17155
17156 \f
17157
17158 int dwarf2_always_disassemble;
17159
17160 static void
17161 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17162                                 struct cmd_list_element *c, const char *value)
17163 {
17164   fprintf_filtered (file,
17165                     _("Whether to always disassemble "
17166                       "DWARF expressions is %s.\n"),
17167                     value);
17168 }
17169
17170 static void
17171 show_check_physname (struct ui_file *file, int from_tty,
17172                      struct cmd_list_element *c, const char *value)
17173 {
17174   fprintf_filtered (file,
17175                     _("Whether to check \"physname\" is %s.\n"),
17176                     value);
17177 }
17178
17179 void _initialize_dwarf2_read (void);
17180
17181 void
17182 _initialize_dwarf2_read (void)
17183 {
17184   struct cmd_list_element *c;
17185
17186   dwarf2_objfile_data_key
17187     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17188
17189   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17190 Set DWARF 2 specific variables.\n\
17191 Configure DWARF 2 variables such as the cache size"),
17192                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17193                   0/*allow-unknown*/, &maintenance_set_cmdlist);
17194
17195   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17196 Show DWARF 2 specific variables\n\
17197 Show DWARF 2 variables such as the cache size"),
17198                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17199                   0/*allow-unknown*/, &maintenance_show_cmdlist);
17200
17201   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17202                             &dwarf2_max_cache_age, _("\
17203 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17204 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17205 A higher limit means that cached compilation units will be stored\n\
17206 in memory longer, and more total memory will be used.  Zero disables\n\
17207 caching, which can slow down startup."),
17208                             NULL,
17209                             show_dwarf2_max_cache_age,
17210                             &set_dwarf2_cmdlist,
17211                             &show_dwarf2_cmdlist);
17212
17213   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17214                            &dwarf2_always_disassemble, _("\
17215 Set whether `info address' always disassembles DWARF expressions."), _("\
17216 Show whether `info address' always disassembles DWARF expressions."), _("\
17217 When enabled, DWARF expressions are always printed in an assembly-like\n\
17218 syntax.  When disabled, expressions will be printed in a more\n\
17219 conversational style, when possible."),
17220                            NULL,
17221                            show_dwarf2_always_disassemble,
17222                            &set_dwarf2_cmdlist,
17223                            &show_dwarf2_cmdlist);
17224
17225   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17226 Set debugging of the dwarf2 DIE reader."), _("\
17227 Show debugging of the dwarf2 DIE reader."), _("\
17228 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17229 The value is the maximum depth to print."),
17230                             NULL,
17231                             NULL,
17232                             &setdebuglist, &showdebuglist);
17233
17234   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17235 Set cross-checking of \"physname\" code against demangler."), _("\
17236 Show cross-checking of \"physname\" code against demangler."), _("\
17237 When enabled, GDB's internal \"physname\" code is checked against\n\
17238 the demangler."),
17239                            NULL, show_check_physname,
17240                            &setdebuglist, &showdebuglist);
17241
17242   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17243                _("\
17244 Save a gdb-index file.\n\
17245 Usage: save gdb-index DIRECTORY"),
17246                &save_cmdlist);
17247   set_cmd_completer (c, filename_completer);
17248 }