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