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