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