OSDN Git Service

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