OSDN Git Service

* dwarf2read.c (dwarf_attr_name): Add DW_AT_GNU_odr_signature.
[pf3gnuchains/sourceware.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4                  2004, 2005, 2006, 2007, 2008, 2009, 2010
5                  Free Software Foundation, Inc.
6
7    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8    Inc.  with support from Florida State University (under contract
9    with the Ada Joint Program Office), and Silicon Graphics, Inc.
10    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12    support.
13
14    This file is part of GDB.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 3 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h"  /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58
59 #include <fcntl.h>
60 #include "gdb_string.h"
61 #include "gdb_assert.h"
62 #include <sys/types.h>
63 #ifdef HAVE_ZLIB_H
64 #include <zlib.h>
65 #endif
66 #ifdef HAVE_MMAP
67 #include <sys/mman.h>
68 #ifndef MAP_FAILED
69 #define MAP_FAILED ((void *) -1)
70 #endif
71 #endif
72
73 typedef struct symbol *symbolp;
74 DEF_VEC_P (symbolp);
75
76 #if 0
77 /* .debug_info header for a compilation unit
78    Because of alignment constraints, this structure has padding and cannot
79    be mapped directly onto the beginning of the .debug_info section.  */
80 typedef struct comp_unit_header
81   {
82     unsigned int length;        /* length of the .debug_info
83                                    contribution */
84     unsigned short version;     /* version number -- 2 for DWARF
85                                    version 2 */
86     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
87     unsigned char addr_size;    /* byte size of an address -- 4 */
88   }
89 _COMP_UNIT_HEADER;
90 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
91 #endif
92
93 /* .debug_line statement program prologue
94    Because of alignment constraints, this structure has padding and cannot
95    be mapped directly onto the beginning of the .debug_info section.  */
96 typedef struct statement_prologue
97   {
98     unsigned int total_length;  /* byte length of the statement
99                                    information */
100     unsigned short version;     /* version number -- 2 for DWARF
101                                    version 2 */
102     unsigned int prologue_length;       /* # bytes between prologue &
103                                            stmt program */
104     unsigned char minimum_instruction_length;   /* byte size of
105                                                    smallest instr */
106     unsigned char default_is_stmt;      /* initial value of is_stmt
107                                            register */
108     char line_base;
109     unsigned char line_range;
110     unsigned char opcode_base;  /* number assigned to first special
111                                    opcode */
112     unsigned char *standard_opcode_lengths;
113   }
114 _STATEMENT_PROLOGUE;
115
116 /* When non-zero, dump DIEs after they are read in.  */
117 static int dwarf2_die_debug = 0;
118
119 static int pagesize;
120
121 /* When set, the file that we're processing is known to have debugging
122    info for C++ namespaces.  GCC 3.3.x did not produce this information,
123    but later versions do.  */
124
125 static int processing_has_namespace_info;
126
127 static const struct objfile_data *dwarf2_objfile_data_key;
128
129 struct dwarf2_section_info
130 {
131   asection *asection;
132   gdb_byte *buffer;
133   bfd_size_type size;
134   int was_mmapped;
135   /* True if we have tried to read this section.  */
136   int readin;
137 };
138
139 /* All offsets in the index are of this type.  It must be
140    architecture-independent.  */
141 typedef uint32_t offset_type;
142
143 DEF_VEC_I (offset_type);
144
145 /* A description of the mapped index.  The file format is described in
146    a comment by the code that writes the index.  */
147 struct mapped_index
148 {
149   /* The total length of the buffer.  */
150   off_t total_size;
151   /* A pointer to the address table data.  */
152   const gdb_byte *address_table;
153   /* Size of the address table data in bytes.  */
154   offset_type address_table_size;
155   /* The hash table.  */
156   const offset_type *index_table;
157   /* Size in slots, each slot is 2 offset_types.  */
158   offset_type index_table_slots;
159   /* A pointer to the constant pool.  */
160   const char *constant_pool;
161 };
162
163 struct dwarf2_per_objfile
164 {
165   struct dwarf2_section_info info;
166   struct dwarf2_section_info abbrev;
167   struct dwarf2_section_info line;
168   struct dwarf2_section_info loc;
169   struct dwarf2_section_info macinfo;
170   struct dwarf2_section_info str;
171   struct dwarf2_section_info ranges;
172   struct dwarf2_section_info types;
173   struct dwarf2_section_info frame;
174   struct dwarf2_section_info eh_frame;
175   struct dwarf2_section_info gdb_index;
176
177   /* Back link.  */
178   struct objfile *objfile;
179
180   /* A list of all the compilation units.  This is used to locate
181      the target compilation unit of a particular reference.  */
182   struct dwarf2_per_cu_data **all_comp_units;
183
184   /* The number of compilation units in ALL_COMP_UNITS.  */
185   int n_comp_units;
186
187   /* The number of .debug_types-related CUs.  */
188   int n_type_comp_units;
189
190   /* The .debug_types-related CUs.  */
191   struct dwarf2_per_cu_data **type_comp_units;
192
193   /* A chain of compilation units that are currently read in, so that
194      they can be freed later.  */
195   struct dwarf2_per_cu_data *read_in_chain;
196
197   /* A table mapping .debug_types signatures to its signatured_type entry.
198      This is NULL if the .debug_types section hasn't been read in yet.  */
199   htab_t signatured_types;
200
201   /* A flag indicating wether this objfile has a section loaded at a
202      VMA of 0.  */
203   int has_section_at_zero;
204
205   /* True if we are using the mapped index.  */
206   unsigned char using_index;
207
208   /* The mapped index.  */
209   struct mapped_index *index_table;
210 };
211
212 static struct dwarf2_per_objfile *dwarf2_per_objfile;
213
214 /* names of the debugging sections */
215
216 /* Note that if the debugging section has been compressed, it might
217    have a name like .zdebug_info.  */
218
219 #define INFO_SECTION     "debug_info"
220 #define ABBREV_SECTION   "debug_abbrev"
221 #define LINE_SECTION     "debug_line"
222 #define LOC_SECTION      "debug_loc"
223 #define MACINFO_SECTION  "debug_macinfo"
224 #define STR_SECTION      "debug_str"
225 #define RANGES_SECTION   "debug_ranges"
226 #define TYPES_SECTION    "debug_types"
227 #define FRAME_SECTION    "debug_frame"
228 #define EH_FRAME_SECTION "eh_frame"
229 #define GDB_INDEX_SECTION "gdb_index"
230
231 /* local data types */
232
233 /* We hold several abbreviation tables in memory at the same time. */
234 #ifndef ABBREV_HASH_SIZE
235 #define ABBREV_HASH_SIZE 121
236 #endif
237
238 /* The data in a compilation unit header, after target2host
239    translation, looks like this.  */
240 struct comp_unit_head
241 {
242   unsigned int length;
243   short version;
244   unsigned char addr_size;
245   unsigned char signed_addr_p;
246   unsigned int abbrev_offset;
247
248   /* Size of file offsets; either 4 or 8.  */
249   unsigned int offset_size;
250
251   /* Size of the length field; either 4 or 12.  */
252   unsigned int initial_length_size;
253
254   /* Offset to the first byte of this compilation unit header in the
255      .debug_info section, for resolving relative reference dies.  */
256   unsigned int offset;
257
258   /* Offset to first die in this cu from the start of the cu.
259      This will be the first byte following the compilation unit header.  */
260   unsigned int first_die_offset;
261 };
262
263 /* Internal state when decoding a particular compilation unit.  */
264 struct dwarf2_cu
265 {
266   /* The objfile containing this compilation unit.  */
267   struct objfile *objfile;
268
269   /* The header of the compilation unit.  */
270   struct comp_unit_head header;
271
272   /* Base address of this compilation unit.  */
273   CORE_ADDR base_address;
274
275   /* Non-zero if base_address has been set.  */
276   int base_known;
277
278   struct function_range *first_fn, *last_fn, *cached_fn;
279
280   /* The language we are debugging.  */
281   enum language language;
282   const struct language_defn *language_defn;
283
284   const char *producer;
285
286   /* The generic symbol table building routines have separate lists for
287      file scope symbols and all all other scopes (local scopes).  So
288      we need to select the right one to pass to add_symbol_to_list().
289      We do it by keeping a pointer to the correct list in list_in_scope.
290
291      FIXME: The original dwarf code just treated the file scope as the
292      first local scope, and all other local scopes as nested local
293      scopes, and worked fine.  Check to see if we really need to
294      distinguish these in buildsym.c.  */
295   struct pending **list_in_scope;
296
297   /* DWARF abbreviation table associated with this compilation unit.  */
298   struct abbrev_info **dwarf2_abbrevs;
299
300   /* Storage for the abbrev table.  */
301   struct obstack abbrev_obstack;
302
303   /* Hash table holding all the loaded partial DIEs.  */
304   htab_t partial_dies;
305
306   /* Storage for things with the same lifetime as this read-in compilation
307      unit, including partial DIEs.  */
308   struct obstack comp_unit_obstack;
309
310   /* When multiple dwarf2_cu structures are living in memory, this field
311      chains them all together, so that they can be released efficiently.
312      We will probably also want a generation counter so that most-recently-used
313      compilation units are cached...  */
314   struct dwarf2_per_cu_data *read_in_chain;
315
316   /* Backchain to our per_cu entry if the tree has been built.  */
317   struct dwarf2_per_cu_data *per_cu;
318
319   /* Pointer to the die -> type map.  Although it is stored
320      permanently in per_cu, we copy it here to avoid double
321      indirection.  */
322   htab_t type_hash;
323
324   /* How many compilation units ago was this CU last referenced?  */
325   int last_used;
326
327   /* A hash table of die offsets for following references.  */
328   htab_t die_hash;
329
330   /* Full DIEs if read in.  */
331   struct die_info *dies;
332
333   /* A set of pointers to dwarf2_per_cu_data objects for compilation
334      units referenced by this one.  Only set during full symbol processing;
335      partial symbol tables do not have dependencies.  */
336   htab_t dependencies;
337
338   /* Header data from the line table, during full symbol processing.  */
339   struct line_header *line_header;
340
341   /* Mark used when releasing cached dies.  */
342   unsigned int mark : 1;
343
344   /* This flag will be set if this compilation unit might include
345      inter-compilation-unit references.  */
346   unsigned int has_form_ref_addr : 1;
347
348   /* This flag will be set if this compilation unit includes any
349      DW_TAG_namespace DIEs.  If we know that there are explicit
350      DIEs for namespaces, we don't need to try to infer them
351      from mangled names.  */
352   unsigned int has_namespace_info : 1;
353 };
354
355 /* When using the index (and thus not using psymtabs), each CU has an
356    object of this type.  This is used to hold information needed by
357    the various "quick" methods.  */
358 struct dwarf2_per_cu_quick_data
359 {
360   /* The line table.  This can be NULL if there was no line table.  */
361   struct line_header *lines;
362
363   /* The file names from the line table.  */
364   const char **file_names;
365   /* The file names from the line table after being run through
366      gdb_realpath.  */
367   const char **full_names;
368
369   /* The corresponding symbol table.  This is NULL if symbols for this
370      CU have not yet been read.  */
371   struct symtab *symtab;
372
373   /* A temporary mark bit used when iterating over all CUs in
374      expand_symtabs_matching.  */
375   unsigned int mark : 1;
376
377   /* True if we've tried to read the line table.  */
378   unsigned int read_lines : 1;
379 };
380
381 /* Persistent data held for a compilation unit, even when not
382    processing it.  We put a pointer to this structure in the
383    read_symtab_private field of the psymtab.  If we encounter
384    inter-compilation-unit references, we also maintain a sorted
385    list of all compilation units.  */
386
387 struct dwarf2_per_cu_data
388 {
389   /* The start offset and length of this compilation unit.  2**29-1
390      bytes should suffice to store the length of any compilation unit
391      - if it doesn't, GDB will fall over anyway.
392      NOTE: Unlike comp_unit_head.length, this length includes
393      initial_length_size.  */
394   unsigned int offset;
395   unsigned int length : 29;
396
397   /* Flag indicating this compilation unit will be read in before
398      any of the current compilation units are processed.  */
399   unsigned int queued : 1;
400
401   /* This flag will be set if we need to load absolutely all DIEs
402      for this compilation unit, instead of just the ones we think
403      are interesting.  It gets set if we look for a DIE in the
404      hash table and don't find it.  */
405   unsigned int load_all_dies : 1;
406
407   /* Non-zero if this CU is from .debug_types.
408      Otherwise it's from .debug_info.  */
409   unsigned int from_debug_types : 1;
410
411   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
412      of the CU cache it gets reset to NULL again.  */
413   struct dwarf2_cu *cu;
414
415   /* If full symbols for this CU have been read in, then this field
416      holds a map of DIE offsets to types.  It isn't always possible
417      to reconstruct this information later, so we have to preserve
418      it.  */
419   htab_t type_hash;
420
421   /* The corresponding objfile.  */
422   struct objfile *objfile;
423
424   /* When using partial symbol tables, the 'psymtab' field is active.
425      Otherwise the 'quick' field is active.  */
426   union
427   {
428     /* The partial symbol table associated with this compilation unit,
429        or NULL for partial units (which do not have an associated
430        symtab).  */
431     struct partial_symtab *psymtab;
432
433     /* Data needed by the "quick" functions.  */
434     struct dwarf2_per_cu_quick_data *quick;
435   } v;
436 };
437
438 /* Entry in the signatured_types hash table.  */
439
440 struct signatured_type
441 {
442   ULONGEST signature;
443
444   /* Offset in .debug_types of the TU (type_unit) for this type.  */
445   unsigned int offset;
446
447   /* Offset in .debug_types of the type defined by this TU.  */
448   unsigned int type_offset;
449
450   /* The CU(/TU) of this type.  */
451   struct dwarf2_per_cu_data per_cu;
452 };
453
454 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
455    which are used for both .debug_info and .debug_types dies.
456    All parameters here are unchanging for the life of the call.
457    This struct exists to abstract away the constant parameters of
458    die reading.  */
459
460 struct die_reader_specs
461 {
462   /* The bfd of this objfile.  */
463   bfd* abfd;
464
465   /* The CU of the DIE we are parsing.  */
466   struct dwarf2_cu *cu;
467
468   /* Pointer to start of section buffer.
469      This is either the start of .debug_info or .debug_types.  */
470   const gdb_byte *buffer;
471 };
472
473 /* The line number information for a compilation unit (found in the
474    .debug_line section) begins with a "statement program header",
475    which contains the following information.  */
476 struct line_header
477 {
478   unsigned int total_length;
479   unsigned short version;
480   unsigned int header_length;
481   unsigned char minimum_instruction_length;
482   unsigned char maximum_ops_per_instruction;
483   unsigned char default_is_stmt;
484   int line_base;
485   unsigned char line_range;
486   unsigned char opcode_base;
487
488   /* standard_opcode_lengths[i] is the number of operands for the
489      standard opcode whose value is i.  This means that
490      standard_opcode_lengths[0] is unused, and the last meaningful
491      element is standard_opcode_lengths[opcode_base - 1].  */
492   unsigned char *standard_opcode_lengths;
493
494   /* The include_directories table.  NOTE!  These strings are not
495      allocated with xmalloc; instead, they are pointers into
496      debug_line_buffer.  If you try to free them, `free' will get
497      indigestion.  */
498   unsigned int num_include_dirs, include_dirs_size;
499   char **include_dirs;
500
501   /* The file_names table.  NOTE!  These strings are not allocated
502      with xmalloc; instead, they are pointers into debug_line_buffer.
503      Don't try to free them directly.  */
504   unsigned int num_file_names, file_names_size;
505   struct file_entry
506   {
507     char *name;
508     unsigned int dir_index;
509     unsigned int mod_time;
510     unsigned int length;
511     int included_p; /* Non-zero if referenced by the Line Number Program.  */
512     struct symtab *symtab; /* The associated symbol table, if any.  */
513   } *file_names;
514
515   /* The start and end of the statement program following this
516      header.  These point into dwarf2_per_objfile->line_buffer.  */
517   gdb_byte *statement_program_start, *statement_program_end;
518 };
519
520 /* When we construct a partial symbol table entry we only
521    need this much information. */
522 struct partial_die_info
523   {
524     /* Offset of this DIE.  */
525     unsigned int offset;
526
527     /* DWARF-2 tag for this DIE.  */
528     ENUM_BITFIELD(dwarf_tag) tag : 16;
529
530     /* Assorted flags describing the data found in this DIE.  */
531     unsigned int has_children : 1;
532     unsigned int is_external : 1;
533     unsigned int is_declaration : 1;
534     unsigned int has_type : 1;
535     unsigned int has_specification : 1;
536     unsigned int has_pc_info : 1;
537
538     /* Flag set if the SCOPE field of this structure has been
539        computed.  */
540     unsigned int scope_set : 1;
541
542     /* Flag set if the DIE has a byte_size attribute.  */
543     unsigned int has_byte_size : 1;
544
545     /* The name of this DIE.  Normally the value of DW_AT_name, but
546        sometimes a default name for unnamed DIEs.  */
547     char *name;
548
549     /* The scope to prepend to our children.  This is generally
550        allocated on the comp_unit_obstack, so will disappear
551        when this compilation unit leaves the cache.  */
552     char *scope;
553
554     /* The location description associated with this DIE, if any.  */
555     struct dwarf_block *locdesc;
556
557     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
558     CORE_ADDR lowpc;
559     CORE_ADDR highpc;
560
561     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
562        DW_AT_sibling, if any.  */
563     gdb_byte *sibling;
564
565     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
566        DW_AT_specification (or DW_AT_abstract_origin or
567        DW_AT_extension).  */
568     unsigned int spec_offset;
569
570     /* Pointers to this DIE's parent, first child, and next sibling,
571        if any.  */
572     struct partial_die_info *die_parent, *die_child, *die_sibling;
573   };
574
575 /* This data structure holds the information of an abbrev. */
576 struct abbrev_info
577   {
578     unsigned int number;        /* number identifying abbrev */
579     enum dwarf_tag tag;         /* dwarf tag */
580     unsigned short has_children;                /* boolean */
581     unsigned short num_attrs;   /* number of attributes */
582     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
583     struct abbrev_info *next;   /* next in chain */
584   };
585
586 struct attr_abbrev
587   {
588     ENUM_BITFIELD(dwarf_attribute) name : 16;
589     ENUM_BITFIELD(dwarf_form) form : 16;
590   };
591
592 /* Attributes have a name and a value */
593 struct attribute
594   {
595     ENUM_BITFIELD(dwarf_attribute) name : 16;
596     ENUM_BITFIELD(dwarf_form) form : 15;
597
598     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
599        field should be in u.str (existing only for DW_STRING) but it is kept
600        here for better struct attribute alignment.  */
601     unsigned int string_is_canonical : 1;
602
603     union
604       {
605         char *str;
606         struct dwarf_block *blk;
607         ULONGEST unsnd;
608         LONGEST snd;
609         CORE_ADDR addr;
610         struct signatured_type *signatured_type;
611       }
612     u;
613   };
614
615 /* This data structure holds a complete die structure. */
616 struct die_info
617   {
618     /* DWARF-2 tag for this DIE.  */
619     ENUM_BITFIELD(dwarf_tag) tag : 16;
620
621     /* Number of attributes */
622     unsigned short num_attrs;
623
624     /* Abbrev number */
625     unsigned int abbrev;
626
627     /* Offset in .debug_info or .debug_types section.  */
628     unsigned int offset;
629
630     /* The dies in a compilation unit form an n-ary tree.  PARENT
631        points to this die's parent; CHILD points to the first child of
632        this node; and all the children of a given node are chained
633        together via their SIBLING fields, terminated by a die whose
634        tag is zero.  */
635     struct die_info *child;     /* Its first child, if any.  */
636     struct die_info *sibling;   /* Its next sibling, if any.  */
637     struct die_info *parent;    /* Its parent, if any.  */
638
639     /* An array of attributes, with NUM_ATTRS elements.  There may be
640        zero, but it's not common and zero-sized arrays are not
641        sufficiently portable C.  */
642     struct attribute attrs[1];
643   };
644
645 struct function_range
646 {
647   const char *name;
648   CORE_ADDR lowpc, highpc;
649   int seen_line;
650   struct function_range *next;
651 };
652
653 /* Get at parts of an attribute structure */
654
655 #define DW_STRING(attr)    ((attr)->u.str)
656 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
657 #define DW_UNSND(attr)     ((attr)->u.unsnd)
658 #define DW_BLOCK(attr)     ((attr)->u.blk)
659 #define DW_SND(attr)       ((attr)->u.snd)
660 #define DW_ADDR(attr)      ((attr)->u.addr)
661 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
662
663 /* Blocks are a bunch of untyped bytes. */
664 struct dwarf_block
665   {
666     unsigned int size;
667     gdb_byte *data;
668   };
669
670 #ifndef ATTR_ALLOC_CHUNK
671 #define ATTR_ALLOC_CHUNK 4
672 #endif
673
674 /* Allocate fields for structs, unions and enums in this size.  */
675 #ifndef DW_FIELD_ALLOC_CHUNK
676 #define DW_FIELD_ALLOC_CHUNK 4
677 #endif
678
679 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
680    but this would require a corresponding change in unpack_field_as_long
681    and friends.  */
682 static int bits_per_byte = 8;
683
684 /* The routines that read and process dies for a C struct or C++ class
685    pass lists of data member fields and lists of member function fields
686    in an instance of a field_info structure, as defined below.  */
687 struct field_info
688   {
689     /* List of data member and baseclasses fields. */
690     struct nextfield
691       {
692         struct nextfield *next;
693         int accessibility;
694         int virtuality;
695         struct field field;
696       }
697      *fields, *baseclasses;
698
699     /* Number of fields (including baseclasses).  */
700     int nfields;
701
702     /* Number of baseclasses.  */
703     int nbaseclasses;
704
705     /* Set if the accesibility of one of the fields is not public.  */
706     int non_public_fields;
707
708     /* Member function fields array, entries are allocated in the order they
709        are encountered in the object file.  */
710     struct nextfnfield
711       {
712         struct nextfnfield *next;
713         struct fn_field fnfield;
714       }
715      *fnfields;
716
717     /* Member function fieldlist array, contains name of possibly overloaded
718        member function, number of overloaded member functions and a pointer
719        to the head of the member function field chain.  */
720     struct fnfieldlist
721       {
722         char *name;
723         int length;
724         struct nextfnfield *head;
725       }
726      *fnfieldlists;
727
728     /* Number of entries in the fnfieldlists array.  */
729     int nfnfields;
730
731     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
732        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
733     struct typedef_field_list
734       {
735         struct typedef_field field;
736         struct typedef_field_list *next;
737       }
738     *typedef_field_list;
739     unsigned typedef_field_list_count;
740   };
741
742 /* One item on the queue of compilation units to read in full symbols
743    for.  */
744 struct dwarf2_queue_item
745 {
746   struct dwarf2_per_cu_data *per_cu;
747   struct dwarf2_queue_item *next;
748 };
749
750 /* The current queue.  */
751 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
752
753 /* Loaded secondary compilation units are kept in memory until they
754    have not been referenced for the processing of this many
755    compilation units.  Set this to zero to disable caching.  Cache
756    sizes of up to at least twenty will improve startup time for
757    typical inter-CU-reference binaries, at an obvious memory cost.  */
758 static int dwarf2_max_cache_age = 5;
759 static void
760 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
761                            struct cmd_list_element *c, const char *value)
762 {
763   fprintf_filtered (file, _("\
764 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
765                     value);
766 }
767
768
769 /* Various complaints about symbol reading that don't abort the process */
770
771 static void
772 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
773 {
774   complaint (&symfile_complaints,
775              _("statement list doesn't fit in .debug_line section"));
776 }
777
778 static void
779 dwarf2_debug_line_missing_file_complaint (void)
780 {
781   complaint (&symfile_complaints,
782              _(".debug_line section has line data without a file"));
783 }
784
785 static void
786 dwarf2_debug_line_missing_end_sequence_complaint (void)
787 {
788   complaint (&symfile_complaints,
789              _(".debug_line section has line program sequence without an end"));
790 }
791
792 static void
793 dwarf2_complex_location_expr_complaint (void)
794 {
795   complaint (&symfile_complaints, _("location expression too complex"));
796 }
797
798 static void
799 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
800                                               int arg3)
801 {
802   complaint (&symfile_complaints,
803              _("const value length mismatch for '%s', got %d, expected %d"), arg1,
804              arg2, arg3);
805 }
806
807 static void
808 dwarf2_macros_too_long_complaint (void)
809 {
810   complaint (&symfile_complaints,
811              _("macro info runs off end of `.debug_macinfo' section"));
812 }
813
814 static void
815 dwarf2_macro_malformed_definition_complaint (const char *arg1)
816 {
817   complaint (&symfile_complaints,
818              _("macro debug info contains a malformed macro definition:\n`%s'"),
819              arg1);
820 }
821
822 static void
823 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
824 {
825   complaint (&symfile_complaints,
826              _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
827 }
828
829 /* local function prototypes */
830
831 static void dwarf2_locate_sections (bfd *, asection *, void *);
832
833 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
834                                            struct objfile *);
835
836 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
837                                            struct die_info *,
838                                            struct partial_symtab *);
839
840 static void dwarf2_build_psymtabs_hard (struct objfile *);
841
842 static void scan_partial_symbols (struct partial_die_info *,
843                                   CORE_ADDR *, CORE_ADDR *,
844                                   int, struct dwarf2_cu *);
845
846 static void add_partial_symbol (struct partial_die_info *,
847                                 struct dwarf2_cu *);
848
849 static void add_partial_namespace (struct partial_die_info *pdi,
850                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
851                                    int need_pc, struct dwarf2_cu *cu);
852
853 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
854                                 CORE_ADDR *highpc, int need_pc,
855                                 struct dwarf2_cu *cu);
856
857 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
858                                      struct dwarf2_cu *cu);
859
860 static void add_partial_subprogram (struct partial_die_info *pdi,
861                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
862                                     int need_pc, struct dwarf2_cu *cu);
863
864 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
865                                      gdb_byte *buffer, gdb_byte *info_ptr,
866                                      bfd *abfd, struct dwarf2_cu *cu);
867
868 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
869
870 static void psymtab_to_symtab_1 (struct partial_symtab *);
871
872 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
873
874 static void dwarf2_free_abbrev_table (void *);
875
876 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
877                                             struct dwarf2_cu *);
878
879 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
880                                                  struct dwarf2_cu *);
881
882 static struct partial_die_info *load_partial_dies (bfd *,
883                                                    gdb_byte *, gdb_byte *,
884                                                    int, struct dwarf2_cu *);
885
886 static gdb_byte *read_partial_die (struct partial_die_info *,
887                                    struct abbrev_info *abbrev,
888                                    unsigned int, bfd *,
889                                    gdb_byte *, gdb_byte *,
890                                    struct dwarf2_cu *);
891
892 static struct partial_die_info *find_partial_die (unsigned int,
893                                                   struct dwarf2_cu *);
894
895 static void fixup_partial_die (struct partial_die_info *,
896                                struct dwarf2_cu *);
897
898 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
899                                  bfd *, gdb_byte *, struct dwarf2_cu *);
900
901 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
902                                        bfd *, gdb_byte *, struct dwarf2_cu *);
903
904 static unsigned int read_1_byte (bfd *, gdb_byte *);
905
906 static int read_1_signed_byte (bfd *, gdb_byte *);
907
908 static unsigned int read_2_bytes (bfd *, gdb_byte *);
909
910 static unsigned int read_4_bytes (bfd *, gdb_byte *);
911
912 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
913
914 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
915                                unsigned int *);
916
917 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
918
919 static LONGEST read_checked_initial_length_and_offset
920   (bfd *, gdb_byte *, const struct comp_unit_head *,
921    unsigned int *, unsigned int *);
922
923 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
924                             unsigned int *);
925
926 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
927
928 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
929
930 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
931
932 static char *read_indirect_string (bfd *, gdb_byte *,
933                                    const struct comp_unit_head *,
934                                    unsigned int *);
935
936 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
937
938 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
939
940 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
941
942 static void set_cu_language (unsigned int, struct dwarf2_cu *);
943
944 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
945                                       struct dwarf2_cu *);
946
947 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
948                                                 unsigned int,
949                                                 struct dwarf2_cu *);
950
951 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
952                                struct dwarf2_cu *cu);
953
954 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
955
956 static struct die_info *die_specification (struct die_info *die,
957                                            struct dwarf2_cu **);
958
959 static void free_line_header (struct line_header *lh);
960
961 static void add_file_name (struct line_header *, char *, unsigned int,
962                            unsigned int, unsigned int);
963
964 static struct line_header *(dwarf_decode_line_header
965                             (unsigned int offset,
966                              bfd *abfd, struct dwarf2_cu *cu));
967
968 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
969                                 struct dwarf2_cu *, struct partial_symtab *);
970
971 static void dwarf2_start_subfile (char *, char *, char *);
972
973 static struct symbol *new_symbol (struct die_info *, struct type *,
974                                   struct dwarf2_cu *);
975
976 static struct symbol *new_symbol_full (struct die_info *, struct type *,
977                                        struct dwarf2_cu *, struct symbol *);
978
979 static void dwarf2_const_value (struct attribute *, struct symbol *,
980                                 struct dwarf2_cu *);
981
982 static void dwarf2_const_value_data (struct attribute *attr,
983                                      struct symbol *sym,
984                                      int bits);
985
986 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
987
988 static int need_gnat_info (struct dwarf2_cu *);
989
990 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
991
992 static void set_descriptive_type (struct type *, struct die_info *,
993                                   struct dwarf2_cu *);
994
995 static struct type *die_containing_type (struct die_info *,
996                                          struct dwarf2_cu *);
997
998 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
999
1000 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1001
1002 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1003
1004 static char *typename_concat (struct obstack *obs, const char *prefix,
1005                               const char *suffix, int physname,
1006                               struct dwarf2_cu *cu);
1007
1008 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1009
1010 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1011
1012 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1013
1014 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1015
1016 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1017                                struct dwarf2_cu *, struct partial_symtab *);
1018
1019 static int dwarf2_get_pc_bounds (struct die_info *,
1020                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1021                                  struct partial_symtab *);
1022
1023 static void get_scope_pc_bounds (struct die_info *,
1024                                  CORE_ADDR *, CORE_ADDR *,
1025                                  struct dwarf2_cu *);
1026
1027 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1028                                         CORE_ADDR, struct dwarf2_cu *);
1029
1030 static void dwarf2_add_field (struct field_info *, struct die_info *,
1031                               struct dwarf2_cu *);
1032
1033 static void dwarf2_attach_fields_to_type (struct field_info *,
1034                                           struct type *, struct dwarf2_cu *);
1035
1036 static void dwarf2_add_member_fn (struct field_info *,
1037                                   struct die_info *, struct type *,
1038                                   struct dwarf2_cu *);
1039
1040 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1041                                              struct type *, struct dwarf2_cu *);
1042
1043 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1046
1047 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1048
1049 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1050
1051 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1052
1053 static struct type *read_module_type (struct die_info *die,
1054                                       struct dwarf2_cu *cu);
1055
1056 static const char *namespace_name (struct die_info *die,
1057                                    int *is_anonymous, struct dwarf2_cu *);
1058
1059 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1062
1063 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1064                                                        struct dwarf2_cu *);
1065
1066 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1067
1068 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1069                                                  gdb_byte *info_ptr,
1070                                                  gdb_byte **new_info_ptr,
1071                                                  struct die_info *parent);
1072
1073 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1074                                                gdb_byte *info_ptr,
1075                                                gdb_byte **new_info_ptr,
1076                                                struct die_info *parent);
1077
1078 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1079                                                gdb_byte *info_ptr,
1080                                                gdb_byte **new_info_ptr,
1081                                                struct die_info *parent);
1082
1083 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1084                                 struct die_info **, gdb_byte *,
1085                                 int *);
1086
1087 static void process_die (struct die_info *, struct dwarf2_cu *);
1088
1089 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1090                                        struct obstack *);
1091
1092 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1093
1094 static struct die_info *dwarf2_extension (struct die_info *die,
1095                                           struct dwarf2_cu **);
1096
1097 static char *dwarf_tag_name (unsigned int);
1098
1099 static char *dwarf_attr_name (unsigned int);
1100
1101 static char *dwarf_form_name (unsigned int);
1102
1103 static char *dwarf_bool_name (unsigned int);
1104
1105 static char *dwarf_type_encoding_name (unsigned int);
1106
1107 #if 0
1108 static char *dwarf_cfi_name (unsigned int);
1109 #endif
1110
1111 static struct die_info *sibling_die (struct die_info *);
1112
1113 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1114
1115 static void dump_die_for_error (struct die_info *);
1116
1117 static void dump_die_1 (struct ui_file *, int level, int max_level,
1118                         struct die_info *);
1119
1120 /*static*/ void dump_die (struct die_info *, int max_level);
1121
1122 static void store_in_ref_table (struct die_info *,
1123                                 struct dwarf2_cu *);
1124
1125 static int is_ref_attr (struct attribute *);
1126
1127 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1128
1129 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1130
1131 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1132                                                struct attribute *,
1133                                                struct dwarf2_cu **);
1134
1135 static struct die_info *follow_die_ref (struct die_info *,
1136                                         struct attribute *,
1137                                         struct dwarf2_cu **);
1138
1139 static struct die_info *follow_die_sig (struct die_info *,
1140                                         struct attribute *,
1141                                         struct dwarf2_cu **);
1142
1143 static void read_signatured_type_at_offset (struct objfile *objfile,
1144                                             unsigned int offset);
1145
1146 static void read_signatured_type (struct objfile *,
1147                                   struct signatured_type *type_sig);
1148
1149 /* memory allocation interface */
1150
1151 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1152
1153 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1154
1155 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1156
1157 static void initialize_cu_func_list (struct dwarf2_cu *);
1158
1159 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1160                                  struct dwarf2_cu *);
1161
1162 static void dwarf_decode_macros (struct line_header *, unsigned int,
1163                                  char *, bfd *, struct dwarf2_cu *);
1164
1165 static int attr_form_is_block (struct attribute *);
1166
1167 static int attr_form_is_section_offset (struct attribute *);
1168
1169 static int attr_form_is_constant (struct attribute *);
1170
1171 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1172                                          struct symbol *sym,
1173                                          struct dwarf2_cu *cu);
1174
1175 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1176                                struct abbrev_info *abbrev,
1177                                struct dwarf2_cu *cu);
1178
1179 static void free_stack_comp_unit (void *);
1180
1181 static hashval_t partial_die_hash (const void *item);
1182
1183 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1184
1185 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1186   (unsigned int offset, struct objfile *objfile);
1187
1188 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1189   (unsigned int offset, struct objfile *objfile);
1190
1191 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1192
1193 static void free_one_comp_unit (void *);
1194
1195 static void free_cached_comp_units (void *);
1196
1197 static void age_cached_comp_units (void);
1198
1199 static void free_one_cached_comp_unit (void *);
1200
1201 static struct type *set_die_type (struct die_info *, struct type *,
1202                                   struct dwarf2_cu *);
1203
1204 static void create_all_comp_units (struct objfile *);
1205
1206 static int create_debug_types_hash_table (struct objfile *objfile);
1207
1208 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1209                                  struct objfile *);
1210
1211 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1212
1213 static void dwarf2_add_dependence (struct dwarf2_cu *,
1214                                    struct dwarf2_per_cu_data *);
1215
1216 static void dwarf2_mark (struct dwarf2_cu *);
1217
1218 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1219
1220 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1221
1222 static void dwarf2_release_queue (void *dummy);
1223
1224 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1225                              struct objfile *objfile);
1226
1227 static void process_queue (struct objfile *objfile);
1228
1229 static void find_file_and_directory (struct die_info *die,
1230                                      struct dwarf2_cu *cu,
1231                                      char **name, char **comp_dir);
1232
1233 static char *file_full_name (int file, struct line_header *lh,
1234                              const char *comp_dir);
1235
1236 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1237                                               gdb_byte *info_ptr,
1238                                               gdb_byte *buffer,
1239                                               unsigned int buffer_size,
1240                                               bfd *abfd);
1241
1242 static void init_cu_die_reader (struct die_reader_specs *reader,
1243                                 struct dwarf2_cu *cu);
1244
1245 static htab_t allocate_signatured_type_hash_table (struct objfile *objfile);
1246
1247 #if WORDS_BIGENDIAN
1248
1249 /* Convert VALUE between big- and little-endian.  */
1250 static offset_type
1251 byte_swap (offset_type value)
1252 {
1253   offset_type result;
1254
1255   result = (value & 0xff) << 24;
1256   result |= (value & 0xff00) << 8;
1257   result |= (value & 0xff0000) >> 8;
1258   result |= (value & 0xff000000) >> 24;
1259   return result;
1260 }
1261
1262 #define MAYBE_SWAP(V)  byte_swap (V)
1263
1264 #else
1265 #define MAYBE_SWAP(V) (V)
1266 #endif /* WORDS_BIGENDIAN */
1267
1268 /* The suffix for an index file.  */
1269 #define INDEX_SUFFIX ".gdb-index"
1270
1271 /* Try to locate the sections we need for DWARF 2 debugging
1272    information and return true if we have enough to do something.  */
1273
1274 int
1275 dwarf2_has_info (struct objfile *objfile)
1276 {
1277   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1278   if (!dwarf2_per_objfile)
1279     {
1280       /* Initialize per-objfile state.  */
1281       struct dwarf2_per_objfile *data
1282         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1283
1284       memset (data, 0, sizeof (*data));
1285       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1286       dwarf2_per_objfile = data;
1287
1288       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1289       dwarf2_per_objfile->objfile = objfile;
1290     }
1291   return (dwarf2_per_objfile->info.asection != NULL
1292           && dwarf2_per_objfile->abbrev.asection != NULL);
1293 }
1294
1295 /* When loading sections, we can either look for ".<name>", or for
1296  * ".z<name>", which indicates a compressed section.  */
1297
1298 static int
1299 section_is_p (const char *section_name, const char *name)
1300 {
1301   return (section_name[0] == '.'
1302           && (strcmp (section_name + 1, name) == 0
1303               || (section_name[1] == 'z'
1304                   && strcmp (section_name + 2, name) == 0)));
1305 }
1306
1307 /* This function is mapped across the sections and remembers the
1308    offset and size of each of the debugging sections we are interested
1309    in.  */
1310
1311 static void
1312 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1313 {
1314   if (section_is_p (sectp->name, INFO_SECTION))
1315     {
1316       dwarf2_per_objfile->info.asection = sectp;
1317       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1318     }
1319   else if (section_is_p (sectp->name, ABBREV_SECTION))
1320     {
1321       dwarf2_per_objfile->abbrev.asection = sectp;
1322       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1323     }
1324   else if (section_is_p (sectp->name, LINE_SECTION))
1325     {
1326       dwarf2_per_objfile->line.asection = sectp;
1327       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1328     }
1329   else if (section_is_p (sectp->name, LOC_SECTION))
1330     {
1331       dwarf2_per_objfile->loc.asection = sectp;
1332       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1333     }
1334   else if (section_is_p (sectp->name, MACINFO_SECTION))
1335     {
1336       dwarf2_per_objfile->macinfo.asection = sectp;
1337       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1338     }
1339   else if (section_is_p (sectp->name, STR_SECTION))
1340     {
1341       dwarf2_per_objfile->str.asection = sectp;
1342       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1343     }
1344   else if (section_is_p (sectp->name, FRAME_SECTION))
1345     {
1346       dwarf2_per_objfile->frame.asection = sectp;
1347       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1348     }
1349   else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1350     {
1351       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1352
1353       if (aflag & SEC_HAS_CONTENTS)
1354         {
1355           dwarf2_per_objfile->eh_frame.asection = sectp;
1356           dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1357         }
1358     }
1359   else if (section_is_p (sectp->name, RANGES_SECTION))
1360     {
1361       dwarf2_per_objfile->ranges.asection = sectp;
1362       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1363     }
1364   else if (section_is_p (sectp->name, TYPES_SECTION))
1365     {
1366       dwarf2_per_objfile->types.asection = sectp;
1367       dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1368     }
1369   else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1370     {
1371       dwarf2_per_objfile->gdb_index.asection = sectp;
1372       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1373     }
1374
1375   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1376       && bfd_section_vma (abfd, sectp) == 0)
1377     dwarf2_per_objfile->has_section_at_zero = 1;
1378 }
1379
1380 /* Decompress a section that was compressed using zlib.  Store the
1381    decompressed buffer, and its size, in OUTBUF and OUTSIZE.  */
1382
1383 static void
1384 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1385                          gdb_byte **outbuf, bfd_size_type *outsize)
1386 {
1387   bfd *abfd = objfile->obfd;
1388 #ifndef HAVE_ZLIB_H
1389   error (_("Support for zlib-compressed DWARF data (from '%s') "
1390            "is disabled in this copy of GDB"),
1391          bfd_get_filename (abfd));
1392 #else
1393   bfd_size_type compressed_size = bfd_get_section_size (sectp);
1394   gdb_byte *compressed_buffer = xmalloc (compressed_size);
1395   struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1396   bfd_size_type uncompressed_size;
1397   gdb_byte *uncompressed_buffer;
1398   z_stream strm;
1399   int rc;
1400   int header_size = 12;
1401
1402   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1403       || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1404     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1405            bfd_get_filename (abfd));
1406
1407   /* Read the zlib header.  In this case, it should be "ZLIB" followed
1408      by the uncompressed section size, 8 bytes in big-endian order.  */
1409   if (compressed_size < header_size
1410       || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1411     error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1412            bfd_get_filename (abfd));
1413   uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1414   uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1415   uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1416   uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1417   uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1418   uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1419   uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1420   uncompressed_size += compressed_buffer[11];
1421
1422   /* It is possible the section consists of several compressed
1423      buffers concatenated together, so we uncompress in a loop.  */
1424   strm.zalloc = NULL;
1425   strm.zfree = NULL;
1426   strm.opaque = NULL;
1427   strm.avail_in = compressed_size - header_size;
1428   strm.next_in = (Bytef*) compressed_buffer + header_size;
1429   strm.avail_out = uncompressed_size;
1430   uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1431                                        uncompressed_size);
1432   rc = inflateInit (&strm);
1433   while (strm.avail_in > 0)
1434     {
1435       if (rc != Z_OK)
1436         error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1437                bfd_get_filename (abfd), rc);
1438       strm.next_out = ((Bytef*) uncompressed_buffer
1439                        + (uncompressed_size - strm.avail_out));
1440       rc = inflate (&strm, Z_FINISH);
1441       if (rc != Z_STREAM_END)
1442         error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1443                bfd_get_filename (abfd), rc);
1444       rc = inflateReset (&strm);
1445     }
1446   rc = inflateEnd (&strm);
1447   if (rc != Z_OK
1448       || strm.avail_out != 0)
1449     error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1450            bfd_get_filename (abfd), rc);
1451
1452   do_cleanups (cleanup);
1453   *outbuf = uncompressed_buffer;
1454   *outsize = uncompressed_size;
1455 #endif
1456 }
1457
1458 /* Read the contents of the section SECTP from object file specified by
1459    OBJFILE, store info about the section into INFO.
1460    If the section is compressed, uncompress it before returning.  */
1461
1462 static void
1463 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1464 {
1465   bfd *abfd = objfile->obfd;
1466   asection *sectp = info->asection;
1467   gdb_byte *buf, *retbuf;
1468   unsigned char header[4];
1469
1470   if (info->readin)
1471     return;
1472   info->buffer = NULL;
1473   info->was_mmapped = 0;
1474   info->readin = 1;
1475
1476   if (info->asection == NULL || info->size == 0)
1477     return;
1478
1479   /* Check if the file has a 4-byte header indicating compression.  */
1480   if (info->size > sizeof (header)
1481       && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1482       && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1483     {
1484       /* Upon decompression, update the buffer and its size.  */
1485       if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1486         {
1487           zlib_decompress_section (objfile, sectp, &info->buffer,
1488                                    &info->size);
1489           return;
1490         }
1491     }
1492
1493 #ifdef HAVE_MMAP
1494   if (pagesize == 0)
1495     pagesize = getpagesize ();
1496
1497   /* Only try to mmap sections which are large enough: we don't want to
1498      waste space due to fragmentation.  Also, only try mmap for sections
1499      without relocations.  */
1500
1501   if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1502     {
1503       off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1504       size_t map_length = info->size + sectp->filepos - pg_offset;
1505       caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1506                                  MAP_PRIVATE, pg_offset);
1507
1508       if (retbuf != MAP_FAILED)
1509         {
1510           info->was_mmapped = 1;
1511           info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1512 #if HAVE_POSIX_MADVISE
1513           posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1514 #endif
1515           return;
1516         }
1517     }
1518 #endif
1519
1520   /* If we get here, we are a normal, not-compressed section.  */
1521   info->buffer = buf
1522     = obstack_alloc (&objfile->objfile_obstack, info->size);
1523
1524   /* When debugging .o files, we may need to apply relocations; see
1525      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1526      We never compress sections in .o files, so we only need to
1527      try this when the section is not compressed.  */
1528   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1529   if (retbuf != NULL)
1530     {
1531       info->buffer = retbuf;
1532       return;
1533     }
1534
1535   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1536       || bfd_bread (buf, info->size, abfd) != info->size)
1537     error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1538            bfd_get_filename (abfd));
1539 }
1540
1541 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1542    SECTION_NAME. */
1543
1544 void
1545 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1546                          asection **sectp, gdb_byte **bufp,
1547                          bfd_size_type *sizep)
1548 {
1549   struct dwarf2_per_objfile *data
1550     = objfile_data (objfile, dwarf2_objfile_data_key);
1551   struct dwarf2_section_info *info;
1552
1553   /* We may see an objfile without any DWARF, in which case we just
1554      return nothing.  */
1555   if (data == NULL)
1556     {
1557       *sectp = NULL;
1558       *bufp = NULL;
1559       *sizep = 0;
1560       return;
1561     }
1562   if (section_is_p (section_name, EH_FRAME_SECTION))
1563     info = &data->eh_frame;
1564   else if (section_is_p (section_name, FRAME_SECTION))
1565     info = &data->frame;
1566   else
1567     gdb_assert (0);
1568
1569   if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1570     /* We haven't read this section in yet.  Do it now.  */
1571     dwarf2_read_section (objfile, info);
1572
1573   *sectp = info->asection;
1574   *bufp = info->buffer;
1575   *sizep = info->size;
1576 }
1577
1578 \f
1579
1580 /* Read in the symbols for PER_CU.  OBJFILE is the objfile from which
1581    this CU came.  */
1582 static void
1583 dw2_do_instantiate_symtab (struct objfile *objfile,
1584                            struct dwarf2_per_cu_data *per_cu)
1585 {
1586   struct cleanup *back_to;
1587
1588   back_to = make_cleanup (dwarf2_release_queue, NULL);
1589
1590   queue_comp_unit (per_cu, objfile);
1591
1592   if (per_cu->from_debug_types)
1593     read_signatured_type_at_offset (objfile, per_cu->offset);
1594   else
1595     load_full_comp_unit (per_cu, objfile);
1596
1597   process_queue (objfile);
1598
1599   /* Age the cache, releasing compilation units that have not
1600      been used recently.  */
1601   age_cached_comp_units ();
1602
1603   do_cleanups (back_to);
1604 }
1605
1606 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
1607    the objfile from which this CU came.  Returns the resulting symbol
1608    table.  */
1609 static struct symtab *
1610 dw2_instantiate_symtab (struct objfile *objfile,
1611                         struct dwarf2_per_cu_data *per_cu)
1612 {
1613   if (!per_cu->v.quick->symtab)
1614     {
1615       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1616       increment_reading_symtab ();
1617       dw2_do_instantiate_symtab (objfile, per_cu);
1618       do_cleanups (back_to);
1619     }
1620   return per_cu->v.quick->symtab;
1621 }
1622
1623 /* Return the CU given its index.  */
1624 static struct dwarf2_per_cu_data *
1625 dw2_get_cu (int index)
1626 {
1627   if (index >= dwarf2_per_objfile->n_comp_units)
1628     {
1629       index -= dwarf2_per_objfile->n_comp_units;
1630       return dwarf2_per_objfile->type_comp_units[index];
1631     }
1632   return dwarf2_per_objfile->all_comp_units[index];
1633 }
1634
1635 /* A helper function that knows how to read a 64-bit value in a way
1636    that doesn't make gdb die.  Returns 1 if the conversion went ok, 0
1637    otherwise.  */
1638 static int
1639 extract_cu_value (const char *bytes, ULONGEST *result)
1640 {
1641   if (sizeof (ULONGEST) < 8)
1642     {
1643       int i;
1644
1645       /* Ignore the upper 4 bytes if they are all zero.  */
1646       for (i = 0; i < 4; ++i)
1647         if (bytes[i + 4] != 0)
1648           return 0;
1649
1650       *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1651     }
1652   else
1653     *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1654   return 1;
1655 }
1656
1657 /* Read the CU list from the mapped index, and use it to create all
1658    the CU objects for this objfile.  Return 0 if something went wrong,
1659    1 if everything went ok.  */
1660 static int
1661 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1662                        offset_type cu_list_elements)
1663 {
1664   offset_type i;
1665
1666   dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1667   dwarf2_per_objfile->all_comp_units
1668     = obstack_alloc (&objfile->objfile_obstack,
1669                      dwarf2_per_objfile->n_comp_units
1670                      * sizeof (struct dwarf2_per_cu_data *));
1671
1672   for (i = 0; i < cu_list_elements; i += 2)
1673     {
1674       struct dwarf2_per_cu_data *the_cu;
1675       ULONGEST offset, length;
1676
1677       if (!extract_cu_value (cu_list, &offset)
1678           || !extract_cu_value (cu_list + 8, &length))
1679         return 0;
1680       cu_list += 2 * 8;
1681
1682       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1683                                struct dwarf2_per_cu_data);
1684       the_cu->offset = offset;
1685       the_cu->length = length;
1686       the_cu->objfile = objfile;
1687       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1688                                         struct dwarf2_per_cu_quick_data);
1689       dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1690     }
1691
1692   return 1;
1693 }
1694
1695 /* Create the signatured type hash table from the index.  */
1696 static int
1697 create_signatured_type_hash_from_index (struct objfile *objfile,
1698                                         const gdb_byte *bytes,
1699                                         offset_type elements)
1700 {
1701   offset_type i;
1702   htab_t type_hash;
1703
1704   dwarf2_per_objfile->n_type_comp_units = elements / 3;
1705   dwarf2_per_objfile->type_comp_units
1706     = obstack_alloc (&objfile->objfile_obstack,
1707                      dwarf2_per_objfile->n_type_comp_units
1708                      * sizeof (struct dwarf2_per_cu_data *));
1709
1710   type_hash = allocate_signatured_type_hash_table (objfile);
1711
1712   for (i = 0; i < elements; i += 3)
1713     {
1714       struct signatured_type *type_sig;
1715       ULONGEST offset, type_offset, signature;
1716       void **slot;
1717
1718       if (!extract_cu_value (bytes, &offset)
1719           || !extract_cu_value (bytes + 8, &type_offset))
1720         return 0;
1721       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1722       bytes += 3 * 8;
1723
1724       type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1725                                  struct signatured_type);
1726       type_sig->signature = signature;
1727       type_sig->offset = offset;
1728       type_sig->type_offset = type_offset;
1729       type_sig->per_cu.from_debug_types = 1;
1730       type_sig->per_cu.offset = offset;
1731       type_sig->per_cu.objfile = objfile;
1732       type_sig->per_cu.v.quick
1733         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734                           struct dwarf2_per_cu_quick_data);
1735
1736       slot = htab_find_slot (type_hash, type_sig, INSERT);
1737       *slot = type_sig;
1738
1739       dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1740     }
1741
1742   dwarf2_per_objfile->signatured_types = type_hash;
1743
1744   return 1;
1745 }
1746
1747 /* Read the address map data from the mapped index, and use it to
1748    populate the objfile's psymtabs_addrmap.  */
1749 static void
1750 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1751 {
1752   const gdb_byte *iter, *end;
1753   struct obstack temp_obstack;
1754   struct addrmap *mutable_map;
1755   struct cleanup *cleanup;
1756   CORE_ADDR baseaddr;
1757
1758   obstack_init (&temp_obstack);
1759   cleanup = make_cleanup_obstack_free (&temp_obstack);
1760   mutable_map = addrmap_create_mutable (&temp_obstack);
1761
1762   iter = index->address_table;
1763   end = iter + index->address_table_size;
1764
1765   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1766
1767   while (iter < end)
1768     {
1769       ULONGEST hi, lo, cu_index;
1770       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1771       iter += 8;
1772       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1773       iter += 8;
1774       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1775       iter += 4;
1776       
1777       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1778                          dw2_get_cu (cu_index));
1779     }
1780
1781   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1782                                                     &objfile->objfile_obstack);
1783   do_cleanups (cleanup);
1784 }
1785
1786 /* The hash function for strings in the mapped index.  This is the
1787    same as the hashtab.c hash function, but we keep a separate copy to
1788    maintain control over the implementation.  This is necessary
1789    because the hash function is tied to the format of the mapped index
1790    file.  */
1791 static hashval_t
1792 mapped_index_string_hash (const void *p)
1793 {
1794   const unsigned char *str = (const unsigned char *) p;
1795   hashval_t r = 0;
1796   unsigned char c;
1797
1798   while ((c = *str++) != 0)
1799     r = r * 67 + c - 113;
1800
1801   return r;
1802 }
1803
1804 /* Find a slot in the mapped index INDEX for the object named NAME.
1805    If NAME is found, set *VEC_OUT to point to the CU vector in the
1806    constant pool and return 1.  If NAME cannot be found, return 0.  */
1807 static int
1808 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1809                           offset_type **vec_out)
1810 {
1811   offset_type hash = mapped_index_string_hash (name);
1812   offset_type slot, step;
1813
1814   slot = hash & (index->index_table_slots - 1);
1815   step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1816
1817   for (;;)
1818     {
1819       /* Convert a slot number to an offset into the table.  */
1820       offset_type i = 2 * slot;
1821       const char *str;
1822       if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1823         return 0;
1824
1825       str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1826       if (!strcmp (name, str))
1827         {
1828           *vec_out = (offset_type *) (index->constant_pool
1829                                       + MAYBE_SWAP (index->index_table[i + 1]));
1830           return 1;
1831         }
1832
1833       slot = (slot + step) & (index->index_table_slots - 1);
1834     }
1835 }
1836
1837 /* Read the index file.  If everything went ok, initialize the "quick"
1838    elements of all the CUs and return 1.  Otherwise, return 0.  */
1839 static int
1840 dwarf2_read_index (struct objfile *objfile)
1841 {
1842   char *addr;
1843   struct mapped_index *map;
1844   offset_type *metadata;
1845   const gdb_byte *cu_list;
1846   const gdb_byte *types_list = NULL;
1847   offset_type version, cu_list_elements;
1848   offset_type types_list_elements = 0;
1849   int i;
1850
1851   if (dwarf2_per_objfile->gdb_index.asection == NULL
1852       || dwarf2_per_objfile->gdb_index.size == 0)
1853     return 0;
1854   dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1855
1856   addr = dwarf2_per_objfile->gdb_index.buffer;
1857   /* Version check.  */
1858   version = MAYBE_SWAP (*(offset_type *) addr);
1859   if (version == 1)
1860     {
1861       /* Index version 1 neglected to account for .debug_types.  So,
1862          if we see .debug_types, we cannot use this index.  */
1863       if (dwarf2_per_objfile->types.asection != NULL
1864           && dwarf2_per_objfile->types.size != 0)
1865         return 0;
1866     }
1867   else if (version != 2)
1868     return 0;
1869
1870   map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1871   map->total_size = dwarf2_per_objfile->gdb_index.size;
1872
1873   metadata = (offset_type *) (addr + sizeof (offset_type));
1874
1875   i = 0;
1876   cu_list = addr + MAYBE_SWAP (metadata[i]);
1877   cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1878                       / 8);
1879   ++i;
1880
1881   if (version == 2)
1882     {
1883       types_list = addr + MAYBE_SWAP (metadata[i]);
1884       types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1885                               - MAYBE_SWAP (metadata[i]))
1886                              / 8);
1887       ++i;
1888     }
1889
1890   map->address_table = addr + MAYBE_SWAP (metadata[i]);
1891   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1892                              - MAYBE_SWAP (metadata[i]));
1893   ++i;
1894
1895   map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1896   map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1897                              - MAYBE_SWAP (metadata[i]))
1898                             / (2 * sizeof (offset_type)));
1899   ++i;
1900
1901   map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1902
1903   if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1904     return 0;
1905
1906   if (version == 2
1907       && types_list_elements
1908       && !create_signatured_type_hash_from_index (objfile, types_list,
1909                                                   types_list_elements))
1910     return 0;
1911
1912   create_addrmap_from_index (objfile, map);
1913
1914   dwarf2_per_objfile->index_table = map;
1915   dwarf2_per_objfile->using_index = 1;
1916
1917   return 1;
1918 }
1919
1920 /* A helper for the "quick" functions which sets the global
1921    dwarf2_per_objfile according to OBJFILE.  */
1922 static void
1923 dw2_setup (struct objfile *objfile)
1924 {
1925   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1926   gdb_assert (dwarf2_per_objfile);
1927 }
1928
1929 /* A helper for the "quick" functions which attempts to read the line
1930    table for THIS_CU.  */
1931 static void
1932 dw2_require_line_header (struct objfile *objfile,
1933                          struct dwarf2_per_cu_data *this_cu)
1934 {
1935   bfd *abfd = objfile->obfd;
1936   struct line_header *lh = NULL;
1937   struct attribute *attr;
1938   struct cleanup *cleanups;
1939   struct die_info *comp_unit_die;
1940   gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1941   int has_children, i;
1942   struct dwarf2_cu cu;
1943   unsigned int bytes_read, buffer_size;
1944   struct die_reader_specs reader_specs;
1945   char *name, *comp_dir;
1946
1947   if (this_cu->v.quick->read_lines)
1948     return;
1949   this_cu->v.quick->read_lines = 1;
1950
1951   memset (&cu, 0, sizeof (cu));
1952   cu.objfile = objfile;
1953   obstack_init (&cu.comp_unit_obstack);
1954
1955   cleanups = make_cleanup (free_stack_comp_unit, &cu);
1956
1957   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
1958   buffer_size = dwarf2_per_objfile->info.size;
1959   buffer = dwarf2_per_objfile->info.buffer;
1960   info_ptr = buffer + this_cu->offset;
1961   beg_of_comp_unit = info_ptr;
1962
1963   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
1964                                           buffer, buffer_size,
1965                                           abfd);
1966
1967   /* Complete the cu_header.  */
1968   cu.header.offset = beg_of_comp_unit - buffer;
1969   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
1970
1971   this_cu->cu = &cu;
1972   cu.per_cu = this_cu;
1973
1974   dwarf2_read_abbrevs (abfd, &cu);
1975   make_cleanup (dwarf2_free_abbrev_table, &cu);
1976
1977   if (this_cu->from_debug_types)
1978     info_ptr += 8 /*signature*/ + cu.header.offset_size;
1979   init_cu_die_reader (&reader_specs, &cu);
1980   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
1981                             &has_children);
1982
1983   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
1984   if (attr)
1985     {
1986       unsigned int line_offset = DW_UNSND (attr);
1987       lh = dwarf_decode_line_header (line_offset, abfd, &cu);
1988     }
1989   if (lh == NULL)
1990     {
1991       do_cleanups (cleanups);
1992       return;
1993     }
1994
1995   find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
1996
1997   this_cu->v.quick->lines = lh;
1998
1999   this_cu->v.quick->file_names
2000     = obstack_alloc (&objfile->objfile_obstack,
2001                      lh->num_file_names * sizeof (char *));
2002   for (i = 0; i < lh->num_file_names; ++i)
2003     this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2004
2005   do_cleanups (cleanups);
2006 }
2007
2008 /* A helper for the "quick" functions which computes and caches the
2009    real path for a given file name from the line table.
2010    dw2_require_line_header must have been called before this is
2011    invoked.  */
2012 static const char *
2013 dw2_require_full_path (struct objfile *objfile,
2014                        struct dwarf2_per_cu_data *cu,
2015                        int index)
2016 {
2017   if (!cu->v.quick->full_names)
2018     cu->v.quick->full_names
2019       = OBSTACK_CALLOC (&objfile->objfile_obstack,
2020                         cu->v.quick->lines->num_file_names,
2021                         sizeof (char *));
2022
2023   if (!cu->v.quick->full_names[index])
2024     cu->v.quick->full_names[index]
2025       = gdb_realpath (cu->v.quick->file_names[index]);
2026
2027   return cu->v.quick->full_names[index];
2028 }
2029
2030 static struct symtab *
2031 dw2_find_last_source_symtab (struct objfile *objfile)
2032 {
2033   int index;
2034   dw2_setup (objfile);
2035   index = dwarf2_per_objfile->n_comp_units - 1;
2036   return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2037 }
2038
2039 static void
2040 dw2_forget_cached_source_info (struct objfile *objfile)
2041 {
2042   int i;
2043
2044   dw2_setup (objfile);
2045   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2046                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2047     {
2048       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2049
2050       if (cu->v.quick->full_names)
2051         {
2052           int j;
2053
2054           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2055             xfree ((void *) cu->v.quick->full_names[j]);
2056         }
2057     }
2058 }
2059
2060 static int
2061 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2062                    const char *full_path, const char *real_path,
2063                    struct symtab **result)
2064 {
2065   int i;
2066   int check_basename = lbasename (name) == name;
2067   struct dwarf2_per_cu_data *base_cu = NULL;
2068
2069   dw2_setup (objfile);
2070   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2071                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2072     {
2073       int j;
2074       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2075
2076       if (cu->v.quick->symtab)
2077         continue;
2078
2079       dw2_require_line_header (objfile, cu);
2080       if (!cu->v.quick->lines)
2081         continue;
2082
2083       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2084         {
2085           const char *this_name = cu->v.quick->file_names[j];
2086
2087           if (FILENAME_CMP (name, this_name) == 0)
2088             {
2089               *result = dw2_instantiate_symtab (objfile, cu);
2090               return 1;
2091             }
2092
2093           if (check_basename && ! base_cu
2094               && FILENAME_CMP (lbasename (this_name), name) == 0)
2095             base_cu = cu;
2096
2097           if (full_path != NULL)
2098             {
2099               const char *this_full_name = dw2_require_full_path (objfile,
2100                                                                   cu, j);
2101
2102               if (this_full_name
2103                   && FILENAME_CMP (full_path, this_full_name) == 0)
2104                 {
2105                   *result = dw2_instantiate_symtab (objfile, cu);
2106                   return 1;
2107                 }
2108             }
2109
2110           if (real_path != NULL)
2111             {
2112               const char *this_full_name = dw2_require_full_path (objfile,
2113                                                                   cu, j);
2114
2115               if (this_full_name != NULL)
2116                 {
2117                   char *rp = gdb_realpath (this_full_name);
2118                   if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2119                     {
2120                       xfree (rp);
2121                       *result = dw2_instantiate_symtab (objfile, cu);
2122                       return 1;
2123                     }
2124                   xfree (rp);
2125                 }
2126             }
2127         }
2128     }
2129
2130   if (base_cu)
2131     {
2132       *result = dw2_instantiate_symtab (objfile, base_cu);
2133       return 1;
2134     }
2135
2136   return 0;
2137 }
2138
2139 static struct symtab *
2140 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2141                    const char *name, domain_enum domain)
2142 {
2143   /* We do all the work in the pre_expand_symtabs_matching hook
2144      instead.  */
2145   return NULL;
2146 }
2147
2148 /* A helper function that expands all symtabs that hold an object
2149    named NAME.  */
2150 static void
2151 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2152 {
2153   dw2_setup (objfile);
2154
2155   if (dwarf2_per_objfile->index_table)
2156     {
2157       offset_type *vec;
2158
2159       if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2160                                     name, &vec))
2161         {
2162           offset_type i, len = MAYBE_SWAP (*vec);
2163           for (i = 0; i < len; ++i)
2164             {
2165               offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2166               struct dwarf2_per_cu_data *cu = dw2_get_cu (cu_index);
2167
2168               dw2_instantiate_symtab (objfile, cu);
2169             }
2170         }
2171     }
2172 }
2173
2174 static void
2175 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2176                                  int kind, const char *name,
2177                                  domain_enum domain)
2178 {
2179   dw2_do_expand_symtabs_matching (objfile, name);
2180 }
2181
2182 static void
2183 dw2_print_stats (struct objfile *objfile)
2184 {
2185   int i, count;
2186
2187   dw2_setup (objfile);
2188   count = 0;
2189   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2190                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2191     {
2192       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2193
2194       if (!cu->v.quick->symtab)
2195         ++count;
2196     }
2197   printf_filtered (_("  Number of unread CUs: %d\n"), count);
2198 }
2199
2200 static void
2201 dw2_dump (struct objfile *objfile)
2202 {
2203   /* Nothing worth printing.  */
2204 }
2205
2206 static void
2207 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2208               struct section_offsets *delta)
2209 {
2210   /* There's nothing to relocate here.  */
2211 }
2212
2213 static void
2214 dw2_expand_symtabs_for_function (struct objfile *objfile,
2215                                  const char *func_name)
2216 {
2217   dw2_do_expand_symtabs_matching (objfile, func_name);
2218 }
2219
2220 static void
2221 dw2_expand_all_symtabs (struct objfile *objfile)
2222 {
2223   int i;
2224
2225   dw2_setup (objfile);
2226
2227   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2228                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2229     {
2230       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2231
2232       dw2_instantiate_symtab (objfile, cu);
2233     }
2234 }
2235
2236 static void
2237 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2238                                   const char *filename)
2239 {
2240   int i;
2241
2242   dw2_setup (objfile);
2243   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2244                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2245     {
2246       int j;
2247       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2248
2249       if (cu->v.quick->symtab)
2250         continue;
2251
2252       dw2_require_line_header (objfile, cu);
2253       if (!cu->v.quick->lines)
2254         continue;
2255
2256       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2257         {
2258           const char *this_name = cu->v.quick->file_names[j];
2259           if (strcmp (this_name, filename) == 0)
2260             {
2261               dw2_instantiate_symtab (objfile, cu);
2262               break;
2263             }
2264         }
2265     }
2266 }
2267
2268 static const char *
2269 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2270 {
2271   struct dwarf2_per_cu_data *cu;
2272   offset_type *vec;
2273
2274   dw2_setup (objfile);
2275
2276   if (!dwarf2_per_objfile->index_table)
2277     return NULL;
2278
2279   if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2280                                  name, &vec))
2281     return NULL;
2282
2283   /* Note that this just looks at the very first one named NAME -- but
2284      actually we are looking for a function.  find_main_filename
2285      should be rewritten so that it doesn't require a custom hook.  It
2286      could just use the ordinary symbol tables.  */
2287   /* vec[0] is the length, which must always be >0.  */
2288   cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2289
2290   dw2_require_line_header (objfile, cu);
2291   if (!cu->v.quick->lines)
2292     return NULL;
2293
2294   return cu->v.quick->file_names[cu->v.quick->lines->num_file_names - 1];
2295 }
2296
2297 static void
2298 dw2_map_ada_symtabs (struct objfile *objfile,
2299                      int (*wild_match) (const char *, int, const char *),
2300                      int (*is_name_suffix) (const char *),
2301                      void (*callback) (struct objfile *,
2302                                        struct symtab *, void *),
2303                      const char *name, int global,
2304                      domain_enum namespace, int wild,
2305                      void *data)
2306 {
2307   /* For now, we don't support Ada, so this function can't be
2308      reached.  */
2309   internal_error (__FILE__, __LINE__,
2310                   _("map_ada_symtabs called via index method"));
2311 }
2312
2313 static void
2314 dw2_expand_symtabs_matching (struct objfile *objfile,
2315                              int (*file_matcher) (const char *, void *),
2316                              int (*name_matcher) (const char *, void *),
2317                              domain_enum kind,
2318                              void *data)
2319 {
2320   int i;
2321   offset_type iter;
2322
2323   dw2_setup (objfile);
2324   if (!dwarf2_per_objfile->index_table)
2325     return;
2326
2327   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2328                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2329     {
2330       int j;
2331       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2332
2333       cu->v.quick->mark = 0;
2334       if (cu->v.quick->symtab)
2335         continue;
2336
2337       dw2_require_line_header (objfile, cu);
2338       if (!cu->v.quick->lines)
2339         continue;
2340
2341       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2342         {
2343           if (file_matcher (cu->v.quick->file_names[j], data))
2344             {
2345               cu->v.quick->mark = 1;
2346               break;
2347             }
2348         }
2349     }
2350
2351   for (iter = 0;
2352        iter < dwarf2_per_objfile->index_table->index_table_slots;
2353        ++iter)
2354     {
2355       offset_type idx = 2 * iter;
2356       const char *name;
2357       offset_type *vec, vec_len, vec_idx;
2358
2359       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2360           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2361         continue;
2362
2363       name = (dwarf2_per_objfile->index_table->constant_pool
2364               + dwarf2_per_objfile->index_table->index_table[idx]);
2365
2366       if (! (*name_matcher) (name, data))
2367         continue;
2368
2369       /* The name was matched, now expand corresponding CUs that were
2370          marked.  */
2371       vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2372                              + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2373       vec_len = MAYBE_SWAP (vec[0]);
2374       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2375         {
2376           struct dwarf2_per_cu_data *cu;
2377
2378           cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2379           if (cu->v.quick->mark)
2380             dw2_instantiate_symtab (objfile, cu);
2381         }
2382     }
2383 }
2384
2385 static struct symtab *
2386 dw2_find_pc_sect_symtab (struct objfile *objfile,
2387                          struct minimal_symbol *msymbol,
2388                          CORE_ADDR pc,
2389                          struct obj_section *section,
2390                          int warn_if_readin)
2391 {
2392   struct dwarf2_per_cu_data *data;
2393
2394   dw2_setup (objfile);
2395
2396   if (!objfile->psymtabs_addrmap)
2397     return NULL;
2398
2399   data = addrmap_find (objfile->psymtabs_addrmap, pc);
2400   if (!data)
2401     return NULL;
2402
2403   if (warn_if_readin && data->v.quick->symtab)
2404     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2405              paddress (get_objfile_arch (objfile), pc));
2406
2407   return dw2_instantiate_symtab (objfile, data);
2408 }
2409
2410 static void
2411 dw2_map_symbol_names (struct objfile *objfile,
2412                       void (*fun) (const char *, void *),
2413                       void *data)
2414 {
2415   offset_type iter;
2416   dw2_setup (objfile);
2417
2418   if (!dwarf2_per_objfile->index_table)
2419     return;
2420
2421   for (iter = 0;
2422        iter < dwarf2_per_objfile->index_table->index_table_slots;
2423        ++iter)
2424     {
2425       offset_type idx = 2 * iter;
2426       const char *name;
2427       offset_type *vec, vec_len, vec_idx;
2428
2429       if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2430           && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2431         continue;
2432
2433       name = (dwarf2_per_objfile->index_table->constant_pool
2434               + dwarf2_per_objfile->index_table->index_table[idx]);
2435
2436       (*fun) (name, data);
2437     }
2438 }
2439
2440 static void
2441 dw2_map_symbol_filenames (struct objfile *objfile,
2442                           void (*fun) (const char *, const char *, void *),
2443                           void *data)
2444 {
2445   int i;
2446
2447   dw2_setup (objfile);
2448   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2449                    + dwarf2_per_objfile->n_type_comp_units); ++i)
2450     {
2451       int j;
2452       struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2453
2454       if (cu->v.quick->symtab)
2455         continue;
2456
2457       dw2_require_line_header (objfile, cu);
2458       if (!cu->v.quick->lines)
2459         continue;
2460
2461       for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
2462         {
2463           const char *this_full_name = dw2_require_full_path (objfile, cu, j);
2464           (*fun) (cu->v.quick->file_names[j], this_full_name, data);
2465         }
2466     }
2467 }
2468
2469 static int
2470 dw2_has_symbols (struct objfile *objfile)
2471 {
2472   return 1;
2473 }
2474
2475 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2476 {
2477   dw2_has_symbols,
2478   dw2_find_last_source_symtab,
2479   dw2_forget_cached_source_info,
2480   dw2_lookup_symtab,
2481   dw2_lookup_symbol,
2482   dw2_pre_expand_symtabs_matching,
2483   dw2_print_stats,
2484   dw2_dump,
2485   dw2_relocate,
2486   dw2_expand_symtabs_for_function,
2487   dw2_expand_all_symtabs,
2488   dw2_expand_symtabs_with_filename,
2489   dw2_find_symbol_file,
2490   dw2_map_ada_symtabs,
2491   dw2_expand_symtabs_matching,
2492   dw2_find_pc_sect_symtab,
2493   dw2_map_symbol_names,
2494   dw2_map_symbol_filenames
2495 };
2496
2497 /* Initialize for reading DWARF for this objfile.  Return 0 if this
2498    file will use psymtabs, or 1 if using the GNU index.  */
2499
2500 int
2501 dwarf2_initialize_objfile (struct objfile *objfile)
2502 {
2503   /* If we're about to read full symbols, don't bother with the
2504      indices.  In this case we also don't care if some other debug
2505      format is making psymtabs, because they are all about to be
2506      expanded anyway.  */
2507   if ((objfile->flags & OBJF_READNOW))
2508     {
2509       int i;
2510
2511       dwarf2_per_objfile->using_index = 1;
2512       create_all_comp_units (objfile);
2513       create_debug_types_hash_table (objfile);
2514
2515       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2516                        + dwarf2_per_objfile->n_type_comp_units); ++i)
2517         {
2518           struct dwarf2_per_cu_data *cu = dw2_get_cu (i);
2519
2520           cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521                                         struct dwarf2_per_cu_quick_data);
2522         }
2523
2524       /* Return 1 so that gdb sees the "quick" functions.  However,
2525          these functions will be no-ops because we will have expanded
2526          all symtabs.  */
2527       return 1;
2528     }
2529
2530   if (dwarf2_read_index (objfile))
2531     return 1;
2532
2533   dwarf2_build_psymtabs (objfile);
2534   return 0;
2535 }
2536
2537 \f
2538
2539 /* Build a partial symbol table.  */
2540
2541 void
2542 dwarf2_build_psymtabs (struct objfile *objfile)
2543 {
2544   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2545     {
2546       init_psymbol_list (objfile, 1024);
2547     }
2548
2549   dwarf2_build_psymtabs_hard (objfile);
2550 }
2551
2552 /* Return TRUE if OFFSET is within CU_HEADER.  */
2553
2554 static inline int
2555 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2556 {
2557   unsigned int bottom = cu_header->offset;
2558   unsigned int top = (cu_header->offset
2559                       + cu_header->length
2560                       + cu_header->initial_length_size);
2561
2562   return (offset >= bottom && offset < top);
2563 }
2564
2565 /* Read in the comp unit header information from the debug_info at info_ptr.
2566    NOTE: This leaves members offset, first_die_offset to be filled in
2567    by the caller.  */
2568
2569 static gdb_byte *
2570 read_comp_unit_head (struct comp_unit_head *cu_header,
2571                      gdb_byte *info_ptr, bfd *abfd)
2572 {
2573   int signed_addr;
2574   unsigned int bytes_read;
2575
2576   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2577   cu_header->initial_length_size = bytes_read;
2578   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2579   info_ptr += bytes_read;
2580   cu_header->version = read_2_bytes (abfd, info_ptr);
2581   info_ptr += 2;
2582   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2583                                           &bytes_read);
2584   info_ptr += bytes_read;
2585   cu_header->addr_size = read_1_byte (abfd, info_ptr);
2586   info_ptr += 1;
2587   signed_addr = bfd_get_sign_extend_vma (abfd);
2588   if (signed_addr < 0)
2589     internal_error (__FILE__, __LINE__,
2590                     _("read_comp_unit_head: dwarf from non elf file"));
2591   cu_header->signed_addr_p = signed_addr;
2592
2593   return info_ptr;
2594 }
2595
2596 static gdb_byte *
2597 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2598                              gdb_byte *buffer, unsigned int buffer_size,
2599                              bfd *abfd)
2600 {
2601   gdb_byte *beg_of_comp_unit = info_ptr;
2602
2603   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2604
2605   if (header->version != 2 && header->version != 3 && header->version != 4)
2606     error (_("Dwarf Error: wrong version in compilation unit header "
2607            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2608            bfd_get_filename (abfd));
2609
2610   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2611     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2612            "(offset 0x%lx + 6) [in module %s]"),
2613            (long) header->abbrev_offset,
2614            (long) (beg_of_comp_unit - buffer),
2615            bfd_get_filename (abfd));
2616
2617   if (beg_of_comp_unit + header->length + header->initial_length_size
2618       > buffer + buffer_size)
2619     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2620            "(offset 0x%lx + 0) [in module %s]"),
2621            (long) header->length,
2622            (long) (beg_of_comp_unit - buffer),
2623            bfd_get_filename (abfd));
2624
2625   return info_ptr;
2626 }
2627
2628 /* Read in the types comp unit header information from .debug_types entry at
2629    types_ptr.  The result is a pointer to one past the end of the header.  */
2630
2631 static gdb_byte *
2632 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2633                           ULONGEST *signature,
2634                           gdb_byte *types_ptr, bfd *abfd)
2635 {
2636   gdb_byte *initial_types_ptr = types_ptr;
2637
2638   dwarf2_read_section (dwarf2_per_objfile->objfile,
2639                        &dwarf2_per_objfile->types);
2640   cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2641
2642   types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2643
2644   *signature = read_8_bytes (abfd, types_ptr);
2645   types_ptr += 8;
2646   types_ptr += cu_header->offset_size;
2647   cu_header->first_die_offset = types_ptr - initial_types_ptr;
2648
2649   return types_ptr;
2650 }
2651
2652 /* Allocate a new partial symtab for file named NAME and mark this new
2653    partial symtab as being an include of PST.  */
2654
2655 static void
2656 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2657                                struct objfile *objfile)
2658 {
2659   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2660
2661   subpst->section_offsets = pst->section_offsets;
2662   subpst->textlow = 0;
2663   subpst->texthigh = 0;
2664
2665   subpst->dependencies = (struct partial_symtab **)
2666     obstack_alloc (&objfile->objfile_obstack,
2667                    sizeof (struct partial_symtab *));
2668   subpst->dependencies[0] = pst;
2669   subpst->number_of_dependencies = 1;
2670
2671   subpst->globals_offset = 0;
2672   subpst->n_global_syms = 0;
2673   subpst->statics_offset = 0;
2674   subpst->n_static_syms = 0;
2675   subpst->symtab = NULL;
2676   subpst->read_symtab = pst->read_symtab;
2677   subpst->readin = 0;
2678
2679   /* No private part is necessary for include psymtabs.  This property
2680      can be used to differentiate between such include psymtabs and
2681      the regular ones.  */
2682   subpst->read_symtab_private = NULL;
2683 }
2684
2685 /* Read the Line Number Program data and extract the list of files
2686    included by the source file represented by PST.  Build an include
2687    partial symtab for each of these included files.  */
2688
2689 static void
2690 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2691                                struct die_info *die,
2692                                struct partial_symtab *pst)
2693 {
2694   struct objfile *objfile = cu->objfile;
2695   bfd *abfd = objfile->obfd;
2696   struct line_header *lh = NULL;
2697   struct attribute *attr;
2698
2699   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2700   if (attr)
2701     {
2702       unsigned int line_offset = DW_UNSND (attr);
2703
2704       lh = dwarf_decode_line_header (line_offset, abfd, cu);
2705     }
2706   if (lh == NULL)
2707     return;  /* No linetable, so no includes.  */
2708
2709   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
2710
2711   free_line_header (lh);
2712 }
2713
2714 static hashval_t
2715 hash_type_signature (const void *item)
2716 {
2717   const struct signatured_type *type_sig = item;
2718
2719   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
2720   return type_sig->signature;
2721 }
2722
2723 static int
2724 eq_type_signature (const void *item_lhs, const void *item_rhs)
2725 {
2726   const struct signatured_type *lhs = item_lhs;
2727   const struct signatured_type *rhs = item_rhs;
2728
2729   return lhs->signature == rhs->signature;
2730 }
2731
2732 /* Allocate a hash table for signatured types.  */
2733
2734 static htab_t
2735 allocate_signatured_type_hash_table (struct objfile *objfile)
2736 {
2737   return htab_create_alloc_ex (41,
2738                                hash_type_signature,
2739                                eq_type_signature,
2740                                NULL,
2741                                &objfile->objfile_obstack,
2742                                hashtab_obstack_allocate,
2743                                dummy_obstack_deallocate);
2744 }
2745
2746 /* A helper function to add a signatured type CU to a list.  */
2747
2748 static int
2749 add_signatured_type_cu_to_list (void **slot, void *datum)
2750 {
2751   struct signatured_type *sigt = *slot;
2752   struct dwarf2_per_cu_data ***datap = datum;
2753
2754   **datap = &sigt->per_cu;
2755   ++*datap;
2756
2757   return 1;
2758 }
2759
2760 /* Create the hash table of all entries in the .debug_types section.
2761    The result is zero if there is an error (e.g. missing .debug_types section),
2762    otherwise non-zero.  */
2763
2764 static int
2765 create_debug_types_hash_table (struct objfile *objfile)
2766 {
2767   gdb_byte *info_ptr;
2768   htab_t types_htab;
2769   struct dwarf2_per_cu_data **iter;
2770
2771   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2772   info_ptr = dwarf2_per_objfile->types.buffer;
2773
2774   if (info_ptr == NULL)
2775     {
2776       dwarf2_per_objfile->signatured_types = NULL;
2777       return 0;
2778     }
2779
2780   types_htab = allocate_signatured_type_hash_table (objfile);
2781
2782   if (dwarf2_die_debug)
2783     fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2784
2785   while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2786     {
2787       unsigned int offset;
2788       unsigned int offset_size;
2789       unsigned int type_offset;
2790       unsigned int length, initial_length_size;
2791       unsigned short version;
2792       ULONGEST signature;
2793       struct signatured_type *type_sig;
2794       void **slot;
2795       gdb_byte *ptr = info_ptr;
2796
2797       offset = ptr - dwarf2_per_objfile->types.buffer;
2798
2799       /* We need to read the type's signature in order to build the hash
2800          table, but we don't need to read anything else just yet.  */
2801
2802       /* Sanity check to ensure entire cu is present.  */
2803       length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2804       if (ptr + length + initial_length_size
2805           > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2806         {
2807           complaint (&symfile_complaints,
2808                      _("debug type entry runs off end of `.debug_types' section, ignored"));
2809           break;
2810         }
2811
2812       offset_size = initial_length_size == 4 ? 4 : 8;
2813       ptr += initial_length_size;
2814       version = bfd_get_16 (objfile->obfd, ptr);
2815       ptr += 2;
2816       ptr += offset_size; /* abbrev offset */
2817       ptr += 1; /* address size */
2818       signature = bfd_get_64 (objfile->obfd, ptr);
2819       ptr += 8;
2820       type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2821
2822       type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2823       memset (type_sig, 0, sizeof (*type_sig));
2824       type_sig->signature = signature;
2825       type_sig->offset = offset;
2826       type_sig->type_offset = type_offset;
2827       type_sig->per_cu.objfile = objfile;
2828       type_sig->per_cu.from_debug_types = 1;
2829
2830       slot = htab_find_slot (types_htab, type_sig, INSERT);
2831       gdb_assert (slot != NULL);
2832       *slot = type_sig;
2833
2834       if (dwarf2_die_debug)
2835         fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature 0x%s\n",
2836                             offset, phex (signature, sizeof (signature)));
2837
2838       info_ptr = info_ptr + initial_length_size + length;
2839     }
2840
2841   dwarf2_per_objfile->signatured_types = types_htab;
2842
2843   dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2844   dwarf2_per_objfile->type_comp_units
2845     = obstack_alloc (&objfile->objfile_obstack,
2846                      dwarf2_per_objfile->n_type_comp_units
2847                      * sizeof (struct dwarf2_per_cu_data *));
2848   iter = &dwarf2_per_objfile->type_comp_units[0];
2849   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2850   gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2851               == dwarf2_per_objfile->n_type_comp_units);
2852
2853   return 1;
2854 }
2855
2856 /* Lookup a signature based type.
2857    Returns NULL if SIG is not present in the table.  */
2858
2859 static struct signatured_type *
2860 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2861 {
2862   struct signatured_type find_entry, *entry;
2863
2864   if (dwarf2_per_objfile->signatured_types == NULL)
2865     {
2866       complaint (&symfile_complaints,
2867                  _("missing `.debug_types' section for DW_FORM_sig8 die"));
2868       return 0;
2869     }
2870
2871   find_entry.signature = sig;
2872   entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2873   return entry;
2874 }
2875
2876 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
2877
2878 static void
2879 init_cu_die_reader (struct die_reader_specs *reader,
2880                     struct dwarf2_cu *cu)
2881 {
2882   reader->abfd = cu->objfile->obfd;
2883   reader->cu = cu;
2884   if (cu->per_cu->from_debug_types)
2885     {
2886       gdb_assert (dwarf2_per_objfile->types.readin);
2887       reader->buffer = dwarf2_per_objfile->types.buffer;
2888     }
2889   else
2890     {
2891       gdb_assert (dwarf2_per_objfile->info.readin);
2892       reader->buffer = dwarf2_per_objfile->info.buffer;
2893     }
2894 }
2895
2896 /* Find the base address of the compilation unit for range lists and
2897    location lists.  It will normally be specified by DW_AT_low_pc.
2898    In DWARF-3 draft 4, the base address could be overridden by
2899    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2900    compilation units with discontinuous ranges.  */
2901
2902 static void
2903 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2904 {
2905   struct attribute *attr;
2906
2907   cu->base_known = 0;
2908   cu->base_address = 0;
2909
2910   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2911   if (attr)
2912     {
2913       cu->base_address = DW_ADDR (attr);
2914       cu->base_known = 1;
2915     }
2916   else
2917     {
2918       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2919       if (attr)
2920         {
2921           cu->base_address = DW_ADDR (attr);
2922           cu->base_known = 1;
2923         }
2924     }
2925 }
2926
2927 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2928    to combine the common parts.
2929    Process a compilation unit for a psymtab.
2930    BUFFER is a pointer to the beginning of the dwarf section buffer,
2931    either .debug_info or debug_types.
2932    INFO_PTR is a pointer to the start of the CU.
2933    Returns a pointer to the next CU.  */
2934
2935 static gdb_byte *
2936 process_psymtab_comp_unit (struct objfile *objfile,
2937                            struct dwarf2_per_cu_data *this_cu,
2938                            gdb_byte *buffer, gdb_byte *info_ptr,
2939                            unsigned int buffer_size)
2940 {
2941   bfd *abfd = objfile->obfd;
2942   gdb_byte *beg_of_comp_unit = info_ptr;
2943   struct die_info *comp_unit_die;
2944   struct partial_symtab *pst;
2945   CORE_ADDR baseaddr;
2946   struct cleanup *back_to_inner;
2947   struct dwarf2_cu cu;
2948   int has_children, has_pc_info;
2949   struct attribute *attr;
2950   CORE_ADDR best_lowpc = 0, best_highpc = 0;
2951   struct die_reader_specs reader_specs;
2952
2953   memset (&cu, 0, sizeof (cu));
2954   cu.objfile = objfile;
2955   obstack_init (&cu.comp_unit_obstack);
2956
2957   back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
2958
2959   info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2960                                           buffer, buffer_size,
2961                                           abfd);
2962
2963   /* Complete the cu_header.  */
2964   cu.header.offset = beg_of_comp_unit - buffer;
2965   cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2966
2967   cu.list_in_scope = &file_symbols;
2968
2969   /* If this compilation unit was already read in, free the
2970      cached copy in order to read it in again.  This is
2971      necessary because we skipped some symbols when we first
2972      read in the compilation unit (see load_partial_dies).
2973      This problem could be avoided, but the benefit is
2974      unclear.  */
2975   if (this_cu->cu != NULL)
2976     free_one_cached_comp_unit (this_cu->cu);
2977
2978   /* Note that this is a pointer to our stack frame, being
2979      added to a global data structure.  It will be cleaned up
2980      in free_stack_comp_unit when we finish with this
2981      compilation unit.  */
2982   this_cu->cu = &cu;
2983   cu.per_cu = this_cu;
2984
2985   /* Read the abbrevs for this compilation unit into a table.  */
2986   dwarf2_read_abbrevs (abfd, &cu);
2987   make_cleanup (dwarf2_free_abbrev_table, &cu);
2988
2989   /* Read the compilation unit die.  */
2990   if (this_cu->from_debug_types)
2991     info_ptr += 8 /*signature*/ + cu.header.offset_size;
2992   init_cu_die_reader (&reader_specs, &cu);
2993   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2994                             &has_children);
2995
2996   if (this_cu->from_debug_types)
2997     {
2998       /* offset,length haven't been set yet for type units.  */
2999       this_cu->offset = cu.header.offset;
3000       this_cu->length = cu.header.length + cu.header.initial_length_size;
3001     }
3002   else if (comp_unit_die->tag == DW_TAG_partial_unit)
3003     {
3004       info_ptr = (beg_of_comp_unit + cu.header.length
3005                   + cu.header.initial_length_size);
3006       do_cleanups (back_to_inner);
3007       return info_ptr;
3008     }
3009
3010   /* Set the language we're debugging.  */
3011   attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3012   if (attr)
3013     set_cu_language (DW_UNSND (attr), &cu);
3014   else
3015     set_cu_language (language_minimal, &cu);
3016
3017   /* Allocate a new partial symbol table structure.  */
3018   attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3019   pst = start_psymtab_common (objfile, objfile->section_offsets,
3020                               (attr != NULL) ? DW_STRING (attr) : "",
3021                               /* TEXTLOW and TEXTHIGH are set below.  */
3022                               0,
3023                               objfile->global_psymbols.next,
3024                               objfile->static_psymbols.next);
3025
3026   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3027   if (attr != NULL)
3028     pst->dirname = DW_STRING (attr);
3029
3030   pst->read_symtab_private = this_cu;
3031
3032   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3033
3034   /* Store the function that reads in the rest of the symbol table */
3035   pst->read_symtab = dwarf2_psymtab_to_symtab;
3036
3037   this_cu->v.psymtab = pst;
3038
3039   dwarf2_find_base_address (comp_unit_die, &cu);
3040
3041   /* Possibly set the default values of LOWPC and HIGHPC from
3042      `DW_AT_ranges'.  */
3043   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3044                                       &best_highpc, &cu, pst);
3045   if (has_pc_info == 1 && best_lowpc < best_highpc)
3046     /* Store the contiguous range if it is not empty; it can be empty for
3047        CUs with no code.  */
3048     addrmap_set_empty (objfile->psymtabs_addrmap,
3049                        best_lowpc + baseaddr,
3050                        best_highpc + baseaddr - 1, pst);
3051
3052   /* Check if comp unit has_children.
3053      If so, read the rest of the partial symbols from this comp unit.
3054      If not, there's no more debug_info for this comp unit. */
3055   if (has_children)
3056     {
3057       struct partial_die_info *first_die;
3058       CORE_ADDR lowpc, highpc;
3059
3060       lowpc = ((CORE_ADDR) -1);
3061       highpc = ((CORE_ADDR) 0);
3062
3063       first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3064
3065       scan_partial_symbols (first_die, &lowpc, &highpc,
3066                             ! has_pc_info, &cu);
3067
3068       /* If we didn't find a lowpc, set it to highpc to avoid
3069          complaints from `maint check'.  */
3070       if (lowpc == ((CORE_ADDR) -1))
3071         lowpc = highpc;
3072
3073       /* If the compilation unit didn't have an explicit address range,
3074          then use the information extracted from its child dies.  */
3075       if (! has_pc_info)
3076         {
3077           best_lowpc = lowpc;
3078           best_highpc = highpc;
3079         }
3080     }
3081   pst->textlow = best_lowpc + baseaddr;
3082   pst->texthigh = best_highpc + baseaddr;
3083
3084   pst->n_global_syms = objfile->global_psymbols.next -
3085     (objfile->global_psymbols.list + pst->globals_offset);
3086   pst->n_static_syms = objfile->static_psymbols.next -
3087     (objfile->static_psymbols.list + pst->statics_offset);
3088   sort_pst_symbols (pst);
3089
3090   info_ptr = (beg_of_comp_unit + cu.header.length
3091               + cu.header.initial_length_size);
3092
3093   if (this_cu->from_debug_types)
3094     {
3095       /* It's not clear we want to do anything with stmt lists here.
3096          Waiting to see what gcc ultimately does.  */
3097     }
3098   else
3099     {
3100       /* Get the list of files included in the current compilation unit,
3101          and build a psymtab for each of them.  */
3102       dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3103     }
3104
3105   do_cleanups (back_to_inner);
3106
3107   return info_ptr;
3108 }
3109
3110 /* Traversal function for htab_traverse_noresize.
3111    Process one .debug_types comp-unit.  */
3112
3113 static int
3114 process_type_comp_unit (void **slot, void *info)
3115 {
3116   struct signatured_type *entry = (struct signatured_type *) *slot;
3117   struct objfile *objfile = (struct objfile *) info;
3118   struct dwarf2_per_cu_data *this_cu;
3119
3120   this_cu = &entry->per_cu;
3121
3122   gdb_assert (dwarf2_per_objfile->types.readin);
3123   process_psymtab_comp_unit (objfile, this_cu,
3124                              dwarf2_per_objfile->types.buffer,
3125                              dwarf2_per_objfile->types.buffer + entry->offset,
3126                              dwarf2_per_objfile->types.size);
3127
3128   return 1;
3129 }
3130
3131 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3132    Build partial symbol tables for the .debug_types comp-units.  */
3133
3134 static void
3135 build_type_psymtabs (struct objfile *objfile)
3136 {
3137   if (! create_debug_types_hash_table (objfile))
3138     return;
3139
3140   htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3141                           process_type_comp_unit, objfile);
3142 }
3143
3144 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
3145
3146 static void
3147 psymtabs_addrmap_cleanup (void *o)
3148 {
3149   struct objfile *objfile = o;
3150
3151   objfile->psymtabs_addrmap = NULL;
3152 }
3153
3154 /* Build the partial symbol table by doing a quick pass through the
3155    .debug_info and .debug_abbrev sections.  */
3156
3157 static void
3158 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3159 {
3160   gdb_byte *info_ptr;
3161   struct cleanup *back_to, *addrmap_cleanup;
3162   struct obstack temp_obstack;
3163
3164   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3165   info_ptr = dwarf2_per_objfile->info.buffer;
3166
3167   /* Any cached compilation units will be linked by the per-objfile
3168      read_in_chain.  Make sure to free them when we're done.  */
3169   back_to = make_cleanup (free_cached_comp_units, NULL);
3170
3171   build_type_psymtabs (objfile);
3172
3173   create_all_comp_units (objfile);
3174
3175   /* Create a temporary address map on a temporary obstack.  We later
3176      copy this to the final obstack.  */
3177   obstack_init (&temp_obstack);
3178   make_cleanup_obstack_free (&temp_obstack);
3179   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3180   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3181
3182   /* Since the objects we're extracting from .debug_info vary in
3183      length, only the individual functions to extract them (like
3184      read_comp_unit_head and load_partial_die) can really know whether
3185      the buffer is large enough to hold another complete object.
3186
3187      At the moment, they don't actually check that.  If .debug_info
3188      holds just one extra byte after the last compilation unit's dies,
3189      then read_comp_unit_head will happily read off the end of the
3190      buffer.  read_partial_die is similarly casual.  Those functions
3191      should be fixed.
3192
3193      For this loop condition, simply checking whether there's any data
3194      left at all should be sufficient.  */
3195
3196   while (info_ptr < (dwarf2_per_objfile->info.buffer
3197                      + dwarf2_per_objfile->info.size))
3198     {
3199       struct dwarf2_per_cu_data *this_cu;
3200
3201       this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3202                                        objfile);
3203
3204       info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3205                                             dwarf2_per_objfile->info.buffer,
3206                                             info_ptr,
3207                                             dwarf2_per_objfile->info.size);
3208     }
3209
3210   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3211                                                     &objfile->objfile_obstack);
3212   discard_cleanups (addrmap_cleanup);
3213
3214   do_cleanups (back_to);
3215 }
3216
3217 /* Load the partial DIEs for a secondary CU into memory.  */
3218
3219 static void
3220 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3221                         struct objfile *objfile)
3222 {
3223   bfd *abfd = objfile->obfd;
3224   gdb_byte *info_ptr, *beg_of_comp_unit;
3225   struct die_info *comp_unit_die;
3226   struct dwarf2_cu *cu;
3227   struct cleanup *back_to;
3228   struct attribute *attr;
3229   int has_children;
3230   struct die_reader_specs reader_specs;
3231
3232   gdb_assert (! this_cu->from_debug_types);
3233
3234   gdb_assert (dwarf2_per_objfile->info.readin);
3235   info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3236   beg_of_comp_unit = info_ptr;
3237
3238   cu = alloc_one_comp_unit (objfile);
3239
3240   /* ??? Missing cleanup for CU?  */
3241
3242   /* Link this compilation unit into the compilation unit tree.  */
3243   this_cu->cu = cu;
3244   cu->per_cu = this_cu;
3245   cu->type_hash = this_cu->type_hash;
3246
3247   info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3248                                           dwarf2_per_objfile->info.buffer,
3249                                           dwarf2_per_objfile->info.size,
3250                                           abfd);
3251
3252   /* Complete the cu_header.  */
3253   cu->header.offset = this_cu->offset;
3254   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3255
3256   /* Read the abbrevs for this compilation unit into a table.  */
3257   dwarf2_read_abbrevs (abfd, cu);
3258   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
3259
3260   /* Read the compilation unit die.  */
3261   init_cu_die_reader (&reader_specs, cu);
3262   info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3263                             &has_children);
3264
3265   /* Set the language we're debugging.  */
3266   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3267   if (attr)
3268     set_cu_language (DW_UNSND (attr), cu);
3269   else
3270     set_cu_language (language_minimal, cu);
3271
3272   /* Check if comp unit has_children.
3273      If so, read the rest of the partial symbols from this comp unit.
3274      If not, there's no more debug_info for this comp unit. */
3275   if (has_children)
3276     load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3277
3278   do_cleanups (back_to);
3279 }
3280
3281 /* Create a list of all compilation units in OBJFILE.  We do this only
3282    if an inter-comp-unit reference is found; presumably if there is one,
3283    there will be many, and one will occur early in the .debug_info section.
3284    So there's no point in building this list incrementally.  */
3285
3286 static void
3287 create_all_comp_units (struct objfile *objfile)
3288 {
3289   int n_allocated;
3290   int n_comp_units;
3291   struct dwarf2_per_cu_data **all_comp_units;
3292   gdb_byte *info_ptr;
3293
3294   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3295   info_ptr = dwarf2_per_objfile->info.buffer;
3296
3297   n_comp_units = 0;
3298   n_allocated = 10;
3299   all_comp_units = xmalloc (n_allocated
3300                             * sizeof (struct dwarf2_per_cu_data *));
3301
3302   while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3303     {
3304       unsigned int length, initial_length_size;
3305       struct dwarf2_per_cu_data *this_cu;
3306       unsigned int offset;
3307
3308       offset = info_ptr - dwarf2_per_objfile->info.buffer;
3309
3310       /* Read just enough information to find out where the next
3311          compilation unit is.  */
3312       length = read_initial_length (objfile->obfd, info_ptr,
3313                                     &initial_length_size);
3314
3315       /* Save the compilation unit for later lookup.  */
3316       this_cu = obstack_alloc (&objfile->objfile_obstack,
3317                                sizeof (struct dwarf2_per_cu_data));
3318       memset (this_cu, 0, sizeof (*this_cu));
3319       this_cu->offset = offset;
3320       this_cu->length = length + initial_length_size;
3321       this_cu->objfile = objfile;
3322
3323       if (n_comp_units == n_allocated)
3324         {
3325           n_allocated *= 2;
3326           all_comp_units = xrealloc (all_comp_units,
3327                                      n_allocated
3328                                      * sizeof (struct dwarf2_per_cu_data *));
3329         }
3330       all_comp_units[n_comp_units++] = this_cu;
3331
3332       info_ptr = info_ptr + this_cu->length;
3333     }
3334
3335   dwarf2_per_objfile->all_comp_units
3336     = obstack_alloc (&objfile->objfile_obstack,
3337                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3338   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3339           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3340   xfree (all_comp_units);
3341   dwarf2_per_objfile->n_comp_units = n_comp_units;
3342 }
3343
3344 /* Process all loaded DIEs for compilation unit CU, starting at
3345    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
3346    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3347    DW_AT_ranges).  If NEED_PC is set, then this function will set
3348    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3349    and record the covered ranges in the addrmap.  */
3350
3351 static void
3352 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3353                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3354 {
3355   struct partial_die_info *pdi;
3356
3357   /* Now, march along the PDI's, descending into ones which have
3358      interesting children but skipping the children of the other ones,
3359      until we reach the end of the compilation unit.  */
3360
3361   pdi = first_die;
3362
3363   while (pdi != NULL)
3364     {
3365       fixup_partial_die (pdi, cu);
3366
3367       /* Anonymous namespaces or modules have no name but have interesting
3368          children, so we need to look at them.  Ditto for anonymous
3369          enums.  */
3370
3371       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3372           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3373         {
3374           switch (pdi->tag)
3375             {
3376             case DW_TAG_subprogram:
3377               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3378               break;
3379             case DW_TAG_variable:
3380             case DW_TAG_typedef:
3381             case DW_TAG_union_type:
3382               if (!pdi->is_declaration)
3383                 {
3384                   add_partial_symbol (pdi, cu);
3385                 }
3386               break;
3387             case DW_TAG_class_type:
3388             case DW_TAG_interface_type:
3389             case DW_TAG_structure_type:
3390               if (!pdi->is_declaration)
3391                 {
3392                   add_partial_symbol (pdi, cu);
3393                 }
3394               break;
3395             case DW_TAG_enumeration_type:
3396               if (!pdi->is_declaration)
3397                 add_partial_enumeration (pdi, cu);
3398               break;
3399             case DW_TAG_base_type:
3400             case DW_TAG_subrange_type:
3401               /* File scope base type definitions are added to the partial
3402                  symbol table.  */
3403               add_partial_symbol (pdi, cu);
3404               break;
3405             case DW_TAG_namespace:
3406               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3407               break;
3408             case DW_TAG_module:
3409               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3410               break;
3411             default:
3412               break;
3413             }
3414         }
3415
3416       /* If the die has a sibling, skip to the sibling.  */
3417
3418       pdi = pdi->die_sibling;
3419     }
3420 }
3421
3422 /* Functions used to compute the fully scoped name of a partial DIE.
3423
3424    Normally, this is simple.  For C++, the parent DIE's fully scoped
3425    name is concatenated with "::" and the partial DIE's name.  For
3426    Java, the same thing occurs except that "." is used instead of "::".
3427    Enumerators are an exception; they use the scope of their parent
3428    enumeration type, i.e. the name of the enumeration type is not
3429    prepended to the enumerator.
3430
3431    There are two complexities.  One is DW_AT_specification; in this
3432    case "parent" means the parent of the target of the specification,
3433    instead of the direct parent of the DIE.  The other is compilers
3434    which do not emit DW_TAG_namespace; in this case we try to guess
3435    the fully qualified name of structure types from their members'
3436    linkage names.  This must be done using the DIE's children rather
3437    than the children of any DW_AT_specification target.  We only need
3438    to do this for structures at the top level, i.e. if the target of
3439    any DW_AT_specification (if any; otherwise the DIE itself) does not
3440    have a parent.  */
3441
3442 /* Compute the scope prefix associated with PDI's parent, in
3443    compilation unit CU.  The result will be allocated on CU's
3444    comp_unit_obstack, or a copy of the already allocated PDI->NAME
3445    field.  NULL is returned if no prefix is necessary.  */
3446 static char *
3447 partial_die_parent_scope (struct partial_die_info *pdi,
3448                           struct dwarf2_cu *cu)
3449 {
3450   char *grandparent_scope;
3451   struct partial_die_info *parent, *real_pdi;
3452
3453   /* We need to look at our parent DIE; if we have a DW_AT_specification,
3454      then this means the parent of the specification DIE.  */
3455
3456   real_pdi = pdi;
3457   while (real_pdi->has_specification)
3458     real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3459
3460   parent = real_pdi->die_parent;
3461   if (parent == NULL)
3462     return NULL;
3463
3464   if (parent->scope_set)
3465     return parent->scope;
3466
3467   fixup_partial_die (parent, cu);
3468
3469   grandparent_scope = partial_die_parent_scope (parent, cu);
3470
3471   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3472      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3473      Work around this problem here.  */
3474   if (cu->language == language_cplus
3475       && parent->tag == DW_TAG_namespace
3476       && strcmp (parent->name, "::") == 0
3477       && grandparent_scope == NULL)
3478     {
3479       parent->scope = NULL;
3480       parent->scope_set = 1;
3481       return NULL;
3482     }
3483
3484   if (parent->tag == DW_TAG_namespace
3485       || parent->tag == DW_TAG_module
3486       || parent->tag == DW_TAG_structure_type
3487       || parent->tag == DW_TAG_class_type
3488       || parent->tag == DW_TAG_interface_type
3489       || parent->tag == DW_TAG_union_type
3490       || parent->tag == DW_TAG_enumeration_type)
3491     {
3492       if (grandparent_scope == NULL)
3493         parent->scope = parent->name;
3494       else
3495         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3496                                          parent->name, 0, cu);
3497     }
3498   else if (parent->tag == DW_TAG_enumerator)
3499     /* Enumerators should not get the name of the enumeration as a prefix.  */
3500     parent->scope = grandparent_scope;
3501   else
3502     {
3503       /* FIXME drow/2004-04-01: What should we be doing with
3504          function-local names?  For partial symbols, we should probably be
3505          ignoring them.  */
3506       complaint (&symfile_complaints,
3507                  _("unhandled containing DIE tag %d for DIE at %d"),
3508                  parent->tag, pdi->offset);
3509       parent->scope = grandparent_scope;
3510     }
3511
3512   parent->scope_set = 1;
3513   return parent->scope;
3514 }
3515
3516 /* Return the fully scoped name associated with PDI, from compilation unit
3517    CU.  The result will be allocated with malloc.  */
3518 static char *
3519 partial_die_full_name (struct partial_die_info *pdi,
3520                        struct dwarf2_cu *cu)
3521 {
3522   char *parent_scope;
3523
3524   parent_scope = partial_die_parent_scope (pdi, cu);
3525   if (parent_scope == NULL)
3526     return NULL;
3527   else
3528     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3529 }
3530
3531 static void
3532 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3533 {
3534   struct objfile *objfile = cu->objfile;
3535   CORE_ADDR addr = 0;
3536   char *actual_name = NULL;
3537   const struct partial_symbol *psym = NULL;
3538   CORE_ADDR baseaddr;
3539   int built_actual_name = 0;
3540
3541   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3542
3543   actual_name = partial_die_full_name (pdi, cu);
3544   if (actual_name)
3545     built_actual_name = 1;
3546
3547   if (actual_name == NULL)
3548     actual_name = pdi->name;
3549
3550   switch (pdi->tag)
3551     {
3552     case DW_TAG_subprogram:
3553       if (pdi->is_external || cu->language == language_ada)
3554         {
3555           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3556              of the global scope.  But in Ada, we want to be able to access
3557              nested procedures globally.  So all Ada subprograms are stored
3558              in the global scope.  */
3559           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3560              mst_text, objfile); */
3561           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3562                                       built_actual_name,
3563                                       VAR_DOMAIN, LOC_BLOCK,
3564                                       &objfile->global_psymbols,
3565                                       0, pdi->lowpc + baseaddr,
3566                                       cu->language, objfile);
3567         }
3568       else
3569         {
3570           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3571              mst_file_text, objfile); */
3572           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3573                                       built_actual_name,
3574                                       VAR_DOMAIN, LOC_BLOCK,
3575                                       &objfile->static_psymbols,
3576                                       0, pdi->lowpc + baseaddr,
3577                                       cu->language, objfile);
3578         }
3579       break;
3580     case DW_TAG_variable:
3581       if (pdi->locdesc)
3582         addr = decode_locdesc (pdi->locdesc, cu);
3583
3584       if (pdi->locdesc
3585           && addr == 0
3586           && !dwarf2_per_objfile->has_section_at_zero)
3587         {
3588           /* A global or static variable may also have been stripped
3589              out by the linker if unused, in which case its address
3590              will be nullified; do not add such variables into partial
3591              symbol table then.  */
3592         }
3593       else if (pdi->is_external)
3594         {
3595           /* Global Variable.
3596              Don't enter into the minimal symbol tables as there is
3597              a minimal symbol table entry from the ELF symbols already.
3598              Enter into partial symbol table if it has a location
3599              descriptor or a type.
3600              If the location descriptor is missing, new_symbol will create
3601              a LOC_UNRESOLVED symbol, the address of the variable will then
3602              be determined from the minimal symbol table whenever the variable
3603              is referenced.
3604              The address for the partial symbol table entry is not
3605              used by GDB, but it comes in handy for debugging partial symbol
3606              table building.  */
3607
3608           if (pdi->locdesc || pdi->has_type)
3609             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3610                                         built_actual_name,
3611                                         VAR_DOMAIN, LOC_STATIC,
3612                                         &objfile->global_psymbols,
3613                                         0, addr + baseaddr,
3614                                         cu->language, objfile);
3615         }
3616       else
3617         {
3618           /* Static Variable. Skip symbols without location descriptors.  */
3619           if (pdi->locdesc == NULL)
3620             {
3621               if (built_actual_name)
3622                 xfree (actual_name);
3623               return;
3624             }
3625           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3626              mst_file_data, objfile); */
3627           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3628                                       built_actual_name,
3629                                       VAR_DOMAIN, LOC_STATIC,
3630                                       &objfile->static_psymbols,
3631                                       0, addr + baseaddr,
3632                                       cu->language, objfile);
3633         }
3634       break;
3635     case DW_TAG_typedef:
3636     case DW_TAG_base_type:
3637     case DW_TAG_subrange_type:
3638       add_psymbol_to_list (actual_name, strlen (actual_name),
3639                            built_actual_name,
3640                            VAR_DOMAIN, LOC_TYPEDEF,
3641                            &objfile->static_psymbols,
3642                            0, (CORE_ADDR) 0, cu->language, objfile);
3643       break;
3644     case DW_TAG_namespace:
3645       add_psymbol_to_list (actual_name, strlen (actual_name),
3646                            built_actual_name,
3647                            VAR_DOMAIN, LOC_TYPEDEF,
3648                            &objfile->global_psymbols,
3649                            0, (CORE_ADDR) 0, cu->language, objfile);
3650       break;
3651     case DW_TAG_class_type:
3652     case DW_TAG_interface_type:
3653     case DW_TAG_structure_type:
3654     case DW_TAG_union_type:
3655     case DW_TAG_enumeration_type:
3656       /* Skip external references.  The DWARF standard says in the section
3657          about "Structure, Union, and Class Type Entries": "An incomplete
3658          structure, union or class type is represented by a structure,
3659          union or class entry that does not have a byte size attribute
3660          and that has a DW_AT_declaration attribute."  */
3661       if (!pdi->has_byte_size && pdi->is_declaration)
3662         {
3663           if (built_actual_name)
3664             xfree (actual_name);
3665           return;
3666         }
3667
3668       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3669          static vs. global.  */
3670       add_psymbol_to_list (actual_name, strlen (actual_name),
3671                            built_actual_name,
3672                            STRUCT_DOMAIN, LOC_TYPEDEF,
3673                            (cu->language == language_cplus
3674                             || cu->language == language_java)
3675                            ? &objfile->global_psymbols
3676                            : &objfile->static_psymbols,
3677                            0, (CORE_ADDR) 0, cu->language, objfile);
3678
3679       break;
3680     case DW_TAG_enumerator:
3681       add_psymbol_to_list (actual_name, strlen (actual_name),
3682                            built_actual_name,
3683                            VAR_DOMAIN, LOC_CONST,
3684                            (cu->language == language_cplus
3685                             || cu->language == language_java)
3686                            ? &objfile->global_psymbols
3687                            : &objfile->static_psymbols,
3688                            0, (CORE_ADDR) 0, cu->language, objfile);
3689       break;
3690     default:
3691       break;
3692     }
3693
3694   if (built_actual_name)
3695     xfree (actual_name);
3696 }
3697
3698 /* Read a partial die corresponding to a namespace; also, add a symbol
3699    corresponding to that namespace to the symbol table.  NAMESPACE is
3700    the name of the enclosing namespace.  */
3701
3702 static void
3703 add_partial_namespace (struct partial_die_info *pdi,
3704                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
3705                        int need_pc, struct dwarf2_cu *cu)
3706 {
3707   /* Add a symbol for the namespace.  */
3708
3709   add_partial_symbol (pdi, cu);
3710
3711   /* Now scan partial symbols in that namespace.  */
3712
3713   if (pdi->has_children)
3714     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3715 }
3716
3717 /* Read a partial die corresponding to a Fortran module.  */
3718
3719 static void
3720 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3721                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3722 {
3723   /* Now scan partial symbols in that module.  */
3724
3725   if (pdi->has_children)
3726     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3727 }
3728
3729 /* Read a partial die corresponding to a subprogram and create a partial
3730    symbol for that subprogram.  When the CU language allows it, this
3731    routine also defines a partial symbol for each nested subprogram
3732    that this subprogram contains.
3733
3734    DIE my also be a lexical block, in which case we simply search
3735    recursively for suprograms defined inside that lexical block.
3736    Again, this is only performed when the CU language allows this
3737    type of definitions.  */
3738
3739 static void
3740 add_partial_subprogram (struct partial_die_info *pdi,
3741                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
3742                         int need_pc, struct dwarf2_cu *cu)
3743 {
3744   if (pdi->tag == DW_TAG_subprogram)
3745     {
3746       if (pdi->has_pc_info)
3747         {
3748           if (pdi->lowpc < *lowpc)
3749             *lowpc = pdi->lowpc;
3750           if (pdi->highpc > *highpc)
3751             *highpc = pdi->highpc;
3752           if (need_pc)
3753             {
3754               CORE_ADDR baseaddr;
3755               struct objfile *objfile = cu->objfile;
3756
3757               baseaddr = ANOFFSET (objfile->section_offsets,
3758                                    SECT_OFF_TEXT (objfile));
3759               addrmap_set_empty (objfile->psymtabs_addrmap,
3760                                  pdi->lowpc + baseaddr,
3761                                  pdi->highpc - 1 + baseaddr,
3762                                  cu->per_cu->v.psymtab);
3763             }
3764           if (!pdi->is_declaration)
3765             /* Ignore subprogram DIEs that do not have a name, they are
3766                illegal.  Do not emit a complaint at this point, we will
3767                do so when we convert this psymtab into a symtab.  */
3768             if (pdi->name)
3769               add_partial_symbol (pdi, cu);
3770         }
3771     }
3772
3773   if (! pdi->has_children)
3774     return;
3775
3776   if (cu->language == language_ada)
3777     {
3778       pdi = pdi->die_child;
3779       while (pdi != NULL)
3780         {
3781           fixup_partial_die (pdi, cu);
3782           if (pdi->tag == DW_TAG_subprogram
3783               || pdi->tag == DW_TAG_lexical_block)
3784             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3785           pdi = pdi->die_sibling;
3786         }
3787     }
3788 }
3789
3790 /* See if we can figure out if the class lives in a namespace.  We do
3791    this by looking for a member function; its demangled name will
3792    contain namespace info, if there is any.  */
3793
3794 static void
3795 guess_structure_name (struct partial_die_info *struct_pdi,
3796                       struct dwarf2_cu *cu)
3797 {
3798   if ((cu->language == language_cplus
3799        || cu->language == language_java)
3800       && cu->has_namespace_info == 0
3801       && struct_pdi->has_children)
3802     {
3803       /* NOTE: carlton/2003-10-07: Getting the info this way changes
3804          what template types look like, because the demangler
3805          frequently doesn't give the same name as the debug info.  We
3806          could fix this by only using the demangled name to get the
3807          prefix (but see comment in read_structure_type).  */
3808
3809       struct partial_die_info *real_pdi;
3810
3811       /* If this DIE (this DIE's specification, if any) has a parent, then
3812          we should not do this.  We'll prepend the parent's fully qualified
3813          name when we create the partial symbol.  */
3814
3815       real_pdi = struct_pdi;
3816       while (real_pdi->has_specification)
3817         real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3818
3819       if (real_pdi->die_parent != NULL)
3820         return;
3821     }
3822 }
3823
3824 /* Read a partial die corresponding to an enumeration type.  */
3825
3826 static void
3827 add_partial_enumeration (struct partial_die_info *enum_pdi,
3828                          struct dwarf2_cu *cu)
3829 {
3830   struct partial_die_info *pdi;
3831
3832   if (enum_pdi->name != NULL)
3833     add_partial_symbol (enum_pdi, cu);
3834
3835   pdi = enum_pdi->die_child;
3836   while (pdi)
3837     {
3838       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3839         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3840       else
3841         add_partial_symbol (pdi, cu);
3842       pdi = pdi->die_sibling;
3843     }
3844 }
3845
3846 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3847    Return the corresponding abbrev, or NULL if the number is zero (indicating
3848    an empty DIE).  In either case *BYTES_READ will be set to the length of
3849    the initial number.  */
3850
3851 static struct abbrev_info *
3852 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3853                  struct dwarf2_cu *cu)
3854 {
3855   bfd *abfd = cu->objfile->obfd;
3856   unsigned int abbrev_number;
3857   struct abbrev_info *abbrev;
3858
3859   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3860
3861   if (abbrev_number == 0)
3862     return NULL;
3863
3864   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3865   if (!abbrev)
3866     {
3867       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3868                       bfd_get_filename (abfd));
3869     }
3870
3871   return abbrev;
3872 }
3873
3874 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3875    Returns a pointer to the end of a series of DIEs, terminated by an empty
3876    DIE.  Any children of the skipped DIEs will also be skipped.  */
3877
3878 static gdb_byte *
3879 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3880 {
3881   struct abbrev_info *abbrev;
3882   unsigned int bytes_read;
3883
3884   while (1)
3885     {
3886       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3887       if (abbrev == NULL)
3888         return info_ptr + bytes_read;
3889       else
3890         info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3891     }
3892 }
3893
3894 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3895    INFO_PTR should point just after the initial uleb128 of a DIE, and the
3896    abbrev corresponding to that skipped uleb128 should be passed in
3897    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
3898    children.  */
3899
3900 static gdb_byte *
3901 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3902               struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3903 {
3904   unsigned int bytes_read;
3905   struct attribute attr;
3906   bfd *abfd = cu->objfile->obfd;
3907   unsigned int form, i;
3908
3909   for (i = 0; i < abbrev->num_attrs; i++)
3910     {
3911       /* The only abbrev we care about is DW_AT_sibling.  */
3912       if (abbrev->attrs[i].name == DW_AT_sibling)
3913         {
3914           read_attribute (&attr, &abbrev->attrs[i],
3915                           abfd, info_ptr, cu);
3916           if (attr.form == DW_FORM_ref_addr)
3917             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
3918           else
3919             return buffer + dwarf2_get_ref_die_offset (&attr);
3920         }
3921
3922       /* If it isn't DW_AT_sibling, skip this attribute.  */
3923       form = abbrev->attrs[i].form;
3924     skip_attribute:
3925       switch (form)
3926         {
3927         case DW_FORM_ref_addr:
3928           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
3929              and later it is offset sized.  */
3930           if (cu->header.version == 2)
3931             info_ptr += cu->header.addr_size;
3932           else
3933             info_ptr += cu->header.offset_size;
3934           break;
3935         case DW_FORM_addr:
3936           info_ptr += cu->header.addr_size;
3937           break;
3938         case DW_FORM_data1:
3939         case DW_FORM_ref1:
3940         case DW_FORM_flag:
3941           info_ptr += 1;
3942           break;
3943         case DW_FORM_flag_present:
3944           break;
3945         case DW_FORM_data2:
3946         case DW_FORM_ref2:
3947           info_ptr += 2;
3948           break;
3949         case DW_FORM_data4:
3950         case DW_FORM_ref4:
3951           info_ptr += 4;
3952           break;
3953         case DW_FORM_data8:
3954         case DW_FORM_ref8:
3955         case DW_FORM_sig8:
3956           info_ptr += 8;
3957           break;
3958         case DW_FORM_string:
3959           read_direct_string (abfd, info_ptr, &bytes_read);
3960           info_ptr += bytes_read;
3961           break;
3962         case DW_FORM_sec_offset:
3963         case DW_FORM_strp:
3964           info_ptr += cu->header.offset_size;
3965           break;
3966         case DW_FORM_exprloc:
3967         case DW_FORM_block:
3968           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3969           info_ptr += bytes_read;
3970           break;
3971         case DW_FORM_block1:
3972           info_ptr += 1 + read_1_byte (abfd, info_ptr);
3973           break;
3974         case DW_FORM_block2:
3975           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
3976           break;
3977         case DW_FORM_block4:
3978           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
3979           break;
3980         case DW_FORM_sdata:
3981         case DW_FORM_udata:
3982         case DW_FORM_ref_udata:
3983           info_ptr = skip_leb128 (abfd, info_ptr);
3984           break;
3985         case DW_FORM_indirect:
3986           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3987           info_ptr += bytes_read;
3988           /* We need to continue parsing from here, so just go back to
3989              the top.  */
3990           goto skip_attribute;
3991
3992         default:
3993           error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
3994                  dwarf_form_name (form),
3995                  bfd_get_filename (abfd));
3996         }
3997     }
3998
3999   if (abbrev->has_children)
4000     return skip_children (buffer, info_ptr, cu);
4001   else
4002     return info_ptr;
4003 }
4004
4005 /* Locate ORIG_PDI's sibling.
4006    INFO_PTR should point to the start of the next DIE after ORIG_PDI
4007    in BUFFER.  */
4008
4009 static gdb_byte *
4010 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4011                     gdb_byte *buffer, gdb_byte *info_ptr,
4012                     bfd *abfd, struct dwarf2_cu *cu)
4013 {
4014   /* Do we know the sibling already?  */
4015
4016   if (orig_pdi->sibling)
4017     return orig_pdi->sibling;
4018
4019   /* Are there any children to deal with?  */
4020
4021   if (!orig_pdi->has_children)
4022     return info_ptr;
4023
4024   /* Skip the children the long way.  */
4025
4026   return skip_children (buffer, info_ptr, cu);
4027 }
4028
4029 /* Expand this partial symbol table into a full symbol table.  */
4030
4031 static void
4032 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4033 {
4034   if (pst != NULL)
4035     {
4036       if (pst->readin)
4037         {
4038           warning (_("bug: psymtab for %s is already read in."), pst->filename);
4039         }
4040       else
4041         {
4042           if (info_verbose)
4043             {
4044               printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4045               gdb_flush (gdb_stdout);
4046             }
4047
4048           /* Restore our global data.  */
4049           dwarf2_per_objfile = objfile_data (pst->objfile,
4050                                              dwarf2_objfile_data_key);
4051
4052           /* If this psymtab is constructed from a debug-only objfile, the
4053              has_section_at_zero flag will not necessarily be correct.  We
4054              can get the correct value for this flag by looking at the data
4055              associated with the (presumably stripped) associated objfile.  */
4056           if (pst->objfile->separate_debug_objfile_backlink)
4057             {
4058               struct dwarf2_per_objfile *dpo_backlink
4059                 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4060                                 dwarf2_objfile_data_key);
4061
4062               dwarf2_per_objfile->has_section_at_zero
4063                 = dpo_backlink->has_section_at_zero;
4064             }
4065
4066           psymtab_to_symtab_1 (pst);
4067
4068           /* Finish up the debug error message.  */
4069           if (info_verbose)
4070             printf_filtered (_("done.\n"));
4071         }
4072     }
4073 }
4074
4075 /* Add PER_CU to the queue.  */
4076
4077 static void
4078 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4079 {
4080   struct dwarf2_queue_item *item;
4081
4082   per_cu->queued = 1;
4083   item = xmalloc (sizeof (*item));
4084   item->per_cu = per_cu;
4085   item->next = NULL;
4086
4087   if (dwarf2_queue == NULL)
4088     dwarf2_queue = item;
4089   else
4090     dwarf2_queue_tail->next = item;
4091
4092   dwarf2_queue_tail = item;
4093 }
4094
4095 /* Process the queue.  */
4096
4097 static void
4098 process_queue (struct objfile *objfile)
4099 {
4100   struct dwarf2_queue_item *item, *next_item;
4101
4102   /* The queue starts out with one item, but following a DIE reference
4103      may load a new CU, adding it to the end of the queue.  */
4104   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4105     {
4106       if (dwarf2_per_objfile->using_index
4107           ? !item->per_cu->v.quick->symtab
4108           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4109         process_full_comp_unit (item->per_cu);
4110
4111       item->per_cu->queued = 0;
4112       next_item = item->next;
4113       xfree (item);
4114     }
4115
4116   dwarf2_queue_tail = NULL;
4117 }
4118
4119 /* Free all allocated queue entries.  This function only releases anything if
4120    an error was thrown; if the queue was processed then it would have been
4121    freed as we went along.  */
4122
4123 static void
4124 dwarf2_release_queue (void *dummy)
4125 {
4126   struct dwarf2_queue_item *item, *last;
4127
4128   item = dwarf2_queue;
4129   while (item)
4130     {
4131       /* Anything still marked queued is likely to be in an
4132          inconsistent state, so discard it.  */
4133       if (item->per_cu->queued)
4134         {
4135           if (item->per_cu->cu != NULL)
4136             free_one_cached_comp_unit (item->per_cu->cu);
4137           item->per_cu->queued = 0;
4138         }
4139
4140       last = item;
4141       item = item->next;
4142       xfree (last);
4143     }
4144
4145   dwarf2_queue = dwarf2_queue_tail = NULL;
4146 }
4147
4148 /* Read in full symbols for PST, and anything it depends on.  */
4149
4150 static void
4151 psymtab_to_symtab_1 (struct partial_symtab *pst)
4152 {
4153   struct dwarf2_per_cu_data *per_cu;
4154   struct cleanup *back_to;
4155   int i;
4156
4157   for (i = 0; i < pst->number_of_dependencies; i++)
4158     if (!pst->dependencies[i]->readin)
4159       {
4160         /* Inform about additional files that need to be read in.  */
4161         if (info_verbose)
4162           {
4163             /* FIXME: i18n: Need to make this a single string.  */
4164             fputs_filtered (" ", gdb_stdout);
4165             wrap_here ("");
4166             fputs_filtered ("and ", gdb_stdout);
4167             wrap_here ("");
4168             printf_filtered ("%s...", pst->dependencies[i]->filename);
4169             wrap_here ("");     /* Flush output */
4170             gdb_flush (gdb_stdout);
4171           }
4172         psymtab_to_symtab_1 (pst->dependencies[i]);
4173       }
4174
4175   per_cu = pst->read_symtab_private;
4176
4177   if (per_cu == NULL)
4178     {
4179       /* It's an include file, no symbols to read for it.
4180          Everything is in the parent symtab.  */
4181       pst->readin = 1;
4182       return;
4183     }
4184
4185   dw2_do_instantiate_symtab (pst->objfile, per_cu);
4186 }
4187
4188 /* Load the DIEs associated with PER_CU into memory.  */
4189
4190 static void
4191 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4192 {
4193   bfd *abfd = objfile->obfd;
4194   struct dwarf2_cu *cu;
4195   unsigned int offset;
4196   gdb_byte *info_ptr, *beg_of_comp_unit;
4197   struct cleanup *back_to, *free_cu_cleanup;
4198   struct attribute *attr;
4199
4200   gdb_assert (! per_cu->from_debug_types);
4201
4202   /* Set local variables from the partial symbol table info.  */
4203   offset = per_cu->offset;
4204
4205   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4206   info_ptr = dwarf2_per_objfile->info.buffer + offset;
4207   beg_of_comp_unit = info_ptr;
4208
4209   cu = alloc_one_comp_unit (objfile);
4210
4211   /* If an error occurs while loading, release our storage.  */
4212   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4213
4214   /* Read in the comp_unit header.  */
4215   info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4216
4217   /* Complete the cu_header.  */
4218   cu->header.offset = offset;
4219   cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4220
4221   /* Read the abbrevs for this compilation unit.  */
4222   dwarf2_read_abbrevs (abfd, cu);
4223   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
4224
4225   /* Link this compilation unit into the compilation unit tree.  */
4226   per_cu->cu = cu;
4227   cu->per_cu = per_cu;
4228   cu->type_hash = per_cu->type_hash;
4229
4230   cu->dies = read_comp_unit (info_ptr, cu);
4231
4232   /* We try not to read any attributes in this function, because not
4233      all objfiles needed for references have been loaded yet, and symbol
4234      table processing isn't initialized.  But we have to set the CU language,
4235      or we won't be able to build types correctly.  */
4236   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4237   if (attr)
4238     set_cu_language (DW_UNSND (attr), cu);
4239   else
4240     set_cu_language (language_minimal, cu);
4241
4242   /* Similarly, if we do not read the producer, we can not apply
4243      producer-specific interpretation.  */
4244   attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4245   if (attr)
4246     cu->producer = DW_STRING (attr);
4247
4248   /* Link this CU into read_in_chain.  */
4249   per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4250   dwarf2_per_objfile->read_in_chain = per_cu;
4251
4252   do_cleanups (back_to);
4253
4254   /* We've successfully allocated this compilation unit.  Let our caller
4255      clean it up when finished with it.  */
4256   discard_cleanups (free_cu_cleanup);
4257 }
4258
4259 /* Generate full symbol information for PST and CU, whose DIEs have
4260    already been loaded into memory.  */
4261
4262 static void
4263 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4264 {
4265   struct dwarf2_cu *cu = per_cu->cu;
4266   struct objfile *objfile = per_cu->objfile;
4267   CORE_ADDR lowpc, highpc;
4268   struct symtab *symtab;
4269   struct cleanup *back_to;
4270   CORE_ADDR baseaddr;
4271
4272   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4273
4274   buildsym_init ();
4275   back_to = make_cleanup (really_free_pendings, NULL);
4276
4277   cu->list_in_scope = &file_symbols;
4278
4279   dwarf2_find_base_address (cu->dies, cu);
4280
4281   /* Do line number decoding in read_file_scope () */
4282   process_die (cu->dies, cu);
4283
4284   /* Some compilers don't define a DW_AT_high_pc attribute for the
4285      compilation unit.  If the DW_AT_high_pc is missing, synthesize
4286      it, by scanning the DIE's below the compilation unit.  */
4287   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4288
4289   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4290
4291   /* Set symtab language to language from DW_AT_language.
4292      If the compilation is from a C file generated by language preprocessors,
4293      do not set the language if it was already deduced by start_subfile.  */
4294   if (symtab != NULL
4295       && !(cu->language == language_c && symtab->language != language_c))
4296     {
4297       symtab->language = cu->language;
4298     }
4299
4300   if (dwarf2_per_objfile->using_index)
4301     per_cu->v.quick->symtab = symtab;
4302   else
4303     {
4304       struct partial_symtab *pst = per_cu->v.psymtab;
4305       pst->symtab = symtab;
4306       pst->readin = 1;
4307     }
4308
4309   do_cleanups (back_to);
4310 }
4311
4312 /* Process a die and its children.  */
4313
4314 static void
4315 process_die (struct die_info *die, struct dwarf2_cu *cu)
4316 {
4317   switch (die->tag)
4318     {
4319     case DW_TAG_padding:
4320       break;
4321     case DW_TAG_compile_unit:
4322       read_file_scope (die, cu);
4323       break;
4324     case DW_TAG_type_unit:
4325       read_type_unit_scope (die, cu);
4326       break;
4327     case DW_TAG_subprogram:
4328     case DW_TAG_inlined_subroutine:
4329       read_func_scope (die, cu);
4330       break;
4331     case DW_TAG_lexical_block:
4332     case DW_TAG_try_block:
4333     case DW_TAG_catch_block:
4334       read_lexical_block_scope (die, cu);
4335       break;
4336     case DW_TAG_class_type:
4337     case DW_TAG_interface_type:
4338     case DW_TAG_structure_type:
4339     case DW_TAG_union_type:
4340       process_structure_scope (die, cu);
4341       break;
4342     case DW_TAG_enumeration_type:
4343       process_enumeration_scope (die, cu);
4344       break;
4345
4346     /* These dies have a type, but processing them does not create
4347        a symbol or recurse to process the children.  Therefore we can
4348        read them on-demand through read_type_die.  */
4349     case DW_TAG_subroutine_type:
4350     case DW_TAG_set_type:
4351     case DW_TAG_array_type:
4352     case DW_TAG_pointer_type:
4353     case DW_TAG_ptr_to_member_type:
4354     case DW_TAG_reference_type:
4355     case DW_TAG_string_type:
4356       break;
4357
4358     case DW_TAG_base_type:
4359     case DW_TAG_subrange_type:
4360     case DW_TAG_typedef:
4361       /* Add a typedef symbol for the type definition, if it has a
4362          DW_AT_name.  */
4363       new_symbol (die, read_type_die (die, cu), cu);
4364       break;
4365     case DW_TAG_common_block:
4366       read_common_block (die, cu);
4367       break;
4368     case DW_TAG_common_inclusion:
4369       break;
4370     case DW_TAG_namespace:
4371       processing_has_namespace_info = 1;
4372       read_namespace (die, cu);
4373       break;
4374     case DW_TAG_module:
4375       processing_has_namespace_info = 1;
4376       read_module (die, cu);
4377       break;
4378     case DW_TAG_imported_declaration:
4379     case DW_TAG_imported_module:
4380       processing_has_namespace_info = 1;
4381       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4382                                  || cu->language != language_fortran))
4383         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4384                    dwarf_tag_name (die->tag));
4385       read_import_statement (die, cu);
4386       break;
4387     default:
4388       new_symbol (die, NULL, cu);
4389       break;
4390     }
4391 }
4392
4393 /* A helper function for dwarf2_compute_name which determines whether DIE
4394    needs to have the name of the scope prepended to the name listed in the
4395    die.  */
4396
4397 static int
4398 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4399 {
4400   struct attribute *attr;
4401
4402   switch (die->tag)
4403     {
4404     case DW_TAG_namespace:
4405     case DW_TAG_typedef:
4406     case DW_TAG_class_type:
4407     case DW_TAG_interface_type:
4408     case DW_TAG_structure_type:
4409     case DW_TAG_union_type:
4410     case DW_TAG_enumeration_type:
4411     case DW_TAG_enumerator:
4412     case DW_TAG_subprogram:
4413     case DW_TAG_member:
4414       return 1;
4415
4416     case DW_TAG_variable:
4417       /* We only need to prefix "globally" visible variables.  These include
4418          any variable marked with DW_AT_external or any variable that
4419          lives in a namespace.  [Variables in anonymous namespaces
4420          require prefixing, but they are not DW_AT_external.]  */
4421
4422       if (dwarf2_attr (die, DW_AT_specification, cu))
4423         {
4424           struct dwarf2_cu *spec_cu = cu;
4425
4426           return die_needs_namespace (die_specification (die, &spec_cu),
4427                                       spec_cu);
4428         }
4429
4430       attr = dwarf2_attr (die, DW_AT_external, cu);
4431       if (attr == NULL && die->parent->tag != DW_TAG_namespace
4432           && die->parent->tag != DW_TAG_module)
4433         return 0;
4434       /* A variable in a lexical block of some kind does not need a
4435          namespace, even though in C++ such variables may be external
4436          and have a mangled name.  */
4437       if (die->parent->tag ==  DW_TAG_lexical_block
4438           || die->parent->tag ==  DW_TAG_try_block
4439           || die->parent->tag ==  DW_TAG_catch_block
4440           || die->parent->tag == DW_TAG_subprogram)
4441         return 0;
4442       return 1;
4443
4444     default:
4445       return 0;
4446     }
4447 }
4448
4449 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
4450    compute the physname for the object, which include a method's
4451    formal parameters (C++/Java) and return type (Java).
4452
4453    For Ada, return the DIE's linkage name rather than the fully qualified
4454    name.  PHYSNAME is ignored..
4455
4456    The result is allocated on the objfile_obstack and canonicalized.  */
4457
4458 static const char *
4459 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4460                      int physname)
4461 {
4462   if (name == NULL)
4463     name = dwarf2_name (die, cu);
4464
4465   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4466      compute it by typename_concat inside GDB.  */
4467   if (cu->language == language_ada
4468       || (cu->language == language_fortran && physname))
4469     {
4470       /* For Ada unit, we prefer the linkage name over the name, as
4471          the former contains the exported name, which the user expects
4472          to be able to reference.  Ideally, we want the user to be able
4473          to reference this entity using either natural or linkage name,
4474          but we haven't started looking at this enhancement yet.  */
4475       struct attribute *attr;
4476
4477       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4478       if (attr == NULL)
4479         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4480       if (attr && DW_STRING (attr))
4481         return DW_STRING (attr);
4482     }
4483
4484   /* These are the only languages we know how to qualify names in.  */
4485   if (name != NULL
4486       && (cu->language == language_cplus || cu->language == language_java
4487           || cu->language == language_fortran))
4488     {
4489       if (die_needs_namespace (die, cu))
4490         {
4491           long length;
4492           char *prefix;
4493           struct ui_file *buf;
4494
4495           prefix = determine_prefix (die, cu);
4496           buf = mem_fileopen ();
4497           if (*prefix != '\0')
4498             {
4499               char *prefixed_name = typename_concat (NULL, prefix, name,
4500                                                      physname, cu);
4501
4502               fputs_unfiltered (prefixed_name, buf);
4503               xfree (prefixed_name);
4504             }
4505           else
4506             fputs_unfiltered (name ? name : "", buf);
4507
4508           /* For Java and C++ methods, append formal parameter type
4509              information, if PHYSNAME.  */
4510
4511           if (physname && die->tag == DW_TAG_subprogram
4512               && (cu->language == language_cplus
4513                   || cu->language == language_java))
4514             {
4515               struct type *type = read_type_die (die, cu);
4516
4517               c_type_print_args (type, buf, 0, cu->language);
4518
4519               if (cu->language == language_java)
4520                 {
4521                   /* For java, we must append the return type to method
4522                      names. */
4523                   if (die->tag == DW_TAG_subprogram)
4524                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4525                                      0, 0);
4526                 }
4527               else if (cu->language == language_cplus)
4528                 {
4529                   if (TYPE_NFIELDS (type) > 0
4530                       && TYPE_FIELD_ARTIFICIAL (type, 0)
4531                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4532                     fputs_unfiltered (" const", buf);
4533                 }
4534             }
4535
4536           name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4537                                        &length);
4538           ui_file_delete (buf);
4539
4540           if (cu->language == language_cplus)
4541             {
4542               char *cname
4543                 = dwarf2_canonicalize_name (name, cu,
4544                                             &cu->objfile->objfile_obstack);
4545
4546               if (cname != NULL)
4547                 name = cname;
4548             }
4549         }
4550     }
4551
4552   return name;
4553 }
4554
4555 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4556    If scope qualifiers are appropriate they will be added.  The result
4557    will be allocated on the objfile_obstack, or NULL if the DIE does
4558    not have a name.  NAME may either be from a previous call to
4559    dwarf2_name or NULL.
4560
4561    The output string will be canonicalized (if C++/Java). */
4562
4563 static const char *
4564 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4565 {
4566   return dwarf2_compute_name (name, die, cu, 0);
4567 }
4568
4569 /* Construct a physname for the given DIE in CU.  NAME may either be
4570    from a previous call to dwarf2_name or NULL.  The result will be
4571    allocated on the objfile_objstack or NULL if the DIE does not have a
4572    name.
4573
4574    The output string will be canonicalized (if C++/Java).  */
4575
4576 static const char *
4577 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4578 {
4579   return dwarf2_compute_name (name, die, cu, 1);
4580 }
4581
4582 /* Read the import statement specified by the given die and record it.  */
4583
4584 static void
4585 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4586 {
4587   struct attribute *import_attr;
4588   struct die_info *imported_die;
4589   struct dwarf2_cu *imported_cu;
4590   const char *imported_name;
4591   const char *imported_name_prefix;
4592   const char *canonical_name;
4593   const char *import_alias;
4594   const char *imported_declaration = NULL;
4595   const char *import_prefix;
4596
4597   char *temp;
4598
4599   import_attr = dwarf2_attr (die, DW_AT_import, cu);
4600   if (import_attr == NULL)
4601     {
4602       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4603                  dwarf_tag_name (die->tag));
4604       return;
4605     }
4606
4607   imported_cu = cu;
4608   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4609   imported_name = dwarf2_name (imported_die, imported_cu);
4610   if (imported_name == NULL)
4611     {
4612       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4613
4614         The import in the following code:
4615         namespace A
4616           {
4617             typedef int B;
4618           }
4619
4620         int main ()
4621           {
4622             using A::B;
4623             B b;
4624             return b;
4625           }
4626
4627         ...
4628          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4629             <52>   DW_AT_decl_file   : 1
4630             <53>   DW_AT_decl_line   : 6
4631             <54>   DW_AT_import      : <0x75>
4632          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4633             <59>   DW_AT_name        : B
4634             <5b>   DW_AT_decl_file   : 1
4635             <5c>   DW_AT_decl_line   : 2
4636             <5d>   DW_AT_type        : <0x6e>
4637         ...
4638          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4639             <76>   DW_AT_byte_size   : 4
4640             <77>   DW_AT_encoding    : 5        (signed)
4641
4642         imports the wrong die ( 0x75 instead of 0x58 ).
4643         This case will be ignored until the gcc bug is fixed.  */
4644       return;
4645     }
4646
4647   /* Figure out the local name after import.  */
4648   import_alias = dwarf2_name (die, cu);
4649
4650   /* Figure out where the statement is being imported to.  */
4651   import_prefix = determine_prefix (die, cu);
4652
4653   /* Figure out what the scope of the imported die is and prepend it
4654      to the name of the imported die.  */
4655   imported_name_prefix = determine_prefix (imported_die, imported_cu);
4656
4657   if (imported_die->tag != DW_TAG_namespace
4658       && imported_die->tag != DW_TAG_module)
4659     {
4660       imported_declaration = imported_name;
4661       canonical_name = imported_name_prefix;
4662     }
4663   else if (strlen (imported_name_prefix) > 0)
4664     {
4665       temp = alloca (strlen (imported_name_prefix)
4666                      + 2 + strlen (imported_name) + 1);
4667       strcpy (temp, imported_name_prefix);
4668       strcat (temp, "::");
4669       strcat (temp, imported_name);
4670       canonical_name = temp;
4671     }
4672   else
4673     canonical_name = imported_name;
4674
4675   cp_add_using_directive (import_prefix,
4676                           canonical_name,
4677                           import_alias,
4678                           imported_declaration,
4679                           &cu->objfile->objfile_obstack);
4680 }
4681
4682 static void
4683 initialize_cu_func_list (struct dwarf2_cu *cu)
4684 {
4685   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4686 }
4687
4688 static void
4689 free_cu_line_header (void *arg)
4690 {
4691   struct dwarf2_cu *cu = arg;
4692
4693   free_line_header (cu->line_header);
4694   cu->line_header = NULL;
4695 }
4696
4697 static void
4698 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
4699                          char **name, char **comp_dir)
4700 {
4701   struct attribute *attr;
4702
4703   *name = NULL;
4704   *comp_dir = NULL;
4705
4706   /* Find the filename.  Do not use dwarf2_name here, since the filename
4707      is not a source language identifier.  */
4708   attr = dwarf2_attr (die, DW_AT_name, cu);
4709   if (attr)
4710     {
4711       *name = DW_STRING (attr);
4712     }
4713
4714   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4715   if (attr)
4716     *comp_dir = DW_STRING (attr);
4717   else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
4718     {
4719       *comp_dir = ldirname (*name);
4720       if (*comp_dir != NULL)
4721         make_cleanup (xfree, *comp_dir);
4722     }
4723   if (*comp_dir != NULL)
4724     {
4725       /* Irix 6.2 native cc prepends <machine>.: to the compilation
4726          directory, get rid of it.  */
4727       char *cp = strchr (*comp_dir, ':');
4728
4729       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
4730         *comp_dir = cp + 1;
4731     }
4732
4733   if (*name == NULL)
4734     *name = "<unknown>";
4735 }
4736
4737 static void
4738 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
4739 {
4740   struct objfile *objfile = cu->objfile;
4741   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4742   CORE_ADDR lowpc = ((CORE_ADDR) -1);
4743   CORE_ADDR highpc = ((CORE_ADDR) 0);
4744   struct attribute *attr;
4745   char *name = NULL;
4746   char *comp_dir = NULL;
4747   struct die_info *child_die;
4748   bfd *abfd = objfile->obfd;
4749   struct line_header *line_header = 0;
4750   CORE_ADDR baseaddr;
4751
4752   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4753
4754   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
4755
4756   /* If we didn't find a lowpc, set it to highpc to avoid complaints
4757      from finish_block.  */
4758   if (lowpc == ((CORE_ADDR) -1))
4759     lowpc = highpc;
4760   lowpc += baseaddr;
4761   highpc += baseaddr;
4762
4763   find_file_and_directory (die, cu, &name, &comp_dir);
4764
4765   attr = dwarf2_attr (die, DW_AT_language, cu);
4766   if (attr)
4767     {
4768       set_cu_language (DW_UNSND (attr), cu);
4769     }
4770
4771   attr = dwarf2_attr (die, DW_AT_producer, cu);
4772   if (attr)
4773     cu->producer = DW_STRING (attr);
4774
4775   /* We assume that we're processing GCC output. */
4776   processing_gcc_compilation = 2;
4777
4778   processing_has_namespace_info = 0;
4779
4780   start_symtab (name, comp_dir, lowpc);
4781   record_debugformat ("DWARF 2");
4782   record_producer (cu->producer);
4783
4784   initialize_cu_func_list (cu);
4785
4786   /* Decode line number information if present.  We do this before
4787      processing child DIEs, so that the line header table is available
4788      for DW_AT_decl_file.  */
4789   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4790   if (attr)
4791     {
4792       unsigned int line_offset = DW_UNSND (attr);
4793       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
4794       if (line_header)
4795         {
4796           cu->line_header = line_header;
4797           make_cleanup (free_cu_line_header, cu);
4798           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
4799         }
4800     }
4801
4802   /* Process all dies in compilation unit.  */
4803   if (die->child != NULL)
4804     {
4805       child_die = die->child;
4806       while (child_die && child_die->tag)
4807         {
4808           process_die (child_die, cu);
4809           child_die = sibling_die (child_die);
4810         }
4811     }
4812
4813   /* Decode macro information, if present.  Dwarf 2 macro information
4814      refers to information in the line number info statement program
4815      header, so we can only read it if we've read the header
4816      successfully.  */
4817   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
4818   if (attr && line_header)
4819     {
4820       unsigned int macro_offset = DW_UNSND (attr);
4821
4822       dwarf_decode_macros (line_header, macro_offset,
4823                            comp_dir, abfd, cu);
4824     }
4825   do_cleanups (back_to);
4826 }
4827
4828 /* For TUs we want to skip the first top level sibling if it's not the
4829    actual type being defined by this TU.  In this case the first top
4830    level sibling is there to provide context only.  */
4831
4832 static void
4833 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
4834 {
4835   struct objfile *objfile = cu->objfile;
4836   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4837   CORE_ADDR lowpc;
4838   struct attribute *attr;
4839   char *name = NULL;
4840   char *comp_dir = NULL;
4841   struct die_info *child_die;
4842   bfd *abfd = objfile->obfd;
4843
4844   /* start_symtab needs a low pc, but we don't really have one.
4845      Do what read_file_scope would do in the absence of such info.  */
4846   lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4847
4848   /* Find the filename.  Do not use dwarf2_name here, since the filename
4849      is not a source language identifier.  */
4850   attr = dwarf2_attr (die, DW_AT_name, cu);
4851   if (attr)
4852     name = DW_STRING (attr);
4853
4854   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
4855   if (attr)
4856     comp_dir = DW_STRING (attr);
4857   else if (name != NULL && IS_ABSOLUTE_PATH (name))
4858     {
4859       comp_dir = ldirname (name);
4860       if (comp_dir != NULL)
4861         make_cleanup (xfree, comp_dir);
4862     }
4863
4864   if (name == NULL)
4865     name = "<unknown>";
4866
4867   attr = dwarf2_attr (die, DW_AT_language, cu);
4868   if (attr)
4869     set_cu_language (DW_UNSND (attr), cu);
4870
4871   /* This isn't technically needed today.  It is done for symmetry
4872      with read_file_scope.  */
4873   attr = dwarf2_attr (die, DW_AT_producer, cu);
4874   if (attr)
4875     cu->producer = DW_STRING (attr);
4876
4877   /* We assume that we're processing GCC output. */
4878   processing_gcc_compilation = 2;
4879
4880   processing_has_namespace_info = 0;
4881
4882   start_symtab (name, comp_dir, lowpc);
4883   record_debugformat ("DWARF 2");
4884   record_producer (cu->producer);
4885
4886   /* Process the dies in the type unit.  */
4887   if (die->child == NULL)
4888     {
4889       dump_die_for_error (die);
4890       error (_("Dwarf Error: Missing children for type unit [in module %s]"),
4891              bfd_get_filename (abfd));
4892     }
4893
4894   child_die = die->child;
4895
4896   while (child_die && child_die->tag)
4897     {
4898       process_die (child_die, cu);
4899
4900       child_die = sibling_die (child_die);
4901     }
4902
4903   do_cleanups (back_to);
4904 }
4905
4906 static void
4907 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
4908                      struct dwarf2_cu *cu)
4909 {
4910   struct function_range *thisfn;
4911
4912   thisfn = (struct function_range *)
4913     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
4914   thisfn->name = name;
4915   thisfn->lowpc = lowpc;
4916   thisfn->highpc = highpc;
4917   thisfn->seen_line = 0;
4918   thisfn->next = NULL;
4919
4920   if (cu->last_fn == NULL)
4921       cu->first_fn = thisfn;
4922   else
4923       cu->last_fn->next = thisfn;
4924
4925   cu->last_fn = thisfn;
4926 }
4927
4928 /* qsort helper for inherit_abstract_dies.  */
4929
4930 static int
4931 unsigned_int_compar (const void *ap, const void *bp)
4932 {
4933   unsigned int a = *(unsigned int *) ap;
4934   unsigned int b = *(unsigned int *) bp;
4935
4936   return (a > b) - (b > a);
4937 }
4938
4939 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
4940    Inherit only the children of the DW_AT_abstract_origin DIE not being already
4941    referenced by DW_AT_abstract_origin from the children of the current DIE.  */
4942
4943 static void
4944 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
4945 {
4946   struct die_info *child_die;
4947   unsigned die_children_count;
4948   /* CU offsets which were referenced by children of the current DIE.  */
4949   unsigned *offsets;
4950   unsigned *offsets_end, *offsetp;
4951   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
4952   struct die_info *origin_die;
4953   /* Iterator of the ORIGIN_DIE children.  */
4954   struct die_info *origin_child_die;
4955   struct cleanup *cleanups;
4956   struct attribute *attr;
4957
4958   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
4959   if (!attr)
4960     return;
4961
4962   origin_die = follow_die_ref (die, attr, &cu);
4963   if (die->tag != origin_die->tag
4964       && !(die->tag == DW_TAG_inlined_subroutine
4965            && origin_die->tag == DW_TAG_subprogram))
4966     complaint (&symfile_complaints,
4967                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
4968                die->offset, origin_die->offset);
4969
4970   child_die = die->child;
4971   die_children_count = 0;
4972   while (child_die && child_die->tag)
4973     {
4974       child_die = sibling_die (child_die);
4975       die_children_count++;
4976     }
4977   offsets = xmalloc (sizeof (*offsets) * die_children_count);
4978   cleanups = make_cleanup (xfree, offsets);
4979
4980   offsets_end = offsets;
4981   child_die = die->child;
4982   while (child_die && child_die->tag)
4983     {
4984       /* For each CHILD_DIE, find the corresponding child of
4985          ORIGIN_DIE.  If there is more than one layer of
4986          DW_AT_abstract_origin, follow them all; there shouldn't be,
4987          but GCC versions at least through 4.4 generate this (GCC PR
4988          40573).  */
4989       struct die_info *child_origin_die = child_die;
4990
4991       while (1)
4992         {
4993           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin, cu);
4994           if (attr == NULL)
4995             break;
4996           child_origin_die = follow_die_ref (child_origin_die, attr, &cu);
4997         }
4998
4999       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5000          counterpart may exist.  */
5001       if (child_origin_die != child_die)
5002         {
5003           if (child_die->tag != child_origin_die->tag
5004               && !(child_die->tag == DW_TAG_inlined_subroutine
5005                    && child_origin_die->tag == DW_TAG_subprogram))
5006             complaint (&symfile_complaints,
5007                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5008                          "different tags"), child_die->offset,
5009                        child_origin_die->offset);
5010           if (child_origin_die->parent != origin_die)
5011             complaint (&symfile_complaints,
5012                        _("Child DIE 0x%x and its abstract origin 0x%x have "
5013                          "different parents"), child_die->offset,
5014                        child_origin_die->offset);
5015           else
5016             *offsets_end++ = child_origin_die->offset;
5017         }
5018       child_die = sibling_die (child_die);
5019     }
5020   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5021          unsigned_int_compar);
5022   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5023     if (offsetp[-1] == *offsetp)
5024       complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5025                                         "to DIE 0x%x as their abstract origin"),
5026                  die->offset, *offsetp);
5027
5028   offsetp = offsets;
5029   origin_child_die = origin_die->child;
5030   while (origin_child_die && origin_child_die->tag)
5031     {
5032       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
5033       while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5034         offsetp++;
5035       if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5036         {
5037           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
5038           process_die (origin_child_die, cu);
5039         }
5040       origin_child_die = sibling_die (origin_child_die);
5041     }
5042
5043   do_cleanups (cleanups);
5044 }
5045
5046 static void
5047 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5048 {
5049   struct objfile *objfile = cu->objfile;
5050   struct context_stack *new;
5051   CORE_ADDR lowpc;
5052   CORE_ADDR highpc;
5053   struct die_info *child_die;
5054   struct attribute *attr, *call_line, *call_file;
5055   char *name;
5056   CORE_ADDR baseaddr;
5057   struct block *block;
5058   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5059   VEC (symbolp) *template_args = NULL;
5060   struct template_symbol *templ_func = NULL;
5061
5062   if (inlined_func)
5063     {
5064       /* If we do not have call site information, we can't show the
5065          caller of this inlined function.  That's too confusing, so
5066          only use the scope for local variables.  */
5067       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5068       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5069       if (call_line == NULL || call_file == NULL)
5070         {
5071           read_lexical_block_scope (die, cu);
5072           return;
5073         }
5074     }
5075
5076   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5077
5078   name = dwarf2_name (die, cu);
5079
5080   /* Ignore functions with missing or empty names.  These are actually
5081      illegal according to the DWARF standard.  */
5082   if (name == NULL)
5083     {
5084       complaint (&symfile_complaints,
5085                  _("missing name for subprogram DIE at %d"), die->offset);
5086       return;
5087     }
5088
5089   /* Ignore functions with missing or invalid low and high pc attributes.  */
5090   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5091     {
5092       attr = dwarf2_attr (die, DW_AT_external, cu);
5093       if (!attr || !DW_UNSND (attr))
5094         complaint (&symfile_complaints,
5095                    _("cannot get low and high bounds for subprogram DIE at %d"),
5096                    die->offset);
5097       return;
5098     }
5099
5100   lowpc += baseaddr;
5101   highpc += baseaddr;
5102
5103   /* Record the function range for dwarf_decode_lines.  */
5104   add_to_cu_func_list (name, lowpc, highpc, cu);
5105
5106   /* If we have any template arguments, then we must allocate a
5107      different sort of symbol.  */
5108   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5109     {
5110       if (child_die->tag == DW_TAG_template_type_param
5111           || child_die->tag == DW_TAG_template_value_param)
5112         {
5113           templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5114                                        struct template_symbol);
5115           templ_func->base.is_cplus_template_function = 1;
5116           break;
5117         }
5118     }
5119
5120   new = push_context (0, lowpc);
5121   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5122                                (struct symbol *) templ_func);
5123
5124   /* If there is a location expression for DW_AT_frame_base, record
5125      it.  */
5126   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5127   if (attr)
5128     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5129        expression is being recorded directly in the function's symbol
5130        and not in a separate frame-base object.  I guess this hack is
5131        to avoid adding some sort of frame-base adjunct/annex to the
5132        function's symbol :-(.  The problem with doing this is that it
5133        results in a function symbol with a location expression that
5134        has nothing to do with the location of the function, ouch!  The
5135        relationship should be: a function's symbol has-a frame base; a
5136        frame-base has-a location expression.  */
5137     dwarf2_symbol_mark_computed (attr, new->name, cu);
5138
5139   cu->list_in_scope = &local_symbols;
5140
5141   if (die->child != NULL)
5142     {
5143       child_die = die->child;
5144       while (child_die && child_die->tag)
5145         {
5146           if (child_die->tag == DW_TAG_template_type_param
5147               || child_die->tag == DW_TAG_template_value_param)
5148             {
5149               struct symbol *arg = new_symbol (child_die, NULL, cu);
5150
5151               VEC_safe_push (symbolp, template_args, arg);
5152             }
5153           else
5154             process_die (child_die, cu);
5155           child_die = sibling_die (child_die);
5156         }
5157     }
5158
5159   inherit_abstract_dies (die, cu);
5160
5161   /* If we have a DW_AT_specification, we might need to import using
5162      directives from the context of the specification DIE.  See the
5163      comment in determine_prefix.  */
5164   if (cu->language == language_cplus
5165       && dwarf2_attr (die, DW_AT_specification, cu))
5166     {
5167       struct dwarf2_cu *spec_cu = cu;
5168       struct die_info *spec_die = die_specification (die, &spec_cu);
5169
5170       while (spec_die)
5171         {
5172           child_die = spec_die->child;
5173           while (child_die && child_die->tag)
5174             {
5175               if (child_die->tag == DW_TAG_imported_module)
5176                 process_die (child_die, spec_cu);
5177               child_die = sibling_die (child_die);
5178             }
5179
5180           /* In some cases, GCC generates specification DIEs that
5181              themselves contain DW_AT_specification attributes.  */
5182           spec_die = die_specification (spec_die, &spec_cu);
5183         }
5184     }
5185
5186   new = pop_context ();
5187   /* Make a block for the local symbols within.  */
5188   block = finish_block (new->name, &local_symbols, new->old_blocks,
5189                         lowpc, highpc, objfile);
5190
5191   /* For C++, set the block's scope.  */
5192   if (cu->language == language_cplus || cu->language == language_fortran)
5193     cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5194                         determine_prefix (die, cu),
5195                         processing_has_namespace_info);
5196
5197   /* If we have address ranges, record them.  */
5198   dwarf2_record_block_ranges (die, block, baseaddr, cu);
5199
5200   /* Attach template arguments to function.  */
5201   if (! VEC_empty (symbolp, template_args))
5202     {
5203       gdb_assert (templ_func != NULL);
5204
5205       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5206       templ_func->template_arguments
5207         = obstack_alloc (&objfile->objfile_obstack,
5208                          (templ_func->n_template_arguments
5209                           * sizeof (struct symbol *)));
5210       memcpy (templ_func->template_arguments,
5211               VEC_address (symbolp, template_args),
5212               (templ_func->n_template_arguments * sizeof (struct symbol *)));
5213       VEC_free (symbolp, template_args);
5214     }
5215
5216   /* In C++, we can have functions nested inside functions (e.g., when
5217      a function declares a class that has methods).  This means that
5218      when we finish processing a function scope, we may need to go
5219      back to building a containing block's symbol lists.  */
5220   local_symbols = new->locals;
5221   param_symbols = new->params;
5222   using_directives = new->using_directives;
5223
5224   /* If we've finished processing a top-level function, subsequent
5225      symbols go in the file symbol list.  */
5226   if (outermost_context_p ())
5227     cu->list_in_scope = &file_symbols;
5228 }
5229
5230 /* Process all the DIES contained within a lexical block scope.  Start
5231    a new scope, process the dies, and then close the scope.  */
5232
5233 static void
5234 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5235 {
5236   struct objfile *objfile = cu->objfile;
5237   struct context_stack *new;
5238   CORE_ADDR lowpc, highpc;
5239   struct die_info *child_die;
5240   CORE_ADDR baseaddr;
5241
5242   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5243
5244   /* Ignore blocks with missing or invalid low and high pc attributes.  */
5245   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5246      as multiple lexical blocks?  Handling children in a sane way would
5247      be nasty.  Might be easier to properly extend generic blocks to
5248      describe ranges.  */
5249   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5250     return;
5251   lowpc += baseaddr;
5252   highpc += baseaddr;
5253
5254   push_context (0, lowpc);
5255   if (die->child != NULL)
5256     {
5257       child_die = die->child;
5258       while (child_die && child_die->tag)
5259         {
5260           process_die (child_die, cu);
5261           child_die = sibling_die (child_die);
5262         }
5263     }
5264   new = pop_context ();
5265
5266   if (local_symbols != NULL || using_directives != NULL)
5267     {
5268       struct block *block
5269         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5270                         highpc, objfile);
5271
5272       /* Note that recording ranges after traversing children, as we
5273          do here, means that recording a parent's ranges entails
5274          walking across all its children's ranges as they appear in
5275          the address map, which is quadratic behavior.
5276
5277          It would be nicer to record the parent's ranges before
5278          traversing its children, simply overriding whatever you find
5279          there.  But since we don't even decide whether to create a
5280          block until after we've traversed its children, that's hard
5281          to do.  */
5282       dwarf2_record_block_ranges (die, block, baseaddr, cu);
5283     }
5284   local_symbols = new->locals;
5285   using_directives = new->using_directives;
5286 }
5287
5288 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5289    Return 1 if the attributes are present and valid, otherwise, return 0.
5290    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
5291
5292 static int
5293 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5294                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
5295                     struct partial_symtab *ranges_pst)
5296 {
5297   struct objfile *objfile = cu->objfile;
5298   struct comp_unit_head *cu_header = &cu->header;
5299   bfd *obfd = objfile->obfd;
5300   unsigned int addr_size = cu_header->addr_size;
5301   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5302   /* Base address selection entry.  */
5303   CORE_ADDR base;
5304   int found_base;
5305   unsigned int dummy;
5306   gdb_byte *buffer;
5307   CORE_ADDR marker;
5308   int low_set;
5309   CORE_ADDR low = 0;
5310   CORE_ADDR high = 0;
5311   CORE_ADDR baseaddr;
5312
5313   found_base = cu->base_known;
5314   base = cu->base_address;
5315
5316   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5317   if (offset >= dwarf2_per_objfile->ranges.size)
5318     {
5319       complaint (&symfile_complaints,
5320                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
5321                  offset);
5322       return 0;
5323     }
5324   buffer = dwarf2_per_objfile->ranges.buffer + offset;
5325
5326   /* Read in the largest possible address.  */
5327   marker = read_address (obfd, buffer, cu, &dummy);
5328   if ((marker & mask) == mask)
5329     {
5330       /* If we found the largest possible address, then
5331          read the base address.  */
5332       base = read_address (obfd, buffer + addr_size, cu, &dummy);
5333       buffer += 2 * addr_size;
5334       offset += 2 * addr_size;
5335       found_base = 1;
5336     }
5337
5338   low_set = 0;
5339
5340   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5341
5342   while (1)
5343     {
5344       CORE_ADDR range_beginning, range_end;
5345
5346       range_beginning = read_address (obfd, buffer, cu, &dummy);
5347       buffer += addr_size;
5348       range_end = read_address (obfd, buffer, cu, &dummy);
5349       buffer += addr_size;
5350       offset += 2 * addr_size;
5351
5352       /* An end of list marker is a pair of zero addresses.  */
5353       if (range_beginning == 0 && range_end == 0)
5354         /* Found the end of list entry.  */
5355         break;
5356
5357       /* Each base address selection entry is a pair of 2 values.
5358          The first is the largest possible address, the second is
5359          the base address.  Check for a base address here.  */
5360       if ((range_beginning & mask) == mask)
5361         {
5362           /* If we found the largest possible address, then
5363              read the base address.  */
5364           base = read_address (obfd, buffer + addr_size, cu, &dummy);
5365           found_base = 1;
5366           continue;
5367         }
5368
5369       if (!found_base)
5370         {
5371           /* We have no valid base address for the ranges
5372              data.  */
5373           complaint (&symfile_complaints,
5374                      _("Invalid .debug_ranges data (no base address)"));
5375           return 0;
5376         }
5377
5378       range_beginning += base;
5379       range_end += base;
5380
5381       if (ranges_pst != NULL && range_beginning < range_end)
5382         addrmap_set_empty (objfile->psymtabs_addrmap,
5383                            range_beginning + baseaddr, range_end - 1 + baseaddr,
5384                            ranges_pst);
5385
5386       /* FIXME: This is recording everything as a low-high
5387          segment of consecutive addresses.  We should have a
5388          data structure for discontiguous block ranges
5389          instead.  */
5390       if (! low_set)
5391         {
5392           low = range_beginning;
5393           high = range_end;
5394           low_set = 1;
5395         }
5396       else
5397         {
5398           if (range_beginning < low)
5399             low = range_beginning;
5400           if (range_end > high)
5401             high = range_end;
5402         }
5403     }
5404
5405   if (! low_set)
5406     /* If the first entry is an end-of-list marker, the range
5407        describes an empty scope, i.e. no instructions.  */
5408     return 0;
5409
5410   if (low_return)
5411     *low_return = low;
5412   if (high_return)
5413     *high_return = high;
5414   return 1;
5415 }
5416
5417 /* Get low and high pc attributes from a die.  Return 1 if the attributes
5418    are present and valid, otherwise, return 0.  Return -1 if the range is
5419    discontinuous, i.e. derived from DW_AT_ranges information.  */
5420 static int
5421 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5422                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
5423                       struct partial_symtab *pst)
5424 {
5425   struct attribute *attr;
5426   CORE_ADDR low = 0;
5427   CORE_ADDR high = 0;
5428   int ret = 0;
5429
5430   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5431   if (attr)
5432     {
5433       high = DW_ADDR (attr);
5434       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5435       if (attr)
5436         low = DW_ADDR (attr);
5437       else
5438         /* Found high w/o low attribute.  */
5439         return 0;
5440
5441       /* Found consecutive range of addresses.  */
5442       ret = 1;
5443     }
5444   else
5445     {
5446       attr = dwarf2_attr (die, DW_AT_ranges, cu);
5447       if (attr != NULL)
5448         {
5449           /* Value of the DW_AT_ranges attribute is the offset in the
5450              .debug_ranges section.  */
5451           if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5452             return 0;
5453           /* Found discontinuous range of addresses.  */
5454           ret = -1;
5455         }
5456     }
5457
5458   if (high < low)
5459     return 0;
5460
5461   /* When using the GNU linker, .gnu.linkonce. sections are used to
5462      eliminate duplicate copies of functions and vtables and such.
5463      The linker will arbitrarily choose one and discard the others.
5464      The AT_*_pc values for such functions refer to local labels in
5465      these sections.  If the section from that file was discarded, the
5466      labels are not in the output, so the relocs get a value of 0.
5467      If this is a discarded function, mark the pc bounds as invalid,
5468      so that GDB will ignore it.  */
5469   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5470     return 0;
5471
5472   *lowpc = low;
5473   *highpc = high;
5474   return ret;
5475 }
5476
5477 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5478    its low and high PC addresses.  Do nothing if these addresses could not
5479    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
5480    and HIGHPC to the high address if greater than HIGHPC.  */
5481
5482 static void
5483 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5484                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
5485                                  struct dwarf2_cu *cu)
5486 {
5487   CORE_ADDR low, high;
5488   struct die_info *child = die->child;
5489
5490   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5491     {
5492       *lowpc = min (*lowpc, low);
5493       *highpc = max (*highpc, high);
5494     }
5495
5496   /* If the language does not allow nested subprograms (either inside
5497      subprograms or lexical blocks), we're done.  */
5498   if (cu->language != language_ada)
5499     return;
5500
5501   /* Check all the children of the given DIE.  If it contains nested
5502      subprograms, then check their pc bounds.  Likewise, we need to
5503      check lexical blocks as well, as they may also contain subprogram
5504      definitions.  */
5505   while (child && child->tag)
5506     {
5507       if (child->tag == DW_TAG_subprogram
5508           || child->tag == DW_TAG_lexical_block)
5509         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5510       child = sibling_die (child);
5511     }
5512 }
5513
5514 /* Get the low and high pc's represented by the scope DIE, and store
5515    them in *LOWPC and *HIGHPC.  If the correct values can't be
5516    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
5517
5518 static void
5519 get_scope_pc_bounds (struct die_info *die,
5520                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
5521                      struct dwarf2_cu *cu)
5522 {
5523   CORE_ADDR best_low = (CORE_ADDR) -1;
5524   CORE_ADDR best_high = (CORE_ADDR) 0;
5525   CORE_ADDR current_low, current_high;
5526
5527   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5528     {
5529       best_low = current_low;
5530       best_high = current_high;
5531     }
5532   else
5533     {
5534       struct die_info *child = die->child;
5535
5536       while (child && child->tag)
5537         {
5538           switch (child->tag) {
5539           case DW_TAG_subprogram:
5540             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5541             break;
5542           case DW_TAG_namespace:
5543           case DW_TAG_module:
5544             /* FIXME: carlton/2004-01-16: Should we do this for
5545                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
5546                that current GCC's always emit the DIEs corresponding
5547                to definitions of methods of classes as children of a
5548                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5549                the DIEs giving the declarations, which could be
5550                anywhere).  But I don't see any reason why the
5551                standards says that they have to be there.  */
5552             get_scope_pc_bounds (child, &current_low, &current_high, cu);
5553
5554             if (current_low != ((CORE_ADDR) -1))
5555               {
5556                 best_low = min (best_low, current_low);
5557                 best_high = max (best_high, current_high);
5558               }
5559             break;
5560           default:
5561             /* Ignore. */
5562             break;
5563           }
5564
5565           child = sibling_die (child);
5566         }
5567     }
5568
5569   *lowpc = best_low;
5570   *highpc = best_high;
5571 }
5572
5573 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5574    in DIE.  */
5575 static void
5576 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5577                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5578 {
5579   struct attribute *attr;
5580
5581   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5582   if (attr)
5583     {
5584       CORE_ADDR high = DW_ADDR (attr);
5585
5586       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5587       if (attr)
5588         {
5589           CORE_ADDR low = DW_ADDR (attr);
5590
5591           record_block_range (block, baseaddr + low, baseaddr + high - 1);
5592         }
5593     }
5594
5595   attr = dwarf2_attr (die, DW_AT_ranges, cu);
5596   if (attr)
5597     {
5598       bfd *obfd = cu->objfile->obfd;
5599
5600       /* The value of the DW_AT_ranges attribute is the offset of the
5601          address range list in the .debug_ranges section.  */
5602       unsigned long offset = DW_UNSND (attr);
5603       gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5604
5605       /* For some target architectures, but not others, the
5606          read_address function sign-extends the addresses it returns.
5607          To recognize base address selection entries, we need a
5608          mask.  */
5609       unsigned int addr_size = cu->header.addr_size;
5610       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5611
5612       /* The base address, to which the next pair is relative.  Note
5613          that this 'base' is a DWARF concept: most entries in a range
5614          list are relative, to reduce the number of relocs against the
5615          debugging information.  This is separate from this function's
5616          'baseaddr' argument, which GDB uses to relocate debugging
5617          information from a shared library based on the address at
5618          which the library was loaded.  */
5619       CORE_ADDR base = cu->base_address;
5620       int base_known = cu->base_known;
5621
5622       gdb_assert (dwarf2_per_objfile->ranges.readin);
5623       if (offset >= dwarf2_per_objfile->ranges.size)
5624         {
5625           complaint (&symfile_complaints,
5626                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5627                      offset);
5628           return;
5629         }
5630
5631       for (;;)
5632         {
5633           unsigned int bytes_read;
5634           CORE_ADDR start, end;
5635
5636           start = read_address (obfd, buffer, cu, &bytes_read);
5637           buffer += bytes_read;
5638           end = read_address (obfd, buffer, cu, &bytes_read);
5639           buffer += bytes_read;
5640
5641           /* Did we find the end of the range list?  */
5642           if (start == 0 && end == 0)
5643             break;
5644
5645           /* Did we find a base address selection entry?  */
5646           else if ((start & base_select_mask) == base_select_mask)
5647             {
5648               base = end;
5649               base_known = 1;
5650             }
5651
5652           /* We found an ordinary address range.  */
5653           else
5654             {
5655               if (!base_known)
5656                 {
5657                   complaint (&symfile_complaints,
5658                              _("Invalid .debug_ranges data (no base address)"));
5659                   return;
5660                 }
5661
5662               record_block_range (block,
5663                                   baseaddr + base + start,
5664                                   baseaddr + base + end - 1);
5665             }
5666         }
5667     }
5668 }
5669
5670 /* Add an aggregate field to the field list.  */
5671
5672 static void
5673 dwarf2_add_field (struct field_info *fip, struct die_info *die,
5674                   struct dwarf2_cu *cu)
5675 {
5676   struct objfile *objfile = cu->objfile;
5677   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5678   struct nextfield *new_field;
5679   struct attribute *attr;
5680   struct field *fp;
5681   char *fieldname = "";
5682
5683   /* Allocate a new field list entry and link it in.  */
5684   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
5685   make_cleanup (xfree, new_field);
5686   memset (new_field, 0, sizeof (struct nextfield));
5687
5688   if (die->tag == DW_TAG_inheritance)
5689     {
5690       new_field->next = fip->baseclasses;
5691       fip->baseclasses = new_field;
5692     }
5693   else
5694     {
5695       new_field->next = fip->fields;
5696       fip->fields = new_field;
5697     }
5698   fip->nfields++;
5699
5700   /* Handle accessibility and virtuality of field.
5701      The default accessibility for members is public, the default
5702      accessibility for inheritance is private.  */
5703   if (die->tag != DW_TAG_inheritance)
5704     new_field->accessibility = DW_ACCESS_public;
5705   else
5706     new_field->accessibility = DW_ACCESS_private;
5707   new_field->virtuality = DW_VIRTUALITY_none;
5708
5709   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
5710   if (attr)
5711     new_field->accessibility = DW_UNSND (attr);
5712   if (new_field->accessibility != DW_ACCESS_public)
5713     fip->non_public_fields = 1;
5714   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
5715   if (attr)
5716     new_field->virtuality = DW_UNSND (attr);
5717
5718   fp = &new_field->field;
5719
5720   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
5721     {
5722       /* Data member other than a C++ static data member.  */
5723
5724       /* Get type of field.  */
5725       fp->type = die_type (die, cu);
5726
5727       SET_FIELD_BITPOS (*fp, 0);
5728
5729       /* Get bit size of field (zero if none).  */
5730       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
5731       if (attr)
5732         {
5733           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
5734         }
5735       else
5736         {
5737           FIELD_BITSIZE (*fp) = 0;
5738         }
5739
5740       /* Get bit offset of field.  */
5741       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5742       if (attr)
5743         {
5744           int byte_offset = 0;
5745
5746           if (attr_form_is_section_offset (attr))
5747             dwarf2_complex_location_expr_complaint ();
5748           else if (attr_form_is_constant (attr))
5749             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5750           else if (attr_form_is_block (attr))
5751             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5752           else
5753             dwarf2_complex_location_expr_complaint ();
5754
5755           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5756         }
5757       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
5758       if (attr)
5759         {
5760           if (gdbarch_bits_big_endian (gdbarch))
5761             {
5762               /* For big endian bits, the DW_AT_bit_offset gives the
5763                  additional bit offset from the MSB of the containing
5764                  anonymous object to the MSB of the field.  We don't
5765                  have to do anything special since we don't need to
5766                  know the size of the anonymous object.  */
5767               FIELD_BITPOS (*fp) += DW_UNSND (attr);
5768             }
5769           else
5770             {
5771               /* For little endian bits, compute the bit offset to the
5772                  MSB of the anonymous object, subtract off the number of
5773                  bits from the MSB of the field to the MSB of the
5774                  object, and then subtract off the number of bits of
5775                  the field itself.  The result is the bit offset of
5776                  the LSB of the field.  */
5777               int anonymous_size;
5778               int bit_offset = DW_UNSND (attr);
5779
5780               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
5781               if (attr)
5782                 {
5783                   /* The size of the anonymous object containing
5784                      the bit field is explicit, so use the
5785                      indicated size (in bytes).  */
5786                   anonymous_size = DW_UNSND (attr);
5787                 }
5788               else
5789                 {
5790                   /* The size of the anonymous object containing
5791                      the bit field must be inferred from the type
5792                      attribute of the data member containing the
5793                      bit field.  */
5794                   anonymous_size = TYPE_LENGTH (fp->type);
5795                 }
5796               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
5797                 - bit_offset - FIELD_BITSIZE (*fp);
5798             }
5799         }
5800
5801       /* Get name of field.  */
5802       fieldname = dwarf2_name (die, cu);
5803       if (fieldname == NULL)
5804         fieldname = "";
5805
5806       /* The name is already allocated along with this objfile, so we don't
5807          need to duplicate it for the type.  */
5808       fp->name = fieldname;
5809
5810       /* Change accessibility for artificial fields (e.g. virtual table
5811          pointer or virtual base class pointer) to private.  */
5812       if (dwarf2_attr (die, DW_AT_artificial, cu))
5813         {
5814           FIELD_ARTIFICIAL (*fp) = 1;
5815           new_field->accessibility = DW_ACCESS_private;
5816           fip->non_public_fields = 1;
5817         }
5818     }
5819   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
5820     {
5821       /* C++ static member.  */
5822
5823       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
5824          is a declaration, but all versions of G++ as of this writing
5825          (so through at least 3.2.1) incorrectly generate
5826          DW_TAG_variable tags.  */
5827
5828       char *physname;
5829
5830       /* Get name of field.  */
5831       fieldname = dwarf2_name (die, cu);
5832       if (fieldname == NULL)
5833         return;
5834
5835       attr = dwarf2_attr (die, DW_AT_const_value, cu);
5836       if (attr
5837           /* Only create a symbol if this is an external value.
5838              new_symbol checks this and puts the value in the global symbol
5839              table, which we want.  If it is not external, new_symbol
5840              will try to put the value in cu->list_in_scope which is wrong.  */
5841           && dwarf2_flag_true_p (die, DW_AT_external, cu))
5842         {
5843           /* A static const member, not much different than an enum as far as
5844              we're concerned, except that we can support more types.  */
5845           new_symbol (die, NULL, cu);
5846         }
5847
5848       /* Get physical name.  */
5849       physname = (char *) dwarf2_physname (fieldname, die, cu);
5850
5851       /* The name is already allocated along with this objfile, so we don't
5852          need to duplicate it for the type.  */
5853       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5854       FIELD_TYPE (*fp) = die_type (die, cu);
5855       FIELD_NAME (*fp) = fieldname;
5856     }
5857   else if (die->tag == DW_TAG_inheritance)
5858     {
5859       /* C++ base class field.  */
5860       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
5861       if (attr)
5862         {
5863           int byte_offset = 0;
5864
5865           if (attr_form_is_section_offset (attr))
5866             dwarf2_complex_location_expr_complaint ();
5867           else if (attr_form_is_constant (attr))
5868             byte_offset = dwarf2_get_attr_constant_value (attr, 0);
5869           else if (attr_form_is_block (attr))
5870             byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
5871           else
5872             dwarf2_complex_location_expr_complaint ();
5873
5874           SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
5875         }
5876       FIELD_BITSIZE (*fp) = 0;
5877       FIELD_TYPE (*fp) = die_type (die, cu);
5878       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
5879       fip->nbaseclasses++;
5880     }
5881 }
5882
5883 /* Add a typedef defined in the scope of the FIP's class.  */
5884
5885 static void
5886 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
5887                     struct dwarf2_cu *cu)
5888 {
5889   struct objfile *objfile = cu->objfile;
5890   struct gdbarch *gdbarch = get_objfile_arch (objfile);
5891   struct typedef_field_list *new_field;
5892   struct attribute *attr;
5893   struct typedef_field *fp;
5894   char *fieldname = "";
5895
5896   /* Allocate a new field list entry and link it in.  */
5897   new_field = xzalloc (sizeof (*new_field));
5898   make_cleanup (xfree, new_field);
5899
5900   gdb_assert (die->tag == DW_TAG_typedef);
5901
5902   fp = &new_field->field;
5903
5904   /* Get name of field.  */
5905   fp->name = dwarf2_name (die, cu);
5906   if (fp->name == NULL)
5907     return;
5908
5909   fp->type = read_type_die (die, cu);
5910
5911   new_field->next = fip->typedef_field_list;
5912   fip->typedef_field_list = new_field;
5913   fip->typedef_field_list_count++;
5914 }
5915
5916 /* Create the vector of fields, and attach it to the type.  */
5917
5918 static void
5919 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
5920                               struct dwarf2_cu *cu)
5921 {
5922   int nfields = fip->nfields;
5923
5924   /* Record the field count, allocate space for the array of fields,
5925      and create blank accessibility bitfields if necessary.  */
5926   TYPE_NFIELDS (type) = nfields;
5927   TYPE_FIELDS (type) = (struct field *)
5928     TYPE_ALLOC (type, sizeof (struct field) * nfields);
5929   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
5930
5931   if (fip->non_public_fields && cu->language != language_ada)
5932     {
5933       ALLOCATE_CPLUS_STRUCT_TYPE (type);
5934
5935       TYPE_FIELD_PRIVATE_BITS (type) =
5936         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5937       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
5938
5939       TYPE_FIELD_PROTECTED_BITS (type) =
5940         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5941       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
5942
5943       TYPE_FIELD_IGNORE_BITS (type) =
5944         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
5945       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
5946     }
5947
5948   /* If the type has baseclasses, allocate and clear a bit vector for
5949      TYPE_FIELD_VIRTUAL_BITS.  */
5950   if (fip->nbaseclasses && cu->language != language_ada)
5951     {
5952       int num_bytes = B_BYTES (fip->nbaseclasses);
5953       unsigned char *pointer;
5954
5955       ALLOCATE_CPLUS_STRUCT_TYPE (type);
5956       pointer = TYPE_ALLOC (type, num_bytes);
5957       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
5958       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
5959       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
5960     }
5961
5962   /* Copy the saved-up fields into the field vector.  Start from the head
5963      of the list, adding to the tail of the field array, so that they end
5964      up in the same order in the array in which they were added to the list.  */
5965   while (nfields-- > 0)
5966     {
5967       struct nextfield *fieldp;
5968
5969       if (fip->fields)
5970         {
5971           fieldp = fip->fields;
5972           fip->fields = fieldp->next;
5973         }
5974       else
5975         {
5976           fieldp = fip->baseclasses;
5977           fip->baseclasses = fieldp->next;
5978         }
5979
5980       TYPE_FIELD (type, nfields) = fieldp->field;
5981       switch (fieldp->accessibility)
5982         {
5983         case DW_ACCESS_private:
5984           if (cu->language != language_ada)
5985             SET_TYPE_FIELD_PRIVATE (type, nfields);
5986           break;
5987
5988         case DW_ACCESS_protected:
5989           if (cu->language != language_ada)
5990             SET_TYPE_FIELD_PROTECTED (type, nfields);
5991           break;
5992
5993         case DW_ACCESS_public:
5994           break;
5995
5996         default:
5997           /* Unknown accessibility.  Complain and treat it as public.  */
5998           {
5999             complaint (&symfile_complaints, _("unsupported accessibility %d"),
6000                        fieldp->accessibility);
6001           }
6002           break;
6003         }
6004       if (nfields < fip->nbaseclasses)
6005         {
6006           switch (fieldp->virtuality)
6007             {
6008             case DW_VIRTUALITY_virtual:
6009             case DW_VIRTUALITY_pure_virtual:
6010               if (cu->language == language_ada)
6011                 error ("unexpected virtuality in component of Ada type");
6012               SET_TYPE_FIELD_VIRTUAL (type, nfields);
6013               break;
6014             }
6015         }
6016     }
6017 }
6018
6019 /* Add a member function to the proper fieldlist.  */
6020
6021 static void
6022 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6023                       struct type *type, struct dwarf2_cu *cu)
6024 {
6025   struct objfile *objfile = cu->objfile;
6026   struct attribute *attr;
6027   struct fnfieldlist *flp;
6028   int i;
6029   struct fn_field *fnp;
6030   char *fieldname;
6031   char *physname;
6032   struct nextfnfield *new_fnfield;
6033   struct type *this_type;
6034
6035   if (cu->language == language_ada)
6036     error ("unexpected member function in Ada type");
6037
6038   /* Get name of member function.  */
6039   fieldname = dwarf2_name (die, cu);
6040   if (fieldname == NULL)
6041     return;
6042
6043   /* Get the mangled name.  */
6044   physname = (char *) dwarf2_physname (fieldname, die, cu);
6045
6046   /* Look up member function name in fieldlist.  */
6047   for (i = 0; i < fip->nfnfields; i++)
6048     {
6049       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6050         break;
6051     }
6052
6053   /* Create new list element if necessary.  */
6054   if (i < fip->nfnfields)
6055     flp = &fip->fnfieldlists[i];
6056   else
6057     {
6058       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6059         {
6060           fip->fnfieldlists = (struct fnfieldlist *)
6061             xrealloc (fip->fnfieldlists,
6062                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6063                       * sizeof (struct fnfieldlist));
6064           if (fip->nfnfields == 0)
6065             make_cleanup (free_current_contents, &fip->fnfieldlists);
6066         }
6067       flp = &fip->fnfieldlists[fip->nfnfields];
6068       flp->name = fieldname;
6069       flp->length = 0;
6070       flp->head = NULL;
6071       fip->nfnfields++;
6072     }
6073
6074   /* Create a new member function field and chain it to the field list
6075      entry. */
6076   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6077   make_cleanup (xfree, new_fnfield);
6078   memset (new_fnfield, 0, sizeof (struct nextfnfield));
6079   new_fnfield->next = flp->head;
6080   flp->head = new_fnfield;
6081   flp->length++;
6082
6083   /* Fill in the member function field info.  */
6084   fnp = &new_fnfield->fnfield;
6085   /* The name is already allocated along with this objfile, so we don't
6086      need to duplicate it for the type.  */
6087   fnp->physname = physname ? physname : "";
6088   fnp->type = alloc_type (objfile);
6089   this_type = read_type_die (die, cu);
6090   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6091     {
6092       int nparams = TYPE_NFIELDS (this_type);
6093
6094       /* TYPE is the domain of this method, and THIS_TYPE is the type
6095            of the method itself (TYPE_CODE_METHOD).  */
6096       smash_to_method_type (fnp->type, type,
6097                             TYPE_TARGET_TYPE (this_type),
6098                             TYPE_FIELDS (this_type),
6099                             TYPE_NFIELDS (this_type),
6100                             TYPE_VARARGS (this_type));
6101
6102       /* Handle static member functions.
6103          Dwarf2 has no clean way to discern C++ static and non-static
6104          member functions. G++ helps GDB by marking the first
6105          parameter for non-static member functions (which is the
6106          this pointer) as artificial. We obtain this information
6107          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
6108       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6109         fnp->voffset = VOFFSET_STATIC;
6110     }
6111   else
6112     complaint (&symfile_complaints, _("member function type missing for '%s'"),
6113                physname);
6114
6115   /* Get fcontext from DW_AT_containing_type if present.  */
6116   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6117     fnp->fcontext = die_containing_type (die, cu);
6118
6119   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6120      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
6121
6122   /* Get accessibility.  */
6123   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6124   if (attr)
6125     {
6126       switch (DW_UNSND (attr))
6127         {
6128         case DW_ACCESS_private:
6129           fnp->is_private = 1;
6130           break;
6131         case DW_ACCESS_protected:
6132           fnp->is_protected = 1;
6133           break;
6134         }
6135     }
6136
6137   /* Check for artificial methods.  */
6138   attr = dwarf2_attr (die, DW_AT_artificial, cu);
6139   if (attr && DW_UNSND (attr) != 0)
6140     fnp->is_artificial = 1;
6141
6142   /* Get index in virtual function table if it is a virtual member
6143      function.  For older versions of GCC, this is an offset in the
6144      appropriate virtual table, as specified by DW_AT_containing_type.
6145      For everyone else, it is an expression to be evaluated relative
6146      to the object address.  */
6147
6148   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6149   if (attr)
6150     {
6151       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6152         {
6153           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6154             {
6155               /* Old-style GCC.  */
6156               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6157             }
6158           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6159                    || (DW_BLOCK (attr)->size > 1
6160                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6161                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6162             {
6163               struct dwarf_block blk;
6164               int offset;
6165
6166               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6167                         ? 1 : 2);
6168               blk.size = DW_BLOCK (attr)->size - offset;
6169               blk.data = DW_BLOCK (attr)->data + offset;
6170               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6171               if ((fnp->voffset % cu->header.addr_size) != 0)
6172                 dwarf2_complex_location_expr_complaint ();
6173               else
6174                 fnp->voffset /= cu->header.addr_size;
6175               fnp->voffset += 2;
6176             }
6177           else
6178             dwarf2_complex_location_expr_complaint ();
6179
6180           if (!fnp->fcontext)
6181             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6182         }
6183       else if (attr_form_is_section_offset (attr))
6184         {
6185           dwarf2_complex_location_expr_complaint ();
6186         }
6187       else
6188         {
6189           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6190                                                  fieldname);
6191         }
6192     }
6193   else
6194     {
6195       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6196       if (attr && DW_UNSND (attr))
6197         {
6198           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
6199           complaint (&symfile_complaints,
6200                      _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6201                      fieldname, die->offset);
6202           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6203           TYPE_CPLUS_DYNAMIC (type) = 1;
6204         }
6205     }
6206 }
6207
6208 /* Create the vector of member function fields, and attach it to the type.  */
6209
6210 static void
6211 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6212                                  struct dwarf2_cu *cu)
6213 {
6214   struct fnfieldlist *flp;
6215   int total_length = 0;
6216   int i;
6217
6218   if (cu->language == language_ada)
6219     error ("unexpected member functions in Ada type");
6220
6221   ALLOCATE_CPLUS_STRUCT_TYPE (type);
6222   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6223     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6224
6225   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6226     {
6227       struct nextfnfield *nfp = flp->head;
6228       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6229       int k;
6230
6231       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6232       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6233       fn_flp->fn_fields = (struct fn_field *)
6234         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6235       for (k = flp->length; (k--, nfp); nfp = nfp->next)
6236         fn_flp->fn_fields[k] = nfp->fnfield;
6237
6238       total_length += flp->length;
6239     }
6240
6241   TYPE_NFN_FIELDS (type) = fip->nfnfields;
6242   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6243 }
6244
6245 /* Returns non-zero if NAME is the name of a vtable member in CU's
6246    language, zero otherwise.  */
6247 static int
6248 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6249 {
6250   static const char vptr[] = "_vptr";
6251   static const char vtable[] = "vtable";
6252
6253   /* Look for the C++ and Java forms of the vtable.  */
6254   if ((cu->language == language_java
6255        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6256        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6257        && is_cplus_marker (name[sizeof (vptr) - 1])))
6258     return 1;
6259
6260   return 0;
6261 }
6262
6263 /* GCC outputs unnamed structures that are really pointers to member
6264    functions, with the ABI-specified layout.  If TYPE describes
6265    such a structure, smash it into a member function type.
6266
6267    GCC shouldn't do this; it should just output pointer to member DIEs.
6268    This is GCC PR debug/28767.  */
6269
6270 static void
6271 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6272 {
6273   struct type *pfn_type, *domain_type, *new_type;
6274
6275   /* Check for a structure with no name and two children.  */
6276   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6277     return;
6278
6279   /* Check for __pfn and __delta members.  */
6280   if (TYPE_FIELD_NAME (type, 0) == NULL
6281       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6282       || TYPE_FIELD_NAME (type, 1) == NULL
6283       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6284     return;
6285
6286   /* Find the type of the method.  */
6287   pfn_type = TYPE_FIELD_TYPE (type, 0);
6288   if (pfn_type == NULL
6289       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6290       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6291     return;
6292
6293   /* Look for the "this" argument.  */
6294   pfn_type = TYPE_TARGET_TYPE (pfn_type);
6295   if (TYPE_NFIELDS (pfn_type) == 0
6296       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6297       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6298     return;
6299
6300   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6301   new_type = alloc_type (objfile);
6302   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6303                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6304                         TYPE_VARARGS (pfn_type));
6305   smash_to_methodptr_type (type, new_type);
6306 }
6307
6308 /* Called when we find the DIE that starts a structure or union scope
6309    (definition) to process all dies that define the members of the
6310    structure or union.
6311
6312    NOTE: we need to call struct_type regardless of whether or not the
6313    DIE has an at_name attribute, since it might be an anonymous
6314    structure or union.  This gets the type entered into our set of
6315    user defined types.
6316
6317    However, if the structure is incomplete (an opaque struct/union)
6318    then suppress creating a symbol table entry for it since gdb only
6319    wants to find the one with the complete definition.  Note that if
6320    it is complete, we just call new_symbol, which does it's own
6321    checking about whether the struct/union is anonymous or not (and
6322    suppresses creating a symbol table entry itself).  */
6323
6324 static struct type *
6325 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6326 {
6327   struct objfile *objfile = cu->objfile;
6328   struct type *type;
6329   struct attribute *attr;
6330   char *name;
6331   struct cleanup *back_to;
6332
6333   /* If the definition of this type lives in .debug_types, read that type.
6334      Don't follow DW_AT_specification though, that will take us back up
6335      the chain and we want to go down.  */
6336   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6337   if (attr)
6338     {
6339       struct dwarf2_cu *type_cu = cu;
6340       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6341
6342       /* We could just recurse on read_structure_type, but we need to call
6343          get_die_type to ensure only one type for this DIE is created.
6344          This is important, for example, because for c++ classes we need
6345          TYPE_NAME set which is only done by new_symbol.  Blech.  */
6346       type = read_type_die (type_die, type_cu);
6347       return set_die_type (die, type, cu);
6348     }
6349
6350   back_to = make_cleanup (null_cleanup, 0);
6351
6352   type = alloc_type (objfile);
6353   INIT_CPLUS_SPECIFIC (type);
6354
6355   name = dwarf2_name (die, cu);
6356   if (name != NULL)
6357     {
6358       if (cu->language == language_cplus
6359           || cu->language == language_java)
6360         {
6361           TYPE_TAG_NAME (type) = (char *) dwarf2_full_name (name, die, cu);
6362           if (die->tag == DW_TAG_structure_type
6363               || die->tag == DW_TAG_class_type)
6364             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6365         }
6366       else
6367         {
6368           /* The name is already allocated along with this objfile, so
6369              we don't need to duplicate it for the type.  */
6370           TYPE_TAG_NAME (type) = (char *) name;
6371           if (die->tag == DW_TAG_class_type)
6372             TYPE_NAME (type) = TYPE_TAG_NAME (type);
6373         }
6374     }
6375
6376   if (die->tag == DW_TAG_structure_type)
6377     {
6378       TYPE_CODE (type) = TYPE_CODE_STRUCT;
6379     }
6380   else if (die->tag == DW_TAG_union_type)
6381     {
6382       TYPE_CODE (type) = TYPE_CODE_UNION;
6383     }
6384   else
6385     {
6386       TYPE_CODE (type) = TYPE_CODE_CLASS;
6387     }
6388
6389   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6390     TYPE_DECLARED_CLASS (type) = 1;
6391
6392   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6393   if (attr)
6394     {
6395       TYPE_LENGTH (type) = DW_UNSND (attr);
6396     }
6397   else
6398     {
6399       TYPE_LENGTH (type) = 0;
6400     }
6401
6402   TYPE_STUB_SUPPORTED (type) = 1;
6403   if (die_is_declaration (die, cu))
6404     TYPE_STUB (type) = 1;
6405   else if (attr == NULL && die->child == NULL
6406            && producer_is_realview (cu->producer))
6407     /* RealView does not output the required DW_AT_declaration
6408        on incomplete types.  */
6409     TYPE_STUB (type) = 1;
6410
6411   /* We need to add the type field to the die immediately so we don't
6412      infinitely recurse when dealing with pointers to the structure
6413      type within the structure itself. */
6414   set_die_type (die, type, cu);
6415
6416   /* set_die_type should be already done.  */
6417   set_descriptive_type (type, die, cu);
6418
6419   if (die->child != NULL && ! die_is_declaration (die, cu))
6420     {
6421       struct field_info fi;
6422       struct die_info *child_die;
6423       VEC (symbolp) *template_args = NULL;
6424
6425       memset (&fi, 0, sizeof (struct field_info));
6426
6427       child_die = die->child;
6428
6429       while (child_die && child_die->tag)
6430         {
6431           if (child_die->tag == DW_TAG_member
6432               || child_die->tag == DW_TAG_variable)
6433             {
6434               /* NOTE: carlton/2002-11-05: A C++ static data member
6435                  should be a DW_TAG_member that is a declaration, but
6436                  all versions of G++ as of this writing (so through at
6437                  least 3.2.1) incorrectly generate DW_TAG_variable
6438                  tags for them instead.  */
6439               dwarf2_add_field (&fi, child_die, cu);
6440             }
6441           else if (child_die->tag == DW_TAG_subprogram)
6442             {
6443               /* C++ member function. */
6444               dwarf2_add_member_fn (&fi, child_die, type, cu);
6445             }
6446           else if (child_die->tag == DW_TAG_inheritance)
6447             {
6448               /* C++ base class field.  */
6449               dwarf2_add_field (&fi, child_die, cu);
6450             }
6451           else if (child_die->tag == DW_TAG_typedef)
6452             dwarf2_add_typedef (&fi, child_die, cu);
6453           else if (child_die->tag == DW_TAG_template_type_param
6454                    || child_die->tag == DW_TAG_template_value_param)
6455             {
6456               struct symbol *arg = new_symbol (child_die, NULL, cu);
6457
6458               VEC_safe_push (symbolp, template_args, arg);
6459             }
6460
6461           child_die = sibling_die (child_die);
6462         }
6463
6464       /* Attach template arguments to type.  */
6465       if (! VEC_empty (symbolp, template_args))
6466         {
6467           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6468           TYPE_N_TEMPLATE_ARGUMENTS (type)
6469             = VEC_length (symbolp, template_args);
6470           TYPE_TEMPLATE_ARGUMENTS (type)
6471             = obstack_alloc (&objfile->objfile_obstack,
6472                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
6473                               * sizeof (struct symbol *)));
6474           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6475                   VEC_address (symbolp, template_args),
6476                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
6477                    * sizeof (struct symbol *)));
6478           VEC_free (symbolp, template_args);
6479         }
6480
6481       /* Attach fields and member functions to the type.  */
6482       if (fi.nfields)
6483         dwarf2_attach_fields_to_type (&fi, type, cu);
6484       if (fi.nfnfields)
6485         {
6486           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6487
6488           /* Get the type which refers to the base class (possibly this
6489              class itself) which contains the vtable pointer for the current
6490              class from the DW_AT_containing_type attribute.  This use of
6491              DW_AT_containing_type is a GNU extension.  */
6492
6493           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6494             {
6495               struct type *t = die_containing_type (die, cu);
6496
6497               TYPE_VPTR_BASETYPE (type) = t;
6498               if (type == t)
6499                 {
6500                   int i;
6501
6502                   /* Our own class provides vtbl ptr.  */
6503                   for (i = TYPE_NFIELDS (t) - 1;
6504                        i >= TYPE_N_BASECLASSES (t);
6505                        --i)
6506                     {
6507                       char *fieldname = TYPE_FIELD_NAME (t, i);
6508
6509                       if (is_vtable_name (fieldname, cu))
6510                         {
6511                           TYPE_VPTR_FIELDNO (type) = i;
6512                           break;
6513                         }
6514                     }
6515
6516                   /* Complain if virtual function table field not found.  */
6517                   if (i < TYPE_N_BASECLASSES (t))
6518                     complaint (&symfile_complaints,
6519                                _("virtual function table pointer not found when defining class '%s'"),
6520                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6521                                "");
6522                 }
6523               else
6524                 {
6525                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6526                 }
6527             }
6528           else if (cu->producer
6529                    && strncmp (cu->producer,
6530                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6531             {
6532               /* The IBM XLC compiler does not provide direct indication
6533                  of the containing type, but the vtable pointer is
6534                  always named __vfp.  */
6535
6536               int i;
6537
6538               for (i = TYPE_NFIELDS (type) - 1;
6539                    i >= TYPE_N_BASECLASSES (type);
6540                    --i)
6541                 {
6542                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6543                     {
6544                       TYPE_VPTR_FIELDNO (type) = i;
6545                       TYPE_VPTR_BASETYPE (type) = type;
6546                       break;
6547                     }
6548                 }
6549             }
6550         }
6551
6552       /* Copy fi.typedef_field_list linked list elements content into the
6553          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
6554       if (fi.typedef_field_list)
6555         {
6556           int i = fi.typedef_field_list_count;
6557
6558           ALLOCATE_CPLUS_STRUCT_TYPE (type);
6559           TYPE_TYPEDEF_FIELD_ARRAY (type)
6560             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6561           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6562
6563           /* Reverse the list order to keep the debug info elements order.  */
6564           while (--i >= 0)
6565             {
6566               struct typedef_field *dest, *src;
6567
6568               dest = &TYPE_TYPEDEF_FIELD (type, i);
6569               src = &fi.typedef_field_list->field;
6570               fi.typedef_field_list = fi.typedef_field_list->next;
6571               *dest = *src;
6572             }
6573         }
6574     }
6575
6576   quirk_gcc_member_function_pointer (type, cu->objfile);
6577
6578   do_cleanups (back_to);
6579   return type;
6580 }
6581
6582 static void
6583 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6584 {
6585   struct die_info *child_die = die->child;
6586   struct type *this_type;
6587
6588   this_type = get_die_type (die, cu);
6589   if (this_type == NULL)
6590     this_type = read_structure_type (die, cu);
6591
6592   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6593      snapshots) has been known to create a die giving a declaration
6594      for a class that has, as a child, a die giving a definition for a
6595      nested class.  So we have to process our children even if the
6596      current die is a declaration.  Normally, of course, a declaration
6597      won't have any children at all.  */
6598
6599   while (child_die != NULL && child_die->tag)
6600     {
6601       if (child_die->tag == DW_TAG_member
6602           || child_die->tag == DW_TAG_variable
6603           || child_die->tag == DW_TAG_inheritance
6604           || child_die->tag == DW_TAG_template_value_param
6605           || child_die->tag == DW_TAG_template_type_param)
6606         {
6607           /* Do nothing.  */
6608         }
6609       else
6610         process_die (child_die, cu);
6611
6612       child_die = sibling_die (child_die);
6613     }
6614
6615   /* Do not consider external references.  According to the DWARF standard,
6616      these DIEs are identified by the fact that they have no byte_size
6617      attribute, and a declaration attribute.  */
6618   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6619       || !die_is_declaration (die, cu))
6620     new_symbol (die, this_type, cu);
6621 }
6622
6623 /* Given a DW_AT_enumeration_type die, set its type.  We do not
6624    complete the type's fields yet, or create any symbols.  */
6625
6626 static struct type *
6627 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6628 {
6629   struct objfile *objfile = cu->objfile;
6630   struct type *type;
6631   struct attribute *attr;
6632   const char *name;
6633
6634   /* If the definition of this type lives in .debug_types, read that type.
6635      Don't follow DW_AT_specification though, that will take us back up
6636      the chain and we want to go down.  */
6637   attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6638   if (attr)
6639     {
6640       struct dwarf2_cu *type_cu = cu;
6641       struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6642
6643       type = read_type_die (type_die, type_cu);
6644       return set_die_type (die, type, cu);
6645     }
6646
6647   type = alloc_type (objfile);
6648
6649   TYPE_CODE (type) = TYPE_CODE_ENUM;
6650   name = dwarf2_full_name (NULL, die, cu);
6651   if (name != NULL)
6652     TYPE_TAG_NAME (type) = (char *) name;
6653
6654   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6655   if (attr)
6656     {
6657       TYPE_LENGTH (type) = DW_UNSND (attr);
6658     }
6659   else
6660     {
6661       TYPE_LENGTH (type) = 0;
6662     }
6663
6664   /* The enumeration DIE can be incomplete.  In Ada, any type can be
6665      declared as private in the package spec, and then defined only
6666      inside the package body.  Such types are known as Taft Amendment
6667      Types.  When another package uses such a type, an incomplete DIE
6668      may be generated by the compiler.  */
6669   if (die_is_declaration (die, cu))
6670     TYPE_STUB (type) = 1;
6671
6672   return set_die_type (die, type, cu);
6673 }
6674
6675 /* Given a pointer to a die which begins an enumeration, process all
6676    the dies that define the members of the enumeration, and create the
6677    symbol for the enumeration type.
6678
6679    NOTE: We reverse the order of the element list.  */
6680
6681 static void
6682 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
6683 {
6684   struct die_info *child_die;
6685   struct field *fields;
6686   struct symbol *sym;
6687   int num_fields;
6688   int unsigned_enum = 1;
6689   char *name;
6690   struct type *this_type;
6691
6692   num_fields = 0;
6693   fields = NULL;
6694   this_type = get_die_type (die, cu);
6695   if (this_type == NULL)
6696     this_type = read_enumeration_type (die, cu);
6697   if (die->child != NULL)
6698     {
6699       child_die = die->child;
6700       while (child_die && child_die->tag)
6701         {
6702           if (child_die->tag != DW_TAG_enumerator)
6703             {
6704               process_die (child_die, cu);
6705             }
6706           else
6707             {
6708               name = dwarf2_name (child_die, cu);
6709               if (name)
6710                 {
6711                   sym = new_symbol (child_die, this_type, cu);
6712                   if (SYMBOL_VALUE (sym) < 0)
6713                     unsigned_enum = 0;
6714
6715                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
6716                     {
6717                       fields = (struct field *)
6718                         xrealloc (fields,
6719                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
6720                                   * sizeof (struct field));
6721                     }
6722
6723                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
6724                   FIELD_TYPE (fields[num_fields]) = NULL;
6725                   SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
6726                   FIELD_BITSIZE (fields[num_fields]) = 0;
6727
6728                   num_fields++;
6729                 }
6730             }
6731
6732           child_die = sibling_die (child_die);
6733         }
6734
6735       if (num_fields)
6736         {
6737           TYPE_NFIELDS (this_type) = num_fields;
6738           TYPE_FIELDS (this_type) = (struct field *)
6739             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
6740           memcpy (TYPE_FIELDS (this_type), fields,
6741                   sizeof (struct field) * num_fields);
6742           xfree (fields);
6743         }
6744       if (unsigned_enum)
6745         TYPE_UNSIGNED (this_type) = 1;
6746     }
6747
6748   new_symbol (die, this_type, cu);
6749 }
6750
6751 /* Extract all information from a DW_TAG_array_type DIE and put it in
6752    the DIE's type field.  For now, this only handles one dimensional
6753    arrays.  */
6754
6755 static struct type *
6756 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
6757 {
6758   struct objfile *objfile = cu->objfile;
6759   struct die_info *child_die;
6760   struct type *type;
6761   struct type *element_type, *range_type, *index_type;
6762   struct type **range_types = NULL;
6763   struct attribute *attr;
6764   int ndim = 0;
6765   struct cleanup *back_to;
6766   char *name;
6767
6768   element_type = die_type (die, cu);
6769
6770   /* The die_type call above may have already set the type for this DIE.  */
6771   type = get_die_type (die, cu);
6772   if (type)
6773     return type;
6774
6775   /* Irix 6.2 native cc creates array types without children for
6776      arrays with unspecified length.  */
6777   if (die->child == NULL)
6778     {
6779       index_type = objfile_type (objfile)->builtin_int;
6780       range_type = create_range_type (NULL, index_type, 0, -1);
6781       type = create_array_type (NULL, element_type, range_type);
6782       return set_die_type (die, type, cu);
6783     }
6784
6785   back_to = make_cleanup (null_cleanup, NULL);
6786   child_die = die->child;
6787   while (child_die && child_die->tag)
6788     {
6789       if (child_die->tag == DW_TAG_subrange_type)
6790         {
6791           struct type *child_type = read_type_die (child_die, cu);
6792
6793           if (child_type != NULL)
6794             {
6795               /* The range type was succesfully read. Save it for
6796                  the array type creation.  */
6797               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
6798                 {
6799                   range_types = (struct type **)
6800                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
6801                               * sizeof (struct type *));
6802                   if (ndim == 0)
6803                     make_cleanup (free_current_contents, &range_types);
6804                 }
6805               range_types[ndim++] = child_type;
6806             }
6807         }
6808       child_die = sibling_die (child_die);
6809     }
6810
6811   /* Dwarf2 dimensions are output from left to right, create the
6812      necessary array types in backwards order.  */
6813
6814   type = element_type;
6815
6816   if (read_array_order (die, cu) == DW_ORD_col_major)
6817     {
6818       int i = 0;
6819
6820       while (i < ndim)
6821         type = create_array_type (NULL, type, range_types[i++]);
6822     }
6823   else
6824     {
6825       while (ndim-- > 0)
6826         type = create_array_type (NULL, type, range_types[ndim]);
6827     }
6828
6829   /* Understand Dwarf2 support for vector types (like they occur on
6830      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
6831      array type.  This is not part of the Dwarf2/3 standard yet, but a
6832      custom vendor extension.  The main difference between a regular
6833      array and the vector variant is that vectors are passed by value
6834      to functions.  */
6835   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
6836   if (attr)
6837     make_vector_type (type);
6838
6839   name = dwarf2_name (die, cu);
6840   if (name)
6841     TYPE_NAME (type) = name;
6842
6843   /* Install the type in the die. */
6844   set_die_type (die, type, cu);
6845
6846   /* set_die_type should be already done.  */
6847   set_descriptive_type (type, die, cu);
6848
6849   do_cleanups (back_to);
6850
6851   return type;
6852 }
6853
6854 static enum dwarf_array_dim_ordering
6855 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
6856 {
6857   struct attribute *attr;
6858
6859   attr = dwarf2_attr (die, DW_AT_ordering, cu);
6860
6861   if (attr) return DW_SND (attr);
6862
6863   /*
6864     GNU F77 is a special case, as at 08/2004 array type info is the
6865     opposite order to the dwarf2 specification, but data is still
6866     laid out as per normal fortran.
6867
6868     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
6869     version checking.
6870   */
6871
6872   if (cu->language == language_fortran
6873       && cu->producer && strstr (cu->producer, "GNU F77"))
6874     {
6875       return DW_ORD_row_major;
6876     }
6877
6878   switch (cu->language_defn->la_array_ordering)
6879     {
6880     case array_column_major:
6881       return DW_ORD_col_major;
6882     case array_row_major:
6883     default:
6884       return DW_ORD_row_major;
6885     };
6886 }
6887
6888 /* Extract all information from a DW_TAG_set_type DIE and put it in
6889    the DIE's type field. */
6890
6891 static struct type *
6892 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
6893 {
6894   struct type *domain_type, *set_type;
6895   struct attribute *attr;
6896
6897   domain_type = die_type (die, cu);
6898
6899   /* The die_type call above may have already set the type for this DIE.  */
6900   set_type = get_die_type (die, cu);
6901   if (set_type)
6902     return set_type;
6903
6904   set_type = create_set_type (NULL, domain_type);
6905
6906   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6907   if (attr)
6908     TYPE_LENGTH (set_type) = DW_UNSND (attr);
6909
6910   return set_die_type (die, set_type, cu);
6911 }
6912
6913 /* First cut: install each common block member as a global variable.  */
6914
6915 static void
6916 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
6917 {
6918   struct die_info *child_die;
6919   struct attribute *attr;
6920   struct symbol *sym;
6921   CORE_ADDR base = (CORE_ADDR) 0;
6922
6923   attr = dwarf2_attr (die, DW_AT_location, cu);
6924   if (attr)
6925     {
6926       /* Support the .debug_loc offsets */
6927       if (attr_form_is_block (attr))
6928         {
6929           base = decode_locdesc (DW_BLOCK (attr), cu);
6930         }
6931       else if (attr_form_is_section_offset (attr))
6932         {
6933           dwarf2_complex_location_expr_complaint ();
6934         }
6935       else
6936         {
6937           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
6938                                                  "common block member");
6939         }
6940     }
6941   if (die->child != NULL)
6942     {
6943       child_die = die->child;
6944       while (child_die && child_die->tag)
6945         {
6946           sym = new_symbol (child_die, NULL, cu);
6947           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
6948           if (attr)
6949             {
6950               CORE_ADDR byte_offset = 0;
6951
6952               if (attr_form_is_section_offset (attr))
6953                 dwarf2_complex_location_expr_complaint ();
6954               else if (attr_form_is_constant (attr))
6955                 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6956               else if (attr_form_is_block (attr))
6957                 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6958               else
6959                 dwarf2_complex_location_expr_complaint ();
6960
6961               SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
6962               add_symbol_to_list (sym, &global_symbols);
6963             }
6964           child_die = sibling_die (child_die);
6965         }
6966     }
6967 }
6968
6969 /* Create a type for a C++ namespace.  */
6970
6971 static struct type *
6972 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
6973 {
6974   struct objfile *objfile = cu->objfile;
6975   const char *previous_prefix, *name;
6976   int is_anonymous;
6977   struct type *type;
6978
6979   /* For extensions, reuse the type of the original namespace.  */
6980   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
6981     {
6982       struct die_info *ext_die;
6983       struct dwarf2_cu *ext_cu = cu;
6984
6985       ext_die = dwarf2_extension (die, &ext_cu);
6986       type = read_type_die (ext_die, ext_cu);
6987       return set_die_type (die, type, cu);
6988     }
6989
6990   name = namespace_name (die, &is_anonymous, cu);
6991
6992   /* Now build the name of the current namespace.  */
6993
6994   previous_prefix = determine_prefix (die, cu);
6995   if (previous_prefix[0] != '\0')
6996     name = typename_concat (&objfile->objfile_obstack,
6997                             previous_prefix, name, 0, cu);
6998
6999   /* Create the type.  */
7000   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7001                     objfile);
7002   TYPE_NAME (type) = (char *) name;
7003   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7004
7005   return set_die_type (die, type, cu);
7006 }
7007
7008 /* Read a C++ namespace.  */
7009
7010 static void
7011 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7012 {
7013   struct objfile *objfile = cu->objfile;
7014   const char *name;
7015   int is_anonymous;
7016
7017   /* Add a symbol associated to this if we haven't seen the namespace
7018      before.  Also, add a using directive if it's an anonymous
7019      namespace.  */
7020
7021   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7022     {
7023       struct type *type;
7024
7025       type = read_type_die (die, cu);
7026       new_symbol (die, type, cu);
7027
7028       name = namespace_name (die, &is_anonymous, cu);
7029       if (is_anonymous)
7030         {
7031           const char *previous_prefix = determine_prefix (die, cu);
7032
7033           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7034                                   NULL, &objfile->objfile_obstack);
7035         }
7036     }
7037
7038   if (die->child != NULL)
7039     {
7040       struct die_info *child_die = die->child;
7041
7042       while (child_die && child_die->tag)
7043         {
7044           process_die (child_die, cu);
7045           child_die = sibling_die (child_die);
7046         }
7047     }
7048 }
7049
7050 /* Read a Fortran module as type.  This DIE can be only a declaration used for
7051    imported module.  Still we need that type as local Fortran "use ... only"
7052    declaration imports depend on the created type in determine_prefix.  */
7053
7054 static struct type *
7055 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7056 {
7057   struct objfile *objfile = cu->objfile;
7058   char *module_name;
7059   struct type *type;
7060
7061   module_name = dwarf2_name (die, cu);
7062   if (!module_name)
7063     complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7064                die->offset);
7065   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7066
7067   /* determine_prefix uses TYPE_TAG_NAME.  */
7068   TYPE_TAG_NAME (type) = TYPE_NAME (type);
7069
7070   return set_die_type (die, type, cu);
7071 }
7072
7073 /* Read a Fortran module.  */
7074
7075 static void
7076 read_module (struct die_info *die, struct dwarf2_cu *cu)
7077 {
7078   struct die_info *child_die = die->child;
7079
7080   while (child_die && child_die->tag)
7081     {
7082       process_die (child_die, cu);
7083       child_die = sibling_die (child_die);
7084     }
7085 }
7086
7087 /* Return the name of the namespace represented by DIE.  Set
7088    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7089    namespace.  */
7090
7091 static const char *
7092 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7093 {
7094   struct die_info *current_die;
7095   const char *name = NULL;
7096
7097   /* Loop through the extensions until we find a name.  */
7098
7099   for (current_die = die;
7100        current_die != NULL;
7101        current_die = dwarf2_extension (die, &cu))
7102     {
7103       name = dwarf2_name (current_die, cu);
7104       if (name != NULL)
7105         break;
7106     }
7107
7108   /* Is it an anonymous namespace?  */
7109
7110   *is_anonymous = (name == NULL);
7111   if (*is_anonymous)
7112     name = "(anonymous namespace)";
7113
7114   return name;
7115 }
7116
7117 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7118    the user defined type vector.  */
7119
7120 static struct type *
7121 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7122 {
7123   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7124   struct comp_unit_head *cu_header = &cu->header;
7125   struct type *type;
7126   struct attribute *attr_byte_size;
7127   struct attribute *attr_address_class;
7128   int byte_size, addr_class;
7129   struct type *target_type;
7130
7131   target_type = die_type (die, cu);
7132
7133   /* The die_type call above may have already set the type for this DIE.  */
7134   type = get_die_type (die, cu);
7135   if (type)
7136     return type;
7137
7138   type = lookup_pointer_type (target_type);
7139
7140   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7141   if (attr_byte_size)
7142     byte_size = DW_UNSND (attr_byte_size);
7143   else
7144     byte_size = cu_header->addr_size;
7145
7146   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7147   if (attr_address_class)
7148     addr_class = DW_UNSND (attr_address_class);
7149   else
7150     addr_class = DW_ADDR_none;
7151
7152   /* If the pointer size or address class is different than the
7153      default, create a type variant marked as such and set the
7154      length accordingly.  */
7155   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7156     {
7157       if (gdbarch_address_class_type_flags_p (gdbarch))
7158         {
7159           int type_flags;
7160
7161           type_flags = gdbarch_address_class_type_flags
7162                          (gdbarch, byte_size, addr_class);
7163           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7164                       == 0);
7165           type = make_type_with_address_space (type, type_flags);
7166         }
7167       else if (TYPE_LENGTH (type) != byte_size)
7168         {
7169           complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7170         }
7171       else
7172         {
7173           /* Should we also complain about unhandled address classes?  */
7174         }
7175     }
7176
7177   TYPE_LENGTH (type) = byte_size;
7178   return set_die_type (die, type, cu);
7179 }
7180
7181 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7182    the user defined type vector.  */
7183
7184 static struct type *
7185 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7186 {
7187   struct type *type;
7188   struct type *to_type;
7189   struct type *domain;
7190
7191   to_type = die_type (die, cu);
7192   domain = die_containing_type (die, cu);
7193
7194   /* The calls above may have already set the type for this DIE.  */
7195   type = get_die_type (die, cu);
7196   if (type)
7197     return type;
7198
7199   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7200     type = lookup_methodptr_type (to_type);
7201   else
7202     type = lookup_memberptr_type (to_type, domain);
7203
7204   return set_die_type (die, type, cu);
7205 }
7206
7207 /* Extract all information from a DW_TAG_reference_type DIE and add to
7208    the user defined type vector.  */
7209
7210 static struct type *
7211 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7212 {
7213   struct comp_unit_head *cu_header = &cu->header;
7214   struct type *type, *target_type;
7215   struct attribute *attr;
7216
7217   target_type = die_type (die, cu);
7218
7219   /* The die_type call above may have already set the type for this DIE.  */
7220   type = get_die_type (die, cu);
7221   if (type)
7222     return type;
7223
7224   type = lookup_reference_type (target_type);
7225   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7226   if (attr)
7227     {
7228       TYPE_LENGTH (type) = DW_UNSND (attr);
7229     }
7230   else
7231     {
7232       TYPE_LENGTH (type) = cu_header->addr_size;
7233     }
7234   return set_die_type (die, type, cu);
7235 }
7236
7237 static struct type *
7238 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7239 {
7240   struct type *base_type, *cv_type;
7241
7242   base_type = die_type (die, cu);
7243
7244   /* The die_type call above may have already set the type for this DIE.  */
7245   cv_type = get_die_type (die, cu);
7246   if (cv_type)
7247     return cv_type;
7248
7249   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7250   return set_die_type (die, cv_type, cu);
7251 }
7252
7253 static struct type *
7254 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7255 {
7256   struct type *base_type, *cv_type;
7257
7258   base_type = die_type (die, cu);
7259
7260   /* The die_type call above may have already set the type for this DIE.  */
7261   cv_type = get_die_type (die, cu);
7262   if (cv_type)
7263     return cv_type;
7264
7265   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7266   return set_die_type (die, cv_type, cu);
7267 }
7268
7269 /* Extract all information from a DW_TAG_string_type DIE and add to
7270    the user defined type vector.  It isn't really a user defined type,
7271    but it behaves like one, with other DIE's using an AT_user_def_type
7272    attribute to reference it.  */
7273
7274 static struct type *
7275 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7276 {
7277   struct objfile *objfile = cu->objfile;
7278   struct gdbarch *gdbarch = get_objfile_arch (objfile);
7279   struct type *type, *range_type, *index_type, *char_type;
7280   struct attribute *attr;
7281   unsigned int length;
7282
7283   attr = dwarf2_attr (die, DW_AT_string_length, cu);
7284   if (attr)
7285     {
7286       length = DW_UNSND (attr);
7287     }
7288   else
7289     {
7290       /* check for the DW_AT_byte_size attribute */
7291       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7292       if (attr)
7293         {
7294           length = DW_UNSND (attr);
7295         }
7296       else
7297         {
7298           length = 1;
7299         }
7300     }
7301
7302   index_type = objfile_type (objfile)->builtin_int;
7303   range_type = create_range_type (NULL, index_type, 1, length);
7304   char_type = language_string_char_type (cu->language_defn, gdbarch);
7305   type = create_string_type (NULL, char_type, range_type);
7306
7307   return set_die_type (die, type, cu);
7308 }
7309
7310 /* Handle DIES due to C code like:
7311
7312    struct foo
7313    {
7314    int (*funcp)(int a, long l);
7315    int b;
7316    };
7317
7318    ('funcp' generates a DW_TAG_subroutine_type DIE)
7319  */
7320
7321 static struct type *
7322 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7323 {
7324   struct type *type;            /* Type that this function returns */
7325   struct type *ftype;           /* Function that returns above type */
7326   struct attribute *attr;
7327
7328   type = die_type (die, cu);
7329
7330   /* The die_type call above may have already set the type for this DIE.  */
7331   ftype = get_die_type (die, cu);
7332   if (ftype)
7333     return ftype;
7334
7335   ftype = lookup_function_type (type);
7336
7337   /* All functions in C++, Pascal and Java have prototypes.  */
7338   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7339   if ((attr && (DW_UNSND (attr) != 0))
7340       || cu->language == language_cplus
7341       || cu->language == language_java
7342       || cu->language == language_pascal)
7343     TYPE_PROTOTYPED (ftype) = 1;
7344   else if (producer_is_realview (cu->producer))
7345     /* RealView does not emit DW_AT_prototyped.  We can not
7346        distinguish prototyped and unprototyped functions; default to
7347        prototyped, since that is more common in modern code (and
7348        RealView warns about unprototyped functions).  */
7349     TYPE_PROTOTYPED (ftype) = 1;
7350
7351   /* Store the calling convention in the type if it's available in
7352      the subroutine die.  Otherwise set the calling convention to
7353      the default value DW_CC_normal.  */
7354   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7355   TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7356
7357   /* We need to add the subroutine type to the die immediately so
7358      we don't infinitely recurse when dealing with parameters
7359      declared as the same subroutine type. */
7360   set_die_type (die, ftype, cu);
7361
7362   if (die->child != NULL)
7363     {
7364       struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7365       struct die_info *child_die;
7366       int nparams, iparams;
7367
7368       /* Count the number of parameters.
7369          FIXME: GDB currently ignores vararg functions, but knows about
7370          vararg member functions.  */
7371       nparams = 0;
7372       child_die = die->child;
7373       while (child_die && child_die->tag)
7374         {
7375           if (child_die->tag == DW_TAG_formal_parameter)
7376             nparams++;
7377           else if (child_die->tag == DW_TAG_unspecified_parameters)
7378             TYPE_VARARGS (ftype) = 1;
7379           child_die = sibling_die (child_die);
7380         }
7381
7382       /* Allocate storage for parameters and fill them in.  */
7383       TYPE_NFIELDS (ftype) = nparams;
7384       TYPE_FIELDS (ftype) = (struct field *)
7385         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7386
7387       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
7388          even if we error out during the parameters reading below.  */
7389       for (iparams = 0; iparams < nparams; iparams++)
7390         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7391
7392       iparams = 0;
7393       child_die = die->child;
7394       while (child_die && child_die->tag)
7395         {
7396           if (child_die->tag == DW_TAG_formal_parameter)
7397             {
7398               struct type *arg_type;
7399
7400               /* DWARF version 2 has no clean way to discern C++
7401                  static and non-static member functions.  G++ helps
7402                  GDB by marking the first parameter for non-static
7403                  member functions (which is the this pointer) as
7404                  artificial.  We pass this information to
7405                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7406
7407                  DWARF version 3 added DW_AT_object_pointer, which GCC
7408                  4.5 does not yet generate.  */
7409               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7410               if (attr)
7411                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7412               else
7413                 {
7414                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7415
7416                   /* GCC/43521: In java, the formal parameter
7417                      "this" is sometimes not marked with DW_AT_artificial.  */
7418                   if (cu->language == language_java)
7419                     {
7420                       const char *name = dwarf2_name (child_die, cu);
7421
7422                       if (name && !strcmp (name, "this"))
7423                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7424                     }
7425                 }
7426               arg_type = die_type (child_die, cu);
7427
7428               /* RealView does not mark THIS as const, which the testsuite
7429                  expects.  GCC marks THIS as const in method definitions,
7430                  but not in the class specifications (GCC PR 43053).  */
7431               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7432                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7433                 {
7434                   int is_this = 0;
7435                   struct dwarf2_cu *arg_cu = cu;
7436                   const char *name = dwarf2_name (child_die, cu);
7437
7438                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7439                   if (attr)
7440                     {
7441                       /* If the compiler emits this, use it.  */
7442                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
7443                         is_this = 1;
7444                     }
7445                   else if (name && strcmp (name, "this") == 0)
7446                     /* Function definitions will have the argument names.  */
7447                     is_this = 1;
7448                   else if (name == NULL && iparams == 0)
7449                     /* Declarations may not have the names, so like
7450                        elsewhere in GDB, assume an artificial first
7451                        argument is "this".  */
7452                     is_this = 1;
7453
7454                   if (is_this)
7455                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7456                                              arg_type, 0);
7457                 }
7458
7459               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7460               iparams++;
7461             }
7462           child_die = sibling_die (child_die);
7463         }
7464     }
7465
7466   return ftype;
7467 }
7468
7469 static struct type *
7470 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7471 {
7472   struct objfile *objfile = cu->objfile;
7473   const char *name = NULL;
7474   struct type *this_type;
7475
7476   name = dwarf2_full_name (NULL, die, cu);
7477   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7478                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
7479   TYPE_NAME (this_type) = (char *) name;
7480   set_die_type (die, this_type, cu);
7481   TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7482   return this_type;
7483 }
7484
7485 /* Find a representation of a given base type and install
7486    it in the TYPE field of the die.  */
7487
7488 static struct type *
7489 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7490 {
7491   struct objfile *objfile = cu->objfile;
7492   struct type *type;
7493   struct attribute *attr;
7494   int encoding = 0, size = 0;
7495   char *name;
7496   enum type_code code = TYPE_CODE_INT;
7497   int type_flags = 0;
7498   struct type *target_type = NULL;
7499
7500   attr = dwarf2_attr (die, DW_AT_encoding, cu);
7501   if (attr)
7502     {
7503       encoding = DW_UNSND (attr);
7504     }
7505   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7506   if (attr)
7507     {
7508       size = DW_UNSND (attr);
7509     }
7510   name = dwarf2_name (die, cu);
7511   if (!name)
7512     {
7513       complaint (&symfile_complaints,
7514                  _("DW_AT_name missing from DW_TAG_base_type"));
7515     }
7516
7517   switch (encoding)
7518     {
7519       case DW_ATE_address:
7520         /* Turn DW_ATE_address into a void * pointer.  */
7521         code = TYPE_CODE_PTR;
7522         type_flags |= TYPE_FLAG_UNSIGNED;
7523         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7524         break;
7525       case DW_ATE_boolean:
7526         code = TYPE_CODE_BOOL;
7527         type_flags |= TYPE_FLAG_UNSIGNED;
7528         break;
7529       case DW_ATE_complex_float:
7530         code = TYPE_CODE_COMPLEX;
7531         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7532         break;
7533       case DW_ATE_decimal_float:
7534         code = TYPE_CODE_DECFLOAT;
7535         break;
7536       case DW_ATE_float:
7537         code = TYPE_CODE_FLT;
7538         break;
7539       case DW_ATE_signed:
7540         break;
7541       case DW_ATE_unsigned:
7542         type_flags |= TYPE_FLAG_UNSIGNED;
7543         break;
7544       case DW_ATE_signed_char:
7545         if (cu->language == language_ada || cu->language == language_m2
7546             || cu->language == language_pascal)
7547           code = TYPE_CODE_CHAR;
7548         break;
7549       case DW_ATE_unsigned_char:
7550         if (cu->language == language_ada || cu->language == language_m2
7551             || cu->language == language_pascal)
7552           code = TYPE_CODE_CHAR;
7553         type_flags |= TYPE_FLAG_UNSIGNED;
7554         break;
7555       case DW_ATE_UTF:
7556         /* We just treat this as an integer and then recognize the
7557            type by name elsewhere.  */
7558         break;
7559
7560       default:
7561         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7562                    dwarf_type_encoding_name (encoding));
7563         break;
7564     }
7565
7566   type = init_type (code, size, type_flags, NULL, objfile);
7567   TYPE_NAME (type) = name;
7568   TYPE_TARGET_TYPE (type) = target_type;
7569
7570   if (name && strcmp (name, "char") == 0)
7571     TYPE_NOSIGN (type) = 1;
7572
7573   return set_die_type (die, type, cu);
7574 }
7575
7576 /* Read the given DW_AT_subrange DIE.  */
7577
7578 static struct type *
7579 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7580 {
7581   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7582   struct type *base_type;
7583   struct type *range_type;
7584   struct attribute *attr;
7585   LONGEST low = 0;
7586   LONGEST high = -1;
7587   char *name;
7588   LONGEST negative_mask;
7589
7590   base_type = die_type (die, cu);
7591   /* Preserve BASE_TYPE's original type, just set its LENGTH.  */
7592   check_typedef (base_type);
7593
7594   /* The die_type call above may have already set the type for this DIE.  */
7595   range_type = get_die_type (die, cu);
7596   if (range_type)
7597     return range_type;
7598
7599   if (cu->language == language_fortran)
7600     {
7601       /* FORTRAN implies a lower bound of 1, if not given.  */
7602       low = 1;
7603     }
7604
7605   /* FIXME: For variable sized arrays either of these could be
7606      a variable rather than a constant value.  We'll allow it,
7607      but we don't know how to handle it.  */
7608   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7609   if (attr)
7610     low = dwarf2_get_attr_constant_value (attr, 0);
7611
7612   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7613   if (attr)
7614     {
7615       if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7616         {
7617           /* GCC encodes arrays with unspecified or dynamic length
7618              with a DW_FORM_block1 attribute or a reference attribute.
7619              FIXME: GDB does not yet know how to handle dynamic
7620              arrays properly, treat them as arrays with unspecified
7621              length for now.
7622
7623              FIXME: jimb/2003-09-22: GDB does not really know
7624              how to handle arrays of unspecified length
7625              either; we just represent them as zero-length
7626              arrays.  Choose an appropriate upper bound given
7627              the lower bound we've computed above.  */
7628           high = low - 1;
7629         }
7630       else
7631         high = dwarf2_get_attr_constant_value (attr, 1);
7632     }
7633   else
7634     {
7635       attr = dwarf2_attr (die, DW_AT_count, cu);
7636       if (attr)
7637         {
7638           int count = dwarf2_get_attr_constant_value (attr, 1);
7639           high = low + count - 1;
7640         }
7641     }
7642
7643   /* Dwarf-2 specifications explicitly allows to create subrange types
7644      without specifying a base type.
7645      In that case, the base type must be set to the type of
7646      the lower bound, upper bound or count, in that order, if any of these
7647      three attributes references an object that has a type.
7648      If no base type is found, the Dwarf-2 specifications say that
7649      a signed integer type of size equal to the size of an address should
7650      be used.
7651      For the following C code: `extern char gdb_int [];'
7652      GCC produces an empty range DIE.
7653      FIXME: muller/2010-05-28: Possible references to object for low bound,
7654      high bound or count are not yet handled by this code.
7655   */
7656   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
7657     {
7658       struct objfile *objfile = cu->objfile;
7659       struct gdbarch *gdbarch = get_objfile_arch (objfile);
7660       int addr_size = gdbarch_addr_bit (gdbarch) /8;
7661       struct type *int_type = objfile_type (objfile)->builtin_int;
7662
7663       /* Test "int", "long int", and "long long int" objfile types,
7664          and select the first one having a size above or equal to the
7665          architecture address size.  */
7666       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7667         base_type = int_type;
7668       else
7669         {
7670           int_type = objfile_type (objfile)->builtin_long;
7671           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7672             base_type = int_type;
7673           else
7674             {
7675               int_type = objfile_type (objfile)->builtin_long_long;
7676               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
7677                 base_type = int_type;
7678             }
7679         }
7680     }
7681
7682   negative_mask =
7683     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
7684   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
7685     low |= negative_mask;
7686   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
7687     high |= negative_mask;
7688
7689   range_type = create_range_type (NULL, base_type, low, high);
7690
7691   /* Mark arrays with dynamic length at least as an array of unspecified
7692      length.  GDB could check the boundary but before it gets implemented at
7693      least allow accessing the array elements.  */
7694   if (attr && attr->form == DW_FORM_block1)
7695     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
7696
7697   name = dwarf2_name (die, cu);
7698   if (name)
7699     TYPE_NAME (range_type) = name;
7700
7701   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7702   if (attr)
7703     TYPE_LENGTH (range_type) = DW_UNSND (attr);
7704
7705   set_die_type (die, range_type, cu);
7706
7707   /* set_die_type should be already done.  */
7708   set_descriptive_type (range_type, die, cu);
7709
7710   return range_type;
7711 }
7712
7713 static struct type *
7714 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
7715 {
7716   struct type *type;
7717
7718   /* For now, we only support the C meaning of an unspecified type: void.  */
7719
7720   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
7721   TYPE_NAME (type) = dwarf2_name (die, cu);
7722
7723   return set_die_type (die, type, cu);
7724 }
7725
7726 /* Trivial hash function for die_info: the hash value of a DIE
7727    is its offset in .debug_info for this objfile.  */
7728
7729 static hashval_t
7730 die_hash (const void *item)
7731 {
7732   const struct die_info *die = item;
7733
7734   return die->offset;
7735 }
7736
7737 /* Trivial comparison function for die_info structures: two DIEs
7738    are equal if they have the same offset.  */
7739
7740 static int
7741 die_eq (const void *item_lhs, const void *item_rhs)
7742 {
7743   const struct die_info *die_lhs = item_lhs;
7744   const struct die_info *die_rhs = item_rhs;
7745
7746   return die_lhs->offset == die_rhs->offset;
7747 }
7748
7749 /* Read a whole compilation unit into a linked list of dies.  */
7750
7751 static struct die_info *
7752 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
7753 {
7754   struct die_reader_specs reader_specs;
7755
7756   gdb_assert (cu->die_hash == NULL);
7757   cu->die_hash
7758     = htab_create_alloc_ex (cu->header.length / 12,
7759                             die_hash,
7760                             die_eq,
7761                             NULL,
7762                             &cu->comp_unit_obstack,
7763                             hashtab_obstack_allocate,
7764                             dummy_obstack_deallocate);
7765
7766   init_cu_die_reader (&reader_specs, cu);
7767
7768   return read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
7769 }
7770
7771 /* Main entry point for reading a DIE and all children.
7772    Read the DIE and dump it if requested.  */
7773
7774 static struct die_info *
7775 read_die_and_children (const struct die_reader_specs *reader,
7776                        gdb_byte *info_ptr,
7777                        gdb_byte **new_info_ptr,
7778                        struct die_info *parent)
7779 {
7780   struct die_info *result = read_die_and_children_1 (reader, info_ptr,
7781                                                      new_info_ptr, parent);
7782
7783   if (dwarf2_die_debug)
7784     {
7785       fprintf_unfiltered (gdb_stdlog,
7786                           "\nRead die from %s of %s:\n",
7787                           reader->buffer == dwarf2_per_objfile->info.buffer
7788                           ? ".debug_info"
7789                           : reader->buffer == dwarf2_per_objfile->types.buffer
7790                           ? ".debug_types"
7791                           : "unknown section",
7792                           reader->abfd->filename);
7793       dump_die (result, dwarf2_die_debug);
7794     }
7795
7796   return result;
7797 }
7798
7799 /* Read a single die and all its descendents.  Set the die's sibling
7800    field to NULL; set other fields in the die correctly, and set all
7801    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
7802    location of the info_ptr after reading all of those dies.  PARENT
7803    is the parent of the die in question.  */
7804
7805 static struct die_info *
7806 read_die_and_children_1 (const struct die_reader_specs *reader,
7807                          gdb_byte *info_ptr,
7808                          gdb_byte **new_info_ptr,
7809                          struct die_info *parent)
7810 {
7811   struct die_info *die;
7812   gdb_byte *cur_ptr;
7813   int has_children;
7814
7815   cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
7816   if (die == NULL)
7817     {
7818       *new_info_ptr = cur_ptr;
7819       return NULL;
7820     }
7821   store_in_ref_table (die, reader->cu);
7822
7823   if (has_children)
7824     die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
7825   else
7826     {
7827       die->child = NULL;
7828       *new_info_ptr = cur_ptr;
7829     }
7830
7831   die->sibling = NULL;
7832   die->parent = parent;
7833   return die;
7834 }
7835
7836 /* Read a die, all of its descendents, and all of its siblings; set
7837    all of the fields of all of the dies correctly.  Arguments are as
7838    in read_die_and_children.  */
7839
7840 static struct die_info *
7841 read_die_and_siblings (const struct die_reader_specs *reader,
7842                        gdb_byte *info_ptr,
7843                        gdb_byte **new_info_ptr,
7844                        struct die_info *parent)
7845 {
7846   struct die_info *first_die, *last_sibling;
7847   gdb_byte *cur_ptr;
7848
7849   cur_ptr = info_ptr;
7850   first_die = last_sibling = NULL;
7851
7852   while (1)
7853     {
7854       struct die_info *die
7855         = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
7856
7857       if (die == NULL)
7858         {
7859           *new_info_ptr = cur_ptr;
7860           return first_die;
7861         }
7862
7863       if (!first_die)
7864         first_die = die;
7865       else
7866         last_sibling->sibling = die;
7867
7868       last_sibling = die;
7869     }
7870 }
7871
7872 /* Read the die from the .debug_info section buffer.  Set DIEP to
7873    point to a newly allocated die with its information, except for its
7874    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
7875    whether the die has children or not.  */
7876
7877 static gdb_byte *
7878 read_full_die (const struct die_reader_specs *reader,
7879                struct die_info **diep, gdb_byte *info_ptr,
7880                int *has_children)
7881 {
7882   unsigned int abbrev_number, bytes_read, i, offset;
7883   struct abbrev_info *abbrev;
7884   struct die_info *die;
7885   struct dwarf2_cu *cu = reader->cu;
7886   bfd *abfd = reader->abfd;
7887
7888   offset = info_ptr - reader->buffer;
7889   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
7890   info_ptr += bytes_read;
7891   if (!abbrev_number)
7892     {
7893       *diep = NULL;
7894       *has_children = 0;
7895       return info_ptr;
7896     }
7897
7898   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
7899   if (!abbrev)
7900     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
7901            abbrev_number,
7902            bfd_get_filename (abfd));
7903
7904   die = dwarf_alloc_die (cu, abbrev->num_attrs);
7905   die->offset = offset;
7906   die->tag = abbrev->tag;
7907   die->abbrev = abbrev_number;
7908
7909   die->num_attrs = abbrev->num_attrs;
7910
7911   for (i = 0; i < abbrev->num_attrs; ++i)
7912     info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
7913                                abfd, info_ptr, cu);
7914
7915   *diep = die;
7916   *has_children = abbrev->has_children;
7917   return info_ptr;
7918 }
7919
7920 /* In DWARF version 2, the description of the debugging information is
7921    stored in a separate .debug_abbrev section.  Before we read any
7922    dies from a section we read in all abbreviations and install them
7923    in a hash table.  This function also sets flags in CU describing
7924    the data found in the abbrev table.  */
7925
7926 static void
7927 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
7928 {
7929   struct comp_unit_head *cu_header = &cu->header;
7930   gdb_byte *abbrev_ptr;
7931   struct abbrev_info *cur_abbrev;
7932   unsigned int abbrev_number, bytes_read, abbrev_name;
7933   unsigned int abbrev_form, hash_number;
7934   struct attr_abbrev *cur_attrs;
7935   unsigned int allocated_attrs;
7936
7937   /* Initialize dwarf2 abbrevs */
7938   obstack_init (&cu->abbrev_obstack);
7939   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
7940                                       (ABBREV_HASH_SIZE
7941                                        * sizeof (struct abbrev_info *)));
7942   memset (cu->dwarf2_abbrevs, 0,
7943           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
7944
7945   dwarf2_read_section (dwarf2_per_objfile->objfile,
7946                        &dwarf2_per_objfile->abbrev);
7947   abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
7948   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7949   abbrev_ptr += bytes_read;
7950
7951   allocated_attrs = ATTR_ALLOC_CHUNK;
7952   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
7953
7954   /* loop until we reach an abbrev number of 0 */
7955   while (abbrev_number)
7956     {
7957       cur_abbrev = dwarf_alloc_abbrev (cu);
7958
7959       /* read in abbrev header */
7960       cur_abbrev->number = abbrev_number;
7961       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7962       abbrev_ptr += bytes_read;
7963       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
7964       abbrev_ptr += 1;
7965
7966       if (cur_abbrev->tag == DW_TAG_namespace)
7967         cu->has_namespace_info = 1;
7968
7969       /* now read in declarations */
7970       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7971       abbrev_ptr += bytes_read;
7972       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7973       abbrev_ptr += bytes_read;
7974       while (abbrev_name)
7975         {
7976           if (cur_abbrev->num_attrs == allocated_attrs)
7977             {
7978               allocated_attrs += ATTR_ALLOC_CHUNK;
7979               cur_attrs
7980                 = xrealloc (cur_attrs, (allocated_attrs
7981                                         * sizeof (struct attr_abbrev)));
7982             }
7983
7984           /* Record whether this compilation unit might have
7985              inter-compilation-unit references.  If we don't know what form
7986              this attribute will have, then it might potentially be a
7987              DW_FORM_ref_addr, so we conservatively expect inter-CU
7988              references.  */
7989
7990           if (abbrev_form == DW_FORM_ref_addr
7991               || abbrev_form == DW_FORM_indirect)
7992             cu->has_form_ref_addr = 1;
7993
7994           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
7995           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
7996           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7997           abbrev_ptr += bytes_read;
7998           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
7999           abbrev_ptr += bytes_read;
8000         }
8001
8002       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8003                                          (cur_abbrev->num_attrs
8004                                           * sizeof (struct attr_abbrev)));
8005       memcpy (cur_abbrev->attrs, cur_attrs,
8006               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8007
8008       hash_number = abbrev_number % ABBREV_HASH_SIZE;
8009       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8010       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8011
8012       /* Get next abbreviation.
8013          Under Irix6 the abbreviations for a compilation unit are not
8014          always properly terminated with an abbrev number of 0.
8015          Exit loop if we encounter an abbreviation which we have
8016          already read (which means we are about to read the abbreviations
8017          for the next compile unit) or if the end of the abbreviation
8018          table is reached.  */
8019       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8020           >= dwarf2_per_objfile->abbrev.size)
8021         break;
8022       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8023       abbrev_ptr += bytes_read;
8024       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8025         break;
8026     }
8027
8028   xfree (cur_attrs);
8029 }
8030
8031 /* Release the memory used by the abbrev table for a compilation unit.  */
8032
8033 static void
8034 dwarf2_free_abbrev_table (void *ptr_to_cu)
8035 {
8036   struct dwarf2_cu *cu = ptr_to_cu;
8037
8038   obstack_free (&cu->abbrev_obstack, NULL);
8039   cu->dwarf2_abbrevs = NULL;
8040 }
8041
8042 /* Lookup an abbrev_info structure in the abbrev hash table.  */
8043
8044 static struct abbrev_info *
8045 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8046 {
8047   unsigned int hash_number;
8048   struct abbrev_info *abbrev;
8049
8050   hash_number = number % ABBREV_HASH_SIZE;
8051   abbrev = cu->dwarf2_abbrevs[hash_number];
8052
8053   while (abbrev)
8054     {
8055       if (abbrev->number == number)
8056         return abbrev;
8057       else
8058         abbrev = abbrev->next;
8059     }
8060   return NULL;
8061 }
8062
8063 /* Returns nonzero if TAG represents a type that we might generate a partial
8064    symbol for.  */
8065
8066 static int
8067 is_type_tag_for_partial (int tag)
8068 {
8069   switch (tag)
8070     {
8071 #if 0
8072     /* Some types that would be reasonable to generate partial symbols for,
8073        that we don't at present.  */
8074     case DW_TAG_array_type:
8075     case DW_TAG_file_type:
8076     case DW_TAG_ptr_to_member_type:
8077     case DW_TAG_set_type:
8078     case DW_TAG_string_type:
8079     case DW_TAG_subroutine_type:
8080 #endif
8081     case DW_TAG_base_type:
8082     case DW_TAG_class_type:
8083     case DW_TAG_interface_type:
8084     case DW_TAG_enumeration_type:
8085     case DW_TAG_structure_type:
8086     case DW_TAG_subrange_type:
8087     case DW_TAG_typedef:
8088     case DW_TAG_union_type:
8089       return 1;
8090     default:
8091       return 0;
8092     }
8093 }
8094
8095 /* Load all DIEs that are interesting for partial symbols into memory.  */
8096
8097 static struct partial_die_info *
8098 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8099                    int building_psymtab, struct dwarf2_cu *cu)
8100 {
8101   struct partial_die_info *part_die;
8102   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8103   struct abbrev_info *abbrev;
8104   unsigned int bytes_read;
8105   unsigned int load_all = 0;
8106
8107   int nesting_level = 1;
8108
8109   parent_die = NULL;
8110   last_die = NULL;
8111
8112   if (cu->per_cu && cu->per_cu->load_all_dies)
8113     load_all = 1;
8114
8115   cu->partial_dies
8116     = htab_create_alloc_ex (cu->header.length / 12,
8117                             partial_die_hash,
8118                             partial_die_eq,
8119                             NULL,
8120                             &cu->comp_unit_obstack,
8121                             hashtab_obstack_allocate,
8122                             dummy_obstack_deallocate);
8123
8124   part_die = obstack_alloc (&cu->comp_unit_obstack,
8125                             sizeof (struct partial_die_info));
8126
8127   while (1)
8128     {
8129       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8130
8131       /* A NULL abbrev means the end of a series of children.  */
8132       if (abbrev == NULL)
8133         {
8134           if (--nesting_level == 0)
8135             {
8136               /* PART_DIE was probably the last thing allocated on the
8137                  comp_unit_obstack, so we could call obstack_free
8138                  here.  We don't do that because the waste is small,
8139                  and will be cleaned up when we're done with this
8140                  compilation unit.  This way, we're also more robust
8141                  against other users of the comp_unit_obstack.  */
8142               return first_die;
8143             }
8144           info_ptr += bytes_read;
8145           last_die = parent_die;
8146           parent_die = parent_die->die_parent;
8147           continue;
8148         }
8149
8150       /* Check whether this DIE is interesting enough to save.  Normally
8151          we would not be interested in members here, but there may be
8152          later variables referencing them via DW_AT_specification (for
8153          static members).  */
8154       if (!load_all
8155           && !is_type_tag_for_partial (abbrev->tag)
8156           && abbrev->tag != DW_TAG_enumerator
8157           && abbrev->tag != DW_TAG_subprogram
8158           && abbrev->tag != DW_TAG_lexical_block
8159           && abbrev->tag != DW_TAG_variable
8160           && abbrev->tag != DW_TAG_namespace
8161           && abbrev->tag != DW_TAG_module
8162           && abbrev->tag != DW_TAG_member)
8163         {
8164           /* Otherwise we skip to the next sibling, if any.  */
8165           info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8166           continue;
8167         }
8168
8169       info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8170                                    buffer, info_ptr, cu);
8171
8172       /* This two-pass algorithm for processing partial symbols has a
8173          high cost in cache pressure.  Thus, handle some simple cases
8174          here which cover the majority of C partial symbols.  DIEs
8175          which neither have specification tags in them, nor could have
8176          specification tags elsewhere pointing at them, can simply be
8177          processed and discarded.
8178
8179          This segment is also optional; scan_partial_symbols and
8180          add_partial_symbol will handle these DIEs if we chain
8181          them in normally.  When compilers which do not emit large
8182          quantities of duplicate debug information are more common,
8183          this code can probably be removed.  */
8184
8185       /* Any complete simple types at the top level (pretty much all
8186          of them, for a language without namespaces), can be processed
8187          directly.  */
8188       if (parent_die == NULL
8189           && part_die->has_specification == 0
8190           && part_die->is_declaration == 0
8191           && (part_die->tag == DW_TAG_typedef
8192               || part_die->tag == DW_TAG_base_type
8193               || part_die->tag == DW_TAG_subrange_type))
8194         {
8195           if (building_psymtab && part_die->name != NULL)
8196             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8197                                  VAR_DOMAIN, LOC_TYPEDEF,
8198                                  &cu->objfile->static_psymbols,
8199                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8200           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8201           continue;
8202         }
8203
8204       /* If we're at the second level, and we're an enumerator, and
8205          our parent has no specification (meaning possibly lives in a
8206          namespace elsewhere), then we can add the partial symbol now
8207          instead of queueing it.  */
8208       if (part_die->tag == DW_TAG_enumerator
8209           && parent_die != NULL
8210           && parent_die->die_parent == NULL
8211           && parent_die->tag == DW_TAG_enumeration_type
8212           && parent_die->has_specification == 0)
8213         {
8214           if (part_die->name == NULL)
8215             complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8216           else if (building_psymtab)
8217             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8218                                  VAR_DOMAIN, LOC_CONST,
8219                                  (cu->language == language_cplus
8220                                   || cu->language == language_java)
8221                                  ? &cu->objfile->global_psymbols
8222                                  : &cu->objfile->static_psymbols,
8223                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
8224
8225           info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8226           continue;
8227         }
8228
8229       /* We'll save this DIE so link it in.  */
8230       part_die->die_parent = parent_die;
8231       part_die->die_sibling = NULL;
8232       part_die->die_child = NULL;
8233
8234       if (last_die && last_die == parent_die)
8235         last_die->die_child = part_die;
8236       else if (last_die)
8237         last_die->die_sibling = part_die;
8238
8239       last_die = part_die;
8240
8241       if (first_die == NULL)
8242         first_die = part_die;
8243
8244       /* Maybe add the DIE to the hash table.  Not all DIEs that we
8245          find interesting need to be in the hash table, because we
8246          also have the parent/sibling/child chains; only those that we
8247          might refer to by offset later during partial symbol reading.
8248
8249          For now this means things that might have be the target of a
8250          DW_AT_specification, DW_AT_abstract_origin, or
8251          DW_AT_extension.  DW_AT_extension will refer only to
8252          namespaces; DW_AT_abstract_origin refers to functions (and
8253          many things under the function DIE, but we do not recurse
8254          into function DIEs during partial symbol reading) and
8255          possibly variables as well; DW_AT_specification refers to
8256          declarations.  Declarations ought to have the DW_AT_declaration
8257          flag.  It happens that GCC forgets to put it in sometimes, but
8258          only for functions, not for types.
8259
8260          Adding more things than necessary to the hash table is harmless
8261          except for the performance cost.  Adding too few will result in
8262          wasted time in find_partial_die, when we reread the compilation
8263          unit with load_all_dies set.  */
8264
8265       if (load_all
8266           || abbrev->tag == DW_TAG_subprogram
8267           || abbrev->tag == DW_TAG_variable
8268           || abbrev->tag == DW_TAG_namespace
8269           || part_die->is_declaration)
8270         {
8271           void **slot;
8272
8273           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8274                                            part_die->offset, INSERT);
8275           *slot = part_die;
8276         }
8277
8278       part_die = obstack_alloc (&cu->comp_unit_obstack,
8279                                 sizeof (struct partial_die_info));
8280
8281       /* For some DIEs we want to follow their children (if any).  For C
8282          we have no reason to follow the children of structures; for other
8283          languages we have to, both so that we can get at method physnames
8284          to infer fully qualified class names, and for DW_AT_specification.
8285
8286          For Ada, we need to scan the children of subprograms and lexical
8287          blocks as well because Ada allows the definition of nested
8288          entities that could be interesting for the debugger, such as
8289          nested subprograms for instance.  */
8290       if (last_die->has_children
8291           && (load_all
8292               || last_die->tag == DW_TAG_namespace
8293               || last_die->tag == DW_TAG_module
8294               || last_die->tag == DW_TAG_enumeration_type
8295               || (cu->language != language_c
8296                   && (last_die->tag == DW_TAG_class_type
8297                       || last_die->tag == DW_TAG_interface_type
8298                       || last_die->tag == DW_TAG_structure_type
8299                       || last_die->tag == DW_TAG_union_type))
8300               || (cu->language == language_ada
8301                   && (last_die->tag == DW_TAG_subprogram
8302                       || last_die->tag == DW_TAG_lexical_block))))
8303         {
8304           nesting_level++;
8305           parent_die = last_die;
8306           continue;
8307         }
8308
8309       /* Otherwise we skip to the next sibling, if any.  */
8310       info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8311
8312       /* Back to the top, do it again.  */
8313     }
8314 }
8315
8316 /* Read a minimal amount of information into the minimal die structure.  */
8317
8318 static gdb_byte *
8319 read_partial_die (struct partial_die_info *part_die,
8320                   struct abbrev_info *abbrev,
8321                   unsigned int abbrev_len, bfd *abfd,
8322                   gdb_byte *buffer, gdb_byte *info_ptr,
8323                   struct dwarf2_cu *cu)
8324 {
8325   unsigned int i;
8326   struct attribute attr;
8327   int has_low_pc_attr = 0;
8328   int has_high_pc_attr = 0;
8329
8330   memset (part_die, 0, sizeof (struct partial_die_info));
8331
8332   part_die->offset = info_ptr - buffer;
8333
8334   info_ptr += abbrev_len;
8335
8336   if (abbrev == NULL)
8337     return info_ptr;
8338
8339   part_die->tag = abbrev->tag;
8340   part_die->has_children = abbrev->has_children;
8341
8342   for (i = 0; i < abbrev->num_attrs; ++i)
8343     {
8344       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8345
8346       /* Store the data if it is of an attribute we want to keep in a
8347          partial symbol table.  */
8348       switch (attr.name)
8349         {
8350         case DW_AT_name:
8351           switch (part_die->tag)
8352             {
8353             case DW_TAG_compile_unit:
8354             case DW_TAG_type_unit:
8355               /* Compilation units have a DW_AT_name that is a filename, not
8356                  a source language identifier.  */
8357             case DW_TAG_enumeration_type:
8358             case DW_TAG_enumerator:
8359               /* These tags always have simple identifiers already; no need
8360                  to canonicalize them.  */
8361               part_die->name = DW_STRING (&attr);
8362               break;
8363             default:
8364               part_die->name
8365                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8366                                             &cu->objfile->objfile_obstack);
8367               break;
8368             }
8369           break;
8370         case DW_AT_linkage_name:
8371         case DW_AT_MIPS_linkage_name:
8372           /* Note that both forms of linkage name might appear.  We
8373              assume they will be the same, and we only store the last
8374              one we see.  */
8375           if (cu->language == language_ada)
8376             part_die->name = DW_STRING (&attr);
8377           break;
8378         case DW_AT_low_pc:
8379           has_low_pc_attr = 1;
8380           part_die->lowpc = DW_ADDR (&attr);
8381           break;
8382         case DW_AT_high_pc:
8383           has_high_pc_attr = 1;
8384           part_die->highpc = DW_ADDR (&attr);
8385           break;
8386         case DW_AT_location:
8387           /* Support the .debug_loc offsets */
8388           if (attr_form_is_block (&attr))
8389             {
8390                part_die->locdesc = DW_BLOCK (&attr);
8391             }
8392           else if (attr_form_is_section_offset (&attr))
8393             {
8394               dwarf2_complex_location_expr_complaint ();
8395             }
8396           else
8397             {
8398               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8399                                                      "partial symbol information");
8400             }
8401           break;
8402         case DW_AT_external:
8403           part_die->is_external = DW_UNSND (&attr);
8404           break;
8405         case DW_AT_declaration:
8406           part_die->is_declaration = DW_UNSND (&attr);
8407           break;
8408         case DW_AT_type:
8409           part_die->has_type = 1;
8410           break;
8411         case DW_AT_abstract_origin:
8412         case DW_AT_specification:
8413         case DW_AT_extension:
8414           part_die->has_specification = 1;
8415           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8416           break;
8417         case DW_AT_sibling:
8418           /* Ignore absolute siblings, they might point outside of
8419              the current compile unit.  */
8420           if (attr.form == DW_FORM_ref_addr)
8421             complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8422           else
8423             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8424           break;
8425         case DW_AT_byte_size:
8426           part_die->has_byte_size = 1;
8427           break;
8428         case DW_AT_calling_convention:
8429           /* DWARF doesn't provide a way to identify a program's source-level
8430              entry point.  DW_AT_calling_convention attributes are only meant
8431              to describe functions' calling conventions.
8432
8433              However, because it's a necessary piece of information in
8434              Fortran, and because DW_CC_program is the only piece of debugging
8435              information whose definition refers to a 'main program' at all,
8436              several compilers have begun marking Fortran main programs with
8437              DW_CC_program --- even when those functions use the standard
8438              calling conventions.
8439
8440              So until DWARF specifies a way to provide this information and
8441              compilers pick up the new representation, we'll support this
8442              practice.  */
8443           if (DW_UNSND (&attr) == DW_CC_program
8444               && cu->language == language_fortran)
8445             set_main_name (part_die->name);
8446           break;
8447         default:
8448           break;
8449         }
8450     }
8451
8452   /* When using the GNU linker, .gnu.linkonce. sections are used to
8453      eliminate duplicate copies of functions and vtables and such.
8454      The linker will arbitrarily choose one and discard the others.
8455      The AT_*_pc values for such functions refer to local labels in
8456      these sections.  If the section from that file was discarded, the
8457      labels are not in the output, so the relocs get a value of 0.
8458      If this is a discarded function, mark the pc bounds as invalid,
8459      so that GDB will ignore it.  */
8460   if (has_low_pc_attr && has_high_pc_attr
8461       && part_die->lowpc < part_die->highpc
8462       && (part_die->lowpc != 0
8463           || dwarf2_per_objfile->has_section_at_zero))
8464     part_die->has_pc_info = 1;
8465
8466   return info_ptr;
8467 }
8468
8469 /* Find a cached partial DIE at OFFSET in CU.  */
8470
8471 static struct partial_die_info *
8472 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8473 {
8474   struct partial_die_info *lookup_die = NULL;
8475   struct partial_die_info part_die;
8476
8477   part_die.offset = offset;
8478   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8479
8480   return lookup_die;
8481 }
8482
8483 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8484    except in the case of .debug_types DIEs which do not reference
8485    outside their CU (they do however referencing other types via
8486    DW_FORM_sig8).  */
8487
8488 static struct partial_die_info *
8489 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8490 {
8491   struct dwarf2_per_cu_data *per_cu = NULL;
8492   struct partial_die_info *pd = NULL;
8493
8494   if (cu->per_cu->from_debug_types)
8495     {
8496       pd = find_partial_die_in_comp_unit (offset, cu);
8497       if (pd != NULL)
8498         return pd;
8499       goto not_found;
8500     }
8501
8502   if (offset_in_cu_p (&cu->header, offset))
8503     {
8504       pd = find_partial_die_in_comp_unit (offset, cu);
8505       if (pd != NULL)
8506         return pd;
8507     }
8508
8509   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8510
8511   if (per_cu->cu == NULL)
8512     {
8513       load_partial_comp_unit (per_cu, cu->objfile);
8514       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
8515       dwarf2_per_objfile->read_in_chain = per_cu;
8516     }
8517
8518   per_cu->cu->last_used = 0;
8519   pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8520
8521   if (pd == NULL && per_cu->load_all_dies == 0)
8522     {
8523       struct cleanup *back_to;
8524       struct partial_die_info comp_unit_die;
8525       struct abbrev_info *abbrev;
8526       unsigned int bytes_read;
8527       char *info_ptr;
8528
8529       per_cu->load_all_dies = 1;
8530
8531       /* Re-read the DIEs.  */
8532       back_to = make_cleanup (null_cleanup, 0);
8533       if (per_cu->cu->dwarf2_abbrevs == NULL)
8534         {
8535           dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8536           make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8537         }
8538       info_ptr = (dwarf2_per_objfile->info.buffer
8539                   + per_cu->cu->header.offset
8540                   + per_cu->cu->header.first_die_offset);
8541       abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8542       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8543                                    per_cu->cu->objfile->obfd,
8544                                    dwarf2_per_objfile->info.buffer, info_ptr,
8545                                    per_cu->cu);
8546       if (comp_unit_die.has_children)
8547         load_partial_dies (per_cu->cu->objfile->obfd,
8548                            dwarf2_per_objfile->info.buffer, info_ptr,
8549                            0, per_cu->cu);
8550       do_cleanups (back_to);
8551
8552       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8553     }
8554
8555  not_found:
8556
8557   if (pd == NULL)
8558     internal_error (__FILE__, __LINE__,
8559                     _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8560                     offset, bfd_get_filename (cu->objfile->obfd));
8561   return pd;
8562 }
8563
8564 /* Adjust PART_DIE before generating a symbol for it.  This function
8565    may set the is_external flag or change the DIE's name.  */
8566
8567 static void
8568 fixup_partial_die (struct partial_die_info *part_die,
8569                    struct dwarf2_cu *cu)
8570 {
8571   /* If we found a reference attribute and the DIE has no name, try
8572      to find a name in the referred to DIE.  */
8573
8574   if (part_die->name == NULL && part_die->has_specification)
8575     {
8576       struct partial_die_info *spec_die;
8577
8578       spec_die = find_partial_die (part_die->spec_offset, cu);
8579
8580       fixup_partial_die (spec_die, cu);
8581
8582       if (spec_die->name)
8583         {
8584           part_die->name = spec_die->name;
8585
8586           /* Copy DW_AT_external attribute if it is set.  */
8587           if (spec_die->is_external)
8588             part_die->is_external = spec_die->is_external;
8589         }
8590     }
8591
8592   /* Set default names for some unnamed DIEs.  */
8593   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
8594                                  || part_die->tag == DW_TAG_class_type))
8595     part_die->name = "(anonymous class)";
8596
8597   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
8598     part_die->name = "(anonymous namespace)";
8599
8600   if (part_die->tag == DW_TAG_structure_type
8601       || part_die->tag == DW_TAG_class_type
8602       || part_die->tag == DW_TAG_union_type)
8603     guess_structure_name (part_die, cu);
8604 }
8605
8606 /* Read an attribute value described by an attribute form.  */
8607
8608 static gdb_byte *
8609 read_attribute_value (struct attribute *attr, unsigned form,
8610                       bfd *abfd, gdb_byte *info_ptr,
8611                       struct dwarf2_cu *cu)
8612 {
8613   struct comp_unit_head *cu_header = &cu->header;
8614   unsigned int bytes_read;
8615   struct dwarf_block *blk;
8616
8617   attr->form = form;
8618   switch (form)
8619     {
8620     case DW_FORM_ref_addr:
8621       if (cu->header.version == 2)
8622         DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8623       else
8624         DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8625       info_ptr += bytes_read;
8626       break;
8627     case DW_FORM_addr:
8628       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
8629       info_ptr += bytes_read;
8630       break;
8631     case DW_FORM_block2:
8632       blk = dwarf_alloc_block (cu);
8633       blk->size = read_2_bytes (abfd, info_ptr);
8634       info_ptr += 2;
8635       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8636       info_ptr += blk->size;
8637       DW_BLOCK (attr) = blk;
8638       break;
8639     case DW_FORM_block4:
8640       blk = dwarf_alloc_block (cu);
8641       blk->size = read_4_bytes (abfd, info_ptr);
8642       info_ptr += 4;
8643       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8644       info_ptr += blk->size;
8645       DW_BLOCK (attr) = blk;
8646       break;
8647     case DW_FORM_data2:
8648       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
8649       info_ptr += 2;
8650       break;
8651     case DW_FORM_data4:
8652       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
8653       info_ptr += 4;
8654       break;
8655     case DW_FORM_data8:
8656       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
8657       info_ptr += 8;
8658       break;
8659     case DW_FORM_sec_offset:
8660       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
8661       info_ptr += bytes_read;
8662       break;
8663     case DW_FORM_string:
8664       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8665       DW_STRING_IS_CANONICAL (attr) = 0;
8666       info_ptr += bytes_read;
8667       break;
8668     case DW_FORM_strp:
8669       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
8670                                                &bytes_read);
8671       DW_STRING_IS_CANONICAL (attr) = 0;
8672       info_ptr += bytes_read;
8673       break;
8674     case DW_FORM_exprloc:
8675     case DW_FORM_block:
8676       blk = dwarf_alloc_block (cu);
8677       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8678       info_ptr += bytes_read;
8679       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8680       info_ptr += blk->size;
8681       DW_BLOCK (attr) = blk;
8682       break;
8683     case DW_FORM_block1:
8684       blk = dwarf_alloc_block (cu);
8685       blk->size = read_1_byte (abfd, info_ptr);
8686       info_ptr += 1;
8687       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
8688       info_ptr += blk->size;
8689       DW_BLOCK (attr) = blk;
8690       break;
8691     case DW_FORM_data1:
8692       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8693       info_ptr += 1;
8694       break;
8695     case DW_FORM_flag:
8696       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
8697       info_ptr += 1;
8698       break;
8699     case DW_FORM_flag_present:
8700       DW_UNSND (attr) = 1;
8701       break;
8702     case DW_FORM_sdata:
8703       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
8704       info_ptr += bytes_read;
8705       break;
8706     case DW_FORM_udata:
8707       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8708       info_ptr += bytes_read;
8709       break;
8710     case DW_FORM_ref1:
8711       DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
8712       info_ptr += 1;
8713       break;
8714     case DW_FORM_ref2:
8715       DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
8716       info_ptr += 2;
8717       break;
8718     case DW_FORM_ref4:
8719       DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
8720       info_ptr += 4;
8721       break;
8722     case DW_FORM_ref8:
8723       DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
8724       info_ptr += 8;
8725       break;
8726     case DW_FORM_sig8:
8727       /* Convert the signature to something we can record in DW_UNSND
8728          for later lookup.
8729          NOTE: This is NULL if the type wasn't found.  */
8730       DW_SIGNATURED_TYPE (attr) =
8731         lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
8732       info_ptr += 8;
8733       break;
8734     case DW_FORM_ref_udata:
8735       DW_ADDR (attr) = (cu->header.offset
8736                         + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
8737       info_ptr += bytes_read;
8738       break;
8739     case DW_FORM_indirect:
8740       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8741       info_ptr += bytes_read;
8742       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
8743       break;
8744     default:
8745       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
8746              dwarf_form_name (form),
8747              bfd_get_filename (abfd));
8748     }
8749
8750   /* We have seen instances where the compiler tried to emit a byte
8751      size attribute of -1 which ended up being encoded as an unsigned
8752      0xffffffff.  Although 0xffffffff is technically a valid size value,
8753      an object of this size seems pretty unlikely so we can relatively
8754      safely treat these cases as if the size attribute was invalid and
8755      treat them as zero by default.  */
8756   if (attr->name == DW_AT_byte_size
8757       && form == DW_FORM_data4
8758       && DW_UNSND (attr) >= 0xffffffff)
8759     {
8760       complaint
8761         (&symfile_complaints,
8762          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
8763          hex_string (DW_UNSND (attr)));
8764       DW_UNSND (attr) = 0;
8765     }
8766
8767   return info_ptr;
8768 }
8769
8770 /* Read an attribute described by an abbreviated attribute.  */
8771
8772 static gdb_byte *
8773 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
8774                 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
8775 {
8776   attr->name = abbrev->name;
8777   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
8778 }
8779
8780 /* read dwarf information from a buffer */
8781
8782 static unsigned int
8783 read_1_byte (bfd *abfd, gdb_byte *buf)
8784 {
8785   return bfd_get_8 (abfd, buf);
8786 }
8787
8788 static int
8789 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
8790 {
8791   return bfd_get_signed_8 (abfd, buf);
8792 }
8793
8794 static unsigned int
8795 read_2_bytes (bfd *abfd, gdb_byte *buf)
8796 {
8797   return bfd_get_16 (abfd, buf);
8798 }
8799
8800 static int
8801 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
8802 {
8803   return bfd_get_signed_16 (abfd, buf);
8804 }
8805
8806 static unsigned int
8807 read_4_bytes (bfd *abfd, gdb_byte *buf)
8808 {
8809   return bfd_get_32 (abfd, buf);
8810 }
8811
8812 static int
8813 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
8814 {
8815   return bfd_get_signed_32 (abfd, buf);
8816 }
8817
8818 static ULONGEST
8819 read_8_bytes (bfd *abfd, gdb_byte *buf)
8820 {
8821   return bfd_get_64 (abfd, buf);
8822 }
8823
8824 static CORE_ADDR
8825 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
8826               unsigned int *bytes_read)
8827 {
8828   struct comp_unit_head *cu_header = &cu->header;
8829   CORE_ADDR retval = 0;
8830
8831   if (cu_header->signed_addr_p)
8832     {
8833       switch (cu_header->addr_size)
8834         {
8835         case 2:
8836           retval = bfd_get_signed_16 (abfd, buf);
8837           break;
8838         case 4:
8839           retval = bfd_get_signed_32 (abfd, buf);
8840           break;
8841         case 8:
8842           retval = bfd_get_signed_64 (abfd, buf);
8843           break;
8844         default:
8845           internal_error (__FILE__, __LINE__,
8846                           _("read_address: bad switch, signed [in module %s]"),
8847                           bfd_get_filename (abfd));
8848         }
8849     }
8850   else
8851     {
8852       switch (cu_header->addr_size)
8853         {
8854         case 2:
8855           retval = bfd_get_16 (abfd, buf);
8856           break;
8857         case 4:
8858           retval = bfd_get_32 (abfd, buf);
8859           break;
8860         case 8:
8861           retval = bfd_get_64 (abfd, buf);
8862           break;
8863         default:
8864           internal_error (__FILE__, __LINE__,
8865                           _("read_address: bad switch, unsigned [in module %s]"),
8866                           bfd_get_filename (abfd));
8867         }
8868     }
8869
8870   *bytes_read = cu_header->addr_size;
8871   return retval;
8872 }
8873
8874 /* Read the initial length from a section.  The (draft) DWARF 3
8875    specification allows the initial length to take up either 4 bytes
8876    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
8877    bytes describe the length and all offsets will be 8 bytes in length
8878    instead of 4.
8879
8880    An older, non-standard 64-bit format is also handled by this
8881    function.  The older format in question stores the initial length
8882    as an 8-byte quantity without an escape value.  Lengths greater
8883    than 2^32 aren't very common which means that the initial 4 bytes
8884    is almost always zero.  Since a length value of zero doesn't make
8885    sense for the 32-bit format, this initial zero can be considered to
8886    be an escape value which indicates the presence of the older 64-bit
8887    format.  As written, the code can't detect (old format) lengths
8888    greater than 4GB.  If it becomes necessary to handle lengths
8889    somewhat larger than 4GB, we could allow other small values (such
8890    as the non-sensical values of 1, 2, and 3) to also be used as
8891    escape values indicating the presence of the old format.
8892
8893    The value returned via bytes_read should be used to increment the
8894    relevant pointer after calling read_initial_length().
8895
8896    [ Note:  read_initial_length() and read_offset() are based on the
8897      document entitled "DWARF Debugging Information Format", revision
8898      3, draft 8, dated November 19, 2001.  This document was obtained
8899      from:
8900
8901         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
8902
8903      This document is only a draft and is subject to change.  (So beware.)
8904
8905      Details regarding the older, non-standard 64-bit format were
8906      determined empirically by examining 64-bit ELF files produced by
8907      the SGI toolchain on an IRIX 6.5 machine.
8908
8909      - Kevin, July 16, 2002
8910    ] */
8911
8912 static LONGEST
8913 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
8914 {
8915   LONGEST length = bfd_get_32 (abfd, buf);
8916
8917   if (length == 0xffffffff)
8918     {
8919       length = bfd_get_64 (abfd, buf + 4);
8920       *bytes_read = 12;
8921     }
8922   else if (length == 0)
8923     {
8924       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
8925       length = bfd_get_64 (abfd, buf);
8926       *bytes_read = 8;
8927     }
8928   else
8929     {
8930       *bytes_read = 4;
8931     }
8932
8933   return length;
8934 }
8935
8936 /* Cover function for read_initial_length.
8937    Returns the length of the object at BUF, and stores the size of the
8938    initial length in *BYTES_READ and stores the size that offsets will be in
8939    *OFFSET_SIZE.
8940    If the initial length size is not equivalent to that specified in
8941    CU_HEADER then issue a complaint.
8942    This is useful when reading non-comp-unit headers.  */
8943
8944 static LONGEST
8945 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
8946                                         const struct comp_unit_head *cu_header,
8947                                         unsigned int *bytes_read,
8948                                         unsigned int *offset_size)
8949 {
8950   LONGEST length = read_initial_length (abfd, buf, bytes_read);
8951
8952   gdb_assert (cu_header->initial_length_size == 4
8953               || cu_header->initial_length_size == 8
8954               || cu_header->initial_length_size == 12);
8955
8956   if (cu_header->initial_length_size != *bytes_read)
8957     complaint (&symfile_complaints,
8958                _("intermixed 32-bit and 64-bit DWARF sections"));
8959
8960   *offset_size = (*bytes_read == 4) ? 4 : 8;
8961   return length;
8962 }
8963
8964 /* Read an offset from the data stream.  The size of the offset is
8965    given by cu_header->offset_size.  */
8966
8967 static LONGEST
8968 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
8969              unsigned int *bytes_read)
8970 {
8971   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
8972
8973   *bytes_read = cu_header->offset_size;
8974   return offset;
8975 }
8976
8977 /* Read an offset from the data stream.  */
8978
8979 static LONGEST
8980 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
8981 {
8982   LONGEST retval = 0;
8983
8984   switch (offset_size)
8985     {
8986     case 4:
8987       retval = bfd_get_32 (abfd, buf);
8988       break;
8989     case 8:
8990       retval = bfd_get_64 (abfd, buf);
8991       break;
8992     default:
8993       internal_error (__FILE__, __LINE__,
8994                       _("read_offset_1: bad switch [in module %s]"),
8995                       bfd_get_filename (abfd));
8996     }
8997
8998   return retval;
8999 }
9000
9001 static gdb_byte *
9002 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9003 {
9004   /* If the size of a host char is 8 bits, we can return a pointer
9005      to the buffer, otherwise we have to copy the data to a buffer
9006      allocated on the temporary obstack.  */
9007   gdb_assert (HOST_CHAR_BIT == 8);
9008   return buf;
9009 }
9010
9011 static char *
9012 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9013 {
9014   /* If the size of a host char is 8 bits, we can return a pointer
9015      to the string, otherwise we have to copy the string to a buffer
9016      allocated on the temporary obstack.  */
9017   gdb_assert (HOST_CHAR_BIT == 8);
9018   if (*buf == '\0')
9019     {
9020       *bytes_read_ptr = 1;
9021       return NULL;
9022     }
9023   *bytes_read_ptr = strlen ((char *) buf) + 1;
9024   return (char *) buf;
9025 }
9026
9027 static char *
9028 read_indirect_string (bfd *abfd, gdb_byte *buf,
9029                       const struct comp_unit_head *cu_header,
9030                       unsigned int *bytes_read_ptr)
9031 {
9032   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9033
9034   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9035   if (dwarf2_per_objfile->str.buffer == NULL)
9036     {
9037       error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9038                       bfd_get_filename (abfd));
9039       return NULL;
9040     }
9041   if (str_offset >= dwarf2_per_objfile->str.size)
9042     {
9043       error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9044                       bfd_get_filename (abfd));
9045       return NULL;
9046     }
9047   gdb_assert (HOST_CHAR_BIT == 8);
9048   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9049     return NULL;
9050   return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9051 }
9052
9053 static unsigned long
9054 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9055 {
9056   unsigned long result;
9057   unsigned int num_read;
9058   int i, shift;
9059   unsigned char byte;
9060
9061   result = 0;
9062   shift = 0;
9063   num_read = 0;
9064   i = 0;
9065   while (1)
9066     {
9067       byte = bfd_get_8 (abfd, buf);
9068       buf++;
9069       num_read++;
9070       result |= ((unsigned long)(byte & 127) << shift);
9071       if ((byte & 128) == 0)
9072         {
9073           break;
9074         }
9075       shift += 7;
9076     }
9077   *bytes_read_ptr = num_read;
9078   return result;
9079 }
9080
9081 static long
9082 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9083 {
9084   long result;
9085   int i, shift, num_read;
9086   unsigned char byte;
9087
9088   result = 0;
9089   shift = 0;
9090   num_read = 0;
9091   i = 0;
9092   while (1)
9093     {
9094       byte = bfd_get_8 (abfd, buf);
9095       buf++;
9096       num_read++;
9097       result |= ((long)(byte & 127) << shift);
9098       shift += 7;
9099       if ((byte & 128) == 0)
9100         {
9101           break;
9102         }
9103     }
9104   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9105     result |= -(((long)1) << shift);
9106   *bytes_read_ptr = num_read;
9107   return result;
9108 }
9109
9110 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
9111
9112 static gdb_byte *
9113 skip_leb128 (bfd *abfd, gdb_byte *buf)
9114 {
9115   int byte;
9116
9117   while (1)
9118     {
9119       byte = bfd_get_8 (abfd, buf);
9120       buf++;
9121       if ((byte & 128) == 0)
9122         return buf;
9123     }
9124 }
9125
9126 static void
9127 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9128 {
9129   switch (lang)
9130     {
9131     case DW_LANG_C89:
9132     case DW_LANG_C99:
9133     case DW_LANG_C:
9134       cu->language = language_c;
9135       break;
9136     case DW_LANG_C_plus_plus:
9137       cu->language = language_cplus;
9138       break;
9139     case DW_LANG_D:
9140       cu->language = language_d;
9141       break;
9142     case DW_LANG_Fortran77:
9143     case DW_LANG_Fortran90:
9144     case DW_LANG_Fortran95:
9145       cu->language = language_fortran;
9146       break;
9147     case DW_LANG_Mips_Assembler:
9148       cu->language = language_asm;
9149       break;
9150     case DW_LANG_Java:
9151       cu->language = language_java;
9152       break;
9153     case DW_LANG_Ada83:
9154     case DW_LANG_Ada95:
9155       cu->language = language_ada;
9156       break;
9157     case DW_LANG_Modula2:
9158       cu->language = language_m2;
9159       break;
9160     case DW_LANG_Pascal83:
9161       cu->language = language_pascal;
9162       break;
9163     case DW_LANG_ObjC:
9164       cu->language = language_objc;
9165       break;
9166     case DW_LANG_Cobol74:
9167     case DW_LANG_Cobol85:
9168     default:
9169       cu->language = language_minimal;
9170       break;
9171     }
9172   cu->language_defn = language_def (cu->language);
9173 }
9174
9175 /* Return the named attribute or NULL if not there.  */
9176
9177 static struct attribute *
9178 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9179 {
9180   unsigned int i;
9181   struct attribute *spec = NULL;
9182
9183   for (i = 0; i < die->num_attrs; ++i)
9184     {
9185       if (die->attrs[i].name == name)
9186         return &die->attrs[i];
9187       if (die->attrs[i].name == DW_AT_specification
9188           || die->attrs[i].name == DW_AT_abstract_origin)
9189         spec = &die->attrs[i];
9190     }
9191
9192   if (spec)
9193     {
9194       die = follow_die_ref (die, spec, &cu);
9195       return dwarf2_attr (die, name, cu);
9196     }
9197
9198   return NULL;
9199 }
9200
9201 /* Return the named attribute or NULL if not there,
9202    but do not follow DW_AT_specification, etc.
9203    This is for use in contexts where we're reading .debug_types dies.
9204    Following DW_AT_specification, DW_AT_abstract_origin will take us
9205    back up the chain, and we want to go down.  */
9206
9207 static struct attribute *
9208 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9209                        struct dwarf2_cu *cu)
9210 {
9211   unsigned int i;
9212
9213   for (i = 0; i < die->num_attrs; ++i)
9214     if (die->attrs[i].name == name)
9215       return &die->attrs[i];
9216
9217   return NULL;
9218 }
9219
9220 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9221    and holds a non-zero value.  This function should only be used for
9222    DW_FORM_flag or DW_FORM_flag_present attributes.  */
9223
9224 static int
9225 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9226 {
9227   struct attribute *attr = dwarf2_attr (die, name, cu);
9228
9229   return (attr && DW_UNSND (attr));
9230 }
9231
9232 static int
9233 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9234 {
9235   /* A DIE is a declaration if it has a DW_AT_declaration attribute
9236      which value is non-zero.  However, we have to be careful with
9237      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9238      (via dwarf2_flag_true_p) follows this attribute.  So we may
9239      end up accidently finding a declaration attribute that belongs
9240      to a different DIE referenced by the specification attribute,
9241      even though the given DIE does not have a declaration attribute.  */
9242   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9243           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9244 }
9245
9246 /* Return the die giving the specification for DIE, if there is
9247    one.  *SPEC_CU is the CU containing DIE on input, and the CU
9248    containing the return value on output.  If there is no
9249    specification, but there is an abstract origin, that is
9250    returned.  */
9251
9252 static struct die_info *
9253 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9254 {
9255   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9256                                              *spec_cu);
9257
9258   if (spec_attr == NULL)
9259     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9260
9261   if (spec_attr == NULL)
9262     return NULL;
9263   else
9264     return follow_die_ref (die, spec_attr, spec_cu);
9265 }
9266
9267 /* Free the line_header structure *LH, and any arrays and strings it
9268    refers to.  */
9269 static void
9270 free_line_header (struct line_header *lh)
9271 {
9272   if (lh->standard_opcode_lengths)
9273     xfree (lh->standard_opcode_lengths);
9274
9275   /* Remember that all the lh->file_names[i].name pointers are
9276      pointers into debug_line_buffer, and don't need to be freed.  */
9277   if (lh->file_names)
9278     xfree (lh->file_names);
9279
9280   /* Similarly for the include directory names.  */
9281   if (lh->include_dirs)
9282     xfree (lh->include_dirs);
9283
9284   xfree (lh);
9285 }
9286
9287
9288 /* Add an entry to LH's include directory table.  */
9289 static void
9290 add_include_dir (struct line_header *lh, char *include_dir)
9291 {
9292   /* Grow the array if necessary.  */
9293   if (lh->include_dirs_size == 0)
9294     {
9295       lh->include_dirs_size = 1; /* for testing */
9296       lh->include_dirs = xmalloc (lh->include_dirs_size
9297                                   * sizeof (*lh->include_dirs));
9298     }
9299   else if (lh->num_include_dirs >= lh->include_dirs_size)
9300     {
9301       lh->include_dirs_size *= 2;
9302       lh->include_dirs = xrealloc (lh->include_dirs,
9303                                    (lh->include_dirs_size
9304                                     * sizeof (*lh->include_dirs)));
9305     }
9306
9307   lh->include_dirs[lh->num_include_dirs++] = include_dir;
9308 }
9309
9310
9311 /* Add an entry to LH's file name table.  */
9312 static void
9313 add_file_name (struct line_header *lh,
9314                char *name,
9315                unsigned int dir_index,
9316                unsigned int mod_time,
9317                unsigned int length)
9318 {
9319   struct file_entry *fe;
9320
9321   /* Grow the array if necessary.  */
9322   if (lh->file_names_size == 0)
9323     {
9324       lh->file_names_size = 1; /* for testing */
9325       lh->file_names = xmalloc (lh->file_names_size
9326                                 * sizeof (*lh->file_names));
9327     }
9328   else if (lh->num_file_names >= lh->file_names_size)
9329     {
9330       lh->file_names_size *= 2;
9331       lh->file_names = xrealloc (lh->file_names,
9332                                  (lh->file_names_size
9333                                   * sizeof (*lh->file_names)));
9334     }
9335
9336   fe = &lh->file_names[lh->num_file_names++];
9337   fe->name = name;
9338   fe->dir_index = dir_index;
9339   fe->mod_time = mod_time;
9340   fe->length = length;
9341   fe->included_p = 0;
9342   fe->symtab = NULL;
9343 }
9344
9345
9346 /* Read the statement program header starting at OFFSET in
9347    .debug_line, according to the endianness of ABFD.  Return a pointer
9348    to a struct line_header, allocated using xmalloc.
9349
9350    NOTE: the strings in the include directory and file name tables of
9351    the returned object point into debug_line_buffer, and must not be
9352    freed.  */
9353 static struct line_header *
9354 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9355                           struct dwarf2_cu *cu)
9356 {
9357   struct cleanup *back_to;
9358   struct line_header *lh;
9359   gdb_byte *line_ptr;
9360   unsigned int bytes_read, offset_size;
9361   int i;
9362   char *cur_dir, *cur_file;
9363
9364   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9365   if (dwarf2_per_objfile->line.buffer == NULL)
9366     {
9367       complaint (&symfile_complaints, _("missing .debug_line section"));
9368       return 0;
9369     }
9370
9371   /* Make sure that at least there's room for the total_length field.
9372      That could be 12 bytes long, but we're just going to fudge that.  */
9373   if (offset + 4 >= dwarf2_per_objfile->line.size)
9374     {
9375       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9376       return 0;
9377     }
9378
9379   lh = xmalloc (sizeof (*lh));
9380   memset (lh, 0, sizeof (*lh));
9381   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9382                           (void *) lh);
9383
9384   line_ptr = dwarf2_per_objfile->line.buffer + offset;
9385
9386   /* Read in the header.  */
9387   lh->total_length =
9388     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9389                                             &bytes_read, &offset_size);
9390   line_ptr += bytes_read;
9391   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9392                                      + dwarf2_per_objfile->line.size))
9393     {
9394       dwarf2_statement_list_fits_in_line_number_section_complaint ();
9395       return 0;
9396     }
9397   lh->statement_program_end = line_ptr + lh->total_length;
9398   lh->version = read_2_bytes (abfd, line_ptr);
9399   line_ptr += 2;
9400   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9401   line_ptr += offset_size;
9402   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9403   line_ptr += 1;
9404   if (lh->version >= 4)
9405     {
9406       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9407       line_ptr += 1;
9408     }
9409   else
9410     lh->maximum_ops_per_instruction = 1;
9411
9412   if (lh->maximum_ops_per_instruction == 0)
9413     {
9414       lh->maximum_ops_per_instruction = 1;
9415       complaint (&symfile_complaints,
9416                  _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9417     }
9418
9419   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9420   line_ptr += 1;
9421   lh->line_base = read_1_signed_byte (abfd, line_ptr);
9422   line_ptr += 1;
9423   lh->line_range = read_1_byte (abfd, line_ptr);
9424   line_ptr += 1;
9425   lh->opcode_base = read_1_byte (abfd, line_ptr);
9426   line_ptr += 1;
9427   lh->standard_opcode_lengths
9428     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9429
9430   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
9431   for (i = 1; i < lh->opcode_base; ++i)
9432     {
9433       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9434       line_ptr += 1;
9435     }
9436
9437   /* Read directory table.  */
9438   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9439     {
9440       line_ptr += bytes_read;
9441       add_include_dir (lh, cur_dir);
9442     }
9443   line_ptr += bytes_read;
9444
9445   /* Read file name table.  */
9446   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9447     {
9448       unsigned int dir_index, mod_time, length;
9449
9450       line_ptr += bytes_read;
9451       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9452       line_ptr += bytes_read;
9453       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9454       line_ptr += bytes_read;
9455       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9456       line_ptr += bytes_read;
9457
9458       add_file_name (lh, cur_file, dir_index, mod_time, length);
9459     }
9460   line_ptr += bytes_read;
9461   lh->statement_program_start = line_ptr;
9462
9463   if (line_ptr > (dwarf2_per_objfile->line.buffer
9464                   + dwarf2_per_objfile->line.size))
9465     complaint (&symfile_complaints,
9466                _("line number info header doesn't fit in `.debug_line' section"));
9467
9468   discard_cleanups (back_to);
9469   return lh;
9470 }
9471
9472 /* This function exists to work around a bug in certain compilers
9473    (particularly GCC 2.95), in which the first line number marker of a
9474    function does not show up until after the prologue, right before
9475    the second line number marker.  This function shifts ADDRESS down
9476    to the beginning of the function if necessary, and is called on
9477    addresses passed to record_line.  */
9478
9479 static CORE_ADDR
9480 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9481 {
9482   struct function_range *fn;
9483
9484   /* Find the function_range containing address.  */
9485   if (!cu->first_fn)
9486     return address;
9487
9488   if (!cu->cached_fn)
9489     cu->cached_fn = cu->first_fn;
9490
9491   fn = cu->cached_fn;
9492   while (fn)
9493     if (fn->lowpc <= address && fn->highpc > address)
9494       goto found;
9495     else
9496       fn = fn->next;
9497
9498   fn = cu->first_fn;
9499   while (fn && fn != cu->cached_fn)
9500     if (fn->lowpc <= address && fn->highpc > address)
9501       goto found;
9502     else
9503       fn = fn->next;
9504
9505   return address;
9506
9507  found:
9508   if (fn->seen_line)
9509     return address;
9510   if (address != fn->lowpc)
9511     complaint (&symfile_complaints,
9512                _("misplaced first line number at 0x%lx for '%s'"),
9513                (unsigned long) address, fn->name);
9514   fn->seen_line = 1;
9515   return fn->lowpc;
9516 }
9517
9518 /* Decode the Line Number Program (LNP) for the given line_header
9519    structure and CU.  The actual information extracted and the type
9520    of structures created from the LNP depends on the value of PST.
9521
9522    1. If PST is NULL, then this procedure uses the data from the program
9523       to create all necessary symbol tables, and their linetables.
9524       The compilation directory of the file is passed in COMP_DIR,
9525       and must not be NULL.
9526
9527    2. If PST is not NULL, this procedure reads the program to determine
9528       the list of files included by the unit represented by PST, and
9529       builds all the associated partial symbol tables.  In this case,
9530       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
9531       is not used to compute the full name of the symtab, and therefore
9532       omitting it when building the partial symtab does not introduce
9533       the potential for inconsistency - a partial symtab and its associated
9534       symbtab having a different fullname -).  */
9535
9536 static void
9537 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
9538                     struct dwarf2_cu *cu, struct partial_symtab *pst)
9539 {
9540   gdb_byte *line_ptr, *extended_end;
9541   gdb_byte *line_end;
9542   unsigned int bytes_read, extended_len;
9543   unsigned char op_code, extended_op, adj_opcode;
9544   CORE_ADDR baseaddr;
9545   struct objfile *objfile = cu->objfile;
9546   struct gdbarch *gdbarch = get_objfile_arch (objfile);
9547   const int decode_for_pst_p = (pst != NULL);
9548   struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
9549
9550   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9551
9552   line_ptr = lh->statement_program_start;
9553   line_end = lh->statement_program_end;
9554
9555   /* Read the statement sequences until there's nothing left.  */
9556   while (line_ptr < line_end)
9557     {
9558       /* state machine registers  */
9559       CORE_ADDR address = 0;
9560       unsigned int file = 1;
9561       unsigned int line = 1;
9562       unsigned int column = 0;
9563       int is_stmt = lh->default_is_stmt;
9564       int basic_block = 0;
9565       int end_sequence = 0;
9566       CORE_ADDR addr;
9567       unsigned char op_index = 0;
9568
9569       if (!decode_for_pst_p && lh->num_file_names >= file)
9570         {
9571           /* Start a subfile for the current file of the state machine.  */
9572           /* lh->include_dirs and lh->file_names are 0-based, but the
9573              directory and file name numbers in the statement program
9574              are 1-based.  */
9575           struct file_entry *fe = &lh->file_names[file - 1];
9576           char *dir = NULL;
9577
9578           if (fe->dir_index)
9579             dir = lh->include_dirs[fe->dir_index - 1];
9580
9581           dwarf2_start_subfile (fe->name, dir, comp_dir);
9582         }
9583
9584       /* Decode the table.  */
9585       while (!end_sequence)
9586         {
9587           op_code = read_1_byte (abfd, line_ptr);
9588           line_ptr += 1;
9589           if (line_ptr > line_end)
9590             {
9591               dwarf2_debug_line_missing_end_sequence_complaint ();
9592               break;
9593             }
9594
9595           if (op_code >= lh->opcode_base)
9596             {
9597               /* Special operand.  */
9598               adj_opcode = op_code - lh->opcode_base;
9599               address += (((op_index + (adj_opcode / lh->line_range))
9600                            / lh->maximum_ops_per_instruction)
9601                           * lh->minimum_instruction_length);
9602               op_index = ((op_index + (adj_opcode / lh->line_range))
9603                           % lh->maximum_ops_per_instruction);
9604               line += lh->line_base + (adj_opcode % lh->line_range);
9605               if (lh->num_file_names < file || file == 0)
9606                 dwarf2_debug_line_missing_file_complaint ();
9607               /* For now we ignore lines not starting on an
9608                  instruction boundary.  */
9609               else if (op_index == 0)
9610                 {
9611                   lh->file_names[file - 1].included_p = 1;
9612                   if (!decode_for_pst_p && is_stmt)
9613                     {
9614                       if (last_subfile != current_subfile)
9615                         {
9616                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9617                           if (last_subfile)
9618                             record_line (last_subfile, 0, addr);
9619                           last_subfile = current_subfile;
9620                         }
9621                       /* Append row to matrix using current values.  */
9622                       addr = check_cu_functions (address, cu);
9623                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9624                       record_line (current_subfile, line, addr);
9625                     }
9626                 }
9627               basic_block = 0;
9628             }
9629           else switch (op_code)
9630             {
9631             case DW_LNS_extended_op:
9632               extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9633               line_ptr += bytes_read;
9634               extended_end = line_ptr + extended_len;
9635               extended_op = read_1_byte (abfd, line_ptr);
9636               line_ptr += 1;
9637               switch (extended_op)
9638                 {
9639                 case DW_LNE_end_sequence:
9640                   end_sequence = 1;
9641                   break;
9642                 case DW_LNE_set_address:
9643                   address = read_address (abfd, line_ptr, cu, &bytes_read);
9644                   op_index = 0;
9645                   line_ptr += bytes_read;
9646                   address += baseaddr;
9647                   break;
9648                 case DW_LNE_define_file:
9649                   {
9650                     char *cur_file;
9651                     unsigned int dir_index, mod_time, length;
9652
9653                     cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
9654                     line_ptr += bytes_read;
9655                     dir_index =
9656                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9657                     line_ptr += bytes_read;
9658                     mod_time =
9659                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9660                     line_ptr += bytes_read;
9661                     length =
9662                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9663                     line_ptr += bytes_read;
9664                     add_file_name (lh, cur_file, dir_index, mod_time, length);
9665                   }
9666                   break;
9667                 case DW_LNE_set_discriminator:
9668                   /* The discriminator is not interesting to the debugger;
9669                      just ignore it.  */
9670                   line_ptr = extended_end;
9671                   break;
9672                 default:
9673                   complaint (&symfile_complaints,
9674                              _("mangled .debug_line section"));
9675                   return;
9676                 }
9677               /* Make sure that we parsed the extended op correctly.  If e.g.
9678                  we expected a different address size than the producer used,
9679                  we may have read the wrong number of bytes.  */
9680               if (line_ptr != extended_end)
9681                 {
9682                   complaint (&symfile_complaints,
9683                              _("mangled .debug_line section"));
9684                   return;
9685                 }
9686               break;
9687             case DW_LNS_copy:
9688               if (lh->num_file_names < file || file == 0)
9689                 dwarf2_debug_line_missing_file_complaint ();
9690               else
9691                 {
9692                   lh->file_names[file - 1].included_p = 1;
9693                   if (!decode_for_pst_p && is_stmt)
9694                     {
9695                       if (last_subfile != current_subfile)
9696                         {
9697                           addr = gdbarch_addr_bits_remove (gdbarch, address);
9698                           if (last_subfile)
9699                             record_line (last_subfile, 0, addr);
9700                           last_subfile = current_subfile;
9701                         }
9702                       addr = check_cu_functions (address, cu);
9703                       addr = gdbarch_addr_bits_remove (gdbarch, addr);
9704                       record_line (current_subfile, line, addr);
9705                     }
9706                 }
9707               basic_block = 0;
9708               break;
9709             case DW_LNS_advance_pc:
9710               {
9711                 CORE_ADDR adjust
9712                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9713
9714                 address += (((op_index + adjust)
9715                              / lh->maximum_ops_per_instruction)
9716                             * lh->minimum_instruction_length);
9717                 op_index = ((op_index + adjust)
9718                             % lh->maximum_ops_per_instruction);
9719                 line_ptr += bytes_read;
9720               }
9721               break;
9722             case DW_LNS_advance_line:
9723               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
9724               line_ptr += bytes_read;
9725               break;
9726             case DW_LNS_set_file:
9727               {
9728                 /* The arrays lh->include_dirs and lh->file_names are
9729                    0-based, but the directory and file name numbers in
9730                    the statement program are 1-based.  */
9731                 struct file_entry *fe;
9732                 char *dir = NULL;
9733
9734                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9735                 line_ptr += bytes_read;
9736                 if (lh->num_file_names < file || file == 0)
9737                   dwarf2_debug_line_missing_file_complaint ();
9738                 else
9739                   {
9740                     fe = &lh->file_names[file - 1];
9741                     if (fe->dir_index)
9742                       dir = lh->include_dirs[fe->dir_index - 1];
9743                     if (!decode_for_pst_p)
9744                       {
9745                         last_subfile = current_subfile;
9746                         dwarf2_start_subfile (fe->name, dir, comp_dir);
9747                       }
9748                   }
9749               }
9750               break;
9751             case DW_LNS_set_column:
9752               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9753               line_ptr += bytes_read;
9754               break;
9755             case DW_LNS_negate_stmt:
9756               is_stmt = (!is_stmt);
9757               break;
9758             case DW_LNS_set_basic_block:
9759               basic_block = 1;
9760               break;
9761             /* Add to the address register of the state machine the
9762                address increment value corresponding to special opcode
9763                255.  I.e., this value is scaled by the minimum
9764                instruction length since special opcode 255 would have
9765                scaled the the increment.  */
9766             case DW_LNS_const_add_pc:
9767               {
9768                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
9769
9770                 address += (((op_index + adjust)
9771                              / lh->maximum_ops_per_instruction)
9772                             * lh->minimum_instruction_length);
9773                 op_index = ((op_index + adjust)
9774                             % lh->maximum_ops_per_instruction);
9775               }
9776               break;
9777             case DW_LNS_fixed_advance_pc:
9778               address += read_2_bytes (abfd, line_ptr);
9779               op_index = 0;
9780               line_ptr += 2;
9781               break;
9782             default:
9783               {
9784                 /* Unknown standard opcode, ignore it.  */
9785                 int i;
9786
9787                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9788                   {
9789                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9790                     line_ptr += bytes_read;
9791                   }
9792               }
9793             }
9794         }
9795       if (lh->num_file_names < file || file == 0)
9796         dwarf2_debug_line_missing_file_complaint ();
9797       else
9798         {
9799           lh->file_names[file - 1].included_p = 1;
9800           if (!decode_for_pst_p)
9801             {
9802               addr = gdbarch_addr_bits_remove (gdbarch, address);
9803               record_line (current_subfile, 0, addr);
9804             }
9805         }
9806     }
9807
9808   if (decode_for_pst_p)
9809     {
9810       int file_index;
9811
9812       /* Now that we're done scanning the Line Header Program, we can
9813          create the psymtab of each included file.  */
9814       for (file_index = 0; file_index < lh->num_file_names; file_index++)
9815         if (lh->file_names[file_index].included_p == 1)
9816           {
9817             const struct file_entry fe = lh->file_names [file_index];
9818             char *include_name = fe.name;
9819             char *dir_name = NULL;
9820             char *pst_filename = pst->filename;
9821
9822             if (fe.dir_index)
9823               dir_name = lh->include_dirs[fe.dir_index - 1];
9824
9825             if (!IS_ABSOLUTE_PATH (include_name) && dir_name != NULL)
9826               {
9827                 include_name = concat (dir_name, SLASH_STRING,
9828                                        include_name, (char *)NULL);
9829                 make_cleanup (xfree, include_name);
9830               }
9831
9832             if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9833               {
9834                 pst_filename = concat (pst->dirname, SLASH_STRING,
9835                                        pst_filename, (char *)NULL);
9836                 make_cleanup (xfree, pst_filename);
9837               }
9838
9839             if (strcmp (include_name, pst_filename) != 0)
9840               dwarf2_create_include_psymtab (include_name, pst, objfile);
9841           }
9842     }
9843   else
9844     {
9845       /* Make sure a symtab is created for every file, even files
9846          which contain only variables (i.e. no code with associated
9847          line numbers).  */
9848
9849       int i;
9850       struct file_entry *fe;
9851
9852       for (i = 0; i < lh->num_file_names; i++)
9853         {
9854           char *dir = NULL;
9855
9856           fe = &lh->file_names[i];
9857           if (fe->dir_index)
9858             dir = lh->include_dirs[fe->dir_index - 1];
9859           dwarf2_start_subfile (fe->name, dir, comp_dir);
9860
9861           /* Skip the main file; we don't need it, and it must be
9862              allocated last, so that it will show up before the
9863              non-primary symtabs in the objfile's symtab list.  */
9864           if (current_subfile == first_subfile)
9865             continue;
9866
9867           if (current_subfile->symtab == NULL)
9868             current_subfile->symtab = allocate_symtab (current_subfile->name,
9869                                                        cu->objfile);
9870           fe->symtab = current_subfile->symtab;
9871         }
9872     }
9873 }
9874
9875 /* Start a subfile for DWARF.  FILENAME is the name of the file and
9876    DIRNAME the name of the source directory which contains FILENAME
9877    or NULL if not known.  COMP_DIR is the compilation directory for the
9878    linetable's compilation unit or NULL if not known.
9879    This routine tries to keep line numbers from identical absolute and
9880    relative file names in a common subfile.
9881
9882    Using the `list' example from the GDB testsuite, which resides in
9883    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
9884    of /srcdir/list0.c yields the following debugging information for list0.c:
9885
9886    DW_AT_name:          /srcdir/list0.c
9887    DW_AT_comp_dir:              /compdir
9888    files.files[0].name: list0.h
9889    files.files[0].dir:  /srcdir
9890    files.files[1].name: list0.c
9891    files.files[1].dir:  /srcdir
9892
9893    The line number information for list0.c has to end up in a single
9894    subfile, so that `break /srcdir/list0.c:1' works as expected.
9895    start_subfile will ensure that this happens provided that we pass the
9896    concatenation of files.files[1].dir and files.files[1].name as the
9897    subfile's name.  */
9898
9899 static void
9900 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
9901 {
9902   char *fullname;
9903
9904   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
9905      `start_symtab' will always pass the contents of DW_AT_comp_dir as
9906      second argument to start_subfile.  To be consistent, we do the
9907      same here.  In order not to lose the line information directory,
9908      we concatenate it to the filename when it makes sense.
9909      Note that the Dwarf3 standard says (speaking of filenames in line
9910      information): ``The directory index is ignored for file names
9911      that represent full path names''.  Thus ignoring dirname in the
9912      `else' branch below isn't an issue.  */
9913
9914   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
9915     fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
9916   else
9917     fullname = filename;
9918
9919   start_subfile (fullname, comp_dir);
9920
9921   if (fullname != filename)
9922     xfree (fullname);
9923 }
9924
9925 static void
9926 var_decode_location (struct attribute *attr, struct symbol *sym,
9927                      struct dwarf2_cu *cu)
9928 {
9929   struct objfile *objfile = cu->objfile;
9930   struct comp_unit_head *cu_header = &cu->header;
9931
9932   /* NOTE drow/2003-01-30: There used to be a comment and some special
9933      code here to turn a symbol with DW_AT_external and a
9934      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
9935      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
9936      with some versions of binutils) where shared libraries could have
9937      relocations against symbols in their debug information - the
9938      minimal symbol would have the right address, but the debug info
9939      would not.  It's no longer necessary, because we will explicitly
9940      apply relocations when we read in the debug information now.  */
9941
9942   /* A DW_AT_location attribute with no contents indicates that a
9943      variable has been optimized away.  */
9944   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
9945     {
9946       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
9947       return;
9948     }
9949
9950   /* Handle one degenerate form of location expression specially, to
9951      preserve GDB's previous behavior when section offsets are
9952      specified.  If this is just a DW_OP_addr then mark this symbol
9953      as LOC_STATIC.  */
9954
9955   if (attr_form_is_block (attr)
9956       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
9957       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
9958     {
9959       unsigned int dummy;
9960
9961       SYMBOL_VALUE_ADDRESS (sym) =
9962         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
9963       SYMBOL_CLASS (sym) = LOC_STATIC;
9964       fixup_symbol_section (sym, objfile);
9965       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
9966                                               SYMBOL_SECTION (sym));
9967       return;
9968     }
9969
9970   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
9971      expression evaluator, and use LOC_COMPUTED only when necessary
9972      (i.e. when the value of a register or memory location is
9973      referenced, or a thread-local block, etc.).  Then again, it might
9974      not be worthwhile.  I'm assuming that it isn't unless performance
9975      or memory numbers show me otherwise.  */
9976
9977   dwarf2_symbol_mark_computed (attr, sym, cu);
9978   SYMBOL_CLASS (sym) = LOC_COMPUTED;
9979 }
9980
9981 /* Given a pointer to a DWARF information entry, figure out if we need
9982    to make a symbol table entry for it, and if so, create a new entry
9983    and return a pointer to it.
9984    If TYPE is NULL, determine symbol type from the die, otherwise
9985    used the passed type.
9986    If SPACE is not NULL, use it to hold the new symbol.  If it is
9987    NULL, allocate a new symbol on the objfile's obstack.  */
9988
9989 static struct symbol *
9990 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
9991                  struct symbol *space)
9992 {
9993   struct objfile *objfile = cu->objfile;
9994   struct symbol *sym = NULL;
9995   char *name;
9996   struct attribute *attr = NULL;
9997   struct attribute *attr2 = NULL;
9998   CORE_ADDR baseaddr;
9999   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10000
10001   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10002
10003   name = dwarf2_name (die, cu);
10004   if (name)
10005     {
10006       const char *linkagename;
10007       int suppress_add = 0;
10008
10009       if (space)
10010         sym = space;
10011       else
10012         sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10013       OBJSTAT (objfile, n_syms++);
10014
10015       /* Cache this symbol's name and the name's demangled form (if any).  */
10016       SYMBOL_LANGUAGE (sym) = cu->language;
10017       linkagename = dwarf2_physname (name, die, cu);
10018       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10019
10020       /* Fortran does not have mangling standard and the mangling does differ
10021          between gfortran, iFort etc.  */
10022       if (cu->language == language_fortran
10023           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10024         symbol_set_demangled_name (&(sym->ginfo),
10025                                    (char *) dwarf2_full_name (name, die, cu),
10026                                    NULL);
10027
10028       /* Default assumptions.
10029          Use the passed type or decode it from the die.  */
10030       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10031       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10032       if (type != NULL)
10033         SYMBOL_TYPE (sym) = type;
10034       else
10035         SYMBOL_TYPE (sym) = die_type (die, cu);
10036       attr = dwarf2_attr (die,
10037                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10038                           cu);
10039       if (attr)
10040         {
10041           SYMBOL_LINE (sym) = DW_UNSND (attr);
10042         }
10043
10044       attr = dwarf2_attr (die,
10045                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10046                           cu);
10047       if (attr)
10048         {
10049           int file_index = DW_UNSND (attr);
10050
10051           if (cu->line_header == NULL
10052               || file_index > cu->line_header->num_file_names)
10053             complaint (&symfile_complaints,
10054                        _("file index out of range"));
10055           else if (file_index > 0)
10056             {
10057               struct file_entry *fe;
10058
10059               fe = &cu->line_header->file_names[file_index - 1];
10060               SYMBOL_SYMTAB (sym) = fe->symtab;
10061             }
10062         }
10063
10064       switch (die->tag)
10065         {
10066         case DW_TAG_label:
10067           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10068           if (attr)
10069             {
10070               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10071             }
10072           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10073           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10074           SYMBOL_CLASS (sym) = LOC_LABEL;
10075           add_symbol_to_list (sym, cu->list_in_scope);
10076           break;
10077         case DW_TAG_subprogram:
10078           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10079              finish_block.  */
10080           SYMBOL_CLASS (sym) = LOC_BLOCK;
10081           attr2 = dwarf2_attr (die, DW_AT_external, cu);
10082           if ((attr2 && (DW_UNSND (attr2) != 0))
10083               || cu->language == language_ada)
10084             {
10085               /* Subprograms marked external are stored as a global symbol.
10086                  Ada subprograms, whether marked external or not, are always
10087                  stored as a global symbol, because we want to be able to
10088                  access them globally.  For instance, we want to be able
10089                  to break on a nested subprogram without having to
10090                  specify the context.  */
10091               add_symbol_to_list (sym, &global_symbols);
10092             }
10093           else
10094             {
10095               add_symbol_to_list (sym, cu->list_in_scope);
10096             }
10097           break;
10098         case DW_TAG_inlined_subroutine:
10099           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10100              finish_block.  */
10101           SYMBOL_CLASS (sym) = LOC_BLOCK;
10102           SYMBOL_INLINED (sym) = 1;
10103           /* Do not add the symbol to any lists.  It will be found via
10104              BLOCK_FUNCTION from the blockvector.  */
10105           break;
10106         case DW_TAG_template_value_param:
10107           suppress_add = 1;
10108           /* Fall through.  */
10109         case DW_TAG_variable:
10110         case DW_TAG_member:
10111           /* Compilation with minimal debug info may result in variables
10112              with missing type entries. Change the misleading `void' type
10113              to something sensible.  */
10114           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10115             SYMBOL_TYPE (sym)
10116               = objfile_type (objfile)->nodebug_data_symbol;
10117
10118           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10119           /* In the case of DW_TAG_member, we should only be called for
10120              static const members.  */
10121           if (die->tag == DW_TAG_member)
10122             {
10123               /* dwarf2_add_field uses die_is_declaration,
10124                  so we do the same.  */
10125               gdb_assert (die_is_declaration (die, cu));
10126               gdb_assert (attr);
10127             }
10128           if (attr)
10129             {
10130               dwarf2_const_value (attr, sym, cu);
10131               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10132               if (suppress_add)
10133                 {
10134                   sym->hash_next = objfile->template_symbols;
10135                   objfile->template_symbols = sym;
10136                 }
10137               else
10138                 {
10139                   if (attr2 && (DW_UNSND (attr2) != 0))
10140                     add_symbol_to_list (sym, &global_symbols);
10141                   else
10142                     add_symbol_to_list (sym, cu->list_in_scope);
10143                 }
10144               break;
10145             }
10146           attr = dwarf2_attr (die, DW_AT_location, cu);
10147           if (attr)
10148             {
10149               var_decode_location (attr, sym, cu);
10150               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10151               if (SYMBOL_CLASS (sym) == LOC_STATIC
10152                   && SYMBOL_VALUE_ADDRESS (sym) == 0
10153                   && !dwarf2_per_objfile->has_section_at_zero)
10154                 {
10155                   /* When a static variable is eliminated by the linker,
10156                      the corresponding debug information is not stripped
10157                      out, but the variable address is set to null;
10158                      do not add such variables into symbol table.  */
10159                 }
10160               else if (attr2 && (DW_UNSND (attr2) != 0))
10161                 {
10162                   struct pending **list_to_add;
10163
10164                   /* Workaround gfortran PR debug/40040 - it uses
10165                      DW_AT_location for variables in -fPIC libraries which may
10166                      get overriden by other libraries/executable and get
10167                      a different address.  Resolve it by the minimal symbol
10168                      which may come from inferior's executable using copy
10169                      relocation.  Make this workaround only for gfortran as for
10170                      other compilers GDB cannot guess the minimal symbol
10171                      Fortran mangling kind.  */
10172                   if (cu->language == language_fortran && die->parent
10173                       && die->parent->tag == DW_TAG_module
10174                       && cu->producer
10175                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10176                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10177
10178                   /* A variable with DW_AT_external is never static,
10179                      but it may be block-scoped.  */
10180                   list_to_add = (cu->list_in_scope == &file_symbols
10181                                  ? &global_symbols : cu->list_in_scope);
10182                   add_symbol_to_list (sym, list_to_add);
10183                 }
10184               else
10185                 add_symbol_to_list (sym, cu->list_in_scope);
10186             }
10187           else
10188             {
10189               /* We do not know the address of this symbol.
10190                  If it is an external symbol and we have type information
10191                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
10192                  The address of the variable will then be determined from
10193                  the minimal symbol table whenever the variable is
10194                  referenced.  */
10195               attr2 = dwarf2_attr (die, DW_AT_external, cu);
10196               if (attr2 && (DW_UNSND (attr2) != 0)
10197                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10198                 {
10199                   struct pending **list_to_add;
10200
10201                   /* A variable with DW_AT_external is never static, but it
10202                      may be block-scoped.  */
10203                   list_to_add = (cu->list_in_scope == &file_symbols
10204                                  ? &global_symbols : cu->list_in_scope);
10205
10206                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10207                   if (suppress_add)
10208                     {
10209                       sym->hash_next = objfile->template_symbols;
10210                       objfile->template_symbols = sym;
10211                     }
10212                   else
10213                     add_symbol_to_list (sym, list_to_add);
10214                 }
10215               else if (!die_is_declaration (die, cu))
10216                 {
10217                   /* Use the default LOC_OPTIMIZED_OUT class.  */
10218                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10219                   if (suppress_add)
10220                     {
10221                       sym->hash_next = objfile->template_symbols;
10222                       objfile->template_symbols = sym;
10223                     }
10224                   else
10225                     add_symbol_to_list (sym, cu->list_in_scope);
10226                 }
10227             }
10228           break;
10229         case DW_TAG_formal_parameter:
10230           /* If we are inside a function, mark this as an argument.  If
10231              not, we might be looking at an argument to an inlined function
10232              when we do not have enough information to show inlined frames;
10233              pretend it's a local variable in that case so that the user can
10234              still see it.  */
10235           if (context_stack_depth > 0
10236               && context_stack[context_stack_depth - 1].name != NULL)
10237             SYMBOL_IS_ARGUMENT (sym) = 1;
10238           attr = dwarf2_attr (die, DW_AT_location, cu);
10239           if (attr)
10240             {
10241               var_decode_location (attr, sym, cu);
10242             }
10243           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10244           if (attr)
10245             {
10246               dwarf2_const_value (attr, sym, cu);
10247             }
10248           attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10249           if (attr && DW_UNSND (attr))
10250             {
10251               struct type *ref_type;
10252
10253               ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10254               SYMBOL_TYPE (sym) = ref_type;
10255             }
10256
10257           add_symbol_to_list (sym, cu->list_in_scope);
10258           break;
10259         case DW_TAG_unspecified_parameters:
10260           /* From varargs functions; gdb doesn't seem to have any
10261              interest in this information, so just ignore it for now.
10262              (FIXME?) */
10263           break;
10264         case DW_TAG_template_type_param:
10265           suppress_add = 1;
10266           /* Fall through.  */
10267         case DW_TAG_class_type:
10268         case DW_TAG_interface_type:
10269         case DW_TAG_structure_type:
10270         case DW_TAG_union_type:
10271         case DW_TAG_set_type:
10272         case DW_TAG_enumeration_type:
10273           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10274           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10275
10276           {
10277             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10278                really ever be static objects: otherwise, if you try
10279                to, say, break of a class's method and you're in a file
10280                which doesn't mention that class, it won't work unless
10281                the check for all static symbols in lookup_symbol_aux
10282                saves you.  See the OtherFileClass tests in
10283                gdb.c++/namespace.exp.  */
10284
10285             if (suppress_add)
10286               {
10287                 sym->hash_next = objfile->template_symbols;
10288                 objfile->template_symbols = sym;
10289               }
10290             else
10291               {
10292                 struct pending **list_to_add;
10293
10294                 list_to_add = (cu->list_in_scope == &file_symbols
10295                                && (cu->language == language_cplus
10296                                    || cu->language == language_java)
10297                                ? &global_symbols : cu->list_in_scope);
10298
10299                 add_symbol_to_list (sym, list_to_add);
10300               }
10301
10302             /* The semantics of C++ state that "struct foo { ... }" also
10303                defines a typedef for "foo".  A Java class declaration also
10304                defines a typedef for the class.  */
10305             if (cu->language == language_cplus
10306                 || cu->language == language_java
10307                 || cu->language == language_ada)
10308               {
10309                 /* The symbol's name is already allocated along with
10310                    this objfile, so we don't need to duplicate it for
10311                    the type.  */
10312                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10313                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10314               }
10315           }
10316           break;
10317         case DW_TAG_typedef:
10318           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10319           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10320           add_symbol_to_list (sym, cu->list_in_scope);
10321           break;
10322         case DW_TAG_base_type:
10323         case DW_TAG_subrange_type:
10324           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10325           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10326           add_symbol_to_list (sym, cu->list_in_scope);
10327           break;
10328         case DW_TAG_enumerator:
10329           attr = dwarf2_attr (die, DW_AT_const_value, cu);
10330           if (attr)
10331             {
10332               dwarf2_const_value (attr, sym, cu);
10333             }
10334           {
10335             /* NOTE: carlton/2003-11-10: See comment above in the
10336                DW_TAG_class_type, etc. block.  */
10337
10338             struct pending **list_to_add;
10339
10340             list_to_add = (cu->list_in_scope == &file_symbols
10341                            && (cu->language == language_cplus
10342                                || cu->language == language_java)
10343                            ? &global_symbols : cu->list_in_scope);
10344
10345             add_symbol_to_list (sym, list_to_add);
10346           }
10347           break;
10348         case DW_TAG_namespace:
10349           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10350           add_symbol_to_list (sym, &global_symbols);
10351           break;
10352         default:
10353           /* Not a tag we recognize.  Hopefully we aren't processing
10354              trash data, but since we must specifically ignore things
10355              we don't recognize, there is nothing else we should do at
10356              this point. */
10357           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10358                      dwarf_tag_name (die->tag));
10359           break;
10360         }
10361
10362       /* For the benefit of old versions of GCC, check for anonymous
10363          namespaces based on the demangled name.  */
10364       if (!processing_has_namespace_info
10365           && cu->language == language_cplus)
10366         cp_scan_for_anonymous_namespaces (sym);
10367     }
10368   return (sym);
10369 }
10370
10371 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
10372
10373 static struct symbol *
10374 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10375 {
10376   return new_symbol_full (die, type, cu, NULL);
10377 }
10378
10379 /* Copy constant value from an attribute to a symbol.  */
10380
10381 static void
10382 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10383                     struct dwarf2_cu *cu)
10384 {
10385   struct objfile *objfile = cu->objfile;
10386   struct comp_unit_head *cu_header = &cu->header;
10387   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10388                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10389   struct dwarf_block *blk;
10390
10391   switch (attr->form)
10392     {
10393     case DW_FORM_addr:
10394       {
10395         struct dwarf2_locexpr_baton *baton;
10396         gdb_byte *data;
10397
10398         if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
10399           dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10400                                                         cu_header->addr_size,
10401                                                         TYPE_LENGTH (SYMBOL_TYPE
10402                                                                      (sym)));
10403         /* Symbols of this form are reasonably rare, so we just
10404            piggyback on the existing location code rather than writing
10405            a new implementation of symbol_computed_ops.  */
10406         baton = obstack_alloc (&objfile->objfile_obstack,
10407                                sizeof (struct dwarf2_locexpr_baton));
10408         baton->per_cu = cu->per_cu;
10409         gdb_assert (baton->per_cu);
10410
10411         baton->size = 2 + cu_header->addr_size;
10412         data = obstack_alloc (&objfile->objfile_obstack, baton->size);
10413         baton->data = data;
10414
10415         data[0] = DW_OP_addr;
10416         store_unsigned_integer (&data[1], cu_header->addr_size,
10417                                 byte_order, DW_ADDR (attr));
10418         data[cu_header->addr_size + 1] = DW_OP_stack_value;
10419
10420         SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10421         SYMBOL_LOCATION_BATON (sym) = baton;
10422         SYMBOL_CLASS (sym) = LOC_COMPUTED;
10423       }
10424       break;
10425     case DW_FORM_string:
10426     case DW_FORM_strp:
10427       /* DW_STRING is already allocated on the obstack, point directly
10428          to it.  */
10429       SYMBOL_VALUE_BYTES (sym) = (gdb_byte *) DW_STRING (attr);
10430       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10431       break;
10432     case DW_FORM_block1:
10433     case DW_FORM_block2:
10434     case DW_FORM_block4:
10435     case DW_FORM_block:
10436     case DW_FORM_exprloc:
10437       blk = DW_BLOCK (attr);
10438       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
10439         dwarf2_const_value_length_mismatch_complaint (SYMBOL_PRINT_NAME (sym),
10440                                                       blk->size,
10441                                                       TYPE_LENGTH (SYMBOL_TYPE
10442                                                                    (sym)));
10443       SYMBOL_VALUE_BYTES (sym) =
10444         obstack_alloc (&objfile->objfile_obstack, blk->size);
10445       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
10446       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
10447       break;
10448
10449       /* The DW_AT_const_value attributes are supposed to carry the
10450          symbol's value "represented as it would be on the target
10451          architecture."  By the time we get here, it's already been
10452          converted to host endianness, so we just need to sign- or
10453          zero-extend it as appropriate.  */
10454     case DW_FORM_data1:
10455       dwarf2_const_value_data (attr, sym, 8);
10456       break;
10457     case DW_FORM_data2:
10458       dwarf2_const_value_data (attr, sym, 16);
10459       break;
10460     case DW_FORM_data4:
10461       dwarf2_const_value_data (attr, sym, 32);
10462       break;
10463     case DW_FORM_data8:
10464       dwarf2_const_value_data (attr, sym, 64);
10465       break;
10466
10467     case DW_FORM_sdata:
10468       SYMBOL_VALUE (sym) = DW_SND (attr);
10469       SYMBOL_CLASS (sym) = LOC_CONST;
10470       break;
10471
10472     case DW_FORM_udata:
10473       SYMBOL_VALUE (sym) = DW_UNSND (attr);
10474       SYMBOL_CLASS (sym) = LOC_CONST;
10475       break;
10476
10477     default:
10478       complaint (&symfile_complaints,
10479                  _("unsupported const value attribute form: '%s'"),
10480                  dwarf_form_name (attr->form));
10481       SYMBOL_VALUE (sym) = 0;
10482       SYMBOL_CLASS (sym) = LOC_CONST;
10483       break;
10484     }
10485 }
10486
10487
10488 /* Given an attr with a DW_FORM_dataN value in host byte order,
10489    zero-extend it as appropriate for the symbol's type.  The DWARF
10490    standard (v4) is not entirely clear about the meaning of using
10491    DW_FORM_dataN for a constant with a signed type, where the type is
10492    wider than the data.  The conclusion of a discussion on the DWARF
10493    list was that this is unspecified.  We choose to always zero-extend
10494    because that is the interpretation long in use by GCC.  */
10495 static void
10496 dwarf2_const_value_data (struct attribute *attr,
10497                          struct symbol *sym,
10498                          int bits)
10499 {
10500   LONGEST l = DW_UNSND (attr);
10501
10502   if (bits < sizeof (l) * 8)
10503     l &= ((LONGEST) 1 << bits) - 1;
10504
10505   SYMBOL_VALUE (sym) = l;
10506   SYMBOL_CLASS (sym) = LOC_CONST;
10507 }
10508
10509
10510 /* Return the type of the die in question using its DW_AT_type attribute.  */
10511
10512 static struct type *
10513 die_type (struct die_info *die, struct dwarf2_cu *cu)
10514 {
10515   struct attribute *type_attr;
10516   struct die_info *type_die;
10517
10518   type_attr = dwarf2_attr (die, DW_AT_type, cu);
10519   if (!type_attr)
10520     {
10521       /* A missing DW_AT_type represents a void type.  */
10522       return objfile_type (cu->objfile)->builtin_void;
10523     }
10524
10525   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10526
10527   return tag_type_to_type (type_die, cu);
10528 }
10529
10530 /* True iff CU's producer generates GNAT Ada auxiliary information
10531    that allows to find parallel types through that information instead
10532    of having to do expensive parallel lookups by type name.  */
10533
10534 static int
10535 need_gnat_info (struct dwarf2_cu *cu)
10536 {
10537   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
10538      of GNAT produces this auxiliary information, without any indication
10539      that it is produced.  Part of enhancing the FSF version of GNAT
10540      to produce that information will be to put in place an indicator
10541      that we can use in order to determine whether the descriptive type
10542      info is available or not.  One suggestion that has been made is
10543      to use a new attribute, attached to the CU die.  For now, assume
10544      that the descriptive type info is not available.  */
10545   return 0;
10546 }
10547
10548
10549 /* Return the auxiliary type of the die in question using its
10550    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
10551    attribute is not present.  */
10552
10553 static struct type *
10554 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
10555 {
10556   struct attribute *type_attr;
10557   struct die_info *type_die;
10558
10559   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
10560   if (!type_attr)
10561     return NULL;
10562
10563   type_die = follow_die_ref (die, type_attr, &cu);
10564   return tag_type_to_type (type_die, cu);
10565 }
10566
10567 /* If DIE has a descriptive_type attribute, then set the TYPE's
10568    descriptive type accordingly.  */
10569
10570 static void
10571 set_descriptive_type (struct type *type, struct die_info *die,
10572                       struct dwarf2_cu *cu)
10573 {
10574   struct type *descriptive_type = die_descriptive_type (die, cu);
10575
10576   if (descriptive_type)
10577     {
10578       ALLOCATE_GNAT_AUX_TYPE (type);
10579       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
10580     }
10581 }
10582
10583 /* Return the containing type of the die in question using its
10584    DW_AT_containing_type attribute.  */
10585
10586 static struct type *
10587 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
10588 {
10589   struct attribute *type_attr;
10590   struct die_info *type_die;
10591
10592   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
10593   if (!type_attr)
10594     error (_("Dwarf Error: Problem turning containing type into gdb type "
10595              "[in module %s]"), cu->objfile->name);
10596
10597   type_die = follow_die_ref_or_sig (die, type_attr, &cu);
10598   return tag_type_to_type (type_die, cu);
10599 }
10600
10601 static struct type *
10602 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
10603 {
10604   struct type *this_type;
10605
10606   this_type = read_type_die (die, cu);
10607   if (!this_type)
10608     {
10609       char *message, *saved;
10610
10611       /* read_type_die already issued a complaint.  */
10612       message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
10613                             cu->objfile->name,
10614                             cu->header.offset,
10615                             die->offset);
10616       saved = obstack_copy0 (&cu->objfile->objfile_obstack,
10617                              message, strlen (message));
10618       xfree (message);
10619
10620       this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
10621     }
10622   return this_type;
10623 }
10624
10625 static struct type *
10626 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
10627 {
10628   struct type *this_type;
10629
10630   this_type = get_die_type (die, cu);
10631   if (this_type)
10632     return this_type;
10633
10634   switch (die->tag)
10635     {
10636     case DW_TAG_class_type:
10637     case DW_TAG_interface_type:
10638     case DW_TAG_structure_type:
10639     case DW_TAG_union_type:
10640       this_type = read_structure_type (die, cu);
10641       break;
10642     case DW_TAG_enumeration_type:
10643       this_type = read_enumeration_type (die, cu);
10644       break;
10645     case DW_TAG_subprogram:
10646     case DW_TAG_subroutine_type:
10647     case DW_TAG_inlined_subroutine:
10648       this_type = read_subroutine_type (die, cu);
10649       break;
10650     case DW_TAG_array_type:
10651       this_type = read_array_type (die, cu);
10652       break;
10653     case DW_TAG_set_type:
10654       this_type = read_set_type (die, cu);
10655       break;
10656     case DW_TAG_pointer_type:
10657       this_type = read_tag_pointer_type (die, cu);
10658       break;
10659     case DW_TAG_ptr_to_member_type:
10660       this_type = read_tag_ptr_to_member_type (die, cu);
10661       break;
10662     case DW_TAG_reference_type:
10663       this_type = read_tag_reference_type (die, cu);
10664       break;
10665     case DW_TAG_const_type:
10666       this_type = read_tag_const_type (die, cu);
10667       break;
10668     case DW_TAG_volatile_type:
10669       this_type = read_tag_volatile_type (die, cu);
10670       break;
10671     case DW_TAG_string_type:
10672       this_type = read_tag_string_type (die, cu);
10673       break;
10674     case DW_TAG_typedef:
10675       this_type = read_typedef (die, cu);
10676       break;
10677     case DW_TAG_subrange_type:
10678       this_type = read_subrange_type (die, cu);
10679       break;
10680     case DW_TAG_base_type:
10681       this_type = read_base_type (die, cu);
10682       break;
10683     case DW_TAG_unspecified_type:
10684       this_type = read_unspecified_type (die, cu);
10685       break;
10686     case DW_TAG_namespace:
10687       this_type = read_namespace_type (die, cu);
10688       break;
10689     case DW_TAG_module:
10690       this_type = read_module_type (die, cu);
10691       break;
10692     default:
10693       complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
10694                  dwarf_tag_name (die->tag));
10695       break;
10696     }
10697
10698   return this_type;
10699 }
10700
10701 /* Return the name of the namespace/class that DIE is defined within,
10702    or "" if we can't tell.  The caller should not xfree the result.
10703
10704    For example, if we're within the method foo() in the following
10705    code:
10706
10707    namespace N {
10708      class C {
10709        void foo () {
10710        }
10711      };
10712    }
10713
10714    then determine_prefix on foo's die will return "N::C".  */
10715
10716 static char *
10717 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
10718 {
10719   struct die_info *parent, *spec_die;
10720   struct dwarf2_cu *spec_cu;
10721   struct type *parent_type;
10722
10723   if (cu->language != language_cplus && cu->language != language_java
10724       && cu->language != language_fortran)
10725     return "";
10726
10727   /* We have to be careful in the presence of DW_AT_specification.
10728      For example, with GCC 3.4, given the code
10729
10730      namespace N {
10731        void foo() {
10732          // Definition of N::foo.
10733        }
10734      }
10735
10736      then we'll have a tree of DIEs like this:
10737
10738      1: DW_TAG_compile_unit
10739        2: DW_TAG_namespace        // N
10740          3: DW_TAG_subprogram     // declaration of N::foo
10741        4: DW_TAG_subprogram       // definition of N::foo
10742             DW_AT_specification   // refers to die #3
10743
10744      Thus, when processing die #4, we have to pretend that we're in
10745      the context of its DW_AT_specification, namely the contex of die
10746      #3.  */
10747   spec_cu = cu;
10748   spec_die = die_specification (die, &spec_cu);
10749   if (spec_die == NULL)
10750     parent = die->parent;
10751   else
10752     {
10753       parent = spec_die->parent;
10754       cu = spec_cu;
10755     }
10756
10757   if (parent == NULL)
10758     return "";
10759   else
10760     switch (parent->tag)
10761       {
10762       case DW_TAG_namespace:
10763         parent_type = read_type_die (parent, cu);
10764         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
10765            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
10766            Work around this problem here.  */
10767         if (cu->language == language_cplus
10768             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
10769           return "";
10770         /* We give a name to even anonymous namespaces.  */
10771         return TYPE_TAG_NAME (parent_type);
10772       case DW_TAG_class_type:
10773       case DW_TAG_interface_type:
10774       case DW_TAG_structure_type:
10775       case DW_TAG_union_type:
10776       case DW_TAG_module:
10777         parent_type = read_type_die (parent, cu);
10778         if (TYPE_TAG_NAME (parent_type) != NULL)
10779           return TYPE_TAG_NAME (parent_type);
10780         else
10781           /* An anonymous structure is only allowed non-static data
10782              members; no typedefs, no member functions, et cetera.
10783              So it does not need a prefix.  */
10784           return "";
10785       default:
10786         return determine_prefix (parent, cu);
10787       }
10788 }
10789
10790 /* Return a newly-allocated string formed by concatenating PREFIX and
10791    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
10792    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
10793    perform an obconcat, otherwise allocate storage for the result.  The CU argument
10794    is used to determine the language and hence, the appropriate separator.  */
10795
10796 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
10797
10798 static char *
10799 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
10800                  int physname, struct dwarf2_cu *cu)
10801 {
10802   const char *lead = "";
10803   const char *sep;
10804
10805   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
10806     sep = "";
10807   else if (cu->language == language_java)
10808     sep = ".";
10809   else if (cu->language == language_fortran && physname)
10810     {
10811       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
10812          DW_AT_MIPS_linkage_name is preferred and used instead.  */
10813
10814       lead = "__";
10815       sep = "_MOD_";
10816     }
10817   else
10818     sep = "::";
10819
10820   if (prefix == NULL)
10821     prefix = "";
10822   if (suffix == NULL)
10823     suffix = "";
10824
10825   if (obs == NULL)
10826     {
10827       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
10828
10829       strcpy (retval, lead);
10830       strcat (retval, prefix);
10831       strcat (retval, sep);
10832       strcat (retval, suffix);
10833       return retval;
10834     }
10835   else
10836     {
10837       /* We have an obstack.  */
10838       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
10839     }
10840 }
10841
10842 /* Return sibling of die, NULL if no sibling.  */
10843
10844 static struct die_info *
10845 sibling_die (struct die_info *die)
10846 {
10847   return die->sibling;
10848 }
10849
10850 /* Get name of a die, return NULL if not found.  */
10851
10852 static char *
10853 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
10854                           struct obstack *obstack)
10855 {
10856   if (name && cu->language == language_cplus)
10857     {
10858       char *canon_name = cp_canonicalize_string (name);
10859
10860       if (canon_name != NULL)
10861         {
10862           if (strcmp (canon_name, name) != 0)
10863             name = obsavestring (canon_name, strlen (canon_name),
10864                                  obstack);
10865           xfree (canon_name);
10866         }
10867     }
10868
10869   return name;
10870 }
10871
10872 /* Get name of a die, return NULL if not found.  */
10873
10874 static char *
10875 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
10876 {
10877   struct attribute *attr;
10878
10879   attr = dwarf2_attr (die, DW_AT_name, cu);
10880   if (!attr || !DW_STRING (attr))
10881     return NULL;
10882
10883   switch (die->tag)
10884     {
10885     case DW_TAG_compile_unit:
10886       /* Compilation units have a DW_AT_name that is a filename, not
10887          a source language identifier.  */
10888     case DW_TAG_enumeration_type:
10889     case DW_TAG_enumerator:
10890       /* These tags always have simple identifiers already; no need
10891          to canonicalize them.  */
10892       return DW_STRING (attr);
10893
10894     case DW_TAG_subprogram:
10895       /* Java constructors will all be named "<init>", so return
10896          the class name when we see this special case.  */
10897       if (cu->language == language_java
10898           && DW_STRING (attr) != NULL
10899           && strcmp (DW_STRING (attr), "<init>") == 0)
10900         {
10901           struct dwarf2_cu *spec_cu = cu;
10902           struct die_info *spec_die;
10903
10904           /* GCJ will output '<init>' for Java constructor names.
10905              For this special case, return the name of the parent class.  */
10906
10907           /* GCJ may output suprogram DIEs with AT_specification set.
10908              If so, use the name of the specified DIE.  */
10909           spec_die = die_specification (die, &spec_cu);
10910           if (spec_die != NULL)
10911             return dwarf2_name (spec_die, spec_cu);
10912
10913           do
10914             {
10915               die = die->parent;
10916               if (die->tag == DW_TAG_class_type)
10917                 return dwarf2_name (die, cu);
10918             }
10919           while (die->tag != DW_TAG_compile_unit);
10920         }
10921       break;
10922
10923     case DW_TAG_class_type:
10924     case DW_TAG_interface_type:
10925     case DW_TAG_structure_type:
10926     case DW_TAG_union_type:
10927       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
10928          structures or unions.  These were of the form "._%d" in GCC 4.1,
10929          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
10930          and GCC 4.4.  We work around this problem by ignoring these.  */
10931       if (strncmp (DW_STRING (attr), "._", 2) == 0
10932           || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
10933         return NULL;
10934       break;
10935
10936     default:
10937       break;
10938     }
10939
10940   if (!DW_STRING_IS_CANONICAL (attr))
10941     {
10942       DW_STRING (attr)
10943         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
10944                                     &cu->objfile->objfile_obstack);
10945       DW_STRING_IS_CANONICAL (attr) = 1;
10946     }
10947   return DW_STRING (attr);
10948 }
10949
10950 /* Return the die that this die in an extension of, or NULL if there
10951    is none.  *EXT_CU is the CU containing DIE on input, and the CU
10952    containing the return value on output.  */
10953
10954 static struct die_info *
10955 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
10956 {
10957   struct attribute *attr;
10958
10959   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
10960   if (attr == NULL)
10961     return NULL;
10962
10963   return follow_die_ref (die, attr, ext_cu);
10964 }
10965
10966 /* Convert a DIE tag into its string name.  */
10967
10968 static char *
10969 dwarf_tag_name (unsigned tag)
10970 {
10971   switch (tag)
10972     {
10973     case DW_TAG_padding:
10974       return "DW_TAG_padding";
10975     case DW_TAG_array_type:
10976       return "DW_TAG_array_type";
10977     case DW_TAG_class_type:
10978       return "DW_TAG_class_type";
10979     case DW_TAG_entry_point:
10980       return "DW_TAG_entry_point";
10981     case DW_TAG_enumeration_type:
10982       return "DW_TAG_enumeration_type";
10983     case DW_TAG_formal_parameter:
10984       return "DW_TAG_formal_parameter";
10985     case DW_TAG_imported_declaration:
10986       return "DW_TAG_imported_declaration";
10987     case DW_TAG_label:
10988       return "DW_TAG_label";
10989     case DW_TAG_lexical_block:
10990       return "DW_TAG_lexical_block";
10991     case DW_TAG_member:
10992       return "DW_TAG_member";
10993     case DW_TAG_pointer_type:
10994       return "DW_TAG_pointer_type";
10995     case DW_TAG_reference_type:
10996       return "DW_TAG_reference_type";
10997     case DW_TAG_compile_unit:
10998       return "DW_TAG_compile_unit";
10999     case DW_TAG_string_type:
11000       return "DW_TAG_string_type";
11001     case DW_TAG_structure_type:
11002       return "DW_TAG_structure_type";
11003     case DW_TAG_subroutine_type:
11004       return "DW_TAG_subroutine_type";
11005     case DW_TAG_typedef:
11006       return "DW_TAG_typedef";
11007     case DW_TAG_union_type:
11008       return "DW_TAG_union_type";
11009     case DW_TAG_unspecified_parameters:
11010       return "DW_TAG_unspecified_parameters";
11011     case DW_TAG_variant:
11012       return "DW_TAG_variant";
11013     case DW_TAG_common_block:
11014       return "DW_TAG_common_block";
11015     case DW_TAG_common_inclusion:
11016       return "DW_TAG_common_inclusion";
11017     case DW_TAG_inheritance:
11018       return "DW_TAG_inheritance";
11019     case DW_TAG_inlined_subroutine:
11020       return "DW_TAG_inlined_subroutine";
11021     case DW_TAG_module:
11022       return "DW_TAG_module";
11023     case DW_TAG_ptr_to_member_type:
11024       return "DW_TAG_ptr_to_member_type";
11025     case DW_TAG_set_type:
11026       return "DW_TAG_set_type";
11027     case DW_TAG_subrange_type:
11028       return "DW_TAG_subrange_type";
11029     case DW_TAG_with_stmt:
11030       return "DW_TAG_with_stmt";
11031     case DW_TAG_access_declaration:
11032       return "DW_TAG_access_declaration";
11033     case DW_TAG_base_type:
11034       return "DW_TAG_base_type";
11035     case DW_TAG_catch_block:
11036       return "DW_TAG_catch_block";
11037     case DW_TAG_const_type:
11038       return "DW_TAG_const_type";
11039     case DW_TAG_constant:
11040       return "DW_TAG_constant";
11041     case DW_TAG_enumerator:
11042       return "DW_TAG_enumerator";
11043     case DW_TAG_file_type:
11044       return "DW_TAG_file_type";
11045     case DW_TAG_friend:
11046       return "DW_TAG_friend";
11047     case DW_TAG_namelist:
11048       return "DW_TAG_namelist";
11049     case DW_TAG_namelist_item:
11050       return "DW_TAG_namelist_item";
11051     case DW_TAG_packed_type:
11052       return "DW_TAG_packed_type";
11053     case DW_TAG_subprogram:
11054       return "DW_TAG_subprogram";
11055     case DW_TAG_template_type_param:
11056       return "DW_TAG_template_type_param";
11057     case DW_TAG_template_value_param:
11058       return "DW_TAG_template_value_param";
11059     case DW_TAG_thrown_type:
11060       return "DW_TAG_thrown_type";
11061     case DW_TAG_try_block:
11062       return "DW_TAG_try_block";
11063     case DW_TAG_variant_part:
11064       return "DW_TAG_variant_part";
11065     case DW_TAG_variable:
11066       return "DW_TAG_variable";
11067     case DW_TAG_volatile_type:
11068       return "DW_TAG_volatile_type";
11069     case DW_TAG_dwarf_procedure:
11070       return "DW_TAG_dwarf_procedure";
11071     case DW_TAG_restrict_type:
11072       return "DW_TAG_restrict_type";
11073     case DW_TAG_interface_type:
11074       return "DW_TAG_interface_type";
11075     case DW_TAG_namespace:
11076       return "DW_TAG_namespace";
11077     case DW_TAG_imported_module:
11078       return "DW_TAG_imported_module";
11079     case DW_TAG_unspecified_type:
11080       return "DW_TAG_unspecified_type";
11081     case DW_TAG_partial_unit:
11082       return "DW_TAG_partial_unit";
11083     case DW_TAG_imported_unit:
11084       return "DW_TAG_imported_unit";
11085     case DW_TAG_condition:
11086       return "DW_TAG_condition";
11087     case DW_TAG_shared_type:
11088       return "DW_TAG_shared_type";
11089     case DW_TAG_type_unit:
11090       return "DW_TAG_type_unit";
11091     case DW_TAG_MIPS_loop:
11092       return "DW_TAG_MIPS_loop";
11093     case DW_TAG_HP_array_descriptor:
11094       return "DW_TAG_HP_array_descriptor";
11095     case DW_TAG_format_label:
11096       return "DW_TAG_format_label";
11097     case DW_TAG_function_template:
11098       return "DW_TAG_function_template";
11099     case DW_TAG_class_template:
11100       return "DW_TAG_class_template";
11101     case DW_TAG_GNU_BINCL:
11102       return "DW_TAG_GNU_BINCL";
11103     case DW_TAG_GNU_EINCL:
11104       return "DW_TAG_GNU_EINCL";
11105     case DW_TAG_upc_shared_type:
11106       return "DW_TAG_upc_shared_type";
11107     case DW_TAG_upc_strict_type:
11108       return "DW_TAG_upc_strict_type";
11109     case DW_TAG_upc_relaxed_type:
11110       return "DW_TAG_upc_relaxed_type";
11111     case DW_TAG_PGI_kanji_type:
11112       return "DW_TAG_PGI_kanji_type";
11113     case DW_TAG_PGI_interface_block:
11114       return "DW_TAG_PGI_interface_block";
11115     default:
11116       return "DW_TAG_<unknown>";
11117     }
11118 }
11119
11120 /* Convert a DWARF attribute code into its string name.  */
11121
11122 static char *
11123 dwarf_attr_name (unsigned attr)
11124 {
11125   switch (attr)
11126     {
11127     case DW_AT_sibling:
11128       return "DW_AT_sibling";
11129     case DW_AT_location:
11130       return "DW_AT_location";
11131     case DW_AT_name:
11132       return "DW_AT_name";
11133     case DW_AT_ordering:
11134       return "DW_AT_ordering";
11135     case DW_AT_subscr_data:
11136       return "DW_AT_subscr_data";
11137     case DW_AT_byte_size:
11138       return "DW_AT_byte_size";
11139     case DW_AT_bit_offset:
11140       return "DW_AT_bit_offset";
11141     case DW_AT_bit_size:
11142       return "DW_AT_bit_size";
11143     case DW_AT_element_list:
11144       return "DW_AT_element_list";
11145     case DW_AT_stmt_list:
11146       return "DW_AT_stmt_list";
11147     case DW_AT_low_pc:
11148       return "DW_AT_low_pc";
11149     case DW_AT_high_pc:
11150       return "DW_AT_high_pc";
11151     case DW_AT_language:
11152       return "DW_AT_language";
11153     case DW_AT_member:
11154       return "DW_AT_member";
11155     case DW_AT_discr:
11156       return "DW_AT_discr";
11157     case DW_AT_discr_value:
11158       return "DW_AT_discr_value";
11159     case DW_AT_visibility:
11160       return "DW_AT_visibility";
11161     case DW_AT_import:
11162       return "DW_AT_import";
11163     case DW_AT_string_length:
11164       return "DW_AT_string_length";
11165     case DW_AT_common_reference:
11166       return "DW_AT_common_reference";
11167     case DW_AT_comp_dir:
11168       return "DW_AT_comp_dir";
11169     case DW_AT_const_value:
11170       return "DW_AT_const_value";
11171     case DW_AT_containing_type:
11172       return "DW_AT_containing_type";
11173     case DW_AT_default_value:
11174       return "DW_AT_default_value";
11175     case DW_AT_inline:
11176       return "DW_AT_inline";
11177     case DW_AT_is_optional:
11178       return "DW_AT_is_optional";
11179     case DW_AT_lower_bound:
11180       return "DW_AT_lower_bound";
11181     case DW_AT_producer:
11182       return "DW_AT_producer";
11183     case DW_AT_prototyped:
11184       return "DW_AT_prototyped";
11185     case DW_AT_return_addr:
11186       return "DW_AT_return_addr";
11187     case DW_AT_start_scope:
11188       return "DW_AT_start_scope";
11189     case DW_AT_bit_stride:
11190       return "DW_AT_bit_stride";
11191     case DW_AT_upper_bound:
11192       return "DW_AT_upper_bound";
11193     case DW_AT_abstract_origin:
11194       return "DW_AT_abstract_origin";
11195     case DW_AT_accessibility:
11196       return "DW_AT_accessibility";
11197     case DW_AT_address_class:
11198       return "DW_AT_address_class";
11199     case DW_AT_artificial:
11200       return "DW_AT_artificial";
11201     case DW_AT_base_types:
11202       return "DW_AT_base_types";
11203     case DW_AT_calling_convention:
11204       return "DW_AT_calling_convention";
11205     case DW_AT_count:
11206       return "DW_AT_count";
11207     case DW_AT_data_member_location:
11208       return "DW_AT_data_member_location";
11209     case DW_AT_decl_column:
11210       return "DW_AT_decl_column";
11211     case DW_AT_decl_file:
11212       return "DW_AT_decl_file";
11213     case DW_AT_decl_line:
11214       return "DW_AT_decl_line";
11215     case DW_AT_declaration:
11216       return "DW_AT_declaration";
11217     case DW_AT_discr_list:
11218       return "DW_AT_discr_list";
11219     case DW_AT_encoding:
11220       return "DW_AT_encoding";
11221     case DW_AT_external:
11222       return "DW_AT_external";
11223     case DW_AT_frame_base:
11224       return "DW_AT_frame_base";
11225     case DW_AT_friend:
11226       return "DW_AT_friend";
11227     case DW_AT_identifier_case:
11228       return "DW_AT_identifier_case";
11229     case DW_AT_macro_info:
11230       return "DW_AT_macro_info";
11231     case DW_AT_namelist_items:
11232       return "DW_AT_namelist_items";
11233     case DW_AT_priority:
11234       return "DW_AT_priority";
11235     case DW_AT_segment:
11236       return "DW_AT_segment";
11237     case DW_AT_specification:
11238       return "DW_AT_specification";
11239     case DW_AT_static_link:
11240       return "DW_AT_static_link";
11241     case DW_AT_type:
11242       return "DW_AT_type";
11243     case DW_AT_use_location:
11244       return "DW_AT_use_location";
11245     case DW_AT_variable_parameter:
11246       return "DW_AT_variable_parameter";
11247     case DW_AT_virtuality:
11248       return "DW_AT_virtuality";
11249     case DW_AT_vtable_elem_location:
11250       return "DW_AT_vtable_elem_location";
11251     /* DWARF 3 values.  */
11252     case DW_AT_allocated:
11253       return "DW_AT_allocated";
11254     case DW_AT_associated:
11255       return "DW_AT_associated";
11256     case DW_AT_data_location:
11257       return "DW_AT_data_location";
11258     case DW_AT_byte_stride:
11259       return "DW_AT_byte_stride";
11260     case DW_AT_entry_pc:
11261       return "DW_AT_entry_pc";
11262     case DW_AT_use_UTF8:
11263       return "DW_AT_use_UTF8";
11264     case DW_AT_extension:
11265       return "DW_AT_extension";
11266     case DW_AT_ranges:
11267       return "DW_AT_ranges";
11268     case DW_AT_trampoline:
11269       return "DW_AT_trampoline";
11270     case DW_AT_call_column:
11271       return "DW_AT_call_column";
11272     case DW_AT_call_file:
11273       return "DW_AT_call_file";
11274     case DW_AT_call_line:
11275       return "DW_AT_call_line";
11276     case DW_AT_description:
11277       return "DW_AT_description";
11278     case DW_AT_binary_scale:
11279       return "DW_AT_binary_scale";
11280     case DW_AT_decimal_scale:
11281       return "DW_AT_decimal_scale";
11282     case DW_AT_small:
11283       return "DW_AT_small";
11284     case DW_AT_decimal_sign:
11285       return "DW_AT_decimal_sign";
11286     case DW_AT_digit_count:
11287       return "DW_AT_digit_count";
11288     case DW_AT_picture_string:
11289       return "DW_AT_picture_string";
11290     case DW_AT_mutable:
11291       return "DW_AT_mutable";
11292     case DW_AT_threads_scaled:
11293       return "DW_AT_threads_scaled";
11294     case DW_AT_explicit:
11295       return "DW_AT_explicit";
11296     case DW_AT_object_pointer:
11297       return "DW_AT_object_pointer";
11298     case DW_AT_endianity:
11299       return "DW_AT_endianity";
11300     case DW_AT_elemental:
11301       return "DW_AT_elemental";
11302     case DW_AT_pure:
11303       return "DW_AT_pure";
11304     case DW_AT_recursive:
11305       return "DW_AT_recursive";
11306     /* DWARF 4 values.  */
11307     case DW_AT_signature:
11308       return "DW_AT_signature";
11309     case DW_AT_linkage_name:
11310       return "DW_AT_linkage_name";
11311     /* SGI/MIPS extensions.  */
11312 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11313     case DW_AT_MIPS_fde:
11314       return "DW_AT_MIPS_fde";
11315 #endif
11316     case DW_AT_MIPS_loop_begin:
11317       return "DW_AT_MIPS_loop_begin";
11318     case DW_AT_MIPS_tail_loop_begin:
11319       return "DW_AT_MIPS_tail_loop_begin";
11320     case DW_AT_MIPS_epilog_begin:
11321       return "DW_AT_MIPS_epilog_begin";
11322     case DW_AT_MIPS_loop_unroll_factor:
11323       return "DW_AT_MIPS_loop_unroll_factor";
11324     case DW_AT_MIPS_software_pipeline_depth:
11325       return "DW_AT_MIPS_software_pipeline_depth";
11326     case DW_AT_MIPS_linkage_name:
11327       return "DW_AT_MIPS_linkage_name";
11328     case DW_AT_MIPS_stride:
11329       return "DW_AT_MIPS_stride";
11330     case DW_AT_MIPS_abstract_name:
11331       return "DW_AT_MIPS_abstract_name";
11332     case DW_AT_MIPS_clone_origin:
11333       return "DW_AT_MIPS_clone_origin";
11334     case DW_AT_MIPS_has_inlines:
11335       return "DW_AT_MIPS_has_inlines";
11336     /* HP extensions.  */
11337 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11338     case DW_AT_HP_block_index:
11339       return "DW_AT_HP_block_index";
11340 #endif
11341     case DW_AT_HP_unmodifiable:
11342       return "DW_AT_HP_unmodifiable";
11343     case DW_AT_HP_actuals_stmt_list:
11344       return "DW_AT_HP_actuals_stmt_list";
11345     case DW_AT_HP_proc_per_section:
11346       return "DW_AT_HP_proc_per_section";
11347     case DW_AT_HP_raw_data_ptr:
11348       return "DW_AT_HP_raw_data_ptr";
11349     case DW_AT_HP_pass_by_reference:
11350       return "DW_AT_HP_pass_by_reference";
11351     case DW_AT_HP_opt_level:
11352       return "DW_AT_HP_opt_level";
11353     case DW_AT_HP_prof_version_id:
11354       return "DW_AT_HP_prof_version_id";
11355     case DW_AT_HP_opt_flags:
11356       return "DW_AT_HP_opt_flags";
11357     case DW_AT_HP_cold_region_low_pc:
11358       return "DW_AT_HP_cold_region_low_pc";
11359     case DW_AT_HP_cold_region_high_pc:
11360       return "DW_AT_HP_cold_region_high_pc";
11361     case DW_AT_HP_all_variables_modifiable:
11362       return "DW_AT_HP_all_variables_modifiable";
11363     case DW_AT_HP_linkage_name:
11364       return "DW_AT_HP_linkage_name";
11365     case DW_AT_HP_prof_flags:
11366       return "DW_AT_HP_prof_flags";
11367     /* GNU extensions.  */
11368     case DW_AT_sf_names:
11369       return "DW_AT_sf_names";
11370     case DW_AT_src_info:
11371       return "DW_AT_src_info";
11372     case DW_AT_mac_info:
11373       return "DW_AT_mac_info";
11374     case DW_AT_src_coords:
11375       return "DW_AT_src_coords";
11376     case DW_AT_body_begin:
11377       return "DW_AT_body_begin";
11378     case DW_AT_body_end:
11379       return "DW_AT_body_end";
11380     case DW_AT_GNU_vector:
11381       return "DW_AT_GNU_vector";
11382     case DW_AT_GNU_odr_signature:
11383       return "DW_AT_GNU_odr_signature";
11384     /* VMS extensions.  */
11385     case DW_AT_VMS_rtnbeg_pd_address:
11386       return "DW_AT_VMS_rtnbeg_pd_address";
11387     /* UPC extension.  */
11388     case DW_AT_upc_threads_scaled:
11389       return "DW_AT_upc_threads_scaled";
11390     /* PGI (STMicroelectronics) extensions.  */
11391     case DW_AT_PGI_lbase:
11392       return "DW_AT_PGI_lbase";
11393     case DW_AT_PGI_soffset:
11394       return "DW_AT_PGI_soffset";
11395     case DW_AT_PGI_lstride:
11396       return "DW_AT_PGI_lstride";
11397     default:
11398       return "DW_AT_<unknown>";
11399     }
11400 }
11401
11402 /* Convert a DWARF value form code into its string name.  */
11403
11404 static char *
11405 dwarf_form_name (unsigned form)
11406 {
11407   switch (form)
11408     {
11409     case DW_FORM_addr:
11410       return "DW_FORM_addr";
11411     case DW_FORM_block2:
11412       return "DW_FORM_block2";
11413     case DW_FORM_block4:
11414       return "DW_FORM_block4";
11415     case DW_FORM_data2:
11416       return "DW_FORM_data2";
11417     case DW_FORM_data4:
11418       return "DW_FORM_data4";
11419     case DW_FORM_data8:
11420       return "DW_FORM_data8";
11421     case DW_FORM_string:
11422       return "DW_FORM_string";
11423     case DW_FORM_block:
11424       return "DW_FORM_block";
11425     case DW_FORM_block1:
11426       return "DW_FORM_block1";
11427     case DW_FORM_data1:
11428       return "DW_FORM_data1";
11429     case DW_FORM_flag:
11430       return "DW_FORM_flag";
11431     case DW_FORM_sdata:
11432       return "DW_FORM_sdata";
11433     case DW_FORM_strp:
11434       return "DW_FORM_strp";
11435     case DW_FORM_udata:
11436       return "DW_FORM_udata";
11437     case DW_FORM_ref_addr:
11438       return "DW_FORM_ref_addr";
11439     case DW_FORM_ref1:
11440       return "DW_FORM_ref1";
11441     case DW_FORM_ref2:
11442       return "DW_FORM_ref2";
11443     case DW_FORM_ref4:
11444       return "DW_FORM_ref4";
11445     case DW_FORM_ref8:
11446       return "DW_FORM_ref8";
11447     case DW_FORM_ref_udata:
11448       return "DW_FORM_ref_udata";
11449     case DW_FORM_indirect:
11450       return "DW_FORM_indirect";
11451     case DW_FORM_sec_offset:
11452       return "DW_FORM_sec_offset";
11453     case DW_FORM_exprloc:
11454       return "DW_FORM_exprloc";
11455     case DW_FORM_flag_present:
11456       return "DW_FORM_flag_present";
11457     case DW_FORM_sig8:
11458       return "DW_FORM_sig8";
11459     default:
11460       return "DW_FORM_<unknown>";
11461     }
11462 }
11463
11464 /* Convert a DWARF stack opcode into its string name.  */
11465
11466 const char *
11467 dwarf_stack_op_name (unsigned op, int def)
11468 {
11469   switch (op)
11470     {
11471     case DW_OP_addr:
11472       return "DW_OP_addr";
11473     case DW_OP_deref:
11474       return "DW_OP_deref";
11475     case DW_OP_const1u:
11476       return "DW_OP_const1u";
11477     case DW_OP_const1s:
11478       return "DW_OP_const1s";
11479     case DW_OP_const2u:
11480       return "DW_OP_const2u";
11481     case DW_OP_const2s:
11482       return "DW_OP_const2s";
11483     case DW_OP_const4u:
11484       return "DW_OP_const4u";
11485     case DW_OP_const4s:
11486       return "DW_OP_const4s";
11487     case DW_OP_const8u:
11488       return "DW_OP_const8u";
11489     case DW_OP_const8s:
11490       return "DW_OP_const8s";
11491     case DW_OP_constu:
11492       return "DW_OP_constu";
11493     case DW_OP_consts:
11494       return "DW_OP_consts";
11495     case DW_OP_dup:
11496       return "DW_OP_dup";
11497     case DW_OP_drop:
11498       return "DW_OP_drop";
11499     case DW_OP_over:
11500       return "DW_OP_over";
11501     case DW_OP_pick:
11502       return "DW_OP_pick";
11503     case DW_OP_swap:
11504       return "DW_OP_swap";
11505     case DW_OP_rot:
11506       return "DW_OP_rot";
11507     case DW_OP_xderef:
11508       return "DW_OP_xderef";
11509     case DW_OP_abs:
11510       return "DW_OP_abs";
11511     case DW_OP_and:
11512       return "DW_OP_and";
11513     case DW_OP_div:
11514       return "DW_OP_div";
11515     case DW_OP_minus:
11516       return "DW_OP_minus";
11517     case DW_OP_mod:
11518       return "DW_OP_mod";
11519     case DW_OP_mul:
11520       return "DW_OP_mul";
11521     case DW_OP_neg:
11522       return "DW_OP_neg";
11523     case DW_OP_not:
11524       return "DW_OP_not";
11525     case DW_OP_or:
11526       return "DW_OP_or";
11527     case DW_OP_plus:
11528       return "DW_OP_plus";
11529     case DW_OP_plus_uconst:
11530       return "DW_OP_plus_uconst";
11531     case DW_OP_shl:
11532       return "DW_OP_shl";
11533     case DW_OP_shr:
11534       return "DW_OP_shr";
11535     case DW_OP_shra:
11536       return "DW_OP_shra";
11537     case DW_OP_xor:
11538       return "DW_OP_xor";
11539     case DW_OP_bra:
11540       return "DW_OP_bra";
11541     case DW_OP_eq:
11542       return "DW_OP_eq";
11543     case DW_OP_ge:
11544       return "DW_OP_ge";
11545     case DW_OP_gt:
11546       return "DW_OP_gt";
11547     case DW_OP_le:
11548       return "DW_OP_le";
11549     case DW_OP_lt:
11550       return "DW_OP_lt";
11551     case DW_OP_ne:
11552       return "DW_OP_ne";
11553     case DW_OP_skip:
11554       return "DW_OP_skip";
11555     case DW_OP_lit0:
11556       return "DW_OP_lit0";
11557     case DW_OP_lit1:
11558       return "DW_OP_lit1";
11559     case DW_OP_lit2:
11560       return "DW_OP_lit2";
11561     case DW_OP_lit3:
11562       return "DW_OP_lit3";
11563     case DW_OP_lit4:
11564       return "DW_OP_lit4";
11565     case DW_OP_lit5:
11566       return "DW_OP_lit5";
11567     case DW_OP_lit6:
11568       return "DW_OP_lit6";
11569     case DW_OP_lit7:
11570       return "DW_OP_lit7";
11571     case DW_OP_lit8:
11572       return "DW_OP_lit8";
11573     case DW_OP_lit9:
11574       return "DW_OP_lit9";
11575     case DW_OP_lit10:
11576       return "DW_OP_lit10";
11577     case DW_OP_lit11:
11578       return "DW_OP_lit11";
11579     case DW_OP_lit12:
11580       return "DW_OP_lit12";
11581     case DW_OP_lit13:
11582       return "DW_OP_lit13";
11583     case DW_OP_lit14:
11584       return "DW_OP_lit14";
11585     case DW_OP_lit15:
11586       return "DW_OP_lit15";
11587     case DW_OP_lit16:
11588       return "DW_OP_lit16";
11589     case DW_OP_lit17:
11590       return "DW_OP_lit17";
11591     case DW_OP_lit18:
11592       return "DW_OP_lit18";
11593     case DW_OP_lit19:
11594       return "DW_OP_lit19";
11595     case DW_OP_lit20:
11596       return "DW_OP_lit20";
11597     case DW_OP_lit21:
11598       return "DW_OP_lit21";
11599     case DW_OP_lit22:
11600       return "DW_OP_lit22";
11601     case DW_OP_lit23:
11602       return "DW_OP_lit23";
11603     case DW_OP_lit24:
11604       return "DW_OP_lit24";
11605     case DW_OP_lit25:
11606       return "DW_OP_lit25";
11607     case DW_OP_lit26:
11608       return "DW_OP_lit26";
11609     case DW_OP_lit27:
11610       return "DW_OP_lit27";
11611     case DW_OP_lit28:
11612       return "DW_OP_lit28";
11613     case DW_OP_lit29:
11614       return "DW_OP_lit29";
11615     case DW_OP_lit30:
11616       return "DW_OP_lit30";
11617     case DW_OP_lit31:
11618       return "DW_OP_lit31";
11619     case DW_OP_reg0:
11620       return "DW_OP_reg0";
11621     case DW_OP_reg1:
11622       return "DW_OP_reg1";
11623     case DW_OP_reg2:
11624       return "DW_OP_reg2";
11625     case DW_OP_reg3:
11626       return "DW_OP_reg3";
11627     case DW_OP_reg4:
11628       return "DW_OP_reg4";
11629     case DW_OP_reg5:
11630       return "DW_OP_reg5";
11631     case DW_OP_reg6:
11632       return "DW_OP_reg6";
11633     case DW_OP_reg7:
11634       return "DW_OP_reg7";
11635     case DW_OP_reg8:
11636       return "DW_OP_reg8";
11637     case DW_OP_reg9:
11638       return "DW_OP_reg9";
11639     case DW_OP_reg10:
11640       return "DW_OP_reg10";
11641     case DW_OP_reg11:
11642       return "DW_OP_reg11";
11643     case DW_OP_reg12:
11644       return "DW_OP_reg12";
11645     case DW_OP_reg13:
11646       return "DW_OP_reg13";
11647     case DW_OP_reg14:
11648       return "DW_OP_reg14";
11649     case DW_OP_reg15:
11650       return "DW_OP_reg15";
11651     case DW_OP_reg16:
11652       return "DW_OP_reg16";
11653     case DW_OP_reg17:
11654       return "DW_OP_reg17";
11655     case DW_OP_reg18:
11656       return "DW_OP_reg18";
11657     case DW_OP_reg19:
11658       return "DW_OP_reg19";
11659     case DW_OP_reg20:
11660       return "DW_OP_reg20";
11661     case DW_OP_reg21:
11662       return "DW_OP_reg21";
11663     case DW_OP_reg22:
11664       return "DW_OP_reg22";
11665     case DW_OP_reg23:
11666       return "DW_OP_reg23";
11667     case DW_OP_reg24:
11668       return "DW_OP_reg24";
11669     case DW_OP_reg25:
11670       return "DW_OP_reg25";
11671     case DW_OP_reg26:
11672       return "DW_OP_reg26";
11673     case DW_OP_reg27:
11674       return "DW_OP_reg27";
11675     case DW_OP_reg28:
11676       return "DW_OP_reg28";
11677     case DW_OP_reg29:
11678       return "DW_OP_reg29";
11679     case DW_OP_reg30:
11680       return "DW_OP_reg30";
11681     case DW_OP_reg31:
11682       return "DW_OP_reg31";
11683     case DW_OP_breg0:
11684       return "DW_OP_breg0";
11685     case DW_OP_breg1:
11686       return "DW_OP_breg1";
11687     case DW_OP_breg2:
11688       return "DW_OP_breg2";
11689     case DW_OP_breg3:
11690       return "DW_OP_breg3";
11691     case DW_OP_breg4:
11692       return "DW_OP_breg4";
11693     case DW_OP_breg5:
11694       return "DW_OP_breg5";
11695     case DW_OP_breg6:
11696       return "DW_OP_breg6";
11697     case DW_OP_breg7:
11698       return "DW_OP_breg7";
11699     case DW_OP_breg8:
11700       return "DW_OP_breg8";
11701     case DW_OP_breg9:
11702       return "DW_OP_breg9";
11703     case DW_OP_breg10:
11704       return "DW_OP_breg10";
11705     case DW_OP_breg11:
11706       return "DW_OP_breg11";
11707     case DW_OP_breg12:
11708       return "DW_OP_breg12";
11709     case DW_OP_breg13:
11710       return "DW_OP_breg13";
11711     case DW_OP_breg14:
11712       return "DW_OP_breg14";
11713     case DW_OP_breg15:
11714       return "DW_OP_breg15";
11715     case DW_OP_breg16:
11716       return "DW_OP_breg16";
11717     case DW_OP_breg17:
11718       return "DW_OP_breg17";
11719     case DW_OP_breg18:
11720       return "DW_OP_breg18";
11721     case DW_OP_breg19:
11722       return "DW_OP_breg19";
11723     case DW_OP_breg20:
11724       return "DW_OP_breg20";
11725     case DW_OP_breg21:
11726       return "DW_OP_breg21";
11727     case DW_OP_breg22:
11728       return "DW_OP_breg22";
11729     case DW_OP_breg23:
11730       return "DW_OP_breg23";
11731     case DW_OP_breg24:
11732       return "DW_OP_breg24";
11733     case DW_OP_breg25:
11734       return "DW_OP_breg25";
11735     case DW_OP_breg26:
11736       return "DW_OP_breg26";
11737     case DW_OP_breg27:
11738       return "DW_OP_breg27";
11739     case DW_OP_breg28:
11740       return "DW_OP_breg28";
11741     case DW_OP_breg29:
11742       return "DW_OP_breg29";
11743     case DW_OP_breg30:
11744       return "DW_OP_breg30";
11745     case DW_OP_breg31:
11746       return "DW_OP_breg31";
11747     case DW_OP_regx:
11748       return "DW_OP_regx";
11749     case DW_OP_fbreg:
11750       return "DW_OP_fbreg";
11751     case DW_OP_bregx:
11752       return "DW_OP_bregx";
11753     case DW_OP_piece:
11754       return "DW_OP_piece";
11755     case DW_OP_deref_size:
11756       return "DW_OP_deref_size";
11757     case DW_OP_xderef_size:
11758       return "DW_OP_xderef_size";
11759     case DW_OP_nop:
11760       return "DW_OP_nop";
11761     /* DWARF 3 extensions.  */
11762     case DW_OP_push_object_address:
11763       return "DW_OP_push_object_address";
11764     case DW_OP_call2:
11765       return "DW_OP_call2";
11766     case DW_OP_call4:
11767       return "DW_OP_call4";
11768     case DW_OP_call_ref:
11769       return "DW_OP_call_ref";
11770     case DW_OP_form_tls_address:
11771       return "DW_OP_form_tls_address";
11772     case DW_OP_call_frame_cfa:
11773       return "DW_OP_call_frame_cfa";
11774     case DW_OP_bit_piece:
11775       return "DW_OP_bit_piece";
11776     /* DWARF 4 extensions.  */
11777     case DW_OP_implicit_value:
11778       return "DW_OP_implicit_value";
11779     case DW_OP_stack_value:
11780       return "DW_OP_stack_value";
11781     /* GNU extensions.  */
11782     case DW_OP_GNU_push_tls_address:
11783       return "DW_OP_GNU_push_tls_address";
11784     case DW_OP_GNU_uninit:
11785       return "DW_OP_GNU_uninit";
11786     default:
11787       return def ? "OP_<unknown>" : NULL;
11788     }
11789 }
11790
11791 static char *
11792 dwarf_bool_name (unsigned mybool)
11793 {
11794   if (mybool)
11795     return "TRUE";
11796   else
11797     return "FALSE";
11798 }
11799
11800 /* Convert a DWARF type code into its string name.  */
11801
11802 static char *
11803 dwarf_type_encoding_name (unsigned enc)
11804 {
11805   switch (enc)
11806     {
11807     case DW_ATE_void:
11808       return "DW_ATE_void";
11809     case DW_ATE_address:
11810       return "DW_ATE_address";
11811     case DW_ATE_boolean:
11812       return "DW_ATE_boolean";
11813     case DW_ATE_complex_float:
11814       return "DW_ATE_complex_float";
11815     case DW_ATE_float:
11816       return "DW_ATE_float";
11817     case DW_ATE_signed:
11818       return "DW_ATE_signed";
11819     case DW_ATE_signed_char:
11820       return "DW_ATE_signed_char";
11821     case DW_ATE_unsigned:
11822       return "DW_ATE_unsigned";
11823     case DW_ATE_unsigned_char:
11824       return "DW_ATE_unsigned_char";
11825     /* DWARF 3.  */
11826     case DW_ATE_imaginary_float:
11827       return "DW_ATE_imaginary_float";
11828     case DW_ATE_packed_decimal:
11829       return "DW_ATE_packed_decimal";
11830     case DW_ATE_numeric_string:
11831       return "DW_ATE_numeric_string";
11832     case DW_ATE_edited:
11833       return "DW_ATE_edited";
11834     case DW_ATE_signed_fixed:
11835       return "DW_ATE_signed_fixed";
11836     case DW_ATE_unsigned_fixed:
11837       return "DW_ATE_unsigned_fixed";
11838     case DW_ATE_decimal_float:
11839       return "DW_ATE_decimal_float";
11840     /* DWARF 4.  */
11841     case DW_ATE_UTF:
11842       return "DW_ATE_UTF";
11843     /* HP extensions.  */
11844     case DW_ATE_HP_float80:
11845       return "DW_ATE_HP_float80";
11846     case DW_ATE_HP_complex_float80:
11847       return "DW_ATE_HP_complex_float80";
11848     case DW_ATE_HP_float128:
11849       return "DW_ATE_HP_float128";
11850     case DW_ATE_HP_complex_float128:
11851       return "DW_ATE_HP_complex_float128";
11852     case DW_ATE_HP_floathpintel:
11853       return "DW_ATE_HP_floathpintel";
11854     case DW_ATE_HP_imaginary_float80:
11855       return "DW_ATE_HP_imaginary_float80";
11856     case DW_ATE_HP_imaginary_float128:
11857       return "DW_ATE_HP_imaginary_float128";
11858     default:
11859       return "DW_ATE_<unknown>";
11860     }
11861 }
11862
11863 /* Convert a DWARF call frame info operation to its string name. */
11864
11865 #if 0
11866 static char *
11867 dwarf_cfi_name (unsigned cfi_opc)
11868 {
11869   switch (cfi_opc)
11870     {
11871     case DW_CFA_advance_loc:
11872       return "DW_CFA_advance_loc";
11873     case DW_CFA_offset:
11874       return "DW_CFA_offset";
11875     case DW_CFA_restore:
11876       return "DW_CFA_restore";
11877     case DW_CFA_nop:
11878       return "DW_CFA_nop";
11879     case DW_CFA_set_loc:
11880       return "DW_CFA_set_loc";
11881     case DW_CFA_advance_loc1:
11882       return "DW_CFA_advance_loc1";
11883     case DW_CFA_advance_loc2:
11884       return "DW_CFA_advance_loc2";
11885     case DW_CFA_advance_loc4:
11886       return "DW_CFA_advance_loc4";
11887     case DW_CFA_offset_extended:
11888       return "DW_CFA_offset_extended";
11889     case DW_CFA_restore_extended:
11890       return "DW_CFA_restore_extended";
11891     case DW_CFA_undefined:
11892       return "DW_CFA_undefined";
11893     case DW_CFA_same_value:
11894       return "DW_CFA_same_value";
11895     case DW_CFA_register:
11896       return "DW_CFA_register";
11897     case DW_CFA_remember_state:
11898       return "DW_CFA_remember_state";
11899     case DW_CFA_restore_state:
11900       return "DW_CFA_restore_state";
11901     case DW_CFA_def_cfa:
11902       return "DW_CFA_def_cfa";
11903     case DW_CFA_def_cfa_register:
11904       return "DW_CFA_def_cfa_register";
11905     case DW_CFA_def_cfa_offset:
11906       return "DW_CFA_def_cfa_offset";
11907     /* DWARF 3.  */
11908     case DW_CFA_def_cfa_expression:
11909       return "DW_CFA_def_cfa_expression";
11910     case DW_CFA_expression:
11911       return "DW_CFA_expression";
11912     case DW_CFA_offset_extended_sf:
11913       return "DW_CFA_offset_extended_sf";
11914     case DW_CFA_def_cfa_sf:
11915       return "DW_CFA_def_cfa_sf";
11916     case DW_CFA_def_cfa_offset_sf:
11917       return "DW_CFA_def_cfa_offset_sf";
11918     case DW_CFA_val_offset:
11919       return "DW_CFA_val_offset";
11920     case DW_CFA_val_offset_sf:
11921       return "DW_CFA_val_offset_sf";
11922     case DW_CFA_val_expression:
11923       return "DW_CFA_val_expression";
11924     /* SGI/MIPS specific.  */
11925     case DW_CFA_MIPS_advance_loc8:
11926       return "DW_CFA_MIPS_advance_loc8";
11927     /* GNU extensions.  */
11928     case DW_CFA_GNU_window_save:
11929       return "DW_CFA_GNU_window_save";
11930     case DW_CFA_GNU_args_size:
11931       return "DW_CFA_GNU_args_size";
11932     case DW_CFA_GNU_negative_offset_extended:
11933       return "DW_CFA_GNU_negative_offset_extended";
11934     default:
11935       return "DW_CFA_<unknown>";
11936     }
11937 }
11938 #endif
11939
11940 static void
11941 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
11942 {
11943   unsigned int i;
11944
11945   print_spaces (indent, f);
11946   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
11947            dwarf_tag_name (die->tag), die->abbrev, die->offset);
11948
11949   if (die->parent != NULL)
11950     {
11951       print_spaces (indent, f);
11952       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
11953                           die->parent->offset);
11954     }
11955
11956   print_spaces (indent, f);
11957   fprintf_unfiltered (f, "  has children: %s\n",
11958            dwarf_bool_name (die->child != NULL));
11959
11960   print_spaces (indent, f);
11961   fprintf_unfiltered (f, "  attributes:\n");
11962
11963   for (i = 0; i < die->num_attrs; ++i)
11964     {
11965       print_spaces (indent, f);
11966       fprintf_unfiltered (f, "    %s (%s) ",
11967                dwarf_attr_name (die->attrs[i].name),
11968                dwarf_form_name (die->attrs[i].form));
11969
11970       switch (die->attrs[i].form)
11971         {
11972         case DW_FORM_ref_addr:
11973         case DW_FORM_addr:
11974           fprintf_unfiltered (f, "address: ");
11975           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
11976           break;
11977         case DW_FORM_block2:
11978         case DW_FORM_block4:
11979         case DW_FORM_block:
11980         case DW_FORM_block1:
11981           fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
11982           break;
11983         case DW_FORM_exprloc:
11984           fprintf_unfiltered (f, "expression: size %u",
11985                               DW_BLOCK (&die->attrs[i])->size);
11986           break;
11987         case DW_FORM_ref1:
11988         case DW_FORM_ref2:
11989         case DW_FORM_ref4:
11990           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
11991                               (long) (DW_ADDR (&die->attrs[i])));
11992           break;
11993         case DW_FORM_data1:
11994         case DW_FORM_data2:
11995         case DW_FORM_data4:
11996         case DW_FORM_data8:
11997         case DW_FORM_udata:
11998         case DW_FORM_sdata:
11999           fprintf_unfiltered (f, "constant: %s",
12000                               pulongest (DW_UNSND (&die->attrs[i])));
12001           break;
12002         case DW_FORM_sec_offset:
12003           fprintf_unfiltered (f, "section offset: %s",
12004                               pulongest (DW_UNSND (&die->attrs[i])));
12005           break;
12006         case DW_FORM_sig8:
12007           if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12008             fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12009                                 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12010           else
12011             fprintf_unfiltered (f, "signatured type, offset: unknown");
12012           break;
12013         case DW_FORM_string:
12014         case DW_FORM_strp:
12015           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12016                    DW_STRING (&die->attrs[i])
12017                    ? DW_STRING (&die->attrs[i]) : "",
12018                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12019           break;
12020         case DW_FORM_flag:
12021           if (DW_UNSND (&die->attrs[i]))
12022             fprintf_unfiltered (f, "flag: TRUE");
12023           else
12024             fprintf_unfiltered (f, "flag: FALSE");
12025           break;
12026         case DW_FORM_flag_present:
12027           fprintf_unfiltered (f, "flag: TRUE");
12028           break;
12029         case DW_FORM_indirect:
12030           /* the reader will have reduced the indirect form to
12031              the "base form" so this form should not occur */
12032           fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12033           break;
12034         default:
12035           fprintf_unfiltered (f, "unsupported attribute form: %d.",
12036                    die->attrs[i].form);
12037           break;
12038         }
12039       fprintf_unfiltered (f, "\n");
12040     }
12041 }
12042
12043 static void
12044 dump_die_for_error (struct die_info *die)
12045 {
12046   dump_die_shallow (gdb_stderr, 0, die);
12047 }
12048
12049 static void
12050 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12051 {
12052   int indent = level * 4;
12053
12054   gdb_assert (die != NULL);
12055
12056   if (level >= max_level)
12057     return;
12058
12059   dump_die_shallow (f, indent, die);
12060
12061   if (die->child != NULL)
12062     {
12063       print_spaces (indent, f);
12064       fprintf_unfiltered (f, "  Children:");
12065       if (level + 1 < max_level)
12066         {
12067           fprintf_unfiltered (f, "\n");
12068           dump_die_1 (f, level + 1, max_level, die->child);
12069         }
12070       else
12071         {
12072           fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12073         }
12074     }
12075
12076   if (die->sibling != NULL && level > 0)
12077     {
12078       dump_die_1 (f, level, max_level, die->sibling);
12079     }
12080 }
12081
12082 /* This is called from the pdie macro in gdbinit.in.
12083    It's not static so gcc will keep a copy callable from gdb.  */
12084
12085 void
12086 dump_die (struct die_info *die, int max_level)
12087 {
12088   dump_die_1 (gdb_stdlog, 0, max_level, die);
12089 }
12090
12091 static void
12092 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12093 {
12094   void **slot;
12095
12096   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12097
12098   *slot = die;
12099 }
12100
12101 static int
12102 is_ref_attr (struct attribute *attr)
12103 {
12104   switch (attr->form)
12105     {
12106     case DW_FORM_ref_addr:
12107     case DW_FORM_ref1:
12108     case DW_FORM_ref2:
12109     case DW_FORM_ref4:
12110     case DW_FORM_ref8:
12111     case DW_FORM_ref_udata:
12112       return 1;
12113     default:
12114       return 0;
12115     }
12116 }
12117
12118 static unsigned int
12119 dwarf2_get_ref_die_offset (struct attribute *attr)
12120 {
12121   if (is_ref_attr (attr))
12122     return DW_ADDR (attr);
12123
12124   complaint (&symfile_complaints,
12125              _("unsupported die ref attribute form: '%s'"),
12126              dwarf_form_name (attr->form));
12127   return 0;
12128 }
12129
12130 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
12131  * the value held by the attribute is not constant.  */
12132
12133 static LONGEST
12134 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12135 {
12136   if (attr->form == DW_FORM_sdata)
12137     return DW_SND (attr);
12138   else if (attr->form == DW_FORM_udata
12139            || attr->form == DW_FORM_data1
12140            || attr->form == DW_FORM_data2
12141            || attr->form == DW_FORM_data4
12142            || attr->form == DW_FORM_data8)
12143     return DW_UNSND (attr);
12144   else
12145     {
12146       complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12147                  dwarf_form_name (attr->form));
12148       return default_value;
12149     }
12150 }
12151
12152 /* THIS_CU has a reference to PER_CU.  If necessary, load the new compilation
12153    unit and add it to our queue.
12154    The result is non-zero if PER_CU was queued, otherwise the result is zero
12155    meaning either PER_CU is already queued or it is already loaded.  */
12156
12157 static int
12158 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12159                        struct dwarf2_per_cu_data *per_cu)
12160 {
12161   /* Mark the dependence relation so that we don't flush PER_CU
12162      too early.  */
12163   dwarf2_add_dependence (this_cu, per_cu);
12164
12165   /* If it's already on the queue, we have nothing to do.  */
12166   if (per_cu->queued)
12167     return 0;
12168
12169   /* If the compilation unit is already loaded, just mark it as
12170      used.  */
12171   if (per_cu->cu != NULL)
12172     {
12173       per_cu->cu->last_used = 0;
12174       return 0;
12175     }
12176
12177   /* Add it to the queue.  */
12178   queue_comp_unit (per_cu, this_cu->objfile);
12179
12180   return 1;
12181 }
12182
12183 /* Follow reference or signature attribute ATTR of SRC_DIE.
12184    On entry *REF_CU is the CU of SRC_DIE.
12185    On exit *REF_CU is the CU of the result.  */
12186
12187 static struct die_info *
12188 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12189                        struct dwarf2_cu **ref_cu)
12190 {
12191   struct die_info *die;
12192
12193   if (is_ref_attr (attr))
12194     die = follow_die_ref (src_die, attr, ref_cu);
12195   else if (attr->form == DW_FORM_sig8)
12196     die = follow_die_sig (src_die, attr, ref_cu);
12197   else
12198     {
12199       dump_die_for_error (src_die);
12200       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12201              (*ref_cu)->objfile->name);
12202     }
12203
12204   return die;
12205 }
12206
12207 /* Follow reference OFFSET.
12208    On entry *REF_CU is the CU of source DIE referencing OFFSET.
12209    On exit *REF_CU is the CU of the result.  */
12210
12211 static struct die_info *
12212 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12213 {
12214   struct die_info temp_die;
12215   struct dwarf2_cu *target_cu, *cu = *ref_cu;
12216
12217   gdb_assert (cu->per_cu != NULL);
12218
12219   if (cu->per_cu->from_debug_types)
12220     {
12221       /* .debug_types CUs cannot reference anything outside their CU.
12222          If they need to, they have to reference a signatured type via
12223          DW_FORM_sig8.  */
12224       if (! offset_in_cu_p (&cu->header, offset))
12225         return NULL;
12226       target_cu = cu;
12227     }
12228   else if (! offset_in_cu_p (&cu->header, offset))
12229     {
12230       struct dwarf2_per_cu_data *per_cu;
12231
12232       per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12233
12234       /* If necessary, add it to the queue and load its DIEs.  */
12235       if (maybe_queue_comp_unit (cu, per_cu))
12236         load_full_comp_unit (per_cu, cu->objfile);
12237
12238       target_cu = per_cu->cu;
12239     }
12240   else
12241     target_cu = cu;
12242
12243   *ref_cu = target_cu;
12244   temp_die.offset = offset;
12245   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12246 }
12247
12248 /* Follow reference attribute ATTR of SRC_DIE.
12249    On entry *REF_CU is the CU of SRC_DIE.
12250    On exit *REF_CU is the CU of the result.  */
12251
12252 static struct die_info *
12253 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12254                 struct dwarf2_cu **ref_cu)
12255 {
12256   unsigned int offset = dwarf2_get_ref_die_offset (attr);
12257   struct dwarf2_cu *cu = *ref_cu;
12258   struct die_info *die;
12259
12260   die = follow_die_offset (offset, ref_cu);
12261   if (!die)
12262     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12263            "at 0x%x [in module %s]"),
12264            offset, src_die->offset, cu->objfile->name);
12265
12266   return die;
12267 }
12268
12269 /* Return DWARF block and its CU referenced by OFFSET at PER_CU.  Returned
12270    value is intended for DW_OP_call*.  */
12271
12272 struct dwarf2_locexpr_baton
12273 dwarf2_fetch_die_location_block (unsigned int offset,
12274                                  struct dwarf2_per_cu_data *per_cu)
12275 {
12276   struct dwarf2_cu *cu = per_cu->cu;
12277   struct die_info *die;
12278   struct attribute *attr;
12279   struct dwarf2_locexpr_baton retval;
12280
12281   die = follow_die_offset (offset, &cu);
12282   if (!die)
12283     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12284            offset, per_cu->cu->objfile->name);
12285
12286   attr = dwarf2_attr (die, DW_AT_location, cu);
12287   if (!attr)
12288     {
12289       /* DWARF: "If there is no such attribute, then there is no effect.".  */
12290
12291       retval.data = NULL;
12292       retval.size = 0;
12293     }
12294   else
12295     {
12296       if (!attr_form_is_block (attr))
12297         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12298                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12299                offset, per_cu->cu->objfile->name);
12300
12301       retval.data = DW_BLOCK (attr)->data;
12302       retval.size = DW_BLOCK (attr)->size;
12303     }
12304   retval.per_cu = cu->per_cu;
12305   return retval;
12306 }
12307
12308 /* Follow the signature attribute ATTR in SRC_DIE.
12309    On entry *REF_CU is the CU of SRC_DIE.
12310    On exit *REF_CU is the CU of the result.  */
12311
12312 static struct die_info *
12313 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12314                 struct dwarf2_cu **ref_cu)
12315 {
12316   struct objfile *objfile = (*ref_cu)->objfile;
12317   struct die_info temp_die;
12318   struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12319   struct dwarf2_cu *sig_cu;
12320   struct die_info *die;
12321
12322   /* sig_type will be NULL if the signatured type is missing from
12323      the debug info.  */
12324   if (sig_type == NULL)
12325     error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12326              "at 0x%x [in module %s]"),
12327            src_die->offset, objfile->name);
12328
12329   /* If necessary, add it to the queue and load its DIEs.  */
12330
12331   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12332     read_signatured_type (objfile, sig_type);
12333
12334   gdb_assert (sig_type->per_cu.cu != NULL);
12335
12336   sig_cu = sig_type->per_cu.cu;
12337   temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12338   die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12339   if (die)
12340     {
12341       *ref_cu = sig_cu;
12342       return die;
12343     }
12344
12345   error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12346          "at 0x%x [in module %s]"),
12347          sig_type->type_offset, src_die->offset, objfile->name);
12348 }
12349
12350 /* Given an offset of a signatured type, return its signatured_type.  */
12351
12352 static struct signatured_type *
12353 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12354 {
12355   gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12356   unsigned int length, initial_length_size;
12357   unsigned int sig_offset;
12358   struct signatured_type find_entry, *type_sig;
12359
12360   length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12361   sig_offset = (initial_length_size
12362                 + 2 /*version*/
12363                 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12364                 + 1 /*address_size*/);
12365   find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12366   type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12367
12368   /* This is only used to lookup previously recorded types.
12369      If we didn't find it, it's our bug.  */
12370   gdb_assert (type_sig != NULL);
12371   gdb_assert (offset == type_sig->offset);
12372
12373   return type_sig;
12374 }
12375
12376 /* Read in signatured type at OFFSET and build its CU and die(s).  */
12377
12378 static void
12379 read_signatured_type_at_offset (struct objfile *objfile,
12380                                 unsigned int offset)
12381 {
12382   struct signatured_type *type_sig;
12383
12384   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12385
12386   /* We have the section offset, but we need the signature to do the
12387      hash table lookup.  */
12388   type_sig = lookup_signatured_type_at_offset (objfile, offset);
12389
12390   gdb_assert (type_sig->per_cu.cu == NULL);
12391
12392   read_signatured_type (objfile, type_sig);
12393
12394   gdb_assert (type_sig->per_cu.cu != NULL);
12395 }
12396
12397 /* Read in a signatured type and build its CU and DIEs.  */
12398
12399 static void
12400 read_signatured_type (struct objfile *objfile,
12401                       struct signatured_type *type_sig)
12402 {
12403   gdb_byte *types_ptr;
12404   struct die_reader_specs reader_specs;
12405   struct dwarf2_cu *cu;
12406   ULONGEST signature;
12407   struct cleanup *back_to, *free_cu_cleanup;
12408   struct attribute *attr;
12409
12410   dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
12411   types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
12412
12413   gdb_assert (type_sig->per_cu.cu == NULL);
12414
12415   cu = xmalloc (sizeof (struct dwarf2_cu));
12416   memset (cu, 0, sizeof (struct dwarf2_cu));
12417   obstack_init (&cu->comp_unit_obstack);
12418   cu->objfile = objfile;
12419   type_sig->per_cu.cu = cu;
12420   cu->per_cu = &type_sig->per_cu;
12421
12422   /* If an error occurs while loading, release our storage.  */
12423   free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
12424
12425   types_ptr = read_type_comp_unit_head (&cu->header, &signature,
12426                                         types_ptr, objfile->obfd);
12427   gdb_assert (signature == type_sig->signature);
12428
12429   cu->die_hash
12430     = htab_create_alloc_ex (cu->header.length / 12,
12431                             die_hash,
12432                             die_eq,
12433                             NULL,
12434                             &cu->comp_unit_obstack,
12435                             hashtab_obstack_allocate,
12436                             dummy_obstack_deallocate);
12437
12438   dwarf2_read_abbrevs (cu->objfile->obfd, cu);
12439   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
12440
12441   init_cu_die_reader (&reader_specs, cu);
12442
12443   cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
12444                                     NULL /*parent*/);
12445
12446   /* We try not to read any attributes in this function, because not
12447      all objfiles needed for references have been loaded yet, and symbol
12448      table processing isn't initialized.  But we have to set the CU language,
12449      or we won't be able to build types correctly.  */
12450   attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
12451   if (attr)
12452     set_cu_language (DW_UNSND (attr), cu);
12453   else
12454     set_cu_language (language_minimal, cu);
12455
12456   do_cleanups (back_to);
12457
12458   /* We've successfully allocated this compilation unit.  Let our caller
12459      clean it up when finished with it.  */
12460   discard_cleanups (free_cu_cleanup);
12461
12462   type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
12463   dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
12464 }
12465
12466 /* Decode simple location descriptions.
12467    Given a pointer to a dwarf block that defines a location, compute
12468    the location and return the value.
12469
12470    NOTE drow/2003-11-18: This function is called in two situations
12471    now: for the address of static or global variables (partial symbols
12472    only) and for offsets into structures which are expected to be
12473    (more or less) constant.  The partial symbol case should go away,
12474    and only the constant case should remain.  That will let this
12475    function complain more accurately.  A few special modes are allowed
12476    without complaint for global variables (for instance, global
12477    register values and thread-local values).
12478
12479    A location description containing no operations indicates that the
12480    object is optimized out.  The return value is 0 for that case.
12481    FIXME drow/2003-11-16: No callers check for this case any more; soon all
12482    callers will only want a very basic result and this can become a
12483    complaint.
12484
12485    Note that stack[0] is unused except as a default error return.
12486    Note that stack overflow is not yet handled.  */
12487
12488 static CORE_ADDR
12489 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
12490 {
12491   struct objfile *objfile = cu->objfile;
12492   int i;
12493   int size = blk->size;
12494   gdb_byte *data = blk->data;
12495   CORE_ADDR stack[64];
12496   int stacki;
12497   unsigned int bytes_read, unsnd;
12498   gdb_byte op;
12499
12500   i = 0;
12501   stacki = 0;
12502   stack[stacki] = 0;
12503
12504   while (i < size)
12505     {
12506       op = data[i++];
12507       switch (op)
12508         {
12509         case DW_OP_lit0:
12510         case DW_OP_lit1:
12511         case DW_OP_lit2:
12512         case DW_OP_lit3:
12513         case DW_OP_lit4:
12514         case DW_OP_lit5:
12515         case DW_OP_lit6:
12516         case DW_OP_lit7:
12517         case DW_OP_lit8:
12518         case DW_OP_lit9:
12519         case DW_OP_lit10:
12520         case DW_OP_lit11:
12521         case DW_OP_lit12:
12522         case DW_OP_lit13:
12523         case DW_OP_lit14:
12524         case DW_OP_lit15:
12525         case DW_OP_lit16:
12526         case DW_OP_lit17:
12527         case DW_OP_lit18:
12528         case DW_OP_lit19:
12529         case DW_OP_lit20:
12530         case DW_OP_lit21:
12531         case DW_OP_lit22:
12532         case DW_OP_lit23:
12533         case DW_OP_lit24:
12534         case DW_OP_lit25:
12535         case DW_OP_lit26:
12536         case DW_OP_lit27:
12537         case DW_OP_lit28:
12538         case DW_OP_lit29:
12539         case DW_OP_lit30:
12540         case DW_OP_lit31:
12541           stack[++stacki] = op - DW_OP_lit0;
12542           break;
12543
12544         case DW_OP_reg0:
12545         case DW_OP_reg1:
12546         case DW_OP_reg2:
12547         case DW_OP_reg3:
12548         case DW_OP_reg4:
12549         case DW_OP_reg5:
12550         case DW_OP_reg6:
12551         case DW_OP_reg7:
12552         case DW_OP_reg8:
12553         case DW_OP_reg9:
12554         case DW_OP_reg10:
12555         case DW_OP_reg11:
12556         case DW_OP_reg12:
12557         case DW_OP_reg13:
12558         case DW_OP_reg14:
12559         case DW_OP_reg15:
12560         case DW_OP_reg16:
12561         case DW_OP_reg17:
12562         case DW_OP_reg18:
12563         case DW_OP_reg19:
12564         case DW_OP_reg20:
12565         case DW_OP_reg21:
12566         case DW_OP_reg22:
12567         case DW_OP_reg23:
12568         case DW_OP_reg24:
12569         case DW_OP_reg25:
12570         case DW_OP_reg26:
12571         case DW_OP_reg27:
12572         case DW_OP_reg28:
12573         case DW_OP_reg29:
12574         case DW_OP_reg30:
12575         case DW_OP_reg31:
12576           stack[++stacki] = op - DW_OP_reg0;
12577           if (i < size)
12578             dwarf2_complex_location_expr_complaint ();
12579           break;
12580
12581         case DW_OP_regx:
12582           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12583           i += bytes_read;
12584           stack[++stacki] = unsnd;
12585           if (i < size)
12586             dwarf2_complex_location_expr_complaint ();
12587           break;
12588
12589         case DW_OP_addr:
12590           stack[++stacki] = read_address (objfile->obfd, &data[i],
12591                                           cu, &bytes_read);
12592           i += bytes_read;
12593           break;
12594
12595         case DW_OP_const1u:
12596           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
12597           i += 1;
12598           break;
12599
12600         case DW_OP_const1s:
12601           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
12602           i += 1;
12603           break;
12604
12605         case DW_OP_const2u:
12606           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
12607           i += 2;
12608           break;
12609
12610         case DW_OP_const2s:
12611           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
12612           i += 2;
12613           break;
12614
12615         case DW_OP_const4u:
12616           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
12617           i += 4;
12618           break;
12619
12620         case DW_OP_const4s:
12621           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
12622           i += 4;
12623           break;
12624
12625         case DW_OP_constu:
12626           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
12627                                                   &bytes_read);
12628           i += bytes_read;
12629           break;
12630
12631         case DW_OP_consts:
12632           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
12633           i += bytes_read;
12634           break;
12635
12636         case DW_OP_dup:
12637           stack[stacki + 1] = stack[stacki];
12638           stacki++;
12639           break;
12640
12641         case DW_OP_plus:
12642           stack[stacki - 1] += stack[stacki];
12643           stacki--;
12644           break;
12645
12646         case DW_OP_plus_uconst:
12647           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
12648           i += bytes_read;
12649           break;
12650
12651         case DW_OP_minus:
12652           stack[stacki - 1] -= stack[stacki];
12653           stacki--;
12654           break;
12655
12656         case DW_OP_deref:
12657           /* If we're not the last op, then we definitely can't encode
12658              this using GDB's address_class enum.  This is valid for partial
12659              global symbols, although the variable's address will be bogus
12660              in the psymtab.  */
12661           if (i < size)
12662             dwarf2_complex_location_expr_complaint ();
12663           break;
12664
12665         case DW_OP_GNU_push_tls_address:
12666           /* The top of the stack has the offset from the beginning
12667              of the thread control block at which the variable is located.  */
12668           /* Nothing should follow this operator, so the top of stack would
12669              be returned.  */
12670           /* This is valid for partial global symbols, but the variable's
12671              address will be bogus in the psymtab.  */
12672           if (i < size)
12673             dwarf2_complex_location_expr_complaint ();
12674           break;
12675
12676         case DW_OP_GNU_uninit:
12677           break;
12678
12679         default:
12680           complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
12681                      dwarf_stack_op_name (op, 1));
12682           return (stack[stacki]);
12683         }
12684     }
12685   return (stack[stacki]);
12686 }
12687
12688 /* memory allocation interface */
12689
12690 static struct dwarf_block *
12691 dwarf_alloc_block (struct dwarf2_cu *cu)
12692 {
12693   struct dwarf_block *blk;
12694
12695   blk = (struct dwarf_block *)
12696     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
12697   return (blk);
12698 }
12699
12700 static struct abbrev_info *
12701 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
12702 {
12703   struct abbrev_info *abbrev;
12704
12705   abbrev = (struct abbrev_info *)
12706     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
12707   memset (abbrev, 0, sizeof (struct abbrev_info));
12708   return (abbrev);
12709 }
12710
12711 static struct die_info *
12712 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
12713 {
12714   struct die_info *die;
12715   size_t size = sizeof (struct die_info);
12716
12717   if (num_attrs > 1)
12718     size += (num_attrs - 1) * sizeof (struct attribute);
12719
12720   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
12721   memset (die, 0, sizeof (struct die_info));
12722   return (die);
12723 }
12724
12725 \f
12726 /* Macro support.  */
12727
12728
12729 /* Return the full name of file number I in *LH's file name table.
12730    Use COMP_DIR as the name of the current directory of the
12731    compilation.  The result is allocated using xmalloc; the caller is
12732    responsible for freeing it.  */
12733 static char *
12734 file_full_name (int file, struct line_header *lh, const char *comp_dir)
12735 {
12736   /* Is the file number a valid index into the line header's file name
12737      table?  Remember that file numbers start with one, not zero.  */
12738   if (1 <= file && file <= lh->num_file_names)
12739     {
12740       struct file_entry *fe = &lh->file_names[file - 1];
12741
12742       if (IS_ABSOLUTE_PATH (fe->name))
12743         return xstrdup (fe->name);
12744       else
12745         {
12746           const char *dir;
12747           int dir_len;
12748           char *full_name;
12749
12750           if (fe->dir_index)
12751             dir = lh->include_dirs[fe->dir_index - 1];
12752           else
12753             dir = comp_dir;
12754
12755           if (dir)
12756             {
12757               dir_len = strlen (dir);
12758               full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
12759               strcpy (full_name, dir);
12760               full_name[dir_len] = '/';
12761               strcpy (full_name + dir_len + 1, fe->name);
12762               return full_name;
12763             }
12764           else
12765             return xstrdup (fe->name);
12766         }
12767     }
12768   else
12769     {
12770       /* The compiler produced a bogus file number.  We can at least
12771          record the macro definitions made in the file, even if we
12772          won't be able to find the file by name.  */
12773       char fake_name[80];
12774
12775       sprintf (fake_name, "<bad macro file number %d>", file);
12776
12777       complaint (&symfile_complaints,
12778                  _("bad file number in macro information (%d)"),
12779                  file);
12780
12781       return xstrdup (fake_name);
12782     }
12783 }
12784
12785
12786 static struct macro_source_file *
12787 macro_start_file (int file, int line,
12788                   struct macro_source_file *current_file,
12789                   const char *comp_dir,
12790                   struct line_header *lh, struct objfile *objfile)
12791 {
12792   /* The full name of this source file.  */
12793   char *full_name = file_full_name (file, lh, comp_dir);
12794
12795   /* We don't create a macro table for this compilation unit
12796      at all until we actually get a filename.  */
12797   if (! pending_macros)
12798     pending_macros = new_macro_table (&objfile->objfile_obstack,
12799                                       objfile->macro_cache);
12800
12801   if (! current_file)
12802     /* If we have no current file, then this must be the start_file
12803        directive for the compilation unit's main source file.  */
12804     current_file = macro_set_main (pending_macros, full_name);
12805   else
12806     current_file = macro_include (current_file, line, full_name);
12807
12808   xfree (full_name);
12809
12810   return current_file;
12811 }
12812
12813
12814 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
12815    followed by a null byte.  */
12816 static char *
12817 copy_string (const char *buf, int len)
12818 {
12819   char *s = xmalloc (len + 1);
12820
12821   memcpy (s, buf, len);
12822   s[len] = '\0';
12823   return s;
12824 }
12825
12826
12827 static const char *
12828 consume_improper_spaces (const char *p, const char *body)
12829 {
12830   if (*p == ' ')
12831     {
12832       complaint (&symfile_complaints,
12833                  _("macro definition contains spaces in formal argument list:\n`%s'"),
12834                  body);
12835
12836       while (*p == ' ')
12837         p++;
12838     }
12839
12840   return p;
12841 }
12842
12843
12844 static void
12845 parse_macro_definition (struct macro_source_file *file, int line,
12846                         const char *body)
12847 {
12848   const char *p;
12849
12850   /* The body string takes one of two forms.  For object-like macro
12851      definitions, it should be:
12852
12853         <macro name> " " <definition>
12854
12855      For function-like macro definitions, it should be:
12856
12857         <macro name> "() " <definition>
12858      or
12859         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
12860
12861      Spaces may appear only where explicitly indicated, and in the
12862      <definition>.
12863
12864      The Dwarf 2 spec says that an object-like macro's name is always
12865      followed by a space, but versions of GCC around March 2002 omit
12866      the space when the macro's definition is the empty string.
12867
12868      The Dwarf 2 spec says that there should be no spaces between the
12869      formal arguments in a function-like macro's formal argument list,
12870      but versions of GCC around March 2002 include spaces after the
12871      commas.  */
12872
12873
12874   /* Find the extent of the macro name.  The macro name is terminated
12875      by either a space or null character (for an object-like macro) or
12876      an opening paren (for a function-like macro).  */
12877   for (p = body; *p; p++)
12878     if (*p == ' ' || *p == '(')
12879       break;
12880
12881   if (*p == ' ' || *p == '\0')
12882     {
12883       /* It's an object-like macro.  */
12884       int name_len = p - body;
12885       char *name = copy_string (body, name_len);
12886       const char *replacement;
12887
12888       if (*p == ' ')
12889         replacement = body + name_len + 1;
12890       else
12891         {
12892           dwarf2_macro_malformed_definition_complaint (body);
12893           replacement = body + name_len;
12894         }
12895
12896       macro_define_object (file, line, name, replacement);
12897
12898       xfree (name);
12899     }
12900   else if (*p == '(')
12901     {
12902       /* It's a function-like macro.  */
12903       char *name = copy_string (body, p - body);
12904       int argc = 0;
12905       int argv_size = 1;
12906       char **argv = xmalloc (argv_size * sizeof (*argv));
12907
12908       p++;
12909
12910       p = consume_improper_spaces (p, body);
12911
12912       /* Parse the formal argument list.  */
12913       while (*p && *p != ')')
12914         {
12915           /* Find the extent of the current argument name.  */
12916           const char *arg_start = p;
12917
12918           while (*p && *p != ',' && *p != ')' && *p != ' ')
12919             p++;
12920
12921           if (! *p || p == arg_start)
12922             dwarf2_macro_malformed_definition_complaint (body);
12923           else
12924             {
12925               /* Make sure argv has room for the new argument.  */
12926               if (argc >= argv_size)
12927                 {
12928                   argv_size *= 2;
12929                   argv = xrealloc (argv, argv_size * sizeof (*argv));
12930                 }
12931
12932               argv[argc++] = copy_string (arg_start, p - arg_start);
12933             }
12934
12935           p = consume_improper_spaces (p, body);
12936
12937           /* Consume the comma, if present.  */
12938           if (*p == ',')
12939             {
12940               p++;
12941
12942               p = consume_improper_spaces (p, body);
12943             }
12944         }
12945
12946       if (*p == ')')
12947         {
12948           p++;
12949
12950           if (*p == ' ')
12951             /* Perfectly formed definition, no complaints.  */
12952             macro_define_function (file, line, name,
12953                                    argc, (const char **) argv,
12954                                    p + 1);
12955           else if (*p == '\0')
12956             {
12957               /* Complain, but do define it.  */
12958               dwarf2_macro_malformed_definition_complaint (body);
12959               macro_define_function (file, line, name,
12960                                      argc, (const char **) argv,
12961                                      p);
12962             }
12963           else
12964             /* Just complain.  */
12965             dwarf2_macro_malformed_definition_complaint (body);
12966         }
12967       else
12968         /* Just complain.  */
12969         dwarf2_macro_malformed_definition_complaint (body);
12970
12971       xfree (name);
12972       {
12973         int i;
12974
12975         for (i = 0; i < argc; i++)
12976           xfree (argv[i]);
12977       }
12978       xfree (argv);
12979     }
12980   else
12981     dwarf2_macro_malformed_definition_complaint (body);
12982 }
12983
12984
12985 static void
12986 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
12987                      char *comp_dir, bfd *abfd,
12988                      struct dwarf2_cu *cu)
12989 {
12990   gdb_byte *mac_ptr, *mac_end;
12991   struct macro_source_file *current_file = 0;
12992   enum dwarf_macinfo_record_type macinfo_type;
12993   int at_commandline;
12994
12995   dwarf2_read_section (dwarf2_per_objfile->objfile,
12996                        &dwarf2_per_objfile->macinfo);
12997   if (dwarf2_per_objfile->macinfo.buffer == NULL)
12998     {
12999       complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13000       return;
13001     }
13002
13003   /* First pass: Find the name of the base filename.
13004      This filename is needed in order to process all macros whose definition
13005      (or undefinition) comes from the command line.  These macros are defined
13006      before the first DW_MACINFO_start_file entry, and yet still need to be
13007      associated to the base file.
13008
13009      To determine the base file name, we scan the macro definitions until we
13010      reach the first DW_MACINFO_start_file entry.  We then initialize
13011      CURRENT_FILE accordingly so that any macro definition found before the
13012      first DW_MACINFO_start_file can still be associated to the base file.  */
13013
13014   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13015   mac_end = dwarf2_per_objfile->macinfo.buffer
13016     + dwarf2_per_objfile->macinfo.size;
13017
13018   do
13019     {
13020       /* Do we at least have room for a macinfo type byte?  */
13021       if (mac_ptr >= mac_end)
13022         {
13023           /* Complaint is printed during the second pass as GDB will probably
13024              stop the first pass earlier upon finding DW_MACINFO_start_file.  */
13025           break;
13026         }
13027
13028       macinfo_type = read_1_byte (abfd, mac_ptr);
13029       mac_ptr++;
13030
13031       switch (macinfo_type)
13032         {
13033           /* A zero macinfo type indicates the end of the macro
13034              information.  */
13035         case 0:
13036           break;
13037
13038         case DW_MACINFO_define:
13039         case DW_MACINFO_undef:
13040           /* Only skip the data by MAC_PTR.  */
13041           {
13042             unsigned int bytes_read;
13043
13044             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13045             mac_ptr += bytes_read;
13046             read_direct_string (abfd, mac_ptr, &bytes_read);
13047             mac_ptr += bytes_read;
13048           }
13049           break;
13050
13051         case DW_MACINFO_start_file:
13052           {
13053             unsigned int bytes_read;
13054             int line, file;
13055
13056             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13057             mac_ptr += bytes_read;
13058             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13059             mac_ptr += bytes_read;
13060
13061             current_file = macro_start_file (file, line, current_file, comp_dir,
13062                                              lh, cu->objfile);
13063           }
13064           break;
13065
13066         case DW_MACINFO_end_file:
13067           /* No data to skip by MAC_PTR.  */
13068           break;
13069
13070         case DW_MACINFO_vendor_ext:
13071           /* Only skip the data by MAC_PTR.  */
13072           {
13073             unsigned int bytes_read;
13074
13075             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13076             mac_ptr += bytes_read;
13077             read_direct_string (abfd, mac_ptr, &bytes_read);
13078             mac_ptr += bytes_read;
13079           }
13080           break;
13081
13082         default:
13083           break;
13084         }
13085     } while (macinfo_type != 0 && current_file == NULL);
13086
13087   /* Second pass: Process all entries.
13088
13089      Use the AT_COMMAND_LINE flag to determine whether we are still processing
13090      command-line macro definitions/undefinitions.  This flag is unset when we
13091      reach the first DW_MACINFO_start_file entry.  */
13092
13093   mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13094
13095   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
13096      GDB is still reading the definitions from command line.  First
13097      DW_MACINFO_start_file will need to be ignored as it was already executed
13098      to create CURRENT_FILE for the main source holding also the command line
13099      definitions.  On first met DW_MACINFO_start_file this flag is reset to
13100      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
13101
13102   at_commandline = 1;
13103
13104   do
13105     {
13106       /* Do we at least have room for a macinfo type byte?  */
13107       if (mac_ptr >= mac_end)
13108         {
13109           dwarf2_macros_too_long_complaint ();
13110           break;
13111         }
13112
13113       macinfo_type = read_1_byte (abfd, mac_ptr);
13114       mac_ptr++;
13115
13116       switch (macinfo_type)
13117         {
13118           /* A zero macinfo type indicates the end of the macro
13119              information.  */
13120         case 0:
13121           break;
13122
13123         case DW_MACINFO_define:
13124         case DW_MACINFO_undef:
13125           {
13126             unsigned int bytes_read;
13127             int line;
13128             char *body;
13129
13130             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13131             mac_ptr += bytes_read;
13132             body = read_direct_string (abfd, mac_ptr, &bytes_read);
13133             mac_ptr += bytes_read;
13134
13135             if (! current_file)
13136               {
13137                 /* DWARF violation as no main source is present.  */
13138                 complaint (&symfile_complaints,
13139                            _("debug info with no main source gives macro %s "
13140                              "on line %d: %s"),
13141                            macinfo_type == DW_MACINFO_define ?
13142                              _("definition") :
13143                                macinfo_type == DW_MACINFO_undef ?
13144                                  _("undefinition") :
13145                                  _("something-or-other"), line, body);
13146                 break;
13147               }
13148             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13149               complaint (&symfile_complaints,
13150                          _("debug info gives %s macro %s with %s line %d: %s"),
13151                          at_commandline ? _("command-line") : _("in-file"),
13152                          macinfo_type == DW_MACINFO_define ?
13153                            _("definition") :
13154                              macinfo_type == DW_MACINFO_undef ?
13155                                _("undefinition") :
13156                                _("something-or-other"),
13157                          line == 0 ? _("zero") : _("non-zero"), line, body);
13158
13159             if (macinfo_type == DW_MACINFO_define)
13160               parse_macro_definition (current_file, line, body);
13161             else if (macinfo_type == DW_MACINFO_undef)
13162               macro_undef (current_file, line, body);
13163           }
13164           break;
13165
13166         case DW_MACINFO_start_file:
13167           {
13168             unsigned int bytes_read;
13169             int line, file;
13170
13171             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13172             mac_ptr += bytes_read;
13173             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13174             mac_ptr += bytes_read;
13175
13176             if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13177               complaint (&symfile_complaints,
13178                          _("debug info gives source %d included "
13179                            "from %s at %s line %d"),
13180                          file, at_commandline ? _("command-line") : _("file"),
13181                          line == 0 ? _("zero") : _("non-zero"), line);
13182
13183             if (at_commandline)
13184               {
13185                 /* This DW_MACINFO_start_file was executed in the pass one.  */
13186                 at_commandline = 0;
13187               }
13188             else
13189               current_file = macro_start_file (file, line,
13190                                                current_file, comp_dir,
13191                                                lh, cu->objfile);
13192           }
13193           break;
13194
13195         case DW_MACINFO_end_file:
13196           if (! current_file)
13197             complaint (&symfile_complaints,
13198                        _("macro debug info has an unmatched `close_file' directive"));
13199           else
13200             {
13201               current_file = current_file->included_by;
13202               if (! current_file)
13203                 {
13204                   enum dwarf_macinfo_record_type next_type;
13205
13206                   /* GCC circa March 2002 doesn't produce the zero
13207                      type byte marking the end of the compilation
13208                      unit.  Complain if it's not there, but exit no
13209                      matter what.  */
13210
13211                   /* Do we at least have room for a macinfo type byte?  */
13212                   if (mac_ptr >= mac_end)
13213                     {
13214                       dwarf2_macros_too_long_complaint ();
13215                       return;
13216                     }
13217
13218                   /* We don't increment mac_ptr here, so this is just
13219                      a look-ahead.  */
13220                   next_type = read_1_byte (abfd, mac_ptr);
13221                   if (next_type != 0)
13222                     complaint (&symfile_complaints,
13223                                _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13224
13225                   return;
13226                 }
13227             }
13228           break;
13229
13230         case DW_MACINFO_vendor_ext:
13231           {
13232             unsigned int bytes_read;
13233             int constant;
13234             char *string;
13235
13236             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13237             mac_ptr += bytes_read;
13238             string = read_direct_string (abfd, mac_ptr, &bytes_read);
13239             mac_ptr += bytes_read;
13240
13241             /* We don't recognize any vendor extensions.  */
13242           }
13243           break;
13244         }
13245     } while (macinfo_type != 0);
13246 }
13247
13248 /* Check if the attribute's form is a DW_FORM_block*
13249    if so return true else false. */
13250 static int
13251 attr_form_is_block (struct attribute *attr)
13252 {
13253   return (attr == NULL ? 0 :
13254       attr->form == DW_FORM_block1
13255       || attr->form == DW_FORM_block2
13256       || attr->form == DW_FORM_block4
13257       || attr->form == DW_FORM_block
13258       || attr->form == DW_FORM_exprloc);
13259 }
13260
13261 /* Return non-zero if ATTR's value is a section offset --- classes
13262    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13263    You may use DW_UNSND (attr) to retrieve such offsets.
13264
13265    Section 7.5.4, "Attribute Encodings", explains that no attribute
13266    may have a value that belongs to more than one of these classes; it
13267    would be ambiguous if we did, because we use the same forms for all
13268    of them.  */
13269 static int
13270 attr_form_is_section_offset (struct attribute *attr)
13271 {
13272   return (attr->form == DW_FORM_data4
13273           || attr->form == DW_FORM_data8
13274           || attr->form == DW_FORM_sec_offset);
13275 }
13276
13277
13278 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13279    zero otherwise.  When this function returns true, you can apply
13280    dwarf2_get_attr_constant_value to it.
13281
13282    However, note that for some attributes you must check
13283    attr_form_is_section_offset before using this test.  DW_FORM_data4
13284    and DW_FORM_data8 are members of both the constant class, and of
13285    the classes that contain offsets into other debug sections
13286    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
13287    that, if an attribute's can be either a constant or one of the
13288    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13289    taken as section offsets, not constants.  */
13290 static int
13291 attr_form_is_constant (struct attribute *attr)
13292 {
13293   switch (attr->form)
13294     {
13295     case DW_FORM_sdata:
13296     case DW_FORM_udata:
13297     case DW_FORM_data1:
13298     case DW_FORM_data2:
13299     case DW_FORM_data4:
13300     case DW_FORM_data8:
13301       return 1;
13302     default:
13303       return 0;
13304     }
13305 }
13306
13307 static void
13308 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13309                              struct dwarf2_cu *cu)
13310 {
13311   if (attr_form_is_section_offset (attr)
13312       /* ".debug_loc" may not exist at all, or the offset may be outside
13313          the section.  If so, fall through to the complaint in the
13314          other branch.  */
13315       && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13316     {
13317       struct dwarf2_loclist_baton *baton;
13318
13319       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13320                              sizeof (struct dwarf2_loclist_baton));
13321       baton->per_cu = cu->per_cu;
13322       gdb_assert (baton->per_cu);
13323
13324       dwarf2_read_section (dwarf2_per_objfile->objfile,
13325                            &dwarf2_per_objfile->loc);
13326
13327       /* We don't know how long the location list is, but make sure we
13328          don't run off the edge of the section.  */
13329       baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13330       baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13331       baton->base_address = cu->base_address;
13332       if (cu->base_known == 0)
13333         complaint (&symfile_complaints,
13334                    _("Location list used without specifying the CU base address."));
13335
13336       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13337       SYMBOL_LOCATION_BATON (sym) = baton;
13338     }
13339   else
13340     {
13341       struct dwarf2_locexpr_baton *baton;
13342
13343       baton = obstack_alloc (&cu->objfile->objfile_obstack,
13344                              sizeof (struct dwarf2_locexpr_baton));
13345       baton->per_cu = cu->per_cu;
13346       gdb_assert (baton->per_cu);
13347
13348       if (attr_form_is_block (attr))
13349         {
13350           /* Note that we're just copying the block's data pointer
13351              here, not the actual data.  We're still pointing into the
13352              info_buffer for SYM's objfile; right now we never release
13353              that buffer, but when we do clean up properly this may
13354              need to change.  */
13355           baton->size = DW_BLOCK (attr)->size;
13356           baton->data = DW_BLOCK (attr)->data;
13357         }
13358       else
13359         {
13360           dwarf2_invalid_attrib_class_complaint ("location description",
13361                                                  SYMBOL_NATURAL_NAME (sym));
13362           baton->size = 0;
13363           baton->data = NULL;
13364         }
13365
13366       SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13367       SYMBOL_LOCATION_BATON (sym) = baton;
13368     }
13369 }
13370
13371 /* Return the OBJFILE associated with the compilation unit CU.  If CU
13372    came from a separate debuginfo file, then the master objfile is
13373    returned.  */
13374
13375 struct objfile *
13376 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13377 {
13378   struct objfile *objfile = per_cu->objfile;
13379
13380   /* Return the master objfile, so that we can report and look up the
13381      correct file containing this variable.  */
13382   if (objfile->separate_debug_objfile_backlink)
13383     objfile = objfile->separate_debug_objfile_backlink;
13384
13385   return objfile;
13386 }
13387
13388 /* Return the address size given in the compilation unit header for CU.  */
13389
13390 CORE_ADDR
13391 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
13392 {
13393   if (per_cu->cu)
13394     return per_cu->cu->header.addr_size;
13395   else
13396     {
13397       /* If the CU is not currently read in, we re-read its header.  */
13398       struct objfile *objfile = per_cu->objfile;
13399       struct dwarf2_per_objfile *per_objfile
13400         = objfile_data (objfile, dwarf2_objfile_data_key);
13401       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13402       struct comp_unit_head cu_header;
13403
13404       memset (&cu_header, 0, sizeof cu_header);
13405       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13406       return cu_header.addr_size;
13407     }
13408 }
13409
13410 /* Return the offset size given in the compilation unit header for CU.  */
13411
13412 int
13413 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
13414 {
13415   if (per_cu->cu)
13416     return per_cu->cu->header.offset_size;
13417   else
13418     {
13419       /* If the CU is not currently read in, we re-read its header.  */
13420       struct objfile *objfile = per_cu->objfile;
13421       struct dwarf2_per_objfile *per_objfile
13422         = objfile_data (objfile, dwarf2_objfile_data_key);
13423       gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
13424       struct comp_unit_head cu_header;
13425
13426       memset (&cu_header, 0, sizeof cu_header);
13427       read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
13428       return cu_header.offset_size;
13429     }
13430 }
13431
13432 /* Return the text offset of the CU.  The returned offset comes from
13433    this CU's objfile.  If this objfile came from a separate debuginfo
13434    file, then the offset may be different from the corresponding
13435    offset in the parent objfile.  */
13436
13437 CORE_ADDR
13438 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
13439 {
13440   struct objfile *objfile = per_cu->objfile;
13441
13442   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13443 }
13444
13445 /* Locate the .debug_info compilation unit from CU's objfile which contains
13446    the DIE at OFFSET.  Raises an error on failure.  */
13447
13448 static struct dwarf2_per_cu_data *
13449 dwarf2_find_containing_comp_unit (unsigned int offset,
13450                                   struct objfile *objfile)
13451 {
13452   struct dwarf2_per_cu_data *this_cu;
13453   int low, high;
13454
13455   low = 0;
13456   high = dwarf2_per_objfile->n_comp_units - 1;
13457   while (high > low)
13458     {
13459       int mid = low + (high - low) / 2;
13460
13461       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
13462         high = mid;
13463       else
13464         low = mid + 1;
13465     }
13466   gdb_assert (low == high);
13467   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
13468     {
13469       if (low == 0)
13470         error (_("Dwarf Error: could not find partial DIE containing "
13471                "offset 0x%lx [in module %s]"),
13472                (long) offset, bfd_get_filename (objfile->obfd));
13473
13474       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
13475       return dwarf2_per_objfile->all_comp_units[low-1];
13476     }
13477   else
13478     {
13479       this_cu = dwarf2_per_objfile->all_comp_units[low];
13480       if (low == dwarf2_per_objfile->n_comp_units - 1
13481           && offset >= this_cu->offset + this_cu->length)
13482         error (_("invalid dwarf2 offset %u"), offset);
13483       gdb_assert (offset < this_cu->offset + this_cu->length);
13484       return this_cu;
13485     }
13486 }
13487
13488 /* Locate the compilation unit from OBJFILE which is located at exactly
13489    OFFSET.  Raises an error on failure.  */
13490
13491 static struct dwarf2_per_cu_data *
13492 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
13493 {
13494   struct dwarf2_per_cu_data *this_cu;
13495
13496   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
13497   if (this_cu->offset != offset)
13498     error (_("no compilation unit with offset %u."), offset);
13499   return this_cu;
13500 }
13501
13502 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it.  */
13503
13504 static struct dwarf2_cu *
13505 alloc_one_comp_unit (struct objfile *objfile)
13506 {
13507   struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
13508   cu->objfile = objfile;
13509   obstack_init (&cu->comp_unit_obstack);
13510   return cu;
13511 }
13512
13513 /* Release one cached compilation unit, CU.  We unlink it from the tree
13514    of compilation units, but we don't remove it from the read_in_chain;
13515    the caller is responsible for that.
13516    NOTE: DATA is a void * because this function is also used as a
13517    cleanup routine.  */
13518
13519 static void
13520 free_one_comp_unit (void *data)
13521 {
13522   struct dwarf2_cu *cu = data;
13523
13524   if (cu->per_cu != NULL)
13525     cu->per_cu->cu = NULL;
13526   cu->per_cu = NULL;
13527
13528   obstack_free (&cu->comp_unit_obstack, NULL);
13529
13530   xfree (cu);
13531 }
13532
13533 /* This cleanup function is passed the address of a dwarf2_cu on the stack
13534    when we're finished with it.  We can't free the pointer itself, but be
13535    sure to unlink it from the cache.  Also release any associated storage
13536    and perform cache maintenance.
13537
13538    Only used during partial symbol parsing.  */
13539
13540 static void
13541 free_stack_comp_unit (void *data)
13542 {
13543   struct dwarf2_cu *cu = data;
13544
13545   obstack_free (&cu->comp_unit_obstack, NULL);
13546   cu->partial_dies = NULL;
13547
13548   if (cu->per_cu != NULL)
13549     {
13550       /* This compilation unit is on the stack in our caller, so we
13551          should not xfree it.  Just unlink it.  */
13552       cu->per_cu->cu = NULL;
13553       cu->per_cu = NULL;
13554
13555       /* If we had a per-cu pointer, then we may have other compilation
13556          units loaded, so age them now.  */
13557       age_cached_comp_units ();
13558     }
13559 }
13560
13561 /* Free all cached compilation units.  */
13562
13563 static void
13564 free_cached_comp_units (void *data)
13565 {
13566   struct dwarf2_per_cu_data *per_cu, **last_chain;
13567
13568   per_cu = dwarf2_per_objfile->read_in_chain;
13569   last_chain = &dwarf2_per_objfile->read_in_chain;
13570   while (per_cu != NULL)
13571     {
13572       struct dwarf2_per_cu_data *next_cu;
13573
13574       next_cu = per_cu->cu->read_in_chain;
13575
13576       free_one_comp_unit (per_cu->cu);
13577       *last_chain = next_cu;
13578
13579       per_cu = next_cu;
13580     }
13581 }
13582
13583 /* Increase the age counter on each cached compilation unit, and free
13584    any that are too old.  */
13585
13586 static void
13587 age_cached_comp_units (void)
13588 {
13589   struct dwarf2_per_cu_data *per_cu, **last_chain;
13590
13591   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
13592   per_cu = dwarf2_per_objfile->read_in_chain;
13593   while (per_cu != NULL)
13594     {
13595       per_cu->cu->last_used ++;
13596       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
13597         dwarf2_mark (per_cu->cu);
13598       per_cu = per_cu->cu->read_in_chain;
13599     }
13600
13601   per_cu = dwarf2_per_objfile->read_in_chain;
13602   last_chain = &dwarf2_per_objfile->read_in_chain;
13603   while (per_cu != NULL)
13604     {
13605       struct dwarf2_per_cu_data *next_cu;
13606
13607       next_cu = per_cu->cu->read_in_chain;
13608
13609       if (!per_cu->cu->mark)
13610         {
13611           free_one_comp_unit (per_cu->cu);
13612           *last_chain = next_cu;
13613         }
13614       else
13615         last_chain = &per_cu->cu->read_in_chain;
13616
13617       per_cu = next_cu;
13618     }
13619 }
13620
13621 /* Remove a single compilation unit from the cache.  */
13622
13623 static void
13624 free_one_cached_comp_unit (void *target_cu)
13625 {
13626   struct dwarf2_per_cu_data *per_cu, **last_chain;
13627
13628   per_cu = dwarf2_per_objfile->read_in_chain;
13629   last_chain = &dwarf2_per_objfile->read_in_chain;
13630   while (per_cu != NULL)
13631     {
13632       struct dwarf2_per_cu_data *next_cu;
13633
13634       next_cu = per_cu->cu->read_in_chain;
13635
13636       if (per_cu->cu == target_cu)
13637         {
13638           free_one_comp_unit (per_cu->cu);
13639           *last_chain = next_cu;
13640           break;
13641         }
13642       else
13643         last_chain = &per_cu->cu->read_in_chain;
13644
13645       per_cu = next_cu;
13646     }
13647 }
13648
13649 /* Release all extra memory associated with OBJFILE.  */
13650
13651 void
13652 dwarf2_free_objfile (struct objfile *objfile)
13653 {
13654   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
13655
13656   if (dwarf2_per_objfile == NULL)
13657     return;
13658
13659   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
13660   free_cached_comp_units (NULL);
13661
13662   if (dwarf2_per_objfile->using_index)
13663     {
13664       int i;
13665
13666       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
13667         {
13668           int j;
13669           struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
13670
13671           if (!cu->v.quick->lines)
13672             continue;
13673
13674           for (j = 0; j < cu->v.quick->lines->num_file_names; ++j)
13675             {
13676               if (cu->v.quick->file_names)
13677                 xfree ((void *) cu->v.quick->file_names[j]);
13678               if (cu->v.quick->full_names)
13679                 xfree ((void *) cu->v.quick->full_names[j]);
13680             }
13681
13682           free_line_header (cu->v.quick->lines);
13683         }
13684     }
13685
13686   /* Everything else should be on the objfile obstack.  */
13687 }
13688
13689 /* A pair of DIE offset and GDB type pointer.  We store these
13690    in a hash table separate from the DIEs, and preserve them
13691    when the DIEs are flushed out of cache.  */
13692
13693 struct dwarf2_offset_and_type
13694 {
13695   unsigned int offset;
13696   struct type *type;
13697 };
13698
13699 /* Hash function for a dwarf2_offset_and_type.  */
13700
13701 static hashval_t
13702 offset_and_type_hash (const void *item)
13703 {
13704   const struct dwarf2_offset_and_type *ofs = item;
13705
13706   return ofs->offset;
13707 }
13708
13709 /* Equality function for a dwarf2_offset_and_type.  */
13710
13711 static int
13712 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
13713 {
13714   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
13715   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
13716
13717   return ofs_lhs->offset == ofs_rhs->offset;
13718 }
13719
13720 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
13721    table if necessary.  For convenience, return TYPE.
13722
13723    The DIEs reading must have careful ordering to:
13724     * Not cause infite loops trying to read in DIEs as a prerequisite for
13725       reading current DIE.
13726     * Not trying to dereference contents of still incompletely read in types
13727       while reading in other DIEs.
13728     * Enable referencing still incompletely read in types just by a pointer to
13729       the type without accessing its fields.
13730
13731    Therefore caller should follow these rules:
13732      * Try to fetch any prerequisite types we may need to build this DIE type
13733        before building the type and calling set_die_type.
13734      * After building typer call set_die_type for current DIE as soon as
13735        possible before fetching more types to complete the current type.
13736      * Make the type as complete as possible before fetching more types.  */
13737
13738 static struct type *
13739 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13740 {
13741   struct dwarf2_offset_and_type **slot, ofs;
13742
13743   /* For Ada types, make sure that the gnat-specific data is always
13744      initialized (if not already set).  There are a few types where
13745      we should not be doing so, because the type-specific area is
13746      already used to hold some other piece of info (eg: TYPE_CODE_FLT
13747      where the type-specific area is used to store the floatformat).
13748      But this is not a problem, because the gnat-specific information
13749      is actually not needed for these types.  */
13750   if (need_gnat_info (cu)
13751       && TYPE_CODE (type) != TYPE_CODE_FUNC
13752       && TYPE_CODE (type) != TYPE_CODE_FLT
13753       && !HAVE_GNAT_AUX_INFO (type))
13754     INIT_GNAT_SPECIFIC (type);
13755
13756   if (cu->type_hash == NULL)
13757     {
13758       gdb_assert (cu->per_cu != NULL);
13759       cu->per_cu->type_hash
13760         = htab_create_alloc_ex (cu->header.length / 24,
13761                                 offset_and_type_hash,
13762                                 offset_and_type_eq,
13763                                 NULL,
13764                                 &cu->objfile->objfile_obstack,
13765                                 hashtab_obstack_allocate,
13766                                 dummy_obstack_deallocate);
13767       cu->type_hash = cu->per_cu->type_hash;
13768     }
13769
13770   ofs.offset = die->offset;
13771   ofs.type = type;
13772   slot = (struct dwarf2_offset_and_type **)
13773     htab_find_slot_with_hash (cu->type_hash, &ofs, ofs.offset, INSERT);
13774   if (*slot)
13775     complaint (&symfile_complaints,
13776                _("A problem internal to GDB: DIE 0x%x has type already set"),
13777                die->offset);
13778   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
13779   **slot = ofs;
13780   return type;
13781 }
13782
13783 /* Find the type for DIE in CU's type_hash, or return NULL if DIE does
13784    not have a saved type.  */
13785
13786 static struct type *
13787 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
13788 {
13789   struct dwarf2_offset_and_type *slot, ofs;
13790   htab_t type_hash = cu->type_hash;
13791
13792   if (type_hash == NULL)
13793     return NULL;
13794
13795   ofs.offset = die->offset;
13796   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
13797   if (slot)
13798     return slot->type;
13799   else
13800     return NULL;
13801 }
13802
13803 /* Add a dependence relationship from CU to REF_PER_CU.  */
13804
13805 static void
13806 dwarf2_add_dependence (struct dwarf2_cu *cu,
13807                        struct dwarf2_per_cu_data *ref_per_cu)
13808 {
13809   void **slot;
13810
13811   if (cu->dependencies == NULL)
13812     cu->dependencies
13813       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
13814                               NULL, &cu->comp_unit_obstack,
13815                               hashtab_obstack_allocate,
13816                               dummy_obstack_deallocate);
13817
13818   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
13819   if (*slot == NULL)
13820     *slot = ref_per_cu;
13821 }
13822
13823 /* Subroutine of dwarf2_mark to pass to htab_traverse.
13824    Set the mark field in every compilation unit in the
13825    cache that we must keep because we are keeping CU.  */
13826
13827 static int
13828 dwarf2_mark_helper (void **slot, void *data)
13829 {
13830   struct dwarf2_per_cu_data *per_cu;
13831
13832   per_cu = (struct dwarf2_per_cu_data *) *slot;
13833   if (per_cu->cu->mark)
13834     return 1;
13835   per_cu->cu->mark = 1;
13836
13837   if (per_cu->cu->dependencies != NULL)
13838     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
13839
13840   return 1;
13841 }
13842
13843 /* Set the mark field in CU and in every other compilation unit in the
13844    cache that we must keep because we are keeping CU.  */
13845
13846 static void
13847 dwarf2_mark (struct dwarf2_cu *cu)
13848 {
13849   if (cu->mark)
13850     return;
13851   cu->mark = 1;
13852   if (cu->dependencies != NULL)
13853     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
13854 }
13855
13856 static void
13857 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
13858 {
13859   while (per_cu)
13860     {
13861       per_cu->cu->mark = 0;
13862       per_cu = per_cu->cu->read_in_chain;
13863     }
13864 }
13865
13866 /* Trivial hash function for partial_die_info: the hash value of a DIE
13867    is its offset in .debug_info for this objfile.  */
13868
13869 static hashval_t
13870 partial_die_hash (const void *item)
13871 {
13872   const struct partial_die_info *part_die = item;
13873
13874   return part_die->offset;
13875 }
13876
13877 /* Trivial comparison function for partial_die_info structures: two DIEs
13878    are equal if they have the same offset.  */
13879
13880 static int
13881 partial_die_eq (const void *item_lhs, const void *item_rhs)
13882 {
13883   const struct partial_die_info *part_die_lhs = item_lhs;
13884   const struct partial_die_info *part_die_rhs = item_rhs;
13885
13886   return part_die_lhs->offset == part_die_rhs->offset;
13887 }
13888
13889 static struct cmd_list_element *set_dwarf2_cmdlist;
13890 static struct cmd_list_element *show_dwarf2_cmdlist;
13891
13892 static void
13893 set_dwarf2_cmd (char *args, int from_tty)
13894 {
13895   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
13896 }
13897
13898 static void
13899 show_dwarf2_cmd (char *args, int from_tty)
13900 {
13901   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
13902 }
13903
13904 /* If section described by INFO was mmapped, munmap it now.  */
13905
13906 static void
13907 munmap_section_buffer (struct dwarf2_section_info *info)
13908 {
13909   if (info->was_mmapped)
13910     {
13911 #ifdef HAVE_MMAP
13912       intptr_t begin = (intptr_t) info->buffer;
13913       intptr_t map_begin = begin & ~(pagesize - 1);
13914       size_t map_length = info->size + begin - map_begin;
13915
13916       gdb_assert (munmap ((void *) map_begin, map_length) == 0);
13917 #else
13918       /* Without HAVE_MMAP, we should never be here to begin with.  */
13919       gdb_assert (0);
13920 #endif
13921     }
13922 }
13923
13924 /* munmap debug sections for OBJFILE, if necessary.  */
13925
13926 static void
13927 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
13928 {
13929   struct dwarf2_per_objfile *data = d;
13930
13931   munmap_section_buffer (&data->info);
13932   munmap_section_buffer (&data->abbrev);
13933   munmap_section_buffer (&data->line);
13934   munmap_section_buffer (&data->str);
13935   munmap_section_buffer (&data->macinfo);
13936   munmap_section_buffer (&data->ranges);
13937   munmap_section_buffer (&data->loc);
13938   munmap_section_buffer (&data->frame);
13939   munmap_section_buffer (&data->eh_frame);
13940   munmap_section_buffer (&data->gdb_index);
13941 }
13942
13943 \f
13944
13945 /* The contents of the hash table we create when building the string
13946    table.  */
13947 struct strtab_entry
13948 {
13949   offset_type offset;
13950   const char *str;
13951 };
13952
13953 /* Hash function for a strtab_entry.  */
13954 static hashval_t
13955 hash_strtab_entry (const void *e)
13956 {
13957   const struct strtab_entry *entry = e;
13958   return mapped_index_string_hash (entry->str);
13959 }
13960
13961 /* Equality function for a strtab_entry.  */
13962 static int
13963 eq_strtab_entry (const void *a, const void *b)
13964 {
13965   const struct strtab_entry *ea = a;
13966   const struct strtab_entry *eb = b;
13967   return !strcmp (ea->str, eb->str);
13968 }
13969
13970 /* Create a strtab_entry hash table.  */
13971 static htab_t
13972 create_strtab (void)
13973 {
13974   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
13975                             xfree, xcalloc, xfree);
13976 }
13977
13978 /* Add a string to the constant pool.  Return the string's offset in
13979    host order.  */
13980 static offset_type
13981 add_string (htab_t table, struct obstack *cpool, const char *str)
13982 {
13983   void **slot;
13984   struct strtab_entry entry;
13985   struct strtab_entry *result;
13986
13987   entry.str = str;
13988   slot = htab_find_slot (table, &entry, INSERT);
13989   if (*slot)
13990     result = *slot;
13991   else
13992     {
13993       result = XNEW (struct strtab_entry);
13994       result->offset = obstack_object_size (cpool);
13995       result->str = str;
13996       obstack_grow_str0 (cpool, str);
13997       *slot = result;
13998     }
13999   return result->offset;
14000 }
14001
14002 /* An entry in the symbol table.  */
14003 struct symtab_index_entry
14004 {
14005   /* The name of the symbol.  */
14006   const char *name;
14007   /* The offset of the name in the constant pool.  */
14008   offset_type index_offset;
14009   /* A sorted vector of the indices of all the CUs that hold an object
14010      of this name.  */
14011   VEC (offset_type) *cu_indices;
14012 };
14013
14014 /* The symbol table.  This is a power-of-2-sized hash table.  */
14015 struct mapped_symtab
14016 {
14017   offset_type n_elements;
14018   offset_type size;
14019   struct symtab_index_entry **data;
14020 };
14021
14022 /* Hash function for a symtab_index_entry.  */
14023 static hashval_t
14024 hash_symtab_entry (const void *e)
14025 {
14026   const struct symtab_index_entry *entry = e;
14027   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14028                          sizeof (offset_type) * VEC_length (offset_type,
14029                                                             entry->cu_indices),
14030                          0);
14031 }
14032
14033 /* Equality function for a symtab_index_entry.  */
14034 static int
14035 eq_symtab_entry (const void *a, const void *b)
14036 {
14037   const struct symtab_index_entry *ea = a;
14038   const struct symtab_index_entry *eb = b;
14039   int len = VEC_length (offset_type, ea->cu_indices);
14040   if (len != VEC_length (offset_type, eb->cu_indices))
14041     return 0;
14042   return !memcmp (VEC_address (offset_type, ea->cu_indices),
14043                   VEC_address (offset_type, eb->cu_indices),
14044                   sizeof (offset_type) * len);
14045 }
14046
14047 /* Destroy a symtab_index_entry.  */
14048 static void
14049 delete_symtab_entry (void *p)
14050 {
14051   struct symtab_index_entry *entry = p;
14052   VEC_free (offset_type, entry->cu_indices);
14053   xfree (entry);
14054 }
14055
14056 /* Create a hash table holding symtab_index_entry objects.  */
14057 static htab_t
14058 create_index_table (void)
14059 {
14060   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14061                             delete_symtab_entry, xcalloc, xfree);
14062 }
14063
14064 /* Create a new mapped symtab object.  */
14065 static struct mapped_symtab *
14066 create_mapped_symtab (void)
14067 {
14068   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14069   symtab->n_elements = 0;
14070   symtab->size = 1024;
14071   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14072   return symtab;
14073 }
14074
14075 /* Destroy a mapped_symtab.  */
14076 static void
14077 cleanup_mapped_symtab (void *p)
14078 {
14079   struct mapped_symtab *symtab = p;
14080   /* The contents of the array are freed when the other hash table is
14081      destroyed.  */
14082   xfree (symtab->data);
14083   xfree (symtab);
14084 }
14085
14086 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
14087    the slot.  */
14088 static struct symtab_index_entry **
14089 find_slot (struct mapped_symtab *symtab, const char *name)
14090 {
14091   offset_type index, step, hash = mapped_index_string_hash (name);
14092
14093   index = hash & (symtab->size - 1);
14094   step = ((hash * 17) & (symtab->size - 1)) | 1;
14095
14096   for (;;)
14097     {
14098       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14099         return &symtab->data[index];
14100       index = (index + step) & (symtab->size - 1);
14101     }
14102 }
14103
14104 /* Expand SYMTAB's hash table.  */
14105 static void
14106 hash_expand (struct mapped_symtab *symtab)
14107 {
14108   offset_type old_size = symtab->size;
14109   offset_type i;
14110   struct symtab_index_entry **old_entries = symtab->data;
14111
14112   symtab->size *= 2;
14113   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14114
14115   for (i = 0; i < old_size; ++i)
14116     {
14117       if (old_entries[i])
14118         {
14119           struct symtab_index_entry **slot = find_slot (symtab,
14120                                                         old_entries[i]->name);
14121           *slot = old_entries[i];
14122         }
14123     }
14124
14125   xfree (old_entries);
14126 }
14127
14128 /* Add an entry to SYMTAB.  NAME is the name of the symbol.  CU_INDEX
14129    is the index of the CU in which the symbol appears.  */
14130 static void
14131 add_index_entry (struct mapped_symtab *symtab, const char *name,
14132                  offset_type cu_index)
14133 {
14134   struct symtab_index_entry **slot;
14135
14136   ++symtab->n_elements;
14137   if (4 * symtab->n_elements / 3 >= symtab->size)
14138     hash_expand (symtab);
14139
14140   slot = find_slot (symtab, name);
14141   if (!*slot)
14142     {
14143       *slot = XNEW (struct symtab_index_entry);
14144       (*slot)->name = name;
14145       (*slot)->cu_indices = NULL;
14146     }
14147   /* Don't push an index twice.  Due to how we add entries we only
14148      have to check the last one.  */ 
14149   if (VEC_empty (offset_type, (*slot)->cu_indices)
14150       || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14151     VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14152 }
14153
14154 /* Add a vector of indices to the constant pool.  */
14155 static offset_type
14156 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14157                       struct symtab_index_entry *entry)
14158 {
14159   void **slot;
14160
14161   slot = htab_find_slot (index_table, entry, INSERT);
14162   if (!*slot)
14163     {
14164       offset_type len = VEC_length (offset_type, entry->cu_indices);
14165       offset_type val = MAYBE_SWAP (len);
14166       offset_type iter;
14167       int i;
14168
14169       *slot = entry;
14170       entry->index_offset = obstack_object_size (cpool);
14171
14172       obstack_grow (cpool, &val, sizeof (val));
14173       for (i = 0;
14174            VEC_iterate (offset_type, entry->cu_indices, i, iter);
14175            ++i)
14176         {
14177           val = MAYBE_SWAP (iter);
14178           obstack_grow (cpool, &val, sizeof (val));
14179         }
14180     }
14181   else
14182     {
14183       struct symtab_index_entry *old_entry = *slot;
14184       entry->index_offset = old_entry->index_offset;
14185       entry = old_entry;
14186     }
14187   return entry->index_offset;
14188 }
14189
14190 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14191    constant pool entries going into the obstack CPOOL.  */
14192 static void
14193 write_hash_table (struct mapped_symtab *symtab,
14194                   struct obstack *output, struct obstack *cpool)
14195 {
14196   offset_type i;
14197   htab_t index_table;
14198   htab_t str_table;
14199
14200   index_table = create_index_table ();
14201   str_table = create_strtab ();
14202   /* We add all the index vectors to the constant pool first, to
14203      ensure alignment is ok.  */
14204   for (i = 0; i < symtab->size; ++i)
14205     {
14206       if (symtab->data[i])
14207         add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14208     }
14209
14210   /* Now write out the hash table.  */
14211   for (i = 0; i < symtab->size; ++i)
14212     {
14213       offset_type str_off, vec_off;
14214
14215       if (symtab->data[i])
14216         {
14217           str_off = add_string (str_table, cpool, symtab->data[i]->name);
14218           vec_off = symtab->data[i]->index_offset;
14219         }
14220       else
14221         {
14222           /* While 0 is a valid constant pool index, it is not valid
14223              to have 0 for both offsets.  */
14224           str_off = 0;
14225           vec_off = 0;
14226         }
14227
14228       str_off = MAYBE_SWAP (str_off);
14229       vec_off = MAYBE_SWAP (vec_off);
14230
14231       obstack_grow (output, &str_off, sizeof (str_off));
14232       obstack_grow (output, &vec_off, sizeof (vec_off));
14233     }
14234
14235   htab_delete (str_table);
14236   htab_delete (index_table);
14237 }
14238
14239 /* Write an address entry to ADDR_OBSTACK.  The addresses are taken
14240    from PST; CU_INDEX is the index of the CU in the vector of all
14241    CUs.  */
14242 static void
14243 add_address_entry (struct objfile *objfile,
14244                    struct obstack *addr_obstack, struct partial_symtab *pst,
14245                    unsigned int cu_index)
14246 {
14247   offset_type offset;
14248   char addr[8];
14249   CORE_ADDR baseaddr;
14250
14251   /* Don't bother recording empty ranges.  */
14252   if (pst->textlow == pst->texthigh)
14253     return;
14254
14255   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14256
14257   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14258   obstack_grow (addr_obstack, addr, 8);
14259   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14260   obstack_grow (addr_obstack, addr, 8);
14261   offset = MAYBE_SWAP (cu_index);
14262   obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14263 }
14264
14265 /* Add a list of partial symbols to SYMTAB.  */
14266 static void
14267 write_psymbols (struct mapped_symtab *symtab,
14268                 struct partial_symbol **psymp,
14269                 int count,
14270                 offset_type cu_index)
14271 {
14272   for (; count-- > 0; ++psymp)
14273     {
14274       if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14275         error (_("Ada is not currently supported by the index"));
14276       add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14277     }
14278 }
14279
14280 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
14281    exception if there is an error.  */
14282 static void
14283 write_obstack (FILE *file, struct obstack *obstack)
14284 {
14285   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14286               file)
14287       != obstack_object_size (obstack))
14288     error (_("couldn't data write to file"));
14289 }
14290
14291 /* Unlink a file if the argument is not NULL.  */
14292 static void
14293 unlink_if_set (void *p)
14294 {
14295   char **filename = p;
14296   if (*filename)
14297     unlink (*filename);
14298 }
14299
14300 /* A helper struct used when iterating over debug_types.  */
14301 struct signatured_type_index_data
14302 {
14303   struct objfile *objfile;
14304   struct mapped_symtab *symtab;
14305   struct obstack *types_list;
14306   int cu_index;
14307 };
14308
14309 /* A helper function that writes a single signatured_type to an
14310    obstack.  */
14311 static int
14312 write_one_signatured_type (void **slot, void *d)
14313 {
14314   struct signatured_type_index_data *info = d;
14315   struct signatured_type *entry = (struct signatured_type *) *slot;
14316   struct dwarf2_per_cu_data *cu = &entry->per_cu;
14317   struct partial_symtab *psymtab = cu->v.psymtab;
14318   gdb_byte val[8];
14319
14320   write_psymbols (info->symtab,
14321                   info->objfile->global_psymbols.list + psymtab->globals_offset,
14322                   psymtab->n_global_syms, info->cu_index);
14323   write_psymbols (info->symtab,
14324                   info->objfile->static_psymbols.list + psymtab->statics_offset,
14325                   psymtab->n_static_syms, info->cu_index);
14326
14327   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14328   obstack_grow (info->types_list, val, 8);
14329   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14330   obstack_grow (info->types_list, val, 8);
14331   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14332   obstack_grow (info->types_list, val, 8);
14333
14334   ++info->cu_index;
14335
14336   return 1;
14337 }
14338
14339 /* Create an index file for OBJFILE in the directory DIR.  */
14340 static void
14341 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14342 {
14343   struct cleanup *cleanup;
14344   char *filename, *cleanup_filename;
14345   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14346   struct obstack cu_list, types_cu_list;
14347   int i;
14348   FILE *out_file;
14349   struct mapped_symtab *symtab;
14350   offset_type val, size_of_contents, total_len;
14351   struct stat st;
14352   char buf[8];
14353
14354   if (!objfile->psymtabs)
14355     return;
14356   if (dwarf2_per_objfile->using_index)
14357     error (_("Cannot use an index to create the index"));
14358
14359   if (stat (objfile->name, &st) < 0)
14360     perror_with_name (_("Could not stat"));
14361
14362   filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
14363                      INDEX_SUFFIX, (char *) NULL);
14364   cleanup = make_cleanup (xfree, filename);
14365
14366   out_file = fopen (filename, "wb");
14367   if (!out_file)
14368     error (_("Can't open `%s' for writing"), filename);
14369
14370   cleanup_filename = filename;
14371   make_cleanup (unlink_if_set, &cleanup_filename);
14372
14373   symtab = create_mapped_symtab ();
14374   make_cleanup (cleanup_mapped_symtab, symtab);
14375
14376   obstack_init (&addr_obstack);
14377   make_cleanup_obstack_free (&addr_obstack);
14378
14379   obstack_init (&cu_list);
14380   make_cleanup_obstack_free (&cu_list);
14381
14382   obstack_init (&types_cu_list);
14383   make_cleanup_obstack_free (&types_cu_list);
14384
14385   /* The list is already sorted, so we don't need to do additional
14386      work here.  Also, the debug_types entries do not appear in
14387      all_comp_units, but only in their own hash table.  */
14388   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14389     {
14390       struct dwarf2_per_cu_data *cu = dwarf2_per_objfile->all_comp_units[i];
14391       struct partial_symtab *psymtab = cu->v.psymtab;
14392       gdb_byte val[8];
14393
14394       write_psymbols (symtab,
14395                       objfile->global_psymbols.list + psymtab->globals_offset,
14396                       psymtab->n_global_syms, i);
14397       write_psymbols (symtab,
14398                       objfile->static_psymbols.list + psymtab->statics_offset,
14399                       psymtab->n_static_syms, i);
14400
14401       add_address_entry (objfile, &addr_obstack, psymtab, i);
14402
14403       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->offset);
14404       obstack_grow (&cu_list, val, 8);
14405       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, cu->length);
14406       obstack_grow (&cu_list, val, 8);
14407     }
14408
14409   /* Write out the .debug_type entries, if any.  */
14410   if (dwarf2_per_objfile->signatured_types)
14411     {
14412       struct signatured_type_index_data sig_data;
14413
14414       sig_data.objfile = objfile;
14415       sig_data.symtab = symtab;
14416       sig_data.types_list = &types_cu_list;
14417       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
14418       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
14419                               write_one_signatured_type, &sig_data);
14420     }
14421
14422   obstack_init (&constant_pool);
14423   make_cleanup_obstack_free (&constant_pool);
14424   obstack_init (&symtab_obstack);
14425   make_cleanup_obstack_free (&symtab_obstack);
14426   write_hash_table (symtab, &symtab_obstack, &constant_pool);
14427
14428   obstack_init (&contents);
14429   make_cleanup_obstack_free (&contents);
14430   size_of_contents = 6 * sizeof (offset_type);
14431   total_len = size_of_contents;
14432
14433   /* The version number.  */
14434   val = MAYBE_SWAP (2);
14435   obstack_grow (&contents, &val, sizeof (val));
14436
14437   /* The offset of the CU list from the start of the file.  */
14438   val = MAYBE_SWAP (total_len);
14439   obstack_grow (&contents, &val, sizeof (val));
14440   total_len += obstack_object_size (&cu_list);
14441
14442   /* The offset of the types CU list from the start of the file.  */
14443   val = MAYBE_SWAP (total_len);
14444   obstack_grow (&contents, &val, sizeof (val));
14445   total_len += obstack_object_size (&types_cu_list);
14446
14447   /* The offset of the address table from the start of the file.  */
14448   val = MAYBE_SWAP (total_len);
14449   obstack_grow (&contents, &val, sizeof (val));
14450   total_len += obstack_object_size (&addr_obstack);
14451
14452   /* The offset of the symbol table from the start of the file.  */
14453   val = MAYBE_SWAP (total_len);
14454   obstack_grow (&contents, &val, sizeof (val));
14455   total_len += obstack_object_size (&symtab_obstack);
14456
14457   /* The offset of the constant pool from the start of the file.  */
14458   val = MAYBE_SWAP (total_len);
14459   obstack_grow (&contents, &val, sizeof (val));
14460   total_len += obstack_object_size (&constant_pool);
14461
14462   gdb_assert (obstack_object_size (&contents) == size_of_contents);
14463
14464   write_obstack (out_file, &contents);
14465   write_obstack (out_file, &cu_list);
14466   write_obstack (out_file, &types_cu_list);
14467   write_obstack (out_file, &addr_obstack);
14468   write_obstack (out_file, &symtab_obstack);
14469   write_obstack (out_file, &constant_pool);
14470
14471   fclose (out_file);
14472
14473   /* We want to keep the file, so we set cleanup_filename to NULL
14474      here.  See unlink_if_set.  */
14475   cleanup_filename = NULL;
14476
14477   do_cleanups (cleanup);
14478 }
14479
14480 /* The mapped index file format is designed to be directly mmap()able
14481    on any architecture.  In most cases, a datum is represented using a
14482    little-endian 32-bit integer value, called an offset_type.  Big
14483    endian machines must byte-swap the values before using them.
14484    Exceptions to this rule are noted.  The data is laid out such that
14485    alignment is always respected.
14486
14487    A mapped index consists of several sections.
14488
14489    1. The file header.  This is a sequence of values, of offset_type
14490    unless otherwise noted:
14491    [0] The version number.  Currently 1 or 2.  The differences are
14492    noted below.  Version 1 did not account for .debug_types sections;
14493    the presence of a .debug_types section invalidates any version 1
14494    index that may exist.
14495    [1] The offset, from the start of the file, of the CU list.
14496    [1.5] In version 2, the offset, from the start of the file, of the
14497    types CU list.  This offset does not appear in version 1.  Note
14498    that this can be empty, in which case this offset will be equal to
14499    the next offset.
14500    [2] The offset, from the start of the file, of the address section.
14501    [3] The offset, from the start of the file, of the symbol table.
14502    [4] The offset, from the start of the file, of the constant pool.
14503
14504    2. The CU list.  This is a sequence of pairs of 64-bit
14505    little-endian values, sorted by the CU offset.  The first element
14506    in each pair is the offset of a CU in the .debug_info section.  The
14507    second element in each pair is the length of that CU.  References
14508    to a CU elsewhere in the map are done using a CU index, which is
14509    just the 0-based index into this table.  Note that if there are
14510    type CUs, then conceptually CUs and type CUs form a single list for
14511    the purposes of CU indices.
14512
14513    2.5 The types CU list.  This does not appear in a version 1 index.
14514    This is a sequence of triplets of 64-bit little-endian values.  In
14515    a triplet, the first value is the CU offset, the second value is
14516    the type offset in the CU, and the third value is the type
14517    signature.  The types CU list is not sorted.
14518
14519    3. The address section.  The address section consists of a sequence
14520    of address entries.  Each address entry has three elements.
14521    [0] The low address.  This is a 64-bit little-endian value.
14522    [1] The high address.  This is a 64-bit little-endian value.
14523    [2] The CU index.  This is an offset_type value.
14524
14525    4. The symbol table.  This is a hash table.  The size of the hash
14526    table is always a power of 2.  The initial hash and the step are
14527    currently defined by the `find_slot' function.
14528
14529    Each slot in the hash table consists of a pair of offset_type
14530    values.  The first value is the offset of the symbol's name in the
14531    constant pool.  The second value is the offset of the CU vector in
14532    the constant pool.
14533
14534    If both values are 0, then this slot in the hash table is empty.
14535    This is ok because while 0 is a valid constant pool index, it
14536    cannot be a valid index for both a string and a CU vector.
14537
14538    A string in the constant pool is stored as a \0-terminated string,
14539    as you'd expect.
14540
14541    A CU vector in the constant pool is a sequence of offset_type
14542    values.  The first value is the number of CU indices in the vector.
14543    Each subsequent value is the index of a CU in the CU list.  This
14544    element in the hash table is used to indicate which CUs define the
14545    symbol.
14546
14547    5. The constant pool.  This is simply a bunch of bytes.  It is
14548    organized so that alignment is correct: CU vectors are stored
14549    first, followed by strings.  */
14550 static void
14551 save_gdb_index_command (char *arg, int from_tty)
14552 {
14553   struct objfile *objfile;
14554
14555   if (!arg || !*arg)
14556     error (_("usage: save gdb-index DIRECTORY"));
14557
14558   ALL_OBJFILES (objfile)
14559   {
14560     struct stat st;
14561
14562     /* If the objfile does not correspond to an actual file, skip it.  */
14563     if (stat (objfile->name, &st) < 0)
14564       continue;
14565
14566     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14567     if (dwarf2_per_objfile)
14568       {
14569         volatile struct gdb_exception except;
14570
14571         TRY_CATCH (except, RETURN_MASK_ERROR)
14572           {
14573             write_psymtabs_to_index (objfile, arg);
14574           }
14575         if (except.reason < 0)
14576           exception_fprintf (gdb_stderr, except,
14577                              _("Error while writing index for `%s': "),
14578                              objfile->name);
14579       }
14580   }
14581 }
14582
14583 \f
14584
14585 int dwarf2_always_disassemble;
14586
14587 static void
14588 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
14589                                 struct cmd_list_element *c, const char *value)
14590 {
14591   fprintf_filtered (file, _("\
14592 Whether to always disassemble DWARF expressions is %s.\n"),
14593                     value);
14594 }
14595
14596 void _initialize_dwarf2_read (void);
14597
14598 void
14599 _initialize_dwarf2_read (void)
14600 {
14601   struct cmd_list_element *c;
14602
14603   dwarf2_objfile_data_key
14604     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
14605
14606   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
14607 Set DWARF 2 specific variables.\n\
14608 Configure DWARF 2 variables such as the cache size"),
14609                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
14610                   0/*allow-unknown*/, &maintenance_set_cmdlist);
14611
14612   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
14613 Show DWARF 2 specific variables\n\
14614 Show DWARF 2 variables such as the cache size"),
14615                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
14616                   0/*allow-unknown*/, &maintenance_show_cmdlist);
14617
14618   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
14619                             &dwarf2_max_cache_age, _("\
14620 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
14621 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
14622 A higher limit means that cached compilation units will be stored\n\
14623 in memory longer, and more total memory will be used.  Zero disables\n\
14624 caching, which can slow down startup."),
14625                             NULL,
14626                             show_dwarf2_max_cache_age,
14627                             &set_dwarf2_cmdlist,
14628                             &show_dwarf2_cmdlist);
14629
14630   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
14631                            &dwarf2_always_disassemble, _("\
14632 Set whether `info address' always disassembles DWARF expressions."), _("\
14633 Show whether `info address' always disassembles DWARF expressions."), _("\
14634 When enabled, DWARF expressions are always printed in an assembly-like\n\
14635 syntax.  When disabled, expressions will be printed in a more\n\
14636 conversational style, when possible."),
14637                            NULL,
14638                            show_dwarf2_always_disassemble,
14639                            &set_dwarf2_cmdlist,
14640                            &show_dwarf2_cmdlist);
14641
14642   add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
14643 Set debugging of the dwarf2 DIE reader."), _("\
14644 Show debugging of the dwarf2 DIE reader."), _("\
14645 When enabled (non-zero), DIEs are dumped after they are read in.\n\
14646 The value is the maximum depth to print."),
14647                             NULL,
14648                             NULL,
14649                             &setdebuglist, &showdebuglist);
14650
14651   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
14652                _("Save a .gdb-index file"),
14653                &save_cmdlist);
14654   set_cmd_completer (c, filename_completer);
14655 }