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