* dwarf2read.c (read_subrange_type): Add comment for variable
[external/binutils.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004
4    Free Software Foundation, Inc.
5
6    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
7    Inc.  with support from Florida State University (under contract
8    with the Ada Joint Program Office), and Silicon Graphics, Inc.
9    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
10    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
11    support in dwarfread.c
12
13    This file is part of GDB.
14
15    This program is free software; you can redistribute it and/or modify
16    it under the terms of the GNU General Public License as published by
17    the Free Software Foundation; either version 2 of the License, or (at
18    your option) any later version.
19
20    This program is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 59 Temple Place - Suite 330,
28    Boston, MA 02111-1307, USA.  */
29
30 #include "defs.h"
31 #include "bfd.h"
32 #include "symtab.h"
33 #include "gdbtypes.h"
34 #include "objfiles.h"
35 #include "elf/dwarf2.h"
36 #include "buildsym.h"
37 #include "demangle.h"
38 #include "expression.h"
39 #include "filenames.h"  /* for DOSish file names */
40 #include "macrotab.h"
41 #include "language.h"
42 #include "complaints.h"
43 #include "bcache.h"
44 #include "dwarf2expr.h"
45 #include "dwarf2loc.h"
46 #include "cp-support.h"
47 #include "hashtab.h"
48 #include "command.h"
49 #include "gdbcmd.h"
50
51 #include <fcntl.h>
52 #include "gdb_string.h"
53 #include "gdb_assert.h"
54 #include <sys/types.h>
55
56 /* A note on memory usage for this file.
57    
58    At the present time, this code reads the debug info sections into
59    the objfile's objfile_obstack.  A definite improvement for startup
60    time, on platforms which do not emit relocations for debug
61    sections, would be to use mmap instead.  The object's complete
62    debug information is loaded into memory, partly to simplify
63    absolute DIE references.
64
65    Whether using obstacks or mmap, the sections should remain loaded
66    until the objfile is released, and pointers into the section data
67    can be used for any other data associated to the objfile (symbol
68    names, type names, location expressions to name a few).  */
69
70 #ifndef DWARF2_REG_TO_REGNUM
71 #define DWARF2_REG_TO_REGNUM(REG) (REG)
72 #endif
73
74 #if 0
75 /* .debug_info header for a compilation unit
76    Because of alignment constraints, this structure has padding and cannot
77    be mapped directly onto the beginning of the .debug_info section.  */
78 typedef struct comp_unit_header
79   {
80     unsigned int length;        /* length of the .debug_info
81                                    contribution */
82     unsigned short version;     /* version number -- 2 for DWARF
83                                    version 2 */
84     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
85     unsigned char addr_size;    /* byte size of an address -- 4 */
86   }
87 _COMP_UNIT_HEADER;
88 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
89 #endif
90
91 /* .debug_pubnames header
92    Because of alignment constraints, this structure has padding and cannot
93    be mapped directly onto the beginning of the .debug_info section.  */
94 typedef struct pubnames_header
95   {
96     unsigned int length;        /* length of the .debug_pubnames
97                                    contribution  */
98     unsigned char version;      /* version number -- 2 for DWARF
99                                    version 2 */
100     unsigned int info_offset;   /* offset into .debug_info section */
101     unsigned int info_size;     /* byte size of .debug_info section
102                                    portion */
103   }
104 _PUBNAMES_HEADER;
105 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
106
107 /* .debug_pubnames header
108    Because of alignment constraints, this structure has padding and cannot
109    be mapped directly onto the beginning of the .debug_info section.  */
110 typedef struct aranges_header
111   {
112     unsigned int length;        /* byte len of the .debug_aranges
113                                    contribution */
114     unsigned short version;     /* version number -- 2 for DWARF
115                                    version 2 */
116     unsigned int info_offset;   /* offset into .debug_info section */
117     unsigned char addr_size;    /* byte size of an address */
118     unsigned char seg_size;     /* byte size of segment descriptor */
119   }
120 _ARANGES_HEADER;
121 #define _ACTUAL_ARANGES_HEADER_SIZE 12
122
123 /* .debug_line statement program prologue
124    Because of alignment constraints, this structure has padding and cannot
125    be mapped directly onto the beginning of the .debug_info section.  */
126 typedef struct statement_prologue
127   {
128     unsigned int total_length;  /* byte length of the statement
129                                    information */
130     unsigned short version;     /* version number -- 2 for DWARF
131                                    version 2 */
132     unsigned int prologue_length;       /* # bytes between prologue &
133                                            stmt program */
134     unsigned char minimum_instruction_length;   /* byte size of
135                                                    smallest instr */
136     unsigned char default_is_stmt;      /* initial value of is_stmt
137                                            register */
138     char line_base;
139     unsigned char line_range;
140     unsigned char opcode_base;  /* number assigned to first special
141                                    opcode */
142     unsigned char *standard_opcode_lengths;
143   }
144 _STATEMENT_PROLOGUE;
145
146 static const struct objfile_data *dwarf2_objfile_data_key;
147
148 struct dwarf2_per_objfile
149 {
150   /* Sizes of debugging sections.  */
151   unsigned int info_size;
152   unsigned int abbrev_size;
153   unsigned int line_size;
154   unsigned int pubnames_size;
155   unsigned int aranges_size;
156   unsigned int loc_size;
157   unsigned int macinfo_size;
158   unsigned int str_size;
159   unsigned int ranges_size;
160   unsigned int frame_size;
161   unsigned int eh_frame_size;
162
163   /* Loaded data from the sections.  */
164   char *info_buffer;
165   char *abbrev_buffer;
166   char *line_buffer;
167   char *str_buffer;
168   char *macinfo_buffer;
169   char *ranges_buffer;
170   char *loc_buffer;
171
172   /* A list of all the compilation units.  This will be set if and
173      only if we have encountered a compilation unit with inter-CU
174      references.  */
175   struct dwarf2_per_cu_data **all_comp_units;
176
177   /* The number of compilation units in ALL_COMP_UNITS.  */
178   int n_comp_units;
179
180   /* A chain of compilation units that are currently read in, so that
181      they can be freed later.  */
182   struct dwarf2_per_cu_data *read_in_chain;
183 };
184
185 static struct dwarf2_per_objfile *dwarf2_per_objfile;
186
187 static asection *dwarf_info_section;
188 static asection *dwarf_abbrev_section;
189 static asection *dwarf_line_section;
190 static asection *dwarf_pubnames_section;
191 static asection *dwarf_aranges_section;
192 static asection *dwarf_loc_section;
193 static asection *dwarf_macinfo_section;
194 static asection *dwarf_str_section;
195 static asection *dwarf_ranges_section;
196 asection *dwarf_frame_section;
197 asection *dwarf_eh_frame_section;
198
199 /* names of the debugging sections */
200
201 #define INFO_SECTION     ".debug_info"
202 #define ABBREV_SECTION   ".debug_abbrev"
203 #define LINE_SECTION     ".debug_line"
204 #define PUBNAMES_SECTION ".debug_pubnames"
205 #define ARANGES_SECTION  ".debug_aranges"
206 #define LOC_SECTION      ".debug_loc"
207 #define MACINFO_SECTION  ".debug_macinfo"
208 #define STR_SECTION      ".debug_str"
209 #define RANGES_SECTION   ".debug_ranges"
210 #define FRAME_SECTION    ".debug_frame"
211 #define EH_FRAME_SECTION ".eh_frame"
212
213 /* local data types */
214
215 /* We hold several abbreviation tables in memory at the same time. */
216 #ifndef ABBREV_HASH_SIZE
217 #define ABBREV_HASH_SIZE 121
218 #endif
219
220 /* The data in a compilation unit header, after target2host
221    translation, looks like this.  */
222 struct comp_unit_head
223   {
224     unsigned long length;
225     short version;
226     unsigned int abbrev_offset;
227     unsigned char addr_size;
228     unsigned char signed_addr_p;
229     unsigned int offset_size;   /* size of file offsets; either 4 or 8 */
230     unsigned int initial_length_size; /* size of the length field; either
231                                          4 or 12 */
232
233     /* Offset to the first byte of this compilation unit header in the 
234      * .debug_info section, for resolving relative reference dies. */
235
236     unsigned int offset;
237
238     /* Pointer to this compilation unit header in the .debug_info
239      * section */
240
241     char *cu_head_ptr;
242
243     /* Pointer to the first die of this compilatio unit.  This will
244      * be the first byte following the compilation unit header. */
245
246     char *first_die_ptr;
247
248     /* Pointer to the next compilation unit header in the program. */
249
250     struct comp_unit_head *next;
251
252     /* Base address of this compilation unit.  */
253
254     CORE_ADDR base_address;
255
256     /* Non-zero if base_address has been set.  */
257
258     int base_known;
259   };
260
261 /* Internal state when decoding a particular compilation unit.  */
262 struct dwarf2_cu
263 {
264   /* The objfile containing this compilation unit.  */
265   struct objfile *objfile;
266
267   /* The header of the compilation unit.
268
269      FIXME drow/2003-11-10: Some of the things from the comp_unit_head
270      should logically be moved to the dwarf2_cu structure.  */
271   struct comp_unit_head header;
272
273   struct function_range *first_fn, *last_fn, *cached_fn;
274
275   /* The language we are debugging.  */
276   enum language language;
277   const struct language_defn *language_defn;
278
279   const char *producer;
280
281   /* The generic symbol table building routines have separate lists for
282      file scope symbols and all all other scopes (local scopes).  So
283      we need to select the right one to pass to add_symbol_to_list().
284      We do it by keeping a pointer to the correct list in list_in_scope.
285
286      FIXME: The original dwarf code just treated the file scope as the
287      first local scope, and all other local scopes as nested local
288      scopes, and worked fine.  Check to see if we really need to
289      distinguish these in buildsym.c.  */
290   struct pending **list_in_scope;
291
292   /* Maintain an array of referenced fundamental types for the current
293      compilation unit being read.  For DWARF version 1, we have to construct
294      the fundamental types on the fly, since no information about the
295      fundamental types is supplied.  Each such fundamental type is created by
296      calling a language dependent routine to create the type, and then a
297      pointer to that type is then placed in the array at the index specified
298      by it's FT_<TYPENAME> value.  The array has a fixed size set by the
299      FT_NUM_MEMBERS compile time constant, which is the number of predefined
300      fundamental types gdb knows how to construct.  */
301   struct type *ftypes[FT_NUM_MEMBERS];  /* Fundamental types */
302
303   /* DWARF abbreviation table associated with this compilation unit.  */
304   struct abbrev_info **dwarf2_abbrevs;
305
306   /* Storage for the abbrev table.  */
307   struct obstack abbrev_obstack;
308
309   /* Hash table holding all the loaded partial DIEs.  */
310   htab_t partial_dies;
311
312   /* Storage for things with the same lifetime as this read-in compilation
313      unit, including partial DIEs.  */
314   struct obstack comp_unit_obstack;
315
316   /* When multiple dwarf2_cu structures are living in memory, this field
317      chains them all together, so that they can be released efficiently.
318      We will probably also want a generation counter so that most-recently-used
319      compilation units are cached...  */
320   struct dwarf2_per_cu_data *read_in_chain;
321
322   /* Backchain to our per_cu entry if the tree has been built.  */
323   struct dwarf2_per_cu_data *per_cu;
324
325   /* How many compilation units ago was this CU last referenced?  */
326   int last_used;
327
328   /* Mark used when releasing cached dies.  */
329   unsigned int mark : 1;
330
331   /* This flag will be set if this compilation unit might include
332      inter-compilation-unit references.  */
333   unsigned int has_form_ref_addr : 1;
334
335   /* This flag will be set if this compilation unit includes any
336      DW_TAG_namespace DIEs.  If we know that there are explicit
337      DIEs for namespaces, we don't need to try to infer them
338      from mangled names.  */
339   unsigned int has_namespace_info : 1;
340 };
341
342 struct dwarf2_per_cu_data
343 {
344   /* The start offset and length of this compilation unit.  2**31-1
345      bytes should suffice to store the length of any compilation unit
346      - if it doesn't, GDB will fall over anyway.  */
347   unsigned long offset;
348   unsigned long length : 31;
349
350   /* Flag indicating this compilation unit will be read in before
351      any of the current compilation units are processed.  */
352   unsigned long queued : 1;
353
354   /* Set iff currently read in.  */
355   struct dwarf2_cu *cu;
356
357   /* If full symbols for this CU have been read in, then this field
358      holds a map of DIE offsets to types.  It isn't always possible
359      to reconstruct this information later, so we have to preserve
360      it.  */
361
362   htab_t type_hash;
363 };
364
365 /* The line number information for a compilation unit (found in the
366    .debug_line section) begins with a "statement program header",
367    which contains the following information.  */
368 struct line_header
369 {
370   unsigned int total_length;
371   unsigned short version;
372   unsigned int header_length;
373   unsigned char minimum_instruction_length;
374   unsigned char default_is_stmt;
375   int line_base;
376   unsigned char line_range;
377   unsigned char opcode_base;
378
379   /* standard_opcode_lengths[i] is the number of operands for the
380      standard opcode whose value is i.  This means that
381      standard_opcode_lengths[0] is unused, and the last meaningful
382      element is standard_opcode_lengths[opcode_base - 1].  */
383   unsigned char *standard_opcode_lengths;
384
385   /* The include_directories table.  NOTE!  These strings are not
386      allocated with xmalloc; instead, they are pointers into
387      debug_line_buffer.  If you try to free them, `free' will get
388      indigestion.  */
389   unsigned int num_include_dirs, include_dirs_size;
390   char **include_dirs;
391
392   /* The file_names table.  NOTE!  These strings are not allocated
393      with xmalloc; instead, they are pointers into debug_line_buffer.
394      Don't try to free them directly.  */
395   unsigned int num_file_names, file_names_size;
396   struct file_entry
397   {
398     char *name;
399     unsigned int dir_index;
400     unsigned int mod_time;
401     unsigned int length;
402     int included_p; /* Non-zero if referenced by the Line Number Program.  */
403   } *file_names;
404
405   /* The start and end of the statement program following this
406      header.  These point into dwarf2_per_objfile->line_buffer.  */
407   char *statement_program_start, *statement_program_end;
408 };
409
410 /* When we construct a partial symbol table entry we only
411    need this much information. */
412 struct partial_die_info
413   {
414     /* Offset of this DIE.  */
415     unsigned int offset;
416
417     /* DWARF-2 tag for this DIE.  */
418     ENUM_BITFIELD(dwarf_tag) tag : 16;
419
420     /* Language code associated with this DIE.  This is only used
421        for the compilation unit DIE.  */
422     unsigned int language : 8;
423
424     /* Assorted flags describing the data found in this DIE.  */
425     unsigned int has_children : 1;
426     unsigned int is_external : 1;
427     unsigned int is_declaration : 1;
428     unsigned int has_type : 1;
429     unsigned int has_specification : 1;
430     unsigned int has_stmt_list : 1;
431     unsigned int has_pc_info : 1;
432
433     /* Flag set if the SCOPE field of this structure has been
434        computed.  */
435     unsigned int scope_set : 1;
436
437     /* The name of this DIE.  Normally the value of DW_AT_name, but
438        sometimes DW_TAG_MIPS_linkage_name or a string computed in some
439        other fashion.  */
440     char *name;
441     char *dirname;
442
443     /* The scope to prepend to our children.  This is generally
444        allocated on the comp_unit_obstack, so will disappear
445        when this compilation unit leaves the cache.  */
446     char *scope;
447
448     /* The location description associated with this DIE, if any.  */
449     struct dwarf_block *locdesc;
450
451     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
452     CORE_ADDR lowpc;
453     CORE_ADDR highpc;
454
455     /* Pointer into the info_buffer pointing at the target of
456        DW_AT_sibling, if any.  */
457     char *sibling;
458
459     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
460        DW_AT_specification (or DW_AT_abstract_origin or
461        DW_AT_extension).  */
462     unsigned int spec_offset;
463
464     /* If HAS_STMT_LIST, the offset of the Line Number Information data.  */
465     unsigned int line_offset;
466
467     /* Pointers to this DIE's parent, first child, and next sibling,
468        if any.  */
469     struct partial_die_info *die_parent, *die_child, *die_sibling;
470   };
471
472 /* This data structure holds the information of an abbrev. */
473 struct abbrev_info
474   {
475     unsigned int number;        /* number identifying abbrev */
476     enum dwarf_tag tag;         /* dwarf tag */
477     unsigned short has_children;                /* boolean */
478     unsigned short num_attrs;   /* number of attributes */
479     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
480     struct abbrev_info *next;   /* next in chain */
481   };
482
483 struct attr_abbrev
484   {
485     enum dwarf_attribute name;
486     enum dwarf_form form;
487   };
488
489 /* This data structure holds a complete die structure. */
490 struct die_info
491   {
492     enum dwarf_tag tag;         /* Tag indicating type of die */
493     unsigned int abbrev;        /* Abbrev number */
494     unsigned int offset;        /* Offset in .debug_info section */
495     unsigned int num_attrs;     /* Number of attributes */
496     struct attribute *attrs;    /* An array of attributes */
497     struct die_info *next_ref;  /* Next die in ref hash table */
498
499     /* The dies in a compilation unit form an n-ary tree.  PARENT
500        points to this die's parent; CHILD points to the first child of
501        this node; and all the children of a given node are chained
502        together via their SIBLING fields, terminated by a die whose
503        tag is zero.  */
504     struct die_info *child;     /* Its first child, if any.  */
505     struct die_info *sibling;   /* Its next sibling, if any.  */
506     struct die_info *parent;    /* Its parent, if any.  */
507
508     struct type *type;          /* Cached type information */
509   };
510
511 /* Attributes have a name and a value */
512 struct attribute
513   {
514     enum dwarf_attribute name;
515     enum dwarf_form form;
516     union
517       {
518         char *str;
519         struct dwarf_block *blk;
520         unsigned long unsnd;
521         long int snd;
522         CORE_ADDR addr;
523       }
524     u;
525   };
526
527 struct function_range
528 {
529   const char *name;
530   CORE_ADDR lowpc, highpc;
531   int seen_line;
532   struct function_range *next;
533 };
534
535 /* Get at parts of an attribute structure */
536
537 #define DW_STRING(attr)    ((attr)->u.str)
538 #define DW_UNSND(attr)     ((attr)->u.unsnd)
539 #define DW_BLOCK(attr)     ((attr)->u.blk)
540 #define DW_SND(attr)       ((attr)->u.snd)
541 #define DW_ADDR(attr)      ((attr)->u.addr)
542
543 /* Blocks are a bunch of untyped bytes. */
544 struct dwarf_block
545   {
546     unsigned int size;
547     char *data;
548   };
549
550 #ifndef ATTR_ALLOC_CHUNK
551 #define ATTR_ALLOC_CHUNK 4
552 #endif
553
554 /* A hash table of die offsets for following references.  */
555 #ifndef REF_HASH_SIZE
556 #define REF_HASH_SIZE 1021
557 #endif
558
559 static struct die_info *die_ref_table[REF_HASH_SIZE];
560
561 /* Allocate fields for structs, unions and enums in this size.  */
562 #ifndef DW_FIELD_ALLOC_CHUNK
563 #define DW_FIELD_ALLOC_CHUNK 4
564 #endif
565
566 /* A zeroed version of a partial die for initialization purposes.  */
567 static struct partial_die_info zeroed_partial_die;
568
569 /* FIXME: decode_locdesc sets these variables to describe the location
570    to the caller.  These ought to be a structure or something.   If
571    none of the flags are set, the object lives at the address returned
572    by decode_locdesc.  */
573
574 static int isreg;               /* Object lives in register.
575                                    decode_locdesc's return value is
576                                    the register number.  */
577
578 /* We put a pointer to this structure in the read_symtab_private field
579    of the psymtab.  */
580
581 struct dwarf2_pinfo
582   {
583     /* Offset in .debug_info for this compilation unit. */
584
585     unsigned long dwarf_info_offset;
586   };
587
588 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
589 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
590
591 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
592    but this would require a corresponding change in unpack_field_as_long
593    and friends.  */
594 static int bits_per_byte = 8;
595
596 /* The routines that read and process dies for a C struct or C++ class
597    pass lists of data member fields and lists of member function fields
598    in an instance of a field_info structure, as defined below.  */
599 struct field_info
600   {
601     /* List of data member and baseclasses fields. */
602     struct nextfield
603       {
604         struct nextfield *next;
605         int accessibility;
606         int virtuality;
607         struct field field;
608       }
609      *fields;
610
611     /* Number of fields.  */
612     int nfields;
613
614     /* Number of baseclasses.  */
615     int nbaseclasses;
616
617     /* Set if the accesibility of one of the fields is not public.  */
618     int non_public_fields;
619
620     /* Member function fields array, entries are allocated in the order they
621        are encountered in the object file.  */
622     struct nextfnfield
623       {
624         struct nextfnfield *next;
625         struct fn_field fnfield;
626       }
627      *fnfields;
628
629     /* Member function fieldlist array, contains name of possibly overloaded
630        member function, number of overloaded member functions and a pointer
631        to the head of the member function field chain.  */
632     struct fnfieldlist
633       {
634         char *name;
635         int length;
636         struct nextfnfield *head;
637       }
638      *fnfieldlists;
639
640     /* Number of entries in the fnfieldlists array.  */
641     int nfnfields;
642   };
643
644 /* Loaded secondary compilation units are kept in memory until they
645    have not been referenced for the processing of this many
646    compilation units.  Set this to zero to disable caching.  Cache
647    sizes of up to at least twenty will improve startup time for
648    typical inter-CU-reference binaries, at an obvious memory cost.  */
649 static int dwarf2_max_cache_age = 5;
650
651 /* Various complaints about symbol reading that don't abort the process */
652
653 static void
654 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
655 {
656   complaint (&symfile_complaints,
657              "statement list doesn't fit in .debug_line section");
658 }
659
660 static void
661 dwarf2_complex_location_expr_complaint (void)
662 {
663   complaint (&symfile_complaints, "location expression too complex");
664 }
665
666 static void
667 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
668                                               int arg3)
669 {
670   complaint (&symfile_complaints,
671              "const value length mismatch for '%s', got %d, expected %d", arg1,
672              arg2, arg3);
673 }
674
675 static void
676 dwarf2_macros_too_long_complaint (void)
677 {
678   complaint (&symfile_complaints,
679              "macro info runs off end of `.debug_macinfo' section");
680 }
681
682 static void
683 dwarf2_macro_malformed_definition_complaint (const char *arg1)
684 {
685   complaint (&symfile_complaints,
686              "macro debug info contains a malformed macro definition:\n`%s'",
687              arg1);
688 }
689
690 static void
691 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
692 {
693   complaint (&symfile_complaints,
694              "invalid attribute class or form for '%s' in '%s'", arg1, arg2);
695 }
696
697 /* local function prototypes */
698
699 static void dwarf2_locate_sections (bfd *, asection *, void *);
700
701 #if 0
702 static void dwarf2_build_psymtabs_easy (struct objfile *, int);
703 #endif
704
705 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
706                                            struct objfile *);
707
708 static void dwarf2_build_include_psymtabs (struct dwarf2_cu *,
709                                            struct partial_die_info *,
710                                            struct partial_symtab *);
711
712 static void dwarf2_build_psymtabs_hard (struct objfile *, int);
713
714 static void scan_partial_symbols (struct partial_die_info *,
715                                   CORE_ADDR *, CORE_ADDR *,
716                                   struct dwarf2_cu *);
717
718 static void add_partial_symbol (struct partial_die_info *,
719                                 struct dwarf2_cu *);
720
721 static int pdi_needs_namespace (enum dwarf_tag tag);
722
723 static void add_partial_namespace (struct partial_die_info *pdi,
724                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
725                                    struct dwarf2_cu *cu);
726
727 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
728                                      struct dwarf2_cu *cu);
729
730 static char *locate_pdi_sibling (struct partial_die_info *orig_pdi,
731                                  char *info_ptr,
732                                  bfd *abfd,
733                                  struct dwarf2_cu *cu);
734
735 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
736
737 static void psymtab_to_symtab_1 (struct partial_symtab *);
738
739 char *dwarf2_read_section (struct objfile *, asection *);
740
741 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
742
743 static void dwarf2_free_abbrev_table (void *);
744
745 static struct abbrev_info *peek_die_abbrev (char *, int *, struct dwarf2_cu *);
746
747 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
748                                                  struct dwarf2_cu *);
749
750 static struct partial_die_info *load_partial_dies (bfd *, char *, int,
751                                                    struct dwarf2_cu *);
752
753 static char *read_partial_die (struct partial_die_info *,
754                                struct abbrev_info *abbrev, unsigned int,
755                                bfd *, char *, struct dwarf2_cu *);
756
757 static struct partial_die_info *find_partial_die (unsigned long,
758                                                   struct dwarf2_cu *,
759                                                   struct dwarf2_cu **);
760
761 static void fixup_partial_die (struct partial_die_info *,
762                                struct dwarf2_cu *);
763
764 static char *read_full_die (struct die_info **, bfd *, char *,
765                             struct dwarf2_cu *, int *);
766
767 static char *read_attribute (struct attribute *, struct attr_abbrev *,
768                              bfd *, char *, struct dwarf2_cu *);
769
770 static char *read_attribute_value (struct attribute *, unsigned,
771                              bfd *, char *, struct dwarf2_cu *);
772
773 static unsigned int read_1_byte (bfd *, char *);
774
775 static int read_1_signed_byte (bfd *, char *);
776
777 static unsigned int read_2_bytes (bfd *, char *);
778
779 static unsigned int read_4_bytes (bfd *, char *);
780
781 static unsigned long read_8_bytes (bfd *, char *);
782
783 static CORE_ADDR read_address (bfd *, char *ptr, struct dwarf2_cu *,
784                                int *bytes_read);
785
786 static LONGEST read_initial_length (bfd *, char *,
787                                     struct comp_unit_head *, int *bytes_read);
788
789 static LONGEST read_offset (bfd *, char *, const struct comp_unit_head *,
790                             int *bytes_read);
791
792 static char *read_n_bytes (bfd *, char *, unsigned int);
793
794 static char *read_string (bfd *, char *, unsigned int *);
795
796 static char *read_indirect_string (bfd *, char *, const struct comp_unit_head *,
797                                    unsigned int *);
798
799 static unsigned long read_unsigned_leb128 (bfd *, char *, unsigned int *);
800
801 static long read_signed_leb128 (bfd *, char *, unsigned int *);
802
803 static char *skip_leb128 (bfd *, char *);
804
805 static void set_cu_language (unsigned int, struct dwarf2_cu *);
806
807 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
808                                       struct dwarf2_cu *);
809
810 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
811                                struct dwarf2_cu *cu);
812
813 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
814
815 static struct die_info *die_specification (struct die_info *die,
816                                            struct dwarf2_cu *);
817
818 static void free_line_header (struct line_header *lh);
819
820 static void add_file_name (struct line_header *, char *, unsigned int,
821                            unsigned int, unsigned int);
822
823 static struct line_header *(dwarf_decode_line_header
824                             (unsigned int offset,
825                              bfd *abfd, struct dwarf2_cu *cu));
826
827 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
828                                 struct dwarf2_cu *, struct partial_symtab *);
829
830 static void dwarf2_start_subfile (char *, char *);
831
832 static struct symbol *new_symbol (struct die_info *, struct type *,
833                                   struct dwarf2_cu *);
834
835 static void dwarf2_const_value (struct attribute *, struct symbol *,
836                                 struct dwarf2_cu *);
837
838 static void dwarf2_const_value_data (struct attribute *attr,
839                                      struct symbol *sym,
840                                      int bits);
841
842 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
843
844 static struct type *die_containing_type (struct die_info *,
845                                          struct dwarf2_cu *);
846
847 #if 0
848 static struct type *type_at_offset (unsigned int, struct objfile *);
849 #endif
850
851 static struct type *tag_type_to_type (struct die_info *, struct dwarf2_cu *);
852
853 static void read_type_die (struct die_info *, struct dwarf2_cu *);
854
855 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
856
857 static char *typename_concat (struct obstack *, const char *prefix, const char *suffix,
858                               struct dwarf2_cu *);
859
860 static void read_typedef (struct die_info *, struct dwarf2_cu *);
861
862 static void read_base_type (struct die_info *, struct dwarf2_cu *);
863
864 static void read_subrange_type (struct die_info *die, struct dwarf2_cu *cu);
865
866 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
867
868 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
869
870 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
871
872 static int dwarf2_get_pc_bounds (struct die_info *,
873                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *);
874
875 static void get_scope_pc_bounds (struct die_info *,
876                                  CORE_ADDR *, CORE_ADDR *,
877                                  struct dwarf2_cu *);
878
879 static void dwarf2_add_field (struct field_info *, struct die_info *,
880                               struct dwarf2_cu *);
881
882 static void dwarf2_attach_fields_to_type (struct field_info *,
883                                           struct type *, struct dwarf2_cu *);
884
885 static void dwarf2_add_member_fn (struct field_info *,
886                                   struct die_info *, struct type *,
887                                   struct dwarf2_cu *);
888
889 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
890                                              struct type *, struct dwarf2_cu *);
891
892 static void read_structure_type (struct die_info *, struct dwarf2_cu *);
893
894 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
895
896 static char *determine_class_name (struct die_info *die, struct dwarf2_cu *cu);
897
898 static void read_common_block (struct die_info *, struct dwarf2_cu *);
899
900 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
901
902 static const char *namespace_name (struct die_info *die,
903                                    int *is_anonymous, struct dwarf2_cu *);
904
905 static void read_enumeration_type (struct die_info *, struct dwarf2_cu *);
906
907 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
908
909 static struct type *dwarf_base_type (int, int, struct dwarf2_cu *);
910
911 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
912
913 static void read_array_type (struct die_info *, struct dwarf2_cu *);
914
915 static enum dwarf_array_dim_ordering read_array_order (struct die_info *, 
916                                                        struct dwarf2_cu *);
917
918 static void read_tag_pointer_type (struct die_info *, struct dwarf2_cu *);
919
920 static void read_tag_ptr_to_member_type (struct die_info *,
921                                          struct dwarf2_cu *);
922
923 static void read_tag_reference_type (struct die_info *, struct dwarf2_cu *);
924
925 static void read_tag_const_type (struct die_info *, struct dwarf2_cu *);
926
927 static void read_tag_volatile_type (struct die_info *, struct dwarf2_cu *);
928
929 static void read_tag_string_type (struct die_info *, struct dwarf2_cu *);
930
931 static void read_subroutine_type (struct die_info *, struct dwarf2_cu *);
932
933 static struct die_info *read_comp_unit (char *, bfd *, struct dwarf2_cu *);
934
935 static struct die_info *read_die_and_children (char *info_ptr, bfd *abfd,
936                                                struct dwarf2_cu *,
937                                                char **new_info_ptr,
938                                                struct die_info *parent);
939
940 static struct die_info *read_die_and_siblings (char *info_ptr, bfd *abfd,
941                                                struct dwarf2_cu *,
942                                                char **new_info_ptr,
943                                                struct die_info *parent);
944
945 static void free_die_list (struct die_info *);
946
947 static struct cleanup *make_cleanup_free_die_list (struct die_info *);
948
949 static void process_die (struct die_info *, struct dwarf2_cu *);
950
951 static char *dwarf2_linkage_name (struct die_info *, struct dwarf2_cu *);
952
953 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
954
955 static struct die_info *dwarf2_extension (struct die_info *die,
956                                           struct dwarf2_cu *);
957
958 static char *dwarf_tag_name (unsigned int);
959
960 static char *dwarf_attr_name (unsigned int);
961
962 static char *dwarf_form_name (unsigned int);
963
964 static char *dwarf_stack_op_name (unsigned int);
965
966 static char *dwarf_bool_name (unsigned int);
967
968 static char *dwarf_type_encoding_name (unsigned int);
969
970 #if 0
971 static char *dwarf_cfi_name (unsigned int);
972
973 struct die_info *copy_die (struct die_info *);
974 #endif
975
976 static struct die_info *sibling_die (struct die_info *);
977
978 static void dump_die (struct die_info *);
979
980 static void dump_die_list (struct die_info *);
981
982 static void store_in_ref_table (unsigned int, struct die_info *);
983
984 static void dwarf2_empty_hash_tables (void);
985
986 static unsigned int dwarf2_get_ref_die_offset (struct attribute *,
987                                                struct dwarf2_cu *);
988
989 static int dwarf2_get_attr_constant_value (struct attribute *, int);
990
991 static struct die_info *follow_die_ref (unsigned int);
992
993 static struct type *dwarf2_fundamental_type (struct objfile *, int,
994                                              struct dwarf2_cu *);
995
996 /* memory allocation interface */
997
998 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
999
1000 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1001
1002 static struct die_info *dwarf_alloc_die (void);
1003
1004 static void initialize_cu_func_list (struct dwarf2_cu *);
1005
1006 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1007                                  struct dwarf2_cu *);
1008
1009 static void dwarf_decode_macros (struct line_header *, unsigned int,
1010                                  char *, bfd *, struct dwarf2_cu *);
1011
1012 static int attr_form_is_block (struct attribute *);
1013
1014 static void
1015 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
1016                              struct dwarf2_cu *cu);
1017
1018 static char *skip_one_die (char *info_ptr, struct abbrev_info *abbrev,
1019                            struct dwarf2_cu *cu);
1020
1021 static void free_stack_comp_unit (void *);
1022
1023 static void *hashtab_obstack_allocate (void *data, size_t size, size_t count);
1024
1025 static void dummy_obstack_deallocate (void *object, void *data);
1026
1027 static hashval_t partial_die_hash (const void *item);
1028
1029 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1030
1031 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1032   (unsigned long offset, struct objfile *objfile);
1033
1034 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1035   (unsigned long offset, struct objfile *objfile);
1036
1037 static void free_one_comp_unit (void *);
1038
1039 static void free_cached_comp_units (void *);
1040
1041 static void age_cached_comp_units (void);
1042
1043 static void free_one_cached_comp_unit (void *);
1044
1045 static void set_die_type (struct die_info *, struct type *,
1046                           struct dwarf2_cu *);
1047
1048 #if 0
1049 static void reset_die_and_siblings_types (struct die_info *,
1050                                           struct dwarf2_cu *);
1051 #endif
1052
1053 static void create_all_comp_units (struct objfile *);
1054
1055 static void dwarf2_mark (struct dwarf2_cu *);
1056
1057 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1058
1059 /* Try to locate the sections we need for DWARF 2 debugging
1060    information and return true if we have enough to do something.  */
1061
1062 int
1063 dwarf2_has_info (struct objfile *objfile)
1064 {
1065   struct dwarf2_per_objfile *data;
1066
1067   /* Initialize per-objfile state.  */
1068   data = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1069   memset (data, 0, sizeof (*data));
1070   set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1071   dwarf2_per_objfile = data;
1072
1073   dwarf_info_section = 0;
1074   dwarf_abbrev_section = 0;
1075   dwarf_line_section = 0;
1076   dwarf_str_section = 0;
1077   dwarf_macinfo_section = 0;
1078   dwarf_frame_section = 0;
1079   dwarf_eh_frame_section = 0;
1080   dwarf_ranges_section = 0;
1081   dwarf_loc_section = 0;
1082   
1083   bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1084   return (dwarf_info_section != NULL && dwarf_abbrev_section != NULL);
1085 }
1086
1087 /* This function is mapped across the sections and remembers the
1088    offset and size of each of the debugging sections we are interested
1089    in.  */
1090
1091 static void
1092 dwarf2_locate_sections (bfd *ignore_abfd, asection *sectp, void *ignore_ptr)
1093 {
1094   if (strcmp (sectp->name, INFO_SECTION) == 0)
1095     {
1096       dwarf2_per_objfile->info_size = bfd_get_section_size (sectp);
1097       dwarf_info_section = sectp;
1098     }
1099   else if (strcmp (sectp->name, ABBREV_SECTION) == 0)
1100     {
1101       dwarf2_per_objfile->abbrev_size = bfd_get_section_size (sectp);
1102       dwarf_abbrev_section = sectp;
1103     }
1104   else if (strcmp (sectp->name, LINE_SECTION) == 0)
1105     {
1106       dwarf2_per_objfile->line_size = bfd_get_section_size (sectp);
1107       dwarf_line_section = sectp;
1108     }
1109   else if (strcmp (sectp->name, PUBNAMES_SECTION) == 0)
1110     {
1111       dwarf2_per_objfile->pubnames_size = bfd_get_section_size (sectp);
1112       dwarf_pubnames_section = sectp;
1113     }
1114   else if (strcmp (sectp->name, ARANGES_SECTION) == 0)
1115     {
1116       dwarf2_per_objfile->aranges_size = bfd_get_section_size (sectp);
1117       dwarf_aranges_section = sectp;
1118     }
1119   else if (strcmp (sectp->name, LOC_SECTION) == 0)
1120     {
1121       dwarf2_per_objfile->loc_size = bfd_get_section_size (sectp);
1122       dwarf_loc_section = sectp;
1123     }
1124   else if (strcmp (sectp->name, MACINFO_SECTION) == 0)
1125     {
1126       dwarf2_per_objfile->macinfo_size = bfd_get_section_size (sectp);
1127       dwarf_macinfo_section = sectp;
1128     }
1129   else if (strcmp (sectp->name, STR_SECTION) == 0)
1130     {
1131       dwarf2_per_objfile->str_size = bfd_get_section_size (sectp);
1132       dwarf_str_section = sectp;
1133     }
1134   else if (strcmp (sectp->name, FRAME_SECTION) == 0)
1135     {
1136       dwarf2_per_objfile->frame_size = bfd_get_section_size (sectp);
1137       dwarf_frame_section = sectp;
1138     }
1139   else if (strcmp (sectp->name, EH_FRAME_SECTION) == 0)
1140     {
1141       flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1142       if (aflag & SEC_HAS_CONTENTS)
1143         {
1144           dwarf2_per_objfile->eh_frame_size = bfd_get_section_size (sectp);
1145           dwarf_eh_frame_section = sectp;
1146         }
1147     }
1148   else if (strcmp (sectp->name, RANGES_SECTION) == 0)
1149     {
1150       dwarf2_per_objfile->ranges_size = bfd_get_section_size (sectp);
1151       dwarf_ranges_section = sectp;
1152     }
1153 }
1154
1155 /* Build a partial symbol table.  */
1156
1157 void
1158 dwarf2_build_psymtabs (struct objfile *objfile, int mainline)
1159 {
1160   /* We definitely need the .debug_info and .debug_abbrev sections */
1161
1162   dwarf2_per_objfile->info_buffer = dwarf2_read_section (objfile, dwarf_info_section);
1163   dwarf2_per_objfile->abbrev_buffer = dwarf2_read_section (objfile, dwarf_abbrev_section);
1164
1165   if (dwarf_line_section)
1166     dwarf2_per_objfile->line_buffer = dwarf2_read_section (objfile, dwarf_line_section);
1167   else
1168     dwarf2_per_objfile->line_buffer = NULL;
1169
1170   if (dwarf_str_section)
1171     dwarf2_per_objfile->str_buffer = dwarf2_read_section (objfile, dwarf_str_section);
1172   else
1173     dwarf2_per_objfile->str_buffer = NULL;
1174
1175   if (dwarf_macinfo_section)
1176     dwarf2_per_objfile->macinfo_buffer = dwarf2_read_section (objfile,
1177                                                 dwarf_macinfo_section);
1178   else
1179     dwarf2_per_objfile->macinfo_buffer = NULL;
1180
1181   if (dwarf_ranges_section)
1182     dwarf2_per_objfile->ranges_buffer = dwarf2_read_section (objfile, dwarf_ranges_section);
1183   else
1184     dwarf2_per_objfile->ranges_buffer = NULL;
1185
1186   if (dwarf_loc_section)
1187     dwarf2_per_objfile->loc_buffer = dwarf2_read_section (objfile, dwarf_loc_section);
1188   else
1189     dwarf2_per_objfile->loc_buffer = NULL;
1190
1191   if (mainline
1192       || (objfile->global_psymbols.size == 0
1193           && objfile->static_psymbols.size == 0))
1194     {
1195       init_psymbol_list (objfile, 1024);
1196     }
1197
1198 #if 0
1199   if (dwarf_aranges_offset && dwarf_pubnames_offset)
1200     {
1201       /* Things are significantly easier if we have .debug_aranges and
1202          .debug_pubnames sections */
1203
1204       dwarf2_build_psymtabs_easy (objfile, mainline);
1205     }
1206   else
1207 #endif
1208     /* only test this case for now */
1209     {
1210       /* In this case we have to work a bit harder */
1211       dwarf2_build_psymtabs_hard (objfile, mainline);
1212     }
1213 }
1214
1215 #if 0
1216 /* Build the partial symbol table from the information in the
1217    .debug_pubnames and .debug_aranges sections.  */
1218
1219 static void
1220 dwarf2_build_psymtabs_easy (struct objfile *objfile, int mainline)
1221 {
1222   bfd *abfd = objfile->obfd;
1223   char *aranges_buffer, *pubnames_buffer;
1224   char *aranges_ptr, *pubnames_ptr;
1225   unsigned int entry_length, version, info_offset, info_size;
1226
1227   pubnames_buffer = dwarf2_read_section (objfile,
1228                                          dwarf_pubnames_section);
1229   pubnames_ptr = pubnames_buffer;
1230   while ((pubnames_ptr - pubnames_buffer) < dwarf2_per_objfile->pubnames_size)
1231     {
1232       struct comp_unit_head cu_header;
1233       int bytes_read;
1234
1235       entry_length = read_initial_length (abfd, pubnames_ptr, &cu_header,
1236                                          &bytes_read);
1237       pubnames_ptr += bytes_read;
1238       version = read_1_byte (abfd, pubnames_ptr);
1239       pubnames_ptr += 1;
1240       info_offset = read_4_bytes (abfd, pubnames_ptr);
1241       pubnames_ptr += 4;
1242       info_size = read_4_bytes (abfd, pubnames_ptr);
1243       pubnames_ptr += 4;
1244     }
1245
1246   aranges_buffer = dwarf2_read_section (objfile,
1247                                         dwarf_aranges_section);
1248
1249 }
1250 #endif
1251
1252 /* Read in the comp unit header information from the debug_info at
1253    info_ptr. */
1254
1255 static char *
1256 read_comp_unit_head (struct comp_unit_head *cu_header,
1257                      char *info_ptr, bfd *abfd)
1258 {
1259   int signed_addr;
1260   int bytes_read;
1261   cu_header->length = read_initial_length (abfd, info_ptr, cu_header,
1262                                            &bytes_read);
1263   info_ptr += bytes_read;
1264   cu_header->version = read_2_bytes (abfd, info_ptr);
1265   info_ptr += 2;
1266   cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
1267                                           &bytes_read);
1268   info_ptr += bytes_read;
1269   cu_header->addr_size = read_1_byte (abfd, info_ptr);
1270   info_ptr += 1;
1271   signed_addr = bfd_get_sign_extend_vma (abfd);
1272   if (signed_addr < 0)
1273     internal_error (__FILE__, __LINE__,
1274                     "read_comp_unit_head: dwarf from non elf file");
1275   cu_header->signed_addr_p = signed_addr;
1276   return info_ptr;
1277 }
1278
1279 static char *
1280 partial_read_comp_unit_head (struct comp_unit_head *header, char *info_ptr,
1281                              bfd *abfd)
1282 {
1283   char *beg_of_comp_unit = info_ptr;
1284
1285   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
1286
1287   if (header->version != 2)
1288     error ("Dwarf Error: wrong version in compilation unit header "
1289            "(is %d, should be %d) [in module %s]", header->version,
1290            2, bfd_get_filename (abfd));
1291
1292   if (header->abbrev_offset >= dwarf2_per_objfile->abbrev_size)
1293     error ("Dwarf Error: bad offset (0x%lx) in compilation unit header "
1294            "(offset 0x%lx + 6) [in module %s]",
1295            (long) header->abbrev_offset,
1296            (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1297            bfd_get_filename (abfd));
1298
1299   if (beg_of_comp_unit + header->length + header->initial_length_size
1300       > dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
1301     error ("Dwarf Error: bad length (0x%lx) in compilation unit header "
1302            "(offset 0x%lx + 0) [in module %s]",
1303            (long) header->length,
1304            (long) (beg_of_comp_unit - dwarf2_per_objfile->info_buffer),
1305            bfd_get_filename (abfd));
1306
1307   return info_ptr;
1308 }
1309
1310 /* Allocate a new partial symtab for file named NAME and mark this new
1311    partial symtab as being an include of PST.  */
1312
1313 static void
1314 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
1315                                struct objfile *objfile)
1316 {
1317   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
1318
1319   subpst->section_offsets = pst->section_offsets;
1320   subpst->textlow = 0;
1321   subpst->texthigh = 0;
1322
1323   subpst->dependencies = (struct partial_symtab **)
1324     obstack_alloc (&objfile->objfile_obstack,
1325                    sizeof (struct partial_symtab *));
1326   subpst->dependencies[0] = pst;
1327   subpst->number_of_dependencies = 1;
1328
1329   subpst->globals_offset = 0;
1330   subpst->n_global_syms = 0;
1331   subpst->statics_offset = 0;
1332   subpst->n_static_syms = 0;
1333   subpst->symtab = NULL;
1334   subpst->read_symtab = pst->read_symtab;
1335   subpst->readin = 0;
1336
1337   /* No private part is necessary for include psymtabs.  This property
1338      can be used to differentiate between such include psymtabs and
1339      the regular ones.  If it ever happens that a regular psymtab can
1340      legitimally have a NULL private part, then we'll have to add a
1341      dedicated field for that in the dwarf2_pinfo structure.  */
1342   subpst->read_symtab_private = NULL;
1343 }
1344
1345 /* Read the Line Number Program data and extract the list of files
1346    included by the source file represented by PST.  Build an include
1347    partial symtab for each of these included files.
1348    
1349    This procedure assumes that there *is* a Line Number Program in
1350    the given CU.  Callers should check that PDI->HAS_STMT_LIST is set
1351    before calling this procedure.  */
1352
1353 static void
1354 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
1355                                struct partial_die_info *pdi,
1356                                struct partial_symtab *pst)
1357 {
1358   struct objfile *objfile = cu->objfile;
1359   bfd *abfd = objfile->obfd;
1360   struct line_header *lh;
1361
1362   lh = dwarf_decode_line_header (pdi->line_offset, abfd, cu);
1363   if (lh == NULL)
1364     return;  /* No linetable, so no includes.  */
1365
1366   dwarf_decode_lines (lh, NULL, abfd, cu, pst);
1367
1368   free_line_header (lh);
1369 }
1370
1371
1372 /* Build the partial symbol table by doing a quick pass through the
1373    .debug_info and .debug_abbrev sections.  */
1374
1375 static void
1376 dwarf2_build_psymtabs_hard (struct objfile *objfile, int mainline)
1377 {
1378   /* Instead of reading this into a big buffer, we should probably use
1379      mmap()  on architectures that support it. (FIXME) */
1380   bfd *abfd = objfile->obfd;
1381   char *info_ptr;
1382   char *beg_of_comp_unit;
1383   struct partial_die_info comp_unit_die;
1384   struct partial_symtab *pst;
1385   struct cleanup *back_to;
1386   CORE_ADDR lowpc, highpc, baseaddr;
1387
1388   info_ptr = dwarf2_per_objfile->info_buffer;
1389
1390   /* Any cached compilation units will be linked by the per-objfile
1391      read_in_chain.  Make sure to free them when we're done.  */
1392   back_to = make_cleanup (free_cached_comp_units, NULL);
1393
1394   /* Since the objects we're extracting from .debug_info vary in
1395      length, only the individual functions to extract them (like
1396      read_comp_unit_head and load_partial_die) can really know whether
1397      the buffer is large enough to hold another complete object.
1398
1399      At the moment, they don't actually check that.  If .debug_info
1400      holds just one extra byte after the last compilation unit's dies,
1401      then read_comp_unit_head will happily read off the end of the
1402      buffer.  read_partial_die is similarly casual.  Those functions
1403      should be fixed.
1404
1405      For this loop condition, simply checking whether there's any data
1406      left at all should be sufficient.  */
1407   while (info_ptr < (dwarf2_per_objfile->info_buffer
1408                      + dwarf2_per_objfile->info_size))
1409     {
1410       struct cleanup *back_to_inner;
1411       struct dwarf2_cu cu;
1412       struct abbrev_info *abbrev;
1413       unsigned int bytes_read;
1414       struct dwarf2_per_cu_data *this_cu;
1415
1416       beg_of_comp_unit = info_ptr;
1417
1418       memset (&cu, 0, sizeof (cu));
1419
1420       obstack_init (&cu.comp_unit_obstack);
1421
1422       back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
1423
1424       cu.objfile = objfile;
1425       info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr, abfd);
1426
1427       /* Complete the cu_header */
1428       cu.header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1429       cu.header.first_die_ptr = info_ptr;
1430       cu.header.cu_head_ptr = beg_of_comp_unit;
1431
1432       cu.list_in_scope = &file_symbols;
1433
1434       /* Read the abbrevs for this compilation unit into a table */
1435       dwarf2_read_abbrevs (abfd, &cu);
1436       make_cleanup (dwarf2_free_abbrev_table, &cu);
1437
1438       if (cu.has_form_ref_addr && dwarf2_per_objfile->all_comp_units == NULL)
1439         create_all_comp_units (objfile);
1440
1441       /* Read the compilation unit die */
1442       abbrev = peek_die_abbrev (info_ptr, &bytes_read, &cu);
1443       info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1444                                    abfd, info_ptr, &cu);
1445
1446       /* Set the language we're debugging */
1447       set_cu_language (comp_unit_die.language, &cu);
1448
1449       /* Allocate a new partial symbol table structure */
1450       pst = start_psymtab_common (objfile, objfile->section_offsets,
1451                                   comp_unit_die.name ? comp_unit_die.name : "",
1452                                   comp_unit_die.lowpc,
1453                                   objfile->global_psymbols.next,
1454                                   objfile->static_psymbols.next);
1455
1456       if (comp_unit_die.dirname)
1457         pst->dirname = xstrdup (comp_unit_die.dirname);
1458
1459       pst->read_symtab_private = (char *)
1460         obstack_alloc (&objfile->objfile_obstack, sizeof (struct dwarf2_pinfo));
1461       DWARF_INFO_OFFSET (pst) = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1462       baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1463
1464       /* Store the function that reads in the rest of the symbol table */
1465       pst->read_symtab = dwarf2_psymtab_to_symtab;
1466
1467       if (dwarf2_per_objfile->all_comp_units != NULL)
1468         {
1469           struct dwarf2_per_cu_data *per_cu;
1470
1471           per_cu = dwarf2_find_comp_unit (cu.header.offset, objfile);
1472
1473           /* If this compilation unit was already read in, free the
1474              cached copy in order to read it in again.  This is
1475              necessary because we skipped some symbols when we first
1476              read in the compilation unit (see load_partial_dies).
1477              This problem could be avoided, but the benefit is
1478              unclear.  */
1479           if (per_cu->cu != NULL)
1480             free_one_cached_comp_unit (per_cu->cu);
1481
1482           cu.per_cu = per_cu;
1483
1484           /* Note that this is a pointer to our stack frame, being
1485              added to a global data structure.  It will be cleaned up
1486              in free_stack_comp_unit when we finish with this
1487              compilation unit.  */
1488           per_cu->cu = &cu;
1489         }
1490       else
1491         cu.per_cu = NULL;
1492
1493       /* Check if comp unit has_children.
1494          If so, read the rest of the partial symbols from this comp unit.
1495          If not, there's no more debug_info for this comp unit. */
1496       if (comp_unit_die.has_children)
1497         {
1498           struct partial_die_info *first_die;
1499
1500           lowpc = ((CORE_ADDR) -1);
1501           highpc = ((CORE_ADDR) 0);
1502
1503           first_die = load_partial_dies (abfd, info_ptr, 1, &cu);
1504
1505           scan_partial_symbols (first_die, &lowpc, &highpc, &cu);
1506
1507           /* If we didn't find a lowpc, set it to highpc to avoid
1508              complaints from `maint check'.  */
1509           if (lowpc == ((CORE_ADDR) -1))
1510             lowpc = highpc;
1511
1512           /* If the compilation unit didn't have an explicit address range,
1513              then use the information extracted from its child dies.  */
1514           if (! comp_unit_die.has_pc_info)
1515             {
1516               comp_unit_die.lowpc = lowpc;
1517               comp_unit_die.highpc = highpc;
1518             }
1519         }
1520       pst->textlow = comp_unit_die.lowpc + baseaddr;
1521       pst->texthigh = comp_unit_die.highpc + baseaddr;
1522
1523       pst->n_global_syms = objfile->global_psymbols.next -
1524         (objfile->global_psymbols.list + pst->globals_offset);
1525       pst->n_static_syms = objfile->static_psymbols.next -
1526         (objfile->static_psymbols.list + pst->statics_offset);
1527       sort_pst_symbols (pst);
1528
1529       /* If there is already a psymtab or symtab for a file of this
1530          name, remove it. (If there is a symtab, more drastic things
1531          also happen.) This happens in VxWorks.  */
1532       free_named_symtabs (pst->filename);
1533
1534       if (comp_unit_die.has_stmt_list)
1535         {
1536           /* Get the list of files included in the current compilation unit,
1537              and build a psymtab for each of them.  */
1538           dwarf2_build_include_psymtabs (&cu, &comp_unit_die, pst);
1539         }
1540
1541       info_ptr = beg_of_comp_unit + cu.header.length
1542                                   + cu.header.initial_length_size;
1543
1544       do_cleanups (back_to_inner);
1545     }
1546   do_cleanups (back_to);
1547 }
1548
1549 /* Load the DIEs for a secondary CU into memory.  */
1550
1551 static void
1552 load_comp_unit (struct dwarf2_per_cu_data *this_cu, struct objfile *objfile)
1553 {
1554   bfd *abfd = objfile->obfd;
1555   char *info_ptr, *beg_of_comp_unit;
1556   struct partial_die_info comp_unit_die;
1557   struct dwarf2_cu *cu;
1558   struct abbrev_info *abbrev;
1559   unsigned int bytes_read;
1560   struct cleanup *back_to;
1561
1562   info_ptr = dwarf2_per_objfile->info_buffer + this_cu->offset;
1563   beg_of_comp_unit = info_ptr;
1564
1565   cu = xmalloc (sizeof (struct dwarf2_cu));
1566   memset (cu, 0, sizeof (struct dwarf2_cu));
1567
1568   obstack_init (&cu->comp_unit_obstack);
1569
1570   cu->objfile = objfile;
1571   info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr, abfd);
1572
1573   /* Complete the cu_header.  */
1574   cu->header.offset = beg_of_comp_unit - dwarf2_per_objfile->info_buffer;
1575   cu->header.first_die_ptr = info_ptr;
1576   cu->header.cu_head_ptr = beg_of_comp_unit;
1577
1578   /* Read the abbrevs for this compilation unit into a table.  */
1579   dwarf2_read_abbrevs (abfd, cu);
1580   back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
1581
1582   /* Read the compilation unit die.  */
1583   abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
1584   info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
1585                                abfd, info_ptr, cu);
1586
1587   /* Set the language we're debugging.  */
1588   set_cu_language (comp_unit_die.language, cu);
1589
1590   /* Link this compilation unit into the compilation unit tree.  */
1591   this_cu->cu = cu;
1592   cu->per_cu = this_cu;
1593
1594   /* Check if comp unit has_children.
1595      If so, read the rest of the partial symbols from this comp unit.
1596      If not, there's no more debug_info for this comp unit. */
1597   if (comp_unit_die.has_children)
1598     load_partial_dies (abfd, info_ptr, 0, cu);
1599
1600   do_cleanups (back_to);
1601 }
1602
1603 /* Create a list of all compilation units in OBJFILE.  We do this only
1604    if an inter-comp-unit reference is found; presumably if there is one,
1605    there will be many, and one will occur early in the .debug_info section.
1606    So there's no point in building this list incrementally.  */
1607
1608 static void
1609 create_all_comp_units (struct objfile *objfile)
1610 {
1611   int n_allocated;
1612   int n_comp_units;
1613   struct dwarf2_per_cu_data **all_comp_units;
1614   char *info_ptr = dwarf2_per_objfile->info_buffer;
1615
1616   n_comp_units = 0;
1617   n_allocated = 10;
1618   all_comp_units = xmalloc (n_allocated
1619                             * sizeof (struct dwarf2_per_cu_data *));
1620   
1621   while (info_ptr < dwarf2_per_objfile->info_buffer + dwarf2_per_objfile->info_size)
1622     {
1623       struct comp_unit_head cu_header;
1624       char *beg_of_comp_unit;
1625       struct dwarf2_per_cu_data *this_cu;
1626       unsigned long offset;
1627       int bytes_read;
1628
1629       offset = info_ptr - dwarf2_per_objfile->info_buffer;
1630
1631       /* Read just enough information to find out where the next
1632          compilation unit is.  */
1633       cu_header.length = read_initial_length (objfile->obfd, info_ptr,
1634                                               &cu_header, &bytes_read);
1635
1636       /* Save the compilation unit for later lookup.  */
1637       this_cu = obstack_alloc (&objfile->objfile_obstack,
1638                                sizeof (struct dwarf2_per_cu_data));
1639       memset (this_cu, 0, sizeof (*this_cu));
1640       this_cu->offset = offset;
1641       this_cu->length = cu_header.length + cu_header.initial_length_size;
1642
1643       if (n_comp_units == n_allocated)
1644         {
1645           n_allocated *= 2;
1646           all_comp_units = xrealloc (all_comp_units,
1647                                      n_allocated
1648                                      * sizeof (struct dwarf2_per_cu_data *));
1649         }
1650       all_comp_units[n_comp_units++] = this_cu;
1651
1652       info_ptr = info_ptr + this_cu->length;
1653     }
1654
1655   dwarf2_per_objfile->all_comp_units
1656     = obstack_alloc (&objfile->objfile_obstack,
1657                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1658   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
1659           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
1660   xfree (all_comp_units);
1661   dwarf2_per_objfile->n_comp_units = n_comp_units;
1662 }
1663
1664 /* Process all loaded DIEs for compilation unit CU, starting at FIRST_DIE.
1665    Also set *LOWPC and *HIGHPC to the lowest and highest PC values found
1666    in CU.  */
1667
1668 static void
1669 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
1670                       CORE_ADDR *highpc, struct dwarf2_cu *cu)
1671 {
1672   struct objfile *objfile = cu->objfile;
1673   bfd *abfd = objfile->obfd;
1674   struct partial_die_info *pdi;
1675
1676   /* Now, march along the PDI's, descending into ones which have
1677      interesting children but skipping the children of the other ones,
1678      until we reach the end of the compilation unit.  */
1679
1680   pdi = first_die;
1681
1682   while (pdi != NULL)
1683     {
1684       fixup_partial_die (pdi, cu);
1685
1686       /* Anonymous namespaces have no name but have interesting
1687          children, so we need to look at them.  Ditto for anonymous
1688          enums.  */
1689
1690       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
1691           || pdi->tag == DW_TAG_enumeration_type)
1692         {
1693           switch (pdi->tag)
1694             {
1695             case DW_TAG_subprogram:
1696               if (pdi->has_pc_info)
1697                 {
1698                   if (pdi->lowpc < *lowpc)
1699                     {
1700                       *lowpc = pdi->lowpc;
1701                     }
1702                   if (pdi->highpc > *highpc)
1703                     {
1704                       *highpc = pdi->highpc;
1705                     }
1706                   if (!pdi->is_declaration)
1707                     {
1708                       add_partial_symbol (pdi, cu);
1709                     }
1710                 }
1711               break;
1712             case DW_TAG_variable:
1713             case DW_TAG_typedef:
1714             case DW_TAG_union_type:
1715               if (!pdi->is_declaration)
1716                 {
1717                   add_partial_symbol (pdi, cu);
1718                 }
1719               break;
1720             case DW_TAG_class_type:
1721             case DW_TAG_structure_type:
1722               if (!pdi->is_declaration)
1723                 {
1724                   add_partial_symbol (pdi, cu);
1725                 }
1726               break;
1727             case DW_TAG_enumeration_type:
1728               if (!pdi->is_declaration)
1729                 add_partial_enumeration (pdi, cu);
1730               break;
1731             case DW_TAG_base_type:
1732             case DW_TAG_subrange_type:
1733               /* File scope base type definitions are added to the partial
1734                  symbol table.  */
1735               add_partial_symbol (pdi, cu);
1736               break;
1737             case DW_TAG_namespace:
1738               add_partial_namespace (pdi, lowpc, highpc, cu);
1739               break;
1740             default:
1741               break;
1742             }
1743         }
1744
1745       /* If the die has a sibling, skip to the sibling.  */
1746
1747       pdi = pdi->die_sibling;
1748     }
1749 }
1750
1751 /* Functions used to compute the fully scoped name of a partial DIE.
1752
1753    Normally, this is simple.  For C++, the parent DIE's fully scoped
1754    name is concatenated with "::" and the partial DIE's name.  For
1755    Java, the same thing occurs except that "." is used instead of "::".
1756    Enumerators are an exception; they use the scope of their parent
1757    enumeration type, i.e. the name of the enumeration type is not
1758    prepended to the enumerator.
1759
1760    There are two complexities.  One is DW_AT_specification; in this
1761    case "parent" means the parent of the target of the specification,
1762    instead of the direct parent of the DIE.  The other is compilers
1763    which do not emit DW_TAG_namespace; in this case we try to guess
1764    the fully qualified name of structure types from their members'
1765    linkage names.  This must be done using the DIE's children rather
1766    than the children of any DW_AT_specification target.  We only need
1767    to do this for structures at the top level, i.e. if the target of
1768    any DW_AT_specification (if any; otherwise the DIE itself) does not
1769    have a parent.  */
1770
1771 /* Compute the scope prefix associated with PDI's parent, in
1772    compilation unit CU.  The result will be allocated on CU's
1773    comp_unit_obstack, or a copy of the already allocated PDI->NAME
1774    field.  NULL is returned if no prefix is necessary.  */
1775 static char *
1776 partial_die_parent_scope (struct partial_die_info *pdi,
1777                           struct dwarf2_cu *cu)
1778 {
1779   char *grandparent_scope;
1780   struct partial_die_info *parent, *real_pdi;
1781   struct dwarf2_cu *spec_cu;
1782
1783   /* We need to look at our parent DIE; if we have a DW_AT_specification,
1784      then this means the parent of the specification DIE.  */
1785
1786   real_pdi = pdi;
1787   spec_cu = cu;
1788   while (real_pdi->has_specification)
1789     real_pdi = find_partial_die (real_pdi->spec_offset, spec_cu, &spec_cu);
1790
1791   parent = real_pdi->die_parent;
1792   if (parent == NULL)
1793     return NULL;
1794
1795   if (parent->scope_set)
1796     return parent->scope;
1797
1798   fixup_partial_die (parent, cu);
1799
1800   grandparent_scope = partial_die_parent_scope (parent, spec_cu);
1801
1802   if (parent->tag == DW_TAG_namespace
1803       || parent->tag == DW_TAG_structure_type
1804       || parent->tag == DW_TAG_class_type
1805       || parent->tag == DW_TAG_union_type)
1806     {
1807       if (grandparent_scope == NULL)
1808         parent->scope = parent->name;
1809       else
1810         parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
1811                                          parent->name, cu);
1812     }
1813   else if (parent->tag == DW_TAG_enumeration_type)
1814     /* Enumerators should not get the name of the enumeration as a prefix.  */
1815     parent->scope = grandparent_scope;
1816   else
1817     {
1818       /* FIXME drow/2004-04-01: What should we be doing with
1819          function-local names?  For partial symbols, we should probably be
1820          ignoring them.  */
1821       complaint (&symfile_complaints,
1822                  "unhandled containing DIE tag %d for DIE at %d",
1823                  parent->tag, pdi->offset);
1824       parent->scope = grandparent_scope;
1825     }
1826
1827   parent->scope_set = 1;
1828   return parent->scope;
1829 }
1830
1831 /* Return the fully scoped name associated with PDI, from compilation unit
1832    CU.  The result will be allocated with malloc.  */
1833 static char *
1834 partial_die_full_name (struct partial_die_info *pdi,
1835                        struct dwarf2_cu *cu)
1836 {
1837   char *parent_scope;
1838
1839   parent_scope = partial_die_parent_scope (pdi, cu);
1840   if (parent_scope == NULL)
1841     return NULL;
1842   else
1843     return typename_concat (NULL, parent_scope, pdi->name, cu);
1844 }
1845
1846 static void
1847 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
1848 {
1849   struct objfile *objfile = cu->objfile;
1850   CORE_ADDR addr = 0;
1851   char *actual_name;
1852   const char *my_prefix;
1853   const struct partial_symbol *psym = NULL;
1854   CORE_ADDR baseaddr;
1855   int built_actual_name = 0;
1856
1857   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1858
1859   actual_name = NULL;
1860
1861   if (pdi_needs_namespace (pdi->tag))
1862     {
1863       actual_name = partial_die_full_name (pdi, cu);
1864       if (actual_name)
1865         built_actual_name = 1;
1866     }
1867
1868   if (actual_name == NULL)
1869     actual_name = pdi->name;
1870
1871   switch (pdi->tag)
1872     {
1873     case DW_TAG_subprogram:
1874       if (pdi->is_external)
1875         {
1876           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
1877              mst_text, objfile); */
1878           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1879                                       VAR_DOMAIN, LOC_BLOCK,
1880                                       &objfile->global_psymbols,
1881                                       0, pdi->lowpc + baseaddr,
1882                                       cu->language, objfile);
1883         }
1884       else
1885         {
1886           /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
1887              mst_file_text, objfile); */
1888           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1889                                       VAR_DOMAIN, LOC_BLOCK,
1890                                       &objfile->static_psymbols,
1891                                       0, pdi->lowpc + baseaddr,
1892                                       cu->language, objfile);
1893         }
1894       break;
1895     case DW_TAG_variable:
1896       if (pdi->is_external)
1897         {
1898           /* Global Variable.
1899              Don't enter into the minimal symbol tables as there is
1900              a minimal symbol table entry from the ELF symbols already.
1901              Enter into partial symbol table if it has a location
1902              descriptor or a type.
1903              If the location descriptor is missing, new_symbol will create
1904              a LOC_UNRESOLVED symbol, the address of the variable will then
1905              be determined from the minimal symbol table whenever the variable
1906              is referenced.
1907              The address for the partial symbol table entry is not
1908              used by GDB, but it comes in handy for debugging partial symbol
1909              table building.  */
1910
1911           if (pdi->locdesc)
1912             addr = decode_locdesc (pdi->locdesc, cu);
1913           if (pdi->locdesc || pdi->has_type)
1914             psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1915                                         VAR_DOMAIN, LOC_STATIC,
1916                                         &objfile->global_psymbols,
1917                                         0, addr + baseaddr,
1918                                         cu->language, objfile);
1919         }
1920       else
1921         {
1922           /* Static Variable. Skip symbols without location descriptors.  */
1923           if (pdi->locdesc == NULL)
1924             return;
1925           addr = decode_locdesc (pdi->locdesc, cu);
1926           /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
1927              mst_file_data, objfile); */
1928           psym = add_psymbol_to_list (actual_name, strlen (actual_name),
1929                                       VAR_DOMAIN, LOC_STATIC,
1930                                       &objfile->static_psymbols,
1931                                       0, addr + baseaddr,
1932                                       cu->language, objfile);
1933         }
1934       break;
1935     case DW_TAG_typedef:
1936     case DW_TAG_base_type:
1937     case DW_TAG_subrange_type:
1938       add_psymbol_to_list (actual_name, strlen (actual_name),
1939                            VAR_DOMAIN, LOC_TYPEDEF,
1940                            &objfile->static_psymbols,
1941                            0, (CORE_ADDR) 0, cu->language, objfile);
1942       break;
1943     case DW_TAG_namespace:
1944       add_psymbol_to_list (actual_name, strlen (actual_name),
1945                            VAR_DOMAIN, LOC_TYPEDEF,
1946                            &objfile->global_psymbols,
1947                            0, (CORE_ADDR) 0, cu->language, objfile);
1948       break;
1949     case DW_TAG_class_type:
1950     case DW_TAG_structure_type:
1951     case DW_TAG_union_type:
1952     case DW_TAG_enumeration_type:
1953       /* Skip aggregate types without children, these are external
1954          references.  */
1955       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
1956          static vs. global.  */
1957       if (pdi->has_children == 0)
1958         return;
1959       add_psymbol_to_list (actual_name, strlen (actual_name),
1960                            STRUCT_DOMAIN, LOC_TYPEDEF,
1961                            (cu->language == language_cplus
1962                             || cu->language == language_java)
1963                            ? &objfile->global_psymbols
1964                            : &objfile->static_psymbols,
1965                            0, (CORE_ADDR) 0, cu->language, objfile);
1966
1967       if (cu->language == language_cplus
1968           || cu->language == language_java)
1969         {
1970           /* For C++ and Java, these implicitly act as typedefs as well. */
1971           add_psymbol_to_list (actual_name, strlen (actual_name),
1972                                VAR_DOMAIN, LOC_TYPEDEF,
1973                                &objfile->global_psymbols,
1974                                0, (CORE_ADDR) 0, cu->language, objfile);
1975         }
1976       break;
1977     case DW_TAG_enumerator:
1978       add_psymbol_to_list (actual_name, strlen (actual_name),
1979                            VAR_DOMAIN, LOC_CONST,
1980                            (cu->language == language_cplus
1981                             || cu->language == language_java)
1982                            ? &objfile->global_psymbols
1983                            : &objfile->static_psymbols,
1984                            0, (CORE_ADDR) 0, cu->language, objfile);
1985       break;
1986     default:
1987       break;
1988     }
1989
1990   /* Check to see if we should scan the name for possible namespace
1991      info.  Only do this if this is C++, if we don't have namespace
1992      debugging info in the file, if the psym is of an appropriate type
1993      (otherwise we'll have psym == NULL), and if we actually had a
1994      mangled name to begin with.  */
1995
1996   /* FIXME drow/2004-02-22: Why don't we do this for classes, i.e. the
1997      cases which do not set PSYM above?  */
1998
1999   if (cu->language == language_cplus
2000       && cu->has_namespace_info == 0
2001       && psym != NULL
2002       && SYMBOL_CPLUS_DEMANGLED_NAME (psym) != NULL)
2003     cp_check_possible_namespace_symbols (SYMBOL_CPLUS_DEMANGLED_NAME (psym),
2004                                          objfile);
2005
2006   if (built_actual_name)
2007     xfree (actual_name);
2008 }
2009
2010 /* Determine whether a die of type TAG living in a C++ class or
2011    namespace needs to have the name of the scope prepended to the
2012    name listed in the die.  */
2013
2014 static int
2015 pdi_needs_namespace (enum dwarf_tag tag)
2016 {
2017   switch (tag)
2018     {
2019     case DW_TAG_namespace:
2020     case DW_TAG_typedef:
2021     case DW_TAG_class_type:
2022     case DW_TAG_structure_type:
2023     case DW_TAG_union_type:
2024     case DW_TAG_enumeration_type:
2025     case DW_TAG_enumerator:
2026       return 1;
2027     default:
2028       return 0;
2029     }
2030 }
2031
2032 /* Read a partial die corresponding to a namespace; also, add a symbol
2033    corresponding to that namespace to the symbol table.  NAMESPACE is
2034    the name of the enclosing namespace.  */
2035
2036 static void
2037 add_partial_namespace (struct partial_die_info *pdi,
2038                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
2039                        struct dwarf2_cu *cu)
2040 {
2041   struct objfile *objfile = cu->objfile;
2042
2043   /* Add a symbol for the namespace.  */
2044
2045   add_partial_symbol (pdi, cu);
2046
2047   /* Now scan partial symbols in that namespace.  */
2048
2049   if (pdi->has_children)
2050     scan_partial_symbols (pdi->die_child, lowpc, highpc, cu);
2051 }
2052
2053 /* See if we can figure out if the class lives in a namespace.  We do
2054    this by looking for a member function; its demangled name will
2055    contain namespace info, if there is any.  */
2056
2057 static void
2058 guess_structure_name (struct partial_die_info *struct_pdi,
2059                       struct dwarf2_cu *cu)
2060 {
2061   if ((cu->language == language_cplus
2062        || cu->language == language_java)
2063       && cu->has_namespace_info == 0
2064       && struct_pdi->has_children)
2065     {
2066       /* NOTE: carlton/2003-10-07: Getting the info this way changes
2067          what template types look like, because the demangler
2068          frequently doesn't give the same name as the debug info.  We
2069          could fix this by only using the demangled name to get the
2070          prefix (but see comment in read_structure_type).  */
2071
2072       struct partial_die_info *child_pdi = struct_pdi->die_child;
2073       struct partial_die_info *real_pdi;
2074       struct dwarf2_cu *spec_cu;
2075
2076       /* If this DIE (this DIE's specification, if any) has a parent, then
2077          we should not do this.  We'll prepend the parent's fully qualified
2078          name when we create the partial symbol.  */
2079
2080       real_pdi = struct_pdi;
2081       spec_cu = cu;
2082       while (real_pdi->has_specification)
2083         real_pdi = find_partial_die (real_pdi->spec_offset, spec_cu, &spec_cu);
2084
2085       if (real_pdi->die_parent != NULL)
2086         return;
2087
2088       while (child_pdi != NULL)
2089         {
2090           if (child_pdi->tag == DW_TAG_subprogram)
2091             {
2092               char *actual_class_name
2093                 = language_class_name_from_physname (cu->language_defn,
2094                                                      child_pdi->name);
2095               if (actual_class_name != NULL)
2096                 {
2097                   struct_pdi->name
2098                     = obsavestring (actual_class_name,
2099                                     strlen (actual_class_name),
2100                                     &cu->comp_unit_obstack);
2101                   xfree (actual_class_name);
2102                 }
2103               break;
2104             }
2105
2106           child_pdi = child_pdi->die_sibling;
2107         }
2108     }
2109 }
2110
2111 /* Read a partial die corresponding to an enumeration type.  */
2112
2113 static void
2114 add_partial_enumeration (struct partial_die_info *enum_pdi,
2115                          struct dwarf2_cu *cu)
2116 {
2117   struct objfile *objfile = cu->objfile;
2118   bfd *abfd = objfile->obfd;
2119   struct partial_die_info *pdi;
2120
2121   if (enum_pdi->name != NULL)
2122     add_partial_symbol (enum_pdi, cu);
2123
2124   pdi = enum_pdi->die_child;
2125   while (pdi)
2126     {
2127       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
2128         complaint (&symfile_complaints, "malformed enumerator DIE ignored");
2129       else
2130         add_partial_symbol (pdi, cu);
2131       pdi = pdi->die_sibling;
2132     }
2133 }
2134
2135 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
2136    Return the corresponding abbrev, or NULL if the number is zero (indicating
2137    an empty DIE).  In either case *BYTES_READ will be set to the length of
2138    the initial number.  */
2139
2140 static struct abbrev_info *
2141 peek_die_abbrev (char *info_ptr, int *bytes_read, struct dwarf2_cu *cu)
2142 {
2143   bfd *abfd = cu->objfile->obfd;
2144   unsigned int abbrev_number;
2145   struct abbrev_info *abbrev;
2146
2147   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
2148
2149   if (abbrev_number == 0)
2150     return NULL;
2151
2152   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
2153   if (!abbrev)
2154     {
2155       error ("Dwarf Error: Could not find abbrev number %d [in module %s]", abbrev_number,
2156                       bfd_get_filename (abfd));
2157     }
2158
2159   return abbrev;
2160 }
2161
2162 /* Scan the debug information for CU starting at INFO_PTR.  Returns a
2163    pointer to the end of a series of DIEs, terminated by an empty
2164    DIE.  Any children of the skipped DIEs will also be skipped.  */
2165
2166 static char *
2167 skip_children (char *info_ptr, struct dwarf2_cu *cu)
2168 {
2169   struct abbrev_info *abbrev;
2170   unsigned int bytes_read;
2171
2172   while (1)
2173     {
2174       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
2175       if (abbrev == NULL)
2176         return info_ptr + bytes_read;
2177       else
2178         info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
2179     }
2180 }
2181
2182 /* Scan the debug information for CU starting at INFO_PTR.  INFO_PTR
2183    should point just after the initial uleb128 of a DIE, and the
2184    abbrev corresponding to that skipped uleb128 should be passed in
2185    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
2186    children.  */
2187
2188 static char *
2189 skip_one_die (char *info_ptr, struct abbrev_info *abbrev,
2190               struct dwarf2_cu *cu)
2191 {
2192   unsigned int bytes_read;
2193   struct attribute attr;
2194   bfd *abfd = cu->objfile->obfd;
2195   unsigned int form, i;
2196
2197   for (i = 0; i < abbrev->num_attrs; i++)
2198     {
2199       /* The only abbrev we care about is DW_AT_sibling.  */
2200       if (abbrev->attrs[i].name == DW_AT_sibling)
2201         {
2202           read_attribute (&attr, &abbrev->attrs[i],
2203                           abfd, info_ptr, cu);
2204           if (attr.form == DW_FORM_ref_addr)
2205             complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
2206           else
2207             return dwarf2_per_objfile->info_buffer
2208               + dwarf2_get_ref_die_offset (&attr, cu);
2209         }
2210
2211       /* If it isn't DW_AT_sibling, skip this attribute.  */
2212       form = abbrev->attrs[i].form;
2213     skip_attribute:
2214       switch (form)
2215         {
2216         case DW_FORM_addr:
2217         case DW_FORM_ref_addr:
2218           info_ptr += cu->header.addr_size;
2219           break;
2220         case DW_FORM_data1:
2221         case DW_FORM_ref1:
2222         case DW_FORM_flag:
2223           info_ptr += 1;
2224           break;
2225         case DW_FORM_data2:
2226         case DW_FORM_ref2:
2227           info_ptr += 2;
2228           break;
2229         case DW_FORM_data4:
2230         case DW_FORM_ref4:
2231           info_ptr += 4;
2232           break;
2233         case DW_FORM_data8:
2234         case DW_FORM_ref8:
2235           info_ptr += 8;
2236           break;
2237         case DW_FORM_string:
2238           read_string (abfd, info_ptr, &bytes_read);
2239           info_ptr += bytes_read;
2240           break;
2241         case DW_FORM_strp:
2242           info_ptr += cu->header.offset_size;
2243           break;
2244         case DW_FORM_block:
2245           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2246           info_ptr += bytes_read;
2247           break;
2248         case DW_FORM_block1:
2249           info_ptr += 1 + read_1_byte (abfd, info_ptr);
2250           break;
2251         case DW_FORM_block2:
2252           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
2253           break;
2254         case DW_FORM_block4:
2255           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
2256           break;
2257         case DW_FORM_sdata:
2258         case DW_FORM_udata:
2259         case DW_FORM_ref_udata:
2260           info_ptr = skip_leb128 (abfd, info_ptr);
2261           break;
2262         case DW_FORM_indirect:
2263           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2264           info_ptr += bytes_read;
2265           /* We need to continue parsing from here, so just go back to
2266              the top.  */
2267           goto skip_attribute;
2268
2269         default:
2270           error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
2271                  dwarf_form_name (form),
2272                  bfd_get_filename (abfd));
2273         }
2274     }
2275
2276   if (abbrev->has_children)
2277     return skip_children (info_ptr, cu);
2278   else
2279     return info_ptr;
2280 }
2281
2282 /* Locate ORIG_PDI's sibling; INFO_PTR should point to the start of
2283    the next DIE after ORIG_PDI.  */
2284
2285 static char *
2286 locate_pdi_sibling (struct partial_die_info *orig_pdi, char *info_ptr,
2287                     bfd *abfd, struct dwarf2_cu *cu)
2288 {
2289   /* Do we know the sibling already?  */
2290
2291   if (orig_pdi->sibling)
2292     return orig_pdi->sibling;
2293
2294   /* Are there any children to deal with?  */
2295
2296   if (!orig_pdi->has_children)
2297     return info_ptr;
2298
2299   /* Skip the children the long way.  */
2300
2301   return skip_children (info_ptr, cu);
2302 }
2303
2304 /* Expand this partial symbol table into a full symbol table.  */
2305
2306 static void
2307 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
2308 {
2309   /* FIXME: This is barely more than a stub.  */
2310   if (pst != NULL)
2311     {
2312       if (pst->readin)
2313         {
2314           warning ("bug: psymtab for %s is already read in.", pst->filename);
2315         }
2316       else
2317         {
2318           if (info_verbose)
2319             {
2320               printf_filtered ("Reading in symbols for %s...", pst->filename);
2321               gdb_flush (gdb_stdout);
2322             }
2323
2324           psymtab_to_symtab_1 (pst);
2325
2326           /* Finish up the debug error message.  */
2327           if (info_verbose)
2328             printf_filtered ("done.\n");
2329         }
2330     }
2331 }
2332
2333 static void
2334 psymtab_to_symtab_1 (struct partial_symtab *pst)
2335 {
2336   struct objfile *objfile = pst->objfile;
2337   bfd *abfd = objfile->obfd;
2338   struct dwarf2_cu cu;
2339   struct die_info *dies;
2340   unsigned long offset;
2341   CORE_ADDR lowpc, highpc;
2342   struct die_info *child_die;
2343   char *info_ptr;
2344   struct symtab *symtab;
2345   struct cleanup *back_to;
2346   struct attribute *attr;
2347   CORE_ADDR baseaddr;
2348   int i;
2349
2350   for (i = 0; i < pst->number_of_dependencies; i++)
2351     if (!pst->dependencies[i]->readin)
2352       {
2353         /* Inform about additional files that need to be read in.  */
2354         if (info_verbose)
2355           {
2356             fputs_filtered (" ", gdb_stdout);
2357             wrap_here ("");
2358             fputs_filtered ("and ", gdb_stdout);
2359             wrap_here ("");
2360             printf_filtered ("%s...", pst->dependencies[i]->filename);
2361             wrap_here ("");     /* Flush output */
2362             gdb_flush (gdb_stdout);
2363           }
2364         psymtab_to_symtab_1 (pst->dependencies[i]);
2365       }
2366
2367   if (pst->read_symtab_private == NULL)
2368     {
2369       /* It's an include file, no symbols to read for it.
2370          Everything is in the parent symtab.  */
2371       pst->readin = 1;
2372       return;
2373     }
2374
2375   dwarf2_per_objfile = objfile_data (pst->objfile, dwarf2_objfile_data_key);
2376
2377   /* Set local variables from the partial symbol table info.  */
2378   offset = DWARF_INFO_OFFSET (pst);
2379
2380   info_ptr = dwarf2_per_objfile->info_buffer + offset;
2381   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2382
2383   /* We're in the global namespace.  */
2384   processing_current_prefix = "";
2385
2386   memset (&cu, 0, sizeof (struct dwarf2_cu));
2387   obstack_init (&cu.comp_unit_obstack);
2388   back_to = make_cleanup (free_stack_comp_unit, &cu);
2389
2390   buildsym_init ();
2391   make_cleanup (really_free_pendings, NULL);
2392
2393   cu.objfile = objfile;
2394
2395   /* read in the comp_unit header  */
2396   info_ptr = read_comp_unit_head (&cu.header, info_ptr, abfd);
2397
2398   /* Read the abbrevs for this compilation unit  */
2399   dwarf2_read_abbrevs (abfd, &cu);
2400   make_cleanup (dwarf2_free_abbrev_table, &cu);
2401
2402   cu.header.offset = offset;
2403
2404   cu.list_in_scope = &file_symbols;
2405
2406   dies = read_comp_unit (info_ptr, abfd, &cu);
2407
2408   make_cleanup_free_die_list (dies);
2409
2410   /* Find the base address of the compilation unit for range lists and
2411      location lists.  It will normally be specified by DW_AT_low_pc.
2412      In DWARF-3 draft 4, the base address could be overridden by
2413      DW_AT_entry_pc.  It's been removed, but GCC still uses this for
2414      compilation units with discontinuous ranges.  */
2415
2416   cu.header.base_known = 0;
2417   cu.header.base_address = 0;
2418
2419   attr = dwarf2_attr (dies, DW_AT_entry_pc, &cu);
2420   if (attr)
2421     {
2422       cu.header.base_address = DW_ADDR (attr);
2423       cu.header.base_known = 1;
2424     }
2425   else
2426     {
2427       attr = dwarf2_attr (dies, DW_AT_low_pc, &cu);
2428       if (attr)
2429         {
2430           cu.header.base_address = DW_ADDR (attr);
2431           cu.header.base_known = 1;
2432         }
2433     }
2434
2435   /* Do line number decoding in read_file_scope () */
2436   process_die (dies, &cu);
2437
2438   /* Some compilers don't define a DW_AT_high_pc attribute for the
2439      compilation unit.  If the DW_AT_high_pc is missing, synthesize
2440      it, by scanning the DIE's below the compilation unit.  */
2441   get_scope_pc_bounds (dies, &lowpc, &highpc, &cu);
2442
2443   symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
2444
2445   /* Set symtab language to language from DW_AT_language.
2446      If the compilation is from a C file generated by language preprocessors,
2447      do not set the language if it was already deduced by start_subfile.  */
2448   if (symtab != NULL
2449       && !(cu.language == language_c && symtab->language != language_c))
2450     {
2451       symtab->language = cu.language;
2452     }
2453   pst->symtab = symtab;
2454   pst->readin = 1;
2455
2456   do_cleanups (back_to);
2457 }
2458
2459 /* Process a die and its children.  */
2460
2461 static void
2462 process_die (struct die_info *die, struct dwarf2_cu *cu)
2463 {
2464   switch (die->tag)
2465     {
2466     case DW_TAG_padding:
2467       break;
2468     case DW_TAG_compile_unit:
2469       read_file_scope (die, cu);
2470       break;
2471     case DW_TAG_subprogram:
2472       read_subroutine_type (die, cu);
2473       read_func_scope (die, cu);
2474       break;
2475     case DW_TAG_inlined_subroutine:
2476       /* FIXME:  These are ignored for now.
2477          They could be used to set breakpoints on all inlined instances
2478          of a function and make GDB `next' properly over inlined functions.  */
2479       break;
2480     case DW_TAG_lexical_block:
2481     case DW_TAG_try_block:
2482     case DW_TAG_catch_block:
2483       read_lexical_block_scope (die, cu);
2484       break;
2485     case DW_TAG_class_type:
2486     case DW_TAG_structure_type:
2487     case DW_TAG_union_type:
2488       read_structure_type (die, cu);
2489       process_structure_scope (die, cu);
2490       break;
2491     case DW_TAG_enumeration_type:
2492       read_enumeration_type (die, cu);
2493       process_enumeration_scope (die, cu);
2494       break;
2495
2496     /* FIXME drow/2004-03-14: These initialize die->type, but do not create
2497        a symbol or process any children.  Therefore it doesn't do anything
2498        that won't be done on-demand by read_type_die.  */
2499     case DW_TAG_subroutine_type:
2500       read_subroutine_type (die, cu);
2501       break;
2502     case DW_TAG_array_type:
2503       read_array_type (die, cu);
2504       break;
2505     case DW_TAG_pointer_type:
2506       read_tag_pointer_type (die, cu);
2507       break;
2508     case DW_TAG_ptr_to_member_type:
2509       read_tag_ptr_to_member_type (die, cu);
2510       break;
2511     case DW_TAG_reference_type:
2512       read_tag_reference_type (die, cu);
2513       break;
2514     case DW_TAG_string_type:
2515       read_tag_string_type (die, cu);
2516       break;
2517     /* END FIXME */
2518
2519     case DW_TAG_base_type:
2520       read_base_type (die, cu);
2521       /* Add a typedef symbol for the type definition, if it has a
2522          DW_AT_name.  */
2523       new_symbol (die, die->type, cu);
2524       break;
2525     case DW_TAG_subrange_type:
2526       read_subrange_type (die, cu);
2527       /* Add a typedef symbol for the type definition, if it has a
2528          DW_AT_name.  */
2529       new_symbol (die, die->type, cu);
2530       break;
2531     case DW_TAG_common_block:
2532       read_common_block (die, cu);
2533       break;
2534     case DW_TAG_common_inclusion:
2535       break;
2536     case DW_TAG_namespace:
2537       processing_has_namespace_info = 1;
2538       read_namespace (die, cu);
2539       break;
2540     case DW_TAG_imported_declaration:
2541     case DW_TAG_imported_module:
2542       /* FIXME: carlton/2002-10-16: Eventually, we should use the
2543          information contained in these.  DW_TAG_imported_declaration
2544          dies shouldn't have children; DW_TAG_imported_module dies
2545          shouldn't in the C++ case, but conceivably could in the
2546          Fortran case, so we'll have to replace this gdb_assert if
2547          Fortran compilers start generating that info.  */
2548       processing_has_namespace_info = 1;
2549       gdb_assert (die->child == NULL);
2550       break;
2551     default:
2552       new_symbol (die, NULL, cu);
2553       break;
2554     }
2555 }
2556
2557 static void
2558 initialize_cu_func_list (struct dwarf2_cu *cu)
2559 {
2560   cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
2561 }
2562
2563 static void
2564 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
2565 {
2566   struct objfile *objfile = cu->objfile;
2567   struct comp_unit_head *cu_header = &cu->header;
2568   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2569   CORE_ADDR lowpc = ((CORE_ADDR) -1);
2570   CORE_ADDR highpc = ((CORE_ADDR) 0);
2571   struct attribute *attr;
2572   char *name = "<unknown>";
2573   char *comp_dir = NULL;
2574   struct die_info *child_die;
2575   bfd *abfd = objfile->obfd;
2576   struct line_header *line_header = 0;
2577   CORE_ADDR baseaddr;
2578   
2579   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2580
2581   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
2582
2583   /* If we didn't find a lowpc, set it to highpc to avoid complaints
2584      from finish_block.  */
2585   if (lowpc == ((CORE_ADDR) -1))
2586     lowpc = highpc;
2587   lowpc += baseaddr;
2588   highpc += baseaddr;
2589
2590   attr = dwarf2_attr (die, DW_AT_name, cu);
2591   if (attr)
2592     {
2593       name = DW_STRING (attr);
2594     }
2595   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
2596   if (attr)
2597     {
2598       comp_dir = DW_STRING (attr);
2599       if (comp_dir)
2600         {
2601           /* Irix 6.2 native cc prepends <machine>.: to the compilation
2602              directory, get rid of it.  */
2603           char *cp = strchr (comp_dir, ':');
2604
2605           if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2606             comp_dir = cp + 1;
2607         }
2608     }
2609
2610   attr = dwarf2_attr (die, DW_AT_language, cu);
2611   if (attr)
2612     {
2613       set_cu_language (DW_UNSND (attr), cu);
2614     }
2615
2616   attr = dwarf2_attr (die, DW_AT_producer, cu);
2617   if (attr) 
2618     cu->producer = DW_STRING (attr);
2619   
2620   /* We assume that we're processing GCC output. */
2621   processing_gcc_compilation = 2;
2622 #if 0
2623   /* FIXME:Do something here.  */
2624   if (dip->at_producer != NULL)
2625     {
2626       handle_producer (dip->at_producer);
2627     }
2628 #endif
2629
2630   /* The compilation unit may be in a different language or objfile,
2631      zero out all remembered fundamental types.  */
2632   memset (cu->ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
2633
2634   start_symtab (name, comp_dir, lowpc);
2635   record_debugformat ("DWARF 2");
2636
2637   initialize_cu_func_list (cu);
2638
2639   /* Process all dies in compilation unit.  */
2640   if (die->child != NULL)
2641     {
2642       child_die = die->child;
2643       while (child_die && child_die->tag)
2644         {
2645           process_die (child_die, cu);
2646           child_die = sibling_die (child_die);
2647         }
2648     }
2649
2650   /* Decode line number information if present.  */
2651   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2652   if (attr)
2653     {
2654       unsigned int line_offset = DW_UNSND (attr);
2655       line_header = dwarf_decode_line_header (line_offset, abfd, cu);
2656       if (line_header)
2657         {
2658           make_cleanup ((make_cleanup_ftype *) free_line_header,
2659                         (void *) line_header);
2660           dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
2661         }
2662     }
2663
2664   /* Decode macro information, if present.  Dwarf 2 macro information
2665      refers to information in the line number info statement program
2666      header, so we can only read it if we've read the header
2667      successfully.  */
2668   attr = dwarf2_attr (die, DW_AT_macro_info, cu);
2669   if (attr && line_header)
2670     {
2671       unsigned int macro_offset = DW_UNSND (attr);
2672       dwarf_decode_macros (line_header, macro_offset,
2673                            comp_dir, abfd, cu);
2674     }
2675   do_cleanups (back_to);
2676 }
2677
2678 static void
2679 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
2680                      struct dwarf2_cu *cu)
2681 {
2682   struct function_range *thisfn;
2683
2684   thisfn = (struct function_range *)
2685     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
2686   thisfn->name = name;
2687   thisfn->lowpc = lowpc;
2688   thisfn->highpc = highpc;
2689   thisfn->seen_line = 0;
2690   thisfn->next = NULL;
2691
2692   if (cu->last_fn == NULL)
2693       cu->first_fn = thisfn;
2694   else
2695       cu->last_fn->next = thisfn;
2696
2697   cu->last_fn = thisfn;
2698 }
2699
2700 static void
2701 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
2702 {
2703   struct objfile *objfile = cu->objfile;
2704   struct context_stack *new;
2705   CORE_ADDR lowpc;
2706   CORE_ADDR highpc;
2707   struct die_info *child_die;
2708   struct attribute *attr;
2709   char *name;
2710   const char *previous_prefix = processing_current_prefix;
2711   struct cleanup *back_to = NULL;
2712   CORE_ADDR baseaddr;
2713
2714   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2715
2716   name = dwarf2_linkage_name (die, cu);
2717
2718   /* Ignore functions with missing or empty names and functions with
2719      missing or invalid low and high pc attributes.  */
2720   if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2721     return;
2722
2723   if (cu->language == language_cplus
2724       || cu->language == language_java)
2725     {
2726       struct die_info *spec_die = die_specification (die, cu);
2727
2728       /* NOTE: carlton/2004-01-23: We have to be careful in the
2729          presence of DW_AT_specification.  For example, with GCC 3.4,
2730          given the code
2731
2732            namespace N {
2733              void foo() {
2734                // Definition of N::foo.
2735              }
2736            }
2737
2738          then we'll have a tree of DIEs like this:
2739
2740          1: DW_TAG_compile_unit
2741            2: DW_TAG_namespace        // N
2742              3: DW_TAG_subprogram     // declaration of N::foo
2743            4: DW_TAG_subprogram       // definition of N::foo
2744                 DW_AT_specification   // refers to die #3
2745
2746          Thus, when processing die #4, we have to pretend that we're
2747          in the context of its DW_AT_specification, namely the contex
2748          of die #3.  */
2749         
2750       if (spec_die != NULL)
2751         {
2752           char *specification_prefix = determine_prefix (spec_die, cu);
2753           processing_current_prefix = specification_prefix;
2754           back_to = make_cleanup (xfree, specification_prefix);
2755         }
2756     }
2757
2758   lowpc += baseaddr;
2759   highpc += baseaddr;
2760
2761   /* Record the function range for dwarf_decode_lines.  */
2762   add_to_cu_func_list (name, lowpc, highpc, cu);
2763
2764   new = push_context (0, lowpc);
2765   new->name = new_symbol (die, die->type, cu);
2766
2767   /* If there is a location expression for DW_AT_frame_base, record
2768      it.  */
2769   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
2770   if (attr)
2771     /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
2772        expression is being recorded directly in the function's symbol
2773        and not in a separate frame-base object.  I guess this hack is
2774        to avoid adding some sort of frame-base adjunct/annex to the
2775        function's symbol :-(.  The problem with doing this is that it
2776        results in a function symbol with a location expression that
2777        has nothing to do with the location of the function, ouch!  The
2778        relationship should be: a function's symbol has-a frame base; a
2779        frame-base has-a location expression.  */
2780     dwarf2_symbol_mark_computed (attr, new->name, cu);
2781
2782   cu->list_in_scope = &local_symbols;
2783
2784   if (die->child != NULL)
2785     {
2786       child_die = die->child;
2787       while (child_die && child_die->tag)
2788         {
2789           process_die (child_die, cu);
2790           child_die = sibling_die (child_die);
2791         }
2792     }
2793
2794   new = pop_context ();
2795   /* Make a block for the local symbols within.  */
2796   finish_block (new->name, &local_symbols, new->old_blocks,
2797                 lowpc, highpc, objfile);
2798   
2799   /* In C++, we can have functions nested inside functions (e.g., when
2800      a function declares a class that has methods).  This means that
2801      when we finish processing a function scope, we may need to go
2802      back to building a containing block's symbol lists.  */
2803   local_symbols = new->locals;
2804   param_symbols = new->params;
2805
2806   /* If we've finished processing a top-level function, subsequent
2807      symbols go in the file symbol list.  */
2808   if (outermost_context_p ())
2809     cu->list_in_scope = &file_symbols;
2810
2811   processing_current_prefix = previous_prefix;
2812   if (back_to != NULL)
2813     do_cleanups (back_to);
2814 }
2815
2816 /* Process all the DIES contained within a lexical block scope.  Start
2817    a new scope, process the dies, and then close the scope.  */
2818
2819 static void
2820 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
2821 {
2822   struct objfile *objfile = cu->objfile;
2823   struct context_stack *new;
2824   CORE_ADDR lowpc, highpc;
2825   struct die_info *child_die;
2826   CORE_ADDR baseaddr;
2827
2828   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2829
2830   /* Ignore blocks with missing or invalid low and high pc attributes.  */
2831   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
2832      as multiple lexical blocks?  Handling children in a sane way would
2833      be nasty.  Might be easier to properly extend generic blocks to 
2834      describe ranges.  */
2835   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu))
2836     return;
2837   lowpc += baseaddr;
2838   highpc += baseaddr;
2839
2840   push_context (0, lowpc);
2841   if (die->child != NULL)
2842     {
2843       child_die = die->child;
2844       while (child_die && child_die->tag)
2845         {
2846           process_die (child_die, cu);
2847           child_die = sibling_die (child_die);
2848         }
2849     }
2850   new = pop_context ();
2851
2852   if (local_symbols != NULL)
2853     {
2854       finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
2855                     highpc, objfile);
2856     }
2857   local_symbols = new->locals;
2858 }
2859
2860 /* Get low and high pc attributes from a die.  Return 1 if the attributes
2861    are present and valid, otherwise, return 0.  Return -1 if the range is
2862    discontinuous, i.e. derived from DW_AT_ranges information.  */
2863 static int
2864 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
2865                       CORE_ADDR *highpc, struct dwarf2_cu *cu)
2866 {
2867   struct objfile *objfile = cu->objfile;
2868   struct comp_unit_head *cu_header = &cu->header;
2869   struct attribute *attr;
2870   bfd *obfd = objfile->obfd;
2871   CORE_ADDR low = 0;
2872   CORE_ADDR high = 0;
2873   int ret = 0;
2874
2875   attr = dwarf2_attr (die, DW_AT_high_pc, cu);
2876   if (attr)
2877     {
2878       high = DW_ADDR (attr);
2879       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2880       if (attr)
2881         low = DW_ADDR (attr);
2882       else
2883         /* Found high w/o low attribute.  */
2884         return 0;
2885
2886       /* Found consecutive range of addresses.  */
2887       ret = 1;
2888     }
2889   else
2890     {
2891       attr = dwarf2_attr (die, DW_AT_ranges, cu);
2892       if (attr != NULL)
2893         {
2894           unsigned int addr_size = cu_header->addr_size;
2895           CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2896           /* Value of the DW_AT_ranges attribute is the offset in the
2897              .debug_ranges section.  */
2898           unsigned int offset = DW_UNSND (attr);
2899           /* Base address selection entry.  */
2900           CORE_ADDR base;
2901           int found_base;
2902           int dummy;
2903           char *buffer;
2904           CORE_ADDR marker;
2905           int low_set;
2906  
2907           found_base = cu_header->base_known;
2908           base = cu_header->base_address;
2909
2910           if (offset >= dwarf2_per_objfile->ranges_size)
2911             {
2912               complaint (&symfile_complaints,
2913                          "Offset %d out of bounds for DW_AT_ranges attribute",
2914                          offset);
2915               return 0;
2916             }
2917           buffer = dwarf2_per_objfile->ranges_buffer + offset;
2918
2919           /* Read in the largest possible address.  */
2920           marker = read_address (obfd, buffer, cu, &dummy);
2921           if ((marker & mask) == mask)
2922             {
2923               /* If we found the largest possible address, then
2924                  read the base address.  */
2925               base = read_address (obfd, buffer + addr_size, cu, &dummy);
2926               buffer += 2 * addr_size;
2927               offset += 2 * addr_size;
2928               found_base = 1;
2929             }
2930
2931           low_set = 0;
2932
2933           while (1)
2934             {
2935               CORE_ADDR range_beginning, range_end;
2936
2937               range_beginning = read_address (obfd, buffer, cu, &dummy);
2938               buffer += addr_size;
2939               range_end = read_address (obfd, buffer, cu, &dummy);
2940               buffer += addr_size;
2941               offset += 2 * addr_size;
2942
2943               /* An end of list marker is a pair of zero addresses.  */
2944               if (range_beginning == 0 && range_end == 0)
2945                 /* Found the end of list entry.  */
2946                 break;
2947
2948               /* Each base address selection entry is a pair of 2 values.
2949                  The first is the largest possible address, the second is
2950                  the base address.  Check for a base address here.  */
2951               if ((range_beginning & mask) == mask)
2952                 {
2953                   /* If we found the largest possible address, then
2954                      read the base address.  */
2955                   base = read_address (obfd, buffer + addr_size, cu, &dummy);
2956                   found_base = 1;
2957                   continue;
2958                 }
2959
2960               if (!found_base)
2961                 {
2962                   /* We have no valid base address for the ranges
2963                      data.  */
2964                   complaint (&symfile_complaints,
2965                              "Invalid .debug_ranges data (no base address)");
2966                   return 0;
2967                 }
2968
2969               range_beginning += base;
2970               range_end += base;
2971
2972               /* FIXME: This is recording everything as a low-high
2973                  segment of consecutive addresses.  We should have a
2974                  data structure for discontiguous block ranges
2975                  instead.  */
2976               if (! low_set)
2977                 {
2978                   low = range_beginning;
2979                   high = range_end;
2980                   low_set = 1;
2981                 }
2982               else
2983                 {
2984                   if (range_beginning < low)
2985                     low = range_beginning;
2986                   if (range_end > high)
2987                     high = range_end;
2988                 }
2989             }
2990
2991           if (! low_set)
2992             /* If the first entry is an end-of-list marker, the range
2993                describes an empty scope, i.e. no instructions.  */
2994             return 0;
2995
2996           ret = -1;
2997         }
2998     }
2999
3000   if (high < low)
3001     return 0;
3002
3003   /* When using the GNU linker, .gnu.linkonce. sections are used to
3004      eliminate duplicate copies of functions and vtables and such.
3005      The linker will arbitrarily choose one and discard the others.
3006      The AT_*_pc values for such functions refer to local labels in
3007      these sections.  If the section from that file was discarded, the
3008      labels are not in the output, so the relocs get a value of 0.
3009      If this is a discarded function, mark the pc bounds as invalid,
3010      so that GDB will ignore it.  */
3011   if (low == 0 && (bfd_get_file_flags (obfd) & HAS_RELOC) == 0)
3012     return 0;
3013
3014   *lowpc = low;
3015   *highpc = high;
3016   return ret;
3017 }
3018
3019 /* Get the low and high pc's represented by the scope DIE, and store
3020    them in *LOWPC and *HIGHPC.  If the correct values can't be
3021    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
3022
3023 static void
3024 get_scope_pc_bounds (struct die_info *die,
3025                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
3026                      struct dwarf2_cu *cu)
3027 {
3028   CORE_ADDR best_low = (CORE_ADDR) -1;
3029   CORE_ADDR best_high = (CORE_ADDR) 0;
3030   CORE_ADDR current_low, current_high;
3031
3032   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu))
3033     {
3034       best_low = current_low;
3035       best_high = current_high;
3036     }
3037   else
3038     {
3039       struct die_info *child = die->child;
3040
3041       while (child && child->tag)
3042         {
3043           switch (child->tag) {
3044           case DW_TAG_subprogram:
3045             if (dwarf2_get_pc_bounds (child, &current_low, &current_high, cu))
3046               {
3047                 best_low = min (best_low, current_low);
3048                 best_high = max (best_high, current_high);
3049               }
3050             break;
3051           case DW_TAG_namespace:
3052             /* FIXME: carlton/2004-01-16: Should we do this for
3053                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
3054                that current GCC's always emit the DIEs corresponding
3055                to definitions of methods of classes as children of a
3056                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
3057                the DIEs giving the declarations, which could be
3058                anywhere).  But I don't see any reason why the
3059                standards says that they have to be there.  */
3060             get_scope_pc_bounds (child, &current_low, &current_high, cu);
3061
3062             if (current_low != ((CORE_ADDR) -1))
3063               {
3064                 best_low = min (best_low, current_low);
3065                 best_high = max (best_high, current_high);
3066               }
3067             break;
3068           default:
3069             /* Ignore. */
3070             break;
3071           }
3072
3073           child = sibling_die (child);
3074         }
3075     }
3076
3077   *lowpc = best_low;
3078   *highpc = best_high;
3079 }
3080
3081 /* Add an aggregate field to the field list.  */
3082
3083 static void
3084 dwarf2_add_field (struct field_info *fip, struct die_info *die,
3085                   struct dwarf2_cu *cu)
3086
3087   struct objfile *objfile = cu->objfile;
3088   struct nextfield *new_field;
3089   struct attribute *attr;
3090   struct field *fp;
3091   char *fieldname = "";
3092
3093   /* Allocate a new field list entry and link it in.  */
3094   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
3095   make_cleanup (xfree, new_field);
3096   memset (new_field, 0, sizeof (struct nextfield));
3097   new_field->next = fip->fields;
3098   fip->fields = new_field;
3099   fip->nfields++;
3100
3101   /* Handle accessibility and virtuality of field.
3102      The default accessibility for members is public, the default
3103      accessibility for inheritance is private.  */
3104   if (die->tag != DW_TAG_inheritance)
3105     new_field->accessibility = DW_ACCESS_public;
3106   else
3107     new_field->accessibility = DW_ACCESS_private;
3108   new_field->virtuality = DW_VIRTUALITY_none;
3109
3110   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
3111   if (attr)
3112     new_field->accessibility = DW_UNSND (attr);
3113   if (new_field->accessibility != DW_ACCESS_public)
3114     fip->non_public_fields = 1;
3115   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
3116   if (attr)
3117     new_field->virtuality = DW_UNSND (attr);
3118
3119   fp = &new_field->field;
3120
3121   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
3122     {
3123       /* Data member other than a C++ static data member.  */
3124       
3125       /* Get type of field.  */
3126       fp->type = die_type (die, cu);
3127
3128       FIELD_STATIC_KIND (*fp) = 0;
3129
3130       /* Get bit size of field (zero if none).  */
3131       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
3132       if (attr)
3133         {
3134           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
3135         }
3136       else
3137         {
3138           FIELD_BITSIZE (*fp) = 0;
3139         }
3140
3141       /* Get bit offset of field.  */
3142       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
3143       if (attr)
3144         {
3145           FIELD_BITPOS (*fp) =
3146             decode_locdesc (DW_BLOCK (attr), cu) * bits_per_byte;
3147         }
3148       else
3149         FIELD_BITPOS (*fp) = 0;
3150       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
3151       if (attr)
3152         {
3153           if (BITS_BIG_ENDIAN)
3154             {
3155               /* For big endian bits, the DW_AT_bit_offset gives the
3156                  additional bit offset from the MSB of the containing
3157                  anonymous object to the MSB of the field.  We don't
3158                  have to do anything special since we don't need to
3159                  know the size of the anonymous object.  */
3160               FIELD_BITPOS (*fp) += DW_UNSND (attr);
3161             }
3162           else
3163             {
3164               /* For little endian bits, compute the bit offset to the
3165                  MSB of the anonymous object, subtract off the number of
3166                  bits from the MSB of the field to the MSB of the
3167                  object, and then subtract off the number of bits of
3168                  the field itself.  The result is the bit offset of
3169                  the LSB of the field.  */
3170               int anonymous_size;
3171               int bit_offset = DW_UNSND (attr);
3172
3173               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3174               if (attr)
3175                 {
3176                   /* The size of the anonymous object containing
3177                      the bit field is explicit, so use the
3178                      indicated size (in bytes).  */
3179                   anonymous_size = DW_UNSND (attr);
3180                 }
3181               else
3182                 {
3183                   /* The size of the anonymous object containing
3184                      the bit field must be inferred from the type
3185                      attribute of the data member containing the
3186                      bit field.  */
3187                   anonymous_size = TYPE_LENGTH (fp->type);
3188                 }
3189               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
3190                 - bit_offset - FIELD_BITSIZE (*fp);
3191             }
3192         }
3193
3194       /* Get name of field.  */
3195       attr = dwarf2_attr (die, DW_AT_name, cu);
3196       if (attr && DW_STRING (attr))
3197         fieldname = DW_STRING (attr);
3198
3199       /* The name is already allocated along with this objfile, so we don't
3200          need to duplicate it for the type.  */
3201       fp->name = fieldname;
3202
3203       /* Change accessibility for artificial fields (e.g. virtual table
3204          pointer or virtual base class pointer) to private.  */
3205       if (dwarf2_attr (die, DW_AT_artificial, cu))
3206         {
3207           new_field->accessibility = DW_ACCESS_private;
3208           fip->non_public_fields = 1;
3209         }
3210     }
3211   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
3212     {
3213       /* C++ static member.  */
3214
3215       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
3216          is a declaration, but all versions of G++ as of this writing
3217          (so through at least 3.2.1) incorrectly generate
3218          DW_TAG_variable tags.  */
3219       
3220       char *physname;
3221
3222       /* Get name of field.  */
3223       attr = dwarf2_attr (die, DW_AT_name, cu);
3224       if (attr && DW_STRING (attr))
3225         fieldname = DW_STRING (attr);
3226       else
3227         return;
3228
3229       /* Get physical name.  */
3230       physname = dwarf2_linkage_name (die, cu);
3231
3232       /* The name is already allocated along with this objfile, so we don't
3233          need to duplicate it for the type.  */
3234       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
3235       FIELD_TYPE (*fp) = die_type (die, cu);
3236       FIELD_NAME (*fp) = fieldname;
3237     }
3238   else if (die->tag == DW_TAG_inheritance)
3239     {
3240       /* C++ base class field.  */
3241       attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
3242       if (attr)
3243         FIELD_BITPOS (*fp) = (decode_locdesc (DW_BLOCK (attr), cu)
3244                               * bits_per_byte);
3245       FIELD_BITSIZE (*fp) = 0;
3246       FIELD_STATIC_KIND (*fp) = 0;
3247       FIELD_TYPE (*fp) = die_type (die, cu);
3248       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
3249       fip->nbaseclasses++;
3250     }
3251 }
3252
3253 /* Create the vector of fields, and attach it to the type.  */
3254
3255 static void
3256 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
3257                               struct dwarf2_cu *cu)
3258 {
3259   int nfields = fip->nfields;
3260
3261   /* Record the field count, allocate space for the array of fields,
3262      and create blank accessibility bitfields if necessary.  */
3263   TYPE_NFIELDS (type) = nfields;
3264   TYPE_FIELDS (type) = (struct field *)
3265     TYPE_ALLOC (type, sizeof (struct field) * nfields);
3266   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
3267
3268   if (fip->non_public_fields)
3269     {
3270       ALLOCATE_CPLUS_STRUCT_TYPE (type);
3271
3272       TYPE_FIELD_PRIVATE_BITS (type) =
3273         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3274       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
3275
3276       TYPE_FIELD_PROTECTED_BITS (type) =
3277         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3278       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
3279
3280       TYPE_FIELD_IGNORE_BITS (type) =
3281         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
3282       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
3283     }
3284
3285   /* If the type has baseclasses, allocate and clear a bit vector for
3286      TYPE_FIELD_VIRTUAL_BITS.  */
3287   if (fip->nbaseclasses)
3288     {
3289       int num_bytes = B_BYTES (fip->nbaseclasses);
3290       char *pointer;
3291
3292       ALLOCATE_CPLUS_STRUCT_TYPE (type);
3293       pointer = (char *) TYPE_ALLOC (type, num_bytes);
3294       TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
3295       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
3296       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
3297     }
3298
3299   /* Copy the saved-up fields into the field vector.  Start from the head
3300      of the list, adding to the tail of the field array, so that they end
3301      up in the same order in the array in which they were added to the list.  */
3302   while (nfields-- > 0)
3303     {
3304       TYPE_FIELD (type, nfields) = fip->fields->field;
3305       switch (fip->fields->accessibility)
3306         {
3307         case DW_ACCESS_private:
3308           SET_TYPE_FIELD_PRIVATE (type, nfields);
3309           break;
3310
3311         case DW_ACCESS_protected:
3312           SET_TYPE_FIELD_PROTECTED (type, nfields);
3313           break;
3314
3315         case DW_ACCESS_public:
3316           break;
3317
3318         default:
3319           /* Unknown accessibility.  Complain and treat it as public.  */
3320           {
3321             complaint (&symfile_complaints, "unsupported accessibility %d",
3322                        fip->fields->accessibility);
3323           }
3324           break;
3325         }
3326       if (nfields < fip->nbaseclasses)
3327         {
3328           switch (fip->fields->virtuality)
3329             {
3330             case DW_VIRTUALITY_virtual:
3331             case DW_VIRTUALITY_pure_virtual:
3332               SET_TYPE_FIELD_VIRTUAL (type, nfields);
3333               break;
3334             }
3335         }
3336       fip->fields = fip->fields->next;
3337     }
3338 }
3339
3340 /* Add a member function to the proper fieldlist.  */
3341
3342 static void
3343 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
3344                       struct type *type, struct dwarf2_cu *cu)
3345 {
3346   struct objfile *objfile = cu->objfile;
3347   struct attribute *attr;
3348   struct fnfieldlist *flp;
3349   int i;
3350   struct fn_field *fnp;
3351   char *fieldname;
3352   char *physname;
3353   struct nextfnfield *new_fnfield;
3354
3355   /* Get name of member function.  */
3356   attr = dwarf2_attr (die, DW_AT_name, cu);
3357   if (attr && DW_STRING (attr))
3358     fieldname = DW_STRING (attr);
3359   else
3360     return;
3361
3362   /* Get the mangled name.  */
3363   physname = dwarf2_linkage_name (die, cu);
3364
3365   /* Look up member function name in fieldlist.  */
3366   for (i = 0; i < fip->nfnfields; i++)
3367     {
3368       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
3369         break;
3370     }
3371
3372   /* Create new list element if necessary.  */
3373   if (i < fip->nfnfields)
3374     flp = &fip->fnfieldlists[i];
3375   else
3376     {
3377       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
3378         {
3379           fip->fnfieldlists = (struct fnfieldlist *)
3380             xrealloc (fip->fnfieldlists,
3381                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
3382                       * sizeof (struct fnfieldlist));
3383           if (fip->nfnfields == 0)
3384             make_cleanup (free_current_contents, &fip->fnfieldlists);
3385         }
3386       flp = &fip->fnfieldlists[fip->nfnfields];
3387       flp->name = fieldname;
3388       flp->length = 0;
3389       flp->head = NULL;
3390       fip->nfnfields++;
3391     }
3392
3393   /* Create a new member function field and chain it to the field list
3394      entry. */
3395   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
3396   make_cleanup (xfree, new_fnfield);
3397   memset (new_fnfield, 0, sizeof (struct nextfnfield));
3398   new_fnfield->next = flp->head;
3399   flp->head = new_fnfield;
3400   flp->length++;
3401
3402   /* Fill in the member function field info.  */
3403   fnp = &new_fnfield->fnfield;
3404   /* The name is already allocated along with this objfile, so we don't
3405      need to duplicate it for the type.  */
3406   fnp->physname = physname ? physname : "";
3407   fnp->type = alloc_type (objfile);
3408   if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
3409     {
3410       int nparams = TYPE_NFIELDS (die->type);
3411
3412       /* TYPE is the domain of this method, and DIE->TYPE is the type
3413            of the method itself (TYPE_CODE_METHOD).  */
3414       smash_to_method_type (fnp->type, type,
3415                             TYPE_TARGET_TYPE (die->type),
3416                             TYPE_FIELDS (die->type),
3417                             TYPE_NFIELDS (die->type),
3418                             TYPE_VARARGS (die->type));
3419
3420       /* Handle static member functions.
3421          Dwarf2 has no clean way to discern C++ static and non-static
3422          member functions. G++ helps GDB by marking the first
3423          parameter for non-static member functions (which is the
3424          this pointer) as artificial. We obtain this information
3425          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
3426       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
3427         fnp->voffset = VOFFSET_STATIC;
3428     }
3429   else
3430     complaint (&symfile_complaints, "member function type missing for '%s'",
3431                physname);
3432
3433   /* Get fcontext from DW_AT_containing_type if present.  */
3434   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
3435     fnp->fcontext = die_containing_type (die, cu);
3436
3437   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
3438      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
3439
3440   /* Get accessibility.  */
3441   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
3442   if (attr)
3443     {
3444       switch (DW_UNSND (attr))
3445         {
3446         case DW_ACCESS_private:
3447           fnp->is_private = 1;
3448           break;
3449         case DW_ACCESS_protected:
3450           fnp->is_protected = 1;
3451           break;
3452         }
3453     }
3454
3455   /* Check for artificial methods.  */
3456   attr = dwarf2_attr (die, DW_AT_artificial, cu);
3457   if (attr && DW_UNSND (attr) != 0)
3458     fnp->is_artificial = 1;
3459
3460   /* Get index in virtual function table if it is a virtual member function.  */
3461   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
3462   if (attr)
3463     {
3464       /* Support the .debug_loc offsets */
3465       if (attr_form_is_block (attr))
3466         {
3467           fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
3468         }
3469       else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
3470         {
3471           dwarf2_complex_location_expr_complaint ();
3472         }
3473       else
3474         {
3475           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
3476                                                  fieldname);
3477         }
3478    }
3479 }
3480
3481 /* Create the vector of member function fields, and attach it to the type.  */
3482
3483 static void
3484 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
3485                                  struct dwarf2_cu *cu)
3486 {
3487   struct fnfieldlist *flp;
3488   int total_length = 0;
3489   int i;
3490
3491   ALLOCATE_CPLUS_STRUCT_TYPE (type);
3492   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
3493     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
3494
3495   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
3496     {
3497       struct nextfnfield *nfp = flp->head;
3498       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
3499       int k;
3500
3501       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
3502       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
3503       fn_flp->fn_fields = (struct fn_field *)
3504         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
3505       for (k = flp->length; (k--, nfp); nfp = nfp->next)
3506         fn_flp->fn_fields[k] = nfp->fnfield;
3507
3508       total_length += flp->length;
3509     }
3510
3511   TYPE_NFN_FIELDS (type) = fip->nfnfields;
3512   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
3513 }
3514
3515
3516 /* Returns non-zero if NAME is the name of a vtable member in CU's
3517    language, zero otherwise.  */
3518 static int
3519 is_vtable_name (const char *name, struct dwarf2_cu *cu)
3520 {
3521   static const char vptr[] = "_vptr";
3522   static const char vtable[] = "vtable";
3523
3524   /* Look for the C++ and Java forms of the vtable.  */
3525   if ((cu->language == language_java
3526        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
3527        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
3528        && is_cplus_marker (name[sizeof (vptr) - 1])))
3529     return 1;
3530
3531   return 0;
3532 }
3533
3534
3535 /* Called when we find the DIE that starts a structure or union scope
3536    (definition) to process all dies that define the members of the
3537    structure or union.
3538
3539    NOTE: we need to call struct_type regardless of whether or not the
3540    DIE has an at_name attribute, since it might be an anonymous
3541    structure or union.  This gets the type entered into our set of
3542    user defined types.
3543
3544    However, if the structure is incomplete (an opaque struct/union)
3545    then suppress creating a symbol table entry for it since gdb only
3546    wants to find the one with the complete definition.  Note that if
3547    it is complete, we just call new_symbol, which does it's own
3548    checking about whether the struct/union is anonymous or not (and
3549    suppresses creating a symbol table entry itself).  */
3550
3551 static void
3552 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
3553 {
3554   struct objfile *objfile = cu->objfile;
3555   struct type *type;
3556   struct attribute *attr;
3557   const char *previous_prefix = processing_current_prefix;
3558   struct cleanup *back_to = NULL;
3559
3560   if (die->type)
3561     return;
3562
3563   type = alloc_type (objfile);
3564
3565   INIT_CPLUS_SPECIFIC (type);
3566   attr = dwarf2_attr (die, DW_AT_name, cu);
3567   if (attr && DW_STRING (attr))
3568     {
3569       if (cu->language == language_cplus
3570           || cu->language == language_java)
3571         {
3572           char *new_prefix = determine_class_name (die, cu);
3573           TYPE_TAG_NAME (type) = obsavestring (new_prefix,
3574                                                strlen (new_prefix),
3575                                                &objfile->objfile_obstack);
3576           back_to = make_cleanup (xfree, new_prefix);
3577           processing_current_prefix = new_prefix;
3578         }
3579       else
3580         {
3581           /* The name is already allocated along with this objfile, so
3582              we don't need to duplicate it for the type.  */
3583           TYPE_TAG_NAME (type) = DW_STRING (attr);
3584         }
3585     }
3586
3587   if (die->tag == DW_TAG_structure_type)
3588     {
3589       TYPE_CODE (type) = TYPE_CODE_STRUCT;
3590     }
3591   else if (die->tag == DW_TAG_union_type)
3592     {
3593       TYPE_CODE (type) = TYPE_CODE_UNION;
3594     }
3595   else
3596     {
3597       /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
3598          in gdbtypes.h.  */
3599       TYPE_CODE (type) = TYPE_CODE_CLASS;
3600     }
3601
3602   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3603   if (attr)
3604     {
3605       TYPE_LENGTH (type) = DW_UNSND (attr);
3606     }
3607   else
3608     {
3609       TYPE_LENGTH (type) = 0;
3610     }
3611
3612   if (die_is_declaration (die, cu))
3613     TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
3614
3615   /* We need to add the type field to the die immediately so we don't
3616      infinitely recurse when dealing with pointers to the structure
3617      type within the structure itself. */
3618   set_die_type (die, type, cu);
3619
3620   if (die->child != NULL && ! die_is_declaration (die, cu))
3621     {
3622       struct field_info fi;
3623       struct die_info *child_die;
3624       struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
3625
3626       memset (&fi, 0, sizeof (struct field_info));
3627
3628       child_die = die->child;
3629
3630       while (child_die && child_die->tag)
3631         {
3632           if (child_die->tag == DW_TAG_member
3633               || child_die->tag == DW_TAG_variable)
3634             {
3635               /* NOTE: carlton/2002-11-05: A C++ static data member
3636                  should be a DW_TAG_member that is a declaration, but
3637                  all versions of G++ as of this writing (so through at
3638                  least 3.2.1) incorrectly generate DW_TAG_variable
3639                  tags for them instead.  */
3640               dwarf2_add_field (&fi, child_die, cu);
3641             }
3642           else if (child_die->tag == DW_TAG_subprogram)
3643             {
3644               /* C++ member function. */
3645               read_type_die (child_die, cu);
3646               dwarf2_add_member_fn (&fi, child_die, type, cu);
3647             }
3648           else if (child_die->tag == DW_TAG_inheritance)
3649             {
3650               /* C++ base class field.  */
3651               dwarf2_add_field (&fi, child_die, cu);
3652             }
3653           child_die = sibling_die (child_die);
3654         }
3655
3656       /* Attach fields and member functions to the type.  */
3657       if (fi.nfields)
3658         dwarf2_attach_fields_to_type (&fi, type, cu);
3659       if (fi.nfnfields)
3660         {
3661           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
3662
3663           /* Get the type which refers to the base class (possibly this
3664              class itself) which contains the vtable pointer for the current
3665              class from the DW_AT_containing_type attribute.  */
3666
3667           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
3668             {
3669               struct type *t = die_containing_type (die, cu);
3670
3671               TYPE_VPTR_BASETYPE (type) = t;
3672               if (type == t)
3673                 {
3674                   int i;
3675
3676                   /* Our own class provides vtbl ptr.  */
3677                   for (i = TYPE_NFIELDS (t) - 1;
3678                        i >= TYPE_N_BASECLASSES (t);
3679                        --i)
3680                     {
3681                       char *fieldname = TYPE_FIELD_NAME (t, i);
3682
3683                       if (is_vtable_name (fieldname, cu))
3684                         {
3685                           TYPE_VPTR_FIELDNO (type) = i;
3686                           break;
3687                         }
3688                     }
3689
3690                   /* Complain if virtual function table field not found.  */
3691                   if (i < TYPE_N_BASECLASSES (t))
3692                     complaint (&symfile_complaints,
3693                                "virtual function table pointer not found when defining class '%s'",
3694                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
3695                                "");
3696                 }
3697               else
3698                 {
3699                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
3700                 }
3701             }
3702         }
3703
3704       do_cleanups (back_to);
3705     }
3706
3707   processing_current_prefix = previous_prefix;
3708   if (back_to != NULL)
3709     do_cleanups (back_to);
3710 }
3711
3712 static void
3713 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
3714 {
3715   struct objfile *objfile = cu->objfile;
3716   const char *previous_prefix = processing_current_prefix;
3717   struct die_info *child_die = die->child;
3718
3719   if (TYPE_TAG_NAME (die->type) != NULL)
3720     processing_current_prefix = TYPE_TAG_NAME (die->type);
3721
3722   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
3723      snapshots) has been known to create a die giving a declaration
3724      for a class that has, as a child, a die giving a definition for a
3725      nested class.  So we have to process our children even if the
3726      current die is a declaration.  Normally, of course, a declaration
3727      won't have any children at all.  */
3728
3729   while (child_die != NULL && child_die->tag)
3730     {
3731       if (child_die->tag == DW_TAG_member
3732           || child_die->tag == DW_TAG_variable
3733           || child_die->tag == DW_TAG_inheritance)
3734         {
3735           /* Do nothing.  */
3736         }
3737       else
3738         process_die (child_die, cu);
3739
3740       child_die = sibling_die (child_die);
3741     }
3742
3743   if (die->child != NULL && ! die_is_declaration (die, cu))
3744     new_symbol (die, die->type, cu);
3745
3746   processing_current_prefix = previous_prefix;
3747 }
3748
3749 /* Given a DW_AT_enumeration_type die, set its type.  We do not
3750    complete the type's fields yet, or create any symbols.  */
3751
3752 static void
3753 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
3754 {
3755   struct objfile *objfile = cu->objfile;
3756   struct type *type;
3757   struct attribute *attr;
3758
3759   if (die->type)
3760     return;
3761
3762   type = alloc_type (objfile);
3763
3764   TYPE_CODE (type) = TYPE_CODE_ENUM;
3765   attr = dwarf2_attr (die, DW_AT_name, cu);
3766   if (attr && DW_STRING (attr))
3767     {
3768       char *name = DW_STRING (attr);
3769
3770       if (processing_has_namespace_info)
3771         {
3772           TYPE_TAG_NAME (type) = typename_concat (&objfile->objfile_obstack,
3773                                                   processing_current_prefix,
3774                                                   name, cu);
3775         }
3776       else
3777         {
3778           /* The name is already allocated along with this objfile, so
3779              we don't need to duplicate it for the type.  */
3780           TYPE_TAG_NAME (type) = name;
3781         }
3782     }
3783
3784   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
3785   if (attr)
3786     {
3787       TYPE_LENGTH (type) = DW_UNSND (attr);
3788     }
3789   else
3790     {
3791       TYPE_LENGTH (type) = 0;
3792     }
3793
3794   set_die_type (die, type, cu);
3795 }
3796
3797 /* Determine the name of the type represented by DIE, which should be
3798    a named C++ or Java compound type.  Return the name in question; the caller
3799    is responsible for xfree()'ing it.  */
3800
3801 static char *
3802 determine_class_name (struct die_info *die, struct dwarf2_cu *cu)
3803 {
3804   struct cleanup *back_to = NULL;
3805   struct die_info *spec_die = die_specification (die, cu);
3806   char *new_prefix = NULL;
3807
3808   /* If this is the definition of a class that is declared by another
3809      die, then processing_current_prefix may not be accurate; see
3810      read_func_scope for a similar example.  */
3811   if (spec_die != NULL)
3812     {
3813       char *specification_prefix = determine_prefix (spec_die, cu);
3814       processing_current_prefix = specification_prefix;
3815       back_to = make_cleanup (xfree, specification_prefix);
3816     }
3817
3818   /* If we don't have namespace debug info, guess the name by trying
3819      to demangle the names of members, just like we did in
3820      guess_structure_name.  */
3821   if (!processing_has_namespace_info)
3822     {
3823       struct die_info *child;
3824
3825       for (child = die->child;
3826            child != NULL && child->tag != 0;
3827            child = sibling_die (child))
3828         {
3829           if (child->tag == DW_TAG_subprogram)
3830             {
3831               new_prefix 
3832                 = language_class_name_from_physname (cu->language_defn,
3833                                                      dwarf2_linkage_name
3834                                                      (child, cu));
3835
3836               if (new_prefix != NULL)
3837                 break;
3838             }
3839         }
3840     }
3841
3842   if (new_prefix == NULL)
3843     {
3844       const char *name = dwarf2_name (die, cu);
3845       new_prefix = typename_concat (NULL, processing_current_prefix,
3846                                     name ? name : "<<anonymous>>", 
3847                                     cu);
3848     }
3849
3850   if (back_to != NULL)
3851     do_cleanups (back_to);
3852
3853   return new_prefix;
3854 }
3855
3856 /* Given a pointer to a die which begins an enumeration, process all
3857    the dies that define the members of the enumeration, and create the
3858    symbol for the enumeration type.
3859
3860    NOTE: We reverse the order of the element list.  */
3861
3862 static void
3863 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
3864 {
3865   struct objfile *objfile = cu->objfile;
3866   struct die_info *child_die;
3867   struct field *fields;
3868   struct attribute *attr;
3869   struct symbol *sym;
3870   int num_fields;
3871   int unsigned_enum = 1;
3872
3873   num_fields = 0;
3874   fields = NULL;
3875   if (die->child != NULL)
3876     {
3877       child_die = die->child;
3878       while (child_die && child_die->tag)
3879         {
3880           if (child_die->tag != DW_TAG_enumerator)
3881             {
3882               process_die (child_die, cu);
3883             }
3884           else
3885             {
3886               attr = dwarf2_attr (child_die, DW_AT_name, cu);
3887               if (attr)
3888                 {
3889                   sym = new_symbol (child_die, die->type, cu);
3890                   if (SYMBOL_VALUE (sym) < 0)
3891                     unsigned_enum = 0;
3892
3893                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
3894                     {
3895                       fields = (struct field *)
3896                         xrealloc (fields,
3897                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
3898                                   * sizeof (struct field));
3899                     }
3900
3901                   FIELD_NAME (fields[num_fields]) = DEPRECATED_SYMBOL_NAME (sym);
3902                   FIELD_TYPE (fields[num_fields]) = NULL;
3903                   FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
3904                   FIELD_BITSIZE (fields[num_fields]) = 0;
3905                   FIELD_STATIC_KIND (fields[num_fields]) = 0;
3906
3907                   num_fields++;
3908                 }
3909             }
3910
3911           child_die = sibling_die (child_die);
3912         }
3913
3914       if (num_fields)
3915         {
3916           TYPE_NFIELDS (die->type) = num_fields;
3917           TYPE_FIELDS (die->type) = (struct field *)
3918             TYPE_ALLOC (die->type, sizeof (struct field) * num_fields);
3919           memcpy (TYPE_FIELDS (die->type), fields,
3920                   sizeof (struct field) * num_fields);
3921           xfree (fields);
3922         }
3923       if (unsigned_enum)
3924         TYPE_FLAGS (die->type) |= TYPE_FLAG_UNSIGNED;
3925     }
3926
3927   new_symbol (die, die->type, cu);
3928 }
3929
3930 /* Extract all information from a DW_TAG_array_type DIE and put it in
3931    the DIE's type field.  For now, this only handles one dimensional
3932    arrays.  */
3933
3934 static void
3935 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
3936 {
3937   struct objfile *objfile = cu->objfile;
3938   struct die_info *child_die;
3939   struct type *type = NULL;
3940   struct type *element_type, *range_type, *index_type;
3941   struct type **range_types = NULL;
3942   struct attribute *attr;
3943   int ndim = 0;
3944   struct cleanup *back_to;
3945
3946   /* Return if we've already decoded this type. */
3947   if (die->type)
3948     {
3949       return;
3950     }
3951
3952   element_type = die_type (die, cu);
3953
3954   /* Irix 6.2 native cc creates array types without children for
3955      arrays with unspecified length.  */
3956   if (die->child == NULL)
3957     {
3958       index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
3959       range_type = create_range_type (NULL, index_type, 0, -1);
3960       set_die_type (die, create_array_type (NULL, element_type, range_type),
3961                     cu);
3962       return;
3963     }
3964
3965   back_to = make_cleanup (null_cleanup, NULL);
3966   child_die = die->child;
3967   while (child_die && child_die->tag)
3968     {
3969       if (child_die->tag == DW_TAG_subrange_type)
3970         {
3971           read_subrange_type (child_die, cu);
3972
3973           if (child_die->type != NULL)
3974             {
3975               /* The range type was succesfully read. Save it for
3976                  the array type creation.  */
3977               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
3978                 {
3979                   range_types = (struct type **)
3980                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
3981                               * sizeof (struct type *));
3982                   if (ndim == 0)
3983                     make_cleanup (free_current_contents, &range_types);
3984                 }
3985               range_types[ndim++] = child_die->type;
3986             }
3987         }
3988       child_die = sibling_die (child_die);
3989     }
3990
3991   /* Dwarf2 dimensions are output from left to right, create the
3992      necessary array types in backwards order.  */
3993
3994   type = element_type;
3995
3996   if (read_array_order (die, cu) == DW_ORD_col_major)
3997     {
3998       int i = 0;
3999       while (i < ndim)
4000         type = create_array_type (NULL, type, range_types[i++]);
4001     }
4002   else
4003     {
4004       while (ndim-- > 0)
4005         type = create_array_type (NULL, type, range_types[ndim]);
4006     }
4007
4008   /* Understand Dwarf2 support for vector types (like they occur on
4009      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
4010      array type.  This is not part of the Dwarf2/3 standard yet, but a
4011      custom vendor extension.  The main difference between a regular
4012      array and the vector variant is that vectors are passed by value
4013      to functions.  */
4014   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
4015   if (attr)
4016     TYPE_FLAGS (type) |= TYPE_FLAG_VECTOR;
4017
4018   do_cleanups (back_to);
4019
4020   /* Install the type in the die. */
4021   set_die_type (die, type, cu);
4022 }
4023
4024 static enum dwarf_array_dim_ordering
4025 read_array_order (struct die_info *die, struct dwarf2_cu *cu) 
4026 {
4027   struct attribute *attr;
4028
4029   attr = dwarf2_attr (die, DW_AT_ordering, cu);
4030
4031   if (attr) return DW_SND (attr);
4032
4033   /*
4034     GNU F77 is a special case, as at 08/2004 array type info is the
4035     opposite order to the dwarf2 specification, but data is still 
4036     laid out as per normal fortran.
4037
4038     FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need 
4039     version checking.
4040   */
4041
4042   if (cu->language == language_fortran &&
4043       cu->producer && strstr (cu->producer, "GNU F77"))
4044     {
4045       return DW_ORD_row_major;
4046     }
4047
4048   switch (cu->language_defn->la_array_ordering) 
4049     {
4050     case array_column_major:
4051       return DW_ORD_col_major;
4052     case array_row_major:
4053     default:
4054       return DW_ORD_row_major;
4055     };
4056 }
4057
4058
4059 /* First cut: install each common block member as a global variable.  */
4060
4061 static void
4062 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
4063 {
4064   struct die_info *child_die;
4065   struct attribute *attr;
4066   struct symbol *sym;
4067   CORE_ADDR base = (CORE_ADDR) 0;
4068
4069   attr = dwarf2_attr (die, DW_AT_location, cu);
4070   if (attr)
4071     {
4072       /* Support the .debug_loc offsets */
4073       if (attr_form_is_block (attr))
4074         {
4075           base = decode_locdesc (DW_BLOCK (attr), cu);
4076         }
4077       else if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
4078         {
4079           dwarf2_complex_location_expr_complaint ();
4080         }
4081       else
4082         {
4083           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
4084                                                  "common block member");
4085         }
4086     }
4087   if (die->child != NULL)
4088     {
4089       child_die = die->child;
4090       while (child_die && child_die->tag)
4091         {
4092           sym = new_symbol (child_die, NULL, cu);
4093           attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
4094           if (attr)
4095             {
4096               SYMBOL_VALUE_ADDRESS (sym) =
4097                 base + decode_locdesc (DW_BLOCK (attr), cu);
4098               add_symbol_to_list (sym, &global_symbols);
4099             }
4100           child_die = sibling_die (child_die);
4101         }
4102     }
4103 }
4104
4105 /* Read a C++ namespace.  */
4106
4107 static void
4108 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
4109 {
4110   struct objfile *objfile = cu->objfile;
4111   const char *previous_prefix = processing_current_prefix;
4112   const char *name;
4113   int is_anonymous;
4114   struct die_info *current_die;
4115   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
4116
4117   name = namespace_name (die, &is_anonymous, cu);
4118
4119   /* Now build the name of the current namespace.  */
4120
4121   if (previous_prefix[0] == '\0')
4122     {
4123       processing_current_prefix = name;
4124     }
4125   else
4126     {
4127       char *temp_name = typename_concat (NULL, previous_prefix, name, cu);
4128       make_cleanup (xfree, temp_name);
4129       processing_current_prefix = temp_name;
4130     }
4131
4132   /* Add a symbol associated to this if we haven't seen the namespace
4133      before.  Also, add a using directive if it's an anonymous
4134      namespace.  */
4135
4136   if (dwarf2_extension (die, cu) == NULL)
4137     {
4138       struct type *type;
4139
4140       /* FIXME: carlton/2003-06-27: Once GDB is more const-correct,
4141          this cast will hopefully become unnecessary.  */
4142       type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
4143                         (char *) processing_current_prefix,
4144                         objfile);
4145       TYPE_TAG_NAME (type) = TYPE_NAME (type);
4146
4147       new_symbol (die, type, cu);
4148       set_die_type (die, type, cu);
4149
4150       if (is_anonymous)
4151         cp_add_using_directive (processing_current_prefix,
4152                                 strlen (previous_prefix),
4153                                 strlen (processing_current_prefix));
4154     }
4155
4156   if (die->child != NULL)
4157     {
4158       struct die_info *child_die = die->child;
4159       
4160       while (child_die && child_die->tag)
4161         {
4162           process_die (child_die, cu);
4163           child_die = sibling_die (child_die);
4164         }
4165     }
4166
4167   processing_current_prefix = previous_prefix;
4168   do_cleanups (back_to);
4169 }
4170
4171 /* Return the name of the namespace represented by DIE.  Set
4172    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
4173    namespace.  */
4174
4175 static const char *
4176 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
4177 {
4178   struct die_info *current_die;
4179   const char *name = NULL;
4180
4181   /* Loop through the extensions until we find a name.  */
4182
4183   for (current_die = die;
4184        current_die != NULL;
4185        current_die = dwarf2_extension (die, cu))
4186     {
4187       name = dwarf2_name (current_die, cu);
4188       if (name != NULL)
4189         break;
4190     }
4191
4192   /* Is it an anonymous namespace?  */
4193
4194   *is_anonymous = (name == NULL);
4195   if (*is_anonymous)
4196     name = "(anonymous namespace)";
4197
4198   return name;
4199 }
4200
4201 /* Extract all information from a DW_TAG_pointer_type DIE and add to
4202    the user defined type vector.  */
4203
4204 static void
4205 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
4206 {
4207   struct comp_unit_head *cu_header = &cu->header;
4208   struct type *type;
4209   struct attribute *attr_byte_size;
4210   struct attribute *attr_address_class;
4211   int byte_size, addr_class;
4212
4213   if (die->type)
4214     {
4215       return;
4216     }
4217
4218   type = lookup_pointer_type (die_type (die, cu));
4219
4220   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
4221   if (attr_byte_size)
4222     byte_size = DW_UNSND (attr_byte_size);
4223   else
4224     byte_size = cu_header->addr_size;
4225
4226   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
4227   if (attr_address_class)
4228     addr_class = DW_UNSND (attr_address_class);
4229   else
4230     addr_class = DW_ADDR_none;
4231
4232   /* If the pointer size or address class is different than the
4233      default, create a type variant marked as such and set the
4234      length accordingly.  */
4235   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
4236     {
4237       if (ADDRESS_CLASS_TYPE_FLAGS_P ())
4238         {
4239           int type_flags;
4240
4241           type_flags = ADDRESS_CLASS_TYPE_FLAGS (byte_size, addr_class);
4242           gdb_assert ((type_flags & ~TYPE_FLAG_ADDRESS_CLASS_ALL) == 0);
4243           type = make_type_with_address_space (type, type_flags);
4244         }
4245       else if (TYPE_LENGTH (type) != byte_size)
4246         {
4247           complaint (&symfile_complaints, "invalid pointer size %d", byte_size);
4248         }
4249       else {
4250         /* Should we also complain about unhandled address classes?  */
4251       }
4252     }
4253
4254   TYPE_LENGTH (type) = byte_size;
4255   set_die_type (die, type, cu);
4256 }
4257
4258 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
4259    the user defined type vector.  */
4260
4261 static void
4262 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
4263 {
4264   struct objfile *objfile = cu->objfile;
4265   struct type *type;
4266   struct type *to_type;
4267   struct type *domain;
4268
4269   if (die->type)
4270     {
4271       return;
4272     }
4273
4274   type = alloc_type (objfile);
4275   to_type = die_type (die, cu);
4276   domain = die_containing_type (die, cu);
4277   smash_to_member_type (type, domain, to_type);
4278
4279   set_die_type (die, type, cu);
4280 }
4281
4282 /* Extract all information from a DW_TAG_reference_type DIE and add to
4283    the user defined type vector.  */
4284
4285 static void
4286 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
4287 {
4288   struct comp_unit_head *cu_header = &cu->header;
4289   struct type *type;
4290   struct attribute *attr;
4291
4292   if (die->type)
4293     {
4294       return;
4295     }
4296
4297   type = lookup_reference_type (die_type (die, cu));
4298   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4299   if (attr)
4300     {
4301       TYPE_LENGTH (type) = DW_UNSND (attr);
4302     }
4303   else
4304     {
4305       TYPE_LENGTH (type) = cu_header->addr_size;
4306     }
4307   set_die_type (die, type, cu);
4308 }
4309
4310 static void
4311 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
4312 {
4313   struct type *base_type;
4314
4315   if (die->type)
4316     {
4317       return;
4318     }
4319
4320   base_type = die_type (die, cu);
4321   set_die_type (die, make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0),
4322                 cu);
4323 }
4324
4325 static void
4326 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
4327 {
4328   struct type *base_type;
4329
4330   if (die->type)
4331     {
4332       return;
4333     }
4334
4335   base_type = die_type (die, cu);
4336   set_die_type (die, make_cv_type (TYPE_CONST (base_type), 1, base_type, 0),
4337                 cu);
4338 }
4339
4340 /* Extract all information from a DW_TAG_string_type DIE and add to
4341    the user defined type vector.  It isn't really a user defined type,
4342    but it behaves like one, with other DIE's using an AT_user_def_type
4343    attribute to reference it.  */
4344
4345 static void
4346 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
4347 {
4348   struct objfile *objfile = cu->objfile;
4349   struct type *type, *range_type, *index_type, *char_type;
4350   struct attribute *attr;
4351   unsigned int length;
4352
4353   if (die->type)
4354     {
4355       return;
4356     }
4357
4358   attr = dwarf2_attr (die, DW_AT_string_length, cu);
4359   if (attr)
4360     {
4361       length = DW_UNSND (attr);
4362     }
4363   else
4364     {
4365       /* check for the DW_AT_byte_size attribute */
4366       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4367       if (attr)
4368         {
4369           length = DW_UNSND (attr);
4370         }
4371       else
4372         {
4373           length = 1;
4374         }
4375     }
4376   index_type = dwarf2_fundamental_type (objfile, FT_INTEGER, cu);
4377   range_type = create_range_type (NULL, index_type, 1, length);
4378   if (cu->language == language_fortran)
4379     {
4380       /* Need to create a unique string type for bounds
4381          information */
4382       type = create_string_type (0, range_type);
4383     }
4384   else
4385     {
4386       char_type = dwarf2_fundamental_type (objfile, FT_CHAR, cu);
4387       type = create_string_type (char_type, range_type);
4388     }
4389   set_die_type (die, type, cu);
4390 }
4391
4392 /* Handle DIES due to C code like:
4393
4394    struct foo
4395    {
4396    int (*funcp)(int a, long l);
4397    int b;
4398    };
4399
4400    ('funcp' generates a DW_TAG_subroutine_type DIE)
4401  */
4402
4403 static void
4404 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
4405 {
4406   struct type *type;            /* Type that this function returns */
4407   struct type *ftype;           /* Function that returns above type */
4408   struct attribute *attr;
4409
4410   /* Decode the type that this subroutine returns */
4411   if (die->type)
4412     {
4413       return;
4414     }
4415   type = die_type (die, cu);
4416   ftype = lookup_function_type (type);
4417
4418   /* All functions in C++ and Java have prototypes.  */
4419   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
4420   if ((attr && (DW_UNSND (attr) != 0))
4421       || cu->language == language_cplus
4422       || cu->language == language_java)
4423     TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
4424
4425   if (die->child != NULL)
4426     {
4427       struct die_info *child_die;
4428       int nparams = 0;
4429       int iparams = 0;
4430
4431       /* Count the number of parameters.
4432          FIXME: GDB currently ignores vararg functions, but knows about
4433          vararg member functions.  */
4434       child_die = die->child;
4435       while (child_die && child_die->tag)
4436         {
4437           if (child_die->tag == DW_TAG_formal_parameter)
4438             nparams++;
4439           else if (child_die->tag == DW_TAG_unspecified_parameters)
4440             TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
4441           child_die = sibling_die (child_die);
4442         }
4443
4444       /* Allocate storage for parameters and fill them in.  */
4445       TYPE_NFIELDS (ftype) = nparams;
4446       TYPE_FIELDS (ftype) = (struct field *)
4447         TYPE_ALLOC (ftype, nparams * sizeof (struct field));
4448
4449       child_die = die->child;
4450       while (child_die && child_die->tag)
4451         {
4452           if (child_die->tag == DW_TAG_formal_parameter)
4453             {
4454               /* Dwarf2 has no clean way to discern C++ static and non-static
4455                  member functions. G++ helps GDB by marking the first
4456                  parameter for non-static member functions (which is the
4457                  this pointer) as artificial. We pass this information
4458                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
4459               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
4460               if (attr)
4461                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
4462               else
4463                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
4464               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, cu);
4465               iparams++;
4466             }
4467           child_die = sibling_die (child_die);
4468         }
4469     }
4470
4471   set_die_type (die, ftype, cu);
4472 }
4473
4474 static void
4475 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
4476 {
4477   struct objfile *objfile = cu->objfile;
4478   struct attribute *attr;
4479   char *name = NULL;
4480
4481   if (!die->type)
4482     {
4483       attr = dwarf2_attr (die, DW_AT_name, cu);
4484       if (attr && DW_STRING (attr))
4485         {
4486           name = DW_STRING (attr);
4487         }
4488       set_die_type (die, init_type (TYPE_CODE_TYPEDEF, 0,
4489                                     TYPE_FLAG_TARGET_STUB, name, objfile),
4490                     cu);
4491       TYPE_TARGET_TYPE (die->type) = die_type (die, cu);
4492     }
4493 }
4494
4495 /* Find a representation of a given base type and install
4496    it in the TYPE field of the die.  */
4497
4498 static void
4499 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
4500 {
4501   struct objfile *objfile = cu->objfile;
4502   struct type *type;
4503   struct attribute *attr;
4504   int encoding = 0, size = 0;
4505
4506   /* If we've already decoded this die, this is a no-op. */
4507   if (die->type)
4508     {
4509       return;
4510     }
4511
4512   attr = dwarf2_attr (die, DW_AT_encoding, cu);
4513   if (attr)
4514     {
4515       encoding = DW_UNSND (attr);
4516     }
4517   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4518   if (attr)
4519     {
4520       size = DW_UNSND (attr);
4521     }
4522   attr = dwarf2_attr (die, DW_AT_name, cu);
4523   if (attr && DW_STRING (attr))
4524     {
4525       enum type_code code = TYPE_CODE_INT;
4526       int type_flags = 0;
4527
4528       switch (encoding)
4529         {
4530         case DW_ATE_address:
4531           /* Turn DW_ATE_address into a void * pointer.  */
4532           code = TYPE_CODE_PTR;
4533           type_flags |= TYPE_FLAG_UNSIGNED;
4534           break;
4535         case DW_ATE_boolean:
4536           code = TYPE_CODE_BOOL;
4537           type_flags |= TYPE_FLAG_UNSIGNED;
4538           break;
4539         case DW_ATE_complex_float:
4540           code = TYPE_CODE_COMPLEX;
4541           break;
4542         case DW_ATE_float:
4543           code = TYPE_CODE_FLT;
4544           break;
4545         case DW_ATE_signed:
4546         case DW_ATE_signed_char:
4547           break;
4548         case DW_ATE_unsigned:
4549         case DW_ATE_unsigned_char:
4550           type_flags |= TYPE_FLAG_UNSIGNED;
4551           break;
4552         default:
4553           complaint (&symfile_complaints, "unsupported DW_AT_encoding: '%s'",
4554                      dwarf_type_encoding_name (encoding));
4555           break;
4556         }
4557       type = init_type (code, size, type_flags, DW_STRING (attr), objfile);
4558       if (encoding == DW_ATE_address)
4559         TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID,
4560                                                            cu);
4561       else if (encoding == DW_ATE_complex_float)
4562         {
4563           if (size == 32)
4564             TYPE_TARGET_TYPE (type)
4565               = dwarf2_fundamental_type (objfile, FT_EXT_PREC_FLOAT, cu);
4566           else if (size == 16)
4567             TYPE_TARGET_TYPE (type)
4568               = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
4569           else if (size == 8)
4570             TYPE_TARGET_TYPE (type)
4571               = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
4572         }
4573     }
4574   else
4575     {
4576       type = dwarf_base_type (encoding, size, cu);
4577     }
4578   set_die_type (die, type, cu);
4579 }
4580
4581 /* Read the given DW_AT_subrange DIE.  */
4582
4583 static void
4584 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
4585 {
4586   struct type *base_type;
4587   struct type *range_type;
4588   struct attribute *attr;
4589   int low = 0;
4590   int high = -1;
4591   
4592   /* If we have already decoded this die, then nothing more to do.  */
4593   if (die->type)
4594     return;
4595
4596   base_type = die_type (die, cu);
4597   if (base_type == NULL)
4598     {
4599       complaint (&symfile_complaints,
4600                 "DW_AT_type missing from DW_TAG_subrange_type");
4601       return;
4602     }
4603
4604   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
4605     base_type = alloc_type (NULL);
4606
4607   if (cu->language == language_fortran)
4608     { 
4609       /* FORTRAN implies a lower bound of 1, if not given.  */
4610       low = 1;
4611     }
4612
4613   /* FIXME: For variable sized arrays either of these could be
4614      a variable rather than a constant value.  We'll allow it,
4615      but we don't know how to handle it.  */
4616   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
4617   if (attr)
4618     low = dwarf2_get_attr_constant_value (attr, 0);
4619
4620   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
4621   if (attr)
4622     {       
4623       if (attr->form == DW_FORM_block1)
4624         {
4625           /* GCC encodes arrays with unspecified or dynamic length
4626              with a DW_FORM_block1 attribute.
4627              FIXME: GDB does not yet know how to handle dynamic
4628              arrays properly, treat them as arrays with unspecified
4629              length for now.
4630
4631              FIXME: jimb/2003-09-22: GDB does not really know
4632              how to handle arrays of unspecified length
4633              either; we just represent them as zero-length
4634              arrays.  Choose an appropriate upper bound given
4635              the lower bound we've computed above.  */
4636           high = low - 1;
4637         }
4638       else
4639         high = dwarf2_get_attr_constant_value (attr, 1);
4640     }
4641
4642   range_type = create_range_type (NULL, base_type, low, high);
4643
4644   attr = dwarf2_attr (die, DW_AT_name, cu);
4645   if (attr && DW_STRING (attr))
4646     TYPE_NAME (range_type) = DW_STRING (attr);
4647   
4648   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4649   if (attr)
4650     TYPE_LENGTH (range_type) = DW_UNSND (attr);
4651
4652   set_die_type (die, range_type, cu);
4653 }
4654   
4655
4656 /* Read a whole compilation unit into a linked list of dies.  */
4657
4658 static struct die_info *
4659 read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
4660 {
4661   /* Reset die reference table; we are
4662      building new ones now.  */
4663   dwarf2_empty_hash_tables ();
4664
4665   return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
4666 }
4667
4668 /* Read a single die and all its descendents.  Set the die's sibling
4669    field to NULL; set other fields in the die correctly, and set all
4670    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
4671    location of the info_ptr after reading all of those dies.  PARENT
4672    is the parent of the die in question.  */
4673
4674 static struct die_info *
4675 read_die_and_children (char *info_ptr, bfd *abfd,
4676                        struct dwarf2_cu *cu,
4677                        char **new_info_ptr,
4678                        struct die_info *parent)
4679 {
4680   struct die_info *die;
4681   char *cur_ptr;
4682   int has_children;
4683
4684   cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
4685   store_in_ref_table (die->offset, die);
4686
4687   if (has_children)
4688     {
4689       die->child = read_die_and_siblings (cur_ptr, abfd, cu,
4690                                           new_info_ptr, die);
4691     }
4692   else
4693     {
4694       die->child = NULL;
4695       *new_info_ptr = cur_ptr;
4696     }
4697
4698   die->sibling = NULL;
4699   die->parent = parent;
4700   return die;
4701 }
4702
4703 /* Read a die, all of its descendents, and all of its siblings; set
4704    all of the fields of all of the dies correctly.  Arguments are as
4705    in read_die_and_children.  */
4706
4707 static struct die_info *
4708 read_die_and_siblings (char *info_ptr, bfd *abfd,
4709                        struct dwarf2_cu *cu,
4710                        char **new_info_ptr,
4711                        struct die_info *parent)
4712 {
4713   struct die_info *first_die, *last_sibling;
4714   char *cur_ptr;
4715
4716   cur_ptr = info_ptr;
4717   first_die = last_sibling = NULL;
4718
4719   while (1)
4720     {
4721       struct die_info *die
4722         = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
4723
4724       if (!first_die)
4725         {
4726           first_die = die;
4727         }
4728       else
4729         {
4730           last_sibling->sibling = die;
4731         }
4732
4733       if (die->tag == 0)
4734         {
4735           *new_info_ptr = cur_ptr;
4736           return first_die;
4737         }
4738       else
4739         {
4740           last_sibling = die;
4741         }
4742     }
4743 }
4744
4745 /* Free a linked list of dies.  */
4746
4747 static void
4748 free_die_list (struct die_info *dies)
4749 {
4750   struct die_info *die, *next;
4751
4752   die = dies;
4753   while (die)
4754     {
4755       if (die->child != NULL)
4756         free_die_list (die->child);
4757       next = die->sibling;
4758       xfree (die->attrs);
4759       xfree (die);
4760       die = next;
4761     }
4762 }
4763
4764 static void
4765 do_free_die_list_cleanup (void *dies)
4766 {
4767   free_die_list (dies);
4768 }
4769
4770 static struct cleanup *
4771 make_cleanup_free_die_list (struct die_info *dies)
4772 {
4773   return make_cleanup (do_free_die_list_cleanup, dies);
4774 }
4775
4776
4777 /* Read the contents of the section at OFFSET and of size SIZE from the
4778    object file specified by OBJFILE into the objfile_obstack and return it.  */
4779
4780 char *
4781 dwarf2_read_section (struct objfile *objfile, asection *sectp)
4782 {
4783   bfd *abfd = objfile->obfd;
4784   char *buf, *retbuf;
4785   bfd_size_type size = bfd_get_section_size (sectp);
4786
4787   if (size == 0)
4788     return NULL;
4789
4790   buf = (char *) obstack_alloc (&objfile->objfile_obstack, size);
4791   retbuf
4792     = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
4793   if (retbuf != NULL)
4794     return retbuf;
4795
4796   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
4797       || bfd_bread (buf, size, abfd) != size)
4798     error ("Dwarf Error: Can't read DWARF data from '%s'",
4799            bfd_get_filename (abfd));
4800
4801   return buf;
4802 }
4803
4804 /* In DWARF version 2, the description of the debugging information is
4805    stored in a separate .debug_abbrev section.  Before we read any
4806    dies from a section we read in all abbreviations and install them
4807    in a hash table.  This function also sets flags in CU describing
4808    the data found in the abbrev table.  */
4809
4810 static void
4811 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
4812 {
4813   struct comp_unit_head *cu_header = &cu->header;
4814   char *abbrev_ptr;
4815   struct abbrev_info *cur_abbrev;
4816   unsigned int abbrev_number, bytes_read, abbrev_name;
4817   unsigned int abbrev_form, hash_number;
4818   struct attr_abbrev *cur_attrs;
4819   unsigned int allocated_attrs;
4820
4821   /* Initialize dwarf2 abbrevs */
4822   obstack_init (&cu->abbrev_obstack);
4823   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
4824                                       (ABBREV_HASH_SIZE
4825                                        * sizeof (struct abbrev_info *)));
4826   memset (cu->dwarf2_abbrevs, 0,
4827           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
4828
4829   abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
4830   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4831   abbrev_ptr += bytes_read;
4832
4833   allocated_attrs = ATTR_ALLOC_CHUNK;
4834   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
4835   
4836   /* loop until we reach an abbrev number of 0 */
4837   while (abbrev_number)
4838     {
4839       cur_abbrev = dwarf_alloc_abbrev (cu);
4840
4841       /* read in abbrev header */
4842       cur_abbrev->number = abbrev_number;
4843       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4844       abbrev_ptr += bytes_read;
4845       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
4846       abbrev_ptr += 1;
4847
4848       if (cur_abbrev->tag == DW_TAG_namespace)
4849         cu->has_namespace_info = 1;
4850
4851       /* now read in declarations */
4852       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4853       abbrev_ptr += bytes_read;
4854       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4855       abbrev_ptr += bytes_read;
4856       while (abbrev_name)
4857         {
4858           if (cur_abbrev->num_attrs == allocated_attrs)
4859             {
4860               allocated_attrs += ATTR_ALLOC_CHUNK;
4861               cur_attrs
4862                 = xrealloc (cur_attrs, (allocated_attrs
4863                                         * sizeof (struct attr_abbrev)));
4864             }
4865
4866           /* Record whether this compilation unit might have
4867              inter-compilation-unit references.  If we don't know what form
4868              this attribute will have, then it might potentially be a
4869              DW_FORM_ref_addr, so we conservatively expect inter-CU
4870              references.  */
4871
4872           if (abbrev_form == DW_FORM_ref_addr
4873               || abbrev_form == DW_FORM_indirect)
4874             cu->has_form_ref_addr = 1;
4875
4876           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
4877           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
4878           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4879           abbrev_ptr += bytes_read;
4880           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4881           abbrev_ptr += bytes_read;
4882         }
4883
4884       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
4885                                          (cur_abbrev->num_attrs
4886                                           * sizeof (struct attr_abbrev)));
4887       memcpy (cur_abbrev->attrs, cur_attrs,
4888               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
4889
4890       hash_number = abbrev_number % ABBREV_HASH_SIZE;
4891       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
4892       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
4893
4894       /* Get next abbreviation.
4895          Under Irix6 the abbreviations for a compilation unit are not
4896          always properly terminated with an abbrev number of 0.
4897          Exit loop if we encounter an abbreviation which we have
4898          already read (which means we are about to read the abbreviations
4899          for the next compile unit) or if the end of the abbreviation
4900          table is reached.  */
4901       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
4902           >= dwarf2_per_objfile->abbrev_size)
4903         break;
4904       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4905       abbrev_ptr += bytes_read;
4906       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
4907         break;
4908     }
4909
4910   xfree (cur_attrs);
4911 }
4912
4913 /* Release the memory used by the abbrev table for a compilation unit.  */
4914
4915 static void
4916 dwarf2_free_abbrev_table (void *ptr_to_cu)
4917 {
4918   struct dwarf2_cu *cu = ptr_to_cu;
4919
4920   obstack_free (&cu->abbrev_obstack, NULL);
4921   cu->dwarf2_abbrevs = NULL;
4922 }
4923
4924 /* Lookup an abbrev_info structure in the abbrev hash table.  */
4925
4926 static struct abbrev_info *
4927 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
4928 {
4929   unsigned int hash_number;
4930   struct abbrev_info *abbrev;
4931
4932   hash_number = number % ABBREV_HASH_SIZE;
4933   abbrev = cu->dwarf2_abbrevs[hash_number];
4934
4935   while (abbrev)
4936     {
4937       if (abbrev->number == number)
4938         return abbrev;
4939       else
4940         abbrev = abbrev->next;
4941     }
4942   return NULL;
4943 }
4944
4945 /* Returns nonzero if TAG represents a type that we might generate a partial
4946    symbol for.  */
4947
4948 static int
4949 is_type_tag_for_partial (int tag)
4950 {
4951   switch (tag)
4952     {
4953 #if 0
4954     /* Some types that would be reasonable to generate partial symbols for,
4955        that we don't at present.  */
4956     case DW_TAG_array_type:
4957     case DW_TAG_file_type:
4958     case DW_TAG_ptr_to_member_type:
4959     case DW_TAG_set_type:
4960     case DW_TAG_string_type:
4961     case DW_TAG_subroutine_type:
4962 #endif
4963     case DW_TAG_base_type:
4964     case DW_TAG_class_type:
4965     case DW_TAG_enumeration_type:
4966     case DW_TAG_structure_type:
4967     case DW_TAG_subrange_type:
4968     case DW_TAG_typedef:
4969     case DW_TAG_union_type:
4970       return 1;
4971     default:
4972       return 0;
4973     }
4974 }
4975
4976 /* Load all DIEs that are interesting for partial symbols into memory.  */
4977
4978 static struct partial_die_info *
4979 load_partial_dies (bfd *abfd, char *info_ptr, int building_psymtab,
4980                    struct dwarf2_cu *cu)
4981 {
4982   struct partial_die_info *part_die;
4983   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
4984   struct abbrev_info *abbrev;
4985   unsigned int bytes_read;
4986
4987   int nesting_level = 1;
4988
4989   parent_die = NULL;
4990   last_die = NULL;
4991
4992   cu->partial_dies
4993     = htab_create_alloc_ex (cu->header.length / 12,
4994                             partial_die_hash,
4995                             partial_die_eq,
4996                             NULL,
4997                             &cu->comp_unit_obstack,
4998                             hashtab_obstack_allocate,
4999                             dummy_obstack_deallocate);
5000
5001   part_die = obstack_alloc (&cu->comp_unit_obstack,
5002                             sizeof (struct partial_die_info));
5003
5004   while (1)
5005     {
5006       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5007
5008       /* A NULL abbrev means the end of a series of children.  */
5009       if (abbrev == NULL)
5010         {
5011           if (--nesting_level == 0)
5012             {
5013               /* PART_DIE was probably the last thing allocated on the
5014                  comp_unit_obstack, so we could call obstack_free
5015                  here.  We don't do that because the waste is small,
5016                  and will be cleaned up when we're done with this
5017                  compilation unit.  This way, we're also more robust
5018                  against other users of the comp_unit_obstack.  */
5019               return first_die;
5020             }
5021           info_ptr += bytes_read;
5022           last_die = parent_die;
5023           parent_die = parent_die->die_parent;
5024           continue;
5025         }
5026
5027       /* Check whether this DIE is interesting enough to save.  */
5028       if (!is_type_tag_for_partial (abbrev->tag)
5029           && abbrev->tag != DW_TAG_enumerator
5030           && abbrev->tag != DW_TAG_subprogram
5031           && abbrev->tag != DW_TAG_variable
5032           && abbrev->tag != DW_TAG_namespace)
5033         {
5034           /* Otherwise we skip to the next sibling, if any.  */
5035           info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5036           continue;
5037         }
5038
5039       info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5040                                    abfd, info_ptr, cu);
5041
5042       /* This two-pass algorithm for processing partial symbols has a
5043          high cost in cache pressure.  Thus, handle some simple cases
5044          here which cover the majority of C partial symbols.  DIEs
5045          which neither have specification tags in them, nor could have
5046          specification tags elsewhere pointing at them, can simply be
5047          processed and discarded.
5048
5049          This segment is also optional; scan_partial_symbols and
5050          add_partial_symbol will handle these DIEs if we chain
5051          them in normally.  When compilers which do not emit large
5052          quantities of duplicate debug information are more common,
5053          this code can probably be removed.  */
5054
5055       /* Any complete simple types at the top level (pretty much all
5056          of them, for a language without namespaces), can be processed
5057          directly.  */
5058       if (parent_die == NULL
5059           && part_die->has_specification == 0
5060           && part_die->is_declaration == 0
5061           && (part_die->tag == DW_TAG_typedef
5062               || part_die->tag == DW_TAG_base_type
5063               || part_die->tag == DW_TAG_subrange_type))
5064         {
5065           if (building_psymtab && part_die->name != NULL)
5066             add_psymbol_to_list (part_die->name, strlen (part_die->name),
5067                                  VAR_DOMAIN, LOC_TYPEDEF,
5068                                  &cu->objfile->static_psymbols,
5069                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
5070           info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5071           continue;
5072         }
5073
5074       /* If we're at the second level, and we're an enumerator, and
5075          our parent has no specification (meaning possibly lives in a
5076          namespace elsewhere), then we can add the partial symbol now
5077          instead of queueing it.  */
5078       if (part_die->tag == DW_TAG_enumerator
5079           && parent_die != NULL
5080           && parent_die->die_parent == NULL
5081           && parent_die->tag == DW_TAG_enumeration_type
5082           && parent_die->has_specification == 0)
5083         {
5084           if (part_die->name == NULL)
5085             complaint (&symfile_complaints, "malformed enumerator DIE ignored");
5086           else if (building_psymtab)
5087             add_psymbol_to_list (part_die->name, strlen (part_die->name),
5088                                  VAR_DOMAIN, LOC_CONST,
5089                                  (cu->language == language_cplus
5090                                   || cu->language == language_java)
5091                                  ? &cu->objfile->global_psymbols
5092                                  : &cu->objfile->static_psymbols,
5093                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
5094
5095           info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5096           continue;
5097         }
5098
5099       /* We'll save this DIE so link it in.  */
5100       part_die->die_parent = parent_die;
5101       part_die->die_sibling = NULL;
5102       part_die->die_child = NULL;
5103
5104       if (last_die && last_die == parent_die)
5105         last_die->die_child = part_die;
5106       else if (last_die)
5107         last_die->die_sibling = part_die;
5108
5109       last_die = part_die;
5110
5111       if (first_die == NULL)
5112         first_die = part_die;
5113
5114       /* Maybe add the DIE to the hash table.  Not all DIEs that we
5115          find interesting need to be in the hash table, because we
5116          also have the parent/sibling/child chains; only those that we
5117          might refer to by offset later during partial symbol reading.
5118
5119          For now this means things that might have be the target of a
5120          DW_AT_specification, DW_AT_abstract_origin, or
5121          DW_AT_extension.  DW_AT_extension will refer only to
5122          namespaces; DW_AT_abstract_origin refers to functions (and
5123          many things under the function DIE, but we do not recurse
5124          into function DIEs during partial symbol reading) and
5125          possibly variables as well; DW_AT_specification refers to
5126          declarations.  Declarations ought to have the DW_AT_declaration
5127          flag.  It happens that GCC forgets to put it in sometimes, but
5128          only for functions, not for types.
5129
5130          Adding more things than necessary to the hash table is harmless
5131          except for the performance cost.  Adding too few will result in
5132          internal errors in find_partial_die.  */
5133
5134       if (abbrev->tag == DW_TAG_subprogram
5135           || abbrev->tag == DW_TAG_variable
5136           || abbrev->tag == DW_TAG_namespace
5137           || part_die->is_declaration)
5138         {
5139           void **slot;
5140
5141           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5142                                            part_die->offset, INSERT);
5143           *slot = part_die;
5144         }
5145
5146       part_die = obstack_alloc (&cu->comp_unit_obstack,
5147                                 sizeof (struct partial_die_info));
5148
5149       /* For some DIEs we want to follow their children (if any).  For C
5150          we have no reason to follow the children of structures; for other
5151          languages we have to, both so that we can get at method physnames
5152          to infer fully qualified class names, and for DW_AT_specification.  */
5153       if (last_die->has_children
5154           && (last_die->tag == DW_TAG_namespace
5155               || last_die->tag == DW_TAG_enumeration_type
5156               || (cu->language != language_c
5157                   && (last_die->tag == DW_TAG_class_type
5158                       || last_die->tag == DW_TAG_structure_type
5159                       || last_die->tag == DW_TAG_union_type))))
5160         {
5161           nesting_level++;
5162           parent_die = last_die;
5163           continue;
5164         }
5165
5166       /* Otherwise we skip to the next sibling, if any.  */
5167       info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5168
5169       /* Back to the top, do it again.  */
5170     }
5171 }
5172
5173 /* Read a minimal amount of information into the minimal die structure.  */
5174
5175 static char *
5176 read_partial_die (struct partial_die_info *part_die,
5177                   struct abbrev_info *abbrev,
5178                   unsigned int abbrev_len, bfd *abfd,
5179                   char *info_ptr, struct dwarf2_cu *cu)
5180 {
5181   unsigned int bytes_read, i;
5182   struct attribute attr;
5183   int has_low_pc_attr = 0;
5184   int has_high_pc_attr = 0;
5185
5186   memset (part_die, 0, sizeof (struct partial_die_info));
5187
5188   part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
5189
5190   info_ptr += abbrev_len;
5191
5192   if (abbrev == NULL)
5193     return info_ptr;
5194
5195   part_die->tag = abbrev->tag;
5196   part_die->has_children = abbrev->has_children;
5197
5198   for (i = 0; i < abbrev->num_attrs; ++i)
5199     {
5200       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
5201
5202       /* Store the data if it is of an attribute we want to keep in a
5203          partial symbol table.  */
5204       switch (attr.name)
5205         {
5206         case DW_AT_name:
5207
5208           /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
5209           if (part_die->name == NULL)
5210             part_die->name = DW_STRING (&attr);
5211           break;
5212         case DW_AT_comp_dir:
5213           if (part_die->dirname == NULL)
5214             part_die->dirname = DW_STRING (&attr);
5215           break;
5216         case DW_AT_MIPS_linkage_name:
5217           part_die->name = DW_STRING (&attr);
5218           break;
5219         case DW_AT_low_pc:
5220           has_low_pc_attr = 1;
5221           part_die->lowpc = DW_ADDR (&attr);
5222           break;
5223         case DW_AT_high_pc:
5224           has_high_pc_attr = 1;
5225           part_die->highpc = DW_ADDR (&attr);
5226           break;
5227         case DW_AT_location:
5228           /* Support the .debug_loc offsets */
5229           if (attr_form_is_block (&attr))
5230             {
5231                part_die->locdesc = DW_BLOCK (&attr);
5232             }
5233           else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
5234             {
5235               dwarf2_complex_location_expr_complaint ();
5236             }
5237           else
5238             {
5239               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5240                                                      "partial symbol information");
5241             }
5242           break;
5243         case DW_AT_language:
5244           part_die->language = DW_UNSND (&attr);
5245           break;
5246         case DW_AT_external:
5247           part_die->is_external = DW_UNSND (&attr);
5248           break;
5249         case DW_AT_declaration:
5250           part_die->is_declaration = DW_UNSND (&attr);
5251           break;
5252         case DW_AT_type:
5253           part_die->has_type = 1;
5254           break;
5255         case DW_AT_abstract_origin:
5256         case DW_AT_specification:
5257         case DW_AT_extension:
5258           part_die->has_specification = 1;
5259           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr, cu);
5260           break;
5261         case DW_AT_sibling:
5262           /* Ignore absolute siblings, they might point outside of
5263              the current compile unit.  */
5264           if (attr.form == DW_FORM_ref_addr)
5265             complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
5266           else
5267             part_die->sibling = dwarf2_per_objfile->info_buffer
5268               + dwarf2_get_ref_die_offset (&attr, cu);
5269           break;
5270         case DW_AT_stmt_list:
5271           part_die->has_stmt_list = 1;
5272           part_die->line_offset = DW_UNSND (&attr);
5273           break;
5274         default:
5275           break;
5276         }
5277     }
5278
5279   /* When using the GNU linker, .gnu.linkonce. sections are used to
5280      eliminate duplicate copies of functions and vtables and such.
5281      The linker will arbitrarily choose one and discard the others.
5282      The AT_*_pc values for such functions refer to local labels in
5283      these sections.  If the section from that file was discarded, the
5284      labels are not in the output, so the relocs get a value of 0.
5285      If this is a discarded function, mark the pc bounds as invalid,
5286      so that GDB will ignore it.  */
5287   if (has_low_pc_attr && has_high_pc_attr
5288       && part_die->lowpc < part_die->highpc
5289       && (part_die->lowpc != 0
5290           || (bfd_get_file_flags (abfd) & HAS_RELOC)))
5291     part_die->has_pc_info = 1;
5292   return info_ptr;
5293 }
5294
5295 /* Find a cached partial DIE at OFFSET in CU.  */
5296
5297 static struct partial_die_info *
5298 find_partial_die_in_comp_unit (unsigned long offset, struct dwarf2_cu *cu)
5299 {
5300   struct partial_die_info *lookup_die = NULL;
5301   struct partial_die_info part_die;
5302
5303   part_die.offset = offset;
5304   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
5305
5306   if (lookup_die == NULL)
5307     internal_error (__FILE__, __LINE__,
5308                     "could not find partial DIE in cache\n");
5309
5310   return lookup_die;
5311 }
5312
5313 /* Find a partial DIE at OFFSET, which may or may not be in CU.  */
5314
5315 static struct partial_die_info *
5316 find_partial_die (unsigned long offset, struct dwarf2_cu *cu,
5317                   struct dwarf2_cu **target_cu)
5318 {
5319   struct dwarf2_per_cu_data *per_cu;
5320
5321   if (offset >= cu->header.offset
5322       && offset < cu->header.offset + cu->header.length)
5323     {
5324       *target_cu = cu;
5325       return find_partial_die_in_comp_unit (offset, cu);
5326     }
5327
5328   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5329
5330   /* If this offset isn't pointing into a known compilation unit,
5331      the debug information is probably corrupted.  */
5332   if (per_cu == NULL)
5333     error ("Dwarf Error: could not find partial DIE containing "
5334            "offset 0x%lx [in module %s]",
5335            (long) offset, bfd_get_filename (cu->objfile->obfd));
5336
5337   if (per_cu->cu == NULL)
5338     {
5339       load_comp_unit (per_cu, cu->objfile);
5340       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5341       dwarf2_per_objfile->read_in_chain = per_cu;
5342     }
5343
5344   per_cu->cu->last_used = 0;
5345   *target_cu = per_cu->cu;
5346   return find_partial_die_in_comp_unit (offset, per_cu->cu);
5347 }
5348
5349 /* Adjust PART_DIE before generating a symbol for it.  This function
5350    may set the is_external flag or change the DIE's name.  */
5351
5352 static void
5353 fixup_partial_die (struct partial_die_info *part_die,
5354                    struct dwarf2_cu *cu)
5355 {
5356   /* If we found a reference attribute and the DIE has no name, try
5357      to find a name in the referred to DIE.  */
5358
5359   if (part_die->name == NULL && part_die->has_specification)
5360     {
5361       struct partial_die_info *spec_die;
5362       struct dwarf2_cu *spec_cu;
5363
5364       spec_die = find_partial_die (part_die->spec_offset, cu, &spec_cu);
5365
5366       fixup_partial_die (spec_die, spec_cu);
5367
5368       if (spec_die->name)
5369         {
5370           part_die->name = spec_die->name;
5371
5372           /* Copy DW_AT_external attribute if it is set.  */
5373           if (spec_die->is_external)
5374             part_die->is_external = spec_die->is_external;
5375         }
5376     }
5377
5378   /* Set default names for some unnamed DIEs.  */
5379   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
5380                                  || part_die->tag == DW_TAG_class_type))
5381     part_die->name = "(anonymous class)";
5382
5383   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
5384     part_die->name = "(anonymous namespace)";
5385
5386   if (part_die->tag == DW_TAG_structure_type
5387       || part_die->tag == DW_TAG_class_type
5388       || part_die->tag == DW_TAG_union_type)
5389     guess_structure_name (part_die, cu);
5390 }
5391
5392 /* Read the die from the .debug_info section buffer.  Set DIEP to
5393    point to a newly allocated die with its information, except for its
5394    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
5395    whether the die has children or not.  */
5396
5397 static char *
5398 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
5399                struct dwarf2_cu *cu, int *has_children)
5400 {
5401   unsigned int abbrev_number, bytes_read, i, offset;
5402   struct abbrev_info *abbrev;
5403   struct die_info *die;
5404
5405   offset = info_ptr - dwarf2_per_objfile->info_buffer;
5406   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5407   info_ptr += bytes_read;
5408   if (!abbrev_number)
5409     {
5410       die = dwarf_alloc_die ();
5411       die->tag = 0;
5412       die->abbrev = abbrev_number;
5413       die->type = NULL;
5414       *diep = die;
5415       *has_children = 0;
5416       return info_ptr;
5417     }
5418
5419   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5420   if (!abbrev)
5421     {
5422       error ("Dwarf Error: could not find abbrev number %d [in module %s]",
5423              abbrev_number,
5424              bfd_get_filename (abfd));
5425     }
5426   die = dwarf_alloc_die ();
5427   die->offset = offset;
5428   die->tag = abbrev->tag;
5429   die->abbrev = abbrev_number;
5430   die->type = NULL;
5431
5432   die->num_attrs = abbrev->num_attrs;
5433   die->attrs = (struct attribute *)
5434     xmalloc (die->num_attrs * sizeof (struct attribute));
5435
5436   for (i = 0; i < abbrev->num_attrs; ++i)
5437     {
5438       info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
5439                                  abfd, info_ptr, cu);
5440     }
5441
5442   *diep = die;
5443   *has_children = abbrev->has_children;
5444   return info_ptr;
5445 }
5446
5447 /* Read an attribute value described by an attribute form.  */
5448
5449 static char *
5450 read_attribute_value (struct attribute *attr, unsigned form,
5451                       bfd *abfd, char *info_ptr,
5452                       struct dwarf2_cu *cu)
5453 {
5454   struct comp_unit_head *cu_header = &cu->header;
5455   unsigned int bytes_read;
5456   struct dwarf_block *blk;
5457
5458   attr->form = form;
5459   switch (form)
5460     {
5461     case DW_FORM_addr:
5462     case DW_FORM_ref_addr:
5463       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
5464       info_ptr += bytes_read;
5465       break;
5466     case DW_FORM_block2:
5467       blk = dwarf_alloc_block (cu);
5468       blk->size = read_2_bytes (abfd, info_ptr);
5469       info_ptr += 2;
5470       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5471       info_ptr += blk->size;
5472       DW_BLOCK (attr) = blk;
5473       break;
5474     case DW_FORM_block4:
5475       blk = dwarf_alloc_block (cu);
5476       blk->size = read_4_bytes (abfd, info_ptr);
5477       info_ptr += 4;
5478       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5479       info_ptr += blk->size;
5480       DW_BLOCK (attr) = blk;
5481       break;
5482     case DW_FORM_data2:
5483       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
5484       info_ptr += 2;
5485       break;
5486     case DW_FORM_data4:
5487       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
5488       info_ptr += 4;
5489       break;
5490     case DW_FORM_data8:
5491       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
5492       info_ptr += 8;
5493       break;
5494     case DW_FORM_string:
5495       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
5496       info_ptr += bytes_read;
5497       break;
5498     case DW_FORM_strp:
5499       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
5500                                                &bytes_read);
5501       info_ptr += bytes_read;
5502       break;
5503     case DW_FORM_block:
5504       blk = dwarf_alloc_block (cu);
5505       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5506       info_ptr += bytes_read;
5507       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5508       info_ptr += blk->size;
5509       DW_BLOCK (attr) = blk;
5510       break;
5511     case DW_FORM_block1:
5512       blk = dwarf_alloc_block (cu);
5513       blk->size = read_1_byte (abfd, info_ptr);
5514       info_ptr += 1;
5515       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5516       info_ptr += blk->size;
5517       DW_BLOCK (attr) = blk;
5518       break;
5519     case DW_FORM_data1:
5520       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5521       info_ptr += 1;
5522       break;
5523     case DW_FORM_flag:
5524       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5525       info_ptr += 1;
5526       break;
5527     case DW_FORM_sdata:
5528       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
5529       info_ptr += bytes_read;
5530       break;
5531     case DW_FORM_udata:
5532       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5533       info_ptr += bytes_read;
5534       break;
5535     case DW_FORM_ref1:
5536       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5537       info_ptr += 1;
5538       break;
5539     case DW_FORM_ref2:
5540       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
5541       info_ptr += 2;
5542       break;
5543     case DW_FORM_ref4:
5544       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
5545       info_ptr += 4;
5546       break;
5547     case DW_FORM_ref8:
5548       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
5549       info_ptr += 8;
5550       break;
5551     case DW_FORM_ref_udata:
5552       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5553       info_ptr += bytes_read;
5554       break;
5555     case DW_FORM_indirect:
5556       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5557       info_ptr += bytes_read;
5558       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
5559       break;
5560     default:
5561       error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
5562              dwarf_form_name (form),
5563              bfd_get_filename (abfd));
5564     }
5565   return info_ptr;
5566 }
5567
5568 /* Read an attribute described by an abbreviated attribute.  */
5569
5570 static char *
5571 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
5572                 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
5573 {
5574   attr->name = abbrev->name;
5575   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
5576 }
5577
5578 /* read dwarf information from a buffer */
5579
5580 static unsigned int
5581 read_1_byte (bfd *abfd, char *buf)
5582 {
5583   return bfd_get_8 (abfd, (bfd_byte *) buf);
5584 }
5585
5586 static int
5587 read_1_signed_byte (bfd *abfd, char *buf)
5588 {
5589   return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
5590 }
5591
5592 static unsigned int
5593 read_2_bytes (bfd *abfd, char *buf)
5594 {
5595   return bfd_get_16 (abfd, (bfd_byte *) buf);
5596 }
5597
5598 static int
5599 read_2_signed_bytes (bfd *abfd, char *buf)
5600 {
5601   return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
5602 }
5603
5604 static unsigned int
5605 read_4_bytes (bfd *abfd, char *buf)
5606 {
5607   return bfd_get_32 (abfd, (bfd_byte *) buf);
5608 }
5609
5610 static int
5611 read_4_signed_bytes (bfd *abfd, char *buf)
5612 {
5613   return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
5614 }
5615
5616 static unsigned long
5617 read_8_bytes (bfd *abfd, char *buf)
5618 {
5619   return bfd_get_64 (abfd, (bfd_byte *) buf);
5620 }
5621
5622 static CORE_ADDR
5623 read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
5624 {
5625   struct comp_unit_head *cu_header = &cu->header;
5626   CORE_ADDR retval = 0;
5627
5628   if (cu_header->signed_addr_p)
5629     {
5630       switch (cu_header->addr_size)
5631         {
5632         case 2:
5633           retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
5634           break;
5635         case 4:
5636           retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
5637           break;
5638         case 8:
5639           retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
5640           break;
5641         default:
5642           internal_error (__FILE__, __LINE__,
5643                           "read_address: bad switch, signed [in module %s]",
5644                           bfd_get_filename (abfd));
5645         }
5646     }
5647   else
5648     {
5649       switch (cu_header->addr_size)
5650         {
5651         case 2:
5652           retval = bfd_get_16 (abfd, (bfd_byte *) buf);
5653           break;
5654         case 4:
5655           retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5656           break;
5657         case 8:
5658           retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5659           break;
5660         default:
5661           internal_error (__FILE__, __LINE__,
5662                           "read_address: bad switch, unsigned [in module %s]",
5663                           bfd_get_filename (abfd));
5664         }
5665     }
5666
5667   *bytes_read = cu_header->addr_size;
5668   return retval;
5669 }
5670
5671 /* Read the initial length from a section.  The (draft) DWARF 3
5672    specification allows the initial length to take up either 4 bytes
5673    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
5674    bytes describe the length and all offsets will be 8 bytes in length
5675    instead of 4.
5676
5677    An older, non-standard 64-bit format is also handled by this
5678    function.  The older format in question stores the initial length
5679    as an 8-byte quantity without an escape value.  Lengths greater
5680    than 2^32 aren't very common which means that the initial 4 bytes
5681    is almost always zero.  Since a length value of zero doesn't make
5682    sense for the 32-bit format, this initial zero can be considered to
5683    be an escape value which indicates the presence of the older 64-bit
5684    format.  As written, the code can't detect (old format) lengths
5685    greater than 4GB.  If it becomes necessary to handle lengths somewhat
5686    larger than 4GB, we could allow other small values (such as the
5687    non-sensical values of 1, 2, and 3) to also be used as escape values
5688    indicating the presence of the old format.
5689
5690    The value returned via bytes_read should be used to increment
5691    the relevant pointer after calling read_initial_length().
5692    
5693    As a side effect, this function sets the fields initial_length_size
5694    and offset_size in cu_header to the values appropriate for the
5695    length field.  (The format of the initial length field determines
5696    the width of file offsets to be fetched later with fetch_offset().)
5697    
5698    [ Note:  read_initial_length() and read_offset() are based on the
5699      document entitled "DWARF Debugging Information Format", revision
5700      3, draft 8, dated November 19, 2001.  This document was obtained
5701      from:
5702
5703         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
5704      
5705      This document is only a draft and is subject to change.  (So beware.)
5706
5707      Details regarding the older, non-standard 64-bit format were
5708      determined empirically by examining 64-bit ELF files produced
5709      by the SGI toolchain on an IRIX 6.5 machine.
5710
5711      - Kevin, July 16, 2002
5712    ] */
5713
5714 static LONGEST
5715 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
5716                      int *bytes_read)
5717 {
5718   LONGEST retval = 0;
5719
5720   retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5721
5722   if (retval == 0xffffffff)
5723     {
5724       retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
5725       *bytes_read = 12;
5726       if (cu_header != NULL)
5727         {
5728           cu_header->initial_length_size = 12;
5729           cu_header->offset_size = 8;
5730         }
5731     }
5732   else if (retval == 0)
5733     {
5734       /* Handle (non-standard) 64-bit DWARF2 formats such as that used
5735          by IRIX.  */
5736       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5737       *bytes_read = 8;
5738       if (cu_header != NULL)
5739         {
5740           cu_header->initial_length_size = 8;
5741           cu_header->offset_size = 8;
5742         }
5743     }
5744   else
5745     {
5746       *bytes_read = 4;
5747       if (cu_header != NULL)
5748         {
5749           cu_header->initial_length_size = 4;
5750           cu_header->offset_size = 4;
5751         }
5752     }
5753
5754  return retval;
5755 }
5756
5757 /* Read an offset from the data stream.  The size of the offset is
5758    given by cu_header->offset_size. */
5759
5760 static LONGEST
5761 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
5762              int *bytes_read)
5763 {
5764   LONGEST retval = 0;
5765
5766   switch (cu_header->offset_size)
5767     {
5768     case 4:
5769       retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5770       *bytes_read = 4;
5771       break;
5772     case 8:
5773       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5774       *bytes_read = 8;
5775       break;
5776     default:
5777       internal_error (__FILE__, __LINE__,
5778                       "read_offset: bad switch [in module %s]",
5779                       bfd_get_filename (abfd));
5780     }
5781
5782  return retval;
5783 }
5784
5785 static char *
5786 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
5787 {
5788   /* If the size of a host char is 8 bits, we can return a pointer
5789      to the buffer, otherwise we have to copy the data to a buffer
5790      allocated on the temporary obstack.  */
5791   gdb_assert (HOST_CHAR_BIT == 8);
5792   return buf;
5793 }
5794
5795 static char *
5796 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5797 {
5798   /* If the size of a host char is 8 bits, we can return a pointer
5799      to the string, otherwise we have to copy the string to a buffer
5800      allocated on the temporary obstack.  */
5801   gdb_assert (HOST_CHAR_BIT == 8);
5802   if (*buf == '\0')
5803     {
5804       *bytes_read_ptr = 1;
5805       return NULL;
5806     }
5807   *bytes_read_ptr = strlen (buf) + 1;
5808   return buf;
5809 }
5810
5811 static char *
5812 read_indirect_string (bfd *abfd, char *buf,
5813                       const struct comp_unit_head *cu_header,
5814                       unsigned int *bytes_read_ptr)
5815 {
5816   LONGEST str_offset = read_offset (abfd, buf, cu_header,
5817                                     (int *) bytes_read_ptr);
5818
5819   if (dwarf2_per_objfile->str_buffer == NULL)
5820     {
5821       error ("DW_FORM_strp used without .debug_str section [in module %s]",
5822                       bfd_get_filename (abfd));
5823       return NULL;
5824     }
5825   if (str_offset >= dwarf2_per_objfile->str_size)
5826     {
5827       error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
5828                       bfd_get_filename (abfd));
5829       return NULL;
5830     }
5831   gdb_assert (HOST_CHAR_BIT == 8);
5832   if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
5833     return NULL;
5834   return dwarf2_per_objfile->str_buffer + str_offset;
5835 }
5836
5837 static unsigned long
5838 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5839 {
5840   unsigned long result;
5841   unsigned int num_read;
5842   int i, shift;
5843   unsigned char byte;
5844
5845   result = 0;
5846   shift = 0;
5847   num_read = 0;
5848   i = 0;
5849   while (1)
5850     {
5851       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5852       buf++;
5853       num_read++;
5854       result |= ((unsigned long)(byte & 127) << shift);
5855       if ((byte & 128) == 0)
5856         {
5857           break;
5858         }
5859       shift += 7;
5860     }
5861   *bytes_read_ptr = num_read;
5862   return result;
5863 }
5864
5865 static long
5866 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5867 {
5868   long result;
5869   int i, shift, size, num_read;
5870   unsigned char byte;
5871
5872   result = 0;
5873   shift = 0;
5874   size = 32;
5875   num_read = 0;
5876   i = 0;
5877   while (1)
5878     {
5879       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5880       buf++;
5881       num_read++;
5882       result |= ((long)(byte & 127) << shift);
5883       shift += 7;
5884       if ((byte & 128) == 0)
5885         {
5886           break;
5887         }
5888     }
5889   if ((shift < size) && (byte & 0x40))
5890     {
5891       result |= -(1 << shift);
5892     }
5893   *bytes_read_ptr = num_read;
5894   return result;
5895 }
5896
5897 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
5898
5899 static char *
5900 skip_leb128 (bfd *abfd, char *buf)
5901 {
5902   int byte;
5903
5904   while (1)
5905     {
5906       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5907       buf++;
5908       if ((byte & 128) == 0)
5909         return buf;
5910     }
5911 }
5912
5913 static void
5914 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
5915 {
5916   switch (lang)
5917     {
5918     case DW_LANG_C89:
5919     case DW_LANG_C:
5920       cu->language = language_c;
5921       break;
5922     case DW_LANG_C_plus_plus:
5923       cu->language = language_cplus;
5924       break;
5925     case DW_LANG_Fortran77:
5926     case DW_LANG_Fortran90:
5927     case DW_LANG_Fortran95:
5928       cu->language = language_fortran;
5929       break;
5930     case DW_LANG_Mips_Assembler:
5931       cu->language = language_asm;
5932       break;
5933     case DW_LANG_Java:
5934       cu->language = language_java;
5935       break;
5936     case DW_LANG_Ada83:
5937     case DW_LANG_Ada95:
5938     case DW_LANG_Cobol74:
5939     case DW_LANG_Cobol85:
5940     case DW_LANG_Pascal83:
5941     case DW_LANG_Modula2:
5942     default:
5943       cu->language = language_minimal;
5944       break;
5945     }
5946   cu->language_defn = language_def (cu->language);
5947 }
5948
5949 /* Return the named attribute or NULL if not there.  */
5950
5951 static struct attribute *
5952 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
5953 {
5954   unsigned int i;
5955   struct attribute *spec = NULL;
5956
5957   for (i = 0; i < die->num_attrs; ++i)
5958     {
5959       if (die->attrs[i].name == name)
5960         {
5961           return &die->attrs[i];
5962         }
5963       if (die->attrs[i].name == DW_AT_specification
5964           || die->attrs[i].name == DW_AT_abstract_origin)
5965         spec = &die->attrs[i];
5966     }
5967   if (spec)
5968     {
5969       struct die_info *ref_die =
5970       follow_die_ref (dwarf2_get_ref_die_offset (spec, cu));
5971
5972       if (ref_die)
5973         return dwarf2_attr (ref_die, name, cu);
5974     }
5975
5976   return NULL;
5977 }
5978
5979 /* Return non-zero iff the attribute NAME is defined for the given DIE,
5980    and holds a non-zero value.  This function should only be used for
5981    DW_FORM_flag attributes.  */
5982
5983 static int
5984 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
5985 {
5986   struct attribute *attr = dwarf2_attr (die, name, cu);
5987
5988   return (attr && DW_UNSND (attr));
5989 }
5990
5991 static int
5992 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
5993 {
5994   /* A DIE is a declaration if it has a DW_AT_declaration attribute
5995      which value is non-zero.  However, we have to be careful with
5996      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
5997      (via dwarf2_flag_true_p) follows this attribute.  So we may
5998      end up accidently finding a declaration attribute that belongs
5999      to a different DIE referenced by the specification attribute,
6000      even though the given DIE does not have a declaration attribute.  */
6001   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
6002           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
6003 }
6004
6005 /* Return the die giving the specification for DIE, if there is
6006    one.  */
6007
6008 static struct die_info *
6009 die_specification (struct die_info *die, struct dwarf2_cu *cu)
6010 {
6011   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification, cu);
6012
6013   if (spec_attr == NULL)
6014     return NULL;
6015   else
6016     return follow_die_ref (dwarf2_get_ref_die_offset (spec_attr, cu));
6017 }
6018
6019 /* Free the line_header structure *LH, and any arrays and strings it
6020    refers to.  */
6021 static void
6022 free_line_header (struct line_header *lh)
6023 {
6024   if (lh->standard_opcode_lengths)
6025     xfree (lh->standard_opcode_lengths);
6026
6027   /* Remember that all the lh->file_names[i].name pointers are
6028      pointers into debug_line_buffer, and don't need to be freed.  */
6029   if (lh->file_names)
6030     xfree (lh->file_names);
6031
6032   /* Similarly for the include directory names.  */
6033   if (lh->include_dirs)
6034     xfree (lh->include_dirs);
6035
6036   xfree (lh);
6037 }
6038
6039
6040 /* Add an entry to LH's include directory table.  */
6041 static void
6042 add_include_dir (struct line_header *lh, char *include_dir)
6043 {
6044   /* Grow the array if necessary.  */
6045   if (lh->include_dirs_size == 0)
6046     {
6047       lh->include_dirs_size = 1; /* for testing */
6048       lh->include_dirs = xmalloc (lh->include_dirs_size
6049                                   * sizeof (*lh->include_dirs));
6050     }
6051   else if (lh->num_include_dirs >= lh->include_dirs_size)
6052     {
6053       lh->include_dirs_size *= 2;
6054       lh->include_dirs = xrealloc (lh->include_dirs,
6055                                    (lh->include_dirs_size
6056                                     * sizeof (*lh->include_dirs)));
6057     }
6058
6059   lh->include_dirs[lh->num_include_dirs++] = include_dir;
6060 }
6061  
6062
6063 /* Add an entry to LH's file name table.  */
6064 static void
6065 add_file_name (struct line_header *lh,
6066                char *name,
6067                unsigned int dir_index,
6068                unsigned int mod_time,
6069                unsigned int length)
6070 {
6071   struct file_entry *fe;
6072
6073   /* Grow the array if necessary.  */
6074   if (lh->file_names_size == 0)
6075     {
6076       lh->file_names_size = 1; /* for testing */
6077       lh->file_names = xmalloc (lh->file_names_size
6078                                 * sizeof (*lh->file_names));
6079     }
6080   else if (lh->num_file_names >= lh->file_names_size)
6081     {
6082       lh->file_names_size *= 2;
6083       lh->file_names = xrealloc (lh->file_names,
6084                                  (lh->file_names_size
6085                                   * sizeof (*lh->file_names)));
6086     }
6087
6088   fe = &lh->file_names[lh->num_file_names++];
6089   fe->name = name;
6090   fe->dir_index = dir_index;
6091   fe->mod_time = mod_time;
6092   fe->length = length;
6093   fe->included_p = 0;
6094 }
6095  
6096
6097 /* Read the statement program header starting at OFFSET in
6098    .debug_line, according to the endianness of ABFD.  Return a pointer
6099    to a struct line_header, allocated using xmalloc.
6100
6101    NOTE: the strings in the include directory and file name tables of
6102    the returned object point into debug_line_buffer, and must not be
6103    freed.  */
6104 static struct line_header *
6105 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
6106                           struct dwarf2_cu *cu)
6107 {
6108   struct cleanup *back_to;
6109   struct line_header *lh;
6110   char *line_ptr;
6111   int bytes_read;
6112   int i;
6113   char *cur_dir, *cur_file;
6114
6115   if (dwarf2_per_objfile->line_buffer == NULL)
6116     {
6117       complaint (&symfile_complaints, "missing .debug_line section");
6118       return 0;
6119     }
6120
6121   /* Make sure that at least there's room for the total_length field.  That
6122      could be 12 bytes long, but we're just going to fudge that.  */
6123   if (offset + 4 >= dwarf2_per_objfile->line_size)
6124     {
6125       dwarf2_statement_list_fits_in_line_number_section_complaint ();
6126       return 0;
6127     }
6128
6129   lh = xmalloc (sizeof (*lh));
6130   memset (lh, 0, sizeof (*lh));
6131   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
6132                           (void *) lh);
6133
6134   line_ptr = dwarf2_per_objfile->line_buffer + offset;
6135
6136   /* read in the header */
6137   lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
6138   line_ptr += bytes_read;
6139   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
6140                                      + dwarf2_per_objfile->line_size))
6141     {
6142       dwarf2_statement_list_fits_in_line_number_section_complaint ();
6143       return 0;
6144     }
6145   lh->statement_program_end = line_ptr + lh->total_length;
6146   lh->version = read_2_bytes (abfd, line_ptr);
6147   line_ptr += 2;
6148   lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
6149   line_ptr += bytes_read;
6150   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
6151   line_ptr += 1;
6152   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
6153   line_ptr += 1;
6154   lh->line_base = read_1_signed_byte (abfd, line_ptr);
6155   line_ptr += 1;
6156   lh->line_range = read_1_byte (abfd, line_ptr);
6157   line_ptr += 1;
6158   lh->opcode_base = read_1_byte (abfd, line_ptr);
6159   line_ptr += 1;
6160   lh->standard_opcode_lengths
6161     = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
6162
6163   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
6164   for (i = 1; i < lh->opcode_base; ++i)
6165     {
6166       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
6167       line_ptr += 1;
6168     }
6169
6170   /* Read directory table  */
6171   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6172     {
6173       line_ptr += bytes_read;
6174       add_include_dir (lh, cur_dir);
6175     }
6176   line_ptr += bytes_read;
6177
6178   /* Read file name table */
6179   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6180     {
6181       unsigned int dir_index, mod_time, length;
6182
6183       line_ptr += bytes_read;
6184       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6185       line_ptr += bytes_read;
6186       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6187       line_ptr += bytes_read;
6188       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6189       line_ptr += bytes_read;
6190
6191       add_file_name (lh, cur_file, dir_index, mod_time, length);
6192     }
6193   line_ptr += bytes_read;
6194   lh->statement_program_start = line_ptr; 
6195
6196   if (line_ptr > (dwarf2_per_objfile->line_buffer
6197                   + dwarf2_per_objfile->line_size))
6198     complaint (&symfile_complaints,
6199                "line number info header doesn't fit in `.debug_line' section");
6200
6201   discard_cleanups (back_to);
6202   return lh;
6203 }
6204
6205 /* This function exists to work around a bug in certain compilers
6206    (particularly GCC 2.95), in which the first line number marker of a
6207    function does not show up until after the prologue, right before
6208    the second line number marker.  This function shifts ADDRESS down
6209    to the beginning of the function if necessary, and is called on
6210    addresses passed to record_line.  */
6211
6212 static CORE_ADDR
6213 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
6214 {
6215   struct function_range *fn;
6216
6217   /* Find the function_range containing address.  */
6218   if (!cu->first_fn)
6219     return address;
6220
6221   if (!cu->cached_fn)
6222     cu->cached_fn = cu->first_fn;
6223
6224   fn = cu->cached_fn;
6225   while (fn)
6226     if (fn->lowpc <= address && fn->highpc > address)
6227       goto found;
6228     else
6229       fn = fn->next;
6230
6231   fn = cu->first_fn;
6232   while (fn && fn != cu->cached_fn)
6233     if (fn->lowpc <= address && fn->highpc > address)
6234       goto found;
6235     else
6236       fn = fn->next;
6237
6238   return address;
6239
6240  found:
6241   if (fn->seen_line)
6242     return address;
6243   if (address != fn->lowpc)
6244     complaint (&symfile_complaints,
6245                "misplaced first line number at 0x%lx for '%s'",
6246                (unsigned long) address, fn->name);
6247   fn->seen_line = 1;
6248   return fn->lowpc;
6249 }
6250
6251 /* Decode the Line Number Program (LNP) for the given line_header
6252    structure and CU.  The actual information extracted and the type
6253    of structures created from the LNP depends on the value of PST.
6254
6255    1. If PST is NULL, then this procedure uses the data from the program
6256       to create all necessary symbol tables, and their linetables.
6257       The compilation directory of the file is passed in COMP_DIR,
6258       and must not be NULL.
6259    
6260    2. If PST is not NULL, this procedure reads the program to determine
6261       the list of files included by the unit represented by PST, and
6262       builds all the associated partial symbol tables.  In this case,
6263       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
6264       is not used to compute the full name of the symtab, and therefore
6265       omitting it when building the partial symtab does not introduce
6266       the potential for inconsistency - a partial symtab and its associated
6267       symbtab having a different fullname -).  */
6268
6269 static void
6270 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
6271                     struct dwarf2_cu *cu, struct partial_symtab *pst)
6272 {
6273   char *line_ptr;
6274   char *line_end;
6275   unsigned int bytes_read;
6276   unsigned char op_code, extended_op, adj_opcode;
6277   CORE_ADDR baseaddr;
6278   struct objfile *objfile = cu->objfile;
6279   const int decode_for_pst_p = (pst != NULL);
6280
6281   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6282
6283   line_ptr = lh->statement_program_start;
6284   line_end = lh->statement_program_end;
6285
6286   /* Read the statement sequences until there's nothing left.  */
6287   while (line_ptr < line_end)
6288     {
6289       /* state machine registers  */
6290       CORE_ADDR address = 0;
6291       unsigned int file = 1;
6292       unsigned int line = 1;
6293       unsigned int column = 0;
6294       int is_stmt = lh->default_is_stmt;
6295       int basic_block = 0;
6296       int end_sequence = 0;
6297
6298       if (!decode_for_pst_p && lh->num_file_names >= file)
6299         {
6300           /* Start a subfile for the current file of the state machine.  */
6301           /* lh->include_dirs and lh->file_names are 0-based, but the
6302              directory and file name numbers in the statement program
6303              are 1-based.  */
6304           struct file_entry *fe = &lh->file_names[file - 1];
6305           char *dir;
6306           if (fe->dir_index)
6307             dir = lh->include_dirs[fe->dir_index - 1];
6308           else
6309             dir = comp_dir;
6310           dwarf2_start_subfile (fe->name, dir);
6311         }
6312
6313       /* Decode the table. */
6314       while (!end_sequence)
6315         {
6316           op_code = read_1_byte (abfd, line_ptr);
6317           line_ptr += 1;
6318
6319           if (op_code >= lh->opcode_base)
6320             {           /* Special operand.  */
6321               adj_opcode = op_code - lh->opcode_base;
6322               address += (adj_opcode / lh->line_range)
6323                 * lh->minimum_instruction_length;
6324               line += lh->line_base + (adj_opcode % lh->line_range);
6325               lh->file_names[file - 1].included_p = 1;
6326               if (!decode_for_pst_p)
6327                 {
6328                   /* append row to matrix using current values */
6329                   record_line (current_subfile, line, 
6330                                check_cu_functions (address, cu));
6331                 }
6332               basic_block = 1;
6333             }
6334           else switch (op_code)
6335             {
6336             case DW_LNS_extended_op:
6337               read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6338               line_ptr += bytes_read;
6339               extended_op = read_1_byte (abfd, line_ptr);
6340               line_ptr += 1;
6341               switch (extended_op)
6342                 {
6343                 case DW_LNE_end_sequence:
6344                   end_sequence = 1;
6345                   lh->file_names[file - 1].included_p = 1;
6346                   if (!decode_for_pst_p)
6347                     record_line (current_subfile, 0, address);
6348                   break;
6349                 case DW_LNE_set_address:
6350                   address = read_address (abfd, line_ptr, cu, &bytes_read);
6351                   line_ptr += bytes_read;
6352                   address += baseaddr;
6353                   break;
6354                 case DW_LNE_define_file:
6355                   {
6356                     char *cur_file;
6357                     unsigned int dir_index, mod_time, length;
6358                     
6359                     cur_file = read_string (abfd, line_ptr, &bytes_read);
6360                     line_ptr += bytes_read;
6361                     dir_index =
6362                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6363                     line_ptr += bytes_read;
6364                     mod_time =
6365                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6366                     line_ptr += bytes_read;
6367                     length =
6368                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6369                     line_ptr += bytes_read;
6370                     add_file_name (lh, cur_file, dir_index, mod_time, length);
6371                   }
6372                   break;
6373                 default:
6374                   complaint (&symfile_complaints,
6375                              "mangled .debug_line section");
6376                   return;
6377                 }
6378               break;
6379             case DW_LNS_copy:
6380               lh->file_names[file - 1].included_p = 1;
6381               if (!decode_for_pst_p)
6382                 record_line (current_subfile, line, 
6383                              check_cu_functions (address, cu));
6384               basic_block = 0;
6385               break;
6386             case DW_LNS_advance_pc:
6387               address += lh->minimum_instruction_length
6388                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6389               line_ptr += bytes_read;
6390               break;
6391             case DW_LNS_advance_line:
6392               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
6393               line_ptr += bytes_read;
6394               break;
6395             case DW_LNS_set_file:
6396               {
6397                 /* lh->include_dirs and lh->file_names are 0-based,
6398                    but the directory and file name numbers in the
6399                    statement program are 1-based.  */
6400                 struct file_entry *fe;
6401                 char *dir;
6402                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6403                 line_ptr += bytes_read;
6404                 fe = &lh->file_names[file - 1];
6405                 if (fe->dir_index)
6406                   dir = lh->include_dirs[fe->dir_index - 1];
6407                 else
6408                   dir = comp_dir;
6409                 if (!decode_for_pst_p)
6410                   dwarf2_start_subfile (fe->name, dir);
6411               }
6412               break;
6413             case DW_LNS_set_column:
6414               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6415               line_ptr += bytes_read;
6416               break;
6417             case DW_LNS_negate_stmt:
6418               is_stmt = (!is_stmt);
6419               break;
6420             case DW_LNS_set_basic_block:
6421               basic_block = 1;
6422               break;
6423             /* Add to the address register of the state machine the
6424                address increment value corresponding to special opcode
6425                255.  Ie, this value is scaled by the minimum instruction
6426                length since special opcode 255 would have scaled the
6427                the increment.  */
6428             case DW_LNS_const_add_pc:
6429               address += (lh->minimum_instruction_length
6430                           * ((255 - lh->opcode_base) / lh->line_range));
6431               break;
6432             case DW_LNS_fixed_advance_pc:
6433               address += read_2_bytes (abfd, line_ptr);
6434               line_ptr += 2;
6435               break;
6436             default:
6437               {  /* Unknown standard opcode, ignore it.  */
6438                 int i;
6439                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
6440                   {
6441                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6442                     line_ptr += bytes_read;
6443                   }
6444               }
6445             }
6446         }
6447     }
6448
6449   if (decode_for_pst_p)
6450     {
6451       int file_index;
6452
6453       /* Now that we're done scanning the Line Header Program, we can
6454          create the psymtab of each included file.  */
6455       for (file_index = 0; file_index < lh->num_file_names; file_index++)
6456         if (lh->file_names[file_index].included_p == 1)
6457           {
6458             char *include_name = lh->file_names [file_index].name;
6459     
6460             if (strcmp (include_name, pst->filename) != 0)
6461               dwarf2_create_include_psymtab (include_name, pst, objfile);
6462           }
6463     }
6464 }
6465
6466 /* Start a subfile for DWARF.  FILENAME is the name of the file and
6467    DIRNAME the name of the source directory which contains FILENAME
6468    or NULL if not known.
6469    This routine tries to keep line numbers from identical absolute and
6470    relative file names in a common subfile.
6471
6472    Using the `list' example from the GDB testsuite, which resides in
6473    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
6474    of /srcdir/list0.c yields the following debugging information for list0.c:
6475
6476    DW_AT_name:          /srcdir/list0.c
6477    DW_AT_comp_dir:              /compdir
6478    files.files[0].name: list0.h
6479    files.files[0].dir:  /srcdir
6480    files.files[1].name: list0.c
6481    files.files[1].dir:  /srcdir
6482
6483    The line number information for list0.c has to end up in a single
6484    subfile, so that `break /srcdir/list0.c:1' works as expected.  */
6485
6486 static void
6487 dwarf2_start_subfile (char *filename, char *dirname)
6488 {
6489   /* If the filename isn't absolute, try to match an existing subfile
6490      with the full pathname.  */
6491
6492   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
6493     {
6494       struct subfile *subfile;
6495       char *fullname = concat (dirname, "/", filename, NULL);
6496
6497       for (subfile = subfiles; subfile; subfile = subfile->next)
6498         {
6499           if (FILENAME_CMP (subfile->name, fullname) == 0)
6500             {
6501               current_subfile = subfile;
6502               xfree (fullname);
6503               return;
6504             }
6505         }
6506       xfree (fullname);
6507     }
6508   start_subfile (filename, dirname);
6509 }
6510
6511 static void
6512 var_decode_location (struct attribute *attr, struct symbol *sym,
6513                      struct dwarf2_cu *cu)
6514 {
6515   struct objfile *objfile = cu->objfile;
6516   struct comp_unit_head *cu_header = &cu->header;
6517
6518   /* NOTE drow/2003-01-30: There used to be a comment and some special
6519      code here to turn a symbol with DW_AT_external and a
6520      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
6521      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
6522      with some versions of binutils) where shared libraries could have
6523      relocations against symbols in their debug information - the
6524      minimal symbol would have the right address, but the debug info
6525      would not.  It's no longer necessary, because we will explicitly
6526      apply relocations when we read in the debug information now.  */
6527
6528   /* A DW_AT_location attribute with no contents indicates that a
6529      variable has been optimized away.  */
6530   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
6531     {
6532       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
6533       return;
6534     }
6535
6536   /* Handle one degenerate form of location expression specially, to
6537      preserve GDB's previous behavior when section offsets are
6538      specified.  If this is just a DW_OP_addr then mark this symbol
6539      as LOC_STATIC.  */
6540
6541   if (attr_form_is_block (attr)
6542       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
6543       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
6544     {
6545       int dummy;
6546
6547       SYMBOL_VALUE_ADDRESS (sym) =
6548         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
6549       fixup_symbol_section (sym, objfile);
6550       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
6551                                               SYMBOL_SECTION (sym));
6552       SYMBOL_CLASS (sym) = LOC_STATIC;
6553       return;
6554     }
6555
6556   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
6557      expression evaluator, and use LOC_COMPUTED only when necessary
6558      (i.e. when the value of a register or memory location is
6559      referenced, or a thread-local block, etc.).  Then again, it might
6560      not be worthwhile.  I'm assuming that it isn't unless performance
6561      or memory numbers show me otherwise.  */
6562
6563   dwarf2_symbol_mark_computed (attr, sym, cu);
6564   SYMBOL_CLASS (sym) = LOC_COMPUTED;
6565 }
6566
6567 /* Given a pointer to a DWARF information entry, figure out if we need
6568    to make a symbol table entry for it, and if so, create a new entry
6569    and return a pointer to it.
6570    If TYPE is NULL, determine symbol type from the die, otherwise
6571    used the passed type.  */
6572
6573 static struct symbol *
6574 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
6575 {
6576   struct objfile *objfile = cu->objfile;
6577   struct symbol *sym = NULL;
6578   char *name;
6579   struct attribute *attr = NULL;
6580   struct attribute *attr2 = NULL;
6581   CORE_ADDR baseaddr;
6582
6583   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6584
6585   if (die->tag != DW_TAG_namespace)
6586     name = dwarf2_linkage_name (die, cu);
6587   else
6588     name = TYPE_NAME (type);
6589
6590   if (name)
6591     {
6592       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
6593                                              sizeof (struct symbol));
6594       OBJSTAT (objfile, n_syms++);
6595       memset (sym, 0, sizeof (struct symbol));
6596
6597       /* Cache this symbol's name and the name's demangled form (if any).  */
6598       SYMBOL_LANGUAGE (sym) = cu->language;
6599       SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
6600
6601       /* Default assumptions.
6602          Use the passed type or decode it from the die.  */
6603       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6604       SYMBOL_CLASS (sym) = LOC_STATIC;
6605       if (type != NULL)
6606         SYMBOL_TYPE (sym) = type;
6607       else
6608         SYMBOL_TYPE (sym) = die_type (die, cu);
6609       attr = dwarf2_attr (die, DW_AT_decl_line, cu);
6610       if (attr)
6611         {
6612           SYMBOL_LINE (sym) = DW_UNSND (attr);
6613         }
6614       switch (die->tag)
6615         {
6616         case DW_TAG_label:
6617           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6618           if (attr)
6619             {
6620               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
6621             }
6622           SYMBOL_CLASS (sym) = LOC_LABEL;
6623           break;
6624         case DW_TAG_subprogram:
6625           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
6626              finish_block.  */
6627           SYMBOL_CLASS (sym) = LOC_BLOCK;
6628           attr2 = dwarf2_attr (die, DW_AT_external, cu);
6629           if (attr2 && (DW_UNSND (attr2) != 0))
6630             {
6631               add_symbol_to_list (sym, &global_symbols);
6632             }
6633           else
6634             {
6635               add_symbol_to_list (sym, cu->list_in_scope);
6636             }
6637           break;
6638         case DW_TAG_variable:
6639           /* Compilation with minimal debug info may result in variables
6640              with missing type entries. Change the misleading `void' type
6641              to something sensible.  */
6642           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
6643             SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
6644                                            TARGET_INT_BIT / HOST_CHAR_BIT, 0,
6645                                            "<variable, no debug info>",
6646                                            objfile);
6647           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6648           if (attr)
6649             {
6650               dwarf2_const_value (attr, sym, cu);
6651               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6652               if (attr2 && (DW_UNSND (attr2) != 0))
6653                 add_symbol_to_list (sym, &global_symbols);
6654               else
6655                 add_symbol_to_list (sym, cu->list_in_scope);
6656               break;
6657             }
6658           attr = dwarf2_attr (die, DW_AT_location, cu);
6659           if (attr)
6660             {
6661               var_decode_location (attr, sym, cu);
6662               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6663               if (attr2 && (DW_UNSND (attr2) != 0))
6664                 add_symbol_to_list (sym, &global_symbols);
6665               else
6666                 add_symbol_to_list (sym, cu->list_in_scope);
6667             }
6668           else
6669             {
6670               /* We do not know the address of this symbol.
6671                  If it is an external symbol and we have type information
6672                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
6673                  The address of the variable will then be determined from
6674                  the minimal symbol table whenever the variable is
6675                  referenced.  */
6676               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6677               if (attr2 && (DW_UNSND (attr2) != 0)
6678                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
6679                 {
6680                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
6681                   add_symbol_to_list (sym, &global_symbols);
6682                 }
6683             }
6684           break;
6685         case DW_TAG_formal_parameter:
6686           attr = dwarf2_attr (die, DW_AT_location, cu);
6687           if (attr)
6688             {
6689               var_decode_location (attr, sym, cu);
6690               /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary?  */
6691               if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
6692                 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
6693             }
6694           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6695           if (attr)
6696             {
6697               dwarf2_const_value (attr, sym, cu);
6698             }
6699           add_symbol_to_list (sym, cu->list_in_scope);
6700           break;
6701         case DW_TAG_unspecified_parameters:
6702           /* From varargs functions; gdb doesn't seem to have any
6703              interest in this information, so just ignore it for now.
6704              (FIXME?) */
6705           break;
6706         case DW_TAG_class_type:
6707         case DW_TAG_structure_type:
6708         case DW_TAG_union_type:
6709         case DW_TAG_enumeration_type:
6710           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6711           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6712
6713           /* Make sure that the symbol includes appropriate enclosing
6714              classes/namespaces in its name.  These are calculated in
6715              read_structure_type, and the correct name is saved in
6716              the type.  */
6717
6718           if (cu->language == language_cplus
6719               || cu->language == language_java)
6720             {
6721               struct type *type = SYMBOL_TYPE (sym);
6722               
6723               if (TYPE_TAG_NAME (type) != NULL)
6724                 {
6725                   /* FIXME: carlton/2003-11-10: Should this use
6726                      SYMBOL_SET_NAMES instead?  (The same problem also
6727                      arises further down in this function.)  */
6728                   /* The type's name is already allocated along with
6729                      this objfile, so we don't need to duplicate it
6730                      for the symbol.  */
6731                   SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
6732                 }
6733             }
6734
6735           {
6736             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
6737                really ever be static objects: otherwise, if you try
6738                to, say, break of a class's method and you're in a file
6739                which doesn't mention that class, it won't work unless
6740                the check for all static symbols in lookup_symbol_aux
6741                saves you.  See the OtherFileClass tests in
6742                gdb.c++/namespace.exp.  */
6743
6744             struct pending **list_to_add;
6745
6746             list_to_add = (cu->list_in_scope == &file_symbols
6747                            && (cu->language == language_cplus
6748                                || cu->language == language_java)
6749                            ? &global_symbols : cu->list_in_scope);
6750           
6751             add_symbol_to_list (sym, list_to_add);
6752
6753             /* The semantics of C++ state that "struct foo { ... }" also
6754                defines a typedef for "foo".  A Java class declaration also
6755                defines a typedef for the class.  Synthesize a typedef symbol
6756                so that "ptype foo" works as expected.  */
6757             if (cu->language == language_cplus
6758                 || cu->language == language_java)
6759               {
6760                 struct symbol *typedef_sym = (struct symbol *)
6761                   obstack_alloc (&objfile->objfile_obstack,
6762                                  sizeof (struct symbol));
6763                 *typedef_sym = *sym;
6764                 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
6765                 /* The symbol's name is already allocated along with
6766                    this objfile, so we don't need to duplicate it for
6767                    the type.  */
6768                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
6769                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NATURAL_NAME (sym);
6770                 add_symbol_to_list (typedef_sym, list_to_add);
6771               }
6772           }
6773           break;
6774         case DW_TAG_typedef:
6775           if (processing_has_namespace_info
6776               && processing_current_prefix[0] != '\0')
6777             {
6778               SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
6779                                                            processing_current_prefix,
6780                                                            name, cu);
6781             }
6782           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6783           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6784           add_symbol_to_list (sym, cu->list_in_scope);
6785           break;
6786         case DW_TAG_base_type:
6787         case DW_TAG_subrange_type:
6788           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6789           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6790           add_symbol_to_list (sym, cu->list_in_scope);
6791           break;
6792         case DW_TAG_enumerator:
6793           if (processing_has_namespace_info
6794               && processing_current_prefix[0] != '\0')
6795             {
6796               SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
6797                                                            processing_current_prefix,
6798                                                            name, cu);
6799             }
6800           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6801           if (attr)
6802             {
6803               dwarf2_const_value (attr, sym, cu);
6804             }
6805           {
6806             /* NOTE: carlton/2003-11-10: See comment above in the
6807                DW_TAG_class_type, etc. block.  */
6808
6809             struct pending **list_to_add;
6810
6811             list_to_add = (cu->list_in_scope == &file_symbols
6812                            && (cu->language == language_cplus
6813                                || cu->language == language_java)
6814                            ? &global_symbols : cu->list_in_scope);
6815           
6816             add_symbol_to_list (sym, list_to_add);
6817           }
6818           break;
6819         case DW_TAG_namespace:
6820           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6821           add_symbol_to_list (sym, &global_symbols);
6822           break;
6823         default:
6824           /* Not a tag we recognize.  Hopefully we aren't processing
6825              trash data, but since we must specifically ignore things
6826              we don't recognize, there is nothing else we should do at
6827              this point. */
6828           complaint (&symfile_complaints, "unsupported tag: '%s'",
6829                      dwarf_tag_name (die->tag));
6830           break;
6831         }
6832     }
6833   return (sym);
6834 }
6835
6836 /* Copy constant value from an attribute to a symbol.  */
6837
6838 static void
6839 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
6840                     struct dwarf2_cu *cu)
6841 {
6842   struct objfile *objfile = cu->objfile;
6843   struct comp_unit_head *cu_header = &cu->header;
6844   struct dwarf_block *blk;
6845
6846   switch (attr->form)
6847     {
6848     case DW_FORM_addr:
6849       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
6850         dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6851                                                       cu_header->addr_size,
6852                                                       TYPE_LENGTH (SYMBOL_TYPE
6853                                                                    (sym)));
6854       SYMBOL_VALUE_BYTES (sym) = (char *)
6855         obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
6856       /* NOTE: cagney/2003-05-09: In-lined store_address call with
6857          it's body - store_unsigned_integer.  */
6858       store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
6859                               DW_ADDR (attr));
6860       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6861       break;
6862     case DW_FORM_block1:
6863     case DW_FORM_block2:
6864     case DW_FORM_block4:
6865     case DW_FORM_block:
6866       blk = DW_BLOCK (attr);
6867       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
6868         dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6869                                                       blk->size,
6870                                                       TYPE_LENGTH (SYMBOL_TYPE
6871                                                                    (sym)));
6872       SYMBOL_VALUE_BYTES (sym) = (char *)
6873         obstack_alloc (&objfile->objfile_obstack, blk->size);
6874       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
6875       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6876       break;
6877
6878       /* The DW_AT_const_value attributes are supposed to carry the
6879          symbol's value "represented as it would be on the target
6880          architecture."  By the time we get here, it's already been
6881          converted to host endianness, so we just need to sign- or
6882          zero-extend it as appropriate.  */
6883     case DW_FORM_data1:
6884       dwarf2_const_value_data (attr, sym, 8);
6885       break;
6886     case DW_FORM_data2:
6887       dwarf2_const_value_data (attr, sym, 16);
6888       break;
6889     case DW_FORM_data4:
6890       dwarf2_const_value_data (attr, sym, 32);
6891       break;
6892     case DW_FORM_data8:
6893       dwarf2_const_value_data (attr, sym, 64);
6894       break;
6895
6896     case DW_FORM_sdata:
6897       SYMBOL_VALUE (sym) = DW_SND (attr);
6898       SYMBOL_CLASS (sym) = LOC_CONST;
6899       break;
6900
6901     case DW_FORM_udata:
6902       SYMBOL_VALUE (sym) = DW_UNSND (attr);
6903       SYMBOL_CLASS (sym) = LOC_CONST;
6904       break;
6905
6906     default:
6907       complaint (&symfile_complaints,
6908                  "unsupported const value attribute form: '%s'",
6909                  dwarf_form_name (attr->form));
6910       SYMBOL_VALUE (sym) = 0;
6911       SYMBOL_CLASS (sym) = LOC_CONST;
6912       break;
6913     }
6914 }
6915
6916
6917 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
6918    or zero-extend it as appropriate for the symbol's type.  */
6919 static void
6920 dwarf2_const_value_data (struct attribute *attr,
6921                          struct symbol *sym,
6922                          int bits)
6923 {
6924   LONGEST l = DW_UNSND (attr);
6925
6926   if (bits < sizeof (l) * 8)
6927     {
6928       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
6929         l &= ((LONGEST) 1 << bits) - 1;
6930       else
6931         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
6932     }
6933
6934   SYMBOL_VALUE (sym) = l;
6935   SYMBOL_CLASS (sym) = LOC_CONST;
6936 }
6937
6938
6939 /* Return the type of the die in question using its DW_AT_type attribute.  */
6940
6941 static struct type *
6942 die_type (struct die_info *die, struct dwarf2_cu *cu)
6943 {
6944   struct type *type;
6945   struct attribute *type_attr;
6946   struct die_info *type_die;
6947   unsigned int ref;
6948
6949   type_attr = dwarf2_attr (die, DW_AT_type, cu);
6950   if (!type_attr)
6951     {
6952       /* A missing DW_AT_type represents a void type.  */
6953       return dwarf2_fundamental_type (cu->objfile, FT_VOID, cu);
6954     }
6955   else
6956     {
6957       ref = dwarf2_get_ref_die_offset (type_attr, cu);
6958       type_die = follow_die_ref (ref);
6959       if (!type_die)
6960         {
6961           error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", 
6962                           ref, cu->objfile->name);
6963           return NULL;
6964         }
6965     }
6966   type = tag_type_to_type (type_die, cu);
6967   if (!type)
6968     {
6969       dump_die (type_die);
6970       error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
6971                       cu->objfile->name);
6972     }
6973   return type;
6974 }
6975
6976 /* Return the containing type of the die in question using its
6977    DW_AT_containing_type attribute.  */
6978
6979 static struct type *
6980 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
6981 {
6982   struct type *type = NULL;
6983   struct attribute *type_attr;
6984   struct die_info *type_die = NULL;
6985   unsigned int ref;
6986
6987   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
6988   if (type_attr)
6989     {
6990       ref = dwarf2_get_ref_die_offset (type_attr, cu);
6991       type_die = follow_die_ref (ref);
6992       if (!type_die)
6993         {
6994           error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref, 
6995                           cu->objfile->name);
6996           return NULL;
6997         }
6998       type = tag_type_to_type (type_die, cu);
6999     }
7000   if (!type)
7001     {
7002       if (type_die)
7003         dump_die (type_die);
7004       error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]", 
7005                       cu->objfile->name);
7006     }
7007   return type;
7008 }
7009
7010 #if 0
7011 static struct type *
7012 type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
7013 {
7014   struct die_info *die;
7015   struct type *type;
7016
7017   die = follow_die_ref (offset);
7018   if (!die)
7019     {
7020       error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
7021       return NULL;
7022     }
7023   type = tag_type_to_type (die, cu);
7024   return type;
7025 }
7026 #endif
7027
7028 static struct type *
7029 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
7030 {
7031   if (die->type)
7032     {
7033       return die->type;
7034     }
7035   else
7036     {
7037       read_type_die (die, cu);
7038       if (!die->type)
7039         {
7040           dump_die (die);
7041           error ("Dwarf Error: Cannot find type of die [in module %s]", 
7042                           cu->objfile->name);
7043         }
7044       return die->type;
7045     }
7046 }
7047
7048 static void
7049 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
7050 {
7051   char *prefix = determine_prefix (die, cu);
7052   const char *old_prefix = processing_current_prefix;
7053   struct cleanup *back_to = make_cleanup (xfree, prefix);
7054   processing_current_prefix = prefix;
7055   
7056   switch (die->tag)
7057     {
7058     case DW_TAG_class_type:
7059     case DW_TAG_structure_type:
7060     case DW_TAG_union_type:
7061       read_structure_type (die, cu);
7062       break;
7063     case DW_TAG_enumeration_type:
7064       read_enumeration_type (die, cu);
7065       break;
7066     case DW_TAG_subprogram:
7067     case DW_TAG_subroutine_type:
7068       read_subroutine_type (die, cu);
7069       break;
7070     case DW_TAG_array_type:
7071       read_array_type (die, cu);
7072       break;
7073     case DW_TAG_pointer_type:
7074       read_tag_pointer_type (die, cu);
7075       break;
7076     case DW_TAG_ptr_to_member_type:
7077       read_tag_ptr_to_member_type (die, cu);
7078       break;
7079     case DW_TAG_reference_type:
7080       read_tag_reference_type (die, cu);
7081       break;
7082     case DW_TAG_const_type:
7083       read_tag_const_type (die, cu);
7084       break;
7085     case DW_TAG_volatile_type:
7086       read_tag_volatile_type (die, cu);
7087       break;
7088     case DW_TAG_string_type:
7089       read_tag_string_type (die, cu);
7090       break;
7091     case DW_TAG_typedef:
7092       read_typedef (die, cu);
7093       break;
7094     case DW_TAG_subrange_type:
7095       read_subrange_type (die, cu);
7096       break;
7097     case DW_TAG_base_type:
7098       read_base_type (die, cu);
7099       break;
7100     default:
7101       complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
7102                  dwarf_tag_name (die->tag));
7103       break;
7104     }
7105
7106   processing_current_prefix = old_prefix;
7107   do_cleanups (back_to);
7108 }
7109
7110 /* Return the name of the namespace/class that DIE is defined within,
7111    or "" if we can't tell.  The caller should xfree the result.  */
7112
7113 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
7114    therein) for an example of how to use this function to deal with
7115    DW_AT_specification.  */
7116
7117 static char *
7118 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
7119 {
7120   struct die_info *parent;
7121
7122   if (cu->language != language_cplus
7123       && cu->language != language_java)
7124     return NULL;
7125
7126   parent = die->parent;
7127
7128   if (parent == NULL)
7129     {
7130       return xstrdup ("");
7131     }
7132   else
7133     {
7134       switch (parent->tag) {
7135       case DW_TAG_namespace:
7136         {
7137           /* FIXME: carlton/2004-03-05: Should I follow extension dies
7138              before doing this check?  */
7139           if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
7140             {
7141               return xstrdup (TYPE_TAG_NAME (parent->type));
7142             }
7143           else
7144             {
7145               int dummy;
7146               char *parent_prefix = determine_prefix (parent, cu);
7147               char *retval = typename_concat (NULL, parent_prefix,
7148                                               namespace_name (parent, &dummy,
7149                                                               cu),
7150                                               cu);
7151               xfree (parent_prefix);
7152               return retval;
7153             }
7154         }
7155         break;
7156       case DW_TAG_class_type:
7157       case DW_TAG_structure_type:
7158         {
7159           if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
7160             {
7161               return xstrdup (TYPE_TAG_NAME (parent->type));
7162             }
7163           else
7164             {
7165               const char *old_prefix = processing_current_prefix;
7166               char *new_prefix = determine_prefix (parent, cu);
7167               char *retval;
7168
7169               processing_current_prefix = new_prefix;
7170               retval = determine_class_name (parent, cu);
7171               processing_current_prefix = old_prefix;
7172
7173               xfree (new_prefix);
7174               return retval;
7175             }
7176         }
7177       default:
7178         return determine_prefix (parent, cu);
7179       }
7180     }
7181 }
7182
7183 /* Return a newly-allocated string formed by concatenating PREFIX and
7184    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
7185    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
7186    perform an obconcat, otherwise allocate storage for the result.  The CU argument
7187    is used to determine the language and hence, the appropriate separator.  */
7188
7189 #define MAX_SEP_LEN 2  /* sizeof ("::")  */
7190
7191 static char *
7192 typename_concat (struct obstack *obs, const char *prefix, const char *suffix, 
7193                  struct dwarf2_cu *cu)
7194 {
7195   char *sep;
7196
7197   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
7198     sep = "";
7199   else if (cu->language == language_java)
7200     sep = ".";
7201   else
7202     sep = "::";
7203
7204   if (obs == NULL)
7205     {
7206       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
7207       retval[0] = '\0';
7208       
7209       if (prefix)
7210         {
7211           strcpy (retval, prefix);
7212           strcat (retval, sep);
7213         }
7214       if (suffix)
7215         strcat (retval, suffix);
7216       
7217       return retval;
7218     }
7219   else
7220     {
7221       /* We have an obstack.  */
7222       return obconcat (obs, prefix, sep, suffix);
7223     }
7224 }
7225
7226 static struct type *
7227 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
7228 {
7229   struct objfile *objfile = cu->objfile;
7230
7231   /* FIXME - this should not produce a new (struct type *)
7232      every time.  It should cache base types.  */
7233   struct type *type;
7234   switch (encoding)
7235     {
7236     case DW_ATE_address:
7237       type = dwarf2_fundamental_type (objfile, FT_VOID, cu);
7238       return type;
7239     case DW_ATE_boolean:
7240       type = dwarf2_fundamental_type (objfile, FT_BOOLEAN, cu);
7241       return type;
7242     case DW_ATE_complex_float:
7243       if (size == 16)
7244         {
7245           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX, cu);
7246         }
7247       else
7248         {
7249           type = dwarf2_fundamental_type (objfile, FT_COMPLEX, cu);
7250         }
7251       return type;
7252     case DW_ATE_float:
7253       if (size == 8)
7254         {
7255           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
7256         }
7257       else
7258         {
7259           type = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
7260         }
7261       return type;
7262     case DW_ATE_signed:
7263       switch (size)
7264         {
7265         case 1:
7266           type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
7267           break;
7268         case 2:
7269           type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT, cu);
7270           break;
7271         default:
7272         case 4:
7273           type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
7274           break;
7275         }
7276       return type;
7277     case DW_ATE_signed_char:
7278       type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
7279       return type;
7280     case DW_ATE_unsigned:
7281       switch (size)
7282         {
7283         case 1:
7284           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
7285           break;
7286         case 2:
7287           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT, cu);
7288           break;
7289         default:
7290         case 4:
7291           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER, cu);
7292           break;
7293         }
7294       return type;
7295     case DW_ATE_unsigned_char:
7296       type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
7297       return type;
7298     default:
7299       type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
7300       return type;
7301     }
7302 }
7303
7304 #if 0
7305 struct die_info *
7306 copy_die (struct die_info *old_die)
7307 {
7308   struct die_info *new_die;
7309   int i, num_attrs;
7310
7311   new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
7312   memset (new_die, 0, sizeof (struct die_info));
7313
7314   new_die->tag = old_die->tag;
7315   new_die->has_children = old_die->has_children;
7316   new_die->abbrev = old_die->abbrev;
7317   new_die->offset = old_die->offset;
7318   new_die->type = NULL;
7319
7320   num_attrs = old_die->num_attrs;
7321   new_die->num_attrs = num_attrs;
7322   new_die->attrs = (struct attribute *)
7323     xmalloc (num_attrs * sizeof (struct attribute));
7324
7325   for (i = 0; i < old_die->num_attrs; ++i)
7326     {
7327       new_die->attrs[i].name = old_die->attrs[i].name;
7328       new_die->attrs[i].form = old_die->attrs[i].form;
7329       new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
7330     }
7331
7332   new_die->next = NULL;
7333   return new_die;
7334 }
7335 #endif
7336
7337 /* Return sibling of die, NULL if no sibling.  */
7338
7339 static struct die_info *
7340 sibling_die (struct die_info *die)
7341 {
7342   return die->sibling;
7343 }
7344
7345 /* Get linkage name of a die, return NULL if not found.  */
7346
7347 static char *
7348 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
7349 {
7350   struct attribute *attr;
7351
7352   attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7353   if (attr && DW_STRING (attr))
7354     return DW_STRING (attr);
7355   attr = dwarf2_attr (die, DW_AT_name, cu);
7356   if (attr && DW_STRING (attr))
7357     return DW_STRING (attr);
7358   return NULL;
7359 }
7360
7361 /* Get name of a die, return NULL if not found.  */
7362
7363 static char *
7364 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
7365 {
7366   struct attribute *attr;
7367
7368   attr = dwarf2_attr (die, DW_AT_name, cu);
7369   if (attr && DW_STRING (attr))
7370     return DW_STRING (attr);
7371   return NULL;
7372 }
7373
7374 /* Return the die that this die in an extension of, or NULL if there
7375    is none.  */
7376
7377 static struct die_info *
7378 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
7379 {
7380   struct attribute *attr;
7381   struct die_info *extension_die;
7382   unsigned int ref;
7383
7384   attr = dwarf2_attr (die, DW_AT_extension, cu);
7385   if (attr == NULL)
7386     return NULL;
7387
7388   ref = dwarf2_get_ref_die_offset (attr, cu);
7389   extension_die = follow_die_ref (ref);
7390   if (!extension_die)
7391     {
7392       error ("Dwarf Error: Cannot find referent at offset %d.", ref);
7393     }
7394
7395   return extension_die;
7396 }
7397
7398 /* Convert a DIE tag into its string name.  */
7399
7400 static char *
7401 dwarf_tag_name (unsigned tag)
7402 {
7403   switch (tag)
7404     {
7405     case DW_TAG_padding:
7406       return "DW_TAG_padding";
7407     case DW_TAG_array_type:
7408       return "DW_TAG_array_type";
7409     case DW_TAG_class_type:
7410       return "DW_TAG_class_type";
7411     case DW_TAG_entry_point:
7412       return "DW_TAG_entry_point";
7413     case DW_TAG_enumeration_type:
7414       return "DW_TAG_enumeration_type";
7415     case DW_TAG_formal_parameter:
7416       return "DW_TAG_formal_parameter";
7417     case DW_TAG_imported_declaration:
7418       return "DW_TAG_imported_declaration";
7419     case DW_TAG_label:
7420       return "DW_TAG_label";
7421     case DW_TAG_lexical_block:
7422       return "DW_TAG_lexical_block";
7423     case DW_TAG_member:
7424       return "DW_TAG_member";
7425     case DW_TAG_pointer_type:
7426       return "DW_TAG_pointer_type";
7427     case DW_TAG_reference_type:
7428       return "DW_TAG_reference_type";
7429     case DW_TAG_compile_unit:
7430       return "DW_TAG_compile_unit";
7431     case DW_TAG_string_type:
7432       return "DW_TAG_string_type";
7433     case DW_TAG_structure_type:
7434       return "DW_TAG_structure_type";
7435     case DW_TAG_subroutine_type:
7436       return "DW_TAG_subroutine_type";
7437     case DW_TAG_typedef:
7438       return "DW_TAG_typedef";
7439     case DW_TAG_union_type:
7440       return "DW_TAG_union_type";
7441     case DW_TAG_unspecified_parameters:
7442       return "DW_TAG_unspecified_parameters";
7443     case DW_TAG_variant:
7444       return "DW_TAG_variant";
7445     case DW_TAG_common_block:
7446       return "DW_TAG_common_block";
7447     case DW_TAG_common_inclusion:
7448       return "DW_TAG_common_inclusion";
7449     case DW_TAG_inheritance:
7450       return "DW_TAG_inheritance";
7451     case DW_TAG_inlined_subroutine:
7452       return "DW_TAG_inlined_subroutine";
7453     case DW_TAG_module:
7454       return "DW_TAG_module";
7455     case DW_TAG_ptr_to_member_type:
7456       return "DW_TAG_ptr_to_member_type";
7457     case DW_TAG_set_type:
7458       return "DW_TAG_set_type";
7459     case DW_TAG_subrange_type:
7460       return "DW_TAG_subrange_type";
7461     case DW_TAG_with_stmt:
7462       return "DW_TAG_with_stmt";
7463     case DW_TAG_access_declaration:
7464       return "DW_TAG_access_declaration";
7465     case DW_TAG_base_type:
7466       return "DW_TAG_base_type";
7467     case DW_TAG_catch_block:
7468       return "DW_TAG_catch_block";
7469     case DW_TAG_const_type:
7470       return "DW_TAG_const_type";
7471     case DW_TAG_constant:
7472       return "DW_TAG_constant";
7473     case DW_TAG_enumerator:
7474       return "DW_TAG_enumerator";
7475     case DW_TAG_file_type:
7476       return "DW_TAG_file_type";
7477     case DW_TAG_friend:
7478       return "DW_TAG_friend";
7479     case DW_TAG_namelist:
7480       return "DW_TAG_namelist";
7481     case DW_TAG_namelist_item:
7482       return "DW_TAG_namelist_item";
7483     case DW_TAG_packed_type:
7484       return "DW_TAG_packed_type";
7485     case DW_TAG_subprogram:
7486       return "DW_TAG_subprogram";
7487     case DW_TAG_template_type_param:
7488       return "DW_TAG_template_type_param";
7489     case DW_TAG_template_value_param:
7490       return "DW_TAG_template_value_param";
7491     case DW_TAG_thrown_type:
7492       return "DW_TAG_thrown_type";
7493     case DW_TAG_try_block:
7494       return "DW_TAG_try_block";
7495     case DW_TAG_variant_part:
7496       return "DW_TAG_variant_part";
7497     case DW_TAG_variable:
7498       return "DW_TAG_variable";
7499     case DW_TAG_volatile_type:
7500       return "DW_TAG_volatile_type";
7501     case DW_TAG_dwarf_procedure:
7502       return "DW_TAG_dwarf_procedure";
7503     case DW_TAG_restrict_type:
7504       return "DW_TAG_restrict_type";
7505     case DW_TAG_interface_type:
7506       return "DW_TAG_interface_type";
7507     case DW_TAG_namespace:
7508       return "DW_TAG_namespace";
7509     case DW_TAG_imported_module:
7510       return "DW_TAG_imported_module";
7511     case DW_TAG_unspecified_type:
7512       return "DW_TAG_unspecified_type";
7513     case DW_TAG_partial_unit:
7514       return "DW_TAG_partial_unit";
7515     case DW_TAG_imported_unit:
7516       return "DW_TAG_imported_unit";
7517     case DW_TAG_MIPS_loop:
7518       return "DW_TAG_MIPS_loop";
7519     case DW_TAG_format_label:
7520       return "DW_TAG_format_label";
7521     case DW_TAG_function_template:
7522       return "DW_TAG_function_template";
7523     case DW_TAG_class_template:
7524       return "DW_TAG_class_template";
7525     default:
7526       return "DW_TAG_<unknown>";
7527     }
7528 }
7529
7530 /* Convert a DWARF attribute code into its string name.  */
7531
7532 static char *
7533 dwarf_attr_name (unsigned attr)
7534 {
7535   switch (attr)
7536     {
7537     case DW_AT_sibling:
7538       return "DW_AT_sibling";
7539     case DW_AT_location:
7540       return "DW_AT_location";
7541     case DW_AT_name:
7542       return "DW_AT_name";
7543     case DW_AT_ordering:
7544       return "DW_AT_ordering";
7545     case DW_AT_subscr_data:
7546       return "DW_AT_subscr_data";
7547     case DW_AT_byte_size:
7548       return "DW_AT_byte_size";
7549     case DW_AT_bit_offset:
7550       return "DW_AT_bit_offset";
7551     case DW_AT_bit_size:
7552       return "DW_AT_bit_size";
7553     case DW_AT_element_list:
7554       return "DW_AT_element_list";
7555     case DW_AT_stmt_list:
7556       return "DW_AT_stmt_list";
7557     case DW_AT_low_pc:
7558       return "DW_AT_low_pc";
7559     case DW_AT_high_pc:
7560       return "DW_AT_high_pc";
7561     case DW_AT_language:
7562       return "DW_AT_language";
7563     case DW_AT_member:
7564       return "DW_AT_member";
7565     case DW_AT_discr:
7566       return "DW_AT_discr";
7567     case DW_AT_discr_value:
7568       return "DW_AT_discr_value";
7569     case DW_AT_visibility:
7570       return "DW_AT_visibility";
7571     case DW_AT_import:
7572       return "DW_AT_import";
7573     case DW_AT_string_length:
7574       return "DW_AT_string_length";
7575     case DW_AT_common_reference:
7576       return "DW_AT_common_reference";
7577     case DW_AT_comp_dir:
7578       return "DW_AT_comp_dir";
7579     case DW_AT_const_value:
7580       return "DW_AT_const_value";
7581     case DW_AT_containing_type:
7582       return "DW_AT_containing_type";
7583     case DW_AT_default_value:
7584       return "DW_AT_default_value";
7585     case DW_AT_inline:
7586       return "DW_AT_inline";
7587     case DW_AT_is_optional:
7588       return "DW_AT_is_optional";
7589     case DW_AT_lower_bound:
7590       return "DW_AT_lower_bound";
7591     case DW_AT_producer:
7592       return "DW_AT_producer";
7593     case DW_AT_prototyped:
7594       return "DW_AT_prototyped";
7595     case DW_AT_return_addr:
7596       return "DW_AT_return_addr";
7597     case DW_AT_start_scope:
7598       return "DW_AT_start_scope";
7599     case DW_AT_stride_size:
7600       return "DW_AT_stride_size";
7601     case DW_AT_upper_bound:
7602       return "DW_AT_upper_bound";
7603     case DW_AT_abstract_origin:
7604       return "DW_AT_abstract_origin";
7605     case DW_AT_accessibility:
7606       return "DW_AT_accessibility";
7607     case DW_AT_address_class:
7608       return "DW_AT_address_class";
7609     case DW_AT_artificial:
7610       return "DW_AT_artificial";
7611     case DW_AT_base_types:
7612       return "DW_AT_base_types";
7613     case DW_AT_calling_convention:
7614       return "DW_AT_calling_convention";
7615     case DW_AT_count:
7616       return "DW_AT_count";
7617     case DW_AT_data_member_location:
7618       return "DW_AT_data_member_location";
7619     case DW_AT_decl_column:
7620       return "DW_AT_decl_column";
7621     case DW_AT_decl_file:
7622       return "DW_AT_decl_file";
7623     case DW_AT_decl_line:
7624       return "DW_AT_decl_line";
7625     case DW_AT_declaration:
7626       return "DW_AT_declaration";
7627     case DW_AT_discr_list:
7628       return "DW_AT_discr_list";
7629     case DW_AT_encoding:
7630       return "DW_AT_encoding";
7631     case DW_AT_external:
7632       return "DW_AT_external";
7633     case DW_AT_frame_base:
7634       return "DW_AT_frame_base";
7635     case DW_AT_friend:
7636       return "DW_AT_friend";
7637     case DW_AT_identifier_case:
7638       return "DW_AT_identifier_case";
7639     case DW_AT_macro_info:
7640       return "DW_AT_macro_info";
7641     case DW_AT_namelist_items:
7642       return "DW_AT_namelist_items";
7643     case DW_AT_priority:
7644       return "DW_AT_priority";
7645     case DW_AT_segment:
7646       return "DW_AT_segment";
7647     case DW_AT_specification:
7648       return "DW_AT_specification";
7649     case DW_AT_static_link:
7650       return "DW_AT_static_link";
7651     case DW_AT_type:
7652       return "DW_AT_type";
7653     case DW_AT_use_location:
7654       return "DW_AT_use_location";
7655     case DW_AT_variable_parameter:
7656       return "DW_AT_variable_parameter";
7657     case DW_AT_virtuality:
7658       return "DW_AT_virtuality";
7659     case DW_AT_vtable_elem_location:
7660       return "DW_AT_vtable_elem_location";
7661     case DW_AT_allocated:
7662       return "DW_AT_allocated";
7663     case DW_AT_associated:
7664       return "DW_AT_associated";
7665     case DW_AT_data_location:
7666       return "DW_AT_data_location";
7667     case DW_AT_stride:
7668       return "DW_AT_stride";
7669     case DW_AT_entry_pc:
7670       return "DW_AT_entry_pc";
7671     case DW_AT_use_UTF8:
7672       return "DW_AT_use_UTF8";
7673     case DW_AT_extension:
7674       return "DW_AT_extension";
7675     case DW_AT_ranges:
7676       return "DW_AT_ranges";
7677     case DW_AT_trampoline:
7678       return "DW_AT_trampoline";
7679     case DW_AT_call_column:
7680       return "DW_AT_call_column";
7681     case DW_AT_call_file:
7682       return "DW_AT_call_file";
7683     case DW_AT_call_line:
7684       return "DW_AT_call_line";
7685 #ifdef MIPS
7686     case DW_AT_MIPS_fde:
7687       return "DW_AT_MIPS_fde";
7688     case DW_AT_MIPS_loop_begin:
7689       return "DW_AT_MIPS_loop_begin";
7690     case DW_AT_MIPS_tail_loop_begin:
7691       return "DW_AT_MIPS_tail_loop_begin";
7692     case DW_AT_MIPS_epilog_begin:
7693       return "DW_AT_MIPS_epilog_begin";
7694     case DW_AT_MIPS_loop_unroll_factor:
7695       return "DW_AT_MIPS_loop_unroll_factor";
7696     case DW_AT_MIPS_software_pipeline_depth:
7697       return "DW_AT_MIPS_software_pipeline_depth";
7698 #endif
7699     case DW_AT_MIPS_linkage_name:
7700       return "DW_AT_MIPS_linkage_name";
7701
7702     case DW_AT_sf_names:
7703       return "DW_AT_sf_names";
7704     case DW_AT_src_info:
7705       return "DW_AT_src_info";
7706     case DW_AT_mac_info:
7707       return "DW_AT_mac_info";
7708     case DW_AT_src_coords:
7709       return "DW_AT_src_coords";
7710     case DW_AT_body_begin:
7711       return "DW_AT_body_begin";
7712     case DW_AT_body_end:
7713       return "DW_AT_body_end";
7714     case DW_AT_GNU_vector:
7715       return "DW_AT_GNU_vector";
7716     default:
7717       return "DW_AT_<unknown>";
7718     }
7719 }
7720
7721 /* Convert a DWARF value form code into its string name.  */
7722
7723 static char *
7724 dwarf_form_name (unsigned form)
7725 {
7726   switch (form)
7727     {
7728     case DW_FORM_addr:
7729       return "DW_FORM_addr";
7730     case DW_FORM_block2:
7731       return "DW_FORM_block2";
7732     case DW_FORM_block4:
7733       return "DW_FORM_block4";
7734     case DW_FORM_data2:
7735       return "DW_FORM_data2";
7736     case DW_FORM_data4:
7737       return "DW_FORM_data4";
7738     case DW_FORM_data8:
7739       return "DW_FORM_data8";
7740     case DW_FORM_string:
7741       return "DW_FORM_string";
7742     case DW_FORM_block:
7743       return "DW_FORM_block";
7744     case DW_FORM_block1:
7745       return "DW_FORM_block1";
7746     case DW_FORM_data1:
7747       return "DW_FORM_data1";
7748     case DW_FORM_flag:
7749       return "DW_FORM_flag";
7750     case DW_FORM_sdata:
7751       return "DW_FORM_sdata";
7752     case DW_FORM_strp:
7753       return "DW_FORM_strp";
7754     case DW_FORM_udata:
7755       return "DW_FORM_udata";
7756     case DW_FORM_ref_addr:
7757       return "DW_FORM_ref_addr";
7758     case DW_FORM_ref1:
7759       return "DW_FORM_ref1";
7760     case DW_FORM_ref2:
7761       return "DW_FORM_ref2";
7762     case DW_FORM_ref4:
7763       return "DW_FORM_ref4";
7764     case DW_FORM_ref8:
7765       return "DW_FORM_ref8";
7766     case DW_FORM_ref_udata:
7767       return "DW_FORM_ref_udata";
7768     case DW_FORM_indirect:
7769       return "DW_FORM_indirect";
7770     default:
7771       return "DW_FORM_<unknown>";
7772     }
7773 }
7774
7775 /* Convert a DWARF stack opcode into its string name.  */
7776
7777 static char *
7778 dwarf_stack_op_name (unsigned op)
7779 {
7780   switch (op)
7781     {
7782     case DW_OP_addr:
7783       return "DW_OP_addr";
7784     case DW_OP_deref:
7785       return "DW_OP_deref";
7786     case DW_OP_const1u:
7787       return "DW_OP_const1u";
7788     case DW_OP_const1s:
7789       return "DW_OP_const1s";
7790     case DW_OP_const2u:
7791       return "DW_OP_const2u";
7792     case DW_OP_const2s:
7793       return "DW_OP_const2s";
7794     case DW_OP_const4u:
7795       return "DW_OP_const4u";
7796     case DW_OP_const4s:
7797       return "DW_OP_const4s";
7798     case DW_OP_const8u:
7799       return "DW_OP_const8u";
7800     case DW_OP_const8s:
7801       return "DW_OP_const8s";
7802     case DW_OP_constu:
7803       return "DW_OP_constu";
7804     case DW_OP_consts:
7805       return "DW_OP_consts";
7806     case DW_OP_dup:
7807       return "DW_OP_dup";
7808     case DW_OP_drop:
7809       return "DW_OP_drop";
7810     case DW_OP_over:
7811       return "DW_OP_over";
7812     case DW_OP_pick:
7813       return "DW_OP_pick";
7814     case DW_OP_swap:
7815       return "DW_OP_swap";
7816     case DW_OP_rot:
7817       return "DW_OP_rot";
7818     case DW_OP_xderef:
7819       return "DW_OP_xderef";
7820     case DW_OP_abs:
7821       return "DW_OP_abs";
7822     case DW_OP_and:
7823       return "DW_OP_and";
7824     case DW_OP_div:
7825       return "DW_OP_div";
7826     case DW_OP_minus:
7827       return "DW_OP_minus";
7828     case DW_OP_mod:
7829       return "DW_OP_mod";
7830     case DW_OP_mul:
7831       return "DW_OP_mul";
7832     case DW_OP_neg:
7833       return "DW_OP_neg";
7834     case DW_OP_not:
7835       return "DW_OP_not";
7836     case DW_OP_or:
7837       return "DW_OP_or";
7838     case DW_OP_plus:
7839       return "DW_OP_plus";
7840     case DW_OP_plus_uconst:
7841       return "DW_OP_plus_uconst";
7842     case DW_OP_shl:
7843       return "DW_OP_shl";
7844     case DW_OP_shr:
7845       return "DW_OP_shr";
7846     case DW_OP_shra:
7847       return "DW_OP_shra";
7848     case DW_OP_xor:
7849       return "DW_OP_xor";
7850     case DW_OP_bra:
7851       return "DW_OP_bra";
7852     case DW_OP_eq:
7853       return "DW_OP_eq";
7854     case DW_OP_ge:
7855       return "DW_OP_ge";
7856     case DW_OP_gt:
7857       return "DW_OP_gt";
7858     case DW_OP_le:
7859       return "DW_OP_le";
7860     case DW_OP_lt:
7861       return "DW_OP_lt";
7862     case DW_OP_ne:
7863       return "DW_OP_ne";
7864     case DW_OP_skip:
7865       return "DW_OP_skip";
7866     case DW_OP_lit0:
7867       return "DW_OP_lit0";
7868     case DW_OP_lit1:
7869       return "DW_OP_lit1";
7870     case DW_OP_lit2:
7871       return "DW_OP_lit2";
7872     case DW_OP_lit3:
7873       return "DW_OP_lit3";
7874     case DW_OP_lit4:
7875       return "DW_OP_lit4";
7876     case DW_OP_lit5:
7877       return "DW_OP_lit5";
7878     case DW_OP_lit6:
7879       return "DW_OP_lit6";
7880     case DW_OP_lit7:
7881       return "DW_OP_lit7";
7882     case DW_OP_lit8:
7883       return "DW_OP_lit8";
7884     case DW_OP_lit9:
7885       return "DW_OP_lit9";
7886     case DW_OP_lit10:
7887       return "DW_OP_lit10";
7888     case DW_OP_lit11:
7889       return "DW_OP_lit11";
7890     case DW_OP_lit12:
7891       return "DW_OP_lit12";
7892     case DW_OP_lit13:
7893       return "DW_OP_lit13";
7894     case DW_OP_lit14:
7895       return "DW_OP_lit14";
7896     case DW_OP_lit15:
7897       return "DW_OP_lit15";
7898     case DW_OP_lit16:
7899       return "DW_OP_lit16";
7900     case DW_OP_lit17:
7901       return "DW_OP_lit17";
7902     case DW_OP_lit18:
7903       return "DW_OP_lit18";
7904     case DW_OP_lit19:
7905       return "DW_OP_lit19";
7906     case DW_OP_lit20:
7907       return "DW_OP_lit20";
7908     case DW_OP_lit21:
7909       return "DW_OP_lit21";
7910     case DW_OP_lit22:
7911       return "DW_OP_lit22";
7912     case DW_OP_lit23:
7913       return "DW_OP_lit23";
7914     case DW_OP_lit24:
7915       return "DW_OP_lit24";
7916     case DW_OP_lit25:
7917       return "DW_OP_lit25";
7918     case DW_OP_lit26:
7919       return "DW_OP_lit26";
7920     case DW_OP_lit27:
7921       return "DW_OP_lit27";
7922     case DW_OP_lit28:
7923       return "DW_OP_lit28";
7924     case DW_OP_lit29:
7925       return "DW_OP_lit29";
7926     case DW_OP_lit30:
7927       return "DW_OP_lit30";
7928     case DW_OP_lit31:
7929       return "DW_OP_lit31";
7930     case DW_OP_reg0:
7931       return "DW_OP_reg0";
7932     case DW_OP_reg1:
7933       return "DW_OP_reg1";
7934     case DW_OP_reg2:
7935       return "DW_OP_reg2";
7936     case DW_OP_reg3:
7937       return "DW_OP_reg3";
7938     case DW_OP_reg4:
7939       return "DW_OP_reg4";
7940     case DW_OP_reg5:
7941       return "DW_OP_reg5";
7942     case DW_OP_reg6:
7943       return "DW_OP_reg6";
7944     case DW_OP_reg7:
7945       return "DW_OP_reg7";
7946     case DW_OP_reg8:
7947       return "DW_OP_reg8";
7948     case DW_OP_reg9:
7949       return "DW_OP_reg9";
7950     case DW_OP_reg10:
7951       return "DW_OP_reg10";
7952     case DW_OP_reg11:
7953       return "DW_OP_reg11";
7954     case DW_OP_reg12:
7955       return "DW_OP_reg12";
7956     case DW_OP_reg13:
7957       return "DW_OP_reg13";
7958     case DW_OP_reg14:
7959       return "DW_OP_reg14";
7960     case DW_OP_reg15:
7961       return "DW_OP_reg15";
7962     case DW_OP_reg16:
7963       return "DW_OP_reg16";
7964     case DW_OP_reg17:
7965       return "DW_OP_reg17";
7966     case DW_OP_reg18:
7967       return "DW_OP_reg18";
7968     case DW_OP_reg19:
7969       return "DW_OP_reg19";
7970     case DW_OP_reg20:
7971       return "DW_OP_reg20";
7972     case DW_OP_reg21:
7973       return "DW_OP_reg21";
7974     case DW_OP_reg22:
7975       return "DW_OP_reg22";
7976     case DW_OP_reg23:
7977       return "DW_OP_reg23";
7978     case DW_OP_reg24:
7979       return "DW_OP_reg24";
7980     case DW_OP_reg25:
7981       return "DW_OP_reg25";
7982     case DW_OP_reg26:
7983       return "DW_OP_reg26";
7984     case DW_OP_reg27:
7985       return "DW_OP_reg27";
7986     case DW_OP_reg28:
7987       return "DW_OP_reg28";
7988     case DW_OP_reg29:
7989       return "DW_OP_reg29";
7990     case DW_OP_reg30:
7991       return "DW_OP_reg30";
7992     case DW_OP_reg31:
7993       return "DW_OP_reg31";
7994     case DW_OP_breg0:
7995       return "DW_OP_breg0";
7996     case DW_OP_breg1:
7997       return "DW_OP_breg1";
7998     case DW_OP_breg2:
7999       return "DW_OP_breg2";
8000     case DW_OP_breg3:
8001       return "DW_OP_breg3";
8002     case DW_OP_breg4:
8003       return "DW_OP_breg4";
8004     case DW_OP_breg5:
8005       return "DW_OP_breg5";
8006     case DW_OP_breg6:
8007       return "DW_OP_breg6";
8008     case DW_OP_breg7:
8009       return "DW_OP_breg7";
8010     case DW_OP_breg8:
8011       return "DW_OP_breg8";
8012     case DW_OP_breg9:
8013       return "DW_OP_breg9";
8014     case DW_OP_breg10:
8015       return "DW_OP_breg10";
8016     case DW_OP_breg11:
8017       return "DW_OP_breg11";
8018     case DW_OP_breg12:
8019       return "DW_OP_breg12";
8020     case DW_OP_breg13:
8021       return "DW_OP_breg13";
8022     case DW_OP_breg14:
8023       return "DW_OP_breg14";
8024     case DW_OP_breg15:
8025       return "DW_OP_breg15";
8026     case DW_OP_breg16:
8027       return "DW_OP_breg16";
8028     case DW_OP_breg17:
8029       return "DW_OP_breg17";
8030     case DW_OP_breg18:
8031       return "DW_OP_breg18";
8032     case DW_OP_breg19:
8033       return "DW_OP_breg19";
8034     case DW_OP_breg20:
8035       return "DW_OP_breg20";
8036     case DW_OP_breg21:
8037       return "DW_OP_breg21";
8038     case DW_OP_breg22:
8039       return "DW_OP_breg22";
8040     case DW_OP_breg23:
8041       return "DW_OP_breg23";
8042     case DW_OP_breg24:
8043       return "DW_OP_breg24";
8044     case DW_OP_breg25:
8045       return "DW_OP_breg25";
8046     case DW_OP_breg26:
8047       return "DW_OP_breg26";
8048     case DW_OP_breg27:
8049       return "DW_OP_breg27";
8050     case DW_OP_breg28:
8051       return "DW_OP_breg28";
8052     case DW_OP_breg29:
8053       return "DW_OP_breg29";
8054     case DW_OP_breg30:
8055       return "DW_OP_breg30";
8056     case DW_OP_breg31:
8057       return "DW_OP_breg31";
8058     case DW_OP_regx:
8059       return "DW_OP_regx";
8060     case DW_OP_fbreg:
8061       return "DW_OP_fbreg";
8062     case DW_OP_bregx:
8063       return "DW_OP_bregx";
8064     case DW_OP_piece:
8065       return "DW_OP_piece";
8066     case DW_OP_deref_size:
8067       return "DW_OP_deref_size";
8068     case DW_OP_xderef_size:
8069       return "DW_OP_xderef_size";
8070     case DW_OP_nop:
8071       return "DW_OP_nop";
8072       /* DWARF 3 extensions.  */
8073     case DW_OP_push_object_address:
8074       return "DW_OP_push_object_address";
8075     case DW_OP_call2:
8076       return "DW_OP_call2";
8077     case DW_OP_call4:
8078       return "DW_OP_call4";
8079     case DW_OP_call_ref:
8080       return "DW_OP_call_ref";
8081       /* GNU extensions.  */
8082     case DW_OP_GNU_push_tls_address:
8083       return "DW_OP_GNU_push_tls_address";
8084     default:
8085       return "OP_<unknown>";
8086     }
8087 }
8088
8089 static char *
8090 dwarf_bool_name (unsigned mybool)
8091 {
8092   if (mybool)
8093     return "TRUE";
8094   else
8095     return "FALSE";
8096 }
8097
8098 /* Convert a DWARF type code into its string name.  */
8099
8100 static char *
8101 dwarf_type_encoding_name (unsigned enc)
8102 {
8103   switch (enc)
8104     {
8105     case DW_ATE_address:
8106       return "DW_ATE_address";
8107     case DW_ATE_boolean:
8108       return "DW_ATE_boolean";
8109     case DW_ATE_complex_float:
8110       return "DW_ATE_complex_float";
8111     case DW_ATE_float:
8112       return "DW_ATE_float";
8113     case DW_ATE_signed:
8114       return "DW_ATE_signed";
8115     case DW_ATE_signed_char:
8116       return "DW_ATE_signed_char";
8117     case DW_ATE_unsigned:
8118       return "DW_ATE_unsigned";
8119     case DW_ATE_unsigned_char:
8120       return "DW_ATE_unsigned_char";
8121     case DW_ATE_imaginary_float:
8122       return "DW_ATE_imaginary_float";
8123     default:
8124       return "DW_ATE_<unknown>";
8125     }
8126 }
8127
8128 /* Convert a DWARF call frame info operation to its string name. */
8129
8130 #if 0
8131 static char *
8132 dwarf_cfi_name (unsigned cfi_opc)
8133 {
8134   switch (cfi_opc)
8135     {
8136     case DW_CFA_advance_loc:
8137       return "DW_CFA_advance_loc";
8138     case DW_CFA_offset:
8139       return "DW_CFA_offset";
8140     case DW_CFA_restore:
8141       return "DW_CFA_restore";
8142     case DW_CFA_nop:
8143       return "DW_CFA_nop";
8144     case DW_CFA_set_loc:
8145       return "DW_CFA_set_loc";
8146     case DW_CFA_advance_loc1:
8147       return "DW_CFA_advance_loc1";
8148     case DW_CFA_advance_loc2:
8149       return "DW_CFA_advance_loc2";
8150     case DW_CFA_advance_loc4:
8151       return "DW_CFA_advance_loc4";
8152     case DW_CFA_offset_extended:
8153       return "DW_CFA_offset_extended";
8154     case DW_CFA_restore_extended:
8155       return "DW_CFA_restore_extended";
8156     case DW_CFA_undefined:
8157       return "DW_CFA_undefined";
8158     case DW_CFA_same_value:
8159       return "DW_CFA_same_value";
8160     case DW_CFA_register:
8161       return "DW_CFA_register";
8162     case DW_CFA_remember_state:
8163       return "DW_CFA_remember_state";
8164     case DW_CFA_restore_state:
8165       return "DW_CFA_restore_state";
8166     case DW_CFA_def_cfa:
8167       return "DW_CFA_def_cfa";
8168     case DW_CFA_def_cfa_register:
8169       return "DW_CFA_def_cfa_register";
8170     case DW_CFA_def_cfa_offset:
8171       return "DW_CFA_def_cfa_offset";
8172
8173     /* DWARF 3 */
8174     case DW_CFA_def_cfa_expression:
8175       return "DW_CFA_def_cfa_expression";
8176     case DW_CFA_expression:
8177       return "DW_CFA_expression";
8178     case DW_CFA_offset_extended_sf:
8179       return "DW_CFA_offset_extended_sf";
8180     case DW_CFA_def_cfa_sf:
8181       return "DW_CFA_def_cfa_sf";
8182     case DW_CFA_def_cfa_offset_sf:
8183       return "DW_CFA_def_cfa_offset_sf";
8184
8185       /* SGI/MIPS specific */
8186     case DW_CFA_MIPS_advance_loc8:
8187       return "DW_CFA_MIPS_advance_loc8";
8188
8189     /* GNU extensions */
8190     case DW_CFA_GNU_window_save:
8191       return "DW_CFA_GNU_window_save";
8192     case DW_CFA_GNU_args_size:
8193       return "DW_CFA_GNU_args_size";
8194     case DW_CFA_GNU_negative_offset_extended:
8195       return "DW_CFA_GNU_negative_offset_extended";
8196
8197     default:
8198       return "DW_CFA_<unknown>";
8199     }
8200 }
8201 #endif
8202
8203 static void
8204 dump_die (struct die_info *die)
8205 {
8206   unsigned int i;
8207
8208   fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
8209            dwarf_tag_name (die->tag), die->abbrev, die->offset);
8210   fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
8211            dwarf_bool_name (die->child != NULL));
8212
8213   fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
8214   for (i = 0; i < die->num_attrs; ++i)
8215     {
8216       fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
8217                dwarf_attr_name (die->attrs[i].name),
8218                dwarf_form_name (die->attrs[i].form));
8219       switch (die->attrs[i].form)
8220         {
8221         case DW_FORM_ref_addr:
8222         case DW_FORM_addr:
8223           fprintf_unfiltered (gdb_stderr, "address: ");
8224           print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
8225           break;
8226         case DW_FORM_block2:
8227         case DW_FORM_block4:
8228         case DW_FORM_block:
8229         case DW_FORM_block1:
8230           fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
8231           break;
8232         case DW_FORM_data1:
8233         case DW_FORM_data2:
8234         case DW_FORM_data4:
8235         case DW_FORM_data8:
8236         case DW_FORM_ref1:
8237         case DW_FORM_ref2:
8238         case DW_FORM_ref4:
8239         case DW_FORM_udata:
8240         case DW_FORM_sdata:
8241           fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
8242           break;
8243         case DW_FORM_string:
8244         case DW_FORM_strp:
8245           fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
8246                    DW_STRING (&die->attrs[i])
8247                    ? DW_STRING (&die->attrs[i]) : "");
8248           break;
8249         case DW_FORM_flag:
8250           if (DW_UNSND (&die->attrs[i]))
8251             fprintf_unfiltered (gdb_stderr, "flag: TRUE");
8252           else
8253             fprintf_unfiltered (gdb_stderr, "flag: FALSE");
8254           break;
8255         case DW_FORM_indirect:
8256           /* the reader will have reduced the indirect form to
8257              the "base form" so this form should not occur */
8258           fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
8259           break;
8260         default:
8261           fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
8262                    die->attrs[i].form);
8263         }
8264       fprintf_unfiltered (gdb_stderr, "\n");
8265     }
8266 }
8267
8268 static void
8269 dump_die_list (struct die_info *die)
8270 {
8271   while (die)
8272     {
8273       dump_die (die);
8274       if (die->child != NULL)
8275         dump_die_list (die->child);
8276       if (die->sibling != NULL)
8277         dump_die_list (die->sibling);
8278     }
8279 }
8280
8281 static void
8282 store_in_ref_table (unsigned int offset, struct die_info *die)
8283 {
8284   int h;
8285   struct die_info *old;
8286
8287   h = (offset % REF_HASH_SIZE);
8288   old = die_ref_table[h];
8289   die->next_ref = old;
8290   die_ref_table[h] = die;
8291 }
8292
8293
8294 static void
8295 dwarf2_empty_hash_tables (void)
8296 {
8297   memset (die_ref_table, 0, sizeof (die_ref_table));
8298 }
8299
8300 static unsigned int
8301 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
8302 {
8303   unsigned int result = 0;
8304
8305   switch (attr->form)
8306     {
8307     case DW_FORM_ref_addr:
8308       result = DW_ADDR (attr);
8309       break;
8310     case DW_FORM_ref1:
8311     case DW_FORM_ref2:
8312     case DW_FORM_ref4:
8313     case DW_FORM_ref8:
8314     case DW_FORM_ref_udata:
8315       result = cu->header.offset + DW_UNSND (attr);
8316       break;
8317     default:
8318       complaint (&symfile_complaints,
8319                  "unsupported die ref attribute form: '%s'",
8320                  dwarf_form_name (attr->form));
8321     }
8322   return result;
8323 }
8324
8325 /* Return the constant value held by the given attribute.  Return -1
8326    if the value held by the attribute is not constant.  */
8327
8328 static int
8329 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
8330 {
8331   if (attr->form == DW_FORM_sdata)
8332     return DW_SND (attr);
8333   else if (attr->form == DW_FORM_udata
8334            || attr->form == DW_FORM_data1
8335            || attr->form == DW_FORM_data2
8336            || attr->form == DW_FORM_data4
8337            || attr->form == DW_FORM_data8)
8338     return DW_UNSND (attr);
8339   else
8340     {
8341       complaint (&symfile_complaints, "Attribute value is not a constant (%s)",
8342                  dwarf_form_name (attr->form));
8343       return default_value;
8344     }
8345 }
8346
8347 static struct die_info *
8348 follow_die_ref (unsigned int offset)
8349 {
8350   struct die_info *die;
8351   int h;
8352
8353   h = (offset % REF_HASH_SIZE);
8354   die = die_ref_table[h];
8355   while (die)
8356     {
8357       if (die->offset == offset)
8358         {
8359           return die;
8360         }
8361       die = die->next_ref;
8362     }
8363   return NULL;
8364 }
8365
8366 static struct type *
8367 dwarf2_fundamental_type (struct objfile *objfile, int typeid,
8368                          struct dwarf2_cu *cu)
8369 {
8370   if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
8371     {
8372       error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
8373              typeid, objfile->name);
8374     }
8375
8376   /* Look for this particular type in the fundamental type vector.  If
8377      one is not found, create and install one appropriate for the
8378      current language and the current target machine. */
8379
8380   if (cu->ftypes[typeid] == NULL)
8381     {
8382       cu->ftypes[typeid] = cu->language_defn->la_fund_type (objfile, typeid);
8383     }
8384
8385   return (cu->ftypes[typeid]);
8386 }
8387
8388 /* Decode simple location descriptions.
8389    Given a pointer to a dwarf block that defines a location, compute
8390    the location and return the value.
8391
8392    NOTE drow/2003-11-18: This function is called in two situations
8393    now: for the address of static or global variables (partial symbols
8394    only) and for offsets into structures which are expected to be
8395    (more or less) constant.  The partial symbol case should go away,
8396    and only the constant case should remain.  That will let this
8397    function complain more accurately.  A few special modes are allowed
8398    without complaint for global variables (for instance, global
8399    register values and thread-local values).
8400
8401    A location description containing no operations indicates that the
8402    object is optimized out.  The return value is 0 for that case.
8403    FIXME drow/2003-11-16: No callers check for this case any more; soon all
8404    callers will only want a very basic result and this can become a
8405    complaint.
8406
8407    When the result is a register number, the global isreg flag is set,
8408    otherwise it is cleared.
8409
8410    Note that stack[0] is unused except as a default error return.
8411    Note that stack overflow is not yet handled.  */
8412
8413 static CORE_ADDR
8414 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
8415 {
8416   struct objfile *objfile = cu->objfile;
8417   struct comp_unit_head *cu_header = &cu->header;
8418   int i;
8419   int size = blk->size;
8420   char *data = blk->data;
8421   CORE_ADDR stack[64];
8422   int stacki;
8423   unsigned int bytes_read, unsnd;
8424   unsigned char op;
8425
8426   i = 0;
8427   stacki = 0;
8428   stack[stacki] = 0;
8429   isreg = 0;
8430
8431   while (i < size)
8432     {
8433       op = data[i++];
8434       switch (op)
8435         {
8436         case DW_OP_lit0:
8437         case DW_OP_lit1:
8438         case DW_OP_lit2:
8439         case DW_OP_lit3:
8440         case DW_OP_lit4:
8441         case DW_OP_lit5:
8442         case DW_OP_lit6:
8443         case DW_OP_lit7:
8444         case DW_OP_lit8:
8445         case DW_OP_lit9:
8446         case DW_OP_lit10:
8447         case DW_OP_lit11:
8448         case DW_OP_lit12:
8449         case DW_OP_lit13:
8450         case DW_OP_lit14:
8451         case DW_OP_lit15:
8452         case DW_OP_lit16:
8453         case DW_OP_lit17:
8454         case DW_OP_lit18:
8455         case DW_OP_lit19:
8456         case DW_OP_lit20:
8457         case DW_OP_lit21:
8458         case DW_OP_lit22:
8459         case DW_OP_lit23:
8460         case DW_OP_lit24:
8461         case DW_OP_lit25:
8462         case DW_OP_lit26:
8463         case DW_OP_lit27:
8464         case DW_OP_lit28:
8465         case DW_OP_lit29:
8466         case DW_OP_lit30:
8467         case DW_OP_lit31:
8468           stack[++stacki] = op - DW_OP_lit0;
8469           break;
8470
8471         case DW_OP_reg0:
8472         case DW_OP_reg1:
8473         case DW_OP_reg2:
8474         case DW_OP_reg3:
8475         case DW_OP_reg4:
8476         case DW_OP_reg5:
8477         case DW_OP_reg6:
8478         case DW_OP_reg7:
8479         case DW_OP_reg8:
8480         case DW_OP_reg9:
8481         case DW_OP_reg10:
8482         case DW_OP_reg11:
8483         case DW_OP_reg12:
8484         case DW_OP_reg13:
8485         case DW_OP_reg14:
8486         case DW_OP_reg15:
8487         case DW_OP_reg16:
8488         case DW_OP_reg17:
8489         case DW_OP_reg18:
8490         case DW_OP_reg19:
8491         case DW_OP_reg20:
8492         case DW_OP_reg21:
8493         case DW_OP_reg22:
8494         case DW_OP_reg23:
8495         case DW_OP_reg24:
8496         case DW_OP_reg25:
8497         case DW_OP_reg26:
8498         case DW_OP_reg27:
8499         case DW_OP_reg28:
8500         case DW_OP_reg29:
8501         case DW_OP_reg30:
8502         case DW_OP_reg31:
8503           isreg = 1;
8504           stack[++stacki] = op - DW_OP_reg0;
8505           if (i < size)
8506             dwarf2_complex_location_expr_complaint ();
8507           break;
8508
8509         case DW_OP_regx:
8510           isreg = 1;
8511           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8512           i += bytes_read;
8513           stack[++stacki] = unsnd;
8514           if (i < size)
8515             dwarf2_complex_location_expr_complaint ();
8516           break;
8517
8518         case DW_OP_addr:
8519           stack[++stacki] = read_address (objfile->obfd, &data[i],
8520                                           cu, &bytes_read);
8521           i += bytes_read;
8522           break;
8523
8524         case DW_OP_const1u:
8525           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
8526           i += 1;
8527           break;
8528
8529         case DW_OP_const1s:
8530           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
8531           i += 1;
8532           break;
8533
8534         case DW_OP_const2u:
8535           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
8536           i += 2;
8537           break;
8538
8539         case DW_OP_const2s:
8540           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
8541           i += 2;
8542           break;
8543
8544         case DW_OP_const4u:
8545           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
8546           i += 4;
8547           break;
8548
8549         case DW_OP_const4s:
8550           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
8551           i += 4;
8552           break;
8553
8554         case DW_OP_constu:
8555           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
8556                                                   &bytes_read);
8557           i += bytes_read;
8558           break;
8559
8560         case DW_OP_consts:
8561           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
8562           i += bytes_read;
8563           break;
8564
8565         case DW_OP_dup:
8566           stack[stacki + 1] = stack[stacki];
8567           stacki++;
8568           break;
8569
8570         case DW_OP_plus:
8571           stack[stacki - 1] += stack[stacki];
8572           stacki--;
8573           break;
8574
8575         case DW_OP_plus_uconst:
8576           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8577           i += bytes_read;
8578           break;
8579
8580         case DW_OP_minus:
8581           stack[stacki - 1] -= stack[stacki];
8582           stacki--;
8583           break;
8584
8585         case DW_OP_deref:
8586           /* If we're not the last op, then we definitely can't encode
8587              this using GDB's address_class enum.  This is valid for partial
8588              global symbols, although the variable's address will be bogus
8589              in the psymtab.  */
8590           if (i < size)
8591             dwarf2_complex_location_expr_complaint ();
8592           break;
8593
8594         case DW_OP_GNU_push_tls_address:
8595           /* The top of the stack has the offset from the beginning
8596              of the thread control block at which the variable is located.  */
8597           /* Nothing should follow this operator, so the top of stack would
8598              be returned.  */
8599           /* This is valid for partial global symbols, but the variable's
8600              address will be bogus in the psymtab.  */
8601           if (i < size)
8602             dwarf2_complex_location_expr_complaint ();
8603           break;
8604
8605         default:
8606           complaint (&symfile_complaints, "unsupported stack op: '%s'",
8607                      dwarf_stack_op_name (op));
8608           return (stack[stacki]);
8609         }
8610     }
8611   return (stack[stacki]);
8612 }
8613
8614 /* memory allocation interface */
8615
8616 static struct dwarf_block *
8617 dwarf_alloc_block (struct dwarf2_cu *cu)
8618 {
8619   struct dwarf_block *blk;
8620
8621   blk = (struct dwarf_block *)
8622     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
8623   return (blk);
8624 }
8625
8626 static struct abbrev_info *
8627 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
8628 {
8629   struct abbrev_info *abbrev;
8630
8631   abbrev = (struct abbrev_info *)
8632     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
8633   memset (abbrev, 0, sizeof (struct abbrev_info));
8634   return (abbrev);
8635 }
8636
8637 static struct die_info *
8638 dwarf_alloc_die (void)
8639 {
8640   struct die_info *die;
8641
8642   die = (struct die_info *) xmalloc (sizeof (struct die_info));
8643   memset (die, 0, sizeof (struct die_info));
8644   return (die);
8645 }
8646
8647 \f
8648 /* Macro support.  */
8649
8650
8651 /* Return the full name of file number I in *LH's file name table.
8652    Use COMP_DIR as the name of the current directory of the
8653    compilation.  The result is allocated using xmalloc; the caller is
8654    responsible for freeing it.  */
8655 static char *
8656 file_full_name (int file, struct line_header *lh, const char *comp_dir)
8657 {
8658   struct file_entry *fe = &lh->file_names[file - 1];
8659   
8660   if (IS_ABSOLUTE_PATH (fe->name))
8661     return xstrdup (fe->name);
8662   else
8663     {
8664       const char *dir;
8665       int dir_len;
8666       char *full_name;
8667
8668       if (fe->dir_index)
8669         dir = lh->include_dirs[fe->dir_index - 1];
8670       else
8671         dir = comp_dir;
8672
8673       if (dir)
8674         {
8675           dir_len = strlen (dir);
8676           full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
8677           strcpy (full_name, dir);
8678           full_name[dir_len] = '/';
8679           strcpy (full_name + dir_len + 1, fe->name);
8680           return full_name;
8681         }
8682       else
8683         return xstrdup (fe->name);
8684     }
8685 }
8686
8687
8688 static struct macro_source_file *
8689 macro_start_file (int file, int line,
8690                   struct macro_source_file *current_file,
8691                   const char *comp_dir,
8692                   struct line_header *lh, struct objfile *objfile)
8693 {
8694   /* The full name of this source file.  */
8695   char *full_name = file_full_name (file, lh, comp_dir);
8696
8697   /* We don't create a macro table for this compilation unit
8698      at all until we actually get a filename.  */
8699   if (! pending_macros)
8700     pending_macros = new_macro_table (&objfile->objfile_obstack,
8701                                       objfile->macro_cache);
8702
8703   if (! current_file)
8704     /* If we have no current file, then this must be the start_file
8705        directive for the compilation unit's main source file.  */
8706     current_file = macro_set_main (pending_macros, full_name);
8707   else
8708     current_file = macro_include (current_file, line, full_name);
8709
8710   xfree (full_name);
8711               
8712   return current_file;
8713 }
8714
8715
8716 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
8717    followed by a null byte.  */
8718 static char *
8719 copy_string (const char *buf, int len)
8720 {
8721   char *s = xmalloc (len + 1);
8722   memcpy (s, buf, len);
8723   s[len] = '\0';
8724
8725   return s;
8726 }
8727
8728
8729 static const char *
8730 consume_improper_spaces (const char *p, const char *body)
8731 {
8732   if (*p == ' ')
8733     {
8734       complaint (&symfile_complaints,
8735                  "macro definition contains spaces in formal argument list:\n`%s'",
8736                  body);
8737
8738       while (*p == ' ')
8739         p++;
8740     }
8741
8742   return p;
8743 }
8744
8745
8746 static void
8747 parse_macro_definition (struct macro_source_file *file, int line,
8748                         const char *body)
8749 {
8750   const char *p;
8751
8752   /* The body string takes one of two forms.  For object-like macro
8753      definitions, it should be:
8754
8755         <macro name> " " <definition>
8756
8757      For function-like macro definitions, it should be:
8758
8759         <macro name> "() " <definition>
8760      or
8761         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
8762
8763      Spaces may appear only where explicitly indicated, and in the
8764      <definition>.
8765
8766      The Dwarf 2 spec says that an object-like macro's name is always
8767      followed by a space, but versions of GCC around March 2002 omit
8768      the space when the macro's definition is the empty string. 
8769
8770      The Dwarf 2 spec says that there should be no spaces between the
8771      formal arguments in a function-like macro's formal argument list,
8772      but versions of GCC around March 2002 include spaces after the
8773      commas.  */
8774
8775
8776   /* Find the extent of the macro name.  The macro name is terminated
8777      by either a space or null character (for an object-like macro) or
8778      an opening paren (for a function-like macro).  */
8779   for (p = body; *p; p++)
8780     if (*p == ' ' || *p == '(')
8781       break;
8782
8783   if (*p == ' ' || *p == '\0')
8784     {
8785       /* It's an object-like macro.  */
8786       int name_len = p - body;
8787       char *name = copy_string (body, name_len);
8788       const char *replacement;
8789
8790       if (*p == ' ')
8791         replacement = body + name_len + 1;
8792       else
8793         {
8794           dwarf2_macro_malformed_definition_complaint (body);
8795           replacement = body + name_len;
8796         }
8797       
8798       macro_define_object (file, line, name, replacement);
8799
8800       xfree (name);
8801     }
8802   else if (*p == '(')
8803     {
8804       /* It's a function-like macro.  */
8805       char *name = copy_string (body, p - body);
8806       int argc = 0;
8807       int argv_size = 1;
8808       char **argv = xmalloc (argv_size * sizeof (*argv));
8809
8810       p++;
8811
8812       p = consume_improper_spaces (p, body);
8813
8814       /* Parse the formal argument list.  */
8815       while (*p && *p != ')')
8816         {
8817           /* Find the extent of the current argument name.  */
8818           const char *arg_start = p;
8819
8820           while (*p && *p != ',' && *p != ')' && *p != ' ')
8821             p++;
8822
8823           if (! *p || p == arg_start)
8824             dwarf2_macro_malformed_definition_complaint (body);
8825           else
8826             {
8827               /* Make sure argv has room for the new argument.  */
8828               if (argc >= argv_size)
8829                 {
8830                   argv_size *= 2;
8831                   argv = xrealloc (argv, argv_size * sizeof (*argv));
8832                 }
8833
8834               argv[argc++] = copy_string (arg_start, p - arg_start);
8835             }
8836
8837           p = consume_improper_spaces (p, body);
8838
8839           /* Consume the comma, if present.  */
8840           if (*p == ',')
8841             {
8842               p++;
8843
8844               p = consume_improper_spaces (p, body);
8845             }
8846         }
8847
8848       if (*p == ')')
8849         {
8850           p++;
8851
8852           if (*p == ' ')
8853             /* Perfectly formed definition, no complaints.  */
8854             macro_define_function (file, line, name,
8855                                    argc, (const char **) argv, 
8856                                    p + 1);
8857           else if (*p == '\0')
8858             {
8859               /* Complain, but do define it.  */
8860               dwarf2_macro_malformed_definition_complaint (body);
8861               macro_define_function (file, line, name,
8862                                      argc, (const char **) argv, 
8863                                      p);
8864             }
8865           else
8866             /* Just complain.  */
8867             dwarf2_macro_malformed_definition_complaint (body);
8868         }
8869       else
8870         /* Just complain.  */
8871         dwarf2_macro_malformed_definition_complaint (body);
8872
8873       xfree (name);
8874       {
8875         int i;
8876
8877         for (i = 0; i < argc; i++)
8878           xfree (argv[i]);
8879       }
8880       xfree (argv);
8881     }
8882   else
8883     dwarf2_macro_malformed_definition_complaint (body);
8884 }
8885
8886
8887 static void
8888 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
8889                      char *comp_dir, bfd *abfd,
8890                      struct dwarf2_cu *cu)
8891 {
8892   char *mac_ptr, *mac_end;
8893   struct macro_source_file *current_file = 0;
8894
8895   if (dwarf2_per_objfile->macinfo_buffer == NULL)
8896     {
8897       complaint (&symfile_complaints, "missing .debug_macinfo section");
8898       return;
8899     }
8900
8901   mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
8902   mac_end = dwarf2_per_objfile->macinfo_buffer
8903     + dwarf2_per_objfile->macinfo_size;
8904
8905   for (;;)
8906     {
8907       enum dwarf_macinfo_record_type macinfo_type;
8908
8909       /* Do we at least have room for a macinfo type byte?  */
8910       if (mac_ptr >= mac_end)
8911         {
8912           dwarf2_macros_too_long_complaint ();
8913           return;
8914         }
8915
8916       macinfo_type = read_1_byte (abfd, mac_ptr);
8917       mac_ptr++;
8918
8919       switch (macinfo_type)
8920         {
8921           /* A zero macinfo type indicates the end of the macro
8922              information.  */
8923         case 0:
8924           return;
8925
8926         case DW_MACINFO_define:
8927         case DW_MACINFO_undef:
8928           {
8929             int bytes_read;
8930             int line;
8931             char *body;
8932
8933             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8934             mac_ptr += bytes_read;
8935             body = read_string (abfd, mac_ptr, &bytes_read);
8936             mac_ptr += bytes_read;
8937
8938             if (! current_file)
8939               complaint (&symfile_complaints,
8940                          "debug info gives macro %s outside of any file: %s",
8941                          macinfo_type ==
8942                          DW_MACINFO_define ? "definition" : macinfo_type ==
8943                          DW_MACINFO_undef ? "undefinition" :
8944                          "something-or-other", body);
8945             else
8946               {
8947                 if (macinfo_type == DW_MACINFO_define)
8948                   parse_macro_definition (current_file, line, body);
8949                 else if (macinfo_type == DW_MACINFO_undef)
8950                   macro_undef (current_file, line, body);
8951               }
8952           }
8953           break;
8954
8955         case DW_MACINFO_start_file:
8956           {
8957             int bytes_read;
8958             int line, file;
8959
8960             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8961             mac_ptr += bytes_read;
8962             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8963             mac_ptr += bytes_read;
8964
8965             current_file = macro_start_file (file, line,
8966                                              current_file, comp_dir,
8967                                              lh, cu->objfile);
8968           }
8969           break;
8970
8971         case DW_MACINFO_end_file:
8972           if (! current_file)
8973             complaint (&symfile_complaints,
8974                        "macro debug info has an unmatched `close_file' directive");
8975           else
8976             {
8977               current_file = current_file->included_by;
8978               if (! current_file)
8979                 {
8980                   enum dwarf_macinfo_record_type next_type;
8981
8982                   /* GCC circa March 2002 doesn't produce the zero
8983                      type byte marking the end of the compilation
8984                      unit.  Complain if it's not there, but exit no
8985                      matter what.  */
8986
8987                   /* Do we at least have room for a macinfo type byte?  */
8988                   if (mac_ptr >= mac_end)
8989                     {
8990                       dwarf2_macros_too_long_complaint ();
8991                       return;
8992                     }
8993
8994                   /* We don't increment mac_ptr here, so this is just
8995                      a look-ahead.  */
8996                   next_type = read_1_byte (abfd, mac_ptr);
8997                   if (next_type != 0)
8998                     complaint (&symfile_complaints,
8999                                "no terminating 0-type entry for macros in `.debug_macinfo' section");
9000
9001                   return;
9002                 }
9003             }
9004           break;
9005
9006         case DW_MACINFO_vendor_ext:
9007           {
9008             int bytes_read;
9009             int constant;
9010             char *string;
9011
9012             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9013             mac_ptr += bytes_read;
9014             string = read_string (abfd, mac_ptr, &bytes_read);
9015             mac_ptr += bytes_read;
9016
9017             /* We don't recognize any vendor extensions.  */
9018           }
9019           break;
9020         }
9021     }
9022 }
9023
9024 /* Check if the attribute's form is a DW_FORM_block*
9025    if so return true else false. */
9026 static int
9027 attr_form_is_block (struct attribute *attr)
9028 {
9029   return (attr == NULL ? 0 :
9030       attr->form == DW_FORM_block1
9031       || attr->form == DW_FORM_block2
9032       || attr->form == DW_FORM_block4
9033       || attr->form == DW_FORM_block);
9034 }
9035
9036 static void
9037 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
9038                              struct dwarf2_cu *cu)
9039 {
9040   if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
9041     {
9042       struct dwarf2_loclist_baton *baton;
9043
9044       baton = obstack_alloc (&cu->objfile->objfile_obstack,
9045                              sizeof (struct dwarf2_loclist_baton));
9046       baton->objfile = cu->objfile;
9047
9048       /* We don't know how long the location list is, but make sure we
9049          don't run off the edge of the section.  */
9050       baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
9051       baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
9052       baton->base_address = cu->header.base_address;
9053       if (cu->header.base_known == 0)
9054         complaint (&symfile_complaints,
9055                    "Location list used without specifying the CU base address.");
9056
9057       SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
9058       SYMBOL_LOCATION_BATON (sym) = baton;
9059     }
9060   else
9061     {
9062       struct dwarf2_locexpr_baton *baton;
9063
9064       baton = obstack_alloc (&cu->objfile->objfile_obstack,
9065                              sizeof (struct dwarf2_locexpr_baton));
9066       baton->objfile = cu->objfile;
9067
9068       if (attr_form_is_block (attr))
9069         {
9070           /* Note that we're just copying the block's data pointer
9071              here, not the actual data.  We're still pointing into the
9072              info_buffer for SYM's objfile; right now we never release
9073              that buffer, but when we do clean up properly this may
9074              need to change.  */
9075           baton->size = DW_BLOCK (attr)->size;
9076           baton->data = DW_BLOCK (attr)->data;
9077         }
9078       else
9079         {
9080           dwarf2_invalid_attrib_class_complaint ("location description",
9081                                                  SYMBOL_NATURAL_NAME (sym));
9082           baton->size = 0;
9083           baton->data = NULL;
9084         }
9085       
9086       SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
9087       SYMBOL_LOCATION_BATON (sym) = baton;
9088     }
9089 }
9090
9091 /* Locate the compilation unit from CU's objfile which contains the
9092    DIE at OFFSET.  Returns NULL on failure.  */
9093
9094 static struct dwarf2_per_cu_data *
9095 dwarf2_find_containing_comp_unit (unsigned long offset,
9096                                   struct objfile *objfile)
9097 {
9098   struct dwarf2_per_cu_data *this_cu;
9099   int low, high;
9100
9101   if (dwarf2_per_objfile->all_comp_units == NULL)
9102     error ("Dwarf Error: offset 0x%lx points outside this "
9103            "compilation unit [in module %s]",
9104            offset, bfd_get_filename (objfile->obfd));
9105
9106   low = 0;
9107   high = dwarf2_per_objfile->n_comp_units - 1;
9108   while (high > low)
9109     {
9110       int mid = low + (high - low) / 2;
9111       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
9112         high = mid;
9113       else
9114         low = mid + 1;
9115     }
9116   gdb_assert (low == high);
9117   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
9118     {
9119       gdb_assert (low > 0);
9120       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
9121       return dwarf2_per_objfile->all_comp_units[low-1];
9122     }
9123   else
9124     {
9125       this_cu = dwarf2_per_objfile->all_comp_units[low];
9126       if (low == dwarf2_per_objfile->n_comp_units - 1
9127           && offset >= this_cu->offset + this_cu->length)
9128         error ("invalid dwarf2 offset %ld", offset);
9129       gdb_assert (offset < this_cu->offset + this_cu->length);
9130       return this_cu;
9131     }
9132 }
9133
9134 static struct dwarf2_per_cu_data *
9135 dwarf2_find_comp_unit (unsigned long offset, struct objfile *objfile)
9136 {
9137   struct dwarf2_per_cu_data *this_cu;
9138   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9139   if (this_cu->offset != offset)
9140     error ("no compilation unit with offset %ld\n", offset);
9141   return this_cu;
9142 }
9143
9144 /* Release one cached compilation unit, CU.  We unlink it from the tree
9145    of compilation units, but we don't remove it from the read_in_chain;
9146    the caller is responsible for that.  */
9147
9148 static void
9149 free_one_comp_unit (void *data)
9150 {
9151   struct dwarf2_cu *cu = data;
9152
9153   if (cu->per_cu != NULL)
9154     cu->per_cu->cu = NULL;
9155   cu->per_cu = NULL;
9156
9157   obstack_free (&cu->comp_unit_obstack, NULL);
9158
9159   xfree (cu);
9160 }
9161
9162 /* This cleanup function is passed the address of a dwarf2_cu on the stack
9163    when we're finished with it.  We can't free the pointer itself, but be
9164    sure to unlink it from the cache.  Also release any associated storage
9165    and perform cache maintenance.
9166
9167    Only used during partial symbol parsing.  */
9168
9169 static void
9170 free_stack_comp_unit (void *data)
9171 {
9172   struct dwarf2_cu *cu = data;
9173
9174   obstack_free (&cu->comp_unit_obstack, NULL);
9175   cu->partial_dies = NULL;
9176
9177   if (cu->per_cu != NULL)
9178     {
9179       /* This compilation unit is on the stack in our caller, so we
9180          should not xfree it.  Just unlink it.  */
9181       cu->per_cu->cu = NULL;
9182       cu->per_cu = NULL;
9183
9184       /* If we had a per-cu pointer, then we may have other compilation
9185          units loaded, so age them now.  */
9186       age_cached_comp_units ();
9187     }
9188 }
9189
9190 /* Free all cached compilation units.  */
9191
9192 static void
9193 free_cached_comp_units (void *data)
9194 {
9195   struct dwarf2_per_cu_data *per_cu, **last_chain;
9196
9197   per_cu = dwarf2_per_objfile->read_in_chain;
9198   last_chain = &dwarf2_per_objfile->read_in_chain;
9199   while (per_cu != NULL)
9200     {
9201       struct dwarf2_per_cu_data *next_cu;
9202
9203       next_cu = per_cu->cu->read_in_chain;
9204
9205       free_one_comp_unit (per_cu->cu);
9206       *last_chain = next_cu;
9207
9208       per_cu = next_cu;
9209     }
9210 }
9211
9212 /* Increase the age counter on each cached compilation unit, and free
9213    any that are too old.  */
9214
9215 static void
9216 age_cached_comp_units (void)
9217 {
9218   struct dwarf2_per_cu_data *per_cu, **last_chain;
9219
9220   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
9221   per_cu = dwarf2_per_objfile->read_in_chain;
9222   while (per_cu != NULL)
9223     {
9224       per_cu->cu->last_used ++;
9225       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
9226         dwarf2_mark (per_cu->cu);
9227       per_cu = per_cu->cu->read_in_chain;
9228     }
9229
9230   per_cu = dwarf2_per_objfile->read_in_chain;
9231   last_chain = &dwarf2_per_objfile->read_in_chain;
9232   while (per_cu != NULL)
9233     {
9234       struct dwarf2_per_cu_data *next_cu;
9235
9236       next_cu = per_cu->cu->read_in_chain;
9237
9238       if (!per_cu->cu->mark)
9239         {
9240           free_one_comp_unit (per_cu->cu);
9241           *last_chain = next_cu;
9242         }
9243       else
9244         last_chain = &per_cu->cu->read_in_chain;
9245
9246       per_cu = next_cu;
9247     }
9248 }
9249
9250 /* Remove a single compilation unit from the cache.  */
9251
9252 static void
9253 free_one_cached_comp_unit (void *target_cu)
9254 {
9255   struct dwarf2_per_cu_data *per_cu, **last_chain;
9256
9257   per_cu = dwarf2_per_objfile->read_in_chain;
9258   last_chain = &dwarf2_per_objfile->read_in_chain;
9259   while (per_cu != NULL)
9260     {
9261       struct dwarf2_per_cu_data *next_cu;
9262
9263       next_cu = per_cu->cu->read_in_chain;
9264
9265       if (per_cu->cu == target_cu)
9266         {
9267           free_one_comp_unit (per_cu->cu);
9268           *last_chain = next_cu;
9269           break;
9270         }
9271       else
9272         last_chain = &per_cu->cu->read_in_chain;
9273
9274       per_cu = next_cu;
9275     }
9276 }
9277
9278 /* A pair of DIE offset and GDB type pointer.  We store these
9279    in a hash table separate from the DIEs, and preserve them
9280    when the DIEs are flushed out of cache.  */
9281
9282 struct dwarf2_offset_and_type
9283 {
9284   unsigned int offset;
9285   struct type *type;
9286 };
9287
9288 /* Hash function for a dwarf2_offset_and_type.  */
9289
9290 static hashval_t
9291 offset_and_type_hash (const void *item)
9292 {
9293   const struct dwarf2_offset_and_type *ofs = item;
9294   return ofs->offset;
9295 }
9296
9297 /* Equality function for a dwarf2_offset_and_type.  */
9298
9299 static int
9300 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
9301 {
9302   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
9303   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9304   return ofs_lhs->offset == ofs_rhs->offset;
9305 }
9306
9307 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
9308    table if necessary.  */
9309
9310 static void
9311 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
9312 {
9313   struct dwarf2_offset_and_type **slot, ofs;
9314
9315   die->type = type;
9316
9317   if (cu->per_cu == NULL)
9318     return;
9319
9320   if (cu->per_cu->type_hash == NULL)
9321     cu->per_cu->type_hash
9322       = htab_create_alloc_ex (cu->header.length / 24,
9323                               offset_and_type_hash,
9324                               offset_and_type_eq,
9325                               NULL,
9326                               &cu->objfile->objfile_obstack,
9327                               hashtab_obstack_allocate,
9328                               dummy_obstack_deallocate);
9329
9330   ofs.offset = die->offset;
9331   ofs.type = type;
9332   slot = (struct dwarf2_offset_and_type **)
9333     htab_find_slot_with_hash (cu->per_cu->type_hash, &ofs, ofs.offset, INSERT);
9334   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
9335   **slot = ofs;
9336 }
9337
9338 #if 0
9339
9340 /* Find the type for DIE in TYPE_HASH, or return NULL if DIE does not
9341    have a saved type.  */
9342
9343 static struct type *
9344 get_die_type (struct die_info *die, htab_t type_hash)
9345 {
9346   struct dwarf2_offset_and_type *slot, ofs;
9347
9348   ofs.offset = die->offset;
9349   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
9350   if (slot)
9351     return slot->type;
9352   else
9353     return NULL;
9354 }
9355
9356 /* Restore the types of the DIE tree starting at START_DIE from the hash
9357    table saved in CU.  */
9358
9359 static void
9360 reset_die_and_siblings_types (struct die_info *start_die, struct dwarf2_cu *cu)
9361 {
9362   struct die_info *die;
9363
9364   if (cu->per_cu->type_hash == NULL)
9365     return;
9366
9367   for (die = start_die; die != NULL; die = die->sibling)
9368     {
9369       die->type = get_die_type (die, cu->per_cu->type_hash);
9370       if (die->child != NULL)
9371         reset_die_and_siblings_types (die->child, cu);
9372     }
9373 }
9374
9375 #endif
9376
9377 /* Set the mark field in CU and in every other compilation unit in the
9378    cache that we must keep because we are keeping CU.  */
9379
9380 static void
9381 dwarf2_mark (struct dwarf2_cu *cu)
9382 {
9383   if (cu->mark)
9384     return;
9385   cu->mark = 1;
9386 }
9387
9388 static void
9389 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
9390 {
9391   while (per_cu)
9392     {
9393       per_cu->cu->mark = 0;
9394       per_cu = per_cu->cu->read_in_chain;
9395     }
9396 }
9397
9398 /* Allocation function for the libiberty hash table which uses an
9399    obstack.  */
9400
9401 static void *
9402 hashtab_obstack_allocate (void *data, size_t size, size_t count)
9403 {
9404   unsigned int total = size * count;
9405   void *ptr = obstack_alloc ((struct obstack *) data, total);
9406   memset (ptr, 0, total);
9407   return ptr;
9408 }
9409
9410 /* Trivial deallocation function for the libiberty splay tree and hash
9411    table - don't deallocate anything.  Rely on later deletion of the
9412    obstack.  */
9413
9414 static void
9415 dummy_obstack_deallocate (void *object, void *data)
9416 {
9417   return;
9418 }
9419
9420 /* Trivial hash function for partial_die_info: the hash value of a DIE
9421    is its offset in .debug_info for this objfile.  */
9422
9423 static hashval_t
9424 partial_die_hash (const void *item)
9425 {
9426   const struct partial_die_info *part_die = item;
9427   return part_die->offset;
9428 }
9429
9430 /* Trivial comparison function for partial_die_info structures: two DIEs
9431    are equal if they have the same offset.  */
9432
9433 static int
9434 partial_die_eq (const void *item_lhs, const void *item_rhs)
9435 {
9436   const struct partial_die_info *part_die_lhs = item_lhs;
9437   const struct partial_die_info *part_die_rhs = item_rhs;
9438   return part_die_lhs->offset == part_die_rhs->offset;
9439 }
9440
9441 static struct cmd_list_element *set_dwarf2_cmdlist;
9442 static struct cmd_list_element *show_dwarf2_cmdlist;
9443
9444 static void
9445 set_dwarf2_cmd (char *args, int from_tty)
9446 {
9447   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
9448 }
9449
9450 static void
9451 show_dwarf2_cmd (char *args, int from_tty)
9452
9453   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
9454 }
9455
9456 void _initialize_dwarf2_read (void);
9457
9458 void
9459 _initialize_dwarf2_read (void)
9460 {
9461   dwarf2_objfile_data_key = register_objfile_data ();
9462
9463   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd,
9464                   "Set DWARF 2 specific variables.\n"
9465                   "Configure DWARF 2 variables such as the cache size",
9466                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
9467                   0/*allow-unknown*/, &maintenance_set_cmdlist);
9468
9469   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd,
9470                   "Show DWARF 2 specific variables\n"
9471                   "Show DWARF 2 variables such as the cache size",
9472                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
9473                   0/*allow-unknown*/, &maintenance_show_cmdlist);
9474
9475   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
9476                             &dwarf2_max_cache_age,
9477                             "Set the upper bound on the age of cached "
9478                             "dwarf2 compilation units.",
9479                             "Show the upper bound on the age of cached "
9480                             "dwarf2 compilation units.",
9481                             "A higher limit means that cached "
9482                             "compilation units will be stored\n"
9483                             "in memory longer, and more total memory will "
9484                             "be used.  Zero disables\n"
9485                             "caching, which can slow down startup.",
9486                             "The upper bound on the age of cached "
9487                             "dwarf2 compilation units is %d.",
9488                             NULL, NULL, &set_dwarf2_cmdlist,
9489                             &show_dwarf2_cmdlist);
9490 }