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