OSDN Git Service

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