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