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