OSDN Git Service

* dwarf2read.c (dwarf2_per_cu_text_offset): Fix reference to
[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
57 #include <fcntl.h>
58 #include "gdb_string.h"
59 #include "gdb_assert.h"
60 #include <sys/types.h>
61 #ifdef HAVE_ZLIB_H
62 #include <zlib.h>
63 #endif
64 #ifdef HAVE_MMAP
65 #include <sys/mman.h>
66 #ifndef MAP_FAILED
67 #define MAP_FAILED ((void *) -1)
68 #endif
69 #endif
70
71 #if 0
72 /* .debug_info header for a compilation unit
73    Because of alignment constraints, this structure has padding and cannot
74    be mapped directly onto the beginning of the .debug_info section.  */
75 typedef struct comp_unit_header
76   {
77     unsigned int length;        /* length of the .debug_info
78                                    contribution */
79     unsigned short version;     /* version number -- 2 for DWARF
80                                    version 2 */
81     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
82     unsigned char addr_size;    /* byte size of an address -- 4 */
83   }
84 _COMP_UNIT_HEADER;
85 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
86 #endif
87
88 /* .debug_line statement program prologue
89    Because of alignment constraints, this structure has padding and cannot
90    be mapped directly onto the beginning of the .debug_info section.  */
91 typedef struct statement_prologue
92   {
93     unsigned int total_length;  /* byte length of the statement
94                                    information */
95     unsigned short version;     /* version number -- 2 for DWARF
96                                    version 2 */
97     unsigned int prologue_length;       /* # bytes between prologue &
98                                            stmt program */
99     unsigned char minimum_instruction_length;   /* byte size of
100                                                    smallest instr */
101     unsigned char default_is_stmt;      /* initial value of is_stmt
102                                            register */
103     char line_base;
104     unsigned char line_range;
105     unsigned char opcode_base;  /* number assigned to first special
106                                    opcode */
107     unsigned char *standard_opcode_lengths;
108   }
109 _STATEMENT_PROLOGUE;
110
111 /* When non-zero, dump DIEs after they are read in.  */
112 static int dwarf2_die_debug = 0;
113
114 static int pagesize;
115
116 /* When set, the file that we're processing is known to have debugging
117    info for C++ namespaces.  GCC 3.3.x did not produce this information,
118    but later versions do.  */
119
120 static int processing_has_namespace_info;
121
122 static const struct objfile_data *dwarf2_objfile_data_key;
123
124 struct dwarf2_section_info
125 {
126   asection *asection;
127   gdb_byte *buffer;
128   bfd_size_type size;
129   int was_mmapped;
130   /* True if we have tried to read this section.  */
131   int readin;
132 };
133
134 /* All offsets in the index are of this type.  It must be
135    architecture-independent.  */
136 typedef uint32_t offset_type;
137
138 DEF_VEC_I (offset_type);
139
140 /* A description of the mapped index.  The file format is described in
141    a comment by the code that writes the index.  */
142 struct mapped_index
143 {
144   /* The total length of the buffer.  */
145   off_t total_size;
146   /* A pointer to the address table data.  */
147   const gdb_byte *address_table;
148   /* Size of the address table data in bytes.  */
149   offset_type address_table_size;
150   /* The hash table.  */
151   const offset_type *index_table;
152   /* Size in slots, each slot is 2 offset_types.  */
153   offset_type index_table_slots;
154   /* A pointer to the constant pool.  */
155   const char *constant_pool;
156 };
157
158 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_data_ptr;
159 DEF_VEC_P (dwarf2_per_cu_data_ptr);
160
161 struct dwarf2_per_objfile
162 {
163   struct dwarf2_section_info info;
164   struct dwarf2_section_info abbrev;
165   struct dwarf2_section_info line;
166   struct dwarf2_section_info loc;
167   struct dwarf2_section_info macinfo;
168   struct dwarf2_section_info str;
169   struct dwarf2_section_info ranges;
170   struct dwarf2_section_info types;
171   struct dwarf2_section_info frame;
172   struct dwarf2_section_info eh_frame;
173   struct dwarf2_section_info gdb_index;
174
175   /* Back link.  */
176   struct objfile *objfile;
177
178   /* A list of all the compilation units.  This is used to locate
179      the target compilation unit of a particular reference.  */
180   struct dwarf2_per_cu_data **all_comp_units;
181
182   /* The number of compilation units in ALL_COMP_UNITS.  */
183   int n_comp_units;
184
185   /* A chain of compilation units that are currently read in, so that
186      they can be freed later.  */
187   struct dwarf2_per_cu_data *read_in_chain;
188
189   /* A table mapping .debug_types signatures to its signatured_type entry.
190      This is NULL if the .debug_types section hasn't been read in yet.  */
191   htab_t signatured_types;
192
193   /* A flag indicating wether this objfile has a section loaded at a
194      VMA of 0.  */
195   int has_section_at_zero;
196
197   /* True if we are using the mapped index.  */
198   unsigned char using_index;
199
200   /* The mapped index.  */
201   struct mapped_index *index_table;
202 };
203
204 static struct dwarf2_per_objfile *dwarf2_per_objfile;
205
206 /* names of the debugging sections */
207
208 /* Note that if the debugging section has been compressed, it might
209    have a name like .zdebug_info.  */
210
211 #define INFO_SECTION     "debug_info"
212 #define ABBREV_SECTION   "debug_abbrev"
213 #define LINE_SECTION     "debug_line"
214 #define LOC_SECTION      "debug_loc"
215 #define MACINFO_SECTION  "debug_macinfo"
216 #define STR_SECTION      "debug_str"
217 #define RANGES_SECTION   "debug_ranges"
218 #define TYPES_SECTION    "debug_types"
219 #define FRAME_SECTION    "debug_frame"
220 #define EH_FRAME_SECTION "eh_frame"
221 #define GDB_INDEX_SECTION "gdb_index"
222
223 /* local data types */
224
225 /* We hold several abbreviation tables in memory at the same time. */
226 #ifndef ABBREV_HASH_SIZE
227 #define ABBREV_HASH_SIZE 121
228 #endif
229
230 /* The data in a compilation unit header, after target2host
231    translation, looks like this.  */
232 struct comp_unit_head
233 {
234   unsigned int length;
235   short version;
236   unsigned char addr_size;
237   unsigned char signed_addr_p;
238   unsigned int abbrev_offset;
239
240   /* Size of file offsets; either 4 or 8.  */
241   unsigned int offset_size;
242
243   /* Size of the length field; either 4 or 12.  */
244   unsigned int initial_length_size;
245
246   /* Offset to the first byte of this compilation unit header in the
247      .debug_info section, for resolving relative reference dies.  */
248   unsigned int offset;
249
250   /* Offset to first die in this cu from the start of the cu.
251      This will be the first byte following the compilation unit header.  */
252   unsigned int first_die_offset;
253 };
254
255 /* Internal state when decoding a particular compilation unit.  */
256 struct dwarf2_cu
257 {
258   /* The objfile containing this compilation unit.  */
259   struct objfile *objfile;
260
261   /* The header of the compilation unit.  */
262   struct comp_unit_head header;
263
264   /* Base address of this compilation unit.  */
265   CORE_ADDR base_address;
266
267   /* Non-zero if base_address has been set.  */
268   int base_known;
269
270   struct function_range *first_fn, *last_fn, *cached_fn;
271
272   /* The language we are debugging.  */
273   enum language language;
274   const struct language_defn *language_defn;
275
276   const char *producer;
277
278   /* The generic symbol table building routines have separate lists for
279      file scope symbols and all all other scopes (local scopes).  So
280      we need to select the right one to pass to add_symbol_to_list().
281      We do it by keeping a pointer to the correct list in list_in_scope.
282
283      FIXME: The original dwarf code just treated the file scope as the
284      first local scope, and all other local scopes as nested local
285      scopes, and worked fine.  Check to see if we really need to
286      distinguish these in buildsym.c.  */
287   struct pending **list_in_scope;
288
289   /* DWARF abbreviation table associated with this compilation unit.  */
290   struct abbrev_info **dwarf2_abbrevs;
291
292   /* Storage for the abbrev table.  */
293   struct obstack abbrev_obstack;
294
295   /* Hash table holding all the loaded partial DIEs.  */
296   htab_t partial_dies;
297
298   /* Storage for things with the same lifetime as this read-in compilation
299      unit, including partial DIEs.  */
300   struct obstack comp_unit_obstack;
301
302   /* When multiple dwarf2_cu structures are living in memory, this field
303      chains them all together, so that they can be released efficiently.
304      We will probably also want a generation counter so that most-recently-used
305      compilation units are cached...  */
306   struct dwarf2_per_cu_data *read_in_chain;
307
308   /* Backchain to our per_cu entry if the tree has been built.  */
309   struct dwarf2_per_cu_data *per_cu;
310
311   /* Pointer to the die -> type map.  Although it is stored
312      permanently in per_cu, we copy it here to avoid double
313      indirection.  */
314   htab_t type_hash;
315
316   /* How many compilation units ago was this CU last referenced?  */
317   int last_used;
318
319   /* A hash table of die offsets for following references.  */
320   htab_t die_hash;
321
322   /* Full DIEs if read in.  */
323   struct die_info *dies;
324
325   /* A set of pointers to dwarf2_per_cu_data objects for compilation
326      units referenced by this one.  Only set during full symbol processing;
327      partial symbol tables do not have dependencies.  */
328   htab_t dependencies;
329
330   /* Header data from the line table, during full symbol processing.  */
331   struct line_header *line_header;
332
333   /* Mark used when releasing cached dies.  */
334   unsigned int mark : 1;
335
336   /* This flag will be set if this compilation unit might include
337      inter-compilation-unit references.  */
338   unsigned int has_form_ref_addr : 1;
339
340   /* This flag will be set if this compilation unit includes any
341      DW_TAG_namespace DIEs.  If we know that there are explicit
342      DIEs for namespaces, we don't need to try to infer them
343      from mangled names.  */
344   unsigned int has_namespace_info : 1;
345 };
346
347 /* When using the index (and thus not using psymtabs), each CU has an
348    object of this type.  This is used to hold information needed by
349    the various "quick" methods.  */
350 struct dwarf2_per_cu_quick_data
351 {
352   /* The line table.  This can be NULL if there was no line table.  */
353   struct line_header *lines;
354
355   /* The file names from the line table.  */
356   const char **file_names;
357   /* The file names from the line table after being run through
358      gdb_realpath.  */
359   const char **full_names;
360
361   /* The corresponding symbol table.  This is NULL if symbols for this
362      CU have not yet been read.  */
363   struct symtab *symtab;
364
365   /* A temporary mark bit used when iterating over all CUs in
366      expand_symtabs_matching.  */
367   unsigned int mark : 1;
368
369   /* True if we've tried to read the line table.  */
370   unsigned int read_lines : 1;
371 };
372
373 /* Persistent data held for a compilation unit, even when not
374    processing it.  We put a pointer to this structure in the
375    read_symtab_private field of the psymtab.  If we encounter
376    inter-compilation-unit references, we also maintain a sorted
377    list of all compilation units.  */
378
379 struct dwarf2_per_cu_data
380 {
381   /* The start offset and length of this compilation unit.  2**29-1
382      bytes should suffice to store the length of any compilation unit
383      - if it doesn't, GDB will fall over anyway.
384      NOTE: Unlike comp_unit_head.length, this length includes
385      initial_length_size.  */
386   unsigned int offset;
387   unsigned int length : 29;
388
389   /* Flag indicating this compilation unit will be read in before
390      any of the current compilation units are processed.  */
391   unsigned int queued : 1;
392
393   /* This flag will be set if we need to load absolutely all DIEs
394      for this compilation unit, instead of just the ones we think
395      are interesting.  It gets set if we look for a DIE in the
396      hash table and don't find it.  */
397   unsigned int load_all_dies : 1;
398
399   /* Non-zero if this CU is from .debug_types.
400      Otherwise it's from .debug_info.  */
401   unsigned int from_debug_types : 1;
402
403   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
404      of the CU cache it gets reset to NULL again.  */
405   struct dwarf2_cu *cu;
406
407   /* If full symbols for this CU have been read in, then this field
408      holds a map of DIE offsets to types.  It isn't always possible
409      to reconstruct this information later, so we have to preserve
410      it.  */
411   htab_t type_hash;
412
413   /* The corresponding objfile.  */
414   struct objfile *objfile;
415
416   /* When using partial symbol tables, the 'psymtab' field is active.
417      Otherwise the 'quick' field is active.  */
418   union
419   {
420     /* The partial symbol table associated with this compilation unit,
421        or NULL for partial units (which do not have an associated
422        symtab).  */
423     struct partial_symtab *psymtab;
424
425     /* Data needed by the "quick" functions.  */
426     struct dwarf2_per_cu_quick_data *quick;
427   } v;
428 };
429
430 /* Entry in the signatured_types hash table.  */
431
432 struct signatured_type
433 {
434   ULONGEST signature;
435
436   /* Offset in .debug_types of the TU (type_unit) for this type.  */
437   unsigned int offset;
438
439   /* Offset in .debug_types of the type defined by this TU.  */
440   unsigned int type_offset;
441
442   /* The CU(/TU) of this type.  */
443   struct dwarf2_per_cu_data per_cu;
444 };
445
446 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
447    which are used for both .debug_info and .debug_types dies.
448    All parameters here are unchanging for the life of the call.
449    This struct exists to abstract away the constant parameters of
450    die reading.  */
451
452 struct die_reader_specs
453 {
454   /* The bfd of this objfile.  */
455   bfd* abfd;
456
457   /* The CU of the DIE we are parsing.  */
458   struct dwarf2_cu *cu;
459
460   /* Pointer to start of section buffer.
461      This is either the start of .debug_info or .debug_types.  */
462   const gdb_byte *buffer;
463 };
464
465 /* The line number information for a compilation unit (found in the
466    .debug_line section) begins with a "statement program header",
467    which contains the following information.  */
468 struct line_header
469 {
470   unsigned int total_length;
471   unsigned short version;
472   unsigned int header_length;
473   unsigned char minimum_instruction_length;
474   unsigned char maximum_ops_per_instruction;
475   unsigned char default_is_stmt;
476   int line_base;
477   unsigned char line_range;
478   unsigned char opcode_base;
479
480   /* standard_opcode_lengths[i] is the number of operands for the
481      standard opcode whose value is i.  This means that
482      standard_opcode_lengths[0] is unused, and the last meaningful
483      element is standard_opcode_lengths[opcode_base - 1].  */
484   unsigned char *standard_opcode_lengths;
485
486   /* The include_directories table.  NOTE!  These strings are not
487      allocated with xmalloc; instead, they are pointers into
488      debug_line_buffer.  If you try to free them, `free' will get
489      indigestion.  */
490   unsigned int num_include_dirs, include_dirs_size;
491   char **include_dirs;
492
493   /* The file_names table.  NOTE!  These strings are not allocated
494      with xmalloc; instead, they are pointers into debug_line_buffer.
495      Don't try to free them directly.  */
496   unsigned int num_file_names, file_names_size;
497   struct file_entry
498   {
499     char *name;
500     unsigned int dir_index;
501     unsigned int mod_time;
502     unsigned int length;
503     int included_p; /* Non-zero if referenced by the Line Number Program.  */
504     struct symtab *symtab; /* The associated symbol table, if any.  */
505   } *file_names;
506
507   /* The start and end of the statement program following this
508      header.  These point into dwarf2_per_objfile->line_buffer.  */
509   gdb_byte *statement_program_start, *statement_program_end;
510 };
511
512 /* When we construct a partial symbol table entry we only
513    need this much information. */
514 struct partial_die_info
515   {
516     /* Offset of this DIE.  */
517     unsigned int offset;
518
519     /* DWARF-2 tag for this DIE.  */
520     ENUM_BITFIELD(dwarf_tag) tag : 16;
521
522     /* Assorted flags describing the data found in this DIE.  */
523     unsigned int has_children : 1;
524     unsigned int is_external : 1;
525     unsigned int is_declaration : 1;
526     unsigned int has_type : 1;
527     unsigned int has_specification : 1;
528     unsigned int has_pc_info : 1;
529
530     /* Flag set if the SCOPE field of this structure has been
531        computed.  */
532     unsigned int scope_set : 1;
533
534     /* Flag set if the DIE has a byte_size attribute.  */
535     unsigned int has_byte_size : 1;
536
537     /* The name of this DIE.  Normally the value of DW_AT_name, but
538        sometimes a default name for unnamed DIEs.  */
539     char *name;
540
541     /* The scope to prepend to our children.  This is generally
542        allocated on the comp_unit_obstack, so will disappear
543        when this compilation unit leaves the cache.  */
544     char *scope;
545
546     /* The location description associated with this DIE, if any.  */
547     struct dwarf_block *locdesc;
548
549     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
550     CORE_ADDR lowpc;
551     CORE_ADDR highpc;
552
553     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
554        DW_AT_sibling, if any.  */
555     gdb_byte *sibling;
556
557     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
558        DW_AT_specification (or DW_AT_abstract_origin or
559        DW_AT_extension).  */
560     unsigned int spec_offset;
561
562     /* Pointers to this DIE's parent, first child, and next sibling,
563        if any.  */
564     struct partial_die_info *die_parent, *die_child, *die_sibling;
565   };
566
567 /* This data structure holds the information of an abbrev. */
568 struct abbrev_info
569   {
570     unsigned int number;        /* number identifying abbrev */
571     enum dwarf_tag tag;         /* dwarf tag */
572     unsigned short has_children;                /* boolean */
573     unsigned short num_attrs;   /* number of attributes */
574     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
575     struct abbrev_info *next;   /* next in chain */
576   };
577
578 struct attr_abbrev
579   {
580     ENUM_BITFIELD(dwarf_attribute) name : 16;
581     ENUM_BITFIELD(dwarf_form) form : 16;
582   };
583
584 /* Attributes have a name and a value */
585 struct attribute
586   {
587     ENUM_BITFIELD(dwarf_attribute) name : 16;
588     ENUM_BITFIELD(dwarf_form) form : 15;
589
590     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
591        field should be in u.str (existing only for DW_STRING) but it is kept
592        here for better struct attribute alignment.  */
593     unsigned int string_is_canonical : 1;
594
595     union
596       {
597         char *str;
598         struct dwarf_block *blk;
599         ULONGEST unsnd;
600         LONGEST snd;
601         CORE_ADDR addr;
602         struct signatured_type *signatured_type;
603       }
604     u;
605   };
606
607 /* This data structure holds a complete die structure. */
608 struct die_info
609   {
610     /* DWARF-2 tag for this DIE.  */
611     ENUM_BITFIELD(dwarf_tag) tag : 16;
612
613     /* Number of attributes */
614     unsigned short num_attrs;
615
616     /* Abbrev number */
617     unsigned int abbrev;
618
619     /* Offset in .debug_info or .debug_types section.  */
620     unsigned int offset;
621
622     /* The dies in a compilation unit form an n-ary tree.  PARENT
623        points to this die's parent; CHILD points to the first child of
624        this node; and all the children of a given node are chained
625        together via their SIBLING fields, terminated by a die whose
626        tag is zero.  */
627     struct die_info *child;     /* Its first child, if any.  */
628     struct die_info *sibling;   /* Its next sibling, if any.  */
629     struct die_info *parent;    /* Its parent, if any.  */
630
631     /* An array of attributes, with NUM_ATTRS elements.  There may be
632        zero, but it's not common and zero-sized arrays are not
633        sufficiently portable C.  */
634     struct attribute attrs[1];
635   };
636
637 struct function_range
638 {
639   const char *name;
640   CORE_ADDR lowpc, highpc;
641   int seen_line;
642   struct function_range *next;
643 };
644
645 /* Get at parts of an attribute structure */
646
647 #define DW_STRING(attr)    ((attr)->u.str)
648 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
649 #define DW_UNSND(attr)     ((attr)->u.unsnd)
650 #define DW_BLOCK(attr)     ((attr)->u.blk)
651 #define DW_SND(attr)       ((attr)->u.snd)
652 #define DW_ADDR(attr)      ((attr)->u.addr)
653 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
654
655 /* Blocks are a bunch of untyped bytes. */
656 struct dwarf_block
657   {
658     unsigned int size;
659     gdb_byte *data;
660   };
661
662 #ifndef ATTR_ALLOC_CHUNK
663 #define ATTR_ALLOC_CHUNK 4
664 #endif
665
666 /* Allocate fields for structs, unions and enums in this size.  */
667 #ifndef DW_FIELD_ALLOC_CHUNK
668 #define DW_FIELD_ALLOC_CHUNK 4
669 #endif
670
671 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
672    but this would require a corresponding change in unpack_field_as_long
673    and friends.  */
674 static int bits_per_byte = 8;
675
676 /* The routines that read and process dies for a C struct or C++ class
677    pass lists of data member fields and lists of member function fields
678    in an instance of a field_info structure, as defined below.  */
679 struct field_info
680   {
681     /* List of data member and baseclasses fields. */
682     struct nextfield
683       {
684         struct nextfield *next;
685         int accessibility;
686         int virtuality;
687         struct field field;
688       }
689      *fields, *baseclasses;
690
691     /* Number of fields (including baseclasses).  */
692     int nfields;
693
694     /* Number of baseclasses.  */
695     int nbaseclasses;
696
697     /* Set if the accesibility of one of the fields is not public.  */
698     int non_public_fields;
699
700     /* Member function fields array, entries are allocated in the order they
701        are encountered in the object file.  */
702     struct nextfnfield
703       {
704         struct nextfnfield *next;
705         struct fn_field fnfield;
706       }
707      *fnfields;
708
709     /* Member function fieldlist array, contains name of possibly overloaded
710        member function, number of overloaded member functions and a pointer
711        to the head of the member function field chain.  */
712     struct fnfieldlist
713       {
714         char *name;
715         int length;
716         struct nextfnfield *head;
717       }
718      *fnfieldlists;
719
720     /* Number of entries in the fnfieldlists array.  */
721     int nfnfields;
722
723     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
724        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
725     struct typedef_field_list
726       {
727         struct typedef_field field;
728         struct typedef_field_list *next;
729       }
730     *typedef_field_list;
731     unsigned typedef_field_list_count;
732   };
733
734 /* One item on the queue of compilation units to read in full symbols
735    for.  */
736 struct dwarf2_queue_item
737 {
738   struct dwarf2_per_cu_data *per_cu;
739   struct dwarf2_queue_item *next;
740 };
741
742 /* The current queue.  */
743 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
744
745 /* Loaded secondary compilation units are kept in memory until they
746    have not been referenced for the processing of this many
747    compilation units.  Set this to zero to disable caching.  Cache
748    sizes of up to at least twenty will improve startup time for
749    typical inter-CU-reference binaries, at an obvious memory cost.  */
750 static int dwarf2_max_cache_age = 5;
751 static void
752 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
753                            struct cmd_list_element *c, const char *value)
754 {
755   fprintf_filtered (file, _("\
756 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
757                     value);
758 }
759
760
761 /* Various complaints about symbol reading that don't abort the process */
762
763 static void
764 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
765 {
766   complaint (&symfile_complaints,
767              _("statement list doesn't fit in .debug_line section"));
768 }
769
770 static void
771 dwarf2_debug_line_missing_file_complaint (void)
772 {
773   complaint (&symfile_complaints,
774              _(".debug_line section has line data without a file"));
775 }
776
777 static void
778 dwarf2_debug_line_missing_end_sequence_complaint (void)
779 {
780   complaint (&symfile_complaints,
781              _(".debug_line section has line program sequence without an end"));
782 }
783
784 static void
785 dwarf2_complex_location_expr_complaint (void)
786 {
787   complaint (&symfile_complaints, _("location expression too complex"));
788 }
789
790 static void
791 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
792                                               int arg3)
793 {
794   complaint (&symfile_complaints,
795              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
796              arg2, arg3);
797 }
798
799 static void
800 dwarf2_macros_too_long_complaint (void)
801 {
802   complaint (&symfile_complaints,
803              _("macro info runs off end of `.debug_macinfo' section"));
804 }
805
806 static void
807 dwarf2_macro_malformed_definition_complaint (const char *arg1)
808 {
809   complaint (&symfile_complaints,
810              _("macro debug info contains a malformed macro definition:\n`%s'"),
811              arg1);
812 }
813
814 static void
815 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
816 {
817   complaint (&symfile_complaints,
818              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
819 }
820
821 /* local function prototypes */
822
823 static void dwarf2_locate_sections (bfd *, asection *, void *);
824
825 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
826                                            struct objfile *);
827
828 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
829                                            struct die_info *,
830                                            struct partial_symtab *);
831
832 static void dwarf2_build_psymtabs_hard (struct objfile *);
833
834 static void scan_partial_symbols (struct partial_die_info *,
835                                   CORE_ADDR *, CORE_ADDR *,
836                                   int, struct dwarf2_cu *);
837
838 static void add_partial_symbol (struct partial_die_info *,
839                                 struct dwarf2_cu *);
840
841 static void add_partial_namespace (struct partial_die_info *pdi,
842                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
843                                    int need_pc, struct dwarf2_cu *cu);
844
845 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
846                                 CORE_ADDR *highpc, int need_pc,
847                                 struct dwarf2_cu *cu);
848
849 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
850                                      struct dwarf2_cu *cu);
851
852 static void add_partial_subprogram (struct partial_die_info *pdi,
853                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
854                                     int need_pc, struct dwarf2_cu *cu);
855
856 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
857                                      gdb_byte *buffer, gdb_byte *info_ptr,
858                                      bfd *abfd, struct dwarf2_cu *cu);
859
860 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
861
862 static void psymtab_to_symtab_1 (struct partial_symtab *);
863
864 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
865
866 static void dwarf2_free_abbrev_table (void *);
867
868 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
869                                             struct dwarf2_cu *);
870
871 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
872                                                  struct dwarf2_cu *);
873
874 static struct partial_die_info *load_partial_dies (bfd *,
875                                                    gdb_byte *, gdb_byte *,
876                                                    int, struct dwarf2_cu *);
877
878 static gdb_byte *read_partial_die (struct partial_die_info *,
879                                    struct abbrev_info *abbrev,
880                                    unsigned int, bfd *,
881                                    gdb_byte *, gdb_byte *,
882                                    struct dwarf2_cu *);
883
884 static struct partial_die_info *find_partial_die (unsigned int,
885                                                   struct dwarf2_cu *);
886
887 static void fixup_partial_die (struct partial_die_info *,
888                                struct dwarf2_cu *);
889
890 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
891                                  bfd *, gdb_byte *, struct dwarf2_cu *);
892
893 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
894                                        bfd *, gdb_byte *, struct dwarf2_cu *);
895
896 static unsigned int read_1_byte (bfd *, gdb_byte *);
897
898 static int read_1_signed_byte (bfd *, gdb_byte *);
899
900 static unsigned int read_2_bytes (bfd *, gdb_byte *);
901
902 static unsigned int read_4_bytes (bfd *, gdb_byte *);
903
904 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
905
906 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
907                                unsigned int *);
908
909 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
910
911 static LONGEST read_checked_initial_length_and_offset
912   (bfd *, gdb_byte *, const struct comp_unit_head *,
913    unsigned int *, unsigned int *);
914
915 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
916                             unsigned int *);
917
918 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
919
920 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
921
922 static char *read_string (bfd *, gdb_byte *, unsigned int *);
923
924 static char *read_indirect_string (bfd *, gdb_byte *,
925                                    const struct comp_unit_head *,
926                                    unsigned int *);
927
928 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
929
930 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
931
932 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
933
934 static void set_cu_language (unsigned int, struct dwarf2_cu *);
935
936 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
937                                       struct dwarf2_cu *);
938
939 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
940                                                 unsigned int,
941                                                 struct dwarf2_cu *);
942
943 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
944                                struct dwarf2_cu *cu);
945
946 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
947
948 static struct die_info *die_specification (struct die_info *die,
949                                            struct dwarf2_cu **);
950
951 static void free_line_header (struct line_header *lh);
952
953 static void add_file_name (struct line_header *, char *, unsigned int,
954                            unsigned int, unsigned int);
955
956 static struct line_header *(dwarf_decode_line_header
957                             (unsigned int offset,
958                              bfd *abfd, struct dwarf2_cu *cu));
959
960 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
961                                 struct dwarf2_cu *, struct partial_symtab *);
962
963 static void dwarf2_start_subfile (char *, char *, char *);
964
965 static struct symbol *new_symbol (struct die_info *, struct type *,
966                                   struct dwarf2_cu *);
967
968 static void dwarf2_const_value (struct attribute *, struct symbol *,
969                                 struct dwarf2_cu *);
970
971 static void dwarf2_const_value_data (struct attribute *attr,
972                                      struct symbol *sym,
973                                      int bits);
974
975 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
976
977 static int need_gnat_info (struct dwarf2_cu *);
978
979 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
980
981 static void set_descriptive_type (struct type *, struct die_info *,
982                                   struct dwarf2_cu *);
983
984 static struct type *die_containing_type (struct die_info *,
985                                          struct dwarf2_cu *);
986
987 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
988
989 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
990
991 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
992
993 static char *typename_concat (struct obstack *obs, const char *prefix,
994                               const char *suffix, int physname,
995                               struct dwarf2_cu *cu);
996
997 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
998
999 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1000
1001 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1002
1003 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1004
1005 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1006                                struct dwarf2_cu *, struct partial_symtab *);
1007
1008 static int dwarf2_get_pc_bounds (struct die_info *,
1009                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1010                                  struct partial_symtab *);
1011
1012 static void get_scope_pc_bounds (struct die_info *,
1013                                  CORE_ADDR *, CORE_ADDR *,
1014                                  struct dwarf2_cu *);
1015
1016 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1017                                         CORE_ADDR, struct dwarf2_cu *);
1018
1019 static void dwarf2_add_field (struct field_info *, struct die_info *,
1020                               struct dwarf2_cu *);
1021
1022 static void dwarf2_attach_fields_to_type (struct field_info *,
1023                                           struct type *, struct dwarf2_cu *);
1024
1025 static void dwarf2_add_member_fn (struct field_info *,
1026                                   struct die_info *, struct type *,
1027                                   struct dwarf2_cu *);
1028
1029 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1030                                              struct type *, struct dwarf2_cu *);
1031
1032 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1033
1034 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1035
1036 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1037
1038 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1039
1040 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1041
1042 static struct type *read_module_type (struct die_info *die,
1043                                       struct dwarf2_cu *cu);
1044
1045 static const char *namespace_name (struct die_info *die,
1046                                    int *is_anonymous, struct dwarf2_cu *);
1047
1048 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1049
1050 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1051
1052 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1053                                                        struct dwarf2_cu *);
1054
1055 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1056
1057 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1058                                                  gdb_byte *info_ptr,
1059                                                  gdb_byte **new_info_ptr,
1060                                                  struct die_info *parent);
1061
1062 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1063                                                gdb_byte *info_ptr,
1064                                                gdb_byte **new_info_ptr,
1065                                                struct die_info *parent);
1066
1067 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1068                                                gdb_byte *info_ptr,
1069                                                gdb_byte **new_info_ptr,
1070                                                struct die_info *parent);
1071
1072 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1073                                 struct die_info **, gdb_byte *,
1074                                 int *);
1075
1076 static void process_die (struct die_info *, struct dwarf2_cu *);
1077
1078 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1079                                        struct obstack *);
1080
1081 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1082
1083 static struct die_info *dwarf2_extension (struct die_info *die,
1084                                           struct dwarf2_cu **);
1085
1086 static char *dwarf_tag_name (unsigned int);
1087
1088 static char *dwarf_attr_name (unsigned int);
1089
1090 static char *dwarf_form_name (unsigned int);
1091
1092 static char *dwarf_bool_name (unsigned int);
1093
1094 static char *dwarf_type_encoding_name (unsigned int);
1095
1096 #if 0
1097 static char *dwarf_cfi_name (unsigned int);
1098 #endif
1099
1100 static struct die_info *sibling_die (struct die_info *);
1101
1102 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1103
1104 static void dump_die_for_error (struct die_info *);
1105
1106 static void dump_die_1 (struct ui_file *, int level, int max_level,
1107                         struct die_info *);
1108
1109 /*static*/ void dump_die (struct die_info *, int max_level);
1110
1111 static void store_in_ref_table (struct die_info *,
1112                                 struct dwarf2_cu *);
1113
1114 static int is_ref_attr (struct attribute *);
1115
1116 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1117
1118 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1119
1120 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1121                                                struct attribute *,
1122                                                struct dwarf2_cu **);
1123
1124 static struct die_info *follow_die_ref (struct die_info *,
1125                                         struct attribute *,
1126                                         struct dwarf2_cu **);
1127
1128 static struct die_info *follow_die_sig (struct die_info *,
1129                                         struct attribute *,
1130                                         struct dwarf2_cu **);
1131
1132 static void read_signatured_type_at_offset (struct objfile *objfile,
1133                                             unsigned int offset);
1134
1135 static void read_signatured_type (struct objfile *,
1136                                   struct signatured_type *type_sig);
1137
1138 /* memory allocation interface */
1139
1140 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1141
1142 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1143
1144 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1145
1146 static void initialize_cu_func_list (struct dwarf2_cu *);
1147
1148 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1149                                  struct dwarf2_cu *);
1150
1151 static void dwarf_decode_macros (struct line_header *, unsigned int,
1152                                  char *, bfd *, struct dwarf2_cu *);
1153
1154 static int attr_form_is_block (struct attribute *);
1155
1156 static int attr_form_is_section_offset (struct attribute *);
1157
1158 static int attr_form_is_constant (struct attribute *);
1159
1160 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1161                                          struct symbol *sym,
1162                                          struct dwarf2_cu *cu);
1163
1164 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1165                                struct abbrev_info *abbrev,
1166                                struct dwarf2_cu *cu);
1167
1168 static void free_stack_comp_unit (void *);
1169
1170 static hashval_t partial_die_hash (const void *item);
1171
1172 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1173
1174 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1175   (unsigned int offset, struct objfile *objfile);
1176
1177 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1178   (unsigned int offset, struct objfile *objfile);
1179
1180 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1181
1182 static void free_one_comp_unit (void *);
1183
1184 static void free_cached_comp_units (void *);
1185
1186 static void age_cached_comp_units (void);
1187
1188 static void free_one_cached_comp_unit (void *);
1189
1190 static struct type *set_die_type (struct die_info *, struct type *,
1191                                   struct dwarf2_cu *);
1192
1193 static void create_all_comp_units (struct objfile *);
1194
1195 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1196                                  struct objfile *);
1197
1198 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1199
1200 static void dwarf2_add_dependence (struct dwarf2_cu *,
1201                                    struct dwarf2_per_cu_data *);
1202
1203 static void dwarf2_mark (struct dwarf2_cu *);
1204
1205 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1206
1207 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1208
1209 static void dwarf2_release_queue (void *dummy);
1210
1211 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1212                              struct objfile *objfile);
1213
1214 static void process_queue (struct objfile *objfile);
1215
1216 static void find_file_and_directory (struct die_info *die,
1217                                      struct dwarf2_cu *cu,
1218                                      char **name, char **comp_dir);
1219
1220 static char *file_full_name (int file, struct line_header *lh,
1221                              const char *comp_dir);
1222
1223 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1224                                               gdb_byte *info_ptr,
1225                                               gdb_byte *buffer,
1226                                               unsigned int buffer_size,
1227                                               bfd *abfd);
1228
1229 static void init_cu_die_reader (struct die_reader_specs *reader,
1230                                 struct dwarf2_cu *cu);
1231
1232 #if WORDS_BIGENDIAN
1233
1234 /* Convert VALUE between big- and little-endian.  */
1235 static offset_type
1236 byte_swap (offset_type value)
1237 {
1238   offset_type result;
1239
1240   result = (value & 0xff) << 24;
1241   result |= (value & 0xff00) << 8;
1242   result |= (value & 0xff0000) >> 8;
1243   result |= (value & 0xff000000) >> 24;
1244   return result;
1245 }
1246
1247 #define MAYBE_SWAP(V)  byte_swap (V)
1248
1249 #else
1250 #define MAYBE_SWAP(V) (V)
1251 #endif /* WORDS_BIGENDIAN */
1252
1253 /* The suffix for an index file.  */
1254 #define INDEX_SUFFIX ".gdb-index"
1255
1256 /* Try to locate the sections we need for DWARF 2 debugging
1257    information and return true if we have enough to do something.  */
1258
1259 int
1260 dwarf2_has_info (struct objfile *objfile)
1261 {
1262   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1263   if (!dwarf2_per_objfile)
1264     {
1265       /* Initialize per-objfile state.  */
1266       struct dwarf2_per_objfile *data
1267         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1268
1269       memset (data, 0, sizeof (*data));
1270       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1271       dwarf2_per_objfile = data;
1272
1273       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1274       dwarf2_per_objfile->objfile = objfile;
1275     }
1276   return (dwarf2_per_objfile->info.asection != NULL
1277           && dwarf2_per_objfile->abbrev.asection != NULL);
1278 }
1279
1280 /* When loading sections, we can either look for ".<name>", or for
1281  * ".z<name>", which indicates a compressed section.  */
1282
1283 static int
1284 section_is_p (const char *section_name, const char *name)
1285 {
1286   return (section_name[0] == '.'
1287           && (strcmp (section_name + 1, name) == 0
1288               || (section_name[1] == 'z'
1289                   && strcmp (section_name + 2, name) == 0)));
1290 }
1291
1292 /* This function is mapped across the sections and remembers the
1293    offset and size of each of the debugging sections we are interested
1294    in.  */
1295
1296 static void
1297 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1298 {
1299   if (section_is_p (sectp->name, INFO_SECTION))
1300     {
1301       dwarf2_per_objfile->info.asection = sectp;
1302       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1303     }
1304   else if (section_is_p (sectp->name, ABBREV_SECTION))
1305     {
1306       dwarf2_per_objfile->abbrev.asection = sectp;
1307       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1308     }
1309   else if (section_is_p (sectp->name, LINE_SECTION))
1310     {
1311       dwarf2_per_objfile->line.asection = sectp;
1312       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1313     }
1314   else if (section_is_p (sectp->name, LOC_SECTION))
1315     {
1316       dwarf2_per_objfile->loc.asection = sectp;
1317       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1318     }
1319   else if (section_is_p (sectp->name, MACINFO_SECTION))
1320     {
1321       dwarf2_per_objfile->macinfo.asection = sectp;
1322       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1323     }
1324   else if (section_is_p (sectp->name, STR_SECTION))
1325     {
1326       dwarf2_per_objfile->str.asection = sectp;
1327       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1328     }
1329   else if (section_is_p (sectp->name, FRAME_SECTION))
1330     {
1331       dwarf2_per_objfile->frame.asection = sectp;
1332       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1333     }
1334   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1335     {
1336       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1337
1338       if (aflag & SEC_HAS_CONTENTS)
1339         {
1340           dwarf2_per_objfile->eh_frame.asection = sectp;
1341           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1342         }
1343     }
1344   else if (section_is_p (sectp->name, RANGES_SECTION))
1345     {
1346       dwarf2_per_objfile->ranges.asection = sectp;
1347       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1348     }
1349   else if (section_is_p (sectp->name, TYPES_SECTION))
1350     {
1351       dwarf2_per_objfile->types.asection = sectp;
1352       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1353     }
1354   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1355     {
1356       dwarf2_per_objfile->gdb_index.asection = sectp;
1357       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1358     }
1359
1360   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1361       && bfd_section_vma (abfd, sectp) == 0)
1362     dwarf2_per_objfile->has_section_at_zero = 1;
1363 }
1364
1365 /* Decompress a section that was compressed using zlib.  Store the
1366    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1367
1368 static void
1369 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1370                          gdb_byte **outbuf, bfd_size_type *outsize)
1371 {
1372   bfd *abfd = objfile->obfd;
1373 #ifndef HAVE_ZLIB_H
1374   error (_("Support for zlib-compressed DWARF data (from '%s') "
1375            "is disabled in this copy of GDB"),
1376          bfd_get_filename (abfd));
1377 #else
1378   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1379   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1380   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1381   bfd_size_type uncompressed_size;
1382   gdb_byte *uncompressed_buffer;
1383   z_stream strm;
1384   int rc;
1385   int header_size = 12;
1386
1387   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1388       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1389     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1390            bfd_get_filename (abfd));
1391
1392   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1393      by the uncompressed section size, 8 bytes in big-endian order.  */
1394   if (compressed_size < header_size
1395       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1396     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1397            bfd_get_filename (abfd));
1398   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1399   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1400   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1401   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1402   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1403   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1404   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1405   uncompressed_size += compressed_buffer[11];
1406
1407   /* It is possible the section consists of several compressed
1408      buffers concatenated together, so we uncompress in a loop.  */
1409   strm.zalloc = NULL;
1410   strm.zfree = NULL;
1411   strm.opaque = NULL;
1412   strm.avail_in = compressed_size - header_size;
1413   strm.next_in = (Bytef*) compressed_buffer + header_size;
1414   strm.avail_out = uncompressed_size;
1415   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1416                                        uncompressed_size);
1417   rc = inflateInit (&strm);
1418   while (strm.avail_in > 0)
1419     {
1420       if (rc != Z_OK)
1421         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1422                bfd_get_filename (abfd), rc);
1423       strm.next_out = ((Bytef*) uncompressed_buffer
1424                        + (uncompressed_size - strm.avail_out));
1425       rc = inflate (&strm, Z_FINISH);
1426       if (rc != Z_STREAM_END)
1427         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1428                bfd_get_filename (abfd), rc);
1429       rc = inflateReset (&strm);
1430     }
1431   rc = inflateEnd (&strm);
1432   if (rc != Z_OK
1433       || strm.avail_out != 0)
1434     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1435            bfd_get_filename (abfd), rc);
1436
1437   do_cleanups (cleanup);
1438   *outbuf = uncompressed_buffer;
1439   *outsize = uncompressed_size;
1440 #endif
1441 }
1442
1443 /* Read the contents of the section SECTP from object file specified by
1444    OBJFILE, store info about the section into INFO.
1445    If the section is compressed, uncompress it before returning.  */
1446
1447 static void
1448 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1449 {
1450   bfd *abfd = objfile->obfd;
1451   asection *sectp = info->asection;
1452   gdb_byte *buf, *retbuf;
1453   unsigned char header[4];
1454
1455   if (info->readin)
1456     return;
1457   info->buffer = NULL;
1458   info->was_mmapped = 0;
1459   info->readin = 1;
1460
1461   if (info->asection == NULL || info->size == 0)
1462     return;
1463
1464   /* Check if the file has a 4-byte header indicating compression.  */
1465   if (info->size > sizeof (header)
1466       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1467       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1468     {
1469       /* Upon decompression, update the buffer and its size.  */
1470       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1471         {
1472           zlib_decompress_section (objfile, sectp, &info->buffer,
1473                                    &info->size);
1474           return;
1475         }
1476     }
1477
1478 #ifdef HAVE_MMAP
1479   if (pagesize == 0)
1480     pagesize = getpagesize ();
1481
1482   /* Only try to mmap sections which are large enough: we don't want to
1483      waste space due to fragmentation.  Also, only try mmap for sections
1484      without relocations.  */
1485
1486   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1487     {
1488       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1489       size_t map_length = info->size + sectp->filepos - pg_offset;
1490       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1491                                  MAP_PRIVATE, pg_offset);
1492
1493       if (retbuf != MAP_FAILED)
1494         {
1495           info->was_mmapped = 1;
1496           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1497 #if HAVE_POSIX_MADVISE
1498           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1499 #endif
1500           return;
1501         }
1502     }
1503 #endif
1504
1505   /* If we get here, we are a normal, not-compressed section.  */
1506   info->buffer = buf
1507     = obstack_alloc (&objfile->objfile_obstack, info->size);
1508
1509   /* When debugging .o files, we may need to apply relocations; see
1510      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1511      We never compress sections in .o files, so we only need to
1512      try this when the section is not compressed.  */
1513   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1514   if (retbuf != NULL)
1515     {
1516       info->buffer = retbuf;
1517       return;
1518     }
1519
1520   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1521       || bfd_bread (buf, info->size, abfd) != info->size)
1522     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1523            bfd_get_filename (abfd));
1524 }
1525
1526 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1527    SECTION_NAME. */
1528
1529 void
1530 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1531                          asection **sectp, gdb_byte **bufp,
1532                          bfd_size_type *sizep)
1533 {
1534   struct dwarf2_per_objfile *data
1535     = objfile_data (objfile, dwarf2_objfile_data_key);
1536   struct dwarf2_section_info *info;
1537
1538   /* We may see an objfile without any DWARF, in which case we just
1539      return nothing.  */
1540   if (data == NULL)
1541     {
1542       *sectp = NULL;
1543       *bufp = NULL;
1544       *sizep = 0;
1545       return;
1546     }
1547   if (section_is_p (section_name, EH_FRAME_SECTION))
1548     info = &data->eh_frame;
1549   else if (section_is_p (section_name, FRAME_SECTION))
1550     info = &data->frame;
1551   else
1552     gdb_assert (0);
1553
1554   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1555     /* We haven't read this section in yet.  Do it now.  */
1556     dwarf2_read_section (objfile, info);
1557
1558   *sectp = info->asection;
1559   *bufp = info->buffer;
1560   *sizep = info->size;
1561 }
1562
1563 \f
1564
1565 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1566    this CU came.  */
1567 static void
1568 dw2_do_instantiate_symtab (struct objfile *objfile,
1569                            struct dwarf2_per_cu_data *per_cu)
1570 {
1571   struct cleanup *back_to;
1572
1573   back_to = make_cleanup (dwarf2_release_queue, NULL);
1574
1575   queue_comp_unit (per_cu, objfile);
1576
1577   if (per_cu->from_debug_types)
1578     read_signatured_type_at_offset (objfile, per_cu->offset);
1579   else
1580     load_full_comp_unit (per_cu, objfile);
1581
1582   process_queue (objfile);
1583
1584   /* Age the cache, releasing compilation units that have not
1585      been used recently.  */
1586   age_cached_comp_units ();
1587
1588   do_cleanups (back_to);
1589 }
1590
1591 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1592    the objfile from which this CU came.  Returns the resulting symbol
1593    table.  */
1594 static struct symtab *
1595 dw2_instantiate_symtab (struct objfile *objfile,
1596                         struct dwarf2_per_cu_data *per_cu)
1597 {
1598   if (!per_cu->v.quick->symtab)
1599     {
1600       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1601       increment_reading_symtab ();
1602       dw2_do_instantiate_symtab (objfile, per_cu);
1603       do_cleanups (back_to);
1604     }
1605   return per_cu->v.quick->symtab;
1606 }
1607
1608 /* A helper function that knows how to read a 64-bit value in a way
1609    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1610    otherwise.  */
1611 static int
1612 extract_cu_value (const char *bytes, ULONGEST *result)
1613 {
1614   if (sizeof (ULONGEST) < 8)
1615     {
1616       int i;
1617
1618       /* Ignore the upper 4 bytes if they are all zero.  */
1619       for (i = 0; i < 4; ++i)
1620         if (bytes[i + 4] != 0)
1621           return 0;
1622
1623       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1624     }
1625   else
1626     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1627   return 1;
1628 }
1629
1630 /* Read the CU list from the mapped index, and use it to create all
1631    the CU objects for this objfile.  Return 0 if something went wrong,
1632    1 if everything went ok.  */
1633 static int
1634 create_cus_from_index (struct objfile *objfile, struct mapped_index *index,
1635                        const gdb_byte *cu_list, offset_type cu_list_elements)
1636 {
1637   offset_type i;
1638   const char *entry;
1639
1640   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1641   dwarf2_per_objfile->all_comp_units
1642     = obstack_alloc (&objfile->objfile_obstack,
1643                      dwarf2_per_objfile->n_comp_units
1644                      * sizeof (struct dwarf2_per_cu_data *));
1645
1646   for (i = 0; i < cu_list_elements; i += 2)
1647     {
1648       struct dwarf2_per_cu_data *the_cu;
1649       ULONGEST offset, length;
1650
1651       if (!extract_cu_value (cu_list, &offset)
1652           || !extract_cu_value (cu_list + 8, &length))
1653         return 0;
1654       cu_list += 2 * 8;
1655
1656       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1657                                struct dwarf2_per_cu_data);
1658       the_cu->offset = offset;
1659       the_cu->length = length;
1660       the_cu->objfile = objfile;
1661       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1662                                         struct dwarf2_per_cu_quick_data);
1663       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1664     }
1665
1666   return 1;
1667 }
1668
1669 /* Read the address map data from the mapped index, and use it to
1670    populate the objfile's psymtabs_addrmap.  */
1671 static void
1672 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1673 {
1674   const gdb_byte *iter, *end;
1675   struct obstack temp_obstack;
1676   struct addrmap *mutable_map;
1677   struct cleanup *cleanup;
1678   CORE_ADDR baseaddr;
1679
1680   obstack_init (&temp_obstack);
1681   cleanup = make_cleanup_obstack_free (&temp_obstack);
1682   mutable_map = addrmap_create_mutable (&temp_obstack);
1683
1684   iter = index->address_table;
1685   end = iter + index->address_table_size;
1686
1687   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1688
1689   while (iter < end)
1690     {
1691       ULONGEST hi, lo, cu_index;
1692       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1693       iter += 8;
1694       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1695       iter += 8;
1696       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1697       iter += 4;
1698       
1699       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1700                          dwarf2_per_objfile->all_comp_units[cu_index]);
1701     }
1702
1703   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1704                                                     &objfile->objfile_obstack);
1705   do_cleanups (cleanup);
1706 }
1707
1708 /* The hash function for strings in the mapped index.  This is the
1709    same as the hashtab.c hash function, but we keep a separate copy to
1710    maintain control over the implementation.  This is necessary
1711    because the hash function is tied to the format of the mapped index
1712    file.  */
1713 static hashval_t
1714 mapped_index_string_hash (const void *p)
1715 {
1716   const unsigned char *str = (const unsigned char *) p;
1717   hashval_t r = 0;
1718   unsigned char c;
1719
1720   while ((c = *str++) != 0)
1721     r = r * 67 + c - 113;
1722
1723   return r;
1724 }
1725
1726 /* Find a slot in the mapped index INDEX for the object named NAME.
1727    If NAME is found, set *VEC_OUT to point to the CU vector in the
1728    constant pool and return 1.  If NAME cannot be found, return 0.  */
1729 static int
1730 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1731                           offset_type **vec_out)
1732 {
1733   offset_type hash = mapped_index_string_hash (name);
1734   offset_type slot, step;
1735
1736   slot = hash & (index->index_table_slots - 1);
1737   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1738
1739   for (;;)
1740     {
1741       /* Convert a slot number to an offset into the table.  */
1742       offset_type i = 2 * slot;
1743       const char *str;
1744       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1745         return 0;
1746
1747       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1748       if (!strcmp (name, str))
1749         {
1750           *vec_out = (offset_type *) (index->constant_pool
1751                                       + MAYBE_SWAP (index->index_table[i + 1]));
1752           return 1;
1753         }
1754
1755       slot = (slot + step) & (index->index_table_slots - 1);
1756     }
1757 }
1758
1759 /* Read the index file.  If everything went ok, initialize the "quick"
1760    elements of all the CUs and return 1.  Otherwise, return 0.  */
1761 static int
1762 dwarf2_read_index (struct objfile *objfile)
1763 {
1764   struct stat st, obstat;
1765   int fd;
1766   char *addr;
1767   struct mapped_index *map;
1768   offset_type val, *metadata;
1769   char buf1[8], buf2[8];
1770   const gdb_byte *cu_list;
1771   offset_type cu_list_elements;
1772
1773   if (dwarf2_per_objfile->gdb_index.asection == NULL
1774       || dwarf2_per_objfile->gdb_index.size == 0)
1775     return 0;
1776   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1777
1778   addr = dwarf2_per_objfile->gdb_index.buffer;
1779   /* Version check.  */
1780   if (MAYBE_SWAP (*(offset_type *) addr) != 1)
1781     return 0;
1782
1783   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1784   map->total_size = st.st_size;
1785
1786   metadata = (offset_type *) (addr + sizeof (offset_type));
1787   cu_list = addr + MAYBE_SWAP (metadata[0]);
1788   cu_list_elements = ((MAYBE_SWAP (metadata[1]) - MAYBE_SWAP (metadata[0]))
1789                       / 8);
1790   map->address_table = addr + MAYBE_SWAP (metadata[1]);
1791   map->address_table_size = (MAYBE_SWAP (metadata[2])
1792                              - MAYBE_SWAP (metadata[1]));
1793   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[2]));
1794   map->index_table_slots = ((MAYBE_SWAP (metadata[3])
1795                              - MAYBE_SWAP (metadata[2]))
1796                             / (2 * sizeof (offset_type)));
1797   map->constant_pool = addr + MAYBE_SWAP (metadata[3]);
1798
1799   if (!create_cus_from_index (objfile, map, cu_list, cu_list_elements))
1800     return 0;
1801
1802   create_addrmap_from_index (objfile, map);
1803
1804   dwarf2_per_objfile->index_table = map;
1805   dwarf2_per_objfile->using_index = 1;
1806
1807   return 1;
1808 }
1809
1810 /* A helper for the "quick" functions which sets the global
1811    dwarf2_per_objfile according to OBJFILE.  */
1812 static void
1813 dw2_setup (struct objfile *objfile)
1814 {
1815   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1816   gdb_assert (dwarf2_per_objfile);
1817 }
1818
1819 /* A helper for the "quick" functions which attempts to read the line
1820    table for THIS_CU.  */
1821 static void
1822 dw2_require_line_header (struct objfile *objfile,
1823                          struct dwarf2_per_cu_data *this_cu)
1824 {
1825   bfd *abfd = objfile->obfd;
1826   struct line_header *lh = NULL;
1827   struct attribute *attr;
1828   struct cleanup *cleanups;
1829   struct die_info *comp_unit_die;
1830   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1831   int has_children, i;
1832   struct dwarf2_cu cu;
1833   unsigned int bytes_read, buffer_size;
1834   struct die_reader_specs reader_specs;
1835   char *name, *comp_dir;
1836
1837   if (this_cu->v.quick->read_lines)
1838     return;
1839   this_cu->v.quick->read_lines = 1;
1840
1841   memset (&cu, 0, sizeof (cu));
1842   cu.objfile = objfile;
1843   obstack_init (&cu.comp_unit_obstack);
1844
1845   cleanups = make_cleanup (free_stack_comp_unit, &cu);
1846
1847   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1848   buffer_size = dwarf2_per_objfile->info.size;
1849   buffer = dwarf2_per_objfile->info.buffer;
1850   info_ptr = buffer + this_cu->offset;
1851   beg_of_comp_unit = info_ptr;
1852
1853   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1854                                           buffer, buffer_size,
1855                                           abfd);
1856
1857   /* Complete the cu_header.  */
1858   cu.header.offset = beg_of_comp_unit - buffer;
1859   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1860
1861   this_cu->cu = &cu;
1862   cu.per_cu = this_cu;
1863
1864   dwarf2_read_abbrevs (abfd, &cu);
1865   make_cleanup (dwarf2_free_abbrev_table, &cu);
1866
1867   if (this_cu->from_debug_types)
1868     info_ptr += 8 /*signature*/ + cu.header.offset_size;
1869   init_cu_die_reader (&reader_specs, &cu);
1870   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1871                             &has_children);
1872
1873   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1874   if (attr)
1875     {
1876       unsigned int line_offset = DW_UNSND (attr);
1877       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1878     }
1879   if (lh == NULL)
1880     {
1881       do_cleanups (cleanups);
1882       return;
1883     }
1884
1885   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1886
1887   this_cu->v.quick->lines = lh;
1888
1889   this_cu->v.quick->file_names
1890     = obstack_alloc (&objfile->objfile_obstack,
1891                      lh->num_file_names * sizeof (char *));
1892   for (i = 0; i < lh->num_file_names; ++i)
1893     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
1894
1895   do_cleanups (cleanups);
1896 }
1897
1898 /* A helper for the "quick" functions which computes and caches the
1899    real path for a given file name from the line table.
1900    dw2_require_line_header must have been called before this is
1901    invoked.  */
1902 static const char *
1903 dw2_require_full_path (struct objfile *objfile,
1904                        struct dwarf2_per_cu_data *cu,
1905                        int index)
1906 {
1907   if (!cu->v.quick->full_names)
1908     cu->v.quick->full_names
1909       = OBSTACK_CALLOC (&objfile->objfile_obstack,
1910                         cu->v.quick->lines->num_file_names,
1911                         sizeof (char *));
1912
1913   if (!cu->v.quick->full_names[index])
1914     cu->v.quick->full_names[index]
1915       = gdb_realpath (cu->v.quick->file_names[index]);
1916
1917   return cu->v.quick->full_names[index];
1918 }
1919
1920 static struct symtab *
1921 dw2_find_last_source_symtab (struct objfile *objfile)
1922 {
1923   int index;
1924   dw2_setup (objfile);
1925   index = dwarf2_per_objfile->n_comp_units - 1;
1926   return dw2_instantiate_symtab (objfile,
1927                                  dwarf2_per_objfile->all_comp_units[index]);
1928 }
1929
1930 static void
1931 dw2_forget_cached_source_info (struct objfile *objfile)
1932 {
1933   int i;
1934
1935   dw2_setup (objfile);
1936   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1937     {
1938       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
1939
1940       if (cu->v.quick->full_names)
1941         {
1942           int j;
1943
1944           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
1945             xfree ((void *) cu->v.quick->full_names[j]);
1946         }
1947     }
1948 }
1949
1950 static int
1951 dw2_lookup_symtab (struct objfile *objfile, const char *name,
1952                    const char *full_path, const char *real_path,
1953                    struct symtab **result)
1954 {
1955   int i;
1956   int check_basename = lbasename (name) == name;
1957   struct dwarf2_per_cu_data *base_cu = NULL;
1958
1959   dw2_setup (objfile);
1960   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
1961     {
1962       int j;
1963       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
1964
1965       if (cu->v.quick->symtab)
1966         continue;
1967
1968       dw2_require_line_header (objfile, cu);
1969       if (!cu->v.quick->lines)
1970         continue;
1971
1972       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
1973         {
1974           const char *this_name = cu->v.quick->file_names[j];
1975
1976           if (FILENAME_CMP (name, this_name) == 0)
1977             {
1978               *result = dw2_instantiate_symtab (objfile, cu);
1979               return 1;
1980             }
1981
1982           if (check_basename && ! base_cu
1983               && FILENAME_CMP (lbasename (this_name), name) == 0)
1984             base_cu = cu;
1985
1986           if (full_path != NULL)
1987             {
1988               const char *this_full_name = dw2_require_full_path (objfile,
1989                                                                   cu, j);
1990
1991               if (this_full_name
1992                   && FILENAME_CMP (full_path, this_full_name) == 0)
1993                 {
1994                   *result = dw2_instantiate_symtab (objfile, cu);
1995                   return 1;
1996                 }
1997             }
1998
1999           if (real_path != NULL)
2000             {
2001               const char *this_full_name = dw2_require_full_path (objfile,
2002                                                                   cu, j);
2003
2004               if (this_full_name != NULL)
2005                 {
2006                   char *rp = gdb_realpath (this_full_name);
2007                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2008                     {
2009                       xfree (rp);
2010                       *result = dw2_instantiate_symtab (objfile, cu);
2011                       return 1;
2012                     }
2013                   xfree (rp);
2014                 }
2015             }
2016         }
2017     }
2018
2019   if (base_cu)
2020     {
2021       *result = dw2_instantiate_symtab (objfile, base_cu);
2022       return 1;
2023     }
2024
2025   return 0;
2026 }
2027
2028 static struct symtab *
2029 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2030                    const char *name, domain_enum domain)
2031 {
2032   /* We do all the work in the pre_expand_symtabs_matching hook
2033      instead.  */
2034   return NULL;
2035 }
2036
2037 /* A helper function that expands all symtabs that hold an object
2038    named NAME.  */
2039 static void
2040 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2041 {
2042   dw2_setup (objfile);
2043
2044   if (dwarf2_per_objfile->index_table)
2045     {
2046       offset_type *vec;
2047
2048       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2049                                     name, &vec))
2050         {
2051           offset_type i, len = MAYBE_SWAP (*vec);
2052           for (i = 0; i < len; ++i)
2053             {
2054               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2055               struct dwarf2_per_cu_data *cu;
2056               cu = dwarf2_per_objfile->all_comp_units[cu_index];
2057               dw2_instantiate_symtab (objfile, cu);
2058             }
2059         }
2060     }
2061 }
2062
2063 static void
2064 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2065                                  int kind, const char *name,
2066                                  domain_enum domain)
2067 {
2068   dw2_do_expand_symtabs_matching (objfile, name);
2069 }
2070
2071 static void
2072 dw2_print_stats (struct objfile *objfile)
2073 {
2074   int i, count;
2075
2076   dw2_setup (objfile);
2077   count = 0;
2078   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2079     {
2080       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2081
2082       if (!cu->v.quick->symtab)
2083         ++count;
2084     }
2085   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2086 }
2087
2088 static void
2089 dw2_dump (struct objfile *objfile)
2090 {
2091   /* Nothing worth printing.  */
2092 }
2093
2094 static void
2095 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2096               struct section_offsets *delta)
2097 {
2098   /* There's nothing to relocate here.  */
2099 }
2100
2101 static void
2102 dw2_expand_symtabs_for_function (struct objfile *objfile,
2103                                  const char *func_name)
2104 {
2105   dw2_do_expand_symtabs_matching (objfile, func_name);
2106 }
2107
2108 static void
2109 dw2_expand_all_symtabs (struct objfile *objfile)
2110 {
2111   int i;
2112
2113   dw2_setup (objfile);
2114   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2115     {
2116       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2117
2118       dw2_instantiate_symtab (objfile, cu);
2119     }
2120 }
2121
2122 static void
2123 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2124                                   const char *filename)
2125 {
2126   int i;
2127
2128   dw2_setup (objfile);
2129   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2130     {
2131       int j;
2132       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2133
2134       if (cu->v.quick->symtab)
2135         continue;
2136
2137       dw2_require_line_header (objfile, cu);
2138       if (!cu->v.quick->lines)
2139         continue;
2140
2141       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2142         {
2143           const char *this_name = cu->v.quick->file_names[j];
2144           if (strcmp (this_name, filename) == 0)
2145             {
2146               dw2_instantiate_symtab (objfile, cu);
2147               break;
2148             }
2149         }
2150     }
2151 }
2152
2153 static const char *
2154 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2155 {
2156   struct dwarf2_per_cu_data *cu;
2157   offset_type *vec;
2158
2159   dw2_setup (objfile);
2160
2161   if (!dwarf2_per_objfile->index_table)
2162     return NULL;
2163
2164   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2165                                  name, &vec))
2166     return NULL;
2167
2168   /* Note that this just looks at the very first one named NAME -- but
2169      actually we are looking for a function.  find_main_filename
2170      should be rewritten so that it doesn't require a custom hook.  It
2171      could just use the ordinary symbol tables.  */
2172   /* vec[0] is the length, which must always be >0.  */
2173   cu = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[1])];
2174
2175   dw2_require_line_header (objfile, cu);
2176   if (!cu->v.quick->lines)
2177     return NULL;
2178
2179   return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2180 }
2181
2182 static void
2183 dw2_map_ada_symtabs (struct objfile *objfile,
2184                      int (*wild_match) (const char *, int, const char *),
2185                      int (*is_name_suffix) (const char *),
2186                      void (*callback) (struct objfile *,
2187                                        struct symtab *, void *),
2188                      const char *name, int global,
2189                      domain_enum namespace, int wild,
2190                      void *data)
2191 {
2192   /* For now, we don't support Ada, so this function can't be
2193      reached.  */
2194   internal_error (__FILE__, __LINE__,
2195                   _("map_ada_symtabs called via index method"));
2196 }
2197
2198 static void
2199 dw2_expand_symtabs_matching (struct objfile *objfile,
2200                              int (*file_matcher) (const char *, void *),
2201                              int (*name_matcher) (const char *, void *),
2202                              domain_enum kind,
2203                              void *data)
2204 {
2205   int i;
2206   offset_type iter;
2207
2208   dw2_setup (objfile);
2209   if (!dwarf2_per_objfile->index_table)
2210     return;
2211
2212   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2213     {
2214       int j;
2215       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2216
2217       cu->v.quick->mark = 0;
2218       if (cu->v.quick->symtab)
2219         continue;
2220
2221       dw2_require_line_header (objfile, cu);
2222       if (!cu->v.quick->lines)
2223         continue;
2224
2225       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2226         {
2227           if (file_matcher (cu->v.quick->file_names[j], data))
2228             {
2229               cu->v.quick->mark = 1;
2230               break;
2231             }
2232         }
2233     }
2234
2235   for (iter = 0;
2236        iter < dwarf2_per_objfile->index_table->index_table_slots;
2237        ++iter)
2238     {
2239       offset_type idx = 2 * iter;
2240       const char *name;
2241       offset_type *vec, vec_len, vec_idx;
2242
2243       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2244           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2245         continue;
2246
2247       name = (dwarf2_per_objfile->index_table->constant_pool
2248               + dwarf2_per_objfile->index_table->index_table[idx]);
2249
2250       if (! (*name_matcher) (name, data))
2251         continue;
2252
2253       /* The name was matched, now expand corresponding CUs that were
2254          marked.  */
2255       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2256                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2257       vec_len = MAYBE_SWAP (vec[0]);
2258       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2259         {
2260           struct dwarf2_per_cu_data *cu
2261             = dwarf2_per_objfile->all_comp_units[MAYBE_SWAP (vec[vec_idx + 1])];
2262           if (cu->v.quick->mark)
2263             dw2_instantiate_symtab (objfile, cu);
2264         }
2265     }
2266 }
2267
2268 static struct symtab *
2269 dw2_find_pc_sect_symtab (struct objfile *objfile,
2270                          struct minimal_symbol *msymbol,
2271                          CORE_ADDR pc,
2272                          struct obj_section *section,
2273                          int warn_if_readin)
2274 {
2275   struct dwarf2_per_cu_data *data;
2276
2277   dw2_setup (objfile);
2278
2279   if (!objfile->psymtabs_addrmap)
2280     return NULL;
2281
2282   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2283   if (!data)
2284     return NULL;
2285
2286   if (warn_if_readin && data->v.quick->symtab)
2287     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)\n"),
2288              paddress (get_objfile_arch (objfile), pc));
2289
2290   return dw2_instantiate_symtab (objfile, data);
2291 }
2292
2293 static void
2294 dw2_map_symbol_names (struct objfile *objfile,
2295                       void (*fun) (const char *, void *),
2296                       void *data)
2297 {
2298   offset_type iter;
2299   dw2_setup (objfile);
2300
2301   if (!dwarf2_per_objfile->index_table)
2302     return;
2303
2304   for (iter = 0;
2305        iter < dwarf2_per_objfile->index_table->index_table_slots;
2306        ++iter)
2307     {
2308       offset_type idx = 2 * iter;
2309       const char *name;
2310       offset_type *vec, vec_len, vec_idx;
2311
2312       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2313           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2314         continue;
2315
2316       name = (dwarf2_per_objfile->index_table->constant_pool
2317               + dwarf2_per_objfile->index_table->index_table[idx]);
2318
2319       (*fun) (name, data);
2320     }
2321 }
2322
2323 static void
2324 dw2_map_symbol_filenames (struct objfile *objfile,
2325                           void (*fun) (const char *, const char *, void *),
2326                           void *data)
2327 {
2328   int i;
2329
2330   dw2_setup (objfile);
2331   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2332     {
2333       int j;
2334       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2335
2336       if (cu->v.quick->symtab)
2337         continue;
2338
2339       dw2_require_line_header (objfile, cu);
2340       if (!cu->v.quick->lines)
2341         continue;
2342
2343       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2344         {
2345           const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2346           (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2347         }
2348     }
2349 }
2350
2351 static int
2352 dw2_has_symbols (struct objfile *objfile)
2353 {
2354   return 1;
2355 }
2356
2357 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2358 {
2359   dw2_has_symbols,
2360   dw2_find_last_source_symtab,
2361   dw2_forget_cached_source_info,
2362   dw2_lookup_symtab,
2363   dw2_lookup_symbol,
2364   dw2_pre_expand_symtabs_matching,
2365   dw2_print_stats,
2366   dw2_dump,
2367   dw2_relocate,
2368   dw2_expand_symtabs_for_function,
2369   dw2_expand_all_symtabs,
2370   dw2_expand_symtabs_with_filename,
2371   dw2_find_symbol_file,
2372   dw2_map_ada_symtabs,
2373   dw2_expand_symtabs_matching,
2374   dw2_find_pc_sect_symtab,
2375   dw2_map_symbol_names,
2376   dw2_map_symbol_filenames
2377 };
2378
2379 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2380    file will use psymtabs, or 1 if using the GNU index.  */
2381
2382 int
2383 dwarf2_initialize_objfile (struct objfile *objfile)
2384 {
2385   /* If we're about to read full symbols, don't bother with the
2386      indices.  In this case we also don't care if some other debug
2387      format is making psymtabs, because they are all about to be
2388      expanded anyway.  */
2389   if ((objfile->flags & OBJF_READNOW))
2390     {
2391       int i;
2392
2393       dwarf2_per_objfile->using_index = 1;
2394       create_all_comp_units (objfile);
2395
2396       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2397         {
2398           struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
2399
2400           cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2401                                         struct dwarf2_per_cu_quick_data);
2402         }
2403
2404       /* Return 1 so that gdb sees the "quick" functions.  However,
2405          these functions will be no-ops because we will have expanded
2406          all symtabs.  */
2407       return 1;
2408     }
2409
2410   if (dwarf2_read_index (objfile))
2411     return 1;
2412
2413   dwarf2_build_psymtabs (objfile);
2414   return 0;
2415 }
2416
2417 \f
2418
2419 /* Build a partial symbol table.  */
2420
2421 void
2422 dwarf2_build_psymtabs (struct objfile *objfile)
2423 {
2424   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2425     {
2426       init_psymbol_list (objfile, 1024);
2427     }
2428
2429   dwarf2_build_psymtabs_hard (objfile);
2430 }
2431
2432 /* Return TRUE if OFFSET is within CU_HEADER.  */
2433
2434 static inline int
2435 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2436 {
2437   unsigned int bottom = cu_header->offset;
2438   unsigned int top = (cu_header->offset
2439                       + cu_header->length
2440                       + cu_header->initial_length_size);
2441
2442   return (offset >= bottom && offset < top);
2443 }
2444
2445 /* Read in the comp unit header information from the debug_info at info_ptr.
2446    NOTE: This leaves members offset, first_die_offset to be filled in
2447    by the caller.  */
2448
2449 static gdb_byte *
2450 read_comp_unit_head (struct comp_unit_head *cu_header,
2451                      gdb_byte *info_ptr, bfd *abfd)
2452 {
2453   int signed_addr;
2454   unsigned int bytes_read;
2455
2456   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2457   cu_header->initial_length_size = bytes_read;
2458   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2459   info_ptr += bytes_read;
2460   cu_header->version = read_2_bytes (abfd, info_ptr);
2461   info_ptr += 2;
2462   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2463                                           &bytes_read);
2464   info_ptr += bytes_read;
2465   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2466   info_ptr += 1;
2467   signed_addr = bfd_get_sign_extend_vma (abfd);
2468   if (signed_addr < 0)
2469     internal_error (__FILE__, __LINE__,
2470                     _("read_comp_unit_head: dwarf from non elf file"));
2471   cu_header->signed_addr_p = signed_addr;
2472
2473   return info_ptr;
2474 }
2475
2476 static gdb_byte *
2477 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2478                              gdb_byte *buffer, unsigned int buffer_size,
2479                              bfd *abfd)
2480 {
2481   gdb_byte *beg_of_comp_unit = info_ptr;
2482
2483   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2484
2485   if (header->version != 2 && header->version != 3 && header->version != 4)
2486     error (_("Dwarf Error: wrong version in compilation unit header "
2487            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2488            bfd_get_filename (abfd));
2489
2490   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2491     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2492            "(offset 0x%lx + 6) [in module %s]"),
2493            (long) header->abbrev_offset,
2494            (long) (beg_of_comp_unit - buffer),
2495            bfd_get_filename (abfd));
2496
2497   if (beg_of_comp_unit + header->length + header->initial_length_size
2498       > buffer + buffer_size)
2499     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2500            "(offset 0x%lx + 0) [in module %s]"),
2501            (long) header->length,
2502            (long) (beg_of_comp_unit - buffer),
2503            bfd_get_filename (abfd));
2504
2505   return info_ptr;
2506 }
2507
2508 /* Read in the types comp unit header information from .debug_types entry at
2509    types_ptr.  The result is a pointer to one past the end of the header.  */
2510
2511 static gdb_byte *
2512 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2513                           ULONGEST *signature,
2514                           gdb_byte *types_ptr, bfd *abfd)
2515 {
2516   gdb_byte *initial_types_ptr = types_ptr;
2517
2518   dwarf2_read_section (dwarf2_per_objfile->objfile,
2519                        &dwarf2_per_objfile->types);
2520   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2521
2522   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2523
2524   *signature = read_8_bytes (abfd, types_ptr);
2525   types_ptr += 8;
2526   types_ptr += cu_header->offset_size;
2527   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2528
2529   return types_ptr;
2530 }
2531
2532 /* Allocate a new partial symtab for file named NAME and mark this new
2533    partial symtab as being an include of PST.  */
2534
2535 static void
2536 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2537                                struct objfile *objfile)
2538 {
2539   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2540
2541   subpst->section_offsets = pst->section_offsets;
2542   subpst->textlow = 0;
2543   subpst->texthigh = 0;
2544
2545   subpst->dependencies = (struct partial_symtab **)
2546     obstack_alloc (&objfile->objfile_obstack,
2547                    sizeof (struct partial_symtab *));
2548   subpst->dependencies[0] = pst;
2549   subpst->number_of_dependencies = 1;
2550
2551   subpst->globals_offset = 0;
2552   subpst->n_global_syms = 0;
2553   subpst->statics_offset = 0;
2554   subpst->n_static_syms = 0;
2555   subpst->symtab = NULL;
2556   subpst->read_symtab = pst->read_symtab;
2557   subpst->readin = 0;
2558
2559   /* No private part is necessary for include psymtabs.  This property
2560      can be used to differentiate between such include psymtabs and
2561      the regular ones.  */
2562   subpst->read_symtab_private = NULL;
2563 }
2564
2565 /* Read the Line Number Program data and extract the list of files
2566    included by the source file represented by PST.  Build an include
2567    partial symtab for each of these included files.  */
2568
2569 static void
2570 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2571                                struct die_info *die,
2572                                struct partial_symtab *pst)
2573 {
2574   struct objfile *objfile = cu->objfile;
2575   bfd *abfd = objfile->obfd;
2576   struct line_header *lh = NULL;
2577   struct attribute *attr;
2578
2579   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2580   if (attr)
2581     {
2582       unsigned int line_offset = DW_UNSND (attr);
2583
2584       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2585     }
2586   if (lh == NULL)
2587     return;  /* No linetable, so no includes.  */
2588
2589   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2590
2591   free_line_header (lh);
2592 }
2593
2594 static hashval_t
2595 hash_type_signature (const void *item)
2596 {
2597   const struct signatured_type *type_sig = item;
2598
2599   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2600   return type_sig->signature;
2601 }
2602
2603 static int
2604 eq_type_signature (const void *item_lhs, const void *item_rhs)
2605 {
2606   const struct signatured_type *lhs = item_lhs;
2607   const struct signatured_type *rhs = item_rhs;
2608
2609   return lhs->signature == rhs->signature;
2610 }
2611
2612 /* Create the hash table of all entries in the .debug_types section.
2613    The result is zero if there is an error (e.g. missing .debug_types section),
2614    otherwise non-zero.  */
2615
2616 static int
2617 create_debug_types_hash_table (struct objfile *objfile)
2618 {
2619   gdb_byte *info_ptr;
2620   htab_t types_htab;
2621
2622   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2623   info_ptr = dwarf2_per_objfile->types.buffer;
2624
2625   if (info_ptr == NULL)
2626     {
2627       dwarf2_per_objfile->signatured_types = NULL;
2628       return 0;
2629     }
2630
2631   types_htab = htab_create_alloc_ex (41,
2632                                      hash_type_signature,
2633                                      eq_type_signature,
2634                                      NULL,
2635                                      &objfile->objfile_obstack,
2636                                      hashtab_obstack_allocate,
2637                                      dummy_obstack_deallocate);
2638
2639   if (dwarf2_die_debug)
2640     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2641
2642   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2643     {
2644       unsigned int offset;
2645       unsigned int offset_size;
2646       unsigned int type_offset;
2647       unsigned int length, initial_length_size;
2648       unsigned short version;
2649       ULONGEST signature;
2650       struct signatured_type *type_sig;
2651       void **slot;
2652       gdb_byte *ptr = info_ptr;
2653
2654       offset = ptr - dwarf2_per_objfile->types.buffer;
2655
2656       /* We need to read the type's signature in order to build the hash
2657          table, but we don't need to read anything else just yet.  */
2658
2659       /* Sanity check to ensure entire cu is present.  */
2660       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2661       if (ptr + length + initial_length_size
2662           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2663         {
2664           complaint (&symfile_complaints,
2665                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2666           break;
2667         }
2668
2669       offset_size = initial_length_size == 4 ? 4 : 8;
2670       ptr += initial_length_size;
2671       version = bfd_get_16 (objfile->obfd, ptr);
2672       ptr += 2;
2673       ptr += offset_size; /* abbrev offset */
2674       ptr += 1; /* address size */
2675       signature = bfd_get_64 (objfile->obfd, ptr);
2676       ptr += 8;
2677       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2678
2679       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2680       memset (type_sig, 0, sizeof (*type_sig));
2681       type_sig->signature = signature;
2682       type_sig->offset = offset;
2683       type_sig->type_offset = type_offset;
2684
2685       slot = htab_find_slot (types_htab, type_sig, INSERT);
2686       gdb_assert (slot != NULL);
2687       *slot = type_sig;
2688
2689       if (dwarf2_die_debug)
2690         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2691                             offset, phex (signature, sizeof (signature)));
2692
2693       info_ptr = info_ptr + initial_length_size + length;
2694     }
2695
2696   dwarf2_per_objfile->signatured_types = types_htab;
2697
2698   return 1;
2699 }
2700
2701 /* Lookup a signature based type.
2702    Returns NULL if SIG is not present in the table.  */
2703
2704 static struct signatured_type *
2705 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2706 {
2707   struct signatured_type find_entry, *entry;
2708
2709   if (dwarf2_per_objfile->signatured_types == NULL)
2710     {
2711       complaint (&symfile_complaints,
2712                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2713       return 0;
2714     }
2715
2716   find_entry.signature = sig;
2717   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2718   return entry;
2719 }
2720
2721 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2722
2723 static void
2724 init_cu_die_reader (struct die_reader_specs *reader,
2725                     struct dwarf2_cu *cu)
2726 {
2727   reader->abfd = cu->objfile->obfd;
2728   reader->cu = cu;
2729   if (cu->per_cu->from_debug_types)
2730     {
2731       gdb_assert (dwarf2_per_objfile->types.readin);
2732       reader->buffer = dwarf2_per_objfile->types.buffer;
2733     }
2734   else
2735     {
2736       gdb_assert (dwarf2_per_objfile->info.readin);
2737       reader->buffer = dwarf2_per_objfile->info.buffer;
2738     }
2739 }
2740
2741 /* Find the base address of the compilation unit for range lists and
2742    location lists.  It will normally be specified by DW_AT_low_pc.
2743    In DWARF-3 draft 4, the base address could be overridden by
2744    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2745    compilation units with discontinuous ranges.  */
2746
2747 static void
2748 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2749 {
2750   struct attribute *attr;
2751
2752   cu->base_known = 0;
2753   cu->base_address = 0;
2754
2755   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2756   if (attr)
2757     {
2758       cu->base_address = DW_ADDR (attr);
2759       cu->base_known = 1;
2760     }
2761   else
2762     {
2763       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2764       if (attr)
2765         {
2766           cu->base_address = DW_ADDR (attr);
2767           cu->base_known = 1;
2768         }
2769     }
2770 }
2771
2772 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2773    to combine the common parts.
2774    Process a compilation unit for a psymtab.
2775    BUFFER is a pointer to the beginning of the dwarf section buffer,
2776    either .debug_info or debug_types.
2777    INFO_PTR is a pointer to the start of the CU.
2778    Returns a pointer to the next CU.  */
2779
2780 static gdb_byte *
2781 process_psymtab_comp_unit (struct objfile *objfile,
2782                            struct dwarf2_per_cu_data *this_cu,
2783                            gdb_byte *buffer, gdb_byte *info_ptr,
2784                            unsigned int buffer_size)
2785 {
2786   bfd *abfd = objfile->obfd;
2787   gdb_byte *beg_of_comp_unit = info_ptr;
2788   struct die_info *comp_unit_die;
2789   struct partial_symtab *pst;
2790   CORE_ADDR baseaddr;
2791   struct cleanup *back_to_inner;
2792   struct dwarf2_cu cu;
2793   int has_children, has_pc_info;
2794   struct attribute *attr;
2795   CORE_ADDR best_lowpc = 0, best_highpc = 0;
2796   struct die_reader_specs reader_specs;
2797
2798   memset (&cu, 0, sizeof (cu));
2799   cu.objfile = objfile;
2800   obstack_init (&cu.comp_unit_obstack);
2801
2802   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
2803
2804   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2805                                           buffer, buffer_size,
2806                                           abfd);
2807
2808   /* Complete the cu_header.  */
2809   cu.header.offset = beg_of_comp_unit - buffer;
2810   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2811
2812   cu.list_in_scope = &file_symbols;
2813
2814   /* If this compilation unit was already read in, free the
2815      cached copy in order to read it in again.  This is
2816      necessary because we skipped some symbols when we first
2817      read in the compilation unit (see load_partial_dies).
2818      This problem could be avoided, but the benefit is
2819      unclear.  */
2820   if (this_cu->cu != NULL)
2821     free_one_cached_comp_unit (this_cu->cu);
2822
2823   /* Note that this is a pointer to our stack frame, being
2824      added to a global data structure.  It will be cleaned up
2825      in free_stack_comp_unit when we finish with this
2826      compilation unit.  */
2827   this_cu->cu = &cu;
2828   cu.per_cu = this_cu;
2829
2830   /* Read the abbrevs for this compilation unit into a table.  */
2831   dwarf2_read_abbrevs (abfd, &cu);
2832   make_cleanup (dwarf2_free_abbrev_table, &cu);
2833
2834   /* Read the compilation unit die.  */
2835   if (this_cu->from_debug_types)
2836     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2837   init_cu_die_reader (&reader_specs, &cu);
2838   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2839                             &has_children);
2840
2841   if (this_cu->from_debug_types)
2842     {
2843       /* offset,length haven't been set yet for type units.  */
2844       this_cu->offset = cu.header.offset;
2845       this_cu->length = cu.header.length + cu.header.initial_length_size;
2846     }
2847   else if (comp_unit_die->tag == DW_TAG_partial_unit)
2848     {
2849       info_ptr = (beg_of_comp_unit + cu.header.length
2850                   + cu.header.initial_length_size);
2851       do_cleanups (back_to_inner);
2852       return info_ptr;
2853     }
2854
2855   /* Set the language we're debugging.  */
2856   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
2857   if (attr)
2858     set_cu_language (DW_UNSND (attr), &cu);
2859   else
2860     set_cu_language (language_minimal, &cu);
2861
2862   /* Allocate a new partial symbol table structure.  */
2863   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
2864   pst = start_psymtab_common (objfile, objfile->section_offsets,
2865                               (attr != NULL) ? DW_STRING (attr) : "",
2866                               /* TEXTLOW and TEXTHIGH are set below.  */
2867                               0,
2868                               objfile->global_psymbols.next,
2869                               objfile->static_psymbols.next);
2870
2871   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
2872   if (attr != NULL)
2873     pst->dirname = DW_STRING (attr);
2874
2875   pst->read_symtab_private = this_cu;
2876
2877   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2878
2879   /* Store the function that reads in the rest of the symbol table */
2880   pst->read_symtab = dwarf2_psymtab_to_symtab;
2881
2882   this_cu->v.psymtab = pst;
2883
2884   dwarf2_find_base_address (comp_unit_die, &cu);
2885
2886   /* Possibly set the default values of LOWPC and HIGHPC from
2887      `DW_AT_ranges'.  */
2888   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
2889                                       &best_highpc, &cu, pst);
2890   if (has_pc_info == 1 && best_lowpc < best_highpc)
2891     /* Store the contiguous range if it is not empty; it can be empty for
2892        CUs with no code.  */
2893     addrmap_set_empty (objfile->psymtabs_addrmap,
2894                        best_lowpc + baseaddr,
2895                        best_highpc + baseaddr - 1, pst);
2896
2897   /* Check if comp unit has_children.
2898      If so, read the rest of the partial symbols from this comp unit.
2899      If not, there's no more debug_info for this comp unit. */
2900   if (has_children)
2901     {
2902       struct partial_die_info *first_die;
2903       CORE_ADDR lowpc, highpc;
2904
2905       lowpc = ((CORE_ADDR) -1);
2906       highpc = ((CORE_ADDR) 0);
2907
2908       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
2909
2910       scan_partial_symbols (first_die, &lowpc, &highpc,
2911                             ! has_pc_info, &cu);
2912
2913       /* If we didn't find a lowpc, set it to highpc to avoid
2914          complaints from `maint check'.  */
2915       if (lowpc == ((CORE_ADDR) -1))
2916         lowpc = highpc;
2917
2918       /* If the compilation unit didn't have an explicit address range,
2919          then use the information extracted from its child dies.  */
2920       if (! has_pc_info)
2921         {
2922           best_lowpc = lowpc;
2923           best_highpc = highpc;
2924         }
2925     }
2926   pst->textlow = best_lowpc + baseaddr;
2927   pst->texthigh = best_highpc + baseaddr;
2928
2929   pst->n_global_syms = objfile->global_psymbols.next -
2930     (objfile->global_psymbols.list + pst->globals_offset);
2931   pst->n_static_syms = objfile->static_psymbols.next -
2932     (objfile->static_psymbols.list + pst->statics_offset);
2933   sort_pst_symbols (pst);
2934
2935   info_ptr = (beg_of_comp_unit + cu.header.length
2936               + cu.header.initial_length_size);
2937
2938   if (this_cu->from_debug_types)
2939     {
2940       /* It's not clear we want to do anything with stmt lists here.
2941          Waiting to see what gcc ultimately does.  */
2942     }
2943   else
2944     {
2945       /* Get the list of files included in the current compilation unit,
2946          and build a psymtab for each of them.  */
2947       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
2948     }
2949
2950   do_cleanups (back_to_inner);
2951
2952   return info_ptr;
2953 }
2954
2955 /* Traversal function for htab_traverse_noresize.
2956    Process one .debug_types comp-unit.  */
2957
2958 static int
2959 process_type_comp_unit (void **slot, void *info)
2960 {
2961   struct signatured_type *entry = (struct signatured_type *) *slot;
2962   struct objfile *objfile = (struct objfile *) info;
2963   struct dwarf2_per_cu_data *this_cu;
2964
2965   this_cu = &entry->per_cu;
2966   this_cu->from_debug_types = 1;
2967
2968   gdb_assert (dwarf2_per_objfile->types.readin);
2969   process_psymtab_comp_unit (objfile, this_cu,
2970                              dwarf2_per_objfile->types.buffer,
2971                              dwarf2_per_objfile->types.buffer + entry->offset,
2972                              dwarf2_per_objfile->types.size);
2973
2974   return 1;
2975 }
2976
2977 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
2978    Build partial symbol tables for the .debug_types comp-units.  */
2979
2980 static void
2981 build_type_psymtabs (struct objfile *objfile)
2982 {
2983   if (! create_debug_types_hash_table (objfile))
2984     return;
2985
2986   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
2987                           process_type_comp_unit, objfile);
2988 }
2989
2990 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
2991
2992 static void
2993 psymtabs_addrmap_cleanup (void *o)
2994 {
2995   struct objfile *objfile = o;
2996
2997   objfile->psymtabs_addrmap = NULL;
2998 }
2999
3000 /* Build the partial symbol table by doing a quick pass through the
3001    .debug_info and .debug_abbrev sections.  */
3002
3003 static void
3004 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3005 {
3006   gdb_byte *info_ptr;
3007   struct cleanup *back_to, *addrmap_cleanup;
3008   struct obstack temp_obstack;
3009
3010   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3011   info_ptr = dwarf2_per_objfile->info.buffer;
3012
3013   /* Any cached compilation units will be linked by the per-objfile
3014      read_in_chain.  Make sure to free them when we're done.  */
3015   back_to = make_cleanup (free_cached_comp_units, NULL);
3016
3017   build_type_psymtabs (objfile);
3018
3019   create_all_comp_units (objfile);
3020
3021   /* Create a temporary address map on a temporary obstack.  We later
3022      copy this to the final obstack.  */
3023   obstack_init (&temp_obstack);
3024   make_cleanup_obstack_free (&temp_obstack);
3025   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3026   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3027
3028   /* Since the objects we're extracting from .debug_info vary in
3029      length, only the individual functions to extract them (like
3030      read_comp_unit_head and load_partial_die) can really know whether
3031      the buffer is large enough to hold another complete object.
3032
3033      At the moment, they don't actually check that.  If .debug_info
3034      holds just one extra byte after the last compilation unit's dies,
3035      then read_comp_unit_head will happily read off the end of the
3036      buffer.  read_partial_die is similarly casual.  Those functions
3037      should be fixed.
3038
3039      For this loop condition, simply checking whether there's any data
3040      left at all should be sufficient.  */
3041
3042   while (info_ptr < (dwarf2_per_objfile->info.buffer
3043                      + dwarf2_per_objfile->info.size))
3044     {
3045       struct dwarf2_per_cu_data *this_cu;
3046
3047       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3048                                        objfile);
3049
3050       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3051                                             dwarf2_per_objfile->info.buffer,
3052                                             info_ptr,
3053                                             dwarf2_per_objfile->info.size);
3054     }
3055
3056   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3057                                                     &objfile->objfile_obstack);
3058   discard_cleanups (addrmap_cleanup);
3059
3060   do_cleanups (back_to);
3061 }
3062
3063 /* Load the partial DIEs for a secondary CU into memory.  */
3064
3065 static void
3066 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3067                         struct objfile *objfile)
3068 {
3069   bfd *abfd = objfile->obfd;
3070   gdb_byte *info_ptr, *beg_of_comp_unit;
3071   struct die_info *comp_unit_die;
3072   struct dwarf2_cu *cu;
3073   struct cleanup *back_to;
3074   struct attribute *attr;
3075   int has_children;
3076   struct die_reader_specs reader_specs;
3077
3078   gdb_assert (! this_cu->from_debug_types);
3079
3080   gdb_assert (dwarf2_per_objfile->info.readin);
3081   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3082   beg_of_comp_unit = info_ptr;
3083
3084   cu = alloc_one_comp_unit (objfile);
3085
3086   /* ??? Missing cleanup for CU?  */
3087
3088   /* Link this compilation unit into the compilation unit tree.  */
3089   this_cu->cu = cu;
3090   cu->per_cu = this_cu;
3091   cu->type_hash = this_cu->type_hash;
3092
3093   info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3094                                           dwarf2_per_objfile->info.buffer,
3095                                           dwarf2_per_objfile->info.size,
3096                                           abfd);
3097
3098   /* Complete the cu_header.  */
3099   cu->header.offset = this_cu->offset;
3100   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3101
3102   /* Read the abbrevs for this compilation unit into a table.  */
3103   dwarf2_read_abbrevs (abfd, cu);
3104   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3105
3106   /* Read the compilation unit die.  */
3107   init_cu_die_reader (&reader_specs, cu);
3108   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3109                             &has_children);
3110
3111   /* Set the language we're debugging.  */
3112   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3113   if (attr)
3114     set_cu_language (DW_UNSND (attr), cu);
3115   else
3116     set_cu_language (language_minimal, cu);
3117
3118   /* Check if comp unit has_children.
3119      If so, read the rest of the partial symbols from this comp unit.
3120      If not, there's no more debug_info for this comp unit. */
3121   if (has_children)
3122     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3123
3124   do_cleanups (back_to);
3125 }
3126
3127 /* Create a list of all compilation units in OBJFILE.  We do this only
3128    if an inter-comp-unit reference is found; presumably if there is one,
3129    there will be many, and one will occur early in the .debug_info section.
3130    So there's no point in building this list incrementally.  */
3131
3132 static void
3133 create_all_comp_units (struct objfile *objfile)
3134 {
3135   int n_allocated;
3136   int n_comp_units;
3137   struct dwarf2_per_cu_data **all_comp_units;
3138   gdb_byte *info_ptr;
3139
3140   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3141   info_ptr = dwarf2_per_objfile->info.buffer;
3142
3143   n_comp_units = 0;
3144   n_allocated = 10;
3145   all_comp_units = xmalloc (n_allocated
3146                             * sizeof (struct dwarf2_per_cu_data *));
3147
3148   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3149     {
3150       unsigned int length, initial_length_size;
3151       struct dwarf2_per_cu_data *this_cu;
3152       unsigned int offset;
3153
3154       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3155
3156       /* Read just enough information to find out where the next
3157          compilation unit is.  */
3158       length = read_initial_length (objfile->obfd, info_ptr,
3159                                     &initial_length_size);
3160
3161       /* Save the compilation unit for later lookup.  */
3162       this_cu = obstack_alloc (&objfile->objfile_obstack,
3163                                sizeof (struct dwarf2_per_cu_data));
3164       memset (this_cu, 0, sizeof (*this_cu));
3165       this_cu->offset = offset;
3166       this_cu->length = length + initial_length_size;
3167       this_cu->objfile = objfile;
3168
3169       if (n_comp_units == n_allocated)
3170         {
3171           n_allocated *= 2;
3172           all_comp_units = xrealloc (all_comp_units,
3173                                      n_allocated
3174                                      * sizeof (struct dwarf2_per_cu_data *));
3175         }
3176       all_comp_units[n_comp_units++] = this_cu;
3177
3178       info_ptr = info_ptr + this_cu->length;
3179     }
3180
3181   dwarf2_per_objfile->all_comp_units
3182     = obstack_alloc (&objfile->objfile_obstack,
3183                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3184   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3185           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3186   xfree (all_comp_units);
3187   dwarf2_per_objfile->n_comp_units = n_comp_units;
3188 }
3189
3190 /* Process all loaded DIEs for compilation unit CU, starting at
3191    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3192    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3193    DW_AT_ranges).  If NEED_PC is set, then this function will set
3194    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3195    and record the covered ranges in the addrmap.  */
3196
3197 static void
3198 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3199                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3200 {
3201   struct partial_die_info *pdi;
3202
3203   /* Now, march along the PDI's, descending into ones which have
3204      interesting children but skipping the children of the other ones,
3205      until we reach the end of the compilation unit.  */
3206
3207   pdi = first_die;
3208
3209   while (pdi != NULL)
3210     {
3211       fixup_partial_die (pdi, cu);
3212
3213       /* Anonymous namespaces or modules have no name but have interesting
3214          children, so we need to look at them.  Ditto for anonymous
3215          enums.  */
3216
3217       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3218           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3219         {
3220           switch (pdi->tag)
3221             {
3222             case DW_TAG_subprogram:
3223               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3224               break;
3225             case DW_TAG_variable:
3226             case DW_TAG_typedef:
3227             case DW_TAG_union_type:
3228               if (!pdi->is_declaration)
3229                 {
3230                   add_partial_symbol (pdi, cu);
3231                 }
3232               break;
3233             case DW_TAG_class_type:
3234             case DW_TAG_interface_type:
3235             case DW_TAG_structure_type:
3236               if (!pdi->is_declaration)
3237                 {
3238                   add_partial_symbol (pdi, cu);
3239                 }
3240               break;
3241             case DW_TAG_enumeration_type:
3242               if (!pdi->is_declaration)
3243                 add_partial_enumeration (pdi, cu);
3244               break;
3245             case DW_TAG_base_type:
3246             case DW_TAG_subrange_type:
3247               /* File scope base type definitions are added to the partial
3248                  symbol table.  */
3249               add_partial_symbol (pdi, cu);
3250               break;
3251             case DW_TAG_namespace:
3252               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3253               break;
3254             case DW_TAG_module:
3255               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3256               break;
3257             default:
3258               break;
3259             }
3260         }
3261
3262       /* If the die has a sibling, skip to the sibling.  */
3263
3264       pdi = pdi->die_sibling;
3265     }
3266 }
3267
3268 /* Functions used to compute the fully scoped name of a partial DIE.
3269
3270    Normally, this is simple.  For C++, the parent DIE's fully scoped
3271    name is concatenated with "::" and the partial DIE's name.  For
3272    Java, the same thing occurs except that "." is used instead of "::".
3273    Enumerators are an exception; they use the scope of their parent
3274    enumeration type, i.e. the name of the enumeration type is not
3275    prepended to the enumerator.
3276
3277    There are two complexities.  One is DW_AT_specification; in this
3278    case "parent" means the parent of the target of the specification,
3279    instead of the direct parent of the DIE.  The other is compilers
3280    which do not emit DW_TAG_namespace; in this case we try to guess
3281    the fully qualified name of structure types from their members'
3282    linkage names.  This must be done using the DIE's children rather
3283    than the children of any DW_AT_specification target.  We only need
3284    to do this for structures at the top level, i.e. if the target of
3285    any DW_AT_specification (if any; otherwise the DIE itself) does not
3286    have a parent.  */
3287
3288 /* Compute the scope prefix associated with PDI's parent, in
3289    compilation unit CU.  The result will be allocated on CU's
3290    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3291    field.  NULL is returned if no prefix is necessary.  */
3292 static char *
3293 partial_die_parent_scope (struct partial_die_info *pdi,
3294                           struct dwarf2_cu *cu)
3295 {
3296   char *grandparent_scope;
3297   struct partial_die_info *parent, *real_pdi;
3298
3299   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3300      then this means the parent of the specification DIE.  */
3301
3302   real_pdi = pdi;
3303   while (real_pdi->has_specification)
3304     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3305
3306   parent = real_pdi->die_parent;
3307   if (parent == NULL)
3308     return NULL;
3309
3310   if (parent->scope_set)
3311     return parent->scope;
3312
3313   fixup_partial_die (parent, cu);
3314
3315   grandparent_scope = partial_die_parent_scope (parent, cu);
3316
3317   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3318      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3319      Work around this problem here.  */
3320   if (cu->language == language_cplus
3321       && parent->tag == DW_TAG_namespace
3322       && strcmp (parent->name, "::") == 0
3323       && grandparent_scope == NULL)
3324     {
3325       parent->scope = NULL;
3326       parent->scope_set = 1;
3327       return NULL;
3328     }
3329
3330   if (parent->tag == DW_TAG_namespace
3331       || parent->tag == DW_TAG_module
3332       || parent->tag == DW_TAG_structure_type
3333       || parent->tag == DW_TAG_class_type
3334       || parent->tag == DW_TAG_interface_type
3335       || parent->tag == DW_TAG_union_type
3336       || parent->tag == DW_TAG_enumeration_type)
3337     {
3338       if (grandparent_scope == NULL)
3339         parent->scope = parent->name;
3340       else
3341         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3342                                          parent->name, 0, cu);
3343     }
3344   else if (parent->tag == DW_TAG_enumerator)
3345     /* Enumerators should not get the name of the enumeration as a prefix.  */
3346     parent->scope = grandparent_scope;
3347   else
3348     {
3349       /* FIXME drow/2004-04-01: What should we be doing with
3350          function-local names?  For partial symbols, we should probably be
3351          ignoring them.  */
3352       complaint (&symfile_complaints,
3353                  _("unhandled containing DIE tag %d for DIE at %d"),
3354                  parent->tag, pdi->offset);
3355       parent->scope = grandparent_scope;
3356     }
3357
3358   parent->scope_set = 1;
3359   return parent->scope;
3360 }
3361
3362 /* Return the fully scoped name associated with PDI, from compilation unit
3363    CU.  The result will be allocated with malloc.  */
3364 static char *
3365 partial_die_full_name (struct partial_die_info *pdi,
3366                        struct dwarf2_cu *cu)
3367 {
3368   char *parent_scope;
3369
3370   parent_scope = partial_die_parent_scope (pdi, cu);
3371   if (parent_scope == NULL)
3372     return NULL;
3373   else
3374     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3375 }
3376
3377 static void
3378 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3379 {
3380   struct objfile *objfile = cu->objfile;
3381   CORE_ADDR addr = 0;
3382   char *actual_name = NULL;
3383   const struct partial_symbol *psym = NULL;
3384   CORE_ADDR baseaddr;
3385   int built_actual_name = 0;
3386
3387   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3388
3389   actual_name = partial_die_full_name (pdi, cu);
3390   if (actual_name)
3391     built_actual_name = 1;
3392
3393   if (actual_name == NULL)
3394     actual_name = pdi->name;
3395
3396   switch (pdi->tag)
3397     {
3398     case DW_TAG_subprogram:
3399       if (pdi->is_external || cu->language == language_ada)
3400         {
3401           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3402              of the global scope.  But in Ada, we want to be able to access
3403              nested procedures globally.  So all Ada subprograms are stored
3404              in the global scope.  */
3405           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3406              mst_text, objfile); */
3407           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3408                                       built_actual_name,
3409                                       VAR_DOMAIN, LOC_BLOCK,
3410                                       &objfile->global_psymbols,
3411                                       0, pdi->lowpc + baseaddr,
3412                                       cu->language, objfile);
3413         }
3414       else
3415         {
3416           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3417              mst_file_text, objfile); */
3418           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3419                                       built_actual_name,
3420                                       VAR_DOMAIN, LOC_BLOCK,
3421                                       &objfile->static_psymbols,
3422                                       0, pdi->lowpc + baseaddr,
3423                                       cu->language, objfile);
3424         }
3425       break;
3426     case DW_TAG_variable:
3427       if (pdi->is_external)
3428         {
3429           /* Global Variable.
3430              Don't enter into the minimal symbol tables as there is
3431              a minimal symbol table entry from the ELF symbols already.
3432              Enter into partial symbol table if it has a location
3433              descriptor or a type.
3434              If the location descriptor is missing, new_symbol will create
3435              a LOC_UNRESOLVED symbol, the address of the variable will then
3436              be determined from the minimal symbol table whenever the variable
3437              is referenced.
3438              The address for the partial symbol table entry is not
3439              used by GDB, but it comes in handy for debugging partial symbol
3440              table building.  */
3441
3442           if (pdi->locdesc)
3443             addr = decode_locdesc (pdi->locdesc, cu);
3444           if (pdi->locdesc || pdi->has_type)
3445             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3446                                         built_actual_name,
3447                                         VAR_DOMAIN, LOC_STATIC,
3448                                         &objfile->global_psymbols,
3449                                         0, addr + baseaddr,
3450                                         cu->language, objfile);
3451         }
3452       else
3453         {
3454           /* Static Variable. Skip symbols without location descriptors.  */
3455           if (pdi->locdesc == NULL)
3456             {
3457               if (built_actual_name)
3458                 xfree (actual_name);
3459               return;
3460             }
3461           addr = decode_locdesc (pdi->locdesc, cu);
3462           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3463              mst_file_data, objfile); */
3464           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3465                                       built_actual_name,
3466                                       VAR_DOMAIN, LOC_STATIC,
3467                                       &objfile->static_psymbols,
3468                                       0, addr + baseaddr,
3469                                       cu->language, objfile);
3470         }
3471       break;
3472     case DW_TAG_typedef:
3473     case DW_TAG_base_type:
3474     case DW_TAG_subrange_type:
3475       add_psymbol_to_list (actual_name, strlen (actual_name),
3476                            built_actual_name,
3477                            VAR_DOMAIN, LOC_TYPEDEF,
3478                            &objfile->static_psymbols,
3479                            0, (CORE_ADDR) 0, cu->language, objfile);
3480       break;
3481     case DW_TAG_namespace:
3482       add_psymbol_to_list (actual_name, strlen (actual_name),
3483                            built_actual_name,
3484                            VAR_DOMAIN, LOC_TYPEDEF,
3485                            &objfile->global_psymbols,
3486                            0, (CORE_ADDR) 0, cu->language, objfile);
3487       break;
3488     case DW_TAG_class_type:
3489     case DW_TAG_interface_type:
3490     case DW_TAG_structure_type:
3491     case DW_TAG_union_type:
3492     case DW_TAG_enumeration_type:
3493       /* Skip external references.  The DWARF standard says in the section
3494          about "Structure, Union, and Class Type Entries": "An incomplete
3495          structure, union or class type is represented by a structure,
3496          union or class entry that does not have a byte size attribute
3497          and that has a DW_AT_declaration attribute."  */
3498       if (!pdi->has_byte_size && pdi->is_declaration)
3499         {
3500           if (built_actual_name)
3501             xfree (actual_name);
3502           return;
3503         }
3504
3505       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3506          static vs. global.  */
3507       add_psymbol_to_list (actual_name, strlen (actual_name),
3508                            built_actual_name,
3509                            STRUCT_DOMAIN, LOC_TYPEDEF,
3510                            (cu->language == language_cplus
3511                             || cu->language == language_java)
3512                            ? &objfile->global_psymbols
3513                            : &objfile->static_psymbols,
3514                            0, (CORE_ADDR) 0, cu->language, objfile);
3515
3516       break;
3517     case DW_TAG_enumerator:
3518       add_psymbol_to_list (actual_name, strlen (actual_name),
3519                            built_actual_name,
3520                            VAR_DOMAIN, LOC_CONST,
3521                            (cu->language == language_cplus
3522                             || cu->language == language_java)
3523                            ? &objfile->global_psymbols
3524                            : &objfile->static_psymbols,
3525                            0, (CORE_ADDR) 0, cu->language, objfile);
3526       break;
3527     default:
3528       break;
3529     }
3530
3531   if (built_actual_name)
3532     xfree (actual_name);
3533 }
3534
3535 /* Read a partial die corresponding to a namespace; also, add a symbol
3536    corresponding to that namespace to the symbol table.  NAMESPACE is
3537    the name of the enclosing namespace.  */
3538
3539 static void
3540 add_partial_namespace (struct partial_die_info *pdi,
3541                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3542                        int need_pc, struct dwarf2_cu *cu)
3543 {
3544   /* Add a symbol for the namespace.  */
3545
3546   add_partial_symbol (pdi, cu);
3547
3548   /* Now scan partial symbols in that namespace.  */
3549
3550   if (pdi->has_children)
3551     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3552 }
3553
3554 /* Read a partial die corresponding to a Fortran module.  */
3555
3556 static void
3557 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3558                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3559 {
3560   /* Now scan partial symbols in that module.  */
3561
3562   if (pdi->has_children)
3563     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3564 }
3565
3566 /* Read a partial die corresponding to a subprogram and create a partial
3567    symbol for that subprogram.  When the CU language allows it, this
3568    routine also defines a partial symbol for each nested subprogram
3569    that this subprogram contains.
3570
3571    DIE my also be a lexical block, in which case we simply search
3572    recursively for suprograms defined inside that lexical block.
3573    Again, this is only performed when the CU language allows this
3574    type of definitions.  */
3575
3576 static void
3577 add_partial_subprogram (struct partial_die_info *pdi,
3578                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3579                         int need_pc, struct dwarf2_cu *cu)
3580 {
3581   if (pdi->tag == DW_TAG_subprogram)
3582     {
3583       if (pdi->has_pc_info)
3584         {
3585           if (pdi->lowpc < *lowpc)
3586             *lowpc = pdi->lowpc;
3587           if (pdi->highpc > *highpc)
3588             *highpc = pdi->highpc;
3589           if (need_pc)
3590             {
3591               CORE_ADDR baseaddr;
3592               struct objfile *objfile = cu->objfile;
3593
3594               baseaddr = ANOFFSET (objfile->section_offsets,
3595                                    SECT_OFF_TEXT (objfile));
3596               addrmap_set_empty (objfile->psymtabs_addrmap,
3597                                  pdi->lowpc + baseaddr,
3598                                  pdi->highpc - 1 + baseaddr,
3599                                  cu->per_cu->v.psymtab);
3600             }
3601           if (!pdi->is_declaration)
3602             /* Ignore subprogram DIEs that do not have a name, they are
3603                illegal.  Do not emit a complaint at this point, we will
3604                do so when we convert this psymtab into a symtab.  */
3605             if (pdi->name)
3606               add_partial_symbol (pdi, cu);
3607         }
3608     }
3609
3610   if (! pdi->has_children)
3611     return;
3612
3613   if (cu->language == language_ada)
3614     {
3615       pdi = pdi->die_child;
3616       while (pdi != NULL)
3617         {
3618           fixup_partial_die (pdi, cu);
3619           if (pdi->tag == DW_TAG_subprogram
3620               || pdi->tag == DW_TAG_lexical_block)
3621             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3622           pdi = pdi->die_sibling;
3623         }
3624     }
3625 }
3626
3627 /* See if we can figure out if the class lives in a namespace.  We do
3628    this by looking for a member function; its demangled name will
3629    contain namespace info, if there is any.  */
3630
3631 static void
3632 guess_structure_name (struct partial_die_info *struct_pdi,
3633                       struct dwarf2_cu *cu)
3634 {
3635   if ((cu->language == language_cplus
3636        || cu->language == language_java)
3637       && cu->has_namespace_info == 0
3638       && struct_pdi->has_children)
3639     {
3640       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3641          what template types look like, because the demangler
3642          frequently doesn't give the same name as the debug info.  We
3643          could fix this by only using the demangled name to get the
3644          prefix (but see comment in read_structure_type).  */
3645
3646       struct partial_die_info *real_pdi;
3647
3648       /* If this DIE (this DIE's specification, if any) has a parent, then
3649          we should not do this.  We'll prepend the parent's fully qualified
3650          name when we create the partial symbol.  */
3651
3652       real_pdi = struct_pdi;
3653       while (real_pdi->has_specification)
3654         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3655
3656       if (real_pdi->die_parent != NULL)
3657         return;
3658     }
3659 }
3660
3661 /* Read a partial die corresponding to an enumeration type.  */
3662
3663 static void
3664 add_partial_enumeration (struct partial_die_info *enum_pdi,
3665                          struct dwarf2_cu *cu)
3666 {
3667   struct partial_die_info *pdi;
3668
3669   if (enum_pdi->name != NULL)
3670     add_partial_symbol (enum_pdi, cu);
3671
3672   pdi = enum_pdi->die_child;
3673   while (pdi)
3674     {
3675       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3676         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3677       else
3678         add_partial_symbol (pdi, cu);
3679       pdi = pdi->die_sibling;
3680     }
3681 }
3682
3683 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3684    Return the corresponding abbrev, or NULL if the number is zero (indicating
3685    an empty DIE).  In either case *BYTES_READ will be set to the length of
3686    the initial number.  */
3687
3688 static struct abbrev_info *
3689 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3690                  struct dwarf2_cu *cu)
3691 {
3692   bfd *abfd = cu->objfile->obfd;
3693   unsigned int abbrev_number;
3694   struct abbrev_info *abbrev;
3695
3696   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3697
3698   if (abbrev_number == 0)
3699     return NULL;
3700
3701   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3702   if (!abbrev)
3703     {
3704       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3705                       bfd_get_filename (abfd));
3706     }
3707
3708   return abbrev;
3709 }
3710
3711 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3712    Returns a pointer to the end of a series of DIEs, terminated by an empty
3713    DIE.  Any children of the skipped DIEs will also be skipped.  */
3714
3715 static gdb_byte *
3716 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3717 {
3718   struct abbrev_info *abbrev;
3719   unsigned int bytes_read;
3720
3721   while (1)
3722     {
3723       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3724       if (abbrev == NULL)
3725         return info_ptr + bytes_read;
3726       else
3727         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3728     }
3729 }
3730
3731 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3732    INFO_PTR should point just after the initial uleb128 of a DIE, and the
3733    abbrev corresponding to that skipped uleb128 should be passed in
3734    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
3735    children.  */
3736
3737 static gdb_byte *
3738 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3739               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3740 {
3741   unsigned int bytes_read;
3742   struct attribute attr;
3743   bfd *abfd = cu->objfile->obfd;
3744   unsigned int form, i;
3745
3746   for (i = 0; i < abbrev->num_attrs; i++)
3747     {
3748       /* The only abbrev we care about is DW_AT_sibling.  */
3749       if (abbrev->attrs[i].name == DW_AT_sibling)
3750         {
3751           read_attribute (&attr, &abbrev->attrs[i],
3752                           abfd, info_ptr, cu);
3753           if (attr.form == DW_FORM_ref_addr)
3754             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
3755           else
3756             return buffer + dwarf2_get_ref_die_offset (&attr);
3757         }
3758
3759       /* If it isn't DW_AT_sibling, skip this attribute.  */
3760       form = abbrev->attrs[i].form;
3761     skip_attribute:
3762       switch (form)
3763         {
3764         case DW_FORM_ref_addr:
3765           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3766              and later it is offset sized.  */
3767           if (cu->header.version == 2)
3768             info_ptr += cu->header.addr_size;
3769           else
3770             info_ptr += cu->header.offset_size;
3771           break;
3772         case DW_FORM_addr:
3773           info_ptr += cu->header.addr_size;
3774           break;
3775         case DW_FORM_data1:
3776         case DW_FORM_ref1:
3777         case DW_FORM_flag:
3778           info_ptr += 1;
3779           break;
3780         case DW_FORM_flag_present:
3781           break;
3782         case DW_FORM_data2:
3783         case DW_FORM_ref2:
3784           info_ptr += 2;
3785           break;
3786         case DW_FORM_data4:
3787         case DW_FORM_ref4:
3788           info_ptr += 4;
3789           break;
3790         case DW_FORM_data8:
3791         case DW_FORM_ref8:
3792         case DW_FORM_sig8:
3793           info_ptr += 8;
3794           break;
3795         case DW_FORM_string:
3796           read_string (abfd, info_ptr, &bytes_read);
3797           info_ptr += bytes_read;
3798           break;
3799         case DW_FORM_sec_offset:
3800         case DW_FORM_strp:
3801           info_ptr += cu->header.offset_size;
3802           break;
3803         case DW_FORM_exprloc:
3804         case DW_FORM_block:
3805           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3806           info_ptr += bytes_read;
3807           break;
3808         case DW_FORM_block1:
3809           info_ptr += 1 + read_1_byte (abfd, info_ptr);
3810           break;
3811         case DW_FORM_block2:
3812           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3813           break;
3814         case DW_FORM_block4:
3815           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3816           break;
3817         case DW_FORM_sdata:
3818         case DW_FORM_udata:
3819         case DW_FORM_ref_udata:
3820           info_ptr = skip_leb128 (abfd, info_ptr);
3821           break;
3822         case DW_FORM_indirect:
3823           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3824           info_ptr += bytes_read;
3825           /* We need to continue parsing from here, so just go back to
3826              the top.  */
3827           goto skip_attribute;
3828
3829         default:
3830           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
3831                  dwarf_form_name (form),
3832                  bfd_get_filename (abfd));
3833         }
3834     }
3835
3836   if (abbrev->has_children)
3837     return skip_children (buffer, info_ptr, cu);
3838   else
3839     return info_ptr;
3840 }
3841
3842 /* Locate ORIG_PDI's sibling.
3843    INFO_PTR should point to the start of the next DIE after ORIG_PDI
3844    in BUFFER.  */
3845
3846 static gdb_byte *
3847 locate_pdi_sibling (struct partial_die_info *orig_pdi,
3848                     gdb_byte *buffer, gdb_byte *info_ptr,
3849                     bfd *abfd, struct dwarf2_cu *cu)
3850 {
3851   /* Do we know the sibling already?  */
3852
3853   if (orig_pdi->sibling)
3854     return orig_pdi->sibling;
3855
3856   /* Are there any children to deal with?  */
3857
3858   if (!orig_pdi->has_children)
3859     return info_ptr;
3860
3861   /* Skip the children the long way.  */
3862
3863   return skip_children (buffer, info_ptr, cu);
3864 }
3865
3866 /* Expand this partial symbol table into a full symbol table.  */
3867
3868 static void
3869 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
3870 {
3871   if (pst != NULL)
3872     {
3873       if (pst->readin)
3874         {
3875           warning (_("bug: psymtab for %s is already read in."), pst->filename);
3876         }
3877       else
3878         {
3879           if (info_verbose)
3880             {
3881               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
3882               gdb_flush (gdb_stdout);
3883             }
3884
3885           /* Restore our global data.  */
3886           dwarf2_per_objfile = objfile_data (pst->objfile,
3887                                              dwarf2_objfile_data_key);
3888
3889           /* If this psymtab is constructed from a debug-only objfile, the
3890              has_section_at_zero flag will not necessarily be correct.  We
3891              can get the correct value for this flag by looking at the data
3892              associated with the (presumably stripped) associated objfile.  */
3893           if (pst->objfile->separate_debug_objfile_backlink)
3894             {
3895               struct dwarf2_per_objfile *dpo_backlink
3896                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
3897                                 dwarf2_objfile_data_key);
3898
3899               dwarf2_per_objfile->has_section_at_zero
3900                 = dpo_backlink->has_section_at_zero;
3901             }
3902
3903           psymtab_to_symtab_1 (pst);
3904
3905           /* Finish up the debug error message.  */
3906           if (info_verbose)
3907             printf_filtered (_("done.\n"));
3908         }
3909     }
3910 }
3911
3912 /* Add PER_CU to the queue.  */
3913
3914 static void
3915 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
3916 {
3917   struct dwarf2_queue_item *item;
3918
3919   per_cu->queued = 1;
3920   item = xmalloc (sizeof (*item));
3921   item->per_cu = per_cu;
3922   item->next = NULL;
3923
3924   if (dwarf2_queue == NULL)
3925     dwarf2_queue = item;
3926   else
3927     dwarf2_queue_tail->next = item;
3928
3929   dwarf2_queue_tail = item;
3930 }
3931
3932 /* Process the queue.  */
3933
3934 static void
3935 process_queue (struct objfile *objfile)
3936 {
3937   struct dwarf2_queue_item *item, *next_item;
3938
3939   /* The queue starts out with one item, but following a DIE reference
3940      may load a new CU, adding it to the end of the queue.  */
3941   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
3942     {
3943       if (dwarf2_per_objfile->using_index
3944           ? !item->per_cu->v.quick->symtab
3945           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
3946         process_full_comp_unit (item->per_cu);
3947
3948       item->per_cu->queued = 0;
3949       next_item = item->next;
3950       xfree (item);
3951     }
3952
3953   dwarf2_queue_tail = NULL;
3954 }
3955
3956 /* Free all allocated queue entries.  This function only releases anything if
3957    an error was thrown; if the queue was processed then it would have been
3958    freed as we went along.  */
3959
3960 static void
3961 dwarf2_release_queue (void *dummy)
3962 {
3963   struct dwarf2_queue_item *item, *last;
3964
3965   item = dwarf2_queue;
3966   while (item)
3967     {
3968       /* Anything still marked queued is likely to be in an
3969          inconsistent state, so discard it.  */
3970       if (item->per_cu->queued)
3971         {
3972           if (item->per_cu->cu != NULL)
3973             free_one_cached_comp_unit (item->per_cu->cu);
3974           item->per_cu->queued = 0;
3975         }
3976
3977       last = item;
3978       item = item->next;
3979       xfree (last);
3980     }
3981
3982   dwarf2_queue = dwarf2_queue_tail = NULL;
3983 }
3984
3985 /* Read in full symbols for PST, and anything it depends on.  */
3986
3987 static void
3988 psymtab_to_symtab_1 (struct partial_symtab *pst)
3989 {
3990   struct dwarf2_per_cu_data *per_cu;
3991   struct cleanup *back_to;
3992   int i;
3993
3994   for (i = 0; i < pst->number_of_dependencies; i++)
3995     if (!pst->dependencies[i]->readin)
3996       {
3997         /* Inform about additional files that need to be read in.  */
3998         if (info_verbose)
3999           {
4000             /* FIXME: i18n: Need to make this a single string.  */
4001             fputs_filtered (" ", gdb_stdout);
4002             wrap_here ("");
4003             fputs_filtered ("and ", gdb_stdout);
4004             wrap_here ("");
4005             printf_filtered ("%s...", pst->dependencies[i]->filename);
4006             wrap_here ("");     /* Flush output */
4007             gdb_flush (gdb_stdout);
4008           }
4009         psymtab_to_symtab_1 (pst->dependencies[i]);
4010       }
4011
4012   per_cu = pst->read_symtab_private;
4013
4014   if (per_cu == NULL)
4015     {
4016       /* It's an include file, no symbols to read for it.
4017          Everything is in the parent symtab.  */
4018       pst->readin = 1;
4019       return;
4020     }
4021
4022   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4023 }
4024
4025 /* Load the DIEs associated with PER_CU into memory.  */
4026
4027 static void
4028 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4029 {
4030   bfd *abfd = objfile->obfd;
4031   struct dwarf2_cu *cu;
4032   unsigned int offset;
4033   gdb_byte *info_ptr, *beg_of_comp_unit;
4034   struct cleanup *back_to, *free_cu_cleanup;
4035   struct attribute *attr;
4036
4037   gdb_assert (! per_cu->from_debug_types);
4038
4039   /* Set local variables from the partial symbol table info.  */
4040   offset = per_cu->offset;
4041
4042   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4043   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4044   beg_of_comp_unit = info_ptr;
4045
4046   cu = alloc_one_comp_unit (objfile);
4047
4048   /* If an error occurs while loading, release our storage.  */
4049   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4050
4051   /* Read in the comp_unit header.  */
4052   info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4053
4054   /* Complete the cu_header.  */
4055   cu->header.offset = offset;
4056   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4057
4058   /* Read the abbrevs for this compilation unit.  */
4059   dwarf2_read_abbrevs (abfd, cu);
4060   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4061
4062   /* Link this compilation unit into the compilation unit tree.  */
4063   per_cu->cu = cu;
4064   cu->per_cu = per_cu;
4065   cu->type_hash = per_cu->type_hash;
4066
4067   cu->dies = read_comp_unit (info_ptr, cu);
4068
4069   /* We try not to read any attributes in this function, because not
4070      all objfiles needed for references have been loaded yet, and symbol
4071      table processing isn't initialized.  But we have to set the CU language,
4072      or we won't be able to build types correctly.  */
4073   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4074   if (attr)
4075     set_cu_language (DW_UNSND (attr), cu);
4076   else
4077     set_cu_language (language_minimal, cu);
4078
4079   /* Similarly, if we do not read the producer, we can not apply
4080      producer-specific interpretation.  */
4081   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4082   if (attr)
4083     cu->producer = DW_STRING (attr);
4084
4085   /* Link this CU into read_in_chain.  */
4086   per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4087   dwarf2_per_objfile->read_in_chain = per_cu;
4088
4089   do_cleanups (back_to);
4090
4091   /* We've successfully allocated this compilation unit.  Let our caller
4092      clean it up when finished with it.  */
4093   discard_cleanups (free_cu_cleanup);
4094 }
4095
4096 /* Generate full symbol information for PST and CU, whose DIEs have
4097    already been loaded into memory.  */
4098
4099 static void
4100 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4101 {
4102   struct dwarf2_cu *cu = per_cu->cu;
4103   struct objfile *objfile = per_cu->objfile;
4104   CORE_ADDR lowpc, highpc;
4105   struct symtab *symtab;
4106   struct cleanup *back_to;
4107   CORE_ADDR baseaddr;
4108
4109   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4110
4111   buildsym_init ();
4112   back_to = make_cleanup (really_free_pendings, NULL);
4113
4114   cu->list_in_scope = &file_symbols;
4115
4116   dwarf2_find_base_address (cu->dies, cu);
4117
4118   /* Do line number decoding in read_file_scope () */
4119   process_die (cu->dies, cu);
4120
4121   /* Some compilers don't define a DW_AT_high_pc attribute for the
4122      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4123      it, by scanning the DIE's below the compilation unit.  */
4124   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4125
4126   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4127
4128   /* Set symtab language to language from DW_AT_language.
4129      If the compilation is from a C file generated by language preprocessors,
4130      do not set the language if it was already deduced by start_subfile.  */
4131   if (symtab != NULL
4132       && !(cu->language == language_c && symtab->language != language_c))
4133     {
4134       symtab->language = cu->language;
4135     }
4136
4137   if (dwarf2_per_objfile->using_index)
4138     per_cu->v.quick->symtab = symtab;
4139   else
4140     {
4141       struct partial_symtab *pst = per_cu->v.psymtab;
4142       pst->symtab = symtab;
4143       pst->readin = 1;
4144     }
4145
4146   do_cleanups (back_to);
4147 }
4148
4149 /* Process a die and its children.  */
4150
4151 static void
4152 process_die (struct die_info *die, struct dwarf2_cu *cu)
4153 {
4154   switch (die->tag)
4155     {
4156     case DW_TAG_padding:
4157       break;
4158     case DW_TAG_compile_unit:
4159       read_file_scope (die, cu);
4160       break;
4161     case DW_TAG_type_unit:
4162       read_type_unit_scope (die, cu);
4163       break;
4164     case DW_TAG_subprogram:
4165     case DW_TAG_inlined_subroutine:
4166       read_func_scope (die, cu);
4167       break;
4168     case DW_TAG_lexical_block:
4169     case DW_TAG_try_block:
4170     case DW_TAG_catch_block:
4171       read_lexical_block_scope (die, cu);
4172       break;
4173     case DW_TAG_class_type:
4174     case DW_TAG_interface_type:
4175     case DW_TAG_structure_type:
4176     case DW_TAG_union_type:
4177       process_structure_scope (die, cu);
4178       break;
4179     case DW_TAG_enumeration_type:
4180       process_enumeration_scope (die, cu);
4181       break;
4182
4183     /* These dies have a type, but processing them does not create
4184        a symbol or recurse to process the children.  Therefore we can
4185        read them on-demand through read_type_die.  */
4186     case DW_TAG_subroutine_type:
4187     case DW_TAG_set_type:
4188     case DW_TAG_array_type:
4189     case DW_TAG_pointer_type:
4190     case DW_TAG_ptr_to_member_type:
4191     case DW_TAG_reference_type:
4192     case DW_TAG_string_type:
4193       break;
4194
4195     case DW_TAG_base_type:
4196     case DW_TAG_subrange_type:
4197     case DW_TAG_typedef:
4198     case DW_TAG_const_type:
4199     case DW_TAG_volatile_type:
4200       /* Add a typedef symbol for the type definition, if it has a
4201          DW_AT_name.  */
4202       new_symbol (die, read_type_die (die, cu), cu);
4203       break;
4204     case DW_TAG_common_block:
4205       read_common_block (die, cu);
4206       break;
4207     case DW_TAG_common_inclusion:
4208       break;
4209     case DW_TAG_namespace:
4210       processing_has_namespace_info = 1;
4211       read_namespace (die, cu);
4212       break;
4213     case DW_TAG_module:
4214       processing_has_namespace_info = 1;
4215       read_module (die, cu);
4216       break;
4217     case DW_TAG_imported_declaration:
4218     case DW_TAG_imported_module:
4219       processing_has_namespace_info = 1;
4220       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4221                                  || cu->language != language_fortran))
4222         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4223                    dwarf_tag_name (die->tag));
4224       read_import_statement (die, cu);
4225       break;
4226     default:
4227       new_symbol (die, NULL, cu);
4228       break;
4229     }
4230 }
4231
4232 /* A helper function for dwarf2_compute_name which determines whether DIE
4233    needs to have the name of the scope prepended to the name listed in the
4234    die.  */
4235
4236 static int
4237 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4238 {
4239   struct attribute *attr;
4240
4241   switch (die->tag)
4242     {
4243     case DW_TAG_namespace:
4244     case DW_TAG_typedef:
4245     case DW_TAG_class_type:
4246     case DW_TAG_interface_type:
4247     case DW_TAG_structure_type:
4248     case DW_TAG_union_type:
4249     case DW_TAG_enumeration_type:
4250     case DW_TAG_enumerator:
4251     case DW_TAG_subprogram:
4252     case DW_TAG_member:
4253       return 1;
4254
4255     case DW_TAG_variable:
4256       /* We only need to prefix "globally" visible variables.  These include
4257          any variable marked with DW_AT_external or any variable that
4258          lives in a namespace.  [Variables in anonymous namespaces
4259          require prefixing, but they are not DW_AT_external.]  */
4260
4261       if (dwarf2_attr (die, DW_AT_specification, cu))
4262         {
4263           struct dwarf2_cu *spec_cu = cu;
4264
4265           return die_needs_namespace (die_specification (die, &spec_cu),
4266                                       spec_cu);
4267         }
4268
4269       attr = dwarf2_attr (die, DW_AT_external, cu);
4270       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4271           && die->parent->tag != DW_TAG_module)
4272         return 0;
4273       /* A variable in a lexical block of some kind does not need a
4274          namespace, even though in C++ such variables may be external
4275          and have a mangled name.  */
4276       if (die->parent->tag ==  DW_TAG_lexical_block
4277           || die->parent->tag ==  DW_TAG_try_block
4278           || die->parent->tag ==  DW_TAG_catch_block
4279           || die->parent->tag == DW_TAG_subprogram)
4280         return 0;
4281       return 1;
4282
4283     default:
4284       return 0;
4285     }
4286 }
4287
4288 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4289    compute the physname for the object, which include a method's
4290    formal parameters (C++/Java) and return type (Java).
4291
4292    For Ada, return the DIE's linkage name rather than the fully qualified
4293    name.  PHYSNAME is ignored..
4294
4295    The result is allocated on the objfile_obstack and canonicalized.  */
4296
4297 static const char *
4298 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4299                      int physname)
4300 {
4301   if (name == NULL)
4302     name = dwarf2_name (die, cu);
4303
4304   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4305      compute it by typename_concat inside GDB.  */
4306   if (cu->language == language_ada
4307       || (cu->language == language_fortran && physname))
4308     {
4309       /* For Ada unit, we prefer the linkage name over the name, as
4310          the former contains the exported name, which the user expects
4311          to be able to reference.  Ideally, we want the user to be able
4312          to reference this entity using either natural or linkage name,
4313          but we haven't started looking at this enhancement yet.  */
4314       struct attribute *attr;
4315
4316       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4317       if (attr == NULL)
4318         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4319       if (attr && DW_STRING (attr))
4320         return DW_STRING (attr);
4321     }
4322
4323   /* These are the only languages we know how to qualify names in.  */
4324   if (name != NULL
4325       && (cu->language == language_cplus || cu->language == language_java
4326           || cu->language == language_fortran))
4327     {
4328       if (die_needs_namespace (die, cu))
4329         {
4330           long length;
4331           char *prefix;
4332           struct ui_file *buf;
4333
4334           prefix = determine_prefix (die, cu);
4335           buf = mem_fileopen ();
4336           if (*prefix != '\0')
4337             {
4338               char *prefixed_name = typename_concat (NULL, prefix, name,
4339                                                      physname, cu);
4340
4341               fputs_unfiltered (prefixed_name, buf);
4342               xfree (prefixed_name);
4343             }
4344           else
4345             fputs_unfiltered (name ? name : "", buf);
4346
4347           /* For Java and C++ methods, append formal parameter type
4348              information, if PHYSNAME.  */
4349
4350           if (physname && die->tag == DW_TAG_subprogram
4351               && (cu->language == language_cplus
4352                   || cu->language == language_java))
4353             {
4354               struct type *type = read_type_die (die, cu);
4355
4356               c_type_print_args (type, buf, 0, cu->language);
4357
4358               if (cu->language == language_java)
4359                 {
4360                   /* For java, we must append the return type to method
4361                      names. */
4362                   if (die->tag == DW_TAG_subprogram)
4363                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4364                                      0, 0);
4365                 }
4366               else if (cu->language == language_cplus)
4367                 {
4368                   if (TYPE_NFIELDS (type) > 0
4369                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4370                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4371                     fputs_unfiltered (" const", buf);
4372                 }
4373             }
4374
4375           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4376                                        &length);
4377           ui_file_delete (buf);
4378
4379           if (cu->language == language_cplus)
4380             {
4381               char *cname
4382                 = dwarf2_canonicalize_name (name, cu,
4383                                             &cu->objfile->objfile_obstack);
4384
4385               if (cname != NULL)
4386                 name = cname;
4387             }
4388         }
4389     }
4390
4391   return name;
4392 }
4393
4394 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4395    If scope qualifiers are appropriate they will be added.  The result
4396    will be allocated on the objfile_obstack, or NULL if the DIE does
4397    not have a name.  NAME may either be from a previous call to
4398    dwarf2_name or NULL.
4399
4400    The output string will be canonicalized (if C++/Java). */
4401
4402 static const char *
4403 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4404 {
4405   return dwarf2_compute_name (name, die, cu, 0);
4406 }
4407
4408 /* Construct a physname for the given DIE in CU.  NAME may either be
4409    from a previous call to dwarf2_name or NULL.  The result will be
4410    allocated on the objfile_objstack or NULL if the DIE does not have a
4411    name.
4412
4413    The output string will be canonicalized (if C++/Java).  */
4414
4415 static const char *
4416 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4417 {
4418   return dwarf2_compute_name (name, die, cu, 1);
4419 }
4420
4421 /* Read the import statement specified by the given die and record it.  */
4422
4423 static void
4424 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4425 {
4426   struct attribute *import_attr;
4427   struct die_info *imported_die;
4428   struct dwarf2_cu *imported_cu;
4429   const char *imported_name;
4430   const char *imported_name_prefix;
4431   const char *canonical_name;
4432   const char *import_alias;
4433   const char *imported_declaration = NULL;
4434   const char *import_prefix;
4435
4436   char *temp;
4437
4438   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4439   if (import_attr == NULL)
4440     {
4441       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4442                  dwarf_tag_name (die->tag));
4443       return;
4444     }
4445
4446   imported_cu = cu;
4447   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4448   imported_name = dwarf2_name (imported_die, imported_cu);
4449   if (imported_name == NULL)
4450     {
4451       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4452
4453         The import in the following code:
4454         namespace A
4455           {
4456             typedef int B;
4457           }
4458
4459         int main ()
4460           {
4461             using A::B;
4462             B b;
4463             return b;
4464           }
4465
4466         ...
4467          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4468             <52>   DW_AT_decl_file   : 1
4469             <53>   DW_AT_decl_line   : 6
4470             <54>   DW_AT_import      : <0x75>
4471          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4472             <59>   DW_AT_name        : B
4473             <5b>   DW_AT_decl_file   : 1
4474             <5c>   DW_AT_decl_line   : 2
4475             <5d>   DW_AT_type        : <0x6e>
4476         ...
4477          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4478             <76>   DW_AT_byte_size   : 4
4479             <77>   DW_AT_encoding    : 5        (signed)
4480
4481         imports the wrong die ( 0x75 instead of 0x58 ).
4482         This case will be ignored until the gcc bug is fixed.  */
4483       return;
4484     }
4485
4486   /* Figure out the local name after import.  */
4487   import_alias = dwarf2_name (die, cu);
4488
4489   /* Figure out where the statement is being imported to.  */
4490   import_prefix = determine_prefix (die, cu);
4491
4492   /* Figure out what the scope of the imported die is and prepend it
4493      to the name of the imported die.  */
4494   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4495
4496   if (imported_die->tag != DW_TAG_namespace
4497       && imported_die->tag != DW_TAG_module)
4498     {
4499       imported_declaration = imported_name;
4500       canonical_name = imported_name_prefix;
4501     }
4502   else if (strlen (imported_name_prefix) > 0)
4503     {
4504       temp = alloca (strlen (imported_name_prefix)
4505                      + 2 + strlen (imported_name) + 1);
4506       strcpy (temp, imported_name_prefix);
4507       strcat (temp, "::");
4508       strcat (temp, imported_name);
4509       canonical_name = temp;
4510     }
4511   else
4512     canonical_name = imported_name;
4513
4514   cp_add_using_directive (import_prefix,
4515                           canonical_name,
4516                           import_alias,
4517                           imported_declaration,
4518                           &cu->objfile->objfile_obstack);
4519 }
4520
4521 static void
4522 initialize_cu_func_list (struct dwarf2_cu *cu)
4523 {
4524   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4525 }
4526
4527 static void
4528 free_cu_line_header (void *arg)
4529 {
4530   struct dwarf2_cu *cu = arg;
4531
4532   free_line_header (cu->line_header);
4533   cu->line_header = NULL;
4534 }
4535
4536 static void
4537 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4538                          char **name, char **comp_dir)
4539 {
4540   struct attribute *attr;
4541
4542   *name = NULL;
4543   *comp_dir = NULL;
4544
4545   /* Find the filename.  Do not use dwarf2_name here, since the filename
4546      is not a source language identifier.  */
4547   attr = dwarf2_attr (die, DW_AT_name, cu);
4548   if (attr)
4549     {
4550       *name = DW_STRING (attr);
4551     }
4552
4553   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4554   if (attr)
4555     *comp_dir = DW_STRING (attr);
4556   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4557     {
4558       *comp_dir = ldirname (*name);
4559       if (*comp_dir != NULL)
4560         make_cleanup (xfree, *comp_dir);
4561     }
4562   if (*comp_dir != NULL)
4563     {
4564       /* Irix 6.2 native cc prepends <machine>.: to the compilation
4565          directory, get rid of it.  */
4566       char *cp = strchr (*comp_dir, ':');
4567
4568       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4569         *comp_dir = cp + 1;
4570     }
4571
4572   if (*name == NULL)
4573     *name = "<unknown>";
4574 }
4575
4576 static void
4577 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
4578 {
4579   struct objfile *objfile = cu->objfile;
4580   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4581   CORE_ADDR lowpc = ((CORE_ADDR) -1);
4582   CORE_ADDR highpc = ((CORE_ADDR) 0);
4583   struct attribute *attr;
4584   char *name = NULL;
4585   char *comp_dir = NULL;
4586   struct die_info *child_die;
4587   bfd *abfd = objfile->obfd;
4588   struct line_header *line_header = 0;
4589   CORE_ADDR baseaddr;
4590
4591   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4592
4593   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
4594
4595   /* If we didn't find a lowpc, set it to highpc to avoid complaints
4596      from finish_block.  */
4597   if (lowpc == ((CORE_ADDR) -1))
4598     lowpc = highpc;
4599   lowpc += baseaddr;
4600   highpc += baseaddr;
4601
4602   find_file_and_directory (die, cu, &name, &comp_dir);
4603
4604   attr = dwarf2_attr (die, DW_AT_language, cu);
4605   if (attr)
4606     {
4607       set_cu_language (DW_UNSND (attr), cu);
4608     }
4609
4610   attr = dwarf2_attr (die, DW_AT_producer, cu);
4611   if (attr)
4612     cu->producer = DW_STRING (attr);
4613
4614   /* We assume that we're processing GCC output. */
4615   processing_gcc_compilation = 2;
4616
4617   processing_has_namespace_info = 0;
4618
4619   start_symtab (name, comp_dir, lowpc);
4620   record_debugformat ("DWARF 2");
4621   record_producer (cu->producer);
4622
4623   initialize_cu_func_list (cu);
4624
4625   /* Decode line number information if present.  We do this before
4626      processing child DIEs, so that the line header table is available
4627      for DW_AT_decl_file.  */
4628   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4629   if (attr)
4630     {
4631       unsigned int line_offset = DW_UNSND (attr);
4632       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
4633       if (line_header)
4634         {
4635           cu->line_header = line_header;
4636           make_cleanup (free_cu_line_header, cu);
4637           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
4638         }
4639     }
4640
4641   /* Process all dies in compilation unit.  */
4642   if (die->child != NULL)
4643     {
4644       child_die = die->child;
4645       while (child_die && child_die->tag)
4646         {
4647           process_die (child_die, cu);
4648           child_die = sibling_die (child_die);
4649         }
4650     }
4651
4652   /* Decode macro information, if present.  Dwarf 2 macro information
4653      refers to information in the line number info statement program
4654      header, so we can only read it if we've read the header
4655      successfully.  */
4656   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
4657   if (attr && line_header)
4658     {
4659       unsigned int macro_offset = DW_UNSND (attr);
4660
4661       dwarf_decode_macros (line_header, macro_offset,
4662                            comp_dir, abfd, cu);
4663     }
4664   do_cleanups (back_to);
4665 }
4666
4667 /* For TUs we want to skip the first top level sibling if it's not the
4668    actual type being defined by this TU.  In this case the first top
4669    level sibling is there to provide context only.  */
4670
4671 static void
4672 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4673 {
4674   struct objfile *objfile = cu->objfile;
4675   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4676   CORE_ADDR lowpc;
4677   struct attribute *attr;
4678   char *name = NULL;
4679   char *comp_dir = NULL;
4680   struct die_info *child_die;
4681   bfd *abfd = objfile->obfd;
4682
4683   /* start_symtab needs a low pc, but we don't really have one.
4684      Do what read_file_scope would do in the absence of such info.  */
4685   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4686
4687   /* Find the filename.  Do not use dwarf2_name here, since the filename
4688      is not a source language identifier.  */
4689   attr = dwarf2_attr (die, DW_AT_name, cu);
4690   if (attr)
4691     name = DW_STRING (attr);
4692
4693   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4694   if (attr)
4695     comp_dir = DW_STRING (attr);
4696   else if (name != NULL && IS_ABSOLUTE_PATH (name))
4697     {
4698       comp_dir = ldirname (name);
4699       if (comp_dir != NULL)
4700         make_cleanup (xfree, comp_dir);
4701     }
4702
4703   if (name == NULL)
4704     name = "<unknown>";
4705
4706   attr = dwarf2_attr (die, DW_AT_language, cu);
4707   if (attr)
4708     set_cu_language (DW_UNSND (attr), cu);
4709
4710   /* This isn't technically needed today.  It is done for symmetry
4711      with read_file_scope.  */
4712   attr = dwarf2_attr (die, DW_AT_producer, cu);
4713   if (attr)
4714     cu->producer = DW_STRING (attr);
4715
4716   /* We assume that we're processing GCC output. */
4717   processing_gcc_compilation = 2;
4718
4719   processing_has_namespace_info = 0;
4720
4721   start_symtab (name, comp_dir, lowpc);
4722   record_debugformat ("DWARF 2");
4723   record_producer (cu->producer);
4724
4725   /* Process the dies in the type unit.  */
4726   if (die->child == NULL)
4727     {
4728       dump_die_for_error (die);
4729       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4730              bfd_get_filename (abfd));
4731     }
4732
4733   child_die = die->child;
4734
4735   while (child_die && child_die->tag)
4736     {
4737       process_die (child_die, cu);
4738
4739       child_die = sibling_die (child_die);
4740     }
4741
4742   do_cleanups (back_to);
4743 }
4744
4745 static void
4746 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4747                      struct dwarf2_cu *cu)
4748 {
4749   struct function_range *thisfn;
4750
4751   thisfn = (struct function_range *)
4752     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
4753   thisfn->name = name;
4754   thisfn->lowpc = lowpc;
4755   thisfn->highpc = highpc;
4756   thisfn->seen_line = 0;
4757   thisfn->next = NULL;
4758
4759   if (cu->last_fn == NULL)
4760       cu->first_fn = thisfn;
4761   else
4762       cu->last_fn->next = thisfn;
4763
4764   cu->last_fn = thisfn;
4765 }
4766
4767 /* qsort helper for inherit_abstract_dies.  */
4768
4769 static int
4770 unsigned_int_compar (const void *ap, const void *bp)
4771 {
4772   unsigned int a = *(unsigned int *) ap;
4773   unsigned int b = *(unsigned int *) bp;
4774
4775   return (a > b) - (b > a);
4776 }
4777
4778 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4779    Inherit only the children of the DW_AT_abstract_origin DIE not being already
4780    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
4781
4782 static void
4783 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4784 {
4785   struct die_info *child_die;
4786   unsigned die_children_count;
4787   /* CU offsets which were referenced by children of the current DIE.  */
4788   unsigned *offsets;
4789   unsigned *offsets_end, *offsetp;
4790   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
4791   struct die_info *origin_die;
4792   /* Iterator of the ORIGIN_DIE children.  */
4793   struct die_info *origin_child_die;
4794   struct cleanup *cleanups;
4795   struct attribute *attr;
4796
4797   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4798   if (!attr)
4799     return;
4800
4801   origin_die = follow_die_ref (die, attr, &cu);
4802   if (die->tag != origin_die->tag
4803       && !(die->tag == DW_TAG_inlined_subroutine
4804            && origin_die->tag == DW_TAG_subprogram))
4805     complaint (&symfile_complaints,
4806                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4807                die->offset, origin_die->offset);
4808
4809   child_die = die->child;
4810   die_children_count = 0;
4811   while (child_die && child_die->tag)
4812     {
4813       child_die = sibling_die (child_die);
4814       die_children_count++;
4815     }
4816   offsets = xmalloc (sizeof (*offsets) * die_children_count);
4817   cleanups = make_cleanup (xfree, offsets);
4818
4819   offsets_end = offsets;
4820   child_die = die->child;
4821   while (child_die && child_die->tag)
4822     {
4823       /* For each CHILD_DIE, find the corresponding child of
4824          ORIGIN_DIE.  If there is more than one layer of
4825          DW_AT_abstract_origin, follow them all; there shouldn't be,
4826          but GCC versions at least through 4.4 generate this (GCC PR
4827          40573).  */
4828       struct die_info *child_origin_die = child_die;
4829
4830       while (1)
4831         {
4832           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4833           if (attr == NULL)
4834             break;
4835           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4836         }
4837
4838       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
4839          counterpart may exist.  */
4840       if (child_origin_die != child_die)
4841         {
4842           if (child_die->tag != child_origin_die->tag
4843               && !(child_die->tag == DW_TAG_inlined_subroutine
4844                    && child_origin_die->tag == DW_TAG_subprogram))
4845             complaint (&symfile_complaints,
4846                        _("Child DIE 0x%x and its abstract origin 0x%x have "
4847                          "different tags"), child_die->offset,
4848                        child_origin_die->offset);
4849           if (child_origin_die->parent != origin_die)
4850             complaint (&symfile_complaints,
4851                        _("Child DIE 0x%x and its abstract origin 0x%x have "
4852                          "different parents"), child_die->offset,
4853                        child_origin_die->offset);
4854           else
4855             *offsets_end++ = child_origin_die->offset;
4856         }
4857       child_die = sibling_die (child_die);
4858     }
4859   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
4860          unsigned_int_compar);
4861   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
4862     if (offsetp[-1] == *offsetp)
4863       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
4864                                         "to DIE 0x%x as their abstract origin"),
4865                  die->offset, *offsetp);
4866
4867   offsetp = offsets;
4868   origin_child_die = origin_die->child;
4869   while (origin_child_die && origin_child_die->tag)
4870     {
4871       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
4872       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
4873         offsetp++;
4874       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
4875         {
4876           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
4877           process_die (origin_child_die, cu);
4878         }
4879       origin_child_die = sibling_die (origin_child_die);
4880     }
4881
4882   do_cleanups (cleanups);
4883 }
4884
4885 static void
4886 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
4887 {
4888   struct objfile *objfile = cu->objfile;
4889   struct context_stack *new;
4890   CORE_ADDR lowpc;
4891   CORE_ADDR highpc;
4892   struct die_info *child_die;
4893   struct attribute *attr, *call_line, *call_file;
4894   char *name;
4895   CORE_ADDR baseaddr;
4896   struct block *block;
4897   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
4898
4899   if (inlined_func)
4900     {
4901       /* If we do not have call site information, we can't show the
4902          caller of this inlined function.  That's too confusing, so
4903          only use the scope for local variables.  */
4904       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
4905       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
4906       if (call_line == NULL || call_file == NULL)
4907         {
4908           read_lexical_block_scope (die, cu);
4909           return;
4910         }
4911     }
4912
4913   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4914
4915   name = dwarf2_name (die, cu);
4916
4917   /* Ignore functions with missing or empty names.  These are actually
4918      illegal according to the DWARF standard.  */
4919   if (name == NULL)
4920     {
4921       complaint (&symfile_complaints,
4922                  _("missing name for subprogram DIE at %d"), die->offset);
4923       return;
4924     }
4925
4926   /* Ignore functions with missing or invalid low and high pc attributes.  */
4927   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
4928     {
4929       attr = dwarf2_attr (die, DW_AT_external, cu);
4930       if (!attr || !DW_UNSND (attr))
4931         complaint (&symfile_complaints,
4932                    _("cannot get low and high bounds for subprogram DIE at %d"),
4933                    die->offset);
4934       return;
4935     }
4936
4937   lowpc += baseaddr;
4938   highpc += baseaddr;
4939
4940   /* Record the function range for dwarf_decode_lines.  */
4941   add_to_cu_func_list (name, lowpc, highpc, cu);
4942
4943   new = push_context (0, lowpc);
4944   new->name = new_symbol (die, read_type_die (die, cu), cu);
4945
4946   /* If there is a location expression for DW_AT_frame_base, record
4947      it.  */
4948   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4949   if (attr)
4950     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
4951        expression is being recorded directly in the function's symbol
4952        and not in a separate frame-base object.  I guess this hack is
4953        to avoid adding some sort of frame-base adjunct/annex to the
4954        function's symbol :-(.  The problem with doing this is that it
4955        results in a function symbol with a location expression that
4956        has nothing to do with the location of the function, ouch!  The
4957        relationship should be: a function's symbol has-a frame base; a
4958        frame-base has-a location expression.  */
4959     dwarf2_symbol_mark_computed (attr, new->name, cu);
4960
4961   cu->list_in_scope = &local_symbols;
4962
4963   if (die->child != NULL)
4964     {
4965       child_die = die->child;
4966       while (child_die && child_die->tag)
4967         {
4968           process_die (child_die, cu);
4969           child_die = sibling_die (child_die);
4970         }
4971     }
4972
4973   inherit_abstract_dies (die, cu);
4974
4975   /* If we have a DW_AT_specification, we might need to import using
4976      directives from the context of the specification DIE.  See the
4977      comment in determine_prefix.  */
4978   if (cu->language == language_cplus
4979       && dwarf2_attr (die, DW_AT_specification, cu))
4980     {
4981       struct dwarf2_cu *spec_cu = cu;
4982       struct die_info *spec_die = die_specification (die, &spec_cu);
4983
4984       while (spec_die)
4985         {
4986           child_die = spec_die->child;
4987           while (child_die && child_die->tag)
4988             {
4989               if (child_die->tag == DW_TAG_imported_module)
4990                 process_die (child_die, spec_cu);
4991               child_die = sibling_die (child_die);
4992             }
4993
4994           /* In some cases, GCC generates specification DIEs that
4995              themselves contain DW_AT_specification attributes.  */
4996           spec_die = die_specification (spec_die, &spec_cu);
4997         }
4998     }
4999
5000   new = pop_context ();
5001   /* Make a block for the local symbols within.  */
5002   block = finish_block (new->name, &local_symbols, new->old_blocks,
5003                         lowpc, highpc, objfile);
5004
5005   /* For C++, set the block's scope.  */
5006   if (cu->language == language_cplus || cu->language == language_fortran)
5007     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5008                         determine_prefix (die, cu),
5009                         processing_has_namespace_info);
5010
5011   /* If we have address ranges, record them.  */
5012   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5013
5014   /* In C++, we can have functions nested inside functions (e.g., when
5015      a function declares a class that has methods).  This means that
5016      when we finish processing a function scope, we may need to go
5017      back to building a containing block's symbol lists.  */
5018   local_symbols = new->locals;
5019   param_symbols = new->params;
5020   using_directives = new->using_directives;
5021
5022   /* If we've finished processing a top-level function, subsequent
5023      symbols go in the file symbol list.  */
5024   if (outermost_context_p ())
5025     cu->list_in_scope = &file_symbols;
5026 }
5027
5028 /* Process all the DIES contained within a lexical block scope.  Start
5029    a new scope, process the dies, and then close the scope.  */
5030
5031 static void
5032 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5033 {
5034   struct objfile *objfile = cu->objfile;
5035   struct context_stack *new;
5036   CORE_ADDR lowpc, highpc;
5037   struct die_info *child_die;
5038   CORE_ADDR baseaddr;
5039
5040   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5041
5042   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5043   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5044      as multiple lexical blocks?  Handling children in a sane way would
5045      be nasty.  Might be easier to properly extend generic blocks to
5046      describe ranges.  */
5047   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5048     return;
5049   lowpc += baseaddr;
5050   highpc += baseaddr;
5051
5052   push_context (0, lowpc);
5053   if (die->child != NULL)
5054     {
5055       child_die = die->child;
5056       while (child_die && child_die->tag)
5057         {
5058           process_die (child_die, cu);
5059           child_die = sibling_die (child_die);
5060         }
5061     }
5062   new = pop_context ();
5063
5064   if (local_symbols != NULL || using_directives != NULL)
5065     {
5066       struct block *block
5067         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5068                         highpc, objfile);
5069
5070       /* Note that recording ranges after traversing children, as we
5071          do here, means that recording a parent's ranges entails
5072          walking across all its children's ranges as they appear in
5073          the address map, which is quadratic behavior.
5074
5075          It would be nicer to record the parent's ranges before
5076          traversing its children, simply overriding whatever you find
5077          there.  But since we don't even decide whether to create a
5078          block until after we've traversed its children, that's hard
5079          to do.  */
5080       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5081     }
5082   local_symbols = new->locals;
5083   using_directives = new->using_directives;
5084 }
5085
5086 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5087    Return 1 if the attributes are present and valid, otherwise, return 0.
5088    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5089
5090 static int
5091 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5092                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5093                     struct partial_symtab *ranges_pst)
5094 {
5095   struct objfile *objfile = cu->objfile;
5096   struct comp_unit_head *cu_header = &cu->header;
5097   bfd *obfd = objfile->obfd;
5098   unsigned int addr_size = cu_header->addr_size;
5099   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5100   /* Base address selection entry.  */
5101   CORE_ADDR base;
5102   int found_base;
5103   unsigned int dummy;
5104   gdb_byte *buffer;
5105   CORE_ADDR marker;
5106   int low_set;
5107   CORE_ADDR low = 0;
5108   CORE_ADDR high = 0;
5109   CORE_ADDR baseaddr;
5110
5111   found_base = cu->base_known;
5112   base = cu->base_address;
5113
5114   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5115   if (offset >= dwarf2_per_objfile->ranges.size)
5116     {
5117       complaint (&symfile_complaints,
5118                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5119                  offset);
5120       return 0;
5121     }
5122   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5123
5124   /* Read in the largest possible address.  */
5125   marker = read_address (obfd, buffer, cu, &dummy);
5126   if ((marker & mask) == mask)
5127     {
5128       /* If we found the largest possible address, then
5129          read the base address.  */
5130       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5131       buffer += 2 * addr_size;
5132       offset += 2 * addr_size;
5133       found_base = 1;
5134     }
5135
5136   low_set = 0;
5137
5138   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5139
5140   while (1)
5141     {
5142       CORE_ADDR range_beginning, range_end;
5143
5144       range_beginning = read_address (obfd, buffer, cu, &dummy);
5145       buffer += addr_size;
5146       range_end = read_address (obfd, buffer, cu, &dummy);
5147       buffer += addr_size;
5148       offset += 2 * addr_size;
5149
5150       /* An end of list marker is a pair of zero addresses.  */
5151       if (range_beginning == 0 && range_end == 0)
5152         /* Found the end of list entry.  */
5153         break;
5154
5155       /* Each base address selection entry is a pair of 2 values.
5156          The first is the largest possible address, the second is
5157          the base address.  Check for a base address here.  */
5158       if ((range_beginning & mask) == mask)
5159         {
5160           /* If we found the largest possible address, then
5161              read the base address.  */
5162           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5163           found_base = 1;
5164           continue;
5165         }
5166
5167       if (!found_base)
5168         {
5169           /* We have no valid base address for the ranges
5170              data.  */
5171           complaint (&symfile_complaints,
5172                      _("Invalid .debug_ranges data (no base address)"));
5173           return 0;
5174         }
5175
5176       range_beginning += base;
5177       range_end += base;
5178
5179       if (ranges_pst != NULL && range_beginning < range_end)
5180         addrmap_set_empty (objfile->psymtabs_addrmap,
5181                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5182                            ranges_pst);
5183
5184       /* FIXME: This is recording everything as a low-high
5185          segment of consecutive addresses.  We should have a
5186          data structure for discontiguous block ranges
5187          instead.  */
5188       if (! low_set)
5189         {
5190           low = range_beginning;
5191           high = range_end;
5192           low_set = 1;
5193         }
5194       else
5195         {
5196           if (range_beginning < low)
5197             low = range_beginning;
5198           if (range_end > high)
5199             high = range_end;
5200         }
5201     }
5202
5203   if (! low_set)
5204     /* If the first entry is an end-of-list marker, the range
5205        describes an empty scope, i.e. no instructions.  */
5206     return 0;
5207
5208   if (low_return)
5209     *low_return = low;
5210   if (high_return)
5211     *high_return = high;
5212   return 1;
5213 }
5214
5215 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5216    are present and valid, otherwise, return 0.  Return -1 if the range is
5217    discontinuous, i.e. derived from DW_AT_ranges information.  */
5218 static int
5219 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5220                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5221                       struct partial_symtab *pst)
5222 {
5223   struct attribute *attr;
5224   CORE_ADDR low = 0;
5225   CORE_ADDR high = 0;
5226   int ret = 0;
5227
5228   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5229   if (attr)
5230     {
5231       high = DW_ADDR (attr);
5232       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5233       if (attr)
5234         low = DW_ADDR (attr);
5235       else
5236         /* Found high w/o low attribute.  */
5237         return 0;
5238
5239       /* Found consecutive range of addresses.  */
5240       ret = 1;
5241     }
5242   else
5243     {
5244       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5245       if (attr != NULL)
5246         {
5247           /* Value of the DW_AT_ranges attribute is the offset in the
5248              .debug_ranges section.  */
5249           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5250             return 0;
5251           /* Found discontinuous range of addresses.  */
5252           ret = -1;
5253         }
5254     }
5255
5256   if (high < low)
5257     return 0;
5258
5259   /* When using the GNU linker, .gnu.linkonce. sections are used to
5260      eliminate duplicate copies of functions and vtables and such.
5261      The linker will arbitrarily choose one and discard the others.
5262      The AT_*_pc values for such functions refer to local labels in
5263      these sections.  If the section from that file was discarded, the
5264      labels are not in the output, so the relocs get a value of 0.
5265      If this is a discarded function, mark the pc bounds as invalid,
5266      so that GDB will ignore it.  */
5267   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5268     return 0;
5269
5270   *lowpc = low;
5271   *highpc = high;
5272   return ret;
5273 }
5274
5275 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5276    its low and high PC addresses.  Do nothing if these addresses could not
5277    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5278    and HIGHPC to the high address if greater than HIGHPC.  */
5279
5280 static void
5281 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5282                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5283                                  struct dwarf2_cu *cu)
5284 {
5285   CORE_ADDR low, high;
5286   struct die_info *child = die->child;
5287
5288   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5289     {
5290       *lowpc = min (*lowpc, low);
5291       *highpc = max (*highpc, high);
5292     }
5293
5294   /* If the language does not allow nested subprograms (either inside
5295      subprograms or lexical blocks), we're done.  */
5296   if (cu->language != language_ada)
5297     return;
5298
5299   /* Check all the children of the given DIE.  If it contains nested
5300      subprograms, then check their pc bounds.  Likewise, we need to
5301      check lexical blocks as well, as they may also contain subprogram
5302      definitions.  */
5303   while (child && child->tag)
5304     {
5305       if (child->tag == DW_TAG_subprogram
5306           || child->tag == DW_TAG_lexical_block)
5307         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5308       child = sibling_die (child);
5309     }
5310 }
5311
5312 /* Get the low and high pc's represented by the scope DIE, and store
5313    them in *LOWPC and *HIGHPC.  If the correct values can't be
5314    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5315
5316 static void
5317 get_scope_pc_bounds (struct die_info *die,
5318                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5319                      struct dwarf2_cu *cu)
5320 {
5321   CORE_ADDR best_low = (CORE_ADDR) -1;
5322   CORE_ADDR best_high = (CORE_ADDR) 0;
5323   CORE_ADDR current_low, current_high;
5324
5325   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5326     {
5327       best_low = current_low;
5328       best_high = current_high;
5329     }
5330   else
5331     {
5332       struct die_info *child = die->child;
5333
5334       while (child && child->tag)
5335         {
5336           switch (child->tag) {
5337           case DW_TAG_subprogram:
5338             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5339             break;
5340           case DW_TAG_namespace:
5341           case DW_TAG_module:
5342             /* FIXME: carlton/2004-01-16: Should we do this for
5343                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5344                that current GCC's always emit the DIEs corresponding
5345                to definitions of methods of classes as children of a
5346                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5347                the DIEs giving the declarations, which could be
5348                anywhere).  But I don't see any reason why the
5349                standards says that they have to be there.  */
5350             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5351
5352             if (current_low != ((CORE_ADDR) -1))
5353               {
5354                 best_low = min (best_low, current_low);
5355                 best_high = max (best_high, current_high);
5356               }
5357             break;
5358           default:
5359             /* Ignore. */
5360             break;
5361           }
5362
5363           child = sibling_die (child);
5364         }
5365     }
5366
5367   *lowpc = best_low;
5368   *highpc = best_high;
5369 }
5370
5371 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5372    in DIE.  */
5373 static void
5374 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5375                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5376 {
5377   struct attribute *attr;
5378
5379   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5380   if (attr)
5381     {
5382       CORE_ADDR high = DW_ADDR (attr);
5383
5384       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5385       if (attr)
5386         {
5387           CORE_ADDR low = DW_ADDR (attr);
5388
5389           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5390         }
5391     }
5392
5393   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5394   if (attr)
5395     {
5396       bfd *obfd = cu->objfile->obfd;
5397
5398       /* The value of the DW_AT_ranges attribute is the offset of the
5399          address range list in the .debug_ranges section.  */
5400       unsigned long offset = DW_UNSND (attr);
5401       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5402
5403       /* For some target architectures, but not others, the
5404          read_address function sign-extends the addresses it returns.
5405          To recognize base address selection entries, we need a
5406          mask.  */
5407       unsigned int addr_size = cu->header.addr_size;
5408       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5409
5410       /* The base address, to which the next pair is relative.  Note
5411          that this 'base' is a DWARF concept: most entries in a range
5412          list are relative, to reduce the number of relocs against the
5413          debugging information.  This is separate from this function's
5414          'baseaddr' argument, which GDB uses to relocate debugging
5415          information from a shared library based on the address at
5416          which the library was loaded.  */
5417       CORE_ADDR base = cu->base_address;
5418       int base_known = cu->base_known;
5419
5420       gdb_assert (dwarf2_per_objfile->ranges.readin);
5421       if (offset >= dwarf2_per_objfile->ranges.size)
5422         {
5423           complaint (&symfile_complaints,
5424                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5425                      offset);
5426           return;
5427         }
5428
5429       for (;;)
5430         {
5431           unsigned int bytes_read;
5432           CORE_ADDR start, end;
5433
5434           start = read_address (obfd, buffer, cu, &bytes_read);
5435           buffer += bytes_read;
5436           end = read_address (obfd, buffer, cu, &bytes_read);
5437           buffer += bytes_read;
5438
5439           /* Did we find the end of the range list?  */
5440           if (start == 0 && end == 0)
5441             break;
5442
5443           /* Did we find a base address selection entry?  */
5444           else if ((start & base_select_mask) == base_select_mask)
5445             {
5446               base = end;
5447               base_known = 1;
5448             }
5449
5450           /* We found an ordinary address range.  */
5451           else
5452             {
5453               if (!base_known)
5454                 {
5455                   complaint (&symfile_complaints,
5456                              _("Invalid .debug_ranges data (no base address)"));
5457                   return;
5458                 }
5459
5460               record_block_range (block,
5461                                   baseaddr + base + start,
5462                                   baseaddr + base + end - 1);
5463             }
5464         }
5465     }
5466 }
5467
5468 /* Add an aggregate field to the field list.  */
5469
5470 static void
5471 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5472                   struct dwarf2_cu *cu)
5473 {
5474   struct objfile *objfile = cu->objfile;
5475   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5476   struct nextfield *new_field;
5477   struct attribute *attr;
5478   struct field *fp;
5479   char *fieldname = "";
5480
5481   /* Allocate a new field list entry and link it in.  */
5482   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5483   make_cleanup (xfree, new_field);
5484   memset (new_field, 0, sizeof (struct nextfield));
5485
5486   if (die->tag == DW_TAG_inheritance)
5487     {
5488       new_field->next = fip->baseclasses;
5489       fip->baseclasses = new_field;
5490     }
5491   else
5492     {
5493       new_field->next = fip->fields;
5494       fip->fields = new_field;
5495     }
5496   fip->nfields++;
5497
5498   /* Handle accessibility and virtuality of field.
5499      The default accessibility for members is public, the default
5500      accessibility for inheritance is private.  */
5501   if (die->tag != DW_TAG_inheritance)
5502     new_field->accessibility = DW_ACCESS_public;
5503   else
5504     new_field->accessibility = DW_ACCESS_private;
5505   new_field->virtuality = DW_VIRTUALITY_none;
5506
5507   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5508   if (attr)
5509     new_field->accessibility = DW_UNSND (attr);
5510   if (new_field->accessibility != DW_ACCESS_public)
5511     fip->non_public_fields = 1;
5512   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5513   if (attr)
5514     new_field->virtuality = DW_UNSND (attr);
5515
5516   fp = &new_field->field;
5517
5518   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
5519     {
5520       /* Data member other than a C++ static data member.  */
5521
5522       /* Get type of field.  */
5523       fp->type = die_type (die, cu);
5524
5525       SET_FIELD_BITPOS (*fp, 0);
5526
5527       /* Get bit size of field (zero if none).  */
5528       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
5529       if (attr)
5530         {
5531           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5532         }
5533       else
5534         {
5535           FIELD_BITSIZE (*fp) = 0;
5536         }
5537
5538       /* Get bit offset of field.  */
5539       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5540       if (attr)
5541         {
5542           int byte_offset = 0;
5543
5544           if (attr_form_is_section_offset (attr))
5545             dwarf2_complex_location_expr_complaint ();
5546           else if (attr_form_is_constant (attr))
5547             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5548           else if (attr_form_is_block (attr))
5549             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5550           else
5551             dwarf2_complex_location_expr_complaint ();
5552
5553           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5554         }
5555       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
5556       if (attr)
5557         {
5558           if (gdbarch_bits_big_endian (gdbarch))
5559             {
5560               /* For big endian bits, the DW_AT_bit_offset gives the
5561                  additional bit offset from the MSB of the containing
5562                  anonymous object to the MSB of the field.  We don't
5563                  have to do anything special since we don't need to
5564                  know the size of the anonymous object.  */
5565               FIELD_BITPOS (*fp) += DW_UNSND (attr);
5566             }
5567           else
5568             {
5569               /* For little endian bits, compute the bit offset to the
5570                  MSB of the anonymous object, subtract off the number of
5571                  bits from the MSB of the field to the MSB of the
5572                  object, and then subtract off the number of bits of
5573                  the field itself.  The result is the bit offset of
5574                  the LSB of the field.  */
5575               int anonymous_size;
5576               int bit_offset = DW_UNSND (attr);
5577
5578               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5579               if (attr)
5580                 {
5581                   /* The size of the anonymous object containing
5582                      the bit field is explicit, so use the
5583                      indicated size (in bytes).  */
5584                   anonymous_size = DW_UNSND (attr);
5585                 }
5586               else
5587                 {
5588                   /* The size of the anonymous object containing
5589                      the bit field must be inferred from the type
5590                      attribute of the data member containing the
5591                      bit field.  */
5592                   anonymous_size = TYPE_LENGTH (fp->type);
5593                 }
5594               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5595                 - bit_offset - FIELD_BITSIZE (*fp);
5596             }
5597         }
5598
5599       /* Get name of field.  */
5600       fieldname = dwarf2_name (die, cu);
5601       if (fieldname == NULL)
5602         fieldname = "";
5603
5604       /* The name is already allocated along with this objfile, so we don't
5605          need to duplicate it for the type.  */
5606       fp->name = fieldname;
5607
5608       /* Change accessibility for artificial fields (e.g. virtual table
5609          pointer or virtual base class pointer) to private.  */
5610       if (dwarf2_attr (die, DW_AT_artificial, cu))
5611         {
5612           FIELD_ARTIFICIAL (*fp) = 1;
5613           new_field->accessibility = DW_ACCESS_private;
5614           fip->non_public_fields = 1;
5615         }
5616     }
5617   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
5618     {
5619       /* C++ static member.  */
5620
5621       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5622          is a declaration, but all versions of G++ as of this writing
5623          (so through at least 3.2.1) incorrectly generate
5624          DW_TAG_variable tags.  */
5625
5626       char *physname;
5627
5628       /* Get name of field.  */
5629       fieldname = dwarf2_name (die, cu);
5630       if (fieldname == NULL)
5631         return;
5632
5633       attr = dwarf2_attr (die, DW_AT_const_value, cu);
5634       if (attr
5635           /* Only create a symbol if this is an external value.
5636              new_symbol checks this and puts the value in the global symbol
5637              table, which we want.  If it is not external, new_symbol
5638              will try to put the value in cu->list_in_scope which is wrong.  */
5639           && dwarf2_flag_true_p (die, DW_AT_external, cu))
5640         {
5641           /* A static const member, not much different than an enum as far as
5642              we're concerned, except that we can support more types.  */
5643           new_symbol (die, NULL, cu);
5644         }
5645
5646       /* Get physical name.  */
5647       physname = (char *) dwarf2_physname (fieldname, die, cu);
5648
5649       /* The name is already allocated along with this objfile, so we don't
5650          need to duplicate it for the type.  */
5651       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5652       FIELD_TYPE (*fp) = die_type (die, cu);
5653       FIELD_NAME (*fp) = fieldname;
5654     }
5655   else if (die->tag == DW_TAG_inheritance)
5656     {
5657       /* C++ base class field.  */
5658       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5659       if (attr)
5660         {
5661           int byte_offset = 0;
5662
5663           if (attr_form_is_section_offset (attr))
5664             dwarf2_complex_location_expr_complaint ();
5665           else if (attr_form_is_constant (attr))
5666             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5667           else if (attr_form_is_block (attr))
5668             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5669           else
5670             dwarf2_complex_location_expr_complaint ();
5671
5672           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5673         }
5674       FIELD_BITSIZE (*fp) = 0;
5675       FIELD_TYPE (*fp) = die_type (die, cu);
5676       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5677       fip->nbaseclasses++;
5678     }
5679 }
5680
5681 /* Add a typedef defined in the scope of the FIP's class.  */
5682
5683 static void
5684 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5685                     struct dwarf2_cu *cu)
5686 {
5687   struct objfile *objfile = cu->objfile;
5688   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5689   struct typedef_field_list *new_field;
5690   struct attribute *attr;
5691   struct typedef_field *fp;
5692   char *fieldname = "";
5693
5694   /* Allocate a new field list entry and link it in.  */
5695   new_field = xzalloc (sizeof (*new_field));
5696   make_cleanup (xfree, new_field);
5697
5698   gdb_assert (die->tag == DW_TAG_typedef);
5699
5700   fp = &new_field->field;
5701
5702   /* Get name of field.  */
5703   fp->name = dwarf2_name (die, cu);
5704   if (fp->name == NULL)
5705     return;
5706
5707   fp->type = read_type_die (die, cu);
5708
5709   new_field->next = fip->typedef_field_list;
5710   fip->typedef_field_list = new_field;
5711   fip->typedef_field_list_count++;
5712 }
5713
5714 /* Create the vector of fields, and attach it to the type.  */
5715
5716 static void
5717 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
5718                               struct dwarf2_cu *cu)
5719 {
5720   int nfields = fip->nfields;
5721
5722   /* Record the field count, allocate space for the array of fields,
5723      and create blank accessibility bitfields if necessary.  */
5724   TYPE_NFIELDS (type) = nfields;
5725   TYPE_FIELDS (type) = (struct field *)
5726     TYPE_ALLOC (type, sizeof (struct field) * nfields);
5727   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5728
5729   if (fip->non_public_fields && cu->language != language_ada)
5730     {
5731       ALLOCATE_CPLUS_STRUCT_TYPE (type);
5732
5733       TYPE_FIELD_PRIVATE_BITS (type) =
5734         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5735       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5736
5737       TYPE_FIELD_PROTECTED_BITS (type) =
5738         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5739       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5740
5741       TYPE_FIELD_IGNORE_BITS (type) =
5742         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5743       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5744     }
5745
5746   /* If the type has baseclasses, allocate and clear a bit vector for
5747      TYPE_FIELD_VIRTUAL_BITS.  */
5748   if (fip->nbaseclasses && cu->language != language_ada)
5749     {
5750       int num_bytes = B_BYTES (fip->nbaseclasses);
5751       unsigned char *pointer;
5752
5753       ALLOCATE_CPLUS_STRUCT_TYPE (type);
5754       pointer = TYPE_ALLOC (type, num_bytes);
5755       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
5756       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5757       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5758     }
5759
5760   /* Copy the saved-up fields into the field vector.  Start from the head
5761      of the list, adding to the tail of the field array, so that they end
5762      up in the same order in the array in which they were added to the list.  */
5763   while (nfields-- > 0)
5764     {
5765       struct nextfield *fieldp;
5766
5767       if (fip->fields)
5768         {
5769           fieldp = fip->fields;
5770           fip->fields = fieldp->next;
5771         }
5772       else
5773         {
5774           fieldp = fip->baseclasses;
5775           fip->baseclasses = fieldp->next;
5776         }
5777
5778       TYPE_FIELD (type, nfields) = fieldp->field;
5779       switch (fieldp->accessibility)
5780         {
5781         case DW_ACCESS_private:
5782           if (cu->language != language_ada)
5783             SET_TYPE_FIELD_PRIVATE (type, nfields);
5784           break;
5785
5786         case DW_ACCESS_protected:
5787           if (cu->language != language_ada)
5788             SET_TYPE_FIELD_PROTECTED (type, nfields);
5789           break;
5790
5791         case DW_ACCESS_public:
5792           break;
5793
5794         default:
5795           /* Unknown accessibility.  Complain and treat it as public.  */
5796           {
5797             complaint (&symfile_complaints, _("unsupported accessibility %d"),
5798                        fieldp->accessibility);
5799           }
5800           break;
5801         }
5802       if (nfields < fip->nbaseclasses)
5803         {
5804           switch (fieldp->virtuality)
5805             {
5806             case DW_VIRTUALITY_virtual:
5807             case DW_VIRTUALITY_pure_virtual:
5808               if (cu->language == language_ada)
5809                 error ("unexpected virtuality in component of Ada type");
5810               SET_TYPE_FIELD_VIRTUAL (type, nfields);
5811               break;
5812             }
5813         }
5814     }
5815 }
5816
5817 /* Add a member function to the proper fieldlist.  */
5818
5819 static void
5820 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
5821                       struct type *type, struct dwarf2_cu *cu)
5822 {
5823   struct objfile *objfile = cu->objfile;
5824   struct attribute *attr;
5825   struct fnfieldlist *flp;
5826   int i;
5827   struct fn_field *fnp;
5828   char *fieldname;
5829   char *physname;
5830   struct nextfnfield *new_fnfield;
5831   struct type *this_type;
5832
5833   if (cu->language == language_ada)
5834     error ("unexpected member function in Ada type");
5835
5836   /* Get name of member function.  */
5837   fieldname = dwarf2_name (die, cu);
5838   if (fieldname == NULL)
5839     return;
5840
5841   /* Get the mangled name.  */
5842   physname = (char *) dwarf2_physname (fieldname, die, cu);
5843
5844   /* Look up member function name in fieldlist.  */
5845   for (i = 0; i < fip->nfnfields; i++)
5846     {
5847       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
5848         break;
5849     }
5850
5851   /* Create new list element if necessary.  */
5852   if (i < fip->nfnfields)
5853     flp = &fip->fnfieldlists[i];
5854   else
5855     {
5856       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
5857         {
5858           fip->fnfieldlists = (struct fnfieldlist *)
5859             xrealloc (fip->fnfieldlists,
5860                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
5861                       * sizeof (struct fnfieldlist));
5862           if (fip->nfnfields == 0)
5863             make_cleanup (free_current_contents, &fip->fnfieldlists);
5864         }
5865       flp = &fip->fnfieldlists[fip->nfnfields];
5866       flp->name = fieldname;
5867       flp->length = 0;
5868       flp->head = NULL;
5869       fip->nfnfields++;
5870     }
5871
5872   /* Create a new member function field and chain it to the field list
5873      entry. */
5874   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
5875   make_cleanup (xfree, new_fnfield);
5876   memset (new_fnfield, 0, sizeof (struct nextfnfield));
5877   new_fnfield->next = flp->head;
5878   flp->head = new_fnfield;
5879   flp->length++;
5880
5881   /* Fill in the member function field info.  */
5882   fnp = &new_fnfield->fnfield;
5883   /* The name is already allocated along with this objfile, so we don't
5884      need to duplicate it for the type.  */
5885   fnp->physname = physname ? physname : "";
5886   fnp->type = alloc_type (objfile);
5887   this_type = read_type_die (die, cu);
5888   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
5889     {
5890       int nparams = TYPE_NFIELDS (this_type);
5891
5892       /* TYPE is the domain of this method, and THIS_TYPE is the type
5893            of the method itself (TYPE_CODE_METHOD).  */
5894       smash_to_method_type (fnp->type, type,
5895                             TYPE_TARGET_TYPE (this_type),
5896                             TYPE_FIELDS (this_type),
5897                             TYPE_NFIELDS (this_type),
5898                             TYPE_VARARGS (this_type));
5899
5900       /* Handle static member functions.
5901          Dwarf2 has no clean way to discern C++ static and non-static
5902          member functions. G++ helps GDB by marking the first
5903          parameter for non-static member functions (which is the
5904          this pointer) as artificial. We obtain this information
5905          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
5906       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
5907         fnp->voffset = VOFFSET_STATIC;
5908     }
5909   else
5910     complaint (&symfile_complaints, _("member function type missing for '%s'"),
5911                physname);
5912
5913   /* Get fcontext from DW_AT_containing_type if present.  */
5914   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
5915     fnp->fcontext = die_containing_type (die, cu);
5916
5917   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
5918      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
5919
5920   /* Get accessibility.  */
5921   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5922   if (attr)
5923     {
5924       switch (DW_UNSND (attr))
5925         {
5926         case DW_ACCESS_private:
5927           fnp->is_private = 1;
5928           break;
5929         case DW_ACCESS_protected:
5930           fnp->is_protected = 1;
5931           break;
5932         }
5933     }
5934
5935   /* Check for artificial methods.  */
5936   attr = dwarf2_attr (die, DW_AT_artificial, cu);
5937   if (attr && DW_UNSND (attr) != 0)
5938     fnp->is_artificial = 1;
5939
5940   /* Get index in virtual function table if it is a virtual member
5941      function.  For older versions of GCC, this is an offset in the
5942      appropriate virtual table, as specified by DW_AT_containing_type.
5943      For everyone else, it is an expression to be evaluated relative
5944      to the object address.  */
5945
5946   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
5947   if (attr)
5948     {
5949       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
5950         {
5951           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
5952             {
5953               /* Old-style GCC.  */
5954               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
5955             }
5956           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
5957                    || (DW_BLOCK (attr)->size > 1
5958                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
5959                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
5960             {
5961               struct dwarf_block blk;
5962               int offset;
5963
5964               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
5965                         ? 1 : 2);
5966               blk.size = DW_BLOCK (attr)->size - offset;
5967               blk.data = DW_BLOCK (attr)->data + offset;
5968               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
5969               if ((fnp->voffset % cu->header.addr_size) != 0)
5970                 dwarf2_complex_location_expr_complaint ();
5971               else
5972                 fnp->voffset /= cu->header.addr_size;
5973               fnp->voffset += 2;
5974             }
5975           else
5976             dwarf2_complex_location_expr_complaint ();
5977
5978           if (!fnp->fcontext)
5979             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
5980         }
5981       else if (attr_form_is_section_offset (attr))
5982         {
5983           dwarf2_complex_location_expr_complaint ();
5984         }
5985       else
5986         {
5987           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
5988                                                  fieldname);
5989         }
5990     }
5991   else
5992     {
5993       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5994       if (attr && DW_UNSND (attr))
5995         {
5996           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
5997           complaint (&symfile_complaints,
5998                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
5999                      fieldname, die->offset);
6000           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6001           TYPE_CPLUS_DYNAMIC (type) = 1;
6002         }
6003     }
6004 }
6005
6006 /* Create the vector of member function fields, and attach it to the type.  */
6007
6008 static void
6009 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6010                                  struct dwarf2_cu *cu)
6011 {
6012   struct fnfieldlist *flp;
6013   int total_length = 0;
6014   int i;
6015
6016   if (cu->language == language_ada)
6017     error ("unexpected member functions in Ada type");
6018
6019   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6020   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6021     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6022
6023   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6024     {
6025       struct nextfnfield *nfp = flp->head;
6026       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6027       int k;
6028
6029       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6030       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6031       fn_flp->fn_fields = (struct fn_field *)
6032         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6033       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6034         fn_flp->fn_fields[k] = nfp->fnfield;
6035
6036       total_length += flp->length;
6037     }
6038
6039   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6040   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6041 }
6042
6043 /* Returns non-zero if NAME is the name of a vtable member in CU's
6044    language, zero otherwise.  */
6045 static int
6046 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6047 {
6048   static const char vptr[] = "_vptr";
6049   static const char vtable[] = "vtable";
6050
6051   /* Look for the C++ and Java forms of the vtable.  */
6052   if ((cu->language == language_java
6053        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6054        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6055        && is_cplus_marker (name[sizeof (vptr) - 1])))
6056     return 1;
6057
6058   return 0;
6059 }
6060
6061 /* GCC outputs unnamed structures that are really pointers to member
6062    functions, with the ABI-specified layout.  If TYPE describes
6063    such a structure, smash it into a member function type.
6064
6065    GCC shouldn't do this; it should just output pointer to member DIEs.
6066    This is GCC PR debug/28767.  */
6067
6068 static void
6069 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6070 {
6071   struct type *pfn_type, *domain_type, *new_type;
6072
6073   /* Check for a structure with no name and two children.  */
6074   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6075     return;
6076
6077   /* Check for __pfn and __delta members.  */
6078   if (TYPE_FIELD_NAME (type, 0) == NULL
6079       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6080       || TYPE_FIELD_NAME (type, 1) == NULL
6081       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6082     return;
6083
6084   /* Find the type of the method.  */
6085   pfn_type = TYPE_FIELD_TYPE (type, 0);
6086   if (pfn_type == NULL
6087       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6088       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6089     return;
6090
6091   /* Look for the "this" argument.  */
6092   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6093   if (TYPE_NFIELDS (pfn_type) == 0
6094       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6095       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6096     return;
6097
6098   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6099   new_type = alloc_type (objfile);
6100   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6101                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6102                         TYPE_VARARGS (pfn_type));
6103   smash_to_methodptr_type (type, new_type);
6104 }
6105
6106 /* Called when we find the DIE that starts a structure or union scope
6107    (definition) to process all dies that define the members of the
6108    structure or union.
6109
6110    NOTE: we need to call struct_type regardless of whether or not the
6111    DIE has an at_name attribute, since it might be an anonymous
6112    structure or union.  This gets the type entered into our set of
6113    user defined types.
6114
6115    However, if the structure is incomplete (an opaque struct/union)
6116    then suppress creating a symbol table entry for it since gdb only
6117    wants to find the one with the complete definition.  Note that if
6118    it is complete, we just call new_symbol, which does it's own
6119    checking about whether the struct/union is anonymous or not (and
6120    suppresses creating a symbol table entry itself).  */
6121
6122 static struct type *
6123 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6124 {
6125   struct objfile *objfile = cu->objfile;
6126   struct type *type;
6127   struct attribute *attr;
6128   char *name;
6129   struct cleanup *back_to;
6130
6131   /* If the definition of this type lives in .debug_types, read that type.
6132      Don't follow DW_AT_specification though, that will take us back up
6133      the chain and we want to go down.  */
6134   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6135   if (attr)
6136     {
6137       struct dwarf2_cu *type_cu = cu;
6138       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6139
6140       /* We could just recurse on read_structure_type, but we need to call
6141          get_die_type to ensure only one type for this DIE is created.
6142          This is important, for example, because for c++ classes we need
6143          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6144       type = read_type_die (type_die, type_cu);
6145       return set_die_type (die, type, cu);
6146     }
6147
6148   back_to = make_cleanup (null_cleanup, 0);
6149
6150   type = alloc_type (objfile);
6151   INIT_CPLUS_SPECIFIC (type);
6152
6153   name = dwarf2_name (die, cu);
6154   if (name != NULL)
6155     {
6156       if (cu->language == language_cplus
6157           || cu->language == language_java)
6158         {
6159           TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6160           if (die->tag == DW_TAG_structure_type
6161               || die->tag == DW_TAG_class_type)
6162             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6163         }
6164       else
6165         {
6166           /* The name is already allocated along with this objfile, so
6167              we don't need to duplicate it for the type.  */
6168           TYPE_TAG_NAME (type) = (char *) name;
6169           if (die->tag == DW_TAG_class_type)
6170             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6171         }
6172     }
6173
6174   if (die->tag == DW_TAG_structure_type)
6175     {
6176       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6177     }
6178   else if (die->tag == DW_TAG_union_type)
6179     {
6180       TYPE_CODE (type) = TYPE_CODE_UNION;
6181     }
6182   else
6183     {
6184       TYPE_CODE (type) = TYPE_CODE_CLASS;
6185     }
6186
6187   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6188     TYPE_DECLARED_CLASS (type) = 1;
6189
6190   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6191   if (attr)
6192     {
6193       TYPE_LENGTH (type) = DW_UNSND (attr);
6194     }
6195   else
6196     {
6197       TYPE_LENGTH (type) = 0;
6198     }
6199
6200   TYPE_STUB_SUPPORTED (type) = 1;
6201   if (die_is_declaration (die, cu))
6202     TYPE_STUB (type) = 1;
6203   else if (attr == NULL && die->child == NULL
6204            && producer_is_realview (cu->producer))
6205     /* RealView does not output the required DW_AT_declaration
6206        on incomplete types.  */
6207     TYPE_STUB (type) = 1;
6208
6209   /* We need to add the type field to the die immediately so we don't
6210      infinitely recurse when dealing with pointers to the structure
6211      type within the structure itself. */
6212   set_die_type (die, type, cu);
6213
6214   /* set_die_type should be already done.  */
6215   set_descriptive_type (type, die, cu);
6216
6217   if (die->child != NULL && ! die_is_declaration (die, cu))
6218     {
6219       struct field_info fi;
6220       struct die_info *child_die;
6221
6222       memset (&fi, 0, sizeof (struct field_info));
6223
6224       child_die = die->child;
6225
6226       while (child_die && child_die->tag)
6227         {
6228           if (child_die->tag == DW_TAG_member
6229               || child_die->tag == DW_TAG_variable)
6230             {
6231               /* NOTE: carlton/2002-11-05: A C++ static data member
6232                  should be a DW_TAG_member that is a declaration, but
6233                  all versions of G++ as of this writing (so through at
6234                  least 3.2.1) incorrectly generate DW_TAG_variable
6235                  tags for them instead.  */
6236               dwarf2_add_field (&fi, child_die, cu);
6237             }
6238           else if (child_die->tag == DW_TAG_subprogram)
6239             {
6240               /* C++ member function. */
6241               dwarf2_add_member_fn (&fi, child_die, type, cu);
6242             }
6243           else if (child_die->tag == DW_TAG_inheritance)
6244             {
6245               /* C++ base class field.  */
6246               dwarf2_add_field (&fi, child_die, cu);
6247             }
6248           else if (child_die->tag == DW_TAG_typedef)
6249             dwarf2_add_typedef (&fi, child_die, cu);
6250           child_die = sibling_die (child_die);
6251         }
6252
6253       /* Attach fields and member functions to the type.  */
6254       if (fi.nfields)
6255         dwarf2_attach_fields_to_type (&fi, type, cu);
6256       if (fi.nfnfields)
6257         {
6258           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6259
6260           /* Get the type which refers to the base class (possibly this
6261              class itself) which contains the vtable pointer for the current
6262              class from the DW_AT_containing_type attribute.  This use of
6263              DW_AT_containing_type is a GNU extension.  */
6264
6265           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6266             {
6267               struct type *t = die_containing_type (die, cu);
6268
6269               TYPE_VPTR_BASETYPE (type) = t;
6270               if (type == t)
6271                 {
6272                   int i;
6273
6274                   /* Our own class provides vtbl ptr.  */
6275                   for (i = TYPE_NFIELDS (t) - 1;
6276                        i >= TYPE_N_BASECLASSES (t);
6277                        --i)
6278                     {
6279                       char *fieldname = TYPE_FIELD_NAME (t, i);
6280
6281                       if (is_vtable_name (fieldname, cu))
6282                         {
6283                           TYPE_VPTR_FIELDNO (type) = i;
6284                           break;
6285                         }
6286                     }
6287
6288                   /* Complain if virtual function table field not found.  */
6289                   if (i < TYPE_N_BASECLASSES (t))
6290                     complaint (&symfile_complaints,
6291                                _("virtual function table pointer not found when defining class '%s'"),
6292                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6293                                "");
6294                 }
6295               else
6296                 {
6297                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6298                 }
6299             }
6300           else if (cu->producer
6301                    && strncmp (cu->producer,
6302                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6303             {
6304               /* The IBM XLC compiler does not provide direct indication
6305                  of the containing type, but the vtable pointer is
6306                  always named __vfp.  */
6307
6308               int i;
6309
6310               for (i = TYPE_NFIELDS (type) - 1;
6311                    i >= TYPE_N_BASECLASSES (type);
6312                    --i)
6313                 {
6314                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6315                     {
6316                       TYPE_VPTR_FIELDNO (type) = i;
6317                       TYPE_VPTR_BASETYPE (type) = type;
6318                       break;
6319                     }
6320                 }
6321             }
6322         }
6323
6324       /* Copy fi.typedef_field_list linked list elements content into the
6325          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6326       if (fi.typedef_field_list)
6327         {
6328           int i = fi.typedef_field_list_count;
6329
6330           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6331           TYPE_TYPEDEF_FIELD_ARRAY (type)
6332             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6333           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6334
6335           /* Reverse the list order to keep the debug info elements order.  */
6336           while (--i >= 0)
6337             {
6338               struct typedef_field *dest, *src;
6339
6340               dest = &TYPE_TYPEDEF_FIELD (type, i);
6341               src = &fi.typedef_field_list->field;
6342               fi.typedef_field_list = fi.typedef_field_list->next;
6343               *dest = *src;
6344             }
6345         }
6346     }
6347
6348   quirk_gcc_member_function_pointer (type, cu->objfile);
6349
6350   do_cleanups (back_to);
6351   return type;
6352 }
6353
6354 static void
6355 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6356 {
6357   struct die_info *child_die = die->child;
6358   struct type *this_type;
6359
6360   this_type = get_die_type (die, cu);
6361   if (this_type == NULL)
6362     this_type = read_structure_type (die, cu);
6363
6364   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6365      snapshots) has been known to create a die giving a declaration
6366      for a class that has, as a child, a die giving a definition for a
6367      nested class.  So we have to process our children even if the
6368      current die is a declaration.  Normally, of course, a declaration
6369      won't have any children at all.  */
6370
6371   while (child_die != NULL && child_die->tag)
6372     {
6373       if (child_die->tag == DW_TAG_member
6374           || child_die->tag == DW_TAG_variable
6375           || child_die->tag == DW_TAG_inheritance)
6376         {
6377           /* Do nothing.  */
6378         }
6379       else
6380         process_die (child_die, cu);
6381
6382       child_die = sibling_die (child_die);
6383     }
6384
6385   /* Do not consider external references.  According to the DWARF standard,
6386      these DIEs are identified by the fact that they have no byte_size
6387      attribute, and a declaration attribute.  */
6388   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6389       || !die_is_declaration (die, cu))
6390     new_symbol (die, this_type, cu);
6391 }
6392
6393 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6394    complete the type's fields yet, or create any symbols.  */
6395
6396 static struct type *
6397 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6398 {
6399   struct objfile *objfile = cu->objfile;
6400   struct type *type;
6401   struct attribute *attr;
6402   const char *name;
6403
6404   /* If the definition of this type lives in .debug_types, read that type.
6405      Don't follow DW_AT_specification though, that will take us back up
6406      the chain and we want to go down.  */
6407   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6408   if (attr)
6409     {
6410       struct dwarf2_cu *type_cu = cu;
6411       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6412
6413       type = read_type_die (type_die, type_cu);
6414       return set_die_type (die, type, cu);
6415     }
6416
6417   type = alloc_type (objfile);
6418
6419   TYPE_CODE (type) = TYPE_CODE_ENUM;
6420   name = dwarf2_full_name (NULL, die, cu);
6421   if (name != NULL)
6422     TYPE_TAG_NAME (type) = (char *) name;
6423
6424   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6425   if (attr)
6426     {
6427       TYPE_LENGTH (type) = DW_UNSND (attr);
6428     }
6429   else
6430     {
6431       TYPE_LENGTH (type) = 0;
6432     }
6433
6434   /* The enumeration DIE can be incomplete.  In Ada, any type can be
6435      declared as private in the package spec, and then defined only
6436      inside the package body.  Such types are known as Taft Amendment
6437      Types.  When another package uses such a type, an incomplete DIE
6438      may be generated by the compiler.  */
6439   if (die_is_declaration (die, cu))
6440     TYPE_STUB (type) = 1;
6441
6442   return set_die_type (die, type, cu);
6443 }
6444
6445 /* Given a pointer to a die which begins an enumeration, process all
6446    the dies that define the members of the enumeration, and create the
6447    symbol for the enumeration type.
6448
6449    NOTE: We reverse the order of the element list.  */
6450
6451 static void
6452 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6453 {
6454   struct die_info *child_die;
6455   struct field *fields;
6456   struct symbol *sym;
6457   int num_fields;
6458   int unsigned_enum = 1;
6459   char *name;
6460   struct type *this_type;
6461
6462   num_fields = 0;
6463   fields = NULL;
6464   this_type = get_die_type (die, cu);
6465   if (this_type == NULL)
6466     this_type = read_enumeration_type (die, cu);
6467   if (die->child != NULL)
6468     {
6469       child_die = die->child;
6470       while (child_die && child_die->tag)
6471         {
6472           if (child_die->tag != DW_TAG_enumerator)
6473             {
6474               process_die (child_die, cu);
6475             }
6476           else
6477             {
6478               name = dwarf2_name (child_die, cu);
6479               if (name)
6480                 {
6481                   sym = new_symbol (child_die, this_type, cu);
6482                   if (SYMBOL_VALUE (sym) < 0)
6483                     unsigned_enum = 0;
6484
6485                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6486                     {
6487                       fields = (struct field *)
6488                         xrealloc (fields,
6489                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
6490                                   * sizeof (struct field));
6491                     }
6492
6493                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
6494                   FIELD_TYPE (fields[num_fields]) = NULL;
6495                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
6496                   FIELD_BITSIZE (fields[num_fields]) = 0;
6497
6498                   num_fields++;
6499                 }
6500             }
6501
6502           child_die = sibling_die (child_die);
6503         }
6504
6505       if (num_fields)
6506         {
6507           TYPE_NFIELDS (this_type) = num_fields;
6508           TYPE_FIELDS (this_type) = (struct field *)
6509             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6510           memcpy (TYPE_FIELDS (this_type), fields,
6511                   sizeof (struct field) * num_fields);
6512           xfree (fields);
6513         }
6514       if (unsigned_enum)
6515         TYPE_UNSIGNED (this_type) = 1;
6516     }
6517
6518   new_symbol (die, this_type, cu);
6519 }
6520
6521 /* Extract all information from a DW_TAG_array_type DIE and put it in
6522    the DIE's type field.  For now, this only handles one dimensional
6523    arrays.  */
6524
6525 static struct type *
6526 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
6527 {
6528   struct objfile *objfile = cu->objfile;
6529   struct die_info *child_die;
6530   struct type *type;
6531   struct type *element_type, *range_type, *index_type;
6532   struct type **range_types = NULL;
6533   struct attribute *attr;
6534   int ndim = 0;
6535   struct cleanup *back_to;
6536   char *name;
6537
6538   element_type = die_type (die, cu);
6539
6540   /* The die_type call above may have already set the type for this DIE.  */
6541   type = get_die_type (die, cu);
6542   if (type)
6543     return type;
6544
6545   /* Irix 6.2 native cc creates array types without children for
6546      arrays with unspecified length.  */
6547   if (die->child == NULL)
6548     {
6549       index_type = objfile_type (objfile)->builtin_int;
6550       range_type = create_range_type (NULL, index_type, 0, -1);
6551       type = create_array_type (NULL, element_type, range_type);
6552       return set_die_type (die, type, cu);
6553     }
6554
6555   back_to = make_cleanup (null_cleanup, NULL);
6556   child_die = die->child;
6557   while (child_die && child_die->tag)
6558     {
6559       if (child_die->tag == DW_TAG_subrange_type)
6560         {
6561           struct type *child_type = read_type_die (child_die, cu);
6562
6563           if (child_type != NULL)
6564             {
6565               /* The range type was succesfully read. Save it for
6566                  the array type creation.  */
6567               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6568                 {
6569                   range_types = (struct type **)
6570                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6571                               * sizeof (struct type *));
6572                   if (ndim == 0)
6573                     make_cleanup (free_current_contents, &range_types);
6574                 }
6575               range_types[ndim++] = child_type;
6576             }
6577         }
6578       child_die = sibling_die (child_die);
6579     }
6580
6581   /* Dwarf2 dimensions are output from left to right, create the
6582      necessary array types in backwards order.  */
6583
6584   type = element_type;
6585
6586   if (read_array_order (die, cu) == DW_ORD_col_major)
6587     {
6588       int i = 0;
6589
6590       while (i < ndim)
6591         type = create_array_type (NULL, type, range_types[i++]);
6592     }
6593   else
6594     {
6595       while (ndim-- > 0)
6596         type = create_array_type (NULL, type, range_types[ndim]);
6597     }
6598
6599   /* Understand Dwarf2 support for vector types (like they occur on
6600      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
6601      array type.  This is not part of the Dwarf2/3 standard yet, but a
6602      custom vendor extension.  The main difference between a regular
6603      array and the vector variant is that vectors are passed by value
6604      to functions.  */
6605   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
6606   if (attr)
6607     make_vector_type (type);
6608
6609   name = dwarf2_name (die, cu);
6610   if (name)
6611     TYPE_NAME (type) = name;
6612
6613   /* Install the type in the die. */
6614   set_die_type (die, type, cu);
6615
6616   /* set_die_type should be already done.  */
6617   set_descriptive_type (type, die, cu);
6618
6619   do_cleanups (back_to);
6620
6621   return type;
6622 }
6623
6624 static enum dwarf_array_dim_ordering
6625 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
6626 {
6627   struct attribute *attr;
6628
6629   attr = dwarf2_attr (die, DW_AT_ordering, cu);
6630
6631   if (attr) return DW_SND (attr);
6632
6633   /*
6634     GNU F77 is a special case, as at 08/2004 array type info is the
6635     opposite order to the dwarf2 specification, but data is still
6636     laid out as per normal fortran.
6637
6638     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
6639     version checking.
6640   */
6641
6642   if (cu->language == language_fortran
6643       && cu->producer && strstr (cu->producer, "GNU F77"))
6644     {
6645       return DW_ORD_row_major;
6646     }
6647
6648   switch (cu->language_defn->la_array_ordering)
6649     {
6650     case array_column_major:
6651       return DW_ORD_col_major;
6652     case array_row_major:
6653     default:
6654       return DW_ORD_row_major;
6655     };
6656 }
6657
6658 /* Extract all information from a DW_TAG_set_type DIE and put it in
6659    the DIE's type field. */
6660
6661 static struct type *
6662 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6663 {
6664   struct type *domain_type, *set_type;
6665   struct attribute *attr;
6666
6667   domain_type = die_type (die, cu);
6668
6669   /* The die_type call above may have already set the type for this DIE.  */
6670   set_type = get_die_type (die, cu);
6671   if (set_type)
6672     return set_type;
6673
6674   set_type = create_set_type (NULL, domain_type);
6675
6676   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6677   if (attr)
6678     TYPE_LENGTH (set_type) = DW_UNSND (attr);
6679
6680   return set_die_type (die, set_type, cu);
6681 }
6682
6683 /* First cut: install each common block member as a global variable.  */
6684
6685 static void
6686 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
6687 {
6688   struct die_info *child_die;
6689   struct attribute *attr;
6690   struct symbol *sym;
6691   CORE_ADDR base = (CORE_ADDR) 0;
6692
6693   attr = dwarf2_attr (die, DW_AT_location, cu);
6694   if (attr)
6695     {
6696       /* Support the .debug_loc offsets */
6697       if (attr_form_is_block (attr))
6698         {
6699           base = decode_locdesc (DW_BLOCK (attr), cu);
6700         }
6701       else if (attr_form_is_section_offset (attr))
6702         {
6703           dwarf2_complex_location_expr_complaint ();
6704         }
6705       else
6706         {
6707           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6708                                                  "common block member");
6709         }
6710     }
6711   if (die->child != NULL)
6712     {
6713       child_die = die->child;
6714       while (child_die && child_die->tag)
6715         {
6716           sym = new_symbol (child_die, NULL, cu);
6717           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
6718           if (attr)
6719             {
6720               CORE_ADDR byte_offset = 0;
6721
6722               if (attr_form_is_section_offset (attr))
6723                 dwarf2_complex_location_expr_complaint ();
6724               else if (attr_form_is_constant (attr))
6725                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6726               else if (attr_form_is_block (attr))
6727                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6728               else
6729                 dwarf2_complex_location_expr_complaint ();
6730
6731               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
6732               add_symbol_to_list (sym, &global_symbols);
6733             }
6734           child_die = sibling_die (child_die);
6735         }
6736     }
6737 }
6738
6739 /* Create a type for a C++ namespace.  */
6740
6741 static struct type *
6742 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
6743 {
6744   struct objfile *objfile = cu->objfile;
6745   const char *previous_prefix, *name;
6746   int is_anonymous;
6747   struct type *type;
6748
6749   /* For extensions, reuse the type of the original namespace.  */
6750   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6751     {
6752       struct die_info *ext_die;
6753       struct dwarf2_cu *ext_cu = cu;
6754
6755       ext_die = dwarf2_extension (die, &ext_cu);
6756       type = read_type_die (ext_die, ext_cu);
6757       return set_die_type (die, type, cu);
6758     }
6759
6760   name = namespace_name (die, &is_anonymous, cu);
6761
6762   /* Now build the name of the current namespace.  */
6763
6764   previous_prefix = determine_prefix (die, cu);
6765   if (previous_prefix[0] != '\0')
6766     name = typename_concat (&objfile->objfile_obstack,
6767                             previous_prefix, name, 0, cu);
6768
6769   /* Create the type.  */
6770   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
6771                     objfile);
6772   TYPE_NAME (type) = (char *) name;
6773   TYPE_TAG_NAME (type) = TYPE_NAME (type);
6774
6775   return set_die_type (die, type, cu);
6776 }
6777
6778 /* Read a C++ namespace.  */
6779
6780 static void
6781 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
6782 {
6783   struct objfile *objfile = cu->objfile;
6784   const char *name;
6785   int is_anonymous;
6786
6787   /* Add a symbol associated to this if we haven't seen the namespace
6788      before.  Also, add a using directive if it's an anonymous
6789      namespace.  */
6790
6791   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
6792     {
6793       struct type *type;
6794
6795       type = read_type_die (die, cu);
6796       new_symbol (die, type, cu);
6797
6798       name = namespace_name (die, &is_anonymous, cu);
6799       if (is_anonymous)
6800         {
6801           const char *previous_prefix = determine_prefix (die, cu);
6802
6803           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
6804                                   NULL, &objfile->objfile_obstack);
6805         }
6806     }
6807
6808   if (die->child != NULL)
6809     {
6810       struct die_info *child_die = die->child;
6811
6812       while (child_die && child_die->tag)
6813         {
6814           process_die (child_die, cu);
6815           child_die = sibling_die (child_die);
6816         }
6817     }
6818 }
6819
6820 /* Read a Fortran module as type.  This DIE can be only a declaration used for
6821    imported module.  Still we need that type as local Fortran "use ... only"
6822    declaration imports depend on the created type in determine_prefix.  */
6823
6824 static struct type *
6825 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
6826 {
6827   struct objfile *objfile = cu->objfile;
6828   char *module_name;
6829   struct type *type;
6830
6831   module_name = dwarf2_name (die, cu);
6832   if (!module_name)
6833     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
6834                die->offset);
6835   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
6836
6837   /* determine_prefix uses TYPE_TAG_NAME.  */
6838   TYPE_TAG_NAME (type) = TYPE_NAME (type);
6839
6840   return set_die_type (die, type, cu);
6841 }
6842
6843 /* Read a Fortran module.  */
6844
6845 static void
6846 read_module (struct die_info *die, struct dwarf2_cu *cu)
6847 {
6848   struct die_info *child_die = die->child;
6849
6850   while (child_die && child_die->tag)
6851     {
6852       process_die (child_die, cu);
6853       child_die = sibling_die (child_die);
6854     }
6855 }
6856
6857 /* Return the name of the namespace represented by DIE.  Set
6858    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
6859    namespace.  */
6860
6861 static const char *
6862 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
6863 {
6864   struct die_info *current_die;
6865   const char *name = NULL;
6866
6867   /* Loop through the extensions until we find a name.  */
6868
6869   for (current_die = die;
6870        current_die != NULL;
6871        current_die = dwarf2_extension (die, &cu))
6872     {
6873       name = dwarf2_name (current_die, cu);
6874       if (name != NULL)
6875         break;
6876     }
6877
6878   /* Is it an anonymous namespace?  */
6879
6880   *is_anonymous = (name == NULL);
6881   if (*is_anonymous)
6882     name = "(anonymous namespace)";
6883
6884   return name;
6885 }
6886
6887 /* Extract all information from a DW_TAG_pointer_type DIE and add to
6888    the user defined type vector.  */
6889
6890 static struct type *
6891 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
6892 {
6893   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
6894   struct comp_unit_head *cu_header = &cu->header;
6895   struct type *type;
6896   struct attribute *attr_byte_size;
6897   struct attribute *attr_address_class;
6898   int byte_size, addr_class;
6899   struct type *target_type;
6900
6901   target_type = die_type (die, cu);
6902
6903   /* The die_type call above may have already set the type for this DIE.  */
6904   type = get_die_type (die, cu);
6905   if (type)
6906     return type;
6907
6908   type = lookup_pointer_type (target_type);
6909
6910   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
6911   if (attr_byte_size)
6912     byte_size = DW_UNSND (attr_byte_size);
6913   else
6914     byte_size = cu_header->addr_size;
6915
6916   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
6917   if (attr_address_class)
6918     addr_class = DW_UNSND (attr_address_class);
6919   else
6920     addr_class = DW_ADDR_none;
6921
6922   /* If the pointer size or address class is different than the
6923      default, create a type variant marked as such and set the
6924      length accordingly.  */
6925   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
6926     {
6927       if (gdbarch_address_class_type_flags_p (gdbarch))
6928         {
6929           int type_flags;
6930
6931           type_flags = gdbarch_address_class_type_flags
6932                          (gdbarch, byte_size, addr_class);
6933           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
6934                       == 0);
6935           type = make_type_with_address_space (type, type_flags);
6936         }
6937       else if (TYPE_LENGTH (type) != byte_size)
6938         {
6939           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
6940         }
6941       else
6942         {
6943           /* Should we also complain about unhandled address classes?  */
6944         }
6945     }
6946
6947   TYPE_LENGTH (type) = byte_size;
6948   return set_die_type (die, type, cu);
6949 }
6950
6951 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
6952    the user defined type vector.  */
6953
6954 static struct type *
6955 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
6956 {
6957   struct type *type;
6958   struct type *to_type;
6959   struct type *domain;
6960
6961   to_type = die_type (die, cu);
6962   domain = die_containing_type (die, cu);
6963
6964   /* The calls above may have already set the type for this DIE.  */
6965   type = get_die_type (die, cu);
6966   if (type)
6967     return type;
6968
6969   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
6970     type = lookup_methodptr_type (to_type);
6971   else
6972     type = lookup_memberptr_type (to_type, domain);
6973
6974   return set_die_type (die, type, cu);
6975 }
6976
6977 /* Extract all information from a DW_TAG_reference_type DIE and add to
6978    the user defined type vector.  */
6979
6980 static struct type *
6981 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
6982 {
6983   struct comp_unit_head *cu_header = &cu->header;
6984   struct type *type, *target_type;
6985   struct attribute *attr;
6986
6987   target_type = die_type (die, cu);
6988
6989   /* The die_type call above may have already set the type for this DIE.  */
6990   type = get_die_type (die, cu);
6991   if (type)
6992     return type;
6993
6994   type = lookup_reference_type (target_type);
6995   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6996   if (attr)
6997     {
6998       TYPE_LENGTH (type) = DW_UNSND (attr);
6999     }
7000   else
7001     {
7002       TYPE_LENGTH (type) = cu_header->addr_size;
7003     }
7004   return set_die_type (die, type, cu);
7005 }
7006
7007 static struct type *
7008 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7009 {
7010   struct type *base_type, *cv_type;
7011
7012   base_type = die_type (die, cu);
7013
7014   /* The die_type call above may have already set the type for this DIE.  */
7015   cv_type = get_die_type (die, cu);
7016   if (cv_type)
7017     return cv_type;
7018
7019   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7020   return set_die_type (die, cv_type, cu);
7021 }
7022
7023 static struct type *
7024 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7025 {
7026   struct type *base_type, *cv_type;
7027
7028   base_type = die_type (die, cu);
7029
7030   /* The die_type call above may have already set the type for this DIE.  */
7031   cv_type = get_die_type (die, cu);
7032   if (cv_type)
7033     return cv_type;
7034
7035   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7036   return set_die_type (die, cv_type, cu);
7037 }
7038
7039 /* Extract all information from a DW_TAG_string_type DIE and add to
7040    the user defined type vector.  It isn't really a user defined type,
7041    but it behaves like one, with other DIE's using an AT_user_def_type
7042    attribute to reference it.  */
7043
7044 static struct type *
7045 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7046 {
7047   struct objfile *objfile = cu->objfile;
7048   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7049   struct type *type, *range_type, *index_type, *char_type;
7050   struct attribute *attr;
7051   unsigned int length;
7052
7053   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7054   if (attr)
7055     {
7056       length = DW_UNSND (attr);
7057     }
7058   else
7059     {
7060       /* check for the DW_AT_byte_size attribute */
7061       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7062       if (attr)
7063         {
7064           length = DW_UNSND (attr);
7065         }
7066       else
7067         {
7068           length = 1;
7069         }
7070     }
7071
7072   index_type = objfile_type (objfile)->builtin_int;
7073   range_type = create_range_type (NULL, index_type, 1, length);
7074   char_type = language_string_char_type (cu->language_defn, gdbarch);
7075   type = create_string_type (NULL, char_type, range_type);
7076
7077   return set_die_type (die, type, cu);
7078 }
7079
7080 /* Handle DIES due to C code like:
7081
7082    struct foo
7083    {
7084    int (*funcp)(int a, long l);
7085    int b;
7086    };
7087
7088    ('funcp' generates a DW_TAG_subroutine_type DIE)
7089  */
7090
7091 static struct type *
7092 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7093 {
7094   struct type *type;            /* Type that this function returns */
7095   struct type *ftype;           /* Function that returns above type */
7096   struct attribute *attr;
7097
7098   type = die_type (die, cu);
7099
7100   /* The die_type call above may have already set the type for this DIE.  */
7101   ftype = get_die_type (die, cu);
7102   if (ftype)
7103     return ftype;
7104
7105   ftype = lookup_function_type (type);
7106
7107   /* All functions in C++, Pascal and Java have prototypes.  */
7108   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7109   if ((attr && (DW_UNSND (attr) != 0))
7110       || cu->language == language_cplus
7111       || cu->language == language_java
7112       || cu->language == language_pascal)
7113     TYPE_PROTOTYPED (ftype) = 1;
7114   else if (producer_is_realview (cu->producer))
7115     /* RealView does not emit DW_AT_prototyped.  We can not
7116        distinguish prototyped and unprototyped functions; default to
7117        prototyped, since that is more common in modern code (and
7118        RealView warns about unprototyped functions).  */
7119     TYPE_PROTOTYPED (ftype) = 1;
7120
7121   /* Store the calling convention in the type if it's available in
7122      the subroutine die.  Otherwise set the calling convention to
7123      the default value DW_CC_normal.  */
7124   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7125   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7126
7127   /* We need to add the subroutine type to the die immediately so
7128      we don't infinitely recurse when dealing with parameters
7129      declared as the same subroutine type. */
7130   set_die_type (die, ftype, cu);
7131
7132   if (die->child != NULL)
7133     {
7134       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7135       struct die_info *child_die;
7136       int nparams, iparams;
7137
7138       /* Count the number of parameters.
7139          FIXME: GDB currently ignores vararg functions, but knows about
7140          vararg member functions.  */
7141       nparams = 0;
7142       child_die = die->child;
7143       while (child_die && child_die->tag)
7144         {
7145           if (child_die->tag == DW_TAG_formal_parameter)
7146             nparams++;
7147           else if (child_die->tag == DW_TAG_unspecified_parameters)
7148             TYPE_VARARGS (ftype) = 1;
7149           child_die = sibling_die (child_die);
7150         }
7151
7152       /* Allocate storage for parameters and fill them in.  */
7153       TYPE_NFIELDS (ftype) = nparams;
7154       TYPE_FIELDS (ftype) = (struct field *)
7155         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7156
7157       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7158          even if we error out during the parameters reading below.  */
7159       for (iparams = 0; iparams < nparams; iparams++)
7160         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7161
7162       iparams = 0;
7163       child_die = die->child;
7164       while (child_die && child_die->tag)
7165         {
7166           if (child_die->tag == DW_TAG_formal_parameter)
7167             {
7168               /* Dwarf2 has no clean way to discern C++ static and non-static
7169                  member functions. G++ helps GDB by marking the first
7170                  parameter for non-static member functions (which is the
7171                  this pointer) as artificial. We pass this information
7172                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
7173               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7174               if (attr)
7175                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7176               else
7177                 {
7178                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7179
7180                   /* GCC/43521: In java, the formal parameter
7181                      "this" is sometimes not marked with DW_AT_artificial.  */
7182                   if (cu->language == language_java)
7183                     {
7184                       const char *name = dwarf2_name (child_die, cu);
7185
7186                       if (name && !strcmp (name, "this"))
7187                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7188                     }
7189                 }
7190               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
7191               iparams++;
7192             }
7193           child_die = sibling_die (child_die);
7194         }
7195     }
7196
7197   return ftype;
7198 }
7199
7200 static struct type *
7201 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7202 {
7203   struct objfile *objfile = cu->objfile;
7204   const char *name = NULL;
7205   struct type *this_type;
7206
7207   name = dwarf2_full_name (NULL, die, cu);
7208   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7209                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7210   TYPE_NAME (this_type) = (char *) name;
7211   set_die_type (die, this_type, cu);
7212   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7213   return this_type;
7214 }
7215
7216 /* Find a representation of a given base type and install
7217    it in the TYPE field of the die.  */
7218
7219 static struct type *
7220 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7221 {
7222   struct objfile *objfile = cu->objfile;
7223   struct type *type;
7224   struct attribute *attr;
7225   int encoding = 0, size = 0;
7226   char *name;
7227   enum type_code code = TYPE_CODE_INT;
7228   int type_flags = 0;
7229   struct type *target_type = NULL;
7230
7231   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7232   if (attr)
7233     {
7234       encoding = DW_UNSND (attr);
7235     }
7236   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7237   if (attr)
7238     {
7239       size = DW_UNSND (attr);
7240     }
7241   name = dwarf2_name (die, cu);
7242   if (!name)
7243     {
7244       complaint (&symfile_complaints,
7245                  _("DW_AT_name missing from DW_TAG_base_type"));
7246     }
7247
7248   switch (encoding)
7249     {
7250       case DW_ATE_address:
7251         /* Turn DW_ATE_address into a void * pointer.  */
7252         code = TYPE_CODE_PTR;
7253         type_flags |= TYPE_FLAG_UNSIGNED;
7254         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7255         break;
7256       case DW_ATE_boolean:
7257         code = TYPE_CODE_BOOL;
7258         type_flags |= TYPE_FLAG_UNSIGNED;
7259         break;
7260       case DW_ATE_complex_float:
7261         code = TYPE_CODE_COMPLEX;
7262         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7263         break;
7264       case DW_ATE_decimal_float:
7265         code = TYPE_CODE_DECFLOAT;
7266         break;
7267       case DW_ATE_float:
7268         code = TYPE_CODE_FLT;
7269         break;
7270       case DW_ATE_signed:
7271         break;
7272       case DW_ATE_unsigned:
7273         type_flags |= TYPE_FLAG_UNSIGNED;
7274         break;
7275       case DW_ATE_signed_char:
7276         if (cu->language == language_ada || cu->language == language_m2
7277             || cu->language == language_pascal)
7278           code = TYPE_CODE_CHAR;
7279         break;
7280       case DW_ATE_unsigned_char:
7281         if (cu->language == language_ada || cu->language == language_m2
7282             || cu->language == language_pascal)
7283           code = TYPE_CODE_CHAR;
7284         type_flags |= TYPE_FLAG_UNSIGNED;
7285         break;
7286       case DW_ATE_UTF:
7287         /* We just treat this as an integer and then recognize the
7288            type by name elsewhere.  */
7289         break;
7290
7291       default:
7292         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7293                    dwarf_type_encoding_name (encoding));
7294         break;
7295     }
7296
7297   type = init_type (code, size, type_flags, NULL, objfile);
7298   TYPE_NAME (type) = name;
7299   TYPE_TARGET_TYPE (type) = target_type;
7300
7301   if (name && strcmp (name, "char") == 0)
7302     TYPE_NOSIGN (type) = 1;
7303
7304   return set_die_type (die, type, cu);
7305 }
7306
7307 /* Read the given DW_AT_subrange DIE.  */
7308
7309 static struct type *
7310 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7311 {
7312   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7313   struct type *base_type;
7314   struct type *range_type;
7315   struct attribute *attr;
7316   LONGEST low = 0;
7317   LONGEST high = -1;
7318   char *name;
7319   LONGEST negative_mask;
7320
7321   base_type = die_type (die, cu);
7322   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7323   check_typedef (base_type);
7324
7325   /* The die_type call above may have already set the type for this DIE.  */
7326   range_type = get_die_type (die, cu);
7327   if (range_type)
7328     return range_type;
7329
7330   if (cu->language == language_fortran)
7331     {
7332       /* FORTRAN implies a lower bound of 1, if not given.  */
7333       low = 1;
7334     }
7335
7336   /* FIXME: For variable sized arrays either of these could be
7337      a variable rather than a constant value.  We'll allow it,
7338      but we don't know how to handle it.  */
7339   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7340   if (attr)
7341     low = dwarf2_get_attr_constant_value (attr, 0);
7342
7343   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7344   if (attr)
7345     {
7346       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7347         {
7348           /* GCC encodes arrays with unspecified or dynamic length
7349              with a DW_FORM_block1 attribute or a reference attribute.
7350              FIXME: GDB does not yet know how to handle dynamic
7351              arrays properly, treat them as arrays with unspecified
7352              length for now.
7353
7354              FIXME: jimb/2003-09-22: GDB does not really know
7355              how to handle arrays of unspecified length
7356              either; we just represent them as zero-length
7357              arrays.  Choose an appropriate upper bound given
7358              the lower bound we've computed above.  */
7359           high = low - 1;
7360         }
7361       else
7362         high = dwarf2_get_attr_constant_value (attr, 1);
7363     }
7364   else
7365     {
7366       attr = dwarf2_attr (die, DW_AT_count, cu);
7367       if (attr)
7368         {
7369           int count = dwarf2_get_attr_constant_value (attr, 1);
7370           high = low + count - 1;
7371         }
7372     }
7373
7374   /* Dwarf-2 specifications explicitly allows to create subrange types
7375      without specifying a base type.
7376      In that case, the base type must be set to the type of
7377      the lower bound, upper bound or count, in that order, if any of these
7378      three attributes references an object that has a type.
7379      If no base type is found, the Dwarf-2 specifications say that
7380      a signed integer type of size equal to the size of an address should
7381      be used.
7382      For the following C code: `extern char gdb_int [];'
7383      GCC produces an empty range DIE.
7384      FIXME: muller/2010-05-28: Possible references to object for low bound,
7385      high bound or count are not yet handled by this code.
7386   */
7387   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7388     {
7389       struct objfile *objfile = cu->objfile;
7390       struct gdbarch *gdbarch = get_objfile_arch (objfile);
7391       int addr_size = gdbarch_addr_bit (gdbarch) /8;
7392       struct type *int_type = objfile_type (objfile)->builtin_int;
7393
7394       /* Test "int", "long int", and "long long int" objfile types,
7395          and select the first one having a size above or equal to the
7396          architecture address size.  */
7397       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7398         base_type = int_type;
7399       else
7400         {
7401           int_type = objfile_type (objfile)->builtin_long;
7402           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7403             base_type = int_type;
7404           else
7405             {
7406               int_type = objfile_type (objfile)->builtin_long_long;
7407               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7408                 base_type = int_type;
7409             }
7410         }
7411     }
7412
7413   negative_mask =
7414     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7415   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7416     low |= negative_mask;
7417   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7418     high |= negative_mask;
7419
7420   range_type = create_range_type (NULL, base_type, low, high);
7421
7422   /* Mark arrays with dynamic length at least as an array of unspecified
7423      length.  GDB could check the boundary but before it gets implemented at
7424      least allow accessing the array elements.  */
7425   if (attr && attr->form == DW_FORM_block1)
7426     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7427
7428   name = dwarf2_name (die, cu);
7429   if (name)
7430     TYPE_NAME (range_type) = name;
7431
7432   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7433   if (attr)
7434     TYPE_LENGTH (range_type) = DW_UNSND (attr);
7435
7436   set_die_type (die, range_type, cu);
7437
7438   /* set_die_type should be already done.  */
7439   set_descriptive_type (range_type, die, cu);
7440
7441   return range_type;
7442 }
7443
7444 static struct type *
7445 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7446 {
7447   struct type *type;
7448
7449   /* For now, we only support the C meaning of an unspecified type: void.  */
7450
7451   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7452   TYPE_NAME (type) = dwarf2_name (die, cu);
7453
7454   return set_die_type (die, type, cu);
7455 }
7456
7457 /* Trivial hash function for die_info: the hash value of a DIE
7458    is its offset in .debug_info for this objfile.  */
7459
7460 static hashval_t
7461 die_hash (const void *item)
7462 {
7463   const struct die_info *die = item;
7464
7465   return die->offset;
7466 }
7467
7468 /* Trivial comparison function for die_info structures: two DIEs
7469    are equal if they have the same offset.  */
7470
7471 static int
7472 die_eq (const void *item_lhs, const void *item_rhs)
7473 {
7474   const struct die_info *die_lhs = item_lhs;
7475   const struct die_info *die_rhs = item_rhs;
7476
7477   return die_lhs->offset == die_rhs->offset;
7478 }
7479
7480 /* Read a whole compilation unit into a linked list of dies.  */
7481
7482 static struct die_info *
7483 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
7484 {
7485   struct die_reader_specs reader_specs;
7486
7487   gdb_assert (cu->die_hash == NULL);
7488   cu->die_hash
7489     = htab_create_alloc_ex (cu->header.length / 12,
7490                             die_hash,
7491                             die_eq,
7492                             NULL,
7493                             &cu->comp_unit_obstack,
7494                             hashtab_obstack_allocate,
7495                             dummy_obstack_deallocate);
7496
7497   init_cu_die_reader (&reader_specs, cu);
7498
7499   return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
7500 }
7501
7502 /* Main entry point for reading a DIE and all children.
7503    Read the DIE and dump it if requested.  */
7504
7505 static struct die_info *
7506 read_die_and_children (const struct die_reader_specs *reader,
7507                        gdb_byte *info_ptr,
7508                        gdb_byte **new_info_ptr,
7509                        struct die_info *parent)
7510 {
7511   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
7512                                                      new_info_ptr, parent);
7513
7514   if (dwarf2_die_debug)
7515     {
7516       fprintf_unfiltered (gdb_stdlog,
7517                           "\nRead die from %s of %s:\n",
7518                           reader->buffer == dwarf2_per_objfile->info.buffer
7519                           ? ".debug_info"
7520                           : reader->buffer == dwarf2_per_objfile->types.buffer
7521                           ? ".debug_types"
7522                           : "unknown section",
7523                           reader->abfd->filename);
7524       dump_die (result, dwarf2_die_debug);
7525     }
7526
7527   return result;
7528 }
7529
7530 /* Read a single die and all its descendents.  Set the die's sibling
7531    field to NULL; set other fields in the die correctly, and set all
7532    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
7533    location of the info_ptr after reading all of those dies.  PARENT
7534    is the parent of the die in question.  */
7535
7536 static struct die_info *
7537 read_die_and_children_1 (const struct die_reader_specs *reader,
7538                          gdb_byte *info_ptr,
7539                          gdb_byte **new_info_ptr,
7540                          struct die_info *parent)
7541 {
7542   struct die_info *die;
7543   gdb_byte *cur_ptr;
7544   int has_children;
7545
7546   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
7547   if (die == NULL)
7548     {
7549       *new_info_ptr = cur_ptr;
7550       return NULL;
7551     }
7552   store_in_ref_table (die, reader->cu);
7553
7554   if (has_children)
7555     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
7556   else
7557     {
7558       die->child = NULL;
7559       *new_info_ptr = cur_ptr;
7560     }
7561
7562   die->sibling = NULL;
7563   die->parent = parent;
7564   return die;
7565 }
7566
7567 /* Read a die, all of its descendents, and all of its siblings; set
7568    all of the fields of all of the dies correctly.  Arguments are as
7569    in read_die_and_children.  */
7570
7571 static struct die_info *
7572 read_die_and_siblings (const struct die_reader_specs *reader,
7573                        gdb_byte *info_ptr,
7574                        gdb_byte **new_info_ptr,
7575                        struct die_info *parent)
7576 {
7577   struct die_info *first_die, *last_sibling;
7578   gdb_byte *cur_ptr;
7579
7580   cur_ptr = info_ptr;
7581   first_die = last_sibling = NULL;
7582
7583   while (1)
7584     {
7585       struct die_info *die
7586         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
7587
7588       if (die == NULL)
7589         {
7590           *new_info_ptr = cur_ptr;
7591           return first_die;
7592         }
7593
7594       if (!first_die)
7595         first_die = die;
7596       else
7597         last_sibling->sibling = die;
7598
7599       last_sibling = die;
7600     }
7601 }
7602
7603 /* Read the die from the .debug_info section buffer.  Set DIEP to
7604    point to a newly allocated die with its information, except for its
7605    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
7606    whether the die has children or not.  */
7607
7608 static gdb_byte *
7609 read_full_die (const struct die_reader_specs *reader,
7610                struct die_info **diep, gdb_byte *info_ptr,
7611                int *has_children)
7612 {
7613   unsigned int abbrev_number, bytes_read, i, offset;
7614   struct abbrev_info *abbrev;
7615   struct die_info *die;
7616   struct dwarf2_cu *cu = reader->cu;
7617   bfd *abfd = reader->abfd;
7618
7619   offset = info_ptr - reader->buffer;
7620   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7621   info_ptr += bytes_read;
7622   if (!abbrev_number)
7623     {
7624       *diep = NULL;
7625       *has_children = 0;
7626       return info_ptr;
7627     }
7628
7629   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7630   if (!abbrev)
7631     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7632            abbrev_number,
7633            bfd_get_filename (abfd));
7634
7635   die = dwarf_alloc_die (cu, abbrev->num_attrs);
7636   die->offset = offset;
7637   die->tag = abbrev->tag;
7638   die->abbrev = abbrev_number;
7639
7640   die->num_attrs = abbrev->num_attrs;
7641
7642   for (i = 0; i < abbrev->num_attrs; ++i)
7643     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7644                                abfd, info_ptr, cu);
7645
7646   *diep = die;
7647   *has_children = abbrev->has_children;
7648   return info_ptr;
7649 }
7650
7651 /* In DWARF version 2, the description of the debugging information is
7652    stored in a separate .debug_abbrev section.  Before we read any
7653    dies from a section we read in all abbreviations and install them
7654    in a hash table.  This function also sets flags in CU describing
7655    the data found in the abbrev table.  */
7656
7657 static void
7658 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
7659 {
7660   struct comp_unit_head *cu_header = &cu->header;
7661   gdb_byte *abbrev_ptr;
7662   struct abbrev_info *cur_abbrev;
7663   unsigned int abbrev_number, bytes_read, abbrev_name;
7664   unsigned int abbrev_form, hash_number;
7665   struct attr_abbrev *cur_attrs;
7666   unsigned int allocated_attrs;
7667
7668   /* Initialize dwarf2 abbrevs */
7669   obstack_init (&cu->abbrev_obstack);
7670   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7671                                       (ABBREV_HASH_SIZE
7672                                        * sizeof (struct abbrev_info *)));
7673   memset (cu->dwarf2_abbrevs, 0,
7674           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
7675
7676   dwarf2_read_section (dwarf2_per_objfile->objfile,
7677                        &dwarf2_per_objfile->abbrev);
7678   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
7679   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7680   abbrev_ptr += bytes_read;
7681
7682   allocated_attrs = ATTR_ALLOC_CHUNK;
7683   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
7684
7685   /* loop until we reach an abbrev number of 0 */
7686   while (abbrev_number)
7687     {
7688       cur_abbrev = dwarf_alloc_abbrev (cu);
7689
7690       /* read in abbrev header */
7691       cur_abbrev->number = abbrev_number;
7692       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7693       abbrev_ptr += bytes_read;
7694       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7695       abbrev_ptr += 1;
7696
7697       if (cur_abbrev->tag == DW_TAG_namespace)
7698         cu->has_namespace_info = 1;
7699
7700       /* now read in declarations */
7701       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7702       abbrev_ptr += bytes_read;
7703       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7704       abbrev_ptr += bytes_read;
7705       while (abbrev_name)
7706         {
7707           if (cur_abbrev->num_attrs == allocated_attrs)
7708             {
7709               allocated_attrs += ATTR_ALLOC_CHUNK;
7710               cur_attrs
7711                 = xrealloc (cur_attrs, (allocated_attrs
7712                                         * sizeof (struct attr_abbrev)));
7713             }
7714
7715           /* Record whether this compilation unit might have
7716              inter-compilation-unit references.  If we don't know what form
7717              this attribute will have, then it might potentially be a
7718              DW_FORM_ref_addr, so we conservatively expect inter-CU
7719              references.  */
7720
7721           if (abbrev_form == DW_FORM_ref_addr
7722               || abbrev_form == DW_FORM_indirect)
7723             cu->has_form_ref_addr = 1;
7724
7725           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7726           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
7727           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7728           abbrev_ptr += bytes_read;
7729           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7730           abbrev_ptr += bytes_read;
7731         }
7732
7733       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7734                                          (cur_abbrev->num_attrs
7735                                           * sizeof (struct attr_abbrev)));
7736       memcpy (cur_abbrev->attrs, cur_attrs,
7737               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7738
7739       hash_number = abbrev_number % ABBREV_HASH_SIZE;
7740       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7741       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
7742
7743       /* Get next abbreviation.
7744          Under Irix6 the abbreviations for a compilation unit are not
7745          always properly terminated with an abbrev number of 0.
7746          Exit loop if we encounter an abbreviation which we have
7747          already read (which means we are about to read the abbreviations
7748          for the next compile unit) or if the end of the abbreviation
7749          table is reached.  */
7750       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7751           >= dwarf2_per_objfile->abbrev.size)
7752         break;
7753       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7754       abbrev_ptr += bytes_read;
7755       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
7756         break;
7757     }
7758
7759   xfree (cur_attrs);
7760 }
7761
7762 /* Release the memory used by the abbrev table for a compilation unit.  */
7763
7764 static void
7765 dwarf2_free_abbrev_table (void *ptr_to_cu)
7766 {
7767   struct dwarf2_cu *cu = ptr_to_cu;
7768
7769   obstack_free (&cu->abbrev_obstack, NULL);
7770   cu->dwarf2_abbrevs = NULL;
7771 }
7772
7773 /* Lookup an abbrev_info structure in the abbrev hash table.  */
7774
7775 static struct abbrev_info *
7776 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
7777 {
7778   unsigned int hash_number;
7779   struct abbrev_info *abbrev;
7780
7781   hash_number = number % ABBREV_HASH_SIZE;
7782   abbrev = cu->dwarf2_abbrevs[hash_number];
7783
7784   while (abbrev)
7785     {
7786       if (abbrev->number == number)
7787         return abbrev;
7788       else
7789         abbrev = abbrev->next;
7790     }
7791   return NULL;
7792 }
7793
7794 /* Returns nonzero if TAG represents a type that we might generate a partial
7795    symbol for.  */
7796
7797 static int
7798 is_type_tag_for_partial (int tag)
7799 {
7800   switch (tag)
7801     {
7802 #if 0
7803     /* Some types that would be reasonable to generate partial symbols for,
7804        that we don't at present.  */
7805     case DW_TAG_array_type:
7806     case DW_TAG_file_type:
7807     case DW_TAG_ptr_to_member_type:
7808     case DW_TAG_set_type:
7809     case DW_TAG_string_type:
7810     case DW_TAG_subroutine_type:
7811 #endif
7812     case DW_TAG_base_type:
7813     case DW_TAG_class_type:
7814     case DW_TAG_interface_type:
7815     case DW_TAG_enumeration_type:
7816     case DW_TAG_structure_type:
7817     case DW_TAG_subrange_type:
7818     case DW_TAG_typedef:
7819     case DW_TAG_union_type:
7820       return 1;
7821     default:
7822       return 0;
7823     }
7824 }
7825
7826 /* Load all DIEs that are interesting for partial symbols into memory.  */
7827
7828 static struct partial_die_info *
7829 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
7830                    int building_psymtab, struct dwarf2_cu *cu)
7831 {
7832   struct partial_die_info *part_die;
7833   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
7834   struct abbrev_info *abbrev;
7835   unsigned int bytes_read;
7836   unsigned int load_all = 0;
7837
7838   int nesting_level = 1;
7839
7840   parent_die = NULL;
7841   last_die = NULL;
7842
7843   if (cu->per_cu && cu->per_cu->load_all_dies)
7844     load_all = 1;
7845
7846   cu->partial_dies
7847     = htab_create_alloc_ex (cu->header.length / 12,
7848                             partial_die_hash,
7849                             partial_die_eq,
7850                             NULL,
7851                             &cu->comp_unit_obstack,
7852                             hashtab_obstack_allocate,
7853                             dummy_obstack_deallocate);
7854
7855   part_die = obstack_alloc (&cu->comp_unit_obstack,
7856                             sizeof (struct partial_die_info));
7857
7858   while (1)
7859     {
7860       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
7861
7862       /* A NULL abbrev means the end of a series of children.  */
7863       if (abbrev == NULL)
7864         {
7865           if (--nesting_level == 0)
7866             {
7867               /* PART_DIE was probably the last thing allocated on the
7868                  comp_unit_obstack, so we could call obstack_free
7869                  here.  We don't do that because the waste is small,
7870                  and will be cleaned up when we're done with this
7871                  compilation unit.  This way, we're also more robust
7872                  against other users of the comp_unit_obstack.  */
7873               return first_die;
7874             }
7875           info_ptr += bytes_read;
7876           last_die = parent_die;
7877           parent_die = parent_die->die_parent;
7878           continue;
7879         }
7880
7881       /* Check whether this DIE is interesting enough to save.  Normally
7882          we would not be interested in members here, but there may be
7883          later variables referencing them via DW_AT_specification (for
7884          static members).  */
7885       if (!load_all
7886           && !is_type_tag_for_partial (abbrev->tag)
7887           && abbrev->tag != DW_TAG_enumerator
7888           && abbrev->tag != DW_TAG_subprogram
7889           && abbrev->tag != DW_TAG_lexical_block
7890           && abbrev->tag != DW_TAG_variable
7891           && abbrev->tag != DW_TAG_namespace
7892           && abbrev->tag != DW_TAG_module
7893           && abbrev->tag != DW_TAG_member)
7894         {
7895           /* Otherwise we skip to the next sibling, if any.  */
7896           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
7897           continue;
7898         }
7899
7900       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
7901                                    buffer, info_ptr, cu);
7902
7903       /* This two-pass algorithm for processing partial symbols has a
7904          high cost in cache pressure.  Thus, handle some simple cases
7905          here which cover the majority of C partial symbols.  DIEs
7906          which neither have specification tags in them, nor could have
7907          specification tags elsewhere pointing at them, can simply be
7908          processed and discarded.
7909
7910          This segment is also optional; scan_partial_symbols and
7911          add_partial_symbol will handle these DIEs if we chain
7912          them in normally.  When compilers which do not emit large
7913          quantities of duplicate debug information are more common,
7914          this code can probably be removed.  */
7915
7916       /* Any complete simple types at the top level (pretty much all
7917          of them, for a language without namespaces), can be processed
7918          directly.  */
7919       if (parent_die == NULL
7920           && part_die->has_specification == 0
7921           && part_die->is_declaration == 0
7922           && (part_die->tag == DW_TAG_typedef
7923               || part_die->tag == DW_TAG_base_type
7924               || part_die->tag == DW_TAG_subrange_type))
7925         {
7926           if (building_psymtab && part_die->name != NULL)
7927             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
7928                                  VAR_DOMAIN, LOC_TYPEDEF,
7929                                  &cu->objfile->static_psymbols,
7930                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
7931           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
7932           continue;
7933         }
7934
7935       /* If we're at the second level, and we're an enumerator, and
7936          our parent has no specification (meaning possibly lives in a
7937          namespace elsewhere), then we can add the partial symbol now
7938          instead of queueing it.  */
7939       if (part_die->tag == DW_TAG_enumerator
7940           && parent_die != NULL
7941           && parent_die->die_parent == NULL
7942           && parent_die->tag == DW_TAG_enumeration_type
7943           && parent_die->has_specification == 0)
7944         {
7945           if (part_die->name == NULL)
7946             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
7947           else if (building_psymtab)
7948             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
7949                                  VAR_DOMAIN, LOC_CONST,
7950                                  (cu->language == language_cplus
7951                                   || cu->language == language_java)
7952                                  ? &cu->objfile->global_psymbols
7953                                  : &cu->objfile->static_psymbols,
7954                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
7955
7956           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
7957           continue;
7958         }
7959
7960       /* We'll save this DIE so link it in.  */
7961       part_die->die_parent = parent_die;
7962       part_die->die_sibling = NULL;
7963       part_die->die_child = NULL;
7964
7965       if (last_die && last_die == parent_die)
7966         last_die->die_child = part_die;
7967       else if (last_die)
7968         last_die->die_sibling = part_die;
7969
7970       last_die = part_die;
7971
7972       if (first_die == NULL)
7973         first_die = part_die;
7974
7975       /* Maybe add the DIE to the hash table.  Not all DIEs that we
7976          find interesting need to be in the hash table, because we
7977          also have the parent/sibling/child chains; only those that we
7978          might refer to by offset later during partial symbol reading.
7979
7980          For now this means things that might have be the target of a
7981          DW_AT_specification, DW_AT_abstract_origin, or
7982          DW_AT_extension.  DW_AT_extension will refer only to
7983          namespaces; DW_AT_abstract_origin refers to functions (and
7984          many things under the function DIE, but we do not recurse
7985          into function DIEs during partial symbol reading) and
7986          possibly variables as well; DW_AT_specification refers to
7987          declarations.  Declarations ought to have the DW_AT_declaration
7988          flag.  It happens that GCC forgets to put it in sometimes, but
7989          only for functions, not for types.
7990
7991          Adding more things than necessary to the hash table is harmless
7992          except for the performance cost.  Adding too few will result in
7993          wasted time in find_partial_die, when we reread the compilation
7994          unit with load_all_dies set.  */
7995
7996       if (load_all
7997           || abbrev->tag == DW_TAG_subprogram
7998           || abbrev->tag == DW_TAG_variable
7999           || abbrev->tag == DW_TAG_namespace
8000           || part_die->is_declaration)
8001         {
8002           void **slot;
8003
8004           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8005                                            part_die->offset, INSERT);
8006           *slot = part_die;
8007         }
8008
8009       part_die = obstack_alloc (&cu->comp_unit_obstack,
8010                                 sizeof (struct partial_die_info));
8011
8012       /* For some DIEs we want to follow their children (if any).  For C
8013          we have no reason to follow the children of structures; for other
8014          languages we have to, both so that we can get at method physnames
8015          to infer fully qualified class names, and for DW_AT_specification.
8016
8017          For Ada, we need to scan the children of subprograms and lexical
8018          blocks as well because Ada allows the definition of nested
8019          entities that could be interesting for the debugger, such as
8020          nested subprograms for instance.  */
8021       if (last_die->has_children
8022           && (load_all
8023               || last_die->tag == DW_TAG_namespace
8024               || last_die->tag == DW_TAG_module
8025               || last_die->tag == DW_TAG_enumeration_type
8026               || (cu->language != language_c
8027                   && (last_die->tag == DW_TAG_class_type
8028                       || last_die->tag == DW_TAG_interface_type
8029                       || last_die->tag == DW_TAG_structure_type
8030                       || last_die->tag == DW_TAG_union_type))
8031               || (cu->language == language_ada
8032                   && (last_die->tag == DW_TAG_subprogram
8033                       || last_die->tag == DW_TAG_lexical_block))))
8034         {
8035           nesting_level++;
8036           parent_die = last_die;
8037           continue;
8038         }
8039
8040       /* Otherwise we skip to the next sibling, if any.  */
8041       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8042
8043       /* Back to the top, do it again.  */
8044     }
8045 }
8046
8047 /* Read a minimal amount of information into the minimal die structure.  */
8048
8049 static gdb_byte *
8050 read_partial_die (struct partial_die_info *part_die,
8051                   struct abbrev_info *abbrev,
8052                   unsigned int abbrev_len, bfd *abfd,
8053                   gdb_byte *buffer, gdb_byte *info_ptr,
8054                   struct dwarf2_cu *cu)
8055 {
8056   unsigned int i;
8057   struct attribute attr;
8058   int has_low_pc_attr = 0;
8059   int has_high_pc_attr = 0;
8060
8061   memset (part_die, 0, sizeof (struct partial_die_info));
8062
8063   part_die->offset = info_ptr - buffer;
8064
8065   info_ptr += abbrev_len;
8066
8067   if (abbrev == NULL)
8068     return info_ptr;
8069
8070   part_die->tag = abbrev->tag;
8071   part_die->has_children = abbrev->has_children;
8072
8073   for (i = 0; i < abbrev->num_attrs; ++i)
8074     {
8075       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8076
8077       /* Store the data if it is of an attribute we want to keep in a
8078          partial symbol table.  */
8079       switch (attr.name)
8080         {
8081         case DW_AT_name:
8082           switch (part_die->tag)
8083             {
8084             case DW_TAG_compile_unit:
8085             case DW_TAG_type_unit:
8086               /* Compilation units have a DW_AT_name that is a filename, not
8087                  a source language identifier.  */
8088             case DW_TAG_enumeration_type:
8089             case DW_TAG_enumerator:
8090               /* These tags always have simple identifiers already; no need
8091                  to canonicalize them.  */
8092               part_die->name = DW_STRING (&attr);
8093               break;
8094             default:
8095               part_die->name
8096                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8097                                             &cu->objfile->objfile_obstack);
8098               break;
8099             }
8100           break;
8101         case DW_AT_linkage_name:
8102         case DW_AT_MIPS_linkage_name:
8103           /* Note that both forms of linkage name might appear.  We
8104              assume they will be the same, and we only store the last
8105              one we see.  */
8106           if (cu->language == language_ada)
8107             part_die->name = DW_STRING (&attr);
8108           break;
8109         case DW_AT_low_pc:
8110           has_low_pc_attr = 1;
8111           part_die->lowpc = DW_ADDR (&attr);
8112           break;
8113         case DW_AT_high_pc:
8114           has_high_pc_attr = 1;
8115           part_die->highpc = DW_ADDR (&attr);
8116           break;
8117         case DW_AT_location:
8118           /* Support the .debug_loc offsets */
8119           if (attr_form_is_block (&attr))
8120             {
8121                part_die->locdesc = DW_BLOCK (&attr);
8122             }
8123           else if (attr_form_is_section_offset (&attr))
8124             {
8125               dwarf2_complex_location_expr_complaint ();
8126             }
8127           else
8128             {
8129               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8130                                                      "partial symbol information");
8131             }
8132           break;
8133         case DW_AT_external:
8134           part_die->is_external = DW_UNSND (&attr);
8135           break;
8136         case DW_AT_declaration:
8137           part_die->is_declaration = DW_UNSND (&attr);
8138           break;
8139         case DW_AT_type:
8140           part_die->has_type = 1;
8141           break;
8142         case DW_AT_abstract_origin:
8143         case DW_AT_specification:
8144         case DW_AT_extension:
8145           part_die->has_specification = 1;
8146           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8147           break;
8148         case DW_AT_sibling:
8149           /* Ignore absolute siblings, they might point outside of
8150              the current compile unit.  */
8151           if (attr.form == DW_FORM_ref_addr)
8152             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8153           else
8154             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8155           break;
8156         case DW_AT_byte_size:
8157           part_die->has_byte_size = 1;
8158           break;
8159         case DW_AT_calling_convention:
8160           /* DWARF doesn't provide a way to identify a program's source-level
8161              entry point.  DW_AT_calling_convention attributes are only meant
8162              to describe functions' calling conventions.
8163
8164              However, because it's a necessary piece of information in
8165              Fortran, and because DW_CC_program is the only piece of debugging
8166              information whose definition refers to a 'main program' at all,
8167              several compilers have begun marking Fortran main programs with
8168              DW_CC_program --- even when those functions use the standard
8169              calling conventions.
8170
8171              So until DWARF specifies a way to provide this information and
8172              compilers pick up the new representation, we'll support this
8173              practice.  */
8174           if (DW_UNSND (&attr) == DW_CC_program
8175               && cu->language == language_fortran)
8176             set_main_name (part_die->name);
8177           break;
8178         default:
8179           break;
8180         }
8181     }
8182
8183   /* When using the GNU linker, .gnu.linkonce. sections are used to
8184      eliminate duplicate copies of functions and vtables and such.
8185      The linker will arbitrarily choose one and discard the others.
8186      The AT_*_pc values for such functions refer to local labels in
8187      these sections.  If the section from that file was discarded, the
8188      labels are not in the output, so the relocs get a value of 0.
8189      If this is a discarded function, mark the pc bounds as invalid,
8190      so that GDB will ignore it.  */
8191   if (has_low_pc_attr && has_high_pc_attr
8192       && part_die->lowpc < part_die->highpc
8193       && (part_die->lowpc != 0
8194           || dwarf2_per_objfile->has_section_at_zero))
8195     part_die->has_pc_info = 1;
8196
8197   return info_ptr;
8198 }
8199
8200 /* Find a cached partial DIE at OFFSET in CU.  */
8201
8202 static struct partial_die_info *
8203 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8204 {
8205   struct partial_die_info *lookup_die = NULL;
8206   struct partial_die_info part_die;
8207
8208   part_die.offset = offset;
8209   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8210
8211   return lookup_die;
8212 }
8213
8214 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8215    except in the case of .debug_types DIEs which do not reference
8216    outside their CU (they do however referencing other types via
8217    DW_FORM_sig8).  */
8218
8219 static struct partial_die_info *
8220 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8221 {
8222   struct dwarf2_per_cu_data *per_cu = NULL;
8223   struct partial_die_info *pd = NULL;
8224
8225   if (cu->per_cu->from_debug_types)
8226     {
8227       pd = find_partial_die_in_comp_unit (offset, cu);
8228       if (pd != NULL)
8229         return pd;
8230       goto not_found;
8231     }
8232
8233   if (offset_in_cu_p (&cu->header, offset))
8234     {
8235       pd = find_partial_die_in_comp_unit (offset, cu);
8236       if (pd != NULL)
8237         return pd;
8238     }
8239
8240   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8241
8242   if (per_cu->cu == NULL)
8243     {
8244       load_partial_comp_unit (per_cu, cu->objfile);
8245       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8246       dwarf2_per_objfile->read_in_chain = per_cu;
8247     }
8248
8249   per_cu->cu->last_used = 0;
8250   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8251
8252   if (pd == NULL && per_cu->load_all_dies == 0)
8253     {
8254       struct cleanup *back_to;
8255       struct partial_die_info comp_unit_die;
8256       struct abbrev_info *abbrev;
8257       unsigned int bytes_read;
8258       char *info_ptr;
8259
8260       per_cu->load_all_dies = 1;
8261
8262       /* Re-read the DIEs.  */
8263       back_to = make_cleanup (null_cleanup, 0);
8264       if (per_cu->cu->dwarf2_abbrevs == NULL)
8265         {
8266           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8267           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8268         }
8269       info_ptr = (dwarf2_per_objfile->info.buffer
8270                   + per_cu->cu->header.offset
8271                   + per_cu->cu->header.first_die_offset);
8272       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8273       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8274                                    per_cu->cu->objfile->obfd,
8275                                    dwarf2_per_objfile->info.buffer, info_ptr,
8276                                    per_cu->cu);
8277       if (comp_unit_die.has_children)
8278         load_partial_dies (per_cu->cu->objfile->obfd,
8279                            dwarf2_per_objfile->info.buffer, info_ptr,
8280                            0, per_cu->cu);
8281       do_cleanups (back_to);
8282
8283       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8284     }
8285
8286  not_found:
8287
8288   if (pd == NULL)
8289     internal_error (__FILE__, __LINE__,
8290                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8291                     offset, bfd_get_filename (cu->objfile->obfd));
8292   return pd;
8293 }
8294
8295 /* Adjust PART_DIE before generating a symbol for it.  This function
8296    may set the is_external flag or change the DIE's name.  */
8297
8298 static void
8299 fixup_partial_die (struct partial_die_info *part_die,
8300                    struct dwarf2_cu *cu)
8301 {
8302   /* If we found a reference attribute and the DIE has no name, try
8303      to find a name in the referred to DIE.  */
8304
8305   if (part_die->name == NULL && part_die->has_specification)
8306     {
8307       struct partial_die_info *spec_die;
8308
8309       spec_die = find_partial_die (part_die->spec_offset, cu);
8310
8311       fixup_partial_die (spec_die, cu);
8312
8313       if (spec_die->name)
8314         {
8315           part_die->name = spec_die->name;
8316
8317           /* Copy DW_AT_external attribute if it is set.  */
8318           if (spec_die->is_external)
8319             part_die->is_external = spec_die->is_external;
8320         }
8321     }
8322
8323   /* Set default names for some unnamed DIEs.  */
8324   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8325                                  || part_die->tag == DW_TAG_class_type))
8326     part_die->name = "(anonymous class)";
8327
8328   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8329     part_die->name = "(anonymous namespace)";
8330
8331   if (part_die->tag == DW_TAG_structure_type
8332       || part_die->tag == DW_TAG_class_type
8333       || part_die->tag == DW_TAG_union_type)
8334     guess_structure_name (part_die, cu);
8335 }
8336
8337 /* Read an attribute value described by an attribute form.  */
8338
8339 static gdb_byte *
8340 read_attribute_value (struct attribute *attr, unsigned form,
8341                       bfd *abfd, gdb_byte *info_ptr,
8342                       struct dwarf2_cu *cu)
8343 {
8344   struct comp_unit_head *cu_header = &cu->header;
8345   unsigned int bytes_read;
8346   struct dwarf_block *blk;
8347
8348   attr->form = form;
8349   switch (form)
8350     {
8351     case DW_FORM_ref_addr:
8352       if (cu->header.version == 2)
8353         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8354       else
8355         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8356       info_ptr += bytes_read;
8357       break;
8358     case DW_FORM_addr:
8359       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8360       info_ptr += bytes_read;
8361       break;
8362     case DW_FORM_block2:
8363       blk = dwarf_alloc_block (cu);
8364       blk->size = read_2_bytes (abfd, info_ptr);
8365       info_ptr += 2;
8366       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8367       info_ptr += blk->size;
8368       DW_BLOCK (attr) = blk;
8369       break;
8370     case DW_FORM_block4:
8371       blk = dwarf_alloc_block (cu);
8372       blk->size = read_4_bytes (abfd, info_ptr);
8373       info_ptr += 4;
8374       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8375       info_ptr += blk->size;
8376       DW_BLOCK (attr) = blk;
8377       break;
8378     case DW_FORM_data2:
8379       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8380       info_ptr += 2;
8381       break;
8382     case DW_FORM_data4:
8383       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8384       info_ptr += 4;
8385       break;
8386     case DW_FORM_data8:
8387       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8388       info_ptr += 8;
8389       break;
8390     case DW_FORM_sec_offset:
8391       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8392       info_ptr += bytes_read;
8393       break;
8394     case DW_FORM_string:
8395       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
8396       DW_STRING_IS_CANONICAL (attr) = 0;
8397       info_ptr += bytes_read;
8398       break;
8399     case DW_FORM_strp:
8400       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8401                                                &bytes_read);
8402       DW_STRING_IS_CANONICAL (attr) = 0;
8403       info_ptr += bytes_read;
8404       break;
8405     case DW_FORM_exprloc:
8406     case DW_FORM_block:
8407       blk = dwarf_alloc_block (cu);
8408       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8409       info_ptr += bytes_read;
8410       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8411       info_ptr += blk->size;
8412       DW_BLOCK (attr) = blk;
8413       break;
8414     case DW_FORM_block1:
8415       blk = dwarf_alloc_block (cu);
8416       blk->size = read_1_byte (abfd, info_ptr);
8417       info_ptr += 1;
8418       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8419       info_ptr += blk->size;
8420       DW_BLOCK (attr) = blk;
8421       break;
8422     case DW_FORM_data1:
8423       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8424       info_ptr += 1;
8425       break;
8426     case DW_FORM_flag:
8427       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8428       info_ptr += 1;
8429       break;
8430     case DW_FORM_flag_present:
8431       DW_UNSND (attr) = 1;
8432       break;
8433     case DW_FORM_sdata:
8434       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8435       info_ptr += bytes_read;
8436       break;
8437     case DW_FORM_udata:
8438       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8439       info_ptr += bytes_read;
8440       break;
8441     case DW_FORM_ref1:
8442       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
8443       info_ptr += 1;
8444       break;
8445     case DW_FORM_ref2:
8446       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
8447       info_ptr += 2;
8448       break;
8449     case DW_FORM_ref4:
8450       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
8451       info_ptr += 4;
8452       break;
8453     case DW_FORM_ref8:
8454       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
8455       info_ptr += 8;
8456       break;
8457     case DW_FORM_sig8:
8458       /* Convert the signature to something we can record in DW_UNSND
8459          for later lookup.
8460          NOTE: This is NULL if the type wasn't found.  */
8461       DW_SIGNATURED_TYPE (attr) =
8462         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8463       info_ptr += 8;
8464       break;
8465     case DW_FORM_ref_udata:
8466       DW_ADDR (attr) = (cu->header.offset
8467                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
8468       info_ptr += bytes_read;
8469       break;
8470     case DW_FORM_indirect:
8471       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8472       info_ptr += bytes_read;
8473       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
8474       break;
8475     default:
8476       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
8477              dwarf_form_name (form),
8478              bfd_get_filename (abfd));
8479     }
8480
8481   /* We have seen instances where the compiler tried to emit a byte
8482      size attribute of -1 which ended up being encoded as an unsigned
8483      0xffffffff.  Although 0xffffffff is technically a valid size value,
8484      an object of this size seems pretty unlikely so we can relatively
8485      safely treat these cases as if the size attribute was invalid and
8486      treat them as zero by default.  */
8487   if (attr->name == DW_AT_byte_size
8488       && form == DW_FORM_data4
8489       && DW_UNSND (attr) >= 0xffffffff)
8490     {
8491       complaint
8492         (&symfile_complaints,
8493          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8494          hex_string (DW_UNSND (attr)));
8495       DW_UNSND (attr) = 0;
8496     }
8497
8498   return info_ptr;
8499 }
8500
8501 /* Read an attribute described by an abbreviated attribute.  */
8502
8503 static gdb_byte *
8504 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
8505                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
8506 {
8507   attr->name = abbrev->name;
8508   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
8509 }
8510
8511 /* read dwarf information from a buffer */
8512
8513 static unsigned int
8514 read_1_byte (bfd *abfd, gdb_byte *buf)
8515 {
8516   return bfd_get_8 (abfd, buf);
8517 }
8518
8519 static int
8520 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
8521 {
8522   return bfd_get_signed_8 (abfd, buf);
8523 }
8524
8525 static unsigned int
8526 read_2_bytes (bfd *abfd, gdb_byte *buf)
8527 {
8528   return bfd_get_16 (abfd, buf);
8529 }
8530
8531 static int
8532 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
8533 {
8534   return bfd_get_signed_16 (abfd, buf);
8535 }
8536
8537 static unsigned int
8538 read_4_bytes (bfd *abfd, gdb_byte *buf)
8539 {
8540   return bfd_get_32 (abfd, buf);
8541 }
8542
8543 static int
8544 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
8545 {
8546   return bfd_get_signed_32 (abfd, buf);
8547 }
8548
8549 static ULONGEST
8550 read_8_bytes (bfd *abfd, gdb_byte *buf)
8551 {
8552   return bfd_get_64 (abfd, buf);
8553 }
8554
8555 static CORE_ADDR
8556 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
8557               unsigned int *bytes_read)
8558 {
8559   struct comp_unit_head *cu_header = &cu->header;
8560   CORE_ADDR retval = 0;
8561
8562   if (cu_header->signed_addr_p)
8563     {
8564       switch (cu_header->addr_size)
8565         {
8566         case 2:
8567           retval = bfd_get_signed_16 (abfd, buf);
8568           break;
8569         case 4:
8570           retval = bfd_get_signed_32 (abfd, buf);
8571           break;
8572         case 8:
8573           retval = bfd_get_signed_64 (abfd, buf);
8574           break;
8575         default:
8576           internal_error (__FILE__, __LINE__,
8577                           _("read_address: bad switch, signed [in module %s]"),
8578                           bfd_get_filename (abfd));
8579         }
8580     }
8581   else
8582     {
8583       switch (cu_header->addr_size)
8584         {
8585         case 2:
8586           retval = bfd_get_16 (abfd, buf);
8587           break;
8588         case 4:
8589           retval = bfd_get_32 (abfd, buf);
8590           break;
8591         case 8:
8592           retval = bfd_get_64 (abfd, buf);
8593           break;
8594         default:
8595           internal_error (__FILE__, __LINE__,
8596                           _("read_address: bad switch, unsigned [in module %s]"),
8597                           bfd_get_filename (abfd));
8598         }
8599     }
8600
8601   *bytes_read = cu_header->addr_size;
8602   return retval;
8603 }
8604
8605 /* Read the initial length from a section.  The (draft) DWARF 3
8606    specification allows the initial length to take up either 4 bytes
8607    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
8608    bytes describe the length and all offsets will be 8 bytes in length
8609    instead of 4.
8610
8611    An older, non-standard 64-bit format is also handled by this
8612    function.  The older format in question stores the initial length
8613    as an 8-byte quantity without an escape value.  Lengths greater
8614    than 2^32 aren't very common which means that the initial 4 bytes
8615    is almost always zero.  Since a length value of zero doesn't make
8616    sense for the 32-bit format, this initial zero can be considered to
8617    be an escape value which indicates the presence of the older 64-bit
8618    format.  As written, the code can't detect (old format) lengths
8619    greater than 4GB.  If it becomes necessary to handle lengths
8620    somewhat larger than 4GB, we could allow other small values (such
8621    as the non-sensical values of 1, 2, and 3) to also be used as
8622    escape values indicating the presence of the old format.
8623
8624    The value returned via bytes_read should be used to increment the
8625    relevant pointer after calling read_initial_length().
8626
8627    [ Note:  read_initial_length() and read_offset() are based on the
8628      document entitled "DWARF Debugging Information Format", revision
8629      3, draft 8, dated November 19, 2001.  This document was obtained
8630      from:
8631
8632         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
8633
8634      This document is only a draft and is subject to change.  (So beware.)
8635
8636      Details regarding the older, non-standard 64-bit format were
8637      determined empirically by examining 64-bit ELF files produced by
8638      the SGI toolchain on an IRIX 6.5 machine.
8639
8640      - Kevin, July 16, 2002
8641    ] */
8642
8643 static LONGEST
8644 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
8645 {
8646   LONGEST length = bfd_get_32 (abfd, buf);
8647
8648   if (length == 0xffffffff)
8649     {
8650       length = bfd_get_64 (abfd, buf + 4);
8651       *bytes_read = 12;
8652     }
8653   else if (length == 0)
8654     {
8655       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
8656       length = bfd_get_64 (abfd, buf);
8657       *bytes_read = 8;
8658     }
8659   else
8660     {
8661       *bytes_read = 4;
8662     }
8663
8664   return length;
8665 }
8666
8667 /* Cover function for read_initial_length.
8668    Returns the length of the object at BUF, and stores the size of the
8669    initial length in *BYTES_READ and stores the size that offsets will be in
8670    *OFFSET_SIZE.
8671    If the initial length size is not equivalent to that specified in
8672    CU_HEADER then issue a complaint.
8673    This is useful when reading non-comp-unit headers.  */
8674
8675 static LONGEST
8676 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8677                                         const struct comp_unit_head *cu_header,
8678                                         unsigned int *bytes_read,
8679                                         unsigned int *offset_size)
8680 {
8681   LONGEST length = read_initial_length (abfd, buf, bytes_read);
8682
8683   gdb_assert (cu_header->initial_length_size == 4
8684               || cu_header->initial_length_size == 8
8685               || cu_header->initial_length_size == 12);
8686
8687   if (cu_header->initial_length_size != *bytes_read)
8688     complaint (&symfile_complaints,
8689                _("intermixed 32-bit and 64-bit DWARF sections"));
8690
8691   *offset_size = (*bytes_read == 4) ? 4 : 8;
8692   return length;
8693 }
8694
8695 /* Read an offset from the data stream.  The size of the offset is
8696    given by cu_header->offset_size.  */
8697
8698 static LONGEST
8699 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
8700              unsigned int *bytes_read)
8701 {
8702   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
8703
8704   *bytes_read = cu_header->offset_size;
8705   return offset;
8706 }
8707
8708 /* Read an offset from the data stream.  */
8709
8710 static LONGEST
8711 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
8712 {
8713   LONGEST retval = 0;
8714
8715   switch (offset_size)
8716     {
8717     case 4:
8718       retval = bfd_get_32 (abfd, buf);
8719       break;
8720     case 8:
8721       retval = bfd_get_64 (abfd, buf);
8722       break;
8723     default:
8724       internal_error (__FILE__, __LINE__,
8725                       _("read_offset_1: bad switch [in module %s]"),
8726                       bfd_get_filename (abfd));
8727     }
8728
8729   return retval;
8730 }
8731
8732 static gdb_byte *
8733 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
8734 {
8735   /* If the size of a host char is 8 bits, we can return a pointer
8736      to the buffer, otherwise we have to copy the data to a buffer
8737      allocated on the temporary obstack.  */
8738   gdb_assert (HOST_CHAR_BIT == 8);
8739   return buf;
8740 }
8741
8742 static char *
8743 read_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8744 {
8745   /* If the size of a host char is 8 bits, we can return a pointer
8746      to the string, otherwise we have to copy the string to a buffer
8747      allocated on the temporary obstack.  */
8748   gdb_assert (HOST_CHAR_BIT == 8);
8749   if (*buf == '\0')
8750     {
8751       *bytes_read_ptr = 1;
8752       return NULL;
8753     }
8754   *bytes_read_ptr = strlen ((char *) buf) + 1;
8755   return (char *) buf;
8756 }
8757
8758 static char *
8759 read_indirect_string (bfd *abfd, gdb_byte *buf,
8760                       const struct comp_unit_head *cu_header,
8761                       unsigned int *bytes_read_ptr)
8762 {
8763   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
8764
8765   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
8766   if (dwarf2_per_objfile->str.buffer == NULL)
8767     {
8768       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
8769                       bfd_get_filename (abfd));
8770       return NULL;
8771     }
8772   if (str_offset >= dwarf2_per_objfile->str.size)
8773     {
8774       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
8775                       bfd_get_filename (abfd));
8776       return NULL;
8777     }
8778   gdb_assert (HOST_CHAR_BIT == 8);
8779   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
8780     return NULL;
8781   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
8782 }
8783
8784 static unsigned long
8785 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8786 {
8787   unsigned long result;
8788   unsigned int num_read;
8789   int i, shift;
8790   unsigned char byte;
8791
8792   result = 0;
8793   shift = 0;
8794   num_read = 0;
8795   i = 0;
8796   while (1)
8797     {
8798       byte = bfd_get_8 (abfd, buf);
8799       buf++;
8800       num_read++;
8801       result |= ((unsigned long)(byte & 127) << shift);
8802       if ((byte & 128) == 0)
8803         {
8804           break;
8805         }
8806       shift += 7;
8807     }
8808   *bytes_read_ptr = num_read;
8809   return result;
8810 }
8811
8812 static long
8813 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8814 {
8815   long result;
8816   int i, shift, num_read;
8817   unsigned char byte;
8818
8819   result = 0;
8820   shift = 0;
8821   num_read = 0;
8822   i = 0;
8823   while (1)
8824     {
8825       byte = bfd_get_8 (abfd, buf);
8826       buf++;
8827       num_read++;
8828       result |= ((long)(byte & 127) << shift);
8829       shift += 7;
8830       if ((byte & 128) == 0)
8831         {
8832           break;
8833         }
8834     }
8835   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
8836     result |= -(((long)1) << shift);
8837   *bytes_read_ptr = num_read;
8838   return result;
8839 }
8840
8841 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
8842
8843 static gdb_byte *
8844 skip_leb128 (bfd *abfd, gdb_byte *buf)
8845 {
8846   int byte;
8847
8848   while (1)
8849     {
8850       byte = bfd_get_8 (abfd, buf);
8851       buf++;
8852       if ((byte & 128) == 0)
8853         return buf;
8854     }
8855 }
8856
8857 static void
8858 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
8859 {
8860   switch (lang)
8861     {
8862     case DW_LANG_C89:
8863     case DW_LANG_C99:
8864     case DW_LANG_C:
8865       cu->language = language_c;
8866       break;
8867     case DW_LANG_C_plus_plus:
8868       cu->language = language_cplus;
8869       break;
8870     case DW_LANG_D:
8871       cu->language = language_d;
8872       break;
8873     case DW_LANG_Fortran77:
8874     case DW_LANG_Fortran90:
8875     case DW_LANG_Fortran95:
8876       cu->language = language_fortran;
8877       break;
8878     case DW_LANG_Mips_Assembler:
8879       cu->language = language_asm;
8880       break;
8881     case DW_LANG_Java:
8882       cu->language = language_java;
8883       break;
8884     case DW_LANG_Ada83:
8885     case DW_LANG_Ada95:
8886       cu->language = language_ada;
8887       break;
8888     case DW_LANG_Modula2:
8889       cu->language = language_m2;
8890       break;
8891     case DW_LANG_Pascal83:
8892       cu->language = language_pascal;
8893       break;
8894     case DW_LANG_ObjC:
8895       cu->language = language_objc;
8896       break;
8897     case DW_LANG_Cobol74:
8898     case DW_LANG_Cobol85:
8899     default:
8900       cu->language = language_minimal;
8901       break;
8902     }
8903   cu->language_defn = language_def (cu->language);
8904 }
8905
8906 /* Return the named attribute or NULL if not there.  */
8907
8908 static struct attribute *
8909 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
8910 {
8911   unsigned int i;
8912   struct attribute *spec = NULL;
8913
8914   for (i = 0; i < die->num_attrs; ++i)
8915     {
8916       if (die->attrs[i].name == name)
8917         return &die->attrs[i];
8918       if (die->attrs[i].name == DW_AT_specification
8919           || die->attrs[i].name == DW_AT_abstract_origin)
8920         spec = &die->attrs[i];
8921     }
8922
8923   if (spec)
8924     {
8925       die = follow_die_ref (die, spec, &cu);
8926       return dwarf2_attr (die, name, cu);
8927     }
8928
8929   return NULL;
8930 }
8931
8932 /* Return the named attribute or NULL if not there,
8933    but do not follow DW_AT_specification, etc.
8934    This is for use in contexts where we're reading .debug_types dies.
8935    Following DW_AT_specification, DW_AT_abstract_origin will take us
8936    back up the chain, and we want to go down.  */
8937
8938 static struct attribute *
8939 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
8940                        struct dwarf2_cu *cu)
8941 {
8942   unsigned int i;
8943
8944   for (i = 0; i < die->num_attrs; ++i)
8945     if (die->attrs[i].name == name)
8946       return &die->attrs[i];
8947
8948   return NULL;
8949 }
8950
8951 /* Return non-zero iff the attribute NAME is defined for the given DIE,
8952    and holds a non-zero value.  This function should only be used for
8953    DW_FORM_flag or DW_FORM_flag_present attributes.  */
8954
8955 static int
8956 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
8957 {
8958   struct attribute *attr = dwarf2_attr (die, name, cu);
8959
8960   return (attr && DW_UNSND (attr));
8961 }
8962
8963 static int
8964 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
8965 {
8966   /* A DIE is a declaration if it has a DW_AT_declaration attribute
8967      which value is non-zero.  However, we have to be careful with
8968      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
8969      (via dwarf2_flag_true_p) follows this attribute.  So we may
8970      end up accidently finding a declaration attribute that belongs
8971      to a different DIE referenced by the specification attribute,
8972      even though the given DIE does not have a declaration attribute.  */
8973   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
8974           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
8975 }
8976
8977 /* Return the die giving the specification for DIE, if there is
8978    one.  *SPEC_CU is the CU containing DIE on input, and the CU
8979    containing the return value on output.  If there is no
8980    specification, but there is an abstract origin, that is
8981    returned.  */
8982
8983 static struct die_info *
8984 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
8985 {
8986   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
8987                                              *spec_cu);
8988
8989   if (spec_attr == NULL)
8990     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
8991
8992   if (spec_attr == NULL)
8993     return NULL;
8994   else
8995     return follow_die_ref (die, spec_attr, spec_cu);
8996 }
8997
8998 /* Free the line_header structure *LH, and any arrays and strings it
8999    refers to.  */
9000 static void
9001 free_line_header (struct line_header *lh)
9002 {
9003   if (lh->standard_opcode_lengths)
9004     xfree (lh->standard_opcode_lengths);
9005
9006   /* Remember that all the lh->file_names[i].name pointers are
9007      pointers into debug_line_buffer, and don't need to be freed.  */
9008   if (lh->file_names)
9009     xfree (lh->file_names);
9010
9011   /* Similarly for the include directory names.  */
9012   if (lh->include_dirs)
9013     xfree (lh->include_dirs);
9014
9015   xfree (lh);
9016 }
9017
9018
9019 /* Add an entry to LH's include directory table.  */
9020 static void
9021 add_include_dir (struct line_header *lh, char *include_dir)
9022 {
9023   /* Grow the array if necessary.  */
9024   if (lh->include_dirs_size == 0)
9025     {
9026       lh->include_dirs_size = 1; /* for testing */
9027       lh->include_dirs = xmalloc (lh->include_dirs_size
9028                                   * sizeof (*lh->include_dirs));
9029     }
9030   else if (lh->num_include_dirs >= lh->include_dirs_size)
9031     {
9032       lh->include_dirs_size *= 2;
9033       lh->include_dirs = xrealloc (lh->include_dirs,
9034                                    (lh->include_dirs_size
9035                                     * sizeof (*lh->include_dirs)));
9036     }
9037
9038   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9039 }
9040
9041
9042 /* Add an entry to LH's file name table.  */
9043 static void
9044 add_file_name (struct line_header *lh,
9045                char *name,
9046                unsigned int dir_index,
9047                unsigned int mod_time,
9048                unsigned int length)
9049 {
9050   struct file_entry *fe;
9051
9052   /* Grow the array if necessary.  */
9053   if (lh->file_names_size == 0)
9054     {
9055       lh->file_names_size = 1; /* for testing */
9056       lh->file_names = xmalloc (lh->file_names_size
9057                                 * sizeof (*lh->file_names));
9058     }
9059   else if (lh->num_file_names >= lh->file_names_size)
9060     {
9061       lh->file_names_size *= 2;
9062       lh->file_names = xrealloc (lh->file_names,
9063                                  (lh->file_names_size
9064                                   * sizeof (*lh->file_names)));
9065     }
9066
9067   fe = &lh->file_names[lh->num_file_names++];
9068   fe->name = name;
9069   fe->dir_index = dir_index;
9070   fe->mod_time = mod_time;
9071   fe->length = length;
9072   fe->included_p = 0;
9073   fe->symtab = NULL;
9074 }
9075
9076
9077 /* Read the statement program header starting at OFFSET in
9078    .debug_line, according to the endianness of ABFD.  Return a pointer
9079    to a struct line_header, allocated using xmalloc.
9080
9081    NOTE: the strings in the include directory and file name tables of
9082    the returned object point into debug_line_buffer, and must not be
9083    freed.  */
9084 static struct line_header *
9085 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9086                           struct dwarf2_cu *cu)
9087 {
9088   struct cleanup *back_to;
9089   struct line_header *lh;
9090   gdb_byte *line_ptr;
9091   unsigned int bytes_read, offset_size;
9092   int i;
9093   char *cur_dir, *cur_file;
9094
9095   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9096   if (dwarf2_per_objfile->line.buffer == NULL)
9097     {
9098       complaint (&symfile_complaints, _("missing .debug_line section"));
9099       return 0;
9100     }
9101
9102   /* Make sure that at least there's room for the total_length field.
9103      That could be 12 bytes long, but we're just going to fudge that.  */
9104   if (offset + 4 >= dwarf2_per_objfile->line.size)
9105     {
9106       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9107       return 0;
9108     }
9109
9110   lh = xmalloc (sizeof (*lh));
9111   memset (lh, 0, sizeof (*lh));
9112   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9113                           (void *) lh);
9114
9115   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9116
9117   /* Read in the header.  */
9118   lh->total_length =
9119     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9120                                             &bytes_read, &offset_size);
9121   line_ptr += bytes_read;
9122   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9123                                      + dwarf2_per_objfile->line.size))
9124     {
9125       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9126       return 0;
9127     }
9128   lh->statement_program_end = line_ptr + lh->total_length;
9129   lh->version = read_2_bytes (abfd, line_ptr);
9130   line_ptr += 2;
9131   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9132   line_ptr += offset_size;
9133   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9134   line_ptr += 1;
9135   if (lh->version >= 4)
9136     {
9137       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9138       line_ptr += 1;
9139     }
9140   else
9141     lh->maximum_ops_per_instruction = 1;
9142
9143   if (lh->maximum_ops_per_instruction == 0)
9144     {
9145       lh->maximum_ops_per_instruction = 1;
9146       complaint (&symfile_complaints,
9147                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9148     }
9149
9150   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9151   line_ptr += 1;
9152   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9153   line_ptr += 1;
9154   lh->line_range = read_1_byte (abfd, line_ptr);
9155   line_ptr += 1;
9156   lh->opcode_base = read_1_byte (abfd, line_ptr);
9157   line_ptr += 1;
9158   lh->standard_opcode_lengths
9159     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9160
9161   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9162   for (i = 1; i < lh->opcode_base; ++i)
9163     {
9164       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9165       line_ptr += 1;
9166     }
9167
9168   /* Read directory table.  */
9169   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9170     {
9171       line_ptr += bytes_read;
9172       add_include_dir (lh, cur_dir);
9173     }
9174   line_ptr += bytes_read;
9175
9176   /* Read file name table.  */
9177   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
9178     {
9179       unsigned int dir_index, mod_time, length;
9180
9181       line_ptr += bytes_read;
9182       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9183       line_ptr += bytes_read;
9184       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9185       line_ptr += bytes_read;
9186       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9187       line_ptr += bytes_read;
9188
9189       add_file_name (lh, cur_file, dir_index, mod_time, length);
9190     }
9191   line_ptr += bytes_read;
9192   lh->statement_program_start = line_ptr;
9193
9194   if (line_ptr > (dwarf2_per_objfile->line.buffer
9195                   + dwarf2_per_objfile->line.size))
9196     complaint (&symfile_complaints,
9197                _("line number info header doesn't fit in `.debug_line' section"));
9198
9199   discard_cleanups (back_to);
9200   return lh;
9201 }
9202
9203 /* This function exists to work around a bug in certain compilers
9204    (particularly GCC 2.95), in which the first line number marker of a
9205    function does not show up until after the prologue, right before
9206    the second line number marker.  This function shifts ADDRESS down
9207    to the beginning of the function if necessary, and is called on
9208    addresses passed to record_line.  */
9209
9210 static CORE_ADDR
9211 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9212 {
9213   struct function_range *fn;
9214
9215   /* Find the function_range containing address.  */
9216   if (!cu->first_fn)
9217     return address;
9218
9219   if (!cu->cached_fn)
9220     cu->cached_fn = cu->first_fn;
9221
9222   fn = cu->cached_fn;
9223   while (fn)
9224     if (fn->lowpc <= address && fn->highpc > address)
9225       goto found;
9226     else
9227       fn = fn->next;
9228
9229   fn = cu->first_fn;
9230   while (fn && fn != cu->cached_fn)
9231     if (fn->lowpc <= address && fn->highpc > address)
9232       goto found;
9233     else
9234       fn = fn->next;
9235
9236   return address;
9237
9238  found:
9239   if (fn->seen_line)
9240     return address;
9241   if (address != fn->lowpc)
9242     complaint (&symfile_complaints,
9243                _("misplaced first line number at 0x%lx for '%s'"),
9244                (unsigned long) address, fn->name);
9245   fn->seen_line = 1;
9246   return fn->lowpc;
9247 }
9248
9249 /* Decode the Line Number Program (LNP) for the given line_header
9250    structure and CU.  The actual information extracted and the type
9251    of structures created from the LNP depends on the value of PST.
9252
9253    1. If PST is NULL, then this procedure uses the data from the program
9254       to create all necessary symbol tables, and their linetables.
9255       The compilation directory of the file is passed in COMP_DIR,
9256       and must not be NULL.
9257
9258    2. If PST is not NULL, this procedure reads the program to determine
9259       the list of files included by the unit represented by PST, and
9260       builds all the associated partial symbol tables.  In this case,
9261       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9262       is not used to compute the full name of the symtab, and therefore
9263       omitting it when building the partial symtab does not introduce
9264       the potential for inconsistency - a partial symtab and its associated
9265       symbtab having a different fullname -).  */
9266
9267 static void
9268 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9269                     struct dwarf2_cu *cu, struct partial_symtab *pst)
9270 {
9271   gdb_byte *line_ptr, *extended_end;
9272   gdb_byte *line_end;
9273   unsigned int bytes_read, extended_len;
9274   unsigned char op_code, extended_op, adj_opcode;
9275   CORE_ADDR baseaddr;
9276   struct objfile *objfile = cu->objfile;
9277   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9278   const int decode_for_pst_p = (pst != NULL);
9279   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9280
9281   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9282
9283   line_ptr = lh->statement_program_start;
9284   line_end = lh->statement_program_end;
9285
9286   /* Read the statement sequences until there's nothing left.  */
9287   while (line_ptr < line_end)
9288     {
9289       /* state machine registers  */
9290       CORE_ADDR address = 0;
9291       unsigned int file = 1;
9292       unsigned int line = 1;
9293       unsigned int column = 0;
9294       int is_stmt = lh->default_is_stmt;
9295       int basic_block = 0;
9296       int end_sequence = 0;
9297       CORE_ADDR addr;
9298       unsigned char op_index = 0;
9299
9300       if (!decode_for_pst_p && lh->num_file_names >= file)
9301         {
9302           /* Start a subfile for the current file of the state machine.  */
9303           /* lh->include_dirs and lh->file_names are 0-based, but the
9304              directory and file name numbers in the statement program
9305              are 1-based.  */
9306           struct file_entry *fe = &lh->file_names[file - 1];
9307           char *dir = NULL;
9308
9309           if (fe->dir_index)
9310             dir = lh->include_dirs[fe->dir_index - 1];
9311
9312           dwarf2_start_subfile (fe->name, dir, comp_dir);
9313         }
9314
9315       /* Decode the table.  */
9316       while (!end_sequence)
9317         {
9318           op_code = read_1_byte (abfd, line_ptr);
9319           line_ptr += 1;
9320           if (line_ptr > line_end)
9321             {
9322               dwarf2_debug_line_missing_end_sequence_complaint ();
9323               break;
9324             }
9325
9326           if (op_code >= lh->opcode_base)
9327             {
9328               /* Special operand.  */
9329               adj_opcode = op_code - lh->opcode_base;
9330               address += (((op_index + (adj_opcode / lh->line_range))
9331                            / lh->maximum_ops_per_instruction)
9332                           * lh->minimum_instruction_length);
9333               op_index = ((op_index + (adj_opcode / lh->line_range))
9334                           % lh->maximum_ops_per_instruction);
9335               line += lh->line_base + (adj_opcode % lh->line_range);
9336               if (lh->num_file_names < file || file == 0)
9337                 dwarf2_debug_line_missing_file_complaint ();
9338               /* For now we ignore lines not starting on an
9339                  instruction boundary.  */
9340               else if (op_index == 0)
9341                 {
9342                   lh->file_names[file - 1].included_p = 1;
9343                   if (!decode_for_pst_p && is_stmt)
9344                     {
9345                       if (last_subfile != current_subfile)
9346                         {
9347                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9348                           if (last_subfile)
9349                             record_line (last_subfile, 0, addr);
9350                           last_subfile = current_subfile;
9351                         }
9352                       /* Append row to matrix using current values.  */
9353                       addr = check_cu_functions (address, cu);
9354                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9355                       record_line (current_subfile, line, addr);
9356                     }
9357                 }
9358               basic_block = 0;
9359             }
9360           else switch (op_code)
9361             {
9362             case DW_LNS_extended_op:
9363               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9364               line_ptr += bytes_read;
9365               extended_end = line_ptr + extended_len;
9366               extended_op = read_1_byte (abfd, line_ptr);
9367               line_ptr += 1;
9368               switch (extended_op)
9369                 {
9370                 case DW_LNE_end_sequence:
9371                   end_sequence = 1;
9372                   break;
9373                 case DW_LNE_set_address:
9374                   address = read_address (abfd, line_ptr, cu, &bytes_read);
9375                   op_index = 0;
9376                   line_ptr += bytes_read;
9377                   address += baseaddr;
9378                   break;
9379                 case DW_LNE_define_file:
9380                   {
9381                     char *cur_file;
9382                     unsigned int dir_index, mod_time, length;
9383
9384                     cur_file = read_string (abfd, line_ptr, &bytes_read);
9385                     line_ptr += bytes_read;
9386                     dir_index =
9387                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9388                     line_ptr += bytes_read;
9389                     mod_time =
9390                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9391                     line_ptr += bytes_read;
9392                     length =
9393                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9394                     line_ptr += bytes_read;
9395                     add_file_name (lh, cur_file, dir_index, mod_time, length);
9396                   }
9397                   break;
9398                 case DW_LNE_set_discriminator:
9399                   /* The discriminator is not interesting to the debugger;
9400                      just ignore it.  */
9401                   line_ptr = extended_end;
9402                   break;
9403                 default:
9404                   complaint (&symfile_complaints,
9405                              _("mangled .debug_line section"));
9406                   return;
9407                 }
9408               /* Make sure that we parsed the extended op correctly.  If e.g.
9409                  we expected a different address size than the producer used,
9410                  we may have read the wrong number of bytes.  */
9411               if (line_ptr != extended_end)
9412                 {
9413                   complaint (&symfile_complaints,
9414                              _("mangled .debug_line section"));
9415                   return;
9416                 }
9417               break;
9418             case DW_LNS_copy:
9419               if (lh->num_file_names < file || file == 0)
9420                 dwarf2_debug_line_missing_file_complaint ();
9421               else
9422                 {
9423                   lh->file_names[file - 1].included_p = 1;
9424                   if (!decode_for_pst_p && is_stmt)
9425                     {
9426                       if (last_subfile != current_subfile)
9427                         {
9428                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9429                           if (last_subfile)
9430                             record_line (last_subfile, 0, addr);
9431                           last_subfile = current_subfile;
9432                         }
9433                       addr = check_cu_functions (address, cu);
9434                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9435                       record_line (current_subfile, line, addr);
9436                     }
9437                 }
9438               basic_block = 0;
9439               break;
9440             case DW_LNS_advance_pc:
9441               {
9442                 CORE_ADDR adjust
9443                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9444
9445                 address += (((op_index + adjust)
9446                              / lh->maximum_ops_per_instruction)
9447                             * lh->minimum_instruction_length);
9448                 op_index = ((op_index + adjust)
9449                             % lh->maximum_ops_per_instruction);
9450                 line_ptr += bytes_read;
9451               }
9452               break;
9453             case DW_LNS_advance_line:
9454               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9455               line_ptr += bytes_read;
9456               break;
9457             case DW_LNS_set_file:
9458               {
9459                 /* The arrays lh->include_dirs and lh->file_names are
9460                    0-based, but the directory and file name numbers in
9461                    the statement program are 1-based.  */
9462                 struct file_entry *fe;
9463                 char *dir = NULL;
9464
9465                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9466                 line_ptr += bytes_read;
9467                 if (lh->num_file_names < file || file == 0)
9468                   dwarf2_debug_line_missing_file_complaint ();
9469                 else
9470                   {
9471                     fe = &lh->file_names[file - 1];
9472                     if (fe->dir_index)
9473                       dir = lh->include_dirs[fe->dir_index - 1];
9474                     if (!decode_for_pst_p)
9475                       {
9476                         last_subfile = current_subfile;
9477                         dwarf2_start_subfile (fe->name, dir, comp_dir);
9478                       }
9479                   }
9480               }
9481               break;
9482             case DW_LNS_set_column:
9483               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9484               line_ptr += bytes_read;
9485               break;
9486             case DW_LNS_negate_stmt:
9487               is_stmt = (!is_stmt);
9488               break;
9489             case DW_LNS_set_basic_block:
9490               basic_block = 1;
9491               break;
9492             /* Add to the address register of the state machine the
9493                address increment value corresponding to special opcode
9494                255.  I.e., this value is scaled by the minimum
9495                instruction length since special opcode 255 would have
9496                scaled the the increment.  */
9497             case DW_LNS_const_add_pc:
9498               {
9499                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9500
9501                 address += (((op_index + adjust)
9502                              / lh->maximum_ops_per_instruction)
9503                             * lh->minimum_instruction_length);
9504                 op_index = ((op_index + adjust)
9505                             % lh->maximum_ops_per_instruction);
9506               }
9507               break;
9508             case DW_LNS_fixed_advance_pc:
9509               address += read_2_bytes (abfd, line_ptr);
9510               op_index = 0;
9511               line_ptr += 2;
9512               break;
9513             default:
9514               {
9515                 /* Unknown standard opcode, ignore it.  */
9516                 int i;
9517
9518                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9519                   {
9520                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9521                     line_ptr += bytes_read;
9522                   }
9523               }
9524             }
9525         }
9526       if (lh->num_file_names < file || file == 0)
9527         dwarf2_debug_line_missing_file_complaint ();
9528       else
9529         {
9530           lh->file_names[file - 1].included_p = 1;
9531           if (!decode_for_pst_p)
9532             {
9533               addr = gdbarch_addr_bits_remove (gdbarch, address);
9534               record_line (current_subfile, 0, addr);
9535             }
9536         }
9537     }
9538
9539   if (decode_for_pst_p)
9540     {
9541       int file_index;
9542
9543       /* Now that we're done scanning the Line Header Program, we can
9544          create the psymtab of each included file.  */
9545       for (file_index = 0; file_index < lh->num_file_names; file_index++)
9546         if (lh->file_names[file_index].included_p == 1)
9547           {
9548             const struct file_entry fe = lh->file_names [file_index];
9549             char *include_name = fe.name;
9550             char *dir_name = NULL;
9551             char *pst_filename = pst->filename;
9552
9553             if (fe.dir_index)
9554               dir_name = lh->include_dirs[fe.dir_index - 1];
9555
9556             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9557               {
9558                 include_name = concat (dir_name, SLASH_STRING,
9559                                        include_name, (char *)NULL);
9560                 make_cleanup (xfree, include_name);
9561               }
9562
9563             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9564               {
9565                 pst_filename = concat (pst->dirname, SLASH_STRING,
9566                                        pst_filename, (char *)NULL);
9567                 make_cleanup (xfree, pst_filename);
9568               }
9569
9570             if (strcmp (include_name, pst_filename) != 0)
9571               dwarf2_create_include_psymtab (include_name, pst, objfile);
9572           }
9573     }
9574   else
9575     {
9576       /* Make sure a symtab is created for every file, even files
9577          which contain only variables (i.e. no code with associated
9578          line numbers).  */
9579
9580       int i;
9581       struct file_entry *fe;
9582
9583       for (i = 0; i < lh->num_file_names; i++)
9584         {
9585           char *dir = NULL;
9586
9587           fe = &lh->file_names[i];
9588           if (fe->dir_index)
9589             dir = lh->include_dirs[fe->dir_index - 1];
9590           dwarf2_start_subfile (fe->name, dir, comp_dir);
9591
9592           /* Skip the main file; we don't need it, and it must be
9593              allocated last, so that it will show up before the
9594              non-primary symtabs in the objfile's symtab list.  */
9595           if (current_subfile == first_subfile)
9596             continue;
9597
9598           if (current_subfile->symtab == NULL)
9599             current_subfile->symtab = allocate_symtab (current_subfile->name,
9600                                                        cu->objfile);
9601           fe->symtab = current_subfile->symtab;
9602         }
9603     }
9604 }
9605
9606 /* Start a subfile for DWARF.  FILENAME is the name of the file and
9607    DIRNAME the name of the source directory which contains FILENAME
9608    or NULL if not known.  COMP_DIR is the compilation directory for the
9609    linetable's compilation unit or NULL if not known.
9610    This routine tries to keep line numbers from identical absolute and
9611    relative file names in a common subfile.
9612
9613    Using the `list' example from the GDB testsuite, which resides in
9614    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9615    of /srcdir/list0.c yields the following debugging information for list0.c:
9616
9617    DW_AT_name:          /srcdir/list0.c
9618    DW_AT_comp_dir:              /compdir
9619    files.files[0].name: list0.h
9620    files.files[0].dir:  /srcdir
9621    files.files[1].name: list0.c
9622    files.files[1].dir:  /srcdir
9623
9624    The line number information for list0.c has to end up in a single
9625    subfile, so that `break /srcdir/list0.c:1' works as expected.
9626    start_subfile will ensure that this happens provided that we pass the
9627    concatenation of files.files[1].dir and files.files[1].name as the
9628    subfile's name.  */
9629
9630 static void
9631 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
9632 {
9633   char *fullname;
9634
9635   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9636      `start_symtab' will always pass the contents of DW_AT_comp_dir as
9637      second argument to start_subfile.  To be consistent, we do the
9638      same here.  In order not to lose the line information directory,
9639      we concatenate it to the filename when it makes sense.
9640      Note that the Dwarf3 standard says (speaking of filenames in line
9641      information): ``The directory index is ignored for file names
9642      that represent full path names''.  Thus ignoring dirname in the
9643      `else' branch below isn't an issue.  */
9644
9645   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
9646     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9647   else
9648     fullname = filename;
9649
9650   start_subfile (fullname, comp_dir);
9651
9652   if (fullname != filename)
9653     xfree (fullname);
9654 }
9655
9656 static void
9657 var_decode_location (struct attribute *attr, struct symbol *sym,
9658                      struct dwarf2_cu *cu)
9659 {
9660   struct objfile *objfile = cu->objfile;
9661   struct comp_unit_head *cu_header = &cu->header;
9662
9663   /* NOTE drow/2003-01-30: There used to be a comment and some special
9664      code here to turn a symbol with DW_AT_external and a
9665      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
9666      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9667      with some versions of binutils) where shared libraries could have
9668      relocations against symbols in their debug information - the
9669      minimal symbol would have the right address, but the debug info
9670      would not.  It's no longer necessary, because we will explicitly
9671      apply relocations when we read in the debug information now.  */
9672
9673   /* A DW_AT_location attribute with no contents indicates that a
9674      variable has been optimized away.  */
9675   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9676     {
9677       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9678       return;
9679     }
9680
9681   /* Handle one degenerate form of location expression specially, to
9682      preserve GDB's previous behavior when section offsets are
9683      specified.  If this is just a DW_OP_addr then mark this symbol
9684      as LOC_STATIC.  */
9685
9686   if (attr_form_is_block (attr)
9687       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9688       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9689     {
9690       unsigned int dummy;
9691
9692       SYMBOL_VALUE_ADDRESS (sym) =
9693         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
9694       SYMBOL_CLASS (sym) = LOC_STATIC;
9695       fixup_symbol_section (sym, objfile);
9696       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9697                                               SYMBOL_SECTION (sym));
9698       return;
9699     }
9700
9701   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9702      expression evaluator, and use LOC_COMPUTED only when necessary
9703      (i.e. when the value of a register or memory location is
9704      referenced, or a thread-local block, etc.).  Then again, it might
9705      not be worthwhile.  I'm assuming that it isn't unless performance
9706      or memory numbers show me otherwise.  */
9707
9708   dwarf2_symbol_mark_computed (attr, sym, cu);
9709   SYMBOL_CLASS (sym) = LOC_COMPUTED;
9710 }
9711
9712 /* Given a pointer to a DWARF information entry, figure out if we need
9713    to make a symbol table entry for it, and if so, create a new entry
9714    and return a pointer to it.
9715    If TYPE is NULL, determine symbol type from the die, otherwise
9716    used the passed type.  */
9717
9718 static struct symbol *
9719 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
9720 {
9721   struct objfile *objfile = cu->objfile;
9722   struct symbol *sym = NULL;
9723   char *name;
9724   struct attribute *attr = NULL;
9725   struct attribute *attr2 = NULL;
9726   CORE_ADDR baseaddr;
9727   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9728
9729   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9730
9731   name = dwarf2_name (die, cu);
9732   if (name)
9733     {
9734       const char *linkagename;
9735
9736       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
9737                                              sizeof (struct symbol));
9738       OBJSTAT (objfile, n_syms++);
9739       memset (sym, 0, sizeof (struct symbol));
9740
9741       /* Cache this symbol's name and the name's demangled form (if any).  */
9742       SYMBOL_LANGUAGE (sym) = cu->language;
9743       linkagename = dwarf2_physname (name, die, cu);
9744       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
9745
9746       /* Fortran does not have mangling standard and the mangling does differ
9747          between gfortran, iFort etc.  */
9748       if (cu->language == language_fortran
9749           && sym->ginfo.language_specific.cplus_specific.demangled_name == NULL)
9750         sym->ginfo.language_specific.cplus_specific.demangled_name
9751           = (char *) dwarf2_full_name (name, die, cu);
9752
9753       /* Default assumptions.
9754          Use the passed type or decode it from the die.  */
9755       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
9756       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9757       if (type != NULL)
9758         SYMBOL_TYPE (sym) = type;
9759       else
9760         SYMBOL_TYPE (sym) = die_type (die, cu);
9761       attr = dwarf2_attr (die,
9762                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
9763                           cu);
9764       if (attr)
9765         {
9766           SYMBOL_LINE (sym) = DW_UNSND (attr);
9767         }
9768
9769       attr = dwarf2_attr (die,
9770                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
9771                           cu);
9772       if (attr)
9773         {
9774           int file_index = DW_UNSND (attr);
9775
9776           if (cu->line_header == NULL
9777               || file_index > cu->line_header->num_file_names)
9778             complaint (&symfile_complaints,
9779                        _("file index out of range"));
9780           else if (file_index > 0)
9781             {
9782               struct file_entry *fe;
9783
9784               fe = &cu->line_header->file_names[file_index - 1];
9785               SYMBOL_SYMTAB (sym) = fe->symtab;
9786             }
9787         }
9788
9789       switch (die->tag)
9790         {
9791         case DW_TAG_label:
9792           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9793           if (attr)
9794             {
9795               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
9796             }
9797           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
9798           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
9799           SYMBOL_CLASS (sym) = LOC_LABEL;
9800           add_symbol_to_list (sym, cu->list_in_scope);
9801           break;
9802         case DW_TAG_subprogram:
9803           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9804              finish_block.  */
9805           SYMBOL_CLASS (sym) = LOC_BLOCK;
9806           attr2 = dwarf2_attr (die, DW_AT_external, cu);
9807           if ((attr2 && (DW_UNSND (attr2) != 0))
9808               || cu->language == language_ada)
9809             {
9810               /* Subprograms marked external are stored as a global symbol.
9811                  Ada subprograms, whether marked external or not, are always
9812                  stored as a global symbol, because we want to be able to
9813                  access them globally.  For instance, we want to be able
9814                  to break on a nested subprogram without having to
9815                  specify the context.  */
9816               add_symbol_to_list (sym, &global_symbols);
9817             }
9818           else
9819             {
9820               add_symbol_to_list (sym, cu->list_in_scope);
9821             }
9822           break;
9823         case DW_TAG_inlined_subroutine:
9824           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9825              finish_block.  */
9826           SYMBOL_CLASS (sym) = LOC_BLOCK;
9827           SYMBOL_INLINED (sym) = 1;
9828           /* Do not add the symbol to any lists.  It will be found via
9829              BLOCK_FUNCTION from the blockvector.  */
9830           break;
9831         case DW_TAG_variable:
9832         case DW_TAG_member:
9833           /* Compilation with minimal debug info may result in variables
9834              with missing type entries. Change the misleading `void' type
9835              to something sensible.  */
9836           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
9837             SYMBOL_TYPE (sym)
9838               = objfile_type (objfile)->nodebug_data_symbol;
9839
9840           attr = dwarf2_attr (die, DW_AT_const_value, cu);
9841           /* In the case of DW_TAG_member, we should only be called for
9842              static const members.  */
9843           if (die->tag == DW_TAG_member)
9844             {
9845               /* dwarf2_add_field uses die_is_declaration,
9846                  so we do the same.  */
9847               gdb_assert (die_is_declaration (die, cu));
9848               gdb_assert (attr);
9849             }
9850           if (attr)
9851             {
9852               dwarf2_const_value (attr, sym, cu);
9853               attr2 = dwarf2_attr (die, DW_AT_external, cu);
9854               if (attr2 && (DW_UNSND (attr2) != 0))
9855                 add_symbol_to_list (sym, &global_symbols);
9856               else
9857                 add_symbol_to_list (sym, cu->list_in_scope);
9858               break;
9859             }
9860           attr = dwarf2_attr (die, DW_AT_location, cu);
9861           if (attr)
9862             {
9863               var_decode_location (attr, sym, cu);
9864               attr2 = dwarf2_attr (die, DW_AT_external, cu);
9865               if (attr2 && (DW_UNSND (attr2) != 0))
9866                 {
9867                   struct pending **list_to_add;
9868
9869                   /* Workaround gfortran PR debug/40040 - it uses
9870                      DW_AT_location for variables in -fPIC libraries which may
9871                      get overriden by other libraries/executable and get
9872                      a different address.  Resolve it by the minimal symbol
9873                      which may come from inferior's executable using copy
9874                      relocation.  Make this workaround only for gfortran as for
9875                      other compilers GDB cannot guess the minimal symbol
9876                      Fortran mangling kind.  */
9877                   if (cu->language == language_fortran && die->parent
9878                       && die->parent->tag == DW_TAG_module
9879                       && cu->producer
9880                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
9881                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
9882
9883                   /* A variable with DW_AT_external is never static,
9884                      but it may be block-scoped.  */
9885                   list_to_add = (cu->list_in_scope == &file_symbols
9886                                  ? &global_symbols : cu->list_in_scope);
9887                   add_symbol_to_list (sym, list_to_add);
9888                 }
9889               else
9890                 add_symbol_to_list (sym, cu->list_in_scope);
9891             }
9892           else
9893             {
9894               /* We do not know the address of this symbol.
9895                  If it is an external symbol and we have type information
9896                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
9897                  The address of the variable will then be determined from
9898                  the minimal symbol table whenever the variable is
9899                  referenced.  */
9900               attr2 = dwarf2_attr (die, DW_AT_external, cu);
9901               if (attr2 && (DW_UNSND (attr2) != 0)
9902                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
9903                 {
9904                   struct pending **list_to_add;
9905
9906                   /* A variable with DW_AT_external is never static, but it
9907                      may be block-scoped.  */
9908                   list_to_add = (cu->list_in_scope == &file_symbols
9909                                  ? &global_symbols : cu->list_in_scope);
9910
9911                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
9912                   add_symbol_to_list (sym, list_to_add);
9913                 }
9914               else if (!die_is_declaration (die, cu))
9915                 {
9916                   /* Use the default LOC_OPTIMIZED_OUT class.  */
9917                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
9918                   add_symbol_to_list (sym, cu->list_in_scope);
9919                 }
9920             }
9921           break;
9922         case DW_TAG_formal_parameter:
9923           /* If we are inside a function, mark this as an argument.  If
9924              not, we might be looking at an argument to an inlined function
9925              when we do not have enough information to show inlined frames;
9926              pretend it's a local variable in that case so that the user can
9927              still see it.  */
9928           if (context_stack_depth > 0
9929               && context_stack[context_stack_depth - 1].name != NULL)
9930             SYMBOL_IS_ARGUMENT (sym) = 1;
9931           attr = dwarf2_attr (die, DW_AT_location, cu);
9932           if (attr)
9933             {
9934               var_decode_location (attr, sym, cu);
9935             }
9936           attr = dwarf2_attr (die, DW_AT_const_value, cu);
9937           if (attr)
9938             {
9939               dwarf2_const_value (attr, sym, cu);
9940             }
9941           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
9942           if (attr && DW_UNSND (attr))
9943             {
9944               struct type *ref_type;
9945
9946               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
9947               SYMBOL_TYPE (sym) = ref_type;
9948             }
9949
9950           add_symbol_to_list (sym, cu->list_in_scope);
9951           break;
9952         case DW_TAG_unspecified_parameters:
9953           /* From varargs functions; gdb doesn't seem to have any
9954              interest in this information, so just ignore it for now.
9955              (FIXME?) */
9956           break;
9957         case DW_TAG_class_type:
9958         case DW_TAG_interface_type:
9959         case DW_TAG_structure_type:
9960         case DW_TAG_union_type:
9961         case DW_TAG_set_type:
9962         case DW_TAG_enumeration_type:
9963           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
9964           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9965
9966           {
9967             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
9968                really ever be static objects: otherwise, if you try
9969                to, say, break of a class's method and you're in a file
9970                which doesn't mention that class, it won't work unless
9971                the check for all static symbols in lookup_symbol_aux
9972                saves you.  See the OtherFileClass tests in
9973                gdb.c++/namespace.exp.  */
9974
9975             struct pending **list_to_add;
9976
9977             list_to_add = (cu->list_in_scope == &file_symbols
9978                            && (cu->language == language_cplus
9979                                || cu->language == language_java)
9980                            ? &global_symbols : cu->list_in_scope);
9981
9982             add_symbol_to_list (sym, list_to_add);
9983
9984             /* The semantics of C++ state that "struct foo { ... }" also
9985                defines a typedef for "foo".  A Java class declaration also
9986                defines a typedef for the class.  */
9987             if (cu->language == language_cplus
9988                 || cu->language == language_java
9989                 || cu->language == language_ada)
9990               {
9991                 /* The symbol's name is already allocated along with
9992                    this objfile, so we don't need to duplicate it for
9993                    the type.  */
9994                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
9995                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
9996               }
9997           }
9998           break;
9999         case DW_TAG_typedef:
10000           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10001           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10002           add_symbol_to_list (sym, cu->list_in_scope);
10003           break;
10004         case DW_TAG_base_type:
10005         case DW_TAG_subrange_type:
10006         case DW_TAG_const_type:
10007         case DW_TAG_volatile_type:
10008           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10009           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10010           add_symbol_to_list (sym, cu->list_in_scope);
10011           break;
10012         case DW_TAG_enumerator:
10013           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10014           if (attr)
10015             {
10016               dwarf2_const_value (attr, sym, cu);
10017             }
10018           {
10019             /* NOTE: carlton/2003-11-10: See comment above in the
10020                DW_TAG_class_type, etc. block.  */
10021
10022             struct pending **list_to_add;
10023
10024             list_to_add = (cu->list_in_scope == &file_symbols
10025                            && (cu->language == language_cplus
10026                                || cu->language == language_java)
10027                            ? &global_symbols : cu->list_in_scope);
10028
10029             add_symbol_to_list (sym, list_to_add);
10030           }
10031           break;
10032         case DW_TAG_namespace:
10033           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10034           add_symbol_to_list (sym, &global_symbols);
10035           break;
10036         default:
10037           /* Not a tag we recognize.  Hopefully we aren't processing
10038              trash data, but since we must specifically ignore things
10039              we don't recognize, there is nothing else we should do at
10040              this point. */
10041           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10042                      dwarf_tag_name (die->tag));
10043           break;
10044         }
10045
10046       /* For the benefit of old versions of GCC, check for anonymous
10047          namespaces based on the demangled name.  */
10048       if (!processing_has_namespace_info
10049           && cu->language == language_cplus)
10050         cp_scan_for_anonymous_namespaces (sym);
10051     }
10052   return (sym);
10053 }
10054
10055 /* Copy constant value from an attribute to a symbol.  */
10056
10057 static void
10058 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10059                     struct dwarf2_cu *cu)
10060 {
10061   struct objfile *objfile = cu->objfile;
10062   struct comp_unit_head *cu_header = &cu->header;
10063   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10064                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10065   struct dwarf_block *blk;
10066
10067   switch (attr->form)
10068     {
10069     case DW_FORM_addr:
10070       {
10071         struct dwarf2_locexpr_baton *baton;
10072         gdb_byte *data;
10073
10074         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10075           dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10076                                                         cu_header->addr_size,
10077                                                         TYPE_LENGTH (SYMBOL_TYPE
10078                                                                      (sym)));
10079         /* Symbols of this form are reasonably rare, so we just
10080            piggyback on the existing location code rather than writing
10081            a new implementation of symbol_computed_ops.  */
10082         baton = obstack_alloc (&objfile->objfile_obstack,
10083                                sizeof (struct dwarf2_locexpr_baton));
10084         baton->per_cu = cu->per_cu;
10085         gdb_assert (baton->per_cu);
10086
10087         baton->size = 2 + cu_header->addr_size;
10088         data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10089         baton->data = data;
10090
10091         data[0] = DW_OP_addr;
10092         store_unsigned_integer (&data[1], cu_header->addr_size,
10093                                 byte_order, DW_ADDR (attr));
10094         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10095
10096         SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10097         SYMBOL_LOCATION_BATON (sym) = baton;
10098         SYMBOL_CLASS (sym) = LOC_COMPUTED;
10099       }
10100       break;
10101     case DW_FORM_string:
10102     case DW_FORM_strp:
10103       /* DW_STRING is already allocated on the obstack, point directly
10104          to it.  */
10105       SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10106       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10107       break;
10108     case DW_FORM_block1:
10109     case DW_FORM_block2:
10110     case DW_FORM_block4:
10111     case DW_FORM_block:
10112     case DW_FORM_exprloc:
10113       blk = DW_BLOCK (attr);
10114       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
10115         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10116                                                       blk->size,
10117                                                       TYPE_LENGTH (SYMBOL_TYPE
10118                                                                    (sym)));
10119       SYMBOL_VALUE_BYTES (sym) =
10120         obstack_alloc (&objfile->objfile_obstack, blk->size);
10121       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10122       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10123       break;
10124
10125       /* The DW_AT_const_value attributes are supposed to carry the
10126          symbol's value "represented as it would be on the target
10127          architecture."  By the time we get here, it's already been
10128          converted to host endianness, so we just need to sign- or
10129          zero-extend it as appropriate.  */
10130     case DW_FORM_data1:
10131       dwarf2_const_value_data (attr, sym, 8);
10132       break;
10133     case DW_FORM_data2:
10134       dwarf2_const_value_data (attr, sym, 16);
10135       break;
10136     case DW_FORM_data4:
10137       dwarf2_const_value_data (attr, sym, 32);
10138       break;
10139     case DW_FORM_data8:
10140       dwarf2_const_value_data (attr, sym, 64);
10141       break;
10142
10143     case DW_FORM_sdata:
10144       SYMBOL_VALUE (sym) = DW_SND (attr);
10145       SYMBOL_CLASS (sym) = LOC_CONST;
10146       break;
10147
10148     case DW_FORM_udata:
10149       SYMBOL_VALUE (sym) = DW_UNSND (attr);
10150       SYMBOL_CLASS (sym) = LOC_CONST;
10151       break;
10152
10153     default:
10154       complaint (&symfile_complaints,
10155                  _("unsupported const value attribute form: '%s'"),
10156                  dwarf_form_name (attr->form));
10157       SYMBOL_VALUE (sym) = 0;
10158       SYMBOL_CLASS (sym) = LOC_CONST;
10159       break;
10160     }
10161 }
10162
10163
10164 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10165    or zero-extend it as appropriate for the symbol's type.  */
10166 static void
10167 dwarf2_const_value_data (struct attribute *attr,
10168                          struct symbol *sym,
10169                          int bits)
10170 {
10171   LONGEST l = DW_UNSND (attr);
10172
10173   if (bits < sizeof (l) * 8)
10174     {
10175       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10176         l &= ((LONGEST) 1 << bits) - 1;
10177       else
10178         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
10179     }
10180
10181   SYMBOL_VALUE (sym) = l;
10182   SYMBOL_CLASS (sym) = LOC_CONST;
10183 }
10184
10185
10186 /* Return the type of the die in question using its DW_AT_type attribute.  */
10187
10188 static struct type *
10189 die_type (struct die_info *die, struct dwarf2_cu *cu)
10190 {
10191   struct attribute *type_attr;
10192   struct die_info *type_die;
10193
10194   type_attr = dwarf2_attr (die, DW_AT_type, cu);
10195   if (!type_attr)
10196     {
10197       /* A missing DW_AT_type represents a void type.  */
10198       return objfile_type (cu->objfile)->builtin_void;
10199     }
10200
10201   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10202
10203   return tag_type_to_type (type_die, cu);
10204 }
10205
10206 /* True iff CU's producer generates GNAT Ada auxiliary information
10207    that allows to find parallel types through that information instead
10208    of having to do expensive parallel lookups by type name.  */
10209
10210 static int
10211 need_gnat_info (struct dwarf2_cu *cu)
10212 {
10213   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10214      of GNAT produces this auxiliary information, without any indication
10215      that it is produced.  Part of enhancing the FSF version of GNAT
10216      to produce that information will be to put in place an indicator
10217      that we can use in order to determine whether the descriptive type
10218      info is available or not.  One suggestion that has been made is
10219      to use a new attribute, attached to the CU die.  For now, assume
10220      that the descriptive type info is not available.  */
10221   return 0;
10222 }
10223
10224
10225 /* Return the auxiliary type of the die in question using its
10226    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
10227    attribute is not present.  */
10228
10229 static struct type *
10230 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10231 {
10232   struct attribute *type_attr;
10233   struct die_info *type_die;
10234
10235   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10236   if (!type_attr)
10237     return NULL;
10238
10239   type_die = follow_die_ref (die, type_attr, &cu);
10240   return tag_type_to_type (type_die, cu);
10241 }
10242
10243 /* If DIE has a descriptive_type attribute, then set the TYPE's
10244    descriptive type accordingly.  */
10245
10246 static void
10247 set_descriptive_type (struct type *type, struct die_info *die,
10248                       struct dwarf2_cu *cu)
10249 {
10250   struct type *descriptive_type = die_descriptive_type (die, cu);
10251
10252   if (descriptive_type)
10253     {
10254       ALLOCATE_GNAT_AUX_TYPE (type);
10255       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10256     }
10257 }
10258
10259 /* Return the containing type of the die in question using its
10260    DW_AT_containing_type attribute.  */
10261
10262 static struct type *
10263 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10264 {
10265   struct attribute *type_attr;
10266   struct die_info *type_die;
10267
10268   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10269   if (!type_attr)
10270     error (_("Dwarf Error: Problem turning containing type into gdb type "
10271              "[in module %s]"), cu->objfile->name);
10272
10273   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10274   return tag_type_to_type (type_die, cu);
10275 }
10276
10277 static struct type *
10278 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
10279 {
10280   struct type *this_type;
10281
10282   this_type = read_type_die (die, cu);
10283   if (!this_type)
10284     {
10285       char *message, *saved;
10286
10287       /* read_type_die already issued a complaint.  */
10288       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10289                             cu->objfile->name,
10290                             cu->header.offset,
10291                             die->offset);
10292       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10293                              message, strlen (message));
10294       xfree (message);
10295
10296       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
10297     }
10298   return this_type;
10299 }
10300
10301 static struct type *
10302 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
10303 {
10304   struct type *this_type;
10305
10306   this_type = get_die_type (die, cu);
10307   if (this_type)
10308     return this_type;
10309
10310   switch (die->tag)
10311     {
10312     case DW_TAG_class_type:
10313     case DW_TAG_interface_type:
10314     case DW_TAG_structure_type:
10315     case DW_TAG_union_type:
10316       this_type = read_structure_type (die, cu);
10317       break;
10318     case DW_TAG_enumeration_type:
10319       this_type = read_enumeration_type (die, cu);
10320       break;
10321     case DW_TAG_subprogram:
10322     case DW_TAG_subroutine_type:
10323     case DW_TAG_inlined_subroutine:
10324       this_type = read_subroutine_type (die, cu);
10325       break;
10326     case DW_TAG_array_type:
10327       this_type = read_array_type (die, cu);
10328       break;
10329     case DW_TAG_set_type:
10330       this_type = read_set_type (die, cu);
10331       break;
10332     case DW_TAG_pointer_type:
10333       this_type = read_tag_pointer_type (die, cu);
10334       break;
10335     case DW_TAG_ptr_to_member_type:
10336       this_type = read_tag_ptr_to_member_type (die, cu);
10337       break;
10338     case DW_TAG_reference_type:
10339       this_type = read_tag_reference_type (die, cu);
10340       break;
10341     case DW_TAG_const_type:
10342       this_type = read_tag_const_type (die, cu);
10343       break;
10344     case DW_TAG_volatile_type:
10345       this_type = read_tag_volatile_type (die, cu);
10346       break;
10347     case DW_TAG_string_type:
10348       this_type = read_tag_string_type (die, cu);
10349       break;
10350     case DW_TAG_typedef:
10351       this_type = read_typedef (die, cu);
10352       break;
10353     case DW_TAG_subrange_type:
10354       this_type = read_subrange_type (die, cu);
10355       break;
10356     case DW_TAG_base_type:
10357       this_type = read_base_type (die, cu);
10358       break;
10359     case DW_TAG_unspecified_type:
10360       this_type = read_unspecified_type (die, cu);
10361       break;
10362     case DW_TAG_namespace:
10363       this_type = read_namespace_type (die, cu);
10364       break;
10365     case DW_TAG_module:
10366       this_type = read_module_type (die, cu);
10367       break;
10368     default:
10369       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
10370                  dwarf_tag_name (die->tag));
10371       break;
10372     }
10373
10374   return this_type;
10375 }
10376
10377 /* Return the name of the namespace/class that DIE is defined within,
10378    or "" if we can't tell.  The caller should not xfree the result.
10379
10380    For example, if we're within the method foo() in the following
10381    code:
10382
10383    namespace N {
10384      class C {
10385        void foo () {
10386        }
10387      };
10388    }
10389
10390    then determine_prefix on foo's die will return "N::C".  */
10391
10392 static char *
10393 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
10394 {
10395   struct die_info *parent, *spec_die;
10396   struct dwarf2_cu *spec_cu;
10397   struct type *parent_type;
10398
10399   if (cu->language != language_cplus && cu->language != language_java
10400       && cu->language != language_fortran)
10401     return "";
10402
10403   /* We have to be careful in the presence of DW_AT_specification.
10404      For example, with GCC 3.4, given the code
10405
10406      namespace N {
10407        void foo() {
10408          // Definition of N::foo.
10409        }
10410      }
10411
10412      then we'll have a tree of DIEs like this:
10413
10414      1: DW_TAG_compile_unit
10415        2: DW_TAG_namespace        // N
10416          3: DW_TAG_subprogram     // declaration of N::foo
10417        4: DW_TAG_subprogram       // definition of N::foo
10418             DW_AT_specification   // refers to die #3
10419
10420      Thus, when processing die #4, we have to pretend that we're in
10421      the context of its DW_AT_specification, namely the contex of die
10422      #3.  */
10423   spec_cu = cu;
10424   spec_die = die_specification (die, &spec_cu);
10425   if (spec_die == NULL)
10426     parent = die->parent;
10427   else
10428     {
10429       parent = spec_die->parent;
10430       cu = spec_cu;
10431     }
10432
10433   if (parent == NULL)
10434     return "";
10435   else
10436     switch (parent->tag)
10437       {
10438       case DW_TAG_namespace:
10439         parent_type = read_type_die (parent, cu);
10440         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10441            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10442            Work around this problem here.  */
10443         if (cu->language == language_cplus
10444             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10445           return "";
10446         /* We give a name to even anonymous namespaces.  */
10447         return TYPE_TAG_NAME (parent_type);
10448       case DW_TAG_class_type:
10449       case DW_TAG_interface_type:
10450       case DW_TAG_structure_type:
10451       case DW_TAG_union_type:
10452       case DW_TAG_module:
10453         parent_type = read_type_die (parent, cu);
10454         if (TYPE_TAG_NAME (parent_type) != NULL)
10455           return TYPE_TAG_NAME (parent_type);
10456         else
10457           /* An anonymous structure is only allowed non-static data
10458              members; no typedefs, no member functions, et cetera.
10459              So it does not need a prefix.  */
10460           return "";
10461       default:
10462         return determine_prefix (parent, cu);
10463       }
10464 }
10465
10466 /* Return a newly-allocated string formed by concatenating PREFIX and
10467    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
10468    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
10469    perform an obconcat, otherwise allocate storage for the result.  The CU argument
10470    is used to determine the language and hence, the appropriate separator.  */
10471
10472 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
10473
10474 static char *
10475 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10476                  int physname, struct dwarf2_cu *cu)
10477 {
10478   const char *lead = "";
10479   const char *sep;
10480
10481   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10482     sep = "";
10483   else if (cu->language == language_java)
10484     sep = ".";
10485   else if (cu->language == language_fortran && physname)
10486     {
10487       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
10488          DW_AT_MIPS_linkage_name is preferred and used instead.  */
10489
10490       lead = "__";
10491       sep = "_MOD_";
10492     }
10493   else
10494     sep = "::";
10495
10496   if (prefix == NULL)
10497     prefix = "";
10498   if (suffix == NULL)
10499     suffix = "";
10500
10501   if (obs == NULL)
10502     {
10503       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
10504
10505       strcpy (retval, lead);
10506       strcat (retval, prefix);
10507       strcat (retval, sep);
10508       strcat (retval, suffix);
10509       return retval;
10510     }
10511   else
10512     {
10513       /* We have an obstack.  */
10514       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
10515     }
10516 }
10517
10518 /* Return sibling of die, NULL if no sibling.  */
10519
10520 static struct die_info *
10521 sibling_die (struct die_info *die)
10522 {
10523   return die->sibling;
10524 }
10525
10526 /* Get name of a die, return NULL if not found.  */
10527
10528 static char *
10529 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10530                           struct obstack *obstack)
10531 {
10532   if (name && cu->language == language_cplus)
10533     {
10534       char *canon_name = cp_canonicalize_string (name);
10535
10536       if (canon_name != NULL)
10537         {
10538           if (strcmp (canon_name, name) != 0)
10539             name = obsavestring (canon_name, strlen (canon_name),
10540                                  obstack);
10541           xfree (canon_name);
10542         }
10543     }
10544
10545   return name;
10546 }
10547
10548 /* Get name of a die, return NULL if not found.  */
10549
10550 static char *
10551 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
10552 {
10553   struct attribute *attr;
10554
10555   attr = dwarf2_attr (die, DW_AT_name, cu);
10556   if (!attr || !DW_STRING (attr))
10557     return NULL;
10558
10559   switch (die->tag)
10560     {
10561     case DW_TAG_compile_unit:
10562       /* Compilation units have a DW_AT_name that is a filename, not
10563          a source language identifier.  */
10564     case DW_TAG_enumeration_type:
10565     case DW_TAG_enumerator:
10566       /* These tags always have simple identifiers already; no need
10567          to canonicalize them.  */
10568       return DW_STRING (attr);
10569
10570     case DW_TAG_subprogram:
10571       /* Java constructors will all be named "<init>", so return
10572          the class name when we see this special case.  */
10573       if (cu->language == language_java
10574           && DW_STRING (attr) != NULL
10575           && strcmp (DW_STRING (attr), "<init>") == 0)
10576         {
10577           struct dwarf2_cu *spec_cu = cu;
10578           struct die_info *spec_die;
10579
10580           /* GCJ will output '<init>' for Java constructor names.
10581              For this special case, return the name of the parent class.  */
10582
10583           /* GCJ may output suprogram DIEs with AT_specification set.
10584              If so, use the name of the specified DIE.  */
10585           spec_die = die_specification (die, &spec_cu);
10586           if (spec_die != NULL)
10587             return dwarf2_name (spec_die, spec_cu);
10588
10589           do
10590             {
10591               die = die->parent;
10592               if (die->tag == DW_TAG_class_type)
10593                 return dwarf2_name (die, cu);
10594             }
10595           while (die->tag != DW_TAG_compile_unit);
10596         }
10597       break;
10598
10599     case DW_TAG_class_type:
10600     case DW_TAG_interface_type:
10601     case DW_TAG_structure_type:
10602     case DW_TAG_union_type:
10603       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10604          structures or unions.  These were of the form "._%d" in GCC 4.1,
10605          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10606          and GCC 4.4.  We work around this problem by ignoring these.  */
10607       if (strncmp (DW_STRING (attr), "._", 2) == 0
10608           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10609         return NULL;
10610       break;
10611
10612     default:
10613       break;
10614     }
10615
10616   if (!DW_STRING_IS_CANONICAL (attr))
10617     {
10618       DW_STRING (attr)
10619         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10620                                     &cu->objfile->objfile_obstack);
10621       DW_STRING_IS_CANONICAL (attr) = 1;
10622     }
10623   return DW_STRING (attr);
10624 }
10625
10626 /* Return the die that this die in an extension of, or NULL if there
10627    is none.  *EXT_CU is the CU containing DIE on input, and the CU
10628    containing the return value on output.  */
10629
10630 static struct die_info *
10631 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
10632 {
10633   struct attribute *attr;
10634
10635   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
10636   if (attr == NULL)
10637     return NULL;
10638
10639   return follow_die_ref (die, attr, ext_cu);
10640 }
10641
10642 /* Convert a DIE tag into its string name.  */
10643
10644 static char *
10645 dwarf_tag_name (unsigned tag)
10646 {
10647   switch (tag)
10648     {
10649     case DW_TAG_padding:
10650       return "DW_TAG_padding";
10651     case DW_TAG_array_type:
10652       return "DW_TAG_array_type";
10653     case DW_TAG_class_type:
10654       return "DW_TAG_class_type";
10655     case DW_TAG_entry_point:
10656       return "DW_TAG_entry_point";
10657     case DW_TAG_enumeration_type:
10658       return "DW_TAG_enumeration_type";
10659     case DW_TAG_formal_parameter:
10660       return "DW_TAG_formal_parameter";
10661     case DW_TAG_imported_declaration:
10662       return "DW_TAG_imported_declaration";
10663     case DW_TAG_label:
10664       return "DW_TAG_label";
10665     case DW_TAG_lexical_block:
10666       return "DW_TAG_lexical_block";
10667     case DW_TAG_member:
10668       return "DW_TAG_member";
10669     case DW_TAG_pointer_type:
10670       return "DW_TAG_pointer_type";
10671     case DW_TAG_reference_type:
10672       return "DW_TAG_reference_type";
10673     case DW_TAG_compile_unit:
10674       return "DW_TAG_compile_unit";
10675     case DW_TAG_string_type:
10676       return "DW_TAG_string_type";
10677     case DW_TAG_structure_type:
10678       return "DW_TAG_structure_type";
10679     case DW_TAG_subroutine_type:
10680       return "DW_TAG_subroutine_type";
10681     case DW_TAG_typedef:
10682       return "DW_TAG_typedef";
10683     case DW_TAG_union_type:
10684       return "DW_TAG_union_type";
10685     case DW_TAG_unspecified_parameters:
10686       return "DW_TAG_unspecified_parameters";
10687     case DW_TAG_variant:
10688       return "DW_TAG_variant";
10689     case DW_TAG_common_block:
10690       return "DW_TAG_common_block";
10691     case DW_TAG_common_inclusion:
10692       return "DW_TAG_common_inclusion";
10693     case DW_TAG_inheritance:
10694       return "DW_TAG_inheritance";
10695     case DW_TAG_inlined_subroutine:
10696       return "DW_TAG_inlined_subroutine";
10697     case DW_TAG_module:
10698       return "DW_TAG_module";
10699     case DW_TAG_ptr_to_member_type:
10700       return "DW_TAG_ptr_to_member_type";
10701     case DW_TAG_set_type:
10702       return "DW_TAG_set_type";
10703     case DW_TAG_subrange_type:
10704       return "DW_TAG_subrange_type";
10705     case DW_TAG_with_stmt:
10706       return "DW_TAG_with_stmt";
10707     case DW_TAG_access_declaration:
10708       return "DW_TAG_access_declaration";
10709     case DW_TAG_base_type:
10710       return "DW_TAG_base_type";
10711     case DW_TAG_catch_block:
10712       return "DW_TAG_catch_block";
10713     case DW_TAG_const_type:
10714       return "DW_TAG_const_type";
10715     case DW_TAG_constant:
10716       return "DW_TAG_constant";
10717     case DW_TAG_enumerator:
10718       return "DW_TAG_enumerator";
10719     case DW_TAG_file_type:
10720       return "DW_TAG_file_type";
10721     case DW_TAG_friend:
10722       return "DW_TAG_friend";
10723     case DW_TAG_namelist:
10724       return "DW_TAG_namelist";
10725     case DW_TAG_namelist_item:
10726       return "DW_TAG_namelist_item";
10727     case DW_TAG_packed_type:
10728       return "DW_TAG_packed_type";
10729     case DW_TAG_subprogram:
10730       return "DW_TAG_subprogram";
10731     case DW_TAG_template_type_param:
10732       return "DW_TAG_template_type_param";
10733     case DW_TAG_template_value_param:
10734       return "DW_TAG_template_value_param";
10735     case DW_TAG_thrown_type:
10736       return "DW_TAG_thrown_type";
10737     case DW_TAG_try_block:
10738       return "DW_TAG_try_block";
10739     case DW_TAG_variant_part:
10740       return "DW_TAG_variant_part";
10741     case DW_TAG_variable:
10742       return "DW_TAG_variable";
10743     case DW_TAG_volatile_type:
10744       return "DW_TAG_volatile_type";
10745     case DW_TAG_dwarf_procedure:
10746       return "DW_TAG_dwarf_procedure";
10747     case DW_TAG_restrict_type:
10748       return "DW_TAG_restrict_type";
10749     case DW_TAG_interface_type:
10750       return "DW_TAG_interface_type";
10751     case DW_TAG_namespace:
10752       return "DW_TAG_namespace";
10753     case DW_TAG_imported_module:
10754       return "DW_TAG_imported_module";
10755     case DW_TAG_unspecified_type:
10756       return "DW_TAG_unspecified_type";
10757     case DW_TAG_partial_unit:
10758       return "DW_TAG_partial_unit";
10759     case DW_TAG_imported_unit:
10760       return "DW_TAG_imported_unit";
10761     case DW_TAG_condition:
10762       return "DW_TAG_condition";
10763     case DW_TAG_shared_type:
10764       return "DW_TAG_shared_type";
10765     case DW_TAG_type_unit:
10766       return "DW_TAG_type_unit";
10767     case DW_TAG_MIPS_loop:
10768       return "DW_TAG_MIPS_loop";
10769     case DW_TAG_HP_array_descriptor:
10770       return "DW_TAG_HP_array_descriptor";
10771     case DW_TAG_format_label:
10772       return "DW_TAG_format_label";
10773     case DW_TAG_function_template:
10774       return "DW_TAG_function_template";
10775     case DW_TAG_class_template:
10776       return "DW_TAG_class_template";
10777     case DW_TAG_GNU_BINCL:
10778       return "DW_TAG_GNU_BINCL";
10779     case DW_TAG_GNU_EINCL:
10780       return "DW_TAG_GNU_EINCL";
10781     case DW_TAG_upc_shared_type:
10782       return "DW_TAG_upc_shared_type";
10783     case DW_TAG_upc_strict_type:
10784       return "DW_TAG_upc_strict_type";
10785     case DW_TAG_upc_relaxed_type:
10786       return "DW_TAG_upc_relaxed_type";
10787     case DW_TAG_PGI_kanji_type:
10788       return "DW_TAG_PGI_kanji_type";
10789     case DW_TAG_PGI_interface_block:
10790       return "DW_TAG_PGI_interface_block";
10791     default:
10792       return "DW_TAG_<unknown>";
10793     }
10794 }
10795
10796 /* Convert a DWARF attribute code into its string name.  */
10797
10798 static char *
10799 dwarf_attr_name (unsigned attr)
10800 {
10801   switch (attr)
10802     {
10803     case DW_AT_sibling:
10804       return "DW_AT_sibling";
10805     case DW_AT_location:
10806       return "DW_AT_location";
10807     case DW_AT_name:
10808       return "DW_AT_name";
10809     case DW_AT_ordering:
10810       return "DW_AT_ordering";
10811     case DW_AT_subscr_data:
10812       return "DW_AT_subscr_data";
10813     case DW_AT_byte_size:
10814       return "DW_AT_byte_size";
10815     case DW_AT_bit_offset:
10816       return "DW_AT_bit_offset";
10817     case DW_AT_bit_size:
10818       return "DW_AT_bit_size";
10819     case DW_AT_element_list:
10820       return "DW_AT_element_list";
10821     case DW_AT_stmt_list:
10822       return "DW_AT_stmt_list";
10823     case DW_AT_low_pc:
10824       return "DW_AT_low_pc";
10825     case DW_AT_high_pc:
10826       return "DW_AT_high_pc";
10827     case DW_AT_language:
10828       return "DW_AT_language";
10829     case DW_AT_member:
10830       return "DW_AT_member";
10831     case DW_AT_discr:
10832       return "DW_AT_discr";
10833     case DW_AT_discr_value:
10834       return "DW_AT_discr_value";
10835     case DW_AT_visibility:
10836       return "DW_AT_visibility";
10837     case DW_AT_import:
10838       return "DW_AT_import";
10839     case DW_AT_string_length:
10840       return "DW_AT_string_length";
10841     case DW_AT_common_reference:
10842       return "DW_AT_common_reference";
10843     case DW_AT_comp_dir:
10844       return "DW_AT_comp_dir";
10845     case DW_AT_const_value:
10846       return "DW_AT_const_value";
10847     case DW_AT_containing_type:
10848       return "DW_AT_containing_type";
10849     case DW_AT_default_value:
10850       return "DW_AT_default_value";
10851     case DW_AT_inline:
10852       return "DW_AT_inline";
10853     case DW_AT_is_optional:
10854       return "DW_AT_is_optional";
10855     case DW_AT_lower_bound:
10856       return "DW_AT_lower_bound";
10857     case DW_AT_producer:
10858       return "DW_AT_producer";
10859     case DW_AT_prototyped:
10860       return "DW_AT_prototyped";
10861     case DW_AT_return_addr:
10862       return "DW_AT_return_addr";
10863     case DW_AT_start_scope:
10864       return "DW_AT_start_scope";
10865     case DW_AT_bit_stride:
10866       return "DW_AT_bit_stride";
10867     case DW_AT_upper_bound:
10868       return "DW_AT_upper_bound";
10869     case DW_AT_abstract_origin:
10870       return "DW_AT_abstract_origin";
10871     case DW_AT_accessibility:
10872       return "DW_AT_accessibility";
10873     case DW_AT_address_class:
10874       return "DW_AT_address_class";
10875     case DW_AT_artificial:
10876       return "DW_AT_artificial";
10877     case DW_AT_base_types:
10878       return "DW_AT_base_types";
10879     case DW_AT_calling_convention:
10880       return "DW_AT_calling_convention";
10881     case DW_AT_count:
10882       return "DW_AT_count";
10883     case DW_AT_data_member_location:
10884       return "DW_AT_data_member_location";
10885     case DW_AT_decl_column:
10886       return "DW_AT_decl_column";
10887     case DW_AT_decl_file:
10888       return "DW_AT_decl_file";
10889     case DW_AT_decl_line:
10890       return "DW_AT_decl_line";
10891     case DW_AT_declaration:
10892       return "DW_AT_declaration";
10893     case DW_AT_discr_list:
10894       return "DW_AT_discr_list";
10895     case DW_AT_encoding:
10896       return "DW_AT_encoding";
10897     case DW_AT_external:
10898       return "DW_AT_external";
10899     case DW_AT_frame_base:
10900       return "DW_AT_frame_base";
10901     case DW_AT_friend:
10902       return "DW_AT_friend";
10903     case DW_AT_identifier_case:
10904       return "DW_AT_identifier_case";
10905     case DW_AT_macro_info:
10906       return "DW_AT_macro_info";
10907     case DW_AT_namelist_items:
10908       return "DW_AT_namelist_items";
10909     case DW_AT_priority:
10910       return "DW_AT_priority";
10911     case DW_AT_segment:
10912       return "DW_AT_segment";
10913     case DW_AT_specification:
10914       return "DW_AT_specification";
10915     case DW_AT_static_link:
10916       return "DW_AT_static_link";
10917     case DW_AT_type:
10918       return "DW_AT_type";
10919     case DW_AT_use_location:
10920       return "DW_AT_use_location";
10921     case DW_AT_variable_parameter:
10922       return "DW_AT_variable_parameter";
10923     case DW_AT_virtuality:
10924       return "DW_AT_virtuality";
10925     case DW_AT_vtable_elem_location:
10926       return "DW_AT_vtable_elem_location";
10927     /* DWARF 3 values.  */
10928     case DW_AT_allocated:
10929       return "DW_AT_allocated";
10930     case DW_AT_associated:
10931       return "DW_AT_associated";
10932     case DW_AT_data_location:
10933       return "DW_AT_data_location";
10934     case DW_AT_byte_stride:
10935       return "DW_AT_byte_stride";
10936     case DW_AT_entry_pc:
10937       return "DW_AT_entry_pc";
10938     case DW_AT_use_UTF8:
10939       return "DW_AT_use_UTF8";
10940     case DW_AT_extension:
10941       return "DW_AT_extension";
10942     case DW_AT_ranges:
10943       return "DW_AT_ranges";
10944     case DW_AT_trampoline:
10945       return "DW_AT_trampoline";
10946     case DW_AT_call_column:
10947       return "DW_AT_call_column";
10948     case DW_AT_call_file:
10949       return "DW_AT_call_file";
10950     case DW_AT_call_line:
10951       return "DW_AT_call_line";
10952     case DW_AT_description:
10953       return "DW_AT_description";
10954     case DW_AT_binary_scale:
10955       return "DW_AT_binary_scale";
10956     case DW_AT_decimal_scale:
10957       return "DW_AT_decimal_scale";
10958     case DW_AT_small:
10959       return "DW_AT_small";
10960     case DW_AT_decimal_sign:
10961       return "DW_AT_decimal_sign";
10962     case DW_AT_digit_count:
10963       return "DW_AT_digit_count";
10964     case DW_AT_picture_string:
10965       return "DW_AT_picture_string";
10966     case DW_AT_mutable:
10967       return "DW_AT_mutable";
10968     case DW_AT_threads_scaled:
10969       return "DW_AT_threads_scaled";
10970     case DW_AT_explicit:
10971       return "DW_AT_explicit";
10972     case DW_AT_object_pointer:
10973       return "DW_AT_object_pointer";
10974     case DW_AT_endianity:
10975       return "DW_AT_endianity";
10976     case DW_AT_elemental:
10977       return "DW_AT_elemental";
10978     case DW_AT_pure:
10979       return "DW_AT_pure";
10980     case DW_AT_recursive:
10981       return "DW_AT_recursive";
10982     /* DWARF 4 values.  */
10983     case DW_AT_signature:
10984       return "DW_AT_signature";
10985     case DW_AT_linkage_name:
10986       return "DW_AT_linkage_name";
10987     /* SGI/MIPS extensions.  */
10988 #ifdef MIPS /* collides with DW_AT_HP_block_index */
10989     case DW_AT_MIPS_fde:
10990       return "DW_AT_MIPS_fde";
10991 #endif
10992     case DW_AT_MIPS_loop_begin:
10993       return "DW_AT_MIPS_loop_begin";
10994     case DW_AT_MIPS_tail_loop_begin:
10995       return "DW_AT_MIPS_tail_loop_begin";
10996     case DW_AT_MIPS_epilog_begin:
10997       return "DW_AT_MIPS_epilog_begin";
10998     case DW_AT_MIPS_loop_unroll_factor:
10999       return "DW_AT_MIPS_loop_unroll_factor";
11000     case DW_AT_MIPS_software_pipeline_depth:
11001       return "DW_AT_MIPS_software_pipeline_depth";
11002     case DW_AT_MIPS_linkage_name:
11003       return "DW_AT_MIPS_linkage_name";
11004     case DW_AT_MIPS_stride:
11005       return "DW_AT_MIPS_stride";
11006     case DW_AT_MIPS_abstract_name:
11007       return "DW_AT_MIPS_abstract_name";
11008     case DW_AT_MIPS_clone_origin:
11009       return "DW_AT_MIPS_clone_origin";
11010     case DW_AT_MIPS_has_inlines:
11011       return "DW_AT_MIPS_has_inlines";
11012     /* HP extensions.  */
11013 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11014     case DW_AT_HP_block_index:
11015       return "DW_AT_HP_block_index";
11016 #endif
11017     case DW_AT_HP_unmodifiable:
11018       return "DW_AT_HP_unmodifiable";
11019     case DW_AT_HP_actuals_stmt_list:
11020       return "DW_AT_HP_actuals_stmt_list";
11021     case DW_AT_HP_proc_per_section:
11022       return "DW_AT_HP_proc_per_section";
11023     case DW_AT_HP_raw_data_ptr:
11024       return "DW_AT_HP_raw_data_ptr";
11025     case DW_AT_HP_pass_by_reference:
11026       return "DW_AT_HP_pass_by_reference";
11027     case DW_AT_HP_opt_level:
11028       return "DW_AT_HP_opt_level";
11029     case DW_AT_HP_prof_version_id:
11030       return "DW_AT_HP_prof_version_id";
11031     case DW_AT_HP_opt_flags:
11032       return "DW_AT_HP_opt_flags";
11033     case DW_AT_HP_cold_region_low_pc:
11034       return "DW_AT_HP_cold_region_low_pc";
11035     case DW_AT_HP_cold_region_high_pc:
11036       return "DW_AT_HP_cold_region_high_pc";
11037     case DW_AT_HP_all_variables_modifiable:
11038       return "DW_AT_HP_all_variables_modifiable";
11039     case DW_AT_HP_linkage_name:
11040       return "DW_AT_HP_linkage_name";
11041     case DW_AT_HP_prof_flags:
11042       return "DW_AT_HP_prof_flags";
11043     /* GNU extensions.  */
11044     case DW_AT_sf_names:
11045       return "DW_AT_sf_names";
11046     case DW_AT_src_info:
11047       return "DW_AT_src_info";
11048     case DW_AT_mac_info:
11049       return "DW_AT_mac_info";
11050     case DW_AT_src_coords:
11051       return "DW_AT_src_coords";
11052     case DW_AT_body_begin:
11053       return "DW_AT_body_begin";
11054     case DW_AT_body_end:
11055       return "DW_AT_body_end";
11056     case DW_AT_GNU_vector:
11057       return "DW_AT_GNU_vector";
11058     /* VMS extensions.  */
11059     case DW_AT_VMS_rtnbeg_pd_address:
11060       return "DW_AT_VMS_rtnbeg_pd_address";
11061     /* UPC extension.  */
11062     case DW_AT_upc_threads_scaled:
11063       return "DW_AT_upc_threads_scaled";
11064     /* PGI (STMicroelectronics) extensions.  */
11065     case DW_AT_PGI_lbase:
11066       return "DW_AT_PGI_lbase";
11067     case DW_AT_PGI_soffset:
11068       return "DW_AT_PGI_soffset";
11069     case DW_AT_PGI_lstride:
11070       return "DW_AT_PGI_lstride";
11071     default:
11072       return "DW_AT_<unknown>";
11073     }
11074 }
11075
11076 /* Convert a DWARF value form code into its string name.  */
11077
11078 static char *
11079 dwarf_form_name (unsigned form)
11080 {
11081   switch (form)
11082     {
11083     case DW_FORM_addr:
11084       return "DW_FORM_addr";
11085     case DW_FORM_block2:
11086       return "DW_FORM_block2";
11087     case DW_FORM_block4:
11088       return "DW_FORM_block4";
11089     case DW_FORM_data2:
11090       return "DW_FORM_data2";
11091     case DW_FORM_data4:
11092       return "DW_FORM_data4";
11093     case DW_FORM_data8:
11094       return "DW_FORM_data8";
11095     case DW_FORM_string:
11096       return "DW_FORM_string";
11097     case DW_FORM_block:
11098       return "DW_FORM_block";
11099     case DW_FORM_block1:
11100       return "DW_FORM_block1";
11101     case DW_FORM_data1:
11102       return "DW_FORM_data1";
11103     case DW_FORM_flag:
11104       return "DW_FORM_flag";
11105     case DW_FORM_sdata:
11106       return "DW_FORM_sdata";
11107     case DW_FORM_strp:
11108       return "DW_FORM_strp";
11109     case DW_FORM_udata:
11110       return "DW_FORM_udata";
11111     case DW_FORM_ref_addr:
11112       return "DW_FORM_ref_addr";
11113     case DW_FORM_ref1:
11114       return "DW_FORM_ref1";
11115     case DW_FORM_ref2:
11116       return "DW_FORM_ref2";
11117     case DW_FORM_ref4:
11118       return "DW_FORM_ref4";
11119     case DW_FORM_ref8:
11120       return "DW_FORM_ref8";
11121     case DW_FORM_ref_udata:
11122       return "DW_FORM_ref_udata";
11123     case DW_FORM_indirect:
11124       return "DW_FORM_indirect";
11125     case DW_FORM_sec_offset:
11126       return "DW_FORM_sec_offset";
11127     case DW_FORM_exprloc:
11128       return "DW_FORM_exprloc";
11129     case DW_FORM_flag_present:
11130       return "DW_FORM_flag_present";
11131     case DW_FORM_sig8:
11132       return "DW_FORM_sig8";
11133     default:
11134       return "DW_FORM_<unknown>";
11135     }
11136 }
11137
11138 /* Convert a DWARF stack opcode into its string name.  */
11139
11140 const char *
11141 dwarf_stack_op_name (unsigned op, int def)
11142 {
11143   switch (op)
11144     {
11145     case DW_OP_addr:
11146       return "DW_OP_addr";
11147     case DW_OP_deref:
11148       return "DW_OP_deref";
11149     case DW_OP_const1u:
11150       return "DW_OP_const1u";
11151     case DW_OP_const1s:
11152       return "DW_OP_const1s";
11153     case DW_OP_const2u:
11154       return "DW_OP_const2u";
11155     case DW_OP_const2s:
11156       return "DW_OP_const2s";
11157     case DW_OP_const4u:
11158       return "DW_OP_const4u";
11159     case DW_OP_const4s:
11160       return "DW_OP_const4s";
11161     case DW_OP_const8u:
11162       return "DW_OP_const8u";
11163     case DW_OP_const8s:
11164       return "DW_OP_const8s";
11165     case DW_OP_constu:
11166       return "DW_OP_constu";
11167     case DW_OP_consts:
11168       return "DW_OP_consts";
11169     case DW_OP_dup:
11170       return "DW_OP_dup";
11171     case DW_OP_drop:
11172       return "DW_OP_drop";
11173     case DW_OP_over:
11174       return "DW_OP_over";
11175     case DW_OP_pick:
11176       return "DW_OP_pick";
11177     case DW_OP_swap:
11178       return "DW_OP_swap";
11179     case DW_OP_rot:
11180       return "DW_OP_rot";
11181     case DW_OP_xderef:
11182       return "DW_OP_xderef";
11183     case DW_OP_abs:
11184       return "DW_OP_abs";
11185     case DW_OP_and:
11186       return "DW_OP_and";
11187     case DW_OP_div:
11188       return "DW_OP_div";
11189     case DW_OP_minus:
11190       return "DW_OP_minus";
11191     case DW_OP_mod:
11192       return "DW_OP_mod";
11193     case DW_OP_mul:
11194       return "DW_OP_mul";
11195     case DW_OP_neg:
11196       return "DW_OP_neg";
11197     case DW_OP_not:
11198       return "DW_OP_not";
11199     case DW_OP_or:
11200       return "DW_OP_or";
11201     case DW_OP_plus:
11202       return "DW_OP_plus";
11203     case DW_OP_plus_uconst:
11204       return "DW_OP_plus_uconst";
11205     case DW_OP_shl:
11206       return "DW_OP_shl";
11207     case DW_OP_shr:
11208       return "DW_OP_shr";
11209     case DW_OP_shra:
11210       return "DW_OP_shra";
11211     case DW_OP_xor:
11212       return "DW_OP_xor";
11213     case DW_OP_bra:
11214       return "DW_OP_bra";
11215     case DW_OP_eq:
11216       return "DW_OP_eq";
11217     case DW_OP_ge:
11218       return "DW_OP_ge";
11219     case DW_OP_gt:
11220       return "DW_OP_gt";
11221     case DW_OP_le:
11222       return "DW_OP_le";
11223     case DW_OP_lt:
11224       return "DW_OP_lt";
11225     case DW_OP_ne:
11226       return "DW_OP_ne";
11227     case DW_OP_skip:
11228       return "DW_OP_skip";
11229     case DW_OP_lit0:
11230       return "DW_OP_lit0";
11231     case DW_OP_lit1:
11232       return "DW_OP_lit1";
11233     case DW_OP_lit2:
11234       return "DW_OP_lit2";
11235     case DW_OP_lit3:
11236       return "DW_OP_lit3";
11237     case DW_OP_lit4:
11238       return "DW_OP_lit4";
11239     case DW_OP_lit5:
11240       return "DW_OP_lit5";
11241     case DW_OP_lit6:
11242       return "DW_OP_lit6";
11243     case DW_OP_lit7:
11244       return "DW_OP_lit7";
11245     case DW_OP_lit8:
11246       return "DW_OP_lit8";
11247     case DW_OP_lit9:
11248       return "DW_OP_lit9";
11249     case DW_OP_lit10:
11250       return "DW_OP_lit10";
11251     case DW_OP_lit11:
11252       return "DW_OP_lit11";
11253     case DW_OP_lit12:
11254       return "DW_OP_lit12";
11255     case DW_OP_lit13:
11256       return "DW_OP_lit13";
11257     case DW_OP_lit14:
11258       return "DW_OP_lit14";
11259     case DW_OP_lit15:
11260       return "DW_OP_lit15";
11261     case DW_OP_lit16:
11262       return "DW_OP_lit16";
11263     case DW_OP_lit17:
11264       return "DW_OP_lit17";
11265     case DW_OP_lit18:
11266       return "DW_OP_lit18";
11267     case DW_OP_lit19:
11268       return "DW_OP_lit19";
11269     case DW_OP_lit20:
11270       return "DW_OP_lit20";
11271     case DW_OP_lit21:
11272       return "DW_OP_lit21";
11273     case DW_OP_lit22:
11274       return "DW_OP_lit22";
11275     case DW_OP_lit23:
11276       return "DW_OP_lit23";
11277     case DW_OP_lit24:
11278       return "DW_OP_lit24";
11279     case DW_OP_lit25:
11280       return "DW_OP_lit25";
11281     case DW_OP_lit26:
11282       return "DW_OP_lit26";
11283     case DW_OP_lit27:
11284       return "DW_OP_lit27";
11285     case DW_OP_lit28:
11286       return "DW_OP_lit28";
11287     case DW_OP_lit29:
11288       return "DW_OP_lit29";
11289     case DW_OP_lit30:
11290       return "DW_OP_lit30";
11291     case DW_OP_lit31:
11292       return "DW_OP_lit31";
11293     case DW_OP_reg0:
11294       return "DW_OP_reg0";
11295     case DW_OP_reg1:
11296       return "DW_OP_reg1";
11297     case DW_OP_reg2:
11298       return "DW_OP_reg2";
11299     case DW_OP_reg3:
11300       return "DW_OP_reg3";
11301     case DW_OP_reg4:
11302       return "DW_OP_reg4";
11303     case DW_OP_reg5:
11304       return "DW_OP_reg5";
11305     case DW_OP_reg6:
11306       return "DW_OP_reg6";
11307     case DW_OP_reg7:
11308       return "DW_OP_reg7";
11309     case DW_OP_reg8:
11310       return "DW_OP_reg8";
11311     case DW_OP_reg9:
11312       return "DW_OP_reg9";
11313     case DW_OP_reg10:
11314       return "DW_OP_reg10";
11315     case DW_OP_reg11:
11316       return "DW_OP_reg11";
11317     case DW_OP_reg12:
11318       return "DW_OP_reg12";
11319     case DW_OP_reg13:
11320       return "DW_OP_reg13";
11321     case DW_OP_reg14:
11322       return "DW_OP_reg14";
11323     case DW_OP_reg15:
11324       return "DW_OP_reg15";
11325     case DW_OP_reg16:
11326       return "DW_OP_reg16";
11327     case DW_OP_reg17:
11328       return "DW_OP_reg17";
11329     case DW_OP_reg18:
11330       return "DW_OP_reg18";
11331     case DW_OP_reg19:
11332       return "DW_OP_reg19";
11333     case DW_OP_reg20:
11334       return "DW_OP_reg20";
11335     case DW_OP_reg21:
11336       return "DW_OP_reg21";
11337     case DW_OP_reg22:
11338       return "DW_OP_reg22";
11339     case DW_OP_reg23:
11340       return "DW_OP_reg23";
11341     case DW_OP_reg24:
11342       return "DW_OP_reg24";
11343     case DW_OP_reg25:
11344       return "DW_OP_reg25";
11345     case DW_OP_reg26:
11346       return "DW_OP_reg26";
11347     case DW_OP_reg27:
11348       return "DW_OP_reg27";
11349     case DW_OP_reg28:
11350       return "DW_OP_reg28";
11351     case DW_OP_reg29:
11352       return "DW_OP_reg29";
11353     case DW_OP_reg30:
11354       return "DW_OP_reg30";
11355     case DW_OP_reg31:
11356       return "DW_OP_reg31";
11357     case DW_OP_breg0:
11358       return "DW_OP_breg0";
11359     case DW_OP_breg1:
11360       return "DW_OP_breg1";
11361     case DW_OP_breg2:
11362       return "DW_OP_breg2";
11363     case DW_OP_breg3:
11364       return "DW_OP_breg3";
11365     case DW_OP_breg4:
11366       return "DW_OP_breg4";
11367     case DW_OP_breg5:
11368       return "DW_OP_breg5";
11369     case DW_OP_breg6:
11370       return "DW_OP_breg6";
11371     case DW_OP_breg7:
11372       return "DW_OP_breg7";
11373     case DW_OP_breg8:
11374       return "DW_OP_breg8";
11375     case DW_OP_breg9:
11376       return "DW_OP_breg9";
11377     case DW_OP_breg10:
11378       return "DW_OP_breg10";
11379     case DW_OP_breg11:
11380       return "DW_OP_breg11";
11381     case DW_OP_breg12:
11382       return "DW_OP_breg12";
11383     case DW_OP_breg13:
11384       return "DW_OP_breg13";
11385     case DW_OP_breg14:
11386       return "DW_OP_breg14";
11387     case DW_OP_breg15:
11388       return "DW_OP_breg15";
11389     case DW_OP_breg16:
11390       return "DW_OP_breg16";
11391     case DW_OP_breg17:
11392       return "DW_OP_breg17";
11393     case DW_OP_breg18:
11394       return "DW_OP_breg18";
11395     case DW_OP_breg19:
11396       return "DW_OP_breg19";
11397     case DW_OP_breg20:
11398       return "DW_OP_breg20";
11399     case DW_OP_breg21:
11400       return "DW_OP_breg21";
11401     case DW_OP_breg22:
11402       return "DW_OP_breg22";
11403     case DW_OP_breg23:
11404       return "DW_OP_breg23";
11405     case DW_OP_breg24:
11406       return "DW_OP_breg24";
11407     case DW_OP_breg25:
11408       return "DW_OP_breg25";
11409     case DW_OP_breg26:
11410       return "DW_OP_breg26";
11411     case DW_OP_breg27:
11412       return "DW_OP_breg27";
11413     case DW_OP_breg28:
11414       return "DW_OP_breg28";
11415     case DW_OP_breg29:
11416       return "DW_OP_breg29";
11417     case DW_OP_breg30:
11418       return "DW_OP_breg30";
11419     case DW_OP_breg31:
11420       return "DW_OP_breg31";
11421     case DW_OP_regx:
11422       return "DW_OP_regx";
11423     case DW_OP_fbreg:
11424       return "DW_OP_fbreg";
11425     case DW_OP_bregx:
11426       return "DW_OP_bregx";
11427     case DW_OP_piece:
11428       return "DW_OP_piece";
11429     case DW_OP_deref_size:
11430       return "DW_OP_deref_size";
11431     case DW_OP_xderef_size:
11432       return "DW_OP_xderef_size";
11433     case DW_OP_nop:
11434       return "DW_OP_nop";
11435     /* DWARF 3 extensions.  */
11436     case DW_OP_push_object_address:
11437       return "DW_OP_push_object_address";
11438     case DW_OP_call2:
11439       return "DW_OP_call2";
11440     case DW_OP_call4:
11441       return "DW_OP_call4";
11442     case DW_OP_call_ref:
11443       return "DW_OP_call_ref";
11444     case DW_OP_form_tls_address:
11445       return "DW_OP_form_tls_address";
11446     case DW_OP_call_frame_cfa:
11447       return "DW_OP_call_frame_cfa";
11448     case DW_OP_bit_piece:
11449       return "DW_OP_bit_piece";
11450     /* DWARF 4 extensions.  */
11451     case DW_OP_implicit_value:
11452       return "DW_OP_implicit_value";
11453     case DW_OP_stack_value:
11454       return "DW_OP_stack_value";
11455     /* GNU extensions.  */
11456     case DW_OP_GNU_push_tls_address:
11457       return "DW_OP_GNU_push_tls_address";
11458     case DW_OP_GNU_uninit:
11459       return "DW_OP_GNU_uninit";
11460     default:
11461       return def ? "OP_<unknown>" : NULL;
11462     }
11463 }
11464
11465 static char *
11466 dwarf_bool_name (unsigned mybool)
11467 {
11468   if (mybool)
11469     return "TRUE";
11470   else
11471     return "FALSE";
11472 }
11473
11474 /* Convert a DWARF type code into its string name.  */
11475
11476 static char *
11477 dwarf_type_encoding_name (unsigned enc)
11478 {
11479   switch (enc)
11480     {
11481     case DW_ATE_void:
11482       return "DW_ATE_void";
11483     case DW_ATE_address:
11484       return "DW_ATE_address";
11485     case DW_ATE_boolean:
11486       return "DW_ATE_boolean";
11487     case DW_ATE_complex_float:
11488       return "DW_ATE_complex_float";
11489     case DW_ATE_float:
11490       return "DW_ATE_float";
11491     case DW_ATE_signed:
11492       return "DW_ATE_signed";
11493     case DW_ATE_signed_char:
11494       return "DW_ATE_signed_char";
11495     case DW_ATE_unsigned:
11496       return "DW_ATE_unsigned";
11497     case DW_ATE_unsigned_char:
11498       return "DW_ATE_unsigned_char";
11499     /* DWARF 3.  */
11500     case DW_ATE_imaginary_float:
11501       return "DW_ATE_imaginary_float";
11502     case DW_ATE_packed_decimal:
11503       return "DW_ATE_packed_decimal";
11504     case DW_ATE_numeric_string:
11505       return "DW_ATE_numeric_string";
11506     case DW_ATE_edited:
11507       return "DW_ATE_edited";
11508     case DW_ATE_signed_fixed:
11509       return "DW_ATE_signed_fixed";
11510     case DW_ATE_unsigned_fixed:
11511       return "DW_ATE_unsigned_fixed";
11512     case DW_ATE_decimal_float:
11513       return "DW_ATE_decimal_float";
11514     /* DWARF 4.  */
11515     case DW_ATE_UTF:
11516       return "DW_ATE_UTF";
11517     /* HP extensions.  */
11518     case DW_ATE_HP_float80:
11519       return "DW_ATE_HP_float80";
11520     case DW_ATE_HP_complex_float80:
11521       return "DW_ATE_HP_complex_float80";
11522     case DW_ATE_HP_float128:
11523       return "DW_ATE_HP_float128";
11524     case DW_ATE_HP_complex_float128:
11525       return "DW_ATE_HP_complex_float128";
11526     case DW_ATE_HP_floathpintel:
11527       return "DW_ATE_HP_floathpintel";
11528     case DW_ATE_HP_imaginary_float80:
11529       return "DW_ATE_HP_imaginary_float80";
11530     case DW_ATE_HP_imaginary_float128:
11531       return "DW_ATE_HP_imaginary_float128";
11532     default:
11533       return "DW_ATE_<unknown>";
11534     }
11535 }
11536
11537 /* Convert a DWARF call frame info operation to its string name. */
11538
11539 #if 0
11540 static char *
11541 dwarf_cfi_name (unsigned cfi_opc)
11542 {
11543   switch (cfi_opc)
11544     {
11545     case DW_CFA_advance_loc:
11546       return "DW_CFA_advance_loc";
11547     case DW_CFA_offset:
11548       return "DW_CFA_offset";
11549     case DW_CFA_restore:
11550       return "DW_CFA_restore";
11551     case DW_CFA_nop:
11552       return "DW_CFA_nop";
11553     case DW_CFA_set_loc:
11554       return "DW_CFA_set_loc";
11555     case DW_CFA_advance_loc1:
11556       return "DW_CFA_advance_loc1";
11557     case DW_CFA_advance_loc2:
11558       return "DW_CFA_advance_loc2";
11559     case DW_CFA_advance_loc4:
11560       return "DW_CFA_advance_loc4";
11561     case DW_CFA_offset_extended:
11562       return "DW_CFA_offset_extended";
11563     case DW_CFA_restore_extended:
11564       return "DW_CFA_restore_extended";
11565     case DW_CFA_undefined:
11566       return "DW_CFA_undefined";
11567     case DW_CFA_same_value:
11568       return "DW_CFA_same_value";
11569     case DW_CFA_register:
11570       return "DW_CFA_register";
11571     case DW_CFA_remember_state:
11572       return "DW_CFA_remember_state";
11573     case DW_CFA_restore_state:
11574       return "DW_CFA_restore_state";
11575     case DW_CFA_def_cfa:
11576       return "DW_CFA_def_cfa";
11577     case DW_CFA_def_cfa_register:
11578       return "DW_CFA_def_cfa_register";
11579     case DW_CFA_def_cfa_offset:
11580       return "DW_CFA_def_cfa_offset";
11581     /* DWARF 3.  */
11582     case DW_CFA_def_cfa_expression:
11583       return "DW_CFA_def_cfa_expression";
11584     case DW_CFA_expression:
11585       return "DW_CFA_expression";
11586     case DW_CFA_offset_extended_sf:
11587       return "DW_CFA_offset_extended_sf";
11588     case DW_CFA_def_cfa_sf:
11589       return "DW_CFA_def_cfa_sf";
11590     case DW_CFA_def_cfa_offset_sf:
11591       return "DW_CFA_def_cfa_offset_sf";
11592     case DW_CFA_val_offset:
11593       return "DW_CFA_val_offset";
11594     case DW_CFA_val_offset_sf:
11595       return "DW_CFA_val_offset_sf";
11596     case DW_CFA_val_expression:
11597       return "DW_CFA_val_expression";
11598     /* SGI/MIPS specific.  */
11599     case DW_CFA_MIPS_advance_loc8:
11600       return "DW_CFA_MIPS_advance_loc8";
11601     /* GNU extensions.  */
11602     case DW_CFA_GNU_window_save:
11603       return "DW_CFA_GNU_window_save";
11604     case DW_CFA_GNU_args_size:
11605       return "DW_CFA_GNU_args_size";
11606     case DW_CFA_GNU_negative_offset_extended:
11607       return "DW_CFA_GNU_negative_offset_extended";
11608     default:
11609       return "DW_CFA_<unknown>";
11610     }
11611 }
11612 #endif
11613
11614 static void
11615 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
11616 {
11617   unsigned int i;
11618
11619   print_spaces (indent, f);
11620   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
11621            dwarf_tag_name (die->tag), die->abbrev, die->offset);
11622
11623   if (die->parent != NULL)
11624     {
11625       print_spaces (indent, f);
11626       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
11627                           die->parent->offset);
11628     }
11629
11630   print_spaces (indent, f);
11631   fprintf_unfiltered (f, "  has children: %s\n",
11632            dwarf_bool_name (die->child != NULL));
11633
11634   print_spaces (indent, f);
11635   fprintf_unfiltered (f, "  attributes:\n");
11636
11637   for (i = 0; i < die->num_attrs; ++i)
11638     {
11639       print_spaces (indent, f);
11640       fprintf_unfiltered (f, "    %s (%s) ",
11641                dwarf_attr_name (die->attrs[i].name),
11642                dwarf_form_name (die->attrs[i].form));
11643
11644       switch (die->attrs[i].form)
11645         {
11646         case DW_FORM_ref_addr:
11647         case DW_FORM_addr:
11648           fprintf_unfiltered (f, "address: ");
11649           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
11650           break;
11651         case DW_FORM_block2:
11652         case DW_FORM_block4:
11653         case DW_FORM_block:
11654         case DW_FORM_block1:
11655           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
11656           break;
11657         case DW_FORM_exprloc:
11658           fprintf_unfiltered (f, "expression: size %u",
11659                               DW_BLOCK (&die->attrs[i])->size);
11660           break;
11661         case DW_FORM_ref1:
11662         case DW_FORM_ref2:
11663         case DW_FORM_ref4:
11664           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
11665                               (long) (DW_ADDR (&die->attrs[i])));
11666           break;
11667         case DW_FORM_data1:
11668         case DW_FORM_data2:
11669         case DW_FORM_data4:
11670         case DW_FORM_data8:
11671         case DW_FORM_udata:
11672         case DW_FORM_sdata:
11673           fprintf_unfiltered (f, "constant: %s",
11674                               pulongest (DW_UNSND (&die->attrs[i])));
11675           break;
11676         case DW_FORM_sec_offset:
11677           fprintf_unfiltered (f, "section offset: %s",
11678                               pulongest (DW_UNSND (&die->attrs[i])));
11679           break;
11680         case DW_FORM_sig8:
11681           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11682             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11683                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11684           else
11685             fprintf_unfiltered (f, "signatured type, offset: unknown");
11686           break;
11687         case DW_FORM_string:
11688         case DW_FORM_strp:
11689           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
11690                    DW_STRING (&die->attrs[i])
11691                    ? DW_STRING (&die->attrs[i]) : "",
11692                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
11693           break;
11694         case DW_FORM_flag:
11695           if (DW_UNSND (&die->attrs[i]))
11696             fprintf_unfiltered (f, "flag: TRUE");
11697           else
11698             fprintf_unfiltered (f, "flag: FALSE");
11699           break;
11700         case DW_FORM_flag_present:
11701           fprintf_unfiltered (f, "flag: TRUE");
11702           break;
11703         case DW_FORM_indirect:
11704           /* the reader will have reduced the indirect form to
11705              the "base form" so this form should not occur */
11706           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
11707           break;
11708         default:
11709           fprintf_unfiltered (f, "unsupported attribute form: %d.",
11710                    die->attrs[i].form);
11711           break;
11712         }
11713       fprintf_unfiltered (f, "\n");
11714     }
11715 }
11716
11717 static void
11718 dump_die_for_error (struct die_info *die)
11719 {
11720   dump_die_shallow (gdb_stderr, 0, die);
11721 }
11722
11723 static void
11724 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
11725 {
11726   int indent = level * 4;
11727
11728   gdb_assert (die != NULL);
11729
11730   if (level >= max_level)
11731     return;
11732
11733   dump_die_shallow (f, indent, die);
11734
11735   if (die->child != NULL)
11736     {
11737       print_spaces (indent, f);
11738       fprintf_unfiltered (f, "  Children:");
11739       if (level + 1 < max_level)
11740         {
11741           fprintf_unfiltered (f, "\n");
11742           dump_die_1 (f, level + 1, max_level, die->child);
11743         }
11744       else
11745         {
11746           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
11747         }
11748     }
11749
11750   if (die->sibling != NULL && level > 0)
11751     {
11752       dump_die_1 (f, level, max_level, die->sibling);
11753     }
11754 }
11755
11756 /* This is called from the pdie macro in gdbinit.in.
11757    It's not static so gcc will keep a copy callable from gdb.  */
11758
11759 void
11760 dump_die (struct die_info *die, int max_level)
11761 {
11762   dump_die_1 (gdb_stdlog, 0, max_level, die);
11763 }
11764
11765 static void
11766 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
11767 {
11768   void **slot;
11769
11770   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
11771
11772   *slot = die;
11773 }
11774
11775 static int
11776 is_ref_attr (struct attribute *attr)
11777 {
11778   switch (attr->form)
11779     {
11780     case DW_FORM_ref_addr:
11781     case DW_FORM_ref1:
11782     case DW_FORM_ref2:
11783     case DW_FORM_ref4:
11784     case DW_FORM_ref8:
11785     case DW_FORM_ref_udata:
11786       return 1;
11787     default:
11788       return 0;
11789     }
11790 }
11791
11792 static unsigned int
11793 dwarf2_get_ref_die_offset (struct attribute *attr)
11794 {
11795   if (is_ref_attr (attr))
11796     return DW_ADDR (attr);
11797
11798   complaint (&symfile_complaints,
11799              _("unsupported die ref attribute form: '%s'"),
11800              dwarf_form_name (attr->form));
11801   return 0;
11802 }
11803
11804 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
11805  * the value held by the attribute is not constant.  */
11806
11807 static LONGEST
11808 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
11809 {
11810   if (attr->form == DW_FORM_sdata)
11811     return DW_SND (attr);
11812   else if (attr->form == DW_FORM_udata
11813            || attr->form == DW_FORM_data1
11814            || attr->form == DW_FORM_data2
11815            || attr->form == DW_FORM_data4
11816            || attr->form == DW_FORM_data8)
11817     return DW_UNSND (attr);
11818   else
11819     {
11820       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
11821                  dwarf_form_name (attr->form));
11822       return default_value;
11823     }
11824 }
11825
11826 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
11827    unit and add it to our queue.
11828    The result is non-zero if PER_CU was queued, otherwise the result is zero
11829    meaning either PER_CU is already queued or it is already loaded.  */
11830
11831 static int
11832 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
11833                        struct dwarf2_per_cu_data *per_cu)
11834 {
11835   /* Mark the dependence relation so that we don't flush PER_CU
11836      too early.  */
11837   dwarf2_add_dependence (this_cu, per_cu);
11838
11839   /* If it's already on the queue, we have nothing to do.  */
11840   if (per_cu->queued)
11841     return 0;
11842
11843   /* If the compilation unit is already loaded, just mark it as
11844      used.  */
11845   if (per_cu->cu != NULL)
11846     {
11847       per_cu->cu->last_used = 0;
11848       return 0;
11849     }
11850
11851   /* Add it to the queue.  */
11852   queue_comp_unit (per_cu, this_cu->objfile);
11853
11854   return 1;
11855 }
11856
11857 /* Follow reference or signature attribute ATTR of SRC_DIE.
11858    On entry *REF_CU is the CU of SRC_DIE.
11859    On exit *REF_CU is the CU of the result.  */
11860
11861 static struct die_info *
11862 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
11863                        struct dwarf2_cu **ref_cu)
11864 {
11865   struct die_info *die;
11866
11867   if (is_ref_attr (attr))
11868     die = follow_die_ref (src_die, attr, ref_cu);
11869   else if (attr->form == DW_FORM_sig8)
11870     die = follow_die_sig (src_die, attr, ref_cu);
11871   else
11872     {
11873       dump_die_for_error (src_die);
11874       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
11875              (*ref_cu)->objfile->name);
11876     }
11877
11878   return die;
11879 }
11880
11881 /* Follow reference OFFSET.
11882    On entry *REF_CU is the CU of source DIE referencing OFFSET.
11883    On exit *REF_CU is the CU of the result.  */
11884
11885 static struct die_info *
11886 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
11887 {
11888   struct die_info temp_die;
11889   struct dwarf2_cu *target_cu, *cu = *ref_cu;
11890
11891   gdb_assert (cu->per_cu != NULL);
11892
11893   if (cu->per_cu->from_debug_types)
11894     {
11895       /* .debug_types CUs cannot reference anything outside their CU.
11896          If they need to, they have to reference a signatured type via
11897          DW_FORM_sig8.  */
11898       if (! offset_in_cu_p (&cu->header, offset))
11899         return NULL;
11900       target_cu = cu;
11901     }
11902   else if (! offset_in_cu_p (&cu->header, offset))
11903     {
11904       struct dwarf2_per_cu_data *per_cu;
11905
11906       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
11907
11908       /* If necessary, add it to the queue and load its DIEs.  */
11909       if (maybe_queue_comp_unit (cu, per_cu))
11910         load_full_comp_unit (per_cu, cu->objfile);
11911
11912       target_cu = per_cu->cu;
11913     }
11914   else
11915     target_cu = cu;
11916
11917   *ref_cu = target_cu;
11918   temp_die.offset = offset;
11919   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
11920 }
11921
11922 /* Follow reference attribute ATTR of SRC_DIE.
11923    On entry *REF_CU is the CU of SRC_DIE.
11924    On exit *REF_CU is the CU of the result.  */
11925
11926 static struct die_info *
11927 follow_die_ref (struct die_info *src_die, struct attribute *attr,
11928                 struct dwarf2_cu **ref_cu)
11929 {
11930   unsigned int offset = dwarf2_get_ref_die_offset (attr);
11931   struct dwarf2_cu *cu = *ref_cu;
11932   struct die_info *die;
11933
11934   die = follow_die_offset (offset, ref_cu);
11935   if (!die)
11936     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
11937            "at 0x%x [in module %s]"),
11938            offset, src_die->offset, cu->objfile->name);
11939
11940   return die;
11941 }
11942
11943 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
11944    value is intended for DW_OP_call*.  */
11945
11946 struct dwarf2_locexpr_baton
11947 dwarf2_fetch_die_location_block (unsigned int offset,
11948                                  struct dwarf2_per_cu_data *per_cu)
11949 {
11950   struct dwarf2_cu *cu = per_cu->cu;
11951   struct die_info *die;
11952   struct attribute *attr;
11953   struct dwarf2_locexpr_baton retval;
11954
11955   die = follow_die_offset (offset, &cu);
11956   if (!die)
11957     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
11958            offset, per_cu->cu->objfile->name);
11959
11960   attr = dwarf2_attr (die, DW_AT_location, cu);
11961   if (!attr)
11962     {
11963       /* DWARF: "If there is no such attribute, then there is no effect.".  */
11964
11965       retval.data = NULL;
11966       retval.size = 0;
11967     }
11968   else
11969     {
11970       if (!attr_form_is_block (attr))
11971         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
11972                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
11973                offset, per_cu->cu->objfile->name);
11974
11975       retval.data = DW_BLOCK (attr)->data;
11976       retval.size = DW_BLOCK (attr)->size;
11977     }
11978   retval.per_cu = cu->per_cu;
11979   return retval;
11980 }
11981
11982 /* Follow the signature attribute ATTR in SRC_DIE.
11983    On entry *REF_CU is the CU of SRC_DIE.
11984    On exit *REF_CU is the CU of the result.  */
11985
11986 static struct die_info *
11987 follow_die_sig (struct die_info *src_die, struct attribute *attr,
11988                 struct dwarf2_cu **ref_cu)
11989 {
11990   struct objfile *objfile = (*ref_cu)->objfile;
11991   struct die_info temp_die;
11992   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11993   struct dwarf2_cu *sig_cu;
11994   struct die_info *die;
11995
11996   /* sig_type will be NULL if the signatured type is missing from
11997      the debug info.  */
11998   if (sig_type == NULL)
11999     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12000              "at 0x%x [in module %s]"),
12001            src_die->offset, objfile->name);
12002
12003   /* If necessary, add it to the queue and load its DIEs.  */
12004
12005   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12006     read_signatured_type (objfile, sig_type);
12007
12008   gdb_assert (sig_type->per_cu.cu != NULL);
12009
12010   sig_cu = sig_type->per_cu.cu;
12011   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12012   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12013   if (die)
12014     {
12015       *ref_cu = sig_cu;
12016       return die;
12017     }
12018
12019   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12020          "at 0x%x [in module %s]"),
12021          sig_type->type_offset, src_die->offset, objfile->name);
12022 }
12023
12024 /* Given an offset of a signatured type, return its signatured_type.  */
12025
12026 static struct signatured_type *
12027 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12028 {
12029   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12030   unsigned int length, initial_length_size;
12031   unsigned int sig_offset;
12032   struct signatured_type find_entry, *type_sig;
12033
12034   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12035   sig_offset = (initial_length_size
12036                 + 2 /*version*/
12037                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12038                 + 1 /*address_size*/);
12039   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12040   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12041
12042   /* This is only used to lookup previously recorded types.
12043      If we didn't find it, it's our bug.  */
12044   gdb_assert (type_sig != NULL);
12045   gdb_assert (offset == type_sig->offset);
12046
12047   return type_sig;
12048 }
12049
12050 /* Read in signatured type at OFFSET and build its CU and die(s).  */
12051
12052 static void
12053 read_signatured_type_at_offset (struct objfile *objfile,
12054                                 unsigned int offset)
12055 {
12056   struct signatured_type *type_sig;
12057
12058   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12059
12060   /* We have the section offset, but we need the signature to do the
12061      hash table lookup.  */
12062   type_sig = lookup_signatured_type_at_offset (objfile, offset);
12063
12064   gdb_assert (type_sig->per_cu.cu == NULL);
12065
12066   read_signatured_type (objfile, type_sig);
12067
12068   gdb_assert (type_sig->per_cu.cu != NULL);
12069 }
12070
12071 /* Read in a signatured type and build its CU and DIEs.  */
12072
12073 static void
12074 read_signatured_type (struct objfile *objfile,
12075                       struct signatured_type *type_sig)
12076 {
12077   gdb_byte *types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12078   struct die_reader_specs reader_specs;
12079   struct dwarf2_cu *cu;
12080   ULONGEST signature;
12081   struct cleanup *back_to, *free_cu_cleanup;
12082   struct attribute *attr;
12083
12084   gdb_assert (type_sig->per_cu.cu == NULL);
12085
12086   cu = xmalloc (sizeof (struct dwarf2_cu));
12087   memset (cu, 0, sizeof (struct dwarf2_cu));
12088   obstack_init (&cu->comp_unit_obstack);
12089   cu->objfile = objfile;
12090   type_sig->per_cu.cu = cu;
12091   cu->per_cu = &type_sig->per_cu;
12092
12093   /* If an error occurs while loading, release our storage.  */
12094   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12095
12096   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12097                                         types_ptr, objfile->obfd);
12098   gdb_assert (signature == type_sig->signature);
12099
12100   cu->die_hash
12101     = htab_create_alloc_ex (cu->header.length / 12,
12102                             die_hash,
12103                             die_eq,
12104                             NULL,
12105                             &cu->comp_unit_obstack,
12106                             hashtab_obstack_allocate,
12107                             dummy_obstack_deallocate);
12108
12109   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12110   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12111
12112   init_cu_die_reader (&reader_specs, cu);
12113
12114   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12115                                     NULL /*parent*/);
12116
12117   /* We try not to read any attributes in this function, because not
12118      all objfiles needed for references have been loaded yet, and symbol
12119      table processing isn't initialized.  But we have to set the CU language,
12120      or we won't be able to build types correctly.  */
12121   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12122   if (attr)
12123     set_cu_language (DW_UNSND (attr), cu);
12124   else
12125     set_cu_language (language_minimal, cu);
12126
12127   do_cleanups (back_to);
12128
12129   /* We've successfully allocated this compilation unit.  Let our caller
12130      clean it up when finished with it.  */
12131   discard_cleanups (free_cu_cleanup);
12132
12133   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12134   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12135 }
12136
12137 /* Decode simple location descriptions.
12138    Given a pointer to a dwarf block that defines a location, compute
12139    the location and return the value.
12140
12141    NOTE drow/2003-11-18: This function is called in two situations
12142    now: for the address of static or global variables (partial symbols
12143    only) and for offsets into structures which are expected to be
12144    (more or less) constant.  The partial symbol case should go away,
12145    and only the constant case should remain.  That will let this
12146    function complain more accurately.  A few special modes are allowed
12147    without complaint for global variables (for instance, global
12148    register values and thread-local values).
12149
12150    A location description containing no operations indicates that the
12151    object is optimized out.  The return value is 0 for that case.
12152    FIXME drow/2003-11-16: No callers check for this case any more; soon all
12153    callers will only want a very basic result and this can become a
12154    complaint.
12155
12156    Note that stack[0] is unused except as a default error return.
12157    Note that stack overflow is not yet handled.  */
12158
12159 static CORE_ADDR
12160 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12161 {
12162   struct objfile *objfile = cu->objfile;
12163   int i;
12164   int size = blk->size;
12165   gdb_byte *data = blk->data;
12166   CORE_ADDR stack[64];
12167   int stacki;
12168   unsigned int bytes_read, unsnd;
12169   gdb_byte op;
12170
12171   i = 0;
12172   stacki = 0;
12173   stack[stacki] = 0;
12174
12175   while (i < size)
12176     {
12177       op = data[i++];
12178       switch (op)
12179         {
12180         case DW_OP_lit0:
12181         case DW_OP_lit1:
12182         case DW_OP_lit2:
12183         case DW_OP_lit3:
12184         case DW_OP_lit4:
12185         case DW_OP_lit5:
12186         case DW_OP_lit6:
12187         case DW_OP_lit7:
12188         case DW_OP_lit8:
12189         case DW_OP_lit9:
12190         case DW_OP_lit10:
12191         case DW_OP_lit11:
12192         case DW_OP_lit12:
12193         case DW_OP_lit13:
12194         case DW_OP_lit14:
12195         case DW_OP_lit15:
12196         case DW_OP_lit16:
12197         case DW_OP_lit17:
12198         case DW_OP_lit18:
12199         case DW_OP_lit19:
12200         case DW_OP_lit20:
12201         case DW_OP_lit21:
12202         case DW_OP_lit22:
12203         case DW_OP_lit23:
12204         case DW_OP_lit24:
12205         case DW_OP_lit25:
12206         case DW_OP_lit26:
12207         case DW_OP_lit27:
12208         case DW_OP_lit28:
12209         case DW_OP_lit29:
12210         case DW_OP_lit30:
12211         case DW_OP_lit31:
12212           stack[++stacki] = op - DW_OP_lit0;
12213           break;
12214
12215         case DW_OP_reg0:
12216         case DW_OP_reg1:
12217         case DW_OP_reg2:
12218         case DW_OP_reg3:
12219         case DW_OP_reg4:
12220         case DW_OP_reg5:
12221         case DW_OP_reg6:
12222         case DW_OP_reg7:
12223         case DW_OP_reg8:
12224         case DW_OP_reg9:
12225         case DW_OP_reg10:
12226         case DW_OP_reg11:
12227         case DW_OP_reg12:
12228         case DW_OP_reg13:
12229         case DW_OP_reg14:
12230         case DW_OP_reg15:
12231         case DW_OP_reg16:
12232         case DW_OP_reg17:
12233         case DW_OP_reg18:
12234         case DW_OP_reg19:
12235         case DW_OP_reg20:
12236         case DW_OP_reg21:
12237         case DW_OP_reg22:
12238         case DW_OP_reg23:
12239         case DW_OP_reg24:
12240         case DW_OP_reg25:
12241         case DW_OP_reg26:
12242         case DW_OP_reg27:
12243         case DW_OP_reg28:
12244         case DW_OP_reg29:
12245         case DW_OP_reg30:
12246         case DW_OP_reg31:
12247           stack[++stacki] = op - DW_OP_reg0;
12248           if (i < size)
12249             dwarf2_complex_location_expr_complaint ();
12250           break;
12251
12252         case DW_OP_regx:
12253           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12254           i += bytes_read;
12255           stack[++stacki] = unsnd;
12256           if (i < size)
12257             dwarf2_complex_location_expr_complaint ();
12258           break;
12259
12260         case DW_OP_addr:
12261           stack[++stacki] = read_address (objfile->obfd, &data[i],
12262                                           cu, &bytes_read);
12263           i += bytes_read;
12264           break;
12265
12266         case DW_OP_const1u:
12267           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12268           i += 1;
12269           break;
12270
12271         case DW_OP_const1s:
12272           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12273           i += 1;
12274           break;
12275
12276         case DW_OP_const2u:
12277           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12278           i += 2;
12279           break;
12280
12281         case DW_OP_const2s:
12282           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12283           i += 2;
12284           break;
12285
12286         case DW_OP_const4u:
12287           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12288           i += 4;
12289           break;
12290
12291         case DW_OP_const4s:
12292           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12293           i += 4;
12294           break;
12295
12296         case DW_OP_constu:
12297           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
12298                                                   &bytes_read);
12299           i += bytes_read;
12300           break;
12301
12302         case DW_OP_consts:
12303           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12304           i += bytes_read;
12305           break;
12306
12307         case DW_OP_dup:
12308           stack[stacki + 1] = stack[stacki];
12309           stacki++;
12310           break;
12311
12312         case DW_OP_plus:
12313           stack[stacki - 1] += stack[stacki];
12314           stacki--;
12315           break;
12316
12317         case DW_OP_plus_uconst:
12318           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12319           i += bytes_read;
12320           break;
12321
12322         case DW_OP_minus:
12323           stack[stacki - 1] -= stack[stacki];
12324           stacki--;
12325           break;
12326
12327         case DW_OP_deref:
12328           /* If we're not the last op, then we definitely can't encode
12329              this using GDB's address_class enum.  This is valid for partial
12330              global symbols, although the variable's address will be bogus
12331              in the psymtab.  */
12332           if (i < size)
12333             dwarf2_complex_location_expr_complaint ();
12334           break;
12335
12336         case DW_OP_GNU_push_tls_address:
12337           /* The top of the stack has the offset from the beginning
12338              of the thread control block at which the variable is located.  */
12339           /* Nothing should follow this operator, so the top of stack would
12340              be returned.  */
12341           /* This is valid for partial global symbols, but the variable's
12342              address will be bogus in the psymtab.  */
12343           if (i < size)
12344             dwarf2_complex_location_expr_complaint ();
12345           break;
12346
12347         case DW_OP_GNU_uninit:
12348           break;
12349
12350         default:
12351           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
12352                      dwarf_stack_op_name (op, 1));
12353           return (stack[stacki]);
12354         }
12355     }
12356   return (stack[stacki]);
12357 }
12358
12359 /* memory allocation interface */
12360
12361 static struct dwarf_block *
12362 dwarf_alloc_block (struct dwarf2_cu *cu)
12363 {
12364   struct dwarf_block *blk;
12365
12366   blk = (struct dwarf_block *)
12367     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
12368   return (blk);
12369 }
12370
12371 static struct abbrev_info *
12372 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
12373 {
12374   struct abbrev_info *abbrev;
12375
12376   abbrev = (struct abbrev_info *)
12377     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
12378   memset (abbrev, 0, sizeof (struct abbrev_info));
12379   return (abbrev);
12380 }
12381
12382 static struct die_info *
12383 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
12384 {
12385   struct die_info *die;
12386   size_t size = sizeof (struct die_info);
12387
12388   if (num_attrs > 1)
12389     size += (num_attrs - 1) * sizeof (struct attribute);
12390
12391   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
12392   memset (die, 0, sizeof (struct die_info));
12393   return (die);
12394 }
12395
12396 \f
12397 /* Macro support.  */
12398
12399
12400 /* Return the full name of file number I in *LH's file name table.
12401    Use COMP_DIR as the name of the current directory of the
12402    compilation.  The result is allocated using xmalloc; the caller is
12403    responsible for freeing it.  */
12404 static char *
12405 file_full_name (int file, struct line_header *lh, const char *comp_dir)
12406 {
12407   /* Is the file number a valid index into the line header's file name
12408      table?  Remember that file numbers start with one, not zero.  */
12409   if (1 <= file && file <= lh->num_file_names)
12410     {
12411       struct file_entry *fe = &lh->file_names[file - 1];
12412
12413       if (IS_ABSOLUTE_PATH (fe->name))
12414         return xstrdup (fe->name);
12415       else
12416         {
12417           const char *dir;
12418           int dir_len;
12419           char *full_name;
12420
12421           if (fe->dir_index)
12422             dir = lh->include_dirs[fe->dir_index - 1];
12423           else
12424             dir = comp_dir;
12425
12426           if (dir)
12427             {
12428               dir_len = strlen (dir);
12429               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12430               strcpy (full_name, dir);
12431               full_name[dir_len] = '/';
12432               strcpy (full_name + dir_len + 1, fe->name);
12433               return full_name;
12434             }
12435           else
12436             return xstrdup (fe->name);
12437         }
12438     }
12439   else
12440     {
12441       /* The compiler produced a bogus file number.  We can at least
12442          record the macro definitions made in the file, even if we
12443          won't be able to find the file by name.  */
12444       char fake_name[80];
12445
12446       sprintf (fake_name, "<bad macro file number %d>", file);
12447
12448       complaint (&symfile_complaints,
12449                  _("bad file number in macro information (%d)"),
12450                  file);
12451
12452       return xstrdup (fake_name);
12453     }
12454 }
12455
12456
12457 static struct macro_source_file *
12458 macro_start_file (int file, int line,
12459                   struct macro_source_file *current_file,
12460                   const char *comp_dir,
12461                   struct line_header *lh, struct objfile *objfile)
12462 {
12463   /* The full name of this source file.  */
12464   char *full_name = file_full_name (file, lh, comp_dir);
12465
12466   /* We don't create a macro table for this compilation unit
12467      at all until we actually get a filename.  */
12468   if (! pending_macros)
12469     pending_macros = new_macro_table (&objfile->objfile_obstack,
12470                                       objfile->macro_cache);
12471
12472   if (! current_file)
12473     /* If we have no current file, then this must be the start_file
12474        directive for the compilation unit's main source file.  */
12475     current_file = macro_set_main (pending_macros, full_name);
12476   else
12477     current_file = macro_include (current_file, line, full_name);
12478
12479   xfree (full_name);
12480
12481   return current_file;
12482 }
12483
12484
12485 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12486    followed by a null byte.  */
12487 static char *
12488 copy_string (const char *buf, int len)
12489 {
12490   char *s = xmalloc (len + 1);
12491
12492   memcpy (s, buf, len);
12493   s[len] = '\0';
12494   return s;
12495 }
12496
12497
12498 static const char *
12499 consume_improper_spaces (const char *p, const char *body)
12500 {
12501   if (*p == ' ')
12502     {
12503       complaint (&symfile_complaints,
12504                  _("macro definition contains spaces in formal argument list:\n`%s'"),
12505                  body);
12506
12507       while (*p == ' ')
12508         p++;
12509     }
12510
12511   return p;
12512 }
12513
12514
12515 static void
12516 parse_macro_definition (struct macro_source_file *file, int line,
12517                         const char *body)
12518 {
12519   const char *p;
12520
12521   /* The body string takes one of two forms.  For object-like macro
12522      definitions, it should be:
12523
12524         <macro name> " " <definition>
12525
12526      For function-like macro definitions, it should be:
12527
12528         <macro name> "() " <definition>
12529      or
12530         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12531
12532      Spaces may appear only where explicitly indicated, and in the
12533      <definition>.
12534
12535      The Dwarf 2 spec says that an object-like macro's name is always
12536      followed by a space, but versions of GCC around March 2002 omit
12537      the space when the macro's definition is the empty string.
12538
12539      The Dwarf 2 spec says that there should be no spaces between the
12540      formal arguments in a function-like macro's formal argument list,
12541      but versions of GCC around March 2002 include spaces after the
12542      commas.  */
12543
12544
12545   /* Find the extent of the macro name.  The macro name is terminated
12546      by either a space or null character (for an object-like macro) or
12547      an opening paren (for a function-like macro).  */
12548   for (p = body; *p; p++)
12549     if (*p == ' ' || *p == '(')
12550       break;
12551
12552   if (*p == ' ' || *p == '\0')
12553     {
12554       /* It's an object-like macro.  */
12555       int name_len = p - body;
12556       char *name = copy_string (body, name_len);
12557       const char *replacement;
12558
12559       if (*p == ' ')
12560         replacement = body + name_len + 1;
12561       else
12562         {
12563           dwarf2_macro_malformed_definition_complaint (body);
12564           replacement = body + name_len;
12565         }
12566
12567       macro_define_object (file, line, name, replacement);
12568
12569       xfree (name);
12570     }
12571   else if (*p == '(')
12572     {
12573       /* It's a function-like macro.  */
12574       char *name = copy_string (body, p - body);
12575       int argc = 0;
12576       int argv_size = 1;
12577       char **argv = xmalloc (argv_size * sizeof (*argv));
12578
12579       p++;
12580
12581       p = consume_improper_spaces (p, body);
12582
12583       /* Parse the formal argument list.  */
12584       while (*p && *p != ')')
12585         {
12586           /* Find the extent of the current argument name.  */
12587           const char *arg_start = p;
12588
12589           while (*p && *p != ',' && *p != ')' && *p != ' ')
12590             p++;
12591
12592           if (! *p || p == arg_start)
12593             dwarf2_macro_malformed_definition_complaint (body);
12594           else
12595             {
12596               /* Make sure argv has room for the new argument.  */
12597               if (argc >= argv_size)
12598                 {
12599                   argv_size *= 2;
12600                   argv = xrealloc (argv, argv_size * sizeof (*argv));
12601                 }
12602
12603               argv[argc++] = copy_string (arg_start, p - arg_start);
12604             }
12605
12606           p = consume_improper_spaces (p, body);
12607
12608           /* Consume the comma, if present.  */
12609           if (*p == ',')
12610             {
12611               p++;
12612
12613               p = consume_improper_spaces (p, body);
12614             }
12615         }
12616
12617       if (*p == ')')
12618         {
12619           p++;
12620
12621           if (*p == ' ')
12622             /* Perfectly formed definition, no complaints.  */
12623             macro_define_function (file, line, name,
12624                                    argc, (const char **) argv,
12625                                    p + 1);
12626           else if (*p == '\0')
12627             {
12628               /* Complain, but do define it.  */
12629               dwarf2_macro_malformed_definition_complaint (body);
12630               macro_define_function (file, line, name,
12631                                      argc, (const char **) argv,
12632                                      p);
12633             }
12634           else
12635             /* Just complain.  */
12636             dwarf2_macro_malformed_definition_complaint (body);
12637         }
12638       else
12639         /* Just complain.  */
12640         dwarf2_macro_malformed_definition_complaint (body);
12641
12642       xfree (name);
12643       {
12644         int i;
12645
12646         for (i = 0; i < argc; i++)
12647           xfree (argv[i]);
12648       }
12649       xfree (argv);
12650     }
12651   else
12652     dwarf2_macro_malformed_definition_complaint (body);
12653 }
12654
12655
12656 static void
12657 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12658                      char *comp_dir, bfd *abfd,
12659                      struct dwarf2_cu *cu)
12660 {
12661   gdb_byte *mac_ptr, *mac_end;
12662   struct macro_source_file *current_file = 0;
12663   enum dwarf_macinfo_record_type macinfo_type;
12664   int at_commandline;
12665
12666   dwarf2_read_section (dwarf2_per_objfile->objfile,
12667                        &dwarf2_per_objfile->macinfo);
12668   if (dwarf2_per_objfile->macinfo.buffer == NULL)
12669     {
12670       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
12671       return;
12672     }
12673
12674   /* First pass: Find the name of the base filename.
12675      This filename is needed in order to process all macros whose definition
12676      (or undefinition) comes from the command line.  These macros are defined
12677      before the first DW_MACINFO_start_file entry, and yet still need to be
12678      associated to the base file.
12679
12680      To determine the base file name, we scan the macro definitions until we
12681      reach the first DW_MACINFO_start_file entry.  We then initialize
12682      CURRENT_FILE accordingly so that any macro definition found before the
12683      first DW_MACINFO_start_file can still be associated to the base file.  */
12684
12685   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12686   mac_end = dwarf2_per_objfile->macinfo.buffer
12687     + dwarf2_per_objfile->macinfo.size;
12688
12689   do
12690     {
12691       /* Do we at least have room for a macinfo type byte?  */
12692       if (mac_ptr >= mac_end)
12693         {
12694           /* Complaint is printed during the second pass as GDB will probably
12695              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
12696           break;
12697         }
12698
12699       macinfo_type = read_1_byte (abfd, mac_ptr);
12700       mac_ptr++;
12701
12702       switch (macinfo_type)
12703         {
12704           /* A zero macinfo type indicates the end of the macro
12705              information.  */
12706         case 0:
12707           break;
12708
12709         case DW_MACINFO_define:
12710         case DW_MACINFO_undef:
12711           /* Only skip the data by MAC_PTR.  */
12712           {
12713             unsigned int bytes_read;
12714
12715             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12716             mac_ptr += bytes_read;
12717             read_string (abfd, mac_ptr, &bytes_read);
12718             mac_ptr += bytes_read;
12719           }
12720           break;
12721
12722         case DW_MACINFO_start_file:
12723           {
12724             unsigned int bytes_read;
12725             int line, file;
12726
12727             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12728             mac_ptr += bytes_read;
12729             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12730             mac_ptr += bytes_read;
12731
12732             current_file = macro_start_file (file, line, current_file, comp_dir,
12733                                              lh, cu->objfile);
12734           }
12735           break;
12736
12737         case DW_MACINFO_end_file:
12738           /* No data to skip by MAC_PTR.  */
12739           break;
12740
12741         case DW_MACINFO_vendor_ext:
12742           /* Only skip the data by MAC_PTR.  */
12743           {
12744             unsigned int bytes_read;
12745
12746             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12747             mac_ptr += bytes_read;
12748             read_string (abfd, mac_ptr, &bytes_read);
12749             mac_ptr += bytes_read;
12750           }
12751           break;
12752
12753         default:
12754           break;
12755         }
12756     } while (macinfo_type != 0 && current_file == NULL);
12757
12758   /* Second pass: Process all entries.
12759
12760      Use the AT_COMMAND_LINE flag to determine whether we are still processing
12761      command-line macro definitions/undefinitions.  This flag is unset when we
12762      reach the first DW_MACINFO_start_file entry.  */
12763
12764   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12765
12766   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
12767      GDB is still reading the definitions from command line.  First
12768      DW_MACINFO_start_file will need to be ignored as it was already executed
12769      to create CURRENT_FILE for the main source holding also the command line
12770      definitions.  On first met DW_MACINFO_start_file this flag is reset to
12771      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
12772
12773   at_commandline = 1;
12774
12775   do
12776     {
12777       /* Do we at least have room for a macinfo type byte?  */
12778       if (mac_ptr >= mac_end)
12779         {
12780           dwarf2_macros_too_long_complaint ();
12781           break;
12782         }
12783
12784       macinfo_type = read_1_byte (abfd, mac_ptr);
12785       mac_ptr++;
12786
12787       switch (macinfo_type)
12788         {
12789           /* A zero macinfo type indicates the end of the macro
12790              information.  */
12791         case 0:
12792           break;
12793
12794         case DW_MACINFO_define:
12795         case DW_MACINFO_undef:
12796           {
12797             unsigned int bytes_read;
12798             int line;
12799             char *body;
12800
12801             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12802             mac_ptr += bytes_read;
12803             body = read_string (abfd, mac_ptr, &bytes_read);
12804             mac_ptr += bytes_read;
12805
12806             if (! current_file)
12807               {
12808                 /* DWARF violation as no main source is present.  */
12809                 complaint (&symfile_complaints,
12810                            _("debug info with no main source gives macro %s "
12811                              "on line %d: %s"),
12812                            macinfo_type == DW_MACINFO_define ?
12813                              _("definition") :
12814                                macinfo_type == DW_MACINFO_undef ?
12815                                  _("undefinition") :
12816                                  _("something-or-other"), line, body);
12817                 break;
12818               }
12819             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12820               complaint (&symfile_complaints,
12821                          _("debug info gives %s macro %s with %s line %d: %s"),
12822                          at_commandline ? _("command-line") : _("in-file"),
12823                          macinfo_type == DW_MACINFO_define ?
12824                            _("definition") :
12825                              macinfo_type == DW_MACINFO_undef ?
12826                                _("undefinition") :
12827                                _("something-or-other"),
12828                          line == 0 ? _("zero") : _("non-zero"), line, body);
12829
12830             if (macinfo_type == DW_MACINFO_define)
12831               parse_macro_definition (current_file, line, body);
12832             else if (macinfo_type == DW_MACINFO_undef)
12833               macro_undef (current_file, line, body);
12834           }
12835           break;
12836
12837         case DW_MACINFO_start_file:
12838           {
12839             unsigned int bytes_read;
12840             int line, file;
12841
12842             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12843             mac_ptr += bytes_read;
12844             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12845             mac_ptr += bytes_read;
12846
12847             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12848               complaint (&symfile_complaints,
12849                          _("debug info gives source %d included "
12850                            "from %s at %s line %d"),
12851                          file, at_commandline ? _("command-line") : _("file"),
12852                          line == 0 ? _("zero") : _("non-zero"), line);
12853
12854             if (at_commandline)
12855               {
12856                 /* This DW_MACINFO_start_file was executed in the pass one.  */
12857                 at_commandline = 0;
12858               }
12859             else
12860               current_file = macro_start_file (file, line,
12861                                                current_file, comp_dir,
12862                                                lh, cu->objfile);
12863           }
12864           break;
12865
12866         case DW_MACINFO_end_file:
12867           if (! current_file)
12868             complaint (&symfile_complaints,
12869                        _("macro debug info has an unmatched `close_file' directive"));
12870           else
12871             {
12872               current_file = current_file->included_by;
12873               if (! current_file)
12874                 {
12875                   enum dwarf_macinfo_record_type next_type;
12876
12877                   /* GCC circa March 2002 doesn't produce the zero
12878                      type byte marking the end of the compilation
12879                      unit.  Complain if it's not there, but exit no
12880                      matter what.  */
12881
12882                   /* Do we at least have room for a macinfo type byte?  */
12883                   if (mac_ptr >= mac_end)
12884                     {
12885                       dwarf2_macros_too_long_complaint ();
12886                       return;
12887                     }
12888
12889                   /* We don't increment mac_ptr here, so this is just
12890                      a look-ahead.  */
12891                   next_type = read_1_byte (abfd, mac_ptr);
12892                   if (next_type != 0)
12893                     complaint (&symfile_complaints,
12894                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
12895
12896                   return;
12897                 }
12898             }
12899           break;
12900
12901         case DW_MACINFO_vendor_ext:
12902           {
12903             unsigned int bytes_read;
12904             int constant;
12905             char *string;
12906
12907             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12908             mac_ptr += bytes_read;
12909             string = read_string (abfd, mac_ptr, &bytes_read);
12910             mac_ptr += bytes_read;
12911
12912             /* We don't recognize any vendor extensions.  */
12913           }
12914           break;
12915         }
12916     } while (macinfo_type != 0);
12917 }
12918
12919 /* Check if the attribute's form is a DW_FORM_block*
12920    if so return true else false. */
12921 static int
12922 attr_form_is_block (struct attribute *attr)
12923 {
12924   return (attr == NULL ? 0 :
12925       attr->form == DW_FORM_block1
12926       || attr->form == DW_FORM_block2
12927       || attr->form == DW_FORM_block4
12928       || attr->form == DW_FORM_block
12929       || attr->form == DW_FORM_exprloc);
12930 }
12931
12932 /* Return non-zero if ATTR's value is a section offset --- classes
12933    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
12934    You may use DW_UNSND (attr) to retrieve such offsets.
12935
12936    Section 7.5.4, "Attribute Encodings", explains that no attribute
12937    may have a value that belongs to more than one of these classes; it
12938    would be ambiguous if we did, because we use the same forms for all
12939    of them.  */
12940 static int
12941 attr_form_is_section_offset (struct attribute *attr)
12942 {
12943   return (attr->form == DW_FORM_data4
12944           || attr->form == DW_FORM_data8
12945           || attr->form == DW_FORM_sec_offset);
12946 }
12947
12948
12949 /* Return non-zero if ATTR's value falls in the 'constant' class, or
12950    zero otherwise.  When this function returns true, you can apply
12951    dwarf2_get_attr_constant_value to it.
12952
12953    However, note that for some attributes you must check
12954    attr_form_is_section_offset before using this test.  DW_FORM_data4
12955    and DW_FORM_data8 are members of both the constant class, and of
12956    the classes that contain offsets into other debug sections
12957    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
12958    that, if an attribute's can be either a constant or one of the
12959    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
12960    taken as section offsets, not constants.  */
12961 static int
12962 attr_form_is_constant (struct attribute *attr)
12963 {
12964   switch (attr->form)
12965     {
12966     case DW_FORM_sdata:
12967     case DW_FORM_udata:
12968     case DW_FORM_data1:
12969     case DW_FORM_data2:
12970     case DW_FORM_data4:
12971     case DW_FORM_data8:
12972       return 1;
12973     default:
12974       return 0;
12975     }
12976 }
12977
12978 static void
12979 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
12980                              struct dwarf2_cu *cu)
12981 {
12982   if (attr_form_is_section_offset (attr)
12983       /* ".debug_loc" may not exist at all, or the offset may be outside
12984          the section.  If so, fall through to the complaint in the
12985          other branch.  */
12986       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
12987     {
12988       struct dwarf2_loclist_baton *baton;
12989
12990       baton = obstack_alloc (&cu->objfile->objfile_obstack,
12991                              sizeof (struct dwarf2_loclist_baton));
12992       baton->per_cu = cu->per_cu;
12993       gdb_assert (baton->per_cu);
12994
12995       dwarf2_read_section (dwarf2_per_objfile->objfile,
12996                            &dwarf2_per_objfile->loc);
12997
12998       /* We don't know how long the location list is, but make sure we
12999          don't run off the edge of the section.  */
13000       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13001       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13002       baton->base_address = cu->base_address;
13003       if (cu->base_known == 0)
13004         complaint (&symfile_complaints,
13005                    _("Location list used without specifying the CU base address."));
13006
13007       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13008       SYMBOL_LOCATION_BATON (sym) = baton;
13009     }
13010   else
13011     {
13012       struct dwarf2_locexpr_baton *baton;
13013
13014       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13015                              sizeof (struct dwarf2_locexpr_baton));
13016       baton->per_cu = cu->per_cu;
13017       gdb_assert (baton->per_cu);
13018
13019       if (attr_form_is_block (attr))
13020         {
13021           /* Note that we're just copying the block's data pointer
13022              here, not the actual data.  We're still pointing into the
13023              info_buffer for SYM's objfile; right now we never release
13024              that buffer, but when we do clean up properly this may
13025              need to change.  */
13026           baton->size = DW_BLOCK (attr)->size;
13027           baton->data = DW_BLOCK (attr)->data;
13028         }
13029       else
13030         {
13031           dwarf2_invalid_attrib_class_complaint ("location description",
13032                                                  SYMBOL_NATURAL_NAME (sym));
13033           baton->size = 0;
13034           baton->data = NULL;
13035         }
13036
13037       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13038       SYMBOL_LOCATION_BATON (sym) = baton;
13039     }
13040 }
13041
13042 /* Return the OBJFILE associated with the compilation unit CU.  If CU
13043    came from a separate debuginfo file, then the master objfile is
13044    returned.  */
13045
13046 struct objfile *
13047 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13048 {
13049   struct objfile *objfile = per_cu->objfile;
13050
13051   /* Return the master objfile, so that we can report and look up the
13052      correct file containing this variable.  */
13053   if (objfile->separate_debug_objfile_backlink)
13054     objfile = objfile->separate_debug_objfile_backlink;
13055
13056   return objfile;
13057 }
13058
13059 /* Return the address size given in the compilation unit header for CU.  */
13060
13061 CORE_ADDR
13062 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13063 {
13064   if (per_cu->cu)
13065     return per_cu->cu->header.addr_size;
13066   else
13067     {
13068       /* If the CU is not currently read in, we re-read its header.  */
13069       struct objfile *objfile = per_cu->objfile;
13070       struct dwarf2_per_objfile *per_objfile
13071         = objfile_data (objfile, dwarf2_objfile_data_key);
13072       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13073       struct comp_unit_head cu_header;
13074
13075       memset (&cu_header, 0, sizeof cu_header);
13076       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13077       return cu_header.addr_size;
13078     }
13079 }
13080
13081 /* Return the offset size given in the compilation unit header for CU.  */
13082
13083 int
13084 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13085 {
13086   if (per_cu->cu)
13087     return per_cu->cu->header.offset_size;
13088   else
13089     {
13090       /* If the CU is not currently read in, we re-read its header.  */
13091       struct objfile *objfile = per_cu->objfile;
13092       struct dwarf2_per_objfile *per_objfile
13093         = objfile_data (objfile, dwarf2_objfile_data_key);
13094       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13095       struct comp_unit_head cu_header;
13096
13097       memset (&cu_header, 0, sizeof cu_header);
13098       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13099       return cu_header.offset_size;
13100     }
13101 }
13102
13103 /* Return the text offset of the CU.  The returned offset comes from
13104    this CU's objfile.  If this objfile came from a separate debuginfo
13105    file, then the offset may be different from the corresponding
13106    offset in the parent objfile.  */
13107
13108 CORE_ADDR
13109 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13110 {
13111   struct objfile *objfile = per_cu->objfile;
13112
13113   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13114 }
13115
13116 /* Locate the .debug_info compilation unit from CU's objfile which contains
13117    the DIE at OFFSET.  Raises an error on failure.  */
13118
13119 static struct dwarf2_per_cu_data *
13120 dwarf2_find_containing_comp_unit (unsigned int offset,
13121                                   struct objfile *objfile)
13122 {
13123   struct dwarf2_per_cu_data *this_cu;
13124   int low, high;
13125
13126   low = 0;
13127   high = dwarf2_per_objfile->n_comp_units - 1;
13128   while (high > low)
13129     {
13130       int mid = low + (high - low) / 2;
13131
13132       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13133         high = mid;
13134       else
13135         low = mid + 1;
13136     }
13137   gdb_assert (low == high);
13138   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13139     {
13140       if (low == 0)
13141         error (_("Dwarf Error: could not find partial DIE containing "
13142                "offset 0x%lx [in module %s]"),
13143                (long) offset, bfd_get_filename (objfile->obfd));
13144
13145       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13146       return dwarf2_per_objfile->all_comp_units[low-1];
13147     }
13148   else
13149     {
13150       this_cu = dwarf2_per_objfile->all_comp_units[low];
13151       if (low == dwarf2_per_objfile->n_comp_units - 1
13152           && offset >= this_cu->offset + this_cu->length)
13153         error (_("invalid dwarf2 offset %u"), offset);
13154       gdb_assert (offset < this_cu->offset + this_cu->length);
13155       return this_cu;
13156     }
13157 }
13158
13159 /* Locate the compilation unit from OBJFILE which is located at exactly
13160    OFFSET.  Raises an error on failure.  */
13161
13162 static struct dwarf2_per_cu_data *
13163 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13164 {
13165   struct dwarf2_per_cu_data *this_cu;
13166
13167   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13168   if (this_cu->offset != offset)
13169     error (_("no compilation unit with offset %u."), offset);
13170   return this_cu;
13171 }
13172
13173 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
13174
13175 static struct dwarf2_cu *
13176 alloc_one_comp_unit (struct objfile *objfile)
13177 {
13178   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13179   cu->objfile = objfile;
13180   obstack_init (&cu->comp_unit_obstack);
13181   return cu;
13182 }
13183
13184 /* Release one cached compilation unit, CU.  We unlink it from the tree
13185    of compilation units, but we don't remove it from the read_in_chain;
13186    the caller is responsible for that.
13187    NOTE: DATA is a void * because this function is also used as a
13188    cleanup routine.  */
13189
13190 static void
13191 free_one_comp_unit (void *data)
13192 {
13193   struct dwarf2_cu *cu = data;
13194
13195   if (cu->per_cu != NULL)
13196     cu->per_cu->cu = NULL;
13197   cu->per_cu = NULL;
13198
13199   obstack_free (&cu->comp_unit_obstack, NULL);
13200
13201   xfree (cu);
13202 }
13203
13204 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13205    when we're finished with it.  We can't free the pointer itself, but be
13206    sure to unlink it from the cache.  Also release any associated storage
13207    and perform cache maintenance.
13208
13209    Only used during partial symbol parsing.  */
13210
13211 static void
13212 free_stack_comp_unit (void *data)
13213 {
13214   struct dwarf2_cu *cu = data;
13215
13216   obstack_free (&cu->comp_unit_obstack, NULL);
13217   cu->partial_dies = NULL;
13218
13219   if (cu->per_cu != NULL)
13220     {
13221       /* This compilation unit is on the stack in our caller, so we
13222          should not xfree it.  Just unlink it.  */
13223       cu->per_cu->cu = NULL;
13224       cu->per_cu = NULL;
13225
13226       /* If we had a per-cu pointer, then we may have other compilation
13227          units loaded, so age them now.  */
13228       age_cached_comp_units ();
13229     }
13230 }
13231
13232 /* Free all cached compilation units.  */
13233
13234 static void
13235 free_cached_comp_units (void *data)
13236 {
13237   struct dwarf2_per_cu_data *per_cu, **last_chain;
13238
13239   per_cu = dwarf2_per_objfile->read_in_chain;
13240   last_chain = &dwarf2_per_objfile->read_in_chain;
13241   while (per_cu != NULL)
13242     {
13243       struct dwarf2_per_cu_data *next_cu;
13244
13245       next_cu = per_cu->cu->read_in_chain;
13246
13247       free_one_comp_unit (per_cu->cu);
13248       *last_chain = next_cu;
13249
13250       per_cu = next_cu;
13251     }
13252 }
13253
13254 /* Increase the age counter on each cached compilation unit, and free
13255    any that are too old.  */
13256
13257 static void
13258 age_cached_comp_units (void)
13259 {
13260   struct dwarf2_per_cu_data *per_cu, **last_chain;
13261
13262   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13263   per_cu = dwarf2_per_objfile->read_in_chain;
13264   while (per_cu != NULL)
13265     {
13266       per_cu->cu->last_used ++;
13267       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13268         dwarf2_mark (per_cu->cu);
13269       per_cu = per_cu->cu->read_in_chain;
13270     }
13271
13272   per_cu = dwarf2_per_objfile->read_in_chain;
13273   last_chain = &dwarf2_per_objfile->read_in_chain;
13274   while (per_cu != NULL)
13275     {
13276       struct dwarf2_per_cu_data *next_cu;
13277
13278       next_cu = per_cu->cu->read_in_chain;
13279
13280       if (!per_cu->cu->mark)
13281         {
13282           free_one_comp_unit (per_cu->cu);
13283           *last_chain = next_cu;
13284         }
13285       else
13286         last_chain = &per_cu->cu->read_in_chain;
13287
13288       per_cu = next_cu;
13289     }
13290 }
13291
13292 /* Remove a single compilation unit from the cache.  */
13293
13294 static void
13295 free_one_cached_comp_unit (void *target_cu)
13296 {
13297   struct dwarf2_per_cu_data *per_cu, **last_chain;
13298
13299   per_cu = dwarf2_per_objfile->read_in_chain;
13300   last_chain = &dwarf2_per_objfile->read_in_chain;
13301   while (per_cu != NULL)
13302     {
13303       struct dwarf2_per_cu_data *next_cu;
13304
13305       next_cu = per_cu->cu->read_in_chain;
13306
13307       if (per_cu->cu == target_cu)
13308         {
13309           free_one_comp_unit (per_cu->cu);
13310           *last_chain = next_cu;
13311           break;
13312         }
13313       else
13314         last_chain = &per_cu->cu->read_in_chain;
13315
13316       per_cu = next_cu;
13317     }
13318 }
13319
13320 /* Release all extra memory associated with OBJFILE.  */
13321
13322 void
13323 dwarf2_free_objfile (struct objfile *objfile)
13324 {
13325   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13326
13327   if (dwarf2_per_objfile == NULL)
13328     return;
13329
13330   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
13331   free_cached_comp_units (NULL);
13332
13333   if (dwarf2_per_objfile->using_index)
13334     {
13335       int i;
13336
13337       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13338         {
13339           int j;
13340           struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13341
13342           if (!cu->v.quick->lines)
13343             continue;
13344
13345           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13346             {
13347               if (cu->v.quick->file_names)
13348                 xfree ((void *) cu->v.quick->file_names[j]);
13349               if (cu->v.quick->full_names)
13350                 xfree ((void *) cu->v.quick->full_names[j]);
13351             }
13352
13353           free_line_header (cu->v.quick->lines);
13354         }
13355     }
13356
13357   /* Everything else should be on the objfile obstack.  */
13358 }
13359
13360 /* A pair of DIE offset and GDB type pointer.  We store these
13361    in a hash table separate from the DIEs, and preserve them
13362    when the DIEs are flushed out of cache.  */
13363
13364 struct dwarf2_offset_and_type
13365 {
13366   unsigned int offset;
13367   struct type *type;
13368 };
13369
13370 /* Hash function for a dwarf2_offset_and_type.  */
13371
13372 static hashval_t
13373 offset_and_type_hash (const void *item)
13374 {
13375   const struct dwarf2_offset_and_type *ofs = item;
13376
13377   return ofs->offset;
13378 }
13379
13380 /* Equality function for a dwarf2_offset_and_type.  */
13381
13382 static int
13383 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13384 {
13385   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13386   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
13387
13388   return ofs_lhs->offset == ofs_rhs->offset;
13389 }
13390
13391 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
13392    table if necessary.  For convenience, return TYPE.
13393
13394    The DIEs reading must have careful ordering to:
13395     * Not cause infite loops trying to read in DIEs as a prerequisite for
13396       reading current DIE.
13397     * Not trying to dereference contents of still incompletely read in types
13398       while reading in other DIEs.
13399     * Enable referencing still incompletely read in types just by a pointer to
13400       the type without accessing its fields.
13401
13402    Therefore caller should follow these rules:
13403      * Try to fetch any prerequisite types we may need to build this DIE type
13404        before building the type and calling set_die_type.
13405      * After building typer call set_die_type for current DIE as soon as
13406        possible before fetching more types to complete the current type.
13407      * Make the type as complete as possible before fetching more types.  */
13408
13409 static struct type *
13410 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13411 {
13412   struct dwarf2_offset_and_type **slot, ofs;
13413
13414   /* For Ada types, make sure that the gnat-specific data is always
13415      initialized (if not already set).  There are a few types where
13416      we should not be doing so, because the type-specific area is
13417      already used to hold some other piece of info (eg: TYPE_CODE_FLT
13418      where the type-specific area is used to store the floatformat).
13419      But this is not a problem, because the gnat-specific information
13420      is actually not needed for these types.  */
13421   if (need_gnat_info (cu)
13422       && TYPE_CODE (type) != TYPE_CODE_FUNC
13423       && TYPE_CODE (type) != TYPE_CODE_FLT
13424       && !HAVE_GNAT_AUX_INFO (type))
13425     INIT_GNAT_SPECIFIC (type);
13426
13427   if (cu->type_hash == NULL)
13428     {
13429       gdb_assert (cu->per_cu != NULL);
13430       cu->per_cu->type_hash
13431         = htab_create_alloc_ex (cu->header.length / 24,
13432                                 offset_and_type_hash,
13433                                 offset_and_type_eq,
13434                                 NULL,
13435                                 &cu->objfile->objfile_obstack,
13436                                 hashtab_obstack_allocate,
13437                                 dummy_obstack_deallocate);
13438       cu->type_hash = cu->per_cu->type_hash;
13439     }
13440
13441   ofs.offset = die->offset;
13442   ofs.type = type;
13443   slot = (struct dwarf2_offset_and_type **)
13444     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
13445   if (*slot)
13446     complaint (&symfile_complaints,
13447                _("A problem internal to GDB: DIE 0x%x has type already set"),
13448                die->offset);
13449   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13450   **slot = ofs;
13451   return type;
13452 }
13453
13454 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13455    not have a saved type.  */
13456
13457 static struct type *
13458 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
13459 {
13460   struct dwarf2_offset_and_type *slot, ofs;
13461   htab_t type_hash = cu->type_hash;
13462
13463   if (type_hash == NULL)
13464     return NULL;
13465
13466   ofs.offset = die->offset;
13467   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13468   if (slot)
13469     return slot->type;
13470   else
13471     return NULL;
13472 }
13473
13474 /* Add a dependence relationship from CU to REF_PER_CU.  */
13475
13476 static void
13477 dwarf2_add_dependence (struct dwarf2_cu *cu,
13478                        struct dwarf2_per_cu_data *ref_per_cu)
13479 {
13480   void **slot;
13481
13482   if (cu->dependencies == NULL)
13483     cu->dependencies
13484       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13485                               NULL, &cu->comp_unit_obstack,
13486                               hashtab_obstack_allocate,
13487                               dummy_obstack_deallocate);
13488
13489   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13490   if (*slot == NULL)
13491     *slot = ref_per_cu;
13492 }
13493
13494 /* Subroutine of dwarf2_mark to pass to htab_traverse.
13495    Set the mark field in every compilation unit in the
13496    cache that we must keep because we are keeping CU.  */
13497
13498 static int
13499 dwarf2_mark_helper (void **slot, void *data)
13500 {
13501   struct dwarf2_per_cu_data *per_cu;
13502
13503   per_cu = (struct dwarf2_per_cu_data *) *slot;
13504   if (per_cu->cu->mark)
13505     return 1;
13506   per_cu->cu->mark = 1;
13507
13508   if (per_cu->cu->dependencies != NULL)
13509     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13510
13511   return 1;
13512 }
13513
13514 /* Set the mark field in CU and in every other compilation unit in the
13515    cache that we must keep because we are keeping CU.  */
13516
13517 static void
13518 dwarf2_mark (struct dwarf2_cu *cu)
13519 {
13520   if (cu->mark)
13521     return;
13522   cu->mark = 1;
13523   if (cu->dependencies != NULL)
13524     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
13525 }
13526
13527 static void
13528 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13529 {
13530   while (per_cu)
13531     {
13532       per_cu->cu->mark = 0;
13533       per_cu = per_cu->cu->read_in_chain;
13534     }
13535 }
13536
13537 /* Trivial hash function for partial_die_info: the hash value of a DIE
13538    is its offset in .debug_info for this objfile.  */
13539
13540 static hashval_t
13541 partial_die_hash (const void *item)
13542 {
13543   const struct partial_die_info *part_die = item;
13544
13545   return part_die->offset;
13546 }
13547
13548 /* Trivial comparison function for partial_die_info structures: two DIEs
13549    are equal if they have the same offset.  */
13550
13551 static int
13552 partial_die_eq (const void *item_lhs, const void *item_rhs)
13553 {
13554   const struct partial_die_info *part_die_lhs = item_lhs;
13555   const struct partial_die_info *part_die_rhs = item_rhs;
13556
13557   return part_die_lhs->offset == part_die_rhs->offset;
13558 }
13559
13560 static struct cmd_list_element *set_dwarf2_cmdlist;
13561 static struct cmd_list_element *show_dwarf2_cmdlist;
13562
13563 static void
13564 set_dwarf2_cmd (char *args, int from_tty)
13565 {
13566   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13567 }
13568
13569 static void
13570 show_dwarf2_cmd (char *args, int from_tty)
13571 {
13572   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13573 }
13574
13575 /* If section described by INFO was mmapped, munmap it now.  */
13576
13577 static void
13578 munmap_section_buffer (struct dwarf2_section_info *info)
13579 {
13580   if (info->was_mmapped)
13581     {
13582 #ifdef HAVE_MMAP
13583       intptr_t begin = (intptr_t) info->buffer;
13584       intptr_t map_begin = begin & ~(pagesize - 1);
13585       size_t map_length = info->size + begin - map_begin;
13586
13587       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13588 #else
13589       /* Without HAVE_MMAP, we should never be here to begin with.  */
13590       gdb_assert (0);
13591 #endif
13592     }
13593 }
13594
13595 /* munmap debug sections for OBJFILE, if necessary.  */
13596
13597 static void
13598 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
13599 {
13600   struct dwarf2_per_objfile *data = d;
13601
13602   munmap_section_buffer (&data->info);
13603   munmap_section_buffer (&data->abbrev);
13604   munmap_section_buffer (&data->line);
13605   munmap_section_buffer (&data->str);
13606   munmap_section_buffer (&data->macinfo);
13607   munmap_section_buffer (&data->ranges);
13608   munmap_section_buffer (&data->loc);
13609   munmap_section_buffer (&data->frame);
13610   munmap_section_buffer (&data->eh_frame);
13611   munmap_section_buffer (&data->gdb_index);
13612 }
13613
13614 \f
13615
13616 /* The contents of the hash table we create when building the string
13617    table.  */
13618 struct strtab_entry
13619 {
13620   offset_type offset;
13621   const char *str;
13622 };
13623
13624 /* Hash function for a strtab_entry.  */
13625 static hashval_t
13626 hash_strtab_entry (const void *e)
13627 {
13628   const struct strtab_entry *entry = e;
13629   return mapped_index_string_hash (entry->str);
13630 }
13631
13632 /* Equality function for a strtab_entry.  */
13633 static int
13634 eq_strtab_entry (const void *a, const void *b)
13635 {
13636   const struct strtab_entry *ea = a;
13637   const struct strtab_entry *eb = b;
13638   return !strcmp (ea->str, eb->str);
13639 }
13640
13641 /* Create a strtab_entry hash table.  */
13642 static htab_t
13643 create_strtab (void)
13644 {
13645   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13646                             xfree, xcalloc, xfree);
13647 }
13648
13649 /* Add a string to the constant pool.  Return the string's offset in
13650    host order.  */
13651 static offset_type
13652 add_string (htab_t table, struct obstack *cpool, const char *str)
13653 {
13654   void **slot;
13655   struct strtab_entry entry;
13656   struct strtab_entry *result;
13657
13658   entry.str = str;
13659   slot = htab_find_slot (table, &entry, INSERT);
13660   if (*slot)
13661     result = *slot;
13662   else
13663     {
13664       result = XNEW (struct strtab_entry);
13665       result->offset = obstack_object_size (cpool);
13666       result->str = str;
13667       obstack_grow_str0 (cpool, str);
13668       *slot = result;
13669     }
13670   return result->offset;
13671 }
13672
13673 /* An entry in the symbol table.  */
13674 struct symtab_index_entry
13675 {
13676   /* The name of the symbol.  */
13677   const char *name;
13678   /* The offset of the name in the constant pool.  */
13679   offset_type index_offset;
13680   /* A sorted vector of the indices of all the CUs that hold an object
13681      of this name.  */
13682   VEC (offset_type) *cu_indices;
13683 };
13684
13685 /* The symbol table.  This is a power-of-2-sized hash table.  */
13686 struct mapped_symtab
13687 {
13688   offset_type n_elements;
13689   offset_type size;
13690   struct symtab_index_entry **data;
13691 };
13692
13693 /* Hash function for a symtab_index_entry.  */
13694 static hashval_t
13695 hash_symtab_entry (const void *e)
13696 {
13697   const struct symtab_index_entry *entry = e;
13698   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13699                          sizeof (offset_type) * VEC_length (offset_type,
13700                                                             entry->cu_indices),
13701                          0);
13702 }
13703
13704 /* Equality function for a symtab_index_entry.  */
13705 static int
13706 eq_symtab_entry (const void *a, const void *b)
13707 {
13708   const struct symtab_index_entry *ea = a;
13709   const struct symtab_index_entry *eb = b;
13710   int len = VEC_length (offset_type, ea->cu_indices);
13711   if (len != VEC_length (offset_type, eb->cu_indices))
13712     return 0;
13713   return !memcmp (VEC_address (offset_type, ea->cu_indices),
13714                   VEC_address (offset_type, eb->cu_indices),
13715                   sizeof (offset_type) * len);
13716 }
13717
13718 /* Destroy a symtab_index_entry.  */
13719 static void
13720 delete_symtab_entry (void *p)
13721 {
13722   struct symtab_index_entry *entry = p;
13723   VEC_free (offset_type, entry->cu_indices);
13724   xfree (entry);
13725 }
13726
13727 /* Create a hash table holding symtab_index_entry objects.  */
13728 static htab_t
13729 create_index_table (void)
13730 {
13731   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
13732                             delete_symtab_entry, xcalloc, xfree);
13733 }
13734
13735 /* Create a new mapped symtab object.  */
13736 static struct mapped_symtab *
13737 create_mapped_symtab (void)
13738 {
13739   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
13740   symtab->n_elements = 0;
13741   symtab->size = 1024;
13742   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13743   return symtab;
13744 }
13745
13746 /* Destroy a mapped_symtab.  */
13747 static void
13748 cleanup_mapped_symtab (void *p)
13749 {
13750   struct mapped_symtab *symtab = p;
13751   /* The contents of the array are freed when the other hash table is
13752      destroyed.  */
13753   xfree (symtab->data);
13754   xfree (symtab);
13755 }
13756
13757 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
13758    the slot.  */
13759 static struct symtab_index_entry **
13760 find_slot (struct mapped_symtab *symtab, const char *name)
13761 {
13762   offset_type index, step, hash = mapped_index_string_hash (name);
13763
13764   index = hash & (symtab->size - 1);
13765   step = ((hash * 17) & (symtab->size - 1)) | 1;
13766
13767   for (;;)
13768     {
13769       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
13770         return &symtab->data[index];
13771       index = (index + step) & (symtab->size - 1);
13772     }
13773 }
13774
13775 /* Expand SYMTAB's hash table.  */
13776 static void
13777 hash_expand (struct mapped_symtab *symtab)
13778 {
13779   offset_type old_size = symtab->size;
13780   offset_type i;
13781   struct symtab_index_entry **old_entries = symtab->data;
13782
13783   symtab->size *= 2;
13784   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13785
13786   for (i = 0; i < old_size; ++i)
13787     {
13788       if (old_entries[i])
13789         {
13790           struct symtab_index_entry **slot = find_slot (symtab,
13791                                                         old_entries[i]->name);
13792           *slot = old_entries[i];
13793         }
13794     }
13795
13796   xfree (old_entries);
13797 }
13798
13799 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
13800    is the index of the CU in which the symbol appears.  */
13801 static void
13802 add_index_entry (struct mapped_symtab *symtab, const char *name,
13803                  offset_type cu_index)
13804 {
13805   struct symtab_index_entry **slot;
13806
13807   ++symtab->n_elements;
13808   if (4 * symtab->n_elements / 3 >= symtab->size)
13809     hash_expand (symtab);
13810
13811   slot = find_slot (symtab, name);
13812   if (!*slot)
13813     {
13814       *slot = XNEW (struct symtab_index_entry);
13815       (*slot)->name = name;
13816       (*slot)->cu_indices = NULL;
13817     }
13818   /* Don't push an index twice.  Due to how we add entries we only
13819      have to check the last one.  */ 
13820   if (VEC_empty (offset_type, (*slot)->cu_indices)
13821       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
13822     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
13823 }
13824
13825 /* Add a vector of indices to the constant pool.  */
13826 static offset_type
13827 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
13828                       struct symtab_index_entry *entry)
13829 {
13830   void **slot;
13831
13832   slot = htab_find_slot (index_table, entry, INSERT);
13833   if (!*slot)
13834     {
13835       offset_type len = VEC_length (offset_type, entry->cu_indices);
13836       offset_type val = MAYBE_SWAP (len);
13837       offset_type iter;
13838       int i;
13839
13840       *slot = entry;
13841       entry->index_offset = obstack_object_size (cpool);
13842
13843       obstack_grow (cpool, &val, sizeof (val));
13844       for (i = 0;
13845            VEC_iterate (offset_type, entry->cu_indices, i, iter);
13846            ++i)
13847         {
13848           val = MAYBE_SWAP (iter);
13849           obstack_grow (cpool, &val, sizeof (val));
13850         }
13851     }
13852   else
13853     {
13854       struct symtab_index_entry *old_entry = *slot;
13855       entry->index_offset = old_entry->index_offset;
13856       entry = old_entry;
13857     }
13858   return entry->index_offset;
13859 }
13860
13861 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
13862    constant pool entries going into the obstack CPOOL.  */
13863 static void
13864 write_hash_table (struct mapped_symtab *symtab,
13865                   struct obstack *output, struct obstack *cpool)
13866 {
13867   offset_type i;
13868   htab_t index_table;
13869   htab_t str_table;
13870
13871   index_table = create_index_table ();
13872   str_table = create_strtab ();
13873   /* We add all the index vectors to the constant pool first, to
13874      ensure alignment is ok.  */
13875   for (i = 0; i < symtab->size; ++i)
13876     {
13877       if (symtab->data[i])
13878         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
13879     }
13880
13881   /* Now write out the hash table.  */
13882   for (i = 0; i < symtab->size; ++i)
13883     {
13884       offset_type str_off, vec_off;
13885
13886       if (symtab->data[i])
13887         {
13888           str_off = add_string (str_table, cpool, symtab->data[i]->name);
13889           vec_off = symtab->data[i]->index_offset;
13890         }
13891       else
13892         {
13893           /* While 0 is a valid constant pool index, it is not valid
13894              to have 0 for both offsets.  */
13895           str_off = 0;
13896           vec_off = 0;
13897         }
13898
13899       str_off = MAYBE_SWAP (str_off);
13900       vec_off = MAYBE_SWAP (vec_off);
13901
13902       obstack_grow (output, &str_off, sizeof (str_off));
13903       obstack_grow (output, &vec_off, sizeof (vec_off));
13904     }
13905
13906   htab_delete (str_table);
13907   htab_delete (index_table);
13908 }
13909
13910 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
13911    from PST; CU_INDEX is the index of the CU in the vector of all
13912    CUs.  */
13913 static void
13914 add_address_entry (struct objfile *objfile,
13915                    struct obstack *addr_obstack, struct partial_symtab *pst,
13916                    unsigned int cu_index)
13917 {
13918   offset_type offset;
13919   char addr[8];
13920   CORE_ADDR baseaddr;
13921
13922   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13923
13924   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
13925   obstack_grow (addr_obstack, addr, 8);
13926   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
13927   obstack_grow (addr_obstack, addr, 8);
13928   offset = MAYBE_SWAP (cu_index);
13929   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
13930 }
13931
13932 /* Add a list of partial symbols to SYMTAB.  */
13933 static void
13934 write_psymbols (struct mapped_symtab *symtab,
13935                 struct partial_symbol **psymp,
13936                 int count,
13937                 offset_type cu_index)
13938 {
13939   for (; count-- > 0; ++psymp)
13940     {
13941       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
13942         error (_("Ada is not currently supported by the index"));
13943       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
13944     }
13945 }
13946
13947 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
13948    exception if there is an error.  */
13949 static void
13950 write_obstack (FILE *file, struct obstack *obstack)
13951 {
13952   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
13953               file)
13954       != obstack_object_size (obstack))
13955     error (_("couldn't data write to file"));
13956 }
13957
13958 /* Unlink a file if the argument is not NULL.  */
13959 static void
13960 unlink_if_set (void *p)
13961 {
13962   char **filename = p;
13963   if (*filename)
13964     unlink (*filename);
13965 }
13966
13967 /* Create an index file for OBJFILE in the directory DIR.  */
13968 static void
13969 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
13970 {
13971   struct cleanup *cleanup;
13972   char *filename, *cleanup_filename;
13973   struct obstack contents, addr_obstack, constant_pool, symtab_obstack, cu_list;
13974   int i;
13975   FILE *out_file;
13976   struct mapped_symtab *symtab;
13977   offset_type val, size_of_contents, total_len;
13978   struct stat st;
13979   char buf[8];
13980
13981   if (!objfile->psymtabs)
13982     return;
13983   if (dwarf2_per_objfile->using_index)
13984     error (_("Cannot use an index to create the index"));
13985
13986   if (stat (objfile->name, &st) < 0)
13987     perror_with_name (_("Could not stat"));
13988
13989   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
13990                      INDEX_SUFFIX, (char *) NULL);
13991   cleanup = make_cleanup (xfree, filename);
13992
13993   out_file = fopen (filename, "wb");
13994   if (!out_file)
13995     error (_("Can't open `%s' for writing"), filename);
13996
13997   cleanup_filename = filename;
13998   make_cleanup (unlink_if_set, &cleanup_filename);
13999
14000   symtab = create_mapped_symtab ();
14001   make_cleanup (cleanup_mapped_symtab, symtab);
14002
14003   obstack_init (&addr_obstack);
14004   make_cleanup_obstack_free (&addr_obstack);
14005
14006   obstack_init (&cu_list);
14007   make_cleanup_obstack_free (&cu_list);
14008
14009   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14010     {
14011       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14012       struct partial_symtab *psymtab = cu->v.psymtab;
14013       gdb_byte val[8];
14014
14015       write_psymbols (symtab,
14016                       objfile->global_psymbols.list + psymtab->globals_offset,
14017                       psymtab->n_global_syms, i);
14018       write_psymbols (symtab,
14019                       objfile->static_psymbols.list + psymtab->statics_offset,
14020                       psymtab->n_static_syms, i);
14021
14022       add_address_entry (objfile, &addr_obstack, psymtab, i);
14023
14024       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14025       obstack_grow (&cu_list, val, 8);
14026       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14027       obstack_grow (&cu_list, val, 8);
14028     }
14029
14030   obstack_init (&constant_pool);
14031   make_cleanup_obstack_free (&constant_pool);
14032   obstack_init (&symtab_obstack);
14033   make_cleanup_obstack_free (&symtab_obstack);
14034   write_hash_table (symtab, &symtab_obstack, &constant_pool);
14035
14036   obstack_init (&contents);
14037   make_cleanup_obstack_free (&contents);
14038   size_of_contents = 5 * sizeof (offset_type);
14039   total_len = size_of_contents;
14040
14041   /* The version number.  */
14042   val = MAYBE_SWAP (1);
14043   obstack_grow (&contents, &val, sizeof (val));
14044
14045   /* The offset of the CU list from the start of the file.  */
14046   val = MAYBE_SWAP (total_len);
14047   obstack_grow (&contents, &val, sizeof (val));
14048   total_len += obstack_object_size (&cu_list);
14049
14050   /* The offset of the address table from the start of the file.  */
14051   val = MAYBE_SWAP (total_len);
14052   obstack_grow (&contents, &val, sizeof (val));
14053   total_len += obstack_object_size (&addr_obstack);
14054
14055   /* The offset of the symbol table from the start of the file.  */
14056   val = MAYBE_SWAP (total_len);
14057   obstack_grow (&contents, &val, sizeof (val));
14058   total_len += obstack_object_size (&symtab_obstack);
14059
14060   /* The offset of the constant pool from the start of the file.  */
14061   val = MAYBE_SWAP (total_len);
14062   obstack_grow (&contents, &val, sizeof (val));
14063   total_len += obstack_object_size (&constant_pool);
14064
14065   gdb_assert (obstack_object_size (&contents) == size_of_contents);
14066
14067   write_obstack (out_file, &contents);
14068   write_obstack (out_file, &cu_list);
14069   write_obstack (out_file, &addr_obstack);
14070   write_obstack (out_file, &symtab_obstack);
14071   write_obstack (out_file, &constant_pool);
14072
14073   fclose (out_file);
14074
14075   /* We want to keep the file, so we set cleanup_filename to NULL
14076      here.  See unlink_if_set.  */
14077   cleanup_filename = NULL;
14078
14079   do_cleanups (cleanup);
14080 }
14081
14082 /* The mapped index file format is designed to be directly mmap()able
14083    on any architecture.  In most cases, a datum is represented using a
14084    little-endian 32-bit integer value, called an offset_type.  Big
14085    endian machines must byte-swap the values before using them.
14086    Exceptions to this rule are noted.  The data is laid out such that
14087    alignment is always respected.
14088
14089    A mapped index consists of several sections.
14090
14091    1. The file header.  This is a sequence of values, of offset_type
14092    unless otherwise noted:
14093    [0] The version number.  Currently 1.
14094    [1] The offset, from the start of the file, of the CU list.
14095    [2] The offset, from the start of the file, of the address section.
14096    [3] The offset, from the start of the file, of the symbol table.
14097    [4] The offset, from the start of the file, of the constant pool.
14098
14099    2. The CU list.  This is a sequence of pairs of 64-bit
14100    little-endian values.  The first element in each pair is the offset
14101    of a CU in the .debug_info section.  The second element in each
14102    pair is the length of that CU.  References to a CU elsewhere in the
14103    map are done using a CU index, which is just the 0-based index into
14104    this table.
14105
14106    3. The address section.  The address section consists of a sequence
14107    of address entries.  Each address entry has three elements.
14108    [0] The low address.  This is a 64-bit little-endian value.
14109    [1] The high address.  This is a 64-bit little-endian value.
14110    [2] The CU index.  This is an offset_type value.
14111
14112    4. The symbol table.  This is a hash table.  The size of the hash
14113    table is always a power of 2.  The initial hash and the step are
14114    currently defined by the `find_slot' function.
14115
14116    Each slot in the hash table consists of a pair of offset_type
14117    values.  The first value is the offset of the symbol's name in the
14118    constant pool.  The second value is the offset of the CU vector in
14119    the constant pool.
14120
14121    If both values are 0, then this slot in the hash table is empty.
14122    This is ok because while 0 is a valid constant pool index, it
14123    cannot be a valid index for both a string and a CU vector.
14124
14125    A string in the constant pool is stored as a \0-terminated string,
14126    as you'd expect.
14127
14128    A CU vector in the constant pool is a sequence of offset_type
14129    values.  The first value is the number of CU indices in the vector.
14130    Each subsequent value is the index of a CU in the CU list.  This
14131    element in the hash table is used to indicate which CUs define the
14132    symbol.
14133
14134    5. The constant pool.  This is simply a bunch of bytes.  It is
14135    organized so that alignment is correct: CU vectors are stored
14136    first, followed by strings.  */
14137 static void
14138 save_gdb_index_command (char *arg, int from_tty)
14139 {
14140   struct objfile *objfile;
14141
14142   if (!arg || !*arg)
14143     error (_("usage: maintenance save-gdb-index DIRECTORY"));
14144
14145   ALL_OBJFILES (objfile)
14146   {
14147     struct stat st;
14148
14149     /* If the objfile does not correspond to an actual file, skip it.  */
14150     if (stat (objfile->name, &st) < 0)
14151       continue;
14152
14153     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14154     if (dwarf2_per_objfile)
14155       {
14156         volatile struct gdb_exception except;
14157
14158         TRY_CATCH (except, RETURN_MASK_ERROR)
14159           {
14160             write_psymtabs_to_index (objfile, arg);
14161           }
14162         if (except.reason < 0)
14163           exception_fprintf (gdb_stderr, except,
14164                              _("Error while writing index for `%s': "),
14165                              objfile->name);
14166       }
14167   }
14168 }
14169
14170 \f
14171
14172 int dwarf2_always_disassemble;
14173
14174 static void
14175 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14176                                 struct cmd_list_element *c, const char *value)
14177 {
14178   fprintf_filtered (file, _("\
14179 Whether to always disassemble DWARF expressions is %s.\n"),
14180                     value);
14181 }
14182
14183 void _initialize_dwarf2_read (void);
14184
14185 void
14186 _initialize_dwarf2_read (void)
14187 {
14188   dwarf2_objfile_data_key
14189     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
14190
14191   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14192 Set DWARF 2 specific variables.\n\
14193 Configure DWARF 2 variables such as the cache size"),
14194                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14195                   0/*allow-unknown*/, &maintenance_set_cmdlist);
14196
14197   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14198 Show DWARF 2 specific variables\n\
14199 Show DWARF 2 variables such as the cache size"),
14200                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14201                   0/*allow-unknown*/, &maintenance_show_cmdlist);
14202
14203   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
14204                             &dwarf2_max_cache_age, _("\
14205 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14206 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14207 A higher limit means that cached compilation units will be stored\n\
14208 in memory longer, and more total memory will be used.  Zero disables\n\
14209 caching, which can slow down startup."),
14210                             NULL,
14211                             show_dwarf2_max_cache_age,
14212                             &set_dwarf2_cmdlist,
14213                             &show_dwarf2_cmdlist);
14214
14215   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14216                            &dwarf2_always_disassemble, _("\
14217 Set whether `info address' always disassembles DWARF expressions."), _("\
14218 Show whether `info address' always disassembles DWARF expressions."), _("\
14219 When enabled, DWARF expressions are always printed in an assembly-like\n\
14220 syntax.  When disabled, expressions will be printed in a more\n\
14221 conversational style, when possible."),
14222                            NULL,
14223                            show_dwarf2_always_disassemble,
14224                            &set_dwarf2_cmdlist,
14225                            &show_dwarf2_cmdlist);
14226
14227   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14228 Set debugging of the dwarf2 DIE reader."), _("\
14229 Show debugging of the dwarf2 DIE reader."), _("\
14230 When enabled (non-zero), DIEs are dumped after they are read in.\n\
14231 The value is the maximum depth to print."),
14232                             NULL,
14233                             NULL,
14234                             &setdebuglist, &showdebuglist);
14235
14236   add_cmd ("gdb-index", class_files, save_gdb_index_command,
14237            _("Save a .gdb-index file"),
14238            &save_cmdlist);
14239 }