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