* dwarf2read.c (struct dwarf2_per_cu_data): New field type_hash.
[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   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
4614   if (attr)
4615     low = dwarf2_get_attr_constant_value (attr, 0);
4616
4617   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
4618   if (attr)
4619     {       
4620       if (attr->form == DW_FORM_block1)
4621         {
4622           /* GCC encodes arrays with unspecified or dynamic length
4623              with a DW_FORM_block1 attribute.
4624              FIXME: GDB does not yet know how to handle dynamic
4625              arrays properly, treat them as arrays with unspecified
4626              length for now.
4627
4628              FIXME: jimb/2003-09-22: GDB does not really know
4629              how to handle arrays of unspecified length
4630              either; we just represent them as zero-length
4631              arrays.  Choose an appropriate upper bound given
4632              the lower bound we've computed above.  */
4633           high = low - 1;
4634         }
4635       else
4636         high = dwarf2_get_attr_constant_value (attr, 1);
4637     }
4638
4639   range_type = create_range_type (NULL, base_type, low, high);
4640
4641   attr = dwarf2_attr (die, DW_AT_name, cu);
4642   if (attr && DW_STRING (attr))
4643     TYPE_NAME (range_type) = DW_STRING (attr);
4644   
4645   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
4646   if (attr)
4647     TYPE_LENGTH (range_type) = DW_UNSND (attr);
4648
4649   set_die_type (die, range_type, cu);
4650 }
4651   
4652
4653 /* Read a whole compilation unit into a linked list of dies.  */
4654
4655 static struct die_info *
4656 read_comp_unit (char *info_ptr, bfd *abfd, struct dwarf2_cu *cu)
4657 {
4658   /* Reset die reference table; we are
4659      building new ones now.  */
4660   dwarf2_empty_hash_tables ();
4661
4662   return read_die_and_children (info_ptr, abfd, cu, &info_ptr, NULL);
4663 }
4664
4665 /* Read a single die and all its descendents.  Set the die's sibling
4666    field to NULL; set other fields in the die correctly, and set all
4667    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
4668    location of the info_ptr after reading all of those dies.  PARENT
4669    is the parent of the die in question.  */
4670
4671 static struct die_info *
4672 read_die_and_children (char *info_ptr, bfd *abfd,
4673                        struct dwarf2_cu *cu,
4674                        char **new_info_ptr,
4675                        struct die_info *parent)
4676 {
4677   struct die_info *die;
4678   char *cur_ptr;
4679   int has_children;
4680
4681   cur_ptr = read_full_die (&die, abfd, info_ptr, cu, &has_children);
4682   store_in_ref_table (die->offset, die);
4683
4684   if (has_children)
4685     {
4686       die->child = read_die_and_siblings (cur_ptr, abfd, cu,
4687                                           new_info_ptr, die);
4688     }
4689   else
4690     {
4691       die->child = NULL;
4692       *new_info_ptr = cur_ptr;
4693     }
4694
4695   die->sibling = NULL;
4696   die->parent = parent;
4697   return die;
4698 }
4699
4700 /* Read a die, all of its descendents, and all of its siblings; set
4701    all of the fields of all of the dies correctly.  Arguments are as
4702    in read_die_and_children.  */
4703
4704 static struct die_info *
4705 read_die_and_siblings (char *info_ptr, bfd *abfd,
4706                        struct dwarf2_cu *cu,
4707                        char **new_info_ptr,
4708                        struct die_info *parent)
4709 {
4710   struct die_info *first_die, *last_sibling;
4711   char *cur_ptr;
4712
4713   cur_ptr = info_ptr;
4714   first_die = last_sibling = NULL;
4715
4716   while (1)
4717     {
4718       struct die_info *die
4719         = read_die_and_children (cur_ptr, abfd, cu, &cur_ptr, parent);
4720
4721       if (!first_die)
4722         {
4723           first_die = die;
4724         }
4725       else
4726         {
4727           last_sibling->sibling = die;
4728         }
4729
4730       if (die->tag == 0)
4731         {
4732           *new_info_ptr = cur_ptr;
4733           return first_die;
4734         }
4735       else
4736         {
4737           last_sibling = die;
4738         }
4739     }
4740 }
4741
4742 /* Free a linked list of dies.  */
4743
4744 static void
4745 free_die_list (struct die_info *dies)
4746 {
4747   struct die_info *die, *next;
4748
4749   die = dies;
4750   while (die)
4751     {
4752       if (die->child != NULL)
4753         free_die_list (die->child);
4754       next = die->sibling;
4755       xfree (die->attrs);
4756       xfree (die);
4757       die = next;
4758     }
4759 }
4760
4761 static void
4762 do_free_die_list_cleanup (void *dies)
4763 {
4764   free_die_list (dies);
4765 }
4766
4767 static struct cleanup *
4768 make_cleanup_free_die_list (struct die_info *dies)
4769 {
4770   return make_cleanup (do_free_die_list_cleanup, dies);
4771 }
4772
4773
4774 /* Read the contents of the section at OFFSET and of size SIZE from the
4775    object file specified by OBJFILE into the objfile_obstack and return it.  */
4776
4777 char *
4778 dwarf2_read_section (struct objfile *objfile, asection *sectp)
4779 {
4780   bfd *abfd = objfile->obfd;
4781   char *buf, *retbuf;
4782   bfd_size_type size = bfd_get_section_size (sectp);
4783
4784   if (size == 0)
4785     return NULL;
4786
4787   buf = (char *) obstack_alloc (&objfile->objfile_obstack, size);
4788   retbuf
4789     = (char *) symfile_relocate_debug_section (abfd, sectp, (bfd_byte *) buf);
4790   if (retbuf != NULL)
4791     return retbuf;
4792
4793   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
4794       || bfd_bread (buf, size, abfd) != size)
4795     error ("Dwarf Error: Can't read DWARF data from '%s'",
4796            bfd_get_filename (abfd));
4797
4798   return buf;
4799 }
4800
4801 /* In DWARF version 2, the description of the debugging information is
4802    stored in a separate .debug_abbrev section.  Before we read any
4803    dies from a section we read in all abbreviations and install them
4804    in a hash table.  This function also sets flags in CU describing
4805    the data found in the abbrev table.  */
4806
4807 static void
4808 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
4809 {
4810   struct comp_unit_head *cu_header = &cu->header;
4811   char *abbrev_ptr;
4812   struct abbrev_info *cur_abbrev;
4813   unsigned int abbrev_number, bytes_read, abbrev_name;
4814   unsigned int abbrev_form, hash_number;
4815   struct attr_abbrev *cur_attrs;
4816   unsigned int allocated_attrs;
4817
4818   /* Initialize dwarf2 abbrevs */
4819   obstack_init (&cu->abbrev_obstack);
4820   cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
4821                                       (ABBREV_HASH_SIZE
4822                                        * sizeof (struct abbrev_info *)));
4823   memset (cu->dwarf2_abbrevs, 0,
4824           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
4825
4826   abbrev_ptr = dwarf2_per_objfile->abbrev_buffer + cu_header->abbrev_offset;
4827   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4828   abbrev_ptr += bytes_read;
4829
4830   allocated_attrs = ATTR_ALLOC_CHUNK;
4831   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
4832   
4833   /* loop until we reach an abbrev number of 0 */
4834   while (abbrev_number)
4835     {
4836       cur_abbrev = dwarf_alloc_abbrev (cu);
4837
4838       /* read in abbrev header */
4839       cur_abbrev->number = abbrev_number;
4840       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4841       abbrev_ptr += bytes_read;
4842       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
4843       abbrev_ptr += 1;
4844
4845       if (cur_abbrev->tag == DW_TAG_namespace)
4846         cu->has_namespace_info = 1;
4847
4848       /* now read in declarations */
4849       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4850       abbrev_ptr += bytes_read;
4851       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4852       abbrev_ptr += bytes_read;
4853       while (abbrev_name)
4854         {
4855           if (cur_abbrev->num_attrs == allocated_attrs)
4856             {
4857               allocated_attrs += ATTR_ALLOC_CHUNK;
4858               cur_attrs
4859                 = xrealloc (cur_attrs, (allocated_attrs
4860                                         * sizeof (struct attr_abbrev)));
4861             }
4862
4863           /* Record whether this compilation unit might have
4864              inter-compilation-unit references.  If we don't know what form
4865              this attribute will have, then it might potentially be a
4866              DW_FORM_ref_addr, so we conservatively expect inter-CU
4867              references.  */
4868
4869           if (abbrev_form == DW_FORM_ref_addr
4870               || abbrev_form == DW_FORM_indirect)
4871             cu->has_form_ref_addr = 1;
4872
4873           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
4874           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
4875           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4876           abbrev_ptr += bytes_read;
4877           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4878           abbrev_ptr += bytes_read;
4879         }
4880
4881       cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
4882                                          (cur_abbrev->num_attrs
4883                                           * sizeof (struct attr_abbrev)));
4884       memcpy (cur_abbrev->attrs, cur_attrs,
4885               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
4886
4887       hash_number = abbrev_number % ABBREV_HASH_SIZE;
4888       cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
4889       cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
4890
4891       /* Get next abbreviation.
4892          Under Irix6 the abbreviations for a compilation unit are not
4893          always properly terminated with an abbrev number of 0.
4894          Exit loop if we encounter an abbreviation which we have
4895          already read (which means we are about to read the abbreviations
4896          for the next compile unit) or if the end of the abbreviation
4897          table is reached.  */
4898       if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev_buffer)
4899           >= dwarf2_per_objfile->abbrev_size)
4900         break;
4901       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
4902       abbrev_ptr += bytes_read;
4903       if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
4904         break;
4905     }
4906
4907   xfree (cur_attrs);
4908 }
4909
4910 /* Release the memory used by the abbrev table for a compilation unit.  */
4911
4912 static void
4913 dwarf2_free_abbrev_table (void *ptr_to_cu)
4914 {
4915   struct dwarf2_cu *cu = ptr_to_cu;
4916
4917   obstack_free (&cu->abbrev_obstack, NULL);
4918   cu->dwarf2_abbrevs = NULL;
4919 }
4920
4921 /* Lookup an abbrev_info structure in the abbrev hash table.  */
4922
4923 static struct abbrev_info *
4924 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
4925 {
4926   unsigned int hash_number;
4927   struct abbrev_info *abbrev;
4928
4929   hash_number = number % ABBREV_HASH_SIZE;
4930   abbrev = cu->dwarf2_abbrevs[hash_number];
4931
4932   while (abbrev)
4933     {
4934       if (abbrev->number == number)
4935         return abbrev;
4936       else
4937         abbrev = abbrev->next;
4938     }
4939   return NULL;
4940 }
4941
4942 /* Returns nonzero if TAG represents a type that we might generate a partial
4943    symbol for.  */
4944
4945 static int
4946 is_type_tag_for_partial (int tag)
4947 {
4948   switch (tag)
4949     {
4950 #if 0
4951     /* Some types that would be reasonable to generate partial symbols for,
4952        that we don't at present.  */
4953     case DW_TAG_array_type:
4954     case DW_TAG_file_type:
4955     case DW_TAG_ptr_to_member_type:
4956     case DW_TAG_set_type:
4957     case DW_TAG_string_type:
4958     case DW_TAG_subroutine_type:
4959 #endif
4960     case DW_TAG_base_type:
4961     case DW_TAG_class_type:
4962     case DW_TAG_enumeration_type:
4963     case DW_TAG_structure_type:
4964     case DW_TAG_subrange_type:
4965     case DW_TAG_typedef:
4966     case DW_TAG_union_type:
4967       return 1;
4968     default:
4969       return 0;
4970     }
4971 }
4972
4973 /* Load all DIEs that are interesting for partial symbols into memory.  */
4974
4975 static struct partial_die_info *
4976 load_partial_dies (bfd *abfd, char *info_ptr, int building_psymtab,
4977                    struct dwarf2_cu *cu)
4978 {
4979   struct partial_die_info *part_die;
4980   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
4981   struct abbrev_info *abbrev;
4982   unsigned int bytes_read;
4983
4984   int nesting_level = 1;
4985
4986   parent_die = NULL;
4987   last_die = NULL;
4988
4989   cu->partial_dies
4990     = htab_create_alloc_ex (cu->header.length / 12,
4991                             partial_die_hash,
4992                             partial_die_eq,
4993                             NULL,
4994                             &cu->comp_unit_obstack,
4995                             hashtab_obstack_allocate,
4996                             dummy_obstack_deallocate);
4997
4998   part_die = obstack_alloc (&cu->comp_unit_obstack,
4999                             sizeof (struct partial_die_info));
5000
5001   while (1)
5002     {
5003       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5004
5005       /* A NULL abbrev means the end of a series of children.  */
5006       if (abbrev == NULL)
5007         {
5008           if (--nesting_level == 0)
5009             {
5010               /* PART_DIE was probably the last thing allocated on the
5011                  comp_unit_obstack, so we could call obstack_free
5012                  here.  We don't do that because the waste is small,
5013                  and will be cleaned up when we're done with this
5014                  compilation unit.  This way, we're also more robust
5015                  against other users of the comp_unit_obstack.  */
5016               return first_die;
5017             }
5018           info_ptr += bytes_read;
5019           last_die = parent_die;
5020           parent_die = parent_die->die_parent;
5021           continue;
5022         }
5023
5024       /* Check whether this DIE is interesting enough to save.  */
5025       if (!is_type_tag_for_partial (abbrev->tag)
5026           && abbrev->tag != DW_TAG_enumerator
5027           && abbrev->tag != DW_TAG_subprogram
5028           && abbrev->tag != DW_TAG_variable
5029           && abbrev->tag != DW_TAG_namespace)
5030         {
5031           /* Otherwise we skip to the next sibling, if any.  */
5032           info_ptr = skip_one_die (info_ptr + bytes_read, abbrev, cu);
5033           continue;
5034         }
5035
5036       info_ptr = read_partial_die (part_die, abbrev, bytes_read,
5037                                    abfd, info_ptr, cu);
5038
5039       /* This two-pass algorithm for processing partial symbols has a
5040          high cost in cache pressure.  Thus, handle some simple cases
5041          here which cover the majority of C partial symbols.  DIEs
5042          which neither have specification tags in them, nor could have
5043          specification tags elsewhere pointing at them, can simply be
5044          processed and discarded.
5045
5046          This segment is also optional; scan_partial_symbols and
5047          add_partial_symbol will handle these DIEs if we chain
5048          them in normally.  When compilers which do not emit large
5049          quantities of duplicate debug information are more common,
5050          this code can probably be removed.  */
5051
5052       /* Any complete simple types at the top level (pretty much all
5053          of them, for a language without namespaces), can be processed
5054          directly.  */
5055       if (parent_die == NULL
5056           && part_die->has_specification == 0
5057           && part_die->is_declaration == 0
5058           && (part_die->tag == DW_TAG_typedef
5059               || part_die->tag == DW_TAG_base_type
5060               || part_die->tag == DW_TAG_subrange_type))
5061         {
5062           if (building_psymtab && part_die->name != NULL)
5063             add_psymbol_to_list (part_die->name, strlen (part_die->name),
5064                                  VAR_DOMAIN, LOC_TYPEDEF,
5065                                  &cu->objfile->static_psymbols,
5066                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
5067           info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5068           continue;
5069         }
5070
5071       /* If we're at the second level, and we're an enumerator, and
5072          our parent has no specification (meaning possibly lives in a
5073          namespace elsewhere), then we can add the partial symbol now
5074          instead of queueing it.  */
5075       if (part_die->tag == DW_TAG_enumerator
5076           && parent_die != NULL
5077           && parent_die->die_parent == NULL
5078           && parent_die->tag == DW_TAG_enumeration_type
5079           && parent_die->has_specification == 0)
5080         {
5081           if (part_die->name == NULL)
5082             complaint (&symfile_complaints, "malformed enumerator DIE ignored");
5083           else if (building_psymtab)
5084             add_psymbol_to_list (part_die->name, strlen (part_die->name),
5085                                  VAR_DOMAIN, LOC_CONST,
5086                                  (cu->language == language_cplus
5087                                   || cu->language == language_java)
5088                                  ? &cu->objfile->global_psymbols
5089                                  : &cu->objfile->static_psymbols,
5090                                  0, (CORE_ADDR) 0, cu->language, cu->objfile);
5091
5092           info_ptr = locate_pdi_sibling (part_die, info_ptr, abfd, cu);
5093           continue;
5094         }
5095
5096       /* We'll save this DIE so link it in.  */
5097       part_die->die_parent = parent_die;
5098       part_die->die_sibling = NULL;
5099       part_die->die_child = NULL;
5100
5101       if (last_die && last_die == parent_die)
5102         last_die->die_child = part_die;
5103       else if (last_die)
5104         last_die->die_sibling = part_die;
5105
5106       last_die = part_die;
5107
5108       if (first_die == NULL)
5109         first_die = part_die;
5110
5111       /* Maybe add the DIE to the hash table.  Not all DIEs that we
5112          find interesting need to be in the hash table, because we
5113          also have the parent/sibling/child chains; only those that we
5114          might refer to by offset later during partial symbol reading.
5115
5116          For now this means things that might have be the target of a
5117          DW_AT_specification, DW_AT_abstract_origin, or
5118          DW_AT_extension.  DW_AT_extension will refer only to
5119          namespaces; DW_AT_abstract_origin refers to functions (and
5120          many things under the function DIE, but we do not recurse
5121          into function DIEs during partial symbol reading) and
5122          possibly variables as well; DW_AT_specification refers to
5123          declarations.  Declarations ought to have the DW_AT_declaration
5124          flag.  It happens that GCC forgets to put it in sometimes, but
5125          only for functions, not for types.
5126
5127          Adding more things than necessary to the hash table is harmless
5128          except for the performance cost.  Adding too few will result in
5129          internal errors in find_partial_die.  */
5130
5131       if (abbrev->tag == DW_TAG_subprogram
5132           || abbrev->tag == DW_TAG_variable
5133           || abbrev->tag == DW_TAG_namespace
5134           || part_die->is_declaration)
5135         {
5136           void **slot;
5137
5138           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
5139                                            part_die->offset, INSERT);
5140           *slot = part_die;
5141         }
5142
5143       part_die = obstack_alloc (&cu->comp_unit_obstack,
5144                                 sizeof (struct partial_die_info));
5145
5146       /* For some DIEs we want to follow their children (if any).  For C
5147          we have no reason to follow the children of structures; for other
5148          languages we have to, both so that we can get at method physnames
5149          to infer fully qualified class names, and for DW_AT_specification.  */
5150       if (last_die->has_children
5151           && (last_die->tag == DW_TAG_namespace
5152               || last_die->tag == DW_TAG_enumeration_type
5153               || (cu->language != language_c
5154                   && (last_die->tag == DW_TAG_class_type
5155                       || last_die->tag == DW_TAG_structure_type
5156                       || last_die->tag == DW_TAG_union_type))))
5157         {
5158           nesting_level++;
5159           parent_die = last_die;
5160           continue;
5161         }
5162
5163       /* Otherwise we skip to the next sibling, if any.  */
5164       info_ptr = locate_pdi_sibling (last_die, info_ptr, abfd, cu);
5165
5166       /* Back to the top, do it again.  */
5167     }
5168 }
5169
5170 /* Read a minimal amount of information into the minimal die structure.  */
5171
5172 static char *
5173 read_partial_die (struct partial_die_info *part_die,
5174                   struct abbrev_info *abbrev,
5175                   unsigned int abbrev_len, bfd *abfd,
5176                   char *info_ptr, struct dwarf2_cu *cu)
5177 {
5178   unsigned int bytes_read, i;
5179   struct attribute attr;
5180   int has_low_pc_attr = 0;
5181   int has_high_pc_attr = 0;
5182
5183   memset (part_die, 0, sizeof (struct partial_die_info));
5184
5185   part_die->offset = info_ptr - dwarf2_per_objfile->info_buffer;
5186
5187   info_ptr += abbrev_len;
5188
5189   if (abbrev == NULL)
5190     return info_ptr;
5191
5192   part_die->tag = abbrev->tag;
5193   part_die->has_children = abbrev->has_children;
5194
5195   for (i = 0; i < abbrev->num_attrs; ++i)
5196     {
5197       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
5198
5199       /* Store the data if it is of an attribute we want to keep in a
5200          partial symbol table.  */
5201       switch (attr.name)
5202         {
5203         case DW_AT_name:
5204
5205           /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
5206           if (part_die->name == NULL)
5207             part_die->name = DW_STRING (&attr);
5208           break;
5209         case DW_AT_comp_dir:
5210           if (part_die->dirname == NULL)
5211             part_die->dirname = DW_STRING (&attr);
5212           break;
5213         case DW_AT_MIPS_linkage_name:
5214           part_die->name = DW_STRING (&attr);
5215           break;
5216         case DW_AT_low_pc:
5217           has_low_pc_attr = 1;
5218           part_die->lowpc = DW_ADDR (&attr);
5219           break;
5220         case DW_AT_high_pc:
5221           has_high_pc_attr = 1;
5222           part_die->highpc = DW_ADDR (&attr);
5223           break;
5224         case DW_AT_location:
5225           /* Support the .debug_loc offsets */
5226           if (attr_form_is_block (&attr))
5227             {
5228                part_die->locdesc = DW_BLOCK (&attr);
5229             }
5230           else if (attr.form == DW_FORM_data4 || attr.form == DW_FORM_data8)
5231             {
5232               dwarf2_complex_location_expr_complaint ();
5233             }
5234           else
5235             {
5236               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
5237                                                      "partial symbol information");
5238             }
5239           break;
5240         case DW_AT_language:
5241           part_die->language = DW_UNSND (&attr);
5242           break;
5243         case DW_AT_external:
5244           part_die->is_external = DW_UNSND (&attr);
5245           break;
5246         case DW_AT_declaration:
5247           part_die->is_declaration = DW_UNSND (&attr);
5248           break;
5249         case DW_AT_type:
5250           part_die->has_type = 1;
5251           break;
5252         case DW_AT_abstract_origin:
5253         case DW_AT_specification:
5254         case DW_AT_extension:
5255           part_die->has_specification = 1;
5256           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr, cu);
5257           break;
5258         case DW_AT_sibling:
5259           /* Ignore absolute siblings, they might point outside of
5260              the current compile unit.  */
5261           if (attr.form == DW_FORM_ref_addr)
5262             complaint (&symfile_complaints, "ignoring absolute DW_AT_sibling");
5263           else
5264             part_die->sibling = dwarf2_per_objfile->info_buffer
5265               + dwarf2_get_ref_die_offset (&attr, cu);
5266           break;
5267         case DW_AT_stmt_list:
5268           part_die->has_stmt_list = 1;
5269           part_die->line_offset = DW_UNSND (&attr);
5270           break;
5271         default:
5272           break;
5273         }
5274     }
5275
5276   /* When using the GNU linker, .gnu.linkonce. sections are used to
5277      eliminate duplicate copies of functions and vtables and such.
5278      The linker will arbitrarily choose one and discard the others.
5279      The AT_*_pc values for such functions refer to local labels in
5280      these sections.  If the section from that file was discarded, the
5281      labels are not in the output, so the relocs get a value of 0.
5282      If this is a discarded function, mark the pc bounds as invalid,
5283      so that GDB will ignore it.  */
5284   if (has_low_pc_attr && has_high_pc_attr
5285       && part_die->lowpc < part_die->highpc
5286       && (part_die->lowpc != 0
5287           || (bfd_get_file_flags (abfd) & HAS_RELOC)))
5288     part_die->has_pc_info = 1;
5289   return info_ptr;
5290 }
5291
5292 /* Find a cached partial DIE at OFFSET in CU.  */
5293
5294 static struct partial_die_info *
5295 find_partial_die_in_comp_unit (unsigned long offset, struct dwarf2_cu *cu)
5296 {
5297   struct partial_die_info *lookup_die = NULL;
5298   struct partial_die_info part_die;
5299
5300   part_die.offset = offset;
5301   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
5302
5303   if (lookup_die == NULL)
5304     internal_error (__FILE__, __LINE__,
5305                     "could not find partial DIE in cache\n");
5306
5307   return lookup_die;
5308 }
5309
5310 /* Find a partial DIE at OFFSET, which may or may not be in CU.  */
5311
5312 static struct partial_die_info *
5313 find_partial_die (unsigned long offset, struct dwarf2_cu *cu,
5314                   struct dwarf2_cu **target_cu)
5315 {
5316   struct dwarf2_per_cu_data *per_cu;
5317
5318   if (offset >= cu->header.offset
5319       && offset < cu->header.offset + cu->header.length)
5320     {
5321       *target_cu = cu;
5322       return find_partial_die_in_comp_unit (offset, cu);
5323     }
5324
5325   per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5326
5327   /* If this offset isn't pointing into a known compilation unit,
5328      the debug information is probably corrupted.  */
5329   if (per_cu == NULL)
5330     error ("Dwarf Error: could not find partial DIE containing "
5331            "offset 0x%lx [in module %s]",
5332            (long) offset, bfd_get_filename (cu->objfile->obfd));
5333
5334   if (per_cu->cu == NULL)
5335     {
5336       load_comp_unit (per_cu, cu->objfile);
5337       per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5338       dwarf2_per_objfile->read_in_chain = per_cu;
5339     }
5340
5341   per_cu->cu->last_used = 0;
5342   *target_cu = per_cu->cu;
5343   return find_partial_die_in_comp_unit (offset, per_cu->cu);
5344 }
5345
5346 /* Adjust PART_DIE before generating a symbol for it.  This function
5347    may set the is_external flag or change the DIE's name.  */
5348
5349 static void
5350 fixup_partial_die (struct partial_die_info *part_die,
5351                    struct dwarf2_cu *cu)
5352 {
5353   /* If we found a reference attribute and the DIE has no name, try
5354      to find a name in the referred to DIE.  */
5355
5356   if (part_die->name == NULL && part_die->has_specification)
5357     {
5358       struct partial_die_info *spec_die;
5359       struct dwarf2_cu *spec_cu;
5360
5361       spec_die = find_partial_die (part_die->spec_offset, cu, &spec_cu);
5362
5363       fixup_partial_die (spec_die, spec_cu);
5364
5365       if (spec_die->name)
5366         {
5367           part_die->name = spec_die->name;
5368
5369           /* Copy DW_AT_external attribute if it is set.  */
5370           if (spec_die->is_external)
5371             part_die->is_external = spec_die->is_external;
5372         }
5373     }
5374
5375   /* Set default names for some unnamed DIEs.  */
5376   if (part_die->name == NULL && (part_die->tag == DW_TAG_structure_type
5377                                  || part_die->tag == DW_TAG_class_type))
5378     part_die->name = "(anonymous class)";
5379
5380   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
5381     part_die->name = "(anonymous namespace)";
5382
5383   if (part_die->tag == DW_TAG_structure_type
5384       || part_die->tag == DW_TAG_class_type
5385       || part_die->tag == DW_TAG_union_type)
5386     guess_structure_name (part_die, cu);
5387 }
5388
5389 /* Read the die from the .debug_info section buffer.  Set DIEP to
5390    point to a newly allocated die with its information, except for its
5391    child, sibling, and parent fields.  Set HAS_CHILDREN to tell
5392    whether the die has children or not.  */
5393
5394 static char *
5395 read_full_die (struct die_info **diep, bfd *abfd, char *info_ptr,
5396                struct dwarf2_cu *cu, int *has_children)
5397 {
5398   unsigned int abbrev_number, bytes_read, i, offset;
5399   struct abbrev_info *abbrev;
5400   struct die_info *die;
5401
5402   offset = info_ptr - dwarf2_per_objfile->info_buffer;
5403   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5404   info_ptr += bytes_read;
5405   if (!abbrev_number)
5406     {
5407       die = dwarf_alloc_die ();
5408       die->tag = 0;
5409       die->abbrev = abbrev_number;
5410       die->type = NULL;
5411       *diep = die;
5412       *has_children = 0;
5413       return info_ptr;
5414     }
5415
5416   abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5417   if (!abbrev)
5418     {
5419       error ("Dwarf Error: could not find abbrev number %d [in module %s]",
5420              abbrev_number,
5421              bfd_get_filename (abfd));
5422     }
5423   die = dwarf_alloc_die ();
5424   die->offset = offset;
5425   die->tag = abbrev->tag;
5426   die->abbrev = abbrev_number;
5427   die->type = NULL;
5428
5429   die->num_attrs = abbrev->num_attrs;
5430   die->attrs = (struct attribute *)
5431     xmalloc (die->num_attrs * sizeof (struct attribute));
5432
5433   for (i = 0; i < abbrev->num_attrs; ++i)
5434     {
5435       info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
5436                                  abfd, info_ptr, cu);
5437     }
5438
5439   *diep = die;
5440   *has_children = abbrev->has_children;
5441   return info_ptr;
5442 }
5443
5444 /* Read an attribute value described by an attribute form.  */
5445
5446 static char *
5447 read_attribute_value (struct attribute *attr, unsigned form,
5448                       bfd *abfd, char *info_ptr,
5449                       struct dwarf2_cu *cu)
5450 {
5451   struct comp_unit_head *cu_header = &cu->header;
5452   unsigned int bytes_read;
5453   struct dwarf_block *blk;
5454
5455   attr->form = form;
5456   switch (form)
5457     {
5458     case DW_FORM_addr:
5459     case DW_FORM_ref_addr:
5460       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
5461       info_ptr += bytes_read;
5462       break;
5463     case DW_FORM_block2:
5464       blk = dwarf_alloc_block (cu);
5465       blk->size = read_2_bytes (abfd, info_ptr);
5466       info_ptr += 2;
5467       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5468       info_ptr += blk->size;
5469       DW_BLOCK (attr) = blk;
5470       break;
5471     case DW_FORM_block4:
5472       blk = dwarf_alloc_block (cu);
5473       blk->size = read_4_bytes (abfd, info_ptr);
5474       info_ptr += 4;
5475       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5476       info_ptr += blk->size;
5477       DW_BLOCK (attr) = blk;
5478       break;
5479     case DW_FORM_data2:
5480       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
5481       info_ptr += 2;
5482       break;
5483     case DW_FORM_data4:
5484       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
5485       info_ptr += 4;
5486       break;
5487     case DW_FORM_data8:
5488       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
5489       info_ptr += 8;
5490       break;
5491     case DW_FORM_string:
5492       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
5493       info_ptr += bytes_read;
5494       break;
5495     case DW_FORM_strp:
5496       DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
5497                                                &bytes_read);
5498       info_ptr += bytes_read;
5499       break;
5500     case DW_FORM_block:
5501       blk = dwarf_alloc_block (cu);
5502       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5503       info_ptr += bytes_read;
5504       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5505       info_ptr += blk->size;
5506       DW_BLOCK (attr) = blk;
5507       break;
5508     case DW_FORM_block1:
5509       blk = dwarf_alloc_block (cu);
5510       blk->size = read_1_byte (abfd, info_ptr);
5511       info_ptr += 1;
5512       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
5513       info_ptr += blk->size;
5514       DW_BLOCK (attr) = blk;
5515       break;
5516     case DW_FORM_data1:
5517       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5518       info_ptr += 1;
5519       break;
5520     case DW_FORM_flag:
5521       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5522       info_ptr += 1;
5523       break;
5524     case DW_FORM_sdata:
5525       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
5526       info_ptr += bytes_read;
5527       break;
5528     case DW_FORM_udata:
5529       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5530       info_ptr += bytes_read;
5531       break;
5532     case DW_FORM_ref1:
5533       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
5534       info_ptr += 1;
5535       break;
5536     case DW_FORM_ref2:
5537       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
5538       info_ptr += 2;
5539       break;
5540     case DW_FORM_ref4:
5541       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
5542       info_ptr += 4;
5543       break;
5544     case DW_FORM_ref8:
5545       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
5546       info_ptr += 8;
5547       break;
5548     case DW_FORM_ref_udata:
5549       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5550       info_ptr += bytes_read;
5551       break;
5552     case DW_FORM_indirect:
5553       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5554       info_ptr += bytes_read;
5555       info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
5556       break;
5557     default:
5558       error ("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]",
5559              dwarf_form_name (form),
5560              bfd_get_filename (abfd));
5561     }
5562   return info_ptr;
5563 }
5564
5565 /* Read an attribute described by an abbreviated attribute.  */
5566
5567 static char *
5568 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
5569                 bfd *abfd, char *info_ptr, struct dwarf2_cu *cu)
5570 {
5571   attr->name = abbrev->name;
5572   return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
5573 }
5574
5575 /* read dwarf information from a buffer */
5576
5577 static unsigned int
5578 read_1_byte (bfd *abfd, char *buf)
5579 {
5580   return bfd_get_8 (abfd, (bfd_byte *) buf);
5581 }
5582
5583 static int
5584 read_1_signed_byte (bfd *abfd, char *buf)
5585 {
5586   return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
5587 }
5588
5589 static unsigned int
5590 read_2_bytes (bfd *abfd, char *buf)
5591 {
5592   return bfd_get_16 (abfd, (bfd_byte *) buf);
5593 }
5594
5595 static int
5596 read_2_signed_bytes (bfd *abfd, char *buf)
5597 {
5598   return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
5599 }
5600
5601 static unsigned int
5602 read_4_bytes (bfd *abfd, char *buf)
5603 {
5604   return bfd_get_32 (abfd, (bfd_byte *) buf);
5605 }
5606
5607 static int
5608 read_4_signed_bytes (bfd *abfd, char *buf)
5609 {
5610   return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
5611 }
5612
5613 static unsigned long
5614 read_8_bytes (bfd *abfd, char *buf)
5615 {
5616   return bfd_get_64 (abfd, (bfd_byte *) buf);
5617 }
5618
5619 static CORE_ADDR
5620 read_address (bfd *abfd, char *buf, struct dwarf2_cu *cu, int *bytes_read)
5621 {
5622   struct comp_unit_head *cu_header = &cu->header;
5623   CORE_ADDR retval = 0;
5624
5625   if (cu_header->signed_addr_p)
5626     {
5627       switch (cu_header->addr_size)
5628         {
5629         case 2:
5630           retval = bfd_get_signed_16 (abfd, (bfd_byte *) buf);
5631           break;
5632         case 4:
5633           retval = bfd_get_signed_32 (abfd, (bfd_byte *) buf);
5634           break;
5635         case 8:
5636           retval = bfd_get_signed_64 (abfd, (bfd_byte *) buf);
5637           break;
5638         default:
5639           internal_error (__FILE__, __LINE__,
5640                           "read_address: bad switch, signed [in module %s]",
5641                           bfd_get_filename (abfd));
5642         }
5643     }
5644   else
5645     {
5646       switch (cu_header->addr_size)
5647         {
5648         case 2:
5649           retval = bfd_get_16 (abfd, (bfd_byte *) buf);
5650           break;
5651         case 4:
5652           retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5653           break;
5654         case 8:
5655           retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5656           break;
5657         default:
5658           internal_error (__FILE__, __LINE__,
5659                           "read_address: bad switch, unsigned [in module %s]",
5660                           bfd_get_filename (abfd));
5661         }
5662     }
5663
5664   *bytes_read = cu_header->addr_size;
5665   return retval;
5666 }
5667
5668 /* Read the initial length from a section.  The (draft) DWARF 3
5669    specification allows the initial length to take up either 4 bytes
5670    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
5671    bytes describe the length and all offsets will be 8 bytes in length
5672    instead of 4.
5673
5674    An older, non-standard 64-bit format is also handled by this
5675    function.  The older format in question stores the initial length
5676    as an 8-byte quantity without an escape value.  Lengths greater
5677    than 2^32 aren't very common which means that the initial 4 bytes
5678    is almost always zero.  Since a length value of zero doesn't make
5679    sense for the 32-bit format, this initial zero can be considered to
5680    be an escape value which indicates the presence of the older 64-bit
5681    format.  As written, the code can't detect (old format) lengths
5682    greater than 4GB.  If it becomes necessary to handle lengths somewhat
5683    larger than 4GB, we could allow other small values (such as the
5684    non-sensical values of 1, 2, and 3) to also be used as escape values
5685    indicating the presence of the old format.
5686
5687    The value returned via bytes_read should be used to increment
5688    the relevant pointer after calling read_initial_length().
5689    
5690    As a side effect, this function sets the fields initial_length_size
5691    and offset_size in cu_header to the values appropriate for the
5692    length field.  (The format of the initial length field determines
5693    the width of file offsets to be fetched later with fetch_offset().)
5694    
5695    [ Note:  read_initial_length() and read_offset() are based on the
5696      document entitled "DWARF Debugging Information Format", revision
5697      3, draft 8, dated November 19, 2001.  This document was obtained
5698      from:
5699
5700         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
5701      
5702      This document is only a draft and is subject to change.  (So beware.)
5703
5704      Details regarding the older, non-standard 64-bit format were
5705      determined empirically by examining 64-bit ELF files produced
5706      by the SGI toolchain on an IRIX 6.5 machine.
5707
5708      - Kevin, July 16, 2002
5709    ] */
5710
5711 static LONGEST
5712 read_initial_length (bfd *abfd, char *buf, struct comp_unit_head *cu_header,
5713                      int *bytes_read)
5714 {
5715   LONGEST retval = 0;
5716
5717   retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5718
5719   if (retval == 0xffffffff)
5720     {
5721       retval = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
5722       *bytes_read = 12;
5723       if (cu_header != NULL)
5724         {
5725           cu_header->initial_length_size = 12;
5726           cu_header->offset_size = 8;
5727         }
5728     }
5729   else if (retval == 0)
5730     {
5731       /* Handle (non-standard) 64-bit DWARF2 formats such as that used
5732          by IRIX.  */
5733       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5734       *bytes_read = 8;
5735       if (cu_header != NULL)
5736         {
5737           cu_header->initial_length_size = 8;
5738           cu_header->offset_size = 8;
5739         }
5740     }
5741   else
5742     {
5743       *bytes_read = 4;
5744       if (cu_header != NULL)
5745         {
5746           cu_header->initial_length_size = 4;
5747           cu_header->offset_size = 4;
5748         }
5749     }
5750
5751  return retval;
5752 }
5753
5754 /* Read an offset from the data stream.  The size of the offset is
5755    given by cu_header->offset_size. */
5756
5757 static LONGEST
5758 read_offset (bfd *abfd, char *buf, const struct comp_unit_head *cu_header,
5759              int *bytes_read)
5760 {
5761   LONGEST retval = 0;
5762
5763   switch (cu_header->offset_size)
5764     {
5765     case 4:
5766       retval = bfd_get_32 (abfd, (bfd_byte *) buf);
5767       *bytes_read = 4;
5768       break;
5769     case 8:
5770       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
5771       *bytes_read = 8;
5772       break;
5773     default:
5774       internal_error (__FILE__, __LINE__,
5775                       "read_offset: bad switch [in module %s]",
5776                       bfd_get_filename (abfd));
5777     }
5778
5779  return retval;
5780 }
5781
5782 static char *
5783 read_n_bytes (bfd *abfd, char *buf, unsigned int size)
5784 {
5785   /* If the size of a host char is 8 bits, we can return a pointer
5786      to the buffer, otherwise we have to copy the data to a buffer
5787      allocated on the temporary obstack.  */
5788   gdb_assert (HOST_CHAR_BIT == 8);
5789   return buf;
5790 }
5791
5792 static char *
5793 read_string (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5794 {
5795   /* If the size of a host char is 8 bits, we can return a pointer
5796      to the string, otherwise we have to copy the string to a buffer
5797      allocated on the temporary obstack.  */
5798   gdb_assert (HOST_CHAR_BIT == 8);
5799   if (*buf == '\0')
5800     {
5801       *bytes_read_ptr = 1;
5802       return NULL;
5803     }
5804   *bytes_read_ptr = strlen (buf) + 1;
5805   return buf;
5806 }
5807
5808 static char *
5809 read_indirect_string (bfd *abfd, char *buf,
5810                       const struct comp_unit_head *cu_header,
5811                       unsigned int *bytes_read_ptr)
5812 {
5813   LONGEST str_offset = read_offset (abfd, buf, cu_header,
5814                                     (int *) bytes_read_ptr);
5815
5816   if (dwarf2_per_objfile->str_buffer == NULL)
5817     {
5818       error ("DW_FORM_strp used without .debug_str section [in module %s]",
5819                       bfd_get_filename (abfd));
5820       return NULL;
5821     }
5822   if (str_offset >= dwarf2_per_objfile->str_size)
5823     {
5824       error ("DW_FORM_strp pointing outside of .debug_str section [in module %s]",
5825                       bfd_get_filename (abfd));
5826       return NULL;
5827     }
5828   gdb_assert (HOST_CHAR_BIT == 8);
5829   if (dwarf2_per_objfile->str_buffer[str_offset] == '\0')
5830     return NULL;
5831   return dwarf2_per_objfile->str_buffer + str_offset;
5832 }
5833
5834 static unsigned long
5835 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5836 {
5837   unsigned long result;
5838   unsigned int num_read;
5839   int i, shift;
5840   unsigned char byte;
5841
5842   result = 0;
5843   shift = 0;
5844   num_read = 0;
5845   i = 0;
5846   while (1)
5847     {
5848       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5849       buf++;
5850       num_read++;
5851       result |= ((unsigned long)(byte & 127) << shift);
5852       if ((byte & 128) == 0)
5853         {
5854           break;
5855         }
5856       shift += 7;
5857     }
5858   *bytes_read_ptr = num_read;
5859   return result;
5860 }
5861
5862 static long
5863 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
5864 {
5865   long result;
5866   int i, shift, size, num_read;
5867   unsigned char byte;
5868
5869   result = 0;
5870   shift = 0;
5871   size = 32;
5872   num_read = 0;
5873   i = 0;
5874   while (1)
5875     {
5876       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5877       buf++;
5878       num_read++;
5879       result |= ((long)(byte & 127) << shift);
5880       shift += 7;
5881       if ((byte & 128) == 0)
5882         {
5883           break;
5884         }
5885     }
5886   if ((shift < size) && (byte & 0x40))
5887     {
5888       result |= -(1 << shift);
5889     }
5890   *bytes_read_ptr = num_read;
5891   return result;
5892 }
5893
5894 /* Return a pointer to just past the end of an LEB128 number in BUF.  */
5895
5896 static char *
5897 skip_leb128 (bfd *abfd, char *buf)
5898 {
5899   int byte;
5900
5901   while (1)
5902     {
5903       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
5904       buf++;
5905       if ((byte & 128) == 0)
5906         return buf;
5907     }
5908 }
5909
5910 static void
5911 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
5912 {
5913   switch (lang)
5914     {
5915     case DW_LANG_C89:
5916     case DW_LANG_C:
5917       cu->language = language_c;
5918       break;
5919     case DW_LANG_C_plus_plus:
5920       cu->language = language_cplus;
5921       break;
5922     case DW_LANG_Fortran77:
5923     case DW_LANG_Fortran90:
5924     case DW_LANG_Fortran95:
5925       cu->language = language_fortran;
5926       break;
5927     case DW_LANG_Mips_Assembler:
5928       cu->language = language_asm;
5929       break;
5930     case DW_LANG_Java:
5931       cu->language = language_java;
5932       break;
5933     case DW_LANG_Ada83:
5934     case DW_LANG_Ada95:
5935     case DW_LANG_Cobol74:
5936     case DW_LANG_Cobol85:
5937     case DW_LANG_Pascal83:
5938     case DW_LANG_Modula2:
5939     default:
5940       cu->language = language_minimal;
5941       break;
5942     }
5943   cu->language_defn = language_def (cu->language);
5944 }
5945
5946 /* Return the named attribute or NULL if not there.  */
5947
5948 static struct attribute *
5949 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
5950 {
5951   unsigned int i;
5952   struct attribute *spec = NULL;
5953
5954   for (i = 0; i < die->num_attrs; ++i)
5955     {
5956       if (die->attrs[i].name == name)
5957         {
5958           return &die->attrs[i];
5959         }
5960       if (die->attrs[i].name == DW_AT_specification
5961           || die->attrs[i].name == DW_AT_abstract_origin)
5962         spec = &die->attrs[i];
5963     }
5964   if (spec)
5965     {
5966       struct die_info *ref_die =
5967       follow_die_ref (dwarf2_get_ref_die_offset (spec, cu));
5968
5969       if (ref_die)
5970         return dwarf2_attr (ref_die, name, cu);
5971     }
5972
5973   return NULL;
5974 }
5975
5976 /* Return non-zero iff the attribute NAME is defined for the given DIE,
5977    and holds a non-zero value.  This function should only be used for
5978    DW_FORM_flag attributes.  */
5979
5980 static int
5981 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
5982 {
5983   struct attribute *attr = dwarf2_attr (die, name, cu);
5984
5985   return (attr && DW_UNSND (attr));
5986 }
5987
5988 static int
5989 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
5990 {
5991   /* A DIE is a declaration if it has a DW_AT_declaration attribute
5992      which value is non-zero.  However, we have to be careful with
5993      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
5994      (via dwarf2_flag_true_p) follows this attribute.  So we may
5995      end up accidently finding a declaration attribute that belongs
5996      to a different DIE referenced by the specification attribute,
5997      even though the given DIE does not have a declaration attribute.  */
5998   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
5999           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
6000 }
6001
6002 /* Return the die giving the specification for DIE, if there is
6003    one.  */
6004
6005 static struct die_info *
6006 die_specification (struct die_info *die, struct dwarf2_cu *cu)
6007 {
6008   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification, cu);
6009
6010   if (spec_attr == NULL)
6011     return NULL;
6012   else
6013     return follow_die_ref (dwarf2_get_ref_die_offset (spec_attr, cu));
6014 }
6015
6016 /* Free the line_header structure *LH, and any arrays and strings it
6017    refers to.  */
6018 static void
6019 free_line_header (struct line_header *lh)
6020 {
6021   if (lh->standard_opcode_lengths)
6022     xfree (lh->standard_opcode_lengths);
6023
6024   /* Remember that all the lh->file_names[i].name pointers are
6025      pointers into debug_line_buffer, and don't need to be freed.  */
6026   if (lh->file_names)
6027     xfree (lh->file_names);
6028
6029   /* Similarly for the include directory names.  */
6030   if (lh->include_dirs)
6031     xfree (lh->include_dirs);
6032
6033   xfree (lh);
6034 }
6035
6036
6037 /* Add an entry to LH's include directory table.  */
6038 static void
6039 add_include_dir (struct line_header *lh, char *include_dir)
6040 {
6041   /* Grow the array if necessary.  */
6042   if (lh->include_dirs_size == 0)
6043     {
6044       lh->include_dirs_size = 1; /* for testing */
6045       lh->include_dirs = xmalloc (lh->include_dirs_size
6046                                   * sizeof (*lh->include_dirs));
6047     }
6048   else if (lh->num_include_dirs >= lh->include_dirs_size)
6049     {
6050       lh->include_dirs_size *= 2;
6051       lh->include_dirs = xrealloc (lh->include_dirs,
6052                                    (lh->include_dirs_size
6053                                     * sizeof (*lh->include_dirs)));
6054     }
6055
6056   lh->include_dirs[lh->num_include_dirs++] = include_dir;
6057 }
6058  
6059
6060 /* Add an entry to LH's file name table.  */
6061 static void
6062 add_file_name (struct line_header *lh,
6063                char *name,
6064                unsigned int dir_index,
6065                unsigned int mod_time,
6066                unsigned int length)
6067 {
6068   struct file_entry *fe;
6069
6070   /* Grow the array if necessary.  */
6071   if (lh->file_names_size == 0)
6072     {
6073       lh->file_names_size = 1; /* for testing */
6074       lh->file_names = xmalloc (lh->file_names_size
6075                                 * sizeof (*lh->file_names));
6076     }
6077   else if (lh->num_file_names >= lh->file_names_size)
6078     {
6079       lh->file_names_size *= 2;
6080       lh->file_names = xrealloc (lh->file_names,
6081                                  (lh->file_names_size
6082                                   * sizeof (*lh->file_names)));
6083     }
6084
6085   fe = &lh->file_names[lh->num_file_names++];
6086   fe->name = name;
6087   fe->dir_index = dir_index;
6088   fe->mod_time = mod_time;
6089   fe->length = length;
6090   fe->included_p = 0;
6091 }
6092  
6093
6094 /* Read the statement program header starting at OFFSET in
6095    .debug_line, according to the endianness of ABFD.  Return a pointer
6096    to a struct line_header, allocated using xmalloc.
6097
6098    NOTE: the strings in the include directory and file name tables of
6099    the returned object point into debug_line_buffer, and must not be
6100    freed.  */
6101 static struct line_header *
6102 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
6103                           struct dwarf2_cu *cu)
6104 {
6105   struct cleanup *back_to;
6106   struct line_header *lh;
6107   char *line_ptr;
6108   int bytes_read;
6109   int i;
6110   char *cur_dir, *cur_file;
6111
6112   if (dwarf2_per_objfile->line_buffer == NULL)
6113     {
6114       complaint (&symfile_complaints, "missing .debug_line section");
6115       return 0;
6116     }
6117
6118   /* Make sure that at least there's room for the total_length field.  That
6119      could be 12 bytes long, but we're just going to fudge that.  */
6120   if (offset + 4 >= dwarf2_per_objfile->line_size)
6121     {
6122       dwarf2_statement_list_fits_in_line_number_section_complaint ();
6123       return 0;
6124     }
6125
6126   lh = xmalloc (sizeof (*lh));
6127   memset (lh, 0, sizeof (*lh));
6128   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
6129                           (void *) lh);
6130
6131   line_ptr = dwarf2_per_objfile->line_buffer + offset;
6132
6133   /* read in the header */
6134   lh->total_length = read_initial_length (abfd, line_ptr, NULL, &bytes_read);
6135   line_ptr += bytes_read;
6136   if (line_ptr + lh->total_length > (dwarf2_per_objfile->line_buffer
6137                                      + dwarf2_per_objfile->line_size))
6138     {
6139       dwarf2_statement_list_fits_in_line_number_section_complaint ();
6140       return 0;
6141     }
6142   lh->statement_program_end = line_ptr + lh->total_length;
6143   lh->version = read_2_bytes (abfd, line_ptr);
6144   line_ptr += 2;
6145   lh->header_length = read_offset (abfd, line_ptr, &cu->header, &bytes_read);
6146   line_ptr += bytes_read;
6147   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
6148   line_ptr += 1;
6149   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
6150   line_ptr += 1;
6151   lh->line_base = read_1_signed_byte (abfd, line_ptr);
6152   line_ptr += 1;
6153   lh->line_range = read_1_byte (abfd, line_ptr);
6154   line_ptr += 1;
6155   lh->opcode_base = read_1_byte (abfd, line_ptr);
6156   line_ptr += 1;
6157   lh->standard_opcode_lengths
6158     = (unsigned char *) xmalloc (lh->opcode_base * sizeof (unsigned char));
6159
6160   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
6161   for (i = 1; i < lh->opcode_base; ++i)
6162     {
6163       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
6164       line_ptr += 1;
6165     }
6166
6167   /* Read directory table  */
6168   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6169     {
6170       line_ptr += bytes_read;
6171       add_include_dir (lh, cur_dir);
6172     }
6173   line_ptr += bytes_read;
6174
6175   /* Read file name table */
6176   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
6177     {
6178       unsigned int dir_index, mod_time, length;
6179
6180       line_ptr += bytes_read;
6181       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6182       line_ptr += bytes_read;
6183       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6184       line_ptr += bytes_read;
6185       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6186       line_ptr += bytes_read;
6187
6188       add_file_name (lh, cur_file, dir_index, mod_time, length);
6189     }
6190   line_ptr += bytes_read;
6191   lh->statement_program_start = line_ptr; 
6192
6193   if (line_ptr > (dwarf2_per_objfile->line_buffer
6194                   + dwarf2_per_objfile->line_size))
6195     complaint (&symfile_complaints,
6196                "line number info header doesn't fit in `.debug_line' section");
6197
6198   discard_cleanups (back_to);
6199   return lh;
6200 }
6201
6202 /* This function exists to work around a bug in certain compilers
6203    (particularly GCC 2.95), in which the first line number marker of a
6204    function does not show up until after the prologue, right before
6205    the second line number marker.  This function shifts ADDRESS down
6206    to the beginning of the function if necessary, and is called on
6207    addresses passed to record_line.  */
6208
6209 static CORE_ADDR
6210 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
6211 {
6212   struct function_range *fn;
6213
6214   /* Find the function_range containing address.  */
6215   if (!cu->first_fn)
6216     return address;
6217
6218   if (!cu->cached_fn)
6219     cu->cached_fn = cu->first_fn;
6220
6221   fn = cu->cached_fn;
6222   while (fn)
6223     if (fn->lowpc <= address && fn->highpc > address)
6224       goto found;
6225     else
6226       fn = fn->next;
6227
6228   fn = cu->first_fn;
6229   while (fn && fn != cu->cached_fn)
6230     if (fn->lowpc <= address && fn->highpc > address)
6231       goto found;
6232     else
6233       fn = fn->next;
6234
6235   return address;
6236
6237  found:
6238   if (fn->seen_line)
6239     return address;
6240   if (address != fn->lowpc)
6241     complaint (&symfile_complaints,
6242                "misplaced first line number at 0x%lx for '%s'",
6243                (unsigned long) address, fn->name);
6244   fn->seen_line = 1;
6245   return fn->lowpc;
6246 }
6247
6248 /* Decode the Line Number Program (LNP) for the given line_header
6249    structure and CU.  The actual information extracted and the type
6250    of structures created from the LNP depends on the value of PST.
6251
6252    1. If PST is NULL, then this procedure uses the data from the program
6253       to create all necessary symbol tables, and their linetables.
6254       The compilation directory of the file is passed in COMP_DIR,
6255       and must not be NULL.
6256    
6257    2. If PST is not NULL, this procedure reads the program to determine
6258       the list of files included by the unit represented by PST, and
6259       builds all the associated partial symbol tables.  In this case,
6260       the value of COMP_DIR is ignored, and can thus be NULL (the COMP_DIR
6261       is not used to compute the full name of the symtab, and therefore
6262       omitting it when building the partial symtab does not introduce
6263       the potential for inconsistency - a partial symtab and its associated
6264       symbtab having a different fullname -).  */
6265
6266 static void
6267 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
6268                     struct dwarf2_cu *cu, struct partial_symtab *pst)
6269 {
6270   char *line_ptr;
6271   char *line_end;
6272   unsigned int bytes_read;
6273   unsigned char op_code, extended_op, adj_opcode;
6274   CORE_ADDR baseaddr;
6275   struct objfile *objfile = cu->objfile;
6276   const int decode_for_pst_p = (pst != NULL);
6277
6278   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6279
6280   line_ptr = lh->statement_program_start;
6281   line_end = lh->statement_program_end;
6282
6283   /* Read the statement sequences until there's nothing left.  */
6284   while (line_ptr < line_end)
6285     {
6286       /* state machine registers  */
6287       CORE_ADDR address = 0;
6288       unsigned int file = 1;
6289       unsigned int line = 1;
6290       unsigned int column = 0;
6291       int is_stmt = lh->default_is_stmt;
6292       int basic_block = 0;
6293       int end_sequence = 0;
6294
6295       if (!decode_for_pst_p && lh->num_file_names >= file)
6296         {
6297           /* Start a subfile for the current file of the state machine.  */
6298           /* lh->include_dirs and lh->file_names are 0-based, but the
6299              directory and file name numbers in the statement program
6300              are 1-based.  */
6301           struct file_entry *fe = &lh->file_names[file - 1];
6302           char *dir;
6303           if (fe->dir_index)
6304             dir = lh->include_dirs[fe->dir_index - 1];
6305           else
6306             dir = comp_dir;
6307           dwarf2_start_subfile (fe->name, dir);
6308         }
6309
6310       /* Decode the table. */
6311       while (!end_sequence)
6312         {
6313           op_code = read_1_byte (abfd, line_ptr);
6314           line_ptr += 1;
6315
6316           if (op_code >= lh->opcode_base)
6317             {           /* Special operand.  */
6318               adj_opcode = op_code - lh->opcode_base;
6319               address += (adj_opcode / lh->line_range)
6320                 * lh->minimum_instruction_length;
6321               line += lh->line_base + (adj_opcode % lh->line_range);
6322               lh->file_names[file - 1].included_p = 1;
6323               if (!decode_for_pst_p)
6324                 {
6325                   /* append row to matrix using current values */
6326                   record_line (current_subfile, line, 
6327                                check_cu_functions (address, cu));
6328                 }
6329               basic_block = 1;
6330             }
6331           else switch (op_code)
6332             {
6333             case DW_LNS_extended_op:
6334               read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6335               line_ptr += bytes_read;
6336               extended_op = read_1_byte (abfd, line_ptr);
6337               line_ptr += 1;
6338               switch (extended_op)
6339                 {
6340                 case DW_LNE_end_sequence:
6341                   end_sequence = 1;
6342                   lh->file_names[file - 1].included_p = 1;
6343                   if (!decode_for_pst_p)
6344                     record_line (current_subfile, 0, address);
6345                   break;
6346                 case DW_LNE_set_address:
6347                   address = read_address (abfd, line_ptr, cu, &bytes_read);
6348                   line_ptr += bytes_read;
6349                   address += baseaddr;
6350                   break;
6351                 case DW_LNE_define_file:
6352                   {
6353                     char *cur_file;
6354                     unsigned int dir_index, mod_time, length;
6355                     
6356                     cur_file = read_string (abfd, line_ptr, &bytes_read);
6357                     line_ptr += bytes_read;
6358                     dir_index =
6359                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6360                     line_ptr += bytes_read;
6361                     mod_time =
6362                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6363                     line_ptr += bytes_read;
6364                     length =
6365                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6366                     line_ptr += bytes_read;
6367                     add_file_name (lh, cur_file, dir_index, mod_time, length);
6368                   }
6369                   break;
6370                 default:
6371                   complaint (&symfile_complaints,
6372                              "mangled .debug_line section");
6373                   return;
6374                 }
6375               break;
6376             case DW_LNS_copy:
6377               lh->file_names[file - 1].included_p = 1;
6378               if (!decode_for_pst_p)
6379                 record_line (current_subfile, line, 
6380                              check_cu_functions (address, cu));
6381               basic_block = 0;
6382               break;
6383             case DW_LNS_advance_pc:
6384               address += lh->minimum_instruction_length
6385                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6386               line_ptr += bytes_read;
6387               break;
6388             case DW_LNS_advance_line:
6389               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
6390               line_ptr += bytes_read;
6391               break;
6392             case DW_LNS_set_file:
6393               {
6394                 /* lh->include_dirs and lh->file_names are 0-based,
6395                    but the directory and file name numbers in the
6396                    statement program are 1-based.  */
6397                 struct file_entry *fe;
6398                 char *dir;
6399                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6400                 line_ptr += bytes_read;
6401                 fe = &lh->file_names[file - 1];
6402                 if (fe->dir_index)
6403                   dir = lh->include_dirs[fe->dir_index - 1];
6404                 else
6405                   dir = comp_dir;
6406                 if (!decode_for_pst_p)
6407                   dwarf2_start_subfile (fe->name, dir);
6408               }
6409               break;
6410             case DW_LNS_set_column:
6411               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6412               line_ptr += bytes_read;
6413               break;
6414             case DW_LNS_negate_stmt:
6415               is_stmt = (!is_stmt);
6416               break;
6417             case DW_LNS_set_basic_block:
6418               basic_block = 1;
6419               break;
6420             /* Add to the address register of the state machine the
6421                address increment value corresponding to special opcode
6422                255.  Ie, this value is scaled by the minimum instruction
6423                length since special opcode 255 would have scaled the
6424                the increment.  */
6425             case DW_LNS_const_add_pc:
6426               address += (lh->minimum_instruction_length
6427                           * ((255 - lh->opcode_base) / lh->line_range));
6428               break;
6429             case DW_LNS_fixed_advance_pc:
6430               address += read_2_bytes (abfd, line_ptr);
6431               line_ptr += 2;
6432               break;
6433             default:
6434               {  /* Unknown standard opcode, ignore it.  */
6435                 int i;
6436                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
6437                   {
6438                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
6439                     line_ptr += bytes_read;
6440                   }
6441               }
6442             }
6443         }
6444     }
6445
6446   if (decode_for_pst_p)
6447     {
6448       int file_index;
6449
6450       /* Now that we're done scanning the Line Header Program, we can
6451          create the psymtab of each included file.  */
6452       for (file_index = 0; file_index < lh->num_file_names; file_index++)
6453         if (lh->file_names[file_index].included_p == 1)
6454           {
6455             char *include_name = lh->file_names [file_index].name;
6456     
6457             if (strcmp (include_name, pst->filename) != 0)
6458               dwarf2_create_include_psymtab (include_name, pst, objfile);
6459           }
6460     }
6461 }
6462
6463 /* Start a subfile for DWARF.  FILENAME is the name of the file and
6464    DIRNAME the name of the source directory which contains FILENAME
6465    or NULL if not known.
6466    This routine tries to keep line numbers from identical absolute and
6467    relative file names in a common subfile.
6468
6469    Using the `list' example from the GDB testsuite, which resides in
6470    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
6471    of /srcdir/list0.c yields the following debugging information for list0.c:
6472
6473    DW_AT_name:          /srcdir/list0.c
6474    DW_AT_comp_dir:              /compdir
6475    files.files[0].name: list0.h
6476    files.files[0].dir:  /srcdir
6477    files.files[1].name: list0.c
6478    files.files[1].dir:  /srcdir
6479
6480    The line number information for list0.c has to end up in a single
6481    subfile, so that `break /srcdir/list0.c:1' works as expected.  */
6482
6483 static void
6484 dwarf2_start_subfile (char *filename, char *dirname)
6485 {
6486   /* If the filename isn't absolute, try to match an existing subfile
6487      with the full pathname.  */
6488
6489   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
6490     {
6491       struct subfile *subfile;
6492       char *fullname = concat (dirname, "/", filename, NULL);
6493
6494       for (subfile = subfiles; subfile; subfile = subfile->next)
6495         {
6496           if (FILENAME_CMP (subfile->name, fullname) == 0)
6497             {
6498               current_subfile = subfile;
6499               xfree (fullname);
6500               return;
6501             }
6502         }
6503       xfree (fullname);
6504     }
6505   start_subfile (filename, dirname);
6506 }
6507
6508 static void
6509 var_decode_location (struct attribute *attr, struct symbol *sym,
6510                      struct dwarf2_cu *cu)
6511 {
6512   struct objfile *objfile = cu->objfile;
6513   struct comp_unit_head *cu_header = &cu->header;
6514
6515   /* NOTE drow/2003-01-30: There used to be a comment and some special
6516      code here to turn a symbol with DW_AT_external and a
6517      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
6518      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
6519      with some versions of binutils) where shared libraries could have
6520      relocations against symbols in their debug information - the
6521      minimal symbol would have the right address, but the debug info
6522      would not.  It's no longer necessary, because we will explicitly
6523      apply relocations when we read in the debug information now.  */
6524
6525   /* A DW_AT_location attribute with no contents indicates that a
6526      variable has been optimized away.  */
6527   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
6528     {
6529       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
6530       return;
6531     }
6532
6533   /* Handle one degenerate form of location expression specially, to
6534      preserve GDB's previous behavior when section offsets are
6535      specified.  If this is just a DW_OP_addr then mark this symbol
6536      as LOC_STATIC.  */
6537
6538   if (attr_form_is_block (attr)
6539       && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
6540       && DW_BLOCK (attr)->data[0] == DW_OP_addr)
6541     {
6542       int dummy;
6543
6544       SYMBOL_VALUE_ADDRESS (sym) =
6545         read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
6546       fixup_symbol_section (sym, objfile);
6547       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
6548                                               SYMBOL_SECTION (sym));
6549       SYMBOL_CLASS (sym) = LOC_STATIC;
6550       return;
6551     }
6552
6553   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
6554      expression evaluator, and use LOC_COMPUTED only when necessary
6555      (i.e. when the value of a register or memory location is
6556      referenced, or a thread-local block, etc.).  Then again, it might
6557      not be worthwhile.  I'm assuming that it isn't unless performance
6558      or memory numbers show me otherwise.  */
6559
6560   dwarf2_symbol_mark_computed (attr, sym, cu);
6561   SYMBOL_CLASS (sym) = LOC_COMPUTED;
6562 }
6563
6564 /* Given a pointer to a DWARF information entry, figure out if we need
6565    to make a symbol table entry for it, and if so, create a new entry
6566    and return a pointer to it.
6567    If TYPE is NULL, determine symbol type from the die, otherwise
6568    used the passed type.  */
6569
6570 static struct symbol *
6571 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
6572 {
6573   struct objfile *objfile = cu->objfile;
6574   struct symbol *sym = NULL;
6575   char *name;
6576   struct attribute *attr = NULL;
6577   struct attribute *attr2 = NULL;
6578   CORE_ADDR baseaddr;
6579
6580   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6581
6582   if (die->tag != DW_TAG_namespace)
6583     name = dwarf2_linkage_name (die, cu);
6584   else
6585     name = TYPE_NAME (type);
6586
6587   if (name)
6588     {
6589       sym = (struct symbol *) obstack_alloc (&objfile->objfile_obstack,
6590                                              sizeof (struct symbol));
6591       OBJSTAT (objfile, n_syms++);
6592       memset (sym, 0, sizeof (struct symbol));
6593
6594       /* Cache this symbol's name and the name's demangled form (if any).  */
6595       SYMBOL_LANGUAGE (sym) = cu->language;
6596       SYMBOL_SET_NAMES (sym, name, strlen (name), objfile);
6597
6598       /* Default assumptions.
6599          Use the passed type or decode it from the die.  */
6600       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6601       SYMBOL_CLASS (sym) = LOC_STATIC;
6602       if (type != NULL)
6603         SYMBOL_TYPE (sym) = type;
6604       else
6605         SYMBOL_TYPE (sym) = die_type (die, cu);
6606       attr = dwarf2_attr (die, DW_AT_decl_line, cu);
6607       if (attr)
6608         {
6609           SYMBOL_LINE (sym) = DW_UNSND (attr);
6610         }
6611       switch (die->tag)
6612         {
6613         case DW_TAG_label:
6614           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6615           if (attr)
6616             {
6617               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
6618             }
6619           SYMBOL_CLASS (sym) = LOC_LABEL;
6620           break;
6621         case DW_TAG_subprogram:
6622           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
6623              finish_block.  */
6624           SYMBOL_CLASS (sym) = LOC_BLOCK;
6625           attr2 = dwarf2_attr (die, DW_AT_external, cu);
6626           if (attr2 && (DW_UNSND (attr2) != 0))
6627             {
6628               add_symbol_to_list (sym, &global_symbols);
6629             }
6630           else
6631             {
6632               add_symbol_to_list (sym, cu->list_in_scope);
6633             }
6634           break;
6635         case DW_TAG_variable:
6636           /* Compilation with minimal debug info may result in variables
6637              with missing type entries. Change the misleading `void' type
6638              to something sensible.  */
6639           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
6640             SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
6641                                            TARGET_INT_BIT / HOST_CHAR_BIT, 0,
6642                                            "<variable, no debug info>",
6643                                            objfile);
6644           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6645           if (attr)
6646             {
6647               dwarf2_const_value (attr, sym, cu);
6648               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6649               if (attr2 && (DW_UNSND (attr2) != 0))
6650                 add_symbol_to_list (sym, &global_symbols);
6651               else
6652                 add_symbol_to_list (sym, cu->list_in_scope);
6653               break;
6654             }
6655           attr = dwarf2_attr (die, DW_AT_location, cu);
6656           if (attr)
6657             {
6658               var_decode_location (attr, sym, cu);
6659               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6660               if (attr2 && (DW_UNSND (attr2) != 0))
6661                 add_symbol_to_list (sym, &global_symbols);
6662               else
6663                 add_symbol_to_list (sym, cu->list_in_scope);
6664             }
6665           else
6666             {
6667               /* We do not know the address of this symbol.
6668                  If it is an external symbol and we have type information
6669                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
6670                  The address of the variable will then be determined from
6671                  the minimal symbol table whenever the variable is
6672                  referenced.  */
6673               attr2 = dwarf2_attr (die, DW_AT_external, cu);
6674               if (attr2 && (DW_UNSND (attr2) != 0)
6675                   && dwarf2_attr (die, DW_AT_type, cu) != NULL)
6676                 {
6677                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
6678                   add_symbol_to_list (sym, &global_symbols);
6679                 }
6680             }
6681           break;
6682         case DW_TAG_formal_parameter:
6683           attr = dwarf2_attr (die, DW_AT_location, cu);
6684           if (attr)
6685             {
6686               var_decode_location (attr, sym, cu);
6687               /* FIXME drow/2003-07-31: Is LOC_COMPUTED_ARG necessary?  */
6688               if (SYMBOL_CLASS (sym) == LOC_COMPUTED)
6689                 SYMBOL_CLASS (sym) = LOC_COMPUTED_ARG;
6690             }
6691           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6692           if (attr)
6693             {
6694               dwarf2_const_value (attr, sym, cu);
6695             }
6696           add_symbol_to_list (sym, cu->list_in_scope);
6697           break;
6698         case DW_TAG_unspecified_parameters:
6699           /* From varargs functions; gdb doesn't seem to have any
6700              interest in this information, so just ignore it for now.
6701              (FIXME?) */
6702           break;
6703         case DW_TAG_class_type:
6704         case DW_TAG_structure_type:
6705         case DW_TAG_union_type:
6706         case DW_TAG_enumeration_type:
6707           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6708           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
6709
6710           /* Make sure that the symbol includes appropriate enclosing
6711              classes/namespaces in its name.  These are calculated in
6712              read_structure_type, and the correct name is saved in
6713              the type.  */
6714
6715           if (cu->language == language_cplus
6716               || cu->language == language_java)
6717             {
6718               struct type *type = SYMBOL_TYPE (sym);
6719               
6720               if (TYPE_TAG_NAME (type) != NULL)
6721                 {
6722                   /* FIXME: carlton/2003-11-10: Should this use
6723                      SYMBOL_SET_NAMES instead?  (The same problem also
6724                      arises further down in this function.)  */
6725                   /* The type's name is already allocated along with
6726                      this objfile, so we don't need to duplicate it
6727                      for the symbol.  */
6728                   SYMBOL_LINKAGE_NAME (sym) = TYPE_TAG_NAME (type);
6729                 }
6730             }
6731
6732           {
6733             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
6734                really ever be static objects: otherwise, if you try
6735                to, say, break of a class's method and you're in a file
6736                which doesn't mention that class, it won't work unless
6737                the check for all static symbols in lookup_symbol_aux
6738                saves you.  See the OtherFileClass tests in
6739                gdb.c++/namespace.exp.  */
6740
6741             struct pending **list_to_add;
6742
6743             list_to_add = (cu->list_in_scope == &file_symbols
6744                            && (cu->language == language_cplus
6745                                || cu->language == language_java)
6746                            ? &global_symbols : cu->list_in_scope);
6747           
6748             add_symbol_to_list (sym, list_to_add);
6749
6750             /* The semantics of C++ state that "struct foo { ... }" also
6751                defines a typedef for "foo".  A Java class declaration also
6752                defines a typedef for the class.  Synthesize a typedef symbol
6753                so that "ptype foo" works as expected.  */
6754             if (cu->language == language_cplus
6755                 || cu->language == language_java)
6756               {
6757                 struct symbol *typedef_sym = (struct symbol *)
6758                   obstack_alloc (&objfile->objfile_obstack,
6759                                  sizeof (struct symbol));
6760                 *typedef_sym = *sym;
6761                 SYMBOL_DOMAIN (typedef_sym) = VAR_DOMAIN;
6762                 /* The symbol's name is already allocated along with
6763                    this objfile, so we don't need to duplicate it for
6764                    the type.  */
6765                 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
6766                   TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_NATURAL_NAME (sym);
6767                 add_symbol_to_list (typedef_sym, list_to_add);
6768               }
6769           }
6770           break;
6771         case DW_TAG_typedef:
6772           if (processing_has_namespace_info
6773               && processing_current_prefix[0] != '\0')
6774             {
6775               SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
6776                                                            processing_current_prefix,
6777                                                            name, cu);
6778             }
6779           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6780           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6781           add_symbol_to_list (sym, cu->list_in_scope);
6782           break;
6783         case DW_TAG_base_type:
6784         case DW_TAG_subrange_type:
6785           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6786           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
6787           add_symbol_to_list (sym, cu->list_in_scope);
6788           break;
6789         case DW_TAG_enumerator:
6790           if (processing_has_namespace_info
6791               && processing_current_prefix[0] != '\0')
6792             {
6793               SYMBOL_LINKAGE_NAME (sym) = typename_concat (&objfile->objfile_obstack,
6794                                                            processing_current_prefix,
6795                                                            name, cu);
6796             }
6797           attr = dwarf2_attr (die, DW_AT_const_value, cu);
6798           if (attr)
6799             {
6800               dwarf2_const_value (attr, sym, cu);
6801             }
6802           {
6803             /* NOTE: carlton/2003-11-10: See comment above in the
6804                DW_TAG_class_type, etc. block.  */
6805
6806             struct pending **list_to_add;
6807
6808             list_to_add = (cu->list_in_scope == &file_symbols
6809                            && (cu->language == language_cplus
6810                                || cu->language == language_java)
6811                            ? &global_symbols : cu->list_in_scope);
6812           
6813             add_symbol_to_list (sym, list_to_add);
6814           }
6815           break;
6816         case DW_TAG_namespace:
6817           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
6818           add_symbol_to_list (sym, &global_symbols);
6819           break;
6820         default:
6821           /* Not a tag we recognize.  Hopefully we aren't processing
6822              trash data, but since we must specifically ignore things
6823              we don't recognize, there is nothing else we should do at
6824              this point. */
6825           complaint (&symfile_complaints, "unsupported tag: '%s'",
6826                      dwarf_tag_name (die->tag));
6827           break;
6828         }
6829     }
6830   return (sym);
6831 }
6832
6833 /* Copy constant value from an attribute to a symbol.  */
6834
6835 static void
6836 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
6837                     struct dwarf2_cu *cu)
6838 {
6839   struct objfile *objfile = cu->objfile;
6840   struct comp_unit_head *cu_header = &cu->header;
6841   struct dwarf_block *blk;
6842
6843   switch (attr->form)
6844     {
6845     case DW_FORM_addr:
6846       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != cu_header->addr_size)
6847         dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6848                                                       cu_header->addr_size,
6849                                                       TYPE_LENGTH (SYMBOL_TYPE
6850                                                                    (sym)));
6851       SYMBOL_VALUE_BYTES (sym) = (char *)
6852         obstack_alloc (&objfile->objfile_obstack, cu_header->addr_size);
6853       /* NOTE: cagney/2003-05-09: In-lined store_address call with
6854          it's body - store_unsigned_integer.  */
6855       store_unsigned_integer (SYMBOL_VALUE_BYTES (sym), cu_header->addr_size,
6856                               DW_ADDR (attr));
6857       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6858       break;
6859     case DW_FORM_block1:
6860     case DW_FORM_block2:
6861     case DW_FORM_block4:
6862     case DW_FORM_block:
6863       blk = DW_BLOCK (attr);
6864       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
6865         dwarf2_const_value_length_mismatch_complaint (DEPRECATED_SYMBOL_NAME (sym),
6866                                                       blk->size,
6867                                                       TYPE_LENGTH (SYMBOL_TYPE
6868                                                                    (sym)));
6869       SYMBOL_VALUE_BYTES (sym) = (char *)
6870         obstack_alloc (&objfile->objfile_obstack, blk->size);
6871       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
6872       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
6873       break;
6874
6875       /* The DW_AT_const_value attributes are supposed to carry the
6876          symbol's value "represented as it would be on the target
6877          architecture."  By the time we get here, it's already been
6878          converted to host endianness, so we just need to sign- or
6879          zero-extend it as appropriate.  */
6880     case DW_FORM_data1:
6881       dwarf2_const_value_data (attr, sym, 8);
6882       break;
6883     case DW_FORM_data2:
6884       dwarf2_const_value_data (attr, sym, 16);
6885       break;
6886     case DW_FORM_data4:
6887       dwarf2_const_value_data (attr, sym, 32);
6888       break;
6889     case DW_FORM_data8:
6890       dwarf2_const_value_data (attr, sym, 64);
6891       break;
6892
6893     case DW_FORM_sdata:
6894       SYMBOL_VALUE (sym) = DW_SND (attr);
6895       SYMBOL_CLASS (sym) = LOC_CONST;
6896       break;
6897
6898     case DW_FORM_udata:
6899       SYMBOL_VALUE (sym) = DW_UNSND (attr);
6900       SYMBOL_CLASS (sym) = LOC_CONST;
6901       break;
6902
6903     default:
6904       complaint (&symfile_complaints,
6905                  "unsupported const value attribute form: '%s'",
6906                  dwarf_form_name (attr->form));
6907       SYMBOL_VALUE (sym) = 0;
6908       SYMBOL_CLASS (sym) = LOC_CONST;
6909       break;
6910     }
6911 }
6912
6913
6914 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
6915    or zero-extend it as appropriate for the symbol's type.  */
6916 static void
6917 dwarf2_const_value_data (struct attribute *attr,
6918                          struct symbol *sym,
6919                          int bits)
6920 {
6921   LONGEST l = DW_UNSND (attr);
6922
6923   if (bits < sizeof (l) * 8)
6924     {
6925       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
6926         l &= ((LONGEST) 1 << bits) - 1;
6927       else
6928         l = (l << (sizeof (l) * 8 - bits)) >> (sizeof (l) * 8 - bits);
6929     }
6930
6931   SYMBOL_VALUE (sym) = l;
6932   SYMBOL_CLASS (sym) = LOC_CONST;
6933 }
6934
6935
6936 /* Return the type of the die in question using its DW_AT_type attribute.  */
6937
6938 static struct type *
6939 die_type (struct die_info *die, struct dwarf2_cu *cu)
6940 {
6941   struct type *type;
6942   struct attribute *type_attr;
6943   struct die_info *type_die;
6944   unsigned int ref;
6945
6946   type_attr = dwarf2_attr (die, DW_AT_type, cu);
6947   if (!type_attr)
6948     {
6949       /* A missing DW_AT_type represents a void type.  */
6950       return dwarf2_fundamental_type (cu->objfile, FT_VOID, cu);
6951     }
6952   else
6953     {
6954       ref = dwarf2_get_ref_die_offset (type_attr, cu);
6955       type_die = follow_die_ref (ref);
6956       if (!type_die)
6957         {
6958           error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", 
6959                           ref, cu->objfile->name);
6960           return NULL;
6961         }
6962     }
6963   type = tag_type_to_type (type_die, cu);
6964   if (!type)
6965     {
6966       dump_die (type_die);
6967       error ("Dwarf Error: Problem turning type die at offset into gdb type [in module %s]",
6968                       cu->objfile->name);
6969     }
6970   return type;
6971 }
6972
6973 /* Return the containing type of the die in question using its
6974    DW_AT_containing_type attribute.  */
6975
6976 static struct type *
6977 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
6978 {
6979   struct type *type = NULL;
6980   struct attribute *type_attr;
6981   struct die_info *type_die = NULL;
6982   unsigned int ref;
6983
6984   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
6985   if (type_attr)
6986     {
6987       ref = dwarf2_get_ref_die_offset (type_attr, cu);
6988       type_die = follow_die_ref (ref);
6989       if (!type_die)
6990         {
6991           error ("Dwarf Error: Cannot find referent at offset %d [in module %s]", ref, 
6992                           cu->objfile->name);
6993           return NULL;
6994         }
6995       type = tag_type_to_type (type_die, cu);
6996     }
6997   if (!type)
6998     {
6999       if (type_die)
7000         dump_die (type_die);
7001       error ("Dwarf Error: Problem turning containing type into gdb type [in module %s]", 
7002                       cu->objfile->name);
7003     }
7004   return type;
7005 }
7006
7007 #if 0
7008 static struct type *
7009 type_at_offset (unsigned int offset, struct dwarf2_cu *cu)
7010 {
7011   struct die_info *die;
7012   struct type *type;
7013
7014   die = follow_die_ref (offset);
7015   if (!die)
7016     {
7017       error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
7018       return NULL;
7019     }
7020   type = tag_type_to_type (die, cu);
7021   return type;
7022 }
7023 #endif
7024
7025 static struct type *
7026 tag_type_to_type (struct die_info *die, struct dwarf2_cu *cu)
7027 {
7028   if (die->type)
7029     {
7030       return die->type;
7031     }
7032   else
7033     {
7034       read_type_die (die, cu);
7035       if (!die->type)
7036         {
7037           dump_die (die);
7038           error ("Dwarf Error: Cannot find type of die [in module %s]", 
7039                           cu->objfile->name);
7040         }
7041       return die->type;
7042     }
7043 }
7044
7045 static void
7046 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
7047 {
7048   char *prefix = determine_prefix (die, cu);
7049   const char *old_prefix = processing_current_prefix;
7050   struct cleanup *back_to = make_cleanup (xfree, prefix);
7051   processing_current_prefix = prefix;
7052   
7053   switch (die->tag)
7054     {
7055     case DW_TAG_class_type:
7056     case DW_TAG_structure_type:
7057     case DW_TAG_union_type:
7058       read_structure_type (die, cu);
7059       break;
7060     case DW_TAG_enumeration_type:
7061       read_enumeration_type (die, cu);
7062       break;
7063     case DW_TAG_subprogram:
7064     case DW_TAG_subroutine_type:
7065       read_subroutine_type (die, cu);
7066       break;
7067     case DW_TAG_array_type:
7068       read_array_type (die, cu);
7069       break;
7070     case DW_TAG_pointer_type:
7071       read_tag_pointer_type (die, cu);
7072       break;
7073     case DW_TAG_ptr_to_member_type:
7074       read_tag_ptr_to_member_type (die, cu);
7075       break;
7076     case DW_TAG_reference_type:
7077       read_tag_reference_type (die, cu);
7078       break;
7079     case DW_TAG_const_type:
7080       read_tag_const_type (die, cu);
7081       break;
7082     case DW_TAG_volatile_type:
7083       read_tag_volatile_type (die, cu);
7084       break;
7085     case DW_TAG_string_type:
7086       read_tag_string_type (die, cu);
7087       break;
7088     case DW_TAG_typedef:
7089       read_typedef (die, cu);
7090       break;
7091     case DW_TAG_subrange_type:
7092       read_subrange_type (die, cu);
7093       break;
7094     case DW_TAG_base_type:
7095       read_base_type (die, cu);
7096       break;
7097     default:
7098       complaint (&symfile_complaints, "unexepected tag in read_type_die: '%s'",
7099                  dwarf_tag_name (die->tag));
7100       break;
7101     }
7102
7103   processing_current_prefix = old_prefix;
7104   do_cleanups (back_to);
7105 }
7106
7107 /* Return the name of the namespace/class that DIE is defined within,
7108    or "" if we can't tell.  The caller should xfree the result.  */
7109
7110 /* NOTE: carlton/2004-01-23: See read_func_scope (and the comment
7111    therein) for an example of how to use this function to deal with
7112    DW_AT_specification.  */
7113
7114 static char *
7115 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
7116 {
7117   struct die_info *parent;
7118
7119   if (cu->language != language_cplus
7120       && cu->language != language_java)
7121     return NULL;
7122
7123   parent = die->parent;
7124
7125   if (parent == NULL)
7126     {
7127       return xstrdup ("");
7128     }
7129   else
7130     {
7131       switch (parent->tag) {
7132       case DW_TAG_namespace:
7133         {
7134           /* FIXME: carlton/2004-03-05: Should I follow extension dies
7135              before doing this check?  */
7136           if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
7137             {
7138               return xstrdup (TYPE_TAG_NAME (parent->type));
7139             }
7140           else
7141             {
7142               int dummy;
7143               char *parent_prefix = determine_prefix (parent, cu);
7144               char *retval = typename_concat (NULL, parent_prefix,
7145                                               namespace_name (parent, &dummy,
7146                                                               cu),
7147                                               cu);
7148               xfree (parent_prefix);
7149               return retval;
7150             }
7151         }
7152         break;
7153       case DW_TAG_class_type:
7154       case DW_TAG_structure_type:
7155         {
7156           if (parent->type != NULL && TYPE_TAG_NAME (parent->type) != NULL)
7157             {
7158               return xstrdup (TYPE_TAG_NAME (parent->type));
7159             }
7160           else
7161             {
7162               const char *old_prefix = processing_current_prefix;
7163               char *new_prefix = determine_prefix (parent, cu);
7164               char *retval;
7165
7166               processing_current_prefix = new_prefix;
7167               retval = determine_class_name (parent, cu);
7168               processing_current_prefix = old_prefix;
7169
7170               xfree (new_prefix);
7171               return retval;
7172             }
7173         }
7174       default:
7175         return determine_prefix (parent, cu);
7176       }
7177     }
7178 }
7179
7180 /* Return a newly-allocated string formed by concatenating PREFIX and
7181    SUFFIX with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
7182    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null,
7183    perform an obconcat, otherwise allocate storage for the result.  The CU argument
7184    is used to determine the language and hence, the appropriate separator.  */
7185
7186 #define MAX_SEP_LEN 2  /* sizeof ("::")  */
7187
7188 static char *
7189 typename_concat (struct obstack *obs, const char *prefix, const char *suffix, 
7190                  struct dwarf2_cu *cu)
7191 {
7192   char *sep;
7193
7194   if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
7195     sep = "";
7196   else if (cu->language == language_java)
7197     sep = ".";
7198   else
7199     sep = "::";
7200
7201   if (obs == NULL)
7202     {
7203       char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
7204       retval[0] = '\0';
7205       
7206       if (prefix)
7207         {
7208           strcpy (retval, prefix);
7209           strcat (retval, sep);
7210         }
7211       if (suffix)
7212         strcat (retval, suffix);
7213       
7214       return retval;
7215     }
7216   else
7217     {
7218       /* We have an obstack.  */
7219       return obconcat (obs, prefix, sep, suffix);
7220     }
7221 }
7222
7223 static struct type *
7224 dwarf_base_type (int encoding, int size, struct dwarf2_cu *cu)
7225 {
7226   struct objfile *objfile = cu->objfile;
7227
7228   /* FIXME - this should not produce a new (struct type *)
7229      every time.  It should cache base types.  */
7230   struct type *type;
7231   switch (encoding)
7232     {
7233     case DW_ATE_address:
7234       type = dwarf2_fundamental_type (objfile, FT_VOID, cu);
7235       return type;
7236     case DW_ATE_boolean:
7237       type = dwarf2_fundamental_type (objfile, FT_BOOLEAN, cu);
7238       return type;
7239     case DW_ATE_complex_float:
7240       if (size == 16)
7241         {
7242           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX, cu);
7243         }
7244       else
7245         {
7246           type = dwarf2_fundamental_type (objfile, FT_COMPLEX, cu);
7247         }
7248       return type;
7249     case DW_ATE_float:
7250       if (size == 8)
7251         {
7252           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT, cu);
7253         }
7254       else
7255         {
7256           type = dwarf2_fundamental_type (objfile, FT_FLOAT, cu);
7257         }
7258       return type;
7259     case DW_ATE_signed:
7260       switch (size)
7261         {
7262         case 1:
7263           type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
7264           break;
7265         case 2:
7266           type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT, cu);
7267           break;
7268         default:
7269         case 4:
7270           type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
7271           break;
7272         }
7273       return type;
7274     case DW_ATE_signed_char:
7275       type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR, cu);
7276       return type;
7277     case DW_ATE_unsigned:
7278       switch (size)
7279         {
7280         case 1:
7281           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
7282           break;
7283         case 2:
7284           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT, cu);
7285           break;
7286         default:
7287         case 4:
7288           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER, cu);
7289           break;
7290         }
7291       return type;
7292     case DW_ATE_unsigned_char:
7293       type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR, cu);
7294       return type;
7295     default:
7296       type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER, cu);
7297       return type;
7298     }
7299 }
7300
7301 #if 0
7302 struct die_info *
7303 copy_die (struct die_info *old_die)
7304 {
7305   struct die_info *new_die;
7306   int i, num_attrs;
7307
7308   new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
7309   memset (new_die, 0, sizeof (struct die_info));
7310
7311   new_die->tag = old_die->tag;
7312   new_die->has_children = old_die->has_children;
7313   new_die->abbrev = old_die->abbrev;
7314   new_die->offset = old_die->offset;
7315   new_die->type = NULL;
7316
7317   num_attrs = old_die->num_attrs;
7318   new_die->num_attrs = num_attrs;
7319   new_die->attrs = (struct attribute *)
7320     xmalloc (num_attrs * sizeof (struct attribute));
7321
7322   for (i = 0; i < old_die->num_attrs; ++i)
7323     {
7324       new_die->attrs[i].name = old_die->attrs[i].name;
7325       new_die->attrs[i].form = old_die->attrs[i].form;
7326       new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
7327     }
7328
7329   new_die->next = NULL;
7330   return new_die;
7331 }
7332 #endif
7333
7334 /* Return sibling of die, NULL if no sibling.  */
7335
7336 static struct die_info *
7337 sibling_die (struct die_info *die)
7338 {
7339   return die->sibling;
7340 }
7341
7342 /* Get linkage name of a die, return NULL if not found.  */
7343
7344 static char *
7345 dwarf2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
7346 {
7347   struct attribute *attr;
7348
7349   attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7350   if (attr && DW_STRING (attr))
7351     return DW_STRING (attr);
7352   attr = dwarf2_attr (die, DW_AT_name, cu);
7353   if (attr && DW_STRING (attr))
7354     return DW_STRING (attr);
7355   return NULL;
7356 }
7357
7358 /* Get name of a die, return NULL if not found.  */
7359
7360 static char *
7361 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
7362 {
7363   struct attribute *attr;
7364
7365   attr = dwarf2_attr (die, DW_AT_name, cu);
7366   if (attr && DW_STRING (attr))
7367     return DW_STRING (attr);
7368   return NULL;
7369 }
7370
7371 /* Return the die that this die in an extension of, or NULL if there
7372    is none.  */
7373
7374 static struct die_info *
7375 dwarf2_extension (struct die_info *die, struct dwarf2_cu *cu)
7376 {
7377   struct attribute *attr;
7378   struct die_info *extension_die;
7379   unsigned int ref;
7380
7381   attr = dwarf2_attr (die, DW_AT_extension, cu);
7382   if (attr == NULL)
7383     return NULL;
7384
7385   ref = dwarf2_get_ref_die_offset (attr, cu);
7386   extension_die = follow_die_ref (ref);
7387   if (!extension_die)
7388     {
7389       error ("Dwarf Error: Cannot find referent at offset %d.", ref);
7390     }
7391
7392   return extension_die;
7393 }
7394
7395 /* Convert a DIE tag into its string name.  */
7396
7397 static char *
7398 dwarf_tag_name (unsigned tag)
7399 {
7400   switch (tag)
7401     {
7402     case DW_TAG_padding:
7403       return "DW_TAG_padding";
7404     case DW_TAG_array_type:
7405       return "DW_TAG_array_type";
7406     case DW_TAG_class_type:
7407       return "DW_TAG_class_type";
7408     case DW_TAG_entry_point:
7409       return "DW_TAG_entry_point";
7410     case DW_TAG_enumeration_type:
7411       return "DW_TAG_enumeration_type";
7412     case DW_TAG_formal_parameter:
7413       return "DW_TAG_formal_parameter";
7414     case DW_TAG_imported_declaration:
7415       return "DW_TAG_imported_declaration";
7416     case DW_TAG_label:
7417       return "DW_TAG_label";
7418     case DW_TAG_lexical_block:
7419       return "DW_TAG_lexical_block";
7420     case DW_TAG_member:
7421       return "DW_TAG_member";
7422     case DW_TAG_pointer_type:
7423       return "DW_TAG_pointer_type";
7424     case DW_TAG_reference_type:
7425       return "DW_TAG_reference_type";
7426     case DW_TAG_compile_unit:
7427       return "DW_TAG_compile_unit";
7428     case DW_TAG_string_type:
7429       return "DW_TAG_string_type";
7430     case DW_TAG_structure_type:
7431       return "DW_TAG_structure_type";
7432     case DW_TAG_subroutine_type:
7433       return "DW_TAG_subroutine_type";
7434     case DW_TAG_typedef:
7435       return "DW_TAG_typedef";
7436     case DW_TAG_union_type:
7437       return "DW_TAG_union_type";
7438     case DW_TAG_unspecified_parameters:
7439       return "DW_TAG_unspecified_parameters";
7440     case DW_TAG_variant:
7441       return "DW_TAG_variant";
7442     case DW_TAG_common_block:
7443       return "DW_TAG_common_block";
7444     case DW_TAG_common_inclusion:
7445       return "DW_TAG_common_inclusion";
7446     case DW_TAG_inheritance:
7447       return "DW_TAG_inheritance";
7448     case DW_TAG_inlined_subroutine:
7449       return "DW_TAG_inlined_subroutine";
7450     case DW_TAG_module:
7451       return "DW_TAG_module";
7452     case DW_TAG_ptr_to_member_type:
7453       return "DW_TAG_ptr_to_member_type";
7454     case DW_TAG_set_type:
7455       return "DW_TAG_set_type";
7456     case DW_TAG_subrange_type:
7457       return "DW_TAG_subrange_type";
7458     case DW_TAG_with_stmt:
7459       return "DW_TAG_with_stmt";
7460     case DW_TAG_access_declaration:
7461       return "DW_TAG_access_declaration";
7462     case DW_TAG_base_type:
7463       return "DW_TAG_base_type";
7464     case DW_TAG_catch_block:
7465       return "DW_TAG_catch_block";
7466     case DW_TAG_const_type:
7467       return "DW_TAG_const_type";
7468     case DW_TAG_constant:
7469       return "DW_TAG_constant";
7470     case DW_TAG_enumerator:
7471       return "DW_TAG_enumerator";
7472     case DW_TAG_file_type:
7473       return "DW_TAG_file_type";
7474     case DW_TAG_friend:
7475       return "DW_TAG_friend";
7476     case DW_TAG_namelist:
7477       return "DW_TAG_namelist";
7478     case DW_TAG_namelist_item:
7479       return "DW_TAG_namelist_item";
7480     case DW_TAG_packed_type:
7481       return "DW_TAG_packed_type";
7482     case DW_TAG_subprogram:
7483       return "DW_TAG_subprogram";
7484     case DW_TAG_template_type_param:
7485       return "DW_TAG_template_type_param";
7486     case DW_TAG_template_value_param:
7487       return "DW_TAG_template_value_param";
7488     case DW_TAG_thrown_type:
7489       return "DW_TAG_thrown_type";
7490     case DW_TAG_try_block:
7491       return "DW_TAG_try_block";
7492     case DW_TAG_variant_part:
7493       return "DW_TAG_variant_part";
7494     case DW_TAG_variable:
7495       return "DW_TAG_variable";
7496     case DW_TAG_volatile_type:
7497       return "DW_TAG_volatile_type";
7498     case DW_TAG_dwarf_procedure:
7499       return "DW_TAG_dwarf_procedure";
7500     case DW_TAG_restrict_type:
7501       return "DW_TAG_restrict_type";
7502     case DW_TAG_interface_type:
7503       return "DW_TAG_interface_type";
7504     case DW_TAG_namespace:
7505       return "DW_TAG_namespace";
7506     case DW_TAG_imported_module:
7507       return "DW_TAG_imported_module";
7508     case DW_TAG_unspecified_type:
7509       return "DW_TAG_unspecified_type";
7510     case DW_TAG_partial_unit:
7511       return "DW_TAG_partial_unit";
7512     case DW_TAG_imported_unit:
7513       return "DW_TAG_imported_unit";
7514     case DW_TAG_MIPS_loop:
7515       return "DW_TAG_MIPS_loop";
7516     case DW_TAG_format_label:
7517       return "DW_TAG_format_label";
7518     case DW_TAG_function_template:
7519       return "DW_TAG_function_template";
7520     case DW_TAG_class_template:
7521       return "DW_TAG_class_template";
7522     default:
7523       return "DW_TAG_<unknown>";
7524     }
7525 }
7526
7527 /* Convert a DWARF attribute code into its string name.  */
7528
7529 static char *
7530 dwarf_attr_name (unsigned attr)
7531 {
7532   switch (attr)
7533     {
7534     case DW_AT_sibling:
7535       return "DW_AT_sibling";
7536     case DW_AT_location:
7537       return "DW_AT_location";
7538     case DW_AT_name:
7539       return "DW_AT_name";
7540     case DW_AT_ordering:
7541       return "DW_AT_ordering";
7542     case DW_AT_subscr_data:
7543       return "DW_AT_subscr_data";
7544     case DW_AT_byte_size:
7545       return "DW_AT_byte_size";
7546     case DW_AT_bit_offset:
7547       return "DW_AT_bit_offset";
7548     case DW_AT_bit_size:
7549       return "DW_AT_bit_size";
7550     case DW_AT_element_list:
7551       return "DW_AT_element_list";
7552     case DW_AT_stmt_list:
7553       return "DW_AT_stmt_list";
7554     case DW_AT_low_pc:
7555       return "DW_AT_low_pc";
7556     case DW_AT_high_pc:
7557       return "DW_AT_high_pc";
7558     case DW_AT_language:
7559       return "DW_AT_language";
7560     case DW_AT_member:
7561       return "DW_AT_member";
7562     case DW_AT_discr:
7563       return "DW_AT_discr";
7564     case DW_AT_discr_value:
7565       return "DW_AT_discr_value";
7566     case DW_AT_visibility:
7567       return "DW_AT_visibility";
7568     case DW_AT_import:
7569       return "DW_AT_import";
7570     case DW_AT_string_length:
7571       return "DW_AT_string_length";
7572     case DW_AT_common_reference:
7573       return "DW_AT_common_reference";
7574     case DW_AT_comp_dir:
7575       return "DW_AT_comp_dir";
7576     case DW_AT_const_value:
7577       return "DW_AT_const_value";
7578     case DW_AT_containing_type:
7579       return "DW_AT_containing_type";
7580     case DW_AT_default_value:
7581       return "DW_AT_default_value";
7582     case DW_AT_inline:
7583       return "DW_AT_inline";
7584     case DW_AT_is_optional:
7585       return "DW_AT_is_optional";
7586     case DW_AT_lower_bound:
7587       return "DW_AT_lower_bound";
7588     case DW_AT_producer:
7589       return "DW_AT_producer";
7590     case DW_AT_prototyped:
7591       return "DW_AT_prototyped";
7592     case DW_AT_return_addr:
7593       return "DW_AT_return_addr";
7594     case DW_AT_start_scope:
7595       return "DW_AT_start_scope";
7596     case DW_AT_stride_size:
7597       return "DW_AT_stride_size";
7598     case DW_AT_upper_bound:
7599       return "DW_AT_upper_bound";
7600     case DW_AT_abstract_origin:
7601       return "DW_AT_abstract_origin";
7602     case DW_AT_accessibility:
7603       return "DW_AT_accessibility";
7604     case DW_AT_address_class:
7605       return "DW_AT_address_class";
7606     case DW_AT_artificial:
7607       return "DW_AT_artificial";
7608     case DW_AT_base_types:
7609       return "DW_AT_base_types";
7610     case DW_AT_calling_convention:
7611       return "DW_AT_calling_convention";
7612     case DW_AT_count:
7613       return "DW_AT_count";
7614     case DW_AT_data_member_location:
7615       return "DW_AT_data_member_location";
7616     case DW_AT_decl_column:
7617       return "DW_AT_decl_column";
7618     case DW_AT_decl_file:
7619       return "DW_AT_decl_file";
7620     case DW_AT_decl_line:
7621       return "DW_AT_decl_line";
7622     case DW_AT_declaration:
7623       return "DW_AT_declaration";
7624     case DW_AT_discr_list:
7625       return "DW_AT_discr_list";
7626     case DW_AT_encoding:
7627       return "DW_AT_encoding";
7628     case DW_AT_external:
7629       return "DW_AT_external";
7630     case DW_AT_frame_base:
7631       return "DW_AT_frame_base";
7632     case DW_AT_friend:
7633       return "DW_AT_friend";
7634     case DW_AT_identifier_case:
7635       return "DW_AT_identifier_case";
7636     case DW_AT_macro_info:
7637       return "DW_AT_macro_info";
7638     case DW_AT_namelist_items:
7639       return "DW_AT_namelist_items";
7640     case DW_AT_priority:
7641       return "DW_AT_priority";
7642     case DW_AT_segment:
7643       return "DW_AT_segment";
7644     case DW_AT_specification:
7645       return "DW_AT_specification";
7646     case DW_AT_static_link:
7647       return "DW_AT_static_link";
7648     case DW_AT_type:
7649       return "DW_AT_type";
7650     case DW_AT_use_location:
7651       return "DW_AT_use_location";
7652     case DW_AT_variable_parameter:
7653       return "DW_AT_variable_parameter";
7654     case DW_AT_virtuality:
7655       return "DW_AT_virtuality";
7656     case DW_AT_vtable_elem_location:
7657       return "DW_AT_vtable_elem_location";
7658     case DW_AT_allocated:
7659       return "DW_AT_allocated";
7660     case DW_AT_associated:
7661       return "DW_AT_associated";
7662     case DW_AT_data_location:
7663       return "DW_AT_data_location";
7664     case DW_AT_stride:
7665       return "DW_AT_stride";
7666     case DW_AT_entry_pc:
7667       return "DW_AT_entry_pc";
7668     case DW_AT_use_UTF8:
7669       return "DW_AT_use_UTF8";
7670     case DW_AT_extension:
7671       return "DW_AT_extension";
7672     case DW_AT_ranges:
7673       return "DW_AT_ranges";
7674     case DW_AT_trampoline:
7675       return "DW_AT_trampoline";
7676     case DW_AT_call_column:
7677       return "DW_AT_call_column";
7678     case DW_AT_call_file:
7679       return "DW_AT_call_file";
7680     case DW_AT_call_line:
7681       return "DW_AT_call_line";
7682 #ifdef MIPS
7683     case DW_AT_MIPS_fde:
7684       return "DW_AT_MIPS_fde";
7685     case DW_AT_MIPS_loop_begin:
7686       return "DW_AT_MIPS_loop_begin";
7687     case DW_AT_MIPS_tail_loop_begin:
7688       return "DW_AT_MIPS_tail_loop_begin";
7689     case DW_AT_MIPS_epilog_begin:
7690       return "DW_AT_MIPS_epilog_begin";
7691     case DW_AT_MIPS_loop_unroll_factor:
7692       return "DW_AT_MIPS_loop_unroll_factor";
7693     case DW_AT_MIPS_software_pipeline_depth:
7694       return "DW_AT_MIPS_software_pipeline_depth";
7695 #endif
7696     case DW_AT_MIPS_linkage_name:
7697       return "DW_AT_MIPS_linkage_name";
7698
7699     case DW_AT_sf_names:
7700       return "DW_AT_sf_names";
7701     case DW_AT_src_info:
7702       return "DW_AT_src_info";
7703     case DW_AT_mac_info:
7704       return "DW_AT_mac_info";
7705     case DW_AT_src_coords:
7706       return "DW_AT_src_coords";
7707     case DW_AT_body_begin:
7708       return "DW_AT_body_begin";
7709     case DW_AT_body_end:
7710       return "DW_AT_body_end";
7711     case DW_AT_GNU_vector:
7712       return "DW_AT_GNU_vector";
7713     default:
7714       return "DW_AT_<unknown>";
7715     }
7716 }
7717
7718 /* Convert a DWARF value form code into its string name.  */
7719
7720 static char *
7721 dwarf_form_name (unsigned form)
7722 {
7723   switch (form)
7724     {
7725     case DW_FORM_addr:
7726       return "DW_FORM_addr";
7727     case DW_FORM_block2:
7728       return "DW_FORM_block2";
7729     case DW_FORM_block4:
7730       return "DW_FORM_block4";
7731     case DW_FORM_data2:
7732       return "DW_FORM_data2";
7733     case DW_FORM_data4:
7734       return "DW_FORM_data4";
7735     case DW_FORM_data8:
7736       return "DW_FORM_data8";
7737     case DW_FORM_string:
7738       return "DW_FORM_string";
7739     case DW_FORM_block:
7740       return "DW_FORM_block";
7741     case DW_FORM_block1:
7742       return "DW_FORM_block1";
7743     case DW_FORM_data1:
7744       return "DW_FORM_data1";
7745     case DW_FORM_flag:
7746       return "DW_FORM_flag";
7747     case DW_FORM_sdata:
7748       return "DW_FORM_sdata";
7749     case DW_FORM_strp:
7750       return "DW_FORM_strp";
7751     case DW_FORM_udata:
7752       return "DW_FORM_udata";
7753     case DW_FORM_ref_addr:
7754       return "DW_FORM_ref_addr";
7755     case DW_FORM_ref1:
7756       return "DW_FORM_ref1";
7757     case DW_FORM_ref2:
7758       return "DW_FORM_ref2";
7759     case DW_FORM_ref4:
7760       return "DW_FORM_ref4";
7761     case DW_FORM_ref8:
7762       return "DW_FORM_ref8";
7763     case DW_FORM_ref_udata:
7764       return "DW_FORM_ref_udata";
7765     case DW_FORM_indirect:
7766       return "DW_FORM_indirect";
7767     default:
7768       return "DW_FORM_<unknown>";
7769     }
7770 }
7771
7772 /* Convert a DWARF stack opcode into its string name.  */
7773
7774 static char *
7775 dwarf_stack_op_name (unsigned op)
7776 {
7777   switch (op)
7778     {
7779     case DW_OP_addr:
7780       return "DW_OP_addr";
7781     case DW_OP_deref:
7782       return "DW_OP_deref";
7783     case DW_OP_const1u:
7784       return "DW_OP_const1u";
7785     case DW_OP_const1s:
7786       return "DW_OP_const1s";
7787     case DW_OP_const2u:
7788       return "DW_OP_const2u";
7789     case DW_OP_const2s:
7790       return "DW_OP_const2s";
7791     case DW_OP_const4u:
7792       return "DW_OP_const4u";
7793     case DW_OP_const4s:
7794       return "DW_OP_const4s";
7795     case DW_OP_const8u:
7796       return "DW_OP_const8u";
7797     case DW_OP_const8s:
7798       return "DW_OP_const8s";
7799     case DW_OP_constu:
7800       return "DW_OP_constu";
7801     case DW_OP_consts:
7802       return "DW_OP_consts";
7803     case DW_OP_dup:
7804       return "DW_OP_dup";
7805     case DW_OP_drop:
7806       return "DW_OP_drop";
7807     case DW_OP_over:
7808       return "DW_OP_over";
7809     case DW_OP_pick:
7810       return "DW_OP_pick";
7811     case DW_OP_swap:
7812       return "DW_OP_swap";
7813     case DW_OP_rot:
7814       return "DW_OP_rot";
7815     case DW_OP_xderef:
7816       return "DW_OP_xderef";
7817     case DW_OP_abs:
7818       return "DW_OP_abs";
7819     case DW_OP_and:
7820       return "DW_OP_and";
7821     case DW_OP_div:
7822       return "DW_OP_div";
7823     case DW_OP_minus:
7824       return "DW_OP_minus";
7825     case DW_OP_mod:
7826       return "DW_OP_mod";
7827     case DW_OP_mul:
7828       return "DW_OP_mul";
7829     case DW_OP_neg:
7830       return "DW_OP_neg";
7831     case DW_OP_not:
7832       return "DW_OP_not";
7833     case DW_OP_or:
7834       return "DW_OP_or";
7835     case DW_OP_plus:
7836       return "DW_OP_plus";
7837     case DW_OP_plus_uconst:
7838       return "DW_OP_plus_uconst";
7839     case DW_OP_shl:
7840       return "DW_OP_shl";
7841     case DW_OP_shr:
7842       return "DW_OP_shr";
7843     case DW_OP_shra:
7844       return "DW_OP_shra";
7845     case DW_OP_xor:
7846       return "DW_OP_xor";
7847     case DW_OP_bra:
7848       return "DW_OP_bra";
7849     case DW_OP_eq:
7850       return "DW_OP_eq";
7851     case DW_OP_ge:
7852       return "DW_OP_ge";
7853     case DW_OP_gt:
7854       return "DW_OP_gt";
7855     case DW_OP_le:
7856       return "DW_OP_le";
7857     case DW_OP_lt:
7858       return "DW_OP_lt";
7859     case DW_OP_ne:
7860       return "DW_OP_ne";
7861     case DW_OP_skip:
7862       return "DW_OP_skip";
7863     case DW_OP_lit0:
7864       return "DW_OP_lit0";
7865     case DW_OP_lit1:
7866       return "DW_OP_lit1";
7867     case DW_OP_lit2:
7868       return "DW_OP_lit2";
7869     case DW_OP_lit3:
7870       return "DW_OP_lit3";
7871     case DW_OP_lit4:
7872       return "DW_OP_lit4";
7873     case DW_OP_lit5:
7874       return "DW_OP_lit5";
7875     case DW_OP_lit6:
7876       return "DW_OP_lit6";
7877     case DW_OP_lit7:
7878       return "DW_OP_lit7";
7879     case DW_OP_lit8:
7880       return "DW_OP_lit8";
7881     case DW_OP_lit9:
7882       return "DW_OP_lit9";
7883     case DW_OP_lit10:
7884       return "DW_OP_lit10";
7885     case DW_OP_lit11:
7886       return "DW_OP_lit11";
7887     case DW_OP_lit12:
7888       return "DW_OP_lit12";
7889     case DW_OP_lit13:
7890       return "DW_OP_lit13";
7891     case DW_OP_lit14:
7892       return "DW_OP_lit14";
7893     case DW_OP_lit15:
7894       return "DW_OP_lit15";
7895     case DW_OP_lit16:
7896       return "DW_OP_lit16";
7897     case DW_OP_lit17:
7898       return "DW_OP_lit17";
7899     case DW_OP_lit18:
7900       return "DW_OP_lit18";
7901     case DW_OP_lit19:
7902       return "DW_OP_lit19";
7903     case DW_OP_lit20:
7904       return "DW_OP_lit20";
7905     case DW_OP_lit21:
7906       return "DW_OP_lit21";
7907     case DW_OP_lit22:
7908       return "DW_OP_lit22";
7909     case DW_OP_lit23:
7910       return "DW_OP_lit23";
7911     case DW_OP_lit24:
7912       return "DW_OP_lit24";
7913     case DW_OP_lit25:
7914       return "DW_OP_lit25";
7915     case DW_OP_lit26:
7916       return "DW_OP_lit26";
7917     case DW_OP_lit27:
7918       return "DW_OP_lit27";
7919     case DW_OP_lit28:
7920       return "DW_OP_lit28";
7921     case DW_OP_lit29:
7922       return "DW_OP_lit29";
7923     case DW_OP_lit30:
7924       return "DW_OP_lit30";
7925     case DW_OP_lit31:
7926       return "DW_OP_lit31";
7927     case DW_OP_reg0:
7928       return "DW_OP_reg0";
7929     case DW_OP_reg1:
7930       return "DW_OP_reg1";
7931     case DW_OP_reg2:
7932       return "DW_OP_reg2";
7933     case DW_OP_reg3:
7934       return "DW_OP_reg3";
7935     case DW_OP_reg4:
7936       return "DW_OP_reg4";
7937     case DW_OP_reg5:
7938       return "DW_OP_reg5";
7939     case DW_OP_reg6:
7940       return "DW_OP_reg6";
7941     case DW_OP_reg7:
7942       return "DW_OP_reg7";
7943     case DW_OP_reg8:
7944       return "DW_OP_reg8";
7945     case DW_OP_reg9:
7946       return "DW_OP_reg9";
7947     case DW_OP_reg10:
7948       return "DW_OP_reg10";
7949     case DW_OP_reg11:
7950       return "DW_OP_reg11";
7951     case DW_OP_reg12:
7952       return "DW_OP_reg12";
7953     case DW_OP_reg13:
7954       return "DW_OP_reg13";
7955     case DW_OP_reg14:
7956       return "DW_OP_reg14";
7957     case DW_OP_reg15:
7958       return "DW_OP_reg15";
7959     case DW_OP_reg16:
7960       return "DW_OP_reg16";
7961     case DW_OP_reg17:
7962       return "DW_OP_reg17";
7963     case DW_OP_reg18:
7964       return "DW_OP_reg18";
7965     case DW_OP_reg19:
7966       return "DW_OP_reg19";
7967     case DW_OP_reg20:
7968       return "DW_OP_reg20";
7969     case DW_OP_reg21:
7970       return "DW_OP_reg21";
7971     case DW_OP_reg22:
7972       return "DW_OP_reg22";
7973     case DW_OP_reg23:
7974       return "DW_OP_reg23";
7975     case DW_OP_reg24:
7976       return "DW_OP_reg24";
7977     case DW_OP_reg25:
7978       return "DW_OP_reg25";
7979     case DW_OP_reg26:
7980       return "DW_OP_reg26";
7981     case DW_OP_reg27:
7982       return "DW_OP_reg27";
7983     case DW_OP_reg28:
7984       return "DW_OP_reg28";
7985     case DW_OP_reg29:
7986       return "DW_OP_reg29";
7987     case DW_OP_reg30:
7988       return "DW_OP_reg30";
7989     case DW_OP_reg31:
7990       return "DW_OP_reg31";
7991     case DW_OP_breg0:
7992       return "DW_OP_breg0";
7993     case DW_OP_breg1:
7994       return "DW_OP_breg1";
7995     case DW_OP_breg2:
7996       return "DW_OP_breg2";
7997     case DW_OP_breg3:
7998       return "DW_OP_breg3";
7999     case DW_OP_breg4:
8000       return "DW_OP_breg4";
8001     case DW_OP_breg5:
8002       return "DW_OP_breg5";
8003     case DW_OP_breg6:
8004       return "DW_OP_breg6";
8005     case DW_OP_breg7:
8006       return "DW_OP_breg7";
8007     case DW_OP_breg8:
8008       return "DW_OP_breg8";
8009     case DW_OP_breg9:
8010       return "DW_OP_breg9";
8011     case DW_OP_breg10:
8012       return "DW_OP_breg10";
8013     case DW_OP_breg11:
8014       return "DW_OP_breg11";
8015     case DW_OP_breg12:
8016       return "DW_OP_breg12";
8017     case DW_OP_breg13:
8018       return "DW_OP_breg13";
8019     case DW_OP_breg14:
8020       return "DW_OP_breg14";
8021     case DW_OP_breg15:
8022       return "DW_OP_breg15";
8023     case DW_OP_breg16:
8024       return "DW_OP_breg16";
8025     case DW_OP_breg17:
8026       return "DW_OP_breg17";
8027     case DW_OP_breg18:
8028       return "DW_OP_breg18";
8029     case DW_OP_breg19:
8030       return "DW_OP_breg19";
8031     case DW_OP_breg20:
8032       return "DW_OP_breg20";
8033     case DW_OP_breg21:
8034       return "DW_OP_breg21";
8035     case DW_OP_breg22:
8036       return "DW_OP_breg22";
8037     case DW_OP_breg23:
8038       return "DW_OP_breg23";
8039     case DW_OP_breg24:
8040       return "DW_OP_breg24";
8041     case DW_OP_breg25:
8042       return "DW_OP_breg25";
8043     case DW_OP_breg26:
8044       return "DW_OP_breg26";
8045     case DW_OP_breg27:
8046       return "DW_OP_breg27";
8047     case DW_OP_breg28:
8048       return "DW_OP_breg28";
8049     case DW_OP_breg29:
8050       return "DW_OP_breg29";
8051     case DW_OP_breg30:
8052       return "DW_OP_breg30";
8053     case DW_OP_breg31:
8054       return "DW_OP_breg31";
8055     case DW_OP_regx:
8056       return "DW_OP_regx";
8057     case DW_OP_fbreg:
8058       return "DW_OP_fbreg";
8059     case DW_OP_bregx:
8060       return "DW_OP_bregx";
8061     case DW_OP_piece:
8062       return "DW_OP_piece";
8063     case DW_OP_deref_size:
8064       return "DW_OP_deref_size";
8065     case DW_OP_xderef_size:
8066       return "DW_OP_xderef_size";
8067     case DW_OP_nop:
8068       return "DW_OP_nop";
8069       /* DWARF 3 extensions.  */
8070     case DW_OP_push_object_address:
8071       return "DW_OP_push_object_address";
8072     case DW_OP_call2:
8073       return "DW_OP_call2";
8074     case DW_OP_call4:
8075       return "DW_OP_call4";
8076     case DW_OP_call_ref:
8077       return "DW_OP_call_ref";
8078       /* GNU extensions.  */
8079     case DW_OP_GNU_push_tls_address:
8080       return "DW_OP_GNU_push_tls_address";
8081     default:
8082       return "OP_<unknown>";
8083     }
8084 }
8085
8086 static char *
8087 dwarf_bool_name (unsigned mybool)
8088 {
8089   if (mybool)
8090     return "TRUE";
8091   else
8092     return "FALSE";
8093 }
8094
8095 /* Convert a DWARF type code into its string name.  */
8096
8097 static char *
8098 dwarf_type_encoding_name (unsigned enc)
8099 {
8100   switch (enc)
8101     {
8102     case DW_ATE_address:
8103       return "DW_ATE_address";
8104     case DW_ATE_boolean:
8105       return "DW_ATE_boolean";
8106     case DW_ATE_complex_float:
8107       return "DW_ATE_complex_float";
8108     case DW_ATE_float:
8109       return "DW_ATE_float";
8110     case DW_ATE_signed:
8111       return "DW_ATE_signed";
8112     case DW_ATE_signed_char:
8113       return "DW_ATE_signed_char";
8114     case DW_ATE_unsigned:
8115       return "DW_ATE_unsigned";
8116     case DW_ATE_unsigned_char:
8117       return "DW_ATE_unsigned_char";
8118     case DW_ATE_imaginary_float:
8119       return "DW_ATE_imaginary_float";
8120     default:
8121       return "DW_ATE_<unknown>";
8122     }
8123 }
8124
8125 /* Convert a DWARF call frame info operation to its string name. */
8126
8127 #if 0
8128 static char *
8129 dwarf_cfi_name (unsigned cfi_opc)
8130 {
8131   switch (cfi_opc)
8132     {
8133     case DW_CFA_advance_loc:
8134       return "DW_CFA_advance_loc";
8135     case DW_CFA_offset:
8136       return "DW_CFA_offset";
8137     case DW_CFA_restore:
8138       return "DW_CFA_restore";
8139     case DW_CFA_nop:
8140       return "DW_CFA_nop";
8141     case DW_CFA_set_loc:
8142       return "DW_CFA_set_loc";
8143     case DW_CFA_advance_loc1:
8144       return "DW_CFA_advance_loc1";
8145     case DW_CFA_advance_loc2:
8146       return "DW_CFA_advance_loc2";
8147     case DW_CFA_advance_loc4:
8148       return "DW_CFA_advance_loc4";
8149     case DW_CFA_offset_extended:
8150       return "DW_CFA_offset_extended";
8151     case DW_CFA_restore_extended:
8152       return "DW_CFA_restore_extended";
8153     case DW_CFA_undefined:
8154       return "DW_CFA_undefined";
8155     case DW_CFA_same_value:
8156       return "DW_CFA_same_value";
8157     case DW_CFA_register:
8158       return "DW_CFA_register";
8159     case DW_CFA_remember_state:
8160       return "DW_CFA_remember_state";
8161     case DW_CFA_restore_state:
8162       return "DW_CFA_restore_state";
8163     case DW_CFA_def_cfa:
8164       return "DW_CFA_def_cfa";
8165     case DW_CFA_def_cfa_register:
8166       return "DW_CFA_def_cfa_register";
8167     case DW_CFA_def_cfa_offset:
8168       return "DW_CFA_def_cfa_offset";
8169
8170     /* DWARF 3 */
8171     case DW_CFA_def_cfa_expression:
8172       return "DW_CFA_def_cfa_expression";
8173     case DW_CFA_expression:
8174       return "DW_CFA_expression";
8175     case DW_CFA_offset_extended_sf:
8176       return "DW_CFA_offset_extended_sf";
8177     case DW_CFA_def_cfa_sf:
8178       return "DW_CFA_def_cfa_sf";
8179     case DW_CFA_def_cfa_offset_sf:
8180       return "DW_CFA_def_cfa_offset_sf";
8181
8182       /* SGI/MIPS specific */
8183     case DW_CFA_MIPS_advance_loc8:
8184       return "DW_CFA_MIPS_advance_loc8";
8185
8186     /* GNU extensions */
8187     case DW_CFA_GNU_window_save:
8188       return "DW_CFA_GNU_window_save";
8189     case DW_CFA_GNU_args_size:
8190       return "DW_CFA_GNU_args_size";
8191     case DW_CFA_GNU_negative_offset_extended:
8192       return "DW_CFA_GNU_negative_offset_extended";
8193
8194     default:
8195       return "DW_CFA_<unknown>";
8196     }
8197 }
8198 #endif
8199
8200 static void
8201 dump_die (struct die_info *die)
8202 {
8203   unsigned int i;
8204
8205   fprintf_unfiltered (gdb_stderr, "Die: %s (abbrev = %d, offset = %d)\n",
8206            dwarf_tag_name (die->tag), die->abbrev, die->offset);
8207   fprintf_unfiltered (gdb_stderr, "\thas children: %s\n",
8208            dwarf_bool_name (die->child != NULL));
8209
8210   fprintf_unfiltered (gdb_stderr, "\tattributes:\n");
8211   for (i = 0; i < die->num_attrs; ++i)
8212     {
8213       fprintf_unfiltered (gdb_stderr, "\t\t%s (%s) ",
8214                dwarf_attr_name (die->attrs[i].name),
8215                dwarf_form_name (die->attrs[i].form));
8216       switch (die->attrs[i].form)
8217         {
8218         case DW_FORM_ref_addr:
8219         case DW_FORM_addr:
8220           fprintf_unfiltered (gdb_stderr, "address: ");
8221           print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
8222           break;
8223         case DW_FORM_block2:
8224         case DW_FORM_block4:
8225         case DW_FORM_block:
8226         case DW_FORM_block1:
8227           fprintf_unfiltered (gdb_stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
8228           break;
8229         case DW_FORM_data1:
8230         case DW_FORM_data2:
8231         case DW_FORM_data4:
8232         case DW_FORM_data8:
8233         case DW_FORM_ref1:
8234         case DW_FORM_ref2:
8235         case DW_FORM_ref4:
8236         case DW_FORM_udata:
8237         case DW_FORM_sdata:
8238           fprintf_unfiltered (gdb_stderr, "constant: %ld", DW_UNSND (&die->attrs[i]));
8239           break;
8240         case DW_FORM_string:
8241         case DW_FORM_strp:
8242           fprintf_unfiltered (gdb_stderr, "string: \"%s\"",
8243                    DW_STRING (&die->attrs[i])
8244                    ? DW_STRING (&die->attrs[i]) : "");
8245           break;
8246         case DW_FORM_flag:
8247           if (DW_UNSND (&die->attrs[i]))
8248             fprintf_unfiltered (gdb_stderr, "flag: TRUE");
8249           else
8250             fprintf_unfiltered (gdb_stderr, "flag: FALSE");
8251           break;
8252         case DW_FORM_indirect:
8253           /* the reader will have reduced the indirect form to
8254              the "base form" so this form should not occur */
8255           fprintf_unfiltered (gdb_stderr, "unexpected attribute form: DW_FORM_indirect");
8256           break;
8257         default:
8258           fprintf_unfiltered (gdb_stderr, "unsupported attribute form: %d.",
8259                    die->attrs[i].form);
8260         }
8261       fprintf_unfiltered (gdb_stderr, "\n");
8262     }
8263 }
8264
8265 static void
8266 dump_die_list (struct die_info *die)
8267 {
8268   while (die)
8269     {
8270       dump_die (die);
8271       if (die->child != NULL)
8272         dump_die_list (die->child);
8273       if (die->sibling != NULL)
8274         dump_die_list (die->sibling);
8275     }
8276 }
8277
8278 static void
8279 store_in_ref_table (unsigned int offset, struct die_info *die)
8280 {
8281   int h;
8282   struct die_info *old;
8283
8284   h = (offset % REF_HASH_SIZE);
8285   old = die_ref_table[h];
8286   die->next_ref = old;
8287   die_ref_table[h] = die;
8288 }
8289
8290
8291 static void
8292 dwarf2_empty_hash_tables (void)
8293 {
8294   memset (die_ref_table, 0, sizeof (die_ref_table));
8295 }
8296
8297 static unsigned int
8298 dwarf2_get_ref_die_offset (struct attribute *attr, struct dwarf2_cu *cu)
8299 {
8300   unsigned int result = 0;
8301
8302   switch (attr->form)
8303     {
8304     case DW_FORM_ref_addr:
8305       result = DW_ADDR (attr);
8306       break;
8307     case DW_FORM_ref1:
8308     case DW_FORM_ref2:
8309     case DW_FORM_ref4:
8310     case DW_FORM_ref8:
8311     case DW_FORM_ref_udata:
8312       result = cu->header.offset + DW_UNSND (attr);
8313       break;
8314     default:
8315       complaint (&symfile_complaints,
8316                  "unsupported die ref attribute form: '%s'",
8317                  dwarf_form_name (attr->form));
8318     }
8319   return result;
8320 }
8321
8322 /* Return the constant value held by the given attribute.  Return -1
8323    if the value held by the attribute is not constant.  */
8324
8325 static int
8326 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
8327 {
8328   if (attr->form == DW_FORM_sdata)
8329     return DW_SND (attr);
8330   else if (attr->form == DW_FORM_udata
8331            || attr->form == DW_FORM_data1
8332            || attr->form == DW_FORM_data2
8333            || attr->form == DW_FORM_data4
8334            || attr->form == DW_FORM_data8)
8335     return DW_UNSND (attr);
8336   else
8337     {
8338       complaint (&symfile_complaints, "Attribute value is not a constant (%s)",
8339                  dwarf_form_name (attr->form));
8340       return default_value;
8341     }
8342 }
8343
8344 static struct die_info *
8345 follow_die_ref (unsigned int offset)
8346 {
8347   struct die_info *die;
8348   int h;
8349
8350   h = (offset % REF_HASH_SIZE);
8351   die = die_ref_table[h];
8352   while (die)
8353     {
8354       if (die->offset == offset)
8355         {
8356           return die;
8357         }
8358       die = die->next_ref;
8359     }
8360   return NULL;
8361 }
8362
8363 static struct type *
8364 dwarf2_fundamental_type (struct objfile *objfile, int typeid,
8365                          struct dwarf2_cu *cu)
8366 {
8367   if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
8368     {
8369       error ("Dwarf Error: internal error - invalid fundamental type id %d [in module %s]",
8370              typeid, objfile->name);
8371     }
8372
8373   /* Look for this particular type in the fundamental type vector.  If
8374      one is not found, create and install one appropriate for the
8375      current language and the current target machine. */
8376
8377   if (cu->ftypes[typeid] == NULL)
8378     {
8379       cu->ftypes[typeid] = cu->language_defn->la_fund_type (objfile, typeid);
8380     }
8381
8382   return (cu->ftypes[typeid]);
8383 }
8384
8385 /* Decode simple location descriptions.
8386    Given a pointer to a dwarf block that defines a location, compute
8387    the location and return the value.
8388
8389    NOTE drow/2003-11-18: This function is called in two situations
8390    now: for the address of static or global variables (partial symbols
8391    only) and for offsets into structures which are expected to be
8392    (more or less) constant.  The partial symbol case should go away,
8393    and only the constant case should remain.  That will let this
8394    function complain more accurately.  A few special modes are allowed
8395    without complaint for global variables (for instance, global
8396    register values and thread-local values).
8397
8398    A location description containing no operations indicates that the
8399    object is optimized out.  The return value is 0 for that case.
8400    FIXME drow/2003-11-16: No callers check for this case any more; soon all
8401    callers will only want a very basic result and this can become a
8402    complaint.
8403
8404    When the result is a register number, the global isreg flag is set,
8405    otherwise it is cleared.
8406
8407    Note that stack[0] is unused except as a default error return.
8408    Note that stack overflow is not yet handled.  */
8409
8410 static CORE_ADDR
8411 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
8412 {
8413   struct objfile *objfile = cu->objfile;
8414   struct comp_unit_head *cu_header = &cu->header;
8415   int i;
8416   int size = blk->size;
8417   char *data = blk->data;
8418   CORE_ADDR stack[64];
8419   int stacki;
8420   unsigned int bytes_read, unsnd;
8421   unsigned char op;
8422
8423   i = 0;
8424   stacki = 0;
8425   stack[stacki] = 0;
8426   isreg = 0;
8427
8428   while (i < size)
8429     {
8430       op = data[i++];
8431       switch (op)
8432         {
8433         case DW_OP_lit0:
8434         case DW_OP_lit1:
8435         case DW_OP_lit2:
8436         case DW_OP_lit3:
8437         case DW_OP_lit4:
8438         case DW_OP_lit5:
8439         case DW_OP_lit6:
8440         case DW_OP_lit7:
8441         case DW_OP_lit8:
8442         case DW_OP_lit9:
8443         case DW_OP_lit10:
8444         case DW_OP_lit11:
8445         case DW_OP_lit12:
8446         case DW_OP_lit13:
8447         case DW_OP_lit14:
8448         case DW_OP_lit15:
8449         case DW_OP_lit16:
8450         case DW_OP_lit17:
8451         case DW_OP_lit18:
8452         case DW_OP_lit19:
8453         case DW_OP_lit20:
8454         case DW_OP_lit21:
8455         case DW_OP_lit22:
8456         case DW_OP_lit23:
8457         case DW_OP_lit24:
8458         case DW_OP_lit25:
8459         case DW_OP_lit26:
8460         case DW_OP_lit27:
8461         case DW_OP_lit28:
8462         case DW_OP_lit29:
8463         case DW_OP_lit30:
8464         case DW_OP_lit31:
8465           stack[++stacki] = op - DW_OP_lit0;
8466           break;
8467
8468         case DW_OP_reg0:
8469         case DW_OP_reg1:
8470         case DW_OP_reg2:
8471         case DW_OP_reg3:
8472         case DW_OP_reg4:
8473         case DW_OP_reg5:
8474         case DW_OP_reg6:
8475         case DW_OP_reg7:
8476         case DW_OP_reg8:
8477         case DW_OP_reg9:
8478         case DW_OP_reg10:
8479         case DW_OP_reg11:
8480         case DW_OP_reg12:
8481         case DW_OP_reg13:
8482         case DW_OP_reg14:
8483         case DW_OP_reg15:
8484         case DW_OP_reg16:
8485         case DW_OP_reg17:
8486         case DW_OP_reg18:
8487         case DW_OP_reg19:
8488         case DW_OP_reg20:
8489         case DW_OP_reg21:
8490         case DW_OP_reg22:
8491         case DW_OP_reg23:
8492         case DW_OP_reg24:
8493         case DW_OP_reg25:
8494         case DW_OP_reg26:
8495         case DW_OP_reg27:
8496         case DW_OP_reg28:
8497         case DW_OP_reg29:
8498         case DW_OP_reg30:
8499         case DW_OP_reg31:
8500           isreg = 1;
8501           stack[++stacki] = op - DW_OP_reg0;
8502           if (i < size)
8503             dwarf2_complex_location_expr_complaint ();
8504           break;
8505
8506         case DW_OP_regx:
8507           isreg = 1;
8508           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8509           i += bytes_read;
8510           stack[++stacki] = unsnd;
8511           if (i < size)
8512             dwarf2_complex_location_expr_complaint ();
8513           break;
8514
8515         case DW_OP_addr:
8516           stack[++stacki] = read_address (objfile->obfd, &data[i],
8517                                           cu, &bytes_read);
8518           i += bytes_read;
8519           break;
8520
8521         case DW_OP_const1u:
8522           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
8523           i += 1;
8524           break;
8525
8526         case DW_OP_const1s:
8527           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
8528           i += 1;
8529           break;
8530
8531         case DW_OP_const2u:
8532           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
8533           i += 2;
8534           break;
8535
8536         case DW_OP_const2s:
8537           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
8538           i += 2;
8539           break;
8540
8541         case DW_OP_const4u:
8542           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
8543           i += 4;
8544           break;
8545
8546         case DW_OP_const4s:
8547           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
8548           i += 4;
8549           break;
8550
8551         case DW_OP_constu:
8552           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
8553                                                   &bytes_read);
8554           i += bytes_read;
8555           break;
8556
8557         case DW_OP_consts:
8558           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
8559           i += bytes_read;
8560           break;
8561
8562         case DW_OP_dup:
8563           stack[stacki + 1] = stack[stacki];
8564           stacki++;
8565           break;
8566
8567         case DW_OP_plus:
8568           stack[stacki - 1] += stack[stacki];
8569           stacki--;
8570           break;
8571
8572         case DW_OP_plus_uconst:
8573           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
8574           i += bytes_read;
8575           break;
8576
8577         case DW_OP_minus:
8578           stack[stacki - 1] -= stack[stacki];
8579           stacki--;
8580           break;
8581
8582         case DW_OP_deref:
8583           /* If we're not the last op, then we definitely can't encode
8584              this using GDB's address_class enum.  This is valid for partial
8585              global symbols, although the variable's address will be bogus
8586              in the psymtab.  */
8587           if (i < size)
8588             dwarf2_complex_location_expr_complaint ();
8589           break;
8590
8591         case DW_OP_GNU_push_tls_address:
8592           /* The top of the stack has the offset from the beginning
8593              of the thread control block at which the variable is located.  */
8594           /* Nothing should follow this operator, so the top of stack would
8595              be returned.  */
8596           /* This is valid for partial global symbols, but the variable's
8597              address will be bogus in the psymtab.  */
8598           if (i < size)
8599             dwarf2_complex_location_expr_complaint ();
8600           break;
8601
8602         default:
8603           complaint (&symfile_complaints, "unsupported stack op: '%s'",
8604                      dwarf_stack_op_name (op));
8605           return (stack[stacki]);
8606         }
8607     }
8608   return (stack[stacki]);
8609 }
8610
8611 /* memory allocation interface */
8612
8613 static struct dwarf_block *
8614 dwarf_alloc_block (struct dwarf2_cu *cu)
8615 {
8616   struct dwarf_block *blk;
8617
8618   blk = (struct dwarf_block *)
8619     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
8620   return (blk);
8621 }
8622
8623 static struct abbrev_info *
8624 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
8625 {
8626   struct abbrev_info *abbrev;
8627
8628   abbrev = (struct abbrev_info *)
8629     obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
8630   memset (abbrev, 0, sizeof (struct abbrev_info));
8631   return (abbrev);
8632 }
8633
8634 static struct die_info *
8635 dwarf_alloc_die (void)
8636 {
8637   struct die_info *die;
8638
8639   die = (struct die_info *) xmalloc (sizeof (struct die_info));
8640   memset (die, 0, sizeof (struct die_info));
8641   return (die);
8642 }
8643
8644 \f
8645 /* Macro support.  */
8646
8647
8648 /* Return the full name of file number I in *LH's file name table.
8649    Use COMP_DIR as the name of the current directory of the
8650    compilation.  The result is allocated using xmalloc; the caller is
8651    responsible for freeing it.  */
8652 static char *
8653 file_full_name (int file, struct line_header *lh, const char *comp_dir)
8654 {
8655   struct file_entry *fe = &lh->file_names[file - 1];
8656   
8657   if (IS_ABSOLUTE_PATH (fe->name))
8658     return xstrdup (fe->name);
8659   else
8660     {
8661       const char *dir;
8662       int dir_len;
8663       char *full_name;
8664
8665       if (fe->dir_index)
8666         dir = lh->include_dirs[fe->dir_index - 1];
8667       else
8668         dir = comp_dir;
8669
8670       if (dir)
8671         {
8672           dir_len = strlen (dir);
8673           full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
8674           strcpy (full_name, dir);
8675           full_name[dir_len] = '/';
8676           strcpy (full_name + dir_len + 1, fe->name);
8677           return full_name;
8678         }
8679       else
8680         return xstrdup (fe->name);
8681     }
8682 }
8683
8684
8685 static struct macro_source_file *
8686 macro_start_file (int file, int line,
8687                   struct macro_source_file *current_file,
8688                   const char *comp_dir,
8689                   struct line_header *lh, struct objfile *objfile)
8690 {
8691   /* The full name of this source file.  */
8692   char *full_name = file_full_name (file, lh, comp_dir);
8693
8694   /* We don't create a macro table for this compilation unit
8695      at all until we actually get a filename.  */
8696   if (! pending_macros)
8697     pending_macros = new_macro_table (&objfile->objfile_obstack,
8698                                       objfile->macro_cache);
8699
8700   if (! current_file)
8701     /* If we have no current file, then this must be the start_file
8702        directive for the compilation unit's main source file.  */
8703     current_file = macro_set_main (pending_macros, full_name);
8704   else
8705     current_file = macro_include (current_file, line, full_name);
8706
8707   xfree (full_name);
8708               
8709   return current_file;
8710 }
8711
8712
8713 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
8714    followed by a null byte.  */
8715 static char *
8716 copy_string (const char *buf, int len)
8717 {
8718   char *s = xmalloc (len + 1);
8719   memcpy (s, buf, len);
8720   s[len] = '\0';
8721
8722   return s;
8723 }
8724
8725
8726 static const char *
8727 consume_improper_spaces (const char *p, const char *body)
8728 {
8729   if (*p == ' ')
8730     {
8731       complaint (&symfile_complaints,
8732                  "macro definition contains spaces in formal argument list:\n`%s'",
8733                  body);
8734
8735       while (*p == ' ')
8736         p++;
8737     }
8738
8739   return p;
8740 }
8741
8742
8743 static void
8744 parse_macro_definition (struct macro_source_file *file, int line,
8745                         const char *body)
8746 {
8747   const char *p;
8748
8749   /* The body string takes one of two forms.  For object-like macro
8750      definitions, it should be:
8751
8752         <macro name> " " <definition>
8753
8754      For function-like macro definitions, it should be:
8755
8756         <macro name> "() " <definition>
8757      or
8758         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
8759
8760      Spaces may appear only where explicitly indicated, and in the
8761      <definition>.
8762
8763      The Dwarf 2 spec says that an object-like macro's name is always
8764      followed by a space, but versions of GCC around March 2002 omit
8765      the space when the macro's definition is the empty string. 
8766
8767      The Dwarf 2 spec says that there should be no spaces between the
8768      formal arguments in a function-like macro's formal argument list,
8769      but versions of GCC around March 2002 include spaces after the
8770      commas.  */
8771
8772
8773   /* Find the extent of the macro name.  The macro name is terminated
8774      by either a space or null character (for an object-like macro) or
8775      an opening paren (for a function-like macro).  */
8776   for (p = body; *p; p++)
8777     if (*p == ' ' || *p == '(')
8778       break;
8779
8780   if (*p == ' ' || *p == '\0')
8781     {
8782       /* It's an object-like macro.  */
8783       int name_len = p - body;
8784       char *name = copy_string (body, name_len);
8785       const char *replacement;
8786
8787       if (*p == ' ')
8788         replacement = body + name_len + 1;
8789       else
8790         {
8791           dwarf2_macro_malformed_definition_complaint (body);
8792           replacement = body + name_len;
8793         }
8794       
8795       macro_define_object (file, line, name, replacement);
8796
8797       xfree (name);
8798     }
8799   else if (*p == '(')
8800     {
8801       /* It's a function-like macro.  */
8802       char *name = copy_string (body, p - body);
8803       int argc = 0;
8804       int argv_size = 1;
8805       char **argv = xmalloc (argv_size * sizeof (*argv));
8806
8807       p++;
8808
8809       p = consume_improper_spaces (p, body);
8810
8811       /* Parse the formal argument list.  */
8812       while (*p && *p != ')')
8813         {
8814           /* Find the extent of the current argument name.  */
8815           const char *arg_start = p;
8816
8817           while (*p && *p != ',' && *p != ')' && *p != ' ')
8818             p++;
8819
8820           if (! *p || p == arg_start)
8821             dwarf2_macro_malformed_definition_complaint (body);
8822           else
8823             {
8824               /* Make sure argv has room for the new argument.  */
8825               if (argc >= argv_size)
8826                 {
8827                   argv_size *= 2;
8828                   argv = xrealloc (argv, argv_size * sizeof (*argv));
8829                 }
8830
8831               argv[argc++] = copy_string (arg_start, p - arg_start);
8832             }
8833
8834           p = consume_improper_spaces (p, body);
8835
8836           /* Consume the comma, if present.  */
8837           if (*p == ',')
8838             {
8839               p++;
8840
8841               p = consume_improper_spaces (p, body);
8842             }
8843         }
8844
8845       if (*p == ')')
8846         {
8847           p++;
8848
8849           if (*p == ' ')
8850             /* Perfectly formed definition, no complaints.  */
8851             macro_define_function (file, line, name,
8852                                    argc, (const char **) argv, 
8853                                    p + 1);
8854           else if (*p == '\0')
8855             {
8856               /* Complain, but do define it.  */
8857               dwarf2_macro_malformed_definition_complaint (body);
8858               macro_define_function (file, line, name,
8859                                      argc, (const char **) argv, 
8860                                      p);
8861             }
8862           else
8863             /* Just complain.  */
8864             dwarf2_macro_malformed_definition_complaint (body);
8865         }
8866       else
8867         /* Just complain.  */
8868         dwarf2_macro_malformed_definition_complaint (body);
8869
8870       xfree (name);
8871       {
8872         int i;
8873
8874         for (i = 0; i < argc; i++)
8875           xfree (argv[i]);
8876       }
8877       xfree (argv);
8878     }
8879   else
8880     dwarf2_macro_malformed_definition_complaint (body);
8881 }
8882
8883
8884 static void
8885 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
8886                      char *comp_dir, bfd *abfd,
8887                      struct dwarf2_cu *cu)
8888 {
8889   char *mac_ptr, *mac_end;
8890   struct macro_source_file *current_file = 0;
8891
8892   if (dwarf2_per_objfile->macinfo_buffer == NULL)
8893     {
8894       complaint (&symfile_complaints, "missing .debug_macinfo section");
8895       return;
8896     }
8897
8898   mac_ptr = dwarf2_per_objfile->macinfo_buffer + offset;
8899   mac_end = dwarf2_per_objfile->macinfo_buffer
8900     + dwarf2_per_objfile->macinfo_size;
8901
8902   for (;;)
8903     {
8904       enum dwarf_macinfo_record_type macinfo_type;
8905
8906       /* Do we at least have room for a macinfo type byte?  */
8907       if (mac_ptr >= mac_end)
8908         {
8909           dwarf2_macros_too_long_complaint ();
8910           return;
8911         }
8912
8913       macinfo_type = read_1_byte (abfd, mac_ptr);
8914       mac_ptr++;
8915
8916       switch (macinfo_type)
8917         {
8918           /* A zero macinfo type indicates the end of the macro
8919              information.  */
8920         case 0:
8921           return;
8922
8923         case DW_MACINFO_define:
8924         case DW_MACINFO_undef:
8925           {
8926             int bytes_read;
8927             int line;
8928             char *body;
8929
8930             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8931             mac_ptr += bytes_read;
8932             body = read_string (abfd, mac_ptr, &bytes_read);
8933             mac_ptr += bytes_read;
8934
8935             if (! current_file)
8936               complaint (&symfile_complaints,
8937                          "debug info gives macro %s outside of any file: %s",
8938                          macinfo_type ==
8939                          DW_MACINFO_define ? "definition" : macinfo_type ==
8940                          DW_MACINFO_undef ? "undefinition" :
8941                          "something-or-other", body);
8942             else
8943               {
8944                 if (macinfo_type == DW_MACINFO_define)
8945                   parse_macro_definition (current_file, line, body);
8946                 else if (macinfo_type == DW_MACINFO_undef)
8947                   macro_undef (current_file, line, body);
8948               }
8949           }
8950           break;
8951
8952         case DW_MACINFO_start_file:
8953           {
8954             int bytes_read;
8955             int line, file;
8956
8957             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8958             mac_ptr += bytes_read;
8959             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
8960             mac_ptr += bytes_read;
8961
8962             current_file = macro_start_file (file, line,
8963                                              current_file, comp_dir,
8964                                              lh, cu->objfile);
8965           }
8966           break;
8967
8968         case DW_MACINFO_end_file:
8969           if (! current_file)
8970             complaint (&symfile_complaints,
8971                        "macro debug info has an unmatched `close_file' directive");
8972           else
8973             {
8974               current_file = current_file->included_by;
8975               if (! current_file)
8976                 {
8977                   enum dwarf_macinfo_record_type next_type;
8978
8979                   /* GCC circa March 2002 doesn't produce the zero
8980                      type byte marking the end of the compilation
8981                      unit.  Complain if it's not there, but exit no
8982                      matter what.  */
8983
8984                   /* Do we at least have room for a macinfo type byte?  */
8985                   if (mac_ptr >= mac_end)
8986                     {
8987                       dwarf2_macros_too_long_complaint ();
8988                       return;
8989                     }
8990
8991                   /* We don't increment mac_ptr here, so this is just
8992                      a look-ahead.  */
8993                   next_type = read_1_byte (abfd, mac_ptr);
8994                   if (next_type != 0)
8995                     complaint (&symfile_complaints,
8996                                "no terminating 0-type entry for macros in `.debug_macinfo' section");
8997
8998                   return;
8999                 }
9000             }
9001           break;
9002
9003         case DW_MACINFO_vendor_ext:
9004           {
9005             int bytes_read;
9006             int constant;
9007             char *string;
9008
9009             constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
9010             mac_ptr += bytes_read;
9011             string = read_string (abfd, mac_ptr, &bytes_read);
9012             mac_ptr += bytes_read;
9013
9014             /* We don't recognize any vendor extensions.  */
9015           }
9016           break;
9017         }
9018     }
9019 }
9020
9021 /* Check if the attribute's form is a DW_FORM_block*
9022    if so return true else false. */
9023 static int
9024 attr_form_is_block (struct attribute *attr)
9025 {
9026   return (attr == NULL ? 0 :
9027       attr->form == DW_FORM_block1
9028       || attr->form == DW_FORM_block2
9029       || attr->form == DW_FORM_block4
9030       || attr->form == DW_FORM_block);
9031 }
9032
9033 static void
9034 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
9035                              struct dwarf2_cu *cu)
9036 {
9037   if (attr->form == DW_FORM_data4 || attr->form == DW_FORM_data8)
9038     {
9039       struct dwarf2_loclist_baton *baton;
9040
9041       baton = obstack_alloc (&cu->objfile->objfile_obstack,
9042                              sizeof (struct dwarf2_loclist_baton));
9043       baton->objfile = cu->objfile;
9044
9045       /* We don't know how long the location list is, but make sure we
9046          don't run off the edge of the section.  */
9047       baton->size = dwarf2_per_objfile->loc_size - DW_UNSND (attr);
9048       baton->data = dwarf2_per_objfile->loc_buffer + DW_UNSND (attr);
9049       baton->base_address = cu->header.base_address;
9050       if (cu->header.base_known == 0)
9051         complaint (&symfile_complaints,
9052                    "Location list used without specifying the CU base address.");
9053
9054       SYMBOL_OPS (sym) = &dwarf2_loclist_funcs;
9055       SYMBOL_LOCATION_BATON (sym) = baton;
9056     }
9057   else
9058     {
9059       struct dwarf2_locexpr_baton *baton;
9060
9061       baton = obstack_alloc (&cu->objfile->objfile_obstack,
9062                              sizeof (struct dwarf2_locexpr_baton));
9063       baton->objfile = cu->objfile;
9064
9065       if (attr_form_is_block (attr))
9066         {
9067           /* Note that we're just copying the block's data pointer
9068              here, not the actual data.  We're still pointing into the
9069              info_buffer for SYM's objfile; right now we never release
9070              that buffer, but when we do clean up properly this may
9071              need to change.  */
9072           baton->size = DW_BLOCK (attr)->size;
9073           baton->data = DW_BLOCK (attr)->data;
9074         }
9075       else
9076         {
9077           dwarf2_invalid_attrib_class_complaint ("location description",
9078                                                  SYMBOL_NATURAL_NAME (sym));
9079           baton->size = 0;
9080           baton->data = NULL;
9081         }
9082       
9083       SYMBOL_OPS (sym) = &dwarf2_locexpr_funcs;
9084       SYMBOL_LOCATION_BATON (sym) = baton;
9085     }
9086 }
9087
9088 /* Locate the compilation unit from CU's objfile which contains the
9089    DIE at OFFSET.  Returns NULL on failure.  */
9090
9091 static struct dwarf2_per_cu_data *
9092 dwarf2_find_containing_comp_unit (unsigned long offset,
9093                                   struct objfile *objfile)
9094 {
9095   struct dwarf2_per_cu_data *this_cu;
9096   int low, high;
9097
9098   if (dwarf2_per_objfile->all_comp_units == NULL)
9099     error ("Dwarf Error: offset 0x%lx points outside this "
9100            "compilation unit [in module %s]",
9101            offset, bfd_get_filename (objfile->obfd));
9102
9103   low = 0;
9104   high = dwarf2_per_objfile->n_comp_units - 1;
9105   while (high > low)
9106     {
9107       int mid = low + (high - low) / 2;
9108       if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
9109         high = mid;
9110       else
9111         low = mid + 1;
9112     }
9113   gdb_assert (low == high);
9114   if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
9115     {
9116       gdb_assert (low > 0);
9117       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
9118       return dwarf2_per_objfile->all_comp_units[low-1];
9119     }
9120   else
9121     {
9122       this_cu = dwarf2_per_objfile->all_comp_units[low];
9123       if (low == dwarf2_per_objfile->n_comp_units - 1
9124           && offset >= this_cu->offset + this_cu->length)
9125         error ("invalid dwarf2 offset %ld", offset);
9126       gdb_assert (offset < this_cu->offset + this_cu->length);
9127       return this_cu;
9128     }
9129 }
9130
9131 static struct dwarf2_per_cu_data *
9132 dwarf2_find_comp_unit (unsigned long offset, struct objfile *objfile)
9133 {
9134   struct dwarf2_per_cu_data *this_cu;
9135   this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9136   if (this_cu->offset != offset)
9137     error ("no compilation unit with offset %ld\n", offset);
9138   return this_cu;
9139 }
9140
9141 /* Release one cached compilation unit, CU.  We unlink it from the tree
9142    of compilation units, but we don't remove it from the read_in_chain;
9143    the caller is responsible for that.  */
9144
9145 static void
9146 free_one_comp_unit (void *data)
9147 {
9148   struct dwarf2_cu *cu = data;
9149
9150   if (cu->per_cu != NULL)
9151     cu->per_cu->cu = NULL;
9152   cu->per_cu = NULL;
9153
9154   obstack_free (&cu->comp_unit_obstack, NULL);
9155
9156   xfree (cu);
9157 }
9158
9159 /* This cleanup function is passed the address of a dwarf2_cu on the stack
9160    when we're finished with it.  We can't free the pointer itself, but be
9161    sure to unlink it from the cache.  Also release any associated storage
9162    and perform cache maintenance.
9163
9164    Only used during partial symbol parsing.  */
9165
9166 static void
9167 free_stack_comp_unit (void *data)
9168 {
9169   struct dwarf2_cu *cu = data;
9170
9171   obstack_free (&cu->comp_unit_obstack, NULL);
9172   cu->partial_dies = NULL;
9173
9174   if (cu->per_cu != NULL)
9175     {
9176       /* This compilation unit is on the stack in our caller, so we
9177          should not xfree it.  Just unlink it.  */
9178       cu->per_cu->cu = NULL;
9179       cu->per_cu = NULL;
9180
9181       /* If we had a per-cu pointer, then we may have other compilation
9182          units loaded, so age them now.  */
9183       age_cached_comp_units ();
9184     }
9185 }
9186
9187 /* Free all cached compilation units.  */
9188
9189 static void
9190 free_cached_comp_units (void *data)
9191 {
9192   struct dwarf2_per_cu_data *per_cu, **last_chain;
9193
9194   per_cu = dwarf2_per_objfile->read_in_chain;
9195   last_chain = &dwarf2_per_objfile->read_in_chain;
9196   while (per_cu != NULL)
9197     {
9198       struct dwarf2_per_cu_data *next_cu;
9199
9200       next_cu = per_cu->cu->read_in_chain;
9201
9202       free_one_comp_unit (per_cu->cu);
9203       *last_chain = next_cu;
9204
9205       per_cu = next_cu;
9206     }
9207 }
9208
9209 /* Increase the age counter on each cached compilation unit, and free
9210    any that are too old.  */
9211
9212 static void
9213 age_cached_comp_units (void)
9214 {
9215   struct dwarf2_per_cu_data *per_cu, **last_chain;
9216
9217   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
9218   per_cu = dwarf2_per_objfile->read_in_chain;
9219   while (per_cu != NULL)
9220     {
9221       per_cu->cu->last_used ++;
9222       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
9223         dwarf2_mark (per_cu->cu);
9224       per_cu = per_cu->cu->read_in_chain;
9225     }
9226
9227   per_cu = dwarf2_per_objfile->read_in_chain;
9228   last_chain = &dwarf2_per_objfile->read_in_chain;
9229   while (per_cu != NULL)
9230     {
9231       struct dwarf2_per_cu_data *next_cu;
9232
9233       next_cu = per_cu->cu->read_in_chain;
9234
9235       if (!per_cu->cu->mark)
9236         {
9237           free_one_comp_unit (per_cu->cu);
9238           *last_chain = next_cu;
9239         }
9240       else
9241         last_chain = &per_cu->cu->read_in_chain;
9242
9243       per_cu = next_cu;
9244     }
9245 }
9246
9247 /* Remove a single compilation unit from the cache.  */
9248
9249 static void
9250 free_one_cached_comp_unit (void *target_cu)
9251 {
9252   struct dwarf2_per_cu_data *per_cu, **last_chain;
9253
9254   per_cu = dwarf2_per_objfile->read_in_chain;
9255   last_chain = &dwarf2_per_objfile->read_in_chain;
9256   while (per_cu != NULL)
9257     {
9258       struct dwarf2_per_cu_data *next_cu;
9259
9260       next_cu = per_cu->cu->read_in_chain;
9261
9262       if (per_cu->cu == target_cu)
9263         {
9264           free_one_comp_unit (per_cu->cu);
9265           *last_chain = next_cu;
9266           break;
9267         }
9268       else
9269         last_chain = &per_cu->cu->read_in_chain;
9270
9271       per_cu = next_cu;
9272     }
9273 }
9274
9275 /* A pair of DIE offset and GDB type pointer.  We store these
9276    in a hash table separate from the DIEs, and preserve them
9277    when the DIEs are flushed out of cache.  */
9278
9279 struct dwarf2_offset_and_type
9280 {
9281   unsigned int offset;
9282   struct type *type;
9283 };
9284
9285 /* Hash function for a dwarf2_offset_and_type.  */
9286
9287 static hashval_t
9288 offset_and_type_hash (const void *item)
9289 {
9290   const struct dwarf2_offset_and_type *ofs = item;
9291   return ofs->offset;
9292 }
9293
9294 /* Equality function for a dwarf2_offset_and_type.  */
9295
9296 static int
9297 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
9298 {
9299   const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
9300   const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
9301   return ofs_lhs->offset == ofs_rhs->offset;
9302 }
9303
9304 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
9305    table if necessary.  */
9306
9307 static void
9308 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
9309 {
9310   struct dwarf2_offset_and_type **slot, ofs;
9311
9312   die->type = type;
9313
9314   if (cu->per_cu == NULL)
9315     return;
9316
9317   if (cu->per_cu->type_hash == NULL)
9318     cu->per_cu->type_hash
9319       = htab_create_alloc_ex (cu->header.length / 24,
9320                               offset_and_type_hash,
9321                               offset_and_type_eq,
9322                               NULL,
9323                               &cu->objfile->objfile_obstack,
9324                               hashtab_obstack_allocate,
9325                               dummy_obstack_deallocate);
9326
9327   ofs.offset = die->offset;
9328   ofs.type = type;
9329   slot = (struct dwarf2_offset_and_type **)
9330     htab_find_slot_with_hash (cu->per_cu->type_hash, &ofs, ofs.offset, INSERT);
9331   *slot = obstack_alloc (&cu->objfile->objfile_obstack, sizeof (**slot));
9332   **slot = ofs;
9333 }
9334
9335 #if 0
9336
9337 /* Find the type for DIE in TYPE_HASH, or return NULL if DIE does not
9338    have a saved type.  */
9339
9340 static struct type *
9341 get_die_type (struct die_info *die, htab_t type_hash)
9342 {
9343   struct dwarf2_offset_and_type *slot, ofs;
9344
9345   ofs.offset = die->offset;
9346   slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
9347   if (slot)
9348     return slot->type;
9349   else
9350     return NULL;
9351 }
9352
9353 /* Restore the types of the DIE tree starting at START_DIE from the hash
9354    table saved in CU.  */
9355
9356 static void
9357 reset_die_and_siblings_types (struct die_info *start_die, struct dwarf2_cu *cu)
9358 {
9359   struct die_info *die;
9360
9361   if (cu->per_cu->type_hash == NULL)
9362     return;
9363
9364   for (die = start_die; die != NULL; die = die->sibling)
9365     {
9366       die->type = get_die_type (die, cu->per_cu->type_hash);
9367       if (die->child != NULL)
9368         reset_die_and_siblings_types (die->child, cu);
9369     }
9370 }
9371
9372 #endif
9373
9374 /* Set the mark field in CU and in every other compilation unit in the
9375    cache that we must keep because we are keeping CU.  */
9376
9377 static void
9378 dwarf2_mark (struct dwarf2_cu *cu)
9379 {
9380   if (cu->mark)
9381     return;
9382   cu->mark = 1;
9383 }
9384
9385 static void
9386 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
9387 {
9388   while (per_cu)
9389     {
9390       per_cu->cu->mark = 0;
9391       per_cu = per_cu->cu->read_in_chain;
9392     }
9393 }
9394
9395 /* Allocation function for the libiberty hash table which uses an
9396    obstack.  */
9397
9398 static void *
9399 hashtab_obstack_allocate (void *data, size_t size, size_t count)
9400 {
9401   unsigned int total = size * count;
9402   void *ptr = obstack_alloc ((struct obstack *) data, total);
9403   memset (ptr, 0, total);
9404   return ptr;
9405 }
9406
9407 /* Trivial deallocation function for the libiberty splay tree and hash
9408    table - don't deallocate anything.  Rely on later deletion of the
9409    obstack.  */
9410
9411 static void
9412 dummy_obstack_deallocate (void *object, void *data)
9413 {
9414   return;
9415 }
9416
9417 /* Trivial hash function for partial_die_info: the hash value of a DIE
9418    is its offset in .debug_info for this objfile.  */
9419
9420 static hashval_t
9421 partial_die_hash (const void *item)
9422 {
9423   const struct partial_die_info *part_die = item;
9424   return part_die->offset;
9425 }
9426
9427 /* Trivial comparison function for partial_die_info structures: two DIEs
9428    are equal if they have the same offset.  */
9429
9430 static int
9431 partial_die_eq (const void *item_lhs, const void *item_rhs)
9432 {
9433   const struct partial_die_info *part_die_lhs = item_lhs;
9434   const struct partial_die_info *part_die_rhs = item_rhs;
9435   return part_die_lhs->offset == part_die_rhs->offset;
9436 }
9437
9438 static struct cmd_list_element *set_dwarf2_cmdlist;
9439 static struct cmd_list_element *show_dwarf2_cmdlist;
9440
9441 static void
9442 set_dwarf2_cmd (char *args, int from_tty)
9443 {
9444   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
9445 }
9446
9447 static void
9448 show_dwarf2_cmd (char *args, int from_tty)
9449
9450   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
9451 }
9452
9453 void _initialize_dwarf2_read (void);
9454
9455 void
9456 _initialize_dwarf2_read (void)
9457 {
9458   dwarf2_objfile_data_key = register_objfile_data ();
9459
9460   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd,
9461                   "Set DWARF 2 specific variables.\n"
9462                   "Configure DWARF 2 variables such as the cache size",
9463                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
9464                   0/*allow-unknown*/, &maintenance_set_cmdlist);
9465
9466   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd,
9467                   "Show DWARF 2 specific variables\n"
9468                   "Show DWARF 2 variables such as the cache size",
9469                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
9470                   0/*allow-unknown*/, &maintenance_show_cmdlist);
9471
9472   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
9473                             &dwarf2_max_cache_age,
9474                             "Set the upper bound on the age of cached "
9475                             "dwarf2 compilation units.",
9476                             "Show the upper bound on the age of cached "
9477                             "dwarf2 compilation units.",
9478                             "A higher limit means that cached "
9479                             "compilation units will be stored\n"
9480                             "in memory longer, and more total memory will "
9481                             "be used.  Zero disables\n"
9482                             "caching, which can slow down startup.",
9483                             "The upper bound on the age of cached "
9484                             "dwarf2 compilation units is %d.",
9485                             NULL, NULL, &set_dwarf2_cmdlist,
9486                             &show_dwarf2_cmdlist);
9487 }