OSDN Git Service

* dwarf2read.c (read_structure_type): Add comment.
[pf3gnuchains/sourceware.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80    Because of alignment constraints, this structure has padding and cannot
81    be mapped directly onto the beginning of the .debug_info section.  */
82 typedef struct comp_unit_header
83   {
84     unsigned int length;        /* length of the .debug_info
85                                    contribution */
86     unsigned short version;     /* version number -- 2 for DWARF
87                                    version 2 */
88     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89     unsigned char addr_size;    /* byte size of an address -- 4 */
90   }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96    Because of alignment constraints, this structure has padding and cannot
97    be mapped directly onto the beginning of the .debug_info section.  */
98 typedef struct statement_prologue
99   {
100     unsigned int total_length;  /* byte length of the statement
101                                    information */
102     unsigned short version;     /* version number -- 2 for DWARF
103                                    version 2 */
104     unsigned int prologue_length;       /* # bytes between prologue &
105                                            stmt program */
106     unsigned char minimum_instruction_length;   /* byte size of
107                                                    smallest instr */
108     unsigned char default_is_stmt;      /* initial value of is_stmt
109                                            register */
110     char line_base;
111     unsigned char line_range;
112     unsigned char opcode_base;  /* number assigned to first special
113                                    opcode */
114     unsigned char *standard_opcode_lengths;
115   }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in.  */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124    info for C++ namespaces.  GCC 3.3.x did not produce this information,
125    but later versions do.  */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133   asection *asection;
134   gdb_byte *buffer;
135   bfd_size_type size;
136   int was_mmapped;
137   /* True if we have tried to read this section.  */
138   int readin;
139 };
140
141 /* All offsets in the index are of this type.  It must be
142    architecture-independent.  */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index.  The file format is described in
148    a comment by the code that writes the index.  */
149 struct mapped_index
150 {
151   /* The total length of the buffer.  */
152   off_t total_size;
153   /* A pointer to the address table data.  */
154   const gdb_byte *address_table;
155   /* Size of the address table data in bytes.  */
156   offset_type address_table_size;
157   /* The hash table.  */
158   const offset_type *index_table;
159   /* Size in slots, each slot is 2 offset_types.  */
160   offset_type index_table_slots;
161   /* A pointer to the constant pool.  */
162   const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167   struct dwarf2_section_info info;
168   struct dwarf2_section_info abbrev;
169   struct dwarf2_section_info line;
170   struct dwarf2_section_info loc;
171   struct dwarf2_section_info macinfo;
172   struct dwarf2_section_info str;
173   struct dwarf2_section_info ranges;
174   struct dwarf2_section_info types;
175   struct dwarf2_section_info frame;
176   struct dwarf2_section_info eh_frame;
177   struct dwarf2_section_info gdb_index;
178
179   /* Back link.  */
180   struct objfile *objfile;
181
182   /* A list of all the compilation units.  This is used to locate
183      the target compilation unit of a particular reference.  */
184   struct dwarf2_per_cu_data **all_comp_units;
185
186   /* The number of compilation units in ALL_COMP_UNITS.  */
187   int n_comp_units;
188
189   /* The number of .debug_types-related CUs.  */
190   int n_type_comp_units;
191
192   /* The .debug_types-related CUs.  */
193   struct dwarf2_per_cu_data **type_comp_units;
194
195   /* A chain of compilation units that are currently read in, so that
196      they can be freed later.  */
197   struct dwarf2_per_cu_data *read_in_chain;
198
199   /* A table mapping .debug_types signatures to its signatured_type entry.
200      This is NULL if the .debug_types section hasn't been read in yet.  */
201   htab_t signatured_types;
202
203   /* A flag indicating wether this objfile has a section loaded at a
204      VMA of 0.  */
205   int has_section_at_zero;
206
207   /* True if we are using the mapped index.  */
208   unsigned char using_index;
209
210   /* The mapped index.  */
211   struct mapped_index *index_table;
212
213   /* Set during partial symbol reading, to prevent queueing of full
214      symbols.  */
215   int reading_partial_symbols;
216 };
217
218 static struct dwarf2_per_objfile *dwarf2_per_objfile;
219
220 /* names of the debugging sections */
221
222 /* Note that if the debugging section has been compressed, it might
223    have a name like .zdebug_info.  */
224
225 #define INFO_SECTION     "debug_info"
226 #define ABBREV_SECTION   "debug_abbrev"
227 #define LINE_SECTION     "debug_line"
228 #define LOC_SECTION      "debug_loc"
229 #define MACINFO_SECTION  "debug_macinfo"
230 #define STR_SECTION      "debug_str"
231 #define RANGES_SECTION   "debug_ranges"
232 #define TYPES_SECTION    "debug_types"
233 #define FRAME_SECTION    "debug_frame"
234 #define EH_FRAME_SECTION "eh_frame"
235 #define GDB_INDEX_SECTION "gdb_index"
236
237 /* local data types */
238
239 /* We hold several abbreviation tables in memory at the same time. */
240 #ifndef ABBREV_HASH_SIZE
241 #define ABBREV_HASH_SIZE 121
242 #endif
243
244 /* The data in a compilation unit header, after target2host
245    translation, looks like this.  */
246 struct comp_unit_head
247 {
248   unsigned int length;
249   short version;
250   unsigned char addr_size;
251   unsigned char signed_addr_p;
252   unsigned int abbrev_offset;
253
254   /* Size of file offsets; either 4 or 8.  */
255   unsigned int offset_size;
256
257   /* Size of the length field; either 4 or 12.  */
258   unsigned int initial_length_size;
259
260   /* Offset to the first byte of this compilation unit header in the
261      .debug_info section, for resolving relative reference dies.  */
262   unsigned int offset;
263
264   /* Offset to first die in this cu from the start of the cu.
265      This will be the first byte following the compilation unit header.  */
266   unsigned int first_die_offset;
267 };
268
269 /* Type used for delaying computation of method physnames.
270    See comments for compute_delayed_physnames.  */
271 struct delayed_method_info
272 {
273   /* The type to which the method is attached, i.e., its parent class.  */
274   struct type *type;
275
276   /* The index of the method in the type's function fieldlists.  */
277   int fnfield_index;
278
279   /* The index of the method in the fieldlist.  */
280   int index;
281
282   /* The name of the DIE.  */
283   const char *name;
284
285   /*  The DIE associated with this method.  */
286   struct die_info *die;
287 };
288
289 typedef struct delayed_method_info delayed_method_info;
290 DEF_VEC_O (delayed_method_info);
291
292 /* Internal state when decoding a particular compilation unit.  */
293 struct dwarf2_cu
294 {
295   /* The objfile containing this compilation unit.  */
296   struct objfile *objfile;
297
298   /* The header of the compilation unit.  */
299   struct comp_unit_head header;
300
301   /* Base address of this compilation unit.  */
302   CORE_ADDR base_address;
303
304   /* Non-zero if base_address has been set.  */
305   int base_known;
306
307   struct function_range *first_fn, *last_fn, *cached_fn;
308
309   /* The language we are debugging.  */
310   enum language language;
311   const struct language_defn *language_defn;
312
313   const char *producer;
314
315   /* The generic symbol table building routines have separate lists for
316      file scope symbols and all all other scopes (local scopes).  So
317      we need to select the right one to pass to add_symbol_to_list().
318      We do it by keeping a pointer to the correct list in list_in_scope.
319
320      FIXME: The original dwarf code just treated the file scope as the
321      first local scope, and all other local scopes as nested local
322      scopes, and worked fine.  Check to see if we really need to
323      distinguish these in buildsym.c.  */
324   struct pending **list_in_scope;
325
326   /* DWARF abbreviation table associated with this compilation unit.  */
327   struct abbrev_info **dwarf2_abbrevs;
328
329   /* Storage for the abbrev table.  */
330   struct obstack abbrev_obstack;
331
332   /* Hash table holding all the loaded partial DIEs.  */
333   htab_t partial_dies;
334
335   /* Storage for things with the same lifetime as this read-in compilation
336      unit, including partial DIEs.  */
337   struct obstack comp_unit_obstack;
338
339   /* When multiple dwarf2_cu structures are living in memory, this field
340      chains them all together, so that they can be released efficiently.
341      We will probably also want a generation counter so that most-recently-used
342      compilation units are cached...  */
343   struct dwarf2_per_cu_data *read_in_chain;
344
345   /* Backchain to our per_cu entry if the tree has been built.  */
346   struct dwarf2_per_cu_data *per_cu;
347
348   /* Pointer to the die -> type map.  Although it is stored
349      permanently in per_cu, we copy it here to avoid double
350      indirection.  */
351   htab_t type_hash;
352
353   /* How many compilation units ago was this CU last referenced?  */
354   int last_used;
355
356   /* A hash table of die offsets for following references.  */
357   htab_t die_hash;
358
359   /* Full DIEs if read in.  */
360   struct die_info *dies;
361
362   /* A set of pointers to dwarf2_per_cu_data objects for compilation
363      units referenced by this one.  Only set during full symbol processing;
364      partial symbol tables do not have dependencies.  */
365   htab_t dependencies;
366
367   /* Header data from the line table, during full symbol processing.  */
368   struct line_header *line_header;
369
370   /* A list of methods which need to have physnames computed
371      after all type information has been read.  */
372   VEC (delayed_method_info) *method_list;
373
374   /* Mark used when releasing cached dies.  */
375   unsigned int mark : 1;
376
377   /* This flag will be set if this compilation unit might include
378      inter-compilation-unit references.  */
379   unsigned int has_form_ref_addr : 1;
380
381   /* This flag will be set if this compilation unit includes any
382      DW_TAG_namespace DIEs.  If we know that there are explicit
383      DIEs for namespaces, we don't need to try to infer them
384      from mangled names.  */
385   unsigned int has_namespace_info : 1;
386 };
387
388 /* When using the index (and thus not using psymtabs), each CU has an
389    object of this type.  This is used to hold information needed by
390    the various "quick" methods.  */
391 struct dwarf2_per_cu_quick_data
392 {
393   /* The line table.  This can be NULL if there was no line table.  */
394   struct line_header *lines;
395
396   /* The file names from the line table.  */
397   const char **file_names;
398   /* The file names from the line table after being run through
399      gdb_realpath.  */
400   const char **full_names;
401
402   /* The corresponding symbol table.  This is NULL if symbols for this
403      CU have not yet been read.  */
404   struct symtab *symtab;
405
406   /* A temporary mark bit used when iterating over all CUs in
407      expand_symtabs_matching.  */
408   unsigned int mark : 1;
409
410   /* True if we've tried to read the line table.  */
411   unsigned int read_lines : 1;
412 };
413
414 /* Persistent data held for a compilation unit, even when not
415    processing it.  We put a pointer to this structure in the
416    read_symtab_private field of the psymtab.  If we encounter
417    inter-compilation-unit references, we also maintain a sorted
418    list of all compilation units.  */
419
420 struct dwarf2_per_cu_data
421 {
422   /* The start offset and length of this compilation unit.  2**29-1
423      bytes should suffice to store the length of any compilation unit
424      - if it doesn't, GDB will fall over anyway.
425      NOTE: Unlike comp_unit_head.length, this length includes
426      initial_length_size.  */
427   unsigned int offset;
428   unsigned int length : 29;
429
430   /* Flag indicating this compilation unit will be read in before
431      any of the current compilation units are processed.  */
432   unsigned int queued : 1;
433
434   /* This flag will be set if we need to load absolutely all DIEs
435      for this compilation unit, instead of just the ones we think
436      are interesting.  It gets set if we look for a DIE in the
437      hash table and don't find it.  */
438   unsigned int load_all_dies : 1;
439
440   /* Non-zero if this CU is from .debug_types.
441      Otherwise it's from .debug_info.  */
442   unsigned int from_debug_types : 1;
443
444   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
445      of the CU cache it gets reset to NULL again.  */
446   struct dwarf2_cu *cu;
447
448   /* If full symbols for this CU have been read in, then this field
449      holds a map of DIE offsets to types.  It isn't always possible
450      to reconstruct this information later, so we have to preserve
451      it.  */
452   htab_t type_hash;
453
454   /* The corresponding objfile.  */
455   struct objfile *objfile;
456
457   /* When using partial symbol tables, the 'psymtab' field is active.
458      Otherwise the 'quick' field is active.  */
459   union
460   {
461     /* The partial symbol table associated with this compilation unit,
462        or NULL for partial units (which do not have an associated
463        symtab).  */
464     struct partial_symtab *psymtab;
465
466     /* Data needed by the "quick" functions.  */
467     struct dwarf2_per_cu_quick_data *quick;
468   } v;
469 };
470
471 /* Entry in the signatured_types hash table.  */
472
473 struct signatured_type
474 {
475   ULONGEST signature;
476
477   /* Offset in .debug_types of the TU (type_unit) for this type.  */
478   unsigned int offset;
479
480   /* Offset in .debug_types of the type defined by this TU.  */
481   unsigned int type_offset;
482
483   /* The CU(/TU) of this type.  */
484   struct dwarf2_per_cu_data per_cu;
485 };
486
487 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
488    which are used for both .debug_info and .debug_types dies.
489    All parameters here are unchanging for the life of the call.
490    This struct exists to abstract away the constant parameters of
491    die reading.  */
492
493 struct die_reader_specs
494 {
495   /* The bfd of this objfile.  */
496   bfd* abfd;
497
498   /* The CU of the DIE we are parsing.  */
499   struct dwarf2_cu *cu;
500
501   /* Pointer to start of section buffer.
502      This is either the start of .debug_info or .debug_types.  */
503   const gdb_byte *buffer;
504 };
505
506 /* The line number information for a compilation unit (found in the
507    .debug_line section) begins with a "statement program header",
508    which contains the following information.  */
509 struct line_header
510 {
511   unsigned int total_length;
512   unsigned short version;
513   unsigned int header_length;
514   unsigned char minimum_instruction_length;
515   unsigned char maximum_ops_per_instruction;
516   unsigned char default_is_stmt;
517   int line_base;
518   unsigned char line_range;
519   unsigned char opcode_base;
520
521   /* standard_opcode_lengths[i] is the number of operands for the
522      standard opcode whose value is i.  This means that
523      standard_opcode_lengths[0] is unused, and the last meaningful
524      element is standard_opcode_lengths[opcode_base - 1].  */
525   unsigned char *standard_opcode_lengths;
526
527   /* The include_directories table.  NOTE!  These strings are not
528      allocated with xmalloc; instead, they are pointers into
529      debug_line_buffer.  If you try to free them, `free' will get
530      indigestion.  */
531   unsigned int num_include_dirs, include_dirs_size;
532   char **include_dirs;
533
534   /* The file_names table.  NOTE!  These strings are not allocated
535      with xmalloc; instead, they are pointers into debug_line_buffer.
536      Don't try to free them directly.  */
537   unsigned int num_file_names, file_names_size;
538   struct file_entry
539   {
540     char *name;
541     unsigned int dir_index;
542     unsigned int mod_time;
543     unsigned int length;
544     int included_p; /* Non-zero if referenced by the Line Number Program.  */
545     struct symtab *symtab; /* The associated symbol table, if any.  */
546   } *file_names;
547
548   /* The start and end of the statement program following this
549      header.  These point into dwarf2_per_objfile->line_buffer.  */
550   gdb_byte *statement_program_start, *statement_program_end;
551 };
552
553 /* When we construct a partial symbol table entry we only
554    need this much information. */
555 struct partial_die_info
556   {
557     /* Offset of this DIE.  */
558     unsigned int offset;
559
560     /* DWARF-2 tag for this DIE.  */
561     ENUM_BITFIELD(dwarf_tag) tag : 16;
562
563     /* Assorted flags describing the data found in this DIE.  */
564     unsigned int has_children : 1;
565     unsigned int is_external : 1;
566     unsigned int is_declaration : 1;
567     unsigned int has_type : 1;
568     unsigned int has_specification : 1;
569     unsigned int has_pc_info : 1;
570
571     /* Flag set if the SCOPE field of this structure has been
572        computed.  */
573     unsigned int scope_set : 1;
574
575     /* Flag set if the DIE has a byte_size attribute.  */
576     unsigned int has_byte_size : 1;
577
578     /* Flag set if any of the DIE's children are template arguments.  */
579     unsigned int has_template_arguments : 1;
580
581     /* The name of this DIE.  Normally the value of DW_AT_name, but
582        sometimes a default name for unnamed DIEs.  */
583     char *name;
584
585     /* The scope to prepend to our children.  This is generally
586        allocated on the comp_unit_obstack, so will disappear
587        when this compilation unit leaves the cache.  */
588     char *scope;
589
590     /* The location description associated with this DIE, if any.  */
591     struct dwarf_block *locdesc;
592
593     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
594     CORE_ADDR lowpc;
595     CORE_ADDR highpc;
596
597     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
598        DW_AT_sibling, if any.  */
599     gdb_byte *sibling;
600
601     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
602        DW_AT_specification (or DW_AT_abstract_origin or
603        DW_AT_extension).  */
604     unsigned int spec_offset;
605
606     /* Pointers to this DIE's parent, first child, and next sibling,
607        if any.  */
608     struct partial_die_info *die_parent, *die_child, *die_sibling;
609   };
610
611 /* This data structure holds the information of an abbrev. */
612 struct abbrev_info
613   {
614     unsigned int number;        /* number identifying abbrev */
615     enum dwarf_tag tag;         /* dwarf tag */
616     unsigned short has_children;                /* boolean */
617     unsigned short num_attrs;   /* number of attributes */
618     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
619     struct abbrev_info *next;   /* next in chain */
620   };
621
622 struct attr_abbrev
623   {
624     ENUM_BITFIELD(dwarf_attribute) name : 16;
625     ENUM_BITFIELD(dwarf_form) form : 16;
626   };
627
628 /* Attributes have a name and a value */
629 struct attribute
630   {
631     ENUM_BITFIELD(dwarf_attribute) name : 16;
632     ENUM_BITFIELD(dwarf_form) form : 15;
633
634     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
635        field should be in u.str (existing only for DW_STRING) but it is kept
636        here for better struct attribute alignment.  */
637     unsigned int string_is_canonical : 1;
638
639     union
640       {
641         char *str;
642         struct dwarf_block *blk;
643         ULONGEST unsnd;
644         LONGEST snd;
645         CORE_ADDR addr;
646         struct signatured_type *signatured_type;
647       }
648     u;
649   };
650
651 /* This data structure holds a complete die structure. */
652 struct die_info
653   {
654     /* DWARF-2 tag for this DIE.  */
655     ENUM_BITFIELD(dwarf_tag) tag : 16;
656
657     /* Number of attributes */
658     unsigned char num_attrs;
659
660     /* True if we're presently building the full type name for the
661        type derived from this DIE.  */
662     unsigned char building_fullname : 1;
663
664     /* Abbrev number */
665     unsigned int abbrev;
666
667     /* Offset in .debug_info or .debug_types section.  */
668     unsigned int offset;
669
670     /* The dies in a compilation unit form an n-ary tree.  PARENT
671        points to this die's parent; CHILD points to the first child of
672        this node; and all the children of a given node are chained
673        together via their SIBLING fields, terminated by a die whose
674        tag is zero.  */
675     struct die_info *child;     /* Its first child, if any.  */
676     struct die_info *sibling;   /* Its next sibling, if any.  */
677     struct die_info *parent;    /* Its parent, if any.  */
678
679     /* An array of attributes, with NUM_ATTRS elements.  There may be
680        zero, but it's not common and zero-sized arrays are not
681        sufficiently portable C.  */
682     struct attribute attrs[1];
683   };
684
685 struct function_range
686 {
687   const char *name;
688   CORE_ADDR lowpc, highpc;
689   int seen_line;
690   struct function_range *next;
691 };
692
693 /* Get at parts of an attribute structure */
694
695 #define DW_STRING(attr)    ((attr)->u.str)
696 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
697 #define DW_UNSND(attr)     ((attr)->u.unsnd)
698 #define DW_BLOCK(attr)     ((attr)->u.blk)
699 #define DW_SND(attr)       ((attr)->u.snd)
700 #define DW_ADDR(attr)      ((attr)->u.addr)
701 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
702
703 /* Blocks are a bunch of untyped bytes. */
704 struct dwarf_block
705   {
706     unsigned int size;
707     gdb_byte *data;
708   };
709
710 #ifndef ATTR_ALLOC_CHUNK
711 #define ATTR_ALLOC_CHUNK 4
712 #endif
713
714 /* Allocate fields for structs, unions and enums in this size.  */
715 #ifndef DW_FIELD_ALLOC_CHUNK
716 #define DW_FIELD_ALLOC_CHUNK 4
717 #endif
718
719 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
720    but this would require a corresponding change in unpack_field_as_long
721    and friends.  */
722 static int bits_per_byte = 8;
723
724 /* The routines that read and process dies for a C struct or C++ class
725    pass lists of data member fields and lists of member function fields
726    in an instance of a field_info structure, as defined below.  */
727 struct field_info
728   {
729     /* List of data member and baseclasses fields. */
730     struct nextfield
731       {
732         struct nextfield *next;
733         int accessibility;
734         int virtuality;
735         struct field field;
736       }
737      *fields, *baseclasses;
738
739     /* Number of fields (including baseclasses).  */
740     int nfields;
741
742     /* Number of baseclasses.  */
743     int nbaseclasses;
744
745     /* Set if the accesibility of one of the fields is not public.  */
746     int non_public_fields;
747
748     /* Member function fields array, entries are allocated in the order they
749        are encountered in the object file.  */
750     struct nextfnfield
751       {
752         struct nextfnfield *next;
753         struct fn_field fnfield;
754       }
755      *fnfields;
756
757     /* Member function fieldlist array, contains name of possibly overloaded
758        member function, number of overloaded member functions and a pointer
759        to the head of the member function field chain.  */
760     struct fnfieldlist
761       {
762         char *name;
763         int length;
764         struct nextfnfield *head;
765       }
766      *fnfieldlists;
767
768     /* Number of entries in the fnfieldlists array.  */
769     int nfnfields;
770
771     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
772        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
773     struct typedef_field_list
774       {
775         struct typedef_field field;
776         struct typedef_field_list *next;
777       }
778     *typedef_field_list;
779     unsigned typedef_field_list_count;
780   };
781
782 /* One item on the queue of compilation units to read in full symbols
783    for.  */
784 struct dwarf2_queue_item
785 {
786   struct dwarf2_per_cu_data *per_cu;
787   struct dwarf2_queue_item *next;
788 };
789
790 /* The current queue.  */
791 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
792
793 /* Loaded secondary compilation units are kept in memory until they
794    have not been referenced for the processing of this many
795    compilation units.  Set this to zero to disable caching.  Cache
796    sizes of up to at least twenty will improve startup time for
797    typical inter-CU-reference binaries, at an obvious memory cost.  */
798 static int dwarf2_max_cache_age = 5;
799 static void
800 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
801                            struct cmd_list_element *c, const char *value)
802 {
803   fprintf_filtered (file, _("\
804 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
805                     value);
806 }
807
808
809 /* Various complaints about symbol reading that don't abort the process */
810
811 static void
812 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
813 {
814   complaint (&symfile_complaints,
815              _("statement list doesn't fit in .debug_line section"));
816 }
817
818 static void
819 dwarf2_debug_line_missing_file_complaint (void)
820 {
821   complaint (&symfile_complaints,
822              _(".debug_line section has line data without a file"));
823 }
824
825 static void
826 dwarf2_debug_line_missing_end_sequence_complaint (void)
827 {
828   complaint (&symfile_complaints,
829              _(".debug_line section has line program sequence without an end"));
830 }
831
832 static void
833 dwarf2_complex_location_expr_complaint (void)
834 {
835   complaint (&symfile_complaints, _("location expression too complex"));
836 }
837
838 static void
839 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
840                                               int arg3)
841 {
842   complaint (&symfile_complaints,
843              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
844              arg2, arg3);
845 }
846
847 static void
848 dwarf2_macros_too_long_complaint (void)
849 {
850   complaint (&symfile_complaints,
851              _("macro info runs off end of `.debug_macinfo' section"));
852 }
853
854 static void
855 dwarf2_macro_malformed_definition_complaint (const char *arg1)
856 {
857   complaint (&symfile_complaints,
858              _("macro debug info contains a malformed macro definition:\n`%s'"),
859              arg1);
860 }
861
862 static void
863 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
864 {
865   complaint (&symfile_complaints,
866              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
867 }
868
869 /* local function prototypes */
870
871 static void dwarf2_locate_sections (bfd *, asection *, void *);
872
873 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
874                                            struct objfile *);
875
876 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
877                                            struct die_info *,
878                                            struct partial_symtab *);
879
880 static void dwarf2_build_psymtabs_hard (struct objfile *);
881
882 static void scan_partial_symbols (struct partial_die_info *,
883                                   CORE_ADDR *, CORE_ADDR *,
884                                   int, struct dwarf2_cu *);
885
886 static void add_partial_symbol (struct partial_die_info *,
887                                 struct dwarf2_cu *);
888
889 static void add_partial_namespace (struct partial_die_info *pdi,
890                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
891                                    int need_pc, struct dwarf2_cu *cu);
892
893 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
894                                 CORE_ADDR *highpc, int need_pc,
895                                 struct dwarf2_cu *cu);
896
897 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
898                                      struct dwarf2_cu *cu);
899
900 static void add_partial_subprogram (struct partial_die_info *pdi,
901                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
902                                     int need_pc, struct dwarf2_cu *cu);
903
904 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
905                                      gdb_byte *buffer, gdb_byte *info_ptr,
906                                      bfd *abfd, struct dwarf2_cu *cu);
907
908 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
909
910 static void psymtab_to_symtab_1 (struct partial_symtab *);
911
912 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
913
914 static void dwarf2_free_abbrev_table (void *);
915
916 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
917                                             struct dwarf2_cu *);
918
919 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
920                                                  struct dwarf2_cu *);
921
922 static struct partial_die_info *load_partial_dies (bfd *,
923                                                    gdb_byte *, gdb_byte *,
924                                                    int, struct dwarf2_cu *);
925
926 static gdb_byte *read_partial_die (struct partial_die_info *,
927                                    struct abbrev_info *abbrev,
928                                    unsigned int, bfd *,
929                                    gdb_byte *, gdb_byte *,
930                                    struct dwarf2_cu *);
931
932 static struct partial_die_info *find_partial_die (unsigned int,
933                                                   struct dwarf2_cu *);
934
935 static void fixup_partial_die (struct partial_die_info *,
936                                struct dwarf2_cu *);
937
938 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
939                                  bfd *, gdb_byte *, struct dwarf2_cu *);
940
941 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
942                                        bfd *, gdb_byte *, struct dwarf2_cu *);
943
944 static unsigned int read_1_byte (bfd *, gdb_byte *);
945
946 static int read_1_signed_byte (bfd *, gdb_byte *);
947
948 static unsigned int read_2_bytes (bfd *, gdb_byte *);
949
950 static unsigned int read_4_bytes (bfd *, gdb_byte *);
951
952 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
953
954 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
955                                unsigned int *);
956
957 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
958
959 static LONGEST read_checked_initial_length_and_offset
960   (bfd *, gdb_byte *, const struct comp_unit_head *,
961    unsigned int *, unsigned int *);
962
963 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
964                             unsigned int *);
965
966 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
967
968 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
969
970 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
971
972 static char *read_indirect_string (bfd *, gdb_byte *,
973                                    const struct comp_unit_head *,
974                                    unsigned int *);
975
976 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
977
978 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
979
980 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
981
982 static void set_cu_language (unsigned int, struct dwarf2_cu *);
983
984 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
985                                       struct dwarf2_cu *);
986
987 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
988                                                 unsigned int,
989                                                 struct dwarf2_cu *);
990
991 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
992                                struct dwarf2_cu *cu);
993
994 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
995
996 static struct die_info *die_specification (struct die_info *die,
997                                            struct dwarf2_cu **);
998
999 static void free_line_header (struct line_header *lh);
1000
1001 static void add_file_name (struct line_header *, char *, unsigned int,
1002                            unsigned int, unsigned int);
1003
1004 static struct line_header *(dwarf_decode_line_header
1005                             (unsigned int offset,
1006                              bfd *abfd, struct dwarf2_cu *cu));
1007
1008 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1009                                 struct dwarf2_cu *, struct partial_symtab *);
1010
1011 static void dwarf2_start_subfile (char *, char *, char *);
1012
1013 static struct symbol *new_symbol (struct die_info *, struct type *,
1014                                   struct dwarf2_cu *);
1015
1016 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1017                                        struct dwarf2_cu *, struct symbol *);
1018
1019 static void dwarf2_const_value (struct attribute *, struct symbol *,
1020                                 struct dwarf2_cu *);
1021
1022 static void dwarf2_const_value_attr (struct attribute *attr,
1023                                      struct type *type,
1024                                      const char *name,
1025                                      struct obstack *obstack,
1026                                      struct dwarf2_cu *cu, long *value,
1027                                      gdb_byte **bytes,
1028                                      struct dwarf2_locexpr_baton **baton);
1029
1030 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1031
1032 static int need_gnat_info (struct dwarf2_cu *);
1033
1034 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1035
1036 static void set_descriptive_type (struct type *, struct die_info *,
1037                                   struct dwarf2_cu *);
1038
1039 static struct type *die_containing_type (struct die_info *,
1040                                          struct dwarf2_cu *);
1041
1042 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
1043
1044 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1045
1046 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1047
1048 static char *typename_concat (struct obstack *obs, const char *prefix,
1049                               const char *suffix, int physname,
1050                               struct dwarf2_cu *cu);
1051
1052 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1053
1054 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1055
1056 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1057
1058 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1059
1060 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1061                                struct dwarf2_cu *, struct partial_symtab *);
1062
1063 static int dwarf2_get_pc_bounds (struct die_info *,
1064                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1065                                  struct partial_symtab *);
1066
1067 static void get_scope_pc_bounds (struct die_info *,
1068                                  CORE_ADDR *, CORE_ADDR *,
1069                                  struct dwarf2_cu *);
1070
1071 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1072                                         CORE_ADDR, struct dwarf2_cu *);
1073
1074 static void dwarf2_add_field (struct field_info *, struct die_info *,
1075                               struct dwarf2_cu *);
1076
1077 static void dwarf2_attach_fields_to_type (struct field_info *,
1078                                           struct type *, struct dwarf2_cu *);
1079
1080 static void dwarf2_add_member_fn (struct field_info *,
1081                                   struct die_info *, struct type *,
1082                                   struct dwarf2_cu *);
1083
1084 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1085                                              struct type *, struct dwarf2_cu *);
1086
1087 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1090
1091 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1092
1093 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1094
1095 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1096
1097 static struct type *read_module_type (struct die_info *die,
1098                                       struct dwarf2_cu *cu);
1099
1100 static const char *namespace_name (struct die_info *die,
1101                                    int *is_anonymous, struct dwarf2_cu *);
1102
1103 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1104
1105 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1106
1107 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1108                                                        struct dwarf2_cu *);
1109
1110 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1111
1112 static struct die_info *read_die_and_children_1 (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 struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1118                                                gdb_byte *info_ptr,
1119                                                gdb_byte **new_info_ptr,
1120                                                struct die_info *parent);
1121
1122 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1123                                                gdb_byte *info_ptr,
1124                                                gdb_byte **new_info_ptr,
1125                                                struct die_info *parent);
1126
1127 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1128                                 struct die_info **, gdb_byte *,
1129                                 int *);
1130
1131 static void process_die (struct die_info *, struct dwarf2_cu *);
1132
1133 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1134                                        struct obstack *);
1135
1136 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1137
1138 static const char *dwarf2_full_name (char *name,
1139                                      struct die_info *die,
1140                                      struct dwarf2_cu *cu);
1141
1142 static struct die_info *dwarf2_extension (struct die_info *die,
1143                                           struct dwarf2_cu **);
1144
1145 static char *dwarf_tag_name (unsigned int);
1146
1147 static char *dwarf_attr_name (unsigned int);
1148
1149 static char *dwarf_form_name (unsigned int);
1150
1151 static char *dwarf_bool_name (unsigned int);
1152
1153 static char *dwarf_type_encoding_name (unsigned int);
1154
1155 #if 0
1156 static char *dwarf_cfi_name (unsigned int);
1157 #endif
1158
1159 static struct die_info *sibling_die (struct die_info *);
1160
1161 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1162
1163 static void dump_die_for_error (struct die_info *);
1164
1165 static void dump_die_1 (struct ui_file *, int level, int max_level,
1166                         struct die_info *);
1167
1168 /*static*/ void dump_die (struct die_info *, int max_level);
1169
1170 static void store_in_ref_table (struct die_info *,
1171                                 struct dwarf2_cu *);
1172
1173 static int is_ref_attr (struct attribute *);
1174
1175 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1176
1177 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1178
1179 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1180                                                struct attribute *,
1181                                                struct dwarf2_cu **);
1182
1183 static struct die_info *follow_die_ref (struct die_info *,
1184                                         struct attribute *,
1185                                         struct dwarf2_cu **);
1186
1187 static struct die_info *follow_die_sig (struct die_info *,
1188                                         struct attribute *,
1189                                         struct dwarf2_cu **);
1190
1191 static void read_signatured_type_at_offset (struct objfile *objfile,
1192                                             unsigned int offset);
1193
1194 static void read_signatured_type (struct objfile *,
1195                                   struct signatured_type *type_sig);
1196
1197 /* memory allocation interface */
1198
1199 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1200
1201 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1202
1203 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1204
1205 static void initialize_cu_func_list (struct dwarf2_cu *);
1206
1207 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1208                                  struct dwarf2_cu *);
1209
1210 static void dwarf_decode_macros (struct line_header *, unsigned int,
1211                                  char *, bfd *, struct dwarf2_cu *);
1212
1213 static int attr_form_is_block (struct attribute *);
1214
1215 static int attr_form_is_section_offset (struct attribute *);
1216
1217 static int attr_form_is_constant (struct attribute *);
1218
1219 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1220                                          struct symbol *sym,
1221                                          struct dwarf2_cu *cu);
1222
1223 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1224                                struct abbrev_info *abbrev,
1225                                struct dwarf2_cu *cu);
1226
1227 static void free_stack_comp_unit (void *);
1228
1229 static hashval_t partial_die_hash (const void *item);
1230
1231 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1232
1233 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1234   (unsigned int offset, struct objfile *objfile);
1235
1236 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1237   (unsigned int offset, struct objfile *objfile);
1238
1239 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1240
1241 static void free_one_comp_unit (void *);
1242
1243 static void free_cached_comp_units (void *);
1244
1245 static void age_cached_comp_units (void);
1246
1247 static void free_one_cached_comp_unit (void *);
1248
1249 static struct type *set_die_type (struct die_info *, struct type *,
1250                                   struct dwarf2_cu *);
1251
1252 static void create_all_comp_units (struct objfile *);
1253
1254 static int create_debug_types_hash_table (struct objfile *objfile);
1255
1256 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1257                                  struct objfile *);
1258
1259 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1260
1261 static void dwarf2_add_dependence (struct dwarf2_cu *,
1262                                    struct dwarf2_per_cu_data *);
1263
1264 static void dwarf2_mark (struct dwarf2_cu *);
1265
1266 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1267
1268 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void dwarf2_release_queue (void *dummy);
1271
1272 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1273                              struct objfile *objfile);
1274
1275 static void process_queue (struct objfile *objfile);
1276
1277 static void find_file_and_directory (struct die_info *die,
1278                                      struct dwarf2_cu *cu,
1279                                      char **name, char **comp_dir);
1280
1281 static char *file_full_name (int file, struct line_header *lh,
1282                              const char *comp_dir);
1283
1284 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1285                                               gdb_byte *info_ptr,
1286                                               gdb_byte *buffer,
1287                                               unsigned int buffer_size,
1288                                               bfd *abfd);
1289
1290 static void init_cu_die_reader (struct die_reader_specs *reader,
1291                                 struct dwarf2_cu *cu);
1292
1293 static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1294
1295 #if WORDS_BIGENDIAN
1296
1297 /* Convert VALUE between big- and little-endian.  */
1298 static offset_type
1299 byte_swap (offset_type value)
1300 {
1301   offset_type result;
1302
1303   result = (value & 0xff) << 24;
1304   result |= (value & 0xff00) << 8;
1305   result |= (value & 0xff0000) >> 8;
1306   result |= (value & 0xff000000) >> 24;
1307   return result;
1308 }
1309
1310 #define MAYBE_SWAP(V)  byte_swap (V)
1311
1312 #else
1313 #define MAYBE_SWAP(V) (V)
1314 #endif /* WORDS_BIGENDIAN */
1315
1316 /* The suffix for an index file.  */
1317 #define INDEX_SUFFIX ".gdb-index"
1318
1319 static const char *dwarf2_physname (char *name, struct die_info *die,
1320                                     struct dwarf2_cu *cu);
1321
1322 /* Try to locate the sections we need for DWARF 2 debugging
1323    information and return true if we have enough to do something.  */
1324
1325 int
1326 dwarf2_has_info (struct objfile *objfile)
1327 {
1328   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329   if (!dwarf2_per_objfile)
1330     {
1331       /* Initialize per-objfile state.  */
1332       struct dwarf2_per_objfile *data
1333         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1334
1335       memset (data, 0, sizeof (*data));
1336       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337       dwarf2_per_objfile = data;
1338
1339       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1340       dwarf2_per_objfile->objfile = objfile;
1341     }
1342   return (dwarf2_per_objfile->info.asection != NULL
1343           && dwarf2_per_objfile->abbrev.asection != NULL);
1344 }
1345
1346 /* When loading sections, we can either look for ".<name>", or for
1347  * ".z<name>", which indicates a compressed section.  */
1348
1349 static int
1350 section_is_p (const char *section_name, const char *name)
1351 {
1352   return (section_name[0] == '.'
1353           && (strcmp (section_name + 1, name) == 0
1354               || (section_name[1] == 'z'
1355                   && strcmp (section_name + 2, name) == 0)));
1356 }
1357
1358 /* This function is mapped across the sections and remembers the
1359    offset and size of each of the debugging sections we are interested
1360    in.  */
1361
1362 static void
1363 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1364 {
1365   if (section_is_p (sectp->name, INFO_SECTION))
1366     {
1367       dwarf2_per_objfile->info.asection = sectp;
1368       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1369     }
1370   else if (section_is_p (sectp->name, ABBREV_SECTION))
1371     {
1372       dwarf2_per_objfile->abbrev.asection = sectp;
1373       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1374     }
1375   else if (section_is_p (sectp->name, LINE_SECTION))
1376     {
1377       dwarf2_per_objfile->line.asection = sectp;
1378       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1379     }
1380   else if (section_is_p (sectp->name, LOC_SECTION))
1381     {
1382       dwarf2_per_objfile->loc.asection = sectp;
1383       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1384     }
1385   else if (section_is_p (sectp->name, MACINFO_SECTION))
1386     {
1387       dwarf2_per_objfile->macinfo.asection = sectp;
1388       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1389     }
1390   else if (section_is_p (sectp->name, STR_SECTION))
1391     {
1392       dwarf2_per_objfile->str.asection = sectp;
1393       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1394     }
1395   else if (section_is_p (sectp->name, FRAME_SECTION))
1396     {
1397       dwarf2_per_objfile->frame.asection = sectp;
1398       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1399     }
1400   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1401     {
1402       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1403
1404       if (aflag & SEC_HAS_CONTENTS)
1405         {
1406           dwarf2_per_objfile->eh_frame.asection = sectp;
1407           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1408         }
1409     }
1410   else if (section_is_p (sectp->name, RANGES_SECTION))
1411     {
1412       dwarf2_per_objfile->ranges.asection = sectp;
1413       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1414     }
1415   else if (section_is_p (sectp->name, TYPES_SECTION))
1416     {
1417       dwarf2_per_objfile->types.asection = sectp;
1418       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1419     }
1420   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1421     {
1422       dwarf2_per_objfile->gdb_index.asection = sectp;
1423       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1424     }
1425
1426   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1427       && bfd_section_vma (abfd, sectp) == 0)
1428     dwarf2_per_objfile->has_section_at_zero = 1;
1429 }
1430
1431 /* Decompress a section that was compressed using zlib.  Store the
1432    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1433
1434 static void
1435 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1436                          gdb_byte **outbuf, bfd_size_type *outsize)
1437 {
1438   bfd *abfd = objfile->obfd;
1439 #ifndef HAVE_ZLIB_H
1440   error (_("Support for zlib-compressed DWARF data (from '%s') "
1441            "is disabled in this copy of GDB"),
1442          bfd_get_filename (abfd));
1443 #else
1444   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1445   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1446   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1447   bfd_size_type uncompressed_size;
1448   gdb_byte *uncompressed_buffer;
1449   z_stream strm;
1450   int rc;
1451   int header_size = 12;
1452
1453   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1454       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1455     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1456            bfd_get_filename (abfd));
1457
1458   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1459      by the uncompressed section size, 8 bytes in big-endian order.  */
1460   if (compressed_size < header_size
1461       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1462     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1463            bfd_get_filename (abfd));
1464   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1465   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1466   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1467   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1468   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1469   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1470   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1471   uncompressed_size += compressed_buffer[11];
1472
1473   /* It is possible the section consists of several compressed
1474      buffers concatenated together, so we uncompress in a loop.  */
1475   strm.zalloc = NULL;
1476   strm.zfree = NULL;
1477   strm.opaque = NULL;
1478   strm.avail_in = compressed_size - header_size;
1479   strm.next_in = (Bytef*) compressed_buffer + header_size;
1480   strm.avail_out = uncompressed_size;
1481   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1482                                        uncompressed_size);
1483   rc = inflateInit (&strm);
1484   while (strm.avail_in > 0)
1485     {
1486       if (rc != Z_OK)
1487         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1488                bfd_get_filename (abfd), rc);
1489       strm.next_out = ((Bytef*) uncompressed_buffer
1490                        + (uncompressed_size - strm.avail_out));
1491       rc = inflate (&strm, Z_FINISH);
1492       if (rc != Z_STREAM_END)
1493         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1494                bfd_get_filename (abfd), rc);
1495       rc = inflateReset (&strm);
1496     }
1497   rc = inflateEnd (&strm);
1498   if (rc != Z_OK
1499       || strm.avail_out != 0)
1500     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1501            bfd_get_filename (abfd), rc);
1502
1503   do_cleanups (cleanup);
1504   *outbuf = uncompressed_buffer;
1505   *outsize = uncompressed_size;
1506 #endif
1507 }
1508
1509 /* Read the contents of the section SECTP from object file specified by
1510    OBJFILE, store info about the section into INFO.
1511    If the section is compressed, uncompress it before returning.  */
1512
1513 static void
1514 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1515 {
1516   bfd *abfd = objfile->obfd;
1517   asection *sectp = info->asection;
1518   gdb_byte *buf, *retbuf;
1519   unsigned char header[4];
1520
1521   if (info->readin)
1522     return;
1523   info->buffer = NULL;
1524   info->was_mmapped = 0;
1525   info->readin = 1;
1526
1527   if (info->asection == NULL || info->size == 0)
1528     return;
1529
1530   /* Check if the file has a 4-byte header indicating compression.  */
1531   if (info->size > sizeof (header)
1532       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1533       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1534     {
1535       /* Upon decompression, update the buffer and its size.  */
1536       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1537         {
1538           zlib_decompress_section (objfile, sectp, &info->buffer,
1539                                    &info->size);
1540           return;
1541         }
1542     }
1543
1544 #ifdef HAVE_MMAP
1545   if (pagesize == 0)
1546     pagesize = getpagesize ();
1547
1548   /* Only try to mmap sections which are large enough: we don't want to
1549      waste space due to fragmentation.  Also, only try mmap for sections
1550      without relocations.  */
1551
1552   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1553     {
1554       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1555       size_t map_length = info->size + sectp->filepos - pg_offset;
1556       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1557                                  MAP_PRIVATE, pg_offset);
1558
1559       if (retbuf != MAP_FAILED)
1560         {
1561           info->was_mmapped = 1;
1562           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1563 #if HAVE_POSIX_MADVISE
1564           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1565 #endif
1566           return;
1567         }
1568     }
1569 #endif
1570
1571   /* If we get here, we are a normal, not-compressed section.  */
1572   info->buffer = buf
1573     = obstack_alloc (&objfile->objfile_obstack, info->size);
1574
1575   /* When debugging .o files, we may need to apply relocations; see
1576      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1577      We never compress sections in .o files, so we only need to
1578      try this when the section is not compressed.  */
1579   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1580   if (retbuf != NULL)
1581     {
1582       info->buffer = retbuf;
1583       return;
1584     }
1585
1586   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1587       || bfd_bread (buf, info->size, abfd) != info->size)
1588     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1589            bfd_get_filename (abfd));
1590 }
1591
1592 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1593    SECTION_NAME. */
1594
1595 void
1596 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1597                          asection **sectp, gdb_byte **bufp,
1598                          bfd_size_type *sizep)
1599 {
1600   struct dwarf2_per_objfile *data
1601     = objfile_data (objfile, dwarf2_objfile_data_key);
1602   struct dwarf2_section_info *info;
1603
1604   /* We may see an objfile without any DWARF, in which case we just
1605      return nothing.  */
1606   if (data == NULL)
1607     {
1608       *sectp = NULL;
1609       *bufp = NULL;
1610       *sizep = 0;
1611       return;
1612     }
1613   if (section_is_p (section_name, EH_FRAME_SECTION))
1614     info = &data->eh_frame;
1615   else if (section_is_p (section_name, FRAME_SECTION))
1616     info = &data->frame;
1617   else
1618     gdb_assert_not_reached ("unexpected section");
1619
1620   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1621     /* We haven't read this section in yet.  Do it now.  */
1622     dwarf2_read_section (objfile, info);
1623
1624   *sectp = info->asection;
1625   *bufp = info->buffer;
1626   *sizep = info->size;
1627 }
1628
1629 \f
1630
1631 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1632    this CU came.  */
1633 static void
1634 dw2_do_instantiate_symtab (struct objfile *objfile,
1635                            struct dwarf2_per_cu_data *per_cu)
1636 {
1637   struct cleanup *back_to;
1638
1639   back_to = make_cleanup (dwarf2_release_queue, NULL);
1640
1641   queue_comp_unit (per_cu, objfile);
1642
1643   if (per_cu->from_debug_types)
1644     read_signatured_type_at_offset (objfile, per_cu->offset);
1645   else
1646     load_full_comp_unit (per_cu, objfile);
1647
1648   process_queue (objfile);
1649
1650   /* Age the cache, releasing compilation units that have not
1651      been used recently.  */
1652   age_cached_comp_units ();
1653
1654   do_cleanups (back_to);
1655 }
1656
1657 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1658    the objfile from which this CU came.  Returns the resulting symbol
1659    table.  */
1660 static struct symtab *
1661 dw2_instantiate_symtab (struct objfile *objfile,
1662                         struct dwarf2_per_cu_data *per_cu)
1663 {
1664   if (!per_cu->v.quick->symtab)
1665     {
1666       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1667       increment_reading_symtab ();
1668       dw2_do_instantiate_symtab (objfile, per_cu);
1669       do_cleanups (back_to);
1670     }
1671   return per_cu->v.quick->symtab;
1672 }
1673
1674 /* Return the CU given its index.  */
1675 static struct dwarf2_per_cu_data *
1676 dw2_get_cu (int index)
1677 {
1678   if (index >= dwarf2_per_objfile->n_comp_units)
1679     {
1680       index -= dwarf2_per_objfile->n_comp_units;
1681       return dwarf2_per_objfile->type_comp_units[index];
1682     }
1683   return dwarf2_per_objfile->all_comp_units[index];
1684 }
1685
1686 /* A helper function that knows how to read a 64-bit value in a way
1687    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1688    otherwise.  */
1689 static int
1690 extract_cu_value (const char *bytes, ULONGEST *result)
1691 {
1692   if (sizeof (ULONGEST) < 8)
1693     {
1694       int i;
1695
1696       /* Ignore the upper 4 bytes if they are all zero.  */
1697       for (i = 0; i < 4; ++i)
1698         if (bytes[i + 4] != 0)
1699           return 0;
1700
1701       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1702     }
1703   else
1704     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1705   return 1;
1706 }
1707
1708 /* Read the CU list from the mapped index, and use it to create all
1709    the CU objects for this objfile.  Return 0 if something went wrong,
1710    1 if everything went ok.  */
1711 static int
1712 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1713                        offset_type cu_list_elements)
1714 {
1715   offset_type i;
1716
1717   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1718   dwarf2_per_objfile->all_comp_units
1719     = obstack_alloc (&objfile->objfile_obstack,
1720                      dwarf2_per_objfile->n_comp_units
1721                      * sizeof (struct dwarf2_per_cu_data *));
1722
1723   for (i = 0; i < cu_list_elements; i += 2)
1724     {
1725       struct dwarf2_per_cu_data *the_cu;
1726       ULONGEST offset, length;
1727
1728       if (!extract_cu_value (cu_list, &offset)
1729           || !extract_cu_value (cu_list + 8, &length))
1730         return 0;
1731       cu_list += 2 * 8;
1732
1733       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734                                struct dwarf2_per_cu_data);
1735       the_cu->offset = offset;
1736       the_cu->length = length;
1737       the_cu->objfile = objfile;
1738       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739                                         struct dwarf2_per_cu_quick_data);
1740       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1741     }
1742
1743   return 1;
1744 }
1745
1746 /* Create the signatured type hash table from the index.  */
1747 static int
1748 create_signatured_type_hash_from_index (struct objfile *objfile,
1749                                         const gdb_byte *bytes,
1750                                         offset_type elements)
1751 {
1752   offset_type i;
1753   htab_t type_hash;
1754
1755   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1756   dwarf2_per_objfile->type_comp_units
1757     = obstack_alloc (&objfile->objfile_obstack,
1758                      dwarf2_per_objfile->n_type_comp_units
1759                      * sizeof (struct dwarf2_per_cu_data *));
1760
1761   type_hash = allocate_signatured_type_hash_table (objfile);
1762
1763   for (i = 0; i < elements; i += 3)
1764     {
1765       struct signatured_type *type_sig;
1766       ULONGEST offset, type_offset, signature;
1767       void **slot;
1768
1769       if (!extract_cu_value (bytes, &offset)
1770           || !extract_cu_value (bytes + 8, &type_offset))
1771         return 0;
1772       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1773       bytes += 3 * 8;
1774
1775       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1776                                  struct signatured_type);
1777       type_sig->signature = signature;
1778       type_sig->offset = offset;
1779       type_sig->type_offset = type_offset;
1780       type_sig->per_cu.from_debug_types = 1;
1781       type_sig->per_cu.offset = offset;
1782       type_sig->per_cu.objfile = objfile;
1783       type_sig->per_cu.v.quick
1784         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1785                           struct dwarf2_per_cu_quick_data);
1786
1787       slot = htab_find_slot (type_hash, type_sig, INSERT);
1788       *slot = type_sig;
1789
1790       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1791     }
1792
1793   dwarf2_per_objfile->signatured_types = type_hash;
1794
1795   return 1;
1796 }
1797
1798 /* Read the address map data from the mapped index, and use it to
1799    populate the objfile's psymtabs_addrmap.  */
1800 static void
1801 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1802 {
1803   const gdb_byte *iter, *end;
1804   struct obstack temp_obstack;
1805   struct addrmap *mutable_map;
1806   struct cleanup *cleanup;
1807   CORE_ADDR baseaddr;
1808
1809   obstack_init (&temp_obstack);
1810   cleanup = make_cleanup_obstack_free (&temp_obstack);
1811   mutable_map = addrmap_create_mutable (&temp_obstack);
1812
1813   iter = index->address_table;
1814   end = iter + index->address_table_size;
1815
1816   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1817
1818   while (iter < end)
1819     {
1820       ULONGEST hi, lo, cu_index;
1821       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1822       iter += 8;
1823       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1824       iter += 8;
1825       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1826       iter += 4;
1827       
1828       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1829                          dw2_get_cu (cu_index));
1830     }
1831
1832   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1833                                                     &objfile->objfile_obstack);
1834   do_cleanups (cleanup);
1835 }
1836
1837 /* The hash function for strings in the mapped index.  This is the
1838    same as the hashtab.c hash function, but we keep a separate copy to
1839    maintain control over the implementation.  This is necessary
1840    because the hash function is tied to the format of the mapped index
1841    file.  */
1842 static hashval_t
1843 mapped_index_string_hash (const void *p)
1844 {
1845   const unsigned char *str = (const unsigned char *) p;
1846   hashval_t r = 0;
1847   unsigned char c;
1848
1849   while ((c = *str++) != 0)
1850     r = r * 67 + c - 113;
1851
1852   return r;
1853 }
1854
1855 /* Find a slot in the mapped index INDEX for the object named NAME.
1856    If NAME is found, set *VEC_OUT to point to the CU vector in the
1857    constant pool and return 1.  If NAME cannot be found, return 0.  */
1858 static int
1859 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1860                           offset_type **vec_out)
1861 {
1862   offset_type hash = mapped_index_string_hash (name);
1863   offset_type slot, step;
1864
1865   slot = hash & (index->index_table_slots - 1);
1866   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1867
1868   for (;;)
1869     {
1870       /* Convert a slot number to an offset into the table.  */
1871       offset_type i = 2 * slot;
1872       const char *str;
1873       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1874         return 0;
1875
1876       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1877       if (!strcmp (name, str))
1878         {
1879           *vec_out = (offset_type *) (index->constant_pool
1880                                       + MAYBE_SWAP (index->index_table[i + 1]));
1881           return 1;
1882         }
1883
1884       slot = (slot + step) & (index->index_table_slots - 1);
1885     }
1886 }
1887
1888 /* Read the index file.  If everything went ok, initialize the "quick"
1889    elements of all the CUs and return 1.  Otherwise, return 0.  */
1890 static int
1891 dwarf2_read_index (struct objfile *objfile)
1892 {
1893   char *addr;
1894   struct mapped_index *map;
1895   offset_type *metadata;
1896   const gdb_byte *cu_list;
1897   const gdb_byte *types_list = NULL;
1898   offset_type version, cu_list_elements;
1899   offset_type types_list_elements = 0;
1900   int i;
1901
1902   if (dwarf2_per_objfile->gdb_index.asection == NULL
1903       || dwarf2_per_objfile->gdb_index.size == 0)
1904     return 0;
1905   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1906
1907   addr = dwarf2_per_objfile->gdb_index.buffer;
1908   /* Version check.  */
1909   version = MAYBE_SWAP (*(offset_type *) addr);
1910   if (version == 1)
1911     {
1912       /* Index version 1 neglected to account for .debug_types.  So,
1913          if we see .debug_types, we cannot use this index.  */
1914       if (dwarf2_per_objfile->types.asection != NULL
1915           && dwarf2_per_objfile->types.size != 0)
1916         return 0;
1917     }
1918   else if (version != 2)
1919     return 0;
1920
1921   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1922   map->total_size = dwarf2_per_objfile->gdb_index.size;
1923
1924   metadata = (offset_type *) (addr + sizeof (offset_type));
1925
1926   i = 0;
1927   cu_list = addr + MAYBE_SWAP (metadata[i]);
1928   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1929                       / 8);
1930   ++i;
1931
1932   if (version == 2)
1933     {
1934       types_list = addr + MAYBE_SWAP (metadata[i]);
1935       types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1936                               - MAYBE_SWAP (metadata[i]))
1937                              / 8);
1938       ++i;
1939     }
1940
1941   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1942   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1943                              - MAYBE_SWAP (metadata[i]));
1944   ++i;
1945
1946   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1947   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1948                              - MAYBE_SWAP (metadata[i]))
1949                             / (2 * sizeof (offset_type)));
1950   ++i;
1951
1952   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1953
1954   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1955     return 0;
1956
1957   if (version == 2
1958       && types_list_elements
1959       && !create_signatured_type_hash_from_index (objfile, types_list,
1960                                                   types_list_elements))
1961     return 0;
1962
1963   create_addrmap_from_index (objfile, map);
1964
1965   dwarf2_per_objfile->index_table = map;
1966   dwarf2_per_objfile->using_index = 1;
1967
1968   return 1;
1969 }
1970
1971 /* A helper for the "quick" functions which sets the global
1972    dwarf2_per_objfile according to OBJFILE.  */
1973 static void
1974 dw2_setup (struct objfile *objfile)
1975 {
1976   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1977   gdb_assert (dwarf2_per_objfile);
1978 }
1979
1980 /* A helper for the "quick" functions which attempts to read the line
1981    table for THIS_CU.  */
1982 static void
1983 dw2_require_line_header (struct objfile *objfile,
1984                          struct dwarf2_per_cu_data *this_cu)
1985 {
1986   bfd *abfd = objfile->obfd;
1987   struct line_header *lh = NULL;
1988   struct attribute *attr;
1989   struct cleanup *cleanups;
1990   struct die_info *comp_unit_die;
1991   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1992   int has_children, i;
1993   struct dwarf2_cu cu;
1994   unsigned int bytes_read, buffer_size;
1995   struct die_reader_specs reader_specs;
1996   char *name, *comp_dir;
1997
1998   if (this_cu->v.quick->read_lines)
1999     return;
2000   this_cu->v.quick->read_lines = 1;
2001
2002   memset (&cu, 0, sizeof (cu));
2003   cu.objfile = objfile;
2004   obstack_init (&cu.comp_unit_obstack);
2005
2006   cleanups = make_cleanup (free_stack_comp_unit, &cu);
2007
2008   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
2009   buffer_size = dwarf2_per_objfile->info.size;
2010   buffer = dwarf2_per_objfile->info.buffer;
2011   info_ptr = buffer + this_cu->offset;
2012   beg_of_comp_unit = info_ptr;
2013
2014   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2015                                           buffer, buffer_size,
2016                                           abfd);
2017
2018   /* Complete the cu_header.  */
2019   cu.header.offset = beg_of_comp_unit - buffer;
2020   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2021
2022   this_cu->cu = &cu;
2023   cu.per_cu = this_cu;
2024
2025   dwarf2_read_abbrevs (abfd, &cu);
2026   make_cleanup (dwarf2_free_abbrev_table, &cu);
2027
2028   if (this_cu->from_debug_types)
2029     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2030   init_cu_die_reader (&reader_specs, &cu);
2031   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2032                             &has_children);
2033
2034   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2035   if (attr)
2036     {
2037       unsigned int line_offset = DW_UNSND (attr);
2038       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2039     }
2040   if (lh == NULL)
2041     {
2042       do_cleanups (cleanups);
2043       return;
2044     }
2045
2046   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2047
2048   this_cu->v.quick->lines = lh;
2049
2050   this_cu->v.quick->file_names
2051     = obstack_alloc (&objfile->objfile_obstack,
2052                      lh->num_file_names * sizeof (char *));
2053   for (i = 0; i < lh->num_file_names; ++i)
2054     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2055
2056   do_cleanups (cleanups);
2057 }
2058
2059 /* A helper for the "quick" functions which computes and caches the
2060    real path for a given file name from the line table.
2061    dw2_require_line_header must have been called before this is
2062    invoked.  */
2063 static const char *
2064 dw2_require_full_path (struct objfile *objfile,
2065                        struct dwarf2_per_cu_data *cu,
2066                        int index)
2067 {
2068   if (!cu->v.quick->full_names)
2069     cu->v.quick->full_names
2070       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2071                         cu->v.quick->lines->num_file_names,
2072                         sizeof (char *));
2073
2074   if (!cu->v.quick->full_names[index])
2075     cu->v.quick->full_names[index]
2076       = gdb_realpath (cu->v.quick->file_names[index]);
2077
2078   return cu->v.quick->full_names[index];
2079 }
2080
2081 static struct symtab *
2082 dw2_find_last_source_symtab (struct objfile *objfile)
2083 {
2084   int index;
2085   dw2_setup (objfile);
2086   index = dwarf2_per_objfile->n_comp_units - 1;
2087   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2088 }
2089
2090 static void
2091 dw2_forget_cached_source_info (struct objfile *objfile)
2092 {
2093   int i;
2094
2095   dw2_setup (objfile);
2096   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2097                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2098     {
2099       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2100
2101       if (cu->v.quick->full_names)
2102         {
2103           int j;
2104
2105           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2106             xfree ((void *) cu->v.quick->full_names[j]);
2107         }
2108     }
2109 }
2110
2111 static int
2112 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2113                    const char *full_path, const char *real_path,
2114                    struct symtab **result)
2115 {
2116   int i;
2117   int check_basename = lbasename (name) == name;
2118   struct dwarf2_per_cu_data *base_cu = NULL;
2119
2120   dw2_setup (objfile);
2121   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2122                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2123     {
2124       int j;
2125       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2126
2127       if (cu->v.quick->symtab)
2128         continue;
2129
2130       dw2_require_line_header (objfile, cu);
2131       if (!cu->v.quick->lines)
2132         continue;
2133
2134       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2135         {
2136           const char *this_name = cu->v.quick->file_names[j];
2137
2138           if (FILENAME_CMP (name, this_name) == 0)
2139             {
2140               *result = dw2_instantiate_symtab (objfile, cu);
2141               return 1;
2142             }
2143
2144           if (check_basename && ! base_cu
2145               && FILENAME_CMP (lbasename (this_name), name) == 0)
2146             base_cu = cu;
2147
2148           if (full_path != NULL)
2149             {
2150               const char *this_full_name = dw2_require_full_path (objfile,
2151                                                                   cu, j);
2152
2153               if (this_full_name
2154                   && FILENAME_CMP (full_path, this_full_name) == 0)
2155                 {
2156                   *result = dw2_instantiate_symtab (objfile, cu);
2157                   return 1;
2158                 }
2159             }
2160
2161           if (real_path != NULL)
2162             {
2163               const char *this_full_name = dw2_require_full_path (objfile,
2164                                                                   cu, j);
2165
2166               if (this_full_name != NULL)
2167                 {
2168                   char *rp = gdb_realpath (this_full_name);
2169                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2170                     {
2171                       xfree (rp);
2172                       *result = dw2_instantiate_symtab (objfile, cu);
2173                       return 1;
2174                     }
2175                   xfree (rp);
2176                 }
2177             }
2178         }
2179     }
2180
2181   if (base_cu)
2182     {
2183       *result = dw2_instantiate_symtab (objfile, base_cu);
2184       return 1;
2185     }
2186
2187   return 0;
2188 }
2189
2190 static struct symtab *
2191 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2192                    const char *name, domain_enum domain)
2193 {
2194   /* We do all the work in the pre_expand_symtabs_matching hook
2195      instead.  */
2196   return NULL;
2197 }
2198
2199 /* A helper function that expands all symtabs that hold an object
2200    named NAME.  */
2201 static void
2202 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2203 {
2204   dw2_setup (objfile);
2205
2206   if (dwarf2_per_objfile->index_table)
2207     {
2208       offset_type *vec;
2209
2210       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2211                                     name, &vec))
2212         {
2213           offset_type i, len = MAYBE_SWAP (*vec);
2214           for (i = 0; i < len; ++i)
2215             {
2216               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2217               struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2218
2219               dw2_instantiate_symtab (objfile, cu);
2220             }
2221         }
2222     }
2223 }
2224
2225 static void
2226 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2227                                  int kind, const char *name,
2228                                  domain_enum domain)
2229 {
2230   dw2_do_expand_symtabs_matching (objfile, name);
2231 }
2232
2233 static void
2234 dw2_print_stats (struct objfile *objfile)
2235 {
2236   int i, count;
2237
2238   dw2_setup (objfile);
2239   count = 0;
2240   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2241                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2242     {
2243       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2244
2245       if (!cu->v.quick->symtab)
2246         ++count;
2247     }
2248   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2249 }
2250
2251 static void
2252 dw2_dump (struct objfile *objfile)
2253 {
2254   /* Nothing worth printing.  */
2255 }
2256
2257 static void
2258 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2259               struct section_offsets *delta)
2260 {
2261   /* There's nothing to relocate here.  */
2262 }
2263
2264 static void
2265 dw2_expand_symtabs_for_function (struct objfile *objfile,
2266                                  const char *func_name)
2267 {
2268   dw2_do_expand_symtabs_matching (objfile, func_name);
2269 }
2270
2271 static void
2272 dw2_expand_all_symtabs (struct objfile *objfile)
2273 {
2274   int i;
2275
2276   dw2_setup (objfile);
2277
2278   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2279                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2280     {
2281       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2282
2283       dw2_instantiate_symtab (objfile, cu);
2284     }
2285 }
2286
2287 static void
2288 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2289                                   const char *filename)
2290 {
2291   int i;
2292
2293   dw2_setup (objfile);
2294   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2295                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2296     {
2297       int j;
2298       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2299
2300       if (cu->v.quick->symtab)
2301         continue;
2302
2303       dw2_require_line_header (objfile, cu);
2304       if (!cu->v.quick->lines)
2305         continue;
2306
2307       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2308         {
2309           const char *this_name = cu->v.quick->file_names[j];
2310           if (strcmp (this_name, filename) == 0)
2311             {
2312               dw2_instantiate_symtab (objfile, cu);
2313               break;
2314             }
2315         }
2316     }
2317 }
2318
2319 static const char *
2320 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2321 {
2322   struct dwarf2_per_cu_data *cu;
2323   offset_type *vec;
2324
2325   dw2_setup (objfile);
2326
2327   if (!dwarf2_per_objfile->index_table)
2328     return NULL;
2329
2330   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2331                                  name, &vec))
2332     return NULL;
2333
2334   /* Note that this just looks at the very first one named NAME -- but
2335      actually we are looking for a function.  find_main_filename
2336      should be rewritten so that it doesn't require a custom hook.  It
2337      could just use the ordinary symbol tables.  */
2338   /* vec[0] is the length, which must always be >0.  */
2339   cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2340
2341   dw2_require_line_header (objfile, cu);
2342   if (!cu->v.quick->lines)
2343     return NULL;
2344
2345   return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2346 }
2347
2348 static void
2349 dw2_map_ada_symtabs (struct objfile *objfile,
2350                      int (*wild_match) (const char *, int, const char *),
2351                      int (*is_name_suffix) (const char *),
2352                      void (*callback) (struct objfile *,
2353                                        struct symtab *, void *),
2354                      const char *name, int global,
2355                      domain_enum namespace, int wild,
2356                      void *data)
2357 {
2358   /* For now, we don't support Ada, so this function can't be
2359      reached.  */
2360   internal_error (__FILE__, __LINE__,
2361                   _("map_ada_symtabs called via index method"));
2362 }
2363
2364 static void
2365 dw2_expand_symtabs_matching (struct objfile *objfile,
2366                              int (*file_matcher) (const char *, void *),
2367                              int (*name_matcher) (const char *, void *),
2368                              domain_enum kind,
2369                              void *data)
2370 {
2371   int i;
2372   offset_type iter;
2373
2374   dw2_setup (objfile);
2375   if (!dwarf2_per_objfile->index_table)
2376     return;
2377
2378   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2379                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2380     {
2381       int j;
2382       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2383
2384       cu->v.quick->mark = 0;
2385       if (cu->v.quick->symtab)
2386         continue;
2387
2388       dw2_require_line_header (objfile, cu);
2389       if (!cu->v.quick->lines)
2390         continue;
2391
2392       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2393         {
2394           if (file_matcher (cu->v.quick->file_names[j], data))
2395             {
2396               cu->v.quick->mark = 1;
2397               break;
2398             }
2399         }
2400     }
2401
2402   for (iter = 0;
2403        iter < dwarf2_per_objfile->index_table->index_table_slots;
2404        ++iter)
2405     {
2406       offset_type idx = 2 * iter;
2407       const char *name;
2408       offset_type *vec, vec_len, vec_idx;
2409
2410       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2411           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2412         continue;
2413
2414       name = (dwarf2_per_objfile->index_table->constant_pool
2415               + dwarf2_per_objfile->index_table->index_table[idx]);
2416
2417       if (! (*name_matcher) (name, data))
2418         continue;
2419
2420       /* The name was matched, now expand corresponding CUs that were
2421          marked.  */
2422       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2423                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2424       vec_len = MAYBE_SWAP (vec[0]);
2425       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2426         {
2427           struct dwarf2_per_cu_data *cu;
2428
2429           cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2430           if (cu->v.quick->mark)
2431             dw2_instantiate_symtab (objfile, cu);
2432         }
2433     }
2434 }
2435
2436 static struct symtab *
2437 dw2_find_pc_sect_symtab (struct objfile *objfile,
2438                          struct minimal_symbol *msymbol,
2439                          CORE_ADDR pc,
2440                          struct obj_section *section,
2441                          int warn_if_readin)
2442 {
2443   struct dwarf2_per_cu_data *data;
2444
2445   dw2_setup (objfile);
2446
2447   if (!objfile->psymtabs_addrmap)
2448     return NULL;
2449
2450   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2451   if (!data)
2452     return NULL;
2453
2454   if (warn_if_readin && data->v.quick->symtab)
2455     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2456              paddress (get_objfile_arch (objfile), pc));
2457
2458   return dw2_instantiate_symtab (objfile, data);
2459 }
2460
2461 static void
2462 dw2_map_symbol_names (struct objfile *objfile,
2463                       void (*fun) (const char *, void *),
2464                       void *data)
2465 {
2466   offset_type iter;
2467   dw2_setup (objfile);
2468
2469   if (!dwarf2_per_objfile->index_table)
2470     return;
2471
2472   for (iter = 0;
2473        iter < dwarf2_per_objfile->index_table->index_table_slots;
2474        ++iter)
2475     {
2476       offset_type idx = 2 * iter;
2477       const char *name;
2478       offset_type *vec, vec_len, vec_idx;
2479
2480       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2481           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2482         continue;
2483
2484       name = (dwarf2_per_objfile->index_table->constant_pool
2485               + dwarf2_per_objfile->index_table->index_table[idx]);
2486
2487       (*fun) (name, data);
2488     }
2489 }
2490
2491 static void
2492 dw2_map_symbol_filenames (struct objfile *objfile,
2493                           void (*fun) (const char *, const char *, void *),
2494                           void *data)
2495 {
2496   int i;
2497
2498   dw2_setup (objfile);
2499   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2500                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2501     {
2502       int j;
2503       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2504
2505       if (cu->v.quick->symtab)
2506         continue;
2507
2508       dw2_require_line_header (objfile, cu);
2509       if (!cu->v.quick->lines)
2510         continue;
2511
2512       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2513         {
2514           const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2515           (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2516         }
2517     }
2518 }
2519
2520 static int
2521 dw2_has_symbols (struct objfile *objfile)
2522 {
2523   return 1;
2524 }
2525
2526 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2527 {
2528   dw2_has_symbols,
2529   dw2_find_last_source_symtab,
2530   dw2_forget_cached_source_info,
2531   dw2_lookup_symtab,
2532   dw2_lookup_symbol,
2533   dw2_pre_expand_symtabs_matching,
2534   dw2_print_stats,
2535   dw2_dump,
2536   dw2_relocate,
2537   dw2_expand_symtabs_for_function,
2538   dw2_expand_all_symtabs,
2539   dw2_expand_symtabs_with_filename,
2540   dw2_find_symbol_file,
2541   dw2_map_ada_symtabs,
2542   dw2_expand_symtabs_matching,
2543   dw2_find_pc_sect_symtab,
2544   dw2_map_symbol_names,
2545   dw2_map_symbol_filenames
2546 };
2547
2548 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2549    file will use psymtabs, or 1 if using the GNU index.  */
2550
2551 int
2552 dwarf2_initialize_objfile (struct objfile *objfile)
2553 {
2554   /* If we're about to read full symbols, don't bother with the
2555      indices.  In this case we also don't care if some other debug
2556      format is making psymtabs, because they are all about to be
2557      expanded anyway.  */
2558   if ((objfile->flags & OBJF_READNOW))
2559     {
2560       int i;
2561
2562       dwarf2_per_objfile->using_index = 1;
2563       create_all_comp_units (objfile);
2564       create_debug_types_hash_table (objfile);
2565
2566       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2567                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2568         {
2569           struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2570
2571           cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2572                                         struct dwarf2_per_cu_quick_data);
2573         }
2574
2575       /* Return 1 so that gdb sees the "quick" functions.  However,
2576          these functions will be no-ops because we will have expanded
2577          all symtabs.  */
2578       return 1;
2579     }
2580
2581   if (dwarf2_read_index (objfile))
2582     return 1;
2583
2584   dwarf2_build_psymtabs (objfile);
2585   return 0;
2586 }
2587
2588 \f
2589
2590 /* Build a partial symbol table.  */
2591
2592 void
2593 dwarf2_build_psymtabs (struct objfile *objfile)
2594 {
2595   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2596     {
2597       init_psymbol_list (objfile, 1024);
2598     }
2599
2600   dwarf2_build_psymtabs_hard (objfile);
2601 }
2602
2603 /* Return TRUE if OFFSET is within CU_HEADER.  */
2604
2605 static inline int
2606 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2607 {
2608   unsigned int bottom = cu_header->offset;
2609   unsigned int top = (cu_header->offset
2610                       + cu_header->length
2611                       + cu_header->initial_length_size);
2612
2613   return (offset >= bottom && offset < top);
2614 }
2615
2616 /* Read in the comp unit header information from the debug_info at info_ptr.
2617    NOTE: This leaves members offset, first_die_offset to be filled in
2618    by the caller.  */
2619
2620 static gdb_byte *
2621 read_comp_unit_head (struct comp_unit_head *cu_header,
2622                      gdb_byte *info_ptr, bfd *abfd)
2623 {
2624   int signed_addr;
2625   unsigned int bytes_read;
2626
2627   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2628   cu_header->initial_length_size = bytes_read;
2629   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2630   info_ptr += bytes_read;
2631   cu_header->version = read_2_bytes (abfd, info_ptr);
2632   info_ptr += 2;
2633   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2634                                           &bytes_read);
2635   info_ptr += bytes_read;
2636   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2637   info_ptr += 1;
2638   signed_addr = bfd_get_sign_extend_vma (abfd);
2639   if (signed_addr < 0)
2640     internal_error (__FILE__, __LINE__,
2641                     _("read_comp_unit_head: dwarf from non elf file"));
2642   cu_header->signed_addr_p = signed_addr;
2643
2644   return info_ptr;
2645 }
2646
2647 static gdb_byte *
2648 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2649                              gdb_byte *buffer, unsigned int buffer_size,
2650                              bfd *abfd)
2651 {
2652   gdb_byte *beg_of_comp_unit = info_ptr;
2653
2654   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2655
2656   if (header->version != 2 && header->version != 3 && header->version != 4)
2657     error (_("Dwarf Error: wrong version in compilation unit header "
2658            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2659            bfd_get_filename (abfd));
2660
2661   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2662     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2663            "(offset 0x%lx + 6) [in module %s]"),
2664            (long) header->abbrev_offset,
2665            (long) (beg_of_comp_unit - buffer),
2666            bfd_get_filename (abfd));
2667
2668   if (beg_of_comp_unit + header->length + header->initial_length_size
2669       > buffer + buffer_size)
2670     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2671            "(offset 0x%lx + 0) [in module %s]"),
2672            (long) header->length,
2673            (long) (beg_of_comp_unit - buffer),
2674            bfd_get_filename (abfd));
2675
2676   return info_ptr;
2677 }
2678
2679 /* Read in the types comp unit header information from .debug_types entry at
2680    types_ptr.  The result is a pointer to one past the end of the header.  */
2681
2682 static gdb_byte *
2683 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2684                           ULONGEST *signature,
2685                           gdb_byte *types_ptr, bfd *abfd)
2686 {
2687   gdb_byte *initial_types_ptr = types_ptr;
2688
2689   dwarf2_read_section (dwarf2_per_objfile->objfile,
2690                        &dwarf2_per_objfile->types);
2691   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2692
2693   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2694
2695   *signature = read_8_bytes (abfd, types_ptr);
2696   types_ptr += 8;
2697   types_ptr += cu_header->offset_size;
2698   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2699
2700   return types_ptr;
2701 }
2702
2703 /* Allocate a new partial symtab for file named NAME and mark this new
2704    partial symtab as being an include of PST.  */
2705
2706 static void
2707 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2708                                struct objfile *objfile)
2709 {
2710   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2711
2712   subpst->section_offsets = pst->section_offsets;
2713   subpst->textlow = 0;
2714   subpst->texthigh = 0;
2715
2716   subpst->dependencies = (struct partial_symtab **)
2717     obstack_alloc (&objfile->objfile_obstack,
2718                    sizeof (struct partial_symtab *));
2719   subpst->dependencies[0] = pst;
2720   subpst->number_of_dependencies = 1;
2721
2722   subpst->globals_offset = 0;
2723   subpst->n_global_syms = 0;
2724   subpst->statics_offset = 0;
2725   subpst->n_static_syms = 0;
2726   subpst->symtab = NULL;
2727   subpst->read_symtab = pst->read_symtab;
2728   subpst->readin = 0;
2729
2730   /* No private part is necessary for include psymtabs.  This property
2731      can be used to differentiate between such include psymtabs and
2732      the regular ones.  */
2733   subpst->read_symtab_private = NULL;
2734 }
2735
2736 /* Read the Line Number Program data and extract the list of files
2737    included by the source file represented by PST.  Build an include
2738    partial symtab for each of these included files.  */
2739
2740 static void
2741 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2742                                struct die_info *die,
2743                                struct partial_symtab *pst)
2744 {
2745   struct objfile *objfile = cu->objfile;
2746   bfd *abfd = objfile->obfd;
2747   struct line_header *lh = NULL;
2748   struct attribute *attr;
2749
2750   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2751   if (attr)
2752     {
2753       unsigned int line_offset = DW_UNSND (attr);
2754
2755       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2756     }
2757   if (lh == NULL)
2758     return;  /* No linetable, so no includes.  */
2759
2760   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2761
2762   free_line_header (lh);
2763 }
2764
2765 static hashval_t
2766 hash_type_signature (const void *item)
2767 {
2768   const struct signatured_type *type_sig = item;
2769
2770   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2771   return type_sig->signature;
2772 }
2773
2774 static int
2775 eq_type_signature (const void *item_lhs, const void *item_rhs)
2776 {
2777   const struct signatured_type *lhs = item_lhs;
2778   const struct signatured_type *rhs = item_rhs;
2779
2780   return lhs->signature == rhs->signature;
2781 }
2782
2783 /* Allocate a hash table for signatured types.  */
2784
2785 static htab_t
2786 allocate_signatured_type_hash_table (struct objfile *objfile)
2787 {
2788   return htab_create_alloc_ex (41,
2789                                hash_type_signature,
2790                                eq_type_signature,
2791                                NULL,
2792                                &objfile->objfile_obstack,
2793                                hashtab_obstack_allocate,
2794                                dummy_obstack_deallocate);
2795 }
2796
2797 /* A helper function to add a signatured type CU to a list.  */
2798
2799 static int
2800 add_signatured_type_cu_to_list (void **slot, void *datum)
2801 {
2802   struct signatured_type *sigt = *slot;
2803   struct dwarf2_per_cu_data ***datap = datum;
2804
2805   **datap = &sigt->per_cu;
2806   ++*datap;
2807
2808   return 1;
2809 }
2810
2811 /* Create the hash table of all entries in the .debug_types section.
2812    The result is zero if there is an error (e.g. missing .debug_types section),
2813    otherwise non-zero.  */
2814
2815 static int
2816 create_debug_types_hash_table (struct objfile *objfile)
2817 {
2818   gdb_byte *info_ptr;
2819   htab_t types_htab;
2820   struct dwarf2_per_cu_data **iter;
2821
2822   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2823   info_ptr = dwarf2_per_objfile->types.buffer;
2824
2825   if (info_ptr == NULL)
2826     {
2827       dwarf2_per_objfile->signatured_types = NULL;
2828       return 0;
2829     }
2830
2831   types_htab = allocate_signatured_type_hash_table (objfile);
2832
2833   if (dwarf2_die_debug)
2834     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2835
2836   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2837     {
2838       unsigned int offset;
2839       unsigned int offset_size;
2840       unsigned int type_offset;
2841       unsigned int length, initial_length_size;
2842       unsigned short version;
2843       ULONGEST signature;
2844       struct signatured_type *type_sig;
2845       void **slot;
2846       gdb_byte *ptr = info_ptr;
2847
2848       offset = ptr - dwarf2_per_objfile->types.buffer;
2849
2850       /* We need to read the type's signature in order to build the hash
2851          table, but we don't need to read anything else just yet.  */
2852
2853       /* Sanity check to ensure entire cu is present.  */
2854       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2855       if (ptr + length + initial_length_size
2856           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2857         {
2858           complaint (&symfile_complaints,
2859                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2860           break;
2861         }
2862
2863       offset_size = initial_length_size == 4 ? 4 : 8;
2864       ptr += initial_length_size;
2865       version = bfd_get_16 (objfile->obfd, ptr);
2866       ptr += 2;
2867       ptr += offset_size; /* abbrev offset */
2868       ptr += 1; /* address size */
2869       signature = bfd_get_64 (objfile->obfd, ptr);
2870       ptr += 8;
2871       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2872
2873       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2874       memset (type_sig, 0, sizeof (*type_sig));
2875       type_sig->signature = signature;
2876       type_sig->offset = offset;
2877       type_sig->type_offset = type_offset;
2878       type_sig->per_cu.objfile = objfile;
2879       type_sig->per_cu.from_debug_types = 1;
2880
2881       slot = htab_find_slot (types_htab, type_sig, INSERT);
2882       gdb_assert (slot != NULL);
2883       *slot = type_sig;
2884
2885       if (dwarf2_die_debug)
2886         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2887                             offset, phex (signature, sizeof (signature)));
2888
2889       info_ptr = info_ptr + initial_length_size + length;
2890     }
2891
2892   dwarf2_per_objfile->signatured_types = types_htab;
2893
2894   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2895   dwarf2_per_objfile->type_comp_units
2896     = obstack_alloc (&objfile->objfile_obstack,
2897                      dwarf2_per_objfile->n_type_comp_units
2898                      * sizeof (struct dwarf2_per_cu_data *));
2899   iter = &dwarf2_per_objfile->type_comp_units[0];
2900   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2901   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2902               == dwarf2_per_objfile->n_type_comp_units);
2903
2904   return 1;
2905 }
2906
2907 /* Lookup a signature based type.
2908    Returns NULL if SIG is not present in the table.  */
2909
2910 static struct signatured_type *
2911 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2912 {
2913   struct signatured_type find_entry, *entry;
2914
2915   if (dwarf2_per_objfile->signatured_types == NULL)
2916     {
2917       complaint (&symfile_complaints,
2918                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2919       return 0;
2920     }
2921
2922   find_entry.signature = sig;
2923   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2924   return entry;
2925 }
2926
2927 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2928
2929 static void
2930 init_cu_die_reader (struct die_reader_specs *reader,
2931                     struct dwarf2_cu *cu)
2932 {
2933   reader->abfd = cu->objfile->obfd;
2934   reader->cu = cu;
2935   if (cu->per_cu->from_debug_types)
2936     {
2937       gdb_assert (dwarf2_per_objfile->types.readin);
2938       reader->buffer = dwarf2_per_objfile->types.buffer;
2939     }
2940   else
2941     {
2942       gdb_assert (dwarf2_per_objfile->info.readin);
2943       reader->buffer = dwarf2_per_objfile->info.buffer;
2944     }
2945 }
2946
2947 /* Find the base address of the compilation unit for range lists and
2948    location lists.  It will normally be specified by DW_AT_low_pc.
2949    In DWARF-3 draft 4, the base address could be overridden by
2950    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2951    compilation units with discontinuous ranges.  */
2952
2953 static void
2954 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2955 {
2956   struct attribute *attr;
2957
2958   cu->base_known = 0;
2959   cu->base_address = 0;
2960
2961   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2962   if (attr)
2963     {
2964       cu->base_address = DW_ADDR (attr);
2965       cu->base_known = 1;
2966     }
2967   else
2968     {
2969       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2970       if (attr)
2971         {
2972           cu->base_address = DW_ADDR (attr);
2973           cu->base_known = 1;
2974         }
2975     }
2976 }
2977
2978 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2979    to combine the common parts.
2980    Process a compilation unit for a psymtab.
2981    BUFFER is a pointer to the beginning of the dwarf section buffer,
2982    either .debug_info or debug_types.
2983    INFO_PTR is a pointer to the start of the CU.
2984    Returns a pointer to the next CU.  */
2985
2986 static gdb_byte *
2987 process_psymtab_comp_unit (struct objfile *objfile,
2988                            struct dwarf2_per_cu_data *this_cu,
2989                            gdb_byte *buffer, gdb_byte *info_ptr,
2990                            unsigned int buffer_size)
2991 {
2992   bfd *abfd = objfile->obfd;
2993   gdb_byte *beg_of_comp_unit = info_ptr;
2994   struct die_info *comp_unit_die;
2995   struct partial_symtab *pst;
2996   CORE_ADDR baseaddr;
2997   struct cleanup *back_to_inner;
2998   struct dwarf2_cu cu;
2999   int has_children, has_pc_info;
3000   struct attribute *attr;
3001   CORE_ADDR best_lowpc = 0, best_highpc = 0;
3002   struct die_reader_specs reader_specs;
3003
3004   memset (&cu, 0, sizeof (cu));
3005   cu.objfile = objfile;
3006   obstack_init (&cu.comp_unit_obstack);
3007
3008   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3009
3010   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3011                                           buffer, buffer_size,
3012                                           abfd);
3013
3014   /* Complete the cu_header.  */
3015   cu.header.offset = beg_of_comp_unit - buffer;
3016   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3017
3018   cu.list_in_scope = &file_symbols;
3019
3020   /* If this compilation unit was already read in, free the
3021      cached copy in order to read it in again.  This is
3022      necessary because we skipped some symbols when we first
3023      read in the compilation unit (see load_partial_dies).
3024      This problem could be avoided, but the benefit is
3025      unclear.  */
3026   if (this_cu->cu != NULL)
3027     free_one_cached_comp_unit (this_cu->cu);
3028
3029   /* Note that this is a pointer to our stack frame, being
3030      added to a global data structure.  It will be cleaned up
3031      in free_stack_comp_unit when we finish with this
3032      compilation unit.  */
3033   this_cu->cu = &cu;
3034   cu.per_cu = this_cu;
3035
3036   /* Read the abbrevs for this compilation unit into a table.  */
3037   dwarf2_read_abbrevs (abfd, &cu);
3038   make_cleanup (dwarf2_free_abbrev_table, &cu);
3039
3040   /* Read the compilation unit die.  */
3041   if (this_cu->from_debug_types)
3042     info_ptr += 8 /*signature*/ + cu.header.offset_size;
3043   init_cu_die_reader (&reader_specs, &cu);
3044   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3045                             &has_children);
3046
3047   if (this_cu->from_debug_types)
3048     {
3049       /* offset,length haven't been set yet for type units.  */
3050       this_cu->offset = cu.header.offset;
3051       this_cu->length = cu.header.length + cu.header.initial_length_size;
3052     }
3053   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3054     {
3055       info_ptr = (beg_of_comp_unit + cu.header.length
3056                   + cu.header.initial_length_size);
3057       do_cleanups (back_to_inner);
3058       return info_ptr;
3059     }
3060
3061   /* Set the language we're debugging.  */
3062   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3063   if (attr)
3064     set_cu_language (DW_UNSND (attr), &cu);
3065   else
3066     set_cu_language (language_minimal, &cu);
3067
3068   /* Allocate a new partial symbol table structure.  */
3069   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3070   pst = start_psymtab_common (objfile, objfile->section_offsets,
3071                               (attr != NULL) ? DW_STRING (attr) : "",
3072                               /* TEXTLOW and TEXTHIGH are set below.  */
3073                               0,
3074                               objfile->global_psymbols.next,
3075                               objfile->static_psymbols.next);
3076
3077   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3078   if (attr != NULL)
3079     pst->dirname = DW_STRING (attr);
3080
3081   pst->read_symtab_private = this_cu;
3082
3083   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3084
3085   /* Store the function that reads in the rest of the symbol table */
3086   pst->read_symtab = dwarf2_psymtab_to_symtab;
3087
3088   this_cu->v.psymtab = pst;
3089
3090   dwarf2_find_base_address (comp_unit_die, &cu);
3091
3092   /* Possibly set the default values of LOWPC and HIGHPC from
3093      `DW_AT_ranges'.  */
3094   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3095                                       &best_highpc, &cu, pst);
3096   if (has_pc_info == 1 && best_lowpc < best_highpc)
3097     /* Store the contiguous range if it is not empty; it can be empty for
3098        CUs with no code.  */
3099     addrmap_set_empty (objfile->psymtabs_addrmap,
3100                        best_lowpc + baseaddr,
3101                        best_highpc + baseaddr - 1, pst);
3102
3103   /* Check if comp unit has_children.
3104      If so, read the rest of the partial symbols from this comp unit.
3105      If not, there's no more debug_info for this comp unit. */
3106   if (has_children)
3107     {
3108       struct partial_die_info *first_die;
3109       CORE_ADDR lowpc, highpc;
3110
3111       lowpc = ((CORE_ADDR) -1);
3112       highpc = ((CORE_ADDR) 0);
3113
3114       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3115
3116       scan_partial_symbols (first_die, &lowpc, &highpc,
3117                             ! has_pc_info, &cu);
3118
3119       /* If we didn't find a lowpc, set it to highpc to avoid
3120          complaints from `maint check'.  */
3121       if (lowpc == ((CORE_ADDR) -1))
3122         lowpc = highpc;
3123
3124       /* If the compilation unit didn't have an explicit address range,
3125          then use the information extracted from its child dies.  */
3126       if (! has_pc_info)
3127         {
3128           best_lowpc = lowpc;
3129           best_highpc = highpc;
3130         }
3131     }
3132   pst->textlow = best_lowpc + baseaddr;
3133   pst->texthigh = best_highpc + baseaddr;
3134
3135   pst->n_global_syms = objfile->global_psymbols.next -
3136     (objfile->global_psymbols.list + pst->globals_offset);
3137   pst->n_static_syms = objfile->static_psymbols.next -
3138     (objfile->static_psymbols.list + pst->statics_offset);
3139   sort_pst_symbols (pst);
3140
3141   info_ptr = (beg_of_comp_unit + cu.header.length
3142               + cu.header.initial_length_size);
3143
3144   if (this_cu->from_debug_types)
3145     {
3146       /* It's not clear we want to do anything with stmt lists here.
3147          Waiting to see what gcc ultimately does.  */
3148     }
3149   else
3150     {
3151       /* Get the list of files included in the current compilation unit,
3152          and build a psymtab for each of them.  */
3153       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3154     }
3155
3156   do_cleanups (back_to_inner);
3157
3158   return info_ptr;
3159 }
3160
3161 /* Traversal function for htab_traverse_noresize.
3162    Process one .debug_types comp-unit.  */
3163
3164 static int
3165 process_type_comp_unit (void **slot, void *info)
3166 {
3167   struct signatured_type *entry = (struct signatured_type *) *slot;
3168   struct objfile *objfile = (struct objfile *) info;
3169   struct dwarf2_per_cu_data *this_cu;
3170
3171   this_cu = &entry->per_cu;
3172
3173   gdb_assert (dwarf2_per_objfile->types.readin);
3174   process_psymtab_comp_unit (objfile, this_cu,
3175                              dwarf2_per_objfile->types.buffer,
3176                              dwarf2_per_objfile->types.buffer + entry->offset,
3177                              dwarf2_per_objfile->types.size);
3178
3179   return 1;
3180 }
3181
3182 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3183    Build partial symbol tables for the .debug_types comp-units.  */
3184
3185 static void
3186 build_type_psymtabs (struct objfile *objfile)
3187 {
3188   if (! create_debug_types_hash_table (objfile))
3189     return;
3190
3191   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3192                           process_type_comp_unit, objfile);
3193 }
3194
3195 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3196
3197 static void
3198 psymtabs_addrmap_cleanup (void *o)
3199 {
3200   struct objfile *objfile = o;
3201
3202   objfile->psymtabs_addrmap = NULL;
3203 }
3204
3205 /* Build the partial symbol table by doing a quick pass through the
3206    .debug_info and .debug_abbrev sections.  */
3207
3208 static void
3209 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3210 {
3211   gdb_byte *info_ptr;
3212   struct cleanup *back_to, *addrmap_cleanup;
3213   struct obstack temp_obstack;
3214
3215   dwarf2_per_objfile->reading_partial_symbols = 1;
3216
3217   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3218   info_ptr = dwarf2_per_objfile->info.buffer;
3219
3220   /* Any cached compilation units will be linked by the per-objfile
3221      read_in_chain.  Make sure to free them when we're done.  */
3222   back_to = make_cleanup (free_cached_comp_units, NULL);
3223
3224   build_type_psymtabs (objfile);
3225
3226   create_all_comp_units (objfile);
3227
3228   /* Create a temporary address map on a temporary obstack.  We later
3229      copy this to the final obstack.  */
3230   obstack_init (&temp_obstack);
3231   make_cleanup_obstack_free (&temp_obstack);
3232   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3233   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3234
3235   /* Since the objects we're extracting from .debug_info vary in
3236      length, only the individual functions to extract them (like
3237      read_comp_unit_head and load_partial_die) can really know whether
3238      the buffer is large enough to hold another complete object.
3239
3240      At the moment, they don't actually check that.  If .debug_info
3241      holds just one extra byte after the last compilation unit's dies,
3242      then read_comp_unit_head will happily read off the end of the
3243      buffer.  read_partial_die is similarly casual.  Those functions
3244      should be fixed.
3245
3246      For this loop condition, simply checking whether there's any data
3247      left at all should be sufficient.  */
3248
3249   while (info_ptr < (dwarf2_per_objfile->info.buffer
3250                      + dwarf2_per_objfile->info.size))
3251     {
3252       struct dwarf2_per_cu_data *this_cu;
3253
3254       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3255                                        objfile);
3256
3257       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3258                                             dwarf2_per_objfile->info.buffer,
3259                                             info_ptr,
3260                                             dwarf2_per_objfile->info.size);
3261     }
3262
3263   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3264                                                     &objfile->objfile_obstack);
3265   discard_cleanups (addrmap_cleanup);
3266
3267   do_cleanups (back_to);
3268 }
3269
3270 /* Load the partial DIEs for a secondary CU into memory.  */
3271
3272 static void
3273 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3274                         struct objfile *objfile)
3275 {
3276   bfd *abfd = objfile->obfd;
3277   gdb_byte *info_ptr, *beg_of_comp_unit;
3278   struct die_info *comp_unit_die;
3279   struct dwarf2_cu *cu;
3280   struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3281   struct attribute *attr;
3282   int has_children;
3283   struct die_reader_specs reader_specs;
3284   int read_cu = 0;
3285
3286   gdb_assert (! this_cu->from_debug_types);
3287
3288   gdb_assert (dwarf2_per_objfile->info.readin);
3289   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3290   beg_of_comp_unit = info_ptr;
3291
3292   if (this_cu->cu == NULL)
3293     {
3294       cu = alloc_one_comp_unit (objfile);
3295
3296       read_cu = 1;
3297
3298       /* If an error occurs while loading, release our storage.  */
3299       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3300
3301       info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3302                                               dwarf2_per_objfile->info.buffer,
3303                                               dwarf2_per_objfile->info.size,
3304                                               abfd);
3305
3306       /* Complete the cu_header.  */
3307       cu->header.offset = this_cu->offset;
3308       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3309
3310       /* Link this compilation unit into the compilation unit tree.  */
3311       this_cu->cu = cu;
3312       cu->per_cu = this_cu;
3313       cu->type_hash = this_cu->type_hash;
3314
3315       /* Link this CU into read_in_chain.  */
3316       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3317       dwarf2_per_objfile->read_in_chain = this_cu;
3318     }
3319   else
3320     {
3321       cu = this_cu->cu;
3322       info_ptr += cu->header.first_die_offset;
3323     }
3324
3325   /* Read the abbrevs for this compilation unit into a table.  */
3326   gdb_assert (cu->dwarf2_abbrevs == NULL);
3327   dwarf2_read_abbrevs (abfd, cu);
3328   free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3329
3330   /* Read the compilation unit die.  */
3331   init_cu_die_reader (&reader_specs, cu);
3332   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3333                             &has_children);
3334
3335   /* Set the language we're debugging.  */
3336   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3337   if (attr)
3338     set_cu_language (DW_UNSND (attr), cu);
3339   else
3340     set_cu_language (language_minimal, cu);
3341
3342   /* Check if comp unit has_children.
3343      If so, read the rest of the partial symbols from this comp unit.
3344      If not, there's no more debug_info for this comp unit. */
3345   if (has_children)
3346     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3347
3348   do_cleanups (free_abbrevs_cleanup);
3349
3350   if (read_cu)
3351     {
3352       /* We've successfully allocated this compilation unit.  Let our
3353          caller clean it up when finished with it.  */
3354       discard_cleanups (free_cu_cleanup);
3355     }
3356 }
3357
3358 /* Create a list of all compilation units in OBJFILE.  We do this only
3359    if an inter-comp-unit reference is found; presumably if there is one,
3360    there will be many, and one will occur early in the .debug_info section.
3361    So there's no point in building this list incrementally.  */
3362
3363 static void
3364 create_all_comp_units (struct objfile *objfile)
3365 {
3366   int n_allocated;
3367   int n_comp_units;
3368   struct dwarf2_per_cu_data **all_comp_units;
3369   gdb_byte *info_ptr;
3370
3371   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3372   info_ptr = dwarf2_per_objfile->info.buffer;
3373
3374   n_comp_units = 0;
3375   n_allocated = 10;
3376   all_comp_units = xmalloc (n_allocated
3377                             * sizeof (struct dwarf2_per_cu_data *));
3378
3379   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3380     {
3381       unsigned int length, initial_length_size;
3382       struct dwarf2_per_cu_data *this_cu;
3383       unsigned int offset;
3384
3385       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3386
3387       /* Read just enough information to find out where the next
3388          compilation unit is.  */
3389       length = read_initial_length (objfile->obfd, info_ptr,
3390                                     &initial_length_size);
3391
3392       /* Save the compilation unit for later lookup.  */
3393       this_cu = obstack_alloc (&objfile->objfile_obstack,
3394                                sizeof (struct dwarf2_per_cu_data));
3395       memset (this_cu, 0, sizeof (*this_cu));
3396       this_cu->offset = offset;
3397       this_cu->length = length + initial_length_size;
3398       this_cu->objfile = objfile;
3399
3400       if (n_comp_units == n_allocated)
3401         {
3402           n_allocated *= 2;
3403           all_comp_units = xrealloc (all_comp_units,
3404                                      n_allocated
3405                                      * sizeof (struct dwarf2_per_cu_data *));
3406         }
3407       all_comp_units[n_comp_units++] = this_cu;
3408
3409       info_ptr = info_ptr + this_cu->length;
3410     }
3411
3412   dwarf2_per_objfile->all_comp_units
3413     = obstack_alloc (&objfile->objfile_obstack,
3414                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3415   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3416           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3417   xfree (all_comp_units);
3418   dwarf2_per_objfile->n_comp_units = n_comp_units;
3419 }
3420
3421 /* Process all loaded DIEs for compilation unit CU, starting at
3422    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3423    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3424    DW_AT_ranges).  If NEED_PC is set, then this function will set
3425    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3426    and record the covered ranges in the addrmap.  */
3427
3428 static void
3429 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3430                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3431 {
3432   struct partial_die_info *pdi;
3433
3434   /* Now, march along the PDI's, descending into ones which have
3435      interesting children but skipping the children of the other ones,
3436      until we reach the end of the compilation unit.  */
3437
3438   pdi = first_die;
3439
3440   while (pdi != NULL)
3441     {
3442       fixup_partial_die (pdi, cu);
3443
3444       /* Anonymous namespaces or modules have no name but have interesting
3445          children, so we need to look at them.  Ditto for anonymous
3446          enums.  */
3447
3448       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3449           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3450         {
3451           switch (pdi->tag)
3452             {
3453             case DW_TAG_subprogram:
3454               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3455               break;
3456             case DW_TAG_variable:
3457             case DW_TAG_typedef:
3458             case DW_TAG_union_type:
3459               if (!pdi->is_declaration)
3460                 {
3461                   add_partial_symbol (pdi, cu);
3462                 }
3463               break;
3464             case DW_TAG_class_type:
3465             case DW_TAG_interface_type:
3466             case DW_TAG_structure_type:
3467               if (!pdi->is_declaration)
3468                 {
3469                   add_partial_symbol (pdi, cu);
3470                 }
3471               break;
3472             case DW_TAG_enumeration_type:
3473               if (!pdi->is_declaration)
3474                 add_partial_enumeration (pdi, cu);
3475               break;
3476             case DW_TAG_base_type:
3477             case DW_TAG_subrange_type:
3478               /* File scope base type definitions are added to the partial
3479                  symbol table.  */
3480               add_partial_symbol (pdi, cu);
3481               break;
3482             case DW_TAG_namespace:
3483               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3484               break;
3485             case DW_TAG_module:
3486               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3487               break;
3488             default:
3489               break;
3490             }
3491         }
3492
3493       /* If the die has a sibling, skip to the sibling.  */
3494
3495       pdi = pdi->die_sibling;
3496     }
3497 }
3498
3499 /* Functions used to compute the fully scoped name of a partial DIE.
3500
3501    Normally, this is simple.  For C++, the parent DIE's fully scoped
3502    name is concatenated with "::" and the partial DIE's name.  For
3503    Java, the same thing occurs except that "." is used instead of "::".
3504    Enumerators are an exception; they use the scope of their parent
3505    enumeration type, i.e. the name of the enumeration type is not
3506    prepended to the enumerator.
3507
3508    There are two complexities.  One is DW_AT_specification; in this
3509    case "parent" means the parent of the target of the specification,
3510    instead of the direct parent of the DIE.  The other is compilers
3511    which do not emit DW_TAG_namespace; in this case we try to guess
3512    the fully qualified name of structure types from their members'
3513    linkage names.  This must be done using the DIE's children rather
3514    than the children of any DW_AT_specification target.  We only need
3515    to do this for structures at the top level, i.e. if the target of
3516    any DW_AT_specification (if any; otherwise the DIE itself) does not
3517    have a parent.  */
3518
3519 /* Compute the scope prefix associated with PDI's parent, in
3520    compilation unit CU.  The result will be allocated on CU's
3521    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3522    field.  NULL is returned if no prefix is necessary.  */
3523 static char *
3524 partial_die_parent_scope (struct partial_die_info *pdi,
3525                           struct dwarf2_cu *cu)
3526 {
3527   char *grandparent_scope;
3528   struct partial_die_info *parent, *real_pdi;
3529
3530   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3531      then this means the parent of the specification DIE.  */
3532
3533   real_pdi = pdi;
3534   while (real_pdi->has_specification)
3535     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3536
3537   parent = real_pdi->die_parent;
3538   if (parent == NULL)
3539     return NULL;
3540
3541   if (parent->scope_set)
3542     return parent->scope;
3543
3544   fixup_partial_die (parent, cu);
3545
3546   grandparent_scope = partial_die_parent_scope (parent, cu);
3547
3548   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3549      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3550      Work around this problem here.  */
3551   if (cu->language == language_cplus
3552       && parent->tag == DW_TAG_namespace
3553       && strcmp (parent->name, "::") == 0
3554       && grandparent_scope == NULL)
3555     {
3556       parent->scope = NULL;
3557       parent->scope_set = 1;
3558       return NULL;
3559     }
3560
3561   if (parent->tag == DW_TAG_namespace
3562       || parent->tag == DW_TAG_module
3563       || parent->tag == DW_TAG_structure_type
3564       || parent->tag == DW_TAG_class_type
3565       || parent->tag == DW_TAG_interface_type
3566       || parent->tag == DW_TAG_union_type
3567       || parent->tag == DW_TAG_enumeration_type)
3568     {
3569       if (grandparent_scope == NULL)
3570         parent->scope = parent->name;
3571       else
3572         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3573                                          parent->name, 0, cu);
3574     }
3575   else if (parent->tag == DW_TAG_enumerator)
3576     /* Enumerators should not get the name of the enumeration as a prefix.  */
3577     parent->scope = grandparent_scope;
3578   else
3579     {
3580       /* FIXME drow/2004-04-01: What should we be doing with
3581          function-local names?  For partial symbols, we should probably be
3582          ignoring them.  */
3583       complaint (&symfile_complaints,
3584                  _("unhandled containing DIE tag %d for DIE at %d"),
3585                  parent->tag, pdi->offset);
3586       parent->scope = grandparent_scope;
3587     }
3588
3589   parent->scope_set = 1;
3590   return parent->scope;
3591 }
3592
3593 /* Return the fully scoped name associated with PDI, from compilation unit
3594    CU.  The result will be allocated with malloc.  */
3595 static char *
3596 partial_die_full_name (struct partial_die_info *pdi,
3597                        struct dwarf2_cu *cu)
3598 {
3599   char *parent_scope;
3600
3601   /* If this is a template instantiation, we can not work out the
3602      template arguments from partial DIEs.  So, unfortunately, we have
3603      to go through the full DIEs.  At least any work we do building
3604      types here will be reused if full symbols are loaded later.  */
3605   if (pdi->has_template_arguments)
3606     {
3607       fixup_partial_die (pdi, cu);
3608
3609       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3610         {
3611           struct die_info *die;
3612           struct attribute attr;
3613           struct dwarf2_cu *ref_cu = cu;
3614
3615           attr.name = 0;
3616           attr.form = DW_FORM_ref_addr;
3617           attr.u.addr = pdi->offset;
3618           die = follow_die_ref (NULL, &attr, &ref_cu);
3619
3620           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3621         }
3622     }
3623
3624   parent_scope = partial_die_parent_scope (pdi, cu);
3625   if (parent_scope == NULL)
3626     return NULL;
3627   else
3628     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3629 }
3630
3631 static void
3632 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3633 {
3634   struct objfile *objfile = cu->objfile;
3635   CORE_ADDR addr = 0;
3636   char *actual_name = NULL;
3637   const struct partial_symbol *psym = NULL;
3638   CORE_ADDR baseaddr;
3639   int built_actual_name = 0;
3640
3641   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3642
3643   actual_name = partial_die_full_name (pdi, cu);
3644   if (actual_name)
3645     built_actual_name = 1;
3646
3647   if (actual_name == NULL)
3648     actual_name = pdi->name;
3649
3650   switch (pdi->tag)
3651     {
3652     case DW_TAG_subprogram:
3653       if (pdi->is_external || cu->language == language_ada)
3654         {
3655           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3656              of the global scope.  But in Ada, we want to be able to access
3657              nested procedures globally.  So all Ada subprograms are stored
3658              in the global scope.  */
3659           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3660              mst_text, objfile); */
3661           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3662                                       built_actual_name,
3663                                       VAR_DOMAIN, LOC_BLOCK,
3664                                       &objfile->global_psymbols,
3665                                       0, pdi->lowpc + baseaddr,
3666                                       cu->language, objfile);
3667         }
3668       else
3669         {
3670           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3671              mst_file_text, objfile); */
3672           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3673                                       built_actual_name,
3674                                       VAR_DOMAIN, LOC_BLOCK,
3675                                       &objfile->static_psymbols,
3676                                       0, pdi->lowpc + baseaddr,
3677                                       cu->language, objfile);
3678         }
3679       break;
3680     case DW_TAG_variable:
3681       if (pdi->locdesc)
3682         addr = decode_locdesc (pdi->locdesc, cu);
3683
3684       if (pdi->locdesc
3685           && addr == 0
3686           && !dwarf2_per_objfile->has_section_at_zero)
3687         {
3688           /* A global or static variable may also have been stripped
3689              out by the linker if unused, in which case its address
3690              will be nullified; do not add such variables into partial
3691              symbol table then.  */
3692         }
3693       else if (pdi->is_external)
3694         {
3695           /* Global Variable.
3696              Don't enter into the minimal symbol tables as there is
3697              a minimal symbol table entry from the ELF symbols already.
3698              Enter into partial symbol table if it has a location
3699              descriptor or a type.
3700              If the location descriptor is missing, new_symbol will create
3701              a LOC_UNRESOLVED symbol, the address of the variable will then
3702              be determined from the minimal symbol table whenever the variable
3703              is referenced.
3704              The address for the partial symbol table entry is not
3705              used by GDB, but it comes in handy for debugging partial symbol
3706              table building.  */
3707
3708           if (pdi->locdesc || pdi->has_type)
3709             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3710                                         built_actual_name,
3711                                         VAR_DOMAIN, LOC_STATIC,
3712                                         &objfile->global_psymbols,
3713                                         0, addr + baseaddr,
3714                                         cu->language, objfile);
3715         }
3716       else
3717         {
3718           /* Static Variable. Skip symbols without location descriptors.  */
3719           if (pdi->locdesc == NULL)
3720             {
3721               if (built_actual_name)
3722                 xfree (actual_name);
3723               return;
3724             }
3725           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3726              mst_file_data, objfile); */
3727           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3728                                       built_actual_name,
3729                                       VAR_DOMAIN, LOC_STATIC,
3730                                       &objfile->static_psymbols,
3731                                       0, addr + baseaddr,
3732                                       cu->language, objfile);
3733         }
3734       break;
3735     case DW_TAG_typedef:
3736     case DW_TAG_base_type:
3737     case DW_TAG_subrange_type:
3738       add_psymbol_to_list (actual_name, strlen (actual_name),
3739                            built_actual_name,
3740                            VAR_DOMAIN, LOC_TYPEDEF,
3741                            &objfile->static_psymbols,
3742                            0, (CORE_ADDR) 0, cu->language, objfile);
3743       break;
3744     case DW_TAG_namespace:
3745       add_psymbol_to_list (actual_name, strlen (actual_name),
3746                            built_actual_name,
3747                            VAR_DOMAIN, LOC_TYPEDEF,
3748                            &objfile->global_psymbols,
3749                            0, (CORE_ADDR) 0, cu->language, objfile);
3750       break;
3751     case DW_TAG_class_type:
3752     case DW_TAG_interface_type:
3753     case DW_TAG_structure_type:
3754     case DW_TAG_union_type:
3755     case DW_TAG_enumeration_type:
3756       /* Skip external references.  The DWARF standard says in the section
3757          about "Structure, Union, and Class Type Entries": "An incomplete
3758          structure, union or class type is represented by a structure,
3759          union or class entry that does not have a byte size attribute
3760          and that has a DW_AT_declaration attribute."  */
3761       if (!pdi->has_byte_size && pdi->is_declaration)
3762         {
3763           if (built_actual_name)
3764             xfree (actual_name);
3765           return;
3766         }
3767
3768       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3769          static vs. global.  */
3770       add_psymbol_to_list (actual_name, strlen (actual_name),
3771                            built_actual_name,
3772                            STRUCT_DOMAIN, LOC_TYPEDEF,
3773                            (cu->language == language_cplus
3774                             || cu->language == language_java)
3775                            ? &objfile->global_psymbols
3776                            : &objfile->static_psymbols,
3777                            0, (CORE_ADDR) 0, cu->language, objfile);
3778
3779       break;
3780     case DW_TAG_enumerator:
3781       add_psymbol_to_list (actual_name, strlen (actual_name),
3782                            built_actual_name,
3783                            VAR_DOMAIN, LOC_CONST,
3784                            (cu->language == language_cplus
3785                             || cu->language == language_java)
3786                            ? &objfile->global_psymbols
3787                            : &objfile->static_psymbols,
3788                            0, (CORE_ADDR) 0, cu->language, objfile);
3789       break;
3790     default:
3791       break;
3792     }
3793
3794   if (built_actual_name)
3795     xfree (actual_name);
3796 }
3797
3798 /* Read a partial die corresponding to a namespace; also, add a symbol
3799    corresponding to that namespace to the symbol table.  NAMESPACE is
3800    the name of the enclosing namespace.  */
3801
3802 static void
3803 add_partial_namespace (struct partial_die_info *pdi,
3804                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3805                        int need_pc, struct dwarf2_cu *cu)
3806 {
3807   /* Add a symbol for the namespace.  */
3808
3809   add_partial_symbol (pdi, cu);
3810
3811   /* Now scan partial symbols in that namespace.  */
3812
3813   if (pdi->has_children)
3814     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3815 }
3816
3817 /* Read a partial die corresponding to a Fortran module.  */
3818
3819 static void
3820 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3821                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3822 {
3823   /* Now scan partial symbols in that module.  */
3824
3825   if (pdi->has_children)
3826     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3827 }
3828
3829 /* Read a partial die corresponding to a subprogram and create a partial
3830    symbol for that subprogram.  When the CU language allows it, this
3831    routine also defines a partial symbol for each nested subprogram
3832    that this subprogram contains.
3833
3834    DIE my also be a lexical block, in which case we simply search
3835    recursively for suprograms defined inside that lexical block.
3836    Again, this is only performed when the CU language allows this
3837    type of definitions.  */
3838
3839 static void
3840 add_partial_subprogram (struct partial_die_info *pdi,
3841                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3842                         int need_pc, struct dwarf2_cu *cu)
3843 {
3844   if (pdi->tag == DW_TAG_subprogram)
3845     {
3846       if (pdi->has_pc_info)
3847         {
3848           if (pdi->lowpc < *lowpc)
3849             *lowpc = pdi->lowpc;
3850           if (pdi->highpc > *highpc)
3851             *highpc = pdi->highpc;
3852           if (need_pc)
3853             {
3854               CORE_ADDR baseaddr;
3855               struct objfile *objfile = cu->objfile;
3856
3857               baseaddr = ANOFFSET (objfile->section_offsets,
3858                                    SECT_OFF_TEXT (objfile));
3859               addrmap_set_empty (objfile->psymtabs_addrmap,
3860                                  pdi->lowpc + baseaddr,
3861                                  pdi->highpc - 1 + baseaddr,
3862                                  cu->per_cu->v.psymtab);
3863             }
3864           if (!pdi->is_declaration)
3865             /* Ignore subprogram DIEs that do not have a name, they are
3866                illegal.  Do not emit a complaint at this point, we will
3867                do so when we convert this psymtab into a symtab.  */
3868             if (pdi->name)
3869               add_partial_symbol (pdi, cu);
3870         }
3871     }
3872
3873   if (! pdi->has_children)
3874     return;
3875
3876   if (cu->language == language_ada)
3877     {
3878       pdi = pdi->die_child;
3879       while (pdi != NULL)
3880         {
3881           fixup_partial_die (pdi, cu);
3882           if (pdi->tag == DW_TAG_subprogram
3883               || pdi->tag == DW_TAG_lexical_block)
3884             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3885           pdi = pdi->die_sibling;
3886         }
3887     }
3888 }
3889
3890 /* See if we can figure out if the class lives in a namespace.  We do
3891    this by looking for a member function; its demangled name will
3892    contain namespace info, if there is any.  */
3893
3894 static void
3895 guess_structure_name (struct partial_die_info *struct_pdi,
3896                       struct dwarf2_cu *cu)
3897 {
3898   if ((cu->language == language_cplus
3899        || cu->language == language_java)
3900       && cu->has_namespace_info == 0
3901       && struct_pdi->has_children)
3902     {
3903       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3904          what template types look like, because the demangler
3905          frequently doesn't give the same name as the debug info.  We
3906          could fix this by only using the demangled name to get the
3907          prefix (but see comment in read_structure_type).  */
3908
3909       struct partial_die_info *real_pdi;
3910
3911       /* If this DIE (this DIE's specification, if any) has a parent, then
3912          we should not do this.  We'll prepend the parent's fully qualified
3913          name when we create the partial symbol.  */
3914
3915       real_pdi = struct_pdi;
3916       while (real_pdi->has_specification)
3917         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3918
3919       if (real_pdi->die_parent != NULL)
3920         return;
3921     }
3922 }
3923
3924 /* Read a partial die corresponding to an enumeration type.  */
3925
3926 static void
3927 add_partial_enumeration (struct partial_die_info *enum_pdi,
3928                          struct dwarf2_cu *cu)
3929 {
3930   struct partial_die_info *pdi;
3931
3932   if (enum_pdi->name != NULL)
3933     add_partial_symbol (enum_pdi, cu);
3934
3935   pdi = enum_pdi->die_child;
3936   while (pdi)
3937     {
3938       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3939         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3940       else
3941         add_partial_symbol (pdi, cu);
3942       pdi = pdi->die_sibling;
3943     }
3944 }
3945
3946 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3947    Return the corresponding abbrev, or NULL if the number is zero (indicating
3948    an empty DIE).  In either case *BYTES_READ will be set to the length of
3949    the initial number.  */
3950
3951 static struct abbrev_info *
3952 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3953                  struct dwarf2_cu *cu)
3954 {
3955   bfd *abfd = cu->objfile->obfd;
3956   unsigned int abbrev_number;
3957   struct abbrev_info *abbrev;
3958
3959   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3960
3961   if (abbrev_number == 0)
3962     return NULL;
3963
3964   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3965   if (!abbrev)
3966     {
3967       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3968                       bfd_get_filename (abfd));
3969     }
3970
3971   return abbrev;
3972 }
3973
3974 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3975    Returns a pointer to the end of a series of DIEs, terminated by an empty
3976    DIE.  Any children of the skipped DIEs will also be skipped.  */
3977
3978 static gdb_byte *
3979 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3980 {
3981   struct abbrev_info *abbrev;
3982   unsigned int bytes_read;
3983
3984   while (1)
3985     {
3986       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3987       if (abbrev == NULL)
3988         return info_ptr + bytes_read;
3989       else
3990         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3991     }
3992 }
3993
3994 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3995    INFO_PTR should point just after the initial uleb128 of a DIE, and the
3996    abbrev corresponding to that skipped uleb128 should be passed in
3997    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
3998    children.  */
3999
4000 static gdb_byte *
4001 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4002               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4003 {
4004   unsigned int bytes_read;
4005   struct attribute attr;
4006   bfd *abfd = cu->objfile->obfd;
4007   unsigned int form, i;
4008
4009   for (i = 0; i < abbrev->num_attrs; i++)
4010     {
4011       /* The only abbrev we care about is DW_AT_sibling.  */
4012       if (abbrev->attrs[i].name == DW_AT_sibling)
4013         {
4014           read_attribute (&attr, &abbrev->attrs[i],
4015                           abfd, info_ptr, cu);
4016           if (attr.form == DW_FORM_ref_addr)
4017             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4018           else
4019             return buffer + dwarf2_get_ref_die_offset (&attr);
4020         }
4021
4022       /* If it isn't DW_AT_sibling, skip this attribute.  */
4023       form = abbrev->attrs[i].form;
4024     skip_attribute:
4025       switch (form)
4026         {
4027         case DW_FORM_ref_addr:
4028           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4029              and later it is offset sized.  */
4030           if (cu->header.version == 2)
4031             info_ptr += cu->header.addr_size;
4032           else
4033             info_ptr += cu->header.offset_size;
4034           break;
4035         case DW_FORM_addr:
4036           info_ptr += cu->header.addr_size;
4037           break;
4038         case DW_FORM_data1:
4039         case DW_FORM_ref1:
4040         case DW_FORM_flag:
4041           info_ptr += 1;
4042           break;
4043         case DW_FORM_flag_present:
4044           break;
4045         case DW_FORM_data2:
4046         case DW_FORM_ref2:
4047           info_ptr += 2;
4048           break;
4049         case DW_FORM_data4:
4050         case DW_FORM_ref4:
4051           info_ptr += 4;
4052           break;
4053         case DW_FORM_data8:
4054         case DW_FORM_ref8:
4055         case DW_FORM_sig8:
4056           info_ptr += 8;
4057           break;
4058         case DW_FORM_string:
4059           read_direct_string (abfd, info_ptr, &bytes_read);
4060           info_ptr += bytes_read;
4061           break;
4062         case DW_FORM_sec_offset:
4063         case DW_FORM_strp:
4064           info_ptr += cu->header.offset_size;
4065           break;
4066         case DW_FORM_exprloc:
4067         case DW_FORM_block:
4068           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4069           info_ptr += bytes_read;
4070           break;
4071         case DW_FORM_block1:
4072           info_ptr += 1 + read_1_byte (abfd, info_ptr);
4073           break;
4074         case DW_FORM_block2:
4075           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4076           break;
4077         case DW_FORM_block4:
4078           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4079           break;
4080         case DW_FORM_sdata:
4081         case DW_FORM_udata:
4082         case DW_FORM_ref_udata:
4083           info_ptr = skip_leb128 (abfd, info_ptr);
4084           break;
4085         case DW_FORM_indirect:
4086           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4087           info_ptr += bytes_read;
4088           /* We need to continue parsing from here, so just go back to
4089              the top.  */
4090           goto skip_attribute;
4091
4092         default:
4093           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4094                  dwarf_form_name (form),
4095                  bfd_get_filename (abfd));
4096         }
4097     }
4098
4099   if (abbrev->has_children)
4100     return skip_children (buffer, info_ptr, cu);
4101   else
4102     return info_ptr;
4103 }
4104
4105 /* Locate ORIG_PDI's sibling.
4106    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4107    in BUFFER.  */
4108
4109 static gdb_byte *
4110 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4111                     gdb_byte *buffer, gdb_byte *info_ptr,
4112                     bfd *abfd, struct dwarf2_cu *cu)
4113 {
4114   /* Do we know the sibling already?  */
4115
4116   if (orig_pdi->sibling)
4117     return orig_pdi->sibling;
4118
4119   /* Are there any children to deal with?  */
4120
4121   if (!orig_pdi->has_children)
4122     return info_ptr;
4123
4124   /* Skip the children the long way.  */
4125
4126   return skip_children (buffer, info_ptr, cu);
4127 }
4128
4129 /* Expand this partial symbol table into a full symbol table.  */
4130
4131 static void
4132 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4133 {
4134   if (pst != NULL)
4135     {
4136       if (pst->readin)
4137         {
4138           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4139         }
4140       else
4141         {
4142           if (info_verbose)
4143             {
4144               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4145               gdb_flush (gdb_stdout);
4146             }
4147
4148           /* Restore our global data.  */
4149           dwarf2_per_objfile = objfile_data (pst->objfile,
4150                                              dwarf2_objfile_data_key);
4151
4152           /* If this psymtab is constructed from a debug-only objfile, the
4153              has_section_at_zero flag will not necessarily be correct.  We
4154              can get the correct value for this flag by looking at the data
4155              associated with the (presumably stripped) associated objfile.  */
4156           if (pst->objfile->separate_debug_objfile_backlink)
4157             {
4158               struct dwarf2_per_objfile *dpo_backlink
4159                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4160                                 dwarf2_objfile_data_key);
4161
4162               dwarf2_per_objfile->has_section_at_zero
4163                 = dpo_backlink->has_section_at_zero;
4164             }
4165
4166           dwarf2_per_objfile->reading_partial_symbols = 0;
4167
4168           psymtab_to_symtab_1 (pst);
4169
4170           /* Finish up the debug error message.  */
4171           if (info_verbose)
4172             printf_filtered (_("done.\n"));
4173         }
4174     }
4175 }
4176
4177 /* Add PER_CU to the queue.  */
4178
4179 static void
4180 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4181 {
4182   struct dwarf2_queue_item *item;
4183
4184   per_cu->queued = 1;
4185   item = xmalloc (sizeof (*item));
4186   item->per_cu = per_cu;
4187   item->next = NULL;
4188
4189   if (dwarf2_queue == NULL)
4190     dwarf2_queue = item;
4191   else
4192     dwarf2_queue_tail->next = item;
4193
4194   dwarf2_queue_tail = item;
4195 }
4196
4197 /* Process the queue.  */
4198
4199 static void
4200 process_queue (struct objfile *objfile)
4201 {
4202   struct dwarf2_queue_item *item, *next_item;
4203
4204   /* The queue starts out with one item, but following a DIE reference
4205      may load a new CU, adding it to the end of the queue.  */
4206   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4207     {
4208       if (dwarf2_per_objfile->using_index
4209           ? !item->per_cu->v.quick->symtab
4210           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4211         process_full_comp_unit (item->per_cu);
4212
4213       item->per_cu->queued = 0;
4214       next_item = item->next;
4215       xfree (item);
4216     }
4217
4218   dwarf2_queue_tail = NULL;
4219 }
4220
4221 /* Free all allocated queue entries.  This function only releases anything if
4222    an error was thrown; if the queue was processed then it would have been
4223    freed as we went along.  */
4224
4225 static void
4226 dwarf2_release_queue (void *dummy)
4227 {
4228   struct dwarf2_queue_item *item, *last;
4229
4230   item = dwarf2_queue;
4231   while (item)
4232     {
4233       /* Anything still marked queued is likely to be in an
4234          inconsistent state, so discard it.  */
4235       if (item->per_cu->queued)
4236         {
4237           if (item->per_cu->cu != NULL)
4238             free_one_cached_comp_unit (item->per_cu->cu);
4239           item->per_cu->queued = 0;
4240         }
4241
4242       last = item;
4243       item = item->next;
4244       xfree (last);
4245     }
4246
4247   dwarf2_queue = dwarf2_queue_tail = NULL;
4248 }
4249
4250 /* Read in full symbols for PST, and anything it depends on.  */
4251
4252 static void
4253 psymtab_to_symtab_1 (struct partial_symtab *pst)
4254 {
4255   struct dwarf2_per_cu_data *per_cu;
4256   struct cleanup *back_to;
4257   int i;
4258
4259   for (i = 0; i < pst->number_of_dependencies; i++)
4260     if (!pst->dependencies[i]->readin)
4261       {
4262         /* Inform about additional files that need to be read in.  */
4263         if (info_verbose)
4264           {
4265             /* FIXME: i18n: Need to make this a single string.  */
4266             fputs_filtered (" ", gdb_stdout);
4267             wrap_here ("");
4268             fputs_filtered ("and ", gdb_stdout);
4269             wrap_here ("");
4270             printf_filtered ("%s...", pst->dependencies[i]->filename);
4271             wrap_here ("");     /* Flush output */
4272             gdb_flush (gdb_stdout);
4273           }
4274         psymtab_to_symtab_1 (pst->dependencies[i]);
4275       }
4276
4277   per_cu = pst->read_symtab_private;
4278
4279   if (per_cu == NULL)
4280     {
4281       /* It's an include file, no symbols to read for it.
4282          Everything is in the parent symtab.  */
4283       pst->readin = 1;
4284       return;
4285     }
4286
4287   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4288 }
4289
4290 /* Load the DIEs associated with PER_CU into memory.  */
4291
4292 static void
4293 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4294 {
4295   bfd *abfd = objfile->obfd;
4296   struct dwarf2_cu *cu;
4297   unsigned int offset;
4298   gdb_byte *info_ptr, *beg_of_comp_unit;
4299   struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4300   struct attribute *attr;
4301   int read_cu = 0;
4302
4303   gdb_assert (! per_cu->from_debug_types);
4304
4305   /* Set local variables from the partial symbol table info.  */
4306   offset = per_cu->offset;
4307
4308   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4309   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4310   beg_of_comp_unit = info_ptr;
4311
4312   if (per_cu->cu == NULL)
4313     {
4314       cu = alloc_one_comp_unit (objfile);
4315
4316       read_cu = 1;
4317
4318       /* If an error occurs while loading, release our storage.  */
4319       free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4320
4321       /* Read in the comp_unit header.  */
4322       info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4323
4324       /* Complete the cu_header.  */
4325       cu->header.offset = offset;
4326       cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4327
4328       /* Read the abbrevs for this compilation unit.  */
4329       dwarf2_read_abbrevs (abfd, cu);
4330       free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4331
4332       /* Link this compilation unit into the compilation unit tree.  */
4333       per_cu->cu = cu;
4334       cu->per_cu = per_cu;
4335       cu->type_hash = per_cu->type_hash;
4336
4337       /* Link this CU into read_in_chain.  */
4338       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4339       dwarf2_per_objfile->read_in_chain = per_cu;
4340     }
4341   else
4342     {
4343       cu = per_cu->cu;
4344       info_ptr += cu->header.first_die_offset;
4345     }
4346
4347   cu->dies = read_comp_unit (info_ptr, cu);
4348
4349   /* We try not to read any attributes in this function, because not
4350      all objfiles needed for references have been loaded yet, and symbol
4351      table processing isn't initialized.  But we have to set the CU language,
4352      or we won't be able to build types correctly.  */
4353   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4354   if (attr)
4355     set_cu_language (DW_UNSND (attr), cu);
4356   else
4357     set_cu_language (language_minimal, cu);
4358
4359   /* Similarly, if we do not read the producer, we can not apply
4360      producer-specific interpretation.  */
4361   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4362   if (attr)
4363     cu->producer = DW_STRING (attr);
4364
4365   if (read_cu)
4366     {
4367       do_cleanups (free_abbrevs_cleanup);
4368
4369       /* We've successfully allocated this compilation unit.  Let our
4370          caller clean it up when finished with it.  */
4371       discard_cleanups (free_cu_cleanup);
4372     }
4373 }
4374
4375 /* Add a DIE to the delayed physname list.  */
4376
4377 static void
4378 add_to_method_list (struct type *type, int fnfield_index, int index,
4379                     const char *name, struct die_info *die,
4380                     struct dwarf2_cu *cu)
4381 {
4382   struct delayed_method_info mi;
4383   mi.type = type;
4384   mi.fnfield_index = fnfield_index;
4385   mi.index = index;
4386   mi.name = name;
4387   mi.die = die;
4388   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4389 }
4390
4391 /* A cleanup for freeing the delayed method list.  */
4392
4393 static void
4394 free_delayed_list (void *ptr)
4395 {
4396   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4397   if (cu->method_list != NULL)
4398     {
4399       VEC_free (delayed_method_info, cu->method_list);
4400       cu->method_list = NULL;
4401     }
4402 }
4403
4404 /* Compute the physnames of any methods on the CU's method list.
4405
4406    The computation of method physnames is delayed in order to avoid the
4407    (bad) condition that one of the method's formal parameters is of an as yet
4408    incomplete type.  */
4409
4410 static void
4411 compute_delayed_physnames (struct dwarf2_cu *cu)
4412 {
4413   int i;
4414   struct delayed_method_info *mi;
4415   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4416     {
4417       char *physname;
4418       struct fn_fieldlist *fn_flp
4419         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4420       physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4421       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4422     }
4423 }
4424
4425 /* Generate full symbol information for PST and CU, whose DIEs have
4426    already been loaded into memory.  */
4427
4428 static void
4429 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4430 {
4431   struct dwarf2_cu *cu = per_cu->cu;
4432   struct objfile *objfile = per_cu->objfile;
4433   CORE_ADDR lowpc, highpc;
4434   struct symtab *symtab;
4435   struct cleanup *back_to, *delayed_list_cleanup;
4436   CORE_ADDR baseaddr;
4437
4438   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4439
4440   buildsym_init ();
4441   back_to = make_cleanup (really_free_pendings, NULL);
4442   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4443
4444   cu->list_in_scope = &file_symbols;
4445
4446   dwarf2_find_base_address (cu->dies, cu);
4447
4448   /* Do line number decoding in read_file_scope () */
4449   process_die (cu->dies, cu);
4450
4451   /* Now that we have processed all the DIEs in the CU, all the types 
4452      should be complete, and it should now be safe to compute all of the
4453      physnames.  */
4454   compute_delayed_physnames (cu);
4455   do_cleanups (delayed_list_cleanup);
4456
4457   /* Some compilers don't define a DW_AT_high_pc attribute for the
4458      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4459      it, by scanning the DIE's below the compilation unit.  */
4460   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4461
4462   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4463
4464   /* Set symtab language to language from DW_AT_language.
4465      If the compilation is from a C file generated by language preprocessors,
4466      do not set the language if it was already deduced by start_subfile.  */
4467   if (symtab != NULL
4468       && !(cu->language == language_c && symtab->language != language_c))
4469     {
4470       symtab->language = cu->language;
4471     }
4472
4473   if (dwarf2_per_objfile->using_index)
4474     per_cu->v.quick->symtab = symtab;
4475   else
4476     {
4477       struct partial_symtab *pst = per_cu->v.psymtab;
4478       pst->symtab = symtab;
4479       pst->readin = 1;
4480     }
4481
4482   do_cleanups (back_to);
4483 }
4484
4485 /* Process a die and its children.  */
4486
4487 static void
4488 process_die (struct die_info *die, struct dwarf2_cu *cu)
4489 {
4490   switch (die->tag)
4491     {
4492     case DW_TAG_padding:
4493       break;
4494     case DW_TAG_compile_unit:
4495       read_file_scope (die, cu);
4496       break;
4497     case DW_TAG_type_unit:
4498       read_type_unit_scope (die, cu);
4499       break;
4500     case DW_TAG_subprogram:
4501     case DW_TAG_inlined_subroutine:
4502       read_func_scope (die, cu);
4503       break;
4504     case DW_TAG_lexical_block:
4505     case DW_TAG_try_block:
4506     case DW_TAG_catch_block:
4507       read_lexical_block_scope (die, cu);
4508       break;
4509     case DW_TAG_class_type:
4510     case DW_TAG_interface_type:
4511     case DW_TAG_structure_type:
4512     case DW_TAG_union_type:
4513       process_structure_scope (die, cu);
4514       break;
4515     case DW_TAG_enumeration_type:
4516       process_enumeration_scope (die, cu);
4517       break;
4518
4519     /* These dies have a type, but processing them does not create
4520        a symbol or recurse to process the children.  Therefore we can
4521        read them on-demand through read_type_die.  */
4522     case DW_TAG_subroutine_type:
4523     case DW_TAG_set_type:
4524     case DW_TAG_array_type:
4525     case DW_TAG_pointer_type:
4526     case DW_TAG_ptr_to_member_type:
4527     case DW_TAG_reference_type:
4528     case DW_TAG_string_type:
4529       break;
4530
4531     case DW_TAG_base_type:
4532     case DW_TAG_subrange_type:
4533     case DW_TAG_typedef:
4534       /* Add a typedef symbol for the type definition, if it has a
4535          DW_AT_name.  */
4536       new_symbol (die, read_type_die (die, cu), cu);
4537       break;
4538     case DW_TAG_common_block:
4539       read_common_block (die, cu);
4540       break;
4541     case DW_TAG_common_inclusion:
4542       break;
4543     case DW_TAG_namespace:
4544       processing_has_namespace_info = 1;
4545       read_namespace (die, cu);
4546       break;
4547     case DW_TAG_module:
4548       processing_has_namespace_info = 1;
4549       read_module (die, cu);
4550       break;
4551     case DW_TAG_imported_declaration:
4552     case DW_TAG_imported_module:
4553       processing_has_namespace_info = 1;
4554       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4555                                  || cu->language != language_fortran))
4556         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4557                    dwarf_tag_name (die->tag));
4558       read_import_statement (die, cu);
4559       break;
4560     default:
4561       new_symbol (die, NULL, cu);
4562       break;
4563     }
4564 }
4565
4566 /* A helper function for dwarf2_compute_name which determines whether DIE
4567    needs to have the name of the scope prepended to the name listed in the
4568    die.  */
4569
4570 static int
4571 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4572 {
4573   struct attribute *attr;
4574
4575   switch (die->tag)
4576     {
4577     case DW_TAG_namespace:
4578     case DW_TAG_typedef:
4579     case DW_TAG_class_type:
4580     case DW_TAG_interface_type:
4581     case DW_TAG_structure_type:
4582     case DW_TAG_union_type:
4583     case DW_TAG_enumeration_type:
4584     case DW_TAG_enumerator:
4585     case DW_TAG_subprogram:
4586     case DW_TAG_member:
4587       return 1;
4588
4589     case DW_TAG_variable:
4590       /* We only need to prefix "globally" visible variables.  These include
4591          any variable marked with DW_AT_external or any variable that
4592          lives in a namespace.  [Variables in anonymous namespaces
4593          require prefixing, but they are not DW_AT_external.]  */
4594
4595       if (dwarf2_attr (die, DW_AT_specification, cu))
4596         {
4597           struct dwarf2_cu *spec_cu = cu;
4598
4599           return die_needs_namespace (die_specification (die, &spec_cu),
4600                                       spec_cu);
4601         }
4602
4603       attr = dwarf2_attr (die, DW_AT_external, cu);
4604       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4605           && die->parent->tag != DW_TAG_module)
4606         return 0;
4607       /* A variable in a lexical block of some kind does not need a
4608          namespace, even though in C++ such variables may be external
4609          and have a mangled name.  */
4610       if (die->parent->tag ==  DW_TAG_lexical_block
4611           || die->parent->tag ==  DW_TAG_try_block
4612           || die->parent->tag ==  DW_TAG_catch_block
4613           || die->parent->tag == DW_TAG_subprogram)
4614         return 0;
4615       return 1;
4616
4617     default:
4618       return 0;
4619     }
4620 }
4621
4622 /* Retrieve the last character from a mem_file.  */
4623
4624 static void
4625 do_ui_file_peek_last (void *object, const char *buffer, long length)
4626 {
4627   char *last_char_p = (char *) object;
4628
4629   if (length > 0)
4630     *last_char_p = buffer[length - 1];
4631 }
4632
4633 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4634    compute the physname for the object, which include a method's
4635    formal parameters (C++/Java) and return type (Java).
4636
4637    For Ada, return the DIE's linkage name rather than the fully qualified
4638    name.  PHYSNAME is ignored..
4639
4640    The result is allocated on the objfile_obstack and canonicalized.  */
4641
4642 static const char *
4643 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4644                      int physname)
4645 {
4646   if (name == NULL)
4647     name = dwarf2_name (die, cu);
4648
4649   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4650      compute it by typename_concat inside GDB.  */
4651   if (cu->language == language_ada
4652       || (cu->language == language_fortran && physname))
4653     {
4654       /* For Ada unit, we prefer the linkage name over the name, as
4655          the former contains the exported name, which the user expects
4656          to be able to reference.  Ideally, we want the user to be able
4657          to reference this entity using either natural or linkage name,
4658          but we haven't started looking at this enhancement yet.  */
4659       struct attribute *attr;
4660
4661       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4662       if (attr == NULL)
4663         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4664       if (attr && DW_STRING (attr))
4665         return DW_STRING (attr);
4666     }
4667
4668   /* These are the only languages we know how to qualify names in.  */
4669   if (name != NULL
4670       && (cu->language == language_cplus || cu->language == language_java
4671           || cu->language == language_fortran))
4672     {
4673       if (die_needs_namespace (die, cu))
4674         {
4675           long length;
4676           char *prefix;
4677           struct ui_file *buf;
4678
4679           prefix = determine_prefix (die, cu);
4680           buf = mem_fileopen ();
4681           if (*prefix != '\0')
4682             {
4683               char *prefixed_name = typename_concat (NULL, prefix, name,
4684                                                      physname, cu);
4685
4686               fputs_unfiltered (prefixed_name, buf);
4687               xfree (prefixed_name);
4688             }
4689           else
4690             fputs_unfiltered (name ? name : "", buf);
4691
4692           /* Template parameters may be specified in the DIE's DW_AT_name, or
4693              as children with DW_TAG_template_type_param or
4694              DW_TAG_value_type_param.  If the latter, add them to the name
4695              here.  If the name already has template parameters, then
4696              skip this step; some versions of GCC emit both, and
4697              it is more efficient to use the pre-computed name.
4698
4699              Something to keep in mind about this process: it is very
4700              unlikely, or in some cases downright impossible, to produce
4701              something that will match the mangled name of a function.
4702              If the definition of the function has the same debug info,
4703              we should be able to match up with it anyway.  But fallbacks
4704              using the minimal symbol, for instance to find a method
4705              implemented in a stripped copy of libstdc++, will not work.
4706              If we do not have debug info for the definition, we will have to
4707              match them up some other way.
4708
4709              When we do name matching there is a related problem with function
4710              templates; two instantiated function templates are allowed to
4711              differ only by their return types, which we do not add here.  */
4712
4713           if (cu->language == language_cplus && strchr (name, '<') == NULL)
4714             {
4715               struct attribute *attr;
4716               struct die_info *child;
4717               int first = 1;
4718
4719               die->building_fullname = 1;
4720
4721               for (child = die->child; child != NULL; child = child->sibling)
4722                 {
4723                   struct type *type;
4724                   long value;
4725                   gdb_byte *bytes;
4726                   struct dwarf2_locexpr_baton *baton;
4727                   struct value *v;
4728
4729                   if (child->tag != DW_TAG_template_type_param
4730                       && child->tag != DW_TAG_template_value_param)
4731                     continue;
4732
4733                   if (first)
4734                     {
4735                       fputs_unfiltered ("<", buf);
4736                       first = 0;
4737                     }
4738                   else
4739                     fputs_unfiltered (", ", buf);
4740
4741                   attr = dwarf2_attr (child, DW_AT_type, cu);
4742                   if (attr == NULL)
4743                     {
4744                       complaint (&symfile_complaints,
4745                                  _("template parameter missing DW_AT_type"));
4746                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
4747                       continue;
4748                     }
4749                   type = die_type (child, cu);
4750
4751                   if (child->tag == DW_TAG_template_type_param)
4752                     {
4753                       c_print_type (type, "", buf, -1, 0);
4754                       continue;
4755                     }
4756
4757                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
4758                   if (attr == NULL)
4759                     {
4760                       complaint (&symfile_complaints,
4761                                  _("template parameter missing DW_AT_const_value"));
4762                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
4763                       continue;
4764                     }
4765
4766                   dwarf2_const_value_attr (attr, type, name,
4767                                            &cu->comp_unit_obstack, cu,
4768                                            &value, &bytes, &baton);
4769
4770                   if (TYPE_NOSIGN (type))
4771                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
4772                        changed, this can use value_print instead.  */
4773                     c_printchar (value, type, buf);
4774                   else
4775                     {
4776                       struct value_print_options opts;
4777
4778                       if (baton != NULL)
4779                         v = dwarf2_evaluate_loc_desc (type, NULL,
4780                                                       baton->data,
4781                                                       baton->size,
4782                                                       baton->per_cu);
4783                       else if (bytes != NULL)
4784                         {
4785                           v = allocate_value (type);
4786                           memcpy (value_contents_writeable (v), bytes,
4787                                   TYPE_LENGTH (type));
4788                         }
4789                       else
4790                         v = value_from_longest (type, value);
4791
4792                       /* Specify decimal so that we do not depend on the radix.  */
4793                       get_formatted_print_options (&opts, 'd');
4794                       opts.raw = 1;
4795                       value_print (v, buf, &opts);
4796                       release_value (v);
4797                       value_free (v);
4798                     }
4799                 }
4800
4801               die->building_fullname = 0;
4802
4803               if (!first)
4804                 {
4805                   /* Close the argument list, with a space if necessary
4806                      (nested templates).  */
4807                   char last_char = '\0';
4808                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
4809                   if (last_char == '>')
4810                     fputs_unfiltered (" >", buf);
4811                   else
4812                     fputs_unfiltered (">", buf);
4813                 }
4814             }
4815
4816           /* For Java and C++ methods, append formal parameter type
4817              information, if PHYSNAME.  */
4818
4819           if (physname && die->tag == DW_TAG_subprogram
4820               && (cu->language == language_cplus
4821                   || cu->language == language_java))
4822             {
4823               struct type *type = read_type_die (die, cu);
4824
4825               c_type_print_args (type, buf, 0, cu->language);
4826
4827               if (cu->language == language_java)
4828                 {
4829                   /* For java, we must append the return type to method
4830                      names. */
4831                   if (die->tag == DW_TAG_subprogram)
4832                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4833                                      0, 0);
4834                 }
4835               else if (cu->language == language_cplus)
4836                 {
4837                   if (TYPE_NFIELDS (type) > 0
4838                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4839                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4840                     fputs_unfiltered (" const", buf);
4841                 }
4842             }
4843
4844           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4845                                        &length);
4846           ui_file_delete (buf);
4847
4848           if (cu->language == language_cplus)
4849             {
4850               char *cname
4851                 = dwarf2_canonicalize_name (name, cu,
4852                                             &cu->objfile->objfile_obstack);
4853
4854               if (cname != NULL)
4855                 name = cname;
4856             }
4857         }
4858     }
4859
4860   return name;
4861 }
4862
4863 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4864    If scope qualifiers are appropriate they will be added.  The result
4865    will be allocated on the objfile_obstack, or NULL if the DIE does
4866    not have a name.  NAME may either be from a previous call to
4867    dwarf2_name or NULL.
4868
4869    The output string will be canonicalized (if C++/Java). */
4870
4871 static const char *
4872 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4873 {
4874   return dwarf2_compute_name (name, die, cu, 0);
4875 }
4876
4877 /* Construct a physname for the given DIE in CU.  NAME may either be
4878    from a previous call to dwarf2_name or NULL.  The result will be
4879    allocated on the objfile_objstack or NULL if the DIE does not have a
4880    name.
4881
4882    The output string will be canonicalized (if C++/Java).  */
4883
4884 static const char *
4885 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4886 {
4887   return dwarf2_compute_name (name, die, cu, 1);
4888 }
4889
4890 /* Read the import statement specified by the given die and record it.  */
4891
4892 static void
4893 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4894 {
4895   struct attribute *import_attr;
4896   struct die_info *imported_die;
4897   struct dwarf2_cu *imported_cu;
4898   const char *imported_name;
4899   const char *imported_name_prefix;
4900   const char *canonical_name;
4901   const char *import_alias;
4902   const char *imported_declaration = NULL;
4903   const char *import_prefix;
4904
4905   char *temp;
4906
4907   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4908   if (import_attr == NULL)
4909     {
4910       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4911                  dwarf_tag_name (die->tag));
4912       return;
4913     }
4914
4915   imported_cu = cu;
4916   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4917   imported_name = dwarf2_name (imported_die, imported_cu);
4918   if (imported_name == NULL)
4919     {
4920       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4921
4922         The import in the following code:
4923         namespace A
4924           {
4925             typedef int B;
4926           }
4927
4928         int main ()
4929           {
4930             using A::B;
4931             B b;
4932             return b;
4933           }
4934
4935         ...
4936          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4937             <52>   DW_AT_decl_file   : 1
4938             <53>   DW_AT_decl_line   : 6
4939             <54>   DW_AT_import      : <0x75>
4940          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4941             <59>   DW_AT_name        : B
4942             <5b>   DW_AT_decl_file   : 1
4943             <5c>   DW_AT_decl_line   : 2
4944             <5d>   DW_AT_type        : <0x6e>
4945         ...
4946          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4947             <76>   DW_AT_byte_size   : 4
4948             <77>   DW_AT_encoding    : 5        (signed)
4949
4950         imports the wrong die ( 0x75 instead of 0x58 ).
4951         This case will be ignored until the gcc bug is fixed.  */
4952       return;
4953     }
4954
4955   /* Figure out the local name after import.  */
4956   import_alias = dwarf2_name (die, cu);
4957
4958   /* Figure out where the statement is being imported to.  */
4959   import_prefix = determine_prefix (die, cu);
4960
4961   /* Figure out what the scope of the imported die is and prepend it
4962      to the name of the imported die.  */
4963   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4964
4965   if (imported_die->tag != DW_TAG_namespace
4966       && imported_die->tag != DW_TAG_module)
4967     {
4968       imported_declaration = imported_name;
4969       canonical_name = imported_name_prefix;
4970     }
4971   else if (strlen (imported_name_prefix) > 0)
4972     {
4973       temp = alloca (strlen (imported_name_prefix)
4974                      + 2 + strlen (imported_name) + 1);
4975       strcpy (temp, imported_name_prefix);
4976       strcat (temp, "::");
4977       strcat (temp, imported_name);
4978       canonical_name = temp;
4979     }
4980   else
4981     canonical_name = imported_name;
4982
4983   cp_add_using_directive (import_prefix,
4984                           canonical_name,
4985                           import_alias,
4986                           imported_declaration,
4987                           &cu->objfile->objfile_obstack);
4988 }
4989
4990 static void
4991 initialize_cu_func_list (struct dwarf2_cu *cu)
4992 {
4993   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4994 }
4995
4996 static void
4997 free_cu_line_header (void *arg)
4998 {
4999   struct dwarf2_cu *cu = arg;
5000
5001   free_line_header (cu->line_header);
5002   cu->line_header = NULL;
5003 }
5004
5005 static void
5006 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5007                          char **name, char **comp_dir)
5008 {
5009   struct attribute *attr;
5010
5011   *name = NULL;
5012   *comp_dir = NULL;
5013
5014   /* Find the filename.  Do not use dwarf2_name here, since the filename
5015      is not a source language identifier.  */
5016   attr = dwarf2_attr (die, DW_AT_name, cu);
5017   if (attr)
5018     {
5019       *name = DW_STRING (attr);
5020     }
5021
5022   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5023   if (attr)
5024     *comp_dir = DW_STRING (attr);
5025   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5026     {
5027       *comp_dir = ldirname (*name);
5028       if (*comp_dir != NULL)
5029         make_cleanup (xfree, *comp_dir);
5030     }
5031   if (*comp_dir != NULL)
5032     {
5033       /* Irix 6.2 native cc prepends <machine>.: to the compilation
5034          directory, get rid of it.  */
5035       char *cp = strchr (*comp_dir, ':');
5036
5037       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5038         *comp_dir = cp + 1;
5039     }
5040
5041   if (*name == NULL)
5042     *name = "<unknown>";
5043 }
5044
5045 static void
5046 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5047 {
5048   struct objfile *objfile = cu->objfile;
5049   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5050   CORE_ADDR lowpc = ((CORE_ADDR) -1);
5051   CORE_ADDR highpc = ((CORE_ADDR) 0);
5052   struct attribute *attr;
5053   char *name = NULL;
5054   char *comp_dir = NULL;
5055   struct die_info *child_die;
5056   bfd *abfd = objfile->obfd;
5057   struct line_header *line_header = 0;
5058   CORE_ADDR baseaddr;
5059
5060   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5061
5062   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5063
5064   /* If we didn't find a lowpc, set it to highpc to avoid complaints
5065      from finish_block.  */
5066   if (lowpc == ((CORE_ADDR) -1))
5067     lowpc = highpc;
5068   lowpc += baseaddr;
5069   highpc += baseaddr;
5070
5071   find_file_and_directory (die, cu, &name, &comp_dir);
5072
5073   attr = dwarf2_attr (die, DW_AT_language, cu);
5074   if (attr)
5075     {
5076       set_cu_language (DW_UNSND (attr), cu);
5077     }
5078
5079   attr = dwarf2_attr (die, DW_AT_producer, cu);
5080   if (attr)
5081     cu->producer = DW_STRING (attr);
5082
5083   /* We assume that we're processing GCC output. */
5084   processing_gcc_compilation = 2;
5085
5086   processing_has_namespace_info = 0;
5087
5088   start_symtab (name, comp_dir, lowpc);
5089   record_debugformat ("DWARF 2");
5090   record_producer (cu->producer);
5091
5092   initialize_cu_func_list (cu);
5093
5094   /* Decode line number information if present.  We do this before
5095      processing child DIEs, so that the line header table is available
5096      for DW_AT_decl_file.  */
5097   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5098   if (attr)
5099     {
5100       unsigned int line_offset = DW_UNSND (attr);
5101       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5102       if (line_header)
5103         {
5104           cu->line_header = line_header;
5105           make_cleanup (free_cu_line_header, cu);
5106           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5107         }
5108     }
5109
5110   /* Process all dies in compilation unit.  */
5111   if (die->child != NULL)
5112     {
5113       child_die = die->child;
5114       while (child_die && child_die->tag)
5115         {
5116           process_die (child_die, cu);
5117           child_die = sibling_die (child_die);
5118         }
5119     }
5120
5121   /* Decode macro information, if present.  Dwarf 2 macro information
5122      refers to information in the line number info statement program
5123      header, so we can only read it if we've read the header
5124      successfully.  */
5125   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5126   if (attr && line_header)
5127     {
5128       unsigned int macro_offset = DW_UNSND (attr);
5129
5130       dwarf_decode_macros (line_header, macro_offset,
5131                            comp_dir, abfd, cu);
5132     }
5133   do_cleanups (back_to);
5134 }
5135
5136 /* For TUs we want to skip the first top level sibling if it's not the
5137    actual type being defined by this TU.  In this case the first top
5138    level sibling is there to provide context only.  */
5139
5140 static void
5141 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5142 {
5143   struct objfile *objfile = cu->objfile;
5144   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5145   CORE_ADDR lowpc;
5146   struct attribute *attr;
5147   char *name = NULL;
5148   char *comp_dir = NULL;
5149   struct die_info *child_die;
5150   bfd *abfd = objfile->obfd;
5151
5152   /* start_symtab needs a low pc, but we don't really have one.
5153      Do what read_file_scope would do in the absence of such info.  */
5154   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5155
5156   /* Find the filename.  Do not use dwarf2_name here, since the filename
5157      is not a source language identifier.  */
5158   attr = dwarf2_attr (die, DW_AT_name, cu);
5159   if (attr)
5160     name = DW_STRING (attr);
5161
5162   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5163   if (attr)
5164     comp_dir = DW_STRING (attr);
5165   else if (name != NULL && IS_ABSOLUTE_PATH (name))
5166     {
5167       comp_dir = ldirname (name);
5168       if (comp_dir != NULL)
5169         make_cleanup (xfree, comp_dir);
5170     }
5171
5172   if (name == NULL)
5173     name = "<unknown>";
5174
5175   attr = dwarf2_attr (die, DW_AT_language, cu);
5176   if (attr)
5177     set_cu_language (DW_UNSND (attr), cu);
5178
5179   /* This isn't technically needed today.  It is done for symmetry
5180      with read_file_scope.  */
5181   attr = dwarf2_attr (die, DW_AT_producer, cu);
5182   if (attr)
5183     cu->producer = DW_STRING (attr);
5184
5185   /* We assume that we're processing GCC output. */
5186   processing_gcc_compilation = 2;
5187
5188   processing_has_namespace_info = 0;
5189
5190   start_symtab (name, comp_dir, lowpc);
5191   record_debugformat ("DWARF 2");
5192   record_producer (cu->producer);
5193
5194   /* Process the dies in the type unit.  */
5195   if (die->child == NULL)
5196     {
5197       dump_die_for_error (die);
5198       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5199              bfd_get_filename (abfd));
5200     }
5201
5202   child_die = die->child;
5203
5204   while (child_die && child_die->tag)
5205     {
5206       process_die (child_die, cu);
5207
5208       child_die = sibling_die (child_die);
5209     }
5210
5211   do_cleanups (back_to);
5212 }
5213
5214 static void
5215 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5216                      struct dwarf2_cu *cu)
5217 {
5218   struct function_range *thisfn;
5219
5220   thisfn = (struct function_range *)
5221     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5222   thisfn->name = name;
5223   thisfn->lowpc = lowpc;
5224   thisfn->highpc = highpc;
5225   thisfn->seen_line = 0;
5226   thisfn->next = NULL;
5227
5228   if (cu->last_fn == NULL)
5229       cu->first_fn = thisfn;
5230   else
5231       cu->last_fn->next = thisfn;
5232
5233   cu->last_fn = thisfn;
5234 }
5235
5236 /* qsort helper for inherit_abstract_dies.  */
5237
5238 static int
5239 unsigned_int_compar (const void *ap, const void *bp)
5240 {
5241   unsigned int a = *(unsigned int *) ap;
5242   unsigned int b = *(unsigned int *) bp;
5243
5244   return (a > b) - (b > a);
5245 }
5246
5247 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5248    Inherit only the children of the DW_AT_abstract_origin DIE not being already
5249    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
5250
5251 static void
5252 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5253 {
5254   struct die_info *child_die;
5255   unsigned die_children_count;
5256   /* CU offsets which were referenced by children of the current DIE.  */
5257   unsigned *offsets;
5258   unsigned *offsets_end, *offsetp;
5259   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
5260   struct die_info *origin_die;
5261   /* Iterator of the ORIGIN_DIE children.  */
5262   struct die_info *origin_child_die;
5263   struct cleanup *cleanups;
5264   struct attribute *attr;
5265
5266   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5267   if (!attr)
5268     return;
5269
5270   origin_die = follow_die_ref (die, attr, &cu);
5271   if (die->tag != origin_die->tag
5272       && !(die->tag == DW_TAG_inlined_subroutine
5273            && origin_die->tag == DW_TAG_subprogram))
5274     complaint (&symfile_complaints,
5275                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5276                die->offset, origin_die->offset);
5277
5278   child_die = die->child;
5279   die_children_count = 0;
5280   while (child_die && child_die->tag)
5281     {
5282       child_die = sibling_die (child_die);
5283       die_children_count++;
5284     }
5285   offsets = xmalloc (sizeof (*offsets) * die_children_count);
5286   cleanups = make_cleanup (xfree, offsets);
5287
5288   offsets_end = offsets;
5289   child_die = die->child;
5290   while (child_die && child_die->tag)
5291     {
5292       /* For each CHILD_DIE, find the corresponding child of
5293          ORIGIN_DIE.  If there is more than one layer of
5294          DW_AT_abstract_origin, follow them all; there shouldn't be,
5295          but GCC versions at least through 4.4 generate this (GCC PR
5296          40573).  */
5297       struct die_info *child_origin_die = child_die;
5298
5299       while (1)
5300         {
5301           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
5302           if (attr == NULL)
5303             break;
5304           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
5305         }
5306
5307       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5308          counterpart may exist.  */
5309       if (child_origin_die != child_die)
5310         {
5311           if (child_die->tag != child_origin_die->tag
5312               && !(child_die->tag == DW_TAG_inlined_subroutine
5313                    && child_origin_die->tag == DW_TAG_subprogram))
5314             complaint (&symfile_complaints,
5315                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5316                          "different tags"), child_die->offset,
5317                        child_origin_die->offset);
5318           if (child_origin_die->parent != origin_die)
5319             complaint (&symfile_complaints,
5320                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5321                          "different parents"), child_die->offset,
5322                        child_origin_die->offset);
5323           else
5324             *offsets_end++ = child_origin_die->offset;
5325         }
5326       child_die = sibling_die (child_die);
5327     }
5328   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5329          unsigned_int_compar);
5330   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5331     if (offsetp[-1] == *offsetp)
5332       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5333                                         "to DIE 0x%x as their abstract origin"),
5334                  die->offset, *offsetp);
5335
5336   offsetp = offsets;
5337   origin_child_die = origin_die->child;
5338   while (origin_child_die && origin_child_die->tag)
5339     {
5340       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5341       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5342         offsetp++;
5343       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5344         {
5345           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5346           process_die (origin_child_die, cu);
5347         }
5348       origin_child_die = sibling_die (origin_child_die);
5349     }
5350
5351   do_cleanups (cleanups);
5352 }
5353
5354 static void
5355 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5356 {
5357   struct objfile *objfile = cu->objfile;
5358   struct context_stack *new;
5359   CORE_ADDR lowpc;
5360   CORE_ADDR highpc;
5361   struct die_info *child_die;
5362   struct attribute *attr, *call_line, *call_file;
5363   char *name;
5364   CORE_ADDR baseaddr;
5365   struct block *block;
5366   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5367   VEC (symbolp) *template_args = NULL;
5368   struct template_symbol *templ_func = NULL;
5369
5370   if (inlined_func)
5371     {
5372       /* If we do not have call site information, we can't show the
5373          caller of this inlined function.  That's too confusing, so
5374          only use the scope for local variables.  */
5375       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5376       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5377       if (call_line == NULL || call_file == NULL)
5378         {
5379           read_lexical_block_scope (die, cu);
5380           return;
5381         }
5382     }
5383
5384   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5385
5386   name = dwarf2_name (die, cu);
5387
5388   /* Ignore functions with missing or empty names.  These are actually
5389      illegal according to the DWARF standard.  */
5390   if (name == NULL)
5391     {
5392       complaint (&symfile_complaints,
5393                  _("missing name for subprogram DIE at %d"), die->offset);
5394       return;
5395     }
5396
5397   /* Ignore functions with missing or invalid low and high pc attributes.  */
5398   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5399     {
5400       attr = dwarf2_attr (die, DW_AT_external, cu);
5401       if (!attr || !DW_UNSND (attr))
5402         complaint (&symfile_complaints,
5403                    _("cannot get low and high bounds for subprogram DIE at %d"),
5404                    die->offset);
5405       return;
5406     }
5407
5408   lowpc += baseaddr;
5409   highpc += baseaddr;
5410
5411   /* Record the function range for dwarf_decode_lines.  */
5412   add_to_cu_func_list (name, lowpc, highpc, cu);
5413
5414   /* If we have any template arguments, then we must allocate a
5415      different sort of symbol.  */
5416   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5417     {
5418       if (child_die->tag == DW_TAG_template_type_param
5419           || child_die->tag == DW_TAG_template_value_param)
5420         {
5421           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5422                                        struct template_symbol);
5423           templ_func->base.is_cplus_template_function = 1;
5424           break;
5425         }
5426     }
5427
5428   new = push_context (0, lowpc);
5429   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5430                                (struct symbol *) templ_func);
5431
5432   /* If there is a location expression for DW_AT_frame_base, record
5433      it.  */
5434   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5435   if (attr)
5436     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5437        expression is being recorded directly in the function's symbol
5438        and not in a separate frame-base object.  I guess this hack is
5439        to avoid adding some sort of frame-base adjunct/annex to the
5440        function's symbol :-(.  The problem with doing this is that it
5441        results in a function symbol with a location expression that
5442        has nothing to do with the location of the function, ouch!  The
5443        relationship should be: a function's symbol has-a frame base; a
5444        frame-base has-a location expression.  */
5445     dwarf2_symbol_mark_computed (attr, new->name, cu);
5446
5447   cu->list_in_scope = &local_symbols;
5448
5449   if (die->child != NULL)
5450     {
5451       child_die = die->child;
5452       while (child_die && child_die->tag)
5453         {
5454           if (child_die->tag == DW_TAG_template_type_param
5455               || child_die->tag == DW_TAG_template_value_param)
5456             {
5457               struct symbol *arg = new_symbol (child_die, NULL, cu);
5458
5459               VEC_safe_push (symbolp, template_args, arg);
5460             }
5461           else
5462             process_die (child_die, cu);
5463           child_die = sibling_die (child_die);
5464         }
5465     }
5466
5467   inherit_abstract_dies (die, cu);
5468
5469   /* If we have a DW_AT_specification, we might need to import using
5470      directives from the context of the specification DIE.  See the
5471      comment in determine_prefix.  */
5472   if (cu->language == language_cplus
5473       && dwarf2_attr (die, DW_AT_specification, cu))
5474     {
5475       struct dwarf2_cu *spec_cu = cu;
5476       struct die_info *spec_die = die_specification (die, &spec_cu);
5477
5478       while (spec_die)
5479         {
5480           child_die = spec_die->child;
5481           while (child_die && child_die->tag)
5482             {
5483               if (child_die->tag == DW_TAG_imported_module)
5484                 process_die (child_die, spec_cu);
5485               child_die = sibling_die (child_die);
5486             }
5487
5488           /* In some cases, GCC generates specification DIEs that
5489              themselves contain DW_AT_specification attributes.  */
5490           spec_die = die_specification (spec_die, &spec_cu);
5491         }
5492     }
5493
5494   new = pop_context ();
5495   /* Make a block for the local symbols within.  */
5496   block = finish_block (new->name, &local_symbols, new->old_blocks,
5497                         lowpc, highpc, objfile);
5498
5499   /* For C++, set the block's scope.  */
5500   if (cu->language == language_cplus || cu->language == language_fortran)
5501     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5502                         determine_prefix (die, cu),
5503                         processing_has_namespace_info);
5504
5505   /* If we have address ranges, record them.  */
5506   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5507
5508   /* Attach template arguments to function.  */
5509   if (! VEC_empty (symbolp, template_args))
5510     {
5511       gdb_assert (templ_func != NULL);
5512
5513       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5514       templ_func->template_arguments
5515         = obstack_alloc (&objfile->objfile_obstack,
5516                          (templ_func->n_template_arguments
5517                           * sizeof (struct symbol *)));
5518       memcpy (templ_func->template_arguments,
5519               VEC_address (symbolp, template_args),
5520               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5521       VEC_free (symbolp, template_args);
5522     }
5523
5524   /* In C++, we can have functions nested inside functions (e.g., when
5525      a function declares a class that has methods).  This means that
5526      when we finish processing a function scope, we may need to go
5527      back to building a containing block's symbol lists.  */
5528   local_symbols = new->locals;
5529   param_symbols = new->params;
5530   using_directives = new->using_directives;
5531
5532   /* If we've finished processing a top-level function, subsequent
5533      symbols go in the file symbol list.  */
5534   if (outermost_context_p ())
5535     cu->list_in_scope = &file_symbols;
5536 }
5537
5538 /* Process all the DIES contained within a lexical block scope.  Start
5539    a new scope, process the dies, and then close the scope.  */
5540
5541 static void
5542 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5543 {
5544   struct objfile *objfile = cu->objfile;
5545   struct context_stack *new;
5546   CORE_ADDR lowpc, highpc;
5547   struct die_info *child_die;
5548   CORE_ADDR baseaddr;
5549
5550   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5551
5552   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5553   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5554      as multiple lexical blocks?  Handling children in a sane way would
5555      be nasty.  Might be easier to properly extend generic blocks to
5556      describe ranges.  */
5557   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5558     return;
5559   lowpc += baseaddr;
5560   highpc += baseaddr;
5561
5562   push_context (0, lowpc);
5563   if (die->child != NULL)
5564     {
5565       child_die = die->child;
5566       while (child_die && child_die->tag)
5567         {
5568           process_die (child_die, cu);
5569           child_die = sibling_die (child_die);
5570         }
5571     }
5572   new = pop_context ();
5573
5574   if (local_symbols != NULL || using_directives != NULL)
5575     {
5576       struct block *block
5577         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5578                         highpc, objfile);
5579
5580       /* Note that recording ranges after traversing children, as we
5581          do here, means that recording a parent's ranges entails
5582          walking across all its children's ranges as they appear in
5583          the address map, which is quadratic behavior.
5584
5585          It would be nicer to record the parent's ranges before
5586          traversing its children, simply overriding whatever you find
5587          there.  But since we don't even decide whether to create a
5588          block until after we've traversed its children, that's hard
5589          to do.  */
5590       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5591     }
5592   local_symbols = new->locals;
5593   using_directives = new->using_directives;
5594 }
5595
5596 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5597    Return 1 if the attributes are present and valid, otherwise, return 0.
5598    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5599
5600 static int
5601 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5602                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5603                     struct partial_symtab *ranges_pst)
5604 {
5605   struct objfile *objfile = cu->objfile;
5606   struct comp_unit_head *cu_header = &cu->header;
5607   bfd *obfd = objfile->obfd;
5608   unsigned int addr_size = cu_header->addr_size;
5609   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5610   /* Base address selection entry.  */
5611   CORE_ADDR base;
5612   int found_base;
5613   unsigned int dummy;
5614   gdb_byte *buffer;
5615   CORE_ADDR marker;
5616   int low_set;
5617   CORE_ADDR low = 0;
5618   CORE_ADDR high = 0;
5619   CORE_ADDR baseaddr;
5620
5621   found_base = cu->base_known;
5622   base = cu->base_address;
5623
5624   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5625   if (offset >= dwarf2_per_objfile->ranges.size)
5626     {
5627       complaint (&symfile_complaints,
5628                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5629                  offset);
5630       return 0;
5631     }
5632   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5633
5634   /* Read in the largest possible address.  */
5635   marker = read_address (obfd, buffer, cu, &dummy);
5636   if ((marker & mask) == mask)
5637     {
5638       /* If we found the largest possible address, then
5639          read the base address.  */
5640       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5641       buffer += 2 * addr_size;
5642       offset += 2 * addr_size;
5643       found_base = 1;
5644     }
5645
5646   low_set = 0;
5647
5648   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5649
5650   while (1)
5651     {
5652       CORE_ADDR range_beginning, range_end;
5653
5654       range_beginning = read_address (obfd, buffer, cu, &dummy);
5655       buffer += addr_size;
5656       range_end = read_address (obfd, buffer, cu, &dummy);
5657       buffer += addr_size;
5658       offset += 2 * addr_size;
5659
5660       /* An end of list marker is a pair of zero addresses.  */
5661       if (range_beginning == 0 && range_end == 0)
5662         /* Found the end of list entry.  */
5663         break;
5664
5665       /* Each base address selection entry is a pair of 2 values.
5666          The first is the largest possible address, the second is
5667          the base address.  Check for a base address here.  */
5668       if ((range_beginning & mask) == mask)
5669         {
5670           /* If we found the largest possible address, then
5671              read the base address.  */
5672           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5673           found_base = 1;
5674           continue;
5675         }
5676
5677       if (!found_base)
5678         {
5679           /* We have no valid base address for the ranges
5680              data.  */
5681           complaint (&symfile_complaints,
5682                      _("Invalid .debug_ranges data (no base address)"));
5683           return 0;
5684         }
5685
5686       range_beginning += base;
5687       range_end += base;
5688
5689       if (ranges_pst != NULL && range_beginning < range_end)
5690         addrmap_set_empty (objfile->psymtabs_addrmap,
5691                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5692                            ranges_pst);
5693
5694       /* FIXME: This is recording everything as a low-high
5695          segment of consecutive addresses.  We should have a
5696          data structure for discontiguous block ranges
5697          instead.  */
5698       if (! low_set)
5699         {
5700           low = range_beginning;
5701           high = range_end;
5702           low_set = 1;
5703         }
5704       else
5705         {
5706           if (range_beginning < low)
5707             low = range_beginning;
5708           if (range_end > high)
5709             high = range_end;
5710         }
5711     }
5712
5713   if (! low_set)
5714     /* If the first entry is an end-of-list marker, the range
5715        describes an empty scope, i.e. no instructions.  */
5716     return 0;
5717
5718   if (low_return)
5719     *low_return = low;
5720   if (high_return)
5721     *high_return = high;
5722   return 1;
5723 }
5724
5725 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5726    are present and valid, otherwise, return 0.  Return -1 if the range is
5727    discontinuous, i.e. derived from DW_AT_ranges information.  */
5728 static int
5729 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5730                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5731                       struct partial_symtab *pst)
5732 {
5733   struct attribute *attr;
5734   CORE_ADDR low = 0;
5735   CORE_ADDR high = 0;
5736   int ret = 0;
5737
5738   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5739   if (attr)
5740     {
5741       high = DW_ADDR (attr);
5742       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5743       if (attr)
5744         low = DW_ADDR (attr);
5745       else
5746         /* Found high w/o low attribute.  */
5747         return 0;
5748
5749       /* Found consecutive range of addresses.  */
5750       ret = 1;
5751     }
5752   else
5753     {
5754       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5755       if (attr != NULL)
5756         {
5757           /* Value of the DW_AT_ranges attribute is the offset in the
5758              .debug_ranges section.  */
5759           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5760             return 0;
5761           /* Found discontinuous range of addresses.  */
5762           ret = -1;
5763         }
5764     }
5765
5766   if (high < low)
5767     return 0;
5768
5769   /* When using the GNU linker, .gnu.linkonce. sections are used to
5770      eliminate duplicate copies of functions and vtables and such.
5771      The linker will arbitrarily choose one and discard the others.
5772      The AT_*_pc values for such functions refer to local labels in
5773      these sections.  If the section from that file was discarded, the
5774      labels are not in the output, so the relocs get a value of 0.
5775      If this is a discarded function, mark the pc bounds as invalid,
5776      so that GDB will ignore it.  */
5777   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5778     return 0;
5779
5780   *lowpc = low;
5781   *highpc = high;
5782   return ret;
5783 }
5784
5785 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5786    its low and high PC addresses.  Do nothing if these addresses could not
5787    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5788    and HIGHPC to the high address if greater than HIGHPC.  */
5789
5790 static void
5791 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5792                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5793                                  struct dwarf2_cu *cu)
5794 {
5795   CORE_ADDR low, high;
5796   struct die_info *child = die->child;
5797
5798   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5799     {
5800       *lowpc = min (*lowpc, low);
5801       *highpc = max (*highpc, high);
5802     }
5803
5804   /* If the language does not allow nested subprograms (either inside
5805      subprograms or lexical blocks), we're done.  */
5806   if (cu->language != language_ada)
5807     return;
5808
5809   /* Check all the children of the given DIE.  If it contains nested
5810      subprograms, then check their pc bounds.  Likewise, we need to
5811      check lexical blocks as well, as they may also contain subprogram
5812      definitions.  */
5813   while (child && child->tag)
5814     {
5815       if (child->tag == DW_TAG_subprogram
5816           || child->tag == DW_TAG_lexical_block)
5817         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5818       child = sibling_die (child);
5819     }
5820 }
5821
5822 /* Get the low and high pc's represented by the scope DIE, and store
5823    them in *LOWPC and *HIGHPC.  If the correct values can't be
5824    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5825
5826 static void
5827 get_scope_pc_bounds (struct die_info *die,
5828                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5829                      struct dwarf2_cu *cu)
5830 {
5831   CORE_ADDR best_low = (CORE_ADDR) -1;
5832   CORE_ADDR best_high = (CORE_ADDR) 0;
5833   CORE_ADDR current_low, current_high;
5834
5835   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5836     {
5837       best_low = current_low;
5838       best_high = current_high;
5839     }
5840   else
5841     {
5842       struct die_info *child = die->child;
5843
5844       while (child && child->tag)
5845         {
5846           switch (child->tag) {
5847           case DW_TAG_subprogram:
5848             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5849             break;
5850           case DW_TAG_namespace:
5851           case DW_TAG_module:
5852             /* FIXME: carlton/2004-01-16: Should we do this for
5853                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5854                that current GCC's always emit the DIEs corresponding
5855                to definitions of methods of classes as children of a
5856                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5857                the DIEs giving the declarations, which could be
5858                anywhere).  But I don't see any reason why the
5859                standards says that they have to be there.  */
5860             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5861
5862             if (current_low != ((CORE_ADDR) -1))
5863               {
5864                 best_low = min (best_low, current_low);
5865                 best_high = max (best_high, current_high);
5866               }
5867             break;
5868           default:
5869             /* Ignore. */
5870             break;
5871           }
5872
5873           child = sibling_die (child);
5874         }
5875     }
5876
5877   *lowpc = best_low;
5878   *highpc = best_high;
5879 }
5880
5881 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5882    in DIE.  */
5883 static void
5884 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5885                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5886 {
5887   struct attribute *attr;
5888
5889   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5890   if (attr)
5891     {
5892       CORE_ADDR high = DW_ADDR (attr);
5893
5894       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5895       if (attr)
5896         {
5897           CORE_ADDR low = DW_ADDR (attr);
5898
5899           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5900         }
5901     }
5902
5903   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5904   if (attr)
5905     {
5906       bfd *obfd = cu->objfile->obfd;
5907
5908       /* The value of the DW_AT_ranges attribute is the offset of the
5909          address range list in the .debug_ranges section.  */
5910       unsigned long offset = DW_UNSND (attr);
5911       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5912
5913       /* For some target architectures, but not others, the
5914          read_address function sign-extends the addresses it returns.
5915          To recognize base address selection entries, we need a
5916          mask.  */
5917       unsigned int addr_size = cu->header.addr_size;
5918       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5919
5920       /* The base address, to which the next pair is relative.  Note
5921          that this 'base' is a DWARF concept: most entries in a range
5922          list are relative, to reduce the number of relocs against the
5923          debugging information.  This is separate from this function's
5924          'baseaddr' argument, which GDB uses to relocate debugging
5925          information from a shared library based on the address at
5926          which the library was loaded.  */
5927       CORE_ADDR base = cu->base_address;
5928       int base_known = cu->base_known;
5929
5930       gdb_assert (dwarf2_per_objfile->ranges.readin);
5931       if (offset >= dwarf2_per_objfile->ranges.size)
5932         {
5933           complaint (&symfile_complaints,
5934                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5935                      offset);
5936           return;
5937         }
5938
5939       for (;;)
5940         {
5941           unsigned int bytes_read;
5942           CORE_ADDR start, end;
5943
5944           start = read_address (obfd, buffer, cu, &bytes_read);
5945           buffer += bytes_read;
5946           end = read_address (obfd, buffer, cu, &bytes_read);
5947           buffer += bytes_read;
5948
5949           /* Did we find the end of the range list?  */
5950           if (start == 0 && end == 0)
5951             break;
5952
5953           /* Did we find a base address selection entry?  */
5954           else if ((start & base_select_mask) == base_select_mask)
5955             {
5956               base = end;
5957               base_known = 1;
5958             }
5959
5960           /* We found an ordinary address range.  */
5961           else
5962             {
5963               if (!base_known)
5964                 {
5965                   complaint (&symfile_complaints,
5966                              _("Invalid .debug_ranges data (no base address)"));
5967                   return;
5968                 }
5969
5970               record_block_range (block,
5971                                   baseaddr + base + start,
5972                                   baseaddr + base + end - 1);
5973             }
5974         }
5975     }
5976 }
5977
5978 /* Add an aggregate field to the field list.  */
5979
5980 static void
5981 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5982                   struct dwarf2_cu *cu)
5983 {
5984   struct objfile *objfile = cu->objfile;
5985   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5986   struct nextfield *new_field;
5987   struct attribute *attr;
5988   struct field *fp;
5989   char *fieldname = "";
5990
5991   /* Allocate a new field list entry and link it in.  */
5992   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5993   make_cleanup (xfree, new_field);
5994   memset (new_field, 0, sizeof (struct nextfield));
5995
5996   if (die->tag == DW_TAG_inheritance)
5997     {
5998       new_field->next = fip->baseclasses;
5999       fip->baseclasses = new_field;
6000     }
6001   else
6002     {
6003       new_field->next = fip->fields;
6004       fip->fields = new_field;
6005     }
6006   fip->nfields++;
6007
6008   /* Handle accessibility and virtuality of field.
6009      The default accessibility for members is public, the default
6010      accessibility for inheritance is private.  */
6011   if (die->tag != DW_TAG_inheritance)
6012     new_field->accessibility = DW_ACCESS_public;
6013   else
6014     new_field->accessibility = DW_ACCESS_private;
6015   new_field->virtuality = DW_VIRTUALITY_none;
6016
6017   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6018   if (attr)
6019     new_field->accessibility = DW_UNSND (attr);
6020   if (new_field->accessibility != DW_ACCESS_public)
6021     fip->non_public_fields = 1;
6022   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6023   if (attr)
6024     new_field->virtuality = DW_UNSND (attr);
6025
6026   fp = &new_field->field;
6027
6028   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6029     {
6030       /* Data member other than a C++ static data member.  */
6031
6032       /* Get type of field.  */
6033       fp->type = die_type (die, cu);
6034
6035       SET_FIELD_BITPOS (*fp, 0);
6036
6037       /* Get bit size of field (zero if none).  */
6038       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6039       if (attr)
6040         {
6041           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6042         }
6043       else
6044         {
6045           FIELD_BITSIZE (*fp) = 0;
6046         }
6047
6048       /* Get bit offset of field.  */
6049       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6050       if (attr)
6051         {
6052           int byte_offset = 0;
6053
6054           if (attr_form_is_section_offset (attr))
6055             dwarf2_complex_location_expr_complaint ();
6056           else if (attr_form_is_constant (attr))
6057             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6058           else if (attr_form_is_block (attr))
6059             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6060           else
6061             dwarf2_complex_location_expr_complaint ();
6062
6063           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6064         }
6065       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6066       if (attr)
6067         {
6068           if (gdbarch_bits_big_endian (gdbarch))
6069             {
6070               /* For big endian bits, the DW_AT_bit_offset gives the
6071                  additional bit offset from the MSB of the containing
6072                  anonymous object to the MSB of the field.  We don't
6073                  have to do anything special since we don't need to
6074                  know the size of the anonymous object.  */
6075               FIELD_BITPOS (*fp) += DW_UNSND (attr);
6076             }
6077           else
6078             {
6079               /* For little endian bits, compute the bit offset to the
6080                  MSB of the anonymous object, subtract off the number of
6081                  bits from the MSB of the field to the MSB of the
6082                  object, and then subtract off the number of bits of
6083                  the field itself.  The result is the bit offset of
6084                  the LSB of the field.  */
6085               int anonymous_size;
6086               int bit_offset = DW_UNSND (attr);
6087
6088               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6089               if (attr)
6090                 {
6091                   /* The size of the anonymous object containing
6092                      the bit field is explicit, so use the
6093                      indicated size (in bytes).  */
6094                   anonymous_size = DW_UNSND (attr);
6095                 }
6096               else
6097                 {
6098                   /* The size of the anonymous object containing
6099                      the bit field must be inferred from the type
6100                      attribute of the data member containing the
6101                      bit field.  */
6102                   anonymous_size = TYPE_LENGTH (fp->type);
6103                 }
6104               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6105                 - bit_offset - FIELD_BITSIZE (*fp);
6106             }
6107         }
6108
6109       /* Get name of field.  */
6110       fieldname = dwarf2_name (die, cu);
6111       if (fieldname == NULL)
6112         fieldname = "";
6113
6114       /* The name is already allocated along with this objfile, so we don't
6115          need to duplicate it for the type.  */
6116       fp->name = fieldname;
6117
6118       /* Change accessibility for artificial fields (e.g. virtual table
6119          pointer or virtual base class pointer) to private.  */
6120       if (dwarf2_attr (die, DW_AT_artificial, cu))
6121         {
6122           FIELD_ARTIFICIAL (*fp) = 1;
6123           new_field->accessibility = DW_ACCESS_private;
6124           fip->non_public_fields = 1;
6125         }
6126     }
6127   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6128     {
6129       /* C++ static member.  */
6130
6131       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6132          is a declaration, but all versions of G++ as of this writing
6133          (so through at least 3.2.1) incorrectly generate
6134          DW_TAG_variable tags.  */
6135
6136       char *physname;
6137
6138       /* Get name of field.  */
6139       fieldname = dwarf2_name (die, cu);
6140       if (fieldname == NULL)
6141         return;
6142
6143       attr = dwarf2_attr (die, DW_AT_const_value, cu);
6144       if (attr
6145           /* Only create a symbol if this is an external value.
6146              new_symbol checks this and puts the value in the global symbol
6147              table, which we want.  If it is not external, new_symbol
6148              will try to put the value in cu->list_in_scope which is wrong.  */
6149           && dwarf2_flag_true_p (die, DW_AT_external, cu))
6150         {
6151           /* A static const member, not much different than an enum as far as
6152              we're concerned, except that we can support more types.  */
6153           new_symbol (die, NULL, cu);
6154         }
6155
6156       /* Get physical name.  */
6157       physname = (char *) dwarf2_physname (fieldname, die, cu);
6158
6159       /* The name is already allocated along with this objfile, so we don't
6160          need to duplicate it for the type.  */
6161       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6162       FIELD_TYPE (*fp) = die_type (die, cu);
6163       FIELD_NAME (*fp) = fieldname;
6164     }
6165   else if (die->tag == DW_TAG_inheritance)
6166     {
6167       /* C++ base class field.  */
6168       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6169       if (attr)
6170         {
6171           int byte_offset = 0;
6172
6173           if (attr_form_is_section_offset (attr))
6174             dwarf2_complex_location_expr_complaint ();
6175           else if (attr_form_is_constant (attr))
6176             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6177           else if (attr_form_is_block (attr))
6178             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6179           else
6180             dwarf2_complex_location_expr_complaint ();
6181
6182           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6183         }
6184       FIELD_BITSIZE (*fp) = 0;
6185       FIELD_TYPE (*fp) = die_type (die, cu);
6186       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6187       fip->nbaseclasses++;
6188     }
6189 }
6190
6191 /* Add a typedef defined in the scope of the FIP's class.  */
6192
6193 static void
6194 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6195                     struct dwarf2_cu *cu)
6196 {
6197   struct objfile *objfile = cu->objfile;
6198   struct gdbarch *gdbarch = get_objfile_arch (objfile);
6199   struct typedef_field_list *new_field;
6200   struct attribute *attr;
6201   struct typedef_field *fp;
6202   char *fieldname = "";
6203
6204   /* Allocate a new field list entry and link it in.  */
6205   new_field = xzalloc (sizeof (*new_field));
6206   make_cleanup (xfree, new_field);
6207
6208   gdb_assert (die->tag == DW_TAG_typedef);
6209
6210   fp = &new_field->field;
6211
6212   /* Get name of field.  */
6213   fp->name = dwarf2_name (die, cu);
6214   if (fp->name == NULL)
6215     return;
6216
6217   fp->type = read_type_die (die, cu);
6218
6219   new_field->next = fip->typedef_field_list;
6220   fip->typedef_field_list = new_field;
6221   fip->typedef_field_list_count++;
6222 }
6223
6224 /* Create the vector of fields, and attach it to the type.  */
6225
6226 static void
6227 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6228                               struct dwarf2_cu *cu)
6229 {
6230   int nfields = fip->nfields;
6231
6232   /* Record the field count, allocate space for the array of fields,
6233      and create blank accessibility bitfields if necessary.  */
6234   TYPE_NFIELDS (type) = nfields;
6235   TYPE_FIELDS (type) = (struct field *)
6236     TYPE_ALLOC (type, sizeof (struct field) * nfields);
6237   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6238
6239   if (fip->non_public_fields && cu->language != language_ada)
6240     {
6241       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6242
6243       TYPE_FIELD_PRIVATE_BITS (type) =
6244         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6245       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6246
6247       TYPE_FIELD_PROTECTED_BITS (type) =
6248         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6249       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6250
6251       TYPE_FIELD_IGNORE_BITS (type) =
6252         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6253       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6254     }
6255
6256   /* If the type has baseclasses, allocate and clear a bit vector for
6257      TYPE_FIELD_VIRTUAL_BITS.  */
6258   if (fip->nbaseclasses && cu->language != language_ada)
6259     {
6260       int num_bytes = B_BYTES (fip->nbaseclasses);
6261       unsigned char *pointer;
6262
6263       ALLOCATE_CPLUS_STRUCT_TYPE (type);
6264       pointer = TYPE_ALLOC (type, num_bytes);
6265       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6266       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6267       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6268     }
6269
6270   /* Copy the saved-up fields into the field vector.  Start from the head
6271      of the list, adding to the tail of the field array, so that they end
6272      up in the same order in the array in which they were added to the list.  */
6273   while (nfields-- > 0)
6274     {
6275       struct nextfield *fieldp;
6276
6277       if (fip->fields)
6278         {
6279           fieldp = fip->fields;
6280           fip->fields = fieldp->next;
6281         }
6282       else
6283         {
6284           fieldp = fip->baseclasses;
6285           fip->baseclasses = fieldp->next;
6286         }
6287
6288       TYPE_FIELD (type, nfields) = fieldp->field;
6289       switch (fieldp->accessibility)
6290         {
6291         case DW_ACCESS_private:
6292           if (cu->language != language_ada)
6293             SET_TYPE_FIELD_PRIVATE (type, nfields);
6294           break;
6295
6296         case DW_ACCESS_protected:
6297           if (cu->language != language_ada)
6298             SET_TYPE_FIELD_PROTECTED (type, nfields);
6299           break;
6300
6301         case DW_ACCESS_public:
6302           break;
6303
6304         default:
6305           /* Unknown accessibility.  Complain and treat it as public.  */
6306           {
6307             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6308                        fieldp->accessibility);
6309           }
6310           break;
6311         }
6312       if (nfields < fip->nbaseclasses)
6313         {
6314           switch (fieldp->virtuality)
6315             {
6316             case DW_VIRTUALITY_virtual:
6317             case DW_VIRTUALITY_pure_virtual:
6318               if (cu->language == language_ada)
6319                 error ("unexpected virtuality in component of Ada type");
6320               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6321               break;
6322             }
6323         }
6324     }
6325 }
6326
6327 /* Add a member function to the proper fieldlist.  */
6328
6329 static void
6330 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6331                       struct type *type, struct dwarf2_cu *cu)
6332 {
6333   struct objfile *objfile = cu->objfile;
6334   struct attribute *attr;
6335   struct fnfieldlist *flp;
6336   int i;
6337   struct fn_field *fnp;
6338   char *fieldname;
6339   struct nextfnfield *new_fnfield;
6340   struct type *this_type;
6341
6342   if (cu->language == language_ada)
6343     error ("unexpected member function in Ada type");
6344
6345   /* Get name of member function.  */
6346   fieldname = dwarf2_name (die, cu);
6347   if (fieldname == NULL)
6348     return;
6349
6350   /* Look up member function name in fieldlist.  */
6351   for (i = 0; i < fip->nfnfields; i++)
6352     {
6353       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6354         break;
6355     }
6356
6357   /* Create new list element if necessary.  */
6358   if (i < fip->nfnfields)
6359     flp = &fip->fnfieldlists[i];
6360   else
6361     {
6362       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6363         {
6364           fip->fnfieldlists = (struct fnfieldlist *)
6365             xrealloc (fip->fnfieldlists,
6366                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6367                       * sizeof (struct fnfieldlist));
6368           if (fip->nfnfields == 0)
6369             make_cleanup (free_current_contents, &fip->fnfieldlists);
6370         }
6371       flp = &fip->fnfieldlists[fip->nfnfields];
6372       flp->name = fieldname;
6373       flp->length = 0;
6374       flp->head = NULL;
6375       i = fip->nfnfields++;
6376     }
6377
6378   /* Create a new member function field and chain it to the field list
6379      entry. */
6380   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6381   make_cleanup (xfree, new_fnfield);
6382   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6383   new_fnfield->next = flp->head;
6384   flp->head = new_fnfield;
6385   flp->length++;
6386
6387   /* Fill in the member function field info.  */
6388   fnp = &new_fnfield->fnfield;
6389
6390   /* Delay processing of the physname until later.  */
6391   if (cu->language == language_cplus || cu->language == language_java)
6392     {
6393       add_to_method_list (type, i, flp->length - 1, fieldname,
6394                           die, cu);
6395     }
6396   else
6397     {
6398       char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6399       fnp->physname = physname ? physname : "";
6400     }
6401
6402   fnp->type = alloc_type (objfile);
6403   this_type = read_type_die (die, cu);
6404   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6405     {
6406       int nparams = TYPE_NFIELDS (this_type);
6407
6408       /* TYPE is the domain of this method, and THIS_TYPE is the type
6409            of the method itself (TYPE_CODE_METHOD).  */
6410       smash_to_method_type (fnp->type, type,
6411                             TYPE_TARGET_TYPE (this_type),
6412                             TYPE_FIELDS (this_type),
6413                             TYPE_NFIELDS (this_type),
6414                             TYPE_VARARGS (this_type));
6415
6416       /* Handle static member functions.
6417          Dwarf2 has no clean way to discern C++ static and non-static
6418          member functions. G++ helps GDB by marking the first
6419          parameter for non-static member functions (which is the
6420          this pointer) as artificial. We obtain this information
6421          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6422       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6423         fnp->voffset = VOFFSET_STATIC;
6424     }
6425   else
6426     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6427                dwarf2_full_name (fieldname, die, cu));
6428
6429   /* Get fcontext from DW_AT_containing_type if present.  */
6430   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6431     fnp->fcontext = die_containing_type (die, cu);
6432
6433   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6434      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6435
6436   /* Get accessibility.  */
6437   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6438   if (attr)
6439     {
6440       switch (DW_UNSND (attr))
6441         {
6442         case DW_ACCESS_private:
6443           fnp->is_private = 1;
6444           break;
6445         case DW_ACCESS_protected:
6446           fnp->is_protected = 1;
6447           break;
6448         }
6449     }
6450
6451   /* Check for artificial methods.  */
6452   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6453   if (attr && DW_UNSND (attr) != 0)
6454     fnp->is_artificial = 1;
6455
6456   /* Get index in virtual function table if it is a virtual member
6457      function.  For older versions of GCC, this is an offset in the
6458      appropriate virtual table, as specified by DW_AT_containing_type.
6459      For everyone else, it is an expression to be evaluated relative
6460      to the object address.  */
6461
6462   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6463   if (attr)
6464     {
6465       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6466         {
6467           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6468             {
6469               /* Old-style GCC.  */
6470               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6471             }
6472           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6473                    || (DW_BLOCK (attr)->size > 1
6474                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6475                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6476             {
6477               struct dwarf_block blk;
6478               int offset;
6479
6480               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6481                         ? 1 : 2);
6482               blk.size = DW_BLOCK (attr)->size - offset;
6483               blk.data = DW_BLOCK (attr)->data + offset;
6484               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6485               if ((fnp->voffset % cu->header.addr_size) != 0)
6486                 dwarf2_complex_location_expr_complaint ();
6487               else
6488                 fnp->voffset /= cu->header.addr_size;
6489               fnp->voffset += 2;
6490             }
6491           else
6492             dwarf2_complex_location_expr_complaint ();
6493
6494           if (!fnp->fcontext)
6495             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6496         }
6497       else if (attr_form_is_section_offset (attr))
6498         {
6499           dwarf2_complex_location_expr_complaint ();
6500         }
6501       else
6502         {
6503           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6504                                                  fieldname);
6505         }
6506     }
6507   else
6508     {
6509       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6510       if (attr && DW_UNSND (attr))
6511         {
6512           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6513           complaint (&symfile_complaints,
6514                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6515                      fieldname, die->offset);
6516           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6517           TYPE_CPLUS_DYNAMIC (type) = 1;
6518         }
6519     }
6520 }
6521
6522 /* Create the vector of member function fields, and attach it to the type.  */
6523
6524 static void
6525 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6526                                  struct dwarf2_cu *cu)
6527 {
6528   struct fnfieldlist *flp;
6529   int total_length = 0;
6530   int i;
6531
6532   if (cu->language == language_ada)
6533     error ("unexpected member functions in Ada type");
6534
6535   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6536   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6537     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6538
6539   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6540     {
6541       struct nextfnfield *nfp = flp->head;
6542       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6543       int k;
6544
6545       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6546       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6547       fn_flp->fn_fields = (struct fn_field *)
6548         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6549       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6550         fn_flp->fn_fields[k] = nfp->fnfield;
6551
6552       total_length += flp->length;
6553     }
6554
6555   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6556   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6557 }
6558
6559 /* Returns non-zero if NAME is the name of a vtable member in CU's
6560    language, zero otherwise.  */
6561 static int
6562 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6563 {
6564   static const char vptr[] = "_vptr";
6565   static const char vtable[] = "vtable";
6566
6567   /* Look for the C++ and Java forms of the vtable.  */
6568   if ((cu->language == language_java
6569        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6570        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6571        && is_cplus_marker (name[sizeof (vptr) - 1])))
6572     return 1;
6573
6574   return 0;
6575 }
6576
6577 /* GCC outputs unnamed structures that are really pointers to member
6578    functions, with the ABI-specified layout.  If TYPE describes
6579    such a structure, smash it into a member function type.
6580
6581    GCC shouldn't do this; it should just output pointer to member DIEs.
6582    This is GCC PR debug/28767.  */
6583
6584 static void
6585 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6586 {
6587   struct type *pfn_type, *domain_type, *new_type;
6588
6589   /* Check for a structure with no name and two children.  */
6590   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6591     return;
6592
6593   /* Check for __pfn and __delta members.  */
6594   if (TYPE_FIELD_NAME (type, 0) == NULL
6595       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6596       || TYPE_FIELD_NAME (type, 1) == NULL
6597       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6598     return;
6599
6600   /* Find the type of the method.  */
6601   pfn_type = TYPE_FIELD_TYPE (type, 0);
6602   if (pfn_type == NULL
6603       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6604       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6605     return;
6606
6607   /* Look for the "this" argument.  */
6608   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6609   if (TYPE_NFIELDS (pfn_type) == 0
6610       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6611       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6612     return;
6613
6614   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6615   new_type = alloc_type (objfile);
6616   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6617                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6618                         TYPE_VARARGS (pfn_type));
6619   smash_to_methodptr_type (type, new_type);
6620 }
6621
6622 /* Called when we find the DIE that starts a structure or union scope
6623    (definition) to process all dies that define the members of the
6624    structure or union.
6625
6626    NOTE: we need to call struct_type regardless of whether or not the
6627    DIE has an at_name attribute, since it might be an anonymous
6628    structure or union.  This gets the type entered into our set of
6629    user defined types.
6630
6631    However, if the structure is incomplete (an opaque struct/union)
6632    then suppress creating a symbol table entry for it since gdb only
6633    wants to find the one with the complete definition.  Note that if
6634    it is complete, we just call new_symbol, which does it's own
6635    checking about whether the struct/union is anonymous or not (and
6636    suppresses creating a symbol table entry itself).  */
6637
6638 static struct type *
6639 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6640 {
6641   struct objfile *objfile = cu->objfile;
6642   struct type *type;
6643   struct attribute *attr;
6644   char *name;
6645   struct cleanup *back_to;
6646
6647   /* If the definition of this type lives in .debug_types, read that type.
6648      Don't follow DW_AT_specification though, that will take us back up
6649      the chain and we want to go down.  */
6650   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6651   if (attr)
6652     {
6653       struct dwarf2_cu *type_cu = cu;
6654       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6655
6656       /* We could just recurse on read_structure_type, but we need to call
6657          get_die_type to ensure only one type for this DIE is created.
6658          This is important, for example, because for c++ classes we need
6659          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6660       type = read_type_die (type_die, type_cu);
6661
6662       /* TYPE_CU may not be the same as CU.
6663          Ensure TYPE is recorded in CU's type_hash table.  */
6664       return set_die_type (die, type, cu);
6665     }
6666
6667   back_to = make_cleanup (null_cleanup, 0);
6668
6669   type = alloc_type (objfile);
6670   INIT_CPLUS_SPECIFIC (type);
6671
6672   name = dwarf2_name (die, cu);
6673   if (name != NULL)
6674     {
6675       if (cu->language == language_cplus
6676           || cu->language == language_java)
6677         {
6678           char *full_name = (char *) dwarf2_full_name (name, die, cu);
6679
6680           /* dwarf2_full_name might have already finished building the DIE's
6681              type.  If so, there is no need to continue.  */
6682           if (get_die_type (die, cu) != NULL)
6683             return get_die_type (die, cu);
6684
6685           TYPE_TAG_NAME (type) = full_name;
6686           if (die->tag == DW_TAG_structure_type
6687               || die->tag == DW_TAG_class_type)
6688             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6689         }
6690       else
6691         {
6692           /* The name is already allocated along with this objfile, so
6693              we don't need to duplicate it for the type.  */
6694           TYPE_TAG_NAME (type) = (char *) name;
6695           if (die->tag == DW_TAG_class_type)
6696             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6697         }
6698     }
6699
6700   if (die->tag == DW_TAG_structure_type)
6701     {
6702       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6703     }
6704   else if (die->tag == DW_TAG_union_type)
6705     {
6706       TYPE_CODE (type) = TYPE_CODE_UNION;
6707     }
6708   else
6709     {
6710       TYPE_CODE (type) = TYPE_CODE_CLASS;
6711     }
6712
6713   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6714     TYPE_DECLARED_CLASS (type) = 1;
6715
6716   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6717   if (attr)
6718     {
6719       TYPE_LENGTH (type) = DW_UNSND (attr);
6720     }
6721   else
6722     {
6723       TYPE_LENGTH (type) = 0;
6724     }
6725
6726   TYPE_STUB_SUPPORTED (type) = 1;
6727   if (die_is_declaration (die, cu))
6728     TYPE_STUB (type) = 1;
6729   else if (attr == NULL && die->child == NULL
6730            && producer_is_realview (cu->producer))
6731     /* RealView does not output the required DW_AT_declaration
6732        on incomplete types.  */
6733     TYPE_STUB (type) = 1;
6734
6735   /* We need to add the type field to the die immediately so we don't
6736      infinitely recurse when dealing with pointers to the structure
6737      type within the structure itself. */
6738   set_die_type (die, type, cu);
6739
6740   /* set_die_type should be already done.  */
6741   set_descriptive_type (type, die, cu);
6742
6743   if (die->child != NULL && ! die_is_declaration (die, cu))
6744     {
6745       struct field_info fi;
6746       struct die_info *child_die;
6747       VEC (symbolp) *template_args = NULL;
6748
6749       memset (&fi, 0, sizeof (struct field_info));
6750
6751       child_die = die->child;
6752
6753       while (child_die && child_die->tag)
6754         {
6755           if (child_die->tag == DW_TAG_member
6756               || child_die->tag == DW_TAG_variable)
6757             {
6758               /* NOTE: carlton/2002-11-05: A C++ static data member
6759                  should be a DW_TAG_member that is a declaration, but
6760                  all versions of G++ as of this writing (so through at
6761                  least 3.2.1) incorrectly generate DW_TAG_variable
6762                  tags for them instead.  */
6763               dwarf2_add_field (&fi, child_die, cu);
6764             }
6765           else if (child_die->tag == DW_TAG_subprogram)
6766             {
6767               /* C++ member function. */
6768               dwarf2_add_member_fn (&fi, child_die, type, cu);
6769             }
6770           else if (child_die->tag == DW_TAG_inheritance)
6771             {
6772               /* C++ base class field.  */
6773               dwarf2_add_field (&fi, child_die, cu);
6774             }
6775           else if (child_die->tag == DW_TAG_typedef)
6776             dwarf2_add_typedef (&fi, child_die, cu);
6777           else if (child_die->tag == DW_TAG_template_type_param
6778                    || child_die->tag == DW_TAG_template_value_param)
6779             {
6780               struct symbol *arg = new_symbol (child_die, NULL, cu);
6781
6782               VEC_safe_push (symbolp, template_args, arg);
6783             }
6784
6785           child_die = sibling_die (child_die);
6786         }
6787
6788       /* Attach template arguments to type.  */
6789       if (! VEC_empty (symbolp, template_args))
6790         {
6791           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6792           TYPE_N_TEMPLATE_ARGUMENTS (type)
6793             = VEC_length (symbolp, template_args);
6794           TYPE_TEMPLATE_ARGUMENTS (type)
6795             = obstack_alloc (&objfile->objfile_obstack,
6796                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
6797                               * sizeof (struct symbol *)));
6798           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6799                   VEC_address (symbolp, template_args),
6800                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
6801                    * sizeof (struct symbol *)));
6802           VEC_free (symbolp, template_args);
6803         }
6804
6805       /* Attach fields and member functions to the type.  */
6806       if (fi.nfields)
6807         dwarf2_attach_fields_to_type (&fi, type, cu);
6808       if (fi.nfnfields)
6809         {
6810           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6811
6812           /* Get the type which refers to the base class (possibly this
6813              class itself) which contains the vtable pointer for the current
6814              class from the DW_AT_containing_type attribute.  This use of
6815              DW_AT_containing_type is a GNU extension.  */
6816
6817           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6818             {
6819               struct type *t = die_containing_type (die, cu);
6820
6821               TYPE_VPTR_BASETYPE (type) = t;
6822               if (type == t)
6823                 {
6824                   int i;
6825
6826                   /* Our own class provides vtbl ptr.  */
6827                   for (i = TYPE_NFIELDS (t) - 1;
6828                        i >= TYPE_N_BASECLASSES (t);
6829                        --i)
6830                     {
6831                       char *fieldname = TYPE_FIELD_NAME (t, i);
6832
6833                       if (is_vtable_name (fieldname, cu))
6834                         {
6835                           TYPE_VPTR_FIELDNO (type) = i;
6836                           break;
6837                         }
6838                     }
6839
6840                   /* Complain if virtual function table field not found.  */
6841                   if (i < TYPE_N_BASECLASSES (t))
6842                     complaint (&symfile_complaints,
6843                                _("virtual function table pointer not found when defining class '%s'"),
6844                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6845                                "");
6846                 }
6847               else
6848                 {
6849                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6850                 }
6851             }
6852           else if (cu->producer
6853                    && strncmp (cu->producer,
6854                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6855             {
6856               /* The IBM XLC compiler does not provide direct indication
6857                  of the containing type, but the vtable pointer is
6858                  always named __vfp.  */
6859
6860               int i;
6861
6862               for (i = TYPE_NFIELDS (type) - 1;
6863                    i >= TYPE_N_BASECLASSES (type);
6864                    --i)
6865                 {
6866                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6867                     {
6868                       TYPE_VPTR_FIELDNO (type) = i;
6869                       TYPE_VPTR_BASETYPE (type) = type;
6870                       break;
6871                     }
6872                 }
6873             }
6874         }
6875
6876       /* Copy fi.typedef_field_list linked list elements content into the
6877          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6878       if (fi.typedef_field_list)
6879         {
6880           int i = fi.typedef_field_list_count;
6881
6882           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6883           TYPE_TYPEDEF_FIELD_ARRAY (type)
6884             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6885           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6886
6887           /* Reverse the list order to keep the debug info elements order.  */
6888           while (--i >= 0)
6889             {
6890               struct typedef_field *dest, *src;
6891
6892               dest = &TYPE_TYPEDEF_FIELD (type, i);
6893               src = &fi.typedef_field_list->field;
6894               fi.typedef_field_list = fi.typedef_field_list->next;
6895               *dest = *src;
6896             }
6897         }
6898     }
6899
6900   quirk_gcc_member_function_pointer (type, cu->objfile);
6901
6902   do_cleanups (back_to);
6903   return type;
6904 }
6905
6906 static void
6907 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6908 {
6909   struct die_info *child_die = die->child;
6910   struct type *this_type;
6911
6912   this_type = get_die_type (die, cu);
6913   if (this_type == NULL)
6914     this_type = read_structure_type (die, cu);
6915
6916   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6917      snapshots) has been known to create a die giving a declaration
6918      for a class that has, as a child, a die giving a definition for a
6919      nested class.  So we have to process our children even if the
6920      current die is a declaration.  Normally, of course, a declaration
6921      won't have any children at all.  */
6922
6923   while (child_die != NULL && child_die->tag)
6924     {
6925       if (child_die->tag == DW_TAG_member
6926           || child_die->tag == DW_TAG_variable
6927           || child_die->tag == DW_TAG_inheritance
6928           || child_die->tag == DW_TAG_template_value_param
6929           || child_die->tag == DW_TAG_template_type_param)
6930         {
6931           /* Do nothing.  */
6932         }
6933       else
6934         process_die (child_die, cu);
6935
6936       child_die = sibling_die (child_die);
6937     }
6938
6939   /* Do not consider external references.  According to the DWARF standard,
6940      these DIEs are identified by the fact that they have no byte_size
6941      attribute, and a declaration attribute.  */
6942   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6943       || !die_is_declaration (die, cu))
6944     new_symbol (die, this_type, cu);
6945 }
6946
6947 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6948    complete the type's fields yet, or create any symbols.  */
6949
6950 static struct type *
6951 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6952 {
6953   struct objfile *objfile = cu->objfile;
6954   struct type *type;
6955   struct attribute *attr;
6956   const char *name;
6957
6958   /* If the definition of this type lives in .debug_types, read that type.
6959      Don't follow DW_AT_specification though, that will take us back up
6960      the chain and we want to go down.  */
6961   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6962   if (attr)
6963     {
6964       struct dwarf2_cu *type_cu = cu;
6965       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6966
6967       type = read_type_die (type_die, type_cu);
6968
6969       /* TYPE_CU may not be the same as CU.
6970          Ensure TYPE is recorded in CU's type_hash table.  */
6971       return set_die_type (die, type, cu);
6972     }
6973
6974   type = alloc_type (objfile);
6975
6976   TYPE_CODE (type) = TYPE_CODE_ENUM;
6977   name = dwarf2_full_name (NULL, die, cu);
6978   if (name != NULL)
6979     TYPE_TAG_NAME (type) = (char *) name;
6980
6981   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6982   if (attr)
6983     {
6984       TYPE_LENGTH (type) = DW_UNSND (attr);
6985     }
6986   else
6987     {
6988       TYPE_LENGTH (type) = 0;
6989     }
6990
6991   /* The enumeration DIE can be incomplete.  In Ada, any type can be
6992      declared as private in the package spec, and then defined only
6993      inside the package body.  Such types are known as Taft Amendment
6994      Types.  When another package uses such a type, an incomplete DIE
6995      may be generated by the compiler.  */
6996   if (die_is_declaration (die, cu))
6997     TYPE_STUB (type) = 1;
6998
6999   return set_die_type (die, type, cu);
7000 }
7001
7002 /* Given a pointer to a die which begins an enumeration, process all
7003    the dies that define the members of the enumeration, and create the
7004    symbol for the enumeration type.
7005
7006    NOTE: We reverse the order of the element list.  */
7007
7008 static void
7009 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7010 {
7011   struct type *this_type;
7012
7013   this_type = get_die_type (die, cu);
7014   if (this_type == NULL)
7015     this_type = read_enumeration_type (die, cu);
7016
7017   if (die->child != NULL)
7018     {
7019       struct die_info *child_die;
7020       struct symbol *sym;
7021       struct field *fields = NULL;
7022       int num_fields = 0;
7023       int unsigned_enum = 1;
7024       char *name;
7025
7026       child_die = die->child;
7027       while (child_die && child_die->tag)
7028         {
7029           if (child_die->tag != DW_TAG_enumerator)
7030             {
7031               process_die (child_die, cu);
7032             }
7033           else
7034             {
7035               name = dwarf2_name (child_die, cu);
7036               if (name)
7037                 {
7038                   sym = new_symbol (child_die, this_type, cu);
7039                   if (SYMBOL_VALUE (sym) < 0)
7040                     unsigned_enum = 0;
7041
7042                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7043                     {
7044                       fields = (struct field *)
7045                         xrealloc (fields,
7046                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
7047                                   * sizeof (struct field));
7048                     }
7049
7050                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7051                   FIELD_TYPE (fields[num_fields]) = NULL;
7052                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7053                   FIELD_BITSIZE (fields[num_fields]) = 0;
7054
7055                   num_fields++;
7056                 }
7057             }
7058
7059           child_die = sibling_die (child_die);
7060         }
7061
7062       if (num_fields)
7063         {
7064           TYPE_NFIELDS (this_type) = num_fields;
7065           TYPE_FIELDS (this_type) = (struct field *)
7066             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7067           memcpy (TYPE_FIELDS (this_type), fields,
7068                   sizeof (struct field) * num_fields);
7069           xfree (fields);
7070         }
7071       if (unsigned_enum)
7072         TYPE_UNSIGNED (this_type) = 1;
7073     }
7074
7075   new_symbol (die, this_type, cu);
7076 }
7077
7078 /* Extract all information from a DW_TAG_array_type DIE and put it in
7079    the DIE's type field.  For now, this only handles one dimensional
7080    arrays.  */
7081
7082 static struct type *
7083 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7084 {
7085   struct objfile *objfile = cu->objfile;
7086   struct die_info *child_die;
7087   struct type *type;
7088   struct type *element_type, *range_type, *index_type;
7089   struct type **range_types = NULL;
7090   struct attribute *attr;
7091   int ndim = 0;
7092   struct cleanup *back_to;
7093   char *name;
7094
7095   element_type = die_type (die, cu);
7096
7097   /* The die_type call above may have already set the type for this DIE.  */
7098   type = get_die_type (die, cu);
7099   if (type)
7100     return type;
7101
7102   /* Irix 6.2 native cc creates array types without children for
7103      arrays with unspecified length.  */
7104   if (die->child == NULL)
7105     {
7106       index_type = objfile_type (objfile)->builtin_int;
7107       range_type = create_range_type (NULL, index_type, 0, -1);
7108       type = create_array_type (NULL, element_type, range_type);
7109       return set_die_type (die, type, cu);
7110     }
7111
7112   back_to = make_cleanup (null_cleanup, NULL);
7113   child_die = die->child;
7114   while (child_die && child_die->tag)
7115     {
7116       if (child_die->tag == DW_TAG_subrange_type)
7117         {
7118           struct type *child_type = read_type_die (child_die, cu);
7119
7120           if (child_type != NULL)
7121             {
7122               /* The range type was succesfully read. Save it for
7123                  the array type creation.  */
7124               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7125                 {
7126                   range_types = (struct type **)
7127                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7128                               * sizeof (struct type *));
7129                   if (ndim == 0)
7130                     make_cleanup (free_current_contents, &range_types);
7131                 }
7132               range_types[ndim++] = child_type;
7133             }
7134         }
7135       child_die = sibling_die (child_die);
7136     }
7137
7138   /* Dwarf2 dimensions are output from left to right, create the
7139      necessary array types in backwards order.  */
7140
7141   type = element_type;
7142
7143   if (read_array_order (die, cu) == DW_ORD_col_major)
7144     {
7145       int i = 0;
7146
7147       while (i < ndim)
7148         type = create_array_type (NULL, type, range_types[i++]);
7149     }
7150   else
7151     {
7152       while (ndim-- > 0)
7153         type = create_array_type (NULL, type, range_types[ndim]);
7154     }
7155
7156   /* Understand Dwarf2 support for vector types (like they occur on
7157      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
7158      array type.  This is not part of the Dwarf2/3 standard yet, but a
7159      custom vendor extension.  The main difference between a regular
7160      array and the vector variant is that vectors are passed by value
7161      to functions.  */
7162   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7163   if (attr)
7164     make_vector_type (type);
7165
7166   name = dwarf2_name (die, cu);
7167   if (name)
7168     TYPE_NAME (type) = name;
7169
7170   /* Install the type in the die. */
7171   set_die_type (die, type, cu);
7172
7173   /* set_die_type should be already done.  */
7174   set_descriptive_type (type, die, cu);
7175
7176   do_cleanups (back_to);
7177
7178   return type;
7179 }
7180
7181 static enum dwarf_array_dim_ordering
7182 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7183 {
7184   struct attribute *attr;
7185
7186   attr = dwarf2_attr (die, DW_AT_ordering, cu);
7187
7188   if (attr) return DW_SND (attr);
7189
7190   /*
7191     GNU F77 is a special case, as at 08/2004 array type info is the
7192     opposite order to the dwarf2 specification, but data is still
7193     laid out as per normal fortran.
7194
7195     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7196     version checking.
7197   */
7198
7199   if (cu->language == language_fortran
7200       && cu->producer && strstr (cu->producer, "GNU F77"))
7201     {
7202       return DW_ORD_row_major;
7203     }
7204
7205   switch (cu->language_defn->la_array_ordering)
7206     {
7207     case array_column_major:
7208       return DW_ORD_col_major;
7209     case array_row_major:
7210     default:
7211       return DW_ORD_row_major;
7212     };
7213 }
7214
7215 /* Extract all information from a DW_TAG_set_type DIE and put it in
7216    the DIE's type field. */
7217
7218 static struct type *
7219 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7220 {
7221   struct type *domain_type, *set_type;
7222   struct attribute *attr;
7223
7224   domain_type = die_type (die, cu);
7225
7226   /* The die_type call above may have already set the type for this DIE.  */
7227   set_type = get_die_type (die, cu);
7228   if (set_type)
7229     return set_type;
7230
7231   set_type = create_set_type (NULL, domain_type);
7232
7233   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7234   if (attr)
7235     TYPE_LENGTH (set_type) = DW_UNSND (attr);
7236
7237   return set_die_type (die, set_type, cu);
7238 }
7239
7240 /* First cut: install each common block member as a global variable.  */
7241
7242 static void
7243 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7244 {
7245   struct die_info *child_die;
7246   struct attribute *attr;
7247   struct symbol *sym;
7248   CORE_ADDR base = (CORE_ADDR) 0;
7249
7250   attr = dwarf2_attr (die, DW_AT_location, cu);
7251   if (attr)
7252     {
7253       /* Support the .debug_loc offsets */
7254       if (attr_form_is_block (attr))
7255         {
7256           base = decode_locdesc (DW_BLOCK (attr), cu);
7257         }
7258       else if (attr_form_is_section_offset (attr))
7259         {
7260           dwarf2_complex_location_expr_complaint ();
7261         }
7262       else
7263         {
7264           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7265                                                  "common block member");
7266         }
7267     }
7268   if (die->child != NULL)
7269     {
7270       child_die = die->child;
7271       while (child_die && child_die->tag)
7272         {
7273           sym = new_symbol (child_die, NULL, cu);
7274           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7275           if (attr)
7276             {
7277               CORE_ADDR byte_offset = 0;
7278
7279               if (attr_form_is_section_offset (attr))
7280                 dwarf2_complex_location_expr_complaint ();
7281               else if (attr_form_is_constant (attr))
7282                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7283               else if (attr_form_is_block (attr))
7284                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7285               else
7286                 dwarf2_complex_location_expr_complaint ();
7287
7288               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7289               add_symbol_to_list (sym, &global_symbols);
7290             }
7291           child_die = sibling_die (child_die);
7292         }
7293     }
7294 }
7295
7296 /* Create a type for a C++ namespace.  */
7297
7298 static struct type *
7299 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7300 {
7301   struct objfile *objfile = cu->objfile;
7302   const char *previous_prefix, *name;
7303   int is_anonymous;
7304   struct type *type;
7305
7306   /* For extensions, reuse the type of the original namespace.  */
7307   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7308     {
7309       struct die_info *ext_die;
7310       struct dwarf2_cu *ext_cu = cu;
7311
7312       ext_die = dwarf2_extension (die, &ext_cu);
7313       type = read_type_die (ext_die, ext_cu);
7314
7315       /* EXT_CU may not be the same as CU.
7316          Ensure TYPE is recorded in CU's type_hash table.  */
7317       return set_die_type (die, type, cu);
7318     }
7319
7320   name = namespace_name (die, &is_anonymous, cu);
7321
7322   /* Now build the name of the current namespace.  */
7323
7324   previous_prefix = determine_prefix (die, cu);
7325   if (previous_prefix[0] != '\0')
7326     name = typename_concat (&objfile->objfile_obstack,
7327                             previous_prefix, name, 0, cu);
7328
7329   /* Create the type.  */
7330   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7331                     objfile);
7332   TYPE_NAME (type) = (char *) name;
7333   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7334
7335   return set_die_type (die, type, cu);
7336 }
7337
7338 /* Read a C++ namespace.  */
7339
7340 static void
7341 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7342 {
7343   struct objfile *objfile = cu->objfile;
7344   const char *name;
7345   int is_anonymous;
7346
7347   /* Add a symbol associated to this if we haven't seen the namespace
7348      before.  Also, add a using directive if it's an anonymous
7349      namespace.  */
7350
7351   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7352     {
7353       struct type *type;
7354
7355       type = read_type_die (die, cu);
7356       new_symbol (die, type, cu);
7357
7358       name = namespace_name (die, &is_anonymous, cu);
7359       if (is_anonymous)
7360         {
7361           const char *previous_prefix = determine_prefix (die, cu);
7362
7363           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7364                                   NULL, &objfile->objfile_obstack);
7365         }
7366     }
7367
7368   if (die->child != NULL)
7369     {
7370       struct die_info *child_die = die->child;
7371
7372       while (child_die && child_die->tag)
7373         {
7374           process_die (child_die, cu);
7375           child_die = sibling_die (child_die);
7376         }
7377     }
7378 }
7379
7380 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7381    imported module.  Still we need that type as local Fortran "use ... only"
7382    declaration imports depend on the created type in determine_prefix.  */
7383
7384 static struct type *
7385 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7386 {
7387   struct objfile *objfile = cu->objfile;
7388   char *module_name;
7389   struct type *type;
7390
7391   module_name = dwarf2_name (die, cu);
7392   if (!module_name)
7393     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7394                die->offset);
7395   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7396
7397   /* determine_prefix uses TYPE_TAG_NAME.  */
7398   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7399
7400   return set_die_type (die, type, cu);
7401 }
7402
7403 /* Read a Fortran module.  */
7404
7405 static void
7406 read_module (struct die_info *die, struct dwarf2_cu *cu)
7407 {
7408   struct die_info *child_die = die->child;
7409
7410   while (child_die && child_die->tag)
7411     {
7412       process_die (child_die, cu);
7413       child_die = sibling_die (child_die);
7414     }
7415 }
7416
7417 /* Return the name of the namespace represented by DIE.  Set
7418    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7419    namespace.  */
7420
7421 static const char *
7422 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7423 {
7424   struct die_info *current_die;
7425   const char *name = NULL;
7426
7427   /* Loop through the extensions until we find a name.  */
7428
7429   for (current_die = die;
7430        current_die != NULL;
7431        current_die = dwarf2_extension (die, &cu))
7432     {
7433       name = dwarf2_name (current_die, cu);
7434       if (name != NULL)
7435         break;
7436     }
7437
7438   /* Is it an anonymous namespace?  */
7439
7440   *is_anonymous = (name == NULL);
7441   if (*is_anonymous)
7442     name = "(anonymous namespace)";
7443
7444   return name;
7445 }
7446
7447 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7448    the user defined type vector.  */
7449
7450 static struct type *
7451 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7452 {
7453   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7454   struct comp_unit_head *cu_header = &cu->header;
7455   struct type *type;
7456   struct attribute *attr_byte_size;
7457   struct attribute *attr_address_class;
7458   int byte_size, addr_class;
7459   struct type *target_type;
7460
7461   target_type = die_type (die, cu);
7462
7463   /* The die_type call above may have already set the type for this DIE.  */
7464   type = get_die_type (die, cu);
7465   if (type)
7466     return type;
7467
7468   type = lookup_pointer_type (target_type);
7469
7470   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7471   if (attr_byte_size)
7472     byte_size = DW_UNSND (attr_byte_size);
7473   else
7474     byte_size = cu_header->addr_size;
7475
7476   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7477   if (attr_address_class)
7478     addr_class = DW_UNSND (attr_address_class);
7479   else
7480     addr_class = DW_ADDR_none;
7481
7482   /* If the pointer size or address class is different than the
7483      default, create a type variant marked as such and set the
7484      length accordingly.  */
7485   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7486     {
7487       if (gdbarch_address_class_type_flags_p (gdbarch))
7488         {
7489           int type_flags;
7490
7491           type_flags = gdbarch_address_class_type_flags
7492                          (gdbarch, byte_size, addr_class);
7493           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7494                       == 0);
7495           type = make_type_with_address_space (type, type_flags);
7496         }
7497       else if (TYPE_LENGTH (type) != byte_size)
7498         {
7499           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7500         }
7501       else
7502         {
7503           /* Should we also complain about unhandled address classes?  */
7504         }
7505     }
7506
7507   TYPE_LENGTH (type) = byte_size;
7508   return set_die_type (die, type, cu);
7509 }
7510
7511 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7512    the user defined type vector.  */
7513
7514 static struct type *
7515 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7516 {
7517   struct type *type;
7518   struct type *to_type;
7519   struct type *domain;
7520
7521   to_type = die_type (die, cu);
7522   domain = die_containing_type (die, cu);
7523
7524   /* The calls above may have already set the type for this DIE.  */
7525   type = get_die_type (die, cu);
7526   if (type)
7527     return type;
7528
7529   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7530     type = lookup_methodptr_type (to_type);
7531   else
7532     type = lookup_memberptr_type (to_type, domain);
7533
7534   return set_die_type (die, type, cu);
7535 }
7536
7537 /* Extract all information from a DW_TAG_reference_type DIE and add to
7538    the user defined type vector.  */
7539
7540 static struct type *
7541 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7542 {
7543   struct comp_unit_head *cu_header = &cu->header;
7544   struct type *type, *target_type;
7545   struct attribute *attr;
7546
7547   target_type = die_type (die, cu);
7548
7549   /* The die_type call above may have already set the type for this DIE.  */
7550   type = get_die_type (die, cu);
7551   if (type)
7552     return type;
7553
7554   type = lookup_reference_type (target_type);
7555   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7556   if (attr)
7557     {
7558       TYPE_LENGTH (type) = DW_UNSND (attr);
7559     }
7560   else
7561     {
7562       TYPE_LENGTH (type) = cu_header->addr_size;
7563     }
7564   return set_die_type (die, type, cu);
7565 }
7566
7567 static struct type *
7568 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7569 {
7570   struct type *base_type, *cv_type;
7571
7572   base_type = die_type (die, cu);
7573
7574   /* The die_type call above may have already set the type for this DIE.  */
7575   cv_type = get_die_type (die, cu);
7576   if (cv_type)
7577     return cv_type;
7578
7579   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7580   return set_die_type (die, cv_type, cu);
7581 }
7582
7583 static struct type *
7584 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7585 {
7586   struct type *base_type, *cv_type;
7587
7588   base_type = die_type (die, cu);
7589
7590   /* The die_type call above may have already set the type for this DIE.  */
7591   cv_type = get_die_type (die, cu);
7592   if (cv_type)
7593     return cv_type;
7594
7595   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7596   return set_die_type (die, cv_type, cu);
7597 }
7598
7599 /* Extract all information from a DW_TAG_string_type DIE and add to
7600    the user defined type vector.  It isn't really a user defined type,
7601    but it behaves like one, with other DIE's using an AT_user_def_type
7602    attribute to reference it.  */
7603
7604 static struct type *
7605 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7606 {
7607   struct objfile *objfile = cu->objfile;
7608   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7609   struct type *type, *range_type, *index_type, *char_type;
7610   struct attribute *attr;
7611   unsigned int length;
7612
7613   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7614   if (attr)
7615     {
7616       length = DW_UNSND (attr);
7617     }
7618   else
7619     {
7620       /* check for the DW_AT_byte_size attribute */
7621       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7622       if (attr)
7623         {
7624           length = DW_UNSND (attr);
7625         }
7626       else
7627         {
7628           length = 1;
7629         }
7630     }
7631
7632   index_type = objfile_type (objfile)->builtin_int;
7633   range_type = create_range_type (NULL, index_type, 1, length);
7634   char_type = language_string_char_type (cu->language_defn, gdbarch);
7635   type = create_string_type (NULL, char_type, range_type);
7636
7637   return set_die_type (die, type, cu);
7638 }
7639
7640 /* Handle DIES due to C code like:
7641
7642    struct foo
7643    {
7644    int (*funcp)(int a, long l);
7645    int b;
7646    };
7647
7648    ('funcp' generates a DW_TAG_subroutine_type DIE)
7649  */
7650
7651 static struct type *
7652 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7653 {
7654   struct type *type;            /* Type that this function returns */
7655   struct type *ftype;           /* Function that returns above type */
7656   struct attribute *attr;
7657
7658   type = die_type (die, cu);
7659
7660   /* The die_type call above may have already set the type for this DIE.  */
7661   ftype = get_die_type (die, cu);
7662   if (ftype)
7663     return ftype;
7664
7665   ftype = lookup_function_type (type);
7666
7667   /* All functions in C++, Pascal and Java have prototypes.  */
7668   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7669   if ((attr && (DW_UNSND (attr) != 0))
7670       || cu->language == language_cplus
7671       || cu->language == language_java
7672       || cu->language == language_pascal)
7673     TYPE_PROTOTYPED (ftype) = 1;
7674   else if (producer_is_realview (cu->producer))
7675     /* RealView does not emit DW_AT_prototyped.  We can not
7676        distinguish prototyped and unprototyped functions; default to
7677        prototyped, since that is more common in modern code (and
7678        RealView warns about unprototyped functions).  */
7679     TYPE_PROTOTYPED (ftype) = 1;
7680
7681   /* Store the calling convention in the type if it's available in
7682      the subroutine die.  Otherwise set the calling convention to
7683      the default value DW_CC_normal.  */
7684   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7685   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7686
7687   /* We need to add the subroutine type to the die immediately so
7688      we don't infinitely recurse when dealing with parameters
7689      declared as the same subroutine type. */
7690   set_die_type (die, ftype, cu);
7691
7692   if (die->child != NULL)
7693     {
7694       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7695       struct die_info *child_die;
7696       int nparams, iparams;
7697
7698       /* Count the number of parameters.
7699          FIXME: GDB currently ignores vararg functions, but knows about
7700          vararg member functions.  */
7701       nparams = 0;
7702       child_die = die->child;
7703       while (child_die && child_die->tag)
7704         {
7705           if (child_die->tag == DW_TAG_formal_parameter)
7706             nparams++;
7707           else if (child_die->tag == DW_TAG_unspecified_parameters)
7708             TYPE_VARARGS (ftype) = 1;
7709           child_die = sibling_die (child_die);
7710         }
7711
7712       /* Allocate storage for parameters and fill them in.  */
7713       TYPE_NFIELDS (ftype) = nparams;
7714       TYPE_FIELDS (ftype) = (struct field *)
7715         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7716
7717       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7718          even if we error out during the parameters reading below.  */
7719       for (iparams = 0; iparams < nparams; iparams++)
7720         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7721
7722       iparams = 0;
7723       child_die = die->child;
7724       while (child_die && child_die->tag)
7725         {
7726           if (child_die->tag == DW_TAG_formal_parameter)
7727             {
7728               struct type *arg_type;
7729
7730               /* DWARF version 2 has no clean way to discern C++
7731                  static and non-static member functions.  G++ helps
7732                  GDB by marking the first parameter for non-static
7733                  member functions (which is the this pointer) as
7734                  artificial.  We pass this information to
7735                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7736
7737                  DWARF version 3 added DW_AT_object_pointer, which GCC
7738                  4.5 does not yet generate.  */
7739               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7740               if (attr)
7741                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7742               else
7743                 {
7744                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7745
7746                   /* GCC/43521: In java, the formal parameter
7747                      "this" is sometimes not marked with DW_AT_artificial.  */
7748                   if (cu->language == language_java)
7749                     {
7750                       const char *name = dwarf2_name (child_die, cu);
7751
7752                       if (name && !strcmp (name, "this"))
7753                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7754                     }
7755                 }
7756               arg_type = die_type (child_die, cu);
7757
7758               /* RealView does not mark THIS as const, which the testsuite
7759                  expects.  GCC marks THIS as const in method definitions,
7760                  but not in the class specifications (GCC PR 43053).  */
7761               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7762                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7763                 {
7764                   int is_this = 0;
7765                   struct dwarf2_cu *arg_cu = cu;
7766                   const char *name = dwarf2_name (child_die, cu);
7767
7768                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7769                   if (attr)
7770                     {
7771                       /* If the compiler emits this, use it.  */
7772                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
7773                         is_this = 1;
7774                     }
7775                   else if (name && strcmp (name, "this") == 0)
7776                     /* Function definitions will have the argument names.  */
7777                     is_this = 1;
7778                   else if (name == NULL && iparams == 0)
7779                     /* Declarations may not have the names, so like
7780                        elsewhere in GDB, assume an artificial first
7781                        argument is "this".  */
7782                     is_this = 1;
7783
7784                   if (is_this)
7785                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7786                                              arg_type, 0);
7787                 }
7788
7789               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7790               iparams++;
7791             }
7792           child_die = sibling_die (child_die);
7793         }
7794     }
7795
7796   return ftype;
7797 }
7798
7799 static struct type *
7800 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7801 {
7802   struct objfile *objfile = cu->objfile;
7803   const char *name = NULL;
7804   struct type *this_type;
7805
7806   name = dwarf2_full_name (NULL, die, cu);
7807   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7808                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7809   TYPE_NAME (this_type) = (char *) name;
7810   set_die_type (die, this_type, cu);
7811   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7812   return this_type;
7813 }
7814
7815 /* Find a representation of a given base type and install
7816    it in the TYPE field of the die.  */
7817
7818 static struct type *
7819 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7820 {
7821   struct objfile *objfile = cu->objfile;
7822   struct type *type;
7823   struct attribute *attr;
7824   int encoding = 0, size = 0;
7825   char *name;
7826   enum type_code code = TYPE_CODE_INT;
7827   int type_flags = 0;
7828   struct type *target_type = NULL;
7829
7830   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7831   if (attr)
7832     {
7833       encoding = DW_UNSND (attr);
7834     }
7835   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7836   if (attr)
7837     {
7838       size = DW_UNSND (attr);
7839     }
7840   name = dwarf2_name (die, cu);
7841   if (!name)
7842     {
7843       complaint (&symfile_complaints,
7844                  _("DW_AT_name missing from DW_TAG_base_type"));
7845     }
7846
7847   switch (encoding)
7848     {
7849       case DW_ATE_address:
7850         /* Turn DW_ATE_address into a void * pointer.  */
7851         code = TYPE_CODE_PTR;
7852         type_flags |= TYPE_FLAG_UNSIGNED;
7853         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7854         break;
7855       case DW_ATE_boolean:
7856         code = TYPE_CODE_BOOL;
7857         type_flags |= TYPE_FLAG_UNSIGNED;
7858         break;
7859       case DW_ATE_complex_float:
7860         code = TYPE_CODE_COMPLEX;
7861         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7862         break;
7863       case DW_ATE_decimal_float:
7864         code = TYPE_CODE_DECFLOAT;
7865         break;
7866       case DW_ATE_float:
7867         code = TYPE_CODE_FLT;
7868         break;
7869       case DW_ATE_signed:
7870         break;
7871       case DW_ATE_unsigned:
7872         type_flags |= TYPE_FLAG_UNSIGNED;
7873         break;
7874       case DW_ATE_signed_char:
7875         if (cu->language == language_ada || cu->language == language_m2
7876             || cu->language == language_pascal)
7877           code = TYPE_CODE_CHAR;
7878         break;
7879       case DW_ATE_unsigned_char:
7880         if (cu->language == language_ada || cu->language == language_m2
7881             || cu->language == language_pascal)
7882           code = TYPE_CODE_CHAR;
7883         type_flags |= TYPE_FLAG_UNSIGNED;
7884         break;
7885       case DW_ATE_UTF:
7886         /* We just treat this as an integer and then recognize the
7887            type by name elsewhere.  */
7888         break;
7889
7890       default:
7891         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7892                    dwarf_type_encoding_name (encoding));
7893         break;
7894     }
7895
7896   type = init_type (code, size, type_flags, NULL, objfile);
7897   TYPE_NAME (type) = name;
7898   TYPE_TARGET_TYPE (type) = target_type;
7899
7900   if (name && strcmp (name, "char") == 0)
7901     TYPE_NOSIGN (type) = 1;
7902
7903   return set_die_type (die, type, cu);
7904 }
7905
7906 /* Read the given DW_AT_subrange DIE.  */
7907
7908 static struct type *
7909 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7910 {
7911   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7912   struct type *base_type;
7913   struct type *range_type;
7914   struct attribute *attr;
7915   LONGEST low = 0;
7916   LONGEST high = -1;
7917   char *name;
7918   LONGEST negative_mask;
7919
7920   base_type = die_type (die, cu);
7921   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7922   check_typedef (base_type);
7923
7924   /* The die_type call above may have already set the type for this DIE.  */
7925   range_type = get_die_type (die, cu);
7926   if (range_type)
7927     return range_type;
7928
7929   if (cu->language == language_fortran)
7930     {
7931       /* FORTRAN implies a lower bound of 1, if not given.  */
7932       low = 1;
7933     }
7934
7935   /* FIXME: For variable sized arrays either of these could be
7936      a variable rather than a constant value.  We'll allow it,
7937      but we don't know how to handle it.  */
7938   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7939   if (attr)
7940     low = dwarf2_get_attr_constant_value (attr, 0);
7941
7942   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7943   if (attr)
7944     {
7945       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7946         {
7947           /* GCC encodes arrays with unspecified or dynamic length
7948              with a DW_FORM_block1 attribute or a reference attribute.
7949              FIXME: GDB does not yet know how to handle dynamic
7950              arrays properly, treat them as arrays with unspecified
7951              length for now.
7952
7953              FIXME: jimb/2003-09-22: GDB does not really know
7954              how to handle arrays of unspecified length
7955              either; we just represent them as zero-length
7956              arrays.  Choose an appropriate upper bound given
7957              the lower bound we've computed above.  */
7958           high = low - 1;
7959         }
7960       else
7961         high = dwarf2_get_attr_constant_value (attr, 1);
7962     }
7963   else
7964     {
7965       attr = dwarf2_attr (die, DW_AT_count, cu);
7966       if (attr)
7967         {
7968           int count = dwarf2_get_attr_constant_value (attr, 1);
7969           high = low + count - 1;
7970         }
7971     }
7972
7973   /* Dwarf-2 specifications explicitly allows to create subrange types
7974      without specifying a base type.
7975      In that case, the base type must be set to the type of
7976      the lower bound, upper bound or count, in that order, if any of these
7977      three attributes references an object that has a type.
7978      If no base type is found, the Dwarf-2 specifications say that
7979      a signed integer type of size equal to the size of an address should
7980      be used.
7981      For the following C code: `extern char gdb_int [];'
7982      GCC produces an empty range DIE.
7983      FIXME: muller/2010-05-28: Possible references to object for low bound,
7984      high bound or count are not yet handled by this code.
7985   */
7986   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7987     {
7988       struct objfile *objfile = cu->objfile;
7989       struct gdbarch *gdbarch = get_objfile_arch (objfile);
7990       int addr_size = gdbarch_addr_bit (gdbarch) /8;
7991       struct type *int_type = objfile_type (objfile)->builtin_int;
7992
7993       /* Test "int", "long int", and "long long int" objfile types,
7994          and select the first one having a size above or equal to the
7995          architecture address size.  */
7996       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7997         base_type = int_type;
7998       else
7999         {
8000           int_type = objfile_type (objfile)->builtin_long;
8001           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8002             base_type = int_type;
8003           else
8004             {
8005               int_type = objfile_type (objfile)->builtin_long_long;
8006               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8007                 base_type = int_type;
8008             }
8009         }
8010     }
8011
8012   negative_mask =
8013     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8014   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8015     low |= negative_mask;
8016   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8017     high |= negative_mask;
8018
8019   range_type = create_range_type (NULL, base_type, low, high);
8020
8021   /* Mark arrays with dynamic length at least as an array of unspecified
8022      length.  GDB could check the boundary but before it gets implemented at
8023      least allow accessing the array elements.  */
8024   if (attr && attr->form == DW_FORM_block1)
8025     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8026
8027   name = dwarf2_name (die, cu);
8028   if (name)
8029     TYPE_NAME (range_type) = name;
8030
8031   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8032   if (attr)
8033     TYPE_LENGTH (range_type) = DW_UNSND (attr);
8034
8035   set_die_type (die, range_type, cu);
8036
8037   /* set_die_type should be already done.  */
8038   set_descriptive_type (range_type, die, cu);
8039
8040   return range_type;
8041 }
8042
8043 static struct type *
8044 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8045 {
8046   struct type *type;
8047
8048   /* For now, we only support the C meaning of an unspecified type: void.  */
8049
8050   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8051   TYPE_NAME (type) = dwarf2_name (die, cu);
8052
8053   return set_die_type (die, type, cu);
8054 }
8055
8056 /* Trivial hash function for die_info: the hash value of a DIE
8057    is its offset in .debug_info for this objfile.  */
8058
8059 static hashval_t
8060 die_hash (const void *item)
8061 {
8062   const struct die_info *die = item;
8063
8064   return die->offset;
8065 }
8066
8067 /* Trivial comparison function for die_info structures: two DIEs
8068    are equal if they have the same offset.  */
8069
8070 static int
8071 die_eq (const void *item_lhs, const void *item_rhs)
8072 {
8073   const struct die_info *die_lhs = item_lhs;
8074   const struct die_info *die_rhs = item_rhs;
8075
8076   return die_lhs->offset == die_rhs->offset;
8077 }
8078
8079 /* Read a whole compilation unit into a linked list of dies.  */
8080
8081 static struct die_info *
8082 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8083 {
8084   struct die_reader_specs reader_specs;
8085   int read_abbrevs = 0;
8086   struct cleanup *back_to = NULL;
8087   struct die_info *die;
8088
8089   if (cu->dwarf2_abbrevs == NULL)
8090     {
8091       dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8092       back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8093       read_abbrevs = 1;
8094     }
8095
8096   gdb_assert (cu->die_hash == NULL);
8097   cu->die_hash
8098     = htab_create_alloc_ex (cu->header.length / 12,
8099                             die_hash,
8100                             die_eq,
8101                             NULL,
8102                             &cu->comp_unit_obstack,
8103                             hashtab_obstack_allocate,
8104                             dummy_obstack_deallocate);
8105
8106   init_cu_die_reader (&reader_specs, cu);
8107
8108   die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8109
8110   if (read_abbrevs)
8111     do_cleanups (back_to);
8112
8113   return die;
8114 }
8115
8116 /* Main entry point for reading a DIE and all children.
8117    Read the DIE and dump it if requested.  */
8118
8119 static struct die_info *
8120 read_die_and_children (const struct die_reader_specs *reader,
8121                        gdb_byte *info_ptr,
8122                        gdb_byte **new_info_ptr,
8123                        struct die_info *parent)
8124 {
8125   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8126                                                      new_info_ptr, parent);
8127
8128   if (dwarf2_die_debug)
8129     {
8130       fprintf_unfiltered (gdb_stdlog,
8131                           "\nRead die from %s of %s:\n",
8132                           reader->buffer == dwarf2_per_objfile->info.buffer
8133                           ? ".debug_info"
8134                           : reader->buffer == dwarf2_per_objfile->types.buffer
8135                           ? ".debug_types"
8136                           : "unknown section",
8137                           reader->abfd->filename);
8138       dump_die (result, dwarf2_die_debug);
8139     }
8140
8141   return result;
8142 }
8143
8144 /* Read a single die and all its descendents.  Set the die's sibling
8145    field to NULL; set other fields in the die correctly, and set all
8146    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
8147    location of the info_ptr after reading all of those dies.  PARENT
8148    is the parent of the die in question.  */
8149
8150 static struct die_info *
8151 read_die_and_children_1 (const struct die_reader_specs *reader,
8152                          gdb_byte *info_ptr,
8153                          gdb_byte **new_info_ptr,
8154                          struct die_info *parent)
8155 {
8156   struct die_info *die;
8157   gdb_byte *cur_ptr;
8158   int has_children;
8159
8160   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8161   if (die == NULL)
8162     {
8163       *new_info_ptr = cur_ptr;
8164       return NULL;
8165     }
8166   store_in_ref_table (die, reader->cu);
8167
8168   if (has_children)
8169     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8170   else
8171     {
8172       die->child = NULL;
8173       *new_info_ptr = cur_ptr;
8174     }
8175
8176   die->sibling = NULL;
8177   die->parent = parent;
8178   return die;
8179 }
8180
8181 /* Read a die, all of its descendents, and all of its siblings; set
8182    all of the fields of all of the dies correctly.  Arguments are as
8183    in read_die_and_children.  */
8184
8185 static struct die_info *
8186 read_die_and_siblings (const struct die_reader_specs *reader,
8187                        gdb_byte *info_ptr,
8188                        gdb_byte **new_info_ptr,
8189                        struct die_info *parent)
8190 {
8191   struct die_info *first_die, *last_sibling;
8192   gdb_byte *cur_ptr;
8193
8194   cur_ptr = info_ptr;
8195   first_die = last_sibling = NULL;
8196
8197   while (1)
8198     {
8199       struct die_info *die
8200         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8201
8202       if (die == NULL)
8203         {
8204           *new_info_ptr = cur_ptr;
8205           return first_die;
8206         }
8207
8208       if (!first_die)
8209         first_die = die;
8210       else
8211         last_sibling->sibling = die;
8212
8213       last_sibling = die;
8214     }
8215 }
8216
8217 /* Read the die from the .debug_info section buffer.  Set DIEP to
8218    point to a newly allocated die with its information, except for its
8219    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
8220    whether the die has children or not.  */
8221
8222 static gdb_byte *
8223 read_full_die (const struct die_reader_specs *reader,
8224                struct die_info **diep, gdb_byte *info_ptr,
8225                int *has_children)
8226 {
8227   unsigned int abbrev_number, bytes_read, i, offset;
8228   struct abbrev_info *abbrev;
8229   struct die_info *die;
8230   struct dwarf2_cu *cu = reader->cu;
8231   bfd *abfd = reader->abfd;
8232
8233   offset = info_ptr - reader->buffer;
8234   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8235   info_ptr += bytes_read;
8236   if (!abbrev_number)
8237     {
8238       *diep = NULL;
8239       *has_children = 0;
8240       return info_ptr;
8241     }
8242
8243   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8244   if (!abbrev)
8245     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8246            abbrev_number,
8247            bfd_get_filename (abfd));
8248
8249   die = dwarf_alloc_die (cu, abbrev->num_attrs);
8250   die->offset = offset;
8251   die->tag = abbrev->tag;
8252   die->abbrev = abbrev_number;
8253
8254   die->num_attrs = abbrev->num_attrs;
8255
8256   for (i = 0; i < abbrev->num_attrs; ++i)
8257     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8258                                abfd, info_ptr, cu);
8259
8260   *diep = die;
8261   *has_children = abbrev->has_children;
8262   return info_ptr;
8263 }
8264
8265 /* In DWARF version 2, the description of the debugging information is
8266    stored in a separate .debug_abbrev section.  Before we read any
8267    dies from a section we read in all abbreviations and install them
8268    in a hash table.  This function also sets flags in CU describing
8269    the data found in the abbrev table.  */
8270
8271 static void
8272 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8273 {
8274   struct comp_unit_head *cu_header = &cu->header;
8275   gdb_byte *abbrev_ptr;
8276   struct abbrev_info *cur_abbrev;
8277   unsigned int abbrev_number, bytes_read, abbrev_name;
8278   unsigned int abbrev_form, hash_number;
8279   struct attr_abbrev *cur_attrs;
8280   unsigned int allocated_attrs;
8281
8282   /* Initialize dwarf2 abbrevs */
8283   obstack_init (&cu->abbrev_obstack);
8284   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8285                                       (ABBREV_HASH_SIZE
8286                                        * sizeof (struct abbrev_info *)));
8287   memset (cu->dwarf2_abbrevs, 0,
8288           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8289
8290   dwarf2_read_section (dwarf2_per_objfile->objfile,
8291                        &dwarf2_per_objfile->abbrev);
8292   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8293   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8294   abbrev_ptr += bytes_read;
8295
8296   allocated_attrs = ATTR_ALLOC_CHUNK;
8297   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8298
8299   /* loop until we reach an abbrev number of 0 */
8300   while (abbrev_number)
8301     {
8302       cur_abbrev = dwarf_alloc_abbrev (cu);
8303
8304       /* read in abbrev header */
8305       cur_abbrev->number = abbrev_number;
8306       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8307       abbrev_ptr += bytes_read;
8308       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8309       abbrev_ptr += 1;
8310
8311       if (cur_abbrev->tag == DW_TAG_namespace)
8312         cu->has_namespace_info = 1;
8313
8314       /* now read in declarations */
8315       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8316       abbrev_ptr += bytes_read;
8317       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8318       abbrev_ptr += bytes_read;
8319       while (abbrev_name)
8320         {
8321           if (cur_abbrev->num_attrs == allocated_attrs)
8322             {
8323               allocated_attrs += ATTR_ALLOC_CHUNK;
8324               cur_attrs
8325                 = xrealloc (cur_attrs, (allocated_attrs
8326                                         * sizeof (struct attr_abbrev)));
8327             }
8328
8329           /* Record whether this compilation unit might have
8330              inter-compilation-unit references.  If we don't know what form
8331              this attribute will have, then it might potentially be a
8332              DW_FORM_ref_addr, so we conservatively expect inter-CU
8333              references.  */
8334
8335           if (abbrev_form == DW_FORM_ref_addr
8336               || abbrev_form == DW_FORM_indirect)
8337             cu->has_form_ref_addr = 1;
8338
8339           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8340           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8341           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8342           abbrev_ptr += bytes_read;
8343           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8344           abbrev_ptr += bytes_read;
8345         }
8346
8347       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8348                                          (cur_abbrev->num_attrs
8349                                           * sizeof (struct attr_abbrev)));
8350       memcpy (cur_abbrev->attrs, cur_attrs,
8351               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8352
8353       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8354       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8355       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8356
8357       /* Get next abbreviation.
8358          Under Irix6 the abbreviations for a compilation unit are not
8359          always properly terminated with an abbrev number of 0.
8360          Exit loop if we encounter an abbreviation which we have
8361          already read (which means we are about to read the abbreviations
8362          for the next compile unit) or if the end of the abbreviation
8363          table is reached.  */
8364       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8365           >= dwarf2_per_objfile->abbrev.size)
8366         break;
8367       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8368       abbrev_ptr += bytes_read;
8369       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8370         break;
8371     }
8372
8373   xfree (cur_attrs);
8374 }
8375
8376 /* Release the memory used by the abbrev table for a compilation unit.  */
8377
8378 static void
8379 dwarf2_free_abbrev_table (void *ptr_to_cu)
8380 {
8381   struct dwarf2_cu *cu = ptr_to_cu;
8382
8383   obstack_free (&cu->abbrev_obstack, NULL);
8384   cu->dwarf2_abbrevs = NULL;
8385 }
8386
8387 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8388
8389 static struct abbrev_info *
8390 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8391 {
8392   unsigned int hash_number;
8393   struct abbrev_info *abbrev;
8394
8395   hash_number = number % ABBREV_HASH_SIZE;
8396   abbrev = cu->dwarf2_abbrevs[hash_number];
8397
8398   while (abbrev)
8399     {
8400       if (abbrev->number == number)
8401         return abbrev;
8402       else
8403         abbrev = abbrev->next;
8404     }
8405   return NULL;
8406 }
8407
8408 /* Returns nonzero if TAG represents a type that we might generate a partial
8409    symbol for.  */
8410
8411 static int
8412 is_type_tag_for_partial (int tag)
8413 {
8414   switch (tag)
8415     {
8416 #if 0
8417     /* Some types that would be reasonable to generate partial symbols for,
8418        that we don't at present.  */
8419     case DW_TAG_array_type:
8420     case DW_TAG_file_type:
8421     case DW_TAG_ptr_to_member_type:
8422     case DW_TAG_set_type:
8423     case DW_TAG_string_type:
8424     case DW_TAG_subroutine_type:
8425 #endif
8426     case DW_TAG_base_type:
8427     case DW_TAG_class_type:
8428     case DW_TAG_interface_type:
8429     case DW_TAG_enumeration_type:
8430     case DW_TAG_structure_type:
8431     case DW_TAG_subrange_type:
8432     case DW_TAG_typedef:
8433     case DW_TAG_union_type:
8434       return 1;
8435     default:
8436       return 0;
8437     }
8438 }
8439
8440 /* Load all DIEs that are interesting for partial symbols into memory.  */
8441
8442 static struct partial_die_info *
8443 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8444                    int building_psymtab, struct dwarf2_cu *cu)
8445 {
8446   struct partial_die_info *part_die;
8447   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8448   struct abbrev_info *abbrev;
8449   unsigned int bytes_read;
8450   unsigned int load_all = 0;
8451
8452   int nesting_level = 1;
8453
8454   parent_die = NULL;
8455   last_die = NULL;
8456
8457   if (cu->per_cu && cu->per_cu->load_all_dies)
8458     load_all = 1;
8459
8460   cu->partial_dies
8461     = htab_create_alloc_ex (cu->header.length / 12,
8462                             partial_die_hash,
8463                             partial_die_eq,
8464                             NULL,
8465                             &cu->comp_unit_obstack,
8466                             hashtab_obstack_allocate,
8467                             dummy_obstack_deallocate);
8468
8469   part_die = obstack_alloc (&cu->comp_unit_obstack,
8470                             sizeof (struct partial_die_info));
8471
8472   while (1)
8473     {
8474       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8475
8476       /* A NULL abbrev means the end of a series of children.  */
8477       if (abbrev == NULL)
8478         {
8479           if (--nesting_level == 0)
8480             {
8481               /* PART_DIE was probably the last thing allocated on the
8482                  comp_unit_obstack, so we could call obstack_free
8483                  here.  We don't do that because the waste is small,
8484                  and will be cleaned up when we're done with this
8485                  compilation unit.  This way, we're also more robust
8486                  against other users of the comp_unit_obstack.  */
8487               return first_die;
8488             }
8489           info_ptr += bytes_read;
8490           last_die = parent_die;
8491           parent_die = parent_die->die_parent;
8492           continue;
8493         }
8494
8495       /* Check for template arguments.  We never save these; if
8496          they're seen, we just mark the parent, and go on our way.  */
8497       if (parent_die != NULL
8498           && cu->language == language_cplus
8499           && (abbrev->tag == DW_TAG_template_type_param
8500               || abbrev->tag == DW_TAG_template_value_param))
8501         {
8502           parent_die->has_template_arguments = 1;
8503
8504           if (!load_all)
8505             {
8506               /* We don't need a partial DIE for the template argument.  */
8507               info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8508                                        cu);
8509               continue;
8510             }
8511         }
8512
8513       /* We only recurse into subprograms looking for template arguments.
8514          Skip their other children.  */
8515       if (!load_all
8516           && cu->language == language_cplus
8517           && parent_die != NULL
8518           && parent_die->tag == DW_TAG_subprogram)
8519         {
8520           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8521           continue;
8522         }
8523
8524       /* Check whether this DIE is interesting enough to save.  Normally
8525          we would not be interested in members here, but there may be
8526          later variables referencing them via DW_AT_specification (for
8527          static members).  */
8528       if (!load_all
8529           && !is_type_tag_for_partial (abbrev->tag)
8530           && abbrev->tag != DW_TAG_enumerator
8531           && abbrev->tag != DW_TAG_subprogram
8532           && abbrev->tag != DW_TAG_lexical_block
8533           && abbrev->tag != DW_TAG_variable
8534           && abbrev->tag != DW_TAG_namespace
8535           && abbrev->tag != DW_TAG_module
8536           && abbrev->tag != DW_TAG_member)
8537         {
8538           /* Otherwise we skip to the next sibling, if any.  */
8539           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8540           continue;
8541         }
8542
8543       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8544                                    buffer, info_ptr, cu);
8545
8546       /* This two-pass algorithm for processing partial symbols has a
8547          high cost in cache pressure.  Thus, handle some simple cases
8548          here which cover the majority of C partial symbols.  DIEs
8549          which neither have specification tags in them, nor could have
8550          specification tags elsewhere pointing at them, can simply be
8551          processed and discarded.
8552
8553          This segment is also optional; scan_partial_symbols and
8554          add_partial_symbol will handle these DIEs if we chain
8555          them in normally.  When compilers which do not emit large
8556          quantities of duplicate debug information are more common,
8557          this code can probably be removed.  */
8558
8559       /* Any complete simple types at the top level (pretty much all
8560          of them, for a language without namespaces), can be processed
8561          directly.  */
8562       if (parent_die == NULL
8563           && part_die->has_specification == 0
8564           && part_die->is_declaration == 0
8565           && (part_die->tag == DW_TAG_typedef
8566               || part_die->tag == DW_TAG_base_type
8567               || part_die->tag == DW_TAG_subrange_type))
8568         {
8569           if (building_psymtab && part_die->name != NULL)
8570             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8571                                  VAR_DOMAIN, LOC_TYPEDEF,
8572                                  &cu->objfile->static_psymbols,
8573                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8574           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8575           continue;
8576         }
8577
8578       /* If we're at the second level, and we're an enumerator, and
8579          our parent has no specification (meaning possibly lives in a
8580          namespace elsewhere), then we can add the partial symbol now
8581          instead of queueing it.  */
8582       if (part_die->tag == DW_TAG_enumerator
8583           && parent_die != NULL
8584           && parent_die->die_parent == NULL
8585           && parent_die->tag == DW_TAG_enumeration_type
8586           && parent_die->has_specification == 0)
8587         {
8588           if (part_die->name == NULL)
8589             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8590           else if (building_psymtab)
8591             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8592                                  VAR_DOMAIN, LOC_CONST,
8593                                  (cu->language == language_cplus
8594                                   || cu->language == language_java)
8595                                  ? &cu->objfile->global_psymbols
8596                                  : &cu->objfile->static_psymbols,
8597                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8598
8599           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8600           continue;
8601         }
8602
8603       /* We'll save this DIE so link it in.  */
8604       part_die->die_parent = parent_die;
8605       part_die->die_sibling = NULL;
8606       part_die->die_child = NULL;
8607
8608       if (last_die && last_die == parent_die)
8609         last_die->die_child = part_die;
8610       else if (last_die)
8611         last_die->die_sibling = part_die;
8612
8613       last_die = part_die;
8614
8615       if (first_die == NULL)
8616         first_die = part_die;
8617
8618       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8619          find interesting need to be in the hash table, because we
8620          also have the parent/sibling/child chains; only those that we
8621          might refer to by offset later during partial symbol reading.
8622
8623          For now this means things that might have be the target of a
8624          DW_AT_specification, DW_AT_abstract_origin, or
8625          DW_AT_extension.  DW_AT_extension will refer only to
8626          namespaces; DW_AT_abstract_origin refers to functions (and
8627          many things under the function DIE, but we do not recurse
8628          into function DIEs during partial symbol reading) and
8629          possibly variables as well; DW_AT_specification refers to
8630          declarations.  Declarations ought to have the DW_AT_declaration
8631          flag.  It happens that GCC forgets to put it in sometimes, but
8632          only for functions, not for types.
8633
8634          Adding more things than necessary to the hash table is harmless
8635          except for the performance cost.  Adding too few will result in
8636          wasted time in find_partial_die, when we reread the compilation
8637          unit with load_all_dies set.  */
8638
8639       if (load_all
8640           || abbrev->tag == DW_TAG_subprogram
8641           || abbrev->tag == DW_TAG_variable
8642           || abbrev->tag == DW_TAG_namespace
8643           || part_die->is_declaration)
8644         {
8645           void **slot;
8646
8647           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8648                                            part_die->offset, INSERT);
8649           *slot = part_die;
8650         }
8651
8652       part_die = obstack_alloc (&cu->comp_unit_obstack,
8653                                 sizeof (struct partial_die_info));
8654
8655       /* For some DIEs we want to follow their children (if any).  For C
8656          we have no reason to follow the children of structures; for other
8657          languages we have to, so that we can get at method physnames
8658          to infer fully qualified class names, for DW_AT_specification,
8659          and for C++ template arguments.  For C++, we also look one level
8660          inside functions to find template arguments (if the name of the
8661          function does not already contain the template arguments).
8662
8663          For Ada, we need to scan the children of subprograms and lexical
8664          blocks as well because Ada allows the definition of nested
8665          entities that could be interesting for the debugger, such as
8666          nested subprograms for instance.  */
8667       if (last_die->has_children
8668           && (load_all
8669               || last_die->tag == DW_TAG_namespace
8670               || last_die->tag == DW_TAG_module
8671               || last_die->tag == DW_TAG_enumeration_type
8672               || (cu->language == language_cplus
8673                   && last_die->tag == DW_TAG_subprogram
8674                   && (last_die->name == NULL
8675                       || strchr (last_die->name, '<') == NULL))
8676               || (cu->language != language_c
8677                   && (last_die->tag == DW_TAG_class_type
8678                       || last_die->tag == DW_TAG_interface_type
8679                       || last_die->tag == DW_TAG_structure_type
8680                       || last_die->tag == DW_TAG_union_type))
8681               || (cu->language == language_ada
8682                   && (last_die->tag == DW_TAG_subprogram
8683                       || last_die->tag == DW_TAG_lexical_block))))
8684         {
8685           nesting_level++;
8686           parent_die = last_die;
8687           continue;
8688         }
8689
8690       /* Otherwise we skip to the next sibling, if any.  */
8691       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8692
8693       /* Back to the top, do it again.  */
8694     }
8695 }
8696
8697 /* Read a minimal amount of information into the minimal die structure.  */
8698
8699 static gdb_byte *
8700 read_partial_die (struct partial_die_info *part_die,
8701                   struct abbrev_info *abbrev,
8702                   unsigned int abbrev_len, bfd *abfd,
8703                   gdb_byte *buffer, gdb_byte *info_ptr,
8704                   struct dwarf2_cu *cu)
8705 {
8706   unsigned int i;
8707   struct attribute attr;
8708   int has_low_pc_attr = 0;
8709   int has_high_pc_attr = 0;
8710
8711   memset (part_die, 0, sizeof (struct partial_die_info));
8712
8713   part_die->offset = info_ptr - buffer;
8714
8715   info_ptr += abbrev_len;
8716
8717   if (abbrev == NULL)
8718     return info_ptr;
8719
8720   part_die->tag = abbrev->tag;
8721   part_die->has_children = abbrev->has_children;
8722
8723   for (i = 0; i < abbrev->num_attrs; ++i)
8724     {
8725       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8726
8727       /* Store the data if it is of an attribute we want to keep in a
8728          partial symbol table.  */
8729       switch (attr.name)
8730         {
8731         case DW_AT_name:
8732           switch (part_die->tag)
8733             {
8734             case DW_TAG_compile_unit:
8735             case DW_TAG_type_unit:
8736               /* Compilation units have a DW_AT_name that is a filename, not
8737                  a source language identifier.  */
8738             case DW_TAG_enumeration_type:
8739             case DW_TAG_enumerator:
8740               /* These tags always have simple identifiers already; no need
8741                  to canonicalize them.  */
8742               part_die->name = DW_STRING (&attr);
8743               break;
8744             default:
8745               part_die->name
8746                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8747                                             &cu->objfile->objfile_obstack);
8748               break;
8749             }
8750           break;
8751         case DW_AT_linkage_name:
8752         case DW_AT_MIPS_linkage_name:
8753           /* Note that both forms of linkage name might appear.  We
8754              assume they will be the same, and we only store the last
8755              one we see.  */
8756           if (cu->language == language_ada)
8757             part_die->name = DW_STRING (&attr);
8758           break;
8759         case DW_AT_low_pc:
8760           has_low_pc_attr = 1;
8761           part_die->lowpc = DW_ADDR (&attr);
8762           break;
8763         case DW_AT_high_pc:
8764           has_high_pc_attr = 1;
8765           part_die->highpc = DW_ADDR (&attr);
8766           break;
8767         case DW_AT_location:
8768           /* Support the .debug_loc offsets */
8769           if (attr_form_is_block (&attr))
8770             {
8771                part_die->locdesc = DW_BLOCK (&attr);
8772             }
8773           else if (attr_form_is_section_offset (&attr))
8774             {
8775               dwarf2_complex_location_expr_complaint ();
8776             }
8777           else
8778             {
8779               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8780                                                      "partial symbol information");
8781             }
8782           break;
8783         case DW_AT_external:
8784           part_die->is_external = DW_UNSND (&attr);
8785           break;
8786         case DW_AT_declaration:
8787           part_die->is_declaration = DW_UNSND (&attr);
8788           break;
8789         case DW_AT_type:
8790           part_die->has_type = 1;
8791           break;
8792         case DW_AT_abstract_origin:
8793         case DW_AT_specification:
8794         case DW_AT_extension:
8795           part_die->has_specification = 1;
8796           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8797           break;
8798         case DW_AT_sibling:
8799           /* Ignore absolute siblings, they might point outside of
8800              the current compile unit.  */
8801           if (attr.form == DW_FORM_ref_addr)
8802             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8803           else
8804             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8805           break;
8806         case DW_AT_byte_size:
8807           part_die->has_byte_size = 1;
8808           break;
8809         case DW_AT_calling_convention:
8810           /* DWARF doesn't provide a way to identify a program's source-level
8811              entry point.  DW_AT_calling_convention attributes are only meant
8812              to describe functions' calling conventions.
8813
8814              However, because it's a necessary piece of information in
8815              Fortran, and because DW_CC_program is the only piece of debugging
8816              information whose definition refers to a 'main program' at all,
8817              several compilers have begun marking Fortran main programs with
8818              DW_CC_program --- even when those functions use the standard
8819              calling conventions.
8820
8821              So until DWARF specifies a way to provide this information and
8822              compilers pick up the new representation, we'll support this
8823              practice.  */
8824           if (DW_UNSND (&attr) == DW_CC_program
8825               && cu->language == language_fortran)
8826             set_main_name (part_die->name);
8827           break;
8828         default:
8829           break;
8830         }
8831     }
8832
8833   /* When using the GNU linker, .gnu.linkonce. sections are used to
8834      eliminate duplicate copies of functions and vtables and such.
8835      The linker will arbitrarily choose one and discard the others.
8836      The AT_*_pc values for such functions refer to local labels in
8837      these sections.  If the section from that file was discarded, the
8838      labels are not in the output, so the relocs get a value of 0.
8839      If this is a discarded function, mark the pc bounds as invalid,
8840      so that GDB will ignore it.  */
8841   if (has_low_pc_attr && has_high_pc_attr
8842       && part_die->lowpc < part_die->highpc
8843       && (part_die->lowpc != 0
8844           || dwarf2_per_objfile->has_section_at_zero))
8845     part_die->has_pc_info = 1;
8846
8847   return info_ptr;
8848 }
8849
8850 /* Find a cached partial DIE at OFFSET in CU.  */
8851
8852 static struct partial_die_info *
8853 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8854 {
8855   struct partial_die_info *lookup_die = NULL;
8856   struct partial_die_info part_die;
8857
8858   part_die.offset = offset;
8859   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8860
8861   return lookup_die;
8862 }
8863
8864 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8865    except in the case of .debug_types DIEs which do not reference
8866    outside their CU (they do however referencing other types via
8867    DW_FORM_sig8).  */
8868
8869 static struct partial_die_info *
8870 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8871 {
8872   struct dwarf2_per_cu_data *per_cu = NULL;
8873   struct partial_die_info *pd = NULL;
8874
8875   if (cu->per_cu->from_debug_types)
8876     {
8877       pd = find_partial_die_in_comp_unit (offset, cu);
8878       if (pd != NULL)
8879         return pd;
8880       goto not_found;
8881     }
8882
8883   if (offset_in_cu_p (&cu->header, offset))
8884     {
8885       pd = find_partial_die_in_comp_unit (offset, cu);
8886       if (pd != NULL)
8887         return pd;
8888     }
8889
8890   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8891
8892   if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8893     load_partial_comp_unit (per_cu, cu->objfile);
8894
8895   per_cu->cu->last_used = 0;
8896   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8897
8898   if (pd == NULL && per_cu->load_all_dies == 0)
8899     {
8900       struct cleanup *back_to;
8901       struct partial_die_info comp_unit_die;
8902       struct abbrev_info *abbrev;
8903       unsigned int bytes_read;
8904       char *info_ptr;
8905
8906       per_cu->load_all_dies = 1;
8907
8908       /* Re-read the DIEs.  */
8909       back_to = make_cleanup (null_cleanup, 0);
8910       if (per_cu->cu->dwarf2_abbrevs == NULL)
8911         {
8912           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8913           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8914         }
8915       info_ptr = (dwarf2_per_objfile->info.buffer
8916                   + per_cu->cu->header.offset
8917                   + per_cu->cu->header.first_die_offset);
8918       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8919       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8920                                    per_cu->cu->objfile->obfd,
8921                                    dwarf2_per_objfile->info.buffer, info_ptr,
8922                                    per_cu->cu);
8923       if (comp_unit_die.has_children)
8924         load_partial_dies (per_cu->cu->objfile->obfd,
8925                            dwarf2_per_objfile->info.buffer, info_ptr,
8926                            0, per_cu->cu);
8927       do_cleanups (back_to);
8928
8929       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8930     }
8931
8932  not_found:
8933
8934   if (pd == NULL)
8935     internal_error (__FILE__, __LINE__,
8936                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8937                     offset, bfd_get_filename (cu->objfile->obfd));
8938   return pd;
8939 }
8940
8941 /* Adjust PART_DIE before generating a symbol for it.  This function
8942    may set the is_external flag or change the DIE's name.  */
8943
8944 static void
8945 fixup_partial_die (struct partial_die_info *part_die,
8946                    struct dwarf2_cu *cu)
8947 {
8948   /* If we found a reference attribute and the DIE has no name, try
8949      to find a name in the referred to DIE.  */
8950
8951   if (part_die->name == NULL && part_die->has_specification)
8952     {
8953       struct partial_die_info *spec_die;
8954
8955       spec_die = find_partial_die (part_die->spec_offset, cu);
8956
8957       fixup_partial_die (spec_die, cu);
8958
8959       if (spec_die->name)
8960         {
8961           part_die->name = spec_die->name;
8962
8963           /* Copy DW_AT_external attribute if it is set.  */
8964           if (spec_die->is_external)
8965             part_die->is_external = spec_die->is_external;
8966         }
8967     }
8968
8969   /* Set default names for some unnamed DIEs.  */
8970   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8971                                  || part_die->tag == DW_TAG_class_type))
8972     part_die->name = "(anonymous class)";
8973
8974   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8975     part_die->name = "(anonymous namespace)";
8976
8977   if (part_die->tag == DW_TAG_structure_type
8978       || part_die->tag == DW_TAG_class_type
8979       || part_die->tag == DW_TAG_union_type)
8980     guess_structure_name (part_die, cu);
8981 }
8982
8983 /* Read an attribute value described by an attribute form.  */
8984
8985 static gdb_byte *
8986 read_attribute_value (struct attribute *attr, unsigned form,
8987                       bfd *abfd, gdb_byte *info_ptr,
8988                       struct dwarf2_cu *cu)
8989 {
8990   struct comp_unit_head *cu_header = &cu->header;
8991   unsigned int bytes_read;
8992   struct dwarf_block *blk;
8993
8994   attr->form = form;
8995   switch (form)
8996     {
8997     case DW_FORM_ref_addr:
8998       if (cu->header.version == 2)
8999         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9000       else
9001         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9002       info_ptr += bytes_read;
9003       break;
9004     case DW_FORM_addr:
9005       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9006       info_ptr += bytes_read;
9007       break;
9008     case DW_FORM_block2:
9009       blk = dwarf_alloc_block (cu);
9010       blk->size = read_2_bytes (abfd, info_ptr);
9011       info_ptr += 2;
9012       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9013       info_ptr += blk->size;
9014       DW_BLOCK (attr) = blk;
9015       break;
9016     case DW_FORM_block4:
9017       blk = dwarf_alloc_block (cu);
9018       blk->size = read_4_bytes (abfd, info_ptr);
9019       info_ptr += 4;
9020       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9021       info_ptr += blk->size;
9022       DW_BLOCK (attr) = blk;
9023       break;
9024     case DW_FORM_data2:
9025       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9026       info_ptr += 2;
9027       break;
9028     case DW_FORM_data4:
9029       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9030       info_ptr += 4;
9031       break;
9032     case DW_FORM_data8:
9033       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9034       info_ptr += 8;
9035       break;
9036     case DW_FORM_sec_offset:
9037       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9038       info_ptr += bytes_read;
9039       break;
9040     case DW_FORM_string:
9041       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9042       DW_STRING_IS_CANONICAL (attr) = 0;
9043       info_ptr += bytes_read;
9044       break;
9045     case DW_FORM_strp:
9046       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9047                                                &bytes_read);
9048       DW_STRING_IS_CANONICAL (attr) = 0;
9049       info_ptr += bytes_read;
9050       break;
9051     case DW_FORM_exprloc:
9052     case DW_FORM_block:
9053       blk = dwarf_alloc_block (cu);
9054       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9055       info_ptr += bytes_read;
9056       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9057       info_ptr += blk->size;
9058       DW_BLOCK (attr) = blk;
9059       break;
9060     case DW_FORM_block1:
9061       blk = dwarf_alloc_block (cu);
9062       blk->size = read_1_byte (abfd, info_ptr);
9063       info_ptr += 1;
9064       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9065       info_ptr += blk->size;
9066       DW_BLOCK (attr) = blk;
9067       break;
9068     case DW_FORM_data1:
9069       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9070       info_ptr += 1;
9071       break;
9072     case DW_FORM_flag:
9073       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9074       info_ptr += 1;
9075       break;
9076     case DW_FORM_flag_present:
9077       DW_UNSND (attr) = 1;
9078       break;
9079     case DW_FORM_sdata:
9080       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9081       info_ptr += bytes_read;
9082       break;
9083     case DW_FORM_udata:
9084       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9085       info_ptr += bytes_read;
9086       break;
9087     case DW_FORM_ref1:
9088       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9089       info_ptr += 1;
9090       break;
9091     case DW_FORM_ref2:
9092       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9093       info_ptr += 2;
9094       break;
9095     case DW_FORM_ref4:
9096       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9097       info_ptr += 4;
9098       break;
9099     case DW_FORM_ref8:
9100       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9101       info_ptr += 8;
9102       break;
9103     case DW_FORM_sig8:
9104       /* Convert the signature to something we can record in DW_UNSND
9105          for later lookup.
9106          NOTE: This is NULL if the type wasn't found.  */
9107       DW_SIGNATURED_TYPE (attr) =
9108         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9109       info_ptr += 8;
9110       break;
9111     case DW_FORM_ref_udata:
9112       DW_ADDR (attr) = (cu->header.offset
9113                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9114       info_ptr += bytes_read;
9115       break;
9116     case DW_FORM_indirect:
9117       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9118       info_ptr += bytes_read;
9119       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9120       break;
9121     default:
9122       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9123              dwarf_form_name (form),
9124              bfd_get_filename (abfd));
9125     }
9126
9127   /* We have seen instances where the compiler tried to emit a byte
9128      size attribute of -1 which ended up being encoded as an unsigned
9129      0xffffffff.  Although 0xffffffff is technically a valid size value,
9130      an object of this size seems pretty unlikely so we can relatively
9131      safely treat these cases as if the size attribute was invalid and
9132      treat them as zero by default.  */
9133   if (attr->name == DW_AT_byte_size
9134       && form == DW_FORM_data4
9135       && DW_UNSND (attr) >= 0xffffffff)
9136     {
9137       complaint
9138         (&symfile_complaints,
9139          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9140          hex_string (DW_UNSND (attr)));
9141       DW_UNSND (attr) = 0;
9142     }
9143
9144   return info_ptr;
9145 }
9146
9147 /* Read an attribute described by an abbreviated attribute.  */
9148
9149 static gdb_byte *
9150 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9151                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9152 {
9153   attr->name = abbrev->name;
9154   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9155 }
9156
9157 /* read dwarf information from a buffer */
9158
9159 static unsigned int
9160 read_1_byte (bfd *abfd, gdb_byte *buf)
9161 {
9162   return bfd_get_8 (abfd, buf);
9163 }
9164
9165 static int
9166 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9167 {
9168   return bfd_get_signed_8 (abfd, buf);
9169 }
9170
9171 static unsigned int
9172 read_2_bytes (bfd *abfd, gdb_byte *buf)
9173 {
9174   return bfd_get_16 (abfd, buf);
9175 }
9176
9177 static int
9178 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9179 {
9180   return bfd_get_signed_16 (abfd, buf);
9181 }
9182
9183 static unsigned int
9184 read_4_bytes (bfd *abfd, gdb_byte *buf)
9185 {
9186   return bfd_get_32 (abfd, buf);
9187 }
9188
9189 static int
9190 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9191 {
9192   return bfd_get_signed_32 (abfd, buf);
9193 }
9194
9195 static ULONGEST
9196 read_8_bytes (bfd *abfd, gdb_byte *buf)
9197 {
9198   return bfd_get_64 (abfd, buf);
9199 }
9200
9201 static CORE_ADDR
9202 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9203               unsigned int *bytes_read)
9204 {
9205   struct comp_unit_head *cu_header = &cu->header;
9206   CORE_ADDR retval = 0;
9207
9208   if (cu_header->signed_addr_p)
9209     {
9210       switch (cu_header->addr_size)
9211         {
9212         case 2:
9213           retval = bfd_get_signed_16 (abfd, buf);
9214           break;
9215         case 4:
9216           retval = bfd_get_signed_32 (abfd, buf);
9217           break;
9218         case 8:
9219           retval = bfd_get_signed_64 (abfd, buf);
9220           break;
9221         default:
9222           internal_error (__FILE__, __LINE__,
9223                           _("read_address: bad switch, signed [in module %s]"),
9224                           bfd_get_filename (abfd));
9225         }
9226     }
9227   else
9228     {
9229       switch (cu_header->addr_size)
9230         {
9231         case 2:
9232           retval = bfd_get_16 (abfd, buf);
9233           break;
9234         case 4:
9235           retval = bfd_get_32 (abfd, buf);
9236           break;
9237         case 8:
9238           retval = bfd_get_64 (abfd, buf);
9239           break;
9240         default:
9241           internal_error (__FILE__, __LINE__,
9242                           _("read_address: bad switch, unsigned [in module %s]"),
9243                           bfd_get_filename (abfd));
9244         }
9245     }
9246
9247   *bytes_read = cu_header->addr_size;
9248   return retval;
9249 }
9250
9251 /* Read the initial length from a section.  The (draft) DWARF 3
9252    specification allows the initial length to take up either 4 bytes
9253    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
9254    bytes describe the length and all offsets will be 8 bytes in length
9255    instead of 4.
9256
9257    An older, non-standard 64-bit format is also handled by this
9258    function.  The older format in question stores the initial length
9259    as an 8-byte quantity without an escape value.  Lengths greater
9260    than 2^32 aren't very common which means that the initial 4 bytes
9261    is almost always zero.  Since a length value of zero doesn't make
9262    sense for the 32-bit format, this initial zero can be considered to
9263    be an escape value which indicates the presence of the older 64-bit
9264    format.  As written, the code can't detect (old format) lengths
9265    greater than 4GB.  If it becomes necessary to handle lengths
9266    somewhat larger than 4GB, we could allow other small values (such
9267    as the non-sensical values of 1, 2, and 3) to also be used as
9268    escape values indicating the presence of the old format.
9269
9270    The value returned via bytes_read should be used to increment the
9271    relevant pointer after calling read_initial_length().
9272
9273    [ Note:  read_initial_length() and read_offset() are based on the
9274      document entitled "DWARF Debugging Information Format", revision
9275      3, draft 8, dated November 19, 2001.  This document was obtained
9276      from:
9277
9278         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9279
9280      This document is only a draft and is subject to change.  (So beware.)
9281
9282      Details regarding the older, non-standard 64-bit format were
9283      determined empirically by examining 64-bit ELF files produced by
9284      the SGI toolchain on an IRIX 6.5 machine.
9285
9286      - Kevin, July 16, 2002
9287    ] */
9288
9289 static LONGEST
9290 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9291 {
9292   LONGEST length = bfd_get_32 (abfd, buf);
9293
9294   if (length == 0xffffffff)
9295     {
9296       length = bfd_get_64 (abfd, buf + 4);
9297       *bytes_read = 12;
9298     }
9299   else if (length == 0)
9300     {
9301       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
9302       length = bfd_get_64 (abfd, buf);
9303       *bytes_read = 8;
9304     }
9305   else
9306     {
9307       *bytes_read = 4;
9308     }
9309
9310   return length;
9311 }
9312
9313 /* Cover function for read_initial_length.
9314    Returns the length of the object at BUF, and stores the size of the
9315    initial length in *BYTES_READ and stores the size that offsets will be in
9316    *OFFSET_SIZE.
9317    If the initial length size is not equivalent to that specified in
9318    CU_HEADER then issue a complaint.
9319    This is useful when reading non-comp-unit headers.  */
9320
9321 static LONGEST
9322 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9323                                         const struct comp_unit_head *cu_header,
9324                                         unsigned int *bytes_read,
9325                                         unsigned int *offset_size)
9326 {
9327   LONGEST length = read_initial_length (abfd, buf, bytes_read);
9328
9329   gdb_assert (cu_header->initial_length_size == 4
9330               || cu_header->initial_length_size == 8
9331               || cu_header->initial_length_size == 12);
9332
9333   if (cu_header->initial_length_size != *bytes_read)
9334     complaint (&symfile_complaints,
9335                _("intermixed 32-bit and 64-bit DWARF sections"));
9336
9337   *offset_size = (*bytes_read == 4) ? 4 : 8;
9338   return length;
9339 }
9340
9341 /* Read an offset from the data stream.  The size of the offset is
9342    given by cu_header->offset_size.  */
9343
9344 static LONGEST
9345 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9346              unsigned int *bytes_read)
9347 {
9348   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9349
9350   *bytes_read = cu_header->offset_size;
9351   return offset;
9352 }
9353
9354 /* Read an offset from the data stream.  */
9355
9356 static LONGEST
9357 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9358 {
9359   LONGEST retval = 0;
9360
9361   switch (offset_size)
9362     {
9363     case 4:
9364       retval = bfd_get_32 (abfd, buf);
9365       break;
9366     case 8:
9367       retval = bfd_get_64 (abfd, buf);
9368       break;
9369     default:
9370       internal_error (__FILE__, __LINE__,
9371                       _("read_offset_1: bad switch [in module %s]"),
9372                       bfd_get_filename (abfd));
9373     }
9374
9375   return retval;
9376 }
9377
9378 static gdb_byte *
9379 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9380 {
9381   /* If the size of a host char is 8 bits, we can return a pointer
9382      to the buffer, otherwise we have to copy the data to a buffer
9383      allocated on the temporary obstack.  */
9384   gdb_assert (HOST_CHAR_BIT == 8);
9385   return buf;
9386 }
9387
9388 static char *
9389 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9390 {
9391   /* If the size of a host char is 8 bits, we can return a pointer
9392      to the string, otherwise we have to copy the string to a buffer
9393      allocated on the temporary obstack.  */
9394   gdb_assert (HOST_CHAR_BIT == 8);
9395   if (*buf == '\0')
9396     {
9397       *bytes_read_ptr = 1;
9398       return NULL;
9399     }
9400   *bytes_read_ptr = strlen ((char *) buf) + 1;
9401   return (char *) buf;
9402 }
9403
9404 static char *
9405 read_indirect_string (bfd *abfd, gdb_byte *buf,
9406                       const struct comp_unit_head *cu_header,
9407                       unsigned int *bytes_read_ptr)
9408 {
9409   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9410
9411   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9412   if (dwarf2_per_objfile->str.buffer == NULL)
9413     {
9414       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9415                       bfd_get_filename (abfd));
9416       return NULL;
9417     }
9418   if (str_offset >= dwarf2_per_objfile->str.size)
9419     {
9420       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9421                       bfd_get_filename (abfd));
9422       return NULL;
9423     }
9424   gdb_assert (HOST_CHAR_BIT == 8);
9425   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9426     return NULL;
9427   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9428 }
9429
9430 static unsigned long
9431 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9432 {
9433   unsigned long result;
9434   unsigned int num_read;
9435   int i, shift;
9436   unsigned char byte;
9437
9438   result = 0;
9439   shift = 0;
9440   num_read = 0;
9441   i = 0;
9442   while (1)
9443     {
9444       byte = bfd_get_8 (abfd, buf);
9445       buf++;
9446       num_read++;
9447       result |= ((unsigned long)(byte & 127) << shift);
9448       if ((byte & 128) == 0)
9449         {
9450           break;
9451         }
9452       shift += 7;
9453     }
9454   *bytes_read_ptr = num_read;
9455   return result;
9456 }
9457
9458 static long
9459 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9460 {
9461   long result;
9462   int i, shift, num_read;
9463   unsigned char byte;
9464
9465   result = 0;
9466   shift = 0;
9467   num_read = 0;
9468   i = 0;
9469   while (1)
9470     {
9471       byte = bfd_get_8 (abfd, buf);
9472       buf++;
9473       num_read++;
9474       result |= ((long)(byte & 127) << shift);
9475       shift += 7;
9476       if ((byte & 128) == 0)
9477         {
9478           break;
9479         }
9480     }
9481   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9482     result |= -(((long)1) << shift);
9483   *bytes_read_ptr = num_read;
9484   return result;
9485 }
9486
9487 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9488
9489 static gdb_byte *
9490 skip_leb128 (bfd *abfd, gdb_byte *buf)
9491 {
9492   int byte;
9493
9494   while (1)
9495     {
9496       byte = bfd_get_8 (abfd, buf);
9497       buf++;
9498       if ((byte & 128) == 0)
9499         return buf;
9500     }
9501 }
9502
9503 static void
9504 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9505 {
9506   switch (lang)
9507     {
9508     case DW_LANG_C89:
9509     case DW_LANG_C99:
9510     case DW_LANG_C:
9511       cu->language = language_c;
9512       break;
9513     case DW_LANG_C_plus_plus:
9514       cu->language = language_cplus;
9515       break;
9516     case DW_LANG_D:
9517       cu->language = language_d;
9518       break;
9519     case DW_LANG_Fortran77:
9520     case DW_LANG_Fortran90:
9521     case DW_LANG_Fortran95:
9522       cu->language = language_fortran;
9523       break;
9524     case DW_LANG_Mips_Assembler:
9525       cu->language = language_asm;
9526       break;
9527     case DW_LANG_Java:
9528       cu->language = language_java;
9529       break;
9530     case DW_LANG_Ada83:
9531     case DW_LANG_Ada95:
9532       cu->language = language_ada;
9533       break;
9534     case DW_LANG_Modula2:
9535       cu->language = language_m2;
9536       break;
9537     case DW_LANG_Pascal83:
9538       cu->language = language_pascal;
9539       break;
9540     case DW_LANG_ObjC:
9541       cu->language = language_objc;
9542       break;
9543     case DW_LANG_Cobol74:
9544     case DW_LANG_Cobol85:
9545     default:
9546       cu->language = language_minimal;
9547       break;
9548     }
9549   cu->language_defn = language_def (cu->language);
9550 }
9551
9552 /* Return the named attribute or NULL if not there.  */
9553
9554 static struct attribute *
9555 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9556 {
9557   unsigned int i;
9558   struct attribute *spec = NULL;
9559
9560   for (i = 0; i < die->num_attrs; ++i)
9561     {
9562       if (die->attrs[i].name == name)
9563         return &die->attrs[i];
9564       if (die->attrs[i].name == DW_AT_specification
9565           || die->attrs[i].name == DW_AT_abstract_origin)
9566         spec = &die->attrs[i];
9567     }
9568
9569   if (spec)
9570     {
9571       die = follow_die_ref (die, spec, &cu);
9572       return dwarf2_attr (die, name, cu);
9573     }
9574
9575   return NULL;
9576 }
9577
9578 /* Return the named attribute or NULL if not there,
9579    but do not follow DW_AT_specification, etc.
9580    This is for use in contexts where we're reading .debug_types dies.
9581    Following DW_AT_specification, DW_AT_abstract_origin will take us
9582    back up the chain, and we want to go down.  */
9583
9584 static struct attribute *
9585 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9586                        struct dwarf2_cu *cu)
9587 {
9588   unsigned int i;
9589
9590   for (i = 0; i < die->num_attrs; ++i)
9591     if (die->attrs[i].name == name)
9592       return &die->attrs[i];
9593
9594   return NULL;
9595 }
9596
9597 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9598    and holds a non-zero value.  This function should only be used for
9599    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9600
9601 static int
9602 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9603 {
9604   struct attribute *attr = dwarf2_attr (die, name, cu);
9605
9606   return (attr && DW_UNSND (attr));
9607 }
9608
9609 static int
9610 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9611 {
9612   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9613      which value is non-zero.  However, we have to be careful with
9614      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9615      (via dwarf2_flag_true_p) follows this attribute.  So we may
9616      end up accidently finding a declaration attribute that belongs
9617      to a different DIE referenced by the specification attribute,
9618      even though the given DIE does not have a declaration attribute.  */
9619   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9620           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9621 }
9622
9623 /* Return the die giving the specification for DIE, if there is
9624    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9625    containing the return value on output.  If there is no
9626    specification, but there is an abstract origin, that is
9627    returned.  */
9628
9629 static struct die_info *
9630 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9631 {
9632   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9633                                              *spec_cu);
9634
9635   if (spec_attr == NULL)
9636     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9637
9638   if (spec_attr == NULL)
9639     return NULL;
9640   else
9641     return follow_die_ref (die, spec_attr, spec_cu);
9642 }
9643
9644 /* Free the line_header structure *LH, and any arrays and strings it
9645    refers to.  */
9646 static void
9647 free_line_header (struct line_header *lh)
9648 {
9649   if (lh->standard_opcode_lengths)
9650     xfree (lh->standard_opcode_lengths);
9651
9652   /* Remember that all the lh->file_names[i].name pointers are
9653      pointers into debug_line_buffer, and don't need to be freed.  */
9654   if (lh->file_names)
9655     xfree (lh->file_names);
9656
9657   /* Similarly for the include directory names.  */
9658   if (lh->include_dirs)
9659     xfree (lh->include_dirs);
9660
9661   xfree (lh);
9662 }
9663
9664
9665 /* Add an entry to LH's include directory table.  */
9666 static void
9667 add_include_dir (struct line_header *lh, char *include_dir)
9668 {
9669   /* Grow the array if necessary.  */
9670   if (lh->include_dirs_size == 0)
9671     {
9672       lh->include_dirs_size = 1; /* for testing */
9673       lh->include_dirs = xmalloc (lh->include_dirs_size
9674                                   * sizeof (*lh->include_dirs));
9675     }
9676   else if (lh->num_include_dirs >= lh->include_dirs_size)
9677     {
9678       lh->include_dirs_size *= 2;
9679       lh->include_dirs = xrealloc (lh->include_dirs,
9680                                    (lh->include_dirs_size
9681                                     * sizeof (*lh->include_dirs)));
9682     }
9683
9684   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9685 }
9686
9687
9688 /* Add an entry to LH's file name table.  */
9689 static void
9690 add_file_name (struct line_header *lh,
9691                char *name,
9692                unsigned int dir_index,
9693                unsigned int mod_time,
9694                unsigned int length)
9695 {
9696   struct file_entry *fe;
9697
9698   /* Grow the array if necessary.  */
9699   if (lh->file_names_size == 0)
9700     {
9701       lh->file_names_size = 1; /* for testing */
9702       lh->file_names = xmalloc (lh->file_names_size
9703                                 * sizeof (*lh->file_names));
9704     }
9705   else if (lh->num_file_names >= lh->file_names_size)
9706     {
9707       lh->file_names_size *= 2;
9708       lh->file_names = xrealloc (lh->file_names,
9709                                  (lh->file_names_size
9710                                   * sizeof (*lh->file_names)));
9711     }
9712
9713   fe = &lh->file_names[lh->num_file_names++];
9714   fe->name = name;
9715   fe->dir_index = dir_index;
9716   fe->mod_time = mod_time;
9717   fe->length = length;
9718   fe->included_p = 0;
9719   fe->symtab = NULL;
9720 }
9721
9722
9723 /* Read the statement program header starting at OFFSET in
9724    .debug_line, according to the endianness of ABFD.  Return a pointer
9725    to a struct line_header, allocated using xmalloc.
9726
9727    NOTE: the strings in the include directory and file name tables of
9728    the returned object point into debug_line_buffer, and must not be
9729    freed.  */
9730 static struct line_header *
9731 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9732                           struct dwarf2_cu *cu)
9733 {
9734   struct cleanup *back_to;
9735   struct line_header *lh;
9736   gdb_byte *line_ptr;
9737   unsigned int bytes_read, offset_size;
9738   int i;
9739   char *cur_dir, *cur_file;
9740
9741   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9742   if (dwarf2_per_objfile->line.buffer == NULL)
9743     {
9744       complaint (&symfile_complaints, _("missing .debug_line section"));
9745       return 0;
9746     }
9747
9748   /* Make sure that at least there's room for the total_length field.
9749      That could be 12 bytes long, but we're just going to fudge that.  */
9750   if (offset + 4 >= dwarf2_per_objfile->line.size)
9751     {
9752       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9753       return 0;
9754     }
9755
9756   lh = xmalloc (sizeof (*lh));
9757   memset (lh, 0, sizeof (*lh));
9758   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9759                           (void *) lh);
9760
9761   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9762
9763   /* Read in the header.  */
9764   lh->total_length =
9765     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9766                                             &bytes_read, &offset_size);
9767   line_ptr += bytes_read;
9768   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9769                                      + dwarf2_per_objfile->line.size))
9770     {
9771       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9772       return 0;
9773     }
9774   lh->statement_program_end = line_ptr + lh->total_length;
9775   lh->version = read_2_bytes (abfd, line_ptr);
9776   line_ptr += 2;
9777   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9778   line_ptr += offset_size;
9779   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9780   line_ptr += 1;
9781   if (lh->version >= 4)
9782     {
9783       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9784       line_ptr += 1;
9785     }
9786   else
9787     lh->maximum_ops_per_instruction = 1;
9788
9789   if (lh->maximum_ops_per_instruction == 0)
9790     {
9791       lh->maximum_ops_per_instruction = 1;
9792       complaint (&symfile_complaints,
9793                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9794     }
9795
9796   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9797   line_ptr += 1;
9798   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9799   line_ptr += 1;
9800   lh->line_range = read_1_byte (abfd, line_ptr);
9801   line_ptr += 1;
9802   lh->opcode_base = read_1_byte (abfd, line_ptr);
9803   line_ptr += 1;
9804   lh->standard_opcode_lengths
9805     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9806
9807   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9808   for (i = 1; i < lh->opcode_base; ++i)
9809     {
9810       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9811       line_ptr += 1;
9812     }
9813
9814   /* Read directory table.  */
9815   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9816     {
9817       line_ptr += bytes_read;
9818       add_include_dir (lh, cur_dir);
9819     }
9820   line_ptr += bytes_read;
9821
9822   /* Read file name table.  */
9823   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9824     {
9825       unsigned int dir_index, mod_time, length;
9826
9827       line_ptr += bytes_read;
9828       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9829       line_ptr += bytes_read;
9830       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9831       line_ptr += bytes_read;
9832       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9833       line_ptr += bytes_read;
9834
9835       add_file_name (lh, cur_file, dir_index, mod_time, length);
9836     }
9837   line_ptr += bytes_read;
9838   lh->statement_program_start = line_ptr;
9839
9840   if (line_ptr > (dwarf2_per_objfile->line.buffer
9841                   + dwarf2_per_objfile->line.size))
9842     complaint (&symfile_complaints,
9843                _("line number info header doesn't fit in `.debug_line' section"));
9844
9845   discard_cleanups (back_to);
9846   return lh;
9847 }
9848
9849 /* This function exists to work around a bug in certain compilers
9850    (particularly GCC 2.95), in which the first line number marker of a
9851    function does not show up until after the prologue, right before
9852    the second line number marker.  This function shifts ADDRESS down
9853    to the beginning of the function if necessary, and is called on
9854    addresses passed to record_line.  */
9855
9856 static CORE_ADDR
9857 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9858 {
9859   struct function_range *fn;
9860
9861   /* Find the function_range containing address.  */
9862   if (!cu->first_fn)
9863     return address;
9864
9865   if (!cu->cached_fn)
9866     cu->cached_fn = cu->first_fn;
9867
9868   fn = cu->cached_fn;
9869   while (fn)
9870     if (fn->lowpc <= address && fn->highpc > address)
9871       goto found;
9872     else
9873       fn = fn->next;
9874
9875   fn = cu->first_fn;
9876   while (fn && fn != cu->cached_fn)
9877     if (fn->lowpc <= address && fn->highpc > address)
9878       goto found;
9879     else
9880       fn = fn->next;
9881
9882   return address;
9883
9884  found:
9885   if (fn->seen_line)
9886     return address;
9887   if (address != fn->lowpc)
9888     complaint (&symfile_complaints,
9889                _("misplaced first line number at 0x%lx for '%s'"),
9890                (unsigned long) address, fn->name);
9891   fn->seen_line = 1;
9892   return fn->lowpc;
9893 }
9894
9895 /* Decode the Line Number Program (LNP) for the given line_header
9896    structure and CU.  The actual information extracted and the type
9897    of structures created from the LNP depends on the value of PST.
9898
9899    1. If PST is NULL, then this procedure uses the data from the program
9900       to create all necessary symbol tables, and their linetables.
9901       The compilation directory of the file is passed in COMP_DIR,
9902       and must not be NULL.
9903
9904    2. If PST is not NULL, this procedure reads the program to determine
9905       the list of files included by the unit represented by PST, and
9906       builds all the associated partial symbol tables.  In this case,
9907       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9908       is not used to compute the full name of the symtab, and therefore
9909       omitting it when building the partial symtab does not introduce
9910       the potential for inconsistency - a partial symtab and its associated
9911       symbtab having a different fullname -).  */
9912
9913 static void
9914 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9915                     struct dwarf2_cu *cu, struct partial_symtab *pst)
9916 {
9917   gdb_byte *line_ptr, *extended_end;
9918   gdb_byte *line_end;
9919   unsigned int bytes_read, extended_len;
9920   unsigned char op_code, extended_op, adj_opcode;
9921   CORE_ADDR baseaddr;
9922   struct objfile *objfile = cu->objfile;
9923   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9924   const int decode_for_pst_p = (pst != NULL);
9925   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9926
9927   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9928
9929   line_ptr = lh->statement_program_start;
9930   line_end = lh->statement_program_end;
9931
9932   /* Read the statement sequences until there's nothing left.  */
9933   while (line_ptr < line_end)
9934     {
9935       /* state machine registers  */
9936       CORE_ADDR address = 0;
9937       unsigned int file = 1;
9938       unsigned int line = 1;
9939       unsigned int column = 0;
9940       int is_stmt = lh->default_is_stmt;
9941       int basic_block = 0;
9942       int end_sequence = 0;
9943       CORE_ADDR addr;
9944       unsigned char op_index = 0;
9945
9946       if (!decode_for_pst_p && lh->num_file_names >= file)
9947         {
9948           /* Start a subfile for the current file of the state machine.  */
9949           /* lh->include_dirs and lh->file_names are 0-based, but the
9950              directory and file name numbers in the statement program
9951              are 1-based.  */
9952           struct file_entry *fe = &lh->file_names[file - 1];
9953           char *dir = NULL;
9954
9955           if (fe->dir_index)
9956             dir = lh->include_dirs[fe->dir_index - 1];
9957
9958           dwarf2_start_subfile (fe->name, dir, comp_dir);
9959         }
9960
9961       /* Decode the table.  */
9962       while (!end_sequence)
9963         {
9964           op_code = read_1_byte (abfd, line_ptr);
9965           line_ptr += 1;
9966           if (line_ptr > line_end)
9967             {
9968               dwarf2_debug_line_missing_end_sequence_complaint ();
9969               break;
9970             }
9971
9972           if (op_code >= lh->opcode_base)
9973             {
9974               /* Special operand.  */
9975               adj_opcode = op_code - lh->opcode_base;
9976               address += (((op_index + (adj_opcode / lh->line_range))
9977                            / lh->maximum_ops_per_instruction)
9978                           * lh->minimum_instruction_length);
9979               op_index = ((op_index + (adj_opcode / lh->line_range))
9980                           % lh->maximum_ops_per_instruction);
9981               line += lh->line_base + (adj_opcode % lh->line_range);
9982               if (lh->num_file_names < file || file == 0)
9983                 dwarf2_debug_line_missing_file_complaint ();
9984               /* For now we ignore lines not starting on an
9985                  instruction boundary.  */
9986               else if (op_index == 0)
9987                 {
9988                   lh->file_names[file - 1].included_p = 1;
9989                   if (!decode_for_pst_p && is_stmt)
9990                     {
9991                       if (last_subfile != current_subfile)
9992                         {
9993                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9994                           if (last_subfile)
9995                             record_line (last_subfile, 0, addr);
9996                           last_subfile = current_subfile;
9997                         }
9998                       /* Append row to matrix using current values.  */
9999                       addr = check_cu_functions (address, cu);
10000                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10001                       record_line (current_subfile, line, addr);
10002                     }
10003                 }
10004               basic_block = 0;
10005             }
10006           else switch (op_code)
10007             {
10008             case DW_LNS_extended_op:
10009               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10010               line_ptr += bytes_read;
10011               extended_end = line_ptr + extended_len;
10012               extended_op = read_1_byte (abfd, line_ptr);
10013               line_ptr += 1;
10014               switch (extended_op)
10015                 {
10016                 case DW_LNE_end_sequence:
10017                   end_sequence = 1;
10018                   break;
10019                 case DW_LNE_set_address:
10020                   address = read_address (abfd, line_ptr, cu, &bytes_read);
10021                   op_index = 0;
10022                   line_ptr += bytes_read;
10023                   address += baseaddr;
10024                   break;
10025                 case DW_LNE_define_file:
10026                   {
10027                     char *cur_file;
10028                     unsigned int dir_index, mod_time, length;
10029
10030                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10031                     line_ptr += bytes_read;
10032                     dir_index =
10033                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10034                     line_ptr += bytes_read;
10035                     mod_time =
10036                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10037                     line_ptr += bytes_read;
10038                     length =
10039                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10040                     line_ptr += bytes_read;
10041                     add_file_name (lh, cur_file, dir_index, mod_time, length);
10042                   }
10043                   break;
10044                 case DW_LNE_set_discriminator:
10045                   /* The discriminator is not interesting to the debugger;
10046                      just ignore it.  */
10047                   line_ptr = extended_end;
10048                   break;
10049                 default:
10050                   complaint (&symfile_complaints,
10051                              _("mangled .debug_line section"));
10052                   return;
10053                 }
10054               /* Make sure that we parsed the extended op correctly.  If e.g.
10055                  we expected a different address size than the producer used,
10056                  we may have read the wrong number of bytes.  */
10057               if (line_ptr != extended_end)
10058                 {
10059                   complaint (&symfile_complaints,
10060                              _("mangled .debug_line section"));
10061                   return;
10062                 }
10063               break;
10064             case DW_LNS_copy:
10065               if (lh->num_file_names < file || file == 0)
10066                 dwarf2_debug_line_missing_file_complaint ();
10067               else
10068                 {
10069                   lh->file_names[file - 1].included_p = 1;
10070                   if (!decode_for_pst_p && is_stmt)
10071                     {
10072                       if (last_subfile != current_subfile)
10073                         {
10074                           addr = gdbarch_addr_bits_remove (gdbarch, address);
10075                           if (last_subfile)
10076                             record_line (last_subfile, 0, addr);
10077                           last_subfile = current_subfile;
10078                         }
10079                       addr = check_cu_functions (address, cu);
10080                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
10081                       record_line (current_subfile, line, addr);
10082                     }
10083                 }
10084               basic_block = 0;
10085               break;
10086             case DW_LNS_advance_pc:
10087               {
10088                 CORE_ADDR adjust
10089                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10090
10091                 address += (((op_index + adjust)
10092                              / lh->maximum_ops_per_instruction)
10093                             * lh->minimum_instruction_length);
10094                 op_index = ((op_index + adjust)
10095                             % lh->maximum_ops_per_instruction);
10096                 line_ptr += bytes_read;
10097               }
10098               break;
10099             case DW_LNS_advance_line:
10100               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10101               line_ptr += bytes_read;
10102               break;
10103             case DW_LNS_set_file:
10104               {
10105                 /* The arrays lh->include_dirs and lh->file_names are
10106                    0-based, but the directory and file name numbers in
10107                    the statement program are 1-based.  */
10108                 struct file_entry *fe;
10109                 char *dir = NULL;
10110
10111                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10112                 line_ptr += bytes_read;
10113                 if (lh->num_file_names < file || file == 0)
10114                   dwarf2_debug_line_missing_file_complaint ();
10115                 else
10116                   {
10117                     fe = &lh->file_names[file - 1];
10118                     if (fe->dir_index)
10119                       dir = lh->include_dirs[fe->dir_index - 1];
10120                     if (!decode_for_pst_p)
10121                       {
10122                         last_subfile = current_subfile;
10123                         dwarf2_start_subfile (fe->name, dir, comp_dir);
10124                       }
10125                   }
10126               }
10127               break;
10128             case DW_LNS_set_column:
10129               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10130               line_ptr += bytes_read;
10131               break;
10132             case DW_LNS_negate_stmt:
10133               is_stmt = (!is_stmt);
10134               break;
10135             case DW_LNS_set_basic_block:
10136               basic_block = 1;
10137               break;
10138             /* Add to the address register of the state machine the
10139                address increment value corresponding to special opcode
10140                255.  I.e., this value is scaled by the minimum
10141                instruction length since special opcode 255 would have
10142                scaled the the increment.  */
10143             case DW_LNS_const_add_pc:
10144               {
10145                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10146
10147                 address += (((op_index + adjust)
10148                              / lh->maximum_ops_per_instruction)
10149                             * lh->minimum_instruction_length);
10150                 op_index = ((op_index + adjust)
10151                             % lh->maximum_ops_per_instruction);
10152               }
10153               break;
10154             case DW_LNS_fixed_advance_pc:
10155               address += read_2_bytes (abfd, line_ptr);
10156               op_index = 0;
10157               line_ptr += 2;
10158               break;
10159             default:
10160               {
10161                 /* Unknown standard opcode, ignore it.  */
10162                 int i;
10163
10164                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10165                   {
10166                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10167                     line_ptr += bytes_read;
10168                   }
10169               }
10170             }
10171         }
10172       if (lh->num_file_names < file || file == 0)
10173         dwarf2_debug_line_missing_file_complaint ();
10174       else
10175         {
10176           lh->file_names[file - 1].included_p = 1;
10177           if (!decode_for_pst_p)
10178             {
10179               addr = gdbarch_addr_bits_remove (gdbarch, address);
10180               record_line (current_subfile, 0, addr);
10181             }
10182         }
10183     }
10184
10185   if (decode_for_pst_p)
10186     {
10187       int file_index;
10188
10189       /* Now that we're done scanning the Line Header Program, we can
10190          create the psymtab of each included file.  */
10191       for (file_index = 0; file_index < lh->num_file_names; file_index++)
10192         if (lh->file_names[file_index].included_p == 1)
10193           {
10194             const struct file_entry fe = lh->file_names [file_index];
10195             char *include_name = fe.name;
10196             char *dir_name = NULL;
10197             char *pst_filename = pst->filename;
10198
10199             if (fe.dir_index)
10200               dir_name = lh->include_dirs[fe.dir_index - 1];
10201
10202             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
10203               {
10204                 include_name = concat (dir_name, SLASH_STRING,
10205                                        include_name, (char *)NULL);
10206                 make_cleanup (xfree, include_name);
10207               }
10208
10209             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10210               {
10211                 pst_filename = concat (pst->dirname, SLASH_STRING,
10212                                        pst_filename, (char *)NULL);
10213                 make_cleanup (xfree, pst_filename);
10214               }
10215
10216             if (strcmp (include_name, pst_filename) != 0)
10217               dwarf2_create_include_psymtab (include_name, pst, objfile);
10218           }
10219     }
10220   else
10221     {
10222       /* Make sure a symtab is created for every file, even files
10223          which contain only variables (i.e. no code with associated
10224          line numbers).  */
10225
10226       int i;
10227       struct file_entry *fe;
10228
10229       for (i = 0; i < lh->num_file_names; i++)
10230         {
10231           char *dir = NULL;
10232
10233           fe = &lh->file_names[i];
10234           if (fe->dir_index)
10235             dir = lh->include_dirs[fe->dir_index - 1];
10236           dwarf2_start_subfile (fe->name, dir, comp_dir);
10237
10238           /* Skip the main file; we don't need it, and it must be
10239              allocated last, so that it will show up before the
10240              non-primary symtabs in the objfile's symtab list.  */
10241           if (current_subfile == first_subfile)
10242             continue;
10243
10244           if (current_subfile->symtab == NULL)
10245             current_subfile->symtab = allocate_symtab (current_subfile->name,
10246                                                        cu->objfile);
10247           fe->symtab = current_subfile->symtab;
10248         }
10249     }
10250 }
10251
10252 /* Start a subfile for DWARF.  FILENAME is the name of the file and
10253    DIRNAME the name of the source directory which contains FILENAME
10254    or NULL if not known.  COMP_DIR is the compilation directory for the
10255    linetable's compilation unit or NULL if not known.
10256    This routine tries to keep line numbers from identical absolute and
10257    relative file names in a common subfile.
10258
10259    Using the `list' example from the GDB testsuite, which resides in
10260    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10261    of /srcdir/list0.c yields the following debugging information for list0.c:
10262
10263    DW_AT_name:          /srcdir/list0.c
10264    DW_AT_comp_dir:              /compdir
10265    files.files[0].name: list0.h
10266    files.files[0].dir:  /srcdir
10267    files.files[1].name: list0.c
10268    files.files[1].dir:  /srcdir
10269
10270    The line number information for list0.c has to end up in a single
10271    subfile, so that `break /srcdir/list0.c:1' works as expected.
10272    start_subfile will ensure that this happens provided that we pass the
10273    concatenation of files.files[1].dir and files.files[1].name as the
10274    subfile's name.  */
10275
10276 static void
10277 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10278 {
10279   char *fullname;
10280
10281   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10282      `start_symtab' will always pass the contents of DW_AT_comp_dir as
10283      second argument to start_subfile.  To be consistent, we do the
10284      same here.  In order not to lose the line information directory,
10285      we concatenate it to the filename when it makes sense.
10286      Note that the Dwarf3 standard says (speaking of filenames in line
10287      information): ``The directory index is ignored for file names
10288      that represent full path names''.  Thus ignoring dirname in the
10289      `else' branch below isn't an issue.  */
10290
10291   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10292     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10293   else
10294     fullname = filename;
10295
10296   start_subfile (fullname, comp_dir);
10297
10298   if (fullname != filename)
10299     xfree (fullname);
10300 }
10301
10302 static void
10303 var_decode_location (struct attribute *attr, struct symbol *sym,
10304                      struct dwarf2_cu *cu)
10305 {
10306   struct objfile *objfile = cu->objfile;
10307   struct comp_unit_head *cu_header = &cu->header;
10308
10309   /* NOTE drow/2003-01-30: There used to be a comment and some special
10310      code here to turn a symbol with DW_AT_external and a
10311      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
10312      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10313      with some versions of binutils) where shared libraries could have
10314      relocations against symbols in their debug information - the
10315      minimal symbol would have the right address, but the debug info
10316      would not.  It's no longer necessary, because we will explicitly
10317      apply relocations when we read in the debug information now.  */
10318
10319   /* A DW_AT_location attribute with no contents indicates that a
10320      variable has been optimized away.  */
10321   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10322     {
10323       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10324       return;
10325     }
10326
10327   /* Handle one degenerate form of location expression specially, to
10328      preserve GDB's previous behavior when section offsets are
10329      specified.  If this is just a DW_OP_addr then mark this symbol
10330      as LOC_STATIC.  */
10331
10332   if (attr_form_is_block (attr)
10333       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10334       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10335     {
10336       unsigned int dummy;
10337
10338       SYMBOL_VALUE_ADDRESS (sym) =
10339         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10340       SYMBOL_CLASS (sym) = LOC_STATIC;
10341       fixup_symbol_section (sym, objfile);
10342       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10343                                               SYMBOL_SECTION (sym));
10344       return;
10345     }
10346
10347   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10348      expression evaluator, and use LOC_COMPUTED only when necessary
10349      (i.e. when the value of a register or memory location is
10350      referenced, or a thread-local block, etc.).  Then again, it might
10351      not be worthwhile.  I'm assuming that it isn't unless performance
10352      or memory numbers show me otherwise.  */
10353
10354   dwarf2_symbol_mark_computed (attr, sym, cu);
10355   SYMBOL_CLASS (sym) = LOC_COMPUTED;
10356 }
10357
10358 /* Given a pointer to a DWARF information entry, figure out if we need
10359    to make a symbol table entry for it, and if so, create a new entry
10360    and return a pointer to it.
10361    If TYPE is NULL, determine symbol type from the die, otherwise
10362    used the passed type.
10363    If SPACE is not NULL, use it to hold the new symbol.  If it is
10364    NULL, allocate a new symbol on the objfile's obstack.  */
10365
10366 static struct symbol *
10367 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10368                  struct symbol *space)
10369 {
10370   struct objfile *objfile = cu->objfile;
10371   struct symbol *sym = NULL;
10372   char *name;
10373   struct attribute *attr = NULL;
10374   struct attribute *attr2 = NULL;
10375   CORE_ADDR baseaddr;
10376   struct pending **list_to_add = NULL;
10377
10378   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10379
10380   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10381
10382   name = dwarf2_name (die, cu);
10383   if (name)
10384     {
10385       const char *linkagename;
10386       int suppress_add = 0;
10387
10388       if (space)
10389         sym = space;
10390       else
10391         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10392       OBJSTAT (objfile, n_syms++);
10393
10394       /* Cache this symbol's name and the name's demangled form (if any).  */
10395       SYMBOL_SET_LANGUAGE (sym, cu->language);
10396       linkagename = dwarf2_physname (name, die, cu);
10397       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10398
10399       /* Fortran does not have mangling standard and the mangling does differ
10400          between gfortran, iFort etc.  */
10401       if (cu->language == language_fortran
10402           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10403         symbol_set_demangled_name (&(sym->ginfo),
10404                                    (char *) dwarf2_full_name (name, die, cu),
10405                                    NULL);
10406
10407       /* Default assumptions.
10408          Use the passed type or decode it from the die.  */
10409       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10410       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10411       if (type != NULL)
10412         SYMBOL_TYPE (sym) = type;
10413       else
10414         SYMBOL_TYPE (sym) = die_type (die, cu);
10415       attr = dwarf2_attr (die,
10416                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10417                           cu);
10418       if (attr)
10419         {
10420           SYMBOL_LINE (sym) = DW_UNSND (attr);
10421         }
10422
10423       attr = dwarf2_attr (die,
10424                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10425                           cu);
10426       if (attr)
10427         {
10428           int file_index = DW_UNSND (attr);
10429
10430           if (cu->line_header == NULL
10431               || file_index > cu->line_header->num_file_names)
10432             complaint (&symfile_complaints,
10433                        _("file index out of range"));
10434           else if (file_index > 0)
10435             {
10436               struct file_entry *fe;
10437
10438               fe = &cu->line_header->file_names[file_index - 1];
10439               SYMBOL_SYMTAB (sym) = fe->symtab;
10440             }
10441         }
10442
10443       switch (die->tag)
10444         {
10445         case DW_TAG_label:
10446           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10447           if (attr)
10448             {
10449               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10450             }
10451           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10452           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10453           SYMBOL_CLASS (sym) = LOC_LABEL;
10454           add_symbol_to_list (sym, cu->list_in_scope);
10455           break;
10456         case DW_TAG_subprogram:
10457           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10458              finish_block.  */
10459           SYMBOL_CLASS (sym) = LOC_BLOCK;
10460           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10461           if ((attr2 && (DW_UNSND (attr2) != 0))
10462               || cu->language == language_ada)
10463             {
10464               /* Subprograms marked external are stored as a global symbol.
10465                  Ada subprograms, whether marked external or not, are always
10466                  stored as a global symbol, because we want to be able to
10467                  access them globally.  For instance, we want to be able
10468                  to break on a nested subprogram without having to
10469                  specify the context.  */
10470               list_to_add = &global_symbols;
10471             }
10472           else
10473             {
10474               list_to_add = cu->list_in_scope;
10475             }
10476           break;
10477         case DW_TAG_inlined_subroutine:
10478           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10479              finish_block.  */
10480           SYMBOL_CLASS (sym) = LOC_BLOCK;
10481           SYMBOL_INLINED (sym) = 1;
10482           /* Do not add the symbol to any lists.  It will be found via
10483              BLOCK_FUNCTION from the blockvector.  */
10484           break;
10485         case DW_TAG_template_value_param:
10486           suppress_add = 1;
10487           /* Fall through.  */
10488         case DW_TAG_variable:
10489         case DW_TAG_member:
10490           /* Compilation with minimal debug info may result in variables
10491              with missing type entries. Change the misleading `void' type
10492              to something sensible.  */
10493           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10494             SYMBOL_TYPE (sym)
10495               = objfile_type (objfile)->nodebug_data_symbol;
10496
10497           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10498           /* In the case of DW_TAG_member, we should only be called for
10499              static const members.  */
10500           if (die->tag == DW_TAG_member)
10501             {
10502               /* dwarf2_add_field uses die_is_declaration,
10503                  so we do the same.  */
10504               gdb_assert (die_is_declaration (die, cu));
10505               gdb_assert (attr);
10506             }
10507           if (attr)
10508             {
10509               dwarf2_const_value (attr, sym, cu);
10510               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10511               if (!suppress_add)
10512                 {
10513                   if (attr2 && (DW_UNSND (attr2) != 0))
10514                     list_to_add = &global_symbols;
10515                   else
10516                     list_to_add = cu->list_in_scope;
10517                 }
10518               break;
10519             }
10520           attr = dwarf2_attr (die, DW_AT_location, cu);
10521           if (attr)
10522             {
10523               var_decode_location (attr, sym, cu);
10524               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10525               if (SYMBOL_CLASS (sym) == LOC_STATIC
10526                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10527                   && !dwarf2_per_objfile->has_section_at_zero)
10528                 {
10529                   /* When a static variable is eliminated by the linker,
10530                      the corresponding debug information is not stripped
10531                      out, but the variable address is set to null;
10532                      do not add such variables into symbol table.  */
10533                 }
10534               else if (attr2 && (DW_UNSND (attr2) != 0))
10535                 {
10536                   /* Workaround gfortran PR debug/40040 - it uses
10537                      DW_AT_location for variables in -fPIC libraries which may
10538                      get overriden by other libraries/executable and get
10539                      a different address.  Resolve it by the minimal symbol
10540                      which may come from inferior's executable using copy
10541                      relocation.  Make this workaround only for gfortran as for
10542                      other compilers GDB cannot guess the minimal symbol
10543                      Fortran mangling kind.  */
10544                   if (cu->language == language_fortran && die->parent
10545                       && die->parent->tag == DW_TAG_module
10546                       && cu->producer
10547                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10548                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10549
10550                   /* A variable with DW_AT_external is never static,
10551                      but it may be block-scoped.  */
10552                   list_to_add = (cu->list_in_scope == &file_symbols
10553                                  ? &global_symbols : cu->list_in_scope);
10554                 }
10555               else
10556                 list_to_add = cu->list_in_scope;
10557             }
10558           else
10559             {
10560               /* We do not know the address of this symbol.
10561                  If it is an external symbol and we have type information
10562                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10563                  The address of the variable will then be determined from
10564                  the minimal symbol table whenever the variable is
10565                  referenced.  */
10566               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10567               if (attr2 && (DW_UNSND (attr2) != 0)
10568                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10569                 {
10570                   /* A variable with DW_AT_external is never static, but it
10571                      may be block-scoped.  */
10572                   list_to_add = (cu->list_in_scope == &file_symbols
10573                                  ? &global_symbols : cu->list_in_scope);
10574
10575                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10576                 }
10577               else if (!die_is_declaration (die, cu))
10578                 {
10579                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10580                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10581                   if (!suppress_add)
10582                     list_to_add = cu->list_in_scope;
10583                 }
10584             }
10585           break;
10586         case DW_TAG_formal_parameter:
10587           /* If we are inside a function, mark this as an argument.  If
10588              not, we might be looking at an argument to an inlined function
10589              when we do not have enough information to show inlined frames;
10590              pretend it's a local variable in that case so that the user can
10591              still see it.  */
10592           if (context_stack_depth > 0
10593               && context_stack[context_stack_depth - 1].name != NULL)
10594             SYMBOL_IS_ARGUMENT (sym) = 1;
10595           attr = dwarf2_attr (die, DW_AT_location, cu);
10596           if (attr)
10597             {
10598               var_decode_location (attr, sym, cu);
10599             }
10600           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10601           if (attr)
10602             {
10603               dwarf2_const_value (attr, sym, cu);
10604             }
10605           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10606           if (attr && DW_UNSND (attr))
10607             {
10608               struct type *ref_type;
10609
10610               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10611               SYMBOL_TYPE (sym) = ref_type;
10612             }
10613
10614           list_to_add = cu->list_in_scope;
10615           break;
10616         case DW_TAG_unspecified_parameters:
10617           /* From varargs functions; gdb doesn't seem to have any
10618              interest in this information, so just ignore it for now.
10619              (FIXME?) */
10620           break;
10621         case DW_TAG_template_type_param:
10622           suppress_add = 1;
10623           /* Fall through.  */
10624         case DW_TAG_class_type:
10625         case DW_TAG_interface_type:
10626         case DW_TAG_structure_type:
10627         case DW_TAG_union_type:
10628         case DW_TAG_set_type:
10629         case DW_TAG_enumeration_type:
10630           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10631           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10632
10633           {
10634             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10635                really ever be static objects: otherwise, if you try
10636                to, say, break of a class's method and you're in a file
10637                which doesn't mention that class, it won't work unless
10638                the check for all static symbols in lookup_symbol_aux
10639                saves you.  See the OtherFileClass tests in
10640                gdb.c++/namespace.exp.  */
10641
10642             if (!suppress_add)
10643               {
10644                 list_to_add = (cu->list_in_scope == &file_symbols
10645                                && (cu->language == language_cplus
10646                                    || cu->language == language_java)
10647                                ? &global_symbols : cu->list_in_scope);
10648               }
10649
10650             /* The semantics of C++ state that "struct foo { ... }" also
10651                defines a typedef for "foo".  A Java class declaration also
10652                defines a typedef for the class.  */
10653             if (cu->language == language_cplus
10654                 || cu->language == language_java
10655                 || cu->language == language_ada)
10656               {
10657                 /* The symbol's name is already allocated along with
10658                    this objfile, so we don't need to duplicate it for
10659                    the type.  */
10660                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10661                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10662               }
10663           }
10664           break;
10665         case DW_TAG_typedef:
10666           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10667           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10668           list_to_add = cu->list_in_scope;
10669           break;
10670         case DW_TAG_base_type:
10671         case DW_TAG_subrange_type:
10672           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10673           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10674           list_to_add = cu->list_in_scope;
10675           break;
10676         case DW_TAG_enumerator:
10677           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10678           if (attr)
10679             {
10680               dwarf2_const_value (attr, sym, cu);
10681             }
10682           {
10683             /* NOTE: carlton/2003-11-10: See comment above in the
10684                DW_TAG_class_type, etc. block.  */
10685
10686             list_to_add = (cu->list_in_scope == &file_symbols
10687                            && (cu->language == language_cplus
10688                                || cu->language == language_java)
10689                            ? &global_symbols : cu->list_in_scope);
10690           }
10691           break;
10692         case DW_TAG_namespace:
10693           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10694           list_to_add = &global_symbols;
10695           break;
10696         default:
10697           /* Not a tag we recognize.  Hopefully we aren't processing
10698              trash data, but since we must specifically ignore things
10699              we don't recognize, there is nothing else we should do at
10700              this point. */
10701           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10702                      dwarf_tag_name (die->tag));
10703           break;
10704         }
10705
10706       if (suppress_add)
10707         {
10708           sym->hash_next = objfile->template_symbols;
10709           objfile->template_symbols = sym;
10710           list_to_add = NULL;
10711         }
10712
10713       if (list_to_add != NULL)
10714         add_symbol_to_list (sym, list_to_add);
10715
10716       /* For the benefit of old versions of GCC, check for anonymous
10717          namespaces based on the demangled name.  */
10718       if (!processing_has_namespace_info
10719           && cu->language == language_cplus)
10720         cp_scan_for_anonymous_namespaces (sym);
10721     }
10722   return (sym);
10723 }
10724
10725 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
10726
10727 static struct symbol *
10728 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10729 {
10730   return new_symbol_full (die, type, cu, NULL);
10731 }
10732
10733 /* Given an attr with a DW_FORM_dataN value in host byte order,
10734    zero-extend it as appropriate for the symbol's type.  The DWARF
10735    standard (v4) is not entirely clear about the meaning of using
10736    DW_FORM_dataN for a constant with a signed type, where the type is
10737    wider than the data.  The conclusion of a discussion on the DWARF
10738    list was that this is unspecified.  We choose to always zero-extend
10739    because that is the interpretation long in use by GCC.  */
10740
10741 static gdb_byte *
10742 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10743                          const char *name, struct obstack *obstack,
10744                          struct dwarf2_cu *cu, long *value, int bits)
10745 {
10746   struct objfile *objfile = cu->objfile;
10747   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10748                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10749   LONGEST l = DW_UNSND (attr);
10750
10751   if (bits < sizeof (*value) * 8)
10752     {
10753       l &= ((LONGEST) 1 << bits) - 1;
10754       *value = l;
10755     }
10756   else if (bits == sizeof (*value) * 8)
10757     *value = l;
10758   else
10759     {
10760       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10761       store_unsigned_integer (bytes, bits / 8, byte_order, l);
10762       return bytes;
10763     }
10764
10765   return NULL;
10766 }
10767
10768 /* Read a constant value from an attribute.  Either set *VALUE, or if
10769    the value does not fit in *VALUE, set *BYTES - either already
10770    allocated on the objfile obstack, or newly allocated on OBSTACK,
10771    or, set *BATON, if we translated the constant to a location
10772    expression.  */
10773
10774 static void
10775 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10776                          const char *name, struct obstack *obstack,
10777                          struct dwarf2_cu *cu,
10778                          long *value, gdb_byte **bytes,
10779                          struct dwarf2_locexpr_baton **baton)
10780 {
10781   struct objfile *objfile = cu->objfile;
10782   struct comp_unit_head *cu_header = &cu->header;
10783   struct dwarf_block *blk;
10784   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10785                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10786
10787   *value = 0;
10788   *bytes = NULL;
10789   *baton = NULL;
10790
10791   switch (attr->form)
10792     {
10793     case DW_FORM_addr:
10794       {
10795         gdb_byte *data;
10796
10797         if (TYPE_LENGTH (type) != cu_header->addr_size)
10798           dwarf2_const_value_length_mismatch_complaint (name,
10799                                                         cu_header->addr_size,
10800                                                         TYPE_LENGTH (type));
10801         /* Symbols of this form are reasonably rare, so we just
10802            piggyback on the existing location code rather than writing
10803            a new implementation of symbol_computed_ops.  */
10804         *baton = obstack_alloc (&objfile->objfile_obstack,
10805                                 sizeof (struct dwarf2_locexpr_baton));
10806         (*baton)->per_cu = cu->per_cu;
10807         gdb_assert ((*baton)->per_cu);
10808
10809         (*baton)->size = 2 + cu_header->addr_size;
10810         data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10811         (*baton)->data = data;
10812
10813         data[0] = DW_OP_addr;
10814         store_unsigned_integer (&data[1], cu_header->addr_size,
10815                                 byte_order, DW_ADDR (attr));
10816         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10817       }
10818       break;
10819     case DW_FORM_string:
10820     case DW_FORM_strp:
10821       /* DW_STRING is already allocated on the objfile obstack, point
10822          directly to it.  */
10823       *bytes = (gdb_byte *) DW_STRING (attr);
10824       break;
10825     case DW_FORM_block1:
10826     case DW_FORM_block2:
10827     case DW_FORM_block4:
10828     case DW_FORM_block:
10829     case DW_FORM_exprloc:
10830       blk = DW_BLOCK (attr);
10831       if (TYPE_LENGTH (type) != blk->size)
10832         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10833                                                       TYPE_LENGTH (type));
10834       *bytes = blk->data;
10835       break;
10836
10837       /* The DW_AT_const_value attributes are supposed to carry the
10838          symbol's value "represented as it would be on the target
10839          architecture."  By the time we get here, it's already been
10840          converted to host endianness, so we just need to sign- or
10841          zero-extend it as appropriate.  */
10842     case DW_FORM_data1:
10843       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10844       break;
10845     case DW_FORM_data2:
10846       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10847       break;
10848     case DW_FORM_data4:
10849       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10850       break;
10851     case DW_FORM_data8:
10852       *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10853       break;
10854
10855     case DW_FORM_sdata:
10856       *value = DW_SND (attr);
10857       break;
10858
10859     case DW_FORM_udata:
10860       *value = DW_UNSND (attr);
10861       break;
10862
10863     default:
10864       complaint (&symfile_complaints,
10865                  _("unsupported const value attribute form: '%s'"),
10866                  dwarf_form_name (attr->form));
10867       *value = 0;
10868       break;
10869     }
10870 }
10871
10872
10873 /* Copy constant value from an attribute to a symbol.  */
10874
10875 static void
10876 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10877                     struct dwarf2_cu *cu)
10878 {
10879   struct objfile *objfile = cu->objfile;
10880   struct comp_unit_head *cu_header = &cu->header;
10881   long value;
10882   gdb_byte *bytes;
10883   struct dwarf2_locexpr_baton *baton;
10884
10885   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10886                            SYMBOL_PRINT_NAME (sym),
10887                            &objfile->objfile_obstack, cu,
10888                            &value, &bytes, &baton);
10889
10890   if (baton != NULL)
10891     {
10892       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10893       SYMBOL_LOCATION_BATON (sym) = baton;
10894       SYMBOL_CLASS (sym) = LOC_COMPUTED;
10895     }
10896   else if (bytes != NULL)
10897      {
10898       SYMBOL_VALUE_BYTES (sym) = bytes;
10899       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10900     }
10901   else
10902     {
10903       SYMBOL_VALUE (sym) = value;
10904       SYMBOL_CLASS (sym) = LOC_CONST;
10905     }
10906 }
10907
10908
10909 /* Return the type of the die in question using its DW_AT_type attribute.  */
10910
10911 static struct type *
10912 die_type (struct die_info *die, struct dwarf2_cu *cu)
10913 {
10914   struct attribute *type_attr;
10915   struct die_info *type_die;
10916
10917   type_attr = dwarf2_attr (die, DW_AT_type, cu);
10918   if (!type_attr)
10919     {
10920       /* A missing DW_AT_type represents a void type.  */
10921       return objfile_type (cu->objfile)->builtin_void;
10922     }
10923
10924   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10925
10926   return tag_type_to_type (type_die, cu);
10927 }
10928
10929 /* True iff CU's producer generates GNAT Ada auxiliary information
10930    that allows to find parallel types through that information instead
10931    of having to do expensive parallel lookups by type name.  */
10932
10933 static int
10934 need_gnat_info (struct dwarf2_cu *cu)
10935 {
10936   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10937      of GNAT produces this auxiliary information, without any indication
10938      that it is produced.  Part of enhancing the FSF version of GNAT
10939      to produce that information will be to put in place an indicator
10940      that we can use in order to determine whether the descriptive type
10941      info is available or not.  One suggestion that has been made is
10942      to use a new attribute, attached to the CU die.  For now, assume
10943      that the descriptive type info is not available.  */
10944   return 0;
10945 }
10946
10947
10948 /* Return the auxiliary type of the die in question using its
10949    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
10950    attribute is not present.  */
10951
10952 static struct type *
10953 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10954 {
10955   struct attribute *type_attr;
10956   struct die_info *type_die;
10957
10958   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10959   if (!type_attr)
10960     return NULL;
10961
10962   type_die = follow_die_ref (die, type_attr, &cu);
10963   return tag_type_to_type (type_die, cu);
10964 }
10965
10966 /* If DIE has a descriptive_type attribute, then set the TYPE's
10967    descriptive type accordingly.  */
10968
10969 static void
10970 set_descriptive_type (struct type *type, struct die_info *die,
10971                       struct dwarf2_cu *cu)
10972 {
10973   struct type *descriptive_type = die_descriptive_type (die, cu);
10974
10975   if (descriptive_type)
10976     {
10977       ALLOCATE_GNAT_AUX_TYPE (type);
10978       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10979     }
10980 }
10981
10982 /* Return the containing type of the die in question using its
10983    DW_AT_containing_type attribute.  */
10984
10985 static struct type *
10986 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10987 {
10988   struct attribute *type_attr;
10989   struct die_info *type_die;
10990
10991   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10992   if (!type_attr)
10993     error (_("Dwarf Error: Problem turning containing type into gdb type "
10994              "[in module %s]"), cu->objfile->name);
10995
10996   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10997   return tag_type_to_type (type_die, cu);
10998 }
10999
11000 static struct type *
11001 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
11002 {
11003   struct type *this_type;
11004
11005   this_type = read_type_die (die, cu);
11006   if (!this_type)
11007     {
11008       char *message, *saved;
11009
11010       /* read_type_die already issued a complaint.  */
11011       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11012                             cu->objfile->name,
11013                             cu->header.offset,
11014                             die->offset);
11015       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11016                              message, strlen (message));
11017       xfree (message);
11018
11019       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11020     }
11021   return this_type;
11022 }
11023
11024 static struct type *
11025 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11026 {
11027   struct type *this_type;
11028
11029   this_type = get_die_type (die, cu);
11030   if (this_type)
11031     return this_type;
11032
11033   switch (die->tag)
11034     {
11035     case DW_TAG_class_type:
11036     case DW_TAG_interface_type:
11037     case DW_TAG_structure_type:
11038     case DW_TAG_union_type:
11039       this_type = read_structure_type (die, cu);
11040       break;
11041     case DW_TAG_enumeration_type:
11042       this_type = read_enumeration_type (die, cu);
11043       break;
11044     case DW_TAG_subprogram:
11045     case DW_TAG_subroutine_type:
11046     case DW_TAG_inlined_subroutine:
11047       this_type = read_subroutine_type (die, cu);
11048       break;
11049     case DW_TAG_array_type:
11050       this_type = read_array_type (die, cu);
11051       break;
11052     case DW_TAG_set_type:
11053       this_type = read_set_type (die, cu);
11054       break;
11055     case DW_TAG_pointer_type:
11056       this_type = read_tag_pointer_type (die, cu);
11057       break;
11058     case DW_TAG_ptr_to_member_type:
11059       this_type = read_tag_ptr_to_member_type (die, cu);
11060       break;
11061     case DW_TAG_reference_type:
11062       this_type = read_tag_reference_type (die, cu);
11063       break;
11064     case DW_TAG_const_type:
11065       this_type = read_tag_const_type (die, cu);
11066       break;
11067     case DW_TAG_volatile_type:
11068       this_type = read_tag_volatile_type (die, cu);
11069       break;
11070     case DW_TAG_string_type:
11071       this_type = read_tag_string_type (die, cu);
11072       break;
11073     case DW_TAG_typedef:
11074       this_type = read_typedef (die, cu);
11075       break;
11076     case DW_TAG_subrange_type:
11077       this_type = read_subrange_type (die, cu);
11078       break;
11079     case DW_TAG_base_type:
11080       this_type = read_base_type (die, cu);
11081       break;
11082     case DW_TAG_unspecified_type:
11083       this_type = read_unspecified_type (die, cu);
11084       break;
11085     case DW_TAG_namespace:
11086       this_type = read_namespace_type (die, cu);
11087       break;
11088     case DW_TAG_module:
11089       this_type = read_module_type (die, cu);
11090       break;
11091     default:
11092       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11093                  dwarf_tag_name (die->tag));
11094       break;
11095     }
11096
11097   return this_type;
11098 }
11099
11100 /* Return the name of the namespace/class that DIE is defined within,
11101    or "" if we can't tell.  The caller should not xfree the result.
11102
11103    For example, if we're within the method foo() in the following
11104    code:
11105
11106    namespace N {
11107      class C {
11108        void foo () {
11109        }
11110      };
11111    }
11112
11113    then determine_prefix on foo's die will return "N::C".  */
11114
11115 static char *
11116 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11117 {
11118   struct die_info *parent, *spec_die;
11119   struct dwarf2_cu *spec_cu;
11120   struct type *parent_type;
11121
11122   if (cu->language != language_cplus && cu->language != language_java
11123       && cu->language != language_fortran)
11124     return "";
11125
11126   /* We have to be careful in the presence of DW_AT_specification.
11127      For example, with GCC 3.4, given the code
11128
11129      namespace N {
11130        void foo() {
11131          // Definition of N::foo.
11132        }
11133      }
11134
11135      then we'll have a tree of DIEs like this:
11136
11137      1: DW_TAG_compile_unit
11138        2: DW_TAG_namespace        // N
11139          3: DW_TAG_subprogram     // declaration of N::foo
11140        4: DW_TAG_subprogram       // definition of N::foo
11141             DW_AT_specification   // refers to die #3
11142
11143      Thus, when processing die #4, we have to pretend that we're in
11144      the context of its DW_AT_specification, namely the contex of die
11145      #3.  */
11146   spec_cu = cu;
11147   spec_die = die_specification (die, &spec_cu);
11148   if (spec_die == NULL)
11149     parent = die->parent;
11150   else
11151     {
11152       parent = spec_die->parent;
11153       cu = spec_cu;
11154     }
11155
11156   if (parent == NULL)
11157     return "";
11158   else if (parent->building_fullname)
11159     {
11160       const char *name;
11161       const char *parent_name;
11162
11163       /* It has been seen on RealView 2.2 built binaries,
11164          DW_TAG_template_type_param types actually _defined_ as
11165          children of the parent class:
11166
11167          enum E {};
11168          template class <class Enum> Class{};
11169          Class<enum E> class_e;
11170
11171          1: DW_TAG_class_type (Class)
11172            2: DW_TAG_enumeration_type (E)
11173              3: DW_TAG_enumerator (enum1:0)
11174              3: DW_TAG_enumerator (enum2:1)
11175              ...
11176            2: DW_TAG_template_type_param
11177               DW_AT_type  DW_FORM_ref_udata (E)
11178
11179          Besides being broken debug info, it can put GDB into an
11180          infinite loop.  Consider:
11181
11182          When we're building the full name for Class<E>, we'll start
11183          at Class, and go look over its template type parameters,
11184          finding E.  We'll then try to build the full name of E, and
11185          reach here.  We're now trying to build the full name of E,
11186          and look over the parent DIE for containing scope.  In the
11187          broken case, if we followed the parent DIE of E, we'd again
11188          find Class, and once again go look at its template type
11189          arguments, etc., etc.  Simply don't consider such parent die
11190          as source-level parent of this die (it can't be, the language
11191          doesn't allow it), and break the loop here.  */
11192       name = dwarf2_name (die, cu);
11193       parent_name = dwarf2_name (parent, cu);
11194       complaint (&symfile_complaints,
11195                  _("template param type '%s' defined within parent '%s'"),
11196                  name ? name : "<unknown>",
11197                  parent_name ? parent_name : "<unknown>");
11198       return "";
11199     }
11200   else
11201     switch (parent->tag)
11202       {
11203       case DW_TAG_namespace:
11204         parent_type = read_type_die (parent, cu);
11205         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11206            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11207            Work around this problem here.  */
11208         if (cu->language == language_cplus
11209             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11210           return "";
11211         /* We give a name to even anonymous namespaces.  */
11212         return TYPE_TAG_NAME (parent_type);
11213       case DW_TAG_class_type:
11214       case DW_TAG_interface_type:
11215       case DW_TAG_structure_type:
11216       case DW_TAG_union_type:
11217       case DW_TAG_module:
11218         parent_type = read_type_die (parent, cu);
11219         if (TYPE_TAG_NAME (parent_type) != NULL)
11220           return TYPE_TAG_NAME (parent_type);
11221         else
11222           /* An anonymous structure is only allowed non-static data
11223              members; no typedefs, no member functions, et cetera.
11224              So it does not need a prefix.  */
11225           return "";
11226       default:
11227         return determine_prefix (parent, cu);
11228       }
11229 }
11230
11231 /* Return a newly-allocated string formed by concatenating PREFIX and
11232    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
11233    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
11234    perform an obconcat, otherwise allocate storage for the result.  The CU argument
11235    is used to determine the language and hence, the appropriate separator.  */
11236
11237 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
11238
11239 static char *
11240 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11241                  int physname, struct dwarf2_cu *cu)
11242 {
11243   const char *lead = "";
11244   const char *sep;
11245
11246   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11247     sep = "";
11248   else if (cu->language == language_java)
11249     sep = ".";
11250   else if (cu->language == language_fortran && physname)
11251     {
11252       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
11253          DW_AT_MIPS_linkage_name is preferred and used instead.  */
11254
11255       lead = "__";
11256       sep = "_MOD_";
11257     }
11258   else
11259     sep = "::";
11260
11261   if (prefix == NULL)
11262     prefix = "";
11263   if (suffix == NULL)
11264     suffix = "";
11265
11266   if (obs == NULL)
11267     {
11268       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11269
11270       strcpy (retval, lead);
11271       strcat (retval, prefix);
11272       strcat (retval, sep);
11273       strcat (retval, suffix);
11274       return retval;
11275     }
11276   else
11277     {
11278       /* We have an obstack.  */
11279       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11280     }
11281 }
11282
11283 /* Return sibling of die, NULL if no sibling.  */
11284
11285 static struct die_info *
11286 sibling_die (struct die_info *die)
11287 {
11288   return die->sibling;
11289 }
11290
11291 /* Get name of a die, return NULL if not found.  */
11292
11293 static char *
11294 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11295                           struct obstack *obstack)
11296 {
11297   if (name && cu->language == language_cplus)
11298     {
11299       char *canon_name = cp_canonicalize_string (name);
11300
11301       if (canon_name != NULL)
11302         {
11303           if (strcmp (canon_name, name) != 0)
11304             name = obsavestring (canon_name, strlen (canon_name),
11305                                  obstack);
11306           xfree (canon_name);
11307         }
11308     }
11309
11310   return name;
11311 }
11312
11313 /* Get name of a die, return NULL if not found.  */
11314
11315 static char *
11316 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11317 {
11318   struct attribute *attr;
11319
11320   attr = dwarf2_attr (die, DW_AT_name, cu);
11321   if (!attr || !DW_STRING (attr))
11322     return NULL;
11323
11324   switch (die->tag)
11325     {
11326     case DW_TAG_compile_unit:
11327       /* Compilation units have a DW_AT_name that is a filename, not
11328          a source language identifier.  */
11329     case DW_TAG_enumeration_type:
11330     case DW_TAG_enumerator:
11331       /* These tags always have simple identifiers already; no need
11332          to canonicalize them.  */
11333       return DW_STRING (attr);
11334
11335     case DW_TAG_subprogram:
11336       /* Java constructors will all be named "<init>", so return
11337          the class name when we see this special case.  */
11338       if (cu->language == language_java
11339           && DW_STRING (attr) != NULL
11340           && strcmp (DW_STRING (attr), "<init>") == 0)
11341         {
11342           struct dwarf2_cu *spec_cu = cu;
11343           struct die_info *spec_die;
11344
11345           /* GCJ will output '<init>' for Java constructor names.
11346              For this special case, return the name of the parent class.  */
11347
11348           /* GCJ may output suprogram DIEs with AT_specification set.
11349              If so, use the name of the specified DIE.  */
11350           spec_die = die_specification (die, &spec_cu);
11351           if (spec_die != NULL)
11352             return dwarf2_name (spec_die, spec_cu);
11353
11354           do
11355             {
11356               die = die->parent;
11357               if (die->tag == DW_TAG_class_type)
11358                 return dwarf2_name (die, cu);
11359             }
11360           while (die->tag != DW_TAG_compile_unit);
11361         }
11362       break;
11363
11364     case DW_TAG_class_type:
11365     case DW_TAG_interface_type:
11366     case DW_TAG_structure_type:
11367     case DW_TAG_union_type:
11368       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11369          structures or unions.  These were of the form "._%d" in GCC 4.1,
11370          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11371          and GCC 4.4.  We work around this problem by ignoring these.  */
11372       if (strncmp (DW_STRING (attr), "._", 2) == 0
11373           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11374         return NULL;
11375       break;
11376
11377     default:
11378       break;
11379     }
11380
11381   if (!DW_STRING_IS_CANONICAL (attr))
11382     {
11383       DW_STRING (attr)
11384         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11385                                     &cu->objfile->objfile_obstack);
11386       DW_STRING_IS_CANONICAL (attr) = 1;
11387     }
11388   return DW_STRING (attr);
11389 }
11390
11391 /* Return the die that this die in an extension of, or NULL if there
11392    is none.  *EXT_CU is the CU containing DIE on input, and the CU
11393    containing the return value on output.  */
11394
11395 static struct die_info *
11396 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11397 {
11398   struct attribute *attr;
11399
11400   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11401   if (attr == NULL)
11402     return NULL;
11403
11404   return follow_die_ref (die, attr, ext_cu);
11405 }
11406
11407 /* Convert a DIE tag into its string name.  */
11408
11409 static char *
11410 dwarf_tag_name (unsigned tag)
11411 {
11412   switch (tag)
11413     {
11414     case DW_TAG_padding:
11415       return "DW_TAG_padding";
11416     case DW_TAG_array_type:
11417       return "DW_TAG_array_type";
11418     case DW_TAG_class_type:
11419       return "DW_TAG_class_type";
11420     case DW_TAG_entry_point:
11421       return "DW_TAG_entry_point";
11422     case DW_TAG_enumeration_type:
11423       return "DW_TAG_enumeration_type";
11424     case DW_TAG_formal_parameter:
11425       return "DW_TAG_formal_parameter";
11426     case DW_TAG_imported_declaration:
11427       return "DW_TAG_imported_declaration";
11428     case DW_TAG_label:
11429       return "DW_TAG_label";
11430     case DW_TAG_lexical_block:
11431       return "DW_TAG_lexical_block";
11432     case DW_TAG_member:
11433       return "DW_TAG_member";
11434     case DW_TAG_pointer_type:
11435       return "DW_TAG_pointer_type";
11436     case DW_TAG_reference_type:
11437       return "DW_TAG_reference_type";
11438     case DW_TAG_compile_unit:
11439       return "DW_TAG_compile_unit";
11440     case DW_TAG_string_type:
11441       return "DW_TAG_string_type";
11442     case DW_TAG_structure_type:
11443       return "DW_TAG_structure_type";
11444     case DW_TAG_subroutine_type:
11445       return "DW_TAG_subroutine_type";
11446     case DW_TAG_typedef:
11447       return "DW_TAG_typedef";
11448     case DW_TAG_union_type:
11449       return "DW_TAG_union_type";
11450     case DW_TAG_unspecified_parameters:
11451       return "DW_TAG_unspecified_parameters";
11452     case DW_TAG_variant:
11453       return "DW_TAG_variant";
11454     case DW_TAG_common_block:
11455       return "DW_TAG_common_block";
11456     case DW_TAG_common_inclusion:
11457       return "DW_TAG_common_inclusion";
11458     case DW_TAG_inheritance:
11459       return "DW_TAG_inheritance";
11460     case DW_TAG_inlined_subroutine:
11461       return "DW_TAG_inlined_subroutine";
11462     case DW_TAG_module:
11463       return "DW_TAG_module";
11464     case DW_TAG_ptr_to_member_type:
11465       return "DW_TAG_ptr_to_member_type";
11466     case DW_TAG_set_type:
11467       return "DW_TAG_set_type";
11468     case DW_TAG_subrange_type:
11469       return "DW_TAG_subrange_type";
11470     case DW_TAG_with_stmt:
11471       return "DW_TAG_with_stmt";
11472     case DW_TAG_access_declaration:
11473       return "DW_TAG_access_declaration";
11474     case DW_TAG_base_type:
11475       return "DW_TAG_base_type";
11476     case DW_TAG_catch_block:
11477       return "DW_TAG_catch_block";
11478     case DW_TAG_const_type:
11479       return "DW_TAG_const_type";
11480     case DW_TAG_constant:
11481       return "DW_TAG_constant";
11482     case DW_TAG_enumerator:
11483       return "DW_TAG_enumerator";
11484     case DW_TAG_file_type:
11485       return "DW_TAG_file_type";
11486     case DW_TAG_friend:
11487       return "DW_TAG_friend";
11488     case DW_TAG_namelist:
11489       return "DW_TAG_namelist";
11490     case DW_TAG_namelist_item:
11491       return "DW_TAG_namelist_item";
11492     case DW_TAG_packed_type:
11493       return "DW_TAG_packed_type";
11494     case DW_TAG_subprogram:
11495       return "DW_TAG_subprogram";
11496     case DW_TAG_template_type_param:
11497       return "DW_TAG_template_type_param";
11498     case DW_TAG_template_value_param:
11499       return "DW_TAG_template_value_param";
11500     case DW_TAG_thrown_type:
11501       return "DW_TAG_thrown_type";
11502     case DW_TAG_try_block:
11503       return "DW_TAG_try_block";
11504     case DW_TAG_variant_part:
11505       return "DW_TAG_variant_part";
11506     case DW_TAG_variable:
11507       return "DW_TAG_variable";
11508     case DW_TAG_volatile_type:
11509       return "DW_TAG_volatile_type";
11510     case DW_TAG_dwarf_procedure:
11511       return "DW_TAG_dwarf_procedure";
11512     case DW_TAG_restrict_type:
11513       return "DW_TAG_restrict_type";
11514     case DW_TAG_interface_type:
11515       return "DW_TAG_interface_type";
11516     case DW_TAG_namespace:
11517       return "DW_TAG_namespace";
11518     case DW_TAG_imported_module:
11519       return "DW_TAG_imported_module";
11520     case DW_TAG_unspecified_type:
11521       return "DW_TAG_unspecified_type";
11522     case DW_TAG_partial_unit:
11523       return "DW_TAG_partial_unit";
11524     case DW_TAG_imported_unit:
11525       return "DW_TAG_imported_unit";
11526     case DW_TAG_condition:
11527       return "DW_TAG_condition";
11528     case DW_TAG_shared_type:
11529       return "DW_TAG_shared_type";
11530     case DW_TAG_type_unit:
11531       return "DW_TAG_type_unit";
11532     case DW_TAG_MIPS_loop:
11533       return "DW_TAG_MIPS_loop";
11534     case DW_TAG_HP_array_descriptor:
11535       return "DW_TAG_HP_array_descriptor";
11536     case DW_TAG_format_label:
11537       return "DW_TAG_format_label";
11538     case DW_TAG_function_template:
11539       return "DW_TAG_function_template";
11540     case DW_TAG_class_template:
11541       return "DW_TAG_class_template";
11542     case DW_TAG_GNU_BINCL:
11543       return "DW_TAG_GNU_BINCL";
11544     case DW_TAG_GNU_EINCL:
11545       return "DW_TAG_GNU_EINCL";
11546     case DW_TAG_upc_shared_type:
11547       return "DW_TAG_upc_shared_type";
11548     case DW_TAG_upc_strict_type:
11549       return "DW_TAG_upc_strict_type";
11550     case DW_TAG_upc_relaxed_type:
11551       return "DW_TAG_upc_relaxed_type";
11552     case DW_TAG_PGI_kanji_type:
11553       return "DW_TAG_PGI_kanji_type";
11554     case DW_TAG_PGI_interface_block:
11555       return "DW_TAG_PGI_interface_block";
11556     default:
11557       return "DW_TAG_<unknown>";
11558     }
11559 }
11560
11561 /* Convert a DWARF attribute code into its string name.  */
11562
11563 static char *
11564 dwarf_attr_name (unsigned attr)
11565 {
11566   switch (attr)
11567     {
11568     case DW_AT_sibling:
11569       return "DW_AT_sibling";
11570     case DW_AT_location:
11571       return "DW_AT_location";
11572     case DW_AT_name:
11573       return "DW_AT_name";
11574     case DW_AT_ordering:
11575       return "DW_AT_ordering";
11576     case DW_AT_subscr_data:
11577       return "DW_AT_subscr_data";
11578     case DW_AT_byte_size:
11579       return "DW_AT_byte_size";
11580     case DW_AT_bit_offset:
11581       return "DW_AT_bit_offset";
11582     case DW_AT_bit_size:
11583       return "DW_AT_bit_size";
11584     case DW_AT_element_list:
11585       return "DW_AT_element_list";
11586     case DW_AT_stmt_list:
11587       return "DW_AT_stmt_list";
11588     case DW_AT_low_pc:
11589       return "DW_AT_low_pc";
11590     case DW_AT_high_pc:
11591       return "DW_AT_high_pc";
11592     case DW_AT_language:
11593       return "DW_AT_language";
11594     case DW_AT_member:
11595       return "DW_AT_member";
11596     case DW_AT_discr:
11597       return "DW_AT_discr";
11598     case DW_AT_discr_value:
11599       return "DW_AT_discr_value";
11600     case DW_AT_visibility:
11601       return "DW_AT_visibility";
11602     case DW_AT_import:
11603       return "DW_AT_import";
11604     case DW_AT_string_length:
11605       return "DW_AT_string_length";
11606     case DW_AT_common_reference:
11607       return "DW_AT_common_reference";
11608     case DW_AT_comp_dir:
11609       return "DW_AT_comp_dir";
11610     case DW_AT_const_value:
11611       return "DW_AT_const_value";
11612     case DW_AT_containing_type:
11613       return "DW_AT_containing_type";
11614     case DW_AT_default_value:
11615       return "DW_AT_default_value";
11616     case DW_AT_inline:
11617       return "DW_AT_inline";
11618     case DW_AT_is_optional:
11619       return "DW_AT_is_optional";
11620     case DW_AT_lower_bound:
11621       return "DW_AT_lower_bound";
11622     case DW_AT_producer:
11623       return "DW_AT_producer";
11624     case DW_AT_prototyped:
11625       return "DW_AT_prototyped";
11626     case DW_AT_return_addr:
11627       return "DW_AT_return_addr";
11628     case DW_AT_start_scope:
11629       return "DW_AT_start_scope";
11630     case DW_AT_bit_stride:
11631       return "DW_AT_bit_stride";
11632     case DW_AT_upper_bound:
11633       return "DW_AT_upper_bound";
11634     case DW_AT_abstract_origin:
11635       return "DW_AT_abstract_origin";
11636     case DW_AT_accessibility:
11637       return "DW_AT_accessibility";
11638     case DW_AT_address_class:
11639       return "DW_AT_address_class";
11640     case DW_AT_artificial:
11641       return "DW_AT_artificial";
11642     case DW_AT_base_types:
11643       return "DW_AT_base_types";
11644     case DW_AT_calling_convention:
11645       return "DW_AT_calling_convention";
11646     case DW_AT_count:
11647       return "DW_AT_count";
11648     case DW_AT_data_member_location:
11649       return "DW_AT_data_member_location";
11650     case DW_AT_decl_column:
11651       return "DW_AT_decl_column";
11652     case DW_AT_decl_file:
11653       return "DW_AT_decl_file";
11654     case DW_AT_decl_line:
11655       return "DW_AT_decl_line";
11656     case DW_AT_declaration:
11657       return "DW_AT_declaration";
11658     case DW_AT_discr_list:
11659       return "DW_AT_discr_list";
11660     case DW_AT_encoding:
11661       return "DW_AT_encoding";
11662     case DW_AT_external:
11663       return "DW_AT_external";
11664     case DW_AT_frame_base:
11665       return "DW_AT_frame_base";
11666     case DW_AT_friend:
11667       return "DW_AT_friend";
11668     case DW_AT_identifier_case:
11669       return "DW_AT_identifier_case";
11670     case DW_AT_macro_info:
11671       return "DW_AT_macro_info";
11672     case DW_AT_namelist_items:
11673       return "DW_AT_namelist_items";
11674     case DW_AT_priority:
11675       return "DW_AT_priority";
11676     case DW_AT_segment:
11677       return "DW_AT_segment";
11678     case DW_AT_specification:
11679       return "DW_AT_specification";
11680     case DW_AT_static_link:
11681       return "DW_AT_static_link";
11682     case DW_AT_type:
11683       return "DW_AT_type";
11684     case DW_AT_use_location:
11685       return "DW_AT_use_location";
11686     case DW_AT_variable_parameter:
11687       return "DW_AT_variable_parameter";
11688     case DW_AT_virtuality:
11689       return "DW_AT_virtuality";
11690     case DW_AT_vtable_elem_location:
11691       return "DW_AT_vtable_elem_location";
11692     /* DWARF 3 values.  */
11693     case DW_AT_allocated:
11694       return "DW_AT_allocated";
11695     case DW_AT_associated:
11696       return "DW_AT_associated";
11697     case DW_AT_data_location:
11698       return "DW_AT_data_location";
11699     case DW_AT_byte_stride:
11700       return "DW_AT_byte_stride";
11701     case DW_AT_entry_pc:
11702       return "DW_AT_entry_pc";
11703     case DW_AT_use_UTF8:
11704       return "DW_AT_use_UTF8";
11705     case DW_AT_extension:
11706       return "DW_AT_extension";
11707     case DW_AT_ranges:
11708       return "DW_AT_ranges";
11709     case DW_AT_trampoline:
11710       return "DW_AT_trampoline";
11711     case DW_AT_call_column:
11712       return "DW_AT_call_column";
11713     case DW_AT_call_file:
11714       return "DW_AT_call_file";
11715     case DW_AT_call_line:
11716       return "DW_AT_call_line";
11717     case DW_AT_description:
11718       return "DW_AT_description";
11719     case DW_AT_binary_scale:
11720       return "DW_AT_binary_scale";
11721     case DW_AT_decimal_scale:
11722       return "DW_AT_decimal_scale";
11723     case DW_AT_small:
11724       return "DW_AT_small";
11725     case DW_AT_decimal_sign:
11726       return "DW_AT_decimal_sign";
11727     case DW_AT_digit_count:
11728       return "DW_AT_digit_count";
11729     case DW_AT_picture_string:
11730       return "DW_AT_picture_string";
11731     case DW_AT_mutable:
11732       return "DW_AT_mutable";
11733     case DW_AT_threads_scaled:
11734       return "DW_AT_threads_scaled";
11735     case DW_AT_explicit:
11736       return "DW_AT_explicit";
11737     case DW_AT_object_pointer:
11738       return "DW_AT_object_pointer";
11739     case DW_AT_endianity:
11740       return "DW_AT_endianity";
11741     case DW_AT_elemental:
11742       return "DW_AT_elemental";
11743     case DW_AT_pure:
11744       return "DW_AT_pure";
11745     case DW_AT_recursive:
11746       return "DW_AT_recursive";
11747     /* DWARF 4 values.  */
11748     case DW_AT_signature:
11749       return "DW_AT_signature";
11750     case DW_AT_linkage_name:
11751       return "DW_AT_linkage_name";
11752     /* SGI/MIPS extensions.  */
11753 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11754     case DW_AT_MIPS_fde:
11755       return "DW_AT_MIPS_fde";
11756 #endif
11757     case DW_AT_MIPS_loop_begin:
11758       return "DW_AT_MIPS_loop_begin";
11759     case DW_AT_MIPS_tail_loop_begin:
11760       return "DW_AT_MIPS_tail_loop_begin";
11761     case DW_AT_MIPS_epilog_begin:
11762       return "DW_AT_MIPS_epilog_begin";
11763     case DW_AT_MIPS_loop_unroll_factor:
11764       return "DW_AT_MIPS_loop_unroll_factor";
11765     case DW_AT_MIPS_software_pipeline_depth:
11766       return "DW_AT_MIPS_software_pipeline_depth";
11767     case DW_AT_MIPS_linkage_name:
11768       return "DW_AT_MIPS_linkage_name";
11769     case DW_AT_MIPS_stride:
11770       return "DW_AT_MIPS_stride";
11771     case DW_AT_MIPS_abstract_name:
11772       return "DW_AT_MIPS_abstract_name";
11773     case DW_AT_MIPS_clone_origin:
11774       return "DW_AT_MIPS_clone_origin";
11775     case DW_AT_MIPS_has_inlines:
11776       return "DW_AT_MIPS_has_inlines";
11777     /* HP extensions.  */
11778 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11779     case DW_AT_HP_block_index:
11780       return "DW_AT_HP_block_index";
11781 #endif
11782     case DW_AT_HP_unmodifiable:
11783       return "DW_AT_HP_unmodifiable";
11784     case DW_AT_HP_actuals_stmt_list:
11785       return "DW_AT_HP_actuals_stmt_list";
11786     case DW_AT_HP_proc_per_section:
11787       return "DW_AT_HP_proc_per_section";
11788     case DW_AT_HP_raw_data_ptr:
11789       return "DW_AT_HP_raw_data_ptr";
11790     case DW_AT_HP_pass_by_reference:
11791       return "DW_AT_HP_pass_by_reference";
11792     case DW_AT_HP_opt_level:
11793       return "DW_AT_HP_opt_level";
11794     case DW_AT_HP_prof_version_id:
11795       return "DW_AT_HP_prof_version_id";
11796     case DW_AT_HP_opt_flags:
11797       return "DW_AT_HP_opt_flags";
11798     case DW_AT_HP_cold_region_low_pc:
11799       return "DW_AT_HP_cold_region_low_pc";
11800     case DW_AT_HP_cold_region_high_pc:
11801       return "DW_AT_HP_cold_region_high_pc";
11802     case DW_AT_HP_all_variables_modifiable:
11803       return "DW_AT_HP_all_variables_modifiable";
11804     case DW_AT_HP_linkage_name:
11805       return "DW_AT_HP_linkage_name";
11806     case DW_AT_HP_prof_flags:
11807       return "DW_AT_HP_prof_flags";
11808     /* GNU extensions.  */
11809     case DW_AT_sf_names:
11810       return "DW_AT_sf_names";
11811     case DW_AT_src_info:
11812       return "DW_AT_src_info";
11813     case DW_AT_mac_info:
11814       return "DW_AT_mac_info";
11815     case DW_AT_src_coords:
11816       return "DW_AT_src_coords";
11817     case DW_AT_body_begin:
11818       return "DW_AT_body_begin";
11819     case DW_AT_body_end:
11820       return "DW_AT_body_end";
11821     case DW_AT_GNU_vector:
11822       return "DW_AT_GNU_vector";
11823     case DW_AT_GNU_odr_signature:
11824       return "DW_AT_GNU_odr_signature";
11825     /* VMS extensions.  */
11826     case DW_AT_VMS_rtnbeg_pd_address:
11827       return "DW_AT_VMS_rtnbeg_pd_address";
11828     /* UPC extension.  */
11829     case DW_AT_upc_threads_scaled:
11830       return "DW_AT_upc_threads_scaled";
11831     /* PGI (STMicroelectronics) extensions.  */
11832     case DW_AT_PGI_lbase:
11833       return "DW_AT_PGI_lbase";
11834     case DW_AT_PGI_soffset:
11835       return "DW_AT_PGI_soffset";
11836     case DW_AT_PGI_lstride:
11837       return "DW_AT_PGI_lstride";
11838     default:
11839       return "DW_AT_<unknown>";
11840     }
11841 }
11842
11843 /* Convert a DWARF value form code into its string name.  */
11844
11845 static char *
11846 dwarf_form_name (unsigned form)
11847 {
11848   switch (form)
11849     {
11850     case DW_FORM_addr:
11851       return "DW_FORM_addr";
11852     case DW_FORM_block2:
11853       return "DW_FORM_block2";
11854     case DW_FORM_block4:
11855       return "DW_FORM_block4";
11856     case DW_FORM_data2:
11857       return "DW_FORM_data2";
11858     case DW_FORM_data4:
11859       return "DW_FORM_data4";
11860     case DW_FORM_data8:
11861       return "DW_FORM_data8";
11862     case DW_FORM_string:
11863       return "DW_FORM_string";
11864     case DW_FORM_block:
11865       return "DW_FORM_block";
11866     case DW_FORM_block1:
11867       return "DW_FORM_block1";
11868     case DW_FORM_data1:
11869       return "DW_FORM_data1";
11870     case DW_FORM_flag:
11871       return "DW_FORM_flag";
11872     case DW_FORM_sdata:
11873       return "DW_FORM_sdata";
11874     case DW_FORM_strp:
11875       return "DW_FORM_strp";
11876     case DW_FORM_udata:
11877       return "DW_FORM_udata";
11878     case DW_FORM_ref_addr:
11879       return "DW_FORM_ref_addr";
11880     case DW_FORM_ref1:
11881       return "DW_FORM_ref1";
11882     case DW_FORM_ref2:
11883       return "DW_FORM_ref2";
11884     case DW_FORM_ref4:
11885       return "DW_FORM_ref4";
11886     case DW_FORM_ref8:
11887       return "DW_FORM_ref8";
11888     case DW_FORM_ref_udata:
11889       return "DW_FORM_ref_udata";
11890     case DW_FORM_indirect:
11891       return "DW_FORM_indirect";
11892     case DW_FORM_sec_offset:
11893       return "DW_FORM_sec_offset";
11894     case DW_FORM_exprloc:
11895       return "DW_FORM_exprloc";
11896     case DW_FORM_flag_present:
11897       return "DW_FORM_flag_present";
11898     case DW_FORM_sig8:
11899       return "DW_FORM_sig8";
11900     default:
11901       return "DW_FORM_<unknown>";
11902     }
11903 }
11904
11905 /* Convert a DWARF stack opcode into its string name.  */
11906
11907 const char *
11908 dwarf_stack_op_name (unsigned op, int def)
11909 {
11910   switch (op)
11911     {
11912     case DW_OP_addr:
11913       return "DW_OP_addr";
11914     case DW_OP_deref:
11915       return "DW_OP_deref";
11916     case DW_OP_const1u:
11917       return "DW_OP_const1u";
11918     case DW_OP_const1s:
11919       return "DW_OP_const1s";
11920     case DW_OP_const2u:
11921       return "DW_OP_const2u";
11922     case DW_OP_const2s:
11923       return "DW_OP_const2s";
11924     case DW_OP_const4u:
11925       return "DW_OP_const4u";
11926     case DW_OP_const4s:
11927       return "DW_OP_const4s";
11928     case DW_OP_const8u:
11929       return "DW_OP_const8u";
11930     case DW_OP_const8s:
11931       return "DW_OP_const8s";
11932     case DW_OP_constu:
11933       return "DW_OP_constu";
11934     case DW_OP_consts:
11935       return "DW_OP_consts";
11936     case DW_OP_dup:
11937       return "DW_OP_dup";
11938     case DW_OP_drop:
11939       return "DW_OP_drop";
11940     case DW_OP_over:
11941       return "DW_OP_over";
11942     case DW_OP_pick:
11943       return "DW_OP_pick";
11944     case DW_OP_swap:
11945       return "DW_OP_swap";
11946     case DW_OP_rot:
11947       return "DW_OP_rot";
11948     case DW_OP_xderef:
11949       return "DW_OP_xderef";
11950     case DW_OP_abs:
11951       return "DW_OP_abs";
11952     case DW_OP_and:
11953       return "DW_OP_and";
11954     case DW_OP_div:
11955       return "DW_OP_div";
11956     case DW_OP_minus:
11957       return "DW_OP_minus";
11958     case DW_OP_mod:
11959       return "DW_OP_mod";
11960     case DW_OP_mul:
11961       return "DW_OP_mul";
11962     case DW_OP_neg:
11963       return "DW_OP_neg";
11964     case DW_OP_not:
11965       return "DW_OP_not";
11966     case DW_OP_or:
11967       return "DW_OP_or";
11968     case DW_OP_plus:
11969       return "DW_OP_plus";
11970     case DW_OP_plus_uconst:
11971       return "DW_OP_plus_uconst";
11972     case DW_OP_shl:
11973       return "DW_OP_shl";
11974     case DW_OP_shr:
11975       return "DW_OP_shr";
11976     case DW_OP_shra:
11977       return "DW_OP_shra";
11978     case DW_OP_xor:
11979       return "DW_OP_xor";
11980     case DW_OP_bra:
11981       return "DW_OP_bra";
11982     case DW_OP_eq:
11983       return "DW_OP_eq";
11984     case DW_OP_ge:
11985       return "DW_OP_ge";
11986     case DW_OP_gt:
11987       return "DW_OP_gt";
11988     case DW_OP_le:
11989       return "DW_OP_le";
11990     case DW_OP_lt:
11991       return "DW_OP_lt";
11992     case DW_OP_ne:
11993       return "DW_OP_ne";
11994     case DW_OP_skip:
11995       return "DW_OP_skip";
11996     case DW_OP_lit0:
11997       return "DW_OP_lit0";
11998     case DW_OP_lit1:
11999       return "DW_OP_lit1";
12000     case DW_OP_lit2:
12001       return "DW_OP_lit2";
12002     case DW_OP_lit3:
12003       return "DW_OP_lit3";
12004     case DW_OP_lit4:
12005       return "DW_OP_lit4";
12006     case DW_OP_lit5:
12007       return "DW_OP_lit5";
12008     case DW_OP_lit6:
12009       return "DW_OP_lit6";
12010     case DW_OP_lit7:
12011       return "DW_OP_lit7";
12012     case DW_OP_lit8:
12013       return "DW_OP_lit8";
12014     case DW_OP_lit9:
12015       return "DW_OP_lit9";
12016     case DW_OP_lit10:
12017       return "DW_OP_lit10";
12018     case DW_OP_lit11:
12019       return "DW_OP_lit11";
12020     case DW_OP_lit12:
12021       return "DW_OP_lit12";
12022     case DW_OP_lit13:
12023       return "DW_OP_lit13";
12024     case DW_OP_lit14:
12025       return "DW_OP_lit14";
12026     case DW_OP_lit15:
12027       return "DW_OP_lit15";
12028     case DW_OP_lit16:
12029       return "DW_OP_lit16";
12030     case DW_OP_lit17:
12031       return "DW_OP_lit17";
12032     case DW_OP_lit18:
12033       return "DW_OP_lit18";
12034     case DW_OP_lit19:
12035       return "DW_OP_lit19";
12036     case DW_OP_lit20:
12037       return "DW_OP_lit20";
12038     case DW_OP_lit21:
12039       return "DW_OP_lit21";
12040     case DW_OP_lit22:
12041       return "DW_OP_lit22";
12042     case DW_OP_lit23:
12043       return "DW_OP_lit23";
12044     case DW_OP_lit24:
12045       return "DW_OP_lit24";
12046     case DW_OP_lit25:
12047       return "DW_OP_lit25";
12048     case DW_OP_lit26:
12049       return "DW_OP_lit26";
12050     case DW_OP_lit27:
12051       return "DW_OP_lit27";
12052     case DW_OP_lit28:
12053       return "DW_OP_lit28";
12054     case DW_OP_lit29:
12055       return "DW_OP_lit29";
12056     case DW_OP_lit30:
12057       return "DW_OP_lit30";
12058     case DW_OP_lit31:
12059       return "DW_OP_lit31";
12060     case DW_OP_reg0:
12061       return "DW_OP_reg0";
12062     case DW_OP_reg1:
12063       return "DW_OP_reg1";
12064     case DW_OP_reg2:
12065       return "DW_OP_reg2";
12066     case DW_OP_reg3:
12067       return "DW_OP_reg3";
12068     case DW_OP_reg4:
12069       return "DW_OP_reg4";
12070     case DW_OP_reg5:
12071       return "DW_OP_reg5";
12072     case DW_OP_reg6:
12073       return "DW_OP_reg6";
12074     case DW_OP_reg7:
12075       return "DW_OP_reg7";
12076     case DW_OP_reg8:
12077       return "DW_OP_reg8";
12078     case DW_OP_reg9:
12079       return "DW_OP_reg9";
12080     case DW_OP_reg10:
12081       return "DW_OP_reg10";
12082     case DW_OP_reg11:
12083       return "DW_OP_reg11";
12084     case DW_OP_reg12:
12085       return "DW_OP_reg12";
12086     case DW_OP_reg13:
12087       return "DW_OP_reg13";
12088     case DW_OP_reg14:
12089       return "DW_OP_reg14";
12090     case DW_OP_reg15:
12091       return "DW_OP_reg15";
12092     case DW_OP_reg16:
12093       return "DW_OP_reg16";
12094     case DW_OP_reg17:
12095       return "DW_OP_reg17";
12096     case DW_OP_reg18:
12097       return "DW_OP_reg18";
12098     case DW_OP_reg19:
12099       return "DW_OP_reg19";
12100     case DW_OP_reg20:
12101       return "DW_OP_reg20";
12102     case DW_OP_reg21:
12103       return "DW_OP_reg21";
12104     case DW_OP_reg22:
12105       return "DW_OP_reg22";
12106     case DW_OP_reg23:
12107       return "DW_OP_reg23";
12108     case DW_OP_reg24:
12109       return "DW_OP_reg24";
12110     case DW_OP_reg25:
12111       return "DW_OP_reg25";
12112     case DW_OP_reg26:
12113       return "DW_OP_reg26";
12114     case DW_OP_reg27:
12115       return "DW_OP_reg27";
12116     case DW_OP_reg28:
12117       return "DW_OP_reg28";
12118     case DW_OP_reg29:
12119       return "DW_OP_reg29";
12120     case DW_OP_reg30:
12121       return "DW_OP_reg30";
12122     case DW_OP_reg31:
12123       return "DW_OP_reg31";
12124     case DW_OP_breg0:
12125       return "DW_OP_breg0";
12126     case DW_OP_breg1:
12127       return "DW_OP_breg1";
12128     case DW_OP_breg2:
12129       return "DW_OP_breg2";
12130     case DW_OP_breg3:
12131       return "DW_OP_breg3";
12132     case DW_OP_breg4:
12133       return "DW_OP_breg4";
12134     case DW_OP_breg5:
12135       return "DW_OP_breg5";
12136     case DW_OP_breg6:
12137       return "DW_OP_breg6";
12138     case DW_OP_breg7:
12139       return "DW_OP_breg7";
12140     case DW_OP_breg8:
12141       return "DW_OP_breg8";
12142     case DW_OP_breg9:
12143       return "DW_OP_breg9";
12144     case DW_OP_breg10:
12145       return "DW_OP_breg10";
12146     case DW_OP_breg11:
12147       return "DW_OP_breg11";
12148     case DW_OP_breg12:
12149       return "DW_OP_breg12";
12150     case DW_OP_breg13:
12151       return "DW_OP_breg13";
12152     case DW_OP_breg14:
12153       return "DW_OP_breg14";
12154     case DW_OP_breg15:
12155       return "DW_OP_breg15";
12156     case DW_OP_breg16:
12157       return "DW_OP_breg16";
12158     case DW_OP_breg17:
12159       return "DW_OP_breg17";
12160     case DW_OP_breg18:
12161       return "DW_OP_breg18";
12162     case DW_OP_breg19:
12163       return "DW_OP_breg19";
12164     case DW_OP_breg20:
12165       return "DW_OP_breg20";
12166     case DW_OP_breg21:
12167       return "DW_OP_breg21";
12168     case DW_OP_breg22:
12169       return "DW_OP_breg22";
12170     case DW_OP_breg23:
12171       return "DW_OP_breg23";
12172     case DW_OP_breg24:
12173       return "DW_OP_breg24";
12174     case DW_OP_breg25:
12175       return "DW_OP_breg25";
12176     case DW_OP_breg26:
12177       return "DW_OP_breg26";
12178     case DW_OP_breg27:
12179       return "DW_OP_breg27";
12180     case DW_OP_breg28:
12181       return "DW_OP_breg28";
12182     case DW_OP_breg29:
12183       return "DW_OP_breg29";
12184     case DW_OP_breg30:
12185       return "DW_OP_breg30";
12186     case DW_OP_breg31:
12187       return "DW_OP_breg31";
12188     case DW_OP_regx:
12189       return "DW_OP_regx";
12190     case DW_OP_fbreg:
12191       return "DW_OP_fbreg";
12192     case DW_OP_bregx:
12193       return "DW_OP_bregx";
12194     case DW_OP_piece:
12195       return "DW_OP_piece";
12196     case DW_OP_deref_size:
12197       return "DW_OP_deref_size";
12198     case DW_OP_xderef_size:
12199       return "DW_OP_xderef_size";
12200     case DW_OP_nop:
12201       return "DW_OP_nop";
12202     /* DWARF 3 extensions.  */
12203     case DW_OP_push_object_address:
12204       return "DW_OP_push_object_address";
12205     case DW_OP_call2:
12206       return "DW_OP_call2";
12207     case DW_OP_call4:
12208       return "DW_OP_call4";
12209     case DW_OP_call_ref:
12210       return "DW_OP_call_ref";
12211     case DW_OP_form_tls_address:
12212       return "DW_OP_form_tls_address";
12213     case DW_OP_call_frame_cfa:
12214       return "DW_OP_call_frame_cfa";
12215     case DW_OP_bit_piece:
12216       return "DW_OP_bit_piece";
12217     /* DWARF 4 extensions.  */
12218     case DW_OP_implicit_value:
12219       return "DW_OP_implicit_value";
12220     case DW_OP_stack_value:
12221       return "DW_OP_stack_value";
12222     /* GNU extensions.  */
12223     case DW_OP_GNU_push_tls_address:
12224       return "DW_OP_GNU_push_tls_address";
12225     case DW_OP_GNU_uninit:
12226       return "DW_OP_GNU_uninit";
12227     default:
12228       return def ? "OP_<unknown>" : NULL;
12229     }
12230 }
12231
12232 static char *
12233 dwarf_bool_name (unsigned mybool)
12234 {
12235   if (mybool)
12236     return "TRUE";
12237   else
12238     return "FALSE";
12239 }
12240
12241 /* Convert a DWARF type code into its string name.  */
12242
12243 static char *
12244 dwarf_type_encoding_name (unsigned enc)
12245 {
12246   switch (enc)
12247     {
12248     case DW_ATE_void:
12249       return "DW_ATE_void";
12250     case DW_ATE_address:
12251       return "DW_ATE_address";
12252     case DW_ATE_boolean:
12253       return "DW_ATE_boolean";
12254     case DW_ATE_complex_float:
12255       return "DW_ATE_complex_float";
12256     case DW_ATE_float:
12257       return "DW_ATE_float";
12258     case DW_ATE_signed:
12259       return "DW_ATE_signed";
12260     case DW_ATE_signed_char:
12261       return "DW_ATE_signed_char";
12262     case DW_ATE_unsigned:
12263       return "DW_ATE_unsigned";
12264     case DW_ATE_unsigned_char:
12265       return "DW_ATE_unsigned_char";
12266     /* DWARF 3.  */
12267     case DW_ATE_imaginary_float:
12268       return "DW_ATE_imaginary_float";
12269     case DW_ATE_packed_decimal:
12270       return "DW_ATE_packed_decimal";
12271     case DW_ATE_numeric_string:
12272       return "DW_ATE_numeric_string";
12273     case DW_ATE_edited:
12274       return "DW_ATE_edited";
12275     case DW_ATE_signed_fixed:
12276       return "DW_ATE_signed_fixed";
12277     case DW_ATE_unsigned_fixed:
12278       return "DW_ATE_unsigned_fixed";
12279     case DW_ATE_decimal_float:
12280       return "DW_ATE_decimal_float";
12281     /* DWARF 4.  */
12282     case DW_ATE_UTF:
12283       return "DW_ATE_UTF";
12284     /* HP extensions.  */
12285     case DW_ATE_HP_float80:
12286       return "DW_ATE_HP_float80";
12287     case DW_ATE_HP_complex_float80:
12288       return "DW_ATE_HP_complex_float80";
12289     case DW_ATE_HP_float128:
12290       return "DW_ATE_HP_float128";
12291     case DW_ATE_HP_complex_float128:
12292       return "DW_ATE_HP_complex_float128";
12293     case DW_ATE_HP_floathpintel:
12294       return "DW_ATE_HP_floathpintel";
12295     case DW_ATE_HP_imaginary_float80:
12296       return "DW_ATE_HP_imaginary_float80";
12297     case DW_ATE_HP_imaginary_float128:
12298       return "DW_ATE_HP_imaginary_float128";
12299     default:
12300       return "DW_ATE_<unknown>";
12301     }
12302 }
12303
12304 /* Convert a DWARF call frame info operation to its string name. */
12305
12306 #if 0
12307 static char *
12308 dwarf_cfi_name (unsigned cfi_opc)
12309 {
12310   switch (cfi_opc)
12311     {
12312     case DW_CFA_advance_loc:
12313       return "DW_CFA_advance_loc";
12314     case DW_CFA_offset:
12315       return "DW_CFA_offset";
12316     case DW_CFA_restore:
12317       return "DW_CFA_restore";
12318     case DW_CFA_nop:
12319       return "DW_CFA_nop";
12320     case DW_CFA_set_loc:
12321       return "DW_CFA_set_loc";
12322     case DW_CFA_advance_loc1:
12323       return "DW_CFA_advance_loc1";
12324     case DW_CFA_advance_loc2:
12325       return "DW_CFA_advance_loc2";
12326     case DW_CFA_advance_loc4:
12327       return "DW_CFA_advance_loc4";
12328     case DW_CFA_offset_extended:
12329       return "DW_CFA_offset_extended";
12330     case DW_CFA_restore_extended:
12331       return "DW_CFA_restore_extended";
12332     case DW_CFA_undefined:
12333       return "DW_CFA_undefined";
12334     case DW_CFA_same_value:
12335       return "DW_CFA_same_value";
12336     case DW_CFA_register:
12337       return "DW_CFA_register";
12338     case DW_CFA_remember_state:
12339       return "DW_CFA_remember_state";
12340     case DW_CFA_restore_state:
12341       return "DW_CFA_restore_state";
12342     case DW_CFA_def_cfa:
12343       return "DW_CFA_def_cfa";
12344     case DW_CFA_def_cfa_register:
12345       return "DW_CFA_def_cfa_register";
12346     case DW_CFA_def_cfa_offset:
12347       return "DW_CFA_def_cfa_offset";
12348     /* DWARF 3.  */
12349     case DW_CFA_def_cfa_expression:
12350       return "DW_CFA_def_cfa_expression";
12351     case DW_CFA_expression:
12352       return "DW_CFA_expression";
12353     case DW_CFA_offset_extended_sf:
12354       return "DW_CFA_offset_extended_sf";
12355     case DW_CFA_def_cfa_sf:
12356       return "DW_CFA_def_cfa_sf";
12357     case DW_CFA_def_cfa_offset_sf:
12358       return "DW_CFA_def_cfa_offset_sf";
12359     case DW_CFA_val_offset:
12360       return "DW_CFA_val_offset";
12361     case DW_CFA_val_offset_sf:
12362       return "DW_CFA_val_offset_sf";
12363     case DW_CFA_val_expression:
12364       return "DW_CFA_val_expression";
12365     /* SGI/MIPS specific.  */
12366     case DW_CFA_MIPS_advance_loc8:
12367       return "DW_CFA_MIPS_advance_loc8";
12368     /* GNU extensions.  */
12369     case DW_CFA_GNU_window_save:
12370       return "DW_CFA_GNU_window_save";
12371     case DW_CFA_GNU_args_size:
12372       return "DW_CFA_GNU_args_size";
12373     case DW_CFA_GNU_negative_offset_extended:
12374       return "DW_CFA_GNU_negative_offset_extended";
12375     default:
12376       return "DW_CFA_<unknown>";
12377     }
12378 }
12379 #endif
12380
12381 static void
12382 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12383 {
12384   unsigned int i;
12385
12386   print_spaces (indent, f);
12387   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12388            dwarf_tag_name (die->tag), die->abbrev, die->offset);
12389
12390   if (die->parent != NULL)
12391     {
12392       print_spaces (indent, f);
12393       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
12394                           die->parent->offset);
12395     }
12396
12397   print_spaces (indent, f);
12398   fprintf_unfiltered (f, "  has children: %s\n",
12399            dwarf_bool_name (die->child != NULL));
12400
12401   print_spaces (indent, f);
12402   fprintf_unfiltered (f, "  attributes:\n");
12403
12404   for (i = 0; i < die->num_attrs; ++i)
12405     {
12406       print_spaces (indent, f);
12407       fprintf_unfiltered (f, "    %s (%s) ",
12408                dwarf_attr_name (die->attrs[i].name),
12409                dwarf_form_name (die->attrs[i].form));
12410
12411       switch (die->attrs[i].form)
12412         {
12413         case DW_FORM_ref_addr:
12414         case DW_FORM_addr:
12415           fprintf_unfiltered (f, "address: ");
12416           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12417           break;
12418         case DW_FORM_block2:
12419         case DW_FORM_block4:
12420         case DW_FORM_block:
12421         case DW_FORM_block1:
12422           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12423           break;
12424         case DW_FORM_exprloc:
12425           fprintf_unfiltered (f, "expression: size %u",
12426                               DW_BLOCK (&die->attrs[i])->size);
12427           break;
12428         case DW_FORM_ref1:
12429         case DW_FORM_ref2:
12430         case DW_FORM_ref4:
12431           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12432                               (long) (DW_ADDR (&die->attrs[i])));
12433           break;
12434         case DW_FORM_data1:
12435         case DW_FORM_data2:
12436         case DW_FORM_data4:
12437         case DW_FORM_data8:
12438         case DW_FORM_udata:
12439         case DW_FORM_sdata:
12440           fprintf_unfiltered (f, "constant: %s",
12441                               pulongest (DW_UNSND (&die->attrs[i])));
12442           break;
12443         case DW_FORM_sec_offset:
12444           fprintf_unfiltered (f, "section offset: %s",
12445                               pulongest (DW_UNSND (&die->attrs[i])));
12446           break;
12447         case DW_FORM_sig8:
12448           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12449             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12450                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12451           else
12452             fprintf_unfiltered (f, "signatured type, offset: unknown");
12453           break;
12454         case DW_FORM_string:
12455         case DW_FORM_strp:
12456           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12457                    DW_STRING (&die->attrs[i])
12458                    ? DW_STRING (&die->attrs[i]) : "",
12459                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12460           break;
12461         case DW_FORM_flag:
12462           if (DW_UNSND (&die->attrs[i]))
12463             fprintf_unfiltered (f, "flag: TRUE");
12464           else
12465             fprintf_unfiltered (f, "flag: FALSE");
12466           break;
12467         case DW_FORM_flag_present:
12468           fprintf_unfiltered (f, "flag: TRUE");
12469           break;
12470         case DW_FORM_indirect:
12471           /* the reader will have reduced the indirect form to
12472              the "base form" so this form should not occur */
12473           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12474           break;
12475         default:
12476           fprintf_unfiltered (f, "unsupported attribute form: %d.",
12477                    die->attrs[i].form);
12478           break;
12479         }
12480       fprintf_unfiltered (f, "\n");
12481     }
12482 }
12483
12484 static void
12485 dump_die_for_error (struct die_info *die)
12486 {
12487   dump_die_shallow (gdb_stderr, 0, die);
12488 }
12489
12490 static void
12491 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12492 {
12493   int indent = level * 4;
12494
12495   gdb_assert (die != NULL);
12496
12497   if (level >= max_level)
12498     return;
12499
12500   dump_die_shallow (f, indent, die);
12501
12502   if (die->child != NULL)
12503     {
12504       print_spaces (indent, f);
12505       fprintf_unfiltered (f, "  Children:");
12506       if (level + 1 < max_level)
12507         {
12508           fprintf_unfiltered (f, "\n");
12509           dump_die_1 (f, level + 1, max_level, die->child);
12510         }
12511       else
12512         {
12513           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12514         }
12515     }
12516
12517   if (die->sibling != NULL && level > 0)
12518     {
12519       dump_die_1 (f, level, max_level, die->sibling);
12520     }
12521 }
12522
12523 /* This is called from the pdie macro in gdbinit.in.
12524    It's not static so gcc will keep a copy callable from gdb.  */
12525
12526 void
12527 dump_die (struct die_info *die, int max_level)
12528 {
12529   dump_die_1 (gdb_stdlog, 0, max_level, die);
12530 }
12531
12532 static void
12533 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12534 {
12535   void **slot;
12536
12537   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12538
12539   *slot = die;
12540 }
12541
12542 static int
12543 is_ref_attr (struct attribute *attr)
12544 {
12545   switch (attr->form)
12546     {
12547     case DW_FORM_ref_addr:
12548     case DW_FORM_ref1:
12549     case DW_FORM_ref2:
12550     case DW_FORM_ref4:
12551     case DW_FORM_ref8:
12552     case DW_FORM_ref_udata:
12553       return 1;
12554     default:
12555       return 0;
12556     }
12557 }
12558
12559 static unsigned int
12560 dwarf2_get_ref_die_offset (struct attribute *attr)
12561 {
12562   if (is_ref_attr (attr))
12563     return DW_ADDR (attr);
12564
12565   complaint (&symfile_complaints,
12566              _("unsupported die ref attribute form: '%s'"),
12567              dwarf_form_name (attr->form));
12568   return 0;
12569 }
12570
12571 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
12572  * the value held by the attribute is not constant.  */
12573
12574 static LONGEST
12575 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12576 {
12577   if (attr->form == DW_FORM_sdata)
12578     return DW_SND (attr);
12579   else if (attr->form == DW_FORM_udata
12580            || attr->form == DW_FORM_data1
12581            || attr->form == DW_FORM_data2
12582            || attr->form == DW_FORM_data4
12583            || attr->form == DW_FORM_data8)
12584     return DW_UNSND (attr);
12585   else
12586     {
12587       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12588                  dwarf_form_name (attr->form));
12589       return default_value;
12590     }
12591 }
12592
12593 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
12594    unit and add it to our queue.
12595    The result is non-zero if PER_CU was queued, otherwise the result is zero
12596    meaning either PER_CU is already queued or it is already loaded.  */
12597
12598 static int
12599 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12600                        struct dwarf2_per_cu_data *per_cu)
12601 {
12602   /* We may arrive here during partial symbol reading, if we need full
12603      DIEs to process an unusual case (e.g. template arguments).  Do
12604      not queue PER_CU, just tell our caller to load its DIEs.  */
12605   if (dwarf2_per_objfile->reading_partial_symbols)
12606     {
12607       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12608         return 1;
12609       return 0;
12610     }
12611
12612   /* Mark the dependence relation so that we don't flush PER_CU
12613      too early.  */
12614   dwarf2_add_dependence (this_cu, per_cu);
12615
12616   /* If it's already on the queue, we have nothing to do.  */
12617   if (per_cu->queued)
12618     return 0;
12619
12620   /* If the compilation unit is already loaded, just mark it as
12621      used.  */
12622   if (per_cu->cu != NULL)
12623     {
12624       per_cu->cu->last_used = 0;
12625       return 0;
12626     }
12627
12628   /* Add it to the queue.  */
12629   queue_comp_unit (per_cu, this_cu->objfile);
12630
12631   return 1;
12632 }
12633
12634 /* Follow reference or signature attribute ATTR of SRC_DIE.
12635    On entry *REF_CU is the CU of SRC_DIE.
12636    On exit *REF_CU is the CU of the result.  */
12637
12638 static struct die_info *
12639 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12640                        struct dwarf2_cu **ref_cu)
12641 {
12642   struct die_info *die;
12643
12644   if (is_ref_attr (attr))
12645     die = follow_die_ref (src_die, attr, ref_cu);
12646   else if (attr->form == DW_FORM_sig8)
12647     die = follow_die_sig (src_die, attr, ref_cu);
12648   else
12649     {
12650       dump_die_for_error (src_die);
12651       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12652              (*ref_cu)->objfile->name);
12653     }
12654
12655   return die;
12656 }
12657
12658 /* Follow reference OFFSET.
12659    On entry *REF_CU is the CU of source DIE referencing OFFSET.
12660    On exit *REF_CU is the CU of the result.  */
12661
12662 static struct die_info *
12663 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12664 {
12665   struct die_info temp_die;
12666   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12667
12668   gdb_assert (cu->per_cu != NULL);
12669
12670   target_cu = cu;
12671
12672   if (cu->per_cu->from_debug_types)
12673     {
12674       /* .debug_types CUs cannot reference anything outside their CU.
12675          If they need to, they have to reference a signatured type via
12676          DW_FORM_sig8.  */
12677       if (! offset_in_cu_p (&cu->header, offset))
12678         return NULL;
12679     }
12680   else if (! offset_in_cu_p (&cu->header, offset))
12681     {
12682       struct dwarf2_per_cu_data *per_cu;
12683
12684       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12685
12686       /* If necessary, add it to the queue and load its DIEs.  */
12687       if (maybe_queue_comp_unit (cu, per_cu))
12688         load_full_comp_unit (per_cu, cu->objfile);
12689
12690       target_cu = per_cu->cu;
12691     }
12692   else if (cu->dies == NULL)
12693     {
12694       /* We're loading full DIEs during partial symbol reading.  */
12695       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12696       load_full_comp_unit (cu->per_cu, cu->objfile);
12697     }
12698
12699   *ref_cu = target_cu;
12700   temp_die.offset = offset;
12701   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12702 }
12703
12704 /* Follow reference attribute ATTR of SRC_DIE.
12705    On entry *REF_CU is the CU of SRC_DIE.
12706    On exit *REF_CU is the CU of the result.  */
12707
12708 static struct die_info *
12709 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12710                 struct dwarf2_cu **ref_cu)
12711 {
12712   unsigned int offset = dwarf2_get_ref_die_offset (attr);
12713   struct dwarf2_cu *cu = *ref_cu;
12714   struct die_info *die;
12715
12716   die = follow_die_offset (offset, ref_cu);
12717   if (!die)
12718     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12719            "at 0x%x [in module %s]"),
12720            offset, src_die->offset, cu->objfile->name);
12721
12722   return die;
12723 }
12724
12725 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
12726    value is intended for DW_OP_call*.  */
12727
12728 struct dwarf2_locexpr_baton
12729 dwarf2_fetch_die_location_block (unsigned int offset,
12730                                  struct dwarf2_per_cu_data *per_cu)
12731 {
12732   struct dwarf2_cu *cu = per_cu->cu;
12733   struct die_info *die;
12734   struct attribute *attr;
12735   struct dwarf2_locexpr_baton retval;
12736
12737   die = follow_die_offset (offset, &cu);
12738   if (!die)
12739     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12740            offset, per_cu->cu->objfile->name);
12741
12742   attr = dwarf2_attr (die, DW_AT_location, cu);
12743   if (!attr)
12744     {
12745       /* DWARF: "If there is no such attribute, then there is no effect.".  */
12746
12747       retval.data = NULL;
12748       retval.size = 0;
12749     }
12750   else
12751     {
12752       if (!attr_form_is_block (attr))
12753         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12754                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12755                offset, per_cu->cu->objfile->name);
12756
12757       retval.data = DW_BLOCK (attr)->data;
12758       retval.size = DW_BLOCK (attr)->size;
12759     }
12760   retval.per_cu = cu->per_cu;
12761   return retval;
12762 }
12763
12764 /* Follow the signature attribute ATTR in SRC_DIE.
12765    On entry *REF_CU is the CU of SRC_DIE.
12766    On exit *REF_CU is the CU of the result.  */
12767
12768 static struct die_info *
12769 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12770                 struct dwarf2_cu **ref_cu)
12771 {
12772   struct objfile *objfile = (*ref_cu)->objfile;
12773   struct die_info temp_die;
12774   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12775   struct dwarf2_cu *sig_cu;
12776   struct die_info *die;
12777
12778   /* sig_type will be NULL if the signatured type is missing from
12779      the debug info.  */
12780   if (sig_type == NULL)
12781     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12782              "at 0x%x [in module %s]"),
12783            src_die->offset, objfile->name);
12784
12785   /* If necessary, add it to the queue and load its DIEs.  */
12786
12787   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12788     read_signatured_type (objfile, sig_type);
12789
12790   gdb_assert (sig_type->per_cu.cu != NULL);
12791
12792   sig_cu = sig_type->per_cu.cu;
12793   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12794   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12795   if (die)
12796     {
12797       *ref_cu = sig_cu;
12798       return die;
12799     }
12800
12801   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12802          "at 0x%x [in module %s]"),
12803          sig_type->type_offset, src_die->offset, objfile->name);
12804 }
12805
12806 /* Given an offset of a signatured type, return its signatured_type.  */
12807
12808 static struct signatured_type *
12809 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12810 {
12811   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12812   unsigned int length, initial_length_size;
12813   unsigned int sig_offset;
12814   struct signatured_type find_entry, *type_sig;
12815
12816   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12817   sig_offset = (initial_length_size
12818                 + 2 /*version*/
12819                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12820                 + 1 /*address_size*/);
12821   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12822   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12823
12824   /* This is only used to lookup previously recorded types.
12825      If we didn't find it, it's our bug.  */
12826   gdb_assert (type_sig != NULL);
12827   gdb_assert (offset == type_sig->offset);
12828
12829   return type_sig;
12830 }
12831
12832 /* Read in signatured type at OFFSET and build its CU and die(s).  */
12833
12834 static void
12835 read_signatured_type_at_offset (struct objfile *objfile,
12836                                 unsigned int offset)
12837 {
12838   struct signatured_type *type_sig;
12839
12840   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12841
12842   /* We have the section offset, but we need the signature to do the
12843      hash table lookup.  */
12844   type_sig = lookup_signatured_type_at_offset (objfile, offset);
12845
12846   gdb_assert (type_sig->per_cu.cu == NULL);
12847
12848   read_signatured_type (objfile, type_sig);
12849
12850   gdb_assert (type_sig->per_cu.cu != NULL);
12851 }
12852
12853 /* Read in a signatured type and build its CU and DIEs.  */
12854
12855 static void
12856 read_signatured_type (struct objfile *objfile,
12857                       struct signatured_type *type_sig)
12858 {
12859   gdb_byte *types_ptr;
12860   struct die_reader_specs reader_specs;
12861   struct dwarf2_cu *cu;
12862   ULONGEST signature;
12863   struct cleanup *back_to, *free_cu_cleanup;
12864   struct attribute *attr;
12865
12866   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12867   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12868
12869   gdb_assert (type_sig->per_cu.cu == NULL);
12870
12871   cu = xmalloc (sizeof (struct dwarf2_cu));
12872   memset (cu, 0, sizeof (struct dwarf2_cu));
12873   obstack_init (&cu->comp_unit_obstack);
12874   cu->objfile = objfile;
12875   type_sig->per_cu.cu = cu;
12876   cu->per_cu = &type_sig->per_cu;
12877
12878   /* If an error occurs while loading, release our storage.  */
12879   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12880
12881   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12882                                         types_ptr, objfile->obfd);
12883   gdb_assert (signature == type_sig->signature);
12884
12885   cu->die_hash
12886     = htab_create_alloc_ex (cu->header.length / 12,
12887                             die_hash,
12888                             die_eq,
12889                             NULL,
12890                             &cu->comp_unit_obstack,
12891                             hashtab_obstack_allocate,
12892                             dummy_obstack_deallocate);
12893
12894   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12895   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12896
12897   init_cu_die_reader (&reader_specs, cu);
12898
12899   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12900                                     NULL /*parent*/);
12901
12902   /* We try not to read any attributes in this function, because not
12903      all objfiles needed for references have been loaded yet, and symbol
12904      table processing isn't initialized.  But we have to set the CU language,
12905      or we won't be able to build types correctly.  */
12906   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12907   if (attr)
12908     set_cu_language (DW_UNSND (attr), cu);
12909   else
12910     set_cu_language (language_minimal, cu);
12911
12912   do_cleanups (back_to);
12913
12914   /* We've successfully allocated this compilation unit.  Let our caller
12915      clean it up when finished with it.  */
12916   discard_cleanups (free_cu_cleanup);
12917
12918   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12919   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12920 }
12921
12922 /* Decode simple location descriptions.
12923    Given a pointer to a dwarf block that defines a location, compute
12924    the location and return the value.
12925
12926    NOTE drow/2003-11-18: This function is called in two situations
12927    now: for the address of static or global variables (partial symbols
12928    only) and for offsets into structures which are expected to be
12929    (more or less) constant.  The partial symbol case should go away,
12930    and only the constant case should remain.  That will let this
12931    function complain more accurately.  A few special modes are allowed
12932    without complaint for global variables (for instance, global
12933    register values and thread-local values).
12934
12935    A location description containing no operations indicates that the
12936    object is optimized out.  The return value is 0 for that case.
12937    FIXME drow/2003-11-16: No callers check for this case any more; soon all
12938    callers will only want a very basic result and this can become a
12939    complaint.
12940
12941    Note that stack[0] is unused except as a default error return.
12942    Note that stack overflow is not yet handled.  */
12943
12944 static CORE_ADDR
12945 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12946 {
12947   struct objfile *objfile = cu->objfile;
12948   int i;
12949   int size = blk->size;
12950   gdb_byte *data = blk->data;
12951   CORE_ADDR stack[64];
12952   int stacki;
12953   unsigned int bytes_read, unsnd;
12954   gdb_byte op;
12955
12956   i = 0;
12957   stacki = 0;
12958   stack[stacki] = 0;
12959
12960   while (i < size)
12961     {
12962       op = data[i++];
12963       switch (op)
12964         {
12965         case DW_OP_lit0:
12966         case DW_OP_lit1:
12967         case DW_OP_lit2:
12968         case DW_OP_lit3:
12969         case DW_OP_lit4:
12970         case DW_OP_lit5:
12971         case DW_OP_lit6:
12972         case DW_OP_lit7:
12973         case DW_OP_lit8:
12974         case DW_OP_lit9:
12975         case DW_OP_lit10:
12976         case DW_OP_lit11:
12977         case DW_OP_lit12:
12978         case DW_OP_lit13:
12979         case DW_OP_lit14:
12980         case DW_OP_lit15:
12981         case DW_OP_lit16:
12982         case DW_OP_lit17:
12983         case DW_OP_lit18:
12984         case DW_OP_lit19:
12985         case DW_OP_lit20:
12986         case DW_OP_lit21:
12987         case DW_OP_lit22:
12988         case DW_OP_lit23:
12989         case DW_OP_lit24:
12990         case DW_OP_lit25:
12991         case DW_OP_lit26:
12992         case DW_OP_lit27:
12993         case DW_OP_lit28:
12994         case DW_OP_lit29:
12995         case DW_OP_lit30:
12996         case DW_OP_lit31:
12997           stack[++stacki] = op - DW_OP_lit0;
12998           break;
12999
13000         case DW_OP_reg0:
13001         case DW_OP_reg1:
13002         case DW_OP_reg2:
13003         case DW_OP_reg3:
13004         case DW_OP_reg4:
13005         case DW_OP_reg5:
13006         case DW_OP_reg6:
13007         case DW_OP_reg7:
13008         case DW_OP_reg8:
13009         case DW_OP_reg9:
13010         case DW_OP_reg10:
13011         case DW_OP_reg11:
13012         case DW_OP_reg12:
13013         case DW_OP_reg13:
13014         case DW_OP_reg14:
13015         case DW_OP_reg15:
13016         case DW_OP_reg16:
13017         case DW_OP_reg17:
13018         case DW_OP_reg18:
13019         case DW_OP_reg19:
13020         case DW_OP_reg20:
13021         case DW_OP_reg21:
13022         case DW_OP_reg22:
13023         case DW_OP_reg23:
13024         case DW_OP_reg24:
13025         case DW_OP_reg25:
13026         case DW_OP_reg26:
13027         case DW_OP_reg27:
13028         case DW_OP_reg28:
13029         case DW_OP_reg29:
13030         case DW_OP_reg30:
13031         case DW_OP_reg31:
13032           stack[++stacki] = op - DW_OP_reg0;
13033           if (i < size)
13034             dwarf2_complex_location_expr_complaint ();
13035           break;
13036
13037         case DW_OP_regx:
13038           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13039           i += bytes_read;
13040           stack[++stacki] = unsnd;
13041           if (i < size)
13042             dwarf2_complex_location_expr_complaint ();
13043           break;
13044
13045         case DW_OP_addr:
13046           stack[++stacki] = read_address (objfile->obfd, &data[i],
13047                                           cu, &bytes_read);
13048           i += bytes_read;
13049           break;
13050
13051         case DW_OP_const1u:
13052           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13053           i += 1;
13054           break;
13055
13056         case DW_OP_const1s:
13057           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13058           i += 1;
13059           break;
13060
13061         case DW_OP_const2u:
13062           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13063           i += 2;
13064           break;
13065
13066         case DW_OP_const2s:
13067           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13068           i += 2;
13069           break;
13070
13071         case DW_OP_const4u:
13072           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13073           i += 4;
13074           break;
13075
13076         case DW_OP_const4s:
13077           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13078           i += 4;
13079           break;
13080
13081         case DW_OP_constu:
13082           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13083                                                   &bytes_read);
13084           i += bytes_read;
13085           break;
13086
13087         case DW_OP_consts:
13088           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13089           i += bytes_read;
13090           break;
13091
13092         case DW_OP_dup:
13093           stack[stacki + 1] = stack[stacki];
13094           stacki++;
13095           break;
13096
13097         case DW_OP_plus:
13098           stack[stacki - 1] += stack[stacki];
13099           stacki--;
13100           break;
13101
13102         case DW_OP_plus_uconst:
13103           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13104           i += bytes_read;
13105           break;
13106
13107         case DW_OP_minus:
13108           stack[stacki - 1] -= stack[stacki];
13109           stacki--;
13110           break;
13111
13112         case DW_OP_deref:
13113           /* If we're not the last op, then we definitely can't encode
13114              this using GDB's address_class enum.  This is valid for partial
13115              global symbols, although the variable's address will be bogus
13116              in the psymtab.  */
13117           if (i < size)
13118             dwarf2_complex_location_expr_complaint ();
13119           break;
13120
13121         case DW_OP_GNU_push_tls_address:
13122           /* The top of the stack has the offset from the beginning
13123              of the thread control block at which the variable is located.  */
13124           /* Nothing should follow this operator, so the top of stack would
13125              be returned.  */
13126           /* This is valid for partial global symbols, but the variable's
13127              address will be bogus in the psymtab.  */
13128           if (i < size)
13129             dwarf2_complex_location_expr_complaint ();
13130           break;
13131
13132         case DW_OP_GNU_uninit:
13133           break;
13134
13135         default:
13136           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13137                      dwarf_stack_op_name (op, 1));
13138           return (stack[stacki]);
13139         }
13140     }
13141   return (stack[stacki]);
13142 }
13143
13144 /* memory allocation interface */
13145
13146 static struct dwarf_block *
13147 dwarf_alloc_block (struct dwarf2_cu *cu)
13148 {
13149   struct dwarf_block *blk;
13150
13151   blk = (struct dwarf_block *)
13152     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13153   return (blk);
13154 }
13155
13156 static struct abbrev_info *
13157 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13158 {
13159   struct abbrev_info *abbrev;
13160
13161   abbrev = (struct abbrev_info *)
13162     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13163   memset (abbrev, 0, sizeof (struct abbrev_info));
13164   return (abbrev);
13165 }
13166
13167 static struct die_info *
13168 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13169 {
13170   struct die_info *die;
13171   size_t size = sizeof (struct die_info);
13172
13173   if (num_attrs > 1)
13174     size += (num_attrs - 1) * sizeof (struct attribute);
13175
13176   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13177   memset (die, 0, sizeof (struct die_info));
13178   return (die);
13179 }
13180
13181 \f
13182 /* Macro support.  */
13183
13184
13185 /* Return the full name of file number I in *LH's file name table.
13186    Use COMP_DIR as the name of the current directory of the
13187    compilation.  The result is allocated using xmalloc; the caller is
13188    responsible for freeing it.  */
13189 static char *
13190 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13191 {
13192   /* Is the file number a valid index into the line header's file name
13193      table?  Remember that file numbers start with one, not zero.  */
13194   if (1 <= file && file <= lh->num_file_names)
13195     {
13196       struct file_entry *fe = &lh->file_names[file - 1];
13197
13198       if (IS_ABSOLUTE_PATH (fe->name))
13199         return xstrdup (fe->name);
13200       else
13201         {
13202           const char *dir;
13203           int dir_len;
13204           char *full_name;
13205
13206           if (fe->dir_index)
13207             dir = lh->include_dirs[fe->dir_index - 1];
13208           else
13209             dir = comp_dir;
13210
13211           if (dir)
13212             {
13213               dir_len = strlen (dir);
13214               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13215               strcpy (full_name, dir);
13216               full_name[dir_len] = '/';
13217               strcpy (full_name + dir_len + 1, fe->name);
13218               return full_name;
13219             }
13220           else
13221             return xstrdup (fe->name);
13222         }
13223     }
13224   else
13225     {
13226       /* The compiler produced a bogus file number.  We can at least
13227          record the macro definitions made in the file, even if we
13228          won't be able to find the file by name.  */
13229       char fake_name[80];
13230
13231       sprintf (fake_name, "<bad macro file number %d>", file);
13232
13233       complaint (&symfile_complaints,
13234                  _("bad file number in macro information (%d)"),
13235                  file);
13236
13237       return xstrdup (fake_name);
13238     }
13239 }
13240
13241
13242 static struct macro_source_file *
13243 macro_start_file (int file, int line,
13244                   struct macro_source_file *current_file,
13245                   const char *comp_dir,
13246                   struct line_header *lh, struct objfile *objfile)
13247 {
13248   /* The full name of this source file.  */
13249   char *full_name = file_full_name (file, lh, comp_dir);
13250
13251   /* We don't create a macro table for this compilation unit
13252      at all until we actually get a filename.  */
13253   if (! pending_macros)
13254     pending_macros = new_macro_table (&objfile->objfile_obstack,
13255                                       objfile->macro_cache);
13256
13257   if (! current_file)
13258     /* If we have no current file, then this must be the start_file
13259        directive for the compilation unit's main source file.  */
13260     current_file = macro_set_main (pending_macros, full_name);
13261   else
13262     current_file = macro_include (current_file, line, full_name);
13263
13264   xfree (full_name);
13265
13266   return current_file;
13267 }
13268
13269
13270 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13271    followed by a null byte.  */
13272 static char *
13273 copy_string (const char *buf, int len)
13274 {
13275   char *s = xmalloc (len + 1);
13276
13277   memcpy (s, buf, len);
13278   s[len] = '\0';
13279   return s;
13280 }
13281
13282
13283 static const char *
13284 consume_improper_spaces (const char *p, const char *body)
13285 {
13286   if (*p == ' ')
13287     {
13288       complaint (&symfile_complaints,
13289                  _("macro definition contains spaces in formal argument list:\n`%s'"),
13290                  body);
13291
13292       while (*p == ' ')
13293         p++;
13294     }
13295
13296   return p;
13297 }
13298
13299
13300 static void
13301 parse_macro_definition (struct macro_source_file *file, int line,
13302                         const char *body)
13303 {
13304   const char *p;
13305
13306   /* The body string takes one of two forms.  For object-like macro
13307      definitions, it should be:
13308
13309         <macro name> " " <definition>
13310
13311      For function-like macro definitions, it should be:
13312
13313         <macro name> "() " <definition>
13314      or
13315         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13316
13317      Spaces may appear only where explicitly indicated, and in the
13318      <definition>.
13319
13320      The Dwarf 2 spec says that an object-like macro's name is always
13321      followed by a space, but versions of GCC around March 2002 omit
13322      the space when the macro's definition is the empty string.
13323
13324      The Dwarf 2 spec says that there should be no spaces between the
13325      formal arguments in a function-like macro's formal argument list,
13326      but versions of GCC around March 2002 include spaces after the
13327      commas.  */
13328
13329
13330   /* Find the extent of the macro name.  The macro name is terminated
13331      by either a space or null character (for an object-like macro) or
13332      an opening paren (for a function-like macro).  */
13333   for (p = body; *p; p++)
13334     if (*p == ' ' || *p == '(')
13335       break;
13336
13337   if (*p == ' ' || *p == '\0')
13338     {
13339       /* It's an object-like macro.  */
13340       int name_len = p - body;
13341       char *name = copy_string (body, name_len);
13342       const char *replacement;
13343
13344       if (*p == ' ')
13345         replacement = body + name_len + 1;
13346       else
13347         {
13348           dwarf2_macro_malformed_definition_complaint (body);
13349           replacement = body + name_len;
13350         }
13351
13352       macro_define_object (file, line, name, replacement);
13353
13354       xfree (name);
13355     }
13356   else if (*p == '(')
13357     {
13358       /* It's a function-like macro.  */
13359       char *name = copy_string (body, p - body);
13360       int argc = 0;
13361       int argv_size = 1;
13362       char **argv = xmalloc (argv_size * sizeof (*argv));
13363
13364       p++;
13365
13366       p = consume_improper_spaces (p, body);
13367
13368       /* Parse the formal argument list.  */
13369       while (*p && *p != ')')
13370         {
13371           /* Find the extent of the current argument name.  */
13372           const char *arg_start = p;
13373
13374           while (*p && *p != ',' && *p != ')' && *p != ' ')
13375             p++;
13376
13377           if (! *p || p == arg_start)
13378             dwarf2_macro_malformed_definition_complaint (body);
13379           else
13380             {
13381               /* Make sure argv has room for the new argument.  */
13382               if (argc >= argv_size)
13383                 {
13384                   argv_size *= 2;
13385                   argv = xrealloc (argv, argv_size * sizeof (*argv));
13386                 }
13387
13388               argv[argc++] = copy_string (arg_start, p - arg_start);
13389             }
13390
13391           p = consume_improper_spaces (p, body);
13392
13393           /* Consume the comma, if present.  */
13394           if (*p == ',')
13395             {
13396               p++;
13397
13398               p = consume_improper_spaces (p, body);
13399             }
13400         }
13401
13402       if (*p == ')')
13403         {
13404           p++;
13405
13406           if (*p == ' ')
13407             /* Perfectly formed definition, no complaints.  */
13408             macro_define_function (file, line, name,
13409                                    argc, (const char **) argv,
13410                                    p + 1);
13411           else if (*p == '\0')
13412             {
13413               /* Complain, but do define it.  */
13414               dwarf2_macro_malformed_definition_complaint (body);
13415               macro_define_function (file, line, name,
13416                                      argc, (const char **) argv,
13417                                      p);
13418             }
13419           else
13420             /* Just complain.  */
13421             dwarf2_macro_malformed_definition_complaint (body);
13422         }
13423       else
13424         /* Just complain.  */
13425         dwarf2_macro_malformed_definition_complaint (body);
13426
13427       xfree (name);
13428       {
13429         int i;
13430
13431         for (i = 0; i < argc; i++)
13432           xfree (argv[i]);
13433       }
13434       xfree (argv);
13435     }
13436   else
13437     dwarf2_macro_malformed_definition_complaint (body);
13438 }
13439
13440
13441 static void
13442 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13443                      char *comp_dir, bfd *abfd,
13444                      struct dwarf2_cu *cu)
13445 {
13446   gdb_byte *mac_ptr, *mac_end;
13447   struct macro_source_file *current_file = 0;
13448   enum dwarf_macinfo_record_type macinfo_type;
13449   int at_commandline;
13450
13451   dwarf2_read_section (dwarf2_per_objfile->objfile,
13452                        &dwarf2_per_objfile->macinfo);
13453   if (dwarf2_per_objfile->macinfo.buffer == NULL)
13454     {
13455       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13456       return;
13457     }
13458
13459   /* First pass: Find the name of the base filename.
13460      This filename is needed in order to process all macros whose definition
13461      (or undefinition) comes from the command line.  These macros are defined
13462      before the first DW_MACINFO_start_file entry, and yet still need to be
13463      associated to the base file.
13464
13465      To determine the base file name, we scan the macro definitions until we
13466      reach the first DW_MACINFO_start_file entry.  We then initialize
13467      CURRENT_FILE accordingly so that any macro definition found before the
13468      first DW_MACINFO_start_file can still be associated to the base file.  */
13469
13470   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13471   mac_end = dwarf2_per_objfile->macinfo.buffer
13472     + dwarf2_per_objfile->macinfo.size;
13473
13474   do
13475     {
13476       /* Do we at least have room for a macinfo type byte?  */
13477       if (mac_ptr >= mac_end)
13478         {
13479           /* Complaint is printed during the second pass as GDB will probably
13480              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
13481           break;
13482         }
13483
13484       macinfo_type = read_1_byte (abfd, mac_ptr);
13485       mac_ptr++;
13486
13487       switch (macinfo_type)
13488         {
13489           /* A zero macinfo type indicates the end of the macro
13490              information.  */
13491         case 0:
13492           break;
13493
13494         case DW_MACINFO_define:
13495         case DW_MACINFO_undef:
13496           /* Only skip the data by MAC_PTR.  */
13497           {
13498             unsigned int bytes_read;
13499
13500             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13501             mac_ptr += bytes_read;
13502             read_direct_string (abfd, mac_ptr, &bytes_read);
13503             mac_ptr += bytes_read;
13504           }
13505           break;
13506
13507         case DW_MACINFO_start_file:
13508           {
13509             unsigned int bytes_read;
13510             int line, file;
13511
13512             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13513             mac_ptr += bytes_read;
13514             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13515             mac_ptr += bytes_read;
13516
13517             current_file = macro_start_file (file, line, current_file, comp_dir,
13518                                              lh, cu->objfile);
13519           }
13520           break;
13521
13522         case DW_MACINFO_end_file:
13523           /* No data to skip by MAC_PTR.  */
13524           break;
13525
13526         case DW_MACINFO_vendor_ext:
13527           /* Only skip the data by MAC_PTR.  */
13528           {
13529             unsigned int bytes_read;
13530
13531             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13532             mac_ptr += bytes_read;
13533             read_direct_string (abfd, mac_ptr, &bytes_read);
13534             mac_ptr += bytes_read;
13535           }
13536           break;
13537
13538         default:
13539           break;
13540         }
13541     } while (macinfo_type != 0 && current_file == NULL);
13542
13543   /* Second pass: Process all entries.
13544
13545      Use the AT_COMMAND_LINE flag to determine whether we are still processing
13546      command-line macro definitions/undefinitions.  This flag is unset when we
13547      reach the first DW_MACINFO_start_file entry.  */
13548
13549   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13550
13551   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
13552      GDB is still reading the definitions from command line.  First
13553      DW_MACINFO_start_file will need to be ignored as it was already executed
13554      to create CURRENT_FILE for the main source holding also the command line
13555      definitions.  On first met DW_MACINFO_start_file this flag is reset to
13556      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
13557
13558   at_commandline = 1;
13559
13560   do
13561     {
13562       /* Do we at least have room for a macinfo type byte?  */
13563       if (mac_ptr >= mac_end)
13564         {
13565           dwarf2_macros_too_long_complaint ();
13566           break;
13567         }
13568
13569       macinfo_type = read_1_byte (abfd, mac_ptr);
13570       mac_ptr++;
13571
13572       switch (macinfo_type)
13573         {
13574           /* A zero macinfo type indicates the end of the macro
13575              information.  */
13576         case 0:
13577           break;
13578
13579         case DW_MACINFO_define:
13580         case DW_MACINFO_undef:
13581           {
13582             unsigned int bytes_read;
13583             int line;
13584             char *body;
13585
13586             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13587             mac_ptr += bytes_read;
13588             body = read_direct_string (abfd, mac_ptr, &bytes_read);
13589             mac_ptr += bytes_read;
13590
13591             if (! current_file)
13592               {
13593                 /* DWARF violation as no main source is present.  */
13594                 complaint (&symfile_complaints,
13595                            _("debug info with no main source gives macro %s "
13596                              "on line %d: %s"),
13597                            macinfo_type == DW_MACINFO_define ?
13598                              _("definition") :
13599                                macinfo_type == DW_MACINFO_undef ?
13600                                  _("undefinition") :
13601                                  _("something-or-other"), line, body);
13602                 break;
13603               }
13604             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13605               complaint (&symfile_complaints,
13606                          _("debug info gives %s macro %s with %s line %d: %s"),
13607                          at_commandline ? _("command-line") : _("in-file"),
13608                          macinfo_type == DW_MACINFO_define ?
13609                            _("definition") :
13610                              macinfo_type == DW_MACINFO_undef ?
13611                                _("undefinition") :
13612                                _("something-or-other"),
13613                          line == 0 ? _("zero") : _("non-zero"), line, body);
13614
13615             if (macinfo_type == DW_MACINFO_define)
13616               parse_macro_definition (current_file, line, body);
13617             else if (macinfo_type == DW_MACINFO_undef)
13618               macro_undef (current_file, line, body);
13619           }
13620           break;
13621
13622         case DW_MACINFO_start_file:
13623           {
13624             unsigned int bytes_read;
13625             int line, file;
13626
13627             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13628             mac_ptr += bytes_read;
13629             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13630             mac_ptr += bytes_read;
13631
13632             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13633               complaint (&symfile_complaints,
13634                          _("debug info gives source %d included "
13635                            "from %s at %s line %d"),
13636                          file, at_commandline ? _("command-line") : _("file"),
13637                          line == 0 ? _("zero") : _("non-zero"), line);
13638
13639             if (at_commandline)
13640               {
13641                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13642                 at_commandline = 0;
13643               }
13644             else
13645               current_file = macro_start_file (file, line,
13646                                                current_file, comp_dir,
13647                                                lh, cu->objfile);
13648           }
13649           break;
13650
13651         case DW_MACINFO_end_file:
13652           if (! current_file)
13653             complaint (&symfile_complaints,
13654                        _("macro debug info has an unmatched `close_file' directive"));
13655           else
13656             {
13657               current_file = current_file->included_by;
13658               if (! current_file)
13659                 {
13660                   enum dwarf_macinfo_record_type next_type;
13661
13662                   /* GCC circa March 2002 doesn't produce the zero
13663                      type byte marking the end of the compilation
13664                      unit.  Complain if it's not there, but exit no
13665                      matter what.  */
13666
13667                   /* Do we at least have room for a macinfo type byte?  */
13668                   if (mac_ptr >= mac_end)
13669                     {
13670                       dwarf2_macros_too_long_complaint ();
13671                       return;
13672                     }
13673
13674                   /* We don't increment mac_ptr here, so this is just
13675                      a look-ahead.  */
13676                   next_type = read_1_byte (abfd, mac_ptr);
13677                   if (next_type != 0)
13678                     complaint (&symfile_complaints,
13679                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13680
13681                   return;
13682                 }
13683             }
13684           break;
13685
13686         case DW_MACINFO_vendor_ext:
13687           {
13688             unsigned int bytes_read;
13689             int constant;
13690             char *string;
13691
13692             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13693             mac_ptr += bytes_read;
13694             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13695             mac_ptr += bytes_read;
13696
13697             /* We don't recognize any vendor extensions.  */
13698           }
13699           break;
13700         }
13701     } while (macinfo_type != 0);
13702 }
13703
13704 /* Check if the attribute's form is a DW_FORM_block*
13705    if so return true else false. */
13706 static int
13707 attr_form_is_block (struct attribute *attr)
13708 {
13709   return (attr == NULL ? 0 :
13710       attr->form == DW_FORM_block1
13711       || attr->form == DW_FORM_block2
13712       || attr->form == DW_FORM_block4
13713       || attr->form == DW_FORM_block
13714       || attr->form == DW_FORM_exprloc);
13715 }
13716
13717 /* Return non-zero if ATTR's value is a section offset --- classes
13718    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13719    You may use DW_UNSND (attr) to retrieve such offsets.
13720
13721    Section 7.5.4, "Attribute Encodings", explains that no attribute
13722    may have a value that belongs to more than one of these classes; it
13723    would be ambiguous if we did, because we use the same forms for all
13724    of them.  */
13725 static int
13726 attr_form_is_section_offset (struct attribute *attr)
13727 {
13728   return (attr->form == DW_FORM_data4
13729           || attr->form == DW_FORM_data8
13730           || attr->form == DW_FORM_sec_offset);
13731 }
13732
13733
13734 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13735    zero otherwise.  When this function returns true, you can apply
13736    dwarf2_get_attr_constant_value to it.
13737
13738    However, note that for some attributes you must check
13739    attr_form_is_section_offset before using this test.  DW_FORM_data4
13740    and DW_FORM_data8 are members of both the constant class, and of
13741    the classes that contain offsets into other debug sections
13742    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
13743    that, if an attribute's can be either a constant or one of the
13744    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13745    taken as section offsets, not constants.  */
13746 static int
13747 attr_form_is_constant (struct attribute *attr)
13748 {
13749   switch (attr->form)
13750     {
13751     case DW_FORM_sdata:
13752     case DW_FORM_udata:
13753     case DW_FORM_data1:
13754     case DW_FORM_data2:
13755     case DW_FORM_data4:
13756     case DW_FORM_data8:
13757       return 1;
13758     default:
13759       return 0;
13760     }
13761 }
13762
13763 static void
13764 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13765                              struct dwarf2_cu *cu)
13766 {
13767   if (attr_form_is_section_offset (attr)
13768       /* ".debug_loc" may not exist at all, or the offset may be outside
13769          the section.  If so, fall through to the complaint in the
13770          other branch.  */
13771       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13772     {
13773       struct dwarf2_loclist_baton *baton;
13774
13775       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13776                              sizeof (struct dwarf2_loclist_baton));
13777       baton->per_cu = cu->per_cu;
13778       gdb_assert (baton->per_cu);
13779
13780       dwarf2_read_section (dwarf2_per_objfile->objfile,
13781                            &dwarf2_per_objfile->loc);
13782
13783       /* We don't know how long the location list is, but make sure we
13784          don't run off the edge of the section.  */
13785       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13786       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13787       baton->base_address = cu->base_address;
13788       if (cu->base_known == 0)
13789         complaint (&symfile_complaints,
13790                    _("Location list used without specifying the CU base address."));
13791
13792       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13793       SYMBOL_LOCATION_BATON (sym) = baton;
13794     }
13795   else
13796     {
13797       struct dwarf2_locexpr_baton *baton;
13798
13799       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13800                              sizeof (struct dwarf2_locexpr_baton));
13801       baton->per_cu = cu->per_cu;
13802       gdb_assert (baton->per_cu);
13803
13804       if (attr_form_is_block (attr))
13805         {
13806           /* Note that we're just copying the block's data pointer
13807              here, not the actual data.  We're still pointing into the
13808              info_buffer for SYM's objfile; right now we never release
13809              that buffer, but when we do clean up properly this may
13810              need to change.  */
13811           baton->size = DW_BLOCK (attr)->size;
13812           baton->data = DW_BLOCK (attr)->data;
13813         }
13814       else
13815         {
13816           dwarf2_invalid_attrib_class_complaint ("location description",
13817                                                  SYMBOL_NATURAL_NAME (sym));
13818           baton->size = 0;
13819           baton->data = NULL;
13820         }
13821
13822       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13823       SYMBOL_LOCATION_BATON (sym) = baton;
13824     }
13825 }
13826
13827 /* Return the OBJFILE associated with the compilation unit CU.  If CU
13828    came from a separate debuginfo file, then the master objfile is
13829    returned.  */
13830
13831 struct objfile *
13832 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13833 {
13834   struct objfile *objfile = per_cu->objfile;
13835
13836   /* Return the master objfile, so that we can report and look up the
13837      correct file containing this variable.  */
13838   if (objfile->separate_debug_objfile_backlink)
13839     objfile = objfile->separate_debug_objfile_backlink;
13840
13841   return objfile;
13842 }
13843
13844 /* Return the address size given in the compilation unit header for CU.  */
13845
13846 CORE_ADDR
13847 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13848 {
13849   if (per_cu->cu)
13850     return per_cu->cu->header.addr_size;
13851   else
13852     {
13853       /* If the CU is not currently read in, we re-read its header.  */
13854       struct objfile *objfile = per_cu->objfile;
13855       struct dwarf2_per_objfile *per_objfile
13856         = objfile_data (objfile, dwarf2_objfile_data_key);
13857       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13858       struct comp_unit_head cu_header;
13859
13860       memset (&cu_header, 0, sizeof cu_header);
13861       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13862       return cu_header.addr_size;
13863     }
13864 }
13865
13866 /* Return the offset size given in the compilation unit header for CU.  */
13867
13868 int
13869 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13870 {
13871   if (per_cu->cu)
13872     return per_cu->cu->header.offset_size;
13873   else
13874     {
13875       /* If the CU is not currently read in, we re-read its header.  */
13876       struct objfile *objfile = per_cu->objfile;
13877       struct dwarf2_per_objfile *per_objfile
13878         = objfile_data (objfile, dwarf2_objfile_data_key);
13879       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13880       struct comp_unit_head cu_header;
13881
13882       memset (&cu_header, 0, sizeof cu_header);
13883       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13884       return cu_header.offset_size;
13885     }
13886 }
13887
13888 /* Return the text offset of the CU.  The returned offset comes from
13889    this CU's objfile.  If this objfile came from a separate debuginfo
13890    file, then the offset may be different from the corresponding
13891    offset in the parent objfile.  */
13892
13893 CORE_ADDR
13894 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13895 {
13896   struct objfile *objfile = per_cu->objfile;
13897
13898   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13899 }
13900
13901 /* Locate the .debug_info compilation unit from CU's objfile which contains
13902    the DIE at OFFSET.  Raises an error on failure.  */
13903
13904 static struct dwarf2_per_cu_data *
13905 dwarf2_find_containing_comp_unit (unsigned int offset,
13906                                   struct objfile *objfile)
13907 {
13908   struct dwarf2_per_cu_data *this_cu;
13909   int low, high;
13910
13911   low = 0;
13912   high = dwarf2_per_objfile->n_comp_units - 1;
13913   while (high > low)
13914     {
13915       int mid = low + (high - low) / 2;
13916
13917       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13918         high = mid;
13919       else
13920         low = mid + 1;
13921     }
13922   gdb_assert (low == high);
13923   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13924     {
13925       if (low == 0)
13926         error (_("Dwarf Error: could not find partial DIE containing "
13927                "offset 0x%lx [in module %s]"),
13928                (long) offset, bfd_get_filename (objfile->obfd));
13929
13930       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13931       return dwarf2_per_objfile->all_comp_units[low-1];
13932     }
13933   else
13934     {
13935       this_cu = dwarf2_per_objfile->all_comp_units[low];
13936       if (low == dwarf2_per_objfile->n_comp_units - 1
13937           && offset >= this_cu->offset + this_cu->length)
13938         error (_("invalid dwarf2 offset %u"), offset);
13939       gdb_assert (offset < this_cu->offset + this_cu->length);
13940       return this_cu;
13941     }
13942 }
13943
13944 /* Locate the compilation unit from OBJFILE which is located at exactly
13945    OFFSET.  Raises an error on failure.  */
13946
13947 static struct dwarf2_per_cu_data *
13948 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13949 {
13950   struct dwarf2_per_cu_data *this_cu;
13951
13952   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13953   if (this_cu->offset != offset)
13954     error (_("no compilation unit with offset %u."), offset);
13955   return this_cu;
13956 }
13957
13958 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
13959
13960 static struct dwarf2_cu *
13961 alloc_one_comp_unit (struct objfile *objfile)
13962 {
13963   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13964   cu->objfile = objfile;
13965   obstack_init (&cu->comp_unit_obstack);
13966   return cu;
13967 }
13968
13969 /* Release one cached compilation unit, CU.  We unlink it from the tree
13970    of compilation units, but we don't remove it from the read_in_chain;
13971    the caller is responsible for that.
13972    NOTE: DATA is a void * because this function is also used as a
13973    cleanup routine.  */
13974
13975 static void
13976 free_one_comp_unit (void *data)
13977 {
13978   struct dwarf2_cu *cu = data;
13979
13980   if (cu->per_cu != NULL)
13981     cu->per_cu->cu = NULL;
13982   cu->per_cu = NULL;
13983
13984   obstack_free (&cu->comp_unit_obstack, NULL);
13985
13986   xfree (cu);
13987 }
13988
13989 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13990    when we're finished with it.  We can't free the pointer itself, but be
13991    sure to unlink it from the cache.  Also release any associated storage
13992    and perform cache maintenance.
13993
13994    Only used during partial symbol parsing.  */
13995
13996 static void
13997 free_stack_comp_unit (void *data)
13998 {
13999   struct dwarf2_cu *cu = data;
14000
14001   obstack_free (&cu->comp_unit_obstack, NULL);
14002   cu->partial_dies = NULL;
14003
14004   if (cu->per_cu != NULL)
14005     {
14006       /* This compilation unit is on the stack in our caller, so we
14007          should not xfree it.  Just unlink it.  */
14008       cu->per_cu->cu = NULL;
14009       cu->per_cu = NULL;
14010
14011       /* If we had a per-cu pointer, then we may have other compilation
14012          units loaded, so age them now.  */
14013       age_cached_comp_units ();
14014     }
14015 }
14016
14017 /* Free all cached compilation units.  */
14018
14019 static void
14020 free_cached_comp_units (void *data)
14021 {
14022   struct dwarf2_per_cu_data *per_cu, **last_chain;
14023
14024   per_cu = dwarf2_per_objfile->read_in_chain;
14025   last_chain = &dwarf2_per_objfile->read_in_chain;
14026   while (per_cu != NULL)
14027     {
14028       struct dwarf2_per_cu_data *next_cu;
14029
14030       next_cu = per_cu->cu->read_in_chain;
14031
14032       free_one_comp_unit (per_cu->cu);
14033       *last_chain = next_cu;
14034
14035       per_cu = next_cu;
14036     }
14037 }
14038
14039 /* Increase the age counter on each cached compilation unit, and free
14040    any that are too old.  */
14041
14042 static void
14043 age_cached_comp_units (void)
14044 {
14045   struct dwarf2_per_cu_data *per_cu, **last_chain;
14046
14047   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14048   per_cu = dwarf2_per_objfile->read_in_chain;
14049   while (per_cu != NULL)
14050     {
14051       per_cu->cu->last_used ++;
14052       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14053         dwarf2_mark (per_cu->cu);
14054       per_cu = per_cu->cu->read_in_chain;
14055     }
14056
14057   per_cu = dwarf2_per_objfile->read_in_chain;
14058   last_chain = &dwarf2_per_objfile->read_in_chain;
14059   while (per_cu != NULL)
14060     {
14061       struct dwarf2_per_cu_data *next_cu;
14062
14063       next_cu = per_cu->cu->read_in_chain;
14064
14065       if (!per_cu->cu->mark)
14066         {
14067           free_one_comp_unit (per_cu->cu);
14068           *last_chain = next_cu;
14069         }
14070       else
14071         last_chain = &per_cu->cu->read_in_chain;
14072
14073       per_cu = next_cu;
14074     }
14075 }
14076
14077 /* Remove a single compilation unit from the cache.  */
14078
14079 static void
14080 free_one_cached_comp_unit (void *target_cu)
14081 {
14082   struct dwarf2_per_cu_data *per_cu, **last_chain;
14083
14084   per_cu = dwarf2_per_objfile->read_in_chain;
14085   last_chain = &dwarf2_per_objfile->read_in_chain;
14086   while (per_cu != NULL)
14087     {
14088       struct dwarf2_per_cu_data *next_cu;
14089
14090       next_cu = per_cu->cu->read_in_chain;
14091
14092       if (per_cu->cu == target_cu)
14093         {
14094           free_one_comp_unit (per_cu->cu);
14095           *last_chain = next_cu;
14096           break;
14097         }
14098       else
14099         last_chain = &per_cu->cu->read_in_chain;
14100
14101       per_cu = next_cu;
14102     }
14103 }
14104
14105 /* Release all extra memory associated with OBJFILE.  */
14106
14107 void
14108 dwarf2_free_objfile (struct objfile *objfile)
14109 {
14110   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14111
14112   if (dwarf2_per_objfile == NULL)
14113     return;
14114
14115   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
14116   free_cached_comp_units (NULL);
14117
14118   if (dwarf2_per_objfile->using_index)
14119     {
14120       int i;
14121
14122       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14123         {
14124           int j;
14125           struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14126
14127           if (!cu->v.quick->lines)
14128             continue;
14129
14130           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
14131             {
14132               if (cu->v.quick->file_names)
14133                 xfree ((void *) cu->v.quick->file_names[j]);
14134               if (cu->v.quick->full_names)
14135                 xfree ((void *) cu->v.quick->full_names[j]);
14136             }
14137
14138           free_line_header (cu->v.quick->lines);
14139         }
14140     }
14141
14142   /* Everything else should be on the objfile obstack.  */
14143 }
14144
14145 /* A pair of DIE offset and GDB type pointer.  We store these
14146    in a hash table separate from the DIEs, and preserve them
14147    when the DIEs are flushed out of cache.  */
14148
14149 struct dwarf2_offset_and_type
14150 {
14151   unsigned int offset;
14152   struct type *type;
14153 };
14154
14155 /* Hash function for a dwarf2_offset_and_type.  */
14156
14157 static hashval_t
14158 offset_and_type_hash (const void *item)
14159 {
14160   const struct dwarf2_offset_and_type *ofs = item;
14161
14162   return ofs->offset;
14163 }
14164
14165 /* Equality function for a dwarf2_offset_and_type.  */
14166
14167 static int
14168 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14169 {
14170   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14171   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14172
14173   return ofs_lhs->offset == ofs_rhs->offset;
14174 }
14175
14176 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
14177    table if necessary.  For convenience, return TYPE.
14178
14179    The DIEs reading must have careful ordering to:
14180     * Not cause infite loops trying to read in DIEs as a prerequisite for
14181       reading current DIE.
14182     * Not trying to dereference contents of still incompletely read in types
14183       while reading in other DIEs.
14184     * Enable referencing still incompletely read in types just by a pointer to
14185       the type without accessing its fields.
14186
14187    Therefore caller should follow these rules:
14188      * Try to fetch any prerequisite types we may need to build this DIE type
14189        before building the type and calling set_die_type.
14190      * After building typer call set_die_type for current DIE as soon as
14191        possible before fetching more types to complete the current type.
14192      * Make the type as complete as possible before fetching more types.  */
14193
14194 static struct type *
14195 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14196 {
14197   struct dwarf2_offset_and_type **slot, ofs;
14198
14199   /* For Ada types, make sure that the gnat-specific data is always
14200      initialized (if not already set).  There are a few types where
14201      we should not be doing so, because the type-specific area is
14202      already used to hold some other piece of info (eg: TYPE_CODE_FLT
14203      where the type-specific area is used to store the floatformat).
14204      But this is not a problem, because the gnat-specific information
14205      is actually not needed for these types.  */
14206   if (need_gnat_info (cu)
14207       && TYPE_CODE (type) != TYPE_CODE_FUNC
14208       && TYPE_CODE (type) != TYPE_CODE_FLT
14209       && !HAVE_GNAT_AUX_INFO (type))
14210     INIT_GNAT_SPECIFIC (type);
14211
14212   if (cu->type_hash == NULL)
14213     {
14214       gdb_assert (cu->per_cu != NULL);
14215       cu->per_cu->type_hash
14216         = htab_create_alloc_ex (cu->header.length / 24,
14217                                 offset_and_type_hash,
14218                                 offset_and_type_eq,
14219                                 NULL,
14220                                 &cu->objfile->objfile_obstack,
14221                                 hashtab_obstack_allocate,
14222                                 dummy_obstack_deallocate);
14223       cu->type_hash = cu->per_cu->type_hash;
14224     }
14225
14226   ofs.offset = die->offset;
14227   ofs.type = type;
14228   slot = (struct dwarf2_offset_and_type **)
14229     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
14230   if (*slot)
14231     complaint (&symfile_complaints,
14232                _("A problem internal to GDB: DIE 0x%x has type already set"),
14233                die->offset);
14234   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
14235   **slot = ofs;
14236   return type;
14237 }
14238
14239 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
14240    not have a saved type.  */
14241
14242 static struct type *
14243 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14244 {
14245   struct dwarf2_offset_and_type *slot, ofs;
14246   htab_t type_hash = cu->type_hash;
14247
14248   if (type_hash == NULL)
14249     return NULL;
14250
14251   ofs.offset = die->offset;
14252   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14253   if (slot)
14254     return slot->type;
14255   else
14256     return NULL;
14257 }
14258
14259 /* Add a dependence relationship from CU to REF_PER_CU.  */
14260
14261 static void
14262 dwarf2_add_dependence (struct dwarf2_cu *cu,
14263                        struct dwarf2_per_cu_data *ref_per_cu)
14264 {
14265   void **slot;
14266
14267   if (cu->dependencies == NULL)
14268     cu->dependencies
14269       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14270                               NULL, &cu->comp_unit_obstack,
14271                               hashtab_obstack_allocate,
14272                               dummy_obstack_deallocate);
14273
14274   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14275   if (*slot == NULL)
14276     *slot = ref_per_cu;
14277 }
14278
14279 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14280    Set the mark field in every compilation unit in the
14281    cache that we must keep because we are keeping CU.  */
14282
14283 static int
14284 dwarf2_mark_helper (void **slot, void *data)
14285 {
14286   struct dwarf2_per_cu_data *per_cu;
14287
14288   per_cu = (struct dwarf2_per_cu_data *) *slot;
14289   if (per_cu->cu->mark)
14290     return 1;
14291   per_cu->cu->mark = 1;
14292
14293   if (per_cu->cu->dependencies != NULL)
14294     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14295
14296   return 1;
14297 }
14298
14299 /* Set the mark field in CU and in every other compilation unit in the
14300    cache that we must keep because we are keeping CU.  */
14301
14302 static void
14303 dwarf2_mark (struct dwarf2_cu *cu)
14304 {
14305   if (cu->mark)
14306     return;
14307   cu->mark = 1;
14308   if (cu->dependencies != NULL)
14309     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14310 }
14311
14312 static void
14313 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14314 {
14315   while (per_cu)
14316     {
14317       per_cu->cu->mark = 0;
14318       per_cu = per_cu->cu->read_in_chain;
14319     }
14320 }
14321
14322 /* Trivial hash function for partial_die_info: the hash value of a DIE
14323    is its offset in .debug_info for this objfile.  */
14324
14325 static hashval_t
14326 partial_die_hash (const void *item)
14327 {
14328   const struct partial_die_info *part_die = item;
14329
14330   return part_die->offset;
14331 }
14332
14333 /* Trivial comparison function for partial_die_info structures: two DIEs
14334    are equal if they have the same offset.  */
14335
14336 static int
14337 partial_die_eq (const void *item_lhs, const void *item_rhs)
14338 {
14339   const struct partial_die_info *part_die_lhs = item_lhs;
14340   const struct partial_die_info *part_die_rhs = item_rhs;
14341
14342   return part_die_lhs->offset == part_die_rhs->offset;
14343 }
14344
14345 static struct cmd_list_element *set_dwarf2_cmdlist;
14346 static struct cmd_list_element *show_dwarf2_cmdlist;
14347
14348 static void
14349 set_dwarf2_cmd (char *args, int from_tty)
14350 {
14351   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14352 }
14353
14354 static void
14355 show_dwarf2_cmd (char *args, int from_tty)
14356 {
14357   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14358 }
14359
14360 /* If section described by INFO was mmapped, munmap it now.  */
14361
14362 static void
14363 munmap_section_buffer (struct dwarf2_section_info *info)
14364 {
14365   if (info->was_mmapped)
14366     {
14367 #ifdef HAVE_MMAP
14368       intptr_t begin = (intptr_t) info->buffer;
14369       intptr_t map_begin = begin & ~(pagesize - 1);
14370       size_t map_length = info->size + begin - map_begin;
14371
14372       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14373 #else
14374       /* Without HAVE_MMAP, we should never be here to begin with.  */
14375       gdb_assert_not_reached ("no mmap support");
14376 #endif
14377     }
14378 }
14379
14380 /* munmap debug sections for OBJFILE, if necessary.  */
14381
14382 static void
14383 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14384 {
14385   struct dwarf2_per_objfile *data = d;
14386
14387   munmap_section_buffer (&data->info);
14388   munmap_section_buffer (&data->abbrev);
14389   munmap_section_buffer (&data->line);
14390   munmap_section_buffer (&data->str);
14391   munmap_section_buffer (&data->macinfo);
14392   munmap_section_buffer (&data->ranges);
14393   munmap_section_buffer (&data->loc);
14394   munmap_section_buffer (&data->frame);
14395   munmap_section_buffer (&data->eh_frame);
14396   munmap_section_buffer (&data->gdb_index);
14397 }
14398
14399 \f
14400
14401 /* The contents of the hash table we create when building the string
14402    table.  */
14403 struct strtab_entry
14404 {
14405   offset_type offset;
14406   const char *str;
14407 };
14408
14409 /* Hash function for a strtab_entry.  */
14410 static hashval_t
14411 hash_strtab_entry (const void *e)
14412 {
14413   const struct strtab_entry *entry = e;
14414   return mapped_index_string_hash (entry->str);
14415 }
14416
14417 /* Equality function for a strtab_entry.  */
14418 static int
14419 eq_strtab_entry (const void *a, const void *b)
14420 {
14421   const struct strtab_entry *ea = a;
14422   const struct strtab_entry *eb = b;
14423   return !strcmp (ea->str, eb->str);
14424 }
14425
14426 /* Create a strtab_entry hash table.  */
14427 static htab_t
14428 create_strtab (void)
14429 {
14430   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14431                             xfree, xcalloc, xfree);
14432 }
14433
14434 /* Add a string to the constant pool.  Return the string's offset in
14435    host order.  */
14436 static offset_type
14437 add_string (htab_t table, struct obstack *cpool, const char *str)
14438 {
14439   void **slot;
14440   struct strtab_entry entry;
14441   struct strtab_entry *result;
14442
14443   entry.str = str;
14444   slot = htab_find_slot (table, &entry, INSERT);
14445   if (*slot)
14446     result = *slot;
14447   else
14448     {
14449       result = XNEW (struct strtab_entry);
14450       result->offset = obstack_object_size (cpool);
14451       result->str = str;
14452       obstack_grow_str0 (cpool, str);
14453       *slot = result;
14454     }
14455   return result->offset;
14456 }
14457
14458 /* An entry in the symbol table.  */
14459 struct symtab_index_entry
14460 {
14461   /* The name of the symbol.  */
14462   const char *name;
14463   /* The offset of the name in the constant pool.  */
14464   offset_type index_offset;
14465   /* A sorted vector of the indices of all the CUs that hold an object
14466      of this name.  */
14467   VEC (offset_type) *cu_indices;
14468 };
14469
14470 /* The symbol table.  This is a power-of-2-sized hash table.  */
14471 struct mapped_symtab
14472 {
14473   offset_type n_elements;
14474   offset_type size;
14475   struct symtab_index_entry **data;
14476 };
14477
14478 /* Hash function for a symtab_index_entry.  */
14479 static hashval_t
14480 hash_symtab_entry (const void *e)
14481 {
14482   const struct symtab_index_entry *entry = e;
14483   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14484                          sizeof (offset_type) * VEC_length (offset_type,
14485                                                             entry->cu_indices),
14486                          0);
14487 }
14488
14489 /* Equality function for a symtab_index_entry.  */
14490 static int
14491 eq_symtab_entry (const void *a, const void *b)
14492 {
14493   const struct symtab_index_entry *ea = a;
14494   const struct symtab_index_entry *eb = b;
14495   int len = VEC_length (offset_type, ea->cu_indices);
14496   if (len != VEC_length (offset_type, eb->cu_indices))
14497     return 0;
14498   return !memcmp (VEC_address (offset_type, ea->cu_indices),
14499                   VEC_address (offset_type, eb->cu_indices),
14500                   sizeof (offset_type) * len);
14501 }
14502
14503 /* Destroy a symtab_index_entry.  */
14504 static void
14505 delete_symtab_entry (void *p)
14506 {
14507   struct symtab_index_entry *entry = p;
14508   VEC_free (offset_type, entry->cu_indices);
14509   xfree (entry);
14510 }
14511
14512 /* Create a hash table holding symtab_index_entry objects.  */
14513 static htab_t
14514 create_index_table (void)
14515 {
14516   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14517                             delete_symtab_entry, xcalloc, xfree);
14518 }
14519
14520 /* Create a new mapped symtab object.  */
14521 static struct mapped_symtab *
14522 create_mapped_symtab (void)
14523 {
14524   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14525   symtab->n_elements = 0;
14526   symtab->size = 1024;
14527   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14528   return symtab;
14529 }
14530
14531 /* Destroy a mapped_symtab.  */
14532 static void
14533 cleanup_mapped_symtab (void *p)
14534 {
14535   struct mapped_symtab *symtab = p;
14536   /* The contents of the array are freed when the other hash table is
14537      destroyed.  */
14538   xfree (symtab->data);
14539   xfree (symtab);
14540 }
14541
14542 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
14543    the slot.  */
14544 static struct symtab_index_entry **
14545 find_slot (struct mapped_symtab *symtab, const char *name)
14546 {
14547   offset_type index, step, hash = mapped_index_string_hash (name);
14548
14549   index = hash & (symtab->size - 1);
14550   step = ((hash * 17) & (symtab->size - 1)) | 1;
14551
14552   for (;;)
14553     {
14554       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14555         return &symtab->data[index];
14556       index = (index + step) & (symtab->size - 1);
14557     }
14558 }
14559
14560 /* Expand SYMTAB's hash table.  */
14561 static void
14562 hash_expand (struct mapped_symtab *symtab)
14563 {
14564   offset_type old_size = symtab->size;
14565   offset_type i;
14566   struct symtab_index_entry **old_entries = symtab->data;
14567
14568   symtab->size *= 2;
14569   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14570
14571   for (i = 0; i < old_size; ++i)
14572     {
14573       if (old_entries[i])
14574         {
14575           struct symtab_index_entry **slot = find_slot (symtab,
14576                                                         old_entries[i]->name);
14577           *slot = old_entries[i];
14578         }
14579     }
14580
14581   xfree (old_entries);
14582 }
14583
14584 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
14585    is the index of the CU in which the symbol appears.  */
14586 static void
14587 add_index_entry (struct mapped_symtab *symtab, const char *name,
14588                  offset_type cu_index)
14589 {
14590   struct symtab_index_entry **slot;
14591
14592   ++symtab->n_elements;
14593   if (4 * symtab->n_elements / 3 >= symtab->size)
14594     hash_expand (symtab);
14595
14596   slot = find_slot (symtab, name);
14597   if (!*slot)
14598     {
14599       *slot = XNEW (struct symtab_index_entry);
14600       (*slot)->name = name;
14601       (*slot)->cu_indices = NULL;
14602     }
14603   /* Don't push an index twice.  Due to how we add entries we only
14604      have to check the last one.  */ 
14605   if (VEC_empty (offset_type, (*slot)->cu_indices)
14606       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14607     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14608 }
14609
14610 /* Add a vector of indices to the constant pool.  */
14611 static offset_type
14612 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14613                       struct symtab_index_entry *entry)
14614 {
14615   void **slot;
14616
14617   slot = htab_find_slot (index_table, entry, INSERT);
14618   if (!*slot)
14619     {
14620       offset_type len = VEC_length (offset_type, entry->cu_indices);
14621       offset_type val = MAYBE_SWAP (len);
14622       offset_type iter;
14623       int i;
14624
14625       *slot = entry;
14626       entry->index_offset = obstack_object_size (cpool);
14627
14628       obstack_grow (cpool, &val, sizeof (val));
14629       for (i = 0;
14630            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14631            ++i)
14632         {
14633           val = MAYBE_SWAP (iter);
14634           obstack_grow (cpool, &val, sizeof (val));
14635         }
14636     }
14637   else
14638     {
14639       struct symtab_index_entry *old_entry = *slot;
14640       entry->index_offset = old_entry->index_offset;
14641       entry = old_entry;
14642     }
14643   return entry->index_offset;
14644 }
14645
14646 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14647    constant pool entries going into the obstack CPOOL.  */
14648 static void
14649 write_hash_table (struct mapped_symtab *symtab,
14650                   struct obstack *output, struct obstack *cpool)
14651 {
14652   offset_type i;
14653   htab_t index_table;
14654   htab_t str_table;
14655
14656   index_table = create_index_table ();
14657   str_table = create_strtab ();
14658   /* We add all the index vectors to the constant pool first, to
14659      ensure alignment is ok.  */
14660   for (i = 0; i < symtab->size; ++i)
14661     {
14662       if (symtab->data[i])
14663         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14664     }
14665
14666   /* Now write out the hash table.  */
14667   for (i = 0; i < symtab->size; ++i)
14668     {
14669       offset_type str_off, vec_off;
14670
14671       if (symtab->data[i])
14672         {
14673           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14674           vec_off = symtab->data[i]->index_offset;
14675         }
14676       else
14677         {
14678           /* While 0 is a valid constant pool index, it is not valid
14679              to have 0 for both offsets.  */
14680           str_off = 0;
14681           vec_off = 0;
14682         }
14683
14684       str_off = MAYBE_SWAP (str_off);
14685       vec_off = MAYBE_SWAP (vec_off);
14686
14687       obstack_grow (output, &str_off, sizeof (str_off));
14688       obstack_grow (output, &vec_off, sizeof (vec_off));
14689     }
14690
14691   htab_delete (str_table);
14692   htab_delete (index_table);
14693 }
14694
14695 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
14696    from PST; CU_INDEX is the index of the CU in the vector of all
14697    CUs.  */
14698 static void
14699 add_address_entry (struct objfile *objfile,
14700                    struct obstack *addr_obstack, struct partial_symtab *pst,
14701                    unsigned int cu_index)
14702 {
14703   offset_type offset;
14704   char addr[8];
14705   CORE_ADDR baseaddr;
14706
14707   /* Don't bother recording empty ranges.  */
14708   if (pst->textlow == pst->texthigh)
14709     return;
14710
14711   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14712
14713   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14714   obstack_grow (addr_obstack, addr, 8);
14715   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14716   obstack_grow (addr_obstack, addr, 8);
14717   offset = MAYBE_SWAP (cu_index);
14718   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14719 }
14720
14721 /* Add a list of partial symbols to SYMTAB.  */
14722 static void
14723 write_psymbols (struct mapped_symtab *symtab,
14724                 struct partial_symbol **psymp,
14725                 int count,
14726                 offset_type cu_index)
14727 {
14728   for (; count-- > 0; ++psymp)
14729     {
14730       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14731         error (_("Ada is not currently supported by the index"));
14732       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14733     }
14734 }
14735
14736 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
14737    exception if there is an error.  */
14738 static void
14739 write_obstack (FILE *file, struct obstack *obstack)
14740 {
14741   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14742               file)
14743       != obstack_object_size (obstack))
14744     error (_("couldn't data write to file"));
14745 }
14746
14747 /* Unlink a file if the argument is not NULL.  */
14748 static void
14749 unlink_if_set (void *p)
14750 {
14751   char **filename = p;
14752   if (*filename)
14753     unlink (*filename);
14754 }
14755
14756 /* A helper struct used when iterating over debug_types.  */
14757 struct signatured_type_index_data
14758 {
14759   struct objfile *objfile;
14760   struct mapped_symtab *symtab;
14761   struct obstack *types_list;
14762   int cu_index;
14763 };
14764
14765 /* A helper function that writes a single signatured_type to an
14766    obstack.  */
14767 static int
14768 write_one_signatured_type (void **slot, void *d)
14769 {
14770   struct signatured_type_index_data *info = d;
14771   struct signatured_type *entry = (struct signatured_type *) *slot;
14772   struct dwarf2_per_cu_data *cu = &entry->per_cu;
14773   struct partial_symtab *psymtab = cu->v.psymtab;
14774   gdb_byte val[8];
14775
14776   write_psymbols (info->symtab,
14777                   info->objfile->global_psymbols.list + psymtab->globals_offset,
14778                   psymtab->n_global_syms, info->cu_index);
14779   write_psymbols (info->symtab,
14780                   info->objfile->static_psymbols.list + psymtab->statics_offset,
14781                   psymtab->n_static_syms, info->cu_index);
14782
14783   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14784   obstack_grow (info->types_list, val, 8);
14785   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14786   obstack_grow (info->types_list, val, 8);
14787   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14788   obstack_grow (info->types_list, val, 8);
14789
14790   ++info->cu_index;
14791
14792   return 1;
14793 }
14794
14795 /* Create an index file for OBJFILE in the directory DIR.  */
14796 static void
14797 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14798 {
14799   struct cleanup *cleanup;
14800   char *filename, *cleanup_filename;
14801   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14802   struct obstack cu_list, types_cu_list;
14803   int i;
14804   FILE *out_file;
14805   struct mapped_symtab *symtab;
14806   offset_type val, size_of_contents, total_len;
14807   struct stat st;
14808   char buf[8];
14809
14810   if (!objfile->psymtabs)
14811     return;
14812   if (dwarf2_per_objfile->using_index)
14813     error (_("Cannot use an index to create the index"));
14814
14815   if (stat (objfile->name, &st) < 0)
14816     perror_with_name (_("Could not stat"));
14817
14818   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14819                      INDEX_SUFFIX, (char *) NULL);
14820   cleanup = make_cleanup (xfree, filename);
14821
14822   out_file = fopen (filename, "wb");
14823   if (!out_file)
14824     error (_("Can't open `%s' for writing"), filename);
14825
14826   cleanup_filename = filename;
14827   make_cleanup (unlink_if_set, &cleanup_filename);
14828
14829   symtab = create_mapped_symtab ();
14830   make_cleanup (cleanup_mapped_symtab, symtab);
14831
14832   obstack_init (&addr_obstack);
14833   make_cleanup_obstack_free (&addr_obstack);
14834
14835   obstack_init (&cu_list);
14836   make_cleanup_obstack_free (&cu_list);
14837
14838   obstack_init (&types_cu_list);
14839   make_cleanup_obstack_free (&types_cu_list);
14840
14841   /* The list is already sorted, so we don't need to do additional
14842      work here.  Also, the debug_types entries do not appear in
14843      all_comp_units, but only in their own hash table.  */
14844   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14845     {
14846       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14847       struct partial_symtab *psymtab = cu->v.psymtab;
14848       gdb_byte val[8];
14849
14850       write_psymbols (symtab,
14851                       objfile->global_psymbols.list + psymtab->globals_offset,
14852                       psymtab->n_global_syms, i);
14853       write_psymbols (symtab,
14854                       objfile->static_psymbols.list + psymtab->statics_offset,
14855                       psymtab->n_static_syms, i);
14856
14857       add_address_entry (objfile, &addr_obstack, psymtab, i);
14858
14859       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14860       obstack_grow (&cu_list, val, 8);
14861       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14862       obstack_grow (&cu_list, val, 8);
14863     }
14864
14865   /* Write out the .debug_type entries, if any.  */
14866   if (dwarf2_per_objfile->signatured_types)
14867     {
14868       struct signatured_type_index_data sig_data;
14869
14870       sig_data.objfile = objfile;
14871       sig_data.symtab = symtab;
14872       sig_data.types_list = &types_cu_list;
14873       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14874       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14875                               write_one_signatured_type, &sig_data);
14876     }
14877
14878   obstack_init (&constant_pool);
14879   make_cleanup_obstack_free (&constant_pool);
14880   obstack_init (&symtab_obstack);
14881   make_cleanup_obstack_free (&symtab_obstack);
14882   write_hash_table (symtab, &symtab_obstack, &constant_pool);
14883
14884   obstack_init (&contents);
14885   make_cleanup_obstack_free (&contents);
14886   size_of_contents = 6 * sizeof (offset_type);
14887   total_len = size_of_contents;
14888
14889   /* The version number.  */
14890   val = MAYBE_SWAP (2);
14891   obstack_grow (&contents, &val, sizeof (val));
14892
14893   /* The offset of the CU list from the start of the file.  */
14894   val = MAYBE_SWAP (total_len);
14895   obstack_grow (&contents, &val, sizeof (val));
14896   total_len += obstack_object_size (&cu_list);
14897
14898   /* The offset of the types CU list from the start of the file.  */
14899   val = MAYBE_SWAP (total_len);
14900   obstack_grow (&contents, &val, sizeof (val));
14901   total_len += obstack_object_size (&types_cu_list);
14902
14903   /* The offset of the address table from the start of the file.  */
14904   val = MAYBE_SWAP (total_len);
14905   obstack_grow (&contents, &val, sizeof (val));
14906   total_len += obstack_object_size (&addr_obstack);
14907
14908   /* The offset of the symbol table from the start of the file.  */
14909   val = MAYBE_SWAP (total_len);
14910   obstack_grow (&contents, &val, sizeof (val));
14911   total_len += obstack_object_size (&symtab_obstack);
14912
14913   /* The offset of the constant pool from the start of the file.  */
14914   val = MAYBE_SWAP (total_len);
14915   obstack_grow (&contents, &val, sizeof (val));
14916   total_len += obstack_object_size (&constant_pool);
14917
14918   gdb_assert (obstack_object_size (&contents) == size_of_contents);
14919
14920   write_obstack (out_file, &contents);
14921   write_obstack (out_file, &cu_list);
14922   write_obstack (out_file, &types_cu_list);
14923   write_obstack (out_file, &addr_obstack);
14924   write_obstack (out_file, &symtab_obstack);
14925   write_obstack (out_file, &constant_pool);
14926
14927   fclose (out_file);
14928
14929   /* We want to keep the file, so we set cleanup_filename to NULL
14930      here.  See unlink_if_set.  */
14931   cleanup_filename = NULL;
14932
14933   do_cleanups (cleanup);
14934 }
14935
14936 /* The mapped index file format is designed to be directly mmap()able
14937    on any architecture.  In most cases, a datum is represented using a
14938    little-endian 32-bit integer value, called an offset_type.  Big
14939    endian machines must byte-swap the values before using them.
14940    Exceptions to this rule are noted.  The data is laid out such that
14941    alignment is always respected.
14942
14943    A mapped index consists of several sections.
14944
14945    1. The file header.  This is a sequence of values, of offset_type
14946    unless otherwise noted:
14947    [0] The version number.  Currently 1 or 2.  The differences are
14948    noted below.  Version 1 did not account for .debug_types sections;
14949    the presence of a .debug_types section invalidates any version 1
14950    index that may exist.
14951    [1] The offset, from the start of the file, of the CU list.
14952    [1.5] In version 2, the offset, from the start of the file, of the
14953    types CU list.  This offset does not appear in version 1.  Note
14954    that this can be empty, in which case this offset will be equal to
14955    the next offset.
14956    [2] The offset, from the start of the file, of the address section.
14957    [3] The offset, from the start of the file, of the symbol table.
14958    [4] The offset, from the start of the file, of the constant pool.
14959
14960    2. The CU list.  This is a sequence of pairs of 64-bit
14961    little-endian values, sorted by the CU offset.  The first element
14962    in each pair is the offset of a CU in the .debug_info section.  The
14963    second element in each pair is the length of that CU.  References
14964    to a CU elsewhere in the map are done using a CU index, which is
14965    just the 0-based index into this table.  Note that if there are
14966    type CUs, then conceptually CUs and type CUs form a single list for
14967    the purposes of CU indices.
14968
14969    2.5 The types CU list.  This does not appear in a version 1 index.
14970    This is a sequence of triplets of 64-bit little-endian values.  In
14971    a triplet, the first value is the CU offset, the second value is
14972    the type offset in the CU, and the third value is the type
14973    signature.  The types CU list is not sorted.
14974
14975    3. The address section.  The address section consists of a sequence
14976    of address entries.  Each address entry has three elements.
14977    [0] The low address.  This is a 64-bit little-endian value.
14978    [1] The high address.  This is a 64-bit little-endian value.
14979    [2] The CU index.  This is an offset_type value.
14980
14981    4. The symbol table.  This is a hash table.  The size of the hash
14982    table is always a power of 2.  The initial hash and the step are
14983    currently defined by the `find_slot' function.
14984
14985    Each slot in the hash table consists of a pair of offset_type
14986    values.  The first value is the offset of the symbol's name in the
14987    constant pool.  The second value is the offset of the CU vector in
14988    the constant pool.
14989
14990    If both values are 0, then this slot in the hash table is empty.
14991    This is ok because while 0 is a valid constant pool index, it
14992    cannot be a valid index for both a string and a CU vector.
14993
14994    A string in the constant pool is stored as a \0-terminated string,
14995    as you'd expect.
14996
14997    A CU vector in the constant pool is a sequence of offset_type
14998    values.  The first value is the number of CU indices in the vector.
14999    Each subsequent value is the index of a CU in the CU list.  This
15000    element in the hash table is used to indicate which CUs define the
15001    symbol.
15002
15003    5. The constant pool.  This is simply a bunch of bytes.  It is
15004    organized so that alignment is correct: CU vectors are stored
15005    first, followed by strings.  */
15006 static void
15007 save_gdb_index_command (char *arg, int from_tty)
15008 {
15009   struct objfile *objfile;
15010
15011   if (!arg || !*arg)
15012     error (_("usage: save gdb-index DIRECTORY"));
15013
15014   ALL_OBJFILES (objfile)
15015   {
15016     struct stat st;
15017
15018     /* If the objfile does not correspond to an actual file, skip it.  */
15019     if (stat (objfile->name, &st) < 0)
15020       continue;
15021
15022     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15023     if (dwarf2_per_objfile)
15024       {
15025         volatile struct gdb_exception except;
15026
15027         TRY_CATCH (except, RETURN_MASK_ERROR)
15028           {
15029             write_psymtabs_to_index (objfile, arg);
15030           }
15031         if (except.reason < 0)
15032           exception_fprintf (gdb_stderr, except,
15033                              _("Error while writing index for `%s': "),
15034                              objfile->name);
15035       }
15036   }
15037 }
15038
15039 \f
15040
15041 int dwarf2_always_disassemble;
15042
15043 static void
15044 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15045                                 struct cmd_list_element *c, const char *value)
15046 {
15047   fprintf_filtered (file, _("\
15048 Whether to always disassemble DWARF expressions is %s.\n"),
15049                     value);
15050 }
15051
15052 void _initialize_dwarf2_read (void);
15053
15054 void
15055 _initialize_dwarf2_read (void)
15056 {
15057   struct cmd_list_element *c;
15058
15059   dwarf2_objfile_data_key
15060     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15061
15062   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15063 Set DWARF 2 specific variables.\n\
15064 Configure DWARF 2 variables such as the cache size"),
15065                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15066                   0/*allow-unknown*/, &maintenance_set_cmdlist);
15067
15068   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15069 Show DWARF 2 specific variables\n\
15070 Show DWARF 2 variables such as the cache size"),
15071                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15072                   0/*allow-unknown*/, &maintenance_show_cmdlist);
15073
15074   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15075                             &dwarf2_max_cache_age, _("\
15076 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15077 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15078 A higher limit means that cached compilation units will be stored\n\
15079 in memory longer, and more total memory will be used.  Zero disables\n\
15080 caching, which can slow down startup."),
15081                             NULL,
15082                             show_dwarf2_max_cache_age,
15083                             &set_dwarf2_cmdlist,
15084                             &show_dwarf2_cmdlist);
15085
15086   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15087                            &dwarf2_always_disassemble, _("\
15088 Set whether `info address' always disassembles DWARF expressions."), _("\
15089 Show whether `info address' always disassembles DWARF expressions."), _("\
15090 When enabled, DWARF expressions are always printed in an assembly-like\n\
15091 syntax.  When disabled, expressions will be printed in a more\n\
15092 conversational style, when possible."),
15093                            NULL,
15094                            show_dwarf2_always_disassemble,
15095                            &set_dwarf2_cmdlist,
15096                            &show_dwarf2_cmdlist);
15097
15098   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15099 Set debugging of the dwarf2 DIE reader."), _("\
15100 Show debugging of the dwarf2 DIE reader."), _("\
15101 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15102 The value is the maximum depth to print."),
15103                             NULL,
15104                             NULL,
15105                             &setdebuglist, &showdebuglist);
15106
15107   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15108                _("Save a .gdb-index file"),
15109                &save_cmdlist);
15110   set_cmd_completer (c, filename_completer);
15111 }