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