]> Git Repo - binutils.git/blob - gdb/dwarf2read.c
* dwarf2read.c (read_string): Rename to ...
[binutils.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 ([email protected]), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57
58 #include <fcntl.h>
59 #include "gdb_string.h"
60 #include "gdb_assert.h"
61 #include <sys/types.h>
62 #ifdef HAVE_ZLIB_H
63 #include <zlib.h>
64 #endif
65 #ifdef HAVE_MMAP
66 #include <sys/mman.h>
67 #ifndef MAP_FAILED
68 #define MAP_FAILED ((void *) -1)
69 #endif
70 #endif
71
72 #if 0
73 /* .debug_info header for a compilation unit
74    Because of alignment constraints, this structure has padding and cannot
75    be mapped directly onto the beginning of the .debug_info section.  */
76 typedef struct comp_unit_header
77   {
78     unsigned int length;        /* length of the .debug_info
79                                    contribution */
80     unsigned short version;     /* version number -- 2 for DWARF
81                                    version 2 */
82     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
83     unsigned char addr_size;    /* byte size of an address -- 4 */
84   }
85 _COMP_UNIT_HEADER;
86 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
87 #endif
88
89 /* .debug_line statement program prologue
90    Because of alignment constraints, this structure has padding and cannot
91    be mapped directly onto the beginning of the .debug_info section.  */
92 typedef struct statement_prologue
93   {
94     unsigned int total_length;  /* byte length of the statement
95                                    information */
96     unsigned short version;     /* version number -- 2 for DWARF
97                                    version 2 */
98     unsigned int prologue_length;       /* # bytes between prologue &
99                                            stmt program */
100     unsigned char minimum_instruction_length;   /* byte size of
101                                                    smallest instr */
102     unsigned char default_is_stmt;      /* initial value of is_stmt
103                                            register */
104     char line_base;
105     unsigned char line_range;
106     unsigned char opcode_base;  /* number assigned to first special
107                                    opcode */
108     unsigned char *standard_opcode_lengths;
109   }
110 _STATEMENT_PROLOGUE;
111
112 /* When non-zero, dump DIEs after they are read in.  */
113 static int dwarf2_die_debug = 0;
114
115 static int pagesize;
116
117 /* When set, the file that we're processing is known to have debugging
118    info for C++ namespaces.  GCC 3.3.x did not produce this information,
119    but later versions do.  */
120
121 static int processing_has_namespace_info;
122
123 static const struct objfile_data *dwarf2_objfile_data_key;
124
125 struct dwarf2_section_info
126 {
127   asection *asection;
128   gdb_byte *buffer;
129   bfd_size_type size;
130   int was_mmapped;
131   /* True if we have tried to read this section.  */
132   int readin;
133 };
134
135 /* All offsets in the index are of this type.  It must be
136    architecture-independent.  */
137 typedef uint32_t offset_type;
138
139 DEF_VEC_I (offset_type);
140
141 /* A description of the mapped index.  The file format is described in
142    a comment by the code that writes the index.  */
143 struct mapped_index
144 {
145   /* The total length of the buffer.  */
146   off_t total_size;
147   /* A pointer to the address table data.  */
148   const gdb_byte *address_table;
149   /* Size of the address table data in bytes.  */
150   offset_type address_table_size;
151   /* The hash table.  */
152   const offset_type *index_table;
153   /* Size in slots, each slot is 2 offset_types.  */
154   offset_type index_table_slots;
155   /* A pointer to the constant pool.  */
156   const char *constant_pool;
157 };
158
159 struct dwarf2_per_objfile
160 {
161   struct dwarf2_section_info info;
162   struct dwarf2_section_info abbrev;
163   struct dwarf2_section_info line;
164   struct dwarf2_section_info loc;
165   struct dwarf2_section_info macinfo;
166   struct dwarf2_section_info str;
167   struct dwarf2_section_info ranges;
168   struct dwarf2_section_info types;
169   struct dwarf2_section_info frame;
170   struct dwarf2_section_info eh_frame;
171   struct dwarf2_section_info gdb_index;
172
173   /* Back link.  */
174   struct objfile *objfile;
175
176   /* A list of all the compilation units.  This is used to locate
177      the target compilation unit of a particular reference.  */
178   struct dwarf2_per_cu_data **all_comp_units;
179
180   /* The number of compilation units in ALL_COMP_UNITS.  */
181   int n_comp_units;
182
183   /* The number of .debug_types-related CUs.  */
184   int n_type_comp_units;
185
186   /* The .debug_types-related CUs.  */
187   struct dwarf2_per_cu_data **type_comp_units;
188
189   /* A chain of compilation units that are currently read in, so that
190      they can be freed later.  */
191   struct dwarf2_per_cu_data *read_in_chain;
192
193   /* A table mapping .debug_types signatures to its signatured_type entry.
194      This is NULL if the .debug_types section hasn't been read in yet.  */
195   htab_t signatured_types;
196
197   /* A flag indicating wether this objfile has a section loaded at a
198      VMA of 0.  */
199   int has_section_at_zero;
200
201   /* True if we are using the mapped index.  */
202   unsigned char using_index;
203
204   /* The mapped index.  */
205   struct mapped_index *index_table;
206 };
207
208 static struct dwarf2_per_objfile *dwarf2_per_objfile;
209
210 /* names of the debugging sections */
211
212 /* Note that if the debugging section has been compressed, it might
213    have a name like .zdebug_info.  */
214
215 #define INFO_SECTION     "debug_info"
216 #define ABBREV_SECTION   "debug_abbrev"
217 #define LINE_SECTION     "debug_line"
218 #define LOC_SECTION      "debug_loc"
219 #define MACINFO_SECTION  "debug_macinfo"
220 #define STR_SECTION      "debug_str"
221 #define RANGES_SECTION   "debug_ranges"
222 #define TYPES_SECTION    "debug_types"
223 #define FRAME_SECTION    "debug_frame"
224 #define EH_FRAME_SECTION "eh_frame"
225 #define GDB_INDEX_SECTION "gdb_index"
226
227 /* local data types */
228
229 /* We hold several abbreviation tables in memory at the same time. */
230 #ifndef ABBREV_HASH_SIZE
231 #define ABBREV_HASH_SIZE 121
232 #endif
233
234 /* The data in a compilation unit header, after target2host
235    translation, looks like this.  */
236 struct comp_unit_head
237 {
238   unsigned int length;
239   short version;
240   unsigned char addr_size;
241   unsigned char signed_addr_p;
242   unsigned int abbrev_offset;
243
244   /* Size of file offsets; either 4 or 8.  */
245   unsigned int offset_size;
246
247   /* Size of the length field; either 4 or 12.  */
248   unsigned int initial_length_size;
249
250   /* Offset to the first byte of this compilation unit header in the
251      .debug_info section, for resolving relative reference dies.  */
252   unsigned int offset;
253
254   /* Offset to first die in this cu from the start of the cu.
255      This will be the first byte following the compilation unit header.  */
256   unsigned int first_die_offset;
257 };
258
259 /* Internal state when decoding a particular compilation unit.  */
260 struct dwarf2_cu
261 {
262   /* The objfile containing this compilation unit.  */
263   struct objfile *objfile;
264
265   /* The header of the compilation unit.  */
266   struct comp_unit_head header;
267
268   /* Base address of this compilation unit.  */
269   CORE_ADDR base_address;
270
271   /* Non-zero if base_address has been set.  */
272   int base_known;
273
274   struct function_range *first_fn, *last_fn, *cached_fn;
275
276   /* The language we are debugging.  */
277   enum language language;
278   const struct language_defn *language_defn;
279
280   const char *producer;
281
282   /* The generic symbol table building routines have separate lists for
283      file scope symbols and all all other scopes (local scopes).  So
284      we need to select the right one to pass to add_symbol_to_list().
285      We do it by keeping a pointer to the correct list in list_in_scope.
286
287      FIXME: The original dwarf code just treated the file scope as the
288      first local scope, and all other local scopes as nested local
289      scopes, and worked fine.  Check to see if we really need to
290      distinguish these in buildsym.c.  */
291   struct pending **list_in_scope;
292
293   /* DWARF abbreviation table associated with this compilation unit.  */
294   struct abbrev_info **dwarf2_abbrevs;
295
296   /* Storage for the abbrev table.  */
297   struct obstack abbrev_obstack;
298
299   /* Hash table holding all the loaded partial DIEs.  */
300   htab_t partial_dies;
301
302   /* Storage for things with the same lifetime as this read-in compilation
303      unit, including partial DIEs.  */
304   struct obstack comp_unit_obstack;
305
306   /* When multiple dwarf2_cu structures are living in memory, this field
307      chains them all together, so that they can be released efficiently.
308      We will probably also want a generation counter so that most-recently-used
309      compilation units are cached...  */
310   struct dwarf2_per_cu_data *read_in_chain;
311
312   /* Backchain to our per_cu entry if the tree has been built.  */
313   struct dwarf2_per_cu_data *per_cu;
314
315   /* Pointer to the die -> type map.  Although it is stored
316      permanently in per_cu, we copy it here to avoid double
317      indirection.  */
318   htab_t type_hash;
319
320   /* How many compilation units ago was this CU last referenced?  */
321   int last_used;
322
323   /* A hash table of die offsets for following references.  */
324   htab_t die_hash;
325
326   /* Full DIEs if read in.  */
327   struct die_info *dies;
328
329   /* A set of pointers to dwarf2_per_cu_data objects for compilation
330      units referenced by this one.  Only set during full symbol processing;
331      partial symbol tables do not have dependencies.  */
332   htab_t dependencies;
333
334   /* Header data from the line table, during full symbol processing.  */
335   struct line_header *line_header;
336
337   /* Mark used when releasing cached dies.  */
338   unsigned int mark : 1;
339
340   /* This flag will be set if this compilation unit might include
341      inter-compilation-unit references.  */
342   unsigned int has_form_ref_addr : 1;
343
344   /* This flag will be set if this compilation unit includes any
345      DW_TAG_namespace DIEs.  If we know that there are explicit
346      DIEs for namespaces, we don't need to try to infer them
347      from mangled names.  */
348   unsigned int has_namespace_info : 1;
349 };
350
351 /* When using the index (and thus not using psymtabs), each CU has an
352    object of this type.  This is used to hold information needed by
353    the various "quick" methods.  */
354 struct dwarf2_per_cu_quick_data
355 {
356   /* The line table.  This can be NULL if there was no line table.  */
357   struct line_header *lines;
358
359   /* The file names from the line table.  */
360   const char **file_names;
361   /* The file names from the line table after being run through
362      gdb_realpath.  */
363   const char **full_names;
364
365   /* The corresponding symbol table.  This is NULL if symbols for this
366      CU have not yet been read.  */
367   struct symtab *symtab;
368
369   /* A temporary mark bit used when iterating over all CUs in
370      expand_symtabs_matching.  */
371   unsigned int mark : 1;
372
373   /* True if we've tried to read the line table.  */
374   unsigned int read_lines : 1;
375 };
376
377 /* Persistent data held for a compilation unit, even when not
378    processing it.  We put a pointer to this structure in the
379    read_symtab_private field of the psymtab.  If we encounter
380    inter-compilation-unit references, we also maintain a sorted
381    list of all compilation units.  */
382
383 struct dwarf2_per_cu_data
384 {
385   /* The start offset and length of this compilation unit.  2**29-1
386      bytes should suffice to store the length of any compilation unit
387      - if it doesn't, GDB will fall over anyway.
388      NOTE: Unlike comp_unit_head.length, this length includes
389      initial_length_size.  */
390   unsigned int offset;
391   unsigned int length : 29;
392
393   /* Flag indicating this compilation unit will be read in before
394      any of the current compilation units are processed.  */
395   unsigned int queued : 1;
396
397   /* This flag will be set if we need to load absolutely all DIEs
398      for this compilation unit, instead of just the ones we think
399      are interesting.  It gets set if we look for a DIE in the
400      hash table and don't find it.  */
401   unsigned int load_all_dies : 1;
402
403   /* Non-zero if this CU is from .debug_types.
404      Otherwise it's from .debug_info.  */
405   unsigned int from_debug_types : 1;
406
407   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
408      of the CU cache it gets reset to NULL again.  */
409   struct dwarf2_cu *cu;
410
411   /* If full symbols for this CU have been read in, then this field
412      holds a map of DIE offsets to types.  It isn't always possible
413      to reconstruct this information later, so we have to preserve
414      it.  */
415   htab_t type_hash;
416
417   /* The corresponding objfile.  */
418   struct objfile *objfile;
419
420   /* When using partial symbol tables, the 'psymtab' field is active.
421      Otherwise the 'quick' field is active.  */
422   union
423   {
424     /* The partial symbol table associated with this compilation unit,
425        or NULL for partial units (which do not have an associated
426        symtab).  */
427     struct partial_symtab *psymtab;
428
429     /* Data needed by the "quick" functions.  */
430     struct dwarf2_per_cu_quick_data *quick;
431   } v;
432 };
433
434 /* Entry in the signatured_types hash table.  */
435
436 struct signatured_type
437 {
438   ULONGEST signature;
439
440   /* Offset in .debug_types of the TU (type_unit) for this type.  */
441   unsigned int offset;
442
443   /* Offset in .debug_types of the type defined by this TU.  */
444   unsigned int type_offset;
445
446   /* The CU(/TU) of this type.  */
447   struct dwarf2_per_cu_data per_cu;
448 };
449
450 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
451    which are used for both .debug_info and .debug_types dies.
452    All parameters here are unchanging for the life of the call.
453    This struct exists to abstract away the constant parameters of
454    die reading.  */
455
456 struct die_reader_specs
457 {
458   /* The bfd of this objfile.  */
459   bfd* abfd;
460
461   /* The CU of the DIE we are parsing.  */
462   struct dwarf2_cu *cu;
463
464   /* Pointer to start of section buffer.
465      This is either the start of .debug_info or .debug_types.  */
466   const gdb_byte *buffer;
467 };
468
469 /* The line number information for a compilation unit (found in the
470    .debug_line section) begins with a "statement program header",
471    which contains the following information.  */
472 struct line_header
473 {
474   unsigned int total_length;
475   unsigned short version;
476   unsigned int header_length;
477   unsigned char minimum_instruction_length;
478   unsigned char maximum_ops_per_instruction;
479   unsigned char default_is_stmt;
480   int line_base;
481   unsigned char line_range;
482   unsigned char opcode_base;
483
484   /* standard_opcode_lengths[i] is the number of operands for the
485      standard opcode whose value is i.  This means that
486      standard_opcode_lengths[0] is unused, and the last meaningful
487      element is standard_opcode_lengths[opcode_base - 1].  */
488   unsigned char *standard_opcode_lengths;
489
490   /* The include_directories table.  NOTE!  These strings are not
491      allocated with xmalloc; instead, they are pointers into
492      debug_line_buffer.  If you try to free them, `free' will get
493      indigestion.  */
494   unsigned int num_include_dirs, include_dirs_size;
495   char **include_dirs;
496
497   /* The file_names table.  NOTE!  These strings are not allocated
498      with xmalloc; instead, they are pointers into debug_line_buffer.
499      Don't try to free them directly.  */
500   unsigned int num_file_names, file_names_size;
501   struct file_entry
502   {
503     char *name;
504     unsigned int dir_index;
505     unsigned int mod_time;
506     unsigned int length;
507     int included_p; /* Non-zero if referenced by the Line Number Program.  */
508     struct symtab *symtab; /* The associated symbol table, if any.  */
509   } *file_names;
510
511   /* The start and end of the statement program following this
512      header.  These point into dwarf2_per_objfile->line_buffer.  */
513   gdb_byte *statement_program_start, *statement_program_end;
514 };
515
516 /* When we construct a partial symbol table entry we only
517    need this much information. */
518 struct partial_die_info
519   {
520     /* Offset of this DIE.  */
521     unsigned int offset;
522
523     /* DWARF-2 tag for this DIE.  */
524     ENUM_BITFIELD(dwarf_tag) tag : 16;
525
526     /* Assorted flags describing the data found in this DIE.  */
527     unsigned int has_children : 1;
528     unsigned int is_external : 1;
529     unsigned int is_declaration : 1;
530     unsigned int has_type : 1;
531     unsigned int has_specification : 1;
532     unsigned int has_pc_info : 1;
533
534     /* Flag set if the SCOPE field of this structure has been
535        computed.  */
536     unsigned int scope_set : 1;
537
538     /* Flag set if the DIE has a byte_size attribute.  */
539     unsigned int has_byte_size : 1;
540
541     /* The name of this DIE.  Normally the value of DW_AT_name, but
542        sometimes a default name for unnamed DIEs.  */
543     char *name;
544
545     /* The scope to prepend to our children.  This is generally
546        allocated on the comp_unit_obstack, so will disappear
547        when this compilation unit leaves the cache.  */
548     char *scope;
549
550     /* The location description associated with this DIE, if any.  */
551     struct dwarf_block *locdesc;
552
553     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
554     CORE_ADDR lowpc;
555     CORE_ADDR highpc;
556
557     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
558        DW_AT_sibling, if any.  */
559     gdb_byte *sibling;
560
561     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
562        DW_AT_specification (or DW_AT_abstract_origin or
563        DW_AT_extension).  */
564     unsigned int spec_offset;
565
566     /* Pointers to this DIE's parent, first child, and next sibling,
567        if any.  */
568     struct partial_die_info *die_parent, *die_child, *die_sibling;
569   };
570
571 /* This data structure holds the information of an abbrev. */
572 struct abbrev_info
573   {
574     unsigned int number;        /* number identifying abbrev */
575     enum dwarf_tag tag;         /* dwarf tag */
576     unsigned short has_children;                /* boolean */
577     unsigned short num_attrs;   /* number of attributes */
578     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
579     struct abbrev_info *next;   /* next in chain */
580   };
581
582 struct attr_abbrev
583   {
584     ENUM_BITFIELD(dwarf_attribute) name : 16;
585     ENUM_BITFIELD(dwarf_form) form : 16;
586   };
587
588 /* Attributes have a name and a value */
589 struct attribute
590   {
591     ENUM_BITFIELD(dwarf_attribute) name : 16;
592     ENUM_BITFIELD(dwarf_form) form : 15;
593
594     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
595        field should be in u.str (existing only for DW_STRING) but it is kept
596        here for better struct attribute alignment.  */
597     unsigned int string_is_canonical : 1;
598
599     union
600       {
601         char *str;
602         struct dwarf_block *blk;
603         ULONGEST unsnd;
604         LONGEST snd;
605         CORE_ADDR addr;
606         struct signatured_type *signatured_type;
607       }
608     u;
609   };
610
611 /* This data structure holds a complete die structure. */
612 struct die_info
613   {
614     /* DWARF-2 tag for this DIE.  */
615     ENUM_BITFIELD(dwarf_tag) tag : 16;
616
617     /* Number of attributes */
618     unsigned short num_attrs;
619
620     /* Abbrev number */
621     unsigned int abbrev;
622
623     /* Offset in .debug_info or .debug_types section.  */
624     unsigned int offset;
625
626     /* The dies in a compilation unit form an n-ary tree.  PARENT
627        points to this die's parent; CHILD points to the first child of
628        this node; and all the children of a given node are chained
629        together via their SIBLING fields, terminated by a die whose
630        tag is zero.  */
631     struct die_info *child;     /* Its first child, if any.  */
632     struct die_info *sibling;   /* Its next sibling, if any.  */
633     struct die_info *parent;    /* Its parent, if any.  */
634
635     /* An array of attributes, with NUM_ATTRS elements.  There may be
636        zero, but it's not common and zero-sized arrays are not
637        sufficiently portable C.  */
638     struct attribute attrs[1];
639   };
640
641 struct function_range
642 {
643   const char *name;
644   CORE_ADDR lowpc, highpc;
645   int seen_line;
646   struct function_range *next;
647 };
648
649 /* Get at parts of an attribute structure */
650
651 #define DW_STRING(attr)    ((attr)->u.str)
652 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
653 #define DW_UNSND(attr)     ((attr)->u.unsnd)
654 #define DW_BLOCK(attr)     ((attr)->u.blk)
655 #define DW_SND(attr)       ((attr)->u.snd)
656 #define DW_ADDR(attr)      ((attr)->u.addr)
657 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
658
659 /* Blocks are a bunch of untyped bytes. */
660 struct dwarf_block
661   {
662     unsigned int size;
663     gdb_byte *data;
664   };
665
666 #ifndef ATTR_ALLOC_CHUNK
667 #define ATTR_ALLOC_CHUNK 4
668 #endif
669
670 /* Allocate fields for structs, unions and enums in this size.  */
671 #ifndef DW_FIELD_ALLOC_CHUNK
672 #define DW_FIELD_ALLOC_CHUNK 4
673 #endif
674
675 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
676    but this would require a corresponding change in unpack_field_as_long
677    and friends.  */
678 static int bits_per_byte = 8;
679
680 /* The routines that read and process dies for a C struct or C++ class
681    pass lists of data member fields and lists of member function fields
682    in an instance of a field_info structure, as defined below.  */
683 struct field_info
684   {
685     /* List of data member and baseclasses fields. */
686     struct nextfield
687       {
688         struct nextfield *next;
689         int accessibility;
690         int virtuality;
691         struct field field;
692       }
693      *fields, *baseclasses;
694
695     /* Number of fields (including baseclasses).  */
696     int nfields;
697
698     /* Number of baseclasses.  */
699     int nbaseclasses;
700
701     /* Set if the accesibility of one of the fields is not public.  */
702     int non_public_fields;
703
704     /* Member function fields array, entries are allocated in the order they
705        are encountered in the object file.  */
706     struct nextfnfield
707       {
708         struct nextfnfield *next;
709         struct fn_field fnfield;
710       }
711      *fnfields;
712
713     /* Member function fieldlist array, contains name of possibly overloaded
714        member function, number of overloaded member functions and a pointer
715        to the head of the member function field chain.  */
716     struct fnfieldlist
717       {
718         char *name;
719         int length;
720         struct nextfnfield *head;
721       }
722      *fnfieldlists;
723
724     /* Number of entries in the fnfieldlists array.  */
725     int nfnfields;
726
727     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
728        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
729     struct typedef_field_list
730       {
731         struct typedef_field field;
732         struct typedef_field_list *next;
733       }
734     *typedef_field_list;
735     unsigned typedef_field_list_count;
736   };
737
738 /* One item on the queue of compilation units to read in full symbols
739    for.  */
740 struct dwarf2_queue_item
741 {
742   struct dwarf2_per_cu_data *per_cu;
743   struct dwarf2_queue_item *next;
744 };
745
746 /* The current queue.  */
747 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
748
749 /* Loaded secondary compilation units are kept in memory until they
750    have not been referenced for the processing of this many
751    compilation units.  Set this to zero to disable caching.  Cache
752    sizes of up to at least twenty will improve startup time for
753    typical inter-CU-reference binaries, at an obvious memory cost.  */
754 static int dwarf2_max_cache_age = 5;
755 static void
756 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
757                            struct cmd_list_element *c, const char *value)
758 {
759   fprintf_filtered (file, _("\
760 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
761                     value);
762 }
763
764
765 /* Various complaints about symbol reading that don't abort the process */
766
767 static void
768 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
769 {
770   complaint (&symfile_complaints,
771              _("statement list doesn't fit in .debug_line section"));
772 }
773
774 static void
775 dwarf2_debug_line_missing_file_complaint (void)
776 {
777   complaint (&symfile_complaints,
778              _(".debug_line section has line data without a file"));
779 }
780
781 static void
782 dwarf2_debug_line_missing_end_sequence_complaint (void)
783 {
784   complaint (&symfile_complaints,
785              _(".debug_line section has line program sequence without an end"));
786 }
787
788 static void
789 dwarf2_complex_location_expr_complaint (void)
790 {
791   complaint (&symfile_complaints, _("location expression too complex"));
792 }
793
794 static void
795 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
796                                               int arg3)
797 {
798   complaint (&symfile_complaints,
799              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
800              arg2, arg3);
801 }
802
803 static void
804 dwarf2_macros_too_long_complaint (void)
805 {
806   complaint (&symfile_complaints,
807              _("macro info runs off end of `.debug_macinfo' section"));
808 }
809
810 static void
811 dwarf2_macro_malformed_definition_complaint (const char *arg1)
812 {
813   complaint (&symfile_complaints,
814              _("macro debug info contains a malformed macro definition:\n`%s'"),
815              arg1);
816 }
817
818 static void
819 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
820 {
821   complaint (&symfile_complaints,
822              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
823 }
824
825 /* local function prototypes */
826
827 static void dwarf2_locate_sections (bfd *, asection *, void *);
828
829 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
830                                            struct objfile *);
831
832 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
833                                            struct die_info *,
834                                            struct partial_symtab *);
835
836 static void dwarf2_build_psymtabs_hard (struct objfile *);
837
838 static void scan_partial_symbols (struct partial_die_info *,
839                                   CORE_ADDR *, CORE_ADDR *,
840                                   int, struct dwarf2_cu *);
841
842 static void add_partial_symbol (struct partial_die_info *,
843                                 struct dwarf2_cu *);
844
845 static void add_partial_namespace (struct partial_die_info *pdi,
846                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
847                                    int need_pc, struct dwarf2_cu *cu);
848
849 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
850                                 CORE_ADDR *highpc, int need_pc,
851                                 struct dwarf2_cu *cu);
852
853 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
854                                      struct dwarf2_cu *cu);
855
856 static void add_partial_subprogram (struct partial_die_info *pdi,
857                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
858                                     int need_pc, struct dwarf2_cu *cu);
859
860 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
861                                      gdb_byte *buffer, gdb_byte *info_ptr,
862                                      bfd *abfd, struct dwarf2_cu *cu);
863
864 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
865
866 static void psymtab_to_symtab_1 (struct partial_symtab *);
867
868 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
869
870 static void dwarf2_free_abbrev_table (void *);
871
872 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
873                                             struct dwarf2_cu *);
874
875 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
876                                                  struct dwarf2_cu *);
877
878 static struct partial_die_info *load_partial_dies (bfd *,
879                                                    gdb_byte *, gdb_byte *,
880                                                    int, struct dwarf2_cu *);
881
882 static gdb_byte *read_partial_die (struct partial_die_info *,
883                                    struct abbrev_info *abbrev,
884                                    unsigned int, bfd *,
885                                    gdb_byte *, gdb_byte *,
886                                    struct dwarf2_cu *);
887
888 static struct partial_die_info *find_partial_die (unsigned int,
889                                                   struct dwarf2_cu *);
890
891 static void fixup_partial_die (struct partial_die_info *,
892                                struct dwarf2_cu *);
893
894 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
895                                  bfd *, gdb_byte *, struct dwarf2_cu *);
896
897 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
898                                        bfd *, gdb_byte *, struct dwarf2_cu *);
899
900 static unsigned int read_1_byte (bfd *, gdb_byte *);
901
902 static int read_1_signed_byte (bfd *, gdb_byte *);
903
904 static unsigned int read_2_bytes (bfd *, gdb_byte *);
905
906 static unsigned int read_4_bytes (bfd *, gdb_byte *);
907
908 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
909
910 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
911                                unsigned int *);
912
913 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
914
915 static LONGEST read_checked_initial_length_and_offset
916   (bfd *, gdb_byte *, const struct comp_unit_head *,
917    unsigned int *, unsigned int *);
918
919 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
920                             unsigned int *);
921
922 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
923
924 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
925
926 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
927
928 static char *read_indirect_string (bfd *, gdb_byte *,
929                                    const struct comp_unit_head *,
930                                    unsigned int *);
931
932 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
933
934 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
935
936 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
937
938 static void set_cu_language (unsigned int, struct dwarf2_cu *);
939
940 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
941                                       struct dwarf2_cu *);
942
943 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
944                                                 unsigned int,
945                                                 struct dwarf2_cu *);
946
947 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
948                                struct dwarf2_cu *cu);
949
950 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
951
952 static struct die_info *die_specification (struct die_info *die,
953                                            struct dwarf2_cu **);
954
955 static void free_line_header (struct line_header *lh);
956
957 static void add_file_name (struct line_header *, char *, unsigned int,
958                            unsigned int, unsigned int);
959
960 static struct line_header *(dwarf_decode_line_header
961                             (unsigned int offset,
962                              bfd *abfd, struct dwarf2_cu *cu));
963
964 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
965                                 struct dwarf2_cu *, struct partial_symtab *);
966
967 static void dwarf2_start_subfile (char *, char *, char *);
968
969 static struct symbol *new_symbol (struct die_info *, struct type *,
970                                   struct dwarf2_cu *);
971
972 static void dwarf2_const_value (struct attribute *, struct symbol *,
973                                 struct dwarf2_cu *);
974
975 static void dwarf2_const_value_data (struct attribute *attr,
976                                      struct symbol *sym,
977                                      int bits);
978
979 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
980
981 static int need_gnat_info (struct dwarf2_cu *);
982
983 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
984
985 static void set_descriptive_type (struct type *, struct die_info *,
986                                   struct dwarf2_cu *);
987
988 static struct type *die_containing_type (struct die_info *,
989                                          struct dwarf2_cu *);
990
991 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
992
993 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
994
995 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
996
997 static char *typename_concat (struct obstack *obs, const char *prefix,
998                               const char *suffix, int physname,
999                               struct dwarf2_cu *cu);
1000
1001 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1002
1003 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1004
1005 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1006
1007 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1008
1009 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1010                                struct dwarf2_cu *, struct partial_symtab *);
1011
1012 static int dwarf2_get_pc_bounds (struct die_info *,
1013                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1014                                  struct partial_symtab *);
1015
1016 static void get_scope_pc_bounds (struct die_info *,
1017                                  CORE_ADDR *, CORE_ADDR *,
1018                                  struct dwarf2_cu *);
1019
1020 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1021                                         CORE_ADDR, struct dwarf2_cu *);
1022
1023 static void dwarf2_add_field (struct field_info *, struct die_info *,
1024                               struct dwarf2_cu *);
1025
1026 static void dwarf2_attach_fields_to_type (struct field_info *,
1027                                           struct type *, struct dwarf2_cu *);
1028
1029 static void dwarf2_add_member_fn (struct field_info *,
1030                                   struct die_info *, struct type *,
1031                                   struct dwarf2_cu *);
1032
1033 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1034                                              struct type *, struct dwarf2_cu *);
1035
1036 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1037
1038 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1039
1040 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1041
1042 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1043
1044 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1045
1046 static struct type *read_module_type (struct die_info *die,
1047                                       struct dwarf2_cu *cu);
1048
1049 static const char *namespace_name (struct die_info *die,
1050                                    int *is_anonymous, struct dwarf2_cu *);
1051
1052 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1053
1054 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1055
1056 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1057                                                        struct dwarf2_cu *);
1058
1059 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1060
1061 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1062                                                  gdb_byte *info_ptr,
1063                                                  gdb_byte **new_info_ptr,
1064                                                  struct die_info *parent);
1065
1066 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1067                                                gdb_byte *info_ptr,
1068                                                gdb_byte **new_info_ptr,
1069                                                struct die_info *parent);
1070
1071 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1072                                                gdb_byte *info_ptr,
1073                                                gdb_byte **new_info_ptr,
1074                                                struct die_info *parent);
1075
1076 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1077                                 struct die_info **, gdb_byte *,
1078                                 int *);
1079
1080 static void process_die (struct die_info *, struct dwarf2_cu *);
1081
1082 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1083                                        struct obstack *);
1084
1085 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1086
1087 static struct die_info *dwarf2_extension (struct die_info *die,
1088                                           struct dwarf2_cu **);
1089
1090 static char *dwarf_tag_name (unsigned int);
1091
1092 static char *dwarf_attr_name (unsigned int);
1093
1094 static char *dwarf_form_name (unsigned int);
1095
1096 static char *dwarf_bool_name (unsigned int);
1097
1098 static char *dwarf_type_encoding_name (unsigned int);
1099
1100 #if 0
1101 static char *dwarf_cfi_name (unsigned int);
1102 #endif
1103
1104 static struct die_info *sibling_die (struct die_info *);
1105
1106 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1107
1108 static void dump_die_for_error (struct die_info *);
1109
1110 static void dump_die_1 (struct ui_file *, int level, int max_level,
1111                         struct die_info *);
1112
1113 /*static*/ void dump_die (struct die_info *, int max_level);
1114
1115 static void store_in_ref_table (struct die_info *,
1116                                 struct dwarf2_cu *);
1117
1118 static int is_ref_attr (struct attribute *);
1119
1120 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1121
1122 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1123
1124 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1125                                                struct attribute *,
1126                                                struct dwarf2_cu **);
1127
1128 static struct die_info *follow_die_ref (struct die_info *,
1129                                         struct attribute *,
1130                                         struct dwarf2_cu **);
1131
1132 static struct die_info *follow_die_sig (struct die_info *,
1133                                         struct attribute *,
1134                                         struct dwarf2_cu **);
1135
1136 static void read_signatured_type_at_offset (struct objfile *objfile,
1137                                             unsigned int offset);
1138
1139 static void read_signatured_type (struct objfile *,
1140                                   struct signatured_type *type_sig);
1141
1142 /* memory allocation interface */
1143
1144 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1145
1146 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1147
1148 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1149
1150 static void initialize_cu_func_list (struct dwarf2_cu *);
1151
1152 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1153                                  struct dwarf2_cu *);
1154
1155 static void dwarf_decode_macros (struct line_header *, unsigned int,
1156                                  char *, bfd *, struct dwarf2_cu *);
1157
1158 static int attr_form_is_block (struct attribute *);
1159
1160 static int attr_form_is_section_offset (struct attribute *);
1161
1162 static int attr_form_is_constant (struct attribute *);
1163
1164 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1165                                          struct symbol *sym,
1166                                          struct dwarf2_cu *cu);
1167
1168 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1169                                struct abbrev_info *abbrev,
1170                                struct dwarf2_cu *cu);
1171
1172 static void free_stack_comp_unit (void *);
1173
1174 static hashval_t partial_die_hash (const void *item);
1175
1176 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1177
1178 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1179   (unsigned int offset, struct objfile *objfile);
1180
1181 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1182   (unsigned int offset, struct objfile *objfile);
1183
1184 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1185
1186 static void free_one_comp_unit (void *);
1187
1188 static void free_cached_comp_units (void *);
1189
1190 static void age_cached_comp_units (void);
1191
1192 static void free_one_cached_comp_unit (void *);
1193
1194 static struct type *set_die_type (struct die_info *, struct type *,
1195                                   struct dwarf2_cu *);
1196
1197 static void create_all_comp_units (struct objfile *);
1198
1199 static int create_debug_types_hash_table (struct objfile *objfile);
1200
1201 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1202                                  struct objfile *);
1203
1204 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1205
1206 static void dwarf2_add_dependence (struct dwarf2_cu *,
1207                                    struct dwarf2_per_cu_data *);
1208
1209 static void dwarf2_mark (struct dwarf2_cu *);
1210
1211 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1212
1213 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1214
1215 static void dwarf2_release_queue (void *dummy);
1216
1217 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1218                              struct objfile *objfile);
1219
1220 static void process_queue (struct objfile *objfile);
1221
1222 static void find_file_and_directory (struct die_info *die,
1223                                      struct dwarf2_cu *cu,
1224                                      char **name, char **comp_dir);
1225
1226 static char *file_full_name (int file, struct line_header *lh,
1227                              const char *comp_dir);
1228
1229 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1230                                               gdb_byte *info_ptr,
1231                                               gdb_byte *buffer,
1232                                               unsigned int buffer_size,
1233                                               bfd *abfd);
1234
1235 static void init_cu_die_reader (struct die_reader_specs *reader,
1236                                 struct dwarf2_cu *cu);
1237
1238 static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1239
1240 #if WORDS_BIGENDIAN
1241
1242 /* Convert VALUE between big- and little-endian.  */
1243 static offset_type
1244 byte_swap (offset_type value)
1245 {
1246   offset_type result;
1247
1248   result = (value & 0xff) << 24;
1249   result |= (value & 0xff00) << 8;
1250   result |= (value & 0xff0000) >> 8;
1251   result |= (value & 0xff000000) >> 24;
1252   return result;
1253 }
1254
1255 #define MAYBE_SWAP(V)  byte_swap (V)
1256
1257 #else
1258 #define MAYBE_SWAP(V) (V)
1259 #endif /* WORDS_BIGENDIAN */
1260
1261 /* The suffix for an index file.  */
1262 #define INDEX_SUFFIX ".gdb-index"
1263
1264 /* Try to locate the sections we need for DWARF 2 debugging
1265    information and return true if we have enough to do something.  */
1266
1267 int
1268 dwarf2_has_info (struct objfile *objfile)
1269 {
1270   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1271   if (!dwarf2_per_objfile)
1272     {
1273       /* Initialize per-objfile state.  */
1274       struct dwarf2_per_objfile *data
1275         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1276
1277       memset (data, 0, sizeof (*data));
1278       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1279       dwarf2_per_objfile = data;
1280
1281       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1282       dwarf2_per_objfile->objfile = objfile;
1283     }
1284   return (dwarf2_per_objfile->info.asection != NULL
1285           && dwarf2_per_objfile->abbrev.asection != NULL);
1286 }
1287
1288 /* When loading sections, we can either look for ".<name>", or for
1289  * ".z<name>", which indicates a compressed section.  */
1290
1291 static int
1292 section_is_p (const char *section_name, const char *name)
1293 {
1294   return (section_name[0] == '.'
1295           && (strcmp (section_name + 1, name) == 0
1296               || (section_name[1] == 'z'
1297                   && strcmp (section_name + 2, name) == 0)));
1298 }
1299
1300 /* This function is mapped across the sections and remembers the
1301    offset and size of each of the debugging sections we are interested
1302    in.  */
1303
1304 static void
1305 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1306 {
1307   if (section_is_p (sectp->name, INFO_SECTION))
1308     {
1309       dwarf2_per_objfile->info.asection = sectp;
1310       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1311     }
1312   else if (section_is_p (sectp->name, ABBREV_SECTION))
1313     {
1314       dwarf2_per_objfile->abbrev.asection = sectp;
1315       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1316     }
1317   else if (section_is_p (sectp->name, LINE_SECTION))
1318     {
1319       dwarf2_per_objfile->line.asection = sectp;
1320       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1321     }
1322   else if (section_is_p (sectp->name, LOC_SECTION))
1323     {
1324       dwarf2_per_objfile->loc.asection = sectp;
1325       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1326     }
1327   else if (section_is_p (sectp->name, MACINFO_SECTION))
1328     {
1329       dwarf2_per_objfile->macinfo.asection = sectp;
1330       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1331     }
1332   else if (section_is_p (sectp->name, STR_SECTION))
1333     {
1334       dwarf2_per_objfile->str.asection = sectp;
1335       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1336     }
1337   else if (section_is_p (sectp->name, FRAME_SECTION))
1338     {
1339       dwarf2_per_objfile->frame.asection = sectp;
1340       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1341     }
1342   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1343     {
1344       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1345
1346       if (aflag & SEC_HAS_CONTENTS)
1347         {
1348           dwarf2_per_objfile->eh_frame.asection = sectp;
1349           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1350         }
1351     }
1352   else if (section_is_p (sectp->name, RANGES_SECTION))
1353     {
1354       dwarf2_per_objfile->ranges.asection = sectp;
1355       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1356     }
1357   else if (section_is_p (sectp->name, TYPES_SECTION))
1358     {
1359       dwarf2_per_objfile->types.asection = sectp;
1360       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1361     }
1362   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1363     {
1364       dwarf2_per_objfile->gdb_index.asection = sectp;
1365       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1366     }
1367
1368   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1369       && bfd_section_vma (abfd, sectp) == 0)
1370     dwarf2_per_objfile->has_section_at_zero = 1;
1371 }
1372
1373 /* Decompress a section that was compressed using zlib.  Store the
1374    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1375
1376 static void
1377 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1378                          gdb_byte **outbuf, bfd_size_type *outsize)
1379 {
1380   bfd *abfd = objfile->obfd;
1381 #ifndef HAVE_ZLIB_H
1382   error (_("Support for zlib-compressed DWARF data (from '%s') "
1383            "is disabled in this copy of GDB"),
1384          bfd_get_filename (abfd));
1385 #else
1386   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1387   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1388   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1389   bfd_size_type uncompressed_size;
1390   gdb_byte *uncompressed_buffer;
1391   z_stream strm;
1392   int rc;
1393   int header_size = 12;
1394
1395   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1396       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1397     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1398            bfd_get_filename (abfd));
1399
1400   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1401      by the uncompressed section size, 8 bytes in big-endian order.  */
1402   if (compressed_size < header_size
1403       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1404     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1405            bfd_get_filename (abfd));
1406   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1407   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1408   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1409   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1410   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1411   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1412   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1413   uncompressed_size += compressed_buffer[11];
1414
1415   /* It is possible the section consists of several compressed
1416      buffers concatenated together, so we uncompress in a loop.  */
1417   strm.zalloc = NULL;
1418   strm.zfree = NULL;
1419   strm.opaque = NULL;
1420   strm.avail_in = compressed_size - header_size;
1421   strm.next_in = (Bytef*) compressed_buffer + header_size;
1422   strm.avail_out = uncompressed_size;
1423   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1424                                        uncompressed_size);
1425   rc = inflateInit (&strm);
1426   while (strm.avail_in > 0)
1427     {
1428       if (rc != Z_OK)
1429         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1430                bfd_get_filename (abfd), rc);
1431       strm.next_out = ((Bytef*) uncompressed_buffer
1432                        + (uncompressed_size - strm.avail_out));
1433       rc = inflate (&strm, Z_FINISH);
1434       if (rc != Z_STREAM_END)
1435         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1436                bfd_get_filename (abfd), rc);
1437       rc = inflateReset (&strm);
1438     }
1439   rc = inflateEnd (&strm);
1440   if (rc != Z_OK
1441       || strm.avail_out != 0)
1442     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1443            bfd_get_filename (abfd), rc);
1444
1445   do_cleanups (cleanup);
1446   *outbuf = uncompressed_buffer;
1447   *outsize = uncompressed_size;
1448 #endif
1449 }
1450
1451 /* Read the contents of the section SECTP from object file specified by
1452    OBJFILE, store info about the section into INFO.
1453    If the section is compressed, uncompress it before returning.  */
1454
1455 static void
1456 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1457 {
1458   bfd *abfd = objfile->obfd;
1459   asection *sectp = info->asection;
1460   gdb_byte *buf, *retbuf;
1461   unsigned char header[4];
1462
1463   if (info->readin)
1464     return;
1465   info->buffer = NULL;
1466   info->was_mmapped = 0;
1467   info->readin = 1;
1468
1469   if (info->asection == NULL || info->size == 0)
1470     return;
1471
1472   /* Check if the file has a 4-byte header indicating compression.  */
1473   if (info->size > sizeof (header)
1474       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1475       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1476     {
1477       /* Upon decompression, update the buffer and its size.  */
1478       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1479         {
1480           zlib_decompress_section (objfile, sectp, &info->buffer,
1481                                    &info->size);
1482           return;
1483         }
1484     }
1485
1486 #ifdef HAVE_MMAP
1487   if (pagesize == 0)
1488     pagesize = getpagesize ();
1489
1490   /* Only try to mmap sections which are large enough: we don't want to
1491      waste space due to fragmentation.  Also, only try mmap for sections
1492      without relocations.  */
1493
1494   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1495     {
1496       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1497       size_t map_length = info->size + sectp->filepos - pg_offset;
1498       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1499                                  MAP_PRIVATE, pg_offset);
1500
1501       if (retbuf != MAP_FAILED)
1502         {
1503           info->was_mmapped = 1;
1504           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1505 #if HAVE_POSIX_MADVISE
1506           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1507 #endif
1508           return;
1509         }
1510     }
1511 #endif
1512
1513   /* If we get here, we are a normal, not-compressed section.  */
1514   info->buffer = buf
1515     = obstack_alloc (&objfile->objfile_obstack, info->size);
1516
1517   /* When debugging .o files, we may need to apply relocations; see
1518      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1519      We never compress sections in .o files, so we only need to
1520      try this when the section is not compressed.  */
1521   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1522   if (retbuf != NULL)
1523     {
1524       info->buffer = retbuf;
1525       return;
1526     }
1527
1528   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1529       || bfd_bread (buf, info->size, abfd) != info->size)
1530     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1531            bfd_get_filename (abfd));
1532 }
1533
1534 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1535    SECTION_NAME. */
1536
1537 void
1538 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1539                          asection **sectp, gdb_byte **bufp,
1540                          bfd_size_type *sizep)
1541 {
1542   struct dwarf2_per_objfile *data
1543     = objfile_data (objfile, dwarf2_objfile_data_key);
1544   struct dwarf2_section_info *info;
1545
1546   /* We may see an objfile without any DWARF, in which case we just
1547      return nothing.  */
1548   if (data == NULL)
1549     {
1550       *sectp = NULL;
1551       *bufp = NULL;
1552       *sizep = 0;
1553       return;
1554     }
1555   if (section_is_p (section_name, EH_FRAME_SECTION))
1556     info = &data->eh_frame;
1557   else if (section_is_p (section_name, FRAME_SECTION))
1558     info = &data->frame;
1559   else
1560     gdb_assert (0);
1561
1562   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1563     /* We haven't read this section in yet.  Do it now.  */
1564     dwarf2_read_section (objfile, info);
1565
1566   *sectp = info->asection;
1567   *bufp = info->buffer;
1568   *sizep = info->size;
1569 }
1570
1571 \f
1572
1573 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1574    this CU came.  */
1575 static void
1576 dw2_do_instantiate_symtab (struct objfile *objfile,
1577                            struct dwarf2_per_cu_data *per_cu)
1578 {
1579   struct cleanup *back_to;
1580
1581   back_to = make_cleanup (dwarf2_release_queue, NULL);
1582
1583   queue_comp_unit (per_cu, objfile);
1584
1585   if (per_cu->from_debug_types)
1586     read_signatured_type_at_offset (objfile, per_cu->offset);
1587   else
1588     load_full_comp_unit (per_cu, objfile);
1589
1590   process_queue (objfile);
1591
1592   /* Age the cache, releasing compilation units that have not
1593      been used recently.  */
1594   age_cached_comp_units ();
1595
1596   do_cleanups (back_to);
1597 }
1598
1599 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1600    the objfile from which this CU came.  Returns the resulting symbol
1601    table.  */
1602 static struct symtab *
1603 dw2_instantiate_symtab (struct objfile *objfile,
1604                         struct dwarf2_per_cu_data *per_cu)
1605 {
1606   if (!per_cu->v.quick->symtab)
1607     {
1608       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1609       increment_reading_symtab ();
1610       dw2_do_instantiate_symtab (objfile, per_cu);
1611       do_cleanups (back_to);
1612     }
1613   return per_cu->v.quick->symtab;
1614 }
1615
1616 /* Return the CU given its index.  */
1617 static struct dwarf2_per_cu_data *
1618 dw2_get_cu (int index)
1619 {
1620   if (index >= dwarf2_per_objfile->n_comp_units)
1621     {
1622       index -= dwarf2_per_objfile->n_comp_units;
1623       return dwarf2_per_objfile->type_comp_units[index];
1624     }
1625   return dwarf2_per_objfile->all_comp_units[index];
1626 }
1627
1628 /* A helper function that knows how to read a 64-bit value in a way
1629    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1630    otherwise.  */
1631 static int
1632 extract_cu_value (const char *bytes, ULONGEST *result)
1633 {
1634   if (sizeof (ULONGEST) < 8)
1635     {
1636       int i;
1637
1638       /* Ignore the upper 4 bytes if they are all zero.  */
1639       for (i = 0; i < 4; ++i)
1640         if (bytes[i + 4] != 0)
1641           return 0;
1642
1643       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1644     }
1645   else
1646     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1647   return 1;
1648 }
1649
1650 /* Read the CU list from the mapped index, and use it to create all
1651    the CU objects for this objfile.  Return 0 if something went wrong,
1652    1 if everything went ok.  */
1653 static int
1654 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1655                        offset_type cu_list_elements)
1656 {
1657   offset_type i;
1658
1659   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1660   dwarf2_per_objfile->all_comp_units
1661     = obstack_alloc (&objfile->objfile_obstack,
1662                      dwarf2_per_objfile->n_comp_units
1663                      * sizeof (struct dwarf2_per_cu_data *));
1664
1665   for (i = 0; i < cu_list_elements; i += 2)
1666     {
1667       struct dwarf2_per_cu_data *the_cu;
1668       ULONGEST offset, length;
1669
1670       if (!extract_cu_value (cu_list, &offset)
1671           || !extract_cu_value (cu_list + 8, &length))
1672         return 0;
1673       cu_list += 2 * 8;
1674
1675       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1676                                struct dwarf2_per_cu_data);
1677       the_cu->offset = offset;
1678       the_cu->length = length;
1679       the_cu->objfile = objfile;
1680       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1681                                         struct dwarf2_per_cu_quick_data);
1682       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1683     }
1684
1685   return 1;
1686 }
1687
1688 /* Create the signatured type hash table from the index.  */
1689 static int
1690 create_signatured_type_hash_from_index (struct objfile *objfile,
1691                                         const gdb_byte *bytes,
1692                                         offset_type elements)
1693 {
1694   offset_type i;
1695   htab_t type_hash;
1696
1697   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1698   dwarf2_per_objfile->type_comp_units
1699     = obstack_alloc (&objfile->objfile_obstack,
1700                      dwarf2_per_objfile->n_type_comp_units
1701                      * sizeof (struct dwarf2_per_cu_data *));
1702
1703   type_hash = allocate_signatured_type_hash_table (objfile);
1704
1705   for (i = 0; i < elements; i += 3)
1706     {
1707       struct signatured_type *type_sig;
1708       ULONGEST offset, type_offset, signature;
1709       void **slot;
1710
1711       if (!extract_cu_value (bytes, &offset)
1712           || !extract_cu_value (bytes + 8, &type_offset))
1713         return 0;
1714       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1715       bytes += 3 * 8;
1716
1717       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1718                                  struct signatured_type);
1719       type_sig->signature = signature;
1720       type_sig->offset = offset;
1721       type_sig->type_offset = type_offset;
1722       type_sig->per_cu.from_debug_types = 1;
1723       type_sig->per_cu.offset = offset;
1724       type_sig->per_cu.objfile = objfile;
1725       type_sig->per_cu.v.quick
1726         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1727                           struct dwarf2_per_cu_quick_data);
1728
1729       slot = htab_find_slot (type_hash, type_sig, INSERT);
1730       *slot = type_sig;
1731
1732       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1733     }
1734
1735   dwarf2_per_objfile->signatured_types = type_hash;
1736
1737   return 1;
1738 }
1739
1740 /* Read the address map data from the mapped index, and use it to
1741    populate the objfile's psymtabs_addrmap.  */
1742 static void
1743 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1744 {
1745   const gdb_byte *iter, *end;
1746   struct obstack temp_obstack;
1747   struct addrmap *mutable_map;
1748   struct cleanup *cleanup;
1749   CORE_ADDR baseaddr;
1750
1751   obstack_init (&temp_obstack);
1752   cleanup = make_cleanup_obstack_free (&temp_obstack);
1753   mutable_map = addrmap_create_mutable (&temp_obstack);
1754
1755   iter = index->address_table;
1756   end = iter + index->address_table_size;
1757
1758   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1759
1760   while (iter < end)
1761     {
1762       ULONGEST hi, lo, cu_index;
1763       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1764       iter += 8;
1765       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1766       iter += 8;
1767       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1768       iter += 4;
1769       
1770       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1771                          dw2_get_cu (cu_index));
1772     }
1773
1774   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1775                                                     &objfile->objfile_obstack);
1776   do_cleanups (cleanup);
1777 }
1778
1779 /* The hash function for strings in the mapped index.  This is the
1780    same as the hashtab.c hash function, but we keep a separate copy to
1781    maintain control over the implementation.  This is necessary
1782    because the hash function is tied to the format of the mapped index
1783    file.  */
1784 static hashval_t
1785 mapped_index_string_hash (const void *p)
1786 {
1787   const unsigned char *str = (const unsigned char *) p;
1788   hashval_t r = 0;
1789   unsigned char c;
1790
1791   while ((c = *str++) != 0)
1792     r = r * 67 + c - 113;
1793
1794   return r;
1795 }
1796
1797 /* Find a slot in the mapped index INDEX for the object named NAME.
1798    If NAME is found, set *VEC_OUT to point to the CU vector in the
1799    constant pool and return 1.  If NAME cannot be found, return 0.  */
1800 static int
1801 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1802                           offset_type **vec_out)
1803 {
1804   offset_type hash = mapped_index_string_hash (name);
1805   offset_type slot, step;
1806
1807   slot = hash & (index->index_table_slots - 1);
1808   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1809
1810   for (;;)
1811     {
1812       /* Convert a slot number to an offset into the table.  */
1813       offset_type i = 2 * slot;
1814       const char *str;
1815       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1816         return 0;
1817
1818       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1819       if (!strcmp (name, str))
1820         {
1821           *vec_out = (offset_type *) (index->constant_pool
1822                                       + MAYBE_SWAP (index->index_table[i + 1]));
1823           return 1;
1824         }
1825
1826       slot = (slot + step) & (index->index_table_slots - 1);
1827     }
1828 }
1829
1830 /* Read the index file.  If everything went ok, initialize the "quick"
1831    elements of all the CUs and return 1.  Otherwise, return 0.  */
1832 static int
1833 dwarf2_read_index (struct objfile *objfile)
1834 {
1835   char *addr;
1836   struct mapped_index *map;
1837   offset_type *metadata;
1838   const gdb_byte *cu_list;
1839   const gdb_byte *types_list = NULL;
1840   offset_type version, cu_list_elements;
1841   offset_type types_list_elements = 0;
1842   int i;
1843
1844   if (dwarf2_per_objfile->gdb_index.asection == NULL
1845       || dwarf2_per_objfile->gdb_index.size == 0)
1846     return 0;
1847   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1848
1849   addr = dwarf2_per_objfile->gdb_index.buffer;
1850   /* Version check.  */
1851   version = MAYBE_SWAP (*(offset_type *) addr);
1852   if (version == 1)
1853     {
1854       /* Index version 1 neglected to account for .debug_types.  So,
1855          if we see .debug_types, we cannot use this index.  */
1856       if (dwarf2_per_objfile->types.asection != NULL
1857           && dwarf2_per_objfile->types.size != 0)
1858         return 0;
1859     }
1860   else if (version != 2)
1861     return 0;
1862
1863   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1864   map->total_size = dwarf2_per_objfile->gdb_index.size;
1865
1866   metadata = (offset_type *) (addr + sizeof (offset_type));
1867
1868   i = 0;
1869   cu_list = addr + MAYBE_SWAP (metadata[i]);
1870   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1871                       / 8);
1872   ++i;
1873
1874   if (version == 2)
1875     {
1876       types_list = addr + MAYBE_SWAP (metadata[i]);
1877       types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1878                               - MAYBE_SWAP (metadata[i]))
1879                              / 8);
1880       ++i;
1881     }
1882
1883   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1884   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1885                              - MAYBE_SWAP (metadata[i]));
1886   ++i;
1887
1888   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1889   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1890                              - MAYBE_SWAP (metadata[i]))
1891                             / (2 * sizeof (offset_type)));
1892   ++i;
1893
1894   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1895
1896   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1897     return 0;
1898
1899   if (version == 2
1900       && types_list_elements
1901       && !create_signatured_type_hash_from_index (objfile, types_list,
1902                                                   types_list_elements))
1903     return 0;
1904
1905   create_addrmap_from_index (objfile, map);
1906
1907   dwarf2_per_objfile->index_table = map;
1908   dwarf2_per_objfile->using_index = 1;
1909
1910   return 1;
1911 }
1912
1913 /* A helper for the "quick" functions which sets the global
1914    dwarf2_per_objfile according to OBJFILE.  */
1915 static void
1916 dw2_setup (struct objfile *objfile)
1917 {
1918   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1919   gdb_assert (dwarf2_per_objfile);
1920 }
1921
1922 /* A helper for the "quick" functions which attempts to read the line
1923    table for THIS_CU.  */
1924 static void
1925 dw2_require_line_header (struct objfile *objfile,
1926                          struct dwarf2_per_cu_data *this_cu)
1927 {
1928   bfd *abfd = objfile->obfd;
1929   struct line_header *lh = NULL;
1930   struct attribute *attr;
1931   struct cleanup *cleanups;
1932   struct die_info *comp_unit_die;
1933   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1934   int has_children, i;
1935   struct dwarf2_cu cu;
1936   unsigned int bytes_read, buffer_size;
1937   struct die_reader_specs reader_specs;
1938   char *name, *comp_dir;
1939
1940   if (this_cu->v.quick->read_lines)
1941     return;
1942   this_cu->v.quick->read_lines = 1;
1943
1944   memset (&cu, 0, sizeof (cu));
1945   cu.objfile = objfile;
1946   obstack_init (&cu.comp_unit_obstack);
1947
1948   cleanups = make_cleanup (free_stack_comp_unit, &cu);
1949
1950   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1951   buffer_size = dwarf2_per_objfile->info.size;
1952   buffer = dwarf2_per_objfile->info.buffer;
1953   info_ptr = buffer + this_cu->offset;
1954   beg_of_comp_unit = info_ptr;
1955
1956   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1957                                           buffer, buffer_size,
1958                                           abfd);
1959
1960   /* Complete the cu_header.  */
1961   cu.header.offset = beg_of_comp_unit - buffer;
1962   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1963
1964   this_cu->cu = &cu;
1965   cu.per_cu = this_cu;
1966
1967   dwarf2_read_abbrevs (abfd, &cu);
1968   make_cleanup (dwarf2_free_abbrev_table, &cu);
1969
1970   if (this_cu->from_debug_types)
1971     info_ptr += 8 /*signature*/ + cu.header.offset_size;
1972   init_cu_die_reader (&reader_specs, &cu);
1973   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1974                             &has_children);
1975
1976   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1977   if (attr)
1978     {
1979       unsigned int line_offset = DW_UNSND (attr);
1980       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1981     }
1982   if (lh == NULL)
1983     {
1984       do_cleanups (cleanups);
1985       return;
1986     }
1987
1988   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1989
1990   this_cu->v.quick->lines = lh;
1991
1992   this_cu->v.quick->file_names
1993     = obstack_alloc (&objfile->objfile_obstack,
1994                      lh->num_file_names * sizeof (char *));
1995   for (i = 0; i < lh->num_file_names; ++i)
1996     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
1997
1998   do_cleanups (cleanups);
1999 }
2000
2001 /* A helper for the "quick" functions which computes and caches the
2002    real path for a given file name from the line table.
2003    dw2_require_line_header must have been called before this is
2004    invoked.  */
2005 static const char *
2006 dw2_require_full_path (struct objfile *objfile,
2007                        struct dwarf2_per_cu_data *cu,
2008                        int index)
2009 {
2010   if (!cu->v.quick->full_names)
2011     cu->v.quick->full_names
2012       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2013                         cu->v.quick->lines->num_file_names,
2014                         sizeof (char *));
2015
2016   if (!cu->v.quick->full_names[index])
2017     cu->v.quick->full_names[index]
2018       = gdb_realpath (cu->v.quick->file_names[index]);
2019
2020   return cu->v.quick->full_names[index];
2021 }
2022
2023 static struct symtab *
2024 dw2_find_last_source_symtab (struct objfile *objfile)
2025 {
2026   int index;
2027   dw2_setup (objfile);
2028   index = dwarf2_per_objfile->n_comp_units - 1;
2029   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2030 }
2031
2032 static void
2033 dw2_forget_cached_source_info (struct objfile *objfile)
2034 {
2035   int i;
2036
2037   dw2_setup (objfile);
2038   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2039                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2040     {
2041       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2042
2043       if (cu->v.quick->full_names)
2044         {
2045           int j;
2046
2047           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2048             xfree ((void *) cu->v.quick->full_names[j]);
2049         }
2050     }
2051 }
2052
2053 static int
2054 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2055                    const char *full_path, const char *real_path,
2056                    struct symtab **result)
2057 {
2058   int i;
2059   int check_basename = lbasename (name) == name;
2060   struct dwarf2_per_cu_data *base_cu = NULL;
2061
2062   dw2_setup (objfile);
2063   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2064                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2065     {
2066       int j;
2067       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2068
2069       if (cu->v.quick->symtab)
2070         continue;
2071
2072       dw2_require_line_header (objfile, cu);
2073       if (!cu->v.quick->lines)
2074         continue;
2075
2076       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2077         {
2078           const char *this_name = cu->v.quick->file_names[j];
2079
2080           if (FILENAME_CMP (name, this_name) == 0)
2081             {
2082               *result = dw2_instantiate_symtab (objfile, cu);
2083               return 1;
2084             }
2085
2086           if (check_basename && ! base_cu
2087               && FILENAME_CMP (lbasename (this_name), name) == 0)
2088             base_cu = cu;
2089
2090           if (full_path != NULL)
2091             {
2092               const char *this_full_name = dw2_require_full_path (objfile,
2093                                                                   cu, j);
2094
2095               if (this_full_name
2096                   && FILENAME_CMP (full_path, this_full_name) == 0)
2097                 {
2098                   *result = dw2_instantiate_symtab (objfile, cu);
2099                   return 1;
2100                 }
2101             }
2102
2103           if (real_path != NULL)
2104             {
2105               const char *this_full_name = dw2_require_full_path (objfile,
2106                                                                   cu, j);
2107
2108               if (this_full_name != NULL)
2109                 {
2110                   char *rp = gdb_realpath (this_full_name);
2111                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2112                     {
2113                       xfree (rp);
2114                       *result = dw2_instantiate_symtab (objfile, cu);
2115                       return 1;
2116                     }
2117                   xfree (rp);
2118                 }
2119             }
2120         }
2121     }
2122
2123   if (base_cu)
2124     {
2125       *result = dw2_instantiate_symtab (objfile, base_cu);
2126       return 1;
2127     }
2128
2129   return 0;
2130 }
2131
2132 static struct symtab *
2133 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2134                    const char *name, domain_enum domain)
2135 {
2136   /* We do all the work in the pre_expand_symtabs_matching hook
2137      instead.  */
2138   return NULL;
2139 }
2140
2141 /* A helper function that expands all symtabs that hold an object
2142    named NAME.  */
2143 static void
2144 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2145 {
2146   dw2_setup (objfile);
2147
2148   if (dwarf2_per_objfile->index_table)
2149     {
2150       offset_type *vec;
2151
2152       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2153                                     name, &vec))
2154         {
2155           offset_type i, len = MAYBE_SWAP (*vec);
2156           for (i = 0; i < len; ++i)
2157             {
2158               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2159               struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2160
2161               dw2_instantiate_symtab (objfile, cu);
2162             }
2163         }
2164     }
2165 }
2166
2167 static void
2168 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2169                                  int kind, const char *name,
2170                                  domain_enum domain)
2171 {
2172   dw2_do_expand_symtabs_matching (objfile, name);
2173 }
2174
2175 static void
2176 dw2_print_stats (struct objfile *objfile)
2177 {
2178   int i, count;
2179
2180   dw2_setup (objfile);
2181   count = 0;
2182   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2183                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2184     {
2185       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2186
2187       if (!cu->v.quick->symtab)
2188         ++count;
2189     }
2190   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2191 }
2192
2193 static void
2194 dw2_dump (struct objfile *objfile)
2195 {
2196   /* Nothing worth printing.  */
2197 }
2198
2199 static void
2200 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2201               struct section_offsets *delta)
2202 {
2203   /* There's nothing to relocate here.  */
2204 }
2205
2206 static void
2207 dw2_expand_symtabs_for_function (struct objfile *objfile,
2208                                  const char *func_name)
2209 {
2210   dw2_do_expand_symtabs_matching (objfile, func_name);
2211 }
2212
2213 static void
2214 dw2_expand_all_symtabs (struct objfile *objfile)
2215 {
2216   int i;
2217
2218   dw2_setup (objfile);
2219
2220   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2221                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2222     {
2223       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2224
2225       dw2_instantiate_symtab (objfile, cu);
2226     }
2227 }
2228
2229 static void
2230 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2231                                   const char *filename)
2232 {
2233   int i;
2234
2235   dw2_setup (objfile);
2236   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2237                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2238     {
2239       int j;
2240       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2241
2242       if (cu->v.quick->symtab)
2243         continue;
2244
2245       dw2_require_line_header (objfile, cu);
2246       if (!cu->v.quick->lines)
2247         continue;
2248
2249       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2250         {
2251           const char *this_name = cu->v.quick->file_names[j];
2252           if (strcmp (this_name, filename) == 0)
2253             {
2254               dw2_instantiate_symtab (objfile, cu);
2255               break;
2256             }
2257         }
2258     }
2259 }
2260
2261 static const char *
2262 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2263 {
2264   struct dwarf2_per_cu_data *cu;
2265   offset_type *vec;
2266
2267   dw2_setup (objfile);
2268
2269   if (!dwarf2_per_objfile->index_table)
2270     return NULL;
2271
2272   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2273                                  name, &vec))
2274     return NULL;
2275
2276   /* Note that this just looks at the very first one named NAME -- but
2277      actually we are looking for a function.  find_main_filename
2278      should be rewritten so that it doesn't require a custom hook.  It
2279      could just use the ordinary symbol tables.  */
2280   /* vec[0] is the length, which must always be >0.  */
2281   cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2282
2283   dw2_require_line_header (objfile, cu);
2284   if (!cu->v.quick->lines)
2285     return NULL;
2286
2287   return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2288 }
2289
2290 static void
2291 dw2_map_ada_symtabs (struct objfile *objfile,
2292                      int (*wild_match) (const char *, int, const char *),
2293                      int (*is_name_suffix) (const char *),
2294                      void (*callback) (struct objfile *,
2295                                        struct symtab *, void *),
2296                      const char *name, int global,
2297                      domain_enum namespace, int wild,
2298                      void *data)
2299 {
2300   /* For now, we don't support Ada, so this function can't be
2301      reached.  */
2302   internal_error (__FILE__, __LINE__,
2303                   _("map_ada_symtabs called via index method"));
2304 }
2305
2306 static void
2307 dw2_expand_symtabs_matching (struct objfile *objfile,
2308                              int (*file_matcher) (const char *, void *),
2309                              int (*name_matcher) (const char *, void *),
2310                              domain_enum kind,
2311                              void *data)
2312 {
2313   int i;
2314   offset_type iter;
2315
2316   dw2_setup (objfile);
2317   if (!dwarf2_per_objfile->index_table)
2318     return;
2319
2320   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2321                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2322     {
2323       int j;
2324       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2325
2326       cu->v.quick->mark = 0;
2327       if (cu->v.quick->symtab)
2328         continue;
2329
2330       dw2_require_line_header (objfile, cu);
2331       if (!cu->v.quick->lines)
2332         continue;
2333
2334       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2335         {
2336           if (file_matcher (cu->v.quick->file_names[j], data))
2337             {
2338               cu->v.quick->mark = 1;
2339               break;
2340             }
2341         }
2342     }
2343
2344   for (iter = 0;
2345        iter < dwarf2_per_objfile->index_table->index_table_slots;
2346        ++iter)
2347     {
2348       offset_type idx = 2 * iter;
2349       const char *name;
2350       offset_type *vec, vec_len, vec_idx;
2351
2352       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2353           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2354         continue;
2355
2356       name = (dwarf2_per_objfile->index_table->constant_pool
2357               + dwarf2_per_objfile->index_table->index_table[idx]);
2358
2359       if (! (*name_matcher) (name, data))
2360         continue;
2361
2362       /* The name was matched, now expand corresponding CUs that were
2363          marked.  */
2364       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2365                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2366       vec_len = MAYBE_SWAP (vec[0]);
2367       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2368         {
2369           struct dwarf2_per_cu_data *cu;
2370
2371           cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2372           if (cu->v.quick->mark)
2373             dw2_instantiate_symtab (objfile, cu);
2374         }
2375     }
2376 }
2377
2378 static struct symtab *
2379 dw2_find_pc_sect_symtab (struct objfile *objfile,
2380                          struct minimal_symbol *msymbol,
2381                          CORE_ADDR pc,
2382                          struct obj_section *section,
2383                          int warn_if_readin)
2384 {
2385   struct dwarf2_per_cu_data *data;
2386
2387   dw2_setup (objfile);
2388
2389   if (!objfile->psymtabs_addrmap)
2390     return NULL;
2391
2392   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2393   if (!data)
2394     return NULL;
2395
2396   if (warn_if_readin && data->v.quick->symtab)
2397     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2398              paddress (get_objfile_arch (objfile), pc));
2399
2400   return dw2_instantiate_symtab (objfile, data);
2401 }
2402
2403 static void
2404 dw2_map_symbol_names (struct objfile *objfile,
2405                       void (*fun) (const char *, void *),
2406                       void *data)
2407 {
2408   offset_type iter;
2409   dw2_setup (objfile);
2410
2411   if (!dwarf2_per_objfile->index_table)
2412     return;
2413
2414   for (iter = 0;
2415        iter < dwarf2_per_objfile->index_table->index_table_slots;
2416        ++iter)
2417     {
2418       offset_type idx = 2 * iter;
2419       const char *name;
2420       offset_type *vec, vec_len, vec_idx;
2421
2422       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2423           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2424         continue;
2425
2426       name = (dwarf2_per_objfile->index_table->constant_pool
2427               + dwarf2_per_objfile->index_table->index_table[idx]);
2428
2429       (*fun) (name, data);
2430     }
2431 }
2432
2433 static void
2434 dw2_map_symbol_filenames (struct objfile *objfile,
2435                           void (*fun) (const char *, const char *, void *),
2436                           void *data)
2437 {
2438   int i;
2439
2440   dw2_setup (objfile);
2441   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2442                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2443     {
2444       int j;
2445       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2446
2447       if (cu->v.quick->symtab)
2448         continue;
2449
2450       dw2_require_line_header (objfile, cu);
2451       if (!cu->v.quick->lines)
2452         continue;
2453
2454       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2455         {
2456           const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2457           (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2458         }
2459     }
2460 }
2461
2462 static int
2463 dw2_has_symbols (struct objfile *objfile)
2464 {
2465   return 1;
2466 }
2467
2468 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2469 {
2470   dw2_has_symbols,
2471   dw2_find_last_source_symtab,
2472   dw2_forget_cached_source_info,
2473   dw2_lookup_symtab,
2474   dw2_lookup_symbol,
2475   dw2_pre_expand_symtabs_matching,
2476   dw2_print_stats,
2477   dw2_dump,
2478   dw2_relocate,
2479   dw2_expand_symtabs_for_function,
2480   dw2_expand_all_symtabs,
2481   dw2_expand_symtabs_with_filename,
2482   dw2_find_symbol_file,
2483   dw2_map_ada_symtabs,
2484   dw2_expand_symtabs_matching,
2485   dw2_find_pc_sect_symtab,
2486   dw2_map_symbol_names,
2487   dw2_map_symbol_filenames
2488 };
2489
2490 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2491    file will use psymtabs, or 1 if using the GNU index.  */
2492
2493 int
2494 dwarf2_initialize_objfile (struct objfile *objfile)
2495 {
2496   /* If we're about to read full symbols, don't bother with the
2497      indices.  In this case we also don't care if some other debug
2498      format is making psymtabs, because they are all about to be
2499      expanded anyway.  */
2500   if ((objfile->flags & OBJF_READNOW))
2501     {
2502       int i;
2503
2504       dwarf2_per_objfile->using_index = 1;
2505       create_all_comp_units (objfile);
2506       create_debug_types_hash_table (objfile);
2507
2508       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2509                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2510         {
2511           struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2512
2513           cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2514                                         struct dwarf2_per_cu_quick_data);
2515         }
2516
2517       /* Return 1 so that gdb sees the "quick" functions.  However,
2518          these functions will be no-ops because we will have expanded
2519          all symtabs.  */
2520       return 1;
2521     }
2522
2523   if (dwarf2_read_index (objfile))
2524     return 1;
2525
2526   dwarf2_build_psymtabs (objfile);
2527   return 0;
2528 }
2529
2530 \f
2531
2532 /* Build a partial symbol table.  */
2533
2534 void
2535 dwarf2_build_psymtabs (struct objfile *objfile)
2536 {
2537   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2538     {
2539       init_psymbol_list (objfile, 1024);
2540     }
2541
2542   dwarf2_build_psymtabs_hard (objfile);
2543 }
2544
2545 /* Return TRUE if OFFSET is within CU_HEADER.  */
2546
2547 static inline int
2548 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2549 {
2550   unsigned int bottom = cu_header->offset;
2551   unsigned int top = (cu_header->offset
2552                       + cu_header->length
2553                       + cu_header->initial_length_size);
2554
2555   return (offset >= bottom && offset < top);
2556 }
2557
2558 /* Read in the comp unit header information from the debug_info at info_ptr.
2559    NOTE: This leaves members offset, first_die_offset to be filled in
2560    by the caller.  */
2561
2562 static gdb_byte *
2563 read_comp_unit_head (struct comp_unit_head *cu_header,
2564                      gdb_byte *info_ptr, bfd *abfd)
2565 {
2566   int signed_addr;
2567   unsigned int bytes_read;
2568
2569   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2570   cu_header->initial_length_size = bytes_read;
2571   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2572   info_ptr += bytes_read;
2573   cu_header->version = read_2_bytes (abfd, info_ptr);
2574   info_ptr += 2;
2575   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2576                                           &bytes_read);
2577   info_ptr += bytes_read;
2578   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2579   info_ptr += 1;
2580   signed_addr = bfd_get_sign_extend_vma (abfd);
2581   if (signed_addr < 0)
2582     internal_error (__FILE__, __LINE__,
2583                     _("read_comp_unit_head: dwarf from non elf file"));
2584   cu_header->signed_addr_p = signed_addr;
2585
2586   return info_ptr;
2587 }
2588
2589 static gdb_byte *
2590 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2591                              gdb_byte *buffer, unsigned int buffer_size,
2592                              bfd *abfd)
2593 {
2594   gdb_byte *beg_of_comp_unit = info_ptr;
2595
2596   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2597
2598   if (header->version != 2 && header->version != 3 && header->version != 4)
2599     error (_("Dwarf Error: wrong version in compilation unit header "
2600            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2601            bfd_get_filename (abfd));
2602
2603   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2604     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2605            "(offset 0x%lx + 6) [in module %s]"),
2606            (long) header->abbrev_offset,
2607            (long) (beg_of_comp_unit - buffer),
2608            bfd_get_filename (abfd));
2609
2610   if (beg_of_comp_unit + header->length + header->initial_length_size
2611       > buffer + buffer_size)
2612     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2613            "(offset 0x%lx + 0) [in module %s]"),
2614            (long) header->length,
2615            (long) (beg_of_comp_unit - buffer),
2616            bfd_get_filename (abfd));
2617
2618   return info_ptr;
2619 }
2620
2621 /* Read in the types comp unit header information from .debug_types entry at
2622    types_ptr.  The result is a pointer to one past the end of the header.  */
2623
2624 static gdb_byte *
2625 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2626                           ULONGEST *signature,
2627                           gdb_byte *types_ptr, bfd *abfd)
2628 {
2629   gdb_byte *initial_types_ptr = types_ptr;
2630
2631   dwarf2_read_section (dwarf2_per_objfile->objfile,
2632                        &dwarf2_per_objfile->types);
2633   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2634
2635   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2636
2637   *signature = read_8_bytes (abfd, types_ptr);
2638   types_ptr += 8;
2639   types_ptr += cu_header->offset_size;
2640   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2641
2642   return types_ptr;
2643 }
2644
2645 /* Allocate a new partial symtab for file named NAME and mark this new
2646    partial symtab as being an include of PST.  */
2647
2648 static void
2649 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2650                                struct objfile *objfile)
2651 {
2652   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2653
2654   subpst->section_offsets = pst->section_offsets;
2655   subpst->textlow = 0;
2656   subpst->texthigh = 0;
2657
2658   subpst->dependencies = (struct partial_symtab **)
2659     obstack_alloc (&objfile->objfile_obstack,
2660                    sizeof (struct partial_symtab *));
2661   subpst->dependencies[0] = pst;
2662   subpst->number_of_dependencies = 1;
2663
2664   subpst->globals_offset = 0;
2665   subpst->n_global_syms = 0;
2666   subpst->statics_offset = 0;
2667   subpst->n_static_syms = 0;
2668   subpst->symtab = NULL;
2669   subpst->read_symtab = pst->read_symtab;
2670   subpst->readin = 0;
2671
2672   /* No private part is necessary for include psymtabs.  This property
2673      can be used to differentiate between such include psymtabs and
2674      the regular ones.  */
2675   subpst->read_symtab_private = NULL;
2676 }
2677
2678 /* Read the Line Number Program data and extract the list of files
2679    included by the source file represented by PST.  Build an include
2680    partial symtab for each of these included files.  */
2681
2682 static void
2683 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2684                                struct die_info *die,
2685                                struct partial_symtab *pst)
2686 {
2687   struct objfile *objfile = cu->objfile;
2688   bfd *abfd = objfile->obfd;
2689   struct line_header *lh = NULL;
2690   struct attribute *attr;
2691
2692   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2693   if (attr)
2694     {
2695       unsigned int line_offset = DW_UNSND (attr);
2696
2697       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2698     }
2699   if (lh == NULL)
2700     return;  /* No linetable, so no includes.  */
2701
2702   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2703
2704   free_line_header (lh);
2705 }
2706
2707 static hashval_t
2708 hash_type_signature (const void *item)
2709 {
2710   const struct signatured_type *type_sig = item;
2711
2712   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2713   return type_sig->signature;
2714 }
2715
2716 static int
2717 eq_type_signature (const void *item_lhs, const void *item_rhs)
2718 {
2719   const struct signatured_type *lhs = item_lhs;
2720   const struct signatured_type *rhs = item_rhs;
2721
2722   return lhs->signature == rhs->signature;
2723 }
2724
2725 /* Allocate a hash table for signatured types.  */
2726
2727 static htab_t
2728 allocate_signatured_type_hash_table (struct objfile *objfile)
2729 {
2730   return htab_create_alloc_ex (41,
2731                                hash_type_signature,
2732                                eq_type_signature,
2733                                NULL,
2734                                &objfile->objfile_obstack,
2735                                hashtab_obstack_allocate,
2736                                dummy_obstack_deallocate);
2737 }
2738
2739 /* A helper function to add a signatured type CU to a list.  */
2740
2741 static int
2742 add_signatured_type_cu_to_list (void **slot, void *datum)
2743 {
2744   struct signatured_type *sigt = *slot;
2745   struct dwarf2_per_cu_data ***datap = datum;
2746
2747   **datap = &sigt->per_cu;
2748   ++*datap;
2749
2750   return 1;
2751 }
2752
2753 /* Create the hash table of all entries in the .debug_types section.
2754    The result is zero if there is an error (e.g. missing .debug_types section),
2755    otherwise non-zero.  */
2756
2757 static int
2758 create_debug_types_hash_table (struct objfile *objfile)
2759 {
2760   gdb_byte *info_ptr;
2761   htab_t types_htab;
2762   struct dwarf2_per_cu_data **iter;
2763
2764   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2765   info_ptr = dwarf2_per_objfile->types.buffer;
2766
2767   if (info_ptr == NULL)
2768     {
2769       dwarf2_per_objfile->signatured_types = NULL;
2770       return 0;
2771     }
2772
2773   types_htab = allocate_signatured_type_hash_table (objfile);
2774
2775   if (dwarf2_die_debug)
2776     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2777
2778   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2779     {
2780       unsigned int offset;
2781       unsigned int offset_size;
2782       unsigned int type_offset;
2783       unsigned int length, initial_length_size;
2784       unsigned short version;
2785       ULONGEST signature;
2786       struct signatured_type *type_sig;
2787       void **slot;
2788       gdb_byte *ptr = info_ptr;
2789
2790       offset = ptr - dwarf2_per_objfile->types.buffer;
2791
2792       /* We need to read the type's signature in order to build the hash
2793          table, but we don't need to read anything else just yet.  */
2794
2795       /* Sanity check to ensure entire cu is present.  */
2796       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2797       if (ptr + length + initial_length_size
2798           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2799         {
2800           complaint (&symfile_complaints,
2801                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2802           break;
2803         }
2804
2805       offset_size = initial_length_size == 4 ? 4 : 8;
2806       ptr += initial_length_size;
2807       version = bfd_get_16 (objfile->obfd, ptr);
2808       ptr += 2;
2809       ptr += offset_size; /* abbrev offset */
2810       ptr += 1; /* address size */
2811       signature = bfd_get_64 (objfile->obfd, ptr);
2812       ptr += 8;
2813       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2814
2815       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2816       memset (type_sig, 0, sizeof (*type_sig));
2817       type_sig->signature = signature;
2818       type_sig->offset = offset;
2819       type_sig->type_offset = type_offset;
2820       type_sig->per_cu.objfile = objfile;
2821       type_sig->per_cu.from_debug_types = 1;
2822
2823       slot = htab_find_slot (types_htab, type_sig, INSERT);
2824       gdb_assert (slot != NULL);
2825       *slot = type_sig;
2826
2827       if (dwarf2_die_debug)
2828         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2829                             offset, phex (signature, sizeof (signature)));
2830
2831       info_ptr = info_ptr + initial_length_size + length;
2832     }
2833
2834   dwarf2_per_objfile->signatured_types = types_htab;
2835
2836   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2837   dwarf2_per_objfile->type_comp_units
2838     = obstack_alloc (&objfile->objfile_obstack,
2839                      dwarf2_per_objfile->n_type_comp_units
2840                      * sizeof (struct dwarf2_per_cu_data *));
2841   iter = &dwarf2_per_objfile->type_comp_units[0];
2842   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2843   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2844               == dwarf2_per_objfile->n_type_comp_units);
2845
2846   return 1;
2847 }
2848
2849 /* Lookup a signature based type.
2850    Returns NULL if SIG is not present in the table.  */
2851
2852 static struct signatured_type *
2853 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2854 {
2855   struct signatured_type find_entry, *entry;
2856
2857   if (dwarf2_per_objfile->signatured_types == NULL)
2858     {
2859       complaint (&symfile_complaints,
2860                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2861       return 0;
2862     }
2863
2864   find_entry.signature = sig;
2865   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2866   return entry;
2867 }
2868
2869 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2870
2871 static void
2872 init_cu_die_reader (struct die_reader_specs *reader,
2873                     struct dwarf2_cu *cu)
2874 {
2875   reader->abfd = cu->objfile->obfd;
2876   reader->cu = cu;
2877   if (cu->per_cu->from_debug_types)
2878     {
2879       gdb_assert (dwarf2_per_objfile->types.readin);
2880       reader->buffer = dwarf2_per_objfile->types.buffer;
2881     }
2882   else
2883     {
2884       gdb_assert (dwarf2_per_objfile->info.readin);
2885       reader->buffer = dwarf2_per_objfile->info.buffer;
2886     }
2887 }
2888
2889 /* Find the base address of the compilation unit for range lists and
2890    location lists.  It will normally be specified by DW_AT_low_pc.
2891    In DWARF-3 draft 4, the base address could be overridden by
2892    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2893    compilation units with discontinuous ranges.  */
2894
2895 static void
2896 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2897 {
2898   struct attribute *attr;
2899
2900   cu->base_known = 0;
2901   cu->base_address = 0;
2902
2903   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2904   if (attr)
2905     {
2906       cu->base_address = DW_ADDR (attr);
2907       cu->base_known = 1;
2908     }
2909   else
2910     {
2911       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2912       if (attr)
2913         {
2914           cu->base_address = DW_ADDR (attr);
2915           cu->base_known = 1;
2916         }
2917     }
2918 }
2919
2920 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2921    to combine the common parts.
2922    Process a compilation unit for a psymtab.
2923    BUFFER is a pointer to the beginning of the dwarf section buffer,
2924    either .debug_info or debug_types.
2925    INFO_PTR is a pointer to the start of the CU.
2926    Returns a pointer to the next CU.  */
2927
2928 static gdb_byte *
2929 process_psymtab_comp_unit (struct objfile *objfile,
2930                            struct dwarf2_per_cu_data *this_cu,
2931                            gdb_byte *buffer, gdb_byte *info_ptr,
2932                            unsigned int buffer_size)
2933 {
2934   bfd *abfd = objfile->obfd;
2935   gdb_byte *beg_of_comp_unit = info_ptr;
2936   struct die_info *comp_unit_die;
2937   struct partial_symtab *pst;
2938   CORE_ADDR baseaddr;
2939   struct cleanup *back_to_inner;
2940   struct dwarf2_cu cu;
2941   int has_children, has_pc_info;
2942   struct attribute *attr;
2943   CORE_ADDR best_lowpc = 0, best_highpc = 0;
2944   struct die_reader_specs reader_specs;
2945
2946   memset (&cu, 0, sizeof (cu));
2947   cu.objfile = objfile;
2948   obstack_init (&cu.comp_unit_obstack);
2949
2950   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
2951
2952   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2953                                           buffer, buffer_size,
2954                                           abfd);
2955
2956   /* Complete the cu_header.  */
2957   cu.header.offset = beg_of_comp_unit - buffer;
2958   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2959
2960   cu.list_in_scope = &file_symbols;
2961
2962   /* If this compilation unit was already read in, free the
2963      cached copy in order to read it in again.  This is
2964      necessary because we skipped some symbols when we first
2965      read in the compilation unit (see load_partial_dies).
2966      This problem could be avoided, but the benefit is
2967      unclear.  */
2968   if (this_cu->cu != NULL)
2969     free_one_cached_comp_unit (this_cu->cu);
2970
2971   /* Note that this is a pointer to our stack frame, being
2972      added to a global data structure.  It will be cleaned up
2973      in free_stack_comp_unit when we finish with this
2974      compilation unit.  */
2975   this_cu->cu = &cu;
2976   cu.per_cu = this_cu;
2977
2978   /* Read the abbrevs for this compilation unit into a table.  */
2979   dwarf2_read_abbrevs (abfd, &cu);
2980   make_cleanup (dwarf2_free_abbrev_table, &cu);
2981
2982   /* Read the compilation unit die.  */
2983   if (this_cu->from_debug_types)
2984     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2985   init_cu_die_reader (&reader_specs, &cu);
2986   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2987                             &has_children);
2988
2989   if (this_cu->from_debug_types)
2990     {
2991       /* offset,length haven't been set yet for type units.  */
2992       this_cu->offset = cu.header.offset;
2993       this_cu->length = cu.header.length + cu.header.initial_length_size;
2994     }
2995   else if (comp_unit_die->tag == DW_TAG_partial_unit)
2996     {
2997       info_ptr = (beg_of_comp_unit + cu.header.length
2998                   + cu.header.initial_length_size);
2999       do_cleanups (back_to_inner);
3000       return info_ptr;
3001     }
3002
3003   /* Set the language we're debugging.  */
3004   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3005   if (attr)
3006     set_cu_language (DW_UNSND (attr), &cu);
3007   else
3008     set_cu_language (language_minimal, &cu);
3009
3010   /* Allocate a new partial symbol table structure.  */
3011   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3012   pst = start_psymtab_common (objfile, objfile->section_offsets,
3013                               (attr != NULL) ? DW_STRING (attr) : "",
3014                               /* TEXTLOW and TEXTHIGH are set below.  */
3015                               0,
3016                               objfile->global_psymbols.next,
3017                               objfile->static_psymbols.next);
3018
3019   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3020   if (attr != NULL)
3021     pst->dirname = DW_STRING (attr);
3022
3023   pst->read_symtab_private = this_cu;
3024
3025   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3026
3027   /* Store the function that reads in the rest of the symbol table */
3028   pst->read_symtab = dwarf2_psymtab_to_symtab;
3029
3030   this_cu->v.psymtab = pst;
3031
3032   dwarf2_find_base_address (comp_unit_die, &cu);
3033
3034   /* Possibly set the default values of LOWPC and HIGHPC from
3035      `DW_AT_ranges'.  */
3036   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3037                                       &best_highpc, &cu, pst);
3038   if (has_pc_info == 1 && best_lowpc < best_highpc)
3039     /* Store the contiguous range if it is not empty; it can be empty for
3040        CUs with no code.  */
3041     addrmap_set_empty (objfile->psymtabs_addrmap,
3042                        best_lowpc + baseaddr,
3043                        best_highpc + baseaddr - 1, pst);
3044
3045   /* Check if comp unit has_children.
3046      If so, read the rest of the partial symbols from this comp unit.
3047      If not, there's no more debug_info for this comp unit. */
3048   if (has_children)
3049     {
3050       struct partial_die_info *first_die;
3051       CORE_ADDR lowpc, highpc;
3052
3053       lowpc = ((CORE_ADDR) -1);
3054       highpc = ((CORE_ADDR) 0);
3055
3056       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3057
3058       scan_partial_symbols (first_die, &lowpc, &highpc,
3059                             ! has_pc_info, &cu);
3060
3061       /* If we didn't find a lowpc, set it to highpc to avoid
3062          complaints from `maint check'.  */
3063       if (lowpc == ((CORE_ADDR) -1))
3064         lowpc = highpc;
3065
3066       /* If the compilation unit didn't have an explicit address range,
3067          then use the information extracted from its child dies.  */
3068       if (! has_pc_info)
3069         {
3070           best_lowpc = lowpc;
3071           best_highpc = highpc;
3072         }
3073     }
3074   pst->textlow = best_lowpc + baseaddr;
3075   pst->texthigh = best_highpc + baseaddr;
3076
3077   pst->n_global_syms = objfile->global_psymbols.next -
3078     (objfile->global_psymbols.list + pst->globals_offset);
3079   pst->n_static_syms = objfile->static_psymbols.next -
3080     (objfile->static_psymbols.list + pst->statics_offset);
3081   sort_pst_symbols (pst);
3082
3083   info_ptr = (beg_of_comp_unit + cu.header.length
3084               + cu.header.initial_length_size);
3085
3086   if (this_cu->from_debug_types)
3087     {
3088       /* It's not clear we want to do anything with stmt lists here.
3089          Waiting to see what gcc ultimately does.  */
3090     }
3091   else
3092     {
3093       /* Get the list of files included in the current compilation unit,
3094          and build a psymtab for each of them.  */
3095       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3096     }
3097
3098   do_cleanups (back_to_inner);
3099
3100   return info_ptr;
3101 }
3102
3103 /* Traversal function for htab_traverse_noresize.
3104    Process one .debug_types comp-unit.  */
3105
3106 static int
3107 process_type_comp_unit (void **slot, void *info)
3108 {
3109   struct signatured_type *entry = (struct signatured_type *) *slot;
3110   struct objfile *objfile = (struct objfile *) info;
3111   struct dwarf2_per_cu_data *this_cu;
3112
3113   this_cu = &entry->per_cu;
3114
3115   gdb_assert (dwarf2_per_objfile->types.readin);
3116   process_psymtab_comp_unit (objfile, this_cu,
3117                              dwarf2_per_objfile->types.buffer,
3118                              dwarf2_per_objfile->types.buffer + entry->offset,
3119                              dwarf2_per_objfile->types.size);
3120
3121   return 1;
3122 }
3123
3124 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3125    Build partial symbol tables for the .debug_types comp-units.  */
3126
3127 static void
3128 build_type_psymtabs (struct objfile *objfile)
3129 {
3130   if (! create_debug_types_hash_table (objfile))
3131     return;
3132
3133   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3134                           process_type_comp_unit, objfile);
3135 }
3136
3137 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3138
3139 static void
3140 psymtabs_addrmap_cleanup (void *o)
3141 {
3142   struct objfile *objfile = o;
3143
3144   objfile->psymtabs_addrmap = NULL;
3145 }
3146
3147 /* Build the partial symbol table by doing a quick pass through the
3148    .debug_info and .debug_abbrev sections.  */
3149
3150 static void
3151 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3152 {
3153   gdb_byte *info_ptr;
3154   struct cleanup *back_to, *addrmap_cleanup;
3155   struct obstack temp_obstack;
3156
3157   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3158   info_ptr = dwarf2_per_objfile->info.buffer;
3159
3160   /* Any cached compilation units will be linked by the per-objfile
3161      read_in_chain.  Make sure to free them when we're done.  */
3162   back_to = make_cleanup (free_cached_comp_units, NULL);
3163
3164   build_type_psymtabs (objfile);
3165
3166   create_all_comp_units (objfile);
3167
3168   /* Create a temporary address map on a temporary obstack.  We later
3169      copy this to the final obstack.  */
3170   obstack_init (&temp_obstack);
3171   make_cleanup_obstack_free (&temp_obstack);
3172   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3173   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3174
3175   /* Since the objects we're extracting from .debug_info vary in
3176      length, only the individual functions to extract them (like
3177      read_comp_unit_head and load_partial_die) can really know whether
3178      the buffer is large enough to hold another complete object.
3179
3180      At the moment, they don't actually check that.  If .debug_info
3181      holds just one extra byte after the last compilation unit's dies,
3182      then read_comp_unit_head will happily read off the end of the
3183      buffer.  read_partial_die is similarly casual.  Those functions
3184      should be fixed.
3185
3186      For this loop condition, simply checking whether there's any data
3187      left at all should be sufficient.  */
3188
3189   while (info_ptr < (dwarf2_per_objfile->info.buffer
3190                      + dwarf2_per_objfile->info.size))
3191     {
3192       struct dwarf2_per_cu_data *this_cu;
3193
3194       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3195                                        objfile);
3196
3197       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3198                                             dwarf2_per_objfile->info.buffer,
3199                                             info_ptr,
3200                                             dwarf2_per_objfile->info.size);
3201     }
3202
3203   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3204                                                     &objfile->objfile_obstack);
3205   discard_cleanups (addrmap_cleanup);
3206
3207   do_cleanups (back_to);
3208 }
3209
3210 /* Load the partial DIEs for a secondary CU into memory.  */
3211
3212 static void
3213 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3214                         struct objfile *objfile)
3215 {
3216   bfd *abfd = objfile->obfd;
3217   gdb_byte *info_ptr, *beg_of_comp_unit;
3218   struct die_info *comp_unit_die;
3219   struct dwarf2_cu *cu;
3220   struct cleanup *back_to;
3221   struct attribute *attr;
3222   int has_children;
3223   struct die_reader_specs reader_specs;
3224
3225   gdb_assert (! this_cu->from_debug_types);
3226
3227   gdb_assert (dwarf2_per_objfile->info.readin);
3228   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3229   beg_of_comp_unit = info_ptr;
3230
3231   cu = alloc_one_comp_unit (objfile);
3232
3233   /* ??? Missing cleanup for CU?  */
3234
3235   /* Link this compilation unit into the compilation unit tree.  */
3236   this_cu->cu = cu;
3237   cu->per_cu = this_cu;
3238   cu->type_hash = this_cu->type_hash;
3239
3240   info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3241                                           dwarf2_per_objfile->info.buffer,
3242                                           dwarf2_per_objfile->info.size,
3243                                           abfd);
3244
3245   /* Complete the cu_header.  */
3246   cu->header.offset = this_cu->offset;
3247   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3248
3249   /* Read the abbrevs for this compilation unit into a table.  */
3250   dwarf2_read_abbrevs (abfd, cu);
3251   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3252
3253   /* Read the compilation unit die.  */
3254   init_cu_die_reader (&reader_specs, cu);
3255   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3256                             &has_children);
3257
3258   /* Set the language we're debugging.  */
3259   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3260   if (attr)
3261     set_cu_language (DW_UNSND (attr), cu);
3262   else
3263     set_cu_language (language_minimal, cu);
3264
3265   /* Check if comp unit has_children.
3266      If so, read the rest of the partial symbols from this comp unit.
3267      If not, there's no more debug_info for this comp unit. */
3268   if (has_children)
3269     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3270
3271   do_cleanups (back_to);
3272 }
3273
3274 /* Create a list of all compilation units in OBJFILE.  We do this only
3275    if an inter-comp-unit reference is found; presumably if there is one,
3276    there will be many, and one will occur early in the .debug_info section.
3277    So there's no point in building this list incrementally.  */
3278
3279 static void
3280 create_all_comp_units (struct objfile *objfile)
3281 {
3282   int n_allocated;
3283   int n_comp_units;
3284   struct dwarf2_per_cu_data **all_comp_units;
3285   gdb_byte *info_ptr;
3286
3287   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3288   info_ptr = dwarf2_per_objfile->info.buffer;
3289
3290   n_comp_units = 0;
3291   n_allocated = 10;
3292   all_comp_units = xmalloc (n_allocated
3293                             * sizeof (struct dwarf2_per_cu_data *));
3294
3295   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3296     {
3297       unsigned int length, initial_length_size;
3298       struct dwarf2_per_cu_data *this_cu;
3299       unsigned int offset;
3300
3301       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3302
3303       /* Read just enough information to find out where the next
3304          compilation unit is.  */
3305       length = read_initial_length (objfile->obfd, info_ptr,
3306                                     &initial_length_size);
3307
3308       /* Save the compilation unit for later lookup.  */
3309       this_cu = obstack_alloc (&objfile->objfile_obstack,
3310                                sizeof (struct dwarf2_per_cu_data));
3311       memset (this_cu, 0, sizeof (*this_cu));
3312       this_cu->offset = offset;
3313       this_cu->length = length + initial_length_size;
3314       this_cu->objfile = objfile;
3315
3316       if (n_comp_units == n_allocated)
3317         {
3318           n_allocated *= 2;
3319           all_comp_units = xrealloc (all_comp_units,
3320                                      n_allocated
3321                                      * sizeof (struct dwarf2_per_cu_data *));
3322         }
3323       all_comp_units[n_comp_units++] = this_cu;
3324
3325       info_ptr = info_ptr + this_cu->length;
3326     }
3327
3328   dwarf2_per_objfile->all_comp_units
3329     = obstack_alloc (&objfile->objfile_obstack,
3330                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3331   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3332           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3333   xfree (all_comp_units);
3334   dwarf2_per_objfile->n_comp_units = n_comp_units;
3335 }
3336
3337 /* Process all loaded DIEs for compilation unit CU, starting at
3338    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3339    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3340    DW_AT_ranges).  If NEED_PC is set, then this function will set
3341    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3342    and record the covered ranges in the addrmap.  */
3343
3344 static void
3345 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3346                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3347 {
3348   struct partial_die_info *pdi;
3349
3350   /* Now, march along the PDI's, descending into ones which have
3351      interesting children but skipping the children of the other ones,
3352      until we reach the end of the compilation unit.  */
3353
3354   pdi = first_die;
3355
3356   while (pdi != NULL)
3357     {
3358       fixup_partial_die (pdi, cu);
3359
3360       /* Anonymous namespaces or modules have no name but have interesting
3361          children, so we need to look at them.  Ditto for anonymous
3362          enums.  */
3363
3364       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3365           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3366         {
3367           switch (pdi->tag)
3368             {
3369             case DW_TAG_subprogram:
3370               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3371               break;
3372             case DW_TAG_variable:
3373             case DW_TAG_typedef:
3374             case DW_TAG_union_type:
3375               if (!pdi->is_declaration)
3376                 {
3377                   add_partial_symbol (pdi, cu);
3378                 }
3379               break;
3380             case DW_TAG_class_type:
3381             case DW_TAG_interface_type:
3382             case DW_TAG_structure_type:
3383               if (!pdi->is_declaration)
3384                 {
3385                   add_partial_symbol (pdi, cu);
3386                 }
3387               break;
3388             case DW_TAG_enumeration_type:
3389               if (!pdi->is_declaration)
3390                 add_partial_enumeration (pdi, cu);
3391               break;
3392             case DW_TAG_base_type:
3393             case DW_TAG_subrange_type:
3394               /* File scope base type definitions are added to the partial
3395                  symbol table.  */
3396               add_partial_symbol (pdi, cu);
3397               break;
3398             case DW_TAG_namespace:
3399               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3400               break;
3401             case DW_TAG_module:
3402               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3403               break;
3404             default:
3405               break;
3406             }
3407         }
3408
3409       /* If the die has a sibling, skip to the sibling.  */
3410
3411       pdi = pdi->die_sibling;
3412     }
3413 }
3414
3415 /* Functions used to compute the fully scoped name of a partial DIE.
3416
3417    Normally, this is simple.  For C++, the parent DIE's fully scoped
3418    name is concatenated with "::" and the partial DIE's name.  For
3419    Java, the same thing occurs except that "." is used instead of "::".
3420    Enumerators are an exception; they use the scope of their parent
3421    enumeration type, i.e. the name of the enumeration type is not
3422    prepended to the enumerator.
3423
3424    There are two complexities.  One is DW_AT_specification; in this
3425    case "parent" means the parent of the target of the specification,
3426    instead of the direct parent of the DIE.  The other is compilers
3427    which do not emit DW_TAG_namespace; in this case we try to guess
3428    the fully qualified name of structure types from their members'
3429    linkage names.  This must be done using the DIE's children rather
3430    than the children of any DW_AT_specification target.  We only need
3431    to do this for structures at the top level, i.e. if the target of
3432    any DW_AT_specification (if any; otherwise the DIE itself) does not
3433    have a parent.  */
3434
3435 /* Compute the scope prefix associated with PDI's parent, in
3436    compilation unit CU.  The result will be allocated on CU's
3437    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3438    field.  NULL is returned if no prefix is necessary.  */
3439 static char *
3440 partial_die_parent_scope (struct partial_die_info *pdi,
3441                           struct dwarf2_cu *cu)
3442 {
3443   char *grandparent_scope;
3444   struct partial_die_info *parent, *real_pdi;
3445
3446   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3447      then this means the parent of the specification DIE.  */
3448
3449   real_pdi = pdi;
3450   while (real_pdi->has_specification)
3451     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3452
3453   parent = real_pdi->die_parent;
3454   if (parent == NULL)
3455     return NULL;
3456
3457   if (parent->scope_set)
3458     return parent->scope;
3459
3460   fixup_partial_die (parent, cu);
3461
3462   grandparent_scope = partial_die_parent_scope (parent, cu);
3463
3464   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3465      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3466      Work around this problem here.  */
3467   if (cu->language == language_cplus
3468       && parent->tag == DW_TAG_namespace
3469       && strcmp (parent->name, "::") == 0
3470       && grandparent_scope == NULL)
3471     {
3472       parent->scope = NULL;
3473       parent->scope_set = 1;
3474       return NULL;
3475     }
3476
3477   if (parent->tag == DW_TAG_namespace
3478       || parent->tag == DW_TAG_module
3479       || parent->tag == DW_TAG_structure_type
3480       || parent->tag == DW_TAG_class_type
3481       || parent->tag == DW_TAG_interface_type
3482       || parent->tag == DW_TAG_union_type
3483       || parent->tag == DW_TAG_enumeration_type)
3484     {
3485       if (grandparent_scope == NULL)
3486         parent->scope = parent->name;
3487       else
3488         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3489                                          parent->name, 0, cu);
3490     }
3491   else if (parent->tag == DW_TAG_enumerator)
3492     /* Enumerators should not get the name of the enumeration as a prefix.  */
3493     parent->scope = grandparent_scope;
3494   else
3495     {
3496       /* FIXME drow/2004-04-01: What should we be doing with
3497          function-local names?  For partial symbols, we should probably be
3498          ignoring them.  */
3499       complaint (&symfile_complaints,
3500                  _("unhandled containing DIE tag %d for DIE at %d"),
3501                  parent->tag, pdi->offset);
3502       parent->scope = grandparent_scope;
3503     }
3504
3505   parent->scope_set = 1;
3506   return parent->scope;
3507 }
3508
3509 /* Return the fully scoped name associated with PDI, from compilation unit
3510    CU.  The result will be allocated with malloc.  */
3511 static char *
3512 partial_die_full_name (struct partial_die_info *pdi,
3513                        struct dwarf2_cu *cu)
3514 {
3515   char *parent_scope;
3516
3517   parent_scope = partial_die_parent_scope (pdi, cu);
3518   if (parent_scope == NULL)
3519     return NULL;
3520   else
3521     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3522 }
3523
3524 static void
3525 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3526 {
3527   struct objfile *objfile = cu->objfile;
3528   CORE_ADDR addr = 0;
3529   char *actual_name = NULL;
3530   const struct partial_symbol *psym = NULL;
3531   CORE_ADDR baseaddr;
3532   int built_actual_name = 0;
3533
3534   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3535
3536   actual_name = partial_die_full_name (pdi, cu);
3537   if (actual_name)
3538     built_actual_name = 1;
3539
3540   if (actual_name == NULL)
3541     actual_name = pdi->name;
3542
3543   switch (pdi->tag)
3544     {
3545     case DW_TAG_subprogram:
3546       if (pdi->is_external || cu->language == language_ada)
3547         {
3548           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3549              of the global scope.  But in Ada, we want to be able to access
3550              nested procedures globally.  So all Ada subprograms are stored
3551              in the global scope.  */
3552           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3553              mst_text, objfile); */
3554           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3555                                       built_actual_name,
3556                                       VAR_DOMAIN, LOC_BLOCK,
3557                                       &objfile->global_psymbols,
3558                                       0, pdi->lowpc + baseaddr,
3559                                       cu->language, objfile);
3560         }
3561       else
3562         {
3563           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3564              mst_file_text, objfile); */
3565           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3566                                       built_actual_name,
3567                                       VAR_DOMAIN, LOC_BLOCK,
3568                                       &objfile->static_psymbols,
3569                                       0, pdi->lowpc + baseaddr,
3570                                       cu->language, objfile);
3571         }
3572       break;
3573     case DW_TAG_variable:
3574       if (pdi->locdesc)
3575         addr = decode_locdesc (pdi->locdesc, cu);
3576
3577       if (pdi->locdesc
3578           && addr == 0
3579           && !dwarf2_per_objfile->has_section_at_zero)
3580         {
3581           /* A global or static variable may also have been stripped
3582              out by the linker if unused, in which case its address
3583              will be nullified; do not add such variables into partial
3584              symbol table then.  */
3585         }
3586       else if (pdi->is_external)
3587         {
3588           /* Global Variable.
3589              Don't enter into the minimal symbol tables as there is
3590              a minimal symbol table entry from the ELF symbols already.
3591              Enter into partial symbol table if it has a location
3592              descriptor or a type.
3593              If the location descriptor is missing, new_symbol will create
3594              a LOC_UNRESOLVED symbol, the address of the variable will then
3595              be determined from the minimal symbol table whenever the variable
3596              is referenced.
3597              The address for the partial symbol table entry is not
3598              used by GDB, but it comes in handy for debugging partial symbol
3599              table building.  */
3600
3601           if (pdi->locdesc || pdi->has_type)
3602             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3603                                         built_actual_name,
3604                                         VAR_DOMAIN, LOC_STATIC,
3605                                         &objfile->global_psymbols,
3606                                         0, addr + baseaddr,
3607                                         cu->language, objfile);
3608         }
3609       else
3610         {
3611           /* Static Variable. Skip symbols without location descriptors.  */
3612           if (pdi->locdesc == NULL)
3613             {
3614               if (built_actual_name)
3615                 xfree (actual_name);
3616               return;
3617             }
3618           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3619              mst_file_data, objfile); */
3620           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3621                                       built_actual_name,
3622                                       VAR_DOMAIN, LOC_STATIC,
3623                                       &objfile->static_psymbols,
3624                                       0, addr + baseaddr,
3625                                       cu->language, objfile);
3626         }
3627       break;
3628     case DW_TAG_typedef:
3629     case DW_TAG_base_type:
3630     case DW_TAG_subrange_type:
3631       add_psymbol_to_list (actual_name, strlen (actual_name),
3632                            built_actual_name,
3633                            VAR_DOMAIN, LOC_TYPEDEF,
3634                            &objfile->static_psymbols,
3635                            0, (CORE_ADDR) 0, cu->language, objfile);
3636       break;
3637     case DW_TAG_namespace:
3638       add_psymbol_to_list (actual_name, strlen (actual_name),
3639                            built_actual_name,
3640                            VAR_DOMAIN, LOC_TYPEDEF,
3641                            &objfile->global_psymbols,
3642                            0, (CORE_ADDR) 0, cu->language, objfile);
3643       break;
3644     case DW_TAG_class_type:
3645     case DW_TAG_interface_type:
3646     case DW_TAG_structure_type:
3647     case DW_TAG_union_type:
3648     case DW_TAG_enumeration_type:
3649       /* Skip external references.  The DWARF standard says in the section
3650          about "Structure, Union, and Class Type Entries": "An incomplete
3651          structure, union or class type is represented by a structure,
3652          union or class entry that does not have a byte size attribute
3653          and that has a DW_AT_declaration attribute."  */
3654       if (!pdi->has_byte_size && pdi->is_declaration)
3655         {
3656           if (built_actual_name)
3657             xfree (actual_name);
3658           return;
3659         }
3660
3661       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3662          static vs. global.  */
3663       add_psymbol_to_list (actual_name, strlen (actual_name),
3664                            built_actual_name,
3665                            STRUCT_DOMAIN, LOC_TYPEDEF,
3666                            (cu->language == language_cplus
3667                             || cu->language == language_java)
3668                            ? &objfile->global_psymbols
3669                            : &objfile->static_psymbols,
3670                            0, (CORE_ADDR) 0, cu->language, objfile);
3671
3672       break;
3673     case DW_TAG_enumerator:
3674       add_psymbol_to_list (actual_name, strlen (actual_name),
3675                            built_actual_name,
3676                            VAR_DOMAIN, LOC_CONST,
3677                            (cu->language == language_cplus
3678                             || cu->language == language_java)
3679                            ? &objfile->global_psymbols
3680                            : &objfile->static_psymbols,
3681                            0, (CORE_ADDR) 0, cu->language, objfile);
3682       break;
3683     default:
3684       break;
3685     }
3686
3687   if (built_actual_name)
3688     xfree (actual_name);
3689 }
3690
3691 /* Read a partial die corresponding to a namespace; also, add a symbol
3692    corresponding to that namespace to the symbol table.  NAMESPACE is
3693    the name of the enclosing namespace.  */
3694
3695 static void
3696 add_partial_namespace (struct partial_die_info *pdi,
3697                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3698                        int need_pc, struct dwarf2_cu *cu)
3699 {
3700   /* Add a symbol for the namespace.  */
3701
3702   add_partial_symbol (pdi, cu);
3703
3704   /* Now scan partial symbols in that namespace.  */
3705
3706   if (pdi->has_children)
3707     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3708 }
3709
3710 /* Read a partial die corresponding to a Fortran module.  */
3711
3712 static void
3713 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3714                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3715 {
3716   /* Now scan partial symbols in that module.  */
3717
3718   if (pdi->has_children)
3719     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3720 }
3721
3722 /* Read a partial die corresponding to a subprogram and create a partial
3723    symbol for that subprogram.  When the CU language allows it, this
3724    routine also defines a partial symbol for each nested subprogram
3725    that this subprogram contains.
3726
3727    DIE my also be a lexical block, in which case we simply search
3728    recursively for suprograms defined inside that lexical block.
3729    Again, this is only performed when the CU language allows this
3730    type of definitions.  */
3731
3732 static void
3733 add_partial_subprogram (struct partial_die_info *pdi,
3734                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3735                         int need_pc, struct dwarf2_cu *cu)
3736 {
3737   if (pdi->tag == DW_TAG_subprogram)
3738     {
3739       if (pdi->has_pc_info)
3740         {
3741           if (pdi->lowpc < *lowpc)
3742             *lowpc = pdi->lowpc;
3743           if (pdi->highpc > *highpc)
3744             *highpc = pdi->highpc;
3745           if (need_pc)
3746             {
3747               CORE_ADDR baseaddr;
3748               struct objfile *objfile = cu->objfile;
3749
3750               baseaddr = ANOFFSET (objfile->section_offsets,
3751                                    SECT_OFF_TEXT (objfile));
3752               addrmap_set_empty (objfile->psymtabs_addrmap,
3753                                  pdi->lowpc + baseaddr,
3754                                  pdi->highpc - 1 + baseaddr,
3755                                  cu->per_cu->v.psymtab);
3756             }
3757           if (!pdi->is_declaration)
3758             /* Ignore subprogram DIEs that do not have a name, they are
3759                illegal.  Do not emit a complaint at this point, we will
3760                do so when we convert this psymtab into a symtab.  */
3761             if (pdi->name)
3762               add_partial_symbol (pdi, cu);
3763         }
3764     }
3765
3766   if (! pdi->has_children)
3767     return;
3768
3769   if (cu->language == language_ada)
3770     {
3771       pdi = pdi->die_child;
3772       while (pdi != NULL)
3773         {
3774           fixup_partial_die (pdi, cu);
3775           if (pdi->tag == DW_TAG_subprogram
3776               || pdi->tag == DW_TAG_lexical_block)
3777             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3778           pdi = pdi->die_sibling;
3779         }
3780     }
3781 }
3782
3783 /* See if we can figure out if the class lives in a namespace.  We do
3784    this by looking for a member function; its demangled name will
3785    contain namespace info, if there is any.  */
3786
3787 static void
3788 guess_structure_name (struct partial_die_info *struct_pdi,
3789                       struct dwarf2_cu *cu)
3790 {
3791   if ((cu->language == language_cplus
3792        || cu->language == language_java)
3793       && cu->has_namespace_info == 0
3794       && struct_pdi->has_children)
3795     {
3796       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3797          what template types look like, because the demangler
3798          frequently doesn't give the same name as the debug info.  We
3799          could fix this by only using the demangled name to get the
3800          prefix (but see comment in read_structure_type).  */
3801
3802       struct partial_die_info *real_pdi;
3803
3804       /* If this DIE (this DIE's specification, if any) has a parent, then
3805          we should not do this.  We'll prepend the parent's fully qualified
3806          name when we create the partial symbol.  */
3807
3808       real_pdi = struct_pdi;
3809       while (real_pdi->has_specification)
3810         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3811
3812       if (real_pdi->die_parent != NULL)
3813         return;
3814     }
3815 }
3816
3817 /* Read a partial die corresponding to an enumeration type.  */
3818
3819 static void
3820 add_partial_enumeration (struct partial_die_info *enum_pdi,
3821                          struct dwarf2_cu *cu)
3822 {
3823   struct partial_die_info *pdi;
3824
3825   if (enum_pdi->name != NULL)
3826     add_partial_symbol (enum_pdi, cu);
3827
3828   pdi = enum_pdi->die_child;
3829   while (pdi)
3830     {
3831       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3832         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3833       else
3834         add_partial_symbol (pdi, cu);
3835       pdi = pdi->die_sibling;
3836     }
3837 }
3838
3839 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3840    Return the corresponding abbrev, or NULL if the number is zero (indicating
3841    an empty DIE).  In either case *BYTES_READ will be set to the length of
3842    the initial number.  */
3843
3844 static struct abbrev_info *
3845 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3846                  struct dwarf2_cu *cu)
3847 {
3848   bfd *abfd = cu->objfile->obfd;
3849   unsigned int abbrev_number;
3850   struct abbrev_info *abbrev;
3851
3852   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3853
3854   if (abbrev_number == 0)
3855     return NULL;
3856
3857   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3858   if (!abbrev)
3859     {
3860       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3861                       bfd_get_filename (abfd));
3862     }
3863
3864   return abbrev;
3865 }
3866
3867 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3868    Returns a pointer to the end of a series of DIEs, terminated by an empty
3869    DIE.  Any children of the skipped DIEs will also be skipped.  */
3870
3871 static gdb_byte *
3872 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3873 {
3874   struct abbrev_info *abbrev;
3875   unsigned int bytes_read;
3876
3877   while (1)
3878     {
3879       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3880       if (abbrev == NULL)
3881         return info_ptr + bytes_read;
3882       else
3883         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3884     }
3885 }
3886
3887 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3888    INFO_PTR should point just after the initial uleb128 of a DIE, and the
3889    abbrev corresponding to that skipped uleb128 should be passed in
3890    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
3891    children.  */
3892
3893 static gdb_byte *
3894 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3895               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3896 {
3897   unsigned int bytes_read;
3898   struct attribute attr;
3899   bfd *abfd = cu->objfile->obfd;
3900   unsigned int form, i;
3901
3902   for (i = 0; i < abbrev->num_attrs; i++)
3903     {
3904       /* The only abbrev we care about is DW_AT_sibling.  */
3905       if (abbrev->attrs[i].name == DW_AT_sibling)
3906         {
3907           read_attribute (&attr, &abbrev->attrs[i],
3908                           abfd, info_ptr, cu);
3909           if (attr.form == DW_FORM_ref_addr)
3910             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
3911           else
3912             return buffer + dwarf2_get_ref_die_offset (&attr);
3913         }
3914
3915       /* If it isn't DW_AT_sibling, skip this attribute.  */
3916       form = abbrev->attrs[i].form;
3917     skip_attribute:
3918       switch (form)
3919         {
3920         case DW_FORM_ref_addr:
3921           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3922              and later it is offset sized.  */
3923           if (cu->header.version == 2)
3924             info_ptr += cu->header.addr_size;
3925           else
3926             info_ptr += cu->header.offset_size;
3927           break;
3928         case DW_FORM_addr:
3929           info_ptr += cu->header.addr_size;
3930           break;
3931         case DW_FORM_data1:
3932         case DW_FORM_ref1:
3933         case DW_FORM_flag:
3934           info_ptr += 1;
3935           break;
3936         case DW_FORM_flag_present:
3937           break;
3938         case DW_FORM_data2:
3939         case DW_FORM_ref2:
3940           info_ptr += 2;
3941           break;
3942         case DW_FORM_data4:
3943         case DW_FORM_ref4:
3944           info_ptr += 4;
3945           break;
3946         case DW_FORM_data8:
3947         case DW_FORM_ref8:
3948         case DW_FORM_sig8:
3949           info_ptr += 8;
3950           break;
3951         case DW_FORM_string:
3952           read_direct_string (abfd, info_ptr, &bytes_read);
3953           info_ptr += bytes_read;
3954           break;
3955         case DW_FORM_sec_offset:
3956         case DW_FORM_strp:
3957           info_ptr += cu->header.offset_size;
3958           break;
3959         case DW_FORM_exprloc:
3960         case DW_FORM_block:
3961           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3962           info_ptr += bytes_read;
3963           break;
3964         case DW_FORM_block1:
3965           info_ptr += 1 + read_1_byte (abfd, info_ptr);
3966           break;
3967         case DW_FORM_block2:
3968           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3969           break;
3970         case DW_FORM_block4:
3971           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3972           break;
3973         case DW_FORM_sdata:
3974         case DW_FORM_udata:
3975         case DW_FORM_ref_udata:
3976           info_ptr = skip_leb128 (abfd, info_ptr);
3977           break;
3978         case DW_FORM_indirect:
3979           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3980           info_ptr += bytes_read;
3981           /* We need to continue parsing from here, so just go back to
3982              the top.  */
3983           goto skip_attribute;
3984
3985         default:
3986           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
3987                  dwarf_form_name (form),
3988                  bfd_get_filename (abfd));
3989         }
3990     }
3991
3992   if (abbrev->has_children)
3993     return skip_children (buffer, info_ptr, cu);
3994   else
3995     return info_ptr;
3996 }
3997
3998 /* Locate ORIG_PDI's sibling.
3999    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4000    in BUFFER.  */
4001
4002 static gdb_byte *
4003 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4004                     gdb_byte *buffer, gdb_byte *info_ptr,
4005                     bfd *abfd, struct dwarf2_cu *cu)
4006 {
4007   /* Do we know the sibling already?  */
4008
4009   if (orig_pdi->sibling)
4010     return orig_pdi->sibling;
4011
4012   /* Are there any children to deal with?  */
4013
4014   if (!orig_pdi->has_children)
4015     return info_ptr;
4016
4017   /* Skip the children the long way.  */
4018
4019   return skip_children (buffer, info_ptr, cu);
4020 }
4021
4022 /* Expand this partial symbol table into a full symbol table.  */
4023
4024 static void
4025 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4026 {
4027   if (pst != NULL)
4028     {
4029       if (pst->readin)
4030         {
4031           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4032         }
4033       else
4034         {
4035           if (info_verbose)
4036             {
4037               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4038               gdb_flush (gdb_stdout);
4039             }
4040
4041           /* Restore our global data.  */
4042           dwarf2_per_objfile = objfile_data (pst->objfile,
4043                                              dwarf2_objfile_data_key);
4044
4045           /* If this psymtab is constructed from a debug-only objfile, the
4046              has_section_at_zero flag will not necessarily be correct.  We
4047              can get the correct value for this flag by looking at the data
4048              associated with the (presumably stripped) associated objfile.  */
4049           if (pst->objfile->separate_debug_objfile_backlink)
4050             {
4051               struct dwarf2_per_objfile *dpo_backlink
4052                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4053                                 dwarf2_objfile_data_key);
4054
4055               dwarf2_per_objfile->has_section_at_zero
4056                 = dpo_backlink->has_section_at_zero;
4057             }
4058
4059           psymtab_to_symtab_1 (pst);
4060
4061           /* Finish up the debug error message.  */
4062           if (info_verbose)
4063             printf_filtered (_("done.\n"));
4064         }
4065     }
4066 }
4067
4068 /* Add PER_CU to the queue.  */
4069
4070 static void
4071 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4072 {
4073   struct dwarf2_queue_item *item;
4074
4075   per_cu->queued = 1;
4076   item = xmalloc (sizeof (*item));
4077   item->per_cu = per_cu;
4078   item->next = NULL;
4079
4080   if (dwarf2_queue == NULL)
4081     dwarf2_queue = item;
4082   else
4083     dwarf2_queue_tail->next = item;
4084
4085   dwarf2_queue_tail = item;
4086 }
4087
4088 /* Process the queue.  */
4089
4090 static void
4091 process_queue (struct objfile *objfile)
4092 {
4093   struct dwarf2_queue_item *item, *next_item;
4094
4095   /* The queue starts out with one item, but following a DIE reference
4096      may load a new CU, adding it to the end of the queue.  */
4097   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4098     {
4099       if (dwarf2_per_objfile->using_index
4100           ? !item->per_cu->v.quick->symtab
4101           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4102         process_full_comp_unit (item->per_cu);
4103
4104       item->per_cu->queued = 0;
4105       next_item = item->next;
4106       xfree (item);
4107     }
4108
4109   dwarf2_queue_tail = NULL;
4110 }
4111
4112 /* Free all allocated queue entries.  This function only releases anything if
4113    an error was thrown; if the queue was processed then it would have been
4114    freed as we went along.  */
4115
4116 static void
4117 dwarf2_release_queue (void *dummy)
4118 {
4119   struct dwarf2_queue_item *item, *last;
4120
4121   item = dwarf2_queue;
4122   while (item)
4123     {
4124       /* Anything still marked queued is likely to be in an
4125          inconsistent state, so discard it.  */
4126       if (item->per_cu->queued)
4127         {
4128           if (item->per_cu->cu != NULL)
4129             free_one_cached_comp_unit (item->per_cu->cu);
4130           item->per_cu->queued = 0;
4131         }
4132
4133       last = item;
4134       item = item->next;
4135       xfree (last);
4136     }
4137
4138   dwarf2_queue = dwarf2_queue_tail = NULL;
4139 }
4140
4141 /* Read in full symbols for PST, and anything it depends on.  */
4142
4143 static void
4144 psymtab_to_symtab_1 (struct partial_symtab *pst)
4145 {
4146   struct dwarf2_per_cu_data *per_cu;
4147   struct cleanup *back_to;
4148   int i;
4149
4150   for (i = 0; i < pst->number_of_dependencies; i++)
4151     if (!pst->dependencies[i]->readin)
4152       {
4153         /* Inform about additional files that need to be read in.  */
4154         if (info_verbose)
4155           {
4156             /* FIXME: i18n: Need to make this a single string.  */
4157             fputs_filtered (" ", gdb_stdout);
4158             wrap_here ("");
4159             fputs_filtered ("and ", gdb_stdout);
4160             wrap_here ("");
4161             printf_filtered ("%s...", pst->dependencies[i]->filename);
4162             wrap_here ("");     /* Flush output */
4163             gdb_flush (gdb_stdout);
4164           }
4165         psymtab_to_symtab_1 (pst->dependencies[i]);
4166       }
4167
4168   per_cu = pst->read_symtab_private;
4169
4170   if (per_cu == NULL)
4171     {
4172       /* It's an include file, no symbols to read for it.
4173          Everything is in the parent symtab.  */
4174       pst->readin = 1;
4175       return;
4176     }
4177
4178   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4179 }
4180
4181 /* Load the DIEs associated with PER_CU into memory.  */
4182
4183 static void
4184 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4185 {
4186   bfd *abfd = objfile->obfd;
4187   struct dwarf2_cu *cu;
4188   unsigned int offset;
4189   gdb_byte *info_ptr, *beg_of_comp_unit;
4190   struct cleanup *back_to, *free_cu_cleanup;
4191   struct attribute *attr;
4192
4193   gdb_assert (! per_cu->from_debug_types);
4194
4195   /* Set local variables from the partial symbol table info.  */
4196   offset = per_cu->offset;
4197
4198   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4199   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4200   beg_of_comp_unit = info_ptr;
4201
4202   cu = alloc_one_comp_unit (objfile);
4203
4204   /* If an error occurs while loading, release our storage.  */
4205   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4206
4207   /* Read in the comp_unit header.  */
4208   info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4209
4210   /* Complete the cu_header.  */
4211   cu->header.offset = offset;
4212   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4213
4214   /* Read the abbrevs for this compilation unit.  */
4215   dwarf2_read_abbrevs (abfd, cu);
4216   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4217
4218   /* Link this compilation unit into the compilation unit tree.  */
4219   per_cu->cu = cu;
4220   cu->per_cu = per_cu;
4221   cu->type_hash = per_cu->type_hash;
4222
4223   cu->dies = read_comp_unit (info_ptr, cu);
4224
4225   /* We try not to read any attributes in this function, because not
4226      all objfiles needed for references have been loaded yet, and symbol
4227      table processing isn't initialized.  But we have to set the CU language,
4228      or we won't be able to build types correctly.  */
4229   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4230   if (attr)
4231     set_cu_language (DW_UNSND (attr), cu);
4232   else
4233     set_cu_language (language_minimal, cu);
4234
4235   /* Similarly, if we do not read the producer, we can not apply
4236      producer-specific interpretation.  */
4237   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4238   if (attr)
4239     cu->producer = DW_STRING (attr);
4240
4241   /* Link this CU into read_in_chain.  */
4242   per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4243   dwarf2_per_objfile->read_in_chain = per_cu;
4244
4245   do_cleanups (back_to);
4246
4247   /* We've successfully allocated this compilation unit.  Let our caller
4248      clean it up when finished with it.  */
4249   discard_cleanups (free_cu_cleanup);
4250 }
4251
4252 /* Generate full symbol information for PST and CU, whose DIEs have
4253    already been loaded into memory.  */
4254
4255 static void
4256 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4257 {
4258   struct dwarf2_cu *cu = per_cu->cu;
4259   struct objfile *objfile = per_cu->objfile;
4260   CORE_ADDR lowpc, highpc;
4261   struct symtab *symtab;
4262   struct cleanup *back_to;
4263   CORE_ADDR baseaddr;
4264
4265   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4266
4267   buildsym_init ();
4268   back_to = make_cleanup (really_free_pendings, NULL);
4269
4270   cu->list_in_scope = &file_symbols;
4271
4272   dwarf2_find_base_address (cu->dies, cu);
4273
4274   /* Do line number decoding in read_file_scope () */
4275   process_die (cu->dies, cu);
4276
4277   /* Some compilers don't define a DW_AT_high_pc attribute for the
4278      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4279      it, by scanning the DIE's below the compilation unit.  */
4280   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4281
4282   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4283
4284   /* Set symtab language to language from DW_AT_language.
4285      If the compilation is from a C file generated by language preprocessors,
4286      do not set the language if it was already deduced by start_subfile.  */
4287   if (symtab != NULL
4288       && !(cu->language == language_c && symtab->language != language_c))
4289     {
4290       symtab->language = cu->language;
4291     }
4292
4293   if (dwarf2_per_objfile->using_index)
4294     per_cu->v.quick->symtab = symtab;
4295   else
4296     {
4297       struct partial_symtab *pst = per_cu->v.psymtab;
4298       pst->symtab = symtab;
4299       pst->readin = 1;
4300     }
4301
4302   do_cleanups (back_to);
4303 }
4304
4305 /* Process a die and its children.  */
4306
4307 static void
4308 process_die (struct die_info *die, struct dwarf2_cu *cu)
4309 {
4310   switch (die->tag)
4311     {
4312     case DW_TAG_padding:
4313       break;
4314     case DW_TAG_compile_unit:
4315       read_file_scope (die, cu);
4316       break;
4317     case DW_TAG_type_unit:
4318       read_type_unit_scope (die, cu);
4319       break;
4320     case DW_TAG_subprogram:
4321     case DW_TAG_inlined_subroutine:
4322       read_func_scope (die, cu);
4323       break;
4324     case DW_TAG_lexical_block:
4325     case DW_TAG_try_block:
4326     case DW_TAG_catch_block:
4327       read_lexical_block_scope (die, cu);
4328       break;
4329     case DW_TAG_class_type:
4330     case DW_TAG_interface_type:
4331     case DW_TAG_structure_type:
4332     case DW_TAG_union_type:
4333       process_structure_scope (die, cu);
4334       break;
4335     case DW_TAG_enumeration_type:
4336       process_enumeration_scope (die, cu);
4337       break;
4338
4339     /* These dies have a type, but processing them does not create
4340        a symbol or recurse to process the children.  Therefore we can
4341        read them on-demand through read_type_die.  */
4342     case DW_TAG_subroutine_type:
4343     case DW_TAG_set_type:
4344     case DW_TAG_array_type:
4345     case DW_TAG_pointer_type:
4346     case DW_TAG_ptr_to_member_type:
4347     case DW_TAG_reference_type:
4348     case DW_TAG_string_type:
4349       break;
4350
4351     case DW_TAG_base_type:
4352     case DW_TAG_subrange_type:
4353     case DW_TAG_typedef:
4354       /* Add a typedef symbol for the type definition, if it has a
4355          DW_AT_name.  */
4356       new_symbol (die, read_type_die (die, cu), cu);
4357       break;
4358     case DW_TAG_common_block:
4359       read_common_block (die, cu);
4360       break;
4361     case DW_TAG_common_inclusion:
4362       break;
4363     case DW_TAG_namespace:
4364       processing_has_namespace_info = 1;
4365       read_namespace (die, cu);
4366       break;
4367     case DW_TAG_module:
4368       processing_has_namespace_info = 1;
4369       read_module (die, cu);
4370       break;
4371     case DW_TAG_imported_declaration:
4372     case DW_TAG_imported_module:
4373       processing_has_namespace_info = 1;
4374       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4375                                  || cu->language != language_fortran))
4376         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4377                    dwarf_tag_name (die->tag));
4378       read_import_statement (die, cu);
4379       break;
4380     default:
4381       new_symbol (die, NULL, cu);
4382       break;
4383     }
4384 }
4385
4386 /* A helper function for dwarf2_compute_name which determines whether DIE
4387    needs to have the name of the scope prepended to the name listed in the
4388    die.  */
4389
4390 static int
4391 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4392 {
4393   struct attribute *attr;
4394
4395   switch (die->tag)
4396     {
4397     case DW_TAG_namespace:
4398     case DW_TAG_typedef:
4399     case DW_TAG_class_type:
4400     case DW_TAG_interface_type:
4401     case DW_TAG_structure_type:
4402     case DW_TAG_union_type:
4403     case DW_TAG_enumeration_type:
4404     case DW_TAG_enumerator:
4405     case DW_TAG_subprogram:
4406     case DW_TAG_member:
4407       return 1;
4408
4409     case DW_TAG_variable:
4410       /* We only need to prefix "globally" visible variables.  These include
4411          any variable marked with DW_AT_external or any variable that
4412          lives in a namespace.  [Variables in anonymous namespaces
4413          require prefixing, but they are not DW_AT_external.]  */
4414
4415       if (dwarf2_attr (die, DW_AT_specification, cu))
4416         {
4417           struct dwarf2_cu *spec_cu = cu;
4418
4419           return die_needs_namespace (die_specification (die, &spec_cu),
4420                                       spec_cu);
4421         }
4422
4423       attr = dwarf2_attr (die, DW_AT_external, cu);
4424       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4425           && die->parent->tag != DW_TAG_module)
4426         return 0;
4427       /* A variable in a lexical block of some kind does not need a
4428          namespace, even though in C++ such variables may be external
4429          and have a mangled name.  */
4430       if (die->parent->tag ==  DW_TAG_lexical_block
4431           || die->parent->tag ==  DW_TAG_try_block
4432           || die->parent->tag ==  DW_TAG_catch_block
4433           || die->parent->tag == DW_TAG_subprogram)
4434         return 0;
4435       return 1;
4436
4437     default:
4438       return 0;
4439     }
4440 }
4441
4442 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4443    compute the physname for the object, which include a method's
4444    formal parameters (C++/Java) and return type (Java).
4445
4446    For Ada, return the DIE's linkage name rather than the fully qualified
4447    name.  PHYSNAME is ignored..
4448
4449    The result is allocated on the objfile_obstack and canonicalized.  */
4450
4451 static const char *
4452 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4453                      int physname)
4454 {
4455   if (name == NULL)
4456     name = dwarf2_name (die, cu);
4457
4458   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4459      compute it by typename_concat inside GDB.  */
4460   if (cu->language == language_ada
4461       || (cu->language == language_fortran && physname))
4462     {
4463       /* For Ada unit, we prefer the linkage name over the name, as
4464          the former contains the exported name, which the user expects
4465          to be able to reference.  Ideally, we want the user to be able
4466          to reference this entity using either natural or linkage name,
4467          but we haven't started looking at this enhancement yet.  */
4468       struct attribute *attr;
4469
4470       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4471       if (attr == NULL)
4472         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4473       if (attr && DW_STRING (attr))
4474         return DW_STRING (attr);
4475     }
4476
4477   /* These are the only languages we know how to qualify names in.  */
4478   if (name != NULL
4479       && (cu->language == language_cplus || cu->language == language_java
4480           || cu->language == language_fortran))
4481     {
4482       if (die_needs_namespace (die, cu))
4483         {
4484           long length;
4485           char *prefix;
4486           struct ui_file *buf;
4487
4488           prefix = determine_prefix (die, cu);
4489           buf = mem_fileopen ();
4490           if (*prefix != '\0')
4491             {
4492               char *prefixed_name = typename_concat (NULL, prefix, name,
4493                                                      physname, cu);
4494
4495               fputs_unfiltered (prefixed_name, buf);
4496               xfree (prefixed_name);
4497             }
4498           else
4499             fputs_unfiltered (name ? name : "", buf);
4500
4501           /* For Java and C++ methods, append formal parameter type
4502              information, if PHYSNAME.  */
4503
4504           if (physname && die->tag == DW_TAG_subprogram
4505               && (cu->language == language_cplus
4506                   || cu->language == language_java))
4507             {
4508               struct type *type = read_type_die (die, cu);
4509
4510               c_type_print_args (type, buf, 0, cu->language);
4511
4512               if (cu->language == language_java)
4513                 {
4514                   /* For java, we must append the return type to method
4515                      names. */
4516                   if (die->tag == DW_TAG_subprogram)
4517                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4518                                      0, 0);
4519                 }
4520               else if (cu->language == language_cplus)
4521                 {
4522                   if (TYPE_NFIELDS (type) > 0
4523                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4524                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4525                     fputs_unfiltered (" const", buf);
4526                 }
4527             }
4528
4529           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4530                                        &length);
4531           ui_file_delete (buf);
4532
4533           if (cu->language == language_cplus)
4534             {
4535               char *cname
4536                 = dwarf2_canonicalize_name (name, cu,
4537                                             &cu->objfile->objfile_obstack);
4538
4539               if (cname != NULL)
4540                 name = cname;
4541             }
4542         }
4543     }
4544
4545   return name;
4546 }
4547
4548 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4549    If scope qualifiers are appropriate they will be added.  The result
4550    will be allocated on the objfile_obstack, or NULL if the DIE does
4551    not have a name.  NAME may either be from a previous call to
4552    dwarf2_name or NULL.
4553
4554    The output string will be canonicalized (if C++/Java). */
4555
4556 static const char *
4557 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4558 {
4559   return dwarf2_compute_name (name, die, cu, 0);
4560 }
4561
4562 /* Construct a physname for the given DIE in CU.  NAME may either be
4563    from a previous call to dwarf2_name or NULL.  The result will be
4564    allocated on the objfile_objstack or NULL if the DIE does not have a
4565    name.
4566
4567    The output string will be canonicalized (if C++/Java).  */
4568
4569 static const char *
4570 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4571 {
4572   return dwarf2_compute_name (name, die, cu, 1);
4573 }
4574
4575 /* Read the import statement specified by the given die and record it.  */
4576
4577 static void
4578 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4579 {
4580   struct attribute *import_attr;
4581   struct die_info *imported_die;
4582   struct dwarf2_cu *imported_cu;
4583   const char *imported_name;
4584   const char *imported_name_prefix;
4585   const char *canonical_name;
4586   const char *import_alias;
4587   const char *imported_declaration = NULL;
4588   const char *import_prefix;
4589
4590   char *temp;
4591
4592   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4593   if (import_attr == NULL)
4594     {
4595       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4596                  dwarf_tag_name (die->tag));
4597       return;
4598     }
4599
4600   imported_cu = cu;
4601   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4602   imported_name = dwarf2_name (imported_die, imported_cu);
4603   if (imported_name == NULL)
4604     {
4605       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4606
4607         The import in the following code:
4608         namespace A
4609           {
4610             typedef int B;
4611           }
4612
4613         int main ()
4614           {
4615             using A::B;
4616             B b;
4617             return b;
4618           }
4619
4620         ...
4621          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4622             <52>   DW_AT_decl_file   : 1
4623             <53>   DW_AT_decl_line   : 6
4624             <54>   DW_AT_import      : <0x75>
4625          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4626             <59>   DW_AT_name        : B
4627             <5b>   DW_AT_decl_file   : 1
4628             <5c>   DW_AT_decl_line   : 2
4629             <5d>   DW_AT_type        : <0x6e>
4630         ...
4631          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4632             <76>   DW_AT_byte_size   : 4
4633             <77>   DW_AT_encoding    : 5        (signed)
4634
4635         imports the wrong die ( 0x75 instead of 0x58 ).
4636         This case will be ignored until the gcc bug is fixed.  */
4637       return;
4638     }
4639
4640   /* Figure out the local name after import.  */
4641   import_alias = dwarf2_name (die, cu);
4642
4643   /* Figure out where the statement is being imported to.  */
4644   import_prefix = determine_prefix (die, cu);
4645
4646   /* Figure out what the scope of the imported die is and prepend it
4647      to the name of the imported die.  */
4648   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4649
4650   if (imported_die->tag != DW_TAG_namespace
4651       && imported_die->tag != DW_TAG_module)
4652     {
4653       imported_declaration = imported_name;
4654       canonical_name = imported_name_prefix;
4655     }
4656   else if (strlen (imported_name_prefix) > 0)
4657     {
4658       temp = alloca (strlen (imported_name_prefix)
4659                      + 2 + strlen (imported_name) + 1);
4660       strcpy (temp, imported_name_prefix);
4661       strcat (temp, "::");
4662       strcat (temp, imported_name);
4663       canonical_name = temp;
4664     }
4665   else
4666     canonical_name = imported_name;
4667
4668   cp_add_using_directive (import_prefix,
4669                           canonical_name,
4670                           import_alias,
4671                           imported_declaration,
4672                           &cu->objfile->objfile_obstack);
4673 }
4674
4675 static void
4676 initialize_cu_func_list (struct dwarf2_cu *cu)
4677 {
4678   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4679 }
4680
4681 static void
4682 free_cu_line_header (void *arg)
4683 {
4684   struct dwarf2_cu *cu = arg;
4685
4686   free_line_header (cu->line_header);
4687   cu->line_header = NULL;
4688 }
4689
4690 static void
4691 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4692                          char **name, char **comp_dir)
4693 {
4694   struct attribute *attr;
4695
4696   *name = NULL;
4697   *comp_dir = NULL;
4698
4699   /* Find the filename.  Do not use dwarf2_name here, since the filename
4700      is not a source language identifier.  */
4701   attr = dwarf2_attr (die, DW_AT_name, cu);
4702   if (attr)
4703     {
4704       *name = DW_STRING (attr);
4705     }
4706
4707   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4708   if (attr)
4709     *comp_dir = DW_STRING (attr);
4710   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4711     {
4712       *comp_dir = ldirname (*name);
4713       if (*comp_dir != NULL)
4714         make_cleanup (xfree, *comp_dir);
4715     }
4716   if (*comp_dir != NULL)
4717     {
4718       /* Irix 6.2 native cc prepends <machine>.: to the compilation
4719          directory, get rid of it.  */
4720       char *cp = strchr (*comp_dir, ':');
4721
4722       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4723         *comp_dir = cp + 1;
4724     }
4725
4726   if (*name == NULL)
4727     *name = "<unknown>";
4728 }
4729
4730 static void
4731 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
4732 {
4733   struct objfile *objfile = cu->objfile;
4734   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4735   CORE_ADDR lowpc = ((CORE_ADDR) -1);
4736   CORE_ADDR highpc = ((CORE_ADDR) 0);
4737   struct attribute *attr;
4738   char *name = NULL;
4739   char *comp_dir = NULL;
4740   struct die_info *child_die;
4741   bfd *abfd = objfile->obfd;
4742   struct line_header *line_header = 0;
4743   CORE_ADDR baseaddr;
4744
4745   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4746
4747   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
4748
4749   /* If we didn't find a lowpc, set it to highpc to avoid complaints
4750      from finish_block.  */
4751   if (lowpc == ((CORE_ADDR) -1))
4752     lowpc = highpc;
4753   lowpc += baseaddr;
4754   highpc += baseaddr;
4755
4756   find_file_and_directory (die, cu, &name, &comp_dir);
4757
4758   attr = dwarf2_attr (die, DW_AT_language, cu);
4759   if (attr)
4760     {
4761       set_cu_language (DW_UNSND (attr), cu);
4762     }
4763
4764   attr = dwarf2_attr (die, DW_AT_producer, cu);
4765   if (attr)
4766     cu->producer = DW_STRING (attr);
4767
4768   /* We assume that we're processing GCC output. */
4769   processing_gcc_compilation = 2;
4770
4771   processing_has_namespace_info = 0;
4772
4773   start_symtab (name, comp_dir, lowpc);
4774   record_debugformat ("DWARF 2");
4775   record_producer (cu->producer);
4776
4777   initialize_cu_func_list (cu);
4778
4779   /* Decode line number information if present.  We do this before
4780      processing child DIEs, so that the line header table is available
4781      for DW_AT_decl_file.  */
4782   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4783   if (attr)
4784     {
4785       unsigned int line_offset = DW_UNSND (attr);
4786       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
4787       if (line_header)
4788         {
4789           cu->line_header = line_header;
4790           make_cleanup (free_cu_line_header, cu);
4791           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
4792         }
4793     }
4794
4795   /* Process all dies in compilation unit.  */
4796   if (die->child != NULL)
4797     {
4798       child_die = die->child;
4799       while (child_die && child_die->tag)
4800         {
4801           process_die (child_die, cu);
4802           child_die = sibling_die (child_die);
4803         }
4804     }
4805
4806   /* Decode macro information, if present.  Dwarf 2 macro information
4807      refers to information in the line number info statement program
4808      header, so we can only read it if we've read the header
4809      successfully.  */
4810   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
4811   if (attr && line_header)
4812     {
4813       unsigned int macro_offset = DW_UNSND (attr);
4814
4815       dwarf_decode_macros (line_header, macro_offset,
4816                            comp_dir, abfd, cu);
4817     }
4818   do_cleanups (back_to);
4819 }
4820
4821 /* For TUs we want to skip the first top level sibling if it's not the
4822    actual type being defined by this TU.  In this case the first top
4823    level sibling is there to provide context only.  */
4824
4825 static void
4826 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4827 {
4828   struct objfile *objfile = cu->objfile;
4829   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4830   CORE_ADDR lowpc;
4831   struct attribute *attr;
4832   char *name = NULL;
4833   char *comp_dir = NULL;
4834   struct die_info *child_die;
4835   bfd *abfd = objfile->obfd;
4836
4837   /* start_symtab needs a low pc, but we don't really have one.
4838      Do what read_file_scope would do in the absence of such info.  */
4839   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4840
4841   /* Find the filename.  Do not use dwarf2_name here, since the filename
4842      is not a source language identifier.  */
4843   attr = dwarf2_attr (die, DW_AT_name, cu);
4844   if (attr)
4845     name = DW_STRING (attr);
4846
4847   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4848   if (attr)
4849     comp_dir = DW_STRING (attr);
4850   else if (name != NULL && IS_ABSOLUTE_PATH (name))
4851     {
4852       comp_dir = ldirname (name);
4853       if (comp_dir != NULL)
4854         make_cleanup (xfree, comp_dir);
4855     }
4856
4857   if (name == NULL)
4858     name = "<unknown>";
4859
4860   attr = dwarf2_attr (die, DW_AT_language, cu);
4861   if (attr)
4862     set_cu_language (DW_UNSND (attr), cu);
4863
4864   /* This isn't technically needed today.  It is done for symmetry
4865      with read_file_scope.  */
4866   attr = dwarf2_attr (die, DW_AT_producer, cu);
4867   if (attr)
4868     cu->producer = DW_STRING (attr);
4869
4870   /* We assume that we're processing GCC output. */
4871   processing_gcc_compilation = 2;
4872
4873   processing_has_namespace_info = 0;
4874
4875   start_symtab (name, comp_dir, lowpc);
4876   record_debugformat ("DWARF 2");
4877   record_producer (cu->producer);
4878
4879   /* Process the dies in the type unit.  */
4880   if (die->child == NULL)
4881     {
4882       dump_die_for_error (die);
4883       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4884              bfd_get_filename (abfd));
4885     }
4886
4887   child_die = die->child;
4888
4889   while (child_die && child_die->tag)
4890     {
4891       process_die (child_die, cu);
4892
4893       child_die = sibling_die (child_die);
4894     }
4895
4896   do_cleanups (back_to);
4897 }
4898
4899 static void
4900 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4901                      struct dwarf2_cu *cu)
4902 {
4903   struct function_range *thisfn;
4904
4905   thisfn = (struct function_range *)
4906     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
4907   thisfn->name = name;
4908   thisfn->lowpc = lowpc;
4909   thisfn->highpc = highpc;
4910   thisfn->seen_line = 0;
4911   thisfn->next = NULL;
4912
4913   if (cu->last_fn == NULL)
4914       cu->first_fn = thisfn;
4915   else
4916       cu->last_fn->next = thisfn;
4917
4918   cu->last_fn = thisfn;
4919 }
4920
4921 /* qsort helper for inherit_abstract_dies.  */
4922
4923 static int
4924 unsigned_int_compar (const void *ap, const void *bp)
4925 {
4926   unsigned int a = *(unsigned int *) ap;
4927   unsigned int b = *(unsigned int *) bp;
4928
4929   return (a > b) - (b > a);
4930 }
4931
4932 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4933    Inherit only the children of the DW_AT_abstract_origin DIE not being already
4934    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
4935
4936 static void
4937 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4938 {
4939   struct die_info *child_die;
4940   unsigned die_children_count;
4941   /* CU offsets which were referenced by children of the current DIE.  */
4942   unsigned *offsets;
4943   unsigned *offsets_end, *offsetp;
4944   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
4945   struct die_info *origin_die;
4946   /* Iterator of the ORIGIN_DIE children.  */
4947   struct die_info *origin_child_die;
4948   struct cleanup *cleanups;
4949   struct attribute *attr;
4950
4951   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4952   if (!attr)
4953     return;
4954
4955   origin_die = follow_die_ref (die, attr, &cu);
4956   if (die->tag != origin_die->tag
4957       && !(die->tag == DW_TAG_inlined_subroutine
4958            && origin_die->tag == DW_TAG_subprogram))
4959     complaint (&symfile_complaints,
4960                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4961                die->offset, origin_die->offset);
4962
4963   child_die = die->child;
4964   die_children_count = 0;
4965   while (child_die && child_die->tag)
4966     {
4967       child_die = sibling_die (child_die);
4968       die_children_count++;
4969     }
4970   offsets = xmalloc (sizeof (*offsets) * die_children_count);
4971   cleanups = make_cleanup (xfree, offsets);
4972
4973   offsets_end = offsets;
4974   child_die = die->child;
4975   while (child_die && child_die->tag)
4976     {
4977       /* For each CHILD_DIE, find the corresponding child of
4978          ORIGIN_DIE.  If there is more than one layer of
4979          DW_AT_abstract_origin, follow them all; there shouldn't be,
4980          but GCC versions at least through 4.4 generate this (GCC PR
4981          40573).  */
4982       struct die_info *child_origin_die = child_die;
4983
4984       while (1)
4985         {
4986           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4987           if (attr == NULL)
4988             break;
4989           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4990         }
4991
4992       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
4993          counterpart may exist.  */
4994       if (child_origin_die != child_die)
4995         {
4996           if (child_die->tag != child_origin_die->tag
4997               && !(child_die->tag == DW_TAG_inlined_subroutine
4998                    && child_origin_die->tag == DW_TAG_subprogram))
4999             complaint (&symfile_complaints,
5000                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5001                          "different tags"), child_die->offset,
5002                        child_origin_die->offset);
5003           if (child_origin_die->parent != origin_die)
5004             complaint (&symfile_complaints,
5005                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5006                          "different parents"), child_die->offset,
5007                        child_origin_die->offset);
5008           else
5009             *offsets_end++ = child_origin_die->offset;
5010         }
5011       child_die = sibling_die (child_die);
5012     }
5013   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5014          unsigned_int_compar);
5015   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5016     if (offsetp[-1] == *offsetp)
5017       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5018                                         "to DIE 0x%x as their abstract origin"),
5019                  die->offset, *offsetp);
5020
5021   offsetp = offsets;
5022   origin_child_die = origin_die->child;
5023   while (origin_child_die && origin_child_die->tag)
5024     {
5025       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5026       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5027         offsetp++;
5028       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5029         {
5030           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5031           process_die (origin_child_die, cu);
5032         }
5033       origin_child_die = sibling_die (origin_child_die);
5034     }
5035
5036   do_cleanups (cleanups);
5037 }
5038
5039 static void
5040 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5041 {
5042   struct objfile *objfile = cu->objfile;
5043   struct context_stack *new;
5044   CORE_ADDR lowpc;
5045   CORE_ADDR highpc;
5046   struct die_info *child_die;
5047   struct attribute *attr, *call_line, *call_file;
5048   char *name;
5049   CORE_ADDR baseaddr;
5050   struct block *block;
5051   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5052
5053   if (inlined_func)
5054     {
5055       /* If we do not have call site information, we can't show the
5056          caller of this inlined function.  That's too confusing, so
5057          only use the scope for local variables.  */
5058       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5059       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5060       if (call_line == NULL || call_file == NULL)
5061         {
5062           read_lexical_block_scope (die, cu);
5063           return;
5064         }
5065     }
5066
5067   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5068
5069   name = dwarf2_name (die, cu);
5070
5071   /* Ignore functions with missing or empty names.  These are actually
5072      illegal according to the DWARF standard.  */
5073   if (name == NULL)
5074     {
5075       complaint (&symfile_complaints,
5076                  _("missing name for subprogram DIE at %d"), die->offset);
5077       return;
5078     }
5079
5080   /* Ignore functions with missing or invalid low and high pc attributes.  */
5081   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5082     {
5083       attr = dwarf2_attr (die, DW_AT_external, cu);
5084       if (!attr || !DW_UNSND (attr))
5085         complaint (&symfile_complaints,
5086                    _("cannot get low and high bounds for subprogram DIE at %d"),
5087                    die->offset);
5088       return;
5089     }
5090
5091   lowpc += baseaddr;
5092   highpc += baseaddr;
5093
5094   /* Record the function range for dwarf_decode_lines.  */
5095   add_to_cu_func_list (name, lowpc, highpc, cu);
5096
5097   new = push_context (0, lowpc);
5098   new->name = new_symbol (die, read_type_die (die, cu), cu);
5099
5100   /* If there is a location expression for DW_AT_frame_base, record
5101      it.  */
5102   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5103   if (attr)
5104     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5105        expression is being recorded directly in the function's symbol
5106        and not in a separate frame-base object.  I guess this hack is
5107        to avoid adding some sort of frame-base adjunct/annex to the
5108        function's symbol :-(.  The problem with doing this is that it
5109        results in a function symbol with a location expression that
5110        has nothing to do with the location of the function, ouch!  The
5111        relationship should be: a function's symbol has-a frame base; a
5112        frame-base has-a location expression.  */
5113     dwarf2_symbol_mark_computed (attr, new->name, cu);
5114
5115   cu->list_in_scope = &local_symbols;
5116
5117   if (die->child != NULL)
5118     {
5119       child_die = die->child;
5120       while (child_die && child_die->tag)
5121         {
5122           process_die (child_die, cu);
5123           child_die = sibling_die (child_die);
5124         }
5125     }
5126
5127   inherit_abstract_dies (die, cu);
5128
5129   /* If we have a DW_AT_specification, we might need to import using
5130      directives from the context of the specification DIE.  See the
5131      comment in determine_prefix.  */
5132   if (cu->language == language_cplus
5133       && dwarf2_attr (die, DW_AT_specification, cu))
5134     {
5135       struct dwarf2_cu *spec_cu = cu;
5136       struct die_info *spec_die = die_specification (die, &spec_cu);
5137
5138       while (spec_die)
5139         {
5140           child_die = spec_die->child;
5141           while (child_die && child_die->tag)
5142             {
5143               if (child_die->tag == DW_TAG_imported_module)
5144                 process_die (child_die, spec_cu);
5145               child_die = sibling_die (child_die);
5146             }
5147
5148           /* In some cases, GCC generates specification DIEs that
5149              themselves contain DW_AT_specification attributes.  */
5150           spec_die = die_specification (spec_die, &spec_cu);
5151         }
5152     }
5153
5154   new = pop_context ();
5155   /* Make a block for the local symbols within.  */
5156   block = finish_block (new->name, &local_symbols, new->old_blocks,
5157                         lowpc, highpc, objfile);
5158
5159   /* For C++, set the block's scope.  */
5160   if (cu->language == language_cplus || cu->language == language_fortran)
5161     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5162                         determine_prefix (die, cu),
5163                         processing_has_namespace_info);
5164
5165   /* If we have address ranges, record them.  */
5166   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5167
5168   /* In C++, we can have functions nested inside functions (e.g., when
5169      a function declares a class that has methods).  This means that
5170      when we finish processing a function scope, we may need to go
5171      back to building a containing block's symbol lists.  */
5172   local_symbols = new->locals;
5173   param_symbols = new->params;
5174   using_directives = new->using_directives;
5175
5176   /* If we've finished processing a top-level function, subsequent
5177      symbols go in the file symbol list.  */
5178   if (outermost_context_p ())
5179     cu->list_in_scope = &file_symbols;
5180 }
5181
5182 /* Process all the DIES contained within a lexical block scope.  Start
5183    a new scope, process the dies, and then close the scope.  */
5184
5185 static void
5186 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5187 {
5188   struct objfile *objfile = cu->objfile;
5189   struct context_stack *new;
5190   CORE_ADDR lowpc, highpc;
5191   struct die_info *child_die;
5192   CORE_ADDR baseaddr;
5193
5194   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5195
5196   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5197   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5198      as multiple lexical blocks?  Handling children in a sane way would
5199      be nasty.  Might be easier to properly extend generic blocks to
5200      describe ranges.  */
5201   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5202     return;
5203   lowpc += baseaddr;
5204   highpc += baseaddr;
5205
5206   push_context (0, lowpc);
5207   if (die->child != NULL)
5208     {
5209       child_die = die->child;
5210       while (child_die && child_die->tag)
5211         {
5212           process_die (child_die, cu);
5213           child_die = sibling_die (child_die);
5214         }
5215     }
5216   new = pop_context ();
5217
5218   if (local_symbols != NULL || using_directives != NULL)
5219     {
5220       struct block *block
5221         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5222                         highpc, objfile);
5223
5224       /* Note that recording ranges after traversing children, as we
5225          do here, means that recording a parent's ranges entails
5226          walking across all its children's ranges as they appear in
5227          the address map, which is quadratic behavior.
5228
5229          It would be nicer to record the parent's ranges before
5230          traversing its children, simply overriding whatever you find
5231          there.  But since we don't even decide whether to create a
5232          block until after we've traversed its children, that's hard
5233          to do.  */
5234       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5235     }
5236   local_symbols = new->locals;
5237   using_directives = new->using_directives;
5238 }
5239
5240 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5241    Return 1 if the attributes are present and valid, otherwise, return 0.
5242    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5243
5244 static int
5245 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5246                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5247                     struct partial_symtab *ranges_pst)
5248 {
5249   struct objfile *objfile = cu->objfile;
5250   struct comp_unit_head *cu_header = &cu->header;
5251   bfd *obfd = objfile->obfd;
5252   unsigned int addr_size = cu_header->addr_size;
5253   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5254   /* Base address selection entry.  */
5255   CORE_ADDR base;
5256   int found_base;
5257   unsigned int dummy;
5258   gdb_byte *buffer;
5259   CORE_ADDR marker;
5260   int low_set;
5261   CORE_ADDR low = 0;
5262   CORE_ADDR high = 0;
5263   CORE_ADDR baseaddr;
5264
5265   found_base = cu->base_known;
5266   base = cu->base_address;
5267
5268   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5269   if (offset >= dwarf2_per_objfile->ranges.size)
5270     {
5271       complaint (&symfile_complaints,
5272                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5273                  offset);
5274       return 0;
5275     }
5276   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5277
5278   /* Read in the largest possible address.  */
5279   marker = read_address (obfd, buffer, cu, &dummy);
5280   if ((marker & mask) == mask)
5281     {
5282       /* If we found the largest possible address, then
5283          read the base address.  */
5284       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5285       buffer += 2 * addr_size;
5286       offset += 2 * addr_size;
5287       found_base = 1;
5288     }
5289
5290   low_set = 0;
5291
5292   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5293
5294   while (1)
5295     {
5296       CORE_ADDR range_beginning, range_end;
5297
5298       range_beginning = read_address (obfd, buffer, cu, &dummy);
5299       buffer += addr_size;
5300       range_end = read_address (obfd, buffer, cu, &dummy);
5301       buffer += addr_size;
5302       offset += 2 * addr_size;
5303
5304       /* An end of list marker is a pair of zero addresses.  */
5305       if (range_beginning == 0 && range_end == 0)
5306         /* Found the end of list entry.  */
5307         break;
5308
5309       /* Each base address selection entry is a pair of 2 values.
5310          The first is the largest possible address, the second is
5311          the base address.  Check for a base address here.  */
5312       if ((range_beginning & mask) == mask)
5313         {
5314           /* If we found the largest possible address, then
5315              read the base address.  */
5316           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5317           found_base = 1;
5318           continue;
5319         }
5320
5321       if (!found_base)
5322         {
5323           /* We have no valid base address for the ranges
5324              data.  */
5325           complaint (&symfile_complaints,
5326                      _("Invalid .debug_ranges data (no base address)"));
5327           return 0;
5328         }
5329
5330       range_beginning += base;
5331       range_end += base;
5332
5333       if (ranges_pst != NULL && range_beginning < range_end)
5334         addrmap_set_empty (objfile->psymtabs_addrmap,
5335                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5336                            ranges_pst);
5337
5338       /* FIXME: This is recording everything as a low-high
5339          segment of consecutive addresses.  We should have a
5340          data structure for discontiguous block ranges
5341          instead.  */
5342       if (! low_set)
5343         {
5344           low = range_beginning;
5345           high = range_end;
5346           low_set = 1;
5347         }
5348       else
5349         {
5350           if (range_beginning < low)
5351             low = range_beginning;
5352           if (range_end > high)
5353             high = range_end;
5354         }
5355     }
5356
5357   if (! low_set)
5358     /* If the first entry is an end-of-list marker, the range
5359        describes an empty scope, i.e. no instructions.  */
5360     return 0;
5361
5362   if (low_return)
5363     *low_return = low;
5364   if (high_return)
5365     *high_return = high;
5366   return 1;
5367 }
5368
5369 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5370    are present and valid, otherwise, return 0.  Return -1 if the range is
5371    discontinuous, i.e. derived from DW_AT_ranges information.  */
5372 static int
5373 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5374                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5375                       struct partial_symtab *pst)
5376 {
5377   struct attribute *attr;
5378   CORE_ADDR low = 0;
5379   CORE_ADDR high = 0;
5380   int ret = 0;
5381
5382   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5383   if (attr)
5384     {
5385       high = DW_ADDR (attr);
5386       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5387       if (attr)
5388         low = DW_ADDR (attr);
5389       else
5390         /* Found high w/o low attribute.  */
5391         return 0;
5392
5393       /* Found consecutive range of addresses.  */
5394       ret = 1;
5395     }
5396   else
5397     {
5398       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5399       if (attr != NULL)
5400         {
5401           /* Value of the DW_AT_ranges attribute is the offset in the
5402              .debug_ranges section.  */
5403           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5404             return 0;
5405           /* Found discontinuous range of addresses.  */
5406           ret = -1;
5407         }
5408     }
5409
5410   if (high < low)
5411     return 0;
5412
5413   /* When using the GNU linker, .gnu.linkonce. sections are used to
5414      eliminate duplicate copies of functions and vtables and such.
5415      The linker will arbitrarily choose one and discard the others.
5416      The AT_*_pc values for such functions refer to local labels in
5417      these sections.  If the section from that file was discarded, the
5418      labels are not in the output, so the relocs get a value of 0.
5419      If this is a discarded function, mark the pc bounds as invalid,
5420      so that GDB will ignore it.  */
5421   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5422     return 0;
5423
5424   *lowpc = low;
5425   *highpc = high;
5426   return ret;
5427 }
5428
5429 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5430    its low and high PC addresses.  Do nothing if these addresses could not
5431    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5432    and HIGHPC to the high address if greater than HIGHPC.  */
5433
5434 static void
5435 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5436                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5437                                  struct dwarf2_cu *cu)
5438 {
5439   CORE_ADDR low, high;
5440   struct die_info *child = die->child;
5441
5442   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5443     {
5444       *lowpc = min (*lowpc, low);
5445       *highpc = max (*highpc, high);
5446     }
5447
5448   /* If the language does not allow nested subprograms (either inside
5449      subprograms or lexical blocks), we're done.  */
5450   if (cu->language != language_ada)
5451     return;
5452
5453   /* Check all the children of the given DIE.  If it contains nested
5454      subprograms, then check their pc bounds.  Likewise, we need to
5455      check lexical blocks as well, as they may also contain subprogram
5456      definitions.  */
5457   while (child && child->tag)
5458     {
5459       if (child->tag == DW_TAG_subprogram
5460           || child->tag == DW_TAG_lexical_block)
5461         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5462       child = sibling_die (child);
5463     }
5464 }
5465
5466 /* Get the low and high pc's represented by the scope DIE, and store
5467    them in *LOWPC and *HIGHPC.  If the correct values can't be
5468    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5469
5470 static void
5471 get_scope_pc_bounds (struct die_info *die,
5472                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5473                      struct dwarf2_cu *cu)
5474 {
5475   CORE_ADDR best_low = (CORE_ADDR) -1;
5476   CORE_ADDR best_high = (CORE_ADDR) 0;
5477   CORE_ADDR current_low, current_high;
5478
5479   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5480     {
5481       best_low = current_low;
5482       best_high = current_high;
5483     }
5484   else
5485     {
5486       struct die_info *child = die->child;
5487
5488       while (child && child->tag)
5489         {
5490           switch (child->tag) {
5491           case DW_TAG_subprogram:
5492             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5493             break;
5494           case DW_TAG_namespace:
5495           case DW_TAG_module:
5496             /* FIXME: carlton/2004-01-16: Should we do this for
5497                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5498                that current GCC's always emit the DIEs corresponding
5499                to definitions of methods of classes as children of a
5500                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5501                the DIEs giving the declarations, which could be
5502                anywhere).  But I don't see any reason why the
5503                standards says that they have to be there.  */
5504             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5505
5506             if (current_low != ((CORE_ADDR) -1))
5507               {
5508                 best_low = min (best_low, current_low);
5509                 best_high = max (best_high, current_high);
5510               }
5511             break;
5512           default:
5513             /* Ignore. */
5514             break;
5515           }
5516
5517           child = sibling_die (child);
5518         }
5519     }
5520
5521   *lowpc = best_low;
5522   *highpc = best_high;
5523 }
5524
5525 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5526    in DIE.  */
5527 static void
5528 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5529                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5530 {
5531   struct attribute *attr;
5532
5533   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5534   if (attr)
5535     {
5536       CORE_ADDR high = DW_ADDR (attr);
5537
5538       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5539       if (attr)
5540         {
5541           CORE_ADDR low = DW_ADDR (attr);
5542
5543           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5544         }
5545     }
5546
5547   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5548   if (attr)
5549     {
5550       bfd *obfd = cu->objfile->obfd;
5551
5552       /* The value of the DW_AT_ranges attribute is the offset of the
5553          address range list in the .debug_ranges section.  */
5554       unsigned long offset = DW_UNSND (attr);
5555       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5556
5557       /* For some target architectures, but not others, the
5558          read_address function sign-extends the addresses it returns.
5559          To recognize base address selection entries, we need a
5560          mask.  */
5561       unsigned int addr_size = cu->header.addr_size;
5562       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5563
5564       /* The base address, to which the next pair is relative.  Note
5565          that this 'base' is a DWARF concept: most entries in a range
5566          list are relative, to reduce the number of relocs against the
5567          debugging information.  This is separate from this function's
5568          'baseaddr' argument, which GDB uses to relocate debugging
5569          information from a shared library based on the address at
5570          which the library was loaded.  */
5571       CORE_ADDR base = cu->base_address;
5572       int base_known = cu->base_known;
5573
5574       gdb_assert (dwarf2_per_objfile->ranges.readin);
5575       if (offset >= dwarf2_per_objfile->ranges.size)
5576         {
5577           complaint (&symfile_complaints,
5578                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5579                      offset);
5580           return;
5581         }
5582
5583       for (;;)
5584         {
5585           unsigned int bytes_read;
5586           CORE_ADDR start, end;
5587
5588           start = read_address (obfd, buffer, cu, &bytes_read);
5589           buffer += bytes_read;
5590           end = read_address (obfd, buffer, cu, &bytes_read);
5591           buffer += bytes_read;
5592
5593           /* Did we find the end of the range list?  */
5594           if (start == 0 && end == 0)
5595             break;
5596
5597           /* Did we find a base address selection entry?  */
5598           else if ((start & base_select_mask) == base_select_mask)
5599             {
5600               base = end;
5601               base_known = 1;
5602             }
5603
5604           /* We found an ordinary address range.  */
5605           else
5606             {
5607               if (!base_known)
5608                 {
5609                   complaint (&symfile_complaints,
5610                              _("Invalid .debug_ranges data (no base address)"));
5611                   return;
5612                 }
5613
5614               record_block_range (block,
5615                                   baseaddr + base + start,
5616                                   baseaddr + base + end - 1);
5617             }
5618         }
5619     }
5620 }
5621
5622 /* Add an aggregate field to the field list.  */
5623
5624 static void
5625 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5626                   struct dwarf2_cu *cu)
5627 {
5628   struct objfile *objfile = cu->objfile;
5629   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5630   struct nextfield *new_field;
5631   struct attribute *attr;
5632   struct field *fp;
5633   char *fieldname = "";
5634
5635   /* Allocate a new field list entry and link it in.  */
5636   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5637   make_cleanup (xfree, new_field);
5638   memset (new_field, 0, sizeof (struct nextfield));
5639
5640   if (die->tag == DW_TAG_inheritance)
5641     {
5642       new_field->next = fip->baseclasses;
5643       fip->baseclasses = new_field;
5644     }
5645   else
5646     {
5647       new_field->next = fip->fields;
5648       fip->fields = new_field;
5649     }
5650   fip->nfields++;
5651
5652   /* Handle accessibility and virtuality of field.
5653      The default accessibility for members is public, the default
5654      accessibility for inheritance is private.  */
5655   if (die->tag != DW_TAG_inheritance)
5656     new_field->accessibility = DW_ACCESS_public;
5657   else
5658     new_field->accessibility = DW_ACCESS_private;
5659   new_field->virtuality = DW_VIRTUALITY_none;
5660
5661   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5662   if (attr)
5663     new_field->accessibility = DW_UNSND (attr);
5664   if (new_field->accessibility != DW_ACCESS_public)
5665     fip->non_public_fields = 1;
5666   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5667   if (attr)
5668     new_field->virtuality = DW_UNSND (attr);
5669
5670   fp = &new_field->field;
5671
5672   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
5673     {
5674       /* Data member other than a C++ static data member.  */
5675
5676       /* Get type of field.  */
5677       fp->type = die_type (die, cu);
5678
5679       SET_FIELD_BITPOS (*fp, 0);
5680
5681       /* Get bit size of field (zero if none).  */
5682       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
5683       if (attr)
5684         {
5685           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5686         }
5687       else
5688         {
5689           FIELD_BITSIZE (*fp) = 0;
5690         }
5691
5692       /* Get bit offset of field.  */
5693       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5694       if (attr)
5695         {
5696           int byte_offset = 0;
5697
5698           if (attr_form_is_section_offset (attr))
5699             dwarf2_complex_location_expr_complaint ();
5700           else if (attr_form_is_constant (attr))
5701             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5702           else if (attr_form_is_block (attr))
5703             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5704           else
5705             dwarf2_complex_location_expr_complaint ();
5706
5707           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5708         }
5709       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
5710       if (attr)
5711         {
5712           if (gdbarch_bits_big_endian (gdbarch))
5713             {
5714               /* For big endian bits, the DW_AT_bit_offset gives the
5715                  additional bit offset from the MSB of the containing
5716                  anonymous object to the MSB of the field.  We don't
5717                  have to do anything special since we don't need to
5718                  know the size of the anonymous object.  */
5719               FIELD_BITPOS (*fp) += DW_UNSND (attr);
5720             }
5721           else
5722             {
5723               /* For little endian bits, compute the bit offset to the
5724                  MSB of the anonymous object, subtract off the number of
5725                  bits from the MSB of the field to the MSB of the
5726                  object, and then subtract off the number of bits of
5727                  the field itself.  The result is the bit offset of
5728                  the LSB of the field.  */
5729               int anonymous_size;
5730               int bit_offset = DW_UNSND (attr);
5731
5732               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5733               if (attr)
5734                 {
5735                   /* The size of the anonymous object containing
5736                      the bit field is explicit, so use the
5737                      indicated size (in bytes).  */
5738                   anonymous_size = DW_UNSND (attr);
5739                 }
5740               else
5741                 {
5742                   /* The size of the anonymous object containing
5743                      the bit field must be inferred from the type
5744                      attribute of the data member containing the
5745                      bit field.  */
5746                   anonymous_size = TYPE_LENGTH (fp->type);
5747                 }
5748               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5749                 - bit_offset - FIELD_BITSIZE (*fp);
5750             }
5751         }
5752
5753       /* Get name of field.  */
5754       fieldname = dwarf2_name (die, cu);
5755       if (fieldname == NULL)
5756         fieldname = "";
5757
5758       /* The name is already allocated along with this objfile, so we don't
5759          need to duplicate it for the type.  */
5760       fp->name = fieldname;
5761
5762       /* Change accessibility for artificial fields (e.g. virtual table
5763          pointer or virtual base class pointer) to private.  */
5764       if (dwarf2_attr (die, DW_AT_artificial, cu))
5765         {
5766           FIELD_ARTIFICIAL (*fp) = 1;
5767           new_field->accessibility = DW_ACCESS_private;
5768           fip->non_public_fields = 1;
5769         }
5770     }
5771   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
5772     {
5773       /* C++ static member.  */
5774
5775       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5776          is a declaration, but all versions of G++ as of this writing
5777          (so through at least 3.2.1) incorrectly generate
5778          DW_TAG_variable tags.  */
5779
5780       char *physname;
5781
5782       /* Get name of field.  */
5783       fieldname = dwarf2_name (die, cu);
5784       if (fieldname == NULL)
5785         return;
5786
5787       attr = dwarf2_attr (die, DW_AT_const_value, cu);
5788       if (attr
5789           /* Only create a symbol if this is an external value.
5790              new_symbol checks this and puts the value in the global symbol
5791              table, which we want.  If it is not external, new_symbol
5792              will try to put the value in cu->list_in_scope which is wrong.  */
5793           && dwarf2_flag_true_p (die, DW_AT_external, cu))
5794         {
5795           /* A static const member, not much different than an enum as far as
5796              we're concerned, except that we can support more types.  */
5797           new_symbol (die, NULL, cu);
5798         }
5799
5800       /* Get physical name.  */
5801       physname = (char *) dwarf2_physname (fieldname, die, cu);
5802
5803       /* The name is already allocated along with this objfile, so we don't
5804          need to duplicate it for the type.  */
5805       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5806       FIELD_TYPE (*fp) = die_type (die, cu);
5807       FIELD_NAME (*fp) = fieldname;
5808     }
5809   else if (die->tag == DW_TAG_inheritance)
5810     {
5811       /* C++ base class field.  */
5812       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5813       if (attr)
5814         {
5815           int byte_offset = 0;
5816
5817           if (attr_form_is_section_offset (attr))
5818             dwarf2_complex_location_expr_complaint ();
5819           else if (attr_form_is_constant (attr))
5820             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5821           else if (attr_form_is_block (attr))
5822             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5823           else
5824             dwarf2_complex_location_expr_complaint ();
5825
5826           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5827         }
5828       FIELD_BITSIZE (*fp) = 0;
5829       FIELD_TYPE (*fp) = die_type (die, cu);
5830       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5831       fip->nbaseclasses++;
5832     }
5833 }
5834
5835 /* Add a typedef defined in the scope of the FIP's class.  */
5836
5837 static void
5838 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5839                     struct dwarf2_cu *cu)
5840 {
5841   struct objfile *objfile = cu->objfile;
5842   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5843   struct typedef_field_list *new_field;
5844   struct attribute *attr;
5845   struct typedef_field *fp;
5846   char *fieldname = "";
5847
5848   /* Allocate a new field list entry and link it in.  */
5849   new_field = xzalloc (sizeof (*new_field));
5850   make_cleanup (xfree, new_field);
5851
5852   gdb_assert (die->tag == DW_TAG_typedef);
5853
5854   fp = &new_field->field;
5855
5856   /* Get name of field.  */
5857   fp->name = dwarf2_name (die, cu);
5858   if (fp->name == NULL)
5859     return;
5860
5861   fp->type = read_type_die (die, cu);
5862
5863   new_field->next = fip->typedef_field_list;
5864   fip->typedef_field_list = new_field;
5865   fip->typedef_field_list_count++;
5866 }
5867
5868 /* Create the vector of fields, and attach it to the type.  */
5869
5870 static void
5871 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
5872                               struct dwarf2_cu *cu)
5873 {
5874   int nfields = fip->nfields;
5875
5876   /* Record the field count, allocate space for the array of fields,
5877      and create blank accessibility bitfields if necessary.  */
5878   TYPE_NFIELDS (type) = nfields;
5879   TYPE_FIELDS (type) = (struct field *)
5880     TYPE_ALLOC (type, sizeof (struct field) * nfields);
5881   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5882
5883   if (fip->non_public_fields && cu->language != language_ada)
5884     {
5885       ALLOCATE_CPLUS_STRUCT_TYPE (type);
5886
5887       TYPE_FIELD_PRIVATE_BITS (type) =
5888         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5889       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5890
5891       TYPE_FIELD_PROTECTED_BITS (type) =
5892         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5893       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5894
5895       TYPE_FIELD_IGNORE_BITS (type) =
5896         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5897       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5898     }
5899
5900   /* If the type has baseclasses, allocate and clear a bit vector for
5901      TYPE_FIELD_VIRTUAL_BITS.  */
5902   if (fip->nbaseclasses && cu->language != language_ada)
5903     {
5904       int num_bytes = B_BYTES (fip->nbaseclasses);
5905       unsigned char *pointer;
5906
5907       ALLOCATE_CPLUS_STRUCT_TYPE (type);
5908       pointer = TYPE_ALLOC (type, num_bytes);
5909       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
5910       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5911       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5912     }
5913
5914   /* Copy the saved-up fields into the field vector.  Start from the head
5915      of the list, adding to the tail of the field array, so that they end
5916      up in the same order in the array in which they were added to the list.  */
5917   while (nfields-- > 0)
5918     {
5919       struct nextfield *fieldp;
5920
5921       if (fip->fields)
5922         {
5923           fieldp = fip->fields;
5924           fip->fields = fieldp->next;
5925         }
5926       else
5927         {
5928           fieldp = fip->baseclasses;
5929           fip->baseclasses = fieldp->next;
5930         }
5931
5932       TYPE_FIELD (type, nfields) = fieldp->field;
5933       switch (fieldp->accessibility)
5934         {
5935         case DW_ACCESS_private:
5936           if (cu->language != language_ada)
5937             SET_TYPE_FIELD_PRIVATE (type, nfields);
5938           break;
5939
5940         case DW_ACCESS_protected:
5941           if (cu->language != language_ada)
5942             SET_TYPE_FIELD_PROTECTED (type, nfields);
5943           break;
5944
5945         case DW_ACCESS_public:
5946           break;
5947
5948         default:
5949           /* Unknown accessibility.  Complain and treat it as public.  */
5950           {
5951             complaint (&symfile_complaints, _("unsupported accessibility %d"),
5952                        fieldp->accessibility);
5953           }
5954           break;
5955         }
5956       if (nfields < fip->nbaseclasses)
5957         {
5958           switch (fieldp->virtuality)
5959             {
5960             case DW_VIRTUALITY_virtual:
5961             case DW_VIRTUALITY_pure_virtual:
5962               if (cu->language == language_ada)
5963                 error ("unexpected virtuality in component of Ada type");
5964               SET_TYPE_FIELD_VIRTUAL (type, nfields);
5965               break;
5966             }
5967         }
5968     }
5969 }
5970
5971 /* Add a member function to the proper fieldlist.  */
5972
5973 static void
5974 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
5975                       struct type *type, struct dwarf2_cu *cu)
5976 {
5977   struct objfile *objfile = cu->objfile;
5978   struct attribute *attr;
5979   struct fnfieldlist *flp;
5980   int i;
5981   struct fn_field *fnp;
5982   char *fieldname;
5983   char *physname;
5984   struct nextfnfield *new_fnfield;
5985   struct type *this_type;
5986
5987   if (cu->language == language_ada)
5988     error ("unexpected member function in Ada type");
5989
5990   /* Get name of member function.  */
5991   fieldname = dwarf2_name (die, cu);
5992   if (fieldname == NULL)
5993     return;
5994
5995   /* Get the mangled name.  */
5996   physname = (char *) dwarf2_physname (fieldname, die, cu);
5997
5998   /* Look up member function name in fieldlist.  */
5999   for (i = 0; i < fip->nfnfields; i++)
6000     {
6001       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6002         break;
6003     }
6004
6005   /* Create new list element if necessary.  */
6006   if (i < fip->nfnfields)
6007     flp = &fip->fnfieldlists[i];
6008   else
6009     {
6010       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6011         {
6012           fip->fnfieldlists = (struct fnfieldlist *)
6013             xrealloc (fip->fnfieldlists,
6014                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6015                       * sizeof (struct fnfieldlist));
6016           if (fip->nfnfields == 0)
6017             make_cleanup (free_current_contents, &fip->fnfieldlists);
6018         }
6019       flp = &fip->fnfieldlists[fip->nfnfields];
6020       flp->name = fieldname;
6021       flp->length = 0;
6022       flp->head = NULL;
6023       fip->nfnfields++;
6024     }
6025
6026   /* Create a new member function field and chain it to the field list
6027      entry. */
6028   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6029   make_cleanup (xfree, new_fnfield);
6030   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6031   new_fnfield->next = flp->head;
6032   flp->head = new_fnfield;
6033   flp->length++;
6034
6035   /* Fill in the member function field info.  */
6036   fnp = &new_fnfield->fnfield;
6037   /* The name is already allocated along with this objfile, so we don't
6038      need to duplicate it for the type.  */
6039   fnp->physname = physname ? physname : "";
6040   fnp->type = alloc_type (objfile);
6041   this_type = read_type_die (die, cu);
6042   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6043     {
6044       int nparams = TYPE_NFIELDS (this_type);
6045
6046       /* TYPE is the domain of this method, and THIS_TYPE is the type
6047            of the method itself (TYPE_CODE_METHOD).  */
6048       smash_to_method_type (fnp->type, type,
6049                             TYPE_TARGET_TYPE (this_type),
6050                             TYPE_FIELDS (this_type),
6051                             TYPE_NFIELDS (this_type),
6052                             TYPE_VARARGS (this_type));
6053
6054       /* Handle static member functions.
6055          Dwarf2 has no clean way to discern C++ static and non-static
6056          member functions. G++ helps GDB by marking the first
6057          parameter for non-static member functions (which is the
6058          this pointer) as artificial. We obtain this information
6059          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6060       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6061         fnp->voffset = VOFFSET_STATIC;
6062     }
6063   else
6064     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6065                physname);
6066
6067   /* Get fcontext from DW_AT_containing_type if present.  */
6068   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6069     fnp->fcontext = die_containing_type (die, cu);
6070
6071   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6072      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6073
6074   /* Get accessibility.  */
6075   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6076   if (attr)
6077     {
6078       switch (DW_UNSND (attr))
6079         {
6080         case DW_ACCESS_private:
6081           fnp->is_private = 1;
6082           break;
6083         case DW_ACCESS_protected:
6084           fnp->is_protected = 1;
6085           break;
6086         }
6087     }
6088
6089   /* Check for artificial methods.  */
6090   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6091   if (attr && DW_UNSND (attr) != 0)
6092     fnp->is_artificial = 1;
6093
6094   /* Get index in virtual function table if it is a virtual member
6095      function.  For older versions of GCC, this is an offset in the
6096      appropriate virtual table, as specified by DW_AT_containing_type.
6097      For everyone else, it is an expression to be evaluated relative
6098      to the object address.  */
6099
6100   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6101   if (attr)
6102     {
6103       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6104         {
6105           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6106             {
6107               /* Old-style GCC.  */
6108               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6109             }
6110           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6111                    || (DW_BLOCK (attr)->size > 1
6112                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6113                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6114             {
6115               struct dwarf_block blk;
6116               int offset;
6117
6118               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6119                         ? 1 : 2);
6120               blk.size = DW_BLOCK (attr)->size - offset;
6121               blk.data = DW_BLOCK (attr)->data + offset;
6122               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6123               if ((fnp->voffset % cu->header.addr_size) != 0)
6124                 dwarf2_complex_location_expr_complaint ();
6125               else
6126                 fnp->voffset /= cu->header.addr_size;
6127               fnp->voffset += 2;
6128             }
6129           else
6130             dwarf2_complex_location_expr_complaint ();
6131
6132           if (!fnp->fcontext)
6133             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6134         }
6135       else if (attr_form_is_section_offset (attr))
6136         {
6137           dwarf2_complex_location_expr_complaint ();
6138         }
6139       else
6140         {
6141           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6142                                                  fieldname);
6143         }
6144     }
6145   else
6146     {
6147       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6148       if (attr && DW_UNSND (attr))
6149         {
6150           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6151           complaint (&symfile_complaints,
6152                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6153                      fieldname, die->offset);
6154           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6155           TYPE_CPLUS_DYNAMIC (type) = 1;
6156         }
6157     }
6158 }
6159
6160 /* Create the vector of member function fields, and attach it to the type.  */
6161
6162 static void
6163 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6164                                  struct dwarf2_cu *cu)
6165 {
6166   struct fnfieldlist *flp;
6167   int total_length = 0;
6168   int i;
6169
6170   if (cu->language == language_ada)
6171     error ("unexpected member functions in Ada type");
6172
6173   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6174   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6175     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6176
6177   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6178     {
6179       struct nextfnfield *nfp = flp->head;
6180       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6181       int k;
6182
6183       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6184       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6185       fn_flp->fn_fields = (struct fn_field *)
6186         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6187       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6188         fn_flp->fn_fields[k] = nfp->fnfield;
6189
6190       total_length += flp->length;
6191     }
6192
6193   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6194   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6195 }
6196
6197 /* Returns non-zero if NAME is the name of a vtable member in CU's
6198    language, zero otherwise.  */
6199 static int
6200 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6201 {
6202   static const char vptr[] = "_vptr";
6203   static const char vtable[] = "vtable";
6204
6205   /* Look for the C++ and Java forms of the vtable.  */
6206   if ((cu->language == language_java
6207        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6208        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6209        && is_cplus_marker (name[sizeof (vptr) - 1])))
6210     return 1;
6211
6212   return 0;
6213 }
6214
6215 /* GCC outputs unnamed structures that are really pointers to member
6216    functions, with the ABI-specified layout.  If TYPE describes
6217    such a structure, smash it into a member function type.
6218
6219    GCC shouldn't do this; it should just output pointer to member DIEs.
6220    This is GCC PR debug/28767.  */
6221
6222 static void
6223 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6224 {
6225   struct type *pfn_type, *domain_type, *new_type;
6226
6227   /* Check for a structure with no name and two children.  */
6228   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6229     return;
6230
6231   /* Check for __pfn and __delta members.  */
6232   if (TYPE_FIELD_NAME (type, 0) == NULL
6233       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6234       || TYPE_FIELD_NAME (type, 1) == NULL
6235       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6236     return;
6237
6238   /* Find the type of the method.  */
6239   pfn_type = TYPE_FIELD_TYPE (type, 0);
6240   if (pfn_type == NULL
6241       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6242       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6243     return;
6244
6245   /* Look for the "this" argument.  */
6246   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6247   if (TYPE_NFIELDS (pfn_type) == 0
6248       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6249       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6250     return;
6251
6252   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6253   new_type = alloc_type (objfile);
6254   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6255                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6256                         TYPE_VARARGS (pfn_type));
6257   smash_to_methodptr_type (type, new_type);
6258 }
6259
6260 /* Called when we find the DIE that starts a structure or union scope
6261    (definition) to process all dies that define the members of the
6262    structure or union.
6263
6264    NOTE: we need to call struct_type regardless of whether or not the
6265    DIE has an at_name attribute, since it might be an anonymous
6266    structure or union.  This gets the type entered into our set of
6267    user defined types.
6268
6269    However, if the structure is incomplete (an opaque struct/union)
6270    then suppress creating a symbol table entry for it since gdb only
6271    wants to find the one with the complete definition.  Note that if
6272    it is complete, we just call new_symbol, which does it's own
6273    checking about whether the struct/union is anonymous or not (and
6274    suppresses creating a symbol table entry itself).  */
6275
6276 static struct type *
6277 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6278 {
6279   struct objfile *objfile = cu->objfile;
6280   struct type *type;
6281   struct attribute *attr;
6282   char *name;
6283   struct cleanup *back_to;
6284
6285   /* If the definition of this type lives in .debug_types, read that type.
6286      Don't follow DW_AT_specification though, that will take us back up
6287      the chain and we want to go down.  */
6288   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6289   if (attr)
6290     {
6291       struct dwarf2_cu *type_cu = cu;
6292       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6293
6294       /* We could just recurse on read_structure_type, but we need to call
6295          get_die_type to ensure only one type for this DIE is created.
6296          This is important, for example, because for c++ classes we need
6297          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6298       type = read_type_die (type_die, type_cu);
6299       return set_die_type (die, type, cu);
6300     }
6301
6302   back_to = make_cleanup (null_cleanup, 0);
6303
6304   type = alloc_type (objfile);
6305   INIT_CPLUS_SPECIFIC (type);
6306
6307   name = dwarf2_name (die, cu);
6308   if (name != NULL)
6309     {
6310       if (cu->language == language_cplus
6311           || cu->language == language_java)
6312         {
6313           TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6314           if (die->tag == DW_TAG_structure_type
6315               || die->tag == DW_TAG_class_type)
6316             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6317         }
6318       else
6319         {
6320           /* The name is already allocated along with this objfile, so
6321              we don't need to duplicate it for the type.  */
6322           TYPE_TAG_NAME (type) = (char *) name;
6323           if (die->tag == DW_TAG_class_type)
6324             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6325         }
6326     }
6327
6328   if (die->tag == DW_TAG_structure_type)
6329     {
6330       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6331     }
6332   else if (die->tag == DW_TAG_union_type)
6333     {
6334       TYPE_CODE (type) = TYPE_CODE_UNION;
6335     }
6336   else
6337     {
6338       TYPE_CODE (type) = TYPE_CODE_CLASS;
6339     }
6340
6341   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6342     TYPE_DECLARED_CLASS (type) = 1;
6343
6344   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6345   if (attr)
6346     {
6347       TYPE_LENGTH (type) = DW_UNSND (attr);
6348     }
6349   else
6350     {
6351       TYPE_LENGTH (type) = 0;
6352     }
6353
6354   TYPE_STUB_SUPPORTED (type) = 1;
6355   if (die_is_declaration (die, cu))
6356     TYPE_STUB (type) = 1;
6357   else if (attr == NULL && die->child == NULL
6358            && producer_is_realview (cu->producer))
6359     /* RealView does not output the required DW_AT_declaration
6360        on incomplete types.  */
6361     TYPE_STUB (type) = 1;
6362
6363   /* We need to add the type field to the die immediately so we don't
6364      infinitely recurse when dealing with pointers to the structure
6365      type within the structure itself. */
6366   set_die_type (die, type, cu);
6367
6368   /* set_die_type should be already done.  */
6369   set_descriptive_type (type, die, cu);
6370
6371   if (die->child != NULL && ! die_is_declaration (die, cu))
6372     {
6373       struct field_info fi;
6374       struct die_info *child_die;
6375
6376       memset (&fi, 0, sizeof (struct field_info));
6377
6378       child_die = die->child;
6379
6380       while (child_die && child_die->tag)
6381         {
6382           if (child_die->tag == DW_TAG_member
6383               || child_die->tag == DW_TAG_variable)
6384             {
6385               /* NOTE: carlton/2002-11-05: A C++ static data member
6386                  should be a DW_TAG_member that is a declaration, but
6387                  all versions of G++ as of this writing (so through at
6388                  least 3.2.1) incorrectly generate DW_TAG_variable
6389                  tags for them instead.  */
6390               dwarf2_add_field (&fi, child_die, cu);
6391             }
6392           else if (child_die->tag == DW_TAG_subprogram)
6393             {
6394               /* C++ member function. */
6395               dwarf2_add_member_fn (&fi, child_die, type, cu);
6396             }
6397           else if (child_die->tag == DW_TAG_inheritance)
6398             {
6399               /* C++ base class field.  */
6400               dwarf2_add_field (&fi, child_die, cu);
6401             }
6402           else if (child_die->tag == DW_TAG_typedef)
6403             dwarf2_add_typedef (&fi, child_die, cu);
6404           child_die = sibling_die (child_die);
6405         }
6406
6407       /* Attach fields and member functions to the type.  */
6408       if (fi.nfields)
6409         dwarf2_attach_fields_to_type (&fi, type, cu);
6410       if (fi.nfnfields)
6411         {
6412           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6413
6414           /* Get the type which refers to the base class (possibly this
6415              class itself) which contains the vtable pointer for the current
6416              class from the DW_AT_containing_type attribute.  This use of
6417              DW_AT_containing_type is a GNU extension.  */
6418
6419           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6420             {
6421               struct type *t = die_containing_type (die, cu);
6422
6423               TYPE_VPTR_BASETYPE (type) = t;
6424               if (type == t)
6425                 {
6426                   int i;
6427
6428                   /* Our own class provides vtbl ptr.  */
6429                   for (i = TYPE_NFIELDS (t) - 1;
6430                        i >= TYPE_N_BASECLASSES (t);
6431                        --i)
6432                     {
6433                       char *fieldname = TYPE_FIELD_NAME (t, i);
6434
6435                       if (is_vtable_name (fieldname, cu))
6436                         {
6437                           TYPE_VPTR_FIELDNO (type) = i;
6438                           break;
6439                         }
6440                     }
6441
6442                   /* Complain if virtual function table field not found.  */
6443                   if (i < TYPE_N_BASECLASSES (t))
6444                     complaint (&symfile_complaints,
6445                                _("virtual function table pointer not found when defining class '%s'"),
6446                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6447                                "");
6448                 }
6449               else
6450                 {
6451                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6452                 }
6453             }
6454           else if (cu->producer
6455                    && strncmp (cu->producer,
6456                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6457             {
6458               /* The IBM XLC compiler does not provide direct indication
6459                  of the containing type, but the vtable pointer is
6460                  always named __vfp.  */
6461
6462               int i;
6463
6464               for (i = TYPE_NFIELDS (type) - 1;
6465                    i >= TYPE_N_BASECLASSES (type);
6466                    --i)
6467                 {
6468                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6469                     {
6470                       TYPE_VPTR_FIELDNO (type) = i;
6471                       TYPE_VPTR_BASETYPE (type) = type;
6472                       break;
6473                     }
6474                 }
6475             }
6476         }
6477
6478       /* Copy fi.typedef_field_list linked list elements content into the
6479          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6480       if (fi.typedef_field_list)
6481         {
6482           int i = fi.typedef_field_list_count;
6483
6484           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6485           TYPE_TYPEDEF_FIELD_ARRAY (type)
6486             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6487           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6488
6489           /* Reverse the list order to keep the debug info elements order.  */
6490           while (--i >= 0)
6491             {
6492               struct typedef_field *dest, *src;
6493
6494               dest = &TYPE_TYPEDEF_FIELD (type, i);
6495               src = &fi.typedef_field_list->field;
6496               fi.typedef_field_list = fi.typedef_field_list->next;
6497               *dest = *src;
6498             }
6499         }
6500     }
6501
6502   quirk_gcc_member_function_pointer (type, cu->objfile);
6503
6504   do_cleanups (back_to);
6505   return type;
6506 }
6507
6508 static void
6509 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6510 {
6511   struct die_info *child_die = die->child;
6512   struct type *this_type;
6513
6514   this_type = get_die_type (die, cu);
6515   if (this_type == NULL)
6516     this_type = read_structure_type (die, cu);
6517
6518   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6519      snapshots) has been known to create a die giving a declaration
6520      for a class that has, as a child, a die giving a definition for a
6521      nested class.  So we have to process our children even if the
6522      current die is a declaration.  Normally, of course, a declaration
6523      won't have any children at all.  */
6524
6525   while (child_die != NULL && child_die->tag)
6526     {
6527       if (child_die->tag == DW_TAG_member
6528           || child_die->tag == DW_TAG_variable
6529           || child_die->tag == DW_TAG_inheritance)
6530         {
6531           /* Do nothing.  */
6532         }
6533       else
6534         process_die (child_die, cu);
6535
6536       child_die = sibling_die (child_die);
6537     }
6538
6539   /* Do not consider external references.  According to the DWARF standard,
6540      these DIEs are identified by the fact that they have no byte_size
6541      attribute, and a declaration attribute.  */
6542   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6543       || !die_is_declaration (die, cu))
6544     new_symbol (die, this_type, cu);
6545 }
6546
6547 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6548    complete the type's fields yet, or create any symbols.  */
6549
6550 static struct type *
6551 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6552 {
6553   struct objfile *objfile = cu->objfile;
6554   struct type *type;
6555   struct attribute *attr;
6556   const char *name;
6557
6558   /* If the definition of this type lives in .debug_types, read that type.
6559      Don't follow DW_AT_specification though, that will take us back up
6560      the chain and we want to go down.  */
6561   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6562   if (attr)
6563     {
6564       struct dwarf2_cu *type_cu = cu;
6565       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6566
6567       type = read_type_die (type_die, type_cu);
6568       return set_die_type (die, type, cu);
6569     }
6570
6571   type = alloc_type (objfile);
6572
6573   TYPE_CODE (type) = TYPE_CODE_ENUM;
6574   name = dwarf2_full_name (NULL, die, cu);
6575   if (name != NULL)
6576     TYPE_TAG_NAME (type) = (char *) name;
6577
6578   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6579   if (attr)
6580     {
6581       TYPE_LENGTH (type) = DW_UNSND (attr);
6582     }
6583   else
6584     {
6585       TYPE_LENGTH (type) = 0;
6586     }
6587
6588   /* The enumeration DIE can be incomplete.  In Ada, any type can be
6589      declared as private in the package spec, and then defined only
6590      inside the package body.  Such types are known as Taft Amendment
6591      Types.  When another package uses such a type, an incomplete DIE
6592      may be generated by the compiler.  */
6593   if (die_is_declaration (die, cu))
6594     TYPE_STUB (type) = 1;
6595
6596   return set_die_type (die, type, cu);
6597 }
6598
6599 /* Given a pointer to a die which begins an enumeration, process all
6600    the dies that define the members of the enumeration, and create the
6601    symbol for the enumeration type.
6602
6603    NOTE: We reverse the order of the element list.  */
6604
6605 static void
6606 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6607 {
6608   struct die_info *child_die;
6609   struct field *fields;
6610   struct symbol *sym;
6611   int num_fields;
6612   int unsigned_enum = 1;
6613   char *name;
6614   struct type *this_type;
6615
6616   num_fields = 0;
6617   fields = NULL;
6618   this_type = get_die_type (die, cu);
6619   if (this_type == NULL)
6620     this_type = read_enumeration_type (die, cu);
6621   if (die->child != NULL)
6622     {
6623       child_die = die->child;
6624       while (child_die && child_die->tag)
6625         {
6626           if (child_die->tag != DW_TAG_enumerator)
6627             {
6628               process_die (child_die, cu);
6629             }
6630           else
6631             {
6632               name = dwarf2_name (child_die, cu);
6633               if (name)
6634                 {
6635                   sym = new_symbol (child_die, this_type, cu);
6636                   if (SYMBOL_VALUE (sym) < 0)
6637                     unsigned_enum = 0;
6638
6639                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6640                     {
6641                       fields = (struct field *)
6642                         xrealloc (fields,
6643                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
6644                                   * sizeof (struct field));
6645                     }
6646
6647                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
6648                   FIELD_TYPE (fields[num_fields]) = NULL;
6649                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
6650                   FIELD_BITSIZE (fields[num_fields]) = 0;
6651
6652                   num_fields++;
6653                 }
6654             }
6655
6656           child_die = sibling_die (child_die);
6657         }
6658
6659       if (num_fields)
6660         {
6661           TYPE_NFIELDS (this_type) = num_fields;
6662           TYPE_FIELDS (this_type) = (struct field *)
6663             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6664           memcpy (TYPE_FIELDS (this_type), fields,
6665                   sizeof (struct field) * num_fields);
6666           xfree (fields);
6667         }
6668       if (unsigned_enum)
6669         TYPE_UNSIGNED (this_type) = 1;
6670     }
6671
6672   new_symbol (die, this_type, cu);
6673 }
6674
6675 /* Extract all information from a DW_TAG_array_type DIE and put it in
6676    the DIE's type field.  For now, this only handles one dimensional
6677    arrays.  */
6678
6679 static struct type *
6680 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
6681 {
6682   struct objfile *objfile = cu->objfile;
6683   struct die_info *child_die;
6684   struct type *type;
6685   struct type *element_type, *range_type, *index_type;
6686   struct type **range_types = NULL;
6687   struct attribute *attr;
6688   int ndim = 0;
6689   struct cleanup *back_to;
6690   char *name;
6691
6692   element_type = die_type (die, cu);
6693
6694   /* The die_type call above may have already set the type for this DIE.  */
6695   type = get_die_type (die, cu);
6696   if (type)
6697     return type;
6698
6699   /* Irix 6.2 native cc creates array types without children for
6700      arrays with unspecified length.  */
6701   if (die->child == NULL)
6702     {
6703       index_type = objfile_type (objfile)->builtin_int;
6704       range_type = create_range_type (NULL, index_type, 0, -1);
6705       type = create_array_type (NULL, element_type, range_type);
6706       return set_die_type (die, type, cu);
6707     }
6708
6709   back_to = make_cleanup (null_cleanup, NULL);
6710   child_die = die->child;
6711   while (child_die && child_die->tag)
6712     {
6713       if (child_die->tag == DW_TAG_subrange_type)
6714         {
6715           struct type *child_type = read_type_die (child_die, cu);
6716
6717           if (child_type != NULL)
6718             {
6719               /* The range type was succesfully read. Save it for
6720                  the array type creation.  */
6721               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6722                 {
6723                   range_types = (struct type **)
6724                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6725                               * sizeof (struct type *));
6726                   if (ndim == 0)
6727                     make_cleanup (free_current_contents, &range_types);
6728                 }
6729               range_types[ndim++] = child_type;
6730             }
6731         }
6732       child_die = sibling_die (child_die);
6733     }
6734
6735   /* Dwarf2 dimensions are output from left to right, create the
6736      necessary array types in backwards order.  */
6737
6738   type = element_type;
6739
6740   if (read_array_order (die, cu) == DW_ORD_col_major)
6741     {
6742       int i = 0;
6743
6744       while (i < ndim)
6745         type = create_array_type (NULL, type, range_types[i++]);
6746     }
6747   else
6748     {
6749       while (ndim-- > 0)
6750         type = create_array_type (NULL, type, range_types[ndim]);
6751     }
6752
6753   /* Understand Dwarf2 support for vector types (like they occur on
6754      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
6755      array type.  This is not part of the Dwarf2/3 standard yet, but a
6756      custom vendor extension.  The main difference between a regular
6757      array and the vector variant is that vectors are passed by value
6758      to functions.  */
6759   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
6760   if (attr)
6761     make_vector_type (type);
6762
6763   name = dwarf2_name (die, cu);
6764   if (name)
6765     TYPE_NAME (type) = name;
6766
6767   /* Install the type in the die. */
6768   set_die_type (die, type, cu);
6769
6770   /* set_die_type should be already done.  */
6771   set_descriptive_type (type, die, cu);
6772
6773   do_cleanups (back_to);
6774
6775   return type;
6776 }
6777
6778 static enum dwarf_array_dim_ordering
6779 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
6780 {
6781   struct attribute *attr;
6782
6783   attr = dwarf2_attr (die, DW_AT_ordering, cu);
6784
6785   if (attr) return DW_SND (attr);
6786
6787   /*
6788     GNU F77 is a special case, as at 08/2004 array type info is the
6789     opposite order to the dwarf2 specification, but data is still
6790     laid out as per normal fortran.
6791
6792     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
6793     version checking.
6794   */
6795
6796   if (cu->language == language_fortran
6797       && cu->producer && strstr (cu->producer, "GNU F77"))
6798     {
6799       return DW_ORD_row_major;
6800     }
6801
6802   switch (cu->language_defn->la_array_ordering)
6803     {
6804     case array_column_major:
6805       return DW_ORD_col_major;
6806     case array_row_major:
6807     default:
6808       return DW_ORD_row_major;
6809     };
6810 }
6811
6812 /* Extract all information from a DW_TAG_set_type DIE and put it in
6813    the DIE's type field. */
6814
6815 static struct type *
6816 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6817 {
6818   struct type *domain_type, *set_type;
6819   struct attribute *attr;
6820
6821   domain_type = die_type (die, cu);
6822
6823   /* The die_type call above may have already set the type for this DIE.  */
6824   set_type = get_die_type (die, cu);
6825   if (set_type)
6826     return set_type;
6827
6828   set_type = create_set_type (NULL, domain_type);
6829
6830   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6831   if (attr)
6832     TYPE_LENGTH (set_type) = DW_UNSND (attr);
6833
6834   return set_die_type (die, set_type, cu);
6835 }
6836
6837 /* First cut: install each common block member as a global variable.  */
6838
6839 static void
6840 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
6841 {
6842   struct die_info *child_die;
6843   struct attribute *attr;
6844   struct symbol *sym;
6845   CORE_ADDR base = (CORE_ADDR) 0;
6846
6847   attr = dwarf2_attr (die, DW_AT_location, cu);
6848   if (attr)
6849     {
6850       /* Support the .debug_loc offsets */
6851       if (attr_form_is_block (attr))
6852         {
6853           base = decode_locdesc (DW_BLOCK (attr), cu);
6854         }
6855       else if (attr_form_is_section_offset (attr))
6856         {
6857           dwarf2_complex_location_expr_complaint ();
6858         }
6859       else
6860         {
6861           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6862                                                  "common block member");
6863         }
6864     }
6865   if (die->child != NULL)
6866     {
6867       child_die = die->child;
6868       while (child_die && child_die->tag)
6869         {
6870           sym = new_symbol (child_die, NULL, cu);
6871           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
6872           if (attr)
6873             {
6874               CORE_ADDR byte_offset = 0;
6875
6876               if (attr_form_is_section_offset (attr))
6877                 dwarf2_complex_location_expr_complaint ();
6878               else if (attr_form_is_constant (attr))
6879                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6880               else if (attr_form_is_block (attr))
6881                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6882               else
6883                 dwarf2_complex_location_expr_complaint ();
6884
6885               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
6886               add_symbol_to_list (sym, &global_symbols);
6887             }
6888           child_die = sibling_die (child_die);
6889         }
6890     }
6891 }
6892
6893 /* Create a type for a C++ namespace.  */
6894
6895 static struct type *
6896 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
6897 {
6898   struct objfile *objfile = cu->objfile;
6899   const char *previous_prefix, *name;
6900   int is_anonymous;
6901   struct type *type;
6902
6903   /* For extensions, reuse the type of the original namespace.  */
6904   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6905     {
6906       struct die_info *ext_die;
6907       struct dwarf2_cu *ext_cu = cu;
6908
6909       ext_die = dwarf2_extension (die, &ext_cu);
6910       type = read_type_die (ext_die, ext_cu);
6911       return set_die_type (die, type, cu);
6912     }
6913
6914   name = namespace_name (die, &is_anonymous, cu);
6915
6916   /* Now build the name of the current namespace.  */
6917
6918   previous_prefix = determine_prefix (die, cu);
6919   if (previous_prefix[0] != '\0')
6920     name = typename_concat (&objfile->objfile_obstack,
6921                             previous_prefix, name, 0, cu);
6922
6923   /* Create the type.  */
6924   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
6925                     objfile);
6926   TYPE_NAME (type) = (char *) name;
6927   TYPE_TAG_NAME (type) = TYPE_NAME (type);
6928
6929   return set_die_type (die, type, cu);
6930 }
6931
6932 /* Read a C++ namespace.  */
6933
6934 static void
6935 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
6936 {
6937   struct objfile *objfile = cu->objfile;
6938   const char *name;
6939   int is_anonymous;
6940
6941   /* Add a symbol associated to this if we haven't seen the namespace
6942      before.  Also, add a using directive if it's an anonymous
6943      namespace.  */
6944
6945   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
6946     {
6947       struct type *type;
6948
6949       type = read_type_die (die, cu);
6950       new_symbol (die, type, cu);
6951
6952       name = namespace_name (die, &is_anonymous, cu);
6953       if (is_anonymous)
6954         {
6955           const char *previous_prefix = determine_prefix (die, cu);
6956
6957           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
6958                                   NULL, &objfile->objfile_obstack);
6959         }
6960     }
6961
6962   if (die->child != NULL)
6963     {
6964       struct die_info *child_die = die->child;
6965
6966       while (child_die && child_die->tag)
6967         {
6968           process_die (child_die, cu);
6969           child_die = sibling_die (child_die);
6970         }
6971     }
6972 }
6973
6974 /* Read a Fortran module as type.  This DIE can be only a declaration used for
6975    imported module.  Still we need that type as local Fortran "use ... only"
6976    declaration imports depend on the created type in determine_prefix.  */
6977
6978 static struct type *
6979 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
6980 {
6981   struct objfile *objfile = cu->objfile;
6982   char *module_name;
6983   struct type *type;
6984
6985   module_name = dwarf2_name (die, cu);
6986   if (!module_name)
6987     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
6988                die->offset);
6989   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
6990
6991   /* determine_prefix uses TYPE_TAG_NAME.  */
6992   TYPE_TAG_NAME (type) = TYPE_NAME (type);
6993
6994   return set_die_type (die, type, cu);
6995 }
6996
6997 /* Read a Fortran module.  */
6998
6999 static void
7000 read_module (struct die_info *die, struct dwarf2_cu *cu)
7001 {
7002   struct die_info *child_die = die->child;
7003
7004   while (child_die && child_die->tag)
7005     {
7006       process_die (child_die, cu);
7007       child_die = sibling_die (child_die);
7008     }
7009 }
7010
7011 /* Return the name of the namespace represented by DIE.  Set
7012    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7013    namespace.  */
7014
7015 static const char *
7016 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7017 {
7018   struct die_info *current_die;
7019   const char *name = NULL;
7020
7021   /* Loop through the extensions until we find a name.  */
7022
7023   for (current_die = die;
7024        current_die != NULL;
7025        current_die = dwarf2_extension (die, &cu))
7026     {
7027       name = dwarf2_name (current_die, cu);
7028       if (name != NULL)
7029         break;
7030     }
7031
7032   /* Is it an anonymous namespace?  */
7033
7034   *is_anonymous = (name == NULL);
7035   if (*is_anonymous)
7036     name = "(anonymous namespace)";
7037
7038   return name;
7039 }
7040
7041 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7042    the user defined type vector.  */
7043
7044 static struct type *
7045 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7046 {
7047   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7048   struct comp_unit_head *cu_header = &cu->header;
7049   struct type *type;
7050   struct attribute *attr_byte_size;
7051   struct attribute *attr_address_class;
7052   int byte_size, addr_class;
7053   struct type *target_type;
7054
7055   target_type = die_type (die, cu);
7056
7057   /* The die_type call above may have already set the type for this DIE.  */
7058   type = get_die_type (die, cu);
7059   if (type)
7060     return type;
7061
7062   type = lookup_pointer_type (target_type);
7063
7064   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7065   if (attr_byte_size)
7066     byte_size = DW_UNSND (attr_byte_size);
7067   else
7068     byte_size = cu_header->addr_size;
7069
7070   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7071   if (attr_address_class)
7072     addr_class = DW_UNSND (attr_address_class);
7073   else
7074     addr_class = DW_ADDR_none;
7075
7076   /* If the pointer size or address class is different than the
7077      default, create a type variant marked as such and set the
7078      length accordingly.  */
7079   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7080     {
7081       if (gdbarch_address_class_type_flags_p (gdbarch))
7082         {
7083           int type_flags;
7084
7085           type_flags = gdbarch_address_class_type_flags
7086                          (gdbarch, byte_size, addr_class);
7087           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7088                       == 0);
7089           type = make_type_with_address_space (type, type_flags);
7090         }
7091       else if (TYPE_LENGTH (type) != byte_size)
7092         {
7093           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7094         }
7095       else
7096         {
7097           /* Should we also complain about unhandled address classes?  */
7098         }
7099     }
7100
7101   TYPE_LENGTH (type) = byte_size;
7102   return set_die_type (die, type, cu);
7103 }
7104
7105 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7106    the user defined type vector.  */
7107
7108 static struct type *
7109 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7110 {
7111   struct type *type;
7112   struct type *to_type;
7113   struct type *domain;
7114
7115   to_type = die_type (die, cu);
7116   domain = die_containing_type (die, cu);
7117
7118   /* The calls above may have already set the type for this DIE.  */
7119   type = get_die_type (die, cu);
7120   if (type)
7121     return type;
7122
7123   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7124     type = lookup_methodptr_type (to_type);
7125   else
7126     type = lookup_memberptr_type (to_type, domain);
7127
7128   return set_die_type (die, type, cu);
7129 }
7130
7131 /* Extract all information from a DW_TAG_reference_type DIE and add to
7132    the user defined type vector.  */
7133
7134 static struct type *
7135 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7136 {
7137   struct comp_unit_head *cu_header = &cu->header;
7138   struct type *type, *target_type;
7139   struct attribute *attr;
7140
7141   target_type = die_type (die, cu);
7142
7143   /* The die_type call above may have already set the type for this DIE.  */
7144   type = get_die_type (die, cu);
7145   if (type)
7146     return type;
7147
7148   type = lookup_reference_type (target_type);
7149   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7150   if (attr)
7151     {
7152       TYPE_LENGTH (type) = DW_UNSND (attr);
7153     }
7154   else
7155     {
7156       TYPE_LENGTH (type) = cu_header->addr_size;
7157     }
7158   return set_die_type (die, type, cu);
7159 }
7160
7161 static struct type *
7162 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7163 {
7164   struct type *base_type, *cv_type;
7165
7166   base_type = die_type (die, cu);
7167
7168   /* The die_type call above may have already set the type for this DIE.  */
7169   cv_type = get_die_type (die, cu);
7170   if (cv_type)
7171     return cv_type;
7172
7173   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7174   return set_die_type (die, cv_type, cu);
7175 }
7176
7177 static struct type *
7178 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7179 {
7180   struct type *base_type, *cv_type;
7181
7182   base_type = die_type (die, cu);
7183
7184   /* The die_type call above may have already set the type for this DIE.  */
7185   cv_type = get_die_type (die, cu);
7186   if (cv_type)
7187     return cv_type;
7188
7189   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7190   return set_die_type (die, cv_type, cu);
7191 }
7192
7193 /* Extract all information from a DW_TAG_string_type DIE and add to
7194    the user defined type vector.  It isn't really a user defined type,
7195    but it behaves like one, with other DIE's using an AT_user_def_type
7196    attribute to reference it.  */
7197
7198 static struct type *
7199 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7200 {
7201   struct objfile *objfile = cu->objfile;
7202   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7203   struct type *type, *range_type, *index_type, *char_type;
7204   struct attribute *attr;
7205   unsigned int length;
7206
7207   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7208   if (attr)
7209     {
7210       length = DW_UNSND (attr);
7211     }
7212   else
7213     {
7214       /* check for the DW_AT_byte_size attribute */
7215       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7216       if (attr)
7217         {
7218           length = DW_UNSND (attr);
7219         }
7220       else
7221         {
7222           length = 1;
7223         }
7224     }
7225
7226   index_type = objfile_type (objfile)->builtin_int;
7227   range_type = create_range_type (NULL, index_type, 1, length);
7228   char_type = language_string_char_type (cu->language_defn, gdbarch);
7229   type = create_string_type (NULL, char_type, range_type);
7230
7231   return set_die_type (die, type, cu);
7232 }
7233
7234 /* Handle DIES due to C code like:
7235
7236    struct foo
7237    {
7238    int (*funcp)(int a, long l);
7239    int b;
7240    };
7241
7242    ('funcp' generates a DW_TAG_subroutine_type DIE)
7243  */
7244
7245 static struct type *
7246 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7247 {
7248   struct type *type;            /* Type that this function returns */
7249   struct type *ftype;           /* Function that returns above type */
7250   struct attribute *attr;
7251
7252   type = die_type (die, cu);
7253
7254   /* The die_type call above may have already set the type for this DIE.  */
7255   ftype = get_die_type (die, cu);
7256   if (ftype)
7257     return ftype;
7258
7259   ftype = lookup_function_type (type);
7260
7261   /* All functions in C++, Pascal and Java have prototypes.  */
7262   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7263   if ((attr && (DW_UNSND (attr) != 0))
7264       || cu->language == language_cplus
7265       || cu->language == language_java
7266       || cu->language == language_pascal)
7267     TYPE_PROTOTYPED (ftype) = 1;
7268   else if (producer_is_realview (cu->producer))
7269     /* RealView does not emit DW_AT_prototyped.  We can not
7270        distinguish prototyped and unprototyped functions; default to
7271        prototyped, since that is more common in modern code (and
7272        RealView warns about unprototyped functions).  */
7273     TYPE_PROTOTYPED (ftype) = 1;
7274
7275   /* Store the calling convention in the type if it's available in
7276      the subroutine die.  Otherwise set the calling convention to
7277      the default value DW_CC_normal.  */
7278   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7279   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7280
7281   /* We need to add the subroutine type to the die immediately so
7282      we don't infinitely recurse when dealing with parameters
7283      declared as the same subroutine type. */
7284   set_die_type (die, ftype, cu);
7285
7286   if (die->child != NULL)
7287     {
7288       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7289       struct die_info *child_die;
7290       int nparams, iparams;
7291
7292       /* Count the number of parameters.
7293          FIXME: GDB currently ignores vararg functions, but knows about
7294          vararg member functions.  */
7295       nparams = 0;
7296       child_die = die->child;
7297       while (child_die && child_die->tag)
7298         {
7299           if (child_die->tag == DW_TAG_formal_parameter)
7300             nparams++;
7301           else if (child_die->tag == DW_TAG_unspecified_parameters)
7302             TYPE_VARARGS (ftype) = 1;
7303           child_die = sibling_die (child_die);
7304         }
7305
7306       /* Allocate storage for parameters and fill them in.  */
7307       TYPE_NFIELDS (ftype) = nparams;
7308       TYPE_FIELDS (ftype) = (struct field *)
7309         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7310
7311       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7312          even if we error out during the parameters reading below.  */
7313       for (iparams = 0; iparams < nparams; iparams++)
7314         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7315
7316       iparams = 0;
7317       child_die = die->child;
7318       while (child_die && child_die->tag)
7319         {
7320           if (child_die->tag == DW_TAG_formal_parameter)
7321             {
7322               /* Dwarf2 has no clean way to discern C++ static and non-static
7323                  member functions. G++ helps GDB by marking the first
7324                  parameter for non-static member functions (which is the
7325                  this pointer) as artificial. We pass this information
7326                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
7327               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7328               if (attr)
7329                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7330               else
7331                 {
7332                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7333
7334                   /* GCC/43521: In java, the formal parameter
7335                      "this" is sometimes not marked with DW_AT_artificial.  */
7336                   if (cu->language == language_java)
7337                     {
7338                       const char *name = dwarf2_name (child_die, cu);
7339
7340                       if (name && !strcmp (name, "this"))
7341                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7342                     }
7343                 }
7344               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
7345               iparams++;
7346             }
7347           child_die = sibling_die (child_die);
7348         }
7349     }
7350
7351   return ftype;
7352 }
7353
7354 static struct type *
7355 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7356 {
7357   struct objfile *objfile = cu->objfile;
7358   const char *name = NULL;
7359   struct type *this_type;
7360
7361   name = dwarf2_full_name (NULL, die, cu);
7362   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7363                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7364   TYPE_NAME (this_type) = (char *) name;
7365   set_die_type (die, this_type, cu);
7366   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7367   return this_type;
7368 }
7369
7370 /* Find a representation of a given base type and install
7371    it in the TYPE field of the die.  */
7372
7373 static struct type *
7374 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7375 {
7376   struct objfile *objfile = cu->objfile;
7377   struct type *type;
7378   struct attribute *attr;
7379   int encoding = 0, size = 0;
7380   char *name;
7381   enum type_code code = TYPE_CODE_INT;
7382   int type_flags = 0;
7383   struct type *target_type = NULL;
7384
7385   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7386   if (attr)
7387     {
7388       encoding = DW_UNSND (attr);
7389     }
7390   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7391   if (attr)
7392     {
7393       size = DW_UNSND (attr);
7394     }
7395   name = dwarf2_name (die, cu);
7396   if (!name)
7397     {
7398       complaint (&symfile_complaints,
7399                  _("DW_AT_name missing from DW_TAG_base_type"));
7400     }
7401
7402   switch (encoding)
7403     {
7404       case DW_ATE_address:
7405         /* Turn DW_ATE_address into a void * pointer.  */
7406         code = TYPE_CODE_PTR;
7407         type_flags |= TYPE_FLAG_UNSIGNED;
7408         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7409         break;
7410       case DW_ATE_boolean:
7411         code = TYPE_CODE_BOOL;
7412         type_flags |= TYPE_FLAG_UNSIGNED;
7413         break;
7414       case DW_ATE_complex_float:
7415         code = TYPE_CODE_COMPLEX;
7416         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7417         break;
7418       case DW_ATE_decimal_float:
7419         code = TYPE_CODE_DECFLOAT;
7420         break;
7421       case DW_ATE_float:
7422         code = TYPE_CODE_FLT;
7423         break;
7424       case DW_ATE_signed:
7425         break;
7426       case DW_ATE_unsigned:
7427         type_flags |= TYPE_FLAG_UNSIGNED;
7428         break;
7429       case DW_ATE_signed_char:
7430         if (cu->language == language_ada || cu->language == language_m2
7431             || cu->language == language_pascal)
7432           code = TYPE_CODE_CHAR;
7433         break;
7434       case DW_ATE_unsigned_char:
7435         if (cu->language == language_ada || cu->language == language_m2
7436             || cu->language == language_pascal)
7437           code = TYPE_CODE_CHAR;
7438         type_flags |= TYPE_FLAG_UNSIGNED;
7439         break;
7440       case DW_ATE_UTF:
7441         /* We just treat this as an integer and then recognize the
7442            type by name elsewhere.  */
7443         break;
7444
7445       default:
7446         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7447                    dwarf_type_encoding_name (encoding));
7448         break;
7449     }
7450
7451   type = init_type (code, size, type_flags, NULL, objfile);
7452   TYPE_NAME (type) = name;
7453   TYPE_TARGET_TYPE (type) = target_type;
7454
7455   if (name && strcmp (name, "char") == 0)
7456     TYPE_NOSIGN (type) = 1;
7457
7458   return set_die_type (die, type, cu);
7459 }
7460
7461 /* Read the given DW_AT_subrange DIE.  */
7462
7463 static struct type *
7464 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7465 {
7466   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7467   struct type *base_type;
7468   struct type *range_type;
7469   struct attribute *attr;
7470   LONGEST low = 0;
7471   LONGEST high = -1;
7472   char *name;
7473   LONGEST negative_mask;
7474
7475   base_type = die_type (die, cu);
7476   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7477   check_typedef (base_type);
7478
7479   /* The die_type call above may have already set the type for this DIE.  */
7480   range_type = get_die_type (die, cu);
7481   if (range_type)
7482     return range_type;
7483
7484   if (cu->language == language_fortran)
7485     {
7486       /* FORTRAN implies a lower bound of 1, if not given.  */
7487       low = 1;
7488     }
7489
7490   /* FIXME: For variable sized arrays either of these could be
7491      a variable rather than a constant value.  We'll allow it,
7492      but we don't know how to handle it.  */
7493   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7494   if (attr)
7495     low = dwarf2_get_attr_constant_value (attr, 0);
7496
7497   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7498   if (attr)
7499     {
7500       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7501         {
7502           /* GCC encodes arrays with unspecified or dynamic length
7503              with a DW_FORM_block1 attribute or a reference attribute.
7504              FIXME: GDB does not yet know how to handle dynamic
7505              arrays properly, treat them as arrays with unspecified
7506              length for now.
7507
7508              FIXME: jimb/2003-09-22: GDB does not really know
7509              how to handle arrays of unspecified length
7510              either; we just represent them as zero-length
7511              arrays.  Choose an appropriate upper bound given
7512              the lower bound we've computed above.  */
7513           high = low - 1;
7514         }
7515       else
7516         high = dwarf2_get_attr_constant_value (attr, 1);
7517     }
7518   else
7519     {
7520       attr = dwarf2_attr (die, DW_AT_count, cu);
7521       if (attr)
7522         {
7523           int count = dwarf2_get_attr_constant_value (attr, 1);
7524           high = low + count - 1;
7525         }
7526     }
7527
7528   /* Dwarf-2 specifications explicitly allows to create subrange types
7529      without specifying a base type.
7530      In that case, the base type must be set to the type of
7531      the lower bound, upper bound or count, in that order, if any of these
7532      three attributes references an object that has a type.
7533      If no base type is found, the Dwarf-2 specifications say that
7534      a signed integer type of size equal to the size of an address should
7535      be used.
7536      For the following C code: `extern char gdb_int [];'
7537      GCC produces an empty range DIE.
7538      FIXME: muller/2010-05-28: Possible references to object for low bound,
7539      high bound or count are not yet handled by this code.
7540   */
7541   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7542     {
7543       struct objfile *objfile = cu->objfile;
7544       struct gdbarch *gdbarch = get_objfile_arch (objfile);
7545       int addr_size = gdbarch_addr_bit (gdbarch) /8;
7546       struct type *int_type = objfile_type (objfile)->builtin_int;
7547
7548       /* Test "int", "long int", and "long long int" objfile types,
7549          and select the first one having a size above or equal to the
7550          architecture address size.  */
7551       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7552         base_type = int_type;
7553       else
7554         {
7555           int_type = objfile_type (objfile)->builtin_long;
7556           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7557             base_type = int_type;
7558           else
7559             {
7560               int_type = objfile_type (objfile)->builtin_long_long;
7561               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7562                 base_type = int_type;
7563             }
7564         }
7565     }
7566
7567   negative_mask =
7568     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7569   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7570     low |= negative_mask;
7571   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7572     high |= negative_mask;
7573
7574   range_type = create_range_type (NULL, base_type, low, high);
7575
7576   /* Mark arrays with dynamic length at least as an array of unspecified
7577      length.  GDB could check the boundary but before it gets implemented at
7578      least allow accessing the array elements.  */
7579   if (attr && attr->form == DW_FORM_block1)
7580     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7581
7582   name = dwarf2_name (die, cu);
7583   if (name)
7584     TYPE_NAME (range_type) = name;
7585
7586   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7587   if (attr)
7588     TYPE_LENGTH (range_type) = DW_UNSND (attr);
7589
7590   set_die_type (die, range_type, cu);
7591
7592   /* set_die_type should be already done.  */
7593   set_descriptive_type (range_type, die, cu);
7594
7595   return range_type;
7596 }
7597
7598 static struct type *
7599 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7600 {
7601   struct type *type;
7602
7603   /* For now, we only support the C meaning of an unspecified type: void.  */
7604
7605   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7606   TYPE_NAME (type) = dwarf2_name (die, cu);
7607
7608   return set_die_type (die, type, cu);
7609 }
7610
7611 /* Trivial hash function for die_info: the hash value of a DIE
7612    is its offset in .debug_info for this objfile.  */
7613
7614 static hashval_t
7615 die_hash (const void *item)
7616 {
7617   const struct die_info *die = item;
7618
7619   return die->offset;
7620 }
7621
7622 /* Trivial comparison function for die_info structures: two DIEs
7623    are equal if they have the same offset.  */
7624
7625 static int
7626 die_eq (const void *item_lhs, const void *item_rhs)
7627 {
7628   const struct die_info *die_lhs = item_lhs;
7629   const struct die_info *die_rhs = item_rhs;
7630
7631   return die_lhs->offset == die_rhs->offset;
7632 }
7633
7634 /* Read a whole compilation unit into a linked list of dies.  */
7635
7636 static struct die_info *
7637 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
7638 {
7639   struct die_reader_specs reader_specs;
7640
7641   gdb_assert (cu->die_hash == NULL);
7642   cu->die_hash
7643     = htab_create_alloc_ex (cu->header.length / 12,
7644                             die_hash,
7645                             die_eq,
7646                             NULL,
7647                             &cu->comp_unit_obstack,
7648                             hashtab_obstack_allocate,
7649                             dummy_obstack_deallocate);
7650
7651   init_cu_die_reader (&reader_specs, cu);
7652
7653   return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
7654 }
7655
7656 /* Main entry point for reading a DIE and all children.
7657    Read the DIE and dump it if requested.  */
7658
7659 static struct die_info *
7660 read_die_and_children (const struct die_reader_specs *reader,
7661                        gdb_byte *info_ptr,
7662                        gdb_byte **new_info_ptr,
7663                        struct die_info *parent)
7664 {
7665   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
7666                                                      new_info_ptr, parent);
7667
7668   if (dwarf2_die_debug)
7669     {
7670       fprintf_unfiltered (gdb_stdlog,
7671                           "\nRead die from %s of %s:\n",
7672                           reader->buffer == dwarf2_per_objfile->info.buffer
7673                           ? ".debug_info"
7674                           : reader->buffer == dwarf2_per_objfile->types.buffer
7675                           ? ".debug_types"
7676                           : "unknown section",
7677                           reader->abfd->filename);
7678       dump_die (result, dwarf2_die_debug);
7679     }
7680
7681   return result;
7682 }
7683
7684 /* Read a single die and all its descendents.  Set the die's sibling
7685    field to NULL; set other fields in the die correctly, and set all
7686    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
7687    location of the info_ptr after reading all of those dies.  PARENT
7688    is the parent of the die in question.  */
7689
7690 static struct die_info *
7691 read_die_and_children_1 (const struct die_reader_specs *reader,
7692                          gdb_byte *info_ptr,
7693                          gdb_byte **new_info_ptr,
7694                          struct die_info *parent)
7695 {
7696   struct die_info *die;
7697   gdb_byte *cur_ptr;
7698   int has_children;
7699
7700   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
7701   if (die == NULL)
7702     {
7703       *new_info_ptr = cur_ptr;
7704       return NULL;
7705     }
7706   store_in_ref_table (die, reader->cu);
7707
7708   if (has_children)
7709     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
7710   else
7711     {
7712       die->child = NULL;
7713       *new_info_ptr = cur_ptr;
7714     }
7715
7716   die->sibling = NULL;
7717   die->parent = parent;
7718   return die;
7719 }
7720
7721 /* Read a die, all of its descendents, and all of its siblings; set
7722    all of the fields of all of the dies correctly.  Arguments are as
7723    in read_die_and_children.  */
7724
7725 static struct die_info *
7726 read_die_and_siblings (const struct die_reader_specs *reader,
7727                        gdb_byte *info_ptr,
7728                        gdb_byte **new_info_ptr,
7729                        struct die_info *parent)
7730 {
7731   struct die_info *first_die, *last_sibling;
7732   gdb_byte *cur_ptr;
7733
7734   cur_ptr = info_ptr;
7735   first_die = last_sibling = NULL;
7736
7737   while (1)
7738     {
7739       struct die_info *die
7740         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
7741
7742       if (die == NULL)
7743         {
7744           *new_info_ptr = cur_ptr;
7745           return first_die;
7746         }
7747
7748       if (!first_die)
7749         first_die = die;
7750       else
7751         last_sibling->sibling = die;
7752
7753       last_sibling = die;
7754     }
7755 }
7756
7757 /* Read the die from the .debug_info section buffer.  Set DIEP to
7758    point to a newly allocated die with its information, except for its
7759    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
7760    whether the die has children or not.  */
7761
7762 static gdb_byte *
7763 read_full_die (const struct die_reader_specs *reader,
7764                struct die_info **diep, gdb_byte *info_ptr,
7765                int *has_children)
7766 {
7767   unsigned int abbrev_number, bytes_read, i, offset;
7768   struct abbrev_info *abbrev;
7769   struct die_info *die;
7770   struct dwarf2_cu *cu = reader->cu;
7771   bfd *abfd = reader->abfd;
7772
7773   offset = info_ptr - reader->buffer;
7774   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7775   info_ptr += bytes_read;
7776   if (!abbrev_number)
7777     {
7778       *diep = NULL;
7779       *has_children = 0;
7780       return info_ptr;
7781     }
7782
7783   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7784   if (!abbrev)
7785     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7786            abbrev_number,
7787            bfd_get_filename (abfd));
7788
7789   die = dwarf_alloc_die (cu, abbrev->num_attrs);
7790   die->offset = offset;
7791   die->tag = abbrev->tag;
7792   die->abbrev = abbrev_number;
7793
7794   die->num_attrs = abbrev->num_attrs;
7795
7796   for (i = 0; i < abbrev->num_attrs; ++i)
7797     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7798                                abfd, info_ptr, cu);
7799
7800   *diep = die;
7801   *has_children = abbrev->has_children;
7802   return info_ptr;
7803 }
7804
7805 /* In DWARF version 2, the description of the debugging information is
7806    stored in a separate .debug_abbrev section.  Before we read any
7807    dies from a section we read in all abbreviations and install them
7808    in a hash table.  This function also sets flags in CU describing
7809    the data found in the abbrev table.  */
7810
7811 static void
7812 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
7813 {
7814   struct comp_unit_head *cu_header = &cu->header;
7815   gdb_byte *abbrev_ptr;
7816   struct abbrev_info *cur_abbrev;
7817   unsigned int abbrev_number, bytes_read, abbrev_name;
7818   unsigned int abbrev_form, hash_number;
7819   struct attr_abbrev *cur_attrs;
7820   unsigned int allocated_attrs;
7821
7822   /* Initialize dwarf2 abbrevs */
7823   obstack_init (&cu->abbrev_obstack);
7824   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7825                                       (ABBREV_HASH_SIZE
7826                                        * sizeof (struct abbrev_info *)));
7827   memset (cu->dwarf2_abbrevs, 0,
7828           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
7829
7830   dwarf2_read_section (dwarf2_per_objfile->objfile,
7831                        &dwarf2_per_objfile->abbrev);
7832   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
7833   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7834   abbrev_ptr += bytes_read;
7835
7836   allocated_attrs = ATTR_ALLOC_CHUNK;
7837   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
7838
7839   /* loop until we reach an abbrev number of 0 */
7840   while (abbrev_number)
7841     {
7842       cur_abbrev = dwarf_alloc_abbrev (cu);
7843
7844       /* read in abbrev header */
7845       cur_abbrev->number = abbrev_number;
7846       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7847       abbrev_ptr += bytes_read;
7848       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7849       abbrev_ptr += 1;
7850
7851       if (cur_abbrev->tag == DW_TAG_namespace)
7852         cu->has_namespace_info = 1;
7853
7854       /* now read in declarations */
7855       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7856       abbrev_ptr += bytes_read;
7857       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7858       abbrev_ptr += bytes_read;
7859       while (abbrev_name)
7860         {
7861           if (cur_abbrev->num_attrs == allocated_attrs)
7862             {
7863               allocated_attrs += ATTR_ALLOC_CHUNK;
7864               cur_attrs
7865                 = xrealloc (cur_attrs, (allocated_attrs
7866                                         * sizeof (struct attr_abbrev)));
7867             }
7868
7869           /* Record whether this compilation unit might have
7870              inter-compilation-unit references.  If we don't know what form
7871              this attribute will have, then it might potentially be a
7872              DW_FORM_ref_addr, so we conservatively expect inter-CU
7873              references.  */
7874
7875           if (abbrev_form == DW_FORM_ref_addr
7876               || abbrev_form == DW_FORM_indirect)
7877             cu->has_form_ref_addr = 1;
7878
7879           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7880           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
7881           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7882           abbrev_ptr += bytes_read;
7883           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7884           abbrev_ptr += bytes_read;
7885         }
7886
7887       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
7888                                          (cur_abbrev->num_attrs
7889                                           * sizeof (struct attr_abbrev)));
7890       memcpy (cur_abbrev->attrs, cur_attrs,
7891               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
7892
7893       hash_number = abbrev_number % ABBREV_HASH_SIZE;
7894       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
7895       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
7896
7897       /* Get next abbreviation.
7898          Under Irix6 the abbreviations for a compilation unit are not
7899          always properly terminated with an abbrev number of 0.
7900          Exit loop if we encounter an abbreviation which we have
7901          already read (which means we are about to read the abbreviations
7902          for the next compile unit) or if the end of the abbreviation
7903          table is reached.  */
7904       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
7905           >= dwarf2_per_objfile->abbrev.size)
7906         break;
7907       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7908       abbrev_ptr += bytes_read;
7909       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
7910         break;
7911     }
7912
7913   xfree (cur_attrs);
7914 }
7915
7916 /* Release the memory used by the abbrev table for a compilation unit.  */
7917
7918 static void
7919 dwarf2_free_abbrev_table (void *ptr_to_cu)
7920 {
7921   struct dwarf2_cu *cu = ptr_to_cu;
7922
7923   obstack_free (&cu->abbrev_obstack, NULL);
7924   cu->dwarf2_abbrevs = NULL;
7925 }
7926
7927 /* Lookup an abbrev_info structure in the abbrev hash table.  */
7928
7929 static struct abbrev_info *
7930 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
7931 {
7932   unsigned int hash_number;
7933   struct abbrev_info *abbrev;
7934
7935   hash_number = number % ABBREV_HASH_SIZE;
7936   abbrev = cu->dwarf2_abbrevs[hash_number];
7937
7938   while (abbrev)
7939     {
7940       if (abbrev->number == number)
7941         return abbrev;
7942       else
7943         abbrev = abbrev->next;
7944     }
7945   return NULL;
7946 }
7947
7948 /* Returns nonzero if TAG represents a type that we might generate a partial
7949    symbol for.  */
7950
7951 static int
7952 is_type_tag_for_partial (int tag)
7953 {
7954   switch (tag)
7955     {
7956 #if 0
7957     /* Some types that would be reasonable to generate partial symbols for,
7958        that we don't at present.  */
7959     case DW_TAG_array_type:
7960     case DW_TAG_file_type:
7961     case DW_TAG_ptr_to_member_type:
7962     case DW_TAG_set_type:
7963     case DW_TAG_string_type:
7964     case DW_TAG_subroutine_type:
7965 #endif
7966     case DW_TAG_base_type:
7967     case DW_TAG_class_type:
7968     case DW_TAG_interface_type:
7969     case DW_TAG_enumeration_type:
7970     case DW_TAG_structure_type:
7971     case DW_TAG_subrange_type:
7972     case DW_TAG_typedef:
7973     case DW_TAG_union_type:
7974       return 1;
7975     default:
7976       return 0;
7977     }
7978 }
7979
7980 /* Load all DIEs that are interesting for partial symbols into memory.  */
7981
7982 static struct partial_die_info *
7983 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
7984                    int building_psymtab, struct dwarf2_cu *cu)
7985 {
7986   struct partial_die_info *part_die;
7987   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
7988   struct abbrev_info *abbrev;
7989   unsigned int bytes_read;
7990   unsigned int load_all = 0;
7991
7992   int nesting_level = 1;
7993
7994   parent_die = NULL;
7995   last_die = NULL;
7996
7997   if (cu->per_cu && cu->per_cu->load_all_dies)
7998     load_all = 1;
7999
8000   cu->partial_dies
8001     = htab_create_alloc_ex (cu->header.length / 12,
8002                             partial_die_hash,
8003                             partial_die_eq,
8004                             NULL,
8005                             &cu->comp_unit_obstack,
8006                             hashtab_obstack_allocate,
8007                             dummy_obstack_deallocate);
8008
8009   part_die = obstack_alloc (&cu->comp_unit_obstack,
8010                             sizeof (struct partial_die_info));
8011
8012   while (1)
8013     {
8014       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8015
8016       /* A NULL abbrev means the end of a series of children.  */
8017       if (abbrev == NULL)
8018         {
8019           if (--nesting_level == 0)
8020             {
8021               /* PART_DIE was probably the last thing allocated on the
8022                  comp_unit_obstack, so we could call obstack_free
8023                  here.  We don't do that because the waste is small,
8024                  and will be cleaned up when we're done with this
8025                  compilation unit.  This way, we're also more robust
8026                  against other users of the comp_unit_obstack.  */
8027               return first_die;
8028             }
8029           info_ptr += bytes_read;
8030           last_die = parent_die;
8031           parent_die = parent_die->die_parent;
8032           continue;
8033         }
8034
8035       /* Check whether this DIE is interesting enough to save.  Normally
8036          we would not be interested in members here, but there may be
8037          later variables referencing them via DW_AT_specification (for
8038          static members).  */
8039       if (!load_all
8040           && !is_type_tag_for_partial (abbrev->tag)
8041           && abbrev->tag != DW_TAG_enumerator
8042           && abbrev->tag != DW_TAG_subprogram
8043           && abbrev->tag != DW_TAG_lexical_block
8044           && abbrev->tag != DW_TAG_variable
8045           && abbrev->tag != DW_TAG_namespace
8046           && abbrev->tag != DW_TAG_module
8047           && abbrev->tag != DW_TAG_member)
8048         {
8049           /* Otherwise we skip to the next sibling, if any.  */
8050           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8051           continue;
8052         }
8053
8054       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8055                                    buffer, info_ptr, cu);
8056
8057       /* This two-pass algorithm for processing partial symbols has a
8058          high cost in cache pressure.  Thus, handle some simple cases
8059          here which cover the majority of C partial symbols.  DIEs
8060          which neither have specification tags in them, nor could have
8061          specification tags elsewhere pointing at them, can simply be
8062          processed and discarded.
8063
8064          This segment is also optional; scan_partial_symbols and
8065          add_partial_symbol will handle these DIEs if we chain
8066          them in normally.  When compilers which do not emit large
8067          quantities of duplicate debug information are more common,
8068          this code can probably be removed.  */
8069
8070       /* Any complete simple types at the top level (pretty much all
8071          of them, for a language without namespaces), can be processed
8072          directly.  */
8073       if (parent_die == NULL
8074           && part_die->has_specification == 0
8075           && part_die->is_declaration == 0
8076           && (part_die->tag == DW_TAG_typedef
8077               || part_die->tag == DW_TAG_base_type
8078               || part_die->tag == DW_TAG_subrange_type))
8079         {
8080           if (building_psymtab && part_die->name != NULL)
8081             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8082                                  VAR_DOMAIN, LOC_TYPEDEF,
8083                                  &cu->objfile->static_psymbols,
8084                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8085           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8086           continue;
8087         }
8088
8089       /* If we're at the second level, and we're an enumerator, and
8090          our parent has no specification (meaning possibly lives in a
8091          namespace elsewhere), then we can add the partial symbol now
8092          instead of queueing it.  */
8093       if (part_die->tag == DW_TAG_enumerator
8094           && parent_die != NULL
8095           && parent_die->die_parent == NULL
8096           && parent_die->tag == DW_TAG_enumeration_type
8097           && parent_die->has_specification == 0)
8098         {
8099           if (part_die->name == NULL)
8100             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8101           else if (building_psymtab)
8102             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8103                                  VAR_DOMAIN, LOC_CONST,
8104                                  (cu->language == language_cplus
8105                                   || cu->language == language_java)
8106                                  ? &cu->objfile->global_psymbols
8107                                  : &cu->objfile->static_psymbols,
8108                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8109
8110           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8111           continue;
8112         }
8113
8114       /* We'll save this DIE so link it in.  */
8115       part_die->die_parent = parent_die;
8116       part_die->die_sibling = NULL;
8117       part_die->die_child = NULL;
8118
8119       if (last_die && last_die == parent_die)
8120         last_die->die_child = part_die;
8121       else if (last_die)
8122         last_die->die_sibling = part_die;
8123
8124       last_die = part_die;
8125
8126       if (first_die == NULL)
8127         first_die = part_die;
8128
8129       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8130          find interesting need to be in the hash table, because we
8131          also have the parent/sibling/child chains; only those that we
8132          might refer to by offset later during partial symbol reading.
8133
8134          For now this means things that might have be the target of a
8135          DW_AT_specification, DW_AT_abstract_origin, or
8136          DW_AT_extension.  DW_AT_extension will refer only to
8137          namespaces; DW_AT_abstract_origin refers to functions (and
8138          many things under the function DIE, but we do not recurse
8139          into function DIEs during partial symbol reading) and
8140          possibly variables as well; DW_AT_specification refers to
8141          declarations.  Declarations ought to have the DW_AT_declaration
8142          flag.  It happens that GCC forgets to put it in sometimes, but
8143          only for functions, not for types.
8144
8145          Adding more things than necessary to the hash table is harmless
8146          except for the performance cost.  Adding too few will result in
8147          wasted time in find_partial_die, when we reread the compilation
8148          unit with load_all_dies set.  */
8149
8150       if (load_all
8151           || abbrev->tag == DW_TAG_subprogram
8152           || abbrev->tag == DW_TAG_variable
8153           || abbrev->tag == DW_TAG_namespace
8154           || part_die->is_declaration)
8155         {
8156           void **slot;
8157
8158           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8159                                            part_die->offset, INSERT);
8160           *slot = part_die;
8161         }
8162
8163       part_die = obstack_alloc (&cu->comp_unit_obstack,
8164                                 sizeof (struct partial_die_info));
8165
8166       /* For some DIEs we want to follow their children (if any).  For C
8167          we have no reason to follow the children of structures; for other
8168          languages we have to, both so that we can get at method physnames
8169          to infer fully qualified class names, and for DW_AT_specification.
8170
8171          For Ada, we need to scan the children of subprograms and lexical
8172          blocks as well because Ada allows the definition of nested
8173          entities that could be interesting for the debugger, such as
8174          nested subprograms for instance.  */
8175       if (last_die->has_children
8176           && (load_all
8177               || last_die->tag == DW_TAG_namespace
8178               || last_die->tag == DW_TAG_module
8179               || last_die->tag == DW_TAG_enumeration_type
8180               || (cu->language != language_c
8181                   && (last_die->tag == DW_TAG_class_type
8182                       || last_die->tag == DW_TAG_interface_type
8183                       || last_die->tag == DW_TAG_structure_type
8184                       || last_die->tag == DW_TAG_union_type))
8185               || (cu->language == language_ada
8186                   && (last_die->tag == DW_TAG_subprogram
8187                       || last_die->tag == DW_TAG_lexical_block))))
8188         {
8189           nesting_level++;
8190           parent_die = last_die;
8191           continue;
8192         }
8193
8194       /* Otherwise we skip to the next sibling, if any.  */
8195       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8196
8197       /* Back to the top, do it again.  */
8198     }
8199 }
8200
8201 /* Read a minimal amount of information into the minimal die structure.  */
8202
8203 static gdb_byte *
8204 read_partial_die (struct partial_die_info *part_die,
8205                   struct abbrev_info *abbrev,
8206                   unsigned int abbrev_len, bfd *abfd,
8207                   gdb_byte *buffer, gdb_byte *info_ptr,
8208                   struct dwarf2_cu *cu)
8209 {
8210   unsigned int i;
8211   struct attribute attr;
8212   int has_low_pc_attr = 0;
8213   int has_high_pc_attr = 0;
8214
8215   memset (part_die, 0, sizeof (struct partial_die_info));
8216
8217   part_die->offset = info_ptr - buffer;
8218
8219   info_ptr += abbrev_len;
8220
8221   if (abbrev == NULL)
8222     return info_ptr;
8223
8224   part_die->tag = abbrev->tag;
8225   part_die->has_children = abbrev->has_children;
8226
8227   for (i = 0; i < abbrev->num_attrs; ++i)
8228     {
8229       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8230
8231       /* Store the data if it is of an attribute we want to keep in a
8232          partial symbol table.  */
8233       switch (attr.name)
8234         {
8235         case DW_AT_name:
8236           switch (part_die->tag)
8237             {
8238             case DW_TAG_compile_unit:
8239             case DW_TAG_type_unit:
8240               /* Compilation units have a DW_AT_name that is a filename, not
8241                  a source language identifier.  */
8242             case DW_TAG_enumeration_type:
8243             case DW_TAG_enumerator:
8244               /* These tags always have simple identifiers already; no need
8245                  to canonicalize them.  */
8246               part_die->name = DW_STRING (&attr);
8247               break;
8248             default:
8249               part_die->name
8250                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8251                                             &cu->objfile->objfile_obstack);
8252               break;
8253             }
8254           break;
8255         case DW_AT_linkage_name:
8256         case DW_AT_MIPS_linkage_name:
8257           /* Note that both forms of linkage name might appear.  We
8258              assume they will be the same, and we only store the last
8259              one we see.  */
8260           if (cu->language == language_ada)
8261             part_die->name = DW_STRING (&attr);
8262           break;
8263         case DW_AT_low_pc:
8264           has_low_pc_attr = 1;
8265           part_die->lowpc = DW_ADDR (&attr);
8266           break;
8267         case DW_AT_high_pc:
8268           has_high_pc_attr = 1;
8269           part_die->highpc = DW_ADDR (&attr);
8270           break;
8271         case DW_AT_location:
8272           /* Support the .debug_loc offsets */
8273           if (attr_form_is_block (&attr))
8274             {
8275                part_die->locdesc = DW_BLOCK (&attr);
8276             }
8277           else if (attr_form_is_section_offset (&attr))
8278             {
8279               dwarf2_complex_location_expr_complaint ();
8280             }
8281           else
8282             {
8283               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8284                                                      "partial symbol information");
8285             }
8286           break;
8287         case DW_AT_external:
8288           part_die->is_external = DW_UNSND (&attr);
8289           break;
8290         case DW_AT_declaration:
8291           part_die->is_declaration = DW_UNSND (&attr);
8292           break;
8293         case DW_AT_type:
8294           part_die->has_type = 1;
8295           break;
8296         case DW_AT_abstract_origin:
8297         case DW_AT_specification:
8298         case DW_AT_extension:
8299           part_die->has_specification = 1;
8300           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8301           break;
8302         case DW_AT_sibling:
8303           /* Ignore absolute siblings, they might point outside of
8304              the current compile unit.  */
8305           if (attr.form == DW_FORM_ref_addr)
8306             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8307           else
8308             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8309           break;
8310         case DW_AT_byte_size:
8311           part_die->has_byte_size = 1;
8312           break;
8313         case DW_AT_calling_convention:
8314           /* DWARF doesn't provide a way to identify a program's source-level
8315              entry point.  DW_AT_calling_convention attributes are only meant
8316              to describe functions' calling conventions.
8317
8318              However, because it's a necessary piece of information in
8319              Fortran, and because DW_CC_program is the only piece of debugging
8320              information whose definition refers to a 'main program' at all,
8321              several compilers have begun marking Fortran main programs with
8322              DW_CC_program --- even when those functions use the standard
8323              calling conventions.
8324
8325              So until DWARF specifies a way to provide this information and
8326              compilers pick up the new representation, we'll support this
8327              practice.  */
8328           if (DW_UNSND (&attr) == DW_CC_program
8329               && cu->language == language_fortran)
8330             set_main_name (part_die->name);
8331           break;
8332         default:
8333           break;
8334         }
8335     }
8336
8337   /* When using the GNU linker, .gnu.linkonce. sections are used to
8338      eliminate duplicate copies of functions and vtables and such.
8339      The linker will arbitrarily choose one and discard the others.
8340      The AT_*_pc values for such functions refer to local labels in
8341      these sections.  If the section from that file was discarded, the
8342      labels are not in the output, so the relocs get a value of 0.
8343      If this is a discarded function, mark the pc bounds as invalid,
8344      so that GDB will ignore it.  */
8345   if (has_low_pc_attr && has_high_pc_attr
8346       && part_die->lowpc < part_die->highpc
8347       && (part_die->lowpc != 0
8348           || dwarf2_per_objfile->has_section_at_zero))
8349     part_die->has_pc_info = 1;
8350
8351   return info_ptr;
8352 }
8353
8354 /* Find a cached partial DIE at OFFSET in CU.  */
8355
8356 static struct partial_die_info *
8357 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8358 {
8359   struct partial_die_info *lookup_die = NULL;
8360   struct partial_die_info part_die;
8361
8362   part_die.offset = offset;
8363   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8364
8365   return lookup_die;
8366 }
8367
8368 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8369    except in the case of .debug_types DIEs which do not reference
8370    outside their CU (they do however referencing other types via
8371    DW_FORM_sig8).  */
8372
8373 static struct partial_die_info *
8374 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8375 {
8376   struct dwarf2_per_cu_data *per_cu = NULL;
8377   struct partial_die_info *pd = NULL;
8378
8379   if (cu->per_cu->from_debug_types)
8380     {
8381       pd = find_partial_die_in_comp_unit (offset, cu);
8382       if (pd != NULL)
8383         return pd;
8384       goto not_found;
8385     }
8386
8387   if (offset_in_cu_p (&cu->header, offset))
8388     {
8389       pd = find_partial_die_in_comp_unit (offset, cu);
8390       if (pd != NULL)
8391         return pd;
8392     }
8393
8394   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8395
8396   if (per_cu->cu == NULL)
8397     {
8398       load_partial_comp_unit (per_cu, cu->objfile);
8399       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8400       dwarf2_per_objfile->read_in_chain = per_cu;
8401     }
8402
8403   per_cu->cu->last_used = 0;
8404   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8405
8406   if (pd == NULL && per_cu->load_all_dies == 0)
8407     {
8408       struct cleanup *back_to;
8409       struct partial_die_info comp_unit_die;
8410       struct abbrev_info *abbrev;
8411       unsigned int bytes_read;
8412       char *info_ptr;
8413
8414       per_cu->load_all_dies = 1;
8415
8416       /* Re-read the DIEs.  */
8417       back_to = make_cleanup (null_cleanup, 0);
8418       if (per_cu->cu->dwarf2_abbrevs == NULL)
8419         {
8420           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8421           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8422         }
8423       info_ptr = (dwarf2_per_objfile->info.buffer
8424                   + per_cu->cu->header.offset
8425                   + per_cu->cu->header.first_die_offset);
8426       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8427       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8428                                    per_cu->cu->objfile->obfd,
8429                                    dwarf2_per_objfile->info.buffer, info_ptr,
8430                                    per_cu->cu);
8431       if (comp_unit_die.has_children)
8432         load_partial_dies (per_cu->cu->objfile->obfd,
8433                            dwarf2_per_objfile->info.buffer, info_ptr,
8434                            0, per_cu->cu);
8435       do_cleanups (back_to);
8436
8437       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8438     }
8439
8440  not_found:
8441
8442   if (pd == NULL)
8443     internal_error (__FILE__, __LINE__,
8444                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8445                     offset, bfd_get_filename (cu->objfile->obfd));
8446   return pd;
8447 }
8448
8449 /* Adjust PART_DIE before generating a symbol for it.  This function
8450    may set the is_external flag or change the DIE's name.  */
8451
8452 static void
8453 fixup_partial_die (struct partial_die_info *part_die,
8454                    struct dwarf2_cu *cu)
8455 {
8456   /* If we found a reference attribute and the DIE has no name, try
8457      to find a name in the referred to DIE.  */
8458
8459   if (part_die->name == NULL && part_die->has_specification)
8460     {
8461       struct partial_die_info *spec_die;
8462
8463       spec_die = find_partial_die (part_die->spec_offset, cu);
8464
8465       fixup_partial_die (spec_die, cu);
8466
8467       if (spec_die->name)
8468         {
8469           part_die->name = spec_die->name;
8470
8471           /* Copy DW_AT_external attribute if it is set.  */
8472           if (spec_die->is_external)
8473             part_die->is_external = spec_die->is_external;
8474         }
8475     }
8476
8477   /* Set default names for some unnamed DIEs.  */
8478   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8479                                  || part_die->tag == DW_TAG_class_type))
8480     part_die->name = "(anonymous class)";
8481
8482   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8483     part_die->name = "(anonymous namespace)";
8484
8485   if (part_die->tag == DW_TAG_structure_type
8486       || part_die->tag == DW_TAG_class_type
8487       || part_die->tag == DW_TAG_union_type)
8488     guess_structure_name (part_die, cu);
8489 }
8490
8491 /* Read an attribute value described by an attribute form.  */
8492
8493 static gdb_byte *
8494 read_attribute_value (struct attribute *attr, unsigned form,
8495                       bfd *abfd, gdb_byte *info_ptr,
8496                       struct dwarf2_cu *cu)
8497 {
8498   struct comp_unit_head *cu_header = &cu->header;
8499   unsigned int bytes_read;
8500   struct dwarf_block *blk;
8501
8502   attr->form = form;
8503   switch (form)
8504     {
8505     case DW_FORM_ref_addr:
8506       if (cu->header.version == 2)
8507         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8508       else
8509         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8510       info_ptr += bytes_read;
8511       break;
8512     case DW_FORM_addr:
8513       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8514       info_ptr += bytes_read;
8515       break;
8516     case DW_FORM_block2:
8517       blk = dwarf_alloc_block (cu);
8518       blk->size = read_2_bytes (abfd, info_ptr);
8519       info_ptr += 2;
8520       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8521       info_ptr += blk->size;
8522       DW_BLOCK (attr) = blk;
8523       break;
8524     case DW_FORM_block4:
8525       blk = dwarf_alloc_block (cu);
8526       blk->size = read_4_bytes (abfd, info_ptr);
8527       info_ptr += 4;
8528       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8529       info_ptr += blk->size;
8530       DW_BLOCK (attr) = blk;
8531       break;
8532     case DW_FORM_data2:
8533       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8534       info_ptr += 2;
8535       break;
8536     case DW_FORM_data4:
8537       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8538       info_ptr += 4;
8539       break;
8540     case DW_FORM_data8:
8541       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8542       info_ptr += 8;
8543       break;
8544     case DW_FORM_sec_offset:
8545       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8546       info_ptr += bytes_read;
8547       break;
8548     case DW_FORM_string:
8549       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8550       DW_STRING_IS_CANONICAL (attr) = 0;
8551       info_ptr += bytes_read;
8552       break;
8553     case DW_FORM_strp:
8554       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8555                                                &bytes_read);
8556       DW_STRING_IS_CANONICAL (attr) = 0;
8557       info_ptr += bytes_read;
8558       break;
8559     case DW_FORM_exprloc:
8560     case DW_FORM_block:
8561       blk = dwarf_alloc_block (cu);
8562       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8563       info_ptr += bytes_read;
8564       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8565       info_ptr += blk->size;
8566       DW_BLOCK (attr) = blk;
8567       break;
8568     case DW_FORM_block1:
8569       blk = dwarf_alloc_block (cu);
8570       blk->size = read_1_byte (abfd, info_ptr);
8571       info_ptr += 1;
8572       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8573       info_ptr += blk->size;
8574       DW_BLOCK (attr) = blk;
8575       break;
8576     case DW_FORM_data1:
8577       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8578       info_ptr += 1;
8579       break;
8580     case DW_FORM_flag:
8581       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8582       info_ptr += 1;
8583       break;
8584     case DW_FORM_flag_present:
8585       DW_UNSND (attr) = 1;
8586       break;
8587     case DW_FORM_sdata:
8588       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8589       info_ptr += bytes_read;
8590       break;
8591     case DW_FORM_udata:
8592       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8593       info_ptr += bytes_read;
8594       break;
8595     case DW_FORM_ref1:
8596       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
8597       info_ptr += 1;
8598       break;
8599     case DW_FORM_ref2:
8600       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
8601       info_ptr += 2;
8602       break;
8603     case DW_FORM_ref4:
8604       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
8605       info_ptr += 4;
8606       break;
8607     case DW_FORM_ref8:
8608       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
8609       info_ptr += 8;
8610       break;
8611     case DW_FORM_sig8:
8612       /* Convert the signature to something we can record in DW_UNSND
8613          for later lookup.
8614          NOTE: This is NULL if the type wasn't found.  */
8615       DW_SIGNATURED_TYPE (attr) =
8616         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8617       info_ptr += 8;
8618       break;
8619     case DW_FORM_ref_udata:
8620       DW_ADDR (attr) = (cu->header.offset
8621                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
8622       info_ptr += bytes_read;
8623       break;
8624     case DW_FORM_indirect:
8625       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8626       info_ptr += bytes_read;
8627       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
8628       break;
8629     default:
8630       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
8631              dwarf_form_name (form),
8632              bfd_get_filename (abfd));
8633     }
8634
8635   /* We have seen instances where the compiler tried to emit a byte
8636      size attribute of -1 which ended up being encoded as an unsigned
8637      0xffffffff.  Although 0xffffffff is technically a valid size value,
8638      an object of this size seems pretty unlikely so we can relatively
8639      safely treat these cases as if the size attribute was invalid and
8640      treat them as zero by default.  */
8641   if (attr->name == DW_AT_byte_size
8642       && form == DW_FORM_data4
8643       && DW_UNSND (attr) >= 0xffffffff)
8644     {
8645       complaint
8646         (&symfile_complaints,
8647          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8648          hex_string (DW_UNSND (attr)));
8649       DW_UNSND (attr) = 0;
8650     }
8651
8652   return info_ptr;
8653 }
8654
8655 /* Read an attribute described by an abbreviated attribute.  */
8656
8657 static gdb_byte *
8658 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
8659                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
8660 {
8661   attr->name = abbrev->name;
8662   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
8663 }
8664
8665 /* read dwarf information from a buffer */
8666
8667 static unsigned int
8668 read_1_byte (bfd *abfd, gdb_byte *buf)
8669 {
8670   return bfd_get_8 (abfd, buf);
8671 }
8672
8673 static int
8674 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
8675 {
8676   return bfd_get_signed_8 (abfd, buf);
8677 }
8678
8679 static unsigned int
8680 read_2_bytes (bfd *abfd, gdb_byte *buf)
8681 {
8682   return bfd_get_16 (abfd, buf);
8683 }
8684
8685 static int
8686 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
8687 {
8688   return bfd_get_signed_16 (abfd, buf);
8689 }
8690
8691 static unsigned int
8692 read_4_bytes (bfd *abfd, gdb_byte *buf)
8693 {
8694   return bfd_get_32 (abfd, buf);
8695 }
8696
8697 static int
8698 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
8699 {
8700   return bfd_get_signed_32 (abfd, buf);
8701 }
8702
8703 static ULONGEST
8704 read_8_bytes (bfd *abfd, gdb_byte *buf)
8705 {
8706   return bfd_get_64 (abfd, buf);
8707 }
8708
8709 static CORE_ADDR
8710 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
8711               unsigned int *bytes_read)
8712 {
8713   struct comp_unit_head *cu_header = &cu->header;
8714   CORE_ADDR retval = 0;
8715
8716   if (cu_header->signed_addr_p)
8717     {
8718       switch (cu_header->addr_size)
8719         {
8720         case 2:
8721           retval = bfd_get_signed_16 (abfd, buf);
8722           break;
8723         case 4:
8724           retval = bfd_get_signed_32 (abfd, buf);
8725           break;
8726         case 8:
8727           retval = bfd_get_signed_64 (abfd, buf);
8728           break;
8729         default:
8730           internal_error (__FILE__, __LINE__,
8731                           _("read_address: bad switch, signed [in module %s]"),
8732                           bfd_get_filename (abfd));
8733         }
8734     }
8735   else
8736     {
8737       switch (cu_header->addr_size)
8738         {
8739         case 2:
8740           retval = bfd_get_16 (abfd, buf);
8741           break;
8742         case 4:
8743           retval = bfd_get_32 (abfd, buf);
8744           break;
8745         case 8:
8746           retval = bfd_get_64 (abfd, buf);
8747           break;
8748         default:
8749           internal_error (__FILE__, __LINE__,
8750                           _("read_address: bad switch, unsigned [in module %s]"),
8751                           bfd_get_filename (abfd));
8752         }
8753     }
8754
8755   *bytes_read = cu_header->addr_size;
8756   return retval;
8757 }
8758
8759 /* Read the initial length from a section.  The (draft) DWARF 3
8760    specification allows the initial length to take up either 4 bytes
8761    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
8762    bytes describe the length and all offsets will be 8 bytes in length
8763    instead of 4.
8764
8765    An older, non-standard 64-bit format is also handled by this
8766    function.  The older format in question stores the initial length
8767    as an 8-byte quantity without an escape value.  Lengths greater
8768    than 2^32 aren't very common which means that the initial 4 bytes
8769    is almost always zero.  Since a length value of zero doesn't make
8770    sense for the 32-bit format, this initial zero can be considered to
8771    be an escape value which indicates the presence of the older 64-bit
8772    format.  As written, the code can't detect (old format) lengths
8773    greater than 4GB.  If it becomes necessary to handle lengths
8774    somewhat larger than 4GB, we could allow other small values (such
8775    as the non-sensical values of 1, 2, and 3) to also be used as
8776    escape values indicating the presence of the old format.
8777
8778    The value returned via bytes_read should be used to increment the
8779    relevant pointer after calling read_initial_length().
8780
8781    [ Note:  read_initial_length() and read_offset() are based on the
8782      document entitled "DWARF Debugging Information Format", revision
8783      3, draft 8, dated November 19, 2001.  This document was obtained
8784      from:
8785
8786         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
8787
8788      This document is only a draft and is subject to change.  (So beware.)
8789
8790      Details regarding the older, non-standard 64-bit format were
8791      determined empirically by examining 64-bit ELF files produced by
8792      the SGI toolchain on an IRIX 6.5 machine.
8793
8794      - Kevin, July 16, 2002
8795    ] */
8796
8797 static LONGEST
8798 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
8799 {
8800   LONGEST length = bfd_get_32 (abfd, buf);
8801
8802   if (length == 0xffffffff)
8803     {
8804       length = bfd_get_64 (abfd, buf + 4);
8805       *bytes_read = 12;
8806     }
8807   else if (length == 0)
8808     {
8809       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
8810       length = bfd_get_64 (abfd, buf);
8811       *bytes_read = 8;
8812     }
8813   else
8814     {
8815       *bytes_read = 4;
8816     }
8817
8818   return length;
8819 }
8820
8821 /* Cover function for read_initial_length.
8822    Returns the length of the object at BUF, and stores the size of the
8823    initial length in *BYTES_READ and stores the size that offsets will be in
8824    *OFFSET_SIZE.
8825    If the initial length size is not equivalent to that specified in
8826    CU_HEADER then issue a complaint.
8827    This is useful when reading non-comp-unit headers.  */
8828
8829 static LONGEST
8830 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8831                                         const struct comp_unit_head *cu_header,
8832                                         unsigned int *bytes_read,
8833                                         unsigned int *offset_size)
8834 {
8835   LONGEST length = read_initial_length (abfd, buf, bytes_read);
8836
8837   gdb_assert (cu_header->initial_length_size == 4
8838               || cu_header->initial_length_size == 8
8839               || cu_header->initial_length_size == 12);
8840
8841   if (cu_header->initial_length_size != *bytes_read)
8842     complaint (&symfile_complaints,
8843                _("intermixed 32-bit and 64-bit DWARF sections"));
8844
8845   *offset_size = (*bytes_read == 4) ? 4 : 8;
8846   return length;
8847 }
8848
8849 /* Read an offset from the data stream.  The size of the offset is
8850    given by cu_header->offset_size.  */
8851
8852 static LONGEST
8853 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
8854              unsigned int *bytes_read)
8855 {
8856   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
8857
8858   *bytes_read = cu_header->offset_size;
8859   return offset;
8860 }
8861
8862 /* Read an offset from the data stream.  */
8863
8864 static LONGEST
8865 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
8866 {
8867   LONGEST retval = 0;
8868
8869   switch (offset_size)
8870     {
8871     case 4:
8872       retval = bfd_get_32 (abfd, buf);
8873       break;
8874     case 8:
8875       retval = bfd_get_64 (abfd, buf);
8876       break;
8877     default:
8878       internal_error (__FILE__, __LINE__,
8879                       _("read_offset_1: bad switch [in module %s]"),
8880                       bfd_get_filename (abfd));
8881     }
8882
8883   return retval;
8884 }
8885
8886 static gdb_byte *
8887 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
8888 {
8889   /* If the size of a host char is 8 bits, we can return a pointer
8890      to the buffer, otherwise we have to copy the data to a buffer
8891      allocated on the temporary obstack.  */
8892   gdb_assert (HOST_CHAR_BIT == 8);
8893   return buf;
8894 }
8895
8896 static char *
8897 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8898 {
8899   /* If the size of a host char is 8 bits, we can return a pointer
8900      to the string, otherwise we have to copy the string to a buffer
8901      allocated on the temporary obstack.  */
8902   gdb_assert (HOST_CHAR_BIT == 8);
8903   if (*buf == '\0')
8904     {
8905       *bytes_read_ptr = 1;
8906       return NULL;
8907     }
8908   *bytes_read_ptr = strlen ((char *) buf) + 1;
8909   return (char *) buf;
8910 }
8911
8912 static char *
8913 read_indirect_string (bfd *abfd, gdb_byte *buf,
8914                       const struct comp_unit_head *cu_header,
8915                       unsigned int *bytes_read_ptr)
8916 {
8917   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
8918
8919   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
8920   if (dwarf2_per_objfile->str.buffer == NULL)
8921     {
8922       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
8923                       bfd_get_filename (abfd));
8924       return NULL;
8925     }
8926   if (str_offset >= dwarf2_per_objfile->str.size)
8927     {
8928       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
8929                       bfd_get_filename (abfd));
8930       return NULL;
8931     }
8932   gdb_assert (HOST_CHAR_BIT == 8);
8933   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
8934     return NULL;
8935   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
8936 }
8937
8938 static unsigned long
8939 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8940 {
8941   unsigned long result;
8942   unsigned int num_read;
8943   int i, shift;
8944   unsigned char byte;
8945
8946   result = 0;
8947   shift = 0;
8948   num_read = 0;
8949   i = 0;
8950   while (1)
8951     {
8952       byte = bfd_get_8 (abfd, buf);
8953       buf++;
8954       num_read++;
8955       result |= ((unsigned long)(byte & 127) << shift);
8956       if ((byte & 128) == 0)
8957         {
8958           break;
8959         }
8960       shift += 7;
8961     }
8962   *bytes_read_ptr = num_read;
8963   return result;
8964 }
8965
8966 static long
8967 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
8968 {
8969   long result;
8970   int i, shift, num_read;
8971   unsigned char byte;
8972
8973   result = 0;
8974   shift = 0;
8975   num_read = 0;
8976   i = 0;
8977   while (1)
8978     {
8979       byte = bfd_get_8 (abfd, buf);
8980       buf++;
8981       num_read++;
8982       result |= ((long)(byte & 127) << shift);
8983       shift += 7;
8984       if ((byte & 128) == 0)
8985         {
8986           break;
8987         }
8988     }
8989   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
8990     result |= -(((long)1) << shift);
8991   *bytes_read_ptr = num_read;
8992   return result;
8993 }
8994
8995 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
8996
8997 static gdb_byte *
8998 skip_leb128 (bfd *abfd, gdb_byte *buf)
8999 {
9000   int byte;
9001
9002   while (1)
9003     {
9004       byte = bfd_get_8 (abfd, buf);
9005       buf++;
9006       if ((byte & 128) == 0)
9007         return buf;
9008     }
9009 }
9010
9011 static void
9012 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9013 {
9014   switch (lang)
9015     {
9016     case DW_LANG_C89:
9017     case DW_LANG_C99:
9018     case DW_LANG_C:
9019       cu->language = language_c;
9020       break;
9021     case DW_LANG_C_plus_plus:
9022       cu->language = language_cplus;
9023       break;
9024     case DW_LANG_D:
9025       cu->language = language_d;
9026       break;
9027     case DW_LANG_Fortran77:
9028     case DW_LANG_Fortran90:
9029     case DW_LANG_Fortran95:
9030       cu->language = language_fortran;
9031       break;
9032     case DW_LANG_Mips_Assembler:
9033       cu->language = language_asm;
9034       break;
9035     case DW_LANG_Java:
9036       cu->language = language_java;
9037       break;
9038     case DW_LANG_Ada83:
9039     case DW_LANG_Ada95:
9040       cu->language = language_ada;
9041       break;
9042     case DW_LANG_Modula2:
9043       cu->language = language_m2;
9044       break;
9045     case DW_LANG_Pascal83:
9046       cu->language = language_pascal;
9047       break;
9048     case DW_LANG_ObjC:
9049       cu->language = language_objc;
9050       break;
9051     case DW_LANG_Cobol74:
9052     case DW_LANG_Cobol85:
9053     default:
9054       cu->language = language_minimal;
9055       break;
9056     }
9057   cu->language_defn = language_def (cu->language);
9058 }
9059
9060 /* Return the named attribute or NULL if not there.  */
9061
9062 static struct attribute *
9063 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9064 {
9065   unsigned int i;
9066   struct attribute *spec = NULL;
9067
9068   for (i = 0; i < die->num_attrs; ++i)
9069     {
9070       if (die->attrs[i].name == name)
9071         return &die->attrs[i];
9072       if (die->attrs[i].name == DW_AT_specification
9073           || die->attrs[i].name == DW_AT_abstract_origin)
9074         spec = &die->attrs[i];
9075     }
9076
9077   if (spec)
9078     {
9079       die = follow_die_ref (die, spec, &cu);
9080       return dwarf2_attr (die, name, cu);
9081     }
9082
9083   return NULL;
9084 }
9085
9086 /* Return the named attribute or NULL if not there,
9087    but do not follow DW_AT_specification, etc.
9088    This is for use in contexts where we're reading .debug_types dies.
9089    Following DW_AT_specification, DW_AT_abstract_origin will take us
9090    back up the chain, and we want to go down.  */
9091
9092 static struct attribute *
9093 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9094                        struct dwarf2_cu *cu)
9095 {
9096   unsigned int i;
9097
9098   for (i = 0; i < die->num_attrs; ++i)
9099     if (die->attrs[i].name == name)
9100       return &die->attrs[i];
9101
9102   return NULL;
9103 }
9104
9105 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9106    and holds a non-zero value.  This function should only be used for
9107    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9108
9109 static int
9110 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9111 {
9112   struct attribute *attr = dwarf2_attr (die, name, cu);
9113
9114   return (attr && DW_UNSND (attr));
9115 }
9116
9117 static int
9118 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9119 {
9120   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9121      which value is non-zero.  However, we have to be careful with
9122      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9123      (via dwarf2_flag_true_p) follows this attribute.  So we may
9124      end up accidently finding a declaration attribute that belongs
9125      to a different DIE referenced by the specification attribute,
9126      even though the given DIE does not have a declaration attribute.  */
9127   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9128           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9129 }
9130
9131 /* Return the die giving the specification for DIE, if there is
9132    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9133    containing the return value on output.  If there is no
9134    specification, but there is an abstract origin, that is
9135    returned.  */
9136
9137 static struct die_info *
9138 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9139 {
9140   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9141                                              *spec_cu);
9142
9143   if (spec_attr == NULL)
9144     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9145
9146   if (spec_attr == NULL)
9147     return NULL;
9148   else
9149     return follow_die_ref (die, spec_attr, spec_cu);
9150 }
9151
9152 /* Free the line_header structure *LH, and any arrays and strings it
9153    refers to.  */
9154 static void
9155 free_line_header (struct line_header *lh)
9156 {
9157   if (lh->standard_opcode_lengths)
9158     xfree (lh->standard_opcode_lengths);
9159
9160   /* Remember that all the lh->file_names[i].name pointers are
9161      pointers into debug_line_buffer, and don't need to be freed.  */
9162   if (lh->file_names)
9163     xfree (lh->file_names);
9164
9165   /* Similarly for the include directory names.  */
9166   if (lh->include_dirs)
9167     xfree (lh->include_dirs);
9168
9169   xfree (lh);
9170 }
9171
9172
9173 /* Add an entry to LH's include directory table.  */
9174 static void
9175 add_include_dir (struct line_header *lh, char *include_dir)
9176 {
9177   /* Grow the array if necessary.  */
9178   if (lh->include_dirs_size == 0)
9179     {
9180       lh->include_dirs_size = 1; /* for testing */
9181       lh->include_dirs = xmalloc (lh->include_dirs_size
9182                                   * sizeof (*lh->include_dirs));
9183     }
9184   else if (lh->num_include_dirs >= lh->include_dirs_size)
9185     {
9186       lh->include_dirs_size *= 2;
9187       lh->include_dirs = xrealloc (lh->include_dirs,
9188                                    (lh->include_dirs_size
9189                                     * sizeof (*lh->include_dirs)));
9190     }
9191
9192   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9193 }
9194
9195
9196 /* Add an entry to LH's file name table.  */
9197 static void
9198 add_file_name (struct line_header *lh,
9199                char *name,
9200                unsigned int dir_index,
9201                unsigned int mod_time,
9202                unsigned int length)
9203 {
9204   struct file_entry *fe;
9205
9206   /* Grow the array if necessary.  */
9207   if (lh->file_names_size == 0)
9208     {
9209       lh->file_names_size = 1; /* for testing */
9210       lh->file_names = xmalloc (lh->file_names_size
9211                                 * sizeof (*lh->file_names));
9212     }
9213   else if (lh->num_file_names >= lh->file_names_size)
9214     {
9215       lh->file_names_size *= 2;
9216       lh->file_names = xrealloc (lh->file_names,
9217                                  (lh->file_names_size
9218                                   * sizeof (*lh->file_names)));
9219     }
9220
9221   fe = &lh->file_names[lh->num_file_names++];
9222   fe->name = name;
9223   fe->dir_index = dir_index;
9224   fe->mod_time = mod_time;
9225   fe->length = length;
9226   fe->included_p = 0;
9227   fe->symtab = NULL;
9228 }
9229
9230
9231 /* Read the statement program header starting at OFFSET in
9232    .debug_line, according to the endianness of ABFD.  Return a pointer
9233    to a struct line_header, allocated using xmalloc.
9234
9235    NOTE: the strings in the include directory and file name tables of
9236    the returned object point into debug_line_buffer, and must not be
9237    freed.  */
9238 static struct line_header *
9239 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9240                           struct dwarf2_cu *cu)
9241 {
9242   struct cleanup *back_to;
9243   struct line_header *lh;
9244   gdb_byte *line_ptr;
9245   unsigned int bytes_read, offset_size;
9246   int i;
9247   char *cur_dir, *cur_file;
9248
9249   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9250   if (dwarf2_per_objfile->line.buffer == NULL)
9251     {
9252       complaint (&symfile_complaints, _("missing .debug_line section"));
9253       return 0;
9254     }
9255
9256   /* Make sure that at least there's room for the total_length field.
9257      That could be 12 bytes long, but we're just going to fudge that.  */
9258   if (offset + 4 >= dwarf2_per_objfile->line.size)
9259     {
9260       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9261       return 0;
9262     }
9263
9264   lh = xmalloc (sizeof (*lh));
9265   memset (lh, 0, sizeof (*lh));
9266   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9267                           (void *) lh);
9268
9269   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9270
9271   /* Read in the header.  */
9272   lh->total_length =
9273     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9274                                             &bytes_read, &offset_size);
9275   line_ptr += bytes_read;
9276   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9277                                      + dwarf2_per_objfile->line.size))
9278     {
9279       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9280       return 0;
9281     }
9282   lh->statement_program_end = line_ptr + lh->total_length;
9283   lh->version = read_2_bytes (abfd, line_ptr);
9284   line_ptr += 2;
9285   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9286   line_ptr += offset_size;
9287   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9288   line_ptr += 1;
9289   if (lh->version >= 4)
9290     {
9291       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9292       line_ptr += 1;
9293     }
9294   else
9295     lh->maximum_ops_per_instruction = 1;
9296
9297   if (lh->maximum_ops_per_instruction == 0)
9298     {
9299       lh->maximum_ops_per_instruction = 1;
9300       complaint (&symfile_complaints,
9301                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9302     }
9303
9304   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9305   line_ptr += 1;
9306   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9307   line_ptr += 1;
9308   lh->line_range = read_1_byte (abfd, line_ptr);
9309   line_ptr += 1;
9310   lh->opcode_base = read_1_byte (abfd, line_ptr);
9311   line_ptr += 1;
9312   lh->standard_opcode_lengths
9313     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9314
9315   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9316   for (i = 1; i < lh->opcode_base; ++i)
9317     {
9318       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9319       line_ptr += 1;
9320     }
9321
9322   /* Read directory table.  */
9323   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9324     {
9325       line_ptr += bytes_read;
9326       add_include_dir (lh, cur_dir);
9327     }
9328   line_ptr += bytes_read;
9329
9330   /* Read file name table.  */
9331   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9332     {
9333       unsigned int dir_index, mod_time, length;
9334
9335       line_ptr += bytes_read;
9336       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9337       line_ptr += bytes_read;
9338       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9339       line_ptr += bytes_read;
9340       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9341       line_ptr += bytes_read;
9342
9343       add_file_name (lh, cur_file, dir_index, mod_time, length);
9344     }
9345   line_ptr += bytes_read;
9346   lh->statement_program_start = line_ptr;
9347
9348   if (line_ptr > (dwarf2_per_objfile->line.buffer
9349                   + dwarf2_per_objfile->line.size))
9350     complaint (&symfile_complaints,
9351                _("line number info header doesn't fit in `.debug_line' section"));
9352
9353   discard_cleanups (back_to);
9354   return lh;
9355 }
9356
9357 /* This function exists to work around a bug in certain compilers
9358    (particularly GCC 2.95), in which the first line number marker of a
9359    function does not show up until after the prologue, right before
9360    the second line number marker.  This function shifts ADDRESS down
9361    to the beginning of the function if necessary, and is called on
9362    addresses passed to record_line.  */
9363
9364 static CORE_ADDR
9365 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9366 {
9367   struct function_range *fn;
9368
9369   /* Find the function_range containing address.  */
9370   if (!cu->first_fn)
9371     return address;
9372
9373   if (!cu->cached_fn)
9374     cu->cached_fn = cu->first_fn;
9375
9376   fn = cu->cached_fn;
9377   while (fn)
9378     if (fn->lowpc <= address && fn->highpc > address)
9379       goto found;
9380     else
9381       fn = fn->next;
9382
9383   fn = cu->first_fn;
9384   while (fn && fn != cu->cached_fn)
9385     if (fn->lowpc <= address && fn->highpc > address)
9386       goto found;
9387     else
9388       fn = fn->next;
9389
9390   return address;
9391
9392  found:
9393   if (fn->seen_line)
9394     return address;
9395   if (address != fn->lowpc)
9396     complaint (&symfile_complaints,
9397                _("misplaced first line number at 0x%lx for '%s'"),
9398                (unsigned long) address, fn->name);
9399   fn->seen_line = 1;
9400   return fn->lowpc;
9401 }
9402
9403 /* Decode the Line Number Program (LNP) for the given line_header
9404    structure and CU.  The actual information extracted and the type
9405    of structures created from the LNP depends on the value of PST.
9406
9407    1. If PST is NULL, then this procedure uses the data from the program
9408       to create all necessary symbol tables, and their linetables.
9409       The compilation directory of the file is passed in COMP_DIR,
9410       and must not be NULL.
9411
9412    2. If PST is not NULL, this procedure reads the program to determine
9413       the list of files included by the unit represented by PST, and
9414       builds all the associated partial symbol tables.  In this case,
9415       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9416       is not used to compute the full name of the symtab, and therefore
9417       omitting it when building the partial symtab does not introduce
9418       the potential for inconsistency - a partial symtab and its associated
9419       symbtab having a different fullname -).  */
9420
9421 static void
9422 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9423                     struct dwarf2_cu *cu, struct partial_symtab *pst)
9424 {
9425   gdb_byte *line_ptr, *extended_end;
9426   gdb_byte *line_end;
9427   unsigned int bytes_read, extended_len;
9428   unsigned char op_code, extended_op, adj_opcode;
9429   CORE_ADDR baseaddr;
9430   struct objfile *objfile = cu->objfile;
9431   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9432   const int decode_for_pst_p = (pst != NULL);
9433   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9434
9435   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9436
9437   line_ptr = lh->statement_program_start;
9438   line_end = lh->statement_program_end;
9439
9440   /* Read the statement sequences until there's nothing left.  */
9441   while (line_ptr < line_end)
9442     {
9443       /* state machine registers  */
9444       CORE_ADDR address = 0;
9445       unsigned int file = 1;
9446       unsigned int line = 1;
9447       unsigned int column = 0;
9448       int is_stmt = lh->default_is_stmt;
9449       int basic_block = 0;
9450       int end_sequence = 0;
9451       CORE_ADDR addr;
9452       unsigned char op_index = 0;
9453
9454       if (!decode_for_pst_p && lh->num_file_names >= file)
9455         {
9456           /* Start a subfile for the current file of the state machine.  */
9457           /* lh->include_dirs and lh->file_names are 0-based, but the
9458              directory and file name numbers in the statement program
9459              are 1-based.  */
9460           struct file_entry *fe = &lh->file_names[file - 1];
9461           char *dir = NULL;
9462
9463           if (fe->dir_index)
9464             dir = lh->include_dirs[fe->dir_index - 1];
9465
9466           dwarf2_start_subfile (fe->name, dir, comp_dir);
9467         }
9468
9469       /* Decode the table.  */
9470       while (!end_sequence)
9471         {
9472           op_code = read_1_byte (abfd, line_ptr);
9473           line_ptr += 1;
9474           if (line_ptr > line_end)
9475             {
9476               dwarf2_debug_line_missing_end_sequence_complaint ();
9477               break;
9478             }
9479
9480           if (op_code >= lh->opcode_base)
9481             {
9482               /* Special operand.  */
9483               adj_opcode = op_code - lh->opcode_base;
9484               address += (((op_index + (adj_opcode / lh->line_range))
9485                            / lh->maximum_ops_per_instruction)
9486                           * lh->minimum_instruction_length);
9487               op_index = ((op_index + (adj_opcode / lh->line_range))
9488                           % lh->maximum_ops_per_instruction);
9489               line += lh->line_base + (adj_opcode % lh->line_range);
9490               if (lh->num_file_names < file || file == 0)
9491                 dwarf2_debug_line_missing_file_complaint ();
9492               /* For now we ignore lines not starting on an
9493                  instruction boundary.  */
9494               else if (op_index == 0)
9495                 {
9496                   lh->file_names[file - 1].included_p = 1;
9497                   if (!decode_for_pst_p && is_stmt)
9498                     {
9499                       if (last_subfile != current_subfile)
9500                         {
9501                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9502                           if (last_subfile)
9503                             record_line (last_subfile, 0, addr);
9504                           last_subfile = current_subfile;
9505                         }
9506                       /* Append row to matrix using current values.  */
9507                       addr = check_cu_functions (address, cu);
9508                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9509                       record_line (current_subfile, line, addr);
9510                     }
9511                 }
9512               basic_block = 0;
9513             }
9514           else switch (op_code)
9515             {
9516             case DW_LNS_extended_op:
9517               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9518               line_ptr += bytes_read;
9519               extended_end = line_ptr + extended_len;
9520               extended_op = read_1_byte (abfd, line_ptr);
9521               line_ptr += 1;
9522               switch (extended_op)
9523                 {
9524                 case DW_LNE_end_sequence:
9525                   end_sequence = 1;
9526                   break;
9527                 case DW_LNE_set_address:
9528                   address = read_address (abfd, line_ptr, cu, &bytes_read);
9529                   op_index = 0;
9530                   line_ptr += bytes_read;
9531                   address += baseaddr;
9532                   break;
9533                 case DW_LNE_define_file:
9534                   {
9535                     char *cur_file;
9536                     unsigned int dir_index, mod_time, length;
9537
9538                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
9539                     line_ptr += bytes_read;
9540                     dir_index =
9541                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9542                     line_ptr += bytes_read;
9543                     mod_time =
9544                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9545                     line_ptr += bytes_read;
9546                     length =
9547                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9548                     line_ptr += bytes_read;
9549                     add_file_name (lh, cur_file, dir_index, mod_time, length);
9550                   }
9551                   break;
9552                 case DW_LNE_set_discriminator:
9553                   /* The discriminator is not interesting to the debugger;
9554                      just ignore it.  */
9555                   line_ptr = extended_end;
9556                   break;
9557                 default:
9558                   complaint (&symfile_complaints,
9559                              _("mangled .debug_line section"));
9560                   return;
9561                 }
9562               /* Make sure that we parsed the extended op correctly.  If e.g.
9563                  we expected a different address size than the producer used,
9564                  we may have read the wrong number of bytes.  */
9565               if (line_ptr != extended_end)
9566                 {
9567                   complaint (&symfile_complaints,
9568                              _("mangled .debug_line section"));
9569                   return;
9570                 }
9571               break;
9572             case DW_LNS_copy:
9573               if (lh->num_file_names < file || file == 0)
9574                 dwarf2_debug_line_missing_file_complaint ();
9575               else
9576                 {
9577                   lh->file_names[file - 1].included_p = 1;
9578                   if (!decode_for_pst_p && is_stmt)
9579                     {
9580                       if (last_subfile != current_subfile)
9581                         {
9582                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9583                           if (last_subfile)
9584                             record_line (last_subfile, 0, addr);
9585                           last_subfile = current_subfile;
9586                         }
9587                       addr = check_cu_functions (address, cu);
9588                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9589                       record_line (current_subfile, line, addr);
9590                     }
9591                 }
9592               basic_block = 0;
9593               break;
9594             case DW_LNS_advance_pc:
9595               {
9596                 CORE_ADDR adjust
9597                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9598
9599                 address += (((op_index + adjust)
9600                              / lh->maximum_ops_per_instruction)
9601                             * lh->minimum_instruction_length);
9602                 op_index = ((op_index + adjust)
9603                             % lh->maximum_ops_per_instruction);
9604                 line_ptr += bytes_read;
9605               }
9606               break;
9607             case DW_LNS_advance_line:
9608               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9609               line_ptr += bytes_read;
9610               break;
9611             case DW_LNS_set_file:
9612               {
9613                 /* The arrays lh->include_dirs and lh->file_names are
9614                    0-based, but the directory and file name numbers in
9615                    the statement program are 1-based.  */
9616                 struct file_entry *fe;
9617                 char *dir = NULL;
9618
9619                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9620                 line_ptr += bytes_read;
9621                 if (lh->num_file_names < file || file == 0)
9622                   dwarf2_debug_line_missing_file_complaint ();
9623                 else
9624                   {
9625                     fe = &lh->file_names[file - 1];
9626                     if (fe->dir_index)
9627                       dir = lh->include_dirs[fe->dir_index - 1];
9628                     if (!decode_for_pst_p)
9629                       {
9630                         last_subfile = current_subfile;
9631                         dwarf2_start_subfile (fe->name, dir, comp_dir);
9632                       }
9633                   }
9634               }
9635               break;
9636             case DW_LNS_set_column:
9637               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9638               line_ptr += bytes_read;
9639               break;
9640             case DW_LNS_negate_stmt:
9641               is_stmt = (!is_stmt);
9642               break;
9643             case DW_LNS_set_basic_block:
9644               basic_block = 1;
9645               break;
9646             /* Add to the address register of the state machine the
9647                address increment value corresponding to special opcode
9648                255.  I.e., this value is scaled by the minimum
9649                instruction length since special opcode 255 would have
9650                scaled the the increment.  */
9651             case DW_LNS_const_add_pc:
9652               {
9653                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9654
9655                 address += (((op_index + adjust)
9656                              / lh->maximum_ops_per_instruction)
9657                             * lh->minimum_instruction_length);
9658                 op_index = ((op_index + adjust)
9659                             % lh->maximum_ops_per_instruction);
9660               }
9661               break;
9662             case DW_LNS_fixed_advance_pc:
9663               address += read_2_bytes (abfd, line_ptr);
9664               op_index = 0;
9665               line_ptr += 2;
9666               break;
9667             default:
9668               {
9669                 /* Unknown standard opcode, ignore it.  */
9670                 int i;
9671
9672                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9673                   {
9674                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9675                     line_ptr += bytes_read;
9676                   }
9677               }
9678             }
9679         }
9680       if (lh->num_file_names < file || file == 0)
9681         dwarf2_debug_line_missing_file_complaint ();
9682       else
9683         {
9684           lh->file_names[file - 1].included_p = 1;
9685           if (!decode_for_pst_p)
9686             {
9687               addr = gdbarch_addr_bits_remove (gdbarch, address);
9688               record_line (current_subfile, 0, addr);
9689             }
9690         }
9691     }
9692
9693   if (decode_for_pst_p)
9694     {
9695       int file_index;
9696
9697       /* Now that we're done scanning the Line Header Program, we can
9698          create the psymtab of each included file.  */
9699       for (file_index = 0; file_index < lh->num_file_names; file_index++)
9700         if (lh->file_names[file_index].included_p == 1)
9701           {
9702             const struct file_entry fe = lh->file_names [file_index];
9703             char *include_name = fe.name;
9704             char *dir_name = NULL;
9705             char *pst_filename = pst->filename;
9706
9707             if (fe.dir_index)
9708               dir_name = lh->include_dirs[fe.dir_index - 1];
9709
9710             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9711               {
9712                 include_name = concat (dir_name, SLASH_STRING,
9713                                        include_name, (char *)NULL);
9714                 make_cleanup (xfree, include_name);
9715               }
9716
9717             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9718               {
9719                 pst_filename = concat (pst->dirname, SLASH_STRING,
9720                                        pst_filename, (char *)NULL);
9721                 make_cleanup (xfree, pst_filename);
9722               }
9723
9724             if (strcmp (include_name, pst_filename) != 0)
9725               dwarf2_create_include_psymtab (include_name, pst, objfile);
9726           }
9727     }
9728   else
9729     {
9730       /* Make sure a symtab is created for every file, even files
9731          which contain only variables (i.e. no code with associated
9732          line numbers).  */
9733
9734       int i;
9735       struct file_entry *fe;
9736
9737       for (i = 0; i < lh->num_file_names; i++)
9738         {
9739           char *dir = NULL;
9740
9741           fe = &lh->file_names[i];
9742           if (fe->dir_index)
9743             dir = lh->include_dirs[fe->dir_index - 1];
9744           dwarf2_start_subfile (fe->name, dir, comp_dir);
9745
9746           /* Skip the main file; we don't need it, and it must be
9747              allocated last, so that it will show up before the
9748              non-primary symtabs in the objfile's symtab list.  */
9749           if (current_subfile == first_subfile)
9750             continue;
9751
9752           if (current_subfile->symtab == NULL)
9753             current_subfile->symtab = allocate_symtab (current_subfile->name,
9754                                                        cu->objfile);
9755           fe->symtab = current_subfile->symtab;
9756         }
9757     }
9758 }
9759
9760 /* Start a subfile for DWARF.  FILENAME is the name of the file and
9761    DIRNAME the name of the source directory which contains FILENAME
9762    or NULL if not known.  COMP_DIR is the compilation directory for the
9763    linetable's compilation unit or NULL if not known.
9764    This routine tries to keep line numbers from identical absolute and
9765    relative file names in a common subfile.
9766
9767    Using the `list' example from the GDB testsuite, which resides in
9768    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9769    of /srcdir/list0.c yields the following debugging information for list0.c:
9770
9771    DW_AT_name:          /srcdir/list0.c
9772    DW_AT_comp_dir:              /compdir
9773    files.files[0].name: list0.h
9774    files.files[0].dir:  /srcdir
9775    files.files[1].name: list0.c
9776    files.files[1].dir:  /srcdir
9777
9778    The line number information for list0.c has to end up in a single
9779    subfile, so that `break /srcdir/list0.c:1' works as expected.
9780    start_subfile will ensure that this happens provided that we pass the
9781    concatenation of files.files[1].dir and files.files[1].name as the
9782    subfile's name.  */
9783
9784 static void
9785 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
9786 {
9787   char *fullname;
9788
9789   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9790      `start_symtab' will always pass the contents of DW_AT_comp_dir as
9791      second argument to start_subfile.  To be consistent, we do the
9792      same here.  In order not to lose the line information directory,
9793      we concatenate it to the filename when it makes sense.
9794      Note that the Dwarf3 standard says (speaking of filenames in line
9795      information): ``The directory index is ignored for file names
9796      that represent full path names''.  Thus ignoring dirname in the
9797      `else' branch below isn't an issue.  */
9798
9799   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
9800     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9801   else
9802     fullname = filename;
9803
9804   start_subfile (fullname, comp_dir);
9805
9806   if (fullname != filename)
9807     xfree (fullname);
9808 }
9809
9810 static void
9811 var_decode_location (struct attribute *attr, struct symbol *sym,
9812                      struct dwarf2_cu *cu)
9813 {
9814   struct objfile *objfile = cu->objfile;
9815   struct comp_unit_head *cu_header = &cu->header;
9816
9817   /* NOTE drow/2003-01-30: There used to be a comment and some special
9818      code here to turn a symbol with DW_AT_external and a
9819      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
9820      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9821      with some versions of binutils) where shared libraries could have
9822      relocations against symbols in their debug information - the
9823      minimal symbol would have the right address, but the debug info
9824      would not.  It's no longer necessary, because we will explicitly
9825      apply relocations when we read in the debug information now.  */
9826
9827   /* A DW_AT_location attribute with no contents indicates that a
9828      variable has been optimized away.  */
9829   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9830     {
9831       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9832       return;
9833     }
9834
9835   /* Handle one degenerate form of location expression specially, to
9836      preserve GDB's previous behavior when section offsets are
9837      specified.  If this is just a DW_OP_addr then mark this symbol
9838      as LOC_STATIC.  */
9839
9840   if (attr_form_is_block (attr)
9841       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9842       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9843     {
9844       unsigned int dummy;
9845
9846       SYMBOL_VALUE_ADDRESS (sym) =
9847         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
9848       SYMBOL_CLASS (sym) = LOC_STATIC;
9849       fixup_symbol_section (sym, objfile);
9850       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9851                                               SYMBOL_SECTION (sym));
9852       return;
9853     }
9854
9855   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9856      expression evaluator, and use LOC_COMPUTED only when necessary
9857      (i.e. when the value of a register or memory location is
9858      referenced, or a thread-local block, etc.).  Then again, it might
9859      not be worthwhile.  I'm assuming that it isn't unless performance
9860      or memory numbers show me otherwise.  */
9861
9862   dwarf2_symbol_mark_computed (attr, sym, cu);
9863   SYMBOL_CLASS (sym) = LOC_COMPUTED;
9864 }
9865
9866 /* Given a pointer to a DWARF information entry, figure out if we need
9867    to make a symbol table entry for it, and if so, create a new entry
9868    and return a pointer to it.
9869    If TYPE is NULL, determine symbol type from the die, otherwise
9870    used the passed type.  */
9871
9872 static struct symbol *
9873 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
9874 {
9875   struct objfile *objfile = cu->objfile;
9876   struct symbol *sym = NULL;
9877   char *name;
9878   struct attribute *attr = NULL;
9879   struct attribute *attr2 = NULL;
9880   CORE_ADDR baseaddr;
9881   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9882
9883   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9884
9885   name = dwarf2_name (die, cu);
9886   if (name)
9887     {
9888       const char *linkagename;
9889
9890       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
9891                                              sizeof (struct symbol));
9892       OBJSTAT (objfile, n_syms++);
9893       memset (sym, 0, sizeof (struct symbol));
9894
9895       /* Cache this symbol's name and the name's demangled form (if any).  */
9896       SYMBOL_LANGUAGE (sym) = cu->language;
9897       linkagename = dwarf2_physname (name, die, cu);
9898       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
9899
9900       /* Fortran does not have mangling standard and the mangling does differ
9901          between gfortran, iFort etc.  */
9902       if (cu->language == language_fortran
9903           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
9904         symbol_set_demangled_name (&(sym->ginfo),
9905                                    (char *) dwarf2_full_name (name, die, cu),
9906                                    NULL);
9907
9908       /* Default assumptions.
9909          Use the passed type or decode it from the die.  */
9910       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
9911       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9912       if (type != NULL)
9913         SYMBOL_TYPE (sym) = type;
9914       else
9915         SYMBOL_TYPE (sym) = die_type (die, cu);
9916       attr = dwarf2_attr (die,
9917                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
9918                           cu);
9919       if (attr)
9920         {
9921           SYMBOL_LINE (sym) = DW_UNSND (attr);
9922         }
9923
9924       attr = dwarf2_attr (die,
9925                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
9926                           cu);
9927       if (attr)
9928         {
9929           int file_index = DW_UNSND (attr);
9930
9931           if (cu->line_header == NULL
9932               || file_index > cu->line_header->num_file_names)
9933             complaint (&symfile_complaints,
9934                        _("file index out of range"));
9935           else if (file_index > 0)
9936             {
9937               struct file_entry *fe;
9938
9939               fe = &cu->line_header->file_names[file_index - 1];
9940               SYMBOL_SYMTAB (sym) = fe->symtab;
9941             }
9942         }
9943
9944       switch (die->tag)
9945         {
9946         case DW_TAG_label:
9947           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
9948           if (attr)
9949             {
9950               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
9951             }
9952           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
9953           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
9954           SYMBOL_CLASS (sym) = LOC_LABEL;
9955           add_symbol_to_list (sym, cu->list_in_scope);
9956           break;
9957         case DW_TAG_subprogram:
9958           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9959              finish_block.  */
9960           SYMBOL_CLASS (sym) = LOC_BLOCK;
9961           attr2 = dwarf2_attr (die, DW_AT_external, cu);
9962           if ((attr2 && (DW_UNSND (attr2) != 0))
9963               || cu->language == language_ada)
9964             {
9965               /* Subprograms marked external are stored as a global symbol.
9966                  Ada subprograms, whether marked external or not, are always
9967                  stored as a global symbol, because we want to be able to
9968                  access them globally.  For instance, we want to be able
9969                  to break on a nested subprogram without having to
9970                  specify the context.  */
9971               add_symbol_to_list (sym, &global_symbols);
9972             }
9973           else
9974             {
9975               add_symbol_to_list (sym, cu->list_in_scope);
9976             }
9977           break;
9978         case DW_TAG_inlined_subroutine:
9979           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
9980              finish_block.  */
9981           SYMBOL_CLASS (sym) = LOC_BLOCK;
9982           SYMBOL_INLINED (sym) = 1;
9983           /* Do not add the symbol to any lists.  It will be found via
9984              BLOCK_FUNCTION from the blockvector.  */
9985           break;
9986         case DW_TAG_variable:
9987         case DW_TAG_member:
9988           /* Compilation with minimal debug info may result in variables
9989              with missing type entries. Change the misleading `void' type
9990              to something sensible.  */
9991           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
9992             SYMBOL_TYPE (sym)
9993               = objfile_type (objfile)->nodebug_data_symbol;
9994
9995           attr = dwarf2_attr (die, DW_AT_const_value, cu);
9996           /* In the case of DW_TAG_member, we should only be called for
9997              static const members.  */
9998           if (die->tag == DW_TAG_member)
9999             {
10000               /* dwarf2_add_field uses die_is_declaration,
10001                  so we do the same.  */
10002               gdb_assert (die_is_declaration (die, cu));
10003               gdb_assert (attr);
10004             }
10005           if (attr)
10006             {
10007               dwarf2_const_value (attr, sym, cu);
10008               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10009               if (attr2 && (DW_UNSND (attr2) != 0))
10010                 add_symbol_to_list (sym, &global_symbols);
10011               else
10012                 add_symbol_to_list (sym, cu->list_in_scope);
10013               break;
10014             }
10015           attr = dwarf2_attr (die, DW_AT_location, cu);
10016           if (attr)
10017             {
10018               var_decode_location (attr, sym, cu);
10019               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10020               if (SYMBOL_CLASS (sym) == LOC_STATIC
10021                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10022                   && !dwarf2_per_objfile->has_section_at_zero)
10023                 {
10024                   /* When a static variable is eliminated by the linker,
10025                      the corresponding debug information is not stripped
10026                      out, but the variable address is set to null;
10027                      do not add such variables into symbol table.  */
10028                 }
10029               else if (attr2 && (DW_UNSND (attr2) != 0))
10030                 {
10031                   struct pending **list_to_add;
10032
10033                   /* Workaround gfortran PR debug/40040 - it uses
10034                      DW_AT_location for variables in -fPIC libraries which may
10035                      get overriden by other libraries/executable and get
10036                      a different address.  Resolve it by the minimal symbol
10037                      which may come from inferior's executable using copy
10038                      relocation.  Make this workaround only for gfortran as for
10039                      other compilers GDB cannot guess the minimal symbol
10040                      Fortran mangling kind.  */
10041                   if (cu->language == language_fortran && die->parent
10042                       && die->parent->tag == DW_TAG_module
10043                       && cu->producer
10044                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10045                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10046
10047                   /* A variable with DW_AT_external is never static,
10048                      but it may be block-scoped.  */
10049                   list_to_add = (cu->list_in_scope == &file_symbols
10050                                  ? &global_symbols : cu->list_in_scope);
10051                   add_symbol_to_list (sym, list_to_add);
10052                 }
10053               else
10054                 add_symbol_to_list (sym, cu->list_in_scope);
10055             }
10056           else
10057             {
10058               /* We do not know the address of this symbol.
10059                  If it is an external symbol and we have type information
10060                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10061                  The address of the variable will then be determined from
10062                  the minimal symbol table whenever the variable is
10063                  referenced.  */
10064               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10065               if (attr2 && (DW_UNSND (attr2) != 0)
10066                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10067                 {
10068                   struct pending **list_to_add;
10069
10070                   /* A variable with DW_AT_external is never static, but it
10071                      may be block-scoped.  */
10072                   list_to_add = (cu->list_in_scope == &file_symbols
10073                                  ? &global_symbols : cu->list_in_scope);
10074
10075                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10076                   add_symbol_to_list (sym, list_to_add);
10077                 }
10078               else if (!die_is_declaration (die, cu))
10079                 {
10080                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10081                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10082                   add_symbol_to_list (sym, cu->list_in_scope);
10083                 }
10084             }
10085           break;
10086         case DW_TAG_formal_parameter:
10087           /* If we are inside a function, mark this as an argument.  If
10088              not, we might be looking at an argument to an inlined function
10089              when we do not have enough information to show inlined frames;
10090              pretend it's a local variable in that case so that the user can
10091              still see it.  */
10092           if (context_stack_depth > 0
10093               && context_stack[context_stack_depth - 1].name != NULL)
10094             SYMBOL_IS_ARGUMENT (sym) = 1;
10095           attr = dwarf2_attr (die, DW_AT_location, cu);
10096           if (attr)
10097             {
10098               var_decode_location (attr, sym, cu);
10099             }
10100           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10101           if (attr)
10102             {
10103               dwarf2_const_value (attr, sym, cu);
10104             }
10105           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10106           if (attr && DW_UNSND (attr))
10107             {
10108               struct type *ref_type;
10109
10110               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10111               SYMBOL_TYPE (sym) = ref_type;
10112             }
10113
10114           add_symbol_to_list (sym, cu->list_in_scope);
10115           break;
10116         case DW_TAG_unspecified_parameters:
10117           /* From varargs functions; gdb doesn't seem to have any
10118              interest in this information, so just ignore it for now.
10119              (FIXME?) */
10120           break;
10121         case DW_TAG_class_type:
10122         case DW_TAG_interface_type:
10123         case DW_TAG_structure_type:
10124         case DW_TAG_union_type:
10125         case DW_TAG_set_type:
10126         case DW_TAG_enumeration_type:
10127           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10128           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10129
10130           {
10131             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10132                really ever be static objects: otherwise, if you try
10133                to, say, break of a class's method and you're in a file
10134                which doesn't mention that class, it won't work unless
10135                the check for all static symbols in lookup_symbol_aux
10136                saves you.  See the OtherFileClass tests in
10137                gdb.c++/namespace.exp.  */
10138
10139             struct pending **list_to_add;
10140
10141             list_to_add = (cu->list_in_scope == &file_symbols
10142                            && (cu->language == language_cplus
10143                                || cu->language == language_java)
10144                            ? &global_symbols : cu->list_in_scope);
10145
10146             add_symbol_to_list (sym, list_to_add);
10147
10148             /* The semantics of C++ state that "struct foo { ... }" also
10149                defines a typedef for "foo".  A Java class declaration also
10150                defines a typedef for the class.  */
10151             if (cu->language == language_cplus
10152                 || cu->language == language_java
10153                 || cu->language == language_ada)
10154               {
10155                 /* The symbol's name is already allocated along with
10156                    this objfile, so we don't need to duplicate it for
10157                    the type.  */
10158                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10159                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10160               }
10161           }
10162           break;
10163         case DW_TAG_typedef:
10164           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10165           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10166           add_symbol_to_list (sym, cu->list_in_scope);
10167           break;
10168         case DW_TAG_base_type:
10169         case DW_TAG_subrange_type:
10170           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10171           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10172           add_symbol_to_list (sym, cu->list_in_scope);
10173           break;
10174         case DW_TAG_enumerator:
10175           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10176           if (attr)
10177             {
10178               dwarf2_const_value (attr, sym, cu);
10179             }
10180           {
10181             /* NOTE: carlton/2003-11-10: See comment above in the
10182                DW_TAG_class_type, etc. block.  */
10183
10184             struct pending **list_to_add;
10185
10186             list_to_add = (cu->list_in_scope == &file_symbols
10187                            && (cu->language == language_cplus
10188                                || cu->language == language_java)
10189                            ? &global_symbols : cu->list_in_scope);
10190
10191             add_symbol_to_list (sym, list_to_add);
10192           }
10193           break;
10194         case DW_TAG_namespace:
10195           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10196           add_symbol_to_list (sym, &global_symbols);
10197           break;
10198         default:
10199           /* Not a tag we recognize.  Hopefully we aren't processing
10200              trash data, but since we must specifically ignore things
10201              we don't recognize, there is nothing else we should do at
10202              this point. */
10203           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10204                      dwarf_tag_name (die->tag));
10205           break;
10206         }
10207
10208       /* For the benefit of old versions of GCC, check for anonymous
10209          namespaces based on the demangled name.  */
10210       if (!processing_has_namespace_info
10211           && cu->language == language_cplus)
10212         cp_scan_for_anonymous_namespaces (sym);
10213     }
10214   return (sym);
10215 }
10216
10217 /* Copy constant value from an attribute to a symbol.  */
10218
10219 static void
10220 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10221                     struct dwarf2_cu *cu)
10222 {
10223   struct objfile *objfile = cu->objfile;
10224   struct comp_unit_head *cu_header = &cu->header;
10225   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10226                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10227   struct dwarf_block *blk;
10228
10229   switch (attr->form)
10230     {
10231     case DW_FORM_addr:
10232       {
10233         struct dwarf2_locexpr_baton *baton;
10234         gdb_byte *data;
10235
10236         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10237           dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10238                                                         cu_header->addr_size,
10239                                                         TYPE_LENGTH (SYMBOL_TYPE
10240                                                                      (sym)));
10241         /* Symbols of this form are reasonably rare, so we just
10242            piggyback on the existing location code rather than writing
10243            a new implementation of symbol_computed_ops.  */
10244         baton = obstack_alloc (&objfile->objfile_obstack,
10245                                sizeof (struct dwarf2_locexpr_baton));
10246         baton->per_cu = cu->per_cu;
10247         gdb_assert (baton->per_cu);
10248
10249         baton->size = 2 + cu_header->addr_size;
10250         data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10251         baton->data = data;
10252
10253         data[0] = DW_OP_addr;
10254         store_unsigned_integer (&data[1], cu_header->addr_size,
10255                                 byte_order, DW_ADDR (attr));
10256         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10257
10258         SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10259         SYMBOL_LOCATION_BATON (sym) = baton;
10260         SYMBOL_CLASS (sym) = LOC_COMPUTED;
10261       }
10262       break;
10263     case DW_FORM_string:
10264     case DW_FORM_strp:
10265       /* DW_STRING is already allocated on the obstack, point directly
10266          to it.  */
10267       SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10268       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10269       break;
10270     case DW_FORM_block1:
10271     case DW_FORM_block2:
10272     case DW_FORM_block4:
10273     case DW_FORM_block:
10274     case DW_FORM_exprloc:
10275       blk = DW_BLOCK (attr);
10276       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
10277         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10278                                                       blk->size,
10279                                                       TYPE_LENGTH (SYMBOL_TYPE
10280                                                                    (sym)));
10281       SYMBOL_VALUE_BYTES (sym) =
10282         obstack_alloc (&objfile->objfile_obstack, blk->size);
10283       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10284       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10285       break;
10286
10287       /* The DW_AT_const_value attributes are supposed to carry the
10288          symbol's value "represented as it would be on the target
10289          architecture."  By the time we get here, it's already been
10290          converted to host endianness, so we just need to sign- or
10291          zero-extend it as appropriate.  */
10292     case DW_FORM_data1:
10293       dwarf2_const_value_data (attr, sym, 8);
10294       break;
10295     case DW_FORM_data2:
10296       dwarf2_const_value_data (attr, sym, 16);
10297       break;
10298     case DW_FORM_data4:
10299       dwarf2_const_value_data (attr, sym, 32);
10300       break;
10301     case DW_FORM_data8:
10302       dwarf2_const_value_data (attr, sym, 64);
10303       break;
10304
10305     case DW_FORM_sdata:
10306       SYMBOL_VALUE (sym) = DW_SND (attr);
10307       SYMBOL_CLASS (sym) = LOC_CONST;
10308       break;
10309
10310     case DW_FORM_udata:
10311       SYMBOL_VALUE (sym) = DW_UNSND (attr);
10312       SYMBOL_CLASS (sym) = LOC_CONST;
10313       break;
10314
10315     default:
10316       complaint (&symfile_complaints,
10317                  _("unsupported const value attribute form: '%s'"),
10318                  dwarf_form_name (attr->form));
10319       SYMBOL_VALUE (sym) = 0;
10320       SYMBOL_CLASS (sym) = LOC_CONST;
10321       break;
10322     }
10323 }
10324
10325
10326 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
10327    or zero-extend it as appropriate for the symbol's type.  */
10328 static void
10329 dwarf2_const_value_data (struct attribute *attr,
10330                          struct symbol *sym,
10331                          int bits)
10332 {
10333   LONGEST l = DW_UNSND (attr);
10334
10335   if (bits < sizeof (l) * 8)
10336     {
10337       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
10338         l &= ((LONGEST) 1 << bits) - 1;
10339       else
10340         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
10341     }
10342
10343   SYMBOL_VALUE (sym) = l;
10344   SYMBOL_CLASS (sym) = LOC_CONST;
10345 }
10346
10347
10348 /* Return the type of the die in question using its DW_AT_type attribute.  */
10349
10350 static struct type *
10351 die_type (struct die_info *die, struct dwarf2_cu *cu)
10352 {
10353   struct attribute *type_attr;
10354   struct die_info *type_die;
10355
10356   type_attr = dwarf2_attr (die, DW_AT_type, cu);
10357   if (!type_attr)
10358     {
10359       /* A missing DW_AT_type represents a void type.  */
10360       return objfile_type (cu->objfile)->builtin_void;
10361     }
10362
10363   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10364
10365   return tag_type_to_type (type_die, cu);
10366 }
10367
10368 /* True iff CU's producer generates GNAT Ada auxiliary information
10369    that allows to find parallel types through that information instead
10370    of having to do expensive parallel lookups by type name.  */
10371
10372 static int
10373 need_gnat_info (struct dwarf2_cu *cu)
10374 {
10375   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10376      of GNAT produces this auxiliary information, without any indication
10377      that it is produced.  Part of enhancing the FSF version of GNAT
10378      to produce that information will be to put in place an indicator
10379      that we can use in order to determine whether the descriptive type
10380      info is available or not.  One suggestion that has been made is
10381      to use a new attribute, attached to the CU die.  For now, assume
10382      that the descriptive type info is not available.  */
10383   return 0;
10384 }
10385
10386
10387 /* Return the auxiliary type of the die in question using its
10388    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
10389    attribute is not present.  */
10390
10391 static struct type *
10392 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10393 {
10394   struct attribute *type_attr;
10395   struct die_info *type_die;
10396
10397   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10398   if (!type_attr)
10399     return NULL;
10400
10401   type_die = follow_die_ref (die, type_attr, &cu);
10402   return tag_type_to_type (type_die, cu);
10403 }
10404
10405 /* If DIE has a descriptive_type attribute, then set the TYPE's
10406    descriptive type accordingly.  */
10407
10408 static void
10409 set_descriptive_type (struct type *type, struct die_info *die,
10410                       struct dwarf2_cu *cu)
10411 {
10412   struct type *descriptive_type = die_descriptive_type (die, cu);
10413
10414   if (descriptive_type)
10415     {
10416       ALLOCATE_GNAT_AUX_TYPE (type);
10417       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10418     }
10419 }
10420
10421 /* Return the containing type of the die in question using its
10422    DW_AT_containing_type attribute.  */
10423
10424 static struct type *
10425 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10426 {
10427   struct attribute *type_attr;
10428   struct die_info *type_die;
10429
10430   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10431   if (!type_attr)
10432     error (_("Dwarf Error: Problem turning containing type into gdb type "
10433              "[in module %s]"), cu->objfile->name);
10434
10435   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10436   return tag_type_to_type (type_die, cu);
10437 }
10438
10439 static struct type *
10440 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
10441 {
10442   struct type *this_type;
10443
10444   this_type = read_type_die (die, cu);
10445   if (!this_type)
10446     {
10447       char *message, *saved;
10448
10449       /* read_type_die already issued a complaint.  */
10450       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10451                             cu->objfile->name,
10452                             cu->header.offset,
10453                             die->offset);
10454       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10455                              message, strlen (message));
10456       xfree (message);
10457
10458       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
10459     }
10460   return this_type;
10461 }
10462
10463 static struct type *
10464 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
10465 {
10466   struct type *this_type;
10467
10468   this_type = get_die_type (die, cu);
10469   if (this_type)
10470     return this_type;
10471
10472   switch (die->tag)
10473     {
10474     case DW_TAG_class_type:
10475     case DW_TAG_interface_type:
10476     case DW_TAG_structure_type:
10477     case DW_TAG_union_type:
10478       this_type = read_structure_type (die, cu);
10479       break;
10480     case DW_TAG_enumeration_type:
10481       this_type = read_enumeration_type (die, cu);
10482       break;
10483     case DW_TAG_subprogram:
10484     case DW_TAG_subroutine_type:
10485     case DW_TAG_inlined_subroutine:
10486       this_type = read_subroutine_type (die, cu);
10487       break;
10488     case DW_TAG_array_type:
10489       this_type = read_array_type (die, cu);
10490       break;
10491     case DW_TAG_set_type:
10492       this_type = read_set_type (die, cu);
10493       break;
10494     case DW_TAG_pointer_type:
10495       this_type = read_tag_pointer_type (die, cu);
10496       break;
10497     case DW_TAG_ptr_to_member_type:
10498       this_type = read_tag_ptr_to_member_type (die, cu);
10499       break;
10500     case DW_TAG_reference_type:
10501       this_type = read_tag_reference_type (die, cu);
10502       break;
10503     case DW_TAG_const_type:
10504       this_type = read_tag_const_type (die, cu);
10505       break;
10506     case DW_TAG_volatile_type:
10507       this_type = read_tag_volatile_type (die, cu);
10508       break;
10509     case DW_TAG_string_type:
10510       this_type = read_tag_string_type (die, cu);
10511       break;
10512     case DW_TAG_typedef:
10513       this_type = read_typedef (die, cu);
10514       break;
10515     case DW_TAG_subrange_type:
10516       this_type = read_subrange_type (die, cu);
10517       break;
10518     case DW_TAG_base_type:
10519       this_type = read_base_type (die, cu);
10520       break;
10521     case DW_TAG_unspecified_type:
10522       this_type = read_unspecified_type (die, cu);
10523       break;
10524     case DW_TAG_namespace:
10525       this_type = read_namespace_type (die, cu);
10526       break;
10527     case DW_TAG_module:
10528       this_type = read_module_type (die, cu);
10529       break;
10530     default:
10531       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
10532                  dwarf_tag_name (die->tag));
10533       break;
10534     }
10535
10536   return this_type;
10537 }
10538
10539 /* Return the name of the namespace/class that DIE is defined within,
10540    or "" if we can't tell.  The caller should not xfree the result.
10541
10542    For example, if we're within the method foo() in the following
10543    code:
10544
10545    namespace N {
10546      class C {
10547        void foo () {
10548        }
10549      };
10550    }
10551
10552    then determine_prefix on foo's die will return "N::C".  */
10553
10554 static char *
10555 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
10556 {
10557   struct die_info *parent, *spec_die;
10558   struct dwarf2_cu *spec_cu;
10559   struct type *parent_type;
10560
10561   if (cu->language != language_cplus && cu->language != language_java
10562       && cu->language != language_fortran)
10563     return "";
10564
10565   /* We have to be careful in the presence of DW_AT_specification.
10566      For example, with GCC 3.4, given the code
10567
10568      namespace N {
10569        void foo() {
10570          // Definition of N::foo.
10571        }
10572      }
10573
10574      then we'll have a tree of DIEs like this:
10575
10576      1: DW_TAG_compile_unit
10577        2: DW_TAG_namespace        // N
10578          3: DW_TAG_subprogram     // declaration of N::foo
10579        4: DW_TAG_subprogram       // definition of N::foo
10580             DW_AT_specification   // refers to die #3
10581
10582      Thus, when processing die #4, we have to pretend that we're in
10583      the context of its DW_AT_specification, namely the contex of die
10584      #3.  */
10585   spec_cu = cu;
10586   spec_die = die_specification (die, &spec_cu);
10587   if (spec_die == NULL)
10588     parent = die->parent;
10589   else
10590     {
10591       parent = spec_die->parent;
10592       cu = spec_cu;
10593     }
10594
10595   if (parent == NULL)
10596     return "";
10597   else
10598     switch (parent->tag)
10599       {
10600       case DW_TAG_namespace:
10601         parent_type = read_type_die (parent, cu);
10602         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10603            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10604            Work around this problem here.  */
10605         if (cu->language == language_cplus
10606             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10607           return "";
10608         /* We give a name to even anonymous namespaces.  */
10609         return TYPE_TAG_NAME (parent_type);
10610       case DW_TAG_class_type:
10611       case DW_TAG_interface_type:
10612       case DW_TAG_structure_type:
10613       case DW_TAG_union_type:
10614       case DW_TAG_module:
10615         parent_type = read_type_die (parent, cu);
10616         if (TYPE_TAG_NAME (parent_type) != NULL)
10617           return TYPE_TAG_NAME (parent_type);
10618         else
10619           /* An anonymous structure is only allowed non-static data
10620              members; no typedefs, no member functions, et cetera.
10621              So it does not need a prefix.  */
10622           return "";
10623       default:
10624         return determine_prefix (parent, cu);
10625       }
10626 }
10627
10628 /* Return a newly-allocated string formed by concatenating PREFIX and
10629    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
10630    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
10631    perform an obconcat, otherwise allocate storage for the result.  The CU argument
10632    is used to determine the language and hence, the appropriate separator.  */
10633
10634 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
10635
10636 static char *
10637 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10638                  int physname, struct dwarf2_cu *cu)
10639 {
10640   const char *lead = "";
10641   const char *sep;
10642
10643   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10644     sep = "";
10645   else if (cu->language == language_java)
10646     sep = ".";
10647   else if (cu->language == language_fortran && physname)
10648     {
10649       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
10650          DW_AT_MIPS_linkage_name is preferred and used instead.  */
10651
10652       lead = "__";
10653       sep = "_MOD_";
10654     }
10655   else
10656     sep = "::";
10657
10658   if (prefix == NULL)
10659     prefix = "";
10660   if (suffix == NULL)
10661     suffix = "";
10662
10663   if (obs == NULL)
10664     {
10665       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
10666
10667       strcpy (retval, lead);
10668       strcat (retval, prefix);
10669       strcat (retval, sep);
10670       strcat (retval, suffix);
10671       return retval;
10672     }
10673   else
10674     {
10675       /* We have an obstack.  */
10676       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
10677     }
10678 }
10679
10680 /* Return sibling of die, NULL if no sibling.  */
10681
10682 static struct die_info *
10683 sibling_die (struct die_info *die)
10684 {
10685   return die->sibling;
10686 }
10687
10688 /* Get name of a die, return NULL if not found.  */
10689
10690 static char *
10691 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10692                           struct obstack *obstack)
10693 {
10694   if (name && cu->language == language_cplus)
10695     {
10696       char *canon_name = cp_canonicalize_string (name);
10697
10698       if (canon_name != NULL)
10699         {
10700           if (strcmp (canon_name, name) != 0)
10701             name = obsavestring (canon_name, strlen (canon_name),
10702                                  obstack);
10703           xfree (canon_name);
10704         }
10705     }
10706
10707   return name;
10708 }
10709
10710 /* Get name of a die, return NULL if not found.  */
10711
10712 static char *
10713 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
10714 {
10715   struct attribute *attr;
10716
10717   attr = dwarf2_attr (die, DW_AT_name, cu);
10718   if (!attr || !DW_STRING (attr))
10719     return NULL;
10720
10721   switch (die->tag)
10722     {
10723     case DW_TAG_compile_unit:
10724       /* Compilation units have a DW_AT_name that is a filename, not
10725          a source language identifier.  */
10726     case DW_TAG_enumeration_type:
10727     case DW_TAG_enumerator:
10728       /* These tags always have simple identifiers already; no need
10729          to canonicalize them.  */
10730       return DW_STRING (attr);
10731
10732     case DW_TAG_subprogram:
10733       /* Java constructors will all be named "<init>", so return
10734          the class name when we see this special case.  */
10735       if (cu->language == language_java
10736           && DW_STRING (attr) != NULL
10737           && strcmp (DW_STRING (attr), "<init>") == 0)
10738         {
10739           struct dwarf2_cu *spec_cu = cu;
10740           struct die_info *spec_die;
10741
10742           /* GCJ will output '<init>' for Java constructor names.
10743              For this special case, return the name of the parent class.  */
10744
10745           /* GCJ may output suprogram DIEs with AT_specification set.
10746              If so, use the name of the specified DIE.  */
10747           spec_die = die_specification (die, &spec_cu);
10748           if (spec_die != NULL)
10749             return dwarf2_name (spec_die, spec_cu);
10750
10751           do
10752             {
10753               die = die->parent;
10754               if (die->tag == DW_TAG_class_type)
10755                 return dwarf2_name (die, cu);
10756             }
10757           while (die->tag != DW_TAG_compile_unit);
10758         }
10759       break;
10760
10761     case DW_TAG_class_type:
10762     case DW_TAG_interface_type:
10763     case DW_TAG_structure_type:
10764     case DW_TAG_union_type:
10765       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10766          structures or unions.  These were of the form "._%d" in GCC 4.1,
10767          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10768          and GCC 4.4.  We work around this problem by ignoring these.  */
10769       if (strncmp (DW_STRING (attr), "._", 2) == 0
10770           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10771         return NULL;
10772       break;
10773
10774     default:
10775       break;
10776     }
10777
10778   if (!DW_STRING_IS_CANONICAL (attr))
10779     {
10780       DW_STRING (attr)
10781         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10782                                     &cu->objfile->objfile_obstack);
10783       DW_STRING_IS_CANONICAL (attr) = 1;
10784     }
10785   return DW_STRING (attr);
10786 }
10787
10788 /* Return the die that this die in an extension of, or NULL if there
10789    is none.  *EXT_CU is the CU containing DIE on input, and the CU
10790    containing the return value on output.  */
10791
10792 static struct die_info *
10793 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
10794 {
10795   struct attribute *attr;
10796
10797   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
10798   if (attr == NULL)
10799     return NULL;
10800
10801   return follow_die_ref (die, attr, ext_cu);
10802 }
10803
10804 /* Convert a DIE tag into its string name.  */
10805
10806 static char *
10807 dwarf_tag_name (unsigned tag)
10808 {
10809   switch (tag)
10810     {
10811     case DW_TAG_padding:
10812       return "DW_TAG_padding";
10813     case DW_TAG_array_type:
10814       return "DW_TAG_array_type";
10815     case DW_TAG_class_type:
10816       return "DW_TAG_class_type";
10817     case DW_TAG_entry_point:
10818       return "DW_TAG_entry_point";
10819     case DW_TAG_enumeration_type:
10820       return "DW_TAG_enumeration_type";
10821     case DW_TAG_formal_parameter:
10822       return "DW_TAG_formal_parameter";
10823     case DW_TAG_imported_declaration:
10824       return "DW_TAG_imported_declaration";
10825     case DW_TAG_label:
10826       return "DW_TAG_label";
10827     case DW_TAG_lexical_block:
10828       return "DW_TAG_lexical_block";
10829     case DW_TAG_member:
10830       return "DW_TAG_member";
10831     case DW_TAG_pointer_type:
10832       return "DW_TAG_pointer_type";
10833     case DW_TAG_reference_type:
10834       return "DW_TAG_reference_type";
10835     case DW_TAG_compile_unit:
10836       return "DW_TAG_compile_unit";
10837     case DW_TAG_string_type:
10838       return "DW_TAG_string_type";
10839     case DW_TAG_structure_type:
10840       return "DW_TAG_structure_type";
10841     case DW_TAG_subroutine_type:
10842       return "DW_TAG_subroutine_type";
10843     case DW_TAG_typedef:
10844       return "DW_TAG_typedef";
10845     case DW_TAG_union_type:
10846       return "DW_TAG_union_type";
10847     case DW_TAG_unspecified_parameters:
10848       return "DW_TAG_unspecified_parameters";
10849     case DW_TAG_variant:
10850       return "DW_TAG_variant";
10851     case DW_TAG_common_block:
10852       return "DW_TAG_common_block";
10853     case DW_TAG_common_inclusion:
10854       return "DW_TAG_common_inclusion";
10855     case DW_TAG_inheritance:
10856       return "DW_TAG_inheritance";
10857     case DW_TAG_inlined_subroutine:
10858       return "DW_TAG_inlined_subroutine";
10859     case DW_TAG_module:
10860       return "DW_TAG_module";
10861     case DW_TAG_ptr_to_member_type:
10862       return "DW_TAG_ptr_to_member_type";
10863     case DW_TAG_set_type:
10864       return "DW_TAG_set_type";
10865     case DW_TAG_subrange_type:
10866       return "DW_TAG_subrange_type";
10867     case DW_TAG_with_stmt:
10868       return "DW_TAG_with_stmt";
10869     case DW_TAG_access_declaration:
10870       return "DW_TAG_access_declaration";
10871     case DW_TAG_base_type:
10872       return "DW_TAG_base_type";
10873     case DW_TAG_catch_block:
10874       return "DW_TAG_catch_block";
10875     case DW_TAG_const_type:
10876       return "DW_TAG_const_type";
10877     case DW_TAG_constant:
10878       return "DW_TAG_constant";
10879     case DW_TAG_enumerator:
10880       return "DW_TAG_enumerator";
10881     case DW_TAG_file_type:
10882       return "DW_TAG_file_type";
10883     case DW_TAG_friend:
10884       return "DW_TAG_friend";
10885     case DW_TAG_namelist:
10886       return "DW_TAG_namelist";
10887     case DW_TAG_namelist_item:
10888       return "DW_TAG_namelist_item";
10889     case DW_TAG_packed_type:
10890       return "DW_TAG_packed_type";
10891     case DW_TAG_subprogram:
10892       return "DW_TAG_subprogram";
10893     case DW_TAG_template_type_param:
10894       return "DW_TAG_template_type_param";
10895     case DW_TAG_template_value_param:
10896       return "DW_TAG_template_value_param";
10897     case DW_TAG_thrown_type:
10898       return "DW_TAG_thrown_type";
10899     case DW_TAG_try_block:
10900       return "DW_TAG_try_block";
10901     case DW_TAG_variant_part:
10902       return "DW_TAG_variant_part";
10903     case DW_TAG_variable:
10904       return "DW_TAG_variable";
10905     case DW_TAG_volatile_type:
10906       return "DW_TAG_volatile_type";
10907     case DW_TAG_dwarf_procedure:
10908       return "DW_TAG_dwarf_procedure";
10909     case DW_TAG_restrict_type:
10910       return "DW_TAG_restrict_type";
10911     case DW_TAG_interface_type:
10912       return "DW_TAG_interface_type";
10913     case DW_TAG_namespace:
10914       return "DW_TAG_namespace";
10915     case DW_TAG_imported_module:
10916       return "DW_TAG_imported_module";
10917     case DW_TAG_unspecified_type:
10918       return "DW_TAG_unspecified_type";
10919     case DW_TAG_partial_unit:
10920       return "DW_TAG_partial_unit";
10921     case DW_TAG_imported_unit:
10922       return "DW_TAG_imported_unit";
10923     case DW_TAG_condition:
10924       return "DW_TAG_condition";
10925     case DW_TAG_shared_type:
10926       return "DW_TAG_shared_type";
10927     case DW_TAG_type_unit:
10928       return "DW_TAG_type_unit";
10929     case DW_TAG_MIPS_loop:
10930       return "DW_TAG_MIPS_loop";
10931     case DW_TAG_HP_array_descriptor:
10932       return "DW_TAG_HP_array_descriptor";
10933     case DW_TAG_format_label:
10934       return "DW_TAG_format_label";
10935     case DW_TAG_function_template:
10936       return "DW_TAG_function_template";
10937     case DW_TAG_class_template:
10938       return "DW_TAG_class_template";
10939     case DW_TAG_GNU_BINCL:
10940       return "DW_TAG_GNU_BINCL";
10941     case DW_TAG_GNU_EINCL:
10942       return "DW_TAG_GNU_EINCL";
10943     case DW_TAG_upc_shared_type:
10944       return "DW_TAG_upc_shared_type";
10945     case DW_TAG_upc_strict_type:
10946       return "DW_TAG_upc_strict_type";
10947     case DW_TAG_upc_relaxed_type:
10948       return "DW_TAG_upc_relaxed_type";
10949     case DW_TAG_PGI_kanji_type:
10950       return "DW_TAG_PGI_kanji_type";
10951     case DW_TAG_PGI_interface_block:
10952       return "DW_TAG_PGI_interface_block";
10953     default:
10954       return "DW_TAG_<unknown>";
10955     }
10956 }
10957
10958 /* Convert a DWARF attribute code into its string name.  */
10959
10960 static char *
10961 dwarf_attr_name (unsigned attr)
10962 {
10963   switch (attr)
10964     {
10965     case DW_AT_sibling:
10966       return "DW_AT_sibling";
10967     case DW_AT_location:
10968       return "DW_AT_location";
10969     case DW_AT_name:
10970       return "DW_AT_name";
10971     case DW_AT_ordering:
10972       return "DW_AT_ordering";
10973     case DW_AT_subscr_data:
10974       return "DW_AT_subscr_data";
10975     case DW_AT_byte_size:
10976       return "DW_AT_byte_size";
10977     case DW_AT_bit_offset:
10978       return "DW_AT_bit_offset";
10979     case DW_AT_bit_size:
10980       return "DW_AT_bit_size";
10981     case DW_AT_element_list:
10982       return "DW_AT_element_list";
10983     case DW_AT_stmt_list:
10984       return "DW_AT_stmt_list";
10985     case DW_AT_low_pc:
10986       return "DW_AT_low_pc";
10987     case DW_AT_high_pc:
10988       return "DW_AT_high_pc";
10989     case DW_AT_language:
10990       return "DW_AT_language";
10991     case DW_AT_member:
10992       return "DW_AT_member";
10993     case DW_AT_discr:
10994       return "DW_AT_discr";
10995     case DW_AT_discr_value:
10996       return "DW_AT_discr_value";
10997     case DW_AT_visibility:
10998       return "DW_AT_visibility";
10999     case DW_AT_import:
11000       return "DW_AT_import";
11001     case DW_AT_string_length:
11002       return "DW_AT_string_length";
11003     case DW_AT_common_reference:
11004       return "DW_AT_common_reference";
11005     case DW_AT_comp_dir:
11006       return "DW_AT_comp_dir";
11007     case DW_AT_const_value:
11008       return "DW_AT_const_value";
11009     case DW_AT_containing_type:
11010       return "DW_AT_containing_type";
11011     case DW_AT_default_value:
11012       return "DW_AT_default_value";
11013     case DW_AT_inline:
11014       return "DW_AT_inline";
11015     case DW_AT_is_optional:
11016       return "DW_AT_is_optional";
11017     case DW_AT_lower_bound:
11018       return "DW_AT_lower_bound";
11019     case DW_AT_producer:
11020       return "DW_AT_producer";
11021     case DW_AT_prototyped:
11022       return "DW_AT_prototyped";
11023     case DW_AT_return_addr:
11024       return "DW_AT_return_addr";
11025     case DW_AT_start_scope:
11026       return "DW_AT_start_scope";
11027     case DW_AT_bit_stride:
11028       return "DW_AT_bit_stride";
11029     case DW_AT_upper_bound:
11030       return "DW_AT_upper_bound";
11031     case DW_AT_abstract_origin:
11032       return "DW_AT_abstract_origin";
11033     case DW_AT_accessibility:
11034       return "DW_AT_accessibility";
11035     case DW_AT_address_class:
11036       return "DW_AT_address_class";
11037     case DW_AT_artificial:
11038       return "DW_AT_artificial";
11039     case DW_AT_base_types:
11040       return "DW_AT_base_types";
11041     case DW_AT_calling_convention:
11042       return "DW_AT_calling_convention";
11043     case DW_AT_count:
11044       return "DW_AT_count";
11045     case DW_AT_data_member_location:
11046       return "DW_AT_data_member_location";
11047     case DW_AT_decl_column:
11048       return "DW_AT_decl_column";
11049     case DW_AT_decl_file:
11050       return "DW_AT_decl_file";
11051     case DW_AT_decl_line:
11052       return "DW_AT_decl_line";
11053     case DW_AT_declaration:
11054       return "DW_AT_declaration";
11055     case DW_AT_discr_list:
11056       return "DW_AT_discr_list";
11057     case DW_AT_encoding:
11058       return "DW_AT_encoding";
11059     case DW_AT_external:
11060       return "DW_AT_external";
11061     case DW_AT_frame_base:
11062       return "DW_AT_frame_base";
11063     case DW_AT_friend:
11064       return "DW_AT_friend";
11065     case DW_AT_identifier_case:
11066       return "DW_AT_identifier_case";
11067     case DW_AT_macro_info:
11068       return "DW_AT_macro_info";
11069     case DW_AT_namelist_items:
11070       return "DW_AT_namelist_items";
11071     case DW_AT_priority:
11072       return "DW_AT_priority";
11073     case DW_AT_segment:
11074       return "DW_AT_segment";
11075     case DW_AT_specification:
11076       return "DW_AT_specification";
11077     case DW_AT_static_link:
11078       return "DW_AT_static_link";
11079     case DW_AT_type:
11080       return "DW_AT_type";
11081     case DW_AT_use_location:
11082       return "DW_AT_use_location";
11083     case DW_AT_variable_parameter:
11084       return "DW_AT_variable_parameter";
11085     case DW_AT_virtuality:
11086       return "DW_AT_virtuality";
11087     case DW_AT_vtable_elem_location:
11088       return "DW_AT_vtable_elem_location";
11089     /* DWARF 3 values.  */
11090     case DW_AT_allocated:
11091       return "DW_AT_allocated";
11092     case DW_AT_associated:
11093       return "DW_AT_associated";
11094     case DW_AT_data_location:
11095       return "DW_AT_data_location";
11096     case DW_AT_byte_stride:
11097       return "DW_AT_byte_stride";
11098     case DW_AT_entry_pc:
11099       return "DW_AT_entry_pc";
11100     case DW_AT_use_UTF8:
11101       return "DW_AT_use_UTF8";
11102     case DW_AT_extension:
11103       return "DW_AT_extension";
11104     case DW_AT_ranges:
11105       return "DW_AT_ranges";
11106     case DW_AT_trampoline:
11107       return "DW_AT_trampoline";
11108     case DW_AT_call_column:
11109       return "DW_AT_call_column";
11110     case DW_AT_call_file:
11111       return "DW_AT_call_file";
11112     case DW_AT_call_line:
11113       return "DW_AT_call_line";
11114     case DW_AT_description:
11115       return "DW_AT_description";
11116     case DW_AT_binary_scale:
11117       return "DW_AT_binary_scale";
11118     case DW_AT_decimal_scale:
11119       return "DW_AT_decimal_scale";
11120     case DW_AT_small:
11121       return "DW_AT_small";
11122     case DW_AT_decimal_sign:
11123       return "DW_AT_decimal_sign";
11124     case DW_AT_digit_count:
11125       return "DW_AT_digit_count";
11126     case DW_AT_picture_string:
11127       return "DW_AT_picture_string";
11128     case DW_AT_mutable:
11129       return "DW_AT_mutable";
11130     case DW_AT_threads_scaled:
11131       return "DW_AT_threads_scaled";
11132     case DW_AT_explicit:
11133       return "DW_AT_explicit";
11134     case DW_AT_object_pointer:
11135       return "DW_AT_object_pointer";
11136     case DW_AT_endianity:
11137       return "DW_AT_endianity";
11138     case DW_AT_elemental:
11139       return "DW_AT_elemental";
11140     case DW_AT_pure:
11141       return "DW_AT_pure";
11142     case DW_AT_recursive:
11143       return "DW_AT_recursive";
11144     /* DWARF 4 values.  */
11145     case DW_AT_signature:
11146       return "DW_AT_signature";
11147     case DW_AT_linkage_name:
11148       return "DW_AT_linkage_name";
11149     /* SGI/MIPS extensions.  */
11150 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11151     case DW_AT_MIPS_fde:
11152       return "DW_AT_MIPS_fde";
11153 #endif
11154     case DW_AT_MIPS_loop_begin:
11155       return "DW_AT_MIPS_loop_begin";
11156     case DW_AT_MIPS_tail_loop_begin:
11157       return "DW_AT_MIPS_tail_loop_begin";
11158     case DW_AT_MIPS_epilog_begin:
11159       return "DW_AT_MIPS_epilog_begin";
11160     case DW_AT_MIPS_loop_unroll_factor:
11161       return "DW_AT_MIPS_loop_unroll_factor";
11162     case DW_AT_MIPS_software_pipeline_depth:
11163       return "DW_AT_MIPS_software_pipeline_depth";
11164     case DW_AT_MIPS_linkage_name:
11165       return "DW_AT_MIPS_linkage_name";
11166     case DW_AT_MIPS_stride:
11167       return "DW_AT_MIPS_stride";
11168     case DW_AT_MIPS_abstract_name:
11169       return "DW_AT_MIPS_abstract_name";
11170     case DW_AT_MIPS_clone_origin:
11171       return "DW_AT_MIPS_clone_origin";
11172     case DW_AT_MIPS_has_inlines:
11173       return "DW_AT_MIPS_has_inlines";
11174     /* HP extensions.  */
11175 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11176     case DW_AT_HP_block_index:
11177       return "DW_AT_HP_block_index";
11178 #endif
11179     case DW_AT_HP_unmodifiable:
11180       return "DW_AT_HP_unmodifiable";
11181     case DW_AT_HP_actuals_stmt_list:
11182       return "DW_AT_HP_actuals_stmt_list";
11183     case DW_AT_HP_proc_per_section:
11184       return "DW_AT_HP_proc_per_section";
11185     case DW_AT_HP_raw_data_ptr:
11186       return "DW_AT_HP_raw_data_ptr";
11187     case DW_AT_HP_pass_by_reference:
11188       return "DW_AT_HP_pass_by_reference";
11189     case DW_AT_HP_opt_level:
11190       return "DW_AT_HP_opt_level";
11191     case DW_AT_HP_prof_version_id:
11192       return "DW_AT_HP_prof_version_id";
11193     case DW_AT_HP_opt_flags:
11194       return "DW_AT_HP_opt_flags";
11195     case DW_AT_HP_cold_region_low_pc:
11196       return "DW_AT_HP_cold_region_low_pc";
11197     case DW_AT_HP_cold_region_high_pc:
11198       return "DW_AT_HP_cold_region_high_pc";
11199     case DW_AT_HP_all_variables_modifiable:
11200       return "DW_AT_HP_all_variables_modifiable";
11201     case DW_AT_HP_linkage_name:
11202       return "DW_AT_HP_linkage_name";
11203     case DW_AT_HP_prof_flags:
11204       return "DW_AT_HP_prof_flags";
11205     /* GNU extensions.  */
11206     case DW_AT_sf_names:
11207       return "DW_AT_sf_names";
11208     case DW_AT_src_info:
11209       return "DW_AT_src_info";
11210     case DW_AT_mac_info:
11211       return "DW_AT_mac_info";
11212     case DW_AT_src_coords:
11213       return "DW_AT_src_coords";
11214     case DW_AT_body_begin:
11215       return "DW_AT_body_begin";
11216     case DW_AT_body_end:
11217       return "DW_AT_body_end";
11218     case DW_AT_GNU_vector:
11219       return "DW_AT_GNU_vector";
11220     /* VMS extensions.  */
11221     case DW_AT_VMS_rtnbeg_pd_address:
11222       return "DW_AT_VMS_rtnbeg_pd_address";
11223     /* UPC extension.  */
11224     case DW_AT_upc_threads_scaled:
11225       return "DW_AT_upc_threads_scaled";
11226     /* PGI (STMicroelectronics) extensions.  */
11227     case DW_AT_PGI_lbase:
11228       return "DW_AT_PGI_lbase";
11229     case DW_AT_PGI_soffset:
11230       return "DW_AT_PGI_soffset";
11231     case DW_AT_PGI_lstride:
11232       return "DW_AT_PGI_lstride";
11233     default:
11234       return "DW_AT_<unknown>";
11235     }
11236 }
11237
11238 /* Convert a DWARF value form code into its string name.  */
11239
11240 static char *
11241 dwarf_form_name (unsigned form)
11242 {
11243   switch (form)
11244     {
11245     case DW_FORM_addr:
11246       return "DW_FORM_addr";
11247     case DW_FORM_block2:
11248       return "DW_FORM_block2";
11249     case DW_FORM_block4:
11250       return "DW_FORM_block4";
11251     case DW_FORM_data2:
11252       return "DW_FORM_data2";
11253     case DW_FORM_data4:
11254       return "DW_FORM_data4";
11255     case DW_FORM_data8:
11256       return "DW_FORM_data8";
11257     case DW_FORM_string:
11258       return "DW_FORM_string";
11259     case DW_FORM_block:
11260       return "DW_FORM_block";
11261     case DW_FORM_block1:
11262       return "DW_FORM_block1";
11263     case DW_FORM_data1:
11264       return "DW_FORM_data1";
11265     case DW_FORM_flag:
11266       return "DW_FORM_flag";
11267     case DW_FORM_sdata:
11268       return "DW_FORM_sdata";
11269     case DW_FORM_strp:
11270       return "DW_FORM_strp";
11271     case DW_FORM_udata:
11272       return "DW_FORM_udata";
11273     case DW_FORM_ref_addr:
11274       return "DW_FORM_ref_addr";
11275     case DW_FORM_ref1:
11276       return "DW_FORM_ref1";
11277     case DW_FORM_ref2:
11278       return "DW_FORM_ref2";
11279     case DW_FORM_ref4:
11280       return "DW_FORM_ref4";
11281     case DW_FORM_ref8:
11282       return "DW_FORM_ref8";
11283     case DW_FORM_ref_udata:
11284       return "DW_FORM_ref_udata";
11285     case DW_FORM_indirect:
11286       return "DW_FORM_indirect";
11287     case DW_FORM_sec_offset:
11288       return "DW_FORM_sec_offset";
11289     case DW_FORM_exprloc:
11290       return "DW_FORM_exprloc";
11291     case DW_FORM_flag_present:
11292       return "DW_FORM_flag_present";
11293     case DW_FORM_sig8:
11294       return "DW_FORM_sig8";
11295     default:
11296       return "DW_FORM_<unknown>";
11297     }
11298 }
11299
11300 /* Convert a DWARF stack opcode into its string name.  */
11301
11302 const char *
11303 dwarf_stack_op_name (unsigned op, int def)
11304 {
11305   switch (op)
11306     {
11307     case DW_OP_addr:
11308       return "DW_OP_addr";
11309     case DW_OP_deref:
11310       return "DW_OP_deref";
11311     case DW_OP_const1u:
11312       return "DW_OP_const1u";
11313     case DW_OP_const1s:
11314       return "DW_OP_const1s";
11315     case DW_OP_const2u:
11316       return "DW_OP_const2u";
11317     case DW_OP_const2s:
11318       return "DW_OP_const2s";
11319     case DW_OP_const4u:
11320       return "DW_OP_const4u";
11321     case DW_OP_const4s:
11322       return "DW_OP_const4s";
11323     case DW_OP_const8u:
11324       return "DW_OP_const8u";
11325     case DW_OP_const8s:
11326       return "DW_OP_const8s";
11327     case DW_OP_constu:
11328       return "DW_OP_constu";
11329     case DW_OP_consts:
11330       return "DW_OP_consts";
11331     case DW_OP_dup:
11332       return "DW_OP_dup";
11333     case DW_OP_drop:
11334       return "DW_OP_drop";
11335     case DW_OP_over:
11336       return "DW_OP_over";
11337     case DW_OP_pick:
11338       return "DW_OP_pick";
11339     case DW_OP_swap:
11340       return "DW_OP_swap";
11341     case DW_OP_rot:
11342       return "DW_OP_rot";
11343     case DW_OP_xderef:
11344       return "DW_OP_xderef";
11345     case DW_OP_abs:
11346       return "DW_OP_abs";
11347     case DW_OP_and:
11348       return "DW_OP_and";
11349     case DW_OP_div:
11350       return "DW_OP_div";
11351     case DW_OP_minus:
11352       return "DW_OP_minus";
11353     case DW_OP_mod:
11354       return "DW_OP_mod";
11355     case DW_OP_mul:
11356       return "DW_OP_mul";
11357     case DW_OP_neg:
11358       return "DW_OP_neg";
11359     case DW_OP_not:
11360       return "DW_OP_not";
11361     case DW_OP_or:
11362       return "DW_OP_or";
11363     case DW_OP_plus:
11364       return "DW_OP_plus";
11365     case DW_OP_plus_uconst:
11366       return "DW_OP_plus_uconst";
11367     case DW_OP_shl:
11368       return "DW_OP_shl";
11369     case DW_OP_shr:
11370       return "DW_OP_shr";
11371     case DW_OP_shra:
11372       return "DW_OP_shra";
11373     case DW_OP_xor:
11374       return "DW_OP_xor";
11375     case DW_OP_bra:
11376       return "DW_OP_bra";
11377     case DW_OP_eq:
11378       return "DW_OP_eq";
11379     case DW_OP_ge:
11380       return "DW_OP_ge";
11381     case DW_OP_gt:
11382       return "DW_OP_gt";
11383     case DW_OP_le:
11384       return "DW_OP_le";
11385     case DW_OP_lt:
11386       return "DW_OP_lt";
11387     case DW_OP_ne:
11388       return "DW_OP_ne";
11389     case DW_OP_skip:
11390       return "DW_OP_skip";
11391     case DW_OP_lit0:
11392       return "DW_OP_lit0";
11393     case DW_OP_lit1:
11394       return "DW_OP_lit1";
11395     case DW_OP_lit2:
11396       return "DW_OP_lit2";
11397     case DW_OP_lit3:
11398       return "DW_OP_lit3";
11399     case DW_OP_lit4:
11400       return "DW_OP_lit4";
11401     case DW_OP_lit5:
11402       return "DW_OP_lit5";
11403     case DW_OP_lit6:
11404       return "DW_OP_lit6";
11405     case DW_OP_lit7:
11406       return "DW_OP_lit7";
11407     case DW_OP_lit8:
11408       return "DW_OP_lit8";
11409     case DW_OP_lit9:
11410       return "DW_OP_lit9";
11411     case DW_OP_lit10:
11412       return "DW_OP_lit10";
11413     case DW_OP_lit11:
11414       return "DW_OP_lit11";
11415     case DW_OP_lit12:
11416       return "DW_OP_lit12";
11417     case DW_OP_lit13:
11418       return "DW_OP_lit13";
11419     case DW_OP_lit14:
11420       return "DW_OP_lit14";
11421     case DW_OP_lit15:
11422       return "DW_OP_lit15";
11423     case DW_OP_lit16:
11424       return "DW_OP_lit16";
11425     case DW_OP_lit17:
11426       return "DW_OP_lit17";
11427     case DW_OP_lit18:
11428       return "DW_OP_lit18";
11429     case DW_OP_lit19:
11430       return "DW_OP_lit19";
11431     case DW_OP_lit20:
11432       return "DW_OP_lit20";
11433     case DW_OP_lit21:
11434       return "DW_OP_lit21";
11435     case DW_OP_lit22:
11436       return "DW_OP_lit22";
11437     case DW_OP_lit23:
11438       return "DW_OP_lit23";
11439     case DW_OP_lit24:
11440       return "DW_OP_lit24";
11441     case DW_OP_lit25:
11442       return "DW_OP_lit25";
11443     case DW_OP_lit26:
11444       return "DW_OP_lit26";
11445     case DW_OP_lit27:
11446       return "DW_OP_lit27";
11447     case DW_OP_lit28:
11448       return "DW_OP_lit28";
11449     case DW_OP_lit29:
11450       return "DW_OP_lit29";
11451     case DW_OP_lit30:
11452       return "DW_OP_lit30";
11453     case DW_OP_lit31:
11454       return "DW_OP_lit31";
11455     case DW_OP_reg0:
11456       return "DW_OP_reg0";
11457     case DW_OP_reg1:
11458       return "DW_OP_reg1";
11459     case DW_OP_reg2:
11460       return "DW_OP_reg2";
11461     case DW_OP_reg3:
11462       return "DW_OP_reg3";
11463     case DW_OP_reg4:
11464       return "DW_OP_reg4";
11465     case DW_OP_reg5:
11466       return "DW_OP_reg5";
11467     case DW_OP_reg6:
11468       return "DW_OP_reg6";
11469     case DW_OP_reg7:
11470       return "DW_OP_reg7";
11471     case DW_OP_reg8:
11472       return "DW_OP_reg8";
11473     case DW_OP_reg9:
11474       return "DW_OP_reg9";
11475     case DW_OP_reg10:
11476       return "DW_OP_reg10";
11477     case DW_OP_reg11:
11478       return "DW_OP_reg11";
11479     case DW_OP_reg12:
11480       return "DW_OP_reg12";
11481     case DW_OP_reg13:
11482       return "DW_OP_reg13";
11483     case DW_OP_reg14:
11484       return "DW_OP_reg14";
11485     case DW_OP_reg15:
11486       return "DW_OP_reg15";
11487     case DW_OP_reg16:
11488       return "DW_OP_reg16";
11489     case DW_OP_reg17:
11490       return "DW_OP_reg17";
11491     case DW_OP_reg18:
11492       return "DW_OP_reg18";
11493     case DW_OP_reg19:
11494       return "DW_OP_reg19";
11495     case DW_OP_reg20:
11496       return "DW_OP_reg20";
11497     case DW_OP_reg21:
11498       return "DW_OP_reg21";
11499     case DW_OP_reg22:
11500       return "DW_OP_reg22";
11501     case DW_OP_reg23:
11502       return "DW_OP_reg23";
11503     case DW_OP_reg24:
11504       return "DW_OP_reg24";
11505     case DW_OP_reg25:
11506       return "DW_OP_reg25";
11507     case DW_OP_reg26:
11508       return "DW_OP_reg26";
11509     case DW_OP_reg27:
11510       return "DW_OP_reg27";
11511     case DW_OP_reg28:
11512       return "DW_OP_reg28";
11513     case DW_OP_reg29:
11514       return "DW_OP_reg29";
11515     case DW_OP_reg30:
11516       return "DW_OP_reg30";
11517     case DW_OP_reg31:
11518       return "DW_OP_reg31";
11519     case DW_OP_breg0:
11520       return "DW_OP_breg0";
11521     case DW_OP_breg1:
11522       return "DW_OP_breg1";
11523     case DW_OP_breg2:
11524       return "DW_OP_breg2";
11525     case DW_OP_breg3:
11526       return "DW_OP_breg3";
11527     case DW_OP_breg4:
11528       return "DW_OP_breg4";
11529     case DW_OP_breg5:
11530       return "DW_OP_breg5";
11531     case DW_OP_breg6:
11532       return "DW_OP_breg6";
11533     case DW_OP_breg7:
11534       return "DW_OP_breg7";
11535     case DW_OP_breg8:
11536       return "DW_OP_breg8";
11537     case DW_OP_breg9:
11538       return "DW_OP_breg9";
11539     case DW_OP_breg10:
11540       return "DW_OP_breg10";
11541     case DW_OP_breg11:
11542       return "DW_OP_breg11";
11543     case DW_OP_breg12:
11544       return "DW_OP_breg12";
11545     case DW_OP_breg13:
11546       return "DW_OP_breg13";
11547     case DW_OP_breg14:
11548       return "DW_OP_breg14";
11549     case DW_OP_breg15:
11550       return "DW_OP_breg15";
11551     case DW_OP_breg16:
11552       return "DW_OP_breg16";
11553     case DW_OP_breg17:
11554       return "DW_OP_breg17";
11555     case DW_OP_breg18:
11556       return "DW_OP_breg18";
11557     case DW_OP_breg19:
11558       return "DW_OP_breg19";
11559     case DW_OP_breg20:
11560       return "DW_OP_breg20";
11561     case DW_OP_breg21:
11562       return "DW_OP_breg21";
11563     case DW_OP_breg22:
11564       return "DW_OP_breg22";
11565     case DW_OP_breg23:
11566       return "DW_OP_breg23";
11567     case DW_OP_breg24:
11568       return "DW_OP_breg24";
11569     case DW_OP_breg25:
11570       return "DW_OP_breg25";
11571     case DW_OP_breg26:
11572       return "DW_OP_breg26";
11573     case DW_OP_breg27:
11574       return "DW_OP_breg27";
11575     case DW_OP_breg28:
11576       return "DW_OP_breg28";
11577     case DW_OP_breg29:
11578       return "DW_OP_breg29";
11579     case DW_OP_breg30:
11580       return "DW_OP_breg30";
11581     case DW_OP_breg31:
11582       return "DW_OP_breg31";
11583     case DW_OP_regx:
11584       return "DW_OP_regx";
11585     case DW_OP_fbreg:
11586       return "DW_OP_fbreg";
11587     case DW_OP_bregx:
11588       return "DW_OP_bregx";
11589     case DW_OP_piece:
11590       return "DW_OP_piece";
11591     case DW_OP_deref_size:
11592       return "DW_OP_deref_size";
11593     case DW_OP_xderef_size:
11594       return "DW_OP_xderef_size";
11595     case DW_OP_nop:
11596       return "DW_OP_nop";
11597     /* DWARF 3 extensions.  */
11598     case DW_OP_push_object_address:
11599       return "DW_OP_push_object_address";
11600     case DW_OP_call2:
11601       return "DW_OP_call2";
11602     case DW_OP_call4:
11603       return "DW_OP_call4";
11604     case DW_OP_call_ref:
11605       return "DW_OP_call_ref";
11606     case DW_OP_form_tls_address:
11607       return "DW_OP_form_tls_address";
11608     case DW_OP_call_frame_cfa:
11609       return "DW_OP_call_frame_cfa";
11610     case DW_OP_bit_piece:
11611       return "DW_OP_bit_piece";
11612     /* DWARF 4 extensions.  */
11613     case DW_OP_implicit_value:
11614       return "DW_OP_implicit_value";
11615     case DW_OP_stack_value:
11616       return "DW_OP_stack_value";
11617     /* GNU extensions.  */
11618     case DW_OP_GNU_push_tls_address:
11619       return "DW_OP_GNU_push_tls_address";
11620     case DW_OP_GNU_uninit:
11621       return "DW_OP_GNU_uninit";
11622     default:
11623       return def ? "OP_<unknown>" : NULL;
11624     }
11625 }
11626
11627 static char *
11628 dwarf_bool_name (unsigned mybool)
11629 {
11630   if (mybool)
11631     return "TRUE";
11632   else
11633     return "FALSE";
11634 }
11635
11636 /* Convert a DWARF type code into its string name.  */
11637
11638 static char *
11639 dwarf_type_encoding_name (unsigned enc)
11640 {
11641   switch (enc)
11642     {
11643     case DW_ATE_void:
11644       return "DW_ATE_void";
11645     case DW_ATE_address:
11646       return "DW_ATE_address";
11647     case DW_ATE_boolean:
11648       return "DW_ATE_boolean";
11649     case DW_ATE_complex_float:
11650       return "DW_ATE_complex_float";
11651     case DW_ATE_float:
11652       return "DW_ATE_float";
11653     case DW_ATE_signed:
11654       return "DW_ATE_signed";
11655     case DW_ATE_signed_char:
11656       return "DW_ATE_signed_char";
11657     case DW_ATE_unsigned:
11658       return "DW_ATE_unsigned";
11659     case DW_ATE_unsigned_char:
11660       return "DW_ATE_unsigned_char";
11661     /* DWARF 3.  */
11662     case DW_ATE_imaginary_float:
11663       return "DW_ATE_imaginary_float";
11664     case DW_ATE_packed_decimal:
11665       return "DW_ATE_packed_decimal";
11666     case DW_ATE_numeric_string:
11667       return "DW_ATE_numeric_string";
11668     case DW_ATE_edited:
11669       return "DW_ATE_edited";
11670     case DW_ATE_signed_fixed:
11671       return "DW_ATE_signed_fixed";
11672     case DW_ATE_unsigned_fixed:
11673       return "DW_ATE_unsigned_fixed";
11674     case DW_ATE_decimal_float:
11675       return "DW_ATE_decimal_float";
11676     /* DWARF 4.  */
11677     case DW_ATE_UTF:
11678       return "DW_ATE_UTF";
11679     /* HP extensions.  */
11680     case DW_ATE_HP_float80:
11681       return "DW_ATE_HP_float80";
11682     case DW_ATE_HP_complex_float80:
11683       return "DW_ATE_HP_complex_float80";
11684     case DW_ATE_HP_float128:
11685       return "DW_ATE_HP_float128";
11686     case DW_ATE_HP_complex_float128:
11687       return "DW_ATE_HP_complex_float128";
11688     case DW_ATE_HP_floathpintel:
11689       return "DW_ATE_HP_floathpintel";
11690     case DW_ATE_HP_imaginary_float80:
11691       return "DW_ATE_HP_imaginary_float80";
11692     case DW_ATE_HP_imaginary_float128:
11693       return "DW_ATE_HP_imaginary_float128";
11694     default:
11695       return "DW_ATE_<unknown>";
11696     }
11697 }
11698
11699 /* Convert a DWARF call frame info operation to its string name. */
11700
11701 #if 0
11702 static char *
11703 dwarf_cfi_name (unsigned cfi_opc)
11704 {
11705   switch (cfi_opc)
11706     {
11707     case DW_CFA_advance_loc:
11708       return "DW_CFA_advance_loc";
11709     case DW_CFA_offset:
11710       return "DW_CFA_offset";
11711     case DW_CFA_restore:
11712       return "DW_CFA_restore";
11713     case DW_CFA_nop:
11714       return "DW_CFA_nop";
11715     case DW_CFA_set_loc:
11716       return "DW_CFA_set_loc";
11717     case DW_CFA_advance_loc1:
11718       return "DW_CFA_advance_loc1";
11719     case DW_CFA_advance_loc2:
11720       return "DW_CFA_advance_loc2";
11721     case DW_CFA_advance_loc4:
11722       return "DW_CFA_advance_loc4";
11723     case DW_CFA_offset_extended:
11724       return "DW_CFA_offset_extended";
11725     case DW_CFA_restore_extended:
11726       return "DW_CFA_restore_extended";
11727     case DW_CFA_undefined:
11728       return "DW_CFA_undefined";
11729     case DW_CFA_same_value:
11730       return "DW_CFA_same_value";
11731     case DW_CFA_register:
11732       return "DW_CFA_register";
11733     case DW_CFA_remember_state:
11734       return "DW_CFA_remember_state";
11735     case DW_CFA_restore_state:
11736       return "DW_CFA_restore_state";
11737     case DW_CFA_def_cfa:
11738       return "DW_CFA_def_cfa";
11739     case DW_CFA_def_cfa_register:
11740       return "DW_CFA_def_cfa_register";
11741     case DW_CFA_def_cfa_offset:
11742       return "DW_CFA_def_cfa_offset";
11743     /* DWARF 3.  */
11744     case DW_CFA_def_cfa_expression:
11745       return "DW_CFA_def_cfa_expression";
11746     case DW_CFA_expression:
11747       return "DW_CFA_expression";
11748     case DW_CFA_offset_extended_sf:
11749       return "DW_CFA_offset_extended_sf";
11750     case DW_CFA_def_cfa_sf:
11751       return "DW_CFA_def_cfa_sf";
11752     case DW_CFA_def_cfa_offset_sf:
11753       return "DW_CFA_def_cfa_offset_sf";
11754     case DW_CFA_val_offset:
11755       return "DW_CFA_val_offset";
11756     case DW_CFA_val_offset_sf:
11757       return "DW_CFA_val_offset_sf";
11758     case DW_CFA_val_expression:
11759       return "DW_CFA_val_expression";
11760     /* SGI/MIPS specific.  */
11761     case DW_CFA_MIPS_advance_loc8:
11762       return "DW_CFA_MIPS_advance_loc8";
11763     /* GNU extensions.  */
11764     case DW_CFA_GNU_window_save:
11765       return "DW_CFA_GNU_window_save";
11766     case DW_CFA_GNU_args_size:
11767       return "DW_CFA_GNU_args_size";
11768     case DW_CFA_GNU_negative_offset_extended:
11769       return "DW_CFA_GNU_negative_offset_extended";
11770     default:
11771       return "DW_CFA_<unknown>";
11772     }
11773 }
11774 #endif
11775
11776 static void
11777 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
11778 {
11779   unsigned int i;
11780
11781   print_spaces (indent, f);
11782   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
11783            dwarf_tag_name (die->tag), die->abbrev, die->offset);
11784
11785   if (die->parent != NULL)
11786     {
11787       print_spaces (indent, f);
11788       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
11789                           die->parent->offset);
11790     }
11791
11792   print_spaces (indent, f);
11793   fprintf_unfiltered (f, "  has children: %s\n",
11794            dwarf_bool_name (die->child != NULL));
11795
11796   print_spaces (indent, f);
11797   fprintf_unfiltered (f, "  attributes:\n");
11798
11799   for (i = 0; i < die->num_attrs; ++i)
11800     {
11801       print_spaces (indent, f);
11802       fprintf_unfiltered (f, "    %s (%s) ",
11803                dwarf_attr_name (die->attrs[i].name),
11804                dwarf_form_name (die->attrs[i].form));
11805
11806       switch (die->attrs[i].form)
11807         {
11808         case DW_FORM_ref_addr:
11809         case DW_FORM_addr:
11810           fprintf_unfiltered (f, "address: ");
11811           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
11812           break;
11813         case DW_FORM_block2:
11814         case DW_FORM_block4:
11815         case DW_FORM_block:
11816         case DW_FORM_block1:
11817           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
11818           break;
11819         case DW_FORM_exprloc:
11820           fprintf_unfiltered (f, "expression: size %u",
11821                               DW_BLOCK (&die->attrs[i])->size);
11822           break;
11823         case DW_FORM_ref1:
11824         case DW_FORM_ref2:
11825         case DW_FORM_ref4:
11826           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
11827                               (long) (DW_ADDR (&die->attrs[i])));
11828           break;
11829         case DW_FORM_data1:
11830         case DW_FORM_data2:
11831         case DW_FORM_data4:
11832         case DW_FORM_data8:
11833         case DW_FORM_udata:
11834         case DW_FORM_sdata:
11835           fprintf_unfiltered (f, "constant: %s",
11836                               pulongest (DW_UNSND (&die->attrs[i])));
11837           break;
11838         case DW_FORM_sec_offset:
11839           fprintf_unfiltered (f, "section offset: %s",
11840                               pulongest (DW_UNSND (&die->attrs[i])));
11841           break;
11842         case DW_FORM_sig8:
11843           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
11844             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
11845                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
11846           else
11847             fprintf_unfiltered (f, "signatured type, offset: unknown");
11848           break;
11849         case DW_FORM_string:
11850         case DW_FORM_strp:
11851           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
11852                    DW_STRING (&die->attrs[i])
11853                    ? DW_STRING (&die->attrs[i]) : "",
11854                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
11855           break;
11856         case DW_FORM_flag:
11857           if (DW_UNSND (&die->attrs[i]))
11858             fprintf_unfiltered (f, "flag: TRUE");
11859           else
11860             fprintf_unfiltered (f, "flag: FALSE");
11861           break;
11862         case DW_FORM_flag_present:
11863           fprintf_unfiltered (f, "flag: TRUE");
11864           break;
11865         case DW_FORM_indirect:
11866           /* the reader will have reduced the indirect form to
11867              the "base form" so this form should not occur */
11868           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
11869           break;
11870         default:
11871           fprintf_unfiltered (f, "unsupported attribute form: %d.",
11872                    die->attrs[i].form);
11873           break;
11874         }
11875       fprintf_unfiltered (f, "\n");
11876     }
11877 }
11878
11879 static void
11880 dump_die_for_error (struct die_info *die)
11881 {
11882   dump_die_shallow (gdb_stderr, 0, die);
11883 }
11884
11885 static void
11886 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
11887 {
11888   int indent = level * 4;
11889
11890   gdb_assert (die != NULL);
11891
11892   if (level >= max_level)
11893     return;
11894
11895   dump_die_shallow (f, indent, die);
11896
11897   if (die->child != NULL)
11898     {
11899       print_spaces (indent, f);
11900       fprintf_unfiltered (f, "  Children:");
11901       if (level + 1 < max_level)
11902         {
11903           fprintf_unfiltered (f, "\n");
11904           dump_die_1 (f, level + 1, max_level, die->child);
11905         }
11906       else
11907         {
11908           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
11909         }
11910     }
11911
11912   if (die->sibling != NULL && level > 0)
11913     {
11914       dump_die_1 (f, level, max_level, die->sibling);
11915     }
11916 }
11917
11918 /* This is called from the pdie macro in gdbinit.in.
11919    It's not static so gcc will keep a copy callable from gdb.  */
11920
11921 void
11922 dump_die (struct die_info *die, int max_level)
11923 {
11924   dump_die_1 (gdb_stdlog, 0, max_level, die);
11925 }
11926
11927 static void
11928 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
11929 {
11930   void **slot;
11931
11932   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
11933
11934   *slot = die;
11935 }
11936
11937 static int
11938 is_ref_attr (struct attribute *attr)
11939 {
11940   switch (attr->form)
11941     {
11942     case DW_FORM_ref_addr:
11943     case DW_FORM_ref1:
11944     case DW_FORM_ref2:
11945     case DW_FORM_ref4:
11946     case DW_FORM_ref8:
11947     case DW_FORM_ref_udata:
11948       return 1;
11949     default:
11950       return 0;
11951     }
11952 }
11953
11954 static unsigned int
11955 dwarf2_get_ref_die_offset (struct attribute *attr)
11956 {
11957   if (is_ref_attr (attr))
11958     return DW_ADDR (attr);
11959
11960   complaint (&symfile_complaints,
11961              _("unsupported die ref attribute form: '%s'"),
11962              dwarf_form_name (attr->form));
11963   return 0;
11964 }
11965
11966 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
11967  * the value held by the attribute is not constant.  */
11968
11969 static LONGEST
11970 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
11971 {
11972   if (attr->form == DW_FORM_sdata)
11973     return DW_SND (attr);
11974   else if (attr->form == DW_FORM_udata
11975            || attr->form == DW_FORM_data1
11976            || attr->form == DW_FORM_data2
11977            || attr->form == DW_FORM_data4
11978            || attr->form == DW_FORM_data8)
11979     return DW_UNSND (attr);
11980   else
11981     {
11982       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
11983                  dwarf_form_name (attr->form));
11984       return default_value;
11985     }
11986 }
11987
11988 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
11989    unit and add it to our queue.
11990    The result is non-zero if PER_CU was queued, otherwise the result is zero
11991    meaning either PER_CU is already queued or it is already loaded.  */
11992
11993 static int
11994 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
11995                        struct dwarf2_per_cu_data *per_cu)
11996 {
11997   /* Mark the dependence relation so that we don't flush PER_CU
11998      too early.  */
11999   dwarf2_add_dependence (this_cu, per_cu);
12000
12001   /* If it's already on the queue, we have nothing to do.  */
12002   if (per_cu->queued)
12003     return 0;
12004
12005   /* If the compilation unit is already loaded, just mark it as
12006      used.  */
12007   if (per_cu->cu != NULL)
12008     {
12009       per_cu->cu->last_used = 0;
12010       return 0;
12011     }
12012
12013   /* Add it to the queue.  */
12014   queue_comp_unit (per_cu, this_cu->objfile);
12015
12016   return 1;
12017 }
12018
12019 /* Follow reference or signature attribute ATTR of SRC_DIE.
12020    On entry *REF_CU is the CU of SRC_DIE.
12021    On exit *REF_CU is the CU of the result.  */
12022
12023 static struct die_info *
12024 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12025                        struct dwarf2_cu **ref_cu)
12026 {
12027   struct die_info *die;
12028
12029   if (is_ref_attr (attr))
12030     die = follow_die_ref (src_die, attr, ref_cu);
12031   else if (attr->form == DW_FORM_sig8)
12032     die = follow_die_sig (src_die, attr, ref_cu);
12033   else
12034     {
12035       dump_die_for_error (src_die);
12036       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12037              (*ref_cu)->objfile->name);
12038     }
12039
12040   return die;
12041 }
12042
12043 /* Follow reference OFFSET.
12044    On entry *REF_CU is the CU of source DIE referencing OFFSET.
12045    On exit *REF_CU is the CU of the result.  */
12046
12047 static struct die_info *
12048 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12049 {
12050   struct die_info temp_die;
12051   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12052
12053   gdb_assert (cu->per_cu != NULL);
12054
12055   if (cu->per_cu->from_debug_types)
12056     {
12057       /* .debug_types CUs cannot reference anything outside their CU.
12058          If they need to, they have to reference a signatured type via
12059          DW_FORM_sig8.  */
12060       if (! offset_in_cu_p (&cu->header, offset))
12061         return NULL;
12062       target_cu = cu;
12063     }
12064   else if (! offset_in_cu_p (&cu->header, offset))
12065     {
12066       struct dwarf2_per_cu_data *per_cu;
12067
12068       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12069
12070       /* If necessary, add it to the queue and load its DIEs.  */
12071       if (maybe_queue_comp_unit (cu, per_cu))
12072         load_full_comp_unit (per_cu, cu->objfile);
12073
12074       target_cu = per_cu->cu;
12075     }
12076   else
12077     target_cu = cu;
12078
12079   *ref_cu = target_cu;
12080   temp_die.offset = offset;
12081   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12082 }
12083
12084 /* Follow reference attribute ATTR of SRC_DIE.
12085    On entry *REF_CU is the CU of SRC_DIE.
12086    On exit *REF_CU is the CU of the result.  */
12087
12088 static struct die_info *
12089 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12090                 struct dwarf2_cu **ref_cu)
12091 {
12092   unsigned int offset = dwarf2_get_ref_die_offset (attr);
12093   struct dwarf2_cu *cu = *ref_cu;
12094   struct die_info *die;
12095
12096   die = follow_die_offset (offset, ref_cu);
12097   if (!die)
12098     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12099            "at 0x%x [in module %s]"),
12100            offset, src_die->offset, cu->objfile->name);
12101
12102   return die;
12103 }
12104
12105 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
12106    value is intended for DW_OP_call*.  */
12107
12108 struct dwarf2_locexpr_baton
12109 dwarf2_fetch_die_location_block (unsigned int offset,
12110                                  struct dwarf2_per_cu_data *per_cu)
12111 {
12112   struct dwarf2_cu *cu = per_cu->cu;
12113   struct die_info *die;
12114   struct attribute *attr;
12115   struct dwarf2_locexpr_baton retval;
12116
12117   die = follow_die_offset (offset, &cu);
12118   if (!die)
12119     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12120            offset, per_cu->cu->objfile->name);
12121
12122   attr = dwarf2_attr (die, DW_AT_location, cu);
12123   if (!attr)
12124     {
12125       /* DWARF: "If there is no such attribute, then there is no effect.".  */
12126
12127       retval.data = NULL;
12128       retval.size = 0;
12129     }
12130   else
12131     {
12132       if (!attr_form_is_block (attr))
12133         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12134                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12135                offset, per_cu->cu->objfile->name);
12136
12137       retval.data = DW_BLOCK (attr)->data;
12138       retval.size = DW_BLOCK (attr)->size;
12139     }
12140   retval.per_cu = cu->per_cu;
12141   return retval;
12142 }
12143
12144 /* Follow the signature attribute ATTR in SRC_DIE.
12145    On entry *REF_CU is the CU of SRC_DIE.
12146    On exit *REF_CU is the CU of the result.  */
12147
12148 static struct die_info *
12149 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12150                 struct dwarf2_cu **ref_cu)
12151 {
12152   struct objfile *objfile = (*ref_cu)->objfile;
12153   struct die_info temp_die;
12154   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12155   struct dwarf2_cu *sig_cu;
12156   struct die_info *die;
12157
12158   /* sig_type will be NULL if the signatured type is missing from
12159      the debug info.  */
12160   if (sig_type == NULL)
12161     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12162              "at 0x%x [in module %s]"),
12163            src_die->offset, objfile->name);
12164
12165   /* If necessary, add it to the queue and load its DIEs.  */
12166
12167   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12168     read_signatured_type (objfile, sig_type);
12169
12170   gdb_assert (sig_type->per_cu.cu != NULL);
12171
12172   sig_cu = sig_type->per_cu.cu;
12173   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12174   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12175   if (die)
12176     {
12177       *ref_cu = sig_cu;
12178       return die;
12179     }
12180
12181   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12182          "at 0x%x [in module %s]"),
12183          sig_type->type_offset, src_die->offset, objfile->name);
12184 }
12185
12186 /* Given an offset of a signatured type, return its signatured_type.  */
12187
12188 static struct signatured_type *
12189 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12190 {
12191   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12192   unsigned int length, initial_length_size;
12193   unsigned int sig_offset;
12194   struct signatured_type find_entry, *type_sig;
12195
12196   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12197   sig_offset = (initial_length_size
12198                 + 2 /*version*/
12199                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12200                 + 1 /*address_size*/);
12201   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12202   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12203
12204   /* This is only used to lookup previously recorded types.
12205      If we didn't find it, it's our bug.  */
12206   gdb_assert (type_sig != NULL);
12207   gdb_assert (offset == type_sig->offset);
12208
12209   return type_sig;
12210 }
12211
12212 /* Read in signatured type at OFFSET and build its CU and die(s).  */
12213
12214 static void
12215 read_signatured_type_at_offset (struct objfile *objfile,
12216                                 unsigned int offset)
12217 {
12218   struct signatured_type *type_sig;
12219
12220   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12221
12222   /* We have the section offset, but we need the signature to do the
12223      hash table lookup.  */
12224   type_sig = lookup_signatured_type_at_offset (objfile, offset);
12225
12226   gdb_assert (type_sig->per_cu.cu == NULL);
12227
12228   read_signatured_type (objfile, type_sig);
12229
12230   gdb_assert (type_sig->per_cu.cu != NULL);
12231 }
12232
12233 /* Read in a signatured type and build its CU and DIEs.  */
12234
12235 static void
12236 read_signatured_type (struct objfile *objfile,
12237                       struct signatured_type *type_sig)
12238 {
12239   gdb_byte *types_ptr;
12240   struct die_reader_specs reader_specs;
12241   struct dwarf2_cu *cu;
12242   ULONGEST signature;
12243   struct cleanup *back_to, *free_cu_cleanup;
12244   struct attribute *attr;
12245
12246   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12247   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12248
12249   gdb_assert (type_sig->per_cu.cu == NULL);
12250
12251   cu = xmalloc (sizeof (struct dwarf2_cu));
12252   memset (cu, 0, sizeof (struct dwarf2_cu));
12253   obstack_init (&cu->comp_unit_obstack);
12254   cu->objfile = objfile;
12255   type_sig->per_cu.cu = cu;
12256   cu->per_cu = &type_sig->per_cu;
12257
12258   /* If an error occurs while loading, release our storage.  */
12259   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12260
12261   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12262                                         types_ptr, objfile->obfd);
12263   gdb_assert (signature == type_sig->signature);
12264
12265   cu->die_hash
12266     = htab_create_alloc_ex (cu->header.length / 12,
12267                             die_hash,
12268                             die_eq,
12269                             NULL,
12270                             &cu->comp_unit_obstack,
12271                             hashtab_obstack_allocate,
12272                             dummy_obstack_deallocate);
12273
12274   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12275   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12276
12277   init_cu_die_reader (&reader_specs, cu);
12278
12279   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12280                                     NULL /*parent*/);
12281
12282   /* We try not to read any attributes in this function, because not
12283      all objfiles needed for references have been loaded yet, and symbol
12284      table processing isn't initialized.  But we have to set the CU language,
12285      or we won't be able to build types correctly.  */
12286   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12287   if (attr)
12288     set_cu_language (DW_UNSND (attr), cu);
12289   else
12290     set_cu_language (language_minimal, cu);
12291
12292   do_cleanups (back_to);
12293
12294   /* We've successfully allocated this compilation unit.  Let our caller
12295      clean it up when finished with it.  */
12296   discard_cleanups (free_cu_cleanup);
12297
12298   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12299   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12300 }
12301
12302 /* Decode simple location descriptions.
12303    Given a pointer to a dwarf block that defines a location, compute
12304    the location and return the value.
12305
12306    NOTE drow/2003-11-18: This function is called in two situations
12307    now: for the address of static or global variables (partial symbols
12308    only) and for offsets into structures which are expected to be
12309    (more or less) constant.  The partial symbol case should go away,
12310    and only the constant case should remain.  That will let this
12311    function complain more accurately.  A few special modes are allowed
12312    without complaint for global variables (for instance, global
12313    register values and thread-local values).
12314
12315    A location description containing no operations indicates that the
12316    object is optimized out.  The return value is 0 for that case.
12317    FIXME drow/2003-11-16: No callers check for this case any more; soon all
12318    callers will only want a very basic result and this can become a
12319    complaint.
12320
12321    Note that stack[0] is unused except as a default error return.
12322    Note that stack overflow is not yet handled.  */
12323
12324 static CORE_ADDR
12325 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12326 {
12327   struct objfile *objfile = cu->objfile;
12328   int i;
12329   int size = blk->size;
12330   gdb_byte *data = blk->data;
12331   CORE_ADDR stack[64];
12332   int stacki;
12333   unsigned int bytes_read, unsnd;
12334   gdb_byte op;
12335
12336   i = 0;
12337   stacki = 0;
12338   stack[stacki] = 0;
12339
12340   while (i < size)
12341     {
12342       op = data[i++];
12343       switch (op)
12344         {
12345         case DW_OP_lit0:
12346         case DW_OP_lit1:
12347         case DW_OP_lit2:
12348         case DW_OP_lit3:
12349         case DW_OP_lit4:
12350         case DW_OP_lit5:
12351         case DW_OP_lit6:
12352         case DW_OP_lit7:
12353         case DW_OP_lit8:
12354         case DW_OP_lit9:
12355         case DW_OP_lit10:
12356         case DW_OP_lit11:
12357         case DW_OP_lit12:
12358         case DW_OP_lit13:
12359         case DW_OP_lit14:
12360         case DW_OP_lit15:
12361         case DW_OP_lit16:
12362         case DW_OP_lit17:
12363         case DW_OP_lit18:
12364         case DW_OP_lit19:
12365         case DW_OP_lit20:
12366         case DW_OP_lit21:
12367         case DW_OP_lit22:
12368         case DW_OP_lit23:
12369         case DW_OP_lit24:
12370         case DW_OP_lit25:
12371         case DW_OP_lit26:
12372         case DW_OP_lit27:
12373         case DW_OP_lit28:
12374         case DW_OP_lit29:
12375         case DW_OP_lit30:
12376         case DW_OP_lit31:
12377           stack[++stacki] = op - DW_OP_lit0;
12378           break;
12379
12380         case DW_OP_reg0:
12381         case DW_OP_reg1:
12382         case DW_OP_reg2:
12383         case DW_OP_reg3:
12384         case DW_OP_reg4:
12385         case DW_OP_reg5:
12386         case DW_OP_reg6:
12387         case DW_OP_reg7:
12388         case DW_OP_reg8:
12389         case DW_OP_reg9:
12390         case DW_OP_reg10:
12391         case DW_OP_reg11:
12392         case DW_OP_reg12:
12393         case DW_OP_reg13:
12394         case DW_OP_reg14:
12395         case DW_OP_reg15:
12396         case DW_OP_reg16:
12397         case DW_OP_reg17:
12398         case DW_OP_reg18:
12399         case DW_OP_reg19:
12400         case DW_OP_reg20:
12401         case DW_OP_reg21:
12402         case DW_OP_reg22:
12403         case DW_OP_reg23:
12404         case DW_OP_reg24:
12405         case DW_OP_reg25:
12406         case DW_OP_reg26:
12407         case DW_OP_reg27:
12408         case DW_OP_reg28:
12409         case DW_OP_reg29:
12410         case DW_OP_reg30:
12411         case DW_OP_reg31:
12412           stack[++stacki] = op - DW_OP_reg0;
12413           if (i < size)
12414             dwarf2_complex_location_expr_complaint ();
12415           break;
12416
12417         case DW_OP_regx:
12418           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12419           i += bytes_read;
12420           stack[++stacki] = unsnd;
12421           if (i < size)
12422             dwarf2_complex_location_expr_complaint ();
12423           break;
12424
12425         case DW_OP_addr:
12426           stack[++stacki] = read_address (objfile->obfd, &data[i],
12427                                           cu, &bytes_read);
12428           i += bytes_read;
12429           break;
12430
12431         case DW_OP_const1u:
12432           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12433           i += 1;
12434           break;
12435
12436         case DW_OP_const1s:
12437           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12438           i += 1;
12439           break;
12440
12441         case DW_OP_const2u:
12442           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12443           i += 2;
12444           break;
12445
12446         case DW_OP_const2s:
12447           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12448           i += 2;
12449           break;
12450
12451         case DW_OP_const4u:
12452           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12453           i += 4;
12454           break;
12455
12456         case DW_OP_const4s:
12457           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12458           i += 4;
12459           break;
12460
12461         case DW_OP_constu:
12462           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
12463                                                   &bytes_read);
12464           i += bytes_read;
12465           break;
12466
12467         case DW_OP_consts:
12468           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12469           i += bytes_read;
12470           break;
12471
12472         case DW_OP_dup:
12473           stack[stacki + 1] = stack[stacki];
12474           stacki++;
12475           break;
12476
12477         case DW_OP_plus:
12478           stack[stacki - 1] += stack[stacki];
12479           stacki--;
12480           break;
12481
12482         case DW_OP_plus_uconst:
12483           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12484           i += bytes_read;
12485           break;
12486
12487         case DW_OP_minus:
12488           stack[stacki - 1] -= stack[stacki];
12489           stacki--;
12490           break;
12491
12492         case DW_OP_deref:
12493           /* If we're not the last op, then we definitely can't encode
12494              this using GDB's address_class enum.  This is valid for partial
12495              global symbols, although the variable's address will be bogus
12496              in the psymtab.  */
12497           if (i < size)
12498             dwarf2_complex_location_expr_complaint ();
12499           break;
12500
12501         case DW_OP_GNU_push_tls_address:
12502           /* The top of the stack has the offset from the beginning
12503              of the thread control block at which the variable is located.  */
12504           /* Nothing should follow this operator, so the top of stack would
12505              be returned.  */
12506           /* This is valid for partial global symbols, but the variable's
12507              address will be bogus in the psymtab.  */
12508           if (i < size)
12509             dwarf2_complex_location_expr_complaint ();
12510           break;
12511
12512         case DW_OP_GNU_uninit:
12513           break;
12514
12515         default:
12516           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
12517                      dwarf_stack_op_name (op, 1));
12518           return (stack[stacki]);
12519         }
12520     }
12521   return (stack[stacki]);
12522 }
12523
12524 /* memory allocation interface */
12525
12526 static struct dwarf_block *
12527 dwarf_alloc_block (struct dwarf2_cu *cu)
12528 {
12529   struct dwarf_block *blk;
12530
12531   blk = (struct dwarf_block *)
12532     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
12533   return (blk);
12534 }
12535
12536 static struct abbrev_info *
12537 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
12538 {
12539   struct abbrev_info *abbrev;
12540
12541   abbrev = (struct abbrev_info *)
12542     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
12543   memset (abbrev, 0, sizeof (struct abbrev_info));
12544   return (abbrev);
12545 }
12546
12547 static struct die_info *
12548 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
12549 {
12550   struct die_info *die;
12551   size_t size = sizeof (struct die_info);
12552
12553   if (num_attrs > 1)
12554     size += (num_attrs - 1) * sizeof (struct attribute);
12555
12556   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
12557   memset (die, 0, sizeof (struct die_info));
12558   return (die);
12559 }
12560
12561 \f
12562 /* Macro support.  */
12563
12564
12565 /* Return the full name of file number I in *LH's file name table.
12566    Use COMP_DIR as the name of the current directory of the
12567    compilation.  The result is allocated using xmalloc; the caller is
12568    responsible for freeing it.  */
12569 static char *
12570 file_full_name (int file, struct line_header *lh, const char *comp_dir)
12571 {
12572   /* Is the file number a valid index into the line header's file name
12573      table?  Remember that file numbers start with one, not zero.  */
12574   if (1 <= file && file <= lh->num_file_names)
12575     {
12576       struct file_entry *fe = &lh->file_names[file - 1];
12577
12578       if (IS_ABSOLUTE_PATH (fe->name))
12579         return xstrdup (fe->name);
12580       else
12581         {
12582           const char *dir;
12583           int dir_len;
12584           char *full_name;
12585
12586           if (fe->dir_index)
12587             dir = lh->include_dirs[fe->dir_index - 1];
12588           else
12589             dir = comp_dir;
12590
12591           if (dir)
12592             {
12593               dir_len = strlen (dir);
12594               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12595               strcpy (full_name, dir);
12596               full_name[dir_len] = '/';
12597               strcpy (full_name + dir_len + 1, fe->name);
12598               return full_name;
12599             }
12600           else
12601             return xstrdup (fe->name);
12602         }
12603     }
12604   else
12605     {
12606       /* The compiler produced a bogus file number.  We can at least
12607          record the macro definitions made in the file, even if we
12608          won't be able to find the file by name.  */
12609       char fake_name[80];
12610
12611       sprintf (fake_name, "<bad macro file number %d>", file);
12612
12613       complaint (&symfile_complaints,
12614                  _("bad file number in macro information (%d)"),
12615                  file);
12616
12617       return xstrdup (fake_name);
12618     }
12619 }
12620
12621
12622 static struct macro_source_file *
12623 macro_start_file (int file, int line,
12624                   struct macro_source_file *current_file,
12625                   const char *comp_dir,
12626                   struct line_header *lh, struct objfile *objfile)
12627 {
12628   /* The full name of this source file.  */
12629   char *full_name = file_full_name (file, lh, comp_dir);
12630
12631   /* We don't create a macro table for this compilation unit
12632      at all until we actually get a filename.  */
12633   if (! pending_macros)
12634     pending_macros = new_macro_table (&objfile->objfile_obstack,
12635                                       objfile->macro_cache);
12636
12637   if (! current_file)
12638     /* If we have no current file, then this must be the start_file
12639        directive for the compilation unit's main source file.  */
12640     current_file = macro_set_main (pending_macros, full_name);
12641   else
12642     current_file = macro_include (current_file, line, full_name);
12643
12644   xfree (full_name);
12645
12646   return current_file;
12647 }
12648
12649
12650 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12651    followed by a null byte.  */
12652 static char *
12653 copy_string (const char *buf, int len)
12654 {
12655   char *s = xmalloc (len + 1);
12656
12657   memcpy (s, buf, len);
12658   s[len] = '\0';
12659   return s;
12660 }
12661
12662
12663 static const char *
12664 consume_improper_spaces (const char *p, const char *body)
12665 {
12666   if (*p == ' ')
12667     {
12668       complaint (&symfile_complaints,
12669                  _("macro definition contains spaces in formal argument list:\n`%s'"),
12670                  body);
12671
12672       while (*p == ' ')
12673         p++;
12674     }
12675
12676   return p;
12677 }
12678
12679
12680 static void
12681 parse_macro_definition (struct macro_source_file *file, int line,
12682                         const char *body)
12683 {
12684   const char *p;
12685
12686   /* The body string takes one of two forms.  For object-like macro
12687      definitions, it should be:
12688
12689         <macro name> " " <definition>
12690
12691      For function-like macro definitions, it should be:
12692
12693         <macro name> "() " <definition>
12694      or
12695         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12696
12697      Spaces may appear only where explicitly indicated, and in the
12698      <definition>.
12699
12700      The Dwarf 2 spec says that an object-like macro's name is always
12701      followed by a space, but versions of GCC around March 2002 omit
12702      the space when the macro's definition is the empty string.
12703
12704      The Dwarf 2 spec says that there should be no spaces between the
12705      formal arguments in a function-like macro's formal argument list,
12706      but versions of GCC around March 2002 include spaces after the
12707      commas.  */
12708
12709
12710   /* Find the extent of the macro name.  The macro name is terminated
12711      by either a space or null character (for an object-like macro) or
12712      an opening paren (for a function-like macro).  */
12713   for (p = body; *p; p++)
12714     if (*p == ' ' || *p == '(')
12715       break;
12716
12717   if (*p == ' ' || *p == '\0')
12718     {
12719       /* It's an object-like macro.  */
12720       int name_len = p - body;
12721       char *name = copy_string (body, name_len);
12722       const char *replacement;
12723
12724       if (*p == ' ')
12725         replacement = body + name_len + 1;
12726       else
12727         {
12728           dwarf2_macro_malformed_definition_complaint (body);
12729           replacement = body + name_len;
12730         }
12731
12732       macro_define_object (file, line, name, replacement);
12733
12734       xfree (name);
12735     }
12736   else if (*p == '(')
12737     {
12738       /* It's a function-like macro.  */
12739       char *name = copy_string (body, p - body);
12740       int argc = 0;
12741       int argv_size = 1;
12742       char **argv = xmalloc (argv_size * sizeof (*argv));
12743
12744       p++;
12745
12746       p = consume_improper_spaces (p, body);
12747
12748       /* Parse the formal argument list.  */
12749       while (*p && *p != ')')
12750         {
12751           /* Find the extent of the current argument name.  */
12752           const char *arg_start = p;
12753
12754           while (*p && *p != ',' && *p != ')' && *p != ' ')
12755             p++;
12756
12757           if (! *p || p == arg_start)
12758             dwarf2_macro_malformed_definition_complaint (body);
12759           else
12760             {
12761               /* Make sure argv has room for the new argument.  */
12762               if (argc >= argv_size)
12763                 {
12764                   argv_size *= 2;
12765                   argv = xrealloc (argv, argv_size * sizeof (*argv));
12766                 }
12767
12768               argv[argc++] = copy_string (arg_start, p - arg_start);
12769             }
12770
12771           p = consume_improper_spaces (p, body);
12772
12773           /* Consume the comma, if present.  */
12774           if (*p == ',')
12775             {
12776               p++;
12777
12778               p = consume_improper_spaces (p, body);
12779             }
12780         }
12781
12782       if (*p == ')')
12783         {
12784           p++;
12785
12786           if (*p == ' ')
12787             /* Perfectly formed definition, no complaints.  */
12788             macro_define_function (file, line, name,
12789                                    argc, (const char **) argv,
12790                                    p + 1);
12791           else if (*p == '\0')
12792             {
12793               /* Complain, but do define it.  */
12794               dwarf2_macro_malformed_definition_complaint (body);
12795               macro_define_function (file, line, name,
12796                                      argc, (const char **) argv,
12797                                      p);
12798             }
12799           else
12800             /* Just complain.  */
12801             dwarf2_macro_malformed_definition_complaint (body);
12802         }
12803       else
12804         /* Just complain.  */
12805         dwarf2_macro_malformed_definition_complaint (body);
12806
12807       xfree (name);
12808       {
12809         int i;
12810
12811         for (i = 0; i < argc; i++)
12812           xfree (argv[i]);
12813       }
12814       xfree (argv);
12815     }
12816   else
12817     dwarf2_macro_malformed_definition_complaint (body);
12818 }
12819
12820
12821 static void
12822 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12823                      char *comp_dir, bfd *abfd,
12824                      struct dwarf2_cu *cu)
12825 {
12826   gdb_byte *mac_ptr, *mac_end;
12827   struct macro_source_file *current_file = 0;
12828   enum dwarf_macinfo_record_type macinfo_type;
12829   int at_commandline;
12830
12831   dwarf2_read_section (dwarf2_per_objfile->objfile,
12832                        &dwarf2_per_objfile->macinfo);
12833   if (dwarf2_per_objfile->macinfo.buffer == NULL)
12834     {
12835       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
12836       return;
12837     }
12838
12839   /* First pass: Find the name of the base filename.
12840      This filename is needed in order to process all macros whose definition
12841      (or undefinition) comes from the command line.  These macros are defined
12842      before the first DW_MACINFO_start_file entry, and yet still need to be
12843      associated to the base file.
12844
12845      To determine the base file name, we scan the macro definitions until we
12846      reach the first DW_MACINFO_start_file entry.  We then initialize
12847      CURRENT_FILE accordingly so that any macro definition found before the
12848      first DW_MACINFO_start_file can still be associated to the base file.  */
12849
12850   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12851   mac_end = dwarf2_per_objfile->macinfo.buffer
12852     + dwarf2_per_objfile->macinfo.size;
12853
12854   do
12855     {
12856       /* Do we at least have room for a macinfo type byte?  */
12857       if (mac_ptr >= mac_end)
12858         {
12859           /* Complaint is printed during the second pass as GDB will probably
12860              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
12861           break;
12862         }
12863
12864       macinfo_type = read_1_byte (abfd, mac_ptr);
12865       mac_ptr++;
12866
12867       switch (macinfo_type)
12868         {
12869           /* A zero macinfo type indicates the end of the macro
12870              information.  */
12871         case 0:
12872           break;
12873
12874         case DW_MACINFO_define:
12875         case DW_MACINFO_undef:
12876           /* Only skip the data by MAC_PTR.  */
12877           {
12878             unsigned int bytes_read;
12879
12880             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12881             mac_ptr += bytes_read;
12882             read_direct_string (abfd, mac_ptr, &bytes_read);
12883             mac_ptr += bytes_read;
12884           }
12885           break;
12886
12887         case DW_MACINFO_start_file:
12888           {
12889             unsigned int bytes_read;
12890             int line, file;
12891
12892             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12893             mac_ptr += bytes_read;
12894             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12895             mac_ptr += bytes_read;
12896
12897             current_file = macro_start_file (file, line, current_file, comp_dir,
12898                                              lh, cu->objfile);
12899           }
12900           break;
12901
12902         case DW_MACINFO_end_file:
12903           /* No data to skip by MAC_PTR.  */
12904           break;
12905
12906         case DW_MACINFO_vendor_ext:
12907           /* Only skip the data by MAC_PTR.  */
12908           {
12909             unsigned int bytes_read;
12910
12911             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12912             mac_ptr += bytes_read;
12913             read_direct_string (abfd, mac_ptr, &bytes_read);
12914             mac_ptr += bytes_read;
12915           }
12916           break;
12917
12918         default:
12919           break;
12920         }
12921     } while (macinfo_type != 0 && current_file == NULL);
12922
12923   /* Second pass: Process all entries.
12924
12925      Use the AT_COMMAND_LINE flag to determine whether we are still processing
12926      command-line macro definitions/undefinitions.  This flag is unset when we
12927      reach the first DW_MACINFO_start_file entry.  */
12928
12929   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
12930
12931   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
12932      GDB is still reading the definitions from command line.  First
12933      DW_MACINFO_start_file will need to be ignored as it was already executed
12934      to create CURRENT_FILE for the main source holding also the command line
12935      definitions.  On first met DW_MACINFO_start_file this flag is reset to
12936      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
12937
12938   at_commandline = 1;
12939
12940   do
12941     {
12942       /* Do we at least have room for a macinfo type byte?  */
12943       if (mac_ptr >= mac_end)
12944         {
12945           dwarf2_macros_too_long_complaint ();
12946           break;
12947         }
12948
12949       macinfo_type = read_1_byte (abfd, mac_ptr);
12950       mac_ptr++;
12951
12952       switch (macinfo_type)
12953         {
12954           /* A zero macinfo type indicates the end of the macro
12955              information.  */
12956         case 0:
12957           break;
12958
12959         case DW_MACINFO_define:
12960         case DW_MACINFO_undef:
12961           {
12962             unsigned int bytes_read;
12963             int line;
12964             char *body;
12965
12966             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
12967             mac_ptr += bytes_read;
12968             body = read_direct_string (abfd, mac_ptr, &bytes_read);
12969             mac_ptr += bytes_read;
12970
12971             if (! current_file)
12972               {
12973                 /* DWARF violation as no main source is present.  */
12974                 complaint (&symfile_complaints,
12975                            _("debug info with no main source gives macro %s "
12976                              "on line %d: %s"),
12977                            macinfo_type == DW_MACINFO_define ?
12978                              _("definition") :
12979                                macinfo_type == DW_MACINFO_undef ?
12980                                  _("undefinition") :
12981                                  _("something-or-other"), line, body);
12982                 break;
12983               }
12984             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
12985               complaint (&symfile_complaints,
12986                          _("debug info gives %s macro %s with %s line %d: %s"),
12987                          at_commandline ? _("command-line") : _("in-file"),
12988                          macinfo_type == DW_MACINFO_define ?
12989                            _("definition") :
12990                              macinfo_type == DW_MACINFO_undef ?
12991                                _("undefinition") :
12992                                _("something-or-other"),
12993                          line == 0 ? _("zero") : _("non-zero"), line, body);
12994
12995             if (macinfo_type == DW_MACINFO_define)
12996               parse_macro_definition (current_file, line, body);
12997             else if (macinfo_type == DW_MACINFO_undef)
12998               macro_undef (current_file, line, body);
12999           }
13000           break;
13001
13002         case DW_MACINFO_start_file:
13003           {
13004             unsigned int bytes_read;
13005             int line, file;
13006
13007             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13008             mac_ptr += bytes_read;
13009             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13010             mac_ptr += bytes_read;
13011
13012             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13013               complaint (&symfile_complaints,
13014                          _("debug info gives source %d included "
13015                            "from %s at %s line %d"),
13016                          file, at_commandline ? _("command-line") : _("file"),
13017                          line == 0 ? _("zero") : _("non-zero"), line);
13018
13019             if (at_commandline)
13020               {
13021                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13022                 at_commandline = 0;
13023               }
13024             else
13025               current_file = macro_start_file (file, line,
13026                                                current_file, comp_dir,
13027                                                lh, cu->objfile);
13028           }
13029           break;
13030
13031         case DW_MACINFO_end_file:
13032           if (! current_file)
13033             complaint (&symfile_complaints,
13034                        _("macro debug info has an unmatched `close_file' directive"));
13035           else
13036             {
13037               current_file = current_file->included_by;
13038               if (! current_file)
13039                 {
13040                   enum dwarf_macinfo_record_type next_type;
13041
13042                   /* GCC circa March 2002 doesn't produce the zero
13043                      type byte marking the end of the compilation
13044                      unit.  Complain if it's not there, but exit no
13045                      matter what.  */
13046
13047                   /* Do we at least have room for a macinfo type byte?  */
13048                   if (mac_ptr >= mac_end)
13049                     {
13050                       dwarf2_macros_too_long_complaint ();
13051                       return;
13052                     }
13053
13054                   /* We don't increment mac_ptr here, so this is just
13055                      a look-ahead.  */
13056                   next_type = read_1_byte (abfd, mac_ptr);
13057                   if (next_type != 0)
13058                     complaint (&symfile_complaints,
13059                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13060
13061                   return;
13062                 }
13063             }
13064           break;
13065
13066         case DW_MACINFO_vendor_ext:
13067           {
13068             unsigned int bytes_read;
13069             int constant;
13070             char *string;
13071
13072             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13073             mac_ptr += bytes_read;
13074             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13075             mac_ptr += bytes_read;
13076
13077             /* We don't recognize any vendor extensions.  */
13078           }
13079           break;
13080         }
13081     } while (macinfo_type != 0);
13082 }
13083
13084 /* Check if the attribute's form is a DW_FORM_block*
13085    if so return true else false. */
13086 static int
13087 attr_form_is_block (struct attribute *attr)
13088 {
13089   return (attr == NULL ? 0 :
13090       attr->form == DW_FORM_block1
13091       || attr->form == DW_FORM_block2
13092       || attr->form == DW_FORM_block4
13093       || attr->form == DW_FORM_block
13094       || attr->form == DW_FORM_exprloc);
13095 }
13096
13097 /* Return non-zero if ATTR's value is a section offset --- classes
13098    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13099    You may use DW_UNSND (attr) to retrieve such offsets.
13100
13101    Section 7.5.4, "Attribute Encodings", explains that no attribute
13102    may have a value that belongs to more than one of these classes; it
13103    would be ambiguous if we did, because we use the same forms for all
13104    of them.  */
13105 static int
13106 attr_form_is_section_offset (struct attribute *attr)
13107 {
13108   return (attr->form == DW_FORM_data4
13109           || attr->form == DW_FORM_data8
13110           || attr->form == DW_FORM_sec_offset);
13111 }
13112
13113
13114 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13115    zero otherwise.  When this function returns true, you can apply
13116    dwarf2_get_attr_constant_value to it.
13117
13118    However, note that for some attributes you must check
13119    attr_form_is_section_offset before using this test.  DW_FORM_data4
13120    and DW_FORM_data8 are members of both the constant class, and of
13121    the classes that contain offsets into other debug sections
13122    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
13123    that, if an attribute's can be either a constant or one of the
13124    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13125    taken as section offsets, not constants.  */
13126 static int
13127 attr_form_is_constant (struct attribute *attr)
13128 {
13129   switch (attr->form)
13130     {
13131     case DW_FORM_sdata:
13132     case DW_FORM_udata:
13133     case DW_FORM_data1:
13134     case DW_FORM_data2:
13135     case DW_FORM_data4:
13136     case DW_FORM_data8:
13137       return 1;
13138     default:
13139       return 0;
13140     }
13141 }
13142
13143 static void
13144 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13145                              struct dwarf2_cu *cu)
13146 {
13147   if (attr_form_is_section_offset (attr)
13148       /* ".debug_loc" may not exist at all, or the offset may be outside
13149          the section.  If so, fall through to the complaint in the
13150          other branch.  */
13151       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13152     {
13153       struct dwarf2_loclist_baton *baton;
13154
13155       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13156                              sizeof (struct dwarf2_loclist_baton));
13157       baton->per_cu = cu->per_cu;
13158       gdb_assert (baton->per_cu);
13159
13160       dwarf2_read_section (dwarf2_per_objfile->objfile,
13161                            &dwarf2_per_objfile->loc);
13162
13163       /* We don't know how long the location list is, but make sure we
13164          don't run off the edge of the section.  */
13165       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13166       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13167       baton->base_address = cu->base_address;
13168       if (cu->base_known == 0)
13169         complaint (&symfile_complaints,
13170                    _("Location list used without specifying the CU base address."));
13171
13172       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13173       SYMBOL_LOCATION_BATON (sym) = baton;
13174     }
13175   else
13176     {
13177       struct dwarf2_locexpr_baton *baton;
13178
13179       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13180                              sizeof (struct dwarf2_locexpr_baton));
13181       baton->per_cu = cu->per_cu;
13182       gdb_assert (baton->per_cu);
13183
13184       if (attr_form_is_block (attr))
13185         {
13186           /* Note that we're just copying the block's data pointer
13187              here, not the actual data.  We're still pointing into the
13188              info_buffer for SYM's objfile; right now we never release
13189              that buffer, but when we do clean up properly this may
13190              need to change.  */
13191           baton->size = DW_BLOCK (attr)->size;
13192           baton->data = DW_BLOCK (attr)->data;
13193         }
13194       else
13195         {
13196           dwarf2_invalid_attrib_class_complaint ("location description",
13197                                                  SYMBOL_NATURAL_NAME (sym));
13198           baton->size = 0;
13199           baton->data = NULL;
13200         }
13201
13202       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13203       SYMBOL_LOCATION_BATON (sym) = baton;
13204     }
13205 }
13206
13207 /* Return the OBJFILE associated with the compilation unit CU.  If CU
13208    came from a separate debuginfo file, then the master objfile is
13209    returned.  */
13210
13211 struct objfile *
13212 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13213 {
13214   struct objfile *objfile = per_cu->objfile;
13215
13216   /* Return the master objfile, so that we can report and look up the
13217      correct file containing this variable.  */
13218   if (objfile->separate_debug_objfile_backlink)
13219     objfile = objfile->separate_debug_objfile_backlink;
13220
13221   return objfile;
13222 }
13223
13224 /* Return the address size given in the compilation unit header for CU.  */
13225
13226 CORE_ADDR
13227 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13228 {
13229   if (per_cu->cu)
13230     return per_cu->cu->header.addr_size;
13231   else
13232     {
13233       /* If the CU is not currently read in, we re-read its header.  */
13234       struct objfile *objfile = per_cu->objfile;
13235       struct dwarf2_per_objfile *per_objfile
13236         = objfile_data (objfile, dwarf2_objfile_data_key);
13237       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13238       struct comp_unit_head cu_header;
13239
13240       memset (&cu_header, 0, sizeof cu_header);
13241       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13242       return cu_header.addr_size;
13243     }
13244 }
13245
13246 /* Return the offset size given in the compilation unit header for CU.  */
13247
13248 int
13249 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13250 {
13251   if (per_cu->cu)
13252     return per_cu->cu->header.offset_size;
13253   else
13254     {
13255       /* If the CU is not currently read in, we re-read its header.  */
13256       struct objfile *objfile = per_cu->objfile;
13257       struct dwarf2_per_objfile *per_objfile
13258         = objfile_data (objfile, dwarf2_objfile_data_key);
13259       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13260       struct comp_unit_head cu_header;
13261
13262       memset (&cu_header, 0, sizeof cu_header);
13263       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13264       return cu_header.offset_size;
13265     }
13266 }
13267
13268 /* Return the text offset of the CU.  The returned offset comes from
13269    this CU's objfile.  If this objfile came from a separate debuginfo
13270    file, then the offset may be different from the corresponding
13271    offset in the parent objfile.  */
13272
13273 CORE_ADDR
13274 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13275 {
13276   struct objfile *objfile = per_cu->objfile;
13277
13278   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13279 }
13280
13281 /* Locate the .debug_info compilation unit from CU's objfile which contains
13282    the DIE at OFFSET.  Raises an error on failure.  */
13283
13284 static struct dwarf2_per_cu_data *
13285 dwarf2_find_containing_comp_unit (unsigned int offset,
13286                                   struct objfile *objfile)
13287 {
13288   struct dwarf2_per_cu_data *this_cu;
13289   int low, high;
13290
13291   low = 0;
13292   high = dwarf2_per_objfile->n_comp_units - 1;
13293   while (high > low)
13294     {
13295       int mid = low + (high - low) / 2;
13296
13297       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13298         high = mid;
13299       else
13300         low = mid + 1;
13301     }
13302   gdb_assert (low == high);
13303   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13304     {
13305       if (low == 0)
13306         error (_("Dwarf Error: could not find partial DIE containing "
13307                "offset 0x%lx [in module %s]"),
13308                (long) offset, bfd_get_filename (objfile->obfd));
13309
13310       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13311       return dwarf2_per_objfile->all_comp_units[low-1];
13312     }
13313   else
13314     {
13315       this_cu = dwarf2_per_objfile->all_comp_units[low];
13316       if (low == dwarf2_per_objfile->n_comp_units - 1
13317           && offset >= this_cu->offset + this_cu->length)
13318         error (_("invalid dwarf2 offset %u"), offset);
13319       gdb_assert (offset < this_cu->offset + this_cu->length);
13320       return this_cu;
13321     }
13322 }
13323
13324 /* Locate the compilation unit from OBJFILE which is located at exactly
13325    OFFSET.  Raises an error on failure.  */
13326
13327 static struct dwarf2_per_cu_data *
13328 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13329 {
13330   struct dwarf2_per_cu_data *this_cu;
13331
13332   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13333   if (this_cu->offset != offset)
13334     error (_("no compilation unit with offset %u."), offset);
13335   return this_cu;
13336 }
13337
13338 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
13339
13340 static struct dwarf2_cu *
13341 alloc_one_comp_unit (struct objfile *objfile)
13342 {
13343   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13344   cu->objfile = objfile;
13345   obstack_init (&cu->comp_unit_obstack);
13346   return cu;
13347 }
13348
13349 /* Release one cached compilation unit, CU.  We unlink it from the tree
13350    of compilation units, but we don't remove it from the read_in_chain;
13351    the caller is responsible for that.
13352    NOTE: DATA is a void * because this function is also used as a
13353    cleanup routine.  */
13354
13355 static void
13356 free_one_comp_unit (void *data)
13357 {
13358   struct dwarf2_cu *cu = data;
13359
13360   if (cu->per_cu != NULL)
13361     cu->per_cu->cu = NULL;
13362   cu->per_cu = NULL;
13363
13364   obstack_free (&cu->comp_unit_obstack, NULL);
13365
13366   xfree (cu);
13367 }
13368
13369 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13370    when we're finished with it.  We can't free the pointer itself, but be
13371    sure to unlink it from the cache.  Also release any associated storage
13372    and perform cache maintenance.
13373
13374    Only used during partial symbol parsing.  */
13375
13376 static void
13377 free_stack_comp_unit (void *data)
13378 {
13379   struct dwarf2_cu *cu = data;
13380
13381   obstack_free (&cu->comp_unit_obstack, NULL);
13382   cu->partial_dies = NULL;
13383
13384   if (cu->per_cu != NULL)
13385     {
13386       /* This compilation unit is on the stack in our caller, so we
13387          should not xfree it.  Just unlink it.  */
13388       cu->per_cu->cu = NULL;
13389       cu->per_cu = NULL;
13390
13391       /* If we had a per-cu pointer, then we may have other compilation
13392          units loaded, so age them now.  */
13393       age_cached_comp_units ();
13394     }
13395 }
13396
13397 /* Free all cached compilation units.  */
13398
13399 static void
13400 free_cached_comp_units (void *data)
13401 {
13402   struct dwarf2_per_cu_data *per_cu, **last_chain;
13403
13404   per_cu = dwarf2_per_objfile->read_in_chain;
13405   last_chain = &dwarf2_per_objfile->read_in_chain;
13406   while (per_cu != NULL)
13407     {
13408       struct dwarf2_per_cu_data *next_cu;
13409
13410       next_cu = per_cu->cu->read_in_chain;
13411
13412       free_one_comp_unit (per_cu->cu);
13413       *last_chain = next_cu;
13414
13415       per_cu = next_cu;
13416     }
13417 }
13418
13419 /* Increase the age counter on each cached compilation unit, and free
13420    any that are too old.  */
13421
13422 static void
13423 age_cached_comp_units (void)
13424 {
13425   struct dwarf2_per_cu_data *per_cu, **last_chain;
13426
13427   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13428   per_cu = dwarf2_per_objfile->read_in_chain;
13429   while (per_cu != NULL)
13430     {
13431       per_cu->cu->last_used ++;
13432       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13433         dwarf2_mark (per_cu->cu);
13434       per_cu = per_cu->cu->read_in_chain;
13435     }
13436
13437   per_cu = dwarf2_per_objfile->read_in_chain;
13438   last_chain = &dwarf2_per_objfile->read_in_chain;
13439   while (per_cu != NULL)
13440     {
13441       struct dwarf2_per_cu_data *next_cu;
13442
13443       next_cu = per_cu->cu->read_in_chain;
13444
13445       if (!per_cu->cu->mark)
13446         {
13447           free_one_comp_unit (per_cu->cu);
13448           *last_chain = next_cu;
13449         }
13450       else
13451         last_chain = &per_cu->cu->read_in_chain;
13452
13453       per_cu = next_cu;
13454     }
13455 }
13456
13457 /* Remove a single compilation unit from the cache.  */
13458
13459 static void
13460 free_one_cached_comp_unit (void *target_cu)
13461 {
13462   struct dwarf2_per_cu_data *per_cu, **last_chain;
13463
13464   per_cu = dwarf2_per_objfile->read_in_chain;
13465   last_chain = &dwarf2_per_objfile->read_in_chain;
13466   while (per_cu != NULL)
13467     {
13468       struct dwarf2_per_cu_data *next_cu;
13469
13470       next_cu = per_cu->cu->read_in_chain;
13471
13472       if (per_cu->cu == target_cu)
13473         {
13474           free_one_comp_unit (per_cu->cu);
13475           *last_chain = next_cu;
13476           break;
13477         }
13478       else
13479         last_chain = &per_cu->cu->read_in_chain;
13480
13481       per_cu = next_cu;
13482     }
13483 }
13484
13485 /* Release all extra memory associated with OBJFILE.  */
13486
13487 void
13488 dwarf2_free_objfile (struct objfile *objfile)
13489 {
13490   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13491
13492   if (dwarf2_per_objfile == NULL)
13493     return;
13494
13495   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
13496   free_cached_comp_units (NULL);
13497
13498   if (dwarf2_per_objfile->using_index)
13499     {
13500       int i;
13501
13502       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13503         {
13504           int j;
13505           struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13506
13507           if (!cu->v.quick->lines)
13508             continue;
13509
13510           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13511             {
13512               if (cu->v.quick->file_names)
13513                 xfree ((void *) cu->v.quick->file_names[j]);
13514               if (cu->v.quick->full_names)
13515                 xfree ((void *) cu->v.quick->full_names[j]);
13516             }
13517
13518           free_line_header (cu->v.quick->lines);
13519         }
13520     }
13521
13522   /* Everything else should be on the objfile obstack.  */
13523 }
13524
13525 /* A pair of DIE offset and GDB type pointer.  We store these
13526    in a hash table separate from the DIEs, and preserve them
13527    when the DIEs are flushed out of cache.  */
13528
13529 struct dwarf2_offset_and_type
13530 {
13531   unsigned int offset;
13532   struct type *type;
13533 };
13534
13535 /* Hash function for a dwarf2_offset_and_type.  */
13536
13537 static hashval_t
13538 offset_and_type_hash (const void *item)
13539 {
13540   const struct dwarf2_offset_and_type *ofs = item;
13541
13542   return ofs->offset;
13543 }
13544
13545 /* Equality function for a dwarf2_offset_and_type.  */
13546
13547 static int
13548 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13549 {
13550   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13551   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
13552
13553   return ofs_lhs->offset == ofs_rhs->offset;
13554 }
13555
13556 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
13557    table if necessary.  For convenience, return TYPE.
13558
13559    The DIEs reading must have careful ordering to:
13560     * Not cause infite loops trying to read in DIEs as a prerequisite for
13561       reading current DIE.
13562     * Not trying to dereference contents of still incompletely read in types
13563       while reading in other DIEs.
13564     * Enable referencing still incompletely read in types just by a pointer to
13565       the type without accessing its fields.
13566
13567    Therefore caller should follow these rules:
13568      * Try to fetch any prerequisite types we may need to build this DIE type
13569        before building the type and calling set_die_type.
13570      * After building typer call set_die_type for current DIE as soon as
13571        possible before fetching more types to complete the current type.
13572      * Make the type as complete as possible before fetching more types.  */
13573
13574 static struct type *
13575 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13576 {
13577   struct dwarf2_offset_and_type **slot, ofs;
13578
13579   /* For Ada types, make sure that the gnat-specific data is always
13580      initialized (if not already set).  There are a few types where
13581      we should not be doing so, because the type-specific area is
13582      already used to hold some other piece of info (eg: TYPE_CODE_FLT
13583      where the type-specific area is used to store the floatformat).
13584      But this is not a problem, because the gnat-specific information
13585      is actually not needed for these types.  */
13586   if (need_gnat_info (cu)
13587       && TYPE_CODE (type) != TYPE_CODE_FUNC
13588       && TYPE_CODE (type) != TYPE_CODE_FLT
13589       && !HAVE_GNAT_AUX_INFO (type))
13590     INIT_GNAT_SPECIFIC (type);
13591
13592   if (cu->type_hash == NULL)
13593     {
13594       gdb_assert (cu->per_cu != NULL);
13595       cu->per_cu->type_hash
13596         = htab_create_alloc_ex (cu->header.length / 24,
13597                                 offset_and_type_hash,
13598                                 offset_and_type_eq,
13599                                 NULL,
13600                                 &cu->objfile->objfile_obstack,
13601                                 hashtab_obstack_allocate,
13602                                 dummy_obstack_deallocate);
13603       cu->type_hash = cu->per_cu->type_hash;
13604     }
13605
13606   ofs.offset = die->offset;
13607   ofs.type = type;
13608   slot = (struct dwarf2_offset_and_type **)
13609     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
13610   if (*slot)
13611     complaint (&symfile_complaints,
13612                _("A problem internal to GDB: DIE 0x%x has type already set"),
13613                die->offset);
13614   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13615   **slot = ofs;
13616   return type;
13617 }
13618
13619 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13620    not have a saved type.  */
13621
13622 static struct type *
13623 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
13624 {
13625   struct dwarf2_offset_and_type *slot, ofs;
13626   htab_t type_hash = cu->type_hash;
13627
13628   if (type_hash == NULL)
13629     return NULL;
13630
13631   ofs.offset = die->offset;
13632   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13633   if (slot)
13634     return slot->type;
13635   else
13636     return NULL;
13637 }
13638
13639 /* Add a dependence relationship from CU to REF_PER_CU.  */
13640
13641 static void
13642 dwarf2_add_dependence (struct dwarf2_cu *cu,
13643                        struct dwarf2_per_cu_data *ref_per_cu)
13644 {
13645   void **slot;
13646
13647   if (cu->dependencies == NULL)
13648     cu->dependencies
13649       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13650                               NULL, &cu->comp_unit_obstack,
13651                               hashtab_obstack_allocate,
13652                               dummy_obstack_deallocate);
13653
13654   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13655   if (*slot == NULL)
13656     *slot = ref_per_cu;
13657 }
13658
13659 /* Subroutine of dwarf2_mark to pass to htab_traverse.
13660    Set the mark field in every compilation unit in the
13661    cache that we must keep because we are keeping CU.  */
13662
13663 static int
13664 dwarf2_mark_helper (void **slot, void *data)
13665 {
13666   struct dwarf2_per_cu_data *per_cu;
13667
13668   per_cu = (struct dwarf2_per_cu_data *) *slot;
13669   if (per_cu->cu->mark)
13670     return 1;
13671   per_cu->cu->mark = 1;
13672
13673   if (per_cu->cu->dependencies != NULL)
13674     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13675
13676   return 1;
13677 }
13678
13679 /* Set the mark field in CU and in every other compilation unit in the
13680    cache that we must keep because we are keeping CU.  */
13681
13682 static void
13683 dwarf2_mark (struct dwarf2_cu *cu)
13684 {
13685   if (cu->mark)
13686     return;
13687   cu->mark = 1;
13688   if (cu->dependencies != NULL)
13689     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
13690 }
13691
13692 static void
13693 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13694 {
13695   while (per_cu)
13696     {
13697       per_cu->cu->mark = 0;
13698       per_cu = per_cu->cu->read_in_chain;
13699     }
13700 }
13701
13702 /* Trivial hash function for partial_die_info: the hash value of a DIE
13703    is its offset in .debug_info for this objfile.  */
13704
13705 static hashval_t
13706 partial_die_hash (const void *item)
13707 {
13708   const struct partial_die_info *part_die = item;
13709
13710   return part_die->offset;
13711 }
13712
13713 /* Trivial comparison function for partial_die_info structures: two DIEs
13714    are equal if they have the same offset.  */
13715
13716 static int
13717 partial_die_eq (const void *item_lhs, const void *item_rhs)
13718 {
13719   const struct partial_die_info *part_die_lhs = item_lhs;
13720   const struct partial_die_info *part_die_rhs = item_rhs;
13721
13722   return part_die_lhs->offset == part_die_rhs->offset;
13723 }
13724
13725 static struct cmd_list_element *set_dwarf2_cmdlist;
13726 static struct cmd_list_element *show_dwarf2_cmdlist;
13727
13728 static void
13729 set_dwarf2_cmd (char *args, int from_tty)
13730 {
13731   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13732 }
13733
13734 static void
13735 show_dwarf2_cmd (char *args, int from_tty)
13736 {
13737   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13738 }
13739
13740 /* If section described by INFO was mmapped, munmap it now.  */
13741
13742 static void
13743 munmap_section_buffer (struct dwarf2_section_info *info)
13744 {
13745   if (info->was_mmapped)
13746     {
13747 #ifdef HAVE_MMAP
13748       intptr_t begin = (intptr_t) info->buffer;
13749       intptr_t map_begin = begin & ~(pagesize - 1);
13750       size_t map_length = info->size + begin - map_begin;
13751
13752       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13753 #else
13754       /* Without HAVE_MMAP, we should never be here to begin with.  */
13755       gdb_assert (0);
13756 #endif
13757     }
13758 }
13759
13760 /* munmap debug sections for OBJFILE, if necessary.  */
13761
13762 static void
13763 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
13764 {
13765   struct dwarf2_per_objfile *data = d;
13766
13767   munmap_section_buffer (&data->info);
13768   munmap_section_buffer (&data->abbrev);
13769   munmap_section_buffer (&data->line);
13770   munmap_section_buffer (&data->str);
13771   munmap_section_buffer (&data->macinfo);
13772   munmap_section_buffer (&data->ranges);
13773   munmap_section_buffer (&data->loc);
13774   munmap_section_buffer (&data->frame);
13775   munmap_section_buffer (&data->eh_frame);
13776   munmap_section_buffer (&data->gdb_index);
13777 }
13778
13779 \f
13780
13781 /* The contents of the hash table we create when building the string
13782    table.  */
13783 struct strtab_entry
13784 {
13785   offset_type offset;
13786   const char *str;
13787 };
13788
13789 /* Hash function for a strtab_entry.  */
13790 static hashval_t
13791 hash_strtab_entry (const void *e)
13792 {
13793   const struct strtab_entry *entry = e;
13794   return mapped_index_string_hash (entry->str);
13795 }
13796
13797 /* Equality function for a strtab_entry.  */
13798 static int
13799 eq_strtab_entry (const void *a, const void *b)
13800 {
13801   const struct strtab_entry *ea = a;
13802   const struct strtab_entry *eb = b;
13803   return !strcmp (ea->str, eb->str);
13804 }
13805
13806 /* Create a strtab_entry hash table.  */
13807 static htab_t
13808 create_strtab (void)
13809 {
13810   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13811                             xfree, xcalloc, xfree);
13812 }
13813
13814 /* Add a string to the constant pool.  Return the string's offset in
13815    host order.  */
13816 static offset_type
13817 add_string (htab_t table, struct obstack *cpool, const char *str)
13818 {
13819   void **slot;
13820   struct strtab_entry entry;
13821   struct strtab_entry *result;
13822
13823   entry.str = str;
13824   slot = htab_find_slot (table, &entry, INSERT);
13825   if (*slot)
13826     result = *slot;
13827   else
13828     {
13829       result = XNEW (struct strtab_entry);
13830       result->offset = obstack_object_size (cpool);
13831       result->str = str;
13832       obstack_grow_str0 (cpool, str);
13833       *slot = result;
13834     }
13835   return result->offset;
13836 }
13837
13838 /* An entry in the symbol table.  */
13839 struct symtab_index_entry
13840 {
13841   /* The name of the symbol.  */
13842   const char *name;
13843   /* The offset of the name in the constant pool.  */
13844   offset_type index_offset;
13845   /* A sorted vector of the indices of all the CUs that hold an object
13846      of this name.  */
13847   VEC (offset_type) *cu_indices;
13848 };
13849
13850 /* The symbol table.  This is a power-of-2-sized hash table.  */
13851 struct mapped_symtab
13852 {
13853   offset_type n_elements;
13854   offset_type size;
13855   struct symtab_index_entry **data;
13856 };
13857
13858 /* Hash function for a symtab_index_entry.  */
13859 static hashval_t
13860 hash_symtab_entry (const void *e)
13861 {
13862   const struct symtab_index_entry *entry = e;
13863   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
13864                          sizeof (offset_type) * VEC_length (offset_type,
13865                                                             entry->cu_indices),
13866                          0);
13867 }
13868
13869 /* Equality function for a symtab_index_entry.  */
13870 static int
13871 eq_symtab_entry (const void *a, const void *b)
13872 {
13873   const struct symtab_index_entry *ea = a;
13874   const struct symtab_index_entry *eb = b;
13875   int len = VEC_length (offset_type, ea->cu_indices);
13876   if (len != VEC_length (offset_type, eb->cu_indices))
13877     return 0;
13878   return !memcmp (VEC_address (offset_type, ea->cu_indices),
13879                   VEC_address (offset_type, eb->cu_indices),
13880                   sizeof (offset_type) * len);
13881 }
13882
13883 /* Destroy a symtab_index_entry.  */
13884 static void
13885 delete_symtab_entry (void *p)
13886 {
13887   struct symtab_index_entry *entry = p;
13888   VEC_free (offset_type, entry->cu_indices);
13889   xfree (entry);
13890 }
13891
13892 /* Create a hash table holding symtab_index_entry objects.  */
13893 static htab_t
13894 create_index_table (void)
13895 {
13896   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
13897                             delete_symtab_entry, xcalloc, xfree);
13898 }
13899
13900 /* Create a new mapped symtab object.  */
13901 static struct mapped_symtab *
13902 create_mapped_symtab (void)
13903 {
13904   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
13905   symtab->n_elements = 0;
13906   symtab->size = 1024;
13907   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13908   return symtab;
13909 }
13910
13911 /* Destroy a mapped_symtab.  */
13912 static void
13913 cleanup_mapped_symtab (void *p)
13914 {
13915   struct mapped_symtab *symtab = p;
13916   /* The contents of the array are freed when the other hash table is
13917      destroyed.  */
13918   xfree (symtab->data);
13919   xfree (symtab);
13920 }
13921
13922 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
13923    the slot.  */
13924 static struct symtab_index_entry **
13925 find_slot (struct mapped_symtab *symtab, const char *name)
13926 {
13927   offset_type index, step, hash = mapped_index_string_hash (name);
13928
13929   index = hash & (symtab->size - 1);
13930   step = ((hash * 17) & (symtab->size - 1)) | 1;
13931
13932   for (;;)
13933     {
13934       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
13935         return &symtab->data[index];
13936       index = (index + step) & (symtab->size - 1);
13937     }
13938 }
13939
13940 /* Expand SYMTAB's hash table.  */
13941 static void
13942 hash_expand (struct mapped_symtab *symtab)
13943 {
13944   offset_type old_size = symtab->size;
13945   offset_type i;
13946   struct symtab_index_entry **old_entries = symtab->data;
13947
13948   symtab->size *= 2;
13949   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
13950
13951   for (i = 0; i < old_size; ++i)
13952     {
13953       if (old_entries[i])
13954         {
13955           struct symtab_index_entry **slot = find_slot (symtab,
13956                                                         old_entries[i]->name);
13957           *slot = old_entries[i];
13958         }
13959     }
13960
13961   xfree (old_entries);
13962 }
13963
13964 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
13965    is the index of the CU in which the symbol appears.  */
13966 static void
13967 add_index_entry (struct mapped_symtab *symtab, const char *name,
13968                  offset_type cu_index)
13969 {
13970   struct symtab_index_entry **slot;
13971
13972   ++symtab->n_elements;
13973   if (4 * symtab->n_elements / 3 >= symtab->size)
13974     hash_expand (symtab);
13975
13976   slot = find_slot (symtab, name);
13977   if (!*slot)
13978     {
13979       *slot = XNEW (struct symtab_index_entry);
13980       (*slot)->name = name;
13981       (*slot)->cu_indices = NULL;
13982     }
13983   /* Don't push an index twice.  Due to how we add entries we only
13984      have to check the last one.  */ 
13985   if (VEC_empty (offset_type, (*slot)->cu_indices)
13986       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
13987     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
13988 }
13989
13990 /* Add a vector of indices to the constant pool.  */
13991 static offset_type
13992 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
13993                       struct symtab_index_entry *entry)
13994 {
13995   void **slot;
13996
13997   slot = htab_find_slot (index_table, entry, INSERT);
13998   if (!*slot)
13999     {
14000       offset_type len = VEC_length (offset_type, entry->cu_indices);
14001       offset_type val = MAYBE_SWAP (len);
14002       offset_type iter;
14003       int i;
14004
14005       *slot = entry;
14006       entry->index_offset = obstack_object_size (cpool);
14007
14008       obstack_grow (cpool, &val, sizeof (val));
14009       for (i = 0;
14010            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14011            ++i)
14012         {
14013           val = MAYBE_SWAP (iter);
14014           obstack_grow (cpool, &val, sizeof (val));
14015         }
14016     }
14017   else
14018     {
14019       struct symtab_index_entry *old_entry = *slot;
14020       entry->index_offset = old_entry->index_offset;
14021       entry = old_entry;
14022     }
14023   return entry->index_offset;
14024 }
14025
14026 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14027    constant pool entries going into the obstack CPOOL.  */
14028 static void
14029 write_hash_table (struct mapped_symtab *symtab,
14030                   struct obstack *output, struct obstack *cpool)
14031 {
14032   offset_type i;
14033   htab_t index_table;
14034   htab_t str_table;
14035
14036   index_table = create_index_table ();
14037   str_table = create_strtab ();
14038   /* We add all the index vectors to the constant pool first, to
14039      ensure alignment is ok.  */
14040   for (i = 0; i < symtab->size; ++i)
14041     {
14042       if (symtab->data[i])
14043         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14044     }
14045
14046   /* Now write out the hash table.  */
14047   for (i = 0; i < symtab->size; ++i)
14048     {
14049       offset_type str_off, vec_off;
14050
14051       if (symtab->data[i])
14052         {
14053           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14054           vec_off = symtab->data[i]->index_offset;
14055         }
14056       else
14057         {
14058           /* While 0 is a valid constant pool index, it is not valid
14059              to have 0 for both offsets.  */
14060           str_off = 0;
14061           vec_off = 0;
14062         }
14063
14064       str_off = MAYBE_SWAP (str_off);
14065       vec_off = MAYBE_SWAP (vec_off);
14066
14067       obstack_grow (output, &str_off, sizeof (str_off));
14068       obstack_grow (output, &vec_off, sizeof (vec_off));
14069     }
14070
14071   htab_delete (str_table);
14072   htab_delete (index_table);
14073 }
14074
14075 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
14076    from PST; CU_INDEX is the index of the CU in the vector of all
14077    CUs.  */
14078 static void
14079 add_address_entry (struct objfile *objfile,
14080                    struct obstack *addr_obstack, struct partial_symtab *pst,
14081                    unsigned int cu_index)
14082 {
14083   offset_type offset;
14084   char addr[8];
14085   CORE_ADDR baseaddr;
14086
14087   /* Don't bother recording empty ranges.  */
14088   if (pst->textlow == pst->texthigh)
14089     return;
14090
14091   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14092
14093   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14094   obstack_grow (addr_obstack, addr, 8);
14095   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14096   obstack_grow (addr_obstack, addr, 8);
14097   offset = MAYBE_SWAP (cu_index);
14098   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14099 }
14100
14101 /* Add a list of partial symbols to SYMTAB.  */
14102 static void
14103 write_psymbols (struct mapped_symtab *symtab,
14104                 struct partial_symbol **psymp,
14105                 int count,
14106                 offset_type cu_index)
14107 {
14108   for (; count-- > 0; ++psymp)
14109     {
14110       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14111         error (_("Ada is not currently supported by the index"));
14112       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14113     }
14114 }
14115
14116 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
14117    exception if there is an error.  */
14118 static void
14119 write_obstack (FILE *file, struct obstack *obstack)
14120 {
14121   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14122               file)
14123       != obstack_object_size (obstack))
14124     error (_("couldn't data write to file"));
14125 }
14126
14127 /* Unlink a file if the argument is not NULL.  */
14128 static void
14129 unlink_if_set (void *p)
14130 {
14131   char **filename = p;
14132   if (*filename)
14133     unlink (*filename);
14134 }
14135
14136 /* A helper struct used when iterating over debug_types.  */
14137 struct signatured_type_index_data
14138 {
14139   struct objfile *objfile;
14140   struct mapped_symtab *symtab;
14141   struct obstack *types_list;
14142   int cu_index;
14143 };
14144
14145 /* A helper function that writes a single signatured_type to an
14146    obstack.  */
14147 static int
14148 write_one_signatured_type (void **slot, void *d)
14149 {
14150   struct signatured_type_index_data *info = d;
14151   struct signatured_type *entry = (struct signatured_type *) *slot;
14152   struct dwarf2_per_cu_data *cu = &entry->per_cu;
14153   struct partial_symtab *psymtab = cu->v.psymtab;
14154   gdb_byte val[8];
14155
14156   write_psymbols (info->symtab,
14157                   info->objfile->global_psymbols.list + psymtab->globals_offset,
14158                   psymtab->n_global_syms, info->cu_index);
14159   write_psymbols (info->symtab,
14160                   info->objfile->static_psymbols.list + psymtab->statics_offset,
14161                   psymtab->n_static_syms, info->cu_index);
14162
14163   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14164   obstack_grow (info->types_list, val, 8);
14165   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14166   obstack_grow (info->types_list, val, 8);
14167   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14168   obstack_grow (info->types_list, val, 8);
14169
14170   ++info->cu_index;
14171
14172   return 1;
14173 }
14174
14175 /* Create an index file for OBJFILE in the directory DIR.  */
14176 static void
14177 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14178 {
14179   struct cleanup *cleanup;
14180   char *filename, *cleanup_filename;
14181   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14182   struct obstack cu_list, types_cu_list;
14183   int i;
14184   FILE *out_file;
14185   struct mapped_symtab *symtab;
14186   offset_type val, size_of_contents, total_len;
14187   struct stat st;
14188   char buf[8];
14189
14190   if (!objfile->psymtabs)
14191     return;
14192   if (dwarf2_per_objfile->using_index)
14193     error (_("Cannot use an index to create the index"));
14194
14195   if (stat (objfile->name, &st) < 0)
14196     perror_with_name (_("Could not stat"));
14197
14198   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14199                      INDEX_SUFFIX, (char *) NULL);
14200   cleanup = make_cleanup (xfree, filename);
14201
14202   out_file = fopen (filename, "wb");
14203   if (!out_file)
14204     error (_("Can't open `%s' for writing"), filename);
14205
14206   cleanup_filename = filename;
14207   make_cleanup (unlink_if_set, &cleanup_filename);
14208
14209   symtab = create_mapped_symtab ();
14210   make_cleanup (cleanup_mapped_symtab, symtab);
14211
14212   obstack_init (&addr_obstack);
14213   make_cleanup_obstack_free (&addr_obstack);
14214
14215   obstack_init (&cu_list);
14216   make_cleanup_obstack_free (&cu_list);
14217
14218   obstack_init (&types_cu_list);
14219   make_cleanup_obstack_free (&types_cu_list);
14220
14221   /* The list is already sorted, so we don't need to do additional
14222      work here.  Also, the debug_types entries do not appear in
14223      all_comp_units, but only in their own hash table.  */
14224   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14225     {
14226       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14227       struct partial_symtab *psymtab = cu->v.psymtab;
14228       gdb_byte val[8];
14229
14230       write_psymbols (symtab,
14231                       objfile->global_psymbols.list + psymtab->globals_offset,
14232                       psymtab->n_global_syms, i);
14233       write_psymbols (symtab,
14234                       objfile->static_psymbols.list + psymtab->statics_offset,
14235                       psymtab->n_static_syms, i);
14236
14237       add_address_entry (objfile, &addr_obstack, psymtab, i);
14238
14239       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14240       obstack_grow (&cu_list, val, 8);
14241       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14242       obstack_grow (&cu_list, val, 8);
14243     }
14244
14245   /* Write out the .debug_type entries, if any.  */
14246   if (dwarf2_per_objfile->signatured_types)
14247     {
14248       struct signatured_type_index_data sig_data;
14249
14250       sig_data.objfile = objfile;
14251       sig_data.symtab = symtab;
14252       sig_data.types_list = &types_cu_list;
14253       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14254       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14255                               write_one_signatured_type, &sig_data);
14256     }
14257
14258   obstack_init (&constant_pool);
14259   make_cleanup_obstack_free (&constant_pool);
14260   obstack_init (&symtab_obstack);
14261   make_cleanup_obstack_free (&symtab_obstack);
14262   write_hash_table (symtab, &symtab_obstack, &constant_pool);
14263
14264   obstack_init (&contents);
14265   make_cleanup_obstack_free (&contents);
14266   size_of_contents = 6 * sizeof (offset_type);
14267   total_len = size_of_contents;
14268
14269   /* The version number.  */
14270   val = MAYBE_SWAP (2);
14271   obstack_grow (&contents, &val, sizeof (val));
14272
14273   /* The offset of the CU list from the start of the file.  */
14274   val = MAYBE_SWAP (total_len);
14275   obstack_grow (&contents, &val, sizeof (val));
14276   total_len += obstack_object_size (&cu_list);
14277
14278   /* The offset of the types CU list from the start of the file.  */
14279   val = MAYBE_SWAP (total_len);
14280   obstack_grow (&contents, &val, sizeof (val));
14281   total_len += obstack_object_size (&types_cu_list);
14282
14283   /* The offset of the address table from the start of the file.  */
14284   val = MAYBE_SWAP (total_len);
14285   obstack_grow (&contents, &val, sizeof (val));
14286   total_len += obstack_object_size (&addr_obstack);
14287
14288   /* The offset of the symbol table from the start of the file.  */
14289   val = MAYBE_SWAP (total_len);
14290   obstack_grow (&contents, &val, sizeof (val));
14291   total_len += obstack_object_size (&symtab_obstack);
14292
14293   /* The offset of the constant pool from the start of the file.  */
14294   val = MAYBE_SWAP (total_len);
14295   obstack_grow (&contents, &val, sizeof (val));
14296   total_len += obstack_object_size (&constant_pool);
14297
14298   gdb_assert (obstack_object_size (&contents) == size_of_contents);
14299
14300   write_obstack (out_file, &contents);
14301   write_obstack (out_file, &cu_list);
14302   write_obstack (out_file, &types_cu_list);
14303   write_obstack (out_file, &addr_obstack);
14304   write_obstack (out_file, &symtab_obstack);
14305   write_obstack (out_file, &constant_pool);
14306
14307   fclose (out_file);
14308
14309   /* We want to keep the file, so we set cleanup_filename to NULL
14310      here.  See unlink_if_set.  */
14311   cleanup_filename = NULL;
14312
14313   do_cleanups (cleanup);
14314 }
14315
14316 /* The mapped index file format is designed to be directly mmap()able
14317    on any architecture.  In most cases, a datum is represented using a
14318    little-endian 32-bit integer value, called an offset_type.  Big
14319    endian machines must byte-swap the values before using them.
14320    Exceptions to this rule are noted.  The data is laid out such that
14321    alignment is always respected.
14322
14323    A mapped index consists of several sections.
14324
14325    1. The file header.  This is a sequence of values, of offset_type
14326    unless otherwise noted:
14327    [0] The version number.  Currently 1 or 2.  The differences are
14328    noted below.  Version 1 did not account for .debug_types sections;
14329    the presence of a .debug_types section invalidates any version 1
14330    index that may exist.
14331    [1] The offset, from the start of the file, of the CU list.
14332    [1.5] In version 2, the offset, from the start of the file, of the
14333    types CU list.  This offset does not appear in version 1.  Note
14334    that this can be empty, in which case this offset will be equal to
14335    the next offset.
14336    [2] The offset, from the start of the file, of the address section.
14337    [3] The offset, from the start of the file, of the symbol table.
14338    [4] The offset, from the start of the file, of the constant pool.
14339
14340    2. The CU list.  This is a sequence of pairs of 64-bit
14341    little-endian values, sorted by the CU offset.  The first element
14342    in each pair is the offset of a CU in the .debug_info section.  The
14343    second element in each pair is the length of that CU.  References
14344    to a CU elsewhere in the map are done using a CU index, which is
14345    just the 0-based index into this table.  Note that if there are
14346    type CUs, then conceptually CUs and type CUs form a single list for
14347    the purposes of CU indices.
14348
14349    2.5 The types CU list.  This does not appear in a version 1 index.
14350    This is a sequence of triplets of 64-bit little-endian values.  In
14351    a triplet, the first value is the CU offset, the second value is
14352    the type offset in the CU, and the third value is the type
14353    signature.  The types CU list is not sorted.
14354
14355    3. The address section.  The address section consists of a sequence
14356    of address entries.  Each address entry has three elements.
14357    [0] The low address.  This is a 64-bit little-endian value.
14358    [1] The high address.  This is a 64-bit little-endian value.
14359    [2] The CU index.  This is an offset_type value.
14360
14361    4. The symbol table.  This is a hash table.  The size of the hash
14362    table is always a power of 2.  The initial hash and the step are
14363    currently defined by the `find_slot' function.
14364
14365    Each slot in the hash table consists of a pair of offset_type
14366    values.  The first value is the offset of the symbol's name in the
14367    constant pool.  The second value is the offset of the CU vector in
14368    the constant pool.
14369
14370    If both values are 0, then this slot in the hash table is empty.
14371    This is ok because while 0 is a valid constant pool index, it
14372    cannot be a valid index for both a string and a CU vector.
14373
14374    A string in the constant pool is stored as a \0-terminated string,
14375    as you'd expect.
14376
14377    A CU vector in the constant pool is a sequence of offset_type
14378    values.  The first value is the number of CU indices in the vector.
14379    Each subsequent value is the index of a CU in the CU list.  This
14380    element in the hash table is used to indicate which CUs define the
14381    symbol.
14382
14383    5. The constant pool.  This is simply a bunch of bytes.  It is
14384    organized so that alignment is correct: CU vectors are stored
14385    first, followed by strings.  */
14386 static void
14387 save_gdb_index_command (char *arg, int from_tty)
14388 {
14389   struct objfile *objfile;
14390
14391   if (!arg || !*arg)
14392     error (_("usage: save gdb-index DIRECTORY"));
14393
14394   ALL_OBJFILES (objfile)
14395   {
14396     struct stat st;
14397
14398     /* If the objfile does not correspond to an actual file, skip it.  */
14399     if (stat (objfile->name, &st) < 0)
14400       continue;
14401
14402     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14403     if (dwarf2_per_objfile)
14404       {
14405         volatile struct gdb_exception except;
14406
14407         TRY_CATCH (except, RETURN_MASK_ERROR)
14408           {
14409             write_psymtabs_to_index (objfile, arg);
14410           }
14411         if (except.reason < 0)
14412           exception_fprintf (gdb_stderr, except,
14413                              _("Error while writing index for `%s': "),
14414                              objfile->name);
14415       }
14416   }
14417 }
14418
14419 \f
14420
14421 int dwarf2_always_disassemble;
14422
14423 static void
14424 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14425                                 struct cmd_list_element *c, const char *value)
14426 {
14427   fprintf_filtered (file, _("\
14428 Whether to always disassemble DWARF expressions is %s.\n"),
14429                     value);
14430 }
14431
14432 void _initialize_dwarf2_read (void);
14433
14434 void
14435 _initialize_dwarf2_read (void)
14436 {
14437   struct cmd_list_element *c;
14438
14439   dwarf2_objfile_data_key
14440     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
14441
14442   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14443 Set DWARF 2 specific variables.\n\
14444 Configure DWARF 2 variables such as the cache size"),
14445                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14446                   0/*allow-unknown*/, &maintenance_set_cmdlist);
14447
14448   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14449 Show DWARF 2 specific variables\n\
14450 Show DWARF 2 variables such as the cache size"),
14451                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14452                   0/*allow-unknown*/, &maintenance_show_cmdlist);
14453
14454   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
14455                             &dwarf2_max_cache_age, _("\
14456 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14457 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14458 A higher limit means that cached compilation units will be stored\n\
14459 in memory longer, and more total memory will be used.  Zero disables\n\
14460 caching, which can slow down startup."),
14461                             NULL,
14462                             show_dwarf2_max_cache_age,
14463                             &set_dwarf2_cmdlist,
14464                             &show_dwarf2_cmdlist);
14465
14466   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14467                            &dwarf2_always_disassemble, _("\
14468 Set whether `info address' always disassembles DWARF expressions."), _("\
14469 Show whether `info address' always disassembles DWARF expressions."), _("\
14470 When enabled, DWARF expressions are always printed in an assembly-like\n\
14471 syntax.  When disabled, expressions will be printed in a more\n\
14472 conversational style, when possible."),
14473                            NULL,
14474                            show_dwarf2_always_disassemble,
14475                            &set_dwarf2_cmdlist,
14476                            &show_dwarf2_cmdlist);
14477
14478   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14479 Set debugging of the dwarf2 DIE reader."), _("\
14480 Show debugging of the dwarf2 DIE reader."), _("\
14481 When enabled (non-zero), DIEs are dumped after they are read in.\n\
14482 The value is the maximum depth to print."),
14483                             NULL,
14484                             NULL,
14485                             &setdebuglist, &showdebuglist);
14486
14487   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
14488                _("Save a .gdb-index file"),
14489                &save_cmdlist);
14490   set_cmd_completer (c, filename_completer);
14491 }
This page took 0.81724 seconds and 4 git commands to generate.