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