Merge from vendor branch DIFFUTILS:
[dragonfly.git] / contrib / gdb / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2    Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
5    Inc.  with support from Florida State University (under contract
6    with the Ada Joint Program Office), and Silicon Graphics, Inc.
7    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
8    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
9    support in dwarfread.c
10
11 This file is part of GDB.
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or (at
16 your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "elf-bfd.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "symfile.h"
33 #include "objfiles.h"
34 #include "elf/dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "language.h"
39 #include "complaints.h"
40
41 #include <fcntl.h>
42 #include "gdb_string.h"
43 #include <sys/types.h>
44
45 /* .debug_info header for a compilation unit 
46    Because of alignment constraints, this structure has padding and cannot
47    be mapped directly onto the beginning of the .debug_info section.  */
48 typedef struct comp_unit_header
49   {
50     unsigned int length;        /* length of the .debug_info
51                                    contribution */
52     unsigned short version;     /* version number -- 2 for DWARF
53                                    version 2 */
54     unsigned int abbrev_offset; /* offset into .debug_abbrev section */
55     unsigned char addr_size;    /* byte size of an address -- 4 */
56   }
57 _COMP_UNIT_HEADER;
58 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
59
60 /* .debug_pubnames header
61    Because of alignment constraints, this structure has padding and cannot
62    be mapped directly onto the beginning of the .debug_info section.  */
63 typedef struct pubnames_header
64   {
65     unsigned int length;        /* length of the .debug_pubnames
66                                    contribution  */
67     unsigned char version;      /* version number -- 2 for DWARF
68                                    version 2 */
69     unsigned int info_offset;   /* offset into .debug_info section */
70     unsigned int info_size;     /* byte size of .debug_info section
71                                    portion */
72   }
73 _PUBNAMES_HEADER;
74 #define _ACTUAL_PUBNAMES_HEADER_SIZE 13
75
76 /* .debug_pubnames header
77    Because of alignment constraints, this structure has padding and cannot
78    be mapped directly onto the beginning of the .debug_info section.  */
79 typedef struct aranges_header
80   {
81     unsigned int length;        /* byte len of the .debug_aranges
82                                    contribution */
83     unsigned short version;     /* version number -- 2 for DWARF
84                                    version 2 */
85     unsigned int info_offset;   /* offset into .debug_info section */
86     unsigned char addr_size;    /* byte size of an address */
87     unsigned char seg_size;     /* byte size of segment descriptor */
88   }
89 _ARANGES_HEADER;
90 #define _ACTUAL_ARANGES_HEADER_SIZE 12
91
92 /* .debug_line statement program prologue
93    Because of alignment constraints, this structure has padding and cannot
94    be mapped directly onto the beginning of the .debug_info section.  */
95 typedef struct statement_prologue
96   {
97     unsigned int total_length;  /* byte length of the statement
98                                    information */
99     unsigned short version;     /* version number -- 2 for DWARF
100                                    version 2 */
101     unsigned int prologue_length;       /* # bytes between prologue &
102                                            stmt program */
103     unsigned char minimum_instruction_length;   /* byte size of
104                                                    smallest instr */
105     unsigned char default_is_stmt;      /* initial value of is_stmt
106                                            register */
107     char line_base;
108     unsigned char line_range;
109     unsigned char opcode_base;  /* number assigned to first special
110                                    opcode */
111     unsigned char *standard_opcode_lengths;
112   }
113 _STATEMENT_PROLOGUE;
114
115 /* offsets and sizes of debugging sections */
116
117 static file_ptr dwarf_info_offset;
118 static file_ptr dwarf_abbrev_offset;
119 static file_ptr dwarf_line_offset;
120 static file_ptr dwarf_pubnames_offset;
121 static file_ptr dwarf_aranges_offset;
122 static file_ptr dwarf_loc_offset;
123 static file_ptr dwarf_macinfo_offset;
124 static file_ptr dwarf_str_offset;
125
126 static unsigned int dwarf_info_size;
127 static unsigned int dwarf_abbrev_size;
128 static unsigned int dwarf_line_size;
129 static unsigned int dwarf_pubnames_size;
130 static unsigned int dwarf_aranges_size;
131 static unsigned int dwarf_loc_size;
132 static unsigned int dwarf_macinfo_size;
133 static unsigned int dwarf_str_size;
134
135 /* names of the debugging sections */
136
137 #define INFO_SECTION     ".debug_info"
138 #define ABBREV_SECTION   ".debug_abbrev"
139 #define LINE_SECTION     ".debug_line"
140 #define PUBNAMES_SECTION ".debug_pubnames"
141 #define ARANGES_SECTION  ".debug_aranges"
142 #define LOC_SECTION      ".debug_loc"
143 #define MACINFO_SECTION  ".debug_macinfo"
144 #define STR_SECTION      ".debug_str"
145
146 /* local data types */
147
148 /* The data in a compilation unit header looks like this.  */
149 struct comp_unit_head
150   {
151     unsigned int length;
152     short version;
153     unsigned int abbrev_offset;
154     unsigned char addr_size;
155   };
156
157 /* The data in the .debug_line statement prologue looks like this.  */
158 struct line_head
159   {
160     unsigned int total_length;
161     unsigned short version;
162     unsigned int prologue_length;
163     unsigned char minimum_instruction_length;
164     unsigned char default_is_stmt;
165     int line_base;
166     unsigned char line_range;
167     unsigned char opcode_base;
168     unsigned char *standard_opcode_lengths;
169   };
170
171 /* When we construct a partial symbol table entry we only
172    need this much information. */
173 struct partial_die_info
174   {
175     enum dwarf_tag tag;
176     unsigned char has_children;
177     unsigned char is_external;
178     unsigned char is_declaration;
179     unsigned char has_type;
180     unsigned int offset;
181     unsigned int abbrev;
182     char *name;
183     CORE_ADDR lowpc;
184     CORE_ADDR highpc;
185     struct dwarf_block *locdesc;
186     unsigned int language;
187     char *sibling;
188   };
189
190 /* This data structure holds the information of an abbrev. */
191 struct abbrev_info
192   {
193     unsigned int number;        /* number identifying abbrev */
194     enum dwarf_tag tag;         /* dwarf tag */
195     int has_children;           /* boolean */
196     unsigned int num_attrs;     /* number of attributes */
197     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
198     struct abbrev_info *next;   /* next in chain */
199   };
200
201 struct attr_abbrev
202   {
203     enum dwarf_attribute name;
204     enum dwarf_form form;
205   };
206
207 /* This data structure holds a complete die structure. */
208 struct die_info
209   {
210     enum dwarf_tag tag;          /* Tag indicating type of die */
211     unsigned short has_children; /* Does the die have children */
212     unsigned int abbrev;         /* Abbrev number */
213     unsigned int offset;         /* Offset in .debug_info section */
214     unsigned int num_attrs;      /* Number of attributes */
215     struct attribute *attrs;     /* An array of attributes */
216     struct die_info *next_ref;   /* Next die in ref hash table */
217     struct die_info *next;       /* Next die in linked list */
218     struct type *type;           /* Cached type information */
219   };
220
221 /* Attributes have a name and a value */
222 struct attribute
223   {
224     enum dwarf_attribute name;
225     enum dwarf_form form;
226     union
227       {
228         char *str;
229         struct dwarf_block *blk;
230         unsigned int unsnd;
231         int snd;
232         CORE_ADDR addr;
233       }
234     u;
235   };
236
237 /* Get at parts of an attribute structure */
238
239 #define DW_STRING(attr)    ((attr)->u.str)
240 #define DW_UNSND(attr)     ((attr)->u.unsnd)
241 #define DW_BLOCK(attr)     ((attr)->u.blk)
242 #define DW_SND(attr)       ((attr)->u.snd)
243 #define DW_ADDR(attr)      ((attr)->u.addr)
244
245 /* Blocks are a bunch of untyped bytes. */
246 struct dwarf_block
247   {
248     unsigned int size;
249     char *data;
250   };
251
252 /* We only hold one compilation unit's abbrevs in
253    memory at any one time.  */
254 #ifndef ABBREV_HASH_SIZE
255 #define ABBREV_HASH_SIZE 121
256 #endif
257 #ifndef ATTR_ALLOC_CHUNK
258 #define ATTR_ALLOC_CHUNK 4
259 #endif
260
261 static struct abbrev_info *dwarf2_abbrevs[ABBREV_HASH_SIZE];
262
263 /* A hash table of die offsets for following references.  */
264 #ifndef REF_HASH_SIZE
265 #define REF_HASH_SIZE 1021
266 #endif
267
268 static struct die_info *die_ref_table[REF_HASH_SIZE];
269
270 /* Obstack for allocating temporary storage used during symbol reading.  */
271 static struct obstack dwarf2_tmp_obstack;
272
273 /* Offset to the first byte of the current compilation unit header,
274    for resolving relative reference dies. */
275 static unsigned int cu_header_offset;
276
277 /* Allocate fields for structs, unions and enums in this size.  */
278 #ifndef DW_FIELD_ALLOC_CHUNK
279 #define DW_FIELD_ALLOC_CHUNK 4
280 #endif
281
282 /* The language we are debugging.  */
283 static enum language cu_language;
284 static const struct language_defn *cu_language_defn;
285
286 /* Actually data from the sections.  */
287 static char *dwarf_info_buffer;
288 static char *dwarf_abbrev_buffer;
289 static char *dwarf_line_buffer;
290
291 /* A zeroed version of a partial die for initialization purposes.  */
292 static struct partial_die_info zeroed_partial_die;
293
294 /* The generic symbol table building routines have separate lists for
295    file scope symbols and all all other scopes (local scopes).  So
296    we need to select the right one to pass to add_symbol_to_list().
297    We do it by keeping a pointer to the correct list in list_in_scope.
298
299    FIXME:  The original dwarf code just treated the file scope as the first
300    local scope, and all other local scopes as nested local scopes, and worked
301    fine.  Check to see if we really need to distinguish these
302    in buildsym.c.  */
303 static struct pending **list_in_scope = &file_symbols;
304
305 /* FIXME: The following variables pass additional information from
306    decode_locdesc to the caller.  */
307 static int optimized_out;       /* Kludge to identify optimized out variables */
308 static int isreg;               /* Kludge to identify register variables */
309 static int offreg;              /* Kludge to identify basereg references */
310 static int basereg;             /* Which base register is it relative to?  */
311 static int islocal;             /* Kludge to identify local variables */
312
313 /* DW_AT_frame_base values for the current function.
314    frame_base_reg is -1 if DW_AT_frame_base is missing, otherwise it
315    contains the register number for the frame register.
316    frame_base_offset is the offset from the frame register to the
317    virtual stack frame. */
318 static int frame_base_reg;
319 static CORE_ADDR frame_base_offset;
320
321 /* This value is added to each symbol value.  FIXME:  Generalize to 
322    the section_offsets structure used by dbxread (once this is done,
323    pass the appropriate section number to end_symtab).  */
324 static CORE_ADDR baseaddr;      /* Add to each symbol value */
325
326 /* We put a pointer to this structure in the read_symtab_private field
327    of the psymtab.
328    The complete dwarf information for an objfile is kept in the
329    psymbol_obstack, so that absolute die references can be handled.
330    Most of the information in this structure is related to an entire
331    object file and could be passed via the sym_private field of the objfile.
332    It is however conceivable that dwarf2 might not be the only type
333    of symbols read from an object file.  */
334
335 struct dwarf2_pinfo
336 {
337   /* Pointer to start of dwarf info buffer for the objfile.  */
338
339   char *dwarf_info_buffer;
340
341   /* Offset in dwarf_info_buffer for this compilation unit. */
342
343   unsigned long dwarf_info_offset;
344
345   /* Pointer to start of dwarf abbreviation buffer for the objfile.  */
346
347   char *dwarf_abbrev_buffer;
348
349   /* Size of dwarf abbreviation section for the objfile.  */
350
351   unsigned int dwarf_abbrev_size;
352
353   /* Pointer to start of dwarf line buffer for the objfile.  */
354
355   char *dwarf_line_buffer;
356 };
357
358 #define PST_PRIVATE(p) ((struct dwarf2_pinfo *)(p)->read_symtab_private)
359 #define DWARF_INFO_BUFFER(p) (PST_PRIVATE(p)->dwarf_info_buffer)
360 #define DWARF_INFO_OFFSET(p) (PST_PRIVATE(p)->dwarf_info_offset)
361 #define DWARF_ABBREV_BUFFER(p) (PST_PRIVATE(p)->dwarf_abbrev_buffer)
362 #define DWARF_ABBREV_SIZE(p) (PST_PRIVATE(p)->dwarf_abbrev_size)
363 #define DWARF_LINE_BUFFER(p) (PST_PRIVATE(p)->dwarf_line_buffer)
364
365 /* Maintain an array of referenced fundamental types for the current
366    compilation unit being read.  For DWARF version 1, we have to construct
367    the fundamental types on the fly, since no information about the
368    fundamental types is supplied.  Each such fundamental type is created by
369    calling a language dependent routine to create the type, and then a
370    pointer to that type is then placed in the array at the index specified
371    by it's FT_<TYPENAME> value.  The array has a fixed size set by the
372    FT_NUM_MEMBERS compile time constant, which is the number of predefined
373    fundamental types gdb knows how to construct.  */
374 static struct type *ftypes[FT_NUM_MEMBERS];     /* Fundamental types */
375
376 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
377    but this would require a corresponding change in unpack_field_as_long
378    and friends.  */
379 static int bits_per_byte = 8;
380
381 /* The routines that read and process dies for a C struct or C++ class
382    pass lists of data member fields and lists of member function fields
383    in an instance of a field_info structure, as defined below.  */
384 struct field_info
385 {
386   /* List of data member and baseclasses fields. */
387   struct nextfield
388     {
389       struct nextfield *next;
390       int accessibility;
391       int virtuality;
392       struct field field;
393     } *fields;
394
395   /* Number of fields.  */
396   int nfields;
397
398   /* Number of baseclasses.  */
399   int nbaseclasses;
400
401   /* Set if the accesibility of one of the fields is not public.  */
402   int non_public_fields;
403
404   /* Member function fields array, entries are allocated in the order they
405      are encountered in the object file.  */
406   struct nextfnfield
407     {
408       struct nextfnfield *next;
409       struct fn_field fnfield;
410     } *fnfields;
411
412   /* Member function fieldlist array, contains name of possibly overloaded
413      member function, number of overloaded member functions and a pointer
414      to the head of the member function field chain.  */
415   struct fnfieldlist
416     {
417       char *name;
418       int length;
419       struct nextfnfield *head;
420     } *fnfieldlists;
421
422   /* Number of entries in the fnfieldlists array.  */
423   int nfnfields;
424 };
425
426 /* FIXME: Kludge to mark a varargs function type for C++ member function
427    argument processing.  */
428 #define TYPE_FLAG_VARARGS       (1 << 10)
429
430 /* Dwarf2 has no clean way to discern C++ static and non-static member
431    functions. G++ helps GDB by marking the first parameter for non-static
432    member functions (which is the this pointer) as artificial.
433    We pass this information between dwarf2_add_member_fn and
434    read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
435 #define TYPE_FIELD_ARTIFICIAL   TYPE_FIELD_BITPOS
436
437 /* Various complaints about symbol reading that don't abort the process */
438
439 static struct complaint dwarf2_const_ignored =
440 {
441   "type qualifier 'const' ignored", 0, 0
442 };
443 static struct complaint dwarf2_volatile_ignored =
444 {
445   "type qualifier 'volatile' ignored", 0, 0
446 };
447 static struct complaint dwarf2_non_const_array_bound_ignored =
448 {
449   "non-constant array bounds form '%s' ignored", 0, 0
450 };
451 static struct complaint dwarf2_missing_line_number_section =
452 {
453   "missing .debug_line section", 0, 0
454 };
455 static struct complaint dwarf2_mangled_line_number_section =
456 {
457   "mangled .debug_line section", 0, 0
458 };
459 static struct complaint dwarf2_unsupported_die_ref_attr =
460 {
461   "unsupported die ref attribute form: '%s'", 0, 0
462 };
463 static struct complaint dwarf2_unsupported_stack_op =
464 {
465   "unsupported stack op: '%s'", 0, 0
466 };
467 static struct complaint dwarf2_unsupported_tag =
468 {
469   "unsupported tag: '%s'", 0, 0
470 };
471 static struct complaint dwarf2_unsupported_at_encoding =
472 {
473   "unsupported DW_AT_encoding: '%s'", 0, 0
474 };
475 static struct complaint dwarf2_unsupported_at_frame_base =
476 {
477   "unsupported DW_AT_frame_base for function '%s'", 0, 0
478 };
479 static struct complaint dwarf2_unexpected_tag =
480 {
481   "unexepected tag in read_type_die: '%s'", 0, 0
482 };
483 static struct complaint dwarf2_missing_at_frame_base =
484 {
485   "DW_AT_frame_base missing for DW_OP_fbreg", 0, 0
486 };
487 static struct complaint dwarf2_bad_static_member_name =
488 {
489   "unrecognized static data member name '%s'", 0, 0
490 };
491 static struct complaint dwarf2_unsupported_accessibility =
492 {
493   "unsupported accessibility %d", 0, 0
494 };
495 static struct complaint dwarf2_bad_member_name_complaint =
496 {
497   "cannot extract member name from '%s'", 0, 0
498 };
499 static struct complaint dwarf2_missing_member_fn_type_complaint =
500 {
501   "member function type missing for '%s'", 0, 0
502 };
503 static struct complaint dwarf2_vtbl_not_found_complaint =
504 {
505   "virtual function table pointer not found when defining class '%s'", 0, 0
506 };
507 static struct complaint dwarf2_absolute_sibling_complaint =
508 {
509   "ignoring absolute DW_AT_sibling", 0, 0
510 };
511 static struct complaint dwarf2_const_value_length_mismatch =
512 {
513   "const value length mismatch for '%s', got %d, expected %d", 0, 0
514 };
515 static struct complaint dwarf2_unsupported_const_value_attr =
516 {
517   "unsupported const value attribute form: '%s'", 0, 0
518 };
519
520 /* Remember the addr_size read from the dwarf.
521    If a target expects to link compilation units with differing address
522    sizes, gdb needs to be sure that the appropriate size is here for
523    whatever scope is currently getting read. */
524 static int address_size;
525
526 /* Some elf32 object file formats while linked for a 32 bit address
527    space contain debug information that has assumed 64 bit
528    addresses. Eg 64 bit MIPS target produced by GCC/GAS/LD where the
529    symbol table contains 32bit address values while its .debug_info
530    section contains 64 bit address values.
531    ADDRESS_SIGNIFICANT_SIZE specifies the number significant bits in
532    the ADDRESS_SIZE bytes read from the file */
533 static int address_significant_size;
534
535 /* Externals references.  */
536 extern int info_verbose;        /* From main.c; nonzero => verbose */
537
538 /* local function prototypes */
539
540 static void dwarf2_locate_sections PARAMS ((bfd *, asection *, PTR));
541
542 #if 0
543 static void dwarf2_build_psymtabs_easy PARAMS ((struct objfile *,
544                                                 struct section_offsets *,
545                                                 int));
546 #endif
547
548 static void dwarf2_build_psymtabs_hard PARAMS ((struct objfile *,
549                                                 struct section_offsets *,
550                                                 int));
551
552 static char *scan_partial_symbols PARAMS ((char *, struct objfile *,
553                                            CORE_ADDR *, CORE_ADDR *));
554
555 static void add_partial_symbol PARAMS ((struct partial_die_info *,
556                                         struct objfile *));
557
558 static void dwarf2_psymtab_to_symtab PARAMS ((struct partial_symtab *));
559
560 static void psymtab_to_symtab_1 PARAMS ((struct partial_symtab *));
561
562 static char *dwarf2_read_section PARAMS ((struct objfile *, file_ptr,
563                                           unsigned int));
564
565 static void dwarf2_read_abbrevs PARAMS ((bfd *, unsigned int));
566
567 static void dwarf2_empty_abbrev_table PARAMS ((PTR));
568
569 static struct abbrev_info *dwarf2_lookup_abbrev PARAMS ((unsigned int));
570
571 static char *read_partial_die PARAMS ((struct partial_die_info *,
572                                        bfd *, char *, int *));
573
574 static char *read_full_die PARAMS ((struct die_info **, bfd *, char *));
575
576 static char *read_attribute PARAMS ((struct attribute *, struct attr_abbrev *,
577                                      bfd *, char *));
578
579 static unsigned int read_1_byte PARAMS ((bfd *, char *));
580
581 static int read_1_signed_byte PARAMS ((bfd *, char *));
582
583 static unsigned int read_2_bytes PARAMS ((bfd *, char *));
584
585 static unsigned int read_4_bytes PARAMS ((bfd *, char *));
586
587 static unsigned int read_8_bytes PARAMS ((bfd *, char *));
588
589 static CORE_ADDR read_address PARAMS ((bfd *, char *));
590
591 static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
592
593 static char *read_string PARAMS ((bfd *, char *, unsigned int *));
594
595 static unsigned int read_unsigned_leb128 PARAMS ((bfd *, char *,
596                                                   unsigned int *));
597
598 static int read_signed_leb128 PARAMS ((bfd *, char *, unsigned int *));
599
600 static void set_cu_language PARAMS ((unsigned int));
601
602 static struct attribute *dwarf_attr PARAMS ((struct die_info *,
603                                              unsigned int));
604
605 static void dwarf_decode_lines PARAMS ((unsigned int, char *, bfd *));
606
607 static void dwarf2_start_subfile PARAMS ((char *, char *));
608
609 static struct symbol *new_symbol PARAMS ((struct die_info *, struct type *,
610                                           struct objfile *));
611
612 static void dwarf2_const_value PARAMS ((struct attribute *, struct symbol *,
613                                         struct objfile *));
614
615 static struct type *die_type PARAMS ((struct die_info *, struct objfile *));
616
617 static struct type *die_containing_type PARAMS ((struct die_info *,
618                                                  struct objfile *));
619
620 #if 0
621 static struct type *type_at_offset PARAMS ((unsigned int, struct objfile *));
622 #endif
623
624 static struct type *tag_type_to_type PARAMS ((struct die_info *,
625                                               struct objfile *));
626
627 static void read_type_die PARAMS ((struct die_info *, struct objfile *));
628
629 static void read_typedef PARAMS ((struct die_info *, struct objfile *));
630
631 static void read_base_type PARAMS ((struct die_info *, struct objfile *));
632
633 static void read_file_scope PARAMS ((struct die_info *, struct objfile *));
634
635 static void read_func_scope PARAMS ((struct die_info *, struct objfile *));
636
637 static void read_lexical_block_scope PARAMS ((struct die_info *,
638                                               struct objfile *));
639
640 static int dwarf2_get_pc_bounds PARAMS ((struct die_info *,
641                                          CORE_ADDR *, CORE_ADDR *,
642                                          struct objfile *));
643
644 static void dwarf2_add_field PARAMS ((struct field_info *, struct die_info *,
645                                       struct objfile *));
646
647 static void dwarf2_attach_fields_to_type PARAMS ((struct field_info *,
648                                                   struct type *, 
649                                                   struct objfile *));
650
651 static char *skip_member_fn_name PARAMS ((char *));
652
653 static void dwarf2_add_member_fn PARAMS ((struct field_info *,
654                                           struct die_info *, struct type *,
655                                           struct objfile *objfile));
656
657 static void dwarf2_attach_fn_fields_to_type PARAMS ((struct field_info *,
658                                                      struct type *,
659                                                      struct objfile *));
660
661 static void read_structure_scope PARAMS ((struct die_info *, struct objfile *));
662
663 static void read_common_block PARAMS ((struct die_info *, struct objfile *));
664
665 static void read_enumeration PARAMS ((struct die_info *, struct objfile *));
666
667 static struct type *dwarf_base_type PARAMS ((int, int, struct objfile *));
668
669 static CORE_ADDR decode_locdesc PARAMS ((struct dwarf_block *,
670                                          struct objfile *));
671
672 static void read_array_type PARAMS ((struct die_info *, struct objfile *));
673
674 static void read_tag_pointer_type PARAMS ((struct die_info *,
675                                            struct objfile *));
676
677 static void read_tag_ptr_to_member_type PARAMS ((struct die_info *,
678                                                  struct objfile *));
679
680 static void read_tag_reference_type PARAMS ((struct die_info *,
681                                              struct objfile *));
682
683 static void read_tag_const_type PARAMS ((struct die_info *, struct objfile *));
684
685 static void read_tag_volatile_type PARAMS ((struct die_info *,
686                                             struct objfile *));
687
688 static void read_tag_string_type PARAMS ((struct die_info *,
689                                           struct objfile *));
690
691 static void read_subroutine_type PARAMS ((struct die_info *,
692                                           struct objfile *));
693
694 struct die_info *read_comp_unit PARAMS ((char *, bfd *));
695
696 static void free_die_list PARAMS ((struct die_info *));
697
698 static void process_die PARAMS ((struct die_info *, struct objfile *));
699
700 static char *dwarf2_linkage_name PARAMS ((struct die_info *));
701
702 static char *dwarf_tag_name PARAMS ((unsigned int));
703
704 static char *dwarf_attr_name PARAMS ((unsigned int));
705
706 static char *dwarf_form_name PARAMS ((unsigned int));
707
708 static char *dwarf_stack_op_name PARAMS ((unsigned int));
709
710 static char *dwarf_bool_name PARAMS ((unsigned int));
711
712 static char *dwarf_type_encoding_name PARAMS ((unsigned int));
713
714 #if 0
715 static char *dwarf_cfi_name PARAMS ((unsigned int));
716
717 struct die_info *copy_die PARAMS ((struct die_info *));
718 #endif
719
720 struct die_info *sibling_die PARAMS ((struct die_info *));
721
722 void dump_die PARAMS ((struct die_info *));
723
724 void dump_die_list PARAMS ((struct die_info *));
725
726 void store_in_ref_table PARAMS ((unsigned int, struct die_info *));
727
728 static void dwarf2_empty_die_ref_table PARAMS ((void));
729
730 static unsigned int dwarf2_get_ref_die_offset PARAMS ((struct attribute *));
731
732 struct die_info *follow_die_ref PARAMS ((unsigned int));
733
734 static struct type *dwarf2_fundamental_type PARAMS ((struct objfile *, int));
735
736 /* memory allocation interface */
737
738 static void dwarf2_free_tmp_obstack PARAMS ((PTR));
739
740 static struct dwarf_block *dwarf_alloc_block PARAMS ((void));
741
742 static struct abbrev_info *dwarf_alloc_abbrev PARAMS ((void));
743
744 static struct die_info *dwarf_alloc_die PARAMS ((void));
745
746 /* Try to locate the sections we need for DWARF 2 debugging
747    information and return true if we have enough to do something.  */
748
749 int
750 dwarf2_has_info (abfd)
751      bfd *abfd;
752 {
753   dwarf_info_offset = dwarf_abbrev_offset = dwarf_line_offset = 0;
754   bfd_map_over_sections (abfd, dwarf2_locate_sections, NULL);
755   if (dwarf_info_offset && dwarf_abbrev_offset)
756     {
757       return 1;
758     }
759   else
760     {
761       return 0;
762     }
763 }
764
765 /* This function is mapped across the sections and remembers the
766    offset and size of each of the debugging sections we are interested
767    in.  */
768
769 static void
770 dwarf2_locate_sections (ignore_abfd, sectp, ignore_ptr)
771      bfd *ignore_abfd;
772      asection *sectp;
773      PTR ignore_ptr;
774 {
775   if (STREQ (sectp->name, INFO_SECTION))
776     {
777       dwarf_info_offset = sectp->filepos;
778       dwarf_info_size = bfd_get_section_size_before_reloc (sectp);
779     }
780   else if (STREQ (sectp->name, ABBREV_SECTION))
781     {
782       dwarf_abbrev_offset = sectp->filepos;
783       dwarf_abbrev_size = bfd_get_section_size_before_reloc (sectp);
784     }
785   else if (STREQ (sectp->name, LINE_SECTION))
786     {
787       dwarf_line_offset = sectp->filepos;
788       dwarf_line_size = bfd_get_section_size_before_reloc (sectp);
789     }
790   else if (STREQ (sectp->name, PUBNAMES_SECTION))
791     {
792       dwarf_pubnames_offset = sectp->filepos;
793       dwarf_pubnames_size = bfd_get_section_size_before_reloc (sectp);
794     }
795   else if (STREQ (sectp->name, ARANGES_SECTION))
796     {
797       dwarf_aranges_offset = sectp->filepos;
798       dwarf_aranges_size = bfd_get_section_size_before_reloc (sectp);
799     }
800   else if (STREQ (sectp->name, LOC_SECTION))
801     {
802       dwarf_loc_offset = sectp->filepos;
803       dwarf_loc_size = bfd_get_section_size_before_reloc (sectp);
804     }
805   else if (STREQ (sectp->name, MACINFO_SECTION))
806     {
807       dwarf_macinfo_offset = sectp->filepos;
808       dwarf_macinfo_size = bfd_get_section_size_before_reloc (sectp);
809     }
810   else if (STREQ (sectp->name, STR_SECTION))
811     {
812       dwarf_str_offset = sectp->filepos;
813       dwarf_str_size = bfd_get_section_size_before_reloc (sectp);
814     }
815 }
816
817 /* Build a partial symbol table.  */
818
819 void
820 dwarf2_build_psymtabs (objfile, section_offsets, mainline)
821     struct objfile *objfile;
822     struct section_offsets *section_offsets;
823     int mainline;
824 {
825
826   /* We definitely need the .debug_info and .debug_abbrev sections */
827
828   dwarf_info_buffer = dwarf2_read_section (objfile,
829                                            dwarf_info_offset,
830                                            dwarf_info_size);
831   dwarf_abbrev_buffer = dwarf2_read_section (objfile,
832                                              dwarf_abbrev_offset,
833                                              dwarf_abbrev_size);
834   dwarf_line_buffer = dwarf2_read_section (objfile,
835                                            dwarf_line_offset,
836                                            dwarf_line_size);
837
838   if (mainline || objfile->global_psymbols.size == 0 ||
839       objfile->static_psymbols.size == 0)
840     {
841       init_psymbol_list (objfile, 1024);
842     }
843
844 #if 0
845   if (dwarf_aranges_offset && dwarf_pubnames_offset)
846     {
847       /* Things are significanlty easier if we have .debug_aranges and
848          .debug_pubnames sections */
849
850       dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline);
851     }
852   else
853 #endif
854     /* only test this case for now */
855     {           
856       /* In this case we have to work a bit harder */
857       dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline);
858     }
859 }
860
861 #if 0
862 /* Build the partial symbol table from the information in the
863    .debug_pubnames and .debug_aranges sections.  */
864
865 static void
866 dwarf2_build_psymtabs_easy (objfile, section_offsets, mainline)
867      struct objfile *objfile;
868      struct section_offsets *section_offsets;
869      int mainline;
870 {
871   bfd *abfd = objfile->obfd;
872   char *aranges_buffer, *pubnames_buffer;
873   char *aranges_ptr, *pubnames_ptr;
874   unsigned int entry_length, version, info_offset, info_size;
875
876   pubnames_buffer = dwarf2_read_section (objfile,
877                                          dwarf_pubnames_offset,
878                                          dwarf_pubnames_size);
879   pubnames_ptr = pubnames_buffer;
880   while ((pubnames_ptr - pubnames_buffer) < dwarf_pubnames_size)
881     {
882       entry_length = read_4_bytes (abfd, pubnames_ptr);
883       pubnames_ptr += 4;
884       version = read_1_byte (abfd, pubnames_ptr);
885       pubnames_ptr += 1;
886       info_offset = read_4_bytes (abfd, pubnames_ptr);
887       pubnames_ptr += 4;
888       info_size = read_4_bytes (abfd, pubnames_ptr);
889       pubnames_ptr += 4;
890     }
891
892   aranges_buffer = dwarf2_read_section (objfile,
893                                         dwarf_aranges_offset,
894                                         dwarf_aranges_size);
895
896 }
897 #endif
898
899 /* Build the partial symbol table by doing a quick pass through the
900    .debug_info and .debug_abbrev sections.  */
901
902 static void
903 dwarf2_build_psymtabs_hard (objfile, section_offsets, mainline)
904      struct objfile *objfile;
905      struct section_offsets *section_offsets;
906      int mainline;
907 {
908   /* Instead of reading this into a big buffer, we should probably use
909      mmap()  on architectures that support it. (FIXME) */
910   bfd *abfd = objfile->obfd;
911   char *info_ptr, *abbrev_ptr;
912   char *beg_of_comp_unit;
913   struct comp_unit_head cu_header;
914   struct partial_die_info comp_unit_die;
915   struct partial_symtab *pst;
916   struct cleanup *back_to;
917   int comp_unit_has_pc_info;
918   CORE_ADDR lowpc, highpc;
919
920   /* Number of bytes of any addresses that are signficant */
921   address_significant_size = get_elf_backend_data (abfd)->s->arch_size / 8;
922
923   info_ptr = dwarf_info_buffer;
924   abbrev_ptr = dwarf_abbrev_buffer;
925
926   obstack_init (&dwarf2_tmp_obstack);
927   back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
928
929   while ((unsigned int) (info_ptr - dwarf_info_buffer)
930           + ((info_ptr - dwarf_info_buffer) % 4) < dwarf_info_size)
931     {
932       beg_of_comp_unit = info_ptr;
933       cu_header.length = read_4_bytes (abfd, info_ptr);
934       info_ptr += 4;
935       cu_header.version = read_2_bytes (abfd, info_ptr);
936       info_ptr += 2;
937       cu_header.abbrev_offset = read_4_bytes (abfd, info_ptr);
938       info_ptr += 4;
939       cu_header.addr_size = read_1_byte (abfd, info_ptr);
940       info_ptr += 1;
941       address_size = cu_header.addr_size;
942
943       if (cu_header.version != 2)
944         {
945           error ("Dwarf Error: wrong version in compilation unit header.");
946           return;
947         }
948       if (cu_header.abbrev_offset >= dwarf_abbrev_size)
949         {
950           error ("Dwarf Error: bad offset (0x%lx) in compilation unit header (offset 0x%lx + 6).",
951                  (long) cu_header.abbrev_offset,
952                  (long) (beg_of_comp_unit - dwarf_info_buffer));
953           return;
954         }
955       if (beg_of_comp_unit + cu_header.length + 4
956           > dwarf_info_buffer + dwarf_info_size)
957         {
958           error ("Dwarf Error: bad length (0x%lx) in compilation unit header (offset 0x%lx + 0).",
959                  (long) cu_header.length,
960                  (long) (beg_of_comp_unit - dwarf_info_buffer));
961           return;
962         }
963       if (address_size < address_significant_size)
964         {
965           error ("Dwarf Error: bad address size (%ld) in compilation unit header (offset 0x%lx + 11).",
966                  (long) cu_header.addr_size,
967                  (long) (beg_of_comp_unit - dwarf_info_buffer));
968         }
969
970       /* Read the abbrevs for this compilation unit into a table */
971       dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
972       make_cleanup (dwarf2_empty_abbrev_table, NULL);
973
974       /* Read the compilation unit die */
975       info_ptr = read_partial_die (&comp_unit_die, abfd,
976                                    info_ptr, &comp_unit_has_pc_info);
977
978       /* Set the language we're debugging */
979       set_cu_language (comp_unit_die.language);
980
981       /* Allocate a new partial symbol table structure */
982       pst = start_psymtab_common (objfile, section_offsets,
983                                   comp_unit_die.name ? comp_unit_die.name : "",
984                                   comp_unit_die.lowpc,
985                                   objfile->global_psymbols.next,
986                                   objfile->static_psymbols.next);
987
988       pst->read_symtab_private = (char *)
989         obstack_alloc (&objfile->psymbol_obstack, sizeof (struct dwarf2_pinfo));
990       cu_header_offset = beg_of_comp_unit - dwarf_info_buffer;
991       DWARF_INFO_BUFFER(pst) = dwarf_info_buffer;
992       DWARF_INFO_OFFSET(pst) = beg_of_comp_unit - dwarf_info_buffer;
993       DWARF_ABBREV_BUFFER(pst) = dwarf_abbrev_buffer;
994       DWARF_ABBREV_SIZE(pst) = dwarf_abbrev_size;
995       DWARF_LINE_BUFFER(pst) = dwarf_line_buffer;
996       baseaddr = ANOFFSET (section_offsets, 0);
997
998       /* Store the function that reads in the rest of the symbol table */
999       pst->read_symtab = dwarf2_psymtab_to_symtab;
1000
1001       /* Check if comp unit has_children.
1002          If so, read the rest of the partial symbols from this comp unit.
1003          If not, there's no more debug_info for this comp unit. */
1004       if (comp_unit_die.has_children)
1005         {
1006           info_ptr = scan_partial_symbols (info_ptr, objfile, &lowpc, &highpc);
1007
1008           /* If the compilation unit didn't have an explicit address range,
1009              then use the information extracted from its child dies.  */
1010           if (!comp_unit_has_pc_info)
1011             {
1012               comp_unit_die.lowpc  = lowpc;
1013               comp_unit_die.highpc = highpc;
1014             }
1015         }
1016       pst->textlow  = comp_unit_die.lowpc + baseaddr;
1017       pst->texthigh = comp_unit_die.highpc + baseaddr;
1018
1019       pst->n_global_syms = objfile->global_psymbols.next -
1020         (objfile->global_psymbols.list + pst->globals_offset);
1021       pst->n_static_syms = objfile->static_psymbols.next -
1022         (objfile->static_psymbols.list + pst->statics_offset);
1023       sort_pst_symbols (pst);
1024
1025       /* If there is already a psymtab or symtab for a file of this
1026          name, remove it. (If there is a symtab, more drastic things
1027          also happen.) This happens in VxWorks.  */
1028       free_named_symtabs (pst->filename);
1029
1030       info_ptr = beg_of_comp_unit + cu_header.length + 4;
1031     }
1032   do_cleanups (back_to);
1033 }
1034
1035 /* Read in all interesting dies to the end of the compilation unit.  */
1036
1037 static char *
1038 scan_partial_symbols (info_ptr, objfile, lowpc, highpc)
1039      char *info_ptr;
1040      struct objfile *objfile;
1041      CORE_ADDR *lowpc;
1042      CORE_ADDR *highpc;
1043 {
1044   bfd *abfd = objfile->obfd;
1045   struct partial_die_info pdi;
1046
1047   /* This function is called after we've read in the comp_unit_die in
1048      order to read its children.  We start the nesting level at 1 since
1049      we have pushed 1 level down in order to read the comp unit's children.
1050      The comp unit itself is at level 0, so we stop reading when we pop
1051      back to that level. */
1052
1053   int nesting_level = 1;
1054   int has_pc_info;
1055   
1056   *lowpc  = ((CORE_ADDR) -1);
1057   *highpc = ((CORE_ADDR) 0);
1058
1059   while (nesting_level)
1060     {
1061       info_ptr = read_partial_die (&pdi, abfd, info_ptr, &has_pc_info);
1062
1063       if (pdi.name)
1064         {
1065           switch (pdi.tag)
1066             {
1067             case DW_TAG_subprogram:
1068               if (has_pc_info)
1069                 {
1070                   if (pdi.lowpc < *lowpc)
1071                     {
1072                       *lowpc = pdi.lowpc;
1073                     }
1074                   if (pdi.highpc > *highpc)
1075                     {
1076                       *highpc = pdi.highpc;
1077                     }
1078                   if ((pdi.is_external || nesting_level == 1)
1079                       && !pdi.is_declaration)
1080                     {
1081                       add_partial_symbol (&pdi, objfile);
1082                     }
1083                 }
1084               break;
1085             case DW_TAG_variable:
1086             case DW_TAG_typedef:
1087             case DW_TAG_class_type:
1088             case DW_TAG_structure_type:
1089             case DW_TAG_union_type:
1090             case DW_TAG_enumeration_type:
1091               if ((pdi.is_external || nesting_level == 1)
1092                   && !pdi.is_declaration)
1093                 {
1094                   add_partial_symbol (&pdi, objfile);
1095                 }
1096               break;
1097             case DW_TAG_enumerator:
1098               /* File scope enumerators are added to the partial symbol
1099                  table.  */
1100               if (nesting_level == 2)
1101                 add_partial_symbol (&pdi, objfile);
1102               break;
1103             case DW_TAG_base_type:
1104               /* File scope base type definitions are added to the partial
1105                  symbol table.  */
1106               if (nesting_level == 1)
1107                 add_partial_symbol (&pdi, objfile);
1108               break;
1109             default:
1110               break;
1111             }
1112         }
1113
1114       /* If the die has a sibling, skip to the sibling.
1115          Do not skip enumeration types, we want to record their
1116          enumerators.  */
1117       if (pdi.sibling && pdi.tag != DW_TAG_enumeration_type)
1118         {
1119           info_ptr = pdi.sibling;
1120         }
1121       else if (pdi.has_children)
1122         {
1123           /* Die has children, but the optional DW_AT_sibling attribute
1124              is missing.  */
1125           nesting_level++;
1126         }
1127
1128       if (pdi.tag == 0)
1129         {
1130           nesting_level--;
1131         }
1132     }
1133
1134   /* If we didn't find a lowpc, set it to highpc to avoid complaints
1135      from `maint check'.  */
1136   if (*lowpc == ((CORE_ADDR) -1))
1137     *lowpc = *highpc;
1138   return info_ptr;
1139 }
1140
1141 static void
1142 add_partial_symbol (pdi, objfile)
1143      struct partial_die_info *pdi;
1144      struct objfile *objfile;
1145 {
1146   CORE_ADDR addr = 0;
1147
1148   switch (pdi->tag)
1149     {
1150     case DW_TAG_subprogram:
1151       if (pdi->is_external)
1152         {
1153           /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1154                                       mst_text, objfile);*/
1155           add_psymbol_to_list (pdi->name, strlen (pdi->name),
1156                                VAR_NAMESPACE, LOC_BLOCK,
1157                                &objfile->global_psymbols,
1158                                0, pdi->lowpc + baseaddr, cu_language, objfile);
1159         }
1160       else
1161         {
1162           /*prim_record_minimal_symbol (pdi->name, pdi->lowpc + baseaddr,
1163                                       mst_file_text, objfile);*/
1164           add_psymbol_to_list (pdi->name, strlen (pdi->name),
1165                                VAR_NAMESPACE, LOC_BLOCK,
1166                                &objfile->static_psymbols,
1167                                0, pdi->lowpc + baseaddr, cu_language, objfile);
1168         }
1169       break;
1170     case DW_TAG_variable:
1171       if (pdi->is_external)
1172         {
1173           /* Global Variable.
1174              Don't enter into the minimal symbol tables as there is
1175              a minimal symbol table entry from the ELF symbols already.
1176              Enter into partial symbol table if it has a location
1177              descriptor or a type.
1178              If the location descriptor is missing, new_symbol will create
1179              a LOC_UNRESOLVED symbol, the address of the variable will then
1180              be determined from the minimal symbol table whenever the variable
1181              is referenced.
1182              The address for the partial symbol table entry is not
1183              used by GDB, but it comes in handy for debugging partial symbol
1184              table building.  */
1185
1186           if (pdi->locdesc)
1187             addr = decode_locdesc (pdi->locdesc, objfile);
1188           if (pdi->locdesc || pdi->has_type)
1189             add_psymbol_to_list (pdi->name, strlen (pdi->name),
1190                                  VAR_NAMESPACE, LOC_STATIC,
1191                                  &objfile->global_psymbols,
1192                                  0, addr + baseaddr, cu_language, objfile);
1193         }
1194       else
1195         {
1196           /* Static Variable. Skip symbols without location descriptors.  */
1197           if (pdi->locdesc == NULL)
1198             return;
1199           addr = decode_locdesc (pdi->locdesc, objfile);
1200           /*prim_record_minimal_symbol (pdi->name, addr + baseaddr,
1201                                       mst_file_data, objfile);*/
1202           add_psymbol_to_list (pdi->name, strlen (pdi->name),
1203                                VAR_NAMESPACE, LOC_STATIC,
1204                                &objfile->static_psymbols,
1205                                0, addr + baseaddr, cu_language, objfile);
1206         }
1207       break;
1208     case DW_TAG_typedef:
1209     case DW_TAG_base_type:
1210       add_psymbol_to_list (pdi->name, strlen (pdi->name),
1211                            VAR_NAMESPACE, LOC_TYPEDEF,
1212                            &objfile->static_psymbols,
1213                            0, (CORE_ADDR) 0, cu_language, objfile);
1214       break;
1215     case DW_TAG_class_type:
1216     case DW_TAG_structure_type:
1217     case DW_TAG_union_type:
1218     case DW_TAG_enumeration_type:
1219       /* Skip aggregate types without children, these are external
1220          references.  */
1221       if (pdi->has_children == 0)
1222         return;
1223       add_psymbol_to_list (pdi->name, strlen (pdi->name),
1224                            STRUCT_NAMESPACE, LOC_TYPEDEF,
1225                            &objfile->static_psymbols,
1226                            0, (CORE_ADDR) 0, cu_language, objfile);
1227
1228       if (cu_language == language_cplus)
1229         {
1230           /* For C++, these implicitly act as typedefs as well. */
1231           add_psymbol_to_list (pdi->name, strlen (pdi->name),
1232                                VAR_NAMESPACE, LOC_TYPEDEF,
1233                                &objfile->static_psymbols,
1234                                0, (CORE_ADDR) 0, cu_language, objfile);
1235         }
1236       break;
1237     case DW_TAG_enumerator:
1238       add_psymbol_to_list (pdi->name, strlen (pdi->name),
1239                            VAR_NAMESPACE, LOC_CONST,
1240                            &objfile->static_psymbols,
1241                            0, (CORE_ADDR) 0, cu_language, objfile);
1242       break;
1243     default:
1244       break;
1245     }
1246 }
1247
1248 /* Expand this partial symbol table into a full symbol table.  */
1249
1250 static void
1251 dwarf2_psymtab_to_symtab (pst)
1252      struct partial_symtab *pst;
1253 {
1254   /* FIXME: This is barely more than a stub.  */
1255   if (pst != NULL)
1256     {
1257       if (pst->readin)
1258         {
1259           warning ("bug: psymtab for %s is already read in.", pst->filename);
1260         }
1261       else
1262         {
1263           if (info_verbose)
1264             {
1265               printf_filtered ("Reading in symbols for %s...", pst->filename);
1266               gdb_flush (gdb_stdout);
1267             }
1268
1269           psymtab_to_symtab_1 (pst);
1270
1271           /* Finish up the debug error message.  */
1272           if (info_verbose)
1273             printf_filtered ("done.\n");
1274         }
1275     }
1276 }
1277
1278 static void
1279 psymtab_to_symtab_1 (pst)
1280      struct partial_symtab *pst;
1281 {
1282   struct objfile *objfile = pst->objfile;
1283   bfd *abfd = objfile->obfd;
1284   struct comp_unit_head cu_header;
1285   struct die_info *dies;
1286   unsigned long offset;
1287   CORE_ADDR lowpc, highpc;
1288   struct die_info *child_die;
1289   char *info_ptr;
1290   struct symtab *symtab;
1291   struct cleanup *back_to;
1292
1293   /* Set local variables from the partial symbol table info.  */
1294   offset = DWARF_INFO_OFFSET(pst);
1295   dwarf_info_buffer = DWARF_INFO_BUFFER(pst);
1296   dwarf_abbrev_buffer = DWARF_ABBREV_BUFFER(pst);
1297   dwarf_abbrev_size = DWARF_ABBREV_SIZE(pst);
1298   dwarf_line_buffer = DWARF_LINE_BUFFER(pst);
1299   baseaddr = ANOFFSET (pst->section_offsets, 0);
1300   cu_header_offset = offset;
1301   info_ptr = dwarf_info_buffer + offset;
1302
1303   obstack_init (&dwarf2_tmp_obstack);
1304   back_to = make_cleanup (dwarf2_free_tmp_obstack, NULL);
1305
1306   buildsym_init ();
1307   make_cleanup ((make_cleanup_func) really_free_pendings, NULL);
1308
1309   /* read in the comp_unit header  */
1310   cu_header.length = read_4_bytes (abfd, info_ptr);
1311   info_ptr += 4;
1312   cu_header.version = read_2_bytes (abfd, info_ptr);
1313   info_ptr += 2;
1314   cu_header.abbrev_offset = read_4_bytes (abfd, info_ptr);
1315   info_ptr += 4;
1316   cu_header.addr_size = read_1_byte (abfd, info_ptr);
1317   info_ptr += 1;
1318
1319   /* Read the abbrevs for this compilation unit  */
1320   dwarf2_read_abbrevs (abfd, cu_header.abbrev_offset);
1321   make_cleanup (dwarf2_empty_abbrev_table, NULL);
1322
1323   dies = read_comp_unit (info_ptr, abfd);
1324
1325   make_cleanup ((make_cleanup_func) free_die_list, dies);
1326
1327   /* Do line number decoding in read_file_scope () */
1328   process_die (dies, objfile);
1329
1330   if (!dwarf2_get_pc_bounds (dies, &lowpc, &highpc, objfile))
1331     {
1332       /* Some compilers don't define a DW_AT_high_pc attribute for
1333          the compilation unit.   If the DW_AT_high_pc is missing,
1334          synthesize it, by scanning the DIE's below the compilation unit.  */
1335       highpc = 0;
1336       if (dies->has_children)
1337         {
1338           child_die = dies->next;
1339           while (child_die && child_die->tag)
1340             {
1341               if (child_die->tag == DW_TAG_subprogram)
1342                 {
1343                   CORE_ADDR low, high;
1344
1345                   if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1346                     {
1347                       highpc = max (highpc, high);
1348                     }
1349                 }
1350               child_die = sibling_die (child_die);
1351             }
1352         }
1353     }
1354   symtab = end_symtab (highpc + baseaddr, objfile, 0);
1355
1356   /* Set symtab language to language from DW_AT_language.
1357      If the compilation is from a C file generated by language preprocessors,
1358      do not set the language if it was already deduced by start_subfile.  */
1359   if (symtab != NULL
1360       && !(cu_language == language_c && symtab->language != language_c))
1361     {
1362       symtab->language = cu_language;
1363     }
1364   pst->symtab = symtab;
1365   pst->readin = 1;
1366   sort_symtab_syms (pst->symtab);
1367
1368   do_cleanups (back_to);
1369 }
1370
1371 /* Process a die and its children.  */
1372
1373 static void
1374 process_die (die, objfile)
1375      struct die_info *die;
1376      struct objfile *objfile;
1377 {
1378   switch (die->tag)
1379     {
1380     case DW_TAG_padding:
1381       break;
1382     case DW_TAG_compile_unit:
1383       read_file_scope (die, objfile);
1384       break;
1385     case DW_TAG_subprogram:
1386       read_subroutine_type (die, objfile);
1387       read_func_scope (die, objfile);
1388       break;
1389     case DW_TAG_inlined_subroutine:
1390       /* FIXME:  These are ignored for now.
1391          They could be used to set breakpoints on all inlined instances
1392          of a function and make GDB `next' properly over inlined functions.  */
1393       break;
1394     case DW_TAG_lexical_block:
1395       read_lexical_block_scope (die, objfile);
1396       break;
1397     case DW_TAG_class_type:
1398     case DW_TAG_structure_type:
1399     case DW_TAG_union_type:
1400       read_structure_scope (die, objfile);
1401       break;
1402     case DW_TAG_enumeration_type:
1403       read_enumeration (die, objfile);
1404       break;
1405     case DW_TAG_subroutine_type:
1406       read_subroutine_type (die, objfile);
1407       break;
1408     case DW_TAG_array_type:
1409       read_array_type (die, objfile);
1410       break;
1411     case DW_TAG_pointer_type:
1412       read_tag_pointer_type (die, objfile);
1413       break;
1414     case DW_TAG_ptr_to_member_type:
1415       read_tag_ptr_to_member_type (die, objfile);
1416       break;
1417     case DW_TAG_reference_type:
1418       read_tag_reference_type (die, objfile);
1419       break;
1420     case DW_TAG_string_type:
1421       read_tag_string_type (die, objfile);
1422       break;
1423     case DW_TAG_base_type:
1424       read_base_type (die, objfile);
1425       if (dwarf_attr (die, DW_AT_name))
1426         {
1427           /* Add a typedef symbol for the base type definition.  */
1428           new_symbol (die, die->type, objfile);
1429         }
1430       break;
1431     case DW_TAG_common_block:
1432       read_common_block (die, objfile);
1433       break;
1434     case DW_TAG_common_inclusion:
1435       break;
1436     default:
1437       new_symbol (die, NULL, objfile);
1438       break;
1439     }
1440 }
1441
1442 static void
1443 read_file_scope (die, objfile)
1444      struct die_info *die;
1445      struct objfile *objfile;
1446 {
1447   unsigned int line_offset = 0;
1448   CORE_ADDR lowpc  = ((CORE_ADDR) -1);
1449   CORE_ADDR highpc = ((CORE_ADDR) 0);
1450   struct attribute *attr;
1451   char *name = "<unknown>";
1452   char *comp_dir = NULL;
1453   struct die_info *child_die;
1454   bfd *abfd = objfile->obfd;
1455
1456   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1457     {
1458       if (die->has_children)
1459         {
1460           child_die = die->next;
1461           while (child_die && child_die->tag)
1462             {
1463               if (child_die->tag == DW_TAG_subprogram)
1464                 {
1465                   CORE_ADDR low, high;
1466
1467                   if (dwarf2_get_pc_bounds (child_die, &low, &high, objfile))
1468                     {
1469                       lowpc = min (lowpc, low);
1470                       highpc = max (highpc, high);
1471                     }
1472                 }
1473               child_die = sibling_die (child_die);
1474             }
1475         }
1476     }
1477
1478   /* If we didn't find a lowpc, set it to highpc to avoid complaints
1479      from finish_block.  */
1480   if (lowpc == ((CORE_ADDR) -1))
1481     lowpc = highpc;
1482   lowpc += baseaddr;
1483   highpc += baseaddr;
1484
1485   attr = dwarf_attr (die, DW_AT_name);
1486   if (attr)
1487     {
1488       name = DW_STRING (attr);
1489     }
1490   attr = dwarf_attr (die, DW_AT_comp_dir);
1491   if (attr)
1492     {
1493       comp_dir = DW_STRING (attr);
1494       if (comp_dir)
1495         {
1496           /* Irix 6.2 native cc prepends <machine>.: to the compilation
1497              directory, get rid of it.  */
1498           char *cp = strchr (comp_dir, ':');
1499
1500           if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1501             comp_dir = cp + 1;
1502         }
1503     }
1504
1505   if (objfile->ei.entry_point >= lowpc &&
1506       objfile->ei.entry_point < highpc)
1507     {
1508       objfile->ei.entry_file_lowpc = lowpc;
1509       objfile->ei.entry_file_highpc = highpc;
1510     }
1511
1512   attr = dwarf_attr (die, DW_AT_language);
1513   if (attr)
1514     {
1515       set_cu_language (DW_UNSND (attr));
1516     }
1517
1518   /* We assume that we're processing GCC output. */
1519   processing_gcc_compilation = 2;
1520 #if 0
1521     /* FIXME:Do something here.  */
1522     if (dip->at_producer != NULL)
1523     {
1524       handle_producer (dip->at_producer);
1525     }
1526 #endif
1527
1528   /* The compilation unit may be in a different language or objfile,
1529      zero out all remembered fundamental types.  */
1530   memset (ftypes, 0, FT_NUM_MEMBERS * sizeof (struct type *));
1531
1532   start_symtab (name, comp_dir, lowpc);
1533   record_debugformat ("DWARF 2");
1534
1535   /* Decode line number information if present.  */
1536   attr = dwarf_attr (die, DW_AT_stmt_list);
1537   if (attr)
1538     {
1539       line_offset = DW_UNSND (attr);
1540       dwarf_decode_lines (line_offset, comp_dir, abfd);
1541     }
1542
1543   /* Process all dies in compilation unit.  */
1544   if (die->has_children)
1545     {
1546       child_die = die->next;
1547       while (child_die && child_die->tag)
1548         {
1549           process_die (child_die, objfile);
1550           child_die = sibling_die (child_die);
1551         }
1552     }
1553 }
1554
1555 static void
1556 read_func_scope (die, objfile)
1557      struct die_info *die;
1558      struct objfile *objfile;
1559 {
1560   register struct context_stack *new;
1561   CORE_ADDR lowpc;
1562   CORE_ADDR highpc;
1563   struct die_info *child_die;
1564   struct attribute *attr;
1565   char *name;
1566
1567   name = dwarf2_linkage_name (die);
1568
1569   /* Ignore functions with missing or empty names and functions with
1570      missing or invalid low and high pc attributes.  */
1571   if (name == NULL || !dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1572     return;
1573
1574   lowpc += baseaddr;
1575   highpc += baseaddr;
1576
1577   if (objfile->ei.entry_point >= lowpc &&
1578       objfile->ei.entry_point < highpc)
1579     {
1580       objfile->ei.entry_func_lowpc = lowpc;
1581       objfile->ei.entry_func_highpc = highpc;
1582     }
1583
1584   if (STREQ (name, "main"))     /* FIXME: hardwired name */
1585     {
1586       objfile->ei.main_func_lowpc = lowpc;
1587       objfile->ei.main_func_highpc = highpc;
1588     }
1589
1590   /* Decode DW_AT_frame_base location descriptor if present, keep result
1591      for DW_OP_fbreg operands in decode_locdesc.  */
1592   frame_base_reg = -1;
1593   frame_base_offset = 0;
1594   attr = dwarf_attr (die, DW_AT_frame_base);
1595   if (attr)
1596     {
1597       CORE_ADDR addr = decode_locdesc (DW_BLOCK (attr), objfile);
1598       if (isreg)
1599         frame_base_reg = addr;
1600       else if (offreg)
1601         {
1602           frame_base_reg = basereg;
1603           frame_base_offset = addr;
1604         }
1605       else
1606         complain (&dwarf2_unsupported_at_frame_base, name);
1607     }
1608
1609   new = push_context (0, lowpc);
1610   new->name = new_symbol (die, die->type, objfile);
1611   list_in_scope = &local_symbols;
1612
1613   if (die->has_children)
1614     {
1615       child_die = die->next;
1616       while (child_die && child_die->tag)
1617         {
1618           process_die (child_die, objfile);
1619           child_die = sibling_die (child_die);
1620         }
1621     }
1622
1623   new = pop_context ();
1624   /* Make a block for the local symbols within.  */
1625   finish_block (new->name, &local_symbols, new->old_blocks,
1626                 lowpc, highpc, objfile);
1627   list_in_scope = &file_symbols;
1628 }
1629
1630 /* Process all the DIES contained within a lexical block scope.  Start
1631    a new scope, process the dies, and then close the scope.  */
1632
1633 static void
1634 read_lexical_block_scope (die, objfile)
1635      struct die_info *die;
1636      struct objfile *objfile;
1637 {
1638   register struct context_stack *new;
1639   CORE_ADDR lowpc, highpc;
1640   struct die_info *child_die;
1641
1642   /* Ignore blocks with missing or invalid low and high pc attributes.  */
1643   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, objfile))
1644     return;
1645   lowpc += baseaddr;
1646   highpc += baseaddr;
1647
1648   push_context (0, lowpc);
1649   if (die->has_children)
1650     {
1651       child_die = die->next;
1652       while (child_die && child_die->tag)
1653         {
1654           process_die (child_die, objfile);
1655           child_die = sibling_die (child_die);
1656         }
1657     }
1658   new = pop_context ();
1659
1660   if (local_symbols != NULL)
1661     {
1662       finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
1663                     highpc, objfile);
1664     }
1665   local_symbols = new->locals;
1666 }
1667
1668 /* Get low and high pc attributes from a die.
1669    Return 1 if the attributes are present and valid, otherwise, return 0.  */
1670
1671 static int
1672 dwarf2_get_pc_bounds (die, lowpc, highpc, objfile)
1673      struct die_info *die;
1674      CORE_ADDR *lowpc;
1675      CORE_ADDR *highpc;
1676      struct objfile *objfile;
1677 {
1678   struct attribute *attr;
1679   CORE_ADDR low;
1680   CORE_ADDR high;
1681
1682   attr = dwarf_attr (die, DW_AT_low_pc);
1683   if (attr)
1684     low = DW_ADDR (attr);
1685   else
1686     return 0;
1687   attr = dwarf_attr (die, DW_AT_high_pc);
1688   if (attr)
1689     high = DW_ADDR (attr);
1690   else
1691     return 0;
1692
1693   if (high < low)
1694     return 0;
1695
1696   /* When using the GNU linker, .gnu.linkonce. sections are used to
1697      eliminate duplicate copies of functions and vtables and such.
1698      The linker will arbitrarily choose one and discard the others.
1699      The AT_*_pc values for such functions refer to local labels in
1700      these sections.  If the section from that file was discarded, the
1701      labels are not in the output, so the relocs get a value of 0.
1702      If this is a discarded function, mark the pc bounds as invalid,
1703      so that GDB will ignore it.  */
1704   if (low == 0 && (bfd_get_file_flags (objfile->obfd) & HAS_RELOC) == 0)
1705     return 0;
1706
1707   *lowpc = low;
1708   *highpc = high;
1709   return 1;
1710 }
1711
1712 /* Add an aggregate field to the field list.  */
1713
1714 static void
1715 dwarf2_add_field (fip, die, objfile)
1716      struct field_info *fip;
1717      struct die_info *die;
1718      struct objfile *objfile;
1719 {
1720   struct nextfield *new_field;
1721   struct attribute *attr;
1722   struct field *fp;
1723   char *fieldname = "";
1724
1725   /* Allocate a new field list entry and link it in.  */
1726   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
1727   make_cleanup (free, new_field);
1728   memset (new_field, 0, sizeof (struct nextfield));
1729   new_field->next = fip->fields;
1730   fip->fields = new_field;
1731   fip->nfields++;
1732
1733   /* Handle accessibility and virtuality of field.
1734      The default accessibility for members is public, the default
1735      accessibility for inheritance is private.  */
1736   if (die->tag != DW_TAG_inheritance)
1737     new_field->accessibility = DW_ACCESS_public;
1738   else
1739     new_field->accessibility = DW_ACCESS_private;
1740   new_field->virtuality = DW_VIRTUALITY_none;
1741
1742   attr = dwarf_attr (die, DW_AT_accessibility);
1743   if (attr)
1744     new_field->accessibility = DW_UNSND (attr);
1745   if (new_field->accessibility != DW_ACCESS_public)
1746     fip->non_public_fields = 1;
1747   attr = dwarf_attr (die, DW_AT_virtuality);
1748   if (attr)
1749     new_field->virtuality = DW_UNSND (attr);
1750
1751   fp = &new_field->field;
1752   if (die->tag == DW_TAG_member)
1753     {
1754       /* Get type of field.  */
1755       fp->type = die_type (die, objfile);
1756
1757       /* Get bit size of field (zero if none).  */
1758       attr = dwarf_attr (die, DW_AT_bit_size);
1759       if (attr)
1760         {
1761           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
1762         }
1763       else
1764         {
1765           FIELD_BITSIZE (*fp) = 0;
1766         }
1767
1768       /* Get bit offset of field.  */
1769       attr = dwarf_attr (die, DW_AT_data_member_location);
1770       if (attr)
1771         {
1772           FIELD_BITPOS (*fp) =
1773             decode_locdesc (DW_BLOCK (attr), objfile) * bits_per_byte;
1774         }
1775       else
1776         FIELD_BITPOS (*fp) = 0;
1777       attr = dwarf_attr (die, DW_AT_bit_offset);
1778       if (attr)
1779         {
1780           if (BITS_BIG_ENDIAN)
1781             {
1782               /* For big endian bits, the DW_AT_bit_offset gives the
1783                  additional bit offset from the MSB of the containing
1784                  anonymous object to the MSB of the field.  We don't
1785                  have to do anything special since we don't need to
1786                  know the size of the anonymous object.  */
1787               FIELD_BITPOS (*fp) += DW_UNSND (attr);
1788             }
1789           else
1790             {
1791               /* For little endian bits, compute the bit offset to the
1792                  MSB of the anonymous object, subtract off the number of
1793                  bits from the MSB of the field to the MSB of the
1794                  object, and then subtract off the number of bits of
1795                  the field itself.  The result is the bit offset of
1796                  the LSB of the field.  */
1797               int anonymous_size;
1798               int bit_offset = DW_UNSND (attr);
1799
1800               attr = dwarf_attr (die, DW_AT_byte_size);
1801               if (attr)
1802                 {
1803                   /* The size of the anonymous object containing
1804                      the bit field is explicit, so use the
1805                      indicated size (in bytes).  */
1806                   anonymous_size = DW_UNSND (attr);
1807                 }
1808               else
1809                 {
1810                   /* The size of the anonymous object containing
1811                      the bit field must be inferred from the type
1812                      attribute of the data member containing the
1813                      bit field.  */
1814                   anonymous_size = TYPE_LENGTH (fp->type);
1815                 }
1816               FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
1817                 - bit_offset - FIELD_BITSIZE (*fp);
1818             }
1819         }
1820
1821       /* Get name of field.  */
1822       attr = dwarf_attr (die, DW_AT_name);
1823       if (attr && DW_STRING (attr))
1824         fieldname = DW_STRING (attr);
1825       fp->name = obsavestring (fieldname, strlen (fieldname),
1826                                &objfile->type_obstack);
1827
1828       /* Change accessibility for artificial fields (e.g. virtual table
1829          pointer or virtual base class pointer) to private.  */
1830       if (dwarf_attr (die, DW_AT_artificial))
1831         {
1832           new_field->accessibility = DW_ACCESS_private;
1833           fip->non_public_fields = 1;
1834         }
1835     }
1836   else if (die->tag == DW_TAG_variable)
1837     {
1838       char *physname;
1839       char *cp;
1840
1841       /* C++ static member.
1842          Get physical name, extract field name from physical name.  */
1843       physname = dwarf2_linkage_name (die);
1844       if (physname == NULL)
1845         return;
1846
1847       cp = physname;
1848       while (*cp && !is_cplus_marker (*cp))
1849         cp++;
1850       if (*cp)
1851         fieldname = cp + 1;
1852       if (*fieldname == '\0')
1853         {
1854           complain (&dwarf2_bad_static_member_name, physname);
1855         }
1856
1857       SET_FIELD_PHYSNAME (*fp, obsavestring (physname, strlen (physname),
1858                                             &objfile->type_obstack));
1859       FIELD_TYPE (*fp) = die_type (die, objfile);
1860       FIELD_NAME (*fp) = obsavestring (fieldname, strlen (fieldname),
1861                                &objfile->type_obstack);
1862     }
1863   else if (die->tag == DW_TAG_inheritance)
1864     {
1865       /* C++ base class field.  */
1866       attr = dwarf_attr (die, DW_AT_data_member_location);
1867       if (attr)
1868         FIELD_BITPOS (*fp) = decode_locdesc (DW_BLOCK (attr), objfile) * bits_per_byte;
1869       FIELD_BITSIZE (*fp) = 0;
1870       FIELD_TYPE (*fp) = die_type (die, objfile);
1871       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
1872       fip->nbaseclasses++;
1873     }
1874 }
1875
1876 /* Create the vector of fields, and attach it to the type.  */
1877
1878 static void
1879 dwarf2_attach_fields_to_type (fip, type, objfile)
1880      struct field_info *fip;
1881      struct type *type;
1882      struct objfile *objfile;
1883 {
1884   int nfields = fip->nfields;
1885
1886   /* Record the field count, allocate space for the array of fields,
1887      and create blank accessibility bitfields if necessary.  */
1888   TYPE_NFIELDS (type) = nfields;
1889   TYPE_FIELDS (type) = (struct field *)
1890     TYPE_ALLOC (type, sizeof (struct field) * nfields);
1891   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
1892
1893   if (fip->non_public_fields)
1894     {
1895       ALLOCATE_CPLUS_STRUCT_TYPE (type);
1896
1897       TYPE_FIELD_PRIVATE_BITS (type) =
1898         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1899       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
1900
1901       TYPE_FIELD_PROTECTED_BITS (type) =
1902         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1903       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
1904
1905       TYPE_FIELD_IGNORE_BITS (type) =
1906         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
1907       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
1908     }
1909
1910   /* If the type has baseclasses, allocate and clear a bit vector for
1911      TYPE_FIELD_VIRTUAL_BITS.  */
1912   if (fip->nbaseclasses)
1913     {
1914       int num_bytes = B_BYTES (fip->nbaseclasses);
1915       char *pointer;
1916
1917       ALLOCATE_CPLUS_STRUCT_TYPE (type);
1918       pointer = (char *) TYPE_ALLOC (type, num_bytes);
1919       TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *) pointer;
1920       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
1921       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
1922     }
1923
1924   /* Copy the saved-up fields into the field vector.  Start from the head
1925      of the list, adding to the tail of the field array, so that they end
1926      up in the same order in the array in which they were added to the list.  */
1927   while (nfields-- > 0)
1928     {
1929       TYPE_FIELD (type, nfields) = fip->fields->field;
1930       switch (fip->fields->accessibility)
1931         {
1932           case DW_ACCESS_private:
1933             SET_TYPE_FIELD_PRIVATE (type, nfields);
1934             break;
1935
1936           case DW_ACCESS_protected:
1937             SET_TYPE_FIELD_PROTECTED (type, nfields);
1938             break;
1939
1940           case DW_ACCESS_public:
1941             break;
1942
1943           default:
1944             /* Unknown accessibility.  Complain and treat it as public.  */
1945             {
1946               complain (&dwarf2_unsupported_accessibility,
1947                         fip->fields->accessibility);
1948             }
1949             break;
1950         }
1951       if (nfields < fip->nbaseclasses)
1952         {
1953           switch (fip->fields->virtuality)
1954             {
1955               case DW_VIRTUALITY_virtual:
1956               case DW_VIRTUALITY_pure_virtual:
1957                 SET_TYPE_FIELD_VIRTUAL (type, nfields);
1958                 break;
1959             }
1960         }
1961       fip->fields = fip->fields->next;
1962     }
1963 }
1964
1965 /* Skip to the end of a member function name in a mangled name.  */
1966
1967 static char *
1968 skip_member_fn_name (physname)
1969      char *physname;
1970 {
1971   char *endname = physname;
1972
1973   /* Skip over leading underscores.  */
1974   while (*endname == '_')
1975     endname++;
1976
1977   /* Find two succesive underscores.  */
1978   do
1979     endname = strchr (endname, '_');
1980   while (endname != NULL && *++endname != '_');
1981
1982   if (endname == NULL)
1983     {
1984       complain (&dwarf2_bad_member_name_complaint, physname);
1985       endname = physname;
1986     }
1987   else
1988     {
1989       /* Take care of trailing underscores.  */
1990       if (endname[1] != '_')
1991         endname--;
1992     }
1993   return endname;
1994 }
1995
1996 /* Add a member function to the proper fieldlist.  */
1997
1998 static void
1999 dwarf2_add_member_fn (fip, die, type, objfile)
2000      struct field_info *fip;
2001      struct die_info *die;
2002      struct type *type;
2003      struct objfile *objfile;
2004 {
2005   struct attribute *attr;
2006   struct fnfieldlist *flp;
2007   int i;
2008   struct fn_field *fnp;
2009   char *fieldname;
2010   char *physname;
2011   struct nextfnfield *new_fnfield;
2012
2013   /* Extract member function name from mangled name.  */
2014   physname = dwarf2_linkage_name (die);
2015   if (physname == NULL)
2016     return;
2017   if ((physname[0] == '_' && physname[1] == '_'
2018         && strchr ("0123456789Qt", physname[2]))
2019       || DESTRUCTOR_PREFIX_P (physname))
2020     {
2021       /* Constructor and destructor field names are set to the name
2022          of the class, but without template parameter lists.
2023          The name might be missing for anonymous aggregates.  */
2024       if (TYPE_TAG_NAME (type))
2025         {
2026           char *p = strchr (TYPE_TAG_NAME (type), '<');
2027
2028           if (p == NULL)
2029             fieldname = TYPE_TAG_NAME (type);
2030           else
2031             fieldname = obsavestring (TYPE_TAG_NAME (type),
2032                                       p - TYPE_TAG_NAME (type),
2033                                       &objfile->type_obstack);
2034         }
2035       else
2036         {
2037           char *anon_name = "";
2038           fieldname = obsavestring (anon_name, strlen (anon_name),
2039                                     &objfile->type_obstack);
2040         }
2041     }
2042   else
2043     {
2044       char *endname = skip_member_fn_name (physname);
2045
2046       /* Ignore member function if we were unable not extract the member
2047          function name.  */
2048       if (endname == physname)
2049         return;
2050       fieldname = obsavestring (physname, endname - physname,
2051                                 &objfile->type_obstack);
2052     }
2053
2054   /* Look up member function name in fieldlist.  */
2055   for (i = 0; i < fip->nfnfields; i++)
2056     {
2057       if (STREQ (fip->fnfieldlists[i].name, fieldname))
2058         break;
2059     }
2060
2061   /* Create new list element if necessary.  */
2062   if (i < fip->nfnfields)
2063     flp = &fip->fnfieldlists[i];
2064   else
2065     {
2066       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
2067         {
2068           fip->fnfieldlists = (struct fnfieldlist *)
2069             xrealloc (fip->fnfieldlists,
2070                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
2071                         * sizeof (struct fnfieldlist));
2072           if (fip->nfnfields == 0)
2073             make_cleanup ((make_cleanup_func) free_current_contents, 
2074                           &fip->fnfieldlists);
2075         }
2076       flp = &fip->fnfieldlists[fip->nfnfields];
2077       flp->name = fieldname;
2078       flp->length = 0;
2079       flp->head = NULL;
2080       fip->nfnfields++;
2081     }
2082
2083   /* Create a new member function field and chain it to the field list
2084      entry. */
2085   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
2086   make_cleanup (free, new_fnfield);
2087   memset (new_fnfield, 0, sizeof (struct nextfnfield));
2088   new_fnfield->next = flp->head;
2089   flp->head = new_fnfield;
2090   flp->length++;
2091
2092   /* Fill in the member function field info.  */
2093   fnp = &new_fnfield->fnfield;
2094   fnp->physname = obsavestring (physname, strlen (physname),
2095                                 &objfile->type_obstack);
2096   fnp->type = alloc_type (objfile);
2097   if (die->type && TYPE_CODE (die->type) == TYPE_CODE_FUNC)
2098     {
2099       struct type *return_type = TYPE_TARGET_TYPE (die->type);
2100       struct type **arg_types;
2101       int nparams = TYPE_NFIELDS (die->type);
2102       int iparams;
2103
2104       /* Copy argument types from the subroutine type.  */
2105       arg_types = (struct type **)
2106         TYPE_ALLOC (fnp->type, (nparams + 1) * sizeof (struct type *));
2107       for (iparams = 0; iparams < nparams; iparams++)
2108         arg_types[iparams] = TYPE_FIELD_TYPE (die->type, iparams);
2109
2110       /* Set last entry in argument type vector.  */
2111       if (TYPE_FLAGS (die->type) & TYPE_FLAG_VARARGS)
2112         arg_types[nparams] = NULL;
2113       else
2114         arg_types[nparams] = dwarf2_fundamental_type (objfile, FT_VOID);
2115
2116       smash_to_method_type (fnp->type, type, return_type, arg_types);
2117
2118       /* Handle static member functions.
2119          Dwarf2 has no clean way to discern C++ static and non-static
2120          member functions. G++ helps GDB by marking the first
2121          parameter for non-static member functions (which is the
2122          this pointer) as artificial. We obtain this information
2123          from read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
2124       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (die->type, 0) == 0)
2125         fnp->voffset = VOFFSET_STATIC;
2126     }
2127   else
2128     complain (&dwarf2_missing_member_fn_type_complaint, physname);
2129
2130   /* Get fcontext from DW_AT_containing_type if present.  */
2131   if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2132     fnp->fcontext = die_containing_type (die, objfile);
2133
2134   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
2135      and is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
2136
2137   /* Get accessibility.  */
2138   attr = dwarf_attr (die, DW_AT_accessibility);
2139   if (attr)
2140     {
2141       switch (DW_UNSND (attr))
2142         {
2143           case DW_ACCESS_private:
2144             fnp->is_private = 1;
2145             break;
2146           case DW_ACCESS_protected:
2147             fnp->is_protected = 1;
2148             break;
2149         }
2150     }
2151
2152   /* Get index in virtual function table if it is a virtual member function.  */
2153   attr = dwarf_attr (die, DW_AT_vtable_elem_location);
2154   if (attr)
2155     fnp->voffset = decode_locdesc (DW_BLOCK (attr), objfile) + 2;
2156 }
2157
2158 /* Create the vector of member function fields, and attach it to the type.  */
2159
2160 static void
2161 dwarf2_attach_fn_fields_to_type (fip, type, objfile)
2162      struct field_info *fip;
2163      struct type *type;
2164      struct objfile *objfile;
2165 {
2166   struct fnfieldlist *flp;
2167   int total_length = 0;
2168   int i;
2169
2170   ALLOCATE_CPLUS_STRUCT_TYPE (type);
2171   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
2172     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
2173
2174   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
2175     {
2176       struct nextfnfield *nfp = flp->head;
2177       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
2178       int k;
2179
2180       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
2181       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
2182       fn_flp->fn_fields = (struct fn_field *)
2183         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
2184       for (k = flp->length; (k--, nfp); nfp = nfp->next)
2185         fn_flp->fn_fields[k] = nfp->fnfield;
2186
2187       total_length += flp->length;
2188     }
2189
2190   TYPE_NFN_FIELDS (type) = fip->nfnfields;
2191   TYPE_NFN_FIELDS_TOTAL (type) = total_length;
2192 }
2193
2194 /* Called when we find the DIE that starts a structure or union scope
2195    (definition) to process all dies that define the members of the
2196    structure or union.
2197
2198    NOTE: we need to call struct_type regardless of whether or not the
2199    DIE has an at_name attribute, since it might be an anonymous
2200    structure or union.  This gets the type entered into our set of
2201    user defined types.
2202
2203    However, if the structure is incomplete (an opaque struct/union)
2204    then suppress creating a symbol table entry for it since gdb only
2205    wants to find the one with the complete definition.  Note that if
2206    it is complete, we just call new_symbol, which does it's own
2207    checking about whether the struct/union is anonymous or not (and
2208    suppresses creating a symbol table entry itself).  */
2209
2210 static void
2211 read_structure_scope (die, objfile)
2212      struct die_info *die;
2213      struct objfile *objfile;
2214 {
2215   struct type *type;
2216   struct attribute *attr;
2217
2218   type = alloc_type (objfile);
2219
2220   INIT_CPLUS_SPECIFIC (type);
2221   attr = dwarf_attr (die, DW_AT_name);
2222   if (attr && DW_STRING (attr))
2223     {
2224       TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2225                                            strlen (DW_STRING (attr)),
2226                                            &objfile->type_obstack);
2227     }
2228
2229   if (die->tag == DW_TAG_structure_type)
2230     {
2231       TYPE_CODE (type) = TYPE_CODE_STRUCT;
2232     }
2233   else if (die->tag == DW_TAG_union_type)
2234     {
2235       TYPE_CODE (type) = TYPE_CODE_UNION;
2236     }
2237   else
2238     {
2239       /* FIXME: TYPE_CODE_CLASS is currently defined to TYPE_CODE_STRUCT
2240          in gdbtypes.h.  */
2241       TYPE_CODE (type) = TYPE_CODE_CLASS;
2242     }
2243
2244   attr = dwarf_attr (die, DW_AT_byte_size);
2245   if (attr)
2246     {
2247       TYPE_LENGTH (type) = DW_UNSND (attr);
2248     }
2249   else
2250     {
2251       TYPE_LENGTH (type) = 0;
2252     }
2253
2254   /* We need to add the type field to the die immediately so we don't
2255      infinitely recurse when dealing with pointers to the structure
2256      type within the structure itself. */
2257   die->type = type;
2258
2259   if (die->has_children)
2260     {
2261       struct field_info fi;
2262       struct die_info *child_die;
2263       struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2264
2265       memset (&fi, 0, sizeof (struct field_info));
2266
2267       child_die = die->next;
2268
2269       while (child_die && child_die->tag)
2270         {
2271           if (child_die->tag == DW_TAG_member)
2272             {
2273               dwarf2_add_field (&fi, child_die, objfile);
2274             }
2275           else if (child_die->tag == DW_TAG_variable)
2276             {
2277               /* C++ static member.  */
2278               dwarf2_add_field (&fi, child_die, objfile);
2279             }
2280           else if (child_die->tag == DW_TAG_subprogram)
2281             {
2282               /* C++ member function. */
2283               process_die (child_die, objfile);
2284               dwarf2_add_member_fn (&fi, child_die, type, objfile);
2285             }
2286           else if (child_die->tag == DW_TAG_inheritance)
2287             {
2288               /* C++ base class field.  */
2289               dwarf2_add_field (&fi, child_die, objfile);
2290             }
2291           else
2292             {
2293               process_die (child_die, objfile);
2294             }
2295           child_die = sibling_die (child_die);
2296         }
2297
2298       /* Attach fields and member functions to the type.  */
2299       if (fi.nfields)
2300         dwarf2_attach_fields_to_type (&fi, type, objfile);
2301       if (fi.nfnfields)
2302         {
2303           dwarf2_attach_fn_fields_to_type (&fi, type, objfile);
2304
2305           /* Get the type which refers to the base class (possibly this
2306              class itself) which contains the vtable pointer for the current
2307              class from the DW_AT_containing_type attribute.  */
2308
2309           if (dwarf_attr (die, DW_AT_containing_type) != NULL)
2310             {
2311               struct type *t = die_containing_type (die, objfile);
2312
2313               TYPE_VPTR_BASETYPE (type) = t;
2314               if (type == t)
2315                 {
2316                   static const char vptr_name[] = { '_','v','p','t','r','\0' };
2317                   int i;
2318
2319                   /* Our own class provides vtbl ptr.  */
2320                   for (i = TYPE_NFIELDS (t) - 1;
2321                        i >= TYPE_N_BASECLASSES (t);
2322                        --i)
2323                     {
2324                       char *fieldname = TYPE_FIELD_NAME (t, i);
2325
2326                       if (STREQN (fieldname, vptr_name, strlen (vptr_name) - 1)
2327                           && is_cplus_marker (fieldname[strlen (vptr_name)]))
2328                         {
2329                           TYPE_VPTR_FIELDNO (type) = i;
2330                           break;
2331                         }
2332                     }
2333
2334                   /* Complain if virtual function table field not found.  */
2335                   if (i < TYPE_N_BASECLASSES (t))
2336                     complain (&dwarf2_vtbl_not_found_complaint,
2337                               TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) : "");
2338                 }
2339               else
2340                 {
2341                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
2342                 }
2343             }
2344         }
2345
2346       new_symbol (die, type, objfile);
2347
2348       do_cleanups (back_to);
2349     }
2350   else
2351     {
2352       /* No children, must be stub. */
2353       TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
2354     }
2355
2356   die->type = type;
2357 }
2358
2359 /* Given a pointer to a die which begins an enumeration, process all
2360    the dies that define the members of the enumeration.
2361
2362    This will be much nicer in draft 6 of the DWARF spec when our
2363    members will be dies instead squished into the DW_AT_element_list
2364    attribute.
2365
2366    NOTE: We reverse the order of the element list.  */
2367
2368 static void
2369 read_enumeration (die, objfile)
2370      struct die_info *die;
2371      struct objfile *objfile;
2372 {
2373   struct die_info *child_die;
2374   struct type *type;
2375   struct field *fields;
2376   struct attribute *attr;
2377   struct symbol *sym;
2378   int num_fields;
2379   int unsigned_enum = 1;
2380
2381   type = alloc_type (objfile);
2382
2383   TYPE_CODE (type) = TYPE_CODE_ENUM;
2384   attr = dwarf_attr (die, DW_AT_name);
2385   if (attr && DW_STRING (attr))
2386     {
2387       TYPE_TAG_NAME (type) = obsavestring (DW_STRING (attr),
2388                                            strlen (DW_STRING (attr)),
2389                                            &objfile->type_obstack);
2390     }
2391
2392   attr = dwarf_attr (die, DW_AT_byte_size);
2393   if (attr)
2394     {
2395       TYPE_LENGTH (type) = DW_UNSND (attr);
2396     }
2397   else
2398     {
2399       TYPE_LENGTH (type) = 0;
2400     }
2401
2402   num_fields = 0;
2403   fields = NULL;
2404   if (die->has_children)
2405     {
2406       child_die = die->next;
2407       while (child_die && child_die->tag)
2408         {
2409           if (child_die->tag != DW_TAG_enumerator)
2410             {
2411               process_die (child_die, objfile);
2412             }
2413           else
2414             {
2415               attr = dwarf_attr (child_die, DW_AT_name);
2416               if (attr)
2417                 {
2418                   sym = new_symbol (child_die, type, objfile);
2419                   if (SYMBOL_VALUE (sym) < 0)
2420                     unsigned_enum = 0;
2421
2422                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
2423                     {
2424                       fields = (struct field *)
2425                         xrealloc (fields,
2426                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
2427                                     * sizeof (struct field));
2428                     }
2429
2430                   FIELD_NAME (fields[num_fields]) = SYMBOL_NAME (sym);
2431                   FIELD_TYPE (fields[num_fields]) = NULL;
2432                   FIELD_BITPOS (fields[num_fields]) = SYMBOL_VALUE (sym);
2433                   FIELD_BITSIZE (fields[num_fields]) = 0;
2434
2435                   num_fields++;
2436                 }
2437             }
2438
2439           child_die = sibling_die (child_die);
2440         }
2441
2442       if (num_fields)
2443         {
2444           TYPE_NFIELDS (type) = num_fields;
2445           TYPE_FIELDS (type) = (struct field *)
2446             TYPE_ALLOC (type, sizeof (struct field) * num_fields);
2447           memcpy (TYPE_FIELDS (type), fields,
2448                   sizeof (struct field) * num_fields);
2449           free (fields);
2450         }
2451       if (unsigned_enum)
2452         TYPE_FLAGS (type) |= TYPE_FLAG_UNSIGNED;
2453     }
2454   die->type = type;
2455   new_symbol (die, type, objfile);
2456 }
2457
2458 /* Extract all information from a DW_TAG_array_type DIE and put it in
2459    the DIE's type field.  For now, this only handles one dimensional
2460    arrays.  */
2461
2462 static void
2463 read_array_type (die, objfile)
2464      struct die_info *die;
2465      struct objfile *objfile;
2466 {
2467   struct die_info *child_die;
2468   struct type *type = NULL;
2469   struct type *element_type, *range_type, *index_type;
2470   struct type **range_types = NULL;
2471   struct attribute *attr;
2472   int ndim = 0;
2473   struct cleanup *back_to;
2474
2475   /* Return if we've already decoded this type. */
2476   if (die->type)
2477     {
2478       return;
2479     }
2480
2481   element_type = die_type (die, objfile);
2482
2483   /* Irix 6.2 native cc creates array types without children for
2484      arrays with unspecified length.  */
2485   if (die->has_children == 0)
2486     {
2487       index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2488       range_type = create_range_type (NULL, index_type, 0, -1);
2489       die->type = create_array_type (NULL, element_type, range_type);
2490       return;
2491     }
2492
2493   back_to = make_cleanup (null_cleanup, NULL);
2494   child_die = die->next;
2495   while (child_die && child_die->tag)
2496     {
2497       if (child_die->tag == DW_TAG_subrange_type)
2498         {
2499           unsigned int low, high;
2500
2501           /* Default bounds to an array with unspecified length.  */
2502           low = 0;
2503           high = -1;
2504           if (cu_language == language_fortran)
2505             {
2506               /* FORTRAN implies a lower bound of 1, if not given.  */
2507               low = 1;
2508             }
2509
2510           index_type = die_type (child_die, objfile);
2511           attr = dwarf_attr (child_die, DW_AT_lower_bound);
2512           if (attr)
2513             {
2514               if (attr->form == DW_FORM_sdata)
2515                 {
2516                   low = DW_SND (attr);
2517                 }
2518               else if (attr->form == DW_FORM_udata
2519                        || attr->form == DW_FORM_data1
2520                        || attr->form == DW_FORM_data2
2521                        || attr->form == DW_FORM_data4)
2522                 {
2523                   low = DW_UNSND (attr);
2524                 }
2525               else
2526                 {
2527                   complain (&dwarf2_non_const_array_bound_ignored,
2528                             dwarf_form_name (attr->form));
2529 #ifdef FORTRAN_HACK
2530                   die->type = lookup_pointer_type (element_type);
2531                   return;
2532 #else
2533                   low = 0;
2534 #endif
2535                 }
2536             }
2537           attr = dwarf_attr (child_die, DW_AT_upper_bound);
2538           if (attr)
2539             {
2540               if (attr->form == DW_FORM_sdata)
2541                 {
2542                   high = DW_SND (attr);
2543                 }
2544               else if (attr->form == DW_FORM_udata
2545                        || attr->form == DW_FORM_data1
2546                        || attr->form == DW_FORM_data2
2547                        || attr->form == DW_FORM_data4)
2548                 {
2549                   high = DW_UNSND (attr);
2550                 }
2551               else if (attr->form == DW_FORM_block1)
2552                 {
2553                   /* GCC encodes arrays with unspecified or dynamic length
2554                      with a DW_FORM_block1 attribute.
2555                      FIXME: GDB does not yet know how to handle dynamic
2556                      arrays properly, treat them as arrays with unspecified
2557                      length for now.  */
2558                   high = -1;
2559                 }
2560               else
2561                 {
2562                   complain (&dwarf2_non_const_array_bound_ignored,
2563                             dwarf_form_name (attr->form));
2564 #ifdef FORTRAN_HACK
2565                   die->type = lookup_pointer_type (element_type);
2566                   return;
2567 #else
2568                   high = 1;
2569 #endif
2570                 }
2571             }
2572
2573           /* Create a range type and save it for array type creation.  */
2574           if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
2575             {
2576               range_types = (struct type **)
2577                 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
2578                                          * sizeof (struct type *));
2579               if (ndim == 0)
2580                 make_cleanup ((make_cleanup_func) free_current_contents, 
2581                               &range_types);
2582             }
2583           range_types[ndim++] = create_range_type (NULL, index_type, low, high);
2584         }
2585       child_die = sibling_die (child_die);
2586     }
2587
2588   /* Dwarf2 dimensions are output from left to right, create the
2589      necessary array types in backwards order.  */
2590   type = element_type;
2591   while (ndim-- > 0)
2592     type = create_array_type (NULL, type, range_types[ndim]);
2593
2594   do_cleanups (back_to);
2595
2596   /* Install the type in the die. */
2597   die->type = type;
2598 }
2599
2600 /* First cut: install each common block member as a global variable.  */
2601
2602 static void
2603 read_common_block (die, objfile)
2604      struct die_info *die;
2605      struct objfile *objfile;
2606 {
2607   struct die_info *child_die;
2608   struct attribute *attr;
2609   struct symbol *sym;
2610   CORE_ADDR base = (CORE_ADDR) 0;
2611
2612   attr = dwarf_attr (die, DW_AT_location);
2613   if (attr)
2614     {
2615       base = decode_locdesc (DW_BLOCK (attr), objfile);
2616     }
2617   if (die->has_children)
2618     {
2619       child_die = die->next;
2620       while (child_die && child_die->tag)
2621         {
2622           sym = new_symbol (child_die, NULL, objfile);
2623           attr = dwarf_attr (child_die, DW_AT_data_member_location);
2624           if (attr)
2625             {
2626               SYMBOL_VALUE_ADDRESS (sym) =
2627                 base + decode_locdesc (DW_BLOCK (attr), objfile);
2628               add_symbol_to_list (sym, &global_symbols);
2629             }
2630           child_die = sibling_die (child_die);
2631         }
2632     }
2633 }
2634
2635 /* Extract all information from a DW_TAG_pointer_type DIE and add to
2636    the user defined type vector.  */
2637
2638 static void
2639 read_tag_pointer_type (die, objfile)
2640      struct die_info *die;
2641      struct objfile *objfile;
2642 {
2643   struct type *type;
2644   struct attribute *attr;
2645
2646   if (die->type)
2647     {
2648       return;
2649     }
2650
2651   type = lookup_pointer_type (die_type (die, objfile));
2652   attr = dwarf_attr (die, DW_AT_byte_size);
2653   if (attr)
2654     {
2655       TYPE_LENGTH (type) = DW_UNSND (attr);
2656     }
2657   else
2658     {
2659       TYPE_LENGTH (type) = address_size;
2660     }
2661   die->type = type;
2662 }
2663
2664 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
2665    the user defined type vector.  */
2666
2667 static void
2668 read_tag_ptr_to_member_type (die, objfile)
2669      struct die_info *die;
2670      struct objfile *objfile;
2671 {
2672   struct type *type;
2673   struct type *to_type;
2674   struct type *domain;
2675
2676   if (die->type)
2677     {
2678       return;
2679     }
2680
2681   type = alloc_type (objfile);
2682   to_type = die_type (die, objfile);
2683   domain = die_containing_type (die, objfile);
2684   smash_to_member_type (type, domain, to_type);
2685
2686   die->type = type;
2687 }
2688
2689 /* Extract all information from a DW_TAG_reference_type DIE and add to
2690    the user defined type vector.  */
2691
2692 static void
2693 read_tag_reference_type (die, objfile)
2694      struct die_info *die;
2695      struct objfile *objfile;
2696 {
2697   struct type *type;
2698   struct attribute *attr;
2699
2700   if (die->type)
2701     {
2702       return;
2703     }
2704
2705   type = lookup_reference_type (die_type (die, objfile));
2706   attr = dwarf_attr (die, DW_AT_byte_size);
2707   if (attr)
2708     {
2709       TYPE_LENGTH (type) = DW_UNSND (attr);
2710     }
2711   else
2712     {
2713       TYPE_LENGTH (type) = address_size;
2714     }
2715   die->type = type;
2716 }
2717
2718 static void
2719 read_tag_const_type (die, objfile)
2720      struct die_info *die;
2721      struct objfile *objfile;
2722 {
2723   if (die->type)
2724     {
2725       return;
2726     }
2727
2728   complain (&dwarf2_const_ignored);
2729   die->type = die_type (die, objfile);
2730 }
2731
2732 static void
2733 read_tag_volatile_type (die, objfile)
2734      struct die_info *die;
2735      struct objfile *objfile;
2736 {
2737   if (die->type)
2738     {
2739       return;
2740     }
2741
2742   complain (&dwarf2_volatile_ignored);
2743   die->type = die_type (die, objfile);
2744 }
2745
2746 /* Extract all information from a DW_TAG_string_type DIE and add to
2747    the user defined type vector.  It isn't really a user defined type,
2748    but it behaves like one, with other DIE's using an AT_user_def_type
2749    attribute to reference it.  */
2750
2751 static void
2752 read_tag_string_type (die, objfile)
2753      struct die_info *die;
2754      struct objfile *objfile;
2755 {
2756   struct type *type, *range_type, *index_type, *char_type;
2757   struct attribute *attr;
2758   unsigned int length;
2759
2760   if (die->type)
2761     {
2762       return;
2763     }
2764
2765   attr = dwarf_attr (die, DW_AT_string_length);
2766   if (attr)
2767     {
2768       length = DW_UNSND (attr);
2769     }
2770   else
2771     {
2772       length = 1;
2773     }
2774   index_type = dwarf2_fundamental_type (objfile, FT_INTEGER);
2775   range_type = create_range_type (NULL, index_type, 1, length);
2776   char_type = dwarf2_fundamental_type (objfile, FT_CHAR);
2777   type = create_string_type (char_type, range_type);
2778   die->type = type;
2779 }
2780
2781 /* Handle DIES due to C code like:
2782
2783    struct foo
2784      {
2785        int (*funcp)(int a, long l);
2786        int b;
2787      };
2788
2789    ('funcp' generates a DW_TAG_subroutine_type DIE)
2790 */
2791
2792 static void
2793 read_subroutine_type (die, objfile)
2794      struct die_info *die;
2795      struct objfile *objfile;
2796 {
2797   struct type *type;            /* Type that this function returns */
2798   struct type *ftype;           /* Function that returns above type */
2799   struct attribute *attr;
2800
2801   /* Decode the type that this subroutine returns */
2802   if (die->type)
2803     {
2804       return;
2805     }
2806   type = die_type (die, objfile);
2807   ftype = lookup_function_type (type);
2808
2809   /* All functions in C++ have prototypes.  */
2810   attr = dwarf_attr (die, DW_AT_prototyped);
2811   if ((attr && (DW_UNSND (attr) != 0))
2812       || cu_language == language_cplus)
2813     TYPE_FLAGS (ftype) |= TYPE_FLAG_PROTOTYPED;
2814
2815   if (die->has_children)
2816     {
2817       struct die_info *child_die;
2818       int nparams = 0;
2819       int iparams = 0;
2820
2821       /* Count the number of parameters.
2822          FIXME: GDB currently ignores vararg functions, but knows about
2823          vararg member functions.  */
2824       child_die = die->next;
2825       while (child_die && child_die->tag)
2826         {
2827           if (child_die->tag == DW_TAG_formal_parameter)
2828             nparams++;
2829           else if (child_die->tag == DW_TAG_unspecified_parameters)
2830             TYPE_FLAGS (ftype) |= TYPE_FLAG_VARARGS;
2831           child_die = sibling_die (child_die);
2832         }
2833
2834       /* Allocate storage for parameters and fill them in.  */
2835       TYPE_NFIELDS (ftype) = nparams;
2836       TYPE_FIELDS (ftype) = (struct field *)
2837         TYPE_ALLOC (ftype, nparams * sizeof (struct field));
2838
2839       child_die = die->next;
2840       while (child_die && child_die->tag)
2841         {
2842           if (child_die->tag == DW_TAG_formal_parameter)
2843             {
2844               /* Dwarf2 has no clean way to discern C++ static and non-static
2845                  member functions. G++ helps GDB by marking the first
2846                  parameter for non-static member functions (which is the
2847                  this pointer) as artificial. We pass this information
2848                  to dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.  */
2849               attr = dwarf_attr (child_die, DW_AT_artificial);
2850               if (attr)
2851                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
2852               else
2853                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
2854               TYPE_FIELD_TYPE (ftype, iparams) = die_type (child_die, objfile);
2855               iparams++;
2856             }
2857           child_die = sibling_die (child_die);
2858         }
2859     }
2860
2861   die->type = ftype;
2862 }
2863
2864 static void
2865 read_typedef (die, objfile)
2866      struct die_info *die;
2867      struct objfile *objfile;
2868 {
2869   struct type *type;
2870
2871   if (!die->type)
2872     {
2873       struct attribute *attr;
2874       struct type *xtype;
2875
2876       xtype = die_type (die, objfile);
2877
2878       type = alloc_type (objfile);
2879       TYPE_CODE (type) = TYPE_CODE_TYPEDEF;
2880       TYPE_FLAGS (type) |= TYPE_FLAG_TARGET_STUB;
2881       TYPE_TARGET_TYPE (type) = xtype;
2882       attr = dwarf_attr (die, DW_AT_name);
2883       if (attr && DW_STRING (attr))
2884         TYPE_NAME (type) = obsavestring (DW_STRING (attr),
2885                                          strlen (DW_STRING (attr)),
2886                                          &objfile->type_obstack);
2887
2888       die->type = type;
2889     }
2890 }
2891
2892 /* Find a representation of a given base type and install
2893    it in the TYPE field of the die.  */
2894
2895 static void
2896 read_base_type (die, objfile)
2897      struct die_info *die;
2898      struct objfile *objfile;
2899 {
2900   struct type *type;
2901   struct attribute *attr;
2902   int encoding = 0, size = 0;
2903
2904   /* If we've already decoded this die, this is a no-op. */
2905   if (die->type)
2906     {
2907       return;
2908     }
2909
2910   attr = dwarf_attr (die, DW_AT_encoding);
2911   if (attr)
2912     {
2913       encoding = DW_UNSND (attr);
2914     }
2915   attr = dwarf_attr (die, DW_AT_byte_size);
2916   if (attr)
2917     {
2918       size = DW_UNSND (attr);
2919     }
2920   attr = dwarf_attr (die, DW_AT_name);
2921   if (attr && DW_STRING (attr))
2922     {
2923       enum type_code code = TYPE_CODE_INT;
2924       int is_unsigned = 0;
2925
2926       switch (encoding)
2927         {
2928         case DW_ATE_address:
2929           /* Turn DW_ATE_address into a void * pointer.  */
2930           code = TYPE_CODE_PTR;
2931           is_unsigned = 1;
2932           break;
2933         case DW_ATE_boolean:
2934           code = TYPE_CODE_BOOL;
2935           is_unsigned = 1;
2936           break;
2937         case DW_ATE_complex_float:
2938           code = TYPE_CODE_COMPLEX;
2939           break;
2940         case DW_ATE_float:
2941           code = TYPE_CODE_FLT;
2942           break;
2943         case DW_ATE_signed:
2944         case DW_ATE_signed_char:
2945           break;
2946         case DW_ATE_unsigned:
2947         case DW_ATE_unsigned_char:
2948           is_unsigned = 1;
2949           break;
2950         default:
2951           complain (&dwarf2_unsupported_at_encoding,
2952                     dwarf_type_encoding_name (encoding));
2953           break;
2954         }
2955       type = init_type (code, size, is_unsigned, DW_STRING (attr), objfile);
2956       if (encoding == DW_ATE_address)
2957         TYPE_TARGET_TYPE (type) = dwarf2_fundamental_type (objfile, FT_VOID);
2958     }
2959   else
2960     {
2961       type = dwarf_base_type (encoding, size, objfile);
2962     }
2963   die->type = type;
2964 }
2965
2966 /* Read a whole compilation unit into a linked list of dies.  */
2967
2968 struct die_info *
2969 read_comp_unit (info_ptr, abfd)
2970     char *info_ptr;
2971     bfd *abfd;
2972 {
2973   struct die_info *first_die, *last_die, *die;
2974   char *cur_ptr;
2975   int nesting_level;
2976
2977   /* Reset die reference table, we are building a new one now. */
2978   dwarf2_empty_die_ref_table ();
2979
2980   cur_ptr = info_ptr;
2981   nesting_level = 0;
2982   first_die = last_die = NULL;
2983   do
2984     {
2985       cur_ptr = read_full_die (&die, abfd, cur_ptr);
2986       if (die->has_children)
2987         {
2988           nesting_level++;
2989         }
2990       if (die->tag == 0)
2991         {
2992           nesting_level--;
2993         }
2994
2995       die->next = NULL;
2996
2997       /* Enter die in reference hash table */
2998       store_in_ref_table (die->offset, die);
2999
3000       if (!first_die)
3001         {
3002           first_die = last_die = die;
3003         }
3004       else
3005         {
3006           last_die->next = die;
3007           last_die = die;
3008         }
3009     }
3010   while (nesting_level > 0);
3011   return first_die;
3012 }
3013
3014 /* Free a linked list of dies.  */
3015
3016 static void
3017 free_die_list (dies)
3018      struct die_info *dies;
3019 {
3020   struct die_info *die, *next;
3021
3022   die = dies;
3023   while (die)
3024     {
3025       next = die->next;
3026       free (die->attrs);
3027       free (die);
3028       die = next;
3029     }
3030 }
3031
3032 /* Read the contents of the section at OFFSET and of size SIZE from the
3033    object file specified by OBJFILE into the psymbol_obstack and return it.  */
3034
3035 static char *
3036 dwarf2_read_section (objfile, offset, size)
3037      struct objfile *objfile;
3038      file_ptr offset;
3039      unsigned int size;
3040 {
3041   bfd *abfd = objfile->obfd;
3042   char *buf;
3043
3044   if (size == 0)
3045     return NULL;
3046
3047   buf = (char *) obstack_alloc (&objfile->psymbol_obstack, size);
3048   if ((bfd_seek (abfd, offset, SEEK_SET) != 0) ||
3049       (bfd_read (buf, size, 1, abfd) != size))
3050     {
3051       buf = NULL;
3052       error ("Dwarf Error: Can't read DWARF data from '%s'",
3053         bfd_get_filename (abfd));
3054     }
3055   return buf;
3056 }
3057
3058 /* In DWARF version 2, the description of the debugging information is
3059    stored in a separate .debug_abbrev section.  Before we read any
3060    dies from a section we read in all abbreviations and install them
3061    in a hash table.  */
3062
3063 static void
3064 dwarf2_read_abbrevs (abfd, offset)
3065      bfd * abfd;
3066      unsigned int offset;
3067 {
3068   char *abbrev_ptr;
3069   struct abbrev_info *cur_abbrev;
3070   unsigned int abbrev_number, bytes_read, abbrev_name;
3071   unsigned int abbrev_form, hash_number;
3072
3073   /* empty the table */
3074   dwarf2_empty_abbrev_table (NULL);
3075
3076   abbrev_ptr = dwarf_abbrev_buffer + offset;
3077   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3078   abbrev_ptr += bytes_read;
3079
3080   /* loop until we reach an abbrev number of 0 */
3081   while (abbrev_number)
3082     {
3083       cur_abbrev = dwarf_alloc_abbrev ();
3084
3085       /* read in abbrev header */
3086       cur_abbrev->number = abbrev_number;
3087       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3088       abbrev_ptr += bytes_read;
3089       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
3090       abbrev_ptr += 1;
3091
3092       /* now read in declarations */
3093       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3094       abbrev_ptr += bytes_read;
3095       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3096       abbrev_ptr += bytes_read;
3097       while (abbrev_name)
3098         {
3099           if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
3100             {
3101               cur_abbrev->attrs = (struct attr_abbrev *)
3102                 xrealloc (cur_abbrev->attrs,
3103                           (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
3104                             * sizeof (struct attr_abbrev));
3105             }
3106           cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
3107           cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
3108           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3109           abbrev_ptr += bytes_read;
3110           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3111           abbrev_ptr += bytes_read;
3112         }
3113
3114       hash_number = abbrev_number % ABBREV_HASH_SIZE;
3115       cur_abbrev->next = dwarf2_abbrevs[hash_number];
3116       dwarf2_abbrevs[hash_number] = cur_abbrev;
3117
3118       /* Get next abbreviation.
3119          Under Irix6 the abbreviations for a compilation unit are not
3120          always properly terminated with an abbrev number of 0.
3121          Exit loop if we encounter an abbreviation which we have
3122          already read (which means we are about to read the abbreviations
3123          for the next compile unit) or if the end of the abbreviation
3124          table is reached.  */
3125       if ((unsigned int) (abbrev_ptr - dwarf_abbrev_buffer)
3126             >= dwarf_abbrev_size)
3127         break;
3128       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
3129       abbrev_ptr += bytes_read;
3130       if (dwarf2_lookup_abbrev (abbrev_number) != NULL)
3131         break;
3132     }
3133 }
3134
3135 /* Empty the abbrev table for a new compilation unit.  */
3136
3137 /* ARGSUSED */
3138 static void
3139 dwarf2_empty_abbrev_table (ignore)
3140      PTR ignore;
3141 {
3142   int i;
3143   struct abbrev_info *abbrev, *next;
3144
3145   for (i = 0; i < ABBREV_HASH_SIZE; ++i)
3146     {
3147       next = NULL;
3148       abbrev = dwarf2_abbrevs[i];
3149       while (abbrev)
3150         {
3151           next = abbrev->next;
3152           free (abbrev->attrs);
3153           free (abbrev);
3154           abbrev = next;
3155         }
3156       dwarf2_abbrevs[i] = NULL;
3157     }
3158 }
3159
3160 /* Lookup an abbrev_info structure in the abbrev hash table.  */
3161
3162 static struct abbrev_info *
3163 dwarf2_lookup_abbrev (number)
3164      unsigned int number;
3165 {
3166   unsigned int hash_number;
3167   struct abbrev_info *abbrev;
3168
3169   hash_number = number % ABBREV_HASH_SIZE;
3170   abbrev = dwarf2_abbrevs[hash_number];
3171
3172   while (abbrev)
3173     {
3174       if (abbrev->number == number)
3175         return abbrev;
3176       else
3177         abbrev = abbrev->next;
3178     }
3179   return NULL;
3180 }
3181
3182 /* Read a minimal amount of information into the minimal die structure.  */
3183
3184 static char *
3185 read_partial_die (part_die, abfd, info_ptr, has_pc_info)
3186      struct partial_die_info *part_die;
3187      bfd * abfd;
3188      char *info_ptr;
3189      int *has_pc_info;
3190 {
3191   unsigned int abbrev_number, bytes_read, i;
3192   struct abbrev_info *abbrev;
3193   struct attribute attr;
3194   struct attribute spec_attr;
3195   int found_spec_attr = 0;
3196   int has_low_pc_attr  = 0;
3197   int has_high_pc_attr = 0;
3198
3199   *part_die = zeroed_partial_die;
3200   *has_pc_info = 0;
3201   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3202   info_ptr += bytes_read;
3203   if (!abbrev_number)
3204     return info_ptr;
3205
3206   abbrev = dwarf2_lookup_abbrev (abbrev_number);
3207   if (!abbrev)
3208     {
3209       error ("Dwarf Error: Could not find abbrev number %d.", abbrev_number);
3210     }
3211   part_die->offset = info_ptr - dwarf_info_buffer;
3212   part_die->tag = abbrev->tag;
3213   part_die->has_children = abbrev->has_children;
3214   part_die->abbrev = abbrev_number;
3215
3216   for (i = 0; i < abbrev->num_attrs; ++i)
3217     {
3218       info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr);
3219
3220       /* Store the data if it is of an attribute we want to keep in a
3221          partial symbol table.  */
3222       switch (attr.name)
3223         {
3224         case DW_AT_name:
3225
3226           /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
3227           if (part_die->name == NULL)
3228             part_die->name = DW_STRING (&attr);
3229           break;
3230         case DW_AT_MIPS_linkage_name:
3231           part_die->name = DW_STRING (&attr);
3232           break;
3233         case DW_AT_low_pc:
3234           has_low_pc_attr = 1;
3235           part_die->lowpc = DW_ADDR (&attr);
3236           break;
3237         case DW_AT_high_pc:
3238           has_high_pc_attr = 1;
3239           part_die->highpc = DW_ADDR (&attr);
3240           break;
3241         case DW_AT_location:
3242           part_die->locdesc = DW_BLOCK (&attr);
3243           break;
3244         case DW_AT_language:
3245           part_die->language = DW_UNSND (&attr);
3246           break;
3247         case DW_AT_external:
3248           part_die->is_external = DW_UNSND (&attr);
3249           break;
3250         case DW_AT_declaration:
3251           part_die->is_declaration = DW_UNSND (&attr);
3252           break;
3253         case DW_AT_type:
3254           part_die->has_type = 1;
3255           break;
3256         case DW_AT_abstract_origin:
3257         case DW_AT_specification:
3258           found_spec_attr = 1;
3259           spec_attr = attr;
3260           break;
3261         case DW_AT_sibling:
3262           /* Ignore absolute siblings, they might point outside of
3263              the current compile unit.  */
3264           if (attr.form == DW_FORM_ref_addr)
3265             complain(&dwarf2_absolute_sibling_complaint);
3266           else
3267             part_die->sibling =
3268               dwarf_info_buffer + dwarf2_get_ref_die_offset (&attr);
3269           break;
3270         default:
3271           break;
3272         }
3273     }
3274
3275   /* If we found a reference attribute and the die has no name, try
3276      to find a name in the referred to die.  */
3277
3278   if (found_spec_attr && part_die->name == NULL)
3279     {
3280       struct partial_die_info spec_die;
3281       char *spec_ptr;
3282       int dummy;
3283
3284       spec_ptr = dwarf_info_buffer + dwarf2_get_ref_die_offset (&spec_attr);
3285       read_partial_die (&spec_die, abfd, spec_ptr, &dummy);
3286       if (spec_die.name)
3287         {
3288           part_die->name = spec_die.name;
3289
3290           /* Copy DW_AT_external attribute if it is set.  */
3291           if (spec_die.is_external)
3292             part_die->is_external = spec_die.is_external;
3293         }
3294     }
3295
3296   /* When using the GNU linker, .gnu.linkonce. sections are used to
3297      eliminate duplicate copies of functions and vtables and such.
3298      The linker will arbitrarily choose one and discard the others.
3299      The AT_*_pc values for such functions refer to local labels in
3300      these sections.  If the section from that file was discarded, the
3301      labels are not in the output, so the relocs get a value of 0.
3302      If this is a discarded function, mark the pc bounds as invalid,
3303      so that GDB will ignore it.  */
3304   if (has_low_pc_attr && has_high_pc_attr
3305       && part_die->lowpc < part_die->highpc
3306       && (part_die->lowpc != 0
3307           || (bfd_get_file_flags (abfd) & HAS_RELOC)))
3308     *has_pc_info = 1;
3309   return info_ptr;
3310 }
3311
3312 /* Read the die from the .debug_info section buffer.  And set diep to
3313    point to a newly allocated die with its information.  */
3314
3315 static char *
3316 read_full_die (diep, abfd, info_ptr)
3317      struct die_info **diep;
3318      bfd *abfd;
3319      char *info_ptr;
3320 {
3321   unsigned int abbrev_number, bytes_read, i, offset;
3322   struct abbrev_info *abbrev;
3323   struct die_info *die;
3324
3325   offset = info_ptr - dwarf_info_buffer;
3326   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3327   info_ptr += bytes_read;
3328   if (!abbrev_number)
3329     {
3330       die = dwarf_alloc_die ();
3331       die->tag = 0;
3332       die->abbrev = abbrev_number;
3333       die->type = NULL;
3334       *diep = die;
3335       return info_ptr;
3336     }
3337
3338   abbrev = dwarf2_lookup_abbrev (abbrev_number);
3339   if (!abbrev)
3340     {
3341       error ("Dwarf Error: could not find abbrev number %d.", abbrev_number);
3342     }
3343   die = dwarf_alloc_die ();
3344   die->offset = offset;
3345   die->tag = abbrev->tag;
3346   die->has_children = abbrev->has_children;
3347   die->abbrev = abbrev_number;
3348   die->type = NULL;
3349
3350   die->num_attrs = abbrev->num_attrs;
3351   die->attrs = (struct attribute *)
3352     xmalloc (die->num_attrs * sizeof (struct attribute));
3353
3354   for (i = 0; i < abbrev->num_attrs; ++i)
3355     {
3356       info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
3357                                  abfd, info_ptr);
3358     }
3359
3360   *diep = die;
3361   return info_ptr;
3362 }
3363
3364 /* Read an attribute described by an abbreviated attribute.  */
3365
3366 static char *
3367 read_attribute (attr, abbrev, abfd, info_ptr)
3368      struct attribute *attr;
3369      struct attr_abbrev *abbrev;
3370      bfd *abfd;
3371      char *info_ptr;
3372 {
3373   unsigned int bytes_read;
3374   struct dwarf_block *blk;
3375
3376   attr->name = abbrev->name;
3377   attr->form = abbrev->form;
3378   switch (abbrev->form)
3379     {
3380     case DW_FORM_addr:
3381     case DW_FORM_ref_addr:
3382       DW_ADDR (attr) = read_address (abfd, info_ptr);
3383       info_ptr += address_size;
3384       break;
3385     case DW_FORM_block2:
3386       blk = dwarf_alloc_block ();
3387       blk->size = read_2_bytes (abfd, info_ptr);
3388       info_ptr += 2;
3389       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3390       info_ptr += blk->size;
3391       DW_BLOCK (attr) = blk;
3392       break;
3393     case DW_FORM_block4:
3394       blk = dwarf_alloc_block ();
3395       blk->size = read_4_bytes (abfd, info_ptr);
3396       info_ptr += 4;
3397       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3398       info_ptr += blk->size;
3399       DW_BLOCK (attr) = blk;
3400       break;
3401     case DW_FORM_data2:
3402       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3403       info_ptr += 2;
3404       break;
3405     case DW_FORM_data4:
3406       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3407       info_ptr += 4;
3408       break;
3409     case DW_FORM_data8:
3410       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
3411       info_ptr += 8;
3412       break;
3413     case DW_FORM_string:
3414       DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
3415       info_ptr += bytes_read;
3416       break;
3417     case DW_FORM_block:
3418       blk = dwarf_alloc_block ();
3419       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3420       info_ptr += bytes_read;
3421       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3422       info_ptr += blk->size;
3423       DW_BLOCK (attr) = blk;
3424       break;
3425     case DW_FORM_block1:
3426       blk = dwarf_alloc_block ();
3427       blk->size = read_1_byte (abfd, info_ptr);
3428       info_ptr += 1;
3429       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
3430       info_ptr += blk->size;
3431       DW_BLOCK (attr) = blk;
3432       break;
3433     case DW_FORM_data1:
3434       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3435       info_ptr += 1;
3436       break;
3437     case DW_FORM_flag:
3438       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3439       info_ptr += 1;
3440       break;
3441     case DW_FORM_sdata:
3442       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
3443       info_ptr += bytes_read;
3444       break;
3445     case DW_FORM_udata:
3446       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3447       info_ptr += bytes_read;
3448       break;
3449     case DW_FORM_ref1:
3450       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
3451       info_ptr += 1;
3452       break;
3453     case DW_FORM_ref2:
3454       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
3455       info_ptr += 2;
3456       break;
3457     case DW_FORM_ref4:
3458       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
3459       info_ptr += 4;
3460       break;
3461     case DW_FORM_ref_udata:
3462       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3463       info_ptr += bytes_read;
3464       break;
3465     case DW_FORM_strp:
3466     case DW_FORM_indirect:
3467     default:
3468       error ("Dwarf Error: Cannot handle %s in DWARF reader.",
3469              dwarf_form_name (abbrev->form));
3470     }
3471   return info_ptr;
3472 }
3473
3474 /* read dwarf information from a buffer */
3475
3476 static unsigned int
3477 read_1_byte (abfd, buf)
3478      bfd *abfd;
3479      char *buf;
3480 {
3481   return bfd_get_8 (abfd, (bfd_byte *) buf);
3482 }
3483
3484 static int
3485 read_1_signed_byte (abfd, buf)
3486      bfd *abfd;
3487      char *buf;
3488 {
3489   return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
3490 }
3491
3492 static unsigned int
3493 read_2_bytes (abfd, buf)
3494      bfd *abfd;
3495      char *buf;
3496 {
3497   return bfd_get_16 (abfd, (bfd_byte *) buf);
3498 }
3499
3500 static int
3501 read_2_signed_bytes (abfd, buf)
3502      bfd *abfd;
3503      char *buf;
3504 {
3505   return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
3506 }
3507
3508 static unsigned int
3509 read_4_bytes (abfd, buf)
3510      bfd *abfd;
3511      char *buf;
3512 {
3513   return bfd_get_32 (abfd, (bfd_byte *) buf);
3514 }
3515
3516 static int
3517 read_4_signed_bytes (abfd, buf)
3518      bfd *abfd;
3519      char *buf;
3520 {
3521   return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
3522 }
3523
3524 static unsigned int
3525 read_8_bytes (abfd, buf)
3526      bfd *abfd;
3527      char *buf;
3528 {
3529   return bfd_get_64 (abfd, (bfd_byte *) buf);
3530 }
3531
3532 static CORE_ADDR
3533 read_address (abfd, buf)
3534      bfd *abfd;
3535      char *buf;
3536 {
3537   CORE_ADDR retval = 0;
3538
3539   switch (address_size)
3540     {
3541     case 4:
3542       retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3543       break;
3544     case 8:
3545       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3546       break;
3547     default:
3548       /* *THE* alternative is 8, right? */
3549       abort ();
3550     }
3551   /* If the address being read is larger than the address that is
3552      applicable for the object file format then mask it down to the
3553      correct size.  Take care to avoid unnecessary shift or shift
3554      overflow */
3555   if (address_size > address_significant_size
3556       && address_significant_size < sizeof (CORE_ADDR))
3557     {
3558       CORE_ADDR mask = ((CORE_ADDR) 0) - 1;
3559       retval &= ~(mask << (address_significant_size * 8));
3560     }
3561   return retval;
3562 }
3563
3564 static char *
3565 read_n_bytes (abfd, buf, size)
3566      bfd * abfd;
3567      char *buf;
3568      unsigned int size;
3569 {
3570   /* If the size of a host char is 8 bits, we can return a pointer
3571      to the buffer, otherwise we have to copy the data to a buffer
3572      allocated on the temporary obstack.  */
3573 #if HOST_CHAR_BIT == 8
3574   return buf;
3575 #else
3576   char *ret;
3577   unsigned int i;
3578
3579   ret = obstack_alloc (&dwarf2_tmp_obstack, size);
3580   for (i = 0; i < size; ++i)
3581     {
3582       ret[i] = bfd_get_8 (abfd, (bfd_byte *) buf);
3583       buf++;
3584     }
3585   return ret;
3586 #endif
3587 }
3588
3589 static char *
3590 read_string (abfd, buf, bytes_read_ptr)
3591      bfd *abfd;
3592      char *buf;
3593      unsigned int *bytes_read_ptr;
3594 {
3595   /* If the size of a host char is 8 bits, we can return a pointer
3596      to the string, otherwise we have to copy the string to a buffer
3597      allocated on the temporary obstack.  */
3598 #if HOST_CHAR_BIT == 8
3599   if (*buf == '\0')
3600     {
3601       *bytes_read_ptr = 1;
3602       return NULL;
3603     }
3604   *bytes_read_ptr = strlen (buf) + 1;
3605   return buf;
3606 #else
3607   int byte;
3608   unsigned int i = 0;
3609
3610   while ((byte = bfd_get_8 (abfd, (bfd_byte *) buf)) != 0)
3611     {
3612       obstack_1grow (&dwarf2_tmp_obstack, byte);
3613       i++;
3614       buf++;
3615     }
3616   if (i == 0)
3617     {
3618       *bytes_read_ptr = 1;
3619       return NULL;
3620     }
3621   obstack_1grow (&dwarf2_tmp_obstack, '\0');
3622   *bytes_read_ptr = i + 1;
3623   return obstack_finish (&dwarf2_tmp_obstack);
3624 #endif
3625 }
3626
3627 static unsigned int
3628 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
3629      bfd *abfd;
3630      char *buf;
3631      unsigned int *bytes_read_ptr;
3632 {
3633   unsigned int result, num_read;
3634   int i, shift;
3635   unsigned char byte;
3636
3637   result = 0;
3638   shift = 0;
3639   num_read = 0;
3640   i = 0;
3641   while (1)
3642     {
3643       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3644       buf++;
3645       num_read++;
3646       result |= ((byte & 127) << shift);
3647       if ((byte & 128) == 0)
3648         {
3649           break;
3650         }
3651       shift += 7;
3652     }
3653   *bytes_read_ptr = num_read;
3654   return result;
3655 }
3656
3657 static int
3658 read_signed_leb128 (abfd, buf, bytes_read_ptr)
3659      bfd *abfd;
3660      char *buf;
3661      unsigned int *bytes_read_ptr;
3662 {
3663   int result;
3664   int i, shift, size, num_read;
3665   unsigned char byte;
3666
3667   result = 0;
3668   shift = 0;
3669   size = 32;
3670   num_read = 0;
3671   i = 0;
3672   while (1)
3673     {
3674       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3675       buf++;
3676       num_read++;
3677       result |= ((byte & 127) << shift);
3678       shift += 7;
3679       if ((byte & 128) == 0)
3680         {
3681           break;
3682         }
3683     }
3684   if ((shift < size) && (byte & 0x40))
3685     {
3686       result |= -(1 << shift);
3687     }
3688   *bytes_read_ptr = num_read;
3689   return result;
3690 }
3691
3692 static void
3693 set_cu_language (lang)
3694      unsigned int lang;
3695 {
3696   switch (lang)
3697     {
3698     case DW_LANG_C89:
3699     case DW_LANG_C:
3700       cu_language = language_c;
3701       break;
3702     case DW_LANG_C_plus_plus:
3703       cu_language = language_cplus;
3704       break;
3705     case DW_LANG_Fortran77:
3706     case DW_LANG_Fortran90:
3707       cu_language = language_fortran;
3708       break;
3709     case DW_LANG_Mips_Assembler:
3710       cu_language = language_asm;
3711       break;
3712     case DW_LANG_Ada83:
3713     case DW_LANG_Cobol74:
3714     case DW_LANG_Cobol85:
3715     case DW_LANG_Pascal83:
3716     case DW_LANG_Modula2:
3717     default:
3718       cu_language = language_unknown;
3719       break;
3720     }
3721   cu_language_defn = language_def (cu_language);
3722 }
3723
3724 /* Return the named attribute or NULL if not there.  */
3725
3726 static struct attribute *
3727 dwarf_attr (die, name)
3728      struct die_info *die;
3729      unsigned int name;
3730 {
3731   unsigned int i;
3732   struct attribute *spec = NULL;
3733
3734   for (i = 0; i < die->num_attrs; ++i)
3735     {
3736       if (die->attrs[i].name == name)
3737         {
3738           return &die->attrs[i];
3739         }
3740       if (die->attrs[i].name == DW_AT_specification
3741           || die->attrs[i].name == DW_AT_abstract_origin)
3742         spec = &die->attrs[i];
3743     }
3744   if (spec)
3745     {
3746       struct die_info *ref_die =
3747         follow_die_ref (dwarf2_get_ref_die_offset (spec));
3748
3749       if (ref_die)
3750         return dwarf_attr (ref_die, name);
3751     }
3752     
3753   return NULL;
3754 }
3755
3756 /* Decode the line number information for the compilation unit whose
3757    line number info is at OFFSET in the .debug_line section.
3758    The compilation directory of the file is passed in COMP_DIR.  */
3759
3760 struct filenames
3761 {
3762   unsigned int num_files;
3763   struct fileinfo
3764   {
3765     char *name;
3766     unsigned int dir;
3767     unsigned int time;
3768     unsigned int size;
3769   }
3770   *files;
3771 };
3772
3773 struct directories
3774 {
3775   unsigned int num_dirs;
3776   char **dirs;
3777 };
3778
3779 static void
3780 dwarf_decode_lines (offset, comp_dir, abfd)
3781      unsigned int offset;
3782      char *comp_dir;
3783      bfd *abfd;
3784 {
3785   char *line_ptr;
3786   char *line_end;
3787   struct line_head lh;
3788   struct cleanup *back_to;
3789   unsigned int i, bytes_read;
3790   char *cur_file, *cur_dir;
3791   unsigned char op_code, extended_op, adj_opcode;
3792
3793 #define FILE_ALLOC_CHUNK 5
3794 #define DIR_ALLOC_CHUNK 5
3795
3796   struct filenames files;
3797   struct directories dirs;
3798
3799   if (dwarf_line_buffer == NULL)
3800     {
3801       complain (&dwarf2_missing_line_number_section);
3802       return;
3803     }
3804
3805   files.num_files = 0;
3806   files.files = NULL;
3807
3808   dirs.num_dirs = 0;
3809   dirs.dirs = NULL;
3810
3811   line_ptr = dwarf_line_buffer + offset;
3812
3813   /* read in the prologue */
3814   lh.total_length = read_4_bytes (abfd, line_ptr);
3815   line_ptr += 4;
3816   line_end = line_ptr + lh.total_length;
3817   lh.version = read_2_bytes (abfd, line_ptr);
3818   line_ptr += 2;
3819   lh.prologue_length = read_4_bytes (abfd, line_ptr);
3820   line_ptr += 4;
3821   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
3822   line_ptr += 1;
3823   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
3824   line_ptr += 1;
3825   lh.line_base = read_1_signed_byte (abfd, line_ptr);
3826   line_ptr += 1;
3827   lh.line_range = read_1_byte (abfd, line_ptr);
3828   line_ptr += 1;
3829   lh.opcode_base = read_1_byte (abfd, line_ptr);
3830   line_ptr += 1;
3831   lh.standard_opcode_lengths = (unsigned char *)
3832     xmalloc (lh.opcode_base * sizeof (unsigned char));
3833   back_to = make_cleanup ((make_cleanup_func) free_current_contents, 
3834                           &lh.standard_opcode_lengths);
3835
3836   lh.standard_opcode_lengths[0] = 1;
3837   for (i = 1; i < lh.opcode_base; ++i)
3838     {
3839       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
3840       line_ptr += 1;
3841     }
3842
3843   /* Read directory table  */
3844   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3845     {
3846       line_ptr += bytes_read;
3847       if ((dirs.num_dirs % DIR_ALLOC_CHUNK) == 0)
3848         {
3849           dirs.dirs = (char **)
3850             xrealloc (dirs.dirs,
3851                       (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
3852           if (dirs.num_dirs == 0)
3853             make_cleanup ((make_cleanup_func) free_current_contents, &dirs.dirs);
3854         }
3855       dirs.dirs[dirs.num_dirs++] = cur_dir;
3856     }
3857   line_ptr += bytes_read;
3858
3859   /* Read file name table */
3860   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3861     {
3862       line_ptr += bytes_read;
3863       if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3864         {
3865           files.files = (struct fileinfo *)
3866             xrealloc (files.files,
3867                       (files.num_files + FILE_ALLOC_CHUNK)
3868                         * sizeof (struct fileinfo));
3869           if (files.num_files == 0)
3870             make_cleanup ((make_cleanup_func) free_current_contents, 
3871                           &files.files);
3872         }
3873       files.files[files.num_files].name = cur_file;
3874       files.files[files.num_files].dir =
3875         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3876       line_ptr += bytes_read;
3877       files.files[files.num_files].time =
3878         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3879       line_ptr += bytes_read;
3880       files.files[files.num_files].size =
3881         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3882       line_ptr += bytes_read;
3883       files.num_files++;
3884     }
3885   line_ptr += bytes_read;
3886
3887   /* Read the statement sequences until there's nothing left.  */
3888   while (line_ptr < line_end)
3889     {
3890       /* state machine registers  */
3891       CORE_ADDR address = 0;
3892       unsigned int file = 1;
3893       unsigned int line = 1;
3894       unsigned int column = 0;
3895       int is_stmt = lh.default_is_stmt;
3896       int basic_block = 0;
3897       int end_sequence = 0;
3898
3899       /* Start a subfile for the current file of the state machine.  */
3900       if (files.num_files >= file)
3901         {
3902           /* The file and directory tables are 0 based, the references
3903              are 1 based.  */
3904           dwarf2_start_subfile (files.files[file - 1].name,
3905                                 (files.files[file - 1].dir
3906                                  ? dirs.dirs[files.files[file - 1].dir - 1]
3907                                  : comp_dir));
3908         }
3909
3910       /* Decode the table. */
3911       while (! end_sequence)
3912         {
3913           op_code = read_1_byte (abfd, line_ptr);
3914           line_ptr += 1;
3915           switch (op_code)
3916             {
3917             case DW_LNS_extended_op:
3918               line_ptr += 1;    /* ignore length */
3919               extended_op = read_1_byte (abfd, line_ptr);
3920               line_ptr += 1;
3921               switch (extended_op)
3922                 {
3923                 case DW_LNE_end_sequence:
3924                   end_sequence = 1;
3925                   record_line (current_subfile, line, address);
3926                   break;
3927                 case DW_LNE_set_address:
3928                   address = read_address (abfd, line_ptr) + baseaddr;
3929                   line_ptr += address_size;
3930                   break;
3931                 case DW_LNE_define_file:
3932                   cur_file = read_string (abfd, line_ptr, &bytes_read);
3933                   line_ptr += bytes_read;
3934                   if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3935                     {
3936                       files.files = (struct fileinfo *)
3937                         xrealloc (files.files,
3938                                   (files.num_files + FILE_ALLOC_CHUNK)
3939                                     * sizeof (struct fileinfo));
3940                       if (files.num_files == 0)
3941                         make_cleanup ((make_cleanup_func) free_current_contents, 
3942                                       &files.files);
3943                     }
3944                   files.files[files.num_files].name = cur_file;
3945                   files.files[files.num_files].dir =
3946                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3947                   line_ptr += bytes_read;
3948                   files.files[files.num_files].time =
3949                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3950                   line_ptr += bytes_read;
3951                   files.files[files.num_files].size =
3952                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3953                   line_ptr += bytes_read;
3954                   files.num_files++;
3955                   break;
3956                 default:
3957                   complain (&dwarf2_mangled_line_number_section);
3958                   goto done;
3959                 }
3960               break;
3961             case DW_LNS_copy:
3962               record_line (current_subfile, line, address);
3963               basic_block = 0;
3964               break;
3965             case DW_LNS_advance_pc:
3966               address += lh.minimum_instruction_length
3967                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3968               line_ptr += bytes_read;
3969               break;
3970             case DW_LNS_advance_line:
3971               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
3972               line_ptr += bytes_read;
3973               break;
3974             case DW_LNS_set_file:
3975               /* The file and directory tables are 0 based, the references
3976                  are 1 based.  */
3977               file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3978               line_ptr += bytes_read;
3979               dwarf2_start_subfile
3980                 (files.files[file - 1].name,
3981                  (files.files[file - 1].dir
3982                   ? dirs.dirs[files.files[file - 1].dir - 1]
3983                   : comp_dir));
3984               break;
3985             case DW_LNS_set_column:
3986               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3987               line_ptr += bytes_read;
3988               break;
3989             case DW_LNS_negate_stmt:
3990               is_stmt = (!is_stmt);
3991               break;
3992             case DW_LNS_set_basic_block:
3993               basic_block = 1;
3994               break;
3995             case DW_LNS_const_add_pc:
3996               address += (255 - lh.opcode_base) / lh.line_range;
3997               break;
3998             case DW_LNS_fixed_advance_pc:
3999               address += read_2_bytes (abfd, line_ptr);
4000               line_ptr += 2;
4001               break;
4002             default:            /* special operand */
4003               adj_opcode = op_code - lh.opcode_base;
4004               address += (adj_opcode / lh.line_range)
4005                 * lh.minimum_instruction_length;
4006               line += lh.line_base + (adj_opcode % lh.line_range);
4007               /* append row to matrix using current values */
4008               record_line (current_subfile, line, address);
4009               basic_block = 1;
4010             }
4011         }
4012     }
4013 done:
4014   do_cleanups (back_to);
4015 }
4016
4017 /* Start a subfile for DWARF.  FILENAME is the name of the file and
4018    DIRNAME the name of the source directory which contains FILENAME
4019    or NULL if not known.
4020    This routine tries to keep line numbers from identical absolute and
4021    relative file names in a common subfile.
4022
4023    Using the `list' example from the GDB testsuite, which resides in
4024    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
4025    of /srcdir/list0.c yields the following debugging information for list0.c:
4026
4027         DW_AT_name:             /srcdir/list0.c
4028         DW_AT_comp_dir:         /compdir
4029         files.files[0].name:    list0.h         
4030         files.files[0].dir:     /srcdir
4031         files.files[1].name:    list0.c         
4032         files.files[1].dir:     /srcdir
4033
4034    The line number information for list0.c has to end up in a single
4035    subfile, so that `break /srcdir/list0.c:1' works as expected.  */
4036
4037 static void
4038 dwarf2_start_subfile (filename, dirname)
4039      char *filename;
4040      char *dirname;
4041 {
4042   /* If the filename isn't absolute, try to match an existing subfile
4043      with the full pathname.  */
4044
4045   if (*filename != '/' && dirname != NULL)
4046     {
4047       struct subfile *subfile;
4048       char *fullname = concat (dirname, "/", filename, NULL);
4049
4050       for (subfile = subfiles; subfile; subfile = subfile->next)
4051         {
4052           if (STREQ (subfile->name, fullname))
4053             {
4054               current_subfile = subfile;
4055               free (fullname);
4056               return;
4057             }
4058         }
4059       free (fullname);
4060     }
4061   start_subfile (filename, dirname);
4062 }
4063
4064 /* Given a pointer to a DWARF information entry, figure out if we need
4065    to make a symbol table entry for it, and if so, create a new entry
4066    and return a pointer to it.
4067    If TYPE is NULL, determine symbol type from the die, otherwise
4068    used the passed type.
4069   */
4070
4071 static struct symbol *
4072 new_symbol (die, type, objfile)
4073      struct die_info *die;
4074      struct type *type;
4075      struct objfile *objfile;
4076 {
4077   struct symbol *sym = NULL;
4078   char *name;
4079   struct attribute *attr = NULL;
4080   struct attribute *attr2 = NULL;
4081   CORE_ADDR addr;
4082
4083   name = dwarf2_linkage_name (die);
4084   if (name)
4085     {
4086       sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
4087                                              sizeof (struct symbol));
4088       OBJSTAT (objfile, n_syms++);
4089       memset (sym, 0, sizeof (struct symbol));
4090       SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
4091                                         &objfile->symbol_obstack);
4092
4093       /* Default assumptions.
4094          Use the passed type or decode it from the die.  */
4095       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4096       SYMBOL_CLASS (sym) = LOC_STATIC;
4097       if (type != NULL)
4098         SYMBOL_TYPE (sym) = type;
4099       else
4100         SYMBOL_TYPE (sym) = die_type (die, objfile);
4101       attr = dwarf_attr (die, DW_AT_decl_line);
4102       if (attr)
4103         {
4104           SYMBOL_LINE (sym) = DW_UNSND (attr);
4105         }
4106
4107       /* If this symbol is from a C++ compilation, then attempt to
4108          cache the demangled form for future reference.  This is a
4109          typical time versus space tradeoff, that was decided in favor
4110          of time because it sped up C++ symbol lookups by a factor of
4111          about 20. */
4112
4113       SYMBOL_LANGUAGE (sym) = cu_language;
4114       SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
4115       switch (die->tag)
4116         {
4117         case DW_TAG_label:
4118           attr = dwarf_attr (die, DW_AT_low_pc);
4119           if (attr)
4120             {
4121               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
4122             }
4123           SYMBOL_CLASS (sym) = LOC_LABEL;
4124           break;
4125         case DW_TAG_subprogram:
4126           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
4127              finish_block.  */
4128           SYMBOL_CLASS (sym) = LOC_BLOCK;
4129           attr2 = dwarf_attr (die, DW_AT_external);
4130           if (attr2 && (DW_UNSND (attr2) != 0))
4131             {
4132               add_symbol_to_list (sym, &global_symbols);
4133             }
4134           else
4135             {
4136               add_symbol_to_list (sym, list_in_scope);
4137             }
4138           break;
4139         case DW_TAG_variable:
4140           /* Compilation with minimal debug info may result in variables
4141              with missing type entries. Change the misleading `void' type
4142              to something sensible.  */
4143           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
4144             SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
4145                                            TARGET_INT_BIT / HOST_CHAR_BIT, 0,
4146                                            "<variable, no debug info>",
4147                                            objfile);
4148           attr = dwarf_attr (die, DW_AT_const_value);
4149           if (attr)
4150             {
4151               dwarf2_const_value (attr, sym, objfile);
4152               attr2 = dwarf_attr (die, DW_AT_external);
4153               if (attr2 && (DW_UNSND (attr2) != 0))
4154                 add_symbol_to_list (sym, &global_symbols);
4155               else
4156                 add_symbol_to_list (sym, list_in_scope);
4157               break;
4158             }
4159           attr = dwarf_attr (die, DW_AT_location);
4160           if (attr)
4161             {
4162               attr2 = dwarf_attr (die, DW_AT_external);
4163               if (attr2 && (DW_UNSND (attr2) != 0))
4164                 {
4165                   SYMBOL_VALUE_ADDRESS (sym) =
4166                     decode_locdesc (DW_BLOCK (attr), objfile);
4167                   add_symbol_to_list (sym, &global_symbols);
4168
4169                   /* In shared libraries the address of the variable
4170                      in the location descriptor might still be relocatable,
4171                      so its value could be zero.
4172                      Enter the symbol as a LOC_UNRESOLVED symbol, if its
4173                      value is zero, the address of the variable will then
4174                      be determined from the minimal symbol table whenever
4175                      the variable is referenced.  */
4176                   if (SYMBOL_VALUE_ADDRESS (sym))
4177                     {
4178                       SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
4179                       SYMBOL_CLASS (sym) = LOC_STATIC;
4180                     }
4181                   else
4182                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4183                 }
4184               else
4185                 {
4186                   SYMBOL_VALUE (sym) = addr =
4187                     decode_locdesc (DW_BLOCK (attr), objfile);
4188                   add_symbol_to_list (sym, list_in_scope);
4189                   if (optimized_out)
4190                     {
4191                       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
4192                     }
4193                   else if (isreg)
4194                     {
4195                       SYMBOL_CLASS (sym) = LOC_REGISTER;
4196                     }
4197                   else if (offreg)
4198                     {
4199                       SYMBOL_CLASS (sym) = LOC_BASEREG;
4200                       SYMBOL_BASEREG (sym) = basereg;
4201                     }
4202                   else if (islocal)
4203                     {
4204                       SYMBOL_CLASS (sym) = LOC_LOCAL;
4205                     }
4206                   else
4207                     {
4208                       SYMBOL_CLASS (sym) = LOC_STATIC;
4209                       SYMBOL_VALUE_ADDRESS (sym) = addr + baseaddr;
4210                     }
4211                 }
4212             }
4213           else
4214             {
4215               /* We do not know the address of this symbol.
4216                  If it is an external symbol and we have type information
4217                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
4218                  The address of the variable will then be determined from
4219                  the minimal symbol table whenever the variable is
4220                  referenced.  */
4221               attr2 = dwarf_attr (die, DW_AT_external);
4222               if (attr2 && (DW_UNSND (attr2) != 0)
4223                   && dwarf_attr (die, DW_AT_type) != NULL)
4224                 {
4225                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4226                   add_symbol_to_list (sym, &global_symbols);
4227                 }
4228             }
4229           break;
4230         case DW_TAG_formal_parameter:
4231           attr = dwarf_attr (die, DW_AT_location);
4232           if (attr)
4233             {
4234               SYMBOL_VALUE (sym) = decode_locdesc (DW_BLOCK (attr), objfile);
4235               if (isreg)
4236                 {
4237                   SYMBOL_CLASS (sym) = LOC_REGPARM;
4238                 }
4239               else if (offreg)
4240                 {
4241                   SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
4242                   SYMBOL_BASEREG (sym) = basereg;
4243                 }
4244               else
4245                 {
4246                   SYMBOL_CLASS (sym) = LOC_ARG;
4247                 }
4248             }
4249           attr = dwarf_attr (die, DW_AT_const_value);
4250           if (attr)
4251             {
4252               dwarf2_const_value (attr, sym, objfile);
4253             }
4254           add_symbol_to_list (sym, list_in_scope);
4255           break;
4256         case DW_TAG_unspecified_parameters:
4257           /* From varargs functions; gdb doesn't seem to have any
4258              interest in this information, so just ignore it for now.
4259              (FIXME?) */
4260           break;
4261         case DW_TAG_class_type:
4262         case DW_TAG_structure_type:
4263         case DW_TAG_union_type:
4264         case DW_TAG_enumeration_type:
4265           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4266           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
4267           add_symbol_to_list (sym, list_in_scope);
4268
4269           /* The semantics of C++ state that "struct foo { ... }" also
4270              defines a typedef for "foo". Synthesize a typedef symbol so
4271              that "ptype foo" works as expected.  */
4272           if (cu_language == language_cplus)
4273             {
4274               struct symbol *typedef_sym = (struct symbol *)
4275                 obstack_alloc (&objfile->symbol_obstack,
4276                                sizeof (struct symbol));
4277               *typedef_sym = *sym;
4278               SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
4279               if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
4280                 TYPE_NAME (SYMBOL_TYPE (sym)) =
4281                   obsavestring (SYMBOL_NAME (sym),
4282                                 strlen (SYMBOL_NAME (sym)),
4283                                 &objfile->type_obstack);
4284               add_symbol_to_list (typedef_sym, list_in_scope);
4285             }
4286           break;
4287         case DW_TAG_typedef:
4288         case DW_TAG_base_type:
4289           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4290           SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4291           add_symbol_to_list (sym, list_in_scope);
4292           break;
4293         case DW_TAG_enumerator:
4294           attr = dwarf_attr (die, DW_AT_const_value);
4295           if (attr)
4296             {
4297               dwarf2_const_value (attr, sym, objfile);
4298             }
4299           add_symbol_to_list (sym, list_in_scope);
4300           break;
4301         default:
4302           /* Not a tag we recognize.  Hopefully we aren't processing
4303              trash data, but since we must specifically ignore things
4304              we don't recognize, there is nothing else we should do at
4305              this point. */
4306           complain (&dwarf2_unsupported_tag, dwarf_tag_name (die->tag));
4307           break;
4308         }
4309     }
4310   return (sym);
4311 }
4312
4313 /* Copy constant value from an attribute to a symbol.  */
4314
4315 static void
4316 dwarf2_const_value (attr, sym, objfile)
4317      struct attribute *attr;
4318      struct symbol *sym;
4319      struct objfile *objfile;
4320 {
4321   struct dwarf_block *blk;
4322
4323   switch (attr->form)
4324     {
4325     case DW_FORM_addr:
4326       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != (unsigned int) address_size)
4327         complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4328                   address_size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4329       SYMBOL_VALUE_BYTES (sym) = (char *)
4330         obstack_alloc (&objfile->symbol_obstack, address_size);
4331       store_address (SYMBOL_VALUE_BYTES (sym), address_size, DW_ADDR (attr));
4332       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4333       break;
4334     case DW_FORM_block1:
4335     case DW_FORM_block2:
4336     case DW_FORM_block4:
4337     case DW_FORM_block:
4338       blk = DW_BLOCK (attr);
4339       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
4340         complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4341                   blk->size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4342       SYMBOL_VALUE_BYTES (sym) = (char *)
4343         obstack_alloc (&objfile->symbol_obstack, blk->size);
4344       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
4345       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4346       break;
4347     case DW_FORM_data2:
4348     case DW_FORM_data4:
4349     case DW_FORM_data8:
4350     case DW_FORM_data1:
4351     case DW_FORM_sdata:
4352     case DW_FORM_udata:
4353       SYMBOL_VALUE (sym) = DW_UNSND (attr);
4354       SYMBOL_CLASS (sym) = LOC_CONST;
4355       break;
4356     default:
4357       complain (&dwarf2_unsupported_const_value_attr,
4358                 dwarf_form_name (attr->form));
4359       SYMBOL_VALUE (sym) = 0;
4360       SYMBOL_CLASS (sym) = LOC_CONST;
4361       break;
4362     }
4363 }
4364
4365 /* Return the type of the die in question using its DW_AT_type attribute.  */
4366
4367 static struct type *
4368 die_type (die, objfile)
4369      struct die_info *die;
4370      struct objfile *objfile;
4371 {
4372   struct type *type;
4373   struct attribute *type_attr;
4374   struct die_info *type_die;
4375   unsigned int ref;
4376
4377   type_attr = dwarf_attr (die, DW_AT_type);
4378   if (!type_attr)
4379     {
4380       /* A missing DW_AT_type represents a void type.  */
4381       return dwarf2_fundamental_type (objfile, FT_VOID);
4382     }
4383   else
4384     {
4385       ref = dwarf2_get_ref_die_offset (type_attr);
4386       type_die = follow_die_ref (ref);
4387       if (!type_die)
4388         {
4389           error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4390           return NULL;
4391         }
4392     }
4393   type = tag_type_to_type (type_die, objfile);
4394   if (!type)
4395     {
4396       dump_die (type_die);
4397       error ("Dwarf Error: Problem turning type die at offset into gdb type.");
4398     }
4399   return type;
4400 }
4401
4402 /* Return the containing type of the die in question using its
4403    DW_AT_containing_type attribute.  */
4404
4405 static struct type *
4406 die_containing_type (die, objfile)
4407      struct die_info *die;
4408      struct objfile *objfile;
4409 {
4410   struct type *type = NULL;
4411   struct attribute *type_attr;
4412   struct die_info *type_die = NULL;
4413   unsigned int ref;
4414
4415   type_attr = dwarf_attr (die, DW_AT_containing_type);
4416   if (type_attr)
4417     {
4418       ref = dwarf2_get_ref_die_offset (type_attr);
4419       type_die = follow_die_ref (ref);
4420       if (!type_die)
4421         {
4422           error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4423           return NULL;
4424         }
4425       type = tag_type_to_type (type_die, objfile);
4426     }
4427   if (!type)
4428     {
4429       if (type_die)
4430         dump_die (type_die);
4431       error ("Dwarf Error: Problem turning containing type into gdb type.");
4432     }
4433   return type;
4434 }
4435
4436 #if 0
4437 static struct type *
4438 type_at_offset (offset, objfile)
4439      unsigned int offset;
4440      struct objfile *objfile;
4441 {
4442   struct die_info *die;
4443   struct type *type;
4444
4445   die = follow_die_ref (offset);
4446   if (!die)
4447     {
4448       error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
4449       return NULL;
4450     }
4451   type = tag_type_to_type (die, objfile);
4452   return type;
4453 }
4454 #endif
4455
4456 static struct type *
4457 tag_type_to_type (die, objfile)
4458      struct die_info *die;
4459      struct objfile *objfile;
4460 {
4461   if (die->type)
4462     {
4463       return die->type;
4464     }
4465   else
4466     {
4467       read_type_die (die, objfile);
4468       if (!die->type)
4469         {
4470           dump_die (die);
4471           error ("Dwarf Error: Cannot find type of die.");
4472         }
4473       return die->type;
4474     }
4475 }
4476
4477 static void
4478 read_type_die (die, objfile)
4479      struct die_info *die;
4480      struct objfile *objfile;
4481 {
4482   switch (die->tag)
4483     {
4484     case DW_TAG_class_type:
4485     case DW_TAG_structure_type:
4486     case DW_TAG_union_type:
4487       read_structure_scope (die, objfile);
4488       break;
4489     case DW_TAG_enumeration_type:
4490       read_enumeration (die, objfile);
4491       break;
4492     case DW_TAG_subprogram:
4493     case DW_TAG_subroutine_type:
4494       read_subroutine_type (die, objfile);
4495       break;
4496     case DW_TAG_array_type:
4497       read_array_type (die, objfile);
4498       break;
4499     case DW_TAG_pointer_type:
4500       read_tag_pointer_type (die, objfile);
4501       break;
4502     case DW_TAG_ptr_to_member_type:
4503       read_tag_ptr_to_member_type (die, objfile);
4504       break;
4505     case DW_TAG_reference_type:
4506       read_tag_reference_type (die, objfile);
4507       break;
4508     case DW_TAG_const_type:
4509       read_tag_const_type (die, objfile);
4510       break;
4511     case DW_TAG_volatile_type:
4512       read_tag_volatile_type (die, objfile);
4513       break;
4514     case DW_TAG_string_type:
4515       read_tag_string_type (die, objfile);
4516       break;
4517     case DW_TAG_typedef:
4518       read_typedef (die, objfile);
4519       break;
4520     case DW_TAG_base_type:
4521       read_base_type (die, objfile);
4522       break;
4523     default:
4524       complain (&dwarf2_unexpected_tag, dwarf_tag_name (die->tag));
4525       break;
4526     }
4527 }
4528
4529 static struct type *
4530 dwarf_base_type (encoding, size, objfile)
4531      int encoding;
4532      int size;
4533      struct objfile *objfile;
4534 {
4535   /* FIXME - this should not produce a new (struct type *)
4536      every time.  It should cache base types.  */
4537   struct type *type;
4538   switch (encoding)
4539     {
4540     case DW_ATE_address:
4541       type = dwarf2_fundamental_type (objfile, FT_VOID);
4542       return type;
4543     case DW_ATE_boolean:
4544       type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
4545       return type;
4546     case DW_ATE_complex_float:
4547       if (size == 16)
4548         {
4549           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
4550         }
4551       else
4552         {
4553           type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
4554         }
4555       return type;
4556     case DW_ATE_float:
4557       if (size == 8)
4558         {
4559           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
4560         }
4561       else
4562         {
4563           type = dwarf2_fundamental_type (objfile, FT_FLOAT);
4564         }
4565       return type;
4566     case DW_ATE_signed:
4567       switch (size)
4568         {
4569         case 1:
4570           type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4571           break;
4572         case 2:
4573           type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
4574           break;
4575         default:
4576         case 4:
4577           type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4578           break;
4579         }
4580       return type;
4581     case DW_ATE_signed_char:
4582       type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4583       return type;
4584     case DW_ATE_unsigned:
4585       switch (size)
4586         {
4587         case 1:
4588           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4589           break;
4590         case 2:
4591           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
4592           break;
4593         default:
4594         case 4:
4595           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
4596           break;
4597         }
4598       return type;
4599     case DW_ATE_unsigned_char:
4600       type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4601       return type;
4602     default:
4603       type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4604       return type;
4605     }
4606 }
4607
4608 #if 0
4609 struct die_info *
4610 copy_die (old_die)
4611      struct die_info *old_die;
4612 {
4613   struct die_info *new_die;
4614   int i, num_attrs;
4615
4616   new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
4617   memset (new_die, 0, sizeof (struct die_info));
4618
4619   new_die->tag = old_die->tag;
4620   new_die->has_children = old_die->has_children;
4621   new_die->abbrev = old_die->abbrev;
4622   new_die->offset = old_die->offset;
4623   new_die->type = NULL;
4624
4625   num_attrs = old_die->num_attrs;
4626   new_die->num_attrs = num_attrs;
4627   new_die->attrs = (struct attribute *)
4628     xmalloc (num_attrs * sizeof (struct attribute));
4629
4630   for (i = 0; i < old_die->num_attrs; ++i)
4631     {
4632       new_die->attrs[i].name = old_die->attrs[i].name;
4633       new_die->attrs[i].form = old_die->attrs[i].form;
4634       new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
4635     }
4636
4637   new_die->next = NULL;
4638   return new_die;
4639 }
4640 #endif
4641
4642 /* Return sibling of die, NULL if no sibling.  */
4643
4644 struct die_info *
4645 sibling_die (die)
4646      struct die_info *die;
4647 {
4648   int nesting_level = 0;
4649
4650   if (!die->has_children)
4651     {
4652       if (die->next && (die->next->tag == 0))
4653         {
4654           return NULL;
4655         }
4656       else
4657         {
4658           return die->next;
4659         }
4660     }
4661   else
4662     {
4663       do
4664         {
4665           if (die->has_children)
4666             {
4667               nesting_level++;
4668             }
4669           if (die->tag == 0)
4670             {
4671               nesting_level--;
4672             }
4673           die = die->next;
4674         }
4675       while (nesting_level);
4676       if (die && (die->tag == 0))
4677         {
4678           return NULL;
4679         }
4680       else
4681         {
4682           return die;
4683         }
4684     }
4685 }
4686
4687 /* Get linkage name of a die, return NULL if not found.  */
4688
4689 static char *
4690 dwarf2_linkage_name (die)
4691      struct die_info *die;
4692 {
4693   struct attribute *attr;
4694
4695   attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
4696   if (attr && DW_STRING (attr))
4697     return DW_STRING (attr);
4698   attr = dwarf_attr (die, DW_AT_name);
4699   if (attr && DW_STRING (attr))
4700     return DW_STRING (attr);
4701   return NULL;
4702 }
4703
4704 /* Convert a DIE tag into its string name.  */
4705
4706 static char *
4707 dwarf_tag_name (tag)
4708      register unsigned tag;
4709 {
4710   switch (tag)
4711     {
4712     case DW_TAG_padding:
4713       return "DW_TAG_padding";
4714     case DW_TAG_array_type:
4715       return "DW_TAG_array_type";
4716     case DW_TAG_class_type:
4717       return "DW_TAG_class_type";
4718     case DW_TAG_entry_point:
4719       return "DW_TAG_entry_point";
4720     case DW_TAG_enumeration_type:
4721       return "DW_TAG_enumeration_type";
4722     case DW_TAG_formal_parameter:
4723       return "DW_TAG_formal_parameter";
4724     case DW_TAG_imported_declaration:
4725       return "DW_TAG_imported_declaration";
4726     case DW_TAG_label:
4727       return "DW_TAG_label";
4728     case DW_TAG_lexical_block:
4729       return "DW_TAG_lexical_block";
4730     case DW_TAG_member:
4731       return "DW_TAG_member";
4732     case DW_TAG_pointer_type:
4733       return "DW_TAG_pointer_type";
4734     case DW_TAG_reference_type:
4735       return "DW_TAG_reference_type";
4736     case DW_TAG_compile_unit:
4737       return "DW_TAG_compile_unit";
4738     case DW_TAG_string_type:
4739       return "DW_TAG_string_type";
4740     case DW_TAG_structure_type:
4741       return "DW_TAG_structure_type";
4742     case DW_TAG_subroutine_type:
4743       return "DW_TAG_subroutine_type";
4744     case DW_TAG_typedef:
4745       return "DW_TAG_typedef";
4746     case DW_TAG_union_type:
4747       return "DW_TAG_union_type";
4748     case DW_TAG_unspecified_parameters:
4749       return "DW_TAG_unspecified_parameters";
4750     case DW_TAG_variant:
4751       return "DW_TAG_variant";
4752     case DW_TAG_common_block:
4753       return "DW_TAG_common_block";
4754     case DW_TAG_common_inclusion:
4755       return "DW_TAG_common_inclusion";
4756     case DW_TAG_inheritance:
4757       return "DW_TAG_inheritance";
4758     case DW_TAG_inlined_subroutine:
4759       return "DW_TAG_inlined_subroutine";
4760     case DW_TAG_module:
4761       return "DW_TAG_module";
4762     case DW_TAG_ptr_to_member_type:
4763       return "DW_TAG_ptr_to_member_type";
4764     case DW_TAG_set_type:
4765       return "DW_TAG_set_type";
4766     case DW_TAG_subrange_type:
4767       return "DW_TAG_subrange_type";
4768     case DW_TAG_with_stmt:
4769       return "DW_TAG_with_stmt";
4770     case DW_TAG_access_declaration:
4771       return "DW_TAG_access_declaration";
4772     case DW_TAG_base_type:
4773       return "DW_TAG_base_type";
4774     case DW_TAG_catch_block:
4775       return "DW_TAG_catch_block";
4776     case DW_TAG_const_type:
4777       return "DW_TAG_const_type";
4778     case DW_TAG_constant:
4779       return "DW_TAG_constant";
4780     case DW_TAG_enumerator:
4781       return "DW_TAG_enumerator";
4782     case DW_TAG_file_type:
4783       return "DW_TAG_file_type";
4784     case DW_TAG_friend:
4785       return "DW_TAG_friend";
4786     case DW_TAG_namelist:
4787       return "DW_TAG_namelist";
4788     case DW_TAG_namelist_item:
4789       return "DW_TAG_namelist_item";
4790     case DW_TAG_packed_type:
4791       return "DW_TAG_packed_type";
4792     case DW_TAG_subprogram:
4793       return "DW_TAG_subprogram";
4794     case DW_TAG_template_type_param:
4795       return "DW_TAG_template_type_param";
4796     case DW_TAG_template_value_param:
4797       return "DW_TAG_template_value_param";
4798     case DW_TAG_thrown_type:
4799       return "DW_TAG_thrown_type";
4800     case DW_TAG_try_block:
4801       return "DW_TAG_try_block";
4802     case DW_TAG_variant_part:
4803       return "DW_TAG_variant_part";
4804     case DW_TAG_variable:
4805       return "DW_TAG_variable";
4806     case DW_TAG_volatile_type:
4807       return "DW_TAG_volatile_type";
4808     case DW_TAG_MIPS_loop:
4809       return "DW_TAG_MIPS_loop";
4810     case DW_TAG_format_label:
4811       return "DW_TAG_format_label";
4812     case DW_TAG_function_template:
4813       return "DW_TAG_function_template";
4814     case DW_TAG_class_template:
4815       return "DW_TAG_class_template";
4816     default:
4817       return "DW_TAG_<unknown>";
4818     }
4819 }
4820
4821 /* Convert a DWARF attribute code into its string name.  */
4822
4823 static char *
4824 dwarf_attr_name (attr)
4825      register unsigned attr;
4826 {
4827   switch (attr)
4828     {
4829     case DW_AT_sibling:
4830       return "DW_AT_sibling";
4831     case DW_AT_location:
4832       return "DW_AT_location";
4833     case DW_AT_name:
4834       return "DW_AT_name";
4835     case DW_AT_ordering:
4836       return "DW_AT_ordering";
4837     case DW_AT_subscr_data:
4838       return "DW_AT_subscr_data";
4839     case DW_AT_byte_size:
4840       return "DW_AT_byte_size";
4841     case DW_AT_bit_offset:
4842       return "DW_AT_bit_offset";
4843     case DW_AT_bit_size:
4844       return "DW_AT_bit_size";
4845     case DW_AT_element_list:
4846       return "DW_AT_element_list";
4847     case DW_AT_stmt_list:
4848       return "DW_AT_stmt_list";
4849     case DW_AT_low_pc:
4850       return "DW_AT_low_pc";
4851     case DW_AT_high_pc:
4852       return "DW_AT_high_pc";
4853     case DW_AT_language:
4854       return "DW_AT_language";
4855     case DW_AT_member:
4856       return "DW_AT_member";
4857     case DW_AT_discr:
4858       return "DW_AT_discr";
4859     case DW_AT_discr_value:
4860       return "DW_AT_discr_value";
4861     case DW_AT_visibility:
4862       return "DW_AT_visibility";
4863     case DW_AT_import:
4864       return "DW_AT_import";
4865     case DW_AT_string_length:
4866       return "DW_AT_string_length";
4867     case DW_AT_common_reference:
4868       return "DW_AT_common_reference";
4869     case DW_AT_comp_dir:
4870       return "DW_AT_comp_dir";
4871     case DW_AT_const_value:
4872       return "DW_AT_const_value";
4873     case DW_AT_containing_type:
4874       return "DW_AT_containing_type";
4875     case DW_AT_default_value:
4876       return "DW_AT_default_value";
4877     case DW_AT_inline:
4878       return "DW_AT_inline";
4879     case DW_AT_is_optional:
4880       return "DW_AT_is_optional";
4881     case DW_AT_lower_bound:
4882       return "DW_AT_lower_bound";
4883     case DW_AT_producer:
4884       return "DW_AT_producer";
4885     case DW_AT_prototyped:
4886       return "DW_AT_prototyped";
4887     case DW_AT_return_addr:
4888       return "DW_AT_return_addr";
4889     case DW_AT_start_scope:
4890       return "DW_AT_start_scope";
4891     case DW_AT_stride_size:
4892       return "DW_AT_stride_size";
4893     case DW_AT_upper_bound:
4894       return "DW_AT_upper_bound";
4895     case DW_AT_abstract_origin:
4896       return "DW_AT_abstract_origin";
4897     case DW_AT_accessibility:
4898       return "DW_AT_accessibility";
4899     case DW_AT_address_class:
4900       return "DW_AT_address_class";
4901     case DW_AT_artificial:
4902       return "DW_AT_artificial";
4903     case DW_AT_base_types:
4904       return "DW_AT_base_types";
4905     case DW_AT_calling_convention:
4906       return "DW_AT_calling_convention";
4907     case DW_AT_count:
4908       return "DW_AT_count";
4909     case DW_AT_data_member_location:
4910       return "DW_AT_data_member_location";
4911     case DW_AT_decl_column:
4912       return "DW_AT_decl_column";
4913     case DW_AT_decl_file:
4914       return "DW_AT_decl_file";
4915     case DW_AT_decl_line:
4916       return "DW_AT_decl_line";
4917     case DW_AT_declaration:
4918       return "DW_AT_declaration";
4919     case DW_AT_discr_list:
4920       return "DW_AT_discr_list";
4921     case DW_AT_encoding:
4922       return "DW_AT_encoding";
4923     case DW_AT_external:
4924       return "DW_AT_external";
4925     case DW_AT_frame_base:
4926       return "DW_AT_frame_base";
4927     case DW_AT_friend:
4928       return "DW_AT_friend";
4929     case DW_AT_identifier_case:
4930       return "DW_AT_identifier_case";
4931     case DW_AT_macro_info:
4932       return "DW_AT_macro_info";
4933     case DW_AT_namelist_items:
4934       return "DW_AT_namelist_items";
4935     case DW_AT_priority:
4936       return "DW_AT_priority";
4937     case DW_AT_segment:
4938       return "DW_AT_segment";
4939     case DW_AT_specification:
4940       return "DW_AT_specification";
4941     case DW_AT_static_link:
4942       return "DW_AT_static_link";
4943     case DW_AT_type:
4944       return "DW_AT_type";
4945     case DW_AT_use_location:
4946       return "DW_AT_use_location";
4947     case DW_AT_variable_parameter:
4948       return "DW_AT_variable_parameter";
4949     case DW_AT_virtuality:
4950       return "DW_AT_virtuality";
4951     case DW_AT_vtable_elem_location:
4952       return "DW_AT_vtable_elem_location";
4953
4954 #ifdef MIPS
4955     case DW_AT_MIPS_fde:
4956       return "DW_AT_MIPS_fde";
4957     case DW_AT_MIPS_loop_begin:
4958       return "DW_AT_MIPS_loop_begin";
4959     case DW_AT_MIPS_tail_loop_begin:
4960       return "DW_AT_MIPS_tail_loop_begin";
4961     case DW_AT_MIPS_epilog_begin:
4962       return "DW_AT_MIPS_epilog_begin";
4963     case DW_AT_MIPS_loop_unroll_factor:
4964       return "DW_AT_MIPS_loop_unroll_factor";
4965     case DW_AT_MIPS_software_pipeline_depth:
4966       return "DW_AT_MIPS_software_pipeline_depth";
4967     case DW_AT_MIPS_linkage_name:
4968       return "DW_AT_MIPS_linkage_name";
4969 #endif
4970
4971     case DW_AT_sf_names:
4972       return "DW_AT_sf_names";
4973     case DW_AT_src_info:
4974       return "DW_AT_src_info";
4975     case DW_AT_mac_info:
4976       return "DW_AT_mac_info";
4977     case DW_AT_src_coords:
4978       return "DW_AT_src_coords";
4979     case DW_AT_body_begin:
4980       return "DW_AT_body_begin";
4981     case DW_AT_body_end:
4982       return "DW_AT_body_end";
4983     default:
4984       return "DW_AT_<unknown>";
4985     }
4986 }
4987
4988 /* Convert a DWARF value form code into its string name.  */
4989
4990 static char *
4991 dwarf_form_name (form)
4992      register unsigned form;
4993 {
4994   switch (form)
4995     {
4996     case DW_FORM_addr:
4997       return "DW_FORM_addr";
4998     case DW_FORM_block2:
4999       return "DW_FORM_block2";
5000     case DW_FORM_block4:
5001       return "DW_FORM_block4";
5002     case DW_FORM_data2:
5003       return "DW_FORM_data2";
5004     case DW_FORM_data4:
5005       return "DW_FORM_data4";
5006     case DW_FORM_data8:
5007       return "DW_FORM_data8";
5008     case DW_FORM_string:
5009       return "DW_FORM_string";
5010     case DW_FORM_block:
5011       return "DW_FORM_block";
5012     case DW_FORM_block1:
5013       return "DW_FORM_block1";
5014     case DW_FORM_data1:
5015       return "DW_FORM_data1";
5016     case DW_FORM_flag:
5017       return "DW_FORM_flag";
5018     case DW_FORM_sdata:
5019       return "DW_FORM_sdata";
5020     case DW_FORM_strp:
5021       return "DW_FORM_strp";
5022     case DW_FORM_udata:
5023       return "DW_FORM_udata";
5024     case DW_FORM_ref_addr:
5025       return "DW_FORM_ref_addr";
5026     case DW_FORM_ref1:
5027       return "DW_FORM_ref1";
5028     case DW_FORM_ref2:
5029       return "DW_FORM_ref2";
5030     case DW_FORM_ref4:
5031       return "DW_FORM_ref4";
5032     case DW_FORM_ref8:
5033       return "DW_FORM_ref8";
5034     case DW_FORM_ref_udata:
5035       return "DW_FORM_ref_udata";
5036     case DW_FORM_indirect:
5037       return "DW_FORM_indirect";
5038     default:
5039       return "DW_FORM_<unknown>";
5040     }
5041 }
5042
5043 /* Convert a DWARF stack opcode into its string name.  */
5044
5045 static char *
5046 dwarf_stack_op_name (op)
5047      register unsigned op;
5048 {
5049   switch (op)
5050     {
5051     case DW_OP_addr:
5052       return "DW_OP_addr";
5053     case DW_OP_deref:
5054       return "DW_OP_deref";
5055     case DW_OP_const1u:
5056       return "DW_OP_const1u";
5057     case DW_OP_const1s:
5058       return "DW_OP_const1s";
5059     case DW_OP_const2u:
5060       return "DW_OP_const2u";
5061     case DW_OP_const2s:
5062       return "DW_OP_const2s";
5063     case DW_OP_const4u:
5064       return "DW_OP_const4u";
5065     case DW_OP_const4s:
5066       return "DW_OP_const4s";
5067     case DW_OP_const8u:
5068       return "DW_OP_const8u";
5069     case DW_OP_const8s:
5070       return "DW_OP_const8s";
5071     case DW_OP_constu:
5072       return "DW_OP_constu";
5073     case DW_OP_consts:
5074       return "DW_OP_consts";
5075     case DW_OP_dup:
5076       return "DW_OP_dup";
5077     case DW_OP_drop:
5078       return "DW_OP_drop";
5079     case DW_OP_over:
5080       return "DW_OP_over";
5081     case DW_OP_pick:
5082       return "DW_OP_pick";
5083     case DW_OP_swap:
5084       return "DW_OP_swap";
5085     case DW_OP_rot:
5086       return "DW_OP_rot";
5087     case DW_OP_xderef:
5088       return "DW_OP_xderef";
5089     case DW_OP_abs:
5090       return "DW_OP_abs";
5091     case DW_OP_and:
5092       return "DW_OP_and";
5093     case DW_OP_div:
5094       return "DW_OP_div";
5095     case DW_OP_minus:
5096       return "DW_OP_minus";
5097     case DW_OP_mod:
5098       return "DW_OP_mod";
5099     case DW_OP_mul:
5100       return "DW_OP_mul";
5101     case DW_OP_neg:
5102       return "DW_OP_neg";
5103     case DW_OP_not:
5104       return "DW_OP_not";
5105     case DW_OP_or:
5106       return "DW_OP_or";
5107     case DW_OP_plus:
5108       return "DW_OP_plus";
5109     case DW_OP_plus_uconst:
5110       return "DW_OP_plus_uconst";
5111     case DW_OP_shl:
5112       return "DW_OP_shl";
5113     case DW_OP_shr:
5114       return "DW_OP_shr";
5115     case DW_OP_shra:
5116       return "DW_OP_shra";
5117     case DW_OP_xor:
5118       return "DW_OP_xor";
5119     case DW_OP_bra:
5120       return "DW_OP_bra";
5121     case DW_OP_eq:
5122       return "DW_OP_eq";
5123     case DW_OP_ge:
5124       return "DW_OP_ge";
5125     case DW_OP_gt:
5126       return "DW_OP_gt";
5127     case DW_OP_le:
5128       return "DW_OP_le";
5129     case DW_OP_lt:
5130       return "DW_OP_lt";
5131     case DW_OP_ne:
5132       return "DW_OP_ne";
5133     case DW_OP_skip:
5134       return "DW_OP_skip";
5135     case DW_OP_lit0:
5136       return "DW_OP_lit0";
5137     case DW_OP_lit1:
5138       return "DW_OP_lit1";
5139     case DW_OP_lit2:
5140       return "DW_OP_lit2";
5141     case DW_OP_lit3:
5142       return "DW_OP_lit3";
5143     case DW_OP_lit4:
5144       return "DW_OP_lit4";
5145     case DW_OP_lit5:
5146       return "DW_OP_lit5";
5147     case DW_OP_lit6:
5148       return "DW_OP_lit6";
5149     case DW_OP_lit7:
5150       return "DW_OP_lit7";
5151     case DW_OP_lit8:
5152       return "DW_OP_lit8";
5153     case DW_OP_lit9:
5154       return "DW_OP_lit9";
5155     case DW_OP_lit10:
5156       return "DW_OP_lit10";
5157     case DW_OP_lit11:
5158       return "DW_OP_lit11";
5159     case DW_OP_lit12:
5160       return "DW_OP_lit12";
5161     case DW_OP_lit13:
5162       return "DW_OP_lit13";
5163     case DW_OP_lit14:
5164       return "DW_OP_lit14";
5165     case DW_OP_lit15:
5166       return "DW_OP_lit15";
5167     case DW_OP_lit16:
5168       return "DW_OP_lit16";
5169     case DW_OP_lit17:
5170       return "DW_OP_lit17";
5171     case DW_OP_lit18:
5172       return "DW_OP_lit18";
5173     case DW_OP_lit19:
5174       return "DW_OP_lit19";
5175     case DW_OP_lit20:
5176       return "DW_OP_lit20";
5177     case DW_OP_lit21:
5178       return "DW_OP_lit21";
5179     case DW_OP_lit22:
5180       return "DW_OP_lit22";
5181     case DW_OP_lit23:
5182       return "DW_OP_lit23";
5183     case DW_OP_lit24:
5184       return "DW_OP_lit24";
5185     case DW_OP_lit25:
5186       return "DW_OP_lit25";
5187     case DW_OP_lit26:
5188       return "DW_OP_lit26";
5189     case DW_OP_lit27:
5190       return "DW_OP_lit27";
5191     case DW_OP_lit28:
5192       return "DW_OP_lit28";
5193     case DW_OP_lit29:
5194       return "DW_OP_lit29";
5195     case DW_OP_lit30:
5196       return "DW_OP_lit30";
5197     case DW_OP_lit31:
5198       return "DW_OP_lit31";
5199     case DW_OP_reg0:
5200       return "DW_OP_reg0";
5201     case DW_OP_reg1:
5202       return "DW_OP_reg1";
5203     case DW_OP_reg2:
5204       return "DW_OP_reg2";
5205     case DW_OP_reg3:
5206       return "DW_OP_reg3";
5207     case DW_OP_reg4:
5208       return "DW_OP_reg4";
5209     case DW_OP_reg5:
5210       return "DW_OP_reg5";
5211     case DW_OP_reg6:
5212       return "DW_OP_reg6";
5213     case DW_OP_reg7:
5214       return "DW_OP_reg7";
5215     case DW_OP_reg8:
5216       return "DW_OP_reg8";
5217     case DW_OP_reg9:
5218       return "DW_OP_reg9";
5219     case DW_OP_reg10:
5220       return "DW_OP_reg10";
5221     case DW_OP_reg11:
5222       return "DW_OP_reg11";
5223     case DW_OP_reg12:
5224       return "DW_OP_reg12";
5225     case DW_OP_reg13:
5226       return "DW_OP_reg13";
5227     case DW_OP_reg14:
5228       return "DW_OP_reg14";
5229     case DW_OP_reg15:
5230       return "DW_OP_reg15";
5231     case DW_OP_reg16:
5232       return "DW_OP_reg16";
5233     case DW_OP_reg17:
5234       return "DW_OP_reg17";
5235     case DW_OP_reg18:
5236       return "DW_OP_reg18";
5237     case DW_OP_reg19:
5238       return "DW_OP_reg19";
5239     case DW_OP_reg20:
5240       return "DW_OP_reg20";
5241     case DW_OP_reg21:
5242       return "DW_OP_reg21";
5243     case DW_OP_reg22:
5244       return "DW_OP_reg22";
5245     case DW_OP_reg23:
5246       return "DW_OP_reg23";
5247     case DW_OP_reg24:
5248       return "DW_OP_reg24";
5249     case DW_OP_reg25:
5250       return "DW_OP_reg25";
5251     case DW_OP_reg26:
5252       return "DW_OP_reg26";
5253     case DW_OP_reg27:
5254       return "DW_OP_reg27";
5255     case DW_OP_reg28:
5256       return "DW_OP_reg28";
5257     case DW_OP_reg29:
5258       return "DW_OP_reg29";
5259     case DW_OP_reg30:
5260       return "DW_OP_reg30";
5261     case DW_OP_reg31:
5262       return "DW_OP_reg31";
5263     case DW_OP_breg0:
5264       return "DW_OP_breg0";
5265     case DW_OP_breg1:
5266       return "DW_OP_breg1";
5267     case DW_OP_breg2:
5268       return "DW_OP_breg2";
5269     case DW_OP_breg3:
5270       return "DW_OP_breg3";
5271     case DW_OP_breg4:
5272       return "DW_OP_breg4";
5273     case DW_OP_breg5:
5274       return "DW_OP_breg5";
5275     case DW_OP_breg6:
5276       return "DW_OP_breg6";
5277     case DW_OP_breg7:
5278       return "DW_OP_breg7";
5279     case DW_OP_breg8:
5280       return "DW_OP_breg8";
5281     case DW_OP_breg9:
5282       return "DW_OP_breg9";
5283     case DW_OP_breg10:
5284       return "DW_OP_breg10";
5285     case DW_OP_breg11:
5286       return "DW_OP_breg11";
5287     case DW_OP_breg12:
5288       return "DW_OP_breg12";
5289     case DW_OP_breg13:
5290       return "DW_OP_breg13";
5291     case DW_OP_breg14:
5292       return "DW_OP_breg14";
5293     case DW_OP_breg15:
5294       return "DW_OP_breg15";
5295     case DW_OP_breg16:
5296       return "DW_OP_breg16";
5297     case DW_OP_breg17:
5298       return "DW_OP_breg17";
5299     case DW_OP_breg18:
5300       return "DW_OP_breg18";
5301     case DW_OP_breg19:
5302       return "DW_OP_breg19";
5303     case DW_OP_breg20:
5304       return "DW_OP_breg20";
5305     case DW_OP_breg21:
5306       return "DW_OP_breg21";
5307     case DW_OP_breg22:
5308       return "DW_OP_breg22";
5309     case DW_OP_breg23:
5310       return "DW_OP_breg23";
5311     case DW_OP_breg24:
5312       return "DW_OP_breg24";
5313     case DW_OP_breg25:
5314       return "DW_OP_breg25";
5315     case DW_OP_breg26:
5316       return "DW_OP_breg26";
5317     case DW_OP_breg27:
5318       return "DW_OP_breg27";
5319     case DW_OP_breg28:
5320       return "DW_OP_breg28";
5321     case DW_OP_breg29:
5322       return "DW_OP_breg29";
5323     case DW_OP_breg30:
5324       return "DW_OP_breg30";
5325     case DW_OP_breg31:
5326       return "DW_OP_breg31";
5327     case DW_OP_regx:
5328       return "DW_OP_regx";
5329     case DW_OP_fbreg:
5330       return "DW_OP_fbreg";
5331     case DW_OP_bregx:
5332       return "DW_OP_bregx";
5333     case DW_OP_piece:
5334       return "DW_OP_piece";
5335     case DW_OP_deref_size:
5336       return "DW_OP_deref_size";
5337     case DW_OP_xderef_size:
5338       return "DW_OP_xderef_size";
5339     case DW_OP_nop:
5340       return "DW_OP_nop";
5341     default:
5342       return "OP_<unknown>";
5343     }
5344 }
5345
5346 static char *
5347 dwarf_bool_name (mybool)
5348      unsigned mybool;
5349 {
5350   if (mybool)
5351     return "TRUE";
5352   else
5353     return "FALSE";
5354 }
5355
5356 /* Convert a DWARF type code into its string name.  */
5357
5358 static char *
5359 dwarf_type_encoding_name (enc)
5360      register unsigned enc;
5361 {
5362   switch (enc)
5363     {
5364     case DW_ATE_address:
5365       return "DW_ATE_address";
5366     case DW_ATE_boolean:
5367       return "DW_ATE_boolean";
5368     case DW_ATE_complex_float:
5369       return "DW_ATE_complex_float";
5370     case DW_ATE_float:
5371       return "DW_ATE_float";
5372     case DW_ATE_signed:
5373       return "DW_ATE_signed";
5374     case DW_ATE_signed_char:
5375       return "DW_ATE_signed_char";
5376     case DW_ATE_unsigned:
5377       return "DW_ATE_unsigned";
5378     case DW_ATE_unsigned_char:
5379       return "DW_ATE_unsigned_char";
5380     default:
5381       return "DW_ATE_<unknown>";
5382     }
5383 }
5384
5385 /* Convert a DWARF call frame info operation to its string name. */
5386
5387 #if 0
5388 static char *
5389 dwarf_cfi_name (cfi_opc)
5390      register unsigned cfi_opc;
5391 {
5392   switch (cfi_opc)
5393     {
5394     case DW_CFA_advance_loc:
5395       return "DW_CFA_advance_loc";
5396     case DW_CFA_offset:
5397       return "DW_CFA_offset";
5398     case DW_CFA_restore:
5399       return "DW_CFA_restore";
5400     case DW_CFA_nop:
5401       return "DW_CFA_nop";
5402     case DW_CFA_set_loc:
5403       return "DW_CFA_set_loc";
5404     case DW_CFA_advance_loc1:
5405       return "DW_CFA_advance_loc1";
5406     case DW_CFA_advance_loc2:
5407       return "DW_CFA_advance_loc2";
5408     case DW_CFA_advance_loc4:
5409       return "DW_CFA_advance_loc4";
5410     case DW_CFA_offset_extended:
5411       return "DW_CFA_offset_extended";
5412     case DW_CFA_restore_extended:
5413       return "DW_CFA_restore_extended";
5414     case DW_CFA_undefined:
5415       return "DW_CFA_undefined";
5416     case DW_CFA_same_value:
5417       return "DW_CFA_same_value";
5418     case DW_CFA_register:
5419       return "DW_CFA_register";
5420     case DW_CFA_remember_state:
5421       return "DW_CFA_remember_state";
5422     case DW_CFA_restore_state:
5423       return "DW_CFA_restore_state";
5424     case DW_CFA_def_cfa:
5425       return "DW_CFA_def_cfa";
5426     case DW_CFA_def_cfa_register:
5427       return "DW_CFA_def_cfa_register";
5428     case DW_CFA_def_cfa_offset:
5429       return "DW_CFA_def_cfa_offset";
5430       /* SGI/MIPS specific */
5431     case DW_CFA_MIPS_advance_loc8:
5432       return "DW_CFA_MIPS_advance_loc8";
5433     default:
5434       return "DW_CFA_<unknown>";
5435     }
5436 }
5437 #endif
5438
5439 void
5440 dump_die (die)
5441      struct die_info *die;
5442 {
5443   unsigned int i;
5444
5445   fprintf (stderr, "Die: %s (abbrev = %d, offset = %d)\n",
5446            dwarf_tag_name (die->tag), die->abbrev, die->offset);
5447   fprintf (stderr, "\thas children: %s\n",
5448            dwarf_bool_name (die->has_children));
5449
5450   fprintf (stderr, "\tattributes:\n");
5451   for (i = 0; i < die->num_attrs; ++i)
5452     {
5453       fprintf (stderr, "\t\t%s (%s) ",
5454                dwarf_attr_name (die->attrs[i].name),
5455                dwarf_form_name (die->attrs[i].form));
5456       switch (die->attrs[i].form)
5457         {
5458         case DW_FORM_ref_addr:
5459         case DW_FORM_addr:
5460           fprintf (stderr, "address: ");
5461           print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
5462           break;
5463         case DW_FORM_block2:
5464         case DW_FORM_block4:
5465         case DW_FORM_block:
5466         case DW_FORM_block1:
5467           fprintf (stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
5468           break;
5469         case DW_FORM_data1:
5470         case DW_FORM_data2:
5471         case DW_FORM_data4:
5472         case DW_FORM_ref1:
5473         case DW_FORM_ref2:
5474         case DW_FORM_ref4:
5475         case DW_FORM_udata:
5476         case DW_FORM_sdata:
5477           fprintf (stderr, "constant: %d", DW_UNSND (&die->attrs[i]));
5478           break;
5479         case DW_FORM_string:
5480           fprintf (stderr, "string: \"%s\"",
5481                    DW_STRING (&die->attrs[i])
5482                      ? DW_STRING (&die->attrs[i]) : "");
5483           break;
5484         case DW_FORM_flag:
5485           if (DW_UNSND (&die->attrs[i]))
5486             fprintf (stderr, "flag: TRUE");
5487           else
5488             fprintf (stderr, "flag: FALSE");
5489           break;
5490         case DW_FORM_strp:      /* we do not support separate string
5491                                    section yet */
5492         case DW_FORM_indirect:  /* we do not handle indirect yet */
5493         case DW_FORM_data8:     /* we do not have 64 bit quantities */
5494         default:
5495           fprintf (stderr, "unsupported attribute form: %d.",
5496                            die->attrs[i].form);
5497         }
5498       fprintf (stderr, "\n");
5499     }
5500 }
5501
5502 void
5503 dump_die_list (die)
5504      struct die_info *die;
5505 {
5506   while (die)
5507     {
5508       dump_die (die);
5509       die = die->next;
5510     }
5511 }
5512
5513 void
5514 store_in_ref_table (offset, die)
5515      unsigned int offset;
5516      struct die_info *die;
5517 {
5518   int h;
5519   struct die_info *old;
5520
5521   h = (offset % REF_HASH_SIZE);
5522   old = die_ref_table[h];
5523   die->next_ref = old;
5524   die_ref_table[h] = die;
5525 }
5526
5527
5528 static void
5529 dwarf2_empty_die_ref_table ()
5530 {
5531   memset (die_ref_table, 0, sizeof (die_ref_table));
5532 }
5533
5534 static unsigned int
5535 dwarf2_get_ref_die_offset (attr)
5536      struct attribute *attr;
5537 {
5538   unsigned int result = 0;
5539
5540   switch (attr->form)
5541     {
5542     case DW_FORM_ref_addr:
5543       result = DW_ADDR (attr);
5544       break;
5545     case DW_FORM_ref1:
5546     case DW_FORM_ref2:
5547     case DW_FORM_ref4:
5548     case DW_FORM_ref_udata:
5549       result = cu_header_offset + DW_UNSND (attr);
5550       break;
5551     default:
5552       complain (&dwarf2_unsupported_die_ref_attr, dwarf_form_name (attr->form));
5553     }
5554   return result;
5555 }
5556
5557 struct die_info *
5558 follow_die_ref (offset)
5559      unsigned int offset;
5560 {
5561   struct die_info *die;
5562   int h;
5563
5564   h = (offset % REF_HASH_SIZE);
5565   die = die_ref_table[h];
5566   while (die)
5567     {
5568       if (die->offset == offset)
5569         {
5570           return die;
5571         }
5572       die = die->next_ref;
5573     }
5574   return NULL;
5575 }
5576
5577 static struct type *
5578 dwarf2_fundamental_type (objfile, typeid)
5579      struct objfile *objfile;
5580      int typeid;
5581 {
5582   if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
5583     {
5584       error ("Dwarf Error: internal error - invalid fundamental type id %d.",
5585              typeid);
5586     }
5587
5588   /* Look for this particular type in the fundamental type vector.  If
5589      one is not found, create and install one appropriate for the
5590      current language and the current target machine. */
5591
5592   if (ftypes[typeid] == NULL)
5593     {
5594       ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
5595     }
5596
5597   return (ftypes[typeid]);
5598 }
5599
5600 /* Decode simple location descriptions.
5601    Given a pointer to a dwarf block that defines a location, compute
5602    the location and return the value.
5603
5604    FIXME: This is a kludge until we figure out a better
5605    way to handle the location descriptions.
5606    Gdb's design does not mesh well with the DWARF2 notion of a location
5607    computing interpreter, which is a shame because the flexibility goes unused.
5608    FIXME: Implement more operations as necessary.
5609
5610    A location description containing no operations indicates that the
5611    object is optimized out. The global optimized_out flag is set for
5612    those, the return value is meaningless.
5613
5614    When the result is a register number, the global isreg flag is set,
5615    otherwise it is cleared.
5616
5617    When the result is a base register offset, the global offreg flag is set
5618    and the register number is returned in basereg, otherwise it is cleared.
5619
5620    When the DW_OP_fbreg operation is encountered without a corresponding
5621    DW_AT_frame_base attribute, the global islocal flag is set.
5622    Hopefully the machine dependent code knows how to set up a virtual
5623    frame pointer for the local references.
5624  
5625    Note that stack[0] is unused except as a default error return.
5626    Note that stack overflow is not yet handled.  */
5627
5628 static CORE_ADDR
5629 decode_locdesc (blk, objfile)
5630      struct dwarf_block *blk;
5631      struct objfile *objfile;
5632 {
5633   int i;
5634   int size = blk->size;
5635   char *data = blk->data;
5636   CORE_ADDR stack[64];
5637   int stacki;
5638   unsigned int bytes_read, unsnd;
5639   unsigned char op;
5640
5641   i = 0;
5642   stacki = 0;
5643   stack[stacki] = 0;
5644   isreg = 0;
5645   offreg = 0;
5646   islocal = 0;
5647   optimized_out = 1;
5648
5649   while (i < size)
5650     {
5651       optimized_out = 0;
5652       op = data[i++];
5653       switch (op)
5654         {
5655         case DW_OP_reg0:
5656         case DW_OP_reg1:
5657         case DW_OP_reg2:
5658         case DW_OP_reg3:
5659         case DW_OP_reg4:
5660         case DW_OP_reg5:
5661         case DW_OP_reg6:
5662         case DW_OP_reg7:
5663         case DW_OP_reg8:
5664         case DW_OP_reg9:
5665         case DW_OP_reg10:
5666         case DW_OP_reg11:
5667         case DW_OP_reg12:
5668         case DW_OP_reg13:
5669         case DW_OP_reg14:
5670         case DW_OP_reg15:
5671         case DW_OP_reg16:
5672         case DW_OP_reg17:
5673         case DW_OP_reg18:
5674         case DW_OP_reg19:
5675         case DW_OP_reg20:
5676         case DW_OP_reg21:
5677         case DW_OP_reg22:
5678         case DW_OP_reg23:
5679         case DW_OP_reg24:
5680         case DW_OP_reg25:
5681         case DW_OP_reg26:
5682         case DW_OP_reg27:
5683         case DW_OP_reg28:
5684         case DW_OP_reg29:
5685         case DW_OP_reg30:
5686         case DW_OP_reg31:
5687           isreg = 1;
5688           stack[++stacki] = op - DW_OP_reg0;
5689           break;
5690
5691         case DW_OP_regx:
5692           isreg = 1;
5693           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5694           i += bytes_read;
5695 #if defined(HARRIS_TARGET) && defined(_M88K)
5696           /* The Harris 88110 gdb ports have long kept their special reg
5697              numbers between their gp-regs and their x-regs.  This is
5698              not how our dwarf is generated.  Punt. */
5699           unsnd += 6;
5700 #endif
5701           stack[++stacki] = unsnd;
5702           break;
5703
5704         case DW_OP_breg0:
5705         case DW_OP_breg1:
5706         case DW_OP_breg2:
5707         case DW_OP_breg3:
5708         case DW_OP_breg4:
5709         case DW_OP_breg5:
5710         case DW_OP_breg6:
5711         case DW_OP_breg7:
5712         case DW_OP_breg8:
5713         case DW_OP_breg9:
5714         case DW_OP_breg10:
5715         case DW_OP_breg11:
5716         case DW_OP_breg12:
5717         case DW_OP_breg13:
5718         case DW_OP_breg14:
5719         case DW_OP_breg15:
5720         case DW_OP_breg16:
5721         case DW_OP_breg17:
5722         case DW_OP_breg18:
5723         case DW_OP_breg19:
5724         case DW_OP_breg20:
5725         case DW_OP_breg21:
5726         case DW_OP_breg22:
5727         case DW_OP_breg23:
5728         case DW_OP_breg24:
5729         case DW_OP_breg25:
5730         case DW_OP_breg26:
5731         case DW_OP_breg27:
5732         case DW_OP_breg28:
5733         case DW_OP_breg29:
5734         case DW_OP_breg30:
5735         case DW_OP_breg31:
5736           offreg = 1;
5737           basereg = op - DW_OP_breg0;
5738           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5739           i += bytes_read;
5740           break;
5741
5742         case DW_OP_fbreg:
5743           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5744           i += bytes_read;
5745           if (frame_base_reg >= 0)
5746             {
5747               offreg = 1;
5748               basereg = frame_base_reg;
5749               stack[stacki] += frame_base_offset;
5750             }
5751           else
5752             {
5753               complain (&dwarf2_missing_at_frame_base);
5754               islocal = 1;
5755             }
5756           break;
5757
5758         case DW_OP_addr:
5759           stack[++stacki] = read_address (objfile->obfd, &data[i]);
5760           i += address_size;
5761           break;
5762
5763         case DW_OP_const1u:
5764           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
5765           i += 1;
5766           break;
5767
5768         case DW_OP_const1s:
5769           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
5770           i += 1;
5771           break;
5772
5773         case DW_OP_const2u:
5774           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
5775           i += 2;
5776           break;
5777
5778         case DW_OP_const2s:
5779           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
5780           i += 2;
5781           break;
5782
5783         case DW_OP_const4u:
5784           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
5785           i += 4;
5786           break;
5787
5788         case DW_OP_const4s:
5789           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
5790           i += 4;
5791           break;
5792
5793         case DW_OP_constu:
5794           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
5795                                                          &bytes_read);
5796           i += bytes_read;
5797           break;
5798
5799         case DW_OP_consts:
5800           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5801           i += bytes_read;
5802           break;
5803
5804         case DW_OP_plus:
5805           stack[stacki - 1] += stack[stacki];
5806           stacki--;
5807           break;
5808
5809         case DW_OP_plus_uconst:
5810           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5811           i += bytes_read;
5812           break;
5813
5814         case DW_OP_minus:
5815           stack[stacki - 1] = stack[stacki] - stack[stacki - 1];
5816           stacki--;
5817           break;
5818
5819         default:
5820           complain (&dwarf2_unsupported_stack_op, dwarf_stack_op_name(op));
5821           return (stack[stacki]);
5822         }
5823     }
5824   return (stack[stacki]);
5825 }
5826
5827 /* memory allocation interface */
5828
5829 /* ARGSUSED */
5830 static void
5831 dwarf2_free_tmp_obstack (ignore)
5832      PTR ignore;
5833 {
5834   obstack_free (&dwarf2_tmp_obstack, NULL);
5835 }
5836
5837 static struct dwarf_block *
5838 dwarf_alloc_block ()
5839 {
5840   struct dwarf_block *blk;
5841
5842   blk = (struct dwarf_block *)
5843     obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
5844   return (blk);
5845 }
5846
5847 static struct abbrev_info *
5848 dwarf_alloc_abbrev ()
5849 {
5850   struct abbrev_info *abbrev;
5851
5852   abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
5853   memset (abbrev, 0, sizeof (struct abbrev_info));
5854   return (abbrev);
5855 }
5856
5857 static struct die_info *
5858 dwarf_alloc_die ()
5859 {
5860   struct die_info *die;
5861
5862   die = (struct die_info *) xmalloc (sizeof (struct die_info));
5863   memset (die, 0, sizeof (struct die_info));
5864   return (die);
5865 }