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