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