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