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