2000-03-21 J.T. Conklin <jtc@redback.com>
[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 2:
3491       retval = bfd_get_16 (abfd, (bfd_byte *) buf);
3492       break;
3493     case 4:
3494       retval = bfd_get_32 (abfd, (bfd_byte *) buf);
3495       break;
3496     case 8:
3497       retval = bfd_get_64 (abfd, (bfd_byte *) buf);
3498       break;
3499     default:
3500       /* *THE* alternative is 8, right? */
3501       abort ();
3502     }
3503   /* If the address being read is larger than the address that is
3504      applicable for the object file format then mask it down to the
3505      correct size.  Take care to avoid unnecessary shift or shift
3506      overflow */
3507   if (address_size > address_significant_size
3508       && address_significant_size < sizeof (CORE_ADDR))
3509     {
3510       CORE_ADDR mask = ((CORE_ADDR) 0) - 1;
3511       retval &= ~(mask << (address_significant_size * 8));
3512     }
3513   return retval;
3514 }
3515
3516 static char *
3517 read_n_bytes (abfd, buf, size)
3518      bfd *abfd;
3519      char *buf;
3520      unsigned int size;
3521 {
3522   /* If the size of a host char is 8 bits, we can return a pointer
3523      to the buffer, otherwise we have to copy the data to a buffer
3524      allocated on the temporary obstack.  */
3525 #if HOST_CHAR_BIT == 8
3526   return buf;
3527 #else
3528   char *ret;
3529   unsigned int i;
3530
3531   ret = obstack_alloc (&dwarf2_tmp_obstack, size);
3532   for (i = 0; i < size; ++i)
3533     {
3534       ret[i] = bfd_get_8 (abfd, (bfd_byte *) buf);
3535       buf++;
3536     }
3537   return ret;
3538 #endif
3539 }
3540
3541 static char *
3542 read_string (abfd, buf, bytes_read_ptr)
3543      bfd *abfd;
3544      char *buf;
3545      unsigned int *bytes_read_ptr;
3546 {
3547   /* If the size of a host char is 8 bits, we can return a pointer
3548      to the string, otherwise we have to copy the string to a buffer
3549      allocated on the temporary obstack.  */
3550 #if HOST_CHAR_BIT == 8
3551   if (*buf == '\0')
3552     {
3553       *bytes_read_ptr = 1;
3554       return NULL;
3555     }
3556   *bytes_read_ptr = strlen (buf) + 1;
3557   return buf;
3558 #else
3559   int byte;
3560   unsigned int i = 0;
3561
3562   while ((byte = bfd_get_8 (abfd, (bfd_byte *) buf)) != 0)
3563     {
3564       obstack_1grow (&dwarf2_tmp_obstack, byte);
3565       i++;
3566       buf++;
3567     }
3568   if (i == 0)
3569     {
3570       *bytes_read_ptr = 1;
3571       return NULL;
3572     }
3573   obstack_1grow (&dwarf2_tmp_obstack, '\0');
3574   *bytes_read_ptr = i + 1;
3575   return obstack_finish (&dwarf2_tmp_obstack);
3576 #endif
3577 }
3578
3579 static unsigned int
3580 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
3581      bfd *abfd;
3582      char *buf;
3583      unsigned int *bytes_read_ptr;
3584 {
3585   unsigned int result, num_read;
3586   int i, shift;
3587   unsigned char byte;
3588
3589   result = 0;
3590   shift = 0;
3591   num_read = 0;
3592   i = 0;
3593   while (1)
3594     {
3595       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3596       buf++;
3597       num_read++;
3598       result |= ((byte & 127) << shift);
3599       if ((byte & 128) == 0)
3600         {
3601           break;
3602         }
3603       shift += 7;
3604     }
3605   *bytes_read_ptr = num_read;
3606   return result;
3607 }
3608
3609 static int
3610 read_signed_leb128 (abfd, buf, bytes_read_ptr)
3611      bfd *abfd;
3612      char *buf;
3613      unsigned int *bytes_read_ptr;
3614 {
3615   int result;
3616   int i, shift, size, num_read;
3617   unsigned char byte;
3618
3619   result = 0;
3620   shift = 0;
3621   size = 32;
3622   num_read = 0;
3623   i = 0;
3624   while (1)
3625     {
3626       byte = bfd_get_8 (abfd, (bfd_byte *) buf);
3627       buf++;
3628       num_read++;
3629       result |= ((byte & 127) << shift);
3630       shift += 7;
3631       if ((byte & 128) == 0)
3632         {
3633           break;
3634         }
3635     }
3636   if ((shift < size) && (byte & 0x40))
3637     {
3638       result |= -(1 << shift);
3639     }
3640   *bytes_read_ptr = num_read;
3641   return result;
3642 }
3643
3644 static void
3645 set_cu_language (lang)
3646      unsigned int lang;
3647 {
3648   switch (lang)
3649     {
3650     case DW_LANG_C89:
3651     case DW_LANG_C:
3652       cu_language = language_c;
3653       break;
3654     case DW_LANG_C_plus_plus:
3655       cu_language = language_cplus;
3656       break;
3657     case DW_LANG_Fortran77:
3658     case DW_LANG_Fortran90:
3659       cu_language = language_fortran;
3660       break;
3661     case DW_LANG_Mips_Assembler:
3662       cu_language = language_asm;
3663       break;
3664     case DW_LANG_Ada83:
3665     case DW_LANG_Cobol74:
3666     case DW_LANG_Cobol85:
3667     case DW_LANG_Pascal83:
3668     case DW_LANG_Modula2:
3669     default:
3670       cu_language = language_unknown;
3671       break;
3672     }
3673   cu_language_defn = language_def (cu_language);
3674 }
3675
3676 /* Return the named attribute or NULL if not there.  */
3677
3678 static struct attribute *
3679 dwarf_attr (die, name)
3680      struct die_info *die;
3681      unsigned int name;
3682 {
3683   unsigned int i;
3684   struct attribute *spec = NULL;
3685
3686   for (i = 0; i < die->num_attrs; ++i)
3687     {
3688       if (die->attrs[i].name == name)
3689         {
3690           return &die->attrs[i];
3691         }
3692       if (die->attrs[i].name == DW_AT_specification
3693           || die->attrs[i].name == DW_AT_abstract_origin)
3694         spec = &die->attrs[i];
3695     }
3696   if (spec)
3697     {
3698       struct die_info *ref_die =
3699       follow_die_ref (dwarf2_get_ref_die_offset (spec));
3700
3701       if (ref_die)
3702         return dwarf_attr (ref_die, name);
3703     }
3704
3705   return NULL;
3706 }
3707
3708 static int
3709 die_is_declaration (struct die_info *die)
3710 {
3711   return (dwarf_attr (die, DW_AT_declaration)
3712           && ! dwarf_attr (die, DW_AT_specification));
3713 }
3714
3715 /* Decode the line number information for the compilation unit whose
3716    line number info is at OFFSET in the .debug_line section.
3717    The compilation directory of the file is passed in COMP_DIR.  */
3718
3719 struct filenames
3720 {
3721   unsigned int num_files;
3722   struct fileinfo
3723     {
3724       char *name;
3725       unsigned int dir;
3726       unsigned int time;
3727       unsigned int size;
3728     }
3729    *files;
3730 };
3731
3732 struct directories
3733   {
3734     unsigned int num_dirs;
3735     char **dirs;
3736   };
3737
3738 static void
3739 dwarf_decode_lines (offset, comp_dir, abfd)
3740      unsigned int offset;
3741      char *comp_dir;
3742      bfd *abfd;
3743 {
3744   char *line_ptr;
3745   char *line_end;
3746   struct line_head lh;
3747   struct cleanup *back_to;
3748   unsigned int i, bytes_read;
3749   char *cur_file, *cur_dir;
3750   unsigned char op_code, extended_op, adj_opcode;
3751
3752 #define FILE_ALLOC_CHUNK 5
3753 #define DIR_ALLOC_CHUNK 5
3754
3755   struct filenames files;
3756   struct directories dirs;
3757
3758   if (dwarf_line_buffer == NULL)
3759     {
3760       complain (&dwarf2_missing_line_number_section);
3761       return;
3762     }
3763
3764   files.num_files = 0;
3765   files.files = NULL;
3766
3767   dirs.num_dirs = 0;
3768   dirs.dirs = NULL;
3769
3770   line_ptr = dwarf_line_buffer + offset;
3771
3772   /* read in the prologue */
3773   lh.total_length = read_4_bytes (abfd, line_ptr);
3774   line_ptr += 4;
3775   line_end = line_ptr + lh.total_length;
3776   lh.version = read_2_bytes (abfd, line_ptr);
3777   line_ptr += 2;
3778   lh.prologue_length = read_4_bytes (abfd, line_ptr);
3779   line_ptr += 4;
3780   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
3781   line_ptr += 1;
3782   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
3783   line_ptr += 1;
3784   lh.line_base = read_1_signed_byte (abfd, line_ptr);
3785   line_ptr += 1;
3786   lh.line_range = read_1_byte (abfd, line_ptr);
3787   line_ptr += 1;
3788   lh.opcode_base = read_1_byte (abfd, line_ptr);
3789   line_ptr += 1;
3790   lh.standard_opcode_lengths = (unsigned char *)
3791     xmalloc (lh.opcode_base * sizeof (unsigned char));
3792   back_to = make_cleanup ((make_cleanup_func) free_current_contents,
3793                           &lh.standard_opcode_lengths);
3794
3795   lh.standard_opcode_lengths[0] = 1;
3796   for (i = 1; i < lh.opcode_base; ++i)
3797     {
3798       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
3799       line_ptr += 1;
3800     }
3801
3802   /* Read directory table  */
3803   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3804     {
3805       line_ptr += bytes_read;
3806       if ((dirs.num_dirs % DIR_ALLOC_CHUNK) == 0)
3807         {
3808           dirs.dirs = (char **)
3809             xrealloc (dirs.dirs,
3810                       (dirs.num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
3811           if (dirs.num_dirs == 0)
3812             make_cleanup ((make_cleanup_func) free_current_contents, &dirs.dirs);
3813         }
3814       dirs.dirs[dirs.num_dirs++] = cur_dir;
3815     }
3816   line_ptr += bytes_read;
3817
3818   /* Read file name table */
3819   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
3820     {
3821       line_ptr += bytes_read;
3822       if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3823         {
3824           files.files = (struct fileinfo *)
3825             xrealloc (files.files,
3826                       (files.num_files + FILE_ALLOC_CHUNK)
3827                       * sizeof (struct fileinfo));
3828           if (files.num_files == 0)
3829             make_cleanup ((make_cleanup_func) free_current_contents,
3830                           &files.files);
3831         }
3832       files.files[files.num_files].name = cur_file;
3833       files.files[files.num_files].dir =
3834         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3835       line_ptr += bytes_read;
3836       files.files[files.num_files].time =
3837         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3838       line_ptr += bytes_read;
3839       files.files[files.num_files].size =
3840         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3841       line_ptr += bytes_read;
3842       files.num_files++;
3843     }
3844   line_ptr += bytes_read;
3845
3846   /* Read the statement sequences until there's nothing left.  */
3847   while (line_ptr < line_end)
3848     {
3849       /* state machine registers  */
3850       CORE_ADDR address = 0;
3851       unsigned int file = 1;
3852       unsigned int line = 1;
3853       unsigned int column = 0;
3854       int is_stmt = lh.default_is_stmt;
3855       int basic_block = 0;
3856       int end_sequence = 0;
3857
3858       /* Start a subfile for the current file of the state machine.  */
3859       if (files.num_files >= file)
3860         {
3861           /* The file and directory tables are 0 based, the references
3862              are 1 based.  */
3863           dwarf2_start_subfile (files.files[file - 1].name,
3864                                 (files.files[file - 1].dir
3865                                  ? dirs.dirs[files.files[file - 1].dir - 1]
3866                                  : comp_dir));
3867         }
3868
3869       /* Decode the table. */
3870       while (!end_sequence)
3871         {
3872           op_code = read_1_byte (abfd, line_ptr);
3873           line_ptr += 1;
3874           switch (op_code)
3875             {
3876             case DW_LNS_extended_op:
3877               line_ptr += 1;    /* ignore length */
3878               extended_op = read_1_byte (abfd, line_ptr);
3879               line_ptr += 1;
3880               switch (extended_op)
3881                 {
3882                 case DW_LNE_end_sequence:
3883                   end_sequence = 1;
3884                   /* Don't call record_line here.  The end_sequence
3885                      instruction provides the address of the first byte
3886                      *after* the last line in the sequence; it's not the
3887                      address of any real source line.  However, the GDB
3888                      linetable structure only records the starts of lines,
3889                      not the ends.  This is a weakness of GDB.  */
3890                   break;
3891                 case DW_LNE_set_address:
3892                   address = read_address (abfd, line_ptr) + baseaddr;
3893                   line_ptr += address_size;
3894                   break;
3895                 case DW_LNE_define_file:
3896                   cur_file = read_string (abfd, line_ptr, &bytes_read);
3897                   line_ptr += bytes_read;
3898                   if ((files.num_files % FILE_ALLOC_CHUNK) == 0)
3899                     {
3900                       files.files = (struct fileinfo *)
3901                         xrealloc (files.files,
3902                                   (files.num_files + FILE_ALLOC_CHUNK)
3903                                   * sizeof (struct fileinfo));
3904                       if (files.num_files == 0)
3905                         make_cleanup ((make_cleanup_func) free_current_contents,
3906                                       &files.files);
3907                     }
3908                   files.files[files.num_files].name = cur_file;
3909                   files.files[files.num_files].dir =
3910                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3911                   line_ptr += bytes_read;
3912                   files.files[files.num_files].time =
3913                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3914                   line_ptr += bytes_read;
3915                   files.files[files.num_files].size =
3916                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3917                   line_ptr += bytes_read;
3918                   files.num_files++;
3919                   break;
3920                 default:
3921                   complain (&dwarf2_mangled_line_number_section);
3922                   goto done;
3923                 }
3924               break;
3925             case DW_LNS_copy:
3926               record_line (current_subfile, line, address);
3927               basic_block = 0;
3928               break;
3929             case DW_LNS_advance_pc:
3930               address += lh.minimum_instruction_length
3931                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3932               line_ptr += bytes_read;
3933               break;
3934             case DW_LNS_advance_line:
3935               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
3936               line_ptr += bytes_read;
3937               break;
3938             case DW_LNS_set_file:
3939               /* The file and directory tables are 0 based, the references
3940                  are 1 based.  */
3941               file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3942               line_ptr += bytes_read;
3943               dwarf2_start_subfile
3944                 (files.files[file - 1].name,
3945                  (files.files[file - 1].dir
3946                   ? dirs.dirs[files.files[file - 1].dir - 1]
3947                   : comp_dir));
3948               break;
3949             case DW_LNS_set_column:
3950               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
3951               line_ptr += bytes_read;
3952               break;
3953             case DW_LNS_negate_stmt:
3954               is_stmt = (!is_stmt);
3955               break;
3956             case DW_LNS_set_basic_block:
3957               basic_block = 1;
3958               break;
3959             /* Add to the address register of the state machine the
3960                address increment value corresponding to special opcode
3961                255.  Ie, this value is scaled by the minimum instruction
3962                length since special opcode 255 would have scaled the
3963                the increment.  */
3964             case DW_LNS_const_add_pc:
3965               address += (lh.minimum_instruction_length
3966                           * ((255 - lh.opcode_base) / lh.line_range));
3967               break;
3968             case DW_LNS_fixed_advance_pc:
3969               address += read_2_bytes (abfd, line_ptr);
3970               line_ptr += 2;
3971               break;
3972             default:            /* special operand */
3973               adj_opcode = op_code - lh.opcode_base;
3974               address += (adj_opcode / lh.line_range)
3975                 * lh.minimum_instruction_length;
3976               line += lh.line_base + (adj_opcode % lh.line_range);
3977               /* append row to matrix using current values */
3978               record_line (current_subfile, line, address);
3979               basic_block = 1;
3980             }
3981         }
3982     }
3983 done:
3984   do_cleanups (back_to);
3985 }
3986
3987 /* Start a subfile for DWARF.  FILENAME is the name of the file and
3988    DIRNAME the name of the source directory which contains FILENAME
3989    or NULL if not known.
3990    This routine tries to keep line numbers from identical absolute and
3991    relative file names in a common subfile.
3992
3993    Using the `list' example from the GDB testsuite, which resides in
3994    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
3995    of /srcdir/list0.c yields the following debugging information for list0.c:
3996
3997    DW_AT_name:          /srcdir/list0.c
3998    DW_AT_comp_dir:              /compdir
3999    files.files[0].name: list0.h         
4000    files.files[0].dir:  /srcdir
4001    files.files[1].name: list0.c         
4002    files.files[1].dir:  /srcdir
4003
4004    The line number information for list0.c has to end up in a single
4005    subfile, so that `break /srcdir/list0.c:1' works as expected.  */
4006
4007 static void
4008 dwarf2_start_subfile (filename, dirname)
4009      char *filename;
4010      char *dirname;
4011 {
4012   /* If the filename isn't absolute, try to match an existing subfile
4013      with the full pathname.  */
4014
4015   if (*filename != '/' && dirname != NULL)
4016     {
4017       struct subfile *subfile;
4018       char *fullname = concat (dirname, "/", filename, NULL);
4019
4020       for (subfile = subfiles; subfile; subfile = subfile->next)
4021         {
4022           if (STREQ (subfile->name, fullname))
4023             {
4024               current_subfile = subfile;
4025               free (fullname);
4026               return;
4027             }
4028         }
4029       free (fullname);
4030     }
4031   start_subfile (filename, dirname);
4032 }
4033
4034 /* Given a pointer to a DWARF information entry, figure out if we need
4035    to make a symbol table entry for it, and if so, create a new entry
4036    and return a pointer to it.
4037    If TYPE is NULL, determine symbol type from the die, otherwise
4038    used the passed type.  */
4039
4040 static struct symbol *
4041 new_symbol (die, type, objfile)
4042      struct die_info *die;
4043      struct type *type;
4044      struct objfile *objfile;
4045 {
4046   struct symbol *sym = NULL;
4047   char *name;
4048   struct attribute *attr = NULL;
4049   struct attribute *attr2 = NULL;
4050   CORE_ADDR addr;
4051
4052   name = dwarf2_linkage_name (die);
4053   if (name)
4054     {
4055       sym = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
4056                                              sizeof (struct symbol));
4057       OBJSTAT (objfile, n_syms++);
4058       memset (sym, 0, sizeof (struct symbol));
4059       SYMBOL_NAME (sym) = obsavestring (name, strlen (name),
4060                                         &objfile->symbol_obstack);
4061
4062       /* Default assumptions.
4063          Use the passed type or decode it from the die.  */
4064       SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4065       SYMBOL_CLASS (sym) = LOC_STATIC;
4066       if (type != NULL)
4067         SYMBOL_TYPE (sym) = type;
4068       else
4069         SYMBOL_TYPE (sym) = die_type (die, objfile);
4070       attr = dwarf_attr (die, DW_AT_decl_line);
4071       if (attr)
4072         {
4073           SYMBOL_LINE (sym) = DW_UNSND (attr);
4074         }
4075
4076       /* If this symbol is from a C++ compilation, then attempt to
4077          cache the demangled form for future reference.  This is a
4078          typical time versus space tradeoff, that was decided in favor
4079          of time because it sped up C++ symbol lookups by a factor of
4080          about 20. */
4081
4082       SYMBOL_LANGUAGE (sym) = cu_language;
4083       SYMBOL_INIT_DEMANGLED_NAME (sym, &objfile->symbol_obstack);
4084       switch (die->tag)
4085         {
4086         case DW_TAG_label:
4087           attr = dwarf_attr (die, DW_AT_low_pc);
4088           if (attr)
4089             {
4090               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
4091             }
4092           SYMBOL_CLASS (sym) = LOC_LABEL;
4093           break;
4094         case DW_TAG_subprogram:
4095           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
4096              finish_block.  */
4097           SYMBOL_CLASS (sym) = LOC_BLOCK;
4098           attr2 = dwarf_attr (die, DW_AT_external);
4099           if (attr2 && (DW_UNSND (attr2) != 0))
4100             {
4101               add_symbol_to_list (sym, &global_symbols);
4102             }
4103           else
4104             {
4105               add_symbol_to_list (sym, list_in_scope);
4106             }
4107           break;
4108         case DW_TAG_variable:
4109           /* Compilation with minimal debug info may result in variables
4110              with missing type entries. Change the misleading `void' type
4111              to something sensible.  */
4112           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
4113             SYMBOL_TYPE (sym) = init_type (TYPE_CODE_INT,
4114                                            TARGET_INT_BIT / HOST_CHAR_BIT, 0,
4115                                            "<variable, no debug info>",
4116                                            objfile);
4117           attr = dwarf_attr (die, DW_AT_const_value);
4118           if (attr)
4119             {
4120               dwarf2_const_value (attr, sym, objfile);
4121               attr2 = dwarf_attr (die, DW_AT_external);
4122               if (attr2 && (DW_UNSND (attr2) != 0))
4123                 add_symbol_to_list (sym, &global_symbols);
4124               else
4125                 add_symbol_to_list (sym, list_in_scope);
4126               break;
4127             }
4128           attr = dwarf_attr (die, DW_AT_location);
4129           if (attr)
4130             {
4131               attr2 = dwarf_attr (die, DW_AT_external);
4132               if (attr2 && (DW_UNSND (attr2) != 0))
4133                 {
4134                   SYMBOL_VALUE_ADDRESS (sym) =
4135                     decode_locdesc (DW_BLOCK (attr), objfile);
4136                   add_symbol_to_list (sym, &global_symbols);
4137
4138                   /* In shared libraries the address of the variable
4139                      in the location descriptor might still be relocatable,
4140                      so its value could be zero.
4141                      Enter the symbol as a LOC_UNRESOLVED symbol, if its
4142                      value is zero, the address of the variable will then
4143                      be determined from the minimal symbol table whenever
4144                      the variable is referenced.  */
4145                   if (SYMBOL_VALUE_ADDRESS (sym))
4146                     {
4147                       SYMBOL_VALUE_ADDRESS (sym) += baseaddr;
4148                       SYMBOL_CLASS (sym) = LOC_STATIC;
4149                     }
4150                   else
4151                     SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4152                 }
4153               else
4154                 {
4155                   SYMBOL_VALUE (sym) = addr =
4156                     decode_locdesc (DW_BLOCK (attr), objfile);
4157                   add_symbol_to_list (sym, list_in_scope);
4158                   if (optimized_out)
4159                     {
4160                       SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
4161                     }
4162                   else if (isreg)
4163                     {
4164                       SYMBOL_CLASS (sym) = LOC_REGISTER;
4165                     }
4166                   else if (offreg)
4167                     {
4168                       SYMBOL_CLASS (sym) = LOC_BASEREG;
4169                       SYMBOL_BASEREG (sym) = basereg;
4170                     }
4171                   else if (islocal)
4172                     {
4173                       SYMBOL_CLASS (sym) = LOC_LOCAL;
4174                     }
4175                   else
4176                     {
4177                       SYMBOL_CLASS (sym) = LOC_STATIC;
4178                       SYMBOL_VALUE_ADDRESS (sym) = addr + baseaddr;
4179                     }
4180                 }
4181             }
4182           else
4183             {
4184               /* We do not know the address of this symbol.
4185                  If it is an external symbol and we have type information
4186                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
4187                  The address of the variable will then be determined from
4188                  the minimal symbol table whenever the variable is
4189                  referenced.  */
4190               attr2 = dwarf_attr (die, DW_AT_external);
4191               if (attr2 && (DW_UNSND (attr2) != 0)
4192                   && dwarf_attr (die, DW_AT_type) != NULL)
4193                 {
4194                   SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
4195                   add_symbol_to_list (sym, &global_symbols);
4196                 }
4197             }
4198           break;
4199         case DW_TAG_formal_parameter:
4200           attr = dwarf_attr (die, DW_AT_location);
4201           if (attr)
4202             {
4203               SYMBOL_VALUE (sym) = decode_locdesc (DW_BLOCK (attr), objfile);
4204               if (isreg)
4205                 {
4206                   SYMBOL_CLASS (sym) = LOC_REGPARM;
4207                 }
4208               else if (offreg)
4209                 {
4210                   if (isderef)
4211                     {
4212                       if (basereg != frame_base_reg)
4213                         complain (&dwarf2_complex_location_expr);
4214                       SYMBOL_CLASS (sym) = LOC_REF_ARG;
4215                     }
4216                   else
4217                     {
4218                       SYMBOL_CLASS (sym) = LOC_BASEREG_ARG;
4219                       SYMBOL_BASEREG (sym) = basereg;
4220                     }
4221                 }
4222               else
4223                 {
4224                   SYMBOL_CLASS (sym) = LOC_ARG;
4225                 }
4226             }
4227           attr = dwarf_attr (die, DW_AT_const_value);
4228           if (attr)
4229             {
4230               dwarf2_const_value (attr, sym, objfile);
4231             }
4232           add_symbol_to_list (sym, list_in_scope);
4233           break;
4234         case DW_TAG_unspecified_parameters:
4235           /* From varargs functions; gdb doesn't seem to have any
4236              interest in this information, so just ignore it for now.
4237              (FIXME?) */
4238           break;
4239         case DW_TAG_class_type:
4240         case DW_TAG_structure_type:
4241         case DW_TAG_union_type:
4242         case DW_TAG_enumeration_type:
4243           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4244           SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
4245           add_symbol_to_list (sym, list_in_scope);
4246
4247           /* The semantics of C++ state that "struct foo { ... }" also
4248              defines a typedef for "foo". Synthesize a typedef symbol so
4249              that "ptype foo" works as expected.  */
4250           if (cu_language == language_cplus)
4251             {
4252               struct symbol *typedef_sym = (struct symbol *)
4253               obstack_alloc (&objfile->symbol_obstack,
4254                              sizeof (struct symbol));
4255               *typedef_sym = *sym;
4256               SYMBOL_NAMESPACE (typedef_sym) = VAR_NAMESPACE;
4257               if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
4258                 TYPE_NAME (SYMBOL_TYPE (sym)) =
4259                   obsavestring (SYMBOL_NAME (sym),
4260                                 strlen (SYMBOL_NAME (sym)),
4261                                 &objfile->type_obstack);
4262               add_symbol_to_list (typedef_sym, list_in_scope);
4263             }
4264           break;
4265         case DW_TAG_typedef:
4266         case DW_TAG_base_type:
4267           SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4268           SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
4269           add_symbol_to_list (sym, list_in_scope);
4270           break;
4271         case DW_TAG_enumerator:
4272           attr = dwarf_attr (die, DW_AT_const_value);
4273           if (attr)
4274             {
4275               dwarf2_const_value (attr, sym, objfile);
4276             }
4277           add_symbol_to_list (sym, list_in_scope);
4278           break;
4279         default:
4280           /* Not a tag we recognize.  Hopefully we aren't processing
4281              trash data, but since we must specifically ignore things
4282              we don't recognize, there is nothing else we should do at
4283              this point. */
4284           complain (&dwarf2_unsupported_tag, dwarf_tag_name (die->tag));
4285           break;
4286         }
4287     }
4288   return (sym);
4289 }
4290
4291 /* Copy constant value from an attribute to a symbol.  */
4292
4293 static void
4294 dwarf2_const_value (attr, sym, objfile)
4295      struct attribute *attr;
4296      struct symbol *sym;
4297      struct objfile *objfile;
4298 {
4299   struct dwarf_block *blk;
4300
4301   switch (attr->form)
4302     {
4303     case DW_FORM_addr:
4304       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != (unsigned int) address_size)
4305         complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4306                   address_size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4307       SYMBOL_VALUE_BYTES (sym) = (char *)
4308         obstack_alloc (&objfile->symbol_obstack, address_size);
4309       store_address (SYMBOL_VALUE_BYTES (sym), address_size, DW_ADDR (attr));
4310       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4311       break;
4312     case DW_FORM_block1:
4313     case DW_FORM_block2:
4314     case DW_FORM_block4:
4315     case DW_FORM_block:
4316       blk = DW_BLOCK (attr);
4317       if (TYPE_LENGTH (SYMBOL_TYPE (sym)) != blk->size)
4318         complain (&dwarf2_const_value_length_mismatch, SYMBOL_NAME (sym),
4319                   blk->size, TYPE_LENGTH (SYMBOL_TYPE (sym)));
4320       SYMBOL_VALUE_BYTES (sym) = (char *)
4321         obstack_alloc (&objfile->symbol_obstack, blk->size);
4322       memcpy (SYMBOL_VALUE_BYTES (sym), blk->data, blk->size);
4323       SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
4324       break;
4325
4326       /* The DW_AT_const_value attributes are supposed to carry the
4327          symbol's value "represented as it would be on the target
4328          architecture."  By the time we get here, it's already been
4329          converted to host endianness, so we just need to sign- or
4330          zero-extend it as appropriate.  */
4331     case DW_FORM_data1:
4332       dwarf2_const_value_data (attr, sym, 8);
4333       break;
4334     case DW_FORM_data2:
4335       dwarf2_const_value_data (attr, sym, 16);
4336       break;
4337     case DW_FORM_data4:
4338       dwarf2_const_value_data (attr, sym, 32);
4339       break;
4340     case DW_FORM_data8:
4341       dwarf2_const_value_data (attr, sym, 64);
4342       break;
4343
4344     case DW_FORM_sdata:
4345       SYMBOL_VALUE (sym) = DW_SND (attr);
4346       SYMBOL_CLASS (sym) = LOC_CONST;
4347       break;
4348
4349     case DW_FORM_udata:
4350       SYMBOL_VALUE (sym) = DW_UNSND (attr);
4351       SYMBOL_CLASS (sym) = LOC_CONST;
4352       break;
4353
4354     default:
4355       complain (&dwarf2_unsupported_const_value_attr,
4356                 dwarf_form_name (attr->form));
4357       SYMBOL_VALUE (sym) = 0;
4358       SYMBOL_CLASS (sym) = LOC_CONST;
4359       break;
4360     }
4361 }
4362
4363
4364 /* Given an attr with a DW_FORM_dataN value in host byte order, sign-
4365    or zero-extend it as appropriate for the symbol's type.  */
4366 static void
4367 dwarf2_const_value_data (struct attribute *attr,
4368                          struct symbol *sym,
4369                          int bits)
4370 {
4371   LONGEST l = DW_UNSND (attr);
4372
4373   if (bits < sizeof (l) * 8)
4374     {
4375       if (TYPE_UNSIGNED (SYMBOL_TYPE (sym)))
4376         l &= ((LONGEST) 1 << bits) - 1;
4377       else
4378         l = (l << (sizeof (l) - bits)) >> (sizeof (l) - bits);
4379     }
4380
4381   SYMBOL_VALUE (sym) = l;
4382   SYMBOL_CLASS (sym) = LOC_CONST;
4383 }
4384
4385
4386 /* Return the type of the die in question using its DW_AT_type attribute.  */
4387
4388 static struct type *
4389 die_type (die, objfile)
4390      struct die_info *die;
4391      struct objfile *objfile;
4392 {
4393   struct type *type;
4394   struct attribute *type_attr;
4395   struct die_info *type_die;
4396   unsigned int ref;
4397
4398   type_attr = dwarf_attr (die, DW_AT_type);
4399   if (!type_attr)
4400     {
4401       /* A missing DW_AT_type represents a void type.  */
4402       return dwarf2_fundamental_type (objfile, FT_VOID);
4403     }
4404   else
4405     {
4406       ref = dwarf2_get_ref_die_offset (type_attr);
4407       type_die = follow_die_ref (ref);
4408       if (!type_die)
4409         {
4410           error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4411           return NULL;
4412         }
4413     }
4414   type = tag_type_to_type (type_die, objfile);
4415   if (!type)
4416     {
4417       dump_die (type_die);
4418       error ("Dwarf Error: Problem turning type die at offset into gdb type.");
4419     }
4420   return type;
4421 }
4422
4423 /* Return the containing type of the die in question using its
4424    DW_AT_containing_type attribute.  */
4425
4426 static struct type *
4427 die_containing_type (die, objfile)
4428      struct die_info *die;
4429      struct objfile *objfile;
4430 {
4431   struct type *type = NULL;
4432   struct attribute *type_attr;
4433   struct die_info *type_die = NULL;
4434   unsigned int ref;
4435
4436   type_attr = dwarf_attr (die, DW_AT_containing_type);
4437   if (type_attr)
4438     {
4439       ref = dwarf2_get_ref_die_offset (type_attr);
4440       type_die = follow_die_ref (ref);
4441       if (!type_die)
4442         {
4443           error ("Dwarf Error: Cannot find referent at offset %d.", ref);
4444           return NULL;
4445         }
4446       type = tag_type_to_type (type_die, objfile);
4447     }
4448   if (!type)
4449     {
4450       if (type_die)
4451         dump_die (type_die);
4452       error ("Dwarf Error: Problem turning containing type into gdb type.");
4453     }
4454   return type;
4455 }
4456
4457 #if 0
4458 static struct type *
4459 type_at_offset (offset, objfile)
4460      unsigned int offset;
4461      struct objfile *objfile;
4462 {
4463   struct die_info *die;
4464   struct type *type;
4465
4466   die = follow_die_ref (offset);
4467   if (!die)
4468     {
4469       error ("Dwarf Error: Cannot find type referent at offset %d.", offset);
4470       return NULL;
4471     }
4472   type = tag_type_to_type (die, objfile);
4473   return type;
4474 }
4475 #endif
4476
4477 static struct type *
4478 tag_type_to_type (die, objfile)
4479      struct die_info *die;
4480      struct objfile *objfile;
4481 {
4482   if (die->type)
4483     {
4484       return die->type;
4485     }
4486   else
4487     {
4488       read_type_die (die, objfile);
4489       if (!die->type)
4490         {
4491           dump_die (die);
4492           error ("Dwarf Error: Cannot find type of die.");
4493         }
4494       return die->type;
4495     }
4496 }
4497
4498 static void
4499 read_type_die (die, objfile)
4500      struct die_info *die;
4501      struct objfile *objfile;
4502 {
4503   switch (die->tag)
4504     {
4505     case DW_TAG_class_type:
4506     case DW_TAG_structure_type:
4507     case DW_TAG_union_type:
4508       read_structure_scope (die, objfile);
4509       break;
4510     case DW_TAG_enumeration_type:
4511       read_enumeration (die, objfile);
4512       break;
4513     case DW_TAG_subprogram:
4514     case DW_TAG_subroutine_type:
4515       read_subroutine_type (die, objfile);
4516       break;
4517     case DW_TAG_array_type:
4518       read_array_type (die, objfile);
4519       break;
4520     case DW_TAG_pointer_type:
4521       read_tag_pointer_type (die, objfile);
4522       break;
4523     case DW_TAG_ptr_to_member_type:
4524       read_tag_ptr_to_member_type (die, objfile);
4525       break;
4526     case DW_TAG_reference_type:
4527       read_tag_reference_type (die, objfile);
4528       break;
4529     case DW_TAG_const_type:
4530       read_tag_const_type (die, objfile);
4531       break;
4532     case DW_TAG_volatile_type:
4533       read_tag_volatile_type (die, objfile);
4534       break;
4535     case DW_TAG_string_type:
4536       read_tag_string_type (die, objfile);
4537       break;
4538     case DW_TAG_typedef:
4539       read_typedef (die, objfile);
4540       break;
4541     case DW_TAG_base_type:
4542       read_base_type (die, objfile);
4543       break;
4544     default:
4545       complain (&dwarf2_unexpected_tag, dwarf_tag_name (die->tag));
4546       break;
4547     }
4548 }
4549
4550 static struct type *
4551 dwarf_base_type (encoding, size, objfile)
4552      int encoding;
4553      int size;
4554      struct objfile *objfile;
4555 {
4556   /* FIXME - this should not produce a new (struct type *)
4557      every time.  It should cache base types.  */
4558   struct type *type;
4559   switch (encoding)
4560     {
4561     case DW_ATE_address:
4562       type = dwarf2_fundamental_type (objfile, FT_VOID);
4563       return type;
4564     case DW_ATE_boolean:
4565       type = dwarf2_fundamental_type (objfile, FT_BOOLEAN);
4566       return type;
4567     case DW_ATE_complex_float:
4568       if (size == 16)
4569         {
4570           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_COMPLEX);
4571         }
4572       else
4573         {
4574           type = dwarf2_fundamental_type (objfile, FT_COMPLEX);
4575         }
4576       return type;
4577     case DW_ATE_float:
4578       if (size == 8)
4579         {
4580           type = dwarf2_fundamental_type (objfile, FT_DBL_PREC_FLOAT);
4581         }
4582       else
4583         {
4584           type = dwarf2_fundamental_type (objfile, FT_FLOAT);
4585         }
4586       return type;
4587     case DW_ATE_signed:
4588       switch (size)
4589         {
4590         case 1:
4591           type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4592           break;
4593         case 2:
4594           type = dwarf2_fundamental_type (objfile, FT_SIGNED_SHORT);
4595           break;
4596         default:
4597         case 4:
4598           type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4599           break;
4600         }
4601       return type;
4602     case DW_ATE_signed_char:
4603       type = dwarf2_fundamental_type (objfile, FT_SIGNED_CHAR);
4604       return type;
4605     case DW_ATE_unsigned:
4606       switch (size)
4607         {
4608         case 1:
4609           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4610           break;
4611         case 2:
4612           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_SHORT);
4613           break;
4614         default:
4615         case 4:
4616           type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_INTEGER);
4617           break;
4618         }
4619       return type;
4620     case DW_ATE_unsigned_char:
4621       type = dwarf2_fundamental_type (objfile, FT_UNSIGNED_CHAR);
4622       return type;
4623     default:
4624       type = dwarf2_fundamental_type (objfile, FT_SIGNED_INTEGER);
4625       return type;
4626     }
4627 }
4628
4629 #if 0
4630 struct die_info *
4631 copy_die (old_die)
4632      struct die_info *old_die;
4633 {
4634   struct die_info *new_die;
4635   int i, num_attrs;
4636
4637   new_die = (struct die_info *) xmalloc (sizeof (struct die_info));
4638   memset (new_die, 0, sizeof (struct die_info));
4639
4640   new_die->tag = old_die->tag;
4641   new_die->has_children = old_die->has_children;
4642   new_die->abbrev = old_die->abbrev;
4643   new_die->offset = old_die->offset;
4644   new_die->type = NULL;
4645
4646   num_attrs = old_die->num_attrs;
4647   new_die->num_attrs = num_attrs;
4648   new_die->attrs = (struct attribute *)
4649     xmalloc (num_attrs * sizeof (struct attribute));
4650
4651   for (i = 0; i < old_die->num_attrs; ++i)
4652     {
4653       new_die->attrs[i].name = old_die->attrs[i].name;
4654       new_die->attrs[i].form = old_die->attrs[i].form;
4655       new_die->attrs[i].u.addr = old_die->attrs[i].u.addr;
4656     }
4657
4658   new_die->next = NULL;
4659   return new_die;
4660 }
4661 #endif
4662
4663 /* Return sibling of die, NULL if no sibling.  */
4664
4665 struct die_info *
4666 sibling_die (die)
4667      struct die_info *die;
4668 {
4669   int nesting_level = 0;
4670
4671   if (!die->has_children)
4672     {
4673       if (die->next && (die->next->tag == 0))
4674         {
4675           return NULL;
4676         }
4677       else
4678         {
4679           return die->next;
4680         }
4681     }
4682   else
4683     {
4684       do
4685         {
4686           if (die->has_children)
4687             {
4688               nesting_level++;
4689             }
4690           if (die->tag == 0)
4691             {
4692               nesting_level--;
4693             }
4694           die = die->next;
4695         }
4696       while (nesting_level);
4697       if (die && (die->tag == 0))
4698         {
4699           return NULL;
4700         }
4701       else
4702         {
4703           return die;
4704         }
4705     }
4706 }
4707
4708 /* Get linkage name of a die, return NULL if not found.  */
4709
4710 static char *
4711 dwarf2_linkage_name (die)
4712      struct die_info *die;
4713 {
4714   struct attribute *attr;
4715
4716   attr = dwarf_attr (die, DW_AT_MIPS_linkage_name);
4717   if (attr && DW_STRING (attr))
4718     return DW_STRING (attr);
4719   attr = dwarf_attr (die, DW_AT_name);
4720   if (attr && DW_STRING (attr))
4721     return DW_STRING (attr);
4722   return NULL;
4723 }
4724
4725 /* Convert a DIE tag into its string name.  */
4726
4727 static char *
4728 dwarf_tag_name (tag)
4729      register unsigned tag;
4730 {
4731   switch (tag)
4732     {
4733     case DW_TAG_padding:
4734       return "DW_TAG_padding";
4735     case DW_TAG_array_type:
4736       return "DW_TAG_array_type";
4737     case DW_TAG_class_type:
4738       return "DW_TAG_class_type";
4739     case DW_TAG_entry_point:
4740       return "DW_TAG_entry_point";
4741     case DW_TAG_enumeration_type:
4742       return "DW_TAG_enumeration_type";
4743     case DW_TAG_formal_parameter:
4744       return "DW_TAG_formal_parameter";
4745     case DW_TAG_imported_declaration:
4746       return "DW_TAG_imported_declaration";
4747     case DW_TAG_label:
4748       return "DW_TAG_label";
4749     case DW_TAG_lexical_block:
4750       return "DW_TAG_lexical_block";
4751     case DW_TAG_member:
4752       return "DW_TAG_member";
4753     case DW_TAG_pointer_type:
4754       return "DW_TAG_pointer_type";
4755     case DW_TAG_reference_type:
4756       return "DW_TAG_reference_type";
4757     case DW_TAG_compile_unit:
4758       return "DW_TAG_compile_unit";
4759     case DW_TAG_string_type:
4760       return "DW_TAG_string_type";
4761     case DW_TAG_structure_type:
4762       return "DW_TAG_structure_type";
4763     case DW_TAG_subroutine_type:
4764       return "DW_TAG_subroutine_type";
4765     case DW_TAG_typedef:
4766       return "DW_TAG_typedef";
4767     case DW_TAG_union_type:
4768       return "DW_TAG_union_type";
4769     case DW_TAG_unspecified_parameters:
4770       return "DW_TAG_unspecified_parameters";
4771     case DW_TAG_variant:
4772       return "DW_TAG_variant";
4773     case DW_TAG_common_block:
4774       return "DW_TAG_common_block";
4775     case DW_TAG_common_inclusion:
4776       return "DW_TAG_common_inclusion";
4777     case DW_TAG_inheritance:
4778       return "DW_TAG_inheritance";
4779     case DW_TAG_inlined_subroutine:
4780       return "DW_TAG_inlined_subroutine";
4781     case DW_TAG_module:
4782       return "DW_TAG_module";
4783     case DW_TAG_ptr_to_member_type:
4784       return "DW_TAG_ptr_to_member_type";
4785     case DW_TAG_set_type:
4786       return "DW_TAG_set_type";
4787     case DW_TAG_subrange_type:
4788       return "DW_TAG_subrange_type";
4789     case DW_TAG_with_stmt:
4790       return "DW_TAG_with_stmt";
4791     case DW_TAG_access_declaration:
4792       return "DW_TAG_access_declaration";
4793     case DW_TAG_base_type:
4794       return "DW_TAG_base_type";
4795     case DW_TAG_catch_block:
4796       return "DW_TAG_catch_block";
4797     case DW_TAG_const_type:
4798       return "DW_TAG_const_type";
4799     case DW_TAG_constant:
4800       return "DW_TAG_constant";
4801     case DW_TAG_enumerator:
4802       return "DW_TAG_enumerator";
4803     case DW_TAG_file_type:
4804       return "DW_TAG_file_type";
4805     case DW_TAG_friend:
4806       return "DW_TAG_friend";
4807     case DW_TAG_namelist:
4808       return "DW_TAG_namelist";
4809     case DW_TAG_namelist_item:
4810       return "DW_TAG_namelist_item";
4811     case DW_TAG_packed_type:
4812       return "DW_TAG_packed_type";
4813     case DW_TAG_subprogram:
4814       return "DW_TAG_subprogram";
4815     case DW_TAG_template_type_param:
4816       return "DW_TAG_template_type_param";
4817     case DW_TAG_template_value_param:
4818       return "DW_TAG_template_value_param";
4819     case DW_TAG_thrown_type:
4820       return "DW_TAG_thrown_type";
4821     case DW_TAG_try_block:
4822       return "DW_TAG_try_block";
4823     case DW_TAG_variant_part:
4824       return "DW_TAG_variant_part";
4825     case DW_TAG_variable:
4826       return "DW_TAG_variable";
4827     case DW_TAG_volatile_type:
4828       return "DW_TAG_volatile_type";
4829     case DW_TAG_MIPS_loop:
4830       return "DW_TAG_MIPS_loop";
4831     case DW_TAG_format_label:
4832       return "DW_TAG_format_label";
4833     case DW_TAG_function_template:
4834       return "DW_TAG_function_template";
4835     case DW_TAG_class_template:
4836       return "DW_TAG_class_template";
4837     default:
4838       return "DW_TAG_<unknown>";
4839     }
4840 }
4841
4842 /* Convert a DWARF attribute code into its string name.  */
4843
4844 static char *
4845 dwarf_attr_name (attr)
4846      register unsigned attr;
4847 {
4848   switch (attr)
4849     {
4850     case DW_AT_sibling:
4851       return "DW_AT_sibling";
4852     case DW_AT_location:
4853       return "DW_AT_location";
4854     case DW_AT_name:
4855       return "DW_AT_name";
4856     case DW_AT_ordering:
4857       return "DW_AT_ordering";
4858     case DW_AT_subscr_data:
4859       return "DW_AT_subscr_data";
4860     case DW_AT_byte_size:
4861       return "DW_AT_byte_size";
4862     case DW_AT_bit_offset:
4863       return "DW_AT_bit_offset";
4864     case DW_AT_bit_size:
4865       return "DW_AT_bit_size";
4866     case DW_AT_element_list:
4867       return "DW_AT_element_list";
4868     case DW_AT_stmt_list:
4869       return "DW_AT_stmt_list";
4870     case DW_AT_low_pc:
4871       return "DW_AT_low_pc";
4872     case DW_AT_high_pc:
4873       return "DW_AT_high_pc";
4874     case DW_AT_language:
4875       return "DW_AT_language";
4876     case DW_AT_member:
4877       return "DW_AT_member";
4878     case DW_AT_discr:
4879       return "DW_AT_discr";
4880     case DW_AT_discr_value:
4881       return "DW_AT_discr_value";
4882     case DW_AT_visibility:
4883       return "DW_AT_visibility";
4884     case DW_AT_import:
4885       return "DW_AT_import";
4886     case DW_AT_string_length:
4887       return "DW_AT_string_length";
4888     case DW_AT_common_reference:
4889       return "DW_AT_common_reference";
4890     case DW_AT_comp_dir:
4891       return "DW_AT_comp_dir";
4892     case DW_AT_const_value:
4893       return "DW_AT_const_value";
4894     case DW_AT_containing_type:
4895       return "DW_AT_containing_type";
4896     case DW_AT_default_value:
4897       return "DW_AT_default_value";
4898     case DW_AT_inline:
4899       return "DW_AT_inline";
4900     case DW_AT_is_optional:
4901       return "DW_AT_is_optional";
4902     case DW_AT_lower_bound:
4903       return "DW_AT_lower_bound";
4904     case DW_AT_producer:
4905       return "DW_AT_producer";
4906     case DW_AT_prototyped:
4907       return "DW_AT_prototyped";
4908     case DW_AT_return_addr:
4909       return "DW_AT_return_addr";
4910     case DW_AT_start_scope:
4911       return "DW_AT_start_scope";
4912     case DW_AT_stride_size:
4913       return "DW_AT_stride_size";
4914     case DW_AT_upper_bound:
4915       return "DW_AT_upper_bound";
4916     case DW_AT_abstract_origin:
4917       return "DW_AT_abstract_origin";
4918     case DW_AT_accessibility:
4919       return "DW_AT_accessibility";
4920     case DW_AT_address_class:
4921       return "DW_AT_address_class";
4922     case DW_AT_artificial:
4923       return "DW_AT_artificial";
4924     case DW_AT_base_types:
4925       return "DW_AT_base_types";
4926     case DW_AT_calling_convention:
4927       return "DW_AT_calling_convention";
4928     case DW_AT_count:
4929       return "DW_AT_count";
4930     case DW_AT_data_member_location:
4931       return "DW_AT_data_member_location";
4932     case DW_AT_decl_column:
4933       return "DW_AT_decl_column";
4934     case DW_AT_decl_file:
4935       return "DW_AT_decl_file";
4936     case DW_AT_decl_line:
4937       return "DW_AT_decl_line";
4938     case DW_AT_declaration:
4939       return "DW_AT_declaration";
4940     case DW_AT_discr_list:
4941       return "DW_AT_discr_list";
4942     case DW_AT_encoding:
4943       return "DW_AT_encoding";
4944     case DW_AT_external:
4945       return "DW_AT_external";
4946     case DW_AT_frame_base:
4947       return "DW_AT_frame_base";
4948     case DW_AT_friend:
4949       return "DW_AT_friend";
4950     case DW_AT_identifier_case:
4951       return "DW_AT_identifier_case";
4952     case DW_AT_macro_info:
4953       return "DW_AT_macro_info";
4954     case DW_AT_namelist_items:
4955       return "DW_AT_namelist_items";
4956     case DW_AT_priority:
4957       return "DW_AT_priority";
4958     case DW_AT_segment:
4959       return "DW_AT_segment";
4960     case DW_AT_specification:
4961       return "DW_AT_specification";
4962     case DW_AT_static_link:
4963       return "DW_AT_static_link";
4964     case DW_AT_type:
4965       return "DW_AT_type";
4966     case DW_AT_use_location:
4967       return "DW_AT_use_location";
4968     case DW_AT_variable_parameter:
4969       return "DW_AT_variable_parameter";
4970     case DW_AT_virtuality:
4971       return "DW_AT_virtuality";
4972     case DW_AT_vtable_elem_location:
4973       return "DW_AT_vtable_elem_location";
4974
4975 #ifdef MIPS
4976     case DW_AT_MIPS_fde:
4977       return "DW_AT_MIPS_fde";
4978     case DW_AT_MIPS_loop_begin:
4979       return "DW_AT_MIPS_loop_begin";
4980     case DW_AT_MIPS_tail_loop_begin:
4981       return "DW_AT_MIPS_tail_loop_begin";
4982     case DW_AT_MIPS_epilog_begin:
4983       return "DW_AT_MIPS_epilog_begin";
4984     case DW_AT_MIPS_loop_unroll_factor:
4985       return "DW_AT_MIPS_loop_unroll_factor";
4986     case DW_AT_MIPS_software_pipeline_depth:
4987       return "DW_AT_MIPS_software_pipeline_depth";
4988     case DW_AT_MIPS_linkage_name:
4989       return "DW_AT_MIPS_linkage_name";
4990 #endif
4991
4992     case DW_AT_sf_names:
4993       return "DW_AT_sf_names";
4994     case DW_AT_src_info:
4995       return "DW_AT_src_info";
4996     case DW_AT_mac_info:
4997       return "DW_AT_mac_info";
4998     case DW_AT_src_coords:
4999       return "DW_AT_src_coords";
5000     case DW_AT_body_begin:
5001       return "DW_AT_body_begin";
5002     case DW_AT_body_end:
5003       return "DW_AT_body_end";
5004     default:
5005       return "DW_AT_<unknown>";
5006     }
5007 }
5008
5009 /* Convert a DWARF value form code into its string name.  */
5010
5011 static char *
5012 dwarf_form_name (form)
5013      register unsigned form;
5014 {
5015   switch (form)
5016     {
5017     case DW_FORM_addr:
5018       return "DW_FORM_addr";
5019     case DW_FORM_block2:
5020       return "DW_FORM_block2";
5021     case DW_FORM_block4:
5022       return "DW_FORM_block4";
5023     case DW_FORM_data2:
5024       return "DW_FORM_data2";
5025     case DW_FORM_data4:
5026       return "DW_FORM_data4";
5027     case DW_FORM_data8:
5028       return "DW_FORM_data8";
5029     case DW_FORM_string:
5030       return "DW_FORM_string";
5031     case DW_FORM_block:
5032       return "DW_FORM_block";
5033     case DW_FORM_block1:
5034       return "DW_FORM_block1";
5035     case DW_FORM_data1:
5036       return "DW_FORM_data1";
5037     case DW_FORM_flag:
5038       return "DW_FORM_flag";
5039     case DW_FORM_sdata:
5040       return "DW_FORM_sdata";
5041     case DW_FORM_strp:
5042       return "DW_FORM_strp";
5043     case DW_FORM_udata:
5044       return "DW_FORM_udata";
5045     case DW_FORM_ref_addr:
5046       return "DW_FORM_ref_addr";
5047     case DW_FORM_ref1:
5048       return "DW_FORM_ref1";
5049     case DW_FORM_ref2:
5050       return "DW_FORM_ref2";
5051     case DW_FORM_ref4:
5052       return "DW_FORM_ref4";
5053     case DW_FORM_ref8:
5054       return "DW_FORM_ref8";
5055     case DW_FORM_ref_udata:
5056       return "DW_FORM_ref_udata";
5057     case DW_FORM_indirect:
5058       return "DW_FORM_indirect";
5059     default:
5060       return "DW_FORM_<unknown>";
5061     }
5062 }
5063
5064 /* Convert a DWARF stack opcode into its string name.  */
5065
5066 static char *
5067 dwarf_stack_op_name (op)
5068      register unsigned op;
5069 {
5070   switch (op)
5071     {
5072     case DW_OP_addr:
5073       return "DW_OP_addr";
5074     case DW_OP_deref:
5075       return "DW_OP_deref";
5076     case DW_OP_const1u:
5077       return "DW_OP_const1u";
5078     case DW_OP_const1s:
5079       return "DW_OP_const1s";
5080     case DW_OP_const2u:
5081       return "DW_OP_const2u";
5082     case DW_OP_const2s:
5083       return "DW_OP_const2s";
5084     case DW_OP_const4u:
5085       return "DW_OP_const4u";
5086     case DW_OP_const4s:
5087       return "DW_OP_const4s";
5088     case DW_OP_const8u:
5089       return "DW_OP_const8u";
5090     case DW_OP_const8s:
5091       return "DW_OP_const8s";
5092     case DW_OP_constu:
5093       return "DW_OP_constu";
5094     case DW_OP_consts:
5095       return "DW_OP_consts";
5096     case DW_OP_dup:
5097       return "DW_OP_dup";
5098     case DW_OP_drop:
5099       return "DW_OP_drop";
5100     case DW_OP_over:
5101       return "DW_OP_over";
5102     case DW_OP_pick:
5103       return "DW_OP_pick";
5104     case DW_OP_swap:
5105       return "DW_OP_swap";
5106     case DW_OP_rot:
5107       return "DW_OP_rot";
5108     case DW_OP_xderef:
5109       return "DW_OP_xderef";
5110     case DW_OP_abs:
5111       return "DW_OP_abs";
5112     case DW_OP_and:
5113       return "DW_OP_and";
5114     case DW_OP_div:
5115       return "DW_OP_div";
5116     case DW_OP_minus:
5117       return "DW_OP_minus";
5118     case DW_OP_mod:
5119       return "DW_OP_mod";
5120     case DW_OP_mul:
5121       return "DW_OP_mul";
5122     case DW_OP_neg:
5123       return "DW_OP_neg";
5124     case DW_OP_not:
5125       return "DW_OP_not";
5126     case DW_OP_or:
5127       return "DW_OP_or";
5128     case DW_OP_plus:
5129       return "DW_OP_plus";
5130     case DW_OP_plus_uconst:
5131       return "DW_OP_plus_uconst";
5132     case DW_OP_shl:
5133       return "DW_OP_shl";
5134     case DW_OP_shr:
5135       return "DW_OP_shr";
5136     case DW_OP_shra:
5137       return "DW_OP_shra";
5138     case DW_OP_xor:
5139       return "DW_OP_xor";
5140     case DW_OP_bra:
5141       return "DW_OP_bra";
5142     case DW_OP_eq:
5143       return "DW_OP_eq";
5144     case DW_OP_ge:
5145       return "DW_OP_ge";
5146     case DW_OP_gt:
5147       return "DW_OP_gt";
5148     case DW_OP_le:
5149       return "DW_OP_le";
5150     case DW_OP_lt:
5151       return "DW_OP_lt";
5152     case DW_OP_ne:
5153       return "DW_OP_ne";
5154     case DW_OP_skip:
5155       return "DW_OP_skip";
5156     case DW_OP_lit0:
5157       return "DW_OP_lit0";
5158     case DW_OP_lit1:
5159       return "DW_OP_lit1";
5160     case DW_OP_lit2:
5161       return "DW_OP_lit2";
5162     case DW_OP_lit3:
5163       return "DW_OP_lit3";
5164     case DW_OP_lit4:
5165       return "DW_OP_lit4";
5166     case DW_OP_lit5:
5167       return "DW_OP_lit5";
5168     case DW_OP_lit6:
5169       return "DW_OP_lit6";
5170     case DW_OP_lit7:
5171       return "DW_OP_lit7";
5172     case DW_OP_lit8:
5173       return "DW_OP_lit8";
5174     case DW_OP_lit9:
5175       return "DW_OP_lit9";
5176     case DW_OP_lit10:
5177       return "DW_OP_lit10";
5178     case DW_OP_lit11:
5179       return "DW_OP_lit11";
5180     case DW_OP_lit12:
5181       return "DW_OP_lit12";
5182     case DW_OP_lit13:
5183       return "DW_OP_lit13";
5184     case DW_OP_lit14:
5185       return "DW_OP_lit14";
5186     case DW_OP_lit15:
5187       return "DW_OP_lit15";
5188     case DW_OP_lit16:
5189       return "DW_OP_lit16";
5190     case DW_OP_lit17:
5191       return "DW_OP_lit17";
5192     case DW_OP_lit18:
5193       return "DW_OP_lit18";
5194     case DW_OP_lit19:
5195       return "DW_OP_lit19";
5196     case DW_OP_lit20:
5197       return "DW_OP_lit20";
5198     case DW_OP_lit21:
5199       return "DW_OP_lit21";
5200     case DW_OP_lit22:
5201       return "DW_OP_lit22";
5202     case DW_OP_lit23:
5203       return "DW_OP_lit23";
5204     case DW_OP_lit24:
5205       return "DW_OP_lit24";
5206     case DW_OP_lit25:
5207       return "DW_OP_lit25";
5208     case DW_OP_lit26:
5209       return "DW_OP_lit26";
5210     case DW_OP_lit27:
5211       return "DW_OP_lit27";
5212     case DW_OP_lit28:
5213       return "DW_OP_lit28";
5214     case DW_OP_lit29:
5215       return "DW_OP_lit29";
5216     case DW_OP_lit30:
5217       return "DW_OP_lit30";
5218     case DW_OP_lit31:
5219       return "DW_OP_lit31";
5220     case DW_OP_reg0:
5221       return "DW_OP_reg0";
5222     case DW_OP_reg1:
5223       return "DW_OP_reg1";
5224     case DW_OP_reg2:
5225       return "DW_OP_reg2";
5226     case DW_OP_reg3:
5227       return "DW_OP_reg3";
5228     case DW_OP_reg4:
5229       return "DW_OP_reg4";
5230     case DW_OP_reg5:
5231       return "DW_OP_reg5";
5232     case DW_OP_reg6:
5233       return "DW_OP_reg6";
5234     case DW_OP_reg7:
5235       return "DW_OP_reg7";
5236     case DW_OP_reg8:
5237       return "DW_OP_reg8";
5238     case DW_OP_reg9:
5239       return "DW_OP_reg9";
5240     case DW_OP_reg10:
5241       return "DW_OP_reg10";
5242     case DW_OP_reg11:
5243       return "DW_OP_reg11";
5244     case DW_OP_reg12:
5245       return "DW_OP_reg12";
5246     case DW_OP_reg13:
5247       return "DW_OP_reg13";
5248     case DW_OP_reg14:
5249       return "DW_OP_reg14";
5250     case DW_OP_reg15:
5251       return "DW_OP_reg15";
5252     case DW_OP_reg16:
5253       return "DW_OP_reg16";
5254     case DW_OP_reg17:
5255       return "DW_OP_reg17";
5256     case DW_OP_reg18:
5257       return "DW_OP_reg18";
5258     case DW_OP_reg19:
5259       return "DW_OP_reg19";
5260     case DW_OP_reg20:
5261       return "DW_OP_reg20";
5262     case DW_OP_reg21:
5263       return "DW_OP_reg21";
5264     case DW_OP_reg22:
5265       return "DW_OP_reg22";
5266     case DW_OP_reg23:
5267       return "DW_OP_reg23";
5268     case DW_OP_reg24:
5269       return "DW_OP_reg24";
5270     case DW_OP_reg25:
5271       return "DW_OP_reg25";
5272     case DW_OP_reg26:
5273       return "DW_OP_reg26";
5274     case DW_OP_reg27:
5275       return "DW_OP_reg27";
5276     case DW_OP_reg28:
5277       return "DW_OP_reg28";
5278     case DW_OP_reg29:
5279       return "DW_OP_reg29";
5280     case DW_OP_reg30:
5281       return "DW_OP_reg30";
5282     case DW_OP_reg31:
5283       return "DW_OP_reg31";
5284     case DW_OP_breg0:
5285       return "DW_OP_breg0";
5286     case DW_OP_breg1:
5287       return "DW_OP_breg1";
5288     case DW_OP_breg2:
5289       return "DW_OP_breg2";
5290     case DW_OP_breg3:
5291       return "DW_OP_breg3";
5292     case DW_OP_breg4:
5293       return "DW_OP_breg4";
5294     case DW_OP_breg5:
5295       return "DW_OP_breg5";
5296     case DW_OP_breg6:
5297       return "DW_OP_breg6";
5298     case DW_OP_breg7:
5299       return "DW_OP_breg7";
5300     case DW_OP_breg8:
5301       return "DW_OP_breg8";
5302     case DW_OP_breg9:
5303       return "DW_OP_breg9";
5304     case DW_OP_breg10:
5305       return "DW_OP_breg10";
5306     case DW_OP_breg11:
5307       return "DW_OP_breg11";
5308     case DW_OP_breg12:
5309       return "DW_OP_breg12";
5310     case DW_OP_breg13:
5311       return "DW_OP_breg13";
5312     case DW_OP_breg14:
5313       return "DW_OP_breg14";
5314     case DW_OP_breg15:
5315       return "DW_OP_breg15";
5316     case DW_OP_breg16:
5317       return "DW_OP_breg16";
5318     case DW_OP_breg17:
5319       return "DW_OP_breg17";
5320     case DW_OP_breg18:
5321       return "DW_OP_breg18";
5322     case DW_OP_breg19:
5323       return "DW_OP_breg19";
5324     case DW_OP_breg20:
5325       return "DW_OP_breg20";
5326     case DW_OP_breg21:
5327       return "DW_OP_breg21";
5328     case DW_OP_breg22:
5329       return "DW_OP_breg22";
5330     case DW_OP_breg23:
5331       return "DW_OP_breg23";
5332     case DW_OP_breg24:
5333       return "DW_OP_breg24";
5334     case DW_OP_breg25:
5335       return "DW_OP_breg25";
5336     case DW_OP_breg26:
5337       return "DW_OP_breg26";
5338     case DW_OP_breg27:
5339       return "DW_OP_breg27";
5340     case DW_OP_breg28:
5341       return "DW_OP_breg28";
5342     case DW_OP_breg29:
5343       return "DW_OP_breg29";
5344     case DW_OP_breg30:
5345       return "DW_OP_breg30";
5346     case DW_OP_breg31:
5347       return "DW_OP_breg31";
5348     case DW_OP_regx:
5349       return "DW_OP_regx";
5350     case DW_OP_fbreg:
5351       return "DW_OP_fbreg";
5352     case DW_OP_bregx:
5353       return "DW_OP_bregx";
5354     case DW_OP_piece:
5355       return "DW_OP_piece";
5356     case DW_OP_deref_size:
5357       return "DW_OP_deref_size";
5358     case DW_OP_xderef_size:
5359       return "DW_OP_xderef_size";
5360     case DW_OP_nop:
5361       return "DW_OP_nop";
5362     default:
5363       return "OP_<unknown>";
5364     }
5365 }
5366
5367 static char *
5368 dwarf_bool_name (mybool)
5369      unsigned mybool;
5370 {
5371   if (mybool)
5372     return "TRUE";
5373   else
5374     return "FALSE";
5375 }
5376
5377 /* Convert a DWARF type code into its string name.  */
5378
5379 static char *
5380 dwarf_type_encoding_name (enc)
5381      register unsigned enc;
5382 {
5383   switch (enc)
5384     {
5385     case DW_ATE_address:
5386       return "DW_ATE_address";
5387     case DW_ATE_boolean:
5388       return "DW_ATE_boolean";
5389     case DW_ATE_complex_float:
5390       return "DW_ATE_complex_float";
5391     case DW_ATE_float:
5392       return "DW_ATE_float";
5393     case DW_ATE_signed:
5394       return "DW_ATE_signed";
5395     case DW_ATE_signed_char:
5396       return "DW_ATE_signed_char";
5397     case DW_ATE_unsigned:
5398       return "DW_ATE_unsigned";
5399     case DW_ATE_unsigned_char:
5400       return "DW_ATE_unsigned_char";
5401     default:
5402       return "DW_ATE_<unknown>";
5403     }
5404 }
5405
5406 /* Convert a DWARF call frame info operation to its string name. */
5407
5408 #if 0
5409 static char *
5410 dwarf_cfi_name (cfi_opc)
5411      register unsigned cfi_opc;
5412 {
5413   switch (cfi_opc)
5414     {
5415     case DW_CFA_advance_loc:
5416       return "DW_CFA_advance_loc";
5417     case DW_CFA_offset:
5418       return "DW_CFA_offset";
5419     case DW_CFA_restore:
5420       return "DW_CFA_restore";
5421     case DW_CFA_nop:
5422       return "DW_CFA_nop";
5423     case DW_CFA_set_loc:
5424       return "DW_CFA_set_loc";
5425     case DW_CFA_advance_loc1:
5426       return "DW_CFA_advance_loc1";
5427     case DW_CFA_advance_loc2:
5428       return "DW_CFA_advance_loc2";
5429     case DW_CFA_advance_loc4:
5430       return "DW_CFA_advance_loc4";
5431     case DW_CFA_offset_extended:
5432       return "DW_CFA_offset_extended";
5433     case DW_CFA_restore_extended:
5434       return "DW_CFA_restore_extended";
5435     case DW_CFA_undefined:
5436       return "DW_CFA_undefined";
5437     case DW_CFA_same_value:
5438       return "DW_CFA_same_value";
5439     case DW_CFA_register:
5440       return "DW_CFA_register";
5441     case DW_CFA_remember_state:
5442       return "DW_CFA_remember_state";
5443     case DW_CFA_restore_state:
5444       return "DW_CFA_restore_state";
5445     case DW_CFA_def_cfa:
5446       return "DW_CFA_def_cfa";
5447     case DW_CFA_def_cfa_register:
5448       return "DW_CFA_def_cfa_register";
5449     case DW_CFA_def_cfa_offset:
5450       return "DW_CFA_def_cfa_offset";
5451       /* SGI/MIPS specific */
5452     case DW_CFA_MIPS_advance_loc8:
5453       return "DW_CFA_MIPS_advance_loc8";
5454     default:
5455       return "DW_CFA_<unknown>";
5456     }
5457 }
5458 #endif
5459
5460 void
5461 dump_die (die)
5462      struct die_info *die;
5463 {
5464   unsigned int i;
5465
5466   fprintf (stderr, "Die: %s (abbrev = %d, offset = %d)\n",
5467            dwarf_tag_name (die->tag), die->abbrev, die->offset);
5468   fprintf (stderr, "\thas children: %s\n",
5469            dwarf_bool_name (die->has_children));
5470
5471   fprintf (stderr, "\tattributes:\n");
5472   for (i = 0; i < die->num_attrs; ++i)
5473     {
5474       fprintf (stderr, "\t\t%s (%s) ",
5475                dwarf_attr_name (die->attrs[i].name),
5476                dwarf_form_name (die->attrs[i].form));
5477       switch (die->attrs[i].form)
5478         {
5479         case DW_FORM_ref_addr:
5480         case DW_FORM_addr:
5481           fprintf (stderr, "address: ");
5482           print_address_numeric (DW_ADDR (&die->attrs[i]), 1, gdb_stderr);
5483           break;
5484         case DW_FORM_block2:
5485         case DW_FORM_block4:
5486         case DW_FORM_block:
5487         case DW_FORM_block1:
5488           fprintf (stderr, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
5489           break;
5490         case DW_FORM_data1:
5491         case DW_FORM_data2:
5492         case DW_FORM_data4:
5493         case DW_FORM_ref1:
5494         case DW_FORM_ref2:
5495         case DW_FORM_ref4:
5496         case DW_FORM_udata:
5497         case DW_FORM_sdata:
5498           fprintf (stderr, "constant: %d", DW_UNSND (&die->attrs[i]));
5499           break;
5500         case DW_FORM_string:
5501           fprintf (stderr, "string: \"%s\"",
5502                    DW_STRING (&die->attrs[i])
5503                    ? DW_STRING (&die->attrs[i]) : "");
5504           break;
5505         case DW_FORM_flag:
5506           if (DW_UNSND (&die->attrs[i]))
5507             fprintf (stderr, "flag: TRUE");
5508           else
5509             fprintf (stderr, "flag: FALSE");
5510           break;
5511         case DW_FORM_strp:      /* we do not support separate string
5512                                    section yet */
5513         case DW_FORM_indirect:  /* we do not handle indirect yet */
5514         case DW_FORM_data8:     /* we do not have 64 bit quantities */
5515         default:
5516           fprintf (stderr, "unsupported attribute form: %d.",
5517                    die->attrs[i].form);
5518         }
5519       fprintf (stderr, "\n");
5520     }
5521 }
5522
5523 void
5524 dump_die_list (die)
5525      struct die_info *die;
5526 {
5527   while (die)
5528     {
5529       dump_die (die);
5530       die = die->next;
5531     }
5532 }
5533
5534 void
5535 store_in_ref_table (offset, die)
5536      unsigned int offset;
5537      struct die_info *die;
5538 {
5539   int h;
5540   struct die_info *old;
5541
5542   h = (offset % REF_HASH_SIZE);
5543   old = die_ref_table[h];
5544   die->next_ref = old;
5545   die_ref_table[h] = die;
5546 }
5547
5548
5549 static void
5550 dwarf2_empty_die_ref_table ()
5551 {
5552   memset (die_ref_table, 0, sizeof (die_ref_table));
5553 }
5554
5555 static unsigned int
5556 dwarf2_get_ref_die_offset (attr)
5557      struct attribute *attr;
5558 {
5559   unsigned int result = 0;
5560
5561   switch (attr->form)
5562     {
5563     case DW_FORM_ref_addr:
5564       result = DW_ADDR (attr);
5565       break;
5566     case DW_FORM_ref1:
5567     case DW_FORM_ref2:
5568     case DW_FORM_ref4:
5569     case DW_FORM_ref_udata:
5570       result = cu_header_offset + DW_UNSND (attr);
5571       break;
5572     default:
5573       complain (&dwarf2_unsupported_die_ref_attr, dwarf_form_name (attr->form));
5574     }
5575   return result;
5576 }
5577
5578 struct die_info *
5579 follow_die_ref (offset)
5580      unsigned int offset;
5581 {
5582   struct die_info *die;
5583   int h;
5584
5585   h = (offset % REF_HASH_SIZE);
5586   die = die_ref_table[h];
5587   while (die)
5588     {
5589       if (die->offset == offset)
5590         {
5591           return die;
5592         }
5593       die = die->next_ref;
5594     }
5595   return NULL;
5596 }
5597
5598 static struct type *
5599 dwarf2_fundamental_type (objfile, typeid)
5600      struct objfile *objfile;
5601      int typeid;
5602 {
5603   if (typeid < 0 || typeid >= FT_NUM_MEMBERS)
5604     {
5605       error ("Dwarf Error: internal error - invalid fundamental type id %d.",
5606              typeid);
5607     }
5608
5609   /* Look for this particular type in the fundamental type vector.  If
5610      one is not found, create and install one appropriate for the
5611      current language and the current target machine. */
5612
5613   if (ftypes[typeid] == NULL)
5614     {
5615       ftypes[typeid] = cu_language_defn->la_fund_type (objfile, typeid);
5616     }
5617
5618   return (ftypes[typeid]);
5619 }
5620
5621 /* Decode simple location descriptions.
5622    Given a pointer to a dwarf block that defines a location, compute
5623    the location and return the value.
5624
5625    FIXME: This is a kludge until we figure out a better
5626    way to handle the location descriptions.
5627    Gdb's design does not mesh well with the DWARF2 notion of a location
5628    computing interpreter, which is a shame because the flexibility goes unused.
5629    FIXME: Implement more operations as necessary.
5630
5631    A location description containing no operations indicates that the
5632    object is optimized out. The global optimized_out flag is set for
5633    those, the return value is meaningless.
5634
5635    When the result is a register number, the global isreg flag is set,
5636    otherwise it is cleared.
5637
5638    When the result is a base register offset, the global offreg flag is set
5639    and the register number is returned in basereg, otherwise it is cleared.
5640
5641    When the DW_OP_fbreg operation is encountered without a corresponding
5642    DW_AT_frame_base attribute, the global islocal flag is set.
5643    Hopefully the machine dependent code knows how to set up a virtual
5644    frame pointer for the local references.
5645
5646    Note that stack[0] is unused except as a default error return.
5647    Note that stack overflow is not yet handled.  */
5648
5649 static CORE_ADDR
5650 decode_locdesc (blk, objfile)
5651      struct dwarf_block *blk;
5652      struct objfile *objfile;
5653 {
5654   int i;
5655   int size = blk->size;
5656   char *data = blk->data;
5657   CORE_ADDR stack[64];
5658   int stacki;
5659   unsigned int bytes_read, unsnd;
5660   unsigned char op;
5661
5662   i = 0;
5663   stacki = 0;
5664   stack[stacki] = 0;
5665   isreg = 0;
5666   offreg = 0;
5667   isderef = 0;
5668   islocal = 0;
5669   optimized_out = 1;
5670
5671   while (i < size)
5672     {
5673       optimized_out = 0;
5674       op = data[i++];
5675       switch (op)
5676         {
5677         case DW_OP_reg0:
5678         case DW_OP_reg1:
5679         case DW_OP_reg2:
5680         case DW_OP_reg3:
5681         case DW_OP_reg4:
5682         case DW_OP_reg5:
5683         case DW_OP_reg6:
5684         case DW_OP_reg7:
5685         case DW_OP_reg8:
5686         case DW_OP_reg9:
5687         case DW_OP_reg10:
5688         case DW_OP_reg11:
5689         case DW_OP_reg12:
5690         case DW_OP_reg13:
5691         case DW_OP_reg14:
5692         case DW_OP_reg15:
5693         case DW_OP_reg16:
5694         case DW_OP_reg17:
5695         case DW_OP_reg18:
5696         case DW_OP_reg19:
5697         case DW_OP_reg20:
5698         case DW_OP_reg21:
5699         case DW_OP_reg22:
5700         case DW_OP_reg23:
5701         case DW_OP_reg24:
5702         case DW_OP_reg25:
5703         case DW_OP_reg26:
5704         case DW_OP_reg27:
5705         case DW_OP_reg28:
5706         case DW_OP_reg29:
5707         case DW_OP_reg30:
5708         case DW_OP_reg31:
5709           isreg = 1;
5710           stack[++stacki] = op - DW_OP_reg0;
5711           break;
5712
5713         case DW_OP_regx:
5714           isreg = 1;
5715           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5716           i += bytes_read;
5717 #if defined(HARRIS_TARGET) && defined(_M88K)
5718           /* The Harris 88110 gdb ports have long kept their special reg
5719              numbers between their gp-regs and their x-regs.  This is
5720              not how our dwarf is generated.  Punt. */
5721           unsnd += 6;
5722 #endif
5723           stack[++stacki] = unsnd;
5724           break;
5725
5726         case DW_OP_breg0:
5727         case DW_OP_breg1:
5728         case DW_OP_breg2:
5729         case DW_OP_breg3:
5730         case DW_OP_breg4:
5731         case DW_OP_breg5:
5732         case DW_OP_breg6:
5733         case DW_OP_breg7:
5734         case DW_OP_breg8:
5735         case DW_OP_breg9:
5736         case DW_OP_breg10:
5737         case DW_OP_breg11:
5738         case DW_OP_breg12:
5739         case DW_OP_breg13:
5740         case DW_OP_breg14:
5741         case DW_OP_breg15:
5742         case DW_OP_breg16:
5743         case DW_OP_breg17:
5744         case DW_OP_breg18:
5745         case DW_OP_breg19:
5746         case DW_OP_breg20:
5747         case DW_OP_breg21:
5748         case DW_OP_breg22:
5749         case DW_OP_breg23:
5750         case DW_OP_breg24:
5751         case DW_OP_breg25:
5752         case DW_OP_breg26:
5753         case DW_OP_breg27:
5754         case DW_OP_breg28:
5755         case DW_OP_breg29:
5756         case DW_OP_breg30:
5757         case DW_OP_breg31:
5758           offreg = 1;
5759           basereg = op - DW_OP_breg0;
5760           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5761           i += bytes_read;
5762           break;
5763
5764         case DW_OP_bregx:
5765           offreg = 1;
5766           basereg = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5767           i += bytes_read;
5768           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5769           i += bytes_read;
5770           break;
5771
5772         case DW_OP_fbreg:
5773           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5774           i += bytes_read;
5775           if (frame_base_reg >= 0)
5776             {
5777               offreg = 1;
5778               basereg = frame_base_reg;
5779               stack[stacki] += frame_base_offset;
5780             }
5781           else
5782             {
5783               complain (&dwarf2_missing_at_frame_base);
5784               islocal = 1;
5785             }
5786           break;
5787
5788         case DW_OP_addr:
5789           stack[++stacki] = read_address (objfile->obfd, &data[i]);
5790           i += address_size;
5791           break;
5792
5793         case DW_OP_const1u:
5794           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
5795           i += 1;
5796           break;
5797
5798         case DW_OP_const1s:
5799           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
5800           i += 1;
5801           break;
5802
5803         case DW_OP_const2u:
5804           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
5805           i += 2;
5806           break;
5807
5808         case DW_OP_const2s:
5809           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
5810           i += 2;
5811           break;
5812
5813         case DW_OP_const4u:
5814           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
5815           i += 4;
5816           break;
5817
5818         case DW_OP_const4s:
5819           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
5820           i += 4;
5821           break;
5822
5823         case DW_OP_constu:
5824           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
5825                                                   &bytes_read);
5826           i += bytes_read;
5827           break;
5828
5829         case DW_OP_consts:
5830           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
5831           i += bytes_read;
5832           break;
5833
5834         case DW_OP_plus:
5835           stack[stacki - 1] += stack[stacki];
5836           stacki--;
5837           break;
5838
5839         case DW_OP_plus_uconst:
5840           stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
5841           i += bytes_read;
5842           break;
5843
5844         case DW_OP_minus:
5845           stack[stacki - 1] = stack[stacki] - stack[stacki - 1];
5846           stacki--;
5847           break;
5848
5849         case DW_OP_deref:
5850           isderef = 1;
5851           /* If we're not the last op, then we definitely can't encode
5852              this using GDB's address_class enum.  */
5853           if (i < size)
5854             complain (&dwarf2_complex_location_expr);
5855           break;
5856
5857         default:
5858           complain (&dwarf2_unsupported_stack_op, dwarf_stack_op_name (op));
5859           return (stack[stacki]);
5860         }
5861     }
5862   return (stack[stacki]);
5863 }
5864
5865 /* memory allocation interface */
5866
5867 /* ARGSUSED */
5868 static void
5869 dwarf2_free_tmp_obstack (ignore)
5870      PTR ignore;
5871 {
5872   obstack_free (&dwarf2_tmp_obstack, NULL);
5873 }
5874
5875 static struct dwarf_block *
5876 dwarf_alloc_block ()
5877 {
5878   struct dwarf_block *blk;
5879
5880   blk = (struct dwarf_block *)
5881     obstack_alloc (&dwarf2_tmp_obstack, sizeof (struct dwarf_block));
5882   return (blk);
5883 }
5884
5885 static struct abbrev_info *
5886 dwarf_alloc_abbrev ()
5887 {
5888   struct abbrev_info *abbrev;
5889
5890   abbrev = (struct abbrev_info *) xmalloc (sizeof (struct abbrev_info));
5891   memset (abbrev, 0, sizeof (struct abbrev_info));
5892   return (abbrev);
5893 }
5894
5895 static struct die_info *
5896 dwarf_alloc_die ()
5897 {
5898   struct die_info *die;
5899
5900   die = (struct die_info *) xmalloc (sizeof (struct die_info));
5901   memset (die, 0, sizeof (struct die_info));
5902   return (die);
5903 }